Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2199587pxb; Fri, 25 Mar 2022 12:54:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwv0hoQsmI08tbdYUn7g/pWcGamUTKNY1ZCxyBWMuhBDwR90cMCSN1fHSgplOpjEn/T6qnB X-Received: by 2002:a62:582:0:b0:4fa:e56c:42ac with SMTP id 124-20020a620582000000b004fae56c42acmr11814278pff.24.1648238074551; Fri, 25 Mar 2022 12:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648238074; cv=none; d=google.com; s=arc-20160816; b=yCFee/RLrVNr1Bhc0Uj9HHqxONVJbHtWCTlCs8PTlJMbOa0RFJXfByget1vSH1NzLh ZZ7PPeLJ5z+eCIbeXn2BK53DsZoTJrj+SRpvlbu05E4BMJYI+lLLSinG3nc6XykkppdL dp51HlXtzAPYow6rh15o4PKy/Z32hjobx1Zwr1vAmq1nyZPGpSy2AVMIPgopmjl3m5Rl A6eCMgTPJFupbUz8mfFTKX2yJFFj7YS9UI1rPnnjYOPFlT9ZArz4jcZIWGnXjXbFr7ZV 80/bE7LoOIC+ZtoK+GlTE1JNsowl1mh++TxC3o4SP2g13eXRe4t1RZ8VdPhHr6u4s2kd JyBg== 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=RG4LIKoTp2FzqV6nlGiPjVA16xRpTWE2Nuu93l7saH8=; b=wuMCOCwhDE+2vKArHd79sELwWeP1j2pcyKRVq/dlSPcAoZIt46747P4IAXX75MSfoF ofJF0MsRm6L0aY6W0rgdOXffEd3rPJvQN9t1ZvukoB5AScBVLL9f+DLKfS85am6eJ+2j o6ZkFmAfq4bcLqhyRZ6vF9ydptIRvUtzZuhGzRU+28ECC0BX3IoLZD76VpbN6n0MJBLy Ty1xuzbmZZbds2g6On/Kg1GCqNJQAabI2bMA+Ieep9mOVm42uPUGeXHE6pxtN1IQlYT8 QCbbcdRwFDSxEi4x3tO8ZlzpGGlSfv6a68VfKk4yd/JFrEM6Ue0Xb9Vm2eYvAhCCMUx0 QrDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oqj9+nwY; 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 h16-20020a17090adb9000b001c785d86222si3378039pjv.45.2022.03.25.12.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:54:34 -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=oqj9+nwY; 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 972B52E51A9; Fri, 25 Mar 2022 11:43:34 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378653AbiCYPfT (ORCPT + 99 others); Fri, 25 Mar 2022 11:35:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377688AbiCYPY1 (ORCPT ); Fri, 25 Mar 2022 11:24:27 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3EC8E7F74; Fri, 25 Mar 2022 08:18:33 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 0CF3760A1B; Fri, 25 Mar 2022 15:18:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 258BAC340E9; Fri, 25 Mar 2022 15:18:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221512; bh=rrWqd7n1HnCx08aNN7+eWFvJM9W2CYPP36O3U7lJ3H4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oqj9+nwY5kT3EvtLJj9pn9/vdAbY8xmEmRfDC6mWIAmBhiugVurEduR8GwxTBdu2Y A7ZhiKRsoq3HY3wixxhcTznxO8ZtTnQoOsQSr0+7XrtVeq0GpqCZlwBDw394/KWeNG ES+bboNixxn/6XIVM1VNjkTl50ZpRDupmyfPz/UY= 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.17 12/39] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 25 Mar 2022 16:14:27 +0100 Message-Id: <20220325150420.596152299@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150420.245733653@linuxfoundation.org> References: <20220325150420.245733653@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) { @@ -2219,6 +2221,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) @@ -2310,6 +2313,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);