From: Pedro Falcato <pfalcato@suse.de>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Jonathan Corbet <corbet@lwn.net>,
David Hildenbrand <david@redhat.com>,
"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>,
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 v2 2/5] mm: add atomic VMA flags, use VM_MAYBE_GUARD as such
Date: Thu, 6 Nov 2025 14:45:06 +0000 [thread overview]
Message-ID: <y6a4qchmj7jnaogx6u5a3i6lni7v54lj25ipwb7tdtakcudakr@vm6vt7eumxax> (raw)
In-Reply-To: <94935cf140e3279c234b39e0d976c4718c547c73.1762422915.git.lorenzo.stoakes@oracle.com>
On Thu, Nov 06, 2025 at 10:46:13AM +0000, Lorenzo Stoakes wrote:
> This patch adds the ability to atomically set VMA flags with only the mmap
> read/VMA read lock held.
>
> As this could be hugely problematic for VMA flags in general given that all
> other accesses are non-atomic and serialised by the mmap/VMA locks, we
> implement this with a strict allow-list - that is, only designated flags
> are allowed to do this.
>
> We make VM_MAYBE_GUARD one of these flags, and then set it under the mmap
> read flag upon guard region installation.
>
> The places where this flag is used currently and matter are:
>
> * VMA merge - performed under mmap/VMA write lock, therefore excluding
> racing writes.
>
> * /proc/$pid/smaps - can race the write, however this isn't meaningful as
> the flag write is performed at the point of the guard region being
> established, and thus an smaps reader can't reasonably expect to avoid
> races. Due to atomicity, a reader will observe either the flag being set
> or not. Therefore consistency will be maintained.
>
> In all other cases the flag being set is irrelevant and atomicity
> guarantees other flags will be read correctly.
Probably important to write down that the only reason why this doesn't make
KCSAN have a small stroke is that we are only changing one bit. i.e we can
only have one bit of atomic flags before annotating every reader.
(Source: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/kcsan/permissive.h#n51)
> We additionally update madvise_guard_install() to ensure that
> anon_vma_prepare() is set for anonymous VMAs to maintain consistency with
> the assumption that any anonymous VMA with page tables will have an
> anon_vma set, and any with an anon_vma unset will not have page tables
> established.
Isn't that what we already had? Or do you mean "*only* set for anonymous VMAs"?
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
With the nits below and above addressed:
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
> ---
> include/linux/mm.h | 23 +++++++++++++++++++++++
> mm/madvise.c | 22 ++++++++++++++--------
> 2 files changed, 37 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 2a5516bff75a..2ea65c646212 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -518,6 +518,9 @@ extern unsigned int kobjsize(const void *objp);
> /* This mask represents all the VMA flag bits used by mlock */
> #define VM_LOCKED_MASK (VM_LOCKED | VM_LOCKONFAULT)
>
> +/* These flags can be updated atomically via VMA/mmap read lock. */
> +#define VM_ATOMIC_SET_ALLOWED VM_MAYBE_GUARD
> +
> /* Arch-specific flags to clear when updating VM flags on protection change */
> #ifndef VM_ARCH_CLEAR
> # define VM_ARCH_CLEAR VM_NONE
> @@ -860,6 +863,26 @@ static inline void vm_flags_mod(struct vm_area_struct *vma,
> __vm_flags_mod(vma, set, clear);
> }
>
> +/*
> + * Set VMA flag atomically. Requires only VMA/mmap read lock. Only specific
> + * valid flags are allowed to do this.
> + */
> +static inline void vma_flag_set_atomic(struct vm_area_struct *vma,
> + int bit)
> +{
> + const vm_flags_t mask = BIT(bit);
> +
> + /* mmap read lock/VMA read lock must be held. */
> + if (!rwsem_is_locked(&vma->vm_mm->mmap_lock))
> + vma_assert_locked(vma);
> +
> + /* Only specific flags are permitted */
> + if (WARN_ON_ONCE(!(mask & VM_ATOMIC_SET_ALLOWED)))
> + return;
VM_WARN_ON_ONCE?
--
Pedro
next prev parent reply other threads:[~2025-11-06 14:45 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-06 10:46 [PATCH v2 0/5] introduce VM_MAYBE_GUARD and make it sticky Lorenzo Stoakes
2025-11-06 10:46 ` [PATCH v2 1/5] mm: introduce VM_MAYBE_GUARD and make visible in /proc/$pid/smaps Lorenzo Stoakes
2025-11-06 11:12 ` Vlastimil Babka
2025-11-06 13:56 ` Lorenzo Stoakes
2025-11-06 14:27 ` Pedro Falcato
2025-11-06 14:54 ` Lorenzo Stoakes
2025-11-06 14:58 ` Lorenzo Stoakes
2025-11-07 9:13 ` Alice Ryhl
2025-11-07 9:44 ` Lorenzo Stoakes
2025-11-07 12:12 ` Alice Ryhl
2025-11-07 12:40 ` Lorenzo Stoakes
2025-11-06 10:46 ` [PATCH v2 2/5] mm: add atomic VMA flags, use VM_MAYBE_GUARD as such Lorenzo Stoakes
2025-11-06 11:31 ` Vlastimil Babka
2025-11-06 14:03 ` Lorenzo Stoakes
2025-11-06 14:45 ` Pedro Falcato [this message]
2025-11-06 15:03 ` Lorenzo Stoakes
2025-11-06 10:46 ` [PATCH v2 3/5] mm: implement sticky, copy on fork VMA flags Lorenzo Stoakes
2025-11-06 13:46 ` Vlastimil Babka
2025-11-06 14:18 ` Lorenzo Stoakes
2025-11-06 14:33 ` Vlastimil Babka
2025-11-06 15:03 ` Pedro Falcato
2025-11-06 10:46 ` [PATCH v2 4/5] tools/testing/vma: add VMA sticky userland tests Lorenzo Stoakes
2025-11-06 10:46 ` [PATCH v2 5/5] selftests/mm/guard-regions: add smaps visibility test Lorenzo Stoakes
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=y6a4qchmj7jnaogx6u5a3i6lni7v54lj25ipwb7tdtakcudakr@vm6vt7eumxax \
--to=pfalcato@suse.de \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=avagin@gmail.com \
--cc=corbet@lwn.net \
--cc=david@redhat.com \
--cc=jannh@google.com \
--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=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
/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