1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * idr-test.c: Test the IDR API 2 * idr-test.c: Test the IDR API 4 * Copyright (c) 2016 Matthew Wilcox <willy@in 3 * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org> >> 4 * >> 5 * This program is free software; you can redistribute it and/or modify it >> 6 * under the terms and conditions of the GNU General Public License, >> 7 * version 2, as published by the Free Software Foundation. >> 8 * >> 9 * This program is distributed in the hope it will be useful, but WITHOUT >> 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or >> 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for >> 12 * more details. 5 */ 13 */ 6 #include <linux/bitmap.h> 14 #include <linux/bitmap.h> 7 #include <linux/idr.h> 15 #include <linux/idr.h> 8 #include <linux/slab.h> 16 #include <linux/slab.h> 9 #include <linux/kernel.h> 17 #include <linux/kernel.h> 10 #include <linux/errno.h> 18 #include <linux/errno.h> 11 19 12 #include "test.h" 20 #include "test.h" 13 21 14 #define DUMMY_PTR ((void *)0x10) 22 #define DUMMY_PTR ((void *)0x10) 15 23 16 int item_idr_free(int id, void *p, void *data) 24 int item_idr_free(int id, void *p, void *data) 17 { 25 { 18 struct item *item = p; 26 struct item *item = p; 19 assert(item->index == id); 27 assert(item->index == id); 20 free(p); 28 free(p); 21 29 22 return 0; 30 return 0; 23 } 31 } 24 32 25 void item_idr_remove(struct idr *idr, int id) 33 void item_idr_remove(struct idr *idr, int id) 26 { 34 { 27 struct item *item = idr_find(idr, id); 35 struct item *item = idr_find(idr, id); 28 assert(item->index == id); 36 assert(item->index == id); 29 idr_remove(idr, id); 37 idr_remove(idr, id); 30 free(item); 38 free(item); 31 } 39 } 32 40 33 void idr_alloc_test(void) 41 void idr_alloc_test(void) 34 { 42 { 35 unsigned long i; 43 unsigned long i; 36 DEFINE_IDR(idr); 44 DEFINE_IDR(idr); 37 45 38 assert(idr_alloc_cyclic(&idr, DUMMY_PT 46 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0); 39 assert(idr_alloc_cyclic(&idr, DUMMY_PT 47 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd); 40 idr_remove(&idr, 0x3ffd); 48 idr_remove(&idr, 0x3ffd); 41 idr_remove(&idr, 0); 49 idr_remove(&idr, 0); 42 50 43 for (i = 0x3ffe; i < 0x4003; i++) { 51 for (i = 0x3ffe; i < 0x4003; i++) { 44 int id; 52 int id; 45 struct item *item; 53 struct item *item; 46 54 47 if (i < 0x4000) 55 if (i < 0x4000) 48 item = item_create(i, 56 item = item_create(i, 0); 49 else 57 else 50 item = item_create(i - 58 item = item_create(i - 0x3fff, 0); 51 59 52 id = idr_alloc_cyclic(&idr, it 60 id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL); 53 assert(id == item->index); 61 assert(id == item->index); 54 } 62 } 55 63 56 idr_for_each(&idr, item_idr_free, &idr 64 idr_for_each(&idr, item_idr_free, &idr); 57 idr_destroy(&idr); 65 idr_destroy(&idr); 58 } 66 } 59 67 60 void idr_replace_test(void) 68 void idr_replace_test(void) 61 { 69 { 62 DEFINE_IDR(idr); 70 DEFINE_IDR(idr); 63 71 64 idr_alloc(&idr, (void *)-1, 10, 11, GF 72 idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL); 65 idr_replace(&idr, &idr, 10); 73 idr_replace(&idr, &idr, 10); 66 74 67 idr_destroy(&idr); 75 idr_destroy(&idr); 68 } 76 } 69 77 70 /* 78 /* 71 * Unlike the radix tree, you can put a NULL p 79 * Unlike the radix tree, you can put a NULL pointer -- with care -- into 72 * the IDR. Some interfaces, like idr_find() 80 * the IDR. Some interfaces, like idr_find() do not distinguish between 73 * "present, value is NULL" and "not present", 81 * "present, value is NULL" and "not present", but that's exactly what some 74 * users want. 82 * users want. 75 */ 83 */ 76 void idr_null_test(void) 84 void idr_null_test(void) 77 { 85 { 78 int i; 86 int i; 79 DEFINE_IDR(idr); 87 DEFINE_IDR(idr); 80 88 81 assert(idr_is_empty(&idr)); 89 assert(idr_is_empty(&idr)); 82 90 83 assert(idr_alloc(&idr, NULL, 0, 0, GFP 91 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0); 84 assert(!idr_is_empty(&idr)); 92 assert(!idr_is_empty(&idr)); 85 idr_remove(&idr, 0); 93 idr_remove(&idr, 0); 86 assert(idr_is_empty(&idr)); 94 assert(idr_is_empty(&idr)); 87 95 88 assert(idr_alloc(&idr, NULL, 0, 0, GFP 96 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0); 89 assert(!idr_is_empty(&idr)); 97 assert(!idr_is_empty(&idr)); 90 idr_destroy(&idr); 98 idr_destroy(&idr); 91 assert(idr_is_empty(&idr)); 99 assert(idr_is_empty(&idr)); 92 100 93 for (i = 0; i < 10; i++) { 101 for (i = 0; i < 10; i++) { 94 assert(idr_alloc(&idr, NULL, 0 102 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i); 95 } 103 } 96 104 97 assert(idr_replace(&idr, DUMMY_PTR, 3) 105 assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL); 98 assert(idr_replace(&idr, DUMMY_PTR, 4) 106 assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL); 99 assert(idr_replace(&idr, NULL, 4) == D 107 assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR); 100 assert(idr_replace(&idr, DUMMY_PTR, 11 108 assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT)); 101 idr_remove(&idr, 5); 109 idr_remove(&idr, 5); 102 assert(idr_alloc(&idr, NULL, 0, 0, GFP 110 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5); 103 idr_remove(&idr, 5); 111 idr_remove(&idr, 5); 104 112 105 for (i = 0; i < 9; i++) { 113 for (i = 0; i < 9; i++) { 106 idr_remove(&idr, i); 114 idr_remove(&idr, i); 107 assert(!idr_is_empty(&idr)); 115 assert(!idr_is_empty(&idr)); 108 } 116 } 109 idr_remove(&idr, 8); 117 idr_remove(&idr, 8); 110 assert(!idr_is_empty(&idr)); 118 assert(!idr_is_empty(&idr)); 111 idr_remove(&idr, 9); 119 idr_remove(&idr, 9); 112 assert(idr_is_empty(&idr)); 120 assert(idr_is_empty(&idr)); 113 121 114 assert(idr_alloc(&idr, NULL, 0, 0, GFP 122 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0); 115 assert(idr_replace(&idr, DUMMY_PTR, 3) 123 assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT)); 116 assert(idr_replace(&idr, DUMMY_PTR, 0) 124 assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL); 117 assert(idr_replace(&idr, NULL, 0) == D 125 assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR); 118 126 119 idr_destroy(&idr); 127 idr_destroy(&idr); 120 assert(idr_is_empty(&idr)); 128 assert(idr_is_empty(&idr)); 121 129 122 for (i = 1; i < 10; i++) { 130 for (i = 1; i < 10; i++) { 123 assert(idr_alloc(&idr, NULL, 1 131 assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i); 124 } 132 } 125 133 126 idr_destroy(&idr); 134 idr_destroy(&idr); 127 assert(idr_is_empty(&idr)); 135 assert(idr_is_empty(&idr)); 128 } 136 } 129 137 130 void idr_nowait_test(void) 138 void idr_nowait_test(void) 131 { 139 { 132 unsigned int i; 140 unsigned int i; 133 DEFINE_IDR(idr); 141 DEFINE_IDR(idr); 134 142 135 idr_preload(GFP_KERNEL); 143 idr_preload(GFP_KERNEL); 136 144 137 for (i = 0; i < 3; i++) { 145 for (i = 0; i < 3; i++) { 138 struct item *item = item_creat 146 struct item *item = item_create(i, 0); 139 assert(idr_alloc(&idr, item, i 147 assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i); 140 } 148 } 141 149 142 idr_preload_end(); 150 idr_preload_end(); 143 151 144 idr_for_each(&idr, item_idr_free, &idr 152 idr_for_each(&idr, item_idr_free, &idr); 145 idr_destroy(&idr); 153 idr_destroy(&idr); 146 } 154 } 147 155 148 void idr_get_next_test(int base) 156 void idr_get_next_test(int base) 149 { 157 { 150 unsigned long i; 158 unsigned long i; 151 int nextid; 159 int nextid; 152 DEFINE_IDR(idr); 160 DEFINE_IDR(idr); 153 idr_init_base(&idr, base); 161 idr_init_base(&idr, base); 154 162 155 int indices[] = {4, 7, 9, 15, 65, 128, 163 int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0}; 156 164 157 for(i = 0; indices[i]; i++) { 165 for(i = 0; indices[i]; i++) { 158 struct item *item = item_creat 166 struct item *item = item_create(indices[i], 0); 159 assert(idr_alloc(&idr, item, i 167 assert(idr_alloc(&idr, item, indices[i], indices[i+1], 160 GFP_KERNEL) = 168 GFP_KERNEL) == indices[i]); 161 } 169 } 162 170 163 for(i = 0, nextid = 0; indices[i]; i++ 171 for(i = 0, nextid = 0; indices[i]; i++) { 164 idr_get_next(&idr, &nextid); 172 idr_get_next(&idr, &nextid); 165 assert(nextid == indices[i]); 173 assert(nextid == indices[i]); 166 nextid++; 174 nextid++; 167 } 175 } 168 176 169 idr_for_each(&idr, item_idr_free, &idr 177 idr_for_each(&idr, item_idr_free, &idr); 170 idr_destroy(&idr); 178 idr_destroy(&idr); 171 } 179 } 172 180 173 int idr_u32_cb(int id, void *ptr, void *data) 181 int idr_u32_cb(int id, void *ptr, void *data) 174 { 182 { 175 BUG_ON(id < 0); 183 BUG_ON(id < 0); 176 BUG_ON(ptr != DUMMY_PTR); 184 BUG_ON(ptr != DUMMY_PTR); 177 return 0; 185 return 0; 178 } 186 } 179 187 180 void idr_u32_test1(struct idr *idr, u32 handle 188 void idr_u32_test1(struct idr *idr, u32 handle) 181 { 189 { 182 static bool warned = false; 190 static bool warned = false; 183 u32 id = handle; 191 u32 id = handle; 184 int sid = 0; 192 int sid = 0; 185 void *ptr; 193 void *ptr; 186 194 187 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, & 195 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL)); 188 BUG_ON(id != handle); 196 BUG_ON(id != handle); 189 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, & 197 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL) != -ENOSPC); 190 BUG_ON(id != handle); 198 BUG_ON(id != handle); 191 if (!warned && id > INT_MAX) 199 if (!warned && id > INT_MAX) 192 printk("vvv Ignore these warni 200 printk("vvv Ignore these warnings\n"); 193 ptr = idr_get_next(idr, &sid); 201 ptr = idr_get_next(idr, &sid); 194 if (id > INT_MAX) { 202 if (id > INT_MAX) { 195 BUG_ON(ptr != NULL); 203 BUG_ON(ptr != NULL); 196 BUG_ON(sid != 0); 204 BUG_ON(sid != 0); 197 } else { 205 } else { 198 BUG_ON(ptr != DUMMY_PTR); 206 BUG_ON(ptr != DUMMY_PTR); 199 BUG_ON(sid != id); 207 BUG_ON(sid != id); 200 } 208 } 201 idr_for_each(idr, idr_u32_cb, NULL); 209 idr_for_each(idr, idr_u32_cb, NULL); 202 if (!warned && id > INT_MAX) { 210 if (!warned && id > INT_MAX) { 203 printk("^^^ Warnings over\n"); 211 printk("^^^ Warnings over\n"); 204 warned = true; 212 warned = true; 205 } 213 } 206 BUG_ON(idr_remove(idr, id) != DUMMY_PT 214 BUG_ON(idr_remove(idr, id) != DUMMY_PTR); 207 BUG_ON(!idr_is_empty(idr)); 215 BUG_ON(!idr_is_empty(idr)); 208 } 216 } 209 217 210 void idr_u32_test(int base) 218 void idr_u32_test(int base) 211 { 219 { 212 DEFINE_IDR(idr); 220 DEFINE_IDR(idr); 213 idr_init_base(&idr, base); 221 idr_init_base(&idr, base); 214 idr_u32_test1(&idr, 10); 222 idr_u32_test1(&idr, 10); 215 idr_u32_test1(&idr, 0x7fffffff); 223 idr_u32_test1(&idr, 0x7fffffff); 216 idr_u32_test1(&idr, 0x80000000); 224 idr_u32_test1(&idr, 0x80000000); 217 idr_u32_test1(&idr, 0x80000001); 225 idr_u32_test1(&idr, 0x80000001); 218 idr_u32_test1(&idr, 0xffe00000); 226 idr_u32_test1(&idr, 0xffe00000); 219 idr_u32_test1(&idr, 0xffffffff); 227 idr_u32_test1(&idr, 0xffffffff); 220 } 228 } 221 229 222 static void idr_align_test(struct idr *idr) 230 static void idr_align_test(struct idr *idr) 223 { 231 { 224 char name[] = "Motorola 68000"; 232 char name[] = "Motorola 68000"; 225 int i, id; 233 int i, id; 226 void *entry; 234 void *entry; 227 235 228 for (i = 0; i < 9; i++) { 236 for (i = 0; i < 9; i++) { 229 BUG_ON(idr_alloc(idr, &name[i] 237 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i); 230 idr_for_each_entry(idr, entry, 238 idr_for_each_entry(idr, entry, id); 231 } 239 } 232 idr_destroy(idr); 240 idr_destroy(idr); 233 241 234 for (i = 1; i < 10; i++) { 242 for (i = 1; i < 10; i++) { 235 BUG_ON(idr_alloc(idr, &name[i] 243 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 1); 236 idr_for_each_entry(idr, entry, 244 idr_for_each_entry(idr, entry, id); 237 } 245 } 238 idr_destroy(idr); 246 idr_destroy(idr); 239 247 240 for (i = 2; i < 11; i++) { 248 for (i = 2; i < 11; i++) { 241 BUG_ON(idr_alloc(idr, &name[i] 249 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 2); 242 idr_for_each_entry(idr, entry, 250 idr_for_each_entry(idr, entry, id); 243 } 251 } 244 idr_destroy(idr); 252 idr_destroy(idr); 245 253 246 for (i = 3; i < 12; i++) { 254 for (i = 3; i < 12; i++) { 247 BUG_ON(idr_alloc(idr, &name[i] 255 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 3); 248 idr_for_each_entry(idr, entry, 256 idr_for_each_entry(idr, entry, id); 249 } 257 } 250 idr_destroy(idr); 258 idr_destroy(idr); 251 259 252 for (i = 0; i < 8; i++) { 260 for (i = 0; i < 8; i++) { 253 BUG_ON(idr_alloc(idr, &name[i] 261 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0); 254 BUG_ON(idr_alloc(idr, &name[i 262 BUG_ON(idr_alloc(idr, &name[i + 1], 0, 0, GFP_KERNEL) != 1); 255 idr_for_each_entry(idr, entry, 263 idr_for_each_entry(idr, entry, id); 256 idr_remove(idr, 1); 264 idr_remove(idr, 1); 257 idr_for_each_entry(idr, entry, 265 idr_for_each_entry(idr, entry, id); 258 idr_remove(idr, 0); 266 idr_remove(idr, 0); 259 BUG_ON(!idr_is_empty(idr)); 267 BUG_ON(!idr_is_empty(idr)); 260 } 268 } 261 269 262 for (i = 0; i < 8; i++) { 270 for (i = 0; i < 8; i++) { 263 BUG_ON(idr_alloc(idr, NULL, 0, 271 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 0); 264 idr_for_each_entry(idr, entry, 272 idr_for_each_entry(idr, entry, id); 265 idr_replace(idr, &name[i], 0); 273 idr_replace(idr, &name[i], 0); 266 idr_for_each_entry(idr, entry, 274 idr_for_each_entry(idr, entry, id); 267 BUG_ON(idr_find(idr, 0) != &na 275 BUG_ON(idr_find(idr, 0) != &name[i]); 268 idr_remove(idr, 0); 276 idr_remove(idr, 0); 269 } 277 } 270 278 271 for (i = 0; i < 8; i++) { 279 for (i = 0; i < 8; i++) { 272 BUG_ON(idr_alloc(idr, &name[i] 280 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0); 273 BUG_ON(idr_alloc(idr, NULL, 0, 281 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 1); 274 idr_remove(idr, 1); 282 idr_remove(idr, 1); 275 idr_for_each_entry(idr, entry, 283 idr_for_each_entry(idr, entry, id); 276 idr_replace(idr, &name[i + 1], 284 idr_replace(idr, &name[i + 1], 0); 277 idr_for_each_entry(idr, entry, 285 idr_for_each_entry(idr, entry, id); 278 idr_remove(idr, 0); 286 idr_remove(idr, 0); 279 } 287 } 280 } 288 } 281 289 282 DEFINE_IDR(find_idr); 290 DEFINE_IDR(find_idr); 283 291 284 static void *idr_throbber(void *arg) 292 static void *idr_throbber(void *arg) 285 { 293 { 286 time_t start = time(NULL); 294 time_t start = time(NULL); 287 int id = *(int *)arg; 295 int id = *(int *)arg; 288 296 289 rcu_register_thread(); 297 rcu_register_thread(); 290 do { 298 do { 291 idr_alloc(&find_idr, xa_mk_val 299 idr_alloc(&find_idr, xa_mk_value(id), id, id + 1, GFP_KERNEL); 292 idr_remove(&find_idr, id); 300 idr_remove(&find_idr, id); 293 } while (time(NULL) < start + 10); 301 } while (time(NULL) < start + 10); 294 rcu_unregister_thread(); 302 rcu_unregister_thread(); 295 303 296 return NULL; 304 return NULL; 297 } 305 } 298 306 299 /* << 300 * There are always either 1 or 2 objects in t << 301 * or we find something at an ID we didn't exp << 302 */ << 303 void idr_find_test_1(int anchor_id, int throbb 307 void idr_find_test_1(int anchor_id, int throbber_id) 304 { 308 { 305 pthread_t throbber; 309 pthread_t throbber; 306 time_t start = time(NULL); 310 time_t start = time(NULL); 307 311 >> 312 pthread_create(&throbber, NULL, idr_throbber, &throbber_id); >> 313 308 BUG_ON(idr_alloc(&find_idr, xa_mk_valu 314 BUG_ON(idr_alloc(&find_idr, xa_mk_value(anchor_id), anchor_id, 309 anchor_id + 1, 315 anchor_id + 1, GFP_KERNEL) != anchor_id); 310 316 311 pthread_create(&throbber, NULL, idr_th << 312 << 313 rcu_read_lock(); << 314 do { 317 do { 315 int id = 0; 318 int id = 0; 316 void *entry = idr_get_next(&fi 319 void *entry = idr_get_next(&find_idr, &id); 317 rcu_read_unlock(); !! 320 BUG_ON(entry != xa_mk_value(id)); 318 if ((id != anchor_id && id != << 319 entry != xa_mk_value(id)) << 320 printf("%s(%d, %d): %p << 321 throbber_id, e << 322 abort(); << 323 } << 324 rcu_read_lock(); << 325 } while (time(NULL) < start + 11); 321 } while (time(NULL) < start + 11); 326 rcu_read_unlock(); << 327 322 328 pthread_join(throbber, NULL); 323 pthread_join(throbber, NULL); 329 324 330 idr_remove(&find_idr, anchor_id); 325 idr_remove(&find_idr, anchor_id); 331 BUG_ON(!idr_is_empty(&find_idr)); 326 BUG_ON(!idr_is_empty(&find_idr)); 332 } 327 } 333 328 334 void idr_find_test(void) 329 void idr_find_test(void) 335 { 330 { 336 idr_find_test_1(100000, 0); 331 idr_find_test_1(100000, 0); 337 idr_find_test_1(0, 100000); 332 idr_find_test_1(0, 100000); 338 } 333 } 339 334 340 void idr_checks(void) 335 void idr_checks(void) 341 { 336 { 342 unsigned long i; 337 unsigned long i; 343 DEFINE_IDR(idr); 338 DEFINE_IDR(idr); 344 339 345 for (i = 0; i < 10000; i++) { 340 for (i = 0; i < 10000; i++) { 346 struct item *item = item_creat 341 struct item *item = item_create(i, 0); 347 assert(idr_alloc(&idr, item, 0 342 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i); 348 } 343 } 349 344 350 assert(idr_alloc(&idr, DUMMY_PTR, 5, 3 345 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0); 351 346 352 for (i = 0; i < 5000; i++) 347 for (i = 0; i < 5000; i++) 353 item_idr_remove(&idr, i); 348 item_idr_remove(&idr, i); 354 349 355 idr_remove(&idr, 3); 350 idr_remove(&idr, 3); 356 351 357 idr_for_each(&idr, item_idr_free, &idr 352 idr_for_each(&idr, item_idr_free, &idr); 358 idr_destroy(&idr); 353 idr_destroy(&idr); 359 354 360 assert(idr_is_empty(&idr)); 355 assert(idr_is_empty(&idr)); 361 356 362 idr_remove(&idr, 3); 357 idr_remove(&idr, 3); 363 idr_remove(&idr, 0); 358 idr_remove(&idr, 0); 364 359 365 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0 360 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0); 366 idr_remove(&idr, 1); 361 idr_remove(&idr, 1); 367 for (i = 1; i < RADIX_TREE_MAP_SIZE; i 362 for (i = 1; i < RADIX_TREE_MAP_SIZE; i++) 368 assert(idr_alloc(&idr, DUMMY_P 363 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i); 369 idr_remove(&idr, 1 << 30); 364 idr_remove(&idr, 1 << 30); 370 idr_destroy(&idr); 365 idr_destroy(&idr); 371 366 372 for (i = INT_MAX - 3UL; i < INT_MAX + 367 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) { 373 struct item *item = item_creat 368 struct item *item = item_create(i, 0); 374 assert(idr_alloc(&idr, item, i 369 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i); 375 } 370 } 376 assert(idr_alloc(&idr, DUMMY_PTR, i - 371 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC); 377 assert(idr_alloc(&idr, DUMMY_PTR, i - 372 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC); 378 373 379 idr_for_each(&idr, item_idr_free, &idr 374 idr_for_each(&idr, item_idr_free, &idr); 380 idr_destroy(&idr); 375 idr_destroy(&idr); 381 idr_destroy(&idr); 376 idr_destroy(&idr); 382 377 383 assert(idr_is_empty(&idr)); 378 assert(idr_is_empty(&idr)); 384 379 385 idr_set_cursor(&idr, INT_MAX - 3UL); 380 idr_set_cursor(&idr, INT_MAX - 3UL); 386 for (i = INT_MAX - 3UL; i < INT_MAX + 381 for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) { 387 struct item *item; 382 struct item *item; 388 unsigned int id; 383 unsigned int id; 389 if (i <= INT_MAX) 384 if (i <= INT_MAX) 390 item = item_create(i, 385 item = item_create(i, 0); 391 else 386 else 392 item = item_create(i - 387 item = item_create(i - INT_MAX - 1, 0); 393 388 394 id = idr_alloc_cyclic(&idr, it 389 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL); 395 assert(id == item->index); 390 assert(id == item->index); 396 } 391 } 397 392 398 idr_for_each(&idr, item_idr_free, &idr 393 idr_for_each(&idr, item_idr_free, &idr); 399 idr_destroy(&idr); 394 idr_destroy(&idr); 400 assert(idr_is_empty(&idr)); 395 assert(idr_is_empty(&idr)); 401 396 402 for (i = 1; i < 10000; i++) { 397 for (i = 1; i < 10000; i++) { 403 struct item *item = item_creat 398 struct item *item = item_create(i, 0); 404 assert(idr_alloc(&idr, item, 1 399 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i); 405 } 400 } 406 401 407 idr_for_each(&idr, item_idr_free, &idr 402 idr_for_each(&idr, item_idr_free, &idr); 408 idr_destroy(&idr); 403 idr_destroy(&idr); 409 404 410 idr_replace_test(); 405 idr_replace_test(); 411 idr_alloc_test(); 406 idr_alloc_test(); 412 idr_null_test(); 407 idr_null_test(); 413 idr_nowait_test(); 408 idr_nowait_test(); 414 idr_get_next_test(0); 409 idr_get_next_test(0); 415 idr_get_next_test(1); 410 idr_get_next_test(1); 416 idr_get_next_test(4); 411 idr_get_next_test(4); 417 idr_u32_test(4); 412 idr_u32_test(4); 418 idr_u32_test(1); 413 idr_u32_test(1); 419 idr_u32_test(0); 414 idr_u32_test(0); 420 idr_align_test(&idr); 415 idr_align_test(&idr); 421 idr_find_test(); 416 idr_find_test(); 422 } 417 } 423 418 424 #define module_init(x) 419 #define module_init(x) 425 #define module_exit(x) 420 #define module_exit(x) 426 #define MODULE_AUTHOR(x) 421 #define MODULE_AUTHOR(x) 427 #define MODULE_DESCRIPTION(X) << 428 #define MODULE_LICENSE(x) 422 #define MODULE_LICENSE(x) 429 #define dump_stack() assert(0) 423 #define dump_stack() assert(0) 430 void ida_dump(struct ida *); 424 void ida_dump(struct ida *); 431 425 432 #include "../../../lib/test_ida.c" 426 #include "../../../lib/test_ida.c" 433 427 434 /* 428 /* 435 * Check that we get the correct error when we 429 * Check that we get the correct error when we run out of memory doing 436 * allocations. In userspace, GFP_NOWAIT will 430 * allocations. In userspace, GFP_NOWAIT will always fail an allocation. 437 * The first test is for not having a bitmap a 431 * The first test is for not having a bitmap available, and the second test 438 * is for not being able to allocate a level o 432 * is for not being able to allocate a level of the radix tree. 439 */ 433 */ 440 void ida_check_nomem(void) 434 void ida_check_nomem(void) 441 { 435 { 442 DEFINE_IDA(ida); 436 DEFINE_IDA(ida); 443 int id; 437 int id; 444 438 445 id = ida_alloc_min(&ida, 256, GFP_NOWA 439 id = ida_alloc_min(&ida, 256, GFP_NOWAIT); 446 IDA_BUG_ON(&ida, id != -ENOMEM); 440 IDA_BUG_ON(&ida, id != -ENOMEM); 447 id = ida_alloc_min(&ida, 1UL << 30, GF 441 id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT); 448 IDA_BUG_ON(&ida, id != -ENOMEM); 442 IDA_BUG_ON(&ida, id != -ENOMEM); 449 IDA_BUG_ON(&ida, !ida_is_empty(&ida)); 443 IDA_BUG_ON(&ida, !ida_is_empty(&ida)); 450 } 444 } 451 445 452 /* 446 /* 453 * Check handling of conversions between excep 447 * Check handling of conversions between exceptional entries and full bitmaps. 454 */ 448 */ 455 void ida_check_conv_user(void) 449 void ida_check_conv_user(void) 456 { 450 { 457 DEFINE_IDA(ida); 451 DEFINE_IDA(ida); 458 unsigned long i; 452 unsigned long i; 459 453 460 for (i = 0; i < 1000000; i++) { 454 for (i = 0; i < 1000000; i++) { 461 int id = ida_alloc(&ida, GFP_N 455 int id = ida_alloc(&ida, GFP_NOWAIT); 462 if (id == -ENOMEM) { 456 if (id == -ENOMEM) { 463 IDA_BUG_ON(&ida, ((i % 457 IDA_BUG_ON(&ida, ((i % IDA_BITMAP_BITS) != 464 BITS 458 BITS_PER_XA_VALUE) && 465 ((i % 459 ((i % IDA_BITMAP_BITS) != 0)); 466 id = ida_alloc(&ida, G 460 id = ida_alloc(&ida, GFP_KERNEL); 467 } else { 461 } else { 468 IDA_BUG_ON(&ida, (i % 462 IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) == 469 BITS_P 463 BITS_PER_XA_VALUE); 470 } 464 } 471 IDA_BUG_ON(&ida, id != i); 465 IDA_BUG_ON(&ida, id != i); 472 } 466 } 473 ida_destroy(&ida); 467 ida_destroy(&ida); 474 } 468 } 475 469 476 void ida_check_random(void) 470 void ida_check_random(void) 477 { 471 { 478 DEFINE_IDA(ida); 472 DEFINE_IDA(ida); 479 DECLARE_BITMAP(bitmap, 2048); 473 DECLARE_BITMAP(bitmap, 2048); 480 unsigned int i; 474 unsigned int i; 481 time_t s = time(NULL); 475 time_t s = time(NULL); 482 476 483 repeat: 477 repeat: 484 memset(bitmap, 0, sizeof(bitmap)); 478 memset(bitmap, 0, sizeof(bitmap)); 485 for (i = 0; i < 100000; i++) { 479 for (i = 0; i < 100000; i++) { 486 int i = rand(); 480 int i = rand(); 487 int bit = i & 2047; 481 int bit = i & 2047; 488 if (test_bit(bit, bitmap)) { 482 if (test_bit(bit, bitmap)) { 489 __clear_bit(bit, bitma 483 __clear_bit(bit, bitmap); 490 ida_free(&ida, bit); 484 ida_free(&ida, bit); 491 } else { 485 } else { 492 __set_bit(bit, bitmap) 486 __set_bit(bit, bitmap); 493 IDA_BUG_ON(&ida, ida_a 487 IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL) 494 != bit 488 != bit); 495 } 489 } 496 } 490 } 497 ida_destroy(&ida); 491 ida_destroy(&ida); 498 if (time(NULL) < s + 10) 492 if (time(NULL) < s + 10) 499 goto repeat; 493 goto repeat; 500 } 494 } 501 495 502 void ida_simple_get_remove_test(void) 496 void ida_simple_get_remove_test(void) 503 { 497 { 504 DEFINE_IDA(ida); 498 DEFINE_IDA(ida); 505 unsigned long i; 499 unsigned long i; 506 500 507 for (i = 0; i < 10000; i++) { 501 for (i = 0; i < 10000; i++) { 508 assert(ida_simple_get(&ida, 0, 502 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i); 509 } 503 } 510 assert(ida_simple_get(&ida, 5, 30, GFP 504 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0); 511 505 512 for (i = 0; i < 10000; i++) { 506 for (i = 0; i < 10000; i++) { 513 ida_simple_remove(&ida, i); 507 ida_simple_remove(&ida, i); 514 } 508 } 515 assert(ida_is_empty(&ida)); 509 assert(ida_is_empty(&ida)); 516 510 517 ida_destroy(&ida); 511 ida_destroy(&ida); 518 } 512 } 519 513 520 void user_ida_checks(void) 514 void user_ida_checks(void) 521 { 515 { 522 radix_tree_cpu_dead(1); 516 radix_tree_cpu_dead(1); 523 517 524 ida_check_nomem(); 518 ida_check_nomem(); 525 ida_check_conv_user(); 519 ida_check_conv_user(); 526 ida_check_random(); 520 ida_check_random(); 527 ida_simple_get_remove_test(); 521 ida_simple_get_remove_test(); 528 522 529 radix_tree_cpu_dead(1); 523 radix_tree_cpu_dead(1); 530 } 524 } 531 525 532 static void *ida_random_fn(void *arg) 526 static void *ida_random_fn(void *arg) 533 { 527 { 534 rcu_register_thread(); 528 rcu_register_thread(); 535 ida_check_random(); 529 ida_check_random(); 536 rcu_unregister_thread(); 530 rcu_unregister_thread(); 537 return NULL; 531 return NULL; 538 } 532 } 539 533 540 static void *ida_leak_fn(void *arg) << 541 { << 542 struct ida *ida = arg; << 543 time_t s = time(NULL); << 544 int i, ret; << 545 << 546 rcu_register_thread(); << 547 << 548 do for (i = 0; i < 1000; i++) { << 549 ret = ida_alloc_range(ida, 128 << 550 if (ret >= 0) << 551 ida_free(ida, 128); << 552 } while (time(NULL) < s + 2); << 553 << 554 rcu_unregister_thread(); << 555 return NULL; << 556 } << 557 << 558 void ida_thread_tests(void) 534 void ida_thread_tests(void) 559 { 535 { 560 DEFINE_IDA(ida); << 561 pthread_t threads[20]; 536 pthread_t threads[20]; 562 int i; 537 int i; 563 538 564 for (i = 0; i < ARRAY_SIZE(threads); i 539 for (i = 0; i < ARRAY_SIZE(threads); i++) 565 if (pthread_create(&threads[i] 540 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) { 566 perror("creating ida t 541 perror("creating ida thread"); 567 exit(1); 542 exit(1); 568 } 543 } 569 544 570 while (i--) 545 while (i--) 571 pthread_join(threads[i], NULL) 546 pthread_join(threads[i], NULL); 572 << 573 for (i = 0; i < ARRAY_SIZE(threads); i << 574 if (pthread_create(&threads[i] << 575 perror("creating ida t << 576 exit(1); << 577 } << 578 << 579 while (i--) << 580 pthread_join(threads[i], NULL) << 581 assert(ida_is_empty(&ida)); << 582 } 547 } 583 548 584 void ida_tests(void) 549 void ida_tests(void) 585 { 550 { 586 user_ida_checks(); 551 user_ida_checks(); 587 ida_checks(); 552 ida_checks(); 588 ida_exit(); 553 ida_exit(); 589 ida_thread_tests(); 554 ida_thread_tests(); 590 } 555 } 591 556 592 int __weak main(void) 557 int __weak main(void) 593 { 558 { 594 rcu_register_thread(); << 595 radix_tree_init(); 559 radix_tree_init(); 596 idr_checks(); 560 idr_checks(); 597 ida_tests(); 561 ida_tests(); 598 radix_tree_cpu_dead(1); 562 radix_tree_cpu_dead(1); 599 rcu_barrier(); 563 rcu_barrier(); 600 if (nr_allocated) 564 if (nr_allocated) 601 printf("nr_allocated = %d\n", 565 printf("nr_allocated = %d\n", nr_allocated); 602 rcu_unregister_thread(); << 603 return 0; 566 return 0; 604 } 567 } 605 568
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.