linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use.
@ 2013-07-26 21:13 Cody P Schafer
  2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

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 it's 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.

Cody P Schafer (5):
  rbtree: add postorder iteration functions.
  rbtree: add rbtree_postorder_for_each_entry_safe() helper.
  rbtree_test: add test for postorder iteration.
  rbtree: allow tests to run as builtin
  mm/zswap: use postorder iteration when destroying rbtree

 include/linux/rbtree.h | 21 +++++++++++++++++++++
 lib/Kconfig.debug      |  2 +-
 lib/rbtree.c           | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/rbtree_test.c      | 12 ++++++++++++
 mm/zswap.c             | 15 ++-------------
 5 files changed, 76 insertions(+), 14 deletions(-)

-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/5] rbtree: add postorder iteration functions
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
@ 2013-07-26 21:13 ` Cody P Schafer
  2013-07-29 15:01   ` Seth Jennings
  2013-07-26 21:13 ` [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper Cody P Schafer
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

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@linux.vnet.ibm.com>
---
 include/linux/rbtree.h |  4 ++++
 lib/rbtree.c           | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index 0022c1b..2879e96 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -68,6 +68,10 @@ extern struct rb_node *rb_prev(const struct rb_node *);
 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 it's 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 --git a/lib/rbtree.c b/lib/rbtree.c
index c0e31fe..65f4eff 100644
--- a/lib/rbtree.c
+++ b/lib/rbtree.c
@@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
 	*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);
-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
  2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
@ 2013-07-26 21:13 ` Cody P Schafer
  2013-07-29 15:06   ` Seth Jennings
  2013-07-26 21:13 ` [PATCH 3/5] rbtree_test: add test for postorder iteration Cody P Schafer
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

Because deletion (of the entire tree) is a relatively common use of the
rbtree_postorder iteration, and because doing it safely means fiddling
with temporary storage, provide a helper to simplify postorder rbtree
iteration.

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
---
 include/linux/rbtree.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index 2879e96..64ab98b 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -85,4 +85,21 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
 	*rb_link = node;
 }
 
+/**
+ * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
+ * given type safe against removal of rb_node entry
+ *
+ * @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'.
+ */
+#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
+	for (pos = rb_entry(rb_first_postorder(root), typeof(*pos), field),\
+	      n = rb_entry(rb_next_postorder(&pos->field), \
+		      typeof(*pos), field); \
+	     &pos->field; \
+	     pos = n, \
+	      n = rb_entry(rb_next_postorder(&pos->field), typeof(*pos), field))
+
 #endif	/* _LINUX_RBTREE_H */
-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 3/5] rbtree_test: add test for postorder iteration
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
  2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
  2013-07-26 21:13 ` [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper Cody P Schafer
@ 2013-07-26 21:13 ` Cody P Schafer
  2013-07-26 21:13 ` [PATCH 4/5] rbtree: allow tests to run as builtin Cody P Schafer
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

Just check that we examine all nodes in the tree for the postorder iteration.

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
---
 lib/rbtree_test.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lib/rbtree_test.c b/lib/rbtree_test.c
index 122f02f..31dd4cc 100644
--- a/lib/rbtree_test.c
+++ b/lib/rbtree_test.c
@@ -114,6 +114,16 @@ static int black_path_count(struct rb_node *rb)
 	return count;
 }
 
+static void check_postorder(int nr_nodes)
+{
+	struct rb_node *rb;
+	int count = 0;
+	for (rb = rb_first_postorder(&root); rb; rb = rb_next_postorder(rb))
+		count++;
+
+	WARN_ON_ONCE(count != nr_nodes);
+}
+
 static void check(int nr_nodes)
 {
 	struct rb_node *rb;
@@ -136,6 +146,8 @@ static void check(int nr_nodes)
 
 	WARN_ON_ONCE(count != nr_nodes);
 	WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1);
+
+	check_postorder(nr_nodes);
 }
 
 static void check_augmented(int nr_nodes)
-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 4/5] rbtree: allow tests to run as builtin
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
                   ` (2 preceding siblings ...)
  2013-07-26 21:13 ` [PATCH 3/5] rbtree_test: add test for postorder iteration Cody P Schafer
@ 2013-07-26 21:13 ` Cody P Schafer
  2013-07-26 21:13 ` [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree Cody P Schafer
  2013-07-29 15:11 ` [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Seth Jennings
  5 siblings, 0 replies; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

No reason require rbtree test code to be a module, allow it to be
builtin (streamlines my development process)

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
---
 lib/Kconfig.debug | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1501aa5..606e3c8 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1442,7 +1442,7 @@ config BACKTRACE_SELF_TEST
 
 config RBTREE_TEST
 	tristate "Red-Black tree test"
-	depends on m && DEBUG_KERNEL
+	depends on DEBUG_KERNEL
 	help
 	  A benchmark measuring the performance of the rbtree library.
 	  Also includes rbtree invariant checks.
-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
                   ` (3 preceding siblings ...)
  2013-07-26 21:13 ` [PATCH 4/5] rbtree: allow tests to run as builtin Cody P Schafer
@ 2013-07-26 21:13 ` Cody P Schafer
  2013-07-29 15:08   ` Seth Jennings
  2013-07-29 15:11 ` [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Seth Jennings
  5 siblings, 1 reply; 12+ messages in thread
From: Cody P Schafer @ 2013-07-26 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Linux MM, David Woodhouse, Rik van Riel, Michel Lespinasse,
	Seth Jennings, Cody P Schafer

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
---
 mm/zswap.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index deda2b6..98d99c4 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -791,25 +791,14 @@ static void zswap_frontswap_invalidate_area(unsigned type)
 {
 	struct zswap_tree *tree = zswap_trees[type];
 	struct rb_node *node;
-	struct zswap_entry *entry;
+	struct zswap_entry *entry, *n;
 
 	if (!tree)
 		return;
 
 	/* walk the tree and free everything */
 	spin_lock(&tree->lock);
-	/*
-	 * TODO: Even though this code should not be executed because
-	 * the try_to_unuse() in swapoff should have emptied the tree,
-	 * it is very wasteful to rebalance the tree after every
-	 * removal when we are freeing the whole tree.
-	 *
-	 * If post-order traversal code is ever added to the rbtree
-	 * implementation, it should be used here.
-	 */
-	while ((node = rb_first(&tree->rbroot))) {
-		entry = rb_entry(node, struct zswap_entry, rbnode);
-		rb_erase(&entry->rbnode, &tree->rbroot);
+	rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) {
 		zbud_free(tree->pool, entry->handle);
 		zswap_entry_cache_free(entry);
 		atomic_dec(&zswap_stored_pages);
-- 
1.8.3.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/5] rbtree: add postorder iteration functions
  2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
@ 2013-07-29 15:01   ` Seth Jennings
  2013-07-29 17:32     ` Cody P Schafer
  0 siblings, 1 reply; 12+ messages in thread
From: Seth Jennings @ 2013-07-29 15:01 UTC (permalink / raw)
  To: Cody P Schafer
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On Fri, Jul 26, 2013 at 02:13:39PM -0700, Cody P Schafer wrote:
> 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@linux.vnet.ibm.com>
> ---
>  include/linux/rbtree.h |  4 ++++
>  lib/rbtree.c           | 40 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
> index 0022c1b..2879e96 100644
> --- a/include/linux/rbtree.h
> +++ b/include/linux/rbtree.h
> @@ -68,6 +68,10 @@ extern struct rb_node *rb_prev(const struct rb_node *);
>  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 it's children */

s/it's/its/

> +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 --git a/lib/rbtree.c b/lib/rbtree.c
> index c0e31fe..65f4eff 100644
> --- a/lib/rbtree.c
> +++ b/lib/rbtree.c
> @@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
>  	*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;

Assigning to an argument passed as const seems weird to me.  I would
think it shouldn't compile but it does.  I guess my understanding of
const is incomplete.

> +		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);

Again here.

Seth

> +
> +	/* 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);
> -- 
> 1.8.3.4
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper
  2013-07-26 21:13 ` [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper Cody P Schafer
@ 2013-07-29 15:06   ` Seth Jennings
  2013-07-29 17:41     ` Cody P Schafer
  0 siblings, 1 reply; 12+ messages in thread
From: Seth Jennings @ 2013-07-29 15:06 UTC (permalink / raw)
  To: Cody P Schafer
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On Fri, Jul 26, 2013 at 02:13:40PM -0700, Cody P Schafer wrote:
> Because deletion (of the entire tree) is a relatively common use of the
> rbtree_postorder iteration, and because doing it safely means fiddling
> with temporary storage, provide a helper to simplify postorder rbtree
> iteration.
> 
> Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
> ---
>  include/linux/rbtree.h | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
> index 2879e96..64ab98b 100644
> --- a/include/linux/rbtree.h
> +++ b/include/linux/rbtree.h
> @@ -85,4 +85,21 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
>  	*rb_link = node;
>  }
> 
> +/**
> + * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
> + * given type safe against removal of rb_node entry
> + *
> + * @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'.
> + */
> +#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
> +	for (pos = rb_entry(rb_first_postorder(root), typeof(*pos), field),\
> +	      n = rb_entry(rb_next_postorder(&pos->field), \
> +		      typeof(*pos), field); \
> +	     &pos->field; \
> +	     pos = n, \
> +	      n = rb_entry(rb_next_postorder(&pos->field), typeof(*pos), field))

One too many spaces.  Also mix of tabs and spaces is weird, but
checkpatch doesn't complain so...

Seth

> +
>  #endif	/* _LINUX_RBTREE_H */
> -- 
> 1.8.3.4
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree
  2013-07-26 21:13 ` [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree Cody P Schafer
@ 2013-07-29 15:08   ` Seth Jennings
  0 siblings, 0 replies; 12+ messages in thread
From: Seth Jennings @ 2013-07-29 15:08 UTC (permalink / raw)
  To: Cody P Schafer
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On Fri, Jul 26, 2013 at 02:13:43PM -0700, Cody P Schafer wrote:
> Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
> ---
>  mm/zswap.c | 15 ++-------------
>  1 file changed, 2 insertions(+), 13 deletions(-)
> 
> diff --git a/mm/zswap.c b/mm/zswap.c
> index deda2b6..98d99c4 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
> @@ -791,25 +791,14 @@ static void zswap_frontswap_invalidate_area(unsigned type)
>  {
>  	struct zswap_tree *tree = zswap_trees[type];
>  	struct rb_node *node;

Getting used variable warning on this now.  Just need to remove it.

Seth

> -	struct zswap_entry *entry;
> +	struct zswap_entry *entry, *n;
> 
>  	if (!tree)
>  		return;
> 
>  	/* walk the tree and free everything */
>  	spin_lock(&tree->lock);
> -	/*
> -	 * TODO: Even though this code should not be executed because
> -	 * the try_to_unuse() in swapoff should have emptied the tree,
> -	 * it is very wasteful to rebalance the tree after every
> -	 * removal when we are freeing the whole tree.
> -	 *
> -	 * If post-order traversal code is ever added to the rbtree
> -	 * implementation, it should be used here.
> -	 */
> -	while ((node = rb_first(&tree->rbroot))) {
> -		entry = rb_entry(node, struct zswap_entry, rbnode);
> -		rb_erase(&entry->rbnode, &tree->rbroot);
> +	rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) {
>  		zbud_free(tree->pool, entry->handle);
>  		zswap_entry_cache_free(entry);
>  		atomic_dec(&zswap_stored_pages);
> -- 
> 1.8.3.4
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use.
  2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
                   ` (4 preceding siblings ...)
  2013-07-26 21:13 ` [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree Cody P Schafer
@ 2013-07-29 15:11 ` Seth Jennings
  5 siblings, 0 replies; 12+ messages in thread
From: Seth Jennings @ 2013-07-29 15:11 UTC (permalink / raw)
  To: Cody P Schafer
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On Fri, Jul 26, 2013 at 02:13:38PM -0700, Cody P Schafer wrote:
> 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 it's most common usage, postorder iteration allows the

s/it's/its/

> 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.

Thanks for doing this Cody!  Other than the nits I've sent, it looks
good.  Whole set:

Reviewed-by: Seth Jennings <sjenning@linux.vnet.ibm.com>

> 
> Cody P Schafer (5):
>   rbtree: add postorder iteration functions.
>   rbtree: add rbtree_postorder_for_each_entry_safe() helper.
>   rbtree_test: add test for postorder iteration.
>   rbtree: allow tests to run as builtin
>   mm/zswap: use postorder iteration when destroying rbtree
> 
>  include/linux/rbtree.h | 21 +++++++++++++++++++++
>  lib/Kconfig.debug      |  2 +-
>  lib/rbtree.c           | 40 ++++++++++++++++++++++++++++++++++++++++
>  lib/rbtree_test.c      | 12 ++++++++++++
>  mm/zswap.c             | 15 ++-------------
>  5 files changed, 76 insertions(+), 14 deletions(-)
> 
> -- 
> 1.8.3.4
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/5] rbtree: add postorder iteration functions
  2013-07-29 15:01   ` Seth Jennings
@ 2013-07-29 17:32     ` Cody P Schafer
  0 siblings, 0 replies; 12+ messages in thread
From: Cody P Schafer @ 2013-07-29 17:32 UTC (permalink / raw)
  To: Seth Jennings
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On 07/29/2013 08:01 AM, Seth Jennings wrote:
> On Fri, Jul 26, 2013 at 02:13:39PM -0700, Cody P Schafer wrote:
>> diff --git a/lib/rbtree.c b/lib/rbtree.c
>> index c0e31fe..65f4eff 100644
>> --- a/lib/rbtree.c
>> +++ b/lib/rbtree.c
>> @@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
>>   	*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;
>
> Assigning to an argument passed as const seems weird to me.  I would
> think it shouldn't compile but it does.  I guess my understanding of
> const is incomplete.
>

Ya, that is due to const's binding:
	const struct rb_node *node1; // the thing pointed to is const
	const struct rb_node node2;  // node is const
	struct rb_node *const node3; // node is const
	const struct rb_node *const node4; // both node and the thing
					   // pointed too are const

And so ends up being perfectly legal (I use the first case listed here).

>> +		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);
>
> Again here.
>
> Seth
>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper
  2013-07-29 15:06   ` Seth Jennings
@ 2013-07-29 17:41     ` Cody P Schafer
  0 siblings, 0 replies; 12+ messages in thread
From: Cody P Schafer @ 2013-07-29 17:41 UTC (permalink / raw)
  To: Seth Jennings
  Cc: Andrew Morton, LKML, Linux MM, David Woodhouse, Rik van Riel,
	Michel Lespinasse

On 07/29/2013 08:06 AM, Seth Jennings wrote:
> On Fri, Jul 26, 2013 at 02:13:40PM -0700, Cody P Schafer wrote:
>> Because deletion (of the entire tree) is a relatively common use of the
>> rbtree_postorder iteration, and because doing it safely means fiddling
>> with temporary storage, provide a helper to simplify postorder rbtree
>> iteration.
>>
>> Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
>> ---
>>   include/linux/rbtree.h | 17 +++++++++++++++++
>>   1 file changed, 17 insertions(+)
>>
>> diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
>> index 2879e96..64ab98b 100644
>> --- a/include/linux/rbtree.h
>> +++ b/include/linux/rbtree.h
>> @@ -85,4 +85,21 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
>>   	*rb_link = node;
>>   }
>>
>> +/**
>> + * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
>> + * given type safe against removal of rb_node entry
>> + *
>> + * @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'.
>> + */
>> +#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
>> +	for (pos = rb_entry(rb_first_postorder(root), typeof(*pos), field),\
>> +	      n = rb_entry(rb_next_postorder(&pos->field), \
>> +		      typeof(*pos), field); \
>> +	     &pos->field; \
>> +	     pos = n, \
>> +	      n = rb_entry(rb_next_postorder(&pos->field), typeof(*pos), field))
>
> One too many spaces.  Also mix of tabs and spaces is weird, but
> checkpatch doesn't complain so...
>
> Seth

The extra space is to set off ';' vs ',' in the macro. And I did that 
instead of a tab to avoid wrapping. I've adjusted them (in the next 
version) to use the same style as list.h's list_for_each*() macros. 
Which results in more wrapping :( .

>
>> +
>>   #endif	/* _LINUX_RBTREE_H */
>> --
>> 1.8.3.4
>>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-07-30 15:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
2013-07-29 15:01   ` Seth Jennings
2013-07-29 17:32     ` Cody P Schafer
2013-07-26 21:13 ` [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper Cody P Schafer
2013-07-29 15:06   ` Seth Jennings
2013-07-29 17:41     ` Cody P Schafer
2013-07-26 21:13 ` [PATCH 3/5] rbtree_test: add test for postorder iteration Cody P Schafer
2013-07-26 21:13 ` [PATCH 4/5] rbtree: allow tests to run as builtin Cody P Schafer
2013-07-26 21:13 ` [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree Cody P Schafer
2013-07-29 15:08   ` Seth Jennings
2013-07-29 15:11 ` [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Seth Jennings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).