1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/devres.c - device resource management
5 * Copyright (c) 2006 SUSE Linux Products GmbH
6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/percpu.h>
17 struct list_head entry
;
19 #ifdef CONFIG_DEBUG_DEVRES
26 struct devres_node node
;
28 * Some archs want to perform DMA into kmalloc caches
29 * and need a guaranteed alignment larger than
30 * the alignment of a 64-bit integer.
31 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
32 * buffer alignment as if it was allocated by plain kmalloc().
34 u8
__aligned(ARCH_KMALLOC_MINALIGN
) data
[];
38 struct devres_node node
[2];
44 #ifdef CONFIG_DEBUG_DEVRES
45 static int log_devres
= 0;
46 module_param_named(log
, log_devres
, int, S_IRUGO
| S_IWUSR
);
48 static void set_node_dbginfo(struct devres_node
*node
, const char *name
,
55 static void devres_log(struct device
*dev
, struct devres_node
*node
,
58 if (unlikely(log_devres
))
59 dev_err(dev
, "DEVRES %3s %p %s (%lu bytes)\n",
60 op
, node
, node
->name
, (unsigned long)node
->size
);
62 #else /* CONFIG_DEBUG_DEVRES */
63 #define set_node_dbginfo(node, n, s) do {} while (0)
64 #define devres_log(dev, node, op) do {} while (0)
65 #endif /* CONFIG_DEBUG_DEVRES */
68 * Release functions for devres group. These callbacks are used only
71 static void group_open_release(struct device
*dev
, void *res
)
76 static void group_close_release(struct device
*dev
, void *res
)
81 static struct devres_group
* node_to_group(struct devres_node
*node
)
83 if (node
->release
== &group_open_release
)
84 return container_of(node
, struct devres_group
, node
[0]);
85 if (node
->release
== &group_close_release
)
86 return container_of(node
, struct devres_group
, node
[1]);
90 static __always_inline
struct devres
* alloc_dr(dr_release_t release
,
91 size_t size
, gfp_t gfp
, int nid
)
96 /* We must catch any near-SIZE_MAX cases that could overflow. */
97 if (unlikely(check_add_overflow(sizeof(struct devres
), size
,
101 dr
= kmalloc_node_track_caller(tot_size
, gfp
, nid
);
105 memset(dr
, 0, offsetof(struct devres
, data
));
107 INIT_LIST_HEAD(&dr
->node
.entry
);
108 dr
->node
.release
= release
;
112 static void add_dr(struct device
*dev
, struct devres_node
*node
)
114 devres_log(dev
, node
, "ADD");
115 BUG_ON(!list_empty(&node
->entry
));
116 list_add_tail(&node
->entry
, &dev
->devres_head
);
119 #ifdef CONFIG_DEBUG_DEVRES
120 void * __devres_alloc_node(dr_release_t release
, size_t size
, gfp_t gfp
, int nid
,
125 dr
= alloc_dr(release
, size
, gfp
| __GFP_ZERO
, nid
);
128 set_node_dbginfo(&dr
->node
, name
, size
);
131 EXPORT_SYMBOL_GPL(__devres_alloc_node
);
134 * devres_alloc - Allocate device resource data
135 * @release: Release function devres will be associated with
136 * @size: Allocation size
137 * @gfp: Allocation flags
140 * Allocate devres of @size bytes. The allocated area is zeroed, then
141 * associated with @release. The returned pointer can be passed to
142 * other devres_*() functions.
145 * Pointer to allocated devres on success, NULL on failure.
147 void * devres_alloc_node(dr_release_t release
, size_t size
, gfp_t gfp
, int nid
)
151 dr
= alloc_dr(release
, size
, gfp
| __GFP_ZERO
, nid
);
156 EXPORT_SYMBOL_GPL(devres_alloc_node
);
160 * devres_for_each_res - Resource iterator
161 * @dev: Device to iterate resource from
162 * @release: Look for resources associated with this release function
163 * @match: Match function (optional)
164 * @match_data: Data for the match function
165 * @fn: Function to be called for each matched resource.
166 * @data: Data for @fn, the 3rd parameter of @fn
168 * Call @fn for each devres of @dev which is associated with @release
169 * and for which @match returns 1.
174 void devres_for_each_res(struct device
*dev
, dr_release_t release
,
175 dr_match_t match
, void *match_data
,
176 void (*fn
)(struct device
*, void *, void *),
179 struct devres_node
*node
;
180 struct devres_node
*tmp
;
186 spin_lock_irqsave(&dev
->devres_lock
, flags
);
187 list_for_each_entry_safe_reverse(node
, tmp
,
188 &dev
->devres_head
, entry
) {
189 struct devres
*dr
= container_of(node
, struct devres
, node
);
191 if (node
->release
!= release
)
193 if (match
&& !match(dev
, dr
->data
, match_data
))
195 fn(dev
, dr
->data
, data
);
197 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
199 EXPORT_SYMBOL_GPL(devres_for_each_res
);
202 * devres_free - Free device resource data
203 * @res: Pointer to devres data to free
205 * Free devres created with devres_alloc().
207 void devres_free(void *res
)
210 struct devres
*dr
= container_of(res
, struct devres
, data
);
212 BUG_ON(!list_empty(&dr
->node
.entry
));
216 EXPORT_SYMBOL_GPL(devres_free
);
219 * devres_add - Register device resource
220 * @dev: Device to add resource to
221 * @res: Resource to register
223 * Register devres @res to @dev. @res should have been allocated
224 * using devres_alloc(). On driver detach, the associated release
225 * function will be invoked and devres will be freed automatically.
227 void devres_add(struct device
*dev
, void *res
)
229 struct devres
*dr
= container_of(res
, struct devres
, data
);
232 spin_lock_irqsave(&dev
->devres_lock
, flags
);
233 add_dr(dev
, &dr
->node
);
234 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
236 EXPORT_SYMBOL_GPL(devres_add
);
238 static struct devres
*find_dr(struct device
*dev
, dr_release_t release
,
239 dr_match_t match
, void *match_data
)
241 struct devres_node
*node
;
243 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
244 struct devres
*dr
= container_of(node
, struct devres
, node
);
246 if (node
->release
!= release
)
248 if (match
&& !match(dev
, dr
->data
, match_data
))
257 * devres_find - Find device resource
258 * @dev: Device to lookup resource from
259 * @release: Look for resources associated with this release function
260 * @match: Match function (optional)
261 * @match_data: Data for the match function
263 * Find the latest devres of @dev which is associated with @release
264 * and for which @match returns 1. If @match is NULL, it's considered
268 * Pointer to found devres, NULL if not found.
270 void * devres_find(struct device
*dev
, dr_release_t release
,
271 dr_match_t match
, void *match_data
)
276 spin_lock_irqsave(&dev
->devres_lock
, flags
);
277 dr
= find_dr(dev
, release
, match
, match_data
);
278 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
284 EXPORT_SYMBOL_GPL(devres_find
);
287 * devres_get - Find devres, if non-existent, add one atomically
288 * @dev: Device to lookup or add devres for
289 * @new_res: Pointer to new initialized devres to add if not found
290 * @match: Match function (optional)
291 * @match_data: Data for the match function
293 * Find the latest devres of @dev which has the same release function
294 * as @new_res and for which @match return 1. If found, @new_res is
295 * freed; otherwise, @new_res is added atomically.
298 * Pointer to found or added devres.
300 void * devres_get(struct device
*dev
, void *new_res
,
301 dr_match_t match
, void *match_data
)
303 struct devres
*new_dr
= container_of(new_res
, struct devres
, data
);
307 spin_lock_irqsave(&dev
->devres_lock
, flags
);
308 dr
= find_dr(dev
, new_dr
->node
.release
, match
, match_data
);
310 add_dr(dev
, &new_dr
->node
);
314 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
315 devres_free(new_res
);
319 EXPORT_SYMBOL_GPL(devres_get
);
322 * devres_remove - Find a device resource and remove it
323 * @dev: Device to find resource from
324 * @release: Look for resources associated with this release function
325 * @match: Match function (optional)
326 * @match_data: Data for the match function
328 * Find the latest devres of @dev associated with @release and for
329 * which @match returns 1. If @match is NULL, it's considered to
330 * match all. If found, the resource is removed atomically and
334 * Pointer to removed devres on success, NULL if not found.
336 void * devres_remove(struct device
*dev
, dr_release_t release
,
337 dr_match_t match
, void *match_data
)
342 spin_lock_irqsave(&dev
->devres_lock
, flags
);
343 dr
= find_dr(dev
, release
, match
, match_data
);
345 list_del_init(&dr
->node
.entry
);
346 devres_log(dev
, &dr
->node
, "REM");
348 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
354 EXPORT_SYMBOL_GPL(devres_remove
);
357 * devres_destroy - Find a device resource and destroy it
358 * @dev: Device to find resource from
359 * @release: Look for resources associated with this release function
360 * @match: Match function (optional)
361 * @match_data: Data for the match function
363 * Find the latest devres of @dev associated with @release and for
364 * which @match returns 1. If @match is NULL, it's considered to
365 * match all. If found, the resource is removed atomically and freed.
367 * Note that the release function for the resource will not be called,
368 * only the devres-allocated data will be freed. The caller becomes
369 * responsible for freeing any other data.
372 * 0 if devres is found and freed, -ENOENT if not found.
374 int devres_destroy(struct device
*dev
, dr_release_t release
,
375 dr_match_t match
, void *match_data
)
379 res
= devres_remove(dev
, release
, match
, match_data
);
386 EXPORT_SYMBOL_GPL(devres_destroy
);
390 * devres_release - Find a device resource and destroy it, calling release
391 * @dev: Device to find resource from
392 * @release: Look for resources associated with this release function
393 * @match: Match function (optional)
394 * @match_data: Data for the match function
396 * Find the latest devres of @dev associated with @release and for
397 * which @match returns 1. If @match is NULL, it's considered to
398 * match all. If found, the resource is removed atomically, the
399 * release function called and the resource freed.
402 * 0 if devres is found and freed, -ENOENT if not found.
404 int devres_release(struct device
*dev
, dr_release_t release
,
405 dr_match_t match
, void *match_data
)
409 res
= devres_remove(dev
, release
, match
, match_data
);
413 (*release
)(dev
, res
);
417 EXPORT_SYMBOL_GPL(devres_release
);
419 static int remove_nodes(struct device
*dev
,
420 struct list_head
*first
, struct list_head
*end
,
421 struct list_head
*todo
)
423 int cnt
= 0, nr_groups
= 0;
424 struct list_head
*cur
;
426 /* First pass - move normal devres entries to @todo and clear
427 * devres_group colors.
431 struct devres_node
*node
;
432 struct devres_group
*grp
;
434 node
= list_entry(cur
, struct devres_node
, entry
);
437 grp
= node_to_group(node
);
439 /* clear color of group markers in the first pass */
443 /* regular devres entry */
444 if (&node
->entry
== first
)
446 list_move_tail(&node
->entry
, todo
);
454 /* Second pass - Scan groups and color them. A group gets
455 * color value of two iff the group is wholly contained in
456 * [cur, end). That is, for a closed group, both opening and
457 * closing markers should be in the range, while just the
458 * opening marker is enough for an open group.
462 struct devres_node
*node
;
463 struct devres_group
*grp
;
465 node
= list_entry(cur
, struct devres_node
, entry
);
468 grp
= node_to_group(node
);
469 BUG_ON(!grp
|| list_empty(&grp
->node
[0].entry
));
472 if (list_empty(&grp
->node
[1].entry
))
475 BUG_ON(grp
->color
<= 0 || grp
->color
> 2);
476 if (grp
->color
== 2) {
477 /* No need to update cur or end. The removed
478 * nodes are always before both.
480 list_move_tail(&grp
->node
[0].entry
, todo
);
481 list_del_init(&grp
->node
[1].entry
);
488 static int release_nodes(struct device
*dev
, struct list_head
*first
,
489 struct list_head
*end
, unsigned long flags
)
490 __releases(&dev
->devres_lock
)
494 struct devres
*dr
, *tmp
;
496 cnt
= remove_nodes(dev
, first
, end
, &todo
);
498 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
500 /* Release. Note that both devres and devres_group are
501 * handled as devres in the following loop. This is safe.
503 list_for_each_entry_safe_reverse(dr
, tmp
, &todo
, node
.entry
) {
504 devres_log(dev
, &dr
->node
, "REL");
505 dr
->node
.release(dev
, dr
->data
);
513 * devres_release_all - Release all managed resources
514 * @dev: Device to release resources for
516 * Release all resources associated with @dev. This function is
517 * called on driver detach.
519 int devres_release_all(struct device
*dev
)
523 /* Looks like an uninitialized device structure */
524 if (WARN_ON(dev
->devres_head
.next
== NULL
))
526 spin_lock_irqsave(&dev
->devres_lock
, flags
);
527 return release_nodes(dev
, dev
->devres_head
.next
, &dev
->devres_head
,
532 * devres_open_group - Open a new devres group
533 * @dev: Device to open devres group for
535 * @gfp: Allocation flags
537 * Open a new devres group for @dev with @id. For @id, using a
538 * pointer to an object which won't be used for another group is
539 * recommended. If @id is NULL, address-wise unique ID is created.
542 * ID of the new group, NULL on failure.
544 void * devres_open_group(struct device
*dev
, void *id
, gfp_t gfp
)
546 struct devres_group
*grp
;
549 grp
= kmalloc(sizeof(*grp
), gfp
);
553 grp
->node
[0].release
= &group_open_release
;
554 grp
->node
[1].release
= &group_close_release
;
555 INIT_LIST_HEAD(&grp
->node
[0].entry
);
556 INIT_LIST_HEAD(&grp
->node
[1].entry
);
557 set_node_dbginfo(&grp
->node
[0], "grp<", 0);
558 set_node_dbginfo(&grp
->node
[1], "grp>", 0);
563 spin_lock_irqsave(&dev
->devres_lock
, flags
);
564 add_dr(dev
, &grp
->node
[0]);
565 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
568 EXPORT_SYMBOL_GPL(devres_open_group
);
570 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
571 static struct devres_group
* find_group(struct device
*dev
, void *id
)
573 struct devres_node
*node
;
575 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
576 struct devres_group
*grp
;
578 if (node
->release
!= &group_open_release
)
581 grp
= container_of(node
, struct devres_group
, node
[0]);
586 } else if (list_empty(&grp
->node
[1].entry
))
594 * devres_close_group - Close a devres group
595 * @dev: Device to close devres group for
596 * @id: ID of target group, can be NULL
598 * Close the group identified by @id. If @id is NULL, the latest open
601 void devres_close_group(struct device
*dev
, void *id
)
603 struct devres_group
*grp
;
606 spin_lock_irqsave(&dev
->devres_lock
, flags
);
608 grp
= find_group(dev
, id
);
610 add_dr(dev
, &grp
->node
[1]);
614 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
616 EXPORT_SYMBOL_GPL(devres_close_group
);
619 * devres_remove_group - Remove a devres group
620 * @dev: Device to remove group for
621 * @id: ID of target group, can be NULL
623 * Remove the group identified by @id. If @id is NULL, the latest
624 * open group is selected. Note that removing a group doesn't affect
625 * any other resources.
627 void devres_remove_group(struct device
*dev
, void *id
)
629 struct devres_group
*grp
;
632 spin_lock_irqsave(&dev
->devres_lock
, flags
);
634 grp
= find_group(dev
, id
);
636 list_del_init(&grp
->node
[0].entry
);
637 list_del_init(&grp
->node
[1].entry
);
638 devres_log(dev
, &grp
->node
[0], "REM");
642 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
646 EXPORT_SYMBOL_GPL(devres_remove_group
);
649 * devres_release_group - Release resources in a devres group
650 * @dev: Device to release group for
651 * @id: ID of target group, can be NULL
653 * Release all resources in the group identified by @id. If @id is
654 * NULL, the latest open group is selected. The selected group and
655 * groups properly nested inside the selected group are removed.
658 * The number of released non-group resources.
660 int devres_release_group(struct device
*dev
, void *id
)
662 struct devres_group
*grp
;
666 spin_lock_irqsave(&dev
->devres_lock
, flags
);
668 grp
= find_group(dev
, id
);
670 struct list_head
*first
= &grp
->node
[0].entry
;
671 struct list_head
*end
= &dev
->devres_head
;
673 if (!list_empty(&grp
->node
[1].entry
))
674 end
= grp
->node
[1].entry
.next
;
676 cnt
= release_nodes(dev
, first
, end
, flags
);
679 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
684 EXPORT_SYMBOL_GPL(devres_release_group
);
687 * Custom devres actions allow inserting a simple function call
688 * into the teadown sequence.
691 struct action_devres
{
693 void (*action
)(void *);
696 static int devm_action_match(struct device
*dev
, void *res
, void *p
)
698 struct action_devres
*devres
= res
;
699 struct action_devres
*target
= p
;
701 return devres
->action
== target
->action
&&
702 devres
->data
== target
->data
;
705 static void devm_action_release(struct device
*dev
, void *res
)
707 struct action_devres
*devres
= res
;
709 devres
->action(devres
->data
);
713 * devm_add_action() - add a custom action to list of managed resources
714 * @dev: Device that owns the action
715 * @action: Function that should be called
716 * @data: Pointer to data passed to @action implementation
718 * This adds a custom action to the list of managed resources so that
719 * it gets executed as part of standard resource unwinding.
721 int devm_add_action(struct device
*dev
, void (*action
)(void *), void *data
)
723 struct action_devres
*devres
;
725 devres
= devres_alloc(devm_action_release
,
726 sizeof(struct action_devres
), GFP_KERNEL
);
731 devres
->action
= action
;
733 devres_add(dev
, devres
);
736 EXPORT_SYMBOL_GPL(devm_add_action
);
739 * devm_remove_action() - removes previously added custom action
740 * @dev: Device that owns the action
741 * @action: Function implementing the action
742 * @data: Pointer to data passed to @action implementation
744 * Removes instance of @action previously added by devm_add_action().
745 * Both action and data should match one of the existing entries.
747 void devm_remove_action(struct device
*dev
, void (*action
)(void *), void *data
)
749 struct action_devres devres
= {
754 WARN_ON(devres_destroy(dev
, devm_action_release
, devm_action_match
,
758 EXPORT_SYMBOL_GPL(devm_remove_action
);
761 * Managed kmalloc/kfree
763 static void devm_kmalloc_release(struct device
*dev
, void *res
)
768 static int devm_kmalloc_match(struct device
*dev
, void *res
, void *data
)
774 * devm_kmalloc - Resource-managed kmalloc
775 * @dev: Device to allocate memory for
776 * @size: Allocation size
777 * @gfp: Allocation gfp flags
779 * Managed kmalloc. Memory allocated with this function is
780 * automatically freed on driver detach. Like all other devres
781 * resources, guaranteed alignment is unsigned long long.
784 * Pointer to allocated memory on success, NULL on failure.
786 void * devm_kmalloc(struct device
*dev
, size_t size
, gfp_t gfp
)
790 /* use raw alloc_dr for kmalloc caller tracing */
791 dr
= alloc_dr(devm_kmalloc_release
, size
, gfp
, dev_to_node(dev
));
796 * This is named devm_kzalloc_release for historical reasons
797 * The initial implementation did not support kmalloc, only kzalloc
799 set_node_dbginfo(&dr
->node
, "devm_kzalloc_release", size
);
800 devres_add(dev
, dr
->data
);
803 EXPORT_SYMBOL_GPL(devm_kmalloc
);
806 * devm_kstrdup - Allocate resource managed space and
807 * copy an existing string into that.
808 * @dev: Device to allocate memory for
809 * @s: the string to duplicate
810 * @gfp: the GFP mask used in the devm_kmalloc() call when
813 * Pointer to allocated string on success, NULL on failure.
815 char *devm_kstrdup(struct device
*dev
, const char *s
, gfp_t gfp
)
823 size
= strlen(s
) + 1;
824 buf
= devm_kmalloc(dev
, size
, gfp
);
826 memcpy(buf
, s
, size
);
829 EXPORT_SYMBOL_GPL(devm_kstrdup
);
832 * devm_kvasprintf - Allocate resource managed space and format a string
834 * @dev: Device to allocate memory for
835 * @gfp: the GFP mask used in the devm_kmalloc() call when
837 * @fmt: The printf()-style format string
838 * @ap: Arguments for the format string
840 * Pointer to allocated string on success, NULL on failure.
842 char *devm_kvasprintf(struct device
*dev
, gfp_t gfp
, const char *fmt
,
850 len
= vsnprintf(NULL
, 0, fmt
, aq
);
853 p
= devm_kmalloc(dev
, len
+1, gfp
);
857 vsnprintf(p
, len
+1, fmt
, ap
);
861 EXPORT_SYMBOL(devm_kvasprintf
);
864 * devm_kasprintf - Allocate resource managed space and format a string
866 * @dev: Device to allocate memory for
867 * @gfp: the GFP mask used in the devm_kmalloc() call when
869 * @fmt: The printf()-style format string
870 * @...: Arguments for the format string
872 * Pointer to allocated string on success, NULL on failure.
874 char *devm_kasprintf(struct device
*dev
, gfp_t gfp
, const char *fmt
, ...)
880 p
= devm_kvasprintf(dev
, gfp
, fmt
, ap
);
885 EXPORT_SYMBOL_GPL(devm_kasprintf
);
888 * devm_kfree - Resource-managed kfree
889 * @dev: Device this memory belongs to
892 * Free memory allocated with devm_kmalloc().
894 void devm_kfree(struct device
*dev
, void *p
)
898 rc
= devres_destroy(dev
, devm_kmalloc_release
, devm_kmalloc_match
, p
);
901 EXPORT_SYMBOL_GPL(devm_kfree
);
904 * devm_kmemdup - Resource-managed kmemdup
905 * @dev: Device this memory belongs to
906 * @src: Memory region to duplicate
907 * @len: Memory region length
908 * @gfp: GFP mask to use
910 * Duplicate region of a memory using resource managed kmalloc
912 void *devm_kmemdup(struct device
*dev
, const void *src
, size_t len
, gfp_t gfp
)
916 p
= devm_kmalloc(dev
, len
, gfp
);
922 EXPORT_SYMBOL_GPL(devm_kmemdup
);
924 struct pages_devres
{
929 static int devm_pages_match(struct device
*dev
, void *res
, void *p
)
931 struct pages_devres
*devres
= res
;
932 struct pages_devres
*target
= p
;
934 return devres
->addr
== target
->addr
;
937 static void devm_pages_release(struct device
*dev
, void *res
)
939 struct pages_devres
*devres
= res
;
941 free_pages(devres
->addr
, devres
->order
);
945 * devm_get_free_pages - Resource-managed __get_free_pages
946 * @dev: Device to allocate memory for
947 * @gfp_mask: Allocation gfp flags
948 * @order: Allocation size is (1 << order) pages
950 * Managed get_free_pages. Memory allocated with this function is
951 * automatically freed on driver detach.
954 * Address of allocated memory on success, 0 on failure.
957 unsigned long devm_get_free_pages(struct device
*dev
,
958 gfp_t gfp_mask
, unsigned int order
)
960 struct pages_devres
*devres
;
963 addr
= __get_free_pages(gfp_mask
, order
);
968 devres
= devres_alloc(devm_pages_release
,
969 sizeof(struct pages_devres
), GFP_KERNEL
);
970 if (unlikely(!devres
)) {
971 free_pages(addr
, order
);
976 devres
->order
= order
;
978 devres_add(dev
, devres
);
981 EXPORT_SYMBOL_GPL(devm_get_free_pages
);
984 * devm_free_pages - Resource-managed free_pages
985 * @dev: Device this memory belongs to
986 * @addr: Memory to free
988 * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
989 * there is no need to supply the @order.
991 void devm_free_pages(struct device
*dev
, unsigned long addr
)
993 struct pages_devres devres
= { .addr
= addr
};
995 WARN_ON(devres_release(dev
, devm_pages_release
, devm_pages_match
,
998 EXPORT_SYMBOL_GPL(devm_free_pages
);
1000 static void devm_percpu_release(struct device
*dev
, void *pdata
)
1004 p
= *(void __percpu
**)pdata
;
1008 static int devm_percpu_match(struct device
*dev
, void *data
, void *p
)
1010 struct devres
*devr
= container_of(data
, struct devres
, data
);
1012 return *(void **)devr
->data
== p
;
1016 * __devm_alloc_percpu - Resource-managed alloc_percpu
1017 * @dev: Device to allocate per-cpu memory for
1018 * @size: Size of per-cpu memory to allocate
1019 * @align: Alignment of per-cpu memory to allocate
1021 * Managed alloc_percpu. Per-cpu memory allocated with this function is
1022 * automatically freed on driver detach.
1025 * Pointer to allocated memory on success, NULL on failure.
1027 void __percpu
*__devm_alloc_percpu(struct device
*dev
, size_t size
,
1031 void __percpu
*pcpu
;
1033 pcpu
= __alloc_percpu(size
, align
);
1037 p
= devres_alloc(devm_percpu_release
, sizeof(void *), GFP_KERNEL
);
1043 *(void __percpu
**)p
= pcpu
;
1049 EXPORT_SYMBOL_GPL(__devm_alloc_percpu
);
1052 * devm_free_percpu - Resource-managed free_percpu
1053 * @dev: Device this memory belongs to
1054 * @pdata: Per-cpu memory to free
1056 * Free memory allocated with devm_alloc_percpu().
1058 void devm_free_percpu(struct device
*dev
, void __percpu
*pdata
)
1060 WARN_ON(devres_destroy(dev
, devm_percpu_release
, devm_percpu_match
,
1063 EXPORT_SYMBOL_GPL(devm_free_percpu
);