docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
Subsystems (3)
Mike Rapoport (Microsoft)
reviewerv2 cover docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
Update hte memory-allocation guide to describe k[mz]alloc_obj() and clarify description of kmalloc() size limitations.
And when I realized that get_maintainers.pl does not list any of mm people for that patch I added the MAINTAINERS update as well :)
---
v2 changes:
* update kmalloc limit phrasing as Vlastimil suggested
* make sure changelog matches the changes for MAINTAINERS update
* fix grammar and spelling
* add tags, thanks everyone!
v1: https://patch.msgid.link/[email protected]
---
Mike Rapoport (Microsoft) (2):
docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section
Documentation/core-api/memory-allocation.rst | 30 +++++++++++++++++++++------- MAINTAINERS | 2 ++ 2 files changed, 25 insertions(+), 7 deletions(-)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260831-docs-memalloc-guide-dc4db94dda96
--
Sincerely yours,
Mike.
Mike Rapoport (Microsoft)
reviewerPATCH v2 1/2 docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
Since v7.0 the most used memory allocation function is kzalloc_obj().
Update the memory-allocation guide to describe k[mz]alloc_obj() family and make kzalloc_obj() the first answer to "How should I allocate memory?" question.
While on it, clarify description of kmalloc() size limitations.
--- Documentation/core-api/memory-allocation.rst | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
Documentation/core-api/memory-allocation.rst +23 -7
@@ -19,6 +19,12 @@ Diversity of the allocation APIs combined with the numerous GFP flags makes the question "How should I allocate memory?" not that easy to answer, although very likely you should use +:: + + kzalloc_obj(<VAR_OR_TYPE>); + +or + :: kzalloc(<size>, GFP_KERNEL);
@@ -139,10 +145,13 @@ allocate memory for an array, there are kmalloc_array() and kcalloc() helpers. The helpers struct_size(), array_size() and array3_size() can be used to safely calculate object sizes without overflowing. -The maximal size of a chunk that can be allocated with `kmalloc` is -limited. The actual limit depends on the hardware and the kernel -configuration, but it is a good practice to use `kmalloc` for objects -smaller than page size. +Since 7.0 there are type aware kmalloc-family helpers that let you safely and +conveniently allocate a single object or arrays of objects with kzalloc_obj() +and kmalloc_obj() and their array versions kzalloc_objs() and +kmalloc_objs(). These helpers only need the type of the object that should be +allocated and the count of elements in the array for the array versions. + +As of v7.2, vast majority of the memory allocations use kzalloc_obj(). The address of a chunk allocated with `kmalloc` is aligned to at least ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the
@@ -154,9 +163,16 @@ Chunks allocated with kmalloc() can be resized with krealloc(). Similarly to kmalloc_array(): a helper for resizing arrays is provided in the form of krealloc_array(). -For large allocations you can use vmalloc() and vzalloc(), or directly -request pages from the page allocator. The memory allocated by `vmalloc` -and related functions is not physically contiguous. +`kmalloc` always allocates physically contiguous memory and the maximal size of +a chunk that can be allocated with `kmalloc` is limited by `KMALLOC_MAX_SIZE`, +which matches the page allocator's MAX_PAGE_ORDER limit. + +Internally, the slab allocator differentiates allocations of different orders +and delegates larger allocations to the page allocator, but for the users of +`kmalloc` family it is entirely transparent. + +For large allocations that do not require physically contiguous memory you can +use vmalloc() and vzalloc() family. If you are not sure whether the allocation size is too large for `kmalloc`, it is possible to use kvmalloc() and its derivatives. It will
Signature · 1 line
2.53.0
Mike Rapoport (Microsoft)
reviewerPATCH v2 2/2 MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section
In reply to Mike Rapoport (Microsoft)Previous efforts to make sure that mm files are properly listed in MAINTAINERS missed
Documentation/core-api/mm-api.rst Documentation/core-api/memory-allocation.rst
Add them to "MEMORY MANAGEMENT - MISC"
--- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
@@ -17275,6 +17275,8 @@ F: Documentation/ABI/testing/sysfs-kernel-mm-cma F: Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers F: Documentation/ABI/testing/sysfs-kernel-mm-numa F: Documentation/admin-guide/mm/ +F: Documentation/core-api/memory-allocation.rst +F: Documentation/core-api/mm-api.rst F: Documentation/mm/ F: drivers/char/mem.c F: include/linux/cma.h
Signature · 1 line
2.53.0