Subversion Repositories HelenOS-doc

Rev

Rev 46 | Rev 54 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 bondari 1
<?xml version="1.0" encoding="UTF-8"?>
11 bondari 2
<chapter id="mm">
3
  <?dbhtml filename="mm.html"?>
9 bondari 4
 
11 bondari 5
  <title>Memory management</title>
9 bondari 6
 
26 bondari 7
  <section>
11 bondari 8
    <title>Virtual memory management</title>
9 bondari 9
 
10
    <section>
35 bondari 11
      <title>Introduction</title>
12
 
13
      <para>Virtual memory is a special memory management technique, used by
14
      kernel to achieve a bunch of mission critical goals. <itemizedlist>
15
          <listitem>
16
             Isolate each task from other tasks that are running on the system at the same time.
17
          </listitem>
18
 
19
          <listitem>
20
             Allow to allocate more memory, than is actual physical memory size of the machine.
21
          </listitem>
22
 
23
          <listitem>
24
             Allowing, in general, to load and execute two programs that are linked on the same address without complicated relocations.
25
          </listitem>
26
        </itemizedlist></para>
38 bondari 27
 
39 bondari 28
      <para><!--
29
 
38 bondari 30
                TLB shootdown ASID/ASID:PAGE/ALL.
31
                TLB shootdown requests can come in asynchroniously
32
                so there is a cache of TLB shootdown requests. Upon cache overflow TLB shootdown ALL is executed
33
 
34
 
35
                <para>
36
                        Address spaces. Address space area (B+ tree). Only for uspace. Set of syscalls (shrink/extend etc).
37
                        Special address space area type - device - prohibits shrink/extend syscalls to call on it.
38
                        Address space has link to mapping tables (hierarchical - per Address space, hash - global tables).
39
                </para>
40
 
41
--></para>
35 bondari 42
    </section>
43
 
44
    <section>
45
      <title>Paging</title>
46
 
47
      <para>Virtual memory is usually using paged memory model, where virtual
48
      memory address space is divided into the <emphasis>pages</emphasis>
49
      (usually having size 4096 bytes) and physical memory is divided into the
39 bondari 50
      frames (same sized as a page, of course). Each page may be mapped to
51
      some frame and then, upon memory access to the virtual address, CPU
52
      performs <emphasis>address translation</emphasis> during the instruction
35 bondari 53
      execution. Non-existing mapping generates page fault exception, calling
54
      kernel exception handler, thus allowing kernel to manipulate rules of
55
      memory access. Information for pages mapping is stored by kernel in the
56
      <link linkend="page_tables">page tables</link></para>
57
 
58
      <para>The majority of the architectures use multi-level page tables,
59
      which means need to access physical memory several times before getting
60
      physical address. This fact would make serios performance overhead in
61
      virtual memory management. To avoid this <link linkend="tlb">Traslation
62
      Lookaside Buffer (TLB)</link> is used.</para>
63
    </section>
64
 
65
    <section>
11 bondari 66
      <title>Address spaces</title>
9 bondari 67
 
35 bondari 68
      <section>
46 bondari 69
        <title>Address space areas</title>
35 bondari 70
 
46 bondari 71
        <para>Each address space consists of mutually disjunctive continuous
72
        address space areas. Address space area is precisely defined by its
47 bondari 73
        base address and the number of frames/pages is contains.</para>
35 bondari 74
 
47 bondari 75
        <para>Address space area , that define behaviour and permissions on
76
        the particular area. <itemizedlist>
46 bondari 77
            <listitem>
78
 
79
 
80
              <emphasis>AS_AREA_READ</emphasis>
81
 
82
               flag indicates reading permission.
83
            </listitem>
84
 
85
            <listitem>
86
 
87
 
88
              <emphasis>AS_AREA_WRITE</emphasis>
89
 
90
               flag indicates writing permission.
91
            </listitem>
92
 
93
            <listitem>
94
 
95
 
96
              <emphasis>AS_AREA_EXEC</emphasis>
97
 
98
               flag indicates code execution permission. Some architectures do not support execution persmission restriction. In this case this flag has no effect.
99
            </listitem>
100
 
101
            <listitem>
102
 
103
 
104
              <emphasis>AS_AREA_DEVICE</emphasis>
105
 
106
               marks area as mapped to the device memory.
107
            </listitem>
108
          </itemizedlist></para>
109
 
110
        <para>Kernel provides possibility tasks create/expand/shrink/share its
111
        address space via the set of syscalls.</para>
35 bondari 112
      </section>
113
 
114
      <section>
115
        <title>Address Space ID (ASID)</title>
116
 
46 bondari 117
        <para>When switching to the different task, kernel also require to
118
        switch mappings to the different address space. In case TLB cannot
47 bondari 119
        distinguish address space mappings, all mapping information in TLB
120
        from the old address space must be flushed, which can create certain
121
        uncessary overhead during the task switching. To avoid this, some
122
        architectures have capability to segregate different address spaces on
123
        hardware level introducing the address space identifier as a part of
124
        TLB record, telling the virtual address space translation unit to
125
        which address space this record is applicable.</para>
35 bondari 126
 
46 bondari 127
        <para>HelenOS kernel can take advantage of this hardware supported
47 bondari 128
        identifier by having an ASID abstraction which is somehow related to
129
        the corresponding architecture identifier. I.e. on ia64 kernel ASID is
130
        derived from RID (region identifier) and on the mips32 kernel ASID is
131
        actually the hardware identifier. As expected, this ASID information
132
        record is the part of <emphasis>as_t</emphasis> structure.</para>
35 bondari 133
 
47 bondari 134
        <para>Due to the hardware limitations, hardware ASID has limited
135
        length from 8 bits on ia64 to 24 bits on mips32, which makes it
136
        impossible to use it as unique address space identifier for all tasks
137
        running in the system. In such situations special ASID stealing
138
        algoritm is used, which takes ASID from inactive task and assigns it
139
        to the active task.</para>
140
 
141
        <para><classname>ASID stealing algoritm here.</classname></para>
35 bondari 142
      </section>
9 bondari 143
    </section>
144
 
145
    <section>
11 bondari 146
      <title>Virtual address translation</title>
9 bondari 147
 
35 bondari 148
      <section id="page_tables">
149
        <title>Page tables</title>
34 bondari 150
 
35 bondari 151
        <para>HelenOS kernel has two different approaches to the paging
152
        implementation: <emphasis>4 level page tables</emphasis> and
153
        <emphasis>global hash tables</emphasis>, which are accessible via
47 bondari 154
        generic paging abstraction layer. Such different functionality was
155
        caused by the major architectural differences between supported
156
        platforms. This abstraction is implemented with help of the global
157
        structure of pointers to basic mapping functions
158
        <emphasis>page_mapping_operations</emphasis>. To achieve different
159
        functionality of page tables, corresponding layer must implement
160
        functions, declared in
161
        <emphasis>page_mapping_operations</emphasis></para>
34 bondari 162
 
35 bondari 163
        <formalpara>
164
          <title>4-level page tables</title>
34 bondari 165
 
35 bondari 166
          <para>4-level page tables are the generalization of the hardware
47 bondari 167
          capabilities of several architectures.<itemizedlist>
35 bondari 168
              <listitem>
169
                 ia32 uses 2-level page tables, with full hardware support.
170
              </listitem>
34 bondari 171
 
35 bondari 172
              <listitem>
173
                 amd64 uses 4-level page tables, also coming with full hardware support.
174
              </listitem>
175
 
176
              <listitem>
177
                 mips and ppc32 have 2-level tables, software simulated support.
178
              </listitem>
179
            </itemizedlist></para>
180
        </formalpara>
181
 
182
        <formalpara>
183
          <title>Global hash tables</title>
184
 
185
          <para>- global page hash table: existuje jen jedna v celem systemu
186
          (vyuziva ji ia64), pozn. ia64 ma zatim vypnuty VHPT. Pouziva se
46 bondari 187
          genericke hash table s oddelenymi collision chains. ASID support is
188
          required to use global hash tables.</para>
35 bondari 189
        </formalpara>
190
 
191
        <para>Thanks to the abstract paging interface, there is possibility
192
        left have more paging implementations, for example B-Tree page
193
        tables.</para>
194
      </section>
195
 
196
      <section id="tlb">
46 bondari 197
        <title>Translation Lookaside Buffer</title>
35 bondari 198
 
199
        <para>- TLB cachuji informace ve strankovacich tabulkach; alternativne
200
        se lze na strankovaci tabulky (ci ruzne hw rozsireni [e.g. VHPT, ppc32
201
        hw hash table]) divat jako na velke TLB</para>
202
 
203
        <para>- pri modifikaci mapovani nebo odstraneni mapovani ze
204
        strankovacich tabulek je potreba zajistit konsistenci TLB a techto
205
        tabulek; nutne delat na vsech CPU; na to mame zjednodusenou verzi TLB
206
        shootdown mechanismu; je to variace na algoritmus popsany zde: D.
207
        Black et al., "Translation Lookaside Buffer Consistency: A Software
208
        Approach," Proc. Third Int'l Conf. Architectural Support for
209
        Programming Languages and Operating Systems, 1989, pp. 113-122.</para>
210
 
211
        <para>- nutno poznamenat, ze existuji odlehcenejsi verze TLB shootdown
46 bondari 212
        algoritm</para>
35 bondari 213
      </section>
214
    </section>
46 bondari 215
 
216
    <section>
217
      <title>---</title>
218
 
219
      <para>At the moment HelenOS does not support swapping.</para>
220
 
221
      <para>- pouzivame vypadky stranky k alokaci ramcu on-demand v ramci
222
      as_area - na architekturach, ktere to podporuji, podporujeme non-exec
223
      stranky</para>
224
    </section>
26 bondari 225
  </section>
9 bondari 226
 
26 bondari 227
  <!-- End of VM -->
24 bondari 228
 
26 bondari 229
  <section>
230
    <!-- Phys mem -->
231
 
11 bondari 232
    <title>Physical memory management</title>
9 bondari 233
 
24 bondari 234
    <section id="zones_and_frames">
235
      <title>Zones and frames</title>
236
 
34 bondari 237
      <para><!--graphic fileref="images/mm2.png" /--><!--graphic fileref="images/buddy_alloc.svg" format="SVG" /--></para>
26 bondari 238
 
239
      <para>On some architectures not whole physical memory is available for
240
      conventional usage. This limitations require from kernel to maintain a
241
      table of available and unavailable ranges of physical memory addresses.
242
      Main idea of zones is in creating memory zone entity, that is a
243
      continuous chunk of memory available for allocation. If some chunk is
244
      not available, we simply do not put it in any zone.</para>
245
 
246
      <para>Zone is also serves for informational purposes, containing
247
      information about number of free and busy frames. Physical memory
248
      allocation is also done inside the certain zone. Allocation of zone
249
      frame must be organized by the <link linkend="frame_allocator">frame
250
      allocator</link> associated with the zone.</para>
251
 
252
      <para>Some of the architectures (mips32, ppc32) have only one zone, that
253
      covers whole physical memory, and the others (like ia32) may have
254
      multiple zones. Information about zones on current machine is stored in
255
      BIOS hardware tables or can be hardcoded into kernel during compile
256
      time.</para>
24 bondari 257
    </section>
258
 
259
    <section id="frame_allocator">
260
      <title>Frame allocator</title>
261
 
39 bondari 262
      <para><mediaobject id="frame_alloc">
263
          <imageobject role="html">
264
            <imagedata fileref="images/frame_alloc.png" format="PNG" />
265
          </imageobject>
266
 
267
          <imageobject role="fop">
268
            <imagedata fileref="images.vector/frame_alloc.svg" format="SVG" />
269
          </imageobject>
270
        </mediaobject></para>
271
 
26 bondari 272
      <formalpara>
273
        <title>Overview</title>
24 bondari 274
 
26 bondari 275
        <para>Frame allocator provides physical memory allocation for the
276
        kernel. Because of zonal organization of physical memory, frame
277
        allocator is always working in context of some zone, thus making
278
        impossible to allocate a piece of memory, which lays in different
279
        zone, which cannot happen, because two adjacent zones can be merged
280
        into one. Frame allocator is also being responsible to update
281
        information on the number of free/busy frames in zone. Physical memory
282
        allocation inside one <link linkend="zones_and_frames">memory
283
        zone</link> is being handled by an instance of <link
284
        linkend="buddy_allocator">buddy allocator</link> tailored to allocate
285
        blocks of physical memory frames.</para>
286
      </formalpara>
24 bondari 287
 
26 bondari 288
      <formalpara>
289
        <title>Allocation / deallocation</title>
24 bondari 290
 
26 bondari 291
        <para>Upon allocation request, frame allocator tries to find first
292
        zone, that can satisfy the incoming request (has required amount of
293
        free frames to allocate). During deallocation, frame allocator needs
294
        to find zone, that contain deallocated frame. This approach could
295
        bring up two potential problems: <itemizedlist>
296
            <listitem>
297
               Linear search of zones does not any good to performance, but number of zones is not expected to be high. And if yes, list of zones can be replaced with more time-efficient B-tree.
298
            </listitem>
24 bondari 299
 
26 bondari 300
            <listitem>
301
               Quickly find out if zone contains required number of frames to allocate and if this chunk of memory is properly aligned. This issue is perfectly solved bu the buddy allocator.
302
            </listitem>
303
          </itemizedlist></para>
304
      </formalpara>
305
    </section>
17 jermar 306
 
34 bondari 307
    <section id="buddy_allocator">
308
      <title>Buddy allocator</title>
17 jermar 309
 
34 bondari 310
      <section>
311
        <title>Overview</title>
17 jermar 312
 
39 bondari 313
        <para><mediaobject id="buddy_alloc">
314
            <imageobject role="html">
315
              <imagedata fileref="images/buddy_alloc.png" format="PNG" />
316
            </imageobject>
317
 
318
            <imageobject role="fop">
319
              <imagedata fileref="images.vector/buddy_alloc.svg" format="SVG" />
320
            </imageobject>
321
          </mediaobject></para>
322
 
45 jermar 323
        <para>In the buddy allocator, the memory is broken down into
324
        power-of-two sized naturally aligned blocks. These blocks are
325
        organized in an array of lists, in which the list with index i
326
        contains all unallocated blocks of size
327
        <mathphrase>2<superscript>i</superscript></mathphrase>. The index i is
328
        called the order of block. Should there be two adjacent equally sized
46 bondari 329
        blocks in the list i<mathphrase />(i.e. buddies), the buddy allocator
330
        would coalesce them and put the resulting block in list <mathphrase>i
331
        + 1</mathphrase>, provided that the resulting block would be naturally
332
        aligned. Similarily, when the allocator is asked to allocate a block
333
        of size <mathphrase>2<superscript>i</superscript></mathphrase>, it
334
        first tries to satisfy the request from the list with index i. If the
335
        request cannot be satisfied (i.e. the list i is empty), the buddy
336
        allocator will try to allocate and split a larger block from the list
337
        with index i + 1. Both of these algorithms are recursive. The
338
        recursion ends either when there are no blocks to coalesce in the
339
        former case or when there are no blocks that can be split in the
340
        latter case.</para>
17 jermar 341
 
34 bondari 342
        <!--graphic fileref="images/mm1.png" format="EPS" /-->
17 jermar 343
 
34 bondari 344
        <para>This approach greatly reduces external fragmentation of memory
345
        and helps in allocating bigger continuous blocks of memory aligned to
346
        their size. On the other hand, the buddy allocator suffers increased
347
        internal fragmentation of memory and is not suitable for general
348
        kernel allocations. This purpose is better addressed by the <link
349
        linkend="slab">slab allocator</link>.</para>
350
      </section>
17 jermar 351
 
34 bondari 352
      <section>
353
        <title>Implementation</title>
17 jermar 354
 
34 bondari 355
        <para>The buddy allocator is, in fact, an abstract framework wich can
356
        be easily specialized to serve one particular task. It knows nothing
357
        about the nature of memory it helps to allocate. In order to beat the
358
        lack of this knowledge, the buddy allocator exports an interface that
45 jermar 359
        each of its clients is required to implement. When supplied with an
34 bondari 360
        implementation of this interface, the buddy allocator can use
45 jermar 361
        specialized external functions to find a buddy for a block, split and
34 bondari 362
        coalesce blocks, manipulate block order and mark blocks busy or
45 jermar 363
        available. For precise documentation of this interface, refer to
39 bondari 364
        <emphasis>"HelenOS Generic Kernel Reference Manual"</emphasis>.</para>
17 jermar 365
 
34 bondari 366
        <formalpara>
367
          <title>Data organization</title>
17 jermar 368
 
34 bondari 369
          <para>Each entity allocable by the buddy allocator is required to
370
          contain space for storing block order number and a link variable
371
          used to interconnect blocks within the same order.</para>
15 bondari 372
 
34 bondari 373
          <para>Whatever entities are allocated by the buddy allocator, the
374
          first entity within a block is used to represent the entire block.
375
          The first entity keeps the order of the whole block. Other entities
376
          within the block are assigned the magic value
377
          <constant>BUDDY_INNER_BLOCK</constant>. This is especially important
45 jermar 378
          for effective identification of buddies in a one-dimensional array
34 bondari 379
          because the entity that represents a potential buddy cannot be
380
          associated with <constant>BUDDY_INNER_BLOCK</constant> (i.e. if it
381
          is associated with <constant>BUDDY_INNER_BLOCK</constant> then it is
382
          not a buddy).</para>
15 bondari 383
 
45 jermar 384
          <para>The buddy allocator always uses the first frame to represent
385
          the frame block. This frame contains <varname>buddy_order</varname>
386
          variable to provide information about the block size it actually
387
          represents (
34 bondari 388
          <mathphrase>2<superscript>buddy_order</superscript></mathphrase>
389
          frames block). Other frames in block have this value set to magic
390
          <constant>BUDDY_INNER_BLOCK</constant> that is much greater than
391
          buddy <varname>max_order</varname> value.</para>
15 bondari 392
 
34 bondari 393
          <para>Each <varname>frame_t</varname> also contains pointer member
394
          to hold frame structure in the linked list inside one order.</para>
395
        </formalpara>
15 bondari 396
 
34 bondari 397
        <formalpara>
398
          <title>Allocation algorithm</title>
15 bondari 399
 
34 bondari 400
          <para>Upon <mathphrase>2<superscript>i</superscript></mathphrase>
401
          frames block allocation request, allocator checks if there are any
402
          blocks available at the order list <varname>i</varname>. If yes,
403
          removes block from order list and returns its address. If no,
404
          recursively allocates
405
          <mathphrase>2<superscript>i+1</superscript></mathphrase> frame
406
          block, splits it into two
407
          <mathphrase>2<superscript>i</superscript></mathphrase> frame blocks.
408
          Then adds one of the blocks to the <varname>i</varname> order list
409
          and returns address of another.</para>
410
        </formalpara>
15 bondari 411
 
34 bondari 412
        <formalpara>
413
          <title>Deallocation algorithm</title>
17 jermar 414
 
34 bondari 415
          <para>Check if block has so called buddy (another free
416
          <mathphrase>2<superscript>i</superscript></mathphrase> frame block
417
          that can be linked with freed block into the
418
          <mathphrase>2<superscript>i+1</superscript></mathphrase> block).
419
          Technically, buddy is a odd/even block for even/odd block
420
          respectively. Plus we can put an extra requirement, that resulting
421
          block must be aligned to its size. This requirement guarantees
422
          natural block alignment for the blocks coming out the allocation
423
          system.</para>
9 bondari 424
 
34 bondari 425
          <para>Using direct pointer arithmetics,
426
          <varname>frame_t::ref_count</varname> and
427
          <varname>frame_t::buddy_order</varname> variables, finding buddy is
428
          done at constant time.</para>
429
        </formalpara>
430
      </section>
26 bondari 431
    </section>
432
 
15 bondari 433
    <section id="slab">
11 bondari 434
      <title>Slab allocator</title>
9 bondari 435
 
26 bondari 436
      <section>
34 bondari 437
        <title>Overview</title>
9 bondari 438
 
34 bondari 439
        <para><termdef><glossterm>Slab</glossterm> represents a contiguous
440
        piece of memory, usually made of several physically contiguous
441
        pages.</termdef> <termdef><glossterm>Slab cache</glossterm> consists
442
        of one or more slabs.</termdef></para>
443
 
26 bondari 444
        <para>The majority of memory allocation requests in the kernel are for
445
        small, frequently used data structures. For this purpose the slab
34 bondari 446
        allocator is a perfect solution. The basic idea behind the slab
26 bondari 447
        allocator is to have lists of commonly used objects available packed
448
        into pages. This avoids the overhead of allocating and destroying
34 bondari 449
        commonly used types of objects such threads, virtual memory structures
450
        etc. Also due to the exact allocated size matching, slab allocation
451
        completely eliminates internal fragmentation issue.</para>
26 bondari 452
      </section>
24 bondari 453
 
26 bondari 454
      <section>
34 bondari 455
        <title>Implementation</title>
9 bondari 456
 
39 bondari 457
        <para><mediaobject id="slab_alloc">
458
            <imageobject role="html">
459
              <imagedata fileref="images/slab_alloc.png" format="PNG" />
460
            </imageobject>
461
 
462
            <imageobject role="fop">
463
              <imagedata fileref="images.vector/slab_alloc.svg" format="SVG" />
464
            </imageobject>
465
          </mediaobject></para>
466
 
26 bondari 467
        <para>The SLAB allocator is closely modelled after <ulink
468
        url="http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/">
469
        OpenSolaris SLAB allocator by Jeff Bonwick and Jonathan Adams </ulink>
470
        with the following exceptions: <itemizedlist>
471
            <listitem>
472
               empty SLABS are deallocated immediately (in Linux they are kept in linked list, in Solaris ???)
473
            </listitem>
474
 
475
            <listitem>
476
               empty magazines are deallocated when not needed (in Solaris they are held in linked list in slab cache)
477
            </listitem>
478
          </itemizedlist> Following features are not currently supported but
479
        would be easy to do: <itemizedlist>
480
            <listitem>
481
               - cache coloring
482
            </listitem>
483
 
484
            <listitem>
34 bondari 485
               - dynamic magazine grow (different magazine sizes are already supported, but we would need to adjust allocation strategy)
26 bondari 486
            </listitem>
487
          </itemizedlist></para>
488
 
34 bondari 489
        <section>
490
          <title>Magazine layer</title>
26 bondari 491
 
34 bondari 492
          <para>Due to the extensive bottleneck on SMP architures, caused by
493
          global SLAB locking mechanism, making processing of all slab
494
          allocation requests serialized, a new layer was introduced to the
495
          classic slab allocator design. Slab allocator was extended to
496
          support per-CPU caches 'magazines' to achieve good SMP scaling.
497
          <termdef>Slab SMP perfromance bottleneck was resolved by introducing
498
          a per-CPU caching scheme called as <glossterm>magazine
499
          layer</glossterm></termdef>.</para>
26 bondari 500
 
34 bondari 501
          <para>Magazine is a N-element cache of objects, so each magazine can
502
          satisfy N allocations. Magazine behaves like a automatic weapon
503
          magazine (LIFO, stack), so the allocation/deallocation become simple
504
          push/pop pointer operation. Trick is that CPU does not access global
505
          slab allocator data during the allocation from its magazine, thus
506
          making possible parallel allocations between CPUs.</para>
26 bondari 507
 
34 bondari 508
          <para>Implementation also requires adding another feature as the
509
          CPU-bound magazine is actually a pair of magazines to avoid
510
          thrashing when during allocation/deallocatiion of 1 item at the
511
          magazine size boundary. LIFO order is enforced, which should avoid
512
          fragmentation as much as possible.</para>
26 bondari 513
 
46 bondari 514
          <para>Another important entity of magazine layer is the common full
515
          magazine list (also called a depot), that stores full magazines that
516
          may be used by any of the CPU magazine caches to reload active CPU
517
          magazine. This list of magazines can be pre-filled with full
518
          magazines during initialization, but in current implementation it is
519
          filled during object deallocation, when CPU magazine becomes
520
          full.</para>
26 bondari 521
 
34 bondari 522
          <para>Slab allocator control structures are allocated from special
523
          slabs, that are marked by special flag, indicating that it should
524
          not be used for slab magazine layer. This is done to avoid possible
525
          infinite recursions and deadlock during conventional slab allocaiton
526
          requests.</para>
527
        </section>
26 bondari 528
 
34 bondari 529
        <section>
530
          <title>Allocation/deallocation</title>
26 bondari 531
 
34 bondari 532
          <para>Every cache contains list of full slabs and list of partialy
533
          full slabs. Empty slabs are immediately freed (thrashing will be
534
          avoided because of magazines).</para>
26 bondari 535
 
34 bondari 536
          <para>The SLAB allocator allocates lots of space and does not free
537
          it. When frame allocator fails to allocate the frame, it calls
538
          slab_reclaim(). It tries 'light reclaim' first, then brutal reclaim.
539
          The light reclaim releases slabs from cpu-shared magazine-list,
540
          until at least 1 slab is deallocated in each cache (this algorithm
541
          should probably change). The brutal reclaim removes all cached
542
          objects, even from CPU-bound magazines.</para>
543
 
544
          <formalpara>
545
            <title>Allocation</title>
546
 
547
            <para><emphasis>Step 1.</emphasis> When it comes to the allocation
548
            request, slab allocator first of all checks availability of memory
549
            in local CPU-bound magazine. If it is there, we would just "pop"
550
            the CPU magazine and return the pointer to object.</para>
551
 
552
            <para><emphasis>Step 2.</emphasis> If the CPU-bound magazine is
553
            empty, allocator will attempt to reload magazin, swapping it with
554
            second CPU magazine and returns to the first step.</para>
555
 
556
            <para><emphasis>Step 3.</emphasis> Now we are in the situation
557
            when both CPU-bound magazines are empty, which makes allocator to
558
            access shared full-magazines depot to reload CPU-bound magazines.
559
            If reload is succesful (meaning there are full magazines in depot)
560
            algoritm continues at Step 1.</para>
561
 
562
            <para><emphasis>Step 4.</emphasis> Final step of the allocation.
563
            In this step object is allocated from the conventional slab layer
564
            and pointer is returned.</para>
565
          </formalpara>
566
 
567
          <formalpara>
568
            <title>Deallocation</title>
569
 
570
            <para><emphasis>Step 1.</emphasis> During deallocation request,
571
            slab allocator will check if the local CPU-bound magazine is not
572
            full. In this case we will just push the pointer to this
573
            magazine.</para>
574
 
575
            <para><emphasis>Step 2.</emphasis> If the CPU-bound magazine is
576
            full, allocator will attempt to reload magazin, swapping it with
577
            second CPU magazine and returns to the first step.</para>
578
 
579
            <para><emphasis>Step 3.</emphasis> Now we are in the situation
580
            when both CPU-bound magazines are full, which makes allocator to
581
            access shared full-magazines depot to put one of the magazines to
582
            the depot and creating new empty magazine. Algoritm continues at
583
            Step 1.</para>
584
          </formalpara>
585
        </section>
26 bondari 586
      </section>
15 bondari 587
    </section>
26 bondari 588
 
589
    <!-- End of Physmem -->
590
  </section>
591
 
592
  <section>
593
    <title>Memory sharing</title>
594
 
595
    <para>Not implemented yet(?)</para>
596
  </section>
11 bondari 597
</chapter>