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 |
Matthew Wilcox (Oracle) | c44aa5e | 2020-01-17 22:13:21 -0500 | [diff] [blame] | 5 | * Copyright (c) 2019-2020 Oracle |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 6 | * Author: Matthew Wilcox <willy@infradead.org> |
| 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 | |
Matthew Wilcox (Oracle) | bd40b17 | 2020-01-31 05:07:55 -0500 | [diff] [blame] | 15 | static const unsigned int order_limit = |
| 16 | IS_ENABLED(CONFIG_XARRAY_MULTI) ? BITS_PER_LONG : 1; |
| 17 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 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 | |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 35 | static void *xa_mk_index(unsigned long index) |
| 36 | { |
| 37 | return xa_mk_value(index & LONG_MAX); |
| 38 | } |
| 39 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 40 | static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) |
| 41 | { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 42 | return xa_store(xa, index, xa_mk_index(index), gfp); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Matthew Wilcox | 12fd2ae | 2019-03-09 22:25:27 -0500 | [diff] [blame] | 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 | |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 51 | static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp) |
| 52 | { |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 53 | u32 id; |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 54 | |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 55 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(index), xa_limit_32b, |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 56 | gfp) != 0); |
| 57 | XA_BUG_ON(xa, id != index); |
| 58 | } |
| 59 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 60 | static void xa_erase_index(struct xarray *xa, unsigned long index) |
| 61 | { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 62 | XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index)); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | bd54211 | 2019-02-04 23:12:08 -0500 | [diff] [blame] | 120 | rcu_read_unlock(); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 121 | |
| 122 | XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); |
Matthew Wilcox | bd54211 | 2019-02-04 23:12:08 -0500 | [diff] [blame] | 123 | |
| 124 | rcu_read_lock(); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 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) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 139 | xas_store(&xas, xa_mk_index(xas.xa_index)); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 140 | } |
| 141 | xas_unlock(&xas); |
| 142 | |
| 143 | xa_erase_index(xa, 0); |
| 144 | xa_erase_index(xa, 1); |
| 145 | } |
| 146 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 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 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 175 | static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index) |
| 176 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 177 | unsigned int order; |
| 178 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1; |
| 179 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | d69d287 | 2019-01-14 13:57:31 -0500 | [diff] [blame] | 215 | xa_set_mark(xa, index + 2, XA_MARK_2); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 216 | XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL)); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 217 | xa_store_order(xa, index, order, xa_mk_index(index), |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 218 | GFP_KERNEL); |
| 219 | for (i = base; i < next; i++) { |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 220 | XA_STATE(xas, xa, i); |
| 221 | unsigned int seen = 0; |
| 222 | void *entry; |
| 223 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 224 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); |
Matthew Wilcox | d69d287 | 2019-01-14 13:57:31 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 227 | |
| 228 | /* We should see two elements in the array */ |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 229 | rcu_read_lock(); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 230 | xas_for_each(&xas, entry, ULONG_MAX) |
| 231 | seen++; |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 232 | rcu_read_unlock(); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 233 | XA_BUG_ON(xa, seen != 2); |
| 234 | |
| 235 | /* One of which is marked */ |
| 236 | xas_set(&xas, 0); |
| 237 | seen = 0; |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 238 | rcu_read_lock(); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 239 | xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) |
| 240 | seen++; |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 241 | rcu_read_unlock(); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 242 | XA_BUG_ON(xa, seen != 1); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Matthew Wilcox | adb9d9c | 2018-04-09 16:52:21 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox (Oracle) | 04e9e9b | 2020-06-14 21:52:04 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | adb9d9c | 2018-04-09 16:52:21 -0400 | [diff] [blame] | 319 | |
| 320 | check_xa_mark_2(xa); |
Matthew Wilcox (Oracle) | 04e9e9b | 2020-06-14 21:52:04 -0400 | [diff] [blame] | 321 | check_xa_mark_3(xa); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 324 | static noinline void check_xa_shrink(struct xarray *xa) |
| 325 | { |
| 326 | XA_STATE(xas, xa, 1); |
| 327 | struct xa_node *node; |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 328 | unsigned int order; |
| 329 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1; |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 93eb07f | 2018-09-08 12:09:52 -0400 | [diff] [blame] | 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 | } |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 371 | } |
| 372 | |
Matthew Wilcox | 12fd2ae | 2019-03-09 22:25:27 -0500 | [diff] [blame] | 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 | |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | fd9dc93 | 2019-02-06 13:07:11 -0500 | [diff] [blame] | 412 | XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 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); |
Matthew Wilcox (Oracle) | 062b735 | 2020-03-31 14:23:59 -0400 | [diff] [blame] | 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); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 421 | xa_erase_index(xa, 12345678); |
| 422 | xa_erase_index(xa, 5); |
| 423 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 424 | } |
| 425 | |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 426 | static noinline void check_reserve(struct xarray *xa) |
| 427 | { |
| 428 | void *entry; |
Matthew Wilcox | 4a31896 | 2018-12-17 14:45:36 -0500 | [diff] [blame] | 429 | unsigned long index; |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 430 | int count; |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 431 | |
| 432 | /* An array with a reserved entry is not empty */ |
| 433 | XA_BUG_ON(xa, !xa_empty(xa)); |
Matthew Wilcox | f818b82 | 2019-02-08 14:02:45 -0500 | [diff] [blame] | 434 | XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 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 */ |
Matthew Wilcox | f818b82 | 2019-02-08 14:02:45 -0500 | [diff] [blame] | 441 | XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 447 | /* cmpxchg sees a reserved entry as ZERO */ |
Matthew Wilcox | f818b82 | 2019-02-08 14:02:45 -0500 | [diff] [blame] | 448 | XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 449 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, XA_ZERO_ENTRY, |
| 450 | xa_mk_value(12345678), GFP_NOWAIT) != NULL); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 451 | xa_release(xa, 12345678); |
| 452 | xa_erase_index(xa, 12345678); |
| 453 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 454 | |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 455 | /* xa_insert treats it as busy */ |
Matthew Wilcox | f818b82 | 2019-02-08 14:02:45 -0500 | [diff] [blame] | 456 | XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0); |
Matthew Wilcox | b0606fe | 2019-01-02 13:57:03 -0500 | [diff] [blame] | 457 | XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) != |
Matthew Wilcox | fd9dc93 | 2019-02-06 13:07:11 -0500 | [diff] [blame] | 458 | -EBUSY); |
Matthew Wilcox | b0606fe | 2019-01-02 13:57:03 -0500 | [diff] [blame] | 459 | XA_BUG_ON(xa, xa_empty(xa)); |
| 460 | XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL); |
Matthew Wilcox | 4c0608f | 2018-10-30 09:45:55 -0400 | [diff] [blame] | 461 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 462 | |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 463 | /* Can iterate through a reserved entry */ |
| 464 | xa_store_index(xa, 5, GFP_KERNEL); |
Matthew Wilcox | f818b82 | 2019-02-08 14:02:45 -0500 | [diff] [blame] | 465 | XA_BUG_ON(xa, xa_reserve(xa, 6, GFP_KERNEL) != 0); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 466 | xa_store_index(xa, 7, GFP_KERNEL); |
| 467 | |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 468 | count = 0; |
Matthew Wilcox | 4a31896 | 2018-12-17 14:45:36 -0500 | [diff] [blame] | 469 | xa_for_each(xa, index, entry) { |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 470 | XA_BUG_ON(xa, index != 5 && index != 7); |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 471 | count++; |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 472 | } |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 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 | |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 489 | xa_destroy(xa); |
| 490 | } |
| 491 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 503 | xas_store(&xas, xa_mk_index(j)); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 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) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 521 | XA_BUG_ON(xa, entry != xa_mk_index(j)); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 522 | xas_store(&xas, NULL); |
| 523 | j++; |
| 524 | } |
| 525 | xas_unlock(&xas); |
| 526 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 527 | } |
| 528 | } |
| 529 | |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 541 | XA_BUG_ON(xa, xa_load(xa, max) != NULL); |
| 542 | XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL); |
| 543 | |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 544 | xas_lock(&xas); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 545 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index)); |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 546 | xas_unlock(&xas); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 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)); |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 562 | xas_lock(&xas); |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 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)); |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 566 | xas_unlock(&xas); |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 567 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 568 | } |
Matthew Wilcox | 4f145cd | 2018-11-29 16:04:35 -0500 | [diff] [blame] | 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 | } |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 595 | #endif |
| 596 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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 */ |
Matthew Wilcox | 5404a7f | 2018-11-05 09:34:04 -0500 | [diff] [blame] | 636 | xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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++) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 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); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 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 |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 655 | XA_BUG_ON(xa, entry != xa_mk_index(j)); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | xa_erase(xa, 0); |
| 659 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 660 | } |
| 661 | } |
Matthew Wilcox | 4f06d63 | 2018-09-09 01:52:17 -0400 | [diff] [blame] | 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); |
Matthew Wilcox | 4f145cd | 2018-11-29 16:04:35 -0500 | [diff] [blame] | 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 | } |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 674 | #endif |
| 675 | } |
| 676 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 677 | static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base) |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 678 | { |
| 679 | int i; |
| 680 | u32 id; |
| 681 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 682 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 683 | /* An empty array should assign %base to the first alloc */ |
| 684 | xa_alloc_index(xa, base, GFP_KERNEL); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 685 | |
| 686 | /* Erasing it should make the array empty again */ |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 687 | xa_erase_index(xa, base); |
| 688 | XA_BUG_ON(xa, !xa_empty(xa)); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 689 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 690 | /* And it should assign %base again */ |
| 691 | xa_alloc_index(xa, base, GFP_KERNEL); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 692 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 693 | /* Allocating and then erasing a lot should not lose base */ |
| 694 | for (i = base + 1; i < 2 * XA_CHUNK_SIZE; i++) |
| 695 | xa_alloc_index(xa, i, GFP_KERNEL); |
| 696 | for (i = base; i < 2 * XA_CHUNK_SIZE; i++) |
| 697 | xa_erase_index(xa, i); |
| 698 | xa_alloc_index(xa, base, GFP_KERNEL); |
| 699 | |
| 700 | /* Destroying the array should do the same as erasing */ |
| 701 | xa_destroy(xa); |
| 702 | |
| 703 | /* And it should assign %base again */ |
| 704 | xa_alloc_index(xa, base, GFP_KERNEL); |
| 705 | |
| 706 | /* The next assigned ID should be base+1 */ |
| 707 | xa_alloc_index(xa, base + 1, GFP_KERNEL); |
| 708 | xa_erase_index(xa, base + 1); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 709 | |
| 710 | /* Storing a value should mark it used */ |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 711 | xa_store_index(xa, base + 1, GFP_KERNEL); |
| 712 | xa_alloc_index(xa, base + 2, GFP_KERNEL); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 713 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 714 | /* If we then erase base, it should be free */ |
| 715 | xa_erase_index(xa, base); |
| 716 | xa_alloc_index(xa, base, GFP_KERNEL); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 717 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 718 | xa_erase_index(xa, base + 1); |
| 719 | xa_erase_index(xa, base + 2); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 720 | |
| 721 | for (i = 1; i < 5000; i++) { |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 722 | xa_alloc_index(xa, base + i, GFP_KERNEL); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 725 | xa_destroy(xa); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 726 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 727 | /* Check that we fail properly at the limit of allocation */ |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 728 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX - 1), |
| 729 | XA_LIMIT(UINT_MAX - 1, UINT_MAX), |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 730 | GFP_KERNEL) != 0); |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 731 | XA_BUG_ON(xa, id != 0xfffffffeU); |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 732 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX), |
| 733 | XA_LIMIT(UINT_MAX - 1, UINT_MAX), |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 734 | GFP_KERNEL) != 0); |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 735 | XA_BUG_ON(xa, id != 0xffffffffU); |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 736 | id = 3; |
| 737 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(0), |
| 738 | XA_LIMIT(UINT_MAX - 1, UINT_MAX), |
| 739 | GFP_KERNEL) != -EBUSY); |
| 740 | XA_BUG_ON(xa, id != 3); |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 741 | xa_destroy(xa); |
Matthew Wilcox | 4848361 | 2018-12-13 13:57:42 -0500 | [diff] [blame] | 742 | |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 743 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5), |
| 744 | GFP_KERNEL) != -EBUSY); |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 745 | XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0); |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 746 | XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5), |
| 747 | GFP_KERNEL) != -EBUSY); |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 748 | xa_erase_index(xa, 3); |
| 749 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 750 | } |
| 751 | |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 752 | static noinline void check_xa_alloc_2(struct xarray *xa, unsigned int base) |
| 753 | { |
| 754 | unsigned int i, id; |
| 755 | unsigned long index; |
| 756 | void *entry; |
| 757 | |
| 758 | /* Allocate and free a NULL and check xa_empty() behaves */ |
| 759 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 760 | XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); |
| 761 | XA_BUG_ON(xa, id != base); |
| 762 | XA_BUG_ON(xa, xa_empty(xa)); |
| 763 | XA_BUG_ON(xa, xa_erase(xa, id) != NULL); |
| 764 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 765 | |
| 766 | /* Ditto, but check destroy instead of erase */ |
| 767 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 768 | XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); |
| 769 | XA_BUG_ON(xa, id != base); |
| 770 | XA_BUG_ON(xa, xa_empty(xa)); |
| 771 | xa_destroy(xa); |
| 772 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 773 | |
| 774 | for (i = base; i < base + 10; i++) { |
| 775 | XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, |
| 776 | GFP_KERNEL) != 0); |
| 777 | XA_BUG_ON(xa, id != i); |
| 778 | } |
| 779 | |
| 780 | XA_BUG_ON(xa, xa_store(xa, 3, xa_mk_index(3), GFP_KERNEL) != NULL); |
| 781 | XA_BUG_ON(xa, xa_store(xa, 4, xa_mk_index(4), GFP_KERNEL) != NULL); |
| 782 | XA_BUG_ON(xa, xa_store(xa, 4, NULL, GFP_KERNEL) != xa_mk_index(4)); |
| 783 | XA_BUG_ON(xa, xa_erase(xa, 5) != NULL); |
| 784 | XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0); |
| 785 | XA_BUG_ON(xa, id != 5); |
| 786 | |
| 787 | xa_for_each(xa, index, entry) { |
| 788 | xa_erase_index(xa, index); |
| 789 | } |
| 790 | |
| 791 | for (i = base; i < base + 9; i++) { |
| 792 | XA_BUG_ON(xa, xa_erase(xa, i) != NULL); |
| 793 | XA_BUG_ON(xa, xa_empty(xa)); |
| 794 | } |
| 795 | XA_BUG_ON(xa, xa_erase(xa, 8) != NULL); |
| 796 | XA_BUG_ON(xa, xa_empty(xa)); |
| 797 | XA_BUG_ON(xa, xa_erase(xa, base + 9) != NULL); |
| 798 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 799 | |
| 800 | xa_destroy(xa); |
| 801 | } |
| 802 | |
Matthew Wilcox | 2fa044e | 2018-11-06 14:13:35 -0500 | [diff] [blame] | 803 | static noinline void check_xa_alloc_3(struct xarray *xa, unsigned int base) |
| 804 | { |
| 805 | struct xa_limit limit = XA_LIMIT(1, 0x3fff); |
| 806 | u32 next = 0; |
| 807 | unsigned int i, id; |
| 808 | unsigned long index; |
| 809 | void *entry; |
| 810 | |
| 811 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(1), limit, |
| 812 | &next, GFP_KERNEL) != 0); |
| 813 | XA_BUG_ON(xa, id != 1); |
| 814 | |
| 815 | next = 0x3ffd; |
| 816 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(0x3ffd), limit, |
| 817 | &next, GFP_KERNEL) != 0); |
| 818 | XA_BUG_ON(xa, id != 0x3ffd); |
| 819 | xa_erase_index(xa, 0x3ffd); |
| 820 | xa_erase_index(xa, 1); |
| 821 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 822 | |
| 823 | for (i = 0x3ffe; i < 0x4003; i++) { |
| 824 | if (i < 0x4000) |
| 825 | entry = xa_mk_index(i); |
| 826 | else |
| 827 | entry = xa_mk_index(i - 0x3fff); |
| 828 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, entry, limit, |
| 829 | &next, GFP_KERNEL) != (id == 1)); |
| 830 | XA_BUG_ON(xa, xa_mk_index(id) != entry); |
| 831 | } |
| 832 | |
| 833 | /* Check wrap-around is handled correctly */ |
| 834 | if (base != 0) |
| 835 | xa_erase_index(xa, base); |
| 836 | xa_erase_index(xa, base + 1); |
| 837 | next = UINT_MAX; |
| 838 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(UINT_MAX), |
| 839 | xa_limit_32b, &next, GFP_KERNEL) != 0); |
| 840 | XA_BUG_ON(xa, id != UINT_MAX); |
| 841 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base), |
| 842 | xa_limit_32b, &next, GFP_KERNEL) != 1); |
| 843 | XA_BUG_ON(xa, id != base); |
| 844 | XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base + 1), |
| 845 | xa_limit_32b, &next, GFP_KERNEL) != 0); |
| 846 | XA_BUG_ON(xa, id != base + 1); |
| 847 | |
| 848 | xa_for_each(xa, index, entry) |
| 849 | xa_erase_index(xa, index); |
| 850 | |
| 851 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 852 | } |
| 853 | |
Matthew Wilcox | 3ccaf57 | 2018-10-26 14:43:22 -0400 | [diff] [blame] | 854 | static DEFINE_XARRAY_ALLOC(xa0); |
| 855 | static DEFINE_XARRAY_ALLOC1(xa1); |
| 856 | |
| 857 | static noinline void check_xa_alloc(void) |
| 858 | { |
| 859 | check_xa_alloc_1(&xa0, 0); |
| 860 | check_xa_alloc_1(&xa1, 1); |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 861 | check_xa_alloc_2(&xa0, 0); |
| 862 | check_xa_alloc_2(&xa1, 1); |
Matthew Wilcox | 2fa044e | 2018-11-06 14:13:35 -0500 | [diff] [blame] | 863 | check_xa_alloc_3(&xa0, 0); |
| 864 | check_xa_alloc_3(&xa1, 1); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 865 | } |
| 866 | |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 867 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, |
| 868 | unsigned int order, unsigned int present) |
| 869 | { |
| 870 | XA_STATE_ORDER(xas, xa, start, order); |
| 871 | void *entry; |
| 872 | unsigned int count = 0; |
| 873 | |
| 874 | retry: |
| 875 | xas_lock(&xas); |
| 876 | xas_for_each_conflict(&xas, entry) { |
| 877 | XA_BUG_ON(xa, !xa_is_value(entry)); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 878 | XA_BUG_ON(xa, entry < xa_mk_index(start)); |
| 879 | XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1)); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 880 | count++; |
| 881 | } |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 882 | xas_store(&xas, xa_mk_index(start)); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 883 | xas_unlock(&xas); |
| 884 | if (xas_nomem(&xas, GFP_KERNEL)) { |
| 885 | count = 0; |
| 886 | goto retry; |
| 887 | } |
| 888 | XA_BUG_ON(xa, xas_error(&xas)); |
| 889 | XA_BUG_ON(xa, count != present); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 890 | XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start)); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 891 | XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) != |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 892 | xa_mk_index(start)); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 893 | xa_erase_index(xa, start); |
| 894 | } |
| 895 | |
| 896 | static noinline void check_store_iter(struct xarray *xa) |
| 897 | { |
| 898 | unsigned int i, j; |
| 899 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; |
| 900 | |
| 901 | for (i = 0; i < max_order; i++) { |
| 902 | unsigned int min = 1 << i; |
| 903 | unsigned int max = (2 << i) - 1; |
| 904 | __check_store_iter(xa, 0, i, 0); |
| 905 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 906 | __check_store_iter(xa, min, i, 0); |
| 907 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 908 | |
| 909 | xa_store_index(xa, min, GFP_KERNEL); |
| 910 | __check_store_iter(xa, min, i, 1); |
| 911 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 912 | xa_store_index(xa, max, GFP_KERNEL); |
| 913 | __check_store_iter(xa, min, i, 1); |
| 914 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 915 | |
| 916 | for (j = 0; j < min; j++) |
| 917 | xa_store_index(xa, j, GFP_KERNEL); |
| 918 | __check_store_iter(xa, 0, i, min); |
| 919 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 920 | for (j = 0; j < min; j++) |
| 921 | xa_store_index(xa, min + j, GFP_KERNEL); |
| 922 | __check_store_iter(xa, min, i, min); |
| 923 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 924 | } |
| 925 | #ifdef CONFIG_XARRAY_MULTI |
| 926 | xa_store_index(xa, 63, GFP_KERNEL); |
| 927 | xa_store_index(xa, 65, GFP_KERNEL); |
| 928 | __check_store_iter(xa, 64, 2, 1); |
| 929 | xa_erase_index(xa, 63); |
| 930 | #endif |
| 931 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 932 | } |
| 933 | |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 934 | static noinline void check_multi_find_1(struct xarray *xa, unsigned order) |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 935 | { |
| 936 | #ifdef CONFIG_XARRAY_MULTI |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 937 | unsigned long multi = 3 << order; |
| 938 | unsigned long next = 4 << order; |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 939 | unsigned long index; |
| 940 | |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 941 | xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL); |
| 942 | XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL); |
Matthew Wilcox (Oracle) | c44aa5e | 2020-01-17 22:13:21 -0500 | [diff] [blame] | 943 | XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 944 | |
| 945 | index = 0; |
| 946 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 947 | xa_mk_value(multi)); |
| 948 | XA_BUG_ON(xa, index != multi); |
| 949 | index = multi + 1; |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 950 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 951 | xa_mk_value(multi)); |
| 952 | XA_BUG_ON(xa, (index < multi) || (index >= next)); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 953 | XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) != |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 954 | xa_mk_value(next)); |
| 955 | XA_BUG_ON(xa, index != next); |
Matthew Wilcox (Oracle) | c44aa5e | 2020-01-17 22:13:21 -0500 | [diff] [blame] | 956 | XA_BUG_ON(xa, xa_find_after(xa, &index, next, XA_PRESENT) != NULL); |
| 957 | XA_BUG_ON(xa, index != next); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 958 | |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 959 | xa_erase_index(xa, multi); |
| 960 | xa_erase_index(xa, next); |
Matthew Wilcox (Oracle) | c44aa5e | 2020-01-17 22:13:21 -0500 | [diff] [blame] | 961 | xa_erase_index(xa, next + 1); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 962 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 963 | #endif |
| 964 | } |
| 965 | |
| 966 | static noinline void check_multi_find_2(struct xarray *xa) |
| 967 | { |
| 968 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1; |
| 969 | unsigned int i, j; |
| 970 | void *entry; |
| 971 | |
| 972 | for (i = 0; i < max_order; i++) { |
| 973 | unsigned long index = 1UL << i; |
| 974 | for (j = 0; j < index; j++) { |
| 975 | XA_STATE(xas, xa, j + index); |
| 976 | xa_store_index(xa, index - 1, GFP_KERNEL); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 977 | xa_store_order(xa, index, i, xa_mk_index(index), |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 978 | GFP_KERNEL); |
| 979 | rcu_read_lock(); |
| 980 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 981 | xa_erase_index(xa, index); |
| 982 | } |
| 983 | rcu_read_unlock(); |
| 984 | xa_erase_index(xa, index - 1); |
| 985 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
Matthew Wilcox (Oracle) | bd40b17 | 2020-01-31 05:07:55 -0500 | [diff] [blame] | 990 | static noinline void check_multi_find_3(struct xarray *xa) |
| 991 | { |
| 992 | unsigned int order; |
| 993 | |
| 994 | for (order = 5; order < order_limit; order++) { |
| 995 | unsigned long index = 1UL << (order - 5); |
| 996 | |
| 997 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 998 | xa_store_order(xa, 0, order - 4, xa_mk_index(0), GFP_KERNEL); |
| 999 | XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT)); |
| 1000 | xa_erase_index(xa, 0); |
| 1001 | } |
| 1002 | } |
| 1003 | |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1004 | static noinline void check_find_1(struct xarray *xa) |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1005 | { |
| 1006 | unsigned long i, j, k; |
| 1007 | |
| 1008 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1009 | |
| 1010 | /* |
| 1011 | * Check xa_find with all pairs between 0 and 99 inclusive, |
| 1012 | * starting at every index between 0 and 99 |
| 1013 | */ |
| 1014 | for (i = 0; i < 100; i++) { |
| 1015 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); |
| 1016 | xa_set_mark(xa, i, XA_MARK_0); |
| 1017 | for (j = 0; j < i; j++) { |
| 1018 | XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) != |
| 1019 | NULL); |
| 1020 | xa_set_mark(xa, j, XA_MARK_0); |
| 1021 | for (k = 0; k < 100; k++) { |
| 1022 | unsigned long index = k; |
| 1023 | void *entry = xa_find(xa, &index, ULONG_MAX, |
| 1024 | XA_PRESENT); |
| 1025 | if (k <= j) |
| 1026 | XA_BUG_ON(xa, index != j); |
| 1027 | else if (k <= i) |
| 1028 | XA_BUG_ON(xa, index != i); |
| 1029 | else |
| 1030 | XA_BUG_ON(xa, entry != NULL); |
| 1031 | |
| 1032 | index = k; |
| 1033 | entry = xa_find(xa, &index, ULONG_MAX, |
| 1034 | XA_MARK_0); |
| 1035 | if (k <= j) |
| 1036 | XA_BUG_ON(xa, index != j); |
| 1037 | else if (k <= i) |
| 1038 | XA_BUG_ON(xa, index != i); |
| 1039 | else |
| 1040 | XA_BUG_ON(xa, entry != NULL); |
| 1041 | } |
| 1042 | xa_erase_index(xa, j); |
| 1043 | XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0)); |
| 1044 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); |
| 1045 | } |
| 1046 | xa_erase_index(xa, i); |
| 1047 | XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0)); |
| 1048 | } |
| 1049 | XA_BUG_ON(xa, !xa_empty(xa)); |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static noinline void check_find_2(struct xarray *xa) |
| 1053 | { |
| 1054 | void *entry; |
Matthew Wilcox | 4a31896 | 2018-12-17 14:45:36 -0500 | [diff] [blame] | 1055 | unsigned long i, j, index; |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1056 | |
Matthew Wilcox | 4a31896 | 2018-12-17 14:45:36 -0500 | [diff] [blame] | 1057 | xa_for_each(xa, index, entry) { |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1058 | XA_BUG_ON(xa, true); |
| 1059 | } |
| 1060 | |
| 1061 | for (i = 0; i < 1024; i++) { |
| 1062 | xa_store_index(xa, index, GFP_KERNEL); |
| 1063 | j = 0; |
Matthew Wilcox | 4a31896 | 2018-12-17 14:45:36 -0500 | [diff] [blame] | 1064 | xa_for_each(xa, index, entry) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1065 | XA_BUG_ON(xa, xa_mk_index(index) != entry); |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1066 | XA_BUG_ON(xa, index != j++); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | xa_destroy(xa); |
| 1071 | } |
| 1072 | |
Matthew Wilcox | 4848361 | 2018-12-13 13:57:42 -0500 | [diff] [blame] | 1073 | static noinline void check_find_3(struct xarray *xa) |
| 1074 | { |
| 1075 | XA_STATE(xas, xa, 0); |
| 1076 | unsigned long i, j, k; |
| 1077 | void *entry; |
| 1078 | |
| 1079 | for (i = 0; i < 100; i++) { |
| 1080 | for (j = 0; j < 100; j++) { |
Matthew Wilcox | 490fd30f | 2018-12-17 17:37:25 -0500 | [diff] [blame] | 1081 | rcu_read_lock(); |
Matthew Wilcox | 4848361 | 2018-12-13 13:57:42 -0500 | [diff] [blame] | 1082 | for (k = 0; k < 100; k++) { |
| 1083 | xas_set(&xas, j); |
| 1084 | xas_for_each_marked(&xas, entry, k, XA_MARK_0) |
| 1085 | ; |
| 1086 | if (j > k) |
| 1087 | XA_BUG_ON(xa, |
| 1088 | xas.xa_node != XAS_RESTART); |
| 1089 | } |
Matthew Wilcox | 490fd30f | 2018-12-17 17:37:25 -0500 | [diff] [blame] | 1090 | rcu_read_unlock(); |
Matthew Wilcox | 4848361 | 2018-12-13 13:57:42 -0500 | [diff] [blame] | 1091 | } |
| 1092 | xa_store_index(xa, i, GFP_KERNEL); |
| 1093 | xa_set_mark(xa, i, XA_MARK_0); |
| 1094 | } |
| 1095 | xa_destroy(xa); |
| 1096 | } |
| 1097 | |
Matthew Wilcox (Oracle) | 430f24f | 2020-01-17 17:45:12 -0500 | [diff] [blame] | 1098 | static noinline void check_find_4(struct xarray *xa) |
| 1099 | { |
| 1100 | unsigned long index = 0; |
| 1101 | void *entry; |
| 1102 | |
| 1103 | xa_store_index(xa, ULONG_MAX, GFP_KERNEL); |
| 1104 | |
| 1105 | entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT); |
| 1106 | XA_BUG_ON(xa, entry != xa_mk_index(ULONG_MAX)); |
| 1107 | |
| 1108 | entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT); |
| 1109 | XA_BUG_ON(xa, entry); |
| 1110 | |
| 1111 | xa_erase_index(xa, ULONG_MAX); |
| 1112 | } |
| 1113 | |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1114 | static noinline void check_find(struct xarray *xa) |
| 1115 | { |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 1116 | unsigned i; |
| 1117 | |
Matthew Wilcox | 8229706e | 2018-11-01 16:55:19 -0400 | [diff] [blame] | 1118 | check_find_1(xa); |
| 1119 | check_find_2(xa); |
Matthew Wilcox | 4848361 | 2018-12-13 13:57:42 -0500 | [diff] [blame] | 1120 | check_find_3(xa); |
Matthew Wilcox (Oracle) | 430f24f | 2020-01-17 17:45:12 -0500 | [diff] [blame] | 1121 | check_find_4(xa); |
Matthew Wilcox (Oracle) | 19c30f4 | 2020-01-17 22:00:41 -0500 | [diff] [blame] | 1122 | |
| 1123 | for (i = 2; i < 10; i++) |
| 1124 | check_multi_find_1(xa, i); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1125 | check_multi_find_2(xa); |
Matthew Wilcox (Oracle) | bd40b17 | 2020-01-31 05:07:55 -0500 | [diff] [blame] | 1126 | check_multi_find_3(xa); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1127 | } |
| 1128 | |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1129 | /* See find_swap_entry() in mm/shmem.c */ |
| 1130 | static noinline unsigned long xa_find_entry(struct xarray *xa, void *item) |
| 1131 | { |
| 1132 | XA_STATE(xas, xa, 0); |
| 1133 | unsigned int checked = 0; |
| 1134 | void *entry; |
| 1135 | |
| 1136 | rcu_read_lock(); |
| 1137 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 1138 | if (xas_retry(&xas, entry)) |
| 1139 | continue; |
| 1140 | if (entry == item) |
| 1141 | break; |
| 1142 | checked++; |
| 1143 | if ((checked % 4) != 0) |
| 1144 | continue; |
| 1145 | xas_pause(&xas); |
| 1146 | } |
| 1147 | rcu_read_unlock(); |
| 1148 | |
| 1149 | return entry ? xas.xa_index : -1; |
| 1150 | } |
| 1151 | |
| 1152 | static noinline void check_find_entry(struct xarray *xa) |
| 1153 | { |
| 1154 | #ifdef CONFIG_XARRAY_MULTI |
| 1155 | unsigned int order; |
| 1156 | unsigned long offset, index; |
| 1157 | |
| 1158 | for (order = 0; order < 20; order++) { |
| 1159 | for (offset = 0; offset < (1UL << (order + 3)); |
| 1160 | offset += (1UL << order)) { |
| 1161 | for (index = 0; index < (1UL << (order + 5)); |
| 1162 | index += (1UL << order)) { |
| 1163 | xa_store_order(xa, index, order, |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1164 | xa_mk_index(index), GFP_KERNEL); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1165 | XA_BUG_ON(xa, xa_load(xa, index) != |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1166 | xa_mk_index(index)); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1167 | XA_BUG_ON(xa, xa_find_entry(xa, |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1168 | xa_mk_index(index)) != index); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1169 | } |
| 1170 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
| 1171 | xa_destroy(xa); |
| 1172 | } |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
| 1176 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
| 1177 | xa_store_index(xa, ULONG_MAX, GFP_KERNEL); |
| 1178 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1179 | XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1180 | xa_erase_index(xa, ULONG_MAX); |
| 1181 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1182 | } |
| 1183 | |
Matthew Wilcox (Oracle) | c36d451 | 2020-01-31 06:17:09 -0500 | [diff] [blame] | 1184 | static noinline void check_pause(struct xarray *xa) |
| 1185 | { |
| 1186 | XA_STATE(xas, xa, 0); |
| 1187 | void *entry; |
| 1188 | unsigned int order; |
| 1189 | unsigned long index = 1; |
| 1190 | unsigned int count = 0; |
| 1191 | |
| 1192 | for (order = 0; order < order_limit; order++) { |
| 1193 | XA_BUG_ON(xa, xa_store_order(xa, index, order, |
| 1194 | xa_mk_index(index), GFP_KERNEL)); |
| 1195 | index += 1UL << order; |
| 1196 | } |
| 1197 | |
| 1198 | rcu_read_lock(); |
| 1199 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 1200 | XA_BUG_ON(xa, entry != xa_mk_index(1UL << count)); |
| 1201 | count++; |
| 1202 | } |
| 1203 | rcu_read_unlock(); |
| 1204 | XA_BUG_ON(xa, count != order_limit); |
| 1205 | |
| 1206 | count = 0; |
| 1207 | xas_set(&xas, 0); |
| 1208 | rcu_read_lock(); |
| 1209 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 1210 | XA_BUG_ON(xa, entry != xa_mk_index(1UL << count)); |
| 1211 | count++; |
| 1212 | xas_pause(&xas); |
| 1213 | } |
| 1214 | rcu_read_unlock(); |
| 1215 | XA_BUG_ON(xa, count != order_limit); |
| 1216 | |
| 1217 | xa_destroy(xa); |
| 1218 | } |
| 1219 | |
Matthew Wilcox (Oracle) | 91abab8 | 2019-07-01 17:03:29 -0400 | [diff] [blame] | 1220 | static noinline void check_move_tiny(struct xarray *xa) |
| 1221 | { |
| 1222 | XA_STATE(xas, xa, 0); |
| 1223 | |
| 1224 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1225 | rcu_read_lock(); |
| 1226 | XA_BUG_ON(xa, xas_next(&xas) != NULL); |
| 1227 | XA_BUG_ON(xa, xas_next(&xas) != NULL); |
| 1228 | rcu_read_unlock(); |
| 1229 | xa_store_index(xa, 0, GFP_KERNEL); |
| 1230 | rcu_read_lock(); |
| 1231 | xas_set(&xas, 0); |
| 1232 | XA_BUG_ON(xa, xas_next(&xas) != xa_mk_index(0)); |
| 1233 | XA_BUG_ON(xa, xas_next(&xas) != NULL); |
| 1234 | xas_set(&xas, 0); |
| 1235 | XA_BUG_ON(xa, xas_prev(&xas) != xa_mk_index(0)); |
| 1236 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 1237 | rcu_read_unlock(); |
| 1238 | xa_erase_index(xa, 0); |
| 1239 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1240 | } |
| 1241 | |
Matthew Wilcox (Oracle) | 82a2231 | 2019-11-07 22:49:11 -0500 | [diff] [blame] | 1242 | static noinline void check_move_max(struct xarray *xa) |
| 1243 | { |
| 1244 | XA_STATE(xas, xa, 0); |
| 1245 | |
| 1246 | xa_store_index(xa, ULONG_MAX, GFP_KERNEL); |
| 1247 | rcu_read_lock(); |
| 1248 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX)); |
| 1249 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL); |
| 1250 | rcu_read_unlock(); |
| 1251 | |
| 1252 | xas_set(&xas, 0); |
| 1253 | rcu_read_lock(); |
| 1254 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX)); |
| 1255 | xas_pause(&xas); |
| 1256 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL); |
| 1257 | rcu_read_unlock(); |
| 1258 | |
| 1259 | xa_erase_index(xa, ULONG_MAX); |
| 1260 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1261 | } |
| 1262 | |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1263 | static noinline void check_move_small(struct xarray *xa, unsigned long idx) |
| 1264 | { |
| 1265 | XA_STATE(xas, xa, 0); |
| 1266 | unsigned long i; |
| 1267 | |
| 1268 | xa_store_index(xa, 0, GFP_KERNEL); |
| 1269 | xa_store_index(xa, idx, GFP_KERNEL); |
| 1270 | |
| 1271 | rcu_read_lock(); |
| 1272 | for (i = 0; i < idx * 4; i++) { |
| 1273 | void *entry = xas_next(&xas); |
| 1274 | if (i <= idx) |
| 1275 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); |
| 1276 | XA_BUG_ON(xa, xas.xa_index != i); |
| 1277 | if (i == 0 || i == idx) |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1278 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1279 | else |
| 1280 | XA_BUG_ON(xa, entry != NULL); |
| 1281 | } |
| 1282 | xas_next(&xas); |
| 1283 | XA_BUG_ON(xa, xas.xa_index != i); |
| 1284 | |
| 1285 | do { |
| 1286 | void *entry = xas_prev(&xas); |
| 1287 | i--; |
| 1288 | if (i <= idx) |
| 1289 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); |
| 1290 | XA_BUG_ON(xa, xas.xa_index != i); |
| 1291 | if (i == 0 || i == idx) |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1292 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1293 | else |
| 1294 | XA_BUG_ON(xa, entry != NULL); |
| 1295 | } while (i > 0); |
| 1296 | |
| 1297 | xas_set(&xas, ULONG_MAX); |
| 1298 | XA_BUG_ON(xa, xas_next(&xas) != NULL); |
| 1299 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 1300 | XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0)); |
| 1301 | XA_BUG_ON(xa, xas.xa_index != 0); |
| 1302 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 1303 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 1304 | rcu_read_unlock(); |
| 1305 | |
| 1306 | xa_erase_index(xa, 0); |
| 1307 | xa_erase_index(xa, idx); |
| 1308 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1309 | } |
| 1310 | |
| 1311 | static noinline void check_move(struct xarray *xa) |
| 1312 | { |
| 1313 | XA_STATE(xas, xa, (1 << 16) - 1); |
| 1314 | unsigned long i; |
| 1315 | |
| 1316 | for (i = 0; i < (1 << 16); i++) |
| 1317 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); |
| 1318 | |
| 1319 | rcu_read_lock(); |
| 1320 | do { |
| 1321 | void *entry = xas_prev(&xas); |
| 1322 | i--; |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1323 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1324 | XA_BUG_ON(xa, i != xas.xa_index); |
| 1325 | } while (i != 0); |
| 1326 | |
| 1327 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 1328 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 1329 | |
| 1330 | do { |
| 1331 | void *entry = xas_next(&xas); |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1332 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1333 | XA_BUG_ON(xa, i != xas.xa_index); |
| 1334 | i++; |
| 1335 | } while (i < (1 << 16)); |
| 1336 | rcu_read_unlock(); |
| 1337 | |
| 1338 | for (i = (1 << 8); i < (1 << 15); i++) |
| 1339 | xa_erase_index(xa, i); |
| 1340 | |
| 1341 | i = xas.xa_index; |
| 1342 | |
| 1343 | rcu_read_lock(); |
| 1344 | do { |
| 1345 | void *entry = xas_prev(&xas); |
| 1346 | i--; |
| 1347 | if ((i < (1 << 8)) || (i >= (1 << 15))) |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1348 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1349 | else |
| 1350 | XA_BUG_ON(xa, entry != NULL); |
| 1351 | XA_BUG_ON(xa, i != xas.xa_index); |
| 1352 | } while (i != 0); |
| 1353 | |
| 1354 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); |
| 1355 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); |
| 1356 | |
| 1357 | do { |
| 1358 | void *entry = xas_next(&xas); |
| 1359 | if ((i < (1 << 8)) || (i >= (1 << 15))) |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1360 | XA_BUG_ON(xa, entry != xa_mk_index(i)); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1361 | else |
| 1362 | XA_BUG_ON(xa, entry != NULL); |
| 1363 | XA_BUG_ON(xa, i != xas.xa_index); |
| 1364 | i++; |
| 1365 | } while (i < (1 << 16)); |
| 1366 | rcu_read_unlock(); |
| 1367 | |
| 1368 | xa_destroy(xa); |
| 1369 | |
Matthew Wilcox (Oracle) | 91abab8 | 2019-07-01 17:03:29 -0400 | [diff] [blame] | 1370 | check_move_tiny(xa); |
Matthew Wilcox (Oracle) | 82a2231 | 2019-11-07 22:49:11 -0500 | [diff] [blame] | 1371 | check_move_max(xa); |
Matthew Wilcox (Oracle) | 91abab8 | 2019-07-01 17:03:29 -0400 | [diff] [blame] | 1372 | |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1373 | for (i = 0; i < 16; i++) |
| 1374 | check_move_small(xa, 1UL << i); |
| 1375 | |
| 1376 | for (i = 2; i < 16; i++) |
| 1377 | check_move_small(xa, (1UL << i) - 1); |
| 1378 | } |
| 1379 | |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1380 | static noinline void xa_store_many_order(struct xarray *xa, |
| 1381 | unsigned long index, unsigned order) |
| 1382 | { |
| 1383 | XA_STATE_ORDER(xas, xa, index, order); |
| 1384 | unsigned int i = 0; |
| 1385 | |
| 1386 | do { |
| 1387 | xas_lock(&xas); |
| 1388 | XA_BUG_ON(xa, xas_find_conflict(&xas)); |
| 1389 | xas_create_range(&xas); |
| 1390 | if (xas_error(&xas)) |
| 1391 | goto unlock; |
| 1392 | for (i = 0; i < (1U << order); i++) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1393 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i))); |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1394 | xas_next(&xas); |
| 1395 | } |
| 1396 | unlock: |
| 1397 | xas_unlock(&xas); |
| 1398 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 1399 | |
| 1400 | XA_BUG_ON(xa, xas_error(&xas)); |
| 1401 | } |
| 1402 | |
| 1403 | static noinline void check_create_range_1(struct xarray *xa, |
| 1404 | unsigned long index, unsigned order) |
| 1405 | { |
| 1406 | unsigned long i; |
| 1407 | |
| 1408 | xa_store_many_order(xa, index, order); |
| 1409 | for (i = index; i < index + (1UL << order); i++) |
| 1410 | xa_erase_index(xa, i); |
| 1411 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1412 | } |
| 1413 | |
| 1414 | static noinline void check_create_range_2(struct xarray *xa, unsigned order) |
| 1415 | { |
| 1416 | unsigned long i; |
| 1417 | unsigned long nr = 1UL << order; |
| 1418 | |
| 1419 | for (i = 0; i < nr * nr; i += nr) |
| 1420 | xa_store_many_order(xa, i, order); |
| 1421 | for (i = 0; i < nr * nr; i++) |
| 1422 | xa_erase_index(xa, i); |
| 1423 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1424 | } |
| 1425 | |
| 1426 | static noinline void check_create_range_3(void) |
| 1427 | { |
| 1428 | XA_STATE(xas, NULL, 0); |
| 1429 | xas_set_err(&xas, -EEXIST); |
| 1430 | xas_create_range(&xas); |
| 1431 | XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST); |
| 1432 | } |
| 1433 | |
| 1434 | static noinline void check_create_range_4(struct xarray *xa, |
| 1435 | unsigned long index, unsigned order) |
| 1436 | { |
| 1437 | XA_STATE_ORDER(xas, xa, index, order); |
| 1438 | unsigned long base = xas.xa_index; |
| 1439 | unsigned long i = 0; |
| 1440 | |
| 1441 | xa_store_index(xa, index, GFP_KERNEL); |
| 1442 | do { |
| 1443 | xas_lock(&xas); |
| 1444 | xas_create_range(&xas); |
| 1445 | if (xas_error(&xas)) |
| 1446 | goto unlock; |
| 1447 | for (i = 0; i < (1UL << order); i++) { |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1448 | void *old = xas_store(&xas, xa_mk_index(base + i)); |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1449 | if (xas.xa_index == index) |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1450 | XA_BUG_ON(xa, old != xa_mk_index(base + i)); |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1451 | else |
| 1452 | XA_BUG_ON(xa, old != NULL); |
| 1453 | xas_next(&xas); |
| 1454 | } |
| 1455 | unlock: |
| 1456 | xas_unlock(&xas); |
| 1457 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 1458 | |
| 1459 | XA_BUG_ON(xa, xas_error(&xas)); |
| 1460 | |
| 1461 | for (i = base; i < base + (1UL << order); i++) |
| 1462 | xa_erase_index(xa, i); |
| 1463 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1464 | } |
| 1465 | |
| 1466 | static noinline void check_create_range(struct xarray *xa) |
| 1467 | { |
| 1468 | unsigned int order; |
| 1469 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1; |
| 1470 | |
| 1471 | for (order = 0; order < max_order; order++) { |
| 1472 | check_create_range_1(xa, 0, order); |
| 1473 | check_create_range_1(xa, 1U << order, order); |
| 1474 | check_create_range_1(xa, 2U << order, order); |
| 1475 | check_create_range_1(xa, 3U << order, order); |
| 1476 | check_create_range_1(xa, 1U << 24, order); |
| 1477 | if (order < 10) |
| 1478 | check_create_range_2(xa, order); |
| 1479 | |
| 1480 | check_create_range_4(xa, 0, order); |
| 1481 | check_create_range_4(xa, 1U << order, order); |
| 1482 | check_create_range_4(xa, 2U << order, order); |
| 1483 | check_create_range_4(xa, 3U << order, order); |
| 1484 | check_create_range_4(xa, 1U << 24, order); |
| 1485 | |
| 1486 | check_create_range_4(xa, 1, order); |
| 1487 | check_create_range_4(xa, (1U << order) + 1, order); |
| 1488 | check_create_range_4(xa, (2U << order) + 1, order); |
| 1489 | check_create_range_4(xa, (2U << order) - 1, order); |
| 1490 | check_create_range_4(xa, (3U << order) + 1, order); |
| 1491 | check_create_range_4(xa, (3U << order) - 1, order); |
| 1492 | check_create_range_4(xa, (1U << 24) + 1, order); |
| 1493 | } |
| 1494 | |
| 1495 | check_create_range_3(); |
| 1496 | } |
| 1497 | |
Matthew Wilcox | 0e9446c | 2018-08-15 14:13:29 -0400 | [diff] [blame] | 1498 | static noinline void __check_store_range(struct xarray *xa, unsigned long first, |
| 1499 | unsigned long last) |
| 1500 | { |
| 1501 | #ifdef CONFIG_XARRAY_MULTI |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1502 | xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL); |
Matthew Wilcox | 0e9446c | 2018-08-15 14:13:29 -0400 | [diff] [blame] | 1503 | |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1504 | XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first)); |
| 1505 | XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first)); |
Matthew Wilcox | 0e9446c | 2018-08-15 14:13:29 -0400 | [diff] [blame] | 1506 | XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL); |
| 1507 | XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL); |
| 1508 | |
| 1509 | xa_store_range(xa, first, last, NULL, GFP_KERNEL); |
| 1510 | #endif |
| 1511 | |
| 1512 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1513 | } |
| 1514 | |
| 1515 | static noinline void check_store_range(struct xarray *xa) |
| 1516 | { |
| 1517 | unsigned long i, j; |
| 1518 | |
| 1519 | for (i = 0; i < 128; i++) { |
| 1520 | for (j = i; j < 128; j++) { |
| 1521 | __check_store_range(xa, i, j); |
| 1522 | __check_store_range(xa, 128 + i, 128 + j); |
| 1523 | __check_store_range(xa, 4095 + i, 4095 + j); |
| 1524 | __check_store_range(xa, 4096 + i, 4096 + j); |
| 1525 | __check_store_range(xa, 123456 + i, 123456 + j); |
Matthew Wilcox | 5404a7f | 2018-11-05 09:34:04 -0500 | [diff] [blame] | 1526 | __check_store_range(xa, (1 << 24) + i, (1 << 24) + j); |
Matthew Wilcox | 0e9446c | 2018-08-15 14:13:29 -0400 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1531 | #ifdef CONFIG_XARRAY_MULTI |
| 1532 | static void check_split_1(struct xarray *xa, unsigned long index, |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1533 | unsigned int order, unsigned int new_order) |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1534 | { |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1535 | XA_STATE_ORDER(xas, xa, index, new_order); |
| 1536 | unsigned int i; |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1537 | |
| 1538 | xa_store_order(xa, index, order, xa, GFP_KERNEL); |
| 1539 | |
| 1540 | xas_split_alloc(&xas, xa, order, GFP_KERNEL); |
| 1541 | xas_lock(&xas); |
| 1542 | xas_split(&xas, xa, order); |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1543 | for (i = 0; i < (1 << order); i += (1 << new_order)) |
| 1544 | __xa_store(xa, index + i, xa_mk_index(index + i), 0); |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1545 | xas_unlock(&xas); |
| 1546 | |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1547 | for (i = 0; i < (1 << order); i++) { |
| 1548 | unsigned int val = index + (i & ~((1 << new_order) - 1)); |
| 1549 | XA_BUG_ON(xa, xa_load(xa, index + i) != xa_mk_index(val)); |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1550 | } |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1551 | |
| 1552 | xa_set_mark(xa, index, XA_MARK_0); |
| 1553 | XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); |
| 1554 | |
| 1555 | xa_destroy(xa); |
| 1556 | } |
| 1557 | |
| 1558 | static noinline void check_split(struct xarray *xa) |
| 1559 | { |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1560 | unsigned int order, new_order; |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1561 | |
| 1562 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1563 | |
| 1564 | for (order = 1; order < 2 * XA_CHUNK_SHIFT; order++) { |
Matthew Wilcox (Oracle) | 3012110 | 2020-11-19 08:32:31 -0500 | [diff] [blame] | 1565 | for (new_order = 0; new_order < order; new_order++) { |
| 1566 | check_split_1(xa, 0, order, new_order); |
| 1567 | check_split_1(xa, 1UL << order, order, new_order); |
| 1568 | check_split_1(xa, 3UL << order, order, new_order); |
| 1569 | } |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | #else |
| 1573 | static void check_split(struct xarray *xa) { } |
| 1574 | #endif |
| 1575 | |
Matthew Wilcox | 76b4e52 | 2018-12-28 23:20:44 -0500 | [diff] [blame] | 1576 | static void check_align_1(struct xarray *xa, char *name) |
| 1577 | { |
| 1578 | int i; |
| 1579 | unsigned int id; |
| 1580 | unsigned long index; |
| 1581 | void *entry; |
| 1582 | |
| 1583 | for (i = 0; i < 8; i++) { |
Matthew Wilcox | a3e4d3f | 2018-12-31 10:41:01 -0500 | [diff] [blame] | 1584 | XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b, |
| 1585 | GFP_KERNEL) != 0); |
Matthew Wilcox | 76b4e52 | 2018-12-28 23:20:44 -0500 | [diff] [blame] | 1586 | XA_BUG_ON(xa, id != i); |
| 1587 | } |
| 1588 | xa_for_each(xa, index, entry) |
| 1589 | XA_BUG_ON(xa, xa_is_err(entry)); |
| 1590 | xa_destroy(xa); |
| 1591 | } |
| 1592 | |
Matthew Wilcox | 4a5c8d8 | 2019-02-21 17:54:44 -0500 | [diff] [blame] | 1593 | /* |
| 1594 | * We should always be able to store without allocating memory after |
| 1595 | * reserving a slot. |
| 1596 | */ |
Matthew Wilcox | 2fbe967 | 2019-02-21 17:36:45 -0500 | [diff] [blame] | 1597 | static void check_align_2(struct xarray *xa, char *name) |
| 1598 | { |
| 1599 | int i; |
| 1600 | |
| 1601 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1602 | |
| 1603 | for (i = 0; i < 8; i++) { |
| 1604 | XA_BUG_ON(xa, xa_store(xa, 0, name + i, GFP_KERNEL) != NULL); |
| 1605 | xa_erase(xa, 0); |
| 1606 | } |
| 1607 | |
Matthew Wilcox | 4a5c8d8 | 2019-02-21 17:54:44 -0500 | [diff] [blame] | 1608 | for (i = 0; i < 8; i++) { |
| 1609 | XA_BUG_ON(xa, xa_reserve(xa, 0, GFP_KERNEL) != 0); |
| 1610 | XA_BUG_ON(xa, xa_store(xa, 0, name + i, 0) != NULL); |
| 1611 | xa_erase(xa, 0); |
| 1612 | } |
| 1613 | |
Matthew Wilcox | 2fbe967 | 2019-02-21 17:36:45 -0500 | [diff] [blame] | 1614 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1615 | } |
| 1616 | |
Matthew Wilcox | 76b4e52 | 2018-12-28 23:20:44 -0500 | [diff] [blame] | 1617 | static noinline void check_align(struct xarray *xa) |
| 1618 | { |
| 1619 | char name[] = "Motorola 68000"; |
| 1620 | |
| 1621 | check_align_1(xa, name); |
| 1622 | check_align_1(xa, name + 1); |
| 1623 | check_align_1(xa, name + 2); |
| 1624 | check_align_1(xa, name + 3); |
Matthew Wilcox | 2fbe967 | 2019-02-21 17:36:45 -0500 | [diff] [blame] | 1625 | check_align_2(xa, name); |
Matthew Wilcox | 76b4e52 | 2018-12-28 23:20:44 -0500 | [diff] [blame] | 1626 | } |
| 1627 | |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 1628 | static LIST_HEAD(shadow_nodes); |
| 1629 | |
| 1630 | static void test_update_node(struct xa_node *node) |
| 1631 | { |
| 1632 | if (node->count && node->count == node->nr_values) { |
| 1633 | if (list_empty(&node->private_list)) |
| 1634 | list_add(&shadow_nodes, &node->private_list); |
| 1635 | } else { |
| 1636 | if (!list_empty(&node->private_list)) |
| 1637 | list_del_init(&node->private_list); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | static noinline void shadow_remove(struct xarray *xa) |
| 1642 | { |
| 1643 | struct xa_node *node; |
| 1644 | |
| 1645 | xa_lock(xa); |
| 1646 | while ((node = list_first_entry_or_null(&shadow_nodes, |
| 1647 | struct xa_node, private_list))) { |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 1648 | XA_BUG_ON(xa, node->array != xa); |
| 1649 | list_del_init(&node->private_list); |
Matthew Wilcox (Oracle) | f82cd2f | 2020-08-18 09:05:56 -0400 | [diff] [blame] | 1650 | xa_delete_node(node, test_update_node); |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 1651 | } |
| 1652 | xa_unlock(xa); |
| 1653 | } |
| 1654 | |
| 1655 | static noinline void check_workingset(struct xarray *xa, unsigned long index) |
| 1656 | { |
| 1657 | XA_STATE(xas, xa, index); |
| 1658 | xas_set_update(&xas, test_update_node); |
| 1659 | |
| 1660 | do { |
| 1661 | xas_lock(&xas); |
| 1662 | xas_store(&xas, xa_mk_value(0)); |
| 1663 | xas_next(&xas); |
| 1664 | xas_store(&xas, xa_mk_value(1)); |
| 1665 | xas_unlock(&xas); |
| 1666 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 1667 | |
| 1668 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); |
| 1669 | |
| 1670 | xas_lock(&xas); |
| 1671 | xas_next(&xas); |
| 1672 | xas_store(&xas, &xas); |
| 1673 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); |
| 1674 | |
| 1675 | xas_store(&xas, xa_mk_value(2)); |
| 1676 | xas_unlock(&xas); |
| 1677 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); |
| 1678 | |
| 1679 | shadow_remove(xa); |
| 1680 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); |
| 1681 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1682 | } |
| 1683 | |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame] | 1684 | /* |
| 1685 | * Check that the pointer / value / sibling entries are accounted the |
| 1686 | * way we expect them to be. |
| 1687 | */ |
| 1688 | static noinline void check_account(struct xarray *xa) |
| 1689 | { |
| 1690 | #ifdef CONFIG_XARRAY_MULTI |
| 1691 | unsigned int order; |
| 1692 | |
| 1693 | for (order = 1; order < 12; order++) { |
| 1694 | XA_STATE(xas, xa, 1 << order); |
| 1695 | |
| 1696 | xa_store_order(xa, 0, order, xa, GFP_KERNEL); |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 1697 | rcu_read_lock(); |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame] | 1698 | xas_load(&xas); |
| 1699 | XA_BUG_ON(xa, xas.xa_node->count == 0); |
| 1700 | XA_BUG_ON(xa, xas.xa_node->count > (1 << order)); |
| 1701 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); |
Matthew Wilcox | fffc9a2 | 2018-11-19 09:36:29 -0500 | [diff] [blame] | 1702 | rcu_read_unlock(); |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame] | 1703 | |
Matthew Wilcox | b7677a1 | 2018-11-05 13:19:54 -0500 | [diff] [blame] | 1704 | xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order), |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame] | 1705 | GFP_KERNEL); |
| 1706 | XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2); |
| 1707 | |
| 1708 | xa_erase(xa, 1 << order); |
| 1709 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); |
| 1710 | |
| 1711 | xa_erase(xa, 0); |
| 1712 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1713 | } |
| 1714 | #endif |
| 1715 | } |
| 1716 | |
Matthew Wilcox (Oracle) | 57417ce | 2020-10-15 20:05:13 -0700 | [diff] [blame] | 1717 | static noinline void check_get_order(struct xarray *xa) |
| 1718 | { |
| 1719 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; |
| 1720 | unsigned int order; |
| 1721 | unsigned long i, j; |
| 1722 | |
| 1723 | for (i = 0; i < 3; i++) |
| 1724 | XA_BUG_ON(xa, xa_get_order(xa, i) != 0); |
| 1725 | |
| 1726 | for (order = 0; order < max_order; order++) { |
| 1727 | for (i = 0; i < 10; i++) { |
| 1728 | xa_store_order(xa, i << order, order, |
| 1729 | xa_mk_index(i << order), GFP_KERNEL); |
| 1730 | for (j = i << order; j < (i + 1) << order; j++) |
| 1731 | XA_BUG_ON(xa, xa_get_order(xa, j) != order); |
| 1732 | xa_erase(xa, i << order); |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
Matthew Wilcox | 687149f | 2017-11-17 08:16:34 -0500 | [diff] [blame] | 1737 | static noinline void check_destroy(struct xarray *xa) |
| 1738 | { |
| 1739 | unsigned long index; |
| 1740 | |
| 1741 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1742 | |
| 1743 | /* Destroying an empty array is a no-op */ |
| 1744 | xa_destroy(xa); |
| 1745 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1746 | |
| 1747 | /* Destroying an array with a single entry */ |
| 1748 | for (index = 0; index < 1000; index++) { |
| 1749 | xa_store_index(xa, index, GFP_KERNEL); |
| 1750 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1751 | xa_destroy(xa); |
| 1752 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1753 | } |
| 1754 | |
| 1755 | /* Destroying an array with a single entry at ULONG_MAX */ |
| 1756 | xa_store(xa, ULONG_MAX, xa, GFP_KERNEL); |
| 1757 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1758 | xa_destroy(xa); |
| 1759 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1760 | |
| 1761 | #ifdef CONFIG_XARRAY_MULTI |
| 1762 | /* Destroying an array with a multi-index entry */ |
| 1763 | xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL); |
| 1764 | XA_BUG_ON(xa, xa_empty(xa)); |
| 1765 | xa_destroy(xa); |
| 1766 | XA_BUG_ON(xa, !xa_empty(xa)); |
| 1767 | #endif |
| 1768 | } |
| 1769 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1770 | static DEFINE_XARRAY(array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1771 | |
| 1772 | static int xarray_checks(void) |
| 1773 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1774 | check_xa_err(&array); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1775 | check_xas_retry(&array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1776 | check_xa_load(&array); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 1777 | check_xa_mark(&array); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1778 | check_xa_shrink(&array); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1779 | check_xas_erase(&array); |
Matthew Wilcox | 12fd2ae | 2019-03-09 22:25:27 -0500 | [diff] [blame] | 1780 | check_insert(&array); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 1781 | check_cmpxchg(&array); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame] | 1782 | check_reserve(&array); |
Matthew Wilcox | b38f6c5 | 2019-02-20 11:30:49 -0500 | [diff] [blame] | 1783 | check_reserve(&xa0); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1784 | check_multi_store(&array); |
Matthew Wilcox (Oracle) | 57417ce | 2020-10-15 20:05:13 -0700 | [diff] [blame] | 1785 | check_get_order(&array); |
Matthew Wilcox | 371c752 | 2018-07-04 10:50:12 -0400 | [diff] [blame] | 1786 | check_xa_alloc(); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1787 | check_find(&array); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1788 | check_find_entry(&array); |
Matthew Wilcox (Oracle) | c36d451 | 2020-01-31 06:17:09 -0500 | [diff] [blame] | 1789 | check_pause(&array); |
Matthew Wilcox | d6427f8 | 2018-08-28 16:13:16 -0400 | [diff] [blame] | 1790 | check_account(&array); |
Matthew Wilcox | 687149f | 2017-11-17 08:16:34 -0500 | [diff] [blame] | 1791 | check_destroy(&array); |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 1792 | check_move(&array); |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 1793 | check_create_range(&array); |
Matthew Wilcox | 0e9446c | 2018-08-15 14:13:29 -0400 | [diff] [blame] | 1794 | check_store_range(&array); |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 1795 | check_store_iter(&array); |
Matthew Wilcox | 76b4e52 | 2018-12-28 23:20:44 -0500 | [diff] [blame] | 1796 | check_align(&xa0); |
Matthew Wilcox (Oracle) | 8fc7564 | 2020-10-15 20:05:16 -0700 | [diff] [blame] | 1797 | check_split(&array); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1798 | |
Matthew Wilcox | a97e790 | 2017-11-24 14:24:59 -0500 | [diff] [blame] | 1799 | check_workingset(&array, 0); |
| 1800 | check_workingset(&array, 64); |
| 1801 | check_workingset(&array, 4096); |
| 1802 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1803 | printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); |
| 1804 | return (tests_run == tests_passed) ? 0 : -EINVAL; |
| 1805 | } |
| 1806 | |
| 1807 | static void xarray_exit(void) |
| 1808 | { |
| 1809 | } |
| 1810 | |
| 1811 | module_init(xarray_checks); |
| 1812 | module_exit(xarray_exit); |
| 1813 | MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>"); |
| 1814 | MODULE_LICENSE("GPL"); |