2 * kprobe/swap_kprobes.c
3 * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM and MIPS
4 * @author Alexey Gerenkov <a.gerenkov@samsung.com> User-Space Probes initial implementation;
5 * Support x86/ARM/MIPS for both user and kernel spaces.
6 * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Copyright (C) IBM Corporation, 2002, 2004
27 * Copyright (C) Samsung Electronics, 2006-2010
29 * @section DESCRIPTION
31 * SWAP kprobe implementation. Dynamic kernel functions instrumentation.
34 #include <linux/version.h>
36 #include <linux/hash.h>
37 #include <linux/module.h>
39 #include <linux/pagemap.h>
40 #include <linux/stop_machine.h>
42 #include <ksyms/ksyms.h>
43 #include <master/swap_initializer.h>
44 #include <swap-asm/swap_kprobes.h>
46 #include "swap_slots.h"
47 #include "swap_kdebug.h"
48 #include "swap_kprobes.h"
49 #include "swap_kprobes_deps.h"
53 * @brief Scheduler address.
55 unsigned long sched_addr;
56 static unsigned long exit_addr;
57 static unsigned long do_group_exit_addr;
58 static unsigned long sys_exit_group_addr;
59 static unsigned long sys_exit_addr;
63 * @brief Current slot manager. Slots are the places where trampolines are
66 struct slot_manager sm;
68 DEFINE_PER_CPU(struct kprobe *, swap_current_kprobe);
69 static DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
71 static DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
72 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance);
74 struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
75 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
79 * @brief Count of kprobes.
81 atomic_t kprobe_count;
82 EXPORT_SYMBOL_GPL(kprobe_count);
85 static void *(*module_alloc)(unsigned long size);
86 static void *(*module_free)(struct module *mod, void *module_region);
88 static void *__wrapper_module_alloc(unsigned long size)
90 return module_alloc(size);
93 static void *__wrapper_module_free(void *module_region)
95 return module_free(NULL, module_region);
98 static void *sm_alloc(struct slot_manager *sm)
100 return __wrapper_module_alloc(PAGE_SIZE);
103 static void sm_free(struct slot_manager *sm, void *ptr)
105 __wrapper_module_free(ptr);
108 static void init_sm(void)
110 sm.slot_size = KPROBES_TRAMP_LEN;
113 INIT_HLIST_HEAD(&sm.page_list);
116 static void exit_sm(void)
121 static void kretprobe_assert(struct kretprobe_instance *ri,
122 unsigned long orig_ret_address,
123 unsigned long trampoline_address)
125 if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
126 struct task_struct *task;
128 panic("kretprobe BUG!: ri = NULL\n");
133 panic("kretprobe BUG!: task = NULL\n");
136 panic("kretprobe BUG!: ri->rp = NULL\n");
138 panic("kretprobe BUG!: "
139 "Processing kretprobe %p @ %p (%d/%d - %s)\n",
140 ri->rp, ri->rp->kp.addr, ri->task->tgid,
141 ri->task->pid, ri->task->comm);
145 /* We have preemption disabled.. so it is safe to use __ versions */
146 static inline void set_kprobe_instance(struct kprobe *kp)
148 __get_cpu_var(kprobe_instance) = kp;
151 static inline void reset_kprobe_instance(void)
153 __get_cpu_var(kprobe_instance) = NULL;
157 * @brief Gets the current kprobe on this CPU.
159 * @return Pointer to the current kprobe.
161 struct kprobe *swap_kprobe_running(void)
163 return __get_cpu_var(swap_current_kprobe);
167 * @brief Sets the current kprobe to NULL.
171 void swap_reset_current_kprobe(void)
173 __get_cpu_var(swap_current_kprobe) = NULL;
177 * @brief Gets kprobe_ctlblk for the current CPU.
179 * @return Current CPU struct kprobe_ctlblk.
181 struct kprobe_ctlblk *swap_get_kprobe_ctlblk(void)
183 return &__get_cpu_var(kprobe_ctlblk);
187 * This routine is called either:
188 * - under the kprobe_mutex - during kprobe_[un]register()
190 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
194 * @brief Gets kprobe.
196 * @param addr Probe address.
197 * @return Kprobe for addr.
199 struct kprobe *swap_get_kprobe(void *addr)
201 struct hlist_head *head;
203 DECLARE_NODE_PTR_FOR_HLIST(node);
205 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
206 swap_hlist_for_each_entry_rcu(p, node, head, hlist) {
215 * Aggregate handlers for multiple kprobes support - these handlers
216 * take care of invoking the individual kprobe handlers on p->list
218 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
223 list_for_each_entry_rcu(kp, &p->list, list) {
224 if (kp->pre_handler) {
225 set_kprobe_instance(kp);
226 ret = kp->pre_handler(kp, regs);
230 reset_kprobe_instance();
236 static void aggr_post_handler(struct kprobe *p,
237 struct pt_regs *regs,
242 list_for_each_entry_rcu(kp, &p->list, list) {
243 if (kp->post_handler) {
244 set_kprobe_instance(kp);
245 kp->post_handler(kp, regs, flags);
246 reset_kprobe_instance();
251 static int aggr_fault_handler(struct kprobe *p,
252 struct pt_regs *regs,
255 struct kprobe *cur = __get_cpu_var(kprobe_instance);
258 * if we faulted "during" the execution of a user specified
259 * probe handler, invoke just that probe's fault handler
261 if (cur && cur->fault_handler) {
262 if (cur->fault_handler(cur, regs, trapnr))
269 static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
271 struct kprobe *cur = __get_cpu_var(kprobe_instance);
273 DBPRINTF("cur = 0x%p\n", cur);
275 DBPRINTF("cur = 0x%p cur->break_handler = 0x%p\n",
276 cur, cur->break_handler);
278 if (cur && cur->break_handler) {
279 if (cur->break_handler(cur, regs))
282 reset_kprobe_instance();
288 * @brief Walks the list and increments nmissed count for multiprobe case.
290 * @param p Pointer to the missed kprobe.
293 void swap_kprobes_inc_nmissed_count(struct kprobe *p)
296 if (p->pre_handler != aggr_pre_handler) {
299 list_for_each_entry_rcu(kp, &p->list, list) {
305 static int alloc_nodes_kretprobe(struct kretprobe *rp);
307 /* Called with kretprobe_lock held */
308 static struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
310 struct kretprobe_instance *ri;
311 DECLARE_NODE_PTR_FOR_HLIST(node);
313 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
317 if (!alloc_nodes_kretprobe(rp)) {
318 swap_hlist_for_each_entry(ri, node, &rp->free_instances,
327 /* Called with kretprobe_lock held */
328 static struct kretprobe_instance *
329 get_free_rp_inst_no_alloc(struct kretprobe *rp)
331 struct kretprobe_instance *ri;
332 DECLARE_NODE_PTR_FOR_HLIST(node);
334 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
341 /* Called with kretprobe_lock held */
342 static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
344 struct kretprobe_instance *ri;
345 DECLARE_NODE_PTR_FOR_HLIST(node);
347 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
354 /* Called with kretprobe_lock held */
355 static void add_rp_inst(struct kretprobe_instance *ri)
358 * Remove rp inst off the free list -
359 * Add it back when probed function returns
361 hlist_del(&ri->uflist);
363 /* Add rp inst onto table */
364 INIT_HLIST_NODE(&ri->hlist);
366 hlist_add_head(&ri->hlist,
367 &kretprobe_inst_table[hash_ptr(ri->task,
370 /* Also add this rp inst to the used list. */
371 INIT_HLIST_NODE(&ri->uflist);
372 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
375 /* Called with kretprobe_lock held */
376 static void recycle_rp_inst(struct kretprobe_instance *ri)
379 hlist_del(&ri->hlist);
380 /* remove rp inst off the used list */
381 hlist_del(&ri->uflist);
382 /* put rp inst back onto the free list */
383 INIT_HLIST_NODE(&ri->uflist);
384 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
388 static struct hlist_head *kretprobe_inst_table_head(void *hash_key)
390 return &kretprobe_inst_table[hash_ptr(hash_key, KPROBE_HASH_BITS)];
393 static void free_rp_inst(struct kretprobe *rp)
395 struct kretprobe_instance *ri;
396 while ((ri = get_free_rp_inst_no_alloc(rp)) != NULL) {
397 hlist_del(&ri->uflist);
403 * Keep all fields in the kprobe consistent
405 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
407 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
408 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
412 * Add the new probe to old_p->list. Fail if this is the
413 * second jprobe at the address - two jprobes can't coexist
415 static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
417 if (p->break_handler) {
418 if (old_p->break_handler)
421 list_add_tail_rcu(&p->list, &old_p->list);
422 old_p->break_handler = aggr_break_handler;
424 list_add_rcu(&p->list, &old_p->list);
427 if (p->post_handler && !old_p->post_handler)
428 old_p->post_handler = aggr_post_handler;
434 * hlist_replace_rcu - replace old entry by new one
435 * @old : the element to be replaced
436 * @new : the new element to insert
438 * The @old entry will be replaced with the @new entry atomically.
440 inline void swap_hlist_replace_rcu(struct hlist_node *old,
441 struct hlist_node *new)
443 struct hlist_node *next = old->next;
446 new->pprev = old->pprev;
449 new->next->pprev = &new->next;
452 old->pprev = LIST_POISON2;
456 * Fill in the required fields of the "manager kprobe". Replace the
457 * earlier kprobe in the hlist with the manager kprobe
459 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
463 ap->pre_handler = aggr_pre_handler;
464 ap->fault_handler = aggr_fault_handler;
466 ap->post_handler = aggr_post_handler;
467 if (p->break_handler)
468 ap->break_handler = aggr_break_handler;
470 INIT_LIST_HEAD(&ap->list);
471 list_add_rcu(&p->list, &ap->list);
473 swap_hlist_replace_rcu(&p->hlist, &ap->hlist);
477 * This is the second or subsequent kprobe at the address - handle
480 static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
486 DBPRINTF("p = %p old_p = %p\n", p, old_p);
487 if (old_p->pre_handler == aggr_pre_handler) {
488 DBPRINTF("aggr_pre_handler\n");
490 copy_kprobe(old_p, p);
491 ret = add_new_kprobe(old_p, p);
493 DBPRINTF("kzalloc\n");
495 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
497 ap = kmalloc(sizeof(struct kprobe), GFP_KERNEL);
499 memset(ap, 0, sizeof(struct kprobe));
503 add_aggr_kprobe(ap, old_p);
505 DBPRINTF("ap = %p p = %p old_p = %p\n", ap, p, old_p);
506 ret = add_new_kprobe(ap, p);
512 static void remove_kprobe(struct kprobe *p)
514 /* TODO: check boostable for x86 and MIPS */
515 swap_slot_free(&sm, p->ainsn.insn);
519 * @brief Registers kprobe.
521 * @param p Pointer to the target kprobe.
522 * @return 0 on success, error code on error.
524 int swap_register_kprobe(struct kprobe *p)
526 struct kprobe *old_p;
529 * If we have a symbol_name argument look it up,
530 * and add it to the address. That way the addr
531 * field can either be global or relative to a symbol.
533 if (p->symbol_name) {
536 p->addr = (kprobe_opcode_t *)swap_ksyms(p->symbol_name);
541 DBPRINTF("p->addr = 0x%p\n", p->addr);
542 p->addr = (kprobe_opcode_t *)(((char *)p->addr) + p->offset);
543 DBPRINTF("p->addr = 0x%p p = 0x%p\n", p->addr, p);
545 #ifdef KPROBES_PROFILE
546 p->start_tm.tv_sec = p->start_tm.tv_usec = 0;
547 p->hnd_tm_sum.tv_sec = p->hnd_tm_sum.tv_usec = 0;
550 p->mod_refcounted = 0;
552 INIT_LIST_HEAD(&p->list);
554 old_p = swap_get_kprobe(p->addr);
556 ret = register_aggr_kprobe(old_p, p);
558 atomic_inc(&kprobe_count);
561 ret = swap_arch_prepare_kprobe(p, &sm);
565 DBPRINTF("before out ret = 0x%x\n", ret);
566 INIT_HLIST_NODE(&p->hlist);
567 hlist_add_head_rcu(&p->hlist,
568 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
569 swap_arch_arm_kprobe(p);
572 DBPRINTF("out ret = 0x%x\n", ret);
575 EXPORT_SYMBOL_GPL(swap_register_kprobe);
577 static void swap_unregister_valid_kprobe(struct kprobe *p, struct kprobe *old_p)
579 struct kprobe *list_p;
581 if ((old_p == p) || ((old_p->pre_handler == aggr_pre_handler) &&
582 (p->list.next == &old_p->list) && (p->list.prev == &old_p->list))) {
583 /* Only probe on the hash list */
584 swap_arch_disarm_kprobe(p);
586 /* FIXME: move sync out from atomic context */
590 hlist_del_rcu(&old_p->hlist);
591 remove_kprobe(old_p);
594 list_del_rcu(&old_p->list);
597 /* Synchronize and remove probe in bottom */
599 list_del_rcu(&p->list);
601 if (p->break_handler)
602 old_p->break_handler = NULL;
603 if (p->post_handler) {
604 list_for_each_entry_rcu(list_p, &old_p->list, list)
605 if (list_p->post_handler)
608 old_p->post_handler = NULL;
611 /* Set NULL addr for reusability if symbol_name is used */
617 * @brief Unregistes kprobe.
619 * @param kp Pointer to the target kprobe.
622 void swap_unregister_kprobe(struct kprobe *kp)
624 struct kprobe *old_p, *list_p;
626 old_p = swap_get_kprobe(kp->addr);
627 if (unlikely(!old_p))
631 list_for_each_entry_rcu(list_p, &old_p->list, list)
633 goto unreg_valid_kprobe;
639 swap_unregister_valid_kprobe(kp, old_p);
641 EXPORT_SYMBOL_GPL(swap_unregister_kprobe);
644 * @brief Registers jprobe.
646 * @param jp Pointer to the target jprobe.
647 * @return swap_register_kprobe result.
649 int swap_register_jprobe(struct jprobe *jp)
651 /* Todo: Verify probepoint is a function entry point */
652 jp->kp.pre_handler = swap_setjmp_pre_handler;
653 jp->kp.break_handler = swap_longjmp_break_handler;
655 return swap_register_kprobe(&jp->kp);
657 EXPORT_SYMBOL_GPL(swap_register_jprobe);
660 * @brief Unregisters jprobe.
662 * @param jp Pointer to the target jprobe.
665 void swap_unregister_jprobe(struct jprobe *jp)
667 swap_unregister_kprobe(&jp->kp);
669 EXPORT_SYMBOL_GPL(swap_unregister_jprobe);
672 * This kprobe pre_handler is registered with every kretprobe. When probe
673 * hits it will set up the return probe.
675 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
677 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
678 struct kretprobe_instance *ri;
679 unsigned long flags = 0;
681 /* TODO: consider to only swap the RA
682 * after the last pre_handler fired */
683 spin_lock_irqsave(&kretprobe_lock, flags);
685 /* TODO: test - remove retprobe after func entry but before its exit */
686 ri = get_free_rp_inst(rp);
693 if (rp->entry_handler)
694 skip = rp->entry_handler(ri, regs);
700 swap_arch_prepare_kretprobe(ri, regs);
707 spin_unlock_irqrestore(&kretprobe_lock, flags);
713 * @brief Trampoline probe handler.
715 * @param p Pointer to the fired kprobe.
716 * @param regs Pointer to CPU registers data.
717 * @return orig_ret_address
719 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
721 struct kretprobe_instance *ri = NULL;
722 struct hlist_head *head;
723 unsigned long flags, orig_ret_address = 0;
724 unsigned long trampoline_address;
726 struct kprobe_ctlblk *kcb;
728 struct hlist_node *tmp;
729 DECLARE_NODE_PTR_FOR_HLIST(node);
731 trampoline_address = (unsigned long)&swap_kretprobe_trampoline;
734 kcb = swap_get_kprobe_ctlblk();
736 spin_lock_irqsave(&kretprobe_lock, flags);
739 * We are using different hash keys (current and mm) for finding kernel
740 * space and user space probes. Kernel space probes can change mm field
741 * in task_struct. User space probes can be shared between threads of
742 * one process so they have different current but same mm.
744 head = kretprobe_inst_table_head(current);
747 regs->XREG(cs) = __KERNEL_CS | get_kernel_rpl();
748 regs->EREG(ip) = trampoline_address;
749 regs->ORIG_EAX_REG = 0xffffffff;
753 * It is possible to have multiple instances associated with a given
754 * task either because an multiple functions in the call path
755 * have a return probe installed on them, and/or more then one
756 * return probe was registered for a target function.
758 * We can handle this because:
759 * - instances are always inserted at the head of the list
760 * - when multiple return probes are registered for the same
761 * function, the first instance's ret_addr will point to the
762 * real return address, and all the rest will point to
763 * kretprobe_trampoline
765 swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
766 if (ri->task != current)
767 /* another task is sharing our hash bucket */
769 if (ri->rp && ri->rp->handler) {
770 __get_cpu_var(swap_current_kprobe) = &ri->rp->kp;
771 swap_get_kprobe_ctlblk()->kprobe_status =
773 ri->rp->handler(ri, regs);
774 __get_cpu_var(swap_current_kprobe) = NULL;
777 orig_ret_address = (unsigned long)ri->ret_addr;
779 if (orig_ret_address != trampoline_address)
781 * This is the real return address. Any other
782 * instances associated with this task are for
783 * other calls deeper on the call stack
787 kretprobe_assert(ri, orig_ret_address, trampoline_address);
789 if (kcb->kprobe_status == KPROBE_REENTER)
790 restore_previous_kprobe(kcb);
792 swap_reset_current_kprobe();
794 spin_unlock_irqrestore(&kretprobe_lock, flags);
795 swap_preempt_enable_no_resched();
798 * By returning a non-zero value, we are telling
799 * kprobe_handler() that we don't want the post_handler
800 * to run (and have re-enabled preemption)
803 return (int)orig_ret_address;
806 #define SCHED_RP_NR 200
807 #define COMMON_RP_NR 10
809 static int alloc_nodes_kretprobe(struct kretprobe *rp)
812 struct kretprobe_instance *inst;
815 DBPRINTF("Alloc aditional mem for retprobes");
817 if ((unsigned long)rp->kp.addr == sched_addr) {
818 rp->maxactive += SCHED_RP_NR; /* max (100, 2 * NR_CPUS); */
819 alloc_nodes = SCHED_RP_NR;
821 #if 1/* def CONFIG_PREEMPT */
822 rp->maxactive += max(COMMON_RP_NR, 2 * NR_CPUS);
824 rp->maxacpptive += NR_CPUS;
826 alloc_nodes = COMMON_RP_NR;
829 for (i = 0; i < alloc_nodes; i++) {
830 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_ATOMIC);
835 INIT_HLIST_NODE(&inst->uflist);
836 hlist_add_head(&inst->uflist, &rp->free_instances);
839 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
840 (unsigned long) (*(rp->kp.addr)),
841 (unsigned long) (*(rp->kp.addr + 1)),
842 (unsigned long) (*(rp->kp.addr + 2)));
847 * @brief Registers kretprobes.
849 * @param rp Pointer to the target kretprobe.
850 * @return 0 on success, error code on error.
852 int swap_register_kretprobe(struct kretprobe *rp)
855 struct kretprobe_instance *inst;
859 rp->kp.pre_handler = pre_handler_kretprobe;
860 rp->kp.post_handler = NULL;
861 rp->kp.fault_handler = NULL;
862 rp->kp.break_handler = NULL;
864 /* Pre-allocate memory for max kretprobe instances */
865 if ((unsigned long)rp->kp.addr == exit_addr) {
866 rp->kp.pre_handler = NULL; /* not needed for do_exit */
868 } else if ((unsigned long)rp->kp.addr == do_group_exit_addr) {
869 rp->kp.pre_handler = NULL;
871 } else if ((unsigned long)rp->kp.addr == sys_exit_group_addr) {
872 rp->kp.pre_handler = NULL;
874 } else if ((unsigned long)rp->kp.addr == sys_exit_addr) {
875 rp->kp.pre_handler = NULL;
877 } else if (rp->maxactive <= 0) {
878 #if 1/* def CONFIG_PREEMPT */
879 rp->maxactive = max(COMMON_RP_NR, 2 * NR_CPUS);
881 rp->maxactive = NR_CPUS;
884 INIT_HLIST_HEAD(&rp->used_instances);
885 INIT_HLIST_HEAD(&rp->free_instances);
886 for (i = 0; i < rp->maxactive; i++) {
887 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_KERNEL);
892 INIT_HLIST_NODE(&inst->uflist);
893 hlist_add_head(&inst->uflist, &rp->free_instances);
896 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
897 (unsigned long) (*(rp->kp.addr)),
898 (unsigned long) (*(rp->kp.addr + 1)),
899 (unsigned long) (*(rp->kp.addr + 2)));
901 /* Establish function entry probe point */
902 ret = swap_register_kprobe(&rp->kp);
906 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
907 (unsigned long) (*(rp->kp.addr)),
908 (unsigned long) (*(rp->kp.addr + 1)),
909 (unsigned long) (*(rp->kp.addr + 2)));
913 EXPORT_SYMBOL_GPL(swap_register_kretprobe);
915 static int swap_disarm_krp_inst(struct kretprobe_instance *ri);
917 static void swap_disarm_krp(struct kretprobe *rp)
919 struct kretprobe_instance *ri;
920 DECLARE_NODE_PTR_FOR_HLIST(node);
922 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
923 if (swap_disarm_krp_inst(ri) != 0) {
924 printk(KERN_INFO "%s (%d/%d): cannot disarm "
925 "krp instance (%08lx)\n",
926 ri->task->comm, ri->task->tgid, ri->task->pid,
927 (unsigned long)rp->kp.addr);
933 struct unreg_krp_args {
934 struct kretprobe **rps;
939 static int __swap_unregister_kretprobes_top(void *data)
941 struct unreg_krp_args *args = data;
942 struct kretprobe **rps = args->rps;
943 size_t size = args->size;
944 int rp_disarm = args->rp_disarm;
946 const size_t end = ((size_t) 0) - 1;
948 for (--size; size != end; --size) {
949 swap_unregister_kprobe(&rps[size]->kp);
951 spin_lock_irqsave(&kretprobe_lock, flags);
952 swap_disarm_krp(rps[size]);
953 spin_unlock_irqrestore(&kretprobe_lock, flags);
961 * @brief Kretprobes unregister top. Unregisters kprobes.
963 * @param rps Pointer to the array of pointers to the target kretprobes.
964 * @param size Size of rps array.
965 * @param rp_disarm Disarm flag. If set kretprobe is disarmed.
968 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
971 struct unreg_krp_args args = {
974 .rp_disarm = rp_disarm,
980 ret = stop_machine(__swap_unregister_kretprobes_top,
983 pr_err("%s failed (%d)\n", __func__, ret);
985 __swap_unregister_kretprobes_top(&args);
988 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes_top);
991 * @brief swap_unregister_kretprobes_top wrapper for a single kretprobe.
993 * @param rp Pointer to the target kretprobe.
994 * @param rp_disarm Disarm flag.
997 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm)
999 swap_unregister_kretprobes_top(&rp, 1, rp_disarm);
1001 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe_top);
1004 * @brief Kretprobe unregister bottom. Here is kretprobe memory is released.
1006 * @param rp Pointer to the target kretprobe.
1009 void swap_unregister_kretprobe_bottom(struct kretprobe *rp)
1011 unsigned long flags;
1012 struct kretprobe_instance *ri;
1014 spin_lock_irqsave(&kretprobe_lock, flags);
1016 while ((ri = get_used_rp_inst(rp)) != NULL)
1017 recycle_rp_inst(ri);
1020 spin_unlock_irqrestore(&kretprobe_lock, flags);
1022 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe_bottom);
1025 * @brief swap_unregister_kretprobe_bottom wrapper for several kretprobes.
1027 * @param rps Pointer to the array of the target kretprobes pointers.
1028 * @param size Size of rps array.
1031 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size)
1033 const size_t end = ((size_t) 0) - 1;
1035 for (--size; size != end; --size)
1036 swap_unregister_kretprobe_bottom(rps[size]);
1038 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes_bottom);
1041 * @brief Unregisters kretprobes.
1043 * @param rpp Pointer to the array of the target kretprobes pointers.
1044 * @param size Size of rpp array.
1047 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size)
1049 swap_unregister_kretprobes_top(rpp, size, 1);
1052 synchronize_sched();
1054 swap_unregister_kretprobes_bottom(rpp, size);
1056 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes);
1059 * @brief swap_unregister_kretprobes wrapper for a single kretprobe.
1061 * @param rp Pointer to the target kretprobe.
1064 void swap_unregister_kretprobe(struct kretprobe *rp)
1066 swap_unregister_kretprobes(&rp, 1);
1068 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe);
1070 static inline void rm_task_trampoline(struct task_struct *p,
1071 struct kretprobe_instance *ri)
1073 arch_set_task_pc(p, (unsigned long)ri->ret_addr);
1076 static int swap_disarm_krp_inst(struct kretprobe_instance *ri)
1078 unsigned long *tramp = (unsigned long *)&swap_kretprobe_trampoline;
1079 unsigned long *sp = ri->sp;
1080 unsigned long *found = NULL;
1081 int retval = -ENOENT;
1084 unsigned long pc = arch_get_task_pc(ri->task);
1086 printk(KERN_INFO "---> [%d] %s (%d/%d): pc = %08lx, ra = %08lx, tramp= %08lx (%08lx)\n",
1088 ri->task->comm, ri->task->tgid, ri->task->pid,
1089 pc, (long unsigned int)ri->ret_addr,
1090 (long unsigned int)tramp,
1091 (long unsigned int)(ri->rp ? ri->rp->kp.addr : NULL));
1093 /* __switch_to retprobe handling */
1094 if (pc == (unsigned long)tramp) {
1095 rm_task_trampoline(ri->task, ri);
1102 while (sp > ri->sp - RETPROBE_STACK_DEPTH) {
1103 if (*sp == (unsigned long)tramp) {
1111 printk(KERN_INFO "---> [%d] %s (%d/%d): tramp (%08lx) "
1112 "found at %08lx (%08lx /%+d) - %p\n",
1114 ri->task->comm, ri->task->tgid, ri->task->pid,
1115 (long unsigned int)tramp,
1116 (long unsigned int)found, (long unsigned int)ri->sp,
1117 found - ri->sp, ri->rp ? ri->rp->kp.addr : NULL);
1118 *found = (unsigned long)ri->ret_addr;
1121 printk(KERN_INFO "---> [%d] %s (%d/%d): tramp (%08lx) "
1122 "NOT found at sp = %08lx - %p\n",
1124 ri->task->comm, ri->task->tgid, ri->task->pid,
1125 (long unsigned int)tramp,
1126 (long unsigned int)ri->sp,
1127 ri->rp ? ri->rp->kp.addr : NULL);
1133 static void krp_inst_flush(struct task_struct *task)
1135 unsigned long flags;
1136 struct kretprobe_instance *ri;
1137 struct hlist_node *tmp;
1138 struct hlist_head *head;
1139 DECLARE_NODE_PTR_FOR_HLIST(node);
1141 spin_lock_irqsave(&kretprobe_lock, flags);
1142 head = kretprobe_inst_table_head(task);
1143 swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
1144 if (ri->task == task) {
1145 printk("task[%u %u %s]: flush krp_inst, ret_addr=%p\n",
1146 task->tgid, task->pid, task->comm,
1148 recycle_rp_inst(ri);
1151 spin_unlock_irqrestore(&kretprobe_lock, flags);
1154 static int put_task_handler(struct kprobe *p, struct pt_regs *regs)
1156 struct task_struct *t = (struct task_struct *)swap_get_karg(regs, 0);
1164 static struct kprobe put_task_kp = {
1165 .pre_handler = put_task_handler,
1168 static int init_module_deps(void)
1172 sched_addr = swap_ksyms("__switch_to");
1173 exit_addr = swap_ksyms("do_exit");
1174 sys_exit_group_addr = swap_ksyms("sys_exit_group");
1175 do_group_exit_addr = swap_ksyms("do_group_exit");
1176 sys_exit_addr = swap_ksyms("sys_exit");
1178 if (sched_addr == 0 ||
1180 sys_exit_group_addr == 0 ||
1181 do_group_exit_addr == 0 ||
1182 sys_exit_addr == 0) {
1186 ret = init_module_dependencies();
1190 return arch_init_module_deps();
1193 static int once(void)
1198 sym = "module_alloc";
1199 module_alloc = (void *)swap_ksyms(sym);
1200 if (module_alloc == NULL)
1203 sym = "module_free";
1204 module_free = (void *)swap_ksyms(sym);
1205 if (module_alloc == NULL)
1208 sym = "__put_task_struct";
1209 put_task_kp.addr = (void *)swap_ksyms(sym);
1210 if (put_task_kp.addr == NULL)
1213 ret = init_module_deps();
1218 * FIXME allocate the probe table, currently defined statically
1219 * initialize all list heads
1221 for (i = 0; i < KPROBE_TABLE_SIZE; ++i) {
1222 INIT_HLIST_HEAD(&kprobe_table[i]);
1223 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1229 printk(KERN_INFO "ERROR: symbol '%s' not found\n", sym);
1233 static int init_kprobes(void)
1238 atomic_set(&kprobe_count, 0);
1240 ret = swap_arch_init_kprobes();
1244 ret = swap_register_kprobe(&put_task_kp);
1246 swap_arch_exit_kprobes();
1253 static void exit_kprobes(void)
1255 swap_unregister_kprobe(&put_task_kp);
1256 swap_arch_exit_kprobes();
1260 SWAP_LIGHT_INIT_MODULE(once, init_kprobes, exit_kprobes, NULL, NULL);
1262 MODULE_LICENSE("GPL");