Skip to content

Commit 68ac5b3

Browse files
Waiman-Longtorvalds
authored andcommitted
mm/memcg: cache vmstat data in percpu memcg_stock_pcp
Before the new slab memory controller with per object byte charging, charging and vmstat data update happen only when new slab pages are allocated or freed. Now they are done with every kmem_cache_alloc() and kmem_cache_free(). This causes additional overhead for workloads that generate a lot of alloc and free calls. The memcg_stock_pcp is used to cache byte charge for a specific obj_cgroup to reduce that overhead. To further reducing it, this patch makes the vmstat data cached in the memcg_stock_pcp structure as well until it accumulates a page size worth of update or when other cached data change. Caching the vmstat data in the per-cpu stock eliminates two writes to non-hot cachelines for memcg specific as well as memcg-lruvecs specific vmstat data by a write to a hot local stock cacheline. On a 2-socket Cascade Lake server with instrumentation enabled and this patch applied, it was found that about 20% (634400 out of 3243830) of the time when mod_objcg_state() is called leads to an actual call to __mod_objcg_state() after initial boot. When doing parallel kernel build, the figure was about 17% (24329265 out of 142512465). So caching the vmstat data reduces the number of calls to __mod_objcg_state() by more than 80%. Link: https://lkml.kernel.org/r/20210506150007.16288-3-longman@redhat.com Signed-off-by: Waiman Long <longman@redhat.com> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Alex Shi <alex.shi@linux.alibaba.com> Cc: Chris Down <chris@chrisdown.name> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Masayoshi Mizuma <msys.mizuma@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Muchun Song <songmuchun@bytedance.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: Roman Gushchin <guro@fb.com> Cc: Tejun Heo <tj@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Wei Yang <richard.weiyang@gmail.com> Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com> Cc: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fdbcb2a commit 68ac5b3

1 file changed

Lines changed: 87 additions & 3 deletions

File tree

‎mm/memcontrol.c‎

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,16 +782,17 @@ void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val)
782782
rcu_read_unlock();
783783
}
784784

785-
void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
786-
enum node_stat_item idx, int nr)
785+
static inline void mod_objcg_mlstate(struct obj_cgroup *objcg,
786+
struct pglist_data *pgdat,
787+
enum node_stat_item idx, int nr)
787788
{
788789
struct mem_cgroup *memcg;
789790
struct lruvec *lruvec;
790791

791792
rcu_read_lock();
792793
memcg = obj_cgroup_memcg(objcg);
793794
lruvec = mem_cgroup_lruvec(memcg, pgdat);
794-
mod_memcg_lruvec_state(lruvec, idx, nr);
795+
__mod_memcg_lruvec_state(lruvec, idx, nr);
795796
rcu_read_unlock();
796797
}
797798

@@ -2059,7 +2060,10 @@ struct memcg_stock_pcp {
20592060

20602061
#ifdef CONFIG_MEMCG_KMEM
20612062
struct obj_cgroup *cached_objcg;
2063+
struct pglist_data *cached_pgdat;
20622064
unsigned int nr_bytes;
2065+
int nr_slab_reclaimable_b;
2066+
int nr_slab_unreclaimable_b;
20632067
#endif
20642068

20652069
struct work_struct work;
@@ -3008,6 +3012,67 @@ void __memcg_kmem_uncharge_page(struct page *page, int order)
30083012
obj_cgroup_put(objcg);
30093013
}
30103014

3015+
void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
3016+
enum node_stat_item idx, int nr)
3017+
{
3018+
struct memcg_stock_pcp *stock;
3019+
unsigned long flags;
3020+
int *bytes;
3021+
3022+
local_irq_save(flags);
3023+
stock = this_cpu_ptr(&memcg_stock);
3024+
3025+
/*
3026+
* Save vmstat data in stock and skip vmstat array update unless
3027+
* accumulating over a page of vmstat data or when pgdat or idx
3028+
* changes.
3029+
*/
3030+
if (stock->cached_objcg != objcg) {
3031+
drain_obj_stock(stock);
3032+
obj_cgroup_get(objcg);
3033+
stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3034+
? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3035+
stock->cached_objcg = objcg;
3036+
stock->cached_pgdat = pgdat;
3037+
} else if (stock->cached_pgdat != pgdat) {
3038+
/* Flush the existing cached vmstat data */
3039+
if (stock->nr_slab_reclaimable_b) {
3040+
mod_objcg_mlstate(objcg, pgdat, NR_SLAB_RECLAIMABLE_B,
3041+
stock->nr_slab_reclaimable_b);
3042+
stock->nr_slab_reclaimable_b = 0;
3043+
}
3044+
if (stock->nr_slab_unreclaimable_b) {
3045+
mod_objcg_mlstate(objcg, pgdat, NR_SLAB_UNRECLAIMABLE_B,
3046+
stock->nr_slab_unreclaimable_b);
3047+
stock->nr_slab_unreclaimable_b = 0;
3048+
}
3049+
stock->cached_pgdat = pgdat;
3050+
}
3051+
3052+
bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b
3053+
: &stock->nr_slab_unreclaimable_b;
3054+
/*
3055+
* Even for large object >= PAGE_SIZE, the vmstat data will still be
3056+
* cached locally at least once before pushing it out.
3057+
*/
3058+
if (!*bytes) {
3059+
*bytes = nr;
3060+
nr = 0;
3061+
} else {
3062+
*bytes += nr;
3063+
if (abs(*bytes) > PAGE_SIZE) {
3064+
nr = *bytes;
3065+
*bytes = 0;
3066+
} else {
3067+
nr = 0;
3068+
}
3069+
}
3070+
if (nr)
3071+
mod_objcg_mlstate(objcg, pgdat, idx, nr);
3072+
3073+
local_irq_restore(flags);
3074+
}
3075+
30113076
static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
30123077
{
30133078
struct memcg_stock_pcp *stock;
@@ -3055,6 +3120,25 @@ static void drain_obj_stock(struct memcg_stock_pcp *stock)
30553120
stock->nr_bytes = 0;
30563121
}
30573122

3123+
/*
3124+
* Flush the vmstat data in current stock
3125+
*/
3126+
if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) {
3127+
if (stock->nr_slab_reclaimable_b) {
3128+
mod_objcg_mlstate(old, stock->cached_pgdat,
3129+
NR_SLAB_RECLAIMABLE_B,
3130+
stock->nr_slab_reclaimable_b);
3131+
stock->nr_slab_reclaimable_b = 0;
3132+
}
3133+
if (stock->nr_slab_unreclaimable_b) {
3134+
mod_objcg_mlstate(old, stock->cached_pgdat,
3135+
NR_SLAB_UNRECLAIMABLE_B,
3136+
stock->nr_slab_unreclaimable_b);
3137+
stock->nr_slab_unreclaimable_b = 0;
3138+
}
3139+
stock->cached_pgdat = NULL;
3140+
}
3141+
30583142
obj_cgroup_put(old);
30593143
stock->cached_objcg = NULL;
30603144
}

0 commit comments

Comments
 (0)