Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1011134iob; Fri, 13 May 2022 19:32:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKtv51OfLyHhBZoPBf3b+uXqZ/S5dwkTe47Yr211ZtgDjQvAth6WtKbi2SPBNCSUaZMpH+ X-Received: by 2002:a05:600c:2249:b0:393:fac7:476b with SMTP id a9-20020a05600c224900b00393fac7476bmr6901078wmm.151.1652495541617; Fri, 13 May 2022 19:32:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652495541; cv=none; d=google.com; s=arc-20160816; b=0stl86FHfZzlXj8wDuK5sOuKXZaM70C6IFA6WjVkXMbzvjlkQC0n3t++t5KWgMyadp rngbVtirsYx9BJmr09GxpBT609j2OlaYTXvy9DTJ5y3tXtwkudNLl8Gv/5P9w0zCCxZJ 9VMI7Ptuz4H/FIjJURohLLNd7JI7jTh7gUBDMVhKubXndVNnVAcsREefMWRpMFxsE/qD ++G1bLwAbjdElQun1gK/Guba2cO44TwrL/KbBEF8W/siipY+/BlXalrGk0wxpp1J9xBq DjUAQ7BIOdpY9dDY6w+Big/d16oD7HBxz0Bu75+qu+b3nx8hyZGifLfTvsWnb3Jh+rnU 08pg== 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=teLnXHwvkhbc2o2W+L71P5WQEAyptv35/Baxzrfin6E=; b=ZQzt4px7TNR+6sPrVSlg5GziQIoxO7rLPSyxMZqD6S7l9DC3p/oxFdOlZZbvbzFDIE vq7I1CfTIaqxmWssm2UdArmkvtkPxeTcgGvMlMejhujtthx8AQxsIV5LUZP2Wa1ArURf 3jSsyveYQCOeQP4275bsbKVHAHbM67Grmj1YkXsT/JgtKCCbW6FefP4DWgXvunPIIYa3 xpYbtj3jJza/9jDlvwRsV3oOVvZ5en8fcmzwspkjWW1BfhO24PxjZaLSUyNE9IgAzVrq sRlqU4ebiZ/1S3w1CECUU0AP3PVeYT0UjWs7tNROsAN7lfx/BMLAMi6dw3S4tFdYZsk2 QhAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yXwBQ7g6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p22-20020a1c5456000000b003941bfcf824si3764517wmi.103.2022.05.13.19.32.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:32:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yXwBQ7g6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 69C4A4E3942; Fri, 13 May 2022 17:45:26 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380700AbiEMO1p (ORCPT + 99 others); Fri, 13 May 2022 10:27:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380834AbiEMO0J (ORCPT ); Fri, 13 May 2022 10:26:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC6B98CCC6; Fri, 13 May 2022 07:25:36 -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 6013862183; Fri, 13 May 2022 14:25:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D3E5C34100; Fri, 13 May 2022 14:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652451935; bh=wF23wzGO1rAl03Q2zTkZgDkIBQxl3LtS4QxKteSChoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yXwBQ7g6KajHNd4SW6p0Frtha7TyR/32V3jjvKUrGHBjgIyulhT0f9F/zAnSFaETt FRI0LwGGfk5zxezj6EKjj0EOV5i40q4KX0M8SIhaDG+cpjeSMzcyr9dOGP6EvGN/z+ y7yyJdGgHdpWG0aWu3yKBPoNEsYnUTcuEdTE/I2M= 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 4.19 10/15] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Fri, 13 May 2022 16:23:32 +0200 Message-Id: <20220513142228.200842349@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220513142227.897535454@linuxfoundation.org> References: <20220513142227.897535454@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=-2.6 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 [OP: backport to 4.19: 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 @@ -1078,15 +1078,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); @@ -1114,18 +1116,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; } @@ -1206,10 +1208,12 @@ static int snd_pcm_action_nonatomic(cons int res; 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; }