Does KMALLOC allocates only in page size memory or it can allocate less ? What are the sizes that the kmalloc can allocate ? Where can I find description of it, as everywher
It all depends on the allocator used in your kernel. Slab, Slub or Slob. See Following kernel configuration variables:
CONFIG_SLAB
CONFIG_SLUB
CONFIG_SLOB
All above allocators use MAX_ORDER and PAGE_SHIFT to decide the maximum limit of kmalloc()
The largest kmalloc size supported by the SLAB allocators is 32 megabyte (2^25) or the maximum allocatable page order if that is less than 32 MB.
#define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
(MAX_ORDER + PAGE_SHIFT - 1) : 25)
#define KMALLOC_SHIFT_MAX KMALLOC_SHIFT_HIGH
SLUB directly allocates requests fitting in to an order-1 page (PAGE_SIZE*2
). Larger requests are passed to the page allocator.
#define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
#define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT)
SLOB passes all requests larger than one page to the page allocator. No kmalloc array is necessary since objects of different sizes can be allocated from the same page.
#define KMALLOC_SHIFT_HIGH PAGE_SHIFT
#define KMALLOC_SHIFT_MAX 30
Maximum allocatable size by kmalloc() is
#define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
Minimum allocatable size from kmalloc() is
#define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
Default KMALLOC_SHIFT_LOW
for slab is 5, and for slub and slob it is 3.