1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2015 Intel Corp., Peter Zijlstra <peterz@infradead.org>
7 * Since RB-trees have non-atomic modifications they're not immediately suited
8 * for RCU/lockless queries. Even though we made RB-tree lookups non-fatal for
9 * lockless lookups; we cannot guarantee they return a correct result.
11 * The simplest solution is a seqlock + RB-tree, this will allow lockless
12 * lookups; but has the constraint (inherent to the seqlock) that read sides
13 * cannot nest in write sides.
15 * If we need to allow unconditional lookups (say as required for NMI context
16 * usage) we need a more complex setup; this data structure provides this by
17 * employing the latch technique -- see @raw_write_seqcount_latch -- to
18 * implement a latched RB-tree which does allow for unconditional lookups by
19 * virtue of always having (at least) one stable copy of the tree.
21 * However, while we have the guarantee that there is at all times one stable
22 * copy, this does not guarantee an iteration will not observe modifications.
23 * What might have been a stable copy at the start of the iteration, need not
24 * remain so for the duration of the iteration.
26 * Therefore, this does require a lockless RB-tree iteration to be non-fatal;
27 * see the comment in lib/rbtree.c. Note however that we only require the first
28 * condition -- not seeing partial stores -- because the latch thing isolates
29 * us from loops. If we were to interrupt a modification the lookup would be
30 * pointed at the stable tree and complete while the modification was halted.
33 #ifndef RB_TREE_LATCH_H
34 #define RB_TREE_LATCH_H
36 #include <linux/rbtree.h>
37 #include <linux/seqlock.h>
38 #include <linux/rcupdate.h>
40 struct latch_tree_node {
41 struct rb_node node[2];
44 struct latch_tree_root {
46 struct rb_root tree[2];
50 * latch_tree_ops - operators to define the tree order
51 * @less: used for insertion; provides the (partial) order between two elements.
52 * @comp: used for lookups; provides the order between the search key and an element.
54 * The operators are related like:
56 * comp(a->key,b) < 0 := less(a,b)
57 * comp(a->key,b) > 0 := less(b,a)
58 * comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
60 * If these operators define a partial order on the elements we make no
61 * guarantee on which of the elements matching the key is found. See
64 struct latch_tree_ops {
65 bool (*less)(struct latch_tree_node *a, struct latch_tree_node *b);
66 int (*comp)(void *key, struct latch_tree_node *b);
69 static __always_inline struct latch_tree_node *
70 __lt_from_rb(struct rb_node *node, int idx)
72 return container_of(node, struct latch_tree_node, node[idx]);
75 static __always_inline void
76 __lt_insert(struct latch_tree_node *ltn, struct latch_tree_root *ltr, int idx,
77 bool (*less)(struct latch_tree_node *a, struct latch_tree_node *b))
79 struct rb_root *root = <r->tree[idx];
80 struct rb_node **link = &root->rb_node;
81 struct rb_node *node = <n->node[idx];
82 struct rb_node *parent = NULL;
83 struct latch_tree_node *ltp;
87 ltp = __lt_from_rb(parent, idx);
90 link = &parent->rb_left;
92 link = &parent->rb_right;
95 rb_link_node_rcu(node, parent, link);
96 rb_insert_color(node, root);
99 static __always_inline void
100 __lt_erase(struct latch_tree_node *ltn, struct latch_tree_root *ltr, int idx)
102 rb_erase(<n->node[idx], <r->tree[idx]);
105 static __always_inline struct latch_tree_node *
106 __lt_find(void *key, struct latch_tree_root *ltr, int idx,
107 int (*comp)(void *key, struct latch_tree_node *node))
109 struct rb_node *node = rcu_dereference_raw(ltr->tree[idx].rb_node);
110 struct latch_tree_node *ltn;
114 ltn = __lt_from_rb(node, idx);
118 node = rcu_dereference_raw(node->rb_left);
120 node = rcu_dereference_raw(node->rb_right);
129 * latch_tree_insert() - insert @node into the trees @root
130 * @node: nodes to insert
131 * @root: trees to insert @node into
132 * @ops: operators defining the node order
134 * It inserts @node into @root in an ordered fashion such that we can always
135 * observe one complete tree. See the comment for raw_write_seqcount_latch().
137 * The inserts use rcu_assign_pointer() to publish the element such that the
138 * tree structure is stored before we can observe the new @node.
140 * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
143 static __always_inline void
144 latch_tree_insert(struct latch_tree_node *node,
145 struct latch_tree_root *root,
146 const struct latch_tree_ops *ops)
148 raw_write_seqcount_latch(&root->seq);
149 __lt_insert(node, root, 0, ops->less);
150 raw_write_seqcount_latch(&root->seq);
151 __lt_insert(node, root, 1, ops->less);
155 * latch_tree_erase() - removes @node from the trees @root
156 * @node: nodes to remote
157 * @root: trees to remove @node from
158 * @ops: operators defining the node order
160 * Removes @node from the trees @root in an ordered fashion such that we can
161 * always observe one complete tree. See the comment for
162 * raw_write_seqcount_latch().
164 * It is assumed that @node will observe one RCU quiescent state before being
167 * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
170 static __always_inline void
171 latch_tree_erase(struct latch_tree_node *node,
172 struct latch_tree_root *root,
173 const struct latch_tree_ops *ops)
175 raw_write_seqcount_latch(&root->seq);
176 __lt_erase(node, root, 0);
177 raw_write_seqcount_latch(&root->seq);
178 __lt_erase(node, root, 1);
182 * latch_tree_find() - find the node matching @key in the trees @root
184 * @root: trees to search for @key
185 * @ops: operators defining the node order
187 * Does a lockless lookup in the trees @root for the node matching @key.
189 * It is assumed that this is called while holding the appropriate RCU read
192 * If the operators define a partial order on the elements (there are multiple
193 * elements which have the same key value) it is undefined which of these
194 * elements will be found. Nor is it possible to iterate the tree to find
195 * further elements with the same key value.
197 * Returns: a pointer to the node matching @key or NULL.
199 static __always_inline struct latch_tree_node *
200 latch_tree_find(void *key, struct latch_tree_root *root,
201 const struct latch_tree_ops *ops)
203 struct latch_tree_node *node;
207 seq = raw_read_seqcount_latch(&root->seq);
208 node = __lt_find(key, root, seq & 1, ops->comp);
209 } while (raw_read_seqcount_latch_retry(&root->seq, seq));
214 #endif /* RB_TREE_LATCH_H */