Skip to content

Commit a8d11cd

Browse files
mhiramatingomolnar
authored andcommitted
kprobes/x86: Consolidate insn decoder users for copying code
Consolidate x86 instruction decoder users on the path of copying original code for kprobes. Kprobes decodes the same instruction a maximum of 3 times when preparing the instruction buffer: - The first time for getting the length of the instruction, - the 2nd for adjusting displacement, - and the 3rd for checking whether the instruction is boostable or not. For each time, the actual decoding target address is slightly different (1st is original address or recovered instruction buffer, 2nd and 3rd are pointing to the copied buffer), but all have the same instruction. Thus, this patch also changes the target address to the copied buffer at first and reuses the decoded "insn" for displacement adjusting and checking boostability. Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: David S . Miller <davem@davemloft.net> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ye Xiaolong <xiaolong.ye@intel.com> Link: http://lkml.kernel.org/r/149076389643.22469.13151892839998777373.stgit@devbox Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ea1e34f commit a8d11cd

3 files changed

Lines changed: 36 additions & 39 deletions

File tree

‎arch/x86/kernel/kprobes/common.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@
6767
#endif
6868

6969
/* Ensure if the instruction can be boostable */
70-
extern int can_boost(kprobe_opcode_t *instruction, void *addr);
70+
extern int can_boost(struct insn *insn, void *orig_addr);
7171
/* Recover instruction if given address is probed */
7272
extern unsigned long recover_probed_instruction(kprobe_opcode_t *buf,
7373
unsigned long addr);
7474
/*
7575
* Copy an instruction and adjust the displacement if the instruction
7676
* uses the %rip-relative addressing mode.
7777
*/
78-
extern int __copy_instruction(u8 *dest, u8 *src);
78+
extern int __copy_instruction(u8 *dest, u8 *src, struct insn *insn);
7979

8080
/* Generate a relative-jump/call instruction */
8181
extern void synthesize_reljump(void *from, void *to);

‎arch/x86/kernel/kprobes/core.c‎

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,33 +164,29 @@ static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
164164
NOKPROBE_SYMBOL(skip_prefixes);
165165

166166
/*
167-
* Returns non-zero if opcode is boostable.
167+
* Returns non-zero if INSN is boostable.
168168
* RIP relative instructions are adjusted at copying time in 64 bits mode
169169
*/
170-
int can_boost(kprobe_opcode_t *opcodes, void *addr)
170+
int can_boost(struct insn *insn, void *addr)
171171
{
172-
struct insn insn;
173172
kprobe_opcode_t opcode;
174173

175174
if (search_exception_tables((unsigned long)addr))
176175
return 0; /* Page fault may occur on this address. */
177176

178-
kernel_insn_init(&insn, (void *)opcodes, MAX_INSN_SIZE);
179-
insn_get_opcode(&insn);
180-
181177
/* 2nd-byte opcode */
182-
if (insn.opcode.nbytes == 2)
183-
return test_bit(insn.opcode.bytes[1],
178+
if (insn->opcode.nbytes == 2)
179+
return test_bit(insn->opcode.bytes[1],
184180
(unsigned long *)twobyte_is_boostable);
185181

186-
if (insn.opcode.nbytes != 1)
182+
if (insn->opcode.nbytes != 1)
187183
return 0;
188184

189185
/* Can't boost Address-size override prefix */
190-
if (unlikely(inat_is_address_size_prefix(insn.attr)))
186+
if (unlikely(inat_is_address_size_prefix(insn->attr)))
191187
return 0;
192188

193-
opcode = insn.opcode.bytes[0];
189+
opcode = insn->opcode.bytes[0];
194190

195191
switch (opcode & 0xf0) {
196192
case 0x60:
@@ -351,35 +347,31 @@ static int is_IF_modifier(kprobe_opcode_t *insn)
351347
* addressing mode.
352348
* This returns the length of copied instruction, or 0 if it has an error.
353349
*/
354-
int __copy_instruction(u8 *dest, u8 *src)
350+
int __copy_instruction(u8 *dest, u8 *src, struct insn *insn)
355351
{
356-
struct insn insn;
357352
kprobe_opcode_t buf[MAX_INSN_SIZE];
358-
int length;
359353
unsigned long recovered_insn =
360354
recover_probed_instruction(buf, (unsigned long)src);
361355

362-
if (!recovered_insn)
356+
if (!recovered_insn || !insn)
363357
return 0;
364-
kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
365-
insn_get_length(&insn);
366-
length = insn.length;
367358

368-
/* Another subsystem puts a breakpoint, failed to recover */
369-
if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
359+
/* This can access kernel text if given address is not recovered */
360+
if (probe_kernel_read(dest, (void *)recovered_insn, MAX_INSN_SIZE))
370361
return 0;
371362

372-
/* This can access kernel text if given address is not recovered */
373-
if (kernel_probe_read(dest, insn.kaddr, length))
363+
kernel_insn_init(insn, dest, MAX_INSN_SIZE);
364+
insn_get_length(insn);
365+
366+
/* Another subsystem puts a breakpoint, failed to recover */
367+
if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
374368
return 0;
375369

376370
#ifdef CONFIG_X86_64
377371
/* Only x86_64 has RIP relative instructions */
378-
if (insn_rip_relative(&insn)) {
372+
if (insn_rip_relative(insn)) {
379373
s64 newdisp;
380374
u8 *disp;
381-
kernel_insn_init(&insn, dest, length);
382-
insn_get_displacement(&insn);
383375
/*
384376
* The copied instruction uses the %rip-relative addressing
385377
* mode. Adjust the displacement for the difference between
@@ -392,29 +384,32 @@ int __copy_instruction(u8 *dest, u8 *src)
392384
* extension of the original signed 32-bit displacement would
393385
* have given.
394386
*/
395-
newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
387+
newdisp = (u8 *) src + (s64) insn->displacement.value
388+
- (u8 *) dest;
396389
if ((s64) (s32) newdisp != newdisp) {
397390
pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
398-
pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value);
391+
pr_err("\tSrc: %p, Dest: %p, old disp: %x\n",
392+
src, dest, insn->displacement.value);
399393
return 0;
400394
}
401-
disp = (u8 *) dest + insn_offset_displacement(&insn);
395+
disp = (u8 *) dest + insn_offset_displacement(insn);
402396
*(s32 *) disp = (s32) newdisp;
403397
}
404398
#endif
405-
return length;
399+
return insn->length;
406400
}
407401

408402
/* Prepare reljump right after instruction to boost */
409-
static void prepare_boost(struct kprobe *p, int length)
403+
static void prepare_boost(struct kprobe *p, struct insn *insn)
410404
{
411-
if (can_boost(p->ainsn.insn, p->addr) &&
412-
MAX_INSN_SIZE - length >= RELATIVEJUMP_SIZE) {
405+
if (can_boost(insn, p->addr) &&
406+
MAX_INSN_SIZE - insn->length >= RELATIVEJUMP_SIZE) {
413407
/*
414408
* These instructions can be executed directly if it
415409
* jumps back to correct address.
416410
*/
417-
synthesize_reljump(p->ainsn.insn + length, p->addr + length);
411+
synthesize_reljump(p->ainsn.insn + insn->length,
412+
p->addr + insn->length);
418413
p->ainsn.boostable = true;
419414
} else {
420415
p->ainsn.boostable = false;
@@ -423,20 +418,21 @@ static void prepare_boost(struct kprobe *p, int length)
423418

424419
static int arch_copy_kprobe(struct kprobe *p)
425420
{
421+
struct insn insn;
426422
int len;
427423

428424
set_memory_rw((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
429425

430426
/* Copy an instruction with recovering if other optprobe modifies it.*/
431-
len = __copy_instruction(p->ainsn.insn, p->addr);
427+
len = __copy_instruction(p->ainsn.insn, p->addr, &insn);
432428
if (!len)
433429
return -EINVAL;
434430

435431
/*
436432
* __copy_instruction can modify the displacement of the instruction,
437433
* but it doesn't affect boostable check.
438434
*/
439-
prepare_boost(p, len);
435+
prepare_boost(p, &insn);
440436

441437
set_memory_ro((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
442438

‎arch/x86/kernel/kprobes/opt.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ NOKPROBE_SYMBOL(optimized_callback);
177177

178178
static int copy_optimized_instructions(u8 *dest, u8 *src)
179179
{
180+
struct insn insn;
180181
int len = 0, ret;
181182

182183
while (len < RELATIVEJUMP_SIZE) {
183-
ret = __copy_instruction(dest + len, src + len);
184-
if (!ret || !can_boost(dest + len, src + len))
184+
ret = __copy_instruction(dest + len, src + len, &insn);
185+
if (!ret || !can_boost(&insn, src + len))
185186
return -EINVAL;
186187
len += ret;
187188
}

0 commit comments

Comments
 (0)