Skip to contentSkip to conversation

Add an owner to the mm_struct (v4)

RFC In review Balbir Singh 4 participants 13 messages Apr 1, 2008 – Apr 3, 2008

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 Add an owner to the mm_struct (v4)

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.

NOTE: This patch was developed on top of 2.6.25-rc5-mm1 and is applied on top of the memory-controller-move-to-own-slab patch (which is already present in the Andrew's patchset).

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            |   24 ++++++++++++++++
 kernel/exit.c              |   66 +++++++++++++++++++++++++++++++++++++++++++++
 kernel/fork.c              |   11 +++++--
 mm/memcontrol.c            |   21 ++------------
 11 files changed, 154 insertions(+), 37 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~memory-controller-add-mm-owner +3 -2
diff -puN include/linux/mm_types.h~memory-controller-add-mm-owner include/linux/mm_types.h
@@ -227,8 +227,9 @@ struct mm_struct {
 	/* aio bits */
 	rwlock_t		ioctx_list_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
kernel/fork.c renamed from kernel/fork.c~memory-controller-add-mm-owner +8 -3
diff -puN kernel/fork.c~memory-controller-add-mm-owner kernel/fork.c
@@ -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;
 }
@@ -394,7 +393,6 @@ void __mmdrop(struct mm_struct *mm)
 {
 	BUG_ON(mm == &init_mm);
 	mm_free_pgd(mm);
-	mm_free_cgroup(mm);
 	destroy_context(mm);
 	free_mm(mm);
 }
@@ -995,6 +993,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.
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~memory-controller-add-mm-owner +4 -13
diff -puN include/linux/memcontrol.h~memory-controller-add-mm-owner include/linux/memcontrol.h
@@ -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 */
mm/memcontrol.c renamed from mm/memcontrol.c~memory-controller-add-mm-owner +3 -18
diff -puN mm/memcontrol.c~memory-controller-add-mm-owner mm/memcontrol.c
@@ -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(u
 	int 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
 	 */
@@ -990,7 +977,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);
@@ -1072,7 +1058,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:
include/linux/sched.h renamed from include/linux/sched.h~memory-controller-add-mm-owner +14 -0
diff -puN include/linux/sched.h~memory-controller-add-mm-owner include/linux/sched.h
@@ -2130,6 +2130,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
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +66 -0

78 lines not on this page · raw message

init/Kconfig renamed from init/Kconfig~memory-controller-add-mm-owner +15 -0
diff -puN init/Kconfig~memory-controller-add-mm-owner init/Kconfig
@@ -364,9 +364,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.
@@ -379,6 +391,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
 
include/linux/init_task.h renamed from include/linux/init_task.h~memory-controller-add-mm-owner +1 -1
diff -puN include/linux/init_task.h~memory-controller-add-mm-owner include/linux/init_task.h
@@ -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]),					\
fs/exec.c renamed from fs/exec.c~memory-controller-add-mm-owner +1 -0
diff -puN fs/exec.c~memory-controller-add-mm-owner fs/exec.c
@@ -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);
kernel/cgroup.c renamed from kernel/cgroup.c~memory-controller-add-mm-owner +24 -0
diff -puN kernel/cgroup.c~memory-controller-add-mm-owner kernel/cgroup.c
@@ -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)
@@ -2481,6 +2482,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;
 }
@@ -2717,6 +2719,28 @@ 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)
+{
+	if (need_mm_owner_callback) {
+		int i;
+		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
+			struct cgroup_subsys *ss = subsys[i];
+			if (ss->mm_owner_changed)
+				ss->mm_owner_changed(ss, old, new);
+		}
+	}
+}
+#endif /* CONFIG_MM_OWNER */
+
 /**
  * cgroup_post_fork - called on a new task after adding it to the task list
  * @child: the task in question
include/linux/cgroup.h renamed from include/linux/cgroup.h~memory-controller-add-mm-owner +15 -0
diff -puN include/linux/cgroup.h~memory-controller-add-mm-owner include/linux/cgroup.h
@@ -292,6 +292,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 task_struct *old,
+					struct task_struct *new);
 	int subsys_id;
 	int active;
 	int disabled;
@@ -377,4 +383,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 */

_

Signature · 10 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL

--
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>
#1 lore raw

Hi,

Quoted 10 lines · Balbir Singh
 @@ -227,8 +227,9 @@ struct mm_struct {
        /* aio bits */
        rwlock_t                ioctx_list_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

Yes, please. This is useful for the revokeat() patches as well. I currently need a big ugly loop to scan each task so I can break COW of private pages.

#2 lore raw
Quoted 14 lines · Pekka Enberg

Hi,

Quoted 10 lines · Balbir Singh
 @@ -227,8 +227,9 @@ struct mm_struct {
        /* aio bits */
        rwlock_t                ioctx_list_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

Quoted 3 lines

Yes, please. This is useful for the revokeat() patches as well. I currently need a big ugly loop to scan each task so I can break COW of private pages.

Hi, Pekka,

It's good to know that this will be useful. I think understand your use case of having to walk the entire tasklist to break COW of private pages, having the owner information right in the mm_struct is definitely useful.

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#3 lore raw
In reply to Balbir Singh

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 612

Quoted 8 lines · On Tue, 01 Apr 2008 18:13:12 +0530 Balbir Singh <[email protected]>…
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search in the children
+	 */
+	list_for_each_entry(c, &p->children, sibling) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	}
+

This finds new owner when "current" is multi-threaded and "current" called pthread_create(), right ?

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 620

Quoted 8 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search in the siblings
+	 */
+	list_for_each_entry(c, &p->parent->children, sibling) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	}
+

This finds new owner when "current" is multi-threaded and "current" is just a child (means it doesn't call pthread_create()) ?

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 628

Quoted 8 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search through everything else. We should not get
+	 * here often
+	 */
+	do_each_thread(g, c) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	} while_each_thread(g, c);

Doing above in synchronized manner seems too heavy.
When this happen ? or Can this be done in lazy "on-demand" manner ?

+assign_new_owner:
+ rcu_read_unlock();
+ BUG_ON(c == p);
+ task_lock(c);
+ if (c->mm != mm) {
+ task_unlock(c);
+ goto retry;
+ }
+ cgroup_mm_owner_callbacks(mm->owner, c);
+ mm->owner = c;
+ task_unlock(c);
+}
Why rcu_read_unlock() before changing owner ? Is it safe ?

Thanks,
-Kame

#4 lore raw
Quoted 13 lines · KAMEZAWA Hiroyuki

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 612

Quoted 8 lines · On Tue, 01 Apr 2008 18:13:12 +0530 Balbir Singh <[email protected]>…
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search in the children
+	 */
+	list_for_each_entry(c, &p->children, sibling) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	}
+

This finds new owner when "current" is multi-threaded and "current" called pthread_create(), right ?

No, it won't find the new owner if we have CLONE_THREAD passed while creating threads. mm_need_new_owner() checks for !delay_group_leader(). If the group_leader is set, we don't need a new owner, it stays around till all threads exit.

Quoted 11 lines

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 620

Quoted 8 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search in the siblings
+	 */
+	list_for_each_entry(c, &p->parent->children, sibling) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	}
+

This finds new owner when "current" is multi-threaded and "current" is just a child (means it doesn't call pthread_create()) ?

Ditto

Quoted 13 lines

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 628

Quoted 9 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search through everything else. We should not get
+	 * here often
+	 */
+	do_each_thread(g, c) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	} while_each_thread(g, c);

Doing above in synchronized manner seems too heavy.
When this happen ? or Can this be done in lazy "on-demand" manner ?

Do you mean under task_lock()?

Quoted 12 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +12 -0
+assign_new_owner:
+	rcu_read_unlock();
+	BUG_ON(c == p);
+	task_lock(c);
+	if (c->mm != mm) {
+		task_unlock(c);
+		goto retry;
+	}
+	cgroup_mm_owner_callbacks(mm->owner, c);
+	mm->owner = c;
+	task_unlock(c);
+}
Quoted 2 lines

Why rcu_read_unlock() before changing owner ? Is it safe ?

It should be safe, since we take task_lock(), but to be doubly sure, we can drop rcu read lock after taking the task_lock().

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#5 lore raw
Quoted 14 lines · On Wed, 02 Apr 2008 08:55:34 +0530 Balbir Singh <[email protected]>…
Quoted 13 lines

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 628

Quoted 9 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search through everything else. We should not get
+	 * here often
+	 */
+	do_each_thread(g, c) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	} while_each_thread(g, c);

Doing above in synchronized manner seems too heavy.
When this happen ? or Can this be done in lazy "on-demand" manner ?

Quoted 2 lines

Do you mean under task_lock()?

No, scanning itself.
How rarely this scan happens under a server which has 10000- threads ?

Thanks,
-Kame

#6 lore raw
Quoted 14 lines · KAMEZAWA Hiroyuki
Quoted 11 lines · On Wed, 02 Apr 2008 08:55:34 +0530 Balbir Singh <[email protected]>…
Quoted 11 lines

kernel/exit.c void exit_fs(struct task_struct *tsk) · line 628

Quoted 8 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +8 -0
+	/*
+	 * Search through everything else. We should not get
+	 * here often
+	 */
+	do_each_thread(g, c) {
+		if (c->mm == mm)
+			goto assign_new_owner;
+	} while_each_thread(g, c);

Doing above in synchronized manner seems too heavy.
When this happen ? or Can this be done in lazy "on-demand" manner ?

Quoted 5 lines
Quoted 2 lines

Do you mean under task_lock()?

No, scanning itself.
How rarely this scan happens under a server which has 10000- threads ?

This routine will be called every time a thread exits, but will quickly exit after checking mm_need_new_owner()

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#7 lore raw
In reply to Balbir Singh
Quoted 10 lines · Balbir Singh
Quoted 10 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +9 -0
+assign_new_owner:
+     rcu_read_unlock();
+     BUG_ON(c == p);
+     task_lock(c);
+     if (c->mm != mm) {
+             task_unlock(c);
+             goto retry;
+     }
+     cgroup_mm_owner_callbacks(mm->owner, c);
Quoted 9 lines
Quoted 5 lines
kernel/exit.c renamed from kernel/exit.c~memory-controller-add-mm-owner +3 -0
+     mm->owner = c;
+     task_unlock(c);
+}

Why rcu_read_unlock() before changing owner ? Is it safe ?

It should be safe, since we take task_lock(), but to be doubly sure, we can drop rcu read lock after taking the task_lock().

I agree with Kamezawa - the task can technically disappear as soon as we leave the RCU critical section. (In practice, it'll only happen with CONFIG_PREEMPT).

Paul

#8 lore raw
In reply to Balbir Singh
Quoted 24 lines · Balbir Singh

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.

NOTE: This patch was developed on top of 2.6.25-rc5-mm1 and is applied on top of the memory-controller-move-to-own-slab patch (which is already present in the Andrew's patchset).

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.

Quoted 6 lines

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.

Hi,

So far I've heard no objections or seen any review suggestions. Paul if you are OK with this patch, I'll ask Andrew to include it in -mm.

People waiting on this patch

1. Pekka Enberg for revoke* syscalls
2. Serge Hallyn for swap namespaces
3. Myself to implement the rlimit controller for cgroups

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#9 lore raw
Quoted 3 lines · Balbir Singh

So far I've heard no objections or seen any review suggestions. Paul if you are OK with this patch, I'll ask Andrew to include it in -mm.

My only thoughts were:

- I think I'd still prefer CONFIG_MM_OWNER to be auto-selected rather than manually configured, but it's not a huge deal either way.

- in theory I think we should goto retry if we get to the end of mm_update_next_owner() without finding any other owner. Otherwise we could miss another user if we race with one process forking a new child and then exiting?

- I was looking through the exit code trying to convince myself that current is still on the tasklist until after it makes this call. If it isn't then we could have trouble finding the new owner. But I can't figure out for sure exactly at what point we come off the tasklist.

- I think we only need the cgroup callback in the event that current->cgroups != new_owner->cgroups. (Hmm, have we already been moved back to the root cgroup by this point? If so, then we'll have no way to know which cgruop to unaccount from).

Paul

Quoted 15 lines

People waiting on this patch

1. Pekka Enberg for revoke* syscalls
2. Serge Hallyn for swap namespaces
3. Myself to implement the rlimit controller for cgroups

--
Warm Regards,
Balbir Singh
Linux Technology Center
IBM, ISTL

#10 lore raw
Quoted 9 lines · Paul Menage
Quoted 2 lines · Balbir Singh

So far I've heard no objections or seen any review suggestions. Paul if you are OK with this patch, I'll ask Andrew to include it in -mm.

My only thoughts were:

- I think I'd still prefer CONFIG_MM_OWNER to be auto-selected rather than manually configured, but it's not a huge deal either way.

It is auto-selected now by CONFIG_CGROUP_MEM_RES_CTLR in the latest patchset

Quoted 5 lines

- in theory I think we should goto retry if we get to the end of mm_update_next_owner() without finding any other owner. Otherwise we could miss another user if we race with one process forking a new child and then exiting?

When we the current task is exiting and we've verified that we are mm->owner and we cannot miss the new process since through the process of forking, it would have added the new process to the tasklist before exiting.

Quoted 5 lines

- I was looking through the exit code trying to convince myself that current is still on the tasklist until after it makes this call. If it isn't then we could have trouble finding the new owner. But I can't figure out for sure exactly at what point we come off the tasklist.

We come off the task list in __unhash_process(), which is in turn called by release_task() through __exit_signal().

Quoted 5 lines

- I think we only need the cgroup callback in the event that current->cgroups != new_owner->cgroups. (Hmm, have we already been moved back to the root cgroup by this point? If so, then we'll have no way to know which cgruop to unaccount from).

I checked to see that cgroup_exit is called after mm_update_new_owner(). We call mm_update_new_owner() from exit_mm(). I did not check for current->cgroups != new_owner->cgroups, since I did not want to limit the callbacks. An interested callback can make that check and no-op the callback.

I am going to change the rcu_read_lock(), so that it is released after we take the task_lock() and repost the patch

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#11 lore raw
Quoted 4 lines · Balbir Singh

I checked to see that cgroup_exit is called after mm_update_new_owner(). We call mm_update_new_owner() from exit_mm(). I did not check for current->cgroups != new_owner->cgroups, since I did not want to limit the callbacks.

No cgroup subsystem should be concerned about mm ownership changes between tasks in the same cgroup. So I think that's a valid and useful optimization.

Paul

#12 lore raw
Quoted 9 lines · Paul Menage
Quoted 3 lines · Balbir Singh

I checked to see that cgroup_exit is called after mm_update_new_owner(). We call mm_update_new_owner() from exit_mm(). I did not check for current->cgroups != new_owner->cgroups, since I did not want to limit the callbacks.

No cgroup subsystem should be concerned about mm ownership changes between tasks in the same cgroup. So I think that's a valid and useful optimization.

Hmm. probably.. I'll do that check. Let me post v5 with these changes

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL