Skip to contentSkip to conversation

ASoC: cs42l43: Move shutter IRQ handling into a worker thread

PATCH In review Charles Keepax 4 participants 13 messages Jul 29, 2024 – Jul 31, 2024

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

PATCH 1/1 ASoC: cs42l43: Move shutter IRQ handling into a worker thread

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

To avoid this mutex inversion add a work item for handling the shutter IRQ and move the notifies into that.

---
 sound/soc/codecs/cs42l43.c | 43 +++++++++++++++++++++++++++-----------
 sound/soc/codecs/cs42l43.h |  3 +++
 2 files changed, 34 insertions(+), 12 deletions(-)
sound/soc/codecs/cs42l43.c +31 -12
index 92674314227c4..74cb396ec0576 100644
@@ -249,9 +249,10 @@ CS42L43_IRQ_COMPLETE(spkr_startup)
 CS42L43_IRQ_COMPLETE(spkl_startup)
 CS42L43_IRQ_COMPLETE(load_detect)
 
-static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
+static void cs42l43_mic_shutter_work(struct work_struct *work)
 {
-	struct cs42l43_codec *priv = data;
+	struct cs42l43_codec *priv = container_of(work, struct cs42l43_codec,
+						  mic_shutter_work);
 	static const char * const controls[] = {
 		"Decimator 1 Switch",
 		"Decimator 2 Switch",
@@ -263,32 +264,47 @@ static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
 	dev_dbg(priv->dev, "Microphone shutter changed\n");
 
 	if (!priv->component)
-		return IRQ_NONE;
+		return;
 
 	for (i = 0; i < ARRAY_SIZE(controls); i++) {
-		ret = snd_soc_component_notify_control(priv->component,
-						       controls[i]);
+		ret = snd_soc_component_notify_control(priv->component, controls[i]);
 		if (ret)
-			return IRQ_NONE;
+			dev_err(priv->dev, "Failed to notify control %s: %d\n",
+				controls[i], ret);
 	}
+}
+
+static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
+{
+	struct cs42l43_codec *priv = data;
+
+	queue_work(system_wq, &priv->mic_shutter_work);
 
 	return IRQ_HANDLED;
 }
 
-static irqreturn_t cs42l43_spk_shutter(int irq, void *data)
+static void cs42l43_spk_shutter_work(struct work_struct *work)
 {
-	struct cs42l43_codec *priv = data;
+	struct cs42l43_codec *priv = container_of(work, struct cs42l43_codec,
+						  mic_shutter_work);
+	const char * const control = "Speaker Digital Switch";
 	int ret;
 
 	dev_dbg(priv->dev, "Speaker shutter changed\n");
 
 	if (!priv->component)
-		return IRQ_NONE;
+		return;
 
-	ret = snd_soc_component_notify_control(priv->component,
-					       "Speaker Digital Switch");
+	ret = snd_soc_component_notify_control(priv->component, control);
 	if (ret)
-		return IRQ_NONE;
+		dev_err(priv->dev, "Failed to notify control %s: %d\n", control, ret);
+}
+
+static irqreturn_t cs42l43_spk_shutter(int irq, void *data)
+{
+	struct cs42l43_codec *priv = data;
+
+	queue_work(system_wq, &priv->spk_shutter_work);
 
 	return IRQ_HANDLED;
 }
@@ -2280,6 +2296,9 @@ static int cs42l43_codec_probe(struct platform_device *pdev)
 	INIT_WORK(&priv->button_release_work, cs42l43_button_release_work);
 	INIT_WORK(&priv->hp_ilimit_work, cs42l43_hp_ilimit_work);
 
+	INIT_WORK(&priv->mic_shutter_work, cs42l43_mic_shutter_work);
+	INIT_WORK(&priv->spk_shutter_work, cs42l43_spk_shutter_work);
+
 	pm_runtime_set_autosuspend_delay(priv->dev, 100);
 	pm_runtime_use_autosuspend(priv->dev);
 	pm_runtime_set_active(priv->dev);
sound/soc/codecs/cs42l43.h +3 -0
index 9924c13e1eb53..5592aab5c8042 100644
@@ -100,6 +100,9 @@ struct cs42l43_codec {
 	struct delayed_work hp_ilimit_clear_work;
 	bool hp_ilimited;
 	int hp_ilimit_count;
+
+	struct work_struct mic_shutter_work;
+	struct work_struct spk_shutter_work;
 };
 
 #if IS_REACHABLE(CONFIG_SND_SOC_CS42L43_SDW)
Signature · 1 line
2.39.2
#1 lore raw
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

thanks,

Takashi

Quoted 113 lines

To avoid this mutex inversion add a work item for handling the shutter IRQ and move the notifies into that.

---
 sound/soc/codecs/cs42l43.c | 43 +++++++++++++++++++++++++++-----------
 sound/soc/codecs/cs42l43.h |  3 +++
 2 files changed, 34 insertions(+), 12 deletions(-)
sound/soc/codecs/cs42l43.c +31 -12
index 92674314227c4..74cb396ec0576 100644
@@ -249,9 +249,10 @@ CS42L43_IRQ_COMPLETE(spkr_startup)
 CS42L43_IRQ_COMPLETE(spkl_startup)
 CS42L43_IRQ_COMPLETE(load_detect)
 
-static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
+static void cs42l43_mic_shutter_work(struct work_struct *work)
 {
-	struct cs42l43_codec *priv = data;
+	struct cs42l43_codec *priv = container_of(work, struct cs42l43_codec,
+						  mic_shutter_work);
 	static const char * const controls[] = {
 		"Decimator 1 Switch",
 		"Decimator 2 Switch",
@@ -263,32 +264,47 @@ static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
 	dev_dbg(priv->dev, "Microphone shutter changed\n");
 
 	if (!priv->component)
-		return IRQ_NONE;
+		return;
 
 	for (i = 0; i < ARRAY_SIZE(controls); i++) {
-		ret = snd_soc_component_notify_control(priv->component,
-						       controls[i]);
+		ret = snd_soc_component_notify_control(priv->component, controls[i]);
 		if (ret)
-			return IRQ_NONE;
+			dev_err(priv->dev, "Failed to notify control %s: %d\n",
+				controls[i], ret);
 	}
+}
+
+static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
+{
+	struct cs42l43_codec *priv = data;
+
+	queue_work(system_wq, &priv->mic_shutter_work);
 
 	return IRQ_HANDLED;
 }
 
-static irqreturn_t cs42l43_spk_shutter(int irq, void *data)
+static void cs42l43_spk_shutter_work(struct work_struct *work)
 {
-	struct cs42l43_codec *priv = data;
+	struct cs42l43_codec *priv = container_of(work, struct cs42l43_codec,
+						  mic_shutter_work);
+	const char * const control = "Speaker Digital Switch";
 	int ret;
 
 	dev_dbg(priv->dev, "Speaker shutter changed\n");
 
 	if (!priv->component)
-		return IRQ_NONE;
+		return;
 
-	ret = snd_soc_component_notify_control(priv->component,
-					       "Speaker Digital Switch");
+	ret = snd_soc_component_notify_control(priv->component, control);
 	if (ret)
-		return IRQ_NONE;
+		dev_err(priv->dev, "Failed to notify control %s: %d\n", control, ret);
+}
+
+static irqreturn_t cs42l43_spk_shutter(int irq, void *data)
+{
+	struct cs42l43_codec *priv = data;
+
+	queue_work(system_wq, &priv->spk_shutter_work);
 
 	return IRQ_HANDLED;
 }
@@ -2280,6 +2296,9 @@ static int cs42l43_codec_probe(struct platform_device *pdev)
 	INIT_WORK(&priv->button_release_work, cs42l43_button_release_work);
 	INIT_WORK(&priv->hp_ilimit_work, cs42l43_hp_ilimit_work);
 
+	INIT_WORK(&priv->mic_shutter_work, cs42l43_mic_shutter_work);
+	INIT_WORK(&priv->spk_shutter_work, cs42l43_spk_shutter_work);
+
 	pm_runtime_set_autosuspend_delay(priv->dev, 100);
 	pm_runtime_use_autosuspend(priv->dev);
 	pm_runtime_set_active(priv->dev);
sound/soc/codecs/cs42l43.h +3 -0
index 9924c13e1eb53..5592aab5c8042 100644
@@ -100,6 +100,9 @@ struct cs42l43_codec {
 	struct delayed_work hp_ilimit_clear_work;
 	bool hp_ilimited;
 	int hp_ilimit_count;
+
+	struct work_struct mic_shutter_work;
+	struct work_struct spk_shutter_work;
 };
 
 #if IS_REACHABLE(CONFIG_SND_SOC_CS42L43_SDW)
Signature · 1 line
2.39.2
#2 lore raw
Quoted 10 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

Quoted 4 lines

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem. On the other side, in the ALSA control handler which is obviously called under the controls_rwsem, we do a pm_runtime_resume, which causes SoundWire stuff to happen that takes the sdw_dev_lock.

Thanks,
Charles

#3 lore raw
Quoted 12 lines · Charles Keepax
Quoted 10 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

Quoted 7 lines
Quoted 4 lines

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem.

snd_ctl_notify() doesn't take rwsem but ctl_files_rwlock. And rwlock isn't taken except for two cases, at a list traverse at enumerating from user-space and at the device disconnection, so it shouldn't race. Anything missing there...?

thanks,

Takashi

Quoted 7 lines

On
the other side, in the ALSA control handler which is obviously called under the controls_rwsem, we do a pm_runtime_resume, which causes SoundWire stuff to happen that takes the sdw_dev_lock.

Thanks,
Charles

#4 lore raw
Quoted 19 lines · Takashi Iwai
Quoted 17 lines · Charles Keepax
Quoted 14 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

Quoted 8 lines
Quoted 2 lines

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem.

snd_ctl_notify() doesn't take rwsem but ctl_files_rwlock. And rwlock isn't taken except for two cases, at a list traverse at enumerating from user-space and at the device disconnection, so it shouldn't race. Anything missing there...?

The trouble here isn't the snd_ctl_notify, the handler uses snd_soc_component_notify_control which also does a snd_soc_card_get_kcontrol, which eventually calls snd_ctl_find_id which does take the controls_rwsem.

Thanks,
Charles

#5 lore raw
Quoted 30 lines · Charles Keepax
Quoted 19 lines · Takashi Iwai
Quoted 17 lines · Charles Keepax
Quoted 14 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

Quoted 8 lines
Quoted 2 lines

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem.

snd_ctl_notify() doesn't take rwsem but ctl_files_rwlock. And rwlock isn't taken except for two cases, at a list traverse at enumerating from user-space and at the device disconnection, so it shouldn't race. Anything missing there...?

Quoted 4 lines

The trouble here isn't the snd_ctl_notify, the handler uses snd_soc_component_notify_control which also does a snd_soc_card_get_kcontrol, which eventually calls snd_ctl_find_id which does take the controls_rwsem.

OK, now it's clearer. I think we can change snd_ctl_find_*() rather to take rwlock instead of rwsem. It's only a quick look-up, hence rwlock can work well.

Totally untested patch set is below.

Takashi

From 07cec46f18e61c8f9d2dda0606f10d8a10c13a51 Mon Sep 17 00:00:00 2001
From: Takashi Iwai <[email protected]>
Date: Tue, 30 Jul 2024 14:04:00 +0200
Subject: [PATCH 1/2] ALSA: control: Rename ctl_files_rwlock to controls_rwlock

We'll re-use the existing rwlock for the protection of control list lookup, too, and now rename it to a more generic name.

This is a preliminary change, only the rename of the struct field here, no functional changes.

---
 include/sound/core.h |  2 +-
 sound/core/control.c | 10 +++++-----
 sound/core/init.c    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
include/sound/core.h +1 -1
index dfef0c9d4b9f..55607e91d5fd 100644
@@ -99,7 +99,7 @@ struct snd_card {
 	struct device *ctl_dev;		/* control device */
 	unsigned int last_numid;	/* last used numeric ID */
 	struct rw_semaphore controls_rwsem;	/* controls lock (list and values) */
-	rwlock_t ctl_files_rwlock;	/* ctl_files list lock */
+	rwlock_t controls_rwlock;	/* lock for ctl_files list */
 	int controls_count;		/* count of all controls */
 	size_t user_ctl_alloc_size;	// current memory allocation by user controls.
 	struct list_head controls;	/* all controls for this card */
sound/core/control.c +5 -5
index 2b857757bb19..4b31f8753262 100644
@@ -79,7 +79,7 @@ static int snd_ctl_open(struct inode *inode, struct file *file)
 		ctl->preferred_subdevice[i] = -1;
 	ctl->pid = get_pid(task_pid(current));
 	file->private_data = ctl;
-	scoped_guard(write_lock_irqsave, &card->ctl_files_rwlock)
+	scoped_guard(write_lock_irqsave, &card->controls_rwlock)
 		list_add_tail(&ctl->list, &card->ctl_files);
 	snd_card_unref(card);
 	return 0;
@@ -117,7 +117,7 @@ static int snd_ctl_release(struct inode *inode, struct file *file)
 	file->private_data = NULL;
 	card = ctl->card;
 
-	scoped_guard(write_lock_irqsave, &card->ctl_files_rwlock)
+	scoped_guard(write_lock_irqsave, &card->controls_rwlock)
 		list_del(&ctl->list);
 
 	scoped_guard(rwsem_write, &card->controls_rwsem) {
@@ -157,7 +157,7 @@ void snd_ctl_notify(struct snd_card *card, unsigned int mask,
 	if (card->shutdown)
 		return;
 
-	guard(read_lock_irqsave)(&card->ctl_files_rwlock);
+	guard(read_lock_irqsave)(&card->controls_rwlock);
 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
 	card->mixer_oss_change_count++;
 #endif
@@ -2192,7 +2192,7 @@ int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
 	struct snd_ctl_file *kctl;
 	int subdevice = -1;
 
-	guard(read_lock_irqsave)(&card->ctl_files_rwlock);
+	guard(read_lock_irqsave)(&card->controls_rwlock);
 	list_for_each_entry(kctl, &card->ctl_files, list) {
 		if (kctl->pid == task_pid(current)) {
 			subdevice = kctl->preferred_subdevice[type];
@@ -2342,7 +2342,7 @@ static int snd_ctl_dev_disconnect(struct snd_device *device)
 	struct snd_card *card = device->device_data;
 	struct snd_ctl_file *ctl;
 
-	scoped_guard(read_lock_irqsave, &card->ctl_files_rwlock) {
+	scoped_guard(read_lock_irqsave, &card->controls_rwlock) {
 		list_for_each_entry(ctl, &card->ctl_files, list) {
 			wake_up(&ctl->change_sleep);
 			snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
sound/core/init.c +1 -1
index b9b708cf980d..b92aa7103589 100644
@@ -315,7 +315,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 	card->module = module;
 	INIT_LIST_HEAD(&card->devices);
 	init_rwsem(&card->controls_rwsem);
-	rwlock_init(&card->ctl_files_rwlock);
+	rwlock_init(&card->controls_rwlock);
 	INIT_LIST_HEAD(&card->controls);
 	INIT_LIST_HEAD(&card->ctl_files);
 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
Signature · 1 line
2.43.0

From 3965247ef976db0817a26572c1a50f5d7e5e1630 Mon Sep 17 00:00:00 2001
From: Takashi Iwai <[email protected]>
Date: Tue, 30 Jul 2024 14:28:10 +0200
Subject: [PATCH 2/2] ALSA: control: Use controls_rwlock instead of rwsem for
look-up

For a look-up of a control element via either numid or name matching, use controls_rwlock instead of controls_rwsem. This avoids the unnecessary (potential) deadlock.

snd_ctl_find_id_mixer_unlocked() is still left just an alias of snd_ctl_find_id_mixer(), as it's used in soc-card.c. Once after converting there, we can remove it later.

---
 include/sound/control.h     | 22 +---------
 include/sound/core.h        |  2 +-
 sound/core/control.c        | 88 +++++++++++++++----------------------
 sound/core/control_compat.c |  2 +-
 sound/core/control_led.c    |  2 +-
 sound/core/oss/mixer_oss.c  | 10 ++---
 6 files changed, 46 insertions(+), 80 deletions(-)
include/sound/control.h +2 -20
index 13511c50825f..6f9d3b8350ce 100644
@@ -140,9 +140,7 @@ int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
 int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
 void snd_ctl_rename(struct snd_card *card, struct snd_kcontrol *kctl, const char *name);
 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, int active);
-struct snd_kcontrol *snd_ctl_find_numid_locked(struct snd_card *card, unsigned int numid);
 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid);
-struct snd_kcontrol *snd_ctl_find_id_locked(struct snd_card *card, const struct snd_ctl_elem_id *id);
 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card, const struct snd_ctl_elem_id *id);
 
 /**
@@ -167,27 +165,11 @@ snd_ctl_find_id_mixer(struct snd_card *card, const char *name)
 	return snd_ctl_find_id(card, &id);
 }
 
-/**
- * snd_ctl_find_id_mixer_locked - find the control instance with the given name string
- * @card: the card instance
- * @name: the name string
- *
- * Finds the control instance with the given name and
- * @SNDRV_CTL_ELEM_IFACE_MIXER. Other fields are set to zero.
- *
- * This is merely a wrapper to snd_ctl_find_id_locked().
- * The caller must down card->controls_rwsem before calling this function.
- *
- * Return: The pointer of the instance if found, or %NULL if not.
- */
+/* to be removed */
 static inline struct snd_kcontrol *
 snd_ctl_find_id_mixer_locked(struct snd_card *card, const char *name)
 {
-	struct snd_ctl_elem_id id = {};
-
-	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
-	strscpy(id.name, name, sizeof(id.name));
-	return snd_ctl_find_id_locked(card, &id);
+	return snd_ctl_find_id_mixer(card, name);
 }
 
 int snd_ctl_create(struct snd_card *card);
include/sound/core.h +1 -1
index 55607e91d5fd..5c418ab24aa4 100644
@@ -99,7 +99,7 @@ struct snd_card {
 	struct device *ctl_dev;		/* control device */
 	unsigned int last_numid;	/* last used numeric ID */
 	struct rw_semaphore controls_rwsem;	/* controls lock (list and values) */
-	rwlock_t controls_rwlock;	/* lock for ctl_files list */
+	rwlock_t controls_rwlock;	/* lock for lookup and ctl_files list */
 	int controls_count;		/* count of all controls */
 	size_t user_ctl_alloc_size;	// current memory allocation by user controls.
 	struct list_head controls;	/* all controls for this card */
sound/core/control.c +36 -52

227 lines not on this page · raw message

sound/core/control_compat.c +1 -1
index 934bb945e702..401c12e62816 100644
@@ -168,7 +168,7 @@ static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
 	int err;
 
 	guard(rwsem_read)(&card->controls_rwsem);
-	kctl = snd_ctl_find_id_locked(card, id);
+	kctl = snd_ctl_find_id(card, id);
 	if (!kctl)
 		return -ENOENT;
 	info = kzalloc(sizeof(*info), GFP_KERNEL);
sound/core/control_led.c +1 -1
index 804805a95e2f..14eb3e6cc94c 100644
@@ -254,7 +254,7 @@ static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,
 	if (!card)
 		return -ENXIO;
 	guard(rwsem_write)(&card->controls_rwsem);
-	kctl = snd_ctl_find_id_locked(card, id);
+	kctl = snd_ctl_find_id(card, id);
 	if (!kctl)
 		return -ENOENT;
 	ioff = snd_ctl_get_ioff(kctl, id);
sound/core/oss/mixer_oss.c +5 -5
index 6a0508093ea6..33bf9a220ada 100644
@@ -510,7 +510,7 @@ static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, c
 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 	strscpy(id.name, name, sizeof(id.name));
 	id.index = index;
-	return snd_ctl_find_id_locked(card, &id);
+	return snd_ctl_find_id(card, &id);
 }
 
 static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
@@ -526,7 +526,7 @@ static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
 	if (numid == ID_UNKNOWN)
 		return;
 	guard(rwsem_read)(&card->controls_rwsem);
-	kctl = snd_ctl_find_numid_locked(card, numid);
+	kctl = snd_ctl_find_numid(card, numid);
 	if (!kctl)
 		return;
 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
@@ -559,7 +559,7 @@ static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer,
 	if (numid == ID_UNKNOWN)
 		return;
 	guard(rwsem_read)(&card->controls_rwsem);
-	kctl = snd_ctl_find_numid_locked(card, numid);
+	kctl = snd_ctl_find_numid(card, numid);
 	if (!kctl)
 		return;
 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
@@ -619,7 +619,7 @@ static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer,
 	if (numid == ID_UNKNOWN)
 		return;
 	guard(rwsem_read)(&card->controls_rwsem);
-	kctl = snd_ctl_find_numid_locked(card, numid);
+	kctl = snd_ctl_find_numid(card, numid);
 	if (!kctl)
 		return;
 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
@@ -656,7 +656,7 @@ static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer,
 	if (numid == ID_UNKNOWN)
 		return;
 	guard(rwsem_read)(&card->controls_rwsem);
-	kctl = snd_ctl_find_numid_locked(card, numid);
+	kctl = snd_ctl_find_numid(card, numid);
 	if (!kctl)
 		return;
 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
Signature · 1 line
2.43.0
maintainer
#6 lore raw
Quoted 37 lines · Takashi Iwai
Quoted 30 lines · Charles Keepax
Quoted 19 lines · Takashi Iwai
Quoted 17 lines · Charles Keepax
Quoted 14 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

Quoted 8 lines
Quoted 2 lines

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem.

snd_ctl_notify() doesn't take rwsem but ctl_files_rwlock. And rwlock isn't taken except for two cases, at a list traverse at enumerating from user-space and at the device disconnection, so it shouldn't race. Anything missing there...?

Quoted 4 lines

The trouble here isn't the snd_ctl_notify, the handler uses snd_soc_component_notify_control which also does a snd_soc_card_get_kcontrol, which eventually calls snd_ctl_find_id which does take the controls_rwsem.

Quoted 5 lines

OK, now it's clearer. I think we can change snd_ctl_find_*() rather to take rwlock instead of rwsem. It's only a quick look-up, hence rwlock can work well.

Totally untested patch set is below.

But why the interrupt routine does not use the cached kcontrol pointer? It looks like a bad driver design when you need to do such name (string based) lookups from the interrupt routine.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#7 lore raw
Quoted 43 lines · Jaroslav Kysela
Quoted 41 lines · Takashi Iwai
Quoted 30 lines · Charles Keepax
Quoted 19 lines · Takashi Iwai
Quoted 17 lines · Charles Keepax
Quoted 14 lines · Takashi Iwai
Quoted 7 lines · Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

That's bad, how does the mutex inversion look like? Generally speaking, a call of snd_ctl_notify() from an irq handler is a very standard procedure, and it should work without too much workaround.

Quoted 8 lines
Quoted 2 lines

SoundWire IRQs are called under the sdw_dev_lock, and then in the IRQ handler we call snd_ctl_notify which takes controls_rwsem.

snd_ctl_notify() doesn't take rwsem but ctl_files_rwlock. And rwlock isn't taken except for two cases, at a list traverse at enumerating from user-space and at the device disconnection, so it shouldn't race. Anything missing there...?

Quoted 4 lines

The trouble here isn't the snd_ctl_notify, the handler uses snd_soc_component_notify_control which also does a snd_soc_card_get_kcontrol, which eventually calls snd_ctl_find_id which does take the controls_rwsem.

OK, now it's clearer. I think we can change snd_ctl_find_*() rather to take rwlock instead of rwsem. It's only a quick look-up, hence rwlock can work well.

Quoted 5 lines
Quoted 1 line

Totally untested patch set is below.

But why the interrupt routine does not use the cached kcontrol pointer? It looks like a bad driver design when you need to do such name (string based) lookups from the interrupt routine.

Yeah, it'd be even better :)

Though, the conversion to rwlock might be still worth; it's a good cleanup to drop the *_unlocked() versions that can be messy.

Takashi

maintainer
#8 lore raw
In reply to Jaroslav Kysela
Quoted 11 lines · Jaroslav Kysela
Quoted 5 lines · Takashi Iwai

OK, now it's clearer. I think we can change snd_ctl_find_*() rather to take rwlock instead of rwsem. It's only a quick look-up, hence rwlock can work well.

Totally untested patch set is below.

But why the interrupt routine does not use the cached kcontrol pointer? It looks like a bad driver design when you need to do such name (string based) lookups from the interrupt routine.

With some of these slower buses it's not immediately obvious that it's worth the bother of caching - the overhead of doing the lookup is negligable in the overall context of handling the interrupt.

maintainer
#9 lore raw
Quoted 9 lines · Mark Brown
Quoted 7 lines · Jaroslav Kysela
Quoted 5 lines · Takashi Iwai

OK, now it's clearer. I think we can change snd_ctl_find_*() rather to take rwlock instead of rwsem. It's only a quick look-up, hence rwlock can work well.

Totally untested patch set is below.

Quoted 7 lines
Quoted 3 lines

But why the interrupt routine does not use the cached kcontrol pointer? It looks like a bad driver design when you need to do such name (string based) lookups from the interrupt routine.

With some of these slower buses it's not immediately obvious that it's worth the bother of caching - the overhead of doing the lookup is negligable in the overall context of handling the interrupt.

I don't buy that argument. We should always try to write an optimal code. Every CPU tick counts.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
maintainer
#10 lore raw
Quoted 8 lines · Jaroslav Kysela
Quoted 3 lines · Mark Brown

With some of these slower buses it's not immediately obvious that it's worth the bother of caching - the overhead of doing the lookup is negligable in the overall context of handling the interrupt.

I don't buy that argument. We should always try to write an optimal code. Every CPU tick counts.

So do the bytes of memory you'd use caching the pointers!

#11 lore raw
Quoted 4 lines · Mark Brown
Quoted 1 line · Jaroslav Kysela

On 30. 07. 24 16:44, Mark Brown wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

With some of these slower buses it's not immediately obvious that it's worth the bother of caching - the overhead of doing the lookup is negligable in the overall context of handling the interrupt.

I don't buy that argument. We should always try to write an optimal code. Every CPU tick counts.

So do the bytes of memory you'd use caching the pointers!

Adding two more works would be even more wastes, that's the beginning of the discussion :)

The problem is the order of ASoC component initialization, though; AFAIUC, the kcontrols aren't instantiated at the point of component probe, hence you can't get kcontrol objects there.

Takashi

#12 lore raw
Quoted 13 lines · Takashi Iwai
Quoted 11 lines · Mark Brown
Quoted 8 lines · Jaroslav Kysela
Quoted 3 lines · Mark Brown

With some of these slower buses it's not immediately obvious that it's worth the bother of caching - the overhead of doing the lookup is negligable in the overall context of handling the interrupt.

I don't buy that argument. We should always try to write an optimal code. Every CPU tick counts.

Quoted 9 lines
Quoted 1 line

So do the bytes of memory you'd use caching the pointers!

Adding two more works would be even more wastes, that's the beginning of the discussion :)

The problem is the order of ASoC component initialization, though; AFAIUC, the kcontrols aren't instantiated at the point of component probe, hence you can't get kcontrol objects there.

I think there probably should be some way to cache the kcontrols, I will have a look at implementing that. In general I chose the delayed work as caching didn't obviously seem worth it as Mark notes, and also the sdw_dev_lock tends to cause a lot of these inversions and the delayed work is a very safe solution, whereas caching makes me slightly nervous we will find another inversion at some point.

In general I think that is the actual problem here, the sdw_dev_lock wraps too much stuff and frequently causes these inversions. Although fixing that may not be possible and certainly requires a lot of thinking.

Thanks,
Charles