Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp4019835pxp; Wed, 23 Mar 2022 09:50:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYcIAm4fZ6wY0JAdR9+OohIpQAxQu0Bfmd7O5Qo8rtVCfzMJHx0MrNyeJhmiJ3630pzAyd X-Received: by 2002:a17:907:7287:b0:6df:8f48:3f76 with SMTP id dt7-20020a170907728700b006df8f483f76mr966914ejc.411.1648054225907; Wed, 23 Mar 2022 09:50:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648054225; cv=none; d=google.com; s=arc-20160816; b=tdZ1GD0PNRcetYqRZHHBE0sOIYhtaql7lB2rTHndt3T8s89YjVEDDMkROfwPWpncMr t9szBuEdWmBndwFhPK95SKRbcCJJsyddYK/KBBqca301UZ3paAQD2//Z2tuB6zzb3PPK 4i1HQoZIhjdNwRPJVvGnQfAwLZztiL8WI7Z4Ih0pKwAJ3nvqUI/NKqln8GgodpDoDa0q Ab1EQsacfbMsjOU2yQstCcs8lPOMHZryVi+TxqgMUm/7G/op2C5tiYRTHxmEvZ18nBCW skP/EBmVstMgsU9qn6wcAxdh/wxuQGRCWRKF7M2ETdp/GD7MvgeIlFZEk/YH91hFY0if hA4w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=7BHpLrva0gn3z4ug3sivlVXdf446dwWnsWltmnc0TZg=; b=0Zc2WHqtv6uNB2dOqrkaPvjA+QFkTL8ByNiNfBV8y2/zDw3U+XyGhZilAr5Op77b6s 8c8lnpiPnEIxu3Ghw62oDPUozxDr44hZH21X7GqF8CkhgOqmGtHcwi724AOygJZTvvCQ yzdYaDI0gu88G3MXtvWMOLmLfZyWE96Y7yQp/WUi9NbqJ5yIC5yNPlRiY63QmZJYaV0X VIK1lj+Uue3lnl/IdEs9k/ocCTqxpdHJhoMxoZ1t2VLTlZ3Ldz+AZ7Hz75npmPyyt9G0 FCFIdycq40pZF2ksV7LCQ57Wxb+stSJTL6G1gqt11qMyijU//CzyGYpL2ofQdaVWV4Rj hMNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=lpqB+3ia; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=DZNpQcmb; 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=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w20-20020a1709060a1400b006e06b168341si1716500ejf.754.2022.03.23.09.49.57; Wed, 23 Mar 2022 09:50:25 -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=@suse.de header.s=susede2_rsa header.b=lpqB+3ia; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=DZNpQcmb; 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=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239367AbiCVRJS (ORCPT + 99 others); Tue, 22 Mar 2022 13:09:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237970AbiCVRJQ (ORCPT ); Tue, 22 Mar 2022 13:09:16 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7066371ECF for ; Tue, 22 Mar 2022 10:07:48 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 2B6DF1F38C; Tue, 22 Mar 2022 17:07:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647968867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7BHpLrva0gn3z4ug3sivlVXdf446dwWnsWltmnc0TZg=; b=lpqB+3ia31feVNsJ+xBeXoPm6qiplwLJhmVWhDZV/IyaXPbcHw3SGJXSzRSs4AC4A/GUng pBBP5SQ+ligtAPpdNJZTRXrE3fhB8LOcXqKJ7LFW4xGGBNLbjQpvNsX2O/aL9VemqGMYlB 53hv0qnsSQn2sro/BM4Y8R47P1laEwE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647968867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7BHpLrva0gn3z4ug3sivlVXdf446dwWnsWltmnc0TZg=; b=DZNpQcmbWVN9G8MQ1OCIhMSCIW7VkpTffNB6yrufNEyRTH9rnv6dvK62Km/q4i3NvKX5TL dXqcSVcCEbXCNUDA== Received: from alsa1.nue.suse.com (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id 25492A3B87; Tue, 22 Mar 2022 17:07:47 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Cc: Hu Jiahui , linux-kernel@vger.kernel.org Subject: [PATCH 2/4] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Tue, 22 Mar 2022 18:07:18 +0100 Message-Id: <20220322170720.3529-3-tiwai@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220322170720.3529-1-tiwai@suse.de> References: <20220322170720.3529-1-tiwai@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 In the current PCM design, the read/write syscalls (as well as the equivalent ioctls) are allowed before the PCM stream is running, that is, at PCM PREPARED state. Meanwhile, we also allow to re-issue hw_params and hw_free ioctl calls at the PREPARED state that may change or free the buffers, too. The problem is that there is no protection against those mix-ups. This patch applies the previously introduced runtime->buffer_mutex to the read/write operations so that the concurrent hw_params or hw_free call can no longer interfere during the operation. The mutex is unlocked before scheduling, so we don't take it too long. Cc: Signed-off-by: Takashi Iwai --- sound/core/pcm_lib.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index f2090025236b..a40a35e51fad 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1906,9 +1906,11 @@ static int wait_for_avail(struct snd_pcm_substream *substream, if (avail >= runtime->twake) break; snd_pcm_stream_unlock_irq(substream); + mutex_unlock(&runtime->buffer_mutex); tout = schedule_timeout(wait_time); + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); set_current_state(TASK_INTERRUPTIBLE); switch (runtime->status->state) { @@ -2219,6 +2221,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, nonblock = !!(substream->f_flags & O_NONBLOCK); + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); err = pcm_accessible_state(runtime); if (err < 0) @@ -2310,6 +2313,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, if (xfer > 0 && err >= 0) snd_pcm_update_state(substream, runtime); snd_pcm_stream_unlock_irq(substream); + mutex_unlock(&runtime->buffer_mutex); return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; } EXPORT_SYMBOL(__snd_pcm_lib_xfer); -- 2.31.1