Skip to contentSkip to conversation

percpu: memcg memory accounting rework

PATCH Applied Roman Gushchin 3 participants 7 messages Jun 3, 2021 – Jun 5, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover percpu: memcg memory accounting rework

This patchset reworks memcg-based accounting of the percpu memory:
instead of using two types of chunks (memcg-aware and non-memcg-aware) it treats all chunks as memcg-aware. An extra memory used for objcg arrays will is compensated by a better chunks utilization, so the total memory footprint should be the same or smaller.

First two patches are preparational changes and cleanups on the memcg side. The third one is percpu accounting rework.

v2:
- make all chunks memcg-aware instead of on-demand objcg allocation, by Dennis

v1:
https://lkml.org/lkml/2021/5/11/1343

Roman Gushchin (3):
mm, memcg: mark cgroup_memory_nosocket, nokmem and noswap as

    __ro_after_init

mm, memcg: introduce mem_cgroup_kmem_disabled()
percpu: rework memcg accounting

 include/linux/memcontrol.h |   5 ++
 mm/memcontrol.c            |  11 ++-
 mm/percpu-internal.h       |  52 +--------------
 mm/percpu-km.c             |   5 +-
 mm/percpu-stats.c          |  46 +++++--------
 mm/percpu-vm.c             |  11 ++-
 mm/percpu.c                | 133 +++++++++++++++----------------------
 7 files changed, 88 insertions(+), 175 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/3 mm, memcg: mark cgroup_memory_nosocket, nokmem and noswap as __ro_after_init

cgroup_memory_nosocket, cgroup_memory_nokmem and cgroup_memory_noswap are initialized during the kernel initialization and never change their value afterwards.

cgroup_memory_nosocket, cgroup_memory_nokmem are written only from cgroup_memory(), which is marked as __init.

cgroup_memory_noswap is written from setup_swap_account() and mem_cgroup_swap_init(), both are marked as __init.

Mark all three variables as __ro_after_init.

---
 mm/memcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/memcontrol.c +3 -3
index e064ac0d850a..e6203ee24a11 100644
@@ -80,14 +80,14 @@ struct mem_cgroup *root_mem_cgroup __read_mostly;
 DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
 
 /* Socket memory accounting disabled? */
-static bool cgroup_memory_nosocket;
+static bool cgroup_memory_nosocket __ro_after_init;
 
 /* Kernel memory accounting disabled? */
-static bool cgroup_memory_nokmem;
+static bool cgroup_memory_nokmem __ro_after_init;
 
 /* Whether the swap controller is active */
 #ifdef CONFIG_MEMCG_SWAP
-bool cgroup_memory_noswap __read_mostly;
+bool cgroup_memory_noswap __ro_after_init;
 #else
 #define cgroup_memory_noswap		1
 #endif
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 2/3 mm, memcg: introduce mem_cgroup_kmem_disabled()

In reply to Roman Gushchin

Introduce a new mem_cgroup_kmem_disabled() helper, similar to mem_cgroup_disabled(), to check whether the kernel memory accounting is off. A user could disable it using a boot option to eliminate some associated costs.

The helper can be used outside of memcontrol.c to dynamically disable the kmem-related code. The returned value is stable after the kernel initialization is finished.

---
 include/linux/memcontrol.h | 5 +++++
 mm/memcontrol.c            | 5 +++++
 2 files changed, 10 insertions(+)
include/linux/memcontrol.h +5 -0
index 0c04d39a7967..8ef51c58f470 100644
@@ -1583,6 +1583,7 @@ static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
 #endif
 
 #ifdef CONFIG_MEMCG_KMEM
+bool mem_cgroup_kmem_disabled(void);
 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
 void __memcg_kmem_uncharge_page(struct page *page, int order);
 
@@ -1636,6 +1637,10 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg)
 struct mem_cgroup *mem_cgroup_from_obj(void *p);
 
 #else
+static inline bool mem_cgroup_kmem_disabled(void)
+{
+	return true;
+}
 
 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
 					 int order)
mm/memcontrol.c +5 -0
index e6203ee24a11..1fa9b00ec71d 100644
@@ -255,6 +255,11 @@ struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
 #ifdef CONFIG_MEMCG_KMEM
 extern spinlock_t css_set_lock;
 
+bool mem_cgroup_kmem_disabled(void)
+{
+	return cgroup_memory_nokmem;
+}
+
 static int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
 			       unsigned int nr_pages);
 static void __memcg_kmem_uncharge(struct mem_cgroup *memcg,
Signature · 1 line
2.31.1
#3 lore raw

PATCH v2 3/3 percpu: rework memcg accounting

In reply to Roman Gushchin

The current implementation of the memcg accounting of the percpu memory is based on the idea of having two separate sets of chunks for accounted and non-accounted memory. This approach has an advantage of not wasting any extra memory for memcg data for non-accounted chunks, however it complicates the code and leads to a higher chunks number due to a lower chunk utilization.

Instead of having two chunk types it's possible to declare all* chunks memcg-aware unless the kernel memory accounting is disabled globally by a boot option. The size of objcg_array is usually small in comparison to chunks themselves (it obviously depends on the number of CPUs), so even if some chunk will have no accounted allocations, the memory waste isn't significant and will likely be compensated by a higher chunk utilization. Also, with time more and more percpu allocations will likely become accounted.

* The first chunk is initialized before the memory cgroup subsystem, so we don't know for sure whether we need to allocate obj_cgroups. Because it's small, let's make it free for use. Then we don't need to allocate obj_cgroups for it.

---
 mm/percpu-internal.h |  52 +----------------
 mm/percpu-km.c       |   5 +-
 mm/percpu-stats.c    |  46 +++++----------
 mm/percpu-vm.c       |  11 ++--
 mm/percpu.c          | 133 +++++++++++++++++--------------------------
 5 files changed, 75 insertions(+), 172 deletions(-)
mm/percpu-internal.h +1 -51
index 10604dce806f..b6dc22904088 100644
@@ -5,25 +5,6 @@
 #include <linux/types.h>
 #include <linux/percpu.h>
 
-/*
- * There are two chunk types: root and memcg-aware.
- * Chunks of each type have separate slots list.
- *
- * Memcg-aware chunks have an attached vector of obj_cgroup pointers, which is
- * used to store memcg membership data of a percpu object.  Obj_cgroups are
- * ref-counted pointers to a memory cgroup with an ability to switch dynamically
- * to the parent memory cgroup.  This allows to reclaim a deleted memory cgroup
- * without reclaiming of all outstanding objects, which hold a reference at it.
- */
-enum pcpu_chunk_type {
-	PCPU_CHUNK_ROOT,
-#ifdef CONFIG_MEMCG_KMEM
-	PCPU_CHUNK_MEMCG,
-#endif
-	PCPU_NR_CHUNK_TYPES,
-	PCPU_FAIL_ALLOC = PCPU_NR_CHUNK_TYPES
-};
-
 /*
  * pcpu_block_md is the metadata block struct.
  * Each chunk's bitmap is split into a number of full blocks.
@@ -91,7 +72,7 @@ extern struct list_head *pcpu_chunk_lists;
 extern int pcpu_nr_slots;
 extern int pcpu_sidelined_slot;
 extern int pcpu_to_depopulate_slot;
-extern int pcpu_nr_empty_pop_pages[];
+extern int pcpu_nr_empty_pop_pages;
 
 extern struct pcpu_chunk *pcpu_first_chunk;
 extern struct pcpu_chunk *pcpu_reserved_chunk;
@@ -132,37 +113,6 @@ static inline int pcpu_chunk_map_bits(struct pcpu_chunk *chunk)
 	return pcpu_nr_pages_to_map_bits(chunk->nr_pages);
 }
 
-#ifdef CONFIG_MEMCG_KMEM
-static inline enum pcpu_chunk_type pcpu_chunk_type(struct pcpu_chunk *chunk)
-{
-	if (chunk->obj_cgroups)
-		return PCPU_CHUNK_MEMCG;
-	return PCPU_CHUNK_ROOT;
-}
-
-static inline bool pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)
-{
-	return chunk_type == PCPU_CHUNK_MEMCG;
-}
-
-#else
-static inline enum pcpu_chunk_type pcpu_chunk_type(struct pcpu_chunk *chunk)
-{
-	return PCPU_CHUNK_ROOT;
-}
-
-static inline bool pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)
-{
-	return false;
-}
-#endif
-
-static inline struct list_head *pcpu_chunk_list(enum pcpu_chunk_type chunk_type)
-{
-	return &pcpu_chunk_lists[pcpu_nr_slots *
-				 pcpu_is_memcg_chunk(chunk_type)];
-}
-
 #ifdef CONFIG_PERCPU_STATS
 
 #include <linux/spinlock.h>
mm/percpu-km.c +2 -3
index c84a9f781a6c..c9d529dc7651 100644
@@ -44,8 +44,7 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
 	/* nada */
 }
 
-static struct pcpu_chunk *pcpu_create_chunk(enum pcpu_chunk_type type,
-					    gfp_t gfp)
+static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)
 {
 	const int nr_pages = pcpu_group_sizes[0] >> PAGE_SHIFT;
 	struct pcpu_chunk *chunk;
@@ -53,7 +52,7 @@ static struct pcpu_chunk *pcpu_create_chunk(enum pcpu_chunk_type type,
 	unsigned long flags;
 	int i;
 
-	chunk = pcpu_alloc_chunk(type, gfp);
+	chunk = pcpu_alloc_chunk(gfp);
 	if (!chunk)
 		return NULL;
 
mm/percpu-stats.c +15 -31

84 lines not on this page · raw message

mm/percpu-vm.c +5 -6
index c75f6f24f2d5..e59566b949d1 100644
@@ -328,13 +328,12 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
 	pcpu_free_pages(chunk, pages, page_start, page_end);
 }
 
-static struct pcpu_chunk *pcpu_create_chunk(enum pcpu_chunk_type type,
-					    gfp_t gfp)
+static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)
 {
 	struct pcpu_chunk *chunk;
 	struct vm_struct **vms;
 
-	chunk = pcpu_alloc_chunk(type, gfp);
+	chunk = pcpu_alloc_chunk(gfp);
 	if (!chunk)
 		return NULL;
 
@@ -403,7 +402,7 @@ static bool pcpu_should_reclaim_chunk(struct pcpu_chunk *chunk)
 	 * chunk, move it to the to_depopulate list.
 	 */
 	return ((chunk->isolated && chunk->nr_empty_pop_pages) ||
-		(pcpu_nr_empty_pop_pages[pcpu_chunk_type(chunk)] >
-		 PCPU_EMPTY_POP_PAGES_HIGH + chunk->nr_empty_pop_pages &&
-		chunk->nr_empty_pop_pages >= chunk->nr_pages / 4));
+		(pcpu_nr_empty_pop_pages > PCPU_EMPTY_POP_PAGES_HIGH +
+		 chunk->nr_empty_pop_pages &&
+		 chunk->nr_empty_pop_pages >= chunk->nr_pages / 4));
 }
mm/percpu.c +52 -81

401 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#4 lore raw
In reply to Roman Gushchin

Hello,

Quoted 22 lines · Roman Gushchin

This patchset reworks memcg-based accounting of the percpu memory:
instead of using two types of chunks (memcg-aware and non-memcg-aware) it treats all chunks as memcg-aware. An extra memory used for objcg arrays will is compensated by a better chunks utilization, so the total memory footprint should be the same or smaller.

First two patches are preparational changes and cleanups on the memcg side. The third one is percpu accounting rework.

v2:
- make all chunks memcg-aware instead of on-demand objcg allocation, by Dennis

v1:
https://lkml.org/lkml/2021/5/11/1343

Roman Gushchin (3):
mm, memcg: mark cgroup_memory_nosocket, nokmem and noswap as

    __ro_after_init

mm, memcg: introduce mem_cgroup_kmem_disabled()
percpu: rework memcg accounting

Quoted 12 lines
 include/linux/memcontrol.h |   5 ++
 mm/memcontrol.c            |  11 ++-
 mm/percpu-internal.h       |  52 +--------------
 mm/percpu-km.c             |   5 +-
 mm/percpu-stats.c          |  46 +++++--------
 mm/percpu-vm.c             |  11 ++-
 mm/percpu.c                | 133 +++++++++++++++----------------------
 7 files changed, 88 insertions(+), 175 deletions(-)
Signature · 1 line
2.31.1

I think I'm pretty happy with this approach. If there is any issue with the delta of memcg on but kmem accounting off, we can tackle that separately. I'll need another day or so to review the last patch a little more carefully though.

Andrew do you mind if I route the first two in my tree too?

Thanks,
Dennis

maintainer
#5 lore raw
Quoted 6 lines · On Fri, 4 Jun 2021 16:10:46 +0000 Dennis Zhou <[email protected]> wrote:

I think I'm pretty happy with this approach. If there is any issue with the delta of memcg on but kmem accounting off, we can tackle that separately. I'll need another day or so to review the last patch a little more carefully though.

Andrew do you mind if I route the first two in my tree too?

Please do.

maintainer Applied
#6 lore raw
In reply to Roman Gushchin

Hello,

Quoted 22 lines · Roman Gushchin

This patchset reworks memcg-based accounting of the percpu memory:
instead of using two types of chunks (memcg-aware and non-memcg-aware) it treats all chunks as memcg-aware. An extra memory used for objcg arrays will is compensated by a better chunks utilization, so the total memory footprint should be the same or smaller.

First two patches are preparational changes and cleanups on the memcg side. The third one is percpu accounting rework.

v2:
- make all chunks memcg-aware instead of on-demand objcg allocation, by Dennis

v1:
https://lkml.org/lkml/2021/5/11/1343

Roman Gushchin (3):
mm, memcg: mark cgroup_memory_nosocket, nokmem and noswap as

    __ro_after_init

mm, memcg: introduce mem_cgroup_kmem_disabled()
percpu: rework memcg accounting

Quoted 12 lines
 include/linux/memcontrol.h |   5 ++
 mm/memcontrol.c            |  11 ++-
 mm/percpu-internal.h       |  52 +--------------
 mm/percpu-km.c             |   5 +-
 mm/percpu-stats.c          |  46 +++++--------
 mm/percpu-vm.c             |  11 ++-
 mm/percpu.c                | 133 +++++++++++++++----------------------
 7 files changed, 88 insertions(+), 175 deletions(-)
Signature · 1 line
2.31.1

Thanks for this! It greatly simplifies the chunk maintenance.

I've applied this to for-5.14.

There is a trivial merge conflict though due to my for-5.14 being based on torvalds#v5.12-rc7, but that's fine.

Thanks,
Dennis