VMEM(9)

✨ NAME ✨

vmem β€” the general purpose resource allocator πŸ’… and yes bestie, this is page VMEM(9), section 9, kernel developer's manual, we're in the big leagues now πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/vmem.h>

vmem_t *
vmem_create(const char *name, vmem_addr_t base, vmem_size_t size,
    vmem_size_t quantum, vmem_size_t qcache_max, int flags);

int
vmem_add(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int flags);

int
vmem_xalloc(vmem_t *vm, const vmem_size_t size, vmem_size_t align,
    const vmem_size_t phase, const vmem_size_t nocross,
    const vmem_addr_t minaddr, const vmem_addr_t maxaddr, int flags,
    vmem_addr_t *addrp);

void
vmem_xfree(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);

int
vmem_alloc(vmem_t *vm, vmem_size_t size, int flags, vmem_addr_t *addrp);

void
vmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);

void
vmem_destroy(vmem_t *vm);

🌸 DESCRIPTION 🌸

So vmem is a general purpose resource allocator. And the way its name is literally lying to you β€” despite being called virtual memory, it can be used for arbitrary resources other than virtual memory. It's giving "the name is a vibe, not a contract" 😌


πŸŽ€ vmem_create()

vmem_create() creates a new vmem arena. New arena, new me, main character energy fr fr ✨

  • name β€” the string to describe the vmem.
  • base β€” the start address of the initial span. Pass 0 if no initial span is required.
  • size β€” the size of the initial span. Pass 0 if no initial span is required.
  • quantum β€” the smallest unit of allocation.
  • qcache_max β€” the largest size of allocations which can be served by quantum cache. Ngl it is merely a hint and can be ignored πŸ™ˆ
  • flags β€” malloc(9) wait flag.

🧚 vmem_add()

vmem_add() adds a span of size size starting at addr to the arena. Returns 0 on success, ENOMEM on failure. And flags is a malloc(9) wait flag. Simple, iconic, no notes πŸ’œ


πŸ’« vmem_xalloc()

vmem_xalloc() allocates a resource from the arena. This is the fancy one, the one with opinions about where things go 😭

  • vm β€” the arena which we allocate from.
  • size β€” specify the size of the allocation.
  • align β€” if zero, don't care about the alignment of the allocation. Otherwise, request a resource segment starting at offset phase from an align aligned boundary.
  • phase β€” see the above description of align. If align is zero, phase should be zero. Otherwise, phase should be smaller than align.
  • nocross β€” request a resource which doesn't cross nocross aligned boundary.
  • minaddr β€” specify the minimum address which can be allocated, or VMEM_ADDR_MIN if the caller does not care.
  • maxaddr β€” specify the maximum address which can be allocated, or VMEM_ADDR_MAX if the caller does not care.
  • flags β€” a bitwise OR of an allocation strategy and a malloc(9) wait flag. The allocation strategy is one of:
    • M_FIRSTFIT β€” prefer allocation performance.
    • M_BESTFIT β€” prefer space efficiency.
    • M_NEXTFIT β€” perform an address-ordered search for free addresses, beginning where the previous search ended.
  • addrp β€” on success, if addrp is not NULL, vmem_xalloc() overwrites it with the start address of the allocated span.

🌈 vmem_xfree()

vmem_xfree() frees resource allocated by vmem_xalloc() to the arena.

  • vm β€” the arena which we free to.
  • addr β€” the resource being freed. It must be the one returned by vmem_xalloc(). Notably, it must not be the one from vmem_alloc(). Otherwise, the behaviour is undefined. Bestie I am begging you, keep your pairs matched πŸ’€
  • size β€” the size of the resource being freed. It must be the same as the size argument used for vmem_xalloc().

⭐ vmem_alloc()

vmem_alloc() allocates a resource from the arena.

  • vm β€” the arena which we allocate from.
  • size β€” specify the size of the allocation.
  • flags β€” a bitwise OR of an vmem allocation strategy flag (see above) and a malloc(9) sleep flag.
  • addrp β€” on success, if addrp is not NULL, vmem_alloc() overwrites it with the start address of the allocated span.

🫢 vmem_free()

vmem_free() frees resource allocated by vmem_alloc() to the arena.

  • vm β€” the arena which we free to.
  • addr β€” the resource being freed. It must be the one returned by vmem_alloc(). Notably, it must not be the one from vmem_xalloc(). Otherwise, the behaviour is undefined. Yes this is the mirror image of the other one, no you cannot mix and match, that's the whole bit 🚫
  • size β€” the size of the resource being freed. It must be the same as the size argument used for vmem_alloc().

πŸ‘‘ vmem_destroy()

vmem_destroy() destroys a vmem arena.

  • vm β€” the vmem arena being destroyed. The caller should ensure that no one will use it anymore.
Okay so this is the "we're so over" moment. Make sure literally nobody is still holding onto it before you do this. No cap.

πŸ’– RETURN VALUES πŸ’–

  • vmem_create() returns a pointer to the newly allocated vmem_t. Otherwise, it returns NULL.
  • On success, vmem_xalloc() and vmem_alloc() return 0. Otherwise, ENOMEM is returned.

πŸ”₯ CODE REFERENCES πŸ”₯

The vmem subsystem is implemented within the file sys/kern/subr_vmem.c. That's the source of truth, go read it if you're feeling brave 🫑


🌟 SEE ALSO 🌟

  • malloc(9)
  • Jeff Bonwick and Jonathan Adams, β€œMagazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources”, 2001 USENIX Annual Technical Conference, 2001.

πŸ¦‹ HISTORY πŸ¦‹

The vmem allocator was originally implemented in NetBSD. It was introduced in FreeBSD 10.0. A whole glow-up timeline, honestly ✨


πŸŽ€ AUTHORS πŸŽ€

Original implementation of vmem was written by YAMAMOTO Takashi. The FreeBSD port was made by Jeff Roberson. Kings πŸ‘‘


πŸ’€ BUGS πŸ’€

vmem relies on malloc(9), so it cannot be used as early during system bootstrap.

Fr fr. It's not a morning person. It needs malloc(9) awake first, so don't try to wake it up at 4am during bootstrap πŸ™ˆ

okay that's the whole page bestie, go allocate responsibly πŸ’…

This was the Debian rendering of VMEM(9), dated May 17, 2019 β€” ty for coming to my TED talk, mwah 🫢

πŸ“¦ where this page comes from