Skip to content

Commit d72da4a

Browse files
Peter Zijlstrarustyrussell
authored andcommitted
rbtree: Make lockless searches non-fatal
Change the insert and erase code such that lockless searches are non-fatal. In and of itself an rbtree cannot be correctly searched while in-modification, we can however provide weaker guarantees that will allow the rbtree to be used in conjunction with other techniques, such as latches; see 9b0fd80 ("seqcount: Add raw_write_seqcount_latch()"). For this to work we need the following guarantees from the rbtree code: 1) a lockless reader must not see partial stores, this would allow it to observe nodes that are invalid memory. 2) there must not be (temporary) loops in the tree structure in the modifier's program order, this would cause a lookup which interrupts the modifier to get stuck indefinitely. For 1) we must use WRITE_ONCE() for all updates to the tree structure; in particular this patch only does rb_{left,right} as those are the only element required for simple searches. It generates slightly worse code, probably because volatile. But in pointer chasing heavy code a few instructions more should not matter. For 2) I have carefully audited the code and drawn every intermediate link state and not found a loop. Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: David Woodhouse <David.Woodhouse@intel.com> Cc: Rik van Riel <riel@redhat.com> Reviewed-by: Michel Lespinasse <walken@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 0be964b commit d72da4a

3 files changed

Lines changed: 81 additions & 32 deletions

File tree

‎include/linux/rbtree.h‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <linux/kernel.h>
3333
#include <linux/stddef.h>
34+
#include <linux/rcupdate.h>
3435

3536
struct rb_node {
3637
unsigned long __rb_parent_color;
@@ -73,18 +74,27 @@ extern struct rb_node *rb_first_postorder(const struct rb_root *);
7374
extern struct rb_node *rb_next_postorder(const struct rb_node *);
7475

7576
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
76-
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
77+
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
7778
struct rb_root *root);
7879

79-
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
80-
struct rb_node ** rb_link)
80+
static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
81+
struct rb_node **rb_link)
8182
{
8283
node->__rb_parent_color = (unsigned long)parent;
8384
node->rb_left = node->rb_right = NULL;
8485

8586
*rb_link = node;
8687
}
8788

89+
static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
90+
struct rb_node **rb_link)
91+
{
92+
node->__rb_parent_color = (unsigned long)parent;
93+
node->rb_left = node->rb_right = NULL;
94+
95+
rcu_assign_pointer(*rb_link, node);
96+
}
97+
8898
#define rb_entry_safe(ptr, type, member) \
8999
({ typeof(ptr) ____ptr = (ptr); \
90100
____ptr ? rb_entry(____ptr, type, member) : NULL; \

‎include/linux/rbtree_augmented.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ __rb_change_child(struct rb_node *old, struct rb_node *new,
123123
{
124124
if (parent) {
125125
if (parent->rb_left == old)
126-
parent->rb_left = new;
126+
WRITE_ONCE(parent->rb_left, new);
127127
else
128-
parent->rb_right = new;
128+
WRITE_ONCE(parent->rb_right, new);
129129
} else
130-
root->rb_node = new;
130+
WRITE_ONCE(root->rb_node, new);
131131
}
132132

133133
extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
@@ -137,7 +137,8 @@ static __always_inline struct rb_node *
137137
__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
138138
const struct rb_augment_callbacks *augment)
139139
{
140-
struct rb_node *child = node->rb_right, *tmp = node->rb_left;
140+
struct rb_node *child = node->rb_right;
141+
struct rb_node *tmp = node->rb_left;
141142
struct rb_node *parent, *rebalance;
142143
unsigned long pc;
143144

@@ -167,6 +168,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
167168
tmp = parent;
168169
} else {
169170
struct rb_node *successor = child, *child2;
171+
170172
tmp = child->rb_left;
171173
if (!tmp) {
172174
/*
@@ -180,6 +182,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
180182
*/
181183
parent = successor;
182184
child2 = successor->rb_right;
185+
183186
augment->copy(node, successor);
184187
} else {
185188
/*
@@ -201,19 +204,23 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
201204
successor = tmp;
202205
tmp = tmp->rb_left;
203206
} while (tmp);
204-
parent->rb_left = child2 = successor->rb_right;
205-
successor->rb_right = child;
207+
child2 = successor->rb_right;
208+
WRITE_ONCE(parent->rb_left, child2);
209+
WRITE_ONCE(successor->rb_right, child);
206210
rb_set_parent(child, successor);
211+
207212
augment->copy(node, successor);
208213
augment->propagate(parent, successor);
209214
}
210215

211-
successor->rb_left = tmp = node->rb_left;
216+
tmp = node->rb_left;
217+
WRITE_ONCE(successor->rb_left, tmp);
212218
rb_set_parent(tmp, successor);
213219

214220
pc = node->__rb_parent_color;
215221
tmp = __rb_parent(pc);
216222
__rb_change_child(node, successor, tmp, root);
223+
217224
if (child2) {
218225
successor->__rb_parent_color = pc;
219226
rb_set_parent_color(child2, parent, RB_BLACK);

‎lib/rbtree.c‎

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@
4444
* parentheses and have some accompanying text comment.
4545
*/
4646

47+
/*
48+
* Notes on lockless lookups:
49+
*
50+
* All stores to the tree structure (rb_left and rb_right) must be done using
51+
* WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
52+
* tree structure as seen in program order.
53+
*
54+
* These two requirements will allow lockless iteration of the tree -- not
55+
* correct iteration mind you, tree rotations are not atomic so a lookup might
56+
* miss entire subtrees.
57+
*
58+
* But they do guarantee that any such traversal will only see valid elements
59+
* and that it will indeed complete -- does not get stuck in a loop.
60+
*
61+
* It also guarantees that if the lookup returns an element it is the 'correct'
62+
* one. But not returning an element does _NOT_ mean it's not present.
63+
*
64+
* NOTE:
65+
*
66+
* Stores to __rb_parent_color are not important for simple lookups so those
67+
* are left undone as of now. Nor did I check for loops involving parent
68+
* pointers.
69+
*/
70+
4771
static inline void rb_set_black(struct rb_node *rb)
4872
{
4973
rb->__rb_parent_color |= RB_BLACK;
@@ -129,8 +153,9 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
129153
* This still leaves us in violation of 4), the
130154
* continuation into Case 3 will fix that.
131155
*/
132-
parent->rb_right = tmp = node->rb_left;
133-
node->rb_left = parent;
156+
tmp = node->rb_left;
157+
WRITE_ONCE(parent->rb_right, tmp);
158+
WRITE_ONCE(node->rb_left, parent);
134159
if (tmp)
135160
rb_set_parent_color(tmp, parent,
136161
RB_BLACK);
@@ -149,8 +174,8 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
149174
* / \
150175
* n U
151176
*/
152-
gparent->rb_left = tmp; /* == parent->rb_right */
153-
parent->rb_right = gparent;
177+
WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
178+
WRITE_ONCE(parent->rb_right, gparent);
154179
if (tmp)
155180
rb_set_parent_color(tmp, gparent, RB_BLACK);
156181
__rb_rotate_set_parents(gparent, parent, root, RB_RED);
@@ -171,8 +196,9 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
171196
tmp = parent->rb_left;
172197
if (node == tmp) {
173198
/* Case 2 - right rotate at parent */
174-
parent->rb_left = tmp = node->rb_right;
175-
node->rb_right = parent;
199+
tmp = node->rb_right;
200+
WRITE_ONCE(parent->rb_left, tmp);
201+
WRITE_ONCE(node->rb_right, parent);
176202
if (tmp)
177203
rb_set_parent_color(tmp, parent,
178204
RB_BLACK);
@@ -183,8 +209,8 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
183209
}
184210

185211
/* Case 3 - left rotate at gparent */
186-
gparent->rb_right = tmp; /* == parent->rb_left */
187-
parent->rb_left = gparent;
212+
WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
213+
WRITE_ONCE(parent->rb_left, gparent);
188214
if (tmp)
189215
rb_set_parent_color(tmp, gparent, RB_BLACK);
190216
__rb_rotate_set_parents(gparent, parent, root, RB_RED);
@@ -224,8 +250,9 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
224250
* / \ / \
225251
* Sl Sr N Sl
226252
*/
227-
parent->rb_right = tmp1 = sibling->rb_left;
228-
sibling->rb_left = parent;
253+
tmp1 = sibling->rb_left;
254+
WRITE_ONCE(parent->rb_right, tmp1);
255+
WRITE_ONCE(sibling->rb_left, parent);
229256
rb_set_parent_color(tmp1, parent, RB_BLACK);
230257
__rb_rotate_set_parents(parent, sibling, root,
231258
RB_RED);
@@ -275,9 +302,10 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
275302
* \
276303
* Sr
277304
*/
278-
sibling->rb_left = tmp1 = tmp2->rb_right;
279-
tmp2->rb_right = sibling;
280-
parent->rb_right = tmp2;
305+
tmp1 = tmp2->rb_right;
306+
WRITE_ONCE(sibling->rb_left, tmp1);
307+
WRITE_ONCE(tmp2->rb_right, sibling);
308+
WRITE_ONCE(parent->rb_right, tmp2);
281309
if (tmp1)
282310
rb_set_parent_color(tmp1, sibling,
283311
RB_BLACK);
@@ -297,8 +325,9 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
297325
* / \ / \
298326
* (sl) sr N (sl)
299327
*/
300-
parent->rb_right = tmp2 = sibling->rb_left;
301-
sibling->rb_left = parent;
328+
tmp2 = sibling->rb_left;
329+
WRITE_ONCE(parent->rb_right, tmp2);
330+
WRITE_ONCE(sibling->rb_left, parent);
302331
rb_set_parent_color(tmp1, sibling, RB_BLACK);
303332
if (tmp2)
304333
rb_set_parent(tmp2, parent);
@@ -310,8 +339,9 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
310339
sibling = parent->rb_left;
311340
if (rb_is_red(sibling)) {
312341
/* Case 1 - right rotate at parent */
313-
parent->rb_left = tmp1 = sibling->rb_right;
314-
sibling->rb_right = parent;
342+
tmp1 = sibling->rb_right;
343+
WRITE_ONCE(parent->rb_left, tmp1);
344+
WRITE_ONCE(sibling->rb_right, parent);
315345
rb_set_parent_color(tmp1, parent, RB_BLACK);
316346
__rb_rotate_set_parents(parent, sibling, root,
317347
RB_RED);
@@ -336,9 +366,10 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
336366
break;
337367
}
338368
/* Case 3 - right rotate at sibling */
339-
sibling->rb_right = tmp1 = tmp2->rb_left;
340-
tmp2->rb_left = sibling;
341-
parent->rb_left = tmp2;
369+
tmp1 = tmp2->rb_left;
370+
WRITE_ONCE(sibling->rb_right, tmp1);
371+
WRITE_ONCE(tmp2->rb_left, sibling);
372+
WRITE_ONCE(parent->rb_left, tmp2);
342373
if (tmp1)
343374
rb_set_parent_color(tmp1, sibling,
344375
RB_BLACK);
@@ -347,8 +378,9 @@ ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
347378
sibling = tmp2;
348379
}
349380
/* Case 4 - left rotate at parent + color flips */
350-
parent->rb_left = tmp2 = sibling->rb_right;
351-
sibling->rb_right = parent;
381+
tmp2 = sibling->rb_right;
382+
WRITE_ONCE(parent->rb_left, tmp2);
383+
WRITE_ONCE(sibling->rb_right, parent);
352384
rb_set_parent_color(tmp1, sibling, RB_BLACK);
353385
if (tmp2)
354386
rb_set_parent(tmp2, parent);

0 commit comments

Comments
 (0)