Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2083089pxb; Fri, 25 Mar 2022 10:46:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxm/47ymEqQR04LG7opLay2aisZKzzn0G4ki/ZJH8p8pjbYGZXdGFthUNBAHAHwlnrKGBvN X-Received: by 2002:a17:90b:4f8d:b0:1c6:408b:6b0d with SMTP id qe13-20020a17090b4f8d00b001c6408b6b0dmr25937936pjb.90.1648230393281; Fri, 25 Mar 2022 10:46:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648230393; cv=none; d=google.com; s=arc-20160816; b=kyCq2HoNuhZSBu44lO6rCMU5XfPZcl3riqKGGkcwc7Px5sfbRR1y1aIp2Ova9ZtJ81 6vQRDHw6tD4mN4el+LW4y0GNCzg7eP+vN44hlpol+A7ts37IQTf0EYsWYXzFbLAHfJLD SzoMpYYIi45lScCCG77aiDvPtIAKX6I3ZieG6sLoLZ8TXWiZMFSxcH0/gQkEzUiycZEa VmorobFHhR86g5cpsw9j9UC2YPcax3yzfOiq1hBykQhrL83OoNm8VrXdsYSaSZQJMbK2 L6G+Oj6Z9lB6h7GuB39VbOjERSR39AsMyVGqmBsbGcxoZ7hx1yrNqnDNJO3kkINW5L1V 3Yzw== 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=RvsazWqAoTxhnbhYTTs6xZbH+hV/3tvqr1fPOBC0bpACfGpsD4G+msDQi8WXNMR0Qe nkmIbi4szsRPvIXQYv9d3GGqzp+n5I6PV7aKzaMCj4YtW0MJexwM5YImb2a1gVL6ZYXU HX4ARDd64ZF3k6kv4CYlyPcO4dMPECJIJonwxl8rP+ix7vKVjainybwvEspZVXzcKiOA hItaU4+nGVbLBfPUvUK9B6xCwwxua/Algne13XwRFm2LTqkKLv6wNbRreqKdkqtJTO61 ghZ0AXUqIvhNzCmbqc5V52uExvztBJ3fS6OUwSmEX74Hk6ogj7+Mjd7AgqtYrFl2LkKL 4ZBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aiCn2h3A; 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 t1-20020a63b241000000b003816043f0e5si2789994pgo.730.2022.03.25.10.46.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 10:46:33 -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=aiCn2h3A; 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 3645E1207CA; Fri, 25 Mar 2022 10:32:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376488AbiCYPWe (ORCPT + 99 others); Fri, 25 Mar 2022 11:22:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376375AbiCYPUP (ORCPT ); Fri, 25 Mar 2022 11:20:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E4FBE29DD; Fri, 25 Mar 2022 08:15:40 -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 0270BB827B7; Fri, 25 Mar 2022 15:15:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4799AC340E9; Fri, 25 Mar 2022 15:15:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221304; bh=kicBNNucxakRS3E2qZE8FCkVQHqIfvzJ9ucZuCzm7a4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aiCn2h3ApH1+ov7YeeRJyWkyl9PfqZKqp6+f2yJ9ozfIJotWI7Bo6BZDhuBLA7I9v 4PvJ1s5SVecOswO0l5DYbBL+Mr8BimrgckWbQpDk/NNnN7lVLZM4jHT4SKGabbALvC G3tquEww/1jrYsRFah7kgE+g/KSYScl1lL0sFGoM= 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.15 15/37] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Fri, 25 Mar 2022 16:14:16 +0100 Message-Id: <20220325150420.370357513@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150419.931802116@linuxfoundation.org> References: <20220325150419.931802116@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; }