Skip to contentSkip to conversation

cgroup: add PIDs subsystem

PATCH Superseded Aleksa Sarai 2 participants 12 messages Jun 5, 2015 – Jun 9, 2015

Subsystems (4)

v13 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v13 cover cgroup: add PIDs subsystem

This is a small update to v12 of the pids patchset[1] (most of which was merged into Tejun's tree, but was later reverted due to some bogus GCC warnings). The main changes are:

* Fix up include/linux/cgroup.h changes to take into account Tejun's refactoring of cgroup.h into cgroup-defs.h.

* Move ->can_attach() and ->cancel_attach() charge/revert into ->attach(), since we can't fail and it's simpler to just charge there.

* Remove tset_get_css(), because the premise of its existence is flawed and switch ->attach() code to just task_get_css() itself.

* Remove a patch that was rendered uneccesary by the cgroup.h refactor.

* Fix up the for_each_subsys_which patchset so that it doesn't trigger a GCC warning when CGROUP_SUBSYS_COUNT = 0, but cgroups are enabled. The warning is bogus anyway, since the whole block becomes a noop in that case.

[1]: https://lkml.org/lkml/2015/5/18/439

Aleksa Sarai (4):
cgroup: use bitmask to filter for_each_subsys
cgroup: replace explicit ss_mask checking with for_each_subsys_which
cgroup: allow a cgroup subsystem to reject a fork
cgroup: implement the PIDs subsystem

Tejun Heo (1):
cgroup, block: implement task_get_css()

 CREDITS                       |   5 +
 include/linux/cgroup-defs.h   |  12 +-
 include/linux/cgroup.h        |  41 ++++-
 include/linux/cgroup_subsys.h |  28 ++++
 init/Kconfig                  |  16 ++
 kernel/Makefile               |   1 +
 kernel/cgroup.c               | 171 +++++++++++++------
 kernel/cgroup_freezer.c       |   2 +-
 kernel/cgroup_pids.c          | 377 ++++++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                 |  17 +-
 kernel/sched/core.c           |   2 +-
 11 files changed, 616 insertions(+), 56 deletions(-)
 create mode 100644 kernel/cgroup_pids.c
Signature · 1 line
2.4.2
#1 lore raw

PATCH v13 1/5 cgroup: use bitmask to filter for_each_subsys

Add a new macro for_each_subsys_which that allows all enabled cgroup subsystems to be filtered by a bitmask, such that mask & (1 << ssid) determines if the subsystem is to be processed in the loop body (where ssid is the unique id of the subsystem).

Also replace the need_forkexit_callback with two separate bitmasks for each callback to make (ss->{fork,exit}) checks unnecessary.

---
 include/linux/cgroup-defs.h |  2 ++
 kernel/cgroup.c             | 54 ++++++++++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 20 deletions(-)
include/linux/cgroup-defs.h +2 -0
index 26d1cea..c5588c4 100644
@@ -490,6 +490,8 @@ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
 
 #else	/* CONFIG_CGROUPS */
 
+#define CGROUP_SUBSYS_COUNT 0
+
 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) {}
 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
 
kernel/cgroup.c +34 -20

86 lines not on this page · raw message

Signature · 1 line
2.4.2
#2 lore raw

PATCH v13 2/5 cgroup: replace explicit ss_mask checking with for_each_subsys_which

In reply to Aleksa Sarai

Replace the explicit checking against ss_masks inside a for_each_subsys block with for_each_subsys_which(..., ss_mask), to take advantage of the more readable (and more efficient) macro.

---
 kernel/cgroup.c | 44 ++++++++++++++++----------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
kernel/cgroup.c +16 -28
index 5734717..c5400d20 100644
@@ -1107,9 +1107,8 @@ static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
 	while (true) {
 		unsigned long new_ss_mask = cur_ss_mask;
 
-		for_each_subsys(ss, ssid)
-			if (cur_ss_mask & (1 << ssid))
-				new_ss_mask |= ss->depends_on;
+		for_each_subsys_which(ss, ssid, &cur_ss_mask)
+			new_ss_mask |= ss->depends_on;
 
 		/*
 		 * Mask out subsystems which aren't available.  This can
@@ -1247,10 +1246,7 @@ static int rebind_subsystems(struct cgroup_root *dst_root,
 
 	lockdep_assert_held(&cgroup_mutex);
 
-	for_each_subsys(ss, ssid) {
-		if (!(ss_mask & (1 << ssid)))
-			continue;
-
+	for_each_subsys_which(ss, ssid, &ss_mask) {
 		/* if @ss has non-root csses attached to it, can't move */
 		if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
 			return -EBUSY;
@@ -1287,18 +1283,14 @@ static int rebind_subsystems(struct cgroup_root *dst_root,
 	 * Nothing can fail from this point on.  Remove files for the
 	 * removed subsystems and rebind each subsystem.
 	 */
-	for_each_subsys(ss, ssid)
-		if (ss_mask & (1 << ssid))
-			cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
+	for_each_subsys_which(ss, ssid, &ss_mask)
+		cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
 
-	for_each_subsys(ss, ssid) {
+	for_each_subsys_which(ss, ssid, &ss_mask) {
 		struct cgroup_root *src_root;
 		struct cgroup_subsys_state *css;
 		struct css_set *cset;
 
-		if (!(ss_mask & (1 << ssid)))
-			continue;
-
 		src_root = ss->root;
 		css = cgroup_css(&src_root->cgrp, ss);
 
@@ -2557,13 +2549,11 @@ static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
 	bool printed = false;
 	int ssid;
 
-	for_each_subsys(ss, ssid) {
-		if (ss_mask & (1 << ssid)) {
-			if (printed)
-				seq_putc(seq, ' ');
-			seq_printf(seq, "%s", ss->name);
-			printed = true;
-		}
+	for_each_subsys_which(ss, ssid, &ss_mask) {
+		if (printed)
+			seq_putc(seq, ' ');
+		seq_printf(seq, "%s", ss->name);
+		printed = true;
 	}
 	if (printed)
 		seq_putc(seq, '\n');
@@ -2705,11 +2695,12 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
 	 */
 	buf = strstrip(buf);
 	while ((tok = strsep(&buf, " "))) {
+		unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
+
 		if (tok[0] == '\0')
 			continue;
-		for_each_subsys(ss, ssid) {
-			if (ss->disabled || strcmp(tok + 1, ss->name) ||
-			    ((1 << ss->id) & cgrp_dfl_root_inhibit_ss_mask))
+		for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
+			if (ss->disabled || strcmp(tok + 1, ss->name))
 				continue;
 
 			if (*tok == '+') {
@@ -2796,10 +2787,7 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
 	 * still around.  In such cases, wait till it's gone using
 	 * offline_waitq.
 	 */
-	for_each_subsys(ss, ssid) {
-		if (!(css_enable & (1 << ssid)))
-			continue;
-
+	for_each_subsys_which(ss, ssid, &css_enable) {
 		cgroup_for_each_live_child(child, cgrp) {
 			DEFINE_WAIT(wait);
 
Signature · 1 line
2.4.2
maintainer
#3 lore raw

Re: [PATCH v13 2/5] cgroup: replace explicit ss_mask checking with for_each_subsys_which

Quoted 5 lines · Aleksa Sarai

Replace the explicit checking against ss_masks inside a for_each_subsys block with for_each_subsys_which(..., ss_mask), to take advantage of the more readable (and more efficient) macro.

Applied 1-2 to cgroup/for-4.2.

Thanks.

Signature · 1 line
tejun
#4 lore raw

PATCH v13 3/5 cgroup, block: implement task_get_css()

In reply to Aleksa Sarai

From: Tejun Heo <[email protected]>

Implement task_get_css() which find and pins the css for the specified task and subsys. As a task is always associated with an online css for every subsystem except while the css_set update is propagating, task_get_css() retries till css_tryget_online() succeeds.

---
 include/linux/cgroup.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/cgroup.h +25 -0
index 82319fb..a593e29 100644
@@ -354,6 +354,31 @@ static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
 }
 
 /**
+ * task_get_css - find and get the css for (task, subsys)
+ * @task: the target task
+ * @subsys_id: the target subsystem ID
+ *
+ * Find the css for the (@task, @subsys_id) combination, increment a
+ * reference on and return it.  This function is guaranteed to return a
+ * valid css.
+ */
+static inline struct cgroup_subsys_state *
+task_get_css(struct task_struct *task, int subsys_id)
+{
+	struct cgroup_subsys_state *css;
+
+	rcu_read_lock();
+	while (true) {
+		css = task_css(task, subsys_id);
+		if (likely(css_tryget_online(css)))
+			break;
+		cpu_relax();
+	}
+	rcu_read_unlock();
+	return css;
+}
+
+/**
  * task_css_is_root - test whether a task belongs to the root css
  * @task: the target task
  * @subsys_id: the target subsystem ID
Signature · 1 line
2.4.2
maintainer
#5 lore raw

Re: [PATCH v13 3/5] cgroup, block: implement task_get_css()

Quoted 9 lines · Aleksa Sarai

From: Tejun Heo <[email protected]>

Implement task_get_css() which find and pins the css for the specified task and subsys. As a task is always associated with an online css for every subsystem except while the css_set update is propagating, task_get_css() retries till css_tryget_online() succeeds.

Another form of this patch was already in block/for-4.2/writeback. I cherry picked that commit into cgroup/for-4.2 to avoid pulling the whole thing into cgroup tree.

Thanks.

Signature · 1 line
tejun
#6 lore raw

PATCH v13 4/5 cgroup: allow a cgroup subsystem to reject a fork

In reply to Aleksa Sarai

Add a new cgroup subsystem callback can_fork that conditionally states whether or not the fork is accepted or rejected by a cgroup policy. In addition, add a cancel_fork callback so that if an error occurs later in the forking process, any state modified by can_fork can be reverted.

Allow for a private opaque pointer to be passed from cgroup_can_fork to cgroup_post_fork, allowing for the fork state to be stored by each subsystem separately.

Also add a tagging system for cgroup_subsys.h to allow for CGROUP_<TAG> enumerations to be be defined and used. In addition, explicitly add a CGROUP_CANFORK_COUNT macro to make arrays easier to define.

This is in preparation for implementing the pids cgroup subsystem.

---
 include/linux/cgroup-defs.h   | 10 +++++-
 include/linux/cgroup.h        | 16 +++++++--
 include/linux/cgroup_subsys.h | 23 +++++++++++++
 kernel/cgroup.c               | 75 +++++++++++++++++++++++++++++++++++++++++--
 kernel/cgroup_freezer.c       |  2 +-
 kernel/fork.c                 | 17 ++++++++--
 kernel/sched/core.c           |  2 +-
 7 files changed, 136 insertions(+), 9 deletions(-)
include/linux/cgroup-defs.h +9 -1
index c5588c4..2009ceb 100644
@@ -34,12 +34,17 @@ struct seq_file;
 
 /* define the enumeration of all cgroup subsystems */
 #define SUBSYS(_x) _x ## _cgrp_id,
+#define SUBSYS_TAG(_t) CGROUP_ ## _t, \
+	__unused_tag_ ## _t = CGROUP_ ## _t - 1,
 enum cgroup_subsys_id {
 #include <linux/cgroup_subsys.h>
 	CGROUP_SUBSYS_COUNT,
 };
+#undef SUBSYS_TAG
 #undef SUBSYS
 
+#define CGROUP_CANFORK_COUNT (CGROUP_CANFORK_END - CGROUP_CANFORK_START)
+
 /* bits in struct cgroup_subsys_state flags field */
 enum {
 	CSS_NO_REF	= (1 << 0), /* no reference counting for this css */
@@ -405,7 +410,9 @@ struct cgroup_subsys {
 			      struct cgroup_taskset *tset);
 	void (*attach)(struct cgroup_subsys_state *css,
 		       struct cgroup_taskset *tset);
-	void (*fork)(struct task_struct *task);
+	int (*can_fork)(struct task_struct *task, void **priv_p);
+	void (*cancel_fork)(struct task_struct *task, void *priv);
+	void (*fork)(struct task_struct *task, void *priv);
 	void (*exit)(struct cgroup_subsys_state *css,
 		     struct cgroup_subsys_state *old_css,
 		     struct task_struct *task);
@@ -490,6 +497,7 @@ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
 
 #else	/* CONFIG_CGROUPS */
 
+#define CGROUP_CANFORK_COUNT 0
 #define CGROUP_SUBSYS_COUNT 0
 
 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) {}
include/linux/cgroup.h +14 -2
index a593e29..17d0046 100644
@@ -62,9 +62,15 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
 		     struct pid *pid, struct task_struct *tsk);
 
 void cgroup_fork(struct task_struct *p);
-void cgroup_post_fork(struct task_struct *p);
+extern int cgroup_can_fork(struct task_struct *p,
+			   void *ss_priv[CGROUP_CANFORK_COUNT]);
+extern void cgroup_cancel_fork(struct task_struct *p,
+			       void *ss_priv[CGROUP_CANFORK_COUNT]);
+extern void cgroup_post_fork(struct task_struct *p,
+			     void *old_ss_priv[CGROUP_CANFORK_COUNT]);
 void cgroup_exit(struct task_struct *p);
 
+
 int cgroup_init_early(void);
 int cgroup_init(void);
 
@@ -524,7 +530,13 @@ static inline int cgroupstats_build(struct cgroupstats *stats,
 				    struct dentry *dentry) { return -EINVAL; }
 
 static inline void cgroup_fork(struct task_struct *p) {}
-static inline void cgroup_post_fork(struct task_struct *p) {}
+static inline int cgroup_can_fork(struct task_struct *p,
+				  void *ss_priv[CGROUP_CANFORK_COUNT])
+{ return 0; }
+static inline void cgroup_cancel_fork(struct task_struct *p,
+				      void *ss_priv[CGROUP_CANFORK_COUNT]) {}
+static inline void cgroup_post_fork(struct task_struct *p,
+				    void *ss_priv[CGROUP_CANFORK_COUNT]) {}
 static inline void cgroup_exit(struct task_struct *p) {}
 
 static inline int cgroup_init_early(void) { return 0; }
include/linux/cgroup_subsys.h +23 -0
index e4a96fb..ec43bce 100644
@@ -3,6 +3,17 @@
  *
  * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS.
  */
+
+/*
+ * This file *must* be included with SUBSYS() defined.
+ * SUBSYS_TAG() is a noop if undefined.
+ */
+
+#ifndef SUBSYS_TAG
+#define __TMP_SUBSYS_TAG
+#define SUBSYS_TAG(_x)
+#endif
+
 #if IS_ENABLED(CONFIG_CPUSETS)
 SUBSYS(cpuset)
 #endif
@@ -48,11 +59,23 @@ SUBSYS(hugetlb)
 #endif
 
 /*
+ * Subsystems that implement the can_fork() family of callbacks.
+ */
+SUBSYS_TAG(CANFORK_START)
+SUBSYS_TAG(CANFORK_END)
+
+/*
  * The following subsystems are not supported on the default hierarchy.
  */
 #if IS_ENABLED(CONFIG_CGROUP_DEBUG)
 SUBSYS(debug)
 #endif
+
+#ifdef __TMP_SUBSYS_TAG
+#undef __TMP_SUBSYS_TAG
+#undef SUBSYS_TAG
+#endif
+
 /*
  * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS.
  */
kernel/cgroup.c +73 -2

111 lines not on this page · raw message

kernel/cgroup_freezer.c +1 -1
index 92b98cc..f1b30ad 100644
@@ -203,7 +203,7 @@ static void freezer_attach(struct cgroup_subsys_state *new_css,
  * to do anything as freezer_attach() will put @task into the appropriate
  * state.
  */
-static void freezer_fork(struct task_struct *task)
+static void freezer_fork(struct task_struct *task, void *private)
 {
 	struct freezer *freezer;
 
kernel/fork.c +15 -2
index 9531275..de7ff51 100644
@@ -1241,6 +1241,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 {
 	int retval;
 	struct task_struct *p;
+	void *cgrp_ss_priv[CGROUP_CANFORK_COUNT] = {};
 
 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
 		return ERR_PTR(-EINVAL);
@@ -1512,6 +1513,16 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	p->task_works = NULL;
 
 	/*
+	 * Ensure that the cgroup subsystem policies allow the new process to be
+	 * forked. It should be noted the the new process's css_set can be changed
+	 * between here and cgroup_post_fork() if an organisation operation is in
+	 * progress.
+	 */
+	retval = cgroup_can_fork(p, cgrp_ss_priv);
+	if (retval)
+		goto bad_fork_free_pid;
+
+	/*
 	 * Make it visible to the rest of the system, but dont wake it up yet.
 	 * Need tasklist lock for parent etc handling!
 	 */
@@ -1547,7 +1558,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 		spin_unlock(&current->sighand->siglock);
 		write_unlock_irq(&tasklist_lock);
 		retval = -ERESTARTNOINTR;
-		goto bad_fork_free_pid;
+		goto bad_fork_cancel_cgroup;
 	}
 
 	if (likely(p->pid)) {
@@ -1589,7 +1600,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	write_unlock_irq(&tasklist_lock);
 
 	proc_fork_connector(p);
-	cgroup_post_fork(p);
+	cgroup_post_fork(p, cgrp_ss_priv);
 	if (clone_flags & CLONE_THREAD)
 		threadgroup_change_end(current);
 	perf_event_fork(p);
@@ -1599,6 +1610,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
 	return p;
 
+bad_fork_cancel_cgroup:
+	cgroup_cancel_fork(p, cgrp_ss_priv);
 bad_fork_free_pid:
 	if (pid != &init_struct_pid)
 		free_pid(pid);
kernel/sched/core.c +1 -1
index f9123a8..050936e 100644
@@ -8007,7 +8007,7 @@ static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
 	sched_offline_group(tg);
 }
 
-static void cpu_cgroup_fork(struct task_struct *task)
+static void cpu_cgroup_fork(struct task_struct *task, void *private)
 {
 	sched_move_task(task);
 }
Signature · 1 line
2.4.2
maintainer
#7 lore raw

Re: [PATCH v13 4/5] cgroup: allow a cgroup subsystem to reject a fork

Hello, Aleksa.

Looks pretty good to me in general. Some minor comments below.

Quoted 8 lines · Aleksa Sarai
include/linux/cgroup.h +0 -0
index a593e29..17d0046 100644
@@ -62,9 +62,15 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
                     struct pid *pid, struct task_struct *tsk);
 
 void cgroup_fork(struct task_struct *p);

include/linux/cgroup.h int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, · line 73

Quoted 10 lines
include/linux/cgroup.h +7 -1
-void cgroup_post_fork(struct task_struct *p);
+extern int cgroup_can_fork(struct task_struct *p,
+                          void *ss_priv[CGROUP_CANFORK_COUNT]);
+extern void cgroup_cancel_fork(struct task_struct *p,
+                              void *ss_priv[CGROUP_CANFORK_COUNT]);
+extern void cgroup_post_fork(struct task_struct *p,
+                            void *old_ss_priv[CGROUP_CANFORK_COUNT]);
 void cgroup_exit(struct task_struct *p);
 
+

Is this blank line intentional?

Quoted 2 lines
include/linux/cgroup.h +0 -0
 int cgroup_init_early(void);
 int cgroup_init(void);

...

include/linux/cgroup.h static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 4930

Quoted 5 lines
include/linux/cgroup.h +1 -0
@@ -4924,6 +4927,7 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
 
 	have_fork_callback |= (bool)ss->fork << ss->id;
 	have_exit_callback |= (bool)ss->exit << ss->id;
+	have_canfork_callback |= (bool)ss->can_fork << ss->id;

Hmmm.... do we still need this mask? We're already restricting iteration pretty heavily. I'd even suggest dropping both have_fork_callback and have_exit_callback too and just put them inside CGROUP_FORK_EXIT_START / STOP although that doesn't belong in this patchset.

...

include/linux/cgroup.h static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)

Quoted 7 lines
include/linux/cgroup.h +7 -0
+static void *subsys_canfork_priv(void *ss_priv[CGROUP_CANFORK_COUNT], int i)
+{
+	void **private;
+	if ((private = subsys_canfork_priv_p(ss_priv, i)) != NULL)
+		return *private;
+	return NULL;
+}
	void **private = subsys_canfork...;

	if (private)
		return *private;
	return NULL;

or even just

	return private ? *private : NULL;

We conventionally don't put assignments in if conditionals.

include/linux/cgroup.h

Quoted 8 lines
include/linux/cgroup.h +8 -0
+void cgroup_cancel_fork(struct task_struct *child,
+			void *ss_priv[CGROUP_CANFORK_COUNT])
+{
+	struct cgroup_subsys *ss;
+	int i;
+
+	for_each_subsys(ss, i)
+		if(ss->cancel_fork)
                  ^
		  space

include/linux/cgroup.h

Quoted 7 lines
include/linux/cgroup.h +4 -0
+			ss->cancel_fork(child, subsys_canfork_priv(ss_priv, i));
+}
+
+/**
  * cgroup_post_fork - called on a new task after adding it to the task list
  * @child: the task in question
  *

Thanks.

Signature · 1 line
tejun
#8 lore raw

Re: [PATCH v13 4/5] cgroup: allow a cgroup subsystem to reject a fork

Quoted 8 lines

kernel/cgroup.c static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 4930

Quoted 5 lines
kernel/cgroup.c +1 -0
@@ -4924,6 +4927,7 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
 
      have_fork_callback |= (bool)ss->fork << ss->id;
      have_exit_callback |= (bool)ss->exit << ss->id;
+     have_canfork_callback |= (bool)ss->can_fork << ss->id;

Hmmm.... do we still need this mask? We're already restricting iteration pretty heavily.

CGROUP_CANFORK_{START,END,COUNT} aren't used to restrict the iteration. They're used for restricting the size of the @ss_priv array. If you want, I can use CANFORK_{START,END} to restrict the iteration -- I just prefer using the for_each_subsys_which API for iterating over active cgroups. :/

--
Aleksa Sarai (cyphar)
www.cyphar.com

maintainer
#9 lore raw

Re: [PATCH v13 4/5] cgroup: allow a cgroup subsystem to reject a fork

Hello,

Quoted 5 lines · Aleksa Sarai

CGROUP_CANFORK_{START,END,COUNT} aren't used to restrict the iteration. They're used for restricting the size of the @ss_priv array. If you want, I can use CANFORK_{START,END} to restrict the iteration -- I just prefer using the for_each_subsys_which API for iterating over active cgroups. :/

Ah, I see. Hmm... yeah, let's keep the code for now, but I think it'd be cheaper / cleaner to use those macro tags to restrict iteration and drop all the masks in the future.

Thanks.

Signature · 1 line
tejun
#10 lore raw

PATCH v13 5/5 cgroup: implement the PIDs subsystem

In reply to Aleksa Sarai

Adds a new single-purpose PIDs subsystem to limit the number of tasks that can be forked inside a cgroup. Essentially this is an implementation of RLIMIT_NPROC that applies to a cgroup rather than a process tree.

However, it should be noted that organisational operations (adding and removing tasks from a PIDs hierarchy) will *not* be prevented. Rather, the number of tasks in the hierarchy cannot exceed the limit through forking. This is due to the fact that, in the unified hierarchy, attach cannot fail (and it is not possible for a task to overcome its PIDs cgroup policy limit by attaching to a child cgroup -- even if migrating mid-fork it must be able to fork in the parent first).

PIDs are fundamentally a global resource, and it is possible to reach PID exhaustion inside a cgroup without hitting any reasonable kmemcg policy. Once you've hit PID exhaustion, you're only in a marginally better state than OOM. This subsystem allows PID exhaustion inside a cgroup to be prevented.

---
 CREDITS                       |   5 +
 include/linux/cgroup_subsys.h |   5 +
 init/Kconfig                  |  16 ++
 kernel/Makefile               |   1 +
 kernel/cgroup_pids.c          | 377 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 404 insertions(+)
 create mode 100644 kernel/cgroup_pids.c
CREDITS +5 -0
index 40cc4bf..0727426 100644
@@ -3215,6 +3215,11 @@ S: 69 rue Dunois
 S: 75013 Paris
 S: France
 
+N: Aleksa Sarai
+E: [email protected]
+W: https://www.cyphar.com/
+D: `pids` cgroup subsystem
+
 N: Dipankar Sarma
 E: [email protected]
 D: RCU
include/linux/cgroup_subsys.h +5 -0
index ec43bce..1f36945 100644
@@ -62,6 +62,11 @@ SUBSYS(hugetlb)
  * Subsystems that implement the can_fork() family of callbacks.
  */
 SUBSYS_TAG(CANFORK_START)
+
+#if IS_ENABLED(CONFIG_CGROUP_PIDS)
+SUBSYS(pids)
+#endif
+
 SUBSYS_TAG(CANFORK_END)
 
 /*
init/Kconfig +16 -0
index b9b824b..f4e4918 100644
@@ -968,6 +968,22 @@ config CGROUP_FREEZER
 	  Provides a way to freeze and unfreeze all tasks in a
 	  cgroup.
 
+config CGROUP_PIDS
+	bool "PIDs cgroup subsystem"
+	help
+	  Provides enforcement of process number limits in the scope of a
+	  cgroup. Any attempt to fork more processes than is allowed in the
+	  cgroup will fail. PIDs are fundamentally a global resource because it
+	  is fairly trivial to reach PID exhaustion before you reach even a
+	  conservative kmemcg limit. As a result, it is possible to grind a
+	  system to halt without being limited by other cgroup policies. The
+	  PIDs cgroup subsystem is designed to stop this from happening.
+
+	  It should be noted that organisational operations (such as attaching
+	  to a cgroup hierarchy will *not* be blocked by the PIDs subsystem),
+	  since the PIDs limit only affects a process's ability to fork, not to
+	  attach to a cgroup.
+
 config CGROUP_DEVICE
 	bool "Device controller for cgroups"
 	help
kernel/Makefile +1 -0
index 0f8f8b0..df5406c 100644
@@ -55,6 +55,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_PIDS) += cgroup_pids.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
kernel/cgroup_pids.c new file +377 -0

377 lines not on this page · raw message

Signature · 1 line
2.4.2
maintainer
#11 lore raw

Re: [PATCH v13 5/5] cgroup: implement the PIDs subsystem

Hello, Aleksa.

On Sat, Jun 06, 2015 at 10:02:18AM +1000, Aleksa Sarai wrote:
...

Quoted 9 lines

+/*
+ * Lockless hierarchical accounting (with enforceable limits) derived from
+ * mm/page_counter.c. Original copyright notice from page_counter code:
+ *
+ * -------------------------------------------------
+ * Lockless hierarchical page accounting & limiting
+ * Copyright (C) 2014 Red Hat, Inc., Johannes Weiner
+ * -------------------------------------------------
+ */

I don't think we need the above copyright notice.

...

kernel/cgroup_pids.c line 375

Quoted 11 lines
kernel/cgroup_pids.c new file +11 -0
+struct cgroup_subsys pids_cgrp_subsys = {
+	.css_alloc	= pids_css_alloc,
+	.css_free	= pids_css_free,
+	.attach		= pids_attach,
+	.can_attach 	= pids_can_attach,
+	.cancel_attach 	= pids_cancel_attach,
+	.can_fork	= pids_can_fork,
+	.cancel_fork	= pids_cancel_fork,
+	.fork		= pids_fork,
+	.exit		= pids_exit,
+	.legacy_cftypes	= files,

Please also init dfl_cftypes the same way too. Also, can you please rename files to pids_files?

kernel/cgroup_pids.c line 376

Quoted 1 line
kernel/cgroup_pids.c new file +1 -0
+	.early_init	= 0,

And the above can be dropped.

Other than the above minor points, everything looks good to me but I think we're a bit too close to the merge window to put this in now. Once you update patch 4 and 5 according to the review, I'll apply them after the next rc1 drops.

Thanks.

Signature · 1 line
tejun