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=1794236914; b=IC5ATkiElcE3DRNg7hAs2e/nmzkCFojOfMAr5miS+GwphE2dsx96cNRLH+8BeK0chLrm 2XuagRBgFLseFrkihS2IrCj11SdKEsssuzDK9vlmFSkLCCZmcyzfaxs72nCCZS7q0BQLG eKzpe/0w9CvjrH6bFDuYzYNQZQ8w0PM6AovvTI3CbGhk34b5BhSJCh405orZXE9paKVrf UHAlD9QqeInwLPTAEuB1WQPz4omaysLeYg1XDNG1F5oPMzuADsiiDsMrsE7jP+Uzlix1n Cil1vtvk0PGHpcr8seZgE4sUturNOP1ZYW19y9Pvidkklej2Ved6o2BGlKfHlOmljjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794236914; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=Sd2qM6+PDFRCO8BLs2DGMh7GhcX/ulkjEDLXnk+kxUA=; b=1DjtbcBuMF4dVcvUj0JjrB4fCaVn1mwRqiwIHPpItWnKCL6D+tW3ngSqL/H6SuZI9RdV iE7IBZgposbOV8LKBui75F5l6WLnWm3MjpTWri5q1Zzf/PS9u7Qle6YzAWXugaxXKUCFh xy16BQtb3LTYCvshVt8JjyKdBri9J6TP2Oh3sXilCeRlaZV6+7RSr7KGjKLkqq93OUXyK BeUyOECe/GhDTXYjueVHT1Ftl725hB+543U4+Dk4AcKXNQn0XP/sYrnpmEtrjw8U4Gnx8 DD3Z+0P10GWM9d96awncFdNI+xiL4GWUaxxmogsbKn23dGJAevOpTCD4dqeXuXYdnPQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SKgc2LkN; 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 77C05606EE85; Mon, 9 Nov 2026 15:08:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SKgc2LkN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 430B91F008A4; Mon, 9 Nov 2026 15:08:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794236914; bh=Sd2qM6+PDFRCO8BLs2DGMh7GhcX/ulkjEDLXnk+kxUA=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=SKgc2LkNZGyEX0Gy5P7smNbb1wHNkIgKilphlpHL9cBlesnaHKPrBoPtYdYYGLKZ/ S0LSKQGm4pCJ9h1Zwf/j5Ji8+pC/lndtFbwLRLaD0KKopxqSb1FlWv7Ap0BifOLzuD 2KzrScbkVNr7CsD929bxREjX1DGHPcFSvId3xgjb0K389RsLtcds/kChaHNL2rH8gc oW+XTesVNeLK1htElh5LeJuJOB3illKFWZ6bRP9cvSBA2oz7gWIxoSRgqjdDsetXuM kn4T3SUUtb87c0Yh2lzEwYyDwa892/7hCZoRJfbByqgXFcnC6xr5IAu5/oiQDMfQUj Xr4Li0+udWeIg== Message-ID: <28b818cb-f038-43ce-a2e2-a9c97befba09@kernel.org> Date: Mon, 9 Nov 2026 16:08:23 +0100 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm/khugepaged: check for empty PTE tables before retracting To: sashiko-reviews@lists.linux.dev, Jonathan Corbet , Shuah Khan , Randy Dunlap , Andrew Morton , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pedro Falcato , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Peter Xu , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Axel Rasmussen Cc: linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, Kiryl Shutsemau References: <20261107-b4-uffd_marker_loss-v1-0-82926b22704c@kernel.org> <20261107-b4-uffd_marker_loss-v1-2-82926b22704c@kernel.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5N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5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/8/26 00:51, sashiko-bot@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 2 potential > issues to consider: > - [Medium] mm/khugepaged: race between retract_page_tables() and > copy_page_range() > - [Medium] mm/khugepaged: retract_page_tables() breaks uffd RWP on file > and shmem VMAs > -- > > commit 15ea4d4b6e2586260561871f9acd4c3bff3278ec > Author: David Hildenbrand (Arm) > > mm/khugepaged: check for empty PTE tables before retracting > > This commit replaces the VMA flag checks in retract_page_tables() with a > scan under the PTE lock to verify that the PTE table is empty before > retracting it. > >> diff --git a/mm/khugepaged.c b/mm/khugepaged.c >> index 2a5ee8fc42335..f39ea3d076554 100644 >> --- a/mm/khugepaged.c >> +++ b/mm/khugepaged.c > [ ... ] >> @@ -2081,48 +2082,6 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, >> try_collapse_pte_mapped_thp(mm, addr, install_pmd); >> } >> >> -/* Can we retract page tables for this file-backed VMA? */ >> -static bool file_backed_vma_is_retractable(struct vm_area_struct *vma) >> -{ >> - /* >> - * Check vma->anon_vma to exclude MAP_PRIVATE mappings that >> - * got written to. These VMAs are likely not worth removing >> - * page tables from, as PMD-mapping is likely to be split later. >> - * >> - * Can't use vma_has_anon_rmap() here as the VMA may be stabilised >> - * by the file rmap lock. >> - */ >> - if (READ_ONCE(vma->anon_vma)) >> - return false; >> - >> - /* >> - * When a vma is registered with uffd-wp or RWP, we cannot recycle >> - * the page table because there may be pte markers installed. >> - * VM_UFFD_RWP ranges similarly rely on per-PTE uffd state >> - * and cannot be recycled to a shared PMD. Other vmas can still >> - * have the same file mapped hugely, but skip this one: it will >> - * always be mapped in small page size for these registrations. >> - */ >> - if (userfaultfd_protected(vma)) >> - return false; >> - >> - /* >> - * Preserve guard and poison markers when retracting page tables. >> - * >> - * These flags are set atomically on marker installation under mmap/VMA >> - * read lock, and here we may not hold any VMA or mmap lock at all. >> - * >> - * This is therefore serialised on the PTE page table lock, which is >> - * obtained on marker installation after the flag is set, so this >> - * check being performed under this lock excludes races. >> - */ >> - if (vma_test_atomic_flag(vma, VMA_MAYBE_GUARD_BIT) || >> - vma_test_atomic_flag(vma, VMA_MAYBE_UFFD_POISON_BIT)) >> - return false; >> - >> - return true; >> -} >> - >> static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) >> { >> struct vm_area_struct *vma; >> @@ -2133,10 +2092,18 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) >> struct mm_struct *mm; >> unsigned long addr; >> pmd_t *pmd, pgt_pmd; >> - spinlock_t *pml; >> - spinlock_t *ptl; >> + pte_t *pte; >> + spinlock_t *pml = NULL, *ptl; >> bool success = false; >> >> + /* >> + * Skip private mappings that have been written to: we'll likely >> + * split them as soon as we CoW later. Use READ_ONCE() because >> + * we are neither holding the VMA lock nor the mmap_lock. >> + */ >> + if (READ_ONCE(vma->anon_vma)) >> + continue; > > [Severity: Medium] > Can removing the VM_COPY_ON_FORK flag checks here cause retract_page_tables() > to race with copy_page_range() during fork()? > > In dup_mmap(), the child VMA is linked into mapping->i_mmap and > i_mmap_unlock_write(mapping) is dropped before copy_page_range() runs. > vma_needs_copy() copies page tables not only when vma_has_anon_rmap() is set, > but also when VM_COPY_ON_FORK flags (VM_MAYBE_GUARD, VM_MAYBE_UFFD_POISON, > VM_UFFD_WP, or VM_UFFD_RWP) are set on a file or shmem VMA whose anon_vma is > still NULL. Ah, try_collapse_pte_mapped_thp() runs withe the mmap lock, whereby retract_page_tables() only holds the rmap lock. > > mm/memory.c:copy_pte_range() { > /* > * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the > * error handling here, assume that exclusive mmap_lock on dst and src > * protects anon from unexpected THP transitions; with shmem and file > * protected by mmap_lock-less collapse skipping areas with an anon rmap > * (whereas vma_needs_copy() skips areas without one). A rework > * can remove such assumptions later, but this is good enough for now. > */ > dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); > if (!dst_pte) { > ret = -ENOMEM; > goto out; > } > } > > Because retract_page_tables() no longer skips VMAs with VM_COPY_ON_FORK flags > set, it can retract the newly allocated empty dst_pmd in the child between > __pte_alloc() and pte_offset_map_lock() (or across cond_resched() before a > non-empty PTE is copied): > > CPU0 CPU1 > dup_mmap() > mapping_rmap_tree_insert_after(dst_vma, ...) > i_mmap_unlock_write(mapping) > copy_page_range(dst_vma, src_vma) > ... > copy_pte_range() > pte_alloc_map_lock(dst_mm, dst_pmd, ...) > __pte_alloc(dst_mm, dst_pmd) > collapse_file() > retract_page_tables() > pte_table_empty(pte) == true > pmdp_collapse_flush(..., dst_pmd) > pte_offset_map_lock(dst_mm, dst_pmd, ...) > --> returns NULL > ret = -ENOMEM; <-- fork() fails spuriously > > On the parent side, retracting an empty src_pmd concurrently with > copy_pmd_range() can also clear *src_pmd between pmd_none(*src_pmd) and > pmd_bad(*src_pmd) inside pmd_none_or_clear_bad(src_pmd), triggering a false > pmd_clear_bad() report. Yeah, that would likely need a different handling. > >> + >> addr = vma->vm_start + >> ((pgoff - vma_start_pgoff(vma)) << PAGE_SHIFT); >> if (addr & ~HPAGE_PMD_MASK || >> @@ -2150,52 +2117,48 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) >> if (collapse_test_exit(mm)) >> continue; >> >> - if (!file_backed_vma_is_retractable(vma)) >> - continue; >> - >> /* PTEs were notified when unmapped; but now for the PMD? */ >> mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, >> addr, addr + HPAGE_PMD_SIZE); >> mmu_notifier_invalidate_range_start(&range); >> >> - pml = pmd_lock(mm, pmd); > [ ... ] >> + pte = pte_offset_map_lock(mm, pmd, addr, &ptl); >> + if (!pte) >> + goto unlock; >> >> /* >> - * Huge page lock is still held, so normally the page table must >> - * remain empty; and we have already skipped vmas with an anon >> - * rmap and userfaultfd_wp() vmas. But since the mmap_lock is not >> - * held, it is still possible for a racing userfaultfd_ioctl() or >> - * madvise() to have inserted ptes or markers. Now that we hold >> - * ptlock, repeating the retractable checks protects us from >> - * races against the prior checks. >> + * The folio lock prevents file faults from modifying the >> + * page table, but other things (userfaultfd, CoW, guard >> + * markers) could have modified the page table until we lock it >> + * and check. >> */ >> - if (likely(file_backed_vma_is_retractable(vma))) { >> - pgt_pmd = pmdp_collapse_flush(vma, addr, pmd); >> - pmdp_get_lockless_sync(); >> - success = true; >> - } >> + if (!pte_table_empty(pte)) >> + goto unlock; > > [Severity: Medium] > Does checking pte_table_empty() instead of userfaultfd_protected(vma) break > userfaultfd RWP (and WP) on file and shmem VMAs? > > Unlike VM_UFFD_WP on write-protected PTEs, try_to_unmap_one() does not > install PTE markers for VM_UFFD_RWP (or for VM_UFFD_WP PTEs that are not > currently write-protected) when collapse_file() unmaps the small folios. > pte_table_empty(pte) is then true and retract_page_tables() clears the PMD to > pmd_none, even though try_collapse_pte_mapped_thp() still refuses > userfaultfd_protected(vma). > > Once the PMD is pmd_none, the next fault in finish_fault() installs a huge > PMD before checking userfaultfd_armed(vma): > > mm/memory.c:finish_fault() { > if (pmd_none(*vmf->pmd)) { > if (!needs_fallback && folio_test_pmd_mappable(folio)) { > ret = do_set_pmd(vmf, folio, page); > if (ret != VM_FAULT_FALLBACK) > return ret; > } > ... > /* Using per-page fault to maintain the uffd semantics */ > if (unlikely(userfaultfd_armed(vma)) || unlikely(needs_fallback)) { > nr_pages = 1; > } > > When userspace later issues UFFDIO_RWPROTECT on that file or shmem VMA, > change_pmd_range() sees pgtable_split_needed() is true and calls > __split_huge_pmd(), which zaps the file huge PMD back to pmd_none without > populating a PTE table (since pgtable_populate_needed() is false for > MM_CP_UFFD_RWP), so RWP protection is lost and subsequent accesses fault the > unprotected huge PMD back in without delivering RWP faults. Installing a > huge PMD also causes PAGEMAP_SCAN to report all 512 pages as accessed or > written after a single 4KB page fault. "Since pgtable_populate_needed() is false for MM_CP_UFFD_RWP" I think RWP also needs proper uffd marker support, independent of the change here. -- Cheers, David