1 /*
2  * multiorder.c: Multi-order radix tree entry testing
3  * Copyright (c) 2016 Intel Corporation
4  * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
5  * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  */
16 #include <linux/radix-tree.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 
20 #include "test.h"
21 
22 static void multiorder_check(unsigned long index, int order)
23 {
24 	unsigned long i;
25 	unsigned long min = index & ~((1UL << order) - 1);
26 	unsigned long max = min + (1UL << order);
27 	RADIX_TREE(tree, GFP_KERNEL);
28 
29 	printf("Multiorder index %ld, order %d\n", index, order);
30 
31 	assert(item_insert_order(&tree, index, order) == 0);
32 
33 	for (i = min; i < max; i++) {
34 		struct item *item = item_lookup(&tree, i);
35 		assert(item != 0);
36 		assert(item->index == index);
37 	}
38 	for (i = 0; i < min; i++)
39 		item_check_absent(&tree, i);
40 	for (i = max; i < 2*max; i++)
41 		item_check_absent(&tree, i);
42 
43 	assert(item_delete(&tree, index) != 0);
44 
45 	for (i = 0; i < 2*max; i++)
46 		item_check_absent(&tree, i);
47 }
48 
49 static void multiorder_shrink(unsigned long index, int order)
50 {
51 	unsigned long i;
52 	unsigned long max = 1 << order;
53 	RADIX_TREE(tree, GFP_KERNEL);
54 	struct radix_tree_node *node;
55 
56 	printf("Multiorder shrink index %ld, order %d\n", index, order);
57 
58 	assert(item_insert_order(&tree, 0, order) == 0);
59 
60 	node = tree.rnode;
61 
62 	assert(item_insert(&tree, index) == 0);
63 	assert(node != tree.rnode);
64 
65 	assert(item_delete(&tree, index) != 0);
66 	assert(node == tree.rnode);
67 
68 	for (i = 0; i < max; i++) {
69 		struct item *item = item_lookup(&tree, i);
70 		assert(item != 0);
71 		assert(item->index == 0);
72 	}
73 	for (i = max; i < 2*max; i++)
74 		item_check_absent(&tree, i);
75 
76 	if (!item_delete(&tree, 0)) {
77 		printf("failed to delete index %ld (order %d)\n", index, order);		abort();
78 	}
79 
80 	for (i = 0; i < 2*max; i++)
81 		item_check_absent(&tree, i);
82 }
83 
84 static void multiorder_insert_bug(void)
85 {
86 	RADIX_TREE(tree, GFP_KERNEL);
87 
88 	item_insert(&tree, 0);
89 	radix_tree_tag_set(&tree, 0, 0);
90 	item_insert_order(&tree, 3 << 6, 6);
91 
92 	item_kill_tree(&tree);
93 }
94 
95 void multiorder_iteration(void)
96 {
97 	RADIX_TREE(tree, GFP_KERNEL);
98 	struct radix_tree_iter iter;
99 	void **slot;
100 	int i, err;
101 
102 	printf("Multiorder iteration test\n");
103 
104 #define NUM_ENTRIES 11
105 	int index[NUM_ENTRIES] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128};
106 	int order[NUM_ENTRIES] = {1, 1, 2, 3,  4,  1,  0,  1,  3,  0, 7};
107 
108 	for (i = 0; i < NUM_ENTRIES; i++) {
109 		err = item_insert_order(&tree, index[i], order[i]);
110 		assert(!err);
111 	}
112 
113 	i = 0;
114 	/* start from index 1 to verify we find the multi-order entry at 0 */
115 	radix_tree_for_each_slot(slot, &tree, &iter, 1) {
116 		int height = order[i] / RADIX_TREE_MAP_SHIFT;
117 		int shift = height * RADIX_TREE_MAP_SHIFT;
118 
119 		assert(iter.index == index[i]);
120 		assert(iter.shift == shift);
121 		i++;
122 	}
123 
124 	/*
125 	 * Now iterate through the tree starting at an elevated multi-order
126 	 * entry, beginning at an index in the middle of the range.
127 	 */
128 	i = 8;
129 	radix_tree_for_each_slot(slot, &tree, &iter, 70) {
130 		int height = order[i] / RADIX_TREE_MAP_SHIFT;
131 		int shift = height * RADIX_TREE_MAP_SHIFT;
132 
133 		assert(iter.index == index[i]);
134 		assert(iter.shift == shift);
135 		i++;
136 	}
137 
138 	item_kill_tree(&tree);
139 }
140 
141 void multiorder_tagged_iteration(void)
142 {
143 	RADIX_TREE(tree, GFP_KERNEL);
144 	struct radix_tree_iter iter;
145 	void **slot;
146 	int i;
147 
148 	printf("Multiorder tagged iteration test\n");
149 
150 #define MT_NUM_ENTRIES 9
151 	int index[MT_NUM_ENTRIES] = {0, 2, 4, 16, 32, 40, 64, 72, 128};
152 	int order[MT_NUM_ENTRIES] = {1, 0, 2, 4,  3,  1,  3,  0,   7};
153 
154 #define TAG_ENTRIES 7
155 	int tag_index[TAG_ENTRIES] = {0, 4, 16, 40, 64, 72, 128};
156 
157 	for (i = 0; i < MT_NUM_ENTRIES; i++)
158 		assert(!item_insert_order(&tree, index[i], order[i]));
159 
160 	assert(!radix_tree_tagged(&tree, 1));
161 
162 	for (i = 0; i < TAG_ENTRIES; i++)
163 		assert(radix_tree_tag_set(&tree, tag_index[i], 1));
164 
165 	i = 0;
166 	/* start from index 1 to verify we find the multi-order entry at 0 */
167 	radix_tree_for_each_tagged(slot, &tree, &iter, 1, 1) {
168 		assert(iter.index == tag_index[i]);
169 		i++;
170 	}
171 
172 	/*
173 	 * Now iterate through the tree starting at an elevated multi-order
174 	 * entry, beginning at an index in the middle of the range.
175 	 */
176 	i = 4;
177 	radix_tree_for_each_slot(slot, &tree, &iter, 70) {
178 		assert(iter.index == tag_index[i]);
179 		i++;
180 	}
181 
182 	item_kill_tree(&tree);
183 }
184 
185 void multiorder_checks(void)
186 {
187 	int i;
188 
189 	for (i = 0; i < 20; i++) {
190 		multiorder_check(200, i);
191 		multiorder_check(0, i);
192 		multiorder_check((1UL << i) + 1, i);
193 	}
194 
195 	for (i = 0; i < 15; i++)
196 		multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
197 
198 	multiorder_insert_bug();
199 	multiorder_iteration();
200 	multiorder_tagged_iteration();
201 }
202