memcg: add comments clarifying aspects of cache attribute propagation
authorGlauber Costa <glommer@parallels.com>
Tue, 18 Dec 2012 22:23:10 +0000 (14:23 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 18 Dec 2012 23:02:15 +0000 (15:02 -0800)
This patch clarifies two aspects of cache attribute propagation.

First, the expected context for the for_each_memcg_cache macro in
memcontrol.h.  The usages already in the codebase are safe.  In mm/slub.c,
it is trivially safe because the lock is acquired right before the loop.
In mm/slab.c, it is less so: the lock is acquired by an outer function a
few steps back in the stack, so a VM_BUG_ON() is added to make sure it is
indeed safe.

A comment is also added to detail why we are returning the value of the
parent cache and ignoring the children's when we propagate the attributes.

Signed-off-by: Glauber Costa <glommer@parallels.com>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Kamezawa Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/memcontrol.h
mm/slab.c
mm/slub.c

index ea02ff9708363a0e9893af1b8d668cc7cee86ec0..0108a56f814ed7559b2f1fcee41a903890ed919d 100644 (file)
@@ -422,6 +422,12 @@ static inline void sock_release_memcg(struct sock *sk)
 extern struct static_key memcg_kmem_enabled_key;
 
 extern int memcg_limited_groups_array_size;
+
+/*
+ * Helper macro to loop through all memcg-specific caches. Callers must still
+ * check if the cache is valid (it is either valid or NULL).
+ * the slab_mutex must be held when looping through those caches
+ */
 #define for_each_memcg_cache_index(_idx)       \
        for ((_idx) = 0; i < memcg_limited_groups_array_size; (_idx)++)
 
index 4dcbf96a77b47987f5a7ac4b94e5767ca1b288db..e7667a3584bc664f023356fc58d67baa30959e69 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -4099,6 +4099,7 @@ static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
        if ((ret < 0) || !is_root_cache(cachep))
                return ret;
 
+       VM_BUG_ON(!mutex_is_locked(&slab_mutex));
        for_each_memcg_cache_index(i) {
                c = cache_from_memcg(cachep, i);
                if (c)
index 21c94d9695ec9d5a74a8172badc6b6fa86450222..efe2cffc29b0d4e93a9ab5078766336f600dfd1a 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5108,12 +5108,25 @@ static ssize_t slab_attr_store(struct kobject *kobj,
                if (s->max_attr_size < len)
                        s->max_attr_size = len;
 
+               /*
+                * This is a best effort propagation, so this function's return
+                * value will be determined by the parent cache only. This is
+                * basically because not all attributes will have a well
+                * defined semantics for rollbacks - most of the actions will
+                * have permanent effects.
+                *
+                * Returning the error value of any of the children that fail
+                * is not 100 % defined, in the sense that users seeing the
+                * error code won't be able to know anything about the state of
+                * the cache.
+                *
+                * Only returning the error code for the parent cache at least
+                * has well defined semantics. The cache being written to
+                * directly either failed or succeeded, in which case we loop
+                * through the descendants with best-effort propagation.
+                */
                for_each_memcg_cache_index(i) {
                        struct kmem_cache *c = cache_from_memcg(s, i);
-                       /*
-                        * This function's return value is determined by the
-                        * parent cache only
-                        */
                        if (c)
                                attribute->store(c, buf, len);
                }