[PATCH v2 1/2] nfs: fix unused variable warnings

Sean Chang posted 2 patches 2 weeks, 2 days ago
There is a newer version of this series
[PATCH v2 1/2] nfs: fix unused variable warnings
Posted by Sean Chang 2 weeks, 2 days ago
Add __maybe_unused to variables only used in specific configurations
to silence compiler warnings found during RISC-V builds.

Signed-off-by: Sean Chang <seanwascoding@gmail.com>
---
v2:
- Split the original treewide patch into subsystem-specific commits.
- Added more detailed commit descriptions to satisfy checkpatch.

 fs/nfs/flexfilelayout/flexfilelayout.c    | 2 +-
 fs/nfs/flexfilelayout/flexfilelayoutdev.c | 3 ++-
 fs/nfs/nfs4proc.c                         | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c
index 9056f05a67dc..de9e8bad6af2 100644
--- a/fs/nfs/flexfilelayout/flexfilelayout.c
+++ b/fs/nfs/flexfilelayout/flexfilelayout.c
@@ -1502,7 +1502,7 @@ static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
 {
 	struct nfs4_ff_layout_mirror *mirror;
 	u32 status = *op_status;
-	int err;
+	int err __maybe_unused;
 
 	if (status == 0) {
 		switch (error) {
diff --git a/fs/nfs/flexfilelayout/flexfilelayoutdev.c b/fs/nfs/flexfilelayout/flexfilelayoutdev.c
index c2d8a13a9dbd..3fb8dba0abf5 100644
--- a/fs/nfs/flexfilelayout/flexfilelayoutdev.c
+++ b/fs/nfs/flexfilelayout/flexfilelayoutdev.c
@@ -53,7 +53,8 @@ nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
 	u32 mp_count;
 	u32 version_count;
 	__be32 *p;
-	int i, ret = -ENOMEM;
+	int i;
+	int ret __maybe_unused = -ENOMEM;
 
 	/* set up xdr stream */
 	scratch = folio_alloc(gfp_flags, 0);
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
index 180229320731..f76c23cdc888 100644
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -9241,7 +9241,7 @@ static int _nfs4_proc_create_session(struct nfs_client *clp,
 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
 {
 	int status;
-	unsigned *ptr;
+	unsigned *ptr __maybe_unused;
 	struct nfs4_session *session = clp->cl_session;
 	struct nfs4_add_xprt_data xprtdata = {
 		.clp = clp,
-- 
2.34.1
Re: [PATCH v2 1/2] nfs: fix unused variable warnings
Posted by Andrew Lunn 2 weeks, 2 days ago
On Tue, Feb 17, 2026 at 01:49:49AM +0800, Sean Chang wrote:
> Add __maybe_unused to variables only used in specific configurations
> to silence compiler warnings found during RISC-V builds.

Could you give more details.

>  int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
>  {
>  	int status;
> -	unsigned *ptr;
> +	unsigned *ptr __maybe_unused;
>  	struct nfs4_session *session = clp->cl_session;
>  	struct nfs4_add_xprt_data xprtdata = {
>  		.clp = clp,

Lets look at this function

int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
{
	int status;
	unsigned *ptr;
	struct nfs4_session *session = clp->cl_session;
	struct nfs4_add_xprt_data xprtdata = {
		.clp = clp,
	};
	struct rpc_add_xprt_test rpcdata = {
		.add_xprt_test = clp->cl_mvops->session_trunk,
		.data = &xprtdata,
	};

	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);

	status = _nfs4_proc_create_session(clp, cred);
	if (status)
		goto out;

	/* Init or reset the session slot tables */
	status = nfs4_setup_session_slot_tables(session);
	dprintk("slot table setup returned %d\n", status);
	if (status)
		goto out;

	ptr = (unsigned *)&session->sess_id.data[0];
	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
	rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
out:
	return status;
}

There is no #ifdef'ery here. How is ptr not used? Is status always
true, so the goto it always taken? But then rpcdata should also be
unused?

	Andrew
Re: [PATCH v2 1/2] nfs: fix unused variable warnings
Posted by Sean 2 weeks, 1 day ago
On Tue, Feb 17, 2026 at 4:06 AM Andrew Lunn <andrew@lunn.ch> wrote:
>
>
> There is no #ifdef'ery here. How is ptr not used? Is status always
> true, so the goto it always taken? But then rpcdata should also be
> unused?
>

If CONFIG_SUNRPC_DEBUG is not enabled, dprintk expands to
do {} while (0) during preprocessing. This means unsigned *ptr has
no remaining references in the function, as its only 'read' was inside
the macro.

Regarding rpcdata, the compiler's unused-variable check doesn't care if a
specific branch (like the goto out) might skip the variable's usage at runtime.
As long as there is at least one valid code path where the variable is
referenced
which there is for rpcdata in the call to
rpc_clnt_probe_trunked_xprts() -> the warning is avoided.
For ptr, the macro expansion leaves it with zero references in any path.

Best regards,
Sean