kunit_add_action_or_reset(9)
π kunit_add_action_or_reset(9) π
okay bestie, so this page lives in the Kernel API Manual and it's literally called kunit_add_action_or_reset. The name says it all, but let me spell it out for you: call a function when the test ends. that's it, that's the whole vibe. π
β¨ SYNOPSIS β¨
int kunit_add_action_or_reset (struct kunit *test , kunit_action_t
*action , void *ctx );
look at her. so clean. π¦
πΈ ARGUMENTS πΈ
testβ the test case you're tying this action to. like, this is the main character test, the action is her plus-one. πactionβ the function that's gonna run on test exit. the cleanup queen herself.ctxβ data passed intofunc. yes, the original saysfunchere even though the argument is calledaction. not me noticing that π β anyway, it's the extra stuff you're handing over as context.
π DESCRIPTION π
so the whole point: you're deferring the execution of a function until the test exits. and exit doesn't have to be pretty β it can be normally or due to a failure. either way, your function still runs. ctx gets passed as additional context, no cap.
and here's the iconic part: all functions registered with kunit_add_action will execute in the opposite order to that they were registered in. reverse order, bestie. last one in, first one out. it's giving stack energy and we're obsessed. π§
this is highkey useful for cleaning up allocated memory and resources, because these functions are called even if the test aborts early β like, say, due to a failed assertion. the test gave up, but the cleanup did NOT. ππ₯
β οΈ okay but listen: if the action cannot be created (e.g. because the system is out of memory, rude), then action(ctx) will be called immediately, and an error will be returned. so it's not silently dropped β it fires right away instead of waiting. important!! π
See also: devm_add_action_or_reset for the devres equivalent. siblings fr fr. π«Ά
π RETURN π
0 on success, and an error if the action could not be deferred. short and sweet. β
β SEE ALSO β
the whole resource family reunion, okay, everybody's here:
- 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(9)kunit_remove_action(9)kunit_release_action(9)
that's the page, bestie, go clean up your resources π
sign-off era: this little gem comes from ./include/kunit and it's dated October 2026 β so yeah, mark your calendar, screenshot it, put it on your fridge. πβ¨
π¦ 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