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>
39 struct latch_tree_node
{
40 struct rb_node node
[2];
43 struct latch_tree_root
{
45 struct rb_root tree
[2];
49 * latch_tree_ops - operators to define the tree order
50 * @less: used for insertion; provides the (partial) order between two elements.
51 * @comp: used for lookups; provides the order between the search key and an element.
53 * The operators are related like:
55 * comp(a->key,b) < 0 := less(a,b)
56 * comp(a->key,b) > 0 := less(b,a)
57 * comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
59 * If these operators define a partial order on the elements we make no
60 * guarantee on which of the elements matching the key is found. See
63 struct latch_tree_ops
{
64 bool (*less
)(struct latch_tree_node
*a
, struct latch_tree_node
*b
);
65 int (*comp
)(void *key
, struct latch_tree_node
*b
);
68 static __always_inline
struct latch_tree_node
*
69 __lt_from_rb(struct rb_node
*node
, int idx
)
71 return container_of(node
, struct latch_tree_node
, node
[idx
]);
74 static __always_inline
void
75 __lt_insert(struct latch_tree_node
*ltn
, struct latch_tree_root
*ltr
, int idx
,
76 bool (*less
)(struct latch_tree_node
*a
, struct latch_tree_node
*b
))
78 struct rb_root
*root
= <r
->tree
[idx
];
79 struct rb_node
**link
= &root
->rb_node
;
80 struct rb_node
*node
= <n
->node
[idx
];
81 struct rb_node
*parent
= NULL
;
82 struct latch_tree_node
*ltp
;
86 ltp
= __lt_from_rb(parent
, idx
);
89 link
= &parent
->rb_left
;
91 link
= &parent
->rb_right
;
94 rb_link_node_rcu(node
, parent
, link
);
95 rb_insert_color(node
, root
);
98 static __always_inline
void
99 __lt_erase(struct latch_tree_node
*ltn
, struct latch_tree_root
*ltr
, int idx
)
101 rb_erase(<n
->node
[idx
], <r
->tree
[idx
]);
104 static __always_inline
struct latch_tree_node
*
105 __lt_find(void *key
, struct latch_tree_root
*ltr
, int idx
,
106 int (*comp
)(void *key
, struct latch_tree_node
*node
))
108 struct rb_node
*node
= rcu_dereference_raw(ltr
->tree
[idx
].rb_node
);
109 struct latch_tree_node
*ltn
;
113 ltn
= __lt_from_rb(node
, idx
);
117 node
= rcu_dereference_raw(node
->rb_left
);
119 node
= rcu_dereference_raw(node
->rb_right
);
128 * latch_tree_insert() - insert @node into the trees @root
129 * @node: nodes to insert
130 * @root: trees to insert @node into
131 * @ops: operators defining the node order
133 * It inserts @node into @root in an ordered fashion such that we can always
134 * observe one complete tree. See the comment for raw_write_seqcount_latch().
136 * The inserts use rcu_assign_pointer() to publish the element such that the
137 * tree structure is stored before we can observe the new @node.
139 * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
142 static __always_inline
void
143 latch_tree_insert(struct latch_tree_node
*node
,
144 struct latch_tree_root
*root
,
145 const struct latch_tree_ops
*ops
)
147 raw_write_seqcount_latch(&root
->seq
);
148 __lt_insert(node
, root
, 0, ops
->less
);
149 raw_write_seqcount_latch(&root
->seq
);
150 __lt_insert(node
, root
, 1, ops
->less
);
154 * latch_tree_erase() - removes @node from the trees @root
155 * @node: nodes to remote
156 * @root: trees to remove @node from
157 * @ops: operators defining the node order
159 * Removes @node from the trees @root in an ordered fashion such that we can
160 * always observe one complete tree. See the comment for
161 * raw_write_seqcount_latch().
163 * It is assumed that @node will observe one RCU quiescent state before being
166 * All modifications (latch_tree_insert, latch_tree_remove) are assumed to be
169 static __always_inline
void
170 latch_tree_erase(struct latch_tree_node
*node
,
171 struct latch_tree_root
*root
,
172 const struct latch_tree_ops
*ops
)
174 raw_write_seqcount_latch(&root
->seq
);
175 __lt_erase(node
, root
, 0);
176 raw_write_seqcount_latch(&root
->seq
);
177 __lt_erase(node
, root
, 1);
181 * latch_tree_find() - find the node matching @key in the trees @root
183 * @root: trees to search for @key
184 * @ops: operators defining the node order
186 * Does a lockless lookup in the trees @root for the node matching @key.
188 * It is assumed that this is called while holding the appropriate RCU read
191 * If the operators define a partial order on the elements (there are multiple
192 * elements which have the same key value) it is undefined which of these
193 * elements will be found. Nor is it possible to iterate the tree to find
194 * further elements with the same key value.
196 * Returns: a pointer to the node matching @key or NULL.
198 static __always_inline
struct latch_tree_node
*
199 latch_tree_find(void *key
, struct latch_tree_root
*root
,
200 const struct latch_tree_ops
*ops
)
202 struct latch_tree_node
*node
;
206 seq
= raw_read_seqcount_latch(&root
->seq
);
207 node
= __lt_find(key
, root
, seq
& 1, ops
->comp
);
208 } while (read_seqcount_retry(&root
->seq
, seq
));
213 #endif /* RB_TREE_LATCH_H */