From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Barry Song <21cnbao@gmail.com>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Barry Song <v-songbaohua@oppo.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
David Hildenbrand <david@redhat.com>,
Vlastimil Babka <vbabka@suse.cz>, Jann Horn <jannh@google.com>,
Suren Baghdasaryan <surenb@google.com>,
Lokesh Gidra <lokeshgidra@google.com>,
Tangquan Zheng <zhengtangquan@oppo.com>,
Qi Zheng <zhengqi.arch@bytedance.com>,
Lance Yang <ioworker0@gmail.com>
Subject: Re: [PATCH v4] mm: use per_vma lock for MADV_DONTNEED
Date: Wed, 18 Jun 2025 11:33:16 +0100 [thread overview]
Message-ID: <05d38430-3512-49b0-90da-1ae7a617a377@lucifer.local> (raw)
In-Reply-To: <CAGsJ_4yPg2AOxjorD3RPyu=Ko+7gpU1=-XWqQohvLWgGrzAEDQ@mail.gmail.com>
On Wed, Jun 18, 2025 at 06:11:26PM +0800, Barry Song wrote:
[sip]
> > ----8<----
> > From 1ffcaea75ebdaffe15805386f6d7733883d265a5 Mon Sep 17 00:00:00 2001
> > From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> > Date: Tue, 17 Jun 2025 14:35:13 +0100
> > Subject: [PATCH] mm/madvise: avoid any chance of uninitialised pointer deref
> >
> > If we were to extend madvise() to support more operations under VMA lock,
> > we could potentially dereference prev to uninitialised state in
> > madvise_update_vma().
> >
> > Avoid this by explicitly setting prev to vma before invoking the visit()
> > function.
> >
> > This has no impact on behaviour, as all visitors compatible with a VMA lock
> > do not require prev to be set to the previous VMA and at any rate we only
> > examine a single VMA in VMA lock mode.
> >
> > Reported-by: Lance Yang <ioworker0@gmail.com>
> > Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> > ---
> > mm/madvise.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index efe5d64e1175..0970623a0e98 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -1333,6 +1333,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
> > return madvise_guard_remove(vma, prev, start, end);
> > }
> >
> > + /* We cannot provide prev in this lock mode. */
> > + VM_WARN_ON_ONCE(arg->lock_mode == MADVISE_VMA_READ_LOCK);
>
> Thanks, Lorenzo.
> Do we even reach this point for MADVISE_MMAP_READ_LOCK cases?
> madvise_update_vma() attempts to merge or split VMAs—wouldn't that be
> a scenario that requires a write lock?
Well we're relying on happening to reach here with the correct lock afaict.
I'm going to be doing some follow-up series to clean all this up!
I'd rather keep this in here for now just to ensure we don't miss some stupidity
here.
Thanks!
>
> The prerequisite for using a VMA read lock is that the operation must
> be safe under an mmap read lock as well.
>
> > anon_name = anon_vma_name(vma);
> > anon_vma_name_get(anon_name);
> > error = madvise_update_vma(vma, prev, start, end, new_flags,
> > @@ -1549,6 +1551,7 @@ int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
> > if (madv_behavior && madv_behavior->lock_mode == MADVISE_VMA_READ_LOCK) {
> > vma = try_vma_read_lock(mm, madv_behavior, start, end);
> > if (vma) {
> > + prev = vma;
> > error = visit(vma, &prev, start, end, arg);
> > vma_end_read(vma);
> > return error;
> > --
> > 2.49.0
>
> Thanks
> Barry
next prev parent reply other threads:[~2025-06-18 10:33 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-07 22:01 Barry Song
2025-06-09 7:21 ` Qi Zheng
2025-06-17 13:38 ` Lorenzo Stoakes
2025-06-18 2:25 ` Lance Yang
2025-06-18 9:52 ` Barry Song
2025-06-18 10:18 ` David Hildenbrand
2025-06-18 10:30 ` Barry Song
2025-06-18 10:32 ` Barry Song
2025-06-18 13:05 ` Lance Yang
2025-06-18 13:13 ` David Hildenbrand
2025-06-18 10:11 ` Barry Song
2025-06-18 10:33 ` Lorenzo Stoakes [this message]
2025-06-18 10:36 ` Barry Song
2025-08-04 0:58 ` Lai, Yi
2025-08-04 7:19 ` Barry Song
2025-08-04 7:57 ` David Hildenbrand
2025-08-04 8:26 ` Qi Zheng
2025-08-04 8:30 ` David Hildenbrand
2025-08-04 8:49 ` Lai, Yi
2025-08-04 9:15 ` Barry Song
2025-08-04 9:35 ` Qi Zheng
2025-08-04 9:52 ` Qi Zheng
2025-08-04 10:04 ` Barry Song
2025-08-04 21:48 ` Barry Song
2025-08-05 2:52 ` Lai, Yi
2025-08-04 8:19 ` Barry Song
2025-11-04 8:34 ` Kefeng Wang
2025-11-04 9:01 ` Lorenzo Stoakes
2025-11-04 12:09 ` Kefeng Wang
2025-11-04 15:21 ` Lorenzo Stoakes
2025-11-05 1:04 ` Kefeng Wang
2025-11-17 23:35 ` Suren Baghdasaryan
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=05d38430-3512-49b0-90da-1ae7a617a377@lucifer.local \
--to=lorenzo.stoakes@oracle.com \
--cc=21cnbao@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=ioworker0@gmail.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lokeshgidra@google.com \
--cc=surenb@google.com \
--cc=v-songbaohua@oppo.com \
--cc=vbabka@suse.cz \
--cc=zhengqi.arch@bytedance.com \
--cc=zhengtangquan@oppo.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