net/switchdev: use struct_size over open coded arithmetic
Subsystems (3)
PATCH 1/1 net/switchdev: use struct_size over open coded arithmetic
From: "Minghao Chi (CGEL ZTE)" <[email protected]>
Replace zero-length array with flexible-array member and make use of the struct_size() helper in kmalloc(). For example:
struct switchdev_deferred_item {
...
unsigned long data[];
};Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.
--- net/switchdev/switchdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/switchdev/switchdev.c +1 -1
@@ -85,7 +85,7 @@ static int switchdev_deferred_enqueue(struct net_device *dev, {struct switchdev_deferred_item *dfitem; - dfitem =kmalloc (sizeof (*dfitem) + data_len,GFP_ATOMIC ); + dfitem =kmalloc (struct_size (*dfitem, data, data_len),GFP_ATOMIC );if (!dfitem)return -ENOMEM ; dfitem->dev = dev;
Signature · 1 line
2.25.1
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.17-rc1 next-20220128]
[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/cgel-zte-gmail-com/net-switchdev-use-struct_size-over-open-coded-arithmetic/20220128-155848 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 23a46422c56144939c091c76cf389aa863ce9c18 config: arm-orion5x_defconfig (https://download.01.org/0day-ci/archive/20220128/202201282035.8dNEcULs-lkp(a)intel.com/config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 33b45ee44b1f32ffdbc995e6fec806271b4b3ba4)
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 arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/bf0e33a8c3deb700b95173a37dd16754341ba70e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review cgel-zte-gmail-com/net-switchdev-use-struct_size-over-open-coded-arithmetic/20220128-155848 git checkout bf0e33a8c3deb700b95173a37dd16754341ba70e
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> net/switchdev/switchdev.c:88:19: error: member reference type 'struct switchdev_deferred_item' is not a pointer; did you mean to use '.'?
dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/overflow.h:194:18: note: expanded from macro 'struct_size'
sizeof(*(p)->member) + __must_be_array((p)->member),\
~~~^
>> net/switchdev/switchdev.c:88:19: error: indirection requires pointer operand ('unsigned long[]' invalid)
dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/overflow.h:194:14: note: expanded from macro 'struct_size'
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~
>> net/switchdev/switchdev.c:88:19: error: member reference type 'struct switchdev_deferred_item' is not a pointer; did you mean to use '.'?
dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/overflow.h:194:49: note: expanded from macro 'struct_size'
sizeof(*(p)->member) + __must_be_array((p)->member),\
~~~~~~~~~~~~~~~~~~~^~~~~~~~~
include/linux/compiler.h:258:59: note: expanded from macro '__must_be_array'
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
include/linux/compiler_types.h:287:63: note: expanded from macro '__same_type'
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
^
include/linux/build_bug.h:16:62: note: expanded from macro 'BUILD_BUG_ON_ZERO'
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
>> net/switchdev/switchdev.c:88:19: error: member reference type 'struct switchdev_deferred_item' is not a pointer; did you mean to use '.'?
dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/overflow.h:194:49: note: expanded from macro 'struct_size'
sizeof(*(p)->member) + __must_be_array((p)->member),\
~~~~~~~~~~~~~~~~~~~^~~~~~~~~
include/linux/compiler.h:258:65: note: expanded from macro '__must_be_array'
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
include/linux/compiler_types.h:287:74: note: expanded from macro '__same_type'
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
^
include/linux/build_bug.h:16:62: note: expanded from macro 'BUILD_BUG_ON_ZERO'
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
>> net/switchdev/switchdev.c:88:19: error: indirection requires pointer operand ('struct switchdev_deferred_item' invalid)
dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/overflow.h:195:14: note: expanded from macro 'struct_size'
sizeof(*(p)))
^~~~5 errors generated.
vim +88 net/switchdev/switchdev.c
81
82 static int switchdev_deferred_enqueue(struct net_device *dev,
83 const void *data, size_t data_len,
84 switchdev_deferred_func_t *func)
85 {
86 struct switchdev_deferred_item *dfitem;
87
> 88 dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
89 if (!dfitem)
90 return -ENOMEM;
91 dfitem->dev = dev;
92 dfitem->func = func;
93 memcpy(dfitem->data, data, data_len);
94 dev_hold_track(dev, &dfitem->dev_tracker, GFP_ATOMIC);
95 spin_lock_bh(&deferred_lock);
96 list_add_tail(&dfitem->list, &deferred);
97 spin_unlock_bh(&deferred_lock);
98 schedule_work(&deferred_process_work);
99 return 0; 100 }
101
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.17-rc1 next-20220128]
[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/cgel-zte-gmail-com/net-switchdev-use-struct_size-over-open-coded-arithmetic/20220128-155848 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 23a46422c56144939c091c76cf389aa863ce9c18 config: i386-randconfig-a005 (https://download.01.org/0day-ci/archive/20220129/202201290005.GR1IxnQb-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/bf0e33a8c3deb700b95173a37dd16754341ba70e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review cgel-zte-gmail-com/net-switchdev-use-struct_size-over-open-coded-arithmetic/20220128-155848 git checkout bf0e33a8c3deb700b95173a37dd16754341ba70e
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash net/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/mm.h:30,
from arch/x86/include/asm/cacheflush.h:5, from include/linux/cacheflush.h:5,
from include/linux/highmem.h:8,
from include/linux/bvec.h:10,
from include/linux/skbuff.h:17,
from include/net/net_namespace.h:40, from include/linux/netdevice.h:37,
from net/switchdev/switchdev.c:13:
net/switchdev/switchdev.c: In function 'switchdev_deferred_enqueue':
>> include/linux/overflow.h:194:18: error: invalid type argument of '->' (have 'struct switchdev_deferred_item')
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~
net/switchdev/switchdev.c:88:19: note: in expansion of macro 'struct_size'
88 | dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
| ^~~~~~~~~~~
In file included from include/linux/container_of.h:5,
from include/linux/kernel.h:21,
from net/switchdev/switchdev.c:8:
include/linux/overflow.h:194:49: error: invalid type argument of '->' (have 'struct switchdev_deferred_item')
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~
include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
| ^
include/linux/compiler.h:258:46: note: in expansion of macro '__same_type'
258 | #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| ^~~~~~~~~~~
include/linux/overflow.h:194:30: note: in expansion of macro '__must_be_array'
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~~~~~~~~~~~~~~
net/switchdev/switchdev.c:88:19: note: in expansion of macro 'struct_size'
88 | dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
| ^~~~~~~~~~~
include/linux/overflow.h:194:49: error: invalid type argument of '->' (have 'struct switchdev_deferred_item')
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~
include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
| ^
include/linux/compiler.h:258:46: note: in expansion of macro '__same_type'
258 | #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| ^~~~~~~~~~~
include/linux/overflow.h:194:30: note: in expansion of macro '__must_be_array'
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~~~~~~~~~~~~~~
net/switchdev/switchdev.c:88:19: note: in expansion of macro 'struct_size'
88 | dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
| ^~~~~~~~~~~
include/linux/build_bug.h:16:51: error: bit-field '<anonymous>' width not an integer constant
16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
| ^
include/linux/compiler.h:258:28: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
258 | #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| ^~~~~~~~~~~~~~~~~
include/linux/overflow.h:194:30: note: in expansion of macro '__must_be_array'
194 | sizeof(*(p)->member) + __must_be_array((p)->member),\
| ^~~~~~~~~~~~~~~
net/switchdev/switchdev.c:88:19: note: in expansion of macro 'struct_size'
88 | dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
| ^~~~~~~~~~~
In file included from include/linux/mm.h:30,
from arch/x86/include/asm/cacheflush.h:5,
from include/linux/cacheflush.h:5,
from include/linux/highmem.h:8,
from include/linux/bvec.h:10,
from include/linux/skbuff.h:17,
from include/net/net_namespace.h:40,
from include/linux/netdevice.h:37,
from net/switchdev/switchdev.c:13:
>> include/linux/overflow.h:195:14: error: invalid type argument of unary '*' (have 'struct switchdev_deferred_item')
195 | sizeof(*(p)))
| ^~~~
net/switchdev/switchdev.c:88:19: note: in expansion of macro 'struct_size'
88 | dfitem = kmalloc(struct_size(*dfitem, data, data_len), GFP_ATOMIC);
| ^~~~~~~~~~~vim +194 include/linux/overflow.h
610b15c50e86eb Kees Cook 2018-05-07 180 610b15c50e86eb Kees Cook 2018-05-07 181 /** 610b15c50e86eb Kees Cook 2018-05-07 182 * struct_size() - Calculate size of structure with trailing array. 610b15c50e86eb Kees Cook 2018-05-07 183 * @p: Pointer to the structure. 610b15c50e86eb Kees Cook 2018-05-07 184 * @member: Name of the array member. b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08 185 * @count: Number of elements in the array. 610b15c50e86eb Kees Cook 2018-05-07 186 * 610b15c50e86eb Kees Cook 2018-05-07 187 * Calculates size of memory needed for structure @p followed by an b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08 188 * array of @count number of @member elements. 610b15c50e86eb Kees Cook 2018-05-07 189 * 610b15c50e86eb Kees Cook 2018-05-07 190 * Return: number of bytes needed or SIZE_MAX on overflow. 610b15c50e86eb Kees Cook 2018-05-07 191 */ b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08 192 #define struct_size(p, member, count) \ b19d57d0f3cc6f Gustavo A. R. Silva 2020-06-08 193 __ab_c_size(count, \ 610b15c50e86eb Kees Cook 2018-05-07 @194 sizeof(*(p)->member) + __must_be_array((p)->member),\ 610b15c50e86eb Kees Cook 2018-05-07 @195 sizeof(*(p))) 610b15c50e86eb Kees Cook 2018-05-07 196
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 10 lines · On Fri, Jan 28, 2022 at 07:57:29AM +0000, [email protected] wrote:
From: "Minghao Chi (CGEL ZTE)" <[email protected]>
Replace zero-length array with flexible-array member and make use of the
struct_size()helper inkmalloc(). For example:struct switchdev_deferred_item { ... unsigned long data[]; };
Quoted 4 lines
Make use of the
struct_size()helper instead of an open-coded version in order to avoid any potential type mistakes.Reported-by Zeal Robot <[email protected]>
Please could you train your bot to zealously compile test patches before submitting them?
At the moment you seem to have more patches which fail to build then actually build. So you are wasting peoples time, which is not going to make Maintainers happy.
Andrew