[PATCH v2 2/3] checkpatch: handle new pr_<level>_cont macros

Thomas Weißschuh posted 3 patches 3 years, 2 months ago
[PATCH v2 2/3] checkpatch: handle new pr_<level>_cont macros
Posted by Thomas Weißschuh 3 years, 2 months ago
These new macros from include/linux/printk.h replace the usage of plain
pr_cont().

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 scripts/checkpatch.pl | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 1e5e66ae5a52..fb1747639c9c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -590,7 +590,7 @@ our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
 
 our $logFunctions = qr{(?x:
 	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
-	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
+	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|_cont|)|
 	TP_printk|
 	WARN(?:_RATELIMIT|_ONCE|)|
 	panic|
@@ -6374,11 +6374,17 @@ sub process {
 		}
 
 # check for logging continuations
-		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
+		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_([a-z]+_)?cont\s*\(/) {
 			WARN("LOGGING_CONTINUATION",
 			     "Avoid logging continuation uses where feasible\n" . $herecurr);
 		}
 
+# check for logging continuations without explicit level
+		if ($line =~ /\bpr_cont\s*\(/) {
+			WARN("LOGGING_CONTINUATION_WITHOUT_LEVEL",
+			     "Avoid logging continuation without level\n" . $herecurr);
+		}
+
 # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
 		if (defined $stat &&
 		    $line =~ /\b$logFunctions\s*\(/ &&
-- 
2.38.1

Re: [PATCH v2 2/3] checkpatch: handle new pr_<level>_cont macros
Posted by Joe Perches 3 years, 2 months ago
On Fri, 2022-11-25 at 20:09 +0100, Thomas Weißschuh wrote:
> These new macros from include/linux/printk.h replace the usage of plain
> pr_cont().
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 1e5e66ae5a52..fb1747639c9c 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -590,7 +590,7 @@ our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
>  
>  our $logFunctions = qr{(?x:
>  	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
> -	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
> +	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|_cont|)|

OK

>  	TP_printk|
>  	WARN(?:_RATELIMIT|_ONCE|)|
>  	panic|
> @@ -6374,11 +6374,17 @@ sub process {
>  		}
>  
>  # check for logging continuations
> -		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
> +		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_([a-z]+_)?cont\s*\(/) {
>  			WARN("LOGGING_CONTINUATION",
>  			     "Avoid logging continuation uses where feasible\n" . $herecurr);
>  		}

OK

> +# check for logging continuations without explicit level
> +		if ($line =~ /\bpr_cont\s*\(/) {
> +			WARN("LOGGING_CONTINUATION_WITHOUT_LEVEL",
> +			     "Avoid logging continuation without level\n" . $herecurr);
> +		}
> +

Not so sure about this one.

I think relatively few situations are going to require interleaving avoidance.

>  # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
>  		if (defined $stat &&
>  		    $line =~ /\b$logFunctions\s*\(/ &&
Re: [PATCH v2 2/3] checkpatch: handle new pr_<level>_cont macros
Posted by Petr Mladek 3 years, 2 months ago
On Fri 2022-11-25 12:17:05, Joe Perches wrote:
> On Fri, 2022-11-25 at 20:09 +0100, Thomas Weißschuh wrote:
> > These new macros from include/linux/printk.h replace the usage of plain
> > pr_cont().
> []
> > --- a/scripts/checkpatch.pl
> > +++ b/scripts/checkpatch.pl
> > @@ -590,7 +590,7 @@ our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
> > +# check for logging continuations without explicit level
> > +		if ($line =~ /\bpr_cont\s*\(/) {
> > +			WARN("LOGGING_CONTINUATION_WITHOUT_LEVEL",
> > +			     "Avoid logging continuation without level\n" . $herecurr);
> > +		}
> > +
> 
> Not so sure about this one.
> 
> I think relatively few situations are going to require interleaving avoidance.

Well, the problem is generic and any pr_cont() is affected except for
NMI context on single CPU system.

I though about a generic solution. We could store the last used printk
log level per-process and per-CPU context. But it does not solve
the situation when an unrelated printk() is printed by a nested
function.

It is exactly the case with try_to_freeze_tasks() in the 3rd patch.
Simplified code:

int freeze_processes(void)
{
	pr_info("Freezing user space processes ... ");
	try_to_freeze_tasks(true);
	pr_info_cont("done.");
}

, where

static int try_to_freeze_tasks(bool user_only)
{
[...]
	if (todo) {
		pr_err("Freezing of tasks %s after %d.%03d seconds "
[...]
}


I would personally add this check into checkpatch.pl. It might help
to make people aware about that pr_cont() is just the best effort.

Best Regards,
Petr