Skip to contentSkip to conversation

ASoC: wm8962: Stop IRQ from requeuing mic_work on remove

PATCH Superseded Myeonghun Pak 2 participants 3 messages Sep 23 – Sep 24

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ASoC: wm8962: Stop IRQ from requeuing mic_work on remove

wm8962_remove() cancels mic_work, but the IRQ that queues it stays registered for the I2C device. Devres frees the IRQ only after that cancel, and card unbind never unbinds the I2C client. A later MICD or MICSCD event queues the work again. The work calls snd_soc_component_read() and snd_soc_jack_report().

FLL, FIFO, and thermal handling do not use the jack, so leave the IRQ registered. Queue mic_work only while the jack pointer is set. On remove, clear that reference, synchronize_irq(), then cancel the work.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Fixes 0512615db6db ("ASoC: wm8962: Convert interrupt handler to direct regmap usage")
Assisted-by LLM

---
Changes in v2:
- Do not disable the IRQ from wm8962_i2c_remove(). Card unbind calls wm8962_remove() while the I2C client stays bound, so that disable never runs there (Charles Keepax).
- Keep the devm IRQ on the I2C device. FLL, FIFO, and thermal handling do not use the jack. Clear the jack, synchronize_irq(), then cancel mic_work so the handler cannot queue it again.

Found by inspection; I do not have the hardware, so this is not runtime tested.

 sound/soc/codecs/wm8962.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
sound/soc/codecs/wm8962.c +20 -6
index 8a9598161b35..d97289461e02 100644
@@ -3115,7 +3115,7 @@ static void wm8962_mic_work(struct work_struct *work)
 
 	wm8962->mic_status = status;
 
-	snd_soc_jack_report(wm8962->jack, status,
+	snd_soc_jack_report(READ_ONCE(wm8962->jack), status,
 			    SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
 	snd_soc_component_update_bits(component, WM8962_MICINT_SOURCE_POL,
@@ -3203,9 +3203,14 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 
 		pm_wakeup_event(dev, 300);
 
-		queue_delayed_work(system_power_efficient_wq,
-				   &wm8962->mic_work,
-				   msecs_to_jiffies(250));
+		/*
+		 * mic_work reports this jack.  Removal clears it before
+		 * synchronize_irq(), so do not queue once it is gone.
+		 */
+		if (READ_ONCE(wm8962->jack))
+			queue_delayed_work(system_power_efficient_wq,
+					   &wm8962->mic_work,
+					   msecs_to_jiffies(250));
 	}
 
 	pm_runtime_put(dev);
@@ -3232,7 +3237,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
 	int irq_mask, enable;
 
-	wm8962->jack = jack;
+	WRITE_ONCE(wm8962->jack, jack);
 	if (jack) {
 		irq_mask = 0;
 		enable = WM8962_MICDET_ENA;
@@ -3247,7 +3252,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
 			    WM8962_MICDET_ENA, enable);
 
 	/* Send an initial empty report */
-	snd_soc_jack_report(wm8962->jack, 0,
+	snd_soc_jack_report(jack, 0,
 			    SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
 	snd_soc_dapm_mutex_lock(dapm);
@@ -3594,6 +3599,15 @@ static void wm8962_remove(struct snd_soc_component *component)
 {
 	struct wm8962_priv *wm8962 = snd_soc_component_get_drvdata(component);
 
+	/*
+	 * The IRQ stays registered for the I2C device, including across
+	 * card unbind.  It is the only producer of mic_work.  Clear the
+	 * jack first so a new handler does not queue, wait out a handler
+	 * that already loaded the old pointer, then drain the work.
+	 */
+	WRITE_ONCE(wm8962->jack, NULL);
+	if (wm8962->irq)
+		synchronize_irq(wm8962->irq);
 	cancel_delayed_work_sync(&wm8962->mic_work);
 
 	wm8962_free_gpio(component);

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5

Signature · 1 line
2.53.0
#1 lore raw
Quoted 13 lines · Myeonghun Pak
sound/soc/codecs/wm8962.c +1 -1
@@ -3115,7 +3115,7 @@ static void wm8962_mic_work(struct work_struct *work)
 
 	wm8962->mic_status = status;
 
-	snd_soc_jack_report(wm8962->jack, status,
+	snd_soc_jack_report(READ_ONCE(wm8962->jack), status,
 			    SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
 	snd_soc_component_update_bits(component, WM8962_MICINT_SOURCE_POL,
@@ -3203,9 +3203,14 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 

sound/soc/codecs/wm8962.c static irqreturn_t wm8962_irq(int irq, void *data) · line 3210

Quoted 10 lines
sound/soc/codecs/wm8962.c +5 -3
 		pm_wakeup_event(dev, 300);
 
-		queue_delayed_work(system_power_efficient_wq,
-				   &wm8962->mic_work,
-				   msecs_to_jiffies(250));
+		/*
+		 * mic_work reports this jack.  Removal clears it before
+		 * synchronize_irq(), so do not queue once it is gone.
+		 */
+		if (READ_ONCE(wm8962->jack))

This feels a bit like its duplicating the mask check above, I think it would be nicer to ensure that works as intended than adding a second layer of filtering.

Quoted 25 lines
sound/soc/codecs/wm8962.c +5 -2
+			queue_delayed_work(system_power_efficient_wq,
+					   &wm8962->mic_work,
+					   msecs_to_jiffies(250));
 	}
 
 	pm_runtime_put(dev);
@@ -3232,7 +3237,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
 	int irq_mask, enable;
 
-	wm8962->jack = jack;
+	WRITE_ONCE(wm8962->jack, jack);
 	if (jack) {
 		irq_mask = 0;
 		enable = WM8962_MICDET_ENA;
@@ -3247,7 +3252,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
 			    WM8962_MICDET_ENA, enable);
 
 	/* Send an initial empty report */
-	snd_soc_jack_report(wm8962->jack, 0,
+	snd_soc_jack_report(jack, 0,
 			    SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
 	snd_soc_dapm_mutex_lock(dapm);

sound/soc/codecs/wm8962.c static void wm8962_remove(struct snd_soc_component *component) · line 3608

Quoted 10 lines
sound/soc/codecs/wm8962.c +7 -0
@@ -3594,6 +3599,15 @@ static void wm8962_remove(struct snd_soc_component *component)
 {
 	struct wm8962_priv *wm8962 = snd_soc_component_get_drvdata(component);
 
+	/*
+	 * The IRQ stays registered for the I2C device, including across
+	 * card unbind.  It is the only producer of mic_work.  Clear the
+	 * jack first so a new handler does not queue, wait out a handler
+	 * that already loaded the old pointer, then drain the work.
+	 */
+	WRITE_ONCE(wm8962->jack, NULL);

So probably calling wm8962_mic_detect() here. Although granted that probably needs some small fixups as there are a couple races, probably also makes sense to move the mic_work sync in there too.

Quoted 8 lines
sound/soc/codecs/wm8962.c +2 -0
+	if (wm8962->irq)
+		synchronize_irq(wm8962->irq);
 	cancel_delayed_work_sync(&wm8962->mic_work);
 
 	wm8962_free_gpio(component);

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5

Signature · 1 line
2.53.0

Thanks,
Charles

#2 lore raw

Thanks, Charles. You're right that the jack check duplicates the existing IRQ mask handling.

I'll update wm8962_mic_detect() to mask the microphone interrupts, wait for any running IRQ handler, and cancel mic_work before changing the jack pointer. I'll call it from wm8962_remove() and keep the IRQ registered for the other interrupt sources.

I'll send v3 with these changes shortly.

Thanks,
Myeonghun

2026년 9월 23일 (수) 오전 5:33, Charles Keepax <[email protected]>님이 작성:

Quoted 82 lines · Myeonghun Pak
Quoted 13 lines
sound/soc/codecs/wm8962.c +1 -1
@@ -3115,7 +3115,7 @@ static void wm8962_mic_work(struct work_struct *work)
 
      wm8962->mic_status = status;
 
-     snd_soc_jack_report(wm8962->jack, status,
+     snd_soc_jack_report(READ_ONCE(wm8962->jack), status,
                          SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
      snd_soc_component_update_bits(component, WM8962_MICINT_SOURCE_POL,
@@ -3203,9 +3203,14 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 

sound/soc/codecs/wm8962.c static irqreturn_t wm8962_irq(int irq, void *data) · line 3210

Quoted 10 lines
sound/soc/codecs/wm8962.c +5 -3
              pm_wakeup_event(dev, 300);
 
-             queue_delayed_work(system_power_efficient_wq,
-                                &wm8962->mic_work,
-                                msecs_to_jiffies(250));
+             /*
+              * mic_work reports this jack.  Removal clears it before
+              * synchronize_irq(), so do not queue once it is gone.
+              */
+             if (READ_ONCE(wm8962->jack))

This feels a bit like its duplicating the mask check above, I think it would be nicer to ensure that works as intended than adding a second layer of filtering.

Quoted 25 lines
sound/soc/codecs/wm8962.c +5 -2
+                     queue_delayed_work(system_power_efficient_wq,
+                                        &wm8962->mic_work,
+                                        msecs_to_jiffies(250));
      }
 
      pm_runtime_put(dev);
@@ -3232,7 +3237,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
      struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
      int irq_mask, enable;
 
-     wm8962->jack = jack;
+     WRITE_ONCE(wm8962->jack, jack);
      if (jack) {
              irq_mask = 0;
              enable = WM8962_MICDET_ENA;
@@ -3247,7 +3252,7 @@ int wm8962_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *
                          WM8962_MICDET_ENA, enable);
 
      /* Send an initial empty report */
-     snd_soc_jack_report(wm8962->jack, 0,
+     snd_soc_jack_report(jack, 0,
                          SND_JACK_MICROPHONE | SND_JACK_BTN_0);
 
      snd_soc_dapm_mutex_lock(dapm);

sound/soc/codecs/wm8962.c static void wm8962_remove(struct snd_soc_component *component) · line 3608

Quoted 10 lines
sound/soc/codecs/wm8962.c +7 -0
@@ -3594,6 +3599,15 @@ static void wm8962_remove(struct snd_soc_component *component)
 {
      struct wm8962_priv *wm8962 = snd_soc_component_get_drvdata(component);
 
+     /*
+      * The IRQ stays registered for the I2C device, including across
+      * card unbind.  It is the only producer of mic_work.  Clear the
+      * jack first so a new handler does not queue, wait out a handler
+      * that already loaded the old pointer, then drain the work.
+      */
+     WRITE_ONCE(wm8962->jack, NULL);

So probably calling wm8962_mic_detect() here. Although granted that probably needs some small fixups as there are a couple races, probably also makes sense to move the mic_work sync in there too.

Quoted 8 lines
sound/soc/codecs/wm8962.c +2 -0
+     if (wm8962->irq)
+             synchronize_irq(wm8962->irq);
      cancel_delayed_work_sync(&wm8962->mic_work);
 
      wm8962_free_gpio(component);

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5 --
2.53.0

Thanks,
Charles