blob: e43a04a495a318f9591f22cf8da68b79c27ab465 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#include <linux/percpu.h>
14
15#include "base.h"
16
17struct devres_node {
18 struct list_head entry;
19 dr_release_t release;
20#ifdef CONFIG_DEBUG_DEVRES
21 const char *name;
22 size_t size;
23#endif
24};
25
26struct devres {
27 struct devres_node node;
28 /*
29 * Some archs want to perform DMA into kmalloc caches
30 * and need a guaranteed alignment larger than
31 * the alignment of a 64-bit integer.
32 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
33 * buffer alignment as if it was allocated by plain kmalloc().
34 */
35 u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
36};
37
38struct devres_group {
39 struct devres_node node[2];
40 void *id;
41 int color;
42 /* -- 8 pointers */
43};
44
45#ifdef CONFIG_DEBUG_DEVRES
46static int log_devres = 0;
47module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
48
49static void set_node_dbginfo(struct devres_node *node, const char *name,
50 size_t size)
51{
52 node->name = name;
53 node->size = size;
54}
55
56static void devres_log(struct device *dev, struct devres_node *node,
57 const char *op)
58{
59 if (unlikely(log_devres))
60 dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
61 op, node, node->name, (unsigned long)node->size);
62}
63#else /* CONFIG_DEBUG_DEVRES */
64#define set_node_dbginfo(node, n, s) do {} while (0)
65#define devres_log(dev, node, op) do {} while (0)
66#endif /* CONFIG_DEBUG_DEVRES */
67
68/*
69 * Release functions for devres group. These callbacks are used only
70 * for identification.
71 */
72static void group_open_release(struct device *dev, void *res)
73{
74 /* noop */
75}
76
77static void group_close_release(struct device *dev, void *res)
78{
79 /* noop */
80}
81
82static struct devres_group * node_to_group(struct devres_node *node)
83{
84 if (node->release == &group_open_release)
85 return container_of(node, struct devres_group, node[0]);
86 if (node->release == &group_close_release)
87 return container_of(node, struct devres_group, node[1]);
88 return NULL;
89}
90
91static __always_inline struct devres * alloc_dr(dr_release_t release,
92 size_t size, gfp_t gfp, int nid)
93{
94 size_t tot_size = sizeof(struct devres) + size;
95 struct devres *dr;
96
97 dr = kmalloc_node_track_caller(tot_size, gfp, nid);
98 if (unlikely(!dr))
99 return NULL;
100
101 memset(dr, 0, offsetof(struct devres, data));
102
103 INIT_LIST_HEAD(&dr->node.entry);
104 dr->node.release = release;
105 return dr;
106}
107
108static void add_dr(struct device *dev, struct devres_node *node)
109{
110 devres_log(dev, node, "ADD");
111 BUG_ON(!list_empty(&node->entry));
112 list_add_tail(&node->entry, &dev->devres_head);
113}
114
115#ifdef CONFIG_DEBUG_DEVRES
116void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
117 const char *name)
118{
119 struct devres *dr;
120
121 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
122 if (unlikely(!dr))
123 return NULL;
124 set_node_dbginfo(&dr->node, name, size);
125 return dr->data;
126}
127EXPORT_SYMBOL_GPL(__devres_alloc_node);
128#else
129/**
130 * devres_alloc - Allocate device resource data
131 * @release: Release function devres will be associated with
132 * @size: Allocation size
133 * @gfp: Allocation flags
134 * @nid: NUMA node
135 *
136 * Allocate devres of @size bytes. The allocated area is zeroed, then
137 * associated with @release. The returned pointer can be passed to
138 * other devres_*() functions.
139 *
140 * RETURNS:
141 * Pointer to allocated devres on success, NULL on failure.
142 */
143void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
144{
145 struct devres *dr;
146
147 dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
148 if (unlikely(!dr))
149 return NULL;
150 return dr->data;
151}
152EXPORT_SYMBOL_GPL(devres_alloc_node);
153#endif
154
155/**
156 * devres_for_each_res - Resource iterator
157 * @dev: Device to iterate resource from
158 * @release: Look for resources associated with this release function
159 * @match: Match function (optional)
160 * @match_data: Data for the match function
161 * @fn: Function to be called for each matched resource.
162 * @data: Data for @fn, the 3rd parameter of @fn
163 *
164 * Call @fn for each devres of @dev which is associated with @release
165 * and for which @match returns 1.
166 *
167 * RETURNS:
168 * void
169 */
170void devres_for_each_res(struct device *dev, dr_release_t release,
171 dr_match_t match, void *match_data,
172 void (*fn)(struct device *, void *, void *),
173 void *data)
174{
175 struct devres_node *node;
176 struct devres_node *tmp;
177 unsigned long flags;
178
179 if (!fn)
180 return;
181
182 spin_lock_irqsave(&dev->devres_lock, flags);
183 list_for_each_entry_safe_reverse(node, tmp,
184 &dev->devres_head, entry) {
185 struct devres *dr = container_of(node, struct devres, node);
186
187 if (node->release != release)
188 continue;
189 if (match && !match(dev, dr->data, match_data))
190 continue;
191 fn(dev, dr->data, data);
192 }
193 spin_unlock_irqrestore(&dev->devres_lock, flags);
194}
195EXPORT_SYMBOL_GPL(devres_for_each_res);
196
197/**
198 * devres_free - Free device resource data
199 * @res: Pointer to devres data to free
200 *
201 * Free devres created with devres_alloc().
202 */
203void devres_free(void *res)
204{
205 if (res) {
206 struct devres *dr = container_of(res, struct devres, data);
207
208 BUG_ON(!list_empty(&dr->node.entry));
209 kfree(dr);
210 }
211}
212EXPORT_SYMBOL_GPL(devres_free);
213
214/**
215 * devres_add - Register device resource
216 * @dev: Device to add resource to
217 * @res: Resource to register
218 *
219 * Register devres @res to @dev. @res should have been allocated
220 * using devres_alloc(). On driver detach, the associated release
221 * function will be invoked and devres will be freed automatically.
222 */
223void devres_add(struct device *dev, void *res)
224{
225 struct devres *dr = container_of(res, struct devres, data);
226 unsigned long flags;
227
228 spin_lock_irqsave(&dev->devres_lock, flags);
229 add_dr(dev, &dr->node);
230 spin_unlock_irqrestore(&dev->devres_lock, flags);
231}
232EXPORT_SYMBOL_GPL(devres_add);
233
234static struct devres *find_dr(struct device *dev, dr_release_t release,
235 dr_match_t match, void *match_data)
236{
237 struct devres_node *node;
238
239 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
240 struct devres *dr = container_of(node, struct devres, node);
241
242 if (node->release != release)
243 continue;
244 if (match && !match(dev, dr->data, match_data))
245 continue;
246 return dr;
247 }
248
249 return NULL;
250}
251
252/**
253 * devres_find - Find device resource
254 * @dev: Device to lookup resource from
255 * @release: Look for resources associated with this release function
256 * @match: Match function (optional)
257 * @match_data: Data for the match function
258 *
259 * Find the latest devres of @dev which is associated with @release
260 * and for which @match returns 1. If @match is NULL, it's considered
261 * to match all.
262 *
263 * RETURNS:
264 * Pointer to found devres, NULL if not found.
265 */
266void * devres_find(struct device *dev, dr_release_t release,
267 dr_match_t match, void *match_data)
268{
269 struct devres *dr;
270 unsigned long flags;
271
272 spin_lock_irqsave(&dev->devres_lock, flags);
273 dr = find_dr(dev, release, match, match_data);
274 spin_unlock_irqrestore(&dev->devres_lock, flags);
275
276 if (dr)
277 return dr->data;
278 return NULL;
279}
280EXPORT_SYMBOL_GPL(devres_find);
281
282/**
283 * devres_get - Find devres, if non-existent, add one atomically
284 * @dev: Device to lookup or add devres for
285 * @new_res: Pointer to new initialized devres to add if not found
286 * @match: Match function (optional)
287 * @match_data: Data for the match function
288 *
289 * Find the latest devres of @dev which has the same release function
290 * as @new_res and for which @match return 1. If found, @new_res is
291 * freed; otherwise, @new_res is added atomically.
292 *
293 * RETURNS:
294 * Pointer to found or added devres.
295 */
296void * devres_get(struct device *dev, void *new_res,
297 dr_match_t match, void *match_data)
298{
299 struct devres *new_dr = container_of(new_res, struct devres, data);
300 struct devres *dr;
301 unsigned long flags;
302
303 spin_lock_irqsave(&dev->devres_lock, flags);
304 dr = find_dr(dev, new_dr->node.release, match, match_data);
305 if (!dr) {
306 add_dr(dev, &new_dr->node);
307 dr = new_dr;
308 new_res = NULL;
309 }
310 spin_unlock_irqrestore(&dev->devres_lock, flags);
311 devres_free(new_res);
312
313 return dr->data;
314}
315EXPORT_SYMBOL_GPL(devres_get);
316
317/**
318 * devres_remove - Find a device resource and remove it
319 * @dev: Device to find resource from
320 * @release: Look for resources associated with this release function
321 * @match: Match function (optional)
322 * @match_data: Data for the match function
323 *
324 * Find the latest devres of @dev associated with @release and for
325 * which @match returns 1. If @match is NULL, it's considered to
326 * match all. If found, the resource is removed atomically and
327 * returned.
328 *
329 * RETURNS:
330 * Pointer to removed devres on success, NULL if not found.
331 */
332void * devres_remove(struct device *dev, dr_release_t release,
333 dr_match_t match, void *match_data)
334{
335 struct devres *dr;
336 unsigned long flags;
337
338 spin_lock_irqsave(&dev->devres_lock, flags);
339 dr = find_dr(dev, release, match, match_data);
340 if (dr) {
341 list_del_init(&dr->node.entry);
342 devres_log(dev, &dr->node, "REM");
343 }
344 spin_unlock_irqrestore(&dev->devres_lock, flags);
345
346 if (dr)
347 return dr->data;
348 return NULL;
349}
350EXPORT_SYMBOL_GPL(devres_remove);
351
352/**
353 * devres_destroy - Find a device resource and destroy it
354 * @dev: Device to find resource from
355 * @release: Look for resources associated with this release function
356 * @match: Match function (optional)
357 * @match_data: Data for the match function
358 *
359 * Find the latest devres of @dev associated with @release and for
360 * which @match returns 1. If @match is NULL, it's considered to
361 * match all. If found, the resource is removed atomically and freed.
362 *
363 * Note that the release function for the resource will not be called,
364 * only the devres-allocated data will be freed. The caller becomes
365 * responsible for freeing any other data.
366 *
367 * RETURNS:
368 * 0 if devres is found and freed, -ENOENT if not found.
369 */
370int devres_destroy(struct device *dev, dr_release_t release,
371 dr_match_t match, void *match_data)
372{
373 void *res;
374
375 res = devres_remove(dev, release, match, match_data);
376 if (unlikely(!res))
377 return -ENOENT;
378
379 devres_free(res);
380 return 0;
381}
382EXPORT_SYMBOL_GPL(devres_destroy);
383
384
385/**
386 * devres_release - Find a device resource and destroy it, calling release
387 * @dev: Device to find resource from
388 * @release: Look for resources associated with this release function
389 * @match: Match function (optional)
390 * @match_data: Data for the match function
391 *
392 * Find the latest devres of @dev associated with @release and for
393 * which @match returns 1. If @match is NULL, it's considered to
394 * match all. If found, the resource is removed atomically, the
395 * release function called and the resource freed.
396 *
397 * RETURNS:
398 * 0 if devres is found and freed, -ENOENT if not found.
399 */
400int devres_release(struct device *dev, dr_release_t release,
401 dr_match_t match, void *match_data)
402{
403 void *res;
404
405 res = devres_remove(dev, release, match, match_data);
406 if (unlikely(!res))
407 return -ENOENT;
408
409 (*release)(dev, res);
410 devres_free(res);
411 return 0;
412}
413EXPORT_SYMBOL_GPL(devres_release);
414
415static int remove_nodes(struct device *dev,
416 struct list_head *first, struct list_head *end,
417 struct list_head *todo)
418{
419 int cnt = 0, nr_groups = 0;
420 struct list_head *cur;
421
422 /* First pass - move normal devres entries to @todo and clear
423 * devres_group colors.
424 */
425 cur = first;
426 while (cur != end) {
427 struct devres_node *node;
428 struct devres_group *grp;
429
430 node = list_entry(cur, struct devres_node, entry);
431 cur = cur->next;
432
433 grp = node_to_group(node);
434 if (grp) {
435 /* clear color of group markers in the first pass */
436 grp->color = 0;
437 nr_groups++;
438 } else {
439 /* regular devres entry */
440 if (&node->entry == first)
441 first = first->next;
442 list_move_tail(&node->entry, todo);
443 cnt++;
444 }
445 }
446
447 if (!nr_groups)
448 return cnt;
449
450 /* Second pass - Scan groups and color them. A group gets
451 * color value of two iff the group is wholly contained in
452 * [cur, end). That is, for a closed group, both opening and
453 * closing markers should be in the range, while just the
454 * opening marker is enough for an open group.
455 */
456 cur = first;
457 while (cur != end) {
458 struct devres_node *node;
459 struct devres_group *grp;
460
461 node = list_entry(cur, struct devres_node, entry);
462 cur = cur->next;
463
464 grp = node_to_group(node);
465 BUG_ON(!grp || list_empty(&grp->node[0].entry));
466
467 grp->color++;
468 if (list_empty(&grp->node[1].entry))
469 grp->color++;
470
471 BUG_ON(grp->color <= 0 || grp->color > 2);
472 if (grp->color == 2) {
473 /* No need to update cur or end. The removed
474 * nodes are always before both.
475 */
476 list_move_tail(&grp->node[0].entry, todo);
477 list_del_init(&grp->node[1].entry);
478 }
479 }
480
481 return cnt;
482}
483
484static int release_nodes(struct device *dev, struct list_head *first,
485 struct list_head *end, unsigned long flags)
486 __releases(&dev->devres_lock)
487{
488 LIST_HEAD(todo);
489 int cnt;
490 struct devres *dr, *tmp;
491
492 cnt = remove_nodes(dev, first, end, &todo);
493
494 spin_unlock_irqrestore(&dev->devres_lock, flags);
495
496 /* Release. Note that both devres and devres_group are
497 * handled as devres in the following loop. This is safe.
498 */
499 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
500 devres_log(dev, &dr->node, "REL");
501 dr->node.release(dev, dr->data);
502 kfree(dr);
503 }
504
505 return cnt;
506}
507
508/**
509 * devres_release_all - Release all managed resources
510 * @dev: Device to release resources for
511 *
512 * Release all resources associated with @dev. This function is
513 * called on driver detach.
514 */
515int devres_release_all(struct device *dev)
516{
517 unsigned long flags;
518
519 /* Looks like an uninitialized device structure */
520 if (WARN_ON(dev->devres_head.next == NULL))
521 return -ENODEV;
522 spin_lock_irqsave(&dev->devres_lock, flags);
523 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
524 flags);
525}
526
527/**
528 * devres_open_group - Open a new devres group
529 * @dev: Device to open devres group for
530 * @id: Separator ID
531 * @gfp: Allocation flags
532 *
533 * Open a new devres group for @dev with @id. For @id, using a
534 * pointer to an object which won't be used for another group is
535 * recommended. If @id is NULL, address-wise unique ID is created.
536 *
537 * RETURNS:
538 * ID of the new group, NULL on failure.
539 */
540void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
541{
542 struct devres_group *grp;
543 unsigned long flags;
544
545 grp = kmalloc(sizeof(*grp), gfp);
546 if (unlikely(!grp))
547 return NULL;
548
549 grp->node[0].release = &group_open_release;
550 grp->node[1].release = &group_close_release;
551 INIT_LIST_HEAD(&grp->node[0].entry);
552 INIT_LIST_HEAD(&grp->node[1].entry);
553 set_node_dbginfo(&grp->node[0], "grp<", 0);
554 set_node_dbginfo(&grp->node[1], "grp>", 0);
555 grp->id = grp;
556 if (id)
557 grp->id = id;
558
559 spin_lock_irqsave(&dev->devres_lock, flags);
560 add_dr(dev, &grp->node[0]);
561 spin_unlock_irqrestore(&dev->devres_lock, flags);
562 return grp->id;
563}
564EXPORT_SYMBOL_GPL(devres_open_group);
565
566/* Find devres group with ID @id. If @id is NULL, look for the latest. */
567static struct devres_group * find_group(struct device *dev, void *id)
568{
569 struct devres_node *node;
570
571 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
572 struct devres_group *grp;
573
574 if (node->release != &group_open_release)
575 continue;
576
577 grp = container_of(node, struct devres_group, node[0]);
578
579 if (id) {
580 if (grp->id == id)
581 return grp;
582 } else if (list_empty(&grp->node[1].entry))
583 return grp;
584 }
585
586 return NULL;
587}
588
589/**
590 * devres_close_group - Close a devres group
591 * @dev: Device to close devres group for
592 * @id: ID of target group, can be NULL
593 *
594 * Close the group identified by @id. If @id is NULL, the latest open
595 * group is selected.
596 */
597void devres_close_group(struct device *dev, void *id)
598{
599 struct devres_group *grp;
600 unsigned long flags;
601
602 spin_lock_irqsave(&dev->devres_lock, flags);
603
604 grp = find_group(dev, id);
605 if (grp)
606 add_dr(dev, &grp->node[1]);
607 else
608 WARN_ON(1);
609
610 spin_unlock_irqrestore(&dev->devres_lock, flags);
611}
612EXPORT_SYMBOL_GPL(devres_close_group);
613
614/**
615 * devres_remove_group - Remove a devres group
616 * @dev: Device to remove group for
617 * @id: ID of target group, can be NULL
618 *
619 * Remove the group identified by @id. If @id is NULL, the latest
620 * open group is selected. Note that removing a group doesn't affect
621 * any other resources.
622 */
623void devres_remove_group(struct device *dev, void *id)
624{
625 struct devres_group *grp;
626 unsigned long flags;
627
628 spin_lock_irqsave(&dev->devres_lock, flags);
629
630 grp = find_group(dev, id);
631 if (grp) {
632 list_del_init(&grp->node[0].entry);
633 list_del_init(&grp->node[1].entry);
634 devres_log(dev, &grp->node[0], "REM");
635 } else
636 WARN_ON(1);
637
638 spin_unlock_irqrestore(&dev->devres_lock, flags);
639
640 kfree(grp);
641}
642EXPORT_SYMBOL_GPL(devres_remove_group);
643
644/**
645 * devres_release_group - Release resources in a devres group
646 * @dev: Device to release group for
647 * @id: ID of target group, can be NULL
648 *
649 * Release all resources in the group identified by @id. If @id is
650 * NULL, the latest open group is selected. The selected group and
651 * groups properly nested inside the selected group are removed.
652 *
653 * RETURNS:
654 * The number of released non-group resources.
655 */
656int devres_release_group(struct device *dev, void *id)
657{
658 struct devres_group *grp;
659 unsigned long flags;
660 int cnt = 0;
661
662 spin_lock_irqsave(&dev->devres_lock, flags);
663
664 grp = find_group(dev, id);
665 if (grp) {
666 struct list_head *first = &grp->node[0].entry;
667 struct list_head *end = &dev->devres_head;
668
669 if (!list_empty(&grp->node[1].entry))
670 end = grp->node[1].entry.next;
671
672 cnt = release_nodes(dev, first, end, flags);
673 } else {
674 WARN_ON(1);
675 spin_unlock_irqrestore(&dev->devres_lock, flags);
676 }
677
678 return cnt;
679}
680EXPORT_SYMBOL_GPL(devres_release_group);
681
682/*
683 * Custom devres actions allow inserting a simple function call
684 * into the teadown sequence.
685 */
686
687struct action_devres {
688 void *data;
689 void (*action)(void *);
690};
691
692static int devm_action_match(struct device *dev, void *res, void *p)
693{
694 struct action_devres *devres = res;
695 struct action_devres *target = p;
696
697 return devres->action == target->action &&
698 devres->data == target->data;
699}
700
701static void devm_action_release(struct device *dev, void *res)
702{
703 struct action_devres *devres = res;
704
705 devres->action(devres->data);
706}
707
708/**
709 * devm_add_action() - add a custom action to list of managed resources
710 * @dev: Device that owns the action
711 * @action: Function that should be called
712 * @data: Pointer to data passed to @action implementation
713 *
714 * This adds a custom action to the list of managed resources so that
715 * it gets executed as part of standard resource unwinding.
716 */
717int devm_add_action(struct device *dev, void (*action)(void *), void *data)
718{
719 struct action_devres *devres;
720
721 devres = devres_alloc(devm_action_release,
722 sizeof(struct action_devres), GFP_KERNEL);
723 if (!devres)
724 return -ENOMEM;
725
726 devres->data = data;
727 devres->action = action;
728
729 devres_add(dev, devres);
730 return 0;
731}
732EXPORT_SYMBOL_GPL(devm_add_action);
733
734/**
735 * devm_remove_action() - removes previously added custom action
736 * @dev: Device that owns the action
737 * @action: Function implementing the action
738 * @data: Pointer to data passed to @action implementation
739 *
740 * Removes instance of @action previously added by devm_add_action().
741 * Both action and data should match one of the existing entries.
742 */
743void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
744{
745 struct action_devres devres = {
746 .data = data,
747 .action = action,
748 };
749
750 WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
751 &devres));
752
753}
754EXPORT_SYMBOL_GPL(devm_remove_action);
755
756/*
757 * Managed kmalloc/kfree
758 */
759static void devm_kmalloc_release(struct device *dev, void *res)
760{
761 /* noop */
762}
763
764static int devm_kmalloc_match(struct device *dev, void *res, void *data)
765{
766 return res == data;
767}
768
769/**
770 * devm_kmalloc - Resource-managed kmalloc
771 * @dev: Device to allocate memory for
772 * @size: Allocation size
773 * @gfp: Allocation gfp flags
774 *
775 * Managed kmalloc. Memory allocated with this function is
776 * automatically freed on driver detach. Like all other devres
777 * resources, guaranteed alignment is unsigned long long.
778 *
779 * RETURNS:
780 * Pointer to allocated memory on success, NULL on failure.
781 */
782void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
783{
784 struct devres *dr;
785
786 /* use raw alloc_dr for kmalloc caller tracing */
787 dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
788 if (unlikely(!dr))
789 return NULL;
790
791 /*
792 * This is named devm_kzalloc_release for historical reasons
793 * The initial implementation did not support kmalloc, only kzalloc
794 */
795 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
796 devres_add(dev, dr->data);
797 return dr->data;
798}
799EXPORT_SYMBOL_GPL(devm_kmalloc);
800
801/**
802 * devm_kstrdup - Allocate resource managed space and
803 * copy an existing string into that.
804 * @dev: Device to allocate memory for
805 * @s: the string to duplicate
806 * @gfp: the GFP mask used in the devm_kmalloc() call when
807 * allocating memory
808 * RETURNS:
809 * Pointer to allocated string on success, NULL on failure.
810 */
811char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
812{
813 size_t size;
814 char *buf;
815
816 if (!s)
817 return NULL;
818
819 size = strlen(s) + 1;
820 buf = devm_kmalloc(dev, size, gfp);
821 if (buf)
822 memcpy(buf, s, size);
823 return buf;
824}
825EXPORT_SYMBOL_GPL(devm_kstrdup);
826
827/**
828 * devm_kvasprintf - Allocate resource managed space and format a string
829 * into that.
830 * @dev: Device to allocate memory for
831 * @gfp: the GFP mask used in the devm_kmalloc() call when
832 * allocating memory
833 * @fmt: The printf()-style format string
834 * @ap: Arguments for the format string
835 * RETURNS:
836 * Pointer to allocated string on success, NULL on failure.
837 */
838char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
839 va_list ap)
840{
841 unsigned int len;
842 char *p;
843 va_list aq;
844
845 va_copy(aq, ap);
846 len = vsnprintf(NULL, 0, fmt, aq);
847 va_end(aq);
848
849 p = devm_kmalloc(dev, len+1, gfp);
850 if (!p)
851 return NULL;
852
853 vsnprintf(p, len+1, fmt, ap);
854
855 return p;
856}
857EXPORT_SYMBOL(devm_kvasprintf);
858
859/**
860 * devm_kasprintf - Allocate resource managed space and format a string
861 * into that.
862 * @dev: Device to allocate memory for
863 * @gfp: the GFP mask used in the devm_kmalloc() call when
864 * allocating memory
865 * @fmt: The printf()-style format string
866 * @...: Arguments for the format string
867 * RETURNS:
868 * Pointer to allocated string on success, NULL on failure.
869 */
870char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
871{
872 va_list ap;
873 char *p;
874
875 va_start(ap, fmt);
876 p = devm_kvasprintf(dev, gfp, fmt, ap);
877 va_end(ap);
878
879 return p;
880}
881EXPORT_SYMBOL_GPL(devm_kasprintf);
882
883/**
884 * devm_kfree - Resource-managed kfree
885 * @dev: Device this memory belongs to
886 * @p: Memory to free
887 *
888 * Free memory allocated with devm_kmalloc().
889 */
890void devm_kfree(struct device *dev, void *p)
891{
892 int rc;
893
894 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
895 WARN_ON(rc);
896}
897EXPORT_SYMBOL_GPL(devm_kfree);
898
899/**
900 * devm_kmemdup - Resource-managed kmemdup
901 * @dev: Device this memory belongs to
902 * @src: Memory region to duplicate
903 * @len: Memory region length
904 * @gfp: GFP mask to use
905 *
906 * Duplicate region of a memory using resource managed kmalloc
907 */
908void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
909{
910 void *p;
911
912 p = devm_kmalloc(dev, len, gfp);
913 if (p)
914 memcpy(p, src, len);
915
916 return p;
917}
918EXPORT_SYMBOL_GPL(devm_kmemdup);
919
920struct pages_devres {
921 unsigned long addr;
922 unsigned int order;
923};
924
925static int devm_pages_match(struct device *dev, void *res, void *p)
926{
927 struct pages_devres *devres = res;
928 struct pages_devres *target = p;
929
930 return devres->addr == target->addr;
931}
932
933static void devm_pages_release(struct device *dev, void *res)
934{
935 struct pages_devres *devres = res;
936
937 free_pages(devres->addr, devres->order);
938}
939
940/**
941 * devm_get_free_pages - Resource-managed __get_free_pages
942 * @dev: Device to allocate memory for
943 * @gfp_mask: Allocation gfp flags
944 * @order: Allocation size is (1 << order) pages
945 *
946 * Managed get_free_pages. Memory allocated with this function is
947 * automatically freed on driver detach.
948 *
949 * RETURNS:
950 * Address of allocated memory on success, 0 on failure.
951 */
952
953unsigned long devm_get_free_pages(struct device *dev,
954 gfp_t gfp_mask, unsigned int order)
955{
956 struct pages_devres *devres;
957 unsigned long addr;
958
959 addr = __get_free_pages(gfp_mask, order);
960
961 if (unlikely(!addr))
962 return 0;
963
964 devres = devres_alloc(devm_pages_release,
965 sizeof(struct pages_devres), GFP_KERNEL);
966 if (unlikely(!devres)) {
967 free_pages(addr, order);
968 return 0;
969 }
970
971 devres->addr = addr;
972 devres->order = order;
973
974 devres_add(dev, devres);
975 return addr;
976}
977EXPORT_SYMBOL_GPL(devm_get_free_pages);
978
979/**
980 * devm_free_pages - Resource-managed free_pages
981 * @dev: Device this memory belongs to
982 * @addr: Memory to free
983 *
984 * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
985 * there is no need to supply the @order.
986 */
987void devm_free_pages(struct device *dev, unsigned long addr)
988{
989 struct pages_devres devres = { .addr = addr };
990
991 WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
992 &devres));
993}
994EXPORT_SYMBOL_GPL(devm_free_pages);
995
996static void devm_percpu_release(struct device *dev, void *pdata)
997{
998 void __percpu *p;
999
1000 p = *(void __percpu **)pdata;
1001 free_percpu(p);
1002}
1003
1004static int devm_percpu_match(struct device *dev, void *data, void *p)
1005{
1006 struct devres *devr = container_of(data, struct devres, data);
1007
1008 return *(void **)devr->data == p;
1009}
1010
1011/**
1012 * __devm_alloc_percpu - Resource-managed alloc_percpu
1013 * @dev: Device to allocate per-cpu memory for
1014 * @size: Size of per-cpu memory to allocate
1015 * @align: Alignment of per-cpu memory to allocate
1016 *
1017 * Managed alloc_percpu. Per-cpu memory allocated with this function is
1018 * automatically freed on driver detach.
1019 *
1020 * RETURNS:
1021 * Pointer to allocated memory on success, NULL on failure.
1022 */
1023void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1024 size_t align)
1025{
1026 void *p;
1027 void __percpu *pcpu;
1028
1029 pcpu = __alloc_percpu(size, align);
1030 if (!pcpu)
1031 return NULL;
1032
1033 p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
1034 if (!p) {
1035 free_percpu(pcpu);
1036 return NULL;
1037 }
1038
1039 *(void __percpu **)p = pcpu;
1040
1041 devres_add(dev, p);
1042
1043 return pcpu;
1044}
1045EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1046
1047/**
1048 * devm_free_percpu - Resource-managed free_percpu
1049 * @dev: Device this memory belongs to
1050 * @pdata: Per-cpu memory to free
1051 *
1052 * Free memory allocated with devm_alloc_percpu().
1053 */
1054void devm_free_percpu(struct device *dev, void __percpu *pdata)
1055{
1056 WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
1057 (void *)pdata));
1058}
1059EXPORT_SYMBOL_GPL(devm_free_percpu);