1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * test_xarray.c: Test the XArray API 4 * Copyright (c) 2017-2018 Microsoft Corporation 5 * Copyright (c) 2019-2020 Oracle 6 * Author: Matthew Wilcox <[email protected]> 7 */ 8 9 #include <linux/xarray.h> 10 #include <linux/module.h> 11 12 static unsigned int tests_run; 13 static unsigned int tests_passed; 14 15 static const unsigned int order_limit = 16 IS_ENABLED(CONFIG_XARRAY_MULTI) ? BITS_PER_LONG : 1; 17 18 #ifndef XA_DEBUG 19 # ifdef __KERNEL__ 20 void xa_dump(const struct xarray *xa) { } 21 # endif 22 #undef XA_BUG_ON 23 #define XA_BUG_ON(xa, x) do { \ 24 tests_run++; \ 25 if (x) { \ 26 printk("BUG at %s:%d\n", __func__, __LINE__); \ 27 xa_dump(xa); \ 28 dump_stack(); \ 29 } else { \ 30 tests_passed++; \ 31 } \ 32 } while (0) 33 #endif 34 35 static void *xa_mk_index(unsigned long index) 36 { 37 return xa_mk_value(index & LONG_MAX); 38 } 39 40 static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) 41 { 42 return xa_store(xa, index, xa_mk_index(index), gfp); 43 } 44 45 static void xa_insert_index(struct xarray *xa, unsigned long index) 46 { 47 XA_BUG_ON(xa, xa_insert(xa, index, xa_mk_index(index), 48 GFP_KERNEL) != 0); 49 } 50 51 static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp) 52 { 53 u32 id; 54 55 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(index), xa_limit_32b, 56 gfp) != 0); 57 XA_BUG_ON(xa, id != index); 58 } 59 60 static void xa_erase_index(struct xarray *xa, unsigned long index) 61 { 62 XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index)); 63 XA_BUG_ON(xa, xa_load(xa, index) != NULL); 64 } 65 66 /* 67 * If anyone needs this, please move it to xarray.c. We have no current 68 * users outside the test suite because all current multislot users want 69 * to use the advanced API. 70 */ 71 static void *xa_store_order(struct xarray *xa, unsigned long index, 72 unsigned order, void *entry, gfp_t gfp) 73 { 74 XA_STATE_ORDER(xas, xa, index, order); 75 void *curr; 76 77 do { 78 xas_lock(&xas); 79 curr = xas_store(&xas, entry); 80 xas_unlock(&xas); 81 } while (xas_nomem(&xas, gfp)); 82 83 return curr; 84 } 85 86 static noinline void check_xa_err(struct xarray *xa) 87 { 88 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0); 89 XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0); 90 #ifndef __KERNEL__ 91 /* The kernel does not fail GFP_NOWAIT allocations */ 92 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); 93 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); 94 #endif 95 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0); 96 XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0); 97 XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0); 98 // kills the test-suite :-( 99 // XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL); 100 } 101 102 static noinline void check_xas_retry(struct xarray *xa) 103 { 104 XA_STATE(xas, xa, 0); 105 void *entry; 106 107 xa_store_index(xa, 0, GFP_KERNEL); 108 xa_store_index(xa, 1, GFP_KERNEL); 109 110 rcu_read_lock(); 111 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0)); 112 xa_erase_index(xa, 1); 113 XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas))); 114 XA_BUG_ON(xa, xas_retry(&xas, NULL)); 115 XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0))); 116 xas_reset(&xas); 117 XA_BUG_ON(xa, xas.xa_node != XAS_RESTART); 118 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); 119 XA_BUG_ON(xa, xas.xa_node != NULL); 120 rcu_read_unlock(); 121 122 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); 123 124 rcu_read_lock(); 125 XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas))); 126 xas.xa_node = XAS_RESTART; 127 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); 128 rcu_read_unlock(); 129 130 /* Make sure we can iterate through retry entries */ 131 xas_lock(&xas); 132 xas_set(&xas, 0); 133 xas_store(&xas, XA_RETRY_ENTRY); 134 xas_set(&xas, 1); 135 xas_store(&xas, XA_RETRY_ENTRY); 136 137 xas_set(&xas, 0); 138 xas_for_each(&xas, entry, ULONG_MAX) { 139 xas_store(&xas, xa_mk_index(xas.xa_index)); 140 } 141 xas_unlock(&xas); 142 143 xa_erase_index(xa, 0); 144 xa_erase_index(xa, 1); 145 } 146 147 static noinline void check_xa_load(struct xarray *xa) 148 { 149 unsigned long i, j; 150 151 for (i = 0; i < 1024; i++) { 152 for (j = 0; j < 1024; j++) { 153 void *entry = xa_load(xa, j); 154 if (j < i) 155 XA_BUG_ON(xa, xa_to_value(entry) != j); 156 else 157 XA_BUG_ON(xa, entry); 158 } 159 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); 160 } 161 162 for (i = 0; i < 1024; i++) { 163 for (j = 0; j < 1024; j++) { 164 void *entry = xa_load(xa, j); 165 if (j >= i) 166 XA_BUG_ON(xa, xa_to_value(entry) != j); 167 else 168 XA_BUG_ON(xa, entry); 169 } 170 xa_erase_index(xa, i); 171 } 172 XA_BUG_ON(xa, !xa_empty(xa)); 173 } 174 175 static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index) 176 { 177 unsigned int order; 178 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1; 179 180 /* NULL elements have no marks set */ 181 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); 182 xa_set_mark(xa, index, XA_MARK_0); 183 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); 184 185 /* Storing a pointer will not make a mark appear */ 186 XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL); 187 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); 188 xa_set_mark(xa, index, XA_MARK_0); 189 XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); 190 191 /* Setting one mark will not set another mark */ 192 XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0)); 193 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1)); 194 195 /* Storing NULL clears marks, and they can't be set again */ 196 xa_erase_index(xa, index); 197 XA_BUG_ON(xa, !xa_empty(xa)); 198 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); 199 xa_set_mark(xa, index, XA_MARK_0); 200 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); 201 202 /* 203 * Storing a multi-index entry over entries with marks gives the 204 * entire entry the union of the marks 205 */ 206 BUG_ON((index % 4) != 0); 207 for (order = 2; order < max_order; order++) { 208 unsigned long base = round_down(index, 1UL << order); 209 unsigned long next = base + (1UL << order); 210 unsigned long i; 211 212 XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL)); 213 xa_set_mark(xa, index + 1, XA_MARK_0); 214 XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL)); 215 xa_set_mark(xa, index + 2, XA_MARK_2); 216 XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL)); 217 xa_store_order(xa, index, order, xa_mk_index(index), 218 GFP_KERNEL); 219 for (i = base; i < next; i++) { 220 XA_STATE(xas, xa, i); 221 unsigned int seen = 0; 222 void *entry; 223 224 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); 225 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_1)); 226 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_2)); 227 228 /* We should see two elements in the array */ 229 rcu_read_lock(); 230 xas_for_each(&xas, entry, ULONG_MAX) 231 seen++; 232 rcu_read_unlock(); 233 XA_BUG_ON(xa, seen != 2); 234 235 /* One of which is marked */ 236 xas_set(&xas, 0); 237 seen = 0; 238 rcu_read_lock(); 239 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) 240 seen++; 241 rcu_read_unlock(); 242 XA_BUG_ON(xa, seen != 1); 243 } 244 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0)); 245 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1)); 246 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2)); 247 xa_erase_index(xa, index); 248 xa_erase_index(xa, next); 249 XA_BUG_ON(xa, !xa_empty(xa)); 250 } 251 XA_BUG_ON(xa, !xa_empty(xa)); 252 } 253 254 static noinline void check_xa_mark_2(struct xarray *xa) 255 { 256 XA_STATE(xas, xa, 0); 257 unsigned long index; 258 unsigned int count = 0; 259 void *entry; 260 261 xa_store_index(xa, 0, GFP_KERNEL); 262 xa_set_mark(xa, 0, XA_MARK_0); 263 xas_lock(&xas); 264 xas_load(&xas); 265 xas_init_marks(&xas); 266 xas_unlock(&xas); 267 XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0); 268 269 for (index = 3500; index < 4500; index++) { 270 xa_store_index(xa, index, GFP_KERNEL); 271 xa_set_mark(xa, index, XA_MARK_0); 272 } 273 274 xas_reset(&xas); 275 rcu_read_lock(); 276 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) 277 count++; 278 rcu_read_unlock(); 279 XA_BUG_ON(xa, count != 1000); 280 281 xas_lock(&xas); 282 xas_for_each(&xas, entry, ULONG_MAX) { 283 xas_init_marks(&xas); 284 XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0)); 285 XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0)); 286 } 287 xas_unlock(&xas); 288 289 xa_destroy(xa); 290 } 291 292 static noinline void check_xa_mark_3(struct xarray *xa) 293 { 294 #ifdef CONFIG_XARRAY_MULTI 295 XA_STATE(xas, xa, 0x41); 296 void *entry; 297 int count = 0; 298 299 xa_store_order(xa, 0x40, 2, xa_mk_index(0x40), GFP_KERNEL); 300 xa_set_mark(xa, 0x41, XA_MARK_0); 301 302 rcu_read_lock(); 303 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) { 304 count++; 305 XA_BUG_ON(xa, entry != xa_mk_index(0x40)); 306 } 307 XA_BUG_ON(xa, count != 1); 308 rcu_read_unlock(); 309 xa_destroy(xa); 310 #endif 311 } 312 313 static noinline void check_xa_mark(struct xarray *xa) 314 { 315 unsigned long index; 316 317 for (index = 0; index < 16384; index += 4) 318 check_xa_mark_1(xa, index); 319 320 check_xa_mark_2(xa); 321 check_xa_mark_3(xa); 322 } 323 324 static noinline void check_xa_shrink(struct xarray *xa) 325 { 326 XA_STATE(xas, xa, 1); 327 struct xa_node *node; 328 unsigned int order; 329 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1; 330 331 XA_BUG_ON(xa, !xa_empty(xa)); 332 XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL); 333 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); 334 335 /* 336 * Check that erasing the entry at 1 shrinks the tree and properly 337 * marks the node as being deleted. 338 */ 339 xas_lock(&xas); 340 XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1)); 341 node = xas.xa_node; 342 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0)); 343 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1)); 344 XA_BUG_ON(xa, xa_load(xa, 1) != NULL); 345 XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS); 346 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY); 347 XA_BUG_ON(xa, xas_load(&xas) != NULL); 348 xas_unlock(&xas); 349 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); 350 xa_erase_index(xa, 0); 351 XA_BUG_ON(xa, !xa_empty(xa)); 352 353 for (order = 0; order < max_order; order++) { 354 unsigned long max = (1UL << order) - 1; 355 xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL); 356 XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0)); 357 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL); 358 rcu_read_lock(); 359 node = xa_head(xa); 360 rcu_read_unlock(); 361 XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) != 362 NULL); 363 rcu_read_lock(); 364 XA_BUG_ON(xa, xa_head(xa) == node); 365 rcu_read_unlock(); 366 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL); 367 xa_erase_index(xa, ULONG_MAX); 368 XA_BUG_ON(xa, xa->xa_head != node); 369 xa_erase_index(xa, 0); 370 } 371 } 372 373 static noinline void check_insert(struct xarray *xa) 374 { 375 unsigned long i; 376 377 for (i = 0; i < 1024; i++) { 378 xa_insert_index(xa, i); 379 XA_BUG_ON(xa, xa_load(xa, i - 1) != NULL); 380 XA_BUG_ON(xa, xa_load(xa, i + 1) != NULL); 381 xa_erase_index(xa, i); 382 } 383 384 for (i = 10; i < BITS_PER_LONG; i++) { 385 xa_insert_index(xa, 1UL << i); 386 XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 1) != NULL); 387 XA_BUG_ON(xa, xa_load(xa, (1UL << i) + 1) != NULL); 388 xa_erase_index(xa, 1UL << i); 389 390 xa_insert_index(xa, (1UL << i) - 1); 391 XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 2) != NULL); 392 XA_BUG_ON(xa, xa_load(xa, 1UL << i) != NULL); 393 xa_erase_index(xa, (1UL << i) - 1); 394 } 395 396 xa_insert_index(xa, ~0UL); 397 XA_BUG_ON(xa, xa_load(xa, 0UL) != NULL); 398 XA_BUG_ON(xa, xa_load(xa, ~1UL) != NULL); 399 xa_erase_index(xa, ~0UL); 400 401 XA_BUG_ON(xa, !xa_empty(xa)); 402 } 403 404 static noinline void check_cmpxchg(struct xarray *xa) 405 { 406 void *FIVE = xa_mk_value(5); 407 void *SIX = xa_mk_value(6); 408 void *LOTS = xa_mk_value(12345678); 409 410 XA_BUG_ON(xa, !xa_empty(xa)); 411 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL); 412 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY); 413 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS); 414 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS); 415 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE); 416 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL); 417 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL); 418 XA_BUG_ON(xa, xa_insert(xa, 5, FIVE, GFP_KERNEL) != -EBUSY); 419 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != FIVE); 420 XA_BUG_ON(xa, xa_insert(xa, 5, FIVE, GFP_KERNEL) == -EBUSY); 421 xa_erase_index(xa, 12345678); 422 xa_erase_index(xa, 5); 423 XA_BUG_ON(xa, !xa_empty(xa)); 424 } 425 426 static noinline void check_reserve(struct xarray *xa) 427 { 428 void *entry; 429 unsigned long index; 430 int count; 431 432 /* An array with a reserved entry is not empty */ 433 XA_BUG_ON(xa, !xa_empty(xa)); 434 XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); 435 XA_BUG_ON(xa, xa_empty(xa)); 436 XA_BUG_ON(xa, xa_load(xa, 12345678)); 437 xa_release(xa, 12345678); 438 XA_BUG_ON(xa, !xa_empty(xa)); 439 440 /* Releasing a used entry does nothing */ 441 XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); 442 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL); 443 xa_release(xa, 12345678); 444 xa_erase_index(xa, 12345678); 445 XA_BUG_ON(xa, !xa_empty(xa)); 446 447 /* cmpxchg sees a reserved entry as ZERO */ 448 XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); 449 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, XA_ZERO_ENTRY, 450 xa_mk_value(12345678), GFP_NOWAIT) != NULL); 451 xa_release(xa, 12345678); 452 xa_erase_index(xa, 12345678); 453 XA_BUG_ON(xa, !xa_empty(xa)); 454 455 /* xa_insert treats it as busy */ 456 XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); 457 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) != 458 -EBUSY); 459 XA_BUG_ON(xa, xa_empty(xa)); 460 XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL); 461 XA_BUG_ON(xa, !xa_empty(xa)); 462 463 /* Can iterate through a reserved entry */ 464 xa_store_index(xa, 5, GFP_KERNEL); 465 XA_BUG_ON(xa, xa_reserve(xa, 6, GFP_KERNEL) != 0); 466 xa_store_index(xa, 7, GFP_KERNEL); 467 468 count = 0; 469 xa_for_each(xa, index, entry) { 470 XA_BUG_ON(xa, index != 5 && index != 7); 471 count++; 472 } 473 XA_BUG_ON(xa, count != 2); 474 475 /* If we free a reserved entry, we should be able to allocate it */ 476 if (xa->xa_flags & XA_FLAGS_ALLOC) { 477 u32 id; 478 479 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(8), 480 XA_LIMIT(5, 10), GFP_KERNEL) != 0); 481 XA_BUG_ON(xa, id != 8); 482 483 xa_release(xa, 6); 484 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(6), 485 XA_LIMIT(5, 10), GFP_KERNEL) != 0); 486 XA_BUG_ON(xa, id != 6); 487 } 488 489 xa_destroy(xa); 490 } 491 492 static noinline void check_xas_erase(struct xarray *xa) 493 { 494 XA_STATE(xas, xa, 0); 495 void *entry; 496 unsigned long i, j; 497 498 for (i = 0; i < 200; i++) { 499 for (j = i; j < 2 * i + 17; j++) { 500 xas_set(&xas, j); 501 do { 502 xas_lock(&xas); 503 xas_store(&xas, xa_mk_index(j)); 504 xas_unlock(&xas); 505 } while (xas_nomem(&xas, GFP_KERNEL)); 506 } 507 508 xas_set(&xas, ULONG_MAX); 509 do { 510 xas_lock(&xas); 511 xas_store(&xas, xa_mk_value(0)); 512 xas_unlock(&xas); 513 } while (xas_nomem(&xas, GFP_KERNEL)); 514 515 xas_lock(&xas); 516 xas_store(&xas, NULL); 517 518 xas_set(&xas, 0); 519 j = i; 520 xas_for_each(&xas, entry, ULONG_MAX) { 521 XA_BUG_ON(xa, entry != xa_mk_index(j)); 522 xas_store(&xas, NULL); 523 j++; 524 } 525 xas_unlock(&xas); 526 XA_BUG_ON(xa, !xa_empty(xa)); 527 } 528 } 529 530 #ifdef CONFIG_XARRAY_MULTI 531 static noinline void check_multi_store_1(struct xarray *xa, unsigned long index, 532 unsigned int order) 533 { 534 XA_STATE(xas, xa, index); 535 unsigned long min = index & ~((1UL << order) - 1); 536 unsigned long max = min + (1UL << order); 537 538 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL); 539 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(index)); 540 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(index)); 541 XA_BUG_ON(xa, xa_load(xa, max) != NULL); 542 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL); 543 544 xas_lock(&xas); 545 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index)); 546 xas_unlock(&xas); 547 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(min)); 548 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(min)); 549 XA_BUG_ON(xa, xa_load(xa, max) != NULL); 550 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL); 551 552 xa_erase_index(xa, min); 553 XA_BUG_ON(xa, !xa_empty(xa)); 554 } 555 556 static noinline void check_multi_store_2(struct xarray *xa, unsigned long index, 557 unsigned int order) 558 { 559 XA_STATE(xas, xa, index); 560 xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL); 561 562 xas_lock(&xas); 563 XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0)); 564 XA_BUG_ON(xa, xas.xa_index != index); 565 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1)); 566 xas_unlock(&xas); 567 XA_BUG_ON(xa, !xa_empty(xa)); 568 } 569 570 static noinline void check_multi_store_3(struct xarray *xa, unsigned long index, 571 unsigned int order) 572 { 573 XA_STATE(xas, xa, 0); 574 void *entry; 575 int n = 0; 576 577 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL); 578 579 xas_lock(&xas); 580 xas_for_each(&xas, entry, ULONG_MAX) { 581 XA_BUG_ON(xa, entry != xa_mk_index(index)); 582 n++; 583 } 584 XA_BUG_ON(xa, n != 1); 585 xas_set(&xas, index + 1); 586 xas_for_each(&xas, entry, ULONG_MAX) { 587 XA_BUG_ON(xa, entry != xa_mk_index(index)); 588 n++; 589 } 590 XA_BUG_ON(xa, n != 2); 591 xas_unlock(&xas); 592 593 xa_destroy(xa); 594 } 595 #endif 596 597 static noinline void check_multi_store(struct xarray *xa) 598 { 599 #ifdef CONFIG_XARRAY_MULTI 600 unsigned long i, j, k; 601 unsigned int max_order = (sizeof(long) == 4) ? 30 : 60; 602 603 /* Loading from any position returns the same value */ 604 xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL); 605 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); 606 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); 607 XA_BUG_ON(xa, xa_load(xa, 2) != NULL); 608 rcu_read_lock(); 609 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2); 610 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); 611 rcu_read_unlock(); 612 613 /* Storing adjacent to the value does not alter the value */ 614 xa_store(xa, 3, xa, GFP_KERNEL); 615 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); 616 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); 617 XA_BUG_ON(xa, xa_load(xa, 2) != NULL); 618 rcu_read_lock(); 619 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3); 620 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); 621 rcu_read_unlock(); 622 623 /* Overwriting multiple indexes works */ 624 xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL); 625 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1)); 626 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1)); 627 XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1)); 628 XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1)); 629 XA_BUG_ON(xa, xa_load(xa, 4) != NULL); 630 rcu_read_lock(); 631 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4); 632 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4); 633 rcu_read_unlock(); 634 635 /* We can erase multiple values with a single store */ 636 xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL); 637 XA_BUG_ON(xa, !xa_empty(xa)); 638 639 /* Even when the first slot is empty but the others aren't */ 640 xa_store_index(xa, 1, GFP_KERNEL); 641 xa_store_index(xa, 2, GFP_KERNEL); 642 xa_store_order(xa, 0, 2, NULL, GFP_KERNEL); 643 XA_BUG_ON(xa, !xa_empty(xa)); 644 645 for (i = 0; i < max_order; i++) { 646 for (j = 0; j < max_order; j++) { 647 xa_store_order(xa, 0, i, xa_mk_index(i), GFP_KERNEL); 648 xa_store_order(xa, 0, j, xa_mk_index(j), GFP_KERNEL); 649 650 for (k = 0; k < max_order; k++) { 651 void *entry = xa_load(xa, (1UL << k) - 1); 652 if ((i < k) && (j < k)) 653 XA_BUG_ON(xa, entry != NULL); 654 else 655 XA_BUG_ON(xa, entry != xa_mk_index(j)); 656 } 657 658 xa_erase(xa, 0); 659 XA_BUG_ON(xa, !xa_empty(xa)); 660 } 661 } 662 663 for (i = 0; i < 20; i++) { 664 check_multi_store_1(xa, 200, i); 665 check_multi_store_1(xa, 0, i); 666 check_multi_store_1(xa, (1UL << i) + 1, i); 667 } 668 check_multi_store_2(xa, 4095, 9); 669 670 for (i = 1; i < 20; i++) { 671 check_multi_store_3(xa, 0, i); 672 check_multi_store_3(xa, 1UL << i, i); 673 } 674 #endif 675 } 676 677 #ifdef CONFIG_XARRAY_MULTI 678 /* mimics page cache __filemap_add_folio() */ 679 static noinline void check_xa_multi_store_adv_add(struct xarray *xa, 680 unsigned long index, 681 unsigned int order, 682 void *p) 683 { 684 XA_STATE(xas, xa, index); 685 unsigned int nrpages = 1UL << order; 686 687 /* users are responsible for index alignemnt to the order when adding */ 688 XA_BUG_ON(xa, index & (nrpages - 1)); 689 690 xas_set_order(&xas, index, order); 691 692 do { 693 xas_lock_irq(&xas); 694 695 xas_store(&xas, p); 696 XA_BUG_ON(xa, xas_error(&xas)); 697 XA_BUG_ON(xa, xa_load(xa, index) != p); 698 699 xas_unlock_irq(&xas); 700 } while (xas_nomem(&xas, GFP_KERNEL)); 701 702 XA_BUG_ON(xa, xas_error(&xas)); 703 } 704 705 /* mimics page_cache_delete() */ 706 static noinline void check_xa_multi_store_adv_del_entry(struct xarray *xa, 707 unsigned long index, 708 unsigned int order) 709 { 710 XA_STATE(xas, xa, index); 711 712 xas_set_order(&xas, index, order); 713 xas_store(&xas, NULL); 714 xas_init_marks(&xas); 715 } 716 717 static noinline void check_xa_multi_store_adv_delete(struct xarray *xa, 718 unsigned long index, 719 unsigned int order) 720 { 721 xa_lock_irq(xa); 722 check_xa_multi_store_adv_del_entry(xa, index, order); 723 xa_unlock_irq(xa); 724 } 725 726 /* mimics page cache filemap_get_entry() */ 727 static noinline void *test_get_entry(struct xarray *xa, unsigned long index) 728 { 729 XA_STATE(xas, xa, index); 730 void *p; 731 static unsigned int loops = 0; 732 733 rcu_read_lock(); 734 repeat: 735 xas_reset(&xas); 736 p = xas_load(&xas); 737 if (xas_retry(&xas, p)) 738 goto repeat; 739 rcu_read_unlock(); 740 741 /* 742 * This is not part of the page cache, this selftest is pretty 743 * aggressive and does not want to trust the xarray API but rather 744 * test it, and for order 20 (4 GiB block size) we can loop over 745 * over a million entries which can cause a soft lockup. Page cache 746 * APIs won't be stupid, proper page cache APIs loop over the proper 747 * order so when using a larger order we skip shared entries. 748 */ 749 if (++loops % XA_CHECK_SCHED == 0) 750 schedule(); 751 752 return p; 753 } 754 755 static unsigned long some_val = 0xdeadbeef; 756 static unsigned long some_val_2 = 0xdeaddead; 757 758 /* mimics the page cache usage */ 759 static noinline void check_xa_multi_store_adv(struct xarray *xa, 760 unsigned long pos, 761 unsigned int order) 762 { 763 unsigned int nrpages = 1UL << order; 764 unsigned long index, base, next_index, next_next_index; 765 unsigned int i; 766 767 index = pos >> PAGE_SHIFT; 768 base = round_down(index, nrpages); 769 next_index = round_down(base + nrpages, nrpages); 770 next_next_index = round_down(next_index + nrpages, nrpages); 771 772 check_xa_multi_store_adv_add(xa, base, order, &some_val); 773 774 for (i = 0; i < nrpages; i++) 775 XA_BUG_ON(xa, test_get_entry(xa, base + i) != &some_val); 776 777 XA_BUG_ON(xa, test_get_entry(xa, next_index) != NULL); 778 779 /* Use order 0 for the next item */ 780 check_xa_multi_store_adv_add(xa, next_index, 0, &some_val_2); 781 XA_BUG_ON(xa, test_get_entry(xa, next_index) != &some_val_2); 782 783 /* Remove the next item */ 784 check_xa_multi_store_adv_delete(xa, next_index, 0); 785 786 /* Now use order for a new pointer */ 787 check_xa_multi_store_adv_add(xa, next_index, order, &some_val_2); 788 789 for (i = 0; i < nrpages; i++) 790 XA_BUG_ON(xa, test_get_entry(xa, next_index + i) != &some_val_2); 791 792 check_xa_multi_store_adv_delete(xa, next_index, order); 793 check_xa_multi_store_adv_delete(xa, base, order); 794 XA_BUG_ON(xa, !xa_empty(xa)); 795 796 /* starting fresh again */ 797 798 /* let's test some holes now */ 799 800 /* hole at base and next_next */ 801 check_xa_multi_store_adv_add(xa, next_index, order, &some_val_2); 802 803 for (i = 0; i < nrpages; i++) 804 XA_BUG_ON(xa, test_get_entry(xa, base + i) != NULL); 805 806 for (i = 0; i < nrpages; i++) 807 XA_BUG_ON(xa, test_get_entry(xa, next_index + i) != &some_val_2); 808 809 for (i = 0; i < nrpages; i++) 810 XA_BUG_ON(xa, test_get_entry(xa, next_next_index + i) != NULL); 811 812 check_xa_multi_store_adv_delete(xa, next_index, order); 813 XA_BUG_ON(xa, !xa_empty(xa)); 814 815 /* hole at base and next */ 816 817 check_xa_multi_store_adv_add(xa, next_next_index, order, &some_val_2); 818 819 for (i = 0; i < nrpages; i++) 820 XA_BUG_ON(xa, test_get_entry(xa, base + i) != NULL); 821 822 for (i = 0; i < nrpages; i++) 823 XA_BUG_ON(xa, test_get_entry(xa, next_index + i) != NULL); 824 825 for (i = 0; i < nrpages; i++) 826 XA_BUG_ON(xa, test_get_entry(xa, next_next_index + i) != &some_val_2); 827 828 check_xa_multi_store_adv_delete(xa, next_next_index, order); 829 XA_BUG_ON(xa, !xa_empty(xa)); 830 } 831 #endif 832 833 static noinline void check_multi_store_advanced(struct xarray *xa) 834 { 835 #ifdef CONFIG_XARRAY_MULTI 836 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; 837 unsigned long end = ULONG_MAX/2; 838 unsigned long pos, i; 839 840 /* 841 * About 117 million tests below. 842 */ 843 for (pos = 7; pos < end; pos = (pos * pos) + 564) { 844 for (i = 0; i < max_order; i++) { 845 check_xa_multi_store_adv(xa, pos, i); 846 check_xa_multi_store_adv(xa, pos + 157, i); 847 } 848 } 849 #endif 850 } 851 852 static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base) 853 { 854 int i; 855 u32 id; 856 857 XA_BUG_ON(xa, !xa_empty(xa)); 858 /* An empty array should assign %base to the first alloc */ 859 xa_alloc_index(xa, base, GFP_KERNEL); 860 861 /* Erasing it should make the array empty again */ 862 xa_erase_index(xa, base); 863 XA_BUG_ON(xa, !xa_empty(xa)); 864 865 /* And it should assign %base again */ 866 xa_alloc_index(xa, base, GFP_KERNEL); 867 868 /* Allocating and then erasing a lot should not lose base */ 869 for (i = base + 1; i < 2 * XA_CHUNK_SIZE; i++) 870 xa_alloc_index(xa, i, GFP_KERNEL); 871 for (i = base; i < 2 * XA_CHUNK_SIZE; i++) 872 xa_erase_index(xa, i); 873 xa_alloc_index(xa, base, GFP_KERNEL); 874 875 /* Destroying the array should do the same as erasing */ 876 xa_destroy(xa); 877 878 /* And it should assign %base again */ 879 xa_alloc_index(xa, base, GFP_KERNEL); 880 881 /* The next assigned ID should be base+1 */ 882 xa_alloc_index(xa, base + 1, GFP_KERNEL); 883 xa_erase_index(xa, base + 1); 884 885 /* Storing a value should mark it used */ 886 xa_store_index(xa, base + 1, GFP_KERNEL); 887 xa_alloc_index(xa, base + 2, GFP_KERNEL); 888 889 /* If we then erase base, it should be free */ 890 xa_erase_index(xa, base); 891 xa_alloc_index(xa, base, GFP_KERNEL); 892 893 xa_erase_index(xa, base + 1); 894 xa_erase_index(xa, base + 2); 895 896 for (i = 1; i < 5000; i++) { 897 xa_alloc_index(xa, base + i, GFP_KERNEL); 898 } 899 900 xa_destroy(xa); 901 902 /* Check that we fail properly at the limit of allocation */ 903 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX - 1), 904 XA_LIMIT(UINT_MAX - 1, UINT_MAX), 905 GFP_KERNEL) != 0); 906 XA_BUG_ON(xa, id != 0xfffffffeU); 907 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX), 908 XA_LIMIT(UINT_MAX - 1, UINT_MAX), 909 GFP_KERNEL) != 0); 910 XA_BUG_ON(xa, id != 0xffffffffU); 911 id = 3; 912 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(0), 913 XA_LIMIT(UINT_MAX - 1, UINT_MAX), 914 GFP_KERNEL) != -EBUSY); 915 XA_BUG_ON(xa, id != 3); 916 xa_destroy(xa); 917 918 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5), 919 GFP_KERNEL) != -EBUSY); 920 XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0); 921 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5), 922 GFP_KERNEL) != -EBUSY); 923 xa_erase_index(xa, 3); 924 XA_BUG_ON(xa, !xa_empty(xa)); 925 } 926 927 static noinline void check_xa_alloc_2(struct xarray *xa, unsigned int base) 928 { 929 unsigned int i, id; 930 unsigned long index; 931 void *entry; 932 933 /* Allocate and free a NULL and check xa_empty() behaves */ 934 XA_BUG_ON(xa, !xa_empty(xa)); 935 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); 936 XA_BUG_ON(xa, id != base); 937 XA_BUG_ON(xa, xa_empty(xa)); 938 XA_BUG_ON(xa, xa_erase(xa, id) != NULL); 939 XA_BUG_ON(xa, !xa_empty(xa)); 940 941 /* Ditto, but check destroy instead of erase */ 942 XA_BUG_ON(xa, !xa_empty(xa)); 943 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); 944 XA_BUG_ON(xa, id != base); 945 XA_BUG_ON(xa, xa_empty(xa)); 946 xa_destroy(xa); 947 XA_BUG_ON(xa, !xa_empty(xa)); 948 949 for (i = base; i < base + 10; i++) { 950 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, 951 GFP_KERNEL) != 0); 952 XA_BUG_ON(xa, id != i); 953 } 954 955 XA_BUG_ON(xa, xa_store(xa, 3, xa_mk_index(3), GFP_KERNEL) != NULL); 956 XA_BUG_ON(xa, xa_store(xa, 4, xa_mk_index(4), GFP_KERNEL) != NULL); 957 XA_BUG_ON(xa, xa_store(xa, 4, NULL, GFP_KERNEL) != xa_mk_index(4)); 958 XA_BUG_ON(xa, xa_erase(xa, 5) != NULL); 959 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); 960 XA_BUG_ON(xa, id != 5); 961 962 xa_for_each(xa, index, entry) { 963 xa_erase_index(xa, index); 964 } 965 966 for (i = base; i < base + 9; i++) { 967 XA_BUG_ON(xa, xa_erase(xa, i) != NULL); 968 XA_BUG_ON(xa, xa_empty(xa)); 969 } 970 XA_BUG_ON(xa, xa_erase(xa, 8) != NULL); 971 XA_BUG_ON(xa, xa_empty(xa)); 972 XA_BUG_ON(xa, xa_erase(xa, base + 9) != NULL); 973 XA_BUG_ON(xa, !xa_empty(xa)); 974 975 xa_destroy(xa); 976 } 977 978 static noinline void check_xa_alloc_3(struct xarray *xa, unsigned int base) 979 { 980 struct xa_limit limit = XA_LIMIT(1, 0x3fff); 981 u32 next = 0; 982 unsigned int i, id; 983 unsigned long index; 984 void *entry; 985 986 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(1), limit, 987 &next, GFP_KERNEL) != 0); 988 XA_BUG_ON(xa, id != 1); 989 990 next = 0x3ffd; 991 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(0x3ffd), limit, 992 &next, GFP_KERNEL) != 0); 993 XA_BUG_ON(xa, id != 0x3ffd); 994 xa_erase_index(xa, 0x3ffd); 995 xa_erase_index(xa, 1); 996 XA_BUG_ON(xa, !xa_empty(xa)); 997 998 for (i = 0x3ffe; i < 0x4003; i++) { 999 if (i < 0x4000) 1000 entry = xa_mk_index(i); 1001 else 1002 entry = xa_mk_index(i - 0x3fff); 1003 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, entry, limit, 1004 &next, GFP_KERNEL) != (id == 1)); 1005 XA_BUG_ON(xa, xa_mk_index(id) != entry); 1006 } 1007 1008 /* Check wrap-around is handled correctly */ 1009 if (base != 0) 1010 xa_erase_index(xa, base); 1011 xa_erase_index(xa, base + 1); 1012 next = UINT_MAX; 1013 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(UINT_MAX), 1014 xa_limit_32b, &next, GFP_KERNEL) != 0); 1015 XA_BUG_ON(xa, id != UINT_MAX); 1016 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base), 1017 xa_limit_32b, &next, GFP_KERNEL) != 1); 1018 XA_BUG_ON(xa, id != base); 1019 XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base + 1), 1020 xa_limit_32b, &next, GFP_KERNEL) != 0); 1021 XA_BUG_ON(xa, id != base + 1); 1022 1023 xa_for_each(xa, index, entry) 1024 xa_erase_index(xa, index); 1025 1026 XA_BUG_ON(xa, !xa_empty(xa)); 1027 } 1028 1029 static DEFINE_XARRAY_ALLOC(xa0); 1030 static DEFINE_XARRAY_ALLOC1(xa1); 1031 1032 static noinline void check_xa_alloc(void) 1033 { 1034 check_xa_alloc_1(&xa0, 0); 1035 check_xa_alloc_1(&xa1, 1); 1036 check_xa_alloc_2(&xa0, 0); 1037 check_xa_alloc_2(&xa1, 1); 1038 check_xa_alloc_3(&xa0, 0); 1039 check_xa_alloc_3(&xa1, 1); 1040 } 1041 1042 static noinline void __check_store_iter(struct xarray *xa, unsigned long start, 1043 unsigned int order, unsigned int present) 1044 { 1045 XA_STATE_ORDER(xas, xa, start, order); 1046 void *entry; 1047 unsigned int count = 0; 1048 1049 retry: 1050 xas_lock(&xas); 1051 xas_for_each_conflict(&xas, entry) { 1052 XA_BUG_ON(xa, !xa_is_value(entry)); 1053 XA_BUG_ON(xa, entry < xa_mk_index(start)); 1054 XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1)); 1055 count++; 1056 } 1057 xas_store(&xas, xa_mk_index(start)); 1058 xas_unlock(&xas); 1059 if (xas_nomem(&xas, GFP_KERNEL)) { 1060 count = 0; 1061 goto retry; 1062 } 1063 XA_BUG_ON(xa, xas_error(&xas)); 1064 XA_BUG_ON(xa, count != present); 1065 XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start)); 1066 XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) != 1067 xa_mk_index(start)); 1068 xa_erase_index(xa, start); 1069 } 1070 1071 static noinline void check_store_iter(struct xarray *xa) 1072 { 1073 unsigned int i, j; 1074 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; 1075 1076 for (i = 0; i < max_order; i++) { 1077 unsigned int min = 1 << i; 1078 unsigned int max = (2 << i) - 1; 1079 __check_store_iter(xa, 0, i, 0); 1080 XA_BUG_ON(xa, !xa_empty(xa)); 1081 __check_store_iter(xa, min, i, 0); 1082 XA_BUG_ON(xa, !xa_empty(xa)); 1083 1084 xa_store_index(xa, min, GFP_KERNEL); 1085 __check_store_iter(xa, min, i, 1); 1086 XA_BUG_ON(xa, !xa_empty(xa)); 1087 xa_store_index(xa, max, GFP_KERNEL); 1088 __check_store_iter(xa, min, i, 1); 1089 XA_BUG_ON(xa, !xa_empty(xa)); 1090 1091 for (j = 0; j < min; j++) 1092 xa_store_index(xa, j, GFP_KERNEL); 1093 __check_store_iter(xa, 0, i, min); 1094 XA_BUG_ON(xa, !xa_empty(xa)); 1095 for (j = 0; j < min; j++) 1096 xa_store_index(xa, min + j, GFP_KERNEL); 1097 __check_store_iter(xa, min, i, min); 1098 XA_BUG_ON(xa, !xa_empty(xa)); 1099 } 1100 #ifdef CONFIG_XARRAY_MULTI 1101 xa_store_index(xa, 63, GFP_KERNEL); 1102 xa_store_index(xa, 65, GFP_KERNEL); 1103 __check_store_iter(xa, 64, 2, 1); 1104 xa_erase_index(xa, 63); 1105 #endif 1106 XA_BUG_ON(xa, !xa_empty(xa)); 1107 } 1108 1109 static noinline void check_multi_find_1(struct xarray *xa, unsigned order) 1110 { 1111 #ifdef CONFIG_XARRAY_MULTI 1112 unsigned long multi = 3 << order; 1113 unsigned long next = 4 << order; 1114 unsigned long index; 1115 1116 xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL); 1117 XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL); 1118 XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL); 1119 1120 index = 0; 1121 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != 1122 xa_mk_value(multi)); 1123 XA_BUG_ON(xa, index != multi); 1124 index = multi + 1; 1125 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != 1126 xa_mk_value(multi)); 1127 XA_BUG_ON(xa, (index < multi) || (index >= next)); 1128 XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) != 1129 xa_mk_value(next)); 1130 XA_BUG_ON(xa, index != next); 1131 XA_BUG_ON(xa, xa_find_after(xa, &index, next, XA_PRESENT) != NULL); 1132 XA_BUG_ON(xa, index != next); 1133 1134 xa_erase_index(xa, multi); 1135 xa_erase_index(xa, next); 1136 xa_erase_index(xa, next + 1); 1137 XA_BUG_ON(xa, !xa_empty(xa)); 1138 #endif 1139 } 1140 1141 static noinline void check_multi_find_2(struct xarray *xa) 1142 { 1143 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1; 1144 unsigned int i, j; 1145 void *entry; 1146 1147 for (i = 0; i < max_order; i++) { 1148 unsigned long index = 1UL << i; 1149 for (j = 0; j < index; j++) { 1150 XA_STATE(xas, xa, j + index); 1151 xa_store_index(xa, index - 1, GFP_KERNEL); 1152 xa_store_order(xa, index, i, xa_mk_index(index), 1153 GFP_KERNEL); 1154 rcu_read_lock(); 1155 xas_for_each(&xas, entry, ULONG_MAX) { 1156 xa_erase_index(xa, index); 1157 } 1158 rcu_read_unlock(); 1159 xa_erase_index(xa, index - 1); 1160 XA_BUG_ON(xa, !xa_empty(xa)); 1161 } 1162 } 1163 } 1164 1165 static noinline void check_multi_find_3(struct xarray *xa) 1166 { 1167 unsigned int order; 1168 1169 for (order = 5; order < order_limit; order++) { 1170 unsigned long index = 1UL << (order - 5); 1171 1172 XA_BUG_ON(xa, !xa_empty(xa)); 1173 xa_store_order(xa, 0, order - 4, xa_mk_index(0), GFP_KERNEL); 1174 XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT)); 1175 xa_erase_index(xa, 0); 1176 } 1177 } 1178 1179 static noinline void check_find_1(struct xarray *xa) 1180 { 1181 unsigned long i, j, k; 1182 1183 XA_BUG_ON(xa, !xa_empty(xa)); 1184 1185 /* 1186 * Check xa_find with all pairs between 0 and 99 inclusive, 1187 * starting at every index between 0 and 99 1188 */ 1189 for (i = 0; i < 100; i++) { 1190 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); 1191 xa_set_mark(xa, i, XA_MARK_0); 1192 for (j = 0; j < i; j++) { 1193 XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) != 1194 NULL); 1195 xa_set_mark(xa, j, XA_MARK_0); 1196 for (k = 0; k < 100; k++) { 1197 unsigned long index = k; 1198 void *entry = xa_find(xa, &index, ULONG_MAX, 1199 XA_PRESENT); 1200 if (k <= j) 1201 XA_BUG_ON(xa, index != j); 1202 else if (k <= i) 1203 XA_BUG_ON(xa, index != i); 1204 else 1205 XA_BUG_ON(xa, entry != NULL); 1206 1207 index = k; 1208 entry = xa_find(xa, &index, ULONG_MAX, 1209 XA_MARK_0); 1210 if (k <= j) 1211 XA_BUG_ON(xa, index != j); 1212 else if (k <= i) 1213 XA_BUG_ON(xa, index != i); 1214 else 1215 XA_BUG_ON(xa, entry != NULL); 1216 } 1217 xa_erase_index(xa, j); 1218 XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0)); 1219 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); 1220 } 1221 xa_erase_index(xa, i); 1222 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0)); 1223 } 1224 XA_BUG_ON(xa, !xa_empty(xa)); 1225 } 1226 1227 static noinline void check_find_2(struct xarray *xa) 1228 { 1229 void *entry; 1230 unsigned long i, j, index; 1231 1232 xa_for_each(xa, index, entry) { 1233 XA_BUG_ON(xa, true); 1234 } 1235 1236 for (i = 0; i < 1024; i++) { 1237 xa_store_index(xa, index, GFP_KERNEL); 1238 j = 0; 1239 xa_for_each(xa, index, entry) { 1240 XA_BUG_ON(xa, xa_mk_index(index) != entry); 1241 XA_BUG_ON(xa, index != j++); 1242 } 1243 } 1244 1245 xa_destroy(xa); 1246 } 1247 1248 static noinline void check_find_3(struct xarray *xa) 1249 { 1250 XA_STATE(xas, xa, 0); 1251 unsigned long i, j, k; 1252 void *entry; 1253 1254 for (i = 0; i < 100; i++) { 1255 for (j = 0; j < 100; j++) { 1256 rcu_read_lock(); 1257 for (k = 0; k < 100; k++) { 1258 xas_set(&xas, j); 1259 xas_for_each_marked(&xas, entry, k, XA_MARK_0) 1260 ; 1261 if (j > k) 1262 XA_BUG_ON(xa, 1263 xas.xa_node != XAS_RESTART); 1264 } 1265 rcu_read_unlock(); 1266 } 1267 xa_store_index(xa, i, GFP_KERNEL); 1268 xa_set_mark(xa, i, XA_MARK_0); 1269 } 1270 xa_destroy(xa); 1271 } 1272 1273 static noinline void check_find_4(struct xarray *xa) 1274 { 1275 unsigned long index = 0; 1276 void *entry; 1277 1278 xa_store_index(xa, ULONG_MAX, GFP_KERNEL); 1279 1280 entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT); 1281 XA_BUG_ON(xa, entry != xa_mk_index(ULONG_MAX)); 1282 1283 entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT); 1284 XA_BUG_ON(xa, entry); 1285 1286 xa_erase_index(xa, ULONG_MAX); 1287 } 1288 1289 static noinline void check_find(struct xarray *xa) 1290 { 1291 unsigned i; 1292 1293 check_find_1(xa); 1294 check_find_2(xa); 1295 check_find_3(xa); 1296 check_find_4(xa); 1297 1298 for (i = 2; i < 10; i++) 1299 check_multi_find_1(xa, i); 1300 check_multi_find_2(xa); 1301 check_multi_find_3(xa); 1302 } 1303 1304 /* See find_swap_entry() in mm/shmem.c */ 1305 static noinline unsigned long xa_find_entry(struct xarray *xa, void *item) 1306 { 1307 XA_STATE(xas, xa, 0); 1308 unsigned int checked = 0; 1309 void *entry; 1310 1311 rcu_read_lock(); 1312 xas_for_each(&xas, entry, ULONG_MAX) { 1313 if (xas_retry(&xas, entry)) 1314 continue; 1315 if (entry == item) 1316 break; 1317 checked++; 1318 if ((checked % 4) != 0) 1319 continue; 1320 xas_pause(&xas); 1321 } 1322 rcu_read_unlock(); 1323 1324 return entry ? xas.xa_index : -1; 1325 } 1326 1327 static noinline void check_find_entry(struct xarray *xa) 1328 { 1329 #ifdef CONFIG_XARRAY_MULTI 1330 unsigned int order; 1331 unsigned long offset, index; 1332 1333 for (order = 0; order < 20; order++) { 1334 for (offset = 0; offset < (1UL << (order + 3)); 1335 offset += (1UL << order)) { 1336 for (index = 0; index < (1UL << (order + 5)); 1337 index += (1UL << order)) { 1338 xa_store_order(xa, index, order, 1339 xa_mk_index(index), GFP_KERNEL); 1340 XA_BUG_ON(xa, xa_load(xa, index) != 1341 xa_mk_index(index)); 1342 XA_BUG_ON(xa, xa_find_entry(xa, 1343 xa_mk_index(index)) != index); 1344 } 1345 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); 1346 xa_destroy(xa); 1347 } 1348 } 1349 #endif 1350 1351 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); 1352 xa_store_index(xa, ULONG_MAX, GFP_KERNEL); 1353 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); 1354 XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1); 1355 xa_erase_index(xa, ULONG_MAX); 1356 XA_BUG_ON(xa, !xa_empty(xa)); 1357 } 1358 1359 static noinline void check_pause(struct xarray *xa) 1360 { 1361 XA_STATE(xas, xa, 0); 1362 void *entry; 1363 unsigned int order; 1364 unsigned long index = 1; 1365 unsigned int count = 0; 1366 1367 for (order = 0; order < order_limit; order++) { 1368 XA_BUG_ON(xa, xa_store_order(xa, index, order, 1369 xa_mk_index(index), GFP_KERNEL)); 1370 index += 1UL << order; 1371 } 1372 1373 rcu_read_lock(); 1374 xas_for_each(&xas, entry, ULONG_MAX) { 1375 XA_BUG_ON(xa, entry != xa_mk_index(1UL << count)); 1376 count++; 1377 } 1378 rcu_read_unlock(); 1379 XA_BUG_ON(xa, count != order_limit); 1380 1381 count = 0; 1382 xas_set(&xas, 0); 1383 rcu_read_lock(); 1384 xas_for_each(&xas, entry, ULONG_MAX) { 1385 XA_BUG_ON(xa, entry != xa_mk_index(1UL << count)); 1386 count++; 1387 xas_pause(&xas); 1388 } 1389 rcu_read_unlock(); 1390 XA_BUG_ON(xa, count != order_limit); 1391 1392 xa_destroy(xa); 1393 } 1394 1395 static noinline void check_move_tiny(struct xarray *xa) 1396 { 1397 XA_STATE(xas, xa, 0); 1398 1399 XA_BUG_ON(xa, !xa_empty(xa)); 1400 rcu_read_lock(); 1401 XA_BUG_ON(xa, xas_next(&xas) != NULL); 1402 XA_BUG_ON(xa, xas_next(&xas) != NULL); 1403 rcu_read_unlock(); 1404 xa_store_index(xa, 0, GFP_KERNEL); 1405 rcu_read_lock(); 1406 xas_set(&xas, 0); 1407 XA_BUG_ON(xa, xas_next(&xas) != xa_mk_index(0)); 1408 XA_BUG_ON(xa, xas_next(&xas) != NULL); 1409 xas_set(&xas, 0); 1410 XA_BUG_ON(xa, xas_prev(&xas) != xa_mk_index(0)); 1411 XA_BUG_ON(xa, xas_prev(&xas) != NULL); 1412 rcu_read_unlock(); 1413 xa_erase_index(xa, 0); 1414 XA_BUG_ON(xa, !xa_empty(xa)); 1415 } 1416 1417 static noinline void check_move_max(struct xarray *xa) 1418 { 1419 XA_STATE(xas, xa, 0); 1420 1421 xa_store_index(xa, ULONG_MAX, GFP_KERNEL); 1422 rcu_read_lock(); 1423 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX)); 1424 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL); 1425 rcu_read_unlock(); 1426 1427 xas_set(&xas, 0); 1428 rcu_read_lock(); 1429 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX)); 1430 xas_pause(&xas); 1431 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL); 1432 rcu_read_unlock(); 1433 1434 xa_erase_index(xa, ULONG_MAX); 1435 XA_BUG_ON(xa, !xa_empty(xa)); 1436 } 1437 1438 static noinline void check_move_small(struct xarray *xa, unsigned long idx) 1439 { 1440 XA_STATE(xas, xa, 0); 1441 unsigned long i; 1442 1443 xa_store_index(xa, 0, GFP_KERNEL); 1444 xa_store_index(xa, idx, GFP_KERNEL); 1445 1446 rcu_read_lock(); 1447 for (i = 0; i < idx * 4; i++) { 1448 void *entry = xas_next(&xas); 1449 if (i <= idx) 1450 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); 1451 XA_BUG_ON(xa, xas.xa_index != i); 1452 if (i == 0 || i == idx) 1453 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1454 else 1455 XA_BUG_ON(xa, entry != NULL); 1456 } 1457 xas_next(&xas); 1458 XA_BUG_ON(xa, xas.xa_index != i); 1459 1460 do { 1461 void *entry = xas_prev(&xas); 1462 i--; 1463 if (i <= idx) 1464 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); 1465 XA_BUG_ON(xa, xas.xa_index != i); 1466 if (i == 0 || i == idx) 1467 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1468 else 1469 XA_BUG_ON(xa, entry != NULL); 1470 } while (i > 0); 1471 1472 xas_set(&xas, ULONG_MAX); 1473 XA_BUG_ON(xa, xas_next(&xas) != NULL); 1474 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); 1475 XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0)); 1476 XA_BUG_ON(xa, xas.xa_index != 0); 1477 XA_BUG_ON(xa, xas_prev(&xas) != NULL); 1478 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); 1479 rcu_read_unlock(); 1480 1481 xa_erase_index(xa, 0); 1482 xa_erase_index(xa, idx); 1483 XA_BUG_ON(xa, !xa_empty(xa)); 1484 } 1485 1486 static noinline void check_move(struct xarray *xa) 1487 { 1488 XA_STATE(xas, xa, (1 << 16) - 1); 1489 unsigned long i; 1490 1491 for (i = 0; i < (1 << 16); i++) 1492 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); 1493 1494 rcu_read_lock(); 1495 do { 1496 void *entry = xas_prev(&xas); 1497 i--; 1498 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1499 XA_BUG_ON(xa, i != xas.xa_index); 1500 } while (i != 0); 1501 1502 XA_BUG_ON(xa, xas_prev(&xas) != NULL); 1503 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); 1504 1505 do { 1506 void *entry = xas_next(&xas); 1507 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1508 XA_BUG_ON(xa, i != xas.xa_index); 1509 i++; 1510 } while (i < (1 << 16)); 1511 rcu_read_unlock(); 1512 1513 for (i = (1 << 8); i < (1 << 15); i++) 1514 xa_erase_index(xa, i); 1515 1516 i = xas.xa_index; 1517 1518 rcu_read_lock(); 1519 do { 1520 void *entry = xas_prev(&xas); 1521 i--; 1522 if ((i < (1 << 8)) || (i >= (1 << 15))) 1523 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1524 else 1525 XA_BUG_ON(xa, entry != NULL); 1526 XA_BUG_ON(xa, i != xas.xa_index); 1527 } while (i != 0); 1528 1529 XA_BUG_ON(xa, xas_prev(&xas) != NULL); 1530 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); 1531 1532 do { 1533 void *entry = xas_next(&xas); 1534 if ((i < (1 << 8)) || (i >= (1 << 15))) 1535 XA_BUG_ON(xa, entry != xa_mk_index(i)); 1536 else 1537 XA_BUG_ON(xa, entry != NULL); 1538 XA_BUG_ON(xa, i != xas.xa_index); 1539 i++; 1540 } while (i < (1 << 16)); 1541 rcu_read_unlock(); 1542 1543 xa_destroy(xa); 1544 1545 check_move_tiny(xa); 1546 check_move_max(xa); 1547 1548 for (i = 0; i < 16; i++) 1549 check_move_small(xa, 1UL << i); 1550 1551 for (i = 2; i < 16; i++) 1552 check_move_small(xa, (1UL << i) - 1); 1553 } 1554 1555 static noinline void xa_store_many_order(struct xarray *xa, 1556 unsigned long index, unsigned order) 1557 { 1558 XA_STATE_ORDER(xas, xa, index, order); 1559 unsigned int i = 0; 1560 1561 do { 1562 xas_lock(&xas); 1563 XA_BUG_ON(xa, xas_find_conflict(&xas)); 1564 xas_create_range(&xas); 1565 if (xas_error(&xas)) 1566 goto unlock; 1567 for (i = 0; i < (1U << order); i++) { 1568 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i))); 1569 xas_next(&xas); 1570 } 1571 unlock: 1572 xas_unlock(&xas); 1573 } while (xas_nomem(&xas, GFP_KERNEL)); 1574 1575 XA_BUG_ON(xa, xas_error(&xas)); 1576 } 1577 1578 static noinline void check_create_range_1(struct xarray *xa, 1579 unsigned long index, unsigned order) 1580 { 1581 unsigned long i; 1582 1583 xa_store_many_order(xa, index, order); 1584 for (i = index; i < index + (1UL << order); i++) 1585 xa_erase_index(xa, i); 1586 XA_BUG_ON(xa, !xa_empty(xa)); 1587 } 1588 1589 static noinline void check_create_range_2(struct xarray *xa, unsigned order) 1590 { 1591 unsigned long i; 1592 unsigned long nr = 1UL << order; 1593 1594 for (i = 0; i < nr * nr; i += nr) 1595 xa_store_many_order(xa, i, order); 1596 for (i = 0; i < nr * nr; i++) 1597 xa_erase_index(xa, i); 1598 XA_BUG_ON(xa, !xa_empty(xa)); 1599 } 1600 1601 static noinline void check_create_range_3(void) 1602 { 1603 XA_STATE(xas, NULL, 0); 1604 xas_set_err(&xas, -EEXIST); 1605 xas_create_range(&xas); 1606 XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST); 1607 } 1608 1609 static noinline void check_create_range_4(struct xarray *xa, 1610 unsigned long index, unsigned order) 1611 { 1612 XA_STATE_ORDER(xas, xa, index, order); 1613 unsigned long base = xas.xa_index; 1614 unsigned long i = 0; 1615 1616 xa_store_index(xa, index, GFP_KERNEL); 1617 do { 1618 xas_lock(&xas); 1619 xas_create_range(&xas); 1620 if (xas_error(&xas)) 1621 goto unlock; 1622 for (i = 0; i < (1UL << order); i++) { 1623 void *old = xas_store(&xas, xa_mk_index(base + i)); 1624 if (xas.xa_index == index) 1625 XA_BUG_ON(xa, old != xa_mk_index(base + i)); 1626 else 1627 XA_BUG_ON(xa, old != NULL); 1628 xas_next(&xas); 1629 } 1630 unlock: 1631 xas_unlock(&xas); 1632 } while (xas_nomem(&xas, GFP_KERNEL)); 1633 1634 XA_BUG_ON(xa, xas_error(&xas)); 1635 1636 for (i = base; i < base + (1UL << order); i++) 1637 xa_erase_index(xa, i); 1638 XA_BUG_ON(xa, !xa_empty(xa)); 1639 } 1640 1641 static noinline void check_create_range_5(struct xarray *xa, 1642 unsigned long index, unsigned int order) 1643 { 1644 XA_STATE_ORDER(xas, xa, index, order); 1645 unsigned int i; 1646 1647 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL); 1648 1649 for (i = 0; i < order + 10; i++) { 1650 do { 1651 xas_lock(&xas); 1652 xas_create_range(&xas); 1653 xas_unlock(&xas); 1654 } while (xas_nomem(&xas, GFP_KERNEL)); 1655 } 1656 1657 xa_destroy(xa); 1658 } 1659 1660 static noinline void check_create_range(struct xarray *xa) 1661 { 1662 unsigned int order; 1663 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1; 1664 1665 for (order = 0; order < max_order; order++) { 1666 check_create_range_1(xa, 0, order); 1667 check_create_range_1(xa, 1U << order, order); 1668 check_create_range_1(xa, 2U << order, order); 1669 check_create_range_1(xa, 3U << order, order); 1670 check_create_range_1(xa, 1U << 24, order); 1671 if (order < 10) 1672 check_create_range_2(xa, order); 1673 1674 check_create_range_4(xa, 0, order); 1675 check_create_range_4(xa, 1U << order, order); 1676 check_create_range_4(xa, 2U << order, order); 1677 check_create_range_4(xa, 3U << order, order); 1678 check_create_range_4(xa, 1U << 24, order); 1679 1680 check_create_range_4(xa, 1, order); 1681 check_create_range_4(xa, (1U << order) + 1, order); 1682 check_create_range_4(xa, (2U << order) + 1, order); 1683 check_create_range_4(xa, (2U << order) - 1, order); 1684 check_create_range_4(xa, (3U << order) + 1, order); 1685 check_create_range_4(xa, (3U << order) - 1, order); 1686 check_create_range_4(xa, (1U << 24) + 1, order); 1687 1688 check_create_range_5(xa, 0, order); 1689 check_create_range_5(xa, (1U << order), order); 1690 } 1691 1692 check_create_range_3(); 1693 } 1694 1695 static noinline void __check_store_range(struct xarray *xa, unsigned long first, 1696 unsigned long last) 1697 { 1698 #ifdef CONFIG_XARRAY_MULTI 1699 xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL); 1700 1701 XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first)); 1702 XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first)); 1703 XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL); 1704 XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL); 1705 1706 xa_store_range(xa, first, last, NULL, GFP_KERNEL); 1707 #endif 1708 1709 XA_BUG_ON(xa, !xa_empty(xa)); 1710 } 1711 1712 static noinline void check_store_range(struct xarray *xa) 1713 { 1714 unsigned long i, j; 1715 1716 for (i = 0; i < 128; i++) { 1717 for (j = i; j < 128; j++) { 1718 __check_store_range(xa, i, j); 1719 __check_store_range(xa, 128 + i, 128 + j); 1720 __check_store_range(xa, 4095 + i, 4095 + j); 1721 __check_store_range(xa, 4096 + i, 4096 + j); 1722 __check_store_range(xa, 123456 + i, 123456 + j); 1723 __check_store_range(xa, (1 << 24) + i, (1 << 24) + j); 1724 } 1725 } 1726 } 1727 1728 #ifdef CONFIG_XARRAY_MULTI 1729 static void check_split_1(struct xarray *xa, unsigned long index, 1730 unsigned int order, unsigned int new_order) 1731 { 1732 XA_STATE_ORDER(xas, xa, index, new_order); 1733 unsigned int i; 1734 1735 xa_store_order(xa, index, order, xa, GFP_KERNEL); 1736 1737 xas_split_alloc(&xas, xa, order, GFP_KERNEL); 1738 xas_lock(&xas); 1739 xas_split(&xas, xa, order); 1740 for (i = 0; i < (1 << order); i += (1 << new_order)) 1741 __xa_store(xa, index + i, xa_mk_index(index + i), 0); 1742 xas_unlock(&xas); 1743 1744 for (i = 0; i < (1 << order); i++) { 1745 unsigned int val = index + (i & ~((1 << new_order) - 1)); 1746 XA_BUG_ON(xa, xa_load(xa, index + i) != xa_mk_index(val)); 1747 } 1748 1749 xa_set_mark(xa, index, XA_MARK_0); 1750 XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); 1751 1752 xa_destroy(xa); 1753 } 1754 1755 static noinline void check_split(struct xarray *xa) 1756 { 1757 unsigned int order, new_order; 1758 1759 XA_BUG_ON(xa, !xa_empty(xa)); 1760 1761 for (order = 1; order < 2 * XA_CHUNK_SHIFT; order++) { 1762 for (new_order = 0; new_order < order; new_order++) { 1763 check_split_1(xa, 0, order, new_order); 1764 check_split_1(xa, 1UL << order, order, new_order); 1765 check_split_1(xa, 3UL << order, order, new_order); 1766 } 1767 } 1768 } 1769 #else 1770 static void check_split(struct xarray *xa) { } 1771 #endif 1772 1773 static void check_align_1(struct xarray *xa, char *name) 1774 { 1775 int i; 1776 unsigned int id; 1777 unsigned long index; 1778 void *entry; 1779 1780 for (i = 0; i < 8; i++) { 1781 XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b, 1782 GFP_KERNEL) != 0); 1783 XA_BUG_ON(xa, id != i); 1784 } 1785 xa_for_each(xa, index, entry) 1786 XA_BUG_ON(xa, xa_is_err(entry)); 1787 xa_destroy(xa); 1788 } 1789 1790 /* 1791 * We should always be able to store without allocating memory after 1792 * reserving a slot. 1793 */ 1794 static void check_align_2(struct xarray *xa, char *name) 1795 { 1796 int i; 1797 1798 XA_BUG_ON(xa, !xa_empty(xa)); 1799 1800 for (i = 0; i < 8; i++) { 1801 XA_BUG_ON(xa, xa_store(xa, 0, name + i, GFP_KERNEL) != NULL); 1802 xa_erase(xa, 0); 1803 } 1804 1805 for (i = 0; i < 8; i++) { 1806 XA_BUG_ON(xa, xa_reserve(xa, 0, GFP_KERNEL) != 0); 1807 XA_BUG_ON(xa, xa_store(xa, 0, name + i, 0) != NULL); 1808 xa_erase(xa, 0); 1809 } 1810 1811 XA_BUG_ON(xa, !xa_empty(xa)); 1812 } 1813 1814 static noinline void check_align(struct xarray *xa) 1815 { 1816 char name[] = "Motorola 68000"; 1817 1818 check_align_1(xa, name); 1819 check_align_1(xa, name + 1); 1820 check_align_1(xa, name + 2); 1821 check_align_1(xa, name + 3); 1822 check_align_2(xa, name); 1823 } 1824 1825 static LIST_HEAD(shadow_nodes); 1826 1827 static void test_update_node(struct xa_node *node) 1828 { 1829 if (node->count && node->count == node->nr_values) { 1830 if (list_empty(&node->private_list)) 1831 list_add(&shadow_nodes, &node->private_list); 1832 } else { 1833 if (!list_empty(&node->private_list)) 1834 list_del_init(&node->private_list); 1835 } 1836 } 1837 1838 static noinline void shadow_remove(struct xarray *xa) 1839 { 1840 struct xa_node *node; 1841 1842 xa_lock(xa); 1843 while ((node = list_first_entry_or_null(&shadow_nodes, 1844 struct xa_node, private_list))) { 1845 XA_BUG_ON(xa, node->array != xa); 1846 list_del_init(&node->private_list); 1847 xa_delete_node(node, test_update_node); 1848 } 1849 xa_unlock(xa); 1850 } 1851 1852 static noinline void check_workingset(struct xarray *xa, unsigned long index) 1853 { 1854 XA_STATE(xas, xa, index); 1855 xas_set_update(&xas, test_update_node); 1856 1857 do { 1858 xas_lock(&xas); 1859 xas_store(&xas, xa_mk_value(0)); 1860 xas_next(&xas); 1861 xas_store(&xas, xa_mk_value(1)); 1862 xas_unlock(&xas); 1863 } while (xas_nomem(&xas, GFP_KERNEL)); 1864 1865 XA_BUG_ON(xa, list_empty(&shadow_nodes)); 1866 1867 xas_lock(&xas); 1868 xas_next(&xas); 1869 xas_store(&xas, &xas); 1870 XA_BUG_ON(xa, !list_empty(&shadow_nodes)); 1871 1872 xas_store(&xas, xa_mk_value(2)); 1873 xas_unlock(&xas); 1874 XA_BUG_ON(xa, list_empty(&shadow_nodes)); 1875 1876 shadow_remove(xa); 1877 XA_BUG_ON(xa, !list_empty(&shadow_nodes)); 1878 XA_BUG_ON(xa, !xa_empty(xa)); 1879 } 1880 1881 /* 1882 * Check that the pointer / value / sibling entries are accounted the 1883 * way we expect them to be. 1884 */ 1885 static noinline void check_account(struct xarray *xa) 1886 { 1887 #ifdef CONFIG_XARRAY_MULTI 1888 unsigned int order; 1889 1890 for (order = 1; order < 12; order++) { 1891 XA_STATE(xas, xa, 1 << order); 1892 1893 xa_store_order(xa, 0, order, xa, GFP_KERNEL); 1894 rcu_read_lock(); 1895 xas_load(&xas); 1896 XA_BUG_ON(xa, xas.xa_node->count == 0); 1897 XA_BUG_ON(xa, xas.xa_node->count > (1 << order)); 1898 XA_BUG_ON(xa, xas.xa_node->nr_values != 0); 1899 rcu_read_unlock(); 1900 1901 xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order), 1902 GFP_KERNEL); 1903 XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2); 1904 1905 xa_erase(xa, 1 << order); 1906 XA_BUG_ON(xa, xas.xa_node->nr_values != 0); 1907 1908 xa_erase(xa, 0); 1909 XA_BUG_ON(xa, !xa_empty(xa)); 1910 } 1911 #endif 1912 } 1913 1914 static noinline void check_get_order(struct xarray *xa) 1915 { 1916 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; 1917 unsigned int order; 1918 unsigned long i, j; 1919 1920 for (i = 0; i < 3; i++) 1921 XA_BUG_ON(xa, xa_get_order(xa, i) != 0); 1922 1923 for (order = 0; order < max_order; order++) { 1924 for (i = 0; i < 10; i++) { 1925 xa_store_order(xa, i << order, order, 1926 xa_mk_index(i << order), GFP_KERNEL); 1927 for (j = i << order; j < (i + 1) << order; j++) 1928 XA_BUG_ON(xa, xa_get_order(xa, j) != order); 1929 xa_erase(xa, i << order); 1930 } 1931 } 1932 } 1933 1934 static noinline void check_destroy(struct xarray *xa) 1935 { 1936 unsigned long index; 1937 1938 XA_BUG_ON(xa, !xa_empty(xa)); 1939 1940 /* Destroying an empty array is a no-op */ 1941 xa_destroy(xa); 1942 XA_BUG_ON(xa, !xa_empty(xa)); 1943 1944 /* Destroying an array with a single entry */ 1945 for (index = 0; index < 1000; index++) { 1946 xa_store_index(xa, index, GFP_KERNEL); 1947 XA_BUG_ON(xa, xa_empty(xa)); 1948 xa_destroy(xa); 1949 XA_BUG_ON(xa, !xa_empty(xa)); 1950 } 1951 1952 /* Destroying an array with a single entry at ULONG_MAX */ 1953 xa_store(xa, ULONG_MAX, xa, GFP_KERNEL); 1954 XA_BUG_ON(xa, xa_empty(xa)); 1955 xa_destroy(xa); 1956 XA_BUG_ON(xa, !xa_empty(xa)); 1957 1958 #ifdef CONFIG_XARRAY_MULTI 1959 /* Destroying an array with a multi-index entry */ 1960 xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL); 1961 XA_BUG_ON(xa, xa_empty(xa)); 1962 xa_destroy(xa); 1963 XA_BUG_ON(xa, !xa_empty(xa)); 1964 #endif 1965 } 1966 1967 static DEFINE_XARRAY(array); 1968 1969 static int xarray_checks(void) 1970 { 1971 check_xa_err(&array); 1972 check_xas_retry(&array); 1973 check_xa_load(&array); 1974 check_xa_mark(&array); 1975 check_xa_shrink(&array); 1976 check_xas_erase(&array); 1977 check_insert(&array); 1978 check_cmpxchg(&array); 1979 check_reserve(&array); 1980 check_reserve(&xa0); 1981 check_multi_store(&array); 1982 check_multi_store_advanced(&array); 1983 check_get_order(&array); 1984 check_xa_alloc(); 1985 check_find(&array); 1986 check_find_entry(&array); 1987 check_pause(&array); 1988 check_account(&array); 1989 check_destroy(&array); 1990 check_move(&array); 1991 check_create_range(&array); 1992 check_store_range(&array); 1993 check_store_iter(&array); 1994 check_align(&xa0); 1995 check_split(&array); 1996 1997 check_workingset(&array, 0); 1998 check_workingset(&array, 64); 1999 check_workingset(&array, 4096); 2000 2001 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); 2002 return (tests_run == tests_passed) ? 0 : -EINVAL; 2003 } 2004 2005 static void xarray_exit(void) 2006 { 2007 } 2008 2009 module_init(xarray_checks); 2010 module_exit(xarray_exit); 2011 MODULE_AUTHOR("Matthew Wilcox <[email protected]>"); 2012 MODULE_LICENSE("GPL"); 2013