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 
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 
set_node_dbginfo(struct devres_node * node,const char * name,size_t size)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 
devres_log(struct device * dev,struct devres_node * node,const char * op)49 static void devres_log(struct device *dev, struct devres_node *node,
50 		       const char *op)
51 {
52 	if (unlikely(log_devres))
53 		dev_printk(KERN_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  */
group_open_release(struct device * dev,void * res)65 static void group_open_release(struct device *dev, void *res)
66 {
67 	/* noop */
68 }
69 
group_close_release(struct device * dev,void * res)70 static void group_close_release(struct device *dev, void *res)
71 {
72 	/* noop */
73 }
74 
node_to_group(struct devres_node * node)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 
alloc_dr(dr_release_t release,size_t size,gfp_t gfp)84 static __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 
add_dr(struct device * dev,struct devres_node * node)100 static 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
__devres_alloc(dr_release_t release,size_t size,gfp_t gfp,const char * name)108 void * __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 }
119 EXPORT_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  */
devres_alloc(dr_release_t release,size_t size,gfp_t gfp)134 void * 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 }
143 EXPORT_SYMBOL_GPL(devres_alloc);
144 #endif
145 
146 /**
147  * devres_free - Free device resource data
148  * @res: Pointer to devres data to free
149  *
150  * Free devres created with devres_alloc().
151  */
devres_free(void * res)152 void devres_free(void *res)
153 {
154 	if (res) {
155 		struct devres *dr = container_of(res, struct devres, data);
156 
157 		BUG_ON(!list_empty(&dr->node.entry));
158 		kfree(dr);
159 	}
160 }
161 EXPORT_SYMBOL_GPL(devres_free);
162 
163 /**
164  * devres_add - Register device resource
165  * @dev: Device to add resource to
166  * @res: Resource to register
167  *
168  * Register devres @res to @dev.  @res should have been allocated
169  * using devres_alloc().  On driver detach, the associated release
170  * function will be invoked and devres will be freed automatically.
171  */
devres_add(struct device * dev,void * res)172 void devres_add(struct device *dev, void *res)
173 {
174 	struct devres *dr = container_of(res, struct devres, data);
175 	unsigned long flags;
176 
177 	spin_lock_irqsave(&dev->devres_lock, flags);
178 	add_dr(dev, &dr->node);
179 	spin_unlock_irqrestore(&dev->devres_lock, flags);
180 }
181 EXPORT_SYMBOL_GPL(devres_add);
182 
find_dr(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)183 static struct devres *find_dr(struct device *dev, dr_release_t release,
184 			      dr_match_t match, void *match_data)
185 {
186 	struct devres_node *node;
187 
188 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
189 		struct devres *dr = container_of(node, struct devres, node);
190 
191 		if (node->release != release)
192 			continue;
193 		if (match && !match(dev, dr->data, match_data))
194 			continue;
195 		return dr;
196 	}
197 
198 	return NULL;
199 }
200 
201 /**
202  * devres_find - Find device resource
203  * @dev: Device to lookup resource from
204  * @release: Look for resources associated with this release function
205  * @match: Match function (optional)
206  * @match_data: Data for the match function
207  *
208  * Find the latest devres of @dev which is associated with @release
209  * and for which @match returns 1.  If @match is NULL, it's considered
210  * to match all.
211  *
212  * RETURNS:
213  * Pointer to found devres, NULL if not found.
214  */
devres_find(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)215 void * devres_find(struct device *dev, dr_release_t release,
216 		   dr_match_t match, void *match_data)
217 {
218 	struct devres *dr;
219 	unsigned long flags;
220 
221 	spin_lock_irqsave(&dev->devres_lock, flags);
222 	dr = find_dr(dev, release, match, match_data);
223 	spin_unlock_irqrestore(&dev->devres_lock, flags);
224 
225 	if (dr)
226 		return dr->data;
227 	return NULL;
228 }
229 EXPORT_SYMBOL_GPL(devres_find);
230 
231 /**
232  * devres_get - Find devres, if non-existent, add one atomically
233  * @dev: Device to lookup or add devres for
234  * @new_res: Pointer to new initialized devres to add if not found
235  * @match: Match function (optional)
236  * @match_data: Data for the match function
237  *
238  * Find the latest devres of @dev which has the same release function
239  * as @new_res and for which @match return 1.  If found, @new_res is
240  * freed; otherwise, @new_res is added atomically.
241  *
242  * RETURNS:
243  * Pointer to found or added devres.
244  */
devres_get(struct device * dev,void * new_res,dr_match_t match,void * match_data)245 void * devres_get(struct device *dev, void *new_res,
246 		  dr_match_t match, void *match_data)
247 {
248 	struct devres *new_dr = container_of(new_res, struct devres, data);
249 	struct devres *dr;
250 	unsigned long flags;
251 
252 	spin_lock_irqsave(&dev->devres_lock, flags);
253 	dr = find_dr(dev, new_dr->node.release, match, match_data);
254 	if (!dr) {
255 		add_dr(dev, &new_dr->node);
256 		dr = new_dr;
257 		new_dr = NULL;
258 	}
259 	spin_unlock_irqrestore(&dev->devres_lock, flags);
260 	devres_free(new_dr);
261 
262 	return dr->data;
263 }
264 EXPORT_SYMBOL_GPL(devres_get);
265 
266 /**
267  * devres_remove - Find a device resource and remove it
268  * @dev: Device to find resource from
269  * @release: Look for resources associated with this release function
270  * @match: Match function (optional)
271  * @match_data: Data for the match function
272  *
273  * Find the latest devres of @dev associated with @release and for
274  * which @match returns 1.  If @match is NULL, it's considered to
275  * match all.  If found, the resource is removed atomically and
276  * returned.
277  *
278  * RETURNS:
279  * Pointer to removed devres on success, NULL if not found.
280  */
devres_remove(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)281 void * devres_remove(struct device *dev, dr_release_t release,
282 		     dr_match_t match, void *match_data)
283 {
284 	struct devres *dr;
285 	unsigned long flags;
286 
287 	spin_lock_irqsave(&dev->devres_lock, flags);
288 	dr = find_dr(dev, release, match, match_data);
289 	if (dr) {
290 		list_del_init(&dr->node.entry);
291 		devres_log(dev, &dr->node, "REM");
292 	}
293 	spin_unlock_irqrestore(&dev->devres_lock, flags);
294 
295 	if (dr)
296 		return dr->data;
297 	return NULL;
298 }
299 EXPORT_SYMBOL_GPL(devres_remove);
300 
301 /**
302  * devres_destroy - Find a device resource and destroy it
303  * @dev: Device to find resource from
304  * @release: Look for resources associated with this release function
305  * @match: Match function (optional)
306  * @match_data: Data for the match function
307  *
308  * Find the latest devres of @dev associated with @release and for
309  * which @match returns 1.  If @match is NULL, it's considered to
310  * match all.  If found, the resource is removed atomically and freed.
311  *
312  * RETURNS:
313  * 0 if devres is found and freed, -ENOENT if not found.
314  */
devres_destroy(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)315 int devres_destroy(struct device *dev, dr_release_t release,
316 		   dr_match_t match, void *match_data)
317 {
318 	void *res;
319 
320 	res = devres_remove(dev, release, match, match_data);
321 	if (unlikely(!res))
322 		return -ENOENT;
323 
324 	devres_free(res);
325 	return 0;
326 }
327 EXPORT_SYMBOL_GPL(devres_destroy);
328 
remove_nodes(struct device * dev,struct list_head * first,struct list_head * end,struct list_head * todo)329 static int remove_nodes(struct device *dev,
330 			struct list_head *first, struct list_head *end,
331 			struct list_head *todo)
332 {
333 	int cnt = 0, nr_groups = 0;
334 	struct list_head *cur;
335 
336 	/* First pass - move normal devres entries to @todo and clear
337 	 * devres_group colors.
338 	 */
339 	cur = first;
340 	while (cur != end) {
341 		struct devres_node *node;
342 		struct devres_group *grp;
343 
344 		node = list_entry(cur, struct devres_node, entry);
345 		cur = cur->next;
346 
347 		grp = node_to_group(node);
348 		if (grp) {
349 			/* clear color of group markers in the first pass */
350 			grp->color = 0;
351 			nr_groups++;
352 		} else {
353 			/* regular devres entry */
354 			if (&node->entry == first)
355 				first = first->next;
356 			list_move_tail(&node->entry, todo);
357 			cnt++;
358 		}
359 	}
360 
361 	if (!nr_groups)
362 		return cnt;
363 
364 	/* Second pass - Scan groups and color them.  A group gets
365 	 * color value of two iff the group is wholly contained in
366 	 * [cur, end).  That is, for a closed group, both opening and
367 	 * closing markers should be in the range, while just the
368 	 * opening marker is enough for an open group.
369 	 */
370 	cur = first;
371 	while (cur != end) {
372 		struct devres_node *node;
373 		struct devres_group *grp;
374 
375 		node = list_entry(cur, struct devres_node, entry);
376 		cur = cur->next;
377 
378 		grp = node_to_group(node);
379 		BUG_ON(!grp || list_empty(&grp->node[0].entry));
380 
381 		grp->color++;
382 		if (list_empty(&grp->node[1].entry))
383 			grp->color++;
384 
385 		BUG_ON(grp->color <= 0 || grp->color > 2);
386 		if (grp->color == 2) {
387 			/* No need to update cur or end.  The removed
388 			 * nodes are always before both.
389 			 */
390 			list_move_tail(&grp->node[0].entry, todo);
391 			list_del_init(&grp->node[1].entry);
392 		}
393 	}
394 
395 	return cnt;
396 }
397 
release_nodes(struct device * dev,struct list_head * first,struct list_head * end,unsigned long flags)398 static int release_nodes(struct device *dev, struct list_head *first,
399 			 struct list_head *end, unsigned long flags)
400 	__releases(&dev->devres_lock)
401 {
402 	LIST_HEAD(todo);
403 	int cnt;
404 	struct devres *dr, *tmp;
405 
406 	cnt = remove_nodes(dev, first, end, &todo);
407 
408 	spin_unlock_irqrestore(&dev->devres_lock, flags);
409 
410 	/* Release.  Note that both devres and devres_group are
411 	 * handled as devres in the following loop.  This is safe.
412 	 */
413 	list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
414 		devres_log(dev, &dr->node, "REL");
415 		dr->node.release(dev, dr->data);
416 		kfree(dr);
417 	}
418 
419 	return cnt;
420 }
421 
422 /**
423  * devres_release_all - Release all managed resources
424  * @dev: Device to release resources for
425  *
426  * Release all resources associated with @dev.  This function is
427  * called on driver detach.
428  */
devres_release_all(struct device * dev)429 int devres_release_all(struct device *dev)
430 {
431 	unsigned long flags;
432 
433 	/* Looks like an uninitialized device structure */
434 	if (WARN_ON(dev->devres_head.next == NULL))
435 		return -ENODEV;
436 	spin_lock_irqsave(&dev->devres_lock, flags);
437 	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
438 			     flags);
439 }
440 
441 /**
442  * devres_open_group - Open a new devres group
443  * @dev: Device to open devres group for
444  * @id: Separator ID
445  * @gfp: Allocation flags
446  *
447  * Open a new devres group for @dev with @id.  For @id, using a
448  * pointer to an object which won't be used for another group is
449  * recommended.  If @id is NULL, address-wise unique ID is created.
450  *
451  * RETURNS:
452  * ID of the new group, NULL on failure.
453  */
devres_open_group(struct device * dev,void * id,gfp_t gfp)454 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
455 {
456 	struct devres_group *grp;
457 	unsigned long flags;
458 
459 	grp = kmalloc(sizeof(*grp), gfp);
460 	if (unlikely(!grp))
461 		return NULL;
462 
463 	grp->node[0].release = &group_open_release;
464 	grp->node[1].release = &group_close_release;
465 	INIT_LIST_HEAD(&grp->node[0].entry);
466 	INIT_LIST_HEAD(&grp->node[1].entry);
467 	set_node_dbginfo(&grp->node[0], "grp<", 0);
468 	set_node_dbginfo(&grp->node[1], "grp>", 0);
469 	grp->id = grp;
470 	if (id)
471 		grp->id = id;
472 
473 	spin_lock_irqsave(&dev->devres_lock, flags);
474 	add_dr(dev, &grp->node[0]);
475 	spin_unlock_irqrestore(&dev->devres_lock, flags);
476 	return grp->id;
477 }
478 EXPORT_SYMBOL_GPL(devres_open_group);
479 
480 /* Find devres group with ID @id.  If @id is NULL, look for the latest. */
find_group(struct device * dev,void * id)481 static struct devres_group * find_group(struct device *dev, void *id)
482 {
483 	struct devres_node *node;
484 
485 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
486 		struct devres_group *grp;
487 
488 		if (node->release != &group_open_release)
489 			continue;
490 
491 		grp = container_of(node, struct devres_group, node[0]);
492 
493 		if (id) {
494 			if (grp->id == id)
495 				return grp;
496 		} else if (list_empty(&grp->node[1].entry))
497 			return grp;
498 	}
499 
500 	return NULL;
501 }
502 
503 /**
504  * devres_close_group - Close a devres group
505  * @dev: Device to close devres group for
506  * @id: ID of target group, can be NULL
507  *
508  * Close the group identified by @id.  If @id is NULL, the latest open
509  * group is selected.
510  */
devres_close_group(struct device * dev,void * id)511 void devres_close_group(struct device *dev, void *id)
512 {
513 	struct devres_group *grp;
514 	unsigned long flags;
515 
516 	spin_lock_irqsave(&dev->devres_lock, flags);
517 
518 	grp = find_group(dev, id);
519 	if (grp)
520 		add_dr(dev, &grp->node[1]);
521 	else
522 		WARN_ON(1);
523 
524 	spin_unlock_irqrestore(&dev->devres_lock, flags);
525 }
526 EXPORT_SYMBOL_GPL(devres_close_group);
527 
528 /**
529  * devres_remove_group - Remove a devres group
530  * @dev: Device to remove group for
531  * @id: ID of target group, can be NULL
532  *
533  * Remove the group identified by @id.  If @id is NULL, the latest
534  * open group is selected.  Note that removing a group doesn't affect
535  * any other resources.
536  */
devres_remove_group(struct device * dev,void * id)537 void devres_remove_group(struct device *dev, void *id)
538 {
539 	struct devres_group *grp;
540 	unsigned long flags;
541 
542 	spin_lock_irqsave(&dev->devres_lock, flags);
543 
544 	grp = find_group(dev, id);
545 	if (grp) {
546 		list_del_init(&grp->node[0].entry);
547 		list_del_init(&grp->node[1].entry);
548 		devres_log(dev, &grp->node[0], "REM");
549 	} else
550 		WARN_ON(1);
551 
552 	spin_unlock_irqrestore(&dev->devres_lock, flags);
553 
554 	kfree(grp);
555 }
556 EXPORT_SYMBOL_GPL(devres_remove_group);
557 
558 /**
559  * devres_release_group - Release resources in a devres group
560  * @dev: Device to release group for
561  * @id: ID of target group, can be NULL
562  *
563  * Release all resources in the group identified by @id.  If @id is
564  * NULL, the latest open group is selected.  The selected group and
565  * groups properly nested inside the selected group are removed.
566  *
567  * RETURNS:
568  * The number of released non-group resources.
569  */
devres_release_group(struct device * dev,void * id)570 int devres_release_group(struct device *dev, void *id)
571 {
572 	struct devres_group *grp;
573 	unsigned long flags;
574 	int cnt = 0;
575 
576 	spin_lock_irqsave(&dev->devres_lock, flags);
577 
578 	grp = find_group(dev, id);
579 	if (grp) {
580 		struct list_head *first = &grp->node[0].entry;
581 		struct list_head *end = &dev->devres_head;
582 
583 		if (!list_empty(&grp->node[1].entry))
584 			end = grp->node[1].entry.next;
585 
586 		cnt = release_nodes(dev, first, end, flags);
587 	} else {
588 		WARN_ON(1);
589 		spin_unlock_irqrestore(&dev->devres_lock, flags);
590 	}
591 
592 	return cnt;
593 }
594 EXPORT_SYMBOL_GPL(devres_release_group);
595 
596 /*
597  * Managed kzalloc/kfree
598  */
devm_kzalloc_release(struct device * dev,void * res)599 static void devm_kzalloc_release(struct device *dev, void *res)
600 {
601 	/* noop */
602 }
603 
devm_kzalloc_match(struct device * dev,void * res,void * data)604 static int devm_kzalloc_match(struct device *dev, void *res, void *data)
605 {
606 	return res == data;
607 }
608 
609 /**
610  * devm_kzalloc - Resource-managed kzalloc
611  * @dev: Device to allocate memory for
612  * @size: Allocation size
613  * @gfp: Allocation gfp flags
614  *
615  * Managed kzalloc.  Memory allocated with this function is
616  * automatically freed on driver detach.  Like all other devres
617  * resources, guaranteed alignment is unsigned long long.
618  *
619  * RETURNS:
620  * Pointer to allocated memory on success, NULL on failure.
621  */
devm_kzalloc(struct device * dev,size_t size,gfp_t gfp)622 void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
623 {
624 	struct devres *dr;
625 
626 	/* use raw alloc_dr for kmalloc caller tracing */
627 	dr = alloc_dr(devm_kzalloc_release, size, gfp);
628 	if (unlikely(!dr))
629 		return NULL;
630 
631 	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
632 	devres_add(dev, dr->data);
633 	return dr->data;
634 }
635 EXPORT_SYMBOL_GPL(devm_kzalloc);
636 
637 /**
638  * devm_kfree - Resource-managed kfree
639  * @dev: Device this memory belongs to
640  * @p: Memory to free
641  *
642  * Free memory allocated with devm_kzalloc().
643  */
devm_kfree(struct device * dev,void * p)644 void devm_kfree(struct device *dev, void *p)
645 {
646 	int rc;
647 
648 	rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
649 	WARN_ON(rc);
650 }
651 EXPORT_SYMBOL_GPL(devm_kfree);
652