Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2195036pxb; Fri, 25 Mar 2022 12:48:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy916xCmApdEBrnN8RSyEd/2fNTAniF1cr4Dwkmy3yucXSy4hNqFOV8XJdHbJpRNXBnfYsL X-Received: by 2002:a05:6a00:c94:b0:4fa:e134:4e6f with SMTP id a20-20020a056a000c9400b004fae1344e6fmr11521522pfv.66.1648237723650; Fri, 25 Mar 2022 12:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648237723; cv=none; d=google.com; s=arc-20160816; b=rrTuldolKT8V64GO4mGSvKKQYaTxTUyRd8O8kSkh0QRVnwK6mrdLXrA+Xfi+tcNdnU rR7EHfg3/1db5JPCvslbnzjOqgzy4jVGwutNwizexzD+8ZI03iX0ppMxxtONOC/C/T2D e82I+gnfJcOaj22w8BXG+zXxAsFY2UcFmnrMLkeCvmX9OSQoRkm8NrPOUfdZtcJ9uaal IN7OL17ttTpdAzjG8UF2+EUpCzgVoOXDpkjN+xfX4DioV0DAJuX21IeK05VDCc0IdJpy AZJavUQvvWM8E4YoBJGY4QDMnr9AcxL4KK3QNCxqjlCfLxeMl4ZXaCiWUe5NaxaIHO0R WtdQ== 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=FskUGQDsu63I+wpYSv9Jeo9t76d5MVdDhc3b9GOQxHM=; b=RivoX+d1z3lFeg4Cpv71Dk81cZNiNu8xMK0DREQQY7kyRNkaR3PK1wYP6os9InFd2j /C+pTe8DACeTtAk9MBDHSO+mhaCXyvrsBrQgbfo+CQneDdH0J0lovAHOt+KUZ2V227sD JO22HYn2ajlQ51OxlMU+71Qk/UaWMJlX/liz5NcdUclUdVTYPbu8E0oFsrslDJ+1P52m PiOWc4yOpXmOADEJB2hiREb0CgTrzYJM1u6cZO7cCPFABfDhlyWQHioCQ6U2XVVJctWF muyenUF51MsLgkDKHUAZETICZRTLLSHu7d1u1sefChL7qm/ep0wujwy6Utiaqixv7N0N g09w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=h45ZF3wL; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id h2-20020a170902704200b00153b4fd714esi3054765plt.251.2022.03.25.12.48.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:48:43 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=h45ZF3wL; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 47E742C0B4F; Fri, 25 Mar 2022 11:38:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376289AbiCYPVE (ORCPT + 99 others); Fri, 25 Mar 2022 11:21:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376356AbiCYPMv (ORCPT ); Fri, 25 Mar 2022 11:12:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75C6413D76; Fri, 25 Mar 2022 08:09:31 -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 26C20B828F8; Fri, 25 Mar 2022 15:09:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8CE88C340E9; Fri, 25 Mar 2022 15:09:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648220968; bh=tnLkXIXuSNDabdjWsHyEMEvhpKFf5TCf644Vn+A74JA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h45ZF3wLdgiUEVqJgNryaxC0sbDOg34pFKMIRAd9pWKCz+FbWSuAkWzlS/HoC/EjC 4dNz+Hjy0TyLey7OhsP0r4rbJSQ0Y9InfgjKmkauGdQ5nDN8PczTAtIF4ReCWjIxcY UBw0WWws+0cQNbpDTT7uirwrzpC98d5CVY0hRJhk= 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.10 20/38] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 25 Mar 2022 16:05:04 +0100 Message-Id: <20220325150420.335498459@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150419.757836392@linuxfoundation.org> References: <20220325150419.757836392@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 @@ -1871,9 +1871,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) { @@ -2167,6 +2169,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) @@ -2254,6 +2257,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);