Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp16159107rwd; Mon, 26 Jun 2023 06:34:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74n/XqYUedqo/TQTeBpQ/kGDoVdhIP2shQ/YAxnwH0eZPeYmlAdUBSShTc4zzG8SRUqZcv X-Received: by 2002:a05:6402:1b0e:b0:514:9df0:e3f3 with SMTP id by14-20020a0564021b0e00b005149df0e3f3mr22144464edb.0.1687786466963; Mon, 26 Jun 2023 06:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687786466; cv=none; d=google.com; s=arc-20160816; b=T/QYxwVnuoc7droS7t+X87C1NX91K6DyNu59w2qJVor/rZmN9O/kGNnNVkroObGjn/ /ZBhm6ZlEu0aw76kzlWBgHYuDqmMDHc4vHdvANzD8E8NABpYOat3Td2P/qkClccnqMRc POeBHSPV6Ra5im1xengAO1cJ8dQDgYC28E3aAOEfA7mYx3RjL4sKudZblBlxBVAcFRv0 mSjiFaReyDlYiAjHqrAEupHYqYkQLzkqJ9PbP88q2SgBZ4BTKvkiCIjGH78vNo7bQvWU jTCPy6eCTiJKRgk8hqH70N3GXN7ZRn/TeDUinHjWo/+tfZSmrP6aq75zuPwoMkGLeewt 7+/w== 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=3qcdlrRe7UNQ3MoyAvsaAyy1Z+/jbolyeeT23DgRd4g=; fh=NqilcjSZH/AWx7IwADvYlFuAx03+ZswMmioal0+Tdh0=; b=Z6WWIkcqP6In/G6guDqSGIkEwrqiX1Cs7wYNtpERvmYxgm/cHfwGurpU/36dxKrOiW 20h2QaD2TznYqlZE4q0d+Jo/k/BvUyFGEg9tmkOajI7NZV4ZHLImiJ2q/nrFE1IPSMxw 1gJJo5a1bHCjUqoU5TZXc1z2hLq5X/hSVu4eGCs59OVCIp2FTw2w7KJ5mNrYMt8fUkO6 d0dMXg5UKSaebWTOPMBeV4UL1XpYz/3b0xRZmZNAU0pYd/t6bnzjNteUyfCzrPg3rzK8 VyrzuYEaMxn9zzwWTEq2QtWdKRG0PzsGXmeNexWgAocusbpoGyYkRg25JWfLWDRV/Wwe vQ9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=fzZFAPE0; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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 n25-20020aa7c459000000b0051d81879768si2386321edr.658.2023.06.26.06.33.54; Mon, 26 Jun 2023 06:34:26 -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=fzZFAPE0; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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 S229573AbjFZNPe (ORCPT + 99 others); Mon, 26 Jun 2023 09:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229899AbjFZNPd (ORCPT ); Mon, 26 Jun 2023 09:15:33 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A267F1AB for ; Mon, 26 Jun 2023 06:15:26 -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 48CFC2186C; Mon, 26 Jun 2023 13:15:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687785325; 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=3qcdlrRe7UNQ3MoyAvsaAyy1Z+/jbolyeeT23DgRd4g=; b=fzZFAPE09sPo7Jfzczkugqdvrwvi69PlDLdEEHlMk6ePa2D7tbb0aa99sAxnakFSkdxgkn 44Yc5KFD7CEelcPtfK2gUH9Lg2qDQnGdWtDBbKATNoOPMO0utA4hI0wrgiK8LiSr4vSYOY B5bNHk7XYlhzon/Gchx3/iDLskfVMV0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687785325; 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=3qcdlrRe7UNQ3MoyAvsaAyy1Z+/jbolyeeT23DgRd4g=; b=RIxn85tyyPk5Oy2URnHvbhkXbGJQG/l+78cUA5jfJXbXULX0a8kIb1OXuL9MBrGwBoDPRZ 40rfR93ujIiRV8BA== 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 17E7613483; Mon, 26 Jun 2023 13:15:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ls88BG2PmWQhYQAAMHmgww (envelope-from ); Mon, 26 Jun 2023 13:15:25 +0000 Date: Mon, 26 Jun 2023 15:15:24 +0200 Message-ID: <87jzvquzyr.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: <87ttuuv5m6.wl-tiwai@suse.de> References: <877crqwvi1.wl-tiwai@suse.de> <871qhywucj.wl-tiwai@suse.de> <4d0931bf-b356-6969-5aaf-b663d7f2b21a@perex.cz> <87wmzqv64o.wl-tiwai@suse.de> <45445f57-0a73-59e6-6f3d-3983ce93a324@perex.cz> <87ttuuv5m6.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 13:13:21 +0200, Takashi Iwai wrote: > > On Mon, 26 Jun 2023 13:09:00 +0200, > Jaroslav Kysela wrote: > > > > On 26. 06. 23 13:02, Takashi Iwai wrote: > > > 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. > > > > The patch looks good, but it may be better to move the "post" variable > > updates to an inline function (mutex lock - update - mutex unlock) for > > a better readability. > > Sounds like a good idea. Let me cook later. ... and here it is. If that looks OK, I'll submit a proper fix patch. thanks, Takashi --- a/sound/core/pcm_memory.c +++ b/sound/core/pcm_memory.c @@ -31,15 +31,41 @@ static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL; module_param(max_alloc_per_card, ulong, 0644); MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card."); +static void __update_allocated_size(struct snd_card *card, ssize_t bytes) +{ + card->total_pcm_alloc_bytes += bytes; +} + +static void update_allocated_size(struct snd_card *card, ssize_t bytes) +{ + mutex_lock(&card->memory_mutex); + __update_allocated_size(card, bytes); + mutex_unlock(&card->memory_mutex); +} + +static void decrease_allocated_size(struct snd_card *card, size_t bytes) +{ + mutex_lock(&card->memory_mutex); + WARN_ON(card->total_pcm_alloc_bytes < bytes); + __update_allocated_size(card, -(ssize_t)bytes); + mutex_unlock(&card->memory_mutex); +} + static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, int str, size_t size, struct snd_dma_buffer *dmab) { enum dma_data_direction dir; int err; + /* check and reserve the requested size */ + 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; + } + __update_allocated_size(card, size); + mutex_unlock(&card->memory_mutex); if (str == SNDRV_PCM_STREAM_PLAYBACK) dir = DMA_TO_DEVICE; @@ -47,9 +73,14 @@ 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) { - mutex_lock(&card->memory_mutex); - card->total_pcm_alloc_bytes += dmab->bytes; - mutex_unlock(&card->memory_mutex); + /* the actual allocation size might be bigger than requested, + * and we need to correct the account + */ + if (dmab->bytes != size) + update_allocated_size(card, dmab->bytes - size); + } else { + /* take back on allocation failure */ + decrease_allocated_size(card, size); } return err; } @@ -58,10 +89,7 @@ static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab) { if (!dmab->area) return; - mutex_lock(&card->memory_mutex); - WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes); - card->total_pcm_alloc_bytes -= dmab->bytes; - mutex_unlock(&card->memory_mutex); + decrease_allocated_size(card, dmab->bytes); snd_dma_free_pages(dmab); dmab->area = NULL; }