Skip to contentSkip to conversation

Staging: nvec: Convert to_nvec_led from a macro to an inline function

PATCH In review Sumitra Sharma 5 participants 7 messages Mar 18, 2023 – Mar 20, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Staging: nvec: Convert to_nvec_led from a macro to an inline function

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

v2: Change patch subject and description, noted by Julia Lawall <[email protected]>

 drivers/staging/nvec/nvec_paz00.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/nvec/nvec_paz00.c +4 -2
index 8b4da95081c8..9573ba762cdd 100644
@@ -14,8 +14,10 @@
 #include <linux/platform_device.h>
 #include "nvec.h"
 
-#define to_nvec_led(led_cdev) \
-	container_of(led_cdev, struct nvec_led, cdev)
+static inline struct nvec_led *to_nvec_led(struct led_classdev *led_cdev)
+{
+	return container_of(led_cdev, struct nvec_led, cdev);
+}
 
 #define NVEC_LED_REQ {'\x0d', '\x10', '\x45', '\x10', '\x00'}
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Sumitra Sharma

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

You need to compile test your changes.

regards,
dan carpenter

#2 lore raw
Quoted 10 lines · Dan Carpenter
Quoted 6 lines · Sumitra Sharma

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

You need to compile test your changes.

Hi dan,

I am facing problems in creating modules and in compiling them.

Any help in this would be appreciated. Here is the link to the thread https://lore.kernel.org/outreachy/alpine.DEB.2.22.394.2303191336090.2867@hadrien/T/#t

Regards,

Sumitra

Quoted 3 lines

regards,
dan carpenter

#3 lore raw
Quoted 12 lines · Sumitra Sharma
Quoted 10 lines · Dan Carpenter
Quoted 6 lines · Sumitra Sharma

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

You need to compile test your changes.

Quoted 6 lines

Hi dan,

I am facing problems in creating modules and in compiling them.

Any help in this would be appreciated. Here is the link to the thread
https://lore.kernel.org/outreachy/alpine.DEB.2.22.394.2303191336090.2867@hadrien/T/#t

If you're editing a .h file then you need to compile all the stuff which includes it. It's slightly complicated. But if you're just editing a .c file like here then what I do is I have use my Smatch script to try compile it.

In Debian run:
apt-get install gcc make sqlite3 libsqlite3-dev libdbd-sqlite3-perl libssl-dev libtry-tiny-perl Or in Fedora run:
yum install gcc make sqlite3 sqlite-devel sqlite perl-DBD-SQLite openssl-devel perl-Try-Tiny

git clone https://github.com/error27/smatch.git
cd smatch
make

cd /path/to/kernel_src/ ~/smatch/smatch_scripts/kchecker drivers/staging/nvec/nvec_paz00.c

Also I run Sparse on the file.
~/smatch/smatch_scripts/kchecker --sparse drivers/staging/nvec/nvec_paz00.c

When I'm patching a file, then I like to look at any other issues I might want to fix and deal with that as well.

regards,
dan carpenter

#4 lore raw
In reply to Sumitra Sharma
Quoted 9 lines · Sumitra Sharma
Quoted 8 lines · Dan Carpenter
Quoted 6 lines · Sumitra Sharma

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

Quoted 6 lines
Quoted 2 lines

You need to compile test your changes.

Hi dan,

I am facing problems in creating modules and in compiling them.

Note that unless the code is under an ifdef for a module, you don't specifically need the driver to be configured as for a module either. Since you don't have the device, you aren't going to be able to test it anyway. So 'y' is fine.

julia

Quoted 12 lines

Any help in this would be appreciated. Here is the link to the thread https://lore.kernel.org/outreachy/alpine.DEB.2.22.394.2303191336090.2867@hadrien/T/#t

Regards,

Sumitra

Quoted 3 lines

regards,
dan carpenter

#5 lore raw
In reply to Sumitra Sharma
Thank you for the patch! Yet something to improve:

Hi Sumitra,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on staging/staging-testing]

url:    https://github.com/intel-lab-lkp/linux/commits/Sumitra-Sharma/Staging-nvec-Convert-to_nvec_led-from-a-macro-to-an-inline-function/20230319-015434
patch link:    https://lore.kernel.org/r/20230318175250.GA49618%40sumitra.com
patch subject: [PATCH v2] Staging: nvec: Convert to_nvec_led from a macro to an inline function
config: arm-allyesconfig (https://download.01.org/0day-ci/archive/20230320/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 12.1.0

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
# https://github.com/intel-lab-lkp/linux/commit/5bb6b930cab3ac631310d3bbf40c9decf91dfd38
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Sumitra-Sharma/Staging-nvec-Convert-to_nvec_led-from-a-macro-to-an-inline-function/20230319-015434 git checkout 5bb6b930cab3ac631310d3bbf40c9decf91dfd38
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/staging/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/container_of.h:5,

                    from include/linux/list.h:5,
                    from include/linux/module.h:12,
                    from drivers/staging/nvec/nvec_paz00.c:10:
   drivers/staging/nvec/nvec_paz00.c: In function 'to_nvec_led':
>> include/linux/container_of.h:20:54: error: invalid use of undefined type 'struct nvec_led'
      20 |         static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
         |                                                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/container_of.h:20:9: note: in expansion of macro 'static_assert'
      20 |         static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
         |         ^~~~~~~~~~~~~
   include/linux/container_of.h:20:23: note: in expansion of macro '__same_type'
      20 |         static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
         |                       ^~~~~~~~~~~
   drivers/staging/nvec/nvec_paz00.c:19:16: note: in expansion of macro 'container_of'
      19 |         return container_of(led_cdev, struct nvec_led, cdev);
         |                ^~~~~~~~~~~~
   include/linux/compiler_types.h:338:27: error: expression in static assertion is not an integer
     338 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
         |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/container_of.h:20:9: note: in expansion of macro 'static_assert'
      20 |         static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
         |         ^~~~~~~~~~~~~
   include/linux/container_of.h:20:23: note: in expansion of macro '__same_type'
      20 |         static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
         |                       ^~~~~~~~~~~
   drivers/staging/nvec/nvec_paz00.c:19:16: note: in expansion of macro 'container_of'
      19 |         return container_of(led_cdev, struct nvec_led, cdev);
         |                ^~~~~~~~~~~~
   In file included from include/uapi/linux/posix_types.h:5,
                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kasan-checks.h:5,
                    from include/asm-generic/rwonce.h:26,
                    from ./arch/arm/include/generated/asm/rwonce.h:1,
                    from include/linux/compiler.h:247,
                    from include/linux/build_bug.h:5:
>> include/linux/stddef.h:16:33: error: invalid use of undefined type 'struct nvec_led'
      16 | #define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
         |                                 ^~~~~~~~~~~~~~~~~~
   include/linux/container_of.h:23:28: note: in expansion of macro 'offsetof'
      23 |         ((type *)(__mptr - offsetof(type, member))); })
         |                            ^~~~~~~~
   drivers/staging/nvec/nvec_paz00.c:19:16: note: in expansion of macro 'container_of'
      19 |         return container_of(led_cdev, struct nvec_led, cdev);
         |                ^~~~~~~~~~~~
   drivers/staging/nvec/nvec_paz00.c:20:1: error: control reaches end of non-void function [-Werror=return-type]
      20 | }
         | ^

cc1: some warnings being treated as errors

vim +20 include/linux/container_of.h

d2a8ebbf8192b8 Andy Shevchenko  2021-11-08   9  
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  10  /**
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  11   * container_of - cast a member of a structure out to the containing structure
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  12   * @ptr:	the pointer to the member.
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  13   * @type:	the type of the container struct this is embedded in.
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  14   * @member:	the name of the member within the struct.
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  15   *
7376e561fd2e01 Sakari Ailus     2022-10-24  16   * WARNING: any const qualifier of @ptr is lost.
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  17   */
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  18  #define container_of(ptr, type, member) ({				\
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  19  	void *__mptr = (void *)(ptr);					\
e1edc277e6f6df Rasmus Villemoes 2021-11-08 @20  	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
e1edc277e6f6df Rasmus Villemoes 2021-11-08  21  		      __same_type(*(ptr), void),			\
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  22  		      "pointer type mismatch in container_of()");	\
d2a8ebbf8192b8 Andy Shevchenko  2021-11-08  23  	((type *)(__mptr - offsetof(type, member))); })

d2a8ebbf8192b8 Andy Shevchenko 2021-11-08 24

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#6 lore raw
In reply to Sumitra Sharma
Quoted 18 lines · Sumitra Sharma

Convert to_nvec_led from a macro to a static inline function, to make the relevant types apparent in the definition and to benefit from the type checking performed by the compiler at call sites.

---

v2: Change patch subject and description, noted by Julia Lawall <[email protected]>

 drivers/staging/nvec/nvec_paz00.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/nvec/nvec_paz00.c +0 -0
index 8b4da95081c8..9573ba762cdd 100644

drivers/staging/nvec/nvec_paz00.c line 20

Quoted 9 lines
drivers/staging/nvec/nvec_paz00.c +4 -2
@@ -14,8 +14,10 @@
 #include <linux/platform_device.h>
 #include "nvec.h"
 
-#define to_nvec_led(led_cdev) \
-	container_of(led_cdev, struct nvec_led, cdev)
+static inline struct nvec_led *to_nvec_led(struct led_classdev *led_cdev)
+{
+	return container_of(led_cdev, struct nvec_led, cdev);
+}

There is no need for a function, or a macro, for this, as it is only used once. Please just open-code it in the one place it is used instead.

thanks,

greg k-h