Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp15798429rwd; Mon, 26 Jun 2023 01:13:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5hBatuX2nQWy1Q05rcX7mt/fGH8LYBmuA/qd/GqoZqZQ8KivJywP4bCFWJHcO0vxMMqz/9 X-Received: by 2002:a17:907:7e82:b0:98e:b48:24fc with SMTP id qb2-20020a1709077e8200b0098e0b4824fcmr4807188ejc.51.1687767198599; Mon, 26 Jun 2023 01:13:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687767198; cv=none; d=google.com; s=arc-20160816; b=zc7ruQES7gO7PqHTlwWJu658El0SMkddgEsfiW76nGNglH4rI9n7iOOl4Z2UjOw6kO GZ1hyDDPUe9Pu/lxWZZdI9EJYtqLVd923eNfP25z30Z9osv9I4WgJhj9ADscjRRkoV7a ecRrh1vJzWxGu1fGeNBNjEE/qoSOL+TxEgpd1fgSM1qHOqxLT1EQgvOrpiLKvCuUd6M9 F8pKBfVVZ65J0+HEldpX+Fj65B+Qp84gPgABYAms6SIrXSt+4Zb3gSbrVqCKVlsOtuZs HRoHfYCw07DXFvQULukcXeBFZ7SPvV1vgAemFiAVRirfZeaS3kKZuTbogI2eZmpuZqe8 sz9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=5VlDdmYvDqnDzHA7zfHHZhmopchZjN51DD43ktpiPIw=; fh=oG6tSGEvkhM7xTHXU0ryd4jOyFNG473KUPk1N+B8nLU=; b=SPPMYztq1IFDCQdH/uxkOXcfuQUsQ0ZBd69VUbEFkU0t9hqdDYo315fUtVAMWaHV4A aNn70EYr+Kh3j7YMgD+C0mArc+WUZsPXSQrdDWdvH8VpiZ67I22UtAOcR7R2U9IVrn3l 9HMwQVbUUrxkRhwI1ClFZR/oxBXzegJqSzIyDf2mvGJurAtKp/igld1RDwQZPwg1d1AF oNJNi1ami1Jex+S8Z/4cjXHFqiYhgeE9m2UIwFiYVagBTSj9nQqIG1xXpzGTtFs5hk4B jqWHoxxnQ41x0ADm8uYEXOybVX+ZPc0EG7NwjhJ6IHAkRsIXn9td3XiApQyGoaClZXS/ QoVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=qQE9c3pS; dkim=neutral (no key) header.i=@suse.de; 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=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ec10-20020a170906b6ca00b0098cfbd83b9asi2605301ejb.776.2023.06.26.01.12.54; Mon, 26 Jun 2023 01:13:18 -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=@suse.de header.s=susede2_rsa header.b=qQE9c3pS; dkim=neutral (no key) header.i=@suse.de; 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=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229689AbjFZHdy (ORCPT + 99 others); Mon, 26 Jun 2023 03:33:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbjFZHdw (ORCPT ); Mon, 26 Jun 2023 03:33:52 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 586A8102 for ; Mon, 26 Jun 2023 00:33:51 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0C5F9210DF; Mon, 26 Jun 2023 07:33:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687764830; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5VlDdmYvDqnDzHA7zfHHZhmopchZjN51DD43ktpiPIw=; b=qQE9c3pSc8pg9de3la3h/ajMjs6oJW+3ObjddNmhfZiBamXWmdbAfSuFh0B0ffjNL0xoSz pn/IYIS8uZNScyMnXh0uPO6zft6QvpfkCXljplhbpzrTPPtQnnJaIrnfFfPcPKafvlKOu1 gVarCKidHAaY4kA6n7TZX0Es6HaZJAY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687764830; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5VlDdmYvDqnDzHA7zfHHZhmopchZjN51DD43ktpiPIw=; b=2uFouN1hTLWN7Dx9ITQ0MzlSyuZU0ZSuKE6t/px02D7s6eWmYLFAEl+2FXWlwQc7G/YTHV tQg3sL/NqNtstCDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 89E431391D; Mon, 26 Jun 2023 07:33:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id quHPIF0/mWRCPAAAMHmgww (envelope-from ); Mon, 26 Jun 2023 07:33:49 +0000 Date: Mon, 26 Jun 2023 09:33:48 +0200 Message-ID: <871qhywucj.wl-tiwai@suse.de> From: Takashi Iwai To: Tuo Li Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, Linux Kernel , baijiaju1990@outlook.com Subject: Re: [BUG] ALSA: core: pcm_memory: a possible data race in do_alloc_pages() In-Reply-To: References: <877crqwvi1.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Mon, 26 Jun 2023 09:31:18 +0200, Tuo Li wrote: > > > Hello, > > Thank you for your reply! FWIW, the simplest fix would be something like below, just extending the mutex coverage. But it'll serialize the all calls, so it might influence on the performance, while it's the safest way. Takashi --- a/sound/core/pcm_memory.c +++ b/sound/core/pcm_memory.c @@ -37,20 +37,22 @@ static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, enum dma_data_direction dir; int err; + mutex_lock(&card->memory_mutex); if (max_alloc_per_card && - card->total_pcm_alloc_bytes + size > max_alloc_per_card) - return -ENOMEM; + card->total_pcm_alloc_bytes + size > max_alloc_per_card) { + err = -ENOMEM; + goto unlock; + } if (str == SNDRV_PCM_STREAM_PLAYBACK) dir = DMA_TO_DEVICE; else dir = DMA_FROM_DEVICE; err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab); - if (!err) { - mutex_lock(&card->memory_mutex); + if (!err) card->total_pcm_alloc_bytes += dmab->bytes; - mutex_unlock(&card->memory_mutex); - } + unlock: + mutex_unlock(&card->memory_mutex); return err; }