hw-breakpoints: Simplify error handling in breakpoint creation requests
[linux-2.6.git] / kernel / hw_breakpoint.c
index c1f64e6..dd3fb4a 100644 (file)
  *
  * Copyright (C) 2007 Alan Stern
  * Copyright (C) IBM Corporation, 2009
+ * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
+ *
+ * Thanks to Ingo Molnar for his many suggestions.
+ *
+ * Authors: Alan Stern <stern@rowland.harvard.edu>
+ *          K.Prasad <prasad@linux.vnet.ibm.com>
+ *          Frederic Weisbecker <fweisbec@gmail.com>
  */
 
 /*
 #include <linux/init.h>
 #include <linux/smp.h>
 
-#include <asm/hw_breakpoint.h>
-#include <asm/processor.h>
+#include <linux/hw_breakpoint.h>
 
-#ifdef CONFIG_X86
-#include <asm/debugreg.h>
-#endif
 /*
- * Spinlock that protects all (un)register operations over kernel/user-space
- * breakpoint requests
+ * Constraints data
  */
-static DEFINE_SPINLOCK(hw_breakpoint_lock);
 
-/* Array of kernel-space breakpoint structures */
-struct hw_breakpoint *hbp_kernel[HBP_NUM];
+/* Number of pinned cpu breakpoints in a cpu */
+static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned);
 
-/*
- * Per-processor copy of hbp_kernel[]. Used only when hbp_kernel is being
- * modified but we need the older copy to handle any hbp exceptions. It will
- * sync with hbp_kernel[] value after updation is done through IPIs.
- */
-DEFINE_PER_CPU(struct hw_breakpoint*, this_hbp_kernel[HBP_NUM]);
+/* Number of pinned task breakpoints in a cpu */
+static DEFINE_PER_CPU(unsigned int, task_bp_pinned[HBP_NUM]);
 
-/*
- * Kernel breakpoints grow downwards, starting from HBP_NUM
- * 'hbp_kernel_pos' denotes lowest numbered breakpoint register occupied for
- * kernel-space request. We will initialise it here and not in an __init
- * routine because load_debug_registers(), which uses this variable can be
- * called very early during CPU initialisation.
- */
-unsigned int hbp_kernel_pos = HBP_NUM;
+/* Number of non-pinned cpu/task breakpoints in a cpu */
+static DEFINE_PER_CPU(unsigned int, nr_bp_flexible);
+
+/* Gather the number of total pinned and un-pinned bp in a cpuset */
+struct bp_busy_slots {
+       unsigned int pinned;
+       unsigned int flexible;
+};
+
+/* Serialize accesses to the above constraints */
+static DEFINE_MUTEX(nr_bp_mutex);
 
 /*
- * An array containing refcount of threads using a given bkpt register
- * Accesses are synchronised by acquiring hw_breakpoint_lock
+ * Report the maximum number of pinned breakpoints a task
+ * have in this cpu
  */
-unsigned int hbp_user_refcount[HBP_NUM];
+static unsigned int max_task_bp_pinned(int cpu)
+{
+       int i;
+       unsigned int *tsk_pinned = per_cpu(task_bp_pinned, cpu);
+
+       for (i = HBP_NUM -1; i >= 0; i--) {
+               if (tsk_pinned[i] > 0)
+                       return i + 1;
+       }
+
+       return 0;
+}
 
 /*
- * Load the debug registers during startup of a CPU.
+ * Report the number of pinned/un-pinned breakpoints we have in
+ * a given cpu (cpu > -1) or in all of them (cpu = -1).
  */
-void load_debug_registers(void)
+static void fetch_bp_busy_slots(struct bp_busy_slots *slots, int cpu)
 {
-       unsigned long flags;
-       struct task_struct *tsk = current;
+       if (cpu >= 0) {
+               slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
+               slots->pinned += max_task_bp_pinned(cpu);
+               slots->flexible = per_cpu(nr_bp_flexible, cpu);
+
+               return;
+       }
 
-       spin_lock_bh(&hw_breakpoint_lock);
+       for_each_online_cpu(cpu) {
+               unsigned int nr;
 
-       /* Prevent IPIs for new kernel breakpoint updates */
-       local_irq_save(flags);
-       arch_update_kernel_hw_breakpoint(NULL);
-       local_irq_restore(flags);
+               nr = per_cpu(nr_cpu_bp_pinned, cpu);
+               nr += max_task_bp_pinned(cpu);
 
-       if (test_tsk_thread_flag(tsk, TIF_DEBUG))
-               arch_install_thread_hw_breakpoint(tsk);
+               if (nr > slots->pinned)
+                       slots->pinned = nr;
 
-       spin_unlock_bh(&hw_breakpoint_lock);
+               nr = per_cpu(nr_bp_flexible, cpu);
+
+               if (nr > slots->flexible)
+                       slots->flexible = nr;
+       }
 }
 
 /*
- * Erase all the hardware breakpoint info associated with a thread.
- *
- * If tsk != current then tsk must not be usable (for example, a
- * child being cleaned up from a failed fork).
+ * Add a pinned breakpoint for the given task in our constraint table
  */
-void flush_thread_hw_breakpoint(struct task_struct *tsk)
+static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
 {
-       int i;
-       struct thread_struct *thread = &(tsk->thread);
+       int count = 0;
+       struct perf_event *bp;
+       struct perf_event_context *ctx = tsk->perf_event_ctxp;
+       unsigned int *task_bp_pinned;
+       struct list_head *list;
+       unsigned long flags;
+
+       if (WARN_ONCE(!ctx, "No perf context for this task"))
+               return;
 
-       spin_lock_bh(&hw_breakpoint_lock);
+       list = &ctx->event_list;
 
-       /* The thread no longer has any breakpoints associated with it */
-       clear_tsk_thread_flag(tsk, TIF_DEBUG);
-       for (i = 0; i < HBP_NUM; i++) {
-               if (thread->hbp[i]) {
-                       hbp_user_refcount[i]--;
-                       kfree(thread->hbp[i]);
-                       thread->hbp[i] = NULL;
-               }
+       spin_lock_irqsave(&ctx->lock, flags);
+
+       /*
+        * The current breakpoint counter is not included in the list
+        * at the open() callback time
+        */
+       list_for_each_entry(bp, list, event_entry) {
+               if (bp->attr.type == PERF_TYPE_BREAKPOINT)
+                       count++;
        }
 
-       arch_flush_thread_hw_breakpoint(tsk);
+       spin_unlock_irqrestore(&ctx->lock, flags);
 
-       /* Actually uninstall the breakpoints if necessary */
-       if (tsk == current)
-               arch_uninstall_thread_hw_breakpoint();
-       spin_unlock_bh(&hw_breakpoint_lock);
+       if (WARN_ONCE(count < 0, "No breakpoint counter found in the counter list"))
+               return;
+
+       task_bp_pinned = per_cpu(task_bp_pinned, cpu);
+       if (enable) {
+               task_bp_pinned[count]++;
+               if (count > 0)
+                       task_bp_pinned[count-1]--;
+       } else {
+               task_bp_pinned[count]--;
+               if (count > 0)
+                       task_bp_pinned[count-1]++;
+       }
 }
 
 /*
- * Copy the hardware breakpoint info from a thread to its cloned child.
+ * Add/remove the given breakpoint in our constraint table
  */
-int copy_thread_hw_breakpoint(struct task_struct *tsk,
-               struct task_struct *child, unsigned long clone_flags)
+static void toggle_bp_slot(struct perf_event *bp, bool enable)
 {
-       /*
-        * We will assume that breakpoint settings are not inherited
-        * and the child starts out with no debug registers set.
-        * But what about CLONE_PTRACE?
-        */
-       clear_tsk_thread_flag(child, TIF_DEBUG);
+       int cpu = bp->cpu;
+       struct task_struct *tsk = bp->ctx->task;
+
+       /* Pinned counter task profiling */
+       if (tsk) {
+               if (cpu >= 0) {
+                       toggle_bp_task_slot(tsk, cpu, enable);
+                       return;
+               }
 
-       /* We will call flush routine since the debugregs are not inherited */
-       arch_flush_thread_hw_breakpoint(child);
+               for_each_online_cpu(cpu)
+                       toggle_bp_task_slot(tsk, cpu, enable);
+               return;
+       }
 
-       return 0;
+       /* Pinned counter cpu profiling */
+       if (enable)
+               per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
+       else
+               per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
 }
 
-static int __register_user_hw_breakpoint(int pos, struct task_struct *tsk,
-                                       struct hw_breakpoint *bp)
+/*
+ * Contraints to check before allowing this new breakpoint counter:
+ *
+ *  == Non-pinned counter == (Considered as pinned for now)
+ *
+ *   - If attached to a single cpu, check:
+ *
+ *       (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
+ *           + max(per_cpu(task_bp_pinned, cpu)))) < HBP_NUM
+ *
+ *       -> If there are already non-pinned counters in this cpu, it means
+ *          there is already a free slot for them.
+ *          Otherwise, we check that the maximum number of per task
+ *          breakpoints (for this cpu) plus the number of per cpu breakpoint
+ *          (for this cpu) doesn't cover every registers.
+ *
+ *   - If attached to every cpus, check:
+ *
+ *       (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
+ *           + max(per_cpu(task_bp_pinned, *)))) < HBP_NUM
+ *
+ *       -> This is roughly the same, except we check the number of per cpu
+ *          bp for every cpu and we keep the max one. Same for the per tasks
+ *          breakpoints.
+ *
+ *
+ * == Pinned counter ==
+ *
+ *   - If attached to a single cpu, check:
+ *
+ *       ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
+ *            + max(per_cpu(task_bp_pinned, cpu))) < HBP_NUM
+ *
+ *       -> Same checks as before. But now the nr_bp_flexible, if any, must keep
+ *          one register at least (or they will never be fed).
+ *
+ *   - If attached to every cpus, check:
+ *
+ *       ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
+ *            + max(per_cpu(task_bp_pinned, *))) < HBP_NUM
+ */
+int reserve_bp_slot(struct perf_event *bp)
 {
-       struct thread_struct *thread = &(tsk->thread);
-       int rc;
+       struct bp_busy_slots slots = {0};
+       int ret = 0;
 
-       /* Do not overcommit. Fail if kernel has used the hbp registers */
-       if (pos >= hbp_kernel_pos)
-               return -ENOSPC;
+       mutex_lock(&nr_bp_mutex);
 
-       rc = arch_validate_hwbkpt_settings(bp, tsk);
-       if (rc)
-               return rc;
+       fetch_bp_busy_slots(&slots, bp->cpu);
 
-       thread->hbp[pos] = bp;
-       hbp_user_refcount[pos]++;
+       /* Flexible counters need to keep at least one slot */
+       if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
+               ret = -ENOSPC;
+               goto end;
+       }
 
-       arch_update_user_hw_breakpoint(pos, tsk);
-       /*
-        * Does it need to be installed right now?
-        * Otherwise it will get installed the next time tsk runs
-        */
-       if (tsk == current)
-               arch_install_thread_hw_breakpoint(tsk);
+       toggle_bp_slot(bp, true);
+
+end:
+       mutex_unlock(&nr_bp_mutex);
 
-       return rc;
+       return ret;
 }
 
-/*
- * Modify the address of a hbp register already in use by the task
- * Do not invoke this in-lieu of a __unregister_user_hw_breakpoint()
- */
-static int __modify_user_hw_breakpoint(int pos, struct task_struct *tsk,
-                                       struct hw_breakpoint *bp)
+void release_bp_slot(struct perf_event *bp)
 {
-       struct thread_struct *thread = &(tsk->thread);
+       mutex_lock(&nr_bp_mutex);
+
+       toggle_bp_slot(bp, false);
+
+       mutex_unlock(&nr_bp_mutex);
+}
+
 
-       if ((pos >= hbp_kernel_pos) || (arch_validate_hwbkpt_settings(bp, tsk)))
-               return -EINVAL;
+int __register_perf_hw_breakpoint(struct perf_event *bp)
+{
+       int ret;
 
-       if (thread->hbp[pos] == NULL)
-               return -EINVAL;
+       ret = reserve_bp_slot(bp);
+       if (ret)
+               return ret;
 
-       thread->hbp[pos] = bp;
        /*
-        * 'pos' must be that of a hbp register already used by 'tsk'
-        * Otherwise arch_modify_user_hw_breakpoint() will fail
+        * Ptrace breakpoints can be temporary perf events only
+        * meant to reserve a slot. In this case, it is created disabled and
+        * we don't want to check the params right now (as we put a null addr)
+        * But perf tools create events as disabled and we want to check
+        * the params for them.
+        * This is a quick hack that will be removed soon, once we remove
+        * the tmp breakpoints from ptrace
         */
-       arch_update_user_hw_breakpoint(pos, tsk);
+       if (!bp->attr.disabled || bp->callback == perf_bp_event)
+               ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
+
+       return ret;
+}
 
-       if (tsk == current)
-               arch_install_thread_hw_breakpoint(tsk);
+int register_perf_hw_breakpoint(struct perf_event *bp)
+{
+       bp->callback = perf_bp_event;
 
-       return 0;
+       return __register_perf_hw_breakpoint(bp);
 }
 
-static void __unregister_user_hw_breakpoint(int pos, struct task_struct *tsk)
+/*
+ * Register a breakpoint bound to a task and a given cpu.
+ * If cpu is -1, the breakpoint is active for the task in every cpu
+ * If the task is -1, the breakpoint is active for every tasks in the given
+ * cpu.
+ */
+static struct perf_event *
+register_user_hw_breakpoint_cpu(unsigned long addr,
+                               int len,
+                               int type,
+                               perf_callback_t triggered,
+                               pid_t pid,
+                               int cpu,
+                               bool active)
 {
-       hbp_user_refcount[pos]--;
-       tsk->thread.hbp[pos] = NULL;
+       struct perf_event_attr *attr;
+       struct perf_event *bp;
+
+       attr = kzalloc(sizeof(*attr), GFP_KERNEL);
+       if (!attr)
+               return ERR_PTR(-ENOMEM);
+
+       attr->type = PERF_TYPE_BREAKPOINT;
+       attr->size = sizeof(*attr);
+       attr->bp_addr = addr;
+       attr->bp_len = len;
+       attr->bp_type = type;
+       /*
+        * Such breakpoints are used by debuggers to trigger signals when
+        * we hit the excepted memory op. We can't miss such events, they
+        * must be pinned.
+        */
+       attr->pinned = 1;
 
-       arch_update_user_hw_breakpoint(pos, tsk);
+       if (!active)
+               attr->disabled = 1;
 
-       if (tsk == current)
-               arch_install_thread_hw_breakpoint(tsk);
+       bp = perf_event_create_kernel_counter(attr, cpu, pid, triggered);
+       kfree(attr);
+
+       return bp;
 }
 
 /**
  * register_user_hw_breakpoint - register a hardware breakpoint for user space
+ * @addr: is the memory address that triggers the breakpoint
+ * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
+ * @type: the type of the access to the memory (read/write/exec)
+ * @triggered: callback to trigger when we hit the breakpoint
  * @tsk: pointer to 'task_struct' of the process to which the address belongs
- * @bp: the breakpoint structure to register
- *
- * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
- * @bp->triggered must be set properly before invocation
+ * @active: should we activate it while registering it
  *
  */
-int register_user_hw_breakpoint(struct task_struct *tsk,
-                                       struct hw_breakpoint *bp)
+struct perf_event *
+register_user_hw_breakpoint(unsigned long addr,
+                           int len,
+                           int type,
+                           perf_callback_t triggered,
+                           struct task_struct *tsk,
+                           bool active)
 {
-       struct thread_struct *thread = &(tsk->thread);
-       int i, rc = -ENOSPC;
-
-       spin_lock_bh(&hw_breakpoint_lock);
-
-       for (i = 0; i < hbp_kernel_pos; i++) {
-               if (!thread->hbp[i]) {
-                       rc = __register_user_hw_breakpoint(i, tsk, bp);
-                       break;
-               }
-       }
-       if (!rc)
-               set_tsk_thread_flag(tsk, TIF_DEBUG);
-
-       spin_unlock_bh(&hw_breakpoint_lock);
-       return rc;
+       return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
+                                              tsk->pid, -1, active);
 }
 EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
 
 /**
  * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
+ * @bp: the breakpoint structure to modify
+ * @addr: is the memory address that triggers the breakpoint
+ * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
+ * @type: the type of the access to the memory (read/write/exec)
+ * @triggered: callback to trigger when we hit the breakpoint
  * @tsk: pointer to 'task_struct' of the process to which the address belongs
- * @bp: the breakpoint structure to unregister
- *
+ * @active: should we activate it while registering it
  */
-int modify_user_hw_breakpoint(struct task_struct *tsk, struct hw_breakpoint *bp)
+struct perf_event *
+modify_user_hw_breakpoint(struct perf_event *bp,
+                         unsigned long addr,
+                         int len,
+                         int type,
+                         perf_callback_t triggered,
+                         struct task_struct *tsk,
+                         bool active)
 {
-       struct thread_struct *thread = &(tsk->thread);
-       int i, ret = -ENOENT;
+       /*
+        * FIXME: do it without unregistering
+        * - We don't want to lose our slot
+        * - If the new bp is incorrect, don't lose the older one
+        */
+       unregister_hw_breakpoint(bp);
 
-       spin_lock_bh(&hw_breakpoint_lock);
-       for (i = 0; i < hbp_kernel_pos; i++) {
-               if (bp == thread->hbp[i]) {
-                       ret = __modify_user_hw_breakpoint(i, tsk, bp);
-                       break;
-               }
-       }
-       spin_unlock_bh(&hw_breakpoint_lock);
-       return ret;
+       return register_user_hw_breakpoint(addr, len, type, triggered,
+                                          tsk, active);
 }
 EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
 
 /**
- * unregister_user_hw_breakpoint - unregister a user-space hardware breakpoint
- * @tsk: pointer to 'task_struct' of the process to which the address belongs
+ * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  * @bp: the breakpoint structure to unregister
- *
  */
-void unregister_user_hw_breakpoint(struct task_struct *tsk,
-                                               struct hw_breakpoint *bp)
+void unregister_hw_breakpoint(struct perf_event *bp)
 {
-       struct thread_struct *thread = &(tsk->thread);
-       int i, pos = -1, hbp_counter = 0;
-
-       spin_lock_bh(&hw_breakpoint_lock);
-       for (i = 0; i < hbp_kernel_pos; i++) {
-               if (thread->hbp[i])
-                       hbp_counter++;
-               if (bp == thread->hbp[i])
-                       pos = i;
-       }
-       if (pos >= 0) {
-               __unregister_user_hw_breakpoint(pos, tsk);
-               hbp_counter--;
-       }
-       if (!hbp_counter)
-               clear_tsk_thread_flag(tsk, TIF_DEBUG);
-
-       spin_unlock_bh(&hw_breakpoint_lock);
+       if (!bp)
+               return;
+       perf_event_release_kernel(bp);
+}
+EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
+
+static struct perf_event *
+register_kernel_hw_breakpoint_cpu(unsigned long addr,
+                                 int len,
+                                 int type,
+                                 perf_callback_t triggered,
+                                 int cpu,
+                                 bool active)
+{
+       return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
+                                              -1, cpu, active);
 }
-EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);
 
 /**
- * register_kernel_hw_breakpoint - register a hardware breakpoint for kernel space
- * @bp: the breakpoint structure to register
- *
- * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
- * @bp->triggered must be set properly before invocation
+ * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
+ * @addr: is the memory address that triggers the breakpoint
+ * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
+ * @type: the type of the access to the memory (read/write/exec)
+ * @triggered: callback to trigger when we hit the breakpoint
+ * @active: should we activate it while registering it
  *
+ * @return a set of per_cpu pointers to perf events
  */
-int register_kernel_hw_breakpoint(struct hw_breakpoint *bp)
+struct perf_event **
+register_wide_hw_breakpoint(unsigned long addr,
+                           int len,
+                           int type,
+                           perf_callback_t triggered,
+                           bool active)
 {
-       int rc;
+       struct perf_event **cpu_events, **pevent, *bp;
+       long err;
+       int cpu;
 
-       rc = arch_validate_hwbkpt_settings(bp, NULL);
-       if (rc)
-               return rc;
+       cpu_events = alloc_percpu(typeof(*cpu_events));
+       if (!cpu_events)
+               return ERR_PTR(-ENOMEM);
 
-       spin_lock_bh(&hw_breakpoint_lock);
+       for_each_possible_cpu(cpu) {
+               pevent = per_cpu_ptr(cpu_events, cpu);
+               bp = register_kernel_hw_breakpoint_cpu(addr, len, type,
+                                       triggered, cpu, active);
 
-       rc = -ENOSPC;
-       /* Check if we are over-committing */
-       if ((hbp_kernel_pos > 0) && (!hbp_user_refcount[hbp_kernel_pos-1])) {
-               hbp_kernel_pos--;
-               hbp_kernel[hbp_kernel_pos] = bp;
-               on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
-               rc = 0;
+               *pevent = bp;
+
+               if (IS_ERR(bp)) {
+                       err = PTR_ERR(bp);
+                       goto fail;
+               }
        }
 
-       spin_unlock_bh(&hw_breakpoint_lock);
-       return rc;
+       return cpu_events;
+
+fail:
+       for_each_possible_cpu(cpu) {
+               pevent = per_cpu_ptr(cpu_events, cpu);
+               if (IS_ERR(*pevent))
+                       break;
+               unregister_hw_breakpoint(*pevent);
+       }
+       free_percpu(cpu_events);
+       /* return the error if any */
+       return ERR_PTR(err);
 }
-EXPORT_SYMBOL_GPL(register_kernel_hw_breakpoint);
+EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
 
 /**
- * unregister_kernel_hw_breakpoint - unregister a HW breakpoint for kernel space
- * @bp: the breakpoint structure to unregister
- *
- * Uninstalls and unregisters @bp.
+ * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
+ * @cpu_events: the per cpu set of events to unregister
  */
-void unregister_kernel_hw_breakpoint(struct hw_breakpoint *bp)
+void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
 {
-       int i, j;
-
-       spin_lock_bh(&hw_breakpoint_lock);
-
-       /* Find the 'bp' in our list of breakpoints for kernel */
-       for (i = hbp_kernel_pos; i < HBP_NUM; i++)
-               if (bp == hbp_kernel[i])
-                       break;
+       int cpu;
+       struct perf_event **pevent;
 
-       /* Check if we did not find a match for 'bp'. If so return early */
-       if (i == HBP_NUM) {
-               spin_unlock_bh(&hw_breakpoint_lock);
-               return;
+       for_each_possible_cpu(cpu) {
+               pevent = per_cpu_ptr(cpu_events, cpu);
+               unregister_hw_breakpoint(*pevent);
        }
-
-       /*
-        * We'll shift the breakpoints one-level above to compact if
-        * unregistration creates a hole
-        */
-       for (j = i; j > hbp_kernel_pos; j--)
-               hbp_kernel[j] = hbp_kernel[j-1];
-
-       hbp_kernel[hbp_kernel_pos] = NULL;
-       on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
-       hbp_kernel_pos++;
-
-       spin_unlock_bh(&hw_breakpoint_lock);
+       free_percpu(cpu_events);
 }
-EXPORT_SYMBOL_GPL(unregister_kernel_hw_breakpoint);
+EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
 
 static struct notifier_block hw_breakpoint_exceptions_nb = {
        .notifier_call = hw_breakpoint_exceptions_notify,
@@ -374,5 +490,12 @@ static int __init init_hw_breakpoint(void)
 {
        return register_die_notifier(&hw_breakpoint_exceptions_nb);
 }
-
 core_initcall(init_hw_breakpoint);
+
+
+struct pmu perf_ops_bp = {
+       .enable         = arch_install_hw_breakpoint,
+       .disable        = arch_uninstall_hw_breakpoint,
+       .read           = hw_breakpoint_pmu_read,
+       .unthrottle     = hw_breakpoint_pmu_unthrottle
+};