Fix warnings for RISC-V builds
Subsystems (5)
v3 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.
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):
nfs: fix unused variable warning when CONFIG_SUNRPC_DEBUG is disabled
net: macb: use ethtool_sprintf to fill ethtool stats strings
drivers/net/ethernet/cadence/macb_main.c | 7 ++----- fs/nfs/flexfilelayout/flexfilelayout.c | 2 +- fs/nfs/flexfilelayout/flexfilelayoutdev.c | 3 ++- fs/nfs/nfs4proc.c | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/2 nfs: fix unused variable warning when CONFIG_SUNRPC_DEBUG is disabled
When CONFIG_SUNRPC_DEBUG is disabled, the dprintk() macro expands to an empty do-while loop. This causes variables used solely within dprintk() calls to appear unused to the compiler, triggering -Wunused-variable warnings.
Fix this by adding __maybe_unused to the affected variables. This ensures the code builds cleanly across different configurations, including RISC-V, ARM, and ARM64 allmodconfig, as verified in the mailing list discussion.
--- fs/nfs/flexfilelayout/flexfilelayout.c | 2 +- fs/nfs/flexfilelayout/flexfilelayoutdev.c | 3 ++- fs/nfs/nfs4proc.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +1 -1
@@ -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) {
fs/nfs/flexfilelayout/flexfilelayoutdev.c +2 -1
@@ -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 );
fs/nfs/nfs4proc.c +1 -1
@@ -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,
Signature · 1 line
2.34.1
PATCH v3 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 v3 1/2] nfs: fix unused variable warning when CONFIG_SUNRPC_DEBUG is disabled
In reply to Sean ChangQuoted 22 lines · Sean Chang
When
CONFIG_SUNRPC_DEBUGis disabled, thedprintk()macro expands to an empty do-while loop. This causes variables used solely withindprintk()calls to appear unused to the compiler, triggering -Wunused-variable warnings.Fix this by adding
__maybe_unusedto the affected variables. This ensures the code builds cleanly across different configurations, including RISC-V, ARM, and ARM64 allmodconfig, as verified in the mailing list discussion.Signed-off-by Sean Chang <[email protected]>--- fs/nfs/flexfilelayout/flexfilelayout.c | 2 +- fs/nfs/flexfilelayout/flexfilelayoutdev.c | 3 ++- fs/nfs/nfs4proc.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)fs/nfs/flexfilelayout/flexfilelayout.c +0
-0
fs/nfs/flexfilelayout/flexfilelayout.c static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, · line 1505
Quoted 5 lines
fs/nfs/flexfilelayout/flexfilelayout.c +1
-1@@ -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 ;
Sorry, but this is ugly. There must be a better way to fix this.
Maybe look at no_printk().
https://elixir.bootlin.com/linux/v6.19.3/source/drivers/video/fbdev/core/fbmon.c#L50
#ifdef DEBUG
#define DPRINTK(fmt, args...) printk(fmt,## args) #else
#define DPRINTK(fmt, args...) no_printk(fmt, ##args) #endif
Andrew
---
pw-bot: cr
Re: [PATCH v3 2/2] net: macb: use ethtool_sprintf to fill ethtool stats strings
In reply to Sean ChangQuoted 9 lines · Sean Chang
The RISC-V toolchain triggers a stringop-truncation warning when using
snprintf()with a fixedETH_GSTRING_LEN(32 bytes) buffer.Convert the driver to use the modern
ethtool_sprintf()API fromlinux/ethtool.h. This removes the need for manualsnprintf()andmemcpy()calls, handles the 32-byte padding automatically, and simplifies the logic by removing manual pointer arithmetic.Signed-off-by Sean Chang <[email protected]>
Andrew
Re: [PATCH v3 1/2] nfs: fix unused variable warning when CONFIG_SUNRPC_DEBUG is disabled
In reply to Andrew LunnQuoted 16 lines · Andrew Lunn
fs/nfs/flexfilelayout/flexfilelayout.c static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, · line 1505
Quoted 11 lines
fs/nfs/flexfilelayout/flexfilelayout.c +1
-1@@ -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 ;Sorry, but this is ugly. There must be a better way to fix this.
Maybe look at
no_printk().
Quoted 8 lines
https://elixir.bootlin.com/linux/v6.19.3/source/drivers/video/fbdev/core/fbmon.c#L50
#ifdef DEBUG
#defineDPRINTK(fmt, args...)printk(fmt,## args)#else
#defineDPRINTK(fmt, args...)no_printk(fmt, ##args)#endif
You are absolutely right, adding __maybe_unused to every variable is indeed ugly and repetitive. I investigated the suggestion of using no_printk(). It provides the same dummy-function behavior while allowing the compiler to perform type checking on the arguments. This effectively silences the -Wunused-variable warnings without generating any machine code.
I propose modifying include/linux/sunrpc/debug.h to update the stubs for dfprintk and dfprintk_rcu when CONFIG_SUNRPC_DEBUG is disabled:
- # 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__)Best regards,
Sean