VM_MAP_FIND(9)

๐Ÿ’– VM_MAP_FIND ๐Ÿ’–

okay bestie, gather round, because we're diving into the Kernel Developer's Manual and today's main character is vm_map_find(9) โ€” she finds free regions in a map and (if she's feeling generous) maps a vm_object for you too. no cap. ๐Ÿฆ‹


โœจ NAME โœจ

vm_map_find โ€” find a free region within a map, and optionally map a vm_object


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/param.h>
#include <vm/vm.h>
#include <vm/vm_map.h>

int
vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
    vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
    int find_space, vm_prot_t prot, vm_prot_t max, int cow);

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

So basically, vm_map_find() rolls up to the target map and tries to find a free region in it that's the given length. And if she finds one? She literally creates a mapping of object by calling vm_map_insert(9). it's giving matchmaker energy fr fr ๐Ÿ’…

The arguments offset, prot, max, and cow get passed unchanged to vm_map_insert(9) when creating the mapping โ€” but only if and only if a free region was found. no free region, no mapping, no passing. periodt.

If object is non-NULL, the reference count on that object must be incremented by the caller before calling this function, to account for the new entry. do not skip this step bestie, I'm begging ๐Ÿ˜ญ

If max_addr is non-zero, it specifies an upper bound on the mapping. The mapping will only succeed if a free region can be found that resides entirely below max_addr. it's giving ceiling, not a suggestion.

The find_space argument is the vibe check โ€” it tells the function which strategy to use when searching for a free region of the requested length. For all values other than VMFS_NO_SPACE, vm_map_findspace(9) gets called to locate a free region of the requested length with a starting address at or above *addr. here are the strategies, and yes, there's lore:

  • VMFS_NO_SPACE โ€” The mapping will only succeed if there is a free region of the requested length at the given address *addr. she's picky, she knows what she wants ๐Ÿ’…
  • VMFS_ANY_SPACE โ€” The mapping will succeed as long as there is a free region. lowkey the chill one, she's not fussy ๐ŸŒˆ
  • VMFS_SUPER_SPACE โ€” The mapping will succeed as long as there is a free region that begins on a superpage boundary. But if object is non-NULL and is already backed by superpages, then the mapping will require a free region that aligns relative to the existing superpages rather than one beginning on a superpage boundary. she's flexible but only in the specific way she's flexible ๐Ÿ˜Œ
  • VMFS_OPTIMAL_SPACE โ€” The mapping will succeed as long as there is a free region. However, if object is non-NULL and is already backed by superpages, this strategy will attempt to find a free region aligned relative to the existing superpages. she's trying her best and honestly it's working โœจ
  • VMFS_ALIGNED_SPACE(n) โ€” The mapping will succeed as long as there is a free region that aligns on a 2^n boundary. math girlie behavior, we love to see it ๐Ÿงฎ

๐Ÿ‘‘ IMPLEMENTATION NOTES ๐Ÿ‘‘

This function acquires a lock on map by calling vm_map_lock(9), and holds it until the function returns. she does not let go early, do not test her ๐Ÿ”’

The search for a free region is defined to be first-fit, from the address addr onwards. first come, first served, no cutting the line.


๐Ÿ”ฅ RETURN VALUES ๐Ÿ”ฅ

vm_map_find() returns KERN_SUCCESS if the mapping was successfully created. slay ๐Ÿ’–

If space could not be found, or if find_space was VMFS_NO_SPACE and the given address addr was already mapped, KERN_NO_SPACE will be returned.

If the discovered range turned out to be bogus, KERN_INVALID_ADDRESS will be returned. not her fault, the range was lying to her ๐Ÿ’€


๐ŸŒŸ SEE ALSO ๐ŸŒŸ

vm_map(9), vm_map_findspace(9), vm_map_insert(9), vm_map_lock(9)

the whole friend group, honestly. go read all of them, iykyk ๐Ÿซถ


๐Ÿ’œ AUTHORS ๐Ÿ’œ

This manual page was written by Bruce M Simpson <bms@spc.org>. king behavior, we stan ๐Ÿ‘‘


and that's the whole page bestie! written for Debian, dated September 12, 2013, living under the VM_MAP_FIND(9) header. go forth and map responsibly ๐Ÿ’…โœจ๐ŸŽ€๐Ÿฆ‹
๐Ÿ“ฆ where this page comes from