55010a773e5cf42c4b90cbe0cacc880650fd751f
[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>struct kmem\_cache</emphasis>. It has a name, an object size,
71 alignment, flags and a constructor.
72 </listitem>
73 <listitem>
74 The destructor parameter has been removed, since 2.6.27.
75 </listitem>
76 <listitem>
77 The typedef <emphasis>kmem\_cache\_t</emphasis> was removed in 2.6.23.
78 </listitem>
79 <listitem>
80 It is destroyed with <emphasis>kmem\_cache\_destroy</emphasis>.
81 </listitem>
82 </itemizedlist>
83 </foil>
84
85 <foil>
86 <title>Using lookaside caches</title>
87 <itemizedlist>
88 <listitem>
89 Use it with <emphasis>kmem\_cache\_alloc</emphasis> and release the object with
90 <emphasis>kmem\_cache\_free</emphasis>.
91 </listitem>
92 </itemizedlist>
93 </foil>
94
95 <foil>
96 <title>Lookaside cache example</title>
97 <screen>
98 </screen>
99 </foil>
100
101 <foil>
102 <title>vmalloc</title>
103 <itemizedlist>
104 <listitem>
105 For large sizes of memory, vmalloc should be used. However, it does not allocate
106 a contiguous physical memory range.
107 </listitem>
108 <listitem>
109 Use vfree to release this memory.
110 </listitem>
111 <listitem>
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.
115 </listitem>
116 </itemizedlist>
117 </foil>
118
119 <foil>
120 <title>Per-CPU variables</title>
121 <itemizedlist>
122 <listitem>
123 Sometimes it is best to use one variable per CPU to avoi some concurrency.
124 </listitem>
125 <listitem>
126 Define the variable using <emphasis>DEFINE\_PER\_CPU</emphasis> macro.
127 </listitem>
128 <listitem>
129 The pair <emphasis>get\_cpu\_var</emphasis> and
130 <emphasis>put\_cpu\_var</emphasis> must be used to access these variables.
131 </listitem>
132 </itemizedlist>
133 </foil>
134
135 </slides>