public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Jaroslav Kysela <perex@perex.cz>
Cc: Tuo Li <islituo@gmail.com>,
	tiwai@suse.com, alsa-devel@alsa-project.org,
	Linux Kernel <linux-kernel@vger.kernel.org>,
	baijiaju1990@outlook.com
Subject: Re: [BUG] ALSA: core: pcm_memory: a possible data race in do_alloc_pages()
Date: Mon, 26 Jun 2023 15:37:36 +0200	[thread overview]
Message-ID: <87bkh2uyxr.wl-tiwai@suse.de> (raw)
In-Reply-To: <fb9db138-82a3-678c-bb94-035f923b3e16@perex.cz>

On Mon, 26 Jun 2023 15:32:40 +0200,
Jaroslav Kysela wrote:
> 
> 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)

I left the optimizations to compilers.  Usually they do inline if it
makes sense, and it's often a more sensible choice.


thanks,

Takashi

      reply	other threads:[~2023-06-26 13:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CADm8Tek6t0WedK+3Y6rbE5YEt19tML8BUL45N2ji4ZAz1KcN_A@mail.gmail.com>
2023-06-26  7:08 ` [BUG] ALSA: core: pcm_memory: a possible data race in do_alloc_pages() Takashi Iwai
     [not found]   ` <CADm8Tenfy8joto5WLCqQWjfT8WimsbJgOss0hJe-ciyDRMrSXw@mail.gmail.com>
2023-06-26  7:33     ` Takashi Iwai
2023-06-26  7:56       ` Jaroslav Kysela
2023-06-26 11:02         ` Takashi Iwai
2023-06-26 11:09           ` Jaroslav Kysela
2023-06-26 11:13             ` Takashi Iwai
2023-06-26 13:15               ` Takashi Iwai
2023-06-26 13:32                 ` Jaroslav Kysela
2023-06-26 13:37                   ` Takashi Iwai [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkh2uyxr.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=baijiaju1990@outlook.com \
    --cc=islituo@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox