diff mbox series

fs/buffer: adjust the order of might_sleep() in __getblk_gfp()

Message ID 20230323093752.17461-1-gouhao@uniontech.com (mailing list archive)
State Mainlined, archived
Headers show
Series fs/buffer: adjust the order of might_sleep() in __getblk_gfp() | expand

Commit Message

Gou Hao March 23, 2023, 9:37 a.m. UTC
From: Gou Hao <gouhao@uniontech.com>

If 'bh' is found in cache, just return directly.
might_sleep() is only required on slow paths.

Signed-off-by: Gou Hao <gouhao@uniontech.com>
---
 fs/buffer.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Matthew Wilcox (Oracle) March 23, 2023, 12:23 p.m. UTC | #1
On Thu, Mar 23, 2023 at 05:37:52PM +0800, gouhao@uniontech.com wrote:
> From: Gou Hao <gouhao@uniontech.com>
> 
> If 'bh' is found in cache, just return directly.
> might_sleep() is only required on slow paths.

You're missing the point.  The caller can't know whether the slow or
fast path will be taken.  So it must _never_ call this function if it
cannot sleep.
Gou Hao March 24, 2023, 1:52 a.m. UTC | #2
>On Thu, Mar 23, 2023 at 05:37:52PM +0800, gouhao@uniontech.com wrote:
>> From: Gou Hao <gouhao@uniontech.com>
>> 
>> If 'bh' is found in cache, just return directly.
>> might_sleep() is only required on slow paths.
>
>You're missing the point.  The caller can't know whether the slow or
>fast path will be taken.  So it must _never_ call this function if it
>cannot sleep.
I see. Thank you for your explanation! :)
diff mbox series

Patch

diff --git a/fs/buffer.c b/fs/buffer.c
index 9e1e2add541e..e13eff504fb5 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -1371,10 +1371,11 @@  __getblk_gfp(struct block_device *bdev, sector_t block,
 {
 	struct buffer_head *bh = __find_get_block(bdev, block, size);
 
+	if (bh)
+		return bh;
+
 	might_sleep();
-	if (bh == NULL)
-		bh = __getblk_slow(bdev, block, size, gfp);
-	return bh;
+	return __getblk_slow(bdev, block, size, gfp);
 }
 EXPORT_SYMBOL(__getblk_gfp);