Skip to contentSkip to conversation

ALSA: control: Avoid nested locks at notification

RFC PATCH In review Takashi Iwai 2 participants 3 messages May 23, 2023 – May 24, 2023 Sound

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ALSA: control: Avoid nested locks at notification

The new control layer stuff introduced the nested rwsem for managing the list of registered control layer ops. Since then, a global snd_ctl_layer_rwsem is always read at each time a control notification is sent via snd_ctl_notify*() in a nested matter inside the card's controls_rwsem lock. This also required a bit complicated way of the lock at snd_ctl_activate_id() and snd_ctl_elem_write() with the downgrade of rwsem.

This patch is an attempt to simplify the handling of ctl layer ops. Now, instead of traversing the global linked list, we keep a local list of lops in each card instance. This reduces the need of the global snd_ctl_layer_rwsem lock at snd_ctl_notify*() invocation. And, since the nested lock is avoided in most places, we can also avoid the rwsem downgrade hack in the above, too.

Since the local list entry is created dynamically, snd_ctl_register_layer() may return an error, and the caller needs to check the return value.

---

I noticed the nested lock while looking at the pending bug report ("USB sound card freezes USB after resume from suspend" -- which hasn't been resolved yet). Only very lightly tested.

 include/sound/control.h  |   2 +-
 include/sound/core.h     |   9 +++
 sound/core/control.c     | 138 +++++++++++++++++++++++++++------------
 sound/core/control_led.c |   3 +-
 sound/core/init.c        |   1 +
 5 files changed, 108 insertions(+), 45 deletions(-)
include/sound/control.h +1 -1
index cc3dcc6cfb0f..c40c693853e6 100644
@@ -156,7 +156,7 @@ int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
 #endif
 
 int snd_ctl_request_layer(const char *module_name);
-void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
+int snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
 void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops);
 
 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
include/sound/core.h +9 -0
index 4ea5f66b59d7..3e49fb483340 100644
@@ -75,6 +75,14 @@ struct snd_device {
 
 #define snd_device(n) list_entry(n, struct snd_device, list)
 
+/* control layer ops */
+struct snd_ctl_layer_ops;
+
+struct snd_ctl_lops_list {
+	struct snd_ctl_layer_ops *lops;
+	struct list_head list;
+};
+
 /* main structure for soundcard */
 
 struct snd_card {
@@ -104,6 +112,7 @@ struct snd_card {
 	size_t user_ctl_alloc_size;	// current memory allocation by user controls.
 	struct list_head controls;	/* all controls for this card */
 	struct list_head ctl_files;	/* active control files */
+	struct list_head lops_list;	/* list of associated control layer ops */
 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
 	struct xarray ctl_numids;	/* hash table for numids */
 	struct xarray ctl_hash;		/* hash table for ctl id matching */
sound/core/control.c +96 -42

224 lines not on this page · raw message

sound/core/control_led.c +1 -2
index 3cadd40100f3..18b366207a9c 100644
@@ -762,8 +762,7 @@ static int __init snd_ctl_led_init(void)
 			return -ENOMEM;
 		}
 	}
-	snd_ctl_register_layer(&snd_ctl_led_lops);
-	return 0;
+	return snd_ctl_register_layer(&snd_ctl_led_lops);
 }
 
 static void __exit snd_ctl_led_exit(void)
sound/core/init.c +1 -0
index df0c22480375..34bda15ad9ef 100644
@@ -314,6 +314,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 	rwlock_init(&card->ctl_files_rwlock);
 	INIT_LIST_HEAD(&card->controls);
 	INIT_LIST_HEAD(&card->ctl_files);
+	INIT_LIST_HEAD(&card->lops_list);
 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
 	xa_init(&card->ctl_numids);
 	xa_init(&card->ctl_hash);
Signature · 1 line
2.35.3
maintainer
#1 lore raw
Quoted 15 lines · Takashi Iwai

The new control layer stuff introduced the nested rwsem for managing the list of registered control layer ops. Since then, a global snd_ctl_layer_rwsem is always read at each time a control notification is sent via snd_ctl_notify*() in a nested matter inside the card's controls_rwsem lock. This also required a bit complicated way of the lock at snd_ctl_activate_id() and snd_ctl_elem_write() with the downgrade of rwsem.

This patch is an attempt to simplify the handling of ctl layer ops. Now, instead of traversing the global linked list, we keep a local list of lops in each card instance. This reduces the need of the global snd_ctl_layer_rwsem lock at snd_ctl_notify*() invocation. And, since the nested lock is avoided in most places, we can also avoid the rwsem downgrade hack in the above, too.

Quoted 3 lines

Since the local list entry is created dynamically, snd_ctl_register_layer() may return an error, and the caller needs to check the return value.

I'm not convinced about this transition. What about to move the layer notifications to a workqueue to reorder the controls_rwsem locking (kctl access) ?

Quoted 4 lines

---

I noticed the nested lock while looking at the pending bug report

From the log or code ?

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#2 lore raw
Quoted 17 lines · Jaroslav Kysela
Quoted 15 lines · Takashi Iwai

The new control layer stuff introduced the nested rwsem for managing the list of registered control layer ops. Since then, a global snd_ctl_layer_rwsem is always read at each time a control notification is sent via snd_ctl_notify*() in a nested matter inside the card's controls_rwsem lock. This also required a bit complicated way of the lock at snd_ctl_activate_id() and snd_ctl_elem_write() with the downgrade of rwsem.

This patch is an attempt to simplify the handling of ctl layer ops. Now, instead of traversing the global linked list, we keep a local list of lops in each card instance. This reduces the need of the global snd_ctl_layer_rwsem lock at snd_ctl_notify*() invocation. And, since the nested lock is avoided in most places, we can also avoid the rwsem downgrade hack in the above, too.

Quoted 7 lines
Quoted 3 lines

Since the local list entry is created dynamically, snd_ctl_register_layer() may return an error, and the caller needs to check the return value.

I'm not convinced about this transition. What about to move the layer notifications to a workqueue to reorder the controls_rwsem locking (kctl access) ?

It'll need allocation of each new notify event, no?

IMO, keeping the list of ops in each card instance may have a room for more possible improvement. The current patch chains always all lops in the card's lops_list, but when the register callback returns a certain error code for the uninteresting lops, we can skip chaining it. Then the unnecessary hook invocation at each notification can be avoided.

Quoted 6 lines
Quoted 4 lines

---

I noticed the nested lock while looking at the pending bug report

From the log or code ?

The code.

And, control_led.c has also a global mutex lock that is applied for almost all code paths. If the hook is called from all cards, this should be optimized as well. (OTOH, if it's called only from one bound card, it won't matter much.)

thanks,

Takashi