scsi: lib/sg_pool.c: clear ‘first_chunk’ in case of no preallocation [Linux 5.3]

This Linux kernel change "scsi: lib/sg_pool.c: clear ‘first_chunk’ in case of no preallocation" is included in the Linux 5.3 release. This change is authored by Ming Lei <ming.lei [at] redhat.com> on Thu Jun 6 16:34:08 2019 +0800. The commit for this change in Linux stable tree is b79d9a0 (patch).

scsi: lib/sg_pool.c: clear 'first_chunk' in case of no preallocation

If user doesn't ask to preallocate by passing zero 'nents_first_chunk' to
sg_alloc_table_chained, we need to make sure that 'first_chunk' is cleared.
Otherwise, __sg_alloc_table() still may think that the 1st SGL should be
from the preallocation.

Fixes the issue by clearing 'first_chunk' in sg_alloc_table_chained() if
'nents_first_chunk' is zero.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Ewan D. Milne <emilne@redhat.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Reported-by: Guenter Roeck <linux@roeck-us.net>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

There are 6 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 lib/sg_pool.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/sg_pool.c b/lib/sg_pool.c
index b3b8cf6..f1cc837 100644
--- a/lib/sg_pool.c
+++ b/lib/sg_pool.c
@@ -102,7 +102,9 @@ void sg_free_table_chained(struct sg_table *table,
  *
  *  Description:
  *    Allocate and chain SGLs in an sg table. If @nents@ is larger than
- *    @nents_first_chunk a chained sg table will be setup.
+ *    @nents_first_chunk a chained sg table will be setup. @first_chunk is
+ *    ignored if nents_first_chunk <= 1 because user expects the SGL points
+ *    non-chain SGL.
  *
  **/
 int sg_alloc_table_chained(struct sg_table *table, int nents,
@@ -121,7 +123,7 @@ int sg_alloc_table_chained(struct sg_table *table, int nents,
    }

    /* User supposes that the 1st SGL includes real entry */
-   if (nents_first_chunk == 1) {
+   if (nents_first_chunk <= 1) {
        first_chunk = NULL;
        nents_first_chunk = 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *