Skip to contentSkip to conversation

bcachefs: Fix readahead involved deadlock

PATCH Applied Alan Huang 2 participants 2 messages Aug 5, 2025

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 bcachefs: Fix readahead involved deadlock

readahead first lock the folio, then invokes aops->readahead, which locks the two state lock to block subsequent direct I/O. However, direct IO or bchfs_fpunch first lock the two state lock to block subsequent buffered I/O, and then lock the folio to invalidate or truncate it. Therefore, there is a deadlock:

thread1 thread2
lock folio bch2_pagecache_block_get
bch2_pagecache_add_get lock folio

---
 fs/bcachefs/fs-io-buffered.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/bcachefs/fs-io-buffered.c +3 -1
index c5ed62a11f23..ed8329c6c00d 100644
@@ -290,7 +290,8 @@ void bch2_readahead(struct readahead_control *ractl)
 	 * scheduling.
 	 */
 	blk_start_plug(&plug);
-	bch2_pagecache_add_get(inode);
+	if (!bch2_pagecache_add_tryget(inode))
+		goto out;
 
 	struct btree_trans *trans = bch2_trans_get(c);
 	while ((folio = readpage_iter_peek(&readpages_iter))) {
@@ -317,6 +318,7 @@ void bch2_readahead(struct readahead_control *ractl)
 	bch2_trans_put(trans);
 
 	bch2_pagecache_add_put(inode);
+out:
 	blk_finish_plug(&plug);
 	darray_exit(&readpages_iter.folios);
 }
Signature · 1 line
2.49.0
#1 lore raw
Quoted 6 lines · Alan Huang

readahead first lock the folio, then invokes aops->readahead, which locks the two state lock to block subsequent direct I/O. However, direct IO or bchfs_fpunch first lock the two state lock to block subsequent buffered I/O, and then lock the folio to invalidate or truncate it. Therefore, there is a deadlock:

Quoted 6 lines

thread1 thread2
lock folio bch2_pagecache_block_get
bch2_pagecache_add_get lock folio

Readahead is allowed to silently fail (we fall back to read_folio), and if the tryget fails that means another thread doesn't want new data added to the pagecache, so this looks good.

Applied.

Quoted 29 lines
---
 fs/bcachefs/fs-io-buffered.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/bcachefs/fs-io-buffered.c +3 -1
index c5ed62a11f23..ed8329c6c00d 100644
@@ -290,7 +290,8 @@ void bch2_readahead(struct readahead_control *ractl)
 	 * scheduling.
 	 */
 	blk_start_plug(&plug);
-	bch2_pagecache_add_get(inode);
+	if (!bch2_pagecache_add_tryget(inode))
+		goto out;
 
 	struct btree_trans *trans = bch2_trans_get(c);
 	while ((folio = readpage_iter_peek(&readpages_iter))) {
@@ -317,6 +318,7 @@ void bch2_readahead(struct readahead_control *ractl)
 	bch2_trans_put(trans);
 
 	bch2_pagecache_add_put(inode);
+out:
 	blk_finish_plug(&plug);
 	darray_exit(&readpages_iter.folios);
 }
Signature · 1 line
2.49.0