Efficiently Allocate Memory in Kernel

c, linux-kernel

Solution

I once wrote a kernel module processing packets on a 10Gbs link. I used `vmalloc` to allocate about 1GByte of continuous (virtual) memory to put a static size hash table into it to perform connection tracking (code).

If you know how much memory you need, I recommend to pre-allocate it. This has two advantages

- It is fast (no mallocing/freeing at runtime)

- You don't have to think of a strategy if `kmalloc(_, GFP_ATOMIC)` can not return you memory. This can actually happen quite often under heavy load.

Disadvantage

- You might allocate more memory then necessary.

So, for writing a special-purpose kernel module, please pre-allocate as much memory as you can get ;)

If you write a kernel module for commodity hardware used by many novice users, it would be nice to allocate memory on demand (and waste less memory).

Where do you allocate memory? `GFP_ATOMIC` can only return a very small amount of memory and should only be used if your memory allocation cannot sleep. You can use `GFP_KERNEL` when it is safe to sleep, e.g., not in interrupt context. See this question for more. It is safe to use `vmalloc` during module initialization to pre-allocate all you memory.

Problem

I want to write a kernel module where I am getting TCP/IP packets near 8 mbps. I have to store these packet for 500ms duration. Later these packets should be forwarded sequentially. And these should be done for 30 members. What should be best approach to implement? Should I use `kmalloc` for once `(kmalloc(64000000, GFP_ATOMIC)`? Because each time if I do `kmalloc` and `kfree` it will take time, leading to a performance issue. Also if I allocate memory in kernel in one shot will the linux kernel will allow me to do that?

Original source

Related problems