idr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/bitmap.h>
  3. #include <linux/bug.h>
  4. #include <linux/export.h>
  5. #include <linux/idr.h>
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/xarray.h>
  9. /**
  10. * idr_alloc_u32() - Allocate an ID.
  11. * @idr: IDR handle.
  12. * @ptr: Pointer to be associated with the new ID.
  13. * @nextid: Pointer to an ID.
  14. * @max: The maximum ID to allocate (inclusive).
  15. * @gfp: Memory allocation flags.
  16. *
  17. * Allocates an unused ID in the range specified by @nextid and @max.
  18. * Note that @max is inclusive whereas the @end parameter to idr_alloc()
  19. * is exclusive. The new ID is assigned to @nextid before the pointer
  20. * is inserted into the IDR, so if @nextid points into the object pointed
  21. * to by @ptr, a concurrent lookup will not find an uninitialised ID.
  22. *
  23. * The caller should provide their own locking to ensure that two
  24. * concurrent modifications to the IDR are not possible. Read-only
  25. * accesses to the IDR may be done under the RCU read lock or may
  26. * exclude simultaneous writers.
  27. *
  28. * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
  29. * or -ENOSPC if no free IDs could be found. If an error occurred,
  30. * @nextid is unchanged.
  31. */
  32. int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
  33. unsigned long max, gfp_t gfp)
  34. {
  35. struct radix_tree_iter iter;
  36. void __rcu **slot;
  37. unsigned int base = idr->idr_base;
  38. unsigned int id = *nextid;
  39. if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR)))
  40. idr->idr_rt.xa_flags |= IDR_RT_MARKER;
  41. if (max < base)
  42. return -ENOSPC;
  43. id = (id < base) ? 0 : id - base;
  44. radix_tree_iter_init(&iter, id);
  45. slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);
  46. if (IS_ERR(slot))
  47. return PTR_ERR(slot);
  48. *nextid = iter.index + base;
  49. /* there is a memory barrier inside radix_tree_iter_replace() */
  50. radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
  51. radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(idr_alloc_u32);
  55. /**
  56. * idr_alloc() - Allocate an ID.
  57. * @idr: IDR handle.
  58. * @ptr: Pointer to be associated with the new ID.
  59. * @start: The minimum ID (inclusive).
  60. * @end: The maximum ID (exclusive).
  61. * @gfp: Memory allocation flags.
  62. *
  63. * Allocates an unused ID in the range specified by @start and @end. If
  64. * @end is <= 0, it is treated as one larger than %INT_MAX. This allows
  65. * callers to use @start + N as @end as long as N is within integer range.
  66. *
  67. * The caller should provide their own locking to ensure that two
  68. * concurrent modifications to the IDR are not possible. Read-only
  69. * accesses to the IDR may be done under the RCU read lock or may
  70. * exclude simultaneous writers.
  71. *
  72. * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
  73. * or -ENOSPC if no free IDs could be found.
  74. */
  75. int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
  76. {
  77. u32 id = start;
  78. int ret;
  79. if (WARN_ON_ONCE(start < 0))
  80. return -EINVAL;
  81. ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
  82. if (ret)
  83. return ret;
  84. return id;
  85. }
  86. EXPORT_SYMBOL_GPL(idr_alloc);
  87. /**
  88. * idr_alloc_cyclic() - Allocate an ID cyclically.
  89. * @idr: IDR handle.
  90. * @ptr: Pointer to be associated with the new ID.
  91. * @start: The minimum ID (inclusive).
  92. * @end: The maximum ID (exclusive).
  93. * @gfp: Memory allocation flags.
  94. *
  95. * Allocates an unused ID in the range specified by @start and @end. If
  96. * @end is <= 0, it is treated as one larger than %INT_MAX. This allows
  97. * callers to use @start + N as @end as long as N is within integer range.
  98. * The search for an unused ID will start at the last ID allocated and will
  99. * wrap around to @start if no free IDs are found before reaching @end.
  100. *
  101. * The caller should provide their own locking to ensure that two
  102. * concurrent modifications to the IDR are not possible. Read-only
  103. * accesses to the IDR may be done under the RCU read lock or may
  104. * exclude simultaneous writers.
  105. *
  106. * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
  107. * or -ENOSPC if no free IDs could be found.
  108. */
  109. int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
  110. {
  111. u32 id = idr->idr_next;
  112. int err, max = end > 0 ? end - 1 : INT_MAX;
  113. if ((int)id < start)
  114. id = start;
  115. err = idr_alloc_u32(idr, ptr, &id, max, gfp);
  116. if ((err == -ENOSPC) && (id > start)) {
  117. id = start;
  118. err = idr_alloc_u32(idr, ptr, &id, max, gfp);
  119. }
  120. if (err)
  121. return err;
  122. idr->idr_next = id + 1;
  123. return id;
  124. }
  125. EXPORT_SYMBOL(idr_alloc_cyclic);
  126. /**
  127. * idr_remove() - Remove an ID from the IDR.
  128. * @idr: IDR handle.
  129. * @id: Pointer ID.
  130. *
  131. * Removes this ID from the IDR. If the ID was not previously in the IDR,
  132. * this function returns %NULL.
  133. *
  134. * Since this function modifies the IDR, the caller should provide their
  135. * own locking to ensure that concurrent modification of the same IDR is
  136. * not possible.
  137. *
  138. * Return: The pointer formerly associated with this ID.
  139. */
  140. void *idr_remove(struct idr *idr, unsigned long id)
  141. {
  142. return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL);
  143. }
  144. EXPORT_SYMBOL_GPL(idr_remove);
  145. /**
  146. * idr_find() - Return pointer for given ID.
  147. * @idr: IDR handle.
  148. * @id: Pointer ID.
  149. *
  150. * Looks up the pointer associated with this ID. A %NULL pointer may
  151. * indicate that @id is not allocated or that the %NULL pointer was
  152. * associated with this ID.
  153. *
  154. * This function can be called under rcu_read_lock(), given that the leaf
  155. * pointers lifetimes are correctly managed.
  156. *
  157. * Return: The pointer associated with this ID.
  158. */
  159. void *idr_find(const struct idr *idr, unsigned long id)
  160. {
  161. return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base);
  162. }
  163. EXPORT_SYMBOL_GPL(idr_find);
  164. /**
  165. * idr_for_each() - Iterate through all stored pointers.
  166. * @idr: IDR handle.
  167. * @fn: Function to be called for each pointer.
  168. * @data: Data passed to callback function.
  169. *
  170. * The callback function will be called for each entry in @idr, passing
  171. * the ID, the entry and @data.
  172. *
  173. * If @fn returns anything other than %0, the iteration stops and that
  174. * value is returned from this function.
  175. *
  176. * idr_for_each() can be called concurrently with idr_alloc() and
  177. * idr_remove() if protected by RCU. Newly added entries may not be
  178. * seen and deleted entries may be seen, but adding and removing entries
  179. * will not cause other entries to be skipped, nor spurious ones to be seen.
  180. */
  181. int idr_for_each(const struct idr *idr,
  182. int (*fn)(int id, void *p, void *data), void *data)
  183. {
  184. struct radix_tree_iter iter;
  185. void __rcu **slot;
  186. int base = idr->idr_base;
  187. radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
  188. int ret;
  189. unsigned long id = iter.index + base;
  190. if (WARN_ON_ONCE(id > INT_MAX))
  191. break;
  192. ret = fn(id, rcu_dereference_raw(*slot), data);
  193. if (ret)
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(idr_for_each);
  199. /**
  200. * idr_get_next_ul() - Find next populated entry.
  201. * @idr: IDR handle.
  202. * @nextid: Pointer to an ID.
  203. *
  204. * Returns the next populated entry in the tree with an ID greater than
  205. * or equal to the value pointed to by @nextid. On exit, @nextid is updated
  206. * to the ID of the found value. To use in a loop, the value pointed to by
  207. * nextid must be incremented by the user.
  208. */
  209. void *idr_get_next_ul(struct idr *idr, unsigned long *nextid)
  210. {
  211. struct radix_tree_iter iter;
  212. void __rcu **slot;
  213. void *entry = NULL;
  214. unsigned long base = idr->idr_base;
  215. unsigned long id = *nextid;
  216. id = (id < base) ? 0 : id - base;
  217. radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, id) {
  218. entry = rcu_dereference_raw(*slot);
  219. if (!entry)
  220. continue;
  221. if (!xa_is_internal(entry))
  222. break;
  223. if (slot != &idr->idr_rt.xa_head && !xa_is_retry(entry))
  224. break;
  225. slot = radix_tree_iter_retry(&iter);
  226. }
  227. if (!slot)
  228. return NULL;
  229. *nextid = iter.index + base;
  230. return entry;
  231. }
  232. EXPORT_SYMBOL(idr_get_next_ul);
  233. /**
  234. * idr_get_next() - Find next populated entry.
  235. * @idr: IDR handle.
  236. * @nextid: Pointer to an ID.
  237. *
  238. * Returns the next populated entry in the tree with an ID greater than
  239. * or equal to the value pointed to by @nextid. On exit, @nextid is updated
  240. * to the ID of the found value. To use in a loop, the value pointed to by
  241. * nextid must be incremented by the user.
  242. */
  243. void *idr_get_next(struct idr *idr, int *nextid)
  244. {
  245. unsigned long id = *nextid;
  246. void *entry = idr_get_next_ul(idr, &id);
  247. if (WARN_ON_ONCE(id > INT_MAX))
  248. return NULL;
  249. *nextid = id;
  250. return entry;
  251. }
  252. EXPORT_SYMBOL(idr_get_next);
  253. /**
  254. * idr_replace() - replace pointer for given ID.
  255. * @idr: IDR handle.
  256. * @ptr: New pointer to associate with the ID.
  257. * @id: ID to change.
  258. *
  259. * Replace the pointer registered with an ID and return the old value.
  260. * This function can be called under the RCU read lock concurrently with
  261. * idr_alloc() and idr_remove() (as long as the ID being removed is not
  262. * the one being replaced!).
  263. *
  264. * Returns: the old value on success. %-ENOENT indicates that @id was not
  265. * found. %-EINVAL indicates that @ptr was not valid.
  266. */
  267. void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
  268. {
  269. struct radix_tree_node *node;
  270. void __rcu **slot = NULL;
  271. void *entry;
  272. id -= idr->idr_base;
  273. entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
  274. if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
  275. return ERR_PTR(-ENOENT);
  276. __radix_tree_replace(&idr->idr_rt, node, slot, ptr);
  277. return entry;
  278. }
  279. EXPORT_SYMBOL(idr_replace);
  280. /**
  281. * DOC: IDA description
  282. *
  283. * The IDA is an ID allocator which does not provide the ability to
  284. * associate an ID with a pointer. As such, it only needs to store one
  285. * bit per ID, and so is more space efficient than an IDR. To use an IDA,
  286. * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
  287. * then initialise it using ida_init()). To allocate a new ID, call
  288. * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range().
  289. * To free an ID, call ida_free().
  290. *
  291. * ida_destroy() can be used to dispose of an IDA without needing to
  292. * free the individual IDs in it. You can use ida_is_empty() to find
  293. * out whether the IDA has any IDs currently allocated.
  294. *
  295. * The IDA handles its own locking. It is safe to call any of the IDA
  296. * functions without synchronisation in your code.
  297. *
  298. * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
  299. * limitation, it should be quite straightforward to raise the maximum.
  300. */
  301. /*
  302. * Developer's notes:
  303. *
  304. * The IDA uses the functionality provided by the XArray to store bitmaps in
  305. * each entry. The XA_FREE_MARK is only cleared when all bits in the bitmap
  306. * have been set.
  307. *
  308. * I considered telling the XArray that each slot is an order-10 node
  309. * and indexing by bit number, but the XArray can't allow a single multi-index
  310. * entry in the head, which would significantly increase memory consumption
  311. * for the IDA. So instead we divide the index by the number of bits in the
  312. * leaf bitmap before doing a radix tree lookup.
  313. *
  314. * As an optimisation, if there are only a few low bits set in any given
  315. * leaf, instead of allocating a 128-byte bitmap, we store the bits
  316. * as a value entry. Value entries never have the XA_FREE_MARK cleared
  317. * because we can always convert them into a bitmap entry.
  318. *
  319. * It would be possible to optimise further; once we've run out of a
  320. * single 128-byte bitmap, we currently switch to a 576-byte node, put
  321. * the 128-byte bitmap in the first entry and then start allocating extra
  322. * 128-byte entries. We could instead use the 512 bytes of the node's
  323. * data as a bitmap before moving to that scheme. I do not believe this
  324. * is a worthwhile optimisation; Rasmus Villemoes surveyed the current
  325. * users of the IDA and almost none of them use more than 1024 entries.
  326. * Those that do use more than the 8192 IDs that the 512 bytes would
  327. * provide.
  328. *
  329. * The IDA always uses a lock to alloc/free. If we add a 'test_bit'
  330. * equivalent, it will still need locking. Going to RCU lookup would require
  331. * using RCU to free bitmaps, and that's not trivial without embedding an
  332. * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte
  333. * bitmap, which is excessive.
  334. */
  335. /**
  336. * ida_alloc_range() - Allocate an unused ID.
  337. * @ida: IDA handle.
  338. * @min: Lowest ID to allocate.
  339. * @max: Highest ID to allocate.
  340. * @gfp: Memory allocation flags.
  341. *
  342. * Allocate an ID between @min and @max, inclusive. The allocated ID will
  343. * not exceed %INT_MAX, even if @max is larger.
  344. *
  345. * Context: Any context. It is safe to call this function without
  346. * locking in your code.
  347. * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
  348. * or %-ENOSPC if there are no free IDs.
  349. */
  350. int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,
  351. gfp_t gfp)
  352. {
  353. XA_STATE(xas, &ida->xa, min / IDA_BITMAP_BITS);
  354. unsigned bit = min % IDA_BITMAP_BITS;
  355. unsigned long flags;
  356. struct ida_bitmap *bitmap, *alloc = NULL;
  357. if ((int)min < 0)
  358. return -ENOSPC;
  359. if ((int)max < 0)
  360. max = INT_MAX;
  361. retry:
  362. xas_lock_irqsave(&xas, flags);
  363. next:
  364. bitmap = xas_find_marked(&xas, max / IDA_BITMAP_BITS, XA_FREE_MARK);
  365. if (xas.xa_index > min / IDA_BITMAP_BITS)
  366. bit = 0;
  367. if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
  368. goto nospc;
  369. if (xa_is_value(bitmap)) {
  370. unsigned long tmp = xa_to_value(bitmap);
  371. if (bit < BITS_PER_XA_VALUE) {
  372. bit = find_next_zero_bit(&tmp, BITS_PER_XA_VALUE, bit);
  373. if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
  374. goto nospc;
  375. if (bit < BITS_PER_XA_VALUE) {
  376. tmp |= 1UL << bit;
  377. xas_store(&xas, xa_mk_value(tmp));
  378. goto out;
  379. }
  380. }
  381. bitmap = alloc;
  382. if (!bitmap)
  383. bitmap = kzalloc_obj(*bitmap, GFP_NOWAIT);
  384. if (!bitmap)
  385. goto alloc;
  386. bitmap->bitmap[0] = tmp;
  387. xas_store(&xas, bitmap);
  388. if (xas_error(&xas)) {
  389. bitmap->bitmap[0] = 0;
  390. goto out;
  391. }
  392. }
  393. if (bitmap) {
  394. bit = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, bit);
  395. if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
  396. goto nospc;
  397. if (bit == IDA_BITMAP_BITS)
  398. goto next;
  399. __set_bit(bit, bitmap->bitmap);
  400. if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
  401. xas_clear_mark(&xas, XA_FREE_MARK);
  402. } else {
  403. if (bit < BITS_PER_XA_VALUE) {
  404. bitmap = xa_mk_value(1UL << bit);
  405. } else {
  406. bitmap = alloc;
  407. if (!bitmap)
  408. bitmap = kzalloc_obj(*bitmap, GFP_NOWAIT);
  409. if (!bitmap)
  410. goto alloc;
  411. __set_bit(bit, bitmap->bitmap);
  412. }
  413. xas_store(&xas, bitmap);
  414. }
  415. out:
  416. xas_unlock_irqrestore(&xas, flags);
  417. if (xas_nomem(&xas, gfp)) {
  418. xas.xa_index = min / IDA_BITMAP_BITS;
  419. bit = min % IDA_BITMAP_BITS;
  420. goto retry;
  421. }
  422. if (bitmap != alloc)
  423. kfree(alloc);
  424. if (xas_error(&xas))
  425. return xas_error(&xas);
  426. return xas.xa_index * IDA_BITMAP_BITS + bit;
  427. alloc:
  428. xas_unlock_irqrestore(&xas, flags);
  429. alloc = kzalloc_obj(*bitmap, gfp);
  430. if (!alloc)
  431. return -ENOMEM;
  432. xas_set(&xas, min / IDA_BITMAP_BITS);
  433. bit = min % IDA_BITMAP_BITS;
  434. goto retry;
  435. nospc:
  436. xas_unlock_irqrestore(&xas, flags);
  437. kfree(alloc);
  438. return -ENOSPC;
  439. }
  440. EXPORT_SYMBOL(ida_alloc_range);
  441. /**
  442. * ida_find_first_range - Get the lowest used ID.
  443. * @ida: IDA handle.
  444. * @min: Lowest ID to get.
  445. * @max: Highest ID to get.
  446. *
  447. * Get the lowest used ID between @min and @max, inclusive. The returned
  448. * ID will not exceed %INT_MAX, even if @max is larger.
  449. *
  450. * Context: Any context. Takes and releases the xa_lock.
  451. * Return: The lowest used ID, or errno if no used ID is found.
  452. */
  453. int ida_find_first_range(struct ida *ida, unsigned int min, unsigned int max)
  454. {
  455. unsigned long index = min / IDA_BITMAP_BITS;
  456. unsigned int offset = min % IDA_BITMAP_BITS;
  457. unsigned long *addr, size, bit;
  458. unsigned long tmp = 0;
  459. unsigned long flags;
  460. void *entry;
  461. int ret;
  462. if ((int)min < 0)
  463. return -EINVAL;
  464. if ((int)max < 0)
  465. max = INT_MAX;
  466. xa_lock_irqsave(&ida->xa, flags);
  467. entry = xa_find(&ida->xa, &index, max / IDA_BITMAP_BITS, XA_PRESENT);
  468. if (!entry) {
  469. ret = -ENOENT;
  470. goto err_unlock;
  471. }
  472. if (index > min / IDA_BITMAP_BITS)
  473. offset = 0;
  474. if (index * IDA_BITMAP_BITS + offset > max) {
  475. ret = -ENOENT;
  476. goto err_unlock;
  477. }
  478. if (xa_is_value(entry)) {
  479. tmp = xa_to_value(entry);
  480. addr = &tmp;
  481. size = BITS_PER_XA_VALUE;
  482. } else {
  483. addr = ((struct ida_bitmap *)entry)->bitmap;
  484. size = IDA_BITMAP_BITS;
  485. }
  486. bit = find_next_bit(addr, size, offset);
  487. xa_unlock_irqrestore(&ida->xa, flags);
  488. if (bit == size ||
  489. index * IDA_BITMAP_BITS + bit > max)
  490. return -ENOENT;
  491. return index * IDA_BITMAP_BITS + bit;
  492. err_unlock:
  493. xa_unlock_irqrestore(&ida->xa, flags);
  494. return ret;
  495. }
  496. EXPORT_SYMBOL(ida_find_first_range);
  497. /**
  498. * ida_free() - Release an allocated ID.
  499. * @ida: IDA handle.
  500. * @id: Previously allocated ID.
  501. *
  502. * Context: Any context. It is safe to call this function without
  503. * locking in your code.
  504. */
  505. void ida_free(struct ida *ida, unsigned int id)
  506. {
  507. XA_STATE(xas, &ida->xa, id / IDA_BITMAP_BITS);
  508. unsigned bit = id % IDA_BITMAP_BITS;
  509. struct ida_bitmap *bitmap;
  510. unsigned long flags;
  511. if ((int)id < 0)
  512. return;
  513. xas_lock_irqsave(&xas, flags);
  514. bitmap = xas_load(&xas);
  515. if (xa_is_value(bitmap)) {
  516. unsigned long v = xa_to_value(bitmap);
  517. if (bit >= BITS_PER_XA_VALUE)
  518. goto err;
  519. if (!(v & (1UL << bit)))
  520. goto err;
  521. v &= ~(1UL << bit);
  522. if (!v)
  523. goto delete;
  524. xas_store(&xas, xa_mk_value(v));
  525. } else {
  526. if (!bitmap || !test_bit(bit, bitmap->bitmap))
  527. goto err;
  528. __clear_bit(bit, bitmap->bitmap);
  529. xas_set_mark(&xas, XA_FREE_MARK);
  530. if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) {
  531. kfree(bitmap);
  532. delete:
  533. xas_store(&xas, NULL);
  534. }
  535. }
  536. xas_unlock_irqrestore(&xas, flags);
  537. return;
  538. err:
  539. xas_unlock_irqrestore(&xas, flags);
  540. WARN(1, "ida_free called for id=%d which is not allocated.\n", id);
  541. }
  542. EXPORT_SYMBOL(ida_free);
  543. /**
  544. * ida_destroy() - Free all IDs.
  545. * @ida: IDA handle.
  546. *
  547. * Calling this function frees all IDs and releases all resources used
  548. * by an IDA. When this call returns, the IDA is empty and can be reused
  549. * or freed. If the IDA is already empty, there is no need to call this
  550. * function.
  551. *
  552. * Context: Any context. It is safe to call this function without
  553. * locking in your code.
  554. */
  555. void ida_destroy(struct ida *ida)
  556. {
  557. XA_STATE(xas, &ida->xa, 0);
  558. struct ida_bitmap *bitmap;
  559. unsigned long flags;
  560. xas_lock_irqsave(&xas, flags);
  561. xas_for_each(&xas, bitmap, ULONG_MAX) {
  562. if (!xa_is_value(bitmap))
  563. kfree(bitmap);
  564. xas_store(&xas, NULL);
  565. }
  566. xas_unlock_irqrestore(&xas, flags);
  567. }
  568. EXPORT_SYMBOL(ida_destroy);
  569. #ifndef __KERNEL__
  570. extern void xa_dump_index(unsigned long index, unsigned int shift);
  571. #define IDA_CHUNK_SHIFT ilog2(IDA_BITMAP_BITS)
  572. static void ida_dump_entry(void *entry, unsigned long index)
  573. {
  574. unsigned long i;
  575. if (!entry)
  576. return;
  577. if (xa_is_node(entry)) {
  578. struct xa_node *node = xa_to_node(entry);
  579. unsigned int shift = node->shift + IDA_CHUNK_SHIFT +
  580. XA_CHUNK_SHIFT;
  581. xa_dump_index(index * IDA_BITMAP_BITS, shift);
  582. xa_dump_node(node);
  583. for (i = 0; i < XA_CHUNK_SIZE; i++)
  584. ida_dump_entry(node->slots[i],
  585. index | (i << node->shift));
  586. } else if (xa_is_value(entry)) {
  587. xa_dump_index(index * IDA_BITMAP_BITS, ilog2(BITS_PER_LONG));
  588. pr_cont("value: data %lx [%px]\n", xa_to_value(entry), entry);
  589. } else {
  590. struct ida_bitmap *bitmap = entry;
  591. xa_dump_index(index * IDA_BITMAP_BITS, IDA_CHUNK_SHIFT);
  592. pr_cont("bitmap: %p data", bitmap);
  593. for (i = 0; i < IDA_BITMAP_LONGS; i++)
  594. pr_cont(" %lx", bitmap->bitmap[i]);
  595. pr_cont("\n");
  596. }
  597. }
  598. static void ida_dump(struct ida *ida)
  599. {
  600. struct xarray *xa = &ida->xa;
  601. pr_debug("ida: %p node %p free %d\n", ida, xa->xa_head,
  602. xa->xa_flags >> ROOT_TAG_SHIFT);
  603. ida_dump_entry(xa->xa_head, 0);
  604. }
  605. #endif