* [PATCH v2 0/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
@ 2026-09-02 8:10 Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 1/2] " Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 2/2] MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section Mike Rapoport (Microsoft)
0 siblings, 2 replies; 3+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-09-02 8:10 UTC (permalink / raw)
To: Jonathan Corbet, Andrew Morton, David Hildenbrand
Cc: Liam R. Howlett, Lorenzo Stoakes, Michal Hocko, Mike Rapoport,
Randy Dunlap, SJ Park, Shuah Khan, Suren Baghdasaryan,
Vlastimil Babka, linux-doc, linux-kernel, linux-mm
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/20260831-docs-memalloc-guide-v1-0-547718c274c1@kernel.org
---
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc
2026-09-02 8:10 [PATCH v2 0/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc Mike Rapoport (Microsoft)
@ 2026-09-02 8:10 ` Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 2/2] MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section Mike Rapoport (Microsoft)
1 sibling, 0 replies; 3+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-09-02 8:10 UTC (permalink / raw)
To: Jonathan Corbet, Andrew Morton, David Hildenbrand
Cc: Liam R. Howlett, Lorenzo Stoakes, Michal Hocko, Mike Rapoport,
Randy Dunlap, SJ Park, Shuah Khan, Suren Baghdasaryan,
Vlastimil Babka, linux-doc, linux-kernel, linux-mm
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.
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Acked-by: SJ Park <sj@kernel.org>
Acked-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
Documentation/core-api/memory-allocation.rst | 30 +++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)
diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst
index 0f19dd5243239..823f7fa57429b 100644
--- a/Documentation/core-api/memory-allocation.rst
+++ b/Documentation/core-api/memory-allocation.rst
@@ -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
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section
2026-09-02 8:10 [PATCH v2 0/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 1/2] " Mike Rapoport (Microsoft)
@ 2026-09-02 8:10 ` Mike Rapoport (Microsoft)
1 sibling, 0 replies; 3+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-09-02 8:10 UTC (permalink / raw)
To: Jonathan Corbet, Andrew Morton, David Hildenbrand
Cc: Liam R. Howlett, Lorenzo Stoakes, Michal Hocko, Mike Rapoport,
Randy Dunlap, SJ Park, Shuah Khan, Suren Baghdasaryan,
Vlastimil Babka, linux-doc, linux-kernel, linux-mm
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"
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: SJ Park <sj@kernel.org>
Acked-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
MAINTAINERS | 2 ++
1 file changed, 2 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 3a19da74d00c9..707f93320ad72 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-02 8:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-02 8:10 [PATCH v2 0/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 1/2] " Mike Rapoport (Microsoft)
2026-09-02 8:10 ` [PATCH v2 2/2] MAINTAINERS: add memory related docs in core-mm/ to MM - MISC section Mike Rapoport (Microsoft)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox