radix-tree.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2006 Nick Piggin
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #ifndef _LINUX_RADIX_TREE_H
  21. #define _LINUX_RADIX_TREE_H
  22. #include <linux/preempt.h>
  23. #include <linux/types.h>
  24. #include <linux/bug.h>
  25. #include <linux/kernel.h>
  26. #include <linux/rcupdate.h>
  27. /*
  28. * An indirect pointer (root->rnode pointing to a radix_tree_node, rather
  29. * than a data item) is signalled by the low bit set in the root->rnode
  30. * pointer.
  31. *
  32. * In this case root->height is > 0, but the indirect pointer tests are
  33. * needed for RCU lookups (because root->height is unreliable). The only
  34. * time callers need worry about this is when doing a lookup_slot under
  35. * RCU.
  36. *
  37. * Indirect pointer in fact is also used to tag the last pointer of a node
  38. * when it is shrunk, before we rcu free the node. See shrink code for
  39. * details.
  40. */
  41. #define RADIX_TREE_INDIRECT_PTR 1
  42. /*
  43. * A common use of the radix tree is to store pointers to struct pages;
  44. * but shmem/tmpfs needs also to store swap entries in the same tree:
  45. * those are marked as exceptional entries to distinguish them.
  46. * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it.
  47. */
  48. #define RADIX_TREE_EXCEPTIONAL_ENTRY 2
  49. #define RADIX_TREE_EXCEPTIONAL_SHIFT 2
  50. static inline int radix_tree_is_indirect_ptr(void *ptr)
  51. {
  52. return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
  53. }
  54. /*** radix-tree API starts here ***/
  55. #define RADIX_TREE_MAX_TAGS 3
  56. /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
  57. struct radix_tree_root {
  58. unsigned int height;
  59. gfp_t gfp_mask;
  60. struct radix_tree_node __rcu *rnode;
  61. };
  62. #define RADIX_TREE_INIT(mask) { \
  63. .height = 0, \
  64. .gfp_mask = (mask), \
  65. .rnode = NULL, \
  66. }
  67. #define RADIX_TREE(name, mask) \
  68. struct radix_tree_root name = RADIX_TREE_INIT(mask)
  69. #define INIT_RADIX_TREE(root, mask) \
  70. do { \
  71. (root)->height = 0; \
  72. (root)->gfp_mask = (mask); \
  73. (root)->rnode = NULL; \
  74. } while (0)
  75. /**
  76. * Radix-tree synchronization
  77. *
  78. * The radix-tree API requires that users provide all synchronisation (with
  79. * specific exceptions, noted below).
  80. *
  81. * Synchronization of access to the data items being stored in the tree, and
  82. * management of their lifetimes must be completely managed by API users.
  83. *
  84. * For API usage, in general,
  85. * - any function _modifying_ the tree or tags (inserting or deleting
  86. * items, setting or clearing tags) must exclude other modifications, and
  87. * exclude any functions reading the tree.
  88. * - any function _reading_ the tree or tags (looking up items or tags,
  89. * gang lookups) must exclude modifications to the tree, but may occur
  90. * concurrently with other readers.
  91. *
  92. * The notable exceptions to this rule are the following functions:
  93. * radix_tree_lookup
  94. * radix_tree_lookup_slot
  95. * radix_tree_tag_get
  96. * radix_tree_gang_lookup
  97. * radix_tree_gang_lookup_slot
  98. * radix_tree_gang_lookup_tag
  99. * radix_tree_gang_lookup_tag_slot
  100. * radix_tree_tagged
  101. *
  102. * The first 7 functions are able to be called locklessly, using RCU. The
  103. * caller must ensure calls to these functions are made within rcu_read_lock()
  104. * regions. Other readers (lock-free or otherwise) and modifications may be
  105. * running concurrently.
  106. *
  107. * It is still required that the caller manage the synchronization and lifetimes
  108. * of the items. So if RCU lock-free lookups are used, typically this would mean
  109. * that the items have their own locks, or are amenable to lock-free access; and
  110. * that the items are freed by RCU (or only freed after having been deleted from
  111. * the radix tree *and* a synchronize_rcu() grace period).
  112. *
  113. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  114. * access to data items when inserting into or looking up from the radix tree)
  115. *
  116. * Note that the value returned by radix_tree_tag_get() may not be relied upon
  117. * if only the RCU read lock is held. Functions to set/clear tags and to
  118. * delete nodes running concurrently with it may affect its result such that
  119. * two consecutive reads in the same locked section may return different
  120. * values. If reliability is required, modification functions must also be
  121. * excluded from concurrency.
  122. *
  123. * radix_tree_tagged is able to be called without locking or RCU.
  124. */
  125. /**
  126. * radix_tree_deref_slot - dereference a slot
  127. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  128. * Returns: item that was stored in that slot with any direct pointer flag
  129. * removed.
  130. *
  131. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  132. * locked across slot lookup and dereference. Not required if write lock is
  133. * held (ie. items cannot be concurrently inserted).
  134. *
  135. * radix_tree_deref_retry must be used to confirm validity of the pointer if
  136. * only the read lock is held.
  137. */
  138. static inline void *radix_tree_deref_slot(void **pslot)
  139. {
  140. return rcu_dereference(*pslot);
  141. }
  142. /**
  143. * radix_tree_deref_slot_protected - dereference a slot without RCU lock but with tree lock held
  144. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  145. * Returns: item that was stored in that slot with any direct pointer flag
  146. * removed.
  147. *
  148. * Similar to radix_tree_deref_slot but only used during migration when a pages
  149. * mapping is being moved. The caller does not hold the RCU read lock but it
  150. * must hold the tree lock to prevent parallel updates.
  151. */
  152. static inline void *radix_tree_deref_slot_protected(void **pslot,
  153. spinlock_t *treelock)
  154. {
  155. return rcu_dereference_protected(*pslot, lockdep_is_held(treelock));
  156. }
  157. /**
  158. * radix_tree_deref_retry - check radix_tree_deref_slot
  159. * @arg: pointer returned by radix_tree_deref_slot
  160. * Returns: 0 if retry is not required, otherwise retry is required
  161. *
  162. * radix_tree_deref_retry must be used with radix_tree_deref_slot.
  163. */
  164. static inline int radix_tree_deref_retry(void *arg)
  165. {
  166. return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR);
  167. }
  168. /**
  169. * radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry?
  170. * @arg: value returned by radix_tree_deref_slot
  171. * Returns: 0 if well-aligned pointer, non-0 if exceptional entry.
  172. */
  173. static inline int radix_tree_exceptional_entry(void *arg)
  174. {
  175. /* Not unlikely because radix_tree_exception often tested first */
  176. return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY;
  177. }
  178. /**
  179. * radix_tree_exception - radix_tree_deref_slot returned either exception?
  180. * @arg: value returned by radix_tree_deref_slot
  181. * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
  182. */
  183. static inline int radix_tree_exception(void *arg)
  184. {
  185. return unlikely((unsigned long)arg &
  186. (RADIX_TREE_INDIRECT_PTR | RADIX_TREE_EXCEPTIONAL_ENTRY));
  187. }
  188. /**
  189. * radix_tree_replace_slot - replace item in a slot
  190. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  191. * @item: new item to store in the slot.
  192. *
  193. * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
  194. * across slot lookup and replacement.
  195. */
  196. static inline void radix_tree_replace_slot(void **pslot, void *item)
  197. {
  198. BUG_ON(radix_tree_is_indirect_ptr(item));
  199. rcu_assign_pointer(*pslot, item);
  200. }
  201. int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
  202. void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
  203. void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
  204. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  205. unsigned int
  206. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  207. unsigned long first_index, unsigned int max_items);
  208. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  209. void ***results, unsigned long *indices,
  210. unsigned long first_index, unsigned int max_items);
  211. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  212. unsigned long index, unsigned long max_scan);
  213. unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
  214. unsigned long index, unsigned long max_scan);
  215. int radix_tree_preload(gfp_t gfp_mask);
  216. void radix_tree_init(void);
  217. void *radix_tree_tag_set(struct radix_tree_root *root,
  218. unsigned long index, unsigned int tag);
  219. void *radix_tree_tag_clear(struct radix_tree_root *root,
  220. unsigned long index, unsigned int tag);
  221. int radix_tree_tag_get(struct radix_tree_root *root,
  222. unsigned long index, unsigned int tag);
  223. unsigned int
  224. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  225. unsigned long first_index, unsigned int max_items,
  226. unsigned int tag);
  227. unsigned int
  228. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  229. unsigned long first_index, unsigned int max_items,
  230. unsigned int tag);
  231. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  232. unsigned long *first_indexp, unsigned long last_index,
  233. unsigned long nr_to_tag,
  234. unsigned int fromtag, unsigned int totag);
  235. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  236. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  237. static inline void radix_tree_preload_end(void)
  238. {
  239. preempt_enable();
  240. }
  241. #endif /* _LINUX_RADIX_TREE_H */