diff options
Diffstat (limited to 'kernel/kprobes.c')
-rw-r--r-- | kernel/kprobes.c | 298 |
1 files changed, 214 insertions, 84 deletions
diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 7ba8cd9845cb..a5e74ddee0e2 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c | |||
@@ -43,6 +43,7 @@ | |||
43 | #include <linux/seq_file.h> | 43 | #include <linux/seq_file.h> |
44 | #include <linux/debugfs.h> | 44 | #include <linux/debugfs.h> |
45 | #include <linux/kdebug.h> | 45 | #include <linux/kdebug.h> |
46 | #include <linux/memory.h> | ||
46 | 47 | ||
47 | #include <asm-generic/sections.h> | 48 | #include <asm-generic/sections.h> |
48 | #include <asm/cacheflush.h> | 49 | #include <asm/cacheflush.h> |
@@ -67,7 +68,7 @@ static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; | |||
67 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; | 68 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; |
68 | 69 | ||
69 | /* NOTE: change this value only with kprobe_mutex held */ | 70 | /* NOTE: change this value only with kprobe_mutex held */ |
70 | static bool kprobe_enabled; | 71 | static bool kprobes_all_disarmed; |
71 | 72 | ||
72 | static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */ | 73 | static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */ |
73 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; | 74 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
@@ -327,7 +328,7 @@ static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) | |||
327 | struct kprobe *kp; | 328 | struct kprobe *kp; |
328 | 329 | ||
329 | list_for_each_entry_rcu(kp, &p->list, list) { | 330 | list_for_each_entry_rcu(kp, &p->list, list) { |
330 | if (kp->pre_handler && !kprobe_gone(kp)) { | 331 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
331 | set_kprobe_instance(kp); | 332 | set_kprobe_instance(kp); |
332 | if (kp->pre_handler(kp, regs)) | 333 | if (kp->pre_handler(kp, regs)) |
333 | return 1; | 334 | return 1; |
@@ -343,7 +344,7 @@ static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs, | |||
343 | struct kprobe *kp; | 344 | struct kprobe *kp; |
344 | 345 | ||
345 | list_for_each_entry_rcu(kp, &p->list, list) { | 346 | list_for_each_entry_rcu(kp, &p->list, list) { |
346 | if (kp->post_handler && !kprobe_gone(kp)) { | 347 | if (kp->post_handler && likely(!kprobe_disabled(kp))) { |
347 | set_kprobe_instance(kp); | 348 | set_kprobe_instance(kp); |
348 | kp->post_handler(kp, regs, flags); | 349 | kp->post_handler(kp, regs, flags); |
349 | reset_kprobe_instance(); | 350 | reset_kprobe_instance(); |
@@ -517,20 +518,28 @@ static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) | |||
517 | } | 518 | } |
518 | 519 | ||
519 | /* | 520 | /* |
520 | * Add the new probe to old_p->list. Fail if this is the | 521 | * Add the new probe to ap->list. Fail if this is the |
521 | * second jprobe at the address - two jprobes can't coexist | 522 | * second jprobe at the address - two jprobes can't coexist |
522 | */ | 523 | */ |
523 | static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p) | 524 | static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
524 | { | 525 | { |
526 | BUG_ON(kprobe_gone(ap) || kprobe_gone(p)); | ||
525 | if (p->break_handler) { | 527 | if (p->break_handler) { |
526 | if (old_p->break_handler) | 528 | if (ap->break_handler) |
527 | return -EEXIST; | 529 | return -EEXIST; |
528 | list_add_tail_rcu(&p->list, &old_p->list); | 530 | list_add_tail_rcu(&p->list, &ap->list); |
529 | old_p->break_handler = aggr_break_handler; | 531 | ap->break_handler = aggr_break_handler; |
530 | } else | 532 | } else |
531 | list_add_rcu(&p->list, &old_p->list); | 533 | list_add_rcu(&p->list, &ap->list); |
532 | if (p->post_handler && !old_p->post_handler) | 534 | if (p->post_handler && !ap->post_handler) |
533 | old_p->post_handler = aggr_post_handler; | 535 | ap->post_handler = aggr_post_handler; |
536 | |||
537 | if (kprobe_disabled(ap) && !kprobe_disabled(p)) { | ||
538 | ap->flags &= ~KPROBE_FLAG_DISABLED; | ||
539 | if (!kprobes_all_disarmed) | ||
540 | /* Arm the breakpoint again. */ | ||
541 | arch_arm_kprobe(ap); | ||
542 | } | ||
534 | return 0; | 543 | return 0; |
535 | } | 544 | } |
536 | 545 | ||
@@ -543,6 +552,7 @@ static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) | |||
543 | copy_kprobe(p, ap); | 552 | copy_kprobe(p, ap); |
544 | flush_insn_slot(ap); | 553 | flush_insn_slot(ap); |
545 | ap->addr = p->addr; | 554 | ap->addr = p->addr; |
555 | ap->flags = p->flags; | ||
546 | ap->pre_handler = aggr_pre_handler; | 556 | ap->pre_handler = aggr_pre_handler; |
547 | ap->fault_handler = aggr_fault_handler; | 557 | ap->fault_handler = aggr_fault_handler; |
548 | /* We don't care the kprobe which has gone. */ | 558 | /* We don't care the kprobe which has gone. */ |
@@ -565,44 +575,59 @@ static int __kprobes register_aggr_kprobe(struct kprobe *old_p, | |||
565 | struct kprobe *p) | 575 | struct kprobe *p) |
566 | { | 576 | { |
567 | int ret = 0; | 577 | int ret = 0; |
568 | struct kprobe *ap; | 578 | struct kprobe *ap = old_p; |
569 | 579 | ||
570 | if (kprobe_gone(old_p)) { | 580 | if (old_p->pre_handler != aggr_pre_handler) { |
581 | /* If old_p is not an aggr_probe, create new aggr_kprobe. */ | ||
582 | ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL); | ||
583 | if (!ap) | ||
584 | return -ENOMEM; | ||
585 | add_aggr_kprobe(ap, old_p); | ||
586 | } | ||
587 | |||
588 | if (kprobe_gone(ap)) { | ||
571 | /* | 589 | /* |
572 | * Attempting to insert new probe at the same location that | 590 | * Attempting to insert new probe at the same location that |
573 | * had a probe in the module vaddr area which already | 591 | * had a probe in the module vaddr area which already |
574 | * freed. So, the instruction slot has already been | 592 | * freed. So, the instruction slot has already been |
575 | * released. We need a new slot for the new probe. | 593 | * released. We need a new slot for the new probe. |
576 | */ | 594 | */ |
577 | ret = arch_prepare_kprobe(old_p); | 595 | ret = arch_prepare_kprobe(ap); |
578 | if (ret) | 596 | if (ret) |
597 | /* | ||
598 | * Even if fail to allocate new slot, don't need to | ||
599 | * free aggr_probe. It will be used next time, or | ||
600 | * freed by unregister_kprobe. | ||
601 | */ | ||
579 | return ret; | 602 | return ret; |
580 | } | 603 | |
581 | if (old_p->pre_handler == aggr_pre_handler) { | ||
582 | copy_kprobe(old_p, p); | ||
583 | ret = add_new_kprobe(old_p, p); | ||
584 | ap = old_p; | ||
585 | } else { | ||
586 | ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL); | ||
587 | if (!ap) { | ||
588 | if (kprobe_gone(old_p)) | ||
589 | arch_remove_kprobe(old_p); | ||
590 | return -ENOMEM; | ||
591 | } | ||
592 | add_aggr_kprobe(ap, old_p); | ||
593 | copy_kprobe(ap, p); | ||
594 | ret = add_new_kprobe(ap, p); | ||
595 | } | ||
596 | if (kprobe_gone(old_p)) { | ||
597 | /* | 604 | /* |
598 | * If the old_p has gone, its breakpoint has been disarmed. | 605 | * Clear gone flag to prevent allocating new slot again, and |
599 | * We have to arm it again after preparing real kprobes. | 606 | * set disabled flag because it is not armed yet. |
600 | */ | 607 | */ |
601 | ap->flags &= ~KPROBE_FLAG_GONE; | 608 | ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) |
602 | if (kprobe_enabled) | 609 | | KPROBE_FLAG_DISABLED; |
603 | arch_arm_kprobe(ap); | ||
604 | } | 610 | } |
605 | return ret; | 611 | |
612 | copy_kprobe(ap, p); | ||
613 | return add_new_kprobe(ap, p); | ||
614 | } | ||
615 | |||
616 | /* Try to disable aggr_kprobe, and return 1 if succeeded.*/ | ||
617 | static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p) | ||
618 | { | ||
619 | struct kprobe *kp; | ||
620 | |||
621 | list_for_each_entry_rcu(kp, &p->list, list) { | ||
622 | if (!kprobe_disabled(kp)) | ||
623 | /* | ||
624 | * There is an active probe on the list. | ||
625 | * We can't disable aggr_kprobe. | ||
626 | */ | ||
627 | return 0; | ||
628 | } | ||
629 | p->flags |= KPROBE_FLAG_DISABLED; | ||
630 | return 1; | ||
606 | } | 631 | } |
607 | 632 | ||
608 | static int __kprobes in_kprobes_functions(unsigned long addr) | 633 | static int __kprobes in_kprobes_functions(unsigned long addr) |
@@ -663,7 +688,9 @@ int __kprobes register_kprobe(struct kprobe *p) | |||
663 | return -EINVAL; | 688 | return -EINVAL; |
664 | } | 689 | } |
665 | 690 | ||
666 | p->flags = 0; | 691 | /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ |
692 | p->flags &= KPROBE_FLAG_DISABLED; | ||
693 | |||
667 | /* | 694 | /* |
668 | * Check if are we probing a module. | 695 | * Check if are we probing a module. |
669 | */ | 696 | */ |
@@ -699,17 +726,20 @@ int __kprobes register_kprobe(struct kprobe *p) | |||
699 | goto out; | 726 | goto out; |
700 | } | 727 | } |
701 | 728 | ||
729 | mutex_lock(&text_mutex); | ||
702 | ret = arch_prepare_kprobe(p); | 730 | ret = arch_prepare_kprobe(p); |
703 | if (ret) | 731 | if (ret) |
704 | goto out; | 732 | goto out_unlock_text; |
705 | 733 | ||
706 | INIT_HLIST_NODE(&p->hlist); | 734 | INIT_HLIST_NODE(&p->hlist); |
707 | hlist_add_head_rcu(&p->hlist, | 735 | hlist_add_head_rcu(&p->hlist, |
708 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); | 736 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
709 | 737 | ||
710 | if (kprobe_enabled) | 738 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) |
711 | arch_arm_kprobe(p); | 739 | arch_arm_kprobe(p); |
712 | 740 | ||
741 | out_unlock_text: | ||
742 | mutex_unlock(&text_mutex); | ||
713 | out: | 743 | out: |
714 | mutex_unlock(&kprobe_mutex); | 744 | mutex_unlock(&kprobe_mutex); |
715 | 745 | ||
@@ -718,26 +748,39 @@ out: | |||
718 | 748 | ||
719 | return ret; | 749 | return ret; |
720 | } | 750 | } |
751 | EXPORT_SYMBOL_GPL(register_kprobe); | ||
721 | 752 | ||
722 | /* | 753 | /* Check passed kprobe is valid and return kprobe in kprobe_table. */ |
723 | * Unregister a kprobe without a scheduler synchronization. | 754 | static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p) |
724 | */ | ||
725 | static int __kprobes __unregister_kprobe_top(struct kprobe *p) | ||
726 | { | 755 | { |
727 | struct kprobe *old_p, *list_p; | 756 | struct kprobe *old_p, *list_p; |
728 | 757 | ||
729 | old_p = get_kprobe(p->addr); | 758 | old_p = get_kprobe(p->addr); |
730 | if (unlikely(!old_p)) | 759 | if (unlikely(!old_p)) |
731 | return -EINVAL; | 760 | return NULL; |
732 | 761 | ||
733 | if (p != old_p) { | 762 | if (p != old_p) { |
734 | list_for_each_entry_rcu(list_p, &old_p->list, list) | 763 | list_for_each_entry_rcu(list_p, &old_p->list, list) |
735 | if (list_p == p) | 764 | if (list_p == p) |
736 | /* kprobe p is a valid probe */ | 765 | /* kprobe p is a valid probe */ |
737 | goto valid_p; | 766 | goto valid; |
738 | return -EINVAL; | 767 | return NULL; |
739 | } | 768 | } |
740 | valid_p: | 769 | valid: |
770 | return old_p; | ||
771 | } | ||
772 | |||
773 | /* | ||
774 | * Unregister a kprobe without a scheduler synchronization. | ||
775 | */ | ||
776 | static int __kprobes __unregister_kprobe_top(struct kprobe *p) | ||
777 | { | ||
778 | struct kprobe *old_p, *list_p; | ||
779 | |||
780 | old_p = __get_valid_kprobe(p); | ||
781 | if (old_p == NULL) | ||
782 | return -EINVAL; | ||
783 | |||
741 | if (old_p == p || | 784 | if (old_p == p || |
742 | (old_p->pre_handler == aggr_pre_handler && | 785 | (old_p->pre_handler == aggr_pre_handler && |
743 | list_is_singular(&old_p->list))) { | 786 | list_is_singular(&old_p->list))) { |
@@ -746,8 +789,11 @@ valid_p: | |||
746 | * enabled and not gone - otherwise, the breakpoint would | 789 | * enabled and not gone - otherwise, the breakpoint would |
747 | * already have been removed. We save on flushing icache. | 790 | * already have been removed. We save on flushing icache. |
748 | */ | 791 | */ |
749 | if (kprobe_enabled && !kprobe_gone(old_p)) | 792 | if (!kprobes_all_disarmed && !kprobe_disabled(old_p)) { |
793 | mutex_lock(&text_mutex); | ||
750 | arch_disarm_kprobe(p); | 794 | arch_disarm_kprobe(p); |
795 | mutex_unlock(&text_mutex); | ||
796 | } | ||
751 | hlist_del_rcu(&old_p->hlist); | 797 | hlist_del_rcu(&old_p->hlist); |
752 | } else { | 798 | } else { |
753 | if (p->break_handler && !kprobe_gone(p)) | 799 | if (p->break_handler && !kprobe_gone(p)) |
@@ -761,6 +807,11 @@ valid_p: | |||
761 | } | 807 | } |
762 | noclean: | 808 | noclean: |
763 | list_del_rcu(&p->list); | 809 | list_del_rcu(&p->list); |
810 | if (!kprobe_disabled(old_p)) { | ||
811 | try_to_disable_aggr_kprobe(old_p); | ||
812 | if (!kprobes_all_disarmed && kprobe_disabled(old_p)) | ||
813 | arch_disarm_kprobe(old_p); | ||
814 | } | ||
764 | } | 815 | } |
765 | return 0; | 816 | return 0; |
766 | } | 817 | } |
@@ -796,11 +847,13 @@ int __kprobes register_kprobes(struct kprobe **kps, int num) | |||
796 | } | 847 | } |
797 | return ret; | 848 | return ret; |
798 | } | 849 | } |
850 | EXPORT_SYMBOL_GPL(register_kprobes); | ||
799 | 851 | ||
800 | void __kprobes unregister_kprobe(struct kprobe *p) | 852 | void __kprobes unregister_kprobe(struct kprobe *p) |
801 | { | 853 | { |
802 | unregister_kprobes(&p, 1); | 854 | unregister_kprobes(&p, 1); |
803 | } | 855 | } |
856 | EXPORT_SYMBOL_GPL(unregister_kprobe); | ||
804 | 857 | ||
805 | void __kprobes unregister_kprobes(struct kprobe **kps, int num) | 858 | void __kprobes unregister_kprobes(struct kprobe **kps, int num) |
806 | { | 859 | { |
@@ -819,6 +872,7 @@ void __kprobes unregister_kprobes(struct kprobe **kps, int num) | |||
819 | if (kps[i]->addr) | 872 | if (kps[i]->addr) |
820 | __unregister_kprobe_bottom(kps[i]); | 873 | __unregister_kprobe_bottom(kps[i]); |
821 | } | 874 | } |
875 | EXPORT_SYMBOL_GPL(unregister_kprobes); | ||
822 | 876 | ||
823 | static struct notifier_block kprobe_exceptions_nb = { | 877 | static struct notifier_block kprobe_exceptions_nb = { |
824 | .notifier_call = kprobe_exceptions_notify, | 878 | .notifier_call = kprobe_exceptions_notify, |
@@ -858,16 +912,19 @@ int __kprobes register_jprobes(struct jprobe **jps, int num) | |||
858 | } | 912 | } |
859 | return ret; | 913 | return ret; |
860 | } | 914 | } |
915 | EXPORT_SYMBOL_GPL(register_jprobes); | ||
861 | 916 | ||
862 | int __kprobes register_jprobe(struct jprobe *jp) | 917 | int __kprobes register_jprobe(struct jprobe *jp) |
863 | { | 918 | { |
864 | return register_jprobes(&jp, 1); | 919 | return register_jprobes(&jp, 1); |
865 | } | 920 | } |
921 | EXPORT_SYMBOL_GPL(register_jprobe); | ||
866 | 922 | ||
867 | void __kprobes unregister_jprobe(struct jprobe *jp) | 923 | void __kprobes unregister_jprobe(struct jprobe *jp) |
868 | { | 924 | { |
869 | unregister_jprobes(&jp, 1); | 925 | unregister_jprobes(&jp, 1); |
870 | } | 926 | } |
927 | EXPORT_SYMBOL_GPL(unregister_jprobe); | ||
871 | 928 | ||
872 | void __kprobes unregister_jprobes(struct jprobe **jps, int num) | 929 | void __kprobes unregister_jprobes(struct jprobe **jps, int num) |
873 | { | 930 | { |
@@ -887,6 +944,7 @@ void __kprobes unregister_jprobes(struct jprobe **jps, int num) | |||
887 | __unregister_kprobe_bottom(&jps[i]->kp); | 944 | __unregister_kprobe_bottom(&jps[i]->kp); |
888 | } | 945 | } |
889 | } | 946 | } |
947 | EXPORT_SYMBOL_GPL(unregister_jprobes); | ||
890 | 948 | ||
891 | #ifdef CONFIG_KRETPROBES | 949 | #ifdef CONFIG_KRETPROBES |
892 | /* | 950 | /* |
@@ -912,10 +970,8 @@ static int __kprobes pre_handler_kretprobe(struct kprobe *p, | |||
912 | ri->rp = rp; | 970 | ri->rp = rp; |
913 | ri->task = current; | 971 | ri->task = current; |
914 | 972 | ||
915 | if (rp->entry_handler && rp->entry_handler(ri, regs)) { | 973 | if (rp->entry_handler && rp->entry_handler(ri, regs)) |
916 | spin_unlock_irqrestore(&rp->lock, flags); | ||
917 | return 0; | 974 | return 0; |
918 | } | ||
919 | 975 | ||
920 | arch_prepare_kretprobe(ri, regs); | 976 | arch_prepare_kretprobe(ri, regs); |
921 | 977 | ||
@@ -982,6 +1038,7 @@ int __kprobes register_kretprobe(struct kretprobe *rp) | |||
982 | free_rp_inst(rp); | 1038 | free_rp_inst(rp); |
983 | return ret; | 1039 | return ret; |
984 | } | 1040 | } |
1041 | EXPORT_SYMBOL_GPL(register_kretprobe); | ||
985 | 1042 | ||
986 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) | 1043 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) |
987 | { | 1044 | { |
@@ -999,11 +1056,13 @@ int __kprobes register_kretprobes(struct kretprobe **rps, int num) | |||
999 | } | 1056 | } |
1000 | return ret; | 1057 | return ret; |
1001 | } | 1058 | } |
1059 | EXPORT_SYMBOL_GPL(register_kretprobes); | ||
1002 | 1060 | ||
1003 | void __kprobes unregister_kretprobe(struct kretprobe *rp) | 1061 | void __kprobes unregister_kretprobe(struct kretprobe *rp) |
1004 | { | 1062 | { |
1005 | unregister_kretprobes(&rp, 1); | 1063 | unregister_kretprobes(&rp, 1); |
1006 | } | 1064 | } |
1065 | EXPORT_SYMBOL_GPL(unregister_kretprobe); | ||
1007 | 1066 | ||
1008 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) | 1067 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) |
1009 | { | 1068 | { |
@@ -1025,24 +1084,30 @@ void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) | |||
1025 | } | 1084 | } |
1026 | } | 1085 | } |
1027 | } | 1086 | } |
1087 | EXPORT_SYMBOL_GPL(unregister_kretprobes); | ||
1028 | 1088 | ||
1029 | #else /* CONFIG_KRETPROBES */ | 1089 | #else /* CONFIG_KRETPROBES */ |
1030 | int __kprobes register_kretprobe(struct kretprobe *rp) | 1090 | int __kprobes register_kretprobe(struct kretprobe *rp) |
1031 | { | 1091 | { |
1032 | return -ENOSYS; | 1092 | return -ENOSYS; |
1033 | } | 1093 | } |
1094 | EXPORT_SYMBOL_GPL(register_kretprobe); | ||
1034 | 1095 | ||
1035 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) | 1096 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) |
1036 | { | 1097 | { |
1037 | return -ENOSYS; | 1098 | return -ENOSYS; |
1038 | } | 1099 | } |
1100 | EXPORT_SYMBOL_GPL(register_kretprobes); | ||
1101 | |||
1039 | void __kprobes unregister_kretprobe(struct kretprobe *rp) | 1102 | void __kprobes unregister_kretprobe(struct kretprobe *rp) |
1040 | { | 1103 | { |
1041 | } | 1104 | } |
1105 | EXPORT_SYMBOL_GPL(unregister_kretprobe); | ||
1042 | 1106 | ||
1043 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) | 1107 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) |
1044 | { | 1108 | { |
1045 | } | 1109 | } |
1110 | EXPORT_SYMBOL_GPL(unregister_kretprobes); | ||
1046 | 1111 | ||
1047 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, | 1112 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, |
1048 | struct pt_regs *regs) | 1113 | struct pt_regs *regs) |
@@ -1056,6 +1121,7 @@ static int __kprobes pre_handler_kretprobe(struct kprobe *p, | |||
1056 | static void __kprobes kill_kprobe(struct kprobe *p) | 1121 | static void __kprobes kill_kprobe(struct kprobe *p) |
1057 | { | 1122 | { |
1058 | struct kprobe *kp; | 1123 | struct kprobe *kp; |
1124 | |||
1059 | p->flags |= KPROBE_FLAG_GONE; | 1125 | p->flags |= KPROBE_FLAG_GONE; |
1060 | if (p->pre_handler == aggr_pre_handler) { | 1126 | if (p->pre_handler == aggr_pre_handler) { |
1061 | /* | 1127 | /* |
@@ -1168,8 +1234,8 @@ static int __init init_kprobes(void) | |||
1168 | } | 1234 | } |
1169 | } | 1235 | } |
1170 | 1236 | ||
1171 | /* By default, kprobes are enabled */ | 1237 | /* By default, kprobes are armed */ |
1172 | kprobe_enabled = true; | 1238 | kprobes_all_disarmed = false; |
1173 | 1239 | ||
1174 | err = arch_init_kprobes(); | 1240 | err = arch_init_kprobes(); |
1175 | if (!err) | 1241 | if (!err) |
@@ -1197,12 +1263,18 @@ static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, | |||
1197 | else | 1263 | else |
1198 | kprobe_type = "k"; | 1264 | kprobe_type = "k"; |
1199 | if (sym) | 1265 | if (sym) |
1200 | seq_printf(pi, "%p %s %s+0x%x %s %s\n", p->addr, kprobe_type, | 1266 | seq_printf(pi, "%p %s %s+0x%x %s %s%s\n", |
1201 | sym, offset, (modname ? modname : " "), | 1267 | p->addr, kprobe_type, sym, offset, |
1202 | (kprobe_gone(p) ? "[GONE]" : "")); | 1268 | (modname ? modname : " "), |
1269 | (kprobe_gone(p) ? "[GONE]" : ""), | ||
1270 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? | ||
1271 | "[DISABLED]" : "")); | ||
1203 | else | 1272 | else |
1204 | seq_printf(pi, "%p %s %p %s\n", p->addr, kprobe_type, p->addr, | 1273 | seq_printf(pi, "%p %s %p %s%s\n", |
1205 | (kprobe_gone(p) ? "[GONE]" : "")); | 1274 | p->addr, kprobe_type, p->addr, |
1275 | (kprobe_gone(p) ? "[GONE]" : ""), | ||
1276 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? | ||
1277 | "[DISABLED]" : "")); | ||
1206 | } | 1278 | } |
1207 | 1279 | ||
1208 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) | 1280 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
@@ -1267,7 +1339,72 @@ static struct file_operations debugfs_kprobes_operations = { | |||
1267 | .release = seq_release, | 1339 | .release = seq_release, |
1268 | }; | 1340 | }; |
1269 | 1341 | ||
1270 | static void __kprobes enable_all_kprobes(void) | 1342 | /* Disable one kprobe */ |
1343 | int __kprobes disable_kprobe(struct kprobe *kp) | ||
1344 | { | ||
1345 | int ret = 0; | ||
1346 | struct kprobe *p; | ||
1347 | |||
1348 | mutex_lock(&kprobe_mutex); | ||
1349 | |||
1350 | /* Check whether specified probe is valid. */ | ||
1351 | p = __get_valid_kprobe(kp); | ||
1352 | if (unlikely(p == NULL)) { | ||
1353 | ret = -EINVAL; | ||
1354 | goto out; | ||
1355 | } | ||
1356 | |||
1357 | /* If the probe is already disabled (or gone), just return */ | ||
1358 | if (kprobe_disabled(kp)) | ||
1359 | goto out; | ||
1360 | |||
1361 | kp->flags |= KPROBE_FLAG_DISABLED; | ||
1362 | if (p != kp) | ||
1363 | /* When kp != p, p is always enabled. */ | ||
1364 | try_to_disable_aggr_kprobe(p); | ||
1365 | |||
1366 | if (!kprobes_all_disarmed && kprobe_disabled(p)) | ||
1367 | arch_disarm_kprobe(p); | ||
1368 | out: | ||
1369 | mutex_unlock(&kprobe_mutex); | ||
1370 | return ret; | ||
1371 | } | ||
1372 | EXPORT_SYMBOL_GPL(disable_kprobe); | ||
1373 | |||
1374 | /* Enable one kprobe */ | ||
1375 | int __kprobes enable_kprobe(struct kprobe *kp) | ||
1376 | { | ||
1377 | int ret = 0; | ||
1378 | struct kprobe *p; | ||
1379 | |||
1380 | mutex_lock(&kprobe_mutex); | ||
1381 | |||
1382 | /* Check whether specified probe is valid. */ | ||
1383 | p = __get_valid_kprobe(kp); | ||
1384 | if (unlikely(p == NULL)) { | ||
1385 | ret = -EINVAL; | ||
1386 | goto out; | ||
1387 | } | ||
1388 | |||
1389 | if (kprobe_gone(kp)) { | ||
1390 | /* This kprobe has gone, we couldn't enable it. */ | ||
1391 | ret = -EINVAL; | ||
1392 | goto out; | ||
1393 | } | ||
1394 | |||
1395 | if (!kprobes_all_disarmed && kprobe_disabled(p)) | ||
1396 | arch_arm_kprobe(p); | ||
1397 | |||
1398 | p->flags &= ~KPROBE_FLAG_DISABLED; | ||
1399 | if (p != kp) | ||
1400 | kp->flags &= ~KPROBE_FLAG_DISABLED; | ||
1401 | out: | ||
1402 | mutex_unlock(&kprobe_mutex); | ||
1403 | return ret; | ||
1404 | } | ||
1405 | EXPORT_SYMBOL_GPL(enable_kprobe); | ||
1406 | |||
1407 | static void __kprobes arm_all_kprobes(void) | ||
1271 | { | 1408 | { |
1272 | struct hlist_head *head; | 1409 | struct hlist_head *head; |
1273 | struct hlist_node *node; | 1410 | struct hlist_node *node; |
@@ -1276,18 +1413,20 @@ static void __kprobes enable_all_kprobes(void) | |||
1276 | 1413 | ||
1277 | mutex_lock(&kprobe_mutex); | 1414 | mutex_lock(&kprobe_mutex); |
1278 | 1415 | ||
1279 | /* If kprobes are already enabled, just return */ | 1416 | /* If kprobes are armed, just return */ |
1280 | if (kprobe_enabled) | 1417 | if (!kprobes_all_disarmed) |
1281 | goto already_enabled; | 1418 | goto already_enabled; |
1282 | 1419 | ||
1420 | mutex_lock(&text_mutex); | ||
1283 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | 1421 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
1284 | head = &kprobe_table[i]; | 1422 | head = &kprobe_table[i]; |
1285 | hlist_for_each_entry_rcu(p, node, head, hlist) | 1423 | hlist_for_each_entry_rcu(p, node, head, hlist) |
1286 | if (!kprobe_gone(p)) | 1424 | if (!kprobe_disabled(p)) |
1287 | arch_arm_kprobe(p); | 1425 | arch_arm_kprobe(p); |
1288 | } | 1426 | } |
1427 | mutex_unlock(&text_mutex); | ||
1289 | 1428 | ||
1290 | kprobe_enabled = true; | 1429 | kprobes_all_disarmed = false; |
1291 | printk(KERN_INFO "Kprobes globally enabled\n"); | 1430 | printk(KERN_INFO "Kprobes globally enabled\n"); |
1292 | 1431 | ||
1293 | already_enabled: | 1432 | already_enabled: |
@@ -1295,7 +1434,7 @@ already_enabled: | |||
1295 | return; | 1434 | return; |
1296 | } | 1435 | } |
1297 | 1436 | ||
1298 | static void __kprobes disable_all_kprobes(void) | 1437 | static void __kprobes disarm_all_kprobes(void) |
1299 | { | 1438 | { |
1300 | struct hlist_head *head; | 1439 | struct hlist_head *head; |
1301 | struct hlist_node *node; | 1440 | struct hlist_node *node; |
@@ -1304,20 +1443,22 @@ static void __kprobes disable_all_kprobes(void) | |||
1304 | 1443 | ||
1305 | mutex_lock(&kprobe_mutex); | 1444 | mutex_lock(&kprobe_mutex); |
1306 | 1445 | ||
1307 | /* If kprobes are already disabled, just return */ | 1446 | /* If kprobes are already disarmed, just return */ |
1308 | if (!kprobe_enabled) | 1447 | if (kprobes_all_disarmed) |
1309 | goto already_disabled; | 1448 | goto already_disabled; |
1310 | 1449 | ||
1311 | kprobe_enabled = false; | 1450 | kprobes_all_disarmed = true; |
1312 | printk(KERN_INFO "Kprobes globally disabled\n"); | 1451 | printk(KERN_INFO "Kprobes globally disabled\n"); |
1452 | mutex_lock(&text_mutex); | ||
1313 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | 1453 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
1314 | head = &kprobe_table[i]; | 1454 | head = &kprobe_table[i]; |
1315 | hlist_for_each_entry_rcu(p, node, head, hlist) { | 1455 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
1316 | if (!arch_trampoline_kprobe(p) && !kprobe_gone(p)) | 1456 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) |
1317 | arch_disarm_kprobe(p); | 1457 | arch_disarm_kprobe(p); |
1318 | } | 1458 | } |
1319 | } | 1459 | } |
1320 | 1460 | ||
1461 | mutex_unlock(&text_mutex); | ||
1321 | mutex_unlock(&kprobe_mutex); | 1462 | mutex_unlock(&kprobe_mutex); |
1322 | /* Allow all currently running kprobes to complete */ | 1463 | /* Allow all currently running kprobes to complete */ |
1323 | synchronize_sched(); | 1464 | synchronize_sched(); |
@@ -1338,7 +1479,7 @@ static ssize_t read_enabled_file_bool(struct file *file, | |||
1338 | { | 1479 | { |
1339 | char buf[3]; | 1480 | char buf[3]; |
1340 | 1481 | ||
1341 | if (kprobe_enabled) | 1482 | if (!kprobes_all_disarmed) |
1342 | buf[0] = '1'; | 1483 | buf[0] = '1'; |
1343 | else | 1484 | else |
1344 | buf[0] = '0'; | 1485 | buf[0] = '0'; |
@@ -1361,12 +1502,12 @@ static ssize_t write_enabled_file_bool(struct file *file, | |||
1361 | case 'y': | 1502 | case 'y': |
1362 | case 'Y': | 1503 | case 'Y': |
1363 | case '1': | 1504 | case '1': |
1364 | enable_all_kprobes(); | 1505 | arm_all_kprobes(); |
1365 | break; | 1506 | break; |
1366 | case 'n': | 1507 | case 'n': |
1367 | case 'N': | 1508 | case 'N': |
1368 | case '0': | 1509 | case '0': |
1369 | disable_all_kprobes(); | 1510 | disarm_all_kprobes(); |
1370 | break; | 1511 | break; |
1371 | } | 1512 | } |
1372 | 1513 | ||
@@ -1409,16 +1550,5 @@ late_initcall(debugfs_kprobe_init); | |||
1409 | 1550 | ||
1410 | module_init(init_kprobes); | 1551 | module_init(init_kprobes); |
1411 | 1552 | ||
1412 | EXPORT_SYMBOL_GPL(register_kprobe); | 1553 | /* defined in arch/.../kernel/kprobes.c */ |
1413 | EXPORT_SYMBOL_GPL(unregister_kprobe); | ||
1414 | EXPORT_SYMBOL_GPL(register_kprobes); | ||
1415 | EXPORT_SYMBOL_GPL(unregister_kprobes); | ||
1416 | EXPORT_SYMBOL_GPL(register_jprobe); | ||
1417 | EXPORT_SYMBOL_GPL(unregister_jprobe); | ||
1418 | EXPORT_SYMBOL_GPL(register_jprobes); | ||
1419 | EXPORT_SYMBOL_GPL(unregister_jprobes); | ||
1420 | EXPORT_SYMBOL_GPL(jprobe_return); | 1554 | EXPORT_SYMBOL_GPL(jprobe_return); |
1421 | EXPORT_SYMBOL_GPL(register_kretprobe); | ||
1422 | EXPORT_SYMBOL_GPL(unregister_kretprobe); | ||
1423 | EXPORT_SYMBOL_GPL(register_kretprobes); | ||
1424 | EXPORT_SYMBOL_GPL(unregister_kretprobes); | ||