Blame view

lib/test_xarray.c 46.5 KB
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1
2
3
4
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * test_xarray.c: Test the XArray API
   * Copyright (c) 2017-2018 Microsoft Corporation
c44aa5e8a   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
5
   * Copyright (c) 2019-2020 Oracle
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
6
7
8
9
10
11
12
13
   * Author: Matthew Wilcox <willy@infradead.org>
   */
  
  #include <linux/xarray.h>
  #include <linux/module.h>
  
  static unsigned int tests_run;
  static unsigned int tests_passed;
bd40b17ca   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
14
15
  static const unsigned int order_limit =
  		IS_ENABLED(CONFIG_XARRAY_MULTI) ? BITS_PER_LONG : 1;
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #ifndef XA_DEBUG
  # ifdef __KERNEL__
  void xa_dump(const struct xarray *xa) { }
  # endif
  #undef XA_BUG_ON
  #define XA_BUG_ON(xa, x) do {					\
  	tests_run++;						\
  	if (x) {						\
  		printk("BUG at %s:%d
  ", __func__, __LINE__);	\
  		xa_dump(xa);					\
  		dump_stack();					\
  	} else {						\
  		tests_passed++;					\
  	}							\
  } while (0)
  #endif
b7677a132   Matthew Wilcox   XArray tests: Han...
33
34
35
36
  static void *xa_mk_index(unsigned long index)
  {
  	return xa_mk_value(index & LONG_MAX);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
37
38
  static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp)
  {
b7677a132   Matthew Wilcox   XArray tests: Han...
39
  	return xa_store(xa, index, xa_mk_index(index), gfp);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
40
  }
12fd2aee6   Matthew Wilcox   XArray tests: Add...
41
42
43
44
45
  static void xa_insert_index(struct xarray *xa, unsigned long index)
  {
  	XA_BUG_ON(xa, xa_insert(xa, index, xa_mk_index(index),
  				GFP_KERNEL) != 0);
  }
371c752dc   Matthew Wilcox   xarray: Track fre...
46
47
  static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp)
  {
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
48
  	u32 id;
371c752dc   Matthew Wilcox   xarray: Track fre...
49

a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
50
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(index), xa_limit_32b,
371c752dc   Matthew Wilcox   xarray: Track fre...
51
52
53
  				gfp) != 0);
  	XA_BUG_ON(xa, id != index);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
54
55
  static void xa_erase_index(struct xarray *xa, unsigned long index)
  {
b7677a132   Matthew Wilcox   XArray tests: Han...
56
  	XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index));
58d6ea308   Matthew Wilcox   xarray: Add XArra...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  	XA_BUG_ON(xa, xa_load(xa, index) != NULL);
  }
  
  /*
   * If anyone needs this, please move it to xarray.c.  We have no current
   * users outside the test suite because all current multislot users want
   * to use the advanced API.
   */
  static void *xa_store_order(struct xarray *xa, unsigned long index,
  		unsigned order, void *entry, gfp_t gfp)
  {
  	XA_STATE_ORDER(xas, xa, index, order);
  	void *curr;
  
  	do {
  		xas_lock(&xas);
  		curr = xas_store(&xas, entry);
  		xas_unlock(&xas);
  	} while (xas_nomem(&xas, gfp));
  
  	return curr;
  }
  
  static noinline void check_xa_err(struct xarray *xa)
  {
  	XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0);
  	XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0);
  #ifndef __KERNEL__
  	/* The kernel does not fail GFP_NOWAIT allocations */
  	XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
  	XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
  #endif
  	XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0);
  	XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0);
  	XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0);
  // kills the test-suite :-(
  //	XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
94
  }
b803b4282   Matthew Wilcox   xarray: Add XArra...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  static noinline void check_xas_retry(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  	void *entry;
  
  	xa_store_index(xa, 0, GFP_KERNEL);
  	xa_store_index(xa, 1, GFP_KERNEL);
  
  	rcu_read_lock();
  	XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0));
  	xa_erase_index(xa, 1);
  	XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas)));
  	XA_BUG_ON(xa, xas_retry(&xas, NULL));
  	XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0)));
  	xas_reset(&xas);
  	XA_BUG_ON(xa, xas.xa_node != XAS_RESTART);
  	XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
  	XA_BUG_ON(xa, xas.xa_node != NULL);
bd54211b8   Matthew Wilcox   XArray tests: RCU...
113
  	rcu_read_unlock();
b803b4282   Matthew Wilcox   xarray: Add XArra...
114
115
  
  	XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
bd54211b8   Matthew Wilcox   XArray tests: RCU...
116
117
  
  	rcu_read_lock();
b803b4282   Matthew Wilcox   xarray: Add XArra...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  	XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas)));
  	xas.xa_node = XAS_RESTART;
  	XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
  	rcu_read_unlock();
  
  	/* Make sure we can iterate through retry entries */
  	xas_lock(&xas);
  	xas_set(&xas, 0);
  	xas_store(&xas, XA_RETRY_ENTRY);
  	xas_set(&xas, 1);
  	xas_store(&xas, XA_RETRY_ENTRY);
  
  	xas_set(&xas, 0);
  	xas_for_each(&xas, entry, ULONG_MAX) {
b7677a132   Matthew Wilcox   XArray tests: Han...
132
  		xas_store(&xas, xa_mk_index(xas.xa_index));
b803b4282   Matthew Wilcox   xarray: Add XArra...
133
134
135
136
137
138
  	}
  	xas_unlock(&xas);
  
  	xa_erase_index(xa, 0);
  	xa_erase_index(xa, 1);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  static noinline void check_xa_load(struct xarray *xa)
  {
  	unsigned long i, j;
  
  	for (i = 0; i < 1024; i++) {
  		for (j = 0; j < 1024; j++) {
  			void *entry = xa_load(xa, j);
  			if (j < i)
  				XA_BUG_ON(xa, xa_to_value(entry) != j);
  			else
  				XA_BUG_ON(xa, entry);
  		}
  		XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  	}
  
  	for (i = 0; i < 1024; i++) {
  		for (j = 0; j < 1024; j++) {
  			void *entry = xa_load(xa, j);
  			if (j >= i)
  				XA_BUG_ON(xa, xa_to_value(entry) != j);
  			else
  				XA_BUG_ON(xa, entry);
  		}
  		xa_erase_index(xa, i);
  	}
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
9b89a0355   Matthew Wilcox   xarray: Add XArra...
166
167
  static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index)
  {
58d6ea308   Matthew Wilcox   xarray: Add XArra...
168
169
  	unsigned int order;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1;
9b89a0355   Matthew Wilcox   xarray: Add XArra...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  	/* NULL elements have no marks set */
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  	xa_set_mark(xa, index, XA_MARK_0);
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  
  	/* Storing a pointer will not make a mark appear */
  	XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL);
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  	xa_set_mark(xa, index, XA_MARK_0);
  	XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
  
  	/* Setting one mark will not set another mark */
  	XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0));
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1));
  
  	/* Storing NULL clears marks, and they can't be set again */
  	xa_erase_index(xa, index);
  	XA_BUG_ON(xa, !xa_empty(xa));
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  	xa_set_mark(xa, index, XA_MARK_0);
  	XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
58d6ea308   Matthew Wilcox   xarray: Add XArra...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  
  	/*
  	 * Storing a multi-index entry over entries with marks gives the
  	 * entire entry the union of the marks
  	 */
  	BUG_ON((index % 4) != 0);
  	for (order = 2; order < max_order; order++) {
  		unsigned long base = round_down(index, 1UL << order);
  		unsigned long next = base + (1UL << order);
  		unsigned long i;
  
  		XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL));
  		xa_set_mark(xa, index + 1, XA_MARK_0);
  		XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL));
d69d287a9   Matthew Wilcox   XArray tests: Che...
205
  		xa_set_mark(xa, index + 2, XA_MARK_2);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
206
  		XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL));
b7677a132   Matthew Wilcox   XArray tests: Han...
207
  		xa_store_order(xa, index, order, xa_mk_index(index),
58d6ea308   Matthew Wilcox   xarray: Add XArra...
208
209
  				GFP_KERNEL);
  		for (i = base; i < next; i++) {
93eb07f72   Matthew Wilcox   xarray: Move mult...
210
211
212
  			XA_STATE(xas, xa, i);
  			unsigned int seen = 0;
  			void *entry;
58d6ea308   Matthew Wilcox   xarray: Add XArra...
213
  			XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
d69d287a9   Matthew Wilcox   XArray tests: Che...
214
215
  			XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_1));
  			XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_2));
93eb07f72   Matthew Wilcox   xarray: Move mult...
216
217
  
  			/* We should see two elements in the array */
fffc9a260   Matthew Wilcox   XArray tests: Add...
218
  			rcu_read_lock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
219
220
  			xas_for_each(&xas, entry, ULONG_MAX)
  				seen++;
fffc9a260   Matthew Wilcox   XArray tests: Add...
221
  			rcu_read_unlock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
222
223
224
225
226
  			XA_BUG_ON(xa, seen != 2);
  
  			/* One of which is marked */
  			xas_set(&xas, 0);
  			seen = 0;
fffc9a260   Matthew Wilcox   XArray tests: Add...
227
  			rcu_read_lock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
228
229
  			xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
  				seen++;
fffc9a260   Matthew Wilcox   XArray tests: Add...
230
  			rcu_read_unlock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
231
  			XA_BUG_ON(xa, seen != 1);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
232
233
234
235
236
237
238
239
240
  		}
  		XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0));
  		XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1));
  		XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2));
  		xa_erase_index(xa, index);
  		xa_erase_index(xa, next);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  	XA_BUG_ON(xa, !xa_empty(xa));
9b89a0355   Matthew Wilcox   xarray: Add XArra...
241
  }
adb9d9c4c   Matthew Wilcox   radix tree: Remov...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  static noinline void check_xa_mark_2(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  	unsigned long index;
  	unsigned int count = 0;
  	void *entry;
  
  	xa_store_index(xa, 0, GFP_KERNEL);
  	xa_set_mark(xa, 0, XA_MARK_0);
  	xas_lock(&xas);
  	xas_load(&xas);
  	xas_init_marks(&xas);
  	xas_unlock(&xas);
  	XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0);
  
  	for (index = 3500; index < 4500; index++) {
  		xa_store_index(xa, index, GFP_KERNEL);
  		xa_set_mark(xa, index, XA_MARK_0);
  	}
  
  	xas_reset(&xas);
  	rcu_read_lock();
  	xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
  		count++;
  	rcu_read_unlock();
  	XA_BUG_ON(xa, count != 1000);
  
  	xas_lock(&xas);
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		xas_init_marks(&xas);
  		XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0));
  		XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0));
  	}
  	xas_unlock(&xas);
  
  	xa_destroy(xa);
  }
04e9e9bb8   Matthew Wilcox (Oracle)   XArray: Test mark...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  static noinline void check_xa_mark_3(struct xarray *xa)
  {
  #ifdef CONFIG_XARRAY_MULTI
  	XA_STATE(xas, xa, 0x41);
  	void *entry;
  	int count = 0;
  
  	xa_store_order(xa, 0x40, 2, xa_mk_index(0x40), GFP_KERNEL);
  	xa_set_mark(xa, 0x41, XA_MARK_0);
  
  	rcu_read_lock();
  	xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) {
  		count++;
  		XA_BUG_ON(xa, entry != xa_mk_index(0x40));
  	}
  	XA_BUG_ON(xa, count != 1);
  	rcu_read_unlock();
  	xa_destroy(xa);
  #endif
  }
9b89a0355   Matthew Wilcox   xarray: Add XArra...
299
300
301
302
303
304
  static noinline void check_xa_mark(struct xarray *xa)
  {
  	unsigned long index;
  
  	for (index = 0; index < 16384; index += 4)
  		check_xa_mark_1(xa, index);
adb9d9c4c   Matthew Wilcox   radix tree: Remov...
305
306
  
  	check_xa_mark_2(xa);
04e9e9bb8   Matthew Wilcox (Oracle)   XArray: Test mark...
307
  	check_xa_mark_3(xa);
9b89a0355   Matthew Wilcox   xarray: Add XArra...
308
  }
58d6ea308   Matthew Wilcox   xarray: Add XArra...
309
310
311
312
  static noinline void check_xa_shrink(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 1);
  	struct xa_node *node;
93eb07f72   Matthew Wilcox   xarray: Move mult...
313
314
  	unsigned int order;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1;
58d6ea308   Matthew Wilcox   xarray: Add XArra...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  	XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL);
  	XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
  
  	/*
  	 * Check that erasing the entry at 1 shrinks the tree and properly
  	 * marks the node as being deleted.
  	 */
  	xas_lock(&xas);
  	XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1));
  	node = xas.xa_node;
  	XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0));
  	XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
  	XA_BUG_ON(xa, xa_load(xa, 1) != NULL);
  	XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS);
  	XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY);
  	XA_BUG_ON(xa, xas_load(&xas) != NULL);
  	xas_unlock(&xas);
  	XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  	xa_erase_index(xa, 0);
  	XA_BUG_ON(xa, !xa_empty(xa));
93eb07f72   Matthew Wilcox   xarray: Move mult...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  
  	for (order = 0; order < max_order; order++) {
  		unsigned long max = (1UL << order) - 1;
  		xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL);
  		XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0));
  		XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
  		rcu_read_lock();
  		node = xa_head(xa);
  		rcu_read_unlock();
  		XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) !=
  				NULL);
  		rcu_read_lock();
  		XA_BUG_ON(xa, xa_head(xa) == node);
  		rcu_read_unlock();
  		XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
  		xa_erase_index(xa, ULONG_MAX);
  		XA_BUG_ON(xa, xa->xa_head != node);
  		xa_erase_index(xa, 0);
  	}
58d6ea308   Matthew Wilcox   xarray: Add XArra...
356
  }
12fd2aee6   Matthew Wilcox   XArray tests: Add...
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  static noinline void check_insert(struct xarray *xa)
  {
  	unsigned long i;
  
  	for (i = 0; i < 1024; i++) {
  		xa_insert_index(xa, i);
  		XA_BUG_ON(xa, xa_load(xa, i - 1) != NULL);
  		XA_BUG_ON(xa, xa_load(xa, i + 1) != NULL);
  		xa_erase_index(xa, i);
  	}
  
  	for (i = 10; i < BITS_PER_LONG; i++) {
  		xa_insert_index(xa, 1UL << i);
  		XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 1) != NULL);
  		XA_BUG_ON(xa, xa_load(xa, (1UL << i) + 1) != NULL);
  		xa_erase_index(xa, 1UL << i);
  
  		xa_insert_index(xa, (1UL << i) - 1);
  		XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 2) != NULL);
  		XA_BUG_ON(xa, xa_load(xa, 1UL << i) != NULL);
  		xa_erase_index(xa, (1UL << i) - 1);
  	}
  
  	xa_insert_index(xa, ~0UL);
  	XA_BUG_ON(xa, xa_load(xa, 0UL) != NULL);
  	XA_BUG_ON(xa, xa_load(xa, ~1UL) != NULL);
  	xa_erase_index(xa, ~0UL);
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
41aec91f5   Matthew Wilcox   xarray: Add XArra...
387
388
389
390
391
392
393
394
  static noinline void check_cmpxchg(struct xarray *xa)
  {
  	void *FIVE = xa_mk_value(5);
  	void *SIX = xa_mk_value(6);
  	void *LOTS = xa_mk_value(12345678);
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  	XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL);
fd9dc93e3   Matthew Wilcox   XArray: Change xa...
395
  	XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY);
41aec91f5   Matthew Wilcox   xarray: Add XArra...
396
397
398
399
400
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS);
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS);
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE);
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL);
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL);
062b73591   Matthew Wilcox (Oracle)   XArray: Test two ...
401
402
403
  	XA_BUG_ON(xa, xa_insert(xa, 5, FIVE, GFP_KERNEL) != -EBUSY);
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != FIVE);
  	XA_BUG_ON(xa, xa_insert(xa, 5, FIVE, GFP_KERNEL) == -EBUSY);
41aec91f5   Matthew Wilcox   xarray: Add XArra...
404
405
406
407
  	xa_erase_index(xa, 12345678);
  	xa_erase_index(xa, 5);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
408
409
410
  static noinline void check_reserve(struct xarray *xa)
  {
  	void *entry;
4a31896c5   Matthew Wilcox   XArray: Change xa...
411
  	unsigned long index;
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
412
  	int count;
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
413
414
415
  
  	/* An array with a reserved entry is not empty */
  	XA_BUG_ON(xa, !xa_empty(xa));
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
416
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
417
418
419
420
421
422
  	XA_BUG_ON(xa, xa_empty(xa));
  	XA_BUG_ON(xa, xa_load(xa, 12345678));
  	xa_release(xa, 12345678);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* Releasing a used entry does nothing */
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
423
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
424
425
426
427
  	XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL);
  	xa_release(xa, 12345678);
  	xa_erase_index(xa, 12345678);
  	XA_BUG_ON(xa, !xa_empty(xa));
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
428
  	/* cmpxchg sees a reserved entry as ZERO */
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
429
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
430
431
  	XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, XA_ZERO_ENTRY,
  				xa_mk_value(12345678), GFP_NOWAIT) != NULL);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
432
433
434
  	xa_release(xa, 12345678);
  	xa_erase_index(xa, 12345678);
  	XA_BUG_ON(xa, !xa_empty(xa));
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
435
  	/* xa_insert treats it as busy */
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
436
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
b0606fed6   Matthew Wilcox   XArray: Honour re...
437
  	XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) !=
fd9dc93e3   Matthew Wilcox   XArray: Change xa...
438
  			-EBUSY);
b0606fed6   Matthew Wilcox   XArray: Honour re...
439
440
  	XA_BUG_ON(xa, xa_empty(xa));
  	XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL);
4c0608f4a   Matthew Wilcox   XArray: Regularis...
441
  	XA_BUG_ON(xa, !xa_empty(xa));
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
442
443
  	/* Can iterate through a reserved entry */
  	xa_store_index(xa, 5, GFP_KERNEL);
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
444
  	XA_BUG_ON(xa, xa_reserve(xa, 6, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
445
  	xa_store_index(xa, 7, GFP_KERNEL);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
446
  	count = 0;
4a31896c5   Matthew Wilcox   XArray: Change xa...
447
  	xa_for_each(xa, index, entry) {
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
448
  		XA_BUG_ON(xa, index != 5 && index != 7);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  		count++;
  	}
  	XA_BUG_ON(xa, count != 2);
  
  	/* If we free a reserved entry, we should be able to allocate it */
  	if (xa->xa_flags & XA_FLAGS_ALLOC) {
  		u32 id;
  
  		XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(8),
  					XA_LIMIT(5, 10), GFP_KERNEL) != 0);
  		XA_BUG_ON(xa, id != 8);
  
  		xa_release(xa, 6);
  		XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(6),
  					XA_LIMIT(5, 10), GFP_KERNEL) != 0);
  		XA_BUG_ON(xa, id != 6);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
465
  	}
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
466

9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
467
468
  	xa_destroy(xa);
  }
b803b4282   Matthew Wilcox   xarray: Add XArra...
469
470
471
472
473
474
475
476
477
478
479
  static noinline void check_xas_erase(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  	void *entry;
  	unsigned long i, j;
  
  	for (i = 0; i < 200; i++) {
  		for (j = i; j < 2 * i + 17; j++) {
  			xas_set(&xas, j);
  			do {
  				xas_lock(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
480
  				xas_store(&xas, xa_mk_index(j));
b803b4282   Matthew Wilcox   xarray: Add XArra...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  				xas_unlock(&xas);
  			} while (xas_nomem(&xas, GFP_KERNEL));
  		}
  
  		xas_set(&xas, ULONG_MAX);
  		do {
  			xas_lock(&xas);
  			xas_store(&xas, xa_mk_value(0));
  			xas_unlock(&xas);
  		} while (xas_nomem(&xas, GFP_KERNEL));
  
  		xas_lock(&xas);
  		xas_store(&xas, NULL);
  
  		xas_set(&xas, 0);
  		j = i;
  		xas_for_each(&xas, entry, ULONG_MAX) {
b7677a132   Matthew Wilcox   XArray tests: Han...
498
  			XA_BUG_ON(xa, entry != xa_mk_index(j));
b803b4282   Matthew Wilcox   xarray: Add XArra...
499
500
501
502
503
504
505
  			xas_store(&xas, NULL);
  			j++;
  		}
  		xas_unlock(&xas);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  }
4f06d6302   Matthew Wilcox   xarray: Move mult...
506
507
508
509
510
511
512
  #ifdef CONFIG_XARRAY_MULTI
  static noinline void check_multi_store_1(struct xarray *xa, unsigned long index,
  		unsigned int order)
  {
  	XA_STATE(xas, xa, index);
  	unsigned long min = index & ~((1UL << order) - 1);
  	unsigned long max = min + (1UL << order);
b7677a132   Matthew Wilcox   XArray tests: Han...
513
514
515
  	xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
  	XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(index));
  	XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(index));
4f06d6302   Matthew Wilcox   xarray: Move mult...
516
517
  	XA_BUG_ON(xa, xa_load(xa, max) != NULL);
  	XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
fffc9a260   Matthew Wilcox   XArray tests: Add...
518
  	xas_lock(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
519
  	XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index));
fffc9a260   Matthew Wilcox   XArray tests: Add...
520
  	xas_unlock(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
521
522
  	XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(min));
  	XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(min));
4f06d6302   Matthew Wilcox   xarray: Move mult...
523
524
525
526
527
528
529
530
531
532
533
534
  	XA_BUG_ON(xa, xa_load(xa, max) != NULL);
  	XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
  
  	xa_erase_index(xa, min);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_multi_store_2(struct xarray *xa, unsigned long index,
  		unsigned int order)
  {
  	XA_STATE(xas, xa, index);
  	xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL);
fffc9a260   Matthew Wilcox   XArray tests: Add...
535
  	xas_lock(&xas);
4f06d6302   Matthew Wilcox   xarray: Move mult...
536
537
538
  	XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0));
  	XA_BUG_ON(xa, xas.xa_index != index);
  	XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
fffc9a260   Matthew Wilcox   XArray tests: Add...
539
  	xas_unlock(&xas);
4f06d6302   Matthew Wilcox   xarray: Move mult...
540
541
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
4f145cd66   Matthew Wilcox   XArray tests: Che...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
  
  static noinline void check_multi_store_3(struct xarray *xa, unsigned long index,
  		unsigned int order)
  {
  	XA_STATE(xas, xa, 0);
  	void *entry;
  	int n = 0;
  
  	xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
  
  	xas_lock(&xas);
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		XA_BUG_ON(xa, entry != xa_mk_index(index));
  		n++;
  	}
  	XA_BUG_ON(xa, n != 1);
  	xas_set(&xas, index + 1);
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		XA_BUG_ON(xa, entry != xa_mk_index(index));
  		n++;
  	}
  	XA_BUG_ON(xa, n != 2);
  	xas_unlock(&xas);
  
  	xa_destroy(xa);
  }
4f06d6302   Matthew Wilcox   xarray: Move mult...
568
  #endif
58d6ea308   Matthew Wilcox   xarray: Add XArra...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  static noinline void check_multi_store(struct xarray *xa)
  {
  #ifdef CONFIG_XARRAY_MULTI
  	unsigned long i, j, k;
  	unsigned int max_order = (sizeof(long) == 4) ? 30 : 60;
  
  	/* Loading from any position returns the same value */
  	xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL);
  	XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  	XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  	XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  	rcu_read_lock();
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2);
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  	rcu_read_unlock();
  
  	/* Storing adjacent to the value does not alter the value */
  	xa_store(xa, 3, xa, GFP_KERNEL);
  	XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  	XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  	XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  	rcu_read_lock();
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3);
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  	rcu_read_unlock();
  
  	/* Overwriting multiple indexes works */
  	xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL);
  	XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1));
  	XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1));
  	XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1));
  	XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1));
  	XA_BUG_ON(xa, xa_load(xa, 4) != NULL);
  	rcu_read_lock();
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4);
  	XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4);
  	rcu_read_unlock();
  
  	/* We can erase multiple values with a single store */
5404a7f1c   Matthew Wilcox   XArray tests: Cor...
608
  	xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
609
610
611
612
613
614
615
616
617
618
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* Even when the first slot is empty but the others aren't */
  	xa_store_index(xa, 1, GFP_KERNEL);
  	xa_store_index(xa, 2, GFP_KERNEL);
  	xa_store_order(xa, 0, 2, NULL, GFP_KERNEL);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	for (i = 0; i < max_order; i++) {
  		for (j = 0; j < max_order; j++) {
b7677a132   Matthew Wilcox   XArray tests: Han...
619
620
  			xa_store_order(xa, 0, i, xa_mk_index(i), GFP_KERNEL);
  			xa_store_order(xa, 0, j, xa_mk_index(j), GFP_KERNEL);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
621
622
623
624
625
626
  
  			for (k = 0; k < max_order; k++) {
  				void *entry = xa_load(xa, (1UL << k) - 1);
  				if ((i < k) && (j < k))
  					XA_BUG_ON(xa, entry != NULL);
  				else
b7677a132   Matthew Wilcox   XArray tests: Han...
627
  					XA_BUG_ON(xa, entry != xa_mk_index(j));
58d6ea308   Matthew Wilcox   xarray: Add XArra...
628
629
630
631
632
633
  			}
  
  			xa_erase(xa, 0);
  			XA_BUG_ON(xa, !xa_empty(xa));
  		}
  	}
4f06d6302   Matthew Wilcox   xarray: Move mult...
634
635
636
637
638
639
640
  
  	for (i = 0; i < 20; i++) {
  		check_multi_store_1(xa, 200, i);
  		check_multi_store_1(xa, 0, i);
  		check_multi_store_1(xa, (1UL << i) + 1, i);
  	}
  	check_multi_store_2(xa, 4095, 9);
4f145cd66   Matthew Wilcox   XArray tests: Che...
641
642
643
644
645
  
  	for (i = 1; i < 20; i++) {
  		check_multi_store_3(xa, 0, i);
  		check_multi_store_3(xa, 1UL << i, i);
  	}
58d6ea308   Matthew Wilcox   xarray: Add XArra...
646
647
  #endif
  }
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
648
  static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base)
371c752dc   Matthew Wilcox   xarray: Track fre...
649
650
651
  {
  	int i;
  	u32 id;
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
652
653
654
  	XA_BUG_ON(xa, !xa_empty(xa));
  	/* An empty array should assign %base to the first alloc */
  	xa_alloc_index(xa, base, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
655
656
  
  	/* Erasing it should make the array empty again */
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  	xa_erase_index(xa, base);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* And it should assign %base again */
  	xa_alloc_index(xa, base, GFP_KERNEL);
  
  	/* Allocating and then erasing a lot should not lose base */
  	for (i = base + 1; i < 2 * XA_CHUNK_SIZE; i++)
  		xa_alloc_index(xa, i, GFP_KERNEL);
  	for (i = base; i < 2 * XA_CHUNK_SIZE; i++)
  		xa_erase_index(xa, i);
  	xa_alloc_index(xa, base, GFP_KERNEL);
  
  	/* Destroying the array should do the same as erasing */
  	xa_destroy(xa);
371c752dc   Matthew Wilcox   xarray: Track fre...
672

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
673
674
  	/* And it should assign %base again */
  	xa_alloc_index(xa, base, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
675

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
676
677
678
  	/* The next assigned ID should be base+1 */
  	xa_alloc_index(xa, base + 1, GFP_KERNEL);
  	xa_erase_index(xa, base + 1);
371c752dc   Matthew Wilcox   xarray: Track fre...
679
680
  
  	/* Storing a value should mark it used */
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
681
682
  	xa_store_index(xa, base + 1, GFP_KERNEL);
  	xa_alloc_index(xa, base + 2, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
683

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
684
685
686
  	/* If we then erase base, it should be free */
  	xa_erase_index(xa, base);
  	xa_alloc_index(xa, base, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
687

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
688
689
  	xa_erase_index(xa, base + 1);
  	xa_erase_index(xa, base + 2);
371c752dc   Matthew Wilcox   xarray: Track fre...
690
691
  
  	for (i = 1; i < 5000; i++) {
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
692
  		xa_alloc_index(xa, base + i, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
693
  	}
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
694
  	xa_destroy(xa);
371c752dc   Matthew Wilcox   xarray: Track fre...
695

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
696
  	/* Check that we fail properly at the limit of allocation */
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
697
698
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX - 1),
  				XA_LIMIT(UINT_MAX - 1, UINT_MAX),
371c752dc   Matthew Wilcox   xarray: Track fre...
699
  				GFP_KERNEL) != 0);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
700
  	XA_BUG_ON(xa, id != 0xfffffffeU);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
701
702
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX),
  				XA_LIMIT(UINT_MAX - 1, UINT_MAX),
371c752dc   Matthew Wilcox   xarray: Track fre...
703
  				GFP_KERNEL) != 0);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
704
  	XA_BUG_ON(xa, id != 0xffffffffU);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
705
706
707
708
709
  	id = 3;
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(0),
  				XA_LIMIT(UINT_MAX - 1, UINT_MAX),
  				GFP_KERNEL) != -EBUSY);
  	XA_BUG_ON(xa, id != 3);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
710
  	xa_destroy(xa);
48483614d   Matthew Wilcox   XArray: Fix xa_al...
711

a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
712
713
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
  				GFP_KERNEL) != -EBUSY);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
714
  	XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
715
716
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
  				GFP_KERNEL) != -EBUSY);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
717
718
719
  	xa_erase_index(xa, 3);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  static noinline void check_xa_alloc_2(struct xarray *xa, unsigned int base)
  {
  	unsigned int i, id;
  	unsigned long index;
  	void *entry;
  
  	/* Allocate and free a NULL and check xa_empty() behaves */
  	XA_BUG_ON(xa, !xa_empty(xa));
  	XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != base);
  	XA_BUG_ON(xa, xa_empty(xa));
  	XA_BUG_ON(xa, xa_erase(xa, id) != NULL);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* Ditto, but check destroy instead of erase */
  	XA_BUG_ON(xa, !xa_empty(xa));
  	XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != base);
  	XA_BUG_ON(xa, xa_empty(xa));
  	xa_destroy(xa);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	for (i = base; i < base + 10; i++) {
  		XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b,
  					GFP_KERNEL) != 0);
  		XA_BUG_ON(xa, id != i);
  	}
  
  	XA_BUG_ON(xa, xa_store(xa, 3, xa_mk_index(3), GFP_KERNEL) != NULL);
  	XA_BUG_ON(xa, xa_store(xa, 4, xa_mk_index(4), GFP_KERNEL) != NULL);
  	XA_BUG_ON(xa, xa_store(xa, 4, NULL, GFP_KERNEL) != xa_mk_index(4));
  	XA_BUG_ON(xa, xa_erase(xa, 5) != NULL);
  	XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != 5);
  
  	xa_for_each(xa, index, entry) {
  		xa_erase_index(xa, index);
  	}
  
  	for (i = base; i < base + 9; i++) {
  		XA_BUG_ON(xa, xa_erase(xa, i) != NULL);
  		XA_BUG_ON(xa, xa_empty(xa));
  	}
  	XA_BUG_ON(xa, xa_erase(xa, 8) != NULL);
  	XA_BUG_ON(xa, xa_empty(xa));
  	XA_BUG_ON(xa, xa_erase(xa, base + 9) != NULL);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	xa_destroy(xa);
  }
2fa044e51   Matthew Wilcox   XArray: Add cycli...
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
  static noinline void check_xa_alloc_3(struct xarray *xa, unsigned int base)
  {
  	struct xa_limit limit = XA_LIMIT(1, 0x3fff);
  	u32 next = 0;
  	unsigned int i, id;
  	unsigned long index;
  	void *entry;
  
  	XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(1), limit,
  				&next, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != 1);
  
  	next = 0x3ffd;
  	XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(0x3ffd), limit,
  				&next, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != 0x3ffd);
  	xa_erase_index(xa, 0x3ffd);
  	xa_erase_index(xa, 1);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	for (i = 0x3ffe; i < 0x4003; i++) {
  		if (i < 0x4000)
  			entry = xa_mk_index(i);
  		else
  			entry = xa_mk_index(i - 0x3fff);
  		XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, entry, limit,
  					&next, GFP_KERNEL) != (id == 1));
  		XA_BUG_ON(xa, xa_mk_index(id) != entry);
  	}
  
  	/* Check wrap-around is handled correctly */
  	if (base != 0)
  		xa_erase_index(xa, base);
  	xa_erase_index(xa, base + 1);
  	next = UINT_MAX;
  	XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(UINT_MAX),
  				xa_limit_32b, &next, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != UINT_MAX);
  	XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base),
  				xa_limit_32b, &next, GFP_KERNEL) != 1);
  	XA_BUG_ON(xa, id != base);
  	XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base + 1),
  				xa_limit_32b, &next, GFP_KERNEL) != 0);
  	XA_BUG_ON(xa, id != base + 1);
  
  	xa_for_each(xa, index, entry)
  		xa_erase_index(xa, index);
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
820
821
822
823
824
825
826
  static DEFINE_XARRAY_ALLOC(xa0);
  static DEFINE_XARRAY_ALLOC1(xa1);
  
  static noinline void check_xa_alloc(void)
  {
  	check_xa_alloc_1(&xa0, 0);
  	check_xa_alloc_1(&xa1, 1);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
827
828
  	check_xa_alloc_2(&xa0, 0);
  	check_xa_alloc_2(&xa1, 1);
2fa044e51   Matthew Wilcox   XArray: Add cycli...
829
830
  	check_xa_alloc_3(&xa0, 0);
  	check_xa_alloc_3(&xa1, 1);
371c752dc   Matthew Wilcox   xarray: Track fre...
831
  }
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
832
833
834
835
836
837
838
839
840
841
842
  static noinline void __check_store_iter(struct xarray *xa, unsigned long start,
  			unsigned int order, unsigned int present)
  {
  	XA_STATE_ORDER(xas, xa, start, order);
  	void *entry;
  	unsigned int count = 0;
  
  retry:
  	xas_lock(&xas);
  	xas_for_each_conflict(&xas, entry) {
  		XA_BUG_ON(xa, !xa_is_value(entry));
b7677a132   Matthew Wilcox   XArray tests: Han...
843
844
  		XA_BUG_ON(xa, entry < xa_mk_index(start));
  		XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
845
846
  		count++;
  	}
b7677a132   Matthew Wilcox   XArray tests: Han...
847
  	xas_store(&xas, xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
848
849
850
851
852
853
854
  	xas_unlock(&xas);
  	if (xas_nomem(&xas, GFP_KERNEL)) {
  		count = 0;
  		goto retry;
  	}
  	XA_BUG_ON(xa, xas_error(&xas));
  	XA_BUG_ON(xa, count != present);
b7677a132   Matthew Wilcox   XArray tests: Han...
855
  	XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
856
  	XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) !=
b7677a132   Matthew Wilcox   XArray tests: Han...
857
  			xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
  	xa_erase_index(xa, start);
  }
  
  static noinline void check_store_iter(struct xarray *xa)
  {
  	unsigned int i, j;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
  
  	for (i = 0; i < max_order; i++) {
  		unsigned int min = 1 << i;
  		unsigned int max = (2 << i) - 1;
  		__check_store_iter(xa, 0, i, 0);
  		XA_BUG_ON(xa, !xa_empty(xa));
  		__check_store_iter(xa, min, i, 0);
  		XA_BUG_ON(xa, !xa_empty(xa));
  
  		xa_store_index(xa, min, GFP_KERNEL);
  		__check_store_iter(xa, min, i, 1);
  		XA_BUG_ON(xa, !xa_empty(xa));
  		xa_store_index(xa, max, GFP_KERNEL);
  		__check_store_iter(xa, min, i, 1);
  		XA_BUG_ON(xa, !xa_empty(xa));
  
  		for (j = 0; j < min; j++)
  			xa_store_index(xa, j, GFP_KERNEL);
  		__check_store_iter(xa, 0, i, min);
  		XA_BUG_ON(xa, !xa_empty(xa));
  		for (j = 0; j < min; j++)
  			xa_store_index(xa, min + j, GFP_KERNEL);
  		__check_store_iter(xa, min, i, min);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  #ifdef CONFIG_XARRAY_MULTI
  	xa_store_index(xa, 63, GFP_KERNEL);
  	xa_store_index(xa, 65, GFP_KERNEL);
  	__check_store_iter(xa, 64, 2, 1);
  	xa_erase_index(xa, 63);
  #endif
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
898
  static noinline void check_multi_find_1(struct xarray *xa, unsigned order)
b803b4282   Matthew Wilcox   xarray: Add XArra...
899
900
  {
  #ifdef CONFIG_XARRAY_MULTI
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
901
902
  	unsigned long multi = 3 << order;
  	unsigned long next = 4 << order;
b803b4282   Matthew Wilcox   xarray: Add XArra...
903
  	unsigned long index;
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
904
905
  	xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL);
  	XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
c44aa5e8a   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
906
  	XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL);
b803b4282   Matthew Wilcox   xarray: Add XArra...
907
908
909
  
  	index = 0;
  	XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
910
911
912
  			xa_mk_value(multi));
  	XA_BUG_ON(xa, index != multi);
  	index = multi + 1;
b803b4282   Matthew Wilcox   xarray: Add XArra...
913
  	XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
914
915
  			xa_mk_value(multi));
  	XA_BUG_ON(xa, (index < multi) || (index >= next));
b803b4282   Matthew Wilcox   xarray: Add XArra...
916
  	XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
917
918
  			xa_mk_value(next));
  	XA_BUG_ON(xa, index != next);
c44aa5e8a   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
919
920
  	XA_BUG_ON(xa, xa_find_after(xa, &index, next, XA_PRESENT) != NULL);
  	XA_BUG_ON(xa, index != next);
b803b4282   Matthew Wilcox   xarray: Add XArra...
921

19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
922
923
  	xa_erase_index(xa, multi);
  	xa_erase_index(xa, next);
c44aa5e8a   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
924
  	xa_erase_index(xa, next + 1);
b803b4282   Matthew Wilcox   xarray: Add XArra...
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  	XA_BUG_ON(xa, !xa_empty(xa));
  #endif
  }
  
  static noinline void check_multi_find_2(struct xarray *xa)
  {
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1;
  	unsigned int i, j;
  	void *entry;
  
  	for (i = 0; i < max_order; i++) {
  		unsigned long index = 1UL << i;
  		for (j = 0; j < index; j++) {
  			XA_STATE(xas, xa, j + index);
  			xa_store_index(xa, index - 1, GFP_KERNEL);
b7677a132   Matthew Wilcox   XArray tests: Han...
940
  			xa_store_order(xa, index, i, xa_mk_index(index),
b803b4282   Matthew Wilcox   xarray: Add XArra...
941
942
943
944
945
946
947
948
949
950
951
  					GFP_KERNEL);
  			rcu_read_lock();
  			xas_for_each(&xas, entry, ULONG_MAX) {
  				xa_erase_index(xa, index);
  			}
  			rcu_read_unlock();
  			xa_erase_index(xa, index - 1);
  			XA_BUG_ON(xa, !xa_empty(xa));
  		}
  	}
  }
bd40b17ca   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
952
953
954
955
956
957
958
959
960
961
962
963
964
  static noinline void check_multi_find_3(struct xarray *xa)
  {
  	unsigned int order;
  
  	for (order = 5; order < order_limit; order++) {
  		unsigned long index = 1UL << (order - 5);
  
  		XA_BUG_ON(xa, !xa_empty(xa));
  		xa_store_order(xa, 0, order - 4, xa_mk_index(0), GFP_KERNEL);
  		XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT));
  		xa_erase_index(xa, 0);
  	}
  }
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
965
  static noinline void check_find_1(struct xarray *xa)
b803b4282   Matthew Wilcox   xarray: Add XArra...
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  {
  	unsigned long i, j, k;
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/*
  	 * Check xa_find with all pairs between 0 and 99 inclusive,
  	 * starting at every index between 0 and 99
  	 */
  	for (i = 0; i < 100; i++) {
  		XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  		xa_set_mark(xa, i, XA_MARK_0);
  		for (j = 0; j < i; j++) {
  			XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) !=
  					NULL);
  			xa_set_mark(xa, j, XA_MARK_0);
  			for (k = 0; k < 100; k++) {
  				unsigned long index = k;
  				void *entry = xa_find(xa, &index, ULONG_MAX,
  								XA_PRESENT);
  				if (k <= j)
  					XA_BUG_ON(xa, index != j);
  				else if (k <= i)
  					XA_BUG_ON(xa, index != i);
  				else
  					XA_BUG_ON(xa, entry != NULL);
  
  				index = k;
  				entry = xa_find(xa, &index, ULONG_MAX,
  								XA_MARK_0);
  				if (k <= j)
  					XA_BUG_ON(xa, index != j);
  				else if (k <= i)
  					XA_BUG_ON(xa, index != i);
  				else
  					XA_BUG_ON(xa, entry != NULL);
  			}
  			xa_erase_index(xa, j);
  			XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0));
  			XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
  		}
  		xa_erase_index(xa, i);
  		XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0));
  	}
  	XA_BUG_ON(xa, !xa_empty(xa));
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1011
1012
1013
1014
1015
  }
  
  static noinline void check_find_2(struct xarray *xa)
  {
  	void *entry;
4a31896c5   Matthew Wilcox   XArray: Change xa...
1016
  	unsigned long i, j, index;
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1017

4a31896c5   Matthew Wilcox   XArray: Change xa...
1018
  	xa_for_each(xa, index, entry) {
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1019
1020
1021
1022
1023
1024
  		XA_BUG_ON(xa, true);
  	}
  
  	for (i = 0; i < 1024; i++) {
  		xa_store_index(xa, index, GFP_KERNEL);
  		j = 0;
4a31896c5   Matthew Wilcox   XArray: Change xa...
1025
  		xa_for_each(xa, index, entry) {
b7677a132   Matthew Wilcox   XArray tests: Han...
1026
  			XA_BUG_ON(xa, xa_mk_index(index) != entry);
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1027
1028
1029
1030
1031
1032
  			XA_BUG_ON(xa, index != j++);
  		}
  	}
  
  	xa_destroy(xa);
  }
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1033
1034
1035
1036
1037
1038
1039
1040
  static noinline void check_find_3(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  	unsigned long i, j, k;
  	void *entry;
  
  	for (i = 0; i < 100; i++) {
  		for (j = 0; j < 100; j++) {
490fd30f8   Matthew Wilcox   XArray tests: Add...
1041
  			rcu_read_lock();
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1042
1043
1044
1045
1046
1047
1048
1049
  			for (k = 0; k < 100; k++) {
  				xas_set(&xas, j);
  				xas_for_each_marked(&xas, entry, k, XA_MARK_0)
  					;
  				if (j > k)
  					XA_BUG_ON(xa,
  						xas.xa_node != XAS_RESTART);
  			}
490fd30f8   Matthew Wilcox   XArray tests: Add...
1050
  			rcu_read_unlock();
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1051
1052
1053
1054
1055
1056
  		}
  		xa_store_index(xa, i, GFP_KERNEL);
  		xa_set_mark(xa, i, XA_MARK_0);
  	}
  	xa_destroy(xa);
  }
430f24f94   Matthew Wilcox (Oracle)   XArray: Fix infin...
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  static noinline void check_find_4(struct xarray *xa)
  {
  	unsigned long index = 0;
  	void *entry;
  
  	xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  
  	entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT);
  	XA_BUG_ON(xa, entry != xa_mk_index(ULONG_MAX));
  
  	entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT);
  	XA_BUG_ON(xa, entry);
  
  	xa_erase_index(xa, ULONG_MAX);
  }
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1072
1073
  static noinline void check_find(struct xarray *xa)
  {
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
1074
  	unsigned i;
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1075
1076
  	check_find_1(xa);
  	check_find_2(xa);
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1077
  	check_find_3(xa);
430f24f94   Matthew Wilcox (Oracle)   XArray: Fix infin...
1078
  	check_find_4(xa);
19c30f4dd   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
1079
1080
1081
  
  	for (i = 2; i < 10; i++)
  		check_multi_find_1(xa, i);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1082
  	check_multi_find_2(xa);
bd40b17ca   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
1083
  	check_multi_find_3(xa);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1084
  }
e21a29552   Matthew Wilcox   shmem: Convert fi...
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  /* See find_swap_entry() in mm/shmem.c */
  static noinline unsigned long xa_find_entry(struct xarray *xa, void *item)
  {
  	XA_STATE(xas, xa, 0);
  	unsigned int checked = 0;
  	void *entry;
  
  	rcu_read_lock();
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		if (xas_retry(&xas, entry))
  			continue;
  		if (entry == item)
  			break;
  		checked++;
  		if ((checked % 4) != 0)
  			continue;
  		xas_pause(&xas);
  	}
  	rcu_read_unlock();
  
  	return entry ? xas.xa_index : -1;
  }
  
  static noinline void check_find_entry(struct xarray *xa)
  {
  #ifdef CONFIG_XARRAY_MULTI
  	unsigned int order;
  	unsigned long offset, index;
  
  	for (order = 0; order < 20; order++) {
  		for (offset = 0; offset < (1UL << (order + 3));
  		     offset += (1UL << order)) {
  			for (index = 0; index < (1UL << (order + 5));
  			     index += (1UL << order)) {
  				xa_store_order(xa, index, order,
b7677a132   Matthew Wilcox   XArray tests: Han...
1120
  						xa_mk_index(index), GFP_KERNEL);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1121
  				XA_BUG_ON(xa, xa_load(xa, index) !=
b7677a132   Matthew Wilcox   XArray tests: Han...
1122
  						xa_mk_index(index));
e21a29552   Matthew Wilcox   shmem: Convert fi...
1123
  				XA_BUG_ON(xa, xa_find_entry(xa,
b7677a132   Matthew Wilcox   XArray tests: Han...
1124
  						xa_mk_index(index)) != index);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
  			}
  			XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
  			xa_destroy(xa);
  		}
  	}
  #endif
  
  	XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
  	xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  	XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
b7677a132   Matthew Wilcox   XArray tests: Han...
1135
  	XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1136
1137
1138
  	xa_erase_index(xa, ULONG_MAX);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
c36d451ad   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
  static noinline void check_pause(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  	void *entry;
  	unsigned int order;
  	unsigned long index = 1;
  	unsigned int count = 0;
  
  	for (order = 0; order < order_limit; order++) {
  		XA_BUG_ON(xa, xa_store_order(xa, index, order,
  					xa_mk_index(index), GFP_KERNEL));
  		index += 1UL << order;
  	}
  
  	rcu_read_lock();
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		XA_BUG_ON(xa, entry != xa_mk_index(1UL << count));
  		count++;
  	}
  	rcu_read_unlock();
  	XA_BUG_ON(xa, count != order_limit);
  
  	count = 0;
  	xas_set(&xas, 0);
  	rcu_read_lock();
  	xas_for_each(&xas, entry, ULONG_MAX) {
  		XA_BUG_ON(xa, entry != xa_mk_index(1UL << count));
  		count++;
  		xas_pause(&xas);
  	}
  	rcu_read_unlock();
  	XA_BUG_ON(xa, count != order_limit);
  
  	xa_destroy(xa);
  }
91abab838   Matthew Wilcox (Oracle)   XArray: Fix xas_n...
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  static noinline void check_move_tiny(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  	rcu_read_lock();
  	XA_BUG_ON(xa, xas_next(&xas) != NULL);
  	XA_BUG_ON(xa, xas_next(&xas) != NULL);
  	rcu_read_unlock();
  	xa_store_index(xa, 0, GFP_KERNEL);
  	rcu_read_lock();
  	xas_set(&xas, 0);
  	XA_BUG_ON(xa, xas_next(&xas) != xa_mk_index(0));
  	XA_BUG_ON(xa, xas_next(&xas) != NULL);
  	xas_set(&xas, 0);
  	XA_BUG_ON(xa, xas_prev(&xas) != xa_mk_index(0));
  	XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  	rcu_read_unlock();
  	xa_erase_index(xa, 0);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
82a22311b   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
  static noinline void check_move_max(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 0);
  
  	xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  	rcu_read_lock();
  	XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX));
  	XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL);
  	rcu_read_unlock();
  
  	xas_set(&xas, 0);
  	rcu_read_lock();
  	XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX));
  	xas_pause(&xas);
  	XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL);
  	rcu_read_unlock();
  
  	xa_erase_index(xa, ULONG_MAX);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
  static noinline void check_move_small(struct xarray *xa, unsigned long idx)
  {
  	XA_STATE(xas, xa, 0);
  	unsigned long i;
  
  	xa_store_index(xa, 0, GFP_KERNEL);
  	xa_store_index(xa, idx, GFP_KERNEL);
  
  	rcu_read_lock();
  	for (i = 0; i < idx * 4; i++) {
  		void *entry = xas_next(&xas);
  		if (i <= idx)
  			XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
  		XA_BUG_ON(xa, xas.xa_index != i);
  		if (i == 0 || i == idx)
b7677a132   Matthew Wilcox   XArray tests: Han...
1230
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
  		else
  			XA_BUG_ON(xa, entry != NULL);
  	}
  	xas_next(&xas);
  	XA_BUG_ON(xa, xas.xa_index != i);
  
  	do {
  		void *entry = xas_prev(&xas);
  		i--;
  		if (i <= idx)
  			XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
  		XA_BUG_ON(xa, xas.xa_index != i);
  		if (i == 0 || i == idx)
b7677a132   Matthew Wilcox   XArray tests: Han...
1244
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
  		else
  			XA_BUG_ON(xa, entry != NULL);
  	} while (i > 0);
  
  	xas_set(&xas, ULONG_MAX);
  	XA_BUG_ON(xa, xas_next(&xas) != NULL);
  	XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  	XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0));
  	XA_BUG_ON(xa, xas.xa_index != 0);
  	XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  	XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  	rcu_read_unlock();
  
  	xa_erase_index(xa, 0);
  	xa_erase_index(xa, idx);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_move(struct xarray *xa)
  {
  	XA_STATE(xas, xa, (1 << 16) - 1);
  	unsigned long i;
  
  	for (i = 0; i < (1 << 16); i++)
  		XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  
  	rcu_read_lock();
  	do {
  		void *entry = xas_prev(&xas);
  		i--;
b7677a132   Matthew Wilcox   XArray tests: Han...
1275
  		XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1276
1277
1278
1279
1280
1281
1282
1283
  		XA_BUG_ON(xa, i != xas.xa_index);
  	} while (i != 0);
  
  	XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  	XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  
  	do {
  		void *entry = xas_next(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
1284
  		XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
  		XA_BUG_ON(xa, i != xas.xa_index);
  		i++;
  	} while (i < (1 << 16));
  	rcu_read_unlock();
  
  	for (i = (1 << 8); i < (1 << 15); i++)
  		xa_erase_index(xa, i);
  
  	i = xas.xa_index;
  
  	rcu_read_lock();
  	do {
  		void *entry = xas_prev(&xas);
  		i--;
  		if ((i < (1 << 8)) || (i >= (1 << 15)))
b7677a132   Matthew Wilcox   XArray tests: Han...
1300
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
  		else
  			XA_BUG_ON(xa, entry != NULL);
  		XA_BUG_ON(xa, i != xas.xa_index);
  	} while (i != 0);
  
  	XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  	XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  
  	do {
  		void *entry = xas_next(&xas);
  		if ((i < (1 << 8)) || (i >= (1 << 15)))
b7677a132   Matthew Wilcox   XArray tests: Han...
1312
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1313
1314
1315
1316
1317
1318
1319
1320
  		else
  			XA_BUG_ON(xa, entry != NULL);
  		XA_BUG_ON(xa, i != xas.xa_index);
  		i++;
  	} while (i < (1 << 16));
  	rcu_read_unlock();
  
  	xa_destroy(xa);
91abab838   Matthew Wilcox (Oracle)   XArray: Fix xas_n...
1321
  	check_move_tiny(xa);
82a22311b   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1322
  	check_move_max(xa);
91abab838   Matthew Wilcox (Oracle)   XArray: Fix xas_n...
1323

64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1324
1325
1326
1327
1328
1329
  	for (i = 0; i < 16; i++)
  		check_move_small(xa, 1UL << i);
  
  	for (i = 2; i < 16; i++)
  		check_move_small(xa, (1UL << i) - 1);
  }
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
  static noinline void xa_store_many_order(struct xarray *xa,
  		unsigned long index, unsigned order)
  {
  	XA_STATE_ORDER(xas, xa, index, order);
  	unsigned int i = 0;
  
  	do {
  		xas_lock(&xas);
  		XA_BUG_ON(xa, xas_find_conflict(&xas));
  		xas_create_range(&xas);
  		if (xas_error(&xas))
  			goto unlock;
  		for (i = 0; i < (1U << order); i++) {
b7677a132   Matthew Wilcox   XArray tests: Han...
1343
  			XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i)));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
  			xas_next(&xas);
  		}
  unlock:
  		xas_unlock(&xas);
  	} while (xas_nomem(&xas, GFP_KERNEL));
  
  	XA_BUG_ON(xa, xas_error(&xas));
  }
  
  static noinline void check_create_range_1(struct xarray *xa,
  		unsigned long index, unsigned order)
  {
  	unsigned long i;
  
  	xa_store_many_order(xa, index, order);
  	for (i = index; i < index + (1UL << order); i++)
  		xa_erase_index(xa, i);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_create_range_2(struct xarray *xa, unsigned order)
  {
  	unsigned long i;
  	unsigned long nr = 1UL << order;
  
  	for (i = 0; i < nr * nr; i += nr)
  		xa_store_many_order(xa, i, order);
  	for (i = 0; i < nr * nr; i++)
  		xa_erase_index(xa, i);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_create_range_3(void)
  {
  	XA_STATE(xas, NULL, 0);
  	xas_set_err(&xas, -EEXIST);
  	xas_create_range(&xas);
  	XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST);
  }
  
  static noinline void check_create_range_4(struct xarray *xa,
  		unsigned long index, unsigned order)
  {
  	XA_STATE_ORDER(xas, xa, index, order);
  	unsigned long base = xas.xa_index;
  	unsigned long i = 0;
  
  	xa_store_index(xa, index, GFP_KERNEL);
  	do {
  		xas_lock(&xas);
  		xas_create_range(&xas);
  		if (xas_error(&xas))
  			goto unlock;
  		for (i = 0; i < (1UL << order); i++) {
b7677a132   Matthew Wilcox   XArray tests: Han...
1398
  			void *old = xas_store(&xas, xa_mk_index(base + i));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1399
  			if (xas.xa_index == index)
b7677a132   Matthew Wilcox   XArray tests: Han...
1400
  				XA_BUG_ON(xa, old != xa_mk_index(base + i));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  			else
  				XA_BUG_ON(xa, old != NULL);
  			xas_next(&xas);
  		}
  unlock:
  		xas_unlock(&xas);
  	} while (xas_nomem(&xas, GFP_KERNEL));
  
  	XA_BUG_ON(xa, xas_error(&xas));
  
  	for (i = base; i < base + (1UL << order); i++)
  		xa_erase_index(xa, i);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_create_range(struct xarray *xa)
  {
  	unsigned int order;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1;
  
  	for (order = 0; order < max_order; order++) {
  		check_create_range_1(xa, 0, order);
  		check_create_range_1(xa, 1U << order, order);
  		check_create_range_1(xa, 2U << order, order);
  		check_create_range_1(xa, 3U << order, order);
  		check_create_range_1(xa, 1U << 24, order);
  		if (order < 10)
  			check_create_range_2(xa, order);
  
  		check_create_range_4(xa, 0, order);
  		check_create_range_4(xa, 1U << order, order);
  		check_create_range_4(xa, 2U << order, order);
  		check_create_range_4(xa, 3U << order, order);
  		check_create_range_4(xa, 1U << 24, order);
  
  		check_create_range_4(xa, 1, order);
  		check_create_range_4(xa, (1U << order) + 1, order);
  		check_create_range_4(xa, (2U << order) + 1, order);
  		check_create_range_4(xa, (2U << order) - 1, order);
  		check_create_range_4(xa, (3U << order) + 1, order);
  		check_create_range_4(xa, (3U << order) - 1, order);
  		check_create_range_4(xa, (1U << 24) + 1, order);
  	}
  
  	check_create_range_3();
  }
0e9446c35   Matthew Wilcox   xarray: Add range...
1447
1448
1449
1450
  static noinline void __check_store_range(struct xarray *xa, unsigned long first,
  		unsigned long last)
  {
  #ifdef CONFIG_XARRAY_MULTI
b7677a132   Matthew Wilcox   XArray tests: Han...
1451
  	xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL);
0e9446c35   Matthew Wilcox   xarray: Add range...
1452

b7677a132   Matthew Wilcox   XArray tests: Han...
1453
1454
  	XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first));
  	XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first));
0e9446c35   Matthew Wilcox   xarray: Add range...
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
  	XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL);
  	XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL);
  
  	xa_store_range(xa, first, last, NULL, GFP_KERNEL);
  #endif
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
  
  static noinline void check_store_range(struct xarray *xa)
  {
  	unsigned long i, j;
  
  	for (i = 0; i < 128; i++) {
  		for (j = i; j < 128; j++) {
  			__check_store_range(xa, i, j);
  			__check_store_range(xa, 128 + i, 128 + j);
  			__check_store_range(xa, 4095 + i, 4095 + j);
  			__check_store_range(xa, 4096 + i, 4096 + j);
  			__check_store_range(xa, 123456 + i, 123456 + j);
5404a7f1c   Matthew Wilcox   XArray tests: Cor...
1475
  			__check_store_range(xa, (1 << 24) + i, (1 << 24) + j);
0e9446c35   Matthew Wilcox   xarray: Add range...
1476
1477
1478
  		}
  	}
  }
8fc75643c   Matthew Wilcox (Oracle)   XArray: add xas_s...
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
  #ifdef CONFIG_XARRAY_MULTI
  static void check_split_1(struct xarray *xa, unsigned long index,
  							unsigned int order)
  {
  	XA_STATE(xas, xa, index);
  	void *entry;
  	unsigned int i = 0;
  
  	xa_store_order(xa, index, order, xa, GFP_KERNEL);
  
  	xas_split_alloc(&xas, xa, order, GFP_KERNEL);
  	xas_lock(&xas);
  	xas_split(&xas, xa, order);
  	xas_unlock(&xas);
  
  	xa_for_each(xa, index, entry) {
  		XA_BUG_ON(xa, entry != xa);
  		i++;
  	}
  	XA_BUG_ON(xa, i != 1 << order);
  
  	xa_set_mark(xa, index, XA_MARK_0);
  	XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
  
  	xa_destroy(xa);
  }
  
  static noinline void check_split(struct xarray *xa)
  {
  	unsigned int order;
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	for (order = 1; order < 2 * XA_CHUNK_SHIFT; order++) {
  		check_split_1(xa, 0, order);
  		check_split_1(xa, 1UL << order, order);
  		check_split_1(xa, 3UL << order, order);
  	}
  }
  #else
  static void check_split(struct xarray *xa) { }
  #endif
76b4e5299   Matthew Wilcox   XArray: Permit st...
1521
1522
1523
1524
1525
1526
1527
1528
  static void check_align_1(struct xarray *xa, char *name)
  {
  	int i;
  	unsigned int id;
  	unsigned long index;
  	void *entry;
  
  	for (i = 0; i < 8; i++) {
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
1529
1530
  		XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b,
  					GFP_KERNEL) != 0);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1531
1532
1533
1534
1535
1536
  		XA_BUG_ON(xa, id != i);
  	}
  	xa_for_each(xa, index, entry)
  		XA_BUG_ON(xa, xa_is_err(entry));
  	xa_destroy(xa);
  }
4a5c8d898   Matthew Wilcox   XArray: Fix xa_re...
1537
1538
1539
1540
  /*
   * We should always be able to store without allocating memory after
   * reserving a slot.
   */
2fbe967b3   Matthew Wilcox   XArray: Fix xa_er...
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
  static void check_align_2(struct xarray *xa, char *name)
  {
  	int i;
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	for (i = 0; i < 8; i++) {
  		XA_BUG_ON(xa, xa_store(xa, 0, name + i, GFP_KERNEL) != NULL);
  		xa_erase(xa, 0);
  	}
4a5c8d898   Matthew Wilcox   XArray: Fix xa_re...
1551
1552
1553
1554
1555
  	for (i = 0; i < 8; i++) {
  		XA_BUG_ON(xa, xa_reserve(xa, 0, GFP_KERNEL) != 0);
  		XA_BUG_ON(xa, xa_store(xa, 0, name + i, 0) != NULL);
  		xa_erase(xa, 0);
  	}
2fbe967b3   Matthew Wilcox   XArray: Fix xa_er...
1556
1557
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
76b4e5299   Matthew Wilcox   XArray: Permit st...
1558
1559
1560
1561
1562
1563
1564
1565
  static noinline void check_align(struct xarray *xa)
  {
  	char name[] = "Motorola 68000";
  
  	check_align_1(xa, name);
  	check_align_1(xa, name + 1);
  	check_align_1(xa, name + 2);
  	check_align_1(xa, name + 3);
2fbe967b3   Matthew Wilcox   XArray: Fix xa_er...
1566
  	check_align_2(xa, name);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1567
  }
a97e7904c   Matthew Wilcox   mm: Convert worki...
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
  static LIST_HEAD(shadow_nodes);
  
  static void test_update_node(struct xa_node *node)
  {
  	if (node->count && node->count == node->nr_values) {
  		if (list_empty(&node->private_list))
  			list_add(&shadow_nodes, &node->private_list);
  	} else {
  		if (!list_empty(&node->private_list))
  			list_del_init(&node->private_list);
  	}
  }
  
  static noinline void shadow_remove(struct xarray *xa)
  {
  	struct xa_node *node;
  
  	xa_lock(xa);
  	while ((node = list_first_entry_or_null(&shadow_nodes,
  					struct xa_node, private_list))) {
a97e7904c   Matthew Wilcox   mm: Convert worki...
1588
1589
  		XA_BUG_ON(xa, node->array != xa);
  		list_del_init(&node->private_list);
f82cd2f0b   Matthew Wilcox (Oracle)   XArray: Add priva...
1590
  		xa_delete_node(node, test_update_node);
a97e7904c   Matthew Wilcox   mm: Convert worki...
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
  	}
  	xa_unlock(xa);
  }
  
  static noinline void check_workingset(struct xarray *xa, unsigned long index)
  {
  	XA_STATE(xas, xa, index);
  	xas_set_update(&xas, test_update_node);
  
  	do {
  		xas_lock(&xas);
  		xas_store(&xas, xa_mk_value(0));
  		xas_next(&xas);
  		xas_store(&xas, xa_mk_value(1));
  		xas_unlock(&xas);
  	} while (xas_nomem(&xas, GFP_KERNEL));
  
  	XA_BUG_ON(xa, list_empty(&shadow_nodes));
  
  	xas_lock(&xas);
  	xas_next(&xas);
  	xas_store(&xas, &xas);
  	XA_BUG_ON(xa, !list_empty(&shadow_nodes));
  
  	xas_store(&xas, xa_mk_value(2));
  	xas_unlock(&xas);
  	XA_BUG_ON(xa, list_empty(&shadow_nodes));
  
  	shadow_remove(xa);
  	XA_BUG_ON(xa, !list_empty(&shadow_nodes));
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
d6427f817   Matthew Wilcox   xarray: Move mult...
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
  /*
   * Check that the pointer / value / sibling entries are accounted the
   * way we expect them to be.
   */
  static noinline void check_account(struct xarray *xa)
  {
  #ifdef CONFIG_XARRAY_MULTI
  	unsigned int order;
  
  	for (order = 1; order < 12; order++) {
  		XA_STATE(xas, xa, 1 << order);
  
  		xa_store_order(xa, 0, order, xa, GFP_KERNEL);
fffc9a260   Matthew Wilcox   XArray tests: Add...
1636
  		rcu_read_lock();
d6427f817   Matthew Wilcox   xarray: Move mult...
1637
1638
1639
1640
  		xas_load(&xas);
  		XA_BUG_ON(xa, xas.xa_node->count == 0);
  		XA_BUG_ON(xa, xas.xa_node->count > (1 << order));
  		XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
fffc9a260   Matthew Wilcox   XArray tests: Add...
1641
  		rcu_read_unlock();
d6427f817   Matthew Wilcox   xarray: Move mult...
1642

b7677a132   Matthew Wilcox   XArray tests: Han...
1643
  		xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order),
d6427f817   Matthew Wilcox   xarray: Move mult...
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
  				GFP_KERNEL);
  		XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2);
  
  		xa_erase(xa, 1 << order);
  		XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
  
  		xa_erase(xa, 0);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  #endif
  }
57417cebc   Matthew Wilcox (Oracle)   XArray: add xa_ge...
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
  static noinline void check_get_order(struct xarray *xa)
  {
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
  	unsigned int order;
  	unsigned long i, j;
  
  	for (i = 0; i < 3; i++)
  		XA_BUG_ON(xa, xa_get_order(xa, i) != 0);
  
  	for (order = 0; order < max_order; order++) {
  		for (i = 0; i < 10; i++) {
  			xa_store_order(xa, i << order, order,
  					xa_mk_index(i << order), GFP_KERNEL);
  			for (j = i << order; j < (i + 1) << order; j++)
  				XA_BUG_ON(xa, xa_get_order(xa, j) != order);
  			xa_erase(xa, i << order);
  		}
  	}
  }
687149fca   Matthew Wilcox   xarray: Destroy a...
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
  static noinline void check_destroy(struct xarray *xa)
  {
  	unsigned long index;
  
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* Destroying an empty array is a no-op */
  	xa_destroy(xa);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  	/* Destroying an array with a single entry */
  	for (index = 0; index < 1000; index++) {
  		xa_store_index(xa, index, GFP_KERNEL);
  		XA_BUG_ON(xa, xa_empty(xa));
  		xa_destroy(xa);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  
  	/* Destroying an array with a single entry at ULONG_MAX */
  	xa_store(xa, ULONG_MAX, xa, GFP_KERNEL);
  	XA_BUG_ON(xa, xa_empty(xa));
  	xa_destroy(xa);
  	XA_BUG_ON(xa, !xa_empty(xa));
  
  #ifdef CONFIG_XARRAY_MULTI
  	/* Destroying an array with a multi-index entry */
  	xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL);
  	XA_BUG_ON(xa, xa_empty(xa));
  	xa_destroy(xa);
  	XA_BUG_ON(xa, !xa_empty(xa));
  #endif
  }
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1706
  static DEFINE_XARRAY(array);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1707
1708
1709
  
  static int xarray_checks(void)
  {
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1710
  	check_xa_err(&array);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1711
  	check_xas_retry(&array);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1712
  	check_xa_load(&array);
9b89a0355   Matthew Wilcox   xarray: Add XArra...
1713
  	check_xa_mark(&array);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1714
  	check_xa_shrink(&array);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1715
  	check_xas_erase(&array);
12fd2aee6   Matthew Wilcox   XArray tests: Add...
1716
  	check_insert(&array);
41aec91f5   Matthew Wilcox   xarray: Add XArra...
1717
  	check_cmpxchg(&array);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
1718
  	check_reserve(&array);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
1719
  	check_reserve(&xa0);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1720
  	check_multi_store(&array);
57417cebc   Matthew Wilcox (Oracle)   XArray: add xa_ge...
1721
  	check_get_order(&array);
371c752dc   Matthew Wilcox   xarray: Track fre...
1722
  	check_xa_alloc();
b803b4282   Matthew Wilcox   xarray: Add XArra...
1723
  	check_find(&array);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1724
  	check_find_entry(&array);
c36d451ad   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1725
  	check_pause(&array);
d6427f817   Matthew Wilcox   xarray: Move mult...
1726
  	check_account(&array);
687149fca   Matthew Wilcox   xarray: Destroy a...
1727
  	check_destroy(&array);
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1728
  	check_move(&array);
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1729
  	check_create_range(&array);
0e9446c35   Matthew Wilcox   xarray: Add range...
1730
  	check_store_range(&array);
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
1731
  	check_store_iter(&array);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1732
  	check_align(&xa0);
8fc75643c   Matthew Wilcox (Oracle)   XArray: add xas_s...
1733
  	check_split(&array);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1734

a97e7904c   Matthew Wilcox   mm: Convert worki...
1735
1736
1737
  	check_workingset(&array, 0);
  	check_workingset(&array, 64);
  	check_workingset(&array, 4096);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
  	printk("XArray: %u of %u tests passed
  ", tests_passed, tests_run);
  	return (tests_run == tests_passed) ? 0 : -EINVAL;
  }
  
  static void xarray_exit(void)
  {
  }
  
  module_init(xarray_checks);
  module_exit(xarray_exit);
  MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
  MODULE_LICENSE("GPL");