diff options
author | Chris Mason <clm@fb.com> | 2016-10-27 13:42:20 -0400 |
---|---|---|
committer | Chris Mason <clm@fb.com> | 2016-10-27 13:42:20 -0400 |
commit | 570dd45042a7c8a7aba1ee029c5dd0f5ccf41b9b (patch) | |
tree | 8eef62c0d0b1481a4b70df1bc6b1c2e6fdbe5c2a | |
parent | 112a3edf4bc687e02b9aa13c33391203fe6b7714 (diff) |
btrfs: fix races on root_log_ctx lists
btrfs_remove_all_log_ctxs takes a shortcut where it avoids walking the
list because it knows all of the waiters are patiently waiting for the
commit to finish.
But, there's a small race where btrfs_sync_log can remove itself from
the list if it finds a log commit is already done. Also, it uses
list_del_init() to remove itself from the list, but there's no way to
know if btrfs_remove_all_log_ctxs has already run, so we don't know for
sure if it is safe to call list_del_init().
This gets rid of all the shortcuts for btrfs_remove_all_log_ctxs(), and
just calls it with the proper locking.
This is part two of the corruption fixed by cbd60aa7cd1. I should have
done this in the first place, but convinced myself the optimizations were
safe. A 12 hour run of dbench 2048 will eventually trigger a list debug
WARN_ON for the list_del_init() in btrfs_sync_log().
Fixes: d1433debe7f4346cf9fc0dafc71c3137d2a97bc4
Reported-by: Dave Jones <davej@codemonkey.org.uk>
cc: stable@vger.kernel.org # 3.15+
Signed-off-by: Chris Mason <clm@fb.com>
-rw-r--r-- | fs/btrfs/tree-log.c | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 688df71c1bf7..4b1c0a6eee04 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c | |||
@@ -2713,14 +2713,12 @@ static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, | |||
2713 | int index, int error) | 2713 | int index, int error) |
2714 | { | 2714 | { |
2715 | struct btrfs_log_ctx *ctx; | 2715 | struct btrfs_log_ctx *ctx; |
2716 | struct btrfs_log_ctx *safe; | ||
2716 | 2717 | ||
2717 | if (!error) { | 2718 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
2718 | INIT_LIST_HEAD(&root->log_ctxs[index]); | 2719 | list_del_init(&ctx->list); |
2719 | return; | ||
2720 | } | ||
2721 | |||
2722 | list_for_each_entry(ctx, &root->log_ctxs[index], list) | ||
2723 | ctx->log_ret = error; | 2720 | ctx->log_ret = error; |
2721 | } | ||
2724 | 2722 | ||
2725 | INIT_LIST_HEAD(&root->log_ctxs[index]); | 2723 | INIT_LIST_HEAD(&root->log_ctxs[index]); |
2726 | } | 2724 | } |
@@ -2961,13 +2959,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, | |||
2961 | mutex_unlock(&root->log_mutex); | 2959 | mutex_unlock(&root->log_mutex); |
2962 | 2960 | ||
2963 | out_wake_log_root: | 2961 | out_wake_log_root: |
2964 | /* | 2962 | mutex_lock(&log_root_tree->log_mutex); |
2965 | * We needn't get log_mutex here because we are sure all | ||
2966 | * the other tasks are blocked. | ||
2967 | */ | ||
2968 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); | 2963 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
2969 | 2964 | ||
2970 | mutex_lock(&log_root_tree->log_mutex); | ||
2971 | log_root_tree->log_transid_committed++; | 2965 | log_root_tree->log_transid_committed++; |
2972 | atomic_set(&log_root_tree->log_commit[index2], 0); | 2966 | atomic_set(&log_root_tree->log_commit[index2], 0); |
2973 | mutex_unlock(&log_root_tree->log_mutex); | 2967 | mutex_unlock(&log_root_tree->log_mutex); |
@@ -2978,10 +2972,8 @@ out_wake_log_root: | |||
2978 | if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) | 2972 | if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) |
2979 | wake_up(&log_root_tree->log_commit_wait[index2]); | 2973 | wake_up(&log_root_tree->log_commit_wait[index2]); |
2980 | out: | 2974 | out: |
2981 | /* See above. */ | ||
2982 | btrfs_remove_all_log_ctxs(root, index1, ret); | ||
2983 | |||
2984 | mutex_lock(&root->log_mutex); | 2975 | mutex_lock(&root->log_mutex); |
2976 | btrfs_remove_all_log_ctxs(root, index1, ret); | ||
2985 | root->log_transid_committed++; | 2977 | root->log_transid_committed++; |
2986 | atomic_set(&root->log_commit[index1], 0); | 2978 | atomic_set(&root->log_commit[index1], 0); |
2987 | mutex_unlock(&root->log_mutex); | 2979 | mutex_unlock(&root->log_mutex); |