* [PATCH] memcg: replace memcg ID idr with xarray
@ 2024-08-09 17:26 Shakeel Butt
2024-08-09 17:40 ` Roman Gushchin
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Shakeel Butt @ 2024-08-09 17:26 UTC (permalink / raw)
To: Andrew Morton
Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Muchun Song,
linux-mm, linux-kernel, Meta kernel team, cgroups,
Matthew Wilcox
At the moment memcg IDs are managed through IDR which requires external
synchronization mechanisms and makes the allocation code a bit awkward.
Let's switch to xarray and make the code simpler.
Suggested-by: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
mm/memcontrol.c | 34 +++++++---------------------------
1 file changed, 7 insertions(+), 27 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e1ffd2950393..b8e6b98485c6 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3363,29 +3363,12 @@ 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 DEFINE_XARRAY_ALLOC1(mem_cgroup_ids);
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);
-
+ xa_erase(&mem_cgroup_ids, memcg->id.id);
memcg->id.id = 0;
}
}
@@ -3420,7 +3403,7 @@ static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
{
WARN_ON_ONCE(!rcu_read_lock_held());
- return idr_find(&mem_cgroup_idr, id);
+ return xa_load(&mem_cgroup_ids, id);
}
#ifdef CONFIG_SHRINKER_DEBUG
@@ -3519,11 +3502,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
if (!memcg)
return ERR_PTR(error);
- memcg->id.id = mem_cgroup_alloc_id();
- if (memcg->id.id < 0) {
- error = memcg->id.id;
+ error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL,
+ XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL);
+ if (error)
goto fail;
- }
memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats),
GFP_KERNEL_ACCOUNT);
@@ -3664,9 +3646,7 @@ 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);
+ xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL);
return 0;
offline_kmem:
--
2.43.5
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] memcg: replace memcg ID idr with xarray
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
@ 2024-08-09 17:40 ` Roman Gushchin
2024-08-09 20:06 ` Matthew Wilcox
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Roman Gushchin @ 2024-08-09 17:40 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Muchun Song,
linux-mm, linux-kernel, Meta kernel team, cgroups,
Matthew Wilcox
On Fri, Aug 09, 2024 at 10:26:18AM -0700, Shakeel Butt wrote:
> At the moment memcg IDs are managed through IDR which requires external
> synchronization mechanisms and makes the allocation code a bit awkward.
> Let's switch to xarray and make the code simpler.
>
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Sweet!
Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] memcg: replace memcg ID idr with xarray
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
2024-08-09 17:40 ` Roman Gushchin
@ 2024-08-09 20:06 ` Matthew Wilcox
2024-08-09 20:23 ` Johannes Weiner
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2024-08-09 20:06 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
Muchun Song, linux-mm, linux-kernel, Meta kernel team, cgroups
On Fri, Aug 09, 2024 at 10:26:18AM -0700, Shakeel Butt wrote:
> At the moment memcg IDs are managed through IDR which requires external
> synchronization mechanisms and makes the allocation code a bit awkward.
> Let's switch to xarray and make the code simpler.
>
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Looks great.
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] memcg: replace memcg ID idr with xarray
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
2024-08-09 17:40 ` Roman Gushchin
2024-08-09 20:06 ` Matthew Wilcox
@ 2024-08-09 20:23 ` Johannes Weiner
2024-08-10 2:38 ` Muchun Song
2024-08-12 8:07 ` Michal Hocko
4 siblings, 0 replies; 6+ messages in thread
From: Johannes Weiner @ 2024-08-09 20:23 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Michal Hocko, Roman Gushchin, Muchun Song,
linux-mm, linux-kernel, Meta kernel team, cgroups,
Matthew Wilcox
On Fri, Aug 09, 2024 at 10:26:18AM -0700, Shakeel Butt wrote:
> At the moment memcg IDs are managed through IDR which requires external
> synchronization mechanisms and makes the allocation code a bit awkward.
> Let's switch to xarray and make the code simpler.
>
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Nice.
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] memcg: replace memcg ID idr with xarray
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
` (2 preceding siblings ...)
2024-08-09 20:23 ` Johannes Weiner
@ 2024-08-10 2:38 ` Muchun Song
2024-08-12 8:07 ` Michal Hocko
4 siblings, 0 replies; 6+ messages in thread
From: Muchun Song @ 2024-08-10 2:38 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
Linux Memory Management List, Linux Kernel Mailing List,
Meta kernel team, cgroups, Matthew Wilcox
> On Aug 10, 2024, at 01:26, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>
> At the moment memcg IDs are managed through IDR which requires external
> synchronization mechanisms and makes the allocation code a bit awkward.
> Let's switch to xarray and make the code simpler.
>
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Muchun Song <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] memcg: replace memcg ID idr with xarray
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
` (3 preceding siblings ...)
2024-08-10 2:38 ` Muchun Song
@ 2024-08-12 8:07 ` Michal Hocko
4 siblings, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2024-08-12 8:07 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Johannes Weiner, Roman Gushchin, Muchun Song,
linux-mm, linux-kernel, Meta kernel team, cgroups,
Matthew Wilcox
On Fri 09-08-24 10:26:18, Shakeel Butt wrote:
> At the moment memcg IDs are managed through IDR which requires external
> synchronization mechanisms and makes the allocation code a bit awkward.
> Let's switch to xarray and make the code simpler.
>
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Neat! I was not aware of this feature of XArray. Is there any actual
reason for idr to have its own implementation with XArray offering a
better interface?
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> mm/memcontrol.c | 34 +++++++---------------------------
> 1 file changed, 7 insertions(+), 27 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index e1ffd2950393..b8e6b98485c6 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3363,29 +3363,12 @@ 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 DEFINE_XARRAY_ALLOC1(mem_cgroup_ids);
>
> 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);
> -
> + xa_erase(&mem_cgroup_ids, memcg->id.id);
> memcg->id.id = 0;
> }
> }
> @@ -3420,7 +3403,7 @@ static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
> struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
> {
> WARN_ON_ONCE(!rcu_read_lock_held());
> - return idr_find(&mem_cgroup_idr, id);
> + return xa_load(&mem_cgroup_ids, id);
> }
>
> #ifdef CONFIG_SHRINKER_DEBUG
> @@ -3519,11 +3502,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
> if (!memcg)
> return ERR_PTR(error);
>
> - memcg->id.id = mem_cgroup_alloc_id();
> - if (memcg->id.id < 0) {
> - error = memcg->id.id;
> + error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL,
> + XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL);
> + if (error)
> goto fail;
> - }
>
> memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats),
> GFP_KERNEL_ACCOUNT);
> @@ -3664,9 +3646,7 @@ 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);
> + xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL);
>
> return 0;
> offline_kmem:
> --
> 2.43.5
>
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-08-12 8:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-09 17:26 [PATCH] memcg: replace memcg ID idr with xarray Shakeel Butt
2024-08-09 17:40 ` Roman Gushchin
2024-08-09 20:06 ` Matthew Wilcox
2024-08-09 20:23 ` Johannes Weiner
2024-08-10 2:38 ` Muchun Song
2024-08-12 8:07 ` Michal Hocko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox