klp_shadow_get_or_alloc(9)
π 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 sameobj.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 thectorneeds. 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 π
- Kernel file
./kernel/livepatch/shadow.cβ Shadow variable API concurrency notes:(9) π struct klp_shadow(9)klp_shadow_match(9)klp_shadow_get(9)klp_shadow_alloc(9)klp_shadow_free(9)klp_shadow_free_all(9)
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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms