Skip to contentSkip to conversation

fs: xfs: fix a possible data race in xfs_inode_set_reclaim_tag()

PATCH Superseded Jia-Ju Bai 2 participants 2 messages May 4, 2020 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 fs: xfs: fix a possible data race in xfs_inode_set_reclaim_tag()

We find that xfs_inode_set_reclaim_tag() and xfs_reclaim_inode() are concurrently executed at runtime in the following call contexts:

Thread1:
xfs_fs_put_super()

    xfs_unmountfs()
      xfs_rtunmount_inodes()
        xfs_irele()
          xfs_fs_destroy_inode()
            xfs_inode_set_reclaim_tag()

Thread2:
xfs_reclaim_worker()
xfs_reclaim_inodes()
xfs_reclaim_inodes_ag()
xfs_reclaim_inode()

In xfs_inode_set_reclaim_tag():
pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
...
spin_lock(&ip->i_flags_lock);

In xfs_reclaim_inode():
spin_lock(&ip->i_flags_lock);
...
ip->i_ino = 0;
spin_unlock(&ip->i_flags_lock);

Thus, a data race can occur for ip->i_ino.

To fix this data race, the spinlock ip->i_flags_lock is used to protect the access to ip->i_ino in xfs_inode_set_reclaim_tag().

This data race is found by our concurrency fuzzer.

---
 fs/xfs/xfs_icache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_icache.c +1 -1
index 8bf1d15be3f6..a2de08222ff5 100644
@@ -229,9 +229,9 @@ xfs_inode_set_reclaim_tag(
 	struct xfs_mount	*mp = ip->i_mount;
 	struct xfs_perag	*pag;
 
+	spin_lock(&ip->i_flags_lock);
 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 	spin_lock(&pag->pag_ici_lock);
-	spin_lock(&ip->i_flags_lock);
 
 	radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
 			   XFS_ICI_RECLAIM_TAG);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 28 lines · Jia-Ju Bai

We find that xfs_inode_set_reclaim_tag() and xfs_reclaim_inode() are concurrently executed at runtime in the following call contexts:

Thread1:
xfs_fs_put_super()

    xfs_unmountfs()
      xfs_rtunmount_inodes()
        xfs_irele()
          xfs_fs_destroy_inode()
            xfs_inode_set_reclaim_tag()

Thread2:
xfs_reclaim_worker()
xfs_reclaim_inodes()
xfs_reclaim_inodes_ag()
xfs_reclaim_inode()

In xfs_inode_set_reclaim_tag():
pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
...
spin_lock(&ip->i_flags_lock);

In xfs_reclaim_inode():
spin_lock(&ip->i_flags_lock);
...
ip->i_ino = 0;
spin_unlock(&ip->i_flags_lock);

Quoted 6 lines

Thus, a data race can occur for ip->i_ino.

To fix this data race, the spinlock ip->i_flags_lock is used to protect the access to ip->i_ino in xfs_inode_set_reclaim_tag().

This data race is found by our concurrency fuzzer.

This data race cannot happen.

xfs_reclaim_inode() will not be called on this inode until -after- the XFS_ICI_RECLAIM_TAG is set in the radix tree for this inode, and setting that is protected by the i_flags_lock.

So while the xfs_perag_get() call doesn't lock the ip->i_ino access, there is are -multiple_ iflags_lock lock/unlock cycles before ip->i_ino is cleared in the reclaim worker. Hence there is a full unlock->lock memory barrier for the ip->i_ino reset inside the critical section vs xfs_inode_set_reclaim_tag().

Hence even if the reclaim worker could access the inode before the XFS_ICI_RECLAIM_TAG is set, no data race exists here.

Quoted 10 lines
---
 fs/xfs/xfs_icache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_icache.c +0 -0
index 8bf1d15be3f6..a2de08222ff5 100644

fs/xfs/xfs_icache.c xfs_inode_set_reclaim_tag( · line 234

Quoted 7 lines
fs/xfs/xfs_icache.c +1 -1
@@ -229,9 +229,9 @@ xfs_inode_set_reclaim_tag(
 	struct xfs_mount	*mp = ip->i_mount;
 	struct xfs_perag	*pag;
 
+	spin_lock(&ip->i_flags_lock);
 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 	spin_lock(&pag->pag_ici_lock);
-	spin_lock(&ip->i_flags_lock);

Also, this creates a lock inversion deadlock here with xfs_iget_cache_hit() clearing the XFS_IRECLAIMABLE flag.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]