Skip to content

Commit 998ac6d

Browse files
ethanwu-synokdave
authored andcommitted
btrfs: Take trans lock before access running trans in check_delayed_ref
In preivous patch: Btrfs: kill trans in run_delalloc_nocow and btrfs_cross_ref_exist We avoid starting btrfs transaction and get this information from fs_info->running_transaction directly. When accessing running_transaction in check_delayed_ref, there's a chance that current transaction will be freed by commit transaction after the NULL pointer check of running_transaction is passed. After looking all the other places using fs_info->running_transaction, they are either protected by trans_lock or holding the transactions. Fix this by using trans_lock and increasing the use_count. Fixes: e4c3b2d ("Btrfs: kill trans in run_delalloc_nocow and btrfs_cross_ref_exist") CC: [email protected] # 4.14+ Signed-off-by: ethanwu <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 17515f1 commit 998ac6d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

fs/btrfs/extent-tree.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3142,7 +3142,11 @@ static noinline int check_delayed_ref(struct btrfs_root *root,
31423142
struct rb_node *node;
31433143
int ret = 0;
31443144

3145+
spin_lock(&root->fs_info->trans_lock);
31453146
cur_trans = root->fs_info->running_transaction;
3147+
if (cur_trans)
3148+
refcount_inc(&cur_trans->use_count);
3149+
spin_unlock(&root->fs_info->trans_lock);
31463150
if (!cur_trans)
31473151
return 0;
31483152

@@ -3151,6 +3155,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root,
31513155
head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
31523156
if (!head) {
31533157
spin_unlock(&delayed_refs->lock);
3158+
btrfs_put_transaction(cur_trans);
31543159
return 0;
31553160
}
31563161

@@ -3167,6 +3172,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root,
31673172
mutex_lock(&head->mutex);
31683173
mutex_unlock(&head->mutex);
31693174
btrfs_put_delayed_ref_head(head);
3175+
btrfs_put_transaction(cur_trans);
31703176
return -EAGAIN;
31713177
}
31723178
spin_unlock(&delayed_refs->lock);
@@ -3199,6 +3205,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root,
31993205
}
32003206
spin_unlock(&head->lock);
32013207
mutex_unlock(&head->mutex);
3208+
btrfs_put_transaction(cur_trans);
32023209
return ret;
32033210
}
32043211

0 commit comments

Comments
 (0)