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>struct kmem\_cache</emphasis>. It has a name, an object size,
71 alignment, flags and a constructor.
74 The destructor parameter has been removed, since 2.6.27.
77 The typedef <emphasis>kmem\_cache\_t</emphasis> was removed in 2.6.23.
80 It is destroyed with <emphasis>kmem\_cache\_destroy</emphasis>.
86 <title>Using lookaside caches</title>
89 Use it with <emphasis>kmem\_cache\_alloc</emphasis> and release the object with
90 <emphasis>kmem\_cache\_free</emphasis>.
96 <title>Lookaside cache example</title>
102 <title>vmalloc</title>
105 For large sizes of memory, vmalloc should be used. However, it does not allocate
106 a contiguous physical memory range.
109 Use vfree to release this memory.
112 vmalloc get many memory pages and map them to a contiguous virtual memory range.
113 However, this virtual memory address is in a differente range from that used by
114 kmalloc and other allocation functions.
120 <title>Per-CPU variables</title>
123 Sometimes it is best to use one variable per CPU to avoi some concurrency.
126 Define the variable using <emphasis>DEFINE\_PER\_CPU</emphasis> macro.
129 The pair <emphasis>get\_cpu\_var</emphasis> and
130 <emphasis>put\_cpu\_var</emphasis> must be used to access these variables.