Skip to contentSkip to conversation

nfs: avoid i_lock contention in nfs_clear_invalid_mapping

PATCH Superseded Mike Snitzer 3 participants 8 messages Oct 18, 2024 – Oct 30, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +14 -5
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+
+	if (inode->i_mapping->nrpages == 0 ||
+	    nfsi->cache_validity & NFS_INO_INVALID_DATA) {
 		nfs_ooo_clear(nfsi);
 	}
 	trace_nfs_set_cache_invalid(inode, 0);
@@ -1408,6 +1410,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
 					 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 		if (ret)
 			goto out;
+		smp_rmb(); /* pairs with smp_wmb() below */
+		if (test_bit(NFS_INO_INVALIDATING, bitlock))
+			continue;
+		/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
+		if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
+			goto out;
+		/* Slow-path that double-checks with spinlock held */
 		spin_lock(&inode->i_lock);
 		if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
 			spin_unlock(&inode->i_lock);
Signature · 1 line
2.44.0
maintainer
#1 lore raw
Quoted 34 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +0 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 10 lines
fs/nfs/inode.c +5 -4
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I don't know this code that well, but it used to do an |= of flags into cache_validity. Now you're replacing cache_validity wholesale with flags. Maybe that should do something like this?

    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);
Quoted 19 lines
fs/nfs/inode.c +9 -0
+	if (inode->i_mapping->nrpages == 0 ||
+	    nfsi->cache_validity & NFS_INO_INVALID_DATA) {
 		nfs_ooo_clear(nfsi);
 	}
 	trace_nfs_set_cache_invalid(inode, 0);
@@ -1408,6 +1410,13 @@ int nfs_clear_invalid_mapping(struct address_space *mapping)
 					 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 		if (ret)
 			goto out;
+		smp_rmb(); /* pairs with smp_wmb() below */
+		if (test_bit(NFS_INO_INVALIDATING, bitlock))
+			continue;
+		/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
+		if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
+			goto out;
+		/* Slow-path that double-checks with spinlock held */
 		spin_lock(&inode->i_lock);
 		if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
 			spin_unlock(&inode->i_lock);
Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 50 lines · Jeff Layton
Quoted 34 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +0 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 10 lines
fs/nfs/inode.c +5 -4
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I don't know this code that well, but it used to do an |= of flags into cache_validity. Now you're replacing cache_validity wholesale with flags. Maybe that should do something like this?

Quoted 2 lines
    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

This will allow further cleanup too, will let v2 speak to that.

Thanks!
Mike

#3 lore raw
Quoted 51 lines · Mike Snitzer
Quoted 50 lines · Jeff Layton
Quoted 34 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +0 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 10 lines
fs/nfs/inode.c +5 -4
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I don't know this code that well, but it used to do an |= of flags into cache_validity. Now you're replacing cache_validity wholesale with flags. Maybe that should do something like this?

Quoted 6 lines
Quoted 2 lines
    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

This will allow further cleanup too, will let v2 speak to that.

I just posted v2, but for completeness: I decided to leave well enough alone and not do any further cleanup.

(my thinking was relative to nfs_ooo_clear, and possibly moving it before the smp_store_release, so that nfsi->cache_validity only changed once using smp_store_release. The interlock between nfs_set_cache_invalid and nfs_clear_invalid_mapping is focused on NFS_INO_INVALID_DATA, which nfs_ooo_clear doesn't touch).

#4 lore raw

Hi Mike,

Quoted 40 lines · Mike Snitzer
Quoted 39 lines · Mike Snitzer
Quoted 38 lines · Jeff Layton
Quoted 37 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +2 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 7 lines
fs/nfs/inode.c +3 -4
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I'm having some issues with non-localio NFS after applying this patch:

- cthon basic tests fail with NFS v3
- cthon general tests fail with NFS v4.1 and v4.2
- xfstests generic/080, generic/472, generic/615, and generic/633 fail with NFS v4.1 and v4.2
- xfstests generic/683, and generic/684 fail with NFS v4.2

I think the problem is the call to smp_store_release(). It's overwriting nfsi->cache_validity with the value of 'flags', losing anything set there but not in 'flags'. Could we instead do something like:
smp_store_release(&nfsi->cache_validity, nfsi->cache_validity | flags)
?

Anna

Quoted 19 lines
Quoted 10 lines
Quoted 6 lines

I don't know this code that well, but it used to do an |= of flags into cache_validity. Now you're replacing cache_validity wholesale with flags. Maybe that should do something like this?

    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

This will allow further cleanup too, will let v2 speak to that.

I just posted v2, but for completeness: I decided to leave well enough alone and not do any further cleanup.

(my thinking was relative to nfs_ooo_clear, and possibly moving it before the smp_store_release, so that nfsi->cache_validity only changed once using smp_store_release. The interlock between nfs_set_cache_invalid and nfs_clear_invalid_mapping is focused on NFS_INO_INVALID_DATA, which nfs_ooo_clear doesn't touch).

#5 lore raw
Quoted 59 lines · Anna Schumaker

Hi Mike,

Quoted 40 lines · Mike Snitzer
Quoted 39 lines · Mike Snitzer
Quoted 38 lines · Jeff Layton
Quoted 37 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +2 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 7 lines
fs/nfs/inode.c +3 -4
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I'm having some issues with non-localio NFS after applying this patch:

- cthon basic tests fail with NFS v3
- cthon general tests fail with NFS v4.1 and v4.2
- xfstests generic/080, generic/472, generic/615, and generic/633 fail with NFS v4.1 and v4.2
- xfstests generic/683, and generic/684 fail with NFS v4.2

Quoted 7 lines

I think the problem is the call to smp_store_release(). It's overwriting nfsi->cache_validity with the value of 'flags', losing anything set there but not in 'flags'. Could we instead do something like:

   smp_store_release(&nfsi->cache_validity, nfsi->cache_validity | flags)

?

Anna

Hi,

v2 addressed this issue like Jeff suggested with:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

I think you must not be using v2?

https://lore.kernel.org/all/[email protected]/

Jeff also provided his Reviewed-by for v2.

If you are using v2 that'll be weird (because I'm not seeing any issues with xfstests, etc).

Thanks,
Mike

#6 lore raw
Quoted 73 lines · Mike Snitzer
Quoted 59 lines · Anna Schumaker

Hi Mike,

Quoted 40 lines · Mike Snitzer
Quoted 39 lines · Mike Snitzer
Quoted 38 lines · Jeff Layton
Quoted 37 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +2 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 7 lines
fs/nfs/inode.c +3 -4
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I'm having some issues with non-localio NFS after applying this patch:

- cthon basic tests fail with NFS v3
- cthon general tests fail with NFS v4.1 and v4.2
- xfstests generic/080, generic/472, generic/615, and generic/633 fail with NFS v4.1 and v4.2
- xfstests generic/683, and generic/684 fail with NFS v4.2

Quoted 7 lines

I think the problem is the call to smp_store_release(). It's overwriting nfsi->cache_validity with the value of 'flags', losing anything set there but not in 'flags'. Could we instead do something like:

   smp_store_release(&nfsi->cache_validity, nfsi->cache_validity | flags)

?

Anna

Hi,

v2 addressed this issue like Jeff suggested with:

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

I think you must not be using v2?

Oops! It looks like b4 grabbed v1 of the patch from the mailing list instead of v2.

Quoted 7 lines

https://lore.kernel.org/all/[email protected]/

Jeff also provided his Reviewed-by for v2.

If you are using v2 that'll be weird (because I'm not seeing any issues with xfstests, etc).

v2 does fix the issues I was seeing. Sorry about the noise!

Anna

Quoted 3 lines

Thanks,
Mike

#7 lore raw
In reply to Mike Snitzer
Quoted 73 lines · Mike Snitzer
Quoted 59 lines · Anna Schumaker

Hi Mike,

Quoted 40 lines · Mike Snitzer
Quoted 39 lines · Mike Snitzer
Quoted 38 lines · Jeff Layton
Quoted 37 lines · Mike Snitzer

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 17dfeb911339 ("NFS: Fix races in nfs_revalidate_mapping")
---
 fs/nfs/inode.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfs/inode.c +2 -1
index 542c7d97b235..130d7226b12a 100644
@@ -205,12 +205,14 @@ void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
 		nfs_fscache_invalidate(inode, 0);
 	flags &= ~NFS_INO_REVAL_FORCED;
 
-	nfsi->cache_validity |= flags;
+	if (inode->i_mapping->nrpages == 0)
+		flags &= ~NFS_INO_INVALID_DATA;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/nfs/inode.c void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) · line 213

Quoted 7 lines
fs/nfs/inode.c +3 -4
-	if (inode->i_mapping->nrpages == 0) {
-		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
-		nfs_ooo_clear(nfsi);
-	} else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
+	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
+	smp_store_release(&nfsi->cache_validity, flags);
+

I'm having some issues with non-localio NFS after applying this patch:

- cthon basic tests fail with NFS v3
- cthon general tests fail with NFS v4.1 and v4.2
- xfstests generic/080, generic/472, generic/615, and generic/633 fail with NFS v4.1 and v4.2
- xfstests generic/683, and generic/684 fail with NFS v4.2

Quoted 7 lines

I think the problem is the call to smp_store_release(). It's overwriting nfsi->cache_validity with the value of 'flags', losing anything set there but not in 'flags'. Could we instead do something like:

   smp_store_release(&nfsi->cache_validity, nfsi->cache_validity | flags)

?

Anna

Hi,

v2 addressed this issue like Jeff suggested with:

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
    flags |= nfsi->cache_validity;
    smp_store_release(&nfsi->cache_validity, flags);

Ah good catch, sorry about that, will fix.

I think you must not be using v2?

Oops! It looks like b4 grabbed v1 of the patch from the mailing list instead of v2.

Quoted 7 lines

https://lore.kernel.org/all/[email protected]/

Jeff also provided his Reviewed-by for v2.

If you are using v2 that'll be weird (because I'm not seeing any issues with xfstests, etc).

v2 does fix the issues I was seeing. Sorry about the noise!

Anna

Quoted 3 lines

Thanks,
Mike