]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mm/mprotect: use long for page accountings and retval
authorPeter Xu <peterx@redhat.com>
Tue, 6 Jan 2026 11:47:13 +0000 (20:47 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 11 Jan 2026 14:19:25 +0000 (15:19 +0100)
commit a79390f5d6a78647fd70856bd42b22d994de0ba2 upstream.

Switch to use type "long" for page accountings and retval across the whole
procedure of change_protection().

The change should have shrinked the possible maximum page number to be
half comparing to previous (ULONG_MAX / 2), but it shouldn't overflow on
any system either because the maximum possible pages touched by change
protection should be ULONG_MAX / PAGE_SIZE.

Two reasons to switch from "unsigned long" to "long":

  1. It suites better on count_vm_numa_events(), whose 2nd parameter takes
     a long type.

  2. It paves way for returning negative (error) values in the future.

Currently the only caller that consumes this retval is change_prot_numa(),
where the unsigned long was converted to an int.  Since at it, touching up
the numa code to also take a long, so it'll avoid any possible overflow
too during the int-size convertion.

Link: https://lkml.kernel.org/r/20230104225207.1066932-3-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Acked-by: Mike Kravetz <mike.kravetz@oracle.com>
Acked-by: James Houghton <jthoughton@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Nadav Amit <nadav.amit@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
[ Adjust context ]
Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/hugetlb.h
include/linux/mm.h
mm/hugetlb.c
mm/mempolicy.c
mm/mprotect.c

index 26f2947c399d0226298a1889b1364e1e29a46194..1ddc2b1f96d5892bd65c4e3ae1dbcdb92069942f 100644 (file)
@@ -233,7 +233,7 @@ void hugetlb_vma_lock_release(struct kref *kref);
 
 int pmd_huge(pmd_t pmd);
 int pud_huge(pud_t pud);
-unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
+long hugetlb_change_protection(struct vm_area_struct *vma,
                unsigned long address, unsigned long end, pgprot_t newprot,
                unsigned long cp_flags);
 
@@ -447,7 +447,7 @@ static inline void move_hugetlb_state(struct page *oldpage,
 {
 }
 
-static inline unsigned long hugetlb_change_protection(
+static inline long hugetlb_change_protection(
                        struct vm_area_struct *vma, unsigned long address,
                        unsigned long end, pgprot_t newprot,
                        unsigned long cp_flags)
index c5720c0b7b772aa268b36cdc7968e2c3db78a0dc..97641304c4552e211054a6c6deefa3866f03139d 100644 (file)
@@ -2148,7 +2148,7 @@ extern unsigned long move_page_tables(struct vm_area_struct *vma,
 #define  MM_CP_UFFD_WP_ALL                 (MM_CP_UFFD_WP | \
                                            MM_CP_UFFD_WP_RESOLVE)
 
-extern unsigned long change_protection(struct mmu_gather *tlb,
+extern long change_protection(struct mmu_gather *tlb,
                              struct vm_area_struct *vma, unsigned long start,
                              unsigned long end, pgprot_t newprot,
                              unsigned long cp_flags);
index 77c1ac7a05910cd2a51efa14a2481e0acb052a1e..e7bac08071dea2bd81fea3d309a2a45640c396ee 100644 (file)
@@ -6668,7 +6668,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
        return i ? i : err;
 }
 
-unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
+long hugetlb_change_protection(struct vm_area_struct *vma,
                unsigned long address, unsigned long end,
                pgprot_t newprot, unsigned long cp_flags)
 {
@@ -6677,7 +6677,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
        pte_t *ptep;
        pte_t pte;
        struct hstate *h = hstate_vma(vma);
-       unsigned long pages = 0, psize = huge_page_size(h);
+       long pages = 0, psize = huge_page_size(h);
        bool shared_pmd = false;
        struct mmu_notifier_range range;
        unsigned long last_addr_mask;
index 399d8cb4881380214971f6b405f262c856a48ba4..97106305ce21ee731d838b8215bb33b2a77d8e51 100644 (file)
@@ -628,7 +628,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
                        unsigned long addr, unsigned long end)
 {
        struct mmu_gather tlb;
-       int nr_updated;
+       long nr_updated;
 
        tlb_gather_mmu(&tlb, vma->vm_mm);
 
index 668bfaa6ed2aebdbc64182e7d2c8a007a2d4bae7..8216f4018ee75444c21849791652ba47c8dda825 100644 (file)
@@ -72,13 +72,13 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma,
        return true;
 }
 
-static unsigned long change_pte_range(struct mmu_gather *tlb,
+static long change_pte_range(struct mmu_gather *tlb,
                struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
                unsigned long end, pgprot_t newprot, unsigned long cp_flags)
 {
        pte_t *pte, oldpte;
        spinlock_t *ptl;
-       unsigned long pages = 0;
+       long pages = 0;
        int target_node = NUMA_NO_NODE;
        bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
        bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
@@ -346,13 +346,13 @@ uffd_wp_protect_file(struct vm_area_struct *vma, unsigned long cp_flags)
                }                                                       \
        } while (0)
 
-static inline unsigned long change_pmd_range(struct mmu_gather *tlb,
+static inline long change_pmd_range(struct mmu_gather *tlb,
                struct vm_area_struct *vma, pud_t *pud, unsigned long addr,
                unsigned long end, pgprot_t newprot, unsigned long cp_flags)
 {
        pmd_t *pmd;
        unsigned long next;
-       unsigned long pages = 0;
+       long pages = 0;
        unsigned long nr_huge_updates = 0;
        struct mmu_notifier_range range;
 
@@ -360,7 +360,7 @@ static inline unsigned long change_pmd_range(struct mmu_gather *tlb,
 
        pmd = pmd_offset(pud, addr);
        do {
-               unsigned long this_pages;
+               long this_pages;
 
                next = pmd_addr_end(addr, end);
 
@@ -430,13 +430,13 @@ next:
        return pages;
 }
 
-static inline unsigned long change_pud_range(struct mmu_gather *tlb,
+static inline long change_pud_range(struct mmu_gather *tlb,
                struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr,
                unsigned long end, pgprot_t newprot, unsigned long cp_flags)
 {
        pud_t *pud;
        unsigned long next;
-       unsigned long pages = 0;
+       long pages = 0;
 
        pud = pud_offset(p4d, addr);
        do {
@@ -451,13 +451,13 @@ static inline unsigned long change_pud_range(struct mmu_gather *tlb,
        return pages;
 }
 
-static inline unsigned long change_p4d_range(struct mmu_gather *tlb,
+static inline long change_p4d_range(struct mmu_gather *tlb,
                struct vm_area_struct *vma, pgd_t *pgd, unsigned long addr,
                unsigned long end, pgprot_t newprot, unsigned long cp_flags)
 {
        p4d_t *p4d;
        unsigned long next;
-       unsigned long pages = 0;
+       long pages = 0;
 
        p4d = p4d_offset(pgd, addr);
        do {
@@ -472,14 +472,14 @@ static inline unsigned long change_p4d_range(struct mmu_gather *tlb,
        return pages;
 }
 
-static unsigned long change_protection_range(struct mmu_gather *tlb,
+static long change_protection_range(struct mmu_gather *tlb,
                struct vm_area_struct *vma, unsigned long addr,
                unsigned long end, pgprot_t newprot, unsigned long cp_flags)
 {
        struct mm_struct *mm = vma->vm_mm;
        pgd_t *pgd;
        unsigned long next;
-       unsigned long pages = 0;
+       long pages = 0;
 
        BUG_ON(addr >= end);
        pgd = pgd_offset(mm, addr);
@@ -498,12 +498,12 @@ static unsigned long change_protection_range(struct mmu_gather *tlb,
        return pages;
 }
 
-unsigned long change_protection(struct mmu_gather *tlb,
+long change_protection(struct mmu_gather *tlb,
                       struct vm_area_struct *vma, unsigned long start,
                       unsigned long end, pgprot_t newprot,
                       unsigned long cp_flags)
 {
-       unsigned long pages;
+       long pages;
 
        BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);