kfree_rcu(9)
π kfree_rcu(9) β Kernel API Manual π
okay so bestie, welcome to the glow-up of kfree_rcu(9), straight out of the Kernel API Manual. this one's short but it's important, so grab your iced coffee and let's get into it π«Ά
β¨ NAME β¨
kfree_rcu β literally just kfree an object after a grace period. that's the whole vibe. no cap.
π SYNOPSIS π
kfree_rcu (ptr , rhf );
πΈ ARGUMENTS πΈ
ptrβ the pointer you're gonnakfree, but only for the double-argument invocations, iykyk πrhfβ the name of thestruct rcu_headthat lives inside the type ofptr. yes, the name. not the thing itself. we're being cute about it π
π¦ DESCRIPTION π¦
so like, here's the situation, bestie. a LOT of rcu callback functions do exactly one thing: call kfree on the base structure. and likeβ¦ individually? precious. iconic. no notes. but their size adds up π and worse β when they get used in a kernel module, that module has to invoke the high-latency rcu_barrier function at module-unload time. the DRAMA. the latency. we simply cannot.
enter kfree_rcu, the main character π this function handles the whole issue for you.
now, to have a universal callback function that handles different offsets of rcu_head, the callback needs to figure out the starting address of the freed object β and that object can be a large kmalloc or vmalloc allocation. for those, the trick of just aligning the pointer down to the page boundary means only offsets up to 4095 bytes can be accommodated. bestie, 4095 is the limit, fr fr.
π¨ if the offset is larger than4095bytes, a compile-time error will be generated inkvfree_rcu_arg_2. if you hit this, you've got two options: either fall back to use ofcall_rcu, or rearrange the structure to position thercu_headstructure into the first4096bytes. your move πββοΈ
the object to be freed can be allocated either by kmalloc, kmalloc_nolock, or kmem_cache_alloc. all three, equally valid, we love a flexible icon β¨
π heads up: the allowable offset might decrease in the future. so like, lowkey don't build your entire personality around 4095.
also, the BUILD_BUG_ON check must not involve any function calls β hence the checks are done in macros here. that's just how it has to be, periodt. π
π₯ SEE ALSO π₯
okay this is the group chat list, and everybody's here. Kernel file ./include/linux/rcupdate.h plus the whole squad:
same_state_synchronize_rcu(9)cond_resched_tasks_rcu_qs(9)rcu_softirq_qs_periodic(9)RCU_LOCKDEP_WARN(9)lockdep_assert_in_rcu_read_lock(9)lockdep_assert_in_rcu_read_lock_bh(9)lockdep_assert_in_rcu_read_lock_sched(9)lockdep_assert_in_rcu_reader(9)unrcu_pointer(9)RCU_INITIALIZER(9)rcu_assign_pointer(9)rcu_replace_pointer(9)rcu_access_pointer(9)rcu_dereference_check(9)rcu_dereference_bh_check(9)rcu_dereference_sched_check(9)rcu_dereference_all_check(9)rcu_dereference_protected(9)rcu_dereference(9)rcu_dereference_bh(9)rcu_dereference_sched(9)rcu_dereference_all(9)rcu_pointer_handoff(9)rcu_read_lock(9)rcu_read_unlock(9)rcu_read_lock_bh(9)rcu_read_unlock_bh(9)rcu_read_lock_sched(9)rcu_read_unlock_sched(9)RCU_INIT_POINTER(9)RCU_POINTER_INITIALIZER(9)kfree_rcu_mightsleep(9)kfree_rcu_nolock(9)rcu_head_init(9)rcu_head_after_call_rcu(9)
that's the whole page, bestie, you ate that π this lil number lives in ./include/linux and it's dated October 2026, shoutout to the kfree_rcu(9) page itself for being so real πβ¨π¦
π¦ 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