~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/kunit/resource.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/kunit/resource.c (Version linux-6.12-rc7) and /lib/kunit/resource.c (Version linux-6.2.16)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * KUnit resource API for test managed resourc      3  * KUnit resource API for test managed resources (allocations, etc.).
  4  *                                                  4  *
  5  * Copyright (C) 2022, Google LLC.                  5  * Copyright (C) 2022, Google LLC.
  6  * Author: Daniel Latypov <dlatypov@google.com      6  * Author: Daniel Latypov <dlatypov@google.com>
  7  */                                                 7  */
  8                                                     8 
  9 #include <kunit/resource.h>                         9 #include <kunit/resource.h>
 10 #include <kunit/test.h>                            10 #include <kunit/test.h>
 11 #include <linux/kref.h>                            11 #include <linux/kref.h>
 12                                                    12 
 13 /*                                                 13 /*
 14  * Used for static resources and when a kunit_     14  * Used for static resources and when a kunit_resource * has been created by
 15  * kunit_alloc_resource().  When an init funct     15  * kunit_alloc_resource().  When an init function is supplied, @data is passed
 16  * into the init function; otherwise, we simpl     16  * into the init function; otherwise, we simply set the resource data field to
 17  * the data value passed in. Doesn't initializ     17  * the data value passed in. Doesn't initialize res->should_kfree.
 18  */                                                18  */
 19 int __kunit_add_resource(struct kunit *test,       19 int __kunit_add_resource(struct kunit *test,
 20                          kunit_resource_init_t     20                          kunit_resource_init_t init,
 21                          kunit_resource_free_t     21                          kunit_resource_free_t free,
 22                          struct kunit_resource     22                          struct kunit_resource *res,
 23                          void *data)               23                          void *data)
 24 {                                                  24 {
 25         int ret = 0;                               25         int ret = 0;
 26         unsigned long flags;                       26         unsigned long flags;
 27                                                    27 
 28         res->free = free;                          28         res->free = free;
 29         kref_init(&res->refcount);                 29         kref_init(&res->refcount);
 30                                                    30 
 31         if (init) {                                31         if (init) {
 32                 ret = init(res, data);             32                 ret = init(res, data);
 33                 if (ret)                           33                 if (ret)
 34                         return ret;                34                         return ret;
 35         } else {                                   35         } else {
 36                 res->data = data;                  36                 res->data = data;
 37         }                                          37         }
 38                                                    38 
 39         spin_lock_irqsave(&test->lock, flags);     39         spin_lock_irqsave(&test->lock, flags);
 40         list_add_tail(&res->node, &test->resou     40         list_add_tail(&res->node, &test->resources);
 41         /* refcount for list is established by     41         /* refcount for list is established by kref_init() */
 42         spin_unlock_irqrestore(&test->lock, fl     42         spin_unlock_irqrestore(&test->lock, flags);
 43                                                    43 
 44         return ret;                                44         return ret;
 45 }                                                  45 }
 46 EXPORT_SYMBOL_GPL(__kunit_add_resource);           46 EXPORT_SYMBOL_GPL(__kunit_add_resource);
 47                                                    47 
 48 void kunit_remove_resource(struct kunit *test,     48 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
 49 {                                                  49 {
 50         unsigned long flags;                       50         unsigned long flags;
 51         bool was_linked;                           51         bool was_linked;
 52                                                    52 
 53         spin_lock_irqsave(&test->lock, flags);     53         spin_lock_irqsave(&test->lock, flags);
 54         was_linked = !list_empty(&res->node);      54         was_linked = !list_empty(&res->node);
 55         list_del_init(&res->node);                 55         list_del_init(&res->node);
 56         spin_unlock_irqrestore(&test->lock, fl     56         spin_unlock_irqrestore(&test->lock, flags);
 57                                                    57 
 58         if (was_linked)                            58         if (was_linked)
 59                 kunit_put_resource(res);           59                 kunit_put_resource(res);
 60 }                                                  60 }
 61 EXPORT_SYMBOL_GPL(kunit_remove_resource);          61 EXPORT_SYMBOL_GPL(kunit_remove_resource);
 62                                                    62 
 63 int kunit_destroy_resource(struct kunit *test,     63 int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
 64                            void *match_data)       64                            void *match_data)
 65 {                                                  65 {
 66         struct kunit_resource *res = kunit_fin     66         struct kunit_resource *res = kunit_find_resource(test, match,
 67                                                    67                                                          match_data);
 68                                                    68 
 69         if (!res)                                  69         if (!res)
 70                 return -ENOENT;                    70                 return -ENOENT;
 71                                                    71 
 72         kunit_remove_resource(test, res);          72         kunit_remove_resource(test, res);
 73                                                    73 
 74         /* We have a reference also via _find(     74         /* We have a reference also via _find(); drop it. */
 75         kunit_put_resource(res);                   75         kunit_put_resource(res);
 76                                                    76 
 77         return 0;                                  77         return 0;
 78 }                                                  78 }
 79 EXPORT_SYMBOL_GPL(kunit_destroy_resource);         79 EXPORT_SYMBOL_GPL(kunit_destroy_resource);
 80                                                << 
 81 struct kunit_action_ctx {                      << 
 82         struct kunit_resource res;             << 
 83         kunit_action_t *func;                  << 
 84         void *ctx;                             << 
 85 };                                             << 
 86                                                << 
 87 static void __kunit_action_free(struct kunit_r << 
 88 {                                              << 
 89         struct kunit_action_ctx *action_ctx =  << 
 90                                                << 
 91         action_ctx->func(action_ctx->ctx);     << 
 92 }                                              << 
 93                                                << 
 94                                                << 
 95 int kunit_add_action(struct kunit *test, void  << 
 96 {                                              << 
 97         struct kunit_action_ctx *action_ctx;   << 
 98                                                << 
 99         KUNIT_ASSERT_NOT_NULL_MSG(test, action << 
100                                                << 
101         action_ctx = kzalloc(sizeof(*action_ct << 
102         if (!action_ctx)                       << 
103                 return -ENOMEM;                << 
104                                                << 
105         action_ctx->func = action;             << 
106         action_ctx->ctx = ctx;                 << 
107                                                << 
108         action_ctx->res.should_kfree = true;   << 
109         /* As init is NULL, this cannot fail.  << 
110         __kunit_add_resource(test, NULL, __kun << 
111                                                << 
112         return 0;                              << 
113 }                                              << 
114 EXPORT_SYMBOL_GPL(kunit_add_action);           << 
115                                                << 
116 int kunit_add_action_or_reset(struct kunit *te << 
117                               void *ctx)       << 
118 {                                              << 
119         int res = kunit_add_action(test, actio << 
120                                                << 
121         if (res)                               << 
122                 action(ctx);                   << 
123         return res;                            << 
124 }                                              << 
125 EXPORT_SYMBOL_GPL(kunit_add_action_or_reset);  << 
126                                                << 
127 static bool __kunit_action_match(struct kunit  << 
128                                 struct kunit_r << 
129 {                                              << 
130         struct kunit_action_ctx *match_ctx = ( << 
131         struct kunit_action_ctx *res_ctx = con << 
132                                                << 
133         /* Make sure this is a free function.  << 
134         if (res->free != __kunit_action_free)  << 
135                 return false;                  << 
136                                                << 
137         /* Both the function and context data  << 
138         return (match_ctx->func == res_ctx->fu << 
139 }                                              << 
140                                                << 
141 void kunit_remove_action(struct kunit *test,   << 
142                         kunit_action_t *action << 
143                         void *ctx)             << 
144 {                                              << 
145         struct kunit_action_ctx match_ctx;     << 
146         struct kunit_resource *res;            << 
147                                                << 
148         match_ctx.func = action;               << 
149         match_ctx.ctx = ctx;                   << 
150                                                << 
151         res = kunit_find_resource(test, __kuni << 
152         if (res) {                             << 
153                 /* Remove the free function so << 
154                 res->free = NULL;              << 
155                 kunit_remove_resource(test, re << 
156                 kunit_put_resource(res);       << 
157         }                                      << 
158 }                                              << 
159 EXPORT_SYMBOL_GPL(kunit_remove_action);        << 
160                                                << 
161 void kunit_release_action(struct kunit *test,  << 
162                          kunit_action_t *actio << 
163                          void *ctx)            << 
164 {                                              << 
165         struct kunit_action_ctx match_ctx;     << 
166         struct kunit_resource *res;            << 
167                                                << 
168         match_ctx.func = action;               << 
169         match_ctx.ctx = ctx;                   << 
170                                                << 
171         res = kunit_find_resource(test, __kuni << 
172         if (res) {                             << 
173                 kunit_remove_resource(test, re << 
174                 /* We have to put() this here, << 
175                 kunit_put_resource(res);       << 
176         }                                      << 
177 }                                              << 
178 EXPORT_SYMBOL_GPL(kunit_release_action);       << 
179                                                    80 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php