idr.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * include/linux/idr.h
  4. *
  5. * 2002-10-18 written by Jim Houston [email protected]
  6. * Copyright (C) 2002 by Concurrent Computer Corporation
  7. *
  8. * Small id to pointer translation service avoiding fixed sized
  9. * tables.
  10. */
  11. #ifndef __IDR_H__
  12. #define __IDR_H__
  13. #include <linux/radix-tree.h>
  14. #include <linux/gfp.h>
  15. #include <linux/percpu.h>
  16. struct idr {
  17. struct radix_tree_root idr_rt;
  18. unsigned int idr_base;
  19. unsigned int idr_next;
  20. };
  21. /*
  22. * The IDR API does not expose the tagging functionality of the radix tree
  23. * to users. Use tag 0 to track whether a node has free space below it.
  24. */
  25. #define IDR_FREE 0
  26. /* Set the IDR flag and the IDR_FREE tag */
  27. #define IDR_RT_MARKER (ROOT_IS_IDR | (__force gfp_t) \
  28. (1 << (ROOT_TAG_SHIFT + IDR_FREE)))
  29. #define IDR_INIT_BASE(name, base) { \
  30. .idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER), \
  31. .idr_base = (base), \
  32. .idr_next = 0, \
  33. }
  34. /**
  35. * IDR_INIT() - Initialise an IDR.
  36. * @name: Name of IDR.
  37. *
  38. * A freshly-initialised IDR contains no IDs.
  39. */
  40. #define IDR_INIT(name) IDR_INIT_BASE(name, 0)
  41. /**
  42. * DEFINE_IDR() - Define a statically-allocated IDR.
  43. * @name: Name of IDR.
  44. *
  45. * An IDR defined using this macro is ready for use with no additional
  46. * initialisation required. It contains no IDs.
  47. */
  48. #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
  49. /**
  50. * idr_get_cursor - Return the current position of the cyclic allocator
  51. * @idr: idr handle
  52. *
  53. * The value returned is the value that will be next returned from
  54. * idr_alloc_cyclic() if it is free (otherwise the search will start from
  55. * this position).
  56. */
  57. static inline unsigned int idr_get_cursor(const struct idr *idr)
  58. {
  59. return READ_ONCE(idr->idr_next);
  60. }
  61. /**
  62. * idr_set_cursor - Set the current position of the cyclic allocator
  63. * @idr: idr handle
  64. * @val: new position
  65. *
  66. * The next call to idr_alloc_cyclic() will return @val if it is free
  67. * (otherwise the search will start from this position).
  68. */
  69. static inline void idr_set_cursor(struct idr *idr, unsigned int val)
  70. {
  71. WRITE_ONCE(idr->idr_next, val);
  72. }
  73. /**
  74. * DOC: idr sync
  75. * idr synchronization (stolen from radix-tree.h)
  76. *
  77. * idr_find() is able to be called locklessly, using RCU. The caller must
  78. * ensure calls to this function are made within rcu_read_lock() regions.
  79. * Other readers (lock-free or otherwise) and modifications may be running
  80. * concurrently.
  81. *
  82. * It is still required that the caller manage the synchronization and
  83. * lifetimes of the items. So if RCU lock-free lookups are used, typically
  84. * this would mean that the items have their own locks, or are amenable to
  85. * lock-free access; and that the items are freed by RCU (or only freed after
  86. * having been deleted from the idr tree *and* a synchronize_rcu() grace
  87. * period).
  88. */
  89. #define idr_lock(idr) xa_lock(&(idr)->idr_rt)
  90. #define idr_unlock(idr) xa_unlock(&(idr)->idr_rt)
  91. #define idr_lock_bh(idr) xa_lock_bh(&(idr)->idr_rt)
  92. #define idr_unlock_bh(idr) xa_unlock_bh(&(idr)->idr_rt)
  93. #define idr_lock_irq(idr) xa_lock_irq(&(idr)->idr_rt)
  94. #define idr_unlock_irq(idr) xa_unlock_irq(&(idr)->idr_rt)
  95. #define idr_lock_irqsave(idr, flags) \
  96. xa_lock_irqsave(&(idr)->idr_rt, flags)
  97. #define idr_unlock_irqrestore(idr, flags) \
  98. xa_unlock_irqrestore(&(idr)->idr_rt, flags)
  99. void idr_preload(gfp_t gfp_mask);
  100. int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t);
  101. int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id,
  102. unsigned long max, gfp_t);
  103. int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t);
  104. void *idr_remove(struct idr *, unsigned long id);
  105. void *idr_find(const struct idr *, unsigned long id);
  106. int idr_for_each(const struct idr *,
  107. int (*fn)(int id, void *p, void *data), void *data);
  108. void *idr_get_next(struct idr *, int *nextid);
  109. void *idr_get_next_ul(struct idr *, unsigned long *nextid);
  110. void *idr_replace(struct idr *, void *, unsigned long id);
  111. void idr_destroy(struct idr *);
  112. /**
  113. * idr_init_base() - Initialise an IDR.
  114. * @idr: IDR handle.
  115. * @base: The base value for the IDR.
  116. *
  117. * This variation of idr_init() creates an IDR which will allocate IDs
  118. * starting at %base.
  119. */
  120. static inline void idr_init_base(struct idr *idr, int base)
  121. {
  122. INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
  123. idr->idr_base = base;
  124. idr->idr_next = 0;
  125. }
  126. /**
  127. * idr_init() - Initialise an IDR.
  128. * @idr: IDR handle.
  129. *
  130. * Initialise a dynamically allocated IDR. To initialise a
  131. * statically allocated IDR, use DEFINE_IDR().
  132. */
  133. static inline void idr_init(struct idr *idr)
  134. {
  135. idr_init_base(idr, 0);
  136. }
  137. /**
  138. * idr_is_empty() - Are there any IDs allocated?
  139. * @idr: IDR handle.
  140. *
  141. * Return: %true if any IDs have been allocated from this IDR.
  142. */
  143. static inline bool idr_is_empty(const struct idr *idr)
  144. {
  145. return radix_tree_empty(&idr->idr_rt) &&
  146. radix_tree_tagged(&idr->idr_rt, IDR_FREE);
  147. }
  148. /**
  149. * idr_preload_end - end preload section started with idr_preload()
  150. *
  151. * Each idr_preload() should be matched with an invocation of this
  152. * function. See idr_preload() for details.
  153. */
  154. static inline void idr_preload_end(void)
  155. {
  156. local_unlock(&radix_tree_preloads.lock);
  157. }
  158. /**
  159. * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
  160. * @idr: IDR handle.
  161. * @entry: The type * to use as cursor
  162. * @id: Entry ID.
  163. *
  164. * @entry and @id do not need to be initialized before the loop, and
  165. * after normal termination @entry is left with the value NULL. This
  166. * is convenient for a "not found" value.
  167. */
  168. #define idr_for_each_entry(idr, entry, id) \
  169. for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; id += 1U)
  170. /**
  171. * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
  172. * @idr: IDR handle.
  173. * @entry: The type * to use as cursor.
  174. * @tmp: A temporary placeholder for ID.
  175. * @id: Entry ID.
  176. *
  177. * @entry and @id do not need to be initialized before the loop, and
  178. * after normal termination @entry is left with the value NULL. This
  179. * is convenient for a "not found" value.
  180. */
  181. #define idr_for_each_entry_ul(idr, entry, tmp, id) \
  182. for (tmp = 0, id = 0; \
  183. ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
  184. tmp = id, ++id)
  185. /**
  186. * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
  187. * @idr: IDR handle.
  188. * @entry: The type * to use as a cursor.
  189. * @id: Entry ID.
  190. *
  191. * Continue to iterate over entries, continuing after the current position.
  192. */
  193. #define idr_for_each_entry_continue(idr, entry, id) \
  194. for ((entry) = idr_get_next((idr), &(id)); \
  195. entry; \
  196. ++id, (entry) = idr_get_next((idr), &(id)))
  197. /**
  198. * idr_for_each_entry_continue_ul() - Continue iteration over an IDR's elements of a given type
  199. * @idr: IDR handle.
  200. * @entry: The type * to use as a cursor.
  201. * @tmp: A temporary placeholder for ID.
  202. * @id: Entry ID.
  203. *
  204. * Continue to iterate over entries, continuing after the current position.
  205. * After normal termination @entry is left with the value NULL. This
  206. * is convenient for a "not found" value.
  207. */
  208. #define idr_for_each_entry_continue_ul(idr, entry, tmp, id) \
  209. for (tmp = id; \
  210. ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
  211. tmp = id, ++id)
  212. /*
  213. * IDA - ID Allocator, use when translation from id to pointer isn't necessary.
  214. */
  215. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  216. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
  217. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  218. struct ida_bitmap {
  219. unsigned long bitmap[IDA_BITMAP_LONGS];
  220. };
  221. struct ida {
  222. struct xarray xa;
  223. };
  224. #define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC)
  225. #define IDA_INIT(name) { \
  226. .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \
  227. }
  228. #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
  229. int ida_alloc_range(struct ida *, unsigned int min, unsigned int max, gfp_t);
  230. void ida_free(struct ida *, unsigned int id);
  231. void ida_destroy(struct ida *ida);
  232. /**
  233. * ida_alloc() - Allocate an unused ID.
  234. * @ida: IDA handle.
  235. * @gfp: Memory allocation flags.
  236. *
  237. * Allocate an ID between 0 and %INT_MAX, inclusive.
  238. *
  239. * Context: Any context. It is safe to call this function without
  240. * locking in your code.
  241. * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
  242. * or %-ENOSPC if there are no free IDs.
  243. */
  244. static inline int ida_alloc(struct ida *ida, gfp_t gfp)
  245. {
  246. return ida_alloc_range(ida, 0, ~0, gfp);
  247. }
  248. /**
  249. * ida_alloc_min() - Allocate an unused ID.
  250. * @ida: IDA handle.
  251. * @min: Lowest ID to allocate.
  252. * @gfp: Memory allocation flags.
  253. *
  254. * Allocate an ID between @min and %INT_MAX, inclusive.
  255. *
  256. * Context: Any context. It is safe to call this function without
  257. * locking in your code.
  258. * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
  259. * or %-ENOSPC if there are no free IDs.
  260. */
  261. static inline int ida_alloc_min(struct ida *ida, unsigned int min, gfp_t gfp)
  262. {
  263. return ida_alloc_range(ida, min, ~0, gfp);
  264. }
  265. /**
  266. * ida_alloc_max() - Allocate an unused ID.
  267. * @ida: IDA handle.
  268. * @max: Highest ID to allocate.
  269. * @gfp: Memory allocation flags.
  270. *
  271. * Allocate an ID between 0 and @max, inclusive.
  272. *
  273. * Context: Any context. It is safe to call this function without
  274. * locking in your code.
  275. * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
  276. * or %-ENOSPC if there are no free IDs.
  277. */
  278. static inline int ida_alloc_max(struct ida *ida, unsigned int max, gfp_t gfp)
  279. {
  280. return ida_alloc_range(ida, 0, max, gfp);
  281. }
  282. static inline void ida_init(struct ida *ida)
  283. {
  284. xa_init_flags(&ida->xa, IDA_INIT_FLAGS);
  285. }
  286. /*
  287. * ida_simple_get() and ida_simple_remove() are deprecated. Use
  288. * ida_alloc() and ida_free() instead respectively.
  289. */
  290. #define ida_simple_get(ida, start, end, gfp) \
  291. ida_alloc_range(ida, start, (end) - 1, gfp)
  292. #define ida_simple_remove(ida, id) ida_free(ida, id)
  293. static inline bool ida_is_empty(const struct ida *ida)
  294. {
  295. return xa_empty(&ida->xa);
  296. }
  297. #endif /* __IDR_H__ */