Skip to content

Commit

Permalink
[ALSA] Remove vmalloc wrapper, kfree_nocheck()
Browse files Browse the repository at this point in the history
- Remove vmalloc wrapper
- Add release_and_free_resource() to remove kfree_nocheck() from each driver
  and simplify the code

Signed-off-by: Takashi Iwai <[email protected]>
  • Loading branch information
tiwai authored and Jaroslav Kysela committed Nov 4, 2005
1 parent 93f2e37 commit b1d5776
Show file tree
Hide file tree
Showing 44 changed files with 86 additions and 311 deletions.
17 changes: 1 addition & 16 deletions Documentation/sound/alsa/DocBook/writing-an-alsa-driver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1433,25 +1433,10 @@
<informalexample>
<programlisting>
<![CDATA[
if (chip->res_port) {
release_resource(chip->res_port);
kfree_nocheck(chip->res_port);
}
release_and_free_resource(chip->res_port);
]]>
</programlisting>
</informalexample>

As you can see, the resource pointer is also to be freed
via <function>kfree_nocheck()</function> after
<function>release_resource()</function> is called. You
cannot use <function>kfree()</function> here, because on ALSA,
<function>kfree()</function> may be a wrapper to its own
allocator with the memory debugging. Since the resource pointer
is allocated externally outside the ALSA, it must be released
via the native
<function>kfree()</function>.
<function>kfree_nocheck()</function> is used for that; it calls
the native <function>kfree()</function> without wrapper.
</para>

<para>
Expand Down
9 changes: 1 addition & 8 deletions include/sound/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,29 +293,21 @@ void *snd_hidden_kmalloc(size_t size, gfp_t flags);
void *snd_hidden_kzalloc(size_t size, gfp_t flags);
void *snd_hidden_kcalloc(size_t n, size_t size, gfp_t flags);
void snd_hidden_kfree(const void *obj);
void *snd_hidden_vmalloc(unsigned long size);
void snd_hidden_vfree(void *obj);
char *snd_hidden_kstrdup(const char *s, gfp_t flags);
#define kmalloc(size, flags) snd_hidden_kmalloc(size, flags)
#define kzalloc(size, flags) snd_hidden_kzalloc(size, flags)
#define kcalloc(n, size, flags) snd_hidden_kcalloc(n, size, flags)
#define kfree(obj) snd_hidden_kfree(obj)
#define vmalloc(size) snd_hidden_vmalloc(size)
#define vfree(obj) snd_hidden_vfree(obj)
#define kmalloc_nocheck(size, flags) snd_wrapper_kmalloc(size, flags)
#define vmalloc_nocheck(size) snd_wrapper_vmalloc(size)
#define kfree_nocheck(obj) snd_wrapper_kfree(obj)
#define vfree_nocheck(obj) snd_wrapper_vfree(obj)
#define kstrdup(s, flags) snd_hidden_kstrdup(s, flags)
#else
#define snd_memory_init() /*NOP*/
#define snd_memory_done() /*NOP*/
#define snd_memory_info_init() /*NOP*/
#define snd_memory_info_done() /*NOP*/
#define kmalloc_nocheck(size, flags) kmalloc(size, flags)
#define vmalloc_nocheck(size) vmalloc(size)
#define kfree_nocheck(obj) kfree(obj)
#define vfree_nocheck(obj) vfree(obj)
#endif
int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
Expand Down Expand Up @@ -372,6 +364,7 @@ unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
#endif

/* misc.c */
void release_and_free_resource(struct resource *res);

#ifdef CONFIG_SND_VERBOSE_PRINTK
void snd_verbose_printk(const char *file, int line, const char *format, ...)
Expand Down
4 changes: 0 additions & 4 deletions include/sound/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ void *snd_wrapper_kmalloc(size_t, gfp_t);
#undef kmalloc
void snd_wrapper_kfree(const void *);
#undef kfree
void *snd_wrapper_vmalloc(size_t);
#undef vmalloc
void snd_wrapper_vfree(void *);
#undef vfree
#endif

#endif /* __SOUND_DRIVER_H */
53 changes: 0 additions & 53 deletions sound/core/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,14 @@ struct snd_alloc_track {
#define snd_alloc_track_entry(obj) (struct snd_alloc_track *)((char*)obj - (unsigned long)((struct snd_alloc_track *)0)->data)

static long snd_alloc_kmalloc;
static long snd_alloc_vmalloc;
static LIST_HEAD(snd_alloc_kmalloc_list);
static LIST_HEAD(snd_alloc_vmalloc_list);
static DEFINE_SPINLOCK(snd_alloc_kmalloc_lock);
static DEFINE_SPINLOCK(snd_alloc_vmalloc_lock);
#define KMALLOC_MAGIC 0x87654321
#define VMALLOC_MAGIC 0x87654320
static snd_info_entry_t *snd_memory_info_entry;

void __init snd_memory_init(void)
{
snd_alloc_kmalloc = 0;
snd_alloc_vmalloc = 0;
}

void snd_memory_done(void)
Expand All @@ -69,8 +64,6 @@ void snd_memory_done(void)

if (snd_alloc_kmalloc > 0)
snd_printk(KERN_ERR "Not freed snd_alloc_kmalloc = %li\n", snd_alloc_kmalloc);
if (snd_alloc_vmalloc > 0)
snd_printk(KERN_ERR "Not freed snd_alloc_vmalloc = %li\n", snd_alloc_vmalloc);
list_for_each_prev(head, &snd_alloc_kmalloc_list) {
t = list_entry(head, struct snd_alloc_track, list);
if (t->magic != KMALLOC_MAGIC) {
Expand All @@ -79,14 +72,6 @@ void snd_memory_done(void)
}
snd_printk(KERN_ERR "kmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
}
list_for_each_prev(head, &snd_alloc_vmalloc_list) {
t = list_entry(head, struct snd_alloc_track, list);
if (t->magic != VMALLOC_MAGIC) {
snd_printk(KERN_ERR "Corrupted vmalloc\n");
break;
}
snd_printk(KERN_ERR "vmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
}
}

static void *__snd_kmalloc(size_t size, gfp_t flags, void *caller)
Expand Down Expand Up @@ -153,43 +138,6 @@ void snd_hidden_kfree(const void *obj)
snd_wrapper_kfree(obj);
}

void *snd_hidden_vmalloc(unsigned long size)
{
void *ptr;
ptr = snd_wrapper_vmalloc(size + sizeof(struct snd_alloc_track));
if (ptr) {
struct snd_alloc_track *t = (struct snd_alloc_track *)ptr;
t->magic = VMALLOC_MAGIC;
t->caller = __builtin_return_address(0);
spin_lock(&snd_alloc_vmalloc_lock);
list_add_tail(&t->list, &snd_alloc_vmalloc_list);
spin_unlock(&snd_alloc_vmalloc_lock);
t->size = size;
snd_alloc_vmalloc += size;
ptr = t->data;
}
return ptr;
}

void snd_hidden_vfree(void *obj)
{
struct snd_alloc_track *t;
if (obj == NULL)
return;
t = snd_alloc_track_entry(obj);
if (t->magic != VMALLOC_MAGIC) {
snd_printk(KERN_ERR "bad vfree (called from %p)\n", __builtin_return_address(0));
return;
}
spin_lock(&snd_alloc_vmalloc_lock);
list_del(&t->list);
spin_unlock(&snd_alloc_vmalloc_lock);
t->magic = 0;
snd_alloc_vmalloc -= t->size;
obj = t;
snd_wrapper_vfree(obj);
}

char *snd_hidden_kstrdup(const char *s, gfp_t flags)
{
int len;
Expand All @@ -207,7 +155,6 @@ char *snd_hidden_kstrdup(const char *s, gfp_t flags)
static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
{
snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
snd_iprintf(buffer, "vmalloc: %li bytes\n", snd_alloc_vmalloc);
}

int __init snd_memory_info_init(void)
Expand Down
9 changes: 9 additions & 0 deletions sound/core/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/ioport.h>
#include <sound/core.h>

void release_and_free_resource(struct resource *res)
{
if (res) {
release_resource(res);
kfree_nocheck(res);
}
}

#ifdef CONFIG_SND_VERBOSE_PRINTK
void snd_verbose_printk(const char *file, int line, const char *format, ...)
{
Expand Down
5 changes: 1 addition & 4 deletions sound/core/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ EXPORT_SYMBOL(snd_unregister_oss_device);
EXPORT_SYMBOL(snd_hidden_kmalloc);
EXPORT_SYMBOL(snd_hidden_kcalloc);
EXPORT_SYMBOL(snd_hidden_kfree);
EXPORT_SYMBOL(snd_hidden_vmalloc);
EXPORT_SYMBOL(snd_hidden_vfree);
EXPORT_SYMBOL(snd_hidden_kstrdup);
#endif
EXPORT_SYMBOL(copy_to_user_fromio);
Expand Down Expand Up @@ -487,6 +485,7 @@ EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
EXPORT_SYMBOL(snd_ctl_elem_read);
EXPORT_SYMBOL(snd_ctl_elem_write);
/* misc.c */
EXPORT_SYMBOL(release_and_free_resource);
#ifdef CONFIG_SND_VERBOSE_PRINTK
EXPORT_SYMBOL(snd_verbose_printk);
#endif
Expand All @@ -497,6 +496,4 @@ EXPORT_SYMBOL(snd_verbose_printd);
#ifdef CONFIG_SND_DEBUG_MEMORY
EXPORT_SYMBOL(snd_wrapper_kmalloc);
EXPORT_SYMBOL(snd_wrapper_kfree);
EXPORT_SYMBOL(snd_wrapper_vmalloc);
EXPORT_SYMBOL(snd_wrapper_vfree);
#endif
10 changes: 0 additions & 10 deletions sound/core/wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,5 @@ void snd_wrapper_kfree(const void *obj)
{
kfree(obj);
}

void *snd_wrapper_vmalloc(unsigned long size)
{
return vmalloc(size);
}

void snd_wrapper_vfree(void *obj)
{
vfree(obj);
}
#endif

5 changes: 1 addition & 4 deletions sound/drivers/mpu401/mpu401_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,7 @@ static void snd_mpu401_uart_free(snd_rawmidi_t *rmidi)
mpu401_t *mpu = rmidi->private_data;
if (mpu->irq_flags && mpu->irq >= 0)
free_irq(mpu->irq, (void *) mpu);
if (mpu->res) {
release_resource(mpu->res);
kfree_nocheck(mpu->res);
}
release_and_free_resource(mpu->res);
kfree(mpu);
}

Expand Down
5 changes: 1 addition & 4 deletions sound/drivers/mtpav.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,7 @@ static void free_mtpav(mtpav_t * crd)
spin_unlock_irqrestore(&crd->spinlock, flags);
if (crd->irq >= 0)
free_irq(crd->irq, (void *)crd);
if (crd->res_port) {
release_resource(crd->res_port);
kfree_nocheck(crd->res_port);
}
release_and_free_resource(crd->res_port);
kfree(crd);
}

Expand Down
10 changes: 2 additions & 8 deletions sound/drivers/opl3/opl3_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,8 @@ static int snd_opl3_free(opl3_t *opl3)
snd_assert(opl3 != NULL, return -ENXIO);
if (opl3->private_free)
opl3->private_free(opl3);
if (opl3->res_l_port) {
release_resource(opl3->res_l_port);
kfree_nocheck(opl3->res_l_port);
}
if (opl3->res_r_port) {
release_resource(opl3->res_r_port);
kfree_nocheck(opl3->res_r_port);
}
release_and_free_resource(opl3->res_l_port);
release_and_free_resource(opl3->res_r_port);
kfree(opl3);
return 0;
}
Expand Down
10 changes: 2 additions & 8 deletions sound/drivers/opl4/opl4_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,8 @@ static void snd_opl4_free(opl4_t *opl4)
#ifdef CONFIG_PROC_FS
snd_opl4_free_proc(opl4);
#endif
if (opl4->res_fm_port) {
release_resource(opl4->res_fm_port);
kfree_nocheck(opl4->res_fm_port);
}
if (opl4->res_pcm_port) {
release_resource(opl4->res_pcm_port);
kfree_nocheck(opl4->res_pcm_port);
}
release_and_free_resource(opl4->res_fm_port);
release_and_free_resource(opl4->res_pcm_port);
kfree(opl4);
}

Expand Down
5 changes: 1 addition & 4 deletions sound/drivers/serial-u16550.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,7 @@ static int snd_uart16550_free(snd_uart16550_t *uart)
{
if (uart->irq >= 0)
free_irq(uart->irq, (void *)uart);
if (uart->res_base) {
release_resource(uart->res_base);
kfree_nocheck(uart->res_base);
}
release_and_free_resource(uart->res_base);
kfree(uart);
return 0;
};
Expand Down
4 changes: 2 additions & 2 deletions sound/drivers/vx/vx_pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static int snd_pcm_alloc_vmalloc_buffer(snd_pcm_substream_t *subs, size_t size)
/* already allocated */
if (runtime->dma_bytes >= size)
return 0; /* already enough large */
vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */
vfree(runtime->dma_area);
}
runtime->dma_area = vmalloc_32(size);
if (! runtime->dma_area)
Expand All @@ -98,7 +98,7 @@ static int snd_pcm_free_vmalloc_buffer(snd_pcm_substream_t *subs)
{
snd_pcm_runtime_t *runtime = subs->runtime;
if (runtime->dma_area) {
vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */
vfree(runtime->dma_area);
runtime->dma_area = NULL;
}
return 0;
Expand Down
5 changes: 1 addition & 4 deletions sound/isa/ad1816a/ad1816a_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,7 @@ static int snd_ad1816a_probe(ad1816a_t *chip)

static int snd_ad1816a_free(ad1816a_t *chip)
{
if (chip->res_port) {
release_resource(chip->res_port);
kfree_nocheck(chip->res_port);
}
release_and_free_resource(chip->res_port);
if (chip->irq >= 0)
free_irq(chip->irq, (void *) chip);
if (chip->dma1 >= 0) {
Expand Down
5 changes: 1 addition & 4 deletions sound/isa/ad1848/ad1848_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,7 @@ static int snd_ad1848_capture_close(snd_pcm_substream_t * substream)

static int snd_ad1848_free(ad1848_t *chip)
{
if (chip->res_port) {
release_resource(chip->res_port);
kfree_nocheck(chip->res_port);
}
release_and_free_resource(chip->res_port);
if (chip->irq >= 0)
free_irq(chip->irq, (void *) chip);
if (chip->dma >= 0) {
Expand Down
10 changes: 2 additions & 8 deletions sound/isa/cs423x/cs4231_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1417,14 +1417,8 @@ static int snd_cs4231_pm_resume(snd_card_t *card)

static int snd_cs4231_free(cs4231_t *chip)
{
if (chip->res_port) {
release_resource(chip->res_port);
kfree_nocheck(chip->res_port);
}
if (chip->res_cport) {
release_resource(chip->res_cport);
kfree_nocheck(chip->res_cport);
}
release_and_free_resource(chip->res_port);
release_and_free_resource(chip->res_cport);
if (chip->irq >= 0) {
disable_irq(chip->irq);
if (!(chip->hwshare & CS4231_HWSHARE_IRQ))
Expand Down
8 changes: 2 additions & 6 deletions sound/isa/cs423x/cs4236.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,8 @@ static void snd_card_cs4236_free(snd_card_t *card)
{
struct snd_card_cs4236 *acard = (struct snd_card_cs4236 *)card->private_data;

if (acard) {
if (acard->res_sb_port) {
release_resource(acard->res_sb_port);
kfree_nocheck(acard->res_sb_port);
}
}
if (acard)
release_and_free_resource(acard->res_sb_port);
}

#ifdef CONFIG_PNP
Expand Down
3 changes: 1 addition & 2 deletions sound/isa/es1688/es1688_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,7 @@ static int snd_es1688_free(es1688_t *chip)
{
if (chip->res_port) {
snd_es1688_init(chip, 0);
release_resource(chip->res_port);
kfree_nocheck(chip->res_port);
release_and_free_resource(chip->res_port);
}
if (chip->irq >= 0)
free_irq(chip->irq, (void *) chip);
Expand Down
Loading

0 comments on commit b1d5776

Please sign in to comment.