Re: [PATCH] mm: handle potential NULL return from anon_vma_name_reuse()
From: David Hildenbrand (Arm)
Date: Tue Apr 21 2026 - 04:58:44 EST
On 4/21/26 10:50, Ye Liu wrote:
> From: Ye Liu <liuye@xxxxxxxxxx>
>
> The anon_vma_name_reuse() function may return NULL if memory allocation
> fails in anon_vma_name_alloc(). Currently, callers dup_anon_vma_name()
> and replace_anon_vma_name() do not check the return value, which could
> lead to NULL pointer dereferences.
When, where?
If we set vma->anon_name = NULL, it just looks like there is no anon
VMA, or what am I missing?
>
> This patch adds proper error handling:
> - In dup_anon_vma_name(), if anon_vma_name_reuse() returns NULL, emit a
> warning via WARN_ON_ONCE(1) since this is an unexpected condition.
> - In replace_anon_vma_name(), return -ENOMEM to propagate the allocation
> failure to the caller.
>
> These changes improve robustness against memory allocation failures.
>
> Signed-off-by: Ye Liu <liuye@xxxxxxxxxx>
> ---
> include/linux/mm_inline.h | 12 +++++++++---
> mm/madvise.c | 7 ++++++-
> 2 files changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> index a171070e15f0..9bbaf8287806 100644
> --- a/include/linux/mm_inline.h
> +++ b/include/linux/mm_inline.h
> @@ -421,9 +421,15 @@ static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma,
> struct vm_area_struct *new_vma)
> {
> struct anon_vma_name *anon_name = anon_vma_name(orig_vma);
> -
> - if (anon_name)
> - new_vma->anon_name = anon_vma_name_reuse(anon_name);
> + struct anon_vma_name *new_name;
> +
> + if (anon_name) {
> + new_name = anon_vma_name_reuse(anon_name);
> + if (new_name)
> + new_vma->anon_name = new_name;
> + else
> + WARN_ON_ONCE(1);
Triggering a warning when an allocation fails? Certainly not.
--
Cheers,
David