All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@linux.intel.com>
To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@linux.intel.com>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	Konstantin Khlebnikov <koct9i@gmail.com>,
	Kirill Shutemov <kirill.shutemov@linux.intel.com>,
	Jan Kara <jack@suse.com>, Neil Brown <neilb@suse.de>,
	Ross Zwisler <ross.zwisler@linux.intel.com>
Subject: [PATCH v2 16/29] radix-tree: Fix several shrinking bugs with multiorder entries
Date: Thu, 14 Apr 2016 10:16:37 -0400	[thread overview]
Message-ID: <1460643410-30196-17-git-send-email-willy@linux.intel.com> (raw)
In-Reply-To: <1460643410-30196-1-git-send-email-willy@linux.intel.com>

Setting the indirect bit on the user data entry used to be unambiguous
because the tree walking code knew not to expect internal nodes in the
last level of the tree.  Multiorder entries can appear at any level of
the tree, and a leaf with the indirect bit set is indistinguishable from
a pointer to a node.

Introduce a special entry (RADIX_TREE_RETRY) which is neither a valid
user entry, nor a valid pointer to a node.  The radix_tree_deref_retry()
function continues to work the same way, but tree walking code can
distinguish it from a pointer to a node.

Also fix the condition for setting slot->parent to NULL; it does not
matter what height the tree is, it only matters whether slot is an
indirect pointer.  Move this code above the comment which is referring
to the assignment to root->rnode.

Also fix the condition for preventing the tree from shrinking to a single
entry if it's a multiorder entry.

Add a test-case to the test suite that checks that the tree goes back
down to its original height after an item is inserted & deleted from a
higher index in the tree.

Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 lib/radix-tree.c                      | 23 +++++++++++----------
 tools/testing/radix-tree/multiorder.c | 39 +++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index f13ddbb..a1ba417 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -80,6 +80,8 @@ static inline void *indirect_to_ptr(void *ptr)
 	return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
 }
 
+#define RADIX_TREE_RETRY	ptr_to_indirect(NULL)
+
 #ifdef CONFIG_RADIX_TREE_MULTIORDER
 /* Sibling slots point directly to another slot in the same node */
 static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
@@ -1443,6 +1445,14 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		slot = to_free->slots[0];
 		if (!slot)
 			break;
+		if (!radix_tree_is_indirect_ptr(slot) && (root->height > 1))
+			break;
+
+		if (radix_tree_is_indirect_ptr(slot)) {
+			slot = indirect_to_ptr(slot);
+			slot->parent = NULL;
+			slot = ptr_to_indirect(slot);
+		}
 
 		/*
 		 * We don't need rcu_assign_pointer(), since we are simply
@@ -1451,14 +1461,6 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		 * (to_free->slots[0]), it will be safe to dereference the new
 		 * one (root->rnode) as far as dependent read barriers go.
 		 */
-		if (root->height > 1) {
-			if (!radix_tree_is_indirect_ptr(slot))
-				break;
-
-			slot = indirect_to_ptr(slot);
-			slot->parent = NULL;
-			slot = ptr_to_indirect(slot);
-		}
 		root->rnode = slot;
 		root->height--;
 
@@ -1480,9 +1482,8 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		 * also results in a stale slot). So tag the slot as indirect
 		 * to force callers to retry.
 		 */
-		if (root->height == 0)
-			*((unsigned long *)&to_free->slots[0]) |=
-						RADIX_TREE_INDIRECT_PTR;
+		if (!radix_tree_is_indirect_ptr(slot))
+			to_free->slots[0] = RADIX_TREE_RETRY;
 
 		radix_tree_node_free(to_free);
 	}
diff --git a/tools/testing/radix-tree/multiorder.c b/tools/testing/radix-tree/multiorder.c
index cfe718c..71f34a0 100644
--- a/tools/testing/radix-tree/multiorder.c
+++ b/tools/testing/radix-tree/multiorder.c
@@ -46,6 +46,41 @@ static void multiorder_check(unsigned long index, int order)
 		item_check_absent(&tree, i);
 }
 
+static void multiorder_shrink(unsigned long index, int order)
+{
+	unsigned long i;
+	unsigned long max = 1 << order;
+	RADIX_TREE(tree, GFP_KERNEL);
+	struct radix_tree_node *node;
+
+	printf("Multiorder shrink index %ld, order %d\n", index, order);
+
+	assert(item_insert_order(&tree, 0, order) == 0);
+
+	node = tree.rnode;
+
+	assert(item_insert(&tree, index) == 0);
+	assert(node != tree.rnode);
+
+	assert(item_delete(&tree, index) != 0);
+	assert(node == tree.rnode);
+
+	for (i = 0; i < max; i++) {
+		struct item *item = item_lookup(&tree, i);
+		assert(item != 0);
+		assert(item->index == 0);
+	}
+	for (i = max; i < 2*max; i++)
+		item_check_absent(&tree, i);
+
+	if (!item_delete(&tree, 0)) {
+		printf("failed to delete index %ld (order %d)\n", index, order);		abort();
+	}
+
+	for (i = 0; i < 2*max; i++)
+		item_check_absent(&tree, i);
+}
+
 void multiorder_checks(void)
 {
 	int i;
@@ -55,4 +90,8 @@ void multiorder_checks(void)
 		multiorder_check(0, i);
 		multiorder_check((1UL << i) + 1, i);
 	}
+
+	for (i = 0; i < 15; i++)
+		multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
+
 }
-- 
2.8.0.rc3

WARNING: multiple messages have this Message-ID (diff)
From: Matthew Wilcox <willy@linux.intel.com>
To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@linux.intel.com>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	Konstantin Khlebnikov <koct9i@gmail.com>,
	Kirill Shutemov <kirill.shutemov@linux.intel.com>,
	Jan Kara <jack@suse.com>, Neil Brown <neilb@suse.de>,
	Ross Zwisler <ross.zwisler@linux.intel.com>
Subject: [PATCH v2 16/29] radix-tree: Fix several shrinking bugs with multiorder entries
Date: Thu, 14 Apr 2016 10:16:37 -0400	[thread overview]
Message-ID: <1460643410-30196-17-git-send-email-willy@linux.intel.com> (raw)
In-Reply-To: <1460643410-30196-1-git-send-email-willy@linux.intel.com>

Setting the indirect bit on the user data entry used to be unambiguous
because the tree walking code knew not to expect internal nodes in the
last level of the tree.  Multiorder entries can appear at any level of
the tree, and a leaf with the indirect bit set is indistinguishable from
a pointer to a node.

Introduce a special entry (RADIX_TREE_RETRY) which is neither a valid
user entry, nor a valid pointer to a node.  The radix_tree_deref_retry()
function continues to work the same way, but tree walking code can
distinguish it from a pointer to a node.

Also fix the condition for setting slot->parent to NULL; it does not
matter what height the tree is, it only matters whether slot is an
indirect pointer.  Move this code above the comment which is referring
to the assignment to root->rnode.

Also fix the condition for preventing the tree from shrinking to a single
entry if it's a multiorder entry.

Add a test-case to the test suite that checks that the tree goes back
down to its original height after an item is inserted & deleted from a
higher index in the tree.

Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 lib/radix-tree.c                      | 23 +++++++++++----------
 tools/testing/radix-tree/multiorder.c | 39 +++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index f13ddbb..a1ba417 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -80,6 +80,8 @@ static inline void *indirect_to_ptr(void *ptr)
 	return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
 }
 
+#define RADIX_TREE_RETRY	ptr_to_indirect(NULL)
+
 #ifdef CONFIG_RADIX_TREE_MULTIORDER
 /* Sibling slots point directly to another slot in the same node */
 static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
@@ -1443,6 +1445,14 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		slot = to_free->slots[0];
 		if (!slot)
 			break;
+		if (!radix_tree_is_indirect_ptr(slot) && (root->height > 1))
+			break;
+
+		if (radix_tree_is_indirect_ptr(slot)) {
+			slot = indirect_to_ptr(slot);
+			slot->parent = NULL;
+			slot = ptr_to_indirect(slot);
+		}
 
 		/*
 		 * We don't need rcu_assign_pointer(), since we are simply
@@ -1451,14 +1461,6 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		 * (to_free->slots[0]), it will be safe to dereference the new
 		 * one (root->rnode) as far as dependent read barriers go.
 		 */
-		if (root->height > 1) {
-			if (!radix_tree_is_indirect_ptr(slot))
-				break;
-
-			slot = indirect_to_ptr(slot);
-			slot->parent = NULL;
-			slot = ptr_to_indirect(slot);
-		}
 		root->rnode = slot;
 		root->height--;
 
@@ -1480,9 +1482,8 @@ static inline void radix_tree_shrink(struct radix_tree_root *root)
 		 * also results in a stale slot). So tag the slot as indirect
 		 * to force callers to retry.
 		 */
-		if (root->height == 0)
-			*((unsigned long *)&to_free->slots[0]) |=
-						RADIX_TREE_INDIRECT_PTR;
+		if (!radix_tree_is_indirect_ptr(slot))
+			to_free->slots[0] = RADIX_TREE_RETRY;
 
 		radix_tree_node_free(to_free);
 	}
diff --git a/tools/testing/radix-tree/multiorder.c b/tools/testing/radix-tree/multiorder.c
index cfe718c..71f34a0 100644
--- a/tools/testing/radix-tree/multiorder.c
+++ b/tools/testing/radix-tree/multiorder.c
@@ -46,6 +46,41 @@ static void multiorder_check(unsigned long index, int order)
 		item_check_absent(&tree, i);
 }
 
+static void multiorder_shrink(unsigned long index, int order)
+{
+	unsigned long i;
+	unsigned long max = 1 << order;
+	RADIX_TREE(tree, GFP_KERNEL);
+	struct radix_tree_node *node;
+
+	printf("Multiorder shrink index %ld, order %d\n", index, order);
+
+	assert(item_insert_order(&tree, 0, order) == 0);
+
+	node = tree.rnode;
+
+	assert(item_insert(&tree, index) == 0);
+	assert(node != tree.rnode);
+
+	assert(item_delete(&tree, index) != 0);
+	assert(node == tree.rnode);
+
+	for (i = 0; i < max; i++) {
+		struct item *item = item_lookup(&tree, i);
+		assert(item != 0);
+		assert(item->index == 0);
+	}
+	for (i = max; i < 2*max; i++)
+		item_check_absent(&tree, i);
+
+	if (!item_delete(&tree, 0)) {
+		printf("failed to delete index %ld (order %d)\n", index, order);		abort();
+	}
+
+	for (i = 0; i < 2*max; i++)
+		item_check_absent(&tree, i);
+}
+
 void multiorder_checks(void)
 {
 	int i;
@@ -55,4 +90,8 @@ void multiorder_checks(void)
 		multiorder_check(0, i);
 		multiorder_check((1UL << i) + 1, i);
 	}
+
+	for (i = 0; i < 15; i++)
+		multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
+
 }
-- 
2.8.0.rc3

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

  parent reply	other threads:[~2016-04-14 14:29 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-14 14:16 [PATCH v2 00/29] Radix tree multiorder fixes Matthew Wilcox
2016-04-14 14:16 ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 01/29] radix-tree: Introduce radix_tree_empty Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-05-03 14:24   ` Jan Kara
2016-05-03 14:24     ` Jan Kara
2016-04-14 14:16 ` [PATCH v2 02/29] radix tree test suite: Fix build Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 03/29] radix tree test suite: Add tests for radix_tree_locate_item() Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 04/29] radix tree test suite: Allow testing other fan-out values Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 05/29] radix tree test suite: keep regression test runs short Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 06/29] radix tree test suite: rebuild when headers change Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 07/29] radix-tree: remove unused looping macros Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 08/29] Introduce CONFIG_RADIX_TREE_MULTIORDER Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 09/29] radix-tree: Add missing sibling entry functionality Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 10/29] radix-tree: Fix sibling entry insertion Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 11/29] radix-tree: Fix deleting a multi-order entry through an alias Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 12/29] radix-tree: Remove restriction on multi-order entries Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 13/29] radix-tree: Introduce radix_tree_load_root() Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 14/29] radix-tree: Fix extending the tree for multi-order entries at offset 0 Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 15/29] radix tree test suite: Start adding multiorder tests Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` Matthew Wilcox [this message]
2016-04-14 14:16   ` [PATCH v2 16/29] radix-tree: Fix several shrinking bugs with multiorder entries Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 17/29] radix-tree: Rewrite __radix_tree_lookup Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 18/29] radix-tree: Fix multiorder BUG_ON in radix_tree_insert Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 19/29] radix-tree: add support for multi-order iterating Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 20/29] radix tree test suite: multi-order iteration test Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 21/29] radix-tree: Rewrite radix_tree_tag_set Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 22/29] radix-tree: Rewrite radix_tree_tag_clear Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 23/29] radix-tree: Rewrite radix_tree_tag_get Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 24/29] radix-tree test suite: add multi-order tag test Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 25/29] radix-tree: Fix radix_tree_create for sibling entries Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 26/29] radix-tree: Rewrite radix_tree_locate_item Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 27/29] radix-tree: Fix radix_tree_range_tag_if_tagged() for multiorder entries Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 28/29] radix-tree: Fix radix_tree_dump() for multi-order entries Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox
2016-04-14 14:16 ` [PATCH v2 29/29] radix-tree: Add copyright statements Matthew Wilcox
2016-04-14 14:16   ` Matthew Wilcox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1460643410-30196-17-git-send-email-willy@linux.intel.com \
    --to=willy@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=koct9i@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=neilb@suse.de \
    --cc=ross.zwisler@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.