Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp16175873rwd; Mon, 26 Jun 2023 06:47:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5OMXyLDyAk6gEm3gpbcaFDETn5YiXVSIHfwMDLyC15CkIeNxxDZrs1muvDxn+SFEa9b5uV X-Received: by 2002:a17:906:ee8c:b0:988:8efc:54fa with SMTP id wt12-20020a170906ee8c00b009888efc54famr16958688ejb.37.1687787274693; Mon, 26 Jun 2023 06:47:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687787274; cv=none; d=google.com; s=arc-20160816; b=LnDTN7AobiHBdGJH+Lxs1SkHmr1T4U9HuBAeYCD68aKPgRkizTH1tQwYOXXW/NPfC8 o3oiClgX5RsACa09ygm//aXBxFX6sTpO4a3ekMZbliWNnPmT/UO8kdmb+W9uZ9bCVEwQ Xkdb3imuIW6Ksa0uhhEGD4UxVVJruzgpmww1CPWzUrDANNvWeA2KLMSaCVD2pFmXJIGw KmWvr8m1zW5opcOgUQfPova0bf81f6fgCsZluIkPOeHJ+sjygSGvlGyLgTypqz2eaZPV SNfStZYgq2ikPGTTgytQeW+TSpprsqtRUbkFIH7tSAza8jh7mkASF1l//fzrlfHZ3Y3/ dVFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature:dkim-filter; bh=P4b7pQGf/nmZaFg3tRkshnVDvy5ehhcr3/lNB4wKevk=; fh=u/QwnlVUSRuKMapMCJ0b/cjMLLjqSpt8+LDAaNPoCdA=; b=iIavvFUYFi8vIEs0RcU7VG+OMY69Mk3+nIzidnCyF9SDx8yty4uVnEdVUwcYlzO8WR nMJXAA96rc/KnpoDIlark9xyVeKw7ooArRpWodgpaejTxf2YjFObT0WW9QLoiSUoVl6m vGimt5+eTVO2EAY0s3GZjtwhMU4O7aq8aUk2PzIjq4Gtzlq1gjwWvVZp1Eldzw4aeyB9 TCTpumF2aIMKr/PzdPYrRwvfxDiNBTVij6T59RAYF39F/yn4ay4pIf3ZrrziPmb5osPX sI3vvZVqxgDe1ByLA/EkZm41fyB7W/MmoioT8IXezS/NlMgjNJy3BQj8k7QFvYSWWz76 ncmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@perex.cz header.s=default header.b=INm+DvE1; 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=perex.cz Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h15-20020a170906590f00b00988a4e660f9si3034203ejq.491.2023.06.26.06.47.28; Mon, 26 Jun 2023 06:47:54 -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=@perex.cz header.s=default header.b=INm+DvE1; 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=perex.cz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229523AbjFZNcx (ORCPT + 99 others); Mon, 26 Jun 2023 09:32:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjFZNcv (ORCPT ); Mon, 26 Jun 2023 09:32:51 -0400 Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2EE01A2 for ; Mon, 26 Jun 2023 06:32:49 -0700 (PDT) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id EB2841E18; Mon, 26 Jun 2023 15:32:46 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz EB2841E18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1687786367; bh=P4b7pQGf/nmZaFg3tRkshnVDvy5ehhcr3/lNB4wKevk=; h=Date:To:Cc:References:From:Subject:In-Reply-To:From; b=INm+DvE15zSIDUunAUMNiMSC6/hpj0qh3QF2GvxufGkWEaPvEN9DjdE2RAZvM5/L2 w5l3jtSrcHcxm00BbLxBo7sj/8HP9D5alhZ1K4pooC04EUlWfrNuzDg1LN0atr0TcB rdKJhvemxKr2z4c45NgTGaFfkY2kAIKm/vsy2wH0= Received: from [192.168.100.98] (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Mon, 26 Jun 2023 15:32:40 +0200 (CEST) Message-ID: Date: Mon, 26 Jun 2023 15:32:40 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Content-Language: en-US To: Takashi Iwai Cc: Tuo Li , tiwai@suse.com, alsa-devel@alsa-project.org, Linux Kernel , baijiaju1990@outlook.com 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> <87jzvquzyr.wl-tiwai@suse.de> From: Jaroslav Kysela Subject: Re: [BUG] ALSA: core: pcm_memory: a possible data race in do_alloc_pages() In-Reply-To: <87jzvquzyr.wl-tiwai@suse.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,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 26. 06. 23 15:15, Takashi Iwai wrote: > 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) Missing inline ? May be also used for > +static void update_allocated_size(struct snd_card *card, ssize_t bytes) > +static void decrease_allocated_size(struct snd_card *card, size_t bytes) The rest is fine in my eyes. Reviewed-by: Jaroslav Kysela Thanks, Jaroslav -- Jaroslav Kysela Linux Sound Maintainer; ALSA Project; Red Hat, Inc.