[PATCH v1 1/4] mm: memcg: convert enum res_type to mem_counter_type

Roman Gushchin
2024-05-03T20:19:00Z

The res_type enum is listing all types of memory tracked by memory
cgroups: generic memory, swap, kernel memory, tcp etc; and it's
currently used only for dealing with corresponding sysfs files.

To prepare for tracking of various types of memory by a single
page_counter structure, a similar enumeration is needed. Instead
of introducing a completely new enumeration, let's re-purpose
the existing one: rename it into mem_counter_type, change items
names to be more meaningful and move to page_counter.h. The latter
is needed to have the total number of different memory types
available.

This change doesn't bring any functional difference, it's a pure
refactoring.

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
---
include/linux/page_counter.h | 8 ++++
mm/memcontrol.c | 91 +++++++++++++++++-------------------
2 files changed, 50 insertions(+), 49 deletions(-)

diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
index 8cd858d912c4..2486f98a0c71 100644
--- a/include/linux/page_counter.h
+++ b/include/linux/page_counter.h
@@ -7,6 +7,14 @@
#include <linux/limits.h>
#include <asm/page.h>

+enum mem_counter_type {
+ MCT_MEMORY, /* cgroup v1 and v2 */
+ MCT_SWAP, /* cgroup v2 only */
+ MCT_MEMSW = MCT_SWAP, /* cgroup v1 only */
+ MCT_KMEM, /* cgroup v1 only */
+ MCT_TCPMEM, /* cgroup v1 only */
+};
+
struct page_counter {
/*
* Make sure 'usage' does not share cacheline with any other field. The
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index a13d16cde372..894e5b6fe468 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -206,14 +206,6 @@ static struct move_charge_struct {
#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2

-/* for encoding cft->private value on file */
-enum res_type {
- _MEM,
- _MEMSWAP,
- _KMEM,
- _TCP,
-};
-
#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
#define MEMFILE_ATTR(val) ((val) & 0xffff)
@@ -4108,16 +4100,16 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
struct page_counter *counter;

switch (MEMFILE_TYPE(cft->private)) {
- case _MEM:
+ case MCT_MEMORY:
counter = &memcg->memory;
break;
- case _MEMSWAP:
+ case MCT_MEMSW:
counter = &memcg->memsw;
break;
- case _KMEM:
+ case MCT_KMEM:
counter = &memcg->kmem;
break;
- case _TCP:
+ case MCT_TCPMEM:
counter = &memcg->tcpmem;
break;
default:
@@ -4273,20 +4265,20 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
break;
}
switch (MEMFILE_TYPE(of_cft(of)->private)) {
- case _MEM:
+ case MCT_MEMORY:
ret = mem_cgroup_resize_max(memcg, nr_pages, false);
break;
- case _MEMSWAP:
+ case MCT_MEMSW:
ret = mem_cgroup_resize_max(memcg, nr_pages, true);
break;
- case _KMEM:
+ case MCT_KMEM:
pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
"Writing any value to this file has no effect. "
"Please report your usecase to linux-mm@kvack.org if you "
"depend on this functionality.\n");
ret = 0;
break;
- case _TCP:
+ case MCT_TCPMEM:
ret = memcg_update_tcp_max(memcg, nr_pages);
break;
}
@@ -4310,16 +4302,16 @@ static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
struct page_counter *counter;

switch (MEMFILE_TYPE(of_cft(of)->private)) {
- case _MEM:
+ case MCT_MEMORY:
counter = &memcg->memory;
break;
- case _MEMSWAP:
+ case MCT_MEMSW:
counter = &memcg->memsw;
break;
- case _KMEM:
+ case MCT_KMEM:
counter = &memcg->kmem;
break;
- case _TCP:
+ case MCT_TCPMEM:
counter = &memcg->tcpmem;
break;
default:
@@ -4706,7 +4698,8 @@ static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
}

static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
- struct eventfd_ctx *eventfd, const char *args, enum res_type type)
+ struct eventfd_ctx *eventfd, const char *args,
+ enum mem_counter_type type)
{
struct mem_cgroup_thresholds *thresholds;
struct mem_cgroup_threshold_ary *new;
@@ -4720,10 +4713,10 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,

mutex_lock(&memcg->thresholds_lock);

- if (type == _MEM) {
+ if (type == MCT_MEMORY) {
thresholds = &memcg->thresholds;
usage = mem_cgroup_usage(memcg, false);
- } else if (type == _MEMSWAP) {
+ } else if (type == MCT_MEMSW) {
thresholds = &memcg->memsw_thresholds;
usage = mem_cgroup_usage(memcg, true);
} else
@@ -4731,7 +4724,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,

/* Check if a threshold crossed before adding a new one */
if (thresholds->primary)
- __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+ __mem_cgroup_threshold(memcg, type == MCT_MEMSW);

size = thresholds->primary ? thresholds->primary->size + 1 : 1;

@@ -4788,17 +4781,17 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
struct eventfd_ctx *eventfd, const char *args)
{
- return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
+ return __mem_cgroup_usage_register_event(memcg, eventfd, args, MCT_MEMORY);
}

static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
struct eventfd_ctx *eventfd, const char *args)
{
- return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
+ return __mem_cgroup_usage_register_event(memcg, eventfd, args, MCT_MEMSW);
}

static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
- struct eventfd_ctx *eventfd, enum res_type type)
+ struct eventfd_ctx *eventfd, enum mem_counter_type type)
{
struct mem_cgroup_thresholds *thresholds;
struct mem_cgroup_threshold_ary *new;
@@ -4807,10 +4800,10 @@ static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,

mutex_lock(&memcg->thresholds_lock);

- if (type == _MEM) {
+ if (type == MCT_MEMORY) {
thresholds = &memcg->thresholds;
usage = mem_cgroup_usage(memcg, false);
- } else if (type == _MEMSWAP) {
+ } else if (type == MCT_MEMSW) {
thresholds = &memcg->memsw_thresholds;
usage = mem_cgroup_usage(memcg, true);
} else
@@ -4820,7 +4813,7 @@ static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
goto unlock;

/* Check if a threshold crossed before removing */
- __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+ __mem_cgroup_threshold(memcg, type == MCT_MEMSW);

/* Calculate new number of threshold */
size = entries = 0;
@@ -4885,13 +4878,13 @@ static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
struct eventfd_ctx *eventfd)
{
- return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
+ return __mem_cgroup_usage_unregister_event(memcg, eventfd, MCT_MEMORY);
}

static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
struct eventfd_ctx *eventfd)
{
- return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
+ return __mem_cgroup_usage_unregister_event(memcg, eventfd, MCT_MEMSW);
}

static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
@@ -5426,30 +5419,30 @@ static int memory_stat_show(struct seq_file *m, void *v);
static struct cftype mem_cgroup_legacy_files[] = {
{
.name = "usage_in_bytes",
- .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_MEMORY, RES_USAGE),
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "max_usage_in_bytes",
- .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_MEMORY, RES_MAX_USAGE),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "limit_in_bytes",
- .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
+ .private = MEMFILE_PRIVATE(MCT_MEMORY, RES_LIMIT),
.write = mem_cgroup_write,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "soft_limit_in_bytes",
- .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
+ .private = MEMFILE_PRIVATE(MCT_MEMORY, RES_SOFT_LIMIT),
.write = mem_cgroup_write,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "failcnt",
- .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
+ .private = MEMFILE_PRIVATE(MCT_MEMORY, RES_FAILCNT),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
@@ -5498,24 +5491,24 @@ static struct cftype mem_cgroup_legacy_files[] = {
#endif
{
.name = "kmem.limit_in_bytes",
- .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
+ .private = MEMFILE_PRIVATE(MCT_KMEM, RES_LIMIT),
.write = mem_cgroup_write,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.usage_in_bytes",
- .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_KMEM, RES_USAGE),
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.failcnt",
- .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
+ .private = MEMFILE_PRIVATE(MCT_KMEM, RES_FAILCNT),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.max_usage_in_bytes",
- .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_KMEM, RES_MAX_USAGE),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
@@ -5527,24 +5520,24 @@ static struct cftype mem_cgroup_legacy_files[] = {
#endif
{
.name = "kmem.tcp.limit_in_bytes",
- .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
+ .private = MEMFILE_PRIVATE(MCT_TCPMEM, RES_LIMIT),
.write = mem_cgroup_write,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.tcp.usage_in_bytes",
- .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_TCPMEM, RES_USAGE),
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.tcp.failcnt",
- .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
+ .private = MEMFILE_PRIVATE(MCT_TCPMEM, RES_FAILCNT),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "kmem.tcp.max_usage_in_bytes",
- .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_TCPMEM, RES_MAX_USAGE),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
@@ -8394,24 +8387,24 @@ static struct cftype swap_files[] = {
static struct cftype memsw_files[] = {
{
.name = "memsw.usage_in_bytes",
- .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_MEMSW, RES_USAGE),
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "memsw.max_usage_in_bytes",
- .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
+ .private = MEMFILE_PRIVATE(MCT_MEMSW, RES_MAX_USAGE),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "memsw.limit_in_bytes",
- .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
+ .private = MEMFILE_PRIVATE(MCT_MEMSW, RES_LIMIT),
.write = mem_cgroup_write,
.read_u64 = mem_cgroup_read_u64,
},
{
.name = "memsw.failcnt",
- .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
+ .private = MEMFILE_PRIVATE(MCT_MEMSW, RES_FAILCNT),
.write = mem_cgroup_reset,
.read_u64 = mem_cgroup_read_u64,
},
--
2.43.2