cgroups: add pids subsystem
Subsystems (4)
v10 cover cgroups: add pids subsystem
This is an updated version of v9 of the pids patchset[1], with a few small fixes:
* Fixed an off-by-one error in the SUBSYS_TAG macro that affects intermediate __unused_* values (causing issues when merging tags into the subsys enum)
* Updated the SUBSYS_TAG macro to be simpler (removed UNUSED_IDENT() magic) and not use __COUNTER__.
* Removed the SUBSYS_TAG_COUNT macro and replaced it with a hard-coded CGROUP_PREFORK_COUNT macro.
[1]: https://lkml.org/lkml/2015/4/11/237
Aleksa Sarai (4):
cgroups: use bitmask to filter for_each_subsys
cgroups: replace explicit ss_mask checking with for_each_subsys_which
cgroups: allow a cgroup subsystem to reject a fork
cgroups: implement the PIDs subsystem
include/linux/cgroup.h | 47 ++++-- include/linux/cgroup_subsys.h | 32 ++++ init/Kconfig | 16 ++ kernel/Makefile | 1 + kernel/cgroup.c | 183 +++++++++++++++------ kernel/cgroup_freezer.c | 2 +- kernel/cgroup_pids.c | 368 ++++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 19 ++- kernel/sched/core.c | 2 +- 9 files changed, 606 insertions(+), 64 deletions(-) create mode 100644 kernel/cgroup_pids.c
Signature · 1 line
2.3.5
PATCH v10 1/4 cgroups: use bitmask to filter for_each_subsys
Add a new macro for_each_subsys_which that allows all enabled cgroup subsystems to be filtered by a bitmask, such that mask & (1 << ssid) determines if the subsystem is to be processed in the loop body (where ssid is the unique id of the subsystem).
Also replace the need_forkexit_callback with two separate bitmasks for each callback to make (ss->{fork,exit}) checks unnecessary.
--- kernel/cgroup.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-)
kernel/cgroup.c +26 -17
@@ -175,12 +175,14 @@ static DEFINE_IDR(cgroup_hierarchy_idr);*/ static u64 css_serial_nr_next =1 ; -/* This flag indicates whether tasks in the fork and exit paths should +/* +* These bitmask flags indicate whether tasks in the fork and exit paths should * check for fork/exit handlers to call. This avoids us having to do * extra work in the fork/exit path if none of the subsystems need to * be called. */ -static int need_forkexit_callback__read_mostly ; +static int need_fork_callback__read_mostly ; +static int need_exit_callback__read_mostly ;static struct cftype cgroup_dfl_base_files[];static struct cftype cgroup_legacy_base_files[];
@@ -409,6 +411,19 @@ static int notify_on_release(const struct cgroup *cgrp)for ((ssid) =0 ; (ssid) <CGROUP_SUBSYS_COUNT && \ (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) +/** +* for_each_subsys_which - filter for_each_subsys with a bitmask +* @ss_mask: the bitmask +* @ss: the iteration cursor +* @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end +* +* The block will only run for cases where the ssid-th bit (1 << ssid) of +* mask is set to 1. + */ +#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid))) +/* iterate across the hierarchies */ #define for_each_root (root) \ list_for_each_entry((root), &cgroup_roots, root_list)
@@ -4932,7 +4947,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)* init_css_set is in the subsystem's root cgroup. */ init_css_set.subsys[ss->id] = css; - need_forkexit_callback |= ss->fork || ss->exit; + need_fork_callback |= (bool ) ss->fork << ss->id; + need_exit_callback |= (bool ) ss->exit << ss->id;/* At system boot, before all subsystems have been * registered, no tasks have been forked, so we don't
@@ -5239,11 +5255,8 @@ void cgroup_post_fork(struct task_struct *child)* css_set; otherwise, @child might change state between ->fork() * and addition to css_set. */ -if (need_forkexit_callback) { -for_each_subsys (ss, i) -if (ss->fork) - ss->fork (child); - } +for_each_subsys_which (need_fork_callback, ss, i) + ss->fork (child); }/**
@@ -5287,16 +5300,12 @@ void cgroup_exit(struct task_struct *tsk) cset =task_css_set (tsk);RCU_INIT_POINTER (tsk->cgroups, &init_css_set); -if (need_forkexit_callback) { -/* see cgroup_post_fork() for details */ -for_each_subsys (ss, i) { -if (ss->exit) { -struct cgroup_subsys_state *old_css = cset->subsys[i]; -struct cgroup_subsys_state *css =task_css (tsk, i); +/* see cgroup_post_fork() for details */ +for_each_subsys_which (need_exit_callback, ss, i) { +struct cgroup_subsys_state *old_css = cset->subsys[i]; +struct cgroup_subsys_state *css =task_css (tsk, i); - ss->exit (css, old_css, tsk); - } - } + ss->exit (css, old_css, tsk); }if (put_cset)
Signature · 1 line
2.3.5
Tejun Heo
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hello, Aleksa.
kernel/cgroup.c static DEFINE_IDR(cgroup_hierarchy_idr); · line 185
Quoted 3 lines · Aleksa Sarai
kernel/cgroup.c +2
-1-static int need_forkexit_callback__read_mostly ; +static int need_fork_callback__read_mostly ; +static int need_exit_callback__read_mostly ;
These are bitmasks now, right? Let's make them unsigned int.
kernel/cgroup.c static int notify_on_release(const struct cgroup *cgrp) · line 425
Quoted 4 lines
static struct cftype cgroup_dfl_base_files[];
kernel/cgroup.c +3
-0+#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid)))
Maybe using for_each_set_bit() is better?
#define for_each_subsys_which(ss_mask, ss, ssid) \ for_each_set_bit(ssid, &(ss_mask), CGROUP_SUBSYS_COUNT) \ if ((ss) = group_subsys[ssid] && false) \ ; \ else
kernel/cgroup.c static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 4951
Quoted 7 lines
kernel/cgroup.c +2
-1@@ -4932,7 +4947,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)* init_css_set is in the subsystem's root cgroup. */ init_css_set.subsys[ss->id] = css; - need_forkexit_callback |= ss->fork || ss->exit; + need_fork_callback |= (bool ) ss->fork << ss->id; + need_exit_callback |= (bool ) ss->exit << ss->id;
^
please drop the space here
Other than the above minor points, looks good to me.
Thanks for the persistence.
Signature · 1 line
tejun
Peter Zijlstra
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Quoted 8 lines · Tejun Heo
Hello, Aleksa.
kernel/cgroup.c static DEFINE_IDR(cgroup_hierarchy_idr); · line 185
Quoted 3 lines · Aleksa Sarai
kernel/cgroup.c +2
-1-static int need_forkexit_callback__read_mostly ; +static int need_fork_callback__read_mostly ; +static int need_exit_callback__read_mostly ;These are bitmasks now, right? Let's make them unsigned int.
If, as per the below you want to use the bitmap ops; it needs be unsigned long.
Quoted 7 lines
kernel/cgroup.c static int notify_on_release(const struct cgroup *cgrp) · line 425
Quoted 4 lines
static struct cftype cgroup_dfl_base_files[];
kernel/cgroup.c +3
-0+#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid)))Maybe using
for_each_set_bit()is better?
Quoted 5 lines
#define for_each_subsys_which(ss_mask, ss, ssid) \ for_each_set_bit(ssid, &(ss_mask), CGROUP_SUBSYS_COUNT) \ if ((ss) = group_subsys[ssid] && false) \ ; \ else
Clever that ;-)
Tejun Heo
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hey,
Quoted 2 lines · Peter Zijlstra
If, as per the below you want to use the bitmap ops; it needs be unsigned long.
Ah, right.
Quoted 7 lines
Quoted 7 lines
kernel/cgroup.c static int notify_on_release(const struct cgroup *cgrp) · line 425
Quoted 4 lines
static struct cftype cgroup_dfl_base_files[];
kernel/cgroup.c +3
-0+#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid)))Maybe using
for_each_set_bit()is better?
Quoted 7 lines
Quoted 5 lines
#define for_each_subsys_which(ss_mask, ss, ssid) \ for_each_set_bit(ssid, &(ss_mask), CGROUP_SUBSYS_COUNT) \ if ((ss) = group_subsys[ssid] && false) \ ; \ elseClever that ;-)
Thanks. It kinda bothers me that for_each_set_bit() doesn't collapse to combo of ffs() + clearing bit off of a temp mask when size is const and <= ulong, which would be quite a bit lighter. Right now it'd be calling into generic find_first/next_bit() functions unconditionally. Ah well, it can be optimized later.
Signature · 1 line
tejun
Re: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hey,
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
kernel/cgroup.c static int notify_on_release(const struct cgroup *cgrp) · line 425
Quoted 4 lines
static struct cftype cgroup_dfl_base_files[];
kernel/cgroup.c +3
-0+#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid)))Maybe using
for_each_set_bit()is better?#define for_each_subsys_which(ss_mask, ss, ssid) \ for_each_set_bit(ssid, &(ss_mask), CGROUP_SUBSYS_COUNT) \ if ((ss) = group_subsys[ssid] && false) \ ; \ else
Quoted 7 lines
Quoted 1 line
Clever that ;-)
Thanks. It kinda bothers me that
for_each_set_bit()doesn't collapse to combo offfs()+ clearing bit off of a temp mask when size is const and <= ulong, which would be quite a bit lighter. Right now it'd be calling into generic find_first/next_bit() functions unconditionally. Ah well, it can be optimized later.
This macro causes issues with lines like:
for_each_subsys_which(ss, ssid, ~cgrp_dfl_root_inhibit_ss_mask)
In addition, there are a bunch of cgroup_* functions that use unsigned ints for bitops (cgroup_calc_child_subsys_mask, rebind_subsystems, cgroup_print_ss_mask). Is there a better solution to this problem, or should I just switch back to my naive solution?
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hello,
Quoted 4 lines · Aleksa Sarai
In addition, there are a bunch of cgroup_* functions that use unsigned ints for bitops (
cgroup_calc_child_subsys_mask,rebind_subsystems,cgroup_print_ss_mask). Is there a better solution to this problem, or should I just switch back to my naive solution?
Hmmmm... You can either convert all masks to ulong (which is fine) or do something like the following.
#define for_each_subsys_which(ss_mask, ss, ssid) \
unsigned long __tmp_mask = (ss_mask); \
for_each_set_bit(ssid, &__tmp_mask, CGROUP_SUBSYS_COUNT)\
if ((ss) = group_subsys[ssid] && false) \
; \
elseThanks.
Signature · 1 line
tejun
Re: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hi Tejun,
Quoted 9 lines
Hmmmm... You can either convert all masks to ulong (which is fine) or do something like the following.
#define for_each_subsys_which(ss_mask, ss, ssid) \ unsigned long __tmp_mask = (ss_mask); \ for_each_set_bit(ssid, &__tmp_mask, CGROUP_SUBSYS_COUNT)\ if ((ss) = group_subsys[ssid] && false) \ ; \ else
I think I'll just do casting and manually make new variables where required. The above (and similar) doesn't work very well, generates warnings like crazy and breaks stuff like:
if (...)
for_each_subsys_which(...)--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
Hello, Aleksa.
Quoted 6 lines · Aleksa Sarai
I think I'll just do casting and manually make new variables where required. The above (and similar) doesn't work very well, generates warnings like crazy and breaks stuff like:
if (...) for_each_subsys_which(...)
Ah, right, and we can't wrap the declaration in a new block. I'm okay with converting ss_masks to unsigned longs if necessary. Just do a wholesale conversion.
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerRe: [PATCH v10 1/4] cgroups: use bitmask to filter for_each_subsys
In reply to Aleksa SaraiOoh, just one more thing.
kernel/cgroup.c static int notify_on_release(const struct cgroup *cgrp) · line 426
Quoted 13 lines · Aleksa Sarai
kernel/cgroup.c +13
-0+/** +* for_each_subsys_which - filter for_each_subsys with a bitmask +* @ss_mask: the bitmask +* @ss: the iteration cursor +* @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end +* +* The block will only run for cases where the ssid-th bit (1 << ssid) of +* mask is set to 1. +*/ +#define for_each_subsys_which (ss_mask, ss, ssid) \ + for_each_subsys((ss), (ssid)) \ + if ((ss_mask) & (1 << (ssid))) +
This isn't completely consistent but we tend to put the cursors in front of what's being iterated. e.g.
for_each_css(css, ssid, cgrp)
for_each_set_bit(bit, addr, size)
Following the pattern, for_each_subsys_which() prolly should do
for_each_subsys_which(ss, ssid, ss_mask)
rather than the other way around.
Thanks.
Signature · 1 line
tejun
PATCH v10 2/4 cgroups: replace explicit ss_mask checking with for_each_subsys_which
In reply to Aleksa SaraiReplace the explicit checking against ss_masks inside a for_each_subsys block with for_each_subsys_which(ss_mask, ...), to take advantage of the more readable macro.
--- kernel/cgroup.c | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-)
kernel/cgroup.c +14 -28
@@ -1099,9 +1099,8 @@ static unsigned int cgroup_calc_child_subsys_mask(struct cgroup *cgrp,while (true) {unsigned int new_ss_mask = cur_ss_mask; -for_each_subsys (ss, ssid) -if (cur_ss_mask & (1 << ssid)) - new_ss_mask |= ss->depends_on; +for_each_subsys_which (cur_ss_mask, ss, ssid) + new_ss_mask |= ss->depends_on;/* * Mask out subsystems which aren't available. This can
@@ -1238,10 +1237,7 @@ static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)lockdep_assert_held (&cgroup_mutex); -for_each_subsys (ss, ssid) { -if (!(ss_mask & (1 << ssid))) -continue ; - +for_each_subsys_which (ss_mask, ss, ssid) {/* if @ss has non-root csses attached to it, can't move */ if (css_next_child (NULL ,cgroup_css (&ss->root->cgrp, ss)))return -EBUSY ;
@@ -1278,18 +1274,14 @@ static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)* Nothing can fail from this point on. Remove files for the * removed subsystems and rebind each subsystem. */ -for_each_subsys (ss, ssid) -if (ss_mask & (1 << ssid)) -cgroup_clear_dir (&ss->root->cgrp,1 << ssid); +for_each_subsys_which (ss_mask, ss, ssid) +cgroup_clear_dir (&ss->root->cgrp,1 << ssid); -for_each_subsys (ss, ssid) { +for_each_subsys_which (ss_mask, ss, ssid) {struct cgroup_root *src_root;struct cgroup_subsys_state *css;struct css_set *cset; -if (!(ss_mask & (1 << ssid))) -continue ; - src_root = ss->root; css =cgroup_css (&src_root->cgrp, ss);
@@ -2563,13 +2555,11 @@ static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)bool printed = false;int ssid; -for_each_subsys (ss, ssid) { -if (ss_mask & (1 << ssid)) { -if (printed) -seq_putc (seq,' ' ); -seq_printf (seq,"%s" , ss->name); - printed = true; - } +for_each_subsys_which (ss_mask, ss, ssid) { +if (printed) +seq_putc (seq,' ' ); +seq_printf (seq,"%s" , ss->name); + printed = true; }if (printed)seq_putc (seq,'\n' );
@@ -2719,9 +2709,8 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,while ((tok =strsep (&buf," " ))) {if (tok[0 ] =='\0' )continue ; -for_each_subsys (ss, ssid) { -if (ss->disabled ||strcmp (tok +1 , ss->name) || - ((1 << ss->id) & cgrp_dfl_root_inhibit_ss_mask)) +for_each_subsys_which (~cgrp_dfl_root_inhibit_ss_mask, ss, ssid) { +if (ss->disabled ||strcmp (tok +1 , ss->name))continue ;if (*tok =='+' ) {
@@ -2808,10 +2797,7 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,* still around. In such cases, wait till it's gone using * offline_waitq. */ -for_each_subsys (ss, ssid) { -if (!(css_enable & (1 << ssid))) -continue ; - +for_each_subsys_which (css_enable, ss, ssid) {cgroup_for_each_live_child (child, cgrp) {DEFINE_WAIT (wait);
Signature · 1 line
2.3.5
Tejun Heo
maintainerRe: [PATCH v10 2/4] cgroups: replace explicit ss_mask checking with for_each_subsys_which
Quoted 5 lines · Aleksa Sarai
Replace the explicit checking against
ss_masksinside afor_each_subsysblock withfor_each_subsys_which(ss_mask, ...), to take advantage of the more readable macro.Signed-off-by Aleksa Sarai <cyphar-gVpy/LI/[email protected]>
Looks good to me.
Thanks.
Signature · 1 line
tejun
PATCH v10 3/4 cgroups: allow a cgroup subsystem to reject a fork
In reply to Aleksa SaraiAdd a new cgroup subsystem callback can_fork that conditionally states whether or not the fork is accepted or rejected by a cgroup policy. In addition, add a cancel_fork callback so that if an error occurs later in the forking process, any state modified by can_fork can be reverted.
Allow for a private opaque pointer to be passed from the cgroup_can_fork to cgroup_post_fork, allowing for the fork state to be stored by each subsystem separately.
In order for a subsystem to know that a task associated with a cgroup hierarchy is being migrated to another hierarchy, add a detach callback to the subsystem which is run after the migration has been confirmed but before the old_cset's refcount is dropped. This is necessary in order for a subsystem to be able to keep a proper count of how many tasks are associated with that subsystem.
Also add a tagging system for cgroup_subsys.h to allow for CGROUP_<TAG> enumerations to be be defined and used (as well as CGROUP_<TAG>_COUNT).
This is in preparation for implementing the pids cgroup subsystem.
--- include/linux/cgroup.h | 47 +++++++++++++----- include/linux/cgroup_subsys.h | 27 +++++++++++ kernel/cgroup.c | 108 ++++++++++++++++++++++++++++++++++++++---- kernel/cgroup_freezer.c | 2 +- kernel/fork.c | 19 +++++++- kernel/sched/core.c | 2 +- 6 files changed, 181 insertions(+), 24 deletions(-)
include/linux/cgroup.h +36 -11
@@ -25,6 +25,19 @@#ifdef CONFIG_CGROUPS +/* define the enumeration of all cgroup subsystems */ +enum cgroup_subsys_id { +#define SUBSYS(_x) _x ## _cgrp_id, +#define SUBSYS_TAG (_t) CGROUP_ ## _t, \ + __unused_tag_ ## _t = CGROUP_ ## _t - 1, +#include <linux/cgroup_subsys.h> +#undef SUBSYS_TAG +#undef SUBSYS + CGROUP_SUBSYS_COUNT, +}; + +#define CGROUP_PREFORK_COUNT (CGROUP_PREFORK_END - CGROUP_PREFORK_START) +struct cgroup_root ;struct cgroup_subsys ;struct cgroup ;
@@ -32,7 +45,12 @@ struct cgroup;extern int cgroup_init_early (void );extern int cgroup_init (void );extern void cgroup_fork (struct task_struct *p); -extern void cgroup_post_fork (struct task_struct *p); +extern int cgroup_can_fork (struct task_struct *p, +void *ss_state[CGROUP_PREFORK_COUNT ]); +extern void cgroup_cancel_fork (struct task_struct *p, +void *ss_state[CGROUP_PREFORK_COUNT ]); +extern void cgroup_post_fork (struct task_struct *p, +void *old_ss_state[CGROUP_PREFORK_COUNT ]);extern void cgroup_exit (struct task_struct *p);extern int cgroupstats_build (struct cgroupstats *stats,struct dentry *dentry);
@@ -40,14 +58,6 @@ extern int cgroupstats_build(struct cgroupstats *stats,extern int proc_cgroup_show (struct seq_file *m,struct pid_namespace *ns,struct pid *pid,struct task_struct *tsk); -/* define the enumeration of all cgroup subsystems */ -#define SUBSYS (_x) _x ## _cgrp_id, -enum cgroup_subsys_id { -#include <linux/cgroup_subsys.h> - CGROUP_SUBSYS_COUNT, -}; -#undef SUBSYS -/* * Per-subsystem/per-cgroup state maintained by the system. This is the * fundamental structural building block that controllers deal with.
@@ -649,7 +659,11 @@ struct cgroup_subsys { struct cgroup_taskset *tset);void (*attach)(struct cgroup_subsys_state *css,struct cgroup_taskset *tset); -void (*fork)(struct task_struct *task); +void (*detach)(struct cgroup_subsys_state *old_css, +struct task_struct *task); +int (*can_fork)(struct task_struct *task,void **private); +void (*cancel_fork)(struct task_struct *task,void *private); +void (*fork)(struct task_struct *task,void *private);void (*exit)(struct cgroup_subsys_state *css,struct cgroup_subsys_state *old_css,struct task_struct *task);
@@ -945,10 +959,21 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,struct cgroup_subsys_state ; +#define CGROUP_PREFORK_COUNT 0 +static inline int cgroup_init_early (void ) {return 0 ; }static inline int cgroup_init (void ) {return 0 ; }static inline void cgroup_fork (struct task_struct *p) {} -static inline void cgroup_post_fork (struct task_struct *p) {} +static inline int cgroup_can_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) +{ +return 0 ; +} +static inline void cgroup_cancel_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) {} +static inline void cgroup_post_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) {} +static inline void cgroup_exit (struct task_struct *p) {} static inline int cgroupstats_build(struct cgroupstats *stats,
include/linux/cgroup_subsys.h +27 -0
@@ -3,6 +3,16 @@* * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS. */ +#ifndef SUBSYS +# define __TMP_SUBSYS +# define SUBSYS (_x) +#endif + +#ifndef SUBSYS_TAG +# define __TMP_SUBSYS_TAG +# define SUBSYS_TAG (_t) +#endif +#if IS_ENABLED (CONFIG_CPUSETS ) SUBSYS(cpuset)#endif
@@ -48,11 +58,28 @@ SUBSYS(hugetlb)#endif /* +* Subsystems that implement the can_fork() family of callbacks. +*/ +SUBSYS_TAG(PREFORK_START) +SUBSYS_TAG(PREFORK_END) + +/* * The following subsystems are not supported on the default hierarchy. */ #if IS_ENABLED(CONFIG_CGROUP_DEBUG) SUBSYS(debug) #endif + +#ifdef __TMP_SUBSYS +# undef __TMP_SUBSYS +# undef SUBSYS +#endif + +#ifdef __TMP_SUBSYS_TAG +# undef __TMP_SUBSYS_TAG +# undef SUBSYS_TAG +#endif + /* * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS. */
kernel/cgroup.c +99 -9
168 lines not on this page · raw message
kernel/cgroup_freezer.c +1 -1
@@ -203,7 +203,7 @@ static void freezer_attach(struct cgroup_subsys_state *new_css,* to do anything as freezer_attach() will put @task into the appropriate * state. */ -static void freezer_fork (struct task_struct *task) +static void freezer_fork (struct task_struct *task,void *private) {struct freezer *freezer;
kernel/fork.c +17 -2
@@ -1196,6 +1196,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, {int retval;struct task_struct *p; +void *ss_state[CGROUP_PREFORK_COUNT ] = {};if ((clone_flags & (CLONE_NEWNS |CLONE_FS )) == (CLONE_NEWNS |CLONE_FS ))return ERR_PTR (-EINVAL );
@@ -1468,6 +1469,18 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->thread_group); p->task_works =NULL ; + +/* +* Ensure that the cgroup subsystem policies allow the new process to be +* forked. If this fork is happening in an organization operation, then +* this will not charge the correct css_set. This is fixed during +* cgroup_post_fork() (when the css_set has been updated) by undoing +* this operation and forcefully charging the correct css_set. +*/ + retval =cgroup_can_fork (p, ss_state); +if (retval) + goto bad_fork_free_pid; +/* * Make it visible to the rest of the system, but dont wake it up yet. * Need tasklist lock for parent etc handling!
@@ -1504,7 +1517,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,spin_unlock (¤t->sighand->siglock);write_unlock_irq (&tasklist_lock); retval = -ERESTARTNOINTR ; - goto bad_fork_free_pid; + goto bad_fork_cancel_cgroup; }if (likely (p->pid)) {
@@ -1546,7 +1559,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,write_unlock_irq (&tasklist_lock);proc_fork_connector (p); -cgroup_post_fork (p); +cgroup_post_fork (p, ss_state);if (clone_flags &CLONE_THREAD )threadgroup_change_end (current);perf_event_fork (p);
@@ -1556,6 +1569,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,return p; +bad_fork_cancel_cgroup: +cgroup_cancel_fork (p, ss_state); bad_fork_free_pid:if (pid != &init_struct_pid)free_pid (pid);
kernel/sched/core.c +1 -1
@@ -7988,7 +7988,7 @@ static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)sched_offline_group (tg); } -static void cpu_cgroup_fork (struct task_struct *task) +static void cpu_cgroup_fork (struct task_struct *task,void *private) {sched_move_task (task); }
Signature · 1 line
2.3.5
Tejun Heo
maintainerRe: [PATCH v10 3/4] cgroups: allow a cgroup subsystem to reject a fork
Quoted 4 lines · Aleksa Sarai
include/linux/cgroup.h +0
-0@@ -25,6 +25,19 @@#ifdef CONFIG_CGROUPS
include/linux/cgroup.h line 37
Quoted 10 lines
include/linux/cgroup.h +10
-0+/* define the enumeration of all cgroup subsystems */ +enum cgroup_subsys_id { +#define SUBSYS(_x) _x ## _cgrp_id, +#define SUBSYS_TAG (_t) CGROUP_ ## _t, \ + __unused_tag_ ## _t = CGROUP_ ## _t - 1, +#include <linux/cgroup_subsys.h> +#undef SUBSYS_TAG +#undef SUBSYS + CGROUP_SUBSYS_COUNT, +};
It's generally a good idea to not combine code movement and modification as it tends to obfuscate what's going on. Can you please create a prep patch to move the chunk above?
include/linux/cgroup.h line 39
Quoted 1 line
include/linux/cgroup.h +1
-0+#define CGROUP_PREFORK_COUNT (CGROUP_PREFORK_END - CGROUP_PREFORK_START)
Is it prefork or can_fork? Given post_fork, maybe pre_fork is a more consistent name?
Quoted 5 lines
include/linux/cgroup.h +1
-0+struct cgroup_root ;struct cgroup_subsys ;struct cgroup ;
include/linux/cgroup.h struct cgroup; · line 53
Quoted 10 lines
include/linux/cgroup.h +6
-1@@ -32,7 +45,12 @@ struct cgroup;extern int cgroup_init_early (void );extern int cgroup_init (void );extern void cgroup_fork (struct task_struct *p); -extern void cgroup_post_fork (struct task_struct *p); +extern int cgroup_can_fork (struct task_struct *p, +void *ss_state[CGROUP_PREFORK_COUNT ]); +extern void cgroup_cancel_fork (struct task_struct *p, +void *ss_state[CGROUP_PREFORK_COUNT ]); +extern void cgroup_post_fork (struct task_struct *p, +void *old_ss_state[CGROUP_PREFORK_COUNT ]);
Also, why are they named @ss_state when the param to the callbacks are @private? Wouldn't ss_private[] be more consistent?
Quoted 5 lines
include/linux/cgroup.h +1
-0@@ -945,10 +959,21 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,struct cgroup_subsys_state ; +#define CGROUP_PREFORK_COUNT 0
include/linux/cgroup.h struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, · line 971
Quoted 10 lines
include/linux/cgroup.h +6
-1+static inline int cgroup_init_early (void ) {return 0 ; }static inline int cgroup_init (void ) {return 0 ; }static inline void cgroup_fork (struct task_struct *p) {} -static inline void cgroup_post_fork (struct task_struct *p) {} +static inline int cgroup_can_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) +{ +return 0 ; +}
Style consistency?
include/linux/cgroup.h struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, · line 975
Quoted 4 lines
include/linux/cgroup.h +4
-0+static inline void cgroup_cancel_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) {} +static inline void cgroup_post_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) {}
Why are the arguments named @s here?
include/linux/cgroup_subsys.h line 9
Quoted 12 lines
include/linux/cgroup_subsys.h +4
-0@@ -3,6 +3,16 @@* * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS. */ +#ifndef SUBSYS +# define __TMP_SUBSYS +# define SUBSYS (_x) +#endif
Does it ever make sense to include cgroup_subsys.h w/o SUBSYS macro?
...
include/linux/cgroup_subsys.h line 16
Quoted 7 lines
include/linux/cgroup_subsys.h +4
-0+* These bitmask flags indicate whether tasks in the fork and exit paths +* should check for fork/exit handlers to call. This avoids us having to do +* extra work in the fork/exit path if a subsystems doesn't need to be +* called. */ static int need_fork_callback__read_mostly ;static int need_exit_callback__read_mostly ;
This comment belongs to an earlier patch but need_ is a bit misnomer at this point. Shouldn't it be more like have_fork_callback?
include/linux/cgroup_subsys.h
Quoted 4 lines
include/linux/cgroup_subsys.h +3
-0+/* Ditto for the can_fork/cancel_fork/reapply_fork callbacks. */ +static int need_canfork_callback__read_mostly ; +static int need_cancelfork_callback__read_mostly ;
And given that the reason we have these masks is avoiding iteration in relatively hot paths. Does cancelfork mask make sense?
include/linux/cgroup_subsys.h static int notify_on_release(const struct cgroup *cgrp) · line 419
Quoted 6 lines
include/linux/cgroup_subsys.h +1
-1@@ -412,7 +416,7 @@ static int notify_on_release(const struct cgroup *cgrp) (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)/** -* for_each_subsys_which - filter for_each_subsys with a bitmask +* for_each_subsys_which - filter for_each_subsys with a subsys bitmask
Does this chunk belong to this patch?
Quoted 4 lines
include/linux/cgroup_subsys.h +0
-0@@ -2078,6 +2084,18 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,list_move_tail (&tsk->cg_list, &new_cset->mg_tasks);/*
include/linux/cgroup_subsys.h static void cgroup_task_migrate(struct cgroup *old_cgrp, · line 2096
Quoted 10 lines
include/linux/cgroup_subsys.h +10
-0+* We detach from the old_cset subsystems here. We must do this +* before we drop the refcount for old_cset, in order to make sure +* that nobody frees it underneath us. +*/ +for_each_e_css (css, i, old_cgrp) { +struct cgroup_subsys_state *old_css = old_cset->subsys[i]; + +if (old_css->ss->detach) + old_css->ss->detach (old_css, tsk); + }
I don't get this. What can ->detach do that ->can_attach cannot?
include/linux/cgroup_subsys.h static void cgroup_task_migrate(struct cgroup *old_cgrp,
Quoted 11 lines
include/linux/cgroup_subsys.h +11
-0+void cgroup_cancel_fork (struct task_struct *child, +void *ss_state[CGROUP_PREFORK_COUNT ]) +{ +struct cgroup_subsys *ss; +int i; + +for_each_subsys_which (need_cancelfork_callback, ss, i) { +void *state =NULL ; + +if (CGROUP_PREFORK_START <= i && i <CGROUP_PREFORK_END ) + state = ss_state[i -CGROUP_PREFORK_START ];
Maybe we want a helper callback which does
if (CGROUP_PREFORK_START <= ssid && ssid < CGROUP_PREFORK_END) return &ss_state[ssid - CGROUP_PREFORK_START]; return NULL;
include/linux/cgroup_subsys.h static struct task_struct *copy_process(unsigned long clone_flags, · line 1199
Quoted 10 lines
include/linux/cgroup_subsys.h +5
-0+ + ss->cancel_fork (child, state); + } +}@@ -1196,6 +1196,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, {int retval;struct task_struct *p; +void *ss_state[CGROUP_PREFORK_COUNT ] = {};
Please use a name which signifies that this is for cgroups.
include/linux/cgroup_subsys.h static struct task_struct *copy_process(unsigned long clone_flags, · line 1479
Quoted 12 lines
include/linux/cgroup_subsys.h +8
-0@@ -1468,6 +1469,18 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->thread_group); p->task_works =NULL ; + +/* +* Ensure that the cgroup subsystem policies allow the new process to be +* forked. If this fork is happening in an organization operation, then +* this will not charge the correct css_set. This is fixed during +* cgroup_post_fork() (when the css_set has been updated) by undoing +* this operation and forcefully charging the correct css_set. +*/
I'm not sure the above description is appropriate for copy_process(). From copy_process()'s perspective, it doesn't matter what goes on internally and the behavior being described is pretty specific to the way the pids controller is implemented.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 3/4] cgroups: allow a cgroup subsystem to reject a fork
Hey,
Quoted 5 lines
Quoted 5 lines
include/linux/cgroup.h +2
-0+#define CGROUP_PREFORK_COUNT 0 +static inline int cgroup_init_early (void ) {return 0 ; }static inline int cgroup_init (void ) {return 0 ; }static inline void cgroup_fork (struct task_struct *p) {}
Quoted 8 lines
include/linux/cgroup.h struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, · line 971
Quoted 6 lines
include/linux/cgroup.h +5
-1-static inline void cgroup_post_fork (struct task_struct *p) {} +static inline int cgroup_can_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) +{ +return 0 ; +}Style consistency?
It's because it wraps. I can move it to be something like:
static inline int cgroup_can_fork(struct task_struct *p,
void *s[CGROUP_PREFORK_COUNT])
{ return 0; }If you like.
Quoted 16 lines
Quoted 4 lines
kernel/cgroup.c +0
-0@@ -2078,6 +2084,18 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,list_move_tail (&tsk->cg_list, &new_cset->mg_tasks);/* kernel/cgroup.c static void cgroup_task_migrate(struct cgroup *old_cgrp, · line 2096
Quoted 10 lines
kernel/cgroup.c +10
-0+* We detach from the old_cset subsystems here. We must do this +* before we drop the refcount for old_cset, in order to make sure +* that nobody frees it underneath us. +*/ +for_each_e_css (css, i, old_cgrp) { +struct cgroup_subsys_state *old_css = old_cset->subsys[i]; + +if (old_css->ss->detach) + old_css->ss->detach (old_css, tsk); + }I don't get this. What can ->detach do that ->can_attach cannot?
->detach signifies that a task is being migrated away from a cgroup. On second thought, we could just use task_css() on each task in the tset to figure out what the cgroup the task is being migrated away from is and just uncharge that inside ->can_attach.
On the same point, are all the tasks in a tset passed to ->can_attach guaranteed to have the same css? Or do I have to uncharge each one individually?
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 3/4] cgroups: allow a cgroup subsystem to reject a fork
Quoted 16 lines · Aleksa Sarai
Hey,
Quoted 5 lines
Quoted 5 lines
include/linux/cgroup.h +2
-0+#define CGROUP_PREFORK_COUNT 0 +static inline int cgroup_init_early (void ) {return 0 ; }static inline int cgroup_init (void ) {return 0 ; }static inline void cgroup_fork (struct task_struct *p) {}Quoted 8 lines
include/linux/cgroup.h struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, · line 971
Quoted 6 lines
include/linux/cgroup.h +5
-1-static inline void cgroup_post_fork (struct task_struct *p) {} +static inline int cgroup_can_fork (struct task_struct *p, +void *s[CGROUP_PREFORK_COUNT ]) +{ +return 0 ; +}Style consistency?
Quoted 5 lines
It's because it wraps. I can move it to be something like:
static inline int cgroup_can_fork(struct
task_struct*p,void *s[CGROUP_PREFORK_COUNT]) { return 0; }
static inline int cgroup_can_fork(struct task_struct *p,
void *s[CGROUP_PREFORK_COUNT])
{ return 0; }
Quoted 17 lines
Quoted 16 lines
Quoted 4 lines
kernel/cgroup.c +0
-0@@ -2078,6 +2084,18 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,list_move_tail (&tsk->cg_list, &new_cset->mg_tasks);/* kernel/cgroup.c static void cgroup_task_migrate(struct cgroup *old_cgrp, · line 2096
Quoted 10 lines
kernel/cgroup.c +10
-0+* We detach from the old_cset subsystems here. We must do this +* before we drop the refcount for old_cset, in order to make sure +* that nobody frees it underneath us. +*/ +for_each_e_css (css, i, old_cgrp) { +struct cgroup_subsys_state *old_css = old_cset->subsys[i]; + +if (old_css->ss->detach) + old_css->ss->detach (old_css, tsk); + }I don't get this. What can ->detach do that ->can_attach cannot?
Quoted 4 lines
->detach signifies that a task is being migrated away from a cgroup. On second thought, we could just use
task_css()on each task in the tset to figure out what the cgroup the task is being migrated away from is and just uncharge that inside ->can_attach.
Yeah, that's how it's supposed to be used. You have access to both pre and post csses before migration and only to the new ones once by the time ->attach() is running.
Quoted 3 lines
On the same point, are all the tasks in a tset passed to ->can_attach guaranteed to have the same css? Or do I have to uncharge each one individually?
They're the same.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 3/4] cgroups: allow a cgroup subsystem to reject a fork
In reply to Tejun HeoHi Tejun,
Quoted 6 lines
kernel/cgroup.c static DEFINE_IDR(cgroup_hierarchy_idr); · line 189
Quoted 3 lines
kernel/cgroup.c +3
-0+/* Ditto for the can_fork/cancel_fork/reapply_fork callbacks. */ +static int need_canfork_callback__read_mostly ; +static int need_cancelfork_callback__read_mostly ;And given that the reason we have these masks is avoiding iteration in relatively hot paths. Does cancelfork mask make sense?
Do you still want me to remove it? I only added it because it made the callback code more consistent for cancel_fork and can_fork.
Quoted 14 lines
kernel/cgroup.c void cgroup_fork(struct task_struct *child) · line 5267
Quoted 11 lines
kernel/cgroup.c +11
-0+void cgroup_cancel_fork (struct task_struct *child, +void *ss_state[CGROUP_PREFORK_COUNT ]) +{ +struct cgroup_subsys *ss; +int i; + +for_each_subsys_which (need_cancelfork_callback, ss, i) { +void *state =NULL ; + +if (CGROUP_PREFORK_START <= i && i <CGROUP_PREFORK_END ) + state = ss_state[i -CGROUP_PREFORK_START ];Maybe we want a helper callback which does
Quoted 3 lines
if (CGROUP_PREFORK_START <= ssid && ssid < CGROUP_PREFORK_END)
return &ss_state[ssid - CGROUP_PREFORK_START];
return NULL;What would be a nice name for it? I can't think of anything better than __get_ss_private() and __get_ss_privatep().
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 3/4] cgroups: allow a cgroup subsystem to reject a fork
Hello, Aleksa.
Quoted 9 lines · Aleksa Sarai
Quoted 6 lines
kernel/cgroup.c static DEFINE_IDR(cgroup_hierarchy_idr); · line 189
Quoted 3 lines
kernel/cgroup.c +3
-0+/* Ditto for the can_fork/cancel_fork/reapply_fork callbacks. */ +static int need_canfork_callback__read_mostly ; +static int need_cancelfork_callback__read_mostly ;And given that the reason we have these masks is avoiding iteration in relatively hot paths. Does cancelfork mask make sense?
Do you still want me to remove it? I only added it because it made the callback code more consistent for
cancel_forkandcan_fork.
This doesn't make much sense to me. Why don't we have masks for other callbacks then? This way, we're breaking the consistency regarding why the mask is used in the first place.
Quoted 8 lines
Quoted 5 lines
Maybe we want a helper callback which does
if (CGROUP_PREFORK_START <= ssid && ssid < CGROUP_PREFORK_END) return &ss_state[ssid - CGROUP_PREFORK_START]; return NULL;What would be a nice name for it? I can't think of anything better than
__get_ss_private()and__get_ss_privatep().
Do we need the double underscores? subsys_prefork_priv()?
Thanks.
Signature · 1 line
tejun
PATCH v10 4/4 cgroups: implement the PIDs subsystem
In reply to Aleksa SaraiAdds a new single-purpose PIDs subsystem to limit the number of tasks that can be forked inside a cgroup. Essentially this is an implementation of RLIMIT_NPROC that applies to a cgroup rather than a process tree.
However, it should be noted that organisational operations (adding and removing tasks from a PIDs hierarchy) will *not* be prevented. Rather, the number of tasks in the hierarchy cannot exceed the limit through forking. This is due to the fact that, in the unified hierarchy, attach cannot fail (and it is not possible for a task to overcome its PIDs cgroup policy limit by attaching to a child cgroup).
PIDs are fundamentally a global resource, and it is possible to reach PID exhaustion inside a cgroup without hitting any reasonable kmemcg policy. Once you've hit PID exhaustion, you're only in a marginally better state than OOM. This subsystem allows PID exhaustion inside a cgroup to be prevented.
--- include/linux/cgroup_subsys.h | 5 + init/Kconfig | 16 ++ kernel/Makefile | 1 + kernel/cgroup_pids.c | 368 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 390 insertions(+) create mode 100644 kernel/cgroup_pids.c
include/linux/cgroup_subsys.h +5 -0
@@ -61,6 +61,11 @@ SUBSYS(hugetlb)* Subsystems that implement the can_fork() family of callbacks. */ SUBSYS_TAG(PREFORK_START) + +#if IS_ENABLED(CONFIG_CGROUP_PIDS) +SUBSYS(pids) +#endif + SUBSYS_TAG(PREFORK_END) /*
init/Kconfig +16 -0
@@ -952,6 +952,22 @@ config CGROUP_FREEZER Provides a way to freeze and unfreeze all tasks in a cgroup. +config CGROUP_PIDS + bool "PIDs cgroup subsystem" + help + Provides enforcement of process number limits in the scope of a + cgroup. Any attempt to fork more processes than is allowed in the + cgroup will fail. PIDs are fundamentally a global resource because it + is fairly trivial to reach PID exhaustion before you reach even a + conservative kmemcg limit. As a result, it is possible to grind a + system to halt without being limited by other cgroup policies. The + PIDs cgroup subsystem is designed to stop this from happening. + + It should be noted that organisational operations (such as attaching + to a cgroup hierarchy will *not* be blocked by the PIDs subsystem), + since the PIDs limit only affects a process's ability to fork, not to + attach to a cgroup. + config CGROUP_DEVICE bool "Device controller for cgroups" help
kernel/Makefile +1 -0
@@ -53,6 +53,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_PIDS) += cgroup_pids.o obj-$(CONFIG_CPUSETS) += cpuset.o obj-$(CONFIG_UTS_NS) += utsname.o obj-$(CONFIG_USER_NS) += user_namespace.o
kernel/cgroup_pids.c new file +368 -0
368 lines not on this page · raw message
Signature · 1 line
2.3.5
Tejun Heo
maintainerRe: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Quoted 18 lines
kernel/cgroup_pids.c new file +17
-0@@ -0,0 +1,368 @@ +/* +* Process number limiting controller for cgroups. +* +* Used to allow a cgroup hierarchy to stop any new processes +* from fork()ing after a certain limit is reached. +* +* Since it is trivial to hit the task limit without hitting +* any kmemcg limits in place, PIDs are a fundamental resource. +* As such, PID exhaustion must be preventable in the scope of +* a cgroup hierarchy by allowing resource limiting of the +* number of tasks in a cgroup. +* +* In order to use the `pids` controller, set the maximum number +* of tasks in pids.max (this is not available in the root cgroup +* for obvious reasons). The number of processes currently +* in the cgroup is given by pids.current. Organisational operations +* are not blocked by cgroup policies, so it is possible to have
kernel/cgroup_pids.c line 27
Quoted 10 lines
kernel/cgroup_pids.c new file +10
-0+* pids.current > pids.max. However, fork()s will still not work. +* +* To set a cgroup to have no limit, set pids.max to "max". fork() +* will return -EBUSY if forking would cause a cgroup policy to be +* violated. +* +* pids.current tracks all child cgroup hierarchies, so +* parent/pids.current is a superset of parent/child/pids.current. +* +* Copyright (C) 2015 Aleksa Sarai <cyphar-gVpy/LI/[email protected]>
The above text looks wrapped too narrow.
kernel/cgroup_pids.c line 45
Quoted 6 lines
kernel/cgroup_pids.c new file +6
-0+struct pids_cgroup { +struct cgroup_subsys_state css; + +/* +* Use 64-bit types so that we can safely represent "max" as +* (PID_MAX_LIMIT + 1).
^^^^^^^^^^^^^^^^^
...
kernel/cgroup_pids.c line 70
Quoted 10 lines
kernel/cgroup_pids.c new file +10
-0+static struct cgroup_subsys_state * +pids_css_alloc (struct cgroup_subsys_state *parent) +{ +struct pids_cgroup *pids; + + pids =kzalloc (sizeof (struct pids_cgroup ),GFP_KERNEL ); +if (!pids) +return ERR_PTR (-ENOMEM ); + + pids->limit =PIDS_MAX ;
^^^^^^^^^
Quoted 3 lines
kernel/cgroup_pids.c new file +3
-0+atomic64_set (&pids->counter,0 ); +return &pids->css; +}
...
kernel/cgroup_pids.c line 200
Quoted 7 lines
kernel/cgroup_pids.c new file +7
-0+static void pids_detach (struct cgroup_subsys_state *old_css, +struct task_struct *task) +{ +struct pids_cgroup *old_pids =css_pids (old_css); + +pids_uncharge (old_pids,1 ); +}
You can do the above as a part of can/cancel.
Quoted 1 line
+static int
pids_can_fork(struct task_struct *task, void **private)
Maybe @priv_p or something which signifies it's of different type from others?
Quoted 1 line
+{
...
kernel/cgroup_pids.c line 220
Quoted 7 lines
kernel/cgroup_pids.c new file +7
-0+rcu_read_lock (); + css =task_css (current, pids_cgrp_id); +if (!css_tryget_online (css)) { + retval = -EBUSY ; + goto err_rcu_unlock; + } +rcu_read_unlock ();
Hmmm... so, the above is guaranteed to succeed in finite amount of time (the race window is actually very narrow) and it'd be silly to fail fork because a task was being moved across cgroups.
I think it'd be a good idea to implement task_get_css() which loops and returns the current css for the requested subsystem with reference count bumped and it can use css_tryget() too. Holding a ref doesn't prevent css from dying anyway, so it doesn't make any difference.
Quoted 2 lines
(quoted diff) +2
-0+static void pids_fork(struct task_struct *task, void *private) +{
...
kernel/cgroup_pids.c line 260
Quoted 3 lines
kernel/cgroup_pids.c new file +3
-0+rcu_read_lock (); + css =task_css (task, pids_cgrp_id); +css_get (css);
Why is this safe? What guarantees that css's ref isn't already zero at this point?
Quoted 18 lines
kernel/cgroup_pids.c new file +18
-0+rcu_read_unlock (); + + pids =css_pids (css); + +/* +* The association has changed, we have to revert and reapply the +* charge/uncharge on the wrong hierarchy to the current one. Since +* the association can only change due to an organisation event, its +* okay for us to ignore the limit in this case. +*/ +if (pids != old_pids) { +pids_uncharge (old_pids,1 ); +pids_charge (pids,1 ); + } + +css_put (css); +css_put (old_css); +}
...
Quoted 10 lines
kernel/cgroup_pids.c new file +10
-0+static ssize_t pids_max_write (struct kernfs_open_file *of,char *buf, +size_t nbytes,loff_t off) +{ +struct cgroup_subsys_state *css =of_css (of); +struct pids_cgroup *pids =css_pids (css); +int64_t limit; +int err; + + buf =strstrip (buf); +if (!strcmp (buf,PIDS_MAX_STR )) {
kernel/cgroup_pids.c line 308
Quoted 10 lines
kernel/cgroup_pids.c new file +10
-0+ limit =PIDS_MAX ; + goto set_limit; + } + + err =kstrtoll (buf,0 , &limit); +if (err) +return err; + +/* We use INT_MAX as the maximum value of pid_t. */ +if (limit <0 || limit >INT_MAX )
This is kinda weird if we're using PIDS_MAX for max as it may end up showing "max" after some larger number is written to the file.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Hi Tejun,
Quoted 8 lines
kernel/cgroup_pids.c line 220
Quoted 7 lines
kernel/cgroup_pids.c new file +7
-0+rcu_read_lock (); + css =task_css (current, pids_cgrp_id); +if (!css_tryget_online (css)) { + retval = -EBUSY ; + goto err_rcu_unlock; + } +rcu_read_unlock ();
Quoted 8 lines
Hmmm... so, the above is guaranteed to succeed in finite amount of time (the race window is actually very narrow) and it'd be silly to fail fork because a task was being moved across cgroups.
I think it'd be a good idea to implement
task_get_css()which loops and returns the current css for the requested subsystem with reference count bumped and it can usecss_tryget()too. Holding a ref doesn't prevent css from dying anyway, so it doesn't make any difference.
Hmmm, okay. I'll work on this later.
Quoted 6 lines
kernel/cgroup_pids.c line 260
Quoted 3 lines
kernel/cgroup_pids.c new file +3
-0+rcu_read_lock (); + css =task_css (task, pids_cgrp_id); +css_get (css);Why is this safe? What guarantees that css's ref isn't already zero at this point?
Because it's already been exposed by pids_fork, so the current css_set (which contains the current css)'s ref has been bumped. There isn't a guarantee that there is a ref to css, but there is a guarantee the css_set it is in has a ref. The problem with using tryget is that we can't fail here.
Quoted 15 lines
Quoted 15 lines
kernel/cgroup_pids.c new file +15
-0+static ssize_t pids_max_write (struct kernfs_open_file *of,char *buf, +size_t nbytes,loff_t off) +{ +struct cgroup_subsys_state *css =of_css (of); +struct pids_cgroup *pids =css_pids (css); +int64_t limit; +int err; + + buf =strstrip (buf); +if (!strcmp (buf,PIDS_MAX_STR )) { + limit =PIDS_MAX ; + goto set_limit; + } + + err =kstrtoll (buf,0 , &limit);
Quoted 8 lines
kernel/cgroup_pids.c line 308
Quoted 5 lines
kernel/cgroup_pids.c new file +5
-0+if (err) +return err; + +/* We use INT_MAX as the maximum value of pid_t. */ +if (limit <0 || limit >INT_MAX )This is kinda weird if we're using
PIDS_MAXfor max as it may end up showing "max" after some larger number is written to the file.
The reason for this is because I believe you said "PIDS_MAX isn't meant to be exposed to userspace" (one of the previous patchsets used PIDS_MAX as the maximum valid value).
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Hello,
Quoted 4 lines · Aleksa Sarai
Quoted 2 lines
Why is this safe? What guarantees that css's ref isn't already zero at this point?
Because it's already been exposed by
pids_fork, so the currentcss_set
But what prevents against the task being migrated to a different cgroup?
Quoted 4 lines
(which contains the current css)'s ref has been bumped. There isn't a guarantee that there is a ref to css, but there is a guarantee the
css_setit is in has a ref. The problem with using tryget is that we can't fail here.
The guarantee you have there is the css_set wouldn't go away until rcu lock is dropped and you can deref csses from it. The way it's currently implemented, you're guaranteed to have references to the csses but that's sort of implementation detail. It can be implemented in different ways.
A task, as long as it's alive, is guaranteed to have a css associated with it all the time. What the tryget protects is races against the task being migrated to a different cgroup, so retrying until success is guaranteed to finish in a short amount of time.
Quoted 9 lines
Quoted 5 lines
kernel/cgroup_pids.c line 308
Quoted 2 lines
kernel/cgroup_pids.c new file +2
-0+/* We use INT_MAX as the maximum value of pid_t. */ +if (limit <0 || limit >INT_MAX )This is kinda weird if we're using
PIDS_MAXfor max as it may end up showing "max" after some larger number is written to the file.The reason for this is because I believe you said "
PIDS_MAXisn't meant to be exposed to userspace" (one of the previous patchsets usedPIDS_MAXas the maximum valid value).
Yeah, but wouldn't it be weird to allow the userland to input PIDS_MAX (whatever value that may be) and reads back max? It can be whatever maximum input value + 1, no?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Hi Tejun
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
kernel/cgroup_pids.c line 308
Quoted 2 lines
kernel/cgroup_pids.c new file +2
-0+/* We use INT_MAX as the maximum value of pid_t. */ +if (limit <0 || limit >INT_MAX )This is kinda weird if we're using
PIDS_MAXfor max as it may end up showing "max" after some larger number is written to the file.
Quoted 7 lines
Quoted 3 lines
The reason for this is because I believe you said "
PIDS_MAXisn't meant to be exposed to userspace" (one of the previous patchsets usedPIDS_MAXas the maximum valid value).Yeah, but wouldn't it be weird to allow the userland to input
PIDS_MAX(whatever value that may be) and reads back max? It can be whatever maximum input value + 1, no?
Would you be okay with this?
if (limit < 0 || limit >= PIDS_MAX)
I'd prefer if we used PIDS_MAX as the maximum input value as well as being the internal representation of the maximum, rather than switching to something like INT_MAX.
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Hello,
Quoted 7 lines · Aleksa Sarai
Would you be okay with this?
if (limit < 0 || limit >= PIDS_MAX)I'd prefer if we used
PIDS_MAXas the maximum input value as well as being the internal representation of the maximum, rather than switching to something likeINT_MAX.
Yeah, that sounds okay to me but I forgot why we went for INT_MAX in the first place. Do you remember why we tried INT_MAX at all?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Quoted 8 lines
Quoted 7 lines
Would you be okay with this?
if (limit < 0 || limit >= PIDS_MAX)I'd prefer if we used
PIDS_MAXas the maximum input value as well as being the internal representation of the maximum, rather than switching to something likeINT_MAX.
Quoted 7 lines
Yeah, that sounds okay to me but I forgot why we went for
INT_MAXin the first place. Do you remember why we triedINT_MAXat all?Thanks.
--
tejun
I think it's because we didn't want to expose PIDS_MAX to userspace. But we're not *really* exposing it, we're just enforcing the input limit for "max".
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Quoted 3 lines · Aleksa Sarai
I think it's because we didn't want to expose
PIDS_MAXto userspace. But we're not *really* exposing it, we're just enforcing the input limit for "max".
Ah, PIDS_MAX is fine then.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Hi Tejun,
One question RE: defaults for .config. What is the kernel policy for deciding if a particular subsystem should be made enabled-by-default?
--
Aleksa Sarai (cyphar)
www.cyphar.com
Tejun Heo
maintainerRe: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
Quoted 2 lines · Aleksa Sarai
One question RE: defaults for .config. What is the kernel policy for deciding if a particular subsystem should be made enabled-by-default?
Just default to N.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v10 4/4] cgroups: implement the PIDs subsystem
In reply to Tejun HeoAlso,
Quoted 8 lines
kernel/cgroup_pids.c line 45
Quoted 6 lines
kernel/cgroup_pids.c new file +6
-0+struct pids_cgroup { +struct cgroup_subsys_state css; + +/* +* Use 64-bit types so that we can safely represent "max" as +* (PID_MAX_LIMIT + 1). ^^^^^^^^^^^^^^^^^...
Quoted 11 lines
kernel/cgroup_pids.c line 70
Quoted 10 lines
kernel/cgroup_pids.c new file +10
-0+static struct cgroup_subsys_state * +pids_css_alloc (struct cgroup_subsys_state *parent) +{ +struct pids_cgroup *pids; + + pids =kzalloc (sizeof (struct pids_cgroup ),GFP_KERNEL ); +if (!pids) +return ERR_PTR (-ENOMEM ); + + pids->limit =PIDS_MAX ;^^^^^^^^^
%PIDS_MAX = (%PID_MAX_LIMIT + 1). I can clarify this in the comments if you want.
--
Aleksa Sarai (cyphar)
www.cyphar.com