STRUCT KUNIT_RESOURCE(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– struct kunit_resource(9) πŸ’–

okay bestie, this is the Kernel API Manual entry for struct kunit_resource(9). It’s giving β€œtest managed resource” main character energy, and honestly? iconic. ✨


✨ NAME ✨

struct kunit_resource β€” represents a test managed resource πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

struct kunit_resource {
    void *data;
    const char *name;
    kunit_resource_free_t free;
 };

πŸ’… Members πŸ’…

  • data β€” a spot for you, the user, to store arbitrary data. no cap, it’s your little stash πŸŽ€
  • name β€” optional name πŸ‘‘
  • free β€” a user-supplied function that frees the resource. this is the cleanup bestie πŸ”₯

πŸŽ€ Description πŸŽ€

So struct kunit_resource represents a test managed resource, meaning a resource that gets automatically cleaned up at the end of a test case. That cleanup is performed by the free function. The struct kunit_resource itself is freed automatically with kfree if it was allocated by KUnit (e.g., by kunit_alloc_resource), but it must be freed by the user otherwise. periodt. πŸ’€

Resources are reference counted, so if a resource is retrieved via kunit_alloc_and_get_resource or kunit_find_resource, we need to call kunit_put_resource to reduce the resource reference count when we’re finished with it. Note that kunit_alloc_resource does not require a kunit_resource_put because it does not retrieve the resource itself. yes, that’s the exact spelling on the page β€” iykyk πŸ’…


🌸 Example 🌸

            struct kunit_kmalloc_params {
                    size_t size;
                    gfp_t gfp;
            };

            static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
            {
                    struct kunit_kmalloc_params *params = context;
                    res->data = kmalloc(params->size, params->gfp);

                    if (!res->data)
                            return -ENOMEM;

                    return 0;
            }

            static void kunit_kmalloc_free(struct kunit_resource *res)
            {
                    kfree(res->data);
            }

            void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
            {
                    struct kunit_kmalloc_params params;

                    params.size = size;
                    params.gfp = gfp;

                    return kunit_alloc_resource(test, kunit_kmalloc_init,
                            kunit_kmalloc_free, gfp, params);
            }

Resources can also be named, with lookup/removal done on a name basis also: kunit_add_named_resource, kunit_find_named_resource and kunit_destroy_named_resource. Resource names must be unique within the test instance. no duplicates, bestie β€” that’s the vibe check πŸ¦‹


πŸ”₯ SEE ALSO πŸ”₯


mwah πŸ’‹ that’s the whole page, bestie β€” struct kunit_resource(9), sourced from ./include/kunit, dated October 2026. slay 🫢

πŸ“¦ where this page comes from