KM_ALLOC(9)

πŸ“¦ in nixpkgs unstable

πŸ’– 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 the uvm_map the 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 a uvm_object if 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 to PMAP_PREFER to virtually align the allocated space.
  • kd_slowdown β€” A pointer to an integer that will be set to 1 if 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms