aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/i386/kernel/kprobes.c17
-rw-r--r--arch/ia64/kernel/kprobes.c18
-rw-r--r--arch/powerpc/kernel/kprobes.c19
-rw-r--r--arch/s390/kernel/kprobes.c18
-rw-r--r--arch/x86_64/kernel/kprobes.c17
-rw-r--r--include/linux/kprobes.h5
-rw-r--r--kernel/kprobes.c64
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 */
229void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, 229void __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 *)&regs->esp; 232 unsigned long *sara = (unsigned long *)&regs->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 */
468void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, 468void __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
487int __kprobes arch_prepare_kprobe(struct kprobe *p) 477int __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 */
129void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, 129void __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
147static int __kprobes kprobe_handler(struct pt_regs *regs) 138static 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 */
274void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, 274void __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
293static int __kprobes kprobe_handler(struct pt_regs *regs) 283static 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 */
269void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, 269void __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
288int __kprobes kprobe_handler(struct pt_regs *regs) 279int __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);
123DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 123DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
124 124
125#ifdef ARCH_SUPPORTS_KRETPROBES 125#ifdef ARCH_SUPPORTS_KRETPROBES
126extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs); 126extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
127 struct pt_regs *regs);
127#else /* ARCH_SUPPORTS_KRETPROBES */ 128#else /* ARCH_SUPPORTS_KRETPROBES */
128static inline void arch_prepare_kretprobe(struct kretprobe *rp, 129static 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);
209int register_kretprobe(struct kretprobe *rp); 210int register_kretprobe(struct kretprobe *rp);
210void unregister_kretprobe(struct kretprobe *rp); 211void unregister_kretprobe(struct kretprobe *rp);
211 212
212struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
213void add_rp_inst(struct kretprobe_instance *ri);
214void kprobe_flush_task(struct task_struct *tk); 213void kprobe_flush_task(struct task_struct *tk);
215void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); 214void 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 */
361struct 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 */
371static 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 */
382void __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 */
401void __kprobes recycle_rp_inst(struct kretprobe_instance *ri, 361void __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)
450static inline void free_rp_inst(struct kretprobe *rp) 410static 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 }