From: Lance Yang <lance.yang@linux.dev>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: akpm@linux-foundation.org, 21cnbao@gmail.com, david@redhat.com,
Liam.Howlett@oracle.com, vbabka@suse.cz, jannh@google.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Lance Yang <ioworker0@gmail.com>
Subject: Re: [PATCH 1/1] mm/madvise: initialize prev pointer in madvise_walk_vmas
Date: Tue, 17 Jun 2025 17:21:03 +0800 [thread overview]
Message-ID: <142cb257-6c04-4a8f-9153-4759c50aff4e@linux.dev> (raw)
In-Reply-To: <6181fd25-6527-4cd0-b67f-2098191d262d@lucifer.local>
On 2025/6/17 16:50, Lorenzo Stoakes wrote:
> Lace - To simplify and not get bogged down in sub-threads am replying at the top
> level.
>
> TL;DR this fix is incorrect, but the issue is correct :)
>
> So the patch at [0] introduced by Barry changed things in a way that _appears_
> broken but in fact aren't, however we should do something about this, obviously.
>
> That patch added:
>
> if (madv_behavior && madv_behavior->lock_mode == MADVISE_VMA_READ_LOCK) {
> vma = try_vma_read_lock(mm, madv_behavior, start, end);
> if (vma) {
> error = visit(vma, &prev, start, end, arg);
> vma_end_read(vma);
> return error;
> }
> }
>
> And the problem is, in this case, we don't initialise prev.
>
> In all other cases, we do (under mmap lock):
>
> vma = find_vma_prev(mm, start, &prev);
> if (vma && start > vma->vm_start)
> prev = vma;
>
> The reason this isn't a problem is that the only madvise operation that
> currently supports this, madvise_dontneed_free() will initialise *prev = vma.
>
> BUT we really shouldn't be relying on this, so I attach a fixpatch.
>
> Given Barry's patch isn't mainline yet, I think this should just be squashed
> into that as a fix?
>
> It kind of sucks to do this, but it resolves any potential bug.
>
> I think a follow up is needed, as there's an implicit assumption it seems that
> prev is updated immediately for most callers, but of course anon_vma_name is a
> special snowflake.
>
> todos++;
Ah, please keep me in the loop ;)
>
> Lance - I suggest you reply to Barry's series with the below fix, or I can if
> you prefer?
Sure, go ahead!
Thanks,
Lance
>
> [0]: https://lore.kernel.org/linux-mm/20250607220150.2980-1-21cnbao@gmail.com/
>
> Thanks!
>
> On Tue, Jun 17, 2025 at 10:05:43AM +0800, Lance Yang wrote:
>> From: Lance Yang <lance.yang@linux.dev>
>>
>> The prev pointer was uninitialized, which could lead to undefined behavior
>> where its address is taken and passed to the visit() callback without being
>> assigned a value.
>>
>> Initializing it to NULL makes the code safer and prevents potential bugs
>> if a future callback function attempts to read from it.
>>
>> Signed-off-by: Lance Yang <lance.yang@linux.dev>
>> ---
>> mm/madvise.c | 4 ++--
>> 1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/madvise.c b/mm/madvise.c
>> index 267d8e4adf31..c87325000303 100644
>> --- a/mm/madvise.c
>> +++ b/mm/madvise.c
>> @@ -1536,10 +1536,10 @@ int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
>> struct vm_area_struct **prev, unsigned long start,
>> unsigned long end, void *arg))
>> {
>> + struct vm_area_struct *prev = NULL;
>> struct vm_area_struct *vma;
>> - struct vm_area_struct *prev;
>> - unsigned long tmp;
>> int unmapped_error = 0;
>> + unsigned long tmp;
>> int error;
>>
>> /*
>> --
>> 2.49.0
>>
>
> ----8<----
> From c8dc9f5b2929e389cac44b79201fff43e0ab8195 Mon Sep 17 00:00:00 2001
> From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Date: Tue, 17 Jun 2025 09:46:27 +0100
> Subject: [PATCH] fix
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> ---
> mm/madvise.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 267d8e4adf31..45ea4588e34e 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -1549,6 +1549,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
next prev parent reply other threads:[~2025-06-17 9:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-17 2:05 [PATCH 1/1] mm/madvise: initialize prev pointer in madvise_walk_vmas Lance Yang
2025-06-17 2:24 ` Barry Song
2025-06-17 4:57 ` Lance Yang
2025-06-17 5:19 ` Barry Song
2025-06-17 6:03 ` Lance Yang
2025-06-17 7:54 ` David Hildenbrand
2025-06-17 8:18 ` Lance Yang
2025-06-17 8:21 ` Lorenzo Stoakes
2025-06-17 8:28 ` David Hildenbrand
2025-06-17 8:34 ` Lorenzo Stoakes
2025-06-17 8:38 ` David Hildenbrand
2025-06-17 8:50 ` Lorenzo Stoakes
2025-06-17 8:53 ` David Hildenbrand
2025-06-17 8:43 ` Lorenzo Stoakes
2025-06-17 8:51 ` Lorenzo Stoakes
2025-06-17 8:26 ` Lorenzo Stoakes
2025-06-17 8:50 ` Lorenzo Stoakes
2025-06-17 9:21 ` Lance Yang [this message]
2025-06-17 9:26 ` 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=142cb257-6c04-4a8f-9153-4759c50aff4e@linux.dev \
--to=lance.yang@linux.dev \
--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=lorenzo.stoakes@oracle.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.