Skip to content

Commit 0c7c1be

Browse files
Kirill Tkhaitorvalds
authored andcommitted
mm: make counting of list_lru_one::nr_items lockless
During the reclaiming slab of a memcg, shrink_slab iterates over all registered shrinkers in the system, and tries to count and consume objects related to the cgroup. In case of memory pressure, this behaves bad: I observe high system time and time spent in list_lru_count_one() for many processes on RHEL7 kernel. This patch makes list_lru_node::memcg_lrus rcu protected, that allows to skip taking spinlock in list_lru_count_one(). Shakeel Butt with the patch observes significant perf graph change. He says: ======================================================================== Setup: running a fork-bomb in a memcg of 200MiB on a 8GiB and 4 vcpu VM and recording the trace with 'perf record -g -a'. The trace without the patch: + 34.19% fb.sh [kernel.kallsyms] [k] queued_spin_lock_slowpath + 30.77% fb.sh [kernel.kallsyms] [k] _raw_spin_lock + 3.53% fb.sh [kernel.kallsyms] [k] list_lru_count_one + 2.26% fb.sh [kernel.kallsyms] [k] super_cache_count + 1.68% fb.sh [kernel.kallsyms] [k] shrink_slab + 0.59% fb.sh [kernel.kallsyms] [k] down_read_trylock + 0.48% fb.sh [kernel.kallsyms] [k] _raw_spin_unlock_irqrestore + 0.38% fb.sh [kernel.kallsyms] [k] shrink_node_memcg + 0.32% fb.sh [kernel.kallsyms] [k] queue_work_on + 0.26% fb.sh [kernel.kallsyms] [k] count_shadow_nodes With the patch: + 0.16% swapper [kernel.kallsyms] [k] default_idle + 0.13% oom_reaper [kernel.kallsyms] [k] mutex_spin_on_owner + 0.05% perf [kernel.kallsyms] [k] copy_user_generic_string + 0.05% init.real [kernel.kallsyms] [k] wait_consider_task + 0.05% kworker/0:0 [kernel.kallsyms] [k] finish_task_switch + 0.04% kworker/2:1 [kernel.kallsyms] [k] finish_task_switch + 0.04% kworker/3:1 [kernel.kallsyms] [k] finish_task_switch + 0.04% kworker/1:0 [kernel.kallsyms] [k] finish_task_switch + 0.03% binary [kernel.kallsyms] [k] copy_page ======================================================================== Thanks Shakeel for the testing. [[email protected]: v2] Link: http://lkml.kernel.org/r/151203869520.3915.2587549826865799173.stgit@localhost.localdomain Link: http://lkml.kernel.org/r/150583358557.26700.8490036563698102569.stgit@localhost.localdomain Signed-off-by: Kirill Tkhai <[email protected]> Tested-by: Shakeel Butt <[email protected]> Acked-by: Vladimir Davydov <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Johannes Weiner <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f5c754d commit 0c7c1be

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

include/linux/list_lru.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct list_lru_one {
3232
};
3333

3434
struct list_lru_memcg {
35+
struct rcu_head rcu;
3536
/* array of per cgroup lists, indexed by memcg_cache_id */
3637
struct list_lru_one *lru[0];
3738
};
@@ -43,7 +44,7 @@ struct list_lru_node {
4344
struct list_lru_one lru;
4445
#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
4546
/* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
46-
struct list_lru_memcg *memcg_lrus;
47+
struct list_lru_memcg __rcu *memcg_lrus;
4748
#endif
4849
long nr_items;
4950
} ____cacheline_aligned_in_smp;

mm/list_lru.c

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ static inline bool list_lru_memcg_aware(struct list_lru *lru)
5252
static inline struct list_lru_one *
5353
list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
5454
{
55+
struct list_lru_memcg *memcg_lrus;
5556
/*
56-
* The lock protects the array of per cgroup lists from relocation
57-
* (see memcg_update_list_lru_node).
57+
* Either lock or RCU protects the array of per cgroup lists
58+
* from relocation (see memcg_update_list_lru_node).
5859
*/
59-
lockdep_assert_held(&nlru->lock);
60-
if (nlru->memcg_lrus && idx >= 0)
61-
return nlru->memcg_lrus->lru[idx];
62-
60+
memcg_lrus = rcu_dereference_check(nlru->memcg_lrus,
61+
lockdep_is_held(&nlru->lock));
62+
if (memcg_lrus && idx >= 0)
63+
return memcg_lrus->lru[idx];
6364
return &nlru->lru;
6465
}
6566

@@ -168,10 +169,10 @@ static unsigned long __list_lru_count_one(struct list_lru *lru,
168169
struct list_lru_one *l;
169170
unsigned long count;
170171

171-
spin_lock(&nlru->lock);
172+
rcu_read_lock();
172173
l = list_lru_from_memcg_idx(nlru, memcg_idx);
173174
count = l->nr_items;
174-
spin_unlock(&nlru->lock);
175+
rcu_read_unlock();
175176

176177
return count;
177178
}
@@ -324,24 +325,41 @@ static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
324325

325326
static int memcg_init_list_lru_node(struct list_lru_node *nlru)
326327
{
328+
struct list_lru_memcg *memcg_lrus;
327329
int size = memcg_nr_cache_ids;
328330

329-
nlru->memcg_lrus = kvmalloc(size * sizeof(void *), GFP_KERNEL);
330-
if (!nlru->memcg_lrus)
331+
memcg_lrus = kvmalloc(sizeof(*memcg_lrus) +
332+
size * sizeof(void *), GFP_KERNEL);
333+
if (!memcg_lrus)
331334
return -ENOMEM;
332335

333-
if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
334-
kvfree(nlru->memcg_lrus);
336+
if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
337+
kvfree(memcg_lrus);
335338
return -ENOMEM;
336339
}
340+
RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
337341

338342
return 0;
339343
}
340344

341345
static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
342346
{
343-
__memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
344-
kvfree(nlru->memcg_lrus);
347+
struct list_lru_memcg *memcg_lrus;
348+
/*
349+
* This is called when shrinker has already been unregistered,
350+
* and nobody can use it. So, there is no need to use kvfree_rcu().
351+
*/
352+
memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
353+
__memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
354+
kvfree(memcg_lrus);
355+
}
356+
357+
static void kvfree_rcu(struct rcu_head *head)
358+
{
359+
struct list_lru_memcg *mlru;
360+
361+
mlru = container_of(head, struct list_lru_memcg, rcu);
362+
kvfree(mlru);
345363
}
346364

347365
static int memcg_update_list_lru_node(struct list_lru_node *nlru,
@@ -351,8 +369,9 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
351369

352370
BUG_ON(old_size > new_size);
353371

354-
old = nlru->memcg_lrus;
355-
new = kvmalloc(new_size * sizeof(void *), GFP_KERNEL);
372+
old = rcu_dereference_protected(nlru->memcg_lrus,
373+
lockdep_is_held(&list_lrus_mutex));
374+
new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL);
356375
if (!new)
357376
return -ENOMEM;
358377

@@ -361,29 +380,33 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
361380
return -ENOMEM;
362381
}
363382

364-
memcpy(new, old, old_size * sizeof(void *));
383+
memcpy(&new->lru, &old->lru, old_size * sizeof(void *));
365384

366385
/*
367-
* The lock guarantees that we won't race with a reader
368-
* (see list_lru_from_memcg_idx).
386+
* The locking below allows readers that hold nlru->lock avoid taking
387+
* rcu_read_lock (see list_lru_from_memcg_idx).
369388
*
370389
* Since list_lru_{add,del} may be called under an IRQ-safe lock,
371390
* we have to use IRQ-safe primitives here to avoid deadlock.
372391
*/
373392
spin_lock_irq(&nlru->lock);
374-
nlru->memcg_lrus = new;
393+
rcu_assign_pointer(nlru->memcg_lrus, new);
375394
spin_unlock_irq(&nlru->lock);
376395

377-
kvfree(old);
396+
call_rcu(&old->rcu, kvfree_rcu);
378397
return 0;
379398
}
380399

381400
static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
382401
int old_size, int new_size)
383402
{
403+
struct list_lru_memcg *memcg_lrus;
404+
405+
memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
406+
lockdep_is_held(&list_lrus_mutex));
384407
/* do not bother shrinking the array back to the old size, because we
385408
* cannot handle allocation failures here */
386-
__memcg_destroy_list_lru_node(nlru->memcg_lrus, old_size, new_size);
409+
__memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
387410
}
388411

389412
static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)

0 commit comments

Comments
 (0)