mm: get rid of vmacache_flush_all() entirely
[linux/fpc-iii.git] / kernel / events / hw_breakpoint.c
blobb3814fce5ecb6bf7729ea858ec17ddb9018105f1
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2007 Alan Stern
17 * Copyright (C) IBM Corporation, 2009
18 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
20 * Thanks to Ingo Molnar for his many suggestions.
22 * Authors: Alan Stern <stern@rowland.harvard.edu>
23 * K.Prasad <prasad@linux.vnet.ibm.com>
24 * Frederic Weisbecker <fweisbec@gmail.com>
28 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
29 * using the CPU's debug registers.
30 * This file contains the arch-independent routines.
33 #include <linux/irqflags.h>
34 #include <linux/kallsyms.h>
35 #include <linux/notifier.h>
36 #include <linux/kprobes.h>
37 #include <linux/kdebug.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/percpu.h>
41 #include <linux/sched.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/list.h>
45 #include <linux/cpu.h>
46 #include <linux/smp.h>
47 #include <linux/bug.h>
49 #include <linux/hw_breakpoint.h>
51 * Constraints data
53 struct bp_cpuinfo {
54 /* Number of pinned cpu breakpoints in a cpu */
55 unsigned int cpu_pinned;
56 /* tsk_pinned[n] is the number of tasks having n+1 breakpoints */
57 unsigned int *tsk_pinned;
58 /* Number of non-pinned cpu/task breakpoints in a cpu */
59 unsigned int flexible; /* XXX: placeholder, see fetch_this_slot() */
62 static DEFINE_PER_CPU(struct bp_cpuinfo, bp_cpuinfo[TYPE_MAX]);
63 static int nr_slots[TYPE_MAX];
65 static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type)
67 return per_cpu_ptr(bp_cpuinfo + type, cpu);
70 /* Keep track of the breakpoints attached to tasks */
71 static LIST_HEAD(bp_task_head);
73 static int constraints_initialized;
75 /* Gather the number of total pinned and un-pinned bp in a cpuset */
76 struct bp_busy_slots {
77 unsigned int pinned;
78 unsigned int flexible;
81 /* Serialize accesses to the above constraints */
82 static DEFINE_MUTEX(nr_bp_mutex);
84 __weak int hw_breakpoint_weight(struct perf_event *bp)
86 return 1;
89 static inline enum bp_type_idx find_slot_idx(u64 bp_type)
91 if (bp_type & HW_BREAKPOINT_RW)
92 return TYPE_DATA;
94 return TYPE_INST;
98 * Report the maximum number of pinned breakpoints a task
99 * have in this cpu
101 static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
103 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
104 int i;
106 for (i = nr_slots[type] - 1; i >= 0; i--) {
107 if (tsk_pinned[i] > 0)
108 return i + 1;
111 return 0;
115 * Count the number of breakpoints of the same type and same task.
116 * The given event must be not on the list.
118 static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type)
120 struct task_struct *tsk = bp->hw.target;
121 struct perf_event *iter;
122 int count = 0;
124 list_for_each_entry(iter, &bp_task_head, hw.bp_list) {
125 if (iter->hw.target == tsk &&
126 find_slot_idx(iter->attr.bp_type) == type &&
127 (iter->cpu < 0 || cpu == iter->cpu))
128 count += hw_breakpoint_weight(iter);
131 return count;
134 static const struct cpumask *cpumask_of_bp(struct perf_event *bp)
136 if (bp->cpu >= 0)
137 return cpumask_of(bp->cpu);
138 return cpu_possible_mask;
142 * Report the number of pinned/un-pinned breakpoints we have in
143 * a given cpu (cpu > -1) or in all of them (cpu = -1).
145 static void
146 fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp,
147 enum bp_type_idx type)
149 const struct cpumask *cpumask = cpumask_of_bp(bp);
150 int cpu;
152 for_each_cpu(cpu, cpumask) {
153 struct bp_cpuinfo *info = get_bp_info(cpu, type);
154 int nr;
156 nr = info->cpu_pinned;
157 if (!bp->hw.target)
158 nr += max_task_bp_pinned(cpu, type);
159 else
160 nr += task_bp_pinned(cpu, bp, type);
162 if (nr > slots->pinned)
163 slots->pinned = nr;
165 nr = info->flexible;
166 if (nr > slots->flexible)
167 slots->flexible = nr;
172 * For now, continue to consider flexible as pinned, until we can
173 * ensure no flexible event can ever be scheduled before a pinned event
174 * in a same cpu.
176 static void
177 fetch_this_slot(struct bp_busy_slots *slots, int weight)
179 slots->pinned += weight;
183 * Add a pinned breakpoint for the given task in our constraint table
185 static void toggle_bp_task_slot(struct perf_event *bp, int cpu,
186 enum bp_type_idx type, int weight)
188 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
189 int old_idx, new_idx;
191 old_idx = task_bp_pinned(cpu, bp, type) - 1;
192 new_idx = old_idx + weight;
194 if (old_idx >= 0)
195 tsk_pinned[old_idx]--;
196 if (new_idx >= 0)
197 tsk_pinned[new_idx]++;
201 * Add/remove the given breakpoint in our constraint table
203 static void
204 toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type,
205 int weight)
207 const struct cpumask *cpumask = cpumask_of_bp(bp);
208 int cpu;
210 if (!enable)
211 weight = -weight;
213 /* Pinned counter cpu profiling */
214 if (!bp->hw.target) {
215 get_bp_info(bp->cpu, type)->cpu_pinned += weight;
216 return;
219 /* Pinned counter task profiling */
220 for_each_cpu(cpu, cpumask)
221 toggle_bp_task_slot(bp, cpu, type, weight);
223 if (enable)
224 list_add_tail(&bp->hw.bp_list, &bp_task_head);
225 else
226 list_del(&bp->hw.bp_list);
230 * Function to perform processor-specific cleanup during unregistration
232 __weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
235 * A weak stub function here for those archs that don't define
236 * it inside arch/.../kernel/hw_breakpoint.c
241 * Contraints to check before allowing this new breakpoint counter:
243 * == Non-pinned counter == (Considered as pinned for now)
245 * - If attached to a single cpu, check:
247 * (per_cpu(info->flexible, cpu) || (per_cpu(info->cpu_pinned, cpu)
248 * + max(per_cpu(info->tsk_pinned, cpu)))) < HBP_NUM
250 * -> If there are already non-pinned counters in this cpu, it means
251 * there is already a free slot for them.
252 * Otherwise, we check that the maximum number of per task
253 * breakpoints (for this cpu) plus the number of per cpu breakpoint
254 * (for this cpu) doesn't cover every registers.
256 * - If attached to every cpus, check:
258 * (per_cpu(info->flexible, *) || (max(per_cpu(info->cpu_pinned, *))
259 * + max(per_cpu(info->tsk_pinned, *)))) < HBP_NUM
261 * -> This is roughly the same, except we check the number of per cpu
262 * bp for every cpu and we keep the max one. Same for the per tasks
263 * breakpoints.
266 * == Pinned counter ==
268 * - If attached to a single cpu, check:
270 * ((per_cpu(info->flexible, cpu) > 1) + per_cpu(info->cpu_pinned, cpu)
271 * + max(per_cpu(info->tsk_pinned, cpu))) < HBP_NUM
273 * -> Same checks as before. But now the info->flexible, if any, must keep
274 * one register at least (or they will never be fed).
276 * - If attached to every cpus, check:
278 * ((per_cpu(info->flexible, *) > 1) + max(per_cpu(info->cpu_pinned, *))
279 * + max(per_cpu(info->tsk_pinned, *))) < HBP_NUM
281 static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type)
283 struct bp_busy_slots slots = {0};
284 enum bp_type_idx type;
285 int weight;
287 /* We couldn't initialize breakpoint constraints on boot */
288 if (!constraints_initialized)
289 return -ENOMEM;
291 /* Basic checks */
292 if (bp_type == HW_BREAKPOINT_EMPTY ||
293 bp_type == HW_BREAKPOINT_INVALID)
294 return -EINVAL;
296 type = find_slot_idx(bp_type);
297 weight = hw_breakpoint_weight(bp);
299 fetch_bp_busy_slots(&slots, bp, type);
301 * Simulate the addition of this breakpoint to the constraints
302 * and see the result.
304 fetch_this_slot(&slots, weight);
306 /* Flexible counters need to keep at least one slot */
307 if (slots.pinned + (!!slots.flexible) > nr_slots[type])
308 return -ENOSPC;
310 toggle_bp_slot(bp, true, type, weight);
312 return 0;
315 int reserve_bp_slot(struct perf_event *bp)
317 int ret;
319 mutex_lock(&nr_bp_mutex);
321 ret = __reserve_bp_slot(bp, bp->attr.bp_type);
323 mutex_unlock(&nr_bp_mutex);
325 return ret;
328 static void __release_bp_slot(struct perf_event *bp, u64 bp_type)
330 enum bp_type_idx type;
331 int weight;
333 type = find_slot_idx(bp_type);
334 weight = hw_breakpoint_weight(bp);
335 toggle_bp_slot(bp, false, type, weight);
338 void release_bp_slot(struct perf_event *bp)
340 mutex_lock(&nr_bp_mutex);
342 arch_unregister_hw_breakpoint(bp);
343 __release_bp_slot(bp, bp->attr.bp_type);
345 mutex_unlock(&nr_bp_mutex);
348 static int __modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
350 int err;
352 __release_bp_slot(bp, old_type);
354 err = __reserve_bp_slot(bp, new_type);
355 if (err) {
357 * Reserve the old_type slot back in case
358 * there's no space for the new type.
360 * This must succeed, because we just released
361 * the old_type slot in the __release_bp_slot
362 * call above. If not, something is broken.
364 WARN_ON(__reserve_bp_slot(bp, old_type));
367 return err;
370 static int modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
372 int ret;
374 mutex_lock(&nr_bp_mutex);
375 ret = __modify_bp_slot(bp, old_type, new_type);
376 mutex_unlock(&nr_bp_mutex);
377 return ret;
381 * Allow the kernel debugger to reserve breakpoint slots without
382 * taking a lock using the dbg_* variant of for the reserve and
383 * release breakpoint slots.
385 int dbg_reserve_bp_slot(struct perf_event *bp)
387 if (mutex_is_locked(&nr_bp_mutex))
388 return -1;
390 return __reserve_bp_slot(bp, bp->attr.bp_type);
393 int dbg_release_bp_slot(struct perf_event *bp)
395 if (mutex_is_locked(&nr_bp_mutex))
396 return -1;
398 __release_bp_slot(bp, bp->attr.bp_type);
400 return 0;
403 static int hw_breakpoint_parse(struct perf_event *bp,
404 const struct perf_event_attr *attr,
405 struct arch_hw_breakpoint *hw)
407 int err;
409 err = hw_breakpoint_arch_parse(bp, attr, hw);
410 if (err)
411 return err;
413 if (arch_check_bp_in_kernelspace(hw)) {
414 if (attr->exclude_kernel)
415 return -EINVAL;
417 * Don't let unprivileged users set a breakpoint in the trap
418 * path to avoid trap recursion attacks.
420 if (!capable(CAP_SYS_ADMIN))
421 return -EPERM;
424 return 0;
427 int register_perf_hw_breakpoint(struct perf_event *bp)
429 struct arch_hw_breakpoint hw;
430 int err;
432 err = reserve_bp_slot(bp);
433 if (err)
434 return err;
436 err = hw_breakpoint_parse(bp, &bp->attr, &hw);
437 if (err) {
438 release_bp_slot(bp);
439 return err;
442 bp->hw.info = hw;
444 return 0;
448 * register_user_hw_breakpoint - register a hardware breakpoint for user space
449 * @attr: breakpoint attributes
450 * @triggered: callback to trigger when we hit the breakpoint
451 * @tsk: pointer to 'task_struct' of the process to which the address belongs
453 struct perf_event *
454 register_user_hw_breakpoint(struct perf_event_attr *attr,
455 perf_overflow_handler_t triggered,
456 void *context,
457 struct task_struct *tsk)
459 return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
460 context);
462 EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
464 static void hw_breakpoint_copy_attr(struct perf_event_attr *to,
465 struct perf_event_attr *from)
467 to->bp_addr = from->bp_addr;
468 to->bp_type = from->bp_type;
469 to->bp_len = from->bp_len;
470 to->disabled = from->disabled;
474 modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
475 bool check)
477 struct arch_hw_breakpoint hw;
478 int err;
480 err = hw_breakpoint_parse(bp, attr, &hw);
481 if (err)
482 return err;
484 if (check) {
485 struct perf_event_attr old_attr;
487 old_attr = bp->attr;
488 hw_breakpoint_copy_attr(&old_attr, attr);
489 if (memcmp(&old_attr, attr, sizeof(*attr)))
490 return -EINVAL;
493 if (bp->attr.bp_type != attr->bp_type) {
494 err = modify_bp_slot(bp, bp->attr.bp_type, attr->bp_type);
495 if (err)
496 return err;
499 hw_breakpoint_copy_attr(&bp->attr, attr);
500 bp->hw.info = hw;
502 return 0;
506 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
507 * @bp: the breakpoint structure to modify
508 * @attr: new breakpoint attributes
510 int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
513 * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it
514 * will not be possible to raise IPIs that invoke __perf_event_disable.
515 * So call the function directly after making sure we are targeting the
516 * current task.
518 if (irqs_disabled() && bp->ctx && bp->ctx->task == current)
519 perf_event_disable_local(bp);
520 else
521 perf_event_disable(bp);
523 if (!attr->disabled) {
524 int err = modify_user_hw_breakpoint_check(bp, attr, false);
526 if (err)
527 return err;
528 perf_event_enable(bp);
529 bp->attr.disabled = 0;
531 return 0;
533 EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
536 * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
537 * @bp: the breakpoint structure to unregister
539 void unregister_hw_breakpoint(struct perf_event *bp)
541 if (!bp)
542 return;
543 perf_event_release_kernel(bp);
545 EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
548 * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
549 * @attr: breakpoint attributes
550 * @triggered: callback to trigger when we hit the breakpoint
552 * @return a set of per_cpu pointers to perf events
554 struct perf_event * __percpu *
555 register_wide_hw_breakpoint(struct perf_event_attr *attr,
556 perf_overflow_handler_t triggered,
557 void *context)
559 struct perf_event * __percpu *cpu_events, *bp;
560 long err = 0;
561 int cpu;
563 cpu_events = alloc_percpu(typeof(*cpu_events));
564 if (!cpu_events)
565 return (void __percpu __force *)ERR_PTR(-ENOMEM);
567 get_online_cpus();
568 for_each_online_cpu(cpu) {
569 bp = perf_event_create_kernel_counter(attr, cpu, NULL,
570 triggered, context);
571 if (IS_ERR(bp)) {
572 err = PTR_ERR(bp);
573 break;
576 per_cpu(*cpu_events, cpu) = bp;
578 put_online_cpus();
580 if (likely(!err))
581 return cpu_events;
583 unregister_wide_hw_breakpoint(cpu_events);
584 return (void __percpu __force *)ERR_PTR(err);
586 EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
589 * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
590 * @cpu_events: the per cpu set of events to unregister
592 void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
594 int cpu;
596 for_each_possible_cpu(cpu)
597 unregister_hw_breakpoint(per_cpu(*cpu_events, cpu));
599 free_percpu(cpu_events);
601 EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
603 static struct notifier_block hw_breakpoint_exceptions_nb = {
604 .notifier_call = hw_breakpoint_exceptions_notify,
605 /* we need to be notified first */
606 .priority = 0x7fffffff
609 static void bp_perf_event_destroy(struct perf_event *event)
611 release_bp_slot(event);
614 static int hw_breakpoint_event_init(struct perf_event *bp)
616 int err;
618 if (bp->attr.type != PERF_TYPE_BREAKPOINT)
619 return -ENOENT;
622 * no branch sampling for breakpoint events
624 if (has_branch_stack(bp))
625 return -EOPNOTSUPP;
627 err = register_perf_hw_breakpoint(bp);
628 if (err)
629 return err;
631 bp->destroy = bp_perf_event_destroy;
633 return 0;
636 static int hw_breakpoint_add(struct perf_event *bp, int flags)
638 if (!(flags & PERF_EF_START))
639 bp->hw.state = PERF_HES_STOPPED;
641 if (is_sampling_event(bp)) {
642 bp->hw.last_period = bp->hw.sample_period;
643 perf_swevent_set_period(bp);
646 return arch_install_hw_breakpoint(bp);
649 static void hw_breakpoint_del(struct perf_event *bp, int flags)
651 arch_uninstall_hw_breakpoint(bp);
654 static void hw_breakpoint_start(struct perf_event *bp, int flags)
656 bp->hw.state = 0;
659 static void hw_breakpoint_stop(struct perf_event *bp, int flags)
661 bp->hw.state = PERF_HES_STOPPED;
664 static struct pmu perf_breakpoint = {
665 .task_ctx_nr = perf_sw_context, /* could eventually get its own */
667 .event_init = hw_breakpoint_event_init,
668 .add = hw_breakpoint_add,
669 .del = hw_breakpoint_del,
670 .start = hw_breakpoint_start,
671 .stop = hw_breakpoint_stop,
672 .read = hw_breakpoint_pmu_read,
675 int __init init_hw_breakpoint(void)
677 int cpu, err_cpu;
678 int i;
680 for (i = 0; i < TYPE_MAX; i++)
681 nr_slots[i] = hw_breakpoint_slots(i);
683 for_each_possible_cpu(cpu) {
684 for (i = 0; i < TYPE_MAX; i++) {
685 struct bp_cpuinfo *info = get_bp_info(cpu, i);
687 info->tsk_pinned = kcalloc(nr_slots[i], sizeof(int),
688 GFP_KERNEL);
689 if (!info->tsk_pinned)
690 goto err_alloc;
694 constraints_initialized = 1;
696 perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT);
698 return register_die_notifier(&hw_breakpoint_exceptions_nb);
700 err_alloc:
701 for_each_possible_cpu(err_cpu) {
702 for (i = 0; i < TYPE_MAX; i++)
703 kfree(get_bp_info(err_cpu, i)->tsk_pinned);
704 if (err_cpu == cpu)
705 break;
708 return -ENOMEM;