Merge ../linux-2.6-watchdog-mm
[pandora-kernel.git] / kernel / kprobes.c
index 64aab08..6fcf8dd 100644 (file)
@@ -37,6 +37,8 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/moduleloader.h>
+#include <linux/kallsyms.h>
+#include <linux/freezer.h>
 #include <asm-generic/sections.h>
 #include <asm/cacheflush.h>
 #include <asm/errno.h>
 #define KPROBE_HASH_BITS 6
 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
 
+
+/*
+ * Some oddball architectures like 64bit powerpc have function descriptors
+ * so this must be overridable.
+ */
+#ifndef kprobe_lookup_name
+#define kprobe_lookup_name(name, addr) \
+       addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
+#endif
+
 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
 static atomic_t kprobe_count;
@@ -72,9 +84,42 @@ struct kprobe_insn_page {
        kprobe_opcode_t *insns;         /* Page of instruction slots */
        char slot_used[INSNS_PER_PAGE];
        int nused;
+       int ngarbage;
+};
+
+enum kprobe_slot_state {
+       SLOT_CLEAN = 0,
+       SLOT_DIRTY = 1,
+       SLOT_USED = 2,
 };
 
 static struct hlist_head kprobe_insn_pages;
+static int kprobe_garbage_slots;
+static int collect_garbage_slots(void);
+
+static int __kprobes check_safety(void)
+{
+       int ret = 0;
+#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
+       ret = freeze_processes();
+       if (ret == 0) {
+               struct task_struct *p, *q;
+               do_each_thread(p, q) {
+                       if (p != current && p->state == TASK_RUNNING &&
+                           p->pid != 0) {
+                               printk("Check failed: %s is running\n",p->comm);
+                               ret = -1;
+                               goto loop_end;
+                       }
+               } while_each_thread(p, q);
+       }
+loop_end:
+       thaw_processes();
+#else
+       synchronize_sched();
+#endif
+       return ret;
+}
 
 /**
  * get_insn_slot() - Find a slot on an executable page for an instruction.
@@ -85,13 +130,14 @@ kprobe_opcode_t __kprobes *get_insn_slot(void)
        struct kprobe_insn_page *kip;
        struct hlist_node *pos;
 
+      retry:
        hlist_for_each(pos, &kprobe_insn_pages) {
                kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
                if (kip->nused < INSNS_PER_PAGE) {
                        int i;
                        for (i = 0; i < INSNS_PER_PAGE; i++) {
-                               if (!kip->slot_used[i]) {
-                                       kip->slot_used[i] = 1;
+                               if (kip->slot_used[i] == SLOT_CLEAN) {
+                                       kip->slot_used[i] = SLOT_USED;
                                        kip->nused++;
                                        return kip->insns + (i * MAX_INSN_SIZE);
                                }
@@ -101,7 +147,11 @@ kprobe_opcode_t __kprobes *get_insn_slot(void)
                }
        }
 
-       /* All out of space.  Need to allocate a new page. Use slot 0.*/
+       /* If there are any garbage slots, collect it and try again. */
+       if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
+               goto retry;
+       }
+       /* All out of space.  Need to allocate a new page. Use slot 0. */
        kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
        if (!kip) {
                return NULL;
@@ -119,13 +169,65 @@ kprobe_opcode_t __kprobes *get_insn_slot(void)
        }
        INIT_HLIST_NODE(&kip->hlist);
        hlist_add_head(&kip->hlist, &kprobe_insn_pages);
-       memset(kip->slot_used, 0, INSNS_PER_PAGE);
-       kip->slot_used[0] = 1;
+       memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
+       kip->slot_used[0] = SLOT_USED;
        kip->nused = 1;
+       kip->ngarbage = 0;
        return kip->insns;
 }
 
-void __kprobes free_insn_slot(kprobe_opcode_t *slot)
+/* Return 1 if all garbages are collected, otherwise 0. */
+static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
+{
+       kip->slot_used[idx] = SLOT_CLEAN;
+       kip->nused--;
+       if (kip->nused == 0) {
+               /*
+                * Page is no longer in use.  Free it unless
+                * it's the last one.  We keep the last one
+                * 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 {
+                       module_free(NULL, kip->insns);
+                       kfree(kip);
+               }
+               return 1;
+       }
+       return 0;
+}
+
+static int __kprobes collect_garbage_slots(void)
+{
+       struct kprobe_insn_page *kip;
+       struct hlist_node *pos, *next;
+
+       /* Ensure no-one is preepmted on the garbages */
+       if (check_safety() != 0)
+               return -EAGAIN;
+
+       hlist_for_each_safe(pos, next, &kprobe_insn_pages) {
+               int i;
+               kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
+               if (kip->ngarbage == 0)
+                       continue;
+               kip->ngarbage = 0;      /* we will collect all garbages */
+               for (i = 0; i < INSNS_PER_PAGE; i++) {
+                       if (kip->slot_used[i] == SLOT_DIRTY &&
+                           collect_one_slot(kip, i))
+                               break;
+               }
+       }
+       kprobe_garbage_slots = 0;
+       return 0;
+}
+
+void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
 {
        struct kprobe_insn_page *kip;
        struct hlist_node *pos;
@@ -135,28 +237,18 @@ void __kprobes free_insn_slot(kprobe_opcode_t *slot)
                if (kip->insns <= slot &&
                    slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
                        int i = (slot - kip->insns) / MAX_INSN_SIZE;
-                       kip->slot_used[i] = 0;
-                       kip->nused--;
-                       if (kip->nused == 0) {
-                               /*
-                                * Page is no longer in use.  Free it unless
-                                * it's the last one.  We keep the last one
-                                * 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 {
-                                       module_free(NULL, kip->insns);
-                                       kfree(kip);
-                               }
+                       if (dirty) {
+                               kip->slot_used[i] = SLOT_DIRTY;
+                               kip->ngarbage++;
+                       } else {
+                               collect_one_slot(kip, i);
                        }
-                       return;
+                       break;
                }
        }
+       if (dirty && (++kprobe_garbage_slots > INSNS_PER_PAGE)) {
+               collect_garbage_slots();
+       }
 }
 #endif
 
@@ -308,7 +400,8 @@ void __kprobes add_rp_inst(struct kretprobe_instance *ri)
 }
 
 /* Called with kretprobe_lock held */
-void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
+void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
+                               struct hlist_head *head)
 {
        /* remove rp inst off the rprobe_inst_table */
        hlist_del(&ri->hlist);
@@ -320,7 +413,7 @@ void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
                hlist_add_head(&ri->uflist, &ri->rp->free_instances);
        } else
                /* Unregistering */
-               kfree(ri);
+               hlist_add_head(&ri->hlist, head);
 }
 
 struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
@@ -336,18 +429,24 @@ struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  */
 void __kprobes kprobe_flush_task(struct task_struct *tk)
 {
-        struct kretprobe_instance *ri;
-        struct hlist_head *head;
+       struct kretprobe_instance *ri;
+       struct hlist_head *head, empty_rp;
        struct hlist_node *node, *tmp;
        unsigned long flags = 0;
 
+       INIT_HLIST_HEAD(&empty_rp);
        spin_lock_irqsave(&kretprobe_lock, flags);
-        head = kretprobe_inst_table_head(tk);
-        hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
-                if (ri->task == tk)
-                        recycle_rp_inst(ri);
-        }
+       head = kretprobe_inst_table_head(tk);
+       hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
+               if (ri->task == tk)
+                       recycle_rp_inst(ri, &empty_rp);
+       }
        spin_unlock_irqrestore(&kretprobe_lock, flags);
+
+       hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
+               hlist_del(&ri->hlist);
+               kfree(ri);
+       }
 }
 
 static inline void free_rp_inst(struct kretprobe *rp)
@@ -393,6 +492,7 @@ static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
 {
        copy_kprobe(p, ap);
+       flush_insn_slot(ap);
        ap->addr = p->addr;
        ap->pre_handler = aggr_pre_handler;
        ap->fault_handler = aggr_fault_handler;
@@ -446,6 +546,21 @@ static int __kprobes __register_kprobe(struct kprobe *p,
        struct kprobe *old_p;
        struct module *probed_mod;
 
+       /*
+        * If we have a symbol_name argument look it up,
+        * and add it to the address.  That way the addr
+        * field can either be global or relative to a symbol.
+        */
+       if (p->symbol_name) {
+               if (p->addr)
+                       return -EINVAL;
+               kprobe_lookup_name(p->symbol_name, p->addr);
+       }
+
+       if (!p->addr)
+               return -EINVAL;
+       p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
+
        if ((!kernel_text_address((unsigned long) p->addr)) ||
                in_kprobes_functions((unsigned long) p->addr))
                return -EINVAL;
@@ -487,7 +602,7 @@ static int __kprobes __register_kprobe(struct kprobe *p,
                                (ARCH_INACTIVE_KPROBE_COUNT + 1))
                register_page_fault_notifier(&kprobe_page_fault_nb);
 
-       arch_arm_kprobe(p);
+       arch_arm_kprobe(p);
 
 out:
        mutex_unlock(&kprobe_mutex);