Skip to content

Commit 4ec85e6

Browse files
thejhjfvogel
authored andcommitted
mm/hugetlb: unshare page tables during VMA split, not before
commit 081056dc00a27bccb55ccc3c6f230a3d5fd3f7e0 upstream. Currently, __split_vma() triggers hugetlb page table unsharing through vm_ops->may_split(). This happens before the VMA lock and rmap locks are taken - which is too early, it allows racing VMA-locked page faults in our process and racing rmap walks from other processes to cause page tables to be shared again before we actually perform the split. Fix it by explicitly calling into the hugetlb unshare logic from __split_vma() in the same place where THP splitting also happens. At that point, both the VMA and the rmap(s) are write-locked. An annoying detail is that we can now call into the helper hugetlb_unshare_pmds() from two different locking contexts: 1. from hugetlb_split(), holding: - mmap lock (exclusively) - VMA lock - file rmap lock (exclusively) 2. hugetlb_unshare_all_pmds(), which I think is designed to be able to call us with only the mmap lock held (in shared mode), but currently only runs while holding mmap lock (exclusively) and VMA lock Backporting note: This commit fixes a racy protection that was introduced in commit b30c14c ("hugetlb: unshare some PMDs when splitting VMAs"); that commit claimed to fix an issue introduced in 5.13, but it should actually also go all the way back. [[email protected]: v2] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 39dde65 ("[PATCH] shared page table for hugetlb page") Signed-off-by: Jann Horn <[email protected]> Cc: Liam Howlett <[email protected]> Reviewed-by: Lorenzo Stoakes <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: <[email protected]> [b30c14c: hugetlb: unshare some PMDs when splitting VMAs] Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [stable backport: added missing include] Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 9cf5b2a3b72c23fb7b84736d5d19ee6ea718762b) Signed-off-by: Jack Vogel <[email protected]>
1 parent de457c5 commit 4ec85e6

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

include/linux/hugetlb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
272272
bool is_hugetlb_entry_migration(pte_t pte);
273273
bool is_hugetlb_entry_hwpoisoned(pte_t pte);
274274
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
275+
void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
275276

276277
#else /* !CONFIG_HUGETLB_PAGE */
277278

@@ -465,6 +466,8 @@ static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
465466

466467
static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
467468

469+
static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
470+
468471
#endif /* !CONFIG_HUGETLB_PAGE */
469472

470473
#ifndef pgd_write

mm/hugetlb.c

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
8787
static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
8888
static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
8989
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
90-
unsigned long start, unsigned long end);
90+
unsigned long start, unsigned long end, bool take_locks);
9191
static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
9292

9393
static void hugetlb_free_folio(struct folio *folio)
@@ -5075,26 +5075,40 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
50755075
{
50765076
if (addr & ~(huge_page_mask(hstate_vma(vma))))
50775077
return -EINVAL;
5078+
return 0;
5079+
}
50785080

5081+
void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
5082+
{
50795083
/*
50805084
* PMD sharing is only possible for PUD_SIZE-aligned address ranges
50815085
* in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
50825086
* split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
5087+
* This function is called in the middle of a VMA split operation, with
5088+
* MM, VMA and rmap all write-locked to prevent concurrent page table
5089+
* walks (except hardware and gup_fast()).
50835090
*/
5091+
vma_assert_write_locked(vma);
5092+
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
5093+
50845094
if (addr & ~PUD_MASK) {
5085-
/*
5086-
* hugetlb_vm_op_split is called right before we attempt to
5087-
* split the VMA. We will need to unshare PMDs in the old and
5088-
* new VMAs, so let's unshare before we split.
5089-
*/
50905095
unsigned long floor = addr & PUD_MASK;
50915096
unsigned long ceil = floor + PUD_SIZE;
50925097

5093-
if (floor >= vma->vm_start && ceil <= vma->vm_end)
5094-
hugetlb_unshare_pmds(vma, floor, ceil);
5098+
if (floor >= vma->vm_start && ceil <= vma->vm_end) {
5099+
/*
5100+
* Locking:
5101+
* Use take_locks=false here.
5102+
* The file rmap lock is already held.
5103+
* The hugetlb VMA lock can't be taken when we already
5104+
* hold the file rmap lock, and we don't need it because
5105+
* its purpose is to synchronize against concurrent page
5106+
* table walks, which are not possible thanks to the
5107+
* locks held by our caller.
5108+
*/
5109+
hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
5110+
}
50955111
}
5096-
5097-
return 0;
50985112
}
50995113

51005114
static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
@@ -7495,9 +7509,16 @@ void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int re
74957509
}
74967510
}
74977511

7512+
/*
7513+
* If @take_locks is false, the caller must ensure that no concurrent page table
7514+
* access can happen (except for gup_fast() and hardware page walks).
7515+
* If @take_locks is true, we take the hugetlb VMA lock (to lock out things like
7516+
* concurrent page fault handling) and the file rmap lock.
7517+
*/
74987518
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
74997519
unsigned long start,
7500-
unsigned long end)
7520+
unsigned long end,
7521+
bool take_locks)
75017522
{
75027523
struct hstate *h = hstate_vma(vma);
75037524
unsigned long sz = huge_page_size(h);
@@ -7521,8 +7542,12 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
75217542
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
75227543
start, end);
75237544
mmu_notifier_invalidate_range_start(&range);
7524-
hugetlb_vma_lock_write(vma);
7525-
i_mmap_lock_write(vma->vm_file->f_mapping);
7545+
if (take_locks) {
7546+
hugetlb_vma_lock_write(vma);
7547+
i_mmap_lock_write(vma->vm_file->f_mapping);
7548+
} else {
7549+
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
7550+
}
75267551
for (address = start; address < end; address += PUD_SIZE) {
75277552
ptep = hugetlb_walk(vma, address, sz);
75287553
if (!ptep)
@@ -7532,8 +7557,10 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
75327557
spin_unlock(ptl);
75337558
}
75347559
flush_hugetlb_tlb_range(vma, start, end);
7535-
i_mmap_unlock_write(vma->vm_file->f_mapping);
7536-
hugetlb_vma_unlock_write(vma);
7560+
if (take_locks) {
7561+
i_mmap_unlock_write(vma->vm_file->f_mapping);
7562+
hugetlb_vma_unlock_write(vma);
7563+
}
75377564
/*
75387565
* No need to call mmu_notifier_arch_invalidate_secondary_tlbs(), see
75397566
* Documentation/mm/mmu_notifier.rst.
@@ -7548,7 +7575,8 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
75487575
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
75497576
{
75507577
hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7551-
ALIGN_DOWN(vma->vm_end, PUD_SIZE));
7578+
ALIGN_DOWN(vma->vm_end, PUD_SIZE),
7579+
/* take_locks = */ true);
75527580
}
75537581

75547582
#ifdef CONFIG_CMA

mm/vma.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,14 @@ static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
416416
init_vma_prep(&vp, vma);
417417
vp.insert = new;
418418
vma_prepare(&vp);
419+
420+
/*
421+
* Get rid of huge pages and shared page tables straddling the split
422+
* boundary.
423+
*/
419424
vma_adjust_trans_huge(vma, vma->vm_start, addr, 0);
425+
if (is_vm_hugetlb_page(vma))
426+
hugetlb_split(vma, addr);
420427

421428
if (new_below) {
422429
vma->vm_start = addr;

mm/vma_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/file.h>
1818
#include <linux/fs.h>
1919
#include <linux/huge_mm.h>
20+
#include <linux/hugetlb.h>
2021
#include <linux/hugetlb_inline.h>
2122
#include <linux/kernel.h>
2223
#include <linux/khugepaged.h>

tools/testing/vma/vma_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
735735
(void)adjust_next;
736736
}
737737

738+
static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {}
739+
738740
static inline void vma_iter_free(struct vma_iterator *vmi)
739741
{
740742
mas_destroy(&vmi->mas);

0 commit comments

Comments
 (0)