Skip to content

Commit 867da60

Browse files
Mike SnitzerAnna Schumaker
authored andcommitted
nfs: avoid i_lock contention in nfs_clear_invalid_mapping
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]>
1 parent bc29408 commit 867da60

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
@@ -205,12 +205,15 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
205205
nfs_fscache_invalidate(inode, 0);
206206
flags &= ~NFS_INO_REVAL_FORCED;
207207

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

210-
if (inode->i_mapping->nrpages == 0) {
211-
nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
212-
nfs_ooo_clear(nfsi);
213-
} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
212+
/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
213+
smp_store_release(&nfsi->cache_validity, flags);
214+
215+
if (inode->i_mapping->nrpages == 0 ||
216+
nfsi->cache_validity & NFS_INO_INVALID_DATA) {
214217
nfs_ooo_clear(nfsi);
215218
}
216219
trace_nfs_set_cache_invalid(inode, 0);
@@ -1421,6 +1424,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
14211424
TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
14221425
if (ret)
14231426
goto out;
1427+
smp_rmb(); /* pairs with smp_wmb() below */
1428+
if (test_bit(NFS_INO_INVALIDATING, bitlock))
1429+
continue;
1430+
/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
1431+
if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
1432+
goto out;
1433+
/* Slow-path that double-checks with spinlock held */
14241434
spin_lock(&inode->i_lock);
14251435
if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
14261436
spin_unlock(&inode->i_lock);

0 commit comments

Comments
 (0)