Skip to contentSkip to conversation

ALSA: seq: Serialize UMP output teardown with event_input

PATCH Superseded Zhang Cen 2 participants 3 messages May 19 – May 20 Sound

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zhang Cen

author
#0 lore raw

PATCH 1/1 ALSA: seq: Serialize UMP output teardown with event_input

seq_ump_process_event() borrows client->out_rfile.output without synchronizing with the first-open / last-close transition in seq_ump_client_open() and seq_ump_client_close().

The last output unuse can therefore drop opened[STR_OUT] to zero and release client->out_rfile while an in-flight event_input callback is still inside snd_rawmidi_kernel_write(). That leaves the rawmidi substream runtime exposed to teardown before the write path has taken its own buffer reference.

Add a small per-client output gate. event_input now claims the current output stream and counts in-flight writers before using it, while the last close clears the ready bit, waits for those writers to drain, and only then calls snd_rawmidi_kernel_release().

The buggy scenario involves two paths, with each column showing the order within that path:

path A label: event_input path path B label: last unuse path
1. seq_ump_process_event() claims 1. seq_ump_client_close()
client->out_rfile.output and drops opened[STR_OUT] to zero.
starts snd_rawmidi_kernel_write(). 2. The close side withdraws the
2. The write still depends on the output from new writers.
borrowed substream runtime. 3. It waits for the in-flight
3. The write drops its output claim event_input writer to finish.
after the rawmidi write completes. 4. It releases client->out_rfile.

This keeps the output substream and runtime pinned for the whole event_input write.

KASAN report as below:
KASAN slab-use-after-free in snd_rawmidi_kernel_write1()

RIP: 0033:0x7f5528af837f
Read of size 8
Call trace:
  dump_stack_lvl() (?:?)
  print_report() (?:?)
  srso_alias_return_thunk() (arch/x86/include/asm/nospec-branch.h:373)
  __virt_addr_valid() (?:?)
  kasan_complete_mode_report_info() (?:?)
  kasan_report() (?:?)
  snd_rawmidi_kernel_write1() (sound/core/rawmidi.c:1518)
  __asan_load8() (?:?)
  update_stack_state() (arch/x86/kernel/unwind_frame.c:196)
  snd_rawmidi_kernel_write() (?:?)
  seq_ump_process_event() (sound/core/seq/seq_ump_client.c:82)
  __snd_seq_deliver_single_event() (?:?)
  snd_seq_deliver_from_ump() (?:?)
  lock_acquire() (?:?)
  find_held_lock() (kernel/locking/lockdep.c:5340)
  snd_seq_port_use_ptr() (sound/core/seq/seq_ports.c:44)
  __kasan_check_write() (?:?)
  do_raw_read_unlock() (kernel/locking/spinlock_debug.c:178)
  _raw_read_unlock() (kernel/locking/spinlock.c:258)
  snd_seq_deliver_single_event() (sound/core/seq/seq_clientmgr.c:71)
  snd_seq_deliver_event() (sound/core/seq/seq_clientmgr.c:829)
  snd_seq_client_enqueue_event() (sound/core/seq/seq_clientmgr.c:938)
  snd_seq_write() (sound/core/seq/seq_clientmgr.c:1026)
  apparmor_file_permission() (security/apparmor/lsm.c:546)
  security_file_permission() (?:?)
  vfs_write() (fs/read_write.c:666)
  __fget_files() (fs/file.c:1065)
  lock_release() (kernel/locking/lockdep.c:5875)
  __rcu_read_unlock() (?:?)
  ksys_write() (fs/read_write.c:727)
  rcu_is_watching() (?:?)
  __x64_sys_write() (?:?)
  x64_sys_call() (arch/x86/entry/syscall_64.c:35)
  do_syscall_64() (arch/x86/entry/syscall_64.c:87)
  entry_SYSCALL_64_after_hwframe() (?:?)
Fixes 81fd444aa371 ("ALSA: seq: Bind UMP device")
---
 sound/core/seq/seq_ump_client.c | 69 ++++++++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 6 deletions(-)
sound/core/seq/seq_ump_client.c +63 -6
index 9079ccfdc8666..0f221beddb8da 100644
@@ -6,6 +6,7 @@
 #include <linux/errno.h>
 #include <linux/mutex.h>
 #include <linux/string.h>
+#include <linux/wait.h>
 #include <linux/module.h>
 #include <asm/byteorder.h>
 #include <sound/core.h>
@@ -38,6 +39,10 @@ struct seq_ump_client {
 	int seq_client;			/* sequencer client id */
 	int opened[2];			/* current opens for each direction */
 	struct snd_rawmidi_file out_rfile; /* rawmidi for output */
+	spinlock_t output_lock;		/* protects output lifetime state */
+	wait_queue_head_t output_sleep; /* waits for in-flight writers */
+	unsigned int output_pending;	/* active event_input writers */
+	bool output_ready;		/* rawmidi output is available */
 	struct seq_ump_input_buffer input; /* input parser context */
 	void *ump_info[SNDRV_UMP_MAX_BLOCKS + 1]; /* shadow of seq client ump_info */
 	struct work_struct group_notify_work; /* FB change notification */
@@ -56,6 +61,50 @@ static unsigned char ump_group_to_seq_port(unsigned char group)
 	return group + 1;
 }
 
+static struct snd_rawmidi_substream *
+seq_ump_claim_output(struct seq_ump_client *client)
+{
+	struct snd_rawmidi_substream *substream = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	if (client->output_ready) {
+		substream = client->out_rfile.output;
+		if (substream)
+			client->output_pending++;
+	}
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	return substream;
+}
+
+static void seq_ump_release_output(struct seq_ump_client *client)
+{
+	unsigned long flags;
+	bool wake = false;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	if (WARN_ON(!client->output_pending)) {
+		spin_unlock_irqrestore(&client->output_lock, flags);
+		return;
+	}
+	if (!--client->output_pending && !client->output_ready)
+		wake = true;
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	if (wake)
+		wake_up(&client->output_sleep);
+}
+
+static bool seq_ump_output_idle(struct seq_ump_client *client)
+{
+	unsigned long flags;
+	bool idle;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	idle = !client->output_pending;
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	return idle;
+}
+
 /* process the incoming rawmidi stream */
 static void seq_ump_input_receive(struct snd_ump_endpoint *ump,
 				  const u32 *val, int words)
@@ -88,9 +137,6 @@ static int seq_ump_process_event(struct snd_seq_event *ev, int direct,
 	unsigned char type;
 	int len;
 
-	substream = client->out_rfile.output;
-	if (!substream)
-		return -ENODEV;
 	if (!snd_seq_ev_is_ump(ev))
 		return 0; /* invalid event, skip */
 	ump_ev = (struct snd_seq_ump_event *)ev;
@@ -98,7 +144,11 @@ static int seq_ump_process_event(struct snd_seq_event *ev, int direct,
 	len = ump_packet_words[type];
 	if (len > 4)
 		return 0; // invalid - skip
+	substream = seq_ump_claim_output(client);
+	if (!substream)
+		return -ENODEV;
 	snd_rawmidi_kernel_write(substream, ev->data.raw8.d, len << 2);
+	seq_ump_release_output(client);
 	return 0;
 }
 
@@ -116,6 +166,8 @@ static int seq_ump_client_open(struct seq_ump_client *client, int dir)
 					      &client->out_rfile);
 		if (err < 0)
 			return err;
+		guard(spinlock_irq)(&client->output_lock);
+		client->output_ready = true;
 	}
 	client->opened[dir]++;
 	return 0;
@@ -127,9 +179,12 @@ static int seq_ump_client_close(struct seq_ump_client *client, int dir)
 	struct snd_ump_endpoint *ump = client->ump;
 
 	guard(mutex)(&ump->open_mutex);
-	if (!--client->opened[dir])
-		if (dir == STR_OUT)
-			snd_rawmidi_kernel_release(&client->out_rfile);
+	if (!--client->opened[dir] && dir == STR_OUT) {
+		scoped_guard(spinlock_irq, &client->output_lock)
+			client->output_ready = false;
+		wait_event(client->output_sleep, seq_ump_output_idle(client));
+		snd_rawmidi_kernel_release(&client->out_rfile);
+	}
 	return 0;
 }
 
@@ -466,6 +521,8 @@ static int snd_seq_ump_probe(struct snd_seq_device *dev)
 		return -ENOMEM;
 
 	INIT_WORK(&client->group_notify_work, handle_group_notify);
+	spin_lock_init(&client->output_lock);
+	init_waitqueue_head(&client->output_sleep);
 	client->ump = ump;
 
 	client->seq_client =
Signature · 1 line
2.43.0
#1 lore raw
Quoted 72 lines · Zhang Cen

seq_ump_process_event() borrows client->out_rfile.output without synchronizing with the first-open / last-close transition in seq_ump_client_open() and seq_ump_client_close().

The last output unuse can therefore drop opened[STR_OUT] to zero and release client->out_rfile while an in-flight event_input callback is still inside snd_rawmidi_kernel_write(). That leaves the rawmidi substream runtime exposed to teardown before the write path has taken its own buffer reference.

Add a small per-client output gate. event_input now claims the current output stream and counts in-flight writers before using it, while the last close clears the ready bit, waits for those writers to drain, and only then calls snd_rawmidi_kernel_release().

The buggy scenario involves two paths, with each column showing the order within that path:

path A label: event_input path path B label: last unuse path
1. seq_ump_process_event() claims 1. seq_ump_client_close()
client->out_rfile.output and drops opened[STR_OUT] to zero.
starts snd_rawmidi_kernel_write(). 2. The close side withdraws the
2. The write still depends on the output from new writers.
borrowed substream runtime. 3. It waits for the in-flight
3. The write drops its output claim event_input writer to finish.
after the rawmidi write completes. 4. It releases client->out_rfile.

This keeps the output substream and runtime pinned for the whole event_input write.

KASAN report as below:
KASAN slab-use-after-free in snd_rawmidi_kernel_write1()

RIP: 0033:0x7f5528af837f
Read of size 8
Call trace:
  dump_stack_lvl() (?:?)
  print_report() (?:?)
  srso_alias_return_thunk() (arch/x86/include/asm/nospec-branch.h:373)
  __virt_addr_valid() (?:?)
  kasan_complete_mode_report_info() (?:?)
  kasan_report() (?:?)
  snd_rawmidi_kernel_write1() (sound/core/rawmidi.c:1518)
  __asan_load8() (?:?)
  update_stack_state() (arch/x86/kernel/unwind_frame.c:196)
  snd_rawmidi_kernel_write() (?:?)
  seq_ump_process_event() (sound/core/seq/seq_ump_client.c:82)
  __snd_seq_deliver_single_event() (?:?)
  snd_seq_deliver_from_ump() (?:?)
  lock_acquire() (?:?)
  find_held_lock() (kernel/locking/lockdep.c:5340)
  snd_seq_port_use_ptr() (sound/core/seq/seq_ports.c:44)
  __kasan_check_write() (?:?)
  do_raw_read_unlock() (kernel/locking/spinlock_debug.c:178)
  _raw_read_unlock() (kernel/locking/spinlock.c:258)
  snd_seq_deliver_single_event() (sound/core/seq/seq_clientmgr.c:71)
  snd_seq_deliver_event() (sound/core/seq/seq_clientmgr.c:829)
  snd_seq_client_enqueue_event() (sound/core/seq/seq_clientmgr.c:938)
  snd_seq_write() (sound/core/seq/seq_clientmgr.c:1026)
  apparmor_file_permission() (security/apparmor/lsm.c:546)
  security_file_permission() (?:?)
  vfs_write() (fs/read_write.c:666)
  __fget_files() (fs/file.c:1065)
  lock_release() (kernel/locking/lockdep.c:5875)
  __rcu_read_unlock() (?:?)
  ksys_write() (fs/read_write.c:727)
  rcu_is_watching() (?:?)
  __x64_sys_write() (?:?)
  x64_sys_call() (arch/x86/entry/syscall_64.c:35)
  do_syscall_64() (arch/x86/entry/syscall_64.c:87)
  entry_SYSCALL_64_after_hwframe() (?:?)
Quoted 2 lines
Fixes 81fd444aa371 ("ALSA: seq: Bind UMP device")

Thanks for the report and the patch.

Through a quick glance, I guess your analysis is right, but I also wonder whether it could be simpler just wrap the whole seq_ump_process_event() with lock. (It could be spinlock, but rwlock might be safer in this case as it might be called recursively.) Then seq_ump_client_close() takes the (write) lock and reset client->out_rfile.output to NULL, followed by the call of snd_rawmidi_kernel_close() outside the lock.

Takashi

Quoted 136 lines
---
 sound/core/seq/seq_ump_client.c | 69 ++++++++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 6 deletions(-)
sound/core/seq/seq_ump_client.c +63 -6
index 9079ccfdc8666..0f221beddb8da 100644
@@ -6,6 +6,7 @@
 #include <linux/errno.h>
 #include <linux/mutex.h>
 #include <linux/string.h>
+#include <linux/wait.h>
 #include <linux/module.h>
 #include <asm/byteorder.h>
 #include <sound/core.h>
@@ -38,6 +39,10 @@ struct seq_ump_client {
 	int seq_client;			/* sequencer client id */
 	int opened[2];			/* current opens for each direction */
 	struct snd_rawmidi_file out_rfile; /* rawmidi for output */
+	spinlock_t output_lock;		/* protects output lifetime state */
+	wait_queue_head_t output_sleep; /* waits for in-flight writers */
+	unsigned int output_pending;	/* active event_input writers */
+	bool output_ready;		/* rawmidi output is available */
 	struct seq_ump_input_buffer input; /* input parser context */
 	void *ump_info[SNDRV_UMP_MAX_BLOCKS + 1]; /* shadow of seq client ump_info */
 	struct work_struct group_notify_work; /* FB change notification */
@@ -56,6 +61,50 @@ static unsigned char ump_group_to_seq_port(unsigned char group)
 	return group + 1;
 }
 
+static struct snd_rawmidi_substream *
+seq_ump_claim_output(struct seq_ump_client *client)
+{
+	struct snd_rawmidi_substream *substream = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	if (client->output_ready) {
+		substream = client->out_rfile.output;
+		if (substream)
+			client->output_pending++;
+	}
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	return substream;
+}
+
+static void seq_ump_release_output(struct seq_ump_client *client)
+{
+	unsigned long flags;
+	bool wake = false;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	if (WARN_ON(!client->output_pending)) {
+		spin_unlock_irqrestore(&client->output_lock, flags);
+		return;
+	}
+	if (!--client->output_pending && !client->output_ready)
+		wake = true;
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	if (wake)
+		wake_up(&client->output_sleep);
+}
+
+static bool seq_ump_output_idle(struct seq_ump_client *client)
+{
+	unsigned long flags;
+	bool idle;
+
+	spin_lock_irqsave(&client->output_lock, flags);
+	idle = !client->output_pending;
+	spin_unlock_irqrestore(&client->output_lock, flags);
+	return idle;
+}
+
 /* process the incoming rawmidi stream */
 static void seq_ump_input_receive(struct snd_ump_endpoint *ump,
 				  const u32 *val, int words)
@@ -88,9 +137,6 @@ static int seq_ump_process_event(struct snd_seq_event *ev, int direct,
 	unsigned char type;
 	int len;
 
-	substream = client->out_rfile.output;
-	if (!substream)
-		return -ENODEV;
 	if (!snd_seq_ev_is_ump(ev))
 		return 0; /* invalid event, skip */
 	ump_ev = (struct snd_seq_ump_event *)ev;
@@ -98,7 +144,11 @@ static int seq_ump_process_event(struct snd_seq_event *ev, int direct,
 	len = ump_packet_words[type];
 	if (len > 4)
 		return 0; // invalid - skip
+	substream = seq_ump_claim_output(client);
+	if (!substream)
+		return -ENODEV;
 	snd_rawmidi_kernel_write(substream, ev->data.raw8.d, len << 2);
+	seq_ump_release_output(client);
 	return 0;
 }
 
@@ -116,6 +166,8 @@ static int seq_ump_client_open(struct seq_ump_client *client, int dir)
 					      &client->out_rfile);
 		if (err < 0)
 			return err;
+		guard(spinlock_irq)(&client->output_lock);
+		client->output_ready = true;
 	}
 	client->opened[dir]++;
 	return 0;
@@ -127,9 +179,12 @@ static int seq_ump_client_close(struct seq_ump_client *client, int dir)
 	struct snd_ump_endpoint *ump = client->ump;
 
 	guard(mutex)(&ump->open_mutex);
-	if (!--client->opened[dir])
-		if (dir == STR_OUT)
-			snd_rawmidi_kernel_release(&client->out_rfile);
+	if (!--client->opened[dir] && dir == STR_OUT) {
+		scoped_guard(spinlock_irq, &client->output_lock)
+			client->output_ready = false;
+		wait_event(client->output_sleep, seq_ump_output_idle(client));
+		snd_rawmidi_kernel_release(&client->out_rfile);
+	}
 	return 0;
 }
 
@@ -466,6 +521,8 @@ static int snd_seq_ump_probe(struct snd_seq_device *dev)
 		return -ENOMEM;
 
 	INIT_WORK(&client->group_notify_work, handle_group_notify);
+	spin_lock_init(&client->output_lock);
+	init_waitqueue_head(&client->output_sleep);
 	client->ump = ump;
 
 	client->seq_client =
Signature · 1 line
2.43.0
author
#2 lore raw

Hi Takashi,

Quoted 9 lines · Takashi Iwai <[email protected]> 于2026年5月20日周三 00:30写道:

Thanks for the report and the patch.

Through a quick glance, I guess your analysis is right, but I also wonder whether it could be simpler just wrap the whole seq_ump_process_event() with lock. (It could be spinlock, but rwlock might be safer in this case as it might be called recursively.) Then seq_ump_client_close() takes the (write) lock and reset client->out_rfile.output to NULL, followed by the call of snd_rawmidi_kernel_close() outside the lock.

Thanks, I looked at the lifetime rule again and I think the rwlock version is a better fit than my v1 ready/pending/waitqueue scheme.

I'll send v2 with this approach.

Best regards,
Zhang Cen