radix-tree.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * A direct pointer (root->rnode pointing directly to a data item,
  28. * rather than another radix_tree_node) is signalled by the low bit
  29. * set in the root->rnode pointer.
  30. *
  31. * In this case root->height is also NULL, but the direct pointer tests are
  32. * needed for RCU lookups when root->height is unreliable.
  33. */
  34. #define RADIX_TREE_DIRECT_PTR 1
  35. static inline void *radix_tree_ptr_to_direct(void *ptr)
  36. {
  37. return (void *)((unsigned long)ptr | RADIX_TREE_DIRECT_PTR);
  38. }
  39. static inline void *radix_tree_direct_to_ptr(void *ptr)
  40. {
  41. return (void *)((unsigned long)ptr & ~RADIX_TREE_DIRECT_PTR);
  42. }
  43. static inline int radix_tree_is_direct_ptr(void *ptr)
  44. {
  45. return (int)((unsigned long)ptr & RADIX_TREE_DIRECT_PTR);
  46. }
  47. /*** radix-tree API starts here ***/
  48. #define RADIX_TREE_MAX_TAGS 2
  49. /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
  50. struct radix_tree_root {
  51. unsigned int height;
  52. gfp_t gfp_mask;
  53. struct radix_tree_node *rnode;
  54. };
  55. #define RADIX_TREE_INIT(mask) { \
  56. .height = 0, \
  57. .gfp_mask = (mask), \
  58. .rnode = NULL, \
  59. }
  60. #define RADIX_TREE(name, mask) \
  61. struct radix_tree_root name = RADIX_TREE_INIT(mask)
  62. #define INIT_RADIX_TREE(root, mask) \
  63. do { \
  64. (root)->height = 0; \
  65. (root)->gfp_mask = (mask); \
  66. (root)->rnode = NULL; \
  67. } while (0)
  68. /**
  69. * Radix-tree synchronization
  70. *
  71. * The radix-tree API requires that users provide all synchronisation (with
  72. * specific exceptions, noted below).
  73. *
  74. * Synchronization of access to the data items being stored in the tree, and
  75. * management of their lifetimes must be completely managed by API users.
  76. *
  77. * For API usage, in general,
  78. * - any function _modifying_ the the tree or tags (inserting or deleting
  79. * items, setting or clearing tags must exclude other modifications, and
  80. * exclude any functions reading the tree.
  81. * - any function _reading_ the the tree or tags (looking up items or tags,
  82. * gang lookups) must exclude modifications to the tree, but may occur
  83. * concurrently with other readers.
  84. *
  85. * The notable exceptions to this rule are the following functions:
  86. * radix_tree_lookup
  87. * radix_tree_tag_get
  88. * radix_tree_gang_lookup
  89. * radix_tree_gang_lookup_tag
  90. * radix_tree_tagged
  91. *
  92. * The first 4 functions are able to be called locklessly, using RCU. The
  93. * caller must ensure calls to these functions are made within rcu_read_lock()
  94. * regions. Other readers (lock-free or otherwise) and modifications may be
  95. * running concurrently.
  96. *
  97. * It is still required that the caller manage the synchronization and lifetimes
  98. * of the items. So if RCU lock-free lookups are used, typically this would mean
  99. * that the items have their own locks, or are amenable to lock-free access; and
  100. * that the items are freed by RCU (or only freed after having been deleted from
  101. * the radix tree *and* a synchronize_rcu() grace period).
  102. *
  103. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  104. * access to data items when inserting into or looking up from the radix tree)
  105. *
  106. * radix_tree_tagged is able to be called without locking or RCU.
  107. */
  108. /**
  109. * radix_tree_deref_slot - dereference a slot
  110. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  111. * Returns: item that was stored in that slot with any direct pointer flag
  112. * removed.
  113. *
  114. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  115. * locked across slot lookup and dereference. More likely, will be used with
  116. * radix_tree_replace_slot(), as well, so caller will hold tree write locked.
  117. */
  118. static inline void *radix_tree_deref_slot(void **pslot)
  119. {
  120. return radix_tree_direct_to_ptr(*pslot);
  121. }
  122. /**
  123. * radix_tree_replace_slot - replace item in a slot
  124. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  125. * @item: new item to store in the slot.
  126. *
  127. * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
  128. * across slot lookup and replacement.
  129. */
  130. static inline void radix_tree_replace_slot(void **pslot, void *item)
  131. {
  132. BUG_ON(radix_tree_is_direct_ptr(item));
  133. rcu_assign_pointer(*pslot,
  134. (void *)((unsigned long)item |
  135. ((unsigned long)*pslot & RADIX_TREE_DIRECT_PTR)));
  136. }
  137. int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
  138. void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
  139. void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
  140. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  141. unsigned int
  142. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  143. unsigned long first_index, unsigned int max_items);
  144. int radix_tree_preload(gfp_t gfp_mask);
  145. void radix_tree_init(void);
  146. void *radix_tree_tag_set(struct radix_tree_root *root,
  147. unsigned long index, unsigned int tag);
  148. void *radix_tree_tag_clear(struct radix_tree_root *root,
  149. unsigned long index, unsigned int tag);
  150. int radix_tree_tag_get(struct radix_tree_root *root,
  151. unsigned long index, unsigned int tag);
  152. unsigned int
  153. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  154. unsigned long first_index, unsigned int max_items,
  155. unsigned int tag);
  156. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  157. static inline void radix_tree_preload_end(void)
  158. {
  159. preempt_enable();
  160. }
  161. #endif /* _LINUX_RADIX_TREE_H */