mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: seq: Wait for one RCU grace period when deleting all ports
@ 2026-10-10 22:25 Ali Ahmet Memiş
  2026-10-11  8:49 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Ali Ahmet Memiş @ 2026-10-10 22:25 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

Since the port list was converted to RCU, snd_seq_delete_all_ports()
calls synchronize_rcu() once per port under register_mutex. Closing a
client with 254 ports now takes about 1.6s and every other client
open or close waits for it.

Unlink all ports first, wait for a single grace period and then free
them.

Fixes: 4c252fbc06d6 ("ALSA: seq: Use RCU for the client port list")
Signed-off-by: Ali Ahmet Memiş <aliamemis@disroot.org>
---
Tested in QEMU on v7.3 with lockdep and KASAN.  Closing a client
with 254 ports went from ~1.6s to ~0.02s, and a second client doing
open/close in a loop no longer stalls.  Closing the client while other
clients send to and query its ports ran 100 rounds without reports.

 sound/core/seq/seq_ports.c | 41 +++++++++++++++++++++++++-------------
 sound/core/seq/seq_ports.h |  1 +
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/sound/core/seq/seq_ports.c b/sound/core/seq/seq_ports.c
index eb67eb0ee..b582d0344 100644
--- a/sound/core/seq/seq_ports.c
+++ b/sound/core/seq/seq_ports.c
@@ -248,17 +248,9 @@ static void clear_subscriber_list(struct snd_seq_client *client,
 }
 
 /* delete port data */
-static int port_delete(struct snd_seq_client *client,
-		       struct snd_seq_client_port *port)
+static void port_free(struct snd_seq_client *client,
+		      struct snd_seq_client_port *port)
 {
-	/* set closing flag and wait for all port access are gone */
-	port->closing = 1;
-	/* the port has already been unlinked from the client's port list;
-	 * wait for a grace period so that RCU readers still traversing the
-	 * list can no longer take a new use_lock reference, then drain the
-	 * outstanding references before freeing
-	 */
-	synchronize_rcu();
 	snd_use_lock_sync(&port->use_lock);
 
 	/* clear subscribers info */
@@ -272,6 +264,20 @@ static int port_delete(struct snd_seq_client *client,
 	snd_BUG_ON(port->c_dest.count != 0);
 
 	kfree(port);
+}
+
+static int port_delete(struct snd_seq_client *client,
+		       struct snd_seq_client_port *port)
+{
+	/* set closing flag and wait for all port access are gone */
+	port->closing = 1;
+	/* the port has already been unlinked from the client's port list;
+	 * wait for a grace period so that RCU readers still traversing the
+	 * list can no longer take a new use_lock reference, then drain the
+	 * outstanding references before freeing
+	 */
+	synchronize_rcu();
+	port_free(client, port);
 	return 0;
 }
 
@@ -302,18 +308,25 @@ int snd_seq_delete_port(struct snd_seq_client *client, int port)
 int snd_seq_delete_all_ports(struct snd_seq_client *client)
 {
 	struct snd_seq_client_port *port, *tmp;
+	LIST_HEAD(deleted);
 
-	/* unlink and delete each port; port_delete() waits for an RCU grace
-	 * period before draining the port, so concurrent lockless readers can
-	 * no longer take a new use_lock reference on it
+	/* unlink all ports first and wait for a single RCU grace period
+	 * before draining and freeing them, so concurrent lockless readers
+	 * can no longer take a new use_lock reference on any of them
 	 */
 	guard(mutex)(&client->ports_mutex);
 	list_for_each_entry_safe(port, tmp, &client->ports_list_head, list) {
 		list_del_rcu(&port->list);
 		client->num_ports--;
+		port->closing = 1;
+		list_add_tail(&port->delete_list, &deleted);
 		snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
-		port_delete(client, port);
 	}
+	if (list_empty(&deleted))
+		return 0;
+	synchronize_rcu();
+	list_for_each_entry_safe(port, tmp, &deleted, delete_list)
+		port_free(client, port);
 	return 0;
 }
 
diff --git a/sound/core/seq/seq_ports.h b/sound/core/seq/seq_ports.h
index 12ad86bf1..ef1787d6c 100644
--- a/sound/core/seq/seq_ports.h
+++ b/sound/core/seq/seq_ports.h
@@ -49,6 +49,7 @@ struct snd_seq_client_port {
 	struct module *owner;		/* owner of this port */
 	char name[64];			/* port name */	
 	struct list_head list;		/* port list */
+	struct list_head delete_list;	/* used by snd_seq_delete_all_ports() */
 	snd_use_lock_t use_lock;
 
 	/* subscribers */
-- 
2.55.0


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

* Re: [PATCH] ALSA: seq: Wait for one RCU grace period when deleting all ports
  2026-10-10 22:25 [PATCH] ALSA: seq: Wait for one RCU grace period when deleting all ports Ali Ahmet Memiş
@ 2026-10-11  8:49 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2026-10-11  8:49 UTC (permalink / raw)
  To: Ali Ahmet Memiş
  Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel

On Sun, 11 Oct 2026 00:25:40 +0200,
Ali Ahmet Memiş wrote:
> 
> Since the port list was converted to RCU, snd_seq_delete_all_ports()
> calls synchronize_rcu() once per port under register_mutex. Closing a
> client with 254 ports now takes about 1.6s and every other client
> open or close waits for it.
> 
> Unlink all ports first, wait for a single grace period and then free
> them.
> 
> Fixes: 4c252fbc06d6 ("ALSA: seq: Use RCU for the client port list")
> Signed-off-by: Ali Ahmet Memiş <aliamemis@disroot.org>
> ---
> Tested in QEMU on v7.3 with lockdep and KASAN.  Closing a client
> with 254 ports went from ~1.6s to ~0.02s, and a second client doing
> open/close in a loop no longer stalls.  Closing the client while other
> clients send to and query its ports ran 100 rounds without reports.

Applied now to for-next branch.
Sounds like a good performance improvement.


thanks,

Takashi

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

end of thread, other threads:[~2026-10-11  8:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-10 22:25 [PATCH] ALSA: seq: Wait for one RCU grace period when deleting all ports Ali Ahmet Memiş
2026-10-11  8:49 ` 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®