blob: 6e79893d61326bb73808d64c847b631a0e1b1eca [file] [log] [blame]
Mike Rapoport2a05c582018-03-21 21:22:45 +02001.. _zsmalloc:
2
3========
Minchan Kimd02be502015-04-15 16:15:46 -07004zsmalloc
Mike Rapoport2a05c582018-03-21 21:22:45 +02005========
Minchan Kimd02be502015-04-15 16:15:46 -07006
7This allocator is designed for use with zram. Thus, the allocator is
8supposed to work well under low memory conditions. In particular, it
9never attempts higher order page allocation which is very likely to
10fail under memory pressure. On the other hand, if we just use single
11(0-order) pages, it would suffer from very high fragmentation --
12any object of size PAGE_SIZE/2 or larger would occupy an entire page.
13This was one of the major issues with its predecessor (xvmalloc).
14
15To overcome these issues, zsmalloc allocates a bunch of 0-order pages
16and links them together using various 'struct page' fields. These linked
17pages act as a single higher-order page i.e. an object can span 0-order
18page boundaries. The code refers to these linked pages as a single entity
19called zspage.
20
21For simplicity, zsmalloc can only allocate objects of size up to PAGE_SIZE
22since this satisfies the requirements of all its current users (in the
23worst case, page is incompressible and is thus stored "as-is" i.e. in
24uncompressed form). For allocation requests larger than this size, failure
25is returned (see zs_malloc).
26
27Additionally, zs_malloc() does not return a dereferenceable pointer.
28Instead, it returns an opaque handle (unsigned long) which encodes actual
29location of the allocated object. The reason for this indirection is that
30zsmalloc does not keep zspages permanently mapped since that would cause
31issues on 32-bit systems where the VA region for kernel space mappings
32is very small. So, before using the allocating memory, the object has to
33be mapped using zs_map_object() to get a usable pointer and subsequently
34unmapped using zs_unmap_object().
35
36stat
Mike Rapoport2a05c582018-03-21 21:22:45 +020037====
Minchan Kimd02be502015-04-15 16:15:46 -070038
39With CONFIG_ZSMALLOC_STAT, we could see zsmalloc internal information via
Mike Rapoport2a05c582018-03-21 21:22:45 +020040``/sys/kernel/debug/zsmalloc/<user name>``. Here is a sample of stat output::
Minchan Kimd02be502015-04-15 16:15:46 -070041
Mike Rapoport2a05c582018-03-21 21:22:45 +020042 # cat /sys/kernel/debug/zsmalloc/zram0/classes
Minchan Kimd02be502015-04-15 16:15:46 -070043
44 class size almost_full almost_empty obj_allocated obj_used pages_used pages_per_zspage
Mike Rapoport2a05c582018-03-21 21:22:45 +020045 ...
46 ...
Minchan Kimd02be502015-04-15 16:15:46 -070047 9 176 0 1 186 129 8 4
48 10 192 1 0 2880 2872 135 3
49 11 208 0 1 819 795 42 2
50 12 224 0 1 219 159 12 4
Mike Rapoport2a05c582018-03-21 21:22:45 +020051 ...
52 ...
Minchan Kimd02be502015-04-15 16:15:46 -070053
54
Mike Rapoport2a05c582018-03-21 21:22:45 +020055class
56 index
57size
58 object size zspage stores
59almost_empty
60 the number of ZS_ALMOST_EMPTY zspages(see below)
61almost_full
62 the number of ZS_ALMOST_FULL zspages(see below)
63obj_allocated
64 the number of objects allocated
65obj_used
66 the number of objects allocated to the user
67pages_used
68 the number of pages allocated for the class
69pages_per_zspage
70 the number of 0-order pages to make a zspage
Minchan Kimd02be502015-04-15 16:15:46 -070071
Mike Rapoport2a05c582018-03-21 21:22:45 +020072We assign a zspage to ZS_ALMOST_EMPTY fullness group when n <= N / f, where
73
74* n = number of allocated objects
75* N = total number of objects zspage can store
76* f = fullness_threshold_frac(ie, 4 at the moment)
Minchan Kimd02be502015-04-15 16:15:46 -070077
78Similarly, we assign zspage to:
Mike Rapoport2a05c582018-03-21 21:22:45 +020079
80* ZS_ALMOST_FULL when n > N / f
81* ZS_EMPTY when n == 0
82* ZS_FULL when n == N