Skip to content

Commit d20d30e

Browse files
keeshtejun
authored andcommitted
cgroup: Avoid compiler warnings with no subsystems
As done before in commit cb4a316 ("cgroup: use bitmask to filter for_each_subsys"), avoid compiler warnings for the pathological case of having no subsystems (i.e. CGROUP_SUBSYS_COUNT == 0). This condition is hit for the arm multi_v7_defconfig config under -Wzero-length-bounds: In file included from ./arch/arm/include/generated/asm/rwonce.h:1, from include/linux/compiler.h:264, from include/uapi/linux/swab.h:6, from include/linux/swab.h:5, from arch/arm/include/asm/opcodes.h:86, from arch/arm/include/asm/bug.h:7, from include/linux/bug.h:5, from include/linux/thread_info.h:13, from include/asm-generic/current.h:5, from ./arch/arm/include/generated/asm/current.h:1, from include/linux/sched.h:12, from include/linux/cgroup.h:12, from kernel/cgroup/cgroup-internal.h:5, from kernel/cgroup/cgroup.c:31: kernel/cgroup/cgroup.c: In function 'of_css': kernel/cgroup/cgroup.c:651:42: warning: array subscript '<unknown>' is outside the bounds of an interior zero-length array 'struct cgroup_subsys_state *[0]' [-Wzero-length-bounds] 651 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); Reported-by: Stephen Rothwell <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Zefan Li <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 9f72daf commit d20d30e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

kernel/cgroup/cgroup.c

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

70+
/*
71+
* To avoid confusing the compiler (and generating warnings) with code
72+
* that attempts to access what would be a 0-element array (i.e. sized
73+
* to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
74+
* constant expression can be added.
75+
*/
76+
#define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
77+
7078
/*
7179
* cgroup_mutex is the master lock. Any modification to cgroup or its
7280
* hierarchy must be performed while holding it.
@@ -248,7 +256,7 @@ static int cgroup_addrm_files(struct cgroup_subsys_state *css,
248256
*/
249257
bool cgroup_ssid_enabled(int ssid)
250258
{
251-
if (CGROUP_SUBSYS_COUNT == 0)
259+
if (!CGROUP_HAS_SUBSYS_CONFIG)
252260
return false;
253261

254262
return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
@@ -472,7 +480,7 @@ static u16 cgroup_ss_mask(struct cgroup *cgrp)
472480
static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
473481
struct cgroup_subsys *ss)
474482
{
475-
if (ss)
483+
if (CGROUP_HAS_SUBSYS_CONFIG && ss)
476484
return rcu_dereference_check(cgrp->subsys[ss->id],
477485
lockdep_is_held(&cgroup_mutex));
478486
else
@@ -550,6 +558,9 @@ struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
550558
{
551559
struct cgroup_subsys_state *css;
552560

561+
if (!CGROUP_HAS_SUBSYS_CONFIG)
562+
return NULL;
563+
553564
do {
554565
css = cgroup_css(cgrp, ss);
555566

@@ -577,6 +588,9 @@ struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
577588
{
578589
struct cgroup_subsys_state *css;
579590

591+
if (!CGROUP_HAS_SUBSYS_CONFIG)
592+
return NULL;
593+
580594
rcu_read_lock();
581595

582596
do {
@@ -647,7 +661,7 @@ struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
647661
* the matching css from the cgroup's subsys table is guaranteed to
648662
* be and stay valid until the enclosing operation is complete.
649663
*/
650-
if (cft->ss)
664+
if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss)
651665
return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
652666
else
653667
return &cgrp->self;
@@ -695,7 +709,7 @@ EXPORT_SYMBOL_GPL(of_css);
695709
*/
696710
#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
697711
unsigned long __ss_mask = (ss_mask); \
698-
if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
712+
if (!CGROUP_HAS_SUBSYS_CONFIG) { \
699713
(ssid) = 0; \
700714
break; \
701715
} \
@@ -2372,7 +2386,7 @@ struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
23722386
struct css_set *cset = tset->cur_cset;
23732387
struct task_struct *task = tset->cur_task;
23742388

2375-
while (&cset->mg_node != tset->csets) {
2389+
while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) {
23762390
if (!task)
23772391
task = list_first_entry(&cset->mg_tasks,
23782392
struct task_struct, cg_list);
@@ -4643,7 +4657,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
46434657
it->ss = css->ss;
46444658
it->flags = flags;
46454659

4646-
if (it->ss)
4660+
if (CGROUP_HAS_SUBSYS_CONFIG && it->ss)
46474661
it->cset_pos = &css->cgroup->e_csets[css->ss->id];
46484662
else
46494663
it->cset_pos = &css->cgroup->cset_links;

0 commit comments

Comments
 (0)