2024-05-06 01:46:34

by Barry Song

[permalink] [raw]
Subject: [PATCH RESEND v6 0/2] codingstyle: avoid unused parameters for a function-like macro

From: Barry Song <[email protected]>

-v6:
* collect ack of Joe, thanks!
* refine docs according to Jonathan, thanks!
* add checkpatch doc according to Joe, thanks!

-v5:
* Simplify the code for Patch 2 according to Joe's suggestions.
* add s-o-b of Barry according to Jeff Johnson
v5 link:
https://lore.kernel.org/all/[email protected]/

-v4:
* fix Xining's email address, s/[email protected]/[email protected]/g
* fix some false positives of checkpatch.pl
* downgrade from ERROR to WARN in checkpatch.pl

Thanks for Joe's comments!

v4 link: https://lore.kernel.org/all/[email protected]/

-v3:
https://lore.kernel.org/all/[email protected]/

A function-like macro could result in build warnings such as
"unused variable." This patchset updates the guidance to
recommend always using a static inline function instead
and also provides checkpatch support for this new rule.

Barry Song (1):
Documentation: coding-style: ask function-like macros to evaluate
parameters

Xining Xu (1):
scripts: checkpatch: check unused parameters for function-like macro

Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++
Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++
scripts/checkpatch.pl | 6 ++++++
3 files changed, 43 insertions(+)

--
2.34.1



2024-05-06 01:46:46

by Barry Song

[permalink] [raw]
Subject: [PATCH RESEND v6 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters

From: Barry Song <[email protected]>

Recent commit 77292bb8ca69c80 ("crypto: scomp - remove memcpy if
sg_nents is 1 and pages are lowmem") leads to warnings on xtensa
and loongarch,
In file included from crypto/scompress.c:12:
include/crypto/scatterwalk.h: In function 'scatterwalk_pagedone':
include/crypto/scatterwalk.h:76:30: warning: variable 'page' set but not used [-Wunused-but-set-variable]
76 | struct page *page;
| ^~~~
crypto/scompress.c: In function 'scomp_acomp_comp_decomp':
>> crypto/scompress.c:174:38: warning: unused variable 'dst_page' [-Wunused-variable]
174 | struct page *dst_page = sg_page(req->dst);
|

The reason is that flush_dcache_page() is implemented as a noop
macro on these platforms as below,

#define flush_dcache_page(page) do { } while (0)

The driver code, for itself, seems be quite innocent and placing
maybe_unused seems pointless,

struct page *dst_page = sg_page(req->dst);

for (i = 0; i < nr_pages; i++)
flush_dcache_page(dst_page + i);

And it should be independent of architectural implementation
differences.

Let's provide guidance on coding style for requesting parameter
evaluation or proposing the migration to a static inline
function.

Signed-off-by: Barry Song <[email protected]>
Suggested-by: Max Filippov <[email protected]>
Reviewed-by: Mark Brown <[email protected]>
Acked-by: Joe Perches <[email protected]>
Cc: Chris Zankel <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Stephen Rothwell <[email protected]>
Cc: Andy Whitcroft <[email protected]>
Cc: Dwaipayan Ray <[email protected]>
Cc: Joe Perches <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Lukas Bulwahn <[email protected]>
Cc: Xining Xu <[email protected]>
---
Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 9c7cf7347394..7e768c65aa92 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -827,6 +827,29 @@ Macros with multiple statements should be enclosed in a do - while block:
do_this(b, c); \
} while (0)

+Function-like macros with unused parameters should be replaced by static
+inline functions to avoid the issue of unused variables:
+
+.. code-block:: c
+
+ static inline void fun(struct foo *foo)
+ {
+ }
+
+Due to historical practices, many files still employ the "cast to (void)"
+approach to evaluate parameters. However, this method is not advisable.
+Inline functions address the issue of "expression with side effects
+evaluated more than once", circumvent unused-variable problems, and
+are generally better documented than macros for some reason.
+
+.. code-block:: c
+
+ /*
+ * Avoid doing this whenever possible and instead opt for static
+ * inline functions
+ */
+ #define macrofun(foo) do { (void) (foo); } while (0)
+
Things to avoid when using macros:

1) macros that affect control flow:
--
2.34.1


2024-05-06 01:47:00

by Barry Song

[permalink] [raw]
Subject: [PATCH RESEND v6 2/2] scripts: checkpatch: check unused parameters for function-like macro

From: Xining Xu <[email protected]>

If function-like macros do not utilize a parameter, it might result in a
build warning. In our coding style guidelines, we advocate for utilizing
static inline functions to replace such macros. This patch verifies
compliance with the new rule.

For a macro such as the one below,

#define test(a) do { } while (0)

The test result is as follows.

WARNING: Argument 'a' is not used in function-like macro
#21: FILE: mm/init-mm.c:20:
+#define test(a) do { } while (0)

total: 0 errors, 1 warnings, 8 lines checked

Signed-off-by: Xining Xu <[email protected]>
Tested-by: Barry Song <[email protected]>
Signed-off-by: Barry Song <[email protected]>
Cc: Chris Zankel <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Stephen Rothwell <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Andy Whitcroft <[email protected]>
Cc: Dwaipayan Ray <[email protected]>
Cc: Joe Perches <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Lukas Bulwahn <[email protected]>
Cc: Max Filippov <[email protected]>
Cc: Jeff Johnson <[email protected]>
Cc: Charlemagne Lasse <[email protected]>
---
Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++
scripts/checkpatch.pl | 6 ++++++
2 files changed, 20 insertions(+)

diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst
index 127968995847..a9fac978a525 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -906,6 +906,20 @@ Macros, Attributes and Symbols

See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/

+ **MACRO_ARG_UNUSED**
+ If function-like macros do not utilize a parameter, it might result
+ in a build warning. We advocate for utilizing static inline functions
+ to replace such macros.
+ For example, for a macro such as the one below::
+
+ #define test(a) do { } while (0)
+
+ there would be a warning like below::
+
+ WARNING: Argument 'a' is not used in function-like macro.
+
+ See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
+
**SINGLE_STATEMENT_DO_WHILE_MACRO**
For the multi-statement macros, it is necessary to use the do-while
loop to avoid unpredictable code paths. The do-while loop helps to
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9c4c4a61bc83..9895d7e38a9f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6040,6 +6040,12 @@ sub process {
CHK("MACRO_ARG_PRECEDENCE",
"Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
}
+
+# check if this is an unused argument
+ if ($define_stmt !~ /\b$arg\b/) {
+ WARN("MACRO_ARG_UNUSED",
+ "Argument '$arg' is not used in function-like macro\n" . "$herectx");
+ }
}

# check for macros with flow control, but without ## concatenation
--
2.34.1


2024-05-06 05:58:13

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH RESEND v6 2/2] scripts: checkpatch: check unused parameters for function-like macro

On Mon, 2024-05-06 at 13:46 +1200, Barry Song wrote:
> From: Xining Xu <[email protected]>
>
> If function-like macros do not utilize a parameter, it might result in a
> build warning. In our coding style guidelines, we advocate for utilizing
> static inline functions to replace such macros. This patch verifies
> compliance with the new rule.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -6040,6 +6040,12 @@ sub process {
> CHK("MACRO_ARG_PRECEDENCE",
> "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
> }
> +
> +# check if this is an unused argument
> + if ($define_stmt !~ /\b$arg\b/) {
> + WARN("MACRO_ARG_UNUSED",
> + "Argument '$arg' is not used in function-like macro\n" . "$herectx");

trivia: This should be aligned to the open parenthesis.

Otherwise:
Acked-by: Joe Perches <[email protected]>


2024-05-06 07:27:29

by Barry Song

[permalink] [raw]
Subject: Re: [PATCH RESEND v6 2/2] scripts: checkpatch: check unused parameters for function-like macro

>> From: Xining Xu <[email protected]>
>>
>> If function-like macros do not utilize a parameter, it might result in a
>> build warning. In our coding style guidelines, we advocate for utilizing
>> static inline functions to replace such macros. This patch verifies
>> compliance with the new rule.
> []
>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
>> @@ -6040,6 +6040,12 @@ sub process {
>> CHK("MACRO_ARG_PRECEDENCE",
>> "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
>> }
>> +
>> +# check if this is an unused argument
>> + if ($define_stmt !~ /\b$arg\b/) {
>> + WARN("MACRO_ARG_UNUSED",
>> + "Argument '$arg' is not used in function-like macro\n" . "$herectx");
>
> trivia: This should be aligned to the open parenthesis.
>

Hi Joe,
I assume you mean the below?

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9895d7e38a9f..2b812210b412 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6044,7 +6044,7 @@ sub process {
# check if this is an unused argument
if ($define_stmt !~ /\b$arg\b/) {
WARN("MACRO_ARG_UNUSED",
- "Argument '$arg' is not used in function-like macro\n" . "$herectx");
+ "Argument '$arg' is not used in function-like macro\n" . "$herectx");
}
}
> Otherwise:
> Acked-by: Joe Perches <[email protected]>

Thanks!

-Barry


2024-05-06 16:32:33

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH RESEND v6 2/2] scripts: checkpatch: check unused parameters for function-like macro

On Mon, 2024-05-06 at 19:16 +1200, Barry Song wrote:
> > > From: Xining Xu <[email protected]>
> > >
> > > If function-like macros do not utilize a parameter, it might result in a
> > > build warning. In our coding style guidelines, we advocate for utilizing
> > > static inline functions to replace such macros. This patch verifies
> > > compliance with the new rule.
> > []
> > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > []
> > > @@ -6040,6 +6040,12 @@ sub process {
> > > ? CHK("MACRO_ARG_PRECEDENCE",
> > > ? "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
> > > ? }
> > > +
> > > +# check if this is an unused argument
> > > + if ($define_stmt !~ /\b$arg\b/) {
> > > + WARN("MACRO_ARG_UNUSED",
> > > + "Argument '$arg' is not used in function-like macro\n" . "$herectx");
> >
> > trivia: This should be aligned to the open parenthesis.
> >
>
> Hi Joe,
> I assume you mean the below?
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 9895d7e38a9f..2b812210b412 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -6044,7 +6044,7 @@ sub process {
> ?# check if this is an unused argument
> ? if ($define_stmt !~ /\b$arg\b/) {
> ? WARN("MACRO_ARG_UNUSED",
> - "Argument '$arg' is not used in function-like macro\n" . "$herectx");
> + "Argument '$arg' is not used in function-like macro\n" . "$herectx");
> ? }
> ? }
> > Otherwise:
> > Acked-by: Joe Perches <[email protected]>

Yes, but please send a v7 instead of a separate patch
to akpm or perhaps Andrew could combine them or fix it
when applying v6