radix-tree.h 17 KB

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