Skip to contentSkip to conversation

kretprobe: kretprobe-booster against 2.6.16-rc1 for i386

PATCH Chuck Ebbert 2 participants 2 messages Feb 7, 2006 – Feb 8, 2006

lore
Share & tools
lore
#0 lore raw

In-Reply-To: <[email protected]>

Quoted 8 lines · Masami Hiramatsu

Here is a patch of the kretprobe-booster for i386 arch against linux-2.6.16-rc1 and also appliable against 2.6.16-rc1-mm4.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +0 -0
@@ -255,17 +255,45 @@ no_kprobe:
  * here. When a retprobed function returns, this probe is hit and
  * trampoline_probe_handler() runs, calling the kretprobe's handler.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 262

Quoted 10 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +3 -4
  */
- void kretprobe_trampoline_holder(void)
+ void __kprobes kretprobe_trampoline_holder(void)
  {
-     asm volatile (  ".global kretprobe_trampoline\n"
+      asm volatile ( ".global kretprobe_trampoline\n"
                      "kretprobe_trampoline: \n"
-                     "nop\n");
- }
+                     "       subl $8, %esp\n"

There is no need to reserve these 8 bytes; they wouldn't be
there if INT3 were done from kernel space anyway.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 273

Quoted 11 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +11 -0
+                     "       pushf\n"
+                     "       subl $20, %esp\n"
+                     "       pushl %eax\n"
+                     "       pushl %ebp\n"
+                     "       pushl %edi\n"
+                     "       pushl %esi\n"
+                     "       pushl %edx\n"
+                     "       pushl %ecx\n"
+                     "       pushl %ebx\n"
+                     "       movl %esp, %eax\n"
+                     "       pushl %eax\n"

If you make trampoline_probe_handler "fastcall" you can just
pass eax to the handler directly.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 275

Quoted 2 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -0
+                     "       addl $60, %eax\n"
+                     "       movl %eax, 56(%esp)\n"

No need for this either, since oldesp isn't there on INT3 call anyway.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 277

Quoted 2 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -0
+                     "       movl $trampoline_handler, %eax\n"
+                     "       call *%eax\n"

Why not just "call trampoline_handler"?

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 289

Quoted 12 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +12 -0
+                     "       addl $4, %esp\n"
+                     "       movl %eax, 56(%esp)\n"
+                     "       popl %ebx\n"
+                     "       popl %ecx\n"
+                     "       popl %edx\n"
+                     "       popl %esi\n"
+                     "       popl %edi\n"
+                     "       popl %ebp\n"
+                     "       popl %eax\n"
+                     "       addl $20, %esp\n"
+                     "       popf\n"
+                     "       addl $4, %esp\n"

This "add" corrupts the flags you just popped from the stack.
This can be fixed by moving the return address up 4 bytes on the stack and doing "ret 4" or by putting the address in regs->eip and just doing an "iret" to return to the caller.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 291

Quoted 2 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -0
+                     "       ret\n");
+}

Patch for 2.6.16-rc1-mm5 follows.

Fix and clean up kretprobe-kretprobe-booster by:

- Not reserving 8 bytes at stack bottom
- Making trampoline_handler fastcall and calling it directly
- Using "iret" to return to original caller

Using "iret" is slightly slower than direct return; time went from 1030 CPU cycles overhead per kretprobe to 1050 cycles (2%), but the code is cleaner.

---

 arch/i386/kernel/kprobes.c |   24 ++++++++++--------------
 1 files changed, 10 insertions(+), 14 deletions(-)
arch/i386/kernel/kprobes.c +10 -14
@@ -325,9 +325,10 @@ no_kprobe:
  {
 	 asm volatile ( ".global kretprobe_trampoline\n"
  			"kretprobe_trampoline: \n"
-			"	subl $8, %esp\n"
 			"	pushf\n"
-			"	subl $20, %esp\n"
+			"	pushl %cs\n"
+			/* skip eip, orig_eax, es, ds */
+			"	subl $16, %esp\n"
 			"	pushl %eax\n"
 			"	pushl %ebp\n"
 			"	pushl %edi\n"
@@ -336,13 +337,9 @@ no_kprobe:
 			"	pushl %ecx\n"
 			"	pushl %ebx\n"
 			"	movl %esp, %eax\n"
-			"	pushl %eax\n"
-			"	addl $60, %eax\n"
-			"	movl %eax, 56(%esp)\n"
-			"	movl $trampoline_handler, %eax\n"
-			"	call *%eax\n"
-			"	addl $4, %esp\n"
-			"	movl %eax, 56(%esp)\n"
+			"	call trampoline_handler\n"
+			/* save new eip */
+			"	movl %eax, 40(%esp)\n"
 			"	popl %ebx\n"
 			"	popl %ecx\n"
 			"	popl %edx\n"
@@ -350,16 +347,15 @@ no_kprobe:
 			"	popl %edi\n"
 			"	popl %ebp\n"
 			"	popl %eax\n"
-			"	addl $20, %esp\n"
-			"	popf\n"
-			"	addl $4, %esp\n"
-			"	ret\n");
+			/* skip ds, es, orig_eax */
+			"	addl $12, %esp\n"
+			"	iret\n");
 }
 
 /*
  * Called from kretprobe_trampoline
  */
-asmlinkage void *__kprobes trampoline_handler(struct pt_regs *regs)
+fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
 {
         struct kretprobe_instance *ri = NULL;
         struct hlist_head *head;
Signature · 1 line
Chuck
#1 lore raw

Hi, Chuck

Quoted 17 lines · Chuck Ebbert

In-Reply-To: <[email protected]>

Quoted 13 lines · Masami Hiramatsu

Here is a patch of the kretprobe-booster for i386 arch against linux-2.6.16-rc1 and also appliable against 2.6.16-rc1-mm4.

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +1 -2
@@ -255,17 +255,45 @@ no_kprobe:
  * here. When a retprobed function returns, this probe is hit and
  * trampoline_probe_handler() runs, calling the kretprobe's handler.
  */
- void kretprobe_trampoline_holder(void)
+ void __kprobes kretprobe_trampoline_holder(void)
  {
-     asm volatile (  ".global kretprobe_trampoline\n"
Quoted 8 lines

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 262

Quoted 5 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -2
+      asm volatile ( ".global kretprobe_trampoline\n"
                      "kretprobe_trampoline: \n"
-                     "nop\n");
- }
+                     "       subl $8, %esp\n"

There is no need to reserve these 8 bytes; they wouldn't be
there if INT3 were done from kernel space anyway.

OK, I agree.

Quoted 12 lines

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 273

Quoted 11 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +11 -0
+                     "       pushf\n"
+                     "       subl $20, %esp\n"
+                     "       pushl %eax\n"
+                     "       pushl %ebp\n"
+                     "       pushl %edi\n"
+                     "       pushl %esi\n"
+                     "       pushl %edx\n"
+                     "       pushl %ecx\n"
+                     "       pushl %ebx\n"
+                     "       movl %esp, %eax\n"
+                     "       pushl %eax\n"
Quoted 2 lines

If you make trampoline_probe_handler "fastcall" you can just
pass eax to the handler directly.

It is a nice idea.

Quoted 4 lines

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 275

Quoted 2 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -0
+                     "       addl $60, %eax\n"
+                     "       movl %eax, 56(%esp)\n"

No need for this either, since oldesp isn't there on INT3 call anyway.

OK, I agree too.

Quoted 4 lines

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 277

Quoted 2 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +2 -0
+                     "       movl $trampoline_handler, %eax\n"
+                     "       call *%eax\n"

Why not just "call trampoline_handler"?

I forgot to do so... thanks!

Quoted 13 lines

arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 no_kprobe: · line 289

Quoted 12 lines
arch/i386/kernel/kprobes.c 2006-01-30 18:40:12.000000000 +0900 renamed from arch/i386/kernel/kprobes.c 2006-01-24 19:07:26.000000000 +0900 +12 -0
+                     "       addl $4, %esp\n"
+                     "       movl %eax, 56(%esp)\n"
+                     "       popl %ebx\n"
+                     "       popl %ecx\n"
+                     "       popl %edx\n"
+                     "       popl %esi\n"
+                     "       popl %edi\n"
+                     "       popl %ebp\n"
+                     "       popl %eax\n"
+                     "       addl $20, %esp\n"
+                     "       popf\n"
+                     "       addl $4, %esp\n"
Quoted 1 line

This "add" corrupts the flags you just popped from the stack.

Sure, this "add" may modify the status flags. It should be fixed.

Quoted 3 lines

This can be fixed by moving the return address up 4 bytes on the stack and doing "ret 4" or by putting the address in regs->eip and just doing an "iret" to return to the caller.

Why would you like to use "iret"?
I worry about its negative side effects, because the "iret" is only for interrupt handlers and may have some side effects.
In my honest opinion, this can be fixed by moving eflags to cs field, putting the return address in eflags and using "ret".
I developed a patch and attach it to this mail.

This patch fixes and cleanups kretprobe-kretprobe-booster by:

- Not reserving 8 bytes at stack bottom
- Making trampoline_handler fastcall and calling it directly
- Not changing the status flags

I'm very happy to be fixed it! Thank you very much for pointing it out!

Signature · 62 lines
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]

Signed-off-by: Masami Hiramatsu <[email protected]>

 kprobes.c |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)
diff -Narup a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c
--- a/arch/i386/kernel/kprobes.c	2006-02-07 11:51:16.000000000 +0900
+++ b/arch/i386/kernel/kprobes.c	2006-02-08 16:05:35.000000000 +0900
@@ -325,8 +325,8 @@ no_kprobe:
  {
 	 asm volatile ( ".global kretprobe_trampoline\n"
  			"kretprobe_trampoline: \n"
-			"	subl $8, %esp\n"
 			"	pushf\n"
+			/* skip cs, eip, orig_eax, es, ds */
 			"	subl $20, %esp\n"
 			"	pushl %eax\n"
 			"	pushl %ebp\n"
@@ -336,13 +336,12 @@ no_kprobe:
 			"	pushl %ecx\n"
 			"	pushl %ebx\n"
 			"	movl %esp, %eax\n"
-			"	pushl %eax\n"
-			"	addl $60, %eax\n"
-			"	movl %eax, 56(%esp)\n"
-			"	movl $trampoline_handler, %eax\n"
-			"	call *%eax\n"
-			"	addl $4, %esp\n"
-			"	movl %eax, 56(%esp)\n"
+			"	call trampoline_handler\n"
+			/* move eflags to cs */
+			"	movl 48(%esp), %edx\n"
+			"	movl %edx, 44(%esp)\n"
+			/* save true return address on eflags */
+			"	movl %eax, 48(%esp)\n"
 			"	popl %ebx\n"
 			"	popl %ecx\n"
 			"	popl %edx\n"
@@ -350,16 +349,16 @@ no_kprobe:
 			"	popl %edi\n"
 			"	popl %ebp\n"
 			"	popl %eax\n"
-			"	addl $20, %esp\n"
+			/* skip eip, orig_eax, es, ds */
+			"	addl $16, %esp\n"
 			"	popf\n"
-			"	addl $4, %esp\n"
 			"	ret\n");
 }

 /*
  * Called from kretprobe_trampoline
  */
-asmlinkage void *__kprobes trampoline_handler(struct pt_regs *regs)
+fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
 {
         struct kretprobe_instance *ri = NULL;
         struct hlist_head *head;