>From feaf5e5ca0b22da6a285dc97eb756e0190fa7411 Mon Sep 17 00:00:00 2001 From: Jan Kara Date: Mon, 24 Oct 2022 12:02:59 +0200 Subject: [PATCH] Debug buffer_head lock Signed-off-by: Jan Kara --- fs/buffer.c | 13 ++++++++++++- include/linux/buffer_head.h | 17 +++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index f6d283579491..4514a810f072 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -66,7 +66,18 @@ EXPORT_SYMBOL(touch_buffer); void __lock_buffer(struct buffer_head *bh) { - wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE); + int loops = 0; + + for (;;) { + wait_on_bit_timeout(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE, HZ); + if (trylock_buffer(bh)) + return; + loops++; + if (WARN_ON(!(loops & 31))) { + printk("Waiting for buffer %llu state %lx page flags %lx for %d loops.\n", (unsigned long long)bh->b_blocknr, bh->b_state, bh->b_page->flags, loops); + printk("Owner: pid %u file %s:%d\n", bh->lock_pid, bh->lock_file, bh->lock_line); + } + } } EXPORT_SYMBOL(__lock_buffer); diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h index 25b4263d66d7..67259a959bac 100644 --- a/include/linux/buffer_head.h +++ b/include/linux/buffer_head.h @@ -76,6 +76,9 @@ struct buffer_head { spinlock_t b_uptodate_lock; /* Used by the first bh in a page, to * serialise IO completion of other * buffers in the page */ + char *lock_file; + unsigned int lock_line; + unsigned int lock_pid; }; /* @@ -395,12 +398,14 @@ static inline int trylock_buffer(struct buffer_head *bh) return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state)); } -static inline void lock_buffer(struct buffer_head *bh) -{ - might_sleep(); - if (!trylock_buffer(bh)) - __lock_buffer(bh); -} +#define lock_buffer(bh) do { \ + might_sleep(); \ + if (!trylock_buffer(bh)) \ + __lock_buffer(bh); \ + (bh)->lock_line = __LINE__; \ + (bh)->lock_file = __FILE__; \ + (bh)->lock_pid = current->pid; \ +} while (0) static inline struct buffer_head *getblk_unmovable(struct block_device *bdev, sector_t block, -- 2.35.3