OSD(9)
π OSD(9) β Object Specific Data, the kernel dev manual π
okay bestie, buckle up, because this is the osd page β and it's giving "I want to glue arbitrary data onto your kernel structs without breaking the ABI" and honestly? iconic. β¨
πΈ NAME πΈ
osd, osd_register, osd_deregister, osd_set, osd_reserve, osd_set_reserved, osd_free_reserved, osd_get, osd_del, osd_call, osd_exit β Object Specific Data. no cap, that's literally the whole family. π
π¦ SYNOPSIS π¦
#include <sys/osd.h>
typedef void
(*osd_destructor_t)(void *value);
typedef int
(*osd_method_t)(void *obj, void *data);
int
osd_register(u_int type, osd_destructor_t destructor,
osd_method_t *methods);
void
osd_deregister(u_int type, u_int slot);
int
osd_set(u_int type, struct osd *osd, u_int slot, void *value);
void **
osd_reserve(u_int slot);
int
osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
void *value);
void
osd_free_reserved(void **rsv);
void *
osd_get(u_int type, struct osd *osd, u_int slot);
void
osd_del(u_int type, struct osd *osd, u_int slot);
int
osd_call(u_int type, u_int method, void *obj, void *data);
void
osd_exit(u_int type, struct osd *osd);
likeβ¦ look at those signatures. so clean. so structured. we love a consistent API. π
π DESCRIPTION π
so the osd framework is basically the β¨glow-upβ¨ mechanism of the kernel: it lets you dynamically associate arbitrary data at run-time with any kernel data structure that's been suitably modified for use with osd. and the one-off modification required? you just embed a struct osd inside the kernel data structure. that's it. that's the whole change. periodt. π
and the bonus benefit?? lowkey the best part: once you've made that initial change to a structure, all subsequent use of osd with that structure involves zero changes to the structure's layout. no cap. by extension, if the data structure is part of the ABI, osd gives you a way of extending the structure in an ABI-preserving manner. obsessed. π
now β the details of the embedded struct osd? not relevant to consumers of the osd framework, and you should not be manipulating them directly. hands off, bestie. π
data associated with a structure is referenced by the osd framework using a type/slot identifier pair. types are statically defined in <sys/osd.h> and provide a high-level grouping for slots to be registered under. slot identifiers are dynamically assigned by the framework when a data type is registered using osd_register(), and they remain valid until a corresponding call to osd_deregister(). π«Ά
π§ Functions π§
osd_register()β registers a type/slot identifier pair with theosdframework for use with a new data type. β οΈ heads up: this function may sleep, so it absolutely cannot be called from a non-sleepable context. the type argument specifies which high-level type grouping from<sys/osd.h>the slot identifier should be allocated under. the destructor argument specifies an optionalosd_destructor_tfunction pointer that will be called for objects of the type being registered which are later destroyed by theosd_del()function β and you can passNULLif no destructor is required. the methods argument specifies an optional array ofosd_method_tfunction pointers which can be later invoked by theosd_call()function;NULLis fine there too if no methods are required. and ngl, the methods argument is currently only useful with theOSD_JAILtype identifier. iykyk. πosd_deregister()β deregisters a previously registered type/slot identifier pair. β οΈ also may sleep, so same deal: no non-sleepable contexts. the type argument says which high-level type grouping from<sys/osd.h>the slot identifier is allocated under. the slot argument specifies the slot identifier being deregistered, and it should be the value that was returned byosd_register()when the data type was registered. πosd_set()β associates a data object pointer with a kernel data structure'sstruct osdmember. the type argument says which high-level type grouping from<sys/osd.h>the slot identifier is allocated under. the osd argument is a pointer to the kernel data structure'sstruct osdwhich will have the value pointer associated with it. the slot argument specifies the slot identifier to assign the value pointer to. and value points to a data object to associate withosd. β¨osd_set_reserved()β does the same asosd_set(), but with an extra argument rsv that is internal-use memory previously allocated viaosd_reserve(). π₯osd_get()β returns the data pointer associated with a kernel data structure'sstruct osdmember from the specified type/slot identifier pair. the type argument says which high-level type grouping from<sys/osd.h>the slot identifier is allocated under. the osd argument is a pointer to the kernel data structure'sstruct osdto retrieve the data pointer from. the slot argument specifies the slot identifier to retrieve the data pointer from. π«Άosd_del()β removes the data pointer associated with a kernel data structure'sstruct osdmember from the specified type/slot identifier pair. the type argument says which high-level type grouping from<sys/osd.h>the slot identifier is allocated under. the osd argument is a pointer to the kernel data structure'sstruct osdto remove the data pointer from. the slot argument specifies the slot identifier to remove the data pointer from. and here's the moment: if anosd_destructor_tfunction pointer was specified at registration time, the destructor function will be called and passed the data pointer for the type/slot identifier pair which is being deleted. bye bestie, we're cleaning up after you πosd_call()β calls the specifiedosd_method_tfunction pointer for all currently registered slots of a given type on the specified obj and data pointers. β οΈ yep, this one may sleep too, so no non-sleepable contexts. the type argument says which high-level type grouping from<sys/osd.h>to call the method for. the method argument specifies the index into theosd_method_tarray that was passed toosd_register(). and the obj and data arguments get passed to the method function pointer of each slot. group chat blast, but make it a function call. π£osd_exit()β removes all data object pointers from all currently registered slots for a given type for the specified kernel data structure'sstruct osdmember. the type argument says which high-level type grouping from<sys/osd.h>to remove data pointers from. the osd argument is a pointer to the kernel data structure'sstruct osdto remove all data object pointers for all currently registered slots from. full send. clean slate. π
π IMPLEMENTATION NOTES π
osd uses a two dimensional matrix (array of arrays) as the data structure to manage the external data associated with a kernel data structure's struct osd member. the type identifier is used as the index into the outer array, and the slot identifier is used as the index into the inner array. so to set or retrieve a data pointer for a given type/slot identifier pair, osd_set() and osd_get() perform the equivalent of array[type][slot] β which is both constant time and fast. it's giving efficiency. it's giving elegance. β¨
if osd_set() is called on a struct osd for the first time, the array for storing data pointers is dynamically allocated using malloc(9) with M_NOWAIT to a size appropriate for the slot identifier being set. if a subsequent call to osd_set() attempts to set a slot identifier which is numerically larger than the slot used in the previous osd_set() call, realloc(9) is used to grow the array to the appropriate size such that the slot identifier can be used. π
and here's the pro tip, bestie: to maximise the efficiency of any code which calls osd_set() sequentially on a number of different slot identifiers (e.g., during an initialisation phase) you should loop through the slot identifiers in descending order, from highest to lowest. that way you get only a single malloc(9) call to create an array of the largest slot size, and all subsequent calls to osd_set() will proceed without any realloc(9) calls. chef's kiss. π€
β οΈ it is possible forosd_set()to fail to allocate this array. to ensure that such allocation succeeds,osd_reserve()may be called (in a non-blocking context), and it will pre-allocate the memory viamalloc(9)withM_WAITOK. then this pre-allocated memory is passed toosd_set_reserved(), which will use it if necessary or otherwise discard it. the memory may also be explicitly discarded by callingosd_free_reserved(). and because this method always allocates memory whether or not it is ultimately needed, it should be used only rarely β such as in the unlikely event thatosd_set()fails.
the osd API is geared towards slot identifiers storing pointers to the same underlying data structure type for a given osd type identifier. that said, this is not a requirement β and khelp(9), for example, stores completely different data types in slots under the OSD_KHELP type identifier. living its truth. π
π Locking π
osd internally uses a mix of mutex(9), rmlock(9) and sx(9) locks to protect its internal data structures and state. the whole squad, basically. π«Ά
responsibility for synchronising access to a kernel data structure's struct osd member is left to the subsystem that uses the data structure and calls the osd API. that's on you, bestie. π
osd_get() only acquires an rmlock in read mode, therefore making it safe to use in the majority of contexts within the kernel, including most fast paths. slay. β‘
π RETURN VALUES π
osd_register()returns the slot identifier for the newly registered data type. πosd_set()andosd_set_reserved()return zero on success, orENOMEMif the specified type/slot identifier pair triggered an internalrealloc(9)which failed (osd_set_reserved()will always succeed when rsv is non-NULL). π€osd_get()returns the data pointer for the specified type/slot identifier pair, orNULLif the slot has not been initialised yet. π«Άosd_reserve()returns a pointer suitable for passing toosd_set_reserved()orosd_free_reserved(). β¨osd_call()returns zero if no method is run or the method for each slot runs successfully. if a method for a slot returns non-zero,osd_call()terminates prematurely and returns the method's error to the caller. π
β SEE ALSO β
πΈ HISTORY πΈ
The Object Specific Data (OSD) facility first appeared in FreeBSD 8.0. a whole era. π
π¦ AUTHORS π¦
The osd facility was written by Pawel Jakub Dawidek <pjd@FreeBSD.org>.
This manual page was written by Lawrence Stewart <lstewart@FreeBSD.org>.
and that's the whole page, bestie!! π this one came from the Debian folks, dated April 26, 2016, and it lives in section OSD(9) of the Kernel Developer's Manual. go forth and associate your arbitrary data responsibly. mwah. π β¨
π¦ 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) 2010 Lawrence Stewart <lstewart@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms