Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2175338pxb; Fri, 25 Mar 2022 12:26:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3y2Os4HQ61y12E4+O489ZIjqSH4dK2S4CQjkpIeAvvd/ICVZbEYECOqSZRUUhhx58fxwR X-Received: by 2002:a17:90b:1a81:b0:1bc:ec26:40a6 with SMTP id ng1-20020a17090b1a8100b001bcec2640a6mr14568497pjb.0.1648236376762; Fri, 25 Mar 2022 12:26:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648236376; cv=none; d=google.com; s=arc-20160816; b=kmzPaTHX3yBWlSxiBE5WHGqMlrN+w901Q5Zt/ddDWiQyQRNthSKncFrEOtr92D0/fQ KEdhjK2z2Cbur8wW5QYIpcbTqueab2LWBYN1zRAUSRRFaRTYzrP/s+7KH0eOSKN4A0c2 qswcpmvhAgbAJsJTZPCDyuaG6SzmgLj9tO1ogS3d7op87UZN2uyg2gbTfUrY4IvzgHTg I8ZEdvpVWgv60356P2yRFcNHzBQ0eKv2F6CFFefIju9zqh0os7qxrT9zhxu3IS/1SyfX OCExmofavOFtaG15fj8PpIbDpW2cPsQUrWnl52bruIt0j7nxkLPlyW2KGXH4FO+2sJ9n n+cw== 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=7v65+peC7QtZCeosmZUaN+JBLgDAT+Ju5kHt7UYOnZY=; b=BNRG44MvmelMy3XWWvAYuYcuoObARk5TvDdWTrg2iuhTRTvJ1p5h1FiPVvnBLBgjWM UZUuxZwM78l6CMyyvZV4Uc+RNJkYPJXOy4RvDZyGkPynHbzUsBPGd+lBl9r7kHuAEb2J PC0k1Fq7ffqWZmEYXC1B0d/pCjFVnpUQJc6iJUtbvImf7xsHJSEZSpxPk59R42rH+nLm kl30HpcqDX+HhFimoLGDZvWQyzXKC2lMpf1JdBGtx2HB1O8lFJe0yBPB5eenWcR271rV 2nubdch54FaFSuVrO/Q5bWEEdiDVCSjEx66HA9bjAG2LWGOOfCor/F9B/eif3RA6N18y DBoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vZrcUGsN; 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 cd20-20020a056a00421400b004fae5444274si3459791pfb.212.2022.03.25.12.26.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:26:16 -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=vZrcUGsN; 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 E26C42A5AA1; Fri, 25 Mar 2022 11:35:14 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354133AbiCYP1u (ORCPT + 99 others); Fri, 25 Mar 2022 11:27:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376896AbiCYPXa (ORCPT ); Fri, 25 Mar 2022 11:23:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FE6192D3E; Fri, 25 Mar 2022 08:16:58 -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 C9A7BB828FF; Fri, 25 Mar 2022 15:16:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31CA3C340E9; Fri, 25 Mar 2022 15:16:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221400; bh=tgEkpY98C6OTEYG4vogZevvzNca7fU5s/7C33kqAar0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vZrcUGsNjwbm9TcYVhLw3KMSzZKfeWA9eYAEYlxFsYpBzVx/p1uk90XSZxItHfuGO 6yppzrMczho8y7RWNrsR8YyudUGB+7FmDWiWZ0zkTNCHnT7dcyPP18jMdxctzqEqtA XvEBEgdEcuX1ItYc9Im0yVAiLq+5e4JyWye1zqqA= 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 11/37] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 25 Mar 2022 16:14:21 +0100 Message-Id: <20220325150420.370767468@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 dca947d4d26dbf925a64a6cfb2ddbc035e831a3d upstream. 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: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-3-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/pcm_lib.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1906,9 +1906,11 @@ static int wait_for_avail(struct snd_pcm 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) { @@ -2202,6 +2204,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(str 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) @@ -2293,6 +2296,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(str 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);