linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cgroup: Avoid compiler warnings with no subsystems
@ 2021-08-27  3:47 Kees Cook
  2021-08-27  7:21 ` kernel test robot
  0 siblings, 1 reply; 2+ messages in thread
From: Kees Cook @ 2021-08-27  3:47 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Kees Cook, Stephen Rothwell, Zefan Li, Johannes Weiner, cgroups,
	linux-kernel, linux-hardening

As done before in commit cb4a31675270 ("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 <sfr@canb.auug.org.au>
Cc: Tejun Heo <tj@kernel.org>
Cc: Zefan Li <lizefan.x@bytedance.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: cgroups@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 kernel/cgroup/cgroup.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index d0725c1a8db5..d23100878002 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -472,7 +472,7 @@ static u16 cgroup_ss_mask(struct cgroup *cgrp)
 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
 					      struct cgroup_subsys *ss)
 {
-	if (ss)
+	if (CGROUP_SUBSYS_COUNT && ss)
 		return rcu_dereference_check(cgrp->subsys[ss->id],
 					lockdep_is_held(&cgroup_mutex));
 	else
@@ -550,6 +550,9 @@ struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
 {
 	struct cgroup_subsys_state *css;
 
+	if (!CGROUP_SUBSYS_COUNT)
+		return NULL;
+
 	do {
 		css = cgroup_css(cgrp, ss);
 
@@ -577,6 +580,9 @@ struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
 {
 	struct cgroup_subsys_state *css;
 
+	if (!CGROUP_SUBSYS_COUNT)
+		return NULL;
+
 	rcu_read_lock();
 
 	do {
@@ -647,7 +653,7 @@ struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
 	 * the matching css from the cgroup's subsys table is guaranteed to
 	 * be and stay valid until the enclosing operation is complete.
 	 */
-	if (cft->ss)
+	if (CGROUP_SUBSYS_COUNT && cft->ss)
 		return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
 	else
 		return &cgrp->self;
@@ -2372,7 +2378,7 @@ struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
 	struct css_set *cset = tset->cur_cset;
 	struct task_struct *task = tset->cur_task;
 
-	while (&cset->mg_node != tset->csets) {
+	while (CGROUP_SUBSYS_COUNT && &cset->mg_node != tset->csets) {
 		if (!task)
 			task = list_first_entry(&cset->mg_tasks,
 						struct task_struct, cg_list);
@@ -4643,7 +4649,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
 	it->ss = css->ss;
 	it->flags = flags;
 
-	if (it->ss)
+	if (CGROUP_SUBSYS_COUNT && it->ss)
 		it->cset_pos = &css->cgroup->e_csets[css->ss->id];
 	else
 		it->cset_pos = &css->cgroup->cset_links;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] cgroup: Avoid compiler warnings with no subsystems
  2021-08-27  3:47 [PATCH] cgroup: Avoid compiler warnings with no subsystems Kees Cook
@ 2021-08-27  7:21 ` kernel test robot
  0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2021-08-27  7:21 UTC (permalink / raw)
  To: Kees Cook, Tejun Heo
  Cc: llvm, kbuild-all, Kees Cook, Zefan Li, Johannes Weiner, cgroups,
	linux-kernel, linux-hardening

[-- Attachment #1: Type: text/plain, Size: 3566 bytes --]

Hi Kees,

I love your patch! Perhaps something to improve:

[auto build test WARNING on cgroup/for-next]
[also build test WARNING on kees/for-next/pstore v5.14-rc7 next-20210826]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Kees-Cook/cgroup-Avoid-compiler-warnings-with-no-subsystems/20210827-115012
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next
config: arm64-buildonly-randconfig-r005-20210827 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 1076082a0d97bd5c16a25ee7cf3dbb6ee4b5a9fe)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/0day-ci/linux/commit/3e93974a5c6d03c5444c3acbaf67061d0897fb96
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Kees-Cook/cgroup-Avoid-compiler-warnings-with-no-subsystems/20210827-115012
        git checkout 3e93974a5c6d03c5444c3acbaf67061d0897fb96
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> kernel/cgroup/cgroup.c:475:26: warning: converting the enum constant to a boolean [-Wint-in-bool-context]
           if (CGROUP_SUBSYS_COUNT && ss)
                                   ^
   kernel/cgroup/cgroup.c:656:26: warning: converting the enum constant to a boolean [-Wint-in-bool-context]
           if (CGROUP_SUBSYS_COUNT && cft->ss)
                                   ^
   kernel/cgroup/cgroup.c:2381:29: warning: converting the enum constant to a boolean [-Wint-in-bool-context]
           while (CGROUP_SUBSYS_COUNT && &cset->mg_node != tset->csets) {
                                      ^
   kernel/cgroup/cgroup.c:4652:26: warning: converting the enum constant to a boolean [-Wint-in-bool-context]
           if (CGROUP_SUBSYS_COUNT && it->ss)
                                   ^
   4 warnings generated.


vim +475 kernel/cgroup/cgroup.c

   460	
   461	/**
   462	 * cgroup_css - obtain a cgroup's css for the specified subsystem
   463	 * @cgrp: the cgroup of interest
   464	 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
   465	 *
   466	 * Return @cgrp's css (cgroup_subsys_state) associated with @ss.  This
   467	 * function must be called either under cgroup_mutex or rcu_read_lock() and
   468	 * the caller is responsible for pinning the returned css if it wants to
   469	 * keep accessing it outside the said locks.  This function may return
   470	 * %NULL if @cgrp doesn't have @subsys_id enabled.
   471	 */
   472	static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
   473						      struct cgroup_subsys *ss)
   474	{
 > 475		if (CGROUP_SUBSYS_COUNT && ss)
   476			return rcu_dereference_check(cgrp->subsys[ss->id],
   477						lockdep_is_held(&cgroup_mutex));
   478		else
   479			return &cgrp->self;
   480	}
   481	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 44884 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-27  7:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-27  3:47 [PATCH] cgroup: Avoid compiler warnings with no subsystems Kees Cook
2021-08-27  7:21 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).