Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 2 | /* |
| 3 | * drivers/base/devres.c - device resource management |
| 4 | * |
| 5 | * Copyright (c) 2006 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2006 Tejun Heo <teheo@suse.de> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Madalin Bucur | ff86aae | 2016-11-15 10:41:01 +0200 | [diff] [blame] | 14 | #include <linux/percpu.h> |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 15 | |
Adrian Bunk | 2a01345 | 2007-06-18 01:42:54 +0200 | [diff] [blame] | 16 | #include "base.h" |
| 17 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 18 | struct devres_node { |
| 19 | struct list_head entry; |
| 20 | dr_release_t release; |
| 21 | #ifdef CONFIG_DEBUG_DEVRES |
| 22 | const char *name; |
| 23 | size_t size; |
| 24 | #endif |
| 25 | }; |
| 26 | |
| 27 | struct devres { |
| 28 | struct devres_node node; |
| 29 | /* -- 3 pointers */ |
| 30 | unsigned long long data[]; /* guarantee ull alignment */ |
| 31 | }; |
| 32 | |
| 33 | struct devres_group { |
| 34 | struct devres_node node[2]; |
| 35 | void *id; |
| 36 | int color; |
| 37 | /* -- 8 pointers */ |
| 38 | }; |
| 39 | |
| 40 | #ifdef CONFIG_DEBUG_DEVRES |
| 41 | static int log_devres = 0; |
| 42 | module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR); |
| 43 | |
| 44 | static void set_node_dbginfo(struct devres_node *node, const char *name, |
| 45 | size_t size) |
| 46 | { |
| 47 | node->name = name; |
| 48 | node->size = size; |
| 49 | } |
| 50 | |
| 51 | static void devres_log(struct device *dev, struct devres_node *node, |
| 52 | const char *op) |
| 53 | { |
| 54 | if (unlikely(log_devres)) |
Joe Perches | a369a7e | 2012-10-28 01:05:41 -0700 | [diff] [blame] | 55 | dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n", |
| 56 | op, node, node->name, (unsigned long)node->size); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 57 | } |
| 58 | #else /* CONFIG_DEBUG_DEVRES */ |
| 59 | #define set_node_dbginfo(node, n, s) do {} while (0) |
| 60 | #define devres_log(dev, node, op) do {} while (0) |
| 61 | #endif /* CONFIG_DEBUG_DEVRES */ |
| 62 | |
| 63 | /* |
| 64 | * Release functions for devres group. These callbacks are used only |
| 65 | * for identification. |
| 66 | */ |
| 67 | static void group_open_release(struct device *dev, void *res) |
| 68 | { |
| 69 | /* noop */ |
| 70 | } |
| 71 | |
| 72 | static void group_close_release(struct device *dev, void *res) |
| 73 | { |
| 74 | /* noop */ |
| 75 | } |
| 76 | |
| 77 | static struct devres_group * node_to_group(struct devres_node *node) |
| 78 | { |
| 79 | if (node->release == &group_open_release) |
| 80 | return container_of(node, struct devres_group, node[0]); |
| 81 | if (node->release == &group_close_release) |
| 82 | return container_of(node, struct devres_group, node[1]); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | static __always_inline struct devres * alloc_dr(dr_release_t release, |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 87 | size_t size, gfp_t gfp, int nid) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 88 | { |
| 89 | size_t tot_size = sizeof(struct devres) + size; |
| 90 | struct devres *dr; |
| 91 | |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 92 | dr = kmalloc_node_track_caller(tot_size, gfp, nid); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 93 | if (unlikely(!dr)) |
| 94 | return NULL; |
| 95 | |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 96 | memset(dr, 0, offsetof(struct devres, data)); |
| 97 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 98 | INIT_LIST_HEAD(&dr->node.entry); |
| 99 | dr->node.release = release; |
| 100 | return dr; |
| 101 | } |
| 102 | |
| 103 | static void add_dr(struct device *dev, struct devres_node *node) |
| 104 | { |
| 105 | devres_log(dev, node, "ADD"); |
| 106 | BUG_ON(!list_empty(&node->entry)); |
| 107 | list_add_tail(&node->entry, &dev->devres_head); |
| 108 | } |
| 109 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 110 | #ifdef CONFIG_DEBUG_DEVRES |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 111 | void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 112 | const char *name) |
| 113 | { |
| 114 | struct devres *dr; |
| 115 | |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 116 | dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 117 | if (unlikely(!dr)) |
| 118 | return NULL; |
| 119 | set_node_dbginfo(&dr->node, name, size); |
| 120 | return dr->data; |
| 121 | } |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(__devres_alloc_node); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 123 | #else |
Randy Dunlap | d3e6975 | 2007-05-09 07:02:59 +0200 | [diff] [blame] | 124 | /** |
| 125 | * devres_alloc - Allocate device resource data |
| 126 | * @release: Release function devres will be associated with |
| 127 | * @size: Allocation size |
| 128 | * @gfp: Allocation flags |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 129 | * @nid: NUMA node |
Randy Dunlap | d3e6975 | 2007-05-09 07:02:59 +0200 | [diff] [blame] | 130 | * |
| 131 | * Allocate devres of @size bytes. The allocated area is zeroed, then |
| 132 | * associated with @release. The returned pointer can be passed to |
| 133 | * other devres_*() functions. |
| 134 | * |
| 135 | * RETURNS: |
| 136 | * Pointer to allocated devres on success, NULL on failure. |
| 137 | */ |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 138 | void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 139 | { |
| 140 | struct devres *dr; |
| 141 | |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 142 | dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 143 | if (unlikely(!dr)) |
| 144 | return NULL; |
| 145 | return dr->data; |
| 146 | } |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 147 | EXPORT_SYMBOL_GPL(devres_alloc_node); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | /** |
Ming Lei | bddb1b9 | 2012-08-04 12:01:26 +0800 | [diff] [blame] | 151 | * devres_for_each_res - Resource iterator |
| 152 | * @dev: Device to iterate resource from |
| 153 | * @release: Look for resources associated with this release function |
| 154 | * @match: Match function (optional) |
| 155 | * @match_data: Data for the match function |
| 156 | * @fn: Function to be called for each matched resource. |
| 157 | * @data: Data for @fn, the 3rd parameter of @fn |
| 158 | * |
| 159 | * Call @fn for each devres of @dev which is associated with @release |
| 160 | * and for which @match returns 1. |
| 161 | * |
| 162 | * RETURNS: |
| 163 | * void |
| 164 | */ |
| 165 | void devres_for_each_res(struct device *dev, dr_release_t release, |
| 166 | dr_match_t match, void *match_data, |
| 167 | void (*fn)(struct device *, void *, void *), |
| 168 | void *data) |
| 169 | { |
| 170 | struct devres_node *node; |
| 171 | struct devres_node *tmp; |
| 172 | unsigned long flags; |
| 173 | |
| 174 | if (!fn) |
| 175 | return; |
| 176 | |
| 177 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 178 | list_for_each_entry_safe_reverse(node, tmp, |
| 179 | &dev->devres_head, entry) { |
| 180 | struct devres *dr = container_of(node, struct devres, node); |
| 181 | |
| 182 | if (node->release != release) |
| 183 | continue; |
| 184 | if (match && !match(dev, dr->data, match_data)) |
| 185 | continue; |
| 186 | fn(dev, dr->data, data); |
| 187 | } |
| 188 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(devres_for_each_res); |
| 191 | |
| 192 | /** |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 193 | * devres_free - Free device resource data |
| 194 | * @res: Pointer to devres data to free |
| 195 | * |
| 196 | * Free devres created with devres_alloc(). |
| 197 | */ |
| 198 | void devres_free(void *res) |
| 199 | { |
| 200 | if (res) { |
| 201 | struct devres *dr = container_of(res, struct devres, data); |
| 202 | |
| 203 | BUG_ON(!list_empty(&dr->node.entry)); |
| 204 | kfree(dr); |
| 205 | } |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(devres_free); |
| 208 | |
| 209 | /** |
| 210 | * devres_add - Register device resource |
| 211 | * @dev: Device to add resource to |
| 212 | * @res: Resource to register |
| 213 | * |
| 214 | * Register devres @res to @dev. @res should have been allocated |
| 215 | * using devres_alloc(). On driver detach, the associated release |
| 216 | * function will be invoked and devres will be freed automatically. |
| 217 | */ |
| 218 | void devres_add(struct device *dev, void *res) |
| 219 | { |
| 220 | struct devres *dr = container_of(res, struct devres, data); |
| 221 | unsigned long flags; |
| 222 | |
| 223 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 224 | add_dr(dev, &dr->node); |
| 225 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(devres_add); |
| 228 | |
| 229 | static struct devres *find_dr(struct device *dev, dr_release_t release, |
| 230 | dr_match_t match, void *match_data) |
| 231 | { |
| 232 | struct devres_node *node; |
| 233 | |
| 234 | list_for_each_entry_reverse(node, &dev->devres_head, entry) { |
| 235 | struct devres *dr = container_of(node, struct devres, node); |
| 236 | |
| 237 | if (node->release != release) |
| 238 | continue; |
| 239 | if (match && !match(dev, dr->data, match_data)) |
| 240 | continue; |
| 241 | return dr; |
| 242 | } |
| 243 | |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * devres_find - Find device resource |
| 249 | * @dev: Device to lookup resource from |
| 250 | * @release: Look for resources associated with this release function |
| 251 | * @match: Match function (optional) |
| 252 | * @match_data: Data for the match function |
| 253 | * |
| 254 | * Find the latest devres of @dev which is associated with @release |
| 255 | * and for which @match returns 1. If @match is NULL, it's considered |
| 256 | * to match all. |
| 257 | * |
| 258 | * RETURNS: |
| 259 | * Pointer to found devres, NULL if not found. |
| 260 | */ |
| 261 | void * devres_find(struct device *dev, dr_release_t release, |
| 262 | dr_match_t match, void *match_data) |
| 263 | { |
| 264 | struct devres *dr; |
| 265 | unsigned long flags; |
| 266 | |
| 267 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 268 | dr = find_dr(dev, release, match, match_data); |
| 269 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 270 | |
| 271 | if (dr) |
| 272 | return dr->data; |
| 273 | return NULL; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(devres_find); |
| 276 | |
| 277 | /** |
| 278 | * devres_get - Find devres, if non-existent, add one atomically |
| 279 | * @dev: Device to lookup or add devres for |
| 280 | * @new_res: Pointer to new initialized devres to add if not found |
| 281 | * @match: Match function (optional) |
| 282 | * @match_data: Data for the match function |
| 283 | * |
| 284 | * Find the latest devres of @dev which has the same release function |
| 285 | * as @new_res and for which @match return 1. If found, @new_res is |
| 286 | * freed; otherwise, @new_res is added atomically. |
| 287 | * |
| 288 | * RETURNS: |
| 289 | * Pointer to found or added devres. |
| 290 | */ |
| 291 | void * devres_get(struct device *dev, void *new_res, |
| 292 | dr_match_t match, void *match_data) |
| 293 | { |
| 294 | struct devres *new_dr = container_of(new_res, struct devres, data); |
| 295 | struct devres *dr; |
| 296 | unsigned long flags; |
| 297 | |
| 298 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 299 | dr = find_dr(dev, new_dr->node.release, match, match_data); |
| 300 | if (!dr) { |
| 301 | add_dr(dev, &new_dr->node); |
| 302 | dr = new_dr; |
Masahiro Yamada | 6452637 | 2015-07-15 10:29:00 +0900 | [diff] [blame] | 303 | new_res = NULL; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 304 | } |
| 305 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
Masahiro Yamada | 6452637 | 2015-07-15 10:29:00 +0900 | [diff] [blame] | 306 | devres_free(new_res); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 307 | |
| 308 | return dr->data; |
| 309 | } |
| 310 | EXPORT_SYMBOL_GPL(devres_get); |
| 311 | |
| 312 | /** |
| 313 | * devres_remove - Find a device resource and remove it |
| 314 | * @dev: Device to find resource from |
| 315 | * @release: Look for resources associated with this release function |
| 316 | * @match: Match function (optional) |
| 317 | * @match_data: Data for the match function |
| 318 | * |
| 319 | * Find the latest devres of @dev associated with @release and for |
| 320 | * which @match returns 1. If @match is NULL, it's considered to |
| 321 | * match all. If found, the resource is removed atomically and |
| 322 | * returned. |
| 323 | * |
| 324 | * RETURNS: |
| 325 | * Pointer to removed devres on success, NULL if not found. |
| 326 | */ |
| 327 | void * devres_remove(struct device *dev, dr_release_t release, |
| 328 | dr_match_t match, void *match_data) |
| 329 | { |
| 330 | struct devres *dr; |
| 331 | unsigned long flags; |
| 332 | |
| 333 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 334 | dr = find_dr(dev, release, match, match_data); |
| 335 | if (dr) { |
| 336 | list_del_init(&dr->node.entry); |
| 337 | devres_log(dev, &dr->node, "REM"); |
| 338 | } |
| 339 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 340 | |
| 341 | if (dr) |
| 342 | return dr->data; |
| 343 | return NULL; |
| 344 | } |
| 345 | EXPORT_SYMBOL_GPL(devres_remove); |
| 346 | |
| 347 | /** |
| 348 | * devres_destroy - Find a device resource and destroy it |
| 349 | * @dev: Device to find resource from |
| 350 | * @release: Look for resources associated with this release function |
| 351 | * @match: Match function (optional) |
| 352 | * @match_data: Data for the match function |
| 353 | * |
| 354 | * Find the latest devres of @dev associated with @release and for |
| 355 | * which @match returns 1. If @match is NULL, it's considered to |
| 356 | * match all. If found, the resource is removed atomically and freed. |
| 357 | * |
Mark Brown | 698cd2d | 2012-05-03 18:15:12 +0100 | [diff] [blame] | 358 | * Note that the release function for the resource will not be called, |
| 359 | * only the devres-allocated data will be freed. The caller becomes |
| 360 | * responsible for freeing any other data. |
| 361 | * |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 362 | * RETURNS: |
| 363 | * 0 if devres is found and freed, -ENOENT if not found. |
| 364 | */ |
| 365 | int devres_destroy(struct device *dev, dr_release_t release, |
| 366 | dr_match_t match, void *match_data) |
| 367 | { |
| 368 | void *res; |
| 369 | |
| 370 | res = devres_remove(dev, release, match, match_data); |
| 371 | if (unlikely(!res)) |
| 372 | return -ENOENT; |
| 373 | |
| 374 | devres_free(res); |
| 375 | return 0; |
| 376 | } |
| 377 | EXPORT_SYMBOL_GPL(devres_destroy); |
| 378 | |
Mark Brown | d926d0e | 2012-05-03 18:15:13 +0100 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * devres_release - Find a device resource and destroy it, calling release |
| 382 | * @dev: Device to find resource from |
| 383 | * @release: Look for resources associated with this release function |
| 384 | * @match: Match function (optional) |
| 385 | * @match_data: Data for the match function |
| 386 | * |
| 387 | * Find the latest devres of @dev associated with @release and for |
| 388 | * which @match returns 1. If @match is NULL, it's considered to |
| 389 | * match all. If found, the resource is removed atomically, the |
| 390 | * release function called and the resource freed. |
| 391 | * |
| 392 | * RETURNS: |
| 393 | * 0 if devres is found and freed, -ENOENT if not found. |
| 394 | */ |
| 395 | int devres_release(struct device *dev, dr_release_t release, |
| 396 | dr_match_t match, void *match_data) |
| 397 | { |
| 398 | void *res; |
| 399 | |
| 400 | res = devres_remove(dev, release, match, match_data); |
| 401 | if (unlikely(!res)) |
| 402 | return -ENOENT; |
| 403 | |
| 404 | (*release)(dev, res); |
| 405 | devres_free(res); |
| 406 | return 0; |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(devres_release); |
| 409 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 410 | static int remove_nodes(struct device *dev, |
| 411 | struct list_head *first, struct list_head *end, |
| 412 | struct list_head *todo) |
| 413 | { |
| 414 | int cnt = 0, nr_groups = 0; |
| 415 | struct list_head *cur; |
| 416 | |
| 417 | /* First pass - move normal devres entries to @todo and clear |
| 418 | * devres_group colors. |
| 419 | */ |
| 420 | cur = first; |
| 421 | while (cur != end) { |
| 422 | struct devres_node *node; |
| 423 | struct devres_group *grp; |
| 424 | |
| 425 | node = list_entry(cur, struct devres_node, entry); |
| 426 | cur = cur->next; |
| 427 | |
| 428 | grp = node_to_group(node); |
| 429 | if (grp) { |
| 430 | /* clear color of group markers in the first pass */ |
| 431 | grp->color = 0; |
| 432 | nr_groups++; |
| 433 | } else { |
| 434 | /* regular devres entry */ |
| 435 | if (&node->entry == first) |
| 436 | first = first->next; |
| 437 | list_move_tail(&node->entry, todo); |
| 438 | cnt++; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (!nr_groups) |
| 443 | return cnt; |
| 444 | |
| 445 | /* Second pass - Scan groups and color them. A group gets |
| 446 | * color value of two iff the group is wholly contained in |
| 447 | * [cur, end). That is, for a closed group, both opening and |
| 448 | * closing markers should be in the range, while just the |
| 449 | * opening marker is enough for an open group. |
| 450 | */ |
| 451 | cur = first; |
| 452 | while (cur != end) { |
| 453 | struct devres_node *node; |
| 454 | struct devres_group *grp; |
| 455 | |
| 456 | node = list_entry(cur, struct devres_node, entry); |
| 457 | cur = cur->next; |
| 458 | |
| 459 | grp = node_to_group(node); |
| 460 | BUG_ON(!grp || list_empty(&grp->node[0].entry)); |
| 461 | |
| 462 | grp->color++; |
| 463 | if (list_empty(&grp->node[1].entry)) |
| 464 | grp->color++; |
| 465 | |
| 466 | BUG_ON(grp->color <= 0 || grp->color > 2); |
| 467 | if (grp->color == 2) { |
| 468 | /* No need to update cur or end. The removed |
| 469 | * nodes are always before both. |
| 470 | */ |
| 471 | list_move_tail(&grp->node[0].entry, todo); |
| 472 | list_del_init(&grp->node[1].entry); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return cnt; |
| 477 | } |
| 478 | |
| 479 | static int release_nodes(struct device *dev, struct list_head *first, |
| 480 | struct list_head *end, unsigned long flags) |
H Hartley Sweeten | 86ec67f | 2011-08-12 14:58:31 -0700 | [diff] [blame] | 481 | __releases(&dev->devres_lock) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 482 | { |
| 483 | LIST_HEAD(todo); |
| 484 | int cnt; |
| 485 | struct devres *dr, *tmp; |
| 486 | |
| 487 | cnt = remove_nodes(dev, first, end, &todo); |
| 488 | |
| 489 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 490 | |
| 491 | /* Release. Note that both devres and devres_group are |
| 492 | * handled as devres in the following loop. This is safe. |
| 493 | */ |
| 494 | list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) { |
| 495 | devres_log(dev, &dr->node, "REL"); |
| 496 | dr->node.release(dev, dr->data); |
| 497 | kfree(dr); |
| 498 | } |
| 499 | |
| 500 | return cnt; |
| 501 | } |
| 502 | |
| 503 | /** |
Randy Dunlap | d3e6975 | 2007-05-09 07:02:59 +0200 | [diff] [blame] | 504 | * devres_release_all - Release all managed resources |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 505 | * @dev: Device to release resources for |
| 506 | * |
| 507 | * Release all resources associated with @dev. This function is |
| 508 | * called on driver detach. |
| 509 | */ |
| 510 | int devres_release_all(struct device *dev) |
| 511 | { |
| 512 | unsigned long flags; |
| 513 | |
Benjamin Herrenschmidt | eb8d3c6 | 2009-06-10 12:43:02 -0700 | [diff] [blame] | 514 | /* Looks like an uninitialized device structure */ |
| 515 | if (WARN_ON(dev->devres_head.next == NULL)) |
| 516 | return -ENODEV; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 517 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 518 | return release_nodes(dev, dev->devres_head.next, &dev->devres_head, |
| 519 | flags); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * devres_open_group - Open a new devres group |
| 524 | * @dev: Device to open devres group for |
| 525 | * @id: Separator ID |
| 526 | * @gfp: Allocation flags |
| 527 | * |
| 528 | * Open a new devres group for @dev with @id. For @id, using a |
| 529 | * pointer to an object which won't be used for another group is |
| 530 | * recommended. If @id is NULL, address-wise unique ID is created. |
| 531 | * |
| 532 | * RETURNS: |
| 533 | * ID of the new group, NULL on failure. |
| 534 | */ |
| 535 | void * devres_open_group(struct device *dev, void *id, gfp_t gfp) |
| 536 | { |
| 537 | struct devres_group *grp; |
| 538 | unsigned long flags; |
| 539 | |
| 540 | grp = kmalloc(sizeof(*grp), gfp); |
| 541 | if (unlikely(!grp)) |
| 542 | return NULL; |
| 543 | |
| 544 | grp->node[0].release = &group_open_release; |
| 545 | grp->node[1].release = &group_close_release; |
| 546 | INIT_LIST_HEAD(&grp->node[0].entry); |
| 547 | INIT_LIST_HEAD(&grp->node[1].entry); |
| 548 | set_node_dbginfo(&grp->node[0], "grp<", 0); |
| 549 | set_node_dbginfo(&grp->node[1], "grp>", 0); |
| 550 | grp->id = grp; |
| 551 | if (id) |
| 552 | grp->id = id; |
| 553 | |
| 554 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 555 | add_dr(dev, &grp->node[0]); |
| 556 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 557 | return grp->id; |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(devres_open_group); |
| 560 | |
| 561 | /* Find devres group with ID @id. If @id is NULL, look for the latest. */ |
| 562 | static struct devres_group * find_group(struct device *dev, void *id) |
| 563 | { |
| 564 | struct devres_node *node; |
| 565 | |
| 566 | list_for_each_entry_reverse(node, &dev->devres_head, entry) { |
| 567 | struct devres_group *grp; |
| 568 | |
| 569 | if (node->release != &group_open_release) |
| 570 | continue; |
| 571 | |
| 572 | grp = container_of(node, struct devres_group, node[0]); |
| 573 | |
| 574 | if (id) { |
| 575 | if (grp->id == id) |
| 576 | return grp; |
| 577 | } else if (list_empty(&grp->node[1].entry)) |
| 578 | return grp; |
| 579 | } |
| 580 | |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * devres_close_group - Close a devres group |
| 586 | * @dev: Device to close devres group for |
| 587 | * @id: ID of target group, can be NULL |
| 588 | * |
| 589 | * Close the group identified by @id. If @id is NULL, the latest open |
| 590 | * group is selected. |
| 591 | */ |
| 592 | void devres_close_group(struct device *dev, void *id) |
| 593 | { |
| 594 | struct devres_group *grp; |
| 595 | unsigned long flags; |
| 596 | |
| 597 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 598 | |
| 599 | grp = find_group(dev, id); |
| 600 | if (grp) |
| 601 | add_dr(dev, &grp->node[1]); |
| 602 | else |
| 603 | WARN_ON(1); |
| 604 | |
| 605 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(devres_close_group); |
| 608 | |
| 609 | /** |
| 610 | * devres_remove_group - Remove a devres group |
| 611 | * @dev: Device to remove group for |
| 612 | * @id: ID of target group, can be NULL |
| 613 | * |
| 614 | * Remove the group identified by @id. If @id is NULL, the latest |
| 615 | * open group is selected. Note that removing a group doesn't affect |
| 616 | * any other resources. |
| 617 | */ |
| 618 | void devres_remove_group(struct device *dev, void *id) |
| 619 | { |
| 620 | struct devres_group *grp; |
| 621 | unsigned long flags; |
| 622 | |
| 623 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 624 | |
| 625 | grp = find_group(dev, id); |
| 626 | if (grp) { |
| 627 | list_del_init(&grp->node[0].entry); |
| 628 | list_del_init(&grp->node[1].entry); |
| 629 | devres_log(dev, &grp->node[0], "REM"); |
| 630 | } else |
| 631 | WARN_ON(1); |
| 632 | |
| 633 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 634 | |
| 635 | kfree(grp); |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(devres_remove_group); |
| 638 | |
| 639 | /** |
| 640 | * devres_release_group - Release resources in a devres group |
| 641 | * @dev: Device to release group for |
| 642 | * @id: ID of target group, can be NULL |
| 643 | * |
| 644 | * Release all resources in the group identified by @id. If @id is |
| 645 | * NULL, the latest open group is selected. The selected group and |
| 646 | * groups properly nested inside the selected group are removed. |
| 647 | * |
| 648 | * RETURNS: |
| 649 | * The number of released non-group resources. |
| 650 | */ |
| 651 | int devres_release_group(struct device *dev, void *id) |
| 652 | { |
| 653 | struct devres_group *grp; |
| 654 | unsigned long flags; |
| 655 | int cnt = 0; |
| 656 | |
| 657 | spin_lock_irqsave(&dev->devres_lock, flags); |
| 658 | |
| 659 | grp = find_group(dev, id); |
| 660 | if (grp) { |
| 661 | struct list_head *first = &grp->node[0].entry; |
| 662 | struct list_head *end = &dev->devres_head; |
| 663 | |
| 664 | if (!list_empty(&grp->node[1].entry)) |
| 665 | end = grp->node[1].entry.next; |
| 666 | |
| 667 | cnt = release_nodes(dev, first, end, flags); |
| 668 | } else { |
| 669 | WARN_ON(1); |
| 670 | spin_unlock_irqrestore(&dev->devres_lock, flags); |
| 671 | } |
| 672 | |
| 673 | return cnt; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(devres_release_group); |
| 676 | |
| 677 | /* |
Dmitry Torokhov | d6b0c58 | 2013-02-23 13:11:14 -0800 | [diff] [blame] | 678 | * Custom devres actions allow inserting a simple function call |
| 679 | * into the teadown sequence. |
| 680 | */ |
| 681 | |
| 682 | struct action_devres { |
| 683 | void *data; |
| 684 | void (*action)(void *); |
| 685 | }; |
| 686 | |
| 687 | static int devm_action_match(struct device *dev, void *res, void *p) |
| 688 | { |
| 689 | struct action_devres *devres = res; |
| 690 | struct action_devres *target = p; |
| 691 | |
| 692 | return devres->action == target->action && |
| 693 | devres->data == target->data; |
| 694 | } |
| 695 | |
| 696 | static void devm_action_release(struct device *dev, void *res) |
| 697 | { |
| 698 | struct action_devres *devres = res; |
| 699 | |
| 700 | devres->action(devres->data); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * devm_add_action() - add a custom action to list of managed resources |
| 705 | * @dev: Device that owns the action |
| 706 | * @action: Function that should be called |
| 707 | * @data: Pointer to data passed to @action implementation |
| 708 | * |
| 709 | * This adds a custom action to the list of managed resources so that |
| 710 | * it gets executed as part of standard resource unwinding. |
| 711 | */ |
| 712 | int devm_add_action(struct device *dev, void (*action)(void *), void *data) |
| 713 | { |
| 714 | struct action_devres *devres; |
| 715 | |
| 716 | devres = devres_alloc(devm_action_release, |
| 717 | sizeof(struct action_devres), GFP_KERNEL); |
| 718 | if (!devres) |
| 719 | return -ENOMEM; |
| 720 | |
| 721 | devres->data = data; |
| 722 | devres->action = action; |
| 723 | |
| 724 | devres_add(dev, devres); |
| 725 | return 0; |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(devm_add_action); |
| 728 | |
| 729 | /** |
| 730 | * devm_remove_action() - removes previously added custom action |
| 731 | * @dev: Device that owns the action |
| 732 | * @action: Function implementing the action |
| 733 | * @data: Pointer to data passed to @action implementation |
| 734 | * |
| 735 | * Removes instance of @action previously added by devm_add_action(). |
| 736 | * Both action and data should match one of the existing entries. |
| 737 | */ |
| 738 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data) |
| 739 | { |
| 740 | struct action_devres devres = { |
| 741 | .data = data, |
| 742 | .action = action, |
| 743 | }; |
| 744 | |
| 745 | WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match, |
| 746 | &devres)); |
| 747 | |
| 748 | } |
| 749 | EXPORT_SYMBOL_GPL(devm_remove_action); |
| 750 | |
| 751 | /* |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 752 | * Managed kmalloc/kfree |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 753 | */ |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 754 | static void devm_kmalloc_release(struct device *dev, void *res) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 755 | { |
| 756 | /* noop */ |
| 757 | } |
| 758 | |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 759 | static int devm_kmalloc_match(struct device *dev, void *res, void *data) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 760 | { |
| 761 | return res == data; |
| 762 | } |
| 763 | |
| 764 | /** |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 765 | * devm_kmalloc - Resource-managed kmalloc |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 766 | * @dev: Device to allocate memory for |
| 767 | * @size: Allocation size |
| 768 | * @gfp: Allocation gfp flags |
| 769 | * |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 770 | * Managed kmalloc. Memory allocated with this function is |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 771 | * automatically freed on driver detach. Like all other devres |
| 772 | * resources, guaranteed alignment is unsigned long long. |
| 773 | * |
| 774 | * RETURNS: |
| 775 | * Pointer to allocated memory on success, NULL on failure. |
| 776 | */ |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 777 | void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 778 | { |
| 779 | struct devres *dr; |
| 780 | |
| 781 | /* use raw alloc_dr for kmalloc caller tracing */ |
Dan Williams | 7c68394 | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 782 | dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev)); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 783 | if (unlikely(!dr)) |
| 784 | return NULL; |
| 785 | |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 786 | /* |
| 787 | * This is named devm_kzalloc_release for historical reasons |
| 788 | * The initial implementation did not support kmalloc, only kzalloc |
| 789 | */ |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 790 | set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); |
| 791 | devres_add(dev, dr->data); |
| 792 | return dr->data; |
| 793 | } |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 794 | EXPORT_SYMBOL_GPL(devm_kmalloc); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 795 | |
| 796 | /** |
Manish Badarkhe | e31108c | 2014-01-29 20:27:27 +0530 | [diff] [blame] | 797 | * devm_kstrdup - Allocate resource managed space and |
| 798 | * copy an existing string into that. |
| 799 | * @dev: Device to allocate memory for |
| 800 | * @s: the string to duplicate |
| 801 | * @gfp: the GFP mask used in the devm_kmalloc() call when |
| 802 | * allocating memory |
| 803 | * RETURNS: |
| 804 | * Pointer to allocated string on success, NULL on failure. |
| 805 | */ |
| 806 | char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) |
| 807 | { |
| 808 | size_t size; |
| 809 | char *buf; |
| 810 | |
| 811 | if (!s) |
| 812 | return NULL; |
| 813 | |
| 814 | size = strlen(s) + 1; |
| 815 | buf = devm_kmalloc(dev, size, gfp); |
| 816 | if (buf) |
| 817 | memcpy(buf, s, size); |
| 818 | return buf; |
| 819 | } |
| 820 | EXPORT_SYMBOL_GPL(devm_kstrdup); |
| 821 | |
| 822 | /** |
Geert Uytterhoeven | bef59c5 | 2014-08-20 15:26:35 +0200 | [diff] [blame] | 823 | * devm_kvasprintf - Allocate resource managed space and format a string |
| 824 | * into that. |
Himangi Saraogi | 75f2a4e | 2014-07-17 02:27:52 +0530 | [diff] [blame] | 825 | * @dev: Device to allocate memory for |
| 826 | * @gfp: the GFP mask used in the devm_kmalloc() call when |
| 827 | * allocating memory |
Geert Uytterhoeven | bef59c5 | 2014-08-20 15:26:35 +0200 | [diff] [blame] | 828 | * @fmt: The printf()-style format string |
| 829 | * @ap: Arguments for the format string |
Himangi Saraogi | 75f2a4e | 2014-07-17 02:27:52 +0530 | [diff] [blame] | 830 | * RETURNS: |
| 831 | * Pointer to allocated string on success, NULL on failure. |
| 832 | */ |
| 833 | char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, |
| 834 | va_list ap) |
| 835 | { |
| 836 | unsigned int len; |
| 837 | char *p; |
| 838 | va_list aq; |
| 839 | |
| 840 | va_copy(aq, ap); |
| 841 | len = vsnprintf(NULL, 0, fmt, aq); |
| 842 | va_end(aq); |
| 843 | |
| 844 | p = devm_kmalloc(dev, len+1, gfp); |
| 845 | if (!p) |
| 846 | return NULL; |
| 847 | |
| 848 | vsnprintf(p, len+1, fmt, ap); |
| 849 | |
| 850 | return p; |
| 851 | } |
| 852 | EXPORT_SYMBOL(devm_kvasprintf); |
| 853 | |
| 854 | /** |
Geert Uytterhoeven | bef59c5 | 2014-08-20 15:26:35 +0200 | [diff] [blame] | 855 | * devm_kasprintf - Allocate resource managed space and format a string |
| 856 | * into that. |
Himangi Saraogi | 75f2a4e | 2014-07-17 02:27:52 +0530 | [diff] [blame] | 857 | * @dev: Device to allocate memory for |
| 858 | * @gfp: the GFP mask used in the devm_kmalloc() call when |
| 859 | * allocating memory |
Geert Uytterhoeven | bef59c5 | 2014-08-20 15:26:35 +0200 | [diff] [blame] | 860 | * @fmt: The printf()-style format string |
| 861 | * @...: Arguments for the format string |
Himangi Saraogi | 75f2a4e | 2014-07-17 02:27:52 +0530 | [diff] [blame] | 862 | * RETURNS: |
| 863 | * Pointer to allocated string on success, NULL on failure. |
| 864 | */ |
| 865 | char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) |
| 866 | { |
| 867 | va_list ap; |
| 868 | char *p; |
| 869 | |
| 870 | va_start(ap, fmt); |
| 871 | p = devm_kvasprintf(dev, gfp, fmt, ap); |
| 872 | va_end(ap); |
| 873 | |
| 874 | return p; |
| 875 | } |
| 876 | EXPORT_SYMBOL_GPL(devm_kasprintf); |
| 877 | |
| 878 | /** |
Randy Dunlap | d3e6975 | 2007-05-09 07:02:59 +0200 | [diff] [blame] | 879 | * devm_kfree - Resource-managed kfree |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 880 | * @dev: Device this memory belongs to |
| 881 | * @p: Memory to free |
| 882 | * |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 883 | * Free memory allocated with devm_kmalloc(). |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 884 | */ |
| 885 | void devm_kfree(struct device *dev, void *p) |
| 886 | { |
| 887 | int rc; |
| 888 | |
Joe Perches | 64c862a8 | 2013-10-11 13:11:38 -0700 | [diff] [blame] | 889 | rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 890 | WARN_ON(rc); |
| 891 | } |
| 892 | EXPORT_SYMBOL_GPL(devm_kfree); |
Srinivas Pandruvada | 3046365 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 893 | |
| 894 | /** |
| 895 | * devm_kmemdup - Resource-managed kmemdup |
| 896 | * @dev: Device this memory belongs to |
| 897 | * @src: Memory region to duplicate |
| 898 | * @len: Memory region length |
| 899 | * @gfp: GFP mask to use |
| 900 | * |
| 901 | * Duplicate region of a memory using resource managed kmalloc |
| 902 | */ |
| 903 | void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) |
| 904 | { |
| 905 | void *p; |
| 906 | |
| 907 | p = devm_kmalloc(dev, len, gfp); |
| 908 | if (p) |
| 909 | memcpy(p, src, len); |
| 910 | |
| 911 | return p; |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(devm_kmemdup); |
Eli Billauer | 43339be | 2014-05-16 11:26:35 +0300 | [diff] [blame] | 914 | |
| 915 | struct pages_devres { |
| 916 | unsigned long addr; |
| 917 | unsigned int order; |
| 918 | }; |
| 919 | |
| 920 | static int devm_pages_match(struct device *dev, void *res, void *p) |
| 921 | { |
| 922 | struct pages_devres *devres = res; |
| 923 | struct pages_devres *target = p; |
| 924 | |
| 925 | return devres->addr == target->addr; |
| 926 | } |
| 927 | |
| 928 | static void devm_pages_release(struct device *dev, void *res) |
| 929 | { |
| 930 | struct pages_devres *devres = res; |
| 931 | |
| 932 | free_pages(devres->addr, devres->order); |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * devm_get_free_pages - Resource-managed __get_free_pages |
| 937 | * @dev: Device to allocate memory for |
| 938 | * @gfp_mask: Allocation gfp flags |
| 939 | * @order: Allocation size is (1 << order) pages |
| 940 | * |
| 941 | * Managed get_free_pages. Memory allocated with this function is |
| 942 | * automatically freed on driver detach. |
| 943 | * |
| 944 | * RETURNS: |
| 945 | * Address of allocated memory on success, 0 on failure. |
| 946 | */ |
| 947 | |
| 948 | unsigned long devm_get_free_pages(struct device *dev, |
| 949 | gfp_t gfp_mask, unsigned int order) |
| 950 | { |
| 951 | struct pages_devres *devres; |
| 952 | unsigned long addr; |
| 953 | |
| 954 | addr = __get_free_pages(gfp_mask, order); |
| 955 | |
| 956 | if (unlikely(!addr)) |
| 957 | return 0; |
| 958 | |
| 959 | devres = devres_alloc(devm_pages_release, |
| 960 | sizeof(struct pages_devres), GFP_KERNEL); |
| 961 | if (unlikely(!devres)) { |
| 962 | free_pages(addr, order); |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | devres->addr = addr; |
| 967 | devres->order = order; |
| 968 | |
| 969 | devres_add(dev, devres); |
| 970 | return addr; |
| 971 | } |
| 972 | EXPORT_SYMBOL_GPL(devm_get_free_pages); |
| 973 | |
| 974 | /** |
| 975 | * devm_free_pages - Resource-managed free_pages |
| 976 | * @dev: Device this memory belongs to |
| 977 | * @addr: Memory to free |
| 978 | * |
| 979 | * Free memory allocated with devm_get_free_pages(). Unlike free_pages, |
| 980 | * there is no need to supply the @order. |
| 981 | */ |
| 982 | void devm_free_pages(struct device *dev, unsigned long addr) |
| 983 | { |
| 984 | struct pages_devres devres = { .addr = addr }; |
| 985 | |
| 986 | WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match, |
| 987 | &devres)); |
| 988 | } |
| 989 | EXPORT_SYMBOL_GPL(devm_free_pages); |
Madalin Bucur | ff86aae | 2016-11-15 10:41:01 +0200 | [diff] [blame] | 990 | |
| 991 | static void devm_percpu_release(struct device *dev, void *pdata) |
| 992 | { |
| 993 | void __percpu *p; |
| 994 | |
| 995 | p = *(void __percpu **)pdata; |
| 996 | free_percpu(p); |
| 997 | } |
| 998 | |
| 999 | static int devm_percpu_match(struct device *dev, void *data, void *p) |
| 1000 | { |
| 1001 | struct devres *devr = container_of(data, struct devres, data); |
| 1002 | |
| 1003 | return *(void **)devr->data == p; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * __devm_alloc_percpu - Resource-managed alloc_percpu |
| 1008 | * @dev: Device to allocate per-cpu memory for |
| 1009 | * @size: Size of per-cpu memory to allocate |
| 1010 | * @align: Alignment of per-cpu memory to allocate |
| 1011 | * |
| 1012 | * Managed alloc_percpu. Per-cpu memory allocated with this function is |
| 1013 | * automatically freed on driver detach. |
| 1014 | * |
| 1015 | * RETURNS: |
| 1016 | * Pointer to allocated memory on success, NULL on failure. |
| 1017 | */ |
| 1018 | void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, |
| 1019 | size_t align) |
| 1020 | { |
| 1021 | void *p; |
| 1022 | void __percpu *pcpu; |
| 1023 | |
| 1024 | pcpu = __alloc_percpu(size, align); |
| 1025 | if (!pcpu) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL); |
| 1029 | if (!p) { |
| 1030 | free_percpu(pcpu); |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | *(void __percpu **)p = pcpu; |
| 1035 | |
| 1036 | devres_add(dev, p); |
| 1037 | |
| 1038 | return pcpu; |
| 1039 | } |
| 1040 | EXPORT_SYMBOL_GPL(__devm_alloc_percpu); |
| 1041 | |
| 1042 | /** |
| 1043 | * devm_free_percpu - Resource-managed free_percpu |
| 1044 | * @dev: Device this memory belongs to |
| 1045 | * @pdata: Per-cpu memory to free |
| 1046 | * |
| 1047 | * Free memory allocated with devm_alloc_percpu(). |
| 1048 | */ |
| 1049 | void devm_free_percpu(struct device *dev, void __percpu *pdata) |
| 1050 | { |
| 1051 | WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match, |
| 1052 | (void *)pdata)); |
| 1053 | } |
| 1054 | EXPORT_SYMBOL_GPL(devm_free_percpu); |