Skip to content

Commit 1fa11e2

Browse files
Arne Jansenchrismason-xx
authored andcommitted
Btrfs: fix deadlock in wait_for_more_refs
Commit a168650 introduced a waiting mechanism to prevent busy waiting in btrfs_run_delayed_refs. This can deadlock with btrfs_run_ordered_operations, where a tree_mod_seq is held while waiting for the io to complete, while the end_io calls btrfs_run_delayed_refs. This whole mechanism is unnecessary. If not enough runnable refs are available to satisfy count, just return as count is more like a guideline than a strict requirement. In case we have to run all refs, commit transaction makes sure that no other threads are working in the transaction anymore, so we just assert here that no refs are blocked. Signed-off-by: Arne Jansen <sensille@gmx.net> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
1 parent 6209526 commit 1fa11e2

5 files changed

Lines changed: 21 additions & 73 deletions

File tree

‎fs/btrfs/ctree.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,6 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
420420
}
421421
spin_unlock(&fs_info->tree_mod_seq_lock);
422422

423-
/*
424-
* we removed the lowest blocker from the blocker list, so there may be
425-
* more processible delayed refs.
426-
*/
427-
wake_up(&fs_info->tree_mod_seq_wait);
428-
429423
/*
430424
* anything that's lower than the lowest existing (read: blocked)
431425
* sequence number can be removed from the tree.

‎fs/btrfs/ctree.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,6 @@ struct btrfs_fs_info {
12521252
atomic_t tree_mod_seq;
12531253
struct list_head tree_mod_seq_list;
12541254
struct seq_list tree_mod_seq_elem;
1255-
wait_queue_head_t tree_mod_seq_wait;
12561255

12571256
/* this protects tree_mod_log */
12581257
rwlock_t tree_mod_log_lock;

‎fs/btrfs/delayed-ref.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,6 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
662662
add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
663663
num_bytes, parent, ref_root, level, action,
664664
for_cow);
665-
if (!need_ref_seq(for_cow, ref_root) &&
666-
waitqueue_active(&fs_info->tree_mod_seq_wait))
667-
wake_up(&fs_info->tree_mod_seq_wait);
668665
spin_unlock(&delayed_refs->lock);
669666
if (need_ref_seq(for_cow, ref_root))
670667
btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
@@ -713,9 +710,6 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
713710
add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
714711
num_bytes, parent, ref_root, owner, offset,
715712
action, for_cow);
716-
if (!need_ref_seq(for_cow, ref_root) &&
717-
waitqueue_active(&fs_info->tree_mod_seq_wait))
718-
wake_up(&fs_info->tree_mod_seq_wait);
719713
spin_unlock(&delayed_refs->lock);
720714
if (need_ref_seq(for_cow, ref_root))
721715
btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
@@ -744,8 +738,6 @@ int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
744738
num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
745739
extent_op->is_data);
746740

747-
if (waitqueue_active(&fs_info->tree_mod_seq_wait))
748-
wake_up(&fs_info->tree_mod_seq_wait);
749741
spin_unlock(&delayed_refs->lock);
750742
return 0;
751743
}

‎fs/btrfs/disk-io.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,8 +2035,6 @@ int open_ctree(struct super_block *sb,
20352035
fs_info->free_chunk_space = 0;
20362036
fs_info->tree_mod_log = RB_ROOT;
20372037

2038-
init_waitqueue_head(&fs_info->tree_mod_seq_wait);
2039-
20402038
/* readahead state */
20412039
INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT);
20422040
spin_lock_init(&fs_info->reada_lock);

‎fs/btrfs/extent-tree.c‎

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,12 +2318,6 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
23182318
ref->in_tree = 0;
23192319
rb_erase(&ref->rb_node, &delayed_refs->root);
23202320
delayed_refs->num_entries--;
2321-
/*
2322-
* we modified num_entries, but as we're currently running
2323-
* delayed refs, skip
2324-
* wake_up(&delayed_refs->seq_wait);
2325-
* here.
2326-
*/
23272321
spin_unlock(&delayed_refs->lock);
23282322

23292323
ret = run_one_delayed_ref(trans, root, ref, extent_op,
@@ -2350,22 +2344,6 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
23502344
return count;
23512345
}
23522346

2353-
static void wait_for_more_refs(struct btrfs_fs_info *fs_info,
2354-
struct btrfs_delayed_ref_root *delayed_refs,
2355-
unsigned long num_refs,
2356-
struct list_head *first_seq)
2357-
{
2358-
spin_unlock(&delayed_refs->lock);
2359-
pr_debug("waiting for more refs (num %ld, first %p)\n",
2360-
num_refs, first_seq);
2361-
wait_event(fs_info->tree_mod_seq_wait,
2362-
num_refs != delayed_refs->num_entries ||
2363-
fs_info->tree_mod_seq_list.next != first_seq);
2364-
pr_debug("done waiting for more refs (num %ld, first %p)\n",
2365-
delayed_refs->num_entries, fs_info->tree_mod_seq_list.next);
2366-
spin_lock(&delayed_refs->lock);
2367-
}
2368-
23692347
#ifdef SCRAMBLE_DELAYED_REFS
23702348
/*
23712349
* Normally delayed refs get processed in ascending bytenr order. This
@@ -2460,13 +2438,11 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
24602438
struct btrfs_delayed_ref_root *delayed_refs;
24612439
struct btrfs_delayed_ref_node *ref;
24622440
struct list_head cluster;
2463-
struct list_head *first_seq = NULL;
24642441
int ret;
24652442
u64 delayed_start;
24662443
int run_all = count == (unsigned long)-1;
24672444
int run_most = 0;
2468-
unsigned long num_refs = 0;
2469-
int consider_waiting;
2445+
int loops;
24702446

24712447
/* We'll clean this up in btrfs_cleanup_transaction */
24722448
if (trans->aborted)
@@ -2484,7 +2460,7 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
24842460
delayed_refs = &trans->transaction->delayed_refs;
24852461
INIT_LIST_HEAD(&cluster);
24862462
again:
2487-
consider_waiting = 0;
2463+
loops = 0;
24882464
spin_lock(&delayed_refs->lock);
24892465

24902466
#ifdef SCRAMBLE_DELAYED_REFS
@@ -2512,31 +2488,6 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
25122488
if (ret)
25132489
break;
25142490

2515-
if (delayed_start >= delayed_refs->run_delayed_start) {
2516-
if (consider_waiting == 0) {
2517-
/*
2518-
* btrfs_find_ref_cluster looped. let's do one
2519-
* more cycle. if we don't run any delayed ref
2520-
* during that cycle (because we can't because
2521-
* all of them are blocked) and if the number of
2522-
* refs doesn't change, we avoid busy waiting.
2523-
*/
2524-
consider_waiting = 1;
2525-
num_refs = delayed_refs->num_entries;
2526-
first_seq = root->fs_info->tree_mod_seq_list.next;
2527-
} else {
2528-
wait_for_more_refs(root->fs_info, delayed_refs,
2529-
num_refs, first_seq);
2530-
/*
2531-
* after waiting, things have changed. we
2532-
* dropped the lock and someone else might have
2533-
* run some refs, built new clusters and so on.
2534-
* therefore, we restart staleness detection.
2535-
*/
2536-
consider_waiting = 0;
2537-
}
2538-
}
2539-
25402491
ret = run_clustered_refs(trans, root, &cluster);
25412492
if (ret < 0) {
25422493
spin_unlock(&delayed_refs->lock);
@@ -2549,9 +2500,26 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
25492500
if (count == 0)
25502501
break;
25512502

2552-
if (ret || delayed_refs->run_delayed_start == 0) {
2503+
if (delayed_start >= delayed_refs->run_delayed_start) {
2504+
if (loops == 0) {
2505+
/*
2506+
* btrfs_find_ref_cluster looped. let's do one
2507+
* more cycle. if we don't run any delayed ref
2508+
* during that cycle (because we can't because
2509+
* all of them are blocked), bail out.
2510+
*/
2511+
loops = 1;
2512+
} else {
2513+
/*
2514+
* no runnable refs left, stop trying
2515+
*/
2516+
BUG_ON(run_all);
2517+
break;
2518+
}
2519+
}
2520+
if (ret) {
25532521
/* refs were run, let's reset staleness detection */
2554-
consider_waiting = 0;
2522+
loops = 0;
25552523
}
25562524
}
25572525

@@ -5296,9 +5264,6 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
52965264
rb_erase(&head->node.rb_node, &delayed_refs->root);
52975265

52985266
delayed_refs->num_entries--;
5299-
smp_mb();
5300-
if (waitqueue_active(&root->fs_info->tree_mod_seq_wait))
5301-
wake_up(&root->fs_info->tree_mod_seq_wait);
53025267

53035268
/*
53045269
* we don't take a ref on the node because we're removing it from the

0 commit comments

Comments
 (0)