xref: /openbmc/linux/include/kunit/resource.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
161695f8cSDaniel Latypov /* SPDX-License-Identifier: GPL-2.0 */
261695f8cSDaniel Latypov /*
361695f8cSDaniel Latypov  * KUnit resource API for test managed resources (allocations, etc.).
461695f8cSDaniel Latypov  *
561695f8cSDaniel Latypov  * Copyright (C) 2022, Google LLC.
661695f8cSDaniel Latypov  * Author: Daniel Latypov <dlatypov@google.com>
761695f8cSDaniel Latypov  */
861695f8cSDaniel Latypov 
961695f8cSDaniel Latypov #ifndef _KUNIT_RESOURCE_H
1061695f8cSDaniel Latypov #define _KUNIT_RESOURCE_H
1161695f8cSDaniel Latypov 
1261695f8cSDaniel Latypov #include <kunit/test.h>
1361695f8cSDaniel Latypov 
1461695f8cSDaniel Latypov #include <linux/kref.h>
1561695f8cSDaniel Latypov #include <linux/list.h>
1661695f8cSDaniel Latypov #include <linux/slab.h>
1761695f8cSDaniel Latypov #include <linux/spinlock.h>
1861695f8cSDaniel Latypov 
1961695f8cSDaniel Latypov struct kunit_resource;
2061695f8cSDaniel Latypov 
2161695f8cSDaniel Latypov typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
2261695f8cSDaniel Latypov typedef void (*kunit_resource_free_t)(struct kunit_resource *);
2361695f8cSDaniel Latypov 
2461695f8cSDaniel Latypov /**
2561695f8cSDaniel Latypov  * struct kunit_resource - represents a *test managed resource*
2661695f8cSDaniel Latypov  * @data: for the user to store arbitrary data.
2761695f8cSDaniel Latypov  * @name: optional name
28ad69172eSDavid Gow  * @free: a user supplied function to free the resource.
2961695f8cSDaniel Latypov  *
3061695f8cSDaniel Latypov  * Represents a *test managed resource*, a resource which will automatically be
31ad69172eSDavid Gow  * cleaned up at the end of a test case. This cleanup is performed by the 'free'
32ad69172eSDavid Gow  * function. The struct kunit_resource itself is freed automatically with
33ad69172eSDavid Gow  * kfree() if it was allocated by KUnit (e.g., by kunit_alloc_resource()), but
34ad69172eSDavid Gow  * must be freed by the user otherwise.
3561695f8cSDaniel Latypov  *
3661695f8cSDaniel Latypov  * Resources are reference counted so if a resource is retrieved via
3761695f8cSDaniel Latypov  * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
3861695f8cSDaniel Latypov  * to call kunit_put_resource() to reduce the resource reference count
3961695f8cSDaniel Latypov  * when finished with it.  Note that kunit_alloc_resource() does not require a
4061695f8cSDaniel Latypov  * kunit_resource_put() because it does not retrieve the resource itself.
4161695f8cSDaniel Latypov  *
4261695f8cSDaniel Latypov  * Example:
4361695f8cSDaniel Latypov  *
4461695f8cSDaniel Latypov  * .. code-block:: c
4561695f8cSDaniel Latypov  *
4661695f8cSDaniel Latypov  *	struct kunit_kmalloc_params {
4761695f8cSDaniel Latypov  *		size_t size;
4861695f8cSDaniel Latypov  *		gfp_t gfp;
4961695f8cSDaniel Latypov  *	};
5061695f8cSDaniel Latypov  *
5161695f8cSDaniel Latypov  *	static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
5261695f8cSDaniel Latypov  *	{
5361695f8cSDaniel Latypov  *		struct kunit_kmalloc_params *params = context;
5461695f8cSDaniel Latypov  *		res->data = kmalloc(params->size, params->gfp);
5561695f8cSDaniel Latypov  *
5661695f8cSDaniel Latypov  *		if (!res->data)
5761695f8cSDaniel Latypov  *			return -ENOMEM;
5861695f8cSDaniel Latypov  *
5961695f8cSDaniel Latypov  *		return 0;
6061695f8cSDaniel Latypov  *	}
6161695f8cSDaniel Latypov  *
6261695f8cSDaniel Latypov  *	static void kunit_kmalloc_free(struct kunit_resource *res)
6361695f8cSDaniel Latypov  *	{
6461695f8cSDaniel Latypov  *		kfree(res->data);
6561695f8cSDaniel Latypov  *	}
6661695f8cSDaniel Latypov  *
6761695f8cSDaniel Latypov  *	void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
6861695f8cSDaniel Latypov  *	{
6961695f8cSDaniel Latypov  *		struct kunit_kmalloc_params params;
7061695f8cSDaniel Latypov  *
7161695f8cSDaniel Latypov  *		params.size = size;
7261695f8cSDaniel Latypov  *		params.gfp = gfp;
7361695f8cSDaniel Latypov  *
7461695f8cSDaniel Latypov  *		return kunit_alloc_resource(test, kunit_kmalloc_init,
75*99be6588SStephen Boyd  *			kunit_kmalloc_free, gfp, &params);
7661695f8cSDaniel Latypov  *	}
7761695f8cSDaniel Latypov  *
7861695f8cSDaniel Latypov  * Resources can also be named, with lookup/removal done on a name
7961695f8cSDaniel Latypov  * basis also.  kunit_add_named_resource(), kunit_find_named_resource()
8061695f8cSDaniel Latypov  * and kunit_destroy_named_resource().  Resource names must be
8161695f8cSDaniel Latypov  * unique within the test instance.
8261695f8cSDaniel Latypov  */
8361695f8cSDaniel Latypov struct kunit_resource {
8461695f8cSDaniel Latypov 	void *data;
8561695f8cSDaniel Latypov 	const char *name;
8661695f8cSDaniel Latypov 	kunit_resource_free_t free;
8761695f8cSDaniel Latypov 
8861695f8cSDaniel Latypov 	/* private: internal use only. */
8961695f8cSDaniel Latypov 	struct kref refcount;
9061695f8cSDaniel Latypov 	struct list_head node;
91ad69172eSDavid Gow 	bool should_kfree;
9261695f8cSDaniel Latypov };
9361695f8cSDaniel Latypov 
9461695f8cSDaniel Latypov /**
9561695f8cSDaniel Latypov  * kunit_get_resource() - Hold resource for use.  Should not need to be used
9661695f8cSDaniel Latypov  *			  by most users as we automatically get resources
9761695f8cSDaniel Latypov  *			  retrieved by kunit_find_resource*().
9861695f8cSDaniel Latypov  * @res: resource
9961695f8cSDaniel Latypov  */
kunit_get_resource(struct kunit_resource * res)10061695f8cSDaniel Latypov static inline void kunit_get_resource(struct kunit_resource *res)
10161695f8cSDaniel Latypov {
10261695f8cSDaniel Latypov 	kref_get(&res->refcount);
10361695f8cSDaniel Latypov }
10461695f8cSDaniel Latypov 
10561695f8cSDaniel Latypov /*
10661695f8cSDaniel Latypov  * Called when refcount reaches zero via kunit_put_resource();
10761695f8cSDaniel Latypov  * should not be called directly.
10861695f8cSDaniel Latypov  */
kunit_release_resource(struct kref * kref)10961695f8cSDaniel Latypov static inline void kunit_release_resource(struct kref *kref)
11061695f8cSDaniel Latypov {
11161695f8cSDaniel Latypov 	struct kunit_resource *res = container_of(kref, struct kunit_resource,
11261695f8cSDaniel Latypov 						  refcount);
11361695f8cSDaniel Latypov 
114ad69172eSDavid Gow 	if (res->free)
11561695f8cSDaniel Latypov 		res->free(res);
116ad69172eSDavid Gow 
117ad69172eSDavid Gow 	/* 'res' is valid here, as if should_kfree is set, res->free may not free
118ad69172eSDavid Gow 	 * 'res' itself, just res->data
119ad69172eSDavid Gow 	 */
120ad69172eSDavid Gow 	if (res->should_kfree)
12161695f8cSDaniel Latypov 		kfree(res);
12261695f8cSDaniel Latypov }
12361695f8cSDaniel Latypov 
12461695f8cSDaniel Latypov /**
12561695f8cSDaniel Latypov  * kunit_put_resource() - When caller is done with retrieved resource,
12661695f8cSDaniel Latypov  *			  kunit_put_resource() should be called to drop
12761695f8cSDaniel Latypov  *			  reference count.  The resource list maintains
12861695f8cSDaniel Latypov  *			  a reference count on resources, so if no users
12961695f8cSDaniel Latypov  *			  are utilizing a resource and it is removed from
13061695f8cSDaniel Latypov  *			  the resource list, it will be freed via the
13161695f8cSDaniel Latypov  *			  associated free function (if any).  Only
13261695f8cSDaniel Latypov  *			  needs to be used if we alloc_and_get() or
13361695f8cSDaniel Latypov  *			  find() resource.
13461695f8cSDaniel Latypov  * @res: resource
13561695f8cSDaniel Latypov  */
kunit_put_resource(struct kunit_resource * res)13661695f8cSDaniel Latypov static inline void kunit_put_resource(struct kunit_resource *res)
13761695f8cSDaniel Latypov {
13861695f8cSDaniel Latypov 	kref_put(&res->refcount, kunit_release_resource);
13961695f8cSDaniel Latypov }
14061695f8cSDaniel Latypov 
14161695f8cSDaniel Latypov /**
142ad69172eSDavid Gow  * __kunit_add_resource() - Internal helper to add a resource.
143ad69172eSDavid Gow  *
144ad69172eSDavid Gow  * res->should_kfree is not initialised.
145ad69172eSDavid Gow  * @test: The test context object.
146ad69172eSDavid Gow  * @init: a user-supplied function to initialize the result (if needed).  If
147ad69172eSDavid Gow  *        none is supplied, the resource data value is simply set to @data.
148ad69172eSDavid Gow  *	  If an init function is supplied, @data is passed to it instead.
149ad69172eSDavid Gow  * @free: a user-supplied function to free the resource (if needed).
150ad69172eSDavid Gow  * @res: The resource.
151ad69172eSDavid Gow  * @data: value to pass to init function or set in resource data field.
152ad69172eSDavid Gow  */
153ad69172eSDavid Gow int __kunit_add_resource(struct kunit *test,
154ad69172eSDavid Gow 			 kunit_resource_init_t init,
155ad69172eSDavid Gow 			 kunit_resource_free_t free,
156ad69172eSDavid Gow 			 struct kunit_resource *res,
157ad69172eSDavid Gow 			 void *data);
158ad69172eSDavid Gow 
159ad69172eSDavid Gow /**
16061695f8cSDaniel Latypov  * kunit_add_resource() - Add a *test managed resource*.
16161695f8cSDaniel Latypov  * @test: The test context object.
16261695f8cSDaniel Latypov  * @init: a user-supplied function to initialize the result (if needed).  If
16361695f8cSDaniel Latypov  *        none is supplied, the resource data value is simply set to @data.
16461695f8cSDaniel Latypov  *	  If an init function is supplied, @data is passed to it instead.
16561695f8cSDaniel Latypov  * @free: a user-supplied function to free the resource (if needed).
16661695f8cSDaniel Latypov  * @res: The resource.
16761695f8cSDaniel Latypov  * @data: value to pass to init function or set in resource data field.
16861695f8cSDaniel Latypov  */
kunit_add_resource(struct kunit * test,kunit_resource_init_t init,kunit_resource_free_t free,struct kunit_resource * res,void * data)169ad69172eSDavid Gow static inline int kunit_add_resource(struct kunit *test,
17061695f8cSDaniel Latypov 				     kunit_resource_init_t init,
17161695f8cSDaniel Latypov 				     kunit_resource_free_t free,
17261695f8cSDaniel Latypov 				     struct kunit_resource *res,
173ad69172eSDavid Gow 				     void *data)
174ad69172eSDavid Gow {
175ad69172eSDavid Gow 	res->should_kfree = false;
176ad69172eSDavid Gow 	return __kunit_add_resource(test, init, free, res, data);
177ad69172eSDavid Gow }
178ad69172eSDavid Gow 
179ad69172eSDavid Gow static inline struct kunit_resource *
180ad69172eSDavid Gow kunit_find_named_resource(struct kunit *test, const char *name);
18161695f8cSDaniel Latypov 
18261695f8cSDaniel Latypov /**
18361695f8cSDaniel Latypov  * kunit_add_named_resource() - Add a named *test managed resource*.
18461695f8cSDaniel Latypov  * @test: The test context object.
18561695f8cSDaniel Latypov  * @init: a user-supplied function to initialize the resource data, if needed.
18661695f8cSDaniel Latypov  * @free: a user-supplied function to free the resource data, if needed.
18761695f8cSDaniel Latypov  * @res: The resource.
18861695f8cSDaniel Latypov  * @name: name to be set for resource.
18961695f8cSDaniel Latypov  * @data: value to pass to init function or set in resource data field.
19061695f8cSDaniel Latypov  */
kunit_add_named_resource(struct kunit * test,kunit_resource_init_t init,kunit_resource_free_t free,struct kunit_resource * res,const char * name,void * data)191ad69172eSDavid Gow static inline int kunit_add_named_resource(struct kunit *test,
19261695f8cSDaniel Latypov 					   kunit_resource_init_t init,
19361695f8cSDaniel Latypov 					   kunit_resource_free_t free,
19461695f8cSDaniel Latypov 					   struct kunit_resource *res,
19561695f8cSDaniel Latypov 					   const char *name,
196ad69172eSDavid Gow 					   void *data)
197ad69172eSDavid Gow {
198ad69172eSDavid Gow 	struct kunit_resource *existing;
199ad69172eSDavid Gow 
200ad69172eSDavid Gow 	if (!name)
201ad69172eSDavid Gow 		return -EINVAL;
202ad69172eSDavid Gow 
203ad69172eSDavid Gow 	existing = kunit_find_named_resource(test, name);
204ad69172eSDavid Gow 	if (existing) {
205ad69172eSDavid Gow 		kunit_put_resource(existing);
206ad69172eSDavid Gow 		return -EEXIST;
207ad69172eSDavid Gow 	}
208ad69172eSDavid Gow 
209ad69172eSDavid Gow 	res->name = name;
210ad69172eSDavid Gow 	res->should_kfree = false;
211ad69172eSDavid Gow 
212ad69172eSDavid Gow 	return __kunit_add_resource(test, init, free, res, data);
213ad69172eSDavid Gow }
214ad69172eSDavid Gow 
215ad69172eSDavid Gow /**
216ad69172eSDavid Gow  * kunit_alloc_and_get_resource() - Allocates and returns a *test managed resource*.
217ad69172eSDavid Gow  * @test: The test context object.
218ad69172eSDavid Gow  * @init: a user supplied function to initialize the resource.
219ad69172eSDavid Gow  * @free: a user supplied function to free the resource (if needed).
220ad69172eSDavid Gow  * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
221ad69172eSDavid Gow  * @context: for the user to pass in arbitrary data to the init function.
222ad69172eSDavid Gow  *
223ad69172eSDavid Gow  * Allocates a *test managed resource*, a resource which will automatically be
224ad69172eSDavid Gow  * cleaned up at the end of a test case. See &struct kunit_resource for an
225ad69172eSDavid Gow  * example.
226ad69172eSDavid Gow  *
227ad69172eSDavid Gow  * This is effectively identical to kunit_alloc_resource, but returns the
228ad69172eSDavid Gow  * struct kunit_resource pointer, not just the 'data' pointer. It therefore
229ad69172eSDavid Gow  * also increments the resource's refcount, so kunit_put_resource() should be
230ad69172eSDavid Gow  * called when you've finished with it.
231ad69172eSDavid Gow  *
232ad69172eSDavid Gow  * Note: KUnit needs to allocate memory for a kunit_resource object. You must
233ad69172eSDavid Gow  * specify an @internal_gfp that is compatible with the use context of your
234ad69172eSDavid Gow  * resource.
235ad69172eSDavid Gow  */
236ad69172eSDavid Gow static inline struct kunit_resource *
kunit_alloc_and_get_resource(struct kunit * test,kunit_resource_init_t init,kunit_resource_free_t free,gfp_t internal_gfp,void * context)237ad69172eSDavid Gow kunit_alloc_and_get_resource(struct kunit *test,
238ad69172eSDavid Gow 			     kunit_resource_init_t init,
239ad69172eSDavid Gow 			     kunit_resource_free_t free,
240ad69172eSDavid Gow 			     gfp_t internal_gfp,
241ad69172eSDavid Gow 			     void *context)
242ad69172eSDavid Gow {
243ad69172eSDavid Gow 	struct kunit_resource *res;
244ad69172eSDavid Gow 	int ret;
245ad69172eSDavid Gow 
246ad69172eSDavid Gow 	res = kzalloc(sizeof(*res), internal_gfp);
247ad69172eSDavid Gow 	if (!res)
248ad69172eSDavid Gow 		return NULL;
249ad69172eSDavid Gow 
250ad69172eSDavid Gow 	res->should_kfree = true;
251ad69172eSDavid Gow 
252ad69172eSDavid Gow 	ret = __kunit_add_resource(test, init, free, res, context);
253ad69172eSDavid Gow 	if (!ret) {
254ad69172eSDavid Gow 		/*
255ad69172eSDavid Gow 		 * bump refcount for get; kunit_resource_put() should be called
256ad69172eSDavid Gow 		 * when done.
257ad69172eSDavid Gow 		 */
258ad69172eSDavid Gow 		kunit_get_resource(res);
259ad69172eSDavid Gow 		return res;
260ad69172eSDavid Gow 	}
261ad69172eSDavid Gow 	return NULL;
262ad69172eSDavid Gow }
26361695f8cSDaniel Latypov 
26461695f8cSDaniel Latypov /**
26561695f8cSDaniel Latypov  * kunit_alloc_resource() - Allocates a *test managed resource*.
26661695f8cSDaniel Latypov  * @test: The test context object.
26761695f8cSDaniel Latypov  * @init: a user supplied function to initialize the resource.
268ad69172eSDavid Gow  * @free: a user supplied function to free the resource (if needed).
26961695f8cSDaniel Latypov  * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
27061695f8cSDaniel Latypov  * @context: for the user to pass in arbitrary data to the init function.
27161695f8cSDaniel Latypov  *
27261695f8cSDaniel Latypov  * Allocates a *test managed resource*, a resource which will automatically be
27361695f8cSDaniel Latypov  * cleaned up at the end of a test case. See &struct kunit_resource for an
27461695f8cSDaniel Latypov  * example.
27561695f8cSDaniel Latypov  *
27661695f8cSDaniel Latypov  * Note: KUnit needs to allocate memory for a kunit_resource object. You must
27761695f8cSDaniel Latypov  * specify an @internal_gfp that is compatible with the use context of your
27861695f8cSDaniel Latypov  * resource.
27961695f8cSDaniel Latypov  */
kunit_alloc_resource(struct kunit * test,kunit_resource_init_t init,kunit_resource_free_t free,gfp_t internal_gfp,void * context)28061695f8cSDaniel Latypov static inline void *kunit_alloc_resource(struct kunit *test,
28161695f8cSDaniel Latypov 					 kunit_resource_init_t init,
28261695f8cSDaniel Latypov 					 kunit_resource_free_t free,
28361695f8cSDaniel Latypov 					 gfp_t internal_gfp,
28461695f8cSDaniel Latypov 					 void *context)
28561695f8cSDaniel Latypov {
28661695f8cSDaniel Latypov 	struct kunit_resource *res;
28761695f8cSDaniel Latypov 
28861695f8cSDaniel Latypov 	res = kzalloc(sizeof(*res), internal_gfp);
28961695f8cSDaniel Latypov 	if (!res)
29061695f8cSDaniel Latypov 		return NULL;
29161695f8cSDaniel Latypov 
292ad69172eSDavid Gow 	res->should_kfree = true;
293ad69172eSDavid Gow 	if (!__kunit_add_resource(test, init, free, res, context))
29461695f8cSDaniel Latypov 		return res->data;
29561695f8cSDaniel Latypov 
29661695f8cSDaniel Latypov 	return NULL;
29761695f8cSDaniel Latypov }
29861695f8cSDaniel Latypov 
29961695f8cSDaniel Latypov typedef bool (*kunit_resource_match_t)(struct kunit *test,
30061695f8cSDaniel Latypov 				       struct kunit_resource *res,
30161695f8cSDaniel Latypov 				       void *match_data);
30261695f8cSDaniel Latypov 
30361695f8cSDaniel Latypov /**
30461695f8cSDaniel Latypov  * kunit_resource_name_match() - Match a resource with the same name.
30561695f8cSDaniel Latypov  * @test: Test case to which the resource belongs.
30661695f8cSDaniel Latypov  * @res: The resource.
30761695f8cSDaniel Latypov  * @match_name: The name to match against.
30861695f8cSDaniel Latypov  */
kunit_resource_name_match(struct kunit * test,struct kunit_resource * res,void * match_name)30961695f8cSDaniel Latypov static inline bool kunit_resource_name_match(struct kunit *test,
31061695f8cSDaniel Latypov 					     struct kunit_resource *res,
31161695f8cSDaniel Latypov 					     void *match_name)
31261695f8cSDaniel Latypov {
31361695f8cSDaniel Latypov 	return res->name && strcmp(res->name, match_name) == 0;
31461695f8cSDaniel Latypov }
31561695f8cSDaniel Latypov 
31661695f8cSDaniel Latypov /**
31761695f8cSDaniel Latypov  * kunit_find_resource() - Find a resource using match function/data.
31861695f8cSDaniel Latypov  * @test: Test case to which the resource belongs.
31961695f8cSDaniel Latypov  * @match: match function to be applied to resources/match data.
32061695f8cSDaniel Latypov  * @match_data: data to be used in matching.
32161695f8cSDaniel Latypov  */
32261695f8cSDaniel Latypov static inline struct kunit_resource *
kunit_find_resource(struct kunit * test,kunit_resource_match_t match,void * match_data)32361695f8cSDaniel Latypov kunit_find_resource(struct kunit *test,
32461695f8cSDaniel Latypov 		    kunit_resource_match_t match,
32561695f8cSDaniel Latypov 		    void *match_data)
32661695f8cSDaniel Latypov {
32761695f8cSDaniel Latypov 	struct kunit_resource *res, *found = NULL;
32861695f8cSDaniel Latypov 	unsigned long flags;
32961695f8cSDaniel Latypov 
33061695f8cSDaniel Latypov 	spin_lock_irqsave(&test->lock, flags);
33161695f8cSDaniel Latypov 
33261695f8cSDaniel Latypov 	list_for_each_entry_reverse(res, &test->resources, node) {
33361695f8cSDaniel Latypov 		if (match(test, res, (void *)match_data)) {
33461695f8cSDaniel Latypov 			found = res;
33561695f8cSDaniel Latypov 			kunit_get_resource(found);
33661695f8cSDaniel Latypov 			break;
33761695f8cSDaniel Latypov 		}
33861695f8cSDaniel Latypov 	}
33961695f8cSDaniel Latypov 
34061695f8cSDaniel Latypov 	spin_unlock_irqrestore(&test->lock, flags);
34161695f8cSDaniel Latypov 
34261695f8cSDaniel Latypov 	return found;
34361695f8cSDaniel Latypov }
34461695f8cSDaniel Latypov 
34561695f8cSDaniel Latypov /**
34661695f8cSDaniel Latypov  * kunit_find_named_resource() - Find a resource using match name.
34761695f8cSDaniel Latypov  * @test: Test case to which the resource belongs.
34861695f8cSDaniel Latypov  * @name: match name.
34961695f8cSDaniel Latypov  */
35061695f8cSDaniel Latypov static inline struct kunit_resource *
kunit_find_named_resource(struct kunit * test,const char * name)35161695f8cSDaniel Latypov kunit_find_named_resource(struct kunit *test,
35261695f8cSDaniel Latypov 			  const char *name)
35361695f8cSDaniel Latypov {
35461695f8cSDaniel Latypov 	return kunit_find_resource(test, kunit_resource_name_match,
35561695f8cSDaniel Latypov 				   (void *)name);
35661695f8cSDaniel Latypov }
35761695f8cSDaniel Latypov 
35861695f8cSDaniel Latypov /**
35961695f8cSDaniel Latypov  * kunit_destroy_resource() - Find a kunit_resource and destroy it.
36061695f8cSDaniel Latypov  * @test: Test case to which the resource belongs.
36161695f8cSDaniel Latypov  * @match: Match function. Returns whether a given resource matches @match_data.
36261695f8cSDaniel Latypov  * @match_data: Data passed into @match.
36361695f8cSDaniel Latypov  *
36461695f8cSDaniel Latypov  * RETURNS:
36561695f8cSDaniel Latypov  * 0 if kunit_resource is found and freed, -ENOENT if not found.
36661695f8cSDaniel Latypov  */
36761695f8cSDaniel Latypov int kunit_destroy_resource(struct kunit *test,
36861695f8cSDaniel Latypov 			   kunit_resource_match_t match,
36961695f8cSDaniel Latypov 			   void *match_data);
37061695f8cSDaniel Latypov 
kunit_destroy_named_resource(struct kunit * test,const char * name)37161695f8cSDaniel Latypov static inline int kunit_destroy_named_resource(struct kunit *test,
37261695f8cSDaniel Latypov 					       const char *name)
37361695f8cSDaniel Latypov {
37461695f8cSDaniel Latypov 	return kunit_destroy_resource(test, kunit_resource_name_match,
37561695f8cSDaniel Latypov 				      (void *)name);
37661695f8cSDaniel Latypov }
37761695f8cSDaniel Latypov 
37861695f8cSDaniel Latypov /**
37961695f8cSDaniel Latypov  * kunit_remove_resource() - remove resource from resource list associated with
38061695f8cSDaniel Latypov  *			     test.
38161695f8cSDaniel Latypov  * @test: The test context object.
38261695f8cSDaniel Latypov  * @res: The resource to be removed.
38361695f8cSDaniel Latypov  *
38461695f8cSDaniel Latypov  * Note that the resource will not be immediately freed since it is likely
38561695f8cSDaniel Latypov  * the caller has a reference to it via alloc_and_get() or find();
38661695f8cSDaniel Latypov  * in this case a final call to kunit_put_resource() is required.
38761695f8cSDaniel Latypov  */
38861695f8cSDaniel Latypov void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
38961695f8cSDaniel Latypov 
39061695f8cSDaniel Latypov /* A 'deferred action' function to be used with kunit_add_action. */
391 typedef void (kunit_action_t)(void *);
392 
393 /**
394  * kunit_add_action() - Call a function when the test ends.
395  * @test: Test case to associate the action with.
396  * @action: The function to run on test exit
397  * @ctx: Data passed into @func
398  *
399  * Defer the execution of a function until the test exits, either normally or
400  * due to a failure.  @ctx is passed as additional context. All functions
401  * registered with kunit_add_action() will execute in the opposite order to that
402  * they were registered in.
403  *
404  * This is useful for cleaning up allocated memory and resources, as these
405  * functions are called even if the test aborts early due to, e.g., a failed
406  * assertion.
407  *
408  * See also: devm_add_action() for the devres equivalent.
409  *
410  * Returns:
411  *   0 on success, an error if the action could not be deferred.
412  */
413 int kunit_add_action(struct kunit *test, kunit_action_t *action, void *ctx);
414 
415 /**
416  * kunit_add_action_or_reset() - Call a function when the test ends.
417  * @test: Test case to associate the action with.
418  * @action: The function to run on test exit
419  * @ctx: Data passed into @func
420  *
421  * Defer the execution of a function until the test exits, either normally or
422  * due to a failure.  @ctx is passed as additional context. All functions
423  * registered with kunit_add_action() will execute in the opposite order to that
424  * they were registered in.
425  *
426  * This is useful for cleaning up allocated memory and resources, as these
427  * functions are called even if the test aborts early due to, e.g., a failed
428  * assertion.
429  *
430  * If the action cannot be created (e.g., due to the system being out of memory),
431  * then action(ctx) will be called immediately, and an error will be returned.
432  *
433  * See also: devm_add_action_or_reset() for the devres equivalent.
434  *
435  * Returns:
436  *   0 on success, an error if the action could not be deferred.
437  */
438 int kunit_add_action_or_reset(struct kunit *test, kunit_action_t *action,
439 			      void *ctx);
440 
441 /**
442  * kunit_remove_action() - Cancel a matching deferred action.
443  * @test: Test case the action is associated with.
444  * @action: The deferred function to cancel.
445  * @ctx: The context passed to the deferred function to trigger.
446  *
447  * Prevent an action deferred via kunit_add_action() from executing when the
448  * test terminates.
449  *
450  * If the function/context pair was deferred multiple times, only the most
451  * recent one will be cancelled.
452  *
453  * See also: devm_remove_action() for the devres equivalent.
454  */
455 void kunit_remove_action(struct kunit *test,
456 			 kunit_action_t *action,
457 			 void *ctx);
458 
459 /**
460  * kunit_release_action() - Run a matching action call immediately.
461  * @test: Test case the action is associated with.
462  * @action: The deferred function to trigger.
463  * @ctx: The context passed to the deferred function to trigger.
464  *
465  * Execute a function deferred via kunit_add_action()) immediately, rather than
466  * when the test ends.
467  *
468  * If the function/context pair was deferred multiple times, it will only be
469  * executed once here. The most recent deferral will no longer execute when
470  * the test ends.
471  *
472  * kunit_release_action(test, func, ctx);
473  * is equivalent to
474  * func(ctx);
475  * kunit_remove_action(test, func, ctx);
476  *
477  * See also: devm_release_action() for the devres equivalent.
478  */
479 void kunit_release_action(struct kunit *test,
480 			  kunit_action_t *action,
481 			  void *ctx);
482 #endif /* _KUNIT_RESOURCE_H */
483