Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6268225iob; Tue, 10 May 2022 14:27:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3vxgjGy2jW4Ci10aHXgxlgmnu/nivMFE40k0zd+lLicgn7ISr40hIw9MlB7ritHfTA9EY X-Received: by 2002:a17:906:4884:b0:6f3:a042:c1c6 with SMTP id v4-20020a170906488400b006f3a042c1c6mr21389556ejq.363.1652218035161; Tue, 10 May 2022 14:27:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652218035; cv=none; d=google.com; s=arc-20160816; b=N9+twpAsxxs9bxGRzGmavZUG41/zTd/RiibcWkCmluSFhNowoXWQ1SAnIeGEXXc1G4 WMYLIxyjJhLsIFVxtMGr/dK42zRNGpwYuqQIm2NVH+j8obt24C5eH5oNEjiAgTv23wHF 72tXGck741DUWpvGXbji5bIkD6eD0xuKqMBDw0d6OFyt4mHmr9pDPG/XUoNd48TDNllZ AkkSln/GHTnN17No03w81IxVkZJBmuwSgAzxtVRxLsa8/KFLR2y5k3Me5/mX3uY48+Am CcyDeshEEMWQV+CyBewCSrxUlagvDJp75Yqav+ymBrTAp8siAk0yMbBHNacPlYrjRqbg 1Nzw== 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=htApwVilIJEVIaP3EktShtFPKkb6mKJ4V5RLqLOzvWY=; b=nmMU1mcXj1TP1pLLOFBe5ufx6BKBH00h/FajiHeTeLxPrWFGbbr1Gmkaf0iqiLSPCV 4/LTbPA+VMffRaKPcWN6yvlLv19uoFmGPpvCDy/kZQiikUQC0Mh12skYVWvqOcWw5Qix pqZ4emNxr3S6k9s9vhB427Gt2JydowmB++56z5Mm+Ff9o6x9WEzRzfHtxY2tIstg1ivJ 5RBwp5db2hFhOzhIuqU4Jo9Z7N+u+HB/8+H3lNAmlIsRA8YSSHNV3s5rzAvkJbsj67cP uJg3A6ph+p6V6UMeZXIMjwCBABvJE9S3Nv+HxqqKl7tM2NmxNPhtjNu7PzrjYVkCLofr zx6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="T7P1/+O3"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dr3-20020a170907720300b006f3ce72ceb7si455695ejc.531.2022.05.10.14.26.51; Tue, 10 May 2022 14:27:15 -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=@linuxfoundation.org header.s=korg header.b="T7P1/+O3"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244800AbiEJNvW (ORCPT + 99 others); Tue, 10 May 2022 09:51:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243298AbiEJNeb (ORCPT ); Tue, 10 May 2022 09:34:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDA47239B0F; Tue, 10 May 2022 06:24:33 -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 ams.source.kernel.org (Postfix) with ESMTPS id 2A742B81DAB; Tue, 10 May 2022 13:24:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8CBC7C385A6; Tue, 10 May 2022 13:24:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652189065; bh=NH1WQQg/EKz+Db3o4G/Q0BKZec7uXTfaOSgL+EuBn+w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T7P1/+O3MI84UXlh9sQ4KEZ/iRz/5soMtDeW21C/z/MEQCJH1otC4/jK19c+NeFVU 70e5QtWRFDzv8YpBOK2gGdzrH7rO7YDubnyV9oWPhWL4fIlHT6SWLPMLzFZAGPmSi5 jXrKoeePP+99FxzhxjfHLM7NGnstIMQ3SMd5y8vM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Jaroslav Kysela , Takashi Iwai , Ovidiu Panait Subject: [PATCH 5.4 44/52] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Tue, 10 May 2022 15:08:13 +0200 Message-Id: <20220510130731.141704841@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130729.852544477@linuxfoundation.org> References: <20220510130729.852544477@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=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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 [OP: backport to 5.4: adjusted context] Signed-off-by: Ovidiu Panait 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 @@ -1042,15 +1042,17 @@ struct action_ops { */ static int snd_pcm_action_group(const struct action_ops *ops, struct snd_pcm_substream *substream, - int state, int do_lock) + int state, int 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); @@ -1078,18 +1080,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; } @@ -1219,10 +1221,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, 0); else res = snd_pcm_action_single(ops, substream, state); + mutex_unlock(&substream->runtime->buffer_mutex); up_read(&snd_pcm_link_rwsem); return res; }