From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD6ADEB64DA for ; Mon, 26 Jun 2023 07:33:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229766AbjFZHdy (ORCPT ); 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 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; }