the cost of vmalloc

Michael Blizek michi1 at
Sat Sep 10 15:30:24 EDT 2011


On 11:15 Sat 10 Sep     , Zheng Da wrote:
> On Sat, Sep 10, 2011 at 3:35 AM, Michael Blizek
> <michi1 at> wrote:
> >> I need to allocate fairly a large piece of memory, about 128KB or
> >> 256KB. I tried to use kmalloc, and it sometimes fails, but vmalloc in
> >> this case usually still work.
> >> But I'm not sure how costly vmalloc is. As far as I know, vmalloc
> >> needs to change the page table, and thus might need to invalidate TLB.
> >> It seems quite expensive. If it is, maybe I can allocate memory with
> >> kmalloc first, and then try to use vmalloc if kmalloc fails. Any
> >> suggestions?
> >
> > If it is at all possible, I would suggest avoiding to allocate more than
> > PAGE_SIZE (usually 4KB) in one continuous chunk. You can use e.g.
> > scatterlists (which are basically lists of pointers) if you need a bigger
> > buffer.
> Actually, I have seen some problems when allocating large pieces of
> memory constantly. Occasionally, I see alloc_pages and kmalloc fails
> to allocate the memory I want.

This does not really suprise me. Once the memory fragments, allocating large
continuous memory regions gets harder.

> The whole point of allocating a large chunk of memory is to avoid
> extra memory copy because I need to run decompression algorithms on
> it.

In this case scatterlists solves 2 problems at once. First, you will not need
to allocate large continuous memory regions. Second, you avoid wasting memory.

> Or memory copy is cheaper?

This is hard to say. But by passing the scatterlist you should be able to
avoid the copying.

> I'm also thinking of using slab allocator. The size of memory I need
> to allocate can change from 20KB to 128KB. Then I need quite a few
> slab allocators.

The slab allocator is good if you have a large number of allocations which
are the same size. To me it does not really sound like a good idea to use the
slab allocator in your case. 

programing a layer 3+4 network protocol for mesh networks

More information about the Kernelnewbies mailing list