mm: Fix memory leak on mm_init error handling
Subsystems (5)
PATCH 1/1 mm: Fix memory leak on mm_init error handling
commit f1a7941243c1 ("mm: convert mm's rss stats into percpu_counter") introduces a memory leak by missing a call to destroy_context() when a percpu_counter fails to allocate.
Before introducing the per-cpu counter allocations, init_new_context() was the last call that could fail in mm_init(), and thus there was no need to ever invoke destroy_context() in the error paths. Adding the following percpu counter allocations adds error paths after init_new_context(), which means its associated destroy_context() needs to be called when percpu counters fail to allocate.
--- kernel/fork.c | 1 + 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
@@ -1171,6 +1171,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, fail_pcpu:while (i >0 )percpu_counter_destroy (&mm->rss_stat[--i]); +destroy_context (mm); fail_nocontext:mm_free_pgd (mm); fail_nopgd:
Signature · 1 line
2.25.1
Quoted 11 lines · Mathieu Desnoyers
commit
f1a7941243c1("mm: convert mm's rss stats intopercpu_counter") introduces a memory leak by missing a call todestroy_context()when apercpu_counterfails to allocate.Before introducing the per-cpu counter allocations,
init_new_context()was the last call that could fail inmm_init(), and thus there was no need to ever invokedestroy_context()in the error paths. Adding the following percpu counter allocations adds error paths afterinit_new_context(), which means its associateddestroy_context()needs to be called when percpu counters fail to allocate.
Quoted 6 lines
Signed-off-by Mathieu Desnoyers <[email protected]>Cc Shakeel Butt <[email protected]>Cc Andrew Morton <[email protected]>Cc Marek Szyprowski <[email protected]>Cc [email protected] # 6.2
Andrew Morton
maintainerQuoted 16 lines · On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers…
commit
f1a7941243c1("mm: convert mm's rss stats intopercpu_counter") introduces a memory leak by missing a call todestroy_context()when apercpu_counterfails to allocate.Before introducing the per-cpu counter allocations,
init_new_context()was the last call that could fail inmm_init(), and thus there was no need to ever invokedestroy_context()in the error paths. Adding the following percpu counter allocations adds error paths afterinit_new_context(), which means its associateddestroy_context()needs to be called when percpu counters fail to allocate....
kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 1177
Quoted 7 lines
kernel/fork.c +1
-0@@ -1171,6 +1171,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, fail_pcpu:while (i >0 )percpu_counter_destroy (&mm->rss_stat[--i]); +destroy_context (mm); fail_nocontext:mm_free_pgd (mm); fail_nopgd:
Is there really a leak? I wasn't able to find a version of init_new_context() which performs allocation.
Quoted 16 lines · Andrew Morton
Quoted 13 lines · On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers…
commit
f1a7941243c1("mm: convert mm's rss stats intopercpu_counter") introduces a memory leak by missing a call todestroy_context()when apercpu_counterfails to allocate.Before introducing the per-cpu counter allocations,
init_new_context()was the last call that could fail inmm_init(), and thus there was no need to ever invokedestroy_context()in the error paths. Adding the following percpu counter allocations adds error paths afterinit_new_context(), which means its associateddestroy_context()needs to be called when percpu counters fail to allocate....
Quoted 14 lines
kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 1177
Quoted 10 lines
kernel/fork.c +1
-0@@ -1171,6 +1171,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, fail_pcpu:while (i >0 )percpu_counter_destroy (&mm->rss_stat[--i]); +destroy_context (mm); fail_nocontext:mm_free_pgd (mm); fail_nopgd:Is there really a leak? I wasn't able to find a version of
init_new_context()which performs allocation.
There are more than 20 archs defining this function and I couldn't check each one of them. I think we can assume there might be new allocation in the future.
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers…
commit
f1a7941243c1("mm: convert mm's rss stats intopercpu_counter") introduces a memory leak by missing a call todestroy_context()when apercpu_counterfails to allocate.Before introducing the per-cpu counter allocations,
init_new_context()was the last call that could fail inmm_init(), and thus there was no need to ever invokedestroy_context()in the error paths. Adding the following percpu counter allocations adds error paths afterinit_new_context(), which means its associateddestroy_context()needs to be called when percpu counters fail to allocate....
Quoted 13 lines
kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 1177
Quoted 10 lines
kernel/fork.c +1
-0@@ -1171,6 +1171,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, fail_pcpu:while (i >0 )percpu_counter_destroy (&mm->rss_stat[--i]); +destroy_context (mm); fail_nocontext:mm_free_pgd (mm); fail_nopgd:Is there really a leak? I wasn't able to find a version of
init_new_context()which performs allocation.
AFAIU, at least on powerpc:
arch/powerpc/mm/book3s64/mmu_context.c: init_new_context() calls radix__init_new_context() or hash__init_new_context() which leak IDs through ida_alloc_range.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com