* [PATCH 1/1] mm: Fix memory leak on mm_init error handling
@ 2023-03-30 13:38 Mathieu Desnoyers
2023-03-30 16:52 ` Shakeel Butt
2023-03-30 19:42 ` Andrew Morton
0 siblings, 2 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2023-03-30 13:38 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-kernel, Mathieu Desnoyers, Shakeel Butt, Marek Szyprowski,
linux-mm, stable
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.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: linux-mm@kvack.org
Cc: stable@vger.kernel.org # 6.2
---
kernel/fork.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/fork.c b/kernel/fork.c
index c0257cbee093..c983c4fe3090 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -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:
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm: Fix memory leak on mm_init error handling
2023-03-30 13:38 [PATCH 1/1] mm: Fix memory leak on mm_init error handling Mathieu Desnoyers
@ 2023-03-30 16:52 ` Shakeel Butt
2023-03-30 19:42 ` Andrew Morton
1 sibling, 0 replies; 5+ messages in thread
From: Shakeel Butt @ 2023-03-30 16:52 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: Andrew Morton, linux-kernel, Marek Szyprowski, linux-mm, stable
On Thu, Mar 30, 2023 at 09:38:22AM -0400, Mathieu Desnoyers wrote:
> 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.
>
Fixes: f1a7941243c1 ("mm: convert mm's rss stats into percpu_counter")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: Shakeel Butt <shakeelb@google.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Marek Szyprowski <m.szyprowski@samsung.com>
> Cc: linux-mm@kvack.org
> Cc: stable@vger.kernel.org # 6.2
Acked-by: Shakeel Butt <shakeelb@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm: Fix memory leak on mm_init error handling
2023-03-30 13:38 [PATCH 1/1] mm: Fix memory leak on mm_init error handling Mathieu Desnoyers
2023-03-30 16:52 ` Shakeel Butt
@ 2023-03-30 19:42 ` Andrew Morton
2023-03-30 19:55 ` Shakeel Butt
2023-03-30 19:56 ` Mathieu Desnoyers
1 sibling, 2 replies; 5+ messages in thread
From: Andrew Morton @ 2023-03-30 19:42 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: linux-kernel, Shakeel Butt, Marek Szyprowski, linux-mm, stable
On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:
> 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.
>
> ...
>
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm: Fix memory leak on mm_init error handling
2023-03-30 19:42 ` Andrew Morton
@ 2023-03-30 19:55 ` Shakeel Butt
2023-03-30 19:56 ` Mathieu Desnoyers
1 sibling, 0 replies; 5+ messages in thread
From: Shakeel Butt @ 2023-03-30 19:55 UTC (permalink / raw)
To: Andrew Morton
Cc: Mathieu Desnoyers, linux-kernel, Marek Szyprowski, linux-mm, stable
On Thu, Mar 30, 2023 at 12:42 PM Andrew Morton
<akpm@linux-foundation.org> wrote:
>
> On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:
>
> > 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.
> >
> > ...
> >
> > --- a/kernel/fork.c
> > +++ b/kernel/fork.c
> > @@ -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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm: Fix memory leak on mm_init error handling
2023-03-30 19:42 ` Andrew Morton
2023-03-30 19:55 ` Shakeel Butt
@ 2023-03-30 19:56 ` Mathieu Desnoyers
1 sibling, 0 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2023-03-30 19:56 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-kernel, Shakeel Butt, Marek Szyprowski, linux-mm, stable
On 2023-03-30 15:42, Andrew Morton wrote:
> On Thu, 30 Mar 2023 09:38:22 -0400 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:
>
>> 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.
>>
>> ...
>>
>> --- a/kernel/fork.c
>> +++ b/kernel/fork.c
>> @@ -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
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-03-30 19:56 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30 13:38 [PATCH 1/1] mm: Fix memory leak on mm_init error handling Mathieu Desnoyers
2023-03-30 16:52 ` Shakeel Butt
2023-03-30 19:42 ` Andrew Morton
2023-03-30 19:55 ` Shakeel Butt
2023-03-30 19:56 ` Mathieu Desnoyers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).