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 <hch@xxxxxx> Cc: Bart Van Assche <bvanassche@xxxxxxx> Cc: Ewan D. Milne <emilne@xxxxxxxxxx> Cc: Hannes Reinecke <hare@xxxxxxxx> Cc: Guenter Roeck <linux@xxxxxxxxxxxx> Fixes: c3288dd8c232 ("scsi: core: avoid pre-allocating big SGL for data") Reported-by: Guenter Roeck <linux@xxxxxxxxxxxx> Tested-by: Guenter Roeck <linux@xxxxxxxxxxxx> Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- 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