jbd2: Optimize jbd2_log_do_checkpoint() a bit
PATCH 1/1 jbd2: Optimize jbd2_log_do_checkpoint() a bit
When we discover written out buffer in transaction checkpoint list we don't have to recheck validity of a transaction. Either this is the last buffer in a transaction - and then we are done - or this isn't and then we can just take another buffer from the checkpoint list without dropping j_list_lock.
--- fs/jbd2/checkpoint.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
fs/jbd2/checkpoint.c +8 -5
@@ -343,12 +343,15 @@ restart:if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ; -get_bh (bh);BUFFER_TRACE (bh,"remove from checkpoint" ); -__jbd2_journal_remove_checkpoint (jh); -spin_unlock (&journal->j_list_lock); -__brelse (bh); - goto retry; +if (__jbd2_journal_remove_checkpoint (jh)) { +/* +* This was the last buffer attached to the +* transaction. We are done. +*/ + goto out; + } +continue ; }/* * Important: we are about to write the buffer, and
Signature · 1 line
1.8.1.4
Theodore Ts'o
maintainerQuoted 16 lines · Jan Kara
When we discover written out buffer in transaction checkpoint list we don't have to recheck validity of a transaction. Either this is the last buffer in a transaction - and then we are done - or this isn't and then we can just take another buffer from the checkpoint list without dropping
j_list_lock.Signed-off-by Jan Kara <[email protected]>--- fs/jbd2/checkpoint.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)fs/jbd2/checkpoint.c +0
-0
fs/jbd2/checkpoint.c restart: · line 350
Quoted 8 lines
fs/jbd2/checkpoint.c +0
-4@@ -343,12 +343,15 @@ restart:if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ; -get_bh (bh);BUFFER_TRACE (bh,"remove from checkpoint" ); -__jbd2_journal_remove_checkpoint (jh); -spin_unlock (&journal->j_list_lock); -__brelse (bh);
Currently, all of the places which call __jbd2_jouranl_remove_checkpoint(jh) are doing so with an elevated b_count. For example, see __try_to_free_cp_buf().
After doing a lot of desk checking, I can't see any reason for holding the elevanted b_count, so I think it should be to remove it, but then we can simplify the other uses __try_to_free_cp_buf(). For example, in the loop that I folded from __wait_cp_io, we could drop the done variable and change:
done = __jbd2_journal_remove_checkpoint(jh); __brelse(bh);
to this:
__brelse(bh); if (__jbd2_journal_remove_checkpoint(jh)) break;
How much testing have you done of this optimization? I'm tempted to try nuking all of the elevated b_counts around the call to __jbd2_journal_remove_checkpoint(), and then doing a test to see if anything blows up.
Cheers,
- Ted
Theodore Ts'o
maintainerPATCH 1/2 jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()
The __jbd2_journal_remove_checkpoint() doesn't require an elevated b_count; indeed, until the jh structure gets released by the call to jbd2_journal_put_journal_head(), the bh's b_count is elevated by virtue of the existence of the jh structure.
--- fs/jbd2/checkpoint.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
fs/jbd2/checkpoint.c +4 -8
@@ -100,11 +100,8 @@ static int __try_to_free_cp_buf(struct journal_head *jh)* Get our reference so that bh cannot be freed before * we unlock it */ -get_bh (bh);JBUFFER_TRACE (jh,"remove from checkpoint list" ); ret =__jbd2_journal_remove_checkpoint (jh) +1 ; -BUFFER_TRACE (bh,"release" ); -__brelse (bh); }return ret; }
@@ -216,7 +213,7 @@ int jbd2_log_do_checkpoint(journal_t *journal)struct buffer_head *bh;transaction_t *transaction;tid_t this_tid; -int result, batch_count =0 , done =0 ; +int result, batch_count =0 ;jbd_debug (1 ,"Start checkpoint\n" );
@@ -291,11 +288,9 @@ restart:if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ; -get_bh (bh);BUFFER_TRACE (bh,"remove from checkpoint" );__jbd2_journal_remove_checkpoint (jh);spin_unlock (&journal->j_list_lock); -__brelse (bh); goto retry; }/*
@@ -338,7 +333,7 @@ restart2: transaction->t_tid != this_tid) goto out; -while (!done && transaction->t_checkpoint_io_list) { +while (transaction->t_checkpoint_io_list) { jh = transaction->t_checkpoint_io_list; bh =jh2bh (jh);get_bh (bh);
@@ -359,8 +354,9 @@ restart2:* know that it has been written out and so we can * drop it from the list */ - done =__jbd2_journal_remove_checkpoint (jh);__brelse (bh); +if (__jbd2_journal_remove_checkpoint (jh)) +break ; } out:spin_unlock (&journal->j_list_lock);
Signature · 1 line
2.1.0
Theodore Ts'o
maintainerPATCH 2/2 jbd2: optimize jbd2_log_do_checkpoint() a bit
From: Jan Kara <[email protected]>
When we discover written out buffer in transaction checkpoint list we don't have to recheck validity of a transaction. Either this is the last buffer in a transaction - and then we are done - or this isn't and then we can just take another buffer from the checkpoint list without dropping j_list_lock.
--- fs/jbd2/checkpoint.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/jbd2/checkpoint.c +4 -3
@@ -289,9 +289,10 @@ restart:if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ;BUFFER_TRACE (bh,"remove from checkpoint" ); -__jbd2_journal_remove_checkpoint (jh); -spin_unlock (&journal->j_list_lock); - goto retry; +if (__jbd2_journal_remove_checkpoint (jh)) +/* The transaction was released; we're done */ + goto out; +continue ; }/* * Important: we are about to write the buffer, and
Signature · 1 line
2.1.0
Re: [PATCH 1/2] jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()
In reply to Theodore Ts'o<resend due to malformed email>
Quoted 16 lines · Theodore Ts'o
The
__jbd2_journal_remove_checkpoint()doesn't require an elevatedb_count; indeed, until the jh structure gets released by the call tojbd2_journal_put_journal_head(), the bh'sb_countis elevated by virtue of the existence of the jh structure.Suggested-by Jan Kara <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>--- fs/jbd2/checkpoint.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)fs/jbd2/checkpoint.c +0
-0
fs/jbd2/checkpoint.c static int __try_to_free_cp_buf(struct journal_head *jh) · line 102
Quoted 3 lines
fs/jbd2/checkpoint.c +0
-0@@ -100,11 +100,8 @@ static int __try_to_free_cp_buf(struct journal_head *jh)* Get our reference so that bh cannot be freed before * we unlock it */
I guess you need drop those comments as well.
--yliu
Quoted 7 lines
fs/jbd2/checkpoint.c +0
-3-get_bh (bh);JBUFFER_TRACE (jh,"remove from checkpoint list" ); ret =__jbd2_journal_remove_checkpoint (jh) +1 ; -BUFFER_TRACE (bh,"release" ); -__brelse (bh); }return ret;
Re: [PATCH 1/2] jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()
In reply to Theodore Ts'oQuoted 7 lines · Ted Tso
The
__jbd2_journal_remove_checkpoint()doesn't require an elevatedb_count; indeed, until the jh structure gets released by the call tojbd2_journal_put_journal_head(), the bh'sb_countis elevated by virtue of the existence of the jh structure.Suggested-by Jan Kara <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>
Looks good so you can add:
Just we can do a bit more as I mentioned in my other email:
fs/jbd2/checkpoint.c restart2: · line 357
Quoted 6 lines
fs/jbd2/checkpoint.c +0
-1@@ -359,8 +354,9 @@ restart2:* know that it has been written out and so we can * drop it from the list */ - done =__jbd2_journal_remove_checkpoint (jh);__brelse (bh);
Here we don't need to grab bh reference unless we are going to callwait_on_buffer(). Which moves get_bh / __brelse out of fast path.
fs/jbd2/checkpoint.c restart2: · line 362
Quoted 5 lines
fs/jbd2/checkpoint.c +2
-0+if (__jbd2_journal_remove_checkpoint (jh)) +break ; } out:spin_unlock (&journal->j_list_lock);
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerPATCH 1/1 jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()
The __jbd2_journal_remove_checkpoint() doesn't require an elevated b_count; indeed, until the jh structure gets released by the call to jbd2_journal_put_journal_head(), the bh's b_count is elevated by virtue of the existence of the jh structure.
--- fs/jbd2/checkpoint.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-)
fs/jbd2/checkpoint.c +5 -14
@@ -96,15 +96,8 @@ static int __try_to_free_cp_buf(struct journal_head *jh)if (jh->b_transaction ==NULL && !buffer_locked (bh) && !buffer_dirty (bh) && !buffer_write_io_error (bh)) { -/* -* Get our reference so that bh cannot be freed before -* we unlock it -*/ -get_bh (bh);JBUFFER_TRACE (jh,"remove from checkpoint list" ); ret =__jbd2_journal_remove_checkpoint (jh) +1 ; -BUFFER_TRACE (bh,"release" ); -__brelse (bh); }return ret; }
@@ -216,7 +209,7 @@ int jbd2_log_do_checkpoint(journal_t *journal)struct buffer_head *bh;transaction_t *transaction;tid_t this_tid; -int result, batch_count =0 , done =0 ; +int result, batch_count =0 ;jbd_debug (1 ,"Start checkpoint\n" );
@@ -291,11 +284,9 @@ restart:if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ; -get_bh (bh);BUFFER_TRACE (bh,"remove from checkpoint" );__jbd2_journal_remove_checkpoint (jh);spin_unlock (&journal->j_list_lock); -__brelse (bh); goto retry; }/*
@@ -338,12 +329,12 @@ restart2: transaction->t_tid != this_tid) goto out; -while (!done && transaction->t_checkpoint_io_list) { +while (transaction->t_checkpoint_io_list) { jh = transaction->t_checkpoint_io_list; bh =jh2bh (jh); -get_bh (bh);if (buffer_locked (bh)) {spin_unlock (&journal->j_list_lock); +get_bh (bh);wait_on_buffer (bh);/* the journal_head may have gone by now */ BUFFER_TRACE (bh,"brelse" );
@@ -359,8 +350,8 @@ restart2:* know that it has been written out and so we can * drop it from the list */ - done =__jbd2_journal_remove_checkpoint (jh); -__brelse (bh); +if (__jbd2_journal_remove_checkpoint (jh)) +break ; } out:spin_unlock (&journal->j_list_lock);
Signature · 1 line
2.1.0
A message that is not in the archive (sent off-list or not yet received): CAGZGoEfjAZuOAbp-AqA-kiL2aTwiRXDO_Di=LPDovRBDNSn5dw@mail.gmail.com
Theodore Ts'o
maintainerRe: [PATCH 1/2] jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()
Quoted 5 lines · Yuanhan Liu
Quoted 3 lines
* Get our reference so that bh cannot be freed before
* we unlock it
*/I guess you need remove those comments as well.
Good catch, thanks.
- Ted
Quoted 21 lines · Ted Tso
Quoted 20 lines · Jan Kara
When we discover written out buffer in transaction checkpoint list we don't have to recheck validity of a transaction. Either this is the last buffer in a transaction - and then we are done - or this isn't and then we can just take another buffer from the checkpoint list without dropping
j_list_lock.Signed-off-by Jan Kara <[email protected]>--- fs/jbd2/checkpoint.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)fs/jbd2/checkpoint.c +0
-1@@ -343,12 +343,15 @@ restart:if (!buffer_dirty (bh)) {if (unlikely (buffer_write_io_error (bh)) && !result) result = -EIO ; -get_bh (bh);
Quoted 8 lines
fs/jbd2/checkpoint.c restart: · line 350
Quoted 4 lines
fs/jbd2/checkpoint.c +0
-3BUFFER_TRACE (bh,"remove from checkpoint" ); -__jbd2_journal_remove_checkpoint (jh); -spin_unlock (&journal->j_list_lock); -__brelse (bh);Currently, all of the places which call
__jbd2_jouranl_remove_checkpoint(jh)are doing so with an elevatedb_count. For example, see__try_to_free_cp_buf().
I did a bit of archeology and commit932bb305ba2a01cd62809644d569f004e77a4355 removed the need to hold buffer reference when calling __jbd2_journal_remove_checkpoint(). So it should be safe to remove that reference handling also from __try_to_free_cp_buf().
Quoted 6 lines
After doing a lot of desk checking, I can't see any reason for holding the elevanted
b_count, so I think it should be to remove it, but then we can simplify the other uses__try_to_free_cp_buf(). For example, in the loop that I folded from__wait_cp_io, we could drop the done variable and change:
Quoted 8 lines
done = __jbd2_journal_remove_checkpoint(jh); __brelse(bh);to this:
__brelse(bh); if (__jbd2_journal_remove_checkpoint(jh)) break;
Well, we don't even need to grab bh reference unless we find the buffer
is locked and are going to wait for it. And yes, we can get rid of that 'done' variable.
Quoted 4 lines
How much testing have you done of this optimization? I'm tempted to try nuking all of the elevated
b_countsaround the call to__jbd2_journal_remove_checkpoint(), and then doing a test to see if anything blows up.
Honestly, I didn't test much but I'm pretty confident we are safe to
remove those bh refs ;)
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR