Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * test_xarray.c: Test the XArray API |
| 4 | * Copyright (c) 2017-2018 Microsoft Corporation |
| 5 | * Author: Matthew Wilcox <willy@infradead.org> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/xarray.h> |
| 9 | #include <linux/module.h> |
| 10 | |
| 11 | static unsigned int tests_run; |
| 12 | static unsigned int tests_passed; |
| 13 | |
| 14 | #ifndef XA_DEBUG |
| 15 | # ifdef __KERNEL__ |
| 16 | void xa_dump(const struct xarray *xa) { } |
| 17 | # endif |
| 18 | #undef XA_BUG_ON |
| 19 | #define XA_BUG_ON(xa, x) do { \ |
| 20 | tests_run++; \ |
| 21 | if (x) { \ |
| 22 | printk("BUG at %s:%d\n", __func__, __LINE__); \ |
| 23 | xa_dump(xa); \ |
| 24 | dump_stack(); \ |
| 25 | } else { \ |
| 26 | tests_passed++; \ |
| 27 | } \ |
| 28 | } while (0) |
| 29 | #endif |
| 30 | |
| 31 | static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) |
| 32 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 33 | return xa_store(xa, index, xa_mk_value(index & LONG_MAX), gfp); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 36 | static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp) |
| 37 | { |
| 38 | u32 id = 0; |
| 39 | |
| 40 | XA_BUG_ON(xa, xa_alloc(xa, &id, UINT_MAX, xa_mk_value(index & LONG_MAX), |
| 41 | gfp) != 0); |
| 42 | XA_BUG_ON(xa, id != index); |
| 43 | } |
| 44 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 45 | static void xa_erase_index(struct xarray *xa, unsigned long index) |
| 46 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 47 | XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_value(index & LONG_MAX)); |
| 48 | XA_BUG_ON(xa, xa_load(xa, index) != NULL); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * If anyone needs this, please move it to xarray.c. We have no current |
| 53 | * users outside the test suite because all current multislot users want |
| 54 | * to use the advanced API. |
| 55 | */ |
| 56 | static void *xa_store_order(struct xarray *xa, unsigned long index, |
| 57 | unsigned order, void *entry, gfp_t gfp) |
| 58 | { |
| 59 | XA_STATE_ORDER(xas, xa, index, order); |
| 60 | void *curr; |
| 61 | |
| 62 | do { |
| 63 | xas_lock(&xas); |
| 64 | curr = xas_store(&xas, entry); |
| 65 | xas_unlock(&xas); |
| 66 | } while (xas_nomem(&xas, gfp)); |
| 67 | |
| 68 | return curr; |
| 69 | } |
| 70 | |
| 71 | static noinline void check_xa_err(struct xarray *xa) |
| 72 | { |
| 73 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0); |
| 74 | XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0); |
| 75 | #ifndef __KERNEL__ |
| 76 | /* The kernel does not fail GFP_NOWAIT allocations */ |
| 77 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); |
| 78 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); |
| 79 | #endif |
| 80 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0); |
| 81 | XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0); |
| 82 | XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0); |
| 83 | // kills the test-suite :-( |
| 84 | // XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 87 | static noinline void check_xas_retry(struct xarray *xa) |
| 88 | { |
| 89 | XA_STATE(xas, xa, 0); |
| 90 | void *entry; |
| 91 | |
| 92 | xa_store_index(xa, 0, GFP_KERNEL); |
| 93 | xa_store_index(xa, 1, GFP_KERNEL); |
| 94 | |
| 95 | rcu_read_lock(); |
| 96 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0)); |
| 97 | xa_erase_index(xa, 1); |
| 98 | XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas))); |
| 99 | XA_BUG_ON(xa, xas_retry(&xas, NULL)); |
| 100 | XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0))); |
| 101 | xas_reset(&xas); |
| 102 | XA_BUG_ON(xa, xas.xa_node != XAS_RESTART); |
| 103 | XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); |
| 104 | XA_BUG_ON(xa, xas.xa_node != NULL); |
| 105 | |
| 106 | XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); |
| 107 | XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas))); |
| 108 | xas.xa_node = XAS_RESTART; |
| 109 | XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); |
| 110 | rcu_read_unlock(); |
| 111 | |
| 112 | /* Make sure we can iterate through retry entries */ |
| 113 | xas_lock(&xas); |
| 114 | xas_set(&xas, 0); |
| 115 | xas_store(&xas, XA_RETRY_ENTRY); |
| 116 | xas_set(&xas, 1); |
| 117 | xas_store(&xas, XA_RETRY_ENTRY); |
| 118 | |
| 119 | xas_set(&xas, 0); |
| 120 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 121 | xas_store(&xas, xa_mk_value(xas.xa_index)); |
| 122 | } |
| 123 | xas_unlock(&xas); |
| 124 | |
| 125 | xa_erase_index(xa, 0); |
| 126 | xa_erase_index(xa, 1); |
| 127 | } |
| 128 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 129 | static noinline void check_xa_load(struct xarray *xa) |
| 130 | { |
| 131 | unsigned long i, j; |
| 132 | |
| 133 | for (i = 0; i < 1024; i++) { |
| 134 | for (j = 0; j < 1024; j++) { |
| 135 | void *entry = xa_load(xa, j); |
| 136 | if (j < i) |
| 137 | XA_BUG_ON(xa, xa_to_value(entry) != j); |
| 138 | else |
| 139 | XA_BUG_ON(xa, entry); |
| 140 | } |
| 141 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); |
| 142 | } |
| 143 | |
| 144 | for (i = 0; i < 1024; i++) { |
| 145 | for (j = 0; j < 1024; j++) { |
| 146 | void *entry = xa_load(xa, j); |
| 147 | if (j >= i) |
| 148 | XA_BUG_ON(xa, xa_to_value(entry) != j); |
| 149 | else |
| 150 | XA_BUG_ON(xa, entry); |
| 151 | } |
| 152 | xa_erase_index(xa, i); |
| 153 | } |
| 154 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 155 | } |
| 156 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 157 | static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index) |
| 158 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 159 | unsigned int order; |
| 160 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1; |
| 161 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 162 | /* NULL elements have no marks set */ |
| 163 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); |
| 164 | xa_set_mark(xa, index, XA_MARK_0); |
| 165 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); |
| 166 | |
| 167 | /* Storing a pointer will not make a mark appear */ |
| 168 | XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL); |
| 169 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); |
| 170 | xa_set_mark(xa, index, XA_MARK_0); |
| 171 | XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); |
| 172 | |
| 173 | /* Setting one mark will not set another mark */ |
| 174 | XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0)); |
| 175 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1)); |
| 176 | |
| 177 | /* Storing NULL clears marks, and they can't be set again */ |
| 178 | xa_erase_index(xa, index); |
| 179 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 180 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); |
| 181 | xa_set_mark(xa, index, XA_MARK_0); |
| 182 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Storing a multi-index entry over entries with marks gives the |
| 186 | * entire entry the union of the marks |
| 187 | */ |
| 188 | BUG_ON((index % 4) != 0); |
| 189 | for (order = 2; order < max_order; order++) { |
| 190 | unsigned long base = round_down(index, 1UL << order); |
| 191 | unsigned long next = base + (1UL << order); |
| 192 | unsigned long i; |
| 193 | |
| 194 | XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL)); |
| 195 | xa_set_mark(xa, index + 1, XA_MARK_0); |
| 196 | XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL)); |
| 197 | xa_set_mark(xa, index + 2, XA_MARK_1); |
| 198 | XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL)); |
| 199 | xa_store_order(xa, index, order, xa_mk_value(index), |
| 200 | GFP_KERNEL); |
| 201 | for (i = base; i < next; i++) { |
| 202 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); |
| 203 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_1)); |
| 204 | XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_2)); |
| 205 | } |
| 206 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0)); |
| 207 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1)); |
| 208 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2)); |
| 209 | xa_erase_index(xa, index); |
| 210 | xa_erase_index(xa, next); |
| 211 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 212 | } |
| 213 | XA_BUG_ON(xa, !xa_empty(xa)); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Matthew Wilcox | adb9d9c | 2018-04-09 16:52:21 -0400 | [diff] [blame] | 216 | static noinline void check_xa_mark_2(struct xarray *xa) |
| 217 | { |
| 218 | XA_STATE(xas, xa, 0); |
| 219 | unsigned long index; |
| 220 | unsigned int count = 0; |
| 221 | void *entry; |
| 222 | |
| 223 | xa_store_index(xa, 0, GFP_KERNEL); |
| 224 | xa_set_mark(xa, 0, XA_MARK_0); |
| 225 | xas_lock(&xas); |
| 226 | xas_load(&xas); |
| 227 | xas_init_marks(&xas); |
| 228 | xas_unlock(&xas); |
| 229 | XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0); |
| 230 | |
| 231 | for (index = 3500; index < 4500; index++) { |
| 232 | xa_store_index(xa, index, GFP_KERNEL); |
| 233 | xa_set_mark(xa, index, XA_MARK_0); |
| 234 | } |
| 235 | |
| 236 | xas_reset(&xas); |
| 237 | rcu_read_lock(); |
| 238 | xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) |
| 239 | count++; |
| 240 | rcu_read_unlock(); |
| 241 | XA_BUG_ON(xa, count != 1000); |
| 242 | |
| 243 | xas_lock(&xas); |
| 244 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 245 | xas_init_marks(&xas); |
| 246 | XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0)); |
| 247 | XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0)); |
| 248 | } |
| 249 | xas_unlock(&xas); |
| 250 | |
| 251 | xa_destroy(xa); |
| 252 | } |
| 253 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 254 | static noinline void check_xa_mark(struct xarray *xa) |
| 255 | { |
| 256 | unsigned long index; |
| 257 | |
| 258 | for (index = 0; index < 16384; index += 4) |
| 259 | check_xa_mark_1(xa, index); |
Matthew Wilcox | adb9d9c | 2018-04-09 16:52:21 -0400 | [diff] [blame] | 260 | |
| 261 | check_xa_mark_2(xa); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 264 | static noinline void check_xa_shrink(struct xarray *xa) |
| 265 | { |
| 266 | XA_STATE(xas, xa, 1); |
| 267 | struct xa_node *node; |
| 268 | |
| 269 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 270 | XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL); |
| 271 | XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); |
| 272 | |
| 273 | /* |
| 274 | * Check that erasing the entry at 1 shrinks the tree and properly |
| 275 | * marks the node as being deleted. |
| 276 | */ |
| 277 | xas_lock(&xas); |
| 278 | XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1)); |
| 279 | node = xas.xa_node; |
| 280 | XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0)); |
| 281 | XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1)); |
| 282 | XA_BUG_ON(xa, xa_load(xa, 1) != NULL); |
| 283 | XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS); |
| 284 | XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY); |
| 285 | XA_BUG_ON(xa, xas_load(&xas) != NULL); |
| 286 | xas_unlock(&xas); |
| 287 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); |
| 288 | xa_erase_index(xa, 0); |
| 289 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 290 | } |
| 291 | |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 292 | static noinline void check_cmpxchg(struct xarray *xa) |
| 293 | { |
| 294 | void *FIVE = xa_mk_value(5); |
| 295 | void *SIX = xa_mk_value(6); |
| 296 | void *LOTS = xa_mk_value(12345678); |
| 297 | |
| 298 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 299 | XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL); |
| 300 | XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EEXIST); |
| 301 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS); |
| 302 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS); |
| 303 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE); |
| 304 | XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL); |
| 305 | XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL); |
| 306 | xa_erase_index(xa, 12345678); |
| 307 | xa_erase_index(xa, 5); |
| 308 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 309 | } |
| 310 | |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 311 | static noinline void check_reserve(struct xarray *xa) |
| 312 | { |
| 313 | void *entry; |
| 314 | unsigned long index = 0; |
| 315 | |
| 316 | /* An array with a reserved entry is not empty */ |
| 317 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 318 | xa_reserve(xa, 12345678, GFP_KERNEL); |
| 319 | XA_BUG_ON(xa, xa_empty(xa)); |
| 320 | XA_BUG_ON(xa, xa_load(xa, 12345678)); |
| 321 | xa_release(xa, 12345678); |
| 322 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 323 | |
| 324 | /* Releasing a used entry does nothing */ |
| 325 | xa_reserve(xa, 12345678, GFP_KERNEL); |
| 326 | XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL); |
| 327 | xa_release(xa, 12345678); |
| 328 | xa_erase_index(xa, 12345678); |
| 329 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 330 | |
| 331 | /* cmpxchg sees a reserved entry as NULL */ |
| 332 | xa_reserve(xa, 12345678, GFP_KERNEL); |
| 333 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, NULL, xa_mk_value(12345678), |
| 334 | GFP_NOWAIT) != NULL); |
| 335 | xa_release(xa, 12345678); |
| 336 | xa_erase_index(xa, 12345678); |
| 337 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 338 | |
| 339 | /* Can iterate through a reserved entry */ |
| 340 | xa_store_index(xa, 5, GFP_KERNEL); |
| 341 | xa_reserve(xa, 6, GFP_KERNEL); |
| 342 | xa_store_index(xa, 7, GFP_KERNEL); |
| 343 | |
| 344 | xa_for_each(xa, entry, index, ULONG_MAX, XA_PRESENT) { |
| 345 | XA_BUG_ON(xa, index != 5 && index != 7); |
| 346 | } |
| 347 | xa_destroy(xa); |
| 348 | } |
| 349 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 350 | static noinline void check_xas_erase(struct xarray *xa) |
| 351 | { |
| 352 | XA_STATE(xas, xa, 0); |
| 353 | void *entry; |
| 354 | unsigned long i, j; |
| 355 | |
| 356 | for (i = 0; i < 200; i++) { |
| 357 | for (j = i; j < 2 * i + 17; j++) { |
| 358 | xas_set(&xas, j); |
| 359 | do { |
| 360 | xas_lock(&xas); |
| 361 | xas_store(&xas, xa_mk_value(j)); |
| 362 | xas_unlock(&xas); |
| 363 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 364 | } |
| 365 | |
| 366 | xas_set(&xas, ULONG_MAX); |
| 367 | do { |
| 368 | xas_lock(&xas); |
| 369 | xas_store(&xas, xa_mk_value(0)); |
| 370 | xas_unlock(&xas); |
| 371 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 372 | |
| 373 | xas_lock(&xas); |
| 374 | xas_store(&xas, NULL); |
| 375 | |
| 376 | xas_set(&xas, 0); |
| 377 | j = i; |
| 378 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 379 | XA_BUG_ON(xa, entry != xa_mk_value(j)); |
| 380 | xas_store(&xas, NULL); |
| 381 | j++; |
| 382 | } |
| 383 | xas_unlock(&xas); |
| 384 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 385 | } |
| 386 | } |
| 387 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 388 | static noinline void check_multi_store(struct xarray *xa) |
| 389 | { |
| 390 | #ifdef CONFIG_XARRAY_MULTI |
| 391 | unsigned long i, j, k; |
| 392 | unsigned int max_order = (sizeof(long) == 4) ? 30 : 60; |
| 393 | |
| 394 | /* Loading from any position returns the same value */ |
| 395 | xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL); |
| 396 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); |
| 397 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); |
| 398 | XA_BUG_ON(xa, xa_load(xa, 2) != NULL); |
| 399 | rcu_read_lock(); |
| 400 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2); |
| 401 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); |
| 402 | rcu_read_unlock(); |
| 403 | |
| 404 | /* Storing adjacent to the value does not alter the value */ |
| 405 | xa_store(xa, 3, xa, GFP_KERNEL); |
| 406 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); |
| 407 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); |
| 408 | XA_BUG_ON(xa, xa_load(xa, 2) != NULL); |
| 409 | rcu_read_lock(); |
| 410 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3); |
| 411 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); |
| 412 | rcu_read_unlock(); |
| 413 | |
| 414 | /* Overwriting multiple indexes works */ |
| 415 | xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL); |
| 416 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1)); |
| 417 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1)); |
| 418 | XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1)); |
| 419 | XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1)); |
| 420 | XA_BUG_ON(xa, xa_load(xa, 4) != NULL); |
| 421 | rcu_read_lock(); |
| 422 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4); |
| 423 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4); |
| 424 | rcu_read_unlock(); |
| 425 | |
| 426 | /* We can erase multiple values with a single store */ |
| 427 | xa_store_order(xa, 0, 63, NULL, GFP_KERNEL); |
| 428 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 429 | |
| 430 | /* Even when the first slot is empty but the others aren't */ |
| 431 | xa_store_index(xa, 1, GFP_KERNEL); |
| 432 | xa_store_index(xa, 2, GFP_KERNEL); |
| 433 | xa_store_order(xa, 0, 2, NULL, GFP_KERNEL); |
| 434 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 435 | |
| 436 | for (i = 0; i < max_order; i++) { |
| 437 | for (j = 0; j < max_order; j++) { |
| 438 | xa_store_order(xa, 0, i, xa_mk_value(i), GFP_KERNEL); |
| 439 | xa_store_order(xa, 0, j, xa_mk_value(j), GFP_KERNEL); |
| 440 | |
| 441 | for (k = 0; k < max_order; k++) { |
| 442 | void *entry = xa_load(xa, (1UL << k) - 1); |
| 443 | if ((i < k) && (j < k)) |
| 444 | XA_BUG_ON(xa, entry != NULL); |
| 445 | else |
| 446 | XA_BUG_ON(xa, entry != xa_mk_value(j)); |
| 447 | } |
| 448 | |
| 449 | xa_erase(xa, 0); |
| 450 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 451 | } |
| 452 | } |
| 453 | #endif |
| 454 | } |
| 455 | |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 456 | static DEFINE_XARRAY_ALLOC(xa0); |
| 457 | |
| 458 | static noinline void check_xa_alloc(void) |
| 459 | { |
| 460 | int i; |
| 461 | u32 id; |
| 462 | |
| 463 | /* An empty array should assign 0 to the first alloc */ |
| 464 | xa_alloc_index(&xa0, 0, GFP_KERNEL); |
| 465 | |
| 466 | /* Erasing it should make the array empty again */ |
| 467 | xa_erase_index(&xa0, 0); |
| 468 | XA_BUG_ON(&xa0, !xa_empty(&xa0)); |
| 469 | |
| 470 | /* And it should assign 0 again */ |
| 471 | xa_alloc_index(&xa0, 0, GFP_KERNEL); |
| 472 | |
| 473 | /* The next assigned ID should be 1 */ |
| 474 | xa_alloc_index(&xa0, 1, GFP_KERNEL); |
| 475 | xa_erase_index(&xa0, 1); |
| 476 | |
| 477 | /* Storing a value should mark it used */ |
| 478 | xa_store_index(&xa0, 1, GFP_KERNEL); |
| 479 | xa_alloc_index(&xa0, 2, GFP_KERNEL); |
| 480 | |
| 481 | /* If we then erase 0, it should be free */ |
| 482 | xa_erase_index(&xa0, 0); |
| 483 | xa_alloc_index(&xa0, 0, GFP_KERNEL); |
| 484 | |
| 485 | xa_erase_index(&xa0, 1); |
| 486 | xa_erase_index(&xa0, 2); |
| 487 | |
| 488 | for (i = 1; i < 5000; i++) { |
| 489 | xa_alloc_index(&xa0, i, GFP_KERNEL); |
| 490 | } |
| 491 | |
| 492 | xa_destroy(&xa0); |
| 493 | |
| 494 | id = 0xfffffffeU; |
| 495 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), |
| 496 | GFP_KERNEL) != 0); |
| 497 | XA_BUG_ON(&xa0, id != 0xfffffffeU); |
| 498 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), |
| 499 | GFP_KERNEL) != 0); |
| 500 | XA_BUG_ON(&xa0, id != 0xffffffffU); |
| 501 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), |
| 502 | GFP_KERNEL) != -ENOSPC); |
| 503 | XA_BUG_ON(&xa0, id != 0xffffffffU); |
| 504 | xa_destroy(&xa0); |
| 505 | } |
| 506 | |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 507 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, |
| 508 | unsigned int order, unsigned int present) |
| 509 | { |
| 510 | XA_STATE_ORDER(xas, xa, start, order); |
| 511 | void *entry; |
| 512 | unsigned int count = 0; |
| 513 | |
| 514 | retry: |
| 515 | xas_lock(&xas); |
| 516 | xas_for_each_conflict(&xas, entry) { |
| 517 | XA_BUG_ON(xa, !xa_is_value(entry)); |
| 518 | XA_BUG_ON(xa, entry < xa_mk_value(start)); |
| 519 | XA_BUG_ON(xa, entry > xa_mk_value(start + (1UL << order) - 1)); |
| 520 | count++; |
| 521 | } |
| 522 | xas_store(&xas, xa_mk_value(start)); |
| 523 | xas_unlock(&xas); |
| 524 | if (xas_nomem(&xas, GFP_KERNEL)) { |
| 525 | count = 0; |
| 526 | goto retry; |
| 527 | } |
| 528 | XA_BUG_ON(xa, xas_error(&xas)); |
| 529 | XA_BUG_ON(xa, count != present); |
| 530 | XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_value(start)); |
| 531 | XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) != |
| 532 | xa_mk_value(start)); |
| 533 | xa_erase_index(xa, start); |
| 534 | } |
| 535 | |
| 536 | static noinline void check_store_iter(struct xarray *xa) |
| 537 | { |
| 538 | unsigned int i, j; |
| 539 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; |
| 540 | |
| 541 | for (i = 0; i < max_order; i++) { |
| 542 | unsigned int min = 1 << i; |
| 543 | unsigned int max = (2 << i) - 1; |
| 544 | __check_store_iter(xa, 0, i, 0); |
| 545 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 546 | __check_store_iter(xa, min, i, 0); |
| 547 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 548 | |
| 549 | xa_store_index(xa, min, GFP_KERNEL); |
| 550 | __check_store_iter(xa, min, i, 1); |
| 551 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 552 | xa_store_index(xa, max, GFP_KERNEL); |
| 553 | __check_store_iter(xa, min, i, 1); |
| 554 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 555 | |
| 556 | for (j = 0; j < min; j++) |
| 557 | xa_store_index(xa, j, GFP_KERNEL); |
| 558 | __check_store_iter(xa, 0, i, min); |
| 559 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 560 | for (j = 0; j < min; j++) |
| 561 | xa_store_index(xa, min + j, GFP_KERNEL); |
| 562 | __check_store_iter(xa, min, i, min); |
| 563 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 564 | } |
| 565 | #ifdef CONFIG_XARRAY_MULTI |
| 566 | xa_store_index(xa, 63, GFP_KERNEL); |
| 567 | xa_store_index(xa, 65, GFP_KERNEL); |
| 568 | __check_store_iter(xa, 64, 2, 1); |
| 569 | xa_erase_index(xa, 63); |
| 570 | #endif |
| 571 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 572 | } |
| 573 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 574 | static noinline void check_multi_find(struct xarray *xa) |
| 575 | { |
| 576 | #ifdef CONFIG_XARRAY_MULTI |
| 577 | unsigned long index; |
| 578 | |
| 579 | xa_store_order(xa, 12, 2, xa_mk_value(12), GFP_KERNEL); |
| 580 | XA_BUG_ON(xa, xa_store_index(xa, 16, GFP_KERNEL) != NULL); |
| 581 | |
| 582 | index = 0; |
| 583 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != |
| 584 | xa_mk_value(12)); |
| 585 | XA_BUG_ON(xa, index != 12); |
| 586 | index = 13; |
| 587 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != |
| 588 | xa_mk_value(12)); |
| 589 | XA_BUG_ON(xa, (index < 12) || (index >= 16)); |
| 590 | XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) != |
| 591 | xa_mk_value(16)); |
| 592 | XA_BUG_ON(xa, index != 16); |
| 593 | |
| 594 | xa_erase_index(xa, 12); |
| 595 | xa_erase_index(xa, 16); |
| 596 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 597 | #endif |
| 598 | } |
| 599 | |
| 600 | static noinline void check_multi_find_2(struct xarray *xa) |
| 601 | { |
| 602 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1; |
| 603 | unsigned int i, j; |
| 604 | void *entry; |
| 605 | |
| 606 | for (i = 0; i < max_order; i++) { |
| 607 | unsigned long index = 1UL << i; |
| 608 | for (j = 0; j < index; j++) { |
| 609 | XA_STATE(xas, xa, j + index); |
| 610 | xa_store_index(xa, index - 1, GFP_KERNEL); |
| 611 | xa_store_order(xa, index, i, xa_mk_value(index), |
| 612 | GFP_KERNEL); |
| 613 | rcu_read_lock(); |
| 614 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 615 | xa_erase_index(xa, index); |
| 616 | } |
| 617 | rcu_read_unlock(); |
| 618 | xa_erase_index(xa, index - 1); |
| 619 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static noinline void check_find(struct xarray *xa) |
| 625 | { |
| 626 | unsigned long i, j, k; |
| 627 | |
| 628 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 629 | |
| 630 | /* |
| 631 | * Check xa_find with all pairs between 0 and 99 inclusive, |
| 632 | * starting at every index between 0 and 99 |
| 633 | */ |
| 634 | for (i = 0; i < 100; i++) { |
| 635 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); |
| 636 | xa_set_mark(xa, i, XA_MARK_0); |
| 637 | for (j = 0; j < i; j++) { |
| 638 | XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) != |
| 639 | NULL); |
| 640 | xa_set_mark(xa, j, XA_MARK_0); |
| 641 | for (k = 0; k < 100; k++) { |
| 642 | unsigned long index = k; |
| 643 | void *entry = xa_find(xa, &index, ULONG_MAX, |
| 644 | XA_PRESENT); |
| 645 | if (k <= j) |
| 646 | XA_BUG_ON(xa, index != j); |
| 647 | else if (k <= i) |
| 648 | XA_BUG_ON(xa, index != i); |
| 649 | else |
| 650 | XA_BUG_ON(xa, entry != NULL); |
| 651 | |
| 652 | index = k; |
| 653 | entry = xa_find(xa, &index, ULONG_MAX, |
| 654 | XA_MARK_0); |
| 655 | if (k <= j) |
| 656 | XA_BUG_ON(xa, index != j); |
| 657 | else if (k <= i) |
| 658 | XA_BUG_ON(xa, index != i); |
| 659 | else |
| 660 | XA_BUG_ON(xa, entry != NULL); |
| 661 | } |
| 662 | xa_erase_index(xa, j); |
| 663 | XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0)); |
| 664 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); |
| 665 | } |
| 666 | xa_erase_index(xa, i); |
| 667 | XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0)); |
| 668 | } |
| 669 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 670 | check_multi_find(xa); |
| 671 | check_multi_find_2(xa); |
| 672 | } |
| 673 | |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 674 | /* See find_swap_entry() in mm/shmem.c */ |
| 675 | static noinline unsigned long xa_find_entry(struct xarray *xa, void *item) |
| 676 | { |
| 677 | XA_STATE(xas, xa, 0); |
| 678 | unsigned int checked = 0; |
| 679 | void *entry; |
| 680 | |
| 681 | rcu_read_lock(); |
| 682 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 683 | if (xas_retry(&xas, entry)) |
| 684 | continue; |
| 685 | if (entry == item) |
| 686 | break; |
| 687 | checked++; |
| 688 | if ((checked % 4) != 0) |
| 689 | continue; |
| 690 | xas_pause(&xas); |
| 691 | } |
| 692 | rcu_read_unlock(); |
| 693 | |
| 694 | return entry ? xas.xa_index : -1; |
| 695 | } |
| 696 | |
| 697 | static noinline void check_find_entry(struct xarray *xa) |
| 698 | { |
| 699 | #ifdef CONFIG_XARRAY_MULTI |
| 700 | unsigned int order; |
| 701 | unsigned long offset, index; |
| 702 | |
| 703 | for (order = 0; order < 20; order++) { |
| 704 | for (offset = 0; offset < (1UL << (order + 3)); |
| 705 | offset += (1UL << order)) { |
| 706 | for (index = 0; index < (1UL << (order + 5)); |
| 707 | index += (1UL << order)) { |
| 708 | xa_store_order(xa, index, order, |
| 709 | xa_mk_value(index), GFP_KERNEL); |
| 710 | XA_BUG_ON(xa, xa_load(xa, index) != |
| 711 | xa_mk_value(index)); |
| 712 | XA_BUG_ON(xa, xa_find_entry(xa, |
| 713 | xa_mk_value(index)) != index); |
| 714 | } |
| 715 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
| 716 | xa_destroy(xa); |
| 717 | } |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
| 722 | xa_store_index(xa, ULONG_MAX, GFP_KERNEL); |
| 723 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
| 724 | XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_value(LONG_MAX)) != -1); |
| 725 | xa_erase_index(xa, ULONG_MAX); |
| 726 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 727 | } |
| 728 | |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 729 | static noinline void check_move_small(struct xarray *xa, unsigned long idx) |
| 730 | { |
| 731 | XA_STATE(xas, xa, 0); |
| 732 | unsigned long i; |
| 733 | |
| 734 | xa_store_index(xa, 0, GFP_KERNEL); |
| 735 | xa_store_index(xa, idx, GFP_KERNEL); |
| 736 | |
| 737 | rcu_read_lock(); |
| 738 | for (i = 0; i < idx * 4; i++) { |
| 739 | void *entry = xas_next(&xas); |
| 740 | if (i <= idx) |
| 741 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); |
| 742 | XA_BUG_ON(xa, xas.xa_index != i); |
| 743 | if (i == 0 || i == idx) |
| 744 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 745 | else |
| 746 | XA_BUG_ON(xa, entry != NULL); |
| 747 | } |
| 748 | xas_next(&xas); |
| 749 | XA_BUG_ON(xa, xas.xa_index != i); |
| 750 | |
| 751 | do { |
| 752 | void *entry = xas_prev(&xas); |
| 753 | i--; |
| 754 | if (i <= idx) |
| 755 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); |
| 756 | XA_BUG_ON(xa, xas.xa_index != i); |
| 757 | if (i == 0 || i == idx) |
| 758 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 759 | else |
| 760 | XA_BUG_ON(xa, entry != NULL); |
| 761 | } while (i > 0); |
| 762 | |
| 763 | xas_set(&xas, ULONG_MAX); |
| 764 | XA_BUG_ON(xa, xas_next(&xas) != NULL); |
| 765 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 766 | XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0)); |
| 767 | XA_BUG_ON(xa, xas.xa_index != 0); |
| 768 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 769 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 770 | rcu_read_unlock(); |
| 771 | |
| 772 | xa_erase_index(xa, 0); |
| 773 | xa_erase_index(xa, idx); |
| 774 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 775 | } |
| 776 | |
| 777 | static noinline void check_move(struct xarray *xa) |
| 778 | { |
| 779 | XA_STATE(xas, xa, (1 << 16) - 1); |
| 780 | unsigned long i; |
| 781 | |
| 782 | for (i = 0; i < (1 << 16); i++) |
| 783 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); |
| 784 | |
| 785 | rcu_read_lock(); |
| 786 | do { |
| 787 | void *entry = xas_prev(&xas); |
| 788 | i--; |
| 789 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 790 | XA_BUG_ON(xa, i != xas.xa_index); |
| 791 | } while (i != 0); |
| 792 | |
| 793 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 794 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 795 | |
| 796 | do { |
| 797 | void *entry = xas_next(&xas); |
| 798 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 799 | XA_BUG_ON(xa, i != xas.xa_index); |
| 800 | i++; |
| 801 | } while (i < (1 << 16)); |
| 802 | rcu_read_unlock(); |
| 803 | |
| 804 | for (i = (1 << 8); i < (1 << 15); i++) |
| 805 | xa_erase_index(xa, i); |
| 806 | |
| 807 | i = xas.xa_index; |
| 808 | |
| 809 | rcu_read_lock(); |
| 810 | do { |
| 811 | void *entry = xas_prev(&xas); |
| 812 | i--; |
| 813 | if ((i < (1 << 8)) || (i >= (1 << 15))) |
| 814 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 815 | else |
| 816 | XA_BUG_ON(xa, entry != NULL); |
| 817 | XA_BUG_ON(xa, i != xas.xa_index); |
| 818 | } while (i != 0); |
| 819 | |
| 820 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 821 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 822 | |
| 823 | do { |
| 824 | void *entry = xas_next(&xas); |
| 825 | if ((i < (1 << 8)) || (i >= (1 << 15))) |
| 826 | XA_BUG_ON(xa, entry != xa_mk_value(i)); |
| 827 | else |
| 828 | XA_BUG_ON(xa, entry != NULL); |
| 829 | XA_BUG_ON(xa, i != xas.xa_index); |
| 830 | i++; |
| 831 | } while (i < (1 << 16)); |
| 832 | rcu_read_unlock(); |
| 833 | |
| 834 | xa_destroy(xa); |
| 835 | |
| 836 | for (i = 0; i < 16; i++) |
| 837 | check_move_small(xa, 1UL << i); |
| 838 | |
| 839 | for (i = 2; i < 16; i++) |
| 840 | check_move_small(xa, (1UL << i) - 1); |
| 841 | } |
| 842 | |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 843 | static noinline void xa_store_many_order(struct xarray *xa, |
| 844 | unsigned long index, unsigned order) |
| 845 | { |
| 846 | XA_STATE_ORDER(xas, xa, index, order); |
| 847 | unsigned int i = 0; |
| 848 | |
| 849 | do { |
| 850 | xas_lock(&xas); |
| 851 | XA_BUG_ON(xa, xas_find_conflict(&xas)); |
| 852 | xas_create_range(&xas); |
| 853 | if (xas_error(&xas)) |
| 854 | goto unlock; |
| 855 | for (i = 0; i < (1U << order); i++) { |
| 856 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(index + i))); |
| 857 | xas_next(&xas); |
| 858 | } |
| 859 | unlock: |
| 860 | xas_unlock(&xas); |
| 861 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 862 | |
| 863 | XA_BUG_ON(xa, xas_error(&xas)); |
| 864 | } |
| 865 | |
| 866 | static noinline void check_create_range_1(struct xarray *xa, |
| 867 | unsigned long index, unsigned order) |
| 868 | { |
| 869 | unsigned long i; |
| 870 | |
| 871 | xa_store_many_order(xa, index, order); |
| 872 | for (i = index; i < index + (1UL << order); i++) |
| 873 | xa_erase_index(xa, i); |
| 874 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 875 | } |
| 876 | |
| 877 | static noinline void check_create_range_2(struct xarray *xa, unsigned order) |
| 878 | { |
| 879 | unsigned long i; |
| 880 | unsigned long nr = 1UL << order; |
| 881 | |
| 882 | for (i = 0; i < nr * nr; i += nr) |
| 883 | xa_store_many_order(xa, i, order); |
| 884 | for (i = 0; i < nr * nr; i++) |
| 885 | xa_erase_index(xa, i); |
| 886 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 887 | } |
| 888 | |
| 889 | static noinline void check_create_range_3(void) |
| 890 | { |
| 891 | XA_STATE(xas, NULL, 0); |
| 892 | xas_set_err(&xas, -EEXIST); |
| 893 | xas_create_range(&xas); |
| 894 | XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST); |
| 895 | } |
| 896 | |
| 897 | static noinline void check_create_range_4(struct xarray *xa, |
| 898 | unsigned long index, unsigned order) |
| 899 | { |
| 900 | XA_STATE_ORDER(xas, xa, index, order); |
| 901 | unsigned long base = xas.xa_index; |
| 902 | unsigned long i = 0; |
| 903 | |
| 904 | xa_store_index(xa, index, GFP_KERNEL); |
| 905 | do { |
| 906 | xas_lock(&xas); |
| 907 | xas_create_range(&xas); |
| 908 | if (xas_error(&xas)) |
| 909 | goto unlock; |
| 910 | for (i = 0; i < (1UL << order); i++) { |
| 911 | void *old = xas_store(&xas, xa_mk_value(base + i)); |
| 912 | if (xas.xa_index == index) |
| 913 | XA_BUG_ON(xa, old != xa_mk_value(base + i)); |
| 914 | else |
| 915 | XA_BUG_ON(xa, old != NULL); |
| 916 | xas_next(&xas); |
| 917 | } |
| 918 | unlock: |
| 919 | xas_unlock(&xas); |
| 920 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 921 | |
| 922 | XA_BUG_ON(xa, xas_error(&xas)); |
| 923 | |
| 924 | for (i = base; i < base + (1UL << order); i++) |
| 925 | xa_erase_index(xa, i); |
| 926 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 927 | } |
| 928 | |
| 929 | static noinline void check_create_range(struct xarray *xa) |
| 930 | { |
| 931 | unsigned int order; |
| 932 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1; |
| 933 | |
| 934 | for (order = 0; order < max_order; order++) { |
| 935 | check_create_range_1(xa, 0, order); |
| 936 | check_create_range_1(xa, 1U << order, order); |
| 937 | check_create_range_1(xa, 2U << order, order); |
| 938 | check_create_range_1(xa, 3U << order, order); |
| 939 | check_create_range_1(xa, 1U << 24, order); |
| 940 | if (order < 10) |
| 941 | check_create_range_2(xa, order); |
| 942 | |
| 943 | check_create_range_4(xa, 0, order); |
| 944 | check_create_range_4(xa, 1U << order, order); |
| 945 | check_create_range_4(xa, 2U << order, order); |
| 946 | check_create_range_4(xa, 3U << order, order); |
| 947 | check_create_range_4(xa, 1U << 24, order); |
| 948 | |
| 949 | check_create_range_4(xa, 1, order); |
| 950 | check_create_range_4(xa, (1U << order) + 1, order); |
| 951 | check_create_range_4(xa, (2U << order) + 1, order); |
| 952 | check_create_range_4(xa, (2U << order) - 1, order); |
| 953 | check_create_range_4(xa, (3U << order) + 1, order); |
| 954 | check_create_range_4(xa, (3U << order) - 1, order); |
| 955 | check_create_range_4(xa, (1U << 24) + 1, order); |
| 956 | } |
| 957 | |
| 958 | check_create_range_3(); |
| 959 | } |
| 960 | |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 961 | static LIST_HEAD(shadow_nodes); |
| 962 | |
| 963 | static void test_update_node(struct xa_node *node) |
| 964 | { |
| 965 | if (node->count && node->count == node->nr_values) { |
| 966 | if (list_empty(&node->private_list)) |
| 967 | list_add(&shadow_nodes, &node->private_list); |
| 968 | } else { |
| 969 | if (!list_empty(&node->private_list)) |
| 970 | list_del_init(&node->private_list); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | static noinline void shadow_remove(struct xarray *xa) |
| 975 | { |
| 976 | struct xa_node *node; |
| 977 | |
| 978 | xa_lock(xa); |
| 979 | while ((node = list_first_entry_or_null(&shadow_nodes, |
| 980 | struct xa_node, private_list))) { |
| 981 | XA_STATE(xas, node->array, 0); |
| 982 | XA_BUG_ON(xa, node->array != xa); |
| 983 | list_del_init(&node->private_list); |
| 984 | xas.xa_node = xa_parent_locked(node->array, node); |
| 985 | xas.xa_offset = node->offset; |
| 986 | xas.xa_shift = node->shift + XA_CHUNK_SHIFT; |
| 987 | xas_set_update(&xas, test_update_node); |
| 988 | xas_store(&xas, NULL); |
| 989 | } |
| 990 | xa_unlock(xa); |
| 991 | } |
| 992 | |
| 993 | static noinline void check_workingset(struct xarray *xa, unsigned long index) |
| 994 | { |
| 995 | XA_STATE(xas, xa, index); |
| 996 | xas_set_update(&xas, test_update_node); |
| 997 | |
| 998 | do { |
| 999 | xas_lock(&xas); |
| 1000 | xas_store(&xas, xa_mk_value(0)); |
| 1001 | xas_next(&xas); |
| 1002 | xas_store(&xas, xa_mk_value(1)); |
| 1003 | xas_unlock(&xas); |
| 1004 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 1005 | |
| 1006 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); |
| 1007 | |
| 1008 | xas_lock(&xas); |
| 1009 | xas_next(&xas); |
| 1010 | xas_store(&xas, &xas); |
| 1011 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); |
| 1012 | |
| 1013 | xas_store(&xas, xa_mk_value(2)); |
| 1014 | xas_unlock(&xas); |
| 1015 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); |
| 1016 | |
| 1017 | shadow_remove(xa); |
| 1018 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); |
| 1019 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1020 | } |
| 1021 | |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame^] | 1022 | /* |
| 1023 | * Check that the pointer / value / sibling entries are accounted the |
| 1024 | * way we expect them to be. |
| 1025 | */ |
| 1026 | static noinline void check_account(struct xarray *xa) |
| 1027 | { |
| 1028 | #ifdef CONFIG_XARRAY_MULTI |
| 1029 | unsigned int order; |
| 1030 | |
| 1031 | for (order = 1; order < 12; order++) { |
| 1032 | XA_STATE(xas, xa, 1 << order); |
| 1033 | |
| 1034 | xa_store_order(xa, 0, order, xa, GFP_KERNEL); |
| 1035 | xas_load(&xas); |
| 1036 | XA_BUG_ON(xa, xas.xa_node->count == 0); |
| 1037 | XA_BUG_ON(xa, xas.xa_node->count > (1 << order)); |
| 1038 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); |
| 1039 | |
| 1040 | xa_store_order(xa, 1 << order, order, xa_mk_value(1 << order), |
| 1041 | GFP_KERNEL); |
| 1042 | XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2); |
| 1043 | |
| 1044 | xa_erase(xa, 1 << order); |
| 1045 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); |
| 1046 | |
| 1047 | xa_erase(xa, 0); |
| 1048 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1049 | } |
| 1050 | #endif |
| 1051 | } |
| 1052 | |
Matthew Wilcox | 687149f | 2017-11-17 08:16:34 -0500 | [diff] [blame] | 1053 | static noinline void check_destroy(struct xarray *xa) |
| 1054 | { |
| 1055 | unsigned long index; |
| 1056 | |
| 1057 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1058 | |
| 1059 | /* Destroying an empty array is a no-op */ |
| 1060 | xa_destroy(xa); |
| 1061 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1062 | |
| 1063 | /* Destroying an array with a single entry */ |
| 1064 | for (index = 0; index < 1000; index++) { |
| 1065 | xa_store_index(xa, index, GFP_KERNEL); |
| 1066 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1067 | xa_destroy(xa); |
| 1068 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1069 | } |
| 1070 | |
| 1071 | /* Destroying an array with a single entry at ULONG_MAX */ |
| 1072 | xa_store(xa, ULONG_MAX, xa, GFP_KERNEL); |
| 1073 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1074 | xa_destroy(xa); |
| 1075 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1076 | |
| 1077 | #ifdef CONFIG_XARRAY_MULTI |
| 1078 | /* Destroying an array with a multi-index entry */ |
| 1079 | xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL); |
| 1080 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1081 | xa_destroy(xa); |
| 1082 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1083 | #endif |
| 1084 | } |
| 1085 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1086 | static DEFINE_XARRAY(array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1087 | |
| 1088 | static int xarray_checks(void) |
| 1089 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1090 | check_xa_err(&array); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1091 | check_xas_retry(&array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1092 | check_xa_load(&array); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 1093 | check_xa_mark(&array); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1094 | check_xa_shrink(&array); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1095 | check_xas_erase(&array); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 1096 | check_cmpxchg(&array); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 1097 | check_reserve(&array); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1098 | check_multi_store(&array); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 1099 | check_xa_alloc(); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1100 | check_find(&array); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1101 | check_find_entry(&array); |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame^] | 1102 | check_account(&array); |
Matthew Wilcox | 687149f | 2017-11-17 08:16:34 -0500 | [diff] [blame] | 1103 | check_destroy(&array); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1104 | check_move(&array); |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1105 | check_create_range(&array); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 1106 | check_store_iter(&array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1107 | |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 1108 | check_workingset(&array, 0); |
| 1109 | check_workingset(&array, 64); |
| 1110 | check_workingset(&array, 4096); |
| 1111 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1112 | printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); |
| 1113 | return (tests_run == tests_passed) ? 0 : -EINVAL; |
| 1114 | } |
| 1115 | |
| 1116 | static void xarray_exit(void) |
| 1117 | { |
| 1118 | } |
| 1119 | |
| 1120 | module_init(xarray_checks); |
| 1121 | module_exit(xarray_exit); |
| 1122 | MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>"); |
| 1123 | MODULE_LICENSE("GPL"); |