memory-allocation.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. =======================
  2. Memory Allocation Guide
  3. =======================
  4. Linux provides a variety of APIs for memory allocation. You can
  5. allocate small chunks using `kmalloc` or `kmem_cache_alloc` families,
  6. large virtually contiguous areas using `vmalloc` and its derivatives,
  7. or you can directly request pages from the page allocator with
  8. `alloc_pages`. It is also possible to use more specialized allocators,
  9. for instance `cma_alloc` or `zs_malloc`.
  10. Most of the memory allocation APIs use GFP flags to express how that
  11. memory should be allocated. The GFP acronym stands for "get free
  12. pages", the underlying memory allocation function.
  13. Diversity of the allocation APIs combined with the numerous GFP flags
  14. makes the question "How should I allocate memory?" not that easy to
  15. answer, although very likely you should use
  16. ::
  17. kzalloc(<size>, GFP_KERNEL);
  18. Of course there are cases when other allocation APIs and different GFP
  19. flags must be used.
  20. Get Free Page flags
  21. ===================
  22. The GFP flags control the allocators behavior. They tell what memory
  23. zones can be used, how hard the allocator should try to find free
  24. memory, whether the memory can be accessed by the userspace etc. The
  25. :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>` provides
  26. reference documentation for the GFP flags and their combinations and
  27. here we briefly outline their recommended usage:
  28. * Most of the time ``GFP_KERNEL`` is what you need. Memory for the
  29. kernel data structures, DMAable memory, inode cache, all these and
  30. many other allocations types can use ``GFP_KERNEL``. Note, that
  31. using ``GFP_KERNEL`` implies ``GFP_RECLAIM``, which means that
  32. direct reclaim may be triggered under memory pressure; the calling
  33. context must be allowed to sleep.
  34. * If the allocation is performed from an atomic context, e.g interrupt
  35. handler, use ``GFP_NOWAIT``. This flag prevents direct reclaim and
  36. IO or filesystem operations. Consequently, under memory pressure
  37. ``GFP_NOWAIT`` allocation is likely to fail. Allocations which
  38. have a reasonable fallback should be using ``GFP_NOWARN``.
  39. * If you think that accessing memory reserves is justified and the kernel
  40. will be stressed unless allocation succeeds, you may use ``GFP_ATOMIC``.
  41. * Untrusted allocations triggered from userspace should be a subject
  42. of kmem accounting and must have ``__GFP_ACCOUNT`` bit set. There
  43. is the handy ``GFP_KERNEL_ACCOUNT`` shortcut for ``GFP_KERNEL``
  44. allocations that should be accounted.
  45. * Userspace allocations should use either of the ``GFP_USER``,
  46. ``GFP_HIGHUSER`` or ``GFP_HIGHUSER_MOVABLE`` flags. The longer
  47. the flag name the less restrictive it is.
  48. ``GFP_HIGHUSER_MOVABLE`` does not require that allocated memory
  49. will be directly accessible by the kernel and implies that the
  50. data is movable.
  51. ``GFP_HIGHUSER`` means that the allocated memory is not movable,
  52. but it is not required to be directly accessible by the kernel. An
  53. example may be a hardware allocation that maps data directly into
  54. userspace but has no addressing limitations.
  55. ``GFP_USER`` means that the allocated memory is not movable and it
  56. must be directly accessible by the kernel.
  57. You may notice that quite a few allocations in the existing code
  58. specify ``GFP_NOIO`` or ``GFP_NOFS``. Historically, they were used to
  59. prevent recursion deadlocks caused by direct memory reclaim calling
  60. back into the FS or IO paths and blocking on already held
  61. resources. Since 4.12 the preferred way to address this issue is to
  62. use new scope APIs described in
  63. :ref:`Documentation/core-api/gfp_mask-from-fs-io.rst <gfp_mask_from_fs_io>`.
  64. Other legacy GFP flags are ``GFP_DMA`` and ``GFP_DMA32``. They are
  65. used to ensure that the allocated memory is accessible by hardware
  66. with limited addressing capabilities. So unless you are writing a
  67. driver for a device with such restrictions, avoid using these flags.
  68. And even with hardware with restrictions it is preferable to use
  69. `dma_alloc*` APIs.
  70. Selecting memory allocator
  71. ==========================
  72. The most straightforward way to allocate memory is to use a function
  73. from the :c:func:`kmalloc` family. And, to be on the safe size it's
  74. best to use routines that set memory to zero, like
  75. :c:func:`kzalloc`. If you need to allocate memory for an array, there
  76. are :c:func:`kmalloc_array` and :c:func:`kcalloc` helpers.
  77. The maximal size of a chunk that can be allocated with `kmalloc` is
  78. limited. The actual limit depends on the hardware and the kernel
  79. configuration, but it is a good practice to use `kmalloc` for objects
  80. smaller than page size.
  81. For large allocations you can use :c:func:`vmalloc` and
  82. :c:func:`vzalloc`, or directly request pages from the page
  83. allocator. The memory allocated by `vmalloc` and related functions is
  84. not physically contiguous.
  85. If you are not sure whether the allocation size is too large for
  86. `kmalloc`, it is possible to use :c:func:`kvmalloc` and its
  87. derivatives. It will try to allocate memory with `kmalloc` and if the
  88. allocation fails it will be retried with `vmalloc`. There are
  89. restrictions on which GFP flags can be used with `kvmalloc`; please
  90. see :c:func:`kvmalloc_node` reference documentation. Note that
  91. `kvmalloc` may return memory that is not physically contiguous.
  92. If you need to allocate many identical objects you can use the slab
  93. cache allocator. The cache should be set up with
  94. :c:func:`kmem_cache_create` before it can be used. Afterwards
  95. :c:func:`kmem_cache_alloc` and its convenience wrappers can allocate
  96. memory from that cache.
  97. When the allocated memory is no longer needed it must be freed. You
  98. can use :c:func:`kvfree` for the memory allocated with `kmalloc`,
  99. `vmalloc` and `kvmalloc`. The slab caches should be freed with
  100. :c:func:`kmem_cache_free`. And don't forget to destroy the cache with
  101. :c:func:`kmem_cache_destroy`.