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.

Cc: Christoph Hellwig <[email protected]>
Cc: Bart Van Assche <[email protected]>
Cc: Ewan D. Milne <[email protected]>
Cc: Hannes Reinecke <[email protected]>
Cc: Guenter Roeck <[email protected]>
Fixes: c3288dd8c232 ("scsi: core: avoid pre-allocating big SGL for data")
Reported-by: Guenter Roeck <[email protected]>
Tested-by: Guenter Roeck <[email protected]>
Signed-off-by: Ming Lei <[email protected]>
---
 lib/sg_pool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/sg_pool.c b/lib/sg_pool.c
index 47eecbe094d8..e042a1722615 100644
--- a/lib/sg_pool.c
+++ b/lib/sg_pool.c
@@ -122,7 +122,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