mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: mpu401: shut down timer before freeing MPU
@ 2026-09-27  9:38 Runyu Xiao
  2026-09-28 12:24 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Runyu Xiao @ 2026-09-27  9:38 UTC (permalink / raw)
  To: Jaroslav Kysela
  Cc: Takashi Iwai, linux-sound, linux-kernel, stable, Runyu Xiao, Jianhao Xu

When an MPU401 interface has no usable IRQ, the driver polls raw-MIDI
input and output with a timer. During raw-MIDI teardown, the timer callback
can already be running when snd_mpu401_uart_remove_timer() clears the last
mode bit and calls timer_delete(). That API neither waits for a running
callback nor prevents it from rearming the timer, and free_irq() does not
synchronize timer callbacks. snd_mpu401_uart_free() can therefore release
mpu while the timer callback still uses it.

Initialize the timer once when the MPU object is created and only rearm it
while a timer mode is active. Shut it down synchronously after freeing the
IRQ so an in-flight callback completes before mpu and its resources are
released.


Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
---
 sound/drivers/mpu401/mpu401_uart.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/sound/drivers/mpu401/mpu401_uart.c b/sound/drivers/mpu401/mpu401_uart.c
index 6de03d72a908..fcf2b686f9f5 100644
--- a/sound/drivers/mpu401/mpu401_uart.c
+++ b/sound/drivers/mpu401/mpu401_uart.c
@@ -155,20 +155,20 @@ static void snd_mpu401_uart_timer(struct timer_list *t)
 
 	scoped_guard(spinlock_irqsave, &mpu->timer_lock) {
 		/*mpu->mode |= MPU401_MODE_TIMER;*/
-		mod_timer(&mpu->timer,  1 + jiffies);
+		if (mpu->timer_invoked)
+			mod_timer(&mpu->timer,  1 + jiffies);
 	}
 	if (mpu->rmidi)
 		_snd_mpu401_uart_interrupt(mpu);
 }
 
 /*
- * initialize the timer callback if not programmed yet
+ * arm the timer if it is not already active
  */
 static void snd_mpu401_uart_add_timer (struct snd_mpu401 *mpu, int input)
 {
 	guard(spinlock_irqsave)(&mpu->timer_lock);
 	if (mpu->timer_invoked == 0) {
-		timer_setup(&mpu->timer, snd_mpu401_uart_timer, 0);
 		mod_timer(&mpu->timer, 1 + jiffies);
 	} 
 	mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER :
@@ -474,6 +474,7 @@ static void snd_mpu401_uart_free(struct snd_rawmidi *rmidi)
 	struct snd_mpu401 *mpu = rmidi->private_data;
 	if (mpu->irq >= 0)
 		free_irq(mpu->irq, (void *) mpu);
+	timer_shutdown_sync(&mpu->timer);
 	release_and_free_resource(mpu->res);
 	kfree(mpu);
 }
@@ -528,6 +529,7 @@ int snd_mpu401_uart_new(struct snd_card *card, int device,
 	spin_lock_init(&mpu->input_lock);
 	spin_lock_init(&mpu->output_lock);
 	spin_lock_init(&mpu->timer_lock);
+	timer_setup(&mpu->timer, snd_mpu401_uart_timer, 0);
 	mpu->hardware = hardware;
 	mpu->irq = -1;
 	mpu->rmidi = rmidi;
--
2.34.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: mpu401: shut down timer before freeing MPU
  2026-09-27  9:38 [PATCH] ALSA: mpu401: shut down timer before freeing MPU Runyu Xiao
@ 2026-09-28 12:24 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2026-09-28 12:24 UTC (permalink / raw)
  To: Runyu Xiao
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, linux-kernel, stable,
	Jianhao Xu

On Sun, 27 Sep 2026 11:38:09 +0200,
Runyu Xiao wrote:
> 
> When an MPU401 interface has no usable IRQ, the driver polls raw-MIDI
> input and output with a timer. During raw-MIDI teardown, the timer callback
> can already be running when snd_mpu401_uart_remove_timer() clears the last
> mode bit and calls timer_delete(). That API neither waits for a running
> callback nor prevents it from rearming the timer, and free_irq() does not
> synchronize timer callbacks. snd_mpu401_uart_free() can therefore release
> mpu while the timer callback still uses it.
> 
> Initialize the timer once when the MPU object is created and only rearm it
> while a timer mode is active. Shut it down synchronously after freeing the
> IRQ so an in-flight callback completes before mpu and its resources are
> released.
> 
> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>

Applied to for-next branch.  Thanks.


Takashi

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-28 12:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-27  9:38 [PATCH] ALSA: mpu401: shut down timer before freeing MPU Runyu Xiao
2026-09-28 12:24 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®