KMEM(9)

πŸ“¦ version 11.0, in nixpkgs unstable

✨ KMEM(9) β€” kernel wired memory allocator ✨

okay bestie, so this one is from the Kernel Developer's Manual and it's literally the page for kmem β€” the kernel wired memory allocator. if you're writing kernel code and you need memory, this is the group chat you want to be in. let's get into it πŸ’…


🌸 SYNOPSIS 🌸

#include <sys/kmem.h>

void *
kmem_alloc(size_t size, km_flag_t kmflags);

void *
kmem_zalloc(size_t size, km_flag_t kmflags);

void
kmem_free(void *p, size_t size);

void *
kmem_intr_alloc(size_t size, km_flag_t kmflags);

void *
kmem_intr_zalloc(size_t size, km_flag_t kmflags);

void
kmem_intr_free(void *p, size_t size);

char *
kmem_asprintf(const char *fmt, ...);

char *
kmem_strdupsize(const char *str, size_t *size, km_flag_t kmflags);

char *
kmem_strdup(const char *str, km_flag_t kmflags);

char *
kmem_strndup(const char *str, size_t manxlen, km_flag_t kmflags);

void
kmem_strfree(char *str);

void *
kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
    km_flag_t kmflags);

void
kmem_tmpbuf_free(void *p, size_t size, void *stackbuf);

options KMEM_SIZE

(yes bestie, that really is a whole lot of functions. we're gonna go through all of them, don't worry 🫢)


πŸ¦‹ DESCRIPTION πŸ¦‹

the basics, no cap

kmem_alloc() allocates kernel wired memory. it takes these arguments:

  • size β€” the size of the allocation, in bytes ✨
  • kmflags β€” one of these two, and you gotta pick:
    • KM_SLEEP β€” if the allocation can't be satisfied immediately, it sleeps until enough memory is available. and if you specify KM_SLEEP, then like... the allocation cannot fail. periodt.
    • KM_NOSLEEP β€” do not sleep. immediately returns NULL if there isn't enough memory available. it should only be used when a failure to allocate won't have harmful, user-visible effects.
okay so real talk: use of KM_NOSLEEP is strongly discouraged because it can create transient, hard to debug failures that occur when the system is under memory pressure 😭

in situations where it's not possible to sleep β€” for example because locks are held by the caller β€” the code path should be restructured so the allocation can be made in another place. lowkey annoying but that's the vibe check.

the contents of allocated memory are uninitialized. so like, whatever was there before? still there. πŸ™ˆ

and unlike Solaris, kmem_alloc(0, flags) is illegal. not "returns a tiny thing", not "works fine" β€” illegal. don't do it.

kmem_zalloc()

kmem_zalloc() is the equivalent of kmem_alloc(), except that it initializes the memory to zero. iconic behaviour honestly, zeroing things out is the self-care of kernel code ✨

kmem_asprintf()

kmem_asprintf() functions as the well known asprintf() function, but allocates memory using kmem_alloc(). this routine can sleep during allocation. the size of the allocated area is the length of the returned character string, plus one (for the NUL terminator). and you gotta take that into consideration when freeing the returned area with kmem_free() β€” that +1 is not a suggestion, bestie πŸ’…

kmem_free()

kmem_free() frees kernel wired memory allocated by kmem_alloc() or kmem_zalloc() so it can be used for other purposes. it takes:

  • p β€” the pointer to the memory being freed. it must be the one returned by kmem_alloc() or kmem_zalloc().
  • size β€” the size of the memory being freed, in bytes. it must be the same as the size argument used for kmem_alloc() or kmem_zalloc() when the memory was allocated.
freeing NULL is illegal. like, do not. 🫠

the interrupt-context trio πŸ’€

kmem_intr_alloc(), kmem_intr_zalloc() and kmem_intr_free() are the equivalents of the above kmem routines which can be called from the interrupt context. these routines are for the special cases. normally, pool_cache(9) should be used for memory allocation from interrupt context. iykyk 🧚

the string helpers πŸŽ€

  • kmem_strdupsize() β€” a utility function that can be used to copy the string in the str argument to a new buffer allocated using kmem_alloc(), and optionally return the size of the allocation (the length of the string plus the trailing NUL) in the size argument, if that is not NULL.
  • kmem_strdup() β€” a simplified version of kmem_strdupsize() that does not return the size of the allocation.
  • kmem_strndup() β€” a variation of kmem_strdup() that copies at most maxlen characters from the string str, always NUL terminating the copied string.
  • kmem_strfree() β€” can be used to free a NUL terminated string by computing the length of the string using strlen(3) and adding one for the NUL, and then using kmem_free().

the tmpbuf duo πŸ“

kmem_tmpbuf_alloc() is a utility function for allocating memory for temporary use, where allocation on the stack is desirable, but only up to a certain size. if the requested size fits within the specified stack buffer, the stack buffer is returned. otherwise, memory is allocated with kmem_alloc().

kmem_tmpbuf_free() compares the result of a previous call to kmem_tmpbuf_alloc() and frees the memory using kmem_free() if it is not the specified stack buffer. cute, right? it remembers where it came from 🫢


πŸ’œ NOTES πŸ’œ

okay so this part is important, bestie. making KM_SLEEP allocations while holding mutexes or reader/writer locks is discouraged, because the caller can sleep for an unbounded amount of time in order to satisfy the allocation. that can in turn block other threads that wish to acquire locks held by the caller. and yeah β€” you should also note that kmem_free() may block too. πŸ’€

for some locks this is permissible or even unavoidable. for others β€” particularly locks that may be taken from soft interrupt context β€” it is a serious problem. as a general rule it's better not to allow this type of situation to develop. one way to circumvent the problem is to make allocations speculative and part of a retryable sequence. for example:

  retry:
    /* speculative unlocked check */
    if (need to allocate) {
        new_item = kmem_alloc(sizeof(*new_item), KM_SLEEP);
    } else {
        new_item = NULL;
    }
    mutex_enter(lock);
    /* check while holding lock for true status */
    if (need to allocate) {
        if (new_item == NULL) {
            mutex_exit(lock);
            goto retry;
        }
        consume(new_item);
        new_item = NULL;
    }
    mutex_exit(lock);
    if (new_item != NULL) {
        /* did not use it after all */
        kmem_free(new_item, sizeof(*new_item));
    }

🌈 OPTIONS 🌈

KMEM_SIZE

kernels compiled with the KMEM_SIZE option ensure the size given in kmem_free() matches the actual allocated size. so here's the tea: on kmem_alloc(), the kernel will allocate an additional contiguous kmem page of eight bytes in the buffer, will register the allocated size in the first kmem page of that buffer, and will return a pointer to the second kmem page in that same buffer. when freeing, the kernel reads the first page and compares the size registered with the one given in kmem_free(). any mismatch triggers a panic. 😭 not a warning, not a log line β€” a panic.

KMEM_SIZE is enabled by default on DIAGNOSTIC. ⭐


πŸ‘‘ RETURN VALUES πŸ‘‘

on success, kmem_alloc(), kmem_asprintf(), kmem_intr_alloc(), kmem_intr_zalloc(), kmem_strdupsize(), and kmem_zalloc() return a pointer to allocated memory. otherwise, NULL is returned. that's it, that's the whole thing πŸŽ€


πŸ”₯ CODE REFERENCES πŸ”₯

the kmem subsystem is implemented within the file sys/kern/subr_kmem.c. go read it if you're feeling brave πŸ’…


🌸 SEE ALSO 🌸

intro(9), memoryallocators(9), percpu(9), pool_cache(9), uvm_km(9)


⭐ CAVEATS ⭐

the kmem_alloc(), kmem_asprintf(), kmem_free(), kmem_strdupsize(), kmem_strfree(), and kmem_zalloc() functions cannot be used from interrupt context, from a soft interrupt, or from a callout. use pool_cache(9) in these situations. no exceptions, bestie πŸ™ˆ


πŸ¦‹ SECURITY CONSIDERATIONS πŸ¦‹

as the memory allocated by kmem_alloc() is uninitialized, it can contain security-sensitive data left by its previous user. it is the caller's responsibility not to expose it to the world. so like... don't leak the gossip, okay? that's your job, not the allocator's πŸ”₯


and that's the whole page, bestie! πŸ’– this one shipped as part of Debian and it's dated January 24, 2021, so now you know exactly where it came from. mwah, go allocate responsibly πŸŽ€βœ¨

πŸ“¦ where this page comes from