radix-tree.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. * @shift: shift for the node that holds our slots
  86. *
  87. * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
  88. * subinterval of slots contained within one radix tree leaf node. It is
  89. * described by a pointer to its first slot and a struct radix_tree_iter
  90. * which holds the chunk's position in the tree and its size. For tagged
  91. * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
  92. * radix tree tag.
  93. */
  94. struct radix_tree_iter {
  95. unsigned long index;
  96. unsigned long next_index;
  97. unsigned long tags;
  98. struct radix_tree_node *node;
  99. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  100. unsigned int shift;
  101. #endif
  102. };
  103. static inline unsigned int iter_shift(const struct radix_tree_iter *iter)
  104. {
  105. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  106. return iter->shift;
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. /**
  112. * Radix-tree synchronization
  113. *
  114. * The radix-tree API requires that users provide all synchronisation (with
  115. * specific exceptions, noted below).
  116. *
  117. * Synchronization of access to the data items being stored in the tree, and
  118. * management of their lifetimes must be completely managed by API users.
  119. *
  120. * For API usage, in general,
  121. * - any function _modifying_ the tree or tags (inserting or deleting
  122. * items, setting or clearing tags) must exclude other modifications, and
  123. * exclude any functions reading the tree.
  124. * - any function _reading_ the tree or tags (looking up items or tags,
  125. * gang lookups) must exclude modifications to the tree, but may occur
  126. * concurrently with other readers.
  127. *
  128. * The notable exceptions to this rule are the following functions:
  129. * __radix_tree_lookup
  130. * radix_tree_lookup
  131. * radix_tree_lookup_slot
  132. * radix_tree_tag_get
  133. * radix_tree_gang_lookup
  134. * radix_tree_gang_lookup_tag
  135. * radix_tree_gang_lookup_tag_slot
  136. * radix_tree_tagged
  137. *
  138. * The first 7 functions are able to be called locklessly, using RCU. The
  139. * caller must ensure calls to these functions are made within rcu_read_lock()
  140. * regions. Other readers (lock-free or otherwise) and modifications may be
  141. * running concurrently.
  142. *
  143. * It is still required that the caller manage the synchronization and lifetimes
  144. * of the items. So if RCU lock-free lookups are used, typically this would mean
  145. * that the items have their own locks, or are amenable to lock-free access; and
  146. * that the items are freed by RCU (or only freed after having been deleted from
  147. * the radix tree *and* a synchronize_rcu() grace period).
  148. *
  149. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  150. * access to data items when inserting into or looking up from the radix tree)
  151. *
  152. * Note that the value returned by radix_tree_tag_get() may not be relied upon
  153. * if only the RCU read lock is held. Functions to set/clear tags and to
  154. * delete nodes running concurrently with it may affect its result such that
  155. * two consecutive reads in the same locked section may return different
  156. * values. If reliability is required, modification functions must also be
  157. * excluded from concurrency.
  158. *
  159. * radix_tree_tagged is able to be called without locking or RCU.
  160. */
  161. /**
  162. * radix_tree_deref_slot - dereference a slot
  163. * @slot: slot pointer, returned by radix_tree_lookup_slot
  164. *
  165. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  166. * locked across slot lookup and dereference. Not required if write lock is
  167. * held (ie. items cannot be concurrently inserted).
  168. *
  169. * radix_tree_deref_retry must be used to confirm validity of the pointer if
  170. * only the read lock is held.
  171. *
  172. * Return: entry stored in that slot.
  173. */
  174. static inline void *radix_tree_deref_slot(void __rcu **slot)
  175. {
  176. return rcu_dereference(*slot);
  177. }
  178. /**
  179. * radix_tree_deref_slot_protected - dereference a slot with tree lock held
  180. * @slot: slot pointer, returned by radix_tree_lookup_slot
  181. *
  182. * Similar to radix_tree_deref_slot. The caller does not hold the RCU read
  183. * lock but it must hold the tree lock to prevent parallel updates.
  184. *
  185. * Return: entry stored in that slot.
  186. */
  187. static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
  188. spinlock_t *treelock)
  189. {
  190. return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
  191. }
  192. /**
  193. * radix_tree_deref_retry - check radix_tree_deref_slot
  194. * @arg: pointer returned by radix_tree_deref_slot
  195. * Returns: 0 if retry is not required, otherwise retry is required
  196. *
  197. * radix_tree_deref_retry must be used with radix_tree_deref_slot.
  198. */
  199. static inline int radix_tree_deref_retry(void *arg)
  200. {
  201. return unlikely(radix_tree_is_internal_node(arg));
  202. }
  203. /**
  204. * radix_tree_exception - radix_tree_deref_slot returned either exception?
  205. * @arg: value returned by radix_tree_deref_slot
  206. * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
  207. */
  208. static inline int radix_tree_exception(void *arg)
  209. {
  210. return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
  211. }
  212. int __radix_tree_insert(struct radix_tree_root *, unsigned long index,
  213. unsigned order, void *);
  214. static inline int radix_tree_insert(struct radix_tree_root *root,
  215. unsigned long index, void *entry)
  216. {
  217. return __radix_tree_insert(root, index, 0, entry);
  218. }
  219. void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
  220. struct radix_tree_node **nodep, void __rcu ***slotp);
  221. void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
  222. void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
  223. unsigned long index);
  224. void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
  225. void __rcu **slot, void *entry);
  226. void radix_tree_iter_replace(struct radix_tree_root *,
  227. const struct radix_tree_iter *, void __rcu **slot, void *entry);
  228. void radix_tree_replace_slot(struct radix_tree_root *,
  229. void __rcu **slot, void *entry);
  230. void radix_tree_iter_delete(struct radix_tree_root *,
  231. struct radix_tree_iter *iter, void __rcu **slot);
  232. void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
  233. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  234. void radix_tree_clear_tags(struct radix_tree_root *, struct radix_tree_node *,
  235. void __rcu **slot);
  236. unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
  237. void **results, unsigned long first_index,
  238. unsigned int max_items);
  239. int radix_tree_preload(gfp_t gfp_mask);
  240. int radix_tree_maybe_preload(gfp_t gfp_mask);
  241. int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
  242. void radix_tree_init(void);
  243. void *radix_tree_tag_set(struct radix_tree_root *,
  244. unsigned long index, unsigned int tag);
  245. void *radix_tree_tag_clear(struct radix_tree_root *,
  246. unsigned long index, unsigned int tag);
  247. int radix_tree_tag_get(const struct radix_tree_root *,
  248. unsigned long index, unsigned int tag);
  249. void radix_tree_iter_tag_set(struct radix_tree_root *,
  250. const struct radix_tree_iter *iter, unsigned int tag);
  251. void radix_tree_iter_tag_clear(struct radix_tree_root *,
  252. const struct radix_tree_iter *iter, unsigned int tag);
  253. unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *,
  254. void **results, unsigned long first_index,
  255. unsigned int max_items, unsigned int tag);
  256. unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *,
  257. void __rcu ***results, unsigned long first_index,
  258. unsigned int max_items, unsigned int tag);
  259. int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag);
  260. static inline void radix_tree_preload_end(void)
  261. {
  262. preempt_enable();
  263. }
  264. int radix_tree_split_preload(unsigned old_order, unsigned new_order, gfp_t);
  265. int radix_tree_split(struct radix_tree_root *, unsigned long index,
  266. unsigned new_order);
  267. int radix_tree_join(struct radix_tree_root *, unsigned long index,
  268. unsigned new_order, void *);
  269. void __rcu **idr_get_free(struct radix_tree_root *root,
  270. struct radix_tree_iter *iter, gfp_t gfp,
  271. unsigned long max);
  272. enum {
  273. RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */
  274. RADIX_TREE_ITER_TAGGED = 0x10, /* lookup tagged slots */
  275. RADIX_TREE_ITER_CONTIG = 0x20, /* stop at first hole */
  276. };
  277. /**
  278. * radix_tree_iter_init - initialize radix tree iterator
  279. *
  280. * @iter: pointer to iterator state
  281. * @start: iteration starting index
  282. * Returns: NULL
  283. */
  284. static __always_inline void __rcu **
  285. radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
  286. {
  287. /*
  288. * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
  289. * in the case of a successful tagged chunk lookup. If the lookup was
  290. * unsuccessful or non-tagged then nobody cares about ->tags.
  291. *
  292. * Set index to zero to bypass next_index overflow protection.
  293. * See the comment in radix_tree_next_chunk() for details.
  294. */
  295. iter->index = 0;
  296. iter->next_index = start;
  297. return NULL;
  298. }
  299. /**
  300. * radix_tree_next_chunk - find next chunk of slots for iteration
  301. *
  302. * @root: radix tree root
  303. * @iter: iterator state
  304. * @flags: RADIX_TREE_ITER_* flags and tag index
  305. * Returns: pointer to chunk first slot, or NULL if there no more left
  306. *
  307. * This function looks up the next chunk in the radix tree starting from
  308. * @iter->next_index. It returns a pointer to the chunk's first slot.
  309. * Also it fills @iter with data about chunk: position in the tree (index),
  310. * its end (next_index), and constructs a bit mask for tagged iterating (tags).
  311. */
  312. void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
  313. struct radix_tree_iter *iter, unsigned flags);
  314. /**
  315. * radix_tree_iter_lookup - look up an index in the radix tree
  316. * @root: radix tree root
  317. * @iter: iterator state
  318. * @index: key to look up
  319. *
  320. * If @index is present in the radix tree, this function returns the slot
  321. * containing it and updates @iter to describe the entry. If @index is not
  322. * present, it returns NULL.
  323. */
  324. static inline void __rcu **
  325. radix_tree_iter_lookup(const struct radix_tree_root *root,
  326. struct radix_tree_iter *iter, unsigned long index)
  327. {
  328. radix_tree_iter_init(iter, index);
  329. return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG);
  330. }
  331. /**
  332. * radix_tree_iter_find - find a present entry
  333. * @root: radix tree root
  334. * @iter: iterator state
  335. * @index: start location
  336. *
  337. * This function returns the slot containing the entry with the lowest index
  338. * which is at least @index. If @index is larger than any present entry, this
  339. * function returns NULL. The @iter is updated to describe the entry found.
  340. */
  341. static inline void __rcu **
  342. radix_tree_iter_find(const struct radix_tree_root *root,
  343. struct radix_tree_iter *iter, unsigned long index)
  344. {
  345. radix_tree_iter_init(iter, index);
  346. return radix_tree_next_chunk(root, iter, 0);
  347. }
  348. /**
  349. * radix_tree_iter_retry - retry this chunk of the iteration
  350. * @iter: iterator state
  351. *
  352. * If we iterate over a tree protected only by the RCU lock, a race
  353. * against deletion or creation may result in seeing a slot for which
  354. * radix_tree_deref_retry() returns true. If so, call this function
  355. * and continue the iteration.
  356. */
  357. static inline __must_check
  358. void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
  359. {
  360. iter->next_index = iter->index;
  361. iter->tags = 0;
  362. return NULL;
  363. }
  364. static inline unsigned long
  365. __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
  366. {
  367. return iter->index + (slots << iter_shift(iter));
  368. }
  369. /**
  370. * radix_tree_iter_resume - resume iterating when the chunk may be invalid
  371. * @slot: pointer to current slot
  372. * @iter: iterator state
  373. * Returns: New slot pointer
  374. *
  375. * If the iterator needs to release then reacquire a lock, the chunk may
  376. * have been invalidated by an insertion or deletion. Call this function
  377. * before releasing the lock to continue the iteration from the next index.
  378. */
  379. void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
  380. struct radix_tree_iter *iter);
  381. /**
  382. * radix_tree_chunk_size - get current chunk size
  383. *
  384. * @iter: pointer to radix tree iterator
  385. * Returns: current chunk size
  386. */
  387. static __always_inline long
  388. radix_tree_chunk_size(struct radix_tree_iter *iter)
  389. {
  390. return (iter->next_index - iter->index) >> iter_shift(iter);
  391. }
  392. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  393. void __rcu **__radix_tree_next_slot(void __rcu **slot,
  394. struct radix_tree_iter *iter, unsigned flags);
  395. #else
  396. /* Can't happen without sibling entries, but the compiler can't tell that */
  397. static inline void __rcu **__radix_tree_next_slot(void __rcu **slot,
  398. struct radix_tree_iter *iter, unsigned flags)
  399. {
  400. return slot;
  401. }
  402. #endif
  403. /**
  404. * radix_tree_next_slot - find next slot in chunk
  405. *
  406. * @slot: pointer to current slot
  407. * @iter: pointer to interator state
  408. * @flags: RADIX_TREE_ITER_*, should be constant
  409. * Returns: pointer to next slot, or NULL if there no more left
  410. *
  411. * This function updates @iter->index in the case of a successful lookup.
  412. * For tagged lookup it also eats @iter->tags.
  413. *
  414. * There are several cases where 'slot' can be passed in as NULL to this
  415. * function. These cases result from the use of radix_tree_iter_resume() or
  416. * radix_tree_iter_retry(). In these cases we don't end up dereferencing
  417. * 'slot' because either:
  418. * a) we are doing tagged iteration and iter->tags has been set to 0, or
  419. * b) we are doing non-tagged iteration, and iter->index and iter->next_index
  420. * have been set up so that radix_tree_chunk_size() returns 1 or 0.
  421. */
  422. static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
  423. struct radix_tree_iter *iter, unsigned flags)
  424. {
  425. if (flags & RADIX_TREE_ITER_TAGGED) {
  426. iter->tags >>= 1;
  427. if (unlikely(!iter->tags))
  428. return NULL;
  429. if (likely(iter->tags & 1ul)) {
  430. iter->index = __radix_tree_iter_add(iter, 1);
  431. slot++;
  432. goto found;
  433. }
  434. if (!(flags & RADIX_TREE_ITER_CONTIG)) {
  435. unsigned offset = __ffs(iter->tags);
  436. iter->tags >>= offset++;
  437. iter->index = __radix_tree_iter_add(iter, offset);
  438. slot += offset;
  439. goto found;
  440. }
  441. } else {
  442. long count = radix_tree_chunk_size(iter);
  443. while (--count > 0) {
  444. slot++;
  445. iter->index = __radix_tree_iter_add(iter, 1);
  446. if (likely(*slot))
  447. goto found;
  448. if (flags & RADIX_TREE_ITER_CONTIG) {
  449. /* forbid switching to the next chunk */
  450. iter->next_index = 0;
  451. break;
  452. }
  453. }
  454. }
  455. return NULL;
  456. found:
  457. if (unlikely(radix_tree_is_internal_node(rcu_dereference_raw(*slot))))
  458. return __radix_tree_next_slot(slot, iter, flags);
  459. return slot;
  460. }
  461. /**
  462. * radix_tree_for_each_slot - iterate over non-empty slots
  463. *
  464. * @slot: the void** variable for pointer to slot
  465. * @root: the struct radix_tree_root pointer
  466. * @iter: the struct radix_tree_iter pointer
  467. * @start: iteration starting index
  468. *
  469. * @slot points to radix tree slot, @iter->index contains its index.
  470. */
  471. #define radix_tree_for_each_slot(slot, root, iter, start) \
  472. for (slot = radix_tree_iter_init(iter, start) ; \
  473. slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
  474. slot = radix_tree_next_slot(slot, iter, 0))
  475. /**
  476. * radix_tree_for_each_tagged - iterate over tagged slots
  477. *
  478. * @slot: the void** variable for pointer to slot
  479. * @root: the struct radix_tree_root pointer
  480. * @iter: the struct radix_tree_iter pointer
  481. * @start: iteration starting index
  482. * @tag: tag index
  483. *
  484. * @slot points to radix tree slot, @iter->index contains its index.
  485. */
  486. #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
  487. for (slot = radix_tree_iter_init(iter, start) ; \
  488. slot || (slot = radix_tree_next_chunk(root, iter, \
  489. RADIX_TREE_ITER_TAGGED | tag)) ; \
  490. slot = radix_tree_next_slot(slot, iter, \
  491. RADIX_TREE_ITER_TAGGED | tag))
  492. #endif /* _LINUX_RADIX_TREE_H */