Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp937642iob; Fri, 13 May 2022 17:06:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzTShRzjkjHqbMY7G5awK0ucEy5QJvaB7yEYiAIL/cmkFlceurCNlEczdDR+8vMzlK5C7o X-Received: by 2002:a1c:4e08:0:b0:393:fd06:c2ce with SMTP id g8-20020a1c4e08000000b00393fd06c2cemr6719824wmh.91.1652486773924; Fri, 13 May 2022 17:06:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652486773; cv=none; d=google.com; s=arc-20160816; b=pQoR4R9iXsLFeed50PWxXZET5y7Wz83fDH51xDhshZ01MQ4GdUMzoLq2ZHkAHUKxho fmGlTt8y+m/7pnxjbLYvHO1tbJNOfohhoqh1hbaoH27kNMXDRXMWJpG/IU9KihwG1Gwg 9ZAnqn0dl5XnzkDDHqo7tjLo2Z9f7JXqQxPa9zutlBRjSUopMuDugZ7v3Oj7nr2aj/Qu zr8EsyB8OqA74yHl4EeXlpjRKPXn68N8yzbibGd5SwXzm+AxN2Tptx+GIRPDwiAgOlFl R2G/VwGQYPn4Ius+ZzSxuxwdgvPzlFL+88GDdWAANF7h1S1HuFfCYarL9xsMdx0MNLjr VKdw== 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=bUp9B0mMZVqZUUnzBGsaVOrn2moT28pfnbQBkkxnJfI=; b=vdue/8zmT5hJMw9YTDkq58+I3amaG0A05gqoV7VEHErQWq/wq599m6G/Pei/JlKJbH dHl1d9aEvsdhvCoaSxCW4KdfJFVaJz3do5WlSF4aqlWx9hrCXWwmctL1RU5BZE5VJUgM U+4TVh1Iuig2x78gwkYux+6N3clyYhbwTGRRxpNMBEQdE9WYuJ0AzCFD8CyuZ6fUTM7f V1e5v+SjYc+5CBq6pmvhBkOQjvMGIIv2W6vd5SaijAyhcgCXUkgAd4mt6o9Vu+ACXjAc OOGUBGoLdU/Jx7dzngi0nU9Ux1DRO2FlGXcSz9Y5mH58aKZz+8ic/EnCZqiMg0r6leyS zJ0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MDSyw7EU; 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 f6-20020a0560001a8600b0020788ef5e23si3731745wry.584.2022.05.13.17.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 17:06:13 -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=MDSyw7EU; 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 0C0372CC13C; Fri, 13 May 2022 16:08:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380916AbiEMOaA (ORCPT + 99 others); Fri, 13 May 2022 10:30:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381085AbiEMO0b (ORCPT ); Fri, 13 May 2022 10:26:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0244850B24; Fri, 13 May 2022 07:26:19 -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 A3C14B83068; Fri, 13 May 2022 14:26:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2767C34100; Fri, 13 May 2022 14:26:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652451976; bh=3gwdWpVVxSY2RAbkBx16m29vhyxddLubsNCdmoGDlqc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MDSyw7EU8tbTpgUnKuMQuajHU6EkNHMcSVrmKXGjte4BH29hrnHM39J3aqqigw3z2 hg3/yWjxrGB+jvj5aDUztfAi2Ai7O7+ypniOHyti4mG3WqBNVqkingIrJ3BVZ2AF0I UtDN2qDbdte0nQ3PyNjF/Y7+ykstMXRkGKuSrdto= 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 4.19 09/15] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 13 May 2022 16:23:31 +0200 Message-Id: <20220513142228.170275746@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220513142227.897535454@linuxfoundation.org> References: <20220513142227.897535454@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=-2.6 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: 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 @@ -1876,9 +1876,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) { @@ -2172,6 +2174,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);