Skip to content

Commit e7a7981

Browse files
fdmananakdave
authored andcommitted
btrfs: check if a log root exists before locking the log_mutex on unlink
This brings back an optimization that commit e678934 ("btrfs: Remove unnecessary check from join_running_log_trans") removed, but in a different form. So it's almost equivalent to a revert. That commit removed an optimization where we avoid locking a root's log_mutex when there is no log tree created in the current transaction. The affected code path is triggered through unlink operations. That commit was based on the assumption that the optimization was not necessary because we used to have the following checks when the patch was authored: int btrfs_del_dir_entries_in_log(...) { (...) if (dir->logged_trans < trans->transid) return 0; ret = join_running_log_trans(root); (...) } int btrfs_del_inode_ref_in_log(...) { (...) if (inode->logged_trans < trans->transid) return 0; ret = join_running_log_trans(root); (...) } However before that patch was merged, another patch was merged first which replaced those checks because they were buggy. That other patch corresponds to commit 803f0f6 ("Btrfs: fix fsync not persisting dentry deletions due to inode evictions"). The assumption that if the logged_trans field of an inode had a smaller value then the current transaction's generation (transid) meant that the inode was not logged in the current transaction was only correct if the inode was not evicted and reloaded in the current transaction. So the corresponding bug fix changed those checks and replaced them with the following helper function: static bool inode_logged(struct btrfs_trans_handle *trans, struct btrfs_inode *inode) { if (inode->logged_trans == trans->transid) return true; if (inode->last_trans == trans->transid && test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) && !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags)) return true; return false; } So if we have a subvolume without a log tree in the current transaction (because we had no fsyncs), every time we unlink an inode we can end up trying to lock the log_mutex of the root through join_running_log_trans() twice, once for the inode being unlinked (by btrfs_del_inode_ref_in_log()) and once for the parent directory (with btrfs_del_dir_entries_in_log()). This means if we have several unlink operations happening in parallel for inodes in the same subvolume, and the those inodes and/or their parent inode were changed in the current transaction, we end up having a lot of contention on the log_mutex. The test robots from intel reported a -30.7% performance regression for a REAIM test after commit e678934 ("btrfs: Remove unnecessary check from join_running_log_trans"). So just bring back the optimization to join_running_log_trans() where we check first if a log root exists before trying to lock the log_mutex. This is done by checking for a bit that is set on the root when a log tree is created and removed when a log tree is freed (at transaction commit time). Commit e678934 ("btrfs: Remove unnecessary check from join_running_log_trans") was merged in the 5.4 merge window while commit 803f0f6 ("Btrfs: fix fsync not persisting dentry deletions due to inode evictions") was merged in the 5.3 merge window. But the first commit was actually authored before the second commit (May 23 2019 vs June 19 2019). Reported-by: kernel test robot <[email protected]> Link: https://lore.kernel.org/lkml/20200611090233.GL12456@shao2-debian/ Fixes: e678934 ("btrfs: Remove unnecessary check from join_running_log_trans") CC: [email protected] # 5.4+ Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 6bd335b commit e7a7981

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

fs/btrfs/ctree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,8 @@ enum {
10091009
BTRFS_ROOT_DEAD_RELOC_TREE,
10101010
/* Mark dead root stored on device whose cleanup needs to be resumed */
10111011
BTRFS_ROOT_DEAD_TREE,
1012+
/* The root has a log tree. Used only for subvolume roots. */
1013+
BTRFS_ROOT_HAS_LOG_TREE,
10121014
};
10131015

10141016
/*

fs/btrfs/tree-log.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
169169
if (ret)
170170
goto out;
171171

172+
set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
172173
clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
173174
root->log_start_pid = current->pid;
174175
}
@@ -195,6 +196,9 @@ static int join_running_log_trans(struct btrfs_root *root)
195196
{
196197
int ret = -ENOENT;
197198

199+
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
200+
return ret;
201+
198202
mutex_lock(&root->log_mutex);
199203
if (root->log_root) {
200204
ret = 0;
@@ -3303,6 +3307,7 @@ int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
33033307
if (root->log_root) {
33043308
free_log_tree(trans, root->log_root);
33053309
root->log_root = NULL;
3310+
clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
33063311
}
33073312
return 0;
33083313
}

0 commit comments

Comments
 (0)