Skip to contentSkip to conversation

mm/memcg: Miscellaneous cleanups and streamlining

PATCH Superseded Waiman Long 3 participants 12 messages Aug 20, 2020 – Sep 14, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mm/memcg: Miscellaneous cleanups and streamlining

Patch 1 removes an unused enum charge_type.

Patch 2 streamlines mem_cgroup_get_max().

Patch 3 unifies swap and memsw page counters in mem_cgroup.

Waiman Long (3):
mm/memcg: Clean up obsolete enum charge_type
mm/memcg: Simplify mem_cgroup_get_max()
mm/memcg: Unify swap and memsw page counters

 include/linux/memcontrol.h |  3 +--
 mm/memcontrol.c            | 30 ++++++++++--------------------
 2 files changed, 11 insertions(+), 22 deletions(-)
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/3 mm/memcg: Clean up obsolete enum charge_type

Since commit 0a31bc97c80c ("mm: memcontrol: rewrite uncharge API") and commit 00501b531c47 ("mm: memcontrol: rewrite charge API") in v3.17, the enum charge_type was no longer used anywhere. However, the enum itself was not removed at that time. Remove the obsolete enum charge_type now.

---
 mm/memcontrol.c | 8 --------
 1 file changed, 8 deletions(-)
mm/memcontrol.c +0 -8
index b807952b4d43..26b7a48d3afb 100644
@@ -197,14 +197,6 @@ static struct move_charge_struct {
 #define	MEM_CGROUP_MAX_RECLAIM_LOOPS		100
 #define	MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS	2
 
-enum charge_type {
-	MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
-	MEM_CGROUP_CHARGE_TYPE_ANON,
-	MEM_CGROUP_CHARGE_TYPE_SWAPOUT,	/* for accounting swapcache */
-	MEM_CGROUP_CHARGE_TYPE_DROP,	/* a page was unused swap cache */
-	NR_CHARGE_TYPE,
-};
-
 /* for encoding cft->private value on file */
 enum res_type {
 	_MEM,
Signature · 1 line
2.18.1
#4 lore raw

PATCH 2/3 mm/memcg: Simplify mem_cgroup_get_max()

In reply to Waiman Long

The mem_cgroup_get_max() function used to get memory+swap max from both the v1 memsw and v2 memory+swap page counters & return the maximum of these 2 values. This is redundant and it is more efficient to just get either the v1 or the v2 values depending on which one is currently in use.

---
 mm/memcontrol.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
mm/memcontrol.c +5 -9
index 26b7a48d3afb..d219dca5239f 100644
@@ -1633,17 +1633,13 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
  */
 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
 {
-	unsigned long max;
+	unsigned long max = READ_ONCE(memcg->memory.max);
 
-	max = READ_ONCE(memcg->memory.max);
 	if (mem_cgroup_swappiness(memcg)) {
-		unsigned long memsw_max;
-		unsigned long swap_max;
-
-		memsw_max = memcg->memsw.max;
-		swap_max = READ_ONCE(memcg->swap.max);
-		swap_max = min(swap_max, (unsigned long)total_swap_pages);
-		max = min(max + swap_max, memsw_max);
+		if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
+			max += READ_ONCE(memcg->swap.max);
+		else
+			max = memcg->memsw.max;
 	}
 	return max;
 }
Signature · 1 line
2.18.1
#9 lore raw

PATCH 3/3 mm/memcg: Unify swap and memsw page counters

In reply to Waiman Long

The swap page counter is v2 only while memsw is v1 only. As v1 and v2 controllers cannot be active at the same time, there is no point to keep both swap and memsw page counters in mem_cgroup. The previous patch has made sure that memsw page counter is updated and accessed only when in v1 code paths. So it is now safe to alias the v1 memsw page counter to v2 swap page counter. This saves 14 long's in the size of mem_cgroup. This is a saving of 112 bytes for 64-bit archs.

---
 include/linux/memcontrol.h | 3 +--
 mm/memcontrol.c            | 8 +++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +1 -2
index d0b036123c6a..d2a819d7db70 100644
@@ -216,10 +216,9 @@ struct mem_cgroup {
 
 	/* Accounted resources */
 	struct page_counter memory;
-	struct page_counter swap;
+	struct page_counter swap;	/* memsw (memory+swap) for v1 */
 
 	/* Legacy consumer-oriented counters */
-	struct page_counter memsw;
 	struct page_counter kmem;
 	struct page_counter tcpmem;
 
mm/memcontrol.c +5 -3
index d219dca5239f..04c3794cdc98 100644
@@ -68,6 +68,11 @@
 
 #include <trace/events/vmscan.h>
 
+/*
+ * The v1 memsw page counter is aliased to the v2 swap page counter.
+ */
+#define memsw	swap
+
 struct cgroup_subsys memory_cgrp_subsys __read_mostly;
 EXPORT_SYMBOL(memory_cgrp_subsys);
 
@@ -5279,13 +5284,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 		memcg->use_hierarchy = true;
 		page_counter_init(&memcg->memory, &parent->memory);
 		page_counter_init(&memcg->swap, &parent->swap);
-		page_counter_init(&memcg->memsw, &parent->memsw);
 		page_counter_init(&memcg->kmem, &parent->kmem);
 		page_counter_init(&memcg->tcpmem, &parent->tcpmem);
 	} else {
 		page_counter_init(&memcg->memory, NULL);
 		page_counter_init(&memcg->swap, NULL);
-		page_counter_init(&memcg->memsw, NULL);
 		page_counter_init(&memcg->kmem, NULL);
 		page_counter_init(&memcg->tcpmem, NULL);
 		/*
@@ -5414,7 +5417,6 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
 
 	page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
-	page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
 	page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
 	page_counter_set_min(&memcg->memory, 0);
Signature · 1 line
2.18.1
#10 lore raw

Re: [PATCH 3/3] mm/memcg: Unify swap and memsw page counters

Quoted 36 lines · Waiman Long

The swap page counter is v2 only while memsw is v1 only. As v1 and v2 controllers cannot be active at the same time, there is no point to keep both swap and memsw page counters in mem_cgroup. The previous patch has made sure that memsw page counter is updated and accessed only when in v1 code paths. So it is now safe to alias the v1 memsw page counter to v2 swap page counter. This saves 14 long's in the size of mem_cgroup. This is a saving of 112 bytes for 64-bit archs.

---
 include/linux/memcontrol.h | 3 +--
 mm/memcontrol.c            | 8 +++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +1 -2
index d0b036123c6a..d2a819d7db70 100644
@@ -216,10 +216,9 @@ struct mem_cgroup {
 
        /* Accounted resources */
        struct page_counter memory;
-       struct page_counter swap;
+       struct page_counter swap;       /* memsw (memory+swap) for v1 */
 
        /* Legacy consumer-oriented counters */
-       struct page_counter memsw;
        struct page_counter kmem;
        struct page_counter tcpmem;
 
mm/memcontrol.c +0 -0
index d219dca5239f..04c3794cdc98 100644

mm/memcontrol.c line 75

Quoted 8 lines
mm/memcontrol.c +5 -0
@@ -68,6 +68,11 @@
 
 #include <trace/events/vmscan.h>
 
+/*
+ * The v1 memsw page counter is aliased to the v2 swap page counter.
+ */
+#define memsw  swap
+

Personally I would prefer a union instead of #define.

Quoted 28 lines
mm/memcontrol.c +0 -4
 struct cgroup_subsys memory_cgrp_subsys __read_mostly;
 EXPORT_SYMBOL(memory_cgrp_subsys);
 
@@ -5279,13 +5284,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
                memcg->use_hierarchy = true;
                page_counter_init(&memcg->memory, &parent->memory);
                page_counter_init(&memcg->swap, &parent->swap);
-               page_counter_init(&memcg->memsw, &parent->memsw);
                page_counter_init(&memcg->kmem, &parent->kmem);
                page_counter_init(&memcg->tcpmem, &parent->tcpmem);
        } else {
                page_counter_init(&memcg->memory, NULL);
                page_counter_init(&memcg->swap, NULL);
-               page_counter_init(&memcg->memsw, NULL);
                page_counter_init(&memcg->kmem, NULL);
                page_counter_init(&memcg->tcpmem, NULL);
                /*
@@ -5414,7 +5417,6 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
 
        page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
        page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
-       page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX);
        page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
        page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
        page_counter_set_min(&memcg->memory, 0);
--

2.18.1

maintainer Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] mm/memcg: Clean up obsolete enum charge_type

In reply to Waiman Long
Quoted 6 lines · Waiman Long

Since commit 0a31bc97c80c ("mm: memcontrol: rewrite uncharge API") and commit 00501b531c47 ("mm: memcontrol: rewrite charge API") in v3.17, the enum charge_type was no longer used anywhere. However, the enum itself was not removed at that time. Remove the obsolete enum charge_type now.

maintainer
#5 lore raw

Re: [PATCH 2/3] mm/memcg: Simplify mem_cgroup_get_max()

In reply to Waiman Long
Quoted 25 lines · Waiman Long

The mem_cgroup_get_max() function used to get memory+swap max from both the v1 memsw and v2 memory+swap page counters & return the maximum of these 2 values. This is redundant and it is more efficient to just get either the v1 or the v2 values depending on which one is currently in use.

---
 mm/memcontrol.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
mm/memcontrol.c +1 -3
index 26b7a48d3afb..d219dca5239f 100644
@@ -1633,17 +1633,13 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
  */
 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
 {
-	unsigned long max;
+	unsigned long max = READ_ONCE(memcg->memory.max);
 
-	max = READ_ONCE(memcg->memory.max);
 	if (mem_cgroup_swappiness(memcg)) {
-		unsigned long memsw_max;

mm/memcontrol.c void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) · line 1642

Quoted 10 lines
mm/memcontrol.c +4 -6
-		unsigned long swap_max;
-
-		memsw_max = memcg->memsw.max;
-		swap_max = READ_ONCE(memcg->swap.max);
-		swap_max = min(swap_max, (unsigned long)total_swap_pages);
-		max = min(max + swap_max, memsw_max);
+		if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
+			max += READ_ONCE(memcg->swap.max);
+		else
+			max = memcg->memsw.max;

I agree with the premise of the patch, but v1 and v2 have sufficiently different logic, and the way v1 overrides max from the innermost branch again also doesn't help in understanding what's going on.

Can you please split out the v1 and v2 code?

	if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
		max = READ_ONCE(memcg->memory.max);
		if (mem_cgroup_swappiness(memcg))
			max += READ_ONCE(memcg->swap.max);
	} else {
		if (mem_cgroup_swappiness(memcg))
			max = memcg->memsw.max;
		else
			max = READ_ONCE(memcg->memory.max);
	}

It's slightly repetitive, but IMO much more readable.

#6 lore raw

Re: [PATCH 2/3] mm/memcg: Simplify mem_cgroup_get_max()

Quoted 53 lines · Johannes Weiner
Quoted 25 lines · Waiman Long

The mem_cgroup_get_max() function used to get memory+swap max from both the v1 memsw and v2 memory+swap page counters & return the maximum of these 2 values. This is redundant and it is more efficient to just get either the v1 or the v2 values depending on which one is currently in use.

---
  mm/memcontrol.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
mm/memcontrol.c +1 -3
index 26b7a48d3afb..d219dca5239f 100644
@@ -1633,17 +1633,13 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
   */
  unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
  {
-	unsigned long max;
+	unsigned long max = READ_ONCE(memcg->memory.max);
 
-	max = READ_ONCE(memcg->memory.max);
  	if (mem_cgroup_swappiness(memcg)) {
-		unsigned long memsw_max;

mm/memcontrol.c void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) · line 1642

Quoted 10 lines
mm/memcontrol.c +4 -6
-		unsigned long swap_max;
-
-		memsw_max = memcg->memsw.max;
-		swap_max = READ_ONCE(memcg->swap.max);
-		swap_max = min(swap_max, (unsigned long)total_swap_pages);
-		max = min(max + swap_max, memsw_max);
+		if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
+			max += READ_ONCE(memcg->swap.max);
+		else
+			max = memcg->memsw.max;

I agree with the premise of the patch, but v1 and v2 have sufficiently different logic, and the way v1 overrides max from the innermost branch again also doesn't help in understanding what's going on.

Can you please split out the v1 and v2 code?

	if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
		max = READ_ONCE(memcg->memory.max);
		if (mem_cgroup_swappiness(memcg))
			max += READ_ONCE(memcg->swap.max);
	} else {
		if (mem_cgroup_swappiness(memcg))
			max = memcg->memsw.max;
		else
			max = READ_ONCE(memcg->memory.max);
	}
Quoted 2 lines

It's slightly repetitive, but IMO much more readable.

Sure. That makes it even better.

Cheers,
Longman

Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] mm/memcg: Clean up obsolete enum charge_type

In reply to Waiman Long
Quoted 7 lines · Waiman Long

Since commit 0a31bc97c80c ("mm: memcontrol: rewrite uncharge API") and commit 00501b531c47 ("mm: memcontrol: rewrite charge API") in v3.17, the enum charge_type was no longer used anywhere. However, the enum itself was not removed at that time. Remove the obsolete enum charge_type now.

#7 lore raw

Re: [PATCH 2/3] mm/memcg: Simplify mem_cgroup_get_max()

In reply to Waiman Long
Quoted 55 lines · Waiman Long
Quoted 53 lines · Johannes Weiner
Quoted 25 lines · Waiman Long

The mem_cgroup_get_max() function used to get memory+swap max from both the v1 memsw and v2 memory+swap page counters & return the maximum of these 2 values. This is redundant and it is more efficient to just get either the v1 or the v2 values depending on which one is currently in use.

---
  mm/memcontrol.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
mm/memcontrol.c +1 -3
index 26b7a48d3afb..d219dca5239f 100644
@@ -1633,17 +1633,13 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
   */
  unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
  {
-    unsigned long max;
+    unsigned long max = READ_ONCE(memcg->memory.max);
 
-    max = READ_ONCE(memcg->memory.max);
     if (mem_cgroup_swappiness(memcg)) {
-            unsigned long memsw_max;

mm/memcontrol.c void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) · line 1642

Quoted 10 lines
mm/memcontrol.c +4 -6
-            unsigned long swap_max;
-
-            memsw_max = memcg->memsw.max;
-            swap_max = READ_ONCE(memcg->swap.max);
-            swap_max = min(swap_max, (unsigned long)total_swap_pages);
-            max = min(max + swap_max, memsw_max);
+            if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
+                    max += READ_ONCE(memcg->swap.max);
+            else
+                    max = memcg->memsw.max;

I agree with the premise of the patch, but v1 and v2 have sufficiently different logic, and the way v1 overrides max from the innermost branch again also doesn't help in understanding what's going on.

Can you please split out the v1 and v2 code?

      if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
              max = READ_ONCE(memcg->memory.max);
              if (mem_cgroup_swappiness(memcg))
                      max += READ_ONCE(memcg->swap.max);
      } else {
              if (mem_cgroup_swappiness(memcg))
                      max = memcg->memsw.max;
              else
                      max = READ_ONCE(memcg->memory.max);
      }
Quoted 4 lines
Quoted 2 lines

It's slightly repetitive, but IMO much more readable.

Sure. That makes it even better.

Can you please also add in the commit message why it is ok to drop total_swap_pages comparison from mem_cgroup_get_max()?

#11 lore raw

Re: [PATCH 3/3] mm/memcg: Unify swap and memsw page counters

In reply to Shakeel Butt
Quoted 37 lines · Shakeel Butt
Quoted 36 lines · Waiman Long

The swap page counter is v2 only while memsw is v1 only. As v1 and v2 controllers cannot be active at the same time, there is no point to keep both swap and memsw page counters in mem_cgroup. The previous patch has made sure that memsw page counter is updated and accessed only when in v1 code paths. So it is now safe to alias the v1 memsw page counter to v2 swap page counter. This saves 14 long's in the size of mem_cgroup. This is a saving of 112 bytes for 64-bit archs.

---
  include/linux/memcontrol.h | 3 +--
  mm/memcontrol.c            | 8 +++++---
  2 files changed, 6 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +1 -2
index d0b036123c6a..d2a819d7db70 100644
@@ -216,10 +216,9 @@ struct mem_cgroup {
 
         /* Accounted resources */
         struct page_counter memory;
-       struct page_counter swap;
+       struct page_counter swap;       /* memsw (memory+swap) for v1 */
 
         /* Legacy consumer-oriented counters */
-       struct page_counter memsw;
         struct page_counter kmem;
         struct page_counter tcpmem;
 
mm/memcontrol.c +0 -0
index d219dca5239f..04c3794cdc98 100644
@@ -68,6 +68,11 @@
 
Quoted 8 lines

mm/memcontrol.c line 75

Quoted 7 lines
mm/memcontrol.c +5 -0
  #include <trace/events/vmscan.h>
 
+/*
+ * The v1 memsw page counter is aliased to the v2 swap page counter.
+ */
+#define memsw  swap
+

Personally I would prefer a union instead of #define.

Yes, that is also what I am thinking about in the v2.

Cheers,
Longman

#8 lore raw

Re: [PATCH 2/3] mm/memcg: Simplify mem_cgroup_get_max()

In reply to Shakeel Butt
Quoted 55 lines · Shakeel Butt
Quoted 54 lines · Waiman Long
Quoted 53 lines · Johannes Weiner
Quoted 25 lines · Waiman Long

The mem_cgroup_get_max() function used to get memory+swap max from both the v1 memsw and v2 memory+swap page counters & return the maximum of these 2 values. This is redundant and it is more efficient to just get either the v1 or the v2 values depending on which one is currently in use.

---
   mm/memcontrol.c | 14 +++++---------
   1 file changed, 5 insertions(+), 9 deletions(-)
mm/memcontrol.c +1 -3
index 26b7a48d3afb..d219dca5239f 100644
@@ -1633,17 +1633,13 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
    */
   unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
   {
-    unsigned long max;
+    unsigned long max = READ_ONCE(memcg->memory.max);
 
-    max = READ_ONCE(memcg->memory.max);
      if (mem_cgroup_swappiness(memcg)) {
-            unsigned long memsw_max;

mm/memcontrol.c void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) · line 1642

Quoted 10 lines
mm/memcontrol.c +4 -6
-            unsigned long swap_max;
-
-            memsw_max = memcg->memsw.max;
-            swap_max = READ_ONCE(memcg->swap.max);
-            swap_max = min(swap_max, (unsigned long)total_swap_pages);
-            max = min(max + swap_max, memsw_max);
+            if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
+                    max += READ_ONCE(memcg->swap.max);
+            else
+                    max = memcg->memsw.max;

I agree with the premise of the patch, but v1 and v2 have sufficiently different logic, and the way v1 overrides max from the innermost branch again also doesn't help in understanding what's going on.

Can you please split out the v1 and v2 code?

       if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
               max = READ_ONCE(memcg->memory.max);
               if (mem_cgroup_swappiness(memcg))
                       max += READ_ONCE(memcg->swap.max);
       } else {
               if (mem_cgroup_swappiness(memcg))
                       max = memcg->memsw.max;
               else
                       max = READ_ONCE(memcg->memory.max);
       }
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

It's slightly repetitive, but IMO much more readable.

Sure. That makes it even better.

Can you please also add in the commit message why it is ok to drop total_swap_pages comparison from mem_cgroup_get_max()?

My bad. I accidentally skipped the total_swap_pages check. Will add it back in v2.

Cheers,
Longman