RMAN(9)

rman, rman_activate_resource, rman_adjust_resource, rman_deactivate_resource, rman_fini, rman_init, rman_init_from_resource, rman_is_region_manager, rman_manage_region, rman_first_free_region, rman_last_free_region, rman_release_resource, rman_reserve_resource, rman_reserve_resource_bound, rman_make_alignment_flags, rman_get_start, rman_get_end, rman_get_device, rman_get_size, rman_get_flags, rman_set_mapping, rman_get_mapping, rman_set_virtual, rman_get_virtual, rman_set_bustag, rman_get_bustag, rman_set_bushandle, rman_get_bushandle, rman_set_rid, rman_get_rid β€” resource management functions πŸ’…


okay bestie, gather round, this is the rman page from the Kernel Developer's Manual β€” section 9, kernel-land, no userland vibes here ✨

🌸 NAME 🌸

rman, rman_activate_resource, rman_adjust_resource, rman_deactivate_resource, rman_fini, rman_init, rman_init_from_resource, rman_is_region_manager, rman_manage_region, rman_first_free_region, rman_last_free_region, rman_release_resource, rman_reserve_resource, rman_reserve_resource_bound, rman_make_alignment_flags, rman_get_start, rman_get_end, rman_get_device, rman_get_size, rman_get_flags, rman_set_mapping, rman_get_mapping, rman_set_virtual, rman_get_virtual, rman_set_bustag, rman_get_bustag, rman_set_bushandle, rman_get_bushandle, rman_set_rid, rman_get_rid β€” resource management functions πŸŽ€

✨ SYNOPSIS ✨

like, this is the whole squad, here's how you call them 🫢

#include <sys/types.h>
#include <sys/rman.h>

int
rman_activate_resource(struct resource *r);

int
rman_adjust_resource(struct resource *r, rman_res_t start,
    rman_res_t end);

int
rman_deactivate_resource(struct resource *r);

int
rman_fini(struct rman *rm);

int
rman_init(struct rman *rm);

int
rman_init_from_resource(struct rman *rm, struct resource *r);

int
rman_is_region_manager(struct resource *r, struct rman *rm);

int
rman_manage_region(struct rman *rm, rman_res_t start, rman_res_t end);

int
rman_first_free_region(struct rman *rm, rman_res_t *start,
    rman_res_t *end);

int
rman_last_free_region(struct rman *rm, rman_res_t *start,
    rman_res_t *end);

int
rman_release_resource(struct resource *r);

struct resource *
rman_reserve_resource(struct rman *rm, rman_res_t start, rman_res_t end,
    rman_res_t count, u_int flags, struct device *dev);

struct resource *
rman_reserve_resource_bound(struct rman *rm, rman_res_t start,
    rman_res_t end, rman_res_t count, rman_res_t bound, u_int flags,
    struct device *dev);

uint32_t
rman_make_alignment_flags(uint32_t size);

rman_res_t
rman_get_start(struct resource *r);

rman_res_t
rman_get_end(struct resource *r);

struct device *
rman_get_device(struct resource *r);

rman_res_t
rman_get_size(struct resource *r);

u_int
rman_get_flags(struct resource *r);

void
rman_set_mapping(struct resource *r, struct resource_map *map);

void
rman_get_mapping(struct resource *r, struct resource_map *map);

void
rman_set_virtual(struct resource *r, void *v);

void *
rman_get_virtual(struct resource *r);

void
rman_set_bustag(struct resource *r, bus_space_tag_t t);

bus_space_tag_t
rman_get_bustag(struct resource *r);

void
rman_set_bushandle(struct resource *r, bus_space_handle_t h);

bus_space_handle_t
rman_get_bushandle(struct resource *r);

void
rman_set_rid(struct resource *r, int rid);

int
rman_get_rid(struct resource *r);

πŸ’– DESCRIPTION πŸ’–

so the rman fam of functions is literally a whole flexible resource management abstraction. the bus management code uses it constantly, it's giving main character energy in there. it implements two big concepts: region and resource. a region descriptor is what you use to manage a region β€” and a region could be memory, or some other form of bus space, whatever, it's flexible like that πŸ’…

every region has a set of bounds. and within those bounds, allocated segments can live. each segment, which we call a resource, has a bunch of properties, and those properties are stored in a 16-bit flag register, like so:

#define RF_ALLOCATED    0x0001 /* resource has been reserved */
#define RF_ACTIVE       0x0002 /* resource allocation has been activated */
#define RF_SHAREABLE    0x0004 /* resource permits contemporaneous sharing */
#define RF_FIRSTSHARE   0x0020 /* first in sharing list */
#define RF_PREFETCHABLE 0x0040 /* resource is prefetchable */
#define RF_UNMAPPED     0x0100 /* don't map resource when activating */

and bits 15:10 of that flag register? those are used to represent the desired alignment of the resource within the region. no cap πŸ‘‘


πŸ¦‹ the init and fini girlies πŸ¦‹

the rman_init() function initializes the region descriptor that rm points to, so it's ready to hang out with the resource management functions. important: you must set the fields rm_type and rm_descr of struct rman before you call rman_init(). like, required required.

  • the field rm_type shall be set to RMAN_ARRAY
  • the field rm_descr shall be set to a string that describes the resource to be managed
  • the rm_start and rm_end fields may be set to limit the range of acceptable resource addresses. if you don't set them, rman_init() will initialize them to allow the entire range of resource addresses 🌈

it also initializes any mutexes associated with the structure. if rman_init() fails to initialize the mutex, it returns ENOMEM; otherwise it returns 0 and rm is initialized. slay.

the rman_fini() function frees any structures associated with the structure that rm points to. if any of the resources within the managed region have the RF_ALLOCATED flag set, it returns EBUSY 😀 β€” otherwise, any mutexes associated with the structure get released and destroyed, and the function returns 0.

🌸 managing regions 🌸

the rman_manage_region() function establishes the whole concept of a region that's under rman control. the rman argument points to the region descriptor, and the start and end arguments specify the bounds of the region.

  • if it's successful, rman_manage_region() returns 0
  • if the region overlaps with an existing region, it returns EBUSY
  • if any part of the region falls outside the valid address range for rm, it returns EINVAL
  • ENOMEM gets returned when rman_manage_region() fails to allocate memory for the region

the rman_init_from_resource() function is a lil wrapper routine that creates a resource manager backed by an existing resource. it initializes rm using rman_init() and then adds a region to rm corresponding to the address range allocated to r via rman_manage_region(). cute, efficient, obsessed πŸ’–

the rman_first_free_region() and rman_last_free_region() functions let you query a resource manager for its first (or last) unallocated region. if rm contains no free region, these functions return ENOENT. otherwise, *start and *end get set to the bounds of the free region, and zero is returned ✨

πŸ”₯ reserving resources πŸ”₯

the rman_reserve_resource_bound() function is where the bulk of the rman logic lives. like, this is the whole personality right here. it attempts to reserve a contiguous range in the specified region rm for the use of the device dev.

the caller can specify the start and end of an acceptable range, plus a boundary restriction and the required alignment, and the code will try to find a free segment that fits.

  • the start argument is the lowest acceptable starting value of the resource
  • the end argument is the highest acceptable ending value of the resource
so start + count - 1 must be ≀ end for any allocation to happen. math said so, periodt πŸ“
  • the alignment requirement (if there is one) is specified in flags
  • the bound argument may be set to specify a boundary restriction such that an allocated region may cross an address that is a multiple of the boundary. the bound argument must be a power of two. it may be set to 0 to specify no boundary restriction
  • a shared segment will be allocated if the RF_SHAREABLE flag is set, otherwise an exclusive segment gets allocated. if this shared segment already exists, the caller has its device added to the list of consumers πŸ‘‘

the rman_reserve_resource() function is used to reserve resources within a previously established region. it's just a simplified interface to rman_reserve_resource_bound() that passes 0 for the bound argument. easy πŸ’…

the rman_make_alignment_flags() function returns the flag mask corresponding to the desired alignment size. use this when you're calling rman_reserve_resource_bound(), bestie.

the rman_is_region_manager() function returns true if the allocated resource r was allocated from rm. otherwise it returns false. simple as πŸ¦‹

πŸŽ€ adjusting, releasing, activating πŸŽ€

the rman_adjust_resource() function adjusts the reserved address range of an allocated resource to reserve start through end. it can be used to grow or shrink one or both ends of the resource range.

ngl there are some real limits here. the current implementation does not support entirely relocating the resource, and it will fail with EINVAL if the new resource range does not overlap the old resource range. if either end of the resource range grows and the new resource range would conflict with another allocated resource, the function fails with EBUSY. and rman_adjust_resource() does not support adjusting the resource range for shared resources β€” those attempts fail with EINVAL.

upon success, the resource r will have a start address of start and an end address of end, and the function returns zero.

⚠️ also important: none of the constraints of the original allocation request β€” like alignment or boundary restrictions β€” are checked by rman_adjust_resource(). it is the caller's responsibility to enforce any such requirements. you're the main character here, act like it 😭

the rman_release_resource() function releases the reserved resource r. it may attempt to merge adjacent free resources. tidy 🧚

the rman_activate_resource() function marks a resource as active by setting the RF_ACTIVE flag. if this is a time shared resource, and the caller has not yet acquired the resource, the function returns EBUSY.

the rman_deactivate_resource() function marks a resource r as inactive by clearing the RF_ACTIVE flag. if other consumers are waiting for this range, it will wakeup their threads. so polite 🌸

πŸ’œ getters and setters πŸ’œ

the rman_get_start(), rman_get_end(), rman_get_size(), and rman_get_flags() functions return the bounds, size and flags of the previously reserved resource r.

the rman_set_bustag() function associates a bus_space_tag_t t with the resource r. the rman_get_bustag() function is used to retrieve this tag once it's set.

the rman_set_bushandle() function associates a bus_space_handle_t h with the resource r. the rman_get_bushandle() function is used to retrieve this handle once it's set.

the rman_set_virtual() function is used to associate a kernel virtual address with a resource r. the rman_get_virtual() function can be used to retrieve the KVA once it's set.

the rman_set_mapping() function is used to associate a resource mapping with a resource r.

the mapping must cover the entire resource, no partial vibes allowed πŸ™ˆ

setting a mapping sets the associated bus_space(9) handle and tag for r as well as the kernel virtual address if the mapping contains one. these individual values can be retrieved via rman_get_bushandle(), rman_get_bustag(), and rman_get_virtual().

the rman_get_mapping() function can be used to retrieve the associated resource mapping once it's set.

the rman_set_rid() function associates a resource identifier with a resource r. the rman_get_rid() function retrieves this RID.

the rman_get_device() function returns a pointer to the device which reserved the resource r. iconic ⭐

🌈 SEE ALSO 🌈


✨ AUTHORS ✨

This manual page was written by Bruce M Simpson <bms@spc.org> πŸ’–

bestie wrote the whole thing fr fr, give them their flowers 🌸

okay that's literally the whole page πŸ’… this lil beauty comes to you from the Debian crew, dated May 20, 2016 β€” so if you're reading this, go forth and slay your kernel modules, mwah 🫢✨

πŸ“¦ where this page comes from