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=1796325258; b=EtMQxAQh5NRP3r99ZkUuw6Ltv0N9LIhIvUA2oojowq0zZcaQQAdW+Ry50KJs4GsdZZpF SXfzAr3TRzfCEdhd4O7ch81HkWbbtYrvfve3X9LPZwRHaissVpJQ7Y68E5m2s/yRBM+Vj CVdKyzU8eqz1VsbRTg3xJi8LID0GJuC9H/3HWsmMr32h5tGQrd+yEBDwgHFc+q2OggYgo yoWa0NNJ9GbrrHOGyWin9bROTD+ksEj2WFC+r94s+Q0UeDqNjeXlLNZ/rdk25f07ZNT4H EheszME3kAdcHKiGxR+GnFgo/zBP0g01WsNi7DL0PYmySA8CDkP0lCggr7hh3XvnzLw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325258; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=x5sNUXbFzCbxEAG3jtg1DcfAKwnSjcqYC0CbE1u88vk=; b=It94OZcwqPN/LbQJSoB9FMnEK4JsEPA91FZMvWzGqGlB3xppFBlfn/2MdsdMwaFaYN95 y3DYahSxNViF6il86tjAD5giJQBQg1Bg9WLr+ceq1nrPI7LmDT78N/e+tR4DPkQu8Ojgg bWqz9XQTlPfMnqdLhk2CIHTdN2g+1SFg76STC1/tf0i07f+XoUR1pSt3ZYpkIXZZALDal eCjwk7fqyNmCX+tcKNL/k8A7aywubrr1EWm8LoFEwY9H6un9a2A27IHbG8vkhn6dI+sYZ lzOXz3f8wvHzDiQBBqRgw2n1QsmW9pgHJqq0UsM5lmX8kT5225X10iZBLhGWhDCimjg== 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=RJvU5+5z; 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 8E90A606A322 for ; Thu, 3 Dec 2026 19:14:18 +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=RJvU5+5z Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58B971F000C2; Thu, 3 Dec 2026 19:14:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325258; bh=x5sNUXbFzCbxEAG3jtg1DcfAKwnSjcqYC0CbE1u88vk=; h=Date:To:From:In-Reply-To:References:Subject; b=RJvU5+5zsZhfsrMaRBIx1ue81CdVWYy0IuehAlguyYig1v/GNQWNJ/TSl2VHftg5M 4O51HtmgWCSkpBOGOwD2d1jGa+sXG8yzc21vl55DyynUWuer01ozbtfI+9vTSIqZH5 oqxvlfvuSJL7V4nTaZhlN0Kgd4j8Ue+LiodbyerI= Date: Thu, 03 Dec 2026 11:14:17 -0800 To: mm-commits@vger.kernel.org,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203-hugetlb_fixes_2-v2-30-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-30-9bbeabb555d2@kernel.org> Subject: + mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch added to mm-unstable branch Message-Id: <20261203191418.58B971F000C2@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-unstable 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-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm 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: Thu, 03 Dec 2026 12:37:53 +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/20261203-hugetlb_fixes_2-v2-30-9bbeabb555d2@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: Hongfu Li Cc: Jinmeng Zhou Cc: Mina Almasry Cc: Davidlohr Bueso Cc: Naoya Horiguchi Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Vivek Kasireddy Cc: Mel Gorman Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Jason Gunthorpe Cc: Andy Whitcroft Cc: Roman Gushchin Cc: Barry Song Cc: Andrea Arcangeli Cc: Lorenzo Stoakes Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Jann Horn Cc: Guillaume Morin Cc: Yang Shi Cc: James Houghton Cc: Rik van Riel Cc: Harry Yoo Cc: Lance Yang Cc: Axel Rasmussen Cc: Peter Xu Cc: Sasha Levin Cc: Kiryl Shutsemau Cc: Christian Brauner Cc: Steven Rostedt Cc: Hongbo Li 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: Andi Kleen Cc: David Rientjes Cc: Joern Engel Cc: Aneesh Kumar K.V Cc: Gerd Hoffmann Cc: Johannes Weiner 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 @@ -4909,7 +4909,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 @@ -4920,8 +4921,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; @@ -4940,6 +4946,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 kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.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 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 hugetlbfs-seal-shm_hugetlb-files-against-growth.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 mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch