2 * drivers/base/devres.c - device resource management
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/slab.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
)
87 size_t tot_size
= sizeof(struct devres
) + size
;
90 dr
= kmalloc_track_caller(tot_size
, gfp
);
94 memset(dr
, 0, tot_size
);
95 INIT_LIST_HEAD(&dr
->node
.entry
);
96 dr
->node
.release
= release
;
100 static void add_dr(struct device
*dev
, struct devres_node
*node
)
102 devres_log(dev
, node
, "ADD");
103 BUG_ON(!list_empty(&node
->entry
));
104 list_add_tail(&node
->entry
, &dev
->devres_head
);
107 #ifdef CONFIG_DEBUG_DEVRES
108 void * __devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
,
113 dr
= alloc_dr(release
, size
, gfp
);
116 set_node_dbginfo(&dr
->node
, name
, size
);
119 EXPORT_SYMBOL_GPL(__devres_alloc
);
122 * devres_alloc - Allocate device resource data
123 * @release: Release function devres will be associated with
124 * @size: Allocation size
125 * @gfp: Allocation flags
127 * Allocate devres of @size bytes. The allocated area is zeroed, then
128 * associated with @release. The returned pointer can be passed to
129 * other devres_*() functions.
132 * Pointer to allocated devres on success, NULL on failure.
134 void * devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
)
138 dr
= alloc_dr(release
, size
, gfp
);
143 EXPORT_SYMBOL_GPL(devres_alloc
);
147 * devres_for_each_res - Resource iterator
148 * @dev: Device to iterate resource from
149 * @release: Look for resources associated with this release function
150 * @match: Match function (optional)
151 * @match_data: Data for the match function
152 * @fn: Function to be called for each matched resource.
153 * @data: Data for @fn, the 3rd parameter of @fn
155 * Call @fn for each devres of @dev which is associated with @release
156 * and for which @match returns 1.
161 void devres_for_each_res(struct device
*dev
, dr_release_t release
,
162 dr_match_t match
, void *match_data
,
163 void (*fn
)(struct device
*, void *, void *),
166 struct devres_node
*node
;
167 struct devres_node
*tmp
;
173 spin_lock_irqsave(&dev
->devres_lock
, flags
);
174 list_for_each_entry_safe_reverse(node
, tmp
,
175 &dev
->devres_head
, entry
) {
176 struct devres
*dr
= container_of(node
, struct devres
, node
);
178 if (node
->release
!= release
)
180 if (match
&& !match(dev
, dr
->data
, match_data
))
182 fn(dev
, dr
->data
, data
);
184 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
186 EXPORT_SYMBOL_GPL(devres_for_each_res
);
189 * devres_free - Free device resource data
190 * @res: Pointer to devres data to free
192 * Free devres created with devres_alloc().
194 void devres_free(void *res
)
197 struct devres
*dr
= container_of(res
, struct devres
, data
);
199 BUG_ON(!list_empty(&dr
->node
.entry
));
203 EXPORT_SYMBOL_GPL(devres_free
);
206 * devres_add - Register device resource
207 * @dev: Device to add resource to
208 * @res: Resource to register
210 * Register devres @res to @dev. @res should have been allocated
211 * using devres_alloc(). On driver detach, the associated release
212 * function will be invoked and devres will be freed automatically.
214 void devres_add(struct device
*dev
, void *res
)
216 struct devres
*dr
= container_of(res
, struct devres
, data
);
219 spin_lock_irqsave(&dev
->devres_lock
, flags
);
220 add_dr(dev
, &dr
->node
);
221 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
223 EXPORT_SYMBOL_GPL(devres_add
);
225 static struct devres
*find_dr(struct device
*dev
, dr_release_t release
,
226 dr_match_t match
, void *match_data
)
228 struct devres_node
*node
;
230 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
231 struct devres
*dr
= container_of(node
, struct devres
, node
);
233 if (node
->release
!= release
)
235 if (match
&& !match(dev
, dr
->data
, match_data
))
244 * devres_find - Find device resource
245 * @dev: Device to lookup resource from
246 * @release: Look for resources associated with this release function
247 * @match: Match function (optional)
248 * @match_data: Data for the match function
250 * Find the latest devres of @dev which is associated with @release
251 * and for which @match returns 1. If @match is NULL, it's considered
255 * Pointer to found devres, NULL if not found.
257 void * devres_find(struct device
*dev
, dr_release_t release
,
258 dr_match_t match
, void *match_data
)
263 spin_lock_irqsave(&dev
->devres_lock
, flags
);
264 dr
= find_dr(dev
, release
, match
, match_data
);
265 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
271 EXPORT_SYMBOL_GPL(devres_find
);
274 * devres_get - Find devres, if non-existent, add one atomically
275 * @dev: Device to lookup or add devres for
276 * @new_res: Pointer to new initialized devres to add if not found
277 * @match: Match function (optional)
278 * @match_data: Data for the match function
280 * Find the latest devres of @dev which has the same release function
281 * as @new_res and for which @match return 1. If found, @new_res is
282 * freed; otherwise, @new_res is added atomically.
285 * Pointer to found or added devres.
287 void * devres_get(struct device
*dev
, void *new_res
,
288 dr_match_t match
, void *match_data
)
290 struct devres
*new_dr
= container_of(new_res
, struct devres
, data
);
294 spin_lock_irqsave(&dev
->devres_lock
, flags
);
295 dr
= find_dr(dev
, new_dr
->node
.release
, match
, match_data
);
297 add_dr(dev
, &new_dr
->node
);
301 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
302 devres_free(new_res
);
306 EXPORT_SYMBOL_GPL(devres_get
);
309 * devres_remove - Find a device resource and remove it
310 * @dev: Device to find resource from
311 * @release: Look for resources associated with this release function
312 * @match: Match function (optional)
313 * @match_data: Data for the match function
315 * Find the latest devres of @dev associated with @release and for
316 * which @match returns 1. If @match is NULL, it's considered to
317 * match all. If found, the resource is removed atomically and
321 * Pointer to removed devres on success, NULL if not found.
323 void * devres_remove(struct device
*dev
, dr_release_t release
,
324 dr_match_t match
, void *match_data
)
329 spin_lock_irqsave(&dev
->devres_lock
, flags
);
330 dr
= find_dr(dev
, release
, match
, match_data
);
332 list_del_init(&dr
->node
.entry
);
333 devres_log(dev
, &dr
->node
, "REM");
335 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
341 EXPORT_SYMBOL_GPL(devres_remove
);
344 * devres_destroy - Find a device resource and destroy it
345 * @dev: Device to find resource from
346 * @release: Look for resources associated with this release function
347 * @match: Match function (optional)
348 * @match_data: Data for the match function
350 * Find the latest devres of @dev associated with @release and for
351 * which @match returns 1. If @match is NULL, it's considered to
352 * match all. If found, the resource is removed atomically and freed.
354 * Note that the release function for the resource will not be called,
355 * only the devres-allocated data will be freed. The caller becomes
356 * responsible for freeing any other data.
359 * 0 if devres is found and freed, -ENOENT if not found.
361 int devres_destroy(struct device
*dev
, dr_release_t release
,
362 dr_match_t match
, void *match_data
)
366 res
= devres_remove(dev
, release
, match
, match_data
);
373 EXPORT_SYMBOL_GPL(devres_destroy
);
377 * devres_release - Find a device resource and destroy it, calling release
378 * @dev: Device to find resource from
379 * @release: Look for resources associated with this release function
380 * @match: Match function (optional)
381 * @match_data: Data for the match function
383 * Find the latest devres of @dev associated with @release and for
384 * which @match returns 1. If @match is NULL, it's considered to
385 * match all. If found, the resource is removed atomically, the
386 * release function called and the resource freed.
389 * 0 if devres is found and freed, -ENOENT if not found.
391 int devres_release(struct device
*dev
, dr_release_t release
,
392 dr_match_t match
, void *match_data
)
396 res
= devres_remove(dev
, release
, match
, match_data
);
400 (*release
)(dev
, res
);
404 EXPORT_SYMBOL_GPL(devres_release
);
406 static int remove_nodes(struct device
*dev
,
407 struct list_head
*first
, struct list_head
*end
,
408 struct list_head
*todo
)
410 int cnt
= 0, nr_groups
= 0;
411 struct list_head
*cur
;
413 /* First pass - move normal devres entries to @todo and clear
414 * devres_group colors.
418 struct devres_node
*node
;
419 struct devres_group
*grp
;
421 node
= list_entry(cur
, struct devres_node
, entry
);
424 grp
= node_to_group(node
);
426 /* clear color of group markers in the first pass */
430 /* regular devres entry */
431 if (&node
->entry
== first
)
433 list_move_tail(&node
->entry
, todo
);
441 /* Second pass - Scan groups and color them. A group gets
442 * color value of two iff the group is wholly contained in
443 * [cur, end). That is, for a closed group, both opening and
444 * closing markers should be in the range, while just the
445 * opening marker is enough for an open group.
449 struct devres_node
*node
;
450 struct devres_group
*grp
;
452 node
= list_entry(cur
, struct devres_node
, entry
);
455 grp
= node_to_group(node
);
456 BUG_ON(!grp
|| list_empty(&grp
->node
[0].entry
));
459 if (list_empty(&grp
->node
[1].entry
))
462 BUG_ON(grp
->color
<= 0 || grp
->color
> 2);
463 if (grp
->color
== 2) {
464 /* No need to update cur or end. The removed
465 * nodes are always before both.
467 list_move_tail(&grp
->node
[0].entry
, todo
);
468 list_del_init(&grp
->node
[1].entry
);
475 static int release_nodes(struct device
*dev
, struct list_head
*first
,
476 struct list_head
*end
, unsigned long flags
)
477 __releases(&dev
->devres_lock
)
481 struct devres
*dr
, *tmp
;
483 cnt
= remove_nodes(dev
, first
, end
, &todo
);
485 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
487 /* Release. Note that both devres and devres_group are
488 * handled as devres in the following loop. This is safe.
490 list_for_each_entry_safe_reverse(dr
, tmp
, &todo
, node
.entry
) {
491 devres_log(dev
, &dr
->node
, "REL");
492 dr
->node
.release(dev
, dr
->data
);
500 * devres_release_all - Release all managed resources
501 * @dev: Device to release resources for
503 * Release all resources associated with @dev. This function is
504 * called on driver detach.
506 int devres_release_all(struct device
*dev
)
510 /* Looks like an uninitialized device structure */
511 if (WARN_ON(dev
->devres_head
.next
== NULL
))
513 spin_lock_irqsave(&dev
->devres_lock
, flags
);
514 return release_nodes(dev
, dev
->devres_head
.next
, &dev
->devres_head
,
519 * devres_open_group - Open a new devres group
520 * @dev: Device to open devres group for
522 * @gfp: Allocation flags
524 * Open a new devres group for @dev with @id. For @id, using a
525 * pointer to an object which won't be used for another group is
526 * recommended. If @id is NULL, address-wise unique ID is created.
529 * ID of the new group, NULL on failure.
531 void * devres_open_group(struct device
*dev
, void *id
, gfp_t gfp
)
533 struct devres_group
*grp
;
536 grp
= kmalloc(sizeof(*grp
), gfp
);
540 grp
->node
[0].release
= &group_open_release
;
541 grp
->node
[1].release
= &group_close_release
;
542 INIT_LIST_HEAD(&grp
->node
[0].entry
);
543 INIT_LIST_HEAD(&grp
->node
[1].entry
);
544 set_node_dbginfo(&grp
->node
[0], "grp<", 0);
545 set_node_dbginfo(&grp
->node
[1], "grp>", 0);
550 spin_lock_irqsave(&dev
->devres_lock
, flags
);
551 add_dr(dev
, &grp
->node
[0]);
552 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
555 EXPORT_SYMBOL_GPL(devres_open_group
);
557 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
558 static struct devres_group
* find_group(struct device
*dev
, void *id
)
560 struct devres_node
*node
;
562 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
563 struct devres_group
*grp
;
565 if (node
->release
!= &group_open_release
)
568 grp
= container_of(node
, struct devres_group
, node
[0]);
573 } else if (list_empty(&grp
->node
[1].entry
))
581 * devres_close_group - Close a devres group
582 * @dev: Device to close devres group for
583 * @id: ID of target group, can be NULL
585 * Close the group identified by @id. If @id is NULL, the latest open
588 void devres_close_group(struct device
*dev
, void *id
)
590 struct devres_group
*grp
;
593 spin_lock_irqsave(&dev
->devres_lock
, flags
);
595 grp
= find_group(dev
, id
);
597 add_dr(dev
, &grp
->node
[1]);
601 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
603 EXPORT_SYMBOL_GPL(devres_close_group
);
606 * devres_remove_group - Remove a devres group
607 * @dev: Device to remove group for
608 * @id: ID of target group, can be NULL
610 * Remove the group identified by @id. If @id is NULL, the latest
611 * open group is selected. Note that removing a group doesn't affect
612 * any other resources.
614 void devres_remove_group(struct device
*dev
, void *id
)
616 struct devres_group
*grp
;
619 spin_lock_irqsave(&dev
->devres_lock
, flags
);
621 grp
= find_group(dev
, id
);
623 list_del_init(&grp
->node
[0].entry
);
624 list_del_init(&grp
->node
[1].entry
);
625 devres_log(dev
, &grp
->node
[0], "REM");
629 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
633 EXPORT_SYMBOL_GPL(devres_remove_group
);
636 * devres_release_group - Release resources in a devres group
637 * @dev: Device to release group for
638 * @id: ID of target group, can be NULL
640 * Release all resources in the group identified by @id. If @id is
641 * NULL, the latest open group is selected. The selected group and
642 * groups properly nested inside the selected group are removed.
645 * The number of released non-group resources.
647 int devres_release_group(struct device
*dev
, void *id
)
649 struct devres_group
*grp
;
653 spin_lock_irqsave(&dev
->devres_lock
, flags
);
655 grp
= find_group(dev
, id
);
657 struct list_head
*first
= &grp
->node
[0].entry
;
658 struct list_head
*end
= &dev
->devres_head
;
660 if (!list_empty(&grp
->node
[1].entry
))
661 end
= grp
->node
[1].entry
.next
;
663 cnt
= release_nodes(dev
, first
, end
, flags
);
666 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
671 EXPORT_SYMBOL_GPL(devres_release_group
);
674 * Custom devres actions allow inserting a simple function call
675 * into the teadown sequence.
678 struct action_devres
{
680 void (*action
)(void *);
683 static int devm_action_match(struct device
*dev
, void *res
, void *p
)
685 struct action_devres
*devres
= res
;
686 struct action_devres
*target
= p
;
688 return devres
->action
== target
->action
&&
689 devres
->data
== target
->data
;
692 static void devm_action_release(struct device
*dev
, void *res
)
694 struct action_devres
*devres
= res
;
696 devres
->action(devres
->data
);
700 * devm_add_action() - add a custom action to list of managed resources
701 * @dev: Device that owns the action
702 * @action: Function that should be called
703 * @data: Pointer to data passed to @action implementation
705 * This adds a custom action to the list of managed resources so that
706 * it gets executed as part of standard resource unwinding.
708 int devm_add_action(struct device
*dev
, void (*action
)(void *), void *data
)
710 struct action_devres
*devres
;
712 devres
= devres_alloc(devm_action_release
,
713 sizeof(struct action_devres
), GFP_KERNEL
);
718 devres
->action
= action
;
720 devres_add(dev
, devres
);
723 EXPORT_SYMBOL_GPL(devm_add_action
);
726 * devm_remove_action() - removes previously added custom action
727 * @dev: Device that owns the action
728 * @action: Function implementing the action
729 * @data: Pointer to data passed to @action implementation
731 * Removes instance of @action previously added by devm_add_action().
732 * Both action and data should match one of the existing entries.
734 void devm_remove_action(struct device
*dev
, void (*action
)(void *), void *data
)
736 struct action_devres devres
= {
741 WARN_ON(devres_destroy(dev
, devm_action_release
, devm_action_match
,
745 EXPORT_SYMBOL_GPL(devm_remove_action
);
748 * Managed kzalloc/kfree
750 static void devm_kzalloc_release(struct device
*dev
, void *res
)
755 static int devm_kzalloc_match(struct device
*dev
, void *res
, void *data
)
761 * devm_kzalloc - Resource-managed kzalloc
762 * @dev: Device to allocate memory for
763 * @size: Allocation size
764 * @gfp: Allocation gfp flags
766 * Managed kzalloc. Memory allocated with this function is
767 * automatically freed on driver detach. Like all other devres
768 * resources, guaranteed alignment is unsigned long long.
771 * Pointer to allocated memory on success, NULL on failure.
773 void * devm_kzalloc(struct device
*dev
, size_t size
, gfp_t gfp
)
777 /* use raw alloc_dr for kmalloc caller tracing */
778 dr
= alloc_dr(devm_kzalloc_release
, size
, gfp
);
782 set_node_dbginfo(&dr
->node
, "devm_kzalloc_release", size
);
783 devres_add(dev
, dr
->data
);
786 EXPORT_SYMBOL_GPL(devm_kzalloc
);
789 * devm_kfree - Resource-managed kfree
790 * @dev: Device this memory belongs to
793 * Free memory allocated with devm_kzalloc().
795 void devm_kfree(struct device
*dev
, void *p
)
799 rc
= devres_destroy(dev
, devm_kzalloc_release
, devm_kzalloc_match
, p
);
802 EXPORT_SYMBOL_GPL(devm_kfree
);