Skip to contentSkip to conversation

cgroups: Start a basic rlimit subsystem

RFC PATCH In review Frederic Weisbecker 4 participants 21 messages Jun 19, 2011 – Jul 6, 2011

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

cover cgroups: Start a basic rlimit subsystem

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

The thing is further expandable to host more general resource limitations in the scope of a cgroup.

Frederic Weisbecker (4):
cgroups: Allow a cgroup subsys to reject a fork
cgroups: Add res_counter_write_u64() API
cgroups: New resource counter inheritance API
cgroups: Add an rlimit subsystem

 include/linux/cgroup.h        |    9 ++-
 include/linux/cgroup_subsys.h |    8 ++
 include/linux/res_counter.h   |    4 +
 init/Kconfig                  |    6 ++
 kernel/Makefile               |    1 +
 kernel/cgroup.c               |   13 +++-
 kernel/cgroup_freezer.c       |    6 +-
 kernel/cgroup_rlim.c          |  142 +++++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                 |    5 +-
 kernel/res_counter.c          |   46 ++++++++++++--
 10 files changed, 225 insertions(+), 15 deletions(-)
 create mode 100644 kernel/cgroup_rlim.c
Signature · 1 line
1.7.5.4
#1 lore raw

RFC PATCH 1/4 cgroups: Allow a cgroup subsys to reject a fork

Make the cgroup subsystem fork callback return a value so that subsystems are able to accept or reject a fork completion with a custom error value.

This prepares for implementing rlimit in cgroups scope.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 include/linux/cgroup.h  |    9 ++++++---
 kernel/cgroup.c         |   13 ++++++++++---
 kernel/cgroup_freezer.c |    6 ++++--
 kernel/fork.c           |    5 ++++-
 4 files changed, 24 insertions(+), 9 deletions(-)
include/linux/cgroup.h +6 -3
index ab4ac0c..07213af 100644
@@ -32,7 +32,7 @@ extern int cgroup_lock_is_held(void);
 extern bool cgroup_lock_live_group(struct cgroup *cgrp);
 extern void cgroup_unlock(void);
 extern void cgroup_fork(struct task_struct *p);
-extern void cgroup_fork_callbacks(struct task_struct *p);
+extern int cgroup_fork_callbacks(struct task_struct *p);
 extern void cgroup_post_fork(struct task_struct *p);
 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
 extern int cgroupstats_build(struct cgroupstats *stats,
@@ -475,7 +475,7 @@ struct cgroup_subsys {
 	void (*attach_task)(struct cgroup *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);
+	int (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
 	void (*exit)(struct cgroup_subsys *ss, struct cgroup *cgrp,
 			struct cgroup *old_cgrp, struct task_struct *task);
 	int (*populate)(struct cgroup_subsys *ss,
@@ -633,7 +633,10 @@ struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
 static inline int cgroup_init_early(void) { return 0; }
 static inline int cgroup_init(void) { return 0; }
 static inline void cgroup_fork(struct task_struct *p) {}
-static inline void cgroup_fork_callbacks(struct task_struct *p) {}
+static inline int cgroup_fork_callbacks(struct task_struct *p)
+{
+	return 0;
+}
 static inline void cgroup_post_fork(struct task_struct *p) {}
 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
 
kernel/cgroup.c +10 -3
index 2731d11..688be3d 100644
@@ -4514,10 +4514,12 @@ void cgroup_fork(struct task_struct *child)
  * tasklist. No need to take any locks since no-one can
  * be operating on this task.
  */
-void cgroup_fork_callbacks(struct task_struct *child)
+int cgroup_fork_callbacks(struct task_struct *child)
 {
 	if (need_forkexit_callback) {
 		int i;
+		int ret;
+
 		/*
 		 * forkexit callbacks are only supported for builtin
 		 * subsystems, and the builtin section of the subsys array is
@@ -4525,10 +4527,15 @@ void cgroup_fork_callbacks(struct task_struct *child)
 		 */
 		for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
 			struct cgroup_subsys *ss = subsys[i];
-			if (ss->fork)
-				ss->fork(ss, child);
+			if (ss->fork) {
+				ret = ss->fork(ss, child);
+				if (ret)
+					return ret;
+			}
 		}
 	}
+
+	return 0;
 }
 
 /**
kernel/cgroup_freezer.c +4 -2
index e691818..e704be6 100644
@@ -186,7 +186,7 @@ static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 	return 0;
 }
 
-static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
+static int freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
 {
 	struct freezer *freezer;
 
@@ -206,7 +206,7 @@ static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
 	 * following check.
 	 */
 	if (!freezer->css.cgroup->parent)
-		return;
+		return 0;
 
 	spin_lock_irq(&freezer->lock);
 	BUG_ON(freezer->state == CGROUP_FROZEN);
@@ -215,6 +215,8 @@ static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
 	if (freezer->state == CGROUP_FREEZING)
 		freeze_task(task, true);
 	spin_unlock_irq(&freezer->lock);
+
+	return 0;
 }
 
 /*
kernel/fork.c +4 -1
index 0276c30..2625461 100644
@@ -1298,7 +1298,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	/* 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. */
-	cgroup_fork_callbacks(p);
+	retval = cgroup_fork_callbacks(p);
+	if (retval)
+		goto bad_fork_free_pid;
+
 	cgroup_callbacks_done = 1;
 
 	/* Need tasklist lock for parent etc handling! */
Signature · 1 line
1.7.5.4
#6 lore raw

RFC PATCH 4/4 cgroups: Add an rlimit subsystem

In reply to Frederic Weisbecker

Add a new rlimit subsystem with a first support to limit the number of tasks that can run inside 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 or other resource abuse inside a given cgroup.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 include/linux/cgroup_subsys.h |    8 ++
 init/Kconfig                  |    6 ++
 kernel/Makefile               |    1 +
 kernel/cgroup_rlim.c          |  142 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 157 insertions(+), 0 deletions(-)
 create mode 100644 kernel/cgroup_rlim.c
include/linux/cgroup_subsys.h +8 -0
index ac663c1..aea8cd5 100644
@@ -59,8 +59,16 @@ SUBSYS(net_cls)
 SUBSYS(blkio)
 #endif
 
+/* */
+
 #ifdef CONFIG_CGROUP_PERF
 SUBSYS(perf)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_RLIM
+SUBSYS(rlim)
+#endif
+
+/* */
init/Kconfig +6 -0
index 412c21b..1a48719 100644
@@ -690,6 +690,12 @@ 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_RLIM
+        bool "Rlimit on cgroups"
+	depends on RESOURCE_COUNTERS
+	help
+	  This option enables the rlimit settings in the scope of a cgroup.
+
 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..6713b38 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_RLIM) += cgroup_rlim.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
kernel/cgroup_rlim.c new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#5 lore raw

RFC PATCH 3/4 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 Andrew Morton <[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

RFC PATCH 2/4 cgroups: Add res_counter_write_u64() API

In reply to 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.

Cc Paul Menage <[email protected]>
Cc Li Zefan <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[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
#14 lore raw
In reply to Frederic Weisbecker
Quoted 8 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

The thing is further expandable to host more general resource limitations in the scope of a cgroup.

As this subsystem is named "rlimit", I think we should have a bigger picture about how this subsystem will be.

For example, which of other RLIMIT_XXX can be make cgroup-aware in a meaningful way and which can't.

Another issue is, we can apply the limit of RLIMIT_NPROC as the sum of the tasks' limit in a cgroup, but some other RLIMIT_XXX can't work in this way. Take RLIMIT_NICE for example, we can apply this limit to each of the tasks in the cgroup.

#15 lore raw
Quoted 13 lines · Li Zefan
Quoted 8 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

The thing is further expandable to host more general resource limitations in the scope of a cgroup.

As this subsystem is named "rlimit", I think we should have a bigger picture about how this subsystem will be.

Quoted 7 lines

For example, which of other RLIMIT_XXX can be make cgroup-aware in a meaningful way and which can't.

Another issue is, we can apply the limit of RLIMIT_NPROC as the sum of the tasks' limit in a cgroup, but some other RLIMIT_XXX can't work in this way. Take RLIMIT_NICE for example, we can apply this limit to each of the tasks in the cgroup.

Looking at the other rlimit options, it seems all of them can be applied to a cgroup. They just won't all be implemented the same way.

Those that count and limit a global user resource, like NPROC, can be implemented using the res_counter charge/uncharge that propagate the resource consuming to the parent cgroups.

Other rlimits that are traditionally only process wide can be implemented here as a simple limit applied to all processes in the whole cgroup.

For example RLIMIT_CORE would be a limit in any core dump on the whole cgroup.

RLIMIT_NOFILE would be a limit on the number of files opened by the whole cgroup.

etc...

I think they all need to be treated case by case when/if users come and propose more rlimit options. These don't all necessary need to mirror the setrlimit options. We could pick existing ones but change a bit their semantics to fit more into the cgroups meaning (as a general rule any rlimit.* file must be a cgroup wide limitation), or create new rlimit options if specific needs arise.

There can be another kind of rlimit options that can be cgroup wide but apply per process, in which case we should tweak a bit the name of the rlimit option file. Consider RLIMIT_STACK for example.
If we want a cgroup option that applies to the total of stack used by the whole cgroup, the file name would be rlim.stack. If we want that limitation to happen to the whole cgroup but per process, it would be rlim.stack_per_process.

#16 lore raw
Quoted 22 lines · Frederic Weisbecker
Quoted 13 lines · Li Zefan
Quoted 8 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

The thing is further expandable to host more general resource limitations in the scope of a cgroup.

As this subsystem is named "rlimit", I think we should have a bigger picture about how this subsystem will be.

Quoted 7 lines

For example, which of other RLIMIT_XXX can be make cgroup-aware in a meaningful way and which can't.

Another issue is, we can apply the limit of RLIMIT_NPROC as the sum of the tasks' limit in a cgroup, but some other RLIMIT_XXX can't work in this way. Take RLIMIT_NICE for example, we can apply this limit to each of the tasks in the cgroup.

Quoted 7 lines

Looking at the other rlimit options, it seems all of them can be applied to a cgroup. They just won't all be implemented the same way.

Those that count and limit a global user resource, like NPROC, can be implemented using the res_counter charge/uncharge that propagate the resource consuming to the parent cgroups.

res_counter seems a bit overkill while atomic should be sufficient for NPROC? Especially when it affects the fork path.

Normally we want to make the impact minimal when cgroup is not used, so we may treat the root cgroup somewhat special, and one choice is to always make it resource unlimited.

Quoted 17 lines

Other rlimits that are traditionally only process wide can be implemented here as a simple limit applied to all processes in the whole cgroup.

For example RLIMIT_CORE would be a limit in any core dump on the whole cgroup.

RLIMIT_NOFILE would be a limit on the number of files opened by the whole cgroup.

etc...

I think they all need to be treated case by case when/if users come and propose more rlimit options. These don't all necessary need to mirror the setrlimit options. We could pick existing ones but change a bit their semantics to fit more into the cgroups meaning (as a general rule any rlimit.* file must be a cgroup wide limitation), or create new rlimit options if specific needs arise.

Quoted 7 lines

There can be another kind of rlimit options that can be cgroup wide but apply per process, in which case we should tweak a bit the name of the rlimit option file. Consider RLIMIT_STACK for example.
If we want a cgroup option that applies to the total of stack used by the whole cgroup, the file name would be rlim.stack. If we want that limitation to happen to the whole cgroup but per process, it would be rlim.stack_per_process.

Or use a single cgroup interface for different rlimits, since all rlmits can be applied per process.

#17 lore raw
Quoted 26 lines · Li Zefan
Quoted 25 lines · Frederic Weisbecker
Quoted 13 lines · Li Zefan
Quoted 8 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

The thing is further expandable to host more general resource limitations in the scope of a cgroup.

As this subsystem is named "rlimit", I think we should have a bigger picture about how this subsystem will be.

Quoted 7 lines

For example, which of other RLIMIT_XXX can be make cgroup-aware in a meaningful way and which can't.

Another issue is, we can apply the limit of RLIMIT_NPROC as the sum of the tasks' limit in a cgroup, but some other RLIMIT_XXX can't work in this way. Take RLIMIT_NICE for example, we can apply this limit to each of the tasks in the cgroup.

Looking at the other rlimit options, it seems all of them can be applied to a cgroup. They just won't all be implemented the same way.

Quoted 7 lines
Quoted 4 lines

Those that count and limit a global user resource, like NPROC, can be implemented using the res_counter charge/uncharge that propagate the resource consuming to the parent cgroups.

res_counter seems a bit overkill while atomic should be sufficient for NPROC? Especially when it affects the fork path.

Agreed. And my first home version of this patchset was not using res_counter but atomic ops, just because I didn't know res_counter in the beginning :) So I have that code about ready.

That said res_counter API is still a perfect fit for this: it handles all the tracking to the parents, the failure path, etc... It may be an overkill for this subsystem in the implementation level, but not semantically.

Would it make sense to eventually optimize res_counter rather than creating an ad hoc clone of it that uses atomic ops?

Note it means that instead of having this:

	spin_lock();
	if (counter + val < limit)
		counter += val;
	spin_unlock();

We'll have this:

if (atomic_add_return(counter, val) >= limit)
atomic_sub(counter, val)

It is fine for proc counting. But is it fine to have temporary wrong counter for other users of res_counter? If not we can still use something based on atomic_cmpxchg() but then I'm not sure it's worth instead of using spinlock.

Quoted 4 lines

Normally we want to make the impact minimal when cgroup is not used, so we may treat the root cgroup somewhat special, and one choice is to always make it resource unlimited.

Makes sense. But then it would be wiser not to create the rlim.nr_proc file for the root cgroup. Is that possible with the current API? If not I can extend it if needed.

Quoted 25 lines
Quoted 17 lines

Other rlimits that are traditionally only process wide can be implemented here as a simple limit applied to all processes in the whole cgroup.

For example RLIMIT_CORE would be a limit in any core dump on the whole cgroup.

RLIMIT_NOFILE would be a limit on the number of files opened by the whole cgroup.

etc...

I think they all need to be treated case by case when/if users come and propose more rlimit options. These don't all necessary need to mirror the setrlimit options. We could pick existing ones but change a bit their semantics to fit more into the cgroups meaning (as a general rule any rlimit.* file must be a cgroup wide limitation), or create new rlimit options if specific needs arise.

Quoted 7 lines

There can be another kind of rlimit options that can be cgroup wide but apply per process, in which case we should tweak a bit the name of the rlimit option file. Consider RLIMIT_STACK for example.
If we want a cgroup option that applies to the total of stack used by the whole cgroup, the file name would be rlim.stack. If we want that limitation to happen to the whole cgroup but per process, it would be rlim.stack_per_process.

Quoted 2 lines

Or use a single cgroup interface for different rlimits, since all rlmits can be applied per process.

I'm not sure what you mean there.

#18 lore raw
In reply to Frederic Weisbecker

Hi Frederick,

Quoted 4 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

My general thoughts on this are:

- do we really want an "rlimit" subsystem rather than grouping things functionally? We definitely shouldn't just stuff things in here because they happen to be controlled via setrlimit currently. Also, some limits might fit more appropriately in other subsystems. (E.g. max locked memory should be a memcg field, and real-time priority should be in the cpu subsystem if it's not already subsumed by existing functionality). Grouping "rlimit" things together in a single subsystem reduces flexibility, since you can't then mount them on separate hierarchies. (This is actually related to one of my regrets about the original implementation of cgroups - the cpuset subsystem should have been split into a "cpunode" subsystem and a "memnode" subsystem, since the two parts of cpusets had no requirement to be located together - they were only linked since before cgroups there was no way to mount them separately).

A lot of the rlimit values are more for the benefit of the process (to prevent runaways) rather than for resource isolation - data segment size, file size, stack size, pending signals, virtual memory limits fall into that category, i think - they're all resource usage that falls under existing cgroup resource limits, such as memory.limit_in_bytes.

Task count is a little blurry in this regard - the main resources that you can consume with a fork bomb are CPU cycles and memory, both of which are already isolated by existing subsystems, so arguably there shouldn't be a need to control the number of tasks itself. But I'm prepared to believe that there are still bits of the kernel that have arbitrary machine-wide limits that can be hit simply by forking a massive number of processes, even if they're not using much memory or CPU cycles.

So for this case, I'd suggest that the best option is to have a numtasks subsystem with "count" and "limit" files. Future rlimit options can go in their own subsystems or be attached to existing subsystems if that makes sense.

Paul

#7 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Frederic Weisbecker

kernel/cgroup_rlim.c line 74

Quoted 5 lines · Frederic Weisbecker
kernel/cgroup_rlim.c new file +5 -0
+static void rlim_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                     struct cgroup *old_cgrp, struct task_struct *task)
+{
+       rlim_remove_proc(old_cgrp);
+}

Since this is used for both the exit callback and the attach callback, it should have a more generic name.

kernel/cgroup_rlim.c line 86

Quoted 7 lines
kernel/cgroup_rlim.c new file +7 -0
+static int rlim_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+{
+       struct rlim *rlim = cgroup_rlim(cgrp);
+       struct res_counter *limit_fail_at;
+
+       return res_counter_charge(&rlim->proc_counter, 1, &limit_fail_at);
+}

Can't this fail spuriously in the presence of hierarchies?

E.g. if cgroup A has children, and A is at its limit, then moving tasks around between A and its children, or between different children of A, seems like it would fail due to the temporary double counting.

Paul

#2 lore raw

Re: [RFC PATCH 1/4] cgroups: Allow a cgroup subsys to reject a fork

In reply to Frederic Weisbecker
Quoted 3 lines · Frederic Weisbecker

Make the cgroup subsystem fork callback return a value so that subsystems are able to accept or reject a fork completion with a custom error value.

This is unnecessary complexity in the cgroup subsystem (and seems to miss cleanup for subsystems that have previously had their fork() method return success).

If you want a subsystem to be able to reject a fork, I think it's better to have that subsystem be called explicitly from do_fork(), and keep that logic out of cgroups.

Paul

#19 lore raw
In reply to Paul Menage
Quoted 25 lines · Paul Menage

Hi Frederick,

Quoted 4 lines · Frederic Weisbecker

This starts a basic rlimit cgroup subsystem with only the equivalent of RLIMIT_NPROC yet. This can be useful to limit the global effects of a local fork bomb for example (local in term of a cgroup).

My general thoughts on this are:

- do we really want an "rlimit" subsystem rather than grouping things functionally? We definitely shouldn't just stuff things in here because they happen to be controlled via setrlimit currently. Also, some limits might fit more appropriately in other subsystems. (E.g. max locked memory should be a memcg field, and real-time priority should be in the cpu subsystem if it's not already subsumed by existing functionality). Grouping "rlimit" things together in a single subsystem reduces flexibility, since you can't then mount them on separate hierarchies. (This is actually related to one of my regrets about the original implementation of cgroups - the cpuset subsystem should have been split into a "cpunode" subsystem and a "memnode" subsystem, since the two parts of cpusets had no requirement to be located together - they were only linked since before cgroups there was no way to mount them separately).

Quoted 6 lines

A lot of the rlimit values are more for the benefit of the process (to prevent runaways) rather than for resource isolation - data segment size, file size, stack size, pending signals, virtual memory limits fall into that category, i think - they're all resource usage that falls under existing cgroup resource limits, such as memory.limit_in_bytes.

Yeah I all agree with you. To mimic rlmit inside a cgroup subsystem would be a bad thing given we already have subsystems where some of the rlimit options can fit and moreover your message made me read again the part about hierarchies in cgroup documentation. I eventually understood the idea/point of building parallel hierarchies with different subsystems mounted in it, and thus eventually I understand your point about the problem on flexibility implied by an everything-rlimit subsystem.

Quoted 8 lines

Task count is a little blurry in this regard - the main resources that you can consume with a fork bomb are CPU cycles and memory, both of which are already isolated by existing subsystems, so arguably there shouldn't be a need to control the number of tasks itself. But I'm prepared to believe that there are still bits of the kernel that have arbitrary machine-wide limits that can be hit simply by forking a massive number of processes, even if they're not using much memory or CPU cycles.

Yeah I've just asked Johannes Weiner about that and he told me can't use memory limits for that as these don't handle kernel memory.

Quoted 4 lines

So for this case, I'd suggest that the best option is to have a numtasks subsystem with "count" and "limit" files. Future rlimit options can go in their own subsystems or be attached to existing subsystems if that makes sense.

Agreed about future rlimit options, but building a single purpose numtask subsystem looks a bit strange. Just because it looks too much single purpose. OTOH I can't figure out any other kind of future limitation that should fit aside in a very similar topic, enough that we wouldn't care about seperating both for flexibility.

So I guess I'm going to take that way.

Thanks!

#3 lore raw

Re: [RFC PATCH 1/4] cgroups: Allow a cgroup subsys to reject a fork

In reply to Paul Menage
Quoted 8 lines · Paul Menage
Quoted 3 lines · Frederic Weisbecker

Make the cgroup subsystem fork callback return a value so that subsystems are able to accept or reject a fork completion with a custom error value.

This is unnecessary complexity in the cgroup subsystem (and seems to miss cleanup for subsystems that have previously had their fork() method return success).

No it seems only the freezer subsystem had this fork callback implemented. But indeed it adds complexity because if a subsystem can cancel a fork, then previous subsystems that called ->fork() would need to have a kind cancel_fork() callback to call.

I haven't looked very deep but the freezer doesn't seem to need any rollback. Future subsystems using the fork() callback may need it though.

Quoted 3 lines

If you want a subsystem to be able to reject a fork, I think it's better to have that subsystem be called explicitly from do_fork(), and keep that logic out of cgroups.

Ok I can do that.

#8 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Paul Menage
Quoted 9 lines · Paul Menage

kernel/cgroup_rlim.c line 74

Quoted 5 lines · Frederic Weisbecker
kernel/cgroup_rlim.c new file +5 -0
+static void rlim_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                     struct cgroup *old_cgrp, struct task_struct *task)
+{
+       rlim_remove_proc(old_cgrp);
+}

Since this is used for both the exit callback and the attach callback, it should have a more generic name.

Right.

Quoted 10 lines

kernel/cgroup_rlim.c line 86

Quoted 7 lines
kernel/cgroup_rlim.c new file +7 -0
+static int rlim_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
+{
+       struct rlim *rlim = cgroup_rlim(cgrp);
+       struct res_counter *limit_fail_at;
+
+       return res_counter_charge(&rlim->proc_counter, 1, &limit_fail_at);
+}

Can't this fail spuriously in the presence of hierarchies?

Quoted 3 lines

E.g. if cgroup A has children, and A is at its limit, then moving tasks around between A and its children, or between different children of A, seems like it would fail due to the temporary double counting.

Good point. Probably I should first uncharge the old cgroup and its parents.

#20 lore raw
In reply to Frederic Weisbecker
Quoted 3 lines · Frederic Weisbecker

Agreed about future rlimit options, but building a single purpose numtask subsystem looks a bit strange. Just because it looks too much single purpose.

Admittedly most subsystems do have more knobs and features, but I don't think it's unreasonable for a subsystem to provide just a single feature. In theory the overhead is going to be very low.

Paul

#9 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Frederic Weisbecker
Quoted 8 lines · Frederic Weisbecker
Quoted 5 lines

Can't this fail spuriously in the presence of hierarchies?

E.g. if cgroup A has children, and A is at its limit, then moving tasks around between A and its children, or between different children of A, seems like it would fail due to the temporary double counting.

Good point. Probably I should first uncharge the old cgroup and its parents.

That can fail too - if a new task gets created between the uncharge and the charge.

What we need is a res_counter_move_charge(A, B, amount) function which will:

- locate C, the nearest common ancestor of A and B
- lock up the chain from B up to but not including C, adding the new charge
- unlock up the chain from B to C
- uncharge along the chain from A up to but not including C (not sure how much locking is needed there since there's no need for roll back).

Paul

#10 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

Quoted 10 lines · Paul Menage <menage <at> google.com> writes:

What we need is a res_counter_move_charge(A, B, amount) function which will:

- locate C, the nearest common ancestor of A and B
- lock up the chain from B up to but not including C, adding the new charge
- unlock up the chain from B to C
- uncharge along the chain from A up to but not including C (not sure how much locking is needed there since there's no need for roll back).

Paul

Another alternative is to use the 'attach' callback in struct cgroup_subsys which gets both the old cgroup and the new cgroup as parameters and do rlim_remove_proc(old_cgrp) and res_counter_charge(new_cgrp) in this same function under the protection of a spinlock.
It would be good to add a return value to the 'attach' callback too.

#12 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Frederic Weisbecker

kernel/cgroup_rlim.c line 18

Quoted 4 lines · Frederic Weisbecker <fweisbec <at> gmail.com> writes:
kernel/cgroup_rlim.c new file +4 -0
+struct rlim {
+	struct res_counter		proc_counter;
+	struct cgroup_subsys_state	css;
+};

This can be used to enforce limits on FDs (RLIMIT_NOFILE), TCP/UDP ports, locked memory, queued data on sockets, etc. How making proc_counter an array (currently of size 1) ? Though with this patch there is just one, but it will great to have ready support for easily adding more.

kernel/cgroup_rlim.c line 122

Quoted 5 lines
kernel/cgroup_rlim.c new file +5 -0
+static struct cftype cft_max_proc = {
+	.name		= "max_proc",
+	.read_u64	= max_proc_read_u64,
+	.write_u64	= max_proc_write_u64,
+};

How about exporting USAGE, MAX_USAGE and FAILCNT too? These are useful for resource estimation.

#11 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Aditya Kali

Please keep the Cc list, everybody may has missed your message. I just found it while browsing randomly my lkml INBOX.

Quoted 9 lines · Aditya Kali
Quoted 8 lines · Paul Menage <menage <at> google.com> writes:

What we need is a res_counter_move_charge(A, B, amount) function which will:

- locate C, the nearest common ancestor of A and B
- lock up the chain from B up to but not including C, adding the new charge
- unlock up the chain from B to C
- uncharge along the chain from A up to but not including C (not sure how much locking is needed there since there's no need for roll back).

Quoted 8 lines
Quoted 2 lines

Paul

Another alternative is to use the 'attach' callback in struct cgroup_subsys which gets both the old cgroup and the new cgroup as parameters and do rlim_remove_proc(old_cgrp) and res_counter_charge(new_cgrp) in this same function under the protection of a spinlock.
It would be good to add a return value to the 'attach' callback too.

But the it would require a global lock, or a per hierarchy one, if you want to protect against forks and exits. And that wouldn't scale due to these fork/exit that would take that big lock.

#13 lore raw

Re: [RFC PATCH 4/4] cgroups: Add an rlimit subsystem

In reply to Aditya Kali

(restore cc list, and for real this time)

Quoted 11 lines · Aditya Kali

Frederic Weisbecker <fweisbec <at> gmail.com

kernel/cgroup_rlim.c line 18

Quoted 4 lines · writes:
kernel/cgroup_rlim.c new file +4 -0
+struct rlim {
+   struct res_counter              proc_counter;
+   struct cgroup_subsys_state      css;
+};

This can be used to enforce limits on FDs (RLIMIT_NOFILE), TCP/UDP ports, locked memory, queued data on sockets, etc. How making proc_counter an array (currently of size 1) ? Though with this patch there is just one, but it will great to have ready support for easily adding more.

Yeah but Paul suggested that we don't have an all-in-one limit subsystem but rather we group things under the same topic. If we group proc counter and file counter under the same subsystem, we won't be able to build two distinct hierarchies: one to deal with nr_proc and one for nr_file, and group tasks differently inside each.

We need a higher granularity than that. And having a single nr_task susbsytem seem to make sense as nothing else appear to be suitable to be in the same subsystem.

Quoted 8 lines

kernel/cgroup_rlim.c line 122

Quoted 6 lines
kernel/cgroup_rlim.c new file +5 -0
+static struct cftype cft_max_proc = {
+   .name           = "max_proc",
+   .read_u64       = max_proc_read_u64,
+   .write_u64      = max_proc_write_u64,
+};

How about exporting USAGE, MAX_USAGE and FAILCNT too? These are useful for resource estimation.

Usage makes sense. But do we really need the rest? May be should we wait for people to request these?

Thanks.