From 7075c56014b0581e412c6d6b2eaabb531e564d21 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 9 Dec 2009 08:19:07 -0200 Subject: [PATCH] Some memory management/allocation stuff. --- 05.memory/05.memory.xml | 132 ++++++++++++++++++++++++++++++++++++++++ 05.memory/Makefile | 13 ++++ 2 files changed, 145 insertions(+) create mode 100644 05.memory/05.memory.xml create mode 100644 05.memory/Makefile diff --git a/05.memory/05.memory.xml b/05.memory/05.memory.xml new file mode 100644 index 0000000..6b5f73a --- /dev/null +++ b/05.memory/05.memory.xml @@ -0,0 +1,132 @@ + + + + + + +Memory Management +ThadeuCascardo + + + +Introduction + + +Kernel space has small stack, in the order of one or two small pages. + + +Memory allocation must be efficient in big SMP systems. + + +Memory protection and virtualization is done through paging. + + +The slab allocator has multiple implementations: SLAB, SLUB, SLOB and SLQB. + + + + + +kmalloc/kfree + + +kmalloc and kfree are equivalent parts for malloc and free from user space, but +for a flags parameter in kmalloc. + + +The flags parameter depends on the context, which we'll see and revise next. +Default usage should be GFP\_KERNEL. + + +The slab allocator implements kmalloc. + + + + + +More about kmalloc + + +Allocated memory by kmalloc is contiguous in physical memory. + + +It does not clear memory, use kzalloc for that. + + +kzfree has been introduced recently, in 2.6.29. + + + + + +Lookaside Caches + + +For efficient allocation of many objects of a predefined size, use lookaside +caches. + + +You should use kmem\_cache\_create to allocate a +kmem\_cache\_t. It has a name, an object size, alignment, +flags and a constructor. + + +The destructor parameter has been removed, since 2.6.27. + + +It is destroyed with kmem\_cache\_destroy. + + + + + +Using lookaside caches + + +Use it with kmem\_cache\_alloc and release the object with +kmem\_cache\_free. + + + + + +Lookaside cache example + + + + + +vmalloc + + +For large sizes of memory, vmalloc should be used. However, it does not allocate +a contiguous physical memory range. + + +Use vfree to release this memory. + + +vmalloc get many memory pages and map them to a contiguous virtual memory range. +However, this virtual memory address is in a differente range from that used by +kmalloc and other allocation functions. + + + + + +Per-CPU variables + + +Sometimes it is best to use one variable per CPU to avoi some concurrency. + + +Define the variable using DEFINE\_PER\_CPU macro. + + +The pair get\_cpu\_var and +put\_cpu\_var must be used to access these variables. + + + + + diff --git a/05.memory/Makefile b/05.memory/Makefile new file mode 100644 index 0000000..5e170ad --- /dev/null +++ b/05.memory/Makefile @@ -0,0 +1,13 @@ +NAME = 05.memory + +all: $(NAME).pdf + +%.pdf: %.tex + TEXINPUTS=.:..: pdflatex $< + +%.tex: %.xml ../beamer.xsl + xsltproc ../beamer.xsl $< > $@ + +clean: + rm -f $(NAME).pdf $(NAME).tex $(NAME).aux $(NAME).log $(NAME).nav \ + $(NAME).out $(NAME).snm $(NAME).toc $(NAME).vrb -- 2.20.1