Skip to content

Commit 290eb13

Browse files
jinghao-jiamhiramat
authored andcommitted
x86/kprobes: Boost more instructions from grp2/3/4/5
With the instruction decoder, we are now able to decode and recognize instructions with opcode extensions. There are more instructions in these groups that can be boosted: Group 2: ROL, ROR, RCL, RCR, SHL/SAL, SHR, SAR Group 3: TEST, NOT, NEG, MUL, IMUL, DIV, IDIV Group 4: INC, DEC (byte operation) Group 5: INC, DEC (word/doubleword/quadword operation) These instructions are not boosted previously because there are reserved opcodes within the groups, e.g., group 2 with ModR/M.nnn == 110 is unmapped. As a result, kprobes attached to them requires two int3 traps as being non-boostable also prevents jump-optimization. Some simple tests on QEMU show that after boosting and jump-optimization a single kprobe on these instructions with an empty pre-handler runs 10x faster (~1000 cycles vs. ~100 cycles). Since these instructions are mostly ALU operations and do not touch special registers like RIP, let's boost them so that we get the performance benefit. Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Jinghao Jia <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent e884edb commit 290eb13

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

arch/x86/kernel/kprobes/core.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,33 @@ bool can_boost(struct insn *insn, void *addr)
169169
case 0x62: /* bound */
170170
case 0x70 ... 0x7f: /* Conditional jumps */
171171
case 0x9a: /* Call far */
172-
case 0xc0 ... 0xc1: /* Grp2 */
173172
case 0xcc ... 0xce: /* software exceptions */
174-
case 0xd0 ... 0xd3: /* Grp2 */
175173
case 0xd6: /* (UD) */
176174
case 0xd8 ... 0xdf: /* ESC */
177175
case 0xe0 ... 0xe3: /* LOOP*, JCXZ */
178176
case 0xe8 ... 0xe9: /* near Call, JMP */
179177
case 0xeb: /* Short JMP */
180178
case 0xf0 ... 0xf4: /* LOCK/REP, HLT */
181-
case 0xf6 ... 0xf7: /* Grp3 */
182-
case 0xfe: /* Grp4 */
183179
/* ... are not boostable */
184180
return false;
181+
case 0xc0 ... 0xc1: /* Grp2 */
182+
case 0xd0 ... 0xd3: /* Grp2 */
183+
/*
184+
* AMD uses nnn == 110 as SHL/SAL, but Intel makes it reserved.
185+
*/
186+
return X86_MODRM_REG(insn->modrm.bytes[0]) != 0b110;
187+
case 0xf6 ... 0xf7: /* Grp3 */
188+
/* AMD uses nnn == 001 as TEST, but Intel makes it reserved. */
189+
return X86_MODRM_REG(insn->modrm.bytes[0]) != 0b001;
190+
case 0xfe: /* Grp4 */
191+
/* Only INC and DEC are boostable */
192+
return X86_MODRM_REG(insn->modrm.bytes[0]) == 0b000 ||
193+
X86_MODRM_REG(insn->modrm.bytes[0]) == 0b001;
185194
case 0xff: /* Grp5 */
186-
/* Only indirect jmp is boostable */
187-
return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
195+
/* Only INC, DEC, and indirect JMP are boostable */
196+
return X86_MODRM_REG(insn->modrm.bytes[0]) == 0b000 ||
197+
X86_MODRM_REG(insn->modrm.bytes[0]) == 0b001 ||
198+
X86_MODRM_REG(insn->modrm.bytes[0]) == 0b100;
188199
default:
189200
return true;
190201
}

0 commit comments

Comments
 (0)