Btrfs: avoid taking the trans_mutex in btrfs_end_transaction
PATCH 1/1 Btrfs: avoid taking the trans_mutex in btrfs_end_transaction
I've been working on making our O_DIRECT latency not suck and I noticed we were taking the trans_mutex in btrfs_end_transaction. So to do this we convert num_writers and use_count to atomic_t's and just decrement them in btrfs_end_transaction. I got rid of the put_transaction() in btrfs_end_transaction() since we will never free the transaction from btrfs_end_transaction(). Tested this with xfstests and everything went smoothly. Thanks,
--- fs/btrfs/disk-io.c | 2 +- fs/btrfs/transaction.c | 42 +++++++++++++++++++++--------------------- fs/btrfs/transaction.h | 4 ++-- 3 files changed, 24 insertions(+), 24 deletions(-)
fs/btrfs/disk-io.c +1 -1
@@ -3057,7 +3057,7 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root)btrfs_destroy_pinned_extent (root, root->fs_info->pinned_extents); - t->use_count =0 ; +atomic_set (&t->use_count,0 );list_del_init (&t->list);memset (t,0 ,sizeof (*t));kmem_cache_free (btrfs_transaction_cachep, t);
fs/btrfs/transaction.c +21 -21
126 lines not on this page · raw message
fs/btrfs/transaction.h +2 -2
@@ -27,11 +27,11 @@ struct btrfs_transaction {* total writers in this transaction, it must be zero before the * transaction can end */ -unsigned long num_writers; +atomic_t num_writers;unsigned long num_joined;int in_commit; -int use_count; +atomic_t use_count;int commit_done;int blocked;struct list_head list;
Signature · 1 line
1.7.2.3
Excerpts from Josef Bacik's message of 2011-04-11 15:49:17 -0400:
Quoted 7 lines
I've been working on making our
O_DIRECTlatency not suck and I noticed we were taking thetrans_mutexinbtrfs_end_transaction. So to do this we convertnum_writersanduse_countto atomic_t's and just decrement them inbtrfs_end_transaction. I got rid of theput_transaction()inbtrfs_end_transaction()since we will never free the transaction frombtrfs_end_transaction(). Tested this with xfstests and everything went smoothly. Thanks,
Hmmm, this is smart, there's no reason to take the lock here. But there's one little problem:
fs/btrfs/transaction.c static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, · line 470
Quoted 11 lines
fs/btrfs/transaction.c +2
-4@@ -466,19 +465,20 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,wake_up_process (info->transaction_kthread); } -if (lock) -mutex_lock (&info->trans_mutex);WARN_ON (cur_trans != info->running_transaction); -WARN_ON (cur_trans->num_writers <1 ); - cur_trans->num_writers--; +WARN_ON (atomic_read (&cur_trans->num_writers) <1 ); +atomic_dec (&cur_trans->num_writers);
We've just decremented num_writers, which could have been the only thing preventing a commit from finishing. The entire commit could finish at any time after this line.
Quoted 4 lines
fs/btrfs/transaction.c +0
-0smp_mb ();if (waitqueue_active (&cur_trans->writer_wait))wake_up (&cur_trans->writer_wait);
fs/btrfs/transaction.c static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, · line 481
Quoted 10 lines
fs/btrfs/transaction.c +6
-3-put_transaction (cur_trans); -if (lock) -mutex_unlock (&info->trans_mutex); +/* +* A trans handle will never actually be putting the last reference of a +* transaction, so just dec the use count to avoid taking the trans +* mutex. +*/ +atomic_dec (&cur_trans->use_count);
Which could make this the last and final reference on cur_trans->use_count.
There's no reason we need the lock in put_transaction, other than manipulating the list of running transactions. But I don't see any reason we can't delete ourselves from the list when the commit is done, which would let you keep a lock less put_transaction.
-chris