kref_get_unless_zero(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… kref_get_unless_zero β€” a Kernel API Manual moment (section 9, bestie)

okay so bestie, welcome to kref_get_unless_zero(9) from the Kernel API Manual β€” yes this whole page lives in section 9 and it's iconic. no cap, this one's short but the logic is chef's kiss. πŸŽ€


✨ NAME ✨

kref_get_unless_zero β€” increment the refcount for an object unless it's zero. that's it. that's the whole vibe. πŸ’…


✨ SYNOPSIS ✨

int kref_get_unless_zero (struct kref *kref );

✨ ARGUMENTS ✨

  • kref β€” the object. that's the whole arg, she's low maintenance. πŸ¦‹

✨ DESCRIPTION ✨

so here's the tea πŸ“: this function exists to make locking around refcounting so much simpler for objects that can be looked up from a lookup structure β€” and which get removed from that lookup structure inside the object destructor.

operations on objects like that need at least a read lock wrapped around the lookup + kref_get, and a write lock wrapped around kref_put + the removal from the lookup structure. and honestly? RCU implementations get extremely tricky from there. 😭

but here's where it eats πŸ’…βœ¨: if you do a lookup and then a kref_get_unless_zero with return value check, then the locking in the kref_put path can be deferred all the way to the actual removal from the lookup structure β€” and RCU lookups become trivial. like, embarrassingly easy. main character behaviour, fr fr.

ngl the "with return value check" part is doing a LOT of heavy lifting here β€” she's bolded and italicised in the original for a reason, bestie. don't skip her. πŸ™ˆ

✨ RETURN ✨

non-zero if the increment succeeded ✨. otherwise, 0. that's it, that's the whole return value arc. πŸ’€


✨ SEE ALSO ✨

go read the whole kref family, they're all baddies. πŸ‘‘


and that's the page, bestie 🫢 β€” sourced from ./include/linux, dated October 2026, and filed under the kref_get_unless_zero(9) manual. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from