summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCody P Schafer <dev@codyps.com>2015-11-06 19:31:28 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-11-06 20:50:42 -0500
commit8de1ee7ebfb4979c6444e81273e12e7a972c367d (patch)
treea1d45a54168a77a814537a50cc29cc4fdcf35615
parent90224350eaaf8b8043b19c393048f732bc2e4120 (diff)
rbtree: clarify documentation of rbtree_postorder_for_each_entry_safe()
I noticed that commit a20135ffbc44 ("writeback: don't drain bdi_writeback_congested on bdi destruction") added a usage of rbtree_postorder_for_each_entry_safe() in mm/backing-dev.c which appears to try to rb_erase() elements from an rbtree while iterating over it using rbtree_postorder_for_each_entry_safe(). Doing this will cause random nodes to be missed by the iteration because rb_erase() may rebalance the tree, changing the ordering that we're trying to iterate over. The previous documentation for rbtree_postorder_for_each_entry_safe() wasn't clear that this wasn't allowed, it was taken from the docs for list_for_each_entry_safe(), where erasing isn't a problem due to list_del() not reordering. Explicitly warn developers about this potential pit-fall. Note that I haven't fixed the actual issue that (it appears) the commit referenced above introduced (not familiar enough with that code). In general (and in this case), the patterns to follow are: - switch to rb_first() + rb_erase(), don't use rbtree_postorder_for_each_entry_safe(). - keep the postorder iteration and don't rb_erase() at all. Instead just clear the fields of rb_node & cgwb_congested_tree as required by other users of those structures. [akpm@linux-foundation.org: tweak comments] Signed-off-by: Cody P Schafer <dev@codyps.com> Cc: John de la Garza <john@jjdev.com> Cc: Michel Lespinasse <walken@google.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--include/linux/rbtree.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index 830c4992088d..a5aa7ae671f4 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -101,13 +101,21 @@ static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent
101 }) 101 })
102 102
103/** 103/**
104 * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of 104 * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
105 * given type safe against removal of rb_node entry 105 * given type allowing the backing memory of @pos to be invalidated
106 * 106 *
107 * @pos: the 'type *' to use as a loop cursor. 107 * @pos: the 'type *' to use as a loop cursor.
108 * @n: another 'type *' to use as temporary storage 108 * @n: another 'type *' to use as temporary storage
109 * @root: 'rb_root *' of the rbtree. 109 * @root: 'rb_root *' of the rbtree.
110 * @field: the name of the rb_node field within 'type'. 110 * @field: the name of the rb_node field within 'type'.
111 *
112 * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
113 * list_for_each_entry_safe() and allows the iteration to continue independent
114 * of changes to @pos by the body of the loop.
115 *
116 * Note, however, that it cannot handle other modifications that re-order the
117 * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
118 * rb_erase() may rebalance the tree, causing us to miss some nodes.
111 */ 119 */
112#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \ 120#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
113 for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \ 121 for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \