test_xarray.c 59 KB

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