Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D80BC4338F for ; Mon, 2 Aug 2021 17:14:03 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8CC0961103 for ; Mon, 2 Aug 2021 17:14:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8CC0961103 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 520FB171E; Mon, 2 Aug 2021 19:13:09 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 520FB171E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1627924439; bh=Ruo3PP7nOd9vdNuNW1FGIUiHScLxcM9IZ+QcGVHJt6M=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Ap4GTFf3D0ijycxMaQiQ5BayfcnWPTnsBBV42wUXZq96JuKpMolg6sF5AXd7GU7RX QSdOySfK5sN6YwEVsdRuYT51RZuMbna2LiXdXuyzBw9wuZuKjYb8g1dugJIv56vEUJ Jb5YMCGCK/sq1pzW7tS1f/GQ6cnmdtxm2kkE4mrg= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id C6B24F800BF; Mon, 2 Aug 2021 19:13:08 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 7E8A9F8025F; Mon, 2 Aug 2021 19:13:06 +0200 (CEST) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 4DB15F800BF for ; Mon, 2 Aug 2021 19:12:57 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 4DB15F800BF Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ZP5EVsEt"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="P1EtyB86" Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 0821D1FFC3; Mon, 2 Aug 2021 17:12:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1627924377; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Olf6bRhDFciDAhc6KOuDcnx1+AxLEi0/r3oxKDjmh5s=; b=ZP5EVsEt4vF1mEOuFxgyoXlgcHgq/n+BjvOMXHYJi7FTeqs/dKwrMJYuMG3/skkn0RueJs aT0wNAbX5v8X1N+VRRJLLEjmKkhbrr9IEpSom58s6AQ0tNCl8AoQhLHh2Yl/g0ziWwFMxq QrL2mk7HmS5Sgo4zP+YwTIfmx8OhqXM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1627924377; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Olf6bRhDFciDAhc6KOuDcnx1+AxLEi0/r3oxKDjmh5s=; b=P1EtyB863x23GOGbH1y/FI2f2PPxRmxTV44Ba8V4LjJlZtxZ8w+LYlhKwgHCaL8O7Lzzaa FknYE9Kr5A3c+KBw== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id ED023A3BBA; Mon, 2 Aug 2021 17:12:56 +0000 (UTC) Date: Mon, 02 Aug 2021 19:12:56 +0200 Message-ID: From: Takashi Iwai To: folkert Subject: Re: aconnect occasionally causes kernel oopses In-Reply-To: <20210802152117.GT890690@belle.intranet.vanheusden.com> References: <20210801182754.GP890690@belle.intranet.vanheusden.com> <20210802061845.GQ890690@belle.intranet.vanheusden.com> <20210802091012.GR890690@belle.intranet.vanheusden.com> <20210802152117.GT890690@belle.intranet.vanheusden.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Cc: alsa-devel@alsa-project.org X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" On Mon, 02 Aug 2021 17:21:17 +0200, folkert wrote: > > [ kernel bug if repeatingly aconnect'ing midi devices ] > > > Does this happen if you do reconnect of kernel sequencer client? > > You can use snd-virmidi as well as snd-dummy. > > I'm asking it because it'll simplify the test a lot, which will be > > almost self-contained. > > Like this? > > root@lappiemctopface:~# aplaymidi -l > Port Client name Port name > 14:0 Midi Through Midi Through Port-0 > 20:0 Virtual Raw MIDI 1-0 VirMIDI 1-0 > 21:0 Virtual Raw MIDI 1-1 VirMIDI 1-1 > 22:0 Virtual Raw MIDI 1-2 VirMIDI 1-2 > 23:0 Virtual Raw MIDI 1-3 VirMIDI 1-3 > 128:0 rtpmidi lappiemctopface Network > 128:1 rtpmidi lappiemctopface metronoom > 128:2 rtpmidi lappiemctopface AppleMidi2IPMidiBridge > 128:3 rtpmidi lappiemctopface oensoens > 130:0 FLUID Synth (11462) Synth input port (11462:0) > > and then: > > root@lappiemctopface:~# cat test.sh > while true > do > aconnect 20:0 21:0 > aconnect -d 20:0 21:0 > done > > root@lappiemctopface:~# for i in `seq 0 3` ; do (./test.sh &) ; done > > This hard locks-up my laptop: it doesn't even respond to capslock (led > on/off) anymore nor the ctrl+prtscr+alt+b combination. Thanks, that's really helpful! I see the possible race now. Could you try the quick fix below? Takashi --- --- a/sound/core/seq/seq_ports.c +++ b/sound/core/seq/seq_ports.c @@ -200,6 +200,17 @@ get_subscriber(struct list_head *p, bool is_src) return list_entry(p, struct snd_seq_subscribers, dest_list); } +static void subscriber_get(struct snd_seq_subscribers *subs) +{ + atomic_inc(&subs->ref_count); +} + +static void subscriber_put(struct snd_seq_subscribers *subs) +{ + if (atomic_dec_and_test(&subs->ref_count)) + kfree(subs); +} + /* * remove all subscribers on the list * this is called from port_delete, for each src and dest list. @@ -228,8 +239,7 @@ static void clear_subscriber_list(struct snd_seq_client *client, * we decrease the counter, and when both ports are deleted * remove the subscriber info */ - if (atomic_dec_and_test(&subs->ref_count)) - kfree(subs); + subscriber_put(subs); continue; } @@ -489,6 +499,8 @@ static int check_and_subscribe_port(struct snd_seq_client *client, s = get_subscriber(p, is_src); if (match_subs_info(&subs->info, &s->info)) goto __error; + if (!subs->ready) + goto __error; } } @@ -505,7 +517,7 @@ static int check_and_subscribe_port(struct snd_seq_client *client, else list_add_tail(&subs->dest_list, &grp->list_head); grp->exclusive = exclusive; - atomic_inc(&subs->ref_count); + subscriber_get(subs); write_unlock_irq(&grp->list_lock); err = 0; @@ -536,6 +548,7 @@ static void delete_and_unsubscribe_port(struct snd_seq_client *client, if (!empty) unsubscribe_port(client, port, grp, &subs->info, ack); up_write(&grp->list_mutex); + subscriber_put(subs); } /* connect two ports */ @@ -555,7 +568,7 @@ int snd_seq_port_connect(struct snd_seq_client *connector, return -ENOMEM; subs->info = *info; - atomic_set(&subs->ref_count, 0); + atomic_set(&subs->ref_count, 1); INIT_LIST_HEAD(&subs->src_list); INIT_LIST_HEAD(&subs->dest_list); @@ -572,13 +585,14 @@ int snd_seq_port_connect(struct snd_seq_client *connector, if (err < 0) goto error_dest; + subs->ready = true; return 0; error_dest: delete_and_unsubscribe_port(src_client, src_port, subs, true, connector->number != src_client->number); error: - kfree(subs); + subscriber_put(subs); return err; } @@ -597,8 +611,8 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector, down_write(&src->list_mutex); /* look for the connection */ list_for_each_entry(subs, &src->list_head, src_list) { - if (match_subs_info(info, &subs->info)) { - atomic_dec(&subs->ref_count); /* mark as not ready */ + if (match_subs_info(info, &subs->info) && subs->ready) { + subs->ready = false; err = 0; break; } @@ -611,7 +625,7 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector, connector->number != src_client->number); delete_and_unsubscribe_port(dest_client, dest_port, subs, false, connector->number != dest_client->number); - kfree(subs); + subscriber_put(subs); return 0; } diff --git a/sound/core/seq/seq_ports.h b/sound/core/seq/seq_ports.h index b1f2c4943174..d9644d5f109e 100644 --- a/sound/core/seq/seq_ports.h +++ b/sound/core/seq/seq_ports.h @@ -30,6 +30,7 @@ struct snd_seq_subscribers { struct list_head src_list; /* link of sources */ struct list_head dest_list; /* link of destinations */ atomic_t ref_count; + bool ready; }; struct snd_seq_port_subs_info {