1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/devres.c - device resource management 4 * 5 * Copyright (c) 2006 SUSE Linux Products GmbH 6 * Copyright (c) 2006 Tejun Heo <[email protected]> 7 */ 8 9 #include <linux/device.h> 10 #include <linux/module.h> 11 #include <linux/slab.h> 12 #include <linux/percpu.h> 13 14 #include <asm/sections.h> 15 16 #include "base.h" 17 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 /* 30 * Some archs want to perform DMA into kmalloc caches 31 * and need a guaranteed alignment larger than 32 * the alignment of a 64-bit integer. 33 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same 34 * buffer alignment as if it was allocated by plain kmalloc(). 35 */ 36 u8 __aligned(ARCH_KMALLOC_MINALIGN) data[]; 37 }; 38 39 struct devres_group { 40 struct devres_node node[2]; 41 void *id; 42 int color; 43 /* -- 8 pointers */ 44 }; 45 46 #ifdef CONFIG_DEBUG_DEVRES 47 static int log_devres = 0; 48 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR); 49 50 static void set_node_dbginfo(struct devres_node *node, const char *name, 51 size_t size) 52 { 53 node->name = name; 54 node->size = size; 55 } 56 57 static void devres_log(struct device *dev, struct devres_node *node, 58 const char *op) 59 { 60 if (unlikely(log_devres)) 61 dev_err(dev, "DEVRES %3s %p %s (%zu bytes)\n", 62 op, node, node->name, node->size); 63 } 64 #else /* CONFIG_DEBUG_DEVRES */ 65 #define set_node_dbginfo(node, n, s) do {} while (0) 66 #define devres_log(dev, node, op) do {} while (0) 67 #endif /* CONFIG_DEBUG_DEVRES */ 68 69 /* 70 * Release functions for devres group. These callbacks are used only 71 * for identification. 72 */ 73 static void group_open_release(struct device *dev, void *res) 74 { 75 /* noop */ 76 } 77 78 static void group_close_release(struct device *dev, void *res) 79 { 80 /* noop */ 81 } 82 83 static struct devres_group * node_to_group(struct devres_node *node) 84 { 85 if (node->release == &group_open_release) 86 return container_of(node, struct devres_group, node[0]); 87 if (node->release == &group_close_release) 88 return container_of(node, struct devres_group, node[1]); 89 return NULL; 90 } 91 92 static bool check_dr_size(size_t size, size_t *tot_size) 93 { 94 /* We must catch any near-SIZE_MAX cases that could overflow. */ 95 if (unlikely(check_add_overflow(sizeof(struct devres), 96 size, tot_size))) 97 return false; 98 99 return true; 100 } 101 102 static __always_inline struct devres * alloc_dr(dr_release_t release, 103 size_t size, gfp_t gfp, int nid) 104 { 105 size_t tot_size; 106 struct devres *dr; 107 108 if (!check_dr_size(size, &tot_size)) 109 return NULL; 110 111 dr = kmalloc_node_track_caller(tot_size, gfp, nid); 112 if (unlikely(!dr)) 113 return NULL; 114 115 memset(dr, 0, offsetof(struct devres, data)); 116 117 INIT_LIST_HEAD(&dr->node.entry); 118 dr->node.release = release; 119 return dr; 120 } 121 122 static void add_dr(struct device *dev, struct devres_node *node) 123 { 124 devres_log(dev, node, "ADD"); 125 BUG_ON(!list_empty(&node->entry)); 126 list_add_tail(&node->entry, &dev->devres_head); 127 } 128 129 static void replace_dr(struct device *dev, 130 struct devres_node *old, struct devres_node *new) 131 { 132 devres_log(dev, old, "REPLACE"); 133 BUG_ON(!list_empty(&new->entry)); 134 list_replace(&old->entry, &new->entry); 135 } 136 137 #ifdef CONFIG_DEBUG_DEVRES 138 void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid, 139 const char *name) 140 { 141 struct devres *dr; 142 143 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid); 144 if (unlikely(!dr)) 145 return NULL; 146 set_node_dbginfo(&dr->node, name, size); 147 return dr->data; 148 } 149 EXPORT_SYMBOL_GPL(__devres_alloc_node); 150 #else 151 /** 152 * devres_alloc_node - Allocate device resource data 153 * @release: Release function devres will be associated with 154 * @size: Allocation size 155 * @gfp: Allocation flags 156 * @nid: NUMA node 157 * 158 * Allocate devres of @size bytes. The allocated area is zeroed, then 159 * associated with @release. The returned pointer can be passed to 160 * other devres_*() functions. 161 * 162 * RETURNS: 163 * Pointer to allocated devres on success, NULL on failure. 164 */ 165 void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid) 166 { 167 struct devres *dr; 168 169 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid); 170 if (unlikely(!dr)) 171 return NULL; 172 return dr->data; 173 } 174 EXPORT_SYMBOL_GPL(devres_alloc_node); 175 #endif 176 177 /** 178 * devres_for_each_res - Resource iterator 179 * @dev: Device to iterate resource from 180 * @release: Look for resources associated with this release function 181 * @match: Match function (optional) 182 * @match_data: Data for the match function 183 * @fn: Function to be called for each matched resource. 184 * @data: Data for @fn, the 3rd parameter of @fn 185 * 186 * Call @fn for each devres of @dev which is associated with @release 187 * and for which @match returns 1. 188 * 189 * RETURNS: 190 * void 191 */ 192 void devres_for_each_res(struct device *dev, dr_release_t release, 193 dr_match_t match, void *match_data, 194 void (*fn)(struct device *, void *, void *), 195 void *data) 196 { 197 struct devres_node *node; 198 struct devres_node *tmp; 199 unsigned long flags; 200 201 if (!fn) 202 return; 203 204 spin_lock_irqsave(&dev->devres_lock, flags); 205 list_for_each_entry_safe_reverse(node, tmp, 206 &dev->devres_head, entry) { 207 struct devres *dr = container_of(node, struct devres, node); 208 209 if (node->release != release) 210 continue; 211 if (match && !match(dev, dr->data, match_data)) 212 continue; 213 fn(dev, dr->data, data); 214 } 215 spin_unlock_irqrestore(&dev->devres_lock, flags); 216 } 217 EXPORT_SYMBOL_GPL(devres_for_each_res); 218 219 /** 220 * devres_free - Free device resource data 221 * @res: Pointer to devres data to free 222 * 223 * Free devres created with devres_alloc(). 224 */ 225 void devres_free(void *res) 226 { 227 if (res) { 228 struct devres *dr = container_of(res, struct devres, data); 229 230 BUG_ON(!list_empty(&dr->node.entry)); 231 kfree(dr); 232 } 233 } 234 EXPORT_SYMBOL_GPL(devres_free); 235 236 /** 237 * devres_add - Register device resource 238 * @dev: Device to add resource to 239 * @res: Resource to register 240 * 241 * Register devres @res to @dev. @res should have been allocated 242 * using devres_alloc(). On driver detach, the associated release 243 * function will be invoked and devres will be freed automatically. 244 */ 245 void devres_add(struct device *dev, void *res) 246 { 247 struct devres *dr = container_of(res, struct devres, data); 248 unsigned long flags; 249 250 spin_lock_irqsave(&dev->devres_lock, flags); 251 add_dr(dev, &dr->node); 252 spin_unlock_irqrestore(&dev->devres_lock, flags); 253 } 254 EXPORT_SYMBOL_GPL(devres_add); 255 256 static struct devres *find_dr(struct device *dev, dr_release_t release, 257 dr_match_t match, void *match_data) 258 { 259 struct devres_node *node; 260 261 list_for_each_entry_reverse(node, &dev->devres_head, entry) { 262 struct devres *dr = container_of(node, struct devres, node); 263 264 if (node->release != release) 265 continue; 266 if (match && !match(dev, dr->data, match_data)) 267 continue; 268 return dr; 269 } 270 271 return NULL; 272 } 273 274 /** 275 * devres_find - Find device resource 276 * @dev: Device to lookup resource from 277 * @release: Look for resources associated with this release function 278 * @match: Match function (optional) 279 * @match_data: Data for the match function 280 * 281 * Find the latest devres of @dev which is associated with @release 282 * and for which @match returns 1. If @match is NULL, it's considered 283 * to match all. 284 * 285 * RETURNS: 286 * Pointer to found devres, NULL if not found. 287 */ 288 void * devres_find(struct device *dev, dr_release_t release, 289 dr_match_t match, void *match_data) 290 { 291 struct devres *dr; 292 unsigned long flags; 293 294 spin_lock_irqsave(&dev->devres_lock, flags); 295 dr = find_dr(dev, release, match, match_data); 296 spin_unlock_irqrestore(&dev->devres_lock, flags); 297 298 if (dr) 299 return dr->data; 300 return NULL; 301 } 302 EXPORT_SYMBOL_GPL(devres_find); 303 304 /** 305 * devres_get - Find devres, if non-existent, add one atomically 306 * @dev: Device to lookup or add devres for 307 * @new_res: Pointer to new initialized devres to add if not found 308 * @match: Match function (optional) 309 * @match_data: Data for the match function 310 * 311 * Find the latest devres of @dev which has the same release function 312 * as @new_res and for which @match return 1. If found, @new_res is 313 * freed; otherwise, @new_res is added atomically. 314 * 315 * RETURNS: 316 * Pointer to found or added devres. 317 */ 318 void * devres_get(struct device *dev, void *new_res, 319 dr_match_t match, void *match_data) 320 { 321 struct devres *new_dr = container_of(new_res, struct devres, data); 322 struct devres *dr; 323 unsigned long flags; 324 325 spin_lock_irqsave(&dev->devres_lock, flags); 326 dr = find_dr(dev, new_dr->node.release, match, match_data); 327 if (!dr) { 328 add_dr(dev, &new_dr->node); 329 dr = new_dr; 330 new_res = NULL; 331 } 332 spin_unlock_irqrestore(&dev->devres_lock, flags); 333 devres_free(new_res); 334 335 return dr->data; 336 } 337 EXPORT_SYMBOL_GPL(devres_get); 338 339 /** 340 * devres_remove - Find a device resource and remove it 341 * @dev: Device to find resource from 342 * @release: Look for resources associated with this release function 343 * @match: Match function (optional) 344 * @match_data: Data for the match function 345 * 346 * Find the latest devres of @dev associated with @release and for 347 * which @match returns 1. If @match is NULL, it's considered to 348 * match all. If found, the resource is removed atomically and 349 * returned. 350 * 351 * RETURNS: 352 * Pointer to removed devres on success, NULL if not found. 353 */ 354 void * devres_remove(struct device *dev, dr_release_t release, 355 dr_match_t match, void *match_data) 356 { 357 struct devres *dr; 358 unsigned long flags; 359 360 spin_lock_irqsave(&dev->devres_lock, flags); 361 dr = find_dr(dev, release, match, match_data); 362 if (dr) { 363 list_del_init(&dr->node.entry); 364 devres_log(dev, &dr->node, "REM"); 365 } 366 spin_unlock_irqrestore(&dev->devres_lock, flags); 367 368 if (dr) 369 return dr->data; 370 return NULL; 371 } 372 EXPORT_SYMBOL_GPL(devres_remove); 373 374 /** 375 * devres_destroy - Find a device resource and destroy it 376 * @dev: Device to find resource from 377 * @release: Look for resources associated with this release function 378 * @match: Match function (optional) 379 * @match_data: Data for the match function 380 * 381 * Find the latest devres of @dev associated with @release and for 382 * which @match returns 1. If @match is NULL, it's considered to 383 * match all. If found, the resource is removed atomically and freed. 384 * 385 * Note that the release function for the resource will not be called, 386 * only the devres-allocated data will be freed. The caller becomes 387 * responsible for freeing any other data. 388 * 389 * RETURNS: 390 * 0 if devres is found and freed, -ENOENT if not found. 391 */ 392 int devres_destroy(struct device *dev, dr_release_t release, 393 dr_match_t match, void *match_data) 394 { 395 void *res; 396 397 res = devres_remove(dev, release, match, match_data); 398 if (unlikely(!res)) 399 return -ENOENT; 400 401 devres_free(res); 402 return 0; 403 } 404 EXPORT_SYMBOL_GPL(devres_destroy); 405 406 407 /** 408 * devres_release - Find a device resource and destroy it, calling release 409 * @dev: Device to find resource from 410 * @release: Look for resources associated with this release function 411 * @match: Match function (optional) 412 * @match_data: Data for the match function 413 * 414 * Find the latest devres of @dev associated with @release and for 415 * which @match returns 1. If @match is NULL, it's considered to 416 * match all. If found, the resource is removed atomically, the 417 * release function called and the resource freed. 418 * 419 * RETURNS: 420 * 0 if devres is found and freed, -ENOENT if not found. 421 */ 422 int devres_release(struct device *dev, dr_release_t release, 423 dr_match_t match, void *match_data) 424 { 425 void *res; 426 427 res = devres_remove(dev, release, match, match_data); 428 if (unlikely(!res)) 429 return -ENOENT; 430 431 (*release)(dev, res); 432 devres_free(res); 433 return 0; 434 } 435 EXPORT_SYMBOL_GPL(devres_release); 436 437 static int remove_nodes(struct device *dev, 438 struct list_head *first, struct list_head *end, 439 struct list_head *todo) 440 { 441 struct devres_node *node, *n; 442 int cnt = 0, nr_groups = 0; 443 444 /* First pass - move normal devres entries to @todo and clear 445 * devres_group colors. 446 */ 447 node = list_entry(first, struct devres_node, entry); 448 list_for_each_entry_safe_from(node, n, end, entry) { 449 struct devres_group *grp; 450 451 grp = node_to_group(node); 452 if (grp) { 453 /* clear color of group markers in the first pass */ 454 grp->color = 0; 455 nr_groups++; 456 } else { 457 /* regular devres entry */ 458 if (&node->entry == first) 459 first = first->next; 460 list_move_tail(&node->entry, todo); 461 cnt++; 462 } 463 } 464 465 if (!nr_groups) 466 return cnt; 467 468 /* Second pass - Scan groups and color them. A group gets 469 * color value of two iff the group is wholly contained in 470 * [current node, end). That is, for a closed group, both opening 471 * and closing markers should be in the range, while just the 472 * opening marker is enough for an open group. 473 */ 474 node = list_entry(first, struct devres_node, entry); 475 list_for_each_entry_safe_from(node, n, end, entry) { 476 struct devres_group *grp; 477 478 grp = node_to_group(node); 479 BUG_ON(!grp || list_empty(&grp->node[0].entry)); 480 481 grp->color++; 482 if (list_empty(&grp->node[1].entry)) 483 grp->color++; 484 485 BUG_ON(grp->color <= 0 || grp->color > 2); 486 if (grp->color == 2) { 487 /* No need to update current node or end. The removed 488 * nodes are always before both. 489 */ 490 list_move_tail(&grp->node[0].entry, todo); 491 list_del_init(&grp->node[1].entry); 492 } 493 } 494 495 return cnt; 496 } 497 498 static void release_nodes(struct device *dev, struct list_head *todo) 499 { 500 struct devres *dr, *tmp; 501 502 /* Release. Note that both devres and devres_group are 503 * handled as devres in the following loop. This is safe. 504 */ 505 list_for_each_entry_safe_reverse(dr, tmp, todo, node.entry) { 506 devres_log(dev, &dr->node, "REL"); 507 dr->node.release(dev, dr->data); 508 kfree(dr); 509 } 510 } 511 512 /** 513 * devres_release_all - Release all managed resources 514 * @dev: Device to release resources for 515 * 516 * Release all resources associated with @dev. This function is 517 * called on driver detach. 518 */ 519 int devres_release_all(struct device *dev) 520 { 521 unsigned long flags; 522 LIST_HEAD(todo); 523 int cnt; 524 525 /* Looks like an uninitialized device structure */ 526 if (WARN_ON(dev->devres_head.next == NULL)) 527 return -ENODEV; 528 529 /* Nothing to release if list is empty */ 530 if (list_empty(&dev->devres_head)) 531 return 0; 532 533 spin_lock_irqsave(&dev->devres_lock, flags); 534 cnt = remove_nodes(dev, dev->devres_head.next, &dev->devres_head, &todo); 535 spin_unlock_irqrestore(&dev->devres_lock, flags); 536 537 release_nodes(dev, &todo); 538 return cnt; 539 } 540 541 /** 542 * devres_open_group - Open a new devres group 543 * @dev: Device to open devres group for 544 * @id: Separator ID 545 * @gfp: Allocation flags 546 * 547 * Open a new devres group for @dev with @id. For @id, using a 548 * pointer to an object which won't be used for another group is 549 * recommended. If @id is NULL, address-wise unique ID is created. 550 * 551 * RETURNS: 552 * ID of the new group, NULL on failure. 553 */ 554 void * devres_open_group(struct device *dev, void *id, gfp_t gfp) 555 { 556 struct devres_group *grp; 557 unsigned long flags; 558 559 grp = kmalloc(sizeof(*grp), gfp); 560 if (unlikely(!grp)) 561 return NULL; 562 563 grp->node[0].release = &group_open_release; 564 grp->node[1].release = &group_close_release; 565 INIT_LIST_HEAD(&grp->node[0].entry); 566 INIT_LIST_HEAD(&grp->node[1].entry); 567 set_node_dbginfo(&grp->node[0], "grp<", 0); 568 set_node_dbginfo(&grp->node[1], "grp>", 0); 569 grp->id = grp; 570 if (id) 571 grp->id = id; 572 573 spin_lock_irqsave(&dev->devres_lock, flags); 574 add_dr(dev, &grp->node[0]); 575 spin_unlock_irqrestore(&dev->devres_lock, flags); 576 return grp->id; 577 } 578 EXPORT_SYMBOL_GPL(devres_open_group); 579 580 /* Find devres group with ID @id. If @id is NULL, look for the latest. */ 581 static struct devres_group * find_group(struct device *dev, void *id) 582 { 583 struct devres_node *node; 584 585 list_for_each_entry_reverse(node, &dev->devres_head, entry) { 586 struct devres_group *grp; 587 588 if (node->release != &group_open_release) 589 continue; 590 591 grp = container_of(node, struct devres_group, node[0]); 592 593 if (id) { 594 if (grp->id == id) 595 return grp; 596 } else if (list_empty(&grp->node[1].entry)) 597 return grp; 598 } 599 600 return NULL; 601 } 602 603 /** 604 * devres_close_group - Close a devres group 605 * @dev: Device to close devres group for 606 * @id: ID of target group, can be NULL 607 * 608 * Close the group identified by @id. If @id is NULL, the latest open 609 * group is selected. 610 */ 611 void devres_close_group(struct device *dev, void *id) 612 { 613 struct devres_group *grp; 614 unsigned long flags; 615 616 spin_lock_irqsave(&dev->devres_lock, flags); 617 618 grp = find_group(dev, id); 619 if (grp) 620 add_dr(dev, &grp->node[1]); 621 else 622 WARN_ON(1); 623 624 spin_unlock_irqrestore(&dev->devres_lock, flags); 625 } 626 EXPORT_SYMBOL_GPL(devres_close_group); 627 628 /** 629 * devres_remove_group - Remove a devres group 630 * @dev: Device to remove group for 631 * @id: ID of target group, can be NULL 632 * 633 * Remove the group identified by @id. If @id is NULL, the latest 634 * open group is selected. Note that removing a group doesn't affect 635 * any other resources. 636 */ 637 void devres_remove_group(struct device *dev, void *id) 638 { 639 struct devres_group *grp; 640 unsigned long flags; 641 642 spin_lock_irqsave(&dev->devres_lock, flags); 643 644 grp = find_group(dev, id); 645 if (grp) { 646 list_del_init(&grp->node[0].entry); 647 list_del_init(&grp->node[1].entry); 648 devres_log(dev, &grp->node[0], "REM"); 649 } else 650 WARN_ON(1); 651 652 spin_unlock_irqrestore(&dev->devres_lock, flags); 653 654 kfree(grp); 655 } 656 EXPORT_SYMBOL_GPL(devres_remove_group); 657 658 /** 659 * devres_release_group - Release resources in a devres group 660 * @dev: Device to release group for 661 * @id: ID of target group, can be NULL 662 * 663 * Release all resources in the group identified by @id. If @id is 664 * NULL, the latest open group is selected. The selected group and 665 * groups properly nested inside the selected group are removed. 666 * 667 * RETURNS: 668 * The number of released non-group resources. 669 */ 670 int devres_release_group(struct device *dev, void *id) 671 { 672 struct devres_group *grp; 673 unsigned long flags; 674 LIST_HEAD(todo); 675 int cnt = 0; 676 677 spin_lock_irqsave(&dev->devres_lock, flags); 678 679 grp = find_group(dev, id); 680 if (grp) { 681 struct list_head *first = &grp->node[0].entry; 682 struct list_head *end = &dev->devres_head; 683 684 if (!list_empty(&grp->node[1].entry)) 685 end = grp->node[1].entry.next; 686 687 cnt = remove_nodes(dev, first, end, &todo); 688 spin_unlock_irqrestore(&dev->devres_lock, flags); 689 690 release_nodes(dev, &todo); 691 } else { 692 WARN_ON(1); 693 spin_unlock_irqrestore(&dev->devres_lock, flags); 694 } 695 696 return cnt; 697 } 698 EXPORT_SYMBOL_GPL(devres_release_group); 699 700 /* 701 * Custom devres actions allow inserting a simple function call 702 * into the teadown sequence. 703 */ 704 705 struct action_devres { 706 void *data; 707 void (*action)(void *); 708 }; 709 710 static int devm_action_match(struct device *dev, void *res, void *p) 711 { 712 struct action_devres *devres = res; 713 struct action_devres *target = p; 714 715 return devres->action == target->action && 716 devres->data == target->data; 717 } 718 719 static void devm_action_release(struct device *dev, void *res) 720 { 721 struct action_devres *devres = res; 722 723 devres->action(devres->data); 724 } 725 726 /** 727 * devm_add_action() - add a custom action to list of managed resources 728 * @dev: Device that owns the action 729 * @action: Function that should be called 730 * @data: Pointer to data passed to @action implementation 731 * 732 * This adds a custom action to the list of managed resources so that 733 * it gets executed as part of standard resource unwinding. 734 */ 735 int devm_add_action(struct device *dev, void (*action)(void *), void *data) 736 { 737 struct action_devres *devres; 738 739 devres = devres_alloc(devm_action_release, 740 sizeof(struct action_devres), GFP_KERNEL); 741 if (!devres) 742 return -ENOMEM; 743 744 devres->data = data; 745 devres->action = action; 746 747 devres_add(dev, devres); 748 return 0; 749 } 750 EXPORT_SYMBOL_GPL(devm_add_action); 751 752 /** 753 * devm_remove_action() - removes previously added custom action 754 * @dev: Device that owns the action 755 * @action: Function implementing the action 756 * @data: Pointer to data passed to @action implementation 757 * 758 * Removes instance of @action previously added by devm_add_action(). 759 * Both action and data should match one of the existing entries. 760 */ 761 void devm_remove_action(struct device *dev, void (*action)(void *), void *data) 762 { 763 struct action_devres devres = { 764 .data = data, 765 .action = action, 766 }; 767 768 WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match, 769 &devres)); 770 } 771 EXPORT_SYMBOL_GPL(devm_remove_action); 772 773 /** 774 * devm_release_action() - release previously added custom action 775 * @dev: Device that owns the action 776 * @action: Function implementing the action 777 * @data: Pointer to data passed to @action implementation 778 * 779 * Releases and removes instance of @action previously added by 780 * devm_add_action(). Both action and data should match one of the 781 * existing entries. 782 */ 783 void devm_release_action(struct device *dev, void (*action)(void *), void *data) 784 { 785 struct action_devres devres = { 786 .data = data, 787 .action = action, 788 }; 789 790 WARN_ON(devres_release(dev, devm_action_release, devm_action_match, 791 &devres)); 792 793 } 794 EXPORT_SYMBOL_GPL(devm_release_action); 795 796 /* 797 * Managed kmalloc/kfree 798 */ 799 static void devm_kmalloc_release(struct device *dev, void *res) 800 { 801 /* noop */ 802 } 803 804 static int devm_kmalloc_match(struct device *dev, void *res, void *data) 805 { 806 return res == data; 807 } 808 809 /** 810 * devm_kmalloc - Resource-managed kmalloc 811 * @dev: Device to allocate memory for 812 * @size: Allocation size 813 * @gfp: Allocation gfp flags 814 * 815 * Managed kmalloc. Memory allocated with this function is 816 * automatically freed on driver detach. Like all other devres 817 * resources, guaranteed alignment is unsigned long long. 818 * 819 * RETURNS: 820 * Pointer to allocated memory on success, NULL on failure. 821 */ 822 void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) 823 { 824 struct devres *dr; 825 826 if (unlikely(!size)) 827 return ZERO_SIZE_PTR; 828 829 /* use raw alloc_dr for kmalloc caller tracing */ 830 dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev)); 831 if (unlikely(!dr)) 832 return NULL; 833 834 /* 835 * This is named devm_kzalloc_release for historical reasons 836 * The initial implementation did not support kmalloc, only kzalloc 837 */ 838 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); 839 devres_add(dev, dr->data); 840 return dr->data; 841 } 842 EXPORT_SYMBOL_GPL(devm_kmalloc); 843 844 /** 845 * devm_krealloc - Resource-managed krealloc() 846 * @dev: Device to re-allocate memory for 847 * @ptr: Pointer to the memory chunk to re-allocate 848 * @new_size: New allocation size 849 * @gfp: Allocation gfp flags 850 * 851 * Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc(). 852 * Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR, 853 * it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the 854 * previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't 855 * change the order in which the release callback for the re-alloc'ed devres 856 * will be called (except when falling back to devm_kmalloc() or when freeing 857 * resources when new_size is zero). The contents of the memory are preserved 858 * up to the lesser of new and old sizes. 859 */ 860 void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) 861 { 862 size_t total_new_size, total_old_size; 863 struct devres *old_dr, *new_dr; 864 unsigned long flags; 865 866 if (unlikely(!new_size)) { 867 devm_kfree(dev, ptr); 868 return ZERO_SIZE_PTR; 869 } 870 871 if (unlikely(ZERO_OR_NULL_PTR(ptr))) 872 return devm_kmalloc(dev, new_size, gfp); 873 874 if (WARN_ON(is_kernel_rodata((unsigned long)ptr))) 875 /* 876 * We cannot reliably realloc a const string returned by 877 * devm_kstrdup_const(). 878 */ 879 return NULL; 880 881 if (!check_dr_size(new_size, &total_new_size)) 882 return NULL; 883 884 total_old_size = ksize(container_of(ptr, struct devres, data)); 885 if (total_old_size == 0) { 886 WARN(1, "Pointer doesn't point to dynamically allocated memory."); 887 return NULL; 888 } 889 890 /* 891 * If new size is smaller or equal to the actual number of bytes 892 * allocated previously - just return the same pointer. 893 */ 894 if (total_new_size <= total_old_size) 895 return ptr; 896 897 /* 898 * Otherwise: allocate new, larger chunk. We need to allocate before 899 * taking the lock as most probably the caller uses GFP_KERNEL. 900 */ 901 new_dr = alloc_dr(devm_kmalloc_release, 902 total_new_size, gfp, dev_to_node(dev)); 903 if (!new_dr) 904 return NULL; 905 906 /* 907 * The spinlock protects the linked list against concurrent 908 * modifications but not the resource itself. 909 */ 910 spin_lock_irqsave(&dev->devres_lock, flags); 911 912 old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); 913 if (!old_dr) { 914 spin_unlock_irqrestore(&dev->devres_lock, flags); 915 kfree(new_dr); 916 WARN(1, "Memory chunk not managed or managed by a different device."); 917 return NULL; 918 } 919 920 replace_dr(dev, &old_dr->node, &new_dr->node); 921 922 spin_unlock_irqrestore(&dev->devres_lock, flags); 923 924 /* 925 * We can copy the memory contents after releasing the lock as we're 926 * no longer modyfing the list links. 927 */ 928 memcpy(new_dr->data, old_dr->data, 929 total_old_size - offsetof(struct devres, data)); 930 /* 931 * Same for releasing the old devres - it's now been removed from the 932 * list. This is also the reason why we must not use devm_kfree() - the 933 * links are no longer valid. 934 */ 935 kfree(old_dr); 936 937 return new_dr->data; 938 } 939 EXPORT_SYMBOL_GPL(devm_krealloc); 940 941 /** 942 * devm_kstrdup - Allocate resource managed space and 943 * copy an existing string into that. 944 * @dev: Device to allocate memory for 945 * @s: the string to duplicate 946 * @gfp: the GFP mask used in the devm_kmalloc() call when 947 * allocating memory 948 * RETURNS: 949 * Pointer to allocated string on success, NULL on failure. 950 */ 951 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) 952 { 953 size_t size; 954 char *buf; 955 956 if (!s) 957 return NULL; 958 959 size = strlen(s) + 1; 960 buf = devm_kmalloc(dev, size, gfp); 961 if (buf) 962 memcpy(buf, s, size); 963 return buf; 964 } 965 EXPORT_SYMBOL_GPL(devm_kstrdup); 966 967 /** 968 * devm_kstrdup_const - resource managed conditional string duplication 969 * @dev: device for which to duplicate the string 970 * @s: the string to duplicate 971 * @gfp: the GFP mask used in the kmalloc() call when allocating memory 972 * 973 * Strings allocated by devm_kstrdup_const will be automatically freed when 974 * the associated device is detached. 975 * 976 * RETURNS: 977 * Source string if it is in .rodata section otherwise it falls back to 978 * devm_kstrdup. 979 */ 980 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp) 981 { 982 if (is_kernel_rodata((unsigned long)s)) 983 return s; 984 985 return devm_kstrdup(dev, s, gfp); 986 } 987 EXPORT_SYMBOL_GPL(devm_kstrdup_const); 988 989 /** 990 * devm_kvasprintf - Allocate resource managed space and format a string 991 * into that. 992 * @dev: Device to allocate memory for 993 * @gfp: the GFP mask used in the devm_kmalloc() call when 994 * allocating memory 995 * @fmt: The printf()-style format string 996 * @ap: Arguments for the format string 997 * RETURNS: 998 * Pointer to allocated string on success, NULL on failure. 999 */ 1000 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, 1001 va_list ap) 1002 { 1003 unsigned int len; 1004 char *p; 1005 va_list aq; 1006 1007 va_copy(aq, ap); 1008 len = vsnprintf(NULL, 0, fmt, aq); 1009 va_end(aq); 1010 1011 p = devm_kmalloc(dev, len+1, gfp); 1012 if (!p) 1013 return NULL; 1014 1015 vsnprintf(p, len+1, fmt, ap); 1016 1017 return p; 1018 } 1019 EXPORT_SYMBOL(devm_kvasprintf); 1020 1021 /** 1022 * devm_kasprintf - Allocate resource managed space and format a string 1023 * into that. 1024 * @dev: Device to allocate memory for 1025 * @gfp: the GFP mask used in the devm_kmalloc() call when 1026 * allocating memory 1027 * @fmt: The printf()-style format string 1028 * @...: Arguments for the format string 1029 * RETURNS: 1030 * Pointer to allocated string on success, NULL on failure. 1031 */ 1032 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) 1033 { 1034 va_list ap; 1035 char *p; 1036 1037 va_start(ap, fmt); 1038 p = devm_kvasprintf(dev, gfp, fmt, ap); 1039 va_end(ap); 1040 1041 return p; 1042 } 1043 EXPORT_SYMBOL_GPL(devm_kasprintf); 1044 1045 /** 1046 * devm_kfree - Resource-managed kfree 1047 * @dev: Device this memory belongs to 1048 * @p: Memory to free 1049 * 1050 * Free memory allocated with devm_kmalloc(). 1051 */ 1052 void devm_kfree(struct device *dev, const void *p) 1053 { 1054 int rc; 1055 1056 /* 1057 * Special cases: pointer to a string in .rodata returned by 1058 * devm_kstrdup_const() or NULL/ZERO ptr. 1059 */ 1060 if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p))) 1061 return; 1062 1063 rc = devres_destroy(dev, devm_kmalloc_release, 1064 devm_kmalloc_match, (void *)p); 1065 WARN_ON(rc); 1066 } 1067 EXPORT_SYMBOL_GPL(devm_kfree); 1068 1069 /** 1070 * devm_kmemdup - Resource-managed kmemdup 1071 * @dev: Device this memory belongs to 1072 * @src: Memory region to duplicate 1073 * @len: Memory region length 1074 * @gfp: GFP mask to use 1075 * 1076 * Duplicate region of a memory using resource managed kmalloc 1077 */ 1078 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) 1079 { 1080 void *p; 1081 1082 p = devm_kmalloc(dev, len, gfp); 1083 if (p) 1084 memcpy(p, src, len); 1085 1086 return p; 1087 } 1088 EXPORT_SYMBOL_GPL(devm_kmemdup); 1089 1090 struct pages_devres { 1091 unsigned long addr; 1092 unsigned int order; 1093 }; 1094 1095 static int devm_pages_match(struct device *dev, void *res, void *p) 1096 { 1097 struct pages_devres *devres = res; 1098 struct pages_devres *target = p; 1099 1100 return devres->addr == target->addr; 1101 } 1102 1103 static void devm_pages_release(struct device *dev, void *res) 1104 { 1105 struct pages_devres *devres = res; 1106 1107 free_pages(devres->addr, devres->order); 1108 } 1109 1110 /** 1111 * devm_get_free_pages - Resource-managed __get_free_pages 1112 * @dev: Device to allocate memory for 1113 * @gfp_mask: Allocation gfp flags 1114 * @order: Allocation size is (1 << order) pages 1115 * 1116 * Managed get_free_pages. Memory allocated with this function is 1117 * automatically freed on driver detach. 1118 * 1119 * RETURNS: 1120 * Address of allocated memory on success, 0 on failure. 1121 */ 1122 1123 unsigned long devm_get_free_pages(struct device *dev, 1124 gfp_t gfp_mask, unsigned int order) 1125 { 1126 struct pages_devres *devres; 1127 unsigned long addr; 1128 1129 addr = __get_free_pages(gfp_mask, order); 1130 1131 if (unlikely(!addr)) 1132 return 0; 1133 1134 devres = devres_alloc(devm_pages_release, 1135 sizeof(struct pages_devres), GFP_KERNEL); 1136 if (unlikely(!devres)) { 1137 free_pages(addr, order); 1138 return 0; 1139 } 1140 1141 devres->addr = addr; 1142 devres->order = order; 1143 1144 devres_add(dev, devres); 1145 return addr; 1146 } 1147 EXPORT_SYMBOL_GPL(devm_get_free_pages); 1148 1149 /** 1150 * devm_free_pages - Resource-managed free_pages 1151 * @dev: Device this memory belongs to 1152 * @addr: Memory to free 1153 * 1154 * Free memory allocated with devm_get_free_pages(). Unlike free_pages, 1155 * there is no need to supply the @order. 1156 */ 1157 void devm_free_pages(struct device *dev, unsigned long addr) 1158 { 1159 struct pages_devres devres = { .addr = addr }; 1160 1161 WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match, 1162 &devres)); 1163 } 1164 EXPORT_SYMBOL_GPL(devm_free_pages); 1165 1166 static void devm_percpu_release(struct device *dev, void *pdata) 1167 { 1168 void __percpu *p; 1169 1170 p = *(void __percpu **)pdata; 1171 free_percpu(p); 1172 } 1173 1174 static int devm_percpu_match(struct device *dev, void *data, void *p) 1175 { 1176 struct devres *devr = container_of(data, struct devres, data); 1177 1178 return *(void **)devr->data == p; 1179 } 1180 1181 /** 1182 * __devm_alloc_percpu - Resource-managed alloc_percpu 1183 * @dev: Device to allocate per-cpu memory for 1184 * @size: Size of per-cpu memory to allocate 1185 * @align: Alignment of per-cpu memory to allocate 1186 * 1187 * Managed alloc_percpu. Per-cpu memory allocated with this function is 1188 * automatically freed on driver detach. 1189 * 1190 * RETURNS: 1191 * Pointer to allocated memory on success, NULL on failure. 1192 */ 1193 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, 1194 size_t align) 1195 { 1196 void *p; 1197 void __percpu *pcpu; 1198 1199 pcpu = __alloc_percpu(size, align); 1200 if (!pcpu) 1201 return NULL; 1202 1203 p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL); 1204 if (!p) { 1205 free_percpu(pcpu); 1206 return NULL; 1207 } 1208 1209 *(void __percpu **)p = pcpu; 1210 1211 devres_add(dev, p); 1212 1213 return pcpu; 1214 } 1215 EXPORT_SYMBOL_GPL(__devm_alloc_percpu); 1216 1217 /** 1218 * devm_free_percpu - Resource-managed free_percpu 1219 * @dev: Device this memory belongs to 1220 * @pdata: Per-cpu memory to free 1221 * 1222 * Free memory allocated with devm_alloc_percpu(). 1223 */ 1224 void devm_free_percpu(struct device *dev, void __percpu *pdata) 1225 { 1226 WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match, 1227 (__force void *)pdata)); 1228 } 1229 EXPORT_SYMBOL_GPL(devm_free_percpu); 1230