ext4: fix deadlock while checkpoint thread waits commit thread to finish
PATCH 1/1 ext4: fix deadlock while checkpoint thread waits commit thread to finish
This issue was found when I tried to put checkpoint work in a separate thread, the deadlock below happened:
Thread1 | Thread2
__jbd2_log_wait_for_space |
jbd2_log_do_checkpoint (hold j_checkpoint_mutex)|
if (jh->b_transaction != NULL) |
... |
jbd2_log_start_commit(journal, tid); |jbd2_update_log_tail
| will lock j_checkpoint_mutex,
| but will be blocked here.
|
jbd2_log_wait_commit(journal, tid); |
wait_event(journal->j_wait_done_commit, |
!tid_gt(tid, journal->j_commit_sequence)); |
... |wake_up(j_wait_done_commit)
} |then deadlock occurs, Thread1 will never be waken up.
To fix this issue, drop j_checkpoint_mutex in jbd2_log_do_checkpoint() when we are going to wait for transaction commit.
--- fs/jbd2/checkpoint.c | 21 +++++++++++++++++---- fs/jbd2/journal.c | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-)
fs/jbd2/checkpoint.c +17 -4
@@ -113,7 +113,7 @@ void __jbd2_log_wait_for_space(journal_t *journal) nblocks =jbd2_space_needed (journal);while (jbd2_log_space_left (journal) < nblocks) {write_unlock (&journal->j_state_lock); -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);/* * Test again, another process may have checkpointed while we
@@ -241,8 +241,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)* done (maybe it's a new transaction, but it fell at the same * address). */ -if (journal->j_checkpoint_transactions != transaction || - transaction->t_tid != this_tid) +if (journal->j_checkpoint_transactions ==NULL || + journal->j_checkpoint_transactions->t_tid != this_tid) goto out;/* checkpoint all of the transaction's buffers */
@@ -276,9 +276,22 @@ int jbd2_log_do_checkpoint(journal_t *journal)"JBD2: %s: Waiting for Godot: block %llu\n" , journal->j_devname, (unsigned long long) bh->b_blocknr); +if (batch_count) +__flush_batch (journal, &batch_count);jbd2_log_start_commit (journal, tid); +/* +* jbd2_journal_commit_transaction() may want +* to take the checkpoint_mutex if JBD2_FLUSHED +* is set, jbd2_update_log_tail() called by +* jbd2_journal_commit_transaction() may also take +* checkpoint_mutex. So we need to temporarily +* drop it. +*/ +mutex_unlock (&journal->j_checkpoint_mutex);jbd2_log_wait_commit (journal, tid); - goto retry; +mutex_lock_io (&journal->j_checkpoint_mutex); +spin_lock (&journal->j_list_lock); + goto restart; }if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result)
fs/jbd2/journal.c +1 -1
@@ -2067,7 +2067,7 @@ int jbd2_journal_wipe(journal_t *journal, int write) err =jbd2_journal_skip_recovery (journal);if (write) {/* Lock to make assertions happy... */ -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);jbd2_mark_journal_empty (journal,REQ_SYNC |REQ_FUA );mutex_unlock (&journal->j_checkpoint_mutex); }
Signature · 1 line
2.17.2
Quoted 17 lines · Xiaoguang Wang
This issue was found when I tried to put checkpoint work in a separate thread, the deadlock below happened:
Thread1 | Thread2 __jbd2_log_wait_for_space | jbd2_log_do_checkpoint (hold j_checkpoint_mutex)| if (jh->b_transaction != NULL) | ... | jbd2_log_start_commit(journal, tid); |jbd2_update_log_tail | will lock j_checkpoint_mutex, | but will be blocked here. | jbd2_log_wait_commit(journal, tid); | wait_event(journal->j_wait_done_commit, | !tid_gt(tid, journal->j_commit_sequence)); | ... |wake_up(j_wait_done_commit) } |
Quoted 6 lines
then deadlock occurs, Thread1 will never be waken up.
To fix this issue, drop
j_checkpoint_mutexinjbd2_log_do_checkpoint()when we are going to wait for transaction commit.Signed-off-by Xiaoguang Wang <[email protected]>
Thanks for the patch! One comment below...
Quoted 14 lines
fs/jbd2/checkpoint.c +1
-1@@ -113,7 +113,7 @@ void __jbd2_log_wait_for_space(journal_t *journal) nblocks =jbd2_space_needed (journal);while (jbd2_log_space_left (journal) < nblocks) {write_unlock (&journal->j_state_lock); -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);/* * Test again, another process may have checkpointed while we
fs/jbd2/checkpoint.c int jbd2_log_do_checkpoint(journal_t *journal) · line 246
Quoted 8 lines
fs/jbd2/checkpoint.c +2
-2@@ -241,8 +241,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)* done (maybe it's a new transaction, but it fell at the same * address). */ -if (journal->j_checkpoint_transactions != transaction || - transaction->t_tid != this_tid) +if (journal->j_checkpoint_transactions ==NULL || + journal->j_checkpoint_transactions->t_tid != this_tid) goto out;
Why did you change this? As far as I can tell there's no difference and the previous condition makes it more obvious that we are still looking at the same transaction.
Otherwise the patch looks good so after removing the above hunk feel free to add:
Honza
Quoted 40 lines
fs/jbd2/checkpoint.c +14
-1@@ -276,9 +276,22 @@ int jbd2_log_do_checkpoint(journal_t *journal)"JBD2: %s: Waiting for Godot: block %llu\n" , journal->j_devname, (unsigned long long) bh->b_blocknr); +if (batch_count) +__flush_batch (journal, &batch_count);jbd2_log_start_commit (journal, tid); +/* +* jbd2_journal_commit_transaction() may want +* to take the checkpoint_mutex if JBD2_FLUSHED +* is set, jbd2_update_log_tail() called by +* jbd2_journal_commit_transaction() may also take +* checkpoint_mutex. So we need to temporarily +* drop it. +*/ +mutex_unlock (&journal->j_checkpoint_mutex);jbd2_log_wait_commit (journal, tid); - goto retry; +mutex_lock_io (&journal->j_checkpoint_mutex); +spin_lock (&journal->j_list_lock); + goto restart; }if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result)fs/jbd2/journal.c +1
-1@@ -2067,7 +2067,7 @@ int jbd2_journal_wipe(journal_t *journal, int write) err =jbd2_journal_skip_recovery (journal);if (write) {/* Lock to make assertions happy... */ -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);jbd2_mark_journal_empty (journal,REQ_SYNC |REQ_FUA );mutex_unlock (&journal->j_checkpoint_mutex); }Signature · 1 line
2.17.2
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
hi,
Quoted 38 lines · Xiaoguang Wang
Quoted 17 lines
This issue was found when I tried to put checkpoint work in a separate thread, the deadlock below happened:
Thread1 | Thread2__jbd2_log_wait_for_space|jbd2_log_do_checkpoint(hold j_checkpoint_mutex)|if (jh->b_transaction != NULL) | ... | jbd2_log_start_commit(journal, tid); |jbd2_update_log_tail | will lock j_checkpoint_mutex, | but will be blocked here. | jbd2_log_wait_commit(journal, tid); | wait_event(journal->j_wait_done_commit, | !tid_gt(tid, journal->j_commit_sequence)); | ... |wake_up(j_wait_done_commit) } |Quoted 6 lines
then deadlock occurs, Thread1 will never be waken up.
To fix this issue, drop
j_checkpoint_mutexinjbd2_log_do_checkpoint()when we are going to wait for transaction commit.Signed-off-by Xiaoguang Wang <[email protected]>Thanks for the patch! One comment below...
Quoted 11 lines
fs/jbd2/checkpoint.c +1
-1@@ -113,7 +113,7 @@ void __jbd2_log_wait_for_space(journal_t *journal) nblocks =jbd2_space_needed (journal);while (jbd2_log_space_left (journal) < nblocks) {write_unlock (&journal->j_state_lock); -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);
Quoted 15 lines
fs/jbd2/checkpoint.c int jbd2_log_do_checkpoint(journal_t *journal) · line 246
Quoted 11 lines
fs/jbd2/checkpoint.c +2
-2/* * Test again, another process may have checkpointed while we @@ -241,8 +241,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)* done (maybe it's a new transaction, but it fell at the same * address). */ -if (journal->j_checkpoint_transactions != transaction || - transaction->t_tid != this_tid) +if (journal->j_checkpoint_transactions ==NULL || + journal->j_checkpoint_transactions->t_tid != this_tid) goto out;Why did you change this? As far as I can tell there's no difference and the previous condition makes it more obvious that we are still looking at the same transaction.
In this patch, we may drop j_checkpoint_mutex, then another thread may acquire this lock, do checkpoint work and freed current transaction, "transaction->t_tid" will cause an invalid pointer dereference.
Regards,
Xiaoguang Wang
Quoted 49 lines
Otherwise the patch looks good so after removing the above hunk feel free to add:
Reviewed-by Jan Kara <[email protected]>Honza
Quoted 40 lines
fs/jbd2/checkpoint.c +14
-1@@ -276,9 +276,22 @@ int jbd2_log_do_checkpoint(journal_t *journal)"JBD2: %s: Waiting for Godot: block %llu\n" , journal->j_devname, (unsigned long long) bh->b_blocknr); +if (batch_count) +__flush_batch (journal, &batch_count);jbd2_log_start_commit (journal, tid); +/* +* jbd2_journal_commit_transaction() may want +* to take the checkpoint_mutex if JBD2_FLUSHED +* is set, jbd2_update_log_tail() called by +* jbd2_journal_commit_transaction() may also take +* checkpoint_mutex. So we need to temporarily +* drop it. +*/ +mutex_unlock (&journal->j_checkpoint_mutex);jbd2_log_wait_commit (journal, tid); - goto retry; +mutex_lock_io (&journal->j_checkpoint_mutex); +spin_lock (&journal->j_list_lock); + goto restart; }if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result)fs/jbd2/journal.c +1
-1@@ -2067,7 +2067,7 @@ int jbd2_journal_wipe(journal_t *journal, int write) err =jbd2_journal_skip_recovery (journal);if (write) {/* Lock to make assertions happy... */ -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);jbd2_mark_journal_empty (journal,REQ_SYNC |REQ_FUA );mutex_unlock (&journal->j_checkpoint_mutex); }Signature · 1 line
2.17.2
Quoted 54 lines · Xiaoguang Wang
hi,
Quoted 44 lines · Xiaoguang Wang
Quoted 17 lines
This issue was found when I tried to put checkpoint work in a separate thread, the deadlock below happened:
Thread1 | Thread2__jbd2_log_wait_for_space|jbd2_log_do_checkpoint(hold j_checkpoint_mutex)|if (jh->b_transaction != NULL) | ... | jbd2_log_start_commit(journal, tid); |jbd2_update_log_tail | will lock j_checkpoint_mutex, | but will be blocked here. | jbd2_log_wait_commit(journal, tid); | wait_event(journal->j_wait_done_commit, | !tid_gt(tid, journal->j_commit_sequence)); | ... |wake_up(j_wait_done_commit) } |Quoted 6 lines
then deadlock occurs, Thread1 will never be waken up.
To fix this issue, drop
j_checkpoint_mutexinjbd2_log_do_checkpoint()when we are going to wait for transaction commit.Signed-off-by Xiaoguang Wang <[email protected]>Thanks for the patch! One comment below...
Quoted 17 lines
fs/jbd2/checkpoint.c +1
-2@@ -113,7 +113,7 @@ void __jbd2_log_wait_for_space(journal_t *journal) nblocks =jbd2_space_needed (journal);while (jbd2_log_space_left (journal) < nblocks) {write_unlock (&journal->j_state_lock); -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);/* * Test again, another process may have checkpointed while we @@ -241,8 +241,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)* done (maybe it's a new transaction, but it fell at the same * address). */ - if (journal->j_checkpoint_transactions != transaction ||Quoted 8 lines
fs/jbd2/checkpoint.c int jbd2_log_do_checkpoint(journal_t *journal) · line 246
Quoted 4 lines
fs/jbd2/checkpoint.c +2
-1- transaction->t_tid != this_tid) +if (journal->j_checkpoint_transactions ==NULL || + journal->j_checkpoint_transactions->t_tid != this_tid) goto out;Why did you change this? As far as I can tell there's no difference and the previous condition makes it more obvious that we are still looking at the same transaction.
Quoted 3 lines
In this patch, we may drop
j_checkpoint_mutex, then another thread may acquire this lock, do checkpoint work and freed current transaction, "transaction->t_tid" will cause an invalid pointer dereference.
That is exactly the reason why we check:
if (journal->j_checkpoint_transactions != transaction || ...
So if this test is false and so transaction->t_tid != this_tid gets evaluated we are sure that j_checkpoint_transactions actually still points to our transaction.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 59 lines · Xiaoguang Wang
Quoted 54 lines
hi,
Quoted 44 lines · Xiaoguang Wang
Quoted 17 lines
This issue was found when I tried to put checkpoint work in a separate thread, the deadlock below happened:
Thread1 | Thread2__jbd2_log_wait_for_space|jbd2_log_do_checkpoint(hold j_checkpoint_mutex)|if (jh->b_transaction != NULL) | ... | jbd2_log_start_commit(journal, tid); |jbd2_update_log_tail | will lock j_checkpoint_mutex, | but will be blocked here. | jbd2_log_wait_commit(journal, tid); | wait_event(journal->j_wait_done_commit, | !tid_gt(tid, journal->j_commit_sequence)); | ... |wake_up(j_wait_done_commit) } |Quoted 6 lines
then deadlock occurs, Thread1 will never be waken up.
To fix this issue, drop
j_checkpoint_mutexinjbd2_log_do_checkpoint()when we are going to wait for transaction commit.Signed-off-by Xiaoguang Wang <[email protected]>Thanks for the patch! One comment below...
Quoted 17 lines
fs/jbd2/checkpoint.c +1
-2@@ -113,7 +113,7 @@ void __jbd2_log_wait_for_space(journal_t *journal) nblocks =jbd2_space_needed (journal);while (jbd2_log_space_left (journal) < nblocks) {write_unlock (&journal->j_state_lock); -mutex_lock (&journal->j_checkpoint_mutex); +mutex_lock_io (&journal->j_checkpoint_mutex);/* * Test again, another process may have checkpointed while we @@ -241,8 +241,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)* done (maybe it's a new transaction, but it fell at the same * address). */ - if (journal->j_checkpoint_transactions != transaction ||Quoted 8 lines
fs/jbd2/checkpoint.c int jbd2_log_do_checkpoint(journal_t *journal) · line 246
Quoted 4 lines
fs/jbd2/checkpoint.c +2
-1- transaction->t_tid != this_tid) +if (journal->j_checkpoint_transactions ==NULL || + journal->j_checkpoint_transactions->t_tid != this_tid) goto out;Why did you change this? As far as I can tell there's no difference and the previous condition makes it more obvious that we are still looking at the same transaction.
Quoted 3 lines
In this patch, we may drop
j_checkpoint_mutex, then another thread may acquire this lock, do checkpoint work and freed current transaction, "transaction->t_tid" will cause an invalid pointer dereference.
Quoted 7 lines
That is exactly the reason why we check:
if (
journal->j_checkpoint_transactions!= transaction || ...So if this test is false and so
transaction->t_tid!=this_tidgets evaluated we are sure thatj_checkpoint_transactionsactually still points to our transaction.
I just realize that "journal->j_checkpoint_transactions != transaction" returns false, we can make sure that transaction is valid, thanks. I'll send a patch v2 soon.
Regards,
Xiaoguang Wang
Quoted 3 lines
Honza