From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Kalesh Singh <kaleshsingh@google.com>
Cc: akpm@linux-foundation.org, minchan@kernel.org, david@redhat.com,
Liam.Howlett@oracle.com, rppt@kernel.org, pfalcato@suse.de,
kernel-team@android.com, android-mm@google.com,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Kees Cook <kees@kernel.org>, Vlastimil Babka <vbabka@suse.cz>,
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>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
Jann Horn <jannh@google.com>, Shuah Khan <shuah@kernel.org>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v2 5/7] mm: harden vma_count against direct modification
Date: Thu, 18 Sep 2025 15:52:22 +0100 [thread overview]
Message-ID: <eb254603-edbd-4bc6-a4bd-57b6a85191b5@lucifer.local> (raw)
In-Reply-To: <20250915163838.631445-6-kaleshsingh@google.com>
On Mon, Sep 15, 2025 at 09:36:36AM -0700, Kalesh Singh wrote:
> To make VMA counting more robust, prevent direct modification of the
> mm->vma_count field. This is achieved by making the public-facing
> member const via a union and requiring all modifications to go
> through a new set of helper functions the operate on a private
> __vma_count.
>
> While there are no other invariants tied to vma_count currently, this
> structural change improves maintainability; as it creates a single,
> centralized point for any future logic, such as adding debug checks
> or updating related statistics (in subsequent patches).
>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>
> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Minchan Kim <minchan@kernel.org>
> Cc: Pedro Falcato <pfalcato@suse.de>
> Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Hmmm I"m not sure about this one.
I think this is a 'we don't need it' situation, and it's making everything a bit
uglier.
I especially hate vma_count_add() and vma_count_sub(). You're essentially
overridding the whole concept in these cases to make stuff that's already in
place work in those cases
I don't think this really adds much honestly.
(You're also clearly missing cases as the kernel bot has found issues)
> ---
> include/linux/mm.h | 25 +++++++++++++++++++++++++
> include/linux/mm_types.h | 5 ++++-
> kernel/fork.c | 2 +-
> mm/mmap.c | 2 +-
> mm/vma.c | 12 ++++++------
> tools/testing/vma/vma.c | 2 +-
> tools/testing/vma/vma_internal.h | 30 +++++++++++++++++++++++++++++-
> 7 files changed, 67 insertions(+), 11 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 138bab2988f8..8bad1454984c 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -4219,4 +4219,29 @@ static inline bool snapshot_page_is_faithful(const struct page_snapshot *ps)
>
> void snapshot_page(struct page_snapshot *ps, const struct page *page);
>
> +static inline void vma_count_init(struct mm_struct *mm)
> +{
> + ACCESS_PRIVATE(mm, __vma_count) = 0;
> +}
> +
> +static inline void vma_count_add(struct mm_struct *mm, int nr_vmas)
> +{
> + ACCESS_PRIVATE(mm, __vma_count) += nr_vmas;
> +}
> +
> +static inline void vma_count_sub(struct mm_struct *mm, int nr_vmas)
> +{
> + vma_count_add(mm, -nr_vmas);
> +}
> +
> +static inline void vma_count_inc(struct mm_struct *mm)
> +{
> + vma_count_add(mm, 1);
> +}
> +
> +static inline void vma_count_dec(struct mm_struct *mm)
> +{
> + vma_count_sub(mm, 1);
> +}
> +
> #endif /* _LINUX_MM_H */
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index 4343be2f9e85..2ea8fc722aa2 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -1020,7 +1020,10 @@ struct mm_struct {
> #ifdef CONFIG_MMU
> atomic_long_t pgtables_bytes; /* size of all page tables */
> #endif
> - int vma_count; /* number of VMAs */
> + union {
> + const int vma_count; /* number of VMAs */
> + int __private __vma_count;
> + };
>
> spinlock_t page_table_lock; /* Protects page tables and some
> * counters
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 8fcbbf947579..ea9eff416e51 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -1037,7 +1037,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
> mmap_init_lock(mm);
> INIT_LIST_HEAD(&mm->mmlist);
> mm_pgtables_bytes_init(mm);
> - mm->vma_count = 0;
> + vma_count_init(mm);
> mm->locked_vm = 0;
> atomic64_set(&mm->pinned_vm, 0);
> memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
> diff --git a/mm/mmap.c b/mm/mmap.c
> index c6769394a174..30ddd550197e 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1828,7 +1828,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
> */
> vma_iter_bulk_store(&vmi, tmp);
>
> - mm->vma_count++;
> + vma_count_inc(mm);
>
> if (tmp->vm_ops && tmp->vm_ops->open)
> tmp->vm_ops->open(tmp);
> diff --git a/mm/vma.c b/mm/vma.c
> index 64f4e7c867c3..0cd3cb472220 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -352,7 +352,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,
> * (it may either follow vma or precede it).
> */
> vma_iter_store_new(vmi, vp->insert);
> - mm->vma_count++;
> + vma_count_inc(mm);
> }
>
> if (vp->anon_vma) {
> @@ -383,7 +383,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,
> }
> if (vp->remove->anon_vma)
> anon_vma_merge(vp->vma, vp->remove);
> - mm->vma_count--;
> + vma_count_dec(mm);
> mpol_put(vma_policy(vp->remove));
> if (!vp->remove2)
> WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end);
> @@ -1266,7 +1266,7 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
> struct mm_struct *mm;
>
> mm = current->mm;
> - mm->vma_count -= vms->vma_count;
> + vma_count_sub(mm, vms->vma_count);
> mm->locked_vm -= vms->locked_vm;
> if (vms->unlock)
> mmap_write_downgrade(mm);
> @@ -1795,7 +1795,7 @@ int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
> vma_start_write(vma);
> vma_iter_store_new(&vmi, vma);
> vma_link_file(vma);
> - mm->vma_count++;
> + vma_count_inc(mm);
> validate_mm(mm);
> return 0;
> }
> @@ -2495,7 +2495,7 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)
> /* Lock the VMA since it is modified after insertion into VMA tree */
> vma_start_write(vma);
> vma_iter_store_new(vmi, vma);
> - map->mm->vma_count++;
> + vma_count_inc(map->mm);
> vma_link_file(vma);
>
> /*
> @@ -2810,7 +2810,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
> if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
> goto mas_store_fail;
>
> - mm->vma_count++;
> + vma_count_inc(mm);
> validate_mm(mm);
> out:
> perf_event_mmap(vma);
> diff --git a/tools/testing/vma/vma.c b/tools/testing/vma/vma.c
> index 69fa7d14a6c2..ee5a1e2365e0 100644
> --- a/tools/testing/vma/vma.c
> +++ b/tools/testing/vma/vma.c
> @@ -261,7 +261,7 @@ static int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
> }
>
> mtree_destroy(&mm->mm_mt);
> - mm->vma_count = 0;
> + vma_count_init(mm);
> return count;
> }
>
> diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h
> index 15525b86145d..6e724ba1adf4 100644
> --- a/tools/testing/vma/vma_internal.h
> +++ b/tools/testing/vma/vma_internal.h
> @@ -251,7 +251,10 @@ struct mutex {};
>
> struct mm_struct {
> struct maple_tree mm_mt;
> - int vma_count; /* number of VMAs */
> + union {
> + const int vma_count; /* number of VMAs */
> + int __vma_count;
> + };
> unsigned long total_vm; /* Total pages mapped */
> unsigned long locked_vm; /* Pages that have PG_mlocked set */
> unsigned long data_vm; /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
> @@ -1526,4 +1529,29 @@ static int vma_count_remaining(const struct mm_struct *mm)
> return (max_count > vma_count) ? (max_count - vma_count) : 0;
> }
>
> +static inline void vma_count_init(struct mm_struct *mm)
> +{
> + mm->__vma_count = 0;
> +}
> +
> +static inline void vma_count_add(struct mm_struct *mm, int nr_vmas)
> +{
> + mm->__vma_count += nr_vmas;
> +}
> +
> +static inline void vma_count_sub(struct mm_struct *mm, int nr_vmas)
> +{
> + vma_count_add(mm, -nr_vmas);
> +}
> +
> +static inline void vma_count_inc(struct mm_struct *mm)
> +{
> + vma_count_add(mm, 1);
> +}
> +
> +static inline void vma_count_dec(struct mm_struct *mm)
> +{
> + vma_count_sub(mm, 1);
> +}
> +
> #endif /* __MM_VMA_INTERNAL_H */
> --
> 2.51.0.384.g4c02a37b29-goog
>
next prev parent reply other threads:[~2025-09-18 14:52 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-15 16:36 [PATCH v2 0/7] vma count: fixes, test and improvements Kalesh Singh
2025-09-15 16:36 ` [PATCH v2 1/7] mm: fix off-by-one error in VMA count limit checks Kalesh Singh
2025-09-15 22:36 ` Andrew Morton
2025-09-16 14:20 ` Jonathan Corbet
2025-09-17 1:16 ` Andrew Morton
2025-09-16 9:45 ` Pedro Falcato
2025-09-17 7:44 ` SeongJae Park
2025-09-17 10:52 ` David Hildenbrand
2025-09-18 11:31 ` Pedro Falcato
2025-09-18 13:53 ` Lorenzo Stoakes
2025-09-15 16:36 ` [PATCH v2 2/7] mm/selftests: add max_vma_count tests Kalesh Singh
2025-09-17 10:58 ` David Hildenbrand
2025-09-17 16:49 ` Kalesh Singh
2025-09-18 14:42 ` Lorenzo Stoakes
2025-09-18 16:21 ` Kalesh Singh
2025-09-15 16:36 ` [PATCH v2 3/7] mm: introduce vma_count_remaining() Kalesh Singh
2025-09-17 13:38 ` David Hildenbrand
2025-09-17 17:10 ` Kalesh Singh
2025-09-18 13:20 ` Lorenzo Stoakes
2025-09-18 13:26 ` Lorenzo Stoakes
2025-09-18 14:31 ` Lorenzo Stoakes
2025-09-18 15:52 ` Kalesh Singh
2025-09-15 16:36 ` [PATCH v2 4/7] mm: rename mm_struct::map_count to vma_count Kalesh Singh
2025-09-17 13:41 ` David Hildenbrand
2025-09-18 11:46 ` Pedro Falcato
2025-09-18 14:48 ` Lorenzo Stoakes
2025-09-15 16:36 ` [PATCH v2 5/7] mm: harden vma_count against direct modification Kalesh Singh
2025-09-18 14:52 ` Lorenzo Stoakes [this message]
2025-09-18 15:43 ` Kalesh Singh
2025-09-15 16:36 ` [PATCH v2 6/7] mm: add assertion for VMA count limit Kalesh Singh
2025-09-17 13:44 ` David Hildenbrand
2025-09-17 17:22 ` Kalesh Singh
2025-09-17 18:34 ` David Hildenbrand
2025-09-17 20:31 ` Kalesh Singh
2025-09-18 11:48 ` Pedro Falcato
2025-09-18 13:30 ` Lorenzo Stoakes
2025-09-15 16:36 ` [PATCH v2 7/7] mm/tracing: introduce max_vma_count_exceeded trace event Kalesh Singh
2025-09-15 23:41 ` Steven Rostedt
2025-09-16 1:19 ` Kalesh Singh
2025-09-16 15:52 ` Steven Rostedt
2025-09-16 17:36 ` Kalesh Singh
2025-09-16 17:48 ` Steven Rostedt
2025-09-16 17:57 ` Kalesh Singh
2025-09-16 18:02 ` Steven Rostedt
2025-09-16 18:23 ` Kalesh Singh
2025-09-16 18:51 ` Steven Rostedt
2025-09-16 20:08 ` Kalesh Singh
2025-09-18 11:38 ` Pedro Falcato
2025-09-18 14:48 ` Steven Rostedt
2025-09-18 13:42 ` Lorenzo Stoakes
2025-09-18 13:51 ` Lorenzo Stoakes
2025-09-18 15:55 ` Kalesh Singh
2025-09-15 22:34 ` [PATCH v2 0/7] vma count: fixes, test and improvements Andrew Morton
2025-09-15 23:10 ` Kalesh Singh
2025-09-16 0:05 ` Andrew Morton
2025-09-16 1:23 ` Kalesh Singh
2025-09-16 10:12 ` Lorenzo Stoakes
2025-09-16 17:47 ` Kalesh Singh
2025-09-17 2:16 ` Andrew Morton
2025-09-17 5:36 ` Lorenzo Stoakes
2025-09-17 23:32 ` Andrew Morton
2025-09-18 10:29 ` Lorenzo Stoakes
2025-09-18 12:07 ` David Hildenbrand
2025-09-18 12:49 ` Lorenzo Stoakes
2025-09-18 20:59 ` 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=eb254603-edbd-4bc6-a4bd-57b6a85191b5@lucifer.local \
--to=lorenzo.stoakes@oracle.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=android-mm@google.com \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=david@redhat.com \
--cc=dietmar.eggemann@arm.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=juri.lelli@redhat.com \
--cc=kaleshsingh@google.com \
--cc=kees@kernel.org \
--cc=kernel-team@android.com \
--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=mathieu.desnoyers@efficios.com \
--cc=mgorman@suse.de \
--cc=mhiramat@kernel.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
--cc=vincent.guittot@linaro.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.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
all inboxes | Powered by JetHome®