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>
16 struct list_head entry;
18 #ifdef CONFIG_DEBUG_DEVRES
25 struct devres_node node;
27 unsigned long long data[]; /* guarantee ull alignment */
31 struct devres_node node[2];
37 #ifdef CONFIG_DEBUG_DEVRES
38 static int log_devres = 0;
39 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
41 static void set_node_dbginfo(struct devres_node *node, const char *name,
48 static void devres_log(struct device *dev, struct devres_node *node,
51 if (unlikely(log_devres))
52 dev_printk(KERN_ERR, dev, "DEVRES %3s %p %s (%lu bytes)\n",
53 op, node, node->name, (unsigned long)node->size);
55 #else /* CONFIG_DEBUG_DEVRES */
56 #define set_node_dbginfo(node, n, s) do {} while (0)
57 #define devres_log(dev, node, op) do {} while (0)
58 #endif /* CONFIG_DEBUG_DEVRES */
61 * Release functions for devres group. These callbacks are used only
64 static void group_open_release(struct device *dev, void *res)
69 static void group_close_release(struct device *dev, void *res)
74 static struct devres_group * node_to_group(struct devres_node *node)
76 if (node->release == &group_open_release)
77 return container_of(node, struct devres_group, node[0]);
78 if (node->release == &group_close_release)
79 return container_of(node, struct devres_group, node[1]);
83 static __always_inline struct devres * alloc_dr(dr_release_t release,
84 size_t size, gfp_t gfp)
86 size_t tot_size = sizeof(struct devres) + size;
89 dr = kmalloc_track_caller(tot_size, gfp);
93 memset(dr, 0, tot_size);
94 INIT_LIST_HEAD(&dr->node.entry);
95 dr->node.release = release;
99 static void add_dr(struct device *dev, struct devres_node *node)
101 devres_log(dev, node, "ADD");
102 BUG_ON(!list_empty(&node->entry));
103 list_add_tail(&node->entry, &dev->devres_head);
106 #ifdef CONFIG_DEBUG_DEVRES
107 void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
112 dr = alloc_dr(release, size, gfp);
115 set_node_dbginfo(&dr->node, name, size);
118 EXPORT_SYMBOL_GPL(__devres_alloc);
121 * devres_alloc - Allocate device resource data
122 * @release: Release function devres will be associated with
123 * @size: Allocation size
124 * @gfp: Allocation flags
126 * Allocate devres of @size bytes. The allocated area is zeroed, then
127 * associated with @release. The returned pointer can be passed to
128 * other devres_*() functions.
131 * Pointer to allocated devres on success, NULL on failure.
133 void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
137 dr = alloc_dr(release, size, gfp);
142 EXPORT_SYMBOL_GPL(devres_alloc);
146 * devres_free - Free device resource data
147 * @res: Pointer to devres data to free
149 * Free devres created with devres_alloc().
151 void devres_free(void *res)
154 struct devres *dr = container_of(res, struct devres, data);
156 BUG_ON(!list_empty(&dr->node.entry));
160 EXPORT_SYMBOL_GPL(devres_free);
163 * devres_add - Register device resource
164 * @dev: Device to add resource to
165 * @res: Resource to register
167 * Register devres @res to @dev. @res should have been allocated
168 * using devres_alloc(). On driver detach, the associated release
169 * function will be invoked and devres will be freed automatically.
171 void devres_add(struct device *dev, void *res)
173 struct devres *dr = container_of(res, struct devres, data);
176 spin_lock_irqsave(&dev->devres_lock, flags);
177 add_dr(dev, &dr->node);
178 spin_unlock_irqrestore(&dev->devres_lock, flags);
180 EXPORT_SYMBOL_GPL(devres_add);
182 static struct devres *find_dr(struct device *dev, dr_release_t release,
183 dr_match_t match, void *match_data)
185 struct devres_node *node;
187 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
188 struct devres *dr = container_of(node, struct devres, node);
190 if (node->release != release)
192 if (match && !match(dev, dr->data, match_data))
201 * devres_find - Find device resource
202 * @dev: Device to lookup resource from
203 * @release: Look for resources associated with this release function
204 * @match: Match function (optional)
205 * @match_data: Data for the match function
207 * Find the latest devres of @dev which is associated with @release
208 * and for which @match returns 1. If @match is NULL, it's considered
212 * Pointer to found devres, NULL if not found.
214 void * devres_find(struct device *dev, dr_release_t release,
215 dr_match_t match, void *match_data)
220 spin_lock_irqsave(&dev->devres_lock, flags);
221 dr = find_dr(dev, release, match, match_data);
222 spin_unlock_irqrestore(&dev->devres_lock, flags);
228 EXPORT_SYMBOL_GPL(devres_find);
231 * devres_get - Find devres, if non-existent, add one atomically
232 * @dev: Device to lookup or add devres for
233 * @new_res: Pointer to new initialized devres to add if not found
234 * @match: Match function (optional)
235 * @match_data: Data for the match function
237 * Find the latest devres of @dev which has the same release function
238 * as @new_res and for which @match return 1. If found, @new_res is
239 * freed; otherwise, @new_res is added atomically.
242 * Pointer to found or added devres.
244 void * devres_get(struct device *dev, void *new_res,
245 dr_match_t match, void *match_data)
247 struct devres *new_dr = container_of(new_res, struct devres, data);
251 spin_lock_irqsave(&dev->devres_lock, flags);
252 dr = find_dr(dev, new_dr->node.release, match, match_data);
254 add_dr(dev, &new_dr->node);
258 spin_unlock_irqrestore(&dev->devres_lock, flags);
263 EXPORT_SYMBOL_GPL(devres_get);
266 * devres_remove - Find a device resource and remove it
267 * @dev: Device to find resource from
268 * @release: Look for resources associated with this release function
269 * @match: Match function (optional)
270 * @match_data: Data for the match function
272 * Find the latest devres of @dev associated with @release and for
273 * which @match returns 1. If @match is NULL, it's considered to
274 * match all. If found, the resource is removed atomically and
278 * Pointer to removed devres on success, NULL if not found.
280 void * devres_remove(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);
289 list_del_init(&dr->node.entry);
290 devres_log(dev, &dr->node, "REM");
292 spin_unlock_irqrestore(&dev->devres_lock, flags);
298 EXPORT_SYMBOL_GPL(devres_remove);
301 * devres_destroy - Find a device resource and destroy it
302 * @dev: Device to find resource from
303 * @release: Look for resources associated with this release function
304 * @match: Match function (optional)
305 * @match_data: Data for the match function
307 * Find the latest devres of @dev associated with @release and for
308 * which @match returns 1. If @match is NULL, it's considered to
309 * match all. If found, the resource is removed atomically and freed.
312 * 0 if devres is found and freed, -ENOENT if not found.
314 int devres_destroy(struct device *dev, dr_release_t release,
315 dr_match_t match, void *match_data)
319 res = devres_remove(dev, release, match, match_data);
326 EXPORT_SYMBOL_GPL(devres_destroy);
328 static int remove_nodes(struct device *dev,
329 struct list_head *first, struct list_head *end,
330 struct list_head *todo)
332 int cnt = 0, nr_groups = 0;
333 struct list_head *cur;
335 /* First pass - move normal devres entries to @todo and clear
336 * devres_group colors.
340 struct devres_node *node;
341 struct devres_group *grp;
343 node = list_entry(cur, struct devres_node, entry);
346 grp = node_to_group(node);
348 /* clear color of group markers in the first pass */
352 /* regular devres entry */
353 if (&node->entry == first)
355 list_move_tail(&node->entry, todo);
363 /* Second pass - Scan groups and color them. A group gets
364 * color value of two iff the group is wholly contained in
365 * [cur, end). That is, for a closed group, both opening and
366 * closing markers should be in the range, while just the
367 * opening marker is enough for an open group.
371 struct devres_node *node;
372 struct devres_group *grp;
374 node = list_entry(cur, struct devres_node, entry);
377 grp = node_to_group(node);
378 BUG_ON(!grp || list_empty(&grp->node[0].entry));
381 if (list_empty(&grp->node[1].entry))
384 BUG_ON(grp->color <= 0 || grp->color > 2);
385 if (grp->color == 2) {
386 /* No need to update cur or end. The removed
387 * nodes are always before both.
389 list_move_tail(&grp->node[0].entry, todo);
390 list_del_init(&grp->node[1].entry);
397 static int release_nodes(struct device *dev, struct list_head *first,
398 struct list_head *end, unsigned long flags)
402 struct devres *dr, *tmp;
404 cnt = remove_nodes(dev, first, end, &todo);
406 spin_unlock_irqrestore(&dev->devres_lock, flags);
408 /* Release. Note that both devres and devres_group are
409 * handled as devres in the following loop. This is safe.
411 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
412 devres_log(dev, &dr->node, "REL");
413 dr->node.release(dev, dr->data);
421 * devres_release_all - Release all managed resources
422 * @dev: Device to release resources for
424 * Release all resources associated with @dev. This function is
425 * called on driver detach.
427 int devres_release_all(struct device *dev)
431 /* Looks like an uninitialized device structure */
432 if (WARN_ON(dev->devres_head.next == NULL))
434 spin_lock_irqsave(&dev->devres_lock, flags);
435 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
440 * devres_open_group - Open a new devres group
441 * @dev: Device to open devres group for
443 * @gfp: Allocation flags
445 * Open a new devres group for @dev with @id. For @id, using a
446 * pointer to an object which won't be used for another group is
447 * recommended. If @id is NULL, address-wise unique ID is created.
450 * ID of the new group, NULL on failure.
452 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
454 struct devres_group *grp;
457 grp = kmalloc(sizeof(*grp), gfp);
461 grp->node[0].release = &group_open_release;
462 grp->node[1].release = &group_close_release;
463 INIT_LIST_HEAD(&grp->node[0].entry);
464 INIT_LIST_HEAD(&grp->node[1].entry);
465 set_node_dbginfo(&grp->node[0], "grp<", 0);
466 set_node_dbginfo(&grp->node[1], "grp>", 0);
471 spin_lock_irqsave(&dev->devres_lock, flags);
472 add_dr(dev, &grp->node[0]);
473 spin_unlock_irqrestore(&dev->devres_lock, flags);
476 EXPORT_SYMBOL_GPL(devres_open_group);
478 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
479 static struct devres_group * find_group(struct device *dev, void *id)
481 struct devres_node *node;
483 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
484 struct devres_group *grp;
486 if (node->release != &group_open_release)
489 grp = container_of(node, struct devres_group, node[0]);
494 } else if (list_empty(&grp->node[1].entry))
502 * devres_close_group - Close a devres group
503 * @dev: Device to close devres group for
504 * @id: ID of target group, can be NULL
506 * Close the group identified by @id. If @id is NULL, the latest open
509 void devres_close_group(struct device *dev, void *id)
511 struct devres_group *grp;
514 spin_lock_irqsave(&dev->devres_lock, flags);
516 grp = find_group(dev, id);
518 add_dr(dev, &grp->node[1]);
522 spin_unlock_irqrestore(&dev->devres_lock, flags);
524 EXPORT_SYMBOL_GPL(devres_close_group);
527 * devres_remove_group - Remove a devres group
528 * @dev: Device to remove group for
529 * @id: ID of target group, can be NULL
531 * Remove the group identified by @id. If @id is NULL, the latest
532 * open group is selected. Note that removing a group doesn't affect
533 * any other resources.
535 void devres_remove_group(struct device *dev, void *id)
537 struct devres_group *grp;
540 spin_lock_irqsave(&dev->devres_lock, flags);
542 grp = find_group(dev, id);
544 list_del_init(&grp->node[0].entry);
545 list_del_init(&grp->node[1].entry);
546 devres_log(dev, &grp->node[0], "REM");
550 spin_unlock_irqrestore(&dev->devres_lock, flags);
554 EXPORT_SYMBOL_GPL(devres_remove_group);
557 * devres_release_group - Release resources in a devres group
558 * @dev: Device to release group for
559 * @id: ID of target group, can be NULL
561 * Release all resources in the group identified by @id. If @id is
562 * NULL, the latest open group is selected. The selected group and
563 * groups properly nested inside the selected group are removed.
566 * The number of released non-group resources.
568 int devres_release_group(struct device *dev, void *id)
570 struct devres_group *grp;
574 spin_lock_irqsave(&dev->devres_lock, flags);
576 grp = find_group(dev, id);
578 struct list_head *first = &grp->node[0].entry;
579 struct list_head *end = &dev->devres_head;
581 if (!list_empty(&grp->node[1].entry))
582 end = grp->node[1].entry.next;
584 cnt = release_nodes(dev, first, end, flags);
587 spin_unlock_irqrestore(&dev->devres_lock, flags);
592 EXPORT_SYMBOL_GPL(devres_release_group);
595 * Managed kzalloc/kfree
597 static void devm_kzalloc_release(struct device *dev, void *res)
602 static int devm_kzalloc_match(struct device *dev, void *res, void *data)
608 * devm_kzalloc - Resource-managed kzalloc
609 * @dev: Device to allocate memory for
610 * @size: Allocation size
611 * @gfp: Allocation gfp flags
613 * Managed kzalloc. Memory allocated with this function is
614 * automatically freed on driver detach. Like all other devres
615 * resources, guaranteed alignment is unsigned long long.
618 * Pointer to allocated memory on success, NULL on failure.
620 void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
624 /* use raw alloc_dr for kmalloc caller tracing */
625 dr = alloc_dr(devm_kzalloc_release, size, gfp);
629 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
630 devres_add(dev, dr->data);
633 EXPORT_SYMBOL_GPL(devm_kzalloc);
636 * devm_kfree - Resource-managed kfree
637 * @dev: Device this memory belongs to
640 * Free memory allocated with dev_kzalloc().
642 void devm_kfree(struct device *dev, void *p)
646 rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
649 EXPORT_SYMBOL_GPL(devm_kfree);