Skip to content

Commit 5b30afc

Browse files
committed
cgroup: Expose some cgroup helpers
Expose the following through cgroup.h: - cgroup_on_dfl() - cgroup_is_dead() - cgroup_for_each_live_child() - cgroup_for_each_live_descendant_pre() - cgroup_for_each_live_descendant_post() Until now, these didn't need to be exposed because controllers only cared about the css hierarchy. The planned sched_ext hierarchical scheduler support will be based on the default cgroup hierarchy, which is in line with the existing BPF cgroup support, and thus needs these exposed. Signed-off-by: Tejun Heo <[email protected]>
1 parent 5ee8dbf commit 5b30afc

3 files changed

Lines changed: 63 additions & 63 deletions

File tree

include/linux/cgroup.h

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ struct kernel_clone_args;
4242

4343
#ifdef CONFIG_CGROUPS
4444

45+
/*
46+
* To avoid confusing the compiler (and generating warnings) with code
47+
* that attempts to access what would be a 0-element array (i.e. sized
48+
* to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
49+
* constant expression can be added.
50+
*/
51+
#define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
52+
4553
enum css_task_iter_flags {
4654
CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */
4755
CSS_TASK_ITER_THREADED = (1U << 1), /* walk all threaded css_sets in the domain */
@@ -76,6 +84,7 @@ enum cgroup_lifetime_events {
7684
extern struct file_system_type cgroup_fs_type;
7785
extern struct cgroup_root cgrp_dfl_root;
7886
extern struct css_set init_css_set;
87+
extern struct mutex cgroup_mutex;
7988
extern spinlock_t css_set_lock;
8089
extern struct blocking_notifier_head cgroup_lifetime_notifier;
8190

@@ -103,6 +112,8 @@ extern struct blocking_notifier_head cgroup_lifetime_notifier;
103112
#define cgroup_subsys_on_dfl(ss) \
104113
static_branch_likely(&ss ## _on_dfl_key)
105114

115+
bool cgroup_on_dfl(const struct cgroup *cgrp);
116+
106117
bool css_has_online_children(struct cgroup_subsys_state *css);
107118
struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
108119
struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
@@ -274,6 +285,32 @@ void css_task_iter_end(struct css_task_iter *it);
274285
for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
275286
(pos) = css_next_descendant_post((pos), (css)))
276287

288+
/* iterate over child cgrps, lock should be held throughout iteration */
289+
#define cgroup_for_each_live_child(child, cgrp) \
290+
list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
291+
if (({ lockdep_assert_held(&cgroup_mutex); \
292+
cgroup_is_dead(child); })) \
293+
; \
294+
else
295+
296+
/* walk live descendants in pre order */
297+
#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
298+
css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
299+
if (({ lockdep_assert_held(&cgroup_mutex); \
300+
(dsct) = (d_css)->cgroup; \
301+
cgroup_is_dead(dsct); })) \
302+
; \
303+
else
304+
305+
/* walk live descendants in postorder */
306+
#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
307+
css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
308+
if (({ lockdep_assert_held(&cgroup_mutex); \
309+
(dsct) = (d_css)->cgroup; \
310+
cgroup_is_dead(dsct); })) \
311+
; \
312+
else
313+
277314
/**
278315
* cgroup_taskset_for_each - iterate cgroup_taskset
279316
* @task: the loop cursor
@@ -336,6 +373,27 @@ static inline u64 cgroup_id(const struct cgroup *cgrp)
336373
return cgrp->kn->id;
337374
}
338375

376+
/**
377+
* cgroup_css - obtain a cgroup's css for the specified subsystem
378+
* @cgrp: the cgroup of interest
379+
* @ss: the subsystem of interest (%NULL returns @cgrp->self)
380+
*
381+
* Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
382+
* function must be called either under cgroup_mutex or rcu_read_lock() and
383+
* the caller is responsible for pinning the returned css if it wants to
384+
* keep accessing it outside the said locks. This function may return
385+
* %NULL if @cgrp doesn't have @subsys_id enabled.
386+
*/
387+
static inline struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
388+
struct cgroup_subsys *ss)
389+
{
390+
if (CGROUP_HAS_SUBSYS_CONFIG && ss)
391+
return rcu_dereference_check(cgrp->subsys[ss->id],
392+
lockdep_is_held(&cgroup_mutex));
393+
else
394+
return &cgrp->self;
395+
}
396+
339397
/**
340398
* css_is_dying - test whether the specified css is dying
341399
* @css: target css
@@ -372,6 +430,11 @@ static inline bool css_is_self(struct cgroup_subsys_state *css)
372430
return false;
373431
}
374432

433+
static inline bool cgroup_is_dead(const struct cgroup *cgrp)
434+
{
435+
return !(cgrp->self.flags & CSS_ONLINE);
436+
}
437+
375438
static inline void cgroup_get(struct cgroup *cgrp)
376439
{
377440
css_get(&cgrp->self);
@@ -387,8 +450,6 @@ static inline void cgroup_put(struct cgroup *cgrp)
387450
css_put(&cgrp->self);
388451
}
389452

390-
extern struct mutex cgroup_mutex;
391-
392453
static inline void cgroup_lock(void)
393454
{
394455
mutex_lock(&cgroup_mutex);

kernel/cgroup/cgroup-internal.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ extern bool cgrp_dfl_visible;
184184
for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
185185
(((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
186186

187-
static inline bool cgroup_is_dead(const struct cgroup *cgrp)
188-
{
189-
return !(cgrp->self.flags & CSS_ONLINE);
190-
}
191-
192187
static inline bool notify_on_release(const struct cgroup *cgrp)
193188
{
194189
return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
@@ -222,7 +217,6 @@ static inline void get_css_set(struct css_set *cset)
222217
}
223218

224219
bool cgroup_ssid_enabled(int ssid);
225-
bool cgroup_on_dfl(const struct cgroup *cgrp);
226220

227221
struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root);
228222
struct cgroup *task_cgroup_from_root(struct task_struct *task,

kernel/cgroup/cgroup.c

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@
6868
/* let's not notify more than 100 times per second */
6969
#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
7070

71-
/*
72-
* To avoid confusing the compiler (and generating warnings) with code
73-
* that attempts to access what would be a 0-element array (i.e. sized
74-
* to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
75-
* constant expression can be added.
76-
*/
77-
#define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
78-
7971
/*
8072
* cgroup_mutex is the master lock. Any modification to cgroup or its
8173
* hierarchy must be performed while holding it.
@@ -509,27 +501,6 @@ static u32 cgroup_ss_mask(struct cgroup *cgrp)
509501
return cgrp->root->subsys_mask;
510502
}
511503

512-
/**
513-
* cgroup_css - obtain a cgroup's css for the specified subsystem
514-
* @cgrp: the cgroup of interest
515-
* @ss: the subsystem of interest (%NULL returns @cgrp->self)
516-
*
517-
* Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
518-
* function must be called either under cgroup_mutex or rcu_read_lock() and
519-
* the caller is responsible for pinning the returned css if it wants to
520-
* keep accessing it outside the said locks. This function may return
521-
* %NULL if @cgrp doesn't have @subsys_id enabled.
522-
*/
523-
static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
524-
struct cgroup_subsys *ss)
525-
{
526-
if (CGROUP_HAS_SUBSYS_CONFIG && ss)
527-
return rcu_dereference_check(cgrp->subsys[ss->id],
528-
lockdep_is_held(&cgroup_mutex));
529-
else
530-
return &cgrp->self;
531-
}
532-
533504
/**
534505
* cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
535506
* @cgrp: the cgroup of interest
@@ -741,32 +712,6 @@ EXPORT_SYMBOL_GPL(of_css);
741712
} \
742713
} while (false)
743714

744-
/* iterate over child cgrps, lock should be held throughout iteration */
745-
#define cgroup_for_each_live_child(child, cgrp) \
746-
list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
747-
if (({ lockdep_assert_held(&cgroup_mutex); \
748-
cgroup_is_dead(child); })) \
749-
; \
750-
else
751-
752-
/* walk live descendants in pre order */
753-
#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
754-
css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
755-
if (({ lockdep_assert_held(&cgroup_mutex); \
756-
(dsct) = (d_css)->cgroup; \
757-
cgroup_is_dead(dsct); })) \
758-
; \
759-
else
760-
761-
/* walk live descendants in postorder */
762-
#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
763-
css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
764-
if (({ lockdep_assert_held(&cgroup_mutex); \
765-
(dsct) = (d_css)->cgroup; \
766-
cgroup_is_dead(dsct); })) \
767-
; \
768-
else
769-
770715
/*
771716
* The default css_set - used by init and its children prior to any
772717
* hierarchies being mounted. It contains a pointer to the root state

0 commit comments

Comments
 (0)