Skip to content

Commit 0f097f0

Browse files
yishaihjgunthorpe
authored andcommitted
lib/scatterlist: Fix to calculate the last_pg properly
The last_pg is wrong, it is actually the first page of the last scatterlist element. To get the last page of the last scatterlist element we have to add prv->length. So it is checking mergability against the wrong page, Further, a SG element is not guaranteed to end on a page boundary, so we have to check the sub page location also for merge eligibility. Fix the above by checking physical contiguity based on PFNs, compute the actual last page and then call pages_are_mergable(). Fixes: 1567b49 ("lib/scatterlist: add check when merging zone device pages") Link: https://lore.kernel.org/r/[email protected] Reported-by: Jason Gunthorpe <[email protected]> Signed-off-by: Yishai Hadas <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent b3deec2 commit 0f097f0

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lib/scatterlist.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,27 @@ int sg_alloc_append_table_from_pages(struct sg_append_table *sgt_append,
470470
return -EOPNOTSUPP;
471471

472472
if (sgt_append->prv) {
473+
unsigned long next_pfn = (page_to_phys(sg_page(sgt_append->prv)) +
474+
sgt_append->prv->offset + sgt_append->prv->length) / PAGE_SIZE;
475+
473476
if (WARN_ON(offset))
474477
return -EINVAL;
475478

476479
/* Merge contiguous pages into the last SG */
477480
prv_len = sgt_append->prv->length;
478-
last_pg = sg_page(sgt_append->prv);
479-
while (n_pages && pages_are_mergeable(pages[0], last_pg)) {
480-
if (sgt_append->prv->length + PAGE_SIZE > max_segment)
481-
break;
482-
sgt_append->prv->length += PAGE_SIZE;
483-
last_pg = pages[0];
484-
pages++;
485-
n_pages--;
481+
if (page_to_pfn(pages[0]) == next_pfn) {
482+
last_pg = pfn_to_page(next_pfn - 1);
483+
while (n_pages && pages_are_mergeable(pages[0], last_pg)) {
484+
if (sgt_append->prv->length + PAGE_SIZE > max_segment)
485+
break;
486+
sgt_append->prv->length += PAGE_SIZE;
487+
last_pg = pages[0];
488+
pages++;
489+
n_pages--;
490+
}
491+
if (!n_pages)
492+
goto out;
486493
}
487-
if (!n_pages)
488-
goto out;
489494
}
490495

491496
/* compute number of contiguous chunks */

0 commit comments

Comments
 (0)