Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2128067pxb; Fri, 25 Mar 2022 11:34:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxrZNyf71xoUOv0vbVMPL5zxWcbEMqqMUMgvhsC4qzoPht7ijnidSh6W4k0WEf62h8qE/1 X-Received: by 2002:a63:af02:0:b0:386:5aa9:a88c with SMTP id w2-20020a63af02000000b003865aa9a88cmr673692pge.533.1648233271011; Fri, 25 Mar 2022 11:34:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648233271; cv=none; d=google.com; s=arc-20160816; b=ug/bF3/U0r2AkQSlCIN4ZLDf69CJT5Aa5lBJwKFhCkSBgyCKL0OHDzWXLwK1lVZFuZ p2iKmb1Crl+7oTl9DUVD/YBztjfamiM6ICv+RxI9hl57XE6kysDpRzxW3plojrR3bkyG VWYvwuCBmKrTEFYnLnVa70qY3ykReYdMT/PWjbJaJLi+WQA+vinHjLJf/EywcqajIxXS glTwIpF0zkZYeAgua1xAtG/g1MM1fHg+k4LoWq/2N6de+BpWtBNNGorDK3XC1GHQko9m gRE1Peyf2eIgLy4BUuBOl2QP+IdCI4gizilhS1k9PopyyggV2y5E9CLSMBJl+P+jHvVj R0zQ== 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=q6AZUeMQcCTyPDs4L7nJhA1a/xcyd7ATxrrIiAvbPP0=; b=bb3sZfKt94js5B5ypkrIdYg8eFhx1WS73+M/UeRNk9pvBkAyYIcSQ9EPtI54Cr16WW 0V6JtgSdhuZM5OHJn5qKLL367ewAUJ2DJzI4jP0qkYFgi1uet18cUxCv1hfPyIDPbUy0 rdx+pIxYWSkcaby0t9dHuqXnG4/wlksknmtKDCCnccHG+1ogQefw4lDru4UEwUOHR3AG Hez94c5MYa2H8XrCXYrLGJm4VbAKJGgj7+hSFVtg1lA9794A6dHIvCU7Y5UpuAI29Cwa uwNAHF1CTUh5nr4R2e2K/U1vMedE77r5arDX8S2cdBXdzqBTwNC59K7bn8WECLqvPWLS CQgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fWGQqZZ8; 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 b10-20020a63cf4a000000b003816043efaesi2973842pgj.419.2022.03.25.11.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 11:34:30 -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=fWGQqZZ8; 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 082C218A785; Fri, 25 Mar 2022 10:54:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376456AbiCYPVi (ORCPT + 99 others); Fri, 25 Mar 2022 11:21:38 -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 S1376378AbiCYPTB (ORCPT ); Fri, 25 Mar 2022 11:19:01 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F9CCDFD72; Fri, 25 Mar 2022 08:15:14 -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 sin.source.kernel.org (Postfix) with ESMTPS id 51351CE2A52; Fri, 25 Mar 2022 15:15:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AA2EC340E9; Fri, 25 Mar 2022 15:15:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221301; bh=NHAaKMt9HrxNA462vkEI8qLSD2LtZZgfaKLsxWUH5UA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fWGQqZZ8dMvbXBrtuE5332sWbFvinIs1jY4gIaqaMnRPQqYlz9vnCCvOXoSPD6cTw SvzWwxdaJfLCTKK+dny2hl3nbKRIJN9B4+lrAQE8zs00dpJMMRxpzUuRBAvA1RKa33 iXC73ynFpdX9EyLychIc8330BnHFkTv3dEfV6280= 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 14/37] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 25 Mar 2022 16:14:15 +0100 Message-Id: <20220325150420.341990836@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 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 @@ -1905,9 +1905,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) { @@ -2201,6 +2203,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) @@ -2288,6 +2291,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);