Linux Kernel & Device Driver Programming

Ch 8 - Allocating Memory

 

Topics


kmalloc

See kmalloc in mm/slab.c.


Flags Argument of kmalloc

basic:


Besides one of the above, additional flags maybe "or"ed in to select additional attributes of the allocation process.


Memory Zones


Memory Allocation "Lookaside" (Slab) Caches


The use of "cache" here has no direct relation to any hardware memory caching, and the word "lookaside" has nothing to with the TLB.


Constructor/Destructors


Examples

For example, see scullc.


get_free_page and "friends"


Examples

For example, see scullp.


vmalloc and "friends"


Comparisons

These all work on the kernel virtual address space. The operations to map/remap ranges of the user virtual address space (of a process) will be covered later. The main operation for mapping physical addresses into user space is remap_page_range.


Beware: vmalloc and ioremap work in entire pages, only.


Examples

For example, see scullp.


Pitfalls


Beware: You must include recovery code for the case of failure on every call to any memory allocator. If you do not you will be contributing to the number of kernel/driver bugs.

Do you know what happens when the kernel runs out of physical memory?


Per-CPU Variables


On some architectures (which provide direct support for per-CPU data) this may be a small bounded resource, so take care to keep size and number of per-CPU variables small.