Skip to contentSkip to conversation

Btrfs: end writeback on pages before finishing ordered extents

PATCH New Josef Bacik 1 participant 1 message Apr 20, 2012

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

PATCH 1/1 Btrfs: end writeback on pages before finishing ordered extents

So we have a slight deadlock issue where somebody will be trying to make an allocation while holding a tree lock and then be forced to wait while we flush out data. Well our flushers will then try to finish the ordered io and get stuck on this lock and we will deadlock. The thing is we don't really need the pages to finish the ordered IO, so instead do the normal data end io stuff in the interrupt context and end page writeback as soon as possible, and then if the ordered extent is finished, schedule that to be finished later on. I had to add a bio flag so that the end io function knows wether it is already in a thread or not so it knows wether it can make allocations or not. Also this doesn't happen for O_DIRECT, which I think is ok but worst case scenario we can fix everybody to just shove ordered extent completion off on a helper thread. I ran this through xfstests and it seemed to work, but its super dangerous so testing is highly encouraged. Thanks,

---
 fs/btrfs/compression.c      |   12 ++++---
 fs/btrfs/disk-io.c          |   71 ++++++++++++++++++++++++++-----------------
 fs/btrfs/disk-io.h          |    2 +-
 fs/btrfs/extent_io.c        |   23 +++++++-------
 fs/btrfs/extent_io.h        |    8 ++++-
 fs/btrfs/free-space-cache.c |    4 +--
 fs/btrfs/inode.c            |   68 ++++++++++++++++++++++++++++++-----------
 fs/btrfs/ordered-data.c     |   51 ++++++++++++++++++-------------
 fs/btrfs/ordered-data.h     |    2 +
 include/linux/blk_types.h   |    2 +
 10 files changed, 154 insertions(+), 89 deletions(-)
fs/btrfs/compression.c +7 -5
index d11afa67..9def84a 100644
@@ -270,6 +270,7 @@ static void end_compressed_bio_write(struct bio *bio, int err)
 	struct inode *inode;
 	struct page *page;
 	unsigned long index;
+	int async = test_bit(BIO_ASYNC_ENDIO, &bio->bi_flags);
 
 	if (err)
 		cb->errors = 1;
@@ -289,7 +290,7 @@ static void end_compressed_bio_write(struct bio *bio, int err)
 	tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
 					 cb->start,
 					 cb->start + cb->len - 1,
-					 NULL, 1);
+					 NULL, 1, async);
 	cb->compressed_pages[0]->mapping = NULL;
 
 	end_compressed_writeback(inode, cb->start, cb->len);
@@ -390,7 +391,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
 			 * freed before we're done setting it up
 			 */
 			atomic_inc(&cb->pending_bios);
-			ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
+			ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0, 1);
 			BUG_ON(ret); /* -ENOMEM */
 
 			if (!skip_sum) {
@@ -419,7 +420,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
 	}
 	bio_get(bio);
 
-	ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
+	ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0, 1);
 	BUG_ON(ret); /* -ENOMEM */
 
 	if (!skip_sum) {
@@ -660,7 +661,8 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 		    PAGE_CACHE_SIZE) {
 			bio_get(comp_bio);
 
-			ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
+			ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0,
+						  1);
 			BUG_ON(ret); /* -ENOMEM */
 
 			/*
@@ -696,7 +698,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	}
 	bio_get(comp_bio);
 
-	ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
+	ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0, 1);
 	BUG_ON(ret); /* -ENOMEM */
 
 	if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
fs/btrfs/disk-io.c +43 -28

127 lines not on this page · raw message

fs/btrfs/disk-io.h +1 -1
index a7ace1a..719a288 100644
@@ -72,7 +72,7 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid);
 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len);
 void btrfs_csum_final(u32 crc, char *result);
 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
-			int metadata);
+			int metadata, int noasync);
 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
 			int rw, struct bio *bio, int mirror_num,
 			unsigned long bio_flags, u64 bio_offset,
fs/btrfs/extent_io.c +12 -11
index 7c501d3..6317ae8 100644
@@ -1170,9 +1170,8 @@ int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
 			      cached_state, mask);
 }
 
-static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
-				 u64 end, struct extent_state **cached_state,
-				 gfp_t mask)
+int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
+			  struct extent_state **cached_state, gfp_t mask)
 {
 	return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
 				cached_state, mask);
@@ -2200,7 +2199,8 @@ static int bio_readpage_error(struct bio *failed_bio, struct page *page,
 
 /* lots and lots of room for performance fixes in the end_bio funcs */
 
-int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
+int end_extent_writepage(struct page *page, int err, u64 start, u64 end,
+			 int async)
 {
 	int uptodate = (err == 0);
 	struct extent_io_tree *tree;
@@ -2210,7 +2210,7 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
 
 	if (tree->ops && tree->ops->writepage_end_io_hook) {
 		ret = tree->ops->writepage_end_io_hook(page, start,
-					       end, NULL, uptodate);
+					       end, NULL, uptodate, async);
 		if (ret)
 			uptodate = 0;
 	}
@@ -2225,6 +2225,7 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
 	}
 
 	if (!uptodate) {
+		BUG_ON(!async); /* Logic error */
 		clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
 		ClearPageUptodate(page);
 		SetPageError(page);
@@ -2248,7 +2249,7 @@ static void end_bio_extent_writepage(struct bio *bio, int err)
 	u64 start;
 	u64 end;
 	int whole_page;
-
+	int async = test_bit(BIO_ASYNC_ENDIO, &bio->bi_flags);
 	do {
 		struct page *page = bvec->bv_page;
 		tree = &BTRFS_I(page->mapping->host)->io_tree;
@@ -2265,7 +2266,7 @@ static void end_bio_extent_writepage(struct bio *bio, int err)
 		if (--bvec >= bio->bi_io_vec)
 			prefetchw(&bvec->bv_page->flags);
 
-		if (end_extent_writepage(page, err, start, end))
+		if (end_extent_writepage(page, err, start, end, async))
 			continue;
 
 		if (whole_page)
@@ -2919,7 +2920,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 	if (last_byte <= start) {
 		if (tree->ops && tree->ops->writepage_end_io_hook)
 			tree->ops->writepage_end_io_hook(page, start,
-							 page_end, NULL, 1);
+							 page_end, NULL, 1, 1);
 		goto done;
 	}
 
@@ -2929,7 +2930,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 		if (cur >= last_byte) {
 			if (tree->ops && tree->ops->writepage_end_io_hook)
 				tree->ops->writepage_end_io_hook(page, cur,
-							 page_end, NULL, 1);
+							 page_end, NULL, 1, 1);
 			break;
 		}
 		em = epd->get_extent(inode, page, pg_offset, cur,
@@ -2965,7 +2966,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 			    tree->ops->writepage_end_io_hook)
 				tree->ops->writepage_end_io_hook(page, cur,
 							 cur + iosize - 1,
-							 NULL, 1);
+							 NULL, 1, 1);
 			else if (compressed) {
 				/* we don't want to end_page_writeback on
 				 * a compressed extent.  this happens
@@ -3501,7 +3502,7 @@ int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
 			if (tree->ops && tree->ops->writepage_end_io_hook)
 				tree->ops->writepage_end_io_hook(page, start,
 						 start + PAGE_CACHE_SIZE - 1,
-						 NULL, 1);
+						 NULL, 1, 1);
 			unlock_page(page);
 		}
 		page_cache_release(page);
fs/btrfs/extent_io.h +6 -2
index b516c3b..2b411d8 100644
@@ -81,7 +81,8 @@ struct extent_io_ops {
 	int (*readpage_end_io_hook)(struct page *page, u64 start, u64 end,
 				    struct extent_state *state, int mirror);
 	int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
-				      struct extent_state *state, int uptodate);
+				      struct extent_state *state, int uptodate,
+				      int async);
 	void (*set_bit_hook)(struct inode *inode, struct extent_state *state,
 			     int *bits);
 	void (*clear_bit_hook)(struct inode *inode, struct extent_state *state,
@@ -225,6 +226,8 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
 		   struct extent_state **cached_state, gfp_t mask);
 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
 			struct extent_state **cached_state, gfp_t mask);
+int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
+			  struct extent_state **cached_state, gfp_t mask);
 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
 		   gfp_t mask);
 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
@@ -325,7 +328,8 @@ struct btrfs_mapping_tree;
 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
 			u64 length, u64 logical, struct page *page,
 			int mirror_num);
-int end_extent_writepage(struct page *page, int err, u64 start, u64 end);
+int end_extent_writepage(struct page *page, int err, u64 start, u64 end,
+			 int async);
 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
 			 int mirror_num);
 #endif
fs/btrfs/free-space-cache.c +1 -3
index baaa518..a99adbd 100644
@@ -972,9 +972,7 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
 		goto out;
 
 
-	ret = filemap_write_and_wait(inode->i_mapping);
-	if (ret)
-		goto out;
+	btrfs_wait_ordered_range(inode, 0, -1);
 
 	key.objectid = BTRFS_FREE_SPACE_OBJECTID;
 	key.offset = offset;
fs/btrfs/inode.c +50 -18

136 lines not on this page · raw message

fs/btrfs/ordered-data.c +30 -21

180 lines not on this page · raw message

fs/btrfs/ordered-data.h +2 -0
index c355ad4..5609108 100644
@@ -113,6 +113,8 @@ struct btrfs_ordered_extent {
 
 	/* a per root list of all the pending ordered extents */
 	struct list_head root_extent_list;
+
+	struct btrfs_work work;
 };
 
 
include/linux/blk_types.h +2 -0
index 4053cbd..385154e 100644
@@ -95,6 +95,8 @@ struct bio {
 #define BIO_FS_INTEGRITY 9	/* fs owns integrity data, not block layer */
 #define BIO_QUIET	10	/* Make BIO Quiet */
 #define BIO_MAPPED_INTEGRITY 11/* integrity metadata has been remapped */
+#define BIO_ASYNC_ENDIO 12	/* bi_endio is being run outside of the
+				 * interrupt context */
 #define bio_flagged(bio, flag)	((bio)->bi_flags & (1 << (flag)))
 
 /*
Signature · 1 line
1.7.7.6