CONTIGMALLOC(9)
π CONTIGMALLOC(9) β the kernel dev manual, but make it make sense π
okay so bestie, we're talking about contigmalloc and contigfree β the duo that manages contiguous kernel physical memory. yes it's a 9 page, yes it's kernel dev territory, and yes you're gonna slay it. π«Ά
β¨ SYNOPSIS β¨
first things first, the includes. you need these:
#include <sys/types.h>
#include <sys/malloc.h>
and here's your girl contigmalloc():
void *
contigmalloc(unsigned long size, struct malloc_type *type, int flags,
vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
vm_paddr_t boundary);
and her bestie contigfree():
void
contigfree(void *addr, unsigned long size, struct malloc_type *type);
now, if you're doing the numa thing (we'll get there), swap those includes out for:
#include <sys/param.h>
#include <sys/domainset.h>
and here's the fancy variant:
void *
contigmalloc_domainset(unsigned long size, struct malloc_type *type,
struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high,
unsigned long alignment, vm_paddr_t boundary);
πΈ DESCRIPTION πΈ
so contigmalloc() is the one that gets you size bytes of contiguous physical memory. and like, it's not just any memory β it's aligned to alignment bytes, and it does NOT cross a boundary of boundary bytes. the way that matters for hardware. π
if it works, the allocation is gonna live between physical addresses low and high. the pointer you get back points to a wired kernel virtual address range of size bytes, allocated from the kernel virtual address (KVA) map. wired means it's staying put, no cap. π₯
contigmalloc_domainset() is literally the same thing but you also get to pick a numa(4) domain selection policy. that's what the ds is for. if you want to see what kinds of policies exist, go peek at domainset(9) β it's got examples and everything. π
now the flags parameter, which changes how contigmalloc() behaves:
M_ZEROβ makes the allocated physical memory get zero filled. clean slate energy. β¨M_NOWAITβ makescontigmalloc()returnNULLif it can't fulfill the request immediately because of a resource shortage. like, no waiting, no patience, it's giving instant-answer-or-nothing. π
other flags? if there are any, they get ignored. not our problem. π
and contigfree()? she's the one who deallocates memory from a previous call to contigmalloc() or contigmalloc_domainset(). you allocated it, you free it, that's the cycle. periodt.
π οΈ IMPLEMENTATION NOTES π οΈ
okay here's the tea. contigmalloc() does not sleep waiting for memory resources to get freed up. instead it actively reclaims pages before it gives up. main character behavior honestly.
but β and this is a big but β unless you specified M_NOWAIT, it might select a page for reclamation that has to be written to backing storage first. and that? that can make it sleep. so like, it's not sleeping for memory, but it can still end up sleeping. iykyk. π
also, important: contigfree() does not accept NULL as an address input. unlike free(9). so don't come at her with NULL, she's not that kind of function. π«
β RETURN VALUES β
contigmalloc() hands you a kernel virtual address if the allocation succeeds. if it doesn't? you get NULL. that's it, that's the whole thing. π
π EXAMPLES π
okay let's actually see it in action:
void *p;
p = contigmalloc(8192, M_DEVBUF, M_ZERO, 0, (1L << 22),
32 * 1024, 1024 * 1024);
so what did we just ask for? 8192 bytes of zero-filled memory, sitting between physical address 0 and 4194303 inclusive, aligned to a 32K boundary, and not crossing a 1M address boundary. iconic request, honestly. π
π DIAGNOSTICS π
bestie. listen.contigmalloc()will panic ifsizeis zero. it will ALSO panic ifalignmentorboundaryis not a power of two. no negotiating, no warnings, just panic. so double-check those before you ship it. π
π SEE ALSO π
and that's the whole page, bestie!! π this was the Debian build of the kernel dev manual, dated October 30, 2018 β go forth and allocate contiguously, you absolute legend. π¦β¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2004 Joseph Koshy
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms