Skip to content

Commit ef5ae6c

Browse files
Mike Snitzersmb49
authored andcommitted
nfs: avoid i_lock contention in nfs_clear_invalid_mapping
BugLink: https://bugs.launchpad.net/bugs/2100292 [ Upstream commit 867da60d463bb2a3e28c9235c487e56e96cffa00 ] 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 <snitzer@kernel.org> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Anna Schumaker <anna.schumaker@oracle.com> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Koichiro Den <koichiro.den@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 3165a30 commit ef5ae6c

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);
@@ -1343,6 +1346,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
13431346
TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
13441347
if (ret)
13451348
goto out;
1349+
smp_rmb(); /* pairs with smp_wmb() below */
1350+
if (test_bit(NFS_INO_INVALIDATING, bitlock))
1351+
continue;
1352+
/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
1353+
if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
1354+
goto out;
1355+
/* Slow-path that double-checks with spinlock held */
13461356
spin_lock(&inode->i_lock);
13471357
if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
13481358
spin_unlock(&inode->i_lock);

0 commit comments

Comments
 (0)