Skip to contentSkip to conversation

Possible fix for lockup in drop_caches

PATCH richard 3 participants 5 messages Dec 17, 2007 – Jan 8, 2008

lore
Share & tools
lore

richard

author
#0 lore raw
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Mon, 3 Dec 2007 16:52:47 +0300 "Denis V. Lunev" <[email protected]> wrote:

There is a AB-BA deadlock regarding drop_caches sysctl. Here are the

code

Quoted 1 line
Quoted 1 line

paths:

snip...

Quoted 11 lines

One way to fix jbd (and jbd2) would be:

static void __journal_temp_unlink_buffer(struct journal_head *jh,
struct buffer_head **bh_to_dirty)

{
	*bh_to_dirty = NULL;
	...
	if (test_clear_buffer_jbddirty(bh))
		*bh_to_dirty = bh;
}
Quoted 2 lines

{
struct buffer_head *bh_to_dirty; /* probably needs

uninitialized_var() */

Quoted 9 lines
	...
	__journal_temp_unlink_buffer(jh, &bh_to_dirty);
	...
	jbd_mark_buffer_dirty(bh_to_dirty);
	brelse(bh_to_dirty);
	...
}
Quoted 8 lines

static inline void jbd_mark_buffer_dirty(struct buffer_head *bh)

{
	if (bh)
		mark_buffer_dirty(bh);
}

--

Hi Andrew,
here's an implementation of your suggested approach for jbd.

Without this patch my test-case will lockup in 3 to 5 iterations, with the patch I have completed 96+ runs.

I don't see any significant difference in write performance with this patch applied. Subjectively I feel that my system is more responsive when under heavy write loads but I don't have data to back that up, so it might just be wishful thinking on my part!

I've tested this on AMD 64x2 SMP 2.6.24-rc*

the patch is against 2.6.24-rc5.

Cheers
Richard

 fs/jbd/commit.c      |   19 +++++++++++---
 fs/jbd/transaction.c |   66 +++++++++++++++++++++++++++++++++++----------------
 include/linux/jbd.h  |   11 ++++++--
 3 files changed, 70 insertions(+), 26 deletions(-)
----
commit 5b3824aa42a07682777836814fc7d1882416c6d3
Author: Richard Kennedy <[email protected]>
Date:   Mon Dec 17 12:05:36 2007 +0000

fix lockup in when calling drop_caches

calling /proc/sys/vm/drop_caches can hang due to a AB/BA lock dependency between j_list_lock and the inode_lock. This patch moves the redirtying of the buffer head out from under the j_list_lock.

based on a suggestion by Andrew Morton.

fs/jbd/commit.c +15 -4
index 610264b..9115b5d 100644
@@ -181,6 +181,7 @@ static void journal_submit_data_buffers(journal_t *journal,
 	int locked;
 	int bufs = 0;
 	struct buffer_head **wbuf = journal->j_wbuf;
+	struct buffer_head *dirty_bh;
 
 	/*
 	 * Whenever we unlock the journal and sleep, things can get added
@@ -254,7 +255,8 @@ write_out_data:
 			put_bh(bh);
 		} else {
 			BUFFER_TRACE(bh, "writeout complete: unfile");
-			__journal_unfile_buffer(jh);
+			__journal_unfile_buffer(jh, &dirty_bh);
+			jbd_mark_buffer_dirty(dirty_bh);
 			jbd_unlock_bh_state(bh);
 			if (locked)
 				unlock_buffer(bh);
@@ -296,6 +298,7 @@ void journal_commit_transaction(journal_t *journal)
 	int first_tag = 0;
 	int tag_flag;
 	int i;
+	struct buffer_head *dirty_bh;
 
 	/*
 	 * First job: lock down the current transaction and wait for
@@ -453,7 +456,9 @@ void journal_commit_transaction(journal_t *journal)
 			continue;
 		}
 		if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
-			__journal_unfile_buffer(jh);
+			struct buffer_head *dirty_bh;
+			__journal_unfile_buffer(jh, &dirty_bh);
+			jbd_mark_buffer_dirty(dirty_bh);
 			jbd_unlock_bh_state(bh);
 			journal_remove_journal_head(bh);
 			put_bh(bh);
@@ -833,7 +838,12 @@ restart_loop:
 			JBUFFER_TRACE(jh, "add to new checkpointing trans");
 			__journal_insert_checkpoint(jh, commit_transaction);
 			JBUFFER_TRACE(jh, "refile for checkpoint writeback");
-			__journal_refile_buffer(jh);
+			__journal_refile_buffer(jh, &dirty_bh);
+			if (dirty_bh) {
+				spin_unlock(&journal->j_list_lock);
+				jbd_mark_buffer_dirty(dirty_bh);
+				spin_lock(&journal->j_list_lock);
+			}
 			jbd_unlock_bh_state(bh);
 		} else {
 			J_ASSERT_BH(bh, !buffer_dirty(bh));
@@ -845,7 +855,8 @@ restart_loop:
 			 * disk and before we process the buffer on BJ_Forget
 			 * list. */
 			JBUFFER_TRACE(jh, "refile or unfile freed buffer");
-			__journal_refile_buffer(jh);
+			/* As !jbddirty dirty_bh will always be null so we can ignore it */
+			__journal_refile_buffer(jh, &dirty_bh);
 			if (!jh->b_transaction) {
 				jbd_unlock_bh_state(bh);
 				 /* needs a brelse */
fs/jbd/transaction.c +46 -20

212 lines not on this page · raw message

include/linux/jbd.h +9 -2
index d9ecd13..8c22a33 100644
@@ -835,14 +835,21 @@ struct journal_s
 
 /* Filing buffers */
 extern void journal_unfile_buffer(journal_t *, struct journal_head *);
-extern void __journal_unfile_buffer(struct journal_head *);
-extern void __journal_refile_buffer(struct journal_head *);
+extern void __journal_unfile_buffer(struct journal_head *,
+		struct buffer_head **dirty_bh);
+extern void __journal_refile_buffer(struct journal_head *,
+		struct buffer_head **dirty_bh);
 extern void journal_refile_buffer(journal_t *, struct journal_head *);
 extern void __journal_file_buffer(struct journal_head *, transaction_t *, int);
 extern void __journal_free_buffer(struct journal_head *bh);
 extern void journal_file_buffer(struct journal_head *, transaction_t *, int);
 extern void __journal_clean_data_list(transaction_t *transaction);
 
+static inline void jbd_mark_buffer_dirty(struct buffer_head *bh)
+{
+	if (bh)
+		mark_buffer_dirty(bh);
+}
 /* Log buffer allocation */
 extern struct journal_head * journal_get_descriptor_buffer(journal_t *);
 int journal_next_log_block(journal_t *, unsigned long *);
#1 lore raw
Quoted 8 lines · On Mon, 17 Dec 2007 12:13:22 +0000 richard <[email protected]> wrote:

fix lockup in when calling drop_caches

calling /proc/sys/vm/drop_caches can hang due to a AB/BA lock dependency between j_list_lock and the inode_lock. This patch moves the redirtying of the buffer head out from under the j_list_lock.

based on a suggestion by Andrew Morton.

Oh boy. Do we really want to add all this stuff to JBD just for drop_caches which is a silly root-only broken-in-22-other-ways thing?

Michael, might your convert-inode-lists-to-tree patches eliminate the need for taking inode_lock in drop_pagecache_sb()? Probably not, as it uses an rbtree. It would have been possible if it was using a radix-tree, I suspect..

fs/jbd/transaction.c static void __journal_temp_unlink_buffer(struct journal_head *jh) · line 1577

Quoted 8 lines
fs/jbd/transaction.c +3 -2
-void __journal_unfile_buffer(struct journal_head *jh)
+void __journal_unfile_buffer(struct journal_head *jh,
+		struct buffer_head **dirty_bh)
 {
-	__journal_temp_unlink_buffer(jh);
+	__journal_temp_unlink_buffer(jh, dirty_bh);
 	jh->b_transaction = NULL;
 }

I suspect the code would end up simpler if __journal_unfile_buffer() were to take an additional ref on the bh which it placed at *dirty_bh.

Callers of __journal_unfile_buffer() could then call

void handle_dirty_bh(struct buffer_head *bh)

{
	if (bh) {
		jbd_mark_buffer_dirty(bh);
		put_bh(bh);
	}
}

?

#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 17 Dec 2007 12:13:22 +0000 richard <[email protected]> wrote:

fix lockup in when calling drop_caches

calling /proc/sys/vm/drop_caches can hang due to a AB/BA lock dependency between j_list_lock and the inode_lock. This patch moves the redirtying of the buffer head out from under the j_list_lock.

Quoted 5 lines
Quoted 2 lines

based on a suggestion by Andrew Morton.

Oh boy. Do we really want to add all this stuff to JBD just for drop_caches which is a silly root-only broken-in-22-other-ways thing?

It did end up with a lot of code but I was hoping that not taking 2 locks at the same time would have a positive performance benefit, not just fix the lockup. But, so far I've not been able to find a benchmark to show any consistent difference.

However, I'm getting some interesting numbers from iozone that suggest this patch improves write performance when the buffers are small enough to fit into memory, _but_ the results are very variable. I'm not sure why the iozone results are so inconsistent, maybe oprofile will help. Anyway more testing is needed :)

Quoted 19 lines

Michael, might your convert-inode-lists-to-tree patches eliminate the need for taking inode_lock in drop_pagecache_sb()? Probably not, as it uses an rbtree. It would have been possible if it was using a radix-tree, I suspect..

fs/jbd/transaction.c static void __journal_temp_unlink_buffer(struct journal_head *jh) · line 1577

Quoted 8 lines
fs/jbd/transaction.c +3 -2
-void __journal_unfile_buffer(struct journal_head *jh)
+void __journal_unfile_buffer(struct journal_head *jh,
+		struct buffer_head **dirty_bh)
 {
-	__journal_temp_unlink_buffer(jh);
+	__journal_temp_unlink_buffer(jh, dirty_bh);
 	jh->b_transaction = NULL;
 }

I suspect the code would end up simpler if __journal_unfile_buffer() were to take an additional ref on the bh which it placed at *dirty_bh.

Callers of __journal_unfile_buffer() could then call

Quoted 7 lines

void handle_dirty_bh(struct buffer_head *bh)

{
	if (bh) {
		jbd_mark_buffer_dirty(bh);
		put_bh(bh);
	}
}

thanks for the suggestion, that looks like a really clean approach, I'll give it a try.

cheers
Richard

#3 lore raw
In reply to Andrew Morton
Quoted 7 lines · Andrew Morton

Oh boy. Do we really want to add all this stuff to JBD just for drop_caches which is a silly root-only broken-in-22-other-ways thing?

Michael, might your convert-inode-lists-to-tree patches eliminate the need for taking inode_lock in drop_pagecache_sb()? Probably not, as it uses an rbtree. It would have been possible if it was using a radix-tree, I suspect..

You are correct. The rbtree will still req

Quoted 25 lines

fs/jbd/transaction.c static void __journal_temp_unlink_buffer(struct journal_head *jh) · line 1577

Quoted 9 lines
fs/jbd/transaction.c +3 -2
-void __journal_unfile_buffer(struct journal_head *jh)
+void __journal_unfile_buffer(struct journal_head *jh,
+             struct buffer_head **dirty_bh)
 {
-     __journal_temp_unlink_buffer(jh);
+     __journal_temp_unlink_buffer(jh, dirty_bh);
      jh->b_transaction = NULL;
 }

I suspect the code would end up simpler if __journal_unfile_buffer() were to take an additional ref on the bh which it placed at *dirty_bh.

Callers of __journal_unfile_buffer() could then call

void handle_dirty_bh(struct buffer_head *bh)

{
        if (bh) {
                jbd_mark_buffer_dirty(bh);
                put_bh(bh);
        }
}

?

#4 lore raw
In reply to Andrew Morton
Quoted 5 lines · Andrew Morton

On Mon, 17 Dec 2007 12:13:22 +0000 richard <[email protected]> wrote:
Michael, might your convert-inode-lists-to-tree patches eliminate the need for taking inode_lock in drop_pagecache_sb()? Probably not, as it uses an rbtree. It would have been possible if it was using a radix-tree, I suspect..

You are correct the new patch based on an rbtree will still require the lock.

mrubin