Skip to contentSkip to conversation

NFSD: move accumulated callback ops to per-net namespace

PATCH Superseded Dai Ngo 2 participants 3 messages Feb 25

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Dai Ngo

author reviewer
#0 lore raw

PATCH v2 1/1 NFSD: move accumulated callback ops to per-net namespace

Track accumulated callback operations on a per-network-namespace basis instead of globally, ensuring proper isolation and behavior when running nfsd in containers.

---
 fs/nfsd/netns.h        |  4 +++
 fs/nfsd/nfs4callback.c | 75 ++++++++++++++++++++++--------------------
 fs/nfsd/nfsctl.c       |  5 +++
 fs/nfsd/state.h        |  2 ++
 4 files changed, 51 insertions(+), 35 deletions(-)

v2:
. free memory allocated for nn->nfsd_cb_version4.counts in
nfsd_net_cb_stats_init() on error in nfsd_net_init().

fs/nfsd/netns.h +4 -0
index 9fa600602658..62a5949490ea 100644
@@ -224,6 +224,10 @@ struct nfsd_net {
 	spinlock_t              local_clients_lock;
 	struct list_head	local_clients;
 #endif
+	struct rpc_version	nfsd_cb_version4;
+	const struct rpc_version *nfsd_cb_versions[2];
+	struct rpc_program	nfsd_cb_program;
+	struct rpc_stat		nfsd_cb_stat;
 };
 
 /* Simple check to find out if a given net was properly initialized */
fs/nfsd/nfs4callback.c +40 -35

104 lines not on this page · raw message

fs/nfsd/nfsctl.c +5 -0
index e9acd2cd602c..b24a838afcaa 100644
@@ -2158,6 +2158,9 @@ static __net_init int nfsd_net_init(struct net *net)
 	int retval;
 	int i;
 
+	retval = nfsd_net_cb_stats_init(nn);
+	if (retval)
+		return retval;
 	retval = nfsd_export_init(net);
 	if (retval)
 		goto out_export_error;
@@ -2198,6 +2201,7 @@ static __net_init int nfsd_net_init(struct net *net)
 out_idmap_error:
 	nfsd_export_shutdown(net);
 out_export_error:
+	nfsd_net_cb_stats_shutdown(nn);
 	return retval;
 }
 
@@ -2227,6 +2231,7 @@ static __net_exit void nfsd_net_exit(struct net *net)
 {
 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 
+	nfsd_net_cb_stats_shutdown(nn);
 	nfsd_proc_stat_shutdown(net);
 	percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
 	nfsd_idmap_shutdown(net);
fs/nfsd/state.h +2 -0
index 6fcbf1e427d4..72d98088627a 100644
@@ -877,4 +877,6 @@ struct nfsd4_get_dir_delegation;
 struct nfs4_delegation *nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
 						struct nfsd4_get_dir_delegation *gdd,
 						struct nfsd_file *nf);
+int nfsd_net_cb_stats_init(struct nfsd_net *nn);
+void nfsd_net_cb_stats_shutdown(struct nfsd_net *nn);
 #endif   /* NFSD4_STATE_H */
Signature · 1 line
2.47.3
maintainer
#1 lore raw

I had Claude take a look and it came up with this:

Moves the callback RPC program, version, and stats structures from global statics into struct nfsd_net so that each network namespace gets its own callback counters and program definition.

Quoted 4 lines
fs/nfsd/nfs4callback.c +0 -0
index aea8bdd2fdc49..aad4276b2f1bc 100644

[ ... ]

Quoted 6 lines
fs/nfsd/nfs4callback.c +6 -0
+int nfsd_net_cb_stats_init(struct nfsd_net *nn)
+{
+     nn->nfsd_cb_version4.counts = kzalloc_objs(unsigned int,
+                     ARRAY_SIZE(nfs4_cb_procedures), GFP_KERNEL);
+     if (!nn->nfsd_cb_version4.counts)
+             return -ENOMEM;

[ ... ]

fs/nfsd/nfs4callback.c bool nfsd4_run_cb(struct nfsd4_callback *cb) · line 1786

Quoted 3 lines
fs/nfsd/nfs4callback.c +3 -0
+     nn->nfsd_cb_program.name = "nfs4_cb";
+     nn->nfsd_cb_program.number = NFS4_CALLBACK;
+     nn->nfsd_cb_program.nrvers = sizeof(struct rpc_version *) * 2;
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Should this be ARRAY_SIZE(nn->nfsd_cb_versions) instead? The nrvers field is documented as "number of versions" in struct rpc_program. The old code used ARRAY_SIZE(nfs_cb_version) which evaluated to 2.

sizeof(struct rpc_version *) is 8 on 64-bit, making nrvers 16 here while the nfsd_cb_versions array only has 2 elements. The version bounds check in rpc_new_client() compares args->version against program->nrvers, and rpc_proc_show() iterates from 0 to nrvers - 1 accessing program->version[i].

Quoted 7 lines
fs/nfsd/nfs4callback.c +7 -0
+     nn->nfsd_cb_program.version = &nn->nfsd_cb_versions[0];
+     nn->nfsd_cb_program.pipe_dir_name = "nfsd4_cb";
+     nn->nfsd_cb_program.stats = &nn->nfsd_cb_stat;
+     nn->nfsd_cb_stat.program = &nn->nfsd_cb_program;
+
+     return 0;
+}
Signature · 1 line
Jeff Layton <[email protected]>
author reviewer
#2 lore raw
Quoted 5 lines · Jeff Layton

I had Claude take a look and it came up with this:

Moves the callback RPC program, version, and stats structures from global statics into struct nfsd_net so that each network namespace gets its own callback counters and program definition.

Clause describes it much more precise than I did, pretty soon I'll be out a job :). Fix in v3.

Quoted 24 lines
Quoted 5 lines
fs/nfsd/nfs4callback.c +0 -0
index aea8bdd2fdc49..aad4276b2f1bc 100644

[ ... ]

Quoted 6 lines
fs/nfsd/nfs4callback.c +6 -0
+int nfsd_net_cb_stats_init(struct nfsd_net *nn)
+{
+     nn->nfsd_cb_version4.counts = kzalloc_objs(unsigned int,
+                     ARRAY_SIZE(nfs4_cb_procedures), GFP_KERNEL);
+     if (!nn->nfsd_cb_version4.counts)
+             return -ENOMEM;

[ ... ]

fs/nfsd/nfs4callback.c bool nfsd4_run_cb(struct nfsd4_callback *cb) · line 1786

Quoted 3 lines
fs/nfsd/nfs4callback.c +3 -0
+     nn->nfsd_cb_program.name = "nfs4_cb";
+     nn->nfsd_cb_program.number = NFS4_CALLBACK;
+     nn->nfsd_cb_program.nrvers = sizeof(struct rpc_version *) * 2;
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Should this be ARRAY_SIZE(nn->nfsd_cb_versions) instead? The nrvers field is documented as "number of versions" in struct rpc_program. The old code used ARRAY_SIZE(nfs_cb_version) which evaluated to 2.

Quoted 5 lines

sizeof(struct rpc_version *) is 8 on 64-bit, making nrvers 16 here while the nfsd_cb_versions array only has 2 elements. The version bounds check in rpc_new_client() compares args->version against program->nrvers, and rpc_proc_show() iterates from 0 to nrvers - 1 accessing program->version[i].

Yes, it's a bug. Fix in v3.

Thanks,
-Dai

Quoted 9 lines
Quoted 8 lines
fs/nfsd/nfs4callback.c +7 -0
+     nn->nfsd_cb_program.version = &nn->nfsd_cb_versions[0];
+     nn->nfsd_cb_program.pipe_dir_name = "nfsd4_cb";
+     nn->nfsd_cb_program.stats = &nn->nfsd_cb_stat;
+     nn->nfsd_cb_stat.program = &nn->nfsd_cb_program;
+
+     return 0;
+}