Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 87509C6FA82 for ; Sat, 10 Sep 2022 10:23:06 +0000 (UTC) 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 B0F6016A0; Sat, 10 Sep 2022 12:22:13 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz B0F6016A0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1662805383; bh=Yjkhh8lFzRCV+VhQgkLeJMUhnXYV17OfJWaNtrsmDQg=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=rX4unQQkg/za6QhkXNhiPupwzjZhDhy7lkxatMpPUcnWjQTTXTH21DXrvRYy431oM EU0NjxISvi7KsDSeA1swI85380t0vtH6JSmLUxOrmAhWR43su+KpwNah3uveigrOPz lRJlZBllkcOiowbDE+qwz44MlLpble5gLT5+lHc8= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 4E6F4F8016C; Sat, 10 Sep 2022 12:22:13 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 114BEF8042F; Sat, 10 Sep 2022 12:22:12 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (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 06E0EF8016B for ; Sat, 10 Sep 2022 12:22:05 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 06E0EF8016B Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="gP9/G/Yj"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="u8rUZljw" 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 5E82A22628; Sat, 10 Sep 2022 10:22:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1662805325; 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=VQcqT8yiM0BTjMe2rGPWC4iiIwuFBqoqxMNH24fJDwk=; b=gP9/G/YjZfD9+QJOQBNFX1dlku0Wu/FYH4Mkk6sJQuXgOxg0N39yna58zcoChUOd2gpAcw bcclxFMeK6lZHCvInu6MHUKlECO17Tx1xN6LK2EXHr2Yov7oQxZd3cWICZOmrTY8wVdll2 qHhdeUhoauSjKCQkjOLMv32E+pXjop8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1662805325; 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=VQcqT8yiM0BTjMe2rGPWC4iiIwuFBqoqxMNH24fJDwk=; b=u8rUZljwCh1vix/3N1Wci/Os2oNvIz6fY6EVfjSwSb2AGUU8WG8VZ2GNSXb1nVtZyFrjH8 Bu/1kkAPbPaZZmCg== 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 4732C13441; Sat, 10 Sep 2022 10:22:05 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id FG6sEE1lHGMbGAAAMHmgww (envelope-from ); Sat, 10 Sep 2022 10:22:05 +0000 Date: Sat, 10 Sep 2022 12:22:04 +0200 Message-ID: <8735czcywj.wl-tiwai@suse.de> From: Takashi Iwai To: Ville =?ISO-8859-1?Q?Syrj=E4l=E4?= Subject: Re: hda codec unbind refcount hang In-Reply-To: References: <87fsh0czd8.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=ISO-8859-1 Content-Transfer-Encoding: 8bit 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 Fri, 09 Sep 2022 21:39:19 +0200, Ville Syrjälä wrote: > > On Fri, Sep 09, 2022 at 05:59:47PM +0200, Takashi Iwai wrote: > > On Fri, 09 Sep 2022 17:45:25 +0200, > > Ville Syrjälä wrote: > > > > > > Hi Takashi, > > > > > > commit 7206998f578d ("ALSA: hda: Fix potential deadlock at codec > > > unbinding") introduced a problem on at least one of my older machines. > > > > > > The problem happens when hda_codec_driver_remove() encounters a > > > codec without any pcms (and thus the refcount is 1) and tries to > > > call refcount_dec(). Turns out refcount_dec() doesn't like to be > > > used for dropping the refcount to 0, and instead if spews a warning > > > and does its saturate thing. The subsequent wait_event() is then > > > permanently stuck waiting on the saturated refcount. > > > > > > I've definitely seen the same kind of pattern used elsewhere > > > in the kernel as well, so the fact that refcount_t can't be used > > > to implement it is a bit of surprise to me. I guess most other > > > places still use atomic_t instead. > > > > Does the patch below work around it? It seem to be a subtle > > difference between refcount_dec() and refcount_dec_and_test(). > > Aye, this works. > > Tested-by: Ville Syrjälä Good to hear. I think the below is slightly safer, assuring the other *_disconnect() calls. Could you give it a try again? Once after confirming it works, I'll re-submit and merge to my tree. thanks, Takashi -- 8< -- From: Takashi Iwai Subject: [PATCH] ALSA: hda: Fix hang at HD-audio codec unbinding due to refcount saturation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We fixed the potential deadlock at dynamic unbinding the HD-audio codec at the commit 7206998f578d ("ALSA: hda: Fix potential deadlock at codec unbinding"), but ironically, this caused another potential deadlock. The current code uses refcount_dec() and waits for the pending task with wait_event for dropping the refcount to 0. This works fine when PCMs are assigned and actually waiting for the refcount drop. Meanwhile, when there was no PCM assigned, the refcount_dec() call itself was supposed to drop to zero -- alas, it doesn't in reality; refcount_dec() complains, spews kernel warning and it saturates instead of dropping to 0, due to the nature of refcount_dec() implementation. This eventually blocks the wait_event() wakeup and the code get stuck there. For avoiding the problem, we call refcount_dec_and_test() and skips the sync-wait if it already reaches to zero. The patch does a slight code reshuffling to make sure to invoke other disconnect calls before the sync-wait, too. Fixes: 7206998f578d ("ALSA: hda: Fix potential deadlock at codec unbinding") Reported-by: Ville Syrjälä Cc: Link: https://lore.kernel.org/r/YxtflWQnslMHVlU7@intel.com Signed-off-by: Takashi Iwai --- sound/pci/hda/hda_bind.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sound/pci/hda/hda_bind.c b/sound/pci/hda/hda_bind.c index cae9a975cbcc..1a868dd9dc4b 100644 --- a/sound/pci/hda/hda_bind.c +++ b/sound/pci/hda/hda_bind.c @@ -157,10 +157,10 @@ static int hda_codec_driver_remove(struct device *dev) return codec->bus->core.ext_ops->hdev_detach(&codec->core); } - refcount_dec(&codec->pcm_ref); snd_hda_codec_disconnect_pcms(codec); snd_hda_jack_tbl_disconnect(codec); - wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref)); + if (!refcount_dec_and_test(&codec->pcm_ref)) + wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref)); snd_power_sync_ref(codec->bus->card); if (codec->patch_ops.free) -- 2.35.3