kref_get_unless_zero(9)
π 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 β¨
- the kernel file
./include/linux/kref.h kref_init(9)kref_get(9)kref_put(9)kref_put_mutex(9)kref_put_lock(9)
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
- 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