Add an owner to the mm_struct (v5)
Subsystems (7)
Changelog v4
------------
1. Release rcu_read_lock() after acquiring task_lock(). Also get a reference to the task_struct
2. Change cgroup mm_owner_changed callback to callback only if the cgroup of old and new task is different and to pass the old and new cgroups instead of task pointers
3. Port the patch to 2.6.25-rc8-mm1
Changelog v3
------------
1. Add mm->owner change callbacks using cgroups
This patch removes the mem_cgroup member from mm_struct and instead adds an owner. This approach was suggested by Paul Menage. The advantage of this approach is that, once the mm->owner is known, using the subsystem id, the cgroup can be determined. It also allows several control groups that are virtually grouped by mm_struct, to exist independent of the memory controller i.e., without adding mem_cgroup's for each controller, to mm_struct.
A new config option CONFIG_MM_OWNER is added and the memory resource controller selects this config option.
This patch also adds cgroup callbacks to notify subsystems when mm->owner changes. The mm_cgroup_changed callback is called with the task_lock() of the new task held and is called just prior to changing the mm->owner.
I am indebted to Paul Menage for the several reviews of this patchset and helping me make it lighter and simpler.
This patch was tested on a powerpc box.
---
fs/exec.c | 1 include/linux/cgroup.h | 15 ++++++++ include/linux/init_task.h | 2 - include/linux/memcontrol.h | 17 ++------- include/linux/mm_types.h | 5 +- include/linux/sched.h | 14 ++++++++ init/Kconfig | 15 ++++++++ kernel/cgroup.c | 30 +++++++++++++++++ kernel/exit.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 11 ++++-- mm/memcontrol.c | 21 +----------- 11 files changed, 171 insertions(+), 37 deletions(-)
fs/exec.c renamed from fs/exec.c~memory-controller-add-mm-owner +1 -0
@@ -735,6 +735,7 @@ static int exec_mmap(struct mm_struct *m tsk->active_mm = mm;activate_mm (active_mm, mm);task_unlock (tsk); +mm_update_next_owner (mm);arch_pick_mmap_layout (mm);if (old_mm) {up_read (&old_mm->mmap_sem);
include/linux/cgroup.h renamed from include/linux/cgroup.h~memory-controller-add-mm-owner +15 -0
@@ -300,6 +300,12 @@ struct cgroup_subsys { struct cgroup *cgrp);void (*post_clone)(struct cgroup_subsys *ss,struct cgroup *cgrp);void (*bind)(struct cgroup_subsys *ss,struct cgroup *root); +/* +* This routine is called with the task_lock of mm->owner held +*/ +void (*mm_owner_changed)(struct cgroup_subsys *ss, +struct cgroup *old, +struct cgroup *new);int subsys_id;int active;int disabled;
@@ -385,4 +391,13 @@ static inline int cgroupstats_build(stru#endif /* !CONFIG_CGROUPS */ +#ifdef CONFIG_MM_OWNER +extern void +cgroup_mm_owner_callbacks (struct task_struct *old,struct task_struct *new); +#else /* !CONFIG_MM_OWNER */ +static inline void +cgroup_mm_owner_callbacks (struct task_struct *old,struct task_struct *new) +{ +} +#endif /* CONFIG_MM_OWNER */ #endif /* _LINUX_CGROUP_H */
include/linux/init_task.h renamed from include/linux/init_task.h~memory-controller-add-mm-owner +1 -1
@@ -57,6 +57,7 @@ .page_table_lock = __SPIN_LOCK_UNLOCKED(name.page_table_lock), \ .mmlist = LIST_HEAD_INIT(name.mmlist), \ .cpu_vm_mask = CPU_MASK_ALL, \ + .owner = &init_task, \ }#define INIT_SIGNALS (sig) { \
@@ -199,7 +200,6 @@ extern struct group_info init_groups; INIT_LOCKDEP \ } -#define INIT_CPU_TIMERS (cpu_timers) \ { \ LIST_HEAD_INIT(cpu_timers[0]), \
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~memory-controller-add-mm-owner +4 -13
@@ -27,9 +27,6 @@ struct mm_struct;#ifdef CONFIG_CGROUP_MEM_RES_CTLR -extern void mm_init_cgroup (struct mm_struct *mm,struct task_struct *p); -extern void mm_free_cgroup (struct mm_struct *mm); -#define page_reset_bad_cgroup (page) ((page)->page_cgroup = 0)extern struct page_cgroup *page_get_page_cgroup (struct page *page);
@@ -48,8 +45,10 @@ extern unsigned long mem_cgroup_isolate_extern void mem_cgroup_out_of_memory (struct mem_cgroup *mem,gfp_t gfp_mask);int task_in_mem_cgroup (struct task_struct *task,const struct mem_cgroup *mem); +extern struct mem_cgroup *mem_cgroup_from_task (struct task_struct *p); +#define mm_match_cgroup (mm, cgroup) \ - ((cgroup) == rcu_dereference((mm)->mem_cgroup)) + ((cgroup) == mem_cgroup_from_task((mm)->owner))extern int mem_cgroup_prepare_migration (struct page *page);extern void mem_cgroup_end_migration (struct page *page);
@@ -73,15 +72,6 @@ extern long mem_cgroup_calc_reclaim_inac struct zone *zone, int priority);#else /* CONFIG_CGROUP_MEM_RES_CTLR */ -static inline void mm_init_cgroup (struct mm_struct *mm, -struct task_struct *p) -{ -} - -static inline void mm_free_cgroup (struct mm_struct *mm) -{ -} -static inline void page_reset_bad_cgroup (struct page *page) { }
@@ -172,6 +162,7 @@ static inline long mem_cgroup_calc_recla {return 0 ; } +#endif /* CONFIG_CGROUP_MEM_CONT */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/mm_types.h renamed from include/linux/mm_types.h~memory-controller-add-mm-owner +3 -2
@@ -230,8 +230,9 @@ struct mm_struct {/* aio bits */ rwlock_t ioctx_list_lock;/* aio lock */ struct kioctx *ioctx_list; -#ifdef CONFIG_CGROUP_MEM_RES_CTLR -struct mem_cgroup *mem_cgroup; +#ifdef CONFIG_MM_OWNER +struct task_struct *owner;/* The thread group leader that */ +/* owns the mm_struct. */ #endif #ifdef CONFIG_PROC_FS
include/linux/sched.h renamed from include/linux/sched.h~memory-controller-add-mm-owner +14 -0
@@ -2144,6 +2144,20 @@ static inline void migration_init(void)#define TASK_STATE_TO_CHAR_STR "RSDTtZX" +#ifdef CONFIG_MM_OWNER +extern void mm_update_next_owner (struct mm_struct *mm); +extern void mm_init_owner (struct mm_struct *mm,struct task_struct *p); +#else +static inline void +mm_update_next_owner (struct mm_struct *mm,struct task_struct *p) +{ +} + +static inline void mm_init_owner (struct mm_struct *mm,struct task_struct *p) +{ +} +#endif /* CONFIG_MM_OWNER */ +#endif /* __KERNEL__ */ #endif
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +15 -0
@@ -371,9 +371,21 @@ config RESOURCE_COUNTERS infrastructure that works with cgroups depends on CGROUPS +config MM_OWNER + bool "Enable ownership of mm structure" + help + This option enables mm_struct's to have an owner. The advantage + of this approach is that it allows for several independent memory + based cgorup controllers to co-exist independently without too + much space overhead + + This feature adds fork/exit overhead. So enable this only if + you need resource controllers + config CGROUP_MEM_RES_CTLR bool "Memory Resource Controller for Control Groups" depends on CGROUPS && RESOURCE_COUNTERS + select MM_OWNER help Provides a memory resource controller that manages both page cache and RSS memory.
@@ -386,6 +398,9 @@ config CGROUP_MEM_RES_CTLR Only enable when you're ok with these trade offs and really sure you need the memory resource controller. + This config option also selects MM_OWNER config option, which + could in turn add some fork/exit overhead. + config SYSFS_DEPRECATED bool
kernel/cgroup.c renamed from kernel/cgroup.c~memory-controller-add-mm-owner +30 -0
@@ -118,6 +118,7 @@ static int root_count;* be called. */ static int need_forkexit_callback; +static int need_mm_owner_callback;/* convenient tests for these bits */ inline int cgroup_is_removed (const struct cgroup *cgrp)
@@ -2485,6 +2486,7 @@ static void __init cgroup_init_subsys(st } need_forkexit_callback |= ss->fork || ss->exit; + need_mm_owner_callback |= !!ss->mm_owner_changed; ss->active =1 ; }
@@ -2721,6 +2723,34 @@ void cgroup_fork_callbacks(struct task_s } } +#ifdef CONFIG_MM_OWNER +/** +* cgroup_mm_owner_callbacks - run callbacks when the mm->owner changes +* @p: the new owner +* +* Called on every change to mm->owner. mm_init_owner() does not +* invoke this routine, since it assigns the mm->owner the first time +* and does not change it. +*/ +void cgroup_mm_owner_callbacks (struct task_struct *old,struct task_struct *new) +{ +struct cgroup *oldcgrp, *newcgrp; + +if (need_mm_owner_callback) { +int i; +for (i =0 ; i <CGROUP_SUBSYS_COUNT ; i++) { +struct cgroup_subsys *ss = subsys[i]; + oldcgrp =task_cgroup (old, ss->subsys_id); + newcgrp =task_cgroup (new, ss->subsys_id); +if (oldcgrp == newcgrp) +continue ; +if (ss->mm_owner_changed) + ss->mm_owner_changed (ss, oldcgrp, newcgrp); + } + } +} +#endif /* CONFIG_MM_OWNER */ +/** * cgroup_post_fork - called on a new task after adding it to the task list * @child: the task in question
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +77 -0
89 lines not on this page · raw message
kernel/fork.c renamed from kernel/fork.c~memory-controller-add-mm-owner +8 -3
@@ -358,14 +358,13 @@ static struct mm_struct * mm_init(struct mm->ioctx_list =NULL ; mm->free_area_cache =TASK_UNMAPPED_BASE ; mm->cached_hole_size = ~0UL ; -mm_init_cgroup (mm, p); +mm_init_owner (mm, p);if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm; } -mm_free_cgroup (mm);free_mm (mm);return NULL ; }
@@ -416,7 +415,6 @@ void mmput(struct mm_struct *mm)spin_unlock (&mmlist_lock); }put_swap_token (mm); -mm_free_cgroup (mm);mmdrop (mm); } }
@@ -996,6 +994,13 @@ static void rt_mutex_init_task(struct ta#endif } +#ifdef CONFIG_MM_OWNER +void mm_init_owner (struct mm_struct *mm,struct task_struct *p) +{ + mm->owner = p; +} +#endif /* CONFIG_MM_OWNER */ +/* * This creates a new process as a copy of the old one, * but does not actually start it yet.
mm/memcontrol.c renamed from mm/memcontrol.c~memory-controller-add-mm-owner +3 -18
@@ -238,26 +238,12 @@ static struct mem_cgroup *mem_cgroup_fro css); } -static struct mem_cgroup *mem_cgroup_from_task (struct task_struct *p) +struct mem_cgroup *mem_cgroup_from_task (struct task_struct *p) {return container_of (task_subsys_state (p, mem_cgroup_subsys_id), struct mem_cgroup, css); } -void mm_init_cgroup (struct mm_struct *mm,struct task_struct *p) -{ -struct mem_cgroup *mem; - - mem =mem_cgroup_from_task (p); -css_get (&mem->css); - mm->mem_cgroup = mem; -} - -void mm_free_cgroup (struct mm_struct *mm) -{ -css_put (&mm->mem_cgroup->css); -} -static inline int page_cgroup_locked (struct page *page) {return bit_spin_is_locked (PAGE_CGROUP_LOCK_BIT , &page->page_cgroup);
@@ -478,6 +464,7 @@ unsigned long mem_cgroup_isolate_pages(uint zid =zone_idx (z);struct mem_cgroup_per_zone *mz; +BUG_ON (!mem_cont); mz =mem_cgroup_zoneinfo (mem_cont, nid, zid);if (active) src = &mz->active_list;
@@ -576,7 +563,7 @@ retry: mm = &init_mm;rcu_read_lock (); - mem =rcu_dereference (mm->mem_cgroup); + mem =mem_cgroup_from_task (rcu_dereference (mm->owner));/* * For every charge from the cgroup, increment reference count */
@@ -1006,7 +993,6 @@ mem_cgroup_create(struct cgroup_subsys *if (unlikely ((cont->parent) ==NULL )) { mem = &init_mem_cgroup; - init_mm.mem_cgroup = mem; page_cgroup_cache =KMEM_CACHE (page_cgroup,SLAB_PANIC ); }else mem =kzalloc (sizeof (struct mem_cgroup ),GFP_KERNEL );
@@ -1088,7 +1074,6 @@ static void mem_cgroup_move_task(struct goto out;css_get (&mem->css); -rcu_assign_pointer (mm->mem_cgroup, mem);css_put (&old_mem->css); out:
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 95 lines · Balbir Singh
Changelog v4
------------
1. Releasercu_read_lock()after acquiringtask_lock(). Also get a reference to thetask_struct
2. Change cgroupmm_owner_changedcallback to callback only if the cgroup of old and new task is different and to pass the old and new cgroups instead of task pointers
3. Port the patch to 2.6.25-rc8-mm1Changelog v3
------------1. Add
mm->ownerchange callbacks using cgroupsThis patch removes the
mem_cgroupmember frommm_structand instead adds an owner. This approach was suggested by Paul Menage. The advantage of this approach is that, once themm->owneris known, using the subsystem id, the cgroup can be determined. It also allows several control groups that are virtually grouped bymm_struct, to exist independent of the memory controller i.e., without adding mem_cgroup's for each controller, tomm_struct.A new config option
CONFIG_MM_OWNERis added and the memory resource controller selects this config option.This patch also adds cgroup callbacks to notify subsystems when
mm->ownerchanges. Themm_cgroup_changedcallback is called with thetask_lock()of the new task held and is called just prior to changing themm->owner.I am indebted to Paul Menage for the several reviews of this patchset and helping me make it lighter and simpler.
This patch was tested on a powerpc box.
Signed-off-by Balbir Singh <[email protected]>---
fs/exec.c | 1 include/linux/cgroup.h | 15 ++++++++ include/linux/init_task.h | 2 - include/linux/memcontrol.h | 17 ++------- include/linux/mm_types.h | 5 +- include/linux/sched.h | 14 ++++++++ init/Kconfig | 15 ++++++++ kernel/cgroup.c | 30 +++++++++++++++++ kernel/exit.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 11 ++++-- mm/memcontrol.c | 21 +----------- 11 files changed, 171 insertions(+), 37 deletions(-)diff -puN
fs/exec.c~memory-controller-add-mm-ownerfs/exec.cfs/exec.c renamed from fs/exec.c~memory-controller-add-mm-owner +1
-0@@ -735,6 +735,7 @@ static int exec_mmap(struct mm_struct *m tsk->active_mm = mm;activate_mm (active_mm, mm);task_unlock (tsk); +mm_update_next_owner (mm);arch_pick_mmap_layout (mm);if (old_mm) {up_read (&old_mm->mmap_sem);diff -puN
include/linux/cgroup.h~memory-controller-add-mm-ownerinclude/linux/cgroup.hinclude/linux/cgroup.h renamed from include/linux/cgroup.h~memory-controller-add-mm-owner +15
-0@@ -300,6 +300,12 @@ struct cgroup_subsys { struct cgroup *cgrp);void (*post_clone)(struct cgroup_subsys *ss,struct cgroup *cgrp);void (*bind)(struct cgroup_subsys *ss,struct cgroup *root); +/* +* This routine is called with the task_lock of mm->owner held +*/ +void (*mm_owner_changed)(struct cgroup_subsys *ss, +struct cgroup *old, +struct cgroup *new);int subsys_id;int active;int disabled;@@ -385,4 +391,13 @@ static inline int cgroupstats_build(stru#endif /* !CONFIG_CGROUPS */ +#ifdef CONFIG_MM_OWNER +extern void +cgroup_mm_owner_callbacks (struct task_struct *old,struct task_struct *new); +#else /* !CONFIG_MM_OWNER */ +static inline void +cgroup_mm_owner_callbacks (struct task_struct *old,struct task_struct *new) +{ +} +#endif /* CONFIG_MM_OWNER */ #endif /* _LINUX_CGROUP_H */ diff -puN
include/linux/init_task.h~memory-controller-add-mm-ownerinclude/linux/init_task.h
include/linux/init_task.h line 60
Quoted 4 lines
include/linux/init_task.h renamed from include/linux/init_task.h~memory-controller-add-mm-owner +1
-0@@ -57,6 +57,7 @@ .page_table_lock = __SPIN_LOCK_UNLOCKED(name.page_table_lock), \ .mmlist = LIST_HEAD_INIT(name.mmlist), \ .cpu_vm_mask = CPU_MASK_ALL, \ + .owner = &init_task, \
#ifdef CONFIG_MM_OWNER .owner = &init_task,
#endif
Otherwise building broken with CONFIG_MM_OWNER disabled.
Quoted 6 lines
#ifdef CONFIG_MM_OWNER .owner = &init_task,#endif
Otherwise building broken with
CONFIG_MM_OWNERdisabled.
Good catch. Let me fix it and send v6
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
just nitpicks ;)
include/linux/memcontrol.h extern unsigned long mem_cgroup_isolate_ · line 51
Quoted 4 lines · On Thu, 03 Apr 2008 11:29:01 +0530 Balbir Singh <[email protected]>…
#define
mm_match_cgroup(mm, cgroup)\include/linux/memcontrol.h renamed from include/linux/memcontrol.h~memory-controller-add-mm-owner +1
-1- ((cgroup) ==rcu_dereference ((mm)->mem_cgroup)) + ((cgroup) ==mem_cgroup_from_task ((mm)->owner))
After this patch, above should be
==
#define mm_match_cgroup_css(mm, css, subsys_id) ((css) == task_subsys_state((mm)->owner, subsys_id)
==
This generic macro may be good for your purpose.
Quoted 5 lines
#endif
diff -puNinit/Kconfig~memory-controller-add-mm-ownerinit/Kconfig
init/Kconfig config RESOURCE_COUNTERS · line 380
Quoted 10 lines
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +7
-0@@ -371,9 +371,21 @@ config RESOURCE_COUNTERS infrastructure that works with cgroups depends on CGROUPS +config MM_OWNER + bool "Enable ownership of mm structure" + help + This option enables mm_struct's to have an owner. The advantage + of this approach is that it allows for several independent memory + based cgorup controllers to co-exist independently without too + much space overhead
Above is an explanation for this patch.
More simple text is better... How about
==
This is necessary for some cgroup subsystem related to memory management.
==
init/Kconfig config RESOURCE_COUNTERS · line 388
Quoted 10 lines
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +5
-0+ + This feature adds fork/exit overhead. So enable this only if + you need resource controllers + config CGROUP_MEM_RES_CTLR bool "Memory Resource Controller for Control Groups" depends on CGROUPS && RESOURCE_COUNTERS + select MM_OWNER
I don't like "select"....this should be
depends on CGROUPS && RESOURCE_COUNTERS && MM_OWNER
Thanks,
-Kame
Quoted 12 lines · KAMEZAWA Hiroyuki
just nitpicks ;)
include/linux/memcontrol.h extern unsigned long mem_cgroup_isolate_ · line 51
Quoted 4 lines · On Thu, 03 Apr 2008 11:29:01 +0530 Balbir Singh <[email protected]>…
#define
mm_match_cgroup(mm, cgroup)\include/linux/memcontrol.h renamed from include/linux/memcontrol.h~memory-controller-add-mm-owner +1
-1- ((cgroup) ==rcu_dereference ((mm)->mem_cgroup)) + ((cgroup) ==mem_cgroup_from_task ((mm)->owner))After this patch, above should be
==
Quoted 5 lines
#define mm_match_cgroup_css(mm, css, subsys_id) ((css) == task_subsys_state((mm)->owner, subsys_id)==
This generic macro may be good for your purpose.
When we call mm_match_cgroup_*, we don't want to dereference mem_cont->css to get the css, hence we abstract it away. This is called from mm/rmap.c
Quoted 9 lines
Quoted 9 lines
#endif
diff -puNinit/Kconfig~memory-controller-add-mm-ownerinit/Kconfiginit/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +0
-0@@ -371,9 +371,21 @@ config RESOURCE_COUNTERS infrastructure that works with cgroups depends on CGROUPS
Quoted 12 lines
init/Kconfig config RESOURCE_COUNTERS · line 380
Quoted 7 lines
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +7
-0+config MM_OWNER + bool "Enable ownership of mm structure" + help + This option enables mm_struct's to have an owner. The advantage + of this approach is that it allows for several independent memory + based cgorup controllers to co-exist independently without too + much space overheadAbove is an explanation for this patch.
More simple text is better... How about
==
This is necessary for some cgroup subsystem related to memory management.
==
Yes, but several other developers have also asked for it. revoke*, swap namespaces, etc will use it. I wanted to have a common definition.
Quoted 6 lines
Quoted 4 lines
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +4
-0+ + This feature adds fork/exit overhead. So enable this only if + you need resource controllers +
Quoted 8 lines
init/Kconfig config RESOURCE_COUNTERS · line 388
Quoted 4 lines
init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +1
-0config CGROUP_MEM_RES_CTLR bool "Memory Resource Controller for Control Groups" depends on CGROUPS && RESOURCE_COUNTERS + select MM_OWNERI don't like "select"....this should be
depends on CGROUPS && RESOURCE_COUNTERS && MM_OWNER
I discussed this will Paul and I think select is better. The user might ignore to enable MM_OWNER and wonder why memory controller or other features are not getting enabled.
Quoted 2 lines
Thanks,
-Kame
Thanks for the review
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL