KM_ALLOC(9)
π NAME π
okay bestie, welcome to the km_alloc(9) page from the Kernel Developerβs Manual β itβs literally the kernel memory allocator vibe check.
km_alloc, km_free β kernel memory allocator π«Ά
π¦ SYNOPSIS π¦
#include <sys/types.h>
#include <uvm/uvm_extern.h>
void *
km_alloc(size_t sz, const struct kmem_va_mode *kv,
const struct kmem_pa_mode *kp, const struct kmem_dyn_mode *kd);
void
km_free(void *v, size_t sz, const struct kmem_va_mode *kv,
const struct kmem_pa_mode *kp);
π DESCRIPTION π
So km_alloc() allocates kernel virtual space thatβs optionally backed by physical pages. And km_free() frees the space that was previously allocated by km_alloc(). iconic little duo, no cap π«
The sz argument specifies the size of the allocation and must be a multiple of PAGE_SIZE. The kv and kp arguments specify the type of virtual and physical memory to be allocated. The kd argument specifies additional options for the allocation. Also important: the arguments passed to km_free() must match those that were used to obtain the space in km_alloc(). like, matching set, bestie π
Typically a user will use certain predefined modes for memory allocation. For virtual space the predefined modes are:
kv_anyβ Allocates the virtual space anywhere.kv_intrsafeβ Allocates the virtual space in the interrupt safe map.kv_pageβ Allocates single pages.
For physical pages the predefined modes are:
kp_dirtyβ Maps dirty pages into the allocation.kp_zeroβ Maps zeroed pages into the allocation.kp_dmaβ Maps dma-accessible pages into the allocation.kp_dma_zeroβ Maps zeroed dma-accessible pages into the allocation.kp_pageableβ Pages will be demand paged.kp_noneβ Leaves the allocation unmapped.
The other parameters for allocation are:
kd_waitokβ Sleeping for physical pages is allowed.kd_nowaitβ Sleeping is not allowed.kd_trylockβ Fail if the allocator cannot obtain locks without waiting.
β¨ custom allocation modes β¨
In case the predefined allocation modes are not sufficient, a custom allocation mode can be created. The structure controlling the virtual space allocation is:
struct kmem_va_mode {
struct vm_map **kv_map;
vsize_t kv_align;
int kv_wait;
int kv_singlepage;
};
kv_mapβ A pointer to the pointer to theuvm_mapthe space will be allocated from.kv_alignβ Alignment constraint of the allocated space.kv_waitβ A flag indicating whether the allocator should wait for space to be freed if the allocation cannot be satisfied.kv_singlepageβ A flag indicating if the allocations will always be for single pages.
struct kmem_pa_mode {
struct uvm_constraint_range *kp_constraint;
struct uvm_object **kp_object;
paddr_t kp_align;
paddr_t kp_boundary;
int kp_nomem;
int kp_maxseg;
int kp_zero;
int kp_pageable;
};
kp_constraintβ A pointer to physical allocation constraints.kp_objectβ A pointer to a pointer to auvm_objectif the pages should be backed by a kernel object.kp_alignβ Physical alignment of the first page in the allocation.kp_boundaryβ Boundary that the physical addresses canβt cross if the allocation is contiguous.kp_nomemβ A flag that specifies that the allocation should not be backed by physical pages.kp_maxsegβ Maximal amount of contiguous physical segments in the allocation.kp_zeroβ A flag that specifies if the returned memory should be zeroed.kp_pageableβ A flag that specifies if the returned memory should be demand paged from the backing object instead of being allocated up front.
struct kmem_dyn_mode {
int kd_waitok;
int kd_trylock;
voff_t kd_prefer;
int *kd_slowdown;
};
kd_waitokβ A flag that specifies if the allocator may sleep waiting for memory.kd_trylockβ A flag that specifies if the allocator should fail if it canβt immediately obtain a lock.kd_preferβ An offset given toPMAP_PREFERto virtually align the allocated space.kd_slowdownβ A pointer to an integer that will be set to1if the internal single page allocator needs the caller to back off to allow the allocator to catch up.
π RETURN VALUES π
km_alloc() returns a kernel virtual address or NULL if the allocation cannot be satisfied. thatβs the whole mood πβ¨
π SEE ALSO π
xoxo, with love from Debian β December 6, 2019 π β¨
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright (c) 2011 Artur Grabowski <art@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms