Skip to contentSkip to conversation

ocfs2: fix lock acquisition order in refcounttree

PATCH In review Diogo Jahchan Koike 3 participants 6 messages Jul 14, 2025 – Apr 25 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: fix lock acquisition order in refcounttree

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

---
 fs/ocfs2/refcounttree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ocfs2/refcounttree.c +2 -2
index 8f732742b26e..c8467b92b64e 100644
@@ -928,8 +928,8 @@ int ocfs2_try_remove_refcount_tree(struct inode *inode,
 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
 
-	down_write(&oi->ip_xattr_sem);
 	down_write(&oi->ip_alloc_sem);
+	down_write(&oi->ip_xattr_sem);
 
 	if (oi->ip_clusters)
 		goto out;
@@ -945,8 +945,8 @@ int ocfs2_try_remove_refcount_tree(struct inode *inode,
 	if (ret)
 		mlog_errno(ret);
 out:
-	up_write(&oi->ip_alloc_sem);
 	up_write(&oi->ip_xattr_sem);
+	up_write(&oi->ip_alloc_sem);
 	return 0;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 4 lines · Diogo Jahchan Koike

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

I noticed that ocfs2_reflink() in the same file wants similar change.

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);
	error = __ocfs2_reflink(old_dentry, old_bh,
				new_orphan_inode, preserve);
	up_write(&OCFS2_I(inode)->ip_alloc_sem);
	up_write(&OCFS2_I(inode)->ip_xattr_sem);
#2 lore raw
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Diogo Jahchan Koike

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

Quoted 9 lines

I noticed that ocfs2_reflink() in the same file wants similar change.

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);
	error = __ocfs2_reflink(old_dentry, old_bh,
				new_orphan_inode, preserve);
	up_write(&OCFS2_I(inode)->ip_alloc_sem);
	up_write(&OCFS2_I(inode)->ip_xattr_sem);

Moreover, I noticed that e.g. ocfs2_xattr_set_handle() firstly acquires ip_xatr_sem and then ocfs2_xattr_ibody_find() might acquire ip_alloc_sem.

Diogo, where do you see the ip_alloc --> ip_xattr ordering?

Unless we unify to either ip_alloc --> ip_xattr ordering or ip_xattr --> ip_alloc ordering (or replace ip_xattr with ip_alloc), this patch simply changes the location of lockdep warning?

#3 lore raw
Quoted 17 lines · Tetsuo Handa
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Diogo Jahchan Koike

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

Quoted 9 lines

I noticed that ocfs2_reflink() in the same file wants similar change.

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);
	error = __ocfs2_reflink(old_dentry, old_bh,
				new_orphan_inode, preserve);
	up_write(&OCFS2_I(inode)->ip_alloc_sem);
	up_write(&OCFS2_I(inode)->ip_xattr_sem);
Quoted 9 lines

Moreover, I noticed that e.g. ocfs2_xattr_set_handle() firstly acquires ip_xatr_sem and then ocfs2_xattr_ibody_find() might acquire ip_alloc_sem.

Diogo, where do you see the ip_alloc --> ip_xattr ordering?

Unless we unify to either ip_alloc --> ip_xattr ordering or ip_xattr --> ip_alloc ordering (or replace ip_xattr with ip_alloc), this patch simply changes the location of lockdep warning?

Since I couldn't find direct ip_alloc --> ip_xattr ordering, I tried effectively replacing ip_xattr with ip_alloc at
https://lkml.kernel.org/r/[email protected]
and got sb_internal --> ip_alloc v.s. ip_alloc --> sb_internal ordering problem, as with other lockdep reports in ocfs2 subsystem at https://syzkaller.appspot.com/upstream/s/ocfs2 .

In the team network driver, this kind of ordering issues has been addressed by simplifying locking dependency at
https://lkml.kernel.org/r/[email protected] .

ocfs2 developers, can you simplify locking dependency in ocfs2, by killing several locks and reordering the locks?

#4 lore raw
In reply to Diogo Jahchan Koike
Quoted 7 lines · On Mon, 14 Jul 2025 19:15:32 -0300 Diogo Jahchan Koike…

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

But ocfs2_reflink() does

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);
#5 lore raw
Quoted 6 lines · Andrew Morton

But ocfs2_reflink() does

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);

Yes, we know. We are waiting for response from OCFS2 people on the below message.

-------- Forwarded Message --------
Message-ID: <[email protected]>
Date: Mon, 21 Jul 2025 19:40:58 +0900
Subject: Re: [PATCH] ocfs2: fix lock acquisition order in refcounttree
From: Tetsuo Handa <[email protected]>
To: Diogo Jahchan Koike <[email protected]>, Mark Fasheh <[email protected]>, Joel Becker <[email protected]>, Joseph Qi <[email protected]>, [email protected]
Cc: [email protected], [email protected]
References: <[email protected]> <[email protected]> <[email protected]>
In-Reply-To: <[email protected]>

Quoted 17 lines · Tetsuo Handa
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Diogo Jahchan Koike

Acquiring the locks in refcounttree should follow the ip_alloc --> ip_xattr ordering, as done by multiple code paths in ocfs2; otherwise, we risk an ABBA deadlock (i.e in the start transaction path).

Quoted 9 lines

I noticed that ocfs2_reflink() in the same file wants similar change.

	down_write(&OCFS2_I(inode)->ip_xattr_sem);
	down_write(&OCFS2_I(inode)->ip_alloc_sem);
	error = __ocfs2_reflink(old_dentry, old_bh,
				new_orphan_inode, preserve);
	up_write(&OCFS2_I(inode)->ip_alloc_sem);
	up_write(&OCFS2_I(inode)->ip_xattr_sem);
Quoted 9 lines

Moreover, I noticed that e.g. ocfs2_xattr_set_handle() firstly acquires ip_xatr_sem and then ocfs2_xattr_ibody_find() might acquire ip_alloc_sem.

Diogo, where do you see the ip_alloc --> ip_xattr ordering?

Unless we unify to either ip_alloc --> ip_xattr ordering or ip_xattr --> ip_alloc ordering (or replace ip_xattr with ip_alloc), this patch simply changes the location of lockdep warning?

Since I couldn't find direct ip_alloc --> ip_xattr ordering, I tried effectively replacing ip_xattr with ip_alloc at
https://lkml.kernel.org/r/[email protected]
and got sb_internal --> ip_alloc v.s. ip_alloc --> sb_internal ordering problem, as with other lockdep reports in ocfs2 subsystem at https://syzkaller.appspot.com/upstream/s/ocfs2 .

In the team network driver, this kind of ordering issues has been addressed by simplifying locking dependency at
https://lkml.kernel.org/r/[email protected] .

ocfs2 developers, can you simplify locking dependency in ocfs2, by killing several locks and reordering the locks?