If user doesn't ask to pre-allocate 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 pre-allocation.

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

Without this patch, the 2nd one can't work.

Cc: Christoph Hellwig <h...@lst.de>
Cc: Bart Van Assche <bvanass...@acm.org>
Cc: Ewan D. Milne <emi...@redhat.com>
Cc: Hannes Reinecke <h...@suse.com>
Cc: Guenter Roeck <li...@roeck-us.net>
Reported-by: Guenter Roeck <li...@roeck-us.net>
Tested-by: Guenter Roeck <li...@roeck-us.net>
Signed-off-by: Ming Lei <ming....@redhat.com>
---
 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 47eecbe094d8..db29e5c1f790 100644
--- a/lib/sg_pool.c
+++ b/lib/sg_pool.c
@@ -103,7 +103,9 @@ EXPORT_SYMBOL_GPL(sg_free_table_chained);
  *
  *  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,
@@ -122,7 +124,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;
        }
-- 
2.20.1

Reply via email to