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 the osd framework 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 optional osd_destructor_t function pointer that will be called for objects of the type being registered which are later destroyed by the osd_del() function β€” and you can pass NULL if no destructor is required. the methods argument specifies an optional array of osd_method_t function pointers which can be later invoked by the osd_call() function; NULL is fine there too if no methods are required. and ngl, the methods argument is currently only useful with the OSD_JAIL type 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 by osd_register() when the data type was registered. πŸŽ€
  • osd_set() β€” associates a data object pointer with a kernel data structure's struct osd member. 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's struct osd which 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 with osd. ✨
  • osd_set_reserved() β€” does the same as osd_set(), but with an extra argument rsv that is internal-use memory previously allocated via osd_reserve(). πŸ”₯
  • osd_get() β€” returns the data pointer associated with a kernel data structure's struct osd member 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's struct osd to 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's struct osd member 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's struct osd to remove the data pointer from. the slot argument specifies the slot identifier to remove the data pointer from. and here's the moment: if an osd_destructor_t function 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 specified osd_method_t function 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 the osd_method_t array that was passed to osd_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's struct osd member. 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's struct osd to 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 for osd_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 via malloc(9) with M_WAITOK. then this pre-allocated memory is passed to osd_set_reserved(), which will use it if necessary or otherwise discard it. the memory may also be explicitly discarded by calling osd_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 that osd_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() and osd_set_reserved() return zero on success, or ENOMEM if the specified type/slot identifier pair triggered an internal realloc(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, or NULL if the slot has not been initialised yet. 🫢
  • osd_reserve() returns a pointer suitable for passing to osd_set_reserved() or osd_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 ⭐

khelp(9)


🌸 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