public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
From: Aleksa Sarai <cyphar@cyphar.com>
To: tj@kernel.org, lizefan@huawei.com, mingo@redhat.com,
	peterz@infradead.org
Cc: richard@nod.at, fweisbec@gmail.com, linux-kernel@vger.kernel.org,
	cgroups@vger.kernel.org, Aleksa Sarai <cyphar@cyphar.com>
Subject: [PATCH RFC 2/2] cgroups: add an nproc subsystem
Date: Mon, 23 Feb 2015 14:08:11 +1100	[thread overview]
Message-ID: <1424660891-12719-3-git-send-email-cyphar@cyphar.com> (raw)
In-Reply-To: <1424660891-12719-1-git-send-email-cyphar@cyphar.com>

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

This is a step to being able to limit the global impact of a fork bomb
inside a cgroup, allowing for cgroups to perform fairly basic resource
limitation which it currently doesn't have the capability to do.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
---
 include/linux/cgroup_subsys.h |   4 +
 init/Kconfig                  |  10 +++
 kernel/Makefile               |   1 +
 kernel/cgroup_nproc.c         | 181 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 196 insertions(+)
 create mode 100644 kernel/cgroup_nproc.c

diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h
index 98c4f9b..e83e0ac 100644
--- a/include/linux/cgroup_subsys.h
+++ b/include/linux/cgroup_subsys.h
@@ -47,6 +47,10 @@ SUBSYS(net_prio)
 SUBSYS(hugetlb)
 #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_NPROC)
+SUBSYS(nproc)
+#endif
+
 /*
  * The following subsystems are not supported on the default hierarchy.
  */
diff --git a/init/Kconfig b/init/Kconfig
index 9afb971..d6315fe 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1047,6 +1047,16 @@ config CGROUP_HUGETLB
 	  control group is tracked in the third page lru pointer. This means
 	  that we cannot use the controller with huge page less than 3 pages.
 
+config CGROUP_NPROC
+	bool "Process number limiting on cgroups"
+	depends on PAGE_COUNTER
+	help
+	  This options enables the setting of process number limits in the scope
+	  of a cgroup. Any attempt to fork more processes than is allowed in the
+	  cgroup will fail. This allows for more basic resource limitation that
+	  applies to a cgroup, similar to RLIMIT_NPROC (except that instead of
+	  applying to a process tree it applies to a cgroup).
+
 config CGROUP_PERF
 	bool "Enable perf_event per-cpu per-container group (cgroup) monitoring"
 	depends on PERF_EVENTS && CGROUPS
diff --git a/kernel/Makefile b/kernel/Makefile
index a59481a..10c4b40 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -52,6 +52,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_NPROC) += cgroup_nproc.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
diff --git a/kernel/cgroup_nproc.c b/kernel/cgroup_nproc.c
new file mode 100644
index 0000000..414d8d5
--- /dev/null
+++ b/kernel/cgroup_nproc.c
@@ -0,0 +1,181 @@
+/*
+ * Process number limiting subsys for cgroups.
+ *
+ * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com>
+ *
+ * Thanks to Frederic Weisbecker for creating the seminal patches which lead to
+ * this being written.
+ *
+ */
+
+#include <linux/cgroup.h>
+#include <linux/slab.h>
+#include <linux/page_counter.h>
+
+struct nproc {
+	struct page_counter		proc_counter;
+	struct cgroup_subsys_state	css;
+};
+
+static inline struct nproc *css_nproc(struct cgroup_subsys_state *css)
+{
+	return css ? container_of(css, struct nproc, css) : NULL;
+}
+
+static inline struct nproc *task_nproc(struct task_struct *task)
+{
+	return css_nproc(task_css(task, nproc_cgrp_id));
+}
+
+static struct nproc *parent_nproc(struct nproc *nproc)
+{
+	return css_nproc(nproc->css.parent);
+}
+
+static struct cgroup_subsys_state *nproc_css_alloc(struct cgroup_subsys_state *parent)
+{
+	struct nproc *nproc;
+
+	nproc = kzalloc(sizeof(struct nproc), GFP_KERNEL);
+	if (!nproc)
+		return ERR_PTR(-ENOMEM);
+
+	return &nproc->css;
+}
+
+static int nproc_css_online(struct cgroup_subsys_state *css)
+{
+	struct nproc *nproc = css_nproc(css);
+	struct nproc *parent = parent_nproc(nproc);
+
+	if (!parent) {
+		page_counter_init(&nproc->proc_counter, NULL);
+		return 0;
+	}
+
+	page_counter_init(&nproc->proc_counter, &parent->proc_counter);
+	return page_counter_limit(&nproc->proc_counter, parent->proc_counter.limit);
+}
+
+static void nproc_css_free(struct cgroup_subsys_state *css)
+{
+	kfree(css_nproc(css));
+}
+
+static inline void nproc_remove_procs(struct nproc *nproc, int num_procs)
+{
+	page_counter_uncharge(&nproc->proc_counter, num_procs);
+}
+
+static inline int nproc_add_procs(struct nproc *nproc, int num_procs)
+{
+	struct page_counter *fail_at;
+	int errcode;
+
+	errcode = page_counter_try_charge(&nproc->proc_counter, num_procs, &fail_at);
+	if (errcode)
+		return -EAGAIN;
+
+	return 0;
+}
+
+static void nproc_cancel_attach(struct cgroup_subsys_state *css,
+				struct cgroup_taskset *tset)
+{
+	struct nproc *nproc = css_nproc(css);
+	unsigned long num_tasks = 0;
+	struct task_struct *task;
+
+	cgroup_taskset_for_each(task, tset)
+		num_tasks++;
+
+	nproc_remove_procs(nproc, num_tasks);
+}
+
+static int nproc_can_attach(struct cgroup_subsys_state *css,
+			    struct cgroup_taskset *tset)
+{
+	struct nproc *nproc = css_nproc(css);
+	unsigned long num_tasks = 0;
+	struct task_struct *task;
+
+	cgroup_taskset_for_each(task, tset)
+		num_tasks++;
+
+	return nproc_add_procs(nproc, num_tasks);
+}
+
+static int nproc_fork(struct task_struct *task)
+{
+	struct nproc *nproc = task_nproc(task);
+
+	return nproc_add_procs(nproc, 1);
+}
+
+static void nproc_exit(struct cgroup_subsys_state *css,
+		       struct cgroup_subsys_state *old_css,
+		       struct task_struct *task)
+{
+	struct nproc *nproc = css_nproc(old_css);
+
+	nproc_remove_procs(nproc, 1);
+}
+
+static int nproc_write_limit(struct cgroup_subsys_state *css,
+			     struct cftype *cft, u64 val)
+{
+	struct nproc *nproc = css_nproc(css);
+
+	return page_counter_limit(&nproc->proc_counter, val);
+}
+
+static u64 nproc_read_limit(struct cgroup_subsys_state *css,
+			    struct cftype *cft)
+{
+	struct nproc *nproc = css_nproc(css);
+
+	return nproc->proc_counter.limit;
+}
+
+static u64 nproc_read_max_limit(struct cgroup_subsys_state *css,
+				       struct cftype *cft)
+{
+	return PAGE_COUNTER_MAX;
+}
+
+static u64 nproc_read_usage(struct cgroup_subsys_state *css,
+			    struct cftype *cft)
+{
+	struct nproc *nproc = css_nproc(css);
+
+	return page_counter_read(&nproc->proc_counter);
+}
+
+static struct cftype files[] = {
+	{
+		.name = "limit",
+		.write_u64 = nproc_write_limit,
+		.read_u64 = nproc_read_limit,
+	},
+	{
+		.name = "max_limit",
+		.read_u64 = nproc_read_max_limit,
+	},
+	{
+		.name = "usage",
+		.read_u64 = nproc_read_usage,
+	},
+	{ }	/* terminate */
+};
+
+struct cgroup_subsys nproc_cgrp_subsys = {
+	.css_alloc	= nproc_css_alloc,
+	.css_online	= nproc_css_online,
+	.css_free	= nproc_css_free,
+	.can_attach	= nproc_can_attach,
+	.cancel_attach	= nproc_cancel_attach,
+	.fork		= nproc_fork,
+	.exit		= nproc_exit,
+	.legacy_cftypes	= files,
+	.early_init	= 0,
+};
-- 
2.3.0

  parent reply	other threads:[~2015-02-23  3:08 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-23  3:08 [PATCH RFC 0/2] add nproc cgroup subsystem Aleksa Sarai
2015-02-23  3:08 ` [PATCH RFC 1/2] cgroups: allow a cgroup subsystem to reject a fork Aleksa Sarai
2015-02-23 14:49   ` Peter Zijlstra
2015-02-23  3:08 ` Aleksa Sarai [this message]
     [not found] ` <1424660891-12719-1-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-02-27  4:17   ` [RFC PATCH v2 0/2] add nproc cgroup subsystem Aleksa Sarai
     [not found]     ` <1425010639-16492-1-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-02-27  4:17       ` [PATCH v2 1/2] cgroups: allow a cgroup subsystem to reject a fork Aleksa Sarai
     [not found]         ` <1425010639-16492-2-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-03-09  3:06           ` Tejun Heo
     [not found]             ` <CAOviyaip7Faz98YWzGoTaXGYVb72sfD+ZL4Xa89reU9+=43jFA@mail.gmail.com>
     [not found]               ` <20150309065902.GP13283@htj.duckdns.org>
     [not found]                 ` <20150309065902.GP13283-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-03-10  8:19                   ` Aleksa Sarai
     [not found]                     ` <CAOviyaj3mf66ho15WrD8qB=ECxKWYTAkWodxWaFVMWeZG4d0FQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-10 12:47                       ` Tejun Heo
     [not found]                         ` <20150310124701.GB28730-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-03-10 14:51                           ` Aleksa Sarai
     [not found]                             ` <CAOviyai7yJrbGb+uYpK35tw7R-KM0jWQ-BmhpyTqnRFJsVYdUA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-10 15:17                               ` Tejun Heo
2015-03-11  5:16                                 ` Aleksa Sarai
2015-03-11 11:46                                   ` Tejun Heo
2015-03-11 23:47                   ` Aleksa Sarai
     [not found]                     ` <CAOviyaj55Yqahz75Gy5=yjFteeKFp7746=80-Ufww2E62Ads_Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-12  1:25                       ` Tejun Heo
2015-02-27  4:17     ` [PATCH v2 2/2] cgroups: add an nproc subsystem Aleksa Sarai
     [not found]       ` <1425010639-16492-3-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-03-02 15:22         ` Tejun Heo
     [not found]           ` <20150302152205.GC17694-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-03-09  1:49             ` Zefan Li
     [not found]               ` <54FCFC39.6050900-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
2015-03-09  2:34                 ` Tejun Heo
2015-03-06  1:45   ` [PATCH v4 0/2] cgroup: add pids subsystem Aleksa Sarai
2015-03-06  1:45     ` [PATCH v4 1/2] cgroups: allow a cgroup subsystem to reject a fork Aleksa Sarai
     [not found]     ` <1425606357-6337-1-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-03-06  1:45       ` [PATCH v4 2/2] cgroups: add a pids subsystem Aleksa Sarai
     [not found]         ` <1425606357-6337-3-git-send-email-cyphar-gVpy/LI/lHzQT0dZR+AlfA@public.gmane.org>
2015-03-09  3:34           ` Tejun Heo
     [not found]             ` <20150309033405.GE13283-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-03-09  3:39               ` Tejun Heo
2015-03-09 18:58               ` Austin S Hemmelgarn
     [not found]                 ` <54FDED43.4050908-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-03-09 19:51                   ` Tejun Heo
2015-03-10  8:10                   ` Aleksa Sarai
2015-03-10 11:32                     ` Austin S Hemmelgarn
     [not found]                       ` <54FED651.6040100-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-03-10 12:31                         ` Aleksa Sarai
     [not found]                           ` <CAOviyagpCNcAN4hdhsxffdpE+yDmw+NXx+FikTe64GJ1hQeXhQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-11 15:13                             ` Austin S Hemmelgarn
     [not found]                               ` <55005BAC.9060405-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-03-12  2:28                                 ` Aleksa Sarai
2015-03-12 15:35                                   ` Austin S Hemmelgarn
2015-03-12  3:47                               ` Tejun Heo
2015-03-09  3:08       ` [PATCH v4 0/2] cgroup: add " Tejun Heo
2015-02-27 11:49 ` [PATCH RFC 0/2] add nproc cgroup subsystem Tejun Heo
     [not found]   ` <20150227114940.GB3964-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-02-27 13:46     ` Richard Weinberger
     [not found]       ` <54F07525.4050100-/L3Ra7n9ekc@public.gmane.org>
2015-02-27 13:52         ` Tejun Heo
2015-02-27 16:42     ` Austin S Hemmelgarn
     [not found]       ` <54F09E62.8000007-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-02-27 17:06         ` Tejun Heo
     [not found]           ` <20150227170640.GK3964-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-02-27 17:25             ` Tim Hockin
2015-02-27 17:45               ` Tejun Heo
     [not found]                 ` <20150227174503.GM3964-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-02-27 17:56                   ` Tejun Heo
2015-02-27 21:45                   ` Tim Hockin
2015-02-27 21:49                     ` Tejun Heo
     [not found]                       ` <CAAAKZwsCc8BtFx58KMFpRTohU81oCBeGVOPGMJrjJt9q5upKfQ@mail.gmail.com>
2015-02-28 16:57                         ` Tejun Heo
     [not found]                           ` <20150228165706.GS3964-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-02-28 22:26                             ` Tim Hockin
     [not found]                               ` <CAAAKZwv=idxvrffHx2QyW=PGH4k42ckq-VLJGQrXkeQ6NmByRQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-02-28 22:50                                 ` Tejun Heo
     [not found]                                   ` <20150228225036.GA4597-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-03-01  4:46                                     ` Tim Hockin
2015-02-28 23:11                                 ` Johannes Weiner
2015-02-27 18:49             ` Austin S Hemmelgarn
     [not found]               ` <54F0BC51.4050506-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-02-27 19:35                 ` Tejun Heo
2015-02-28  9:26                 ` Aleksa Sarai
     [not found]                   ` <CAOviyajSOY6kTiwTA+APf9VGT=Ui=0QQH6KUqwaxHB3ahuJk2g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-02-28 11:59                     ` Tejun Heo
     [not found]                       ` <CAAAKZws45c3PhFQMGrm_K+OZV+KOyGV9sXTakHcTfNP1kHxzOQ@mail.gmail.com>
     [not found]                         ` <CAAAKZws45c3PhFQMGrm_K+OZV+KOyGV9sXTakHcTfNP1kHxzOQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-02-28 16:43                           ` Tejun Heo
2015-03-02 13:13                             ` Austin S Hemmelgarn
     [not found]                               ` <54F461F3.3030903-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-03-02 13:31                                 ` Aleksa Sarai
     [not found]                                   ` <CAOviyahKJthwLTND51HhaRNB_KJC60T7HFHjdqPZf3pQmAUAhw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-02 13:54                                     ` Tejun Heo
2015-03-02 13:49                               ` Tejun Heo
2015-02-27 17:12       ` Tim Hockin
     [not found]         ` <CAO_RewbeTbMuqVG5wsui_gHwrdgqjF0KLk6yr5a3bb76VOkofg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-02-27 17:15           ` Tejun Heo
2015-03-04 20:23 ` [PATCH v3 0/2] cgroup: add pids subsystem Aleksa Sarai
2015-03-04 20:23   ` [PATCH v3 1/2] cgroups: allow a cgroup subsystem to reject a fork Aleksa Sarai
2015-03-04 20:23   ` [PATCH v3 2/2] cgroups: add a pids subsystem Aleksa Sarai
2015-03-05  8:39     ` Aleksa Sarai
2015-03-05 14:37     ` Marian Marinov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1424660891-12719-3-git-send-email-cyphar@cyphar.com \
    --to=cyphar@cyphar.com \
    --cc=cgroups@vger.kernel.org \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=richard@nod.at \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox