Re: [PATCH 1/2] docs/core-api: memory-allocation: add k[mz]alloc_obj() and clarify kmalloc

From: Vlastimil Babka (SUSE)

Date: Wed Sep 02 2026 - 03:57:11 EST


On 9/1/26 18:10, Mike Rapoport wrote:
> On Tue, Sep 01, 2026 at 12:17:02PM +0200, Vlastimil Babka (SUSE) wrote:
>> On 8/31/26 17:13, Mike Rapoport (Microsoft) wrote:
>> > Since v7.0 the most used memory allocation function is kzalloc_obj().
>> >
>> > Update hte 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.
>> >
>> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
>>
>> Acked-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
>
> Thanks!
>
>> Suggestion below:
>>
>> > ---
>> > Documentation/core-api/memory-allocation.rst | 30 +++++++++++++++++++++-------
>> > 1 file changed, 23 insertions(+), 7 deletions(-)
>> >
>> > -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 `MAX_ORDER`, the
>> > +same limit also applies to the page allocator.
>>
>> Would it be better to say the maximal size is KMALLOC_MAX_SIZE which is
>> derived from page allocator's MAX_PAGE_ORDER?
>
> My point was that whenever you allocate with kmalloc() the limit is
> MAX_PAGE_ORDER, not necessarily for actual slab allocations.

The actual slab allocations limit is KMALLOC_MAX_CACHE_SIZE and that's
indeed not the point and hence I wouldn't mention it here. KMALLOC_MAX_SIZE
is literally a conversion of MAX_PAGE_ORDER to bytes, and since kmalloc()
takes bytes and not order as an argument, it seems more natural to me, e.g.:

`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.

>> (Also `MAX_ORDER` doesn't exist anymore)
>
> Will fix.
>
>> > +Internally, 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
>> >
>>
>