]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
scatterlist: disallow non-contigous page ranges in a single SG entry
authorDavid Hildenbrand <david@redhat.com>
Mon, 1 Sep 2025 15:03:45 +0000 (17:03 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 21 Sep 2025 21:22:06 +0000 (14:22 -0700)
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>
include/linux/scatterlist.h
mm/util.c

index 6f8a4965f9b985007a7dcc79c631a67b6e04549e..29f6ceb98d74b118d08b6a3d4eb7f62dcde0495d 100644 (file)
@@ -158,6 +158,7 @@ static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
                               unsigned int len, unsigned int offset)
 {
+       VM_WARN_ON_ONCE(!page_range_contiguous(page, ALIGN(len + offset, PAGE_SIZE) / PAGE_SIZE));
        sg_assign_page(sg, page);
        sg->offset = offset;
        sg->length = len;
@@ -600,7 +601,7 @@ void __sg_page_iter_start(struct sg_page_iter *piter,
  */
 static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
 {
-       return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
+       return sg_page(piter->sg) + piter->sg_pgoffset;
 }
 
 /**
index 4b9d40c7128681c234b4198d54ab377e12e986ec..e29d3310e26bb6b4544bec1d47004e5570e5ddbd 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -1315,4 +1315,5 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
                        return false;
        return true;
 }
+EXPORT_SYMBOL(page_range_contiguous);
 #endif