Number of Tasks Resource controller
Subsystems (5)
RFC PATCH 5/6 numtasks - Add fork rate control support
In reply to [email protected]5/6: ckrm_numtasks_forkrate
Adds support to control the forkrate in the system. --
kernel/ckrm/ckrm_numtasks.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 75 insertions(+)
kernel/ckrm/ckrm_numtasks.c +75 -0
@@ -27,6 +27,11 @@ static int total_numtasks __read_mostlystatic 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_clasif (!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 charmodule_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 )
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------RFC PATCH 2/6 numtasks - Add task control support
In reply to [email protected]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. --
include/linux/ckrm_tsk.h | 28 +++++++++++ kernel/ckrm/ckrm_numtasks.c | 104 +++++++++++++++++++++++++++++++++++++++++++- kernel/fork.c | 7 ++ 3 files changed, 137 insertions(+), 2 deletions(-)
include/linux/ckrm_tsk.h new file +28 -0
@@ -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 */
kernel/ckrm/ckrm_numtasks.c +103 -1
135 lines not on this page · raw message
kernel/fork.c +6 -1
@@ -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
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------RFC PATCH 3/6 numtasks - Add shares and stats support
In reply to [email protected]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.
--
kernel/ckrm/ckrm_numtasks.c | 134 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 134 insertions(+)
kernel/ckrm/ckrm_numtasks.c +134 -0
@@ -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 )
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------RFC PATCH 4/6 numtasks - Add configuration support
In reply to [email protected]4/6: ckrm_numtasks_config
Use module parameters to dynamically set the total numtasks and maximum forkrate allowed
--
include/linux/moduleparam.h | 12 ++++++-- kernel/ckrm/ckrm_numtasks.c | 64 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 6 deletions(-)
include/linux/moduleparam.h +9 -3
@@ -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)
kernel/ckrm/ckrm_numtasks.c +61 -3
100 lines not on this page · raw message
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------RFC PATCH 1/6 numtasks - Initialization routines
In reply to [email protected]1/6: ckrm_numtasks_init
Hooks up with CKRM core by defining the basic alloc/free functions and registering the controller with the core.
--
init/Kconfig | 10 ++++ kernel/ckrm/Makefile | 1 kernel/ckrm/ckrm_numtasks.c | 90 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+)
kernel/ckrm/Makefile +1 -0
@@ -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
kernel/ckrm/ckrm_numtasks.c new file +90 -0
90 lines not on this page · raw message
init/Kconfig +10 -0
@@ -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"
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------RFC PATCH 6/6 numtasks - Documentation for Numtasks controller
In reply to [email protected]6/6: ckrm_numtasks_docs
Documents what the numtasks controller does and how to use it. --
Documentation/ckrm/numtasks | 130 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 130 insertions(+)
Documentation/ckrm/numtasks new file +130 -0
@@ -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
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------cover Number of Tasks Resource controller
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.
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------