kunit_add_action(9)
π 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 intofunc. 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:
- Kernel file
./include/kunit/resource.h struct kunit_resource(9)kunit_get_resource(9)kunit_put_resource(9)__kunit_add_resource(9)kunit_add_resource(9)kunit_add_named_resource(9)kunit_alloc_and_get_resource(9)kunit_alloc_resource(9)kunit_resource_name_match(9)kunit_find_resource(9)kunit_find_named_resource(9)kunit_destroy_resource(9)kunit_remove_resource(9)KUNIT_DEFINE_ACTION_WRAPPER(9)kunit_add_action_or_reset(9)kunit_remove_action(9)kunit_release_action(9)
π 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
- 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