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