linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Qiu-ji Chen <chenqiuji666@gmail.com>, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	baijiaju1990@gmail.com, stable@vger.kernel.org
Subject: Re: [PATCH] mm: fix a possible null pointer dereference in setup_zone_pageset()
Date: Thu, 7 Nov 2024 13:04:31 +0100	[thread overview]
Message-ID: <2a3d6cd7-ee80-4a60-939c-129e7d3e169d@redhat.com> (raw)
In-Reply-To: <20241107113447.402194-1-chenqiuji666@gmail.com>

On 07.11.24 12:34, Qiu-ji Chen wrote:
> The function call alloc_percpu() returns a pointer to the memory address,
> but it hasn't been checked. Our static analysis tool indicates that null
> pointer dereference may exist in pointer zone->per_cpu_pageset. It is
> always safe to judge the null pointer before use.
> 
> Signed-off-by: Qiu-ji Chen <chenqiuji666@gmail.com>
> Cc: stable@vger.kernel.org
> Fixes: 9420f89db2dd ("mm: move most of core MM initialization to mm/mm_init.c")
> ---
>   mm/page_alloc.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 8afab64814dc..5deae1193dc3 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -5703,8 +5703,14 @@ void __meminit setup_zone_pageset(struct zone *zone)
>   	/* Size may be 0 on !SMP && !NUMA */
>   	if (sizeof(struct per_cpu_zonestat) > 0)
>   		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
> +	if (!zone->per_cpu_pageset)
> +		return;

Don't we initialize this for all with &boot_pageset? How could this ever 
happen?

>   
>   	zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages);
> +	if (!zone->per_cpu_pageset) {
> +		free_percpu(zone->per_cpu_pageset);
> +		return;

If it's NULL, we free it. Why?

> +	}
>   	for_each_possible_cpu(cpu) {
>   		struct per_cpu_pages *pcp;
>   		struct per_cpu_zonestat *pzstats;

Also, how could core code ever recover if this function would return 
early, leaving something partially initialized?


The missing NULL check is concerning, but looking into alloc_percpu() we 
treat these as atomic allocations and would print a warning in case this 
would ever happen. So likely it never really happens in practice.

I wonder if we simply want to leave it unmodified (IOW set to 
&boot_pageset) in case the allocation fails. We'd already print a 
warning in this unexpected scenario.

-- 
Cheers,

David / dhildenb



      reply	other threads:[~2024-11-07 12:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-07 11:34 Qiu-ji Chen
2024-11-07 12:04 ` David Hildenbrand [this message]

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=2a3d6cd7-ee80-4a60-939c-129e7d3e169d@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=baijiaju1990@gmail.com \
    --cc=chenqiuji666@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=stable@vger.kernel.org \
    /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