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=1794591383; b=tXNfhSfBiZidFaLuPETqCGmreMB+ISPhRXxO8vfbaaxrexfgc/uK7YijIntIZrlmzYZv LBVX2LZys0KOHqyU3tDPZiOYASl67mpm2acgfaAxDkxKW3hifk19tPROLhFeZYjzAfYca a6RS6gU3bnQjVj8kXSd6TBFNZJPbFPkieVt1yA8EBQ48a7JcEOVPY3ved3kYQ2+MMz+gH jWlcn72d9ncWkDhj3sAnH4x/BcXHeim6PaF8raNMFQakETG9exEGhn7h0SSPo66grg8vg ET3wB1tFJ17gg9cVAiS7a5pYoBZHmcHZl6rVCImPxjhXxHA3rnKAvQ8yoe/GUOMIB/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794591383; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=YdKXIWGgvvliG2E2OhF2NbWeCylPEmLIYzn+GqTB9Mg=; b=HT8uSx2c2gCvIICEXAhmod4DCvmF0cSXWyd86SCjkZtJyYD9OfweVUgMX/BaI4V5o+4j 5lVUIdoBBfd5RJzVhWhNA9ER2KLCdoUrYXaJAUs3AP0G+TbWYi2uH1yLfoOTUg/zKF9eB gDiYCfwj7/qGG5FQRrXnkLojOWaGhk5AnwSLci02zxRnvSQhgURej7dHpiaOWftwpglqI bJ7UCUywgkWiFwIyZCi8Xe+McVYEGA4azYWDORRGeCvST60mDXKaGWn3wWQuqiUe7OXJY 87FJlozwhl8gdtthVHhEVS4kVbLSa2SBg46Cbl6PvGZisZxehGYDcYBnYa6ReKx3WrQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="sfTnggC/"; 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 7F9DF606B237; Fri, 13 Nov 2026 17:36:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=sfTnggC/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 319E21F000C4; Fri, 13 Nov 2026 17:36:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794591383; bh=YdKXIWGgvvliG2E2OhF2NbWeCylPEmLIYzn+GqTB9Mg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=sfTnggC/M6jP7juXlmRaoNkFrnH0z6vsuY2ZXqLBnD0FKqct2fMpQmbn24p0J4kad MGjsQ3WLPWkEiw+g/0y4MfDsD2pP0n15rOnsMh8lyHTMZFSUixNUYhLfOowkzh7Ilx 1za37GnOcfq2NrcWeLIhxUccYtQ8Sf7kCNPfwSi8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "David Hildenbrand (Arm)" , Andrew Morton , Kyle Zeng , "Lorenzo Stoakes (ARM)" , Muchun Song , "Mike Rapoport (Microsoft)" , Lance Yang , Oscar Salvador , Peter Xu , Miaohe Lin , Lokesh Gidra , "Liam R. Howlett" , Sasha Levin Subject: [PATCH 6.12 485/670] mm/userfaultfd: only allocate anon_vma in CoW mappings Date: Fri, 13 Nov 2026 10:50:49 -0500 Message-ID: <20261113153828.373861894@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261113153811.177190860@linuxfoundation.org> References: <20261113153811.177190860@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) [ Upstream commit 514c9247db305922591d806ec65d3c3fe420538d ] 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) Reviewed-by: Kyle Zeng Tested-by: Kyle Zeng Acked-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) Tested-by: Lance Yang Cc: Oscar Salvador Cc: Peter Xu Cc: Miaohe Lin Cc: Lokesh Gidra Cc: Liam R. Howlett Cc: [ sashal: adapted VMA checks to use is_cow_mapping(vma->vm_flags) with direct vma->anon_vma access ] Signed-off-by: Sasha Levin --- mm/hugetlb.c | 24 ++++++++++++------------ mm/userfaultfd.c | 5 ++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 72ae5df851567..43f4f2a06f463 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5305,10 +5305,11 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 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); } @@ -5316,6 +5317,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 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; @@ -5327,14 +5335,6 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 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); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index e2cb788e6ad89..29d2366d88fa1 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -48,8 +48,7 @@ struct vm_area_struct *find_vma_and_prepare_anon(struct mm_struct *mm, 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 (is_cow_mapping(vma->vm_flags) && unlikely(anon_vma_prepare(vma))) vma = ERR_PTR(-ENOMEM); return vma; @@ -77,7 +76,7 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, * We know we're going to need to use anon_vma, so check * that early. */ - if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma->anon_vma)) + if (is_cow_mapping(vma->vm_flags) && unlikely(!vma->anon_vma)) vma_end_read(vma); else return vma; -- 2.53.0