Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4479544ybx; Mon, 4 Nov 2019 14:07:08 -0800 (PST) X-Google-Smtp-Source: APXvYqxzQbjgaEXaxRm7xjbnFIIp4qi4kSTtkhO/HMpukwtCy3JUGJelOW7bVvMoi86x2ORphPc+ X-Received: by 2002:a17:906:245b:: with SMTP id a27mr26667044ejb.192.1572905228173; Mon, 04 Nov 2019 14:07:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905228; cv=none; d=google.com; s=arc-20160816; b=AtD/MblemNtcPAYR8WPiyraiKeMqnZ2AdVInaZbBn2qAff7uuzW7slQsFPu9vX09es jzv6GaaXGpFV+WNFcg18+3p9CotrsHAR1eWluTev6CvsGk8q6DsRH9Cnu9eM5q1RR35I 1l544pHD2qySm+jIFowWQjrNhcErXBt96Y9M+x3RWzmvEXUwzmPTp3NBANy7DZ6zEtFC sjr+iphPe1jN/QUkTb4DtkOsVP0abqK8nPAKKNhZ76wyMttL9AWyXtIvKYmWB0E8188b jHTZVdFIEBkHteseTOZnJZg4nkN4bCdzwTbbQI8sYb40ulcGARAQojTfeVcaCH6JZSmX 38Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=10JhFvgO/gb/ykLDy/aKyoxcUwJ49e66CFso3jl/XMk=; b=f8Zq2NGnDMUwCnHzwPMvTpkNSzyqb1zPrdrlySHOfD01MYTkQS7K2veF+Jlw1TmxHU o5jXY3TaRW3Xpe4yhEwcPNn9mtTsOx9JtyU1OcxxxrrPMgjaj6fPKoVBeXrJOs6sXB5w AqnryNI1yfmcnOEyFvGp0X0oNvzHVa7aUMJK/TcprVmx7QBbPfufo6J2LTiEjZtytjWS dbdmaFt/fCA0gwQJ9uZEzMHaKp+lbIPRac6ttskWlkoeEMEGSboBOUPsdzBVOyYUcRCf 5zQLjeFLoz/A37V9ycx7tg/YcKsduJLHUiEoSibm8Z8YCPgUCBo1F5WNSHEHXr6oXb4P Mb4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=U9d7A4Sh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l19si5010003ejq.261.2019.11.04.14.06.45; Mon, 04 Nov 2019 14:07:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=U9d7A4Sh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389510AbfKDWD2 (ORCPT + 99 others); Mon, 4 Nov 2019 17:03:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:33704 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388674AbfKDWDY (ORCPT ); Mon, 4 Nov 2019 17:03:24 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A2194205C9; Mon, 4 Nov 2019 22:03:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905003; bh=R8oem6kYBfXs8/8RMKhTDq7Q6vVq9CXFQzcsiBrmlOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U9d7A4Sham83wIXf2Om4VdSzaZSTjhdYLpSZV7ObSeK7af8JzTgnizSbX2Jxb1PnQ KBtPxQVOnfj7b5VxszQiA8t3zciSGrX06IsU72j58KQ4P7JZbhhFQPUe5nlT84NurH 8+3ij82GywpD+twU+ZyXQkyRkAheYfkLh3E8k+os= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Takashi Iwai , Sasha Levin , "Kirill A . Shutemov" Subject: [PATCH 4.19 146/149] ALSA: timer: Fix mutex deadlock at releasing card Date: Mon, 4 Nov 2019 22:45:39 +0100 Message-Id: <20191104212146.920774513@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212126.090054740@linuxfoundation.org> References: <20191104212126.090054740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Takashi Iwai [ Upstream commit a39331867335d4a94b6165e306265c9e24aca073 ] When a card is disconnected while in use, the system waits until all opened files are closed then releases the card. This is done via put_device() of the card device in each device release code. The recently reported mutex deadlock bug happens in this code path; snd_timer_close() for the timer device deals with the global register_mutex and it calls put_device() there. When this timer device is the last one, the card gets freed and it eventually calls snd_timer_free(), which has again the protection with the global register_mutex -- boom. Basically put_device() call itself is race-free, so a relative simple workaround is to move this put_device() call out of the mutex. For achieving that, in this patch, snd_timer_close_locked() got a new argument to store the card device pointer in return, and each caller invokes put_device() with the returned object after the mutex unlock. Reported-and-tested-by: Kirill A. Shutemov Cc: Signed-off-by: Takashi Iwai Signed-off-by: Sasha Levin --- sound/core/timer.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/sound/core/timer.c b/sound/core/timer.c index 316794eb44017..ec74705f003b8 100644 --- a/sound/core/timer.c +++ b/sound/core/timer.c @@ -240,7 +240,8 @@ static int snd_timer_check_master(struct snd_timer_instance *master) return 0; } -static int snd_timer_close_locked(struct snd_timer_instance *timeri); +static int snd_timer_close_locked(struct snd_timer_instance *timeri, + struct device **card_devp_to_put); /* * open a timer instance @@ -252,6 +253,7 @@ int snd_timer_open(struct snd_timer_instance **ti, { struct snd_timer *timer; struct snd_timer_instance *timeri = NULL; + struct device *card_dev_to_put = NULL; int err; mutex_lock(®ister_mutex); @@ -275,7 +277,7 @@ int snd_timer_open(struct snd_timer_instance **ti, list_add_tail(&timeri->open_list, &snd_timer_slave_list); err = snd_timer_check_slave(timeri); if (err < 0) { - snd_timer_close_locked(timeri); + snd_timer_close_locked(timeri, &card_dev_to_put); timeri = NULL; } goto unlock; @@ -327,7 +329,7 @@ int snd_timer_open(struct snd_timer_instance **ti, timeri = NULL; if (timer->card) - put_device(&timer->card->card_dev); + card_dev_to_put = &timer->card->card_dev; module_put(timer->module); goto unlock; } @@ -337,12 +339,15 @@ int snd_timer_open(struct snd_timer_instance **ti, timer->num_instances++; err = snd_timer_check_master(timeri); if (err < 0) { - snd_timer_close_locked(timeri); + snd_timer_close_locked(timeri, &card_dev_to_put); timeri = NULL; } unlock: mutex_unlock(®ister_mutex); + /* put_device() is called after unlock for avoiding deadlock */ + if (card_dev_to_put) + put_device(card_dev_to_put); *ti = timeri; return err; } @@ -352,7 +357,8 @@ EXPORT_SYMBOL(snd_timer_open); * close a timer instance * call this with register_mutex down. */ -static int snd_timer_close_locked(struct snd_timer_instance *timeri) +static int snd_timer_close_locked(struct snd_timer_instance *timeri, + struct device **card_devp_to_put) { struct snd_timer *timer = NULL; struct snd_timer_instance *slave, *tmp; @@ -404,7 +410,7 @@ static int snd_timer_close_locked(struct snd_timer_instance *timeri) timer->hw.close(timer); /* release a card refcount for safe disconnection */ if (timer->card) - put_device(&timer->card->card_dev); + *card_devp_to_put = &timer->card->card_dev; module_put(timer->module); } @@ -416,14 +422,18 @@ static int snd_timer_close_locked(struct snd_timer_instance *timeri) */ int snd_timer_close(struct snd_timer_instance *timeri) { + struct device *card_dev_to_put = NULL; int err; if (snd_BUG_ON(!timeri)) return -ENXIO; mutex_lock(®ister_mutex); - err = snd_timer_close_locked(timeri); + err = snd_timer_close_locked(timeri, &card_dev_to_put); mutex_unlock(®ister_mutex); + /* put_device() is called after unlock for avoiding deadlock */ + if (card_dev_to_put) + put_device(card_dev_to_put); return err; } EXPORT_SYMBOL(snd_timer_close); -- 2.20.1