[PATCH 1/3] mm: use nth_page() in place of direct struct page manipulation.

From: Zi Yan
Date: Wed Aug 30 2023 - 15:30:19 EST


From: Zi Yan <ziy@xxxxxxxxxx>

When dealing with hugetlb pages, manipulating struct page pointers
directly can get to wrong struct page, since struct page is not guaranteed
to be contiguous on SPARSEMEM without VMEMMAP. Use nth_page() to handle
it properly.

Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
---
mm/cma.c | 2 +-
mm/hugetlb.c | 2 +-
mm/memory_hotplug.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/cma.c b/mm/cma.c
index da2967c6a223..2b2494fd6b59 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -505,7 +505,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,
*/
if (page) {
for (i = 0; i < count; i++)
- page_kasan_tag_reset(page + i);
+ page_kasan_tag_reset(nth_page(page, i));
}

if (ret && !no_warn) {
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index ba6d39b71cb1..77e8d2cf4eed 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6474,7 +6474,7 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
}
}

- page += ((address & ~huge_page_mask(h)) >> PAGE_SHIFT);
+ page = nth_page(page, ((address & ~huge_page_mask(h)) >> PAGE_SHIFT));

/*
* Note that page may be a sub-page, and with vmemmap
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 1b03f4ec6fd2..3b301c4023ff 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -1689,7 +1689,7 @@ static int scan_movable_pages(unsigned long start, unsigned long end,
*/
if (HPageMigratable(head))
goto found;
- skip = compound_nr(head) - (page - head);
+ skip = compound_nr(head) - (pfn - page_to_pfn(head));
pfn += skip - 1;
}
return -ENOENT;
--
2.40.1