[bug report] ALSA: memalloc: Add fallback SG-buffer allocations for x86

Takashi Iwai tiwai at suse.de
Wed Apr 20 10:00:47 CEST 2022


On Wed, 20 Apr 2022 09:44:58 +0200,
Dan Carpenter wrote:
> 
> Hello Takashi Iwai,
> 
> The patch 925ca893b4a6: "ALSA: memalloc: Add fallback SG-buffer
> allocations for x86" from Apr 13, 2022, leads to the following Smatch
> static checker warning:
> 
> 	sound/core/memalloc.c:732 snd_dma_sg_fallback_alloc()
> 	error: 'p' came from dma_alloc_coherent() so we can't do virt_to_phys()
> 
> sound/core/memalloc.c
>     708 static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
>     709 {
>     710         struct snd_dma_sg_fallback *sgbuf;
>     711         struct page **pages;
>     712         size_t i, count;
>     713         void *p;
>     714 
>     715         sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
>     716         if (!sgbuf)
>     717                 return NULL;
>     718         count = PAGE_ALIGN(size) >> PAGE_SHIFT;
>     719         pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
>     720         if (!pages)
>     721                 goto error;
>     722         sgbuf->pages = pages;
>     723         sgbuf->addrs = kvcalloc(count, sizeof(*sgbuf->addrs), GFP_KERNEL);
>     724         if (!sgbuf->addrs)
>     725                 goto error;
>     726 
>     727         for (i = 0; i < count; sgbuf->count++, i++) {
>     728                 p = dma_alloc_coherent(dmab->dev.dev, PAGE_SIZE,
>     729                                        &sgbuf->addrs[i], DEFAULT_GFP);
>     730                 if (!p)
>     731                         goto error;
> --> 732                 sgbuf->pages[i] = virt_to_page(p);
> 
> The warning is a bit useless.  It's complaining about __phys_addr()
> and not virt_to_phys().  I don't really understand the rules here, it
> might be legal in certain contexts.

In general it's not good to perform virt_to_page() for the
dma_alloc_coherent(), but here the code is special only for x86 and
certain circumstances, so this must work -- it's the very same stuff
we had over a decade, after all.  The code was resurrected in a
simplified form as a fallback now.


thanks,

Takashi


More information about the Alsa-devel mailing list