Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6218809iob; Tue, 10 May 2022 13:07:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmfgJpNgmDZGQf6TX0SdfinzrXMALWm9reSv1/zw0l3Slp1Rw2inOm+ZbczE7hDF4JS7Om X-Received: by 2002:aa7:da12:0:b0:427:b434:a374 with SMTP id r18-20020aa7da12000000b00427b434a374mr24408880eds.408.1652213264658; Tue, 10 May 2022 13:07:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652213264; cv=none; d=google.com; s=arc-20160816; b=HAbH1+xxKDE0Go7nNB9rzkJZMUzuIa3Xw/av/v1P8h7SHboBf0W5RbvZc0bWiPaOc/ 8Idv9XxRM0q4mjYNO7vP0AY9ReJtmaCZQqydNFLBbdHfUsGWiTa2xfVlyam5R04UKqjT FCa9cL4ZubUHaHubYzcpYc0k2SVmdGJ+qU6u3U8bSo79p+ZScz1gLsolEpWLUs2mooad gJN113I07eBqPIBNkgPyutCEkW20QKr+qFYWOANkpr4n7AkVNvOKUK8RKgPnKvZZZmdQ ijZfAU/WGFVS+5XKedfviQZdqcYHO6YBl/A1gQMVG+KUjrxikY7kQ7UUjoQBngjQjbbD 8Hkw== 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=ByG9N8S015/p0WoWvQi0j8vrcVIVNNzFaIVr4obrkkU=; b=QjNne47RMX/pgOrrmuTTZJsGZH1P1Meh5EP3KlNLNBeGFq+Nl85b13xld0UH9hyfx8 Qf8oa4X8VLDz4DFosB8pWqtAdZWrltHtDeq7s6v7A1/+eJmNkVJ9/pKu9OdtRH1y2B9e CCICPKIXi70qGxZYJJ73cuMLGrIa3nj7/SINtsHfHlH8+WVg3cse6u/rQP3kyJsVDEJ+ Z4d1kJhGx/VfZN+mJOTqTHHIeeLQHvAY8svNJ4kG/hvPF/5+U2cf4N0yVksa7HNk3u/F lovOp2600+fuaB5BA9/wHXnwk/0XKqgQ1WytW2a39vEObnH0mgoRbIBCcWoNc3zlndZO Y00Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uG5P7bC3; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs17-20020a170906dc9100b006e8bd9bf296si335532ejc.279.2022.05.10.13.07.19; Tue, 10 May 2022 13:07:44 -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=@linuxfoundation.org header.s=korg header.b=uG5P7bC3; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244945AbiEJNvf (ORCPT + 99 others); Tue, 10 May 2022 09:51:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243456AbiEJNeM (ORCPT ); Tue, 10 May 2022 09:34:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57657238D75; Tue, 10 May 2022 06:24:30 -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 6C3D761768; Tue, 10 May 2022 13:24:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 724FEC385C2; Tue, 10 May 2022 13:24:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652189062; bh=dwo1c1VqB1TjhnB+lmO0evpTKYjAmpjBUdoni3XMw14=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uG5P7bC38VJFI5zBaFd5K6kf30OwVzI3+J1FKTrNBYrNbeC3aktHoP0r92ey6A23y RZ9RflW6axqvC2Et0BiLIITkkzvZWmcmLqQZJNygytgFmfsgBiCRt2akiL1n2dBQp6 Of5rmVAr6490CjLZtFAna+ADjJUekCF3y+DdmEEQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Jaroslav Kysela , Takashi Iwai , Ovidiu Panait Subject: [PATCH 5.4 43/52] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Tue, 10 May 2022 15:08:12 +0200 Message-Id: <20220510130731.112125943@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130729.852544477@linuxfoundation.org> References: <20220510130729.852544477@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=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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: Ovidiu Panait 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 @@ -1861,9 +1861,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) { @@ -2157,6 +2159,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) @@ -2244,6 +2247,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);