MALLOC(9)

πŸ’– MALLOC(9) β€” kernel dev's manual, bestie edition πŸ’–

okay so bestie, gather round, because this is MALLOC(9) from the Kernel Developer's Manual and it's literally the page about how the kernel gets its memory. no cap, this one matters.

✨ NAME ✨

malloc, free, realloc, reallocf, MALLOC_DEFINE, MALLOC_DECLARE β€” kernel memory management routines 🧠✨

🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/malloc.h>

void *
malloc(size_t size, struct malloc_type *type, int flags);

void *
mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
    int flags);

void
free(void *addr, struct malloc_type *type);

void *
realloc(void *addr, size_t size, struct malloc_type *type, int flags);

void *
reallocf(void *addr, size_t size, struct malloc_type *type, int flags);

size_t
malloc_usable_size(const void *addr);

MALLOC_DECLARE(type);

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/kernel.h>

MALLOC_DEFINE(type, shortdesc, longdesc);

#include <sys/param.h>
#include <sys/domainset.h>

void *
malloc_domainset(size_t size, struct malloc_type *type,
    struct domainset *ds, int flags);

void
free_domain(void *addr, struct malloc_type *type);

πŸ’… DESCRIPTION πŸ’…

The malloc() function allocates uninitialized memory in kernel address space for an object whose size is specified by size. like, it hands you a chunk, it does not clean it, it does not decorate it. you get raw memory and you say thank you. 🫢

The malloc_domainset() variant allocates memory from a specific numa(4) domain using the specified domain selection policy. See domainset(9) for some example policies. Memory allocated with this function should be returned with free_domain(). it's giving "I know exactly which neighborhood I want my memory to live in". 🏘️

The mallocarray() function allocates uninitialized memory in kernel address space for an array of nmemb entries whose size is specified by size. array girlies stay winning. 🌈

The free() function releases memory at address addr that was previously allocated by malloc() for re-use. The memory is not zeroed. so like, don't go reading it expecting ✨nothing✨, because it is very much still ✨something✨. If addr is NULL, then free() does nothing. iconic, no notes, it just vibes and leaves. πŸ’…

The realloc() function changes the size of the previously allocated memory referenced by addr to size bytes. The contents of the memory are unchanged up to the lesser of the new and old sizes. Note that the returned value may differ from addr. If the requested memory cannot be allocated, NULL is returned and the memory referenced by addr is valid and unchanged. If addr is NULL, the realloc() function behaves identically to malloc() for the specified size. so yeah, it might move house and not tell you the old address, so always use the return value, bestie. 😭

The reallocf() function is identical to realloc() except that it will free the passed pointer when the requested memory cannot be allocated. the "f" is for "fine, I'll clean up after myself". 🧹

The malloc_usable_size() function returns the usable size of the allocation pointed to by addr. The return value may be larger than the size that was requested during allocation. so you asked for a small fry and got a medium. that's just how the allocator does. 🍟


Unlike its standard C library counterpart (malloc(3)), the kernel version takes two more arguments. The flags argument further qualifies malloc()'s operational characteristics as follows:

  • M_ZERO β€” Causes the allocated memory to be set to all zeros. squeaky clean, no mystery leftovers. ✨
  • M_NODUMP β€” For allocations greater than page size, causes the allocated memory to be excluded from kernel core dumps. keep it out of the screenshots, pls. πŸ™ˆ
  • M_NOWAIT β€” Causes malloc(), realloc(), and reallocf() to return NULL if the request cannot be immediately fulfilled due to resource shortage. Note that M_NOWAIT is required when running in an interrupt context. no waiting, no sleeping, we move fast here. ⚑
  • M_WAITOK β€” Indicates that it is OK to wait for resources. If the request cannot be immediately fulfilled, the current process is put to sleep to wait for resources to be released by other processes. The malloc(), mallocarray(), realloc(), and reallocf() functions cannot return NULL if M_WAITOK is specified. If the multiplication of nmemb and size would cause an integer overflow, the mallocarray() function induces a panic. πŸ’€ yes, a whole panic. it's not playing.
  • M_USE_RESERVE β€” Indicates that the system can use its reserve of memory to satisfy the request. This option should only be used in combination with M_NOWAIT when an allocation failure cannot be tolerated by the caller without catastrophic effects on the system. 🚨 emergency fund behavior, use responsibly.
  • M_EXEC β€” Indicates that the system should allocate executable memory. If this flag is not set, the system will not allocate executable memory. Not all platforms enforce a distinction between executable and non-executable memory. executable memory, aka the spicy kind. πŸ”₯
Exactly one of either M_WAITOK or M_NOWAIT must be specified. pick ONE. not both, not neither. this is not a choose-your-own-adventure. πŸ‘‘

The type argument is used to perform statistics on memory usage, and for basic sanity checks. It can be used to identify multiple allocations. The statistics can be examined by vmstat -m. so it's basically the receipt that says who allocated what. 🧾

A type is defined using struct malloc_type via the MALLOC_DECLARE() and MALLOC_DEFINE() macros.

/* sys/something/foo_extern.h */

MALLOC_DECLARE(M_FOOBUF);

/* sys/something/foo_main.c */

MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");

/* sys/something/foo_subr.c */

...
buf = malloc(sizeof(*buf), M_FOOBUF, M_NOWAIT);

In order to use MALLOC_DEFINE(), one must include <sys/param.h> (instead of <sys/types.h>) and <sys/kernel.h>. swap that include, bestie, don't skip it. πŸŽ€


🌈 CONTEXT 🌈

malloc(), realloc() and reallocf() may not be called from fast interrupts handlers. When called from threaded interrupts, flags must contain M_NOWAIT. πŸ›‘

malloc(), realloc() and reallocf() may sleep when called with M_WAITOK. free() never sleeps. However, malloc(), realloc(), reallocf() and free() may not be called in a critical section or while holding a spin lock. πŸ”’

Any calls to malloc() (even with M_NOWAIT) or free() when holding a vnode(9) interlock, will cause a LOR (Lock Order Reversal) due to the intertwining of VM Objects and Vnodes. 😭 yeah. this one bites people. heads up.

πŸ¦‹ IMPLEMENTATION NOTES πŸ¦‹

The memory allocator allocates memory in chunks that have size a power of two for requests up to the size of a page of memory. For larger requests, one or more pages is allocated. While it should not be relied upon, this information may be useful for optimizing the efficiency of memory use. iykyk β€” it's a peek behind the curtain, not a contract. πŸ‘€


⭐ RETURN VALUES ⭐

The malloc(), realloc(), and reallocf() functions return a kernel virtual address that is suitably aligned for storage of any type of object, or NULL if the request could not be satisfied (implying that M_NOWAIT was set). so if you get NULL back, check your flags, because that's the tell. πŸ•΅οΈ


πŸŽ€ DIAGNOSTICS πŸŽ€

A kernel compiled with the INVARIANTS configuration option attempts to detect memory corruption caused by such things as writing outside the allocated area and imbalanced calls to the malloc() and free() functions. Failing consistency checks will cause a panic or a system console message. the kernel is literally watching you and it will say something. πŸ’€πŸ“’


πŸ‘‘ SEE ALSO πŸ‘‘

numa(4), vmstat(8), contigmalloc(9), domainset(9), memguard(9), vnode(9)


and that's the whole page, bestie. go forth, allocate responsibly, and always pair your malloc() with a free() β€” balanced calls are the vibe, imbalanced calls are the panic. 🫢

this one came from the Debian crew, dated August 28, 2020, and it's the MALLOC(9) page from the Kernel Developer's Manual β€” mwah, slay, periodt. πŸ’–βœ¨

πŸ“¦ where this page comes from