Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp15987867rwd; Mon, 26 Jun 2023 04:21:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6onkxSvbbqvZPCkmGY+/xoVqXJDtD2xflDX4xanf7cNgEzyCd10nuppRlZoxxHNagKuM+T X-Received: by 2002:a17:902:ea06:b0:1b8:50d:9dd9 with SMTP id s6-20020a170902ea0600b001b8050d9dd9mr2122267plg.51.1687778499719; Mon, 26 Jun 2023 04:21:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687778499; cv=none; d=google.com; s=arc-20160816; b=hGBA0YC78D1m3kqUL8ome8QrjqoVdg8OItRRQHk9FMx3ynuhF2HtPt68CUg8PsttEp 3ifv8LnpSN3ebVt423/yQOV+WZpSgvWdPrdihrTLH7XTQAPKoihXY1gWcbuUpOzfFvz2 Zq6gshaiBM3abf3zZK/qjsBgQVNz2Q9iYlHPGNHg0vbjk2iY088ACkqi6+FrzNMhQJ4w /8lSeFNzSTuD59KdaYxFyoNjLRRDRdv3uP4zkEP/WlwBOptPjImsloL2bl87qMt26RzU Sidm8NkXchgJptDH/JEqL2Mc0eH3WSBplpJitCx6kxbqc9KAoAS1HGEaUhmNm1asPC2Q JzcA== 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=nbqsb2L4j9asp0Yxl6Eyl8kIxlFivGaAHwXM+9WqDB8=; fh=NqilcjSZH/AWx7IwADvYlFuAx03+ZswMmioal0+Tdh0=; b=BqPsO8Fk09RvT/cBh10qxVeH2s8HgIUnluP7i3iQqXcZ2VZzTe3OzHHbXS3txQmf8U kqycYuhtyu1+/JFJjPuOWXPe94CA8w//7j8jcuyyi0WoKXOWGBwgh5vgPGStx8QOg8P8 p3AT2VTBCOJDcTnpwmfxbPhmjdjobEUOFTWZkj8df11VGAGRU83SgCNnUHJrkBed+0iD fxKNvj7R+GILjOeTSjPSMrJvPL3qt8a5QNrZqxDAybzzNfpPPdg7TOqSj4uaiA646RsF mRt4KinC9NW09wIFxv5VpANz96HQAMsvfXdh5Lqq1D9CXmJxVqgwNImSTvwk9E9qbwfk m6VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=eFEd58D+; 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 l17-20020a170903121100b001ab1fb8cc83si5161889plh.161.2023.06.26.04.21.28; Mon, 26 Jun 2023 04:21:39 -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=eFEd58D+; 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 S230087AbjFZLCv (ORCPT + 99 others); Mon, 26 Jun 2023 07:02:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbjFZLCh (ORCPT ); Mon, 26 Jun 2023 07:02:37 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FFC0173E for ; Mon, 26 Jun 2023 04:02:17 -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 EC45E21861; Mon, 26 Jun 2023 11:02:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687777335; 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=nbqsb2L4j9asp0Yxl6Eyl8kIxlFivGaAHwXM+9WqDB8=; b=eFEd58D+kQOIKSAmvIA/C7aJUK7aNO+6TWCMdriqrZVwF9YSoFWvnNxX+x++SortqYOOEM YGIVJ6GL6ZVfJBNiOFj3jJy3vEbID9mp/+rRps29EvDxWkvT5p+Clwgj7EIHc4XIFuRlH5 dMhD9xlqsSx/xxj/OxZ+g+Ll/EVVpjA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687777335; 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=nbqsb2L4j9asp0Yxl6Eyl8kIxlFivGaAHwXM+9WqDB8=; b=wRzUWXpVfr9iB3sJfGLeJ8W6oHK2/QELqTR5N7/VRDS5HoKSF3VRHNxtfPnQPy8Ce7NBXG /n4aqOCP+BYau6Cg== 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 BD86713483; Mon, 26 Jun 2023 11:02:15 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7aHdLDdwmWRXHwAAMHmgww (envelope-from ); Mon, 26 Jun 2023 11:02:15 +0000 Date: Mon, 26 Jun 2023 13:02:15 +0200 Message-ID: <87wmzqv64o.wl-tiwai@suse.de> From: Takashi Iwai To: Jaroslav Kysela Cc: Tuo Li , 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: <4d0931bf-b356-6969-5aaf-b663d7f2b21a@perex.cz> References: <877crqwvi1.wl-tiwai@suse.de> <871qhywucj.wl-tiwai@suse.de> <4d0931bf-b356-6969-5aaf-b663d7f2b21a@perex.cz> 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:56:47 +0200, Jaroslav Kysela wrote: > > On 26. 06. 23 9:33, Takashi Iwai wrote: > > 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. > > It may be better to update total_pcm_alloc_bytes before > snd_dma_alloc_dir_pages() call and decrease this value when allocation > fails to allow parallel allocations. Then the mutex can be held only > for the total_pcm_alloc_bytes variable update. Yes, it'd work. But a tricky part is that the actual allocation size can be bigger, and we need to correct the total_pcm_alloc_bytes after the allocation result. So the end result would be a patch like below, which is a bit more complex than the previous simpler approach. But it might be OK. > Eventually, total_pcm_alloc_bytes may be atomic. Possible, but the same problem like the above applies, so I believe the mutex is good enough. Another alternative would be to move the size check after the successful allocation, assuming that the size exceeds a very exceptional scenario. The code flow would be a bit simpler. thanks, Takashi --- a/sound/core/pcm_memory.c +++ b/sound/core/pcm_memory.c @@ -37,9 +37,14 @@ 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) + card->total_pcm_alloc_bytes + size > max_alloc_per_card) { + mutex_unlock(&card->memory_mutex); return -ENOMEM; + } + card->total_pcm_alloc_bytes += size + mutex_unlock(&card->memory_mutex); if (str == SNDRV_PCM_STREAM_PLAYBACK) dir = DMA_TO_DEVICE; @@ -47,8 +52,18 @@ static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, dir = DMA_FROM_DEVICE; err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab); if (!err) { + /* the actual allocation size might be bigger than requested, + * and we need to correct the account + */ + if (dmab->bytes != size) { + mutex_lock(&card->memory_mutex); + card->total_pcm_alloc_bytes += dmab->bytes - size; + mutex_unlock(&card->memory_mutex); + } + } else { + /* allocation failure, take back */ mutex_lock(&card->memory_mutex); - card->total_pcm_alloc_bytes += dmab->bytes; + card->total_pcm_alloc_bytes -= size; mutex_unlock(&card->memory_mutex); } return err;