Blame view

lib/test_xarray.c 43.3 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
dd05cf12c   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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   * Author: Matthew Wilcox <willy@infradead.org>
   */
  
  #include <linux/xarray.h>
  #include <linux/module.h>
  
  static unsigned int tests_run;
  static unsigned int tests_passed;
  
  #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...
32
33
34
35
  static void *xa_mk_index(unsigned long index)
  {
  	return xa_mk_value(index & LONG_MAX);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
36
37
  static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp)
  {
b7677a132   Matthew Wilcox   XArray tests: Han...
38
  	return xa_store(xa, index, xa_mk_index(index), gfp);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
39
  }
12fd2aee6   Matthew Wilcox   XArray tests: Add...
40
41
42
43
44
  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...
45
46
  static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp)
  {
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
47
  	u32 id;
371c752dc   Matthew Wilcox   xarray: Track fre...
48

a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
49
  	XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(index), xa_limit_32b,
371c752dc   Matthew Wilcox   xarray: Track fre...
50
51
52
  				gfp) != 0);
  	XA_BUG_ON(xa, id != index);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
53
54
  static void xa_erase_index(struct xarray *xa, unsigned long index)
  {
b7677a132   Matthew Wilcox   XArray tests: Han...
55
  	XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index));
58d6ea308   Matthew Wilcox   xarray: Add XArra...
56
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
  	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...
93
  }
b803b4282   Matthew Wilcox   xarray: Add XArra...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  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...
112
  	rcu_read_unlock();
b803b4282   Matthew Wilcox   xarray: Add XArra...
113
114
  
  	XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
bd54211b8   Matthew Wilcox   XArray tests: RCU...
115
116
  
  	rcu_read_lock();
b803b4282   Matthew Wilcox   xarray: Add XArra...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	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...
131
  		xas_store(&xas, xa_mk_index(xas.xa_index));
b803b4282   Matthew Wilcox   xarray: Add XArra...
132
133
134
135
136
137
  	}
  	xas_unlock(&xas);
  
  	xa_erase_index(xa, 0);
  	xa_erase_index(xa, 1);
  }
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
138
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
  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...
165
166
  static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index)
  {
58d6ea308   Matthew Wilcox   xarray: Add XArra...
167
168
  	unsigned int order;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1;
9b89a0355   Matthew Wilcox   xarray: Add XArra...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  	/* 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...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  	/*
  	 * 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...
204
  		xa_set_mark(xa, index + 2, XA_MARK_2);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
205
  		XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL));
b7677a132   Matthew Wilcox   XArray tests: Han...
206
  		xa_store_order(xa, index, order, xa_mk_index(index),
58d6ea308   Matthew Wilcox   xarray: Add XArra...
207
208
  				GFP_KERNEL);
  		for (i = base; i < next; i++) {
93eb07f72   Matthew Wilcox   xarray: Move mult...
209
210
211
  			XA_STATE(xas, xa, i);
  			unsigned int seen = 0;
  			void *entry;
58d6ea308   Matthew Wilcox   xarray: Add XArra...
212
  			XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
d69d287a9   Matthew Wilcox   XArray tests: Che...
213
214
  			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...
215
216
  
  			/* We should see two elements in the array */
fffc9a260   Matthew Wilcox   XArray tests: Add...
217
  			rcu_read_lock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
218
219
  			xas_for_each(&xas, entry, ULONG_MAX)
  				seen++;
fffc9a260   Matthew Wilcox   XArray tests: Add...
220
  			rcu_read_unlock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
221
222
223
224
225
  			XA_BUG_ON(xa, seen != 2);
  
  			/* One of which is marked */
  			xas_set(&xas, 0);
  			seen = 0;
fffc9a260   Matthew Wilcox   XArray tests: Add...
226
  			rcu_read_lock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
227
228
  			xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
  				seen++;
fffc9a260   Matthew Wilcox   XArray tests: Add...
229
  			rcu_read_unlock();
93eb07f72   Matthew Wilcox   xarray: Move mult...
230
  			XA_BUG_ON(xa, seen != 1);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
231
232
233
234
235
236
237
238
239
  		}
  		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...
240
  }
adb9d9c4c   Matthew Wilcox   radix tree: Remov...
241
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
  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);
  }
9b89a0355   Matthew Wilcox   xarray: Add XArra...
278
279
280
281
282
283
  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...
284
285
  
  	check_xa_mark_2(xa);
9b89a0355   Matthew Wilcox   xarray: Add XArra...
286
  }
58d6ea308   Matthew Wilcox   xarray: Add XArra...
287
288
289
290
  static noinline void check_xa_shrink(struct xarray *xa)
  {
  	XA_STATE(xas, xa, 1);
  	struct xa_node *node;
93eb07f72   Matthew Wilcox   xarray: Move mult...
291
292
  	unsigned int order;
  	unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1;
58d6ea308   Matthew Wilcox   xarray: Add XArra...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  
  	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...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  
  	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...
334
  }
12fd2aee6   Matthew Wilcox   XArray tests: Add...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  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...
365
366
367
368
369
370
371
372
  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...
373
  	XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY);
41aec91f5   Matthew Wilcox   xarray: Add XArra...
374
375
376
377
378
379
380
381
382
  	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);
  	xa_erase_index(xa, 12345678);
  	xa_erase_index(xa, 5);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
383
384
385
  static noinline void check_reserve(struct xarray *xa)
  {
  	void *entry;
4a31896c5   Matthew Wilcox   XArray: Change xa...
386
  	unsigned long index;
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
387
  	int count;
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
388
389
390
  
  	/* An array with a reserved entry is not empty */
  	XA_BUG_ON(xa, !xa_empty(xa));
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
391
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
392
393
394
395
396
397
  	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...
398
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
399
400
401
402
  	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...
403
  	/* cmpxchg sees a reserved entry as ZERO */
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
404
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
405
406
  	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...
407
408
409
  	xa_release(xa, 12345678);
  	xa_erase_index(xa, 12345678);
  	XA_BUG_ON(xa, !xa_empty(xa));
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
410
  	/* xa_insert treats it as busy */
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
411
  	XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
b0606fed6   Matthew Wilcox   XArray: Honour re...
412
  	XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) !=
fd9dc93e3   Matthew Wilcox   XArray: Change xa...
413
  			-EBUSY);
b0606fed6   Matthew Wilcox   XArray: Honour re...
414
415
  	XA_BUG_ON(xa, xa_empty(xa));
  	XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL);
4c0608f4a   Matthew Wilcox   XArray: Regularis...
416
  	XA_BUG_ON(xa, !xa_empty(xa));
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
417
418
  	/* Can iterate through a reserved entry */
  	xa_store_index(xa, 5, GFP_KERNEL);
f818b82b8   Matthew Wilcox   XArray: Mark xa_i...
419
  	XA_BUG_ON(xa, xa_reserve(xa, 6, GFP_KERNEL) != 0);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
420
  	xa_store_index(xa, 7, GFP_KERNEL);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
421
  	count = 0;
4a31896c5   Matthew Wilcox   XArray: Change xa...
422
  	xa_for_each(xa, index, entry) {
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
423
  		XA_BUG_ON(xa, index != 5 && index != 7);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  		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...
440
  	}
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
441

9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
442
443
  	xa_destroy(xa);
  }
b803b4282   Matthew Wilcox   xarray: Add XArra...
444
445
446
447
448
449
450
451
452
453
454
  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...
455
  				xas_store(&xas, xa_mk_index(j));
b803b4282   Matthew Wilcox   xarray: Add XArra...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  				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...
473
  			XA_BUG_ON(xa, entry != xa_mk_index(j));
b803b4282   Matthew Wilcox   xarray: Add XArra...
474
475
476
477
478
479
480
  			xas_store(&xas, NULL);
  			j++;
  		}
  		xas_unlock(&xas);
  		XA_BUG_ON(xa, !xa_empty(xa));
  	}
  }
4f06d6302   Matthew Wilcox   xarray: Move mult...
481
482
483
484
485
486
487
  #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...
488
489
490
  	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...
491
492
  	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...
493
  	xas_lock(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
494
  	XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index));
fffc9a260   Matthew Wilcox   XArray tests: Add...
495
  	xas_unlock(&xas);
b7677a132   Matthew Wilcox   XArray tests: Han...
496
497
  	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...
498
499
500
501
502
503
504
505
506
507
508
509
  	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...
510
  	xas_lock(&xas);
4f06d6302   Matthew Wilcox   xarray: Move mult...
511
512
513
  	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...
514
  	xas_unlock(&xas);
4f06d6302   Matthew Wilcox   xarray: Move mult...
515
516
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
4f145cd66   Matthew Wilcox   XArray tests: Che...
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  
  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...
543
  #endif
58d6ea308   Matthew Wilcox   xarray: Add XArra...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
  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...
583
  	xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
584
585
586
587
588
589
590
591
592
593
  	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...
594
595
  			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...
596
597
598
599
600
601
  
  			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...
602
  					XA_BUG_ON(xa, entry != xa_mk_index(j));
58d6ea308   Matthew Wilcox   xarray: Add XArra...
603
604
605
606
607
608
  			}
  
  			xa_erase(xa, 0);
  			XA_BUG_ON(xa, !xa_empty(xa));
  		}
  	}
4f06d6302   Matthew Wilcox   xarray: Move mult...
609
610
611
612
613
614
615
  
  	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...
616
617
618
619
620
  
  	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...
621
622
  #endif
  }
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
623
  static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base)
371c752dc   Matthew Wilcox   xarray: Track fre...
624
625
626
  {
  	int i;
  	u32 id;
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
627
628
629
  	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...
630
631
  
  	/* Erasing it should make the array empty again */
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  	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...
647

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

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
651
652
653
  	/* 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...
654
655
  
  	/* Storing a value should mark it used */
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
656
657
  	xa_store_index(xa, base + 1, GFP_KERNEL);
  	xa_alloc_index(xa, base + 2, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
658

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
659
660
661
  	/* 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...
662

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
663
664
  	xa_erase_index(xa, base + 1);
  	xa_erase_index(xa, base + 2);
371c752dc   Matthew Wilcox   xarray: Track fre...
665
666
  
  	for (i = 1; i < 5000; i++) {
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
667
  		xa_alloc_index(xa, base + i, GFP_KERNEL);
371c752dc   Matthew Wilcox   xarray: Track fre...
668
  	}
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
669
  	xa_destroy(xa);
371c752dc   Matthew Wilcox   xarray: Track fre...
670

3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
671
  	/* Check that we fail properly at the limit of allocation */
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
672
673
  	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...
674
  				GFP_KERNEL) != 0);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
675
  	XA_BUG_ON(xa, id != 0xfffffffeU);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
676
677
  	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...
678
  				GFP_KERNEL) != 0);
3ccaf57a6   Matthew Wilcox   XArray: Add suppo...
679
  	XA_BUG_ON(xa, id != 0xffffffffU);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
680
681
682
683
684
  	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...
685
  	xa_destroy(xa);
48483614d   Matthew Wilcox   XArray: Fix xa_al...
686

a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
687
688
  	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...
689
  	XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0);
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
690
691
  	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...
692
693
694
  	xa_erase_index(xa, 3);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
a3e4d3f97   Matthew Wilcox   XArray: Redesign ...
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
  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...
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
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
  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...
795
796
797
798
799
800
801
  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 ...
802
803
  	check_xa_alloc_2(&xa0, 0);
  	check_xa_alloc_2(&xa1, 1);
2fa044e51   Matthew Wilcox   XArray: Add cycli...
804
805
  	check_xa_alloc_3(&xa0, 0);
  	check_xa_alloc_3(&xa1, 1);
371c752dc   Matthew Wilcox   xarray: Track fre...
806
  }
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
807
808
809
810
811
812
813
814
815
816
817
  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...
818
819
  		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...
820
821
  		count++;
  	}
b7677a132   Matthew Wilcox   XArray tests: Han...
822
  	xas_store(&xas, xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
823
824
825
826
827
828
829
  	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...
830
  	XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
831
  	XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) !=
b7677a132   Matthew Wilcox   XArray tests: Han...
832
  			xa_mk_index(start));
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
  	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));
  }
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
873
  static noinline void check_multi_find_1(struct xarray *xa, unsigned order)
b803b4282   Matthew Wilcox   xarray: Add XArra...
874
875
  {
  #ifdef CONFIG_XARRAY_MULTI
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
876
877
  	unsigned long multi = 3 << order;
  	unsigned long next = 4 << order;
b803b4282   Matthew Wilcox   xarray: Add XArra...
878
  	unsigned long index;
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
879
880
  	xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL);
  	XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
dd05cf12c   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
881
  	XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL);
b803b4282   Matthew Wilcox   xarray: Add XArra...
882
883
884
  
  	index = 0;
  	XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
885
886
887
  			xa_mk_value(multi));
  	XA_BUG_ON(xa, index != multi);
  	index = multi + 1;
b803b4282   Matthew Wilcox   xarray: Add XArra...
888
  	XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
889
890
  			xa_mk_value(multi));
  	XA_BUG_ON(xa, (index < multi) || (index >= next));
b803b4282   Matthew Wilcox   xarray: Add XArra...
891
  	XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
892
893
  			xa_mk_value(next));
  	XA_BUG_ON(xa, index != next);
dd05cf12c   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
894
895
  	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...
896

db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
897
898
  	xa_erase_index(xa, multi);
  	xa_erase_index(xa, next);
dd05cf12c   Matthew Wilcox (Oracle)   XArray: Fix xas_f...
899
  	xa_erase_index(xa, next + 1);
b803b4282   Matthew Wilcox   xarray: Add XArra...
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
  	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...
915
  			xa_store_order(xa, index, i, xa_mk_index(index),
b803b4282   Matthew Wilcox   xarray: Add XArra...
916
917
918
919
920
921
922
923
924
925
926
  					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));
  		}
  	}
  }
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
927
  static noinline void check_find_1(struct xarray *xa)
b803b4282   Matthew Wilcox   xarray: Add XArra...
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  {
  	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...
973
974
975
976
977
  }
  
  static noinline void check_find_2(struct xarray *xa)
  {
  	void *entry;
4a31896c5   Matthew Wilcox   XArray: Change xa...
978
  	unsigned long i, j, index;
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
979

4a31896c5   Matthew Wilcox   XArray: Change xa...
980
  	xa_for_each(xa, index, entry) {
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
981
982
983
984
985
986
  		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...
987
  		xa_for_each(xa, index, entry) {
b7677a132   Matthew Wilcox   XArray tests: Han...
988
  			XA_BUG_ON(xa, xa_mk_index(index) != entry);
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
989
990
991
992
993
994
  			XA_BUG_ON(xa, index != j++);
  		}
  	}
  
  	xa_destroy(xa);
  }
48483614d   Matthew Wilcox   XArray: Fix xa_al...
995
996
997
998
999
1000
1001
1002
  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...
1003
  			rcu_read_lock();
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1004
1005
1006
1007
1008
1009
1010
1011
  			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...
1012
  			rcu_read_unlock();
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1013
1014
1015
1016
1017
1018
  		}
  		xa_store_index(xa, i, GFP_KERNEL);
  		xa_set_mark(xa, i, XA_MARK_0);
  	}
  	xa_destroy(xa);
  }
a5135ca1f   Matthew Wilcox (Oracle)   XArray: Fix infin...
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
  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...
1034
1035
  static noinline void check_find(struct xarray *xa)
  {
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
1036
  	unsigned i;
8229706e0   Matthew Wilcox   XArray: Fix xa_fo...
1037
1038
  	check_find_1(xa);
  	check_find_2(xa);
48483614d   Matthew Wilcox   XArray: Fix xa_al...
1039
  	check_find_3(xa);
a5135ca1f   Matthew Wilcox (Oracle)   XArray: Fix infin...
1040
  	check_find_4(xa);
db3856128   Matthew Wilcox (Oracle)   XArray: Fix xa_fi...
1041
1042
1043
  
  	for (i = 2; i < 10; i++)
  		check_multi_find_1(xa, i);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1044
1045
  	check_multi_find_2(xa);
  }
e21a29552   Matthew Wilcox   shmem: Convert fi...
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  /* 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...
1081
  						xa_mk_index(index), GFP_KERNEL);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1082
  				XA_BUG_ON(xa, xa_load(xa, index) !=
b7677a132   Matthew Wilcox   XArray tests: Han...
1083
  						xa_mk_index(index));
e21a29552   Matthew Wilcox   shmem: Convert fi...
1084
  				XA_BUG_ON(xa, xa_find_entry(xa,
b7677a132   Matthew Wilcox   XArray tests: Han...
1085
  						xa_mk_index(index)) != index);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
  			}
  			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...
1096
  	XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1097
1098
1099
  	xa_erase_index(xa, ULONG_MAX);
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
91abab838   Matthew Wilcox (Oracle)   XArray: Fix xas_n...
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  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));
  }
08022255a   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  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...
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
  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...
1156
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  		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...
1170
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
  		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...
1201
  		XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1202
1203
1204
1205
1206
1207
1208
1209
  		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...
1210
  		XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
  		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...
1226
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
  		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...
1238
  			XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1239
1240
1241
1242
1243
1244
1245
1246
  		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...
1247
  	check_move_tiny(xa);
08022255a   Matthew Wilcox (Oracle)   XArray: Fix xas_p...
1248
  	check_move_max(xa);
91abab838   Matthew Wilcox (Oracle)   XArray: Fix xas_n...
1249

64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1250
1251
1252
1253
1254
1255
  	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...
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  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...
1269
  			XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i)));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  			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...
1324
  			void *old = xas_store(&xas, xa_mk_index(base + i));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1325
  			if (xas.xa_index == index)
b7677a132   Matthew Wilcox   XArray tests: Han...
1326
  				XA_BUG_ON(xa, old != xa_mk_index(base + i));
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
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
  			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...
1373
1374
1375
1376
  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...
1377
  	xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL);
0e9446c35   Matthew Wilcox   xarray: Add range...
1378

b7677a132   Matthew Wilcox   XArray tests: Han...
1379
1380
  	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...
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
  	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...
1401
  			__check_store_range(xa, (1 << 24) + i, (1 << 24) + j);
0e9446c35   Matthew Wilcox   xarray: Add range...
1402
1403
1404
  		}
  	}
  }
76b4e5299   Matthew Wilcox   XArray: Permit st...
1405
1406
1407
1408
1409
1410
1411
1412
  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 ...
1413
1414
  		XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b,
  					GFP_KERNEL) != 0);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1415
1416
1417
1418
1419
1420
  		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...
1421
1422
1423
1424
  /*
   * We should always be able to store without allocating memory after
   * reserving a slot.
   */
2fbe967b3   Matthew Wilcox   XArray: Fix xa_er...
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
  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...
1435
1436
1437
1438
1439
  	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...
1440
1441
  	XA_BUG_ON(xa, !xa_empty(xa));
  }
76b4e5299   Matthew Wilcox   XArray: Permit st...
1442
1443
1444
1445
1446
1447
1448
1449
  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...
1450
  	check_align_2(xa, name);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1451
  }
a97e7904c   Matthew Wilcox   mm: Convert worki...
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
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
  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))) {
  		XA_STATE(xas, node->array, 0);
  		XA_BUG_ON(xa, node->array != xa);
  		list_del_init(&node->private_list);
  		xas.xa_node = xa_parent_locked(node->array, node);
  		xas.xa_offset = node->offset;
  		xas.xa_shift = node->shift + XA_CHUNK_SHIFT;
  		xas_set_update(&xas, test_update_node);
  		xas_store(&xas, NULL);
  	}
  	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...
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
  /*
   * 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...
1525
  		rcu_read_lock();
d6427f817   Matthew Wilcox   xarray: Move mult...
1526
1527
1528
1529
  		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...
1530
  		rcu_read_unlock();
d6427f817   Matthew Wilcox   xarray: Move mult...
1531

b7677a132   Matthew Wilcox   XArray tests: Han...
1532
  		xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order),
d6427f817   Matthew Wilcox   xarray: Move mult...
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
  				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
  }
687149fca   Matthew Wilcox   xarray: Destroy a...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
  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...
1576
  static DEFINE_XARRAY(array);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1577
1578
1579
  
  static int xarray_checks(void)
  {
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1580
  	check_xa_err(&array);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1581
  	check_xas_retry(&array);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1582
  	check_xa_load(&array);
9b89a0355   Matthew Wilcox   xarray: Add XArra...
1583
  	check_xa_mark(&array);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1584
  	check_xa_shrink(&array);
b803b4282   Matthew Wilcox   xarray: Add XArra...
1585
  	check_xas_erase(&array);
12fd2aee6   Matthew Wilcox   XArray tests: Add...
1586
  	check_insert(&array);
41aec91f5   Matthew Wilcox   xarray: Add XArra...
1587
  	check_cmpxchg(&array);
9f14d4f1f   Matthew Wilcox   xarray: Add xa_re...
1588
  	check_reserve(&array);
b38f6c502   Matthew Wilcox   XArray: Fix xa_re...
1589
  	check_reserve(&xa0);
58d6ea308   Matthew Wilcox   xarray: Add XArra...
1590
  	check_multi_store(&array);
371c752dc   Matthew Wilcox   xarray: Track fre...
1591
  	check_xa_alloc();
b803b4282   Matthew Wilcox   xarray: Add XArra...
1592
  	check_find(&array);
e21a29552   Matthew Wilcox   shmem: Convert fi...
1593
  	check_find_entry(&array);
d6427f817   Matthew Wilcox   xarray: Move mult...
1594
  	check_account(&array);
687149fca   Matthew Wilcox   xarray: Destroy a...
1595
  	check_destroy(&array);
64d3e9a9e   Matthew Wilcox   xarray: Step thro...
1596
  	check_move(&array);
2264f5132   Matthew Wilcox   xarray: Add xas_c...
1597
  	check_create_range(&array);
0e9446c35   Matthew Wilcox   xarray: Add range...
1598
  	check_store_range(&array);
4e99d4e95   Matthew Wilcox   xarray: Add xas_f...
1599
  	check_store_iter(&array);
76b4e5299   Matthew Wilcox   XArray: Permit st...
1600
  	check_align(&xa0);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1601

a97e7904c   Matthew Wilcox   mm: Convert worki...
1602
1603
1604
  	check_workingset(&array, 0);
  	check_workingset(&array, 64);
  	check_workingset(&array, 4096);
ad3d6c726   Matthew Wilcox   xarray: Add XArra...
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
  	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");