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>
14 #include <asm/sections.h>
19 struct list_head entry;
21 #ifdef CONFIG_DEBUG_DEVRES
28 struct devres_node node;
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().
36 u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
40 struct devres_node node[2];
46 #ifdef CONFIG_DEBUG_DEVRES
47 static int log_devres = 0;
48 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
50 static void set_node_dbginfo(struct devres_node *node, const char *name,
57 static void devres_log(struct device *dev, struct devres_node *node,
60 if (unlikely(log_devres))
61 dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
62 op, node, node->name, (unsigned long)node->size);
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 */
70 * Release functions for devres group. These callbacks are used only
73 static void group_open_release(struct device *dev, void *res)
78 static void group_close_release(struct device *dev, void *res)
83 static struct devres_group * node_to_group(struct devres_node *node)
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]);
92 static bool check_dr_size(size_t size, size_t *tot_size)
94 /* We must catch any near-SIZE_MAX cases that could overflow. */
95 if (unlikely(check_add_overflow(sizeof(struct devres),
102 static __always_inline struct devres * alloc_dr(dr_release_t release,
103 size_t size, gfp_t gfp, int nid)
108 if (!check_dr_size(size, &tot_size))
111 dr = kmalloc_node_track_caller(tot_size, gfp, nid);
115 memset(dr, 0, offsetof(struct devres, data));
117 INIT_LIST_HEAD(&dr->node.entry);
118 dr->node.release = release;
122 static void add_dr(struct device *dev, struct devres_node *node)
124 devres_log(dev, node, "ADD");
125 BUG_ON(!list_empty(&node->entry));
126 list_add_tail(&node->entry, &dev->devres_head);
129 #ifdef CONFIG_DEBUG_DEVRES
130 void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
135 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
138 set_node_dbginfo(&dr->node, name, size);
141 EXPORT_SYMBOL_GPL(__devres_alloc_node);
144 * devres_alloc - Allocate device resource data
145 * @release: Release function devres will be associated with
146 * @size: Allocation size
147 * @gfp: Allocation flags
150 * Allocate devres of @size bytes. The allocated area is zeroed, then
151 * associated with @release. The returned pointer can be passed to
152 * other devres_*() functions.
155 * Pointer to allocated devres on success, NULL on failure.
157 void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
161 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
166 EXPORT_SYMBOL_GPL(devres_alloc_node);
170 * devres_for_each_res - Resource iterator
171 * @dev: Device to iterate resource from
172 * @release: Look for resources associated with this release function
173 * @match: Match function (optional)
174 * @match_data: Data for the match function
175 * @fn: Function to be called for each matched resource.
176 * @data: Data for @fn, the 3rd parameter of @fn
178 * Call @fn for each devres of @dev which is associated with @release
179 * and for which @match returns 1.
184 void devres_for_each_res(struct device *dev, dr_release_t release,
185 dr_match_t match, void *match_data,
186 void (*fn)(struct device *, void *, void *),
189 struct devres_node *node;
190 struct devres_node *tmp;
196 spin_lock_irqsave(&dev->devres_lock, flags);
197 list_for_each_entry_safe_reverse(node, tmp,
198 &dev->devres_head, entry) {
199 struct devres *dr = container_of(node, struct devres, node);
201 if (node->release != release)
203 if (match && !match(dev, dr->data, match_data))
205 fn(dev, dr->data, data);
207 spin_unlock_irqrestore(&dev->devres_lock, flags);
209 EXPORT_SYMBOL_GPL(devres_for_each_res);
212 * devres_free - Free device resource data
213 * @res: Pointer to devres data to free
215 * Free devres created with devres_alloc().
217 void devres_free(void *res)
220 struct devres *dr = container_of(res, struct devres, data);
222 BUG_ON(!list_empty(&dr->node.entry));
226 EXPORT_SYMBOL_GPL(devres_free);
229 * devres_add - Register device resource
230 * @dev: Device to add resource to
231 * @res: Resource to register
233 * Register devres @res to @dev. @res should have been allocated
234 * using devres_alloc(). On driver detach, the associated release
235 * function will be invoked and devres will be freed automatically.
237 void devres_add(struct device *dev, void *res)
239 struct devres *dr = container_of(res, struct devres, data);
242 spin_lock_irqsave(&dev->devres_lock, flags);
243 add_dr(dev, &dr->node);
244 spin_unlock_irqrestore(&dev->devres_lock, flags);
246 EXPORT_SYMBOL_GPL(devres_add);
248 static struct devres *find_dr(struct device *dev, dr_release_t release,
249 dr_match_t match, void *match_data)
251 struct devres_node *node;
253 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
254 struct devres *dr = container_of(node, struct devres, node);
256 if (node->release != release)
258 if (match && !match(dev, dr->data, match_data))
267 * devres_find - Find device resource
268 * @dev: Device to lookup resource from
269 * @release: Look for resources associated with this release function
270 * @match: Match function (optional)
271 * @match_data: Data for the match function
273 * Find the latest devres of @dev which is associated with @release
274 * and for which @match returns 1. If @match is NULL, it's considered
278 * Pointer to found devres, NULL if not found.
280 void * devres_find(struct device *dev, dr_release_t release,
281 dr_match_t match, void *match_data)
286 spin_lock_irqsave(&dev->devres_lock, flags);
287 dr = find_dr(dev, release, match, match_data);
288 spin_unlock_irqrestore(&dev->devres_lock, flags);
294 EXPORT_SYMBOL_GPL(devres_find);
297 * devres_get - Find devres, if non-existent, add one atomically
298 * @dev: Device to lookup or add devres for
299 * @new_res: Pointer to new initialized devres to add if not found
300 * @match: Match function (optional)
301 * @match_data: Data for the match function
303 * Find the latest devres of @dev which has the same release function
304 * as @new_res and for which @match return 1. If found, @new_res is
305 * freed; otherwise, @new_res is added atomically.
308 * Pointer to found or added devres.
310 void * devres_get(struct device *dev, void *new_res,
311 dr_match_t match, void *match_data)
313 struct devres *new_dr = container_of(new_res, struct devres, data);
317 spin_lock_irqsave(&dev->devres_lock, flags);
318 dr = find_dr(dev, new_dr->node.release, match, match_data);
320 add_dr(dev, &new_dr->node);
324 spin_unlock_irqrestore(&dev->devres_lock, flags);
325 devres_free(new_res);
329 EXPORT_SYMBOL_GPL(devres_get);
332 * devres_remove - Find a device resource and remove it
333 * @dev: Device to find resource from
334 * @release: Look for resources associated with this release function
335 * @match: Match function (optional)
336 * @match_data: Data for the match function
338 * Find the latest devres of @dev associated with @release and for
339 * which @match returns 1. If @match is NULL, it's considered to
340 * match all. If found, the resource is removed atomically and
344 * Pointer to removed devres on success, NULL if not found.
346 void * devres_remove(struct device *dev, dr_release_t release,
347 dr_match_t match, void *match_data)
352 spin_lock_irqsave(&dev->devres_lock, flags);
353 dr = find_dr(dev, release, match, match_data);
355 list_del_init(&dr->node.entry);
356 devres_log(dev, &dr->node, "REM");
358 spin_unlock_irqrestore(&dev->devres_lock, flags);
364 EXPORT_SYMBOL_GPL(devres_remove);
367 * devres_destroy - Find a device resource and destroy it
368 * @dev: Device to find resource from
369 * @release: Look for resources associated with this release function
370 * @match: Match function (optional)
371 * @match_data: Data for the match function
373 * Find the latest devres of @dev associated with @release and for
374 * which @match returns 1. If @match is NULL, it's considered to
375 * match all. If found, the resource is removed atomically and freed.
377 * Note that the release function for the resource will not be called,
378 * only the devres-allocated data will be freed. The caller becomes
379 * responsible for freeing any other data.
382 * 0 if devres is found and freed, -ENOENT if not found.
384 int devres_destroy(struct device *dev, dr_release_t release,
385 dr_match_t match, void *match_data)
389 res = devres_remove(dev, release, match, match_data);
396 EXPORT_SYMBOL_GPL(devres_destroy);
400 * devres_release - Find a device resource and destroy it, calling release
401 * @dev: Device to find resource from
402 * @release: Look for resources associated with this release function
403 * @match: Match function (optional)
404 * @match_data: Data for the match function
406 * Find the latest devres of @dev associated with @release and for
407 * which @match returns 1. If @match is NULL, it's considered to
408 * match all. If found, the resource is removed atomically, the
409 * release function called and the resource freed.
412 * 0 if devres is found and freed, -ENOENT if not found.
414 int devres_release(struct device *dev, dr_release_t release,
415 dr_match_t match, void *match_data)
419 res = devres_remove(dev, release, match, match_data);
423 (*release)(dev, res);
427 EXPORT_SYMBOL_GPL(devres_release);
429 static int remove_nodes(struct device *dev,
430 struct list_head *first, struct list_head *end,
431 struct list_head *todo)
433 int cnt = 0, nr_groups = 0;
434 struct list_head *cur;
436 /* First pass - move normal devres entries to @todo and clear
437 * devres_group colors.
441 struct devres_node *node;
442 struct devres_group *grp;
444 node = list_entry(cur, struct devres_node, entry);
447 grp = node_to_group(node);
449 /* clear color of group markers in the first pass */
453 /* regular devres entry */
454 if (&node->entry == first)
456 list_move_tail(&node->entry, todo);
464 /* Second pass - Scan groups and color them. A group gets
465 * color value of two iff the group is wholly contained in
466 * [cur, end). That is, for a closed group, both opening and
467 * closing markers should be in the range, while just the
468 * opening marker is enough for an open group.
472 struct devres_node *node;
473 struct devres_group *grp;
475 node = list_entry(cur, struct devres_node, entry);
478 grp = node_to_group(node);
479 BUG_ON(!grp || list_empty(&grp->node[0].entry));
482 if (list_empty(&grp->node[1].entry))
485 BUG_ON(grp->color <= 0 || grp->color > 2);
486 if (grp->color == 2) {
487 /* No need to update cur or end. The removed
488 * nodes are always before both.
490 list_move_tail(&grp->node[0].entry, todo);
491 list_del_init(&grp->node[1].entry);
498 static int release_nodes(struct device *dev, struct list_head *first,
499 struct list_head *end, unsigned long flags)
500 __releases(&dev->devres_lock)
504 struct devres *dr, *tmp;
506 cnt = remove_nodes(dev, first, end, &todo);
508 spin_unlock_irqrestore(&dev->devres_lock, flags);
510 /* Release. Note that both devres and devres_group are
511 * handled as devres in the following loop. This is safe.
513 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
514 devres_log(dev, &dr->node, "REL");
515 dr->node.release(dev, dr->data);
523 * devres_release_all - Release all managed resources
524 * @dev: Device to release resources for
526 * Release all resources associated with @dev. This function is
527 * called on driver detach.
529 int devres_release_all(struct device *dev)
533 /* Looks like an uninitialized device structure */
534 if (WARN_ON(dev->devres_head.next == NULL))
536 spin_lock_irqsave(&dev->devres_lock, flags);
537 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
542 * devres_open_group - Open a new devres group
543 * @dev: Device to open devres group for
545 * @gfp: Allocation flags
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.
552 * ID of the new group, NULL on failure.
554 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
556 struct devres_group *grp;
559 grp = kmalloc(sizeof(*grp), gfp);
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);
573 spin_lock_irqsave(&dev->devres_lock, flags);
574 add_dr(dev, &grp->node[0]);
575 spin_unlock_irqrestore(&dev->devres_lock, flags);
578 EXPORT_SYMBOL_GPL(devres_open_group);
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)
583 struct devres_node *node;
585 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
586 struct devres_group *grp;
588 if (node->release != &group_open_release)
591 grp = container_of(node, struct devres_group, node[0]);
596 } else if (list_empty(&grp->node[1].entry))
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
608 * Close the group identified by @id. If @id is NULL, the latest open
611 void devres_close_group(struct device *dev, void *id)
613 struct devres_group *grp;
616 spin_lock_irqsave(&dev->devres_lock, flags);
618 grp = find_group(dev, id);
620 add_dr(dev, &grp->node[1]);
624 spin_unlock_irqrestore(&dev->devres_lock, flags);
626 EXPORT_SYMBOL_GPL(devres_close_group);
629 * devres_remove_group - Remove a devres group
630 * @dev: Device to remove group for
631 * @id: ID of target group, can be NULL
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.
637 void devres_remove_group(struct device *dev, void *id)
639 struct devres_group *grp;
642 spin_lock_irqsave(&dev->devres_lock, flags);
644 grp = find_group(dev, id);
646 list_del_init(&grp->node[0].entry);
647 list_del_init(&grp->node[1].entry);
648 devres_log(dev, &grp->node[0], "REM");
652 spin_unlock_irqrestore(&dev->devres_lock, flags);
656 EXPORT_SYMBOL_GPL(devres_remove_group);
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
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.
668 * The number of released non-group resources.
670 int devres_release_group(struct device *dev, void *id)
672 struct devres_group *grp;
676 spin_lock_irqsave(&dev->devres_lock, flags);
678 grp = find_group(dev, id);
680 struct list_head *first = &grp->node[0].entry;
681 struct list_head *end = &dev->devres_head;
683 if (!list_empty(&grp->node[1].entry))
684 end = grp->node[1].entry.next;
686 cnt = release_nodes(dev, first, end, flags);
689 spin_unlock_irqrestore(&dev->devres_lock, flags);
694 EXPORT_SYMBOL_GPL(devres_release_group);
697 * Custom devres actions allow inserting a simple function call
698 * into the teadown sequence.
701 struct action_devres {
703 void (*action)(void *);
706 static int devm_action_match(struct device *dev, void *res, void *p)
708 struct action_devres *devres = res;
709 struct action_devres *target = p;
711 return devres->action == target->action &&
712 devres->data == target->data;
715 static void devm_action_release(struct device *dev, void *res)
717 struct action_devres *devres = res;
719 devres->action(devres->data);
723 * devm_add_action() - add a custom action to list of managed resources
724 * @dev: Device that owns the action
725 * @action: Function that should be called
726 * @data: Pointer to data passed to @action implementation
728 * This adds a custom action to the list of managed resources so that
729 * it gets executed as part of standard resource unwinding.
731 int devm_add_action(struct device *dev, void (*action)(void *), void *data)
733 struct action_devres *devres;
735 devres = devres_alloc(devm_action_release,
736 sizeof(struct action_devres), GFP_KERNEL);
741 devres->action = action;
743 devres_add(dev, devres);
746 EXPORT_SYMBOL_GPL(devm_add_action);
749 * devm_remove_action() - removes previously added custom action
750 * @dev: Device that owns the action
751 * @action: Function implementing the action
752 * @data: Pointer to data passed to @action implementation
754 * Removes instance of @action previously added by devm_add_action().
755 * Both action and data should match one of the existing entries.
757 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
759 struct action_devres devres = {
764 WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
767 EXPORT_SYMBOL_GPL(devm_remove_action);
770 * devm_release_action() - release previously added custom action
771 * @dev: Device that owns the action
772 * @action: Function implementing the action
773 * @data: Pointer to data passed to @action implementation
775 * Releases and removes instance of @action previously added by
776 * devm_add_action(). Both action and data should match one of the
779 void devm_release_action(struct device *dev, void (*action)(void *), void *data)
781 struct action_devres devres = {
786 WARN_ON(devres_release(dev, devm_action_release, devm_action_match,
790 EXPORT_SYMBOL_GPL(devm_release_action);
793 * Managed kmalloc/kfree
795 static void devm_kmalloc_release(struct device *dev, void *res)
800 static int devm_kmalloc_match(struct device *dev, void *res, void *data)
806 * devm_kmalloc - Resource-managed kmalloc
807 * @dev: Device to allocate memory for
808 * @size: Allocation size
809 * @gfp: Allocation gfp flags
811 * Managed kmalloc. Memory allocated with this function is
812 * automatically freed on driver detach. Like all other devres
813 * resources, guaranteed alignment is unsigned long long.
816 * Pointer to allocated memory on success, NULL on failure.
818 void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
823 return ZERO_SIZE_PTR;
825 /* use raw alloc_dr for kmalloc caller tracing */
826 dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
831 * This is named devm_kzalloc_release for historical reasons
832 * The initial implementation did not support kmalloc, only kzalloc
834 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
835 devres_add(dev, dr->data);
838 EXPORT_SYMBOL_GPL(devm_kmalloc);
841 * devm_kstrdup - Allocate resource managed space and
842 * copy an existing string into that.
843 * @dev: Device to allocate memory for
844 * @s: the string to duplicate
845 * @gfp: the GFP mask used in the devm_kmalloc() call when
848 * Pointer to allocated string on success, NULL on failure.
850 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
858 size = strlen(s) + 1;
859 buf = devm_kmalloc(dev, size, gfp);
861 memcpy(buf, s, size);
864 EXPORT_SYMBOL_GPL(devm_kstrdup);
867 * devm_kstrdup_const - resource managed conditional string duplication
868 * @dev: device for which to duplicate the string
869 * @s: the string to duplicate
870 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
872 * Strings allocated by devm_kstrdup_const will be automatically freed when
873 * the associated device is detached.
876 * Source string if it is in .rodata section otherwise it falls back to
879 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
881 if (is_kernel_rodata((unsigned long)s))
884 return devm_kstrdup(dev, s, gfp);
886 EXPORT_SYMBOL_GPL(devm_kstrdup_const);
889 * devm_kvasprintf - Allocate resource managed space and format a string
891 * @dev: Device to allocate memory for
892 * @gfp: the GFP mask used in the devm_kmalloc() call when
894 * @fmt: The printf()-style format string
895 * @ap: Arguments for the format string
897 * Pointer to allocated string on success, NULL on failure.
899 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
907 len = vsnprintf(NULL, 0, fmt, aq);
910 p = devm_kmalloc(dev, len+1, gfp);
914 vsnprintf(p, len+1, fmt, ap);
918 EXPORT_SYMBOL(devm_kvasprintf);
921 * devm_kasprintf - Allocate resource managed space and format a string
923 * @dev: Device to allocate memory for
924 * @gfp: the GFP mask used in the devm_kmalloc() call when
926 * @fmt: The printf()-style format string
927 * @...: Arguments for the format string
929 * Pointer to allocated string on success, NULL on failure.
931 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
937 p = devm_kvasprintf(dev, gfp, fmt, ap);
942 EXPORT_SYMBOL_GPL(devm_kasprintf);
945 * devm_kfree - Resource-managed kfree
946 * @dev: Device this memory belongs to
949 * Free memory allocated with devm_kmalloc().
951 void devm_kfree(struct device *dev, const void *p)
956 * Special cases: pointer to a string in .rodata returned by
957 * devm_kstrdup_const() or NULL/ZERO ptr.
959 if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p)))
962 rc = devres_destroy(dev, devm_kmalloc_release,
963 devm_kmalloc_match, (void *)p);
966 EXPORT_SYMBOL_GPL(devm_kfree);
969 * devm_kmemdup - Resource-managed kmemdup
970 * @dev: Device this memory belongs to
971 * @src: Memory region to duplicate
972 * @len: Memory region length
973 * @gfp: GFP mask to use
975 * Duplicate region of a memory using resource managed kmalloc
977 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
981 p = devm_kmalloc(dev, len, gfp);
987 EXPORT_SYMBOL_GPL(devm_kmemdup);
989 struct pages_devres {
994 static int devm_pages_match(struct device *dev, void *res, void *p)
996 struct pages_devres *devres = res;
997 struct pages_devres *target = p;
999 return devres->addr == target->addr;
1002 static void devm_pages_release(struct device *dev, void *res)
1004 struct pages_devres *devres = res;
1006 free_pages(devres->addr, devres->order);
1010 * devm_get_free_pages - Resource-managed __get_free_pages
1011 * @dev: Device to allocate memory for
1012 * @gfp_mask: Allocation gfp flags
1013 * @order: Allocation size is (1 << order) pages
1015 * Managed get_free_pages. Memory allocated with this function is
1016 * automatically freed on driver detach.
1019 * Address of allocated memory on success, 0 on failure.
1022 unsigned long devm_get_free_pages(struct device *dev,
1023 gfp_t gfp_mask, unsigned int order)
1025 struct pages_devres *devres;
1028 addr = __get_free_pages(gfp_mask, order);
1030 if (unlikely(!addr))
1033 devres = devres_alloc(devm_pages_release,
1034 sizeof(struct pages_devres), GFP_KERNEL);
1035 if (unlikely(!devres)) {
1036 free_pages(addr, order);
1040 devres->addr = addr;
1041 devres->order = order;
1043 devres_add(dev, devres);
1046 EXPORT_SYMBOL_GPL(devm_get_free_pages);
1049 * devm_free_pages - Resource-managed free_pages
1050 * @dev: Device this memory belongs to
1051 * @addr: Memory to free
1053 * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
1054 * there is no need to supply the @order.
1056 void devm_free_pages(struct device *dev, unsigned long addr)
1058 struct pages_devres devres = { .addr = addr };
1060 WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
1063 EXPORT_SYMBOL_GPL(devm_free_pages);
1065 static void devm_percpu_release(struct device *dev, void *pdata)
1069 p = *(void __percpu **)pdata;
1073 static int devm_percpu_match(struct device *dev, void *data, void *p)
1075 struct devres *devr = container_of(data, struct devres, data);
1077 return *(void **)devr->data == p;
1081 * __devm_alloc_percpu - Resource-managed alloc_percpu
1082 * @dev: Device to allocate per-cpu memory for
1083 * @size: Size of per-cpu memory to allocate
1084 * @align: Alignment of per-cpu memory to allocate
1086 * Managed alloc_percpu. Per-cpu memory allocated with this function is
1087 * automatically freed on driver detach.
1090 * Pointer to allocated memory on success, NULL on failure.
1092 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1096 void __percpu *pcpu;
1098 pcpu = __alloc_percpu(size, align);
1102 p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
1108 *(void __percpu **)p = pcpu;
1114 EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1117 * devm_free_percpu - Resource-managed free_percpu
1118 * @dev: Device this memory belongs to
1119 * @pdata: Per-cpu memory to free
1121 * Free memory allocated with devm_alloc_percpu().
1123 void devm_free_percpu(struct device *dev, void __percpu *pdata)
1125 WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
1128 EXPORT_SYMBOL_GPL(devm_free_percpu);