checkpatch: Check for .byte-spelled insn opcodes documentation on x86
PATCH 1/1 checkpatch: Check for .byte-spelled insn opcodes documentation on x86
From: Borislav Petkov <[email protected]>
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.
--- scripts/checkpatch.pl | 12 ++++++++++++ 1 file changed, 12 insertions(+)
scripts/checkpatch.pl +12 -0
@@ -6858,6 +6858,18 @@ 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. + if ($rawline =~ /(\.byte[\s0-9a-fx,]+)/ && $realfile =~ "^arch/x86/") { + my $comment = ctx_locate_comment(0, $linenr); + if (! $comment || ($comment !~ /binutils version [0-9.]+/ms)) { + WARN("MISSING_BINUTILS_VERSION", + "Please document which binutils version supports these .byte-spelled\n" . + "\tinsn opcodes by adding \"binutils version <num>\" in a comment" . + " above them.\n" . $herecurr); + } + } } # If we have no input at all, then there is nothing to report on
Signature · 1 line
2.21.0
Joe Perches
maintainerQuoted 9 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
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.
OK but several notes:
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 6864
Quoted 8 lines
scripts/checkpatch.pl +4
-0@@ -6858,6 +6858,18 @@ 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. + if ($rawline =~ /(\.byte(?:0[xX][0-9a-fA-F]+0-9a-fx,]+)/ && $realfile =~ "^arch/x86/") {
Given the location, this only works on .c and .h files. It does not work on .S files. Should it?
No need for a capture group.
Please use @ not " as all the other $realfile comparisons use that form when expecting a /
So it looks like the regex would be more complete as:
if ($realfile =~ m@^arch/x86/@ &&
$rawline =~ /\.byte\s+(?:$Constant|(?:\\)?$Ident|"\s*$Ident)\b/) {etc...
scripts/checkpatch.pl sub process { · line 6865
Quoted 1 line
scripts/checkpatch.pl +1
-0+ my $comment = ctx_locate_comment(0, $linenr);
A patch can modify any number of files.
This should use ctx_locate_comment($file ? 0 : $first_line, $linenr) as checkpatch tests work on patch contexts not the entire file before this line.
scripts/checkpatch.pl sub process { · line 6866
Quoted 1 line
scripts/checkpatch.pl +1
-0+ if (! $comment || ($comment !~ /binutils version [0-9.]+/(ms)) {
No need for the $!comment test
scripts/checkpatch.pl sub process { · line 6870
Quoted 4 lines
scripts/checkpatch.pl +4
-0+ WARN("MISSING_BINUTILS_VERSION", + "Please document which binutils version supports these .byte-spelled\n" . + "\tinsn opcodes by adding \"binutils version <num>\" in a comment" . + " above them.\n" . $herecurr);
checkpatch uses only a single line output only before $herecurr Output line length doesn't matter.
Joe Perches
maintainerQuoted 8 lines · Borislav Petkov
Quoted 2 lines · Joe Perches
Given the location, this only works on .c and .h files. It does not work on .S files. Should it?
Probably not because there will be too many false positives. .byte is used not only to spell instruction opcodes in .S files. And the main case we're addressing here is using those .byte spelled opcodes in asm volatile constructs so...
[]
Quoted 7 lines
Quoted 4 lines
So it looks like the regex would be more complete as:
if ($realfile =~ m@^arch/x86/@ && $rawline =~ /\.byte\s+(?:$Constant|(?:\\)?$Ident|"\s*$Ident)\b/) {This is much less readable than what I have now (yes, realfile test should come first):
I care less about readability than completeness, correctness, and minimal false positives.
Quoted 9 lines
if ($realfile =~ m@^arch/x86/@ && $rawline =~ /\.byte[\s0-9a-fx,]+/) {Also, this
$rawline =~ /\.byte\s+(?:$Constant|(?:\\)?$Ident|"\s*$Ident)\b/) {matches
".byte 0x66"
And? So does the regex you propose.
Quoted 9 lines
This
$rawline =~ /\.byte[\s0-9a-fx,]+)) {matches
".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"which is what it needs to match.
Is that the only case you are trying to match?
two or more "0x<hex>," ?
Then this could use:
/"\s*\.byte\s+(?:0x[0-9a-fA-F]{1,2}\s*,\s*){2,4}/
$ git grep -P '"\s*\.byte\s+(?:0x[0-9a-fA-F]{1,2}\s*,\s*){2,4}' arch/x86
arch/x86/include/asm/intel_pconfig.h:#define PCONFIG ".byte 0x0f, 0x01, 0xc5"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc8;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfa;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc9;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfb;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
arch/x86/include/asm/segment.h: ".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
arch/x86/include/asm/smap.h:#define __ASM_CLAC ".byte 0x0f,0x01,0xca"
arch/x86/include/asm/smap.h:#define __ASM_STAC ".byte 0x0f,0x01,0xcb"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xee\n\t"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xef\n\t"
arch/x86/include/asm/special_insns.h: ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");vs:
$ git grep -P '\.byte[\s0-9a-fx,]+' -- 'arch/x86/*.[ch]' | cat
arch/x86/include/asm/alternative-asm.h: .byte \orig_len
arch/x86/include/asm/alternative-asm.h: .byte \alt_len
arch/x86/include/asm/alternative-asm.h: .byte \pad_len
arch/x86/include/asm/alternative.h: " .byte " alt_total_slen "\n" /* source len */ \
arch/x86/include/asm/alternative.h: " .byte " alt_rlen(num) "\n" /* replacement len */ \
arch/x86/include/asm/alternative.h: " .byte " alt_pad_len "\n" /* pad len */
arch/x86/include/asm/bug.h:#define ASM_UD0 ".byte 0x0f, 0xff" /* + ModRM (for Intel) */
arch/x86/include/asm/bug.h:#define ASM_UD1 ".byte 0x0f, 0xb9" /* + ModRM */
arch/x86/include/asm/bug.h:#define ASM_UD2 ".byte 0x0f, 0x0b"
arch/x86/include/asm/cpufeature.h: " .byte 3b - 1b\n" /* src len */
arch/x86/include/asm/cpufeature.h: " .byte 5f - 4f\n" /* repl len */
arch/x86/include/asm/cpufeature.h: " .byte 3b - 2b\n" /* pad len */
arch/x86/include/asm/cpufeature.h: " .byte 3b - 1b\n" /* src len */
arch/x86/include/asm/cpufeature.h: " .byte 0\n" /* repl len */
arch/x86/include/asm/cpufeature.h: " .byte 0\n" /* pad len */
arch/x86/include/asm/fpu/internal.h:#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
arch/x86/include/asm/fpu/internal.h:#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
arch/x86/include/asm/fpu/internal.h:#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
arch/x86/include/asm/fpu/internal.h:#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
arch/x86/include/asm/fpu/internal.h:#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
arch/x86/include/asm/idtentry.h: * Note, that the 'pushq imm8' is emitted via '.byte 0x6a, vector' because
arch/x86/include/asm/idtentry.h: * .byte achieves the same thing and the only fixup needed in the C entry
arch/x86/include/asm/idtentry.h: .byte 0x6a, vector
arch/x86/include/asm/idtentry.h: .byte 0x6a, vector
arch/x86/include/asm/inst.h: * Generate .byte code for some instructions not supported by old
arch/x86/include/asm/inst.h: .byte 0x40 | ((\opd1 & 8) >> 3) | ((\opd2 & 8) >> 1) | (\W << 3)
arch/x86/include/asm/inst.h: .byte \mod | (\opd1 & 7) | ((\opd2 & 7) << 3)
arch/x86/include/asm/inst.h: .byte 0xf3
arch/x86/include/asm/inst.h: .byte 0x0f, 0xc7
arch/x86/include/asm/intel_pconfig.h:#define PCONFIG ".byte 0x0f, 0x01, 0xc5"
arch/x86/include/asm/jump_label.h: ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t"
arch/x86/include/asm/jump_label.h: ".byte 0xe9\n\t .long %l[l_yes] - 2f\n\t"
arch/x86/include/asm/jump_label.h: .byte 0xe9
arch/x86/include/asm/jump_label.h: .byte STATIC_KEY_INIT_NOP
arch/x86/include/asm/jump_label.h: .byte STATIC_KEY_INIT_NOP
arch/x86/include/asm/jump_label.h: .byte 0xe9
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc8;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfa;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc9;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfb;"
arch/x86/include/asm/mwait.h: asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
arch/x86/include/asm/nops.h:#define _ASM_MK_NOP(x) .byte x
arch/x86/include/asm/nops.h:#define _ASM_MK_NOP(x) ".byte " __stringify(x) "\n"
arch/x86/include/asm/paravirt.h: .byte ptype; \
arch/x86/include/asm/paravirt.h: .byte 772b-771b; \
arch/x86/include/asm/paravirt_types.h: " .byte " type "\n" \
arch/x86/include/asm/paravirt_types.h: " .byte 772b-771b\n" \
arch/x86/include/asm/segment.h: ".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
arch/x86/include/asm/smap.h:#define __ASM_CLAC ".byte 0x0f,0x01,0xca"
arch/x86/include/asm/smap.h:#define __ASM_STAC ".byte 0x0f,0x01,0xcb"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xee\n\t"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xef\n\t"
arch/x86/include/asm/special_insns.h: alternative_io(".byte " __stringify(NOP_DS_PREFIX) "; clflush %P0",
arch/x86/include/asm/special_insns.h: ".byte 0x66; clflush %P0",
arch/x86/include/asm/special_insns.h: ".byte " __stringify(NOP_DS_PREFIX) "; clflush (%[pax])",
arch/x86/include/asm/special_insns.h: ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
arch/x86/include/asm/special_insns.h: ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
arch/x86/include/asm/static_call.h: __ARCH_DEFINE_STATIC_CALL_TRAMP(name, ".byte 0xe9; .long " #func " - (. + 4)")
arch/x86/include/asm/xen/interface.h:#define XEN_EMULATE_PREFIX __ASM_FORM(.byte __XEN_EMULATE_PREFIX ;)
arch/x86/kvm/vmx/ops.h: ".byte 0x3e\n\t" /* branch taken hint */
arch/x86/kvm/vmx/ops.h: ".byte 0x2e\n\t" /* branch not taken hint */ \
arch/x86/kvm/vmx/ops.h: ".byte 0x2e\n\t" /* branch not taken hint */ \
arch/x86/realmode/rm/realmode.h:#define LJMPW_RM(to) .byte 0xea ; .word (to), real_mode_segand if lines like:
arch/x86/include/asm/inst.h: * Generate .byte code for some instructions not supported by oldarch/x86/include/asm/paravirt_types.h: " .byte " type "\n" \
are false positives then perhaps change the regex to add required spaces after the \.byte before the match.
Joe Perches
maintainerQuoted 10 lines · Borislav Petkov
Quoted 3 lines · Joe Perches
Then this could use:
/"\s*\.byte\s+(?:0x[0-9a-fA-F]{1,2}\s*,\s*){2,4}/
Yes, this is getting close.
I've tweaked it a bit to:
'/\s*\.byte\s+(?:0x[0-9a-f]{1,2}[\s,]*){2,}/i'
^^^ ^
now useless without the " matches .BYTE
you probably want (?i:0x[etc...]
I'd prefer to add an upper bound to the {m,n} use. Unbounded multiple
matches {m,} can cause perl aborts.
This regex would also match
.byte 0x020x02
(which admittedly wouldn't compile, but I've seen really
bad patches submitted too)
Quoted 3 lines
which assumes at least 2 opcode bytes; upper limit can be more than 4. It still has some false positives in crypto but I'd say that's good enough. I'll play more with it later
A readability convenience would be to add and use:
our $Hex_byte = qr{(?i)0x[0-9a-f]{1,2}\b};
So if the minimum length if the isns .byte block is 2, with a separating comma then the regex could be:
/\.byte\s+$Hex_byte\s*,\s*$Hex_byte\b/
which I think is pretty readable.
$ git grep -P '\.byte\s+(?i:0x[0-9a-f]{1,2}\s*,\s*0x[0-9a-f]{1,2})\b' -- 'arch/x86/*.[ch]'
arch/x86/include/asm/bug.h:#define ASM_UD0 ".byte 0x0f, 0xff" /* + ModRM (for Intel) */
arch/x86/include/asm/bug.h:#define ASM_UD1 ".byte 0x0f, 0xb9" /* + ModRM */
arch/x86/include/asm/bug.h:#define ASM_UD2 ".byte 0x0f, 0x0b"
arch/x86/include/asm/inst.h: .byte 0x0f, 0xc7
arch/x86/include/asm/intel_pconfig.h:#define PCONFIG ".byte 0x0f, 0x01, 0xc5"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc8;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfa;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xc9;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x0f, 0x01, 0xfb;"
arch/x86/include/asm/mwait.h: asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
arch/x86/include/asm/mwait.h: asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
arch/x86/include/asm/segment.h: ".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
arch/x86/include/asm/smap.h:#define __ASM_CLAC ".byte 0x0f,0x01,0xca"
arch/x86/include/asm/smap.h:#define __ASM_STAC ".byte 0x0f,0x01,0xcb"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xee\n\t"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x0f,0x01,0xef\n\t"
arch/x86/include/asm/special_insns.h: ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
arch/x86/include/asm/special_insns.h: asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");Joe Perches
maintainerQuoted 2 lines · Borislav Petkov
Quoted 2 lines
checkpatch uses only a single line output only before $herecurr Output line length doesn't matter.
[]
Quoted 11 lines
WARNING: Please document which binutils version supports these .byte-spelled
insn opcodes by adding "binutils version <num>" in a comment above them.
#90: FILE:arch/x86/include/asm/special_insns.h:254:
+ asm volatile(".byte0x66,0x0f,0x38,0xf8,0x02"is easier readable than this:
WARNING: Please document which binutils version supports these .byte-spelledinsn opcodes by adding "binutils version <num>" in a comment above them. #90: FILE:
arch/x86/include/asm/special_insns.h:254:
+ asm volatile(".byte0x66,0x0f,0x38,0xf8,0x02"
Readability is a consideration but it still must be a single line.
using --terse requires single line error output
Perhaps:
if ($comment !~ /\bbinutils version [0-9.]+/ms) {
WARN("MISSING_BINUTILS_VERSION",
"Please add a comment for .byte-spelled insn opcodes with \"binutils version <minimum_required_version>\"\n" . $herecurr);PATCH 1/1 checkpatch: Check for .byte-spelled insn opcodes documentation on x86
In reply to Borislav Petkov--
From: Borislav Petkov <[email protected]>
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.
--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
scripts/checkpatch.pl +14 -0
@@ -6858,6 +6858,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. + if ($realfile =~ m@^arch/x86/@ && + $rawline =~ /\.byte[\s0-9a-fx,]+/) { + + my $comment = ctx_locate_comment($file ? 0 : $first_line, $linenr); + if ($comment !~ /binutils version [0-9.]+/ms) { + WARN("MISSING_BINUTILS_VERSION", + "Please document which binutils version supports these .byte-spelled\n" . + "\tinsn opcodes by adding \"binutils version <num>\" in a comment" . + " above them.\n" . $herecurr); + } + } } # If we have no input at all, then there is nothing to report on
Signature · 8 lines
2.21.0
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 2 lines · Joe Perches
Given the location, this only works on .c and .h files. It does not work on .S files. Should it?
Probably not because there will be too many false positives. .byte is used not only to spell instruction opcodes in .S files. And the main case we're addressing here is using those .byte spelled opcodes in asm volatile constructs so...
Quoted 1 line
No need for a capture group.
Debugging leftover, gone now.
Quoted 2 lines
Please use @ not " as all the other $realfile comparisons use that form when expecting a /
Done.
Quoted 4 lines
So it looks like the regex would be more complete as:
if ($realfile =~ m@^arch/x86/@ && $rawline =~ /\.byte\s+(?:$Constant|(?:\\)?$Ident|"\s*$Ident)\b/) {
This is much less readable than what I have now (yes, realfile test should come first):
if ($realfile =~ m@^arch/x86/@ && $rawline =~ /\.byte[\s0-9a-fx,]+/) {Also, this
$rawline =~ /\.byte\s+(?:$Constant|(?:\\)?$Ident|"\s*$Ident)\b/) {matches
".byte 0x66"
This
$rawline =~ /\.byte[\s0-9a-fx,]+)) {matches
".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
which is what it needs to match.
Quoted 5 lines
A patch can modify any number of files.
This should use
ctx_locate_comment($file ? 0 : $first_line, $linenr)as checkpatch tests work on patch contexts not the entire file before this line.
Done.
Quoted 1 line
No need for the $!comment test
Done.
Quoted 2 lines
checkpatch uses only a single line output only before $herecurr Output line length doesn't matter.
Well, this:
WARNING: Please document which binutils version supports these .byte-spelled
insn opcodes by adding "binutils version <num>" in a comment above them.
#90: FILE: arch/x86/include/asm/special_insns.h:254:
+ asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
is easier readable than this:
WARNING: Please document which binutils version supports these .byte-spelledinsn opcodes by adding "binutils version <num>" in a comment above them. #90: FILE: arch/x86/include/asm/special_insns.h:254:
+ asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 3 lines · Joe Perches
Then this could use:
/"\s*\.byte\s+(?:0x[0-9a-fA-F]{1,2}\s*,\s*){2,4}/
Yes, this is getting close.
I've tweaked it a bit to:
'/\s*\.byte\s+(?:0x[0-9a-f]{1,2}[\s,]*){2,}/i'
which assumes at least 2 opcode bytes; upper limit can be more than 4. It still has some false positives in crypto but I'd say that's good enough. I'll play more with it later.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 3 lines · Joe Perches
Quoted 1 line
'/\s*\.byte\s+(?:0x[0-9a-f]{1,2}[\s,]*){2,}/i'
^^^ ^
now useless without the "
There are \.byte specifications without " so not useless.
Quoted 1 line
matches .BYTE
so what. It would have failed before, when trying to compile it.
Quoted 5 lines
you probably want (?i:0x[etc...]
I'd prefer to add an upper bound to the {m,n} use. Unbounded multiple
matches {m,} can cause perl aborts.
Ok, we can make that 15. Max insn length on x86 is 15 bytes and that is unrealistically high for this use case so we should be good. And the range must be {1,15} because you can have single-byte instructions.
And that's fine if there are *some* false positives. And whatever we do, it won't match everything. For example:
arch/x86/include/asm/fpu/internal.h:208:#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27" arch/x86/include/asm/fpu/internal.h:209:#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37" arch/x86/include/asm/fpu/internal.h:210:#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f" arch/x86/include/asm/fpu/internal.h:211:#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f" arch/x86/include/asm/fpu/internal.h:212:#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
but that's fine. I prefer for the regex to remain readable and single outliers like those are caught in manual review.
As another example, sometimes it would be a false positive for another reason:
arch/x86/include/asm/idtentry.h:500: * Note, that the 'pushq imm8' is emitted via '.byte 0x6a, vector' because
that's why I've changed the text to say "Please consider..." implying thatdocumenting binutils version might not always be necessary/needed.
All in all, it's fine if there are some false positives and it can make reviewers have a second look.
Quoted 6 lines
This regex would also match
.byte 0x020x02
(which admittedly wouldn't compile, but I've seen really bad patches submitted too)
That's fine - I love reviewing !compiled patches. They will never send !compiled again.
Quoted 10 lines
A readability convenience would be to add and use:
our $Hex_byte = qr{(?i)0x[0-9a-f]{1,2}\b};
So if the minimum length if the isns .byte block is 2, with a separating comma then the regex could be:
/\.byte\s+$Hex_byte\s*,\s*$Hex_byte\b/
which I think is pretty readable.
Yap, makes sense. v3 coming up...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 1/1 checkpatch: Check for .byte-spelled insn opcodes documentation on x86
From: Borislav Petkov <[email protected]>
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.
--- scripts/checkpatch.pl | 13 +++++++++++++ 1 file changed, 13 insertions(+)
scripts/checkpatch.pl +13 -0
@@ -408,6 +408,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}}; our $Int = qr{[0-9]+$Int_type?}; our $Octal = qr{0[0-7]+$Int_type?}; our $String = qr{"[X\t]*"};
@@ -6858,6 +6859,18 @@ 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 ($realfile =~ m@^arch/x86/@ && + $rawline =~ /\s*\.byte\s+(?:$Hex_byte[,\s]*){1,15}/) { + + 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); + } + } } # If we have no input at all, then there is nothing to report on
Signature · 7 lines
2.21.0
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJoe Perches
maintainerQuoted 6 lines · Borislav Petkov
Quoted 3 lines · Joe Perches
Quoted 1 line
'/\s*\.byte\s+(?:0x[0-9a-f]{1,2}[\s,]*){2,}/i'
^^^ ^
now useless without the "There are \.byte specifications without " so not useless.
Matching optional leading spaces is useless.
Joe Perches
maintainerQuoted 1 line · Borislav Petkov
From: Borislav Petkov <[email protected]>
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl our $Lval = qr{$Ident(?:$Member)*}; · line 411
Quoted 5 lines
scripts/checkpatch.pl +1
-0@@ -408,6 +408,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}};
$Hex_byte needs to be generic and this needs to
have a trailing \b otherwise it would match0x12 from 0x1234 and leave 34
Joe Perches
maintainerPATCH 1/1 checkpatch: Check for .byte-spelled insn opcodes documentation on x86
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
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.
---
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 +18 -0
@@ -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
Quoted 13 lines · Joe Perches
From: Borislav Petkov <[email protected]>
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 <[email protected]>Signed-off-by Borislav Petkov <[email protected]>Signed-off-by Joe Perches <[email protected]>
Quoted 7 lines
---
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.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJoe Perches
maintainerQuoted 22 lines · Borislav Petkov
Quoted 13 lines · Joe Perches
From: Borislav Petkov <[email protected]>
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 <[email protected]>Signed-off-by Borislav Petkov <[email protected]>Signed-off-by Joe Perches <[email protected]>Quoted 7 lines
---
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(+)
Quoted 6 lines
./scripts/checkpatch.pl/tmp/test
Global symbol "$rawline" requires explicit package name (did you forget to declare "my $rawline"?) at./scripts/checkpatch.plline 6943. Global symbol "$herecurr" requires explicit package name (did you forget to declare "my $herecurr"?) at./scripts/checkpatch.plline 6948. Execution of./scripts/checkpatch.plaborted 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.
Quoted 1 line · Joe Perches
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.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJoe Perches
maintainerQuoted 11 lines · Borislav Petkov
Quoted 1 line · Joe Perches
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.
Quoted 1 line · Joe Perches
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.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJoe Perches
maintainerQuoted 6 lines · Borislav Petkov
Quoted 1 line · Joe Perches
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.
Quoted 7 lines
- 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...
---
Quoted 5 lines · Joe Perches
Quoted 3 lines · Borislav Petkov
Quoted 1 line · Joe Perches
Workie here. This is against -next.
Nevermind - I had an old version in that branch.
btw: adding "my mistake" would have been more appreciated...