Skip to content

Commit f4b1363

Browse files
josefbacikkdave
authored andcommitted
btrfs: do not do delalloc reservation under page lock
We ran into a deadlock in production with the fixup worker. The stack traces were as follows: Thread responsible for the writeout, waiting on the page lock [<0>] io_schedule+0x12/0x40 [<0>] __lock_page+0x109/0x1e0 [<0>] extent_write_cache_pages+0x206/0x360 [<0>] extent_writepages+0x40/0x60 [<0>] do_writepages+0x31/0xb0 [<0>] __writeback_single_inode+0x3d/0x350 [<0>] writeback_sb_inodes+0x19d/0x3c0 [<0>] __writeback_inodes_wb+0x5d/0xb0 [<0>] wb_writeback+0x231/0x2c0 [<0>] wb_workfn+0x308/0x3c0 [<0>] process_one_work+0x1e0/0x390 [<0>] worker_thread+0x2b/0x3c0 [<0>] kthread+0x113/0x130 [<0>] ret_from_fork+0x35/0x40 [<0>] 0xffffffffffffffff Thread of the fixup worker who is holding the page lock [<0>] start_delalloc_inodes+0x241/0x2d0 [<0>] btrfs_start_delalloc_roots+0x179/0x230 [<0>] btrfs_alloc_data_chunk_ondemand+0x11b/0x2e0 [<0>] btrfs_check_data_free_space+0x53/0xa0 [<0>] btrfs_delalloc_reserve_space+0x20/0x70 [<0>] btrfs_writepage_fixup_worker+0x1fc/0x2a0 [<0>] normal_work_helper+0x11c/0x360 [<0>] process_one_work+0x1e0/0x390 [<0>] worker_thread+0x2b/0x3c0 [<0>] kthread+0x113/0x130 [<0>] ret_from_fork+0x35/0x40 [<0>] 0xffffffffffffffff Thankfully the stars have to align just right to hit this. First you have to end up in the fixup worker, which is tricky by itself (my reproducer does DIO reads into a MMAP'ed region, so not a common operation). Then you have to have less than a page size of free data space and 0 unallocated space so you go down the "commit the transaction to free up pinned space" path. This was accomplished by a random balance that was running on the host. Then you get this deadlock. I'm still in the process of trying to force the deadlock to happen on demand, but I've hit other issues. I can still trigger the fixup worker path itself so this patch has been tested in that regard, so the normal case is fine. Fixes: 87826df ("btrfs: delalloc for page dirtied out-of-band in fixup worker") Signed-off-by: Josef Bacik <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 5ab5805 commit f4b1363

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

fs/btrfs/inode.c

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
21892189
/* see btrfs_writepage_start_hook for details on why this is required */
21902190
struct btrfs_writepage_fixup {
21912191
struct page *page;
2192+
struct inode *inode;
21922193
struct btrfs_work work;
21932194
};
21942195

@@ -2203,9 +2204,20 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
22032204
u64 page_start;
22042205
u64 page_end;
22052206
int ret = 0;
2207+
bool free_delalloc_space = true;
22062208

22072209
fixup = container_of(work, struct btrfs_writepage_fixup, work);
22082210
page = fixup->page;
2211+
inode = fixup->inode;
2212+
page_start = page_offset(page);
2213+
page_end = page_offset(page) + PAGE_SIZE - 1;
2214+
2215+
/*
2216+
* This is similar to page_mkwrite, we need to reserve the space before
2217+
* we take the page lock.
2218+
*/
2219+
ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2220+
PAGE_SIZE);
22092221
again:
22102222
lock_page(page);
22112223

@@ -2214,25 +2226,48 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
22142226
* page->mapping may go NULL, but it shouldn't be moved to a different
22152227
* address space.
22162228
*/
2217-
if (!page->mapping || !PageDirty(page) || !PageChecked(page))
2229+
if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2230+
/*
2231+
* Unfortunately this is a little tricky, either
2232+
*
2233+
* 1) We got here and our page had already been dealt with and
2234+
* we reserved our space, thus ret == 0, so we need to just
2235+
* drop our space reservation and bail. This can happen the
2236+
* first time we come into the fixup worker, or could happen
2237+
* while waiting for the ordered extent.
2238+
* 2) Our page was already dealt with, but we happened to get an
2239+
* ENOSPC above from the btrfs_delalloc_reserve_space. In
2240+
* this case we obviously don't have anything to release, but
2241+
* because the page was already dealt with we don't want to
2242+
* mark the page with an error, so make sure we're resetting
2243+
* ret to 0. This is why we have this check _before_ the ret
2244+
* check, because we do not want to have a surprise ENOSPC
2245+
* when the page was already properly dealt with.
2246+
*/
2247+
if (!ret) {
2248+
btrfs_delalloc_release_extents(BTRFS_I(inode),
2249+
PAGE_SIZE);
2250+
btrfs_delalloc_release_space(inode, data_reserved,
2251+
page_start, PAGE_SIZE,
2252+
true);
2253+
}
2254+
ret = 0;
22182255
goto out_page;
2256+
}
22192257

22202258
/*
2221-
* We keep the PageChecked() bit set until we're done with the
2222-
* btrfs_start_ordered_extent() dance that we do below. That drops and
2223-
* retakes the page lock, so we don't want new fixup workers queued for
2224-
* this page during the churn.
2259+
* We can't mess with the page state unless it is locked, so now that
2260+
* it is locked bail if we failed to make our space reservation.
22252261
*/
2226-
inode = page->mapping->host;
2227-
page_start = page_offset(page);
2228-
page_end = page_offset(page) + PAGE_SIZE - 1;
2262+
if (ret)
2263+
goto out_page;
22292264

22302265
lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
22312266
&cached_state);
22322267

22332268
/* already ordered? We're done */
22342269
if (PagePrivate2(page))
2235-
goto out;
2270+
goto out_reserved;
22362271

22372272
ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
22382273
PAGE_SIZE);
@@ -2245,11 +2280,6 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
22452280
goto again;
22462281
}
22472282

2248-
ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2249-
PAGE_SIZE);
2250-
if (ret)
2251-
goto out;
2252-
22532283
ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
22542284
&cached_state);
22552285
if (ret)
@@ -2263,12 +2293,12 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
22632293
* The page was dirty when we started, nothing should have cleaned it.
22642294
*/
22652295
BUG_ON(!PageDirty(page));
2296+
free_delalloc_space = false;
22662297
out_reserved:
22672298
btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2268-
if (ret)
2299+
if (free_delalloc_space)
22692300
btrfs_delalloc_release_space(inode, data_reserved, page_start,
22702301
PAGE_SIZE, true);
2271-
out:
22722302
unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
22732303
&cached_state);
22742304
out_page:
@@ -2287,6 +2317,12 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
22872317
put_page(page);
22882318
kfree(fixup);
22892319
extent_changeset_free(data_reserved);
2320+
/*
2321+
* As a precaution, do a delayed iput in case it would be the last iput
2322+
* that could need flushing space. Recursing back to fixup worker would
2323+
* deadlock.
2324+
*/
2325+
btrfs_add_delayed_iput(inode);
22902326
}
22912327

22922328
/*
@@ -2324,10 +2360,18 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
23242360
if (!fixup)
23252361
return -EAGAIN;
23262362

2363+
/*
2364+
* We are already holding a reference to this inode from
2365+
* write_cache_pages. We need to hold it because the space reservation
2366+
* takes place outside of the page lock, and we can't trust
2367+
* page->mapping outside of the page lock.
2368+
*/
2369+
ihold(inode);
23272370
SetPageChecked(page);
23282371
get_page(page);
23292372
btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
23302373
fixup->page = page;
2374+
fixup->inode = inode;
23312375
btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
23322376

23332377
return -EAGAIN;

0 commit comments

Comments
 (0)