1
2
3
4
5
6
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
16struct devres_node {
17 struct list_head entry;
18 dr_release_t release;
19#ifdef CONFIG_DEBUG_DEVRES
20 const char *name;
21 size_t size;
22#endif
23};
24
25struct devres {
26 struct devres_node node;
27
28 unsigned long long data[];
29};
30
31struct devres_group {
32 struct devres_node node[2];
33 void *id;
34 int color;
35
36};
37
38#ifdef CONFIG_DEBUG_DEVRES
39static int log_devres = 0;
40module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
41
42static void set_node_dbginfo(struct devres_node *node, const char *name,
43 size_t size)
44{
45 node->name = name;
46 node->size = size;
47}
48
49static void devres_log(struct device *dev, struct devres_node *node,
50 const char *op)
51{
52 if (unlikely(log_devres))
53 dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
54 op, node, node->name, (unsigned long)node->size);
55}
56#else
57#define set_node_dbginfo(node, n, s) do {} while (0)
58#define devres_log(dev, node, op) do {} while (0)
59#endif
60
61
62
63
64
65static void group_open_release(struct device *dev, void *res)
66{
67
68}
69
70static void group_close_release(struct device *dev, void *res)
71{
72
73}
74
75static struct devres_group * node_to_group(struct devres_node *node)
76{
77 if (node->release == &group_open_release)
78 return container_of(node, struct devres_group, node[0]);
79 if (node->release == &group_close_release)
80 return container_of(node, struct devres_group, node[1]);
81 return NULL;
82}
83
84static __always_inline struct devres * alloc_dr(dr_release_t release,
85 size_t size, gfp_t gfp, int nid)
86{
87 size_t tot_size;
88 struct devres *dr;
89
90
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
106static 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
114void * __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}
125EXPORT_SYMBOL_GPL(__devres_alloc_node);
126#else
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141void * 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}
150EXPORT_SYMBOL_GPL(devres_alloc_node);
151#endif
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168void 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}
193EXPORT_SYMBOL_GPL(devres_for_each_res);
194
195
196
197
198
199
200
201void 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}
210EXPORT_SYMBOL_GPL(devres_free);
211
212
213
214
215
216
217
218
219
220
221void 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}
230EXPORT_SYMBOL_GPL(devres_add);
231
232static 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
252
253
254
255
256
257
258
259
260
261
262
263
264void * 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}
278EXPORT_SYMBOL_GPL(devres_find);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294void * 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}
313EXPORT_SYMBOL_GPL(devres_get);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330void * 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}
348EXPORT_SYMBOL_GPL(devres_remove);
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368int 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}
380EXPORT_SYMBOL_GPL(devres_destroy);
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398int 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}
411EXPORT_SYMBOL_GPL(devres_release);
412
413static 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
421
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
434 grp->color = 0;
435 nr_groups++;
436 } else {
437
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
449
450
451
452
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
472
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
482static 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
495
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
508
509
510
511
512
513int devres_release_all(struct device *dev)
514{
515 unsigned long flags;
516
517
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
527
528
529
530
531
532
533
534
535
536
537
538void * 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}
562EXPORT_SYMBOL_GPL(devres_open_group);
563
564
565static 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
589
590
591
592
593
594
595void 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}
610EXPORT_SYMBOL_GPL(devres_close_group);
611
612
613
614
615
616
617
618
619
620
621void 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}
640EXPORT_SYMBOL_GPL(devres_remove_group);
641
642
643
644
645
646
647
648
649
650
651
652
653
654int 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}
678EXPORT_SYMBOL_GPL(devres_release_group);
679
680
681
682
683
684
685struct action_devres {
686 void *data;
687 void (*action)(void *);
688};
689
690static 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
699static 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
708
709
710
711
712
713
714
715int 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}
730EXPORT_SYMBOL_GPL(devm_add_action);
731
732
733
734
735
736
737
738
739
740
741void 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}
752EXPORT_SYMBOL_GPL(devm_remove_action);
753
754
755
756
757static void devm_kmalloc_release(struct device *dev, void *res)
758{
759
760}
761
762static int devm_kmalloc_match(struct device *dev, void *res, void *data)
763{
764 return res == data;
765}
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
781{
782 struct devres *dr;
783
784
785 dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
786 if (unlikely(!dr))
787 return NULL;
788
789
790
791
792
793 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
794 devres_add(dev, dr->data);
795 return dr->data;
796}
797EXPORT_SYMBOL_GPL(devm_kmalloc);
798
799
800
801
802
803
804
805
806
807
808
809char *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}
823EXPORT_SYMBOL_GPL(devm_kstrdup);
824
825
826
827
828
829
830
831
832
833
834
835
836char *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}
855EXPORT_SYMBOL(devm_kvasprintf);
856
857
858
859
860
861
862
863
864
865
866
867
868char *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}
879EXPORT_SYMBOL_GPL(devm_kasprintf);
880
881
882
883
884
885
886
887
888void 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}
895EXPORT_SYMBOL_GPL(devm_kfree);
896
897
898
899
900
901
902
903
904
905
906void *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}
916EXPORT_SYMBOL_GPL(devm_kmemdup);
917
918struct pages_devres {
919 unsigned long addr;
920 unsigned int order;
921};
922
923static 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
931static 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
940
941
942
943
944
945
946
947
948
949
950
951unsigned 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}
975EXPORT_SYMBOL_GPL(devm_get_free_pages);
976
977
978
979
980
981
982
983
984
985void 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}
992EXPORT_SYMBOL_GPL(devm_free_pages);
993
994static 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
1002static 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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021void __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}
1043EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1044
1045
1046
1047
1048
1049
1050
1051
1052void 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}
1057EXPORT_SYMBOL_GPL(devm_free_percpu);
1058