Skip to content

Commit 935d4f0

Browse files
Ryan Robertsakpm00
authored andcommitted
mm: hugetlb: add huge page size param to set_huge_pte_at()
Patch series "Fix set_huge_pte_at() panic on arm64", v2. This series fixes a bug in arm64's implementation of set_huge_pte_at(), which can result in an unprivileged user causing a kernel panic. The problem was triggered when running the new uffd poison mm selftest for HUGETLB memory. This test (and the uffd poison feature) was merged for v6.5-rc7. Ideally, I'd like to get this fix in for v6.6 and I've cc'ed stable (correctly this time) to get it backported to v6.5, where the issue first showed up. Description of Bug ================== arm64's huge pte implementation supports multiple huge page sizes, some of which are implemented in the page table with multiple contiguous entries. So set_huge_pte_at() needs to work out how big the logical pte is, so that it can also work out how many physical ptes (or pmds) need to be written. It previously did this by grabbing the folio out of the pte and querying its size. However, there are cases when the pte being set is actually a swap entry. But this also used to work fine, because for huge ptes, we only ever saw migration entries and hwpoison entries. And both of these types of swap entries have a PFN embedded, so the code would grab that and everything still worked out. But over time, more calls to set_huge_pte_at() have been added that set swap entry types that do not embed a PFN. And this causes the code to go bang. The triggering case is for the uffd poison test, commit 99aa772 ("selftests/mm: add uffd unit test for UFFDIO_POISON"), which causes a PTE_MARKER_POISONED swap entry to be set, coutesey of commit 8a13897 ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs") - added in v6.5-rc7. Although review shows that there are other call sites that set PTE_MARKER_UFFD_WP (which also has no PFN), these don't trigger on arm64 because arm64 doesn't support UFFD WP. If CONFIG_DEBUG_VM is enabled, we do at least get a BUG(), but otherwise, it will dereference a bad pointer in page_folio(): static inline struct folio *hugetlb_swap_entry_to_folio(swp_entry_t entry) { VM_BUG_ON(!is_migration_entry(entry) && !is_hwpoison_entry(entry)); return page_folio(pfn_to_page(swp_offset_pfn(entry))); } Fix === The simplest fix would have been to revert the dodgy cleanup commit 18f3962 ("mm: hugetlb: kill set_huge_swap_pte_at()"), but since things have moved on, this would have required an audit of all the new set_huge_pte_at() call sites to see if they should be converted to set_huge_swap_pte_at(). As per the original intent of the change, it would also leave us open to future bugs when people invariably get it wrong and call the wrong helper. So instead, I've added a huge page size parameter to set_huge_pte_at(). This means that the arm64 code has the size in all cases. It's a bigger change, due to needing to touch the arches that implement the function, but it is entirely mechanical, so in my view, low risk. I've compile-tested all touched arches; arm64, parisc, powerpc, riscv, s390, sparc (and additionally x86_64). I've additionally booted and run mm selftests against arm64, where I observe the uffd poison test is fixed, and there are no other regressions. This patch (of 2): In order to fix a bug, arm64 needs to be told the size of the huge page for which the pte is being set in set_huge_pte_at(). Provide for this by adding an `unsigned long sz` parameter to the function. This follows the same pattern as huge_pte_clear(). This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit. No behavioral changes intended. Link: https://lkml.kernel.org/r/20230922115804.2043771-1-ryan.roberts@arm.com Link: https://lkml.kernel.org/r/20230922115804.2043771-2-ryan.roberts@arm.com Fixes: 8a13897 ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs") Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> [powerpc 8xx] Reviewed-by: Lorenzo Stoakes <lstoakes@gmail.com> [vmalloc change] Cc: Alexandre Ghiti <alex@ghiti.fr> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Axel Rasmussen <axelrasmussen@google.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christian Borntraeger <borntraeger@linux.ibm.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: David S. Miller <davem@davemloft.net> Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Helge Deller <deller@gmx.de> Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Muchun Song <muchun.song@linux.dev> Cc: Nicholas Piggin <npiggin@gmail.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Peter Xu <peterx@redhat.com> Cc: Qi Zheng <zhengqi.arch@bytedance.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: SeongJae Park <sj@kernel.org> Cc: Sven Schnelle <svens@linux.ibm.com> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Will Deacon <will@kernel.org> Cc: <stable@vger.kernel.org> [6.5+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent a8091f0 commit 935d4f0

File tree

22 files changed

+100
-49
lines changed

22 files changed

+100
-49
lines changed

arch/arm64/include/asm/hugetlb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags);
2828
#define arch_make_huge_pte arch_make_huge_pte
2929
#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT
3030
extern void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
31-
pte_t *ptep, pte_t pte);
31+
pte_t *ptep, pte_t pte, unsigned long sz);
3232
#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS
3333
extern int huge_ptep_set_access_flags(struct vm_area_struct *vma,
3434
unsigned long addr, pte_t *ptep,

arch/arm64/mm/hugetlbpage.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static inline struct folio *hugetlb_swap_entry_to_folio(swp_entry_t entry)
249249
}
250250

251251
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
252-
pte_t *ptep, pte_t pte)
252+
pte_t *ptep, pte_t pte, unsigned long sz)
253253
{
254254
size_t pgsize;
255255
int i;
@@ -571,5 +571,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr
571571
void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
572572
pte_t old_pte, pte_t pte)
573573
{
574-
set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
574+
unsigned long psize = huge_page_size(hstate_vma(vma));
575+
576+
set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
575577
}

arch/parisc/include/asm/hugetlb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT
88
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
9-
pte_t *ptep, pte_t pte);
9+
pte_t *ptep, pte_t pte, unsigned long sz);
1010

1111
#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR
1212
pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,

arch/parisc/mm/hugetlbpage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
140140
}
141141

142142
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
143-
pte_t *ptep, pte_t entry)
143+
pte_t *ptep, pte_t entry, unsigned long sz)
144144
{
145145
__set_huge_pte_at(mm, addr, ptep, entry);
146146
}

arch/powerpc/include/asm/nohash/32/hugetlb-8xx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static inline int check_and_get_huge_psize(int shift)
4646
}
4747

4848
#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT
49-
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
49+
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
50+
pte_t pte, unsigned long sz);
5051

5152
#define __HAVE_ARCH_HUGE_PTE_CLEAR
5253
static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,

arch/powerpc/mm/book3s64/hugetlbpage.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
143143
void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
144144
pte_t *ptep, pte_t old_pte, pte_t pte)
145145
{
146+
unsigned long psize;
146147

147148
if (radix_enabled())
148149
return radix__huge_ptep_modify_prot_commit(vma, addr, ptep,
149150
old_pte, pte);
150-
set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
151+
152+
psize = huge_page_size(hstate_vma(vma));
153+
set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
151154
}
152155

153156
void __init hugetlbpage_init_defaultsize(void)

arch/powerpc/mm/book3s64/radix_hugetlbpage.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void radix__huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
4747
pte_t old_pte, pte_t pte)
4848
{
4949
struct mm_struct *mm = vma->vm_mm;
50+
unsigned long psize = huge_page_size(hstate_vma(vma));
5051

5152
/*
5253
* POWER9 NMMU must flush the TLB after clearing the PTE before
@@ -58,5 +59,5 @@ void radix__huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
5859
atomic_read(&mm->context.copros) > 0)
5960
radix__flush_hugetlb_page(vma, addr);
6061

61-
set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
62+
set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
6263
}

arch/powerpc/mm/nohash/8xx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static int __ref __early_map_kernel_hugepage(unsigned long va, phys_addr_t pa,
9191
if (new && WARN_ON(pte_present(*ptep) && pgprot_val(prot)))
9292
return -EINVAL;
9393

94-
set_huge_pte_at(&init_mm, va, ptep, pte_mkhuge(pfn_pte(pa >> PAGE_SHIFT, prot)));
94+
set_huge_pte_at(&init_mm, va, ptep,
95+
pte_mkhuge(pfn_pte(pa >> PAGE_SHIFT, prot)), psize);
9596

9697
return 0;
9798
}

arch/powerpc/mm/pgtable.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
288288
}
289289

290290
#if defined(CONFIG_PPC_8xx)
291-
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
291+
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
292+
pte_t pte, unsigned long sz)
292293
{
293294
pmd_t *pmd = pmd_off(mm, addr);
294295
pte_basic_t val;

arch/riscv/include/asm/hugetlb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
1818

1919
#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT
2020
void set_huge_pte_at(struct mm_struct *mm,
21-
unsigned long addr, pte_t *ptep, pte_t pte);
21+
unsigned long addr, pte_t *ptep, pte_t pte,
22+
unsigned long sz);
2223

2324
#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR
2425
pte_t huge_ptep_get_and_clear(struct mm_struct *mm,

0 commit comments

Comments
 (0)