rbtree_latch.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Latched RB-trees
  3. *
  4. * Copyright (C) 2015 Intel Corp., Peter Zijlstra <peterz@infradead.org>
  5. *
  6. * Since RB-trees have non-atomic modifications they're not immediately suited
  7. * for RCU/lockless queries. Even though we made RB-tree lookups non-fatal for
  8. * lockless lookups; we cannot guarantee they return a correct result.
  9. *
  10. * The simplest solution is a seqlock + RB-tree, this will allow lockless
  11. * lookups; but has the constraint (inherent to the seqlock) that read sides
  12. * cannot nest in write sides.
  13. *
  14. * If we need to allow unconditional lookups (say as required for NMI context
  15. * usage) we need a more complex setup; this data structure provides this by
  16. * employing the latch technique -- see @raw_write_seqcount_latch -- to
  17. * implement a latched RB-tree which does allow for unconditional lookups by
  18. * virtue of always having (at least) one stable copy of the tree.
  19. *
  20. * However, while we have the guarantee that there is at all times one stable
  21. * copy, this does not guarantee an iteration will not observe modifications.
  22. * What might have been a stable copy at the start of the iteration, need not
  23. * remain so for the duration of the iteration.
  24. *
  25. * Therefore, this does require a lockless RB-tree iteration to be non-fatal;
  26. * see the comment in lib/rbtree.c. Note however that we only require the first
  27. * condition -- not seeing partial stores -- because the latch thing isolates
  28. * us from loops. If we were to interrupt a modification the lookup would be
  29. * pointed at the stable tree and complete while the modification was halted.
  30. */
  31. #ifndef RB_TREE_LATCH_H
  32. #define RB_TREE_LATCH_H
  33. #include <linux/rbtree.h>
  34. #include <linux/seqlock.h>
  35. struct latch_tree_node {
  36. struct rb_node node[2];
  37. };
  38. struct latch_tree_root {
  39. seqcount_t seq;
  40. struct rb_root tree[2];
  41. };
  42. /**
  43. * latch_tree_ops - operators to define the tree order
  44. * @less: used for insertion; provides the (partial) order between two elements.
  45. * @comp: used for lookups; provides the order between the search key and an element.
  46. *
  47. * The operators are related like:
  48. *
  49. * comp(a->key,b) < 0 := less(a,b)
  50. * comp(a->key,b) > 0 := less(b,a)
  51. * comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
  52. *
  53. * If these operators define a partial order on the elements we make no
  54. * guarantee on which of the elements matching the key is found. See
  55. * latch_tree_find().
  56. */
  57. struct latch_tree_ops {
  58. bool (*less)(struct latch_tree_node *a, struct latch_tree_node *b);
  59. int (*comp)(void *key, struct latch_tree_node *b);
  60. };
  61. static __always_inline struct latch_tree_node *
  62. __lt_from_rb(struct rb_node *node, int idx)
  63. {
  64. return container_of(node, struct latch_tree_node, node[idx]);
  65. }
  66. static __always_inline void
  67. __lt_insert(struct latch_tree_node *ltn, struct latch_tree_root *ltr, int idx,
  68. bool (*less)(struct latch_tree_node *a, struct latch_tree_node *b))
  69. {
  70. struct rb_root *root = &ltr->tree[idx];
  71. struct rb_node **link = &root->rb_node;
  72. struct rb_node *node = &ltn->node[idx];
  73. struct rb_node *parent = NULL;
  74. struct latch_tree_node *ltp;
  75. while (*link) {
  76. parent = *link;
  77. ltp = __lt_from_rb(parent, idx);
  78. if (less(ltn, ltp))
  79. link = &parent->rb_left;
  80. else
  81. link = &parent->rb_right;
  82. }
  83. rb_link_node_rcu(node, parent, link);
  84. rb_insert_color(node, root);
  85. }
  86. static __always_inline void
  87. __lt_erase(struct latch_tree_node *ltn, struct latch_tree_root *ltr, int idx)
  88. {
  89. rb_erase(&ltn->node[idx], &ltr->tree[idx]);
  90. }
  91. static __always_inline struct latch_tree_node *
  92. __lt_find(void *key, struct latch_tree_root *ltr, int idx,
  93. int (*comp)(void *key, struct latch_tree_node *node))
  94. {
  95. struct rb_node *node = rcu_dereference_raw(ltr->tree[idx].rb_node);
  96. struct latch_tree_node *ltn;
  97. int c;
  98. while (node) {
  99. ltn = __lt_from_rb(node, idx);
  100. c = comp(key, ltn);
  101. if (c < 0)
  102. node = rcu_dereference_raw(node->rb_left);
  103. else if (c > 0)
  104. node = rcu_dereference_raw(node->rb_right);
  105. else
  106. return ltn;
  107. }
  108. return NULL;
  109. }
  110. /**
  111. * latch_tree_insert() - insert @node into the trees @root
  112. * @node: nodes to insert
  113. * @root: trees to insert @node into
  114. * @ops: operators defining the node order
  115. *
  116. * It inserts @node into @root in an ordered fashion such that we can always
  117. * observe one complete tree. See the comment for raw_write_seqcount_latch().
  118. *
  119. * The inserts use rcu_assign_pointer() to publish the element such that the
  120. * tree structure is stored before we can observe the new @node.
  121. *
  122. * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
  123. * serialized.
  124. */
  125. static __always_inline void
  126. latch_tree_insert(struct latch_tree_node *node,
  127. struct latch_tree_root *root,
  128. const struct latch_tree_ops *ops)
  129. {
  130. raw_write_seqcount_latch(&root->seq);
  131. __lt_insert(node, root, 0, ops->less);
  132. raw_write_seqcount_latch(&root->seq);
  133. __lt_insert(node, root, 1, ops->less);
  134. }
  135. /**
  136. * latch_tree_erase() - removes @node from the trees @root
  137. * @node: nodes to remote
  138. * @root: trees to remove @node from
  139. * @ops: operators defining the node order
  140. *
  141. * Removes @node from the trees @root in an ordered fashion such that we can
  142. * always observe one complete tree. See the comment for
  143. * raw_write_seqcount_latch().
  144. *
  145. * It is assumed that @node will observe one RCU quiescent state before being
  146. * reused of freed.
  147. *
  148. * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
  149. * serialized.
  150. */
  151. static __always_inline void
  152. latch_tree_erase(struct latch_tree_node *node,
  153. struct latch_tree_root *root,
  154. const struct latch_tree_ops *ops)
  155. {
  156. raw_write_seqcount_latch(&root->seq);
  157. __lt_erase(node, root, 0);
  158. raw_write_seqcount_latch(&root->seq);
  159. __lt_erase(node, root, 1);
  160. }
  161. /**
  162. * latch_tree_find() - find the node matching @key in the trees @root
  163. * @key: search key
  164. * @root: trees to search for @key
  165. * @ops: operators defining the node order
  166. *
  167. * Does a lockless lookup in the trees @root for the node matching @key.
  168. *
  169. * It is assumed that this is called while holding the appropriate RCU read
  170. * side lock.
  171. *
  172. * If the operators define a partial order on the elements (there are multiple
  173. * elements which have the same key value) it is undefined which of these
  174. * elements will be found. Nor is it possible to iterate the tree to find
  175. * further elements with the same key value.
  176. *
  177. * Returns: a pointer to the node matching @key or NULL.
  178. */
  179. static __always_inline struct latch_tree_node *
  180. latch_tree_find(void *key, struct latch_tree_root *root,
  181. const struct latch_tree_ops *ops)
  182. {
  183. struct latch_tree_node *node;
  184. unsigned int seq;
  185. do {
  186. seq = raw_read_seqcount_latch(&root->seq);
  187. node = __lt_find(key, root, seq & 1, ops->comp);
  188. } while (read_seqcount_retry(&root->seq, seq));
  189. return node;
  190. }
  191. #endif /* RB_TREE_LATCH_H */