[PATCH v1] bcachefs: use mutex_trylock in bch2_replicas_entry_validate

From: Piotr Zalewski
Date: Mon Oct 07 2024 - 16:56:07 EST


Use mutex_trylock instead of mutex_lock in bch2_replicas_entry_validate to
prevent potential deadlock[1].

[1] https://syzkaller.appspot.com/bug?extid=4d24267b490e2b68a5fa

Reported-by: syzbot+4d24267b490e2b68a5fa@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=4d24267b490e2b68a5fa
Fixes: 49fd90b2cc33 ("bcachefs: Fix unlocked access to c->disk_sb.sb in bch2_replicas_entry_validate()")
Signed-off-by: Piotr Zalewski <pZ010001011111@xxxxxxxxx>
---
fs/bcachefs/replicas.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/bcachefs/replicas.c b/fs/bcachefs/replicas.c
index bcb3276747e0..d301832c5a1b 100644
--- a/fs/bcachefs/replicas.c
+++ b/fs/bcachefs/replicas.c
@@ -98,9 +98,10 @@ int bch2_replicas_entry_validate(struct bch_replicas_entry_v1 *r,
struct bch_fs *c,
struct printbuf *err)
{
- mutex_lock(&c->sb_lock);
+ int acquired = mutex_trylock(&c->sb_lock);
int ret = bch2_replicas_entry_validate_locked(r, c->disk_sb.sb, err);
- mutex_unlock(&c->sb_lock);
+ if (acquired)
+ mutex_unlock(&c->sb_lock);
return ret;
}

--
2.46.2