All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hirofumi Nakagawa <hnakagawa@miraclelinux.com>
To: linux-kernel@vger.kernel.org
Cc: menage@google.com
Subject: [RFC][PATCH][1/3] rlimit cgroup
Date: Wed, 23 Jul 2008 21:24:04 +0900	[thread overview]
Message-ID: <488722E4.7010203@miraclelinux.com> (raw)


Signed-off-by: Hirofumi Nakagawa <hnakagawa@miraclelinux.com>
---
 include/linux/cgroup_subsys.h |    4
 init/Kconfig                  |    9 ++
 kernel/Makefile               |    1
 kernel/cgroup_rlimit.c        |  179 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 193 insertions(+)

--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.26-rc8-mm1/kernel/cgroup_rlimit.c	2008-07-23 18:52:34.000000000 +0900
@@ -0,0 +1,179 @@
+/*
+ *	Copyright (C) 2008 MIRACLE LINUX Corp.
+ *
+ *	Written by Hirofumi Nakagawa <hnakagawa@miraclelinux.com>
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License as
+ *	published by the Free Software Foundation, version 2.
+ */
+#include <linux/cgroup.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/resource.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+
+struct cgroup_subsys rlimit_cgroup_subsys __read_mostly;
+
+struct rlimit_cgroup {
+	struct cgroup_subsys_state css;
+
+	struct rlimit rlim[RLIM_NLIMITS];
+
+	struct mutex lock;
+};
+
+static struct rlimit_cgroup *rlimit_cgroup_from_cgrp(struct cgroup *cgrp)
+{
+	return container_of(cgroup_subsys_state(cgrp, rlimit_cgroup_subsys_id),
+			struct rlimit_cgroup, css);
+}
+
+static int parse_rlimit_input(struct rlimit *limit, const char *buf)
+{
+	int c, n;
+
+	c = sscanf(buf, "%d %ld %ld", &n, &limit->rlim_cur, &limit->rlim_max);
+	if (c != 3)
+		return -1;
+
+	return n;
+}
+
+static int rlimit_limits_write_string(struct cgroup *cgrp,
+	struct cftype *cft,
+	const char *buffer)
+{
+	struct rlimit_cgroup *rlimitcg = rlimit_cgroup_from_cgrp(cgrp);
+	struct rlimit limit;
+	struct cgroup_iter it;
+	struct task_struct *tsk;
+	int n;
+
+	n = parse_rlimit_input(&limit, buffer);
+	if (n < 0)
+		return -EINVAL;
+
+	mutex_lock(&rlimitcg->lock);
+	rlimitcg->rlim[n] = limit;
+
+	cgroup_iter_start(cgrp, &it);
+
+	while ((tsk = cgroup_iter_next(cgrp, &it)))
+		__setrlimit(tsk, n, &limit);
+
+	cgroup_iter_end(cgrp, &it);
+
+	mutex_unlock(&rlimitcg->lock);
+
+	return 0;
+}
+
+static int rlimit_limits_read_seq_string(struct cgroup *cgrp,
+	struct cftype *cft,
+	struct seq_file *m)
+{
+	struct rlimit_cgroup *rlimitcg = rlimit_cgroup_from_cgrp(cgrp);
+	char buf[91];
+	int i, c;
+
+	mutex_lock(&rlimitcg->lock);
+	sprintf(buf, "%-10s %-25s %-20s %-20s %-10s\n",
+		"Number", "Limit", "Soft Limit", "Hard Limit", "Unit");
+	seq_puts(m, buf);
+
+	for (i = 0; i < RLIM_NLIMITS; i++) {
+		c = sprintf(buf, "%-10d ", i);
+
+		if (rlimitcg->rlim[i].rlim_cur == RLIM_INFINITY)
+			c += sprintf(&buf[c], "%-25s %-20s ",
+					lnames[i].name, "unlimited");
+		else
+			c += sprintf(&buf[c], "%-25s %-20lu ",
+				lnames[i].name, rlimitcg->rlim[i].rlim_cur);
+
+		if (rlimitcg->rlim[i].rlim_max == RLIM_INFINITY)
+			c += sprintf(&buf[c], "%-20s ", "unlimited");
+		else
+			c += sprintf(&buf[c], "%-20lu ",
+					rlimitcg->rlim[i].rlim_max);
+
+		if (lnames[i].unit)
+			sprintf(&buf[c], "%-10s\n",
+					lnames[i].unit);
+		else
+			sprintf(&buf[c], "\n");
+
+		seq_puts(m, buf);
+	}
+
+	mutex_unlock(&rlimitcg->lock);
+
+	return 0;
+}
+
+static struct cftype rlimit_cgroup_files[] = {
+	{
+		.name = "limits",
+		.write_string = rlimit_limits_write_string,
+		.read_seq_string = rlimit_limits_read_seq_string,
+	},
+};
+
+static struct cgroup_subsys_state*
+rlimit_cgroup_create(struct cgroup_subsys *ss,
+	struct cgroup *cgrp)
+{
+	struct rlimit_cgroup *rlimitcg;
+	int i;
+
+	rlimitcg = kzalloc(sizeof(struct rlimit_cgroup), GFP_KERNEL);
+	if (!rlimitcg)
+		return ERR_PTR(-ENOMEM);
+
+	mutex_init(&rlimitcg->lock);
+
+	for (i = 0; i < RLIM_NLIMITS; i++)
+		rlimitcg->rlim[i] = current->signal->rlim[i];
+
+	return &rlimitcg->css;
+}
+
+static void rlimit_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
+{
+	kfree(rlimit_cgroup_from_cgrp(cgrp));
+}
+
+static void rlimit_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
+	struct cgroup *old_cgrp, struct task_struct *tsk)
+{
+	struct rlimit_cgroup *rlimitcg = rlimit_cgroup_from_cgrp(cgrp);
+	int i;
+
+	mutex_lock(&rlimitcg->lock);
+	for (i = 0; i < RLIM_NLIMITS; i++)
+		__setrlimit(tsk, i, &rlimitcg->rlim[i]);
+	mutex_unlock(&rlimitcg->lock);
+}
+
+
+static int rlimit_cgroup_populate(struct cgroup_subsys *ss,
+	struct cgroup *cgrp)
+{
+	if (rlimit_cgroup_subsys.disabled)
+		return 0;
+
+	return cgroup_add_files(cgrp, ss, rlimit_cgroup_files,
+			ARRAY_SIZE(rlimit_cgroup_files));
+}
+
+struct cgroup_subsys rlimit_cgroup_subsys = {
+	.name = "rlimit",
+	.subsys_id = rlimit_cgroup_subsys_id,
+	.create = rlimit_cgroup_create,
+	.destroy = rlimit_cgroup_destroy,
+	.attach = rlimit_cgroup_attach,
+	.populate = rlimit_cgroup_populate,
+	.early_init = 0,
+};
--- linux-2.6.26-rc8-mm1.orig/kernel/Makefile	2008-07-23 18:48:04.000000000 +0900
+++ linux-2.6.26-rc8-mm1/kernel/Makefile	2008-07-23 18:48:09.000000000 +0900
@@ -51,6 +51,7 @@ obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_DEBUG) += cgroup_debug.o
+obj-$(CONFIG_CGROUP_RLIMIT) += cgroup_rlimit.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_NS) += ns_cgroup.o
 obj-$(CONFIG_UTS_NS) += utsname.o
--- linux-2.6.26-rc8-mm1.orig/include/linux/cgroup_subsys.h	2008-07-23 18:48:04.000000000 +0900
+++ linux-2.6.26-rc8-mm1/include/linux/cgroup_subsys.h	2008-07-23 18:48:09.000000000 +0900
@@ -52,3 +52,7 @@ SUBSYS(memrlimit_cgroup)
 #endif

 /* */
+
+#ifdef CONFIG_CGROUP_RLIMIT
+SUBSYS(rlimit_cgroup)
+#endif
--- linux-2.6.26-rc8-mm1.orig/init/Kconfig	2008-07-23 18:48:04.000000000 +0900
+++ linux-2.6.26-rc8-mm1/init/Kconfig	2008-07-23 21:20:36.000000000 +0900
@@ -290,6 +290,15 @@ config CGROUP_DEBUG

 	  Say N if unsure

+config CGROUP_RLIMIT
+	bool "Simple rlimit interface cgroup subsystem"
+       	depends on CGROUPS && EXPERIMENTAL
+       	default n
+       	help
+	  Provides a rlimit interface cgroup subsystem.
+
+	  Say N if unsure
+
 config CGROUP_NS
         bool "Namespace cgroup subsystem"
         depends on CGROUPS



                 reply	other threads:[~2008-07-23 12:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=488722E4.7010203@miraclelinux.com \
    --to=hnakagawa@miraclelinux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=menage@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.