Number of Tasks Resource controller
Subsystems (5)
PATCH 3/6 numtasks - Add shares and stats support
In reply to Chandra Seetharaman3/6: numtasks_shares_n_stats
Sets interface to be called from core when a resource group's shares are changes or when stats are requested.
--
numtasks.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 134 insertions(+)
kernel/res_group/numtasks.c +134 -0
@@ -130,6 +130,10 @@ static void numtasks_res_init_one(struct numtasks_res->shares.max_shares =SHARE_DONT_CARE ; numtasks_res->shares.child_shares_divisor =SHARE_DEFAULT_DIVISOR ; numtasks_res->shares.unused_min_shares =SHARE_DEFAULT_DIVISOR ; + + numtasks_res->cnt_min_shares =SHARE_DONT_CARE ; + numtasks_res->cnt_unused =SHARE_DONT_CARE ; + numtasks_res->cnt_max_shares =SHARE_DONT_CARE ; } static struct res_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 ==SHARE_DONT_CARE ) || + (parent_shares ==SHARE_DONT_CARE )) +return 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 ==SHARE_DONT_CARE ) +return 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 numtasks *res, +struct numtasks *parres) +{ +struct res_shares *par = &parres->shares; +struct res_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 (resource group to which parres belongs) lock held. +*/ +static void recalc_and_propagate (struct numtasks *res, +struct numtasks *parres) +{ +struct resource_group *child =NULL ; +struct numtasks *childres; + +if (parres) +recalc_self (res, parres); + +/* propagate to children */ +spin_lock (&res->rgroup->group_lock); +for_each_child (child, res->rgroup) { + childres =get_numtasks (child); +BUG_ON (!childres); +spin_lock (&child->group_lock); +recalc_and_propagate (childres, res); +spin_unlock (&child->group_lock); + } +spin_unlock (&res->rgroup->group_lock); +} + +static void numtasks_shares_changed (struct res_shares *my_res) +{ +struct numtasks *parres, *res; +struct res_shares *cur, *par; + + res =get_shares_numtasks (my_res); +if (!res) +return ; + cur = &res->shares; + +if (!is_res_group_root (res->rgroup)) { +spin_lock (&res->rgroup->parent->group_lock); + parres =get_numtasks (res->rgroup->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 (!is_res_group_root (res->rgroup)) +spin_unlock (&res->rgroup->parent->group_lock); +} + +static ssize_t numtasks_show_stats (struct res_shares *my_res, +char *buf,size_t buf_size) +{ +ssize_t i, j =0 ; +struct 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 res_controller numtasks_ctlr = { .name = res_ctlr_name, .depth_supported =3 ,
@@ -171,6 +303,8 @@ struct res_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_numtasks_res (void )
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------PATCH 1/6 numtasks - Initialization routines
In reply to Chandra Seetharaman1/6: numtasks_init
Hooks up with Resource Groups core by defining the basic alloc/free functions and registering the controller with the core. --
init/Kconfig | 10 ++++ kernel/res_group/Makefile | 1 kernel/res_group/numtasks.c | 90 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+)
kernel/res_group/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-y = res_group.o shares.o task.o +obj-$(CONFIG_RES_GROUPS_NUMTASKS) += numtasks.o obj-$(CONFIG_RGCS) += rgcs.o
kernel/res_group/numtasks.c new file +90 -0
90 lines not on this page · raw message
init/Kconfig +10 -0
@@ -175,6 +175,16 @@ config RGCS Say M if unsure, Y to save on module loading. N doesn't make sense when Resource Groups has been configured. +config RES_GROUPS_NUMTASKS + bool "Number of Tasks Resource Controller" + depends on RES_GROUPS + default y + help + Provides a Resource Controller for Resource Groups that allows + limiting number of tasks a resource group 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.
----------------------------------------------------------------------PATCH 2/6 numtasks - Add task control support
In reply to Chandra Seetharaman2/6: numtasks_tasksupport
Adds task management support by defining a function to be called from fork() to see if the resource group is within its share allocation Sets interface to be called from core when a task is moved to a resource group.
--
include/linux/numtasks.h | 28 +++++++++++ kernel/fork.c | 7 ++ kernel/res_group/numtasks.c | 104 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 2 deletions(-)
include/linux/numtasks.h new file +28 -0
@@ -0,0 +1,28 @@ +/* numtasks.h - No. of tasks resource controller for Resource Groups +* +* Copyright (C) Chandra Seetharaman, IBM Corp. 2003, 2004, 2005 +* +* Provides No. of tasks resource controller for Resource Groups +* +* 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_NUMTASKS_H +#define _LINUX_NUMTASKS_H + +#ifdef CONFIG_RES_GROUPS_NUMTASKS +#include <linux/res_group_rc.h> + +extern int numtasks_allow_fork (struct resource_group *); + +#else /* CONFIG_RES_GROUPS_NUMTASKS */ + +#define numtasks_allow_fork (rgroup) (0) + +#endif /* CONFIG_RES_GROUPS_NUMTASKS */ +#endif /* _LINUX_NUMTASKS_H */
kernel/res_group/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/res_group.h> +#include <linux/numtasks.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -1311,7 +1312,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;
@@ -1324,6 +1325,10 @@ long do_fork(unsigned long clone_flags, clone_flags |=CLONE_PTRACE ; } + rc =numtasks_allow_fork (current->res_group); +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.
----------------------------------------------------------------------PATCH 4/6 numtasks - Add configuration support
In reply to Chandra Seetharaman4/6: numtasks_config
Use module parameters to dynamically set the total numtasks and maximum forkrate allowed
--
include/linux/moduleparam.h | 12 ++++++-- kernel/res_group/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/res_group/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.
----------------------------------------------------------------------PATCH 5/6 numtasks - Add fork rate control support
In reply to Chandra Seetharaman5/6: numtasks_forkrate
Adds support to control the forkrate in the system. --
numtasks.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 75 insertions(+)
kernel/res_group/numtasks.c +75 -0
@@ -27,6 +27,11 @@ static int total_numtasks __read_mostlystatic struct resource_group *root_rgroup;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 numtasks {struct resource_group *rgroup;/* resource group i am part of... */ struct res_shares shares;
@@ -41,6 +46,11 @@ struct numtasks {/* stats */ int successes;int failures; +int forkrate_failures; + +/* Fork rate fields */ +int forks_in_period; +unsigned long period_start; };struct res_controller numtasks_ctlr;
@@ -58,8 +68,24 @@ static struct numtasks *get_numtasks(str &numtasks_ctlr)); } +static inline int check_forkrate (struct 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 resource_group *rgroup) { +int rc;struct numtasks *res;/* controller is not registered; no resource group is given */
@@ -71,6 +97,11 @@ int numtasks_allow_fork(struct resource_if (!res)return 0 ; +/* Check forkrate before checking resource group's usage */ + rc =check_forkrate (res); +if (rc) +return rc; +if (res->cnt_max_shares ==SHARE_DONT_CARE )return 0 ;
@@ -146,6 +177,7 @@ static void numtasks_res_init_one(struct numtasks_res->cnt_min_shares =SHARE_DONT_CARE ; numtasks_res->cnt_unused =SHARE_DONT_CARE ; numtasks_res->cnt_max_shares =SHARE_DONT_CARE ; + numtasks_res->period_start = jiffies; } static struct res_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 resource_group *rgroup,unsigned long now) +{ +struct numtasks *res; +struct resource_group *child =NULL ; + + res =get_numtasks (rgroup); +if (!res) +return ; + res->forks_in_period =0 ; + res->period_start = now; + +spin_lock (&rgroup->group_lock); +for_each_child (child, rgroup) +reset_forkrates (child, now); +spin_unlock (&rgroup->group_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_rgroup, 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_rgroup, jiffies); +return 0 ; +} +module_param_set_call (forkrate_interval, int, set_forkrate_interval, +S_IRUGO |S_IWUSR ); +int __init init_numtasks_res (void ) {if (numtasks_ctlr.ctlr_id !=NO_RES_ID )
Signature · 4 lines
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- [email protected] | .......you may get it.
----------------------------------------------------------------------PATCH 6/6 numtasks - Documentation for Numtasks controller
In reply to Chandra Seetharaman6/6: numtasks_docs
Documents what the numtasks controller does and how to use it. --
numtasks | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 133 insertions(+)
Documentation/res_groups/numtasks new file +133 -0
@@ -0,0 +1,133 @@ +Introduction +------------- + +Numtasks is a resource controller under the Resource Groups framework that +allows the user/sysadmin to + - manage the number of tasks a resource group can create. + - limit the fork rate across the system. + +As with any other resource under the Resource Groups framework, numtasks also +assigns all the resources to the default resource group(/config/res_groups). +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/numtasks/parameters/ + +Installation +------------- + +1. Configure "Number of Tasks Resource Manager" under Resource Groups (see + Documentation/res_groups/installation). + +2. Reboot the system with the new kernel. + +3. Verify the controller's presence by reading the file + /config/res_groups/shares (should show a line with res=numtasks) + +Usage +----- + +For brevity, unless otherwise specified all the following commands are +executed in the default resource group(/config/res_groups). + +As explained above, files in /sys/module/numtasks/parameters/ +shows total_numtasks and forkrate info. + + # cd /sys/module/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 +resource group. +In other words, the numbers in shares file will be the absolute number of +tasks a resource group is allowed. + + # cd /config/res_groups + # 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 resource group share +------------------------- + +'min_shares' specifies the number of tasks this resource group is entitled +to get +'max_shares' is the maximum number of tasks this resource group can get. + +Following command will set the min_shares of resource group c1 to be 250 +and max_shares 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/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 resource group will be allowed 'forkrate' forks in +'forkrate_interval' seconds. + +Monitoring +---------- + +stats file shows statistics of the number of tasks usage of a resource +group + # cd /config/res_groups + # 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
Andrew,
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
on top of Resource Groups.
Please consider these for inclusion in -mm tree.
--
Patch descriptions:
1/6: numtasks_init
Hooks up with Resource Groups core by defining the basic alloc/free functions and registering the controller with the core.
2/6: numtasks_tasksupport
Adds task management support by defining a function to be called from fork() to see if the resource group is within its share allocation Sets interface to be called from core when a task is moved to a resource group.
3/6: numtasks_shares_n_stats
Sets interface to be called from core when a resource group's shares are changes or when stats are requested.
4/6: numtasks_config
Use module parameters to dynamically set the total numtasks and maximum forkrate allowed.
5/6: numtasks_forkrate
Adds support to control the forkrate in the system.
6/6: 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.
----------------------------------------------------------------------