sched_ext/for-6.11: Warning [-Wcalloc-transposed-args] during build

Vishal Chourasia posted 1 patch 1 year, 5 months ago
sched_ext/for-6.11: Warning [-Wcalloc-transposed-args] during build
Posted by Vishal Chourasia 1 year, 5 months ago
I encountered a warning while building the sched-ext for the for-6.11
branch. Below are the details:

# make -j40 -s vmlinux
In file included from ./include/linux/rhashtable-types.h:12,
                 from ./include/linux/sched/ext.h:15,
                 from ./include/linux/sched.h:83,
                 from ./include/linux/sched/signal.h:7,
                 from ./include/linux/sched/cputime.h:5,
                 from kernel/sched/build_policy.c:17:
kernel/sched/ext.c: In function ‘alloc_exit_info’:
kernel/sched/ext.c:3815:32: warning: ‘kmalloc_array_noprof’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
 3815 |         ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
      |                                ^
./include/linux/alloc_tag.h:206:16: note: in definition of macro ‘alloc_hooks_tag’
  206 |         typeof(_do_alloc) _res = _do_alloc;                             \
      |                ^~~~~~~~~
./include/linux/slab.h:701:49: note: in expansion of macro ‘alloc_hooks’
  701 | #define kmalloc_array(...)                      alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
      |                                                 ^~~~~~~~~~~
./include/linux/slab.h:730:41: note: in expansion of macro ‘kmalloc_array’
  730 | #define kcalloc(n, size, flags)         kmalloc_array(n, size, (flags) | __GFP_ZERO)
      |                                         ^~~~~~~~~~~~~
kernel/sched/ext.c:3815:18: note: in expansion of macro ‘kcalloc’
 3815 |         ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
      |                  ^~~~~~~
kernel/sched/ext.c:3815:32: note: earlier argument should specify number of elements, later size of each element
 3815 |         ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
      |                                ^
./include/linux/alloc_tag.h:206:16: note: in definition of macro ‘alloc_hooks_tag’
  206 |         typeof(_do_alloc) _res = _do_alloc;                             \
      |                ^~~~~~~~~
./include/linux/slab.h:701:49: note: in expansion of macro ‘alloc_hooks’
  701 | #define kmalloc_array(...)                      alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
      |                                                 ^~~~~~~~~~~
./include/linux/slab.h:730:41: note: in expansion of macro ‘kmalloc_array’
  730 | #define kcalloc(n, size, flags)         kmalloc_array(n, size, (flags) | __GFP_ZERO)
      |                                         ^~~~~~~~~~~~~
kernel/sched/ext.c:3815:18: note: in expansion of macro ‘kcalloc’
 3815 |         ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
      |                  ^~~~~~~

# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/ppc64le-redhat-linux/14/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: ppc64le-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release
--enable-targets=powerpcle-linux --disable-multilib --with-system-zlib
--enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array
--without-isl --enable-offload-targets=nvptx-none
--enable-offload-defaulted --without-cuda-driver
--enable-gnu-indirect-function --enable-secureplt --with-long-double-128
--with-long-double-format=ieee --with-cpu-32=power9
--with-tune-32=power9 --with-cpu-64=power9 --with-tune-64=power9
--build=ppc64le-redhat-linux --with-build-config=bootstrap-lto
--enable-link-serialization=1 --enable-host-pie --enable-host-bind-now
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.1 20240507 (Red Hat 14.1.1-2) (GCC)


If this is intentional, (I am not sure why)

// consider changing to 

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index f6e25545bfc0..9050f8051918 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3812,7 +3812,7 @@ static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
        if (!ei)
                return NULL;

-       ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
+       ei->bt = kcalloc(sizeof(ei->bt[0]) * SCX_EXIT_BT_LEN, 1, GFP_KERNEL);
        ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
        ei->dump = kzalloc(exit_dump_len, GFP_KERNEL);

else 

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index f6e25545bfc0..ae9ec8f542f2 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3812,7 +3812,7 @@ static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
        if (!ei)
                return NULL;

-       ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
+       ei->bt = kcalloc(SCX_EXIT_BT_LEN, sizeof(ei->bt[0]), GFP_KERNEL);
        ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
        ei->dump = kzalloc(exit_dump_len, GFP_KERNEL);

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wcalloc-transposed-args
[PATCH sched_ext/for-6.11] sched_ext: Swap argument positions in kcalloc() call to avoid compiler warning
Posted by Tejun Heo 1 year, 5 months ago
From b651d7c39289850b5a0a2c67231dd36117340a2e Mon Sep 17 00:00:00 2001
From: Tejun Heo <tj@kernel.org>
Date: Mon, 1 Jul 2024 08:30:02 -1000
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

alloc_exit_info() calls kcalloc() but puts in the size of the element as the
first argument which triggers the following gcc warning:

  kernel/sched/ext.c:3815:32: warning: ‘kmalloc_array_noprof’ sizes
  specified with ‘sizeof’ in the earlier argument and not in the later
  argument [-Wcalloc-transposed-args]

Fix it by swapping the positions of the first two arguments. No functional
changes.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Vishal Chourasia <vishalc@linux.ibm.com>
Link: http://lkml.kernel.org/r/ZoG6zreEtQhAUr_2@linux.ibm.com
---
Applied to sched_ext/for-6.11.

Thanks.

 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index f6e25545bfc0..ae9ec8f542f2 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3812,7 +3812,7 @@ static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
 	if (!ei)
 		return NULL;
 
-	ei->bt = kcalloc(sizeof(ei->bt[0]), SCX_EXIT_BT_LEN, GFP_KERNEL);
+	ei->bt = kcalloc(SCX_EXIT_BT_LEN, sizeof(ei->bt[0]), GFP_KERNEL);
 	ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
 	ei->dump = kzalloc(exit_dump_len, GFP_KERNEL);
 
-- 
2.45.2