Skip to content

Commit f3344ad

Browse files
Muchun Songtorvalds
authored andcommitted
mm: memcontrol: optimize per-lruvec stats counter memory usage
The vmstat threshold is 32 (MEMCG_CHARGE_BATCH), Actually the threshold can be as big as MEMCG_CHARGE_BATCH * PAGE_SIZE. It still fits into s32. So introduce struct batched_lruvec_stat to optimize memory usage. The size of struct lruvec_stat is 304 bytes on 64 bit systems. As it is a per-cpu structure. So with this patch, we can save 304 / 2 * ncpu bytes per-memcg per-node where ncpu is the number of the possible CPU. If there are c memory cgroup (include dying cgroup) and n NUMA node in the system. Finally, we can save (152 * ncpu * c * n) bytes. [akpm@linux-foundation.org: fix typo in comment] Link: https://lkml.kernel.org/r/20201210042121.39665-1-songmuchun@bytedance.com Signed-off-by: Muchun Song <songmuchun@bytedance.com> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Shakeel Butt <shakeelb@google.com> Cc: Roman Gushchin <guro@fb.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Chris Down <chris@chrisdown.name> Cc: Yafang Shao <laoar.shao@gmail.com> Cc: Wei Yang <richard.weiyang@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2e9bd48 commit f3344ad

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

include/linux/memcontrol.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ struct lruvec_stat {
9292
long count[NR_VM_NODE_STAT_ITEMS];
9393
};
9494

95+
struct batched_lruvec_stat {
96+
s32 count[NR_VM_NODE_STAT_ITEMS];
97+
};
98+
9599
/*
96100
* Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
97101
* which have elements charged to this memcg.
@@ -107,11 +111,17 @@ struct memcg_shrinker_map {
107111
struct mem_cgroup_per_node {
108112
struct lruvec lruvec;
109113

110-
/* Legacy local VM stats */
114+
/*
115+
* Legacy local VM stats. This should be struct lruvec_stat and
116+
* cannot be optimized to struct batched_lruvec_stat. Because
117+
* the threshold of the lruvec_stat_cpu can be as big as
118+
* MEMCG_CHARGE_BATCH * PAGE_SIZE. It can fit into s32. But this
119+
* filed has no upper limit.
120+
*/
111121
struct lruvec_stat __percpu *lruvec_stat_local;
112122

113123
/* Subtree VM stats (batched updates) */
114-
struct lruvec_stat __percpu *lruvec_stat_cpu;
124+
struct batched_lruvec_stat __percpu *lruvec_stat_cpu;
115125
atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
116126

117127
unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];

mm/memcontrol.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5208,7 +5208,7 @@ static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
52085208
return 1;
52095209
}
52105210

5211-
pn->lruvec_stat_cpu = alloc_percpu_gfp(struct lruvec_stat,
5211+
pn->lruvec_stat_cpu = alloc_percpu_gfp(struct batched_lruvec_stat,
52125212
GFP_KERNEL_ACCOUNT);
52135213
if (!pn->lruvec_stat_cpu) {
52145214
free_percpu(pn->lruvec_stat_local);
@@ -7093,6 +7093,14 @@ static int __init mem_cgroup_init(void)
70937093
{
70947094
int cpu, node;
70957095

7096+
/*
7097+
* Currently s32 type (can refer to struct batched_lruvec_stat) is
7098+
* used for per-memcg-per-cpu caching of per-node statistics. In order
7099+
* to work fine, we should make sure that the overfill threshold can't
7100+
* exceed S32_MAX / PAGE_SIZE.
7101+
*/
7102+
BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
7103+
70967104
cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
70977105
memcg_hotplug_cpu_dead);
70987106

0 commit comments

Comments
 (0)