kernfs_remove_self(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… kernfs_remove_self β€” like, the Kernel API Manual edition, bestie

okay so bestie, listen up, because this one is genuinely iconic in a nerdy way: kernfs_remove_self lets a kernfs_node yeet itself out of existence from inside its own method. main character energy fr fr πŸ‘‘


✨ NAME ✨

kernfs_remove_self β€” remove a kernfs_node from its own method πŸŽ€


🌸 SYNOPSIS 🌸

bool kernfs_remove_self (struct kernfs_node *kn );

πŸ¦‹ ARGUMENTS πŸ¦‹

  • kn β€” the self kernfs_node to remove. yes, the one doing the deleting. it's giving self-care honestly πŸ’…

🧚 DESCRIPTION 🧚

okay so first thing: the caller must be running off of a kernfs operation which is invoked with an active reference β€” like, one of kernfs_ops, no cap. this whole thing exists so you can implement a file operation that deletes itself. 🫢

for example: the "delete" file for a sysfs device directory can be implemented by invoking kernfs_remove_self on the "delete" file itself. literally the file is the main character and the victim, we love a multitasker ✨

this function breaks the circular dependency of trying to deactivate self while holding an active ref itself. and here's the chill part β€” it isn't necessary to modify the usual removal path to use kernfs_remove_self. the "delete" implementation can simply invoke kernfs_remove_self on self before proceeding with the usual removal path. kernfs will ignore later kernfs_remove on self. so like, no messy aftermath, it just doesn't care anymore 😌

now the spicy part 🌢️: kernfs_remove_self can be called multiple times concurrently on the same kernfs_node. only the first one actually performs removal and returns true. all others will wait until the kernfs operation which won self-removal finishes and return false. and note β€” the losers wait for the completion of not only the winning kernfs_remove_self but also the whole kernfs_ops which won the arbitration. lowkey that's a whole group project dynamic 😭 but it's useful! this can be used to guarantee, for example, all concurrent writes to a "delete" file to finish only after the whole operation is complete. slay, synchronized πŸ’…


πŸ”₯ RETURN πŸ”₯

  • true if kn is removed by this call
  • otherwise false

(and remember: only the first concurrent caller gets that true. everyone else gets false and a participation medal πŸŽ–οΈ)


πŸ‘‘ SEE ALSO πŸ‘‘

kernel file ./fs/kernfs/dir.c β€” and the whole squad, alphabetically unbothered:


🫢 reminder bestie: the caller has to be running off a kernfs operation invoked with an active reference β€” that's not a vibe suggestion, it's a requirement. don't come for me if you skip it πŸ™ˆ

and that's the whole page, bestie! πŸ“ this little gem lives in ./fs/kernfs and the version/date stamps say October 2026 β€” so like, future-proof and fabulous βœ¨πŸ’œ bye, ily, go remove yourself from your own method πŸ’…πŸŒˆβ­

πŸ“¦ where this page comes from