Skip to contentSkip to conversation

Btrfs: convert refs and pages_reading to ints

PATCH In review Josef Bacik 2 participants 3 messages Mar 9, 2012 – Mar 13, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: convert refs and pages_reading to ints

I need to be able to safely deal with refs in my next patch, so convert refs and pages_reading to ints and introduce an eb_lock spinlock so I can use this to safely manipulate the refs count when marking eb's as stale. Thanks,

---
 fs/btrfs/backref.c           |    4 +-
 fs/btrfs/disk-io.c           |    8 +++++-
 fs/btrfs/extent_io.c         |   49 ++++++++++++++++++++++-------------------
 fs/btrfs/extent_io.h         |   22 ++++++++++++++++--
 include/trace/events/btrfs.h |    2 +-
 5 files changed, 54 insertions(+), 31 deletions(-)
fs/btrfs/backref.c +2 -2
index b9a8432..04cf24d 100644
@@ -904,7 +904,7 @@ static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
 		eb = path->nodes[0];
 		/* make sure we can use eb after releasing the path */
 		if (eb != eb_in)
-			atomic_inc(&eb->refs);
+			extent_buffer_get(eb);
 		btrfs_release_path(path);
 
 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
@@ -1251,7 +1251,7 @@ static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
 		slot = path->slots[0];
 		eb = path->nodes[0];
 		/* make sure we can use eb after releasing the path */
-		atomic_inc(&eb->refs);
+		extent_buffer_get(eb);
 		btrfs_release_path(path);
 
 		item = btrfs_item_nr(eb, slot);
fs/btrfs/disk-io.c +6 -2
index 6f97129..b8f2284 100644
@@ -566,7 +566,9 @@ static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
 	tree = &BTRFS_I(page->mapping->host)->io_tree;
 	eb = (struct extent_buffer *)page->private;
 
-	reads_done = atomic_dec_and_test(&eb->pages_reading);
+	spin_lock(&eb->eb_lock);
+	reads_done = (--eb->pages_reading == 0);
+	spin_unlock(&eb->eb_lock);
 	if (!reads_done)
 		goto err;
 
@@ -3391,7 +3393,9 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
 			if (eb) {
 				ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY,
 							 &eb->bflags);
-				atomic_set(&eb->refs, 1);
+				spin_lock(&eb->eb_lock);
+				eb->refs = 1;
+				spin_unlock(&eb->eb_lock);
 			}
 			if (PageWriteback(page))
 				end_page_writeback(page);
fs/btrfs/extent_io.c +26 -23

137 lines not on this page · raw message

fs/btrfs/extent_io.h +19 -3
index f030a2d..d11e872 100644
@@ -128,8 +128,9 @@ struct extent_buffer {
 	unsigned long map_len;
 	unsigned long bflags;
 	struct extent_io_tree *tree;
-	atomic_t refs;
-	atomic_t pages_reading;
+	spinlock_t eb_lock;
+	int refs;
+	int pages_reading;
 	struct list_head leak_list;
 	struct rcu_head rcu_head;
 	pid_t lock_owner;
@@ -272,7 +273,22 @@ struct page *extent_buffer_page(struct extent_buffer *eb, unsigned long i);
 
 static inline void extent_buffer_get(struct extent_buffer *eb)
 {
-	atomic_inc(&eb->refs);
+	spin_lock(&eb->eb_lock);
+	eb->refs++;
+	spin_unlock(&eb->eb_lock);
+}
+
+static inline int extent_buffer_inc_not_zero(struct extent_buffer *eb)
+{
+	int ret = 0;
+
+	spin_lock(&eb->eb_lock);
+	if (eb->refs) {
+		eb->refs++;
+		ret = 1;
+	}
+	spin_unlock(&eb->eb_lock);
+	return ret;
 }
 
 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
include/trace/events/btrfs.h +1 -1
index 84f3001..b4b04e7 100644
@@ -618,7 +618,7 @@ TRACE_EVENT(btrfs_cow_block,
 	TP_fast_assign(
 		__entry->root_objectid	= root->root_key.objectid;
 		__entry->buf_start	= buf->start;
-		__entry->refs		= atomic_read(&buf->refs);
+		__entry->refs		= buf->refs;
 		__entry->cow_start	= cow->start;
 		__entry->buf_level	= btrfs_header_level(buf);
 		__entry->cow_level	= btrfs_header_level(cow);
Signature · 1 line
1.7.5.2
#1 lore raw

Hi Josef,

Quoted 1 line · Josef Bacik

I need to be able to safely deal with refs in my next patch, so convert refs and

Did I miss your next patch?

Quoted 2 lines

pages_reading to ints and introduce an eb_lock spinlock so I can use this to safely manipulate the refs count when marking eb's as stale. Thanks,

I don't see what makes this version safer, are you synchronizing eb->refs with eb->pages_reading? This would be strange, because eb->pages_reading sounds like it could never be != 0 when eb->refs goes down to 0. Could you extend your description a bit, please?

fs/btrfs/extent_io.h struct extent_buffer { · line 131

Quoted 11 lines
fs/btrfs/extent_io.h +1 -2
index f030a2d..d11e872 100644
@@ -128,8 +128,9 @@ struct extent_buffer {
 	unsigned long map_len;
 	unsigned long bflags;
 	struct extent_io_tree *tree;
-	atomic_t refs;
-	atomic_t pages_reading;
+	spinlock_t eb_lock;

If you need that one, then please use another name for this. We already have the extent buffer's rwlock, it'll only be a matter of time until somebody (me) confuses eb->lock and eb->eb_lock. I'd like something representing its purpose (which I didn't catch yet). eb->refs_lock or eb->pages_lock might be appropriate.

fs/btrfs/extent_io.h struct extent_buffer { · line 136

Quoted 5 lines
fs/btrfs/extent_io.h +2 -0
+	int refs;
+	int pages_reading;
 	struct list_head leak_list;
 	struct rcu_head rcu_head;
 	pid_t lock_owner;

Thanks,
-Jan

#2 lore raw
Quoted 7 lines · Jan Schmidt

Hi Josef,

Quoted 1 line · Josef Bacik

I need to be able to safely deal with refs in my next patch, so convert refs and

Did I miss your next patch?

Quoted 8 lines
Quoted 2 lines

pages_reading to ints and introduce an eb_lock spinlock so I can use this to safely manipulate the refs count when marking eb's as stale. Thanks,

I don't see what makes this version safer, are you synchronizing eb->refs with eb->pages_reading? This would be strange, because eb->pages_reading sounds like it could never be != 0 when eb->refs goes down to 0. Could you extend your description a bit, please?

So I'm introducing eb_lock to protect eb local stuff, such as refs and pages_reading. The idea is that since I have to have a spin_lock anyway I might as well use it for pages_reading as well, especially since further down the line (like what I'm currently working on) I need to change it to cover pages in write as well and I need to do it in a way that atomic won't help.

Quoted 9 lines
Quoted 9 lines
fs/btrfs/extent_io.h +0 -1
index f030a2d..d11e872 100644
@@ -128,8 +128,9 @@ struct extent_buffer {
 	unsigned long map_len;
 	unsigned long bflags;
 	struct extent_io_tree *tree;
-	atomic_t refs;
Quoted 9 lines

fs/btrfs/extent_io.h struct extent_buffer { · line 131

Quoted 2 lines
fs/btrfs/extent_io.h +1 -1
-	atomic_t pages_reading;
+	spinlock_t eb_lock;

If you need that one, then please use another name for this. We already have the extent buffer's rwlock, it'll only be a matter of time until somebody (me) confuses eb->lock and eb->eb_lock. I'd like something representing its purpose (which I didn't catch yet). eb->refs_lock or eb->pages_lock might be appropriate.

pages_lock sounds fine to me. Thanks,

Josef