Skip to content

Commit c1adf20

Browse files
committed
Introduce rb_replace_node_rcu()
Implement an RCU-safe variant of rb_replace_node() and rearrange rb_replace_node() to do things in the same order. Signed-off-by: David Howells <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]>
1 parent 1291e9d commit c1adf20

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

include/linux/rbtree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern struct rb_node *rb_next_postorder(const struct rb_node *);
7676
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
7777
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
7878
struct rb_root *root);
79+
extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
80+
struct rb_root *root);
7981

8082
static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
8183
struct rb_node **rb_link)

include/linux/rbtree_augmented.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ __rb_change_child(struct rb_node *old, struct rb_node *new,
130130
WRITE_ONCE(root->rb_node, new);
131131
}
132132

133+
static inline void
134+
__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
135+
struct rb_node *parent, struct rb_root *root)
136+
{
137+
if (parent) {
138+
if (parent->rb_left == old)
139+
rcu_assign_pointer(parent->rb_left, new);
140+
else
141+
rcu_assign_pointer(parent->rb_right, new);
142+
} else
143+
rcu_assign_pointer(root->rb_node, new);
144+
}
145+
133146
extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
134147
void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
135148

lib/rbtree.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,17 +539,39 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
539539
{
540540
struct rb_node *parent = rb_parent(victim);
541541

542+
/* Copy the pointers/colour from the victim to the replacement */
543+
*new = *victim;
544+
542545
/* Set the surrounding nodes to point to the replacement */
543-
__rb_change_child(victim, new, parent, root);
544546
if (victim->rb_left)
545547
rb_set_parent(victim->rb_left, new);
546548
if (victim->rb_right)
547549
rb_set_parent(victim->rb_right, new);
550+
__rb_change_child(victim, new, parent, root);
551+
}
552+
EXPORT_SYMBOL(rb_replace_node);
553+
554+
void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
555+
struct rb_root *root)
556+
{
557+
struct rb_node *parent = rb_parent(victim);
548558

549559
/* Copy the pointers/colour from the victim to the replacement */
550560
*new = *victim;
561+
562+
/* Set the surrounding nodes to point to the replacement */
563+
if (victim->rb_left)
564+
rb_set_parent(victim->rb_left, new);
565+
if (victim->rb_right)
566+
rb_set_parent(victim->rb_right, new);
567+
568+
/* Set the parent's pointer to the new node last after an RCU barrier
569+
* so that the pointers onwards are seen to be set correctly when doing
570+
* an RCU walk over the tree.
571+
*/
572+
__rb_change_child_rcu(victim, new, parent, root);
551573
}
552-
EXPORT_SYMBOL(rb_replace_node);
574+
EXPORT_SYMBOL(rb_replace_node_rcu);
553575

554576
static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
555577
{

0 commit comments

Comments
 (0)