Subject: + rbtree-add-postorder-iteration-functions.patch added to -mm tree To: cody@xxxxxxxxxxxxxxxxxx,David.Woodhouse@xxxxxxxxx,riel@xxxxxxxxxx,sjenning@xxxxxxxxxxxxxxxxxx,walken@xxxxxxxxxx From: akpm@xxxxxxxxxxxxxxxxxxxx Date: Mon, 29 Jul 2013 15:43:55 -0700 The patch titled Subject: rbtree: add postorder iteration functions has been added to the -mm tree. Its filename is rbtree-add-postorder-iteration-functions.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/rbtree-add-postorder-iteration-functions.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/rbtree-add-postorder-iteration-functions.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/SubmitChecklist when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Cody P Schafer <cody@xxxxxxxxxxxxxxxxxx> Subject: rbtree: add postorder iteration functions Postorder iteration yields all of a node's children prior to yielding the node itself, and this particular implementation also avoids examining the leaf links in a node after that node has been yielded. In what I expect will be its most common usage, postorder iteration allows the deletion of every node in an rbtree without modifying the rbtree nodes (no _requirement_ that they be nulled) while avoiding referencing child nodes after they have been "deleted" (most commonly, freed). I have only updated zswap to use this functionality at this point, but numerous bits of code (most notably in the filesystem drivers) use a hand rolled postorder iteration that NULLs child links as it traverses the tree. Each of those instances could be replaced with this common implementation. 1 & 2 add rbtree postorder iteration functions. 3 adds testing of the iteration to the rbtree runtime tests 4 allows building the rbtree runtime tests as builtins 5 updates zswap. This patch: Add postorder iteration functions for rbtree. These are useful for safely freeing an entire rbtree without modifying the tree at all. Signed-off-by: Cody P Schafer <cody@xxxxxxxxxxxxxxxxxx> Reviewed-by: Seth Jennings <sjenning@xxxxxxxxxxxxxxxxxx> Cc: David Woodhouse <David.Woodhouse@xxxxxxxxx> Cc: Rik van Riel <riel@xxxxxxxxxx> Cc: Michel Lespinasse <walken@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- include/linux/rbtree.h | 4 +++ lib/rbtree.c | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff -puN include/linux/rbtree.h~rbtree-add-postorder-iteration-functions include/linux/rbtree.h --- a/include/linux/rbtree.h~rbtree-add-postorder-iteration-functions +++ a/include/linux/rbtree.h @@ -68,6 +68,10 @@ extern struct rb_node *rb_prev(const str extern struct rb_node *rb_first(const struct rb_root *); extern struct rb_node *rb_last(const struct rb_root *); +/* Postorder iteration - always visit the parent after its children */ +extern struct rb_node *rb_first_postorder(const struct rb_root *); +extern struct rb_node *rb_next_postorder(const struct rb_node *); + /* Fast replacement of a single node without remove/rebalance/add/rebalance */ extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, struct rb_root *root); diff -puN lib/rbtree.c~rbtree-add-postorder-iteration-functions lib/rbtree.c --- a/lib/rbtree.c~rbtree-add-postorder-iteration-functions +++ a/lib/rbtree.c @@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *vic *new = *victim; } EXPORT_SYMBOL(rb_replace_node); + +static struct rb_node *rb_left_deepest_node(const struct rb_node *node) +{ + for (;;) { + if (node->rb_left) + node = node->rb_left; + else if (node->rb_right) + node = node->rb_right; + else + return (struct rb_node *)node; + } +} + +struct rb_node *rb_next_postorder(const struct rb_node *node) +{ + const struct rb_node *parent; + if (!node) + return NULL; + parent = rb_parent(node); + + /* If we're sitting on node, we've already seen our children */ + if (parent && node == parent->rb_left && parent->rb_right) { + /* If we are the parent's left node, go to the parent's right + * node then all the way down to the left */ + return rb_left_deepest_node(parent->rb_right); + } else + /* Otherwise we are the parent's right node, and the parent + * should be next */ + return (struct rb_node *)parent; +} +EXPORT_SYMBOL(rb_next_postorder); + +struct rb_node *rb_first_postorder(const struct rb_root *root) +{ + if (!root->rb_node) + return NULL; + + return rb_left_deepest_node(root->rb_node); +} +EXPORT_SYMBOL(rb_first_postorder); _ Patches currently in -mm which might be from cody@xxxxxxxxxxxxxxxxxx are mm-page_alloc-restructure-free-page-stealing-code-and-fix-a-bug.patch mm-page_alloc-restructure-free-page-stealing-code-and-fix-a-bug-fix.patch mm-fix-the-value-of-fallback_migratetype-in-alloc_extfrag-tracepoint.patch rbtree-add-postorder-iteration-functions.patch rbtree-add-rbtree_postorder_for_each_entry_safe-helper.patch rbtree_test-add-test-for-postorder-iteration.patch rbtree-allow-tests-to-run-as-builtin.patch mm-zswap-use-postorder-iteration-when-destroying-rbtree.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html