6b5f73a1747cf925c5ce95aea9e956ac473cf032
[cascardo/kernel/old_slides/.git] / 05.memory / 05.memory.xml
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">
3
4 <slides>
5
6 <slidesinfo>
7 <title>Memory Management</title>
8 <author><firstname>Thadeu</firstname><surname>Cascardo</surname></author>
9 </slidesinfo>
10
11 <foil>
12 <title>Introduction</title>
13 <itemizedlist>
14 <listitem>
15 Kernel space has small stack, in the order of one or two small pages.
16 </listitem>
17 <listitem>
18 Memory allocation must be efficient in big SMP systems.
19 </listitem>
20 <listitem>
21 Memory protection and virtualization is done through paging.
22 </listitem>
23 <listitem>
24 The slab allocator has multiple implementations: SLAB, SLUB, SLOB and SLQB.
25 </listitem>
26 </itemizedlist>
27 </foil>
28
29 <foil>
30 <title>kmalloc/kfree</title>
31 <itemizedlist>
32 <listitem>
33 kmalloc and kfree are equivalent parts for malloc and free from user space, but
34 for a flags parameter in kmalloc.
35 </listitem>
36 <listitem>
37 The flags parameter depends on the context, which we'll see and revise next.
38 Default usage should be GFP\_KERNEL.
39 </listitem>
40 <listitem>
41 The slab allocator implements kmalloc.
42 </listitem>
43 </itemizedlist>
44 </foil>
45
46 <foil>
47 <title>More about kmalloc</title>
48 <itemizedlist>
49 <listitem>
50 Allocated memory by kmalloc is contiguous in physical memory.
51 </listitem>
52 <listitem>
53 It does not clear memory, use kzalloc for that.
54 </listitem>
55 <listitem>
56 kzfree has been introduced recently, in 2.6.29.
57 </listitem>
58 </itemizedlist>
59 </foil>
60
61 <foil>
62 <title>Lookaside Caches</title>
63 <itemizedlist>
64 <listitem>
65 For efficient allocation of many objects of a predefined size, use lookaside
66 caches.
67 </listitem>
68 <listitem>
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.
72 </listitem>
73 <listitem>
74 The destructor parameter has been removed, since 2.6.27.
75 </listitem>
76 <listitem>
77 It is destroyed with <emphasis>kmem\_cache\_destroy</emphasis>.
78 </listitem>
79 </itemizedlist>
80 </foil>
81
82 <foil>
83 <title>Using lookaside caches</title>
84 <itemizedlist>
85 <listitem>
86 Use it with <emphasis>kmem\_cache\_alloc</emphasis> and release the object with
87 <emphasis>kmem\_cache\_free</emphasis>.
88 </listitem>
89 </itemizedlist>
90 </foil>
91
92 <foil>
93 <title>Lookaside cache example</title>
94 <screen>
95 </screen>
96 </foil>
97
98 <foil>
99 <title>vmalloc</title>
100 <itemizedlist>
101 <listitem>
102 For large sizes of memory, vmalloc should be used. However, it does not allocate
103 a contiguous physical memory range.
104 </listitem>
105 <listitem>
106 Use vfree to release this memory.
107 </listitem>
108 <listitem>
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.
112 </listitem>
113 </itemizedlist>
114 </foil>
115
116 <foil>
117 <title>Per-CPU variables</title>
118 <itemizedlist>
119 <listitem>
120 Sometimes it is best to use one variable per CPU to avoi some concurrency.
121 </listitem>
122 <listitem>
123 Define the variable using <emphasis>DEFINE\_PER\_CPU</emphasis> macro.
124 </listitem>
125 <listitem>
126 The pair <emphasis>get\_cpu\_var</emphasis> and
127 <emphasis>put\_cpu\_var</emphasis> must be used to access these variables.
128 </listitem>
129 </itemizedlist>
130 </foil>
131
132 </slides>