kmemcg shrinkers
Subsystems (10)
v10 cover kmemcg shrinkers
Now that the list_lru seems to have stabilized, I am sending this out for review again. This now contains only the memcg part of the patchset, rebased on top of today's linux-next. Comments appreciated.
This patchset implements targeted shrinking for memcg when kmem limits are present. So far, we've been accounting kernel objects but failing allocations when short of memory. This is because our only option would be to call the global shrinker, depleting objects from all caches and breaking isolation.
The main idea is to associate per-memcg lists with each of the LRUs. The main LRU still provides a single entry point and when adding or removing an element from the LRU, we use the page information to figure out which memcg it belongs to and relay it to the right list.
Main changes from *v9
* Fixed iteration over all memcgs from list_lru side.
Main changes from *v8
* fixed xfs umount bug
* rebase to current linux-next
Main changes from *v7:
* Fixed races for memcg
* Enhanced memcg hierarchy walks during global pressure (we were walking only the global list, not all memcgs)
Main changes from *v6:
* Change nr_unused_dentry to long, Dave reported an int not being enough
* Fixed shrink_list leak, by Dave
* LRU API now gets a node id, instead of a node mask.
* per-node deferred work, leading to smoother behavior
Main changes from *v5:
* Rebased to linux-next, and fix the conflicts with the dcache.
* Make sure LRU_RETRY only retry once
* Prevent the bcache shrinker to scan the caches when disabled (by returning 0 in the count function)
* Fix i915 return code when mutex cannot be acquired.
* Only scan less-than-batch objects in memcg scenarios
Main changes from *v4:
* Fixed a bug in user-generated memcg pressure
* Fixed overly-agressive slab shrinker behavior spotted by Mel Gorman
* Various other fixes and comments by Mel Gorman
Main changes from *v3:
* Merged suggestions from mailing list.
* Removed the memcg-walking code from LRU. vmscan now drives all the hierarchy decisions, which makes more sense
* lazily free the old memcg arrays (needs now to be saved in struct lru). Since we need to call synchronize_rcu, calling it for every LRU can become expensive
* Moved the dead memcg shrinker to vmpressure. Already independently sent to linux-mm for review.
* Changed locking convention for LRU_RETRY. It now needs to return locked, which silents warnings about possible lock unbalance (although previous code was correct)
Main changes from *v2:
* shrink dead memcgs when global pressure kicks in. Uses the new lru API.
* bugfixes and comments from the mailing list.
* proper hierarchy-aware walk in shrink_slab.
Main changes from *v1:
* merged comments from the mailing list
* reworked lru-memcg API
* effective proportional shrinking
* sanitized locking on the memcg side
* bill user memory first when kmem == umem
* various bugfixes
*** BLURB HERE ***
Glauber Costa (16):
memcg: make cache index determination more robust
memcg: consolidate callers of memcg_cache_id
vmscan: also shrink slab in memcg pressure
memcg: move stop and resume accounting functions memcg,list_lru: duplicate LRUs upon kmemcg creation
lru: add an element to a memcg list
list_lru: per-memcg walks
memcg: move initialization to memcg creation
memcg: per-memcg kmem shrinking
memcg: scan cache objects hierarchically
vmscan: take at least one pass with shrinkers
super: targeted memcg reclaim
memcg: allow kmem limit to be resized down
vmpressure: in-kernel notifications
memcg: reap dead memcgs upon global memory pressure
memcg: flush memcg items upon memcg destruction
fs/dcache.c | 7 +- fs/inode.c | 7 +- fs/internal.h | 5 +- fs/super.c | 35 ++- include/linux/list_lru.h | 94 +++++++- include/linux/memcontrol.h | 43 ++++ include/linux/shrinker.h | 6 +- include/linux/swap.h | 2 + include/linux/vmpressure.h | 6 + mm/list_lru.c | 246 +++++++++++++++++-- mm/memcontrol.c | 574 +++++++++++++++++++++++++++++++++++++++------ mm/slab_common.c | 1 - mm/vmpressure.c | 52 +++- mm/vmscan.c | 179 ++++++++++++-- 14 files changed, 1116 insertions(+), 141 deletions(-)
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 1/16 memcg: make cache index determination more robust
From: Glauber Costa <[email protected]>
I caught myself doing something like the following outside memcg core:
memcg_id = -1; if (memcg && memcg_kmem_is_active(memcg)) memcg_id = memcg_cache_id(memcg);
to be able to handle all possible memcgs in a sane manner. In particular, the root cache will have kmemcg_id = -1 (just because we don't call memcg_kmem_init to the root cache since it is not limitable). We have always coped with that by making sure we sanitize which cache is passed to memcg_cache_id. Although this example is given for root, what we really need to know is whether or not a cache is kmem active.
But outside the memcg core testing for root, for instance, is not trivial since we don't export mem_cgroup_is_root. I ended up realizing that this tests really belong inside memcg_cache_id. This patch moves a similar but stronger test inside memcg_cache_id and make sure it always return a meaningful value.
--- mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/memcontrol.c +3 -1
@@ -3061,7 +3061,9 @@ void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)*/ int memcg_cache_id (struct mem_cgroup *memcg) { -return memcg ? memcg->kmemcg_id :-1 ; +if (!memcg || !memcg_can_account_kmem (memcg)) +return -1 ; +return memcg->kmemcg_id; }/*
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 2/16 memcg: consolidate callers of memcg_cache_id
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
Each caller of memcg_cache_id ends up sanitizing its parameters in its own way. Now that the memcg_cache_id itself is more robust, we can consolidate this.
Also, as suggested by Michal, a special helper memcg_cache_idx is used when the result is expected to be used directly as an array index to make sure we never accesses in a negative index.
--- mm/memcontrol.c | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-)
mm/memcontrol.c +29 -20
@@ -2941,6 +2941,30 @@ static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg) }/* +* helper for acessing a memcg's index. It will be used as an index in the +* child cache array in kmem_cache, and also to derive its name. This function +* will return -1 when this is not a kmem-limited memcg. +*/ +int memcg_cache_id (struct mem_cgroup *memcg) +{ +if (!memcg || !memcg_can_account_kmem (memcg)) +return -1 ; +return memcg->kmemcg_id; +} + +/* +* This helper around memcg_cache_id is not intented for use outside memcg +* core. It is meant for places where the cache id is used directly as an array +* index +*/ +static int memcg_cache_idx (struct mem_cgroup *memcg) +{ +int ret =memcg_cache_id (memcg); +BUG_ON (ret <0 ); +return ret; +} + +/* * This is a bit cumbersome, but it is rarely used and avoids a backpointer * in the memcg_cache_params struct. */
@@ -2950,7 +2974,7 @@ static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)VM_BUG_ON (p->is_root_cache); cachep = p->root_cache; -return cachep->memcg_params->memcg_caches[memcg_cache_id (p->memcg)]; +return cachep->memcg_params->memcg_caches[memcg_cache_idx (p->memcg)]; }#ifdef CONFIG_SLABINFO
@@ -3055,18 +3079,6 @@ void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep) }/* -* helper for acessing a memcg's index. It will be used as an index in the -* child cache array in kmem_cache, and also to derive its name. This function -* will return -1 when this is not a kmem-limited memcg. -*/ -int memcg_cache_id (struct mem_cgroup *memcg) -{ -if (!memcg || !memcg_can_account_kmem (memcg)) -return -1 ; -return memcg->kmemcg_id; -} - -/* * This ends up being protected by the set_limit mutex, during normal * operation, because that is its main call site. *
@@ -3225,7 +3237,7 @@ void memcg_release_cache(struct kmem_cache *s) goto out; memcg = s->memcg_params->memcg; - id =memcg_cache_id (memcg); + id =memcg_cache_idx (memcg); root = s->memcg_params->root_cache; root->memcg_params->memcg_caches[id] =NULL ;
@@ -3388,9 +3400,7 @@ static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,struct kmem_cache *new_cachep;int idx; -BUG_ON (!memcg_can_account_kmem (memcg)); - - idx =memcg_cache_id (memcg); + idx =memcg_cache_idx (memcg);mutex_lock (&memcg_cache_mutex); new_cachep = cachep->memcg_params->memcg_caches[idx];
@@ -3563,10 +3573,9 @@ struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,rcu_read_lock (); memcg =mem_cgroup_from_task (rcu_dereference (current->mm->owner)); -if (!memcg_can_account_kmem (memcg)) - goto out; - idx =memcg_cache_id (memcg); +if (idx <0 ) +return cachep;/* * barrier to mare sure we're always seeing the up to date value. The
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 3/16 vmscan: also shrink slab in memcg pressure
In reply to Glauber CostaWithout the surrounding infrastructure, this patch is a bit of a hammer:
it will basically shrink objects from all memcgs under memcg pressure. At least, however, we will keep the scan limited to the shrinkers marked as per-memcg.
Future patches will implement the in-shrinker logic to filter objects based on its memcg association.
--- include/linux/memcontrol.h | 17 ++++++++++++++++ include/linux/shrinker.h | 6 +++++- mm/memcontrol.c | 16 ++++++++++++++- mm/vmscan.c | 51 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 83 insertions(+), 7 deletions(-)
include/linux/memcontrol.h +17 -0
@@ -200,6 +200,9 @@ void mem_cgroup_split_huge_fixup(struct page *head);bool mem_cgroup_bad_page_check (struct page *page);void mem_cgroup_print_bad_page (struct page *page);#endif + +unsigned long +memcg_zone_reclaimable_pages (struct mem_cgroup *memcg,struct zone *zone);#else /* CONFIG_MEMCG */ struct mem_cgroup ;
@@ -378,6 +381,12 @@ static inline void mem_cgroup_replace_page_cache(struct page *oldpage, struct page *newpage) { } + +static inline unsigned long +memcg_zone_reclaimable_pages (struct mem_cgroup *memcg,struct zone *zone) +{ +return 0 ; +}#endif /* CONFIG_MEMCG */ #if !defined(CONFIG_MEMCG ) || !defined(CONFIG_DEBUG_VM )
@@ -430,6 +439,8 @@ static inline bool memcg_kmem_enabled(void)return static_key_false (&memcg_kmem_enabled_key); } +bool memcg_kmem_is_active (struct mem_cgroup *memcg); +/* * In general, we'll do everything in our power to not incur in any overhead * for non-memcg users for the kmem functions. Not even a function call, if we
@@ -563,6 +574,12 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) return __memcg_kmem_get_cache(cachep, gfp); }#else + +static inline bool memcg_kmem_is_active (struct mem_cgroup *memcg) +{ +return false; +} +#define for_each_memcg_cache_index (_idx) \ for (; NULL; )
include/linux/shrinker.h +5 -1
@@ -22,6 +22,9 @@ struct shrink_control {nodemask_t nodes_to_scan;/* current node being shrunk (for NUMA aware shrinkers) */ int nid; + +/* reclaim from this memcg only (if not NULL) */ +struct mem_cgroup *target_mem_cgroup; };#define SHRINK_STOP (~0UL)
@@ -63,7 +66,8 @@ struct shrinker {#define DEFAULT_SEEKS 2/* A good number if you don't know better. */ /* Flags */ -#define SHRINKER_NUMA_AWARE (1 << 0) +#define SHRINKER_NUMA_AWARE (1 << 0) +#define SHRINKER_MEMCG_AWARE (1 << 1)extern int register_shrinker (struct shrinker *);extern void unregister_shrinker (struct shrinker *);
mm/memcontrol.c +15 -1
@@ -369,7 +369,7 @@ static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)set_bit (KMEM_ACCOUNTED_ACTIVE , &memcg->kmem_account_flags); } -static bool memcg_kmem_is_active (struct mem_cgroup *memcg) +bool memcg_kmem_is_active (struct mem_cgroup *memcg) {return test_bit (KMEM_ACCOUNTED_ACTIVE , &memcg->kmem_account_flags); }
@@ -950,6 +950,20 @@ mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,return ret; } +unsigned long +memcg_zone_reclaimable_pages (struct mem_cgroup *memcg,struct zone *zone) +{ +int nid =zone_to_nid (zone); +int zid =zone_idx (zone); +unsigned long val; + + val =mem_cgroup_zone_nr_lru_pages (memcg, nid, zid,LRU_ALL_FILE ); +if (do_swap_account) + val +=mem_cgroup_zone_nr_lru_pages (memcg, nid, zid, +LRU_ALL_ANON ); +return val; +} +static unsigned long mem_cgroup_node_nr_lru_pages (struct mem_cgroup *memcg,int nid,unsigned int lru_mask)
mm/vmscan.c +46 -5
87 lines not on this page · raw message
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 4/16 memcg: move stop and resume accounting functions
In reply to Glauber CostaI need to move this up a bit, and I am doing it in a separate patch just to reduce churn in the patch that needs it.
--- mm/memcontrol.c | 63 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 31 deletions(-)
mm/memcontrol.c +32 -31
@@ -2991,6 +2991,38 @@ static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)return cachep->memcg_params->memcg_caches[memcg_cache_idx (p->memcg)]; } +/* +* During the creation a new cache, we need to disable our accounting mechanism +* altogether. This is true even if we are not creating, but rather just +* enqueing new caches to be created. +* +* This is because that process will trigger allocations; some visible, like +* explicit kmallocs to auxiliary data structures, name strings and internal +* cache structures; some well concealed, like INIT_WORK() that can allocate +* objects during debug. +* +* If any allocation happens during memcg_kmem_get_cache, we will recurse back +* to it. This may not be a bounded recursion: since the first cache creation +* failed to complete (waiting on the allocation), we'll just try to create the +* cache again, failing at the same point. +* +* memcg_kmem_get_cache is prepared to abort after seeing a positive count of +* memcg_kmem_skip_account. So we enclose anything that might allocate memory +* inside the following two functions. +*/ +static inline void memcg_stop_kmem_account (void ) +{ +VM_BUG_ON (!current->mm); + current->memcg_kmem_skip_account++; +} + +static inline void memcg_resume_kmem_account (void ) +{ +VM_BUG_ON (!current->mm); + current->memcg_kmem_skip_account--; +} + +#ifdef CONFIG_SLABINFO static int mem_cgroup_slabinfo_read (struct cgroup *cont,struct cftype *cft,struct seq_file *m)
@@ -3265,37 +3297,6 @@ out:kfree (s->memcg_params); } -/* -* During the creation a new cache, we need to disable our accounting mechanism -* altogether. This is true even if we are not creating, but rather just -* enqueing new caches to be created. -* -* This is because that process will trigger allocations; some visible, like -* explicit kmallocs to auxiliary data structures, name strings and internal -* cache structures; some well concealed, like INIT_WORK() that can allocate -* objects during debug. -* -* If any allocation happens during memcg_kmem_get_cache, we will recurse back -* to it. This may not be a bounded recursion: since the first cache creation -* failed to complete (waiting on the allocation), we'll just try to create the -* cache again, failing at the same point. -* -* memcg_kmem_get_cache is prepared to abort after seeing a positive count of -* memcg_kmem_skip_account. So we enclose anything that might allocate memory -* inside the following two functions. -*/ -static inline void memcg_stop_kmem_account (void ) -{ -VM_BUG_ON (!current->mm); - current->memcg_kmem_skip_account++; -} - -static inline void memcg_resume_kmem_account (void ) -{ -VM_BUG_ON (!current->mm); - current->memcg_kmem_skip_account--; -} -static void kmem_cache_destroy_work_func (struct work_struct *w) {struct kmem_cache *cachep;
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 5/16 memcg,list_lru: duplicate LRUs upon kmemcg creation
In reply to Glauber CostaWhen a new memcg is created, we need to open up room for its descriptors in all of the list_lrus that are marked per-memcg. The process is quite similar to the one we are using for the kmem caches: we initialize the new structures in an array indexed by kmemcg_id, and grow the array if needed. Key data like the size of the array will be shared between the kmem cache code and the list_lru code (they basically describe the same thing)
--- include/linux/list_lru.h | 53 ++++++++++++++- include/linux/memcontrol.h | 11 ++++ mm/list_lru.c | 117 ++++++++++++++++++++++++++++++--- mm/memcontrol.c | 157 +++++++++++++++++++++++++++++++++++++++++++-- mm/slab_common.c | 1 - 5 files changed, 323 insertions(+), 16 deletions(-)
include/linux/list_lru.h +52 -1
@@ -26,13 +26,64 @@ struct list_lru_node {long nr_items; }____cacheline_aligned_in_smp ; +/* +* This is supposed to be M x N matrix, where M is kmem-limited memcg, and N is +* the number of nodes. Both dimensions are likely to be very small, but are +* potentially very big. Therefore we will allocate or grow them dynamically. +* +* The value of M will increase as new memcgs appear and can be 0 if no memcgs +* are being used. This is done in mm/memcontrol.c in a way quite similar to +* the way we use for the slab cache management. +* +* The value of N can't be determined at compile time, but won't increase once +* we determine it. It is nr_node_ids, the firmware-provided maximum number of +* nodes in a system. +*/ +struct list_lru_array { +struct list_lru_node node[1 ]; +}; +struct list_lru {struct list_lru_node *node;nodemask_t active_nodes; +#ifdef CONFIG_MEMCG_KMEM +/* All memcg-aware LRUs will be chained in the lrus list */ +struct list_head lrus; +/* M x N matrix as described above */ +struct list_lru_array **memcg_lrus; +#endif }; +struct mem_cgroup ; +/* memcg functions +* +* This is the list_lru-side of the memcg update routines. They live here to avoid +* exposing too much of the internal structures and keeping things logically +* grouped. Those functions are not supposed to be called outside memcg core. +* +* They are called in two situations: when a memcg becomes kmem limited and +* when a new lru appears. A memcg becomes limited through a write to a cgroup +* file, and a new lru tends to appear when filesystems - or other future users +* - appear. Both situations tend to lead to predictable GFP_KERNEL allocations +* so we won't pass flags here. If you ever need to register lrus from +* contexts that are not GFP_KERNEL-safe, you may have to change this. +*/ +int memcg_update_all_lrus (unsigned long num); +struct list_lru_array *lru_alloc_memcg_array (void ); +void memcg_list_lru_register (struct list_lru *lru); +void memcg_destroy_all_lrus (struct mem_cgroup *memcg);void list_lru_destroy (struct list_lru *lru); -int list_lru_init (struct list_lru *lru); + +int __list_lru_init (struct list_lru *lru,bool memcg_enabled); +static inline int list_lru_init (struct list_lru *lru) +{ +return __list_lru_init (lru, false); +} + +static inline int list_lru_init_memcg (struct list_lru *lru) +{ +return __list_lru_init (lru, true); +}/** * list_lru_add: add an element to the lru list's tail
include/linux/memcontrol.h +11 -0
@@ -23,6 +23,7 @@#include <linux/vm_event_item.h> #include <linux/hardirq.h> #include <linux/jump_label.h> +#include <linux/list_lru.h> struct mem_cgroup ;struct page_cgroup ;
@@ -470,6 +471,11 @@ void memcg_update_array_size(int num_groups);struct kmem_cache *__memcg_kmem_get_cache (struct kmem_cache *cachep,gfp_t gfp); +int memcg_init_lru (struct list_lru *lru,bool memcg_enabled); + +int memcg_kmem_update_lru_size (struct list_lru *lru,int num_groups, +bool new_lru); +void mem_cgroup_destroy_cache (struct kmem_cache *cachep);void kmem_cache_destroy_memcg_children (struct kmem_cache *s);
@@ -633,6 +639,11 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)static inline void kmem_cache_destroy_memcg_children (struct kmem_cache *s) { } + +static inline int memcg_init_lru (struct list_lru *lru,bool memcg_enabled) +{ +return 0 ; +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +109 -8
139 lines not on this page · raw message
mm/memcontrol.c +151 -6
182 lines not on this page · raw message
mm/slab_common.c +0 -1
@@ -102,7 +102,6 @@ int memcg_update_all_caches(int num_memcgs) goto out; } -memcg_update_array_size (num_memcgs); out:mutex_unlock (&slab_mutex);return ret;
Signature · 7 lines
1.8.2.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v10 6/16 lru: add an element to a memcg list
In reply to Glauber CostaWith the infrastructure we now have, we can add an element to a memcg LRU list instead of the global list. The memcg lists are still per-node.
Technically, we will never trigger per-node shrinking if the memcg is short of memory. Therefore an alternative to this would be to add the element to *both* a single-node memcg array and a per-node global array.
There are two main reasons for this design choice:
1) adding an extra list_head to each of the objects would waste 16-bytes per object, always remembering that we are talking about 1 dentry + 1 inode in the common case. This means a close to 10 % increase in the dentry size, and a lower yet significant increase in the inode size. In terms of total memory, this design pays 32-byte per-superblock-per-node (size of struct list_lru_node), which means that in any scenario where we have more than 10 dentries + inodes, we would already be paying more memory in the two-list-heads approach than we will here with 1 node x 10 superblocks. The turning point of course depends on the workload, but I hope the figures above would convince you that the memory footprint is in my side in any workload that matters.
2) The main drawback of this, namely, that we lose global LRU order, is not really seen by me as a disadvantage: if we are using memcg to isolate the workloads, global pressure should try to balance the amount reclaimed from all memcgs the same way the shrinkers will already naturally balance the amount reclaimed from each superblock. (This patchset needs some love in this regard, btw).
To help us easily track down which nodes have and which nodes don't have elements in the list, we will use an auxiliary node bitmap at the global level.
[ v11: correctly deal with compound pages ]
[ v8: create LRUs before creating caches, and avoid races in which
elements are added to a non existing LRU ]
[ v2: move memcg_kmem_lru_of_page to list_lru.c and then unpublish the
auxiliary functions it uses ]
--- include/linux/list_lru.h | 11 +++++ include/linux/memcontrol.h | 7 +++ mm/list_lru.c | 110 +++++++++++++++++++++++++++++++++++++++++---- mm/memcontrol.c | 28 +++++++++++- 4 files changed, 146 insertions(+), 10 deletions(-)
include/linux/list_lru.h +11 -0
@@ -46,11 +46,22 @@ struct list_lru_array {struct list_lru {struct list_lru_node *node;nodemask_t active_nodes; +atomic_long_t node_totals[MAX_NUMNODES ];#ifdef CONFIG_MEMCG_KMEM /* All memcg-aware LRUs will be chained in the lrus list */ struct list_head lrus;/* M x N matrix as described above */ struct list_lru_array **memcg_lrus; +/* +* The memcg_lrus is RCU protected, so we need to keep the previous +* array around when we update it. But we can only do that after +* synchronize_rcu(). A typical system has many LRUs, which means +* that if we call synchronize_rcu after each LRU update, this +* will become very expensive. We add this pointer here, and then +* after all LRUs are updated, we call synchronize_rcu() once, and +* free all the old_arrays. +*/ +void *old_array;#endif };
include/linux/memcontrol.h +7 -0
@@ -473,6 +473,8 @@ __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);int memcg_init_lru (struct list_lru *lru,bool memcg_enabled); +struct mem_cgroup *mem_cgroup_from_kmem_page (struct page *page); +int memcg_kmem_update_lru_size (struct list_lru *lru,int num_groups,bool new_lru);
@@ -644,6 +646,11 @@ static inline int memcg_init_lru(struct list_lru *lru, bool memcg_enabled) {return 0 ; } + +static inline struct mem_cgroup *mem_cgroup_from_kmem_page (struct page *page) +{ +return NULL ; +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +101 -9
153 lines not on this page · raw message
mm/memcontrol.c +27 -1
@@ -3332,9 +3332,15 @@ int memcg_kmem_update_lru_size(struct list_lru *lru, int num_groups,* either follow the new array or the old one and they contain * exactly the same information. The new space at the end is * always empty anyway. +* +* We do have to make sure that no more users of the old +* memcg_lrus array exist before we free, and this is achieved +* by rcu. Since it would be too slow to synchronize RCU for +* every LRU, we store the pointer and let the LRU code free +* all of them when all LRUs are updated. */ if (lru->memcg_lrus) -kfree (old_array); + lru->old_array = old_array; }if (lru->memcg_lrus) {
@@ -3442,6 +3448,26 @@ out:kfree (s->memcg_params); } +struct mem_cgroup *mem_cgroup_from_kmem_page (struct page *page) +{ +struct page_cgroup *pc; +struct mem_cgroup *memcg =NULL ; + +/* +* Because we mark the page at commit time after the allocation has +* succeeded, only the head page of a compound page will be marked +*/ + pc =lookup_page_cgroup (compound_head (page)); +if (!PageCgroupUsed (pc)) +return NULL ; + +lock_page_cgroup (pc); +if (PageCgroupUsed (pc)) + memcg = pc->mem_cgroup; +unlock_page_cgroup (pc); +return memcg; +} +static void kmem_cache_destroy_work_func (struct work_struct *w) {struct kmem_cache *cachep;
Signature · 1 line
1.8.2.1
PATCH v10 7/16 list_lru: per-memcg walks
In reply to Glauber CostaThis patch extends the list_lru interfaces to allow for a memcg parameter. Because most of its users won't need it, instead of modifying the function signatures we create a new set of _memcg() functions and write the old API on top of that.
--- include/linux/list_lru.h | 30 +++++++++++++++++++++++++----- include/linux/memcontrol.h | 2 ++ mm/list_lru.c | 25 ++++++++++++++++++------- 3 files changed, 45 insertions(+), 12 deletions(-)
include/linux/list_lru.h +25 -5
@@ -128,15 +128,24 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item);bool list_lru_del (struct list_lru *lru,struct list_head *item);/** -* list_lru_count_node: return the number of objects currently held by @lru +* list_lru_count_node_memcg: return the number of objects currently held by @lru * @lru: the lru pointer. * @nid: the node id to count from. +* @memcg: restricts the count to this memcg, NULL for global. * * Always return a non-negative number, 0 for empty lists. There is no * guarantee that the list is not updated while the count is being computed. * Callers that want such a guarantee need to provide an outer lock. */ -unsigned long list_lru_count_node (struct list_lru *lru,int nid); +unsigned long list_lru_count_node_memcg (struct list_lru *lru,int nid, +struct mem_cgroup *memcg); + +static inline unsigned long +list_lru_count_node (struct list_lru *lru,int nid) +{ +return list_lru_count_node_memcg (lru, nid,NULL ); +} +static inline unsigned long list_lru_count (struct list_lru *lru) {long count =0 ;
@@ -158,6 +167,7 @@ typedef enum lru_status* the item currently being scanned * @cb_arg: opaque type that will be passed to @isolate * @nr_to_walk: how many items to scan. +* @memcg: restricts the scan to this memcg, NULL for global. * * This function will scan all elements in a particular list_lru, calling the * @isolate callback for each of those items, along with the current list
@@ -171,9 +181,19 @@ typedef enum lru_status* * Return value: the number of objects effectively removed from the LRU. */ -unsigned long list_lru_walk_node (struct list_lru *lru,int nid, -list_lru_walk_cb isolate,void *cb_arg, -unsigned long *nr_to_walk); +unsigned long +list_lru_walk_node_memcg (struct list_lru *lru,int nid, +list_lru_walk_cb isolate,void *cb_arg, +unsigned long *nr_to_walk,struct mem_cgroup *memcg); + +static inline unsigned long +list_lru_walk_node (struct list_lru *lru,int nid, +list_lru_walk_cb isolate,void *cb_arg, +unsigned long *nr_to_walk) +{ +return list_lru_walk_node_memcg (lru, nid, isolate, cb_arg, + nr_to_walk,NULL ); +} static inline unsigned long list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
include/linux/memcontrol.h +2 -0
@@ -591,6 +591,8 @@ static inline bool memcg_kmem_is_active(struct mem_cgroup *memcg)#define for_each_memcg_cache_index (_idx) \ for (; NULL; ) +#define memcg_limited_groups_array_size 0 +static inline bool memcg_kmem_enabled (void ) {return false;
mm/list_lru.c +18 -7
@@ -128,10 +128,16 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item)EXPORT_SYMBOL_GPL (list_lru_del);unsigned long -list_lru_count_node (struct list_lru *lru,int nid) +list_lru_count_node_memcg (struct list_lru *lru,int nid, +struct mem_cgroup *memcg) {unsigned long count =0 ; -struct list_lru_node *nlru = &lru->node[nid]; +int memcg_id =memcg_cache_id (memcg); +struct list_lru_node *nlru; + + nlru =lru_node_of_index (lru, memcg_id, nid); +if (!nlru) +return 0 ;spin_lock (&nlru->lock);WARN_ON_ONCE (nlru->nr_items <0 );
@@ -140,16 +146,18 @@ list_lru_count_node(struct list_lru *lru, int nid)return count; } -EXPORT_SYMBOL_GPL (list_lru_count_node); +EXPORT_SYMBOL_GPL (list_lru_count_node_memcg);unsigned long -list_lru_walk_node (struct list_lru *lru,int nid,list_lru_walk_cb isolate, -void *cb_arg,unsigned long *nr_to_walk) +list_lru_walk_node_memcg (struct list_lru *lru,int nid, +list_lru_walk_cb isolate,void *cb_arg, +unsigned long *nr_to_walk,struct mem_cgroup *memcg) { -struct list_lru_node *nlru = &lru->node[nid];struct list_head *item, *n;unsigned long isolated =0 ; +struct list_lru_node *nlru; +int memcg_id =memcg_cache_id (memcg);/* * If we don't keep state of at which pass we are, we can loop at * LRU_RETRY, since we have no guarantees that the caller will be able
@@ -159,6 +167,9 @@ list_lru_walk_node(struct list_lru *lru, int nid, list_lru_walk_cb isolate,*/ bool first_pass = true; + nlru =lru_node_of_index (lru, memcg_id, nid); +if (!nlru) +return 0 ;spin_lock (&nlru->lock); restart:list_for_each_safe (item, n, &nlru->list) {
@@ -196,7 +207,7 @@ restart:spin_unlock (&nlru->lock);return isolated; } -EXPORT_SYMBOL_GPL (list_lru_walk_node); +EXPORT_SYMBOL_GPL (list_lru_walk_node_memcg);/* * Each list_lru that is memcg-aware is inserted into the all_memcgs_lrus,
Signature · 1 line
1.8.2.1
PATCH v10 8/16 memcg: move initialization to memcg creation
In reply to Glauber CostaThose structures are only used for memcgs that are effectively using kmemcg. However, in a later patch I intend to use scan that list inconditionally (list empty meaning no kmem caches present), which simplifies the code a lot.
So move the initialization to early kmem creation.
--- mm/memcontrol.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/memcontrol.c +2 -3
@@ -3173,9 +3173,6 @@ int memcg_update_cache_sizes(struct mem_cgroup *memcg)memcg_update_array_size (num +1 ); -INIT_LIST_HEAD (&memcg->memcg_slab_caches); -mutex_init (&memcg->slab_caches_mutex); -return 0 ; out:ida_simple_remove (&kmem_limited_groups, num);
@@ -6085,6 +6082,8 @@ static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) {int ret; +INIT_LIST_HEAD (&memcg->memcg_slab_caches); +mutex_init (&memcg->slab_caches_mutex); memcg->kmemcg_id =-1 ; ret =memcg_propagate_kmem (memcg);if (ret)
Signature · 1 line
1.8.2.1
PATCH v10 9/16 memcg: per-memcg kmem shrinking
In reply to Glauber CostaIf the kernel limit is smaller than the user limit, we will have situations in which our allocations fail but freeing user pages will buy us nothing. In those, we would like to call a specialized memcg reclaimer that only frees kernel memory and leave the user memory alone. Those are also expected to fail when we account memcg->kmem, instead of when we account memcg->res. Based on that, this patch implements a memcg-specific reclaimer, that only shrinks kernel objects, withouth touching user pages.
There might be situations in which there are plenty of objects to shrink, but we can't do it because the __GFP_FS flag is not set. Although they can happen with user pages, they are a lot more common with fs-metadata: this is the case with almost all inode allocation.
For those cases, the best we can do is to spawn a worker and fail the current allocation.
--- include/linux/swap.h | 2 + mm/memcontrol.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++--- mm/vmscan.c | 44 ++++++++++++++++++- 3 files changed, 155 insertions(+), 7 deletions(-)
include/linux/swap.h +2 -0
@@ -276,6 +276,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,extern int __isolate_lru_page (struct page *page,isolate_mode_t mode);extern unsigned long try_to_free_mem_cgroup_pages (struct mem_cgroup *mem,gfp_t gfp_mask,bool noswap); +extern unsigned long try_to_free_mem_cgroup_kmem (struct mem_cgroup *mem, +gfp_t gfp_mask); extern unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem, gfp_t gfp_mask, bool noswap, struct zone *zone,
mm/memcontrol.c +110 -6
181 lines not on this page · raw message
mm/vmscan.c +43 -1
50 lines not on this page · raw message
Signature · 1 line
1.8.2.1
PATCH v10 10/16 memcg: scan cache objects hierarchically
In reply to Glauber CostaWhen reaching shrink_slab, we should descent in children memcg searching for objects that could be shrunk. This is true even if the memcg does not have kmem limits on, since the kmem res_counter will also be billed against the user res_counter of the parent.
It is possible that we will free objects and not free any pages, that will just harm the child groups without helping the parent group at all. But at this point, we basically are prepared to pay the price.
--- include/linux/memcontrol.h | 6 +++++ mm/memcontrol.c | 13 ++++++++++ mm/vmscan.c | 65 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 73 insertions(+), 11 deletions(-)
include/linux/memcontrol.h +6 -0
@@ -440,6 +440,7 @@ static inline bool memcg_kmem_enabled(void)return static_key_false (&memcg_kmem_enabled_key); } +bool memcg_kmem_should_reclaim (struct mem_cgroup *memcg);bool memcg_kmem_is_active (struct mem_cgroup *memcg);/*
@@ -583,6 +584,11 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) }#else +static inline bool memcg_kmem_should_reclaim (struct mem_cgroup *memcg) +{ +return false; +} +static inline bool memcg_kmem_is_active (struct mem_cgroup *memcg) {return false;
mm/memcontrol.c +13 -0
@@ -2976,6 +2976,19 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, }#ifdef CONFIG_MEMCG_KMEM +bool memcg_kmem_should_reclaim (struct mem_cgroup *memcg) +{ +struct mem_cgroup *iter; + +for_each_mem_cgroup_tree (iter, memcg) { +if (memcg_kmem_is_active (iter)) { +mem_cgroup_iter_break (memcg, iter); +return true; + } + } +return false; +} +static inline bool memcg_can_account_kmem (struct mem_cgroup *memcg) { return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
mm/vmscan.c +54 -11
92 lines not on this page · raw message
Signature · 1 line
1.8.2.1
PATCH v10 11/16 vmscan: take at least one pass with shrinkers
In reply to Glauber CostaIn very low free kernel memory situations, it may be the case that we have less objects to free than our initial batch size. If this is the case, it is better to shrink those, and open space for the new workload then to keep them and fail the new allocations.
In particular, we are concerned with the direct reclaim case for memcg. Although this same technique can be applied to other situations just as well, we will start conservative and apply it for that case, which is the one that matters the most.
[ v6: only do it per memcg ]
[ v5: differentiate no-scan case, don't do this for kswapd ]
--- mm/vmscan.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
mm/vmscan.c +18 -5
@@ -291,20 +291,33 @@ shrink_slab_node(struct shrink_control *shrinkctl, struct shrinker *shrinker, nr_pages_scanned, lru_pages, max_pass, delta, total_scan); -while (total_scan >= batch_size) { +do {unsigned long ret; +unsigned long nr_to_scan =min (batch_size, total_scan); +struct mem_cgroup *memcg = shrinkctl->target_mem_cgroup; + +/* +* Differentiate between "few objects" and "no objects" +* as returned by the count step. +*/ +if (!total_scan) +break ; + +if ((total_scan < batch_size) && + !(memcg &&memcg_kmem_is_active (memcg))) +break ; - shrinkctl->nr_to_scan = batch_size; + shrinkctl->nr_to_scan = nr_to_scan; ret = shrinker->scan_objects (shrinker, shrinkctl);if (ret ==SHRINK_STOP )break ; freed += ret; -count_vm_events (SLABS_SCANNED , batch_size); - total_scan -= batch_size; +count_vm_events (SLABS_SCANNED , nr_to_scan); + total_scan -= nr_to_scan;cond_resched (); - } + }while (total_scan >= batch_size);/* * move the unused scan count back into the shrinker in a
Signature · 1 line
1.8.2.1
PATCH v10 12/16 super: targeted memcg reclaim
In reply to Glauber CostaWe now have all our dentries and inodes placed in memcg-specific LRU lists. All we have to do is restrict the reclaim to the said lists in case of memcg pressure.
That can't be done so easily for the fs_objects part of the equation, since this is heavily fs-specific. What we do is pass on the context, and let the filesystems decide if they ever chose or want to. At this time, we just don't shrink them in memcg pressure (none is supported), leaving that for global pressure only.
Marking the superblock shrinker and its LRUs as memcg-aware will guarantee that the shrinkers will get invoked during targetted reclaim.
--- fs/dcache.c | 7 ++++--- fs/inode.c | 7 ++++--- fs/internal.h | 5 +++-- fs/super.c | 35 ++++++++++++++++++++++------------- 4 files changed, 33 insertions(+), 21 deletions(-)
fs/dcache.c +4 -3
@@ -901,13 +901,14 @@ dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)* use. */ long prune_dcache_sb (struct super_block *sb,unsigned long nr_to_scan, -int nid) +int nid,struct mem_cgroup *memcg) {LIST_HEAD (dispose);long freed; - freed =list_lru_walk_node (&sb->s_dentry_lru, nid, dentry_lru_isolate, - &dispose, &nr_to_scan); + freed =list_lru_walk_node_memcg (&sb->s_dentry_lru, nid, + dentry_lru_isolate, &dispose, + &nr_to_scan, memcg);shrink_dentry_list (&dispose);return freed; }
fs/inode.c +4 -3
@@ -749,13 +749,14 @@ inode_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)* then are freed outside inode_lock by dispose_list(). */ long prune_icache_sb (struct super_block *sb,unsigned long nr_to_scan, -int nid) +int nid,struct mem_cgroup *memcg) {LIST_HEAD (freeable);long freed; - freed =list_lru_walk_node (&sb->s_inode_lru, nid, inode_lru_isolate, - &freeable, &nr_to_scan); + freed =list_lru_walk_node_memcg (&sb->s_inode_lru, nid, + inode_lru_isolate, &freeable, + &nr_to_scan, memcg);dispose_list (&freeable);return freed; }
fs/internal.h +3 -2
@@ -16,6 +16,7 @@ struct file_system_type;struct linux_binprm ;struct path ;struct mount ; +struct mem_cgroup ;/* * block_dev.c
@@ -112,7 +113,7 @@ extern int open_check_o_direct(struct file *f);*/ extern spinlock_t inode_sb_list_lock;extern long prune_icache_sb (struct super_block *sb,unsigned long nr_to_scan, -int nid); +int nid,struct mem_cgroup *memcg);extern void inode_add_lru (struct inode *inode);/*
@@ -129,7 +130,7 @@ extern int invalidate_inodes(struct super_block *, bool);*/ extern struct dentry *__d_alloc (struct super_block *,const struct qstr *);extern long prune_dcache_sb (struct super_block *sb,unsigned long nr_to_scan, -int nid); +int nid,struct mem_cgroup *memcg);/* * read_write.c
fs/super.c +22 -13
@@ -34,6 +34,7 @@#include <linux/cleancache.h> #include <linux/fsnotify.h> #include <linux/lockdep.h> +#include <linux/memcontrol.h> #include "internal.h"
@@ -57,6 +58,7 @@ static unsigned long super_cache_scan(struct shrinker *shrink, struct shrink_control *sc) {struct super_block *sb; +struct mem_cgroup *memcg = sc->target_mem_cgroup;long fs_objects =0 ;long total_objects;long freed =0 ;
@@ -75,11 +77,12 @@ static unsigned long super_cache_scan(struct shrinker *shrink,if (!grab_super_passive (sb))return SHRINK_STOP ; -if (sb->s_op && sb->s_op->nr_cached_objects) +if (sb->s_op && sb->s_op->nr_cached_objects && !memcg) fs_objects = sb->s_op->nr_cached_objects (sb, sc->nid); - inodes =list_lru_count_node (&sb->s_inode_lru, sc->nid); - dentries =list_lru_count_node (&sb->s_dentry_lru, sc->nid); + inodes =list_lru_count_node_memcg (&sb->s_inode_lru, sc->nid, memcg); + dentries =list_lru_count_node_memcg (&sb->s_dentry_lru, sc->nid, memcg); + total_objects = dentries + inodes + fs_objects +1 ;/* proportion the scan between the caches */
@@ -90,8 +93,8 @@ static unsigned long super_cache_scan(struct shrinker *shrink,* prune the dcache first as the icache is pinned by it, then * prune the icache, followed by the filesystem specific caches */ - freed =prune_dcache_sb (sb, dentries, sc->nid); - freed +=prune_icache_sb (sb, inodes, sc->nid); + freed =prune_dcache_sb (sb, dentries, sc->nid, memcg); + freed +=prune_icache_sb (sb, inodes, sc->nid, memcg);if (fs_objects) { fs_objects = mult_frac(sc->nr_to_scan, fs_objects,
@@ -109,20 +112,26 @@ static unsigned long super_cache_count(struct shrinker *shrink, {struct super_block *sb;long total_objects =0 ; +struct mem_cgroup *memcg = sc->target_mem_cgroup; sb = container_of(shrink, struct super_block, s_shrink);if (!grab_super_passive (sb))return 0 ; -if (sb->s_op && sb->s_op->nr_cached_objects) +/* +* Ideally we would pass memcg to nr_cached_objects, and +* let the underlying filesystem decide. Most likely the +* path will be if (!memcg) return;, but even then. +*/ +if (sb->s_op && sb->s_op->nr_cached_objects && !memcg) total_objects = sb->s_op->nr_cached_objects (sb, sc->nid); - total_objects +=list_lru_count_node (&sb->s_dentry_lru, - sc->nid); - total_objects +=list_lru_count_node (&sb->s_inode_lru, - sc->nid); + total_objects +=list_lru_count_node_memcg (&sb->s_dentry_lru, + sc->nid, memcg); + total_objects +=list_lru_count_node_memcg (&sb->s_inode_lru, + sc->nid, memcg); total_objects =vfs_pressure_ratio (total_objects);drop_super (sb);
@@ -202,9 +211,9 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)INIT_HLIST_BL_HEAD (&s->s_anon);INIT_LIST_HEAD (&s->s_inodes); -if (list_lru_init (&s->s_dentry_lru)) +if (list_lru_init_memcg (&s->s_dentry_lru)) goto err_out; -if (list_lru_init (&s->s_inode_lru)) +if (list_lru_init_memcg (&s->s_inode_lru)) goto err_out_dentry_lru;INIT_LIST_HEAD (&s->s_mounts);
@@ -242,7 +251,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags) s->s_shrink.scan_objects = super_cache_scan; s->s_shrink.count_objects = super_cache_count; s->s_shrink.batch =1024 ; - s->s_shrink.flags =SHRINKER_NUMA_AWARE ; + s->s_shrink.flags =SHRINKER_NUMA_AWARE |SHRINKER_MEMCG_AWARE ; } out:return s;
Signature · 1 line
1.8.2.1
PATCH v10 13/16 memcg: allow kmem limit to be resized down
In reply to Glauber CostaThe userspace memory limit can be freely resized down. Upon attempt, reclaim will be called to flush the pages away until we either reach the limit we want or give up.
It wasn't possible so far with the kmem limit, since we had no way to shrink the kmem buffers other than using the big hammer of shrink_slab, that effectively frees data around the whole system.
The situation flips now that we have a per-memcg shrinker infrastructure. We will proceed analogously to our user memory counterpart and try to shrink our buffers until we either reach the limit we want or give up.
--- mm/memcontrol.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)
mm/memcontrol.c +38 -5
@@ -5446,10 +5446,39 @@ static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,return simple_read_from_buffer (buf, nbytes, ppos, str, len); } +#ifdef CONFIG_MEMCG_KMEM +/* +* This is slightly different than res or memsw reclaim. We already have +* vmscan behind us to drive the reclaim, so we can basically keep trying until +* all buffers that can be flushed are flushed. We have a very clear signal +* about it in the form of the return value of try_to_free_mem_cgroup_kmem. +*/ +static int mem_cgroup_resize_kmem_limit (struct mem_cgroup *memcg, +unsigned long long val) +{ +int ret = -EBUSY ; + +for (;;) { +if (signal_pending (current)) { + ret = -EINTR ; +break ; + } + + ret =res_counter_set_limit (&memcg->kmem, val); +if (!ret) +break ; + +/* Can't free anything, pointless to continue */ +if (!try_to_free_mem_cgroup_kmem (memcg,GFP_KERNEL )) +break ; + } + +return ret; +} +static int memcg_update_kmem_limit (struct cgroup *cont,u64 val) {int ret = -EINVAL ; -#ifdef CONFIG_MEMCG_KMEM struct mem_cgroup *memcg =mem_cgroup_from_cont (cont);/* * For simplicity, we won't allow this to be disabled. It also can't
@@ -5484,16 +5513,15 @@ static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)* starts accounting before all call sites are patched */ memcg_kmem_set_active (memcg); - }else - ret =res_counter_set_limit (&memcg->kmem, val); + }else { + ret =mem_cgroup_resize_kmem_limit (memcg, val); + } out:mutex_unlock (&set_limit_mutex);mutex_unlock (&memcg_create_mutex); -#endif return ret; } -#ifdef CONFIG_MEMCG_KMEM static int memcg_propagate_kmem (struct mem_cgroup *memcg) {int ret =0 ;
@@ -5530,6 +5558,11 @@ static int memcg_propagate_kmem(struct mem_cgroup *memcg) out: return ret; } +#else +static int memcg_update_kmem_limit (struct cgroup *cont,u64 val) +{ +return -EINVAL ; +}#endif /* CONFIG_MEMCG_KMEM */ /*
Signature · 1 line
1.8.2.1
PATCH v10 14/16 vmpressure: in-kernel notifications
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
During the past weeks, it became clear to us that the shrinker interface we have right now works very well for some particular types of users, but not that well for others. The later are usually people interested in one-shot notifications, that were forced to adapt themselves to the count+scan behavior of shrinkers. To do so, they had no choice than to greatly abuse the shrinker interface producing little monsters all over.
During LSF/MM, one of the proposals that popped out during our session was to reuse Anton Voronstsov's vmpressure for this. They are designed for userspace consumption, but also provide a well-stablished, cgroup-aware entry point for notifications.
This patch extends that to also support in-kernel users. Events that should be generated for in-kernel consumption will be marked as such, and for those, we will call a registered function instead of triggering an eventfd notification.
Please note that due to my lack of understanding of each shrinker user, I will stay away from converting the actual users, you are all welcome to do so.
--- include/linux/vmpressure.h | 6 ++++++ mm/vmpressure.c | 52 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-)
include/linux/vmpressure.h +6 -0
@@ -19,6 +19,9 @@ struct vmpressure {/* Have to grab the lock on events traversal or modifications. */ struct mutex events_lock; +/* False if only kernel users want to be notified, true otherwise. */ +bool notify_userspace; +struct work_struct work; };
@@ -36,6 +39,9 @@ extern struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css);extern int vmpressure_register_event (struct cgroup *cg,struct cftype *cft,struct eventfd_ctx *eventfd,const char *args); + +extern int vmpressure_register_kernel_event (struct cgroup *cg, +void (*fn)(void ));extern void vmpressure_unregister_event (struct cgroup *cg,struct cftype *cft,struct eventfd_ctx *eventfd);#else
mm/vmpressure.c +49 -3
90 lines not on this page · raw message
Signature · 1 line
1.8.2.1
PATCH v10 15/16 memcg: reap dead memcgs upon global memory pressure
In reply to Glauber CostaWhen we delete kmem-enabled memcgs, they can still be zombieing around for a while. The reason is that the objects may still be alive, and we won't be able to delete them at destruction time.
The only entry point for that, though, are the shrinkers. The shrinker interface, however, is not exactly tailored to our needs. It could be a little bit better by using the API Dave Chinner proposed, but it is still not ideal since we aren't really a count-and-scan event, but more a one-off flush-all-you-can event that would have to abuse that somehow.
--- mm/memcontrol.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 3 deletions(-)
mm/memcontrol.c +78 -3
@@ -297,8 +297,16 @@ struct mem_cgroup {/* thresholds for mem+swap usage. RCU-protected */ struct mem_cgroup_thresholds memsw_thresholds; -/* For oom notifier event fd */ -struct list_head oom_notify; +union { +/* For oom notifier event fd */ +struct list_head oom_notify; +/* +* we can only trigger an oom event if the memcg is alive. +* so we will reuse this field to hook the memcg in the list +* of dead memcgs. +*/ +struct list_head dead; + };/* * Should we move charges of a task when a task is moved into this
@@ -348,6 +356,29 @@ struct mem_cgroup {/* WARNING: nodeinfo must be the last member here */ }; +#if defined(CONFIG_MEMCG_KMEM ) || defined(CONFIG_MEMCG_SWAP ) +static LIST_HEAD (dangling_memcgs); +static DEFINE_MUTEX (dangling_memcgs_mutex); + +static inline void memcg_dangling_del (struct mem_cgroup *memcg) +{ +mutex_lock (&dangling_memcgs_mutex); +list_del (&memcg->dead); +mutex_unlock (&dangling_memcgs_mutex); +} + +static inline void memcg_dangling_add (struct mem_cgroup *memcg) +{ +INIT_LIST_HEAD (&memcg->dead); +mutex_lock (&dangling_memcgs_mutex); +list_add (&memcg->dead, &dangling_memcgs); +mutex_unlock (&dangling_memcgs_mutex); +} +#else +static inline void memcg_dangling_free (struct mem_cgroup *memcg) {} +static inline void memcg_dangling_add (struct mem_cgroup *memcg) {} +#endif +static size_t memcg_size (void ) { return sizeof(struct mem_cgroup) +
@@ -6227,6 +6258,41 @@ static int mem_cgroup_oom_control_write(struct cgroup *cgrp, }#ifdef CONFIG_MEMCG_KMEM +static void memcg_vmpressure_shrink_dead (void ) +{ +struct memcg_cache_params *params, *tmp; +struct kmem_cache *cachep; +struct mem_cgroup *memcg; + +mutex_lock (&dangling_memcgs_mutex); +list_for_each_entry (memcg, &dangling_memcgs, dead) { +mutex_lock (&memcg->slab_caches_mutex); +/* The element may go away as an indirect result of shrink */ +list_for_each_entry_safe (params, tmp, + &memcg->memcg_slab_caches, list) { + cachep =memcg_params_to_cache (params); +/* +* the cpu_hotplug lock is taken in kmem_cache_create +* outside the slab_caches_mutex manipulation. It will +* be taken by kmem_cache_shrink to flush the cache. +* So we need to drop the lock. It is all right because +* the lock only protects elements moving in and out the +* list. +*/ +mutex_unlock (&memcg->slab_caches_mutex); +kmem_cache_shrink (cachep); +mutex_lock (&memcg->slab_caches_mutex); + } +mutex_unlock (&memcg->slab_caches_mutex); + } +mutex_unlock (&dangling_memcgs_mutex); +} + +static void memcg_register_kmem_events (struct cgroup *cont) +{ +vmpressure_register_kernel_event (cont, memcg_vmpressure_shrink_dead); +} +static int memcg_init_kmem (struct mem_cgroup *memcg,struct cgroup_subsys *ss) {int ret;
@@ -6276,6 +6342,10 @@ static void kmem_cgroup_css_offline(struct mem_cgroup *memcg) css_put(&memcg->css); }#else +static inline void memcg_register_kmem_events (struct cgroup *cont) +{ +} +static int memcg_init_kmem (struct mem_cgroup *memcg,struct cgroup_subsys *ss) {return 0 ;
@@ -6607,8 +6677,10 @@ mem_cgroup_css_online(struct cgroup *cont)struct mem_cgroup *memcg, *parent;int error =0 ; -if (!cont->parent) +if (!cont->parent) { +memcg_register_kmem_events (cont);return 0 ; + }mutex_lock (&memcg_create_mutex); memcg =mem_cgroup_from_cont (cont);
@@ -6672,6 +6744,7 @@ static void mem_cgroup_css_offline(struct cgroup *cont)mem_cgroup_invalidate_reclaim_iterators (memcg);mem_cgroup_reparent_charges (memcg);mem_cgroup_destroy_all_caches (memcg); +memcg_dangling_add (memcg); }static void mem_cgroup_css_free (struct cgroup *cont)
@@ -6680,7 +6753,9 @@ static void mem_cgroup_css_free(struct cgroup *cont)mem_cgroup_sockets_destroy (memcg); +memcg_dangling_del (memcg);__mem_cgroup_free (memcg); + }#ifdef CONFIG_MMU
Signature · 1 line
1.8.2.1
PATCH v10 16/16 memcg: flush memcg items upon memcg destruction
In reply to Glauber CostaWhen a memcg is destroyed, it won't be imediately released until all objects are gone. This means that if a memcg is restarted with the very same workload - a very common case, the objects already cached won't be billed to the new memcg. This is mostly undesirable since a container can exploit this by restarting itself every time it reaches its limit, and then coming up again with a fresh new limit.
Since now we have targeted reclaim, I sustain that we should assume that a memcg that is destroyed should be flushed away. It makes perfect sense if we assume that a memcg that goes away most likely indicates an isolated workload that is terminated.
--- mm/memcontrol.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
mm/memcontrol.c +17 -0
@@ -6310,10 +6310,27 @@ static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)static void kmem_cgroup_css_offline (struct mem_cgroup *memcg) { +int ret;if (!memcg_kmem_is_active (memcg))return ;/* +* When a memcg is destroyed, it won't be imediately released until all +* objects are gone. This means that if a memcg is restarted with the +* very same workload - a very common case, the objects already cached +* won't be billed to the new memcg. This is mostly undesirable since a +* container can exploit this by restarting itself every time it +* reaches its limit, and then coming up again with a fresh new limit. +* +* Therefore a memcg that is destroyed should be flushed away. It makes +* perfect sense if we assume that a memcg that goes away indicates an +* isolated workload that is terminated. +*/ +do { + ret =try_to_free_mem_cgroup_kmem (memcg,GFP_KERNEL ); + }while (ret); + +/* * kmem charges can outlive the cgroup. In the case of slab * pages, for instance, a page contain objects from various * processes. As we prevent from taking a reference for every
Signature · 1 line
1.8.2.1