Skip to contentSkip to conversation

nfsd: improvements for wake_up_bit/var

PATCH Applied NeilBrown 4 participants 8 messages Aug 30, 2024 – Sep 2, 2024

Subsystems (2)
lore
Share & tools
lore

NeilBrown

author
#0 lore raw

cover nfsd: improvements for wake_up_bit/var

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

NeilBrown

[PATCH 1/2] nfsd: use clear_and_wake_up_bit()
[PATCH 2/2] nfsd: avoid races with wake_up_var()

author
#1 lore raw

PATCH 1/2 nfsd: use clear_and_wake_up_bit()

nfsd has two places that open-code clear_and_wake_up_bit(). One has the required memory barriers. The other does not.

Change both to use clear_and_wake_up_bit() so with have the barriers without the noise.

---
 fs/nfsd/nfs4recover.c | 5 +----
 fs/nfsd/nfs4state.c   | 3 +--
 2 files changed, 2 insertions(+), 6 deletions(-)
fs/nfsd/nfs4recover.c +1 -4
index 67d8673a9391..b10cfb217fa2 100644
@@ -1895,10 +1895,7 @@ nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
 static void
 nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
 {
-	smp_mb__before_atomic();
-	clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
-	smp_mb__after_atomic();
-	wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
+	clear_and_wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
 }
 
 static void
fs/nfsd/nfs4state.c +1 -2
index b67f151837c1..3f85575ee0db 100644
@@ -3076,8 +3076,7 @@ nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
 			container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
 
 	nfs4_put_stid(&dp->dl_stid);
-	clear_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags);
-	wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY);
+	clear_and_wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY);
 }
 
 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
Signature · 1 line
2.44.0
author
#2 lore raw

PATCH 2/2 nfsd: avoid races with wake_up_var()

In reply to NeilBrown

wake_up_var() needs a barrier after the important change is made in the var and before wake_up_var() is called, else it is possible that a wake up won't be sent when it should.

In each case here the var is changed in an "atomic" manner, so smb_mb__after_atomic() is sufficient.

In one case the important change (removing the lease) is performed *after* the wake_up, which is backwards. The code survives in part because the wait_var_event is given a timeout.

This patch adds the required barriers and calls destroy_delegation() *before* waking any threads waiting for the delegation to be destroyed.

---
 fs/nfsd/nfs4state.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +4 -1
index 3f85575ee0db..10e3a46289a1 100644
@@ -4700,6 +4700,7 @@ void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
 	if (so != NULL) {
 		cstate->replay_owner = NULL;
 		atomic_set(&so->so_replay.rp_locked, RP_UNLOCKED);
+		smb_mb__after_atomic();
 		wake_up_var(&so->so_replay.rp_locked);
 		nfs4_put_stateowner(so);
 	}
@@ -5000,6 +5001,7 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
 	 * so tell them to stop waiting.
 	 */
 	atomic_set(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED);
+	smb_mb__after_atomic();
 	wake_up_var(&oo->oo_owner.so_replay.rp_locked);
 	wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
 
@@ -7469,8 +7471,9 @@ nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		goto put_stateid;
 
 	trace_nfsd_deleg_return(stateid);
-	wake_up_var(d_inode(cstate->current_fh.fh_dentry));
 	destroy_delegation(dp);
+	smb_mb__after_atomic();
+	wake_up_var(d_inode(cstate->current_fh.fh_dentry));
 put_stateid:
 	nfs4_put_stid(&dp->dl_stid);
 out:
Signature · 1 line
2.44.0
#3 lore raw
In reply to NeilBrown

Hi Neil,
Do we need a barrier in nfs4_disable_swap() as well ?

10830 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10831 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);

Quoted 1 line
Quoted 1 line
Quoted 1 line

HERE>>>>

10832 wake_up_var(&clp->cl_state);

Regards,
Santosh

Quoted 13 lines · NeilBrown

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

NeilBrown

[PATCH 1/2] nfsd: use clear_and_wake_up_bit()
[PATCH 2/2] nfsd: avoid races with wake_up_var()

author
#4 lore raw
Quoted 7 lines · Santosh Pradhan

Hi Neil,
Do we need a barrier in nfs4_disable_swap() as well ?

10830 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10831 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);

Quoted 1 line
Quoted 1 line
Quoted 1 line

HERE>>>>

10832 wake_up_var(&clp->cl_state);

Probably but that is in fs/nfs/, not fs/nfsd/.

Thanks,
NeilBrown

Quoted 19 lines

Regards,
Santosh

Quoted 13 lines · NeilBrown

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

NeilBrown

[PATCH 1/2] nfsd: use clear_and_wake_up_bit()
[PATCH 2/2] nfsd: avoid races with wake_up_var()

maintainer Reviewed-by on the v1 series
#5 lore raw
In reply to NeilBrown
Quoted 11 lines · NeilBrown

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

NeilBrown

[PATCH 1/2] nfsd: use clear_and_wake_up_bit()
[PATCH 2/2] nfsd: avoid races with wake_up_var()

Nice catches.

Applied
#6 lore raw
In reply to NeilBrown
Quoted 8 lines · NeilBrown

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

[...]

Applied to nfsd-next for v6.12, thanks!

[1/2] nfsd: use clear_and_wake_up_bit()

      commit: 2b9a19d16beda1b2ca5edab47d74b73d4d958b12
[2/2] nfsd: avoid races with wake_up_var()
      commit: a2bf7d13821603fb90c3f6e695bd5fb4ee19de71

Both of these patches threw compilation errors. I corrected those issues before applying. Please check my work.

checkpatch.pl complained about the lack of comment on the added barriers. I felt the code was self-explanatory so made no change.

Signature · 1 line
Chuck Lever
author
#7 lore raw
Quoted 12 lines · Chuck Lever
Quoted 8 lines · NeilBrown

I've been digging into wake_up_bit and wake_up_var recently. There are a lot of places where the required barriers aren't quite right.

This patch fixes them up for nfsd. The bugs are mostly minor, though the rp_locked on might be a credible problem on weakly ordered hosts (e.g. power64).

[...]

Applied to nfsd-next for v6.12, thanks!

Quoted 7 lines

[1/2] nfsd: use clear_and_wake_up_bit()

      commit: 2b9a19d16beda1b2ca5edab47d74b73d4d958b12
[2/2] nfsd: avoid races with wake_up_var()
      commit: a2bf7d13821603fb90c3f6e695bd5fb4ee19de71

Both of these patches threw compilation errors. I corrected those issues before applying. Please check my work.

Thanks for that ... I really shouldn't post patches in the afternoon - I'm not thinking as clearly :-(

Quoted 4 lines

checkpatch.pl complained about the lack of comment on the added barriers. I felt the code was self-explanatory so made no change.

I'd really like a wake_up_var_after_atomic() but Linus wasn't keen on going in that direction.

if/when my improvements land
https://lore.kernel.org/all/[email protected]/
we could change rp_locked to an "int" and use
store_release_wake_up()
which includes that required barrier.

NeilBrown