xref: /openbmc/linux/drivers/base/devres.c (revision a8c5cb99)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/base/devres.c - device resource management
4  *
5  * Copyright (c) 2006  SUSE Linux Products GmbH
6  * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/percpu.h>
13 
14 #include "base.h"
15 
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
23 };
24 
25 struct devres {
26 	struct devres_node		node;
27 	/* -- 3 pointers */
28 	unsigned long long		data[];	/* guarantee ull alignment */
29 };
30 
31 struct devres_group {
32 	struct devres_node		node[2];
33 	void				*id;
34 	int				color;
35 	/* -- 8 pointers */
36 };
37 
38 #ifdef CONFIG_DEBUG_DEVRES
39 static int log_devres = 0;
40 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
41 
42 static 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 
49 static 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  */
65 static void group_open_release(struct device *dev, void *res)
66 {
67 	/* noop */
68 }
69 
70 static void group_close_release(struct device *dev, void *res)
71 {
72 	/* noop */
73 }
74 
75 static 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 
84 static __always_inline struct devres * alloc_dr(dr_release_t release,
85 						size_t size, gfp_t gfp, int nid)
86 {
87 	size_t tot_size;
88 	struct devres *dr;
89 
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;
94 
95 	dr = kmalloc_node_track_caller(tot_size, gfp, nid);
96 	if (unlikely(!dr))
97 		return NULL;
98 
99 	memset(dr, 0, offsetof(struct devres, data));
100 
101 	INIT_LIST_HEAD(&dr->node.entry);
102 	dr->node.release = release;
103 	return dr;
104 }
105 
106 static void add_dr(struct device *dev, struct devres_node *node)
107 {
108 	devres_log(dev, node, "ADD");
109 	BUG_ON(!list_empty(&node->entry));
110 	list_add_tail(&node->entry, &dev->devres_head);
111 }
112 
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)
116 {
117 	struct devres *dr;
118 
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;
124 }
125 EXPORT_SYMBOL_GPL(__devres_alloc_node);
126 #else
127 /**
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
133  *
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.
137  *
138  * RETURNS:
139  * Pointer to allocated devres on success, NULL on failure.
140  */
141 void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
142 {
143 	struct devres *dr;
144 
145 	dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
146 	if (unlikely(!dr))
147 		return NULL;
148 	return dr->data;
149 }
150 EXPORT_SYMBOL_GPL(devres_alloc_node);
151 #endif
152 
153 /**
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
161  *
162  * Call @fn for each devres of @dev which is associated with @release
163  * and for which @match returns 1.
164  *
165  * RETURNS:
166  * 	void
167  */
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)
172 {
173 	struct devres_node *node;
174 	struct devres_node *tmp;
175 	unsigned long flags;
176 
177 	if (!fn)
178 		return;
179 
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);
184 
185 		if (node->release != release)
186 			continue;
187 		if (match && !match(dev, dr->data, match_data))
188 			continue;
189 		fn(dev, dr->data, data);
190 	}
191 	spin_unlock_irqrestore(&dev->devres_lock, flags);
192 }
193 EXPORT_SYMBOL_GPL(devres_for_each_res);
194 
195 /**
196  * devres_free - Free device resource data
197  * @res: Pointer to devres data to free
198  *
199  * Free devres created with devres_alloc().
200  */
201 void devres_free(void *res)
202 {
203 	if (res) {
204 		struct devres *dr = container_of(res, struct devres, data);
205 
206 		BUG_ON(!list_empty(&dr->node.entry));
207 		kfree(dr);
208 	}
209 }
210 EXPORT_SYMBOL_GPL(devres_free);
211 
212 /**
213  * devres_add - Register device resource
214  * @dev: Device to add resource to
215  * @res: Resource to register
216  *
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.
220  */
221 void devres_add(struct device *dev, void *res)
222 {
223 	struct devres *dr = container_of(res, struct devres, data);
224 	unsigned long flags;
225 
226 	spin_lock_irqsave(&dev->devres_lock, flags);
227 	add_dr(dev, &dr->node);
228 	spin_unlock_irqrestore(&dev->devres_lock, flags);
229 }
230 EXPORT_SYMBOL_GPL(devres_add);
231 
232 static struct devres *find_dr(struct device *dev, dr_release_t release,
233 			      dr_match_t match, void *match_data)
234 {
235 	struct devres_node *node;
236 
237 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
238 		struct devres *dr = container_of(node, struct devres, node);
239 
240 		if (node->release != release)
241 			continue;
242 		if (match && !match(dev, dr->data, match_data))
243 			continue;
244 		return dr;
245 	}
246 
247 	return NULL;
248 }
249 
250 /**
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
256  *
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.
260  *
261  * RETURNS:
262  * Pointer to found devres, NULL if not found.
263  */
264 void * devres_find(struct device *dev, dr_release_t release,
265 		   dr_match_t match, void *match_data)
266 {
267 	struct devres *dr;
268 	unsigned long flags;
269 
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);
273 
274 	if (dr)
275 		return dr->data;
276 	return NULL;
277 }
278 EXPORT_SYMBOL_GPL(devres_find);
279 
280 /**
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
286  *
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.
290  *
291  * RETURNS:
292  * Pointer to found or added devres.
293  */
294 void * devres_get(struct device *dev, void *new_res,
295 		  dr_match_t match, void *match_data)
296 {
297 	struct devres *new_dr = container_of(new_res, struct devres, data);
298 	struct devres *dr;
299 	unsigned long flags;
300 
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;
307 	}
308 	spin_unlock_irqrestore(&dev->devres_lock, flags);
309 	devres_free(new_res);
310 
311 	return dr->data;
312 }
313 EXPORT_SYMBOL_GPL(devres_get);
314 
315 /**
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
321  *
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.
326  *
327  * RETURNS:
328  * Pointer to removed devres on success, NULL if not found.
329  */
330 void * devres_remove(struct device *dev, dr_release_t release,
331 		     dr_match_t match, void *match_data)
332 {
333 	struct devres *dr;
334 	unsigned long flags;
335 
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");
341 	}
342 	spin_unlock_irqrestore(&dev->devres_lock, flags);
343 
344 	if (dr)
345 		return dr->data;
346 	return NULL;
347 }
348 EXPORT_SYMBOL_GPL(devres_remove);
349 
350 /**
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
356  *
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.
360  *
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.
364  *
365  * RETURNS:
366  * 0 if devres is found and freed, -ENOENT if not found.
367  */
368 int devres_destroy(struct device *dev, dr_release_t release,
369 		   dr_match_t match, void *match_data)
370 {
371 	void *res;
372 
373 	res = devres_remove(dev, release, match, match_data);
374 	if (unlikely(!res))
375 		return -ENOENT;
376 
377 	devres_free(res);
378 	return 0;
379 }
380 EXPORT_SYMBOL_GPL(devres_destroy);
381 
382 
383 /**
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
389  *
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.
394  *
395  * RETURNS:
396  * 0 if devres is found and freed, -ENOENT if not found.
397  */
398 int devres_release(struct device *dev, dr_release_t release,
399 		   dr_match_t match, void *match_data)
400 {
401 	void *res;
402 
403 	res = devres_remove(dev, release, match, match_data);
404 	if (unlikely(!res))
405 		return -ENOENT;
406 
407 	(*release)(dev, res);
408 	devres_free(res);
409 	return 0;
410 }
411 EXPORT_SYMBOL_GPL(devres_release);
412 
413 static int remove_nodes(struct device *dev,
414 			struct list_head *first, struct list_head *end,
415 			struct list_head *todo)
416 {
417 	int cnt = 0, nr_groups = 0;
418 	struct list_head *cur;
419 
420 	/* First pass - move normal devres entries to @todo and clear
421 	 * devres_group colors.
422 	 */
423 	cur = first;
424 	while (cur != end) {
425 		struct devres_node *node;
426 		struct devres_group *grp;
427 
428 		node = list_entry(cur, struct devres_node, entry);
429 		cur = cur->next;
430 
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++;
442 		}
443 	}
444 
445 	if (!nr_groups)
446 		return cnt;
447 
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.
453 	 */
454 	cur = first;
455 	while (cur != end) {
456 		struct devres_node *node;
457 		struct devres_group *grp;
458 
459 		node = list_entry(cur, struct devres_node, entry);
460 		cur = cur->next;
461 
462 		grp = node_to_group(node);
463 		BUG_ON(!grp || list_empty(&grp->node[0].entry));
464 
465 		grp->color++;
466 		if (list_empty(&grp->node[1].entry))
467 			grp->color++;
468 
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.
473 			 */
474 			list_move_tail(&grp->node[0].entry, todo);
475 			list_del_init(&grp->node[1].entry);
476 		}
477 	}
478 
479 	return cnt;
480 }
481 
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)
485 {
486 	LIST_HEAD(todo);
487 	int cnt;
488 	struct devres *dr, *tmp;
489 
490 	cnt = remove_nodes(dev, first, end, &todo);
491 
492 	spin_unlock_irqrestore(&dev->devres_lock, flags);
493 
494 	/* Release.  Note that both devres and devres_group are
495 	 * handled as devres in the following loop.  This is safe.
496 	 */
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);
501 	}
502 
503 	return cnt;
504 }
505 
506 /**
507  * devres_release_all - Release all managed resources
508  * @dev: Device to release resources for
509  *
510  * Release all resources associated with @dev.  This function is
511  * called on driver detach.
512  */
513 int devres_release_all(struct device *dev)
514 {
515 	unsigned long flags;
516 
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);
523 }
524 
525 /**
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
530  *
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.
534  *
535  * RETURNS:
536  * ID of the new group, NULL on failure.
537  */
538 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
539 {
540 	struct devres_group *grp;
541 	unsigned long flags;
542 
543 	grp = kmalloc(sizeof(*grp), gfp);
544 	if (unlikely(!grp))
545 		return NULL;
546 
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;
556 
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;
561 }
562 EXPORT_SYMBOL_GPL(devres_open_group);
563 
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)
566 {
567 	struct devres_node *node;
568 
569 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
570 		struct devres_group *grp;
571 
572 		if (node->release != &group_open_release)
573 			continue;
574 
575 		grp = container_of(node, struct devres_group, node[0]);
576 
577 		if (id) {
578 			if (grp->id == id)
579 				return grp;
580 		} else if (list_empty(&grp->node[1].entry))
581 			return grp;
582 	}
583 
584 	return NULL;
585 }
586 
587 /**
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
591  *
592  * Close the group identified by @id.  If @id is NULL, the latest open
593  * group is selected.
594  */
595 void devres_close_group(struct device *dev, void *id)
596 {
597 	struct devres_group *grp;
598 	unsigned long flags;
599 
600 	spin_lock_irqsave(&dev->devres_lock, flags);
601 
602 	grp = find_group(dev, id);
603 	if (grp)
604 		add_dr(dev, &grp->node[1]);
605 	else
606 		WARN_ON(1);
607 
608 	spin_unlock_irqrestore(&dev->devres_lock, flags);
609 }
610 EXPORT_SYMBOL_GPL(devres_close_group);
611 
612 /**
613  * devres_remove_group - Remove a devres group
614  * @dev: Device to remove group for
615  * @id: ID of target group, can be NULL
616  *
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.
620  */
621 void devres_remove_group(struct device *dev, void *id)
622 {
623 	struct devres_group *grp;
624 	unsigned long flags;
625 
626 	spin_lock_irqsave(&dev->devres_lock, flags);
627 
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);
635 
636 	spin_unlock_irqrestore(&dev->devres_lock, flags);
637 
638 	kfree(grp);
639 }
640 EXPORT_SYMBOL_GPL(devres_remove_group);
641 
642 /**
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
646  *
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.
650  *
651  * RETURNS:
652  * The number of released non-group resources.
653  */
654 int devres_release_group(struct device *dev, void *id)
655 {
656 	struct devres_group *grp;
657 	unsigned long flags;
658 	int cnt = 0;
659 
660 	spin_lock_irqsave(&dev->devres_lock, flags);
661 
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;
666 
667 		if (!list_empty(&grp->node[1].entry))
668 			end = grp->node[1].entry.next;
669 
670 		cnt = release_nodes(dev, first, end, flags);
671 	} else {
672 		WARN_ON(1);
673 		spin_unlock_irqrestore(&dev->devres_lock, flags);
674 	}
675 
676 	return cnt;
677 }
678 EXPORT_SYMBOL_GPL(devres_release_group);
679 
680 /*
681  * Custom devres actions allow inserting a simple function call
682  * into the teadown sequence.
683  */
684 
685 struct action_devres {
686 	void *data;
687 	void (*action)(void *);
688 };
689 
690 static int devm_action_match(struct device *dev, void *res, void *p)
691 {
692 	struct action_devres *devres = res;
693 	struct action_devres *target = p;
694 
695 	return devres->action == target->action &&
696 	       devres->data == target->data;
697 }
698 
699 static void devm_action_release(struct device *dev, void *res)
700 {
701 	struct action_devres *devres = res;
702 
703 	devres->action(devres->data);
704 }
705 
706 /**
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
711  *
712  * This adds a custom action to the list of managed resources so that
713  * it gets executed as part of standard resource unwinding.
714  */
715 int devm_add_action(struct device *dev, void (*action)(void *), void *data)
716 {
717 	struct action_devres *devres;
718 
719 	devres = devres_alloc(devm_action_release,
720 			      sizeof(struct action_devres), GFP_KERNEL);
721 	if (!devres)
722 		return -ENOMEM;
723 
724 	devres->data = data;
725 	devres->action = action;
726 
727 	devres_add(dev, devres);
728 	return 0;
729 }
730 EXPORT_SYMBOL_GPL(devm_add_action);
731 
732 /**
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
737  *
738  * Removes instance of @action previously added by devm_add_action().
739  * Both action and data should match one of the existing entries.
740  */
741 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
742 {
743 	struct action_devres devres = {
744 		.data = data,
745 		.action = action,
746 	};
747 
748 	WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
749 			       &devres));
750 
751 }
752 EXPORT_SYMBOL_GPL(devm_remove_action);
753 
754 /*
755  * Managed kmalloc/kfree
756  */
757 static void devm_kmalloc_release(struct device *dev, void *res)
758 {
759 	/* noop */
760 }
761 
762 static int devm_kmalloc_match(struct device *dev, void *res, void *data)
763 {
764 	return res == data;
765 }
766 
767 /**
768  * devm_kmalloc - Resource-managed kmalloc
769  * @dev: Device to allocate memory for
770  * @size: Allocation size
771  * @gfp: Allocation gfp flags
772  *
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.
776  *
777  * RETURNS:
778  * Pointer to allocated memory on success, NULL on failure.
779  */
780 void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
781 {
782 	struct devres *dr;
783 
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;
788 
789 	/*
790 	 * This is named devm_kzalloc_release for historical reasons
791 	 * The initial implementation did not support kmalloc, only kzalloc
792 	 */
793 	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
794 	devres_add(dev, dr->data);
795 	return dr->data;
796 }
797 EXPORT_SYMBOL_GPL(devm_kmalloc);
798 
799 /**
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.
808  */
809 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
810 {
811 	size_t size;
812 	char *buf;
813 
814 	if (!s)
815 		return NULL;
816 
817 	size = strlen(s) + 1;
818 	buf = devm_kmalloc(dev, size, gfp);
819 	if (buf)
820 		memcpy(buf, s, size);
821 	return buf;
822 }
823 EXPORT_SYMBOL_GPL(devm_kstrdup);
824 
825 /**
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.
835  */
836 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
837 		      va_list ap)
838 {
839 	unsigned int len;
840 	char *p;
841 	va_list aq;
842 
843 	va_copy(aq, ap);
844 	len = vsnprintf(NULL, 0, fmt, aq);
845 	va_end(aq);
846 
847 	p = devm_kmalloc(dev, len+1, gfp);
848 	if (!p)
849 		return NULL;
850 
851 	vsnprintf(p, len+1, fmt, ap);
852 
853 	return p;
854 }
855 EXPORT_SYMBOL(devm_kvasprintf);
856 
857 /**
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.
867  */
868 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
869 {
870 	va_list ap;
871 	char *p;
872 
873 	va_start(ap, fmt);
874 	p = devm_kvasprintf(dev, gfp, fmt, ap);
875 	va_end(ap);
876 
877 	return p;
878 }
879 EXPORT_SYMBOL_GPL(devm_kasprintf);
880 
881 /**
882  * devm_kfree - Resource-managed kfree
883  * @dev: Device this memory belongs to
884  * @p: Memory to free
885  *
886  * Free memory allocated with devm_kmalloc().
887  */
888 void devm_kfree(struct device *dev, void *p)
889 {
890 	int rc;
891 
892 	rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
893 	WARN_ON(rc);
894 }
895 EXPORT_SYMBOL_GPL(devm_kfree);
896 
897 /**
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
903  *
904  * Duplicate region of a memory using resource managed kmalloc
905  */
906 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
907 {
908 	void *p;
909 
910 	p = devm_kmalloc(dev, len, gfp);
911 	if (p)
912 		memcpy(p, src, len);
913 
914 	return p;
915 }
916 EXPORT_SYMBOL_GPL(devm_kmemdup);
917 
918 struct pages_devres {
919 	unsigned long addr;
920 	unsigned int order;
921 };
922 
923 static int devm_pages_match(struct device *dev, void *res, void *p)
924 {
925 	struct pages_devres *devres = res;
926 	struct pages_devres *target = p;
927 
928 	return devres->addr == target->addr;
929 }
930 
931 static void devm_pages_release(struct device *dev, void *res)
932 {
933 	struct pages_devres *devres = res;
934 
935 	free_pages(devres->addr, devres->order);
936 }
937 
938 /**
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
943  *
944  * Managed get_free_pages.  Memory allocated with this function is
945  * automatically freed on driver detach.
946  *
947  * RETURNS:
948  * Address of allocated memory on success, 0 on failure.
949  */
950 
951 unsigned long devm_get_free_pages(struct device *dev,
952 				  gfp_t gfp_mask, unsigned int order)
953 {
954 	struct pages_devres *devres;
955 	unsigned long addr;
956 
957 	addr = __get_free_pages(gfp_mask, order);
958 
959 	if (unlikely(!addr))
960 		return 0;
961 
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;
967 	}
968 
969 	devres->addr = addr;
970 	devres->order = order;
971 
972 	devres_add(dev, devres);
973 	return addr;
974 }
975 EXPORT_SYMBOL_GPL(devm_get_free_pages);
976 
977 /**
978  * devm_free_pages - Resource-managed free_pages
979  * @dev: Device this memory belongs to
980  * @addr: Memory to free
981  *
982  * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
983  * there is no need to supply the @order.
984  */
985 void devm_free_pages(struct device *dev, unsigned long addr)
986 {
987 	struct pages_devres devres = { .addr = addr };
988 
989 	WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
990 			       &devres));
991 }
992 EXPORT_SYMBOL_GPL(devm_free_pages);
993 
994 static void devm_percpu_release(struct device *dev, void *pdata)
995 {
996 	void __percpu *p;
997 
998 	p = *(void __percpu **)pdata;
999 	free_percpu(p);
1000 }
1001 
1002 static int devm_percpu_match(struct device *dev, void *data, void *p)
1003 {
1004 	struct devres *devr = container_of(data, struct devres, data);
1005 
1006 	return *(void **)devr->data == p;
1007 }
1008 
1009 /**
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
1014  *
1015  * Managed alloc_percpu. Per-cpu memory allocated with this function is
1016  * automatically freed on driver detach.
1017  *
1018  * RETURNS:
1019  * Pointer to allocated memory on success, NULL on failure.
1020  */
1021 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1022 		size_t align)
1023 {
1024 	void *p;
1025 	void __percpu *pcpu;
1026 
1027 	pcpu = __alloc_percpu(size, align);
1028 	if (!pcpu)
1029 		return NULL;
1030 
1031 	p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
1032 	if (!p) {
1033 		free_percpu(pcpu);
1034 		return NULL;
1035 	}
1036 
1037 	*(void __percpu **)p = pcpu;
1038 
1039 	devres_add(dev, p);
1040 
1041 	return pcpu;
1042 }
1043 EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1044 
1045 /**
1046  * devm_free_percpu - Resource-managed free_percpu
1047  * @dev: Device this memory belongs to
1048  * @pdata: Per-cpu memory to free
1049  *
1050  * Free memory allocated with devm_alloc_percpu().
1051  */
1052 void devm_free_percpu(struct device *dev, void __percpu *pdata)
1053 {
1054 	WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
1055 			       (void *)pdata));
1056 }
1057 EXPORT_SYMBOL_GPL(devm_free_percpu);
1058