Fix warnings for RISC-V builds
Subsystems (5)
v4 cover Fix warnings for RISC-V builds
This series addresses several compiler warnings found when building the kernel for RISC-V.
The first patch fixes unused variable warnings in the NFS client (including nfs4proc and flexfilelayout) that occur in certain build configurations.
The second patch fixes a format-truncation warning in the MACB ethernet driver by ensuring the snprintf output fits within the destination buffer.
v4:
- Refactor patch 1 to use no_printk() in sunrpc headers instead of marking variables as __maybe_unused. This provides a cleaner, global fix and enables compiler type checking.
v3:
- Expand commit descriptions to include technical details regarding macro expansion, as requested by Andrew Lunn.
- Test the different platform, such as ARM, ARM64, X86_64.
v2:
- Split the original treewide patch into subsystem-specific commits.
- Added more detailed commit descriptions to satisfy checkpatch.
Sean Chang (2):
sunrpc: fix unused variable warnings by using no_printk
net: macb: use ethtool_sprintf to fill ethtool stats strings
drivers/net/ethernet/cadence/macb_main.c | 7 ++----- include/linux/sunrpc/debug.h | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
PATCH v4 1/2 sunrpc: fix unused variable warnings by using no_printk
When CONFIG_SUNRPC_DEBUG is disabled, the dfprintk() macros currently expand to empty do-while loops. This causes variables used solely within these calls to appear unused, triggering -Wunused-variable warnings.
Instead of marking every affected variable with __maybe_unused, update the dfprintk and dfprintk_rcu stubs to use no_printk(). This allows the compiler to see the variables and perform type checking without emitting any code, thus silencing the warnings globally for these macros.
--- include/linux/sunrpc/debug.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sunrpc/debug.h +2 -2
@@ -52,8 +52,8 @@ do { \# define RPC_IFDEBUG (x) x#else # define ifdebug (fac) if (0) -# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)# define RPC_IFDEBUG (x)#endif
Signature · 1 line
2.34.1
PATCH v4 2/2 net: macb: use ethtool_sprintf to fill ethtool stats strings
In reply to Sean ChangThe RISC-V toolchain triggers a stringop-truncation warning when using snprintf() with a fixed ETH_GSTRING_LEN (32 bytes) buffer.
Convert the driver to use the modern ethtool_sprintf() API from linux/ethtool.h. This removes the need for manual snprintf() and memcpy() calls, handles the 32-byte padding automatically, and simplifies the logic by removing manual pointer arithmetic.
--- drivers/net/ethernet/cadence/macb_main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +2 -5
@@ -3145,7 +3145,6 @@ static int gem_get_sset_count(struct net_device *dev, int sset)static void gem_get_ethtool_strings (struct net_device *dev,u32 sset,u8 *p) { -char stat_string[ETH_GSTRING_LEN ];struct macb *bp =netdev_priv (dev);struct macb_queue *queue;unsigned int i;
@@ -3158,10 +3157,8 @@ static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p) ETH_GSTRING_LEN);for (q =0 , queue = bp->queues; q < bp->num_queues; ++q, ++queue) { -for (i =0 ; i <QUEUE_STATS_LEN ; i++, p +=ETH_GSTRING_LEN ) { -snprintf (stat_string,ETH_GSTRING_LEN ,"q%d_%s" , - q, queue_statistics[i].stat_string); -memcpy (p, stat_string,ETH_GSTRING_LEN ); +for (i =0 ; i <QUEUE_STATS_LEN ; i++) { +ethtool_sprintf (&p,"q%u_%s" , q, queue_statistics[i].stat_string); } }break ;
Signature · 1 line
2.34.1
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
In reply to Sean ChangQuoted 34 lines · Sean Chang
When
CONFIG_SUNRPC_DEBUGis disabled, thedfprintk()macros currently expand to empty do-while loops. This causes variables used solely within these calls to appear unused, triggering -Wunused-variable warnings.Instead of marking every affected variable with
__maybe_unused, update the dfprintk anddfprintk_rcustubs to useno_printk(). This allows the compiler to see the variables and perform type checking without emitting any code, thus silencing the warnings globally for these macros.Suggested-by Andrew Lunn <[email protected]>Signed-off-by Sean Chang <[email protected]>--- include/linux/sunrpc/debug.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/sunrpc/debug.h +2
-2@@ -52,8 +52,8 @@ do { \# define RPC_IFDEBUG (x) x#else # define ifdebug (fac) if (0) -# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)# define RPC_IFDEBUG (x)#endif Signature · 1 line
2.34.1
Seems obviously good to me.
Signature · 1 line
Chuck Lever
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
In reply to Sean ChangQuoted 5 lines · Sean Chang
When
CONFIG_SUNRPC_DEBUGis disabled, thedfprintk()macros currently expand to empty do-while loops. This causes variables used solely within these calls to appear unused, triggering -Wunused-variable warnings.
Quoted 8 lines
Instead of marking every affected variable with
__maybe_unused, update the dfprintk anddfprintk_rcustubs to useno_printk(). This allows the compiler to see the variables and perform type checking without emitting any code, thus silencing the warnings globally for these macros.Suggested-by Andrew Lunn <[email protected]>Signed-off-by Sean Chang <[email protected]>
Andrew
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
In reply to Sean ChangQuoted 22 lines · On Fri, 27 Feb 2026 23:26:23 +0800 Sean Chang <[email protected]> wrote:
When
CONFIG_SUNRPC_DEBUGis disabled, thedfprintk()macros currently expand to empty do-while loops. This causes variables used solely within these calls to appear unused, triggering -Wunused-variable warnings.Instead of marking every affected variable with
__maybe_unused, update the dfprintk anddfprintk_rcustubs to useno_printk(). This allows the compiler to see the variables and perform type checking without emitting any code, thus silencing the warnings globally for these macros.Suggested-by Andrew Lunn <[email protected]>Signed-off-by Sean Chang <[email protected]>--- include/linux/sunrpc/debug.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/sunrpc/debug.h +0
-0
include/linux/sunrpc/debug.h do { \ · line 56
Quoted 7 lines
include/linux/sunrpc/debug.h +2
-2@@ -52,8 +52,8 @@ do { \# define RPC_IFDEBUG (x) x#else # define ifdebug (fac) if (0) -# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
You can omit fmt, then you don't need the ##
#define dfprintk(fac, ...) no_printk(__VA_ARGS__)
David
Quoted 3 lines
include/linux/sunrpc/debug.h +0
-0# define RPC_IFDEBUG (x)#endif
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 8 lines
include/linux/sunrpc/debug.h do { \ · line 56
Quoted 5 lines
# define ifdebug(fac) if (0)include/linux/sunrpc/debug.h +2
-2-# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)You can omit fmt, then you don't need the ##
#definedfprintk(fac, ...)no_printk(__VA_ARGS__)
/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/
#define no_printk(fmt, ...) \
({ \
if (0) \
_printk(fmt, ##__VA_ARGS__); \
0; \
})Without fmt, gcc cannot do format checking. Or worse, it takes the first member of __VA_ARGS__ as the format, and gives spurious errors?
Andrew
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 14 lines · On Fri, 27 Feb 2026 18:57:33 +0100 Andrew Lunn <[email protected]> wrote:
Quoted 8 lines
include/linux/sunrpc/debug.h do { \ · line 56
Quoted 5 lines
# define ifdebug(fac) if (0)include/linux/sunrpc/debug.h +2
-2-# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)You can omit fmt, then you don't need the ##
#definedfprintk(fac, ...)no_printk(__VA_ARGS__)/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/
#defineno_printk(fmt, ...)\
Quoted 8 lines
({ \ if (0) \ _printk(fmt, ##__VA_ARGS__); \ 0; \ })Without fmt, gcc cannot do format checking. Or worse, it takes the first member of
__VA_ARGS__as the format, and gives spurious errors?
By the time the compiler looks at it the pre-processor has expanded __VA_ARGS__.
So it doesn't matter that the format string is in the __VA_ARGS__ list rather than preceding it.
David
Quoted 2 lines
Andrew
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 24 lines · David Laight
Quoted 20 lines · On Fri, 27 Feb 2026 18:57:33 +0100 Andrew Lunn <[email protected]> wrote:
Quoted 8 lines
include/linux/sunrpc/debug.h do { \ · line 56
Quoted 5 lines
# define ifdebug(fac) if (0)include/linux/sunrpc/debug.h +2
-2-# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)You can omit fmt, then you don't need the ## #define
dfprintk(fac, ...)no_printk(__VA_ARGS__)/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/#define no_printk(fmt, ...) \ ({ \ if (0) \ _printk(fmt, ##__VA_ARGS__); \ 0; \})
Quoted 8 lines
Quoted 2 lines
Without fmt, gcc cannot do format checking. Or worse, it takes the first member of
__VA_ARGS__as the format, and gives spurious errors?By the time the compiler looks at it the pre-processor has expanded
__VA_ARGS__.
So it doesn't matter that the format string is in the__VA_ARGS__list rather than preceding it.
Hi David,
Thanks for the explanation. I agree that since __VA_ARGS__ will always contain the format string in this context, the ## and explicit fmt are indeed redundant. Since no_printk itself already handles the ##__VA_ARGS__ logic internally, there's no need to duplicate it in the dfprintk definition.
I'll switch to the simpler in v5:
-# define dfprintk(fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
-# define dfprintk_rcu(fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
+#define dfprintk(fac, ...) no_printk(__VA_ARGS__)
+# define dfprintk_rcu(fac, ...) no_printk(__VA_ARGS__)
Best regards,
Sean
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 45 lines · Sean Chang
Quoted 24 lines · David Laight
Quoted 20 lines · On Fri, 27 Feb 2026 18:57:33 +0100 Andrew Lunn <[email protected]> wrote:
Quoted 8 lines
include/linux/sunrpc/debug.h do { \ · line 56
Quoted 5 lines
# define ifdebug(fac) if (0)include/linux/sunrpc/debug.h +2
-2-# define dfprintk (fac, fmt, ...) do {} while (0) -# define dfprintk_rcu (fac, fmt, ...) do {} while (0) +# define dfprintk (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +# define dfprintk_rcu (fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__)You can omit fmt, then you don't need the ## #define
dfprintk(fac, ...)no_printk(__VA_ARGS__)/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/#define no_printk(fmt, ...) \ ({ \ if (0) \ _printk(fmt, ##__VA_ARGS__); \ 0; \})
Quoted 8 lines
Quoted 2 lines
Without fmt, gcc cannot do format checking. Or worse, it takes the first member of
__VA_ARGS__as the format, and gives spurious errors?By the time the compiler looks at it the pre-processor has expanded
__VA_ARGS__.
So it doesn't matter that the format string is in the__VA_ARGS__list rather than preceding it.Hi David,
Thanks for the explanation. I agree that since__VA_ARGS__will always contain the format string in this context, the ## and explicit fmt are indeed redundant. Sinceno_printkitself already handles the ##__VA_ARGS__ logic internally, there's no need to duplicate it in the dfprintk definition.I'll switch to the simpler in v5:
Review
Quoted 4 lines
(quoted diff) +2
-2-# define dfprintk(fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) -# define dfprintk_rcu(fac, fmt, ...) no_printk(fmt, ##__VA_ARGS__) +#define dfprintk(fac, ...) no_printk(__VA_ARGS__) +# define dfprintk_rcu(fac, ...) no_printk(__VA_ARGS__)
HI all,
I've also identified the cause of the build error in fs/nfsd/nfsfh.c reported by syzbot [1]. The "use of undeclared identifier 'buf'" occurs because buf is only declared within the RPC_IFDEBUG macro, which is removed when CONFIG_SUNRPC_DEBUG is disabled.
To fix this, I will follow the pattern used in net/sunrpc/xprtrdma/ svc_rdma_transport.c by wrapping the dprintk call that references buf within an #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) block.
static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt) {
....RPC_IFDEBUG(struct sockaddr *sap);
...
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)dprintk("svcrdma: new connection accepted on device %s:\n", dev->name); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr; dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap)); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr; dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap)); dprintk(" max_sge : %d\n", newxprt->sc_max_send_sges); dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);dprintk(" rdma_rw_ctxs : %d\n", ctxts);dprintk(" max_requests : %d\n", newxprt->sc_max_requests); dprintk(" ord : %d\n", conn_param.initiator_depth);
#endif
...
}
The refactor in fs/nfsd/nfsfh.c will look like this:
static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp, struct svc_cred *cred,
struct svc_export *exp)
{
/* Check if the request originated from a secure port. */
if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) {
RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);+#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
dprintk("nfsd: request from insecure port %s!\n", svc_print_addr(rqstp, buf, sizeof(buf)));
+#endif
return nfserr_perm;
}
/* Set user creds for this exportpoint */
return nfserrno(nfsd_setuser(cred, exp));
}This fix, along with the macro simplification suggested by David, will be included in v5.
[1] https://lore.kernel.org/all/[email protected]/
Best regards,
Sean
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 8 lines
HI all,
I've also identified the cause of the build error infs/nfsd/nfsfh.creported by syzbot [1]. The "use of undeclared identifier 'buf'" occurs because buf is only declared within theRPC_IFDEBUGmacro, which is removed whenCONFIG_SUNRPC_DEBUGis disabled.
To fix this, I will follow the pattern used innet/sunrpc/xprtrdma/svc_rdma_transport.cby wrapping the dprintk call that references buf within an #ifIS_ENABLED(CONFIG_SUNRPC_DEBUG)block.
Please try to avoid adding such #if code. Compile testing does not work as well if there are millions of #if def combinations. Ideally we want the stub functions to allow as much as possible to be compiled, and then let the optimizer throw it out because it is unreachable.
Quoted 30 lines
static struct
svc_xprt*svc_rdma_accept(structsvc_xprt*xprt) {
....RPC_IFDEBUG(struct sockaddr *sap);
...
#ifIS_ENABLED(CONFIG_SUNRPC_DEBUG)dprintk("svcrdma: new connection accepted on device %s:\n", dev->name); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap)); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));dprintk(" max_sge : %d\n", newxprt->sc_max_send_sges);dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);dprintk(" rdma_rw_ctxs : %d\n", ctxts);dprintk(" max_requests : %d\n", newxprt->sc_max_requests);dprintk(" ord : %d\n", conn_param.initiator_depth);
#endif
...
}The refactor in
fs/nfsd/nfsfh.cwill look like this:static
__be32nfsd_setuser_and_check_port(structsvc_rqst*rqstp, structsvc_cred*cred,
structsvc_export*exp){ /* Check if the request originated from a secure port. */ if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) { RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Review
Quoted 4 lines
(quoted diff) +2
-0+#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) dprintk("nfsd: request from insecure port %s!\n", svc_print_addr(rqstp, buf, sizeof(buf))); +#endif
In this case, now dprintk() uses it arguments, i think you can drop the RPC_IFDEBUG() and always have buf. This code then gets compiled. Ask make to produce fs/nfsd/nfsfh.lst and see if it generated any code for this, or has it optimized it out.
Andrew
Re: [PATCH v4 1/2] sunrpc: fix unused variable warnings by using no_printk
Quoted 29 lines · Andrew Lunn
Please try to avoid adding such #if code. Compile testing does not work as well if there are millions of #if def combinations. Ideally we want the stub functions to allow as much as possible to be compiled, and then let the optimizer throw it out because it is unreachable.
Quoted 23 lines
static struct
svc_xprt*svc_rdma_accept(structsvc_xprt*xprt) {
....RPC_IFDEBUG(struct sockaddr *sap);
...
#ifIS_ENABLED(CONFIG_SUNRPC_DEBUG)dprintk("svcrdma: new connection accepted on device %s:\n", dev->name); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap)); sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));dprintk(" max_sge : %d\n", newxprt->sc_max_send_sges);dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);dprintk(" rdma_rw_ctxs : %d\n", ctxts);dprintk(" max_requests : %d\n", newxprt->sc_max_requests);dprintk(" ord : %d\n", conn_param.initiator_depth);
#endif
...
}The refactor in
fs/nfsd/nfsfh.cwill look like this:
Quoted 17 lines
Review
Quoted 11 lines
static
__be32nfsd_setuser_and_check_port(structsvc_rqst*rqstp, structsvc_cred*cred,
structsvc_export*exp){ /* Check if the request originated from a secure port. */ if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) { RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);(quoted diff) +2
-0+#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) dprintk("nfsd: request from insecure port %s!\n", svc_print_addr(rqstp, buf, sizeof(buf))); +#endifIn this case, now
dprintk()uses it arguments, i think you can drop theRPC_IFDEBUG()and always have buf. This code then gets compiled. Ask make to producefs/nfsd/nfsfh.lstand see if it generated any code for this, or has it optimized it out.
Hi Andrew,
You are absolutely right. I have verified the generated code with fs/nfsd/nfsfh.lst and net/sunrpc/xprtrdma/svc_rdma_transport.lst under -O2 optimization.
Even after dropping the #ifdef and always declaring the char buf[], the compiler successfully optimizes everything out when CONFIG_SUNRPC_DEBUG is disabled. Specifically:
- There is no stack allocation (no sub %rsp) for the buffer.
- The logic flows directly as if the dprintk lines were never there.
Given this, I agree it is the perfect time to also remove the RPC_IFDEBUG(x) macro entirely, as it is only used in these two files and is now redundant with the new no_printk approach.
I will send out v6 shortly with these cleanups.
Best Regards,
Sean