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>
14 struct list_head entry
;
16 #ifdef CONFIG_DEBUG_DEVRES
23 struct devres_node node
;
25 unsigned long long data
[]; /* guarantee ull alignment */
29 struct devres_node node
[2];
35 #ifdef CONFIG_DEBUG_DEVRES
36 static int log_devres
= 0;
37 module_param_named(log
, log_devres
, int, S_IRUGO
| S_IWUSR
);
39 static void set_node_dbginfo(struct devres_node
*node
, const char *name
,
46 static void devres_log(struct device
*dev
, struct devres_node
*node
,
49 if (unlikely(log_devres
))
50 dev_printk(KERN_ERR
, dev
, "DEVRES %3s %p %s (%lu bytes)\n",
51 op
, node
, node
->name
, (unsigned long)node
->size
);
53 #else /* CONFIG_DEBUG_DEVRES */
54 #define set_node_dbginfo(node, n, s) do {} while (0)
55 #define devres_log(dev, node, op) do {} while (0)
56 #endif /* CONFIG_DEBUG_DEVRES */
59 * Release functions for devres group. These callbacks are used only
62 static void group_open_release(struct device
*dev
, void *res
)
67 static void group_close_release(struct device
*dev
, void *res
)
72 static struct devres_group
* node_to_group(struct devres_node
*node
)
74 if (node
->release
== &group_open_release
)
75 return container_of(node
, struct devres_group
, node
[0]);
76 if (node
->release
== &group_close_release
)
77 return container_of(node
, struct devres_group
, node
[1]);
81 static __always_inline
struct devres
* alloc_dr(dr_release_t release
,
82 size_t size
, gfp_t gfp
)
84 size_t tot_size
= sizeof(struct devres
) + size
;
87 dr
= kmalloc_track_caller(tot_size
, gfp
);
91 memset(dr
, 0, tot_size
);
92 INIT_LIST_HEAD(&dr
->node
.entry
);
93 dr
->node
.release
= release
;
97 static void add_dr(struct device
*dev
, struct devres_node
*node
)
99 devres_log(dev
, node
, "ADD");
100 BUG_ON(!list_empty(&node
->entry
));
101 list_add_tail(&node
->entry
, &dev
->devres_head
);
104 #ifdef CONFIG_DEBUG_DEVRES
105 void * __devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
,
110 dr
= alloc_dr(release
, size
, gfp
);
113 set_node_dbginfo(&dr
->node
, name
, size
);
116 EXPORT_SYMBOL_GPL(__devres_alloc
);
119 * devres_alloc - Allocate device resource data
120 * @release: Release function devres will be associated with
121 * @size: Allocation size
122 * @gfp: Allocation flags
124 * Allocate devres of @size bytes. The allocated area is zeroed, then
125 * associated with @release. The returned pointer can be passed to
126 * other devres_*() functions.
129 * Pointer to allocated devres on success, NULL on failure.
131 void * devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
)
135 dr
= alloc_dr(release
, size
, gfp
);
140 EXPORT_SYMBOL_GPL(devres_alloc
);
144 * devres_free - Free device resource data
145 * @res: Pointer to devres data to free
147 * Free devres created with devres_alloc().
149 void devres_free(void *res
)
152 struct devres
*dr
= container_of(res
, struct devres
, data
);
154 BUG_ON(!list_empty(&dr
->node
.entry
));
158 EXPORT_SYMBOL_GPL(devres_free
);
161 * devres_add - Register device resource
162 * @dev: Device to add resource to
163 * @res: Resource to register
165 * Register devres @res to @dev. @res should have been allocated
166 * using devres_alloc(). On driver detach, the associated release
167 * function will be invoked and devres will be freed automatically.
169 void devres_add(struct device
*dev
, void *res
)
171 struct devres
*dr
= container_of(res
, struct devres
, data
);
174 spin_lock_irqsave(&dev
->devres_lock
, flags
);
175 add_dr(dev
, &dr
->node
);
176 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
178 EXPORT_SYMBOL_GPL(devres_add
);
180 static struct devres
*find_dr(struct device
*dev
, dr_release_t release
,
181 dr_match_t match
, void *match_data
)
183 struct devres_node
*node
;
185 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
186 struct devres
*dr
= container_of(node
, struct devres
, node
);
188 if (node
->release
!= release
)
190 if (match
&& !match(dev
, dr
->data
, match_data
))
199 * devres_find - Find device resource
200 * @dev: Device to lookup resource from
201 * @release: Look for resources associated with this release function
202 * @match: Match function (optional)
203 * @match_data: Data for the match function
205 * Find the latest devres of @dev which is associated with @release
206 * and for which @match returns 1. If @match is NULL, it's considered
210 * Pointer to found devres, NULL if not found.
212 void * devres_find(struct device
*dev
, dr_release_t release
,
213 dr_match_t match
, void *match_data
)
218 spin_lock_irqsave(&dev
->devres_lock
, flags
);
219 dr
= find_dr(dev
, release
, match
, match_data
);
220 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
226 EXPORT_SYMBOL_GPL(devres_find
);
229 * devres_get - Find devres, if non-existent, add one atomically
230 * @dev: Device to lookup or add devres for
231 * @new_res: Pointer to new initialized devres to add if not found
232 * @match: Match function (optional)
233 * @match_data: Data for the match function
235 * Find the latest devres of @dev which has the same release function
236 * as @new_res and for which @match return 1. If found, @new_res is
237 * freed; otherwise, @new_res is added atomically.
240 * Pointer to found or added devres.
242 void * devres_get(struct device
*dev
, void *new_res
,
243 dr_match_t match
, void *match_data
)
245 struct devres
*new_dr
= container_of(new_res
, struct devres
, data
);
249 spin_lock_irqsave(&dev
->devres_lock
, flags
);
250 dr
= find_dr(dev
, new_dr
->node
.release
, match
, match_data
);
252 add_dr(dev
, &new_dr
->node
);
256 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
261 EXPORT_SYMBOL_GPL(devres_get
);
264 * devres_remove - Find a device resource and remove it
265 * @dev: Device to find resource from
266 * @release: Look for resources associated with this release function
267 * @match: Match function (optional)
268 * @match_data: Data for the match function
270 * Find the latest devres of @dev associated with @release and for
271 * which @match returns 1. If @match is NULL, it's considered to
272 * match all. If found, the resource is removed atomically and
276 * Pointer to removed devres on success, NULL if not found.
278 void * devres_remove(struct device
*dev
, dr_release_t release
,
279 dr_match_t match
, void *match_data
)
284 spin_lock_irqsave(&dev
->devres_lock
, flags
);
285 dr
= find_dr(dev
, release
, match
, match_data
);
287 list_del_init(&dr
->node
.entry
);
288 devres_log(dev
, &dr
->node
, "REM");
290 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
296 EXPORT_SYMBOL_GPL(devres_remove
);
299 * devres_destroy - Find a device resource and destroy it
300 * @dev: Device to find resource from
301 * @release: Look for resources associated with this release function
302 * @match: Match function (optional)
303 * @match_data: Data for the match function
305 * Find the latest devres of @dev associated with @release and for
306 * which @match returns 1. If @match is NULL, it's considered to
307 * match all. If found, the resource is removed atomically and freed.
310 * 0 if devres is found and freed, -ENOENT if not found.
312 int devres_destroy(struct device
*dev
, dr_release_t release
,
313 dr_match_t match
, void *match_data
)
317 res
= devres_remove(dev
, release
, match
, match_data
);
324 EXPORT_SYMBOL_GPL(devres_destroy
);
326 static int remove_nodes(struct device
*dev
,
327 struct list_head
*first
, struct list_head
*end
,
328 struct list_head
*todo
)
330 int cnt
= 0, nr_groups
= 0;
331 struct list_head
*cur
;
333 /* First pass - move normal devres entries to @todo and clear
334 * devres_group colors.
338 struct devres_node
*node
;
339 struct devres_group
*grp
;
341 node
= list_entry(cur
, struct devres_node
, entry
);
344 grp
= node_to_group(node
);
346 /* clear color of group markers in the first pass */
350 /* regular devres entry */
351 if (&node
->entry
== first
)
353 list_move_tail(&node
->entry
, todo
);
361 /* Second pass - Scan groups and color them. A group gets
362 * color value of two iff the group is wholly contained in
363 * [cur, end). That is, for a closed group, both opening and
364 * closing markers should be in the range, while just the
365 * opening marker is enough for an open group.
369 struct devres_node
*node
;
370 struct devres_group
*grp
;
372 node
= list_entry(cur
, struct devres_node
, entry
);
375 grp
= node_to_group(node
);
376 BUG_ON(!grp
|| list_empty(&grp
->node
[0].entry
));
379 if (list_empty(&grp
->node
[1].entry
))
382 BUG_ON(grp
->color
<= 0 || grp
->color
> 2);
383 if (grp
->color
== 2) {
384 /* No need to update cur or end. The removed
385 * nodes are always before both.
387 list_move_tail(&grp
->node
[0].entry
, todo
);
388 list_del_init(&grp
->node
[1].entry
);
395 static int release_nodes(struct device
*dev
, struct list_head
*first
,
396 struct list_head
*end
, unsigned long flags
)
400 struct devres
*dr
, *tmp
;
402 cnt
= remove_nodes(dev
, first
, end
, &todo
);
404 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
406 /* Release. Note that both devres and devres_group are
407 * handled as devres in the following loop. This is safe.
409 list_for_each_entry_safe_reverse(dr
, tmp
, &todo
, node
.entry
) {
410 devres_log(dev
, &dr
->node
, "REL");
411 dr
->node
.release(dev
, dr
->data
);
419 * devres_release_all - Release all managed resources
420 * @dev: Device to release resources for
422 * Release all resources associated with @dev. This function is
423 * called on driver detach.
425 int devres_release_all(struct device
*dev
)
429 spin_lock_irqsave(&dev
->devres_lock
, flags
);
430 return release_nodes(dev
, dev
->devres_head
.next
, &dev
->devres_head
,
435 * devres_open_group - Open a new devres group
436 * @dev: Device to open devres group for
438 * @gfp: Allocation flags
440 * Open a new devres group for @dev with @id. For @id, using a
441 * pointer to an object which won't be used for another group is
442 * recommended. If @id is NULL, address-wise unique ID is created.
445 * ID of the new group, NULL on failure.
447 void * devres_open_group(struct device
*dev
, void *id
, gfp_t gfp
)
449 struct devres_group
*grp
;
452 grp
= kmalloc(sizeof(*grp
), gfp
);
456 grp
->node
[0].release
= &group_open_release
;
457 grp
->node
[1].release
= &group_close_release
;
458 INIT_LIST_HEAD(&grp
->node
[0].entry
);
459 INIT_LIST_HEAD(&grp
->node
[1].entry
);
460 set_node_dbginfo(&grp
->node
[0], "grp<", 0);
461 set_node_dbginfo(&grp
->node
[1], "grp>", 0);
466 spin_lock_irqsave(&dev
->devres_lock
, flags
);
467 add_dr(dev
, &grp
->node
[0]);
468 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
471 EXPORT_SYMBOL_GPL(devres_open_group
);
473 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
474 static struct devres_group
* find_group(struct device
*dev
, void *id
)
476 struct devres_node
*node
;
478 list_for_each_entry_reverse(node
, &dev
->devres_head
, entry
) {
479 struct devres_group
*grp
;
481 if (node
->release
!= &group_open_release
)
484 grp
= container_of(node
, struct devres_group
, node
[0]);
489 } else if (list_empty(&grp
->node
[1].entry
))
497 * devres_close_group - Close a devres group
498 * @dev: Device to close devres group for
499 * @id: ID of target group, can be NULL
501 * Close the group identified by @id. If @id is NULL, the latest open
504 void devres_close_group(struct device
*dev
, void *id
)
506 struct devres_group
*grp
;
509 spin_lock_irqsave(&dev
->devres_lock
, flags
);
511 grp
= find_group(dev
, id
);
513 add_dr(dev
, &grp
->node
[1]);
517 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
519 EXPORT_SYMBOL_GPL(devres_close_group
);
522 * devres_remove_group - Remove a devres group
523 * @dev: Device to remove group for
524 * @id: ID of target group, can be NULL
526 * Remove the group identified by @id. If @id is NULL, the latest
527 * open group is selected. Note that removing a group doesn't affect
528 * any other resources.
530 void devres_remove_group(struct device
*dev
, void *id
)
532 struct devres_group
*grp
;
535 spin_lock_irqsave(&dev
->devres_lock
, flags
);
537 grp
= find_group(dev
, id
);
539 list_del_init(&grp
->node
[0].entry
);
540 list_del_init(&grp
->node
[1].entry
);
541 devres_log(dev
, &grp
->node
[0], "REM");
545 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
549 EXPORT_SYMBOL_GPL(devres_remove_group
);
552 * devres_release_group - Release resources in a devres group
553 * @dev: Device to release group for
554 * @id: ID of target group, can be NULL
556 * Release all resources in the group identified by @id. If @id is
557 * NULL, the latest open group is selected. The selected group and
558 * groups properly nested inside the selected group are removed.
561 * The number of released non-group resources.
563 int devres_release_group(struct device
*dev
, void *id
)
565 struct devres_group
*grp
;
569 spin_lock_irqsave(&dev
->devres_lock
, flags
);
571 grp
= find_group(dev
, id
);
573 struct list_head
*first
= &grp
->node
[0].entry
;
574 struct list_head
*end
= &dev
->devres_head
;
576 if (!list_empty(&grp
->node
[1].entry
))
577 end
= grp
->node
[1].entry
.next
;
579 cnt
= release_nodes(dev
, first
, end
, flags
);
582 spin_unlock_irqrestore(&dev
->devres_lock
, flags
);
587 EXPORT_SYMBOL_GPL(devres_release_group
);
590 * Managed kzalloc/kfree
592 static void devm_kzalloc_release(struct device
*dev
, void *res
)
597 static int devm_kzalloc_match(struct device
*dev
, void *res
, void *data
)
603 * devm_kzalloc - Resource-managed kzalloc
604 * @dev: Device to allocate memory for
605 * @size: Allocation size
606 * @gfp: Allocation gfp flags
608 * Managed kzalloc. Memory allocated with this function is
609 * automatically freed on driver detach. Like all other devres
610 * resources, guaranteed alignment is unsigned long long.
613 * Pointer to allocated memory on success, NULL on failure.
615 void * devm_kzalloc(struct device
*dev
, size_t size
, gfp_t gfp
)
619 /* use raw alloc_dr for kmalloc caller tracing */
620 dr
= alloc_dr(devm_kzalloc_release
, size
, gfp
);
624 set_node_dbginfo(&dr
->node
, "devm_kzalloc_release", size
);
625 devres_add(dev
, dr
->data
);
628 EXPORT_SYMBOL_GPL(devm_kzalloc
);
631 * devm_kfree - Resource-managed kfree
632 * @dev: Device this memory belongs to
635 * Free memory allocated with dev_kzalloc().
637 void devm_kfree(struct device
*dev
, void *p
)
641 rc
= devres_destroy(dev
, devm_kzalloc_release
, devm_kzalloc_match
, p
);
644 EXPORT_SYMBOL_GPL(devm_kfree
);