public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC] [PATCH 0/6] Number of Tasks Resource controller
@ 2006-04-21  2:25 sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 1/6] numtasks - Initialization routines sekharan
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

Numtasks controller is a simple resource controller that provides user the
ability to
	- control the number of tasks a class can have
	- control the rate at which forks happen in the system

Patch descriptions:
1/6: ckrm_numtasks_init
	- Hooks up with CKRM core by defining the basic alloc/free
	  functions and registering the controller with the core.

2/6: ckrm_numtasks_tasksupport
	- Adds task management support by defining a function to be called
	  from fork() to see if the class is within its share allocation
	- sets interface to be called from core when a class is moved to a
	  class.

3/6: ckrm_numtasks_shares_n_stats
	- sets interface to be called from core when a class's shares are
	  changes or when stats are requested.

4/6: ckrm_numtasks_config
	- Use module parameters to dynamically set the total numtasks
		and maximum forkrate allowed

5/6: ckrm_numtasks_forkrate
	- Adds support to control the forkrate in the system.

6/6: ckrm_numtasks_docs
	- Documents what the numtasks controller does and how to use it.

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 1/6] numtasks - Initialization routines
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
@ 2006-04-21  2:25 ` sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 2/6] numtasks - Add task control support sekharan
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

1/6: ckrm_numtasks_init

Hooks up with CKRM core by defining the basic alloc/free functions and
registering the controller with the core.
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-Off-By: Matt Helsley <matthltc@us.ibm.com>

 init/Kconfig                |   10 ++++
 kernel/ckrm/Makefile        |    1 
 kernel/ckrm/ckrm_numtasks.c |   90 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+)

Index: linux2617-rc2/kernel/ckrm/Makefile
===================================================================
--- linux2617-rc2.orig/kernel/ckrm/Makefile
+++ linux2617-rc2/kernel/ckrm/Makefile
@@ -1,2 +1,3 @@
 obj-y = ckrm.o ckrm_shares.o ckrm_task.o
+obj-$(CONFIG_CKRM_RES_NUMTASKS) += ckrm_numtasks.o
 obj-$(CONFIG_CKRM_RCFS) += ckrm_rcfs.o
Index: linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
===================================================================
--- /dev/null
+++ linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
@@ -0,0 +1,90 @@
+/* ckrm_numtasks.c - "Number of tasks" resource controller for CKRM
+ *
+ * Copyright (C) Chandra Seetharaman,  IBM Corp. 2003-2006
+ *	      (C) Matt Helsley, IBM Corp. 2006
+ *
+ * Latest version, more details at http://ckrm.sf.net
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+
+/*
+ * CKRM Resource controller for tracking number of tasks in a class.
+ */
+#include <linux/module.h>
+#include <linux/ckrm_rc.h>
+
+static const char res_ctlr_name[] = "numtasks";
+
+struct ckrm_numtasks {
+	struct ckrm_shares shares;
+};
+
+struct ckrm_controller numtasks_ctlr;
+
+static struct ckrm_numtasks *get_shares_numtasks(struct ckrm_shares *shares)
+{
+	if (shares)
+		return container_of(shares, struct ckrm_numtasks, shares);
+	return NULL;
+}
+
+/* Initialize share struct values */
+static void numtasks_res_init_one(struct ckrm_numtasks *numtasks_res)
+{
+	numtasks_res->shares.min_shares = CKRM_SHARE_DONT_CARE;
+	numtasks_res->shares.max_shares = CKRM_SHARE_DONT_CARE;
+	numtasks_res->shares.child_shares_divisor = CKRM_SHARE_DEFAULT_DIVISOR;
+	numtasks_res->shares.unused_min_shares = CKRM_SHARE_DEFAULT_DIVISOR;
+}
+
+static struct ckrm_shares *numtasks_alloc_shares_struct(
+						struct ckrm_class *class)
+{
+	struct ckrm_numtasks *res;
+
+	res = kzalloc(sizeof(struct ckrm_numtasks), GFP_KERNEL);
+	if (!res)
+		return NULL;
+	numtasks_res_init_one(res);
+	return &res->shares;
+}
+
+/*
+ * No locking of this resource class object necessary as we are not
+ * supposed to be assigned (or used) when/after this function is called.
+ */
+static void numtasks_free_shares_struct(struct ckrm_shares *my_res)
+{
+	kfree(get_shares_numtasks(my_res));
+}
+
+struct ckrm_controller numtasks_ctlr = {
+	.name = res_ctlr_name,
+	.depth_supported = 3,
+	.ctlr_id = CKRM_NO_RES_ID,
+	.alloc_shares_struct = numtasks_alloc_shares_struct,
+	.free_shares_struct = numtasks_free_shares_struct,
+};
+
+int __init init_ckrm_numtasks_res(void)
+{
+	if (numtasks_ctlr.ctlr_id != CKRM_NO_RES_ID)
+		return -EBUSY; /* already registered */
+	return ckrm_register_controller(&numtasks_ctlr);
+}
+
+void __exit exit_ckrm_numtasks_res(void)
+{
+	int rc;
+	do {
+		rc = ckrm_unregister_controller(&numtasks_ctlr);
+	} while (rc == -EBUSY);
+	BUG_ON(rc != 0);
+}
+module_init(init_ckrm_numtasks_res)
+module_exit(exit_ckrm_numtasks_res)
Index: linux2617-rc2/init/Kconfig
===================================================================
--- linux2617-rc2.orig/init/Kconfig
+++ linux2617-rc2/init/Kconfig
@@ -175,6 +175,16 @@ config CKRM_RCFS
 	  Say M if unsure, Y to save on module loading. N doesn't make sense
 	  when CKRM has been configured.
 
+config CKRM_RES_NUMTASKS
+	bool "Number of Tasks Resource Manager"
+	depends on CKRM
+	default y
+	help
+	  Provides a Resource Controller for CKRM that allows limiting no of
+	  tasks a task class can have.
+
+	  Say N if unsure, Y to use the feature.
+
 endmenu
 config SYSCTL
 	bool "Sysctl support"

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 2/6] numtasks - Add task control support
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 1/6] numtasks - Initialization routines sekharan
@ 2006-04-21  2:25 ` sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 3/6] numtasks - Add shares and stats support sekharan
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

2/6: ckrm_numtasks_tasksupport

Adds task management support by defining a function to be called from
fork() to see if the class is within its share allocation
Sets interface to be called from core when a class is moved to a class.
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>

 include/linux/ckrm_tsk.h    |   28 +++++++++++
 kernel/ckrm/ckrm_numtasks.c |  104 +++++++++++++++++++++++++++++++++++++++++++-
 kernel/fork.c               |    7 ++
 3 files changed, 137 insertions(+), 2 deletions(-)

Index: linux2617-rc2/include/linux/ckrm_tsk.h
===================================================================
--- /dev/null
+++ linux2617-rc2/include/linux/ckrm_tsk.h
@@ -0,0 +1,28 @@
+/* ckrm_tsk.h - No. of tasks resource controller for CKRM
+ *
+ * Copyright (C) Chandra Seetharaman, IBM Corp. 2003, 2004, 2005
+ *
+ * Provides No. of tasks resource controller for CKRM
+ *
+ * Latest version, more details at http://ckrm.sf.net
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+#ifndef _LINUX_CKRM_TSK_H
+#define _LINUX_CKRM_TSK_H
+
+#ifdef CONFIG_CKRM_RES_NUMTASKS
+#include <linux/ckrm_rc.h>
+
+extern int numtasks_allow_fork(struct ckrm_class *);
+
+#else /* CONFIG_CKRM_RES_NUMTASKS */
+
+#define numtasks_allow_fork(class) (0)
+
+#endif /* CONFIG_CKRM_RES_NUMTASKS */
+#endif /* _LINUX_CKRM_RES_H */
Index: linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
===================================================================
--- linux2617-rc2.orig/kernel/ckrm/ckrm_numtasks.c
+++ linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
@@ -21,7 +21,19 @@
 static const char res_ctlr_name[] = "numtasks";
 
 struct ckrm_numtasks {
+	struct ckrm_class *class;	/* the class i am part of... */
 	struct ckrm_shares shares;
+	int cnt_min_shares;	/* num_tasks min_shares in local units */
+	int cnt_unused;		/* has to borrow if more than this is needed */
+	int cnt_max_shares;	/* no tasks over this limit. */
+				/* Three above cnt_* fields are protected
+				 * by class's class_lock */
+	atomic_t cnt_cur_alloc;	/* current alloc from self */
+	atomic_t cnt_borrowed;	/* borrowed from the parent */
+
+	/* stats */
+	int successes;
+	int failures;
 };
 
 struct ckrm_controller numtasks_ctlr;
@@ -33,6 +45,84 @@ static struct ckrm_numtasks *get_shares_
 	return NULL;
 }
 
+static struct ckrm_numtasks *get_class_numtasks(struct ckrm_class *class)
+{
+	return get_shares_numtasks(ckrm_get_controller_shares(class,
+						&numtasks_ctlr));
+}
+
+int numtasks_allow_fork(struct ckrm_class *class)
+{
+	struct ckrm_numtasks *res;
+
+	/* controller is not registered; no class is given */
+	if ((numtasks_ctlr.ctlr_id == CKRM_NO_RES_ID) || (class == NULL))
+		return 0;
+	res = get_class_numtasks(class);
+
+	/* numtasks not available for this class */
+	if (!res)
+		return 0;
+
+	if (res->cnt_max_shares == CKRM_SHARE_DONT_CARE)
+		return 0;
+
+	/* Over the limit ? */
+	if (atomic_read(&res->cnt_cur_alloc) >= res->cnt_max_shares) {
+		res->failures++;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+static void inc_usage_count(struct ckrm_numtasks *res)
+{
+	atomic_inc(&res->cnt_cur_alloc);
+
+	if (ckrm_is_class_root(res->class)) {
+		res->successes++;
+		return;
+	}
+	/* Do we need to borrow from our parent ? */
+	if ((res->cnt_unused == CKRM_SHARE_DONT_CARE) ||
+			(atomic_read(&res->cnt_cur_alloc) > res->cnt_unused)) {
+		inc_usage_count(get_class_numtasks(res->class->parent));
+		atomic_inc(&res->cnt_borrowed);
+	} else
+		res->successes++;
+}
+
+static void dec_usage_count(struct ckrm_numtasks *res)
+{
+	if (atomic_read(&res->cnt_cur_alloc) == 0)
+		return;
+	atomic_dec(&res->cnt_cur_alloc);
+	if (atomic_read(&res->cnt_borrowed) > 0) {
+		atomic_dec(&res->cnt_borrowed);
+		dec_usage_count(get_class_numtasks(res->class->parent));
+	}
+}
+
+static void numtasks_move_task(struct task_struct *task,
+		struct ckrm_shares *old, struct ckrm_shares *new)
+{
+	struct ckrm_numtasks *oldres, *newres;
+
+	if (old == new)
+		return;
+
+	/* Decrement usage count of old class */
+	oldres = get_shares_numtasks(old);
+	if (oldres)
+		dec_usage_count(oldres);
+
+	/* Increment usage count of new class */
+	newres = get_shares_numtasks(new);
+	if (newres)
+		inc_usage_count(newres);
+}
+
 /* Initialize share struct values */
 static void numtasks_res_init_one(struct ckrm_numtasks *numtasks_res)
 {
@@ -50,6 +140,7 @@ static struct ckrm_shares *numtasks_allo
 	res = kzalloc(sizeof(struct ckrm_numtasks), GFP_KERNEL);
 	if (!res)
 		return NULL;
+	res->class = class;
 	numtasks_res_init_one(res);
 	return &res->shares;
 }
@@ -60,7 +151,17 @@ static struct ckrm_shares *numtasks_allo
  */
 static void numtasks_free_shares_struct(struct ckrm_shares *my_res)
 {
-	kfree(get_shares_numtasks(my_res));
+	struct ckrm_numtasks *res, *parres;
+	int i, borrowed;
+
+	res = get_shares_numtasks(my_res);
+	if (!ckrm_is_class_root(res->class)) {
+		parres = get_class_numtasks(res->class->parent);
+		borrowed = atomic_read(&res->cnt_borrowed);
+		for (i = 0; i < borrowed; i++)
+			dec_usage_count(parres);
+	}
+	kfree(res);
 }
 
 struct ckrm_controller numtasks_ctlr = {
@@ -69,6 +170,7 @@ struct ckrm_controller numtasks_ctlr = {
 	.ctlr_id = CKRM_NO_RES_ID,
 	.alloc_shares_struct = numtasks_alloc_shares_struct,
 	.free_shares_struct = numtasks_free_shares_struct,
+	.move_task = numtasks_move_task,
 };
 
 int __init init_ckrm_numtasks_res(void)
Index: linux2617-rc2/kernel/fork.c
===================================================================
--- linux2617-rc2.orig/kernel/fork.c
+++ linux2617-rc2/kernel/fork.c
@@ -45,6 +45,7 @@
 #include <linux/acct.h>
 #include <linux/cn_proc.h>
 #include <linux/ckrm.h>
+#include <linux/ckrm_tsk.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -1310,7 +1311,7 @@ long do_fork(unsigned long clone_flags,
 	      int __user *child_tidptr)
 {
 	struct task_struct *p;
-	int trace = 0;
+	int trace = 0, rc;
 	struct pid *pid = alloc_pid();
 	long nr;
 
@@ -1323,6 +1324,10 @@ long do_fork(unsigned long clone_flags,
 			clone_flags |= CLONE_PTRACE;
 	}
 
+	rc = numtasks_allow_fork(current->class);
+	if (rc)
+		return rc;
+
 	p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);
 	/*
 	 * Do this prior waking up the new thread - the thread pointer

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 3/6] numtasks - Add shares and stats support
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 1/6] numtasks - Initialization routines sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 2/6] numtasks - Add task control support sekharan
@ 2006-04-21  2:25 ` sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 4/6] numtasks - Add configuration support sekharan
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

3/6: ckrm_numtasks_shares_n_stats

Sets interface to be called from core when a class's shares are
changes or when stats are requested.
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: MAEDA Naoaki <maeda.naoaki@jp.fujitsu.com>
Signed-Off-By: Matt Helsley <matthltc@us.ibm.com>

 kernel/ckrm/ckrm_numtasks.c |  134 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+)

Index: linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
===================================================================
--- linux2617-rc2.orig/kernel/ckrm/ckrm_numtasks.c
+++ linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
@@ -130,6 +130,10 @@ static void numtasks_res_init_one(struct
 	numtasks_res->shares.max_shares = CKRM_SHARE_DONT_CARE;
 	numtasks_res->shares.child_shares_divisor = CKRM_SHARE_DEFAULT_DIVISOR;
 	numtasks_res->shares.unused_min_shares = CKRM_SHARE_DEFAULT_DIVISOR;
+
+	numtasks_res->cnt_min_shares = CKRM_SHARE_DONT_CARE;
+	numtasks_res->cnt_unused = CKRM_SHARE_DONT_CARE;
+	numtasks_res->cnt_max_shares = CKRM_SHARE_DONT_CARE;
 }
 
 static struct ckrm_shares *numtasks_alloc_shares_struct(
@@ -164,6 +168,134 @@ static void numtasks_free_shares_struct(
 	kfree(res);
 }
 
+static int recalc_shares(int self_shares, int parent_shares, int parent_divisor)
+{
+	u64 numerator;
+
+	if ((self_shares == CKRM_SHARE_DONT_CARE) ||
+				(parent_shares == CKRM_SHARE_DONT_CARE))
+		return CKRM_SHARE_DONT_CARE;
+	if (parent_divisor == 0)
+		return 0;
+	numerator = (u64) self_shares * parent_shares;
+	do_div(numerator, parent_divisor);
+	return numerator;
+}
+
+static int recalc_unused_shares(int self_cnt_min_shares,
+				int self_unused_min_shares, int self_divisor)
+{
+	u64 numerator;
+
+	if (self_cnt_min_shares == CKRM_SHARE_DONT_CARE)
+		return CKRM_SHARE_DONT_CARE;
+	if (self_divisor == 0)
+		return 0;
+	numerator = (u64) self_unused_min_shares * self_cnt_min_shares;
+	do_div(numerator, self_divisor);
+	return numerator;
+}
+
+static void recalc_self(struct ckrm_numtasks *res,
+				struct ckrm_numtasks *parres)
+{
+	struct ckrm_shares *par = &parres->shares;
+	struct ckrm_shares *self = &res->shares;
+
+	res->cnt_min_shares = recalc_shares(self->min_shares,
+						parres->cnt_min_shares,
+						par->child_shares_divisor);
+	res->cnt_max_shares = recalc_shares(self->max_shares,
+						parres->cnt_max_shares,
+						par->child_shares_divisor);
+
+	/*
+	 * Now that we know the new cnt_min/cnt_max boundaries we can update
+	 * the unused quantity.
+	 */
+	res->cnt_unused = recalc_unused_shares(res->cnt_min_shares,
+						self->unused_min_shares,
+						self->child_shares_divisor);
+}
+
+
+/*
+ * Recalculate the min_shares and max_shares in real units and propagate the
+ * same to children.
+ * Called with parent's (class to which parres belongs) lock held.
+ */
+static void recalc_and_propagate(struct ckrm_numtasks *res,
+				struct ckrm_numtasks *parres)
+{
+	struct ckrm_class *child = NULL;
+	struct ckrm_numtasks *childres;
+
+	if (parres)
+		recalc_self(res, parres);
+
+	/* propagate to children */
+	spin_lock(&res->class->class_lock);
+	for_each_child(child, res->class) {
+		childres = get_class_numtasks(child);
+		BUG_ON(!childres);
+		spin_lock(&child->class_lock);
+		recalc_and_propagate(childres, res);
+		spin_unlock(&child->class_lock);
+	}
+	spin_unlock(&res->class->class_lock);
+}
+
+static void numtasks_shares_changed(struct ckrm_shares *my_res)
+{
+	struct ckrm_numtasks *parres, *res;
+	struct ckrm_shares *cur, *par;
+
+	res = get_shares_numtasks(my_res);
+	if (!res)
+		return;
+	cur = &res->shares;
+
+	if (!ckrm_is_class_root(res->class)) {
+		spin_lock(&res->class->parent->class_lock);
+		parres = get_class_numtasks(res->class->parent);
+		par = &parres->shares;
+	} else {
+		parres = NULL;
+		par = NULL;
+	}
+	if (parres)
+		parres->cnt_unused = recalc_unused_shares(
+						parres->cnt_min_shares,
+						par->unused_min_shares,
+						par->child_shares_divisor);
+	recalc_and_propagate(res, parres);
+	if (!ckrm_is_class_root(res->class))
+		spin_unlock(&res->class->parent->class_lock);
+}
+
+static ssize_t numtasks_show_stats(struct ckrm_shares *my_res,
+					char *buf, size_t buf_size)
+{
+	ssize_t i, j = 0;
+	struct ckrm_numtasks *res;
+
+	res = get_shares_numtasks(my_res);
+	if (!res)
+		return -EINVAL;
+
+	i = snprintf(buf, buf_size, "%s: Current usage %d\n",
+					res_ctlr_name,
+					atomic_read(&res->cnt_cur_alloc));
+	buf += i; j += i; buf_size -= i;
+	i = snprintf(buf, buf_size, "%s: Number of successes %d\n",
+					res_ctlr_name, res->successes);
+	buf += i; j += i; buf_size -= i;
+	i = snprintf(buf, buf_size, "%s: Number of failures %d\n",
+					res_ctlr_name, res->failures);
+	j += i;
+	return j;
+}
+
 struct ckrm_controller numtasks_ctlr = {
 	.name = res_ctlr_name,
 	.depth_supported = 3,
@@ -171,6 +303,8 @@ struct ckrm_controller numtasks_ctlr = {
 	.alloc_shares_struct = numtasks_alloc_shares_struct,
 	.free_shares_struct = numtasks_free_shares_struct,
 	.move_task = numtasks_move_task,
+	.shares_changed = numtasks_shares_changed,
+	.show_stats = numtasks_show_stats,
 };
 
 int __init init_ckrm_numtasks_res(void)

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 4/6] numtasks - Add configuration support
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
                   ` (2 preceding siblings ...)
  2006-04-21  2:25 ` [RFC] [PATCH 3/6] numtasks - Add shares and stats support sekharan
@ 2006-04-21  2:25 ` sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 5/6] numtasks - Add fork rate control support sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 6/6] numtasks - Documentation for Numtasks controller sekharan
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

4/6: ckrm_numtasks_config

Use module parameters to dynamically set the total numtasks and maximum
forkrate allowed
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-Off-By: Matt Helsley <matthltc@us.ibm.com>

 include/linux/moduleparam.h |   12 ++++++--
 kernel/ckrm/ckrm_numtasks.c |   64 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 70 insertions(+), 6 deletions(-)

Index: linux2617-rc2/include/linux/moduleparam.h
===================================================================
--- linux2617-rc2.orig/include/linux/moduleparam.h
+++ linux2617-rc2/include/linux/moduleparam.h
@@ -75,11 +75,17 @@ struct kparam_array
 /* Helper functions: type is byte, short, ushort, int, uint, long,
    ulong, charp, bool or invbool, or XXX if you define param_get_XXX,
    param_set_XXX and param_check_XXX. */
-#define module_param_named(name, value, type, perm)			   \
-	param_check_##type(name, &(value));				   \
-	module_param_call(name, param_set_##type, param_get_##type, &value, perm); \
+#define module_param_named_call(name, value, type, set, perm)		\
+	param_check_##type(name, &(value));				\
+	module_param_call(name, set, param_get_##type, &(value), perm); \
 	__MODULE_PARM_TYPE(name, #type)
 
+#define module_param_named(name, value, type, perm)			   \
+	module_param_named_call(name, value, type, param_set_##type, perm)
+
+#define module_param_set_call(name, type, setfn, perm) \
+	module_param_named_call(name, name, type, setfn, perm)
+
 #define module_param(name, type, perm)				\
 	module_param_named(name, name, type, perm)
 
Index: linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
===================================================================
--- linux2617-rc2.orig/kernel/ckrm/ckrm_numtasks.c
+++ linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
@@ -20,6 +20,13 @@
 
 static const char res_ctlr_name[] = "numtasks";
 
+#define UNLIMITED INT_MAX
+#define DEF_TOTAL_NUM_TASKS UNLIMITED
+static int total_numtasks __read_mostly = DEF_TOTAL_NUM_TASKS;
+
+static struct ckrm_class *root_class;
+static int total_cnt_alloc = 0;
+
 struct ckrm_numtasks {
 	struct ckrm_class *class;	/* the class i am part of... */
 	struct ckrm_shares shares;
@@ -81,6 +88,7 @@ static void inc_usage_count(struct ckrm_
 	atomic_inc(&res->cnt_cur_alloc);
 
 	if (ckrm_is_class_root(res->class)) {
+		total_cnt_alloc++;
 		res->successes++;
 		return;
 	}
@@ -89,8 +97,10 @@ static void inc_usage_count(struct ckrm_
 			(atomic_read(&res->cnt_cur_alloc) > res->cnt_unused)) {
 		inc_usage_count(get_class_numtasks(res->class->parent));
 		atomic_inc(&res->cnt_borrowed);
-	} else
-		res->successes++;
+	} else {
+		total_cnt_alloc++;
+  		res->successes++;
+	}
 }
 
 static void dec_usage_count(struct ckrm_numtasks *res)
@@ -101,7 +111,9 @@ static void dec_usage_count(struct ckrm_
 	if (atomic_read(&res->cnt_borrowed) > 0) {
 		atomic_dec(&res->cnt_borrowed);
 		dec_usage_count(get_class_numtasks(res->class->parent));
-	}
+	} else
+		total_cnt_alloc--;
+
 }
 
 static void numtasks_move_task(struct task_struct *task,
@@ -146,6 +158,8 @@ static struct ckrm_shares *numtasks_allo
 		return NULL;
 	res->class = class;
 	numtasks_res_init_one(res);
+	if (ckrm_is_class_root(class))
+		root_class = class; /* store the root class. */
 	return &res->shares;
 }
 
@@ -307,6 +321,50 @@ struct ckrm_controller numtasks_ctlr = {
 	.show_stats = numtasks_show_stats,
 };
 
+/*
+ * Writeable module parameters use these set_<parameter> functions to respond
+ * to changes. Otherwise the values can be read and used any time.
+ */
+static int set_numtasks_config_val(int *var, int old_value, const char *val,
+				struct kernel_param *kp)
+{
+	int rc = param_set_int(val, kp);
+
+	if (rc < 0)
+		return rc;
+	if (*var < 1) {
+		*var = old_value;
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int set_total_numtasks(const char *val, struct kernel_param *kp)
+{
+	int prev = total_numtasks;
+	int rc = set_numtasks_config_val(&total_numtasks, prev, val, kp);
+	struct ckrm_numtasks *shares = NULL;
+
+	if (!root_class)
+		return 0;
+	if (rc < 0)
+		return rc;
+	if (total_numtasks <= total_cnt_alloc) {
+		total_numtasks = prev;
+		return -EINVAL;
+	}
+	shares = get_class_numtasks(root_class);
+	spin_lock(&root_class->class_lock);
+	shares->cnt_min_shares = total_numtasks;
+	shares->cnt_unused = total_numtasks;
+	shares->cnt_max_shares = total_numtasks;
+	recalc_and_propagate(shares, NULL);
+	spin_unlock(&root_class->class_lock);
+	return 0;
+}
+module_param_set_call(total_numtasks, int, set_total_numtasks,
+			S_IRUGO | S_IWUSR);
+
 int __init init_ckrm_numtasks_res(void)
 {
 	if (numtasks_ctlr.ctlr_id != CKRM_NO_RES_ID)

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 5/6] numtasks - Add fork rate control support
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
                   ` (3 preceding siblings ...)
  2006-04-21  2:25 ` [RFC] [PATCH 4/6] numtasks - Add configuration support sekharan
@ 2006-04-21  2:25 ` sekharan
  2006-04-21  2:25 ` [RFC] [PATCH 6/6] numtasks - Documentation for Numtasks controller sekharan
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

5/6: ckrm_numtasks_forkrate

Adds support to control the forkrate in the system.
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-Off-By: Matt Helsley <matthltc@us.ibm.com>

 kernel/ckrm/ckrm_numtasks.c |   75 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 75 insertions(+)

Index: linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
===================================================================
--- linux2617-rc2.orig/kernel/ckrm/ckrm_numtasks.c
+++ linux2617-rc2/kernel/ckrm/ckrm_numtasks.c
@@ -27,6 +27,11 @@ static int total_numtasks __read_mostly 
 static struct ckrm_class *root_class;
 static int total_cnt_alloc = 0;
 
+#define DEF_FORKRATE UNLIMITED
+#define DEF_FORKRATE_INTERVAL (1)
+static int forkrate __read_mostly = DEF_FORKRATE;
+static int forkrate_interval __read_mostly = DEF_FORKRATE_INTERVAL;
+
 struct ckrm_numtasks {
 	struct ckrm_class *class;	/* the class i am part of... */
 	struct ckrm_shares shares;
@@ -41,6 +46,11 @@ struct ckrm_numtasks {
 	/* stats */
 	int successes;
 	int failures;
+	int forkrate_failures;
+
+	/* Fork rate fields */
+	int forks_in_period;
+	unsigned long period_start;
 };
 
 struct ckrm_controller numtasks_ctlr;
@@ -58,8 +68,24 @@ static struct ckrm_numtasks *get_class_n
 						&numtasks_ctlr));
 }
 
+static inline int check_forkrate(struct ckrm_numtasks *res)
+{
+	if (time_after(jiffies, res->period_start + forkrate_interval * HZ)) {
+		res->period_start = jiffies;
+		res->forks_in_period = 0;
+	}
+
+	if (res->forks_in_period >= forkrate) {
+ 		res->forkrate_failures++;
+		return -ENOSPC;
+	}
+	res->forks_in_period++;
+	return 0;
+}
+
 int numtasks_allow_fork(struct ckrm_class *class)
 {
+	int rc;
 	struct ckrm_numtasks *res;
 
 	/* controller is not registered; no class is given */
@@ -71,6 +97,11 @@ int numtasks_allow_fork(struct ckrm_clas
 	if (!res)
 		return 0;
 
+	/* Check forkrate before checking class's usage */
+	rc = check_forkrate(res);
+	if (rc)
+		return rc;
+
 	if (res->cnt_max_shares == CKRM_SHARE_DONT_CARE)
 		return 0;
 
@@ -146,6 +177,7 @@ static void numtasks_res_init_one(struct
 	numtasks_res->cnt_min_shares = CKRM_SHARE_DONT_CARE;
 	numtasks_res->cnt_unused = CKRM_SHARE_DONT_CARE;
 	numtasks_res->cnt_max_shares = CKRM_SHARE_DONT_CARE;
+	numtasks_res->period_start = jiffies;
 }
 
 static struct ckrm_shares *numtasks_alloc_shares_struct(
@@ -306,6 +338,9 @@ static ssize_t numtasks_show_stats(struc
 	buf += i; j += i; buf_size -= i;
 	i = snprintf(buf, buf_size, "%s: Number of failures %d\n",
 					res_ctlr_name, res->failures);
+	buf += i; j += i; buf_size -= i;
+	i = snprintf(buf, buf_size, "%s: Number of forkrate failures %d\n",
+					res_ctlr_name, res->forkrate_failures);
 	j += i;
 	return j;
 }
@@ -365,6 +400,46 @@ static int set_total_numtasks(const char
 module_param_set_call(total_numtasks, int, set_total_numtasks,
 			S_IRUGO | S_IWUSR);
 
+static void reset_forkrates(struct ckrm_class *class, unsigned long now)
+{
+	struct ckrm_numtasks *res;
+	struct ckrm_class *child = NULL;
+
+	res = get_class_numtasks(class);
+	if (!res)
+		return;
+	res->forks_in_period = 0;
+	res->period_start = now;
+
+	spin_lock(&class->class_lock);
+	for_each_child(child, class)
+		reset_forkrates(child, now);
+	spin_unlock(&class->class_lock);
+}
+
+static int set_forkrate(const char *val, struct kernel_param *kp)
+{
+	int prev = forkrate;
+	int rc = set_numtasks_config_val(&forkrate, prev, val, kp);
+	if (rc < 0)
+		return rc;
+	reset_forkrates(root_class, jiffies);
+	return 0;
+}
+module_param_set_call(forkrate, int, set_forkrate, S_IRUGO | S_IWUSR);
+
+static int set_forkrate_interval(const char *val, struct kernel_param *kp)
+{
+	int prev = forkrate_interval;
+	int rc = set_numtasks_config_val(&forkrate_interval, prev, val, kp);
+	if (rc < 0)
+		return rc;
+	reset_forkrates(root_class, jiffies);
+	return 0;
+}
+module_param_set_call(forkrate_interval, int, set_forkrate_interval,
+			S_IRUGO | S_IWUSR);
+
 int __init init_ckrm_numtasks_res(void)
 {
 	if (numtasks_ctlr.ctlr_id != CKRM_NO_RES_ID)

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] [PATCH 6/6] numtasks - Documentation for Numtasks controller
  2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
                   ` (4 preceding siblings ...)
  2006-04-21  2:25 ` [RFC] [PATCH 5/6] numtasks - Add fork rate control support sekharan
@ 2006-04-21  2:25 ` sekharan
  5 siblings, 0 replies; 7+ messages in thread
From: sekharan @ 2006-04-21  2:25 UTC (permalink / raw)
  To: linux-kernel, ckrm-tech; +Cc: sekharan

6/6: ckrm_numtasks_docs

Documents what the numtasks controller does and how to use it.
--

Signed-Off-By: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-Off-By: Matt Helsley <matthltc@us.ibm.com>

 Documentation/ckrm/numtasks |  130 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+)

Index: linux2617-rc2/Documentation/ckrm/numtasks
===================================================================
--- /dev/null
+++ linux2617-rc2/Documentation/ckrm/numtasks
@@ -0,0 +1,130 @@
+Introduction
+-------------
+
+Numtasks is a resource controller under the CKRM framework that allows the
+user/sysadmin to
+	- manage the number of tasks a class can create.
+        - limit the fork rate across the system.
+
+As with any other resource under the CKRM framework, numtasks also assigns
+all the resources to the default class(/config/ckrm). Since, the number
+of tasks in a system is not limited, this resource controller provides a
+way to set the total number of tasks available in the system through the config
+file. The config variable that affect this is total_numtasks.
+
+This resource controller also allows the sysadmin to limit the number of forks
+that are allowed in the system within the specified number of seconds. This
+can be acheived by changing the attributes forkrate and forkrate_interval in
+the config file. Using this feature one can protect the system from being
+attacked by fork bomb type applications.
+
+Configuration parameters of numtasks controller (forkrate, total_numtasks
+and forkrate_interval) can be read/changed through the modparam interface
+/sys/module/ckrm_numtasks/parameters/
+
+Installation
+-------------
+
+1. Configure "Number of Tasks Resource Manager" under CKRM (see
+      Documentation/ckrm/installation).
+
+2. Reboot the system with the new kernel.
+
+3. Verify the controller's presence by reading the file
+   /config/ckrm/shares (should show a line with res=numtasks)
+
+Usage
+-----
+
+For brevity, unless otherwise specified all the following commands are
+executed in the default class (/config/ckrm).
+
+As explained above, files in /sys/module/ckrm_numtasks/parameters/
+shows total_numtasks and forkrate info.
+
+   # cd /sys/module/ckrm_numtasks/parameters/
+   # ls
+   .  ..  forkrate  forkrate_interval  total_numtasks
+   # cat total_numtasks
+   2147483647
+   		# value is INT_MAX which means unlimited
+   # cat forkrate
+   2147483647
+   		# value is INT_MAX which means unlimited
+   # cat forkrate_interval
+   1
+   		# forkrate forks are allowed per 1 sec
+
+By default, the total_numtasks is set to "unlimited", forkrate is set
+to "unlimited" and forkrate_interval is set to 1 second. Which means the
+total number of tasks in a system is unlimited and the forks per second is
+also unlimited.
+
+sysadmin can change these values by just writing the attribute/value pair
+to the config file.
+
+   # echo 10000 > forkrate
+   # cat forkrate
+   10000
+   # echo 100001 > total_numtasks
+   # cat total_numtasks
+   100001
+
+By making child_shares_divisor to be same as total_numtasks, sysadmin can
+make the numbers in shares file be same as the number of tasks for a class.
+In other words, the numbers in shares file will be the absolute number of
+tasks a class is allowed.
+
+   # cd /config/ckrm
+   # cat shares
+   res=numtasks,min_shares=-3,max_shares=-3,child_shares_divisor=100
+   # echo res=numtasks,child_shares_divisor=1000 > shares
+   # cat shares
+   res=numtasks,min_shares=-3,max_shares=-3,child_shares_divisor=1000
+
+Class creation
+--------------
+
+   # mkdir c1
+
+Its initial share is don't care. The parent's share values will be unchanged.
+
+Setting a new class share
+-------------------------
+
+'min_shares' specifies the number of tasks this class is entitled to get
+'limit' is the maximum number of tasks this class can get.
+
+Following command will set the min_shares of class c1 to be 250 and the limit
+to be 500
+
+   # echo 'res=numtasks,min_shares=250,max_shares=500' > c1/shares
+   # cat c1/shares
+   res=numtasks,min_shares=250,max_shares=500,child_shares_divisor=100
+
+Note that the min_shares of 250 and max_shares of 500 is w.r.t the
+paren't's 1000 above, and not the absolute numbers.
+
+Limiting forks in a time period
+-------------------------------
+By default, this resource controller allows unlimited forks per second.
+
+Following commands would change it to allow only 100 forks per 10 seconds
+
+   # cd /sys/module/ckrm_numtasks/parameters
+   # cat 100 > forkrate
+   # cat 10 > forkrate_interval
+
+Note that the same set of values is used across the system. In other words,
+each individual class will be allowed 'forkrate' forks in 'forkrate_interval'
+seconds.
+
+Monitoring
+----------
+
+stats file shows statistics of the number of tasks usage of a class
+   # cd /config/ckrm
+   # cat stats
+   numtasks: Number of successes 12554
+   numtasks: Number of failures 0
+   numtasks: Number of forkrate failures 0

-- 

----------------------------------------------------------------------
    Chandra Seetharaman               | Be careful what you choose....
              - sekharan@us.ibm.com   |      .......you may get it.
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-04-21  2:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-21  2:25 [RFC] [PATCH 0/6] Number of Tasks Resource controller sekharan
2006-04-21  2:25 ` [RFC] [PATCH 1/6] numtasks - Initialization routines sekharan
2006-04-21  2:25 ` [RFC] [PATCH 2/6] numtasks - Add task control support sekharan
2006-04-21  2:25 ` [RFC] [PATCH 3/6] numtasks - Add shares and stats support sekharan
2006-04-21  2:25 ` [RFC] [PATCH 4/6] numtasks - Add configuration support sekharan
2006-04-21  2:25 ` [RFC] [PATCH 5/6] numtasks - Add fork rate control support sekharan
2006-04-21  2:25 ` [RFC] [PATCH 6/6] numtasks - Documentation for Numtasks controller sekharan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox