radix-tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2001 Momchil Velikov
  4. * Portions Copyright (C) 2001 Christoph Hellwig
  5. * Copyright (C) 2006 Nick Piggin
  6. * Copyright (C) 2012 Konstantin Khlebnikov
  7. */
  8. #ifndef _LINUX_RADIX_TREE_H
  9. #define _LINUX_RADIX_TREE_H
  10. #include <linux/bitops.h>
  11. #include <linux/gfp_types.h>
  12. #include <linux/list.h>
  13. #include <linux/lockdep.h>
  14. #include <linux/math.h>
  15. #include <linux/percpu.h>
  16. #include <linux/preempt.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/types.h>
  20. #include <linux/xarray.h>
  21. #include <linux/local_lock.h>
  22. /* Keep unconverted code working */
  23. #define radix_tree_root xarray
  24. #define radix_tree_node xa_node
  25. struct radix_tree_preload {
  26. local_lock_t lock;
  27. unsigned nr;
  28. /* nodes->parent points to next preallocated node */
  29. struct radix_tree_node *nodes;
  30. };
  31. DECLARE_PER_CPU(struct radix_tree_preload, radix_tree_preloads);
  32. /*
  33. * The bottom two bits of the slot determine how the remaining bits in the
  34. * slot are interpreted:
  35. *
  36. * 00 - data pointer
  37. * 10 - internal entry
  38. * x1 - value entry
  39. *
  40. * The internal entry may be a pointer to the next level in the tree, a
  41. * sibling entry, or an indicator that the entry in this slot has been moved
  42. * to another location in the tree and the lookup should be restarted. While
  43. * NULL fits the 'data pointer' pattern, it means that there is no entry in
  44. * the tree for this index (no matter what level of the tree it is found at).
  45. * This means that storing a NULL entry in the tree is the same as deleting
  46. * the entry from the tree.
  47. */
  48. #define RADIX_TREE_ENTRY_MASK 3UL
  49. #define RADIX_TREE_INTERNAL_NODE 2UL
  50. static inline bool radix_tree_is_internal_node(void *ptr)
  51. {
  52. return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
  53. RADIX_TREE_INTERNAL_NODE;
  54. }
  55. /*** radix-tree API starts here ***/
  56. #define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
  57. #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
  58. #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
  59. #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
  60. #define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
  61. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  62. #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
  63. RADIX_TREE_MAP_SHIFT))
  64. /* The IDR tag is stored in the low bits of xa_flags */
  65. #define ROOT_IS_IDR ((__force gfp_t)4)
  66. /* The top bits of xa_flags are used to store the root tags */
  67. #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
  68. #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
  69. #define RADIX_TREE(name, mask) \
  70. struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
  71. #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
  72. static inline bool radix_tree_empty(const struct radix_tree_root *root)
  73. {
  74. return root->xa_head == NULL;
  75. }
  76. /**
  77. * struct radix_tree_iter - radix tree iterator state
  78. *
  79. * @index: index of current slot
  80. * @next_index: one beyond the last index for this chunk
  81. * @tags: bit-mask for tag-iterating
  82. * @node: node that contains current slot
  83. *
  84. * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
  85. * subinterval of slots contained within one radix tree leaf node. It is
  86. * described by a pointer to its first slot and a struct radix_tree_iter
  87. * which holds the chunk's position in the tree and its size. For tagged
  88. * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
  89. * radix tree tag.
  90. */
  91. struct radix_tree_iter {
  92. unsigned long index;
  93. unsigned long next_index;
  94. unsigned long tags;
  95. struct radix_tree_node *node;
  96. };
  97. /**
  98. * Radix-tree synchronization
  99. *
  100. * The radix-tree API requires that users provide all synchronisation (with
  101. * specific exceptions, noted below).
  102. *
  103. * Synchronization of access to the data items being stored in the tree, and
  104. * management of their lifetimes must be completely managed by API users.
  105. *
  106. * For API usage, in general,
  107. * - any function _modifying_ the tree or tags (inserting or deleting
  108. * items, setting or clearing tags) must exclude other modifications, and
  109. * exclude any functions reading the tree.
  110. * - any function _reading_ the tree or tags (looking up items or tags,
  111. * gang lookups) must exclude modifications to the tree, but may occur
  112. * concurrently with other readers.
  113. *
  114. * The notable exceptions to this rule are the following functions:
  115. * __radix_tree_lookup
  116. * radix_tree_lookup
  117. * radix_tree_lookup_slot
  118. * radix_tree_tag_get
  119. * radix_tree_gang_lookup
  120. * radix_tree_gang_lookup_tag
  121. * radix_tree_gang_lookup_tag_slot
  122. * radix_tree_tagged
  123. *
  124. * The first 7 functions are able to be called locklessly, using RCU. The
  125. * caller must ensure calls to these functions are made within rcu_read_lock()
  126. * regions. Other readers (lock-free or otherwise) and modifications may be
  127. * running concurrently.
  128. *
  129. * It is still required that the caller manage the synchronization and lifetimes
  130. * of the items. So if RCU lock-free lookups are used, typically this would mean
  131. * that the items have their own locks, or are amenable to lock-free access; and
  132. * that the items are freed by RCU (or only freed after having been deleted from
  133. * the radix tree *and* a synchronize_rcu() grace period).
  134. *
  135. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  136. * access to data items when inserting into or looking up from the radix tree)
  137. *
  138. * Note that the value returned by radix_tree_tag_get() may not be relied upon
  139. * if only the RCU read lock is held. Functions to set/clear tags and to
  140. * delete nodes running concurrently with it may affect its result such that
  141. * two consecutive reads in the same locked section may return different
  142. * values. If reliability is required, modification functions must also be
  143. * excluded from concurrency.
  144. *
  145. * radix_tree_tagged is able to be called without locking or RCU.
  146. */
  147. /**
  148. * radix_tree_deref_slot - dereference a slot
  149. * @slot: slot pointer, returned by radix_tree_lookup_slot
  150. *
  151. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  152. * locked across slot lookup and dereference. Not required if write lock is
  153. * held (ie. items cannot be concurrently inserted).
  154. *
  155. * radix_tree_deref_retry must be used to confirm validity of the pointer if
  156. * only the read lock is held.
  157. *
  158. * Return: entry stored in that slot.
  159. */
  160. static inline void *radix_tree_deref_slot(void __rcu **slot)
  161. {
  162. return rcu_dereference(*slot);
  163. }
  164. /**
  165. * radix_tree_deref_slot_protected - dereference a slot with tree lock held
  166. * @slot: slot pointer, returned by radix_tree_lookup_slot
  167. *
  168. * Similar to radix_tree_deref_slot. The caller does not hold the RCU read
  169. * lock but it must hold the tree lock to prevent parallel updates.
  170. *
  171. * Return: entry stored in that slot.
  172. */
  173. static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
  174. spinlock_t *treelock)
  175. {
  176. return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
  177. }
  178. /**
  179. * radix_tree_deref_retry - check radix_tree_deref_slot
  180. * @arg: pointer returned by radix_tree_deref_slot
  181. * Returns: 0 if retry is not required, otherwise retry is required
  182. *
  183. * radix_tree_deref_retry must be used with radix_tree_deref_slot.
  184. */
  185. static inline int radix_tree_deref_retry(void *arg)
  186. {
  187. return unlikely(radix_tree_is_internal_node(arg));
  188. }
  189. /**
  190. * radix_tree_exception - radix_tree_deref_slot returned either exception?
  191. * @arg: value returned by radix_tree_deref_slot
  192. * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
  193. */
  194. static inline int radix_tree_exception(void *arg)
  195. {
  196. return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
  197. }
  198. int radix_tree_insert(struct radix_tree_root *, unsigned long index,
  199. void *);
  200. void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
  201. struct radix_tree_node **nodep, void __rcu ***slotp);
  202. void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
  203. void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
  204. unsigned long index);
  205. void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
  206. void __rcu **slot, void *entry);
  207. void radix_tree_iter_replace(struct radix_tree_root *,
  208. const struct radix_tree_iter *, void __rcu **slot, void *entry);
  209. void radix_tree_replace_slot(struct radix_tree_root *,
  210. void __rcu **slot, void *entry);
  211. void radix_tree_iter_delete(struct radix_tree_root *,
  212. struct radix_tree_iter *iter, void __rcu **slot);
  213. void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
  214. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  215. unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
  216. void **results, unsigned long first_index,
  217. unsigned int max_items);
  218. int radix_tree_preload(gfp_t gfp_mask);
  219. int radix_tree_maybe_preload(gfp_t gfp_mask);
  220. void radix_tree_init(void);
  221. void *radix_tree_tag_set(struct radix_tree_root *,
  222. unsigned long index, unsigned int tag);
  223. void *radix_tree_tag_clear(struct radix_tree_root *,
  224. unsigned long index, unsigned int tag);
  225. int radix_tree_tag_get(const struct radix_tree_root *,
  226. unsigned long index, unsigned int tag);
  227. void radix_tree_iter_tag_clear(struct radix_tree_root *,
  228. const struct radix_tree_iter *iter, unsigned int tag);
  229. unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *,
  230. void **results, unsigned long first_index,
  231. unsigned int max_items, unsigned int tag);
  232. unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *,
  233. void __rcu ***results, unsigned long first_index,
  234. unsigned int max_items, unsigned int tag);
  235. int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag);
  236. static inline void radix_tree_preload_end(void)
  237. {
  238. local_unlock(&radix_tree_preloads.lock);
  239. }
  240. void __rcu **idr_get_free(struct radix_tree_root *root,
  241. struct radix_tree_iter *iter, gfp_t gfp,
  242. unsigned long max);
  243. enum {
  244. RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */
  245. RADIX_TREE_ITER_TAGGED = 0x10, /* lookup tagged slots */
  246. RADIX_TREE_ITER_CONTIG = 0x20, /* stop at first hole */
  247. };
  248. /**
  249. * radix_tree_iter_init - initialize radix tree iterator
  250. *
  251. * @iter: pointer to iterator state
  252. * @start: iteration starting index
  253. * Returns: NULL
  254. */
  255. static __always_inline void __rcu **
  256. radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
  257. {
  258. /*
  259. * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
  260. * in the case of a successful tagged chunk lookup. If the lookup was
  261. * unsuccessful or non-tagged then nobody cares about ->tags.
  262. *
  263. * Set index to zero to bypass next_index overflow protection.
  264. * See the comment in radix_tree_next_chunk() for details.
  265. */
  266. iter->index = 0;
  267. iter->next_index = start;
  268. return NULL;
  269. }
  270. /**
  271. * radix_tree_next_chunk - find next chunk of slots for iteration
  272. *
  273. * @root: radix tree root
  274. * @iter: iterator state
  275. * @flags: RADIX_TREE_ITER_* flags and tag index
  276. * Returns: pointer to chunk first slot, or NULL if there no more left
  277. *
  278. * This function looks up the next chunk in the radix tree starting from
  279. * @iter->next_index. It returns a pointer to the chunk's first slot.
  280. * Also it fills @iter with data about chunk: position in the tree (index),
  281. * its end (next_index), and constructs a bit mask for tagged iterating (tags).
  282. */
  283. void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
  284. struct radix_tree_iter *iter, unsigned flags);
  285. /**
  286. * radix_tree_iter_lookup - look up an index in the radix tree
  287. * @root: radix tree root
  288. * @iter: iterator state
  289. * @index: key to look up
  290. *
  291. * If @index is present in the radix tree, this function returns the slot
  292. * containing it and updates @iter to describe the entry. If @index is not
  293. * present, it returns NULL.
  294. */
  295. static inline void __rcu **
  296. radix_tree_iter_lookup(const struct radix_tree_root *root,
  297. struct radix_tree_iter *iter, unsigned long index)
  298. {
  299. radix_tree_iter_init(iter, index);
  300. return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG);
  301. }
  302. /**
  303. * radix_tree_iter_retry - retry this chunk of the iteration
  304. * @iter: iterator state
  305. *
  306. * If we iterate over a tree protected only by the RCU lock, a race
  307. * against deletion or creation may result in seeing a slot for which
  308. * radix_tree_deref_retry() returns true. If so, call this function
  309. * and continue the iteration.
  310. */
  311. static inline __must_check
  312. void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
  313. {
  314. iter->next_index = iter->index;
  315. iter->tags = 0;
  316. return NULL;
  317. }
  318. static inline unsigned long
  319. __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
  320. {
  321. return iter->index + slots;
  322. }
  323. /**
  324. * radix_tree_iter_resume - resume iterating when the chunk may be invalid
  325. * @slot: pointer to current slot
  326. * @iter: iterator state
  327. * Returns: New slot pointer
  328. *
  329. * If the iterator needs to release then reacquire a lock, the chunk may
  330. * have been invalidated by an insertion or deletion. Call this function
  331. * before releasing the lock to continue the iteration from the next index.
  332. */
  333. void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
  334. struct radix_tree_iter *iter);
  335. /**
  336. * radix_tree_chunk_size - get current chunk size
  337. *
  338. * @iter: pointer to radix tree iterator
  339. * Returns: current chunk size
  340. */
  341. static __always_inline long
  342. radix_tree_chunk_size(struct radix_tree_iter *iter)
  343. {
  344. return iter->next_index - iter->index;
  345. }
  346. /**
  347. * radix_tree_next_slot - find next slot in chunk
  348. *
  349. * @slot: pointer to current slot
  350. * @iter: pointer to iterator state
  351. * @flags: RADIX_TREE_ITER_*, should be constant
  352. * Returns: pointer to next slot, or NULL if there no more left
  353. *
  354. * This function updates @iter->index in the case of a successful lookup.
  355. * For tagged lookup it also eats @iter->tags.
  356. *
  357. * There are several cases where 'slot' can be passed in as NULL to this
  358. * function. These cases result from the use of radix_tree_iter_resume() or
  359. * radix_tree_iter_retry(). In these cases we don't end up dereferencing
  360. * 'slot' because either:
  361. * a) we are doing tagged iteration and iter->tags has been set to 0, or
  362. * b) we are doing non-tagged iteration, and iter->index and iter->next_index
  363. * have been set up so that radix_tree_chunk_size() returns 1 or 0.
  364. */
  365. static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
  366. struct radix_tree_iter *iter, unsigned flags)
  367. {
  368. if (flags & RADIX_TREE_ITER_TAGGED) {
  369. iter->tags >>= 1;
  370. if (unlikely(!iter->tags))
  371. return NULL;
  372. if (likely(iter->tags & 1ul)) {
  373. iter->index = __radix_tree_iter_add(iter, 1);
  374. slot++;
  375. goto found;
  376. }
  377. if (!(flags & RADIX_TREE_ITER_CONTIG)) {
  378. unsigned offset = __ffs(iter->tags);
  379. iter->tags >>= offset++;
  380. iter->index = __radix_tree_iter_add(iter, offset);
  381. slot += offset;
  382. goto found;
  383. }
  384. } else {
  385. long count = radix_tree_chunk_size(iter);
  386. while (--count > 0) {
  387. slot++;
  388. iter->index = __radix_tree_iter_add(iter, 1);
  389. if (likely(*slot))
  390. goto found;
  391. if (flags & RADIX_TREE_ITER_CONTIG) {
  392. /* forbid switching to the next chunk */
  393. iter->next_index = 0;
  394. break;
  395. }
  396. }
  397. }
  398. return NULL;
  399. found:
  400. return slot;
  401. }
  402. /**
  403. * radix_tree_for_each_slot - iterate over non-empty slots
  404. *
  405. * @slot: the void** variable for pointer to slot
  406. * @root: the struct radix_tree_root pointer
  407. * @iter: the struct radix_tree_iter pointer
  408. * @start: iteration starting index
  409. *
  410. * @slot points to radix tree slot, @iter->index contains its index.
  411. */
  412. #define radix_tree_for_each_slot(slot, root, iter, start) \
  413. for (slot = radix_tree_iter_init(iter, start) ; \
  414. slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
  415. slot = radix_tree_next_slot(slot, iter, 0))
  416. /**
  417. * radix_tree_for_each_tagged - iterate over tagged slots
  418. *
  419. * @slot: the void** variable for pointer to slot
  420. * @root: the struct radix_tree_root pointer
  421. * @iter: the struct radix_tree_iter pointer
  422. * @start: iteration starting index
  423. * @tag: tag index
  424. *
  425. * @slot points to radix tree slot, @iter->index contains its index.
  426. */
  427. #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
  428. for (slot = radix_tree_iter_init(iter, start) ; \
  429. slot || (slot = radix_tree_next_chunk(root, iter, \
  430. RADIX_TREE_ITER_TAGGED | tag)) ; \
  431. slot = radix_tree_next_slot(slot, iter, \
  432. RADIX_TREE_ITER_TAGGED | tag))
  433. #endif /* _LINUX_RADIX_TREE_H */