zsmalloc.txt 3.0 KB

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