kfree_rcu(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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 gonna kfree, but only for the double-argument invocations, iykyk πŸ‘€
  • rhf β€” the name of the struct rcu_head that lives inside the type of ptr. 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 than 4095 bytes, a compile-time error will be generated in kvfree_rcu_arg_2. if you hit this, you've got two options: either fall back to use of call_rcu, or rearrange the structure to position the rcu_head structure into the first 4096 bytes. 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:


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