Root/drivers/base/devres.c

1/*
2 * drivers/base/devres.c - device resource management
3 *
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
6 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/device.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13
14#include "base.h"
15
16struct devres_node {
17    struct list_head entry;
18    dr_release_t release;
19#ifdef CONFIG_DEBUG_DEVRES
20    const char *name;
21    size_t size;
22#endif
23};
24
25struct devres {
26    struct devres_node node;
27    /* -- 3 pointers */
28    unsigned long long data[]; /* guarantee ull alignment */
29};
30
31struct devres_group {
32    struct devres_node node[2];
33    void *id;
34    int color;
35    /* -- 8 pointers */
36};
37
38#ifdef CONFIG_DEBUG_DEVRES
39static int log_devres = 0;
40module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
41
42static void set_node_dbginfo(struct devres_node *node, const char *name,
43                 size_t size)
44{
45    node->name = name;
46    node->size = size;
47}
48
49static void devres_log(struct device *dev, struct devres_node *node,
50               const char *op)
51{
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);
55}
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 */
60
61/*
62 * Release functions for devres group. These callbacks are used only
63 * for identification.
64 */
65static void group_open_release(struct device *dev, void *res)
66{
67    /* noop */
68}
69
70static void group_close_release(struct device *dev, void *res)
71{
72    /* noop */
73}
74
75static struct devres_group * node_to_group(struct devres_node *node)
76{
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]);
81    return NULL;
82}
83
84static __always_inline struct devres * alloc_dr(dr_release_t release,
85                        size_t size, gfp_t gfp)
86{
87    size_t tot_size = sizeof(struct devres) + size;
88    struct devres *dr;
89
90    dr = kmalloc_track_caller(tot_size, gfp);
91    if (unlikely(!dr))
92        return NULL;
93
94    memset(dr, 0, tot_size);
95    INIT_LIST_HEAD(&dr->node.entry);
96    dr->node.release = release;
97    return dr;
98}
99
100static void add_dr(struct device *dev, struct devres_node *node)
101{
102    devres_log(dev, node, "ADD");
103    BUG_ON(!list_empty(&node->entry));
104    list_add_tail(&node->entry, &dev->devres_head);
105}
106
107#ifdef CONFIG_DEBUG_DEVRES
108void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
109              const char *name)
110{
111    struct devres *dr;
112
113    dr = alloc_dr(release, size, gfp);
114    if (unlikely(!dr))
115        return NULL;
116    set_node_dbginfo(&dr->node, name, size);
117    return dr->data;
118}
119EXPORT_SYMBOL_GPL(__devres_alloc);
120#else
121/**
122 * devres_alloc - Allocate device resource data
123 * @release: Release function devres will be associated with
124 * @size: Allocation size
125 * @gfp: Allocation flags
126 *
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.
130 *
131 * RETURNS:
132 * Pointer to allocated devres on success, NULL on failure.
133 */
134void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
135{
136    struct devres *dr;
137
138    dr = alloc_dr(release, size, gfp);
139    if (unlikely(!dr))
140        return NULL;
141    return dr->data;
142}
143EXPORT_SYMBOL_GPL(devres_alloc);
144#endif
145
146/**
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
154 *
155 * Call @fn for each devres of @dev which is associated with @release
156 * and for which @match returns 1.
157 *
158 * RETURNS:
159 * void
160 */
161void 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 *),
164            void *data)
165{
166    struct devres_node *node;
167    struct devres_node *tmp;
168    unsigned long flags;
169
170    if (!fn)
171        return;
172
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);
177
178        if (node->release != release)
179            continue;
180        if (match && !match(dev, dr->data, match_data))
181            continue;
182        fn(dev, dr->data, data);
183    }
184    spin_unlock_irqrestore(&dev->devres_lock, flags);
185}
186EXPORT_SYMBOL_GPL(devres_for_each_res);
187
188/**
189 * devres_free - Free device resource data
190 * @res: Pointer to devres data to free
191 *
192 * Free devres created with devres_alloc().
193 */
194void devres_free(void *res)
195{
196    if (res) {
197        struct devres *dr = container_of(res, struct devres, data);
198
199        BUG_ON(!list_empty(&dr->node.entry));
200        kfree(dr);
201    }
202}
203EXPORT_SYMBOL_GPL(devres_free);
204
205/**
206 * devres_add - Register device resource
207 * @dev: Device to add resource to
208 * @res: Resource to register
209 *
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.
213 */
214void devres_add(struct device *dev, void *res)
215{
216    struct devres *dr = container_of(res, struct devres, data);
217    unsigned long flags;
218
219    spin_lock_irqsave(&dev->devres_lock, flags);
220    add_dr(dev, &dr->node);
221    spin_unlock_irqrestore(&dev->devres_lock, flags);
222}
223EXPORT_SYMBOL_GPL(devres_add);
224
225static struct devres *find_dr(struct device *dev, dr_release_t release,
226                  dr_match_t match, void *match_data)
227{
228    struct devres_node *node;
229
230    list_for_each_entry_reverse(node, &dev->devres_head, entry) {
231        struct devres *dr = container_of(node, struct devres, node);
232
233        if (node->release != release)
234            continue;
235        if (match && !match(dev, dr->data, match_data))
236            continue;
237        return dr;
238    }
239
240    return NULL;
241}
242
243/**
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
249 *
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
252 * to match all.
253 *
254 * RETURNS:
255 * Pointer to found devres, NULL if not found.
256 */
257void * devres_find(struct device *dev, dr_release_t release,
258           dr_match_t match, void *match_data)
259{
260    struct devres *dr;
261    unsigned long flags;
262
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);
266
267    if (dr)
268        return dr->data;
269    return NULL;
270}
271EXPORT_SYMBOL_GPL(devres_find);
272
273/**
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
279 *
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.
283 *
284 * RETURNS:
285 * Pointer to found or added devres.
286 */
287void * devres_get(struct device *dev, void *new_res,
288          dr_match_t match, void *match_data)
289{
290    struct devres *new_dr = container_of(new_res, struct devres, data);
291    struct devres *dr;
292    unsigned long flags;
293
294    spin_lock_irqsave(&dev->devres_lock, flags);
295    dr = find_dr(dev, new_dr->node.release, match, match_data);
296    if (!dr) {
297        add_dr(dev, &new_dr->node);
298        dr = new_dr;
299        new_dr = NULL;
300    }
301    spin_unlock_irqrestore(&dev->devres_lock, flags);
302    devres_free(new_dr);
303
304    return dr->data;
305}
306EXPORT_SYMBOL_GPL(devres_get);
307
308/**
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
314 *
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
318 * returned.
319 *
320 * RETURNS:
321 * Pointer to removed devres on success, NULL if not found.
322 */
323void * devres_remove(struct device *dev, dr_release_t release,
324             dr_match_t match, void *match_data)
325{
326    struct devres *dr;
327    unsigned long flags;
328
329    spin_lock_irqsave(&dev->devres_lock, flags);
330    dr = find_dr(dev, release, match, match_data);
331    if (dr) {
332        list_del_init(&dr->node.entry);
333        devres_log(dev, &dr->node, "REM");
334    }
335    spin_unlock_irqrestore(&dev->devres_lock, flags);
336
337    if (dr)
338        return dr->data;
339    return NULL;
340}
341EXPORT_SYMBOL_GPL(devres_remove);
342
343/**
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
349 *
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.
353 *
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.
357 *
358 * RETURNS:
359 * 0 if devres is found and freed, -ENOENT if not found.
360 */
361int devres_destroy(struct device *dev, dr_release_t release,
362           dr_match_t match, void *match_data)
363{
364    void *res;
365
366    res = devres_remove(dev, release, match, match_data);
367    if (unlikely(!res))
368        return -ENOENT;
369
370    devres_free(res);
371    return 0;
372}
373EXPORT_SYMBOL_GPL(devres_destroy);
374
375
376/**
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
382 *
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.
387 *
388 * RETURNS:
389 * 0 if devres is found and freed, -ENOENT if not found.
390 */
391int devres_release(struct device *dev, dr_release_t release,
392           dr_match_t match, void *match_data)
393{
394    void *res;
395
396    res = devres_remove(dev, release, match, match_data);
397    if (unlikely(!res))
398        return -ENOENT;
399
400    (*release)(dev, res);
401    devres_free(res);
402    return 0;
403}
404EXPORT_SYMBOL_GPL(devres_release);
405
406static int remove_nodes(struct device *dev,
407            struct list_head *first, struct list_head *end,
408            struct list_head *todo)
409{
410    int cnt = 0, nr_groups = 0;
411    struct list_head *cur;
412
413    /* First pass - move normal devres entries to @todo and clear
414     * devres_group colors.
415     */
416    cur = first;
417    while (cur != end) {
418        struct devres_node *node;
419        struct devres_group *grp;
420
421        node = list_entry(cur, struct devres_node, entry);
422        cur = cur->next;
423
424        grp = node_to_group(node);
425        if (grp) {
426            /* clear color of group markers in the first pass */
427            grp->color = 0;
428            nr_groups++;
429        } else {
430            /* regular devres entry */
431            if (&node->entry == first)
432                first = first->next;
433            list_move_tail(&node->entry, todo);
434            cnt++;
435        }
436    }
437
438    if (!nr_groups)
439        return cnt;
440
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.
446     */
447    cur = first;
448    while (cur != end) {
449        struct devres_node *node;
450        struct devres_group *grp;
451
452        node = list_entry(cur, struct devres_node, entry);
453        cur = cur->next;
454
455        grp = node_to_group(node);
456        BUG_ON(!grp || list_empty(&grp->node[0].entry));
457
458        grp->color++;
459        if (list_empty(&grp->node[1].entry))
460            grp->color++;
461
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.
466             */
467            list_move_tail(&grp->node[0].entry, todo);
468            list_del_init(&grp->node[1].entry);
469        }
470    }
471
472    return cnt;
473}
474
475static int release_nodes(struct device *dev, struct list_head *first,
476             struct list_head *end, unsigned long flags)
477    __releases(&dev->devres_lock)
478{
479    LIST_HEAD(todo);
480    int cnt;
481    struct devres *dr, *tmp;
482
483    cnt = remove_nodes(dev, first, end, &todo);
484
485    spin_unlock_irqrestore(&dev->devres_lock, flags);
486
487    /* Release. Note that both devres and devres_group are
488     * handled as devres in the following loop. This is safe.
489     */
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);
493        kfree(dr);
494    }
495
496    return cnt;
497}
498
499/**
500 * devres_release_all - Release all managed resources
501 * @dev: Device to release resources for
502 *
503 * Release all resources associated with @dev. This function is
504 * called on driver detach.
505 */
506int devres_release_all(struct device *dev)
507{
508    unsigned long flags;
509
510    /* Looks like an uninitialized device structure */
511    if (WARN_ON(dev->devres_head.next == NULL))
512        return -ENODEV;
513    spin_lock_irqsave(&dev->devres_lock, flags);
514    return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
515                 flags);
516}
517
518/**
519 * devres_open_group - Open a new devres group
520 * @dev: Device to open devres group for
521 * @id: Separator ID
522 * @gfp: Allocation flags
523 *
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.
527 *
528 * RETURNS:
529 * ID of the new group, NULL on failure.
530 */
531void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
532{
533    struct devres_group *grp;
534    unsigned long flags;
535
536    grp = kmalloc(sizeof(*grp), gfp);
537    if (unlikely(!grp))
538        return NULL;
539
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);
546    grp->id = grp;
547    if (id)
548        grp->id = id;
549
550    spin_lock_irqsave(&dev->devres_lock, flags);
551    add_dr(dev, &grp->node[0]);
552    spin_unlock_irqrestore(&dev->devres_lock, flags);
553    return grp->id;
554}
555EXPORT_SYMBOL_GPL(devres_open_group);
556
557/* Find devres group with ID @id. If @id is NULL, look for the latest. */
558static struct devres_group * find_group(struct device *dev, void *id)
559{
560    struct devres_node *node;
561
562    list_for_each_entry_reverse(node, &dev->devres_head, entry) {
563        struct devres_group *grp;
564
565        if (node->release != &group_open_release)
566            continue;
567
568        grp = container_of(node, struct devres_group, node[0]);
569
570        if (id) {
571            if (grp->id == id)
572                return grp;
573        } else if (list_empty(&grp->node[1].entry))
574            return grp;
575    }
576
577    return NULL;
578}
579
580/**
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
584 *
585 * Close the group identified by @id. If @id is NULL, the latest open
586 * group is selected.
587 */
588void devres_close_group(struct device *dev, void *id)
589{
590    struct devres_group *grp;
591    unsigned long flags;
592
593    spin_lock_irqsave(&dev->devres_lock, flags);
594
595    grp = find_group(dev, id);
596    if (grp)
597        add_dr(dev, &grp->node[1]);
598    else
599        WARN_ON(1);
600
601    spin_unlock_irqrestore(&dev->devres_lock, flags);
602}
603EXPORT_SYMBOL_GPL(devres_close_group);
604
605/**
606 * devres_remove_group - Remove a devres group
607 * @dev: Device to remove group for
608 * @id: ID of target group, can be NULL
609 *
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.
613 */
614void devres_remove_group(struct device *dev, void *id)
615{
616    struct devres_group *grp;
617    unsigned long flags;
618
619    spin_lock_irqsave(&dev->devres_lock, flags);
620
621    grp = find_group(dev, id);
622    if (grp) {
623        list_del_init(&grp->node[0].entry);
624        list_del_init(&grp->node[1].entry);
625        devres_log(dev, &grp->node[0], "REM");
626    } else
627        WARN_ON(1);
628
629    spin_unlock_irqrestore(&dev->devres_lock, flags);
630
631    kfree(grp);
632}
633EXPORT_SYMBOL_GPL(devres_remove_group);
634
635/**
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
639 *
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.
643 *
644 * RETURNS:
645 * The number of released non-group resources.
646 */
647int devres_release_group(struct device *dev, void *id)
648{
649    struct devres_group *grp;
650    unsigned long flags;
651    int cnt = 0;
652
653    spin_lock_irqsave(&dev->devres_lock, flags);
654
655    grp = find_group(dev, id);
656    if (grp) {
657        struct list_head *first = &grp->node[0].entry;
658        struct list_head *end = &dev->devres_head;
659
660        if (!list_empty(&grp->node[1].entry))
661            end = grp->node[1].entry.next;
662
663        cnt = release_nodes(dev, first, end, flags);
664    } else {
665        WARN_ON(1);
666        spin_unlock_irqrestore(&dev->devres_lock, flags);
667    }
668
669    return cnt;
670}
671EXPORT_SYMBOL_GPL(devres_release_group);
672
673/*
674 * Managed kzalloc/kfree
675 */
676static void devm_kzalloc_release(struct device *dev, void *res)
677{
678    /* noop */
679}
680
681static int devm_kzalloc_match(struct device *dev, void *res, void *data)
682{
683    return res == data;
684}
685
686/**
687 * devm_kzalloc - Resource-managed kzalloc
688 * @dev: Device to allocate memory for
689 * @size: Allocation size
690 * @gfp: Allocation gfp flags
691 *
692 * Managed kzalloc. Memory allocated with this function is
693 * automatically freed on driver detach. Like all other devres
694 * resources, guaranteed alignment is unsigned long long.
695 *
696 * RETURNS:
697 * Pointer to allocated memory on success, NULL on failure.
698 */
699void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
700{
701    struct devres *dr;
702
703    /* use raw alloc_dr for kmalloc caller tracing */
704    dr = alloc_dr(devm_kzalloc_release, size, gfp);
705    if (unlikely(!dr))
706        return NULL;
707
708    set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
709    devres_add(dev, dr->data);
710    return dr->data;
711}
712EXPORT_SYMBOL_GPL(devm_kzalloc);
713
714/**
715 * devm_kfree - Resource-managed kfree
716 * @dev: Device this memory belongs to
717 * @p: Memory to free
718 *
719 * Free memory allocated with devm_kzalloc().
720 */
721void devm_kfree(struct device *dev, void *p)
722{
723    int rc;
724
725    rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
726    WARN_ON(rc);
727}
728EXPORT_SYMBOL_GPL(devm_kfree);
729

Archive Download this file



interactive