Skip to content

Commit 3aef2ca

Browse files
Davidlohr Buesoacmel
authored andcommitted
tools: Update rbtree implementation
There have been a number of changes in the kernel's rbrtee implementation, including loose lockless searching guarantees and rb_root_cached, which later patches will use as an optimization. Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Link: http://lkml.kernel.org/r/20181206191819.30182-2-dave@stgolabs.net Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 95420d3 commit 3aef2ca

3 files changed

Lines changed: 229 additions & 61 deletions

File tree

‎tools/include/linux/rbtree.h‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,28 @@ struct rb_root {
4343
struct rb_node *rb_node;
4444
};
4545

46+
/*
47+
* Leftmost-cached rbtrees.
48+
*
49+
* We do not cache the rightmost node based on footprint
50+
* size vs number of potential users that could benefit
51+
* from O(1) rb_last(). Just not worth it, users that want
52+
* this feature can always implement the logic explicitly.
53+
* Furthermore, users that want to cache both pointers may
54+
* find it a bit asymmetric, but that's ok.
55+
*/
56+
struct rb_root_cached {
57+
struct rb_root rb_root;
58+
struct rb_node *rb_leftmost;
59+
};
4660

4761
#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
4862

4963
#define RB_ROOT (struct rb_root) { NULL, }
64+
#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
5065
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
5166

52-
#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
67+
#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
5368

5469
/* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
5570
#define RB_EMPTY_NODE(node) \
@@ -68,13 +83,21 @@ extern struct rb_node *rb_prev(const struct rb_node *);
6883
extern struct rb_node *rb_first(const struct rb_root *);
6984
extern struct rb_node *rb_last(const struct rb_root *);
7085

86+
extern void rb_insert_color_cached(struct rb_node *,
87+
struct rb_root_cached *, bool);
88+
extern void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
89+
/* Same as rb_first(), but O(1) */
90+
#define rb_first_cached(root) (root)->rb_leftmost
91+
7192
/* Postorder iteration - always visit the parent after its children */
7293
extern struct rb_node *rb_first_postorder(const struct rb_root *);
7394
extern struct rb_node *rb_next_postorder(const struct rb_node *);
7495

7596
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
7697
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
7798
struct rb_root *root);
99+
extern void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
100+
struct rb_root_cached *root);
78101

79102
static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
80103
struct rb_node **rb_link)
@@ -90,12 +113,29 @@ static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
90113
____ptr ? rb_entry(____ptr, type, member) : NULL; \
91114
})
92115

93-
94-
/*
95-
* Handy for checking that we are not deleting an entry that is
96-
* already in a list, found in block/{blk-throttle,cfq-iosched}.c,
97-
* probably should be moved to lib/rbtree.c...
116+
/**
117+
* rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
118+
* given type allowing the backing memory of @pos to be invalidated
119+
*
120+
* @pos: the 'type *' to use as a loop cursor.
121+
* @n: another 'type *' to use as temporary storage
122+
* @root: 'rb_root *' of the rbtree.
123+
* @field: the name of the rb_node field within 'type'.
124+
*
125+
* rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
126+
* list_for_each_entry_safe() and allows the iteration to continue independent
127+
* of changes to @pos by the body of the loop.
128+
*
129+
* Note, however, that it cannot handle other modifications that re-order the
130+
* rbtree it is iterating over. This includes calling rb_erase() on @pos, as
131+
* rb_erase() may rebalance the tree, causing us to miss some nodes.
98132
*/
133+
#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
134+
for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
135+
pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
136+
typeof(*pos), field); 1; }); \
137+
pos = n)
138+
99139
static inline void rb_erase_init(struct rb_node *n, struct rb_root *root)
100140
{
101141
rb_erase(n, root);

‎tools/include/linux/rbtree_augmented.h‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ struct rb_augment_callbacks {
4444
void (*rotate)(struct rb_node *old, struct rb_node *new);
4545
};
4646

47-
extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
47+
extern void __rb_insert_augmented(struct rb_node *node,
48+
struct rb_root *root,
49+
bool newleft, struct rb_node **leftmost,
4850
void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
4951
/*
5052
* Fixup the rbtree and update the augmented information when rebalancing.
@@ -60,7 +62,16 @@ static inline void
6062
rb_insert_augmented(struct rb_node *node, struct rb_root *root,
6163
const struct rb_augment_callbacks *augment)
6264
{
63-
__rb_insert_augmented(node, root, augment->rotate);
65+
__rb_insert_augmented(node, root, false, NULL, augment->rotate);
66+
}
67+
68+
static inline void
69+
rb_insert_augmented_cached(struct rb_node *node,
70+
struct rb_root_cached *root, bool newleft,
71+
const struct rb_augment_callbacks *augment)
72+
{
73+
__rb_insert_augmented(node, &root->rb_root,
74+
newleft, &root->rb_leftmost, augment->rotate);
6475
}
6576

6677
#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
@@ -93,7 +104,9 @@ rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
93104
old->rbaugmented = rbcompute(old); \
94105
} \
95106
rbstatic const struct rb_augment_callbacks rbname = { \
96-
rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
107+
.propagate = rbname ## _propagate, \
108+
.copy = rbname ## _copy, \
109+
.rotate = rbname ## _rotate \
97110
};
98111

99112

@@ -126,24 +139,29 @@ __rb_change_child(struct rb_node *old, struct rb_node *new,
126139
{
127140
if (parent) {
128141
if (parent->rb_left == old)
129-
parent->rb_left = new;
142+
WRITE_ONCE(parent->rb_left, new);
130143
else
131-
parent->rb_right = new;
144+
WRITE_ONCE(parent->rb_right, new);
132145
} else
133-
root->rb_node = new;
146+
WRITE_ONCE(root->rb_node, new);
134147
}
135148

136149
extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
137150
void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
138151

139152
static __always_inline struct rb_node *
140153
__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
154+
struct rb_node **leftmost,
141155
const struct rb_augment_callbacks *augment)
142156
{
143-
struct rb_node *child = node->rb_right, *tmp = node->rb_left;
157+
struct rb_node *child = node->rb_right;
158+
struct rb_node *tmp = node->rb_left;
144159
struct rb_node *parent, *rebalance;
145160
unsigned long pc;
146161

162+
if (leftmost && node == *leftmost)
163+
*leftmost = rb_next(node);
164+
147165
if (!tmp) {
148166
/*
149167
* Case 1: node to erase has no more than 1 child (easy!)
@@ -170,6 +188,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
170188
tmp = parent;
171189
} else {
172190
struct rb_node *successor = child, *child2;
191+
173192
tmp = child->rb_left;
174193
if (!tmp) {
175194
/*
@@ -183,6 +202,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
183202
*/
184203
parent = successor;
185204
child2 = successor->rb_right;
205+
186206
augment->copy(node, successor);
187207
} else {
188208
/*
@@ -204,19 +224,23 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
204224
successor = tmp;
205225
tmp = tmp->rb_left;
206226
} while (tmp);
207-
parent->rb_left = child2 = successor->rb_right;
208-
successor->rb_right = child;
227+
child2 = successor->rb_right;
228+
WRITE_ONCE(parent->rb_left, child2);
229+
WRITE_ONCE(successor->rb_right, child);
209230
rb_set_parent(child, successor);
231+
210232
augment->copy(node, successor);
211233
augment->propagate(parent, successor);
212234
}
213235

214-
successor->rb_left = tmp = node->rb_left;
236+
tmp = node->rb_left;
237+
WRITE_ONCE(successor->rb_left, tmp);
215238
rb_set_parent(tmp, successor);
216239

217240
pc = node->__rb_parent_color;
218241
tmp = __rb_parent(pc);
219242
__rb_change_child(node, successor, tmp, root);
243+
220244
if (child2) {
221245
successor->__rb_parent_color = pc;
222246
rb_set_parent_color(child2, parent, RB_BLACK);
@@ -237,9 +261,21 @@ static __always_inline void
237261
rb_erase_augmented(struct rb_node *node, struct rb_root *root,
238262
const struct rb_augment_callbacks *augment)
239263
{
240-
struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
264+
struct rb_node *rebalance = __rb_erase_augmented(node, root,
265+
NULL, augment);
241266
if (rebalance)
242267
__rb_erase_color(rebalance, root, augment->rotate);
243268
}
244269

245-
#endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */
270+
static __always_inline void
271+
rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
272+
const struct rb_augment_callbacks *augment)
273+
{
274+
struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root,
275+
&root->rb_leftmost,
276+
augment);
277+
if (rebalance)
278+
__rb_erase_color(rebalance, &root->rb_root, augment->rotate);
279+
}
280+
281+
#endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */

0 commit comments

Comments
 (0)