On Thu, Aug 28, 2025 at 12:01:27AM +0200, David Hildenbrand wrote:
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.
Ah you mention this here (I wrote end of this first :)
We can now drop the nth_page() usage in sg_page_iter_page().
Acked-by: Marek Szyprowski m.szyprowski@samsung.com Signed-off-by: David Hildenbrand david@redhat.com
All LGTM, so:
Reviewed-by: Lorenzo Stoakes lorenzo.stoakes@oracle.com
include/linux/scatterlist.h | 3 ++- mm/util.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 6f8a4965f9b98..29f6ceb98d74b 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -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));
This is pretty horrible as one statement, but I guess we can't really do better, I had a quick look around for some helper that could work but nothing is clearly suitable.
So this should be fine.
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;
}
/** diff --git a/mm/util.c b/mm/util.c index 0bf349b19b652..e8b9da6b13230 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1312,5 +1312,6 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages) return false; return true; } +EXPORT_SYMBOL(page_range_contiguous);
Kinda sad that we're doing this as EXPORT_SYMBOL() rather than EXPORT_SYMBOL_GPL() :( but I guess necessary to stay consistent...
#endif
#endif /* CONFIG_MMU */
2.50.1