Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3797053rwb; Tue, 20 Sep 2022 05:03:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7kzFG7gT4pqSlDcUQKwVZaQPjEF8cjRk/d2jb9CUhe6lHFJNb2b/Znr0id2bK2aCXTgsMH X-Received: by 2002:a63:555a:0:b0:43a:20e5:101f with SMTP id f26-20020a63555a000000b0043a20e5101fmr8636477pgm.48.1663675396475; Tue, 20 Sep 2022 05:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663675396; cv=none; d=google.com; s=arc-20160816; b=0cV3zLMKPDD2B/X4W7iXsnAlGzCQ4v6FWoXeajymoKjZiVvf6PvWEsLMYpp/zpqPuX z+hJXbbNpJSAQUJXnZAMwHsDDjWLGkrUV1FBjv5EmGVmMlHdi0TndrzHWzqzVcG1YxwH M/bdTJTfSckG2sdurrKNX9NJtrGEqyBxKanmPdXgarrIXvTqXNawMy1I54q4K83dNY83 uFYvswJhbaLOznZ6rblhTGLgHEGTUbth18RzelCZWvHfyMyAxkjp8ClFQ1nJIrGa2wlT ef31XzlscBMLlObgvK9FZXJy/FNI1FZH2rtMK0IZ/7IqlB65zh27trYPD6nScoh/1lcE +W8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=EO2wkZqfwT9DTmM5Nw73PTujWB07LvgVqPcksqDNuPY=; b=RjoMVf4qUF3hRNZlGBcKRaU7xLZ+BhqSCvEPA5DA/zvh7MrVBwvHPiz38b9heagPvY pcfeUpf7mSY0w5+fKE8AIUb/TGCuHxP3WfKZYV3i65I8XNb8FD1+fZbG64taZ/jOQT3w aPZxTTsJ7bmo08YdP7A2XuBcFn5YqMR0qjYrIt/BWr9FVoBhb5u/I9xD1uixxHtIMqQv EXWnafn2apXU0dqDuvgB5lIngIGL0f2Tt3AxTkOFWPIGxMubkuKQ9e4b3DEp2Cbi//ds QKmR8+jhgCGKX/Vici0rD1d/qCGCmr5N0q0VEA2ln5zXBayNZh4XJDX4Bx5kf8ps0+3k oBYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=PAqnTuuw; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c7-20020a056a00008700b005410f1a72cfsi1431897pfj.146.2022.09.20.05.03.03; Tue, 20 Sep 2022 05:03:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=PAqnTuuw; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230193AbiITLfb (ORCPT + 99 others); Tue, 20 Sep 2022 07:35:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230058AbiITLf2 (ORCPT ); Tue, 20 Sep 2022 07:35:28 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED7F96CD3D for ; Tue, 20 Sep 2022 04:35:27 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9F5E621BE4; Tue, 20 Sep 2022 11:35:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1663673726; 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=EO2wkZqfwT9DTmM5Nw73PTujWB07LvgVqPcksqDNuPY=; b=PAqnTuuwFLGksia6DhR5SgUPSMjLK9OeBhPEpBEk0yTK0lGIehKapzF6+1sgn6JzE5Zy0b pRI+yAeKYoOH4PvJaRQB21RYBekmopnN8i3AvD1NYgkbr8QcJxHB3oINTH1xCLoUY6dGQY YYfM9eAjubw6aJHxw0iiRptUl0JwfOk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1663673726; 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=EO2wkZqfwT9DTmM5Nw73PTujWB07LvgVqPcksqDNuPY=; b=sYMrkr/Q6AFJbxZb0hUBr08jXTI+JrVRbnc9tcfS++/ISvhVNDt09o8Yvc0UzJ2YeTJhdx OIu/WWj6Otq3k5CQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7F1121346B; Tue, 20 Sep 2022 11:35:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 3ackHn6lKWOiQAAAMHmgww (envelope-from ); Tue, 20 Sep 2022 11:35:26 +0000 Date: Tue, 20 Sep 2022 13:35:25 +0200 Message-ID: <875yhigtxe.wl-tiwai@suse.de> From: Takashi Iwai To: Rondreis Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: possible deadlock in snd_rawmidi_free In-Reply-To: <87y1ufh3u9.wl-tiwai@suse.de> References: <87y1ufh3u9.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 19 Sep 2022 15:49:02 +0200, Takashi Iwai wrote: > > On Mon, 19 Sep 2022 14:46:13 +0200, > Rondreis wrote: > > > > Hello, > > > > When fuzzing the Linux kernel driver v6.0-rc4, the following crash was > > triggered. > > > > HEAD commit: 7e18e42e4b280c85b76967a9106a13ca61c16179 > > git tree: upstream > > > > kernel config: https://pastebin.com/raw/xtrgsXP3 > > console output: https://pastebin.com/raw/9tabWDtu > > > > Sorry for failing to extract the reproducer, and the crash occurred at > > the moment of disconnecting the midi device. On other versions of > > Linux, I also triggered this crash. > > > > I would appreciate it if you have any idea how to solve this bug. > > I think there are two ways to work around it. > > The first one is to move the unregister_sound*() calls out of the > sound_oss_mutex, something like: > -- 8< -- > > --- a/sound/core/sound_oss.c > +++ b/sound/core/sound_oss.c > @@ -162,7 +162,6 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev) > mutex_unlock(&sound_oss_mutex); > return -ENOENT; > } > - unregister_sound_special(minor); > switch (SNDRV_MINOR_OSS_DEVICE(minor)) { > case SNDRV_MINOR_OSS_PCM: > track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO); > @@ -174,12 +173,18 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev) > track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1); > break; > } > - if (track2 >= 0) { > - unregister_sound_special(track2); > + if (track2 >= 0) > snd_oss_minors[track2] = NULL; > - } > snd_oss_minors[minor] = NULL; > mutex_unlock(&sound_oss_mutex); > + > + /* call unregister_sound_special() outside sound_oss_mutex; > + * otherwise may deadlock, as it can trigger the release of a card > + */ > + unregister_sound_special(minor); > + if (track2 >= 0) > + unregister_sound_special(track2); > + > kfree(mptr); > return 0; > } > -- 8< -- > > This should be OK, as the unregister_sound_*() itself can be called > concurrently. > > Another workaround would be just to remove the register_mutex call at > snd_rawmidi_free(), e.g. something like: > > -- 8< -- > --- a/sound/core/rawmidi.c > +++ b/sound/core/rawmidi.c > @@ -1899,10 +1899,8 @@ static int snd_rawmidi_free(struct snd_rawmidi *rmidi) > > snd_info_free_entry(rmidi->proc_entry); > rmidi->proc_entry = NULL; > - mutex_lock(®ister_mutex); > if (rmidi->ops && rmidi->ops->dev_unregister) > rmidi->ops->dev_unregister(rmidi); > - mutex_unlock(®ister_mutex); > > snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]); > snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]); > -- 8< -- > > This register_mutex there should be superfluous since the device has > been already processed and detached by snd_rawmidi_dev_disconnect() > beforehand. But if the first one is confirmed to work, the second one > can be left untouched. Could you check whether one of two changes above fixes the bug? Once after confirmed, I'll cook a proper patch for the submission. thanks, Takashi