klp_shadow_get_or_alloc(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– klp_shadow_get_or_alloc(9) β€” Kernel API Manual πŸ’–

okay so bestie, welcome to the Kernel API Manual, and today's main character is klp_shadow_get_or_alloc(9) β€” the "you already have one? cool, here it is. no? fine, i'll make one" of the livepatch shadow-variable world. it's giving ✨find-or-create✨ energy and honestly? iconic.


✨ SYNOPSIS ✨

void * klp_shadow_get_or_alloc (void *obj , unsigned long id , size_t
size , gfp_t gfp_flags , klp_shadow_ctor_t ctor , void *ctor_data );

like, that's the whole signature. six args, one pointer back out. slay πŸ’…


πŸŽ€ ARGUMENTS πŸŽ€

  • obj β€” pointer to the parent object 🫢 this is the thing your shadow is attached to, your anchor, your home base.
  • id β€” the data identifier. this is how you tell one shadow apart from another on the same obj.
  • size β€” size of the attached data, in bytes. so the kernel knows how much room to give you πŸ’–
  • gfp_flags β€” the GFP mask used for allocation. yes, the allocation vibes. πŸ“
  • ctor β€” a custom constructor to initialize the shadow data. it's optional! (no pressure, bestie β€” you can pass nothing.)
  • ctor_data β€” pointer to any data the ctor needs. also optional. team "only if you need it" 🌸

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea β˜• β€” if a shadow variable for <obj, id> is already chillin' there, you just get a pointer right back to that existing shadow data. no drama, no new allocation, literally just "oh you're already here, hey bestie πŸ’œ".

otherwise? it goes ahead and creates a brand new shadow variable, exactly like klp_shadow_alloc does. new shadow, fresh out the box ✨

now the important part, ok? this function guarantees that only one shadow variable exists with the given id for the given obj. one. not two. not zero when you expected one. it's a whole monogamy situation πŸ’€

it also guarantees that the constructor function will only be called when the variable did not exist before. so no calling ctor on something that was already there β€” that would be weird and rude.

and the cost of all this safety? ctor gets called in atomic context under a spin lock πŸ”₯ so like… keep that in mind, bestie. your constructor is not gonna be out here sleeping. no cap.

πŸ‘‘ RETURN πŸ‘‘

the shadow variable data element, bestie. that's the good stuff 🫢

or NULL on failure 😭 (we don't talk about failure.)


🌈 SEE ALSO 🌈

go read the concurrency notes though, fr fr β€” they're the lore 🧚


and that's the page!! brought to you by the fine folks over at ./kernel/livepatch, dated October 2026 β€” mwah πŸ’‹ this has been your girl, signing off, stay hydrated and keep your spin locks short βœŒοΈπŸ’–

πŸ“¦ where this page comes from