Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scatterlist: disallow non-contigous page ranges in a single SG entry

The expectation is that there is currently no user that would pass in
non-contigous page ranges: no allocator, not even VMA, will hand these
out.

The only problematic part would be if someone would provide a range
obtained directly from memblock, or manually merge problematic ranges. If
we find such cases, we should fix them to create separate SG entries.

Let's check in sg_set_page() that this is really the case. No need to
check in sg_set_folio(), as pages in a folio are guaranteed to be
contiguous. As sg_set_page() gets inlined into modules, we have to export
the page_range_contiguous() helper -- use EXPORT_SYMBOL, there is nothing
special about this helper such that we would want to enforce GPL-only
modules.

We can now drop the nth_page() usage in sg_page_iter_page().

Link: https://lkml.kernel.org/r/20250901150359.867252-25-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Hildenbrand and committed by
Andrew Morton
80e7bb74 a16c46c2

+3 -1
+2 -1
include/linux/scatterlist.h
··· 158 158 static inline void sg_set_page(struct scatterlist *sg, struct page *page, 159 159 unsigned int len, unsigned int offset) 160 160 { 161 + VM_WARN_ON_ONCE(!page_range_contiguous(page, ALIGN(len + offset, PAGE_SIZE) / PAGE_SIZE)); 161 162 sg_assign_page(sg, page); 162 163 sg->offset = offset; 163 164 sg->length = len; ··· 601 600 */ 602 601 static inline struct page *sg_page_iter_page(struct sg_page_iter *piter) 603 602 { 604 - return nth_page(sg_page(piter->sg), piter->sg_pgoffset); 603 + return sg_page(piter->sg) + piter->sg_pgoffset; 605 604 } 606 605 607 606 /**
+1
mm/util.c
··· 1315 1315 return false; 1316 1316 return true; 1317 1317 } 1318 + EXPORT_SYMBOL(page_range_contiguous); 1318 1319 #endif