mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ali Ahmet Memiş" <aliamemis@disroot.org>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: seq: Wait for one RCU grace period when deleting all ports
Date: Sun, 11 Oct 2026 01:25:40 +0300	[thread overview]
Message-ID: <20261010222543.1470723-1-aliamemis@disroot.org> (raw)

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


             reply	other threads:[~2026-10-10 22:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-10 22:25 Ali Ahmet Memiş [this message]
2026-10-11  8:49 ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261010222543.1470723-1-aliamemis@disroot.org \
    --to=aliamemis@disroot.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®