string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / kernel / kprobes.c
index a5e74dd..e5342a3 100644 (file)
@@ -90,6 +90,9 @@ static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  */
 static struct kprobe_blackpoint kprobe_blacklist[] = {
        {"preempt_schedule",},
+       {"native_get_debugreg",},
+       {"irq_entries_start",},
+       {"common_interrupt",},
        {NULL}    /* Terminator */
 };
 
@@ -103,7 +106,7 @@ static struct kprobe_blackpoint kprobe_blacklist[] = {
 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
 
 struct kprobe_insn_page {
-       struct hlist_node hlist;
+       struct list_head list;
        kprobe_opcode_t *insns;         /* Page of instruction slots */
        char slot_used[INSNS_PER_PAGE];
        int nused;
@@ -117,7 +120,7 @@ enum kprobe_slot_state {
 };
 
 static DEFINE_MUTEX(kprobe_insn_mutex);        /* Protects kprobe_insn_pages */
-static struct hlist_head kprobe_insn_pages;
+static LIST_HEAD(kprobe_insn_pages);
 static int kprobe_garbage_slots;
 static int collect_garbage_slots(void);
 
@@ -152,10 +155,9 @@ loop_end:
 static kprobe_opcode_t __kprobes *__get_insn_slot(void)
 {
        struct kprobe_insn_page *kip;
-       struct hlist_node *pos;
 
  retry:
-       hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
+       list_for_each_entry(kip, &kprobe_insn_pages, list) {
                if (kip->nused < INSNS_PER_PAGE) {
                        int i;
                        for (i = 0; i < INSNS_PER_PAGE; i++) {
@@ -189,8 +191,8 @@ static kprobe_opcode_t __kprobes *__get_insn_slot(void)
                kfree(kip);
                return NULL;
        }
-       INIT_HLIST_NODE(&kip->hlist);
-       hlist_add_head(&kip->hlist, &kprobe_insn_pages);
+       INIT_LIST_HEAD(&kip->list);
+       list_add(&kip->list, &kprobe_insn_pages);
        memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
        kip->slot_used[0] = SLOT_USED;
        kip->nused = 1;
@@ -219,12 +221,8 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
                 * so as not to have to set it up again the
                 * next time somebody inserts a probe.
                 */
-               hlist_del(&kip->hlist);
-               if (hlist_empty(&kprobe_insn_pages)) {
-                       INIT_HLIST_NODE(&kip->hlist);
-                       hlist_add_head(&kip->hlist,
-                                      &kprobe_insn_pages);
-               } else {
+               if (!list_is_singular(&kprobe_insn_pages)) {
+                       list_del(&kip->list);
                        module_free(NULL, kip->insns);
                        kfree(kip);
                }
@@ -235,18 +233,13 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
 
 static int __kprobes collect_garbage_slots(void)
 {
-       struct kprobe_insn_page *kip;
-       struct hlist_node *pos, *next;
-       int safety;
+       struct kprobe_insn_page *kip, *next;
 
        /* Ensure no-one is preepmted on the garbages */
-       mutex_unlock(&kprobe_insn_mutex);
-       safety = check_safety();
-       mutex_lock(&kprobe_insn_mutex);
-       if (safety != 0)
+       if (check_safety())
                return -EAGAIN;
 
-       hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
+       list_for_each_entry_safe(kip, next, &kprobe_insn_pages, list) {
                int i;
                if (kip->ngarbage == 0)
                        continue;
@@ -264,19 +257,17 @@ static int __kprobes collect_garbage_slots(void)
 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
 {
        struct kprobe_insn_page *kip;
-       struct hlist_node *pos;
 
        mutex_lock(&kprobe_insn_mutex);
-       hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
+       list_for_each_entry(kip, &kprobe_insn_pages, list) {
                if (kip->insns <= slot &&
                    slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
                        int i = (slot - kip->insns) / MAX_INSN_SIZE;
                        if (dirty) {
                                kip->slot_used[i] = SLOT_DIRTY;
                                kip->ngarbage++;
-                       } else {
+                       } else
                                collect_one_slot(kip, i);
-                       }
                        break;
                }
        }
@@ -319,6 +310,22 @@ struct kprobe __kprobes *get_kprobe(void *addr)
        return NULL;
 }
 
+/* Arm a kprobe with text_mutex */
+static void __kprobes arm_kprobe(struct kprobe *kp)
+{
+       mutex_lock(&text_mutex);
+       arch_arm_kprobe(kp);
+       mutex_unlock(&text_mutex);
+}
+
+/* Disarm a kprobe with text_mutex */
+static void __kprobes disarm_kprobe(struct kprobe *kp)
+{
+       mutex_lock(&text_mutex);
+       arch_disarm_kprobe(kp);
+       mutex_unlock(&text_mutex);
+}
+
 /*
  * Aggregate handlers for multiple kprobes support - these handlers
  * take care of invoking the individual kprobe handlers on p->list
@@ -538,7 +545,7 @@ static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
                ap->flags &= ~KPROBE_FLAG_DISABLED;
                if (!kprobes_all_disarmed)
                        /* Arm the breakpoint again. */
-                       arch_arm_kprobe(ap);
+                       arm_kprobe(ap);
        }
        return 0;
 }
@@ -669,6 +676,40 @@ static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
        return (kprobe_opcode_t *)(((char *)addr) + p->offset);
 }
 
+/* Check passed kprobe is valid and return kprobe in kprobe_table. */
+static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
+{
+       struct kprobe *old_p, *list_p;
+
+       old_p = get_kprobe(p->addr);
+       if (unlikely(!old_p))
+               return NULL;
+
+       if (p != old_p) {
+               list_for_each_entry_rcu(list_p, &old_p->list, list)
+                       if (list_p == p)
+                       /* kprobe p is a valid probe */
+                               goto valid;
+               return NULL;
+       }
+valid:
+       return old_p;
+}
+
+/* Return error if the kprobe is being re-registered */
+static inline int check_kprobe_rereg(struct kprobe *p)
+{
+       int ret = 0;
+       struct kprobe *old_p;
+
+       mutex_lock(&kprobe_mutex);
+       old_p = __get_valid_kprobe(p);
+       if (old_p)
+               ret = -EINVAL;
+       mutex_unlock(&kprobe_mutex);
+       return ret;
+}
+
 int __kprobes register_kprobe(struct kprobe *p)
 {
        int ret = 0;
@@ -681,8 +722,12 @@ int __kprobes register_kprobe(struct kprobe *p)
                return -EINVAL;
        p->addr = addr;
 
+       ret = check_kprobe_rereg(p);
+       if (ret)
+               return ret;
+
        preempt_disable();
-       if (!__kernel_text_address((unsigned long) p->addr) ||
+       if (!kernel_text_address((unsigned long) p->addr) ||
            in_kprobes_functions((unsigned long) p->addr)) {
                preempt_enable();
                return -EINVAL;
@@ -750,26 +795,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(register_kprobe);
 
-/* Check passed kprobe is valid and return kprobe in kprobe_table. */
-static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
-{
-       struct kprobe *old_p, *list_p;
-
-       old_p = get_kprobe(p->addr);
-       if (unlikely(!old_p))
-               return NULL;
-
-       if (p != old_p) {
-               list_for_each_entry_rcu(list_p, &old_p->list, list)
-                       if (list_p == p)
-                       /* kprobe p is a valid probe */
-                               goto valid;
-               return NULL;
-       }
-valid:
-       return old_p;
-}
-
 /*
  * Unregister a kprobe without a scheduler synchronization.
  */
@@ -789,11 +814,8 @@ static int __kprobes __unregister_kprobe_top(struct kprobe *p)
                 * enabled and not gone - otherwise, the breakpoint would
                 * already have been removed. We save on flushing icache.
                 */
-               if (!kprobes_all_disarmed && !kprobe_disabled(old_p)) {
-                       mutex_lock(&text_mutex);
-                       arch_disarm_kprobe(p);
-                       mutex_unlock(&text_mutex);
-               }
+               if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
+                       disarm_kprobe(p);
                hlist_del_rcu(&old_p->hlist);
        } else {
                if (p->break_handler && !kprobe_gone(p))
@@ -810,7 +832,7 @@ noclean:
                if (!kprobe_disabled(old_p)) {
                        try_to_disable_aggr_kprobe(old_p);
                        if (!kprobes_all_disarmed && kprobe_disabled(old_p))
-                               arch_disarm_kprobe(old_p);
+                               disarm_kprobe(old_p);
                }
        }
        return 0;
@@ -1013,9 +1035,9 @@ int __kprobes register_kretprobe(struct kretprobe *rp)
        /* Pre-allocate memory for max kretprobe instances */
        if (rp->maxactive <= 0) {
 #ifdef CONFIG_PREEMPT
-               rp->maxactive = max(10, 2 * NR_CPUS);
+               rp->maxactive = max(10, 2 * num_possible_cpus());
 #else
-               rp->maxactive = NR_CPUS;
+               rp->maxactive = num_possible_cpus();
 #endif
        }
        spin_lock_init(&rp->lock);
@@ -1140,6 +1162,13 @@ static void __kprobes kill_kprobe(struct kprobe *p)
        arch_remove_kprobe(p);
 }
 
+void __kprobes dump_kprobe(struct kprobe *kp)
+{
+       printk(KERN_WARNING "Dumping kprobe:\n");
+       printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
+              kp->symbol_name, kp->addr, kp->offset);
+}
+
 /* Module notifier call back, checking kprobes on the module */
 static int __kprobes kprobes_module_callback(struct notifier_block *nb,
                                             unsigned long val, void *data)
@@ -1320,7 +1349,7 @@ static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
        return 0;
 }
 
-static struct seq_operations kprobes_seq_ops = {
+static const struct seq_operations kprobes_seq_ops = {
        .start = kprobe_seq_start,
        .next  = kprobe_seq_next,
        .stop  = kprobe_seq_stop,
@@ -1332,7 +1361,7 @@ static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
        return seq_open(filp, &kprobes_seq_ops);
 }
 
-static struct file_operations debugfs_kprobes_operations = {
+static const struct file_operations debugfs_kprobes_operations = {
        .open           = kprobes_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
@@ -1364,7 +1393,7 @@ int __kprobes disable_kprobe(struct kprobe *kp)
                try_to_disable_aggr_kprobe(p);
 
        if (!kprobes_all_disarmed && kprobe_disabled(p))
-               arch_disarm_kprobe(p);
+               disarm_kprobe(p);
 out:
        mutex_unlock(&kprobe_mutex);
        return ret;
@@ -1393,7 +1422,7 @@ int __kprobes enable_kprobe(struct kprobe *kp)
        }
 
        if (!kprobes_all_disarmed && kprobe_disabled(p))
-               arch_arm_kprobe(p);
+               arm_kprobe(p);
 
        p->flags &= ~KPROBE_FLAG_DISABLED;
        if (p != kp)
@@ -1514,7 +1543,7 @@ static ssize_t write_enabled_file_bool(struct file *file,
        return count;
 }
 
-static struct file_operations fops_kp = {
+static const struct file_operations fops_kp = {
        .read =         read_enabled_file_bool,
        .write =        write_enabled_file_bool,
 };