radix-tree.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/kernel.h>
  25. #include <linux/rcupdate.h>
  26. /*
  27. * An indirect pointer (root->rnode pointing to a radix_tree_node, rather
  28. * than a data item) is signalled by the low bit set in the root->rnode
  29. * pointer.
  30. *
  31. * In this case root->height is > 0, but the indirect pointer tests are
  32. * needed for RCU lookups (because root->height is unreliable). The only
  33. * time callers need worry about this is when doing a lookup_slot under
  34. * RCU.
  35. */
  36. #define RADIX_TREE_INDIRECT_PTR 1
  37. #define RADIX_TREE_RETRY ((void *)-1UL)
  38. static inline void *radix_tree_ptr_to_indirect(void *ptr)
  39. {
  40. return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
  41. }
  42. static inline void *radix_tree_indirect_to_ptr(void *ptr)
  43. {
  44. return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
  45. }
  46. #define radix_tree_indirect_to_ptr(ptr) \
  47. radix_tree_indirect_to_ptr((void __force *)(ptr))
  48. static inline int radix_tree_is_indirect_ptr(void *ptr)
  49. {
  50. return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
  51. }
  52. /*** radix-tree API starts here ***/
  53. #define RADIX_TREE_MAX_TAGS 3
  54. /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
  55. struct radix_tree_root {
  56. unsigned int height;
  57. gfp_t gfp_mask;
  58. struct radix_tree_node __rcu *rnode;
  59. };
  60. #define RADIX_TREE_INIT(mask) { \
  61. .height = 0, \
  62. .gfp_mask = (mask), \
  63. .rnode = NULL, \
  64. }
  65. #define RADIX_TREE(name, mask) \
  66. struct radix_tree_root name = RADIX_TREE_INIT(mask)
  67. #define INIT_RADIX_TREE(root, mask) \
  68. do { \
  69. (root)->height = 0; \
  70. (root)->gfp_mask = (mask); \
  71. (root)->rnode = NULL; \
  72. } while (0)
  73. /**
  74. * Radix-tree synchronization
  75. *
  76. * The radix-tree API requires that users provide all synchronisation (with
  77. * specific exceptions, noted below).
  78. *
  79. * Synchronization of access to the data items being stored in the tree, and
  80. * management of their lifetimes must be completely managed by API users.
  81. *
  82. * For API usage, in general,
  83. * - any function _modifying_ the tree or tags (inserting or deleting
  84. * items, setting or clearing tags) must exclude other modifications, and
  85. * exclude any functions reading the tree.
  86. * - any function _reading_ the tree or tags (looking up items or tags,
  87. * gang lookups) must exclude modifications to the tree, but may occur
  88. * concurrently with other readers.
  89. *
  90. * The notable exceptions to this rule are the following functions:
  91. * radix_tree_lookup
  92. * radix_tree_lookup_slot
  93. * radix_tree_tag_get
  94. * radix_tree_gang_lookup
  95. * radix_tree_gang_lookup_slot
  96. * radix_tree_gang_lookup_tag
  97. * radix_tree_gang_lookup_tag_slot
  98. * radix_tree_tagged
  99. *
  100. * The first 7 functions are able to be called locklessly, using RCU. The
  101. * caller must ensure calls to these functions are made within rcu_read_lock()
  102. * regions. Other readers (lock-free or otherwise) and modifications may be
  103. * running concurrently.
  104. *
  105. * It is still required that the caller manage the synchronization and lifetimes
  106. * of the items. So if RCU lock-free lookups are used, typically this would mean
  107. * that the items have their own locks, or are amenable to lock-free access; and
  108. * that the items are freed by RCU (or only freed after having been deleted from
  109. * the radix tree *and* a synchronize_rcu() grace period).
  110. *
  111. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  112. * access to data items when inserting into or looking up from the radix tree)
  113. *
  114. * Note that the value returned by radix_tree_tag_get() may not be relied upon
  115. * if only the RCU read lock is held. Functions to set/clear tags and to
  116. * delete nodes running concurrently with it may affect its result such that
  117. * two consecutive reads in the same locked section may return different
  118. * values. If reliability is required, modification functions must also be
  119. * excluded from concurrency.
  120. *
  121. * radix_tree_tagged is able to be called without locking or RCU.
  122. */
  123. /**
  124. * radix_tree_deref_slot - dereference a slot
  125. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  126. * Returns: item that was stored in that slot with any direct pointer flag
  127. * removed.
  128. *
  129. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  130. * locked across slot lookup and dereference. More likely, will be used with
  131. * radix_tree_replace_slot(), as well, so caller will hold tree write locked.
  132. */
  133. static inline void *radix_tree_deref_slot(void **pslot)
  134. {
  135. void *ret = rcu_dereference(*pslot);
  136. if (unlikely(radix_tree_is_indirect_ptr(ret)))
  137. ret = RADIX_TREE_RETRY;
  138. return ret;
  139. }
  140. /**
  141. * radix_tree_replace_slot - replace item in a slot
  142. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  143. * @item: new item to store in the slot.
  144. *
  145. * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
  146. * across slot lookup and replacement.
  147. */
  148. static inline void radix_tree_replace_slot(void **pslot, void *item)
  149. {
  150. BUG_ON(radix_tree_is_indirect_ptr(item));
  151. rcu_assign_pointer(*pslot, item);
  152. }
  153. int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
  154. void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
  155. void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
  156. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  157. unsigned int
  158. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  159. unsigned long first_index, unsigned int max_items);
  160. unsigned int
  161. radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
  162. unsigned long first_index, unsigned int max_items);
  163. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  164. unsigned long index, unsigned long max_scan);
  165. unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
  166. unsigned long index, unsigned long max_scan);
  167. int radix_tree_preload(gfp_t gfp_mask);
  168. void radix_tree_init(void);
  169. void *radix_tree_tag_set(struct radix_tree_root *root,
  170. unsigned long index, unsigned int tag);
  171. void *radix_tree_tag_clear(struct radix_tree_root *root,
  172. unsigned long index, unsigned int tag);
  173. int radix_tree_tag_get(struct radix_tree_root *root,
  174. unsigned long index, unsigned int tag);
  175. unsigned int
  176. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  177. unsigned long first_index, unsigned int max_items,
  178. unsigned int tag);
  179. unsigned int
  180. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  181. unsigned long first_index, unsigned int max_items,
  182. unsigned int tag);
  183. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  184. unsigned long *first_indexp, unsigned long last_index,
  185. unsigned long nr_to_tag,
  186. unsigned int fromtag, unsigned int totag);
  187. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  188. static inline void radix_tree_preload_end(void)
  189. {
  190. preempt_enable();
  191. }
  192. #endif /* _LINUX_RADIX_TREE_H */