* FAILED: patch "[PATCH] memcg: protect concurrent access to mem_cgroup_idr" failed to apply to 5.10-stable tree
@ 2024-08-12 11:43 gregkh
2024-09-06 15:41 ` [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr Tomas Krcka
2024-09-09 14:55 ` [PATCH 5.10.y v2] " Tomas Krcka
0 siblings, 2 replies; 9+ messages in thread
From: gregkh @ 2024-08-12 11:43 UTC (permalink / raw)
To: shakeel.butt, akpm, hannes, mhocko, muchun.song, roman.gushchin,
stable
Cc: stable
The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm
tree, then please email the backport, including the original git commit
id to <stable@vger.kernel.org>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 9972605a238339b85bd16b084eed5f18414d22db
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<stable@vger.kernel.org>' --in-reply-to '2024081218-demote-shakily-f31c@gregkh' --subject-prefix 'PATCH 5.10.y' HEAD^..
Possible dependencies:
9972605a2383 ("memcg: protect concurrent access to mem_cgroup_idr")
6f0df8e16eb5 ("memcontrol: ensure memcg acquired by id is properly set up")
e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists")
7348cc91821b ("mm: multi-gen LRU: remove aging fairness safeguard")
a579086c99ed ("mm: multi-gen LRU: remove eviction fairness safeguard")
adb8213014b2 ("mm: memcg: fix stale protection of reclaim target memcg")
57e9cc50f4dd ("mm: vmscan: split khugepaged stats from direct reclaim stats")
e4fea72b1438 ("mglru: mm/vmscan.c: fix imprecise comments")
d396def5d86d ("memcg: rearrange code")
410f8e82689e ("memcg: extract memcg_vmstats from struct mem_cgroup")
d6c3af7d8a2b ("mm: multi-gen LRU: debugfs interface")
1332a809d95a ("mm: multi-gen LRU: thrashing prevention")
354ed5974429 ("mm: multi-gen LRU: kill switch")
f76c83378851 ("mm: multi-gen LRU: optimize multiple memcgs")
bd74fdaea146 ("mm: multi-gen LRU: support page table walks")
018ee47f1489 ("mm: multi-gen LRU: exploit locality in rmap")
ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
ec1c86b25f4b ("mm: multi-gen LRU: groundwork")
f1e1a7be4718 ("mm/vmscan.c: refactor shrink_node()")
d3629af59f41 ("mm/vmscan: make the annotations of refaults code at the right place")
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 9972605a238339b85bd16b084eed5f18414d22db Mon Sep 17 00:00:00 2001
From: Shakeel Butt <shakeel.butt@linux.dev>
Date: Fri, 2 Aug 2024 16:58:22 -0700
Subject: [PATCH] memcg: protect concurrent access to mem_cgroup_idr
Commit 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after
many small jobs") decoupled the memcg IDs from the CSS ID space to fix the
cgroup creation failures. It introduced IDR to maintain the memcg ID
space. The IDR depends on external synchronization mechanisms for
modifications. For the mem_cgroup_idr, the idr_alloc() and idr_replace()
happen within css callback and thus are protected through cgroup_mutex
from concurrent modifications. However idr_remove() for mem_cgroup_idr
was not protected against concurrency and can be run concurrently for
different memcgs when they hit their refcnt to zero. Fix that.
We have been seeing list_lru based kernel crashes at a low frequency in
our fleet for a long time. These crashes were in different part of
list_lru code including list_lru_add(), list_lru_del() and reparenting
code. Upon further inspection, it looked like for a given object (dentry
and inode), the super_block's list_lru didn't have list_lru_one for the
memcg of that object. The initial suspicions were either the object is
not allocated through kmem_cache_alloc_lru() or somehow
memcg_list_lru_alloc() failed to allocate list_lru_one() for a memcg but
returned success. No evidence were found for these cases.
Looking more deeply, we started seeing situations where valid memcg's id
is not present in mem_cgroup_idr and in some cases multiple valid memcgs
have same id and mem_cgroup_idr is pointing to one of them. So, the most
reasonable explanation is that these situations can happen due to race
between multiple idr_remove() calls or race between
idr_alloc()/idr_replace() and idr_remove(). These races are causing
multiple memcgs to acquire the same ID and then offlining of one of them
would cleanup list_lrus on the system for all of them. Later access from
other memcgs to the list_lru cause crashes due to missing list_lru_one.
Link: https://lkml.kernel.org/r/20240802235822.1830976-1-shakeel.butt@linux.dev
Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 960371788687..f29157288b7d 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3386,11 +3386,28 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
#define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)
static DEFINE_IDR(mem_cgroup_idr);
+static DEFINE_SPINLOCK(memcg_idr_lock);
+
+static int mem_cgroup_alloc_id(void)
+{
+ int ret;
+
+ idr_preload(GFP_KERNEL);
+ spin_lock(&memcg_idr_lock);
+ ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
+ GFP_NOWAIT);
+ spin_unlock(&memcg_idr_lock);
+ idr_preload_end();
+ return ret;
+}
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
{
if (memcg->id.id > 0) {
+ spin_lock(&memcg_idr_lock);
idr_remove(&mem_cgroup_idr, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
+
memcg->id.id = 0;
}
}
@@ -3524,8 +3541,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
if (!memcg)
return ERR_PTR(error);
- memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
- 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
+ memcg->id.id = mem_cgroup_alloc_id();
if (memcg->id.id < 0) {
error = memcg->id.id;
goto fail;
@@ -3667,7 +3683,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
* publish it here at the end of onlining. This matches the
* regular ID destruction during offlining.
*/
+ spin_lock(&memcg_idr_lock);
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
return 0;
offline_kmem:
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-08-12 11:43 FAILED: patch "[PATCH] memcg: protect concurrent access to mem_cgroup_idr" failed to apply to 5.10-stable tree gregkh
@ 2024-09-06 15:41 ` Tomas Krcka
2024-09-08 13:02 ` Greg KH
2024-09-09 14:55 ` [PATCH 5.10.y v2] " Tomas Krcka
1 sibling, 1 reply; 9+ messages in thread
From: Tomas Krcka @ 2024-09-06 15:41 UTC (permalink / raw)
To: stable
Cc: Shakeel Butt, Muchun Song, Roman Gushchin, Johannes Weiner,
Michal Hocko, Andrew Morton, Tomas Krcka
From: Shakeel Butt <shakeel.butt@linux.dev>
commit 9972605a238339b85bd16b084eed5f18414d22db upstream.
Commit 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after
many small jobs") decoupled the memcg IDs from the CSS ID space to fix the
cgroup creation failures. It introduced IDR to maintain the memcg ID
space. The IDR depends on external synchronization mechanisms for
modifications. For the mem_cgroup_idr, the idr_alloc() and idr_replace()
happen within css callback and thus are protected through cgroup_mutex
from concurrent modifications. However idr_remove() for mem_cgroup_idr
was not protected against concurrency and can be run concurrently for
different memcgs when they hit their refcnt to zero. Fix that.
We have been seeing list_lru based kernel crashes at a low frequency in
our fleet for a long time. These crashes were in different part of
list_lru code including list_lru_add(), list_lru_del() and reparenting
code. Upon further inspection, it looked like for a given object (dentry
and inode), the super_block's list_lru didn't have list_lru_one for the
memcg of that object. The initial suspicions were either the object is
not allocated through kmem_cache_alloc_lru() or somehow
memcg_list_lru_alloc() failed to allocate list_lru_one() for a memcg but
returned success. No evidence were found for these cases.
Looking more deeply, we started seeing situations where valid memcg's id
is not present in mem_cgroup_idr and in some cases multiple valid memcgs
have same id and mem_cgroup_idr is pointing to one of them. So, the most
reasonable explanation is that these situations can happen due to race
between multiple idr_remove() calls or race between
idr_alloc()/idr_replace() and idr_remove(). These races are causing
multiple memcgs to acquire the same ID and then offlining of one of them
would cleanup list_lrus on the system for all of them. Later access from
other memcgs to the list_lru cause crashes due to missing list_lru_one.
Link: https://lkml.kernel.org/r/20240802235822.1830976-1-shakeel.butt@linux.dev
Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
[Conflict due to
be740503ed03 ("mm: memcontrol: fix cannot alloc the maximum memcg ID")
6f0df8e16eb5 ("memcontrol: ensure memcg acquired by id is properly set up")
both are not in the tree]
Signed-off-by: Tomas Krcka <krckatom@amazon.de>
---
mm/memcontrol.c | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 874f91715296..8de7c72ae025 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -5160,11 +5160,28 @@ static struct cftype mem_cgroup_legacy_files[] = {
*/
static DEFINE_IDR(mem_cgroup_idr);
+static DEFINE_SPINLOCK(memcg_idr_lock);
+
+static int mem_cgroup_alloc_id(void)
+{
+ int ret;
+
+ idr_preload(GFP_KERNEL);
+ spin_lock(&memcg_idr_lock);
+ ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
+ GFP_NOWAIT);
+ spin_unlock(&memcg_idr_lock);
+ idr_preload_end();
+ return ret;
+}
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
{
if (memcg->id.id > 0) {
+ spin_lock(&memcg_idr_lock);
idr_remove(&mem_cgroup_idr, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
+
memcg->id.id = 0;
}
}
@@ -5294,9 +5311,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
if (!memcg)
return ERR_PTR(error);
- memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
- 1, MEM_CGROUP_ID_MAX,
- GFP_KERNEL);
+ memcg->id.id = mem_cgroup_alloc_id();
if (memcg->id.id < 0) {
error = memcg->id.id;
goto fail;
@@ -5342,7 +5357,9 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
memcg->deferred_split_queue.split_queue_len = 0;
#endif
+ spin_lock(&memcg_idr_lock);
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
return memcg;
fail:
mem_cgroup_id_remove(memcg);
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-09-06 15:41 ` [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr Tomas Krcka
@ 2024-09-08 13:02 ` Greg KH
2024-09-09 13:43 ` Krcka, Tomas
0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2024-09-08 13:02 UTC (permalink / raw)
To: Tomas Krcka
Cc: stable, Shakeel Butt, Muchun Song, Roman Gushchin,
Johannes Weiner, Michal Hocko, Andrew Morton, Tomas Krcka
On Fri, Sep 06, 2024 at 03:41:40PM +0000, Tomas Krcka wrote:
> From: Shakeel Butt <shakeel.butt@linux.dev>
>
> commit 9972605a238339b85bd16b084eed5f18414d22db upstream.
To quote the documentation:
When using option 2 or 3 you can ask for your change to be included in specific
stable series. When doing so, ensure the fix or an equivalent is applicable,
submitted, or already present in all newer stable trees still supported. This is
meant to prevent regressions that users might later encounter on updating, if
e.g. a fix merged for 5.19-rc1 would be backported to 5.10.y, but not to 5.15.y.
I've dropped this from the review queue and will wait for all of the
needed versions to be submitted.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-09-08 13:02 ` Greg KH
@ 2024-09-09 13:43 ` Krcka, Tomas
2024-09-09 14:04 ` Greg KH
0 siblings, 1 reply; 9+ messages in thread
From: Krcka, Tomas @ 2024-09-09 13:43 UTC (permalink / raw)
To: Greg KH
Cc: Tomas Krcka, stable@vger.kernel.org, Shakeel Butt, Muchun Song,
Roman Gushchin, Johannes Weiner, Michal Hocko, Andrew Morton
Hi Greg,
Got it, thanks.
Submitted
v6.1 - https://lore.kernel.org/stable/20240909134012.11944-1-krckatom@amazon.de/
v5.15 - https://lore.kernel.org/stable/20240909134046.12713-1-krckatom@amazon.de/
Tomas
> On 8. Sep 2024, at 15:02, Greg KH <gregkh@linuxfoundation.org> wrote:
>
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
>
>
>
> On Fri, Sep 06, 2024 at 03:41:40PM +0000, Tomas Krcka wrote:
>> From: Shakeel Butt <shakeel.butt@linux.dev>
>>
>> commit 9972605a238339b85bd16b084eed5f18414d22db upstream.
>
> To quote the documentation:
>
> When using option 2 or 3 you can ask for your change to be included in specific
> stable series. When doing so, ensure the fix or an equivalent is applicable,
> submitted, or already present in all newer stable trees still supported. This is
> meant to prevent regressions that users might later encounter on updating, if
> e.g. a fix merged for 5.19-rc1 would be backported to 5.10.y, but not to 5.15.y.
>
> I've dropped this from the review queue and will wait for all of the
> needed versions to be submitted.
>
> thanks,
>
> greg k-h
Amazon Web Services Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-09-09 13:43 ` Krcka, Tomas
@ 2024-09-09 14:04 ` Greg KH
2024-09-09 14:10 ` Krcka, Tomas
0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2024-09-09 14:04 UTC (permalink / raw)
To: Krcka, Tomas
Cc: Tomas Krcka, stable@vger.kernel.org, Shakeel Butt, Muchun Song,
Roman Gushchin, Johannes Weiner, Michal Hocko, Andrew Morton
On Mon, Sep 09, 2024 at 01:43:25PM +0000, Krcka, Tomas wrote:
> Hi Greg,
> Got it, thanks.
Please do not top-post.
> Submitted
> v6.1 - https://lore.kernel.org/stable/20240909134012.11944-1-krckatom@amazon.de/
> v5.15 - https://lore.kernel.org/stable/20240909134046.12713-1-krckatom@amazon.de/
No 5.10?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-09-09 14:04 ` Greg KH
@ 2024-09-09 14:10 ` Krcka, Tomas
2024-09-09 14:45 ` Greg KH
0 siblings, 1 reply; 9+ messages in thread
From: Krcka, Tomas @ 2024-09-09 14:10 UTC (permalink / raw)
To: Greg KH
Cc: Tomas Krcka, stable@vger.kernel.org, Shakeel Butt, Muchun Song,
Roman Gushchin, Johannes Weiner, Michal Hocko, Andrew Morton
> On 9. Sep 2024, at 16:04, Greg KH <gregkh@linuxfoundation.org> wrote:
>
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
>
>
>
> On Mon, Sep 09, 2024 at 01:43:25PM +0000, Krcka, Tomas wrote:
>> Hi Greg,
>> Got it, thanks.
>
> Please do not top-post.
>
>> Submitted
>> v6.1 - https://lore.kernel.org/stable/20240909134012.11944-1-krckatom@amazon.de/
>> v5.15 - https://lore.kernel.org/stable/20240909134046.12713-1-krckatom@amazon.de/
>
> No 5.10?
>
5.10 - is the original one https://lore.kernel.org/stable/20240906154140.70821-1-krckatom@amazon.de/
Or shall I re-post it ?
Amazon Web Services Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr
2024-09-09 14:10 ` Krcka, Tomas
@ 2024-09-09 14:45 ` Greg KH
0 siblings, 0 replies; 9+ messages in thread
From: Greg KH @ 2024-09-09 14:45 UTC (permalink / raw)
To: Krcka, Tomas
Cc: Tomas Krcka, stable@vger.kernel.org, Shakeel Butt, Muchun Song,
Roman Gushchin, Johannes Weiner, Michal Hocko, Andrew Morton
On Mon, Sep 09, 2024 at 02:10:24PM +0000, Krcka, Tomas wrote:
>
>
> > On 9. Sep 2024, at 16:04, Greg KH <gregkh@linuxfoundation.org> wrote:
> >
> > CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
> >
> >
> >
> > On Mon, Sep 09, 2024 at 01:43:25PM +0000, Krcka, Tomas wrote:
> >> Hi Greg,
> >> Got it, thanks.
> >
> > Please do not top-post.
> >
> >> Submitted
> >> v6.1 - https://lore.kernel.org/stable/20240909134012.11944-1-krckatom@amazon.de/
> >> v5.15 - https://lore.kernel.org/stable/20240909134046.12713-1-krckatom@amazon.de/
> >
> > No 5.10?
> >
>
> 5.10 - is the original one https://lore.kernel.org/stable/20240906154140.70821-1-krckatom@amazon.de/
>
> Or shall I re-post it ?
That's what my original response asked for, right? :)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 5.10.y v2] memcg: protect concurrent access to mem_cgroup_idr
2024-08-12 11:43 FAILED: patch "[PATCH] memcg: protect concurrent access to mem_cgroup_idr" failed to apply to 5.10-stable tree gregkh
2024-09-06 15:41 ` [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr Tomas Krcka
@ 2024-09-09 14:55 ` Tomas Krcka
2024-09-10 7:56 ` Greg KH
1 sibling, 1 reply; 9+ messages in thread
From: Tomas Krcka @ 2024-09-09 14:55 UTC (permalink / raw)
To: stable
Cc: Shakeel Butt, Muchun Song, Roman Gushchin, Johannes Weiner,
Michal Hocko, Andrew Morton, Tomas Krcka
From: Shakeel Butt <shakeel.butt@linux.dev>
commit 9972605a238339b85bd16b084eed5f18414d22db upstream.
Commit 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after
many small jobs") decoupled the memcg IDs from the CSS ID space to fix the
cgroup creation failures. It introduced IDR to maintain the memcg ID
space. The IDR depends on external synchronization mechanisms for
modifications. For the mem_cgroup_idr, the idr_alloc() and idr_replace()
happen within css callback and thus are protected through cgroup_mutex
from concurrent modifications. However idr_remove() for mem_cgroup_idr
was not protected against concurrency and can be run concurrently for
different memcgs when they hit their refcnt to zero. Fix that.
We have been seeing list_lru based kernel crashes at a low frequency in
our fleet for a long time. These crashes were in different part of
list_lru code including list_lru_add(), list_lru_del() and reparenting
code. Upon further inspection, it looked like for a given object (dentry
and inode), the super_block's list_lru didn't have list_lru_one for the
memcg of that object. The initial suspicions were either the object is
not allocated through kmem_cache_alloc_lru() or somehow
memcg_list_lru_alloc() failed to allocate list_lru_one() for a memcg but
returned success. No evidence were found for these cases.
Looking more deeply, we started seeing situations where valid memcg's id
is not present in mem_cgroup_idr and in some cases multiple valid memcgs
have same id and mem_cgroup_idr is pointing to one of them. So, the most
reasonable explanation is that these situations can happen due to race
between multiple idr_remove() calls or race between
idr_alloc()/idr_replace() and idr_remove(). These races are causing
multiple memcgs to acquire the same ID and then offlining of one of them
would cleanup list_lrus on the system for all of them. Later access from
other memcgs to the list_lru cause crashes due to missing list_lru_one.
Link: https://lkml.kernel.org/r/20240802235822.1830976-1-shakeel.butt@linux.dev
Fixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
[Adapted due to commits
be740503ed03 ("mm: memcontrol: fix cannot alloc the maximum memcg ID")
6f0df8e16eb5 ("memcontrol: ensure memcg acquired by id is properly set up")
not in the tree]
Signed-off-by: Tomas Krcka <krckatom@amazon.de>
---
mm/memcontrol.c | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 874f91715296..8de7c72ae025 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -5160,11 +5160,28 @@ static struct cftype mem_cgroup_legacy_files[] = {
*/
static DEFINE_IDR(mem_cgroup_idr);
+static DEFINE_SPINLOCK(memcg_idr_lock);
+
+static int mem_cgroup_alloc_id(void)
+{
+ int ret;
+
+ idr_preload(GFP_KERNEL);
+ spin_lock(&memcg_idr_lock);
+ ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
+ GFP_NOWAIT);
+ spin_unlock(&memcg_idr_lock);
+ idr_preload_end();
+ return ret;
+}
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
{
if (memcg->id.id > 0) {
+ spin_lock(&memcg_idr_lock);
idr_remove(&mem_cgroup_idr, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
+
memcg->id.id = 0;
}
}
@@ -5294,9 +5311,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
if (!memcg)
return ERR_PTR(error);
- memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
- 1, MEM_CGROUP_ID_MAX,
- GFP_KERNEL);
+ memcg->id.id = mem_cgroup_alloc_id();
if (memcg->id.id < 0) {
error = memcg->id.id;
goto fail;
@@ -5342,7 +5357,9 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
memcg->deferred_split_queue.split_queue_len = 0;
#endif
+ spin_lock(&memcg_idr_lock);
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
+ spin_unlock(&memcg_idr_lock);
return memcg;
fail:
mem_cgroup_id_remove(memcg);
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 5.10.y v2] memcg: protect concurrent access to mem_cgroup_idr
2024-09-09 14:55 ` [PATCH 5.10.y v2] " Tomas Krcka
@ 2024-09-10 7:56 ` Greg KH
0 siblings, 0 replies; 9+ messages in thread
From: Greg KH @ 2024-09-10 7:56 UTC (permalink / raw)
To: Tomas Krcka
Cc: stable, Shakeel Butt, Muchun Song, Roman Gushchin,
Johannes Weiner, Michal Hocko, Andrew Morton, Tomas Krcka
On Mon, Sep 09, 2024 at 02:55:57PM +0000, Tomas Krcka wrote:
> From: Shakeel Butt <shakeel.butt@linux.dev>
>
> commit 9972605a238339b85bd16b084eed5f18414d22db upstream.
>
Thanks, all now queued up.
greg k-h
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-09-10 7:56 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-12 11:43 FAILED: patch "[PATCH] memcg: protect concurrent access to mem_cgroup_idr" failed to apply to 5.10-stable tree gregkh
2024-09-06 15:41 ` [PATCH 5.10.y] memcg: protect concurrent access to mem_cgroup_idr Tomas Krcka
2024-09-08 13:02 ` Greg KH
2024-09-09 13:43 ` Krcka, Tomas
2024-09-09 14:04 ` Greg KH
2024-09-09 14:10 ` Krcka, Tomas
2024-09-09 14:45 ` Greg KH
2024-09-09 14:55 ` [PATCH 5.10.y v2] " Tomas Krcka
2024-09-10 7:56 ` Greg KH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox