Skip to content

Commit 9eeadc8

Browse files
htejuntorvalds
authored andcommitted
slab: reorganize memcg_cache_params
We're going to change how memcg caches are iterated. In preparation, clean up and reorganize memcg_cache_params. * The shared ->list is replaced by ->children in root and ->children_node in children. * ->is_root_cache is removed. Instead ->root_cache is moved out of the child union and now used by both root and children. NULL indicates root cache. Non-NULL a memcg one. This patch doesn't cause any observable behavior changes. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Tejun Heo <[email protected]> Acked-by: Vladimir Davydov <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: David Rientjes <[email protected]> Cc: Joonsoo Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 657dc2f commit 9eeadc8

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

include/linux/slab.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -545,22 +545,37 @@ struct memcg_cache_array {
545545
* array to be accessed without taking any locks, on relocation we free the old
546546
* version only after a grace period.
547547
*
548-
* Child caches will hold extra metadata needed for its operation. Fields are:
548+
* Root and child caches hold different metadata.
549549
*
550-
* @memcg: pointer to the memcg this cache belongs to
551-
* @root_cache: pointer to the global, root cache, this cache was derived from
550+
* @root_cache: Common to root and child caches. NULL for root, pointer to
551+
* the root cache for children.
552552
*
553-
* Both root and child caches of the same kind are linked into a list chained
554-
* through @list.
553+
* The following fields are specific to root caches.
554+
*
555+
* @memcg_caches: kmemcg ID indexed table of child caches. This table is
556+
* used to index child cachces during allocation and cleared
557+
* early during shutdown.
558+
*
559+
* @children: List of all child caches. While the child caches are also
560+
* reachable through @memcg_caches, a child cache remains on
561+
* this list until it is actually destroyed.
562+
*
563+
* The following fields are specific to child caches.
564+
*
565+
* @memcg: Pointer to the memcg this cache belongs to.
566+
*
567+
* @children_node: List node for @root_cache->children list.
555568
*/
556569
struct memcg_cache_params {
557-
bool is_root_cache;
558-
struct list_head list;
570+
struct kmem_cache *root_cache;
559571
union {
560-
struct memcg_cache_array __rcu *memcg_caches;
572+
struct {
573+
struct memcg_cache_array __rcu *memcg_caches;
574+
struct list_head children;
575+
};
561576
struct {
562577
struct mem_cgroup *memcg;
563-
struct kmem_cache *root_cache;
578+
struct list_head children_node;
564579
};
565580
};
566581
};

mm/slab.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
206206
* slab_mutex.
207207
*/
208208
#define for_each_memcg_cache(iter, root) \
209-
list_for_each_entry(iter, &(root)->memcg_params.list, \
210-
memcg_params.list)
209+
list_for_each_entry(iter, &(root)->memcg_params.children, \
210+
memcg_params.children_node)
211211

212212
static inline bool is_root_cache(struct kmem_cache *s)
213213
{
214-
return s->memcg_params.is_root_cache;
214+
return !s->memcg_params.root_cache;
215215
}
216216

217217
static inline bool slab_equal_or_root(struct kmem_cache *s,

mm/slab_common.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,20 @@ int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
140140
#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
141141
void slab_init_memcg_params(struct kmem_cache *s)
142142
{
143-
s->memcg_params.is_root_cache = true;
144-
INIT_LIST_HEAD(&s->memcg_params.list);
143+
s->memcg_params.root_cache = NULL;
145144
RCU_INIT_POINTER(s->memcg_params.memcg_caches, NULL);
145+
INIT_LIST_HEAD(&s->memcg_params.children);
146146
}
147147

148148
static int init_memcg_params(struct kmem_cache *s,
149149
struct mem_cgroup *memcg, struct kmem_cache *root_cache)
150150
{
151151
struct memcg_cache_array *arr;
152152

153-
if (memcg) {
154-
s->memcg_params.is_root_cache = false;
155-
s->memcg_params.memcg = memcg;
153+
if (root_cache) {
156154
s->memcg_params.root_cache = root_cache;
155+
s->memcg_params.memcg = memcg;
156+
INIT_LIST_HEAD(&s->memcg_params.children_node);
157157
return 0;
158158
}
159159

@@ -223,7 +223,7 @@ int memcg_update_all_caches(int num_memcgs)
223223

224224
static void unlink_memcg_cache(struct kmem_cache *s)
225225
{
226-
list_del(&s->memcg_params.list);
226+
list_del(&s->memcg_params.children_node);
227227
}
228228
#else
229229
static inline int init_memcg_params(struct kmem_cache *s,
@@ -594,7 +594,8 @@ void memcg_create_kmem_cache(struct mem_cgroup *memcg,
594594
goto out_unlock;
595595
}
596596

597-
list_add(&s->memcg_params.list, &root_cache->memcg_params.list);
597+
list_add(&s->memcg_params.children_node,
598+
&root_cache->memcg_params.children);
598599

599600
/*
600601
* Since readers won't lock (see cache_from_memcg_idx()), we need a
@@ -690,7 +691,7 @@ static int shutdown_memcg_caches(struct kmem_cache *s)
690691
* list so as not to try to destroy it for a second
691692
* time while iterating over inactive caches below.
692693
*/
693-
list_move(&c->memcg_params.list, &busy);
694+
list_move(&c->memcg_params.children_node, &busy);
694695
else
695696
/*
696697
* The cache is empty and will be destroyed soon. Clear
@@ -705,17 +706,17 @@ static int shutdown_memcg_caches(struct kmem_cache *s)
705706
* Second, shutdown all caches left from memory cgroups that are now
706707
* offline.
707708
*/
708-
list_for_each_entry_safe(c, c2, &s->memcg_params.list,
709-
memcg_params.list)
709+
list_for_each_entry_safe(c, c2, &s->memcg_params.children,
710+
memcg_params.children_node)
710711
shutdown_cache(c);
711712

712-
list_splice(&busy, &s->memcg_params.list);
713+
list_splice(&busy, &s->memcg_params.children);
713714

714715
/*
715716
* A cache being destroyed must be empty. In particular, this means
716717
* that all per memcg caches attached to it must be empty too.
717718
*/
718-
if (!list_empty(&s->memcg_params.list))
719+
if (!list_empty(&s->memcg_params.children))
719720
return -EBUSY;
720721
return 0;
721722
}

0 commit comments

Comments
 (0)