Skip to content

Commit 600a45e

Browse files
Miao Xiekdave
authored andcommitted
Btrfs: fix deadlock on page lock when doing auto-defragment
When I ran xfstests circularly on a auto-defragment btrfs, the deadlock happened. Steps to reproduce: [tty0] # export MOUNT_OPTIONS="-o autodefrag" # export TEST_DEV=<partition1> # export TEST_DIR=<mountpoint1> # export SCRATCH_DEV=<partition2> # export SCRATCH_MNT=<mountpoint2> # while [ 1 ] > do > ./check 091 127 263 > sleep 1 > done [tty1] # while [ 1 ] > do > echo 3 > /proc/sys/vm/drop_caches > done Several hours later, the test processes will hang on, and the deadlock will happen on page lock. The reason is that: Auto defrag task Flush thread Test task btrfs_writepages() add ordered extent (including page 1, 2) set page 1 writeback set page 2 writeback endio_fn() end page 2 writeback release page 2 lock page 1 alloc and lock page 2 page 2 is not uptodate btrfs_readpage() start ordered extent() btrfs_writepages() try to lock page 1 so deadlock happens. Fix this bug by unlocking the page which is in writeback, and re-locking it after the writeback end. Signed-off-by: Miao Xie <miax@cn.fujitsu.com>
1 parent 013bd4c commit 600a45e

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ static int cluster_pages_for_defrag(struct inode *inode,
862862
int i_done;
863863
struct btrfs_ordered_extent *ordered;
864864
struct extent_state *cached_state = NULL;
865+
struct extent_io_tree *tree;
865866
gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
866867

867868
if (isize == 0)
@@ -872,18 +873,34 @@ static int cluster_pages_for_defrag(struct inode *inode,
872873
num_pages << PAGE_CACHE_SHIFT);
873874
if (ret)
874875
return ret;
875-
again:
876-
ret = 0;
877876
i_done = 0;
877+
tree = &BTRFS_I(inode)->io_tree;
878878

879879
/* step one, lock all the pages */
880880
for (i = 0; i < num_pages; i++) {
881881
struct page *page;
882+
again:
882883
page = find_or_create_page(inode->i_mapping,
883-
start_index + i, mask);
884+
start_index + i, mask);
884885
if (!page)
885886
break;
886887

888+
page_start = page_offset(page);
889+
page_end = page_start + PAGE_CACHE_SIZE - 1;
890+
while (1) {
891+
lock_extent(tree, page_start, page_end, GFP_NOFS);
892+
ordered = btrfs_lookup_ordered_extent(inode,
893+
page_start);
894+
unlock_extent(tree, page_start, page_end, GFP_NOFS);
895+
if (!ordered)
896+
break;
897+
898+
unlock_page(page);
899+
btrfs_start_ordered_extent(inode, ordered, 1);
900+
btrfs_put_ordered_extent(ordered);
901+
lock_page(page);
902+
}
903+
887904
if (!PageUptodate(page)) {
888905
btrfs_readpage(NULL, page);
889906
lock_page(page);
@@ -894,15 +911,22 @@ static int cluster_pages_for_defrag(struct inode *inode,
894911
break;
895912
}
896913
}
914+
897915
isize = i_size_read(inode);
898916
file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
899-
if (!isize || page->index > file_end ||
900-
page->mapping != inode->i_mapping) {
917+
if (!isize || page->index > file_end) {
901918
/* whoops, we blew past eof, skip this page */
902919
unlock_page(page);
903920
page_cache_release(page);
904921
break;
905922
}
923+
924+
if (page->mapping != inode->i_mapping) {
925+
unlock_page(page);
926+
page_cache_release(page);
927+
goto again;
928+
}
929+
906930
pages[i] = page;
907931
i_done++;
908932
}
@@ -925,25 +949,6 @@ static int cluster_pages_for_defrag(struct inode *inode,
925949
lock_extent_bits(&BTRFS_I(inode)->io_tree,
926950
page_start, page_end - 1, 0, &cached_state,
927951
GFP_NOFS);
928-
ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
929-
if (ordered &&
930-
ordered->file_offset + ordered->len > page_start &&
931-
ordered->file_offset < page_end) {
932-
btrfs_put_ordered_extent(ordered);
933-
unlock_extent_cached(&BTRFS_I(inode)->io_tree,
934-
page_start, page_end - 1,
935-
&cached_state, GFP_NOFS);
936-
for (i = 0; i < i_done; i++) {
937-
unlock_page(pages[i]);
938-
page_cache_release(pages[i]);
939-
}
940-
btrfs_wait_ordered_range(inode, page_start,
941-
page_end - page_start);
942-
goto again;
943-
}
944-
if (ordered)
945-
btrfs_put_ordered_extent(ordered);
946-
947952
clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
948953
page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
949954
EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,

0 commit comments

Comments
 (0)