1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/export.h>
27#include <linux/radix-tree.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
30#include <linux/kmemleak.h>
31#include <linux/notifier.h>
32#include <linux/cpu.h>
33#include <linux/string.h>
34#include <linux/bitops.h>
35#include <linux/rcupdate.h>
36#include <linux/preempt.h>
37
38
39
40
41
42
43static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
44
45
46
47
48static struct kmem_cache *radix_tree_node_cachep;
49
50
51
52
53
54
55
56
57
58
59
60
61#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
62
63
64
65
66struct radix_tree_preload {
67 int nr;
68
69 struct radix_tree_node *nodes;
70};
71static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
72
73static inline void *ptr_to_indirect(void *ptr)
74{
75 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
76}
77
78static inline void *indirect_to_ptr(void *ptr)
79{
80 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
81}
82
83static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
84{
85 return root->gfp_mask & __GFP_BITS_MASK;
86}
87
88static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
89 int offset)
90{
91 __set_bit(offset, node->tags[tag]);
92}
93
94static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
95 int offset)
96{
97 __clear_bit(offset, node->tags[tag]);
98}
99
100static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
101 int offset)
102{
103 return test_bit(offset, node->tags[tag]);
104}
105
106static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
107{
108 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
109}
110
111static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
112{
113 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
114}
115
116static inline void root_tag_clear_all(struct radix_tree_root *root)
117{
118 root->gfp_mask &= __GFP_BITS_MASK;
119}
120
121static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
122{
123 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
124}
125
126
127
128
129
130static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
131{
132 int idx;
133 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
134 if (node->tags[tag][idx])
135 return 1;
136 }
137 return 0;
138}
139
140
141
142
143
144
145
146
147
148
149
150
151static __always_inline unsigned long
152radix_tree_find_next_bit(const unsigned long *addr,
153 unsigned long size, unsigned long offset)
154{
155 if (!__builtin_constant_p(size))
156 return find_next_bit(addr, size, offset);
157
158 if (offset < size) {
159 unsigned long tmp;
160
161 addr += offset / BITS_PER_LONG;
162 tmp = *addr >> (offset % BITS_PER_LONG);
163 if (tmp)
164 return __ffs(tmp) + offset;
165 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
166 while (offset < size) {
167 tmp = *++addr;
168 if (tmp)
169 return __ffs(tmp) + offset;
170 offset += BITS_PER_LONG;
171 }
172 }
173 return size;
174}
175
176#if 0
177static void dump_node(void *slot, int height, int offset)
178{
179 struct radix_tree_node *node;
180 int i;
181
182 if (!slot)
183 return;
184
185 if (height == 0) {
186 pr_debug("radix entry %p offset %d\n", slot, offset);
187 return;
188 }
189
190 node = indirect_to_ptr(slot);
191 pr_debug("radix node: %p offset %d tags %lx %lx %lx path %x count %d parent %p\n",
192 slot, offset, node->tags[0][0], node->tags[1][0],
193 node->tags[2][0], node->path, node->count, node->parent);
194
195 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
196 dump_node(node->slots[i], height - 1, i);
197}
198
199
200static void radix_tree_dump(struct radix_tree_root *root)
201{
202 pr_debug("radix root: %p height %d rnode %p tags %x\n",
203 root, root->height, root->rnode,
204 root->gfp_mask >> __GFP_BITS_SHIFT);
205 if (!radix_tree_is_indirect_ptr(root->rnode))
206 return;
207 dump_node(root->rnode, root->height, 0);
208}
209#endif
210
211
212
213
214
215static struct radix_tree_node *
216radix_tree_node_alloc(struct radix_tree_root *root)
217{
218 struct radix_tree_node *ret = NULL;
219 gfp_t gfp_mask = root_gfp_mask(root);
220
221
222
223
224
225
226 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
227 struct radix_tree_preload *rtp;
228
229
230
231
232
233 ret = kmem_cache_alloc(radix_tree_node_cachep,
234 gfp_mask | __GFP_ACCOUNT | __GFP_NOWARN);
235 if (ret)
236 goto out;
237
238
239
240
241
242
243 rtp = this_cpu_ptr(&radix_tree_preloads);
244 if (rtp->nr) {
245 ret = rtp->nodes;
246 rtp->nodes = ret->private_data;
247 ret->private_data = NULL;
248 rtp->nr--;
249 }
250
251
252
253
254 kmemleak_update_trace(ret);
255 goto out;
256 }
257 ret = kmem_cache_alloc(radix_tree_node_cachep,
258 gfp_mask | __GFP_ACCOUNT);
259out:
260 BUG_ON(radix_tree_is_indirect_ptr(ret));
261 return ret;
262}
263
264static void radix_tree_node_rcu_free(struct rcu_head *head)
265{
266 struct radix_tree_node *node =
267 container_of(head, struct radix_tree_node, rcu_head);
268 int i;
269
270
271
272
273
274
275 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
276 tag_clear(node, i, 0);
277
278 node->slots[0] = NULL;
279 node->count = 0;
280
281 kmem_cache_free(radix_tree_node_cachep, node);
282}
283
284static inline void
285radix_tree_node_free(struct radix_tree_node *node)
286{
287 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
288}
289
290
291
292
293
294
295
296
297
298
299static int __radix_tree_preload(gfp_t gfp_mask)
300{
301 struct radix_tree_preload *rtp;
302 struct radix_tree_node *node;
303 int ret = -ENOMEM;
304
305 preempt_disable();
306 rtp = this_cpu_ptr(&radix_tree_preloads);
307 while (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
308 preempt_enable();
309 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
310 if (node == NULL)
311 goto out;
312 preempt_disable();
313 rtp = this_cpu_ptr(&radix_tree_preloads);
314 if (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
315 node->private_data = rtp->nodes;
316 rtp->nodes = node;
317 rtp->nr++;
318 } else {
319 kmem_cache_free(radix_tree_node_cachep, node);
320 }
321 }
322 ret = 0;
323out:
324 return ret;
325}
326
327
328
329
330
331
332
333
334
335
336int radix_tree_preload(gfp_t gfp_mask)
337{
338
339 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
340 return __radix_tree_preload(gfp_mask);
341}
342EXPORT_SYMBOL(radix_tree_preload);
343
344
345
346
347
348
349int radix_tree_maybe_preload(gfp_t gfp_mask)
350{
351 if (gfpflags_allow_blocking(gfp_mask))
352 return __radix_tree_preload(gfp_mask);
353
354 preempt_disable();
355 return 0;
356}
357EXPORT_SYMBOL(radix_tree_maybe_preload);
358
359
360
361
362
363static inline unsigned long radix_tree_maxindex(unsigned int height)
364{
365 return height_to_maxindex[height];
366}
367
368
369
370
371static int radix_tree_extend(struct radix_tree_root *root,
372 unsigned long index, unsigned order)
373{
374 struct radix_tree_node *node;
375 struct radix_tree_node *slot;
376 unsigned int height;
377 int tag;
378
379
380 height = root->height + 1;
381 while (index > radix_tree_maxindex(height))
382 height++;
383
384 if ((root->rnode == NULL) && (order == 0)) {
385 root->height = height;
386 goto out;
387 }
388
389 do {
390 unsigned int newheight;
391 if (!(node = radix_tree_node_alloc(root)))
392 return -ENOMEM;
393
394
395 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
396 if (root_tag_get(root, tag))
397 tag_set(node, tag, 0);
398 }
399
400
401 newheight = root->height+1;
402 BUG_ON(newheight & ~RADIX_TREE_HEIGHT_MASK);
403 node->path = newheight;
404 node->count = 1;
405 node->parent = NULL;
406 slot = root->rnode;
407 if (radix_tree_is_indirect_ptr(slot) && newheight > 1) {
408 slot = indirect_to_ptr(slot);
409 slot->parent = node;
410 slot = ptr_to_indirect(slot);
411 }
412 node->slots[0] = slot;
413 node = ptr_to_indirect(node);
414 rcu_assign_pointer(root->rnode, node);
415 root->height = newheight;
416 } while (height > root->height);
417out:
418 return 0;
419}
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
439 unsigned order, struct radix_tree_node **nodep,
440 void ***slotp)
441{
442 struct radix_tree_node *node = NULL, *slot;
443 unsigned int height, shift, offset;
444 int error;
445
446 BUG_ON((0 < order) && (order < RADIX_TREE_MAP_SHIFT));
447
448
449 if (index > radix_tree_maxindex(root->height)) {
450 error = radix_tree_extend(root, index, order);
451 if (error)
452 return error;
453 }
454
455 slot = root->rnode;
456
457 height = root->height;
458 shift = height * RADIX_TREE_MAP_SHIFT;
459
460 offset = 0;
461 while (shift > order) {
462 if (slot == NULL) {
463
464 if (!(slot = radix_tree_node_alloc(root)))
465 return -ENOMEM;
466 slot->path = height;
467 slot->parent = node;
468 if (node) {
469 rcu_assign_pointer(node->slots[offset],
470 ptr_to_indirect(slot));
471 node->count++;
472 slot->path |= offset << RADIX_TREE_HEIGHT_SHIFT;
473 } else
474 rcu_assign_pointer(root->rnode,
475 ptr_to_indirect(slot));
476 } else if (!radix_tree_is_indirect_ptr(slot))
477 break;
478
479
480 height--;
481 shift -= RADIX_TREE_MAP_SHIFT;
482 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
483 node = indirect_to_ptr(slot);
484 slot = node->slots[offset];
485 }
486
487
488 if ((shift - order) > 0) {
489 int i, n = 1 << (shift - order);
490 offset = offset & ~(n - 1);
491 slot = ptr_to_indirect(&node->slots[offset]);
492 for (i = 0; i < n; i++) {
493 if (node->slots[offset + i])
494 return -EEXIST;
495 }
496
497 for (i = 1; i < n; i++) {
498 rcu_assign_pointer(node->slots[offset + i], slot);
499 node->count++;
500 }
501 }
502
503 if (nodep)
504 *nodep = node;
505 if (slotp)
506 *slotp = node ? node->slots + offset : (void **)&root->rnode;
507 return 0;
508}
509
510
511
512
513
514
515
516
517
518
519int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
520 unsigned order, void *item)
521{
522 struct radix_tree_node *node;
523 void **slot;
524 int error;
525
526 BUG_ON(radix_tree_is_indirect_ptr(item));
527
528 error = __radix_tree_create(root, index, order, &node, &slot);
529 if (error)
530 return error;
531 if (*slot != NULL)
532 return -EEXIST;
533 rcu_assign_pointer(*slot, item);
534
535 if (node) {
536 node->count++;
537 BUG_ON(tag_get(node, 0, index & RADIX_TREE_MAP_MASK));
538 BUG_ON(tag_get(node, 1, index & RADIX_TREE_MAP_MASK));
539 } else {
540 BUG_ON(root_tag_get(root, 0));
541 BUG_ON(root_tag_get(root, 1));
542 }
543
544 return 0;
545}
546EXPORT_SYMBOL(__radix_tree_insert);
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
563 struct radix_tree_node **nodep, void ***slotp)
564{
565 struct radix_tree_node *node, *parent;
566 unsigned int height, shift;
567 void **slot;
568
569 node = rcu_dereference_raw(root->rnode);
570 if (node == NULL)
571 return NULL;
572
573 if (!radix_tree_is_indirect_ptr(node)) {
574 if (index > 0)
575 return NULL;
576
577 if (nodep)
578 *nodep = NULL;
579 if (slotp)
580 *slotp = (void **)&root->rnode;
581 return node;
582 }
583 node = indirect_to_ptr(node);
584
585 height = node->path & RADIX_TREE_HEIGHT_MASK;
586 if (index > radix_tree_maxindex(height))
587 return NULL;
588
589 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
590
591 do {
592 parent = node;
593 slot = node->slots + ((index >> shift) & RADIX_TREE_MAP_MASK);
594 node = rcu_dereference_raw(*slot);
595 if (node == NULL)
596 return NULL;
597 if (!radix_tree_is_indirect_ptr(node))
598 break;
599 node = indirect_to_ptr(node);
600
601 shift -= RADIX_TREE_MAP_SHIFT;
602 height--;
603 } while (height > 0);
604
605 if (nodep)
606 *nodep = parent;
607 if (slotp)
608 *slotp = slot;
609 return node;
610}
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
626{
627 void **slot;
628
629 if (!__radix_tree_lookup(root, index, NULL, &slot))
630 return NULL;
631 return slot;
632}
633EXPORT_SYMBOL(radix_tree_lookup_slot);
634
635
636
637
638
639
640
641
642
643
644
645
646
647void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
648{
649 return __radix_tree_lookup(root, index, NULL, NULL);
650}
651EXPORT_SYMBOL(radix_tree_lookup);
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666void *radix_tree_tag_set(struct radix_tree_root *root,
667 unsigned long index, unsigned int tag)
668{
669 unsigned int height, shift;
670 struct radix_tree_node *slot;
671
672 height = root->height;
673 BUG_ON(index > radix_tree_maxindex(height));
674
675 slot = indirect_to_ptr(root->rnode);
676 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
677
678 while (height > 0) {
679 int offset;
680
681 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
682 if (!tag_get(slot, tag, offset))
683 tag_set(slot, tag, offset);
684 slot = slot->slots[offset];
685 BUG_ON(slot == NULL);
686 if (!radix_tree_is_indirect_ptr(slot))
687 break;
688 slot = indirect_to_ptr(slot);
689 shift -= RADIX_TREE_MAP_SHIFT;
690 height--;
691 }
692
693
694 if (slot && !root_tag_get(root, tag))
695 root_tag_set(root, tag);
696
697 return slot;
698}
699EXPORT_SYMBOL(radix_tree_tag_set);
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715void *radix_tree_tag_clear(struct radix_tree_root *root,
716 unsigned long index, unsigned int tag)
717{
718 struct radix_tree_node *node = NULL;
719 struct radix_tree_node *slot = NULL;
720 unsigned int height, shift;
721 int uninitialized_var(offset);
722
723 height = root->height;
724 if (index > radix_tree_maxindex(height))
725 goto out;
726
727 shift = height * RADIX_TREE_MAP_SHIFT;
728 slot = root->rnode;
729
730 while (shift) {
731 if (slot == NULL)
732 goto out;
733 if (!radix_tree_is_indirect_ptr(slot))
734 break;
735 slot = indirect_to_ptr(slot);
736
737 shift -= RADIX_TREE_MAP_SHIFT;
738 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
739 node = slot;
740 slot = slot->slots[offset];
741 }
742
743 if (slot == NULL)
744 goto out;
745
746 while (node) {
747 if (!tag_get(node, tag, offset))
748 goto out;
749 tag_clear(node, tag, offset);
750 if (any_tag_set(node, tag))
751 goto out;
752
753 index >>= RADIX_TREE_MAP_SHIFT;
754 offset = index & RADIX_TREE_MAP_MASK;
755 node = node->parent;
756 }
757
758
759 if (root_tag_get(root, tag))
760 root_tag_clear(root, tag);
761
762out:
763 return slot;
764}
765EXPORT_SYMBOL(radix_tree_tag_clear);
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782int radix_tree_tag_get(struct radix_tree_root *root,
783 unsigned long index, unsigned int tag)
784{
785 unsigned int height, shift;
786 struct radix_tree_node *node;
787
788
789 if (!root_tag_get(root, tag))
790 return 0;
791
792 node = rcu_dereference_raw(root->rnode);
793 if (node == NULL)
794 return 0;
795
796 if (!radix_tree_is_indirect_ptr(node))
797 return (index == 0);
798 node = indirect_to_ptr(node);
799
800 height = node->path & RADIX_TREE_HEIGHT_MASK;
801 if (index > radix_tree_maxindex(height))
802 return 0;
803
804 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
805
806 for ( ; ; ) {
807 int offset;
808
809 if (node == NULL)
810 return 0;
811 node = indirect_to_ptr(node);
812
813 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
814 if (!tag_get(node, tag, offset))
815 return 0;
816 if (height == 1)
817 return 1;
818 node = rcu_dereference_raw(node->slots[offset]);
819 if (!radix_tree_is_indirect_ptr(node))
820 return 1;
821 shift -= RADIX_TREE_MAP_SHIFT;
822 height--;
823 }
824}
825EXPORT_SYMBOL(radix_tree_tag_get);
826
827
828
829
830
831
832
833
834
835void **radix_tree_next_chunk(struct radix_tree_root *root,
836 struct radix_tree_iter *iter, unsigned flags)
837{
838 unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK;
839 struct radix_tree_node *rnode, *node;
840 unsigned long index, offset, height;
841
842 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
843 return NULL;
844
845
846
847
848
849
850
851
852
853
854 index = iter->next_index;
855 if (!index && iter->index)
856 return NULL;
857
858 rnode = rcu_dereference_raw(root->rnode);
859 if (radix_tree_is_indirect_ptr(rnode)) {
860 rnode = indirect_to_ptr(rnode);
861 } else if (rnode && !index) {
862
863 iter->index = 0;
864 iter->next_index = 1;
865 iter->tags = 1;
866 return (void **)&root->rnode;
867 } else
868 return NULL;
869
870restart:
871 height = rnode->path & RADIX_TREE_HEIGHT_MASK;
872 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
873 offset = index >> shift;
874
875
876 if (offset >= RADIX_TREE_MAP_SIZE)
877 return NULL;
878
879 node = rnode;
880 while (1) {
881 struct radix_tree_node *slot;
882 if ((flags & RADIX_TREE_ITER_TAGGED) ?
883 !test_bit(offset, node->tags[tag]) :
884 !node->slots[offset]) {
885
886 if (flags & RADIX_TREE_ITER_CONTIG)
887 return NULL;
888
889 if (flags & RADIX_TREE_ITER_TAGGED)
890 offset = radix_tree_find_next_bit(
891 node->tags[tag],
892 RADIX_TREE_MAP_SIZE,
893 offset + 1);
894 else
895 while (++offset < RADIX_TREE_MAP_SIZE) {
896 if (node->slots[offset])
897 break;
898 }
899 index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
900 index += offset << shift;
901
902 if (!index)
903 return NULL;
904 if (offset == RADIX_TREE_MAP_SIZE)
905 goto restart;
906 }
907
908
909 if (!shift)
910 break;
911
912 slot = rcu_dereference_raw(node->slots[offset]);
913 if (slot == NULL)
914 goto restart;
915 if (!radix_tree_is_indirect_ptr(slot))
916 break;
917 node = indirect_to_ptr(slot);
918 shift -= RADIX_TREE_MAP_SHIFT;
919 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
920 }
921
922
923 iter->index = index;
924 iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1;
925
926
927 if (flags & RADIX_TREE_ITER_TAGGED) {
928 unsigned tag_long, tag_bit;
929
930 tag_long = offset / BITS_PER_LONG;
931 tag_bit = offset % BITS_PER_LONG;
932 iter->tags = node->tags[tag][tag_long] >> tag_bit;
933
934 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
935
936 if (tag_bit)
937 iter->tags |= node->tags[tag][tag_long + 1] <<
938 (BITS_PER_LONG - tag_bit);
939
940 iter->next_index = index + BITS_PER_LONG;
941 }
942 }
943
944 return node->slots + offset;
945}
946EXPORT_SYMBOL(radix_tree_next_chunk);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
976 unsigned long *first_indexp, unsigned long last_index,
977 unsigned long nr_to_tag,
978 unsigned int iftag, unsigned int settag)
979{
980 unsigned int height = root->height;
981 struct radix_tree_node *node = NULL;
982 struct radix_tree_node *slot;
983 unsigned int shift;
984 unsigned long tagged = 0;
985 unsigned long index = *first_indexp;
986
987 last_index = min(last_index, radix_tree_maxindex(height));
988 if (index > last_index)
989 return 0;
990 if (!nr_to_tag)
991 return 0;
992 if (!root_tag_get(root, iftag)) {
993 *first_indexp = last_index + 1;
994 return 0;
995 }
996 if (height == 0) {
997 *first_indexp = last_index + 1;
998 root_tag_set(root, settag);
999 return 1;
1000 }
1001
1002 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
1003 slot = indirect_to_ptr(root->rnode);
1004
1005 for (;;) {
1006 unsigned long upindex;
1007 int offset;
1008
1009 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1010 if (!slot->slots[offset])
1011 goto next;
1012 if (!tag_get(slot, iftag, offset))
1013 goto next;
1014 if (shift) {
1015 node = slot;
1016 slot = slot->slots[offset];
1017 if (radix_tree_is_indirect_ptr(slot)) {
1018 slot = indirect_to_ptr(slot);
1019 shift -= RADIX_TREE_MAP_SHIFT;
1020 continue;
1021 } else {
1022 slot = node;
1023 node = node->parent;
1024 }
1025 }
1026
1027
1028 tagged += 1 << shift;
1029 tag_set(slot, settag, offset);
1030
1031
1032 upindex = index;
1033 while (node) {
1034 upindex >>= RADIX_TREE_MAP_SHIFT;
1035 offset = upindex & RADIX_TREE_MAP_MASK;
1036
1037
1038 if (tag_get(node, settag, offset))
1039 break;
1040 tag_set(node, settag, offset);
1041 node = node->parent;
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051 node = NULL;
1052
1053next:
1054
1055 index = ((index >> shift) + 1) << shift;
1056
1057 if (index > last_index || !index)
1058 break;
1059 if (tagged >= nr_to_tag)
1060 break;
1061 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
1062
1063
1064
1065
1066
1067 slot = slot->parent;
1068 shift += RADIX_TREE_MAP_SHIFT;
1069 }
1070 }
1071
1072
1073
1074
1075 if (tagged > 0)
1076 root_tag_set(root, settag);
1077 *first_indexp = index;
1078
1079 return tagged;
1080}
1081EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102unsigned int
1103radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1104 unsigned long first_index, unsigned int max_items)
1105{
1106 struct radix_tree_iter iter;
1107 void **slot;
1108 unsigned int ret = 0;
1109
1110 if (unlikely(!max_items))
1111 return 0;
1112
1113 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1114 results[ret] = rcu_dereference_raw(*slot);
1115 if (!results[ret])
1116 continue;
1117 if (radix_tree_is_indirect_ptr(results[ret])) {
1118 slot = radix_tree_iter_retry(&iter);
1119 continue;
1120 }
1121 if (++ret == max_items)
1122 break;
1123 }
1124
1125 return ret;
1126}
1127EXPORT_SYMBOL(radix_tree_gang_lookup);
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147unsigned int
1148radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1149 void ***results, unsigned long *indices,
1150 unsigned long first_index, unsigned int max_items)
1151{
1152 struct radix_tree_iter iter;
1153 void **slot;
1154 unsigned int ret = 0;
1155
1156 if (unlikely(!max_items))
1157 return 0;
1158
1159 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1160 results[ret] = slot;
1161 if (indices)
1162 indices[ret] = iter.index;
1163 if (++ret == max_items)
1164 break;
1165 }
1166
1167 return ret;
1168}
1169EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184unsigned int
1185radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
1186 unsigned long first_index, unsigned int max_items,
1187 unsigned int tag)
1188{
1189 struct radix_tree_iter iter;
1190 void **slot;
1191 unsigned int ret = 0;
1192
1193 if (unlikely(!max_items))
1194 return 0;
1195
1196 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1197 results[ret] = rcu_dereference_raw(*slot);
1198 if (!results[ret])
1199 continue;
1200 if (radix_tree_is_indirect_ptr(results[ret])) {
1201 slot = radix_tree_iter_retry(&iter);
1202 continue;
1203 }
1204 if (++ret == max_items)
1205 break;
1206 }
1207
1208 return ret;
1209}
1210EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225unsigned int
1226radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1227 unsigned long first_index, unsigned int max_items,
1228 unsigned int tag)
1229{
1230 struct radix_tree_iter iter;
1231 void **slot;
1232 unsigned int ret = 0;
1233
1234 if (unlikely(!max_items))
1235 return 0;
1236
1237 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1238 results[ret] = slot;
1239 if (++ret == max_items)
1240 break;
1241 }
1242
1243 return ret;
1244}
1245EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1246
1247#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1248#include <linux/sched.h>
1249
1250
1251
1252
1253static unsigned long __locate(struct radix_tree_node *slot, void *item,
1254 unsigned long index, unsigned long *found_index)
1255{
1256 unsigned int shift, height;
1257 unsigned long i;
1258
1259 height = slot->path & RADIX_TREE_HEIGHT_MASK;
1260 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
1261
1262 for ( ; height > 1; height--) {
1263 i = (index >> shift) & RADIX_TREE_MAP_MASK;
1264 for (;;) {
1265 if (slot->slots[i] != NULL)
1266 break;
1267 index &= ~((1UL << shift) - 1);
1268 index += 1UL << shift;
1269 if (index == 0)
1270 goto out;
1271 i++;
1272 if (i == RADIX_TREE_MAP_SIZE)
1273 goto out;
1274 }
1275
1276 slot = rcu_dereference_raw(slot->slots[i]);
1277 if (slot == NULL)
1278 goto out;
1279 if (!radix_tree_is_indirect_ptr(slot)) {
1280 if (slot == item) {
1281 *found_index = index + i;
1282 index = 0;
1283 } else {
1284 index += shift;
1285 }
1286 goto out;
1287 }
1288 slot = indirect_to_ptr(slot);
1289 shift -= RADIX_TREE_MAP_SHIFT;
1290 }
1291
1292
1293 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
1294 if (slot->slots[i] == item) {
1295 *found_index = index + i;
1296 index = 0;
1297 goto out;
1298 }
1299 }
1300 index += RADIX_TREE_MAP_SIZE;
1301out:
1302 return index;
1303}
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1315{
1316 struct radix_tree_node *node;
1317 unsigned long max_index;
1318 unsigned long cur_index = 0;
1319 unsigned long found_index = -1;
1320
1321 do {
1322 rcu_read_lock();
1323 node = rcu_dereference_raw(root->rnode);
1324 if (!radix_tree_is_indirect_ptr(node)) {
1325 rcu_read_unlock();
1326 if (node == item)
1327 found_index = 0;
1328 break;
1329 }
1330
1331 node = indirect_to_ptr(node);
1332 max_index = radix_tree_maxindex(node->path &
1333 RADIX_TREE_HEIGHT_MASK);
1334 if (cur_index > max_index) {
1335 rcu_read_unlock();
1336 break;
1337 }
1338
1339 cur_index = __locate(node, item, cur_index, &found_index);
1340 rcu_read_unlock();
1341 cond_resched();
1342 } while (cur_index != 0 && cur_index <= max_index);
1343
1344 return found_index;
1345}
1346#else
1347unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1348{
1349 return -1;
1350}
1351#endif
1352
1353
1354
1355
1356
1357static inline void radix_tree_shrink(struct radix_tree_root *root)
1358{
1359
1360 while (root->height > 0) {
1361 struct radix_tree_node *to_free = root->rnode;
1362 struct radix_tree_node *slot;
1363
1364 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
1365 to_free = indirect_to_ptr(to_free);
1366
1367
1368
1369
1370
1371
1372 if (to_free->count != 1)
1373 break;
1374 slot = to_free->slots[0];
1375 if (!slot)
1376 break;
1377
1378
1379
1380
1381
1382
1383
1384
1385 if (root->height > 1) {
1386 if (!radix_tree_is_indirect_ptr(slot))
1387 break;
1388
1389 slot = indirect_to_ptr(slot);
1390 slot->parent = NULL;
1391 slot = ptr_to_indirect(slot);
1392 }
1393 root->rnode = slot;
1394 root->height--;
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414 if (root->height == 0)
1415 *((unsigned long *)&to_free->slots[0]) |=
1416 RADIX_TREE_INDIRECT_PTR;
1417
1418 radix_tree_node_free(to_free);
1419 }
1420}
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433bool __radix_tree_delete_node(struct radix_tree_root *root,
1434 struct radix_tree_node *node)
1435{
1436 bool deleted = false;
1437
1438 do {
1439 struct radix_tree_node *parent;
1440
1441 if (node->count) {
1442 if (node == indirect_to_ptr(root->rnode)) {
1443 radix_tree_shrink(root);
1444 if (root->height == 0)
1445 deleted = true;
1446 }
1447 return deleted;
1448 }
1449
1450 parent = node->parent;
1451 if (parent) {
1452 unsigned int offset;
1453
1454 offset = node->path >> RADIX_TREE_HEIGHT_SHIFT;
1455 parent->slots[offset] = NULL;
1456 parent->count--;
1457 } else {
1458 root_tag_clear_all(root);
1459 root->height = 0;
1460 root->rnode = NULL;
1461 }
1462
1463 radix_tree_node_free(node);
1464 deleted = true;
1465
1466 node = parent;
1467 } while (node);
1468
1469 return deleted;
1470}
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483void *radix_tree_delete_item(struct radix_tree_root *root,
1484 unsigned long index, void *item)
1485{
1486 struct radix_tree_node *node;
1487 unsigned int offset, i;
1488 void **slot;
1489 void *entry;
1490 int tag;
1491
1492 entry = __radix_tree_lookup(root, index, &node, &slot);
1493 if (!entry)
1494 return NULL;
1495
1496 if (item && entry != item)
1497 return NULL;
1498
1499 if (!node) {
1500 root_tag_clear_all(root);
1501 root->rnode = NULL;
1502 return entry;
1503 }
1504
1505 offset = index & RADIX_TREE_MAP_MASK;
1506
1507
1508
1509
1510
1511 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
1512 if (tag_get(node, tag, offset))
1513 radix_tree_tag_clear(root, index, tag);
1514 }
1515
1516
1517 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1518 if (node->slots[offset + i] != ptr_to_indirect(slot))
1519 break;
1520 node->slots[offset + i] = NULL;
1521 node->count--;
1522 }
1523 node->slots[offset] = NULL;
1524 node->count--;
1525
1526 __radix_tree_delete_node(root, node);
1527
1528 return entry;
1529}
1530EXPORT_SYMBOL(radix_tree_delete_item);
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1542{
1543 return radix_tree_delete_item(root, index, NULL);
1544}
1545EXPORT_SYMBOL(radix_tree_delete);
1546
1547
1548
1549
1550
1551
1552int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
1553{
1554 return root_tag_get(root, tag);
1555}
1556EXPORT_SYMBOL(radix_tree_tagged);
1557
1558static void
1559radix_tree_node_ctor(void *arg)
1560{
1561 struct radix_tree_node *node = arg;
1562
1563 memset(node, 0, sizeof(*node));
1564 INIT_LIST_HEAD(&node->private_list);
1565}
1566
1567static __init unsigned long __maxindex(unsigned int height)
1568{
1569 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1570 int shift = RADIX_TREE_INDEX_BITS - width;
1571
1572 if (shift < 0)
1573 return ~0UL;
1574 if (shift >= BITS_PER_LONG)
1575 return 0UL;
1576 return ~0UL >> shift;
1577}
1578
1579static __init void radix_tree_init_maxindex(void)
1580{
1581 unsigned int i;
1582
1583 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1584 height_to_maxindex[i] = __maxindex(i);
1585}
1586
1587static int radix_tree_callback(struct notifier_block *nfb,
1588 unsigned long action,
1589 void *hcpu)
1590{
1591 int cpu = (long)hcpu;
1592 struct radix_tree_preload *rtp;
1593 struct radix_tree_node *node;
1594
1595
1596 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1597 rtp = &per_cpu(radix_tree_preloads, cpu);
1598 while (rtp->nr) {
1599 node = rtp->nodes;
1600 rtp->nodes = node->private_data;
1601 kmem_cache_free(radix_tree_node_cachep, node);
1602 rtp->nr--;
1603 }
1604 }
1605 return NOTIFY_OK;
1606}
1607
1608void __init radix_tree_init(void)
1609{
1610 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1611 sizeof(struct radix_tree_node), 0,
1612 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1613 radix_tree_node_ctor);
1614 radix_tree_init_maxindex();
1615 hotcpu_notifier(radix_tree_callback, 0);
1616}
1617