Root Cause Analysis
1. Functions, Structs, and Files Involved
fs/ocfs2/xattr.c:
enum { OCFS2_IP_ALLOC_SEM_UNPUBLISHED = 1 } (defines the lockdep subclass for unpublished inodes).
ocfs2_xattr_set_handle(): acquires down_write_nested(&OCFS2_I(inode)->ip_alloc_sem, OCFS2_IP_ALLOC_SEM_UNPUBLISHED).
fs/ocfs2/file.c:
ocfs2_remap_file_range(): locks source and destination file allocation semaphores, taking down_write_nested(&OCFS2_I(inode_out)->ip_alloc_sem, SINGLE_DEPTH_NESTING).
fs/ocfs2/refcounttree.c:
ocfs2_reflink_remap_blocks() / ocfs2_duplicate_inline_data(): starts a transaction while holding the allocation semaphores.
fs/ocfs2/journal.c:
ocfs2_start_trans(): starts a transaction, acquiring sb_internal via sb_start_intwrite(osb->sb) and down_read(&osb->journal->j_trans_barrier).
include/linux/lockdep.h:
#define SINGLE_DEPTH_NESTING 1.
2. Detailed Technical Mechanism
In OCFS2, the canonical locking order between the allocation semaphore (oi->ip_alloc_sem) and filesystem journal transactions is:
$$\text{inode->ip_alloc_sem} \longrightarrow \text{transaction (sb_internal / j_trans_barrier)}$$
-
Transaction $\rightarrow$ ip_alloc_sem dependency in inode creation (ocfs2_xattr_set_handle):
During inode creation paths (e.g. ocfs2_mknod, ocfs2_mkdir, ocfs2_symlink), a transaction is already active when security xattrs and ACLs are initialized (ocfs2_init_security_set() / ocfs2_init_acl()). Inside ocfs2_xattr_set_handle(), ip_alloc_sem is acquired:
down_write_nested(&OCFS2_I(inode)->ip_alloc_sem,
OCFS2_IP_ALLOC_SEM_UNPUBLISHED);
To avoid lockdep detecting a reverse dependency (transaction -> ip_alloc_sem) against the normal ip_alloc_sem -> transaction order used during truncation and allocation, OCFS2_IP_ALLOC_SEM_UNPUBLISHED was introduced. However, it was defined as:
enum {
OCFS2_IP_ALLOC_SEM_UNPUBLISHED = 1,
};
This assigned lockdep subclass 1 (&ocfs2_file_ip_alloc_sem_key/1) to the semaphore. As a result, lockdep registered the dependency chain:
$$\text{sb_internal} \longrightarrow \text{journal->j_trans_barrier} \longrightarrow \text{&ocfs2_file_ip_alloc_sem_key/1}$$
-
Subclass collision in ocfs2_remap_file_range():
When remapping file ranges across two distinct inodes (same_inode == false), ocfs2_remap_file_range() locks the allocation semaphore for both inodes:
down_write(&OCFS2_I(inode_in)->ip_alloc_sem);
if (!same_inode)
down_write_nested(&OCFS2_I(inode_out)->ip_alloc_sem,
SINGLE_DEPTH_NESTING);
Because SINGLE_DEPTH_NESTING is #define SINGLE_DEPTH_NESTING 1, inode_out->ip_alloc_sem is also acquired with subclass 1 (&ocfs2_file_ip_alloc_sem_key/1).
-
Subsequent transaction start:
With both inode_in->ip_alloc_sem (subclass 0) and inode_out->ip_alloc_sem (subclass 1) held, ocfs2_remap_file_range() calls ocfs2_reflink_remap_blocks(). When remapping inline data, it invokes ocfs2_duplicate_inline_data(), which calls:
handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ocfs2_start_trans() attempts to acquire:
sb_start_intwrite(osb->sb); /* acquires sb_internal */
down_read(&osb->journal->j_trans_barrier);
Because lockdep treats OCFS2_IP_ALLOC_SEM_UNPUBLISHED and SINGLE_DEPTH_NESTING as the exact same lock class (&ocfs2_file_ip_alloc_sem_key/1), it observes:
- Chain 1 (from
mknod): sb_internal -> j_trans_barrier -> ip_alloc_sem/1
- Chain 2 (from
remap_file_range): ip_alloc_sem/1 -> sb_internal
Lockdep flags this as a possible circular locking dependency (ABBA deadlock).
3. Invariant Violation and Conclusion
- Classification: False positive lockdep warning caused by lockdep subclass namespace collision.
- Why no real deadlock occurs: The inode locked via
OCFS2_IP_ALLOC_SEM_UNPUBLISHED in ocfs2_xattr_set_handle() is newly allocated, unpublished, unhashed, and not yet linked to any directory entry. It is completely inaccessible to concurrent threads, making concurrent contention between ocfs2_xattr_set_handle() and ocfs2_remap_file_range() on the same inode impossible.
- Remedy: Assign
OCFS2_IP_ALLOC_SEM_UNPUBLISHED a distinct subclass index that does not collide with SINGLE_DEPTH_NESTING (e.g., subclass 2).