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=1796007161; b=Pa+mGsbrHaAB+CMmyzeXcPj0gTAak+v9KP/RBSGZtJxrtiMM92b+IGKmdemKxJunC8Bk UU0rJSJWG7bpyKKH9l1jHuyHbGRdNzRcKoR8QtxTgl/hO8x3ghdnXbLLf9uVhAHYUY4/Y +6dJIzwDHXmgutaL8AuWl3q1J5WGu+SKwgaA8PSGC9D+cWxIkXHyGLjAotCYYKSZrFdJA VlZqdvXzDQCmr9dGdkLj1wtmJ8HtKChVycvs2JZXHD48h6HE8LJL7ClsIESdBJdKXE0IM kaKl7dfhF1OUwrRGVs9nEEw3tCtJ8GeTLIoUbTPrOErawOe/96A4T/GC2Ux5swN6iSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796007161; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=/I0gGEHFe5qRMXaenOIVT54MoZxyMpCuBEYzQYUqtn0=; b=1TbhnV2pT7UH2j6luVMxf1M9WpGpMA1YTQrVq9bMbLVmr3RpaVJD6mcDKj/+i50VARyT jTx3chZI38ggUf2g4wJLxsBkEWrwDoQAeNllp1HsUsbLk1fOmRBSzIkVYFCODUP5G0PIp 7dFkHrtWy2zVwGzWYFGjnx1rEplapPwvhq0AHldcIn/Sq9nG0MRTeI1Hb2alQXR+KccWT QEzDyoe0M3dvBzPBFQQWMVulmgmU7OEVVkutgxjDOj+Q8B6NSmr3WeNq64yxj1yjhaBHx YVP38GSOXpljZ3Wmlz/WYLkiw585TQq35h56DUGFUiY3Yc1GM/VBMc30zdDFpWV2Aog== 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=Q7VqHGDJ; 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 D729C606A31C for ; Mon, 30 Nov 2026 02:52:41 +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=Q7VqHGDJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 491501F00897; Mon, 30 Nov 2026 02:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796007161; bh=/I0gGEHFe5qRMXaenOIVT54MoZxyMpCuBEYzQYUqtn0=; h=Date:To:From:In-Reply-To:References:Subject; b=Q7VqHGDJXaM3z5DzrxlwTEdWORJ2we86uat3RHqVBhNWIpQEm1jUJwMwPGT5OjDBh p4VrNCIAmbYtqSRT9hDgMrt/fsGjU8KsP+I59YXk0m+NUD2096o8qrVu5Y7U6iPAHy fzJnXxxRpukNbxgxThKSd+jJ8sVi5/lcKjdu/Uiw= Date: Sun, 29 Nov 2026 18:52:40 -0800 To: mm-commits@vger.kernel.org,david@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261125-hugetlb_fixes_2-v1-19-db27fd2f71a8@kernel.org> References: <20261125-hugetlb_fixes_2-v1-19-db27fd2f71a8@kernel.org> Subject: + mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing.patch added to mm-new branch Message-Id: <20261130025241.491501F00897@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: use the VMA lock to stabilize flags for PMD sharing has been added to the -mm mm-new branch. Its filename is mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing.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: "David Hildenbrand (Arm)" Subject: mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing Date: Wed, 25 Nov 2026 23:33:17 +0100 huge_pmd_share() currently only shares the page tables of another VMA if it can read-lock the mmap lock of the corresponding mm. That refuses sharing whenever that mm is write-locked or if the mmap lock is contended. mprotect() holds the VMA write lock while changing the flags and the page table entries, so let's read-lock only that VMA instead. As we hold i_mmap_rwsem, that VMA remains part of its mm and cannot go away. Add vma_trylock_read(), a variant of vma_start_read(), that does not require being called with the RCU read lock held, and will also not drop it itself. Link: https://patch.msgid.link/20261125-hugetlb_fixes_2-v1-19-db27fd2f71a8@kernel.org 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: Chris Mason Cc: luojiajun Cc: Josef Bacik Cc: Daehyeon Ko <4ncienth@gmail.com> Cc: Ackerley Tng Cc: "Oscar Salvador (SUSE)" Cc: Jiaqi Yan include/linux/mmap_lock.h | 1 mm/hugetlb.c | 4 - mm/mmap_lock.c | 78 ++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 28 deletions(-) --- a/include/linux/mmap_lock.h~mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing +++ a/include/linux/mmap_lock.h @@ -278,6 +278,7 @@ static inline bool vma_start_read_locked struct vm_area_struct *vma_start_read_unlocked(struct mm_struct *mm, unsigned long address); +bool vma_trylock_read(struct vm_area_struct *vma); static inline void vma_end_read(struct vm_area_struct *vma) { --- a/mm/hugetlb.c~mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing +++ a/mm/hugetlb.c @@ -7264,7 +7264,7 @@ pte_t *huge_pmd_share(struct mm_struct * * Stabilize the svma flags, as mprotect() changes flags * before adjusting page tables. */ - if (!mmap_read_trylock(svma->vm_mm)) + if (!vma_trylock_read(svma)) continue; saddr = page_table_shareable(svma, vma, addr, idx); @@ -7274,7 +7274,7 @@ pte_t *huge_pmd_share(struct mm_struct * if (spte) ptdesc_pmd_pts_inc(virt_to_ptdesc(spte)); } - mmap_read_unlock(svma->vm_mm); + vma_end_read(svma); if (spte) break; } --- a/mm/mmap_lock.c~mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing +++ a/mm/mmap_lock.c @@ -212,26 +212,17 @@ static bool vma_seqcount_is_write_locked } /* - * Try to read-lock a vma. The function is allowed to occasionally yield false - * locked result to avoid performance overhead, in which case we fall back to - * using mmap_lock. The function should never yield false unlocked result. - * False locked result is possible if vma gets reused and attached to a - * different mm before we lock it. - * Returns the vma on success, NULL on failure to lock and EAGAIN if vma got - * detached. - * - * IMPORTANT: RCU lock must be held upon entering the function, but upon error - * IT IS RELEASED. The caller must handle this correctly. + * Like vma_start_read(), but without RCU handling: the caller must make sure + * that the vma cannot be freed, for example by holding the RCU lock. */ -static struct vm_area_struct *vma_start_read(struct mm_struct *mm, - struct vm_area_struct *vma) +static struct vm_area_struct *__vma_start_read(struct mm_struct *mm, + struct vm_area_struct *vma) { unsigned int vma_seq; unsigned int mm_seq; struct mm_struct *other_mm; int oldcnt; - RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held"); /* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -241,10 +232,8 @@ static struct vm_area_struct *vma_start_ */ vma_seq = READ_ONCE(vma->vm_lock_seq); mm_seq = READ_ONCE(mm->mm_lock_seq.sequence); - if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) { - vma = NULL; - goto err; - } + if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) + return NULL; /* * If VM_REFCNT_EXCLUDE_READERS_FLAG is set, @@ -257,8 +246,7 @@ static struct vm_area_struct *vma_start_ if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt, VM_REFCNT_LIMIT))) { /* return EAGAIN if vma got detached from under us */ - vma = oldcnt ? NULL : ERR_PTR(-EAGAIN); - goto err; + return oldcnt ? NULL : ERR_PTR(-EAGAIN); } __vma_lockdep_acquire_read(vma); @@ -288,15 +276,10 @@ static struct vm_area_struct *vma_start_ mm_seq = raw_read_seqcount(&mm->mm_lock_seq); if (unlikely(vma_seqcount_is_write_locked(vma_seq, mm_seq))) { vma_refcount_put(vma); - vma = NULL; - goto err; + return NULL; } return vma; -err: - rcu_read_unlock(); - - return vma; err_unstable: /* * If vma got attached to another mm from under us, that mm is not @@ -309,11 +292,54 @@ err_unstable: mmgrab(other_mm); vma_refcount_put(vma); mmdrop_async(other_mm); - rcu_read_unlock(); return NULL; } +/** + * vma_start_read() - Try to read-lock a vma looked up under RCU. + * @mm: The mm the vma is expected to belong to. + * @vma: The vma to read-lock. + * + * The function is allowed to occasionally yield false locked result to avoid + * performance overhead, in which case we fall back to using mmap_lock. The + * function should never yield false unlocked result. False locked result is + * possible if vma gets reused and attached to a different mm before we lock it. + * + * Context: RCU lock must be held upon entering the function, but upon error + * IT IS RELEASED. The caller must handle this correctly. + * + * Return: The vma on success, NULL on failure to lock and ERR_PTR(-EAGAIN) if + * the vma got detached. + */ +static struct vm_area_struct *vma_start_read(struct mm_struct *mm, + struct vm_area_struct *vma) +{ + RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held"); + + vma = __vma_start_read(mm, vma); + if (IS_ERR_OR_NULL(vma)) + rcu_read_unlock(); + return vma; +} + +/** + * vma_trylock_read() - Try to read-lock a VMA that belongs to its mm. + * @vma: The VMA to read-lock. + * + * Unlike vma_start_read(), this function does not deal with VMAs that might + * get freed and reused in another mm concurrently. + * + * Context: The caller must make sure that @vma remains part of @vma->vm_mm + * and cannot go away. + * + * Return: true if @vma is read-locked, false otherwise. + */ +bool vma_trylock_read(struct vm_area_struct *vma) +{ + return !IS_ERR_OR_NULL(__vma_start_read(vma->vm_mm, vma)); +} + /* * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be * stable and not isolated. If the VMA is not found or is being modified the _ Patches currently in -mm which might be from david@kernel.org are mm-hugetlb-charge-the-copy-made-at-fork-to-the-parent.patch mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch uprobes-skip-mappings-not-managed-by-core-mm.patch mm-memory-store-the-pfn-of-cow-pfnmap-mappings-in-the-anon-page-offset.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing.patch rfc-mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch