Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp128827pxb; Mon, 31 Jan 2022 17:13:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxKhE1aYipNYGkie7ZNPsNPHb29i+Kn7+KWhvS2bnQXrNdXNFS0TrniIdEy03s/PMoAv43 X-Received: by 2002:a05:6402:1617:: with SMTP id f23mr23321122edv.252.1643678014405; Mon, 31 Jan 2022 17:13:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643678014; cv=none; d=google.com; s=arc-20160816; b=L1tl4rgT9PnGaB3dcl2C84qMfcIvPA2GOYm4m+PLIYbTtmj+KjomBfjvgD6ZojbdVN RHMNfD+xbRSdY/F/C5MCZZtukegSoOQukycfygn0IX5xlid65RUpK/27Fzc0kkTUlUhl 64qeVfsRYXXsE51gTbklsMuIwWxCMPK9MqoEizJ6xUljF0dEQEdSSSgkd4b70vpQd5+D eaEVGYlSxYOwbUTWz7kP45iM0CyafVejipNpNISYLU4RnvAuAaanIOjQvUSQUPH7vul8 LGME82VSvgL6KsH8z9nwpFKfURPWNHD+6PCMw8mqIdM24/K9o48ykYdkICK20weoed0x 5JWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature:dkim-signature; bh=YQGe8ku0xgyOr5vWVm7ZKhVbHPS12hwqWMo7r42m+vg=; b=I9qRvnD2R5w2uJPFj3DotBedIIgTegcus/4BVWBI9MKv/bId7kS1gCc7nDrTDks5vl dqxBJwhbMWRZOMeuCTfn5S8RyFaoTCnAKk3D53PW30RVn5zMp9Ybe/UJtQjfCJ5hD0yc jdTVP1lWTcaghIzDVNIJ3bQo/UB9m188XVWminrDQVdIQNUVbJI9Ls30uVpZfydssFdK TFNY5Dk7/5c+S9ymnyisR1IghkMCS6dDemRZ2G3oOH2+zgTWwWVnM1QOBKEZeJFbJruc JVV9t1/3X/j4jvsBRDP4ky30zpz5+xkmMNHGPzcuXqHRqLu5KofJYRG0xC0oDQIxDzSw MRGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=fPDltyJA; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=1fAjckwk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u18si8467876eda.465.2022.01.31.17.13.09; Mon, 31 Jan 2022 17:13:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=fPDltyJA; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=1fAjckwk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1345175AbiA2IU0 (ORCPT + 99 others); Sat, 29 Jan 2022 03:20:26 -0500 Received: from smtp-out2.suse.de ([195.135.220.29]:53774 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345045AbiA2IUY (ORCPT ); Sat, 29 Jan 2022 03:20:24 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 33D1E1F37B; Sat, 29 Jan 2022 08:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1643444423; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YQGe8ku0xgyOr5vWVm7ZKhVbHPS12hwqWMo7r42m+vg=; b=fPDltyJAcjS9Xa6/tv0Mu9nklS7kHMr+HGru4P5KtNHNWzWVnO+lu5VlKgbxN117jux9MO zUbjqZ5j7iP3ZxD0pqjrXRYGoyZjYEGrN3opxreAus3nyRhx1bdU0jwZqHWS72ekmVvtWP 1sjj5dIEYzi0FJvXraixyWF4t+8zCA0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1643444423; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YQGe8ku0xgyOr5vWVm7ZKhVbHPS12hwqWMo7r42m+vg=; b=1fAjckwkqjuW6ClPPv5NZqCZsDGYOSyG1duC3rHJUbNZ6Yd1jFDfCQh3nLrR6vqdk0imam Stgt+XWEvTthPPBA== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id DF6ECA3B83; Sat, 29 Jan 2022 08:20:22 +0000 (UTC) Date: Sat, 29 Jan 2022 09:20:22 +0100 Message-ID: From: Takashi Iwai To: Jia-Ju Bai Cc: perex@perex.cz, tiwai@suse.com, broonie@kernel.org, o-takashi@sakamocchi.jp, alsa-devel@alsa-project.org, linux-kernel Subject: Re: [BUG] ALSA: core: possible deadlock involving waiting and locking operations In-Reply-To: <37c84cd1-80c6-cbcf-6673-d90d99501d4f@gmail.com> References: <56766037-972e-9e5b-74c1-88633a72a77f@gmail.com> <37c84cd1-80c6-cbcf-6673-d90d99501d4f@gmail.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=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 29 Jan 2022 09:07:05 +0100, Jia-Ju Bai wrote: > > > > On 2022/1/29 12:27, Takashi Sakamoto wrote: > > Hi, > > > > On Sat, Jan 29, 2022 at 11:33:26AM +0800, Jia-Ju Bai wrote: > >> Hello, > >> > >> My static analysis tool reports a possible deadlock in the sound driver > >> in Linux 5.10: > >> > >> snd_card_disconnect_sync() > >>   spin_lock_irq(&card->files_lock); --> Line 461 (Lock A) > >>   wait_event_lock_irq(card->remove_sleep, ...); --> Line 462 (Wait X) > >>   spin_unlock_irq(&card->files_lock); --> Line 465 (Unlock A) > >> > >> snd_hwdep_release() > >>   mutex_lock(&hw->open_mutex); --> Line 152 (Lock B) > >>   mutex_unlock(&hw->open_mutex); --> Line 157 (Unlock B) > >>   snd_card_file_remove() > >>     wake_up_all(&card->remove_sleep); --> Line 976 (Wake X) > >> > >> snd_hwdep_open() > >>   mutex_lock(&hw->open_mutex); --> Line 95 (Lock B) > >>   snd_card_file_add() > >>     spin_lock(&card->files_lock); --> Line 932 (Lock A) > >>     spin_unlock(&card->files_lock); --> Line 940 (Unlock A) > >>   mutex_unlock(&hw->open_mutex); --> Line 139 (Unlock B) > >> > >> When snd_card_disconnect_sync() is executed, "Wait X" is performed by > >> holding "Lock A". If snd_hwdep_open() is executed at this time, it holds > >> "Lock B" and then waits for acquiring "Lock A". If snd_hwdep_release() > >> is executed at this time, it waits for acquiring "Lock B", and thus > >> "Wake X" cannot be performed to wake up "Wait X" in > >> snd_card_disconnect_sync(), causing a possible deadlock. > >> > >> I am not quite sure whether this possible problem is real and how to fix > >> it if it is real. > >> Any feedback would be appreciated, thanks :) > > I'm interested in your report about the deadlock, and seek the cause > > of issue. Then I realized that we should take care of the replacement of > > file_operation before acquiring spinlock in snd_card_disconnect_sync(). > > > > ``` > > snd_card_disconnect_sync() > > ->snd_card_disconnect() > > ->spin_lock() > > ->list_for_each_entry() > > mfile->file->f_op = snd_shutdown_f_ops > > ->spin_unlock() > > ->spin_lock_irq() > > ->wait_event_lock_irq() > > ->spin_unlock_irq() > > ``` > > > > The implementation of snd_shutdown_f_ops has no value for .open, therefore > > snd_hwdep_open() is not called anymore when waiting the event. The mutex > > (Lock B) is not acquired in process context of ALSA hwdep application. > > > > The original .release function can be called by snd_disconnect_release() > > via replaced snd_shutdown_f_ops. In the case, as you can see, the spinlock > > (Lock A) is not acquired. > > > > I think there are no race conditions against Lock A and B in process > > context of ALSA hwdep application after card disconnection. But it would > > be probable to overlook the other case. I would be glad to receive your > > check for the above procedure. > > Thanks a lot for the quick reply :) > Your explanation is reasonable, because snd_shutdown_f_ops indeed has > no value for .open. > > However, my static analysis tool finds another possible deadlock in > the mentioned code: > > snd_card_disconnect_sync() >   spin_lock_irq(&card->files_lock); --> Line 461 (Lock A) >   wait_event_lock_irq(card->remove_sleep, ...); --> Line 462 (Wait X) >   spin_unlock_irq(&card->files_lock); --> Line 465 (Unlock A) > > snd_hwdep_release() >   snd_card_file_remove() >     spin_lock(&card->files_lock); --> Line 962 (Lock A) >     wake_up_all(&card->remove_sleep); --> Line 976 (Wake X) >     spin_unlock(&card->files_lock); --> Line 977 (Unlock A) > > When snd_card_disconnect_sync() is executed, "Wait X" is performed by > holding "Lock A". No, it's wait_event_lock_irq(), and this helper unlocks the given lock during waiting and re-locks it after schedule(). See the macro expansion in include/linux/wait.h. Takashi