Skip to content

Commit 60d3fd3

Browse files
Vladimir Davydovtorvalds
authored andcommitted
list_lru: introduce per-memcg lists
There are several FS shrinkers, including super_block::s_shrink, that keep reclaimable objects in the list_lru structure. Hence to turn them to memcg-aware shrinkers, it is enough to make list_lru per-memcg. This patch does the trick. It adds an array of lru lists to the list_lru_node structure (per-node part of the list_lru), one for each kmem-active memcg, and dispatches every item addition or removal to the list corresponding to the memcg which the item is accounted to. So now the list_lru structure is not just per node, but per node and per memcg. Not all list_lrus need this feature, so this patch also adds a new method, list_lru_init_memcg, which initializes a list_lru as memcg aware. Otherwise (i.e. if initialized with old list_lru_init), the list_lru won't have per memcg lists. Just like per memcg caches arrays, the arrays of per-memcg lists are indexed by memcg_cache_id, so we must grow them whenever memcg_nr_cache_ids is increased. So we introduce a callback, memcg_update_all_list_lrus, invoked by memcg_alloc_cache_id if the id space is full. The locking is implemented in a manner similar to lruvecs, i.e. we have one lock per node that protects all lists (both global and per cgroup) on the node. Signed-off-by: Vladimir Davydov <vdavydov@parallels.com> Cc: Dave Chinner <david@fromorbit.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@suse.cz> Cc: Greg Thelen <gthelen@google.com> Cc: Glauber Costa <glommer@gmail.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c0a5b56 commit 60d3fd3

4 files changed

Lines changed: 424 additions & 36 deletions

File tree

‎include/linux/list_lru.h‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/nodemask.h>
1212
#include <linux/shrinker.h>
1313

14+
struct mem_cgroup;
15+
1416
/* list_lru_walk_cb has to always return one of those */
1517
enum lru_status {
1618
LRU_REMOVED, /* item removed from list */
@@ -22,11 +24,26 @@ enum lru_status {
2224
internally, but has to return locked. */
2325
};
2426

25-
struct list_lru_node {
26-
spinlock_t lock;
27+
struct list_lru_one {
2728
struct list_head list;
2829
/* kept as signed so we can catch imbalance bugs */
2930
long nr_items;
31+
};
32+
33+
struct list_lru_memcg {
34+
/* array of per cgroup lists, indexed by memcg_cache_id */
35+
struct list_lru_one *lru[0];
36+
};
37+
38+
struct list_lru_node {
39+
/* protects all lists on the node, including per cgroup */
40+
spinlock_t lock;
41+
/* global list, used for the root cgroup in cgroup aware lrus */
42+
struct list_lru_one lru;
43+
#ifdef CONFIG_MEMCG_KMEM
44+
/* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
45+
struct list_lru_memcg *memcg_lrus;
46+
#endif
3047
} ____cacheline_aligned_in_smp;
3148

3249
struct list_lru {
@@ -37,11 +54,14 @@ struct list_lru {
3754
};
3855

3956
void list_lru_destroy(struct list_lru *lru);
40-
int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key);
41-
static inline int list_lru_init(struct list_lru *lru)
42-
{
43-
return list_lru_init_key(lru, NULL);
44-
}
57+
int __list_lru_init(struct list_lru *lru, bool memcg_aware,
58+
struct lock_class_key *key);
59+
60+
#define list_lru_init(lru) __list_lru_init((lru), false, NULL)
61+
#define list_lru_init_key(lru, key) __list_lru_init((lru), false, (key))
62+
#define list_lru_init_memcg(lru) __list_lru_init((lru), true, NULL)
63+
64+
int memcg_update_all_list_lrus(int num_memcgs);
4565

4666
/**
4767
* list_lru_add: add an element to the lru list's tail
@@ -75,20 +95,23 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item);
7595
bool list_lru_del(struct list_lru *lru, struct list_head *item);
7696

7797
/**
78-
* list_lru_count_node: return the number of objects currently held by @lru
98+
* list_lru_count_one: return the number of objects currently held by @lru
7999
* @lru: the lru pointer.
80100
* @nid: the node id to count from.
101+
* @memcg: the cgroup to count from.
81102
*
82103
* Always return a non-negative number, 0 for empty lists. There is no
83104
* guarantee that the list is not updated while the count is being computed.
84105
* Callers that want such a guarantee need to provide an outer lock.
85106
*/
107+
unsigned long list_lru_count_one(struct list_lru *lru,
108+
int nid, struct mem_cgroup *memcg);
86109
unsigned long list_lru_count_node(struct list_lru *lru, int nid);
87110

88111
static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
89112
struct shrink_control *sc)
90113
{
91-
return list_lru_count_node(lru, sc->nid);
114+
return list_lru_count_one(lru, sc->nid, sc->memcg);
92115
}
93116

94117
static inline unsigned long list_lru_count(struct list_lru *lru)
@@ -105,9 +128,10 @@ static inline unsigned long list_lru_count(struct list_lru *lru)
105128
typedef enum lru_status
106129
(*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg);
107130
/**
108-
* list_lru_walk_node: walk a list_lru, isolating and disposing freeable items.
131+
* list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
109132
* @lru: the lru pointer.
110133
* @nid: the node id to scan from.
134+
* @memcg: the cgroup to scan from.
111135
* @isolate: callback function that is resposible for deciding what to do with
112136
* the item currently being scanned
113137
* @cb_arg: opaque type that will be passed to @isolate
@@ -125,6 +149,10 @@ typedef enum lru_status
125149
*
126150
* Return value: the number of objects effectively removed from the LRU.
127151
*/
152+
unsigned long list_lru_walk_one(struct list_lru *lru,
153+
int nid, struct mem_cgroup *memcg,
154+
list_lru_walk_cb isolate, void *cb_arg,
155+
unsigned long *nr_to_walk);
128156
unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
129157
list_lru_walk_cb isolate, void *cb_arg,
130158
unsigned long *nr_to_walk);
@@ -133,8 +161,8 @@ static inline unsigned long
133161
list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
134162
list_lru_walk_cb isolate, void *cb_arg)
135163
{
136-
return list_lru_walk_node(lru, sc->nid, isolate, cb_arg,
137-
&sc->nr_to_scan);
164+
return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
165+
&sc->nr_to_scan);
138166
}
139167

140168
static inline unsigned long

‎include/linux/memcontrol.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ int memcg_cache_id(struct mem_cgroup *memcg);
439439
struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep);
440440
void __memcg_kmem_put_cache(struct kmem_cache *cachep);
441441

442+
struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr);
443+
442444
int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
443445
unsigned long nr_pages);
444446
void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages);
@@ -535,6 +537,13 @@ static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
535537
if (memcg_kmem_enabled())
536538
__memcg_kmem_put_cache(cachep);
537539
}
540+
541+
static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
542+
{
543+
if (!memcg_kmem_enabled())
544+
return NULL;
545+
return __mem_cgroup_from_kmem(ptr);
546+
}
538547
#else
539548
#define for_each_memcg_cache_index(_idx) \
540549
for (; NULL; )
@@ -586,6 +595,11 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
586595
static inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
587596
{
588597
}
598+
599+
static inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
600+
{
601+
return NULL;
602+
}
589603
#endif /* CONFIG_MEMCG_KMEM */
590604
#endif /* _LINUX_MEMCONTROL_H */
591605

0 commit comments

Comments
 (0)