Re: [PATCH 1/1] mm/madvise: initialize prev pointer in madvise_walk_vmas

From: Barry Song
Date: Mon Jun 16 2025 - 22:24:48 EST


On Tue, Jun 17, 2025 at 2:05 PM Lance Yang <ioworker0@xxxxxxxxx> wrote:
>
> From: Lance Yang <lance.yang@xxxxxxxxx>
>
> 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.

Is there any read-before-write case here? I haven't found one.

It also looks like we're assuming that *prev == NULL implies
a specific condition:

*prev = NULL; /* tell sys_madvise we drop mmap_lock */

*prev = NULL; /* mmap_lock has been dropped, prev is stale */

>
> Signed-off-by: Lance Yang <lance.yang@xxxxxxxxx>
> ---
> 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
>

Thanks
Barry