My understanding is as follows: the kernel is dealing with the physical memory of the system, which is available only in page-sized chunks; thus when you call kmalloc()
you are going to get only certain predefined, fixed-size byte arrays.
The actual memory you get back is dependent on the system's architecture, but the smallest allocation that kmalloc can handle is as big as 32 or 64 bytes. You will get back from a call to kmalloc()
at least as much memory as you asked for (usually more). Typically you will not get more than 128 KB (again, architecture dependent)
To get the page size (in bytes) of your system you can execute the command:
getconf PAGESIZE
or
getconf PAGE_SIZE
This information on max page size is in /usr/src/linux/include/linux/slab.h
And yes, the page sizes are generally powers of 2, but again, you're not going to get exactly what you ask for, but a little bit more.
You can use some code like this:
void * stuff;
stuff = kmalloc(1,GFP_KERNEL);
printk("I got: %zu bytes of memory
", ksize(stuff));
kfree(stuff);
To show the actual amount of memory allocated:
[90144.702588] I got: 32 bytes of memory
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…