Question

How will I come to know that where exactly or at what point I should use the kmalloc() to allocate a memory to the device in the device driver?

Is it during initialization or during open? As in malloc,wil kmalloc allocates memory dynamically?

Was it helpful?

Solution

In general, you can use kmalloc() when you need physically contigous memory in kernel space. You can use this during init/open depending on your use case. If you kmalloc in init() but never use the device, then memory allocated is waste. If kmalloc is used in open(), memory allocated is actually used because memory is allocated only if device is used.

Also, note that you can use vmalloc() in kernel in case you are not in need of physically contigous memory allocation.

OTHER TIPS

It depends on when you need it. There is no hard and fast rule.

For eg, in the i2c driver in linux kernel, there are two kmalloc calls and none in initialization or any specific function.

And yes, it acts similar to user space malloc call and allocates memory dynamically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top