From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1230429B216; Wed, 14 Oct 2026 17:01:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791997278; cv=none; b=FgEcjN2LnrzN5mBDrU7JFn1nuAkXznDRet9dRHxlNOxQqG3xks2XX7njZL3a/Bv3/SNn0Qx2F+yrT3rqERgr3stOGozUP19bVKRKKtEZl/qP7xj4EyGseSe4Hlp44R2QSojE3C2QDAsA+8pXEu/P4lVuoQbISjJzWz1OhaAUNGI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791997278; c=relaxed/simple; bh=7vLQ2e4/or8ykId+WxFVvt7Qu3V2lKAPqsK0X8fVauU=; h=Date:To:From:Subject:Message-Id; b=ly556/1eL0CoejNGVyjHUxgCD3LBbvK5lN1qe6DCj4/mU5kUA8px9qtc+buslLrJG2V92JdHygnX5Q5n9NkdUgsBY5a/DJOB0/J+7iVSe6suXfrllrdjdfFhT4F3dBBc/fglXlkAAflBB/NqmaV4MIJrdqQ5G62K+WAvvXYFxhs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=TvQPy11B; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="TvQPy11B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 728631F000C6; Wed, 14 Oct 2026 17:01:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1791997276; bh=kCGPGu7hdQHKuy5ZtSx0uWKZDmClArGj76KeIC4u+Ww=; h=Date:To:From:Subject; b=TvQPy11BODTJfmpqUFqff+KcLPPJ3gAQxabAAP5n/6VJdcIIN2RtK4FQ/ib0Ii+S4 GMvb3hLb1bYp6CPp6c0HkeunDS8nKJ4JuPW+iuNEGbTFEe2jZKpCEZVsQyHwYi/SDV vRgEKWMaaFNv8CMEXnyElxy31LULQsQVFpy2Yy2g= Date: Wed, 14 Oct 2026 10:01:15 -0700 To: mm-commits@vger.kernel.org,stable@vger.kernel.org,rppt@kernel.org,peterx@redhat.com,osalvador@suse.de,muchun.song@linux.dev,lokeshgidra@google.com,ljs@kernel.org,linmiaohe@huawei.com,liam@infradead.org,kylebot@openai.com,david@kernel.org,akpm@linux-foundation.org From: Andrew Morton Subject: + mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings.patch added to mm-unstable branch Message-Id: <20261014170116.728631F000C6@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The patch titled Subject: mm/userfaultfd: only allocate anon_vma in CoW mappings has been added to the -mm mm-unstable branch. Its filename is mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings.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: "David Hildenbrand (Arm)" Subject: mm/userfaultfd: only allocate anon_vma in CoW mappings Date: Wed, 14 Oct 2026 13:53:11 +0200 find_vma_and_prepare_anon() can end up allocating an anon_vma in VMAs that are not CoW mappings, which is wrong. If we have a MAP_SHARED R/O file mapped, we lack VM_SHARED (but do have VM_MAYSHARE) and lack VM_MAYWRITE. While uffd later rejects such mappings (uffd registration requires VM_MAYWRITE, see comment in validate_dst_vma) find_vma_and_prepare_anon() still wrongly allocates an anon vma before it validates the vma. This in turn later confuses copy_page_range()->vma_needs_copy() such that we end up calling copy_hugetlb_page_range() with ranges that actually do not contain *any* per-pagetable information that would require copying, which is the basis for PMD-table sharing in the first place. copy_hugetlb_page_range() should not have to worry about shared PMD tables at all: it doesn't make any sense to copy page tables that are shared, as they can be fully recreated later. Ending up in the function with shareable page tables is the real issue. If a destination PMD table is shared in copy_hugetlb_page_range(), it might get concurrently unshared as we are not holding relevant locks, resulting in us using a page table that is no longer mapped and possibly freed after calling: dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz); To fix it, do not allocate an anon_vma in non-CoW mappings and add a safety guard to copy_hugetlb_page_range(), making sure that we never end up messing with shared PMD tables in there. Before commit 867a43a34ff8a ("userfaultfd: use per-vma locks in userfaultfd operations") we would always validate the VMA before allocating an anon_vma, and consequently never wrongly allocate an anon_vma. 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. 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 VM_SHARED in userfaultfd code should be cleaned up later to focus on CoW vs. non-CoW mappings. Link: https://lore.kernel.org/20261014-copy_hugetlb_page_range-v1-1-5b68e03fe7fe@kernel.org Fixes: 867a43a34ff8 ("userfaultfd: use per-vma locks in userfaultfd operations") Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Reported-by: Kyle Zeng Reviewed-by: Lorenzo Stoakes (ARM) Cc: Muchun Song Cc: Oscar Salvador Cc: Mike Rapoport Cc: Peter Xu Cc: Miaohe Lin Cc: Lokesh Gidra Cc: Liam R. Howlett Cc: --- mm/hugetlb.c | 24 ++++++++++++------------ mm/userfaultfd.c | 5 ++--- 2 files changed, 14 insertions(+), 15 deletions(-) --- a/mm/hugetlb.c~mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings +++ a/mm/hugetlb.c @@ -4984,10 +4984,11 @@ int copy_hugetlb_page_range(struct mm_st raw_write_seqcount_begin(&src->write_protect_seq); } else { /* - * For shared mappings the vma lock must be held before - * calling hugetlb_walk() in the src vma. Otherwise, the - * returned ptep could go away if part of a shared pmd and - * another thread calls huge_pmd_unshare. + * 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); } @@ -4995,6 +4996,13 @@ int copy_hugetlb_page_range(struct mm_st 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; @@ -5006,14 +5014,6 @@ int copy_hugetlb_page_range(struct mm_st break; } -#ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING - /* If the pagetables are shared, there is nothing to do */ - if (ptdesc_pmd_is_shared(virt_to_ptdesc(dst_pte))) { - addr |= last_addr_mask; - continue; - } -#endif - dst_ptl = huge_pte_lock(h, dst, dst_pte); src_ptl = huge_pte_lockptr(h, src, src_pte); spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); --- a/mm/userfaultfd.c~mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings +++ a/mm/userfaultfd.c @@ -115,8 +115,7 @@ struct vm_area_struct *find_vma_and_prep vma = vma_lookup(mm, addr); if (!vma) vma = ERR_PTR(-ENOENT); - else if (!(vma->vm_flags & VM_SHARED) && - unlikely(anon_vma_prepare(vma))) + else if (vma_is_cow_mapping(vma) && unlikely(anon_vma_prepare(vma))) vma = ERR_PTR(-ENOMEM); return vma; @@ -145,7 +144,7 @@ static struct vm_area_struct *uffd_lock_ * We know we're going to need an anon rmap, so check that * early. */ - if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma_has_anon_rmap(vma))) + if (vma_is_cow_mapping(vma) && unlikely(!vma_has_anon_rmap(vma))) vma_end_read(vma); else return vma; _ Patches currently in -mm which might be from david@kernel.org are arm64-mm-move-__check_safe_pte_update-fix.patch mm-sparse-move-mem_section-init-to-sparse_extreme_init.patch mm-sparse-refactor-sparse_sections_init.patch mm-sparse-move-initialization-of-section-metadata-to-sparse_metadata_init.patch mm-sparse-rename-and-cleanup-sparse_init_nid.patch mm-sparse-cleanup-sparse_init_one_section.patch mm-sparse-rename-__highest_present_section_nr-to-__highest_used_section_nr.patch mm-sparse-remove-pfn_in_present_section.patch mm-sparse-move-__highest_used_section_nr-handling.patch scripts-gdb-mmpy-remove-fallbacks-for-section_has_mem_map-and-section_is_early.patch mm-sparse-remove-section_marked_present.patch mm-sparse-remove-flags-parameter-from-sparse_init_one_section.patch fs-proc-page-clarify-comment-in-get_max_dump_pfn.patch mm-memory_hotplug-drop-config_have_arch_pfn_valid-handling-from-pfn_to_online_page.patch vmcoreinfo-export-section_has_mem_map.patch mm-disallow-ioremaping-ordinary-ram-in-generic_access_phys.patch mm-userfaultfd-only-allocate-anon_vma-in-cow-mappings.patch