History

btrfs: replace cleaner_delayed_iput_mutex with a waitqueue

The throttle path doesn't take cleaner_delayed_iput_mutex, which means we could think we're done flushing iputs in the data space reservation path when we could have a throttler doing an iput. There's no real reason to serialize the delayed iput flushing, so instead of taking the cleaner_delayed_iput_mutex whenever we flush the delayed iputs just replace it with an atomic counter and a waitqueue. This removes the short (or long depending on how big the inode is) window where we think there are no more pending iputs when there really are some. The waiting is killable as it could be indirectly called from user operations like fallocate or zero-range. Such call sites should handle the error but otherwise it's not necessary. Eg. flush_space just needs to attempt to make space by waiting on iputs.

Josef Bacik committed 7 years ago

Commit 034f7841 parent 3ece54e4 files · +35 −9Browse files

Unified · Split

4 files
  1. fs/btrfs/ctree.h +3 −1
  2. fs/btrfs/disk-io.c +2 −3
  3. fs/btrfs/extent-tree.c +8 −5
  4. fs/btrfs/inode.c +22 −0
fs/btrfs/ctree.h+3−1
@@ -934,7 +934,8 @@ struct btrfs_fs_info { · 933 unmodified lines
934934
935935 spinlock_t delayed_iput_lock;
936936 struct list_head delayed_iputs;
937− struct mutex cleaner_delayed_iput_mutex;
937+ atomic_t nr_delayed_iputs;
938+ wait_queue_head_t delayed_iputs_wait;
938939
939940 /* this protects tree_mod_seq_list */
940941 spinlock_t tree_mod_seq_lock;
@@ -3282,6 +3283,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root); · 2341 unmodified lines
32823283 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size);
32833284 void btrfs_add_delayed_iput(struct inode *inode);
32843285 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
3286+int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
32853287 int btrfs_prealloc_file_range(struct inode *inode, int mode,
32863288 u64 start, u64 num_bytes, u64 min_size,
32873289 loff_t actual_len, u64 *alloc_hint);
fs/btrfs/disk-io.c+2−3
@@ -1717,9 +1717,7 @@ static int cleaner_kthread(void *arg) · 1716 unmodified lines
17171717 goto sleep;
17181718 }
17191719
1720− mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
17211720 btrfs_run_delayed_iputs(fs_info);
1722− mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
17231721
17241722 again = btrfs_clean_one_deleted_snapshot(root);
17251723 mutex_unlock(&fs_info->cleaner_mutex);
@@ -2676,7 +2674,6 @@ int open_ctree(struct super_block *sb, · 950 unmodified lines
26762674 mutex_init(&fs_info->delete_unused_bgs_mutex);
26772675 mutex_init(&fs_info->reloc_mutex);
26782676 mutex_init(&fs_info->delalloc_root_mutex);
2679− mutex_init(&fs_info->cleaner_delayed_iput_mutex);
26802677 seqlock_init(&fs_info->profiles_lock);
26812678
26822679 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
@@ -2698,6 +2695,7 @@ int open_ctree(struct super_block *sb, · 15 unmodified lines
26982695 atomic_set(&fs_info->defrag_running, 0);
26992696 atomic_set(&fs_info->qgroup_op_seq, 0);
27002697 atomic_set(&fs_info->reada_works_cnt, 0);
2698+ atomic_set(&fs_info->nr_delayed_iputs, 0);
27012699 atomic64_set(&fs_info->tree_mod_seq, 0);
27022700 fs_info->sb = sb;
27032701 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
@@ -2775,6 +2773,7 @@ int open_ctree(struct super_block *sb, · 71 unmodified lines
27752773 init_waitqueue_head(&fs_info->transaction_wait);
27762774 init_waitqueue_head(&fs_info->transaction_blocked_wait);
27772775 init_waitqueue_head(&fs_info->async_submit_wait);
2776+ init_waitqueue_head(&fs_info->delayed_iputs_wait);
27782777
27792778 INIT_LIST_HEAD(&fs_info->pinned_chunks);
27802779
fs/btrfs/extent-tree.c+8−5
@@ -4279,10 +4279,14 @@ commit_trans: · 4278 unmodified lines
42794279 /*
42804280 * The cleaner kthread might still be doing iput
42814281 * operations. Wait for it to finish so that
4282− * more space is released.
4282+ * more space is released. We don't need to
4283+ * explicitly run the delayed iputs here because
4284+ * the commit_transaction would have woken up
4285+ * the cleaner.
42834286 */
4284− mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4285− mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
4287+ ret = btrfs_wait_on_delayed_iputs(fs_info);
4288+ if (ret)
4289+ return ret;
42864290 goto again;
42874291 } else {
42884292 btrfs_end_transaction(trans);
@@ -4967,9 +4971,8 @@ static void flush_space(struct btrfs_fs_info *fs_info, · 678 unmodified lines
49674971 * bunch of pinned space, so make sure we run the iputs before
49684972 * we do our pinned bytes check below.
49694973 */
4970− mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
49714974 btrfs_run_delayed_iputs(fs_info);
4972− mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
4975+ btrfs_wait_on_delayed_iputs(fs_info);
49734976
49744977 ret = may_commit_transaction(fs_info, space_info);
49754978 break;
fs/btrfs/inode.c+22−0
@@ -3256,6 +3256,7 @@ void btrfs_add_delayed_iput(struct inode *inode) · 3255 unmodified lines
32563256 if (atomic_add_unless(&inode->i_count, -1, 1))
32573257 return;
32583258
3259+ atomic_inc(&fs_info->nr_delayed_iputs);
32593260 spin_lock(&fs_info->delayed_iput_lock);
32603261 ASSERT(list_empty(&binode->delayed_iput));
32613262 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
@@ -3276,11 +3277,32 @@ void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) · 14 unmodified lines
32763277 list_del_init(&inode->delayed_iput);
32773278 spin_unlock(&fs_info->delayed_iput_lock);
32783279 iput(&inode->vfs_inode);
3280+ if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3281+ wake_up(&fs_info->delayed_iputs_wait);
32793282 spin_lock(&fs_info->delayed_iput_lock);
32803283 }
32813284 spin_unlock(&fs_info->delayed_iput_lock);
32823285 }
32833286
3287+/**
3288+ * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3289+ * @fs_info - the fs_info for this fs
3290+ * @return - EINTR if we were killed, 0 if nothing's pending
3291+ *
3292+ * This will wait on any delayed iputs that are currently running with KILLABLE
3293+ * set. Once they are all done running we will return, unless we are killed in
3294+ * which case we return EINTR. This helps in user operations like fallocate etc
3295+ * that might get blocked on the iputs.
3296+ */
3297+int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3298+{
3299+ int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3300+ atomic_read(&fs_info->nr_delayed_iputs) == 0);
3301+ if (ret)
3302+ return -EINTR;
3303+ return 0;
3304+}
3305+
32843306 /*
32853307 * This creates an orphan entry for the given inode in case something goes wrong
32863308 * in the middle of an unlink.