linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/2] mm/memcontrol: add check for allocation failure in mem_cgroup_init()
@ 2023-06-15  7:32 Haifeng Xu
  2023-06-15  8:26 ` Michal Hocko
  0 siblings, 1 reply; 3+ messages in thread
From: Haifeng Xu @ 2023-06-15  7:32 UTC (permalink / raw)
  To: mhocko
  Cc: roman.gushchin, hannes, shakeelb, akpm, cgroups, linux-mm,
	linux-kernel, Haifeng Xu

If mem_cgroup_init() fails to allocate mem_cgroup_tree_per_node, we
should not try to initilaize it. Add check for this case to avoid
potential NULL pointer dereference.

Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
---
 mm/memcontrol.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index c73c5fb33f65..7ebf64e48b25 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -7422,6 +7422,8 @@ static int __init mem_cgroup_init(void)
 		struct mem_cgroup_tree_per_node *rtpn;
 
 		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
+		if (!rtpn)
+			continue;
 
 		rtpn->rb_root = RB_ROOT;
 		rtpn->rb_rightmost = NULL;
-- 
2.25.1



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] mm/memcontrol: add check for allocation failure in mem_cgroup_init()
  2023-06-15  7:32 [PATCH 2/2] mm/memcontrol: add check for allocation failure in mem_cgroup_init() Haifeng Xu
@ 2023-06-15  8:26 ` Michal Hocko
  2023-06-16  8:47   ` Haifeng Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Michal Hocko @ 2023-06-15  8:26 UTC (permalink / raw)
  To: Haifeng Xu
  Cc: roman.gushchin, hannes, shakeelb, akpm, cgroups, linux-mm, linux-kernel

On Thu 15-06-23 07:32:26, Haifeng Xu wrote:
> If mem_cgroup_init() fails to allocate mem_cgroup_tree_per_node, we
> should not try to initilaize it. Add check for this case to avoid
> potential NULL pointer dereference.

Technically yes and it seems that all users of soft_limit_tree.rb_tree_per_node
correctly check for NULL so this would be graceful failure handling. At
least superficially because the feature itself would be semi-broken when
used. But more practically this is a 24B allocation and if we fail to
allocate that early during the boot we are screwed anyway. Would such
a system have any chance to boot all the way to userspace? Woul any
userspace actually work?

Is this patch motivated by a code reading or is there any actual
practical upside of handling the error here?
 
> Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
> ---
>  mm/memcontrol.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index c73c5fb33f65..7ebf64e48b25 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -7422,6 +7422,8 @@ static int __init mem_cgroup_init(void)
>  		struct mem_cgroup_tree_per_node *rtpn;
>  
>  		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
> +		if (!rtpn)
> +			continue;
>  
>  		rtpn->rb_root = RB_ROOT;
>  		rtpn->rb_rightmost = NULL;
> -- 
> 2.25.1

-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] mm/memcontrol: add check for allocation failure in mem_cgroup_init()
  2023-06-15  8:26 ` Michal Hocko
@ 2023-06-16  8:47   ` Haifeng Xu
  0 siblings, 0 replies; 3+ messages in thread
From: Haifeng Xu @ 2023-06-16  8:47 UTC (permalink / raw)
  To: Michal Hocko
  Cc: roman.gushchin, hannes, shakeelb, akpm, cgroups, linux-mm, linux-kernel



On 2023/6/15 16:26, Michal Hocko wrote:
> On Thu 15-06-23 07:32:26, Haifeng Xu wrote:
>> If mem_cgroup_init() fails to allocate mem_cgroup_tree_per_node, we
>> should not try to initilaize it. Add check for this case to avoid
>> potential NULL pointer dereference.
> 
> Technically yes and it seems that all users of soft_limit_tree.rb_tree_per_node
> correctly check for NULL so this would be graceful failure handling. At
> least superficially because the feature itself would be semi-broken when
> used. But more practically this is a 24B allocation and if we fail to
> allocate that early during the boot we are screwed anyway. Would such
> a system have any chance to boot all the way to userspace? Woul any
> userspace actually work?
> 

The memory request is too small and It's unlikely to fail during early init.
If it fails, I think the system won't work.

> Is this patch motivated by a code reading or is there any actual
> practical upside of handling the error here?
>  

There is no real world problem, just from code review.

>> Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
>> ---
>>  mm/memcontrol.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
>> index c73c5fb33f65..7ebf64e48b25 100644
>> --- a/mm/memcontrol.c
>> +++ b/mm/memcontrol.c
>> @@ -7422,6 +7422,8 @@ static int __init mem_cgroup_init(void)
>>  		struct mem_cgroup_tree_per_node *rtpn;
>>  
>>  		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
>> +		if (!rtpn)
>> +			continue;
>>  
>>  		rtpn->rb_root = RB_ROOT;
>>  		rtpn->rb_rightmost = NULL;
>> -- 
>> 2.25.1
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-16  8:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15  7:32 [PATCH 2/2] mm/memcontrol: add check for allocation failure in mem_cgroup_init() Haifeng Xu
2023-06-15  8:26 ` Michal Hocko
2023-06-16  8:47   ` Haifeng Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox