Skip to content

Commit 6173b0b

Browse files
Mike Snitzergregkh
Mike Snitzer
authored andcommitted
nfs: avoid i_lock contention in nfs_clear_invalid_mapping
[ Upstream commit 867da60 ] Multi-threaded buffered reads to the same file exposed significant inode spinlock contention in nfs_clear_invalid_mapping(). Eliminate this spinlock contention by checking flags without locking, instead using smp_rmb and smp_load_acquire accordingly, but then take spinlock and double-check these inode flags. Also refactor nfs_set_cache_invalid() slightly to use smp_store_release() to pair with nfs_clear_invalid_mapping()'s smp_load_acquire(). While this fix is beneficial for all multi-threaded buffered reads issued by an NFS client, this issue was identified in the context of surprisingly low LOCALIO performance with 4K multi-threaded buffered read IO. This fix dramatically speeds up LOCALIO performance: before: read: IOPS=1583k, BW=6182MiB/s (6482MB/s)(121GiB/20002msec) after: read: IOPS=3046k, BW=11.6GiB/s (12.5GB/s)(232GiB/20001msec) Fixes: 17dfeb9 ("NFS: Fix races in nfs_revalidate_mapping") Signed-off-by: Mike Snitzer <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Anna Schumaker <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 351f033 commit 6173b0b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

fs/nfs/inode.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
206206
nfs_fscache_invalidate(inode, 0);
207207
flags &= ~NFS_INO_REVAL_FORCED;
208208

209-
nfsi->cache_validity |= flags;
209+
flags |= nfsi->cache_validity;
210+
if (inode->i_mapping->nrpages == 0)
211+
flags &= ~NFS_INO_INVALID_DATA;
210212

211-
if (inode->i_mapping->nrpages == 0) {
212-
nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
213-
nfs_ooo_clear(nfsi);
214-
} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
213+
/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
214+
smp_store_release(&nfsi->cache_validity, flags);
215+
216+
if (inode->i_mapping->nrpages == 0 ||
217+
nfsi->cache_validity & NFS_INO_INVALID_DATA) {
215218
nfs_ooo_clear(nfsi);
216219
}
217220
trace_nfs_set_cache_invalid(inode, 0);
@@ -1340,6 +1343,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
13401343
TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
13411344
if (ret)
13421345
goto out;
1346+
smp_rmb(); /* pairs with smp_wmb() below */
1347+
if (test_bit(NFS_INO_INVALIDATING, bitlock))
1348+
continue;
1349+
/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
1350+
if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
1351+
goto out;
1352+
/* Slow-path that double-checks with spinlock held */
13431353
spin_lock(&inode->i_lock);
13441354
if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
13451355
spin_unlock(&inode->i_lock);

0 commit comments

Comments
 (0)