162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * KUnit resource API for test managed resources (allocations, etc.). 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2022, Google LLC. 662306a36Sopenharmony_ci * Author: Daniel Latypov <dlatypov@google.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#ifndef _KUNIT_RESOURCE_H 1062306a36Sopenharmony_ci#define _KUNIT_RESOURCE_H 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <kunit/test.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/kref.h> 1562306a36Sopenharmony_ci#include <linux/list.h> 1662306a36Sopenharmony_ci#include <linux/slab.h> 1762306a36Sopenharmony_ci#include <linux/spinlock.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cistruct kunit_resource; 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_citypedef int (*kunit_resource_init_t)(struct kunit_resource *, void *); 2262306a36Sopenharmony_citypedef void (*kunit_resource_free_t)(struct kunit_resource *); 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci/** 2562306a36Sopenharmony_ci * struct kunit_resource - represents a *test managed resource* 2662306a36Sopenharmony_ci * @data: for the user to store arbitrary data. 2762306a36Sopenharmony_ci * @name: optional name 2862306a36Sopenharmony_ci * @free: a user supplied function to free the resource. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * Represents a *test managed resource*, a resource which will automatically be 3162306a36Sopenharmony_ci * cleaned up at the end of a test case. This cleanup is performed by the 'free' 3262306a36Sopenharmony_ci * function. The struct kunit_resource itself is freed automatically with 3362306a36Sopenharmony_ci * kfree() if it was allocated by KUnit (e.g., by kunit_alloc_resource()), but 3462306a36Sopenharmony_ci * must be freed by the user otherwise. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * Resources are reference counted so if a resource is retrieved via 3762306a36Sopenharmony_ci * kunit_alloc_and_get_resource() or kunit_find_resource(), we need 3862306a36Sopenharmony_ci * to call kunit_put_resource() to reduce the resource reference count 3962306a36Sopenharmony_ci * when finished with it. Note that kunit_alloc_resource() does not require a 4062306a36Sopenharmony_ci * kunit_resource_put() because it does not retrieve the resource itself. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * Example: 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * .. code-block:: c 4562306a36Sopenharmony_ci * 4662306a36Sopenharmony_ci * struct kunit_kmalloc_params { 4762306a36Sopenharmony_ci * size_t size; 4862306a36Sopenharmony_ci * gfp_t gfp; 4962306a36Sopenharmony_ci * }; 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * static int kunit_kmalloc_init(struct kunit_resource *res, void *context) 5262306a36Sopenharmony_ci * { 5362306a36Sopenharmony_ci * struct kunit_kmalloc_params *params = context; 5462306a36Sopenharmony_ci * res->data = kmalloc(params->size, params->gfp); 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * if (!res->data) 5762306a36Sopenharmony_ci * return -ENOMEM; 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * return 0; 6062306a36Sopenharmony_ci * } 6162306a36Sopenharmony_ci * 6262306a36Sopenharmony_ci * static void kunit_kmalloc_free(struct kunit_resource *res) 6362306a36Sopenharmony_ci * { 6462306a36Sopenharmony_ci * kfree(res->data); 6562306a36Sopenharmony_ci * } 6662306a36Sopenharmony_ci * 6762306a36Sopenharmony_ci * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) 6862306a36Sopenharmony_ci * { 6962306a36Sopenharmony_ci * struct kunit_kmalloc_params params; 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * params.size = size; 7262306a36Sopenharmony_ci * params.gfp = gfp; 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * return kunit_alloc_resource(test, kunit_kmalloc_init, 7562306a36Sopenharmony_ci * kunit_kmalloc_free, gfp, ¶ms); 7662306a36Sopenharmony_ci * } 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * Resources can also be named, with lookup/removal done on a name 7962306a36Sopenharmony_ci * basis also. kunit_add_named_resource(), kunit_find_named_resource() 8062306a36Sopenharmony_ci * and kunit_destroy_named_resource(). Resource names must be 8162306a36Sopenharmony_ci * unique within the test instance. 8262306a36Sopenharmony_ci */ 8362306a36Sopenharmony_cistruct kunit_resource { 8462306a36Sopenharmony_ci void *data; 8562306a36Sopenharmony_ci const char *name; 8662306a36Sopenharmony_ci kunit_resource_free_t free; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci /* private: internal use only. */ 8962306a36Sopenharmony_ci struct kref refcount; 9062306a36Sopenharmony_ci struct list_head node; 9162306a36Sopenharmony_ci bool should_kfree; 9262306a36Sopenharmony_ci}; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci/** 9562306a36Sopenharmony_ci * kunit_get_resource() - Hold resource for use. Should not need to be used 9662306a36Sopenharmony_ci * by most users as we automatically get resources 9762306a36Sopenharmony_ci * retrieved by kunit_find_resource*(). 9862306a36Sopenharmony_ci * @res: resource 9962306a36Sopenharmony_ci */ 10062306a36Sopenharmony_cistatic inline void kunit_get_resource(struct kunit_resource *res) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci kref_get(&res->refcount); 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci/* 10662306a36Sopenharmony_ci * Called when refcount reaches zero via kunit_put_resource(); 10762306a36Sopenharmony_ci * should not be called directly. 10862306a36Sopenharmony_ci */ 10962306a36Sopenharmony_cistatic inline void kunit_release_resource(struct kref *kref) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci struct kunit_resource *res = container_of(kref, struct kunit_resource, 11262306a36Sopenharmony_ci refcount); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci if (res->free) 11562306a36Sopenharmony_ci res->free(res); 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci /* 'res' is valid here, as if should_kfree is set, res->free may not free 11862306a36Sopenharmony_ci * 'res' itself, just res->data 11962306a36Sopenharmony_ci */ 12062306a36Sopenharmony_ci if (res->should_kfree) 12162306a36Sopenharmony_ci kfree(res); 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci/** 12562306a36Sopenharmony_ci * kunit_put_resource() - When caller is done with retrieved resource, 12662306a36Sopenharmony_ci * kunit_put_resource() should be called to drop 12762306a36Sopenharmony_ci * reference count. The resource list maintains 12862306a36Sopenharmony_ci * a reference count on resources, so if no users 12962306a36Sopenharmony_ci * are utilizing a resource and it is removed from 13062306a36Sopenharmony_ci * the resource list, it will be freed via the 13162306a36Sopenharmony_ci * associated free function (if any). Only 13262306a36Sopenharmony_ci * needs to be used if we alloc_and_get() or 13362306a36Sopenharmony_ci * find() resource. 13462306a36Sopenharmony_ci * @res: resource 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_cistatic inline void kunit_put_resource(struct kunit_resource *res) 13762306a36Sopenharmony_ci{ 13862306a36Sopenharmony_ci kref_put(&res->refcount, kunit_release_resource); 13962306a36Sopenharmony_ci} 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci/** 14262306a36Sopenharmony_ci * __kunit_add_resource() - Internal helper to add a resource. 14362306a36Sopenharmony_ci * 14462306a36Sopenharmony_ci * res->should_kfree is not initialised. 14562306a36Sopenharmony_ci * @test: The test context object. 14662306a36Sopenharmony_ci * @init: a user-supplied function to initialize the result (if needed). If 14762306a36Sopenharmony_ci * none is supplied, the resource data value is simply set to @data. 14862306a36Sopenharmony_ci * If an init function is supplied, @data is passed to it instead. 14962306a36Sopenharmony_ci * @free: a user-supplied function to free the resource (if needed). 15062306a36Sopenharmony_ci * @res: The resource. 15162306a36Sopenharmony_ci * @data: value to pass to init function or set in resource data field. 15262306a36Sopenharmony_ci */ 15362306a36Sopenharmony_ciint __kunit_add_resource(struct kunit *test, 15462306a36Sopenharmony_ci kunit_resource_init_t init, 15562306a36Sopenharmony_ci kunit_resource_free_t free, 15662306a36Sopenharmony_ci struct kunit_resource *res, 15762306a36Sopenharmony_ci void *data); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci/** 16062306a36Sopenharmony_ci * kunit_add_resource() - Add a *test managed resource*. 16162306a36Sopenharmony_ci * @test: The test context object. 16262306a36Sopenharmony_ci * @init: a user-supplied function to initialize the result (if needed). If 16362306a36Sopenharmony_ci * none is supplied, the resource data value is simply set to @data. 16462306a36Sopenharmony_ci * If an init function is supplied, @data is passed to it instead. 16562306a36Sopenharmony_ci * @free: a user-supplied function to free the resource (if needed). 16662306a36Sopenharmony_ci * @res: The resource. 16762306a36Sopenharmony_ci * @data: value to pass to init function or set in resource data field. 16862306a36Sopenharmony_ci */ 16962306a36Sopenharmony_cistatic inline int kunit_add_resource(struct kunit *test, 17062306a36Sopenharmony_ci kunit_resource_init_t init, 17162306a36Sopenharmony_ci kunit_resource_free_t free, 17262306a36Sopenharmony_ci struct kunit_resource *res, 17362306a36Sopenharmony_ci void *data) 17462306a36Sopenharmony_ci{ 17562306a36Sopenharmony_ci res->should_kfree = false; 17662306a36Sopenharmony_ci return __kunit_add_resource(test, init, free, res, data); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_cistatic inline struct kunit_resource * 18062306a36Sopenharmony_cikunit_find_named_resource(struct kunit *test, const char *name); 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci/** 18362306a36Sopenharmony_ci * kunit_add_named_resource() - Add a named *test managed resource*. 18462306a36Sopenharmony_ci * @test: The test context object. 18562306a36Sopenharmony_ci * @init: a user-supplied function to initialize the resource data, if needed. 18662306a36Sopenharmony_ci * @free: a user-supplied function to free the resource data, if needed. 18762306a36Sopenharmony_ci * @res: The resource. 18862306a36Sopenharmony_ci * @name: name to be set for resource. 18962306a36Sopenharmony_ci * @data: value to pass to init function or set in resource data field. 19062306a36Sopenharmony_ci */ 19162306a36Sopenharmony_cistatic inline int kunit_add_named_resource(struct kunit *test, 19262306a36Sopenharmony_ci kunit_resource_init_t init, 19362306a36Sopenharmony_ci kunit_resource_free_t free, 19462306a36Sopenharmony_ci struct kunit_resource *res, 19562306a36Sopenharmony_ci const char *name, 19662306a36Sopenharmony_ci void *data) 19762306a36Sopenharmony_ci{ 19862306a36Sopenharmony_ci struct kunit_resource *existing; 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci if (!name) 20162306a36Sopenharmony_ci return -EINVAL; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci existing = kunit_find_named_resource(test, name); 20462306a36Sopenharmony_ci if (existing) { 20562306a36Sopenharmony_ci kunit_put_resource(existing); 20662306a36Sopenharmony_ci return -EEXIST; 20762306a36Sopenharmony_ci } 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci res->name = name; 21062306a36Sopenharmony_ci res->should_kfree = false; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci return __kunit_add_resource(test, init, free, res, data); 21362306a36Sopenharmony_ci} 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci/** 21662306a36Sopenharmony_ci * kunit_alloc_and_get_resource() - Allocates and returns a *test managed resource*. 21762306a36Sopenharmony_ci * @test: The test context object. 21862306a36Sopenharmony_ci * @init: a user supplied function to initialize the resource. 21962306a36Sopenharmony_ci * @free: a user supplied function to free the resource (if needed). 22062306a36Sopenharmony_ci * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL 22162306a36Sopenharmony_ci * @context: for the user to pass in arbitrary data to the init function. 22262306a36Sopenharmony_ci * 22362306a36Sopenharmony_ci * Allocates a *test managed resource*, a resource which will automatically be 22462306a36Sopenharmony_ci * cleaned up at the end of a test case. See &struct kunit_resource for an 22562306a36Sopenharmony_ci * example. 22662306a36Sopenharmony_ci * 22762306a36Sopenharmony_ci * This is effectively identical to kunit_alloc_resource, but returns the 22862306a36Sopenharmony_ci * struct kunit_resource pointer, not just the 'data' pointer. It therefore 22962306a36Sopenharmony_ci * also increments the resource's refcount, so kunit_put_resource() should be 23062306a36Sopenharmony_ci * called when you've finished with it. 23162306a36Sopenharmony_ci * 23262306a36Sopenharmony_ci * Note: KUnit needs to allocate memory for a kunit_resource object. You must 23362306a36Sopenharmony_ci * specify an @internal_gfp that is compatible with the use context of your 23462306a36Sopenharmony_ci * resource. 23562306a36Sopenharmony_ci */ 23662306a36Sopenharmony_cistatic inline struct kunit_resource * 23762306a36Sopenharmony_cikunit_alloc_and_get_resource(struct kunit *test, 23862306a36Sopenharmony_ci kunit_resource_init_t init, 23962306a36Sopenharmony_ci kunit_resource_free_t free, 24062306a36Sopenharmony_ci gfp_t internal_gfp, 24162306a36Sopenharmony_ci void *context) 24262306a36Sopenharmony_ci{ 24362306a36Sopenharmony_ci struct kunit_resource *res; 24462306a36Sopenharmony_ci int ret; 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci res = kzalloc(sizeof(*res), internal_gfp); 24762306a36Sopenharmony_ci if (!res) 24862306a36Sopenharmony_ci return NULL; 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci res->should_kfree = true; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci ret = __kunit_add_resource(test, init, free, res, context); 25362306a36Sopenharmony_ci if (!ret) { 25462306a36Sopenharmony_ci /* 25562306a36Sopenharmony_ci * bump refcount for get; kunit_resource_put() should be called 25662306a36Sopenharmony_ci * when done. 25762306a36Sopenharmony_ci */ 25862306a36Sopenharmony_ci kunit_get_resource(res); 25962306a36Sopenharmony_ci return res; 26062306a36Sopenharmony_ci } 26162306a36Sopenharmony_ci return NULL; 26262306a36Sopenharmony_ci} 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci/** 26562306a36Sopenharmony_ci * kunit_alloc_resource() - Allocates a *test managed resource*. 26662306a36Sopenharmony_ci * @test: The test context object. 26762306a36Sopenharmony_ci * @init: a user supplied function to initialize the resource. 26862306a36Sopenharmony_ci * @free: a user supplied function to free the resource (if needed). 26962306a36Sopenharmony_ci * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL 27062306a36Sopenharmony_ci * @context: for the user to pass in arbitrary data to the init function. 27162306a36Sopenharmony_ci * 27262306a36Sopenharmony_ci * Allocates a *test managed resource*, a resource which will automatically be 27362306a36Sopenharmony_ci * cleaned up at the end of a test case. See &struct kunit_resource for an 27462306a36Sopenharmony_ci * example. 27562306a36Sopenharmony_ci * 27662306a36Sopenharmony_ci * Note: KUnit needs to allocate memory for a kunit_resource object. You must 27762306a36Sopenharmony_ci * specify an @internal_gfp that is compatible with the use context of your 27862306a36Sopenharmony_ci * resource. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_cistatic inline void *kunit_alloc_resource(struct kunit *test, 28162306a36Sopenharmony_ci kunit_resource_init_t init, 28262306a36Sopenharmony_ci kunit_resource_free_t free, 28362306a36Sopenharmony_ci gfp_t internal_gfp, 28462306a36Sopenharmony_ci void *context) 28562306a36Sopenharmony_ci{ 28662306a36Sopenharmony_ci struct kunit_resource *res; 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci res = kzalloc(sizeof(*res), internal_gfp); 28962306a36Sopenharmony_ci if (!res) 29062306a36Sopenharmony_ci return NULL; 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci res->should_kfree = true; 29362306a36Sopenharmony_ci if (!__kunit_add_resource(test, init, free, res, context)) 29462306a36Sopenharmony_ci return res->data; 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci return NULL; 29762306a36Sopenharmony_ci} 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_citypedef bool (*kunit_resource_match_t)(struct kunit *test, 30062306a36Sopenharmony_ci struct kunit_resource *res, 30162306a36Sopenharmony_ci void *match_data); 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci/** 30462306a36Sopenharmony_ci * kunit_resource_name_match() - Match a resource with the same name. 30562306a36Sopenharmony_ci * @test: Test case to which the resource belongs. 30662306a36Sopenharmony_ci * @res: The resource. 30762306a36Sopenharmony_ci * @match_name: The name to match against. 30862306a36Sopenharmony_ci */ 30962306a36Sopenharmony_cistatic inline bool kunit_resource_name_match(struct kunit *test, 31062306a36Sopenharmony_ci struct kunit_resource *res, 31162306a36Sopenharmony_ci void *match_name) 31262306a36Sopenharmony_ci{ 31362306a36Sopenharmony_ci return res->name && strcmp(res->name, match_name) == 0; 31462306a36Sopenharmony_ci} 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci/** 31762306a36Sopenharmony_ci * kunit_find_resource() - Find a resource using match function/data. 31862306a36Sopenharmony_ci * @test: Test case to which the resource belongs. 31962306a36Sopenharmony_ci * @match: match function to be applied to resources/match data. 32062306a36Sopenharmony_ci * @match_data: data to be used in matching. 32162306a36Sopenharmony_ci */ 32262306a36Sopenharmony_cistatic inline struct kunit_resource * 32362306a36Sopenharmony_cikunit_find_resource(struct kunit *test, 32462306a36Sopenharmony_ci kunit_resource_match_t match, 32562306a36Sopenharmony_ci void *match_data) 32662306a36Sopenharmony_ci{ 32762306a36Sopenharmony_ci struct kunit_resource *res, *found = NULL; 32862306a36Sopenharmony_ci unsigned long flags; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci spin_lock_irqsave(&test->lock, flags); 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci list_for_each_entry_reverse(res, &test->resources, node) { 33362306a36Sopenharmony_ci if (match(test, res, (void *)match_data)) { 33462306a36Sopenharmony_ci found = res; 33562306a36Sopenharmony_ci kunit_get_resource(found); 33662306a36Sopenharmony_ci break; 33762306a36Sopenharmony_ci } 33862306a36Sopenharmony_ci } 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ci spin_unlock_irqrestore(&test->lock, flags); 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci return found; 34362306a36Sopenharmony_ci} 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci/** 34662306a36Sopenharmony_ci * kunit_find_named_resource() - Find a resource using match name. 34762306a36Sopenharmony_ci * @test: Test case to which the resource belongs. 34862306a36Sopenharmony_ci * @name: match name. 34962306a36Sopenharmony_ci */ 35062306a36Sopenharmony_cistatic inline struct kunit_resource * 35162306a36Sopenharmony_cikunit_find_named_resource(struct kunit *test, 35262306a36Sopenharmony_ci const char *name) 35362306a36Sopenharmony_ci{ 35462306a36Sopenharmony_ci return kunit_find_resource(test, kunit_resource_name_match, 35562306a36Sopenharmony_ci (void *)name); 35662306a36Sopenharmony_ci} 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci/** 35962306a36Sopenharmony_ci * kunit_destroy_resource() - Find a kunit_resource and destroy it. 36062306a36Sopenharmony_ci * @test: Test case to which the resource belongs. 36162306a36Sopenharmony_ci * @match: Match function. Returns whether a given resource matches @match_data. 36262306a36Sopenharmony_ci * @match_data: Data passed into @match. 36362306a36Sopenharmony_ci * 36462306a36Sopenharmony_ci * RETURNS: 36562306a36Sopenharmony_ci * 0 if kunit_resource is found and freed, -ENOENT if not found. 36662306a36Sopenharmony_ci */ 36762306a36Sopenharmony_ciint kunit_destroy_resource(struct kunit *test, 36862306a36Sopenharmony_ci kunit_resource_match_t match, 36962306a36Sopenharmony_ci void *match_data); 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_cistatic inline int kunit_destroy_named_resource(struct kunit *test, 37262306a36Sopenharmony_ci const char *name) 37362306a36Sopenharmony_ci{ 37462306a36Sopenharmony_ci return kunit_destroy_resource(test, kunit_resource_name_match, 37562306a36Sopenharmony_ci (void *)name); 37662306a36Sopenharmony_ci} 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ci/** 37962306a36Sopenharmony_ci * kunit_remove_resource() - remove resource from resource list associated with 38062306a36Sopenharmony_ci * test. 38162306a36Sopenharmony_ci * @test: The test context object. 38262306a36Sopenharmony_ci * @res: The resource to be removed. 38362306a36Sopenharmony_ci * 38462306a36Sopenharmony_ci * Note that the resource will not be immediately freed since it is likely 38562306a36Sopenharmony_ci * the caller has a reference to it via alloc_and_get() or find(); 38662306a36Sopenharmony_ci * in this case a final call to kunit_put_resource() is required. 38762306a36Sopenharmony_ci */ 38862306a36Sopenharmony_civoid kunit_remove_resource(struct kunit *test, struct kunit_resource *res); 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci/* A 'deferred action' function to be used with kunit_add_action. */ 39162306a36Sopenharmony_citypedef void (kunit_action_t)(void *); 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_ci/** 39462306a36Sopenharmony_ci * kunit_add_action() - Call a function when the test ends. 39562306a36Sopenharmony_ci * @test: Test case to associate the action with. 39662306a36Sopenharmony_ci * @action: The function to run on test exit 39762306a36Sopenharmony_ci * @ctx: Data passed into @func 39862306a36Sopenharmony_ci * 39962306a36Sopenharmony_ci * Defer the execution of a function until the test exits, either normally or 40062306a36Sopenharmony_ci * due to a failure. @ctx is passed as additional context. All functions 40162306a36Sopenharmony_ci * registered with kunit_add_action() will execute in the opposite order to that 40262306a36Sopenharmony_ci * they were registered in. 40362306a36Sopenharmony_ci * 40462306a36Sopenharmony_ci * This is useful for cleaning up allocated memory and resources, as these 40562306a36Sopenharmony_ci * functions are called even if the test aborts early due to, e.g., a failed 40662306a36Sopenharmony_ci * assertion. 40762306a36Sopenharmony_ci * 40862306a36Sopenharmony_ci * See also: devm_add_action() for the devres equivalent. 40962306a36Sopenharmony_ci * 41062306a36Sopenharmony_ci * Returns: 41162306a36Sopenharmony_ci * 0 on success, an error if the action could not be deferred. 41262306a36Sopenharmony_ci */ 41362306a36Sopenharmony_ciint kunit_add_action(struct kunit *test, kunit_action_t *action, void *ctx); 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci/** 41662306a36Sopenharmony_ci * kunit_add_action_or_reset() - Call a function when the test ends. 41762306a36Sopenharmony_ci * @test: Test case to associate the action with. 41862306a36Sopenharmony_ci * @action: The function to run on test exit 41962306a36Sopenharmony_ci * @ctx: Data passed into @func 42062306a36Sopenharmony_ci * 42162306a36Sopenharmony_ci * Defer the execution of a function until the test exits, either normally or 42262306a36Sopenharmony_ci * due to a failure. @ctx is passed as additional context. All functions 42362306a36Sopenharmony_ci * registered with kunit_add_action() will execute in the opposite order to that 42462306a36Sopenharmony_ci * they were registered in. 42562306a36Sopenharmony_ci * 42662306a36Sopenharmony_ci * This is useful for cleaning up allocated memory and resources, as these 42762306a36Sopenharmony_ci * functions are called even if the test aborts early due to, e.g., a failed 42862306a36Sopenharmony_ci * assertion. 42962306a36Sopenharmony_ci * 43062306a36Sopenharmony_ci * If the action cannot be created (e.g., due to the system being out of memory), 43162306a36Sopenharmony_ci * then action(ctx) will be called immediately, and an error will be returned. 43262306a36Sopenharmony_ci * 43362306a36Sopenharmony_ci * See also: devm_add_action_or_reset() for the devres equivalent. 43462306a36Sopenharmony_ci * 43562306a36Sopenharmony_ci * Returns: 43662306a36Sopenharmony_ci * 0 on success, an error if the action could not be deferred. 43762306a36Sopenharmony_ci */ 43862306a36Sopenharmony_ciint kunit_add_action_or_reset(struct kunit *test, kunit_action_t *action, 43962306a36Sopenharmony_ci void *ctx); 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci/** 44262306a36Sopenharmony_ci * kunit_remove_action() - Cancel a matching deferred action. 44362306a36Sopenharmony_ci * @test: Test case the action is associated with. 44462306a36Sopenharmony_ci * @action: The deferred function to cancel. 44562306a36Sopenharmony_ci * @ctx: The context passed to the deferred function to trigger. 44662306a36Sopenharmony_ci * 44762306a36Sopenharmony_ci * Prevent an action deferred via kunit_add_action() from executing when the 44862306a36Sopenharmony_ci * test terminates. 44962306a36Sopenharmony_ci * 45062306a36Sopenharmony_ci * If the function/context pair was deferred multiple times, only the most 45162306a36Sopenharmony_ci * recent one will be cancelled. 45262306a36Sopenharmony_ci * 45362306a36Sopenharmony_ci * See also: devm_remove_action() for the devres equivalent. 45462306a36Sopenharmony_ci */ 45562306a36Sopenharmony_civoid kunit_remove_action(struct kunit *test, 45662306a36Sopenharmony_ci kunit_action_t *action, 45762306a36Sopenharmony_ci void *ctx); 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci/** 46062306a36Sopenharmony_ci * kunit_release_action() - Run a matching action call immediately. 46162306a36Sopenharmony_ci * @test: Test case the action is associated with. 46262306a36Sopenharmony_ci * @action: The deferred function to trigger. 46362306a36Sopenharmony_ci * @ctx: The context passed to the deferred function to trigger. 46462306a36Sopenharmony_ci * 46562306a36Sopenharmony_ci * Execute a function deferred via kunit_add_action()) immediately, rather than 46662306a36Sopenharmony_ci * when the test ends. 46762306a36Sopenharmony_ci * 46862306a36Sopenharmony_ci * If the function/context pair was deferred multiple times, it will only be 46962306a36Sopenharmony_ci * executed once here. The most recent deferral will no longer execute when 47062306a36Sopenharmony_ci * the test ends. 47162306a36Sopenharmony_ci * 47262306a36Sopenharmony_ci * kunit_release_action(test, func, ctx); 47362306a36Sopenharmony_ci * is equivalent to 47462306a36Sopenharmony_ci * func(ctx); 47562306a36Sopenharmony_ci * kunit_remove_action(test, func, ctx); 47662306a36Sopenharmony_ci * 47762306a36Sopenharmony_ci * See also: devm_release_action() for the devres equivalent. 47862306a36Sopenharmony_ci */ 47962306a36Sopenharmony_civoid kunit_release_action(struct kunit *test, 48062306a36Sopenharmony_ci kunit_action_t *action, 48162306a36Sopenharmony_ci void *ctx); 48262306a36Sopenharmony_ci#endif /* _KUNIT_RESOURCE_H */ 483