test_xarray.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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(struct xarray *xa)
  251. {
  252. unsigned long index;
  253. for (index = 0; index < 16384; index += 4)
  254. check_xa_mark_1(xa, index);
  255. check_xa_mark_2(xa);
  256. }
  257. static noinline void check_xa_shrink(struct xarray *xa)
  258. {
  259. XA_STATE(xas, xa, 1);
  260. struct xa_node *node;
  261. unsigned int order;
  262. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1;
  263. XA_BUG_ON(xa, !xa_empty(xa));
  264. XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL);
  265. XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
  266. /*
  267. * Check that erasing the entry at 1 shrinks the tree and properly
  268. * marks the node as being deleted.
  269. */
  270. xas_lock(&xas);
  271. XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1));
  272. node = xas.xa_node;
  273. XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0));
  274. XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
  275. XA_BUG_ON(xa, xa_load(xa, 1) != NULL);
  276. XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS);
  277. XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY);
  278. XA_BUG_ON(xa, xas_load(&xas) != NULL);
  279. xas_unlock(&xas);
  280. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  281. xa_erase_index(xa, 0);
  282. XA_BUG_ON(xa, !xa_empty(xa));
  283. for (order = 0; order < max_order; order++) {
  284. unsigned long max = (1UL << order) - 1;
  285. xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL);
  286. XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0));
  287. XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
  288. rcu_read_lock();
  289. node = xa_head(xa);
  290. rcu_read_unlock();
  291. XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) !=
  292. NULL);
  293. rcu_read_lock();
  294. XA_BUG_ON(xa, xa_head(xa) == node);
  295. rcu_read_unlock();
  296. XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
  297. xa_erase_index(xa, ULONG_MAX);
  298. XA_BUG_ON(xa, xa->xa_head != node);
  299. xa_erase_index(xa, 0);
  300. }
  301. }
  302. static noinline void check_insert(struct xarray *xa)
  303. {
  304. unsigned long i;
  305. for (i = 0; i < 1024; i++) {
  306. xa_insert_index(xa, i);
  307. XA_BUG_ON(xa, xa_load(xa, i - 1) != NULL);
  308. XA_BUG_ON(xa, xa_load(xa, i + 1) != NULL);
  309. xa_erase_index(xa, i);
  310. }
  311. for (i = 10; i < BITS_PER_LONG; i++) {
  312. xa_insert_index(xa, 1UL << i);
  313. XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 1) != NULL);
  314. XA_BUG_ON(xa, xa_load(xa, (1UL << i) + 1) != NULL);
  315. xa_erase_index(xa, 1UL << i);
  316. xa_insert_index(xa, (1UL << i) - 1);
  317. XA_BUG_ON(xa, xa_load(xa, (1UL << i) - 2) != NULL);
  318. XA_BUG_ON(xa, xa_load(xa, 1UL << i) != NULL);
  319. xa_erase_index(xa, (1UL << i) - 1);
  320. }
  321. xa_insert_index(xa, ~0UL);
  322. XA_BUG_ON(xa, xa_load(xa, 0UL) != NULL);
  323. XA_BUG_ON(xa, xa_load(xa, ~1UL) != NULL);
  324. xa_erase_index(xa, ~0UL);
  325. XA_BUG_ON(xa, !xa_empty(xa));
  326. }
  327. static noinline void check_cmpxchg(struct xarray *xa)
  328. {
  329. void *FIVE = xa_mk_value(5);
  330. void *SIX = xa_mk_value(6);
  331. void *LOTS = xa_mk_value(12345678);
  332. XA_BUG_ON(xa, !xa_empty(xa));
  333. XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL);
  334. XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY);
  335. XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS);
  336. XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS);
  337. XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE);
  338. XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL);
  339. XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL);
  340. xa_erase_index(xa, 12345678);
  341. xa_erase_index(xa, 5);
  342. XA_BUG_ON(xa, !xa_empty(xa));
  343. }
  344. static noinline void check_reserve(struct xarray *xa)
  345. {
  346. void *entry;
  347. unsigned long index;
  348. int count;
  349. /* An array with a reserved entry is not empty */
  350. XA_BUG_ON(xa, !xa_empty(xa));
  351. XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
  352. XA_BUG_ON(xa, xa_empty(xa));
  353. XA_BUG_ON(xa, xa_load(xa, 12345678));
  354. xa_release(xa, 12345678);
  355. XA_BUG_ON(xa, !xa_empty(xa));
  356. /* Releasing a used entry does nothing */
  357. XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
  358. XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL);
  359. xa_release(xa, 12345678);
  360. xa_erase_index(xa, 12345678);
  361. XA_BUG_ON(xa, !xa_empty(xa));
  362. /* cmpxchg sees a reserved entry as ZERO */
  363. XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
  364. XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, XA_ZERO_ENTRY,
  365. xa_mk_value(12345678), GFP_NOWAIT) != NULL);
  366. xa_release(xa, 12345678);
  367. xa_erase_index(xa, 12345678);
  368. XA_BUG_ON(xa, !xa_empty(xa));
  369. /* xa_insert treats it as busy */
  370. XA_BUG_ON(xa, xa_reserve(xa, 12345678, GFP_KERNEL) != 0);
  371. XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) !=
  372. -EBUSY);
  373. XA_BUG_ON(xa, xa_empty(xa));
  374. XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL);
  375. XA_BUG_ON(xa, !xa_empty(xa));
  376. /* Can iterate through a reserved entry */
  377. xa_store_index(xa, 5, GFP_KERNEL);
  378. XA_BUG_ON(xa, xa_reserve(xa, 6, GFP_KERNEL) != 0);
  379. xa_store_index(xa, 7, GFP_KERNEL);
  380. count = 0;
  381. xa_for_each(xa, index, entry) {
  382. XA_BUG_ON(xa, index != 5 && index != 7);
  383. count++;
  384. }
  385. XA_BUG_ON(xa, count != 2);
  386. /* If we free a reserved entry, we should be able to allocate it */
  387. if (xa->xa_flags & XA_FLAGS_ALLOC) {
  388. u32 id;
  389. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(8),
  390. XA_LIMIT(5, 10), GFP_KERNEL) != 0);
  391. XA_BUG_ON(xa, id != 8);
  392. xa_release(xa, 6);
  393. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_value(6),
  394. XA_LIMIT(5, 10), GFP_KERNEL) != 0);
  395. XA_BUG_ON(xa, id != 6);
  396. }
  397. xa_destroy(xa);
  398. }
  399. static noinline void check_xas_erase(struct xarray *xa)
  400. {
  401. XA_STATE(xas, xa, 0);
  402. void *entry;
  403. unsigned long i, j;
  404. for (i = 0; i < 200; i++) {
  405. for (j = i; j < 2 * i + 17; j++) {
  406. xas_set(&xas, j);
  407. do {
  408. xas_lock(&xas);
  409. xas_store(&xas, xa_mk_index(j));
  410. xas_unlock(&xas);
  411. } while (xas_nomem(&xas, GFP_KERNEL));
  412. }
  413. xas_set(&xas, ULONG_MAX);
  414. do {
  415. xas_lock(&xas);
  416. xas_store(&xas, xa_mk_value(0));
  417. xas_unlock(&xas);
  418. } while (xas_nomem(&xas, GFP_KERNEL));
  419. xas_lock(&xas);
  420. xas_store(&xas, NULL);
  421. xas_set(&xas, 0);
  422. j = i;
  423. xas_for_each(&xas, entry, ULONG_MAX) {
  424. XA_BUG_ON(xa, entry != xa_mk_index(j));
  425. xas_store(&xas, NULL);
  426. j++;
  427. }
  428. xas_unlock(&xas);
  429. XA_BUG_ON(xa, !xa_empty(xa));
  430. }
  431. }
  432. #ifdef CONFIG_XARRAY_MULTI
  433. static noinline void check_multi_store_1(struct xarray *xa, unsigned long index,
  434. unsigned int order)
  435. {
  436. XA_STATE(xas, xa, index);
  437. unsigned long min = index & ~((1UL << order) - 1);
  438. unsigned long max = min + (1UL << order);
  439. xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
  440. XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(index));
  441. XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(index));
  442. XA_BUG_ON(xa, xa_load(xa, max) != NULL);
  443. XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
  444. xas_lock(&xas);
  445. XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index));
  446. xas_unlock(&xas);
  447. XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(min));
  448. XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(min));
  449. XA_BUG_ON(xa, xa_load(xa, max) != NULL);
  450. XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
  451. xa_erase_index(xa, min);
  452. XA_BUG_ON(xa, !xa_empty(xa));
  453. }
  454. static noinline void check_multi_store_2(struct xarray *xa, unsigned long index,
  455. unsigned int order)
  456. {
  457. XA_STATE(xas, xa, index);
  458. xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL);
  459. xas_lock(&xas);
  460. XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0));
  461. XA_BUG_ON(xa, xas.xa_index != index);
  462. XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
  463. xas_unlock(&xas);
  464. XA_BUG_ON(xa, !xa_empty(xa));
  465. }
  466. static noinline void check_multi_store_3(struct xarray *xa, unsigned long index,
  467. unsigned int order)
  468. {
  469. XA_STATE(xas, xa, 0);
  470. void *entry;
  471. int n = 0;
  472. xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
  473. xas_lock(&xas);
  474. xas_for_each(&xas, entry, ULONG_MAX) {
  475. XA_BUG_ON(xa, entry != xa_mk_index(index));
  476. n++;
  477. }
  478. XA_BUG_ON(xa, n != 1);
  479. xas_set(&xas, index + 1);
  480. xas_for_each(&xas, entry, ULONG_MAX) {
  481. XA_BUG_ON(xa, entry != xa_mk_index(index));
  482. n++;
  483. }
  484. XA_BUG_ON(xa, n != 2);
  485. xas_unlock(&xas);
  486. xa_destroy(xa);
  487. }
  488. #endif
  489. static noinline void check_multi_store(struct xarray *xa)
  490. {
  491. #ifdef CONFIG_XARRAY_MULTI
  492. unsigned long i, j, k;
  493. unsigned int max_order = (sizeof(long) == 4) ? 30 : 60;
  494. /* Loading from any position returns the same value */
  495. xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL);
  496. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  497. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  498. XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  499. rcu_read_lock();
  500. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2);
  501. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  502. rcu_read_unlock();
  503. /* Storing adjacent to the value does not alter the value */
  504. xa_store(xa, 3, xa, GFP_KERNEL);
  505. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  506. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  507. XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  508. rcu_read_lock();
  509. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3);
  510. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  511. rcu_read_unlock();
  512. /* Overwriting multiple indexes works */
  513. xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL);
  514. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1));
  515. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1));
  516. XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1));
  517. XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1));
  518. XA_BUG_ON(xa, xa_load(xa, 4) != NULL);
  519. rcu_read_lock();
  520. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4);
  521. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4);
  522. rcu_read_unlock();
  523. /* We can erase multiple values with a single store */
  524. xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL);
  525. XA_BUG_ON(xa, !xa_empty(xa));
  526. /* Even when the first slot is empty but the others aren't */
  527. xa_store_index(xa, 1, GFP_KERNEL);
  528. xa_store_index(xa, 2, GFP_KERNEL);
  529. xa_store_order(xa, 0, 2, NULL, GFP_KERNEL);
  530. XA_BUG_ON(xa, !xa_empty(xa));
  531. for (i = 0; i < max_order; i++) {
  532. for (j = 0; j < max_order; j++) {
  533. xa_store_order(xa, 0, i, xa_mk_index(i), GFP_KERNEL);
  534. xa_store_order(xa, 0, j, xa_mk_index(j), GFP_KERNEL);
  535. for (k = 0; k < max_order; k++) {
  536. void *entry = xa_load(xa, (1UL << k) - 1);
  537. if ((i < k) && (j < k))
  538. XA_BUG_ON(xa, entry != NULL);
  539. else
  540. XA_BUG_ON(xa, entry != xa_mk_index(j));
  541. }
  542. xa_erase(xa, 0);
  543. XA_BUG_ON(xa, !xa_empty(xa));
  544. }
  545. }
  546. for (i = 0; i < 20; i++) {
  547. check_multi_store_1(xa, 200, i);
  548. check_multi_store_1(xa, 0, i);
  549. check_multi_store_1(xa, (1UL << i) + 1, i);
  550. }
  551. check_multi_store_2(xa, 4095, 9);
  552. for (i = 1; i < 20; i++) {
  553. check_multi_store_3(xa, 0, i);
  554. check_multi_store_3(xa, 1UL << i, i);
  555. }
  556. #endif
  557. }
  558. static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base)
  559. {
  560. int i;
  561. u32 id;
  562. XA_BUG_ON(xa, !xa_empty(xa));
  563. /* An empty array should assign %base to the first alloc */
  564. xa_alloc_index(xa, base, GFP_KERNEL);
  565. /* Erasing it should make the array empty again */
  566. xa_erase_index(xa, base);
  567. XA_BUG_ON(xa, !xa_empty(xa));
  568. /* And it should assign %base again */
  569. xa_alloc_index(xa, base, GFP_KERNEL);
  570. /* Allocating and then erasing a lot should not lose base */
  571. for (i = base + 1; i < 2 * XA_CHUNK_SIZE; i++)
  572. xa_alloc_index(xa, i, GFP_KERNEL);
  573. for (i = base; i < 2 * XA_CHUNK_SIZE; i++)
  574. xa_erase_index(xa, i);
  575. xa_alloc_index(xa, base, GFP_KERNEL);
  576. /* Destroying the array should do the same as erasing */
  577. xa_destroy(xa);
  578. /* And it should assign %base again */
  579. xa_alloc_index(xa, base, GFP_KERNEL);
  580. /* The next assigned ID should be base+1 */
  581. xa_alloc_index(xa, base + 1, GFP_KERNEL);
  582. xa_erase_index(xa, base + 1);
  583. /* Storing a value should mark it used */
  584. xa_store_index(xa, base + 1, GFP_KERNEL);
  585. xa_alloc_index(xa, base + 2, GFP_KERNEL);
  586. /* If we then erase base, it should be free */
  587. xa_erase_index(xa, base);
  588. xa_alloc_index(xa, base, GFP_KERNEL);
  589. xa_erase_index(xa, base + 1);
  590. xa_erase_index(xa, base + 2);
  591. for (i = 1; i < 5000; i++) {
  592. xa_alloc_index(xa, base + i, GFP_KERNEL);
  593. }
  594. xa_destroy(xa);
  595. /* Check that we fail properly at the limit of allocation */
  596. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX - 1),
  597. XA_LIMIT(UINT_MAX - 1, UINT_MAX),
  598. GFP_KERNEL) != 0);
  599. XA_BUG_ON(xa, id != 0xfffffffeU);
  600. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX),
  601. XA_LIMIT(UINT_MAX - 1, UINT_MAX),
  602. GFP_KERNEL) != 0);
  603. XA_BUG_ON(xa, id != 0xffffffffU);
  604. id = 3;
  605. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(0),
  606. XA_LIMIT(UINT_MAX - 1, UINT_MAX),
  607. GFP_KERNEL) != -EBUSY);
  608. XA_BUG_ON(xa, id != 3);
  609. xa_destroy(xa);
  610. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
  611. GFP_KERNEL) != -EBUSY);
  612. XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0);
  613. XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
  614. GFP_KERNEL) != -EBUSY);
  615. xa_erase_index(xa, 3);
  616. XA_BUG_ON(xa, !xa_empty(xa));
  617. }
  618. static noinline void check_xa_alloc_2(struct xarray *xa, unsigned int base)
  619. {
  620. unsigned int i, id;
  621. unsigned long index;
  622. void *entry;
  623. /* Allocate and free a NULL and check xa_empty() behaves */
  624. XA_BUG_ON(xa, !xa_empty(xa));
  625. XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  626. XA_BUG_ON(xa, id != base);
  627. XA_BUG_ON(xa, xa_empty(xa));
  628. XA_BUG_ON(xa, xa_erase(xa, id) != NULL);
  629. XA_BUG_ON(xa, !xa_empty(xa));
  630. /* Ditto, but check destroy instead of erase */
  631. XA_BUG_ON(xa, !xa_empty(xa));
  632. XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  633. XA_BUG_ON(xa, id != base);
  634. XA_BUG_ON(xa, xa_empty(xa));
  635. xa_destroy(xa);
  636. XA_BUG_ON(xa, !xa_empty(xa));
  637. for (i = base; i < base + 10; i++) {
  638. XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b,
  639. GFP_KERNEL) != 0);
  640. XA_BUG_ON(xa, id != i);
  641. }
  642. XA_BUG_ON(xa, xa_store(xa, 3, xa_mk_index(3), GFP_KERNEL) != NULL);
  643. XA_BUG_ON(xa, xa_store(xa, 4, xa_mk_index(4), GFP_KERNEL) != NULL);
  644. XA_BUG_ON(xa, xa_store(xa, 4, NULL, GFP_KERNEL) != xa_mk_index(4));
  645. XA_BUG_ON(xa, xa_erase(xa, 5) != NULL);
  646. XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
  647. XA_BUG_ON(xa, id != 5);
  648. xa_for_each(xa, index, entry) {
  649. xa_erase_index(xa, index);
  650. }
  651. for (i = base; i < base + 9; i++) {
  652. XA_BUG_ON(xa, xa_erase(xa, i) != NULL);
  653. XA_BUG_ON(xa, xa_empty(xa));
  654. }
  655. XA_BUG_ON(xa, xa_erase(xa, 8) != NULL);
  656. XA_BUG_ON(xa, xa_empty(xa));
  657. XA_BUG_ON(xa, xa_erase(xa, base + 9) != NULL);
  658. XA_BUG_ON(xa, !xa_empty(xa));
  659. xa_destroy(xa);
  660. }
  661. static noinline void check_xa_alloc_3(struct xarray *xa, unsigned int base)
  662. {
  663. struct xa_limit limit = XA_LIMIT(1, 0x3fff);
  664. u32 next = 0;
  665. unsigned int i, id;
  666. unsigned long index;
  667. void *entry;
  668. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(1), limit,
  669. &next, GFP_KERNEL) != 0);
  670. XA_BUG_ON(xa, id != 1);
  671. next = 0x3ffd;
  672. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(0x3ffd), limit,
  673. &next, GFP_KERNEL) != 0);
  674. XA_BUG_ON(xa, id != 0x3ffd);
  675. xa_erase_index(xa, 0x3ffd);
  676. xa_erase_index(xa, 1);
  677. XA_BUG_ON(xa, !xa_empty(xa));
  678. for (i = 0x3ffe; i < 0x4003; i++) {
  679. if (i < 0x4000)
  680. entry = xa_mk_index(i);
  681. else
  682. entry = xa_mk_index(i - 0x3fff);
  683. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, entry, limit,
  684. &next, GFP_KERNEL) != (id == 1));
  685. XA_BUG_ON(xa, xa_mk_index(id) != entry);
  686. }
  687. /* Check wrap-around is handled correctly */
  688. if (base != 0)
  689. xa_erase_index(xa, base);
  690. xa_erase_index(xa, base + 1);
  691. next = UINT_MAX;
  692. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(UINT_MAX),
  693. xa_limit_32b, &next, GFP_KERNEL) != 0);
  694. XA_BUG_ON(xa, id != UINT_MAX);
  695. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base),
  696. xa_limit_32b, &next, GFP_KERNEL) != 1);
  697. XA_BUG_ON(xa, id != base);
  698. XA_BUG_ON(xa, xa_alloc_cyclic(xa, &id, xa_mk_index(base + 1),
  699. xa_limit_32b, &next, GFP_KERNEL) != 0);
  700. XA_BUG_ON(xa, id != base + 1);
  701. xa_for_each(xa, index, entry)
  702. xa_erase_index(xa, index);
  703. XA_BUG_ON(xa, !xa_empty(xa));
  704. }
  705. static DEFINE_XARRAY_ALLOC(xa0);
  706. static DEFINE_XARRAY_ALLOC1(xa1);
  707. static noinline void check_xa_alloc(void)
  708. {
  709. check_xa_alloc_1(&xa0, 0);
  710. check_xa_alloc_1(&xa1, 1);
  711. check_xa_alloc_2(&xa0, 0);
  712. check_xa_alloc_2(&xa1, 1);
  713. check_xa_alloc_3(&xa0, 0);
  714. check_xa_alloc_3(&xa1, 1);
  715. }
  716. static noinline void __check_store_iter(struct xarray *xa, unsigned long start,
  717. unsigned int order, unsigned int present)
  718. {
  719. XA_STATE_ORDER(xas, xa, start, order);
  720. void *entry;
  721. unsigned int count = 0;
  722. retry:
  723. xas_lock(&xas);
  724. xas_for_each_conflict(&xas, entry) {
  725. XA_BUG_ON(xa, !xa_is_value(entry));
  726. XA_BUG_ON(xa, entry < xa_mk_index(start));
  727. XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1));
  728. count++;
  729. }
  730. xas_store(&xas, xa_mk_index(start));
  731. xas_unlock(&xas);
  732. if (xas_nomem(&xas, GFP_KERNEL)) {
  733. count = 0;
  734. goto retry;
  735. }
  736. XA_BUG_ON(xa, xas_error(&xas));
  737. XA_BUG_ON(xa, count != present);
  738. XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start));
  739. XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) !=
  740. xa_mk_index(start));
  741. xa_erase_index(xa, start);
  742. }
  743. static noinline void check_store_iter(struct xarray *xa)
  744. {
  745. unsigned int i, j;
  746. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
  747. for (i = 0; i < max_order; i++) {
  748. unsigned int min = 1 << i;
  749. unsigned int max = (2 << i) - 1;
  750. __check_store_iter(xa, 0, i, 0);
  751. XA_BUG_ON(xa, !xa_empty(xa));
  752. __check_store_iter(xa, min, i, 0);
  753. XA_BUG_ON(xa, !xa_empty(xa));
  754. xa_store_index(xa, min, GFP_KERNEL);
  755. __check_store_iter(xa, min, i, 1);
  756. XA_BUG_ON(xa, !xa_empty(xa));
  757. xa_store_index(xa, max, GFP_KERNEL);
  758. __check_store_iter(xa, min, i, 1);
  759. XA_BUG_ON(xa, !xa_empty(xa));
  760. for (j = 0; j < min; j++)
  761. xa_store_index(xa, j, GFP_KERNEL);
  762. __check_store_iter(xa, 0, i, min);
  763. XA_BUG_ON(xa, !xa_empty(xa));
  764. for (j = 0; j < min; j++)
  765. xa_store_index(xa, min + j, GFP_KERNEL);
  766. __check_store_iter(xa, min, i, min);
  767. XA_BUG_ON(xa, !xa_empty(xa));
  768. }
  769. #ifdef CONFIG_XARRAY_MULTI
  770. xa_store_index(xa, 63, GFP_KERNEL);
  771. xa_store_index(xa, 65, GFP_KERNEL);
  772. __check_store_iter(xa, 64, 2, 1);
  773. xa_erase_index(xa, 63);
  774. #endif
  775. XA_BUG_ON(xa, !xa_empty(xa));
  776. }
  777. static noinline void check_multi_find_1(struct xarray *xa, unsigned order)
  778. {
  779. #ifdef CONFIG_XARRAY_MULTI
  780. unsigned long multi = 3 << order;
  781. unsigned long next = 4 << order;
  782. unsigned long index;
  783. xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL);
  784. XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
  785. XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL);
  786. index = 0;
  787. XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
  788. xa_mk_value(multi));
  789. XA_BUG_ON(xa, index != multi);
  790. index = multi + 1;
  791. XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
  792. xa_mk_value(multi));
  793. XA_BUG_ON(xa, (index < multi) || (index >= next));
  794. XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
  795. xa_mk_value(next));
  796. XA_BUG_ON(xa, index != next);
  797. XA_BUG_ON(xa, xa_find_after(xa, &index, next, XA_PRESENT) != NULL);
  798. XA_BUG_ON(xa, index != next);
  799. xa_erase_index(xa, multi);
  800. xa_erase_index(xa, next);
  801. xa_erase_index(xa, next + 1);
  802. XA_BUG_ON(xa, !xa_empty(xa));
  803. #endif
  804. }
  805. static noinline void check_multi_find_2(struct xarray *xa)
  806. {
  807. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1;
  808. unsigned int i, j;
  809. void *entry;
  810. for (i = 0; i < max_order; i++) {
  811. unsigned long index = 1UL << i;
  812. for (j = 0; j < index; j++) {
  813. XA_STATE(xas, xa, j + index);
  814. xa_store_index(xa, index - 1, GFP_KERNEL);
  815. xa_store_order(xa, index, i, xa_mk_index(index),
  816. GFP_KERNEL);
  817. rcu_read_lock();
  818. xas_for_each(&xas, entry, ULONG_MAX) {
  819. xa_erase_index(xa, index);
  820. }
  821. rcu_read_unlock();
  822. xa_erase_index(xa, index - 1);
  823. XA_BUG_ON(xa, !xa_empty(xa));
  824. }
  825. }
  826. }
  827. static noinline void check_multi_find_3(struct xarray *xa)
  828. {
  829. unsigned int order;
  830. for (order = 5; order < order_limit; order++) {
  831. unsigned long index = 1UL << (order - 5);
  832. XA_BUG_ON(xa, !xa_empty(xa));
  833. xa_store_order(xa, 0, order - 4, xa_mk_index(0), GFP_KERNEL);
  834. XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT));
  835. xa_erase_index(xa, 0);
  836. }
  837. }
  838. static noinline void check_find_1(struct xarray *xa)
  839. {
  840. unsigned long i, j, k;
  841. XA_BUG_ON(xa, !xa_empty(xa));
  842. /*
  843. * Check xa_find with all pairs between 0 and 99 inclusive,
  844. * starting at every index between 0 and 99
  845. */
  846. for (i = 0; i < 100; i++) {
  847. XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  848. xa_set_mark(xa, i, XA_MARK_0);
  849. for (j = 0; j < i; j++) {
  850. XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) !=
  851. NULL);
  852. xa_set_mark(xa, j, XA_MARK_0);
  853. for (k = 0; k < 100; k++) {
  854. unsigned long index = k;
  855. void *entry = xa_find(xa, &index, ULONG_MAX,
  856. XA_PRESENT);
  857. if (k <= j)
  858. XA_BUG_ON(xa, index != j);
  859. else if (k <= i)
  860. XA_BUG_ON(xa, index != i);
  861. else
  862. XA_BUG_ON(xa, entry != NULL);
  863. index = k;
  864. entry = xa_find(xa, &index, ULONG_MAX,
  865. XA_MARK_0);
  866. if (k <= j)
  867. XA_BUG_ON(xa, index != j);
  868. else if (k <= i)
  869. XA_BUG_ON(xa, index != i);
  870. else
  871. XA_BUG_ON(xa, entry != NULL);
  872. }
  873. xa_erase_index(xa, j);
  874. XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0));
  875. XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
  876. }
  877. xa_erase_index(xa, i);
  878. XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0));
  879. }
  880. XA_BUG_ON(xa, !xa_empty(xa));
  881. }
  882. static noinline void check_find_2(struct xarray *xa)
  883. {
  884. void *entry;
  885. unsigned long i, j, index;
  886. xa_for_each(xa, index, entry) {
  887. XA_BUG_ON(xa, true);
  888. }
  889. for (i = 0; i < 1024; i++) {
  890. xa_store_index(xa, index, GFP_KERNEL);
  891. j = 0;
  892. xa_for_each(xa, index, entry) {
  893. XA_BUG_ON(xa, xa_mk_index(index) != entry);
  894. XA_BUG_ON(xa, index != j++);
  895. }
  896. }
  897. xa_destroy(xa);
  898. }
  899. static noinline void check_find_3(struct xarray *xa)
  900. {
  901. XA_STATE(xas, xa, 0);
  902. unsigned long i, j, k;
  903. void *entry;
  904. for (i = 0; i < 100; i++) {
  905. for (j = 0; j < 100; j++) {
  906. rcu_read_lock();
  907. for (k = 0; k < 100; k++) {
  908. xas_set(&xas, j);
  909. xas_for_each_marked(&xas, entry, k, XA_MARK_0)
  910. ;
  911. if (j > k)
  912. XA_BUG_ON(xa,
  913. xas.xa_node != XAS_RESTART);
  914. }
  915. rcu_read_unlock();
  916. }
  917. xa_store_index(xa, i, GFP_KERNEL);
  918. xa_set_mark(xa, i, XA_MARK_0);
  919. }
  920. xa_destroy(xa);
  921. }
  922. static noinline void check_find_4(struct xarray *xa)
  923. {
  924. unsigned long index = 0;
  925. void *entry;
  926. xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  927. entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT);
  928. XA_BUG_ON(xa, entry != xa_mk_index(ULONG_MAX));
  929. entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT);
  930. XA_BUG_ON(xa, entry);
  931. xa_erase_index(xa, ULONG_MAX);
  932. }
  933. static noinline void check_find(struct xarray *xa)
  934. {
  935. unsigned i;
  936. check_find_1(xa);
  937. check_find_2(xa);
  938. check_find_3(xa);
  939. check_find_4(xa);
  940. for (i = 2; i < 10; i++)
  941. check_multi_find_1(xa, i);
  942. check_multi_find_2(xa);
  943. check_multi_find_3(xa);
  944. }
  945. /* See find_swap_entry() in mm/shmem.c */
  946. static noinline unsigned long xa_find_entry(struct xarray *xa, void *item)
  947. {
  948. XA_STATE(xas, xa, 0);
  949. unsigned int checked = 0;
  950. void *entry;
  951. rcu_read_lock();
  952. xas_for_each(&xas, entry, ULONG_MAX) {
  953. if (xas_retry(&xas, entry))
  954. continue;
  955. if (entry == item)
  956. break;
  957. checked++;
  958. if ((checked % 4) != 0)
  959. continue;
  960. xas_pause(&xas);
  961. }
  962. rcu_read_unlock();
  963. return entry ? xas.xa_index : -1;
  964. }
  965. static noinline void check_find_entry(struct xarray *xa)
  966. {
  967. #ifdef CONFIG_XARRAY_MULTI
  968. unsigned int order;
  969. unsigned long offset, index;
  970. for (order = 0; order < 20; order++) {
  971. for (offset = 0; offset < (1UL << (order + 3));
  972. offset += (1UL << order)) {
  973. for (index = 0; index < (1UL << (order + 5));
  974. index += (1UL << order)) {
  975. xa_store_order(xa, index, order,
  976. xa_mk_index(index), GFP_KERNEL);
  977. XA_BUG_ON(xa, xa_load(xa, index) !=
  978. xa_mk_index(index));
  979. XA_BUG_ON(xa, xa_find_entry(xa,
  980. xa_mk_index(index)) != index);
  981. }
  982. XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
  983. xa_destroy(xa);
  984. }
  985. }
  986. #endif
  987. XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
  988. xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  989. XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
  990. XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1);
  991. xa_erase_index(xa, ULONG_MAX);
  992. XA_BUG_ON(xa, !xa_empty(xa));
  993. }
  994. static noinline void check_pause(struct xarray *xa)
  995. {
  996. XA_STATE(xas, xa, 0);
  997. void *entry;
  998. unsigned int order;
  999. unsigned long index = 1;
  1000. unsigned int count = 0;
  1001. for (order = 0; order < order_limit; order++) {
  1002. XA_BUG_ON(xa, xa_store_order(xa, index, order,
  1003. xa_mk_index(index), GFP_KERNEL));
  1004. index += 1UL << order;
  1005. }
  1006. rcu_read_lock();
  1007. xas_for_each(&xas, entry, ULONG_MAX) {
  1008. XA_BUG_ON(xa, entry != xa_mk_index(1UL << count));
  1009. count++;
  1010. }
  1011. rcu_read_unlock();
  1012. XA_BUG_ON(xa, count != order_limit);
  1013. count = 0;
  1014. xas_set(&xas, 0);
  1015. rcu_read_lock();
  1016. xas_for_each(&xas, entry, ULONG_MAX) {
  1017. XA_BUG_ON(xa, entry != xa_mk_index(1UL << count));
  1018. count++;
  1019. xas_pause(&xas);
  1020. }
  1021. rcu_read_unlock();
  1022. XA_BUG_ON(xa, count != order_limit);
  1023. xa_destroy(xa);
  1024. }
  1025. static noinline void check_move_tiny(struct xarray *xa)
  1026. {
  1027. XA_STATE(xas, xa, 0);
  1028. XA_BUG_ON(xa, !xa_empty(xa));
  1029. rcu_read_lock();
  1030. XA_BUG_ON(xa, xas_next(&xas) != NULL);
  1031. XA_BUG_ON(xa, xas_next(&xas) != NULL);
  1032. rcu_read_unlock();
  1033. xa_store_index(xa, 0, GFP_KERNEL);
  1034. rcu_read_lock();
  1035. xas_set(&xas, 0);
  1036. XA_BUG_ON(xa, xas_next(&xas) != xa_mk_index(0));
  1037. XA_BUG_ON(xa, xas_next(&xas) != NULL);
  1038. xas_set(&xas, 0);
  1039. XA_BUG_ON(xa, xas_prev(&xas) != xa_mk_index(0));
  1040. XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  1041. rcu_read_unlock();
  1042. xa_erase_index(xa, 0);
  1043. XA_BUG_ON(xa, !xa_empty(xa));
  1044. }
  1045. static noinline void check_move_max(struct xarray *xa)
  1046. {
  1047. XA_STATE(xas, xa, 0);
  1048. xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
  1049. rcu_read_lock();
  1050. XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX));
  1051. XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL);
  1052. rcu_read_unlock();
  1053. xas_set(&xas, 0);
  1054. rcu_read_lock();
  1055. XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_index(ULONG_MAX));
  1056. xas_pause(&xas);
  1057. XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != NULL);
  1058. rcu_read_unlock();
  1059. xa_erase_index(xa, ULONG_MAX);
  1060. XA_BUG_ON(xa, !xa_empty(xa));
  1061. }
  1062. static noinline void check_move_small(struct xarray *xa, unsigned long idx)
  1063. {
  1064. XA_STATE(xas, xa, 0);
  1065. unsigned long i;
  1066. xa_store_index(xa, 0, GFP_KERNEL);
  1067. xa_store_index(xa, idx, GFP_KERNEL);
  1068. rcu_read_lock();
  1069. for (i = 0; i < idx * 4; i++) {
  1070. void *entry = xas_next(&xas);
  1071. if (i <= idx)
  1072. XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
  1073. XA_BUG_ON(xa, xas.xa_index != i);
  1074. if (i == 0 || i == idx)
  1075. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1076. else
  1077. XA_BUG_ON(xa, entry != NULL);
  1078. }
  1079. xas_next(&xas);
  1080. XA_BUG_ON(xa, xas.xa_index != i);
  1081. do {
  1082. void *entry = xas_prev(&xas);
  1083. i--;
  1084. if (i <= idx)
  1085. XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
  1086. XA_BUG_ON(xa, xas.xa_index != i);
  1087. if (i == 0 || i == idx)
  1088. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1089. else
  1090. XA_BUG_ON(xa, entry != NULL);
  1091. } while (i > 0);
  1092. xas_set(&xas, ULONG_MAX);
  1093. XA_BUG_ON(xa, xas_next(&xas) != NULL);
  1094. XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  1095. XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0));
  1096. XA_BUG_ON(xa, xas.xa_index != 0);
  1097. XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  1098. XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  1099. rcu_read_unlock();
  1100. xa_erase_index(xa, 0);
  1101. xa_erase_index(xa, idx);
  1102. XA_BUG_ON(xa, !xa_empty(xa));
  1103. }
  1104. static noinline void check_move(struct xarray *xa)
  1105. {
  1106. XA_STATE(xas, xa, (1 << 16) - 1);
  1107. unsigned long i;
  1108. for (i = 0; i < (1 << 16); i++)
  1109. XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  1110. rcu_read_lock();
  1111. do {
  1112. void *entry = xas_prev(&xas);
  1113. i--;
  1114. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1115. XA_BUG_ON(xa, i != xas.xa_index);
  1116. } while (i != 0);
  1117. XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  1118. XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  1119. do {
  1120. void *entry = xas_next(&xas);
  1121. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1122. XA_BUG_ON(xa, i != xas.xa_index);
  1123. i++;
  1124. } while (i < (1 << 16));
  1125. rcu_read_unlock();
  1126. for (i = (1 << 8); i < (1 << 15); i++)
  1127. xa_erase_index(xa, i);
  1128. i = xas.xa_index;
  1129. rcu_read_lock();
  1130. do {
  1131. void *entry = xas_prev(&xas);
  1132. i--;
  1133. if ((i < (1 << 8)) || (i >= (1 << 15)))
  1134. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1135. else
  1136. XA_BUG_ON(xa, entry != NULL);
  1137. XA_BUG_ON(xa, i != xas.xa_index);
  1138. } while (i != 0);
  1139. XA_BUG_ON(xa, xas_prev(&xas) != NULL);
  1140. XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
  1141. do {
  1142. void *entry = xas_next(&xas);
  1143. if ((i < (1 << 8)) || (i >= (1 << 15)))
  1144. XA_BUG_ON(xa, entry != xa_mk_index(i));
  1145. else
  1146. XA_BUG_ON(xa, entry != NULL);
  1147. XA_BUG_ON(xa, i != xas.xa_index);
  1148. i++;
  1149. } while (i < (1 << 16));
  1150. rcu_read_unlock();
  1151. xa_destroy(xa);
  1152. check_move_tiny(xa);
  1153. check_move_max(xa);
  1154. for (i = 0; i < 16; i++)
  1155. check_move_small(xa, 1UL << i);
  1156. for (i = 2; i < 16; i++)
  1157. check_move_small(xa, (1UL << i) - 1);
  1158. }
  1159. static noinline void xa_store_many_order(struct xarray *xa,
  1160. unsigned long index, unsigned order)
  1161. {
  1162. XA_STATE_ORDER(xas, xa, index, order);
  1163. unsigned int i = 0;
  1164. do {
  1165. xas_lock(&xas);
  1166. XA_BUG_ON(xa, xas_find_conflict(&xas));
  1167. xas_create_range(&xas);
  1168. if (xas_error(&xas))
  1169. goto unlock;
  1170. for (i = 0; i < (1U << order); i++) {
  1171. XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i)));
  1172. xas_next(&xas);
  1173. }
  1174. unlock:
  1175. xas_unlock(&xas);
  1176. } while (xas_nomem(&xas, GFP_KERNEL));
  1177. XA_BUG_ON(xa, xas_error(&xas));
  1178. }
  1179. static noinline void check_create_range_1(struct xarray *xa,
  1180. unsigned long index, unsigned order)
  1181. {
  1182. unsigned long i;
  1183. xa_store_many_order(xa, index, order);
  1184. for (i = index; i < index + (1UL << order); i++)
  1185. xa_erase_index(xa, i);
  1186. XA_BUG_ON(xa, !xa_empty(xa));
  1187. }
  1188. static noinline void check_create_range_2(struct xarray *xa, unsigned order)
  1189. {
  1190. unsigned long i;
  1191. unsigned long nr = 1UL << order;
  1192. for (i = 0; i < nr * nr; i += nr)
  1193. xa_store_many_order(xa, i, order);
  1194. for (i = 0; i < nr * nr; i++)
  1195. xa_erase_index(xa, i);
  1196. XA_BUG_ON(xa, !xa_empty(xa));
  1197. }
  1198. static noinline void check_create_range_3(void)
  1199. {
  1200. XA_STATE(xas, NULL, 0);
  1201. xas_set_err(&xas, -EEXIST);
  1202. xas_create_range(&xas);
  1203. XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST);
  1204. }
  1205. static noinline void check_create_range_4(struct xarray *xa,
  1206. unsigned long index, unsigned order)
  1207. {
  1208. XA_STATE_ORDER(xas, xa, index, order);
  1209. unsigned long base = xas.xa_index;
  1210. unsigned long i = 0;
  1211. xa_store_index(xa, index, GFP_KERNEL);
  1212. do {
  1213. xas_lock(&xas);
  1214. xas_create_range(&xas);
  1215. if (xas_error(&xas))
  1216. goto unlock;
  1217. for (i = 0; i < (1UL << order); i++) {
  1218. void *old = xas_store(&xas, xa_mk_index(base + i));
  1219. if (xas.xa_index == index)
  1220. XA_BUG_ON(xa, old != xa_mk_index(base + i));
  1221. else
  1222. XA_BUG_ON(xa, old != NULL);
  1223. xas_next(&xas);
  1224. }
  1225. unlock:
  1226. xas_unlock(&xas);
  1227. } while (xas_nomem(&xas, GFP_KERNEL));
  1228. XA_BUG_ON(xa, xas_error(&xas));
  1229. for (i = base; i < base + (1UL << order); i++)
  1230. xa_erase_index(xa, i);
  1231. XA_BUG_ON(xa, !xa_empty(xa));
  1232. }
  1233. static noinline void check_create_range(struct xarray *xa)
  1234. {
  1235. unsigned int order;
  1236. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1;
  1237. for (order = 0; order < max_order; order++) {
  1238. check_create_range_1(xa, 0, order);
  1239. check_create_range_1(xa, 1U << order, order);
  1240. check_create_range_1(xa, 2U << order, order);
  1241. check_create_range_1(xa, 3U << order, order);
  1242. check_create_range_1(xa, 1U << 24, order);
  1243. if (order < 10)
  1244. check_create_range_2(xa, order);
  1245. check_create_range_4(xa, 0, order);
  1246. check_create_range_4(xa, 1U << order, order);
  1247. check_create_range_4(xa, 2U << order, order);
  1248. check_create_range_4(xa, 3U << order, order);
  1249. check_create_range_4(xa, 1U << 24, order);
  1250. check_create_range_4(xa, 1, order);
  1251. check_create_range_4(xa, (1U << order) + 1, order);
  1252. check_create_range_4(xa, (2U << order) + 1, order);
  1253. check_create_range_4(xa, (2U << order) - 1, order);
  1254. check_create_range_4(xa, (3U << order) + 1, order);
  1255. check_create_range_4(xa, (3U << order) - 1, order);
  1256. check_create_range_4(xa, (1U << 24) + 1, order);
  1257. }
  1258. check_create_range_3();
  1259. }
  1260. static noinline void __check_store_range(struct xarray *xa, unsigned long first,
  1261. unsigned long last)
  1262. {
  1263. #ifdef CONFIG_XARRAY_MULTI
  1264. xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL);
  1265. XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first));
  1266. XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first));
  1267. XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL);
  1268. XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL);
  1269. xa_store_range(xa, first, last, NULL, GFP_KERNEL);
  1270. #endif
  1271. XA_BUG_ON(xa, !xa_empty(xa));
  1272. }
  1273. static noinline void check_store_range(struct xarray *xa)
  1274. {
  1275. unsigned long i, j;
  1276. for (i = 0; i < 128; i++) {
  1277. for (j = i; j < 128; j++) {
  1278. __check_store_range(xa, i, j);
  1279. __check_store_range(xa, 128 + i, 128 + j);
  1280. __check_store_range(xa, 4095 + i, 4095 + j);
  1281. __check_store_range(xa, 4096 + i, 4096 + j);
  1282. __check_store_range(xa, 123456 + i, 123456 + j);
  1283. __check_store_range(xa, (1 << 24) + i, (1 << 24) + j);
  1284. }
  1285. }
  1286. }
  1287. #ifdef CONFIG_XARRAY_MULTI
  1288. static void check_split_1(struct xarray *xa, unsigned long index,
  1289. unsigned int order)
  1290. {
  1291. XA_STATE(xas, xa, index);
  1292. void *entry;
  1293. unsigned int i = 0;
  1294. xa_store_order(xa, index, order, xa, GFP_KERNEL);
  1295. xas_split_alloc(&xas, xa, order, GFP_KERNEL);
  1296. xas_lock(&xas);
  1297. xas_split(&xas, xa, order);
  1298. xas_unlock(&xas);
  1299. xa_for_each(xa, index, entry) {
  1300. XA_BUG_ON(xa, entry != xa);
  1301. i++;
  1302. }
  1303. XA_BUG_ON(xa, i != 1 << order);
  1304. xa_set_mark(xa, index, XA_MARK_0);
  1305. XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
  1306. xa_destroy(xa);
  1307. }
  1308. static noinline void check_split(struct xarray *xa)
  1309. {
  1310. unsigned int order;
  1311. XA_BUG_ON(xa, !xa_empty(xa));
  1312. for (order = 1; order < 2 * XA_CHUNK_SHIFT; order++) {
  1313. check_split_1(xa, 0, order);
  1314. check_split_1(xa, 1UL << order, order);
  1315. check_split_1(xa, 3UL << order, order);
  1316. }
  1317. }
  1318. #else
  1319. static void check_split(struct xarray *xa) { }
  1320. #endif
  1321. static void check_align_1(struct xarray *xa, char *name)
  1322. {
  1323. int i;
  1324. unsigned int id;
  1325. unsigned long index;
  1326. void *entry;
  1327. for (i = 0; i < 8; i++) {
  1328. XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b,
  1329. GFP_KERNEL) != 0);
  1330. XA_BUG_ON(xa, id != i);
  1331. }
  1332. xa_for_each(xa, index, entry)
  1333. XA_BUG_ON(xa, xa_is_err(entry));
  1334. xa_destroy(xa);
  1335. }
  1336. /*
  1337. * We should always be able to store without allocating memory after
  1338. * reserving a slot.
  1339. */
  1340. static void check_align_2(struct xarray *xa, char *name)
  1341. {
  1342. int i;
  1343. XA_BUG_ON(xa, !xa_empty(xa));
  1344. for (i = 0; i < 8; i++) {
  1345. XA_BUG_ON(xa, xa_store(xa, 0, name + i, GFP_KERNEL) != NULL);
  1346. xa_erase(xa, 0);
  1347. }
  1348. for (i = 0; i < 8; i++) {
  1349. XA_BUG_ON(xa, xa_reserve(xa, 0, GFP_KERNEL) != 0);
  1350. XA_BUG_ON(xa, xa_store(xa, 0, name + i, 0) != NULL);
  1351. xa_erase(xa, 0);
  1352. }
  1353. XA_BUG_ON(xa, !xa_empty(xa));
  1354. }
  1355. static noinline void check_align(struct xarray *xa)
  1356. {
  1357. char name[] = "Motorola 68000";
  1358. check_align_1(xa, name);
  1359. check_align_1(xa, name + 1);
  1360. check_align_1(xa, name + 2);
  1361. check_align_1(xa, name + 3);
  1362. check_align_2(xa, name);
  1363. }
  1364. static LIST_HEAD(shadow_nodes);
  1365. static void test_update_node(struct xa_node *node)
  1366. {
  1367. if (node->count && node->count == node->nr_values) {
  1368. if (list_empty(&node->private_list))
  1369. list_add(&shadow_nodes, &node->private_list);
  1370. } else {
  1371. if (!list_empty(&node->private_list))
  1372. list_del_init(&node->private_list);
  1373. }
  1374. }
  1375. static noinline void shadow_remove(struct xarray *xa)
  1376. {
  1377. struct xa_node *node;
  1378. xa_lock(xa);
  1379. while ((node = list_first_entry_or_null(&shadow_nodes,
  1380. struct xa_node, private_list))) {
  1381. XA_STATE(xas, node->array, 0);
  1382. XA_BUG_ON(xa, node->array != xa);
  1383. list_del_init(&node->private_list);
  1384. xas.xa_node = xa_parent_locked(node->array, node);
  1385. xas.xa_offset = node->offset;
  1386. xas.xa_shift = node->shift + XA_CHUNK_SHIFT;
  1387. xas_set_update(&xas, test_update_node);
  1388. xas_store(&xas, NULL);
  1389. }
  1390. xa_unlock(xa);
  1391. }
  1392. static noinline void check_workingset(struct xarray *xa, unsigned long index)
  1393. {
  1394. XA_STATE(xas, xa, index);
  1395. xas_set_update(&xas, test_update_node);
  1396. do {
  1397. xas_lock(&xas);
  1398. xas_store(&xas, xa_mk_value(0));
  1399. xas_next(&xas);
  1400. xas_store(&xas, xa_mk_value(1));
  1401. xas_unlock(&xas);
  1402. } while (xas_nomem(&xas, GFP_KERNEL));
  1403. XA_BUG_ON(xa, list_empty(&shadow_nodes));
  1404. xas_lock(&xas);
  1405. xas_next(&xas);
  1406. xas_store(&xas, &xas);
  1407. XA_BUG_ON(xa, !list_empty(&shadow_nodes));
  1408. xas_store(&xas, xa_mk_value(2));
  1409. xas_unlock(&xas);
  1410. XA_BUG_ON(xa, list_empty(&shadow_nodes));
  1411. shadow_remove(xa);
  1412. XA_BUG_ON(xa, !list_empty(&shadow_nodes));
  1413. XA_BUG_ON(xa, !xa_empty(xa));
  1414. }
  1415. /*
  1416. * Check that the pointer / value / sibling entries are accounted the
  1417. * way we expect them to be.
  1418. */
  1419. static noinline void check_account(struct xarray *xa)
  1420. {
  1421. #ifdef CONFIG_XARRAY_MULTI
  1422. unsigned int order;
  1423. for (order = 1; order < 12; order++) {
  1424. XA_STATE(xas, xa, 1 << order);
  1425. xa_store_order(xa, 0, order, xa, GFP_KERNEL);
  1426. rcu_read_lock();
  1427. xas_load(&xas);
  1428. XA_BUG_ON(xa, xas.xa_node->count == 0);
  1429. XA_BUG_ON(xa, xas.xa_node->count > (1 << order));
  1430. XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
  1431. rcu_read_unlock();
  1432. xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order),
  1433. GFP_KERNEL);
  1434. XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2);
  1435. xa_erase(xa, 1 << order);
  1436. XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
  1437. xa_erase(xa, 0);
  1438. XA_BUG_ON(xa, !xa_empty(xa));
  1439. }
  1440. #endif
  1441. }
  1442. static noinline void check_get_order(struct xarray *xa)
  1443. {
  1444. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
  1445. unsigned int order;
  1446. unsigned long i, j;
  1447. for (i = 0; i < 3; i++)
  1448. XA_BUG_ON(xa, xa_get_order(xa, i) != 0);
  1449. for (order = 0; order < max_order; order++) {
  1450. for (i = 0; i < 10; i++) {
  1451. xa_store_order(xa, i << order, order,
  1452. xa_mk_index(i << order), GFP_KERNEL);
  1453. for (j = i << order; j < (i + 1) << order; j++)
  1454. XA_BUG_ON(xa, xa_get_order(xa, j) != order);
  1455. xa_erase(xa, i << order);
  1456. }
  1457. }
  1458. }
  1459. static noinline void check_destroy(struct xarray *xa)
  1460. {
  1461. unsigned long index;
  1462. XA_BUG_ON(xa, !xa_empty(xa));
  1463. /* Destroying an empty array is a no-op */
  1464. xa_destroy(xa);
  1465. XA_BUG_ON(xa, !xa_empty(xa));
  1466. /* Destroying an array with a single entry */
  1467. for (index = 0; index < 1000; index++) {
  1468. xa_store_index(xa, index, GFP_KERNEL);
  1469. XA_BUG_ON(xa, xa_empty(xa));
  1470. xa_destroy(xa);
  1471. XA_BUG_ON(xa, !xa_empty(xa));
  1472. }
  1473. /* Destroying an array with a single entry at ULONG_MAX */
  1474. xa_store(xa, ULONG_MAX, xa, GFP_KERNEL);
  1475. XA_BUG_ON(xa, xa_empty(xa));
  1476. xa_destroy(xa);
  1477. XA_BUG_ON(xa, !xa_empty(xa));
  1478. #ifdef CONFIG_XARRAY_MULTI
  1479. /* Destroying an array with a multi-index entry */
  1480. xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL);
  1481. XA_BUG_ON(xa, xa_empty(xa));
  1482. xa_destroy(xa);
  1483. XA_BUG_ON(xa, !xa_empty(xa));
  1484. #endif
  1485. }
  1486. static DEFINE_XARRAY(array);
  1487. static int xarray_checks(void)
  1488. {
  1489. check_xa_err(&array);
  1490. check_xas_retry(&array);
  1491. check_xa_load(&array);
  1492. check_xa_mark(&array);
  1493. check_xa_shrink(&array);
  1494. check_xas_erase(&array);
  1495. check_insert(&array);
  1496. check_cmpxchg(&array);
  1497. check_reserve(&array);
  1498. check_reserve(&xa0);
  1499. check_multi_store(&array);
  1500. check_get_order(&array);
  1501. check_xa_alloc();
  1502. check_find(&array);
  1503. check_find_entry(&array);
  1504. check_pause(&array);
  1505. check_account(&array);
  1506. check_destroy(&array);
  1507. check_move(&array);
  1508. check_create_range(&array);
  1509. check_store_range(&array);
  1510. check_store_iter(&array);
  1511. check_align(&xa0);
  1512. check_split(&array);
  1513. check_workingset(&array, 0);
  1514. check_workingset(&array, 64);
  1515. check_workingset(&array, 4096);
  1516. printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
  1517. return (tests_run == tests_passed) ? 0 : -EINVAL;
  1518. }
  1519. static void xarray_exit(void)
  1520. {
  1521. }
  1522. module_init(xarray_checks);
  1523. module_exit(xarray_exit);
  1524. MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
  1525. MODULE_LICENSE("GPL");