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 unsigned long long data
[]; /* guarantee ull alignment */
32 struct devres_node node
[2];
38 #ifdef CONFIG_DEBUG_DEVRES
39 static int log_devres
= 0;
40 module_param_named(log
, log_devres
, int, S_IRUGO
| S_IWUSR
);
42 static void set_node_dbginfo(struct devres_node
*node
, const char *name
,
49 static void devres_log(struct device
*dev
, struct devres_node
*node
,
52 if (unlikely(log_devres
))
53 dev_err(dev
, "DEVRES %3s %p %s (%lu bytes)\n",
54 op
, node
, node
->name
, (unsigned long)node
->size
);
56 #else /* CONFIG_DEBUG_DEVRES */
57 #define set_node_dbginfo(node, n, s) do {} while (0)
58 #define devres_log(dev, node, op) do {} while (0)
59 #endif /* CONFIG_DEBUG_DEVRES */
62 * Release functions for devres group. These callbacks are used only
65 static void group_open_release(struct device
*dev
, void *res
)
70 static void group_close_release(struct device
*dev
, void *res
)
75 static struct devres_group
* node_to_group(struct devres_node
*node
)
77 if (node
->release
== &group_open_release
)
78 return container_of(node
, struct devres_group
, node
[0]);
79 if (node
->release
== &group_close_release
)
80 return container_of(node
, struct devres_group
, node
[1]);
84 static __always_inline
struct devres
* alloc_dr(dr_release_t release
,
85 size_t size
, gfp_t gfp
, int nid
)
90 /* We must catch any near-SIZE_MAX cases that could overflow. */
91 if (unlikely(check_add_overflow(sizeof(struct devres
), size
,
95 dr
= kmalloc_node_track_caller(tot_size
, gfp
, nid
);
99 memset(dr
, 0, offsetof(struct devres
, data
));
101 INIT_LIST_HEAD(&dr
->node
.entry
);
102 dr
->node
.release
= release
;
106 static void add_dr(struct device
*dev
, struct devres_node
*node
)
108 devres_log(dev
, node
, "ADD");
109 BUG_ON(!list_empty(&node
->entry
));
110 list_add_tail(&node
->entry
, &dev
->devres_head
);
113 #ifdef CONFIG_DEBUG_DEVRES
114 void * __devres_alloc_node(dr_release_t release
, size_t size
, gfp_t gfp
, int nid
,
119 dr
= alloc_dr(release
, size
, gfp
| __GFP_ZERO
, nid
);
122 set_node_dbginfo(&dr
->node
, name
, size
);
125 EXPORT_SYMBOL_GPL(__devres_alloc_node
);
128 * devres_alloc - Allocate device resource data
129 * @release: Release function devres will be associated with
130 * @size: Allocation size
131 * @gfp: Allocation flags
134 * Allocate devres of @size bytes. The allocated area is zeroed, then
135 * associated with @release. The returned pointer can be passed to
136 * other devres_*() functions.
139 * Pointer to allocated devres on success, NULL on failure.
141 void * devres_alloc_node(dr_release_t release
, size_t size
, gfp_t gfp
, int nid
)
145 dr
= alloc_dr(release
, size
, gfp
| __GFP_ZERO
, nid
);
150 EXPORT_SYMBOL_GPL(devres_alloc_node
);
154 * devres_for_each_res - Resource iterator
155 * @dev: Device to iterate resource from
156 * @release: Look for resources associated with this release function
157 * @match: Match function (optional)
158 * @match_data: Data for the match function
159 * @fn: Function to be called for each matched resource.
160 * @data: Data for @fn, the 3rd parameter of @fn
162 * Call @fn for each devres of @dev which is associated with @release
163 * and for which @match returns 1.
168 void devres_for_each_res(struct device
*dev
, dr_release_t release
,
169 dr_match_t match
, void *match_data
,
170 void (*fn
)(struct device
*, void *, void *),
173 struct devres_node
*node
;
174 struct devres_node
*tmp
;
180 spin_lock_irqsave(&dev
->devres_lock
, flags
);
181 list_for_each_entry_safe_reverse(node
, tmp
,
182 &dev
->devres_head
, entry
) {
183 struct devres
*dr
= container_of(node
, struct devres
, node
);
185 if (node
->release
!= release
)
187 if (match
&& !match(dev
, dr
->data
, match_data
))
189 fn(dev
, dr
->data
, data
);
191 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
193 EXPORT_SYMBOL_GPL(devres_for_each_res
);
196 * devres_free - Free device resource data
197 * @res: Pointer to devres data to free
199 * Free devres created with devres_alloc().
201 void devres_free(void *res
)
204 struct devres
*dr
= container_of(res
, struct devres
, data
);
206 BUG_ON(!list_empty(&dr
->node
.entry
));
210 EXPORT_SYMBOL_GPL(devres_free
);
213 * devres_add - Register device resource
214 * @dev: Device to add resource to
215 * @res: Resource to register
217 * Register devres @res to @dev. @res should have been allocated
218 * using devres_alloc(). On driver detach, the associated release
219 * function will be invoked and devres will be freed automatically.
221 void devres_add(struct device
*dev
, void *res
)
223 struct devres
*dr
= container_of(res
, struct devres
, data
);
226 spin_lock_irqsave(&dev
->devres_lock
, flags
);
227 add_dr(dev
, &dr
->node
);
228 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
230 EXPORT_SYMBOL_GPL(devres_add
);
232 static struct devres
*find_dr(struct device
*dev
, dr_release_t release
,
233 dr_match_t match
, void *match_data
)
235 struct devres_node
*node
;
237 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
238 struct devres
*dr
= container_of(node
, struct devres
, node
);
240 if (node
->release
!= release
)
242 if (match
&& !match(dev
, dr
->data
, match_data
))
251 * devres_find - Find device resource
252 * @dev: Device to lookup resource from
253 * @release: Look for resources associated with this release function
254 * @match: Match function (optional)
255 * @match_data: Data for the match function
257 * Find the latest devres of @dev which is associated with @release
258 * and for which @match returns 1. If @match is NULL, it's considered
262 * Pointer to found devres, NULL if not found.
264 void * devres_find(struct device
*dev
, dr_release_t release
,
265 dr_match_t match
, void *match_data
)
270 spin_lock_irqsave(&dev
->devres_lock
, flags
);
271 dr
= find_dr(dev
, release
, match
, match_data
);
272 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
278 EXPORT_SYMBOL_GPL(devres_find
);
281 * devres_get - Find devres, if non-existent, add one atomically
282 * @dev: Device to lookup or add devres for
283 * @new_res: Pointer to new initialized devres to add if not found
284 * @match: Match function (optional)
285 * @match_data: Data for the match function
287 * Find the latest devres of @dev which has the same release function
288 * as @new_res and for which @match return 1. If found, @new_res is
289 * freed; otherwise, @new_res is added atomically.
292 * Pointer to found or added devres.
294 void * devres_get(struct device
*dev
, void *new_res
,
295 dr_match_t match
, void *match_data
)
297 struct devres
*new_dr
= container_of(new_res
, struct devres
, data
);
301 spin_lock_irqsave(&dev
->devres_lock
, flags
);
302 dr
= find_dr(dev
, new_dr
->node
.release
, match
, match_data
);
304 add_dr(dev
, &new_dr
->node
);
308 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
309 devres_free(new_res
);
313 EXPORT_SYMBOL_GPL(devres_get
);
316 * devres_remove - Find a device resource and remove it
317 * @dev: Device to find resource from
318 * @release: Look for resources associated with this release function
319 * @match: Match function (optional)
320 * @match_data: Data for the match function
322 * Find the latest devres of @dev associated with @release and for
323 * which @match returns 1. If @match is NULL, it's considered to
324 * match all. If found, the resource is removed atomically and
328 * Pointer to removed devres on success, NULL if not found.
330 void * devres_remove(struct device
*dev
, dr_release_t release
,
331 dr_match_t match
, void *match_data
)
336 spin_lock_irqsave(&dev
->devres_lock
, flags
);
337 dr
= find_dr(dev
, release
, match
, match_data
);
339 list_del_init(&dr
->node
.entry
);
340 devres_log(dev
, &dr
->node
, "REM");
342 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
348 EXPORT_SYMBOL_GPL(devres_remove
);
351 * devres_destroy - Find a device resource and destroy it
352 * @dev: Device to find resource from
353 * @release: Look for resources associated with this release function
354 * @match: Match function (optional)
355 * @match_data: Data for the match function
357 * Find the latest devres of @dev associated with @release and for
358 * which @match returns 1. If @match is NULL, it's considered to
359 * match all. If found, the resource is removed atomically and freed.
361 * Note that the release function for the resource will not be called,
362 * only the devres-allocated data will be freed. The caller becomes
363 * responsible for freeing any other data.
366 * 0 if devres is found and freed, -ENOENT if not found.
368 int devres_destroy(struct device
*dev
, dr_release_t release
,
369 dr_match_t match
, void *match_data
)
373 res
= devres_remove(dev
, release
, match
, match_data
);
380 EXPORT_SYMBOL_GPL(devres_destroy
);
384 * devres_release - Find a device resource and destroy it, calling release
385 * @dev: Device to find resource from
386 * @release: Look for resources associated with this release function
387 * @match: Match function (optional)
388 * @match_data: Data for the match function
390 * Find the latest devres of @dev associated with @release and for
391 * which @match returns 1. If @match is NULL, it's considered to
392 * match all. If found, the resource is removed atomically, the
393 * release function called and the resource freed.
396 * 0 if devres is found and freed, -ENOENT if not found.
398 int devres_release(struct device
*dev
, dr_release_t release
,
399 dr_match_t match
, void *match_data
)
403 res
= devres_remove(dev
, release
, match
, match_data
);
407 (*release
)(dev
, res
);
411 EXPORT_SYMBOL_GPL(devres_release
);
413 static int remove_nodes(struct device
*dev
,
414 struct list_head
*first
, struct list_head
*end
,
415 struct list_head
*todo
)
417 int cnt
= 0, nr_groups
= 0;
418 struct list_head
*cur
;
420 /* First pass - move normal devres entries to @todo and clear
421 * devres_group colors.
425 struct devres_node
*node
;
426 struct devres_group
*grp
;
428 node
= list_entry(cur
, struct devres_node
, entry
);
431 grp
= node_to_group(node
);
433 /* clear color of group markers in the first pass */
437 /* regular devres entry */
438 if (&node
->entry
== first
)
440 list_move_tail(&node
->entry
, todo
);
448 /* Second pass - Scan groups and color them. A group gets
449 * color value of two iff the group is wholly contained in
450 * [cur, end). That is, for a closed group, both opening and
451 * closing markers should be in the range, while just the
452 * opening marker is enough for an open group.
456 struct devres_node
*node
;
457 struct devres_group
*grp
;
459 node
= list_entry(cur
, struct devres_node
, entry
);
462 grp
= node_to_group(node
);
463 BUG_ON(!grp
|| list_empty(&grp
->node
[0].entry
));
466 if (list_empty(&grp
->node
[1].entry
))
469 BUG_ON(grp
->color
<= 0 || grp
->color
> 2);
470 if (grp
->color
== 2) {
471 /* No need to update cur or end. The removed
472 * nodes are always before both.
474 list_move_tail(&grp
->node
[0].entry
, todo
);
475 list_del_init(&grp
->node
[1].entry
);
482 static int release_nodes(struct device
*dev
, struct list_head
*first
,
483 struct list_head
*end
, unsigned long flags
)
484 __releases(&dev
->devres_lock
)
488 struct devres
*dr
, *tmp
;
490 cnt
= remove_nodes(dev
, first
, end
, &todo
);
492 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
494 /* Release. Note that both devres and devres_group are
495 * handled as devres in the following loop. This is safe.
497 list_for_each_entry_safe_reverse(dr
, tmp
, &todo
, node
.entry
) {
498 devres_log(dev
, &dr
->node
, "REL");
499 dr
->node
.release(dev
, dr
->data
);
507 * devres_release_all - Release all managed resources
508 * @dev: Device to release resources for
510 * Release all resources associated with @dev. This function is
511 * called on driver detach.
513 int devres_release_all(struct device
*dev
)
517 /* Looks like an uninitialized device structure */
518 if (WARN_ON(dev
->devres_head
.next
== NULL
))
520 spin_lock_irqsave(&dev
->devres_lock
, flags
);
521 return release_nodes(dev
, dev
->devres_head
.next
, &dev
->devres_head
,
526 * devres_open_group - Open a new devres group
527 * @dev: Device to open devres group for
529 * @gfp: Allocation flags
531 * Open a new devres group for @dev with @id. For @id, using a
532 * pointer to an object which won't be used for another group is
533 * recommended. If @id is NULL, address-wise unique ID is created.
536 * ID of the new group, NULL on failure.
538 void * devres_open_group(struct device
*dev
, void *id
, gfp_t gfp
)
540 struct devres_group
*grp
;
543 grp
= kmalloc(sizeof(*grp
), gfp
);
547 grp
->node
[0].release
= &group_open_release
;
548 grp
->node
[1].release
= &group_close_release
;
549 INIT_LIST_HEAD(&grp
->node
[0].entry
);
550 INIT_LIST_HEAD(&grp
->node
[1].entry
);
551 set_node_dbginfo(&grp
->node
[0], "grp<", 0);
552 set_node_dbginfo(&grp
->node
[1], "grp>", 0);
557 spin_lock_irqsave(&dev
->devres_lock
, flags
);
558 add_dr(dev
, &grp
->node
[0]);
559 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
562 EXPORT_SYMBOL_GPL(devres_open_group
);
564 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
565 static struct devres_group
* find_group(struct device
*dev
, void *id
)
567 struct devres_node
*node
;
569 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
570 struct devres_group
*grp
;
572 if (node
->release
!= &group_open_release
)
575 grp
= container_of(node
, struct devres_group
, node
[0]);
580 } else if (list_empty(&grp
->node
[1].entry
))
588 * devres_close_group - Close a devres group
589 * @dev: Device to close devres group for
590 * @id: ID of target group, can be NULL
592 * Close the group identified by @id. If @id is NULL, the latest open
595 void devres_close_group(struct device
*dev
, void *id
)
597 struct devres_group
*grp
;
600 spin_lock_irqsave(&dev
->devres_lock
, flags
);
602 grp
= find_group(dev
, id
);
604 add_dr(dev
, &grp
->node
[1]);
608 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
610 EXPORT_SYMBOL_GPL(devres_close_group
);
613 * devres_remove_group - Remove a devres group
614 * @dev: Device to remove group for
615 * @id: ID of target group, can be NULL
617 * Remove the group identified by @id. If @id is NULL, the latest
618 * open group is selected. Note that removing a group doesn't affect
619 * any other resources.
621 void devres_remove_group(struct device
*dev
, void *id
)
623 struct devres_group
*grp
;
626 spin_lock_irqsave(&dev
->devres_lock
, flags
);
628 grp
= find_group(dev
, id
);
630 list_del_init(&grp
->node
[0].entry
);
631 list_del_init(&grp
->node
[1].entry
);
632 devres_log(dev
, &grp
->node
[0], "REM");
636 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
640 EXPORT_SYMBOL_GPL(devres_remove_group
);
643 * devres_release_group - Release resources in a devres group
644 * @dev: Device to release group for
645 * @id: ID of target group, can be NULL
647 * Release all resources in the group identified by @id. If @id is
648 * NULL, the latest open group is selected. The selected group and
649 * groups properly nested inside the selected group are removed.
652 * The number of released non-group resources.
654 int devres_release_group(struct device
*dev
, void *id
)
656 struct devres_group
*grp
;
660 spin_lock_irqsave(&dev
->devres_lock
, flags
);
662 grp
= find_group(dev
, id
);
664 struct list_head
*first
= &grp
->node
[0].entry
;
665 struct list_head
*end
= &dev
->devres_head
;
667 if (!list_empty(&grp
->node
[1].entry
))
668 end
= grp
->node
[1].entry
.next
;
670 cnt
= release_nodes(dev
, first
, end
, flags
);
673 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
678 EXPORT_SYMBOL_GPL(devres_release_group
);
681 * Custom devres actions allow inserting a simple function call
682 * into the teadown sequence.
685 struct action_devres
{
687 void (*action
)(void *);
690 static int devm_action_match(struct device
*dev
, void *res
, void *p
)
692 struct action_devres
*devres
= res
;
693 struct action_devres
*target
= p
;
695 return devres
->action
== target
->action
&&
696 devres
->data
== target
->data
;
699 static void devm_action_release(struct device
*dev
, void *res
)
701 struct action_devres
*devres
= res
;
703 devres
->action(devres
->data
);
707 * devm_add_action() - add a custom action to list of managed resources
708 * @dev: Device that owns the action
709 * @action: Function that should be called
710 * @data: Pointer to data passed to @action implementation
712 * This adds a custom action to the list of managed resources so that
713 * it gets executed as part of standard resource unwinding.
715 int devm_add_action(struct device
*dev
, void (*action
)(void *), void *data
)
717 struct action_devres
*devres
;
719 devres
= devres_alloc(devm_action_release
,
720 sizeof(struct action_devres
), GFP_KERNEL
);
725 devres
->action
= action
;
727 devres_add(dev
, devres
);
730 EXPORT_SYMBOL_GPL(devm_add_action
);
733 * devm_remove_action() - removes previously added custom action
734 * @dev: Device that owns the action
735 * @action: Function implementing the action
736 * @data: Pointer to data passed to @action implementation
738 * Removes instance of @action previously added by devm_add_action().
739 * Both action and data should match one of the existing entries.
741 void devm_remove_action(struct device
*dev
, void (*action
)(void *), void *data
)
743 struct action_devres devres
= {
748 WARN_ON(devres_destroy(dev
, devm_action_release
, devm_action_match
,
752 EXPORT_SYMBOL_GPL(devm_remove_action
);
755 * Managed kmalloc/kfree
757 static void devm_kmalloc_release(struct device
*dev
, void *res
)
762 static int devm_kmalloc_match(struct device
*dev
, void *res
, void *data
)
768 * devm_kmalloc - Resource-managed kmalloc
769 * @dev: Device to allocate memory for
770 * @size: Allocation size
771 * @gfp: Allocation gfp flags
773 * Managed kmalloc. Memory allocated with this function is
774 * automatically freed on driver detach. Like all other devres
775 * resources, guaranteed alignment is unsigned long long.
778 * Pointer to allocated memory on success, NULL on failure.
780 void * devm_kmalloc(struct device
*dev
, size_t size
, gfp_t gfp
)
784 /* use raw alloc_dr for kmalloc caller tracing */
785 dr
= alloc_dr(devm_kmalloc_release
, size
, gfp
, dev_to_node(dev
));
790 * This is named devm_kzalloc_release for historical reasons
791 * The initial implementation did not support kmalloc, only kzalloc
793 set_node_dbginfo(&dr
->node
, "devm_kzalloc_release", size
);
794 devres_add(dev
, dr
->data
);
797 EXPORT_SYMBOL_GPL(devm_kmalloc
);
800 * devm_kstrdup - Allocate resource managed space and
801 * copy an existing string into that.
802 * @dev: Device to allocate memory for
803 * @s: the string to duplicate
804 * @gfp: the GFP mask used in the devm_kmalloc() call when
807 * Pointer to allocated string on success, NULL on failure.
809 char *devm_kstrdup(struct device
*dev
, const char *s
, gfp_t gfp
)
817 size
= strlen(s
) + 1;
818 buf
= devm_kmalloc(dev
, size
, gfp
);
820 memcpy(buf
, s
, size
);
823 EXPORT_SYMBOL_GPL(devm_kstrdup
);
826 * devm_kvasprintf - Allocate resource managed space and format a string
828 * @dev: Device to allocate memory for
829 * @gfp: the GFP mask used in the devm_kmalloc() call when
831 * @fmt: The printf()-style format string
832 * @ap: Arguments for the format string
834 * Pointer to allocated string on success, NULL on failure.
836 char *devm_kvasprintf(struct device
*dev
, gfp_t gfp
, const char *fmt
,
844 len
= vsnprintf(NULL
, 0, fmt
, aq
);
847 p
= devm_kmalloc(dev
, len
+1, gfp
);
851 vsnprintf(p
, len
+1, fmt
, ap
);
855 EXPORT_SYMBOL(devm_kvasprintf
);
858 * devm_kasprintf - Allocate resource managed space and format a string
860 * @dev: Device to allocate memory for
861 * @gfp: the GFP mask used in the devm_kmalloc() call when
863 * @fmt: The printf()-style format string
864 * @...: Arguments for the format string
866 * Pointer to allocated string on success, NULL on failure.
868 char *devm_kasprintf(struct device
*dev
, gfp_t gfp
, const char *fmt
, ...)
874 p
= devm_kvasprintf(dev
, gfp
, fmt
, ap
);
879 EXPORT_SYMBOL_GPL(devm_kasprintf
);
882 * devm_kfree - Resource-managed kfree
883 * @dev: Device this memory belongs to
886 * Free memory allocated with devm_kmalloc().
888 void devm_kfree(struct device
*dev
, void *p
)
892 rc
= devres_destroy(dev
, devm_kmalloc_release
, devm_kmalloc_match
, p
);
895 EXPORT_SYMBOL_GPL(devm_kfree
);
898 * devm_kmemdup - Resource-managed kmemdup
899 * @dev: Device this memory belongs to
900 * @src: Memory region to duplicate
901 * @len: Memory region length
902 * @gfp: GFP mask to use
904 * Duplicate region of a memory using resource managed kmalloc
906 void *devm_kmemdup(struct device
*dev
, const void *src
, size_t len
, gfp_t gfp
)
910 p
= devm_kmalloc(dev
, len
, gfp
);
916 EXPORT_SYMBOL_GPL(devm_kmemdup
);
918 struct pages_devres
{
923 static int devm_pages_match(struct device
*dev
, void *res
, void *p
)
925 struct pages_devres
*devres
= res
;
926 struct pages_devres
*target
= p
;
928 return devres
->addr
== target
->addr
;
931 static void devm_pages_release(struct device
*dev
, void *res
)
933 struct pages_devres
*devres
= res
;
935 free_pages(devres
->addr
, devres
->order
);
939 * devm_get_free_pages - Resource-managed __get_free_pages
940 * @dev: Device to allocate memory for
941 * @gfp_mask: Allocation gfp flags
942 * @order: Allocation size is (1 << order) pages
944 * Managed get_free_pages. Memory allocated with this function is
945 * automatically freed on driver detach.
948 * Address of allocated memory on success, 0 on failure.
951 unsigned long devm_get_free_pages(struct device
*dev
,
952 gfp_t gfp_mask
, unsigned int order
)
954 struct pages_devres
*devres
;
957 addr
= __get_free_pages(gfp_mask
, order
);
962 devres
= devres_alloc(devm_pages_release
,
963 sizeof(struct pages_devres
), GFP_KERNEL
);
964 if (unlikely(!devres
)) {
965 free_pages(addr
, order
);
970 devres
->order
= order
;
972 devres_add(dev
, devres
);
975 EXPORT_SYMBOL_GPL(devm_get_free_pages
);
978 * devm_free_pages - Resource-managed free_pages
979 * @dev: Device this memory belongs to
980 * @addr: Memory to free
982 * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
983 * there is no need to supply the @order.
985 void devm_free_pages(struct device
*dev
, unsigned long addr
)
987 struct pages_devres devres
= { .addr
= addr
};
989 WARN_ON(devres_release(dev
, devm_pages_release
, devm_pages_match
,
992 EXPORT_SYMBOL_GPL(devm_free_pages
);
994 static void devm_percpu_release(struct device
*dev
, void *pdata
)
998 p
= *(void __percpu
**)pdata
;
1002 static int devm_percpu_match(struct device
*dev
, void *data
, void *p
)
1004 struct devres
*devr
= container_of(data
, struct devres
, data
);
1006 return *(void **)devr
->data
== p
;
1010 * __devm_alloc_percpu - Resource-managed alloc_percpu
1011 * @dev: Device to allocate per-cpu memory for
1012 * @size: Size of per-cpu memory to allocate
1013 * @align: Alignment of per-cpu memory to allocate
1015 * Managed alloc_percpu. Per-cpu memory allocated with this function is
1016 * automatically freed on driver detach.
1019 * Pointer to allocated memory on success, NULL on failure.
1021 void __percpu
*__devm_alloc_percpu(struct device
*dev
, size_t size
,
1025 void __percpu
*pcpu
;
1027 pcpu
= __alloc_percpu(size
, align
);
1031 p
= devres_alloc(devm_percpu_release
, sizeof(void *), GFP_KERNEL
);
1037 *(void __percpu
**)p
= pcpu
;
1043 EXPORT_SYMBOL_GPL(__devm_alloc_percpu
);
1046 * devm_free_percpu - Resource-managed free_percpu
1047 * @dev: Device this memory belongs to
1048 * @pdata: Per-cpu memory to free
1050 * Free memory allocated with devm_alloc_percpu().
1052 void devm_free_percpu(struct device
*dev
, void __percpu
*pdata
)
1054 WARN_ON(devres_destroy(dev
, devm_percpu_release
, devm_percpu_match
,
1057 EXPORT_SYMBOL_GPL(devm_free_percpu
);