Skip to contentSkip to conversation

libtracecmd rbtree: Fix deletion of leaf nodes

PATCH In review Steven Rostedt 2 participants 2 messages Jan 10, 2024 – Jan 11, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracecmd rbtree: Fix deletion of leaf nodes

From: "Steven Rostedt (Google)" <[email protected]>

In the rbtree deletion, the deletion had the logic:

	if (!node->left || !node->right)
		y = node;
	else
		y = next_node(node);

	if (y->left)
		x = y->left;
	else
		x = y->right;

	x->parent = y->parent;

Where if the node had both right and left, the y returned would not have children. This is fine, but the x would be NULL. In that case, do not update the parent of x. But instead, skip over and continue the work. As x->parent would be the same as y->parent, instead of checking the x->parent (where x could be NULL), check y->parent, which is already known to be the same as x->parent if x is not NULL.

Also add another check_tree() at the end of the deletion routine.

Fixes 5274db32a ("libtracecmd: Use an rbtree for mapping of cache pages")

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=218357

---
 lib/trace-cmd/trace-rbtree.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
lib/trace-cmd/trace-rbtree.c +5 -3
index 24beabec..90671819 100644
@@ -314,7 +314,7 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
 	struct trace_rbtree_node *x, *y;
 	bool do_fixup = false;
 
-	if (!node->left && !node->right) {
+	if (!node->left && !node->right && !node->parent) {
 		tree->node = NULL;
 		goto out;
 	}
@@ -329,9 +329,10 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
 	else
 		x = y->right;
 
-	x->parent = y->parent;
+	if (x)
+		x->parent = y->parent;
 
-	if (!x->parent) {
+	if (!y->parent) {
 		tree->node = x;
 	} else {
 		if (is_left(y))
@@ -367,6 +368,7 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
  out:
 	node->parent = node->left = node->right = NULL;
 	tree->nr_nodes--;
+	check_tree(tree);
 }
 
 __hidden struct trace_rbtree_node *trace_rbtree_next(struct trace_rbtree *tree,
Signature · 1 line
2.42.0
Tested-by on v1 1/1
#1 lore raw

Thanks for the fix,

Quoted 67 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

In the rbtree deletion, the deletion had the logic:

	if (!node->left || !node->right)
		y = node;
	else
		y = next_node(node);

	if (y->left)
		x = y->left;
	else
		x = y->right;

	x->parent = y->parent;

Where if the node had both right and left, the y returned would not have children. This is fine, but the x would be NULL. In that case, do not update the parent of x. But instead, skip over and continue the work. As x->parent would be the same as y->parent, instead of checking the x->parent (where x could be NULL), check y->parent, which is already known to be the same as x->parent if x is not NULL.

Also add another check_tree() at the end of the deletion routine.

Fixes 5274db32a ("libtracecmd: Use an rbtree for mapping of cache pages")

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=218357

---
  lib/trace-cmd/trace-rbtree.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
lib/trace-cmd/trace-rbtree.c +5 -3
index 24beabec..90671819 100644
@@ -314,7 +314,7 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
  	struct trace_rbtree_node *x, *y;
  	bool do_fixup = false;
 
-	if (!node->left && !node->right) {
+	if (!node->left && !node->right && !node->parent) {
  		tree->node = NULL;
  		goto out;
  	}
@@ -329,9 +329,10 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
  	else
  		x = y->right;
 
-	x->parent = y->parent;
+	if (x)
+		x->parent = y->parent;
 
-	if (!x->parent) {
+	if (!y->parent) {
  		tree->node = x;
  	} else {
  		if (is_left(y))
@@ -367,6 +368,7 @@ void trace_rbtree_delete(struct trace_rbtree *tree, struct trace_rbtree_node *no
   out:
  	node->parent = node->left = node->right = NULL;
  	tree->nr_nodes--;
+	check_tree(tree);
  }
 
  __hidden struct trace_rbtree_node *trace_rbtree_next(struct trace_rbtree *tree,