Skip to contentSkip to conversation

jbd2: Optimize jbd2_log_do_checkpoint() a bit

PATCH In review Jan Kara 3 participants 9 messages Sep 2, 2014 – Sep 3, 2014 1 message not in the archive Journalling layer for block devices (JBD2)

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

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
index 993a187527f3..3722e2e53638 100644
@@ -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
maintainer
#1 lore raw
Quoted 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.

---
 fs/jbd2/checkpoint.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
fs/jbd2/checkpoint.c +0 -0
index 993a187527f3..3722e2e53638 100644

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

maintainer
#2 lore raw

PATCH 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
index 22fcd50..cb6e17c 100644
@@ -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
maintainer
#3 lore raw

PATCH 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
index cb6e17c..7713f94 100644
@@ -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
#4 lore raw

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 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 +0 -0
index 22fcd50..cb6e17c 100644

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;
author Reviewed-by on v1 1/2
#5 lore raw

Re: [PATCH 1/2] jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()

In reply to Theodore Ts'o
Quoted 7 lines · Ted Tso

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.

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 call
wait_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
maintainer
#6 lore raw

PATCH 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
index 18c7a8d..90d6091 100644
@@ -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

maintainer
#8 lore raw

Re: [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

author
#9 lore raw
In reply to Theodore Ts'o
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.

---
 fs/jbd2/checkpoint.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
fs/jbd2/checkpoint.c +0 -1
index 993a187527f3..3722e2e53638 100644
@@ -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 -3
 			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().

I did a bit of archeology and commit
932bb305ba2a01cd62809644d569f004e77a4355 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_counts around 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