kunit_add_action(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… kunit_add_action(9) πŸ’…

okay bestie, so you know how your kernel test can just abort mid-way and leave a whole mess of allocated memory behind? yeah. this is the function that fixes that. it lives in the Kernel API Manual and it's literally about to be your new fave. ✨


✨ NAME ✨

kunit_add_action β€” Call a function when the test ends.

periodt. that's the whole vibe. πŸŽ€


🌸 SYNOPSIS 🌸

int kunit_add_action (struct kunit *test , kunit_action_t *action , void
*ctx );

like, three args, one job: schedule a lil cleanup callback for when the test is done. no cap. 🫢


πŸ¦‹ ARGUMENTS πŸ¦‹

  • test β€” the test case you're associating the action with. this is the main character everything hangs off of. πŸ‘‘
  • action β€” the function that runs on test exit. yes, that one. the cleanup queen. πŸ’…
  • ctx β€” data passed into func. it's the lil care package your callback gets handed. πŸ“

🌈 DESCRIPTION 🌈

okay so here's the tea β˜• β€” kunit_add_action defers the execution of a function until the test exits, whether that's a normal, happy exit or a full-on failure exit. ctx gets passed in as additional context, so your callback has everything it needs.

and the iconic part? all functions registered with kunit_add_action will execute in the opposite order to that they were registered in. stack behaviour, bestie. last in, first out. the way that actually makes sense for cleanup 😭πŸ”₯

this is super useful for cleaning up allocated memory and resources, because these functions get called even if the test aborts early due to, like, a failed assertion. your cleanup does not ghost you. it shows up. 🫢

also see: devm_add_action for the devres equivalent β€” same energy, different subsystem. iykyk πŸ’œ

πŸ’– RETURN πŸ’–

0 on success. an error if the action could not be deferred. that's it. that's the whole return section. πŸ’€


🧚 SEE ALSO 🧚

bestie the friend group is LARGE, buckle up:


πŸ’€ reminder bestie: registration order is reversed on execution. don't be the person who forgets that and then wonders why their teardown is backwards. πŸ™ˆ

okay that's the whole page!! mwah πŸ’‹ this came from ./include/kunit and was dated October 2026, and the page is literally just called kunit_add_action(9). slay, go forth and clean up your resources properly. πŸŒŸπŸ’œ

πŸ“¦ where this page comes from