Skip to content

Commit ef4db23

Browse files
committed
ALSA: rawmidi: Use kvmalloc() for buffers
The size of in-kernel rawmidi buffers may be big up to 1MB, and it can be specified freely by user-space; which implies that user-space may trigger kmalloc() errors frequently. This patch replaces the buffer allocation via kvmalloc() for dealing with bigger buffers gracefully. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent f5beb59 commit ef4db23

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

‎sound/core/rawmidi.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/mutex.h>
3030
#include <linux/module.h>
3131
#include <linux/delay.h>
32+
#include <linux/mm.h>
3233
#include <sound/rawmidi.h>
3334
#include <sound/info.h>
3435
#include <sound/control.h>
@@ -128,7 +129,7 @@ static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
128129
runtime->avail = 0;
129130
else
130131
runtime->avail = runtime->buffer_size;
131-
runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL);
132+
runtime->buffer = kvmalloc(runtime->buffer_size, GFP_KERNEL);
132133
if (!runtime->buffer) {
133134
kfree(runtime);
134135
return -ENOMEM;
@@ -142,7 +143,7 @@ static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
142143
{
143144
struct snd_rawmidi_runtime *runtime = substream->runtime;
144145

145-
kfree(runtime->buffer);
146+
kvfree(runtime->buffer);
146147
kfree(runtime);
147148
substream->runtime = NULL;
148149
return 0;
@@ -654,7 +655,7 @@ static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
654655
if (params->avail_min < 1 || params->avail_min > params->buffer_size)
655656
return -EINVAL;
656657
if (params->buffer_size != runtime->buffer_size) {
657-
newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
658+
newbuf = kvmalloc(params->buffer_size, GFP_KERNEL);
658659
if (!newbuf)
659660
return -ENOMEM;
660661
spin_lock_irq(&runtime->lock);
@@ -663,7 +664,7 @@ static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime,
663664
runtime->buffer_size = params->buffer_size;
664665
__reset_runtime_ptrs(runtime, is_input);
665666
spin_unlock_irq(&runtime->lock);
666-
kfree(oldbuf);
667+
kvfree(oldbuf);
667668
}
668669
runtime->avail_min = params->avail_min;
669670
return 0;

0 commit comments

Comments
 (0)