From 8e79d40e8f546e719b4c884c43cef5624a4d1579 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 12 May 2022 13:40:09 +0200 Subject: [PATCH] btrfs-progs: kernel-lib: sync include/rtree.h Minor fixups to comments. Signed-off-by: David Sterba --- kernel-lib/rbtree.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/kernel-lib/rbtree.h b/kernel-lib/rbtree.h index 87b67125..508edee0 100644 --- a/kernel-lib/rbtree.h +++ b/kernel-lib/rbtree.h @@ -19,11 +19,11 @@ linux/include/linux/rbtree.h To use rbtrees you'll have to implement your own insert and search cores. - This will avoid us to use callbacks and to drop dramatically performances. + This will avoid us to use callbacks and to drop drammatically performances. I know it's not the cleaner way, but in C (not in C++) to get performances and genericity... - See Documentation/rbtree.txt for documentation and samples. + See Documentation/core-api/rbtree.rst for documentation and samples. */ #ifndef _LINUX_RBTREE_H @@ -47,9 +47,9 @@ extern "C" { #define rb_entry(ptr, type, member) container_of(ptr, type, member) -#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) +#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL) -/* 'empty' nodes are nodes that are known not to be inserted in an rtbree */ +/* 'empty' nodes are nodes that are known not to be inserted in an rbtree */ #define RB_EMPTY_NODE(node) \ ((node)->__rb_parent_color == (unsigned long)(node)) #define RB_CLEAR_NODE(node) \ @@ -74,8 +74,8 @@ extern struct rb_node *rb_next_postorder(const struct rb_node *); extern void rb_replace_node(struct rb_node *victim, struct rb_node *new_node, struct rb_root *root); -static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, - struct rb_node ** rb_link) +static inline void rb_link_node(struct rb_node *node, struct rb_node *parent, + struct rb_node **rb_link) { node->__rb_parent_color = (unsigned long)parent; node->rb_left = node->rb_right = NULL; @@ -89,13 +89,21 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, }) /** - * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of - * given type safe against removal of rb_node entry + * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of + * given type allowing the backing memory of @pos to be invalidated * * @pos: the 'type *' to use as a loop cursor. * @n: another 'type *' to use as temporary storage * @root: 'rb_root *' of the rbtree. * @field: the name of the rb_node field within 'type'. + * + * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as + * list_for_each_entry_safe() and allows the iteration to continue independent + * of changes to @pos by the body of the loop. + * + * Note, however, that it cannot handle other modifications that re-order the + * rbtree it is iterating over. This includes calling rb_erase() on @pos, as + * rb_erase() may rebalance the tree, causing us to miss some nodes. */ #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \ for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \