ALSA: Replace snd_malloc_pages() and snd_free_pages() with standard helpers, take#2
authorTakashi Iwai <tiwai@suse.de>
Fri, 23 Nov 2018 18:38:13 +0000 (19:38 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 27 Mar 2019 16:15:01 +0000 (17:15 +0100)
snd_malloc_pages() and snd_free_pages() are merely thin wrappers of
the standard page allocator / free functions.  Even the arguments are
compatible with some standard helpers, so there is little merit of
keeping these wrappers.

This patch replaces the all existing callers of snd_malloc_pages() and
snd_free_pages() with the direct calls of the standard helper
functions.  In this version, we use a recently introduced one,
alloc_pages_exact(), which suits better than the old
snd_malloc_pages() implementation for our purposes.  Then we can avoid
the waste of pages by alignment to power-of-two.

Since alloc_pages_exact() does split pages, we need no longer
__GFP_COMP flag; or better to say, we must not pass __GFP_COMP to
alloc_pages_exact().  So the former unconditional addition of
__GFP_COMP flag in snd_malloc_pages() is dropped, as well as in most
other places.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/memalloc.h
sound/core/memalloc.c
sound/core/pcm.c
sound/usb/usx2y/usX2Yhwdep.c
sound/usb/usx2y/usbusx2y.c
sound/usb/usx2y/usx2yhwdeppcm.c

index 1ac0dd82a9163847dc5280aa55efbead4f671bdb..4c6f3b5a7cfff90e8d18626df07b8f92c92a3ad7 100644 (file)
@@ -151,9 +151,5 @@ int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
                                  struct snd_dma_buffer *dmab);
 void snd_dma_free_pages(struct snd_dma_buffer *dmab);
 
-/* basic memory allocation functions */
-void *snd_malloc_pages(size_t size, gfp_t gfp_flags);
-void snd_free_pages(void *ptr, size_t size);
-
 #endif /* __SOUND_MEMALLOC_H */
 
index eb974235c92bdfba19d706735a88a9cf95897664..9f48e1d3a257066488a1c5c460a4080283f0c6f8 100644 (file)
 #endif
 #include <sound/memalloc.h>
 
-/*
- *
- *  Generic memory allocators
- *
- */
-
-/**
- * snd_malloc_pages - allocate pages with the given size
- * @size: the size to allocate in bytes
- * @gfp_flags: the allocation conditions, GFP_XXX
- *
- * Allocates the physically contiguous pages with the given size.
- *
- * Return: The pointer of the buffer, or %NULL if no enough memory.
- */
-void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
-{
-       int pg;
-
-       if (WARN_ON(!size))
-               return NULL;
-       if (WARN_ON(!gfp_flags))
-               return NULL;
-       gfp_flags |= __GFP_COMP;        /* compound page lets parts be mapped */
-       pg = get_order(size);
-       return (void *) __get_free_pages(gfp_flags, pg);
-}
-EXPORT_SYMBOL(snd_malloc_pages);
-
-/**
- * snd_free_pages - release the pages
- * @ptr: the buffer pointer to release
- * @size: the allocated buffer size
- *
- * Releases the buffer allocated via snd_malloc_pages().
- */
-void snd_free_pages(void *ptr, size_t size)
-{
-       int pg;
-
-       if (ptr == NULL)
-               return;
-       pg = get_order(size);
-       free_pages((unsigned long) ptr, pg);
-}
-EXPORT_SYMBOL(snd_free_pages);
-
 /*
  *
  *  Bus-specific memory allocators
@@ -190,8 +143,8 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size,
        dmab->bytes = 0;
        switch (type) {
        case SNDRV_DMA_TYPE_CONTINUOUS:
-               dmab->area = snd_malloc_pages(size,
-                                       (__force gfp_t)(unsigned long)device);
+               dmab->area = alloc_pages_exact(size,
+                                              (__force gfp_t)(unsigned long)device);
                dmab->addr = 0;
                break;
 #ifdef CONFIG_HAS_DMA
@@ -275,7 +228,7 @@ void snd_dma_free_pages(struct snd_dma_buffer *dmab)
 {
        switch (dmab->dev.type) {
        case SNDRV_DMA_TYPE_CONTINUOUS:
-               snd_free_pages(dmab->area, dmab->bytes);
+               free_pages_exact(dmab->area, dmab->bytes);
                break;
 #ifdef CONFIG_HAS_DMA
 #ifdef CONFIG_GENERIC_ALLOCATOR
index 7b63aee124af3e3394c3f9646747f61f97cb141c..998e477522fdbd57e0d377fd499bfd5e9fd7f7ff 100644 (file)
@@ -959,22 +959,22 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
                return -ENOMEM;
 
        size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
-       runtime->status = snd_malloc_pages(size, GFP_KERNEL);
+       runtime->status = alloc_pages_exact(size, GFP_KERNEL);
        if (runtime->status == NULL) {
                kfree(runtime);
                return -ENOMEM;
        }
-       memset((void*)runtime->status, 0, size);
+       memset(runtime->status, 0, size);
 
        size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
-       runtime->control = snd_malloc_pages(size, GFP_KERNEL);
+       runtime->control = alloc_pages_exact(size, GFP_KERNEL);
        if (runtime->control == NULL) {
-               snd_free_pages((void*)runtime->status,
+               free_pages_exact(runtime->status,
                               PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
                kfree(runtime);
                return -ENOMEM;
        }
-       memset((void*)runtime->control, 0, size);
+       memset(runtime->control, 0, size);
 
        init_waitqueue_head(&runtime->sleep);
        init_waitqueue_head(&runtime->tsleep);
@@ -1000,9 +1000,9 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
        runtime = substream->runtime;
        if (runtime->private_free != NULL)
                runtime->private_free(runtime);
-       snd_free_pages((void*)runtime->status,
+       free_pages_exact(runtime->status,
                       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
-       snd_free_pages((void*)runtime->control,
+       free_pages_exact(runtime->control,
                       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
        kfree(runtime->hw_constraints.rules);
        /* Avoid concurrent access to runtime via PCM timer interface */
index c1dd9a7b48df6749d8c566fc6fbac2a7fb1d630b..bfe1108416cfcc2df54320906d148ae4189d7d5e 100644 (file)
@@ -75,7 +75,8 @@ static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct v
 
        if (!us428->us428ctls_sharedmem) {
                init_waitqueue_head(&us428->us428ctls_wait_queue_head);
-               if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
+               us428->us428ctls_sharedmem = alloc_pages_exact(sizeof(struct us428ctls_sharedmem), GFP_KERNEL);
+               if (!us428->us428ctls_sharedmem)
                        return -ENOMEM;
                memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
                us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
index da4a5a541512ddabba38d84565e2b98e1457fb3e..9f7bbed2c0f031c2957d4465d86c17972a6f1dbe 100644 (file)
@@ -437,7 +437,8 @@ static void snd_usX2Y_card_private_free(struct snd_card *card)
        kfree(usX2Y(card)->In04Buf);
        usb_free_urb(usX2Y(card)->In04urb);
        if (usX2Y(card)->us428ctls_sharedmem)
-               snd_free_pages(usX2Y(card)->us428ctls_sharedmem, sizeof(*usX2Y(card)->us428ctls_sharedmem));
+               free_pages_exact(usX2Y(card)->us428ctls_sharedmem,
+                                sizeof(*usX2Y(card)->us428ctls_sharedmem));
        if (usX2Y(card)->card_index >= 0  &&  usX2Y(card)->card_index < SNDRV_CARDS)
                snd_usX2Y_card_used[usX2Y(card)->card_index] = 0;
 }
index 714cf50d4a4c6baefd6b17b63237fc891413dd98..ace8185c3f6d1477c650cc9ace5d90b85efd66f8 100644 (file)
@@ -488,7 +488,9 @@ static int snd_usX2Y_usbpcm_prepare(struct snd_pcm_substream *substream)
        snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
 
        if (NULL == usX2Y->hwdep_pcm_shm) {
-               if (NULL == (usX2Y->hwdep_pcm_shm = snd_malloc_pages(sizeof(struct snd_usX2Y_hwdep_pcm_shm), GFP_KERNEL)))
+               usX2Y->hwdep_pcm_shm = alloc_pages_exact(sizeof(struct snd_usX2Y_hwdep_pcm_shm),
+                                                        GFP_KERNEL);
+               if (!usX2Y->hwdep_pcm_shm)
                        return -ENOMEM;
                memset(usX2Y->hwdep_pcm_shm, 0, sizeof(struct snd_usX2Y_hwdep_pcm_shm));
        }
@@ -700,7 +702,7 @@ static void snd_usX2Y_hwdep_pcm_private_free(struct snd_hwdep *hwdep)
 {
        struct usX2Ydev *usX2Y = hwdep->private_data;
        if (NULL != usX2Y->hwdep_pcm_shm)
-               snd_free_pages(usX2Y->hwdep_pcm_shm, sizeof(struct snd_usX2Y_hwdep_pcm_shm));
+               free_pages_exact(usX2Y->hwdep_pcm_shm, sizeof(struct snd_usX2Y_hwdep_pcm_shm));
 }