diff options
author | Christoph Hellwig <hch@lst.de> | 2007-05-08 03:34:14 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-05-08 14:15:19 -0400 |
commit | 4c4308cb93450989846ac49faeb6dab943e7657e (patch) | |
tree | c06092cae6f95a243cdd758d07491cf5fa24a1dd | |
parent | 6f716acd5fa20ae6a35ab29ae37fa9189e839ed5 (diff) |
kprobes: kretprobes simplifications
- consolidate duplicate code in all arch_prepare_kretprobe instances
into common code
- replace various odd helpers that use hlist_for_each_entry to get
the first elemenet of a list with either a hlist_for_each_entry_save
or an opencoded access to the first element in the caller
- inline add_rp_inst into it's only remaining caller
- use kretprobe_inst_table_head instead of opencoding it
Signed-off-by: Christoph Hellwig <hch@lst.de>
Cc: Prasanna S Panchamukhi <prasanna@in.ibm.com>
Acked-by: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | arch/i386/kernel/kprobes.c | 17 | ||||
-rw-r--r-- | arch/ia64/kernel/kprobes.c | 18 | ||||
-rw-r--r-- | arch/powerpc/kernel/kprobes.c | 19 | ||||
-rw-r--r-- | arch/s390/kernel/kprobes.c | 18 | ||||
-rw-r--r-- | arch/x86_64/kernel/kprobes.c | 17 | ||||
-rw-r--r-- | include/linux/kprobes.h | 5 | ||||
-rw-r--r-- | kernel/kprobes.c | 64 |
7 files changed, 44 insertions, 114 deletions
diff --git a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c index 3fbef288c376..b6a9d64c2251 100644 --- a/arch/i386/kernel/kprobes.c +++ b/arch/i386/kernel/kprobes.c | |||
@@ -226,24 +226,15 @@ static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) | |||
226 | } | 226 | } |
227 | 227 | ||
228 | /* Called with kretprobe_lock held */ | 228 | /* Called with kretprobe_lock held */ |
229 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, | 229 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
230 | struct pt_regs *regs) | 230 | struct pt_regs *regs) |
231 | { | 231 | { |
232 | unsigned long *sara = (unsigned long *)®s->esp; | 232 | unsigned long *sara = (unsigned long *)®s->esp; |
233 | 233 | ||
234 | struct kretprobe_instance *ri; | 234 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
235 | 235 | ||
236 | if ((ri = get_free_rp_inst(rp)) != NULL) { | 236 | /* Replace the return addr with trampoline addr */ |
237 | ri->rp = rp; | 237 | *sara = (unsigned long) &kretprobe_trampoline; |
238 | ri->task = current; | ||
239 | ri->ret_addr = (kprobe_opcode_t *) *sara; | ||
240 | |||
241 | /* Replace the return addr with trampoline addr */ | ||
242 | *sara = (unsigned long) &kretprobe_trampoline; | ||
243 | add_rp_inst(ri); | ||
244 | } else { | ||
245 | rp->nmissed++; | ||
246 | } | ||
247 | } | 238 | } |
248 | 239 | ||
249 | /* | 240 | /* |
diff --git a/arch/ia64/kernel/kprobes.c b/arch/ia64/kernel/kprobes.c index 353689edebd5..0b72f0f94192 100644 --- a/arch/ia64/kernel/kprobes.c +++ b/arch/ia64/kernel/kprobes.c | |||
@@ -465,23 +465,13 @@ int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) | |||
465 | } | 465 | } |
466 | 466 | ||
467 | /* Called with kretprobe_lock held */ | 467 | /* Called with kretprobe_lock held */ |
468 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, | 468 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
469 | struct pt_regs *regs) | 469 | struct pt_regs *regs) |
470 | { | 470 | { |
471 | struct kretprobe_instance *ri; | 471 | ri->ret_addr = (kprobe_opcode_t *)regs->b0; |
472 | 472 | ||
473 | if ((ri = get_free_rp_inst(rp)) != NULL) { | 473 | /* Replace the return addr with trampoline addr */ |
474 | ri->rp = rp; | 474 | regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; |
475 | ri->task = current; | ||
476 | ri->ret_addr = (kprobe_opcode_t *)regs->b0; | ||
477 | |||
478 | /* Replace the return addr with trampoline addr */ | ||
479 | regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; | ||
480 | |||
481 | add_rp_inst(ri); | ||
482 | } else { | ||
483 | rp->nmissed++; | ||
484 | } | ||
485 | } | 475 | } |
486 | 476 | ||
487 | int __kprobes arch_prepare_kprobe(struct kprobe *p) | 477 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index 3d54ad7dd1f9..aed58e1cb91f 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c | |||
@@ -126,22 +126,13 @@ static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | |||
126 | } | 126 | } |
127 | 127 | ||
128 | /* Called with kretprobe_lock held */ | 128 | /* Called with kretprobe_lock held */ |
129 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, | 129 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
130 | struct pt_regs *regs) | 130 | struct pt_regs *regs) |
131 | { | 131 | { |
132 | struct kretprobe_instance *ri; | 132 | ri->ret_addr = (kprobe_opcode_t *)regs->link; |
133 | 133 | ||
134 | if ((ri = get_free_rp_inst(rp)) != NULL) { | 134 | /* Replace the return addr with trampoline addr */ |
135 | ri->rp = rp; | 135 | regs->link = (unsigned long)kretprobe_trampoline; |
136 | ri->task = current; | ||
137 | ri->ret_addr = (kprobe_opcode_t *)regs->link; | ||
138 | |||
139 | /* Replace the return addr with trampoline addr */ | ||
140 | regs->link = (unsigned long)kretprobe_trampoline; | ||
141 | add_rp_inst(ri); | ||
142 | } else { | ||
143 | rp->nmissed++; | ||
144 | } | ||
145 | } | 136 | } |
146 | 137 | ||
147 | static int __kprobes kprobe_handler(struct pt_regs *regs) | 138 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
diff --git a/arch/s390/kernel/kprobes.c b/arch/s390/kernel/kprobes.c index 8516a94d8163..9d0f0d09d473 100644 --- a/arch/s390/kernel/kprobes.c +++ b/arch/s390/kernel/kprobes.c | |||
@@ -271,23 +271,13 @@ static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | |||
271 | } | 271 | } |
272 | 272 | ||
273 | /* Called with kretprobe_lock held */ | 273 | /* Called with kretprobe_lock held */ |
274 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, | 274 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
275 | struct pt_regs *regs) | 275 | struct pt_regs *regs) |
276 | { | 276 | { |
277 | struct kretprobe_instance *ri; | 277 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; |
278 | 278 | ||
279 | if ((ri = get_free_rp_inst(rp)) != NULL) { | 279 | /* Replace the return addr with trampoline addr */ |
280 | ri->rp = rp; | 280 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; |
281 | ri->task = current; | ||
282 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; | ||
283 | |||
284 | /* Replace the return addr with trampoline addr */ | ||
285 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; | ||
286 | |||
287 | add_rp_inst(ri); | ||
288 | } else { | ||
289 | rp->nmissed++; | ||
290 | } | ||
291 | } | 281 | } |
292 | 282 | ||
293 | static int __kprobes kprobe_handler(struct pt_regs *regs) | 283 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
diff --git a/arch/x86_64/kernel/kprobes.c b/arch/x86_64/kernel/kprobes.c index 5841ba5f479b..f995bea6e2c1 100644 --- a/arch/x86_64/kernel/kprobes.c +++ b/arch/x86_64/kernel/kprobes.c | |||
@@ -266,23 +266,14 @@ static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) | |||
266 | } | 266 | } |
267 | 267 | ||
268 | /* Called with kretprobe_lock held */ | 268 | /* Called with kretprobe_lock held */ |
269 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, | 269 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
270 | struct pt_regs *regs) | 270 | struct pt_regs *regs) |
271 | { | 271 | { |
272 | unsigned long *sara = (unsigned long *)regs->rsp; | 272 | unsigned long *sara = (unsigned long *)regs->rsp; |
273 | struct kretprobe_instance *ri; | ||
274 | 273 | ||
275 | if ((ri = get_free_rp_inst(rp)) != NULL) { | 274 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
276 | ri->rp = rp; | 275 | /* Replace the return addr with trampoline addr */ |
277 | ri->task = current; | 276 | *sara = (unsigned long) &kretprobe_trampoline; |
278 | ri->ret_addr = (kprobe_opcode_t *) *sara; | ||
279 | |||
280 | /* Replace the return addr with trampoline addr */ | ||
281 | *sara = (unsigned long) &kretprobe_trampoline; | ||
282 | add_rp_inst(ri); | ||
283 | } else { | ||
284 | rp->nmissed++; | ||
285 | } | ||
286 | } | 277 | } |
287 | 278 | ||
288 | int __kprobes kprobe_handler(struct pt_regs *regs) | 279 | int __kprobes kprobe_handler(struct pt_regs *regs) |
diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 850ee871e353..6fc623e41fd8 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h | |||
@@ -123,7 +123,8 @@ DECLARE_PER_CPU(struct kprobe *, current_kprobe); | |||
123 | DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | 123 | DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
124 | 124 | ||
125 | #ifdef ARCH_SUPPORTS_KRETPROBES | 125 | #ifdef ARCH_SUPPORTS_KRETPROBES |
126 | extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs); | 126 | extern void arch_prepare_kretprobe(struct kretprobe_instance *ri, |
127 | struct pt_regs *regs); | ||
127 | #else /* ARCH_SUPPORTS_KRETPROBES */ | 128 | #else /* ARCH_SUPPORTS_KRETPROBES */ |
128 | static inline void arch_prepare_kretprobe(struct kretprobe *rp, | 129 | static inline void arch_prepare_kretprobe(struct kretprobe *rp, |
129 | struct pt_regs *regs) | 130 | struct pt_regs *regs) |
@@ -209,8 +210,6 @@ void jprobe_return(void); | |||
209 | int register_kretprobe(struct kretprobe *rp); | 210 | int register_kretprobe(struct kretprobe *rp); |
210 | void unregister_kretprobe(struct kretprobe *rp); | 211 | void unregister_kretprobe(struct kretprobe *rp); |
211 | 212 | ||
212 | struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp); | ||
213 | void add_rp_inst(struct kretprobe_instance *ri); | ||
214 | void kprobe_flush_task(struct task_struct *tk); | 213 | void kprobe_flush_task(struct task_struct *tk); |
215 | void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); | 214 | void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); |
216 | #else /* CONFIG_KPROBES */ | 215 | #else /* CONFIG_KPROBES */ |
diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 22857003a65b..f58f171bd65f 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c | |||
@@ -358,46 +358,6 @@ void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) | |||
358 | } | 358 | } |
359 | 359 | ||
360 | /* Called with kretprobe_lock held */ | 360 | /* Called with kretprobe_lock held */ |
361 | struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp) | ||
362 | { | ||
363 | struct hlist_node *node; | ||
364 | struct kretprobe_instance *ri; | ||
365 | hlist_for_each_entry(ri, node, &rp->free_instances, uflist) | ||
366 | return ri; | ||
367 | return NULL; | ||
368 | } | ||
369 | |||
370 | /* Called with kretprobe_lock held */ | ||
371 | static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe | ||
372 | *rp) | ||
373 | { | ||
374 | struct hlist_node *node; | ||
375 | struct kretprobe_instance *ri; | ||
376 | hlist_for_each_entry(ri, node, &rp->used_instances, uflist) | ||
377 | return ri; | ||
378 | return NULL; | ||
379 | } | ||
380 | |||
381 | /* Called with kretprobe_lock held */ | ||
382 | void __kprobes add_rp_inst(struct kretprobe_instance *ri) | ||
383 | { | ||
384 | /* | ||
385 | * Remove rp inst off the free list - | ||
386 | * Add it back when probed function returns | ||
387 | */ | ||
388 | hlist_del(&ri->uflist); | ||
389 | |||
390 | /* Add rp inst onto table */ | ||
391 | INIT_HLIST_NODE(&ri->hlist); | ||
392 | hlist_add_head(&ri->hlist, | ||
393 | &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]); | ||
394 | |||
395 | /* Also add this rp inst to the used list. */ | ||
396 | INIT_HLIST_NODE(&ri->uflist); | ||
397 | hlist_add_head(&ri->uflist, &ri->rp->used_instances); | ||
398 | } | ||
399 | |||
400 | /* Called with kretprobe_lock held */ | ||
401 | void __kprobes recycle_rp_inst(struct kretprobe_instance *ri, | 361 | void __kprobes recycle_rp_inst(struct kretprobe_instance *ri, |
402 | struct hlist_head *head) | 362 | struct hlist_head *head) |
403 | { | 363 | { |
@@ -450,7 +410,9 @@ void __kprobes kprobe_flush_task(struct task_struct *tk) | |||
450 | static inline void free_rp_inst(struct kretprobe *rp) | 410 | static inline void free_rp_inst(struct kretprobe *rp) |
451 | { | 411 | { |
452 | struct kretprobe_instance *ri; | 412 | struct kretprobe_instance *ri; |
453 | while ((ri = get_free_rp_inst(rp)) != NULL) { | 413 | struct hlist_node *pos, *next; |
414 | |||
415 | hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) { | ||
454 | hlist_del(&ri->uflist); | 416 | hlist_del(&ri->uflist); |
455 | kfree(ri); | 417 | kfree(ri); |
456 | } | 418 | } |
@@ -732,7 +694,21 @@ static int __kprobes pre_handler_kretprobe(struct kprobe *p, | |||
732 | 694 | ||
733 | /*TODO: consider to only swap the RA after the last pre_handler fired */ | 695 | /*TODO: consider to only swap the RA after the last pre_handler fired */ |
734 | spin_lock_irqsave(&kretprobe_lock, flags); | 696 | spin_lock_irqsave(&kretprobe_lock, flags); |
735 | arch_prepare_kretprobe(rp, regs); | 697 | if (!hlist_empty(&rp->free_instances)) { |
698 | struct kretprobe_instance *ri; | ||
699 | |||
700 | ri = hlist_entry(rp->free_instances.first, | ||
701 | struct kretprobe_instance, uflist); | ||
702 | ri->rp = rp; | ||
703 | ri->task = current; | ||
704 | arch_prepare_kretprobe(ri, regs); | ||
705 | |||
706 | /* XXX(hch): why is there no hlist_move_head? */ | ||
707 | hlist_del(&ri->uflist); | ||
708 | hlist_add_head(&ri->uflist, &ri->rp->used_instances); | ||
709 | hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task)); | ||
710 | } else | ||
711 | rp->nmissed++; | ||
736 | spin_unlock_irqrestore(&kretprobe_lock, flags); | 712 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
737 | return 0; | 713 | return 0; |
738 | } | 714 | } |
@@ -795,11 +771,13 @@ void __kprobes unregister_kretprobe(struct kretprobe *rp) | |||
795 | { | 771 | { |
796 | unsigned long flags; | 772 | unsigned long flags; |
797 | struct kretprobe_instance *ri; | 773 | struct kretprobe_instance *ri; |
774 | struct hlist_node *pos, *next; | ||
798 | 775 | ||
799 | unregister_kprobe(&rp->kp); | 776 | unregister_kprobe(&rp->kp); |
777 | |||
800 | /* No race here */ | 778 | /* No race here */ |
801 | spin_lock_irqsave(&kretprobe_lock, flags); | 779 | spin_lock_irqsave(&kretprobe_lock, flags); |
802 | while ((ri = get_used_rp_inst(rp)) != NULL) { | 780 | hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) { |
803 | ri->rp = NULL; | 781 | ri->rp = NULL; |
804 | hlist_del(&ri->uflist); | 782 | hlist_del(&ri->uflist); |
805 | } | 783 | } |