126 lines · 5.9 KB · Text
RawDownload
.. _memory_allocation: =======================Memory Allocation Guide======================= Linux provides a variety of APIs for memory allocation. You canallocate small chunks using `kmalloc` or `kmem_cache_alloc` families,large virtually contiguous areas using `vmalloc` and its derivatives,or you can directly request pages from the page allocator with`alloc_pages`. It is also possible to use more specialized allocators,for instance `cma_alloc` or `zs_malloc`. Most of the memory allocation APIs use GFP flags to express how thatmemory should be allocated. The GFP acronym stands for "get freepages", the underlying memory allocation function. Diversity of the allocation APIs combined with the numerous GFP flagsmakes the question "How should I allocate memory?" not that easy toanswer, although very likely you should use ::   kzalloc(<size>, GFP_KERNEL); Of course there are cases when other allocation APIs and different GFPflags must be used. Get Free Page flags=================== The GFP flags control the allocators behavior. They tell what memoryzones can be used, how hard the allocator should try to find freememory, whether the memory can be accessed by the userspace etc. The:ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>` providesreference documentation for the GFP flags and their combinations andhere we briefly outline their recommended usage:   * Most of the time ``GFP_KERNEL`` is what you need. Memory for the    kernel data structures, DMAable memory, inode cache, all these and    many other allocations types can use ``GFP_KERNEL``. Note, that    using ``GFP_KERNEL`` implies ``GFP_RECLAIM``, which means that    direct reclaim may be triggered under memory pressure; the calling    context must be allowed to sleep.  * If the allocation is performed from an atomic context, e.g interrupt    handler, use ``GFP_NOWAIT``. This flag prevents direct reclaim and    IO or filesystem operations. Consequently, under memory pressure    ``GFP_NOWAIT`` allocation is likely to fail. Allocations which    have a reasonable fallback should be using ``GFP_NOWARN``.  * If you think that accessing memory reserves is justified and the kernel    will be stressed unless allocation succeeds, you may use ``GFP_ATOMIC``.  * Untrusted allocations triggered from userspace should be a subject    of kmem accounting and must have ``__GFP_ACCOUNT`` bit set. There    is the handy ``GFP_KERNEL_ACCOUNT`` shortcut for ``GFP_KERNEL``    allocations that should be accounted.  * Userspace allocations should use either of the ``GFP_USER``,    ``GFP_HIGHUSER`` or ``GFP_HIGHUSER_MOVABLE`` flags. The longer    the flag name the less restrictive it is.     ``GFP_HIGHUSER_MOVABLE`` does not require that allocated memory    will be directly accessible by the kernel and implies that the    data is movable.     ``GFP_HIGHUSER`` means that the allocated memory is not movable,    but it is not required to be directly accessible by the kernel. An    example may be a hardware allocation that maps data directly into    userspace but has no addressing limitations.     ``GFP_USER`` means that the allocated memory is not movable and it    must be directly accessible by the kernel. You may notice that quite a few allocations in the existing codespecify ``GFP_NOIO`` or ``GFP_NOFS``. Historically, they were used toprevent recursion deadlocks caused by direct memory reclaim callingback into the FS or IO paths and blocking on already heldresources. Since 4.12 the preferred way to address this issue is touse new scope APIs described in:ref:`Documentation/core-api/gfp_mask-from-fs-io.rst <gfp_mask_from_fs_io>`. Other legacy GFP flags are ``GFP_DMA`` and ``GFP_DMA32``. They areused to ensure that the allocated memory is accessible by hardwarewith limited addressing capabilities. So unless you are writing adriver for a device with such restrictions, avoid using these flags.And even with hardware with restrictions it is preferable to use`dma_alloc*` APIs. Selecting memory allocator========================== The most straightforward way to allocate memory is to use a functionfrom the :c:func:`kmalloc` family. And, to be on the safe size it'sbest to use routines that set memory to zero, like:c:func:`kzalloc`. If you need to allocate memory for an array, thereare :c:func:`kmalloc_array` and :c:func:`kcalloc` helpers. The maximal size of a chunk that can be allocated with `kmalloc` islimited. The actual limit depends on the hardware and the kernelconfiguration, but it is a good practice to use `kmalloc` for objectssmaller than page size. For large allocations you can use :c:func:`vmalloc` and:c:func:`vzalloc`, or directly request pages from the pageallocator. The memory allocated by `vmalloc` and related functions isnot physically contiguous. If you are not sure whether the allocation size is too large for`kmalloc`, it is possible to use :c:func:`kvmalloc` and itsderivatives. It will try to allocate memory with `kmalloc` and if theallocation fails it will be retried with `vmalloc`. There arerestrictions on which GFP flags can be used with `kvmalloc`; pleasesee :c:func:`kvmalloc_node` reference documentation. Note that`kvmalloc` may return memory that is not physically contiguous. If you need to allocate many identical objects you can use the slabcache allocator. The cache should be set up with:c:func:`kmem_cache_create` or :c:func:`kmem_cache_create_usercopy`before it can be used. The second function should be used if a part ofthe cache might be copied to the userspace.  After the cache iscreated :c:func:`kmem_cache_alloc` and its convenience wrappers canallocate memory from that cache. When the allocated memory is no longer needed it must be freed. Youcan use :c:func:`kvfree` for the memory allocated with `kmalloc`,`vmalloc` and `kvmalloc`. The slab caches should be freed with:c:func:`kmem_cache_free`. And don't forget to destroy the cache with:c:func:`kmem_cache_destroy`.