Skip to content

Commit 2346a40

Browse files
Mike Snitzermehmetb0
authored andcommitted
nfs: avoid i_lock contention in nfs_clear_invalid_mapping
BugLink: https://bugs.launchpad.net/bugs/2089533 [ 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 <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: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent d93ad50 commit 2346a40

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
@@ -212,12 +212,15 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
212212
nfs_fscache_invalidate(inode);
213213
flags &= ~(NFS_INO_REVAL_PAGECACHE | NFS_INO_REVAL_FORCED);
214214

215-
nfsi->cache_validity |= flags;
215+
flags |= nfsi->cache_validity;
216+
if (inode->i_mapping->nrpages == 0)
217+
flags &= ~NFS_INO_INVALID_DATA;
216218

217-
if (inode->i_mapping->nrpages == 0) {
218-
nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
219-
nfs_ooo_clear(nfsi);
220-
} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
219+
/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
220+
smp_store_release(&nfsi->cache_validity, flags);
221+
222+
if (inode->i_mapping->nrpages == 0 ||
223+
nfsi->cache_validity & NFS_INO_INVALID_DATA) {
221224
nfs_ooo_clear(nfsi);
222225
}
223226
trace_nfs_set_cache_invalid(inode, 0);
@@ -1350,6 +1353,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
13501353
nfs_wait_bit_killable, TASK_KILLABLE);
13511354
if (ret)
13521355
goto out;
1356+
smp_rmb(); /* pairs with smp_wmb() below */
1357+
if (test_bit(NFS_INO_INVALIDATING, bitlock))
1358+
continue;
1359+
/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
1360+
if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
1361+
goto out;
1362+
/* Slow-path that double-checks with spinlock held */
13531363
spin_lock(&inode->i_lock);
13541364
if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
13551365
spin_unlock(&inode->i_lock);

0 commit comments

Comments
 (0)