From nobody Tue Sep 29 02:02:39 2026 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E032478E5E; Thu, 13 Aug 2026 14:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786632159; cv=none; b=f/LPhL+jw22BATHpZMb8VNMFP5q58MdE64eI3M2KmhwetaUtTX8IRC5VNqjekhfCkUUmiCsrfKimznEp++GoQumi/4peaJJtEv8a8MKDq4oiyH9vVJ72A0boTS0R79skuZNFrPj+oR8dOnBdwUTRzgpd9JbZ3KNKLE5FTNszLaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786632159; c=relaxed/simple; bh=iVVRZxgjH8ohHuof150lDEJguRDg3Ixea+Gwa0UGNRQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=se92wVFzSO5NACieUyU+edIfs5/58NlWXH+LatF8gtDJH1Q6NZeMDt2osEnD6IpSn3F/IvrRhOueiHRQx5gNwpBhogV3Bs2aLv+poE3qwhyc/FdQmsiFr0JzBarPKuD3lDw7sU1xP15NziawSTIbT06GrvLnncUR87DKo3gsapY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=OyUnkl+i; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=UzmRE/jO; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=H7NGlCYH; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=hbRoWB0N; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="OyUnkl+i"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="UzmRE/jO"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="H7NGlCYH"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="hbRoWB0N" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 94E5382972; Thu, 13 Aug 2026 14:42:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1786632151; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=p/+HfkjhL9H9LSp3s+Sq6R2s3GZk9sn4kwHvhmRdri4=; b=OyUnkl+iTTl7inXh4D6Qfpr27Hn8HL5dkEWPl/jb6r8bpVdp/S1euzO40sCK7v9UehowSX wMwTvKIJFkSCbWjPYJd2rfQ+RIYotwWVyPx5L14Mqu3OA+2nQARf9bpeWuokeed8Ls5qKG dJagGiBFLFkJ3TKOi6TD2PUGvAjbD/A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1786632151; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=p/+HfkjhL9H9LSp3s+Sq6R2s3GZk9sn4kwHvhmRdri4=; b=UzmRE/jOpoWC3vFy3NTsmsLxzF1FeZtxRY7+ESVjflwz6k1/oIlqMrhdRgJnBY1qHnesr1 YXyZ9oqGsMvFpDCA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=H7NGlCYH; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=hbRoWB0N DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1786632147; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=p/+HfkjhL9H9LSp3s+Sq6R2s3GZk9sn4kwHvhmRdri4=; b=H7NGlCYHeaSV6z+aOD+9NFhRLUKxcZYN4vuc0WHeCJ5PfzbaIB+SAqx/W6R42vmukFAi6n JcdsFysyOn4GuuxNrKGYQybB5zIB49jtxRJ7pXilMkxaJksZDCMe5rIup33IbVnD4MsIGP 7W4fQXnXKfwOCkXNtbacnZpf4hojyIs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1786632147; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=p/+HfkjhL9H9LSp3s+Sq6R2s3GZk9sn4kwHvhmRdri4=; b=hbRoWB0NIHHkhXy1b//kzPqLPUYcTOtNjX5dJ0Zoctc/K0iJjKoOuqag1IOlD7qsZAXkUS DFZfmoaNk5mBfEBA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 75E3077E83; Thu, 13 Aug 2026 14:42:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id D6peG9PXfWoEUQAAD6G6ig (envelope-from ); Thu, 13 Aug 2026 14:42:27 +0000 From: Takashi Iwai To: linux-sound@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] ALSA: seq: midi: Optimize event_input locking with RCU Date: Thu, 13 Aug 2026 16:42:16 +0200 Message-ID: <20260813144224.753399-1-tiwai@suse.de> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -3.01 X-Spam-Level: X-Rspamd-Action: no action X-Rspamd-Queue-Id: 94E5382972 X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:mid,suse.de:email,suse.de:dkim]; ARC_NA(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCPT_COUNT_TWO(0.00)[2]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO Content-Type: text/plain; charset="utf-8" The recent fix for serializing the output teardown introduced a spinlock invocation at every MIDI output event via event_process_midi. Since this is a hot path, let's do performance optimization with RCU. The new output_substream __rcu pointer is published via rcu_assign_pointer() in midisynth_use() after output_rfile is set, and cleared in midisynth_unuse() before the resource teardown. event_process_midi() reads it under rcu_read_lock() and bumps output_use_lock inside that section, which is necessary to close the window between the pointer dereference and the refcount increment. midisynth_unuse() calls synchronize_rcu() before snd_use_lock_sync(): this guarantees that any reader who obtained a non-NULL pointer has already called atomic_inc (output_use_lock), so the subsequent snd_use_lock_sync() sees the correct in-flight count. Signed-off-by: Takashi Iwai --- sound/core/seq/seq_midi.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sound/core/seq/seq_midi.c b/sound/core/seq/seq_midi.c index 2eb12199c92f..c2f89aee1914 100644 --- a/sound/core/seq/seq_midi.c +++ b/sound/core/seq/seq_midi.c @@ -43,8 +43,8 @@ struct seq_midisynth { int device; int subdevice; struct snd_rawmidi_file input_rfile; - spinlock_t output_lock; /* protects output_rfile publication */ snd_use_lock_t output_use_lock; /* in-flight event_input users */ + struct snd_rawmidi_substream __rcu *output_substream; struct snd_rawmidi_file output_rfile; int seq_client; int seq_port; @@ -134,8 +134,8 @@ static int event_process_midi(struct snd_seq_event *ev,= int direct, if (snd_BUG_ON(!msynth)) return -EINVAL; =20 - scoped_guard(spinlock_irqsave, &msynth->output_lock) { - substream =3D msynth->output_rfile.output; + scoped_guard(rcu) { + substream =3D rcu_dereference(msynth->output_substream); if (!substream) return -ENODEV; snd_use_lock_use(&msynth->output_use_lock); @@ -177,7 +177,6 @@ static int snd_seq_midisynth_new(struct seq_midisynth *= msynth, msynth->card =3D card; msynth->device =3D device; msynth->subdevice =3D subdevice; - spin_lock_init(&msynth->output_lock); snd_use_lock_init(&msynth->output_use_lock); return 0; } @@ -252,8 +251,8 @@ static int midisynth_use(void *private_data, struct snd= _seq_port_subscribe *info return err; } snd_midi_event_reset_decode(msynth->parser); - scoped_guard(spinlock_irqsave, &msynth->output_lock) - msynth->output_rfile =3D rfile; + msynth->output_rfile =3D rfile; + rcu_assign_pointer(msynth->output_substream, rfile.output); return 0; } =20 @@ -261,17 +260,16 @@ static int midisynth_use(void *private_data, struct s= nd_seq_port_subscribe *info static int midisynth_unuse(void *private_data, struct snd_seq_port_subscri= be *info) { struct seq_midisynth *msynth =3D private_data; - struct snd_rawmidi_file rfile =3D {}; + struct snd_rawmidi_file rfile; =20 - scoped_guard(spinlock_irqsave, &msynth->output_lock) { - rfile =3D msynth->output_rfile; - msynth->output_rfile =3D (struct snd_rawmidi_file){}; - } + rcu_assign_pointer(msynth->output_substream, NULL); + synchronize_rcu(); + snd_use_lock_sync(&msynth->output_use_lock); + rfile =3D msynth->output_rfile; + msynth->output_rfile =3D (struct snd_rawmidi_file){}; =20 if (snd_BUG_ON(!rfile.output)) return -EINVAL; - - snd_use_lock_sync(&msynth->output_use_lock); snd_rawmidi_drain_output(rfile.output); return snd_rawmidi_kernel_release(&rfile); } --=20 2.55.0