AM I correct to observe that in case of any memory allocation done within device driver routines, kzalloc is preferred over kmalloc ?
I have seen kernel patches replacing kmalloc+memset with kzalloc. But my doubt is why one needs to set memory content at all ? Why can't we just use kmalloc when the memory is later expected to get written with relevant content anyway ?
The kmalloc() function guarantees that the pages are physically contiguous (and virtually contiguous). The vmalloc() function works in a similar fashion to kmalloc() , except it allocates memory that is only virtually contiguous and not necessarily physically contiguous.
Use this flag in code executing in process context without any locks. A call to kmalloc() with this flag can sleep; thus, you must use this flag only when it is safe to do so. The kernel utilizes the ability to sleep in order to free memory, if needed.
What is different functions: malloc() and kmalloc() ? They differ only in that: the malloc() can be called in user-space and kernel-space, and it allocates a physically fragmented memory area. but kmalloc() can be called only in kernel-space, and it allocates physically contiguous memory chunk.
The maximum size allocatable by kmalloc() is 1024 pages, or 4MB on x86.
It depends on the definition of relevant content.
If you do not care about the content of the memory you can just usekmalloc
; this is the case of buffer allocation, you do not care about the initial content because you are going to write your data. In this case you save the 'cost' of setting the memory to 0.
But things are different if you are going to allocate memory for a structure.
Personally, I prefer kzalloc
only when I want to allocate structures where I'm going to set some value (different than 0) but at the same time I want to set all the other fields of the structure to a known and valid state (zero). For example:
struct test {
int counter;
void *buffer;
int n_data;
};
In this case if I would use kzalloc
I will save some line of code because:
set to NULL
the buffer it is also fine because I will allocate it later and by setting it to NULL
I can simply write the following because it is a known state:
if (!t->buffer)
t->buffer = kmalloc(10);
setting the number of data n_data
to zero is good because the buffer at the beginning is empty and not allocated.
Of course, if in your structure you are going to set manually most of the fields with a value different than zero, then it make less sense (at least for me) to initialize everything to zero with kzalloc
Well. This is not very speicific to Linux Kernel and kmalloc/kzalloc as such. But its general problem around allocating and using the memory.
These guys have covered many cases of what could happen if you just do allocation and not zero it out. And these are still user-level stuff, imagine this things happening in Kernel.
Refer: zeroing out memory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With