From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
Miaohe Lin <linmiaohe@huawei.com>,
Lokesh Gidra <lokeshgidra@google.com>,
"Liam R. Howlett" <liam@infradead.org>,
Kyle Zeng <kylebot@openai.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] mm/userfaultfd: only allocate anon_vma in CoW mappings
Date: Wed, 14 Oct 2026 20:29:35 +0200 [thread overview]
Message-ID: <a5c6edb4-9e8d-47f1-a316-6627e518fa7a@kernel.org> (raw)
In-Reply-To: <as99gyi-NUprRK6e@gremlin>
On 10/14/26 16:51, Lorenzo Stoakes (ARM) wrote:
> On Wed, Oct 14, 2026 at 01:53:11PM +0200, David Hildenbrand (Arm) wrote:
>> As uffd_lock_vma() is part of the same lookup machinery and
>> also contains a misleading VM_SHARED check, change that one as well
>> directly for consistency.
>
> OK, I think that one isn't _strictly necessary_, but while we're there,
> worth doing too, clearly.
>
> We should also go through and convert the remaining !VM_SHARED checks ->
> vma_is_cow_mapping() too if possible, but that's a follow up, obviously!
>
>>
>> A follow-up cleanup will stop taking the hugetlb_vma_lock_read(src_vma)
>> lock in copy_hugetlb_page_range() entirely. Further, the usage of
>
> Hmm really? Is it possible to do that without the hugetlb lock?
>
Yes, we don't have to sync against PMD sharing if PMD sharing is impossible.
This is what I have laying around:
commit ad6a5c65bd5fa31d93a2de2d2e5e473b26ad6a3d
Author: David Hildenbrand (Arm) <david@kernel.org>
Date: Sat Oct 10 13:00:13 2026 +0200
mm/hugetlb: stop syncing against PMD table sharing in copy_hugetlb_page_range()
We must never end up in copy_hugetlb_page_range() when PMD sharing
applies, yet we still have to perform the hugetlb_vma_lock_read() so
hugetlb_walk() is happy.
Let's introduce a new vma_supports_pmd_sharing() and use that instead
in hugetlb_walk(). Also convert want_pmd_share(), which now looks a lot
cleaner, and document why the uffd checks cannot go into
__vma_shareable_lock().
We can now relocate our safety checks in copy_hugetlb_page_range() to
the beginning of the function.
No functional change intended.
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 5029c72418631..b2da5d5816944 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -1389,6 +1389,20 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
bool __vma_private_lock(struct vm_area_struct *vma);
+static inline bool vma_supports_pmd_sharing(struct vm_area_struct *vma)
+{
+#ifdef CONFIG_USERFAULTFD
+ /*
+ * Keep the UFFD checks out of __vma_shareable_lock(): UFFD flags can
+ * change at runtime while the hugetlb VMA lock is held. They must not
+ * cause unlocking or freeing an existing lock to be skipped.
+ */
+ if (uffd_disable_huge_pmd_share(vma))
+ return false;
+#endif
+ return __vma_shareable_lock(vma);
+}
+
/*
* Safe version of huge_pte_offset() to check the locks. See comments
* above huge_pte_offset().
@@ -1406,7 +1420,7 @@ hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz)
*
* NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP.
*/
- if (__vma_shareable_lock(vma))
+ if (vma_supports_pmd_sharing(vma))
WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) &&
!lockdep_is_held(
&vma->vm_file->f_mapping->i_mmap_rwsem));
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 60f9b209c8db1..606e93e32eefb 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4987,22 +4987,16 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
* Shared mappings only need copying when we could have
* information stored in page tables (e.g., userfaultfd PTE
* bits or markers), which is incompatible with PMD sharing.
- * Still take the hugetlb VMA lock for now to satisfy
- * hugetlb_walk()'s locking requirements.
*/
- hugetlb_vma_lock_read(src_vma);
+ if (WARN_ON_ONCE(vma_supports_pmd_sharing(src_vma) ||
+ vma_supports_pmd_sharing(dst_vma)))
+ return -EINVAL;
}
last_addr_mask = hugetlb_mask_last_page(h);
for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
spinlock_t *src_ptl, *dst_ptl;
- if (WARN_ON_ONCE(want_pmd_share(src_vma, addr) ||
- want_pmd_share(dst_vma, addr))) {
- ret = -EINVAL;
- break;
- }
-
src_pte = hugetlb_walk(src_vma, addr, sz);
if (!src_pte) {
addr |= last_addr_mask;
@@ -5147,8 +5141,6 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
if (cow) {
raw_write_seqcount_end(&src->write_protect_seq);
mmu_notifier_invalidate_range_end(&range);
- } else {
- hugetlb_vma_unlock_read(src_vma);
}
return ret;
@@ -7005,17 +6997,9 @@ bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
unsigned long start = addr & PUD_MASK;
unsigned long end = start + PUD_SIZE;
-#ifdef CONFIG_USERFAULTFD
- if (uffd_disable_huge_pmd_share(vma))
- return false;
-#endif
- /*
- * check on proper vm_flags and page table alignment
- */
- if (!(vma->vm_flags & VM_MAYSHARE))
- return false;
- if (!vma->vm_private_data) /* vma lock required for sharing */
+ if (!vma_supports_pmd_sharing(vma))
return false;
+
if (!range_in_vma(vma, start, end))
return false;
return true;
Will send that out for the next cycle.
> It'd be good in general if we could get rid of that lock as a whole :) but maybe
> dreaming.
Yeah :)
Thanks!
--
Cheers,
David
next prev parent reply other threads:[~2026-10-14 18:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 11:53 [PATCH] mm/userfaultfd: only allocate anon_vma in CoW mappings David Hildenbrand (Arm)
2026-10-14 14:51 ` Lorenzo Stoakes (ARM)
2026-10-14 18:04 ` Kyle Zeng
2026-10-14 18:29 ` David Hildenbrand (Arm)
2026-10-14 18:29 ` David Hildenbrand (Arm) [this message]
2026-10-15 9:05 ` Lorenzo Stoakes (ARM)
2026-10-15 8:16 ` Lance Yang
2026-10-15 8:33 ` Mike Rapoport
2026-10-15 8:37 ` Muchun Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a5c6edb4-9e8d-47f1-a316-6627e518fa7a@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=kylebot@openai.com \
--cc=liam@infradead.org \
--cc=linmiaohe@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lokeshgidra@google.com \
--cc=muchun.song@linux.dev \
--cc=osalvador@suse.de \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox