Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1016144iob; Fri, 13 May 2022 19:44:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyELPl/JMXQ9Yr4ZTgLBqUnWpf2CcOCF5rnfbqFyyQSNSeHsiPpKCabCfOiuSL+iPleGaN X-Received: by 2002:a5d:64ea:0:b0:20c:5cb5:b34f with SMTP id g10-20020a5d64ea000000b0020c5cb5b34fmr6262326wri.508.1652496279228; Fri, 13 May 2022 19:44:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652496279; cv=none; d=google.com; s=arc-20160816; b=tg8O6+VwJMN8GhyaKJwy41NhqEf+m1zBA+M/ZrHSr3NSdfTOjttbd9GyuCNuT4QaRu sgo2K0Q3p4y+dXqhwN7uex413KuD5jmQm9RtIDY00vyFTyVhxZoJKxcDRMZwIJT36G0e txpDKgdytmJo3aY5P8cY9TcdXsynqSTEtqtJZTeHvv3RdOP0Nxvm+Mz2FKHsNKUMv5Wt 2Qkzg2CWwR0WlkVkB3ys/Nn+l9lRsJ+uJRkNkOTD2dZgxN45/nk+gAvfEGaHo/yoKVod xuxsnxCvHsNeZAW+Xgfhnx22sNxk/fBaH4HIfYkVzBpCgK5H6nSQwfgMExqq7udbpC/5 id4Q== 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=Ju+WdGljuaBQZ/Og9X1Nx/XiG6pnL7StWAbZ+XMo1j8=; b=ysE3/qj6LHH5s5soIs0S/gtstE8jadF2RMldTRxRm99bdhO1qH3E3Fogwcb/KA4HIs FiZwQiS7yiD8UxOLfw4lsms9U9lQ/oTNqEUwtz+FyQ8ZCnY3WrQPO9/a8sJuOr2WPfO3 5758P28uUBijApbTy4hAHhm5U+4G2DU4MciXjx0ERg+v5dOWMJCMOZF8X9YfGtb65nnZ +yXRM9KvnqK7MaDqjnVsUI7F4iDUoj9G87MTYYjwpXdv1En1bKHZOpDVwnUhQRfClBSC 5Hh7kn/KWdx3JVQrfHvvU4FvkMSvm6R68MC38WIHN2ULVp3TnMMaSndHEjy9DD/V96TL nn/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=15HROKfr; 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 p11-20020a05600c418b00b00394083bff91si6063107wmh.93.2022.05.13.19.44.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:44:39 -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=15HROKfr; 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 6E603359BE6; Fri, 13 May 2022 16:32:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380728AbiEMOZt (ORCPT + 99 others); Fri, 13 May 2022 10:25:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380051AbiEMOZD (ORCPT ); Fri, 13 May 2022 10:25:03 -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 63C685EBF5; Fri, 13 May 2022 07:24:54 -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 03F60B83070; Fri, 13 May 2022 14:24:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CF59C34100; Fri, 13 May 2022 14:24:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652451891; bh=tDeMGXYetPdznxd8T4iTYspronwQhS+2uCH0+5cPBrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=15HROKfr6i7NokElj4sOyhIevhl40PWHHA6EJUozGOUjGoBVcp9YTxATe/9b/Dxvr S8P+XxIWaTLnKuOdNyr7vbJEHwcockMXP9YFNoO18QrBM8voYRPdqclC3XIQSFkbGm Nf+gZfyk2Ze6PdrWlubcSxMYRdyCR4uEtlWxV/fs= 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.14 10/14] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 13 May 2022 16:23:26 +0200 Message-Id: <20220513142227.687032320@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220513142227.381154244@linuxfoundation.org> References: <20220513142227.381154244@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 @@ -1878,9 +1878,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) { @@ -2174,6 +2176,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) @@ -2259,6 +2262,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);