Skip to contentSkip to conversation

ALSA: control: Use list_for_each_entry_safe()

PATCH Superseded Andy Shevchenko 4 participants 6 messages Aug 29, 2023 Sound

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: control: Use list_for_each_entry_safe()

Instead of reiterating the list, use list_for_each_entry_safe() that allows to continue without starting over.

---

Takashi, if you have anybody or want yourself to spend some time, I believe you can simplify a lot the parser in this file with the help of lib/cmdline.c APIs.

 sound/core/control_led.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
sound/core/control_led.c +5 -10
index a78eb48927c7..afc9ffc388e3 100644
@@ -297,16 +297,13 @@ static void snd_ctl_led_clean(struct snd_card *card)
 {
 	unsigned int group;
 	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
 
 	for (group = 0; group < MAX_LED; group++) {
 		led = &snd_ctl_leds[group];
-repeat:
-		list_for_each_entry(lctl, &led->controls, list)
-			if (!card || lctl->card == card) {
+		list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
+			if (!card || lctl->card == card)
 				snd_ctl_led_ctl_destroy(lctl);
-				goto repeat;
-			}
 	}
 }
 
@@ -314,7 +311,7 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
 {
 	struct snd_card *card;
 	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
 	struct snd_kcontrol_volatile *vd;
 	bool change = false;
 
@@ -329,14 +326,12 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
 		return -ENXIO;
 	}
 	led = &snd_ctl_leds[group];
-repeat:
-	list_for_each_entry(lctl, &led->controls, list)
+	list_for_each_entry(lctl, _lctl, &led->controls, list)
 		if (lctl->card == card) {
 			vd = &lctl->kctl->vd[lctl->index_offset];
 			vd->access &= ~group_to_access(group);
 			snd_ctl_led_ctl_destroy(lctl);
 			change = true;
-			goto repeat;
 		}
 	mutex_unlock(&snd_ctl_led_mutex);
 	if (change)
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw
Quoted 5 lines · Andy Shevchenko

Instead of reiterating the list, use list_for_each_entry_safe() that allows to continue without starting over.

Through a quick glance, it should be OK, but I need to read and understand whether this change is perfectly safe or not -- unless Jaroslav gives his review and ack.

Quoted 5 lines

---

Takashi, if you have anybody or want yourself to spend some time, I believe you can simplify a lot the parser in this file with the help of lib/cmdline.c APIs.

Thanks for the hint. Yeah, it looks feasible, but too late for 6.6, it's a nice TODO ;)

Takashi

Quoted 56 lines
 sound/core/control_led.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
sound/core/control_led.c +5 -10
index a78eb48927c7..afc9ffc388e3 100644
@@ -297,16 +297,13 @@ static void snd_ctl_led_clean(struct snd_card *card)
 {
 	unsigned int group;
 	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
 
 	for (group = 0; group < MAX_LED; group++) {
 		led = &snd_ctl_leds[group];
-repeat:
-		list_for_each_entry(lctl, &led->controls, list)
-			if (!card || lctl->card == card) {
+		list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
+			if (!card || lctl->card == card)
 				snd_ctl_led_ctl_destroy(lctl);
-				goto repeat;
-			}
 	}
 }
 
@@ -314,7 +311,7 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
 {
 	struct snd_card *card;
 	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
 	struct snd_kcontrol_volatile *vd;
 	bool change = false;
 
@@ -329,14 +326,12 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
 		return -ENXIO;
 	}
 	led = &snd_ctl_leds[group];
-repeat:
-	list_for_each_entry(lctl, &led->controls, list)
+	list_for_each_entry(lctl, _lctl, &led->controls, list)
 		if (lctl->card == card) {
 			vd = &lctl->kctl->vd[lctl->index_offset];
 			vd->access &= ~group_to_access(group);
 			snd_ctl_led_ctl_destroy(lctl);
 			change = true;
-			goto repeat;
 		}
 	mutex_unlock(&snd_ctl_led_mutex);
 	if (change)
Signature · 1 line
2.40.0.1.gaa8946217a0b
#2 lore raw
Quoted 9 lines · Takashi Iwai
Quoted 3 lines · Andy Shevchenko

Instead of reiterating the list, use list_for_each_entry_safe() that allows to continue without starting over.

Through a quick glance, it should be OK, but I need to read and understand whether this change is perfectly safe or not -- unless Jaroslav gives his review and ack.

Sure.

Quoted 8 lines
Quoted 5 lines

---

Takashi, if you have anybody or want yourself to spend some time, I believe you can simplify a lot the parser in this file with the help of lib/cmdline.c APIs.

Thanks for the hint. Yeah, it looks feasible, but too late for 6.6, it's a nice TODO ;)

Of course.

Just a reference what I did to gpio-aggregator module:
ac505b6f5fa8 ("gpio: aggregator: Replace custom get_arg() with a generic next_arg()") deb631c40114 ("gpio: aggregator: Replace isrange() by using get_options()")

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko

Instead of reiterating the list, use list_for_each_entry_safe() that allows to continue without starting over.

---

Takashi, if you have anybody or want yourself to spend some time, I believe you can simplify a lot the parser in this file with the help of lib/cmdline.c APIs.

You probably mean next_arg() function. Unfortunately, it does not handle all cases we need to parse. The control IDs are a bit different than standard arguments.

Quoted 37 lines
  sound/core/control_led.c | 15 +++++----------
  1 file changed, 5 insertions(+), 10 deletions(-)
sound/core/control_led.c +4 -7
index a78eb48927c7..afc9ffc388e3 100644
@@ -297,16 +297,13 @@ static void snd_ctl_led_clean(struct snd_card *card)
  {
  	unsigned int group;
  	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
 
  	for (group = 0; group < MAX_LED; group++) {
  		led = &snd_ctl_leds[group];
-repeat:
-		list_for_each_entry(lctl, &led->controls, list)
-			if (!card || lctl->card == card) {
+		list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
+			if (!card || lctl->card == card)
  				snd_ctl_led_ctl_destroy(lctl);
-				goto repeat;
-			}
  	}
  }
 
@@ -314,7 +311,7 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
  {
  	struct snd_card *card;
  	struct snd_ctl_led *led;
-	struct snd_ctl_led_ctl *lctl;
+	struct snd_ctl_led_ctl *lctl, _lctl;
  	struct snd_kcontrol_volatile *vd;
  	bool change = false;
 

sound/core/control_led.c static int snd_ctl_led_reset(int card_number, unsigned int group) · line 329

Quoted 6 lines
sound/core/control_led.c +1 -2
@@ -329,14 +326,12 @@ static int snd_ctl_led_reset(int card_number, unsigned int group)
  		return -ENXIO;
  	}
  	led = &snd_ctl_leds[group];
-repeat:
-	list_for_each_entry(lctl, &led->controls, list)
+	list_for_each_entry(lctl, _lctl, &led->controls, list)

The list_for_each_entry_safe() should be used here, too.

With the fix:

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#4 lore raw
Quoted 1 line · Jaroslav Kysela

On 29. 08. 23 15:52, Andy Shevchenko wrote:

...

Quoted 7 lines
Quoted 3 lines

Takashi, if you have anybody or want yourself to spend some time, I believe you can simplify a lot the parser in this file with the help of lib/cmdline.c APIs.

You probably mean next_arg() function. Unfortunately, it does not handle all cases we need to parse. The control IDs are a bit different than standard arguments.

Not only that, but also get_option() / get_options(). It might still make sense to look into and expand next_arg() to do what you want (like I have done to get_option() which later allows to have parse_int_array_user() be implemented).

It also have test cases, which can be expanded / amended as well.

...

Quoted 4 lines

sound/core/control_led.c static int snd_ctl_led_reset(int card_number, unsigned int group) · line 329

Quoted 2 lines
sound/core/control_led.c +1 -1
-	list_for_each_entry(lctl, &led->controls, list)
+	list_for_each_entry(lctl, _lctl, &led->controls, list)

The list_for_each_entry_safe() should be used here, too.

Oh, good catch!

Quoted 3 lines

With the fix:

Thank you, I'll send a v2 soon.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Andy Shevchenko
kernel test robot noticed the following build warnings:

Hi Andy,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on tiwai-sound/for-linus linus/master v6.5 next-20230829] [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#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/ALSA-control-Use-list_for_each_entry_safe/20230829-215501
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
patch link:    https://lore.kernel.org/r/20230829135252.3915124-1-andriy.shevchenko%40linux.intel.com
patch subject: [PATCH v1 1/1] ALSA: control: Use list_for_each_entry_safe()
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20230829/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230829/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

include/linux/list.h:520:9: note: in expansion of macro 'container_of'

     520 |         container_of(ptr, type, member)
         |         ^~~~~~~~~~~~
   include/linux/list.h:564:9: note: in expansion of macro 'list_entry'
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |         ^~~~~~~~~~
   include/linux/list.h:779:27: note: in expansion of macro 'list_next_entry'
     779 |              pos = n, n = list_next_entry(n, member))
         |                           ^~~~~~~~~~~~~~~
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/list.h:564:25: error: invalid type argument of '->' (have 'struct snd_ctl_led_ctl')
     564 |         list_entry((pos)->member.next, typeof(*(pos)), 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:21:23: note: in expansion of macro '__same_type'
      21 |                       __same_type(*(ptr), void),                        \
         |                       ^~~~~~~~~~~
   include/linux/list.h:520:9: note: in expansion of macro 'container_of'
     520 |         container_of(ptr, type, member)
         |         ^~~~~~~~~~~~
   include/linux/list.h:564:9: note: in expansion of macro 'list_entry'
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |         ^~~~~~~~~~
   include/linux/list.h:779:27: note: in expansion of macro 'list_next_entry'
     779 |              pos = n, n = list_next_entry(n, member))
         |                           ^~~~~~~~~~~~~~~
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
   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) ||       \
         |                       ^~~~~~~~~~~
   include/linux/list.h:520:9: note: in expansion of macro 'container_of'
     520 |         container_of(ptr, type, member)
         |         ^~~~~~~~~~~~
   include/linux/list.h:564:9: note: in expansion of macro 'list_entry'
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |         ^~~~~~~~~~
   include/linux/list.h:779:27: note: in expansion of macro 'list_next_entry'
     779 |              pos = n, n = list_next_entry(n, member))
         |                           ^~~~~~~~~~~~~~~
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/list.h:564:47: error: invalid type argument of unary '*' (have 'struct snd_ctl_led_ctl')
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |                                               ^~~~~~
   include/linux/container_of.h:23:11: note: in definition of macro 'container_of'
      23 |         ((type *)(__mptr - offsetof(type, member))); })
         |           ^~~~
   include/linux/list.h:564:9: note: in expansion of macro 'list_entry'
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |         ^~~~~~~~~~
   include/linux/list.h:779:27: note: in expansion of macro 'list_next_entry'
     779 |              pos = n, n = list_next_entry(n, member))
         |                           ^~~~~~~~~~~~~~~
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
   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/uapi/linux/sysinfo.h:5,
                    from include/uapi/linux/kernel.h:5,
                    from include/linux/cache.h:5,
                    from include/linux/slab.h:15:
   include/linux/list.h:564:47: error: invalid type argument of unary '*' (have 'struct snd_ctl_led_ctl')
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |                                               ^~~~~~
   include/linux/stddef.h:16:52: note: in definition of macro 'offsetof'
      16 | #define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
         |                                                    ^~~~
   include/linux/list.h:520:9: note: in expansion of macro 'container_of'
     520 |         container_of(ptr, type, member)
         |         ^~~~~~~~~~~~
   include/linux/list.h:564:9: note: in expansion of macro 'list_entry'
     564 |         list_entry((pos)->member.next, typeof(*(pos)), member)
         |         ^~~~~~~~~~
   include/linux/list.h:779:27: note: in expansion of macro 'list_next_entry'
     779 |              pos = n, n = list_next_entry(n, member))
         |                           ^~~~~~~~~~~~~~~
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
>> include/linux/list.h:779:21: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     779 |              pos = n, n = list_next_entry(n, member))
         |                     ^
   sound/core/control_led.c:304:17: note: in expansion of macro 'list_for_each_entry_safe'
     304 |                 list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~
   sound/core/control_led.c: In function 'snd_ctl_led_reset':
   sound/core/control_led.c:329:62: error: macro "list_for_each_entry" passed 4 arguments, but takes just 3
     329 |         list_for_each_entry(lctl, _lctl, &led->controls, list)
         |                                                              ^
   include/linux/list.h:688: note: macro "list_for_each_entry" defined here
     688 | #define list_for_each_entry(pos, head, member)                          \
         | 
   sound/core/control_led.c:329:9: error: 'list_for_each_entry' undeclared (first use in this function); did you mean 'bus_for_each_drv'?
     329 |         list_for_each_entry(lctl, _lctl, &led->controls, list)
         |         ^~~~~~~~~~~~~~~~~~~
         |         bus_for_each_drv
   sound/core/control_led.c:329:9: note: each undeclared identifier is reported only once for each function it appears in
   sound/core/control_led.c:329:28: error: expected ';' before 'if'
     329 |         list_for_each_entry(lctl, _lctl, &led->controls, list)
         |                            ^
         |                            ;
     330 |                 if (lctl->card == card) {
         |                 ~~          
>> sound/core/control_led.c:315:39: warning: unused variable 'vd' [-Wunused-variable]
     315 |         struct snd_kcontrol_volatile *vd;
         |                                       ^~
>> sound/core/control_led.c:314:39: warning: unused variable '_lctl' [-Wunused-variable]
     314 |         struct snd_ctl_led_ctl *lctl, _lctl;
         |                                       ^~~~~
>> sound/core/control_led.c:314:33: warning: unused variable 'lctl' [-Wunused-variable]
     314 |         struct snd_ctl_led_ctl *lctl, _lctl;
         |                                 ^~~~
>> sound/core/control_led.c:313:29: warning: variable 'led' set but not used [-Wunused-but-set-variable]
     313 |         struct snd_ctl_led *led;
         |                             ^~~

vim +/vd +315 sound/core/control_led.c

22d8de62f11b287 Jaroslav Kysela 2021-03-17  309  
a135dfb5de15013 Jaroslav Kysela 2021-03-17  310  static int snd_ctl_led_reset(int card_number, unsigned int group)
a135dfb5de15013 Jaroslav Kysela 2021-03-17  311  {
a135dfb5de15013 Jaroslav Kysela 2021-03-17  312  	struct snd_card *card;
a135dfb5de15013 Jaroslav Kysela 2021-03-17 @313  	struct snd_ctl_led *led;
8d00c707fea8284 Andy Shevchenko 2023-08-29 @314  	struct snd_ctl_led_ctl *lctl, _lctl;
a135dfb5de15013 Jaroslav Kysela 2021-03-17 @315  	struct snd_kcontrol_volatile *vd;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  316  	bool change = false;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  317  
a135dfb5de15013 Jaroslav Kysela 2021-03-17  318  	card = snd_card_ref(card_number);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  319  	if (!card)
a135dfb5de15013 Jaroslav Kysela 2021-03-17  320  		return -ENXIO;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  321  
a135dfb5de15013 Jaroslav Kysela 2021-03-17  322  	mutex_lock(&snd_ctl_led_mutex);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  323  	if (!snd_ctl_led_card_valid[card_number]) {
a135dfb5de15013 Jaroslav Kysela 2021-03-17  324  		mutex_unlock(&snd_ctl_led_mutex);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  325  		snd_card_unref(card);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  326  		return -ENXIO;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  327  	}
a135dfb5de15013 Jaroslav Kysela 2021-03-17  328  	led = &snd_ctl_leds[group];
8d00c707fea8284 Andy Shevchenko 2023-08-29  329  	list_for_each_entry(lctl, _lctl, &led->controls, list)
a135dfb5de15013 Jaroslav Kysela 2021-03-17  330  		if (lctl->card == card) {
a135dfb5de15013 Jaroslav Kysela 2021-03-17  331  			vd = &lctl->kctl->vd[lctl->index_offset];
a135dfb5de15013 Jaroslav Kysela 2021-03-17  332  			vd->access &= ~group_to_access(group);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  333  			snd_ctl_led_ctl_destroy(lctl);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  334  			change = true;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  335  		}
a135dfb5de15013 Jaroslav Kysela 2021-03-17  336  	mutex_unlock(&snd_ctl_led_mutex);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  337  	if (change)
a135dfb5de15013 Jaroslav Kysela 2021-03-17  338  		snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  339  	snd_card_unref(card);
a135dfb5de15013 Jaroslav Kysela 2021-03-17  340  	return 0;
a135dfb5de15013 Jaroslav Kysela 2021-03-17  341  }
a135dfb5de15013 Jaroslav Kysela 2021-03-17  342  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki