Skip to content

Commit af6cfcd

Browse files
thejhgregkh
authored andcommitted
mm/hugetlb: unshare page tables during VMA split, not before
commit 081056d 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: code got moved from mmap.c to vma.c] Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 39c8683 commit af6cfcd

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

include/linux/hugetlb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
281281

282282
bool is_hugetlb_entry_migration(pte_t pte);
283283
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
284+
void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
284285

285286
#else /* !CONFIG_HUGETLB_PAGE */
286287

@@ -491,6 +492,8 @@ static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
491492

492493
static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
493494

495+
static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
496+
494497
#endif /* !CONFIG_HUGETLB_PAGE */
495498
/*
496499
* hugepages at page global directory. If arch support

mm/hugetlb.c

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
9696
static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
9797
static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
9898
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
99-
unsigned long start, unsigned long end);
99+
unsigned long start, unsigned long end, bool take_locks);
100100
static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
101101

102102
static inline bool subpool_is_free(struct hugepage_subpool *spool)
@@ -4903,26 +4903,40 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
49034903
{
49044904
if (addr & ~(huge_page_mask(hstate_vma(vma))))
49054905
return -EINVAL;
4906+
return 0;
4907+
}
49064908

4909+
void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
4910+
{
49074911
/*
49084912
* PMD sharing is only possible for PUD_SIZE-aligned address ranges
49094913
* in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
49104914
* split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
4915+
* This function is called in the middle of a VMA split operation, with
4916+
* MM, VMA and rmap all write-locked to prevent concurrent page table
4917+
* walks (except hardware and gup_fast()).
49114918
*/
4919+
vma_assert_write_locked(vma);
4920+
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
4921+
49124922
if (addr & ~PUD_MASK) {
4913-
/*
4914-
* hugetlb_vm_op_split is called right before we attempt to
4915-
* split the VMA. We will need to unshare PMDs in the old and
4916-
* new VMAs, so let's unshare before we split.
4917-
*/
49184923
unsigned long floor = addr & PUD_MASK;
49194924
unsigned long ceil = floor + PUD_SIZE;
49204925

4921-
if (floor >= vma->vm_start && ceil <= vma->vm_end)
4922-
hugetlb_unshare_pmds(vma, floor, ceil);
4926+
if (floor >= vma->vm_start && ceil <= vma->vm_end) {
4927+
/*
4928+
* Locking:
4929+
* Use take_locks=false here.
4930+
* The file rmap lock is already held.
4931+
* The hugetlb VMA lock can't be taken when we already
4932+
* hold the file rmap lock, and we don't need it because
4933+
* its purpose is to synchronize against concurrent page
4934+
* table walks, which are not possible thanks to the
4935+
* locks held by our caller.
4936+
*/
4937+
hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
4938+
}
49234939
}
4924-
4925-
return 0;
49264940
}
49274941

49284942
static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
@@ -7305,9 +7319,16 @@ void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int re
73057319
}
73067320
}
73077321

7322+
/*
7323+
* If @take_locks is false, the caller must ensure that no concurrent page table
7324+
* access can happen (except for gup_fast() and hardware page walks).
7325+
* If @take_locks is true, we take the hugetlb VMA lock (to lock out things like
7326+
* concurrent page fault handling) and the file rmap lock.
7327+
*/
73087328
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
73097329
unsigned long start,
7310-
unsigned long end)
7330+
unsigned long end,
7331+
bool take_locks)
73117332
{
73127333
struct hstate *h = hstate_vma(vma);
73137334
unsigned long sz = huge_page_size(h);
@@ -7331,8 +7352,12 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
73317352
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
73327353
start, end);
73337354
mmu_notifier_invalidate_range_start(&range);
7334-
hugetlb_vma_lock_write(vma);
7335-
i_mmap_lock_write(vma->vm_file->f_mapping);
7355+
if (take_locks) {
7356+
hugetlb_vma_lock_write(vma);
7357+
i_mmap_lock_write(vma->vm_file->f_mapping);
7358+
} else {
7359+
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
7360+
}
73367361
for (address = start; address < end; address += PUD_SIZE) {
73377362
ptep = hugetlb_walk(vma, address, sz);
73387363
if (!ptep)
@@ -7342,8 +7367,10 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
73427367
spin_unlock(ptl);
73437368
}
73447369
flush_hugetlb_tlb_range(vma, start, end);
7345-
i_mmap_unlock_write(vma->vm_file->f_mapping);
7346-
hugetlb_vma_unlock_write(vma);
7370+
if (take_locks) {
7371+
i_mmap_unlock_write(vma->vm_file->f_mapping);
7372+
hugetlb_vma_unlock_write(vma);
7373+
}
73477374
/*
73487375
* No need to call mmu_notifier_arch_invalidate_secondary_tlbs(), see
73497376
* Documentation/mm/mmu_notifier.rst.
@@ -7358,7 +7385,8 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
73587385
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
73597386
{
73607387
hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7361-
ALIGN_DOWN(vma->vm_end, PUD_SIZE));
7388+
ALIGN_DOWN(vma->vm_end, PUD_SIZE),
7389+
/* take_locks = */ true);
73627390
}
73637391

73647392
#ifdef CONFIG_CMA

mm/mmap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,13 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
24022402
init_vma_prep(&vp, vma);
24032403
vp.insert = new;
24042404
vma_prepare(&vp);
2405+
/*
2406+
* Get rid of huge pages and shared page tables straddling the split
2407+
* boundary.
2408+
*/
24052409
vma_adjust_trans_huge(vma, vma->vm_start, addr, 0);
2410+
if (is_vm_hugetlb_page(vma))
2411+
hugetlb_split(vma, addr);
24062412

24072413
if (new_below) {
24082414
vma->vm_start = addr;

0 commit comments

Comments
 (0)