Commit 3c6f17e6 authored by Yang Shi's avatar Yang Shi Committed by Linus Torvalds

mm: vmscan: add per memcg shrinker nr_deferred

Currently the number of deferred objects are per shrinker, but some
slabs, for example, vfs inode/dentry cache are per memcg, this would
result in poor isolation among memcgs.

The deferred objects typically are generated by __GFP_NOFS allocations,
one memcg with excessive __GFP_NOFS allocations may blow up deferred
objects, then other innocent memcgs may suffer from over shrink,
excessive reclaim latency, etc.

For example, two workloads run in memcgA and memcgB respectively,
workload in B is vfs heavy workload.  Workload in A generates excessive
deferred objects, then B's vfs cache might be hit heavily (drop half of
caches) by B's limit reclaim or global reclaim.

We observed this hit in our production environment which was running vfs
heavy workload shown as the below tracing log:

  <...>-409454 [016] .... 28286961.747146: mm_shrink_slab_start: super_cache_scan+0x0/0x1a0 ffff9a83046f3458:
  nid: 1 objects to shrink 3641681686040 gfp_flags GFP_HIGHUSER_MOVABLE|__GFP_ZERO pgs_scanned 1 lru_pgs 15721
  cache items 246404277 delta 31345 total_scan 123202138
  <...>-409454 [022] .... 28287105.928018: mm_shrink_slab_end: super_cache_scan+0x0/0x1a0 ffff9a83046f3458:
  nid: 1 unused scan count 3641681686040 new scan count 3641798379189 total_scan 602
  last shrinker return val 123186855

The vfs cache and page cache ratio was 10:1 on this machine, and half of
caches were dropped.  This also resulted in significant amount of page
caches were dropped due to inodes eviction.

Make nr_deferred per memcg for memcg aware shrinkers would solve the
unfairness and bring better isolation.

The following patch will add nr_deferred to parent memcg when memcg
offline.  To preserve nr_deferred when reparenting memcgs to root, root
memcg needs shrinker_info allocated too.

When memcg is not enabled (!CONFIG_MEMCG or memcg disabled), the
shrinker's nr_deferred would be used.  And non memcg aware shrinkers use
shrinker's nr_deferred all the time.

Link: https://lkml.kernel.org/r/20210311190845.9708-10-shy828301@gmail.comSigned-off-by: default avatarYang Shi <shy828301@gmail.com>
Acked-by: default avatarRoman Gushchin <guro@fb.com>
Acked-by: default avatarKirill Tkhai <ktkhai@virtuozzo.com>
Reviewed-by: default avatarShakeel Butt <shakeelb@google.com>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 41ca668a
...@@ -114,12 +114,13 @@ struct batched_lruvec_stat { ...@@ -114,12 +114,13 @@ struct batched_lruvec_stat {
}; };
/* /*
* Bitmap of shrinker::id corresponding to memcg-aware shrinkers, * Bitmap and deferred work of shrinker::id corresponding to memcg-aware
* which have elements charged to this memcg. * shrinkers, which have elements charged to this memcg.
*/ */
struct shrinker_info { struct shrinker_info {
struct rcu_head rcu; struct rcu_head rcu;
unsigned long map[]; atomic_long_t *nr_deferred;
unsigned long *map;
}; };
/* /*
......
...@@ -187,11 +187,17 @@ static DECLARE_RWSEM(shrinker_rwsem); ...@@ -187,11 +187,17 @@ static DECLARE_RWSEM(shrinker_rwsem);
#ifdef CONFIG_MEMCG #ifdef CONFIG_MEMCG
static int shrinker_nr_max; static int shrinker_nr_max;
/* The shrinker_info is expanded in a batch of BITS_PER_LONG */
static inline int shrinker_map_size(int nr_items) static inline int shrinker_map_size(int nr_items)
{ {
return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long));
} }
static inline int shrinker_defer_size(int nr_items)
{
return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t));
}
static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
int nid) int nid)
{ {
...@@ -200,11 +206,13 @@ static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, ...@@ -200,11 +206,13 @@ static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
} }
static int expand_one_shrinker_info(struct mem_cgroup *memcg, static int expand_one_shrinker_info(struct mem_cgroup *memcg,
int size, int old_size) int map_size, int defer_size,
int old_map_size, int old_defer_size)
{ {
struct shrinker_info *new, *old; struct shrinker_info *new, *old;
struct mem_cgroup_per_node *pn; struct mem_cgroup_per_node *pn;
int nid; int nid;
int size = map_size + defer_size;
for_each_node(nid) { for_each_node(nid) {
pn = memcg->nodeinfo[nid]; pn = memcg->nodeinfo[nid];
...@@ -217,9 +225,16 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg, ...@@ -217,9 +225,16 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg,
if (!new) if (!new)
return -ENOMEM; return -ENOMEM;
/* Set all old bits, clear all new bits */ new->nr_deferred = (atomic_long_t *)(new + 1);
memset(new->map, (int)0xff, old_size); new->map = (void *)new->nr_deferred + defer_size;
memset((void *)new->map + old_size, 0, size - old_size);
/* map: set all old bits, clear all new bits */
memset(new->map, (int)0xff, old_map_size);
memset((void *)new->map + old_map_size, 0, map_size - old_map_size);
/* nr_deferred: copy old values, clear all new values */
memcpy(new->nr_deferred, old->nr_deferred, old_defer_size);
memset((void *)new->nr_deferred + old_defer_size, 0,
defer_size - old_defer_size);
rcu_assign_pointer(pn->shrinker_info, new); rcu_assign_pointer(pn->shrinker_info, new);
kvfree_rcu(old, rcu); kvfree_rcu(old, rcu);
...@@ -234,9 +249,6 @@ void free_shrinker_info(struct mem_cgroup *memcg) ...@@ -234,9 +249,6 @@ void free_shrinker_info(struct mem_cgroup *memcg)
struct shrinker_info *info; struct shrinker_info *info;
int nid; int nid;
if (mem_cgroup_is_root(memcg))
return;
for_each_node(nid) { for_each_node(nid) {
pn = memcg->nodeinfo[nid]; pn = memcg->nodeinfo[nid];
info = rcu_dereference_protected(pn->shrinker_info, true); info = rcu_dereference_protected(pn->shrinker_info, true);
...@@ -249,12 +261,12 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) ...@@ -249,12 +261,12 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
{ {
struct shrinker_info *info; struct shrinker_info *info;
int nid, size, ret = 0; int nid, size, ret = 0;
int map_size, defer_size = 0;
if (mem_cgroup_is_root(memcg))
return 0;
down_write(&shrinker_rwsem); down_write(&shrinker_rwsem);
size = shrinker_map_size(shrinker_nr_max); map_size = shrinker_map_size(shrinker_nr_max);
defer_size = shrinker_defer_size(shrinker_nr_max);
size = map_size + defer_size;
for_each_node(nid) { for_each_node(nid) {
info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
if (!info) { if (!info) {
...@@ -262,6 +274,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) ...@@ -262,6 +274,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
ret = -ENOMEM; ret = -ENOMEM;
break; break;
} }
info->nr_deferred = (atomic_long_t *)(info + 1);
info->map = (void *)info->nr_deferred + defer_size;
rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
} }
up_write(&shrinker_rwsem); up_write(&shrinker_rwsem);
...@@ -269,15 +283,21 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) ...@@ -269,15 +283,21 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
return ret; return ret;
} }
static inline bool need_expand(int nr_max)
{
return round_up(nr_max, BITS_PER_LONG) >
round_up(shrinker_nr_max, BITS_PER_LONG);
}
static int expand_shrinker_info(int new_id) static int expand_shrinker_info(int new_id)
{ {
int size, old_size, ret = 0; int ret = 0;
int new_nr_max = new_id + 1; int new_nr_max = new_id + 1;
int map_size, defer_size = 0;
int old_map_size, old_defer_size = 0;
struct mem_cgroup *memcg; struct mem_cgroup *memcg;
size = shrinker_map_size(new_nr_max); if (!need_expand(new_nr_max))
old_size = shrinker_map_size(shrinker_nr_max);
if (size <= old_size)
goto out; goto out;
if (!root_mem_cgroup) if (!root_mem_cgroup)
...@@ -285,11 +305,15 @@ static int expand_shrinker_info(int new_id) ...@@ -285,11 +305,15 @@ static int expand_shrinker_info(int new_id)
lockdep_assert_held(&shrinker_rwsem); lockdep_assert_held(&shrinker_rwsem);
map_size = shrinker_map_size(new_nr_max);
defer_size = shrinker_defer_size(new_nr_max);
old_map_size = shrinker_map_size(shrinker_nr_max);
old_defer_size = shrinker_defer_size(shrinker_nr_max);
memcg = mem_cgroup_iter(NULL, NULL, NULL); memcg = mem_cgroup_iter(NULL, NULL, NULL);
do { do {
if (mem_cgroup_is_root(memcg)) ret = expand_one_shrinker_info(memcg, map_size, defer_size,
continue; old_map_size, old_defer_size);
ret = expand_one_shrinker_info(memcg, size, old_size);
if (ret) { if (ret) {
mem_cgroup_iter_break(NULL, memcg); mem_cgroup_iter_break(NULL, memcg);
goto out; goto out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment