Patchwork [07/26] jbd2: Fix race in t_outstanding_credits update in jbd2_journal_extend()

login
register
mail settings
Submitter Jan Kara
Date May 31, 2013, 9:42 a.m.
Message ID <1369993379-13017-8-git-send-email-jack@suse.cz>
Download mbox | patch
Permalink /patch/247880/
State Accepted
Headers show

Comments

Jan Kara - May 31, 2013, 9:42 a.m.
jbd2_journal_extend() first checked whether transaction can accept extending
handle with more credits and then added credits to t_outstanding_credits.
This can race with start_this_handle() adding another handle to a transaction
and thus overbooking a transaction. Make jbd2_journal_extend() use
atomic_add_return() to close the race.

Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/jbd2/transaction.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Theodore Ts'o - June 4, 2013, 4:23 p.m.
On Fri, May 31, 2013 at 11:42:40AM +0200, Jan Kara wrote:
> jbd2_journal_extend() first checked whether transaction can accept extending
> handle with more credits and then added credits to t_outstanding_credits.
> This can race with start_this_handle() adding another handle to a transaction
> and thus overbooking a transaction. Make jbd2_journal_extend() use
> atomic_add_return() to close the race.
> 
> Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
> Signed-off-by: Jan Kara <jack@suse.cz>

Thanks, applied.

					- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index dd89ad2..8cdf489 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -433,11 +433,13 @@  int jbd2_journal_extend(handle_t *handle, int nblocks)
 	}
 
 	spin_lock(&transaction->t_handle_lock);
-	wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
+	wanted = atomic_add_return(nblocks,
+				   &transaction->t_outstanding_credits);
 
 	if (wanted > journal->j_max_transaction_buffers) {
 		jbd_debug(3, "denied handle %p %d blocks: "
 			  "transaction too large\n", handle, nblocks);
+		atomic_sub(nblocks, &transaction->t_outstanding_credits);
 		goto unlock;
 	}
 
@@ -445,6 +447,7 @@  int jbd2_journal_extend(handle_t *handle, int nblocks)
 	    jbd2_log_space_left(journal)) {
 		jbd_debug(3, "denied handle %p %d blocks: "
 			  "insufficient log space\n", handle, nblocks);
+		atomic_sub(nblocks, &transaction->t_outstanding_credits);
 		goto unlock;
 	}
 
@@ -456,7 +459,6 @@  int jbd2_journal_extend(handle_t *handle, int nblocks)
 
 	handle->h_buffer_credits += nblocks;
 	handle->h_requested_credits += nblocks;
-	atomic_add(nblocks, &transaction->t_outstanding_credits);
 	result = 0;
 
 	jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);