Commit e1489b4f authored by Ioannis Angelakopoulos's avatar Ioannis Angelakopoulos Committed by David Sterba

btrfs: add lockdep annotations for num_writers wait event

Annotate the num_writers wait event in fs/btrfs/transaction.c with
lockdep in order to catch deadlocks involving this wait event.
Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarIoannis Angelakopoulos <iangelak@fb.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent ab9a323f
...@@ -1092,6 +1092,12 @@ struct btrfs_fs_info { ...@@ -1092,6 +1092,12 @@ struct btrfs_fs_info {
/* Updates are not protected by any lock */ /* Updates are not protected by any lock */
struct btrfs_commit_stats commit_stats; struct btrfs_commit_stats commit_stats;
/*
* Annotations for transaction events (structures are empty when
* compiled without lockdep).
*/
struct lockdep_map btrfs_trans_num_writers_map;
#ifdef CONFIG_BTRFS_FS_REF_VERIFY #ifdef CONFIG_BTRFS_FS_REF_VERIFY
spinlock_t ref_verify_lock; spinlock_t ref_verify_lock;
struct rb_root block_tree; struct rb_root block_tree;
......
...@@ -2990,6 +2990,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) ...@@ -2990,6 +2990,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
mutex_init(&fs_info->zoned_data_reloc_io_lock); mutex_init(&fs_info->zoned_data_reloc_io_lock);
seqlock_init(&fs_info->profiles_lock); seqlock_init(&fs_info->profiles_lock);
btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
INIT_LIST_HEAD(&fs_info->space_info); INIT_LIST_HEAD(&fs_info->space_info);
INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
......
...@@ -313,6 +313,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info, ...@@ -313,6 +313,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
atomic_inc(&cur_trans->num_writers); atomic_inc(&cur_trans->num_writers);
extwriter_counter_inc(cur_trans, type); extwriter_counter_inc(cur_trans, type);
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
return 0; return 0;
} }
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
...@@ -334,16 +335,20 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info, ...@@ -334,16 +335,20 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
if (!cur_trans) if (!cur_trans)
return -ENOMEM; return -ENOMEM;
btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
spin_lock(&fs_info->trans_lock); spin_lock(&fs_info->trans_lock);
if (fs_info->running_transaction) { if (fs_info->running_transaction) {
/* /*
* someone started a transaction after we unlocked. Make sure * someone started a transaction after we unlocked. Make sure
* to redo the checks above * to redo the checks above
*/ */
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
kfree(cur_trans); kfree(cur_trans);
goto loop; goto loop;
} else if (BTRFS_FS_ERROR(fs_info)) { } else if (BTRFS_FS_ERROR(fs_info)) {
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
kfree(cur_trans); kfree(cur_trans);
return -EROFS; return -EROFS;
} }
...@@ -1022,6 +1027,9 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, ...@@ -1022,6 +1027,9 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
extwriter_counter_dec(cur_trans, trans->type); extwriter_counter_dec(cur_trans, trans->type);
cond_wake_up(&cur_trans->writer_wait); cond_wake_up(&cur_trans->writer_wait);
btrfs_lockdep_release(info, btrfs_trans_num_writers);
btrfs_put_transaction(cur_trans); btrfs_put_transaction(cur_trans);
if (current->journal_info == trans) if (current->journal_info == trans)
...@@ -1994,6 +2002,12 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) ...@@ -1994,6 +2002,12 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
if (cur_trans == fs_info->running_transaction) { if (cur_trans == fs_info->running_transaction) {
cur_trans->state = TRANS_STATE_COMMIT_DOING; cur_trans->state = TRANS_STATE_COMMIT_DOING;
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
/*
* The thread has already released the lockdep map as reader
* already in btrfs_commit_transaction().
*/
btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
wait_event(cur_trans->writer_wait, wait_event(cur_trans->writer_wait,
atomic_read(&cur_trans->num_writers) == 1); atomic_read(&cur_trans->num_writers) == 1);
...@@ -2222,7 +2236,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2222,7 +2236,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
btrfs_put_transaction(prev_trans); btrfs_put_transaction(prev_trans);
if (ret) if (ret)
goto cleanup_transaction; goto lockdep_release;
} else { } else {
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
} }
...@@ -2236,7 +2250,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2236,7 +2250,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
*/ */
if (BTRFS_FS_ERROR(fs_info)) { if (BTRFS_FS_ERROR(fs_info)) {
ret = -EROFS; ret = -EROFS;
goto cleanup_transaction; goto lockdep_release;
} }
} }
...@@ -2250,19 +2264,21 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2250,19 +2264,21 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
ret = btrfs_start_delalloc_flush(fs_info); ret = btrfs_start_delalloc_flush(fs_info);
if (ret) if (ret)
goto cleanup_transaction; goto lockdep_release;
ret = btrfs_run_delayed_items(trans); ret = btrfs_run_delayed_items(trans);
if (ret) if (ret)
goto cleanup_transaction; goto lockdep_release;
wait_event(cur_trans->writer_wait, wait_event(cur_trans->writer_wait,
extwriter_counter_read(cur_trans) == 0); extwriter_counter_read(cur_trans) == 0);
/* some pending stuffs might be added after the previous flush. */ /* some pending stuffs might be added after the previous flush. */
ret = btrfs_run_delayed_items(trans); ret = btrfs_run_delayed_items(trans);
if (ret) if (ret) {
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
goto cleanup_transaction; goto cleanup_transaction;
}
btrfs_wait_delalloc_flush(fs_info); btrfs_wait_delalloc_flush(fs_info);
...@@ -2284,6 +2300,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2284,6 +2300,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
add_pending_snapshot(trans); add_pending_snapshot(trans);
cur_trans->state = TRANS_STATE_COMMIT_DOING; cur_trans->state = TRANS_STATE_COMMIT_DOING;
spin_unlock(&fs_info->trans_lock); spin_unlock(&fs_info->trans_lock);
/*
* The thread has started/joined the transaction thus it holds the
* lockdep map as a reader. It has to release it before acquiring the
* lockdep map as a writer.
*/
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
wait_event(cur_trans->writer_wait, wait_event(cur_trans->writer_wait,
atomic_read(&cur_trans->num_writers) == 1); atomic_read(&cur_trans->num_writers) == 1);
...@@ -2515,6 +2539,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2515,6 +2539,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
cleanup_transaction(trans, ret); cleanup_transaction(trans, ret);
return ret; return ret;
lockdep_release:
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
goto cleanup_transaction;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment