From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796007142; b=mZMcBSBPLrHlE07ZN7iv7vvEYVI2wRcMLY5GNf+mOrqHdp26WCQjfK28g9ERzLE3TQUy BB5B3epdLXP7XRC2+yFY7QwnJbm16sZPoC9RGsCf0RfG8CmTJU0LBdCJBZAJFofhnoDIP 0E6xPXPxXRaHPdj5XWBQADugIQZNTRoc6SWxGmA40wjiu+4GhpcJ/nJX5ZMCgJ1qYK4z1 lpdpba9U3Vo3P27T4/lZ+zHrO9z58dFuTM/icITpJjI4eo6s6GUdvJ9JLyWj89ROybUvn tr6Tgo98oPYgoWtA9n1/wo2oXjnPXeGkRmDhRwaHUPTxDXDgu0VJQDkL+viKg/gsRyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796007142; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=z1UR9XnQDT0aDCP7cDLKuQflyXo7wNIDI3oFpa3hyc8=; b=x1zRUcoiQbmBY9jEcLs9W9GF3w6Bc4dgk5R75lv5ZwEhjQJ2eHgLAtm2TINqM1ujm+eN db2W5NjsVzZqgaYnnkNDilg1GBSxPNFMm5KQmB/G6jpyUt9OqnAvb8tbJ2YPLziSqKHCm kgxal5xiEuNmEtHUudrKI7fEIM1wnD6gL89XCY6uUzKvghoP9VV70gZDg4mIQKMvOKLoO rJu7ocbgoj1prPIF4ydwc9jbv0N8isUdjJUP5sECQzi1t4OsyHRosp5DEHfc6m9KOmBM5 a0lowJqHERSkPQbMqzYb1sCSiaRlGWW/WcBIE2uZ6Amy7BxGoeCZaatwArKLAgWokmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=HMW5qHNq; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE381606A31D for ; Mon, 30 Nov 2026 02:52:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=HMW5qHNq Received: by smtp.kernel.org (Postfix) with ESMTPSA id 491391F00899; Mon, 30 Nov 2026 02:52:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796007142; bh=z1UR9XnQDT0aDCP7cDLKuQflyXo7wNIDI3oFpa3hyc8=; h=Date:To:From:In-Reply-To:References:Subject; b=HMW5qHNqFWeLSpYb+r6wdM9RRaodFtXHx8VHyCzRhkptLPFSzf+1KoBYnLHnpVxu0 VIZNM4qEXCGdpKK1lPSgEIOa6N3n1TiZRZNMd8eeJuWCXT+9zUfk0pvMcxCKnxMXlN ZPqv+rv0/RSRSMSaOcNm+mZFFQOz8yuVOgDdrt3E= Date: Sun, 29 Nov 2026 18:52:21 -0800 To: mm-commits@vger.kernel.org,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261125-hugetlb_fixes_2-v1-13-db27fd2f71a8@kernel.org> References: <20261125-hugetlb_fixes_2-v1-13-db27fd2f71a8@kernel.org> Subject: + mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch added to mm-new branch Message-Id: <20261130025222.491391F00899@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails has been added to the -mm mm-new branch. Its filename is mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Chris Mason Subject: mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails Date: Wed, 25 Nov 2026 23:33:11 +0100 If we split a VMA that has shared PMD page tables mapped, and allocating the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can race with a hole punch and access page tables that were already freed. __split_vma() calls hugetlb_vm_op_open() for the new VMA, which allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that allocation fails, the new VMA is left without a lock. A VMA that has shared PMD page tables needs the hugetlb VMA lock, since the lock synchronizes huge_pmd_unshare() with concurrent page table walks, including faults. want_pmd_share() and page_table_shareable() prevent new sharing without the lock, but a split leaves previously shared page tables in place for the new VMA, and hugetlb_vm_op_open() can't currently return an error if the allocation fails. This can, for example, happen for an OOM victim. Then, nothing prevents a hole punch from unsharing the page table of the new VMA while a fault in the new VMA is still using it, and the last other sharer from freeing it: fault in the new VMA hole punch, then munmap() ==================== ========================= hugetlb_vma_lock_read() no lock, does nothing huge_pte_alloc() pointer into shared table folio_zero_user() hugetlb_vma_trylock_write() no lock, returns 1 huge_pmd_unshare(new VMA) pud_clear() last sharer frees the table huge_pte_lock() set_huge_pte_at() Let's fix it by unsharing the PMD page tables of a new VMA that has no lock. Also, exit hugetlb_split() early if the original VMA has no lock, as it cannot have shared PMD page tables then. This has low urgency, as it requires allocating the hugetlb VMA lock to fail during a split, which is practically only possible for OOM victims, and a race with a hole punch. [david@kernel.org: return early if the original VMA has no lock, adjust comment and description] Link: https://patch.msgid.link/20261125-hugetlb_fixes_2-v1-13-db27fd2f71a8@kernel.org Fixes: 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping") Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Cc: Muchun Song Cc: Oscar Salvador Cc: Jinmeng Zhou Cc: Hongfu Li Cc: Andrea Arcangeli Cc: Mike Kravetz Cc: Naoya Horiguchi Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Vivek Kasireddy Cc: Gerd Hoffmann Cc: Mike Rapoport Cc: Mel Gorman Cc: Dave McCracken Cc: Andy Whitcroft Cc: Adam Litke Cc: Lorenzo Stoakes Cc: Rik van Riel Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Harry Yoo Cc: Jann Horn Cc: Lance Yang Cc: James Houghton Cc: Yang Shi Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Gregory Price Cc: Zi Yan Cc: Joshua Hahn Cc: Rakie Kim Cc: Ying Huang Cc: Matthew Brost Cc: Byungchul Park Cc: Alistair Popple Cc: Wanpeng Li Cc: Andi Kleen Cc: Kenneth W Chen Cc: Michal Hocko Cc: David Rientjes Cc: Joern Engel Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Aneesh Kumar K.V Cc: KAMEZAWA Hiroyuki Cc: Kiryl Shutsemau Cc: Sasha Levin Cc: Guillaume Morin Cc: Wanpeng Li Cc: Jason Gunthorpe Cc: luojiajun Cc: Josef Bacik Cc: Daehyeon Ko <4ncienth@gmail.com> Cc: Ackerley Tng Cc: "Oscar Salvador (SUSE)" Cc: Jiaqi Yan Cc: include/linux/hugetlb.h | 6 ++++-- mm/hugetlb.c | 17 ++++++++++++++++- mm/vma.c | 2 +- tools/testing/vma/include/stubs.h | 3 ++- 4 files changed, 23 insertions(+), 5 deletions(-) --- a/include/linux/hugetlb.h~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +++ a/include/linux/hugetlb.h @@ -274,7 +274,8 @@ long hugetlb_change_protection(struct vm unsigned long cp_flags); void hugetlb_unshare_all_pmds(struct vm_area_struct *vma); void fixup_hugetlb_reservations(struct vm_area_struct *vma); -void hugetlb_split(struct vm_area_struct *vma, unsigned long addr); +void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new, + unsigned long addr); unsigned int arch_hugetlb_cma_order(void); @@ -457,7 +458,8 @@ static inline void fixup_hugetlb_reserva { } -static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {} +static inline void hugetlb_split(struct vm_area_struct *vma, + struct vm_area_struct *new, unsigned long addr) {} #endif /* !CONFIG_HUGETLB_PAGE */ --- a/mm/hugetlb.c~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +++ a/mm/hugetlb.c @@ -4889,7 +4889,8 @@ static int hugetlb_vm_op_split(struct vm return 0; } -void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) +void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new, + unsigned long addr) { /* * PMD sharing is only possible for PUD_SIZE-aligned address ranges @@ -4900,8 +4901,13 @@ void hugetlb_split(struct vm_area_struct * walks (except hardware and gup_fast()). */ vma_assert_write_locked(vma); + vma_assert_write_locked(new); i_mmap_assert_write_locked(vma->vm_file->f_mapping); + /* Shared PMDs require a hugetlb VMA lock. */ + if (!__vma_shareable_lock(vma)) + return; + if (addr & ~PUD_MASK) { unsigned long floor = addr & PUD_MASK; unsigned long ceil = floor + PUD_SIZE; @@ -4920,6 +4926,15 @@ void hugetlb_split(struct vm_area_struct hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false); } } + + /* + * If we failed to allocate the hugetlb VMA lock, unshare everything + * that the new VMA covers. + */ + if (!__vma_shareable_lock(new)) + hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE), + ALIGN_DOWN(new->vm_end, PUD_SIZE), + /* take_locks = */ false); } static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) --- a/mm/vma.c~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +++ a/mm/vma.c @@ -590,7 +590,7 @@ __split_vma(struct vma_iterator *vmi, st */ vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL); if (vma_is_hugetlb(vma)) - hugetlb_split(vma, addr); + hugetlb_split(vma, new, addr); if (new_below) { vma->vm_start = addr; --- a/tools/testing/vma/include/stubs.h~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +++ a/tools/testing/vma/include/stubs.h @@ -466,7 +466,8 @@ static inline void vma_adjust_trans_huge { } -static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {} +static inline void hugetlb_split(struct vm_area_struct *, + struct vm_area_struct *, unsigned long) {} static inline bool vma_supports_mlock(const struct vm_area_struct *vma) { _ Patches currently in -mm which might be from mason@kernel.org are mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch