~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/test_xarray.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/test_xarray.c (Version linux-6.12-rc7) and /lib/test_xarray.c (Version linux-5.9.16)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php