Skip to content

Commit 6c68fa6

Browse files
committed
Merge tag 'for-7.2-rc6-fixup-worker-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
Pull Btrfs Fixes 2: Electric Boogaloo from David Sterba: "This brings back the fixup worker infrastructure. It's a mechanism to detect pages/folios that are marked dirty without filesystem knowledge and require COW fixup. The consequence of not doing so is silent data loss. The first patch covers the scenarios in detail, also reflecting folio API port and subpage block size support added in recent years. The original fixup worker was only for pages. The patch is relatively big, half of the code is debugging and support code, the rest is the core design around the detection and fix. The second patch handles an unlikely case when there's work left during unmount" * tag 'for-7.2-rc6-fixup-worker-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: btrfs: flush the fixup workers during close_ctree btrfs: trigger cow fixup via dirty_folio()
2 parents 6335463 + ae2567b commit 6c68fa6

8 files changed

Lines changed: 651 additions & 12 deletions

File tree

fs/btrfs/btrfs_inode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ int btrfs_prealloc_file_range_trans(struct inode *inode,
600600
loff_t actual_len, u64 *alloc_hint);
601601
int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct folio *locked_folio,
602602
u64 start, u64 end, struct writeback_control *wbc);
603+
void btrfs_queue_writepage_fixup(struct btrfs_inode *inode, struct folio *folio);
603604
int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
604605
int compress_type);
605606
int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,

fs/btrfs/disk-io.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,8 @@ static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
17601760
/* helper to cleanup workers */
17611761
static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
17621762
{
1763+
if (fs_info->fixup_workers)
1764+
destroy_workqueue(fs_info->fixup_workers);
17631765
btrfs_destroy_workqueue(fs_info->delalloc_workers);
17641766
btrfs_destroy_workqueue(fs_info->workers);
17651767
if (fs_info->endio_workers)
@@ -1967,6 +1969,9 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
19671969
fs_info->caching_workers =
19681970
btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
19691971

1972+
fs_info->fixup_workers =
1973+
alloc_ordered_workqueue("btrfs-fixup", ordered_flags);
1974+
19701975
fs_info->endio_workers =
19711976
alloc_workqueue("btrfs-endio", flags, max_active);
19721977
fs_info->endio_meta_workers =
@@ -1992,7 +1997,7 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
19921997
fs_info->endio_workers && fs_info->endio_meta_workers &&
19931998
fs_info->endio_write_workers &&
19941999
fs_info->endio_freespace_worker && fs_info->rmw_workers &&
1995-
fs_info->caching_workers &&
2000+
fs_info->caching_workers && fs_info->fixup_workers &&
19962001
fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
19972002
fs_info->discard_ctl.discard_workers)) {
19982003
return -ENOMEM;
@@ -4364,6 +4369,18 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
43644369
/* clear out the rbtree of defraggable inodes */
43654370
btrfs_cleanup_defrag_inodes(fs_info);
43664371

4372+
/*
4373+
* Before the unmount, we sync down all the writeback which can
4374+
* generate fixup work. We are about to run delalloc for autodefrag so
4375+
* piggy back on that by also flushing the fixup work which can also
4376+
* generate delalloc we would like to get run.
4377+
*
4378+
* After this, it is still possible that some thread doing writeback is
4379+
* in btrfs_queue_writepage_fixup() and might finish queueing some final
4380+
* work, racing the btrfs_fs_closing() check there.
4381+
*/
4382+
flush_workqueue(fs_info->fixup_workers);
4383+
43674384
/*
43684385
* Handle the error fs first, as it will flush and wait for all ordered
43694386
* extents. This will generate delayed iputs, thus we want to handle
@@ -4441,6 +4458,15 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
44414458
cancel_work_sync(&fs_info->preempt_reclaim_work);
44424459
cancel_work_sync(&fs_info->em_shrinker_work);
44434460

4461+
/*
4462+
* Reclaim workers can run writeback which can queue fixup.
4463+
* After the above cancel_work_sync() calls, any such queueing attempts are
4464+
* guaranteed to see btrfs_fs_closing(), so at this point we can genuinely fully
4465+
* flush the fixup workqueue. This relies on the belief that *now* no thread can
4466+
* still be sitting in btrfs_queue_writepage_fixup().
4467+
*/
4468+
flush_workqueue(fs_info->fixup_workers);
4469+
44444470
/*
44454471
* Run delayed iputs again because an async reclaim worker may have
44464472
* added new ones if it was flushing delalloc:

fs/btrfs/extent_io.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,115 @@ static bool find_next_delalloc_bitmap(struct folio *folio,
14401440
return true;
14411441
}
14421442

1443+
/*
1444+
* Debug checks for fixup selection logic to help ensure the invariants
1445+
* we expect for fixup marking hold in practice.
1446+
*
1447+
* - A dirty block without a fixup bit is covered by delalloc or a running
1448+
* ordered extent (it was dirtied by a reserving write path).
1449+
* - A block with a fixup bit is never covered by delalloc: every delalloc
1450+
* setter holds the folio lock and cancels the fixup state of the blocks
1451+
* it covers (btrfs_folio_set_dirty()) before releasing it.
1452+
*/
1453+
static void debug_check_writepage_fixup(struct btrfs_inode *inode, u64 start,
1454+
u32 len, bool needs_fixup)
1455+
{
1456+
struct btrfs_ordered_extent *ordered;
1457+
bool delalloc;
1458+
1459+
if (!IS_ENABLED(CONFIG_BTRFS_DEBUG))
1460+
return;
1461+
1462+
delalloc = btrfs_test_range_bit_exists(&inode->io_tree, start,
1463+
start + len - 1, EXTENT_DELALLOC);
1464+
if (needs_fixup) {
1465+
if (unlikely(delalloc))
1466+
DEBUG_WARN("writeback: delalloc and fixup conflict. ino %llu start %llu",
1467+
btrfs_ino(inode), start);
1468+
} else {
1469+
if (delalloc)
1470+
return;
1471+
1472+
ordered = btrfs_lookup_ordered_range(inode, start, len);
1473+
if (unlikely(!ordered))
1474+
DEBUG_WARN("dirty block, no delalloc, fixup, ordered. ino %llu start %llu",
1475+
btrfs_ino(inode), start);
1476+
else
1477+
btrfs_put_ordered_extent(ordered);
1478+
}
1479+
}
1480+
1481+
/*
1482+
* Handle folios dirtied without a delalloc reservation, e.g.
1483+
* O_DIRECT read into a MAP_SHARED mapping dirtying via set_page_dirty_lock().
1484+
*
1485+
* btrfs_data_dirty_folio() records the affected blocks in the fixup bitmap
1486+
* and the folio fixup flag and we check them here in writeback.
1487+
*
1488+
* Don't submit such blocks and queue work for the fixup worker to reserve
1489+
* space for them so that they can be submitted properly by writeback.
1490+
*
1491+
* Return 1 if the folio needed fixup, 0 if not, and a negative error code
1492+
* on error.
1493+
*/
1494+
static noinline_for_stack int writepage_fixup(struct btrfs_inode *inode,
1495+
struct folio *folio,
1496+
struct btrfs_bio_ctrl *bio_ctrl)
1497+
{
1498+
struct btrfs_fs_info *fs_info = inode_to_fs_info(&inode->vfs_inode);
1499+
const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
1500+
const u32 sectorsize = fs_info->sectorsize;
1501+
const u64 page_start = folio_pos(folio);
1502+
bool found_fixup = false;
1503+
unsigned int bit;
1504+
1505+
/*
1506+
* A folio was dirtied without calling aops->dirty_folio() which we
1507+
* explicitly assert is not allowed.
1508+
*/
1509+
if (unlikely(bitmap_empty(bio_ctrl->submit_bitmap, blocks_per_folio))) {
1510+
DEBUG_WARN();
1511+
btrfs_err_rl(fs_info,
1512+
"root %lld ino %llu folio %llu is dirty with an empty dirty bitmap",
1513+
btrfs_root_id(inode->root), btrfs_ino(inode),
1514+
folio_pos(folio));
1515+
return -EUCLEAN;
1516+
}
1517+
1518+
/* Cheap check on the folio flag. Set iff the fixup bitmap is non-empty. */
1519+
if (likely(!folio_test_fixup_pending(folio)))
1520+
return 0;
1521+
1522+
for_each_set_bit(bit, bio_ctrl->submit_bitmap, blocks_per_folio) {
1523+
const u64 start = page_start + (bit << fs_info->sectorsize_bits);
1524+
const bool needs_fixup = btrfs_folio_test_fixup(fs_info, folio,
1525+
start, sectorsize);
1526+
1527+
debug_check_writepage_fixup(inode, start, sectorsize, needs_fixup);
1528+
if (needs_fixup) {
1529+
bitmap_clear(bio_ctrl->submit_bitmap, bit, 1);
1530+
found_fixup = true;
1531+
}
1532+
}
1533+
if (likely(found_fixup)) {
1534+
btrfs_queue_writepage_fixup(inode, folio);
1535+
folio_redirty_for_writepage(bio_ctrl->wbc, folio);
1536+
if (bitmap_empty(bio_ctrl->submit_bitmap, blocks_per_folio)) {
1537+
folio_unlock(folio);
1538+
return 1;
1539+
}
1540+
return 0;
1541+
}
1542+
/* We should always find fixup if the folio fixup flag was set. */
1543+
DEBUG_WARN();
1544+
btrfs_err_rl(fs_info,
1545+
"root %lld ino %llu folio %llu is fixup with an empty fixup bitmap",
1546+
btrfs_root_id(inode->root), btrfs_ino(inode),
1547+
folio_pos(folio));
1548+
1549+
return -EUCLEAN;
1550+
}
1551+
14431552
/*
14441553
* Do all of the delayed allocation setup.
14451554
*
@@ -1492,6 +1601,10 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
14921601
/* Save the dirty bitmap as our submission bitmap will be a subset of it. */
14931602
btrfs_copy_subpage_dirty_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
14941603

1604+
ret = writepage_fixup(inode, folio, bio_ctrl);
1605+
if (ret)
1606+
return ret;
1607+
14951608
for_each_set_bitrange(start_bit, end_bit, bio_ctrl->submit_bitmap,
14961609
blocks_per_folio) {
14971610
u64 start = page_start + (start_bit << fs_info->sectorsize_bits);

fs/btrfs/fs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ struct btrfs_fs_info {
713713
struct btrfs_workqueue *endio_write_workers;
714714
struct btrfs_workqueue *endio_freespace_worker;
715715
struct btrfs_workqueue *caching_workers;
716+
717+
struct workqueue_struct *fixup_workers;
716718
struct btrfs_workqueue *delayed_workers;
717719

718720
struct task_struct *transaction_kthread;
@@ -1200,6 +1202,16 @@ static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
12001202
clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
12011203
}
12021204

1205+
/*
1206+
* We use the folio owner_2 flag to indicate the folio has blocks that were
1207+
* dirtied without a space reservation and need the writepage fixup before
1208+
* writeback. For bs < folio_size the fixup bitmap tracks the affected
1209+
* blocks.
1210+
*/
1211+
#define folio_test_fixup_pending(folio) folio_test_owner_2(folio)
1212+
#define folio_set_fixup_pending(folio) folio_set_owner_2(folio)
1213+
#define folio_clear_fixup_pending(folio) folio_clear_owner_2(folio)
1214+
12031215
#define BTRFS_FS_ERROR(fs_info) (READ_ONCE((fs_info)->fs_error))
12041216

12051217
#define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \

0 commit comments

Comments
 (0)