Skip to contentSkip to conversation

gpiolib: Introduce for_each_gpio_desc() macro

PATCH Superseded Andy Shevchenko 2 participants 6 messages May 10, 2021 – May 12, 2021 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: Introduce for_each_gpio_desc() macro

In a few places we are using a loop against all GPIO descriptors for a given device. Replace it with a consolidated for_each tupe of macro.

---
 drivers/gpio/gpiolib-of.c    | 9 ++++-----
 drivers/gpio/gpiolib-sysfs.c | 3 +--
 drivers/gpio/gpiolib.c       | 7 ++++---
 drivers/gpio/gpiolib.h       | 6 ++++++
 4 files changed, 15 insertions(+), 10 deletions(-)
drivers/gpio/gpiolib-of.c +4 -5
index bbcc7c073f63..fb1bfbfb3fc3 100644
@@ -711,13 +711,12 @@ static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
 				   struct device_node *hog)
 {
-	struct gpio_desc *descs = chip->gpiodev->descs;
+	struct gpio_desc *desc;
 	unsigned int i;
 
-	for (i = 0; i < chip->ngpio; i++) {
-		if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
-		    descs[i].hog == hog)
-			gpiochip_free_own_desc(&descs[i]);
+	for_each_gpio_desc(i, chip, desc) {
+		if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)
+			gpiochip_free_own_desc(desc);
 	}
 }
 
drivers/gpio/gpiolib-sysfs.c +1 -2
index ae49bb23c6ed..b0c7a303adde 100644
@@ -801,8 +801,7 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)
 	mutex_unlock(&sysfs_lock);
 
 	/* unregister gpiod class devices owned by sysfs */
-	for (i = 0; i < chip->ngpio; i++) {
-		desc = &gdev->descs[i];
+	for_each_gpio_desc(i, chip, desc) {
 		if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
 			gpiod_free(desc);
 	}
drivers/gpio/gpiolib.c +4 -3
index 220a9d8dd4e3..d61e11084a09 100644
@@ -4012,11 +4012,12 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
  */
 static void gpiochip_free_hogs(struct gpio_chip *gc)
 {
+	struct gpio_desc *desc;
 	int id;
 
-	for (id = 0; id < gc->ngpio; id++) {
-		if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
-			gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
+	for_each_gpio_desc(id, gc, desc) {
+		if (test_bit(FLAG_IS_HOGGED, &desc->flags))
+			gpiochip_free_own_desc(desc);
 	}
 }
 
drivers/gpio/gpiolib.h +6 -0
index 30bc3f80f83e..b6d8d71bfbd5 100644
@@ -82,6 +82,12 @@ struct gpio_array {
 };
 
 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
+
+#define for_each_gpio_desc(i, gc, desc)			\
+	for (i = 0, desc = gpiochip_get_desc(gc, i);	\
+	     i < gc->ngpio;				\
+	     i++, desc = gpiochip_get_desc(gc, i))	\
+
 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
 				  unsigned int array_size,
 				  struct gpio_desc **desc_array,
Signature · 1 line
2.30.2
#1 lore raw
I love your patch! Perhaps something to improve:

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on gpio/for-next]
[also build test WARNING on v5.13-rc1 next-20210510]
[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/Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next
config: arm-randconfig-s031-20210510 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/ceaf41face19ca3a36b81b3b866c1708a90cb4e2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305 git checkout ceaf41face19ca3a36b81b3b866c1708a90cb4e2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=arm

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> drivers/gpio/gpiolib-of.c:718:50: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned long const volatile *addr @@     got unsigned long flags @@

drivers/gpio/gpiolib-of.c:718:50: sparse: expected unsigned long const volatile *addr
drivers/gpio/gpiolib-of.c:718:50: sparse: got unsigned long flags

vim +718 drivers/gpio/gpiolib-of.c

704
705 #ifdef CONFIG_OF_DYNAMIC
706 /**
707 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708 * @chip: gpio chip to act on
709 * @hog: device node describing the hogs
710 */
711 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712 struct device_node *hog)
713 {
714 struct gpio_desc *desc;
715 unsigned int i;
716
717 for_each_gpio_desc(i, chip, desc) {

 > 718			if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)

719 gpiochip_free_own_desc(desc);
720 }
721 }
722

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

#2 lore raw
Quoted 26 lines · kernel test robot

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on gpio/for-next]
[also build test WARNING on v5.13-rc1 next-20210510]
[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/Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next
config: arm-randconfig-s031-20210510 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/ceaf41face19ca3a36b81b3b866c1708a90cb4e2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305 git checkout ceaf41face19ca3a36b81b3b866c1708a90cb4e2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=arm

Quoted 2 lines

If you fix the issue, kindly add following tag as appropriate

Right, I missed OF part compilation, thanks for reminder, I'll fix my kernel configuration and the patch in v2.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Andy Shevchenko
I love your patch! Perhaps something to improve:

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on gpio/for-next]
[also build test WARNING on v5.13-rc1 next-20210510]
[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/Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next
config: h8300-randconfig-r012-20210510 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.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/0day-ci/linux/commit/ceaf41face19ca3a36b81b3b866c1708a90cb4e2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305 git checkout ceaf41face19ca3a36b81b3b866c1708a90cb4e2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=h8300

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

drivers/gpio/gpiolib-of.c: In function 'of_gpiochip_remove_hog':

>> drivers/gpio/gpiolib-of.c:718:36: warning: passing argument 2 of 'test_bit' makes pointer from integer without a cast [-Wint-conversion]
     718 |   if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)
         |                                ~~~~^~~~~~~
         |                                    |
         |                                    long unsigned int
   In file included from include/linux/bitops.h:29,
                    from include/linux/kernel.h:12,
                    from include/asm-generic/bug.h:20,
                    from arch/h8300/include/asm/bug.h:8,
                    from include/linux/bug.h:5,
                    from include/linux/thread_info.h:12,
                    from include/asm-generic/current.h:5,
                    from ./arch/h8300/include/generated/asm/current.h:1,
                    from include/linux/sched.h:12,
                    from include/linux/ratelimit.h:6,
                    from include/linux/dev_printk.h:16,
                    from include/linux/device.h:15,
                    from drivers/gpio/gpiolib-of.c:10:

arch/h8300/include/asm/bitops.h:63:66: note: expected 'const volatile long unsigned int *' but argument is of type 'long unsigned int'
63 | static inline int test_bit(int nr, const volatile unsigned long *addr)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~

vim +/test_bit +718 drivers/gpio/gpiolib-of.c

704
705 #ifdef CONFIG_OF_DYNAMIC
706 /**
707 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708 * @chip: gpio chip to act on
709 * @hog: device node describing the hogs
710 */
711 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712 struct device_node *hog)
713 {
714 struct gpio_desc *desc;
715 unsigned int i;
716
717 for_each_gpio_desc(i, chip, desc) {

 > 718			if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)

719 gpiochip_free_own_desc(desc);
720 }
721 }
722

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

#4 lore raw
In reply to Andy Shevchenko
I love your patch! Perhaps something to improve:

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on gpio/for-next]
[also build test WARNING on v5.13-rc1 next-20210510]
[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/Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next
config: riscv-randconfig-r011-20210510 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 492173d42b32cb91d5d0d72d5ed84fcab80d059a)

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 riscv cross compiling tool for clang build
# apt-get install binutils-riscv64-linux-gnu
# https://github.com/0day-ci/linux/commit/ceaf41face19ca3a36b81b3b866c1708a90cb4e2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305 git checkout ceaf41face19ca3a36b81b3b866c1708a90cb4e2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=riscv

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/gpio/gpiolib-of.c:718:32: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'const volatile unsigned long *'; take the address with & [-Wint-conversion]
                   if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)
                                                ^~~~~~~~~~~
                                                &
   include/linux/compiler.h:56:47: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                                                 ^~~~
   include/linux/compiler.h:58:52: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                      ^~~~
   include/asm-generic/bitops/non-atomic.h:104:66: note: passing argument to parameter 'addr' here
   static inline int test_bit(int nr, const volatile unsigned long *addr)
                                                                    ^
>> drivers/gpio/gpiolib-of.c:718:32: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'const volatile unsigned long *'; take the address with & [-Wint-conversion]
                   if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)
                                                ^~~~~~~~~~~
                                                &
   include/linux/compiler.h:56:47: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                                                 ^~~~
   include/linux/compiler.h:58:61: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                               ^~~~
   include/asm-generic/bitops/non-atomic.h:104:66: note: passing argument to parameter 'addr' here
   static inline int test_bit(int nr, const volatile unsigned long *addr)
                                                                    ^
>> drivers/gpio/gpiolib-of.c:718:32: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'const volatile unsigned long *'; take the address with & [-Wint-conversion]
                   if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)
                                                ^~~~~~~~~~~
                                                &
   include/linux/compiler.h:56:47: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                                                 ^~~~
   include/linux/compiler.h:58:86: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                                                        ^~~~
   include/linux/compiler.h:69:3: note: expanded from macro '__trace_if_value'
           (cond) ?                                        \
            ^~~~
   include/asm-generic/bitops/non-atomic.h:104:66: note: passing argument to parameter 'addr' here
   static inline int test_bit(int nr, const volatile unsigned long *addr)
                                                                    ^

3 warnings generated.

vim +718 drivers/gpio/gpiolib-of.c

704
705 #ifdef CONFIG_OF_DYNAMIC
706 /**
707 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708 * @chip: gpio chip to act on
709 * @hog: device node describing the hogs
710 */
711 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712 struct device_node *hog)
713 {
714 struct gpio_desc *desc;
715 unsigned int i;
716
717 for_each_gpio_desc(i, chip, desc) {

 > 718			if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)

719 gpiochip_free_own_desc(desc);
720 }
721 }
722

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

#5 lore raw
In reply to Andy Shevchenko
I love your patch! Perhaps something to improve:

Hi Andy,

I love your patch! Perhaps something to improve:

[auto build test WARNING on gpio/for-next]
[also build test WARNING on v5.13-rc1 next-20210512]
[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/Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next
config: m68k-randconfig-s032-20210512 (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/ceaf41face19ca3a36b81b3b866c1708a90cb4e2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Andy-Shevchenko/gpiolib-Introduce-for_each_gpio_desc-macro/20210511-035305 git checkout ceaf41face19ca3a36b81b3b866c1708a90cb4e2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=m68k

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> drivers/gpio/gpiolib-of.c:718:50: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned long const volatile *vaddr @@     got unsigned long flags @@

drivers/gpio/gpiolib-of.c:718:50: sparse: expected unsigned long const volatile *vaddr
drivers/gpio/gpiolib-of.c:718:50: sparse: got unsigned long flags

vim +718 drivers/gpio/gpiolib-of.c

704
705 #ifdef CONFIG_OF_DYNAMIC
706 /**
707 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708 * @chip: gpio chip to act on
709 * @hog: device node describing the hogs
710 */
711 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712 struct device_node *hog)
713 {
714 struct gpio_desc *desc;
715 unsigned int i;
716
717 for_each_gpio_desc(i, chip, desc) {

 > 718			if (test_bit(FLAG_IS_HOGGED, desc->flags) && desc->hog == hog)

719 gpiochip_free_own_desc(desc);
720 }
721 }
722

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