Skip to contentSkip to conversation

Btrfs: Simplify the logic in alloc_extent_buffer() for existing extent buffer case

PATCH In review Chandra Seetharaman 2 participants 4 messages Oct 4, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: Simplify the logic in alloc_extent_buffer() for existing extent buffer case

alloc_extent_buffer() uses radix_tree_lookup() when radix_tree_insert() fails with EEXIST. That part of the code is very similar to the code in find_extent_buffer(). This patch replaces radix_tree_lookup() and surrounding code in alloc_extent_buffer() with find_extent_buffer().

While at it, this patch also changes the other usage of radix_tree_lookup() in alloc_extent_buffer() with find_extent_buffer() to reduce redundancy.

fs/btrfs/extent_io.c +26 -37
index c09a40d..50345fb 100644
@@ -4451,6 +4451,23 @@ static void mark_extent_buffer_accessed(struct extent_buffer *eb)
 	}
 }
 
+struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
+					 u64 start, unsigned long len)
+{
+	struct extent_buffer *eb;
+
+	rcu_read_lock();
+	eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
+	if (eb && atomic_inc_not_zero(&eb->refs)) {
+		rcu_read_unlock();
+		mark_extent_buffer_accessed(eb);
+		return eb;
+	}
+	rcu_read_unlock();
+
+	return NULL;
+}
+
 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
 					  u64 start, unsigned long len)
 {
@@ -4464,14 +4481,10 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
 	int uptodate = 1;
 	int ret;
 
-	rcu_read_lock();
-	eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
-	if (eb && atomic_inc_not_zero(&eb->refs)) {
-		rcu_read_unlock();
-		mark_extent_buffer_accessed(eb);
+
+	eb = find_extent_buffer(tree, start, len);
+	if (eb)
 		return eb;
-	}
-	rcu_read_unlock();
 
 	eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
 	if (!eb)
@@ -4530,24 +4543,17 @@ again:
 
 	spin_lock(&tree->buffer_lock);
 	ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
+	spin_unlock(&tree->buffer_lock);
+	radix_tree_preload_end();
 	if (ret == -EEXIST) {
-		exists = radix_tree_lookup(&tree->buffer,
-						start >> PAGE_CACHE_SHIFT);
-		if (!atomic_inc_not_zero(&exists->refs)) {
-			spin_unlock(&tree->buffer_lock);
-			radix_tree_preload_end();
-			exists = NULL;
+		exists = find_extent_buffer(tree, start, len);
+		if (exists)
+			goto free_eb;
+		else
 			goto again;
-		}
-		spin_unlock(&tree->buffer_lock);
-		radix_tree_preload_end();
-		mark_extent_buffer_accessed(exists);
-		goto free_eb;
 	}
 	/* add one reference for the tree */
 	check_buffer_tree_ref(eb);
-	spin_unlock(&tree->buffer_lock);
-	radix_tree_preload_end();
 
 	/*
 	 * there is a race where release page may have
@@ -4578,23 +4584,6 @@ free_eb:
 	return exists;
 }
 
-struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
-					 u64 start, unsigned long len)
-{
-	struct extent_buffer *eb;
-
-	rcu_read_lock();
-	eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
-	if (eb && atomic_inc_not_zero(&eb->refs)) {
-		rcu_read_unlock();
-		mark_extent_buffer_accessed(eb);
-		return eb;
-	}
-	rcu_read_unlock();
-
-	return NULL;
-}
-
 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
 {
 	struct extent_buffer *eb =
#1 lore raw
Quoted 16 lines · Chandra Seetharaman

alloc_extent_buffer() uses radix_tree_lookup() when radix_tree_insert() fails with EEXIST. That part of the code is very similar to the code in find_extent_buffer(). This patch replaces radix_tree_lookup() and surrounding code in alloc_extent_buffer() with find_extent_buffer().

While at it, this patch also changes the other usage of radix_tree_lookup() in alloc_extent_buffer() with find_extent_buffer() to reduce redundancy.

fs/btrfs/extent_io.c +0 -0
index c09a40d..50345fb 100644

fs/btrfs/extent_io.c static void mark_extent_buffer_accessed(struct extent_buffer *eb) · line 4455

Quoted 5 lines
fs/btrfs/extent_io.c +2 -0
@@ -4451,6 +4451,23 @@ static void mark_extent_buffer_accessed(struct extent_buffer *eb)
 	}
 }
 
+struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
+					 u64 start, unsigned long len)

len isn't used.

Quoted 4 lines
fs/btrfs/extent_io.c +0 -0
@@ -4530,24 +4543,17 @@ again:
 
 	spin_lock(&tree->buffer_lock);
 	ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);

fs/btrfs/extent_io.c again: · line 4549

Quoted 10 lines
fs/btrfs/extent_io.c +3 -6
+	spin_unlock(&tree->buffer_lock);
+	radix_tree_preload_end();
 	if (ret == -EEXIST) {
-		exists = radix_tree_lookup(&tree->buffer,
-						start >> PAGE_CACHE_SHIFT);
-		if (!atomic_inc_not_zero(&exists->refs)) {
-			spin_unlock(&tree->buffer_lock);
-			radix_tree_preload_end();
-			exists = NULL;
+		exists = find_extent_buffer(tree, start, len);

Is it safe to do the lookup under rcu instead of under the spinlock? The commit message should mention the difference.

- z

#2 lore raw

On Fri, 2013-10-04 at 14:38 -0700, Zach Brown wrote:

Thanks for the review Zach.

Quoted 20 lines · Chandra Seetharaman
Quoted 19 lines

alloc_extent_buffer() uses radix_tree_lookup() when radix_tree_insert() fails with EEXIST. That part of the code is very similar to the code in find_extent_buffer(). This patch replaces radix_tree_lookup() and surrounding code in alloc_extent_buffer() with find_extent_buffer().

While at it, this patch also changes the other usage of radix_tree_lookup() in alloc_extent_buffer() with find_extent_buffer() to reduce redundancy.

fs/btrfs/extent_io.c +0 -0
index c09a40d..50345fb 100644
@@ -4451,6 +4451,23 @@ static void mark_extent_buffer_accessed(struct extent_buffer *eb)
 	}
 }
 
Quoted 4 lines

fs/btrfs/extent_io.c static void mark_extent_buffer_accessed(struct extent_buffer *eb) · line 4455

Quoted 2 lines
fs/btrfs/extent_io.c +2 -0
+struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
+					 u64 start, unsigned long len)

len isn't used.

Thought about removing the unused argument. But didn't know all the history behind why it was there in the first place. So, didn't bother it.

You think I should remove it.

Quoted 17 lines
Quoted 5 lines
fs/btrfs/extent_io.c +0 -0
@@ -4530,24 +4543,17 @@ again:
 
 	spin_lock(&tree->buffer_lock);
 	ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);

fs/btrfs/extent_io.c again: · line 4549

Quoted 10 lines
fs/btrfs/extent_io.c +3 -6
+	spin_unlock(&tree->buffer_lock);
+	radix_tree_preload_end();
 	if (ret == -EEXIST) {
-		exists = radix_tree_lookup(&tree->buffer,
-						start >> PAGE_CACHE_SHIFT);
-		if (!atomic_inc_not_zero(&exists->refs)) {
-			spin_unlock(&tree->buffer_lock);
-			radix_tree_preload_end();
-			exists = NULL;
+		exists = find_extent_buffer(tree, start, len);

Is it safe to do the lookup under rcu instead of under the spinlock?

Yes. it is fine.

eb->refs is what is used to make sure the buffer remains in the tree. eb will be removed from the tree only if eb->refs is zero. find_extent_buffer() will return NULL if refs is already zero.

Besides, prior to this patch, there are other two instances of radix_tree_lookup() (one in find_extent_buffer() and another in alloc_extent_buffer()) which uses only rcu protection.

Quoted 1 line

The commit message should mention the difference.

How does this commit message sound:
---------------
alloc_extent_buffer() uses radix_tree_lookup() when radix_tree_insert() fails with EEXIST. That part of the code is very similar to the code in find_extent_buffer(). This patch replaces radix_tree_lookup() and surrounding code in alloc_extent_buffer() with find_extent_buffer().

Note that radix_tree_lookup() does not need to be protected by tree->buffer_lock. It is protected by eb->refs.

While at it, this patch also changes the other usage of radix_tree_lookup() in alloc_extent_buffer() with find_extent_buffer() to reduce redundancy.
---------------

Quoted 3 lines

- z

Reviewed-by
#3 lore raw
Quoted 10 lines
Quoted 4 lines

fs/btrfs/extent_io.c static void mark_extent_buffer_accessed(struct extent_buffer *eb) · line 4455

Quoted 2 lines
fs/btrfs/extent_io.c +2 -0
+struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
+					 u64 start, unsigned long len)

len isn't used.

Thought about removing the unused argument. But didn't know all the history behind why it was there in the first place. So, didn't bother it.

You think I should remove it.

Yeah, I would remove it. Leave the tiny job of adding it to whoever needs it in the future. Until then we don't need the noise.

Quoted 1 line

How does this commit message sound:

Yup, seems fine to me.

- z