slab accounting for memcg
Subsystems (6)
v4 cover slab accounting for memcg
This is a followup to the previous kmem series. I divided them logically so it gets easier for reviewers. But I believe they are ready to be merged together (although we can do a two-pass merge if people would prefer)
Throwaway git tree found at:
git://git.kernel.org/pub/scm/linux/kernel/git/glommer/memcg.git kmemcg-slab
I've bundled the following important changes since last submission:
* no more messing with the cache name after destruction: aggregated figures are shown in /proc/slabinfo.
* memory.kmem.slabinfo file with memcg-specific cache information during its lifespan.
* full slub attribute propagation.
* reusing the standard workqueue mechanism.
* cache-side indexing, instead of memcg-side indexing. The memcg css_id serves as an index, and we don't need extra indexes for that.
* struct memcg_cache_params no longer bundled in struct kmem_cache: We now will have only a pointer in the struct, allowing memory consumption when disable to fall down ever further.
Patches need to be adjusted to cope with those changes, but other than that, look the same - just a lot simpler.
I also put quite some effort to overcome my writing disability and get some decent changelogs in place.
For a detailed explanation about this whole effort, please refer to my previous post (https://lkml.org/lkml/2012/10/8/119)
*** BLURB HERE ***
Glauber Costa (19):
slab: Ignore internal flags in cache creation
move slabinfo processing to slab_common.c
move print_slabinfo_header to slab_common.c
sl[au]b: process slabinfo_show in common code
slab: don't preemptively remove element from list in cache destroy slab/slub: struct memcg_params
consider a memcg parameter in kmem_create_cache Allocate memory for memcg caches whenever a new memcg appears
memcg: infrastructure to match an allocation to the right cache
memcg: skip memcg kmem allocations in specified code regions sl[au]b: always get the cache from its page in kfree sl[au]b: Allocate objects from memcg cache
memcg: destroy memcg caches
memcg/sl[au]b Track all the memcg children of a kmem_cache. memcg/sl[au]b: shrink dead caches
Aggregate memcg cache values in slabinfo
slab: propagate tunables values
slub: slub-specific propagation changes.
Add slab-specific documentation about the kmem controller
Documentation/cgroups/memory.txt | 7 + include/linux/memcontrol.h | 88 ++++++ include/linux/sched.h | 1 + include/linux/slab.h | 47 +++ include/linux/slab_def.h | 3 + include/linux/slub_def.h | 19 +- init/Kconfig | 2 +- mm/memcontrol.c | 599 +++++++++++++++++++++++++++++++++++++-- mm/slab.c | 210 ++++++-------- mm/slab.h | 157 +++++++++- mm/slab_common.c | 224 ++++++++++++++- mm/slub.c | 193 ++++++++----- 12 files changed, 1311 insertions(+), 239 deletions(-)
Signature · 1 line
1.7.11.4
PATCH v4 1/19 slab: Ignore internal flags in cache creation
Some flags are used internally by the allocators for management purposes. One example of that is the CFLGS_OFF_SLAB flag that slab uses to mark that the metadata for that cache is stored outside of the slab.
No cache should ever pass those as a creation flags. We can just ignore this bit if it happens to be passed (such as when duplicating a cache in the kmem memcg patches).
Because such flags can vary from allocator to allocator, we allow them to make their own decisions on that, defining SLAB_AVAILABLE_FLAGS with all flags that are valid at creation time. Allocators that doesn't have any specific flag requirement should define that to mean all flags.
Common code will mask out all flags not belonging to that set.
[ v2: leave the mask out decision up to the allocators ] [ v3: define flags for all allocators ]
[ v4: move all definitions to slab.h ]
--- mm/slab.c | 22 ---------------------- mm/slab.h | 25 +++++++++++++++++++++++++ mm/slab_common.c | 7 +++++++ mm/slub.c | 3 --- 4 files changed, 32 insertions(+), 25 deletions(-)
mm/slab.c +0 -22
@@ -162,23 +162,6 @@*/ static bool pfmemalloc_active__read_mostly ; -/* Legal flag mask for kmem_cache_create(). */ -#if DEBUG -# define CREATE_MASK (SLAB_RED_ZONE | \ - SLAB_POISON | SLAB_HWCACHE_ALIGN | \ - SLAB_CACHE_DMA | \ - SLAB_STORE_USER | \ - SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ - SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \ - SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK) -#else -# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \ - SLAB_CACHE_DMA | \ - SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ - SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \ - SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK) -#endif -/* * kmem_bufctl_t: *
@@ -2385,11 +2368,6 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)if (flags &SLAB_DESTROY_BY_RCU )BUG_ON (flags &SLAB_POISON );#endif -/* -* Always checks flags, a caller might be expecting debug support which -* isn't available. -*/ -BUG_ON (flags & ~CREATE_MASK );/* * Check that size is in terms of words. This is needed to avoid
mm/slab.h +25 -0
@@ -45,6 +45,31 @@ static inline struct kmem_cache *__kmem_cache_alias(const char *name, size_t siz#endif +/* Legal flag mask for kmem_cache_create(), for various configurations */ +#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | SLAB_PANIC | \ + SLAB_DESTROY_BY_RCU | SLAB_DEBUG_OBJECTS ) + +#if defined(CONFIG_DEBUG_SLAB ) +#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER) +#elif defined(CONFIG_SLUB_DEBUG ) +#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ + SLAB_TRACE | SLAB_DEBUG_FREE) +#else +#define SLAB_DEBUG_FLAGS (0) +#endif + +#if defined(CONFIG_SLAB ) +#define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \ + SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | SLAB_NOTRACK) +#elif defined(CONFIG_SLUB ) +#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \ + SLAB_TEMPORARY | SLAB_NOTRACK) +#else +#define SLAB_CACHE_FLAGS (0) +#endif + +#define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS) +int __kmem_cache_shutdown (struct kmem_cache *);#endif
mm/slab_common.c +7 -0
@@ -107,6 +107,13 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t alignif (!kmem_cache_sanity_check (name, size) ==0 ) goto out_locked; +/* +* Some allocators will constraint the set of valid flags to a subset +* of all flags. We expect them to define CACHE_CREATE_MASK in this +* case, and we'll just provide them with a sanitized version of the +* passed flags. +*/ + flags &=CACHE_CREATE_MASK ; s =__kmem_cache_alias (name, size, align, flags, ctor);if (s)
mm/slub.c +0 -3
@@ -112,9 +112,6 @@* the fast path and disables lockless freelists. */ -#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ - SLAB_TRACE | SLAB_DEBUG_FREE) -static inline int kmem_cache_debug (struct kmem_cache *s) {#ifdef CONFIG_SLUB_DEBUG
Signature · 1 line
1.7.11.4
PATCH v4 2/19 move slabinfo processing to slab_common.c
In reply to Glauber CostaThis patch moves all the common machinery to slabinfo processing to slab_common.c. We can do better by noticing that the output is heavily common, and having the allocators to just provide finished information about this. But after this first step, this can be done easier.
--- mm/slab.c | 72 ++++++++++---------------------------------------------- mm/slab.h | 8 +++++++ mm/slab_common.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/slub.c | 51 ++++----------------------------------- 4 files changed, 96 insertions(+), 105 deletions(-)
mm/slab.c +13 -59
@@ -4263,7 +4263,7 @@ out:#ifdef CONFIG_SLABINFO -static void print_slabinfo_header (struct seq_file *m) +void print_slabinfo_header (struct seq_file *m) {/* * Output format version, so at least we can change it
@@ -4286,28 +4286,7 @@ static void print_slabinfo_header(struct seq_file *m)seq_putc (m,'\n' ); } -static void *s_start (struct seq_file *m,loff_t *pos) -{ -loff_t n = *pos; - -mutex_lock (&slab_mutex); -if (!n) -print_slabinfo_header (m); - -return seq_list_start (&slab_caches, *pos); -} - -static void *s_next (struct seq_file *m,void *p,loff_t *pos) -{ -return seq_list_next (p, &slab_caches, pos); -} - -static void s_stop (struct seq_file *m,void *p) -{ -mutex_unlock (&slab_mutex); -} - -static int s_show (struct seq_file *m,void *p) +int slabinfo_show (struct seq_file *m,void *p) { struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);struct slab *slabp;
@@ -4404,27 +4383,6 @@ static int s_show(struct seq_file *m, void *p)return 0 ; } -/* -* slabinfo_op - iterator that generates /proc/slabinfo -* -* Output layout: -* cache-name -* num-active-objs -* total-objs -* object size -* num-active-slabs -* total-slabs -* num-pages-per-slab -* + further values on SMP and with statistics enabled -*/ - -static const struct seq_operations slabinfo_op = { - .start = s_start, - .next = s_next, - .stop = s_stop, - .show = s_show, -}; -#define MAX_SLABINFO_WRITE 128/** * slabinfo_write - Tuning for the slab allocator
@@ -4433,7 +4391,7 @@ static const struct seq_operations slabinfo_op = {* @count: data length * @ppos: unused */ -static ssize_t slabinfo_write (struct file *file,const char __user *buffer, +ssize_t slabinfo_write (struct file *file,const char __user *buffer,size_t count,loff_t *ppos) {char kbuf[MAX_SLABINFO_WRITE +1 ], *tmp;
@@ -4476,19 +4434,6 @@ static ssize_t slabinfo_write(struct file *file, const char __user *buffer,return res; } -static int slabinfo_open (struct inode *inode,struct file *file) -{ -return seq_open (file, &slabinfo_op); -} - -static const struct file_operations proc_slabinfo_operations = { - .open = slabinfo_open, - .read = seq_read, - .write = slabinfo_write, - .llseek = seq_lseek, - .release = seq_release, -}; -#ifdef CONFIG_DEBUG_SLAB_LEAK static void *leaks_start (struct seq_file *m,loff_t *pos)
@@ -4617,6 +4562,16 @@ static int leaks_show(struct seq_file *m, void *p)return 0 ; } +static void *s_next (struct seq_file *m,void *p,loff_t *pos) +{ +return seq_list_next (p, &slab_caches, pos); +} + +static void s_stop (struct seq_file *m,void *p) +{ +mutex_unlock (&slab_mutex); +} +static const struct seq_operations slabstats_op = { .start = leaks_start, .next = s_next,
@@ -4651,7 +4606,6 @@ static const struct file_operations proc_slabstats_operations = {static int __init slab_proc_init (void ) { -proc_create ("slabinfo" ,S_IWUSR |S_IRUSR ,NULL ,&proc_slabinfo_operations);#ifdef CONFIG_DEBUG_SLAB_LEAK proc_create ("slab_allocators" ,0 ,NULL , &proc_slabstats_operations);#endif
mm/slab.h +8 -0
@@ -72,4 +72,12 @@ static inline struct kmem_cache *__kmem_cache_alias(const char *name, size_t sizint __kmem_cache_shutdown (struct kmem_cache *); +struct seq_file ; +struct file ; +void print_slabinfo_header (struct seq_file *m); + +int slabinfo_show (struct seq_file *m,void *p); + +ssize_t slabinfo_write (struct file *file,const char __user *buffer, +size_t count,loff_t *ppos);#endif
mm/slab_common.c +70 -0
79 lines not on this page · raw message
mm/slub.c +5 -46
69 lines not on this page · raw message
Signature · 1 line
1.7.11.4
PATCH v4 3/19 move print_slabinfo_header to slab_common.c
In reply to Glauber CostaThe header format is highly similar between slab and slub. The main difference lays in the fact that slab may optionally have statistics added here in case of CONFIG_SLAB_DEBUG, while the slub will stick them somewhere else.
By making sure that information conditionally lives inside a globally-visible CONFIG_DEBUG_SLAB switch, we can move the header printing to a common location.
--- mm/slab.c | 24 ------------------------ mm/slab.h | 2 -- mm/slab_common.c | 23 +++++++++++++++++++++++ mm/slub.c | 10 ---------- 4 files changed, 23 insertions(+), 36 deletions(-)
mm/slab.c +0 -24
@@ -4262,30 +4262,6 @@ out: }#ifdef CONFIG_SLABINFO - -void print_slabinfo_header (struct seq_file *m) -{ -/* -* Output format version, so at least we can change it -* without _too_ many complaints. -*/ -#if STATS -seq_puts (m,"slabinfo - version: 2.1 (statistics)\n" ); -#else -seq_puts (m,"slabinfo - version: 2.1\n" ); -#endif -seq_puts (m,"# name <active_objs> <num_objs> <objsize> " -"<objperslab> <pagesperslab>" ); -seq_puts (m," : tunables <limit> <batchcount> <sharedfactor>" ); -seq_puts (m," : slabdata <active_slabs> <num_slabs> <sharedavail>" ); -#if STATS -seq_puts (m," : globalstat <listallocs> <maxobjs> <grown> <reaped> " -"<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>" ); -seq_puts (m," : cpustat <allochit> <allocmiss> <freehit> <freemiss>" ); -#endif -seq_putc (m,'\n' ); -} -int slabinfo_show (struct seq_file *m,void *p) { struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
mm/slab.h +0 -2
@@ -74,8 +74,6 @@ int __kmem_cache_shutdown(struct kmem_cache *);struct seq_file ;struct file ; -void print_slabinfo_header (struct seq_file *m); -int slabinfo_show (struct seq_file *m,void *p); ssize_t slabinfo_write(struct file *file, const char __user *buffer,
mm/slab_common.c +23 -0
@@ -200,6 +200,29 @@ int slab_is_available(void) }#ifdef CONFIG_SLABINFO +static void print_slabinfo_header (struct seq_file *m) +{ +/* +* Output format version, so at least we can change it +* without _too_ many complaints. +*/ +#ifdef CONFIG_DEBUG_SLAB +seq_puts (m,"slabinfo - version: 2.1 (statistics)\n" ); +#else +seq_puts (m,"slabinfo - version: 2.1\n" ); +#endif +seq_puts (m,"# name <active_objs> <num_objs> <objsize> " +"<objperslab> <pagesperslab>" ); +seq_puts (m," : tunables <limit> <batchcount> <sharedfactor>" ); +seq_puts (m," : slabdata <active_slabs> <num_slabs> <sharedavail>" ); +#ifdef CONFIG_DEBUG_SLAB +seq_puts (m," : globalstat <listallocs> <maxobjs> <grown> <reaped> " +"<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>" ); +seq_puts (m," : cpustat <allochit> <allocmiss> <freehit> <freemiss>" ); +#endif +seq_putc (m,'\n' ); +} +static void *s_start (struct seq_file *m,loff_t *pos) {loff_t n = *pos;
mm/slub.c +0 -10
@@ -5394,16 +5394,6 @@ __initcall(slab_sysfs_init);* The /proc/slabinfo ABI */ #ifdef CONFIG_SLABINFO -void print_slabinfo_header (struct seq_file *m) -{ -seq_puts (m,"slabinfo - version: 2.1\n" ); -seq_puts (m,"# name <active_objs> <num_objs> <object_size> " -"<objperslab> <pagesperslab>" ); -seq_puts (m," : tunables <limit> <batchcount> <sharedfactor>" ); -seq_puts (m," : slabdata <active_slabs> <num_slabs> <sharedavail>" ); -seq_putc (m,'\n' ); -} -int slabinfo_show (struct seq_file *m,void *p) {unsigned long nr_partials =0 ;
Signature · 1 line
1.7.11.4
PATCH v4 4/19 sl[au]b: process slabinfo_show in common code
In reply to Glauber CostaWith all the infrastructure in place, we can now have slabinfo_show done from slab_common.c. A cache-specific function is called to grab information about the cache itself, since that is still heavily dependent on the implementation. But with the values produced by it, all the printing and handling is done from common code.
[ v2: moved objects_per_slab and cache_order to slabinfo ]
--- mm/slab.c | 26 +++++++++++++++----------- mm/slab.h | 16 +++++++++++++++- mm/slab_common.c | 18 +++++++++++++++++- mm/slub.c | 24 ++++++++++-------------- 4 files changed, 57 insertions(+), 27 deletions(-)
mm/slab.c +15 -11
@@ -4262,9 +4262,8 @@ out: }#ifdef CONFIG_SLABINFO -int slabinfo_show (struct seq_file *m,void *p) +void get_slabinfo (struct kmem_cache *cachep,struct slabinfo *sinfo) { - struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);struct slab *slabp;unsigned long active_objs;unsigned long num_objs;
@@ -4319,13 +4318,20 @@ int slabinfo_show(struct seq_file *m, void *p)if (error)printk (KERN_ERR "slab: cache %s error: %s\n" , name, error); -seq_printf (m,"%-17s %6lu %6lu %6u %4u %4d" , - name, active_objs, num_objs, cachep->size, - cachep->num, (1 << cachep->gfporder)); -seq_printf (m," : tunables %4u %4u %4u" , - cachep->limit, cachep->batchcount, cachep->shared); -seq_printf (m," : slabdata %6lu %6lu %6lu" , - active_slabs, num_slabs, shared_avail); + sinfo->active_objs = active_objs; + sinfo->num_objs = num_objs; + sinfo->active_slabs = active_slabs; + sinfo->num_slabs = num_slabs; + sinfo->shared_avail = shared_avail; + sinfo->limit = cachep->limit; + sinfo->batchcount = cachep->batchcount; + sinfo->shared = cachep->shared; + sinfo->objects_per_slab = cachep->num; + sinfo->cache_order = cachep->gfporder; +} + +void slabinfo_show_stats (struct seq_file *m,struct kmem_cache *cachep) +{#if STATS {/* list3 stats */ unsigned long high = cachep->high_mark;
@@ -4355,8 +4361,6 @@ int slabinfo_show(struct seq_file *m, void *p) allochit, allocmiss, freehit, freemiss); }#endif -seq_putc (m,'\n' ); -return 0 ; }#define MAX_SLABINFO_WRITE 128
mm/slab.h +15 -1
@@ -74,8 +74,22 @@ int __kmem_cache_shutdown(struct kmem_cache *);struct seq_file ;struct file ; -int slabinfo_show (struct seq_file *m,void *p); +struct slabinfo { +unsigned long active_objs; +unsigned long num_objs; +unsigned long active_slabs; +unsigned long num_slabs; +unsigned long shared_avail; +unsigned int limit; +unsigned int batchcount; +unsigned int shared; +unsigned int objects_per_slab; +unsigned int cache_order; +}; + +void get_slabinfo (struct kmem_cache *s,struct slabinfo *sinfo); +void slabinfo_show_stats (struct seq_file *m,struct kmem_cache *s);ssize_t slabinfo_write (struct file *file,const char __user *buffer,size_t count,loff_t *ppos);#endif
mm/slab_common.c +17 -1
@@ -246,7 +246,23 @@ static void s_stop(struct seq_file *m, void *p)static int s_show (struct seq_file *m,void *p) { -return slabinfo_show (m, p); + struct kmem_cache *s = list_entry(p, struct kmem_cache, list); +struct slabinfo sinfo; + +memset (&sinfo,0 ,sizeof (sinfo)); +get_slabinfo (s, &sinfo); + +seq_printf (m,"%-17s %6lu %6lu %6u %4u %4d" , + s->name, sinfo.active_objs, sinfo.num_objs, s->size, + sinfo.objects_per_slab, (1 << sinfo.cache_order)); + +seq_printf (m," : tunables %4u %4u %4u" , + sinfo.limit, sinfo.batchcount, sinfo.shared); +seq_printf (m," : slabdata %6lu %6lu %6lu" , + sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail); +slabinfo_show_stats (m, s); +seq_putc (m,'\n' ); +return 0 ; }/*
mm/slub.c +10 -14
@@ -5394,18 +5394,14 @@ __initcall(slab_sysfs_init);* The /proc/slabinfo ABI */ #ifdef CONFIG_SLABINFO -int slabinfo_show (struct seq_file *m,void *p) +void get_slabinfo (struct kmem_cache *s,struct slabinfo *sinfo) {unsigned long nr_partials =0 ;unsigned long nr_slabs =0 ; -unsigned long nr_inuse =0 ;unsigned long nr_objs =0 ;unsigned long nr_free =0 ; -struct kmem_cache *s;int node; - s = list_entry(p, struct kmem_cache, list); -for_each_online_node (node) {struct kmem_cache_node *n =get_node (s, node);
@@ -5418,16 +5414,16 @@ int slabinfo_show(struct seq_file *m, void *p) nr_free +=count_partial (n, count_free); } - nr_inuse = nr_objs - nr_free; + sinfo->active_objs = nr_objs - nr_free; + sinfo->num_objs = nr_objs; + sinfo->active_slabs = nr_slabs; + sinfo->num_slabs = nr_slabs; + sinfo->objects_per_slab =oo_objects (s->oo); + sinfo->cache_order =oo_order (s->oo); +} -seq_printf (m,"%-17s %6lu %6lu %6u %4u %4d" , s->name, nr_inuse, - nr_objs, s->size,oo_objects (s->oo), - (1 <<oo_order (s->oo))); -seq_printf (m," : tunables %4u %4u %4u" ,0 ,0 ,0 ); -seq_printf (m," : slabdata %6lu %6lu %6lu" , nr_slabs, nr_slabs, -0UL ); -seq_putc (m,'\n' ); -return 0 ; +void slabinfo_show_stats (struct seq_file *m,struct kmem_cache *s) +{ } ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Signature · 1 line
1.7.11.4
PATCH v4 5/19 slab: don't preemptively remove element from list in cache destroy
In reply to Glauber CostaAfter the slab/slub/slob merge, we are deleting the element from the slab_cache lists, and then if the destruction fail, we add it back again. This behavior was present in some caches, but not in others, if my memory doesn't fail me.
I, however, see no reason why we need to do so, since we are now locked during the whole deletion (which wasn't necessarily true before). I propose a simplification in which we delete it only when there is no more going back, so we don't need to add it again.
--- mm/slab_common.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/slab_common.c +2 -3
@@ -174,16 +174,15 @@ void kmem_cache_destroy(struct kmem_cache *s)mutex_lock (&slab_mutex); s->refcount--;if (!s->refcount) { -list_del (&s->list); -if (!__kmem_cache_shutdown (s)) {if (s->flags &SLAB_DESTROY_BY_RCU )rcu_barrier (); +list_del (&s->list); +kfree (s->name);kmem_cache_free (kmem_cache, s); }else { -list_add (&s->list, &slab_caches);printk (KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n" , s->name);dump_stack ();
Signature · 1 line
1.7.11.4
PATCH v4 6/19 slab/slub: struct memcg_params
In reply to Glauber CostaFor the kmem slab controller, we need to record some extra information in the kmem_cache structure.
--- include/linux/slab.h | 25 +++++++++++++++++++++++++ include/linux/slab_def.h | 3 +++ include/linux/slub_def.h | 3 +++ mm/slab.h | 13 +++++++++++++ 4 files changed, 44 insertions(+)
include/linux/slab.h +25 -0
@@ -177,6 +177,31 @@ unsigned int kmem_cache_size(struct kmem_cache *);#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)#endif +#include <linux/workqueue.h> +/* +* This is the main placeholder for memcg-related information in kmem caches. +* struct kmem_cache will hold a pointer to it, so the memory cost while +* disabled is 1 pointer. The runtime cost while enabled, gets bigger than it +* would otherwise be if that would be bundled in kmem_cache: we'll need an +* extra pointer chase. But the trade off clearly lays in favor of not +* penalizing non-users. +* +* Both the root cache and the child caches will have it. For the root cache, +* this will hold a dynamically allocated array large enough to hold +* information about the currently limited memcgs in the system. +* +* Child caches will hold extra metadata needed for its operation. Fields are: +* +* @memcg: pointer to the memcg this cache belongs to +*/ +struct memcg_cache_params { +bool is_root_cache; +union { +struct kmem_cache *memcg_caches[0 ]; +struct mem_cgroup *memcg; + }; +}; +/* * Common kmalloc functions provided by all allocators */
include/linux/slab_def.h +3 -0
@@ -81,6 +81,9 @@ struct kmem_cache {*/ int obj_offset;#endif /* CONFIG_DEBUG_SLAB */ +#ifdef CONFIG_MEMCG_KMEM +struct memcg_cache_params *memcg_params; +#endif /* 6) per-cpu/per-node data, touched during every alloc/free */ /*
include/linux/slub_def.h +3 -0
@@ -101,6 +101,9 @@ struct kmem_cache {#ifdef CONFIG_SYSFS struct kobject kobj;/* For sysfs */ #endif +#ifdef CONFIG_MEMCG_KMEM +struct memcg_cache_params *memcg_params; +#endif #ifdef CONFIG_NUMA /*
mm/slab.h +13 -0
@@ -92,4 +92,17 @@ void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);void slabinfo_show_stats (struct seq_file *m,struct kmem_cache *s);ssize_t slabinfo_write (struct file *file,const char __user *buffer,size_t count,loff_t *ppos); + +#ifdef CONFIG_MEMCG_KMEM +static inline bool is_root_cache (struct kmem_cache *s) +{ +return !s->memcg_params || s->memcg_params->is_root_cache; +} +#else +static inline bool is_root_cache (struct kmem_cache *s) +{ +return true; +} + +#endif #endif
Signature · 1 line
1.7.11.4
PATCH v4 7/19 consider a memcg parameter in kmem_create_cache
In reply to Glauber CostaAllow a memcg parameter to be passed during cache creation. When the slub allocator is being used, it will only merge caches that belong to the same memcg.
Default function is created as a wrapper, passing NULL to the memcg version. We only merge caches that belong to the same memcg.
A helper is provided, memcg_css_id: because slub needs a unique cache name for sysfs. Since this is visible, but not the canonical location for slab data, the cache name is not used, the css_id should suffice.
[ v2: moved to idr/ida instead of redoing the indexes ]
[ v3: moved call to ida_init away from cgroup creation to fix a bug ] [ v4: no longer using the index mechanism ]
--- include/linux/memcontrol.h | 22 +++++++++++++++++++++ include/linux/slab.h | 12 +++++++++++- mm/memcontrol.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ mm/slab.h | 23 ++++++++++++++++++---- mm/slab_common.c | 40 +++++++++++++++++++++++++++---------- mm/slub.c | 16 +++++++++++---- 6 files changed, 143 insertions(+), 19 deletions(-)
include/linux/memcontrol.h +22 -0
@@ -28,6 +28,7 @@ struct mem_cgroup;struct page_cgroup ;struct page ;struct mm_struct ; +struct kmem_cache ;/* Stats that can be updated by kernel. */ enum mem_cgroup_page_stat_item {
@@ -414,6 +415,11 @@ void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order);void __memcg_kmem_uncharge_page (struct page *page,int order); +int memcg_css_id (struct mem_cgroup *memcg); +int memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s); +void memcg_release_cache (struct kmem_cache *cachep); +void memcg_cache_list_add (struct mem_cgroup *memcg,struct kmem_cache *cachep); +/** * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. * @gfp: the gfp allocation flags.
@@ -506,6 +512,22 @@ static inline void memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) { } + +static inline int memcg_register_cache (struct mem_cgroup *memcg, +struct kmem_cache *s) +{ +return 0 ; +} + +static inline void memcg_release_cache (struct kmem_cache *cachep) +{ +} + +static inline void memcg_cache_list_add (struct mem_cgroup *memcg, +struct kmem_cache *s) +{ +BUG (); +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/slab.h +11 -1
@@ -116,6 +116,7 @@ struct kmem_cache { };#endif +struct mem_cgroup ;/* * struct kmem_cache related prototypes */
@@ -125,6 +126,9 @@ int slab_is_available(void);struct kmem_cache *kmem_cache_create (const char *,size_t ,size_t ,unsigned long ,void (*)(void *)); +struct kmem_cache * +kmem_cache_create_memcg (struct mem_cgroup *,const char *,size_t ,size_t , +unsigned long ,void (*)(void *));void kmem_cache_destroy (struct kmem_cache *);int kmem_cache_shrink (struct kmem_cache *);void kmem_cache_free (struct kmem_cache *,void *);
@@ -193,15 +197,21 @@ unsigned int kmem_cache_size(struct kmem_cache *);* Child caches will hold extra metadata needed for its operation. Fields are: * * @memcg: pointer to the memcg this cache belongs to +* @root_cache: pointer to the global, root cache, this cache was derived from */ struct memcg_cache_params {bool is_root_cache;union {struct kmem_cache *memcg_caches[0 ]; -struct mem_cgroup *memcg; +struct { +struct mem_cgroup *memcg; +struct kmem_cache *root_cache; + }; }; }; +int memcg_update_all_caches (int num_memcgs); +/* * Common kmalloc functions provided by all allocators */
mm/memcontrol.c +49 -0
61 lines not on this page · raw message
mm/slab.h +19 -4
@@ -35,12 +35,15 @@ extern struct kmem_cache *kmem_cache;/* Functions provided by the slab allocators */ extern int __kmem_cache_create (struct kmem_cache *,unsigned long flags); +struct mem_cgroup ;#ifdef CONFIG_SLUB -struct kmem_cache *__kmem_cache_alias (const char *name,size_t size, -size_t align,unsigned long flags,void (*ctor)(void *)); +struct kmem_cache * +__kmem_cache_alias (struct mem_cgroup *memcg,const char *name,size_t size, +size_t align,unsigned long flags,void (*ctor)(void *));#else -static inline struct kmem_cache *__kmem_cache_alias (const char *name,size_t size, -size_t align,unsigned long flags,void (*ctor)(void *)) +static inline struct kmem_cache * +__kmem_cache_alias (struct mem_cgroup *memcg,const char *name,size_t size, +size_t align,unsigned long flags,void (*ctor)(void *)) {return NULL ; }#endif
@@ -98,11 +101,23 @@ static inline bool is_root_cache(struct kmem_cache *s) {return !s->memcg_params || s->memcg_params->is_root_cache; } + +static inline bool cache_match_memcg (struct kmem_cache *cachep, +struct mem_cgroup *memcg) +{ +return (is_root_cache (cachep) && !memcg) || + (cachep->memcg_params->memcg == memcg); +}#else static inline bool is_root_cache (struct kmem_cache *s) {return true; } +static inline bool cache_match_memcg (struct kmem_cache *cachep, +struct mem_cgroup *memcg) +{ +return true; +}#endif #endif
mm/slab_common.c +30 -10
@@ -18,6 +18,7 @@#include <asm/cacheflush.h> #include <asm/tlbflush.h> #include <asm/page.h> +#include <linux/memcontrol.h> #include "slab.h"
@@ -27,7 +28,8 @@ DEFINE_MUTEX(slab_mutex);struct kmem_cache *kmem_cache;#ifdef CONFIG_DEBUG_VM -static int kmem_cache_sanity_check (const char *name,size_t size) +static int kmem_cache_sanity_check (struct mem_cgroup *memcg,const char *name, +size_t size) {struct kmem_cache *s =NULL ;
@@ -53,7 +55,7 @@ static int kmem_cache_sanity_check(const char *name, size_t size)continue ; } -if (!strcmp (s->name, name)) { +if (cache_match_memcg (s, memcg) && !strcmp (s->name, name)) {pr_err ("%s (%s): Cache name already exists.\n" , __func__, name);dump_stack ();
@@ -66,7 +68,8 @@ static int kmem_cache_sanity_check(const char *name, size_t size) return 0; }#else -static inline int kmem_cache_sanity_check (const char *name,size_t size) +static inline int kmem_cache_sanity_check (struct mem_cgroup *memcg, +const char *name,size_t size) {return 0 ; }
@@ -97,8 +100,9 @@ static inline int kmem_cache_sanity_check(const char *name, size_t size)* as davem. */ -struct kmem_cache *kmem_cache_create (const char *name,size_t size,size_t align, -unsigned long flags,void (*ctor)(void *)) +struct kmem_cache * +kmem_cache_create_memcg (struct mem_cgroup *memcg,const char *name,size_t size, +size_t align,unsigned long flags,void (*ctor)(void *)) {struct kmem_cache *s =NULL ;int err =0 ;
@@ -106,7 +110,7 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t alignget_online_cpus ();mutex_lock (&slab_mutex); -if (!kmem_cache_sanity_check (name, size) ==0 ) +if (!kmem_cache_sanity_check (memcg, name, size) ==0 ) goto out_locked;/*
@@ -117,7 +121,7 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align*/ flags &=CACHE_CREATE_MASK ; - s =__kmem_cache_alias (name, size, align, flags, ctor); + s =__kmem_cache_alias (memcg, name, size, align, flags, ctor);if (s) goto out_locked;
@@ -126,6 +130,13 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align s->object_size = s->size = size; s->align = align; s->ctor = ctor; + +if (memcg_register_cache (memcg, s)) { +kmem_cache_free (kmem_cache, s); + err = -ENOMEM ; + goto out_locked; + } + s->name =kstrdup (name,GFP_KERNEL );if (!s->name) {kmem_cache_free (kmem_cache, s);
@@ -135,10 +146,11 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align err =__kmem_cache_create (s, flags);if (!err) { - s->refcount =1 ; -list_add (&s->list, &slab_caches); - +if (!memcg) +list_add (&s->list, &slab_caches); +else +memcg_cache_list_add (memcg, s); }else {kfree (s->name);kmem_cache_free (kmem_cache, s);
@@ -166,6 +178,13 @@ out_locked:return s; } + +struct kmem_cache * +kmem_cache_create (const char *name,size_t size,size_t align, +unsigned long flags,void (*ctor)(void *)) +{ +return kmem_cache_create_memcg (NULL , name, size, align, flags, ctor); +}EXPORT_SYMBOL (kmem_cache_create);void kmem_cache_destroy (struct kmem_cache *s)
@@ -180,6 +199,7 @@ void kmem_cache_destroy(struct kmem_cache *s)list_del (&s->list); +memcg_release_cache (s);kfree (s->name);kmem_cache_free (kmem_cache, s); }else {
mm/slub.c +12 -4
@@ -31,6 +31,7 @@#include <linux/fault-inject.h> #include <linux/stacktrace.h> #include <linux/prefetch.h> +#include <linux/memcontrol.h> #include <trace/events/kmem.h>
@@ -3880,7 +3881,7 @@ static int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static struct kmem_cache *find_mergeable (size_t size, +static struct kmem_cache *find_mergeable (struct mem_cgroup *memcg,size_t size,size_t align,unsigned long flags,const char *name,void (*ctor)(void *)) {
@@ -3916,17 +3917,20 @@ static struct kmem_cache *find_mergeable(size_t size,if (s->size - size >=sizeof (void *))continue ; +if (!cache_match_memcg (s, memcg)) +continue ;return s; }return NULL ; } -struct kmem_cache *__kmem_cache_alias (const char *name,size_t size, -size_t align,unsigned long flags,void (*ctor)(void *)) +struct kmem_cache * +__kmem_cache_alias (struct mem_cgroup *memcg,const char *name,size_t size, +size_t align,unsigned long flags,void (*ctor)(void *)) {struct kmem_cache *s; - s =find_mergeable (size, align, flags, name, ctor); + s =find_mergeable (memcg, size, align, flags, name, ctor);if (s) { s->refcount++;/*
@@ -5246,6 +5250,10 @@ static char *create_unique_id(struct kmem_cache *s)if (p != name +1 ) *p++ ='-' ; p +=sprintf (p,"%07d" , s->size); +#ifdef CONFIG_MEMCG_KMEM +if (!is_root_cache (s)) + p +=sprintf (p,"-%08d" ,memcg_css_id (s->memcg_params->memcg)); +#endif BUG_ON (p > name +ID_STR_LENGTH -1 );return name; }
Signature · 1 line
1.7.11.4
PATCH v4 8/19 Allocate memory for memcg caches whenever a new memcg appears
In reply to Glauber CostaEvery cache that is considered a root cache (basically the "original" caches, tied to the root memcg/no-memcg) will have an array that should be large enough to store a cache pointer per each memcg in the system.
Theoreticaly, this is as high as 1 << sizeof(css_id), which is currently in the 64k pointers range. Most of the time, we won't be using that much.
What goes in this patch, is a simple scheme to dynamically allocate such an array, in order to minimize memory usage for memcg caches. Because we would also like to avoid allocations all the time, at least for now, the array will only grow. It will tend to be big enough to hold the maximum number of kmem-limited memcgs ever achieved.
We'll allocate it to be a minimum of 64 kmem-limited memcgs. When we have more than that, we'll start doubling the size of this array every time the limit is reached.
Because we are only considering kmem limited memcgs, a natural point for this to happen is when we write to the limit. At that point, we already have set_limit_mutex held, so that will become our natural synchronization mechanism.
--- include/linux/memcontrol.h | 2 + mm/memcontrol.c | 171 ++++++++++++++++++++++++++++++++++++++++----- mm/slab_common.c | 25 +++++++ 3 files changed, 181 insertions(+), 17 deletions(-)
include/linux/memcontrol.h +2 -0
@@ -420,6 +420,8 @@ int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s);void memcg_release_cache (struct kmem_cache *cachep);void memcg_cache_list_add (struct mem_cgroup *memcg,struct kmem_cache *cachep); +int memcg_update_cache_size (struct kmem_cache *s,int num_groups); +void memcg_update_array_size (int num_groups);/** * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. * @gfp: the gfp allocation flags.
mm/memcontrol.c +154 -17
223 lines not on this page · raw message
mm/slab_common.c +25 -0
@@ -75,6 +75,31 @@ static inline int kmem_cache_sanity_check(struct mem_cgroup *memcg, }#endif +#ifdef CONFIG_MEMCG_KMEM +int memcg_update_all_caches (int num_memcgs) +{ +struct kmem_cache *s; +int ret =0 ; +mutex_lock (&slab_mutex); + +list_for_each_entry (s, &slab_caches, list) { + ret =memcg_update_cache_size (s, num_memcgs); +/* +* See comment in memcontrol.c, memcg_update_cache_size: +* Instead of freeing the memory, we'll just leave the caches +* up to this point in an updated state. +*/ +if (ret) + goto out; + } + +memcg_update_array_size (num_memcgs); +out: +mutex_unlock (&slab_mutex); +return ret; +} +#endif +/* * kmem_cache_create - Create a cache. * @name: A string which is used in /proc/slabinfo to identify this cache.
Signature · 1 line
1.7.11.4
PATCH v4 9/19 memcg: infrastructure to match an allocation to the right cache
In reply to Glauber CostaThe page allocator is able to bind a page to a memcg when it is allocated. But for the caches, we'd like to have as many objects as possible in a page belonging to the same cache.
This is done in this patch by calling memcg_kmem_get_cache in the beginning of every allocation function. This routing is patched out by static branches when kernel memory controller is not being used.
It assumes that the task allocating, which determines the memcg in the page allocator, belongs to the same cgroup throughout the whole process. Misacounting can happen if the task calls memcg_kmem_get_cache() while belonging to a cgroup, and later on changes. This is considered acceptable, and should only happen upon task migration.
Before the cache is created by the memcg core, there is also a possible
imbalance: the task belongs to a memcg, but the cache being allocated from is the global cache, since the child cache is not yet guaranteed to be ready. This case is also fine, since in this case the GFP_KMEMCG will not be passed and the page allocator will not attempt any cgroup accounting.
[ v4: use a standard workqueue mechanism, create right away if
possible, index from cache side ]
--- include/linux/memcontrol.h | 42 ++++++++++ init/Kconfig | 2 +- mm/memcontrol.c | 188 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+), 1 deletion(-)
include/linux/memcontrol.h +42 -0
@@ -422,6 +422,10 @@ void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);int memcg_update_cache_size (struct kmem_cache *s,int num_groups);void memcg_update_array_size (int num_groups); + +struct kmem_cache * +__memcg_kmem_get_cache (struct kmem_cache *cachep,gfp_t gfp); +/** * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. * @gfp: the gfp allocation flags.
@@ -492,6 +496,38 @@ memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) __memcg_kmem_commit_charge(page, memcg, order); } +/** +* memcg_kmem_get_cache: selects the correct per-memcg cache for allocation +* @cachep: the original global kmem cache +* @gfp: allocation flags. +* +* This function assumes that the task allocating, which determines the memcg +* in the page allocator, belongs to the same cgroup throughout the whole +* process. Misacounting can happen if the task calls memcg_kmem_get_cache() +* while belonging to a cgroup, and later on changes. This is considered +* acceptable, and should only happen upon task migration. +* +* Before the cache is created by the memcg core, there is also a possible +* imbalance: the task belongs to a memcg, but the cache being allocated from +* is the global cache, since the child cache is not yet guaranteed to be +* ready. This case is also fine, since in this case the GFP_KMEMCG will not be +* passed and the page allocator will not attempt any cgroup accounting. +*/ +static __always_inline struct kmem_cache * +memcg_kmem_get_cache (struct kmem_cache *cachep,gfp_t gfp) +{ +if (!memcg_kmem_enabled ()) +return cachep; +if (gfp & __GFP_NOFAIL) +return cachep; +if (in_interrupt () || (!current->mm) || (current->flags &PF_KTHREAD )) +return cachep; +if (unlikely (test_thread_flag (TIF_MEMDIE ) + ||fatal_signal_pending (current))) +return cachep; + +return __memcg_kmem_get_cache (cachep, gfp); +}#else static inline void sock_update_memcg (struct sock *sk) {
@@ -530,6 +566,12 @@ static inline void memcg_cache_list_add(struct mem_cgroup *memcg, {BUG (); } + +static inline struct kmem_cache * +memcg_kmem_get_cache (struct kmem_cache *cachep,gfp_t gfp) +{ +return cachep; +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
init/Kconfig +1 -1
@@ -741,7 +741,7 @@ config MEMCG_SWAP_ENABLED then swapaccount=0 does the trick). config MEMCG_KMEM bool "Memory Resource Controller Kernel Memory accounting (EXPERIMENTAL)" - depends on MEMCG && EXPERIMENTAL + depends on MEMCG && EXPERIMENTAL && !SLOB default n help The Kernel Memory extension for Memory Resource Controller can limit
mm/memcontrol.c +188 -0
204 lines not on this page · raw message
Signature · 1 line
1.7.11.4
PATCH v4 10/19 memcg: skip memcg kmem allocations in specified code regions
In reply to Glauber CostaThis patch creates a mechanism that skip memcg allocations during certain pieces of our core code. It basically works in the same way as preempt_disable()/preempt_enable(): By marking a region under which all allocations will be accounted to the root memcg.
We need this to prevent races in early cache creation, when we allocate data using caches that are not necessarily created already.
[ v2: wrap the whole enqueue process, INIT_WORK can alloc memory ]
--- include/linux/sched.h | 1 + mm/memcontrol.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-)
include/linux/sched.h +1 -0
@@ -1581,6 +1581,7 @@ struct task_struct {unsigned long nr_pages;/* uncharged usage */ unsigned long memsw_nr_pages; /* uncharged mem+swap usage */ } memcg_batch; +unsigned int memcg_kmem_skip_account;#endif #ifdef CONFIG_HAVE_HW_BREAKPOINT atomic_t ptrace_bp_refcnt;
mm/memcontrol.c +61 -2
94 lines not on this page · raw message
Signature · 1 line
1.7.11.4
PATCH v4 11/19 sl[au]b: always get the cache from its page in kfree
In reply to Glauber Costastruct page already have this information. If we start chaining caches, this information will always be more trustworthy than whatever is passed into the function
A parent pointer is added to the slub structure, so we can make sure the freeing comes from either the right slab, or from its rightful parent.
[ v3: added parent testing with VM_BUG_ON ]
[ v4: make it faster when kmemcg not in use ]
--- include/linux/memcontrol.h | 4 ++++ mm/slab.c | 17 ++++++++++++++++- mm/slab.h | 13 +++++++++++++ mm/slub.c | 14 ++++++++++++-- 4 files changed, 45 insertions(+), 3 deletions(-)
include/linux/memcontrol.h +4 -0
@@ -536,6 +536,10 @@ static inline void sock_release_memcg(struct sock *sk) { } +static inline bool memcg_kmem_enabled (void ) +{ +return false; +}static inline bool memcg_kmem_newpage_charge (gfp_t gfp,struct mem_cgroup **memcg,int order) {
mm/slab.c +16 -1
@@ -3911,9 +3911,24 @@ EXPORT_SYMBOL(__kmalloc);* Free an object which was previously allocated from this * cache. */ -void kmem_cache_free (struct kmem_cache *cachep,void *objp) +void kmem_cache_free (struct kmem_cache *s,void *objp) {unsigned long flags; +struct kmem_cache *cachep; + +/* +* When kmemcg is not being used, both assignments should return the +* same value. but we don't want to pay the assignment price in that +* case. If it is not compiled in, the compiler should be smart enough +* to not do even the assignment. In that case, slab_equal_or_root +* will also be a constant. +*/ +if (memcg_kmem_enabled ()) { + cachep =virt_to_cache (objp); +VM_BUG_ON (!slab_equal_or_root (cachep, s)); + }else + cachep = s; +local_irq_save (flags);debug_check_no_locks_freed (objp, cachep->object_size);
mm/slab.h +13 -0
@@ -108,6 +108,13 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep,return (is_root_cache (cachep) && !memcg) || (cachep->memcg_params->memcg == memcg); } + +static inline bool slab_equal_or_root (struct kmem_cache *s, +struct kmem_cache *p) +{ +return (p == s) || + (s->memcg_params && (p == s->memcg_params->root_cache)); +}#else static inline bool is_root_cache (struct kmem_cache *s) {
@@ -119,5 +126,11 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep, {return true; } + +static inline bool slab_equal_or_root (struct kmem_cache *s, +struct kmem_cache *p) +{ +return true; +}#endif #endif
mm/slub.c +12 -2
@@ -2609,9 +2609,19 @@ redo:void kmem_cache_free (struct kmem_cache *s,void *x) { -struct page *page; +struct page *page =virt_to_head_page (x); - page =virt_to_head_page (x); +/* +* When kmemcg is not being used, both assignments should return the +* same value. but we don't want to pay the assignment price in that +* case. If it is not compiled in, the compiler should be smart enough +* to not do even the assignment. In that case, slab_equal_or_root +* will also be a constant. +*/ +if (memcg_kmem_enabled ()) { +VM_BUG_ON (!slab_equal_or_root (page->slab, s)); + s = page->slab; + }if (kmem_cache_debug (s) && page->slab != s) { pr_err("kmem_cache_free: Wrong slab cache. %s but object"
Signature · 1 line
1.7.11.4
PATCH v4 12/19 sl[au]b: Allocate objects from memcg cache
In reply to Glauber CostaWe are able to match a cache allocation to a particular memcg. If the task doesn't change groups during the allocation itself - a rare event, this will give us a good picture about who is the first group to touch a cache page.
This patch uses the now available infrastructure by calling memcg_kmem_get_cache() before all the cache allocations.
--- include/linux/slub_def.h | 15 ++++++++++----- mm/memcontrol.c | 3 +++ mm/slab.c | 6 +++++- mm/slub.c | 5 +++-- 4 files changed, 21 insertions(+), 8 deletions(-)
include/linux/slub_def.h +10 -5
@@ -13,6 +13,8 @@#include <linux/kobject.h> #include <linux/kmemleak.h> +#include <linux/memcontrol.h> +#include <linux/mm.h> enum stat_item {ALLOC_FASTPATH ,/* Allocation from cpu slab */
@@ -209,14 +211,14 @@ static __always_inline int kmalloc_index(size_t size)* This ought to end up with a global pointer to the right cache * in kmalloc_caches. */ -static __always_inline struct kmem_cache *kmalloc_slab(size_t size) +static __always_inline struct kmem_cache *kmalloc_slab(gfp_t flags, size_t size) {int index =kmalloc_index (size);if (index ==0 )return NULL ; -return kmalloc_caches[index]; +return memcg_kmem_get_cache (kmalloc_caches[index], flags); }void *kmem_cache_alloc (struct kmem_cache *,gfp_t );
@@ -225,7 +227,10 @@ void *__kmalloc(size_t size, gfp_t flags); static __always_inline void *kmalloc_order (size_t size,gfp_t flags,unsigned int order) { -void *ret = (void *)__get_free_pages (flags | __GFP_COMP, order); +void *ret; + + flags |= (__GFP_COMP | __GFP_KMEMCG); + ret = (void *)__get_free_pages (flags, order);kmemleak_alloc (ret, size,1 , flags);return ret; }
@@ -274,7 +279,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags)return kmalloc_large (size, flags);if (!(flags &SLUB_DMA )) { -struct kmem_cache *s =kmalloc_slab (size); +struct kmem_cache *s =kmalloc_slab (flags, size);if (!s)return ZERO_SIZE_PTR ;
@@ -307,7 +312,7 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) {if (__builtin_constant_p (size) && size <=SLUB_MAX_SIZE && !(flags &SLUB_DMA )) { -struct kmem_cache *s =kmalloc_slab (size); +struct kmem_cache *s =kmalloc_slab (flags, size);if (!s)return ZERO_SIZE_PTR ;
mm/memcontrol.c +3 -0
@@ -3014,6 +3014,9 @@ static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg, new =kmem_cache_create_memcg (memcg, name, s->object_size, s->align, (s->flags & ~SLAB_PANIC ), s->ctor); +if (new) + new->allocflags |= __GFP_KMEMCG; +kfree (name);return new; }
mm/slab.c +5 -1
@@ -1949,7 +1949,7 @@ static void kmem_freepages(struct kmem_cache *cachep, void *addr) }if (current->reclaim_state) current->reclaim_state->reclaimed_slab += nr_freed; -free_pages ((unsigned long )addr, cachep->gfporder); +free_accounted_pages ((unsigned long )addr, cachep->gfporder); }static void kmem_rcu_free (struct rcu_head *head)
@@ -3514,6 +3514,8 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (slab_should_failslab (cachep, flags))return NULL ; + cachep =memcg_kmem_get_cache (cachep, flags); +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags);
@@ -3599,6 +3601,8 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)if (slab_should_failslab (cachep, flags))return NULL ; + cachep =memcg_kmem_get_cache (cachep, flags); +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags); objp =__do_cache_alloc (cachep, flags);
mm/slub.c +3 -2
@@ -1405,7 +1405,7 @@ static void __free_slab(struct kmem_cache *s, struct page *page)reset_page_mapcount (page);if (current->reclaim_state) current->reclaim_state->reclaimed_slab += pages; -__free_pages (page, order); +__free_accounted_pages (page, order); }#define need_reserve_slab_rcu \
@@ -2321,6 +2321,7 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,if (slab_pre_alloc_hook (s, gfpflags))return NULL ; + s =memcg_kmem_get_cache (s, gfpflags); redo:/*
@@ -3478,7 +3479,7 @@ void kfree(const void *x)if (unlikely (!PageSlab (page))) {BUG_ON (!PageCompound (page));kmemleak_free (x); -__free_pages (page,compound_order (page)); +__free_accounted_pages (page,compound_order (page));return ; }slab_free (page->slab, page, object, _RET_IP_);
Signature · 1 line
1.7.11.4
PATCH v4 13/19 memcg: destroy memcg caches
In reply to Glauber CostaThis patch implements destruction of memcg caches. Right now, only caches where our reference counter is the last remaining are deleted. If there are any other reference counters around, we just leave the caches lying around until they go away.
When that happen, a destruction function is called from the cache code. Caches are only destroyed in process context, so we queue them up for later processing in the general case.
--- include/linux/memcontrol.h | 2 ++ include/linux/slab.h | 9 +++++++++ mm/memcontrol.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ mm/slab.c | 3 +++ mm/slab.h | 24 +++++++++++++++++++++++ mm/slub.c | 7 ++++++- 6 files changed, 91 insertions(+), 1 deletion(-)
include/linux/memcontrol.h +2 -0
@@ -426,6 +426,8 @@ void memcg_update_array_size(int num_groups);struct kmem_cache *__memcg_kmem_get_cache (struct kmem_cache *cachep,gfp_t gfp); +void mem_cgroup_destroy_cache (struct kmem_cache *cachep); +/** * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. * @gfp: the gfp allocation flags.
include/linux/slab.h +9 -0
@@ -198,6 +198,11 @@ unsigned int kmem_cache_size(struct kmem_cache *);* * @memcg: pointer to the memcg this cache belongs to * @root_cache: pointer to the global, root cache, this cache was derived from +* @cachep: backpointer to the kmem_cache structure that hold us. +* @dead: set to true after the memcg dies; the cache may still be around. +* @nr_pages: number of pages that belongs to this cache. +* @destroy: worker to be called whenever we are ready, or believe we may be +* ready, to destroy this cache. */ struct memcg_cache_params {bool is_root_cache;
@@ -206,6 +211,10 @@ struct memcg_cache_params {struct {struct mem_cgroup *memcg;struct kmem_cache *root_cache; +struct kmem_cache *cachep; +bool dead; +atomic_t nr_pages; +struct work_struct destroy; }; }; };
mm/memcontrol.c +47 -0
77 lines not on this page · raw message
mm/slab.c +3 -0
@@ -1911,6 +1911,7 @@ static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)if (page->pfmemalloc)SetPageSlabPfmemalloc (page + i); } +memcg_bind_pages (cachep, cachep->gfporder);if (kmemcheck_enabled && !(cachep->flags &SLAB_NOTRACK )) {kmemcheck_alloc_shadow (page, cachep->gfporder, flags, nodeid);
@@ -1947,6 +1948,8 @@ static void kmem_freepages(struct kmem_cache *cachep, void *addr)__ClearPageSlab (page); page++; } + +memcg_release_pages (cachep, cachep->gfporder);if (current->reclaim_state) current->reclaim_state->reclaimed_slab += nr_freed;free_accounted_pages ((unsigned long )addr, cachep->gfporder);
mm/slab.h +24 -0
@@ -1,5 +1,6 @@#ifndef MM_SLAB_H #define MM_SLAB_H +#include <linux/memcontrol.h> /* * Internal slab definitions */
@@ -109,6 +110,21 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep, (cachep->memcg_params->memcg == memcg); } +static inline void memcg_bind_pages (struct kmem_cache *s,int order) +{ +if (!is_root_cache (s)) +atomic_add (1 << order, &s->memcg_params->nr_pages); +} + +static inline void memcg_release_pages (struct kmem_cache *s,int order) +{ +if (is_root_cache (s)) +return ; + +if (atomic_sub_and_test ((1 << order), &s->memcg_params->nr_pages)) +mem_cgroup_destroy_cache (s); +} +static inline bool slab_equal_or_root (struct kmem_cache *s,struct kmem_cache *p) {
@@ -127,6 +143,14 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep,return true; } +static inline void memcg_bind_pages (struct kmem_cache *s,int order) +{ +} + +static inline void memcg_release_pages (struct kmem_cache *s,int order) +{ +} +static inline bool slab_equal_or_root (struct kmem_cache *s,struct kmem_cache *p) {
mm/slub.c +6 -1
@@ -1344,6 +1344,7 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)void *start;void *last;void *p; +int order;BUG_ON (flags &GFP_SLAB_BUG_MASK );
@@ -1352,7 +1353,9 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)if (!page) goto out; + order =compound_order (page);inc_slabs_node (s,page_to_nid (page), page->objects); +memcg_bind_pages (s, order); page->slab = s;__SetPageSlab (page);if (page->pfmemalloc)
@@ -1361,7 +1364,7 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node) start =page_address (page);if (unlikely (s->flags &SLAB_POISON )) -memset (start,POISON_INUSE ,PAGE_SIZE <<compound_order (page)); +memset (start,POISON_INUSE ,PAGE_SIZE << order); last = start;for_each_object (p, s, start, page->objects) {
@@ -1402,6 +1405,8 @@ static void __free_slab(struct kmem_cache *s, struct page *page)__ClearPageSlabPfmemalloc (page);__ClearPageSlab (page); + +memcg_release_pages (s, order);reset_page_mapcount (page);if (current->reclaim_state) current->reclaim_state->reclaimed_slab += pages;
Signature · 1 line
1.7.11.4
PATCH v4 14/19 memcg/sl[au]b Track all the memcg children of a kmem_cache.
In reply to Glauber CostaThis enables us to remove all the children of a kmem_cache being destroyed, if for example the kernel module it's being used in gets unloaded. Otherwise, the children will still point to the destroyed parent.
--- include/linux/memcontrol.h | 6 ++++++ mm/memcontrol.c | 32 ++++++++++++++++++++++++++++++-- mm/slab_common.c | 3 +++ 3 files changed, 39 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +6 -0
@@ -427,6 +427,7 @@ struct kmem_cache * __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);void mem_cgroup_destroy_cache (struct kmem_cache *cachep); +void kmem_cache_destroy_memcg_children (struct kmem_cache *s);/** * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
@@ -578,6 +579,11 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) {return cachep; } + + +static inline void kmem_cache_destroy_memcg_children (struct kmem_cache *s) +{ +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
mm/memcontrol.c +30 -2
@@ -2716,6 +2716,8 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,memcg_check_events (memcg, page); } +static DEFINE_MUTEX (set_limit_mutex); +#ifdef CONFIG_MEMCG_KMEM static inline bool memcg_can_account_kmem (struct mem_cgroup *memcg) {
@@ -3093,6 +3095,34 @@ out:return new_cachep; } +void kmem_cache_destroy_memcg_children (struct kmem_cache *s) +{ +struct kmem_cache *c; +int i; + +if (!s->memcg_params) +return ; +if (!s->memcg_params->is_root_cache) +return ; + +/* +* If the cache is being destroyed, we trust that there is no one else +* requesting objects from it. Even if there are, the sanity checks in +* kmem_cache_destroy should caught this ill-case. +* +* Still, we don't want anyone else freeing memcg_caches under our +* noses, which can happen if a new memcg comes to life. As usual, +* we'll take the set_limit_mutex to protect ourselves against this. +*/ +mutex_lock (&set_limit_mutex); +for (i =0 ; i < memcg_limited_groups_array_size; i++) { + c = s->memcg_params->memcg_caches[i]; +if (c) +kmem_cache_destroy (c); + } +mutex_unlock (&set_limit_mutex); +} +struct create_work {struct mem_cgroup *memcg;struct kmem_cache *cachep;
@@ -4189,8 +4219,6 @@ void mem_cgroup_print_bad_page(struct page *page) }#endif -static DEFINE_MUTEX (set_limit_mutex); -static int mem_cgroup_resize_limit (struct mem_cgroup *memcg,unsigned long long val) {
mm/slab_common.c +3 -0
@@ -214,6 +214,9 @@ EXPORT_SYMBOL(kmem_cache_create);void kmem_cache_destroy (struct kmem_cache *s) { +/* Destroy all the children caches if we aren't a memcg cache */ +kmem_cache_destroy_memcg_children (s); +get_online_cpus ();mutex_lock (&slab_mutex); s->refcount--;
Signature · 1 line
1.7.11.4
PATCH v4 15/19 memcg/sl[au]b: shrink dead caches
In reply to Glauber CostaIn the slub allocator, when the last object of a page goes away, we don't necessarily free it - there is not necessarily a test for empty page in any slab_free path.
This means that when we destroy a memcg cache that happened to be empty, those caches may take a lot of time to go away: removing the memcg reference won't destroy them - because there are pending references, and the empty pages will stay there, until a shrinker is called upon for any reason.
This patch marks all memcg caches as dead. kmem_cache_shrink is called for the ones who are not yet dead - this will force internal cache reorganization, and then all references to empty pages will be removed.
An unlikely branch is used to make sure this case does not affect performance in the usual slab_free path.
The slab allocator has a time based reaper that would eventually get rid of the objects, but we can also call it explicitly, since dead caches are not a likely event.
[ v2: also call verify_dead for the slab ]
[ v3: use delayed_work to avoid calling verify_dead at every free]
--- include/linux/slab.h | 2 +- mm/memcontrol.c | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-)
include/linux/slab.h +1 -1
@@ -214,7 +214,7 @@ struct memcg_cache_params {struct kmem_cache *cachep;bool dead;atomic_t nr_pages; -struct work_struct destroy; +struct delayed_work destroy; }; }; };
mm/memcontrol.c +22 -6
@@ -2955,11 +2955,27 @@ static void kmem_cache_destroy_work_func(struct work_struct *w) {struct kmem_cache *cachep;struct memcg_cache_params *p; +struct delayed_work *dw =to_delayed_work (w); - p = container_of(w, struct memcg_cache_params, destroy); + p = container_of(dw, struct memcg_cache_params, destroy); cachep = p->cachep; -if (!atomic_read (&cachep->memcg_params->nr_pages)) +/* +* If we get down to 0 after shrink, we could delete right away. +* However, memcg_release_pages() already puts us back in the workqueue +* in that case. If we proceed deleting, we'll get a dangling +* reference, and removing the object from the workqueue in that case is +* unnecessary complication. We are not a fast path. +* +* If we aren't down to zero, we'll schedule a slow worker and try again +*/ +if (atomic_read (&cachep->memcg_params->nr_pages) !=0 ) { +kmem_cache_shrink (cachep); +if (atomic_read (&cachep->memcg_params->nr_pages) ==0 ) +return ; +/* Once per minute should be good enough. */ +schedule_delayed_work (&cachep->memcg_params->destroy,60 *HZ ); + }else kmem_cache_destroy (cachep); } static DECLARE_WORK(kmem_cache_destroy_work, kmem_cache_destroy_work_func);
@@ -2973,7 +2989,7 @@ void mem_cgroup_destroy_cache(struct kmem_cache *cachep)* We have to defer the actual destroying to a workqueue, because * we might currently be in a context that cannot sleep. */ -schedule_work (&cachep->memcg_params->destroy); +schedule_delayed_work (&cachep->memcg_params->destroy,0 ); }/*
@@ -3137,9 +3153,9 @@ static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)list_for_each_entry (cachep, &memcg->memcg_slab_caches, list) { cachep->memcg_params->dead = true; -INIT_WORK (&cachep->memcg_params->destroy, - kmem_cache_destroy_work_func); -schedule_work (&cachep->memcg_params->destroy); +INIT_DELAYED_WORK (&cachep->memcg_params->destroy, + kmem_cache_destroy_work_func); +schedule_delayed_work (&cachep->memcg_params->destroy,0 ); }mutex_unlock (&memcg->slab_caches_mutex); }
Signature · 1 line
1.7.11.4
PATCH v4 16/19 Aggregate memcg cache values in slabinfo
In reply to Glauber CostaWhen we create caches in memcgs, we need to display their usage information somewhere. We'll adopt a scheme similar to /proc/meminfo, with aggregate totals shown in the global file, and per-group information stored in the group itself.
For the time being, only reads are allowed in the per-group cache.
--- include/linux/memcontrol.h | 8 ++++++++ include/linux/slab.h | 3 +++ mm/memcontrol.c | 34 +++++++++++++++++++++++++++++++++- mm/slab.h | 27 +++++++++++++++++++++++++++ mm/slab_common.c | 42 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 109 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +8 -0
@@ -404,6 +404,11 @@ void sock_update_memcg(struct sock *sk);void sock_release_memcg (struct sock *sk);extern struct static_key memcg_kmem_enabled_key; + +extern int memcg_limited_groups_array_size; +#define for_each_memcg_cache_index (_idx) \ + for ((_idx) = 0; i < memcg_limited_groups_array_size; (_idx)++) +static inline bool memcg_kmem_enabled (void ) {return static_key_false (&memcg_kmem_enabled_key);
@@ -539,6 +544,9 @@ static inline void sock_release_memcg(struct sock *sk) { } +#define for_each_memcg_cache_index (_idx) \ + for (; NULL; ) +static inline bool memcg_kmem_enabled (void ) {return false;
include/linux/slab.h +3 -0
@@ -221,6 +221,9 @@ struct memcg_cache_params {int memcg_update_all_caches (int num_memcgs); +int cache_show (struct kmem_cache *s,struct seq_file *m); +void print_slabinfo_header (struct seq_file *m); +/* * Common kmalloc functions provided by all allocators */
mm/memcontrol.c +33 -1
@@ -552,7 +552,7 @@ static void disarm_sock_keys(struct mem_cgroup *memcg)#endif #ifdef CONFIG_MEMCG_KMEM -static int memcg_limited_groups_array_size; +int memcg_limited_groups_array_size;#define MEMCG_CACHES_MIN_SIZE 64/* * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
@@ -2725,6 +2725,33 @@ static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg) (memcg->kmem_accounted &KMEM_ACCOUNTED_MASK ); } + +static int mem_cgroup_slabinfo_write (struct cgroup *cont,struct cftype *cft, +const char *buffer) +{ +return -EIO ; +} + +static int mem_cgroup_slabinfo_read (struct cgroup *cont,struct cftype *cft, +struct seq_file *m) +{ +struct mem_cgroup *memcg =mem_cgroup_from_cont (cont); +struct kmem_cache *s; + +if (!memcg_can_account_kmem (memcg)) +return -EIO ; + +print_slabinfo_header (m); + +mutex_lock (&memcg->slab_caches_mutex); +list_for_each_entry (s, &memcg->memcg_slab_caches, list) +cache_show (s, m); + +mutex_unlock (&memcg->slab_caches_mutex); + +return 0 ; +} +static int memcg_charge_kmem (struct mem_cgroup *memcg,gfp_t gfp,u64 size) {struct res_counter *fail_res;
@@ -5661,6 +5688,11 @@ static struct cftype mem_cgroup_files[] = { .trigger = mem_cgroup_reset, .read = mem_cgroup_read, }, + { + .name = "kmem.slabinfo", + .write_string = mem_cgroup_slabinfo_write, + .read_seq_string = mem_cgroup_slabinfo_read, + },#endif { }, /* terminate */ };
mm/slab.h +27 -0
@@ -131,6 +131,23 @@ static inline bool slab_equal_or_root(struct kmem_cache *s,return (p == s) || (s->memcg_params && (p == s->memcg_params->root_cache)); } + +/* +* We use suffixes to the name in memcg because we can't have caches +* created in the system with the same name. But when we print them +* locally, better refer to them with the base name +*/ +static inline const char *cache_name (struct kmem_cache *s) +{ +if (!is_root_cache (s)) +return s->memcg_params->root_cache->name; +return s->name; +} + +static inline struct kmem_cache *cache_from_memcg (struct kmem_cache *s,int idx) +{ +return s->memcg_params->memcg_caches[idx]; +}#else static inline bool is_root_cache (struct kmem_cache *s) {
@@ -156,5 +173,15 @@ static inline bool slab_equal_or_root(struct kmem_cache *s, {return true; } + +static inline const char *cache_name (struct kmem_cache *s) +{ +return s->name; +} + +static inline struct kmem_cache *cache_from_memcg (struct kmem_cache *s,int idx) +{ +return NULL ; +}#endif #endif
mm/slab_common.c +38 -4
67 lines not on this page · raw message
Signature · 1 line
1.7.11.4
PATCH v4 17/19 slab: propagate tunables values
In reply to Glauber CostaSLAB allows us to tune a particular cache behavior with tunables. When creating a new memcg cache copy, we'd like to preserve any tunables the parent cache already had.
This could be done by an explicit call to do_tune_cpucache() after the cache is created. But this is not very convenient now that the caches are created from common code, since this function is SLAB-specific.
Another method of doing that is taking advantage of the fact that do_tune_cpucache() is always called from enable_cpucache(), which is called at cache initialization. We can just preset the values, and then things work as expected.
It can also happen that a root cache has its tunables updated during normal system operation. In this case, we will propagate the change to all caches that are already active.
This change will require us to move the assignment of root_cache in memcg_params a bit earlier. We need this to be already set - which memcg_kmem_register_cache will do - when we reach __kmem_cache_create()
--- include/linux/memcontrol.h | 8 +++++--- include/linux/slab.h | 2 +- mm/memcontrol.c | 10 ++++++---- mm/slab.c | 44 +++++++++++++++++++++++++++++++++++++++++--- mm/slab.h | 12 ++++++++++++ mm/slab_common.c | 7 ++++--- 6 files changed, 69 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +5 -3
@@ -421,7 +421,8 @@ void __memcg_kmem_commit_charge(struct page *page,void __memcg_kmem_uncharge_page (struct page *page,int order);int memcg_css_id (struct mem_cgroup *memcg); -int memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s); +int memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s, +struct kmem_cache *root_cache);void memcg_release_cache (struct kmem_cache *cachep);void memcg_cache_list_add (struct mem_cgroup *memcg,struct kmem_cache *cachep);
@@ -566,8 +567,9 @@ memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) { } -static inline int memcg_register_cache (struct mem_cgroup *memcg, -struct kmem_cache *s) +static inline int +memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s, +struct kmem_cache *root_cache) {return 0 ; }
include/linux/slab.h +1 -1
@@ -128,7 +128,7 @@ struct kmem_cache *kmem_cache_create(const char *, size_t, size_t, void (*)(void *));struct kmem_cache *kmem_cache_create_memcg (struct mem_cgroup *,const char *,size_t ,size_t , -unsigned long ,void (*)(void *)); +unsigned long ,void (*)(void *),struct kmem_cache *);void kmem_cache_destroy (struct kmem_cache *);int kmem_cache_shrink (struct kmem_cache *);void kmem_cache_free (struct kmem_cache *,void *);
mm/memcontrol.c +6 -4
@@ -2944,7 +2944,8 @@ int memcg_update_cache_size(struct kmem_cache *s, int num_groups)return 0 ; } -int memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s) +int memcg_register_cache (struct mem_cgroup *memcg,struct kmem_cache *s, +struct kmem_cache *root_cache) {size_t size =sizeof (struct memcg_cache_params );
@@ -2958,8 +2959,10 @@ int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s)if (!s->memcg_params)return -ENOMEM ; -if (memcg) +if (memcg) { s->memcg_params->memcg = memcg; + s->memcg_params->root_cache = root_cache; + }return 0 ; }
@@ -3082,7 +3085,7 @@ static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,return NULL ; new =kmem_cache_create_memcg (memcg, name, s->object_size, s->align, - (s->flags & ~SLAB_PANIC ), s->ctor); + (s->flags & ~SLAB_PANIC ), s->ctor, s);if (new) new->allocflags |= __GFP_KMEMCG;
@@ -3130,7 +3133,6 @@ static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, cachep->memcg_params->memcg_caches[idx] = new_cachep;wmb ();/* the readers won't lock, make sure everybody sees it */ new_cachep->memcg_params->memcg = memcg; - new_cachep->memcg_params->root_cache = cachep; new_cachep->memcg_params->cachep = new_cachep;atomic_set (&new_cachep->memcg_params->nr_pages ,0 ); out:
mm/slab.c +41 -3
67 lines not on this page · raw message
mm/slab.h +12 -0
@@ -148,6 +148,13 @@ static inline struct kmem_cache *cache_from_memcg(struct kmem_cache *s, int idx) {return s->memcg_params->memcg_caches[idx]; } + +static inline struct kmem_cache *memcg_root_cache (struct kmem_cache *s) +{ +if (is_root_cache (s)) +return s; +return s->memcg_params->root_cache; +}#else static inline bool is_root_cache (struct kmem_cache *s) {
@@ -183,5 +190,10 @@ static inline struct kmem_cache *cache_from_memcg(struct kmem_cache *s, int idx) {return NULL ; } + +static inline struct kmem_cache *memcg_root_cache (struct kmem_cache *s) +{ +return s; +}#endif #endif
mm/slab_common.c +4 -3
@@ -127,7 +127,8 @@ out:struct kmem_cache *kmem_cache_create_memcg (struct mem_cgroup *memcg,const char *name,size_t size, -size_t align,unsigned long flags,void (*ctor)(void *)) +size_t align,unsigned long flags,void (*ctor)(void *), +struct kmem_cache *parent_cache) {struct kmem_cache *s =NULL ;int err =0 ;
@@ -156,7 +157,7 @@ kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size, s->align = align; s->ctor = ctor; -if (memcg_register_cache (memcg, s)) { +if (memcg_register_cache (memcg, s, parent_cache)) {kmem_cache_free (kmem_cache, s); err = -ENOMEM ; goto out_locked;
@@ -208,7 +209,7 @@ struct kmem_cache * kmem_cache_create(const char *name, size_t size, size_t align, unsigned long flags, void (*ctor)(void *)) { - return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor); + return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor, NULL); }EXPORT_SYMBOL (kmem_cache_create);
Signature · 1 line
1.7.11.4
PATCH v4 18/19 slub: slub-specific propagation changes.
In reply to Glauber CostaSLUB allows us to tune a particular cache behavior with sysfs-based tunables. When creating a new memcg cache copy, we'd like to preserve any tunables the parent cache already had.
This can be done by tapping into the store attribute function provided by the allocator. We of course don't need to mess with read-only fields. Since the attributes can have multiple types and are stored internally by sysfs, the best strategy is to issue a ->show() in the root cache, and then ->store() in the memcg cache.
The drawback of that, is that sysfs can allocate up to a page in buffering for show(), that we are likely not to need, but also can't guarantee. To avoid always allocating a page for that, we can update the caches at store time with the maximum attribute size ever stored to the root cache. We will then get a buffer big enough to hold it. The corolary to this, is that if no stores happened, nothing will be propagated.
It can also happen that a root cache has its tunables updated during normal system operation. In this case, we will propagate the change to all caches that are already active.
--- include/linux/slub_def.h | 1 + mm/slub.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+)
include/linux/slub_def.h +1 -0
@@ -105,6 +105,7 @@ struct kmem_cache {#endif #ifdef CONFIG_MEMCG_KMEM struct memcg_cache_params *memcg_params; +int max_attr_size;/* for propagation, maximum size of a stored attr */ #endif #ifdef CONFIG_NUMA
mm/slub.c +71 -0
99 lines not on this page · raw message
Signature · 1 line
1.7.11.4
PATCH v4 19/19 Add slab-specific documentation about the kmem controller
In reply to Glauber Costa--- Documentation/cgroups/memory.txt | 7 +++++++ 1 file changed, 7 insertions(+)
Documentation/cgroups/memory.txt +7 -0
@@ -300,6 +300,13 @@ to trigger slab reclaim when those limits are reached. kernel memory, we prevent new processes from being created when the kernel memory usage is too high. +* slab pages: pages allocated by the SLAB or SLUB allocator are tracked. A copy +of each kmem_cache is created everytime the cache is touched by the first time +from inside the memcg. The creation is done lazily, so some objects can still be +skipped while the cache is being created. All objects in a slab page should +belong to the same memcg. This only fails to hold when a task is migrated to a +different memcg during the page allocation by the cache. + * sockets memory pressure: some sockets protocols have memory pressure thresholds. The Memory Controller allows them to be controlled individually per cgroup, instead of globally.
Signature · 1 line
1.7.11.4
Quoted 8 lines · Glauber Costa
This is a followup to the previous kmem series. I divided them logically so it gets easier for reviewers. But I believe they are ready to be merged together (although we can do a two-pass merge if people would prefer)
Throwaway git tree found at:
git://git.kernel.org/pub/scm/linux/kernel/git/glommer/memcg.gitkmemcg-slab
To all reviewers of my previous series (and others as well), I'd like to draw attention to this one.
This is the follow up to the kmemcg-stack series, to be applied right ontop. I believe the first series (kmemcg-stack) got more attention (which is good), but this one got a bunch of fixes and reviews in the process as well.
Thanks