1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE slides SYSTEM "/usr/share/xml/docbook/custom/slides/3.3.1/schema/dtd/slides-full.dtd">
7 <title>Memory Management</title>
8 <author><firstname>Thadeu</firstname><surname>Cascardo</surname></author>
12 <title>Introduction</title>
15 Kernel space has small stack, in the order of one or two small pages.
18 Memory allocation must be efficient in big SMP systems.
21 Memory protection and virtualization is done through paging.
24 The slab allocator has multiple implementations: SLAB, SLUB, SLOB and SLQB.
30 <title>kmalloc/kfree</title>
33 kmalloc and kfree are equivalent parts for malloc and free from user space, but
34 for a flags parameter in kmalloc.
37 The flags parameter depends on the context, which we'll see and revise next.
38 Default usage should be GFP\_KERNEL.
41 The slab allocator implements kmalloc.
47 <title>More about kmalloc</title>
50 Allocated memory by kmalloc is contiguous in physical memory.
53 It does not clear memory, use kzalloc for that.
56 kzfree has been introduced recently, in 2.6.29.
62 <title>Lookaside Caches</title>
65 For efficient allocation of many objects of a predefined size, use lookaside
69 You should use <emphasis>kmem\_cache\_create</emphasis> to allocate a
70 <emphasis>kmem\_cache\_t</emphasis>. It has a name, an object size, alignment,
71 flags and a constructor.
74 The destructor parameter has been removed, since 2.6.27.
77 It is destroyed with <emphasis>kmem\_cache\_destroy</emphasis>.
83 <title>Using lookaside caches</title>
86 Use it with <emphasis>kmem\_cache\_alloc</emphasis> and release the object with
87 <emphasis>kmem\_cache\_free</emphasis>.
93 <title>Lookaside cache example</title>
99 <title>vmalloc</title>
102 For large sizes of memory, vmalloc should be used. However, it does not allocate
103 a contiguous physical memory range.
106 Use vfree to release this memory.
109 vmalloc get many memory pages and map them to a contiguous virtual memory range.
110 However, this virtual memory address is in a differente range from that used by
111 kmalloc and other allocation functions.
117 <title>Per-CPU variables</title>
120 Sometimes it is best to use one variable per CPU to avoi some concurrency.
123 Define the variable using <emphasis>DEFINE\_PER\_CPU</emphasis> macro.
126 The pair <emphasis>get\_cpu\_var</emphasis> and
127 <emphasis>put\_cpu\_var</emphasis> must be used to access these variables.