ALSA: usb-audio: Check mixer unit descriptors more strictly
[linux/fpc-iii.git] / drivers / base / devres.c
blobf98a097e73f29c92f0bbf86d805f4fd3fd64109d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/percpu.h>
14 #include "base.h"
16 struct 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
25 struct devres {
26 struct devres_node node;
27 /* -- 3 pointers */
28 unsigned long long data[]; /* guarantee ull alignment */
31 struct devres_group {
32 struct devres_node node[2];
33 void *id;
34 int color;
35 /* -- 8 pointers */
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,
43 size_t size)
45 node->name = name;
46 node->size = size;
49 static void devres_log(struct device *dev, struct devres_node *node,
50 const char *op)
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
63 * for identification.
65 static void group_open_release(struct device *dev, void *res)
67 /* noop */
70 static void group_close_release(struct device *dev, void *res)
72 /* noop */
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]);
81 return NULL;
84 static __always_inline struct devres * alloc_dr(dr_release_t release,
85 size_t size, gfp_t gfp, int nid)
87 size_t tot_size;
88 struct devres *dr;
90 /* We must catch any near-SIZE_MAX cases that could overflow. */
91 if (unlikely(check_add_overflow(sizeof(struct devres), size,
92 &tot_size)))
93 return NULL;
95 dr = kmalloc_node_track_caller(tot_size, gfp, nid);
96 if (unlikely(!dr))
97 return NULL;
99 memset(dr, 0, offsetof(struct devres, data));
101 INIT_LIST_HEAD(&dr->node.entry);
102 dr->node.release = release;
103 return dr;
106 static void add_dr(struct device *dev, struct devres_node *node)
108 devres_log(dev, node, "ADD");
109 BUG_ON(!list_empty(&node->entry));
110 list_add_tail(&node->entry, &dev->devres_head);
113 #ifdef CONFIG_DEBUG_DEVRES
114 void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
115 const char *name)
117 struct devres *dr;
119 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
120 if (unlikely(!dr))
121 return NULL;
122 set_node_dbginfo(&dr->node, name, size);
123 return dr->data;
125 EXPORT_SYMBOL_GPL(__devres_alloc_node);
126 #else
128 * devres_alloc - Allocate device resource data
129 * @release: Release function devres will be associated with
130 * @size: Allocation size
131 * @gfp: Allocation flags
132 * @nid: NUMA node
134 * Allocate devres of @size bytes. The allocated area is zeroed, then
135 * associated with @release. The returned pointer can be passed to
136 * other devres_*() functions.
138 * RETURNS:
139 * Pointer to allocated devres on success, NULL on failure.
141 void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
143 struct devres *dr;
145 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
146 if (unlikely(!dr))
147 return NULL;
148 return dr->data;
150 EXPORT_SYMBOL_GPL(devres_alloc_node);
151 #endif
154 * devres_for_each_res - Resource iterator
155 * @dev: Device to iterate resource from
156 * @release: Look for resources associated with this release function
157 * @match: Match function (optional)
158 * @match_data: Data for the match function
159 * @fn: Function to be called for each matched resource.
160 * @data: Data for @fn, the 3rd parameter of @fn
162 * Call @fn for each devres of @dev which is associated with @release
163 * and for which @match returns 1.
165 * RETURNS:
166 * void
168 void devres_for_each_res(struct device *dev, dr_release_t release,
169 dr_match_t match, void *match_data,
170 void (*fn)(struct device *, void *, void *),
171 void *data)
173 struct devres_node *node;
174 struct devres_node *tmp;
175 unsigned long flags;
177 if (!fn)
178 return;
180 spin_lock_irqsave(&dev->devres_lock, flags);
181 list_for_each_entry_safe_reverse(node, tmp,
182 &dev->devres_head, entry) {
183 struct devres *dr = container_of(node, struct devres, node);
185 if (node->release != release)
186 continue;
187 if (match && !match(dev, dr->data, match_data))
188 continue;
189 fn(dev, dr->data, data);
191 spin_unlock_irqrestore(&dev->devres_lock, flags);
193 EXPORT_SYMBOL_GPL(devres_for_each_res);
196 * devres_free - Free device resource data
197 * @res: Pointer to devres data to free
199 * Free devres created with devres_alloc().
201 void devres_free(void *res)
203 if (res) {
204 struct devres *dr = container_of(res, struct devres, data);
206 BUG_ON(!list_empty(&dr->node.entry));
207 kfree(dr);
210 EXPORT_SYMBOL_GPL(devres_free);
213 * devres_add - Register device resource
214 * @dev: Device to add resource to
215 * @res: Resource to register
217 * Register devres @res to @dev. @res should have been allocated
218 * using devres_alloc(). On driver detach, the associated release
219 * function will be invoked and devres will be freed automatically.
221 void devres_add(struct device *dev, void *res)
223 struct devres *dr = container_of(res, struct devres, data);
224 unsigned long flags;
226 spin_lock_irqsave(&dev->devres_lock, flags);
227 add_dr(dev, &dr->node);
228 spin_unlock_irqrestore(&dev->devres_lock, flags);
230 EXPORT_SYMBOL_GPL(devres_add);
232 static struct devres *find_dr(struct device *dev, dr_release_t release,
233 dr_match_t match, void *match_data)
235 struct devres_node *node;
237 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
238 struct devres *dr = container_of(node, struct devres, node);
240 if (node->release != release)
241 continue;
242 if (match && !match(dev, dr->data, match_data))
243 continue;
244 return dr;
247 return NULL;
251 * devres_find - Find device resource
252 * @dev: Device to lookup resource from
253 * @release: Look for resources associated with this release function
254 * @match: Match function (optional)
255 * @match_data: Data for the match function
257 * Find the latest devres of @dev which is associated with @release
258 * and for which @match returns 1. If @match is NULL, it's considered
259 * to match all.
261 * RETURNS:
262 * Pointer to found devres, NULL if not found.
264 void * devres_find(struct device *dev, dr_release_t release,
265 dr_match_t match, void *match_data)
267 struct devres *dr;
268 unsigned long flags;
270 spin_lock_irqsave(&dev->devres_lock, flags);
271 dr = find_dr(dev, release, match, match_data);
272 spin_unlock_irqrestore(&dev->devres_lock, flags);
274 if (dr)
275 return dr->data;
276 return NULL;
278 EXPORT_SYMBOL_GPL(devres_find);
281 * devres_get - Find devres, if non-existent, add one atomically
282 * @dev: Device to lookup or add devres for
283 * @new_res: Pointer to new initialized devres to add if not found
284 * @match: Match function (optional)
285 * @match_data: Data for the match function
287 * Find the latest devres of @dev which has the same release function
288 * as @new_res and for which @match return 1. If found, @new_res is
289 * freed; otherwise, @new_res is added atomically.
291 * RETURNS:
292 * Pointer to found or added devres.
294 void * devres_get(struct device *dev, void *new_res,
295 dr_match_t match, void *match_data)
297 struct devres *new_dr = container_of(new_res, struct devres, data);
298 struct devres *dr;
299 unsigned long flags;
301 spin_lock_irqsave(&dev->devres_lock, flags);
302 dr = find_dr(dev, new_dr->node.release, match, match_data);
303 if (!dr) {
304 add_dr(dev, &new_dr->node);
305 dr = new_dr;
306 new_res = NULL;
308 spin_unlock_irqrestore(&dev->devres_lock, flags);
309 devres_free(new_res);
311 return dr->data;
313 EXPORT_SYMBOL_GPL(devres_get);
316 * devres_remove - Find a device resource and remove it
317 * @dev: Device to find resource from
318 * @release: Look for resources associated with this release function
319 * @match: Match function (optional)
320 * @match_data: Data for the match function
322 * Find the latest devres of @dev associated with @release and for
323 * which @match returns 1. If @match is NULL, it's considered to
324 * match all. If found, the resource is removed atomically and
325 * returned.
327 * RETURNS:
328 * Pointer to removed devres on success, NULL if not found.
330 void * devres_remove(struct device *dev, dr_release_t release,
331 dr_match_t match, void *match_data)
333 struct devres *dr;
334 unsigned long flags;
336 spin_lock_irqsave(&dev->devres_lock, flags);
337 dr = find_dr(dev, release, match, match_data);
338 if (dr) {
339 list_del_init(&dr->node.entry);
340 devres_log(dev, &dr->node, "REM");
342 spin_unlock_irqrestore(&dev->devres_lock, flags);
344 if (dr)
345 return dr->data;
346 return NULL;
348 EXPORT_SYMBOL_GPL(devres_remove);
351 * devres_destroy - Find a device resource and destroy it
352 * @dev: Device to find resource from
353 * @release: Look for resources associated with this release function
354 * @match: Match function (optional)
355 * @match_data: Data for the match function
357 * Find the latest devres of @dev associated with @release and for
358 * which @match returns 1. If @match is NULL, it's considered to
359 * match all. If found, the resource is removed atomically and freed.
361 * Note that the release function for the resource will not be called,
362 * only the devres-allocated data will be freed. The caller becomes
363 * responsible for freeing any other data.
365 * RETURNS:
366 * 0 if devres is found and freed, -ENOENT if not found.
368 int devres_destroy(struct device *dev, dr_release_t release,
369 dr_match_t match, void *match_data)
371 void *res;
373 res = devres_remove(dev, release, match, match_data);
374 if (unlikely(!res))
375 return -ENOENT;
377 devres_free(res);
378 return 0;
380 EXPORT_SYMBOL_GPL(devres_destroy);
384 * devres_release - Find a device resource and destroy it, calling release
385 * @dev: Device to find resource from
386 * @release: Look for resources associated with this release function
387 * @match: Match function (optional)
388 * @match_data: Data for the match function
390 * Find the latest devres of @dev associated with @release and for
391 * which @match returns 1. If @match is NULL, it's considered to
392 * match all. If found, the resource is removed atomically, the
393 * release function called and the resource freed.
395 * RETURNS:
396 * 0 if devres is found and freed, -ENOENT if not found.
398 int devres_release(struct device *dev, dr_release_t release,
399 dr_match_t match, void *match_data)
401 void *res;
403 res = devres_remove(dev, release, match, match_data);
404 if (unlikely(!res))
405 return -ENOENT;
407 (*release)(dev, res);
408 devres_free(res);
409 return 0;
411 EXPORT_SYMBOL_GPL(devres_release);
413 static int remove_nodes(struct device *dev,
414 struct list_head *first, struct list_head *end,
415 struct list_head *todo)
417 int cnt = 0, nr_groups = 0;
418 struct list_head *cur;
420 /* First pass - move normal devres entries to @todo and clear
421 * devres_group colors.
423 cur = first;
424 while (cur != end) {
425 struct devres_node *node;
426 struct devres_group *grp;
428 node = list_entry(cur, struct devres_node, entry);
429 cur = cur->next;
431 grp = node_to_group(node);
432 if (grp) {
433 /* clear color of group markers in the first pass */
434 grp->color = 0;
435 nr_groups++;
436 } else {
437 /* regular devres entry */
438 if (&node->entry == first)
439 first = first->next;
440 list_move_tail(&node->entry, todo);
441 cnt++;
445 if (!nr_groups)
446 return cnt;
448 /* Second pass - Scan groups and color them. A group gets
449 * color value of two iff the group is wholly contained in
450 * [cur, end). That is, for a closed group, both opening and
451 * closing markers should be in the range, while just the
452 * opening marker is enough for an open group.
454 cur = first;
455 while (cur != end) {
456 struct devres_node *node;
457 struct devres_group *grp;
459 node = list_entry(cur, struct devres_node, entry);
460 cur = cur->next;
462 grp = node_to_group(node);
463 BUG_ON(!grp || list_empty(&grp->node[0].entry));
465 grp->color++;
466 if (list_empty(&grp->node[1].entry))
467 grp->color++;
469 BUG_ON(grp->color <= 0 || grp->color > 2);
470 if (grp->color == 2) {
471 /* No need to update cur or end. The removed
472 * nodes are always before both.
474 list_move_tail(&grp->node[0].entry, todo);
475 list_del_init(&grp->node[1].entry);
479 return cnt;
482 static int release_nodes(struct device *dev, struct list_head *first,
483 struct list_head *end, unsigned long flags)
484 __releases(&dev->devres_lock)
486 LIST_HEAD(todo);
487 int cnt;
488 struct devres *dr, *tmp;
490 cnt = remove_nodes(dev, first, end, &todo);
492 spin_unlock_irqrestore(&dev->devres_lock, flags);
494 /* Release. Note that both devres and devres_group are
495 * handled as devres in the following loop. This is safe.
497 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
498 devres_log(dev, &dr->node, "REL");
499 dr->node.release(dev, dr->data);
500 kfree(dr);
503 return cnt;
507 * devres_release_all - Release all managed resources
508 * @dev: Device to release resources for
510 * Release all resources associated with @dev. This function is
511 * called on driver detach.
513 int devres_release_all(struct device *dev)
515 unsigned long flags;
517 /* Looks like an uninitialized device structure */
518 if (WARN_ON(dev->devres_head.next == NULL))
519 return -ENODEV;
520 spin_lock_irqsave(&dev->devres_lock, flags);
521 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
522 flags);
526 * devres_open_group - Open a new devres group
527 * @dev: Device to open devres group for
528 * @id: Separator ID
529 * @gfp: Allocation flags
531 * Open a new devres group for @dev with @id. For @id, using a
532 * pointer to an object which won't be used for another group is
533 * recommended. If @id is NULL, address-wise unique ID is created.
535 * RETURNS:
536 * ID of the new group, NULL on failure.
538 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
540 struct devres_group *grp;
541 unsigned long flags;
543 grp = kmalloc(sizeof(*grp), gfp);
544 if (unlikely(!grp))
545 return NULL;
547 grp->node[0].release = &group_open_release;
548 grp->node[1].release = &group_close_release;
549 INIT_LIST_HEAD(&grp->node[0].entry);
550 INIT_LIST_HEAD(&grp->node[1].entry);
551 set_node_dbginfo(&grp->node[0], "grp<", 0);
552 set_node_dbginfo(&grp->node[1], "grp>", 0);
553 grp->id = grp;
554 if (id)
555 grp->id = id;
557 spin_lock_irqsave(&dev->devres_lock, flags);
558 add_dr(dev, &grp->node[0]);
559 spin_unlock_irqrestore(&dev->devres_lock, flags);
560 return grp->id;
562 EXPORT_SYMBOL_GPL(devres_open_group);
564 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
565 static struct devres_group * find_group(struct device *dev, void *id)
567 struct devres_node *node;
569 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
570 struct devres_group *grp;
572 if (node->release != &group_open_release)
573 continue;
575 grp = container_of(node, struct devres_group, node[0]);
577 if (id) {
578 if (grp->id == id)
579 return grp;
580 } else if (list_empty(&grp->node[1].entry))
581 return grp;
584 return NULL;
588 * devres_close_group - Close a devres group
589 * @dev: Device to close devres group for
590 * @id: ID of target group, can be NULL
592 * Close the group identified by @id. If @id is NULL, the latest open
593 * group is selected.
595 void devres_close_group(struct device *dev, void *id)
597 struct devres_group *grp;
598 unsigned long flags;
600 spin_lock_irqsave(&dev->devres_lock, flags);
602 grp = find_group(dev, id);
603 if (grp)
604 add_dr(dev, &grp->node[1]);
605 else
606 WARN_ON(1);
608 spin_unlock_irqrestore(&dev->devres_lock, flags);
610 EXPORT_SYMBOL_GPL(devres_close_group);
613 * devres_remove_group - Remove a devres group
614 * @dev: Device to remove group for
615 * @id: ID of target group, can be NULL
617 * Remove the group identified by @id. If @id is NULL, the latest
618 * open group is selected. Note that removing a group doesn't affect
619 * any other resources.
621 void devres_remove_group(struct device *dev, void *id)
623 struct devres_group *grp;
624 unsigned long flags;
626 spin_lock_irqsave(&dev->devres_lock, flags);
628 grp = find_group(dev, id);
629 if (grp) {
630 list_del_init(&grp->node[0].entry);
631 list_del_init(&grp->node[1].entry);
632 devres_log(dev, &grp->node[0], "REM");
633 } else
634 WARN_ON(1);
636 spin_unlock_irqrestore(&dev->devres_lock, flags);
638 kfree(grp);
640 EXPORT_SYMBOL_GPL(devres_remove_group);
643 * devres_release_group - Release resources in a devres group
644 * @dev: Device to release group for
645 * @id: ID of target group, can be NULL
647 * Release all resources in the group identified by @id. If @id is
648 * NULL, the latest open group is selected. The selected group and
649 * groups properly nested inside the selected group are removed.
651 * RETURNS:
652 * The number of released non-group resources.
654 int devres_release_group(struct device *dev, void *id)
656 struct devres_group *grp;
657 unsigned long flags;
658 int cnt = 0;
660 spin_lock_irqsave(&dev->devres_lock, flags);
662 grp = find_group(dev, id);
663 if (grp) {
664 struct list_head *first = &grp->node[0].entry;
665 struct list_head *end = &dev->devres_head;
667 if (!list_empty(&grp->node[1].entry))
668 end = grp->node[1].entry.next;
670 cnt = release_nodes(dev, first, end, flags);
671 } else {
672 WARN_ON(1);
673 spin_unlock_irqrestore(&dev->devres_lock, flags);
676 return cnt;
678 EXPORT_SYMBOL_GPL(devres_release_group);
681 * Custom devres actions allow inserting a simple function call
682 * into the teadown sequence.
685 struct action_devres {
686 void *data;
687 void (*action)(void *);
690 static int devm_action_match(struct device *dev, void *res, void *p)
692 struct action_devres *devres = res;
693 struct action_devres *target = p;
695 return devres->action == target->action &&
696 devres->data == target->data;
699 static void devm_action_release(struct device *dev, void *res)
701 struct action_devres *devres = res;
703 devres->action(devres->data);
707 * devm_add_action() - add a custom action to list of managed resources
708 * @dev: Device that owns the action
709 * @action: Function that should be called
710 * @data: Pointer to data passed to @action implementation
712 * This adds a custom action to the list of managed resources so that
713 * it gets executed as part of standard resource unwinding.
715 int devm_add_action(struct device *dev, void (*action)(void *), void *data)
717 struct action_devres *devres;
719 devres = devres_alloc(devm_action_release,
720 sizeof(struct action_devres), GFP_KERNEL);
721 if (!devres)
722 return -ENOMEM;
724 devres->data = data;
725 devres->action = action;
727 devres_add(dev, devres);
728 return 0;
730 EXPORT_SYMBOL_GPL(devm_add_action);
733 * devm_remove_action() - removes previously added custom action
734 * @dev: Device that owns the action
735 * @action: Function implementing the action
736 * @data: Pointer to data passed to @action implementation
738 * Removes instance of @action previously added by devm_add_action().
739 * Both action and data should match one of the existing entries.
741 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
743 struct action_devres devres = {
744 .data = data,
745 .action = action,
748 WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
749 &devres));
752 EXPORT_SYMBOL_GPL(devm_remove_action);
755 * Managed kmalloc/kfree
757 static void devm_kmalloc_release(struct device *dev, void *res)
759 /* noop */
762 static int devm_kmalloc_match(struct device *dev, void *res, void *data)
764 return res == data;
768 * devm_kmalloc - Resource-managed kmalloc
769 * @dev: Device to allocate memory for
770 * @size: Allocation size
771 * @gfp: Allocation gfp flags
773 * Managed kmalloc. Memory allocated with this function is
774 * automatically freed on driver detach. Like all other devres
775 * resources, guaranteed alignment is unsigned long long.
777 * RETURNS:
778 * Pointer to allocated memory on success, NULL on failure.
780 void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
782 struct devres *dr;
784 /* use raw alloc_dr for kmalloc caller tracing */
785 dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
786 if (unlikely(!dr))
787 return NULL;
790 * This is named devm_kzalloc_release for historical reasons
791 * The initial implementation did not support kmalloc, only kzalloc
793 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
794 devres_add(dev, dr->data);
795 return dr->data;
797 EXPORT_SYMBOL_GPL(devm_kmalloc);
800 * devm_kstrdup - Allocate resource managed space and
801 * copy an existing string into that.
802 * @dev: Device to allocate memory for
803 * @s: the string to duplicate
804 * @gfp: the GFP mask used in the devm_kmalloc() call when
805 * allocating memory
806 * RETURNS:
807 * Pointer to allocated string on success, NULL on failure.
809 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
811 size_t size;
812 char *buf;
814 if (!s)
815 return NULL;
817 size = strlen(s) + 1;
818 buf = devm_kmalloc(dev, size, gfp);
819 if (buf)
820 memcpy(buf, s, size);
821 return buf;
823 EXPORT_SYMBOL_GPL(devm_kstrdup);
826 * devm_kvasprintf - Allocate resource managed space and format a string
827 * into that.
828 * @dev: Device to allocate memory for
829 * @gfp: the GFP mask used in the devm_kmalloc() call when
830 * allocating memory
831 * @fmt: The printf()-style format string
832 * @ap: Arguments for the format string
833 * RETURNS:
834 * Pointer to allocated string on success, NULL on failure.
836 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
837 va_list ap)
839 unsigned int len;
840 char *p;
841 va_list aq;
843 va_copy(aq, ap);
844 len = vsnprintf(NULL, 0, fmt, aq);
845 va_end(aq);
847 p = devm_kmalloc(dev, len+1, gfp);
848 if (!p)
849 return NULL;
851 vsnprintf(p, len+1, fmt, ap);
853 return p;
855 EXPORT_SYMBOL(devm_kvasprintf);
858 * devm_kasprintf - Allocate resource managed space and format a string
859 * into that.
860 * @dev: Device to allocate memory for
861 * @gfp: the GFP mask used in the devm_kmalloc() call when
862 * allocating memory
863 * @fmt: The printf()-style format string
864 * @...: Arguments for the format string
865 * RETURNS:
866 * Pointer to allocated string on success, NULL on failure.
868 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
870 va_list ap;
871 char *p;
873 va_start(ap, fmt);
874 p = devm_kvasprintf(dev, gfp, fmt, ap);
875 va_end(ap);
877 return p;
879 EXPORT_SYMBOL_GPL(devm_kasprintf);
882 * devm_kfree - Resource-managed kfree
883 * @dev: Device this memory belongs to
884 * @p: Memory to free
886 * Free memory allocated with devm_kmalloc().
888 void devm_kfree(struct device *dev, void *p)
890 int rc;
892 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
893 WARN_ON(rc);
895 EXPORT_SYMBOL_GPL(devm_kfree);
898 * devm_kmemdup - Resource-managed kmemdup
899 * @dev: Device this memory belongs to
900 * @src: Memory region to duplicate
901 * @len: Memory region length
902 * @gfp: GFP mask to use
904 * Duplicate region of a memory using resource managed kmalloc
906 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
908 void *p;
910 p = devm_kmalloc(dev, len, gfp);
911 if (p)
912 memcpy(p, src, len);
914 return p;
916 EXPORT_SYMBOL_GPL(devm_kmemdup);
918 struct pages_devres {
919 unsigned long addr;
920 unsigned int order;
923 static int devm_pages_match(struct device *dev, void *res, void *p)
925 struct pages_devres *devres = res;
926 struct pages_devres *target = p;
928 return devres->addr == target->addr;
931 static void devm_pages_release(struct device *dev, void *res)
933 struct pages_devres *devres = res;
935 free_pages(devres->addr, devres->order);
939 * devm_get_free_pages - Resource-managed __get_free_pages
940 * @dev: Device to allocate memory for
941 * @gfp_mask: Allocation gfp flags
942 * @order: Allocation size is (1 << order) pages
944 * Managed get_free_pages. Memory allocated with this function is
945 * automatically freed on driver detach.
947 * RETURNS:
948 * Address of allocated memory on success, 0 on failure.
951 unsigned long devm_get_free_pages(struct device *dev,
952 gfp_t gfp_mask, unsigned int order)
954 struct pages_devres *devres;
955 unsigned long addr;
957 addr = __get_free_pages(gfp_mask, order);
959 if (unlikely(!addr))
960 return 0;
962 devres = devres_alloc(devm_pages_release,
963 sizeof(struct pages_devres), GFP_KERNEL);
964 if (unlikely(!devres)) {
965 free_pages(addr, order);
966 return 0;
969 devres->addr = addr;
970 devres->order = order;
972 devres_add(dev, devres);
973 return addr;
975 EXPORT_SYMBOL_GPL(devm_get_free_pages);
978 * devm_free_pages - Resource-managed free_pages
979 * @dev: Device this memory belongs to
980 * @addr: Memory to free
982 * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
983 * there is no need to supply the @order.
985 void devm_free_pages(struct device *dev, unsigned long addr)
987 struct pages_devres devres = { .addr = addr };
989 WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
990 &devres));
992 EXPORT_SYMBOL_GPL(devm_free_pages);
994 static void devm_percpu_release(struct device *dev, void *pdata)
996 void __percpu *p;
998 p = *(void __percpu **)pdata;
999 free_percpu(p);
1002 static int devm_percpu_match(struct device *dev, void *data, void *p)
1004 struct devres *devr = container_of(data, struct devres, data);
1006 return *(void **)devr->data == p;
1010 * __devm_alloc_percpu - Resource-managed alloc_percpu
1011 * @dev: Device to allocate per-cpu memory for
1012 * @size: Size of per-cpu memory to allocate
1013 * @align: Alignment of per-cpu memory to allocate
1015 * Managed alloc_percpu. Per-cpu memory allocated with this function is
1016 * automatically freed on driver detach.
1018 * RETURNS:
1019 * Pointer to allocated memory on success, NULL on failure.
1021 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1022 size_t align)
1024 void *p;
1025 void __percpu *pcpu;
1027 pcpu = __alloc_percpu(size, align);
1028 if (!pcpu)
1029 return NULL;
1031 p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
1032 if (!p) {
1033 free_percpu(pcpu);
1034 return NULL;
1037 *(void __percpu **)p = pcpu;
1039 devres_add(dev, p);
1041 return pcpu;
1043 EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1046 * devm_free_percpu - Resource-managed free_percpu
1047 * @dev: Device this memory belongs to
1048 * @pdata: Per-cpu memory to free
1050 * Free memory allocated with devm_alloc_percpu().
1052 void devm_free_percpu(struct device *dev, void __percpu *pdata)
1054 WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
1055 (void *)pdata));
1057 EXPORT_SYMBOL_GPL(devm_free_percpu);