[-v4] checkpatch: Check for .byte-spelled insn opcodes documentation on x86
diff mbox series

Message ID b74a95944a4bc6be1ea4ae8cf065c23e03511ba5.camel@perches.com
State New, archived
Headers show
Series
  • [-v4] checkpatch: Check for .byte-spelled insn opcodes documentation on x86
Related show

Commit Message

Joe Perches Oct. 12, 2020, 5:09 p.m. UTC
From: Borislav Petkov <bp@suse.de>

Instruction opcode bytes spelled using the gas directive .byte should
carry a comment above them stating which binutils version has added
support for the instruction mnemonic so that they can be replaced with
the mnemonic when that binutils version is equal or less than the
minimum-supported version by the kernel.

Add a check for that.

Requested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Signed-off-by: Joe Perches <joe@perches.com>
---

v4: trivial neatening of $Hex_byte and adding a mechanism to
    only emit the message once per patched file (Joe)

 scripts/checkpatch.pl | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Borislav Petkov Oct. 12, 2020, 5:15 p.m. UTC | #1
On Mon, Oct 12, 2020 at 10:09:44AM -0700, Joe Perches wrote:
> From: Borislav Petkov <bp@suse.de>
> 
> Instruction opcode bytes spelled using the gas directive .byte should
> carry a comment above them stating which binutils version has added
> support for the instruction mnemonic so that they can be replaced with
> the mnemonic when that binutils version is equal or less than the
> minimum-supported version by the kernel.
> 
> Add a check for that.
> 
> Requested-by: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Borislav Petkov <bp@suse.de>
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
> 
> v4: trivial neatening of $Hex_byte and adding a mechanism to
>     only emit the message once per patched file (Joe)
> 
>  scripts/checkpatch.pl | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)

./scripts/checkpatch.pl /tmp/test
Global symbol "$rawline" requires explicit package name (did you forget to declare "my $rawline"?) at ./scripts/checkpatch.pl line 6943.
Global symbol "$herecurr" requires explicit package name (did you forget to declare "my $herecurr"?) at ./scripts/checkpatch.pl line 6948.
Execution of ./scripts/checkpatch.pl aborted due to compilation errors.

No workie.
Joe Perches Oct. 12, 2020, 5:17 p.m. UTC | #2
On Mon, 2020-10-12 at 19:15 +0200, Borislav Petkov wrote:
> On Mon, Oct 12, 2020 at 10:09:44AM -0700, Joe Perches wrote:
> > From: Borislav Petkov <bp@suse.de>
> > 
> > Instruction opcode bytes spelled using the gas directive .byte should
> > carry a comment above them stating which binutils version has added
> > support for the instruction mnemonic so that they can be replaced with
> > the mnemonic when that binutils version is equal or less than the
> > minimum-supported version by the kernel.
> > 
> > Add a check for that.
> > 
> > Requested-by: Peter Zijlstra <peterz@infradead.org>
> > Signed-off-by: Borislav Petkov <bp@suse.de>
> > Signed-off-by: Joe Perches <joe@perches.com>
> > ---
> > 
> > v4: trivial neatening of $Hex_byte and adding a mechanism to
> >     only emit the message once per patched file (Joe)
> > 
> >  scripts/checkpatch.pl | 18 ++++++++++++++++++
> >  1 file changed, 18 insertions(+)
> 
> ./scripts/checkpatch.pl /tmp/test
> Global symbol "$rawline" requires explicit package name (did you forget to declare "my $rawline"?) at ./scripts/checkpatch.pl line 6943.
> Global symbol "$herecurr" requires explicit package name (did you forget to declare "my $herecurr"?) at ./scripts/checkpatch.pl line 6948.
> Execution of ./scripts/checkpatch.pl aborted due to compilation errors.
> 
> No workie.

Workie here.  This is against -next.

$ ./scripts/checkpatch.pl -f arch/x86/include/asm/smap.h
WARNING: Please consider documenting which binutils version supports these .byte-spelled insn opcodes by adding "binutils version <num>" in a comment above them
#16: FILE: arch/x86/include/asm/smap.h:16:
+#define __ASM_CLAC	".byte 0x0f,0x01,0xca"

WARNING: Prefer using '"%s...", __func__' to using 'smap_save', this function's name, in a string
#60: FILE: arch/x86/include/asm/smap.h:60:
+	asm volatile ("# smap_save\n\t"

WARNING: Prefer using '"%s...", __func__' to using 'smap_restore', this function's name, in a string
#71: FILE: arch/x86/include/asm/smap.h:71:
+	asm volatile ("# smap_restore\n\t"

total: 0 errors, 3 warnings, 99 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

arch/x86/include/asm/smap.h has style problems, please review.

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.
Borislav Petkov Oct. 12, 2020, 5:31 p.m. UTC | #3
On Mon, Oct 12, 2020 at 10:17:56AM -0700, Joe Perches wrote:
> Workie here.  This is against -next.

Nevermind - I had an old version in that branch.

What I mind to, however, is:

"adding a mechanism to only emit the message once per patched file (Joe)"

This needs to happen for every .byte line which doesn't have a comment
documenting the binutils version.

Thx.
Joe Perches Oct. 12, 2020, 5:40 p.m. UTC | #4
On Mon, 2020-10-12 at 19:31 +0200, Borislav Petkov wrote:
> On Mon, Oct 12, 2020 at 10:17:56AM -0700, Joe Perches wrote:
> > Workie here.  This is against -next.
> 
> Nevermind - I had an old version in that branch.
> 
> What I mind to, however, is:
> 
> "adding a mechanism to only emit the message once per patched file (Joe)"
> 
> This needs to happen for every .byte line which doesn't have a comment
> documenting the binutils version.

Why?  I think it unnecessary.
It's noisy and would also be duplicative in the code.

/* binutils version x.y */
#define __ASM_CLAC	".byte 0x0f,0x01,0xca"
#define __ASM_STAC	".byte 0x0f,0x01,0xcb"

Both should not need separate binutils version info
if added in a patch context.
Borislav Petkov Oct. 12, 2020, 5:55 p.m. UTC | #5
On Mon, Oct 12, 2020 at 10:40:07AM -0700, Joe Perches wrote:
> Why?  I think it unnecessary.

Joe, I'm sick'n'tired of debating with you what needs to happen.

Please forget that patch altogether - I'll add the functionality to our
own checker script where I don't need to debate with you what I have to
and I have not to do.

And I won't be getting private emails about you teaching me how I should
have replied to your mail. The only "mistake" I made is even thinking
about adding this to checkpatch. Won't happen again.
Joe Perches Oct. 12, 2020, 6:03 p.m. UTC | #6
On Mon, 2020-10-12 at 19:55 +0200, Borislav Petkov wrote:
> On Mon, Oct 12, 2020 at 10:40:07AM -0700, Joe Perches wrote:
> > Why?  I think it unnecessary.
> 
> Joe, I'm sick'n'tired of debating with you what needs to happen.
> 
> Please forget that patch altogether

Fine by me.

>  - I'll add the functionality to our
> own checker script where I don't need to debate with you what I have to
> and I have not to do.
> 
> And I won't be getting private emails about you teaching me how I should
> have replied to your mail. The only "mistake" I made is even thinking
> about adding this to checkpatch. Won't happen again.

For the record:

My single-line private email to you was not "teaching",
it was just a statement of what I would have preferred.

Just remain the "best you" you want to be...

---

On Mon, 2020-10-12 at 10:41 -0700, Joe Perches wrote:
> On Mon, 2020-10-12 at 19:31 +0200, Borislav Petkov wrote:
> > On Mon, Oct 12, 2020 at 10:17:56AM -0700, Joe Perches wrote:
> > > Workie here.  This is against -next.
> > Nevermind - I had an old version in that branch.
> btw: adding "my mistake" would have been more appreciated...

Patch
diff mbox series

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index fab38b493cef..7568f583701c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -414,6 +414,7 @@  our $Lval	= qr{$Ident(?:$Member)*};
 our $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
 our $Binary	= qr{(?i)0b[01]+$Int_type?};
 our $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
+our $Hex_byte	= qr{(?i)0x[0-9a-f]{1,2}\b};
 our $Int	= qr{[0-9]+$Int_type?};
 our $Octal	= qr{0[0-7]+$Int_type?};
 our $String	= qr{"[X\t]*"};
@@ -2408,6 +2409,7 @@  sub process {
 	my $comment_edge = 0;
 	my $first_line = 0;
 	my $p1_prefix = '';
+	my $warned_binutils = 0;
 
 	my $prev_values = 'E';
 
@@ -2589,6 +2591,7 @@  sub process {
 			$realfile =~ s@^([^/]*)/@@ if (!$file);
 			$in_commit_log = 0;
 			$found_file = 1;
+			$warned_binutils = 0;
 		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
 			$realfile = $1;
 			$realfile =~ s@^([^/]*)/@@ if (!$file);
@@ -2606,6 +2609,7 @@  sub process {
 				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
 			}
 			$found_file = 1;
+			$warned_binutils = 0;
 		}
 
 #make up the handle for any error we report on this line
@@ -6954,6 +6958,20 @@  sub process {
 			WARN("DUPLICATED_SYSCTL_CONST",
 				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
 		}
+
+# document which binutils version supports the actual insn mnemonic so that the naked opcode bytes can be replaced.
+# x86-only. Upper limit is rather arbitrary (max insn length on x86) but imposed so as to avoid perl aborts.
+		if (!$warned_binutils &&
+		    $realfile =~ m@^arch/x86/@ &&
+		    $rawline =~ /\s*\.byte\s+$Hex_byte(?:\s*,\s*$Hex_byte){0,14}/) {
+
+			my $comment = ctx_locate_comment($file ? 0 : $first_line, $linenr);
+			if ($comment !~ /binutils (?:version )*[0-9.]+/ms) {
+				WARN("MISSING_BINUTILS_VERSION",
+				     "Please consider documenting which binutils version supports these .byte-spelled insn opcodes by adding \"binutils version <num>\" in a comment above them\n" . $herecurr);
+				$warned_binutils = 1;
+			}
+		}
 	}
 
 	# If we have no input at all, then there is nothing to report on