lib/kunit/resource.c
Source file repositories/reference/linux-study-clean/lib/kunit/resource.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kunit/resource.c- Extension
.c- Size
- 4420 bytes
- Lines
- 179
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/resource.hkunit/test.hlinux/kref.h
Detected Declarations
struct kunit_action_ctxfunction __kunit_add_resourcefunction kunit_remove_resourcefunction kunit_destroy_resourcefunction __kunit_action_freefunction kunit_add_actionfunction kunit_add_action_or_resetfunction __kunit_action_matchfunction kunit_remove_actionfunction kunit_release_actionexport __kunit_add_resourceexport kunit_remove_resourceexport kunit_destroy_resourceexport kunit_add_actionexport kunit_add_action_or_resetexport kunit_remove_actionexport kunit_release_action
Annotated Snippet
struct kunit_action_ctx {
struct kunit_resource res;
kunit_action_t *func;
void *ctx;
};
static void __kunit_action_free(struct kunit_resource *res)
{
struct kunit_action_ctx *action_ctx = container_of(res, struct kunit_action_ctx, res);
action_ctx->func(action_ctx->ctx);
}
int kunit_add_action(struct kunit *test, void (*action)(void *), void *ctx)
{
struct kunit_action_ctx *action_ctx;
KUNIT_ASSERT_NOT_NULL_MSG(test, action, "Tried to action a NULL function!");
action_ctx = kzalloc_obj(*action_ctx);
if (!action_ctx)
return -ENOMEM;
action_ctx->func = action;
action_ctx->ctx = ctx;
action_ctx->res.should_kfree = true;
/* As init is NULL, this cannot fail. */
__kunit_add_resource(test, NULL, __kunit_action_free, &action_ctx->res, action_ctx);
return 0;
}
EXPORT_SYMBOL_GPL(kunit_add_action);
int kunit_add_action_or_reset(struct kunit *test, void (*action)(void *),
void *ctx)
{
int res = kunit_add_action(test, action, ctx);
if (res)
action(ctx);
return res;
}
EXPORT_SYMBOL_GPL(kunit_add_action_or_reset);
static bool __kunit_action_match(struct kunit *test,
struct kunit_resource *res, void *match_data)
{
struct kunit_action_ctx *match_ctx = (struct kunit_action_ctx *)match_data;
struct kunit_action_ctx *res_ctx = container_of(res, struct kunit_action_ctx, res);
/* Make sure this is a free function. */
if (res->free != __kunit_action_free)
return false;
/* Both the function and context data should match. */
return (match_ctx->func == res_ctx->func) && (match_ctx->ctx == res_ctx->ctx);
}
void kunit_remove_action(struct kunit *test,
kunit_action_t *action,
void *ctx)
{
struct kunit_action_ctx match_ctx;
struct kunit_resource *res;
match_ctx.func = action;
match_ctx.ctx = ctx;
res = kunit_find_resource(test, __kunit_action_match, &match_ctx);
if (res) {
/* Remove the free function so we don't run the action. */
res->free = NULL;
kunit_remove_resource(test, res);
kunit_put_resource(res);
}
}
EXPORT_SYMBOL_GPL(kunit_remove_action);
void kunit_release_action(struct kunit *test,
kunit_action_t *action,
void *ctx)
{
struct kunit_action_ctx match_ctx;
struct kunit_resource *res;
match_ctx.func = action;
match_ctx.ctx = ctx;
Annotation
- Immediate include surface: `kunit/resource.h`, `kunit/test.h`, `linux/kref.h`.
- Detected declarations: `struct kunit_action_ctx`, `function __kunit_add_resource`, `function kunit_remove_resource`, `function kunit_destroy_resource`, `function __kunit_action_free`, `function kunit_add_action`, `function kunit_add_action_or_reset`, `function __kunit_action_match`, `function kunit_remove_action`, `function kunit_release_action`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.