Skip to contentSkip to conversation

cgroups: New max number of tasks subsystem (was: cgroups rlim subsystem)

PATCH Superseded Frederic Weisbecker 4 participants 22 messages Jul 11, 2011 – Jul 28, 2011

Subsystems (7)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover cgroups: New max number of tasks subsystem (was: cgroups rlim subsystem)

Hi all,

Changes in this release are:

* switch from general rlim subsystem to self-contained max number of tasks subsystem

* Don't use subsys->fork() callback but a static call to avoid cgroups complications.

* When moving a task accross cgroups, don't charge/uncharge the common ancestors, to fix race against concurrent forks.
To fix that, Paul proposed a res_counter_move_charge() that do all in once. But we need to be able to cancel the attachment in case a following subsystem refuses the attachment itself. Thus the charge and uncharge still need to be seperate. More specifically, we can only uncharge the old cgroup once we are sure of the attachment. Because we can't cancel that uncharge later due to possible forks in the middle.

Thanks.

Frederic Weisbecker (7):
cgroups: Add res_counter_write_u64() API
cgroups: New resource counter inheritance API
cgroups: Add previous cgroup in can_attach_task/attach_task callbacks
cgroups: New cancel_attach_task subsystem callback
cgroups: Ability to stop res charge propagation on bounded ancestor
cgroups: Add res counter common ancestor searching
cgroups: Add a max number of tasks subsystem

 block/blk-cgroup.c            |   10 ++-
 include/linux/cgroup.h        |   15 +++-
 include/linux/cgroup_subsys.h |    8 ++
 include/linux/res_counter.h   |   12 +++
 init/Kconfig                  |    7 ++
 kernel/Makefile               |    1 +
 kernel/cgroup.c               |   25 ++++--
 kernel/cgroup_freezer.c       |    3 +-
 kernel/cgroup_max_tasks.c     |  176 +++++++++++++++++++++++++++++++++++++++++
 kernel/cpuset.c               |    6 +-
 kernel/events/core.c          |    5 +-
 kernel/fork.c                 |    4 +
 kernel/res_counter.c          |   88 ++++++++++++++++++---
 kernel/sched.c                |    6 +-
 14 files changed, 335 insertions(+), 31 deletions(-)
 create mode 100644 kernel/cgroup_max_tasks.c
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/7 cgroups: Add res_counter_write_u64() API

Extend the resource counter API with a mirror of res_counter_read_u64() to make it handy to update a resource counter value from a cgroup subsystem u64 value file.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/res_counter.h |    2 ++
 kernel/res_counter.c        |   21 +++++++++++++++------
 2 files changed, 17 insertions(+), 6 deletions(-)
include/linux/res_counter.h +2 -0
index c9d625c..1b3fe05 100644
@@ -82,6 +82,8 @@ int res_counter_memparse_write_strategy(const char *buf,
 int res_counter_write(struct res_counter *counter, int member,
 		      const char *buffer, write_strategy_fn write_strategy);
 
+void res_counter_write_u64(struct res_counter *counter, int member, u64 val);
+
 /*
  * the field descriptors. one for each member of res_counter
  */
kernel/res_counter.c +15 -6
index 34683ef..806d041 100644
@@ -168,12 +168,22 @@ int res_counter_memparse_write_strategy(const char *buf,
 	return 0;
 }
 
+void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
+{
+	unsigned long long *target;
+	unsigned long flags;
+
+	spin_lock_irqsave(&counter->lock, flags);
+	target = res_counter_member(counter, member);
+	*target = val;
+	spin_unlock_irqrestore(&counter->lock, flags);
+}
+
 int res_counter_write(struct res_counter *counter, int member,
 		      const char *buf, write_strategy_fn write_strategy)
 {
 	char *end;
-	unsigned long flags;
-	unsigned long long tmp, *val;
+	unsigned long long tmp;
 
 	if (write_strategy) {
 		if (write_strategy(buf, &tmp))
@@ -183,9 +193,8 @@ int res_counter_write(struct res_counter *counter, int member,
 		if (*end != '\0')
 			return -EINVAL;
 	}
-	spin_lock_irqsave(&counter->lock, flags);
-	val = res_counter_member(counter, member);
-	*val = tmp;
-	spin_unlock_irqrestore(&counter->lock, flags);
+
+	res_counter_write_u64(counter, member, tmp);
+
 	return 0;
 }
Signature · 1 line
1.7.5.4
Acked-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] cgroups: Add res_counter_write_u64() API

Quoted 5 lines · Frederic Weisbecker

Extend the resource counter API with a mirror of res_counter_read_u64() to make it handy to update a resource counter value from a cgroup subsystem u64 value file.

Thanks,
Paul

Quoted 68 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 include/linux/res_counter.h |    2 ++ kernel/res_counter.c        |   21 +++++++++++++++------

 2 files changed, 17 insertions(+), 6 deletions(-)
include/linux/res_counter.h +2 -0
index c9d625c..1b3fe05 100644
@@ -82,6 +82,8 @@ int res_counter_memparse_write_strategy(const char *buf,
  int res_counter_write(struct res_counter *counter, int member,
                      const char *buffer, write_strategy_fn write_strategy);
 
+void res_counter_write_u64(struct res_counter *counter, int member, u64 val);
+
  /*
  * the field descriptors. one for each member of res_counter
  */
kernel/res_counter.c +15 -7
index 34683ef..806d041 100644
@@ -168,12 +168,22 @@ int res_counter_memparse_write_strategy(const char *buf,
        return 0;
  }
 
+void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
+{
+       unsigned long long *target;
+       unsigned long flags;
+
+       spin_lock_irqsave(&counter->lock, flags);
+       target = res_counter_member(counter, member);
+       *target = val;
+       spin_unlock_irqrestore(&counter->lock, flags);
+}
+
  int res_counter_write(struct res_counter *counter, int member,
                      const char *buf, write_strategy_fn write_strategy)
  {
        char *end;
-       unsigned long flags;
-       unsigned long long tmp, *val;
+       unsigned long long tmp;
 
        if (write_strategy) {
                if (write_strategy(buf, &tmp))
@@ -183,9 +193,8 @@ int res_counter_write(struct res_counter *counter, int member,
                if (*end != '\0')
                        return -EINVAL;
        }
-       spin_lock_irqsave(&counter->lock, flags);
-       val = res_counter_member(counter, member);
-       *val = tmp;
-       spin_unlock_irqrestore(&counter->lock, flags);
+
+       res_counter_write_u64(counter, member, tmp);
+
        return 0;
  }
--

1.7.5.4

#3 lore raw

PATCH 2/7 cgroups: New resource counter inheritance API

In reply to Frederic Weisbecker

Provide an API to inherit a counter value from a parent. This can be useful to implement cgroup.clone_children on a resource counter.

Still the resources of the children are limited by those of the parent, so this is only to provide a default setting behaviour when clone_children is set.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/res_counter.h |    2 ++
 kernel/res_counter.c        |   25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 0 deletions(-)
include/linux/res_counter.h +2 -0
index 1b3fe05..109d118 100644
@@ -84,6 +84,8 @@ int res_counter_write(struct res_counter *counter, int member,
 
 void res_counter_write_u64(struct res_counter *counter, int member, u64 val);
 
+void res_counter_inherit(struct res_counter *counter, int member);
+
 /*
  * the field descriptors. one for each member of res_counter
  */
kernel/res_counter.c +25 -0
index 806d041..8cb0362 100644
@@ -198,3 +198,28 @@ int res_counter_write(struct res_counter *counter, int member,
 
 	return 0;
 }
+
+void res_counter_inherit(struct res_counter *counter, int member)
+{
+	struct res_counter *parent;
+	unsigned long long *pval, val;
+	unsigned long flags;
+
+	parent = counter->parent;
+	if (!parent)
+		return;
+
+	local_irq_save(flags);
+
+	spin_lock(&counter->lock);
+	pval = res_counter_member(counter, member);
+	val = *pval;
+	spin_unlock(&counter->lock);
+
+	spin_lock(&parent->lock);
+	pval = res_counter_member(parent, member);
+	*pval = val;
+	spin_unlock(&parent->lock);
+
+	local_irq_restore(flags);
+}
Signature · 1 line
1.7.5.4
#4 lore raw

Re: [PATCH 2/7] cgroups: New resource counter inheritance API

Quoted 9 lines · Frederic Weisbecker

Provide an API to inherit a counter value from a parent. This can be useful to implement cgroup.clone_children on a resource counter.

Still the resources of the children are limited by those of the parent, so this is only to provide a default setting behaviour when clone_children is set.

Can't this be just:

void res_counter_inherit(struct res_counter *counter, int member) {
struct res_counter *parent;
parent = counter->parent;
if (parent)
res_counter_write_u64(counter, member,
res_counter_read_u64(parent, member));
}

This is just used at cgroup creation time, right? So the performance impact of an extra cli/sti shouldn't matter.

Also, looking at the code res_counter_read_u64() appears to not do any locking. I don't recall why I added it like that, but it probably ought to do at least an atomic64_read().

Paul

#5 lore raw

Re: [PATCH 2/7] cgroups: New resource counter inheritance API

Quoted 21 lines · Paul Menage
Quoted 9 lines · Frederic Weisbecker

Provide an API to inherit a counter value from a parent. This can be useful to implement cgroup.clone_children on a resource counter.

Still the resources of the children are limited by those of the parent, so this is only to provide a default setting behaviour when clone_children is set.

Can't this be just:

void res_counter_inherit(struct res_counter *counter, int member) {
struct res_counter *parent;
parent = counter->parent;
if (parent)
res_counter_write_u64(counter, member,
res_counter_read_u64(parent, member));
}

Quoted 2 lines

This is just used at cgroup creation time, right? So the performance impact of an extra cli/sti shouldn't matter.

Yeah indeed.

Quoted 3 lines

Also, looking at the code res_counter_read_u64() appears to not do any locking. I don't recall why I added it like that, but it probably ought to do at least an atomic64_read().

It does in 32 bits, using the res counter spinlock.

#6 lore raw

PATCH 3/7 cgroups: Add previous cgroup in can_attach_task/attach_task callbacks

In reply to Frederic Weisbecker

This is to prepare the integration of a new max number of proc cgroup subsystem. We'll need to release some resources from the previous cgroup.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 block/blk-cgroup.c      |   10 ++++++----
 include/linux/cgroup.h  |    5 +++--
 kernel/cgroup.c         |   10 ++++++----
 kernel/cgroup_freezer.c |    3 ++-
 kernel/cpuset.c         |    6 ++++--
 kernel/events/core.c    |    5 +++--
 kernel/sched.c          |    6 ++++--
 7 files changed, 28 insertions(+), 17 deletions(-)
block/blk-cgroup.c +6 -4
index bcaf16e..d1bfe88 100644
@@ -30,8 +30,8 @@ EXPORT_SYMBOL_GPL(blkio_root_cgroup);
 
 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
 						  struct cgroup *);
-static int blkiocg_can_attach_task(struct cgroup *, struct task_struct *);
-static void blkiocg_attach_task(struct cgroup *, struct task_struct *);
+static int blkiocg_can_attach_task(struct cgroup *, struct cgroup *, struct task_struct *);
+static void blkiocg_attach_task(struct cgroup *, struct cgroup *, struct task_struct *);
 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
 
@@ -1614,7 +1614,8 @@ done:
  * of the main cic data structures.  For now we allow a task to change
  * its cgroup only if it's the only owner of its ioc.
  */
-static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static int blkiocg_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+				   struct task_struct *tsk)
 {
 	struct io_context *ioc;
 	int ret = 0;
@@ -1629,7 +1630,8 @@ static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 	return ret;
 }
 
-static void blkiocg_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static void blkiocg_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+				struct task_struct *tsk)
 {
 	struct io_context *ioc;
 
include/linux/cgroup.h +3 -2
index ab4ac0c..e8288a0 100644
@@ -468,11 +468,12 @@ struct cgroup_subsys {
 	void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
 	int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
 			  struct task_struct *tsk);
-	int (*can_attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
+	int (*can_attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp,
+			       struct task_struct *tsk);
 	void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
 			      struct task_struct *tsk);
 	void (*pre_attach)(struct cgroup *cgrp);
-	void (*attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
+	void (*attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp, struct task_struct *tsk);
 	void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
 		       struct cgroup *old_cgrp, struct task_struct *tsk);
 	void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
kernel/cgroup.c +6 -4
index 2731d11..c3ee4cf 100644
@@ -1841,7 +1841,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 			}
 		}
 		if (ss->can_attach_task) {
-			retval = ss->can_attach_task(cgrp, tsk);
+			retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
 			if (retval) {
 				failed_ss = ss;
 				goto out;
@@ -1857,7 +1857,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 		if (ss->pre_attach)
 			ss->pre_attach(cgrp);
 		if (ss->attach_task)
-			ss->attach_task(cgrp, tsk);
+			ss->attach_task(cgrp, oldcgrp, tsk);
 		if (ss->attach)
 			ss->attach(ss, cgrp, oldcgrp, tsk);
 	}
@@ -2072,7 +2072,9 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
 			/* run on each task in the threadgroup. */
 			for (i = 0; i < group_size; i++) {
 				tsk = flex_array_get_ptr(group, i);
-				retval = ss->can_attach_task(cgrp, tsk);
+				oldcgrp = task_cgroup_from_root(tsk, root);
+
+				retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
 				if (retval) {
 					failed_ss = ss;
 					cancel_failed_ss = true;
@@ -2135,7 +2137,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
 		/* attach each task to each subsystem */
 		for_each_subsys(root, ss) {
 			if (ss->attach_task)
-				ss->attach_task(cgrp, tsk);
+				ss->attach_task(cgrp, oldcgrp, tsk);
 		}
 		/* if the thread is PF_EXITING, it can just get skipped. */
 		retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
kernel/cgroup_freezer.c +2 -1
index e691818..c1421a1 100644
@@ -175,7 +175,8 @@ static int freezer_can_attach(struct cgroup_subsys *ss,
 	return 0;
 }
 
-static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static int freezer_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+				   struct task_struct *tsk)
 {
 	rcu_read_lock();
 	if (__cgroup_freezing_or_frozen(tsk)) {
kernel/cpuset.c +4 -2
index 9c9b754..f66c9b4 100644
@@ -1390,7 +1390,8 @@ static int cpuset_can_attach(struct cgroup_subsys *ss, struct cgroup *cont,
 	return 0;
 }
 
-static int cpuset_can_attach_task(struct cgroup *cgrp, struct task_struct *task)
+static int cpuset_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+				  struct task_struct *task)
 {
 	return security_task_setscheduler(task);
 }
@@ -1418,7 +1419,8 @@ static void cpuset_pre_attach(struct cgroup *cont)
 }
 
 /* Per-thread attachment work. */
-static void cpuset_attach_task(struct cgroup *cont, struct task_struct *tsk)
+static void cpuset_attach_task(struct cgroup *cont, struct cgroup *old,
+			       struct task_struct *tsk)
 {
 	int err;
 	struct cpuset *cs = cgroup_cs(cont);
kernel/events/core.c +3 -2
index 9efe710..3daf0eb 100644
@@ -7403,7 +7403,8 @@ static int __perf_cgroup_move(void *info)
 }
 
 static void
-perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
+perf_cgroup_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+			struct task_struct *task)
 {
 	task_function_call(task, __perf_cgroup_move, task);
 }
@@ -7419,7 +7420,7 @@ static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
 	if (!(task->flags & PF_EXITING))
 		return;
 
-	perf_cgroup_attach_task(cgrp, task);
+	perf_cgroup_attach_task(cgrp, old_cgrp, task);
 }
 
 struct cgroup_subsys perf_subsys = {
kernel/sched.c +4 -2
index 3f2e502..adec58d 100644
@@ -8790,7 +8790,8 @@ cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
 }
 
 static int
-cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+			   struct task_struct *tsk)
 {
 #ifdef CONFIG_RT_GROUP_SCHED
 	if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
@@ -8804,7 +8805,8 @@ cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 }
 
 static void
-cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+cpu_cgroup_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+		       struct task_struct *tsk)
 {
 	sched_move_task(tsk);
 }
Signature · 1 line
1.7.5.4
Acked-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] cgroups: Add previous cgroup in can_attach_task/attach_task callbacks

Quoted 5 lines · Frederic Weisbecker

This is to prepare the integration of a new max number of proc cgroup subsystem. We'll need to release some resources from the previous cgroup.

Quoted 199 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 block/blk-cgroup.c      |   10 ++++++----
 include/linux/cgroup.h  |    5 +++--
 kernel/cgroup.c         |   10 ++++++----
 kernel/cgroup_freezer.c |    3 ++-
 kernel/cpuset.c         |    6 ++++--
 kernel/events/core.c    |    5 +++--
 kernel/sched.c          |    6 ++++--

 7 files changed, 28 insertions(+), 17 deletions(-)
block/blk-cgroup.c +6 -4
index bcaf16e..d1bfe88 100644
@@ -30,8 +30,8 @@ EXPORT_SYMBOL_GPL(blkio_root_cgroup);
 
  static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
                                                  struct cgroup *);
-static int blkiocg_can_attach_task(struct cgroup *, struct task_struct *);
-static void blkiocg_attach_task(struct cgroup *, struct task_struct *);
+static int blkiocg_can_attach_task(struct cgroup *, struct cgroup *, struct task_struct *);
+static void blkiocg_attach_task(struct cgroup *, struct cgroup *, struct task_struct *);
  static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
  static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
 
@@ -1614,7 +1614,8 @@ done:
  * of the main cic data structures.  For now we allow a task to change
  * its cgroup only if it's the only owner of its ioc.
  */
-static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static int blkiocg_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                                  struct task_struct *tsk)
  {
        struct io_context *ioc;
        int ret = 0;
@@ -1629,7 +1630,8 @@ static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
        return ret;
  }
 
-static void blkiocg_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static void blkiocg_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                               struct task_struct *tsk)
  {
        struct io_context *ioc;
 
include/linux/cgroup.h +3 -2
index ab4ac0c..e8288a0 100644
@@ -468,11 +468,12 @@ struct cgroup_subsys {
        void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
        int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
                          struct task_struct *tsk);
-       int (*can_attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
+       int (*can_attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                              struct task_struct *tsk);
        void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
                              struct task_struct *tsk);
        void (*pre_attach)(struct cgroup *cgrp);
-       void (*attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
+       void (*attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp, struct task_struct *tsk);
        void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
                       struct cgroup *old_cgrp, struct task_struct *tsk);
        void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
kernel/cgroup.c +6 -4
index 2731d11..c3ee4cf 100644
@@ -1841,7 +1841,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
                        }
                }
                if (ss->can_attach_task) {
-                       retval = ss->can_attach_task(cgrp, tsk);
+                       retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
                        if (retval) {
                                failed_ss = ss;
                                goto out;
@@ -1857,7 +1857,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
                if (ss->pre_attach)
                        ss->pre_attach(cgrp);
                if (ss->attach_task)
-                       ss->attach_task(cgrp, tsk);
+                       ss->attach_task(cgrp, oldcgrp, tsk);
                if (ss->attach)
                        ss->attach(ss, cgrp, oldcgrp, tsk);
        }
@@ -2072,7 +2072,9 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
                        /* run on each task in the threadgroup. */
                        for (i = 0; i < group_size; i++) {
                                tsk = flex_array_get_ptr(group, i);
-                               retval = ss->can_attach_task(cgrp, tsk);
+                               oldcgrp = task_cgroup_from_root(tsk, root);
+
+                               retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
                                if (retval) {
                                        failed_ss = ss;
                                        cancel_failed_ss = true;
@@ -2135,7 +2137,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
                /* attach each task to each subsystem */
                for_each_subsys(root, ss) {
                        if (ss->attach_task)
-                               ss->attach_task(cgrp, tsk);
+                               ss->attach_task(cgrp, oldcgrp, tsk);
                }
                /* if the thread is PF_EXITING, it can just get skipped. */
                retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
kernel/cgroup_freezer.c +2 -1
index e691818..c1421a1 100644
@@ -175,7 +175,8 @@ static int freezer_can_attach(struct cgroup_subsys *ss,
        return 0;
  }
 
-static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+static int freezer_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                                  struct task_struct *tsk)
  {
        rcu_read_lock();
        if (__cgroup_freezing_or_frozen(tsk)) {
kernel/cpuset.c +4 -2
index 9c9b754..f66c9b4 100644
@@ -1390,7 +1390,8 @@ static int cpuset_can_attach(struct cgroup_subsys *ss, struct cgroup *cont,
        return 0;
  }
 
-static int cpuset_can_attach_task(struct cgroup *cgrp, struct task_struct *task)
+static int cpuset_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                                 struct task_struct *task)
  {
        return security_task_setscheduler(task);
  }
@@ -1418,7 +1419,8 @@ static void cpuset_pre_attach(struct cgroup *cont)
  }
 
  /* Per-thread attachment work. */
-static void cpuset_attach_task(struct cgroup *cont, struct task_struct *tsk)
+static void cpuset_attach_task(struct cgroup *cont, struct cgroup *old,
+                              struct task_struct *tsk)
  {
        int err;
        struct cpuset *cs = cgroup_cs(cont);
kernel/events/core.c +0 -0
index 9efe710..3daf0eb 100644
@@ -7403,7 +7403,8 @@ static int __perf_cgroup_move(void *info)
  }
 

 static void

kernel/events/core.c +2 -1
-perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
+perf_cgroup_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                       struct task_struct *task)

 {
       task_function_call(task, __perf_cgroup_move, task); }

kernel/events/core.c +1 -1
@@ -7419,7 +7420,7 @@ static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
        if (!(task->flags & PF_EXITING))
                return;
 
-       perf_cgroup_attach_task(cgrp, task);
+       perf_cgroup_attach_task(cgrp, old_cgrp, task);
  }
 
  struct cgroup_subsys perf_subsys = {
kernel/sched.c +0 -0
index 3f2e502..adec58d 100644
@@ -8790,7 +8790,8 @@ cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  }
 

 static int

kernel/sched.c +2 -1
-cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                          struct task_struct *tsk)

 {
 #ifdef CONFIG_RT_GROUP_SCHED
       if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))

kernel/sched.c +0 -0
@@ -8804,7 +8805,8 @@ cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  }
 

 static void

kernel/sched.c +2 -1
-cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+cpu_cgroup_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
+                      struct task_struct *tsk)

 {
       sched_move_task(tsk);
 }
--
1.7.5.4

#8 lore raw

PATCH 4/7 cgroups: New cancel_attach_task subsystem callback

In reply to Frederic Weisbecker

To cancel a process attachment on a subsystem, we only call the cancel_attach() callback once on the leader but we have no way to cancel the attachment individually for each member of the process group.

This is going to be needed for the max number of tasks susbystem that is coming.

To prepare for this integration, call a new cancel_attach_task() callback on each task of the group until we reach the member that failed to attach.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/cgroup.h |    1 +
 kernel/cgroup.c        |   15 ++++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/cgroup.h +1 -0
index e8288a0..94454143 100644
@@ -472,6 +472,7 @@ struct cgroup_subsys {
 			       struct task_struct *tsk);
 	void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
 			      struct task_struct *tsk);
+	void (*cancel_attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
 	void (*pre_attach)(struct cgroup *cgrp);
 	void (*attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp, struct task_struct *tsk);
 	void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
kernel/cgroup.c +12 -3
index c3ee4cf..210dc05 100644
@@ -1989,7 +1989,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
 {
 	int retval, i, group_size;
 	struct cgroup_subsys *ss, *failed_ss = NULL;
-	bool cancel_failed_ss = false;
+	struct task_struct *failed_task = NULL;
 	/* guaranteed to be initialized later, but the compiler needs this */
 	struct cgroup *oldcgrp = NULL;
 	struct css_set *oldcg;
@@ -2077,7 +2077,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
 				retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
 				if (retval) {
 					failed_ss = ss;
-					cancel_failed_ss = true;
+					failed_task = tsk;
 					goto out_cancel_attach;
 				}
 			}
@@ -2172,8 +2172,17 @@ out_cancel_attach:
 	/* same deal as in cgroup_attach_task */
 	if (retval) {
 		for_each_subsys(root, ss) {
+			if (ss->cancel_attach_task && (ss != failed_ss || failed_task)) {
+				for (i = 0; i < group_size; i++) {
+					tsk = flex_array_get_ptr(group, i);
+					if (tsk == failed_task)
+						break;
+					ss->cancel_attach_task(cgrp, tsk);
+				}
+			}
+
 			if (ss == failed_ss) {
-				if (cancel_failed_ss && ss->cancel_attach)
+				if (failed_task && ss->cancel_attach)
 					ss->cancel_attach(ss, cgrp, leader);
 				break;
 			}
Signature · 1 line
1.7.5.4
Acked-by on v1 4/7
#9 lore raw

Re: [PATCH 4/7] cgroups: New cancel_attach_task subsystem callback

Quoted 5 lines · Frederic Weisbecker

To cancel a process attachment on a subsystem, we only call the cancel_attach() callback once on the leader but we have no way to cancel the attachment individually for each member of the process group.

Quoted 8 lines

This is going to be needed for the max number of tasks susbystem that is coming.

To prepare for this integration, call a new cancel_attach_task() callback on each task of the group until we reach the member that failed to attach.

Quoted 66 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 include/linux/cgroup.h |    1 +
 kernel/cgroup.c        |   15 ++++++++++++---

 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/cgroup.h +1 -0
index e8288a0..94454143 100644
@@ -472,6 +472,7 @@ struct cgroup_subsys {
                               struct task_struct *tsk);
        void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
                              struct task_struct *tsk);
+       void (*cancel_attach_task)(struct cgroup *cgrp, struct task_struct *tsk);
        void (*pre_attach)(struct cgroup *cgrp);
        void (*attach_task)(struct cgroup *cgrp, struct cgroup *old_cgrp, struct task_struct *tsk);
        void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
kernel/cgroup.c +12 -4
index c3ee4cf..210dc05 100644
@@ -1989,7 +1989,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
  {
        int retval, i, group_size;
        struct cgroup_subsys *ss, *failed_ss = NULL;
-       bool cancel_failed_ss = false;
+       struct task_struct *failed_task = NULL;
        /* guaranteed to be initialized later, but the compiler needs this */
        struct cgroup *oldcgrp = NULL;
        struct css_set *oldcg;
@@ -2077,7 +2077,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
                                retval = ss->can_attach_task(cgrp, oldcgrp, tsk);
                                if (retval) {
                                        failed_ss = ss;
-                                       cancel_failed_ss = true;
+                                       failed_task = tsk;
                                        goto out_cancel_attach;
                                }
                        }
@@ -2172,8 +2172,17 @@ out_cancel_attach:
        /* same deal as in cgroup_attach_task */
        if (retval) {
                for_each_subsys(root, ss) {
+                       if (ss->cancel_attach_task && (ss != failed_ss || failed_task)) {
+                               for (i = 0; i < group_size; i++) {
+                                       tsk = flex_array_get_ptr(group, i);
+                                       if (tsk == failed_task)
+                                               break;
+                                       ss->cancel_attach_task(cgrp, tsk);
+                               }
+                       }
+
                        if (ss == failed_ss) {
-                               if (cancel_failed_ss && ss->cancel_attach)
+                               if (failed_task && ss->cancel_attach)
                                        ss->cancel_attach(ss, cgrp, leader);
                                break;
                        }
--

1.7.5.4

#10 lore raw

PATCH 5/7 cgroups: Ability to stop res charge propagation on bounded ancestor

In reply to Frederic Weisbecker

Moving a task from a cgroup to another may require to substract its resource charge from the old cgroup and add it to the new one.

For this to happen, the uncharge/charge propagation can just stop when we reach the common ancestor for the two cgroups. Further the performance reasons, we also want to avoid to temporarily overload the common ancestors with a non-accurate resource counter usage if we charge first the new cgroup and uncharge the old one thereafter. This is going to be a requirement for the coming max number of task subsystem.

To solve this, provide a pair of new API that can charge/uncharge a resource counter until we reach a given ancestor.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/res_counter.h |    6 ++++++
 kernel/res_counter.c        |   23 ++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)
include/linux/res_counter.h +6 -0
index 109d118..8c421ac 100644
@@ -117,6 +117,10 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent);
 
 int __must_check res_counter_charge_locked(struct res_counter *counter,
 		unsigned long val);
+int __must_check res_counter_charge_until(struct res_counter *counter,
+					  struct res_counter *limit,
+					  unsigned long val,
+					  struct res_counter **limit_fail_at);
 int __must_check res_counter_charge(struct res_counter *counter,
 		unsigned long val, struct res_counter **limit_fail_at);
 
@@ -131,6 +135,8 @@ int __must_check res_counter_charge(struct res_counter *counter,
  */
 
 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
+void res_counter_uncharge_until(struct res_counter *counter, struct res_counter *limit,
+				unsigned long val);
 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
 
 /**
kernel/res_counter.c +18 -5
index 8cb0362..39f2513 100644
@@ -35,8 +35,9 @@ int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
 	return 0;
 }
 
-int res_counter_charge(struct res_counter *counter, unsigned long val,
-			struct res_counter **limit_fail_at)
+int res_counter_charge_until(struct res_counter *counter,
+			     struct res_counter *limit, unsigned long val,
+			     struct res_counter **limit_fail_at)
 {
 	int ret;
 	unsigned long flags;
@@ -44,7 +45,7 @@ int res_counter_charge(struct res_counter *counter, unsigned long val,
 
 	*limit_fail_at = NULL;
 	local_irq_save(flags);
-	for (c = counter; c != NULL; c = c->parent) {
+	for (c = counter; c != limit; c = c->parent) {
 		spin_lock(&c->lock);
 		ret = res_counter_charge_locked(c, val);
 		spin_unlock(&c->lock);
@@ -66,6 +67,12 @@ done:
 	return ret;
 }
 
+int res_counter_charge(struct res_counter *counter, unsigned long val,
+			struct res_counter **limit_fail_at)
+{
+	return res_counter_charge_until(counter, NULL, val, limit_fail_at);
+}
+
 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
 {
 	if (WARN_ON(counter->usage < val))
@@ -74,13 +81,15 @@ void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
 	counter->usage -= val;
 }
 
-void res_counter_uncharge(struct res_counter *counter, unsigned long val)
+void res_counter_uncharge_until(struct res_counter *counter,
+				struct res_counter *limit,
+				unsigned long val)
 {
 	unsigned long flags;
 	struct res_counter *c;
 
 	local_irq_save(flags);
-	for (c = counter; c != NULL; c = c->parent) {
+	for (c = counter; c != limit; c = c->parent) {
 		spin_lock(&c->lock);
 		res_counter_uncharge_locked(c, val);
 		spin_unlock(&c->lock);
@@ -88,6 +97,10 @@ void res_counter_uncharge(struct res_counter *counter, unsigned long val)
 	local_irq_restore(flags);
 }
 
+void res_counter_uncharge(struct res_counter *counter, unsigned long val)
+{
+	res_counter_uncharge_until(counter, NULL, val);
+}
 
 static inline unsigned long long *
 res_counter_member(struct res_counter *counter, int member)
Signature · 1 line
1.7.5.4
#11 lore raw

Re: [PATCH 5/7] cgroups: Ability to stop res charge propagation on bounded ancestor

Quoted 11 lines · On Mon, 11 Jul 2011 16:15:04 +0200 Frederic Weisbecker <[email protected]>…

Moving a task from a cgroup to another may require to substract its resource charge from the old cgroup and add it to the new one.

For this to happen, the uncharge/charge propagation can just stop when we reach the common ancestor for the two cgroups. Further the performance reasons, we also want to avoid to temporarily overload the common ancestors with a non-accurate resource counter usage if we charge first the new cgroup and uncharge the old one thereafter. This is going to be a requirement for the coming max number of task subsystem.

Quoted 8 lines

To solve this, provide a pair of new API that can charge/uncharge a resource counter until we reach a given ancestor.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

Hmm, do you have the number to show the benefit of this new function ? And....tasks is moving among cgroups so frequently as to show the benefit of this function in your environment ??

Thanks,
-Kame

#12 lore raw

Re: [PATCH 5/7] cgroups: Ability to stop res charge propagation on bounded ancestor

Quoted 24 lines · KAMEZAWA Hiroyuki
Quoted 11 lines · On Mon, 11 Jul 2011 16:15:04 +0200 Frederic Weisbecker <[email protected]>…

Moving a task from a cgroup to another may require to substract its resource charge from the old cgroup and add it to the new one.

For this to happen, the uncharge/charge propagation can just stop when we reach the common ancestor for the two cgroups. Further the performance reasons, we also want to avoid to temporarily overload the common ancestors with a non-accurate resource counter usage if we charge first the new cgroup and uncharge the old one thereafter. This is going to be a requirement for the coming max number of task subsystem.

Quoted 8 lines

To solve this, provide a pair of new API that can charge/uncharge a resource counter until we reach a given ancestor.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
Quoted 3 lines

Hmm, do you have the number to show the benefit of this new function ? And....tasks is moving among cgroups so frequently as to show the benefit of this function in your environment ??

So the benefit is not really in the optimization, although that's a side effect.

Let me clarify the point in the changelog.
Imagine we have these cgroups:

                 A  (usage = 2, limit = 2)
                 |
                / \
               /   \
              /     \
             /       \
            /         \
           /           \
          B             C  (usage = 1, limit = 2)

(usage = 1, limit = 2)

The usage in A is the accumulation of the usage in B and C. Imagine i want to move a task from C to B. This should work well. We need to first check if we can charge B and do it, and then later uncharge C.

But if we do:

err = res_counter_charge(B)
if (err)
exit
res_counter_uncharge(C)

it is going to fail because charging B will also charge A. And A will refuse because it's already full. Ideally we should first uncharge C and then charge B, so that A doesn't reject:

res_counter_uncharge(C)
err = res_countrer_charge(B)
if (err)
res_counter_charge(C)

The problem is that if charging B fails we need to rollback on C, but it might be too late as a fork might have happen inside C since we uncharged it, so we couldn't charge it back.

So the only solution is to first charge B but stop the charge propagation on A. And then uncharge on C but stop uncharge on A.

Acked-by on v1 5/7
#13 lore raw

Re: [PATCH 5/7] cgroups: Ability to stop res charge propagation on bounded ancestor

In reply to Frederic Weisbecker
Quoted 11 lines · Frederic Weisbecker

Moving a task from a cgroup to another may require to substract its resource charge from the old cgroup and add it to the new one.

For this to happen, the uncharge/charge propagation can just stop when we reach the common ancestor for the two cgroups. Further the performance reasons, we also want to avoid to temporarily overload the common ancestors with a non-accurate resource counter usage if we charge first the new cgroup and uncharge the old one thereafter. This is going to be a requirement for the coming max number of task subsystem.

Quoted 4 lines

To solve this, provide a pair of new API that can charge/uncharge a resource counter until we reach a given ancestor.

Looks good, thanks.
Paul

Quoted 104 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 include/linux/res_counter.h |    6 ++++++ kernel/res_counter.c        |   23 ++++++++++++++++++-----

 2 files changed, 24 insertions(+), 5 deletions(-)
include/linux/res_counter.h +6 -0
index 109d118..8c421ac 100644
@@ -117,6 +117,10 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent);
 
  int __must_check res_counter_charge_locked(struct res_counter *counter,
                unsigned long val);
+int __must_check res_counter_charge_until(struct res_counter *counter,
+                                         struct res_counter *limit,
+                                         unsigned long val,
+                                         struct res_counter **limit_fail_at);
  int __must_check res_counter_charge(struct res_counter *counter,
                unsigned long val, struct res_counter **limit_fail_at);
 
@@ -131,6 +135,8 @@ int __must_check res_counter_charge(struct res_counter *counter,
  */
 
  void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
+void res_counter_uncharge_until(struct res_counter *counter, struct res_counter *limit,
+                               unsigned long val);
  void res_counter_uncharge(struct res_counter *counter, unsigned long val);
 
  /**
kernel/res_counter.c +18 -5
index 8cb0362..39f2513 100644
@@ -35,8 +35,9 @@ int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
        return 0;
  }
 
-int res_counter_charge(struct res_counter *counter, unsigned long val,
-                       struct res_counter **limit_fail_at)
+int res_counter_charge_until(struct res_counter *counter,
+                            struct res_counter *limit, unsigned long val,
+                            struct res_counter **limit_fail_at)
  {
        int ret;
        unsigned long flags;
@@ -44,7 +45,7 @@ int res_counter_charge(struct res_counter *counter, unsigned long val,
 
        *limit_fail_at = NULL;
        local_irq_save(flags);
-       for (c = counter; c != NULL; c = c->parent) {
+       for (c = counter; c != limit; c = c->parent) {
                spin_lock(&c->lock);
                ret = res_counter_charge_locked(c, val);
                spin_unlock(&c->lock);
@@ -66,6 +67,12 @@ done:
        return ret;
  }
 
+int res_counter_charge(struct res_counter *counter, unsigned long val,
+                       struct res_counter **limit_fail_at)
+{
+       return res_counter_charge_until(counter, NULL, val, limit_fail_at);
+}
+
  void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
  {
        if (WARN_ON(counter->usage < val))
@@ -74,13 +81,15 @@ void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
        counter->usage -= val;
  }
 
-void res_counter_uncharge(struct res_counter *counter, unsigned long val)
+void res_counter_uncharge_until(struct res_counter *counter,
+                               struct res_counter *limit,
+                               unsigned long val)
  {
        unsigned long flags;
        struct res_counter *c;
 
        local_irq_save(flags);
-       for (c = counter; c != NULL; c = c->parent) {
+       for (c = counter; c != limit; c = c->parent) {
                spin_lock(&c->lock);
                res_counter_uncharge_locked(c, val);
                spin_unlock(&c->lock);
@@ -88,6 +97,10 @@ void res_counter_uncharge(struct res_counter *counter, unsigned long val)
        local_irq_restore(flags);
  }
 
+void res_counter_uncharge(struct res_counter *counter, unsigned long val)
+{
+       res_counter_uncharge_until(counter, NULL, val);
+}
 

 static inline unsigned long long * res_counter_member(struct res_counter *counter, int member) --
1.7.5.4

#14 lore raw

PATCH 6/7 cgroups: Add res counter common ancestor searching

In reply to Frederic Weisbecker

Add a new API to find the common ancestor between two resource counters. This includes the passed resource counter themselves.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/res_counter.h |    2 ++
 kernel/res_counter.c        |   19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 0 deletions(-)
include/linux/res_counter.h +2 -0
index 8c421ac..354ed30 100644
@@ -139,6 +139,8 @@ void res_counter_uncharge_until(struct res_counter *counter, struct res_counter
 				unsigned long val);
 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
 
+struct res_counter *res_counter_common_ancestor(struct res_counter *l, struct res_counter *r);
+
 /**
  * res_counter_margin - calculate chargeable space of a counter
  * @cnt: the counter
kernel/res_counter.c +19 -0
index 39f2513..1b2efd6 100644
@@ -102,6 +102,25 @@ void res_counter_uncharge(struct res_counter *counter, unsigned long val)
 	res_counter_uncharge_until(counter, NULL, val);
 }
 
+struct res_counter *
+res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
+{
+	struct res_counter *iter;
+
+	while (r1) {
+		iter = r2;
+		while (iter) {
+			if (iter == r1)
+				return iter;
+			iter = iter->parent;
+		}
+
+		r1 = r1->parent;
+	}
+
+	return NULL;
+}
+
 static inline unsigned long long *
 res_counter_member(struct res_counter *counter, int member)
 {
Signature · 1 line
1.7.5.4
Acked-by on v1 6/7
#15 lore raw

Re: [PATCH 6/7] cgroups: Add res counter common ancestor searching

Quoted 4 lines · Frederic Weisbecker

Add a new API to find the common ancestor between two resource counters. This includes the passed resource counter themselves.

There's something distasteful about doing an O(N^2) search on every charge/uncharge operation, but I guess in general N isn't going to be more than 2 or 3 so it's not worth optimizing.

If you wanted to avoid the N^2 search then you could add a "depth" field in struct res_counter, which would be initialized to parent->depth+1, and then easily get both r1 and r2 to the same depth before proceeding up the tree in lock step, but that would bloat all instances of res_counter, which might work out as more expensive in the long run.

Paul

Quoted 56 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 include/linux/res_counter.h |    2 ++ kernel/res_counter.c        |   19 +++++++++++++++++++

 2 files changed, 21 insertions(+), 0 deletions(-)
include/linux/res_counter.h +2 -0
index 8c421ac..354ed30 100644
@@ -139,6 +139,8 @@ void res_counter_uncharge_until(struct res_counter *counter, struct res_counter
                                unsigned long val);
  void res_counter_uncharge(struct res_counter *counter, unsigned long val);
 
+struct res_counter *res_counter_common_ancestor(struct res_counter *l, struct res_counter *r);
+
  /**
  * res_counter_margin - calculate chargeable space of a counter
  * @cnt: the counter
kernel/res_counter.c +19 -0
index 39f2513..1b2efd6 100644
@@ -102,6 +102,25 @@ void res_counter_uncharge(struct res_counter *counter, unsigned long val)
        res_counter_uncharge_until(counter, NULL, val);
  }
 
+struct res_counter *
+res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
+{
+       struct res_counter *iter;
+
+       while (r1) {
+               iter = r2;
+               while (iter) {
+                       if (iter == r1)
+                               return iter;
+                       iter = iter->parent;
+               }
+
+               r1 = r1->parent;
+       }
+
+       return NULL;
+}
+

 static inline unsigned long long * res_counter_member(struct res_counter *counter, int member) {
--
1.7.5.4

#16 lore raw

Re: [PATCH 6/7] cgroups: Add res counter common ancestor searching

Quoted 12 lines · Paul Menage
Quoted 4 lines · Frederic Weisbecker

Add a new API to find the common ancestor between two resource counters. This includes the passed resource counter themselves.

There's something distasteful about doing an O(N^2) search on every charge/uncharge operation, but I guess in general N isn't going to be more than 2 or 3 so it's not worth optimizing.

Quoted 6 lines

If you wanted to avoid the N^2 search then you could add a "depth" field in struct res_counter, which would be initialized to parent->depth+1, and then easily get both r1 and r2 to the same depth before proceeding up the tree in lock step, but that would bloat all instances of res_counter, which might work out as more expensive in the long run.

Yeah it only happens on slow paths and probably cgroups hierarchies don't tend to be so deep.

But if that's a problem we can still optimize the way you said, and perhaps move that depth field into the struct task_counter instead.

Thanks.

Quoted 59 lines

Paul

Quoted 56 lines
Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>

---
 include/linux/res_counter.h |    2 ++ kernel/res_counter.c        |   19 +++++++++++++++++++

 2 files changed, 21 insertions(+), 0 deletions(-)
include/linux/res_counter.h +2 -0
index 8c421ac..354ed30 100644
@@ -139,6 +139,8 @@ void res_counter_uncharge_until(struct res_counter *counter, struct res_counter
                                unsigned long val);
  void res_counter_uncharge(struct res_counter *counter, unsigned long val);
 
+struct res_counter *res_counter_common_ancestor(struct res_counter *l, struct res_counter *r);
+
  /**
  * res_counter_margin - calculate chargeable space of a counter
  * @cnt: the counter
kernel/res_counter.c +19 -0
index 39f2513..1b2efd6 100644
@@ -102,6 +102,25 @@ void res_counter_uncharge(struct res_counter *counter, unsigned long val)
        res_counter_uncharge_until(counter, NULL, val);
  }
 
+struct res_counter *
+res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
+{
+       struct res_counter *iter;
+
+       while (r1) {
+               iter = r2;
+               while (iter) {
+                       if (iter == r1)
+                               return iter;
+                       iter = iter->parent;
+               }
+
+               r1 = r1->parent;
+       }
+
+       return NULL;
+}
+

 static inline unsigned long long * res_counter_member(struct res_counter *counter, int member) {
--
1.7.5.4

#17 lore raw

PATCH 7/7 cgroups: Add a max number of tasks subsystem

In reply to Frederic Weisbecker

Add a new subsystem to limit the number of running tasks, similar to the NR_PROC rlimit but in the scope of a cgroup.

This is a step to be able to isolate a bit more a cgroup against the rest of the system and limit the global impact of a fork bomb inside a given cgroup.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Aditya Kali <[email protected]>
---
 include/linux/cgroup.h        |    9 ++
 include/linux/cgroup_subsys.h |    8 ++
 init/Kconfig                  |    7 ++
 kernel/Makefile               |    1 +
 kernel/cgroup_max_tasks.c     |  176 +++++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                 |    4 +
 6 files changed, 205 insertions(+), 0 deletions(-)
 create mode 100644 kernel/cgroup_max_tasks.c
include/linux/cgroup.h +9 -0
index 94454143..e6dec8c 100644
@@ -660,4 +660,13 @@ static inline int cgroup_attach_task_current_cg(struct task_struct *t)
 
 #endif /* !CONFIG_CGROUPS */
 
+#ifdef CONFIG_CGROUP_MAX_TASKS
+int cgroup_max_tasks_fork(struct task_struct *child);
+#else
+static inline int cgroup_max_tasks_fork(struct task_struct *child)
+{
+	return 0;
+}
+#endif /* CONFIG_CGROUP_MAX_TASKS */
+
 #endif /* _LINUX_CGROUP_H */
include/linux/cgroup_subsys.h +8 -0
index ac663c1..227540d 100644
@@ -59,8 +59,16 @@ SUBSYS(net_cls)
 SUBSYS(blkio)
 #endif
 
+/* */
+
 #ifdef CONFIG_CGROUP_PERF
 SUBSYS(perf)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_MAX_TASKS
+SUBSYS(max_tasks)
+#endif
+
+/* */
init/Kconfig +7 -0
index 412c21b..46e4c03 100644
@@ -690,6 +690,13 @@ config CGROUP_MEM_RES_CTLR_SWAP_ENABLED
 	  select this option (if, for some reason, they need to disable it
 	  then noswapaccount does the trick).
 
+config CGROUP_MAX_TASKS
+        bool "Control max number of tasks in a cgroup"
+	depends on RESOURCE_COUNTERS
+	help
+	  This option let the user to set up an upper bound allowed number
+	  of tasks.
+
 config CGROUP_PERF
 	bool "Enable perf_event per-cpu per-container group (cgroup) monitoring"
 	depends on PERF_EVENTS && CGROUPS
kernel/Makefile +1 -0
index 2d64cfc..1b5722e 100644
@@ -60,6 +60,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_FREEZER) += cgroup_freezer.o
+obj-$(CONFIG_CGROUP_MAX_TASKS) += cgroup_max_tasks.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
kernel/cgroup_max_tasks.c new file +176 -0

176 lines not on this page · raw message

kernel/fork.c +4 -0
index 0276c30..feca609 100644
@@ -1295,6 +1295,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	p->group_leader = p;
 	INIT_LIST_HEAD(&p->thread_group);
 
+	retval = cgroup_max_tasks_fork(p);
+	if (retval)
+		goto bad_fork_free_pid;
+
 	/* Now that the task is set up, run cgroup callbacks if
 	 * necessary. We need to run them before the task is visible
 	 * on the tasklist. */
Signature · 1 line
1.7.5.4
#18 lore raw

Re: [PATCH 7/7] cgroups: Add a max number of tasks subsystem

include/linux/cgroup_subsys.h SUBSYS(net_cls) · line 72

Quoted 5 lines · Frederic Weisbecker

 /* */

include/linux/cgroup_subsys.h +4 -0
+
+#ifdef CONFIG_CGROUP_MAX_TASKS
+SUBSYS(max_tasks)
+#endif

I think I'd be inclined to make the naming slightly (IMO) cleaner:
call the subsystem 'tasks' and the files 'limit' and 'count' (or maybe 'usage' to be more in line with memcg).

init/Kconfig config CGROUP_MEM_RES_CTLR_SWAP_ENABLED · line 699

Quoted 8 lines
init/Kconfig +7 -0
+config CGROUP_MAX_TASKS
+        bool "Control max number of tasks in a cgroup"
+       depends on RESOURCE_COUNTERS
+       help
+         This option let the user to set up an upper bound allowed number
+         of tasks.
+

Needs to depend on CGROUPS too?

kernel/cgroup_max_tasks.c line 18

Quoted 6 lines
kernel/cgroup_max_tasks.c new file +6 -0
+
+
+struct task_counter {
+       struct res_counter              res;
+       struct cgroup_subsys_state      css;
+};

All other CSS structures put the "css" field as the first thing in the structure. Not sure that anything relies on that, but consistency can't hurt and it probably makes the code fractionally smaller since the translation between CSS and task_counter becomes a no-op.

kernel/cgroup_max_tasks.c line 75

Quoted 8 lines
kernel/cgroup_max_tasks.c new file +8 -0
+static void task_counter_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                             struct cgroup *old_cgrp, struct task_struct *task)
+{
+       struct task_counter *cnt = cgroup_task_counter(old_cgrp);
+
+       if (cnt != &root_counter)
+               res_counter_uncharge_until(&cnt->res, &root_counter.res, 1);
+}

Do we even need the root_counter to be exposed in any way? Why not make children of the root cgroup just have a NULL parent res_counter?

You'll still need a task_counter object so that the cgroups framework has a CSS object for cgroups housekeeping, but I don't think it needs to actually be used for anything.

Paul

#19 lore raw

Re: [PATCH 7/7] cgroups: Add a max number of tasks subsystem

Quoted 11 lines · Paul Menage

include/linux/cgroup_subsys.h SUBSYS(net_cls) · line 72

Quoted 5 lines · Frederic Weisbecker

/* */

include/linux/cgroup_subsys.h +4 -0
+
+#ifdef CONFIG_CGROUP_MAX_TASKS
+SUBSYS(max_tasks)
+#endif

I think I'd be inclined to make the naming slightly (IMO) cleaner:
call the subsystem 'tasks' and the files 'limit' and 'count' (or maybe 'usage' to be more in line with memcg).

Quoted 1 line
Quoted 10 lines

init/Kconfig config CGROUP_MEM_RES_CTLR_SWAP_ENABLED · line 699

Quoted 7 lines
init/Kconfig +7 -0
+config CGROUP_MAX_TASKS
+        bool "Control max number of tasks in a cgroup"
+       depends on RESOURCE_COUNTERS
+       help
+         This option let the user to set up an upper bound allowed number
+         of tasks.
+

Needs to depend on CGROUPS too?

it's done implicitly, because it's inside "menuconfig CGROUPS".

Quoted 9 lines

kernel/cgroup_max_tasks.c line 18

Quoted 6 lines
kernel/cgroup_max_tasks.c new file +6 -0
+
+
+struct task_counter {
+       struct res_counter              res;
+       struct cgroup_subsys_state      css;
+};

All other CSS structures put the "css" field as the first thing in the structure. Not sure that anything relies on that, but consistency

no as far as I know, and that's a bad thing.

Quoted 21 lines

can't hurt and it probably makes the code fractionally smaller since the translation between CSS and task_counter becomes a no-op.

kernel/cgroup_max_tasks.c line 75

Quoted 8 lines
kernel/cgroup_max_tasks.c new file +8 -0
+static void task_counter_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                             struct cgroup *old_cgrp, struct task_struct *task)
+{
+       struct task_counter *cnt = cgroup_task_counter(old_cgrp);
+
+       if (cnt != &root_counter)
+               res_counter_uncharge_until(&cnt->res, &root_counter.res, 1);
+}

Do we even need the root_counter to be exposed in any way? Why not make children of the root cgroup just have a NULL parent res_counter?

You'll still need a task_counter object so that the cgroups framework has a CSS object for cgroups housekeeping, but I don't think it needs to actually be used for anything.

Paul

#20 lore raw

Re: [PATCH 7/7] cgroups: Add a max number of tasks subsystem

In reply to Paul Menage
Quoted 10 lines · Paul Menage

include/linux/cgroup_subsys.h SUBSYS(net_cls) · line 72

Quoted 5 lines · Frederic Weisbecker

 /* */

include/linux/cgroup_subsys.h +4 -0
+
+#ifdef CONFIG_CGROUP_MAX_TASKS
+SUBSYS(max_tasks)
+#endif

I think I'd be inclined to make the naming slightly (IMO) cleaner:
call the subsystem 'tasks' and the files 'limit' and 'count' (or maybe 'usage' to be more in line with memcg).

Sure. Will rename.

Quoted 11 lines

init/Kconfig config CGROUP_MEM_RES_CTLR_SWAP_ENABLED · line 699

Quoted 8 lines
init/Kconfig +7 -0
+config CGROUP_MAX_TASKS
+        bool "Control max number of tasks in a cgroup"
+       depends on RESOURCE_COUNTERS
+       help
+         This option let the user to set up an upper bound allowed number
+         of tasks.
+

Needs to depend on CGROUPS too?

Quoted 11 lines

kernel/cgroup_max_tasks.c line 18

Quoted 6 lines
kernel/cgroup_max_tasks.c new file +6 -0
+
+
+struct task_counter {
+       struct res_counter              res;
+       struct cgroup_subsys_state      css;
+};

All other CSS structures put the "css" field as the first thing in the structure. Not sure that anything relies on that, but consistency can't hurt and it probably makes the code fractionally smaller since the translation between CSS and task_counter becomes a no-op.

I guess Li responded to the two above points.

Quoted 12 lines

kernel/cgroup_max_tasks.c line 75

Quoted 8 lines
kernel/cgroup_max_tasks.c new file +8 -0
+static void task_counter_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                             struct cgroup *old_cgrp, struct task_struct *task)
+{
+       struct task_counter *cnt = cgroup_task_counter(old_cgrp);
+
+       if (cnt != &root_counter)
+               res_counter_uncharge_until(&cnt->res, &root_counter.res, 1);
+}

Do we even need the root_counter to be exposed in any way? Why not make children of the root cgroup just have a NULL parent res_counter?

Quoted 5 lines

You'll still need a task_counter object so that the cgroups framework has a CSS object for cgroups housekeeping, but I don't think it needs to actually be used for anything.

Paul

Right, will try to get that.

Thanks.

#21 lore raw
In reply to Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker

Hi all,

Changes in this release are:

* switch from general rlim subsystem to self-contained max number of tasks subsystem

* Don't use subsys->fork() callback but a static call to avoid cgroups complications.

Quoted 8 lines

* When moving a task accross cgroups, don't charge/uncharge the common ancestors, to fix race against concurrent forks.
To fix that, Paul proposed a res_counter_move_charge() that do all in once. But we need to be able to cancel the attachment in case a following subsystem refuses the attachment itself. Thus the charge and uncharge still need to be seperate. More specifically, we can only uncharge the old cgroup once we are sure of the attachment. Because we can't cancel that uncharge later due to possible forks in the middle.

Ah and following Li's suggestion, the root cgroup is not touched anymore. Considering this feature is to isolate a bit cgroups local impact from the rest of the system, we don't need to limit in the global scope. And we already have ways to do it.

So we avoid the overhead of this subsystem for the trivial (only one cgroup, root) case. Although I believe that cgroups are more and more used and the trivial case is moving toward using more cgroups :)

Also Li suggested I use atomic ops rather than res counter. I'm still not sure I can really do that as it exposes the counters with possibly temporary overloaded values in the fail case.

Thanks.

Quoted 32 lines

Thanks.

Frederic Weisbecker (7):
cgroups: Add res_counter_write_u64() API
cgroups: New resource counter inheritance API
cgroups: Add previous cgroup in can_attach_task/attach_task callbacks
cgroups: New cancel_attach_task subsystem callback
cgroups: Ability to stop res charge propagation on bounded ancestor
cgroups: Add res counter common ancestor searching
cgroups: Add a max number of tasks subsystem

 block/blk-cgroup.c            |   10 ++-
 include/linux/cgroup.h        |   15 +++-
 include/linux/cgroup_subsys.h |    8 ++
 include/linux/res_counter.h   |   12 +++
 init/Kconfig                  |    7 ++
 kernel/Makefile               |    1 +
 kernel/cgroup.c               |   25 ++++--
 kernel/cgroup_freezer.c       |    3 +-
 kernel/cgroup_max_tasks.c     |  176 +++++++++++++++++++++++++++++++++++++++++
 kernel/cpuset.c               |    6 +-
 kernel/events/core.c          |    5 +-
 kernel/fork.c                 |    4 +
 kernel/res_counter.c          |   88 ++++++++++++++++++---
 kernel/sched.c                |    6 +-
 14 files changed, 335 insertions(+), 31 deletions(-)
 create mode 100644 kernel/cgroup_max_tasks.c
Signature · 1 line
1.7.5.4