From: "David Hildenbrand (Red Hat)" <david@kernel.org>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Jonathan Corbet <corbet@lwn.net>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
Vlastimil Babka <vbabka@suse.cz>, Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Nico Pache <npache@redhat.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-doc@vger.kernel.org, linux-mm@kvack.org,
linux-trace-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, Andrei Vagin <avagin@gmail.com>
Subject: Re: [PATCH v4 6/9] mm: set the VM_MAYBE_GUARD flag on guard region install
Date: Wed, 19 Nov 2025 10:16:14 +0100 [thread overview]
Message-ID: <61f7c6d2-a15e-4c6a-9704-0e3db65eed3c@kernel.org> (raw)
In-Reply-To: <e9e9ce95b6ac17497de7f60fc110c7dd9e489e8d.1763460113.git.lorenzo.stoakes@oracle.com>
>
> +/* Can we retract page tables for this file-backed VMA? */
> +static bool file_backed_vma_is_retractable(struct vm_area_struct *vma)
It's not really the VMA that is retractable :)
Given that the function we are called this from is called
"retract_page_tables" (and not file_backed_...) I guess I would just
have called this
"page_tables_are_retractable"
"page_tables_support_retract"
Or sth. along those lines.
> +{
> + /*
> + * 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.
> + */
> + if (READ_ONCE(vma->anon_vma))
> + return false;
> +
> + /*
> + * When a vma is registered with uffd-wp, we cannot recycle
> + * the page table because there may be pte markers installed.
> + * Other vmas can still have the same file mapped hugely, but
> + * skip this one: it will always be mapped in small page size
> + * for uffd-wp registered ranges.
> + */
> + if (userfaultfd_wp(vma))
> + return false;
> +
> + /*
> + * If the VMA contains guard regions then we can't collapse it.
> + *
> + * This is set atomically on guard 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 guard region installation after the flag is set, so this
> + * check being performed under this lock excludes races.
> + */
> + if (vma_flag_test_atomic(vma, VM_MAYBE_GUARD_BIT))
> + return false;
> +
> + return true;
> +}
> +
> static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
> {
> struct vm_area_struct *vma;
> @@ -1724,14 +1761,6 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
> spinlock_t *ptl;
> bool success = false;
>
> - /*
> - * 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.
> - */
> - if (READ_ONCE(vma->anon_vma))
> - continue;
> -
> addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
> if (addr & ~HPAGE_PMD_MASK ||
> vma->vm_end < addr + HPAGE_PMD_SIZE)
> @@ -1743,14 +1772,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
>
> if (hpage_collapse_test_exit(mm))
> continue;
> - /*
> - * When a vma is registered with uffd-wp, we cannot recycle
> - * the page table because there may be pte markers installed.
> - * Other vmas can still have the same file mapped hugely, but
> - * skip this one: it will always be mapped in small page size
> - * for uffd-wp registered ranges.
> - */
> - if (userfaultfd_wp(vma))
> +
> + if (!file_backed_vma_is_retractable(vma))
> continue;
>
> /* PTEs were notified when unmapped; but now for the PMD? */
> @@ -1777,15 +1800,15 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
> spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
>
> /*
> - * Huge page lock is still held, so normally the page table
> - * must remain empty; and we have already skipped anon_vma
> - * and userfaultfd_wp() vmas. But since the mmap_lock is not
> - * held, it is still possible for a racing userfaultfd_ioctl()
> - * to have inserted ptes or markers. Now that we hold ptlock,
> - * repeating the anon_vma check protects from one category,
> - * and repeating the userfaultfd_wp() check from another.
> + * Huge page lock is still held, so normally the page table must
> + * remain empty; and we have already skipped anon_vma 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.
> */
> - if (likely(!vma->anon_vma && !userfaultfd_wp(vma))) {
> + if (likely(file_backed_vma_is_retractable(vma))) {
> pgt_pmd = pmdp_collapse_flush(vma, addr, pmd);
> pmdp_get_lockless_sync();
> success = true;
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 0b3280752bfb..5dbe40be7c65 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -1141,15 +1141,21 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)
> return -EINVAL;
>
> /*
> - * If we install guard markers, then the range is no longer
> - * empty from a page table perspective and therefore it's
> - * appropriate to have an anon_vma.
> - *
> - * This ensures that on fork, we copy page tables correctly.
> + * Set atomically under read lock. All pertinent readers will need to
> + * acquire an mmap/VMA write lock to read it. All remaining readers may
> + * or may not see the flag set, but we don't care.
> + */
> + vma_flag_set_atomic(vma, VM_MAYBE_GUARD_BIT);
> +
In general LGTM.
> + /*
> + * If anonymous and we are establishing page tables the VMA ought to
> + * have an anon_vma associated with it.
Do you know why? I know that as soon as we have anon folios in there we
need it, but is it still required for guard regions? Patch #5 should
handle the for case I guess.
Which other code depends on that?
--
Cheers
David
next prev parent reply other threads:[~2025-11-19 9:16 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-18 10:17 [PATCH v4 0/9] introduce VM_MAYBE_GUARD and make it sticky Lorenzo Stoakes
2025-11-18 10:17 ` [PATCH v4 1/9] mm: introduce VM_MAYBE_GUARD and make visible in /proc/$pid/smaps Lorenzo Stoakes
2025-11-20 5:48 ` Lance Yang
2025-11-18 10:17 ` [PATCH v4 2/9] mm: add atomic VMA flags and set VM_MAYBE_GUARD as such Lorenzo Stoakes
2025-11-19 9:06 ` David Hildenbrand (Red Hat)
2025-11-20 6:11 ` Lance Yang
2025-11-18 10:17 ` [PATCH v4 3/9] mm: update vma_modify_flags() to handle residual flags, document Lorenzo Stoakes
2025-11-19 16:27 ` Vlastimil Babka
2025-11-19 16:36 ` Pedro Falcato
2025-11-18 10:17 ` [PATCH v4 4/9] mm: implement sticky VMA flags Lorenzo Stoakes
2025-11-21 16:09 ` David Hildenbrand (Red Hat)
2025-11-18 10:17 ` [PATCH v4 5/9] mm: introduce copy-on-fork VMAs and make VM_MAYBE_GUARD one Lorenzo Stoakes
2025-11-19 9:17 ` David Hildenbrand (Red Hat)
2025-11-18 10:17 ` [PATCH v4 6/9] mm: set the VM_MAYBE_GUARD flag on guard region install Lorenzo Stoakes
2025-11-19 9:16 ` David Hildenbrand (Red Hat) [this message]
2025-11-19 9:27 ` Lorenzo Stoakes
2025-11-18 10:17 ` [PATCH v4 7/9] tools/testing/vma: add VMA sticky userland tests Lorenzo Stoakes
2025-11-18 10:17 ` [PATCH v4 8/9] tools/testing/selftests/mm: add MADV_COLLAPSE test case Lorenzo Stoakes
2025-11-18 10:17 ` [PATCH v4 9/9] tools/testing/selftests/mm: add smaps visibility guard region test Lorenzo Stoakes
2025-11-18 17:33 ` [PATCH v4 0/9] introduce VM_MAYBE_GUARD and make it sticky Andrew Morton
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=61f7c6d2-a15e-4c6a-9704-0e3db65eed3c@kernel.org \
--to=david@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=avagin@gmail.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=corbet@lwn.net \
--cc=dev.jain@arm.com \
--cc=jannh@google.com \
--cc=lance.yang@linux.dev \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=mhocko@suse.com \
--cc=npache@redhat.com \
--cc=pfalcato@suse.de \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
--cc=ziy@nvidia.com \
/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