Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2172386pxb; Fri, 25 Mar 2022 12:23:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/ms669QJurt+DnsdTr8FW9bHqFhu+m45Xs1dwujMOPgwtR75mDCNiwPKPIxYs+wrk8XzJ X-Received: by 2002:a65:6657:0:b0:381:1b99:3f04 with SMTP id z23-20020a656657000000b003811b993f04mr828975pgv.512.1648236187279; Fri, 25 Mar 2022 12:23:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648236187; cv=none; d=google.com; s=arc-20160816; b=xpR6ZrmPd2XLYI7mhJhMYmrZ9vNpPhDPAVdMQxktdO8e3SFv6hyc5wfJcoKY1NVg2F 6lnUlJMefQJdlgMgXNz68bxdHwtx1PPi5QAvc6d4/qJwlVVZLbZnjJuCz9pnsi0qDV3b 5RoOAW8mH3/2Zl0ymkoXtjNGdMZhDLLVpVd/UjvUdvqiZ8gU1/bqUZlmbktsdoJaG2N4 sANMDS2gstHHofGSiuCnrBRisQVptgz1ao5OP3LFO6zFtMXGV02mZIdOs4rn0cIVm86Y 0HoFU1CnaK4rN6lek9Zy1CMb+OGcMvTLRathmLmhLgqbGZmLjfUe+fpwpQgCa6Mr2JfS oiCg== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=0DaD37NkjMeEohpuI9m8EWVl12d6hIN7aC5UMdoahYc=; b=qBlbQUa1ndg99lMRpufJ/mWA4uUcdJqKKjmkgKR1zdopc+CLxDfi7CZe/ugbEl7aV2 v+jHys9iQ8a47AXWg2yMNsL5qPq+H2m+2HVf1w5qBaRulc7RJ4LevVKfHBajY2/+USMr 9ze7fgY+EZe5pAtb7YoB6iGCqyQcSkgDu4FZEL0F4JaqOX4X28sb337Zl9Rvk/OnahMV 8bfbgxr1QX6I5m2XDZO+7sOfZ4rDGkNuI73TE5l9uFG8WflCSrc0dN9qriArQYijuvr2 A2twmEd9+OxzATLzOU1FnfCHOGPB7qnKZQbpRnc7QWBIbT6OkhWXNTd21UwI7rUHBigc IErA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LPKjeRoz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id t11-20020a63b70b000000b003816043f03dsi3476177pgf.562.2022.03.25.12.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:23:07 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LPKjeRoz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D93226F91B; Fri, 25 Mar 2022 11:29:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377038AbiCYP2V (ORCPT + 99 others); Fri, 25 Mar 2022 11:28:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376917AbiCYPXb (ORCPT ); Fri, 25 Mar 2022 11:23:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8067972B2; Fri, 25 Mar 2022 08:17:00 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DBBE860C7E; Fri, 25 Mar 2022 15:16:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED0F8C340E9; Fri, 25 Mar 2022 15:16:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221412; bh=kicBNNucxakRS3E2qZE8FCkVQHqIfvzJ9ucZuCzm7a4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LPKjeRozUxgbVdVevDgIB76EZqAJ6kqQahTCJQyUUkADSOqTm34zkr1M931+NL9Ba hGBGzQFpHJ8srn0/r6x5vFrTlDZaijq7LxnULJUKhtshqS+IqWO8RKCunWTCjl3I/3 VtA2KjBOnXiYTOERNJZMiazkMWGRXfY+qiL8cvSE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jaroslav Kysela , Takashi Iwai Subject: [PATCH 5.16 15/37] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Fri, 25 Mar 2022 16:14:25 +0100 Message-Id: <20220325150420.483429814@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150420.046488912@linuxfoundation.org> References: <20220325150420.046488912@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Takashi Iwai commit 3c3201f8c7bb77eb53b08a3ca8d9a4ddc500b4c0 upstream. Like the previous fixes to hw_params and hw_free ioctl races, we need to paper over the concurrent prepare ioctl calls against hw_params and hw_free, too. This patch implements the locking with the existing runtime->buffer_mutex for prepare ioctls. Unlike the previous case for snd_pcm_hw_hw_params() and snd_pcm_hw_free(), snd_pcm_prepare() is performed to the linked streams, hence the lock can't be applied simply on the top. For tracking the lock in each linked substream, we modify snd_pcm_action_group() slightly and apply the buffer_mutex for the case stream_lock=false (formerly there was no lock applied) there. Cc: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-4-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/pcm_native.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -1177,15 +1177,17 @@ struct action_ops { static int snd_pcm_action_group(const struct action_ops *ops, struct snd_pcm_substream *substream, snd_pcm_state_t state, - bool do_lock) + bool stream_lock) { struct snd_pcm_substream *s = NULL; struct snd_pcm_substream *s1; int res = 0, depth = 1; snd_pcm_group_for_each_entry(s, substream) { - if (do_lock && s != substream) { - if (s->pcm->nonatomic) + if (s != substream) { + if (!stream_lock) + mutex_lock_nested(&s->runtime->buffer_mutex, depth); + else if (s->pcm->nonatomic) mutex_lock_nested(&s->self_group.mutex, depth); else spin_lock_nested(&s->self_group.lock, depth); @@ -1213,18 +1215,18 @@ static int snd_pcm_action_group(const st ops->post_action(s, state); } _unlock: - if (do_lock) { - /* unlock streams */ - snd_pcm_group_for_each_entry(s1, substream) { - if (s1 != substream) { - if (s1->pcm->nonatomic) - mutex_unlock(&s1->self_group.mutex); - else - spin_unlock(&s1->self_group.lock); - } - if (s1 == s) /* end */ - break; + /* unlock streams */ + snd_pcm_group_for_each_entry(s1, substream) { + if (s1 != substream) { + if (!stream_lock) + mutex_unlock(&s1->runtime->buffer_mutex); + else if (s1->pcm->nonatomic) + mutex_unlock(&s1->self_group.mutex); + else + spin_unlock(&s1->self_group.lock); } + if (s1 == s) /* end */ + break; } return res; } @@ -1354,10 +1356,12 @@ static int snd_pcm_action_nonatomic(cons /* Guarantee the group members won't change during non-atomic action */ down_read(&snd_pcm_link_rwsem); + mutex_lock(&substream->runtime->buffer_mutex); if (snd_pcm_stream_linked(substream)) res = snd_pcm_action_group(ops, substream, state, false); else res = snd_pcm_action_single(ops, substream, state); + mutex_unlock(&substream->runtime->buffer_mutex); up_read(&snd_pcm_link_rwsem); return res; }