Commit | Line | Data |
---|---|---|
62a038d3 P |
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. | |
6 | * | |
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. | |
11 | * | |
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. | |
15 | * | |
16 | * Copyright (C) 2007 Alan Stern | |
17 | * Copyright (C) IBM Corporation, 2009 | |
24f1e32c | 18 | * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com> |
ba1c813a FW |
19 | * |
20 | * Thanks to Ingo Molnar for his many suggestions. | |
ba6909b7 P |
21 | * |
22 | * Authors: Alan Stern <stern@rowland.harvard.edu> | |
23 | * K.Prasad <prasad@linux.vnet.ibm.com> | |
24 | * Frederic Weisbecker <fweisbec@gmail.com> | |
62a038d3 P |
25 | */ |
26 | ||
27 | /* | |
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. | |
31 | */ | |
32 | ||
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> | |
feef47d0 | 43 | #include <linux/slab.h> |
45a73372 | 44 | #include <linux/list.h> |
88f7a890 | 45 | #include <linux/cpu.h> |
62a038d3 | 46 | #include <linux/smp.h> |
ea6a9d53 | 47 | #include <linux/bug.h> |
62a038d3 | 48 | |
24f1e32c | 49 | #include <linux/hw_breakpoint.h> |
ba1c813a FW |
50 | /* |
51 | * Constraints data | |
52 | */ | |
bde96030 ON |
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() */ | |
60 | }; | |
62a038d3 | 61 | |
bde96030 | 62 | static DEFINE_PER_CPU(struct bp_cpuinfo, bp_cpuinfo[TYPE_MAX]); |
feef47d0 FW |
63 | static int nr_slots[TYPE_MAX]; |
64 | ||
bde96030 ON |
65 | static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type) |
66 | { | |
67 | return per_cpu_ptr(bp_cpuinfo + type, cpu); | |
68 | } | |
69 | ||
45a73372 FW |
70 | /* Keep track of the breakpoints attached to tasks */ |
71 | static LIST_HEAD(bp_task_head); | |
72 | ||
feef47d0 FW |
73 | static int constraints_initialized; |
74 | ||
ba1c813a FW |
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; | |
79 | }; | |
80 | ||
81 | /* Serialize accesses to the above constraints */ | |
82 | static DEFINE_MUTEX(nr_bp_mutex); | |
83 | ||
f93a2054 FW |
84 | __weak int hw_breakpoint_weight(struct perf_event *bp) |
85 | { | |
86 | return 1; | |
87 | } | |
88 | ||
cbd9d9f1 | 89 | static inline enum bp_type_idx find_slot_idx(u64 bp_type) |
0102752e | 90 | { |
cbd9d9f1 | 91 | if (bp_type & HW_BREAKPOINT_RW) |
0102752e FW |
92 | return TYPE_DATA; |
93 | ||
94 | return TYPE_INST; | |
95 | } | |
96 | ||
ba1c813a FW |
97 | /* |
98 | * Report the maximum number of pinned breakpoints a task | |
99 | * have in this cpu | |
100 | */ | |
0102752e | 101 | static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type) |
62a038d3 | 102 | { |
bde96030 | 103 | unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned; |
ba1c813a | 104 | int i; |
62a038d3 | 105 | |
feef47d0 | 106 | for (i = nr_slots[type] - 1; i >= 0; i--) { |
ba1c813a FW |
107 | if (tsk_pinned[i] > 0) |
108 | return i + 1; | |
62a038d3 P |
109 | } |
110 | ||
24f1e32c | 111 | return 0; |
62a038d3 P |
112 | } |
113 | ||
45a73372 FW |
114 | /* |
115 | * Count the number of breakpoints of the same type and same task. | |
116 | * The given event must be not on the list. | |
117 | */ | |
0d855354 | 118 | static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type) |
56053170 | 119 | { |
50f16a8b | 120 | struct task_struct *tsk = bp->hw.target; |
45a73372 | 121 | struct perf_event *iter; |
56053170 FW |
122 | int count = 0; |
123 | ||
45a73372 | 124 | list_for_each_entry(iter, &bp_task_head, hw.bp_list) { |
50f16a8b | 125 | if (iter->hw.target == tsk && |
cbd9d9f1 | 126 | find_slot_idx(iter->attr.bp_type) == type && |
8b4d801b | 127 | (iter->cpu < 0 || cpu == iter->cpu)) |
45a73372 | 128 | count += hw_breakpoint_weight(iter); |
56053170 FW |
129 | } |
130 | ||
56053170 FW |
131 | return count; |
132 | } | |
133 | ||
1c10adbb ON |
134 | static const struct cpumask *cpumask_of_bp(struct perf_event *bp) |
135 | { | |
136 | if (bp->cpu >= 0) | |
137 | return cpumask_of(bp->cpu); | |
138 | return cpu_possible_mask; | |
139 | } | |
140 | ||
ba1c813a FW |
141 | /* |
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). | |
144 | */ | |
56053170 | 145 | static void |
0102752e FW |
146 | fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp, |
147 | enum bp_type_idx type) | |
ba1c813a | 148 | { |
1c10adbb ON |
149 | const struct cpumask *cpumask = cpumask_of_bp(bp); |
150 | int cpu; | |
56053170 | 151 | |
1c10adbb | 152 | for_each_cpu(cpu, cpumask) { |
bde96030 ON |
153 | struct bp_cpuinfo *info = get_bp_info(cpu, type); |
154 | int nr; | |
ba1c813a | 155 | |
bde96030 | 156 | nr = info->cpu_pinned; |
50f16a8b | 157 | if (!bp->hw.target) |
0102752e | 158 | nr += max_task_bp_pinned(cpu, type); |
56053170 | 159 | else |
0d855354 | 160 | nr += task_bp_pinned(cpu, bp, type); |
ba1c813a FW |
161 | |
162 | if (nr > slots->pinned) | |
163 | slots->pinned = nr; | |
164 | ||
bde96030 | 165 | nr = info->flexible; |
ba1c813a FW |
166 | if (nr > slots->flexible) |
167 | slots->flexible = nr; | |
168 | } | |
169 | } | |
170 | ||
f93a2054 FW |
171 | /* |
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. | |
175 | */ | |
176 | static void | |
177 | fetch_this_slot(struct bp_busy_slots *slots, int weight) | |
178 | { | |
179 | slots->pinned += weight; | |
180 | } | |
181 | ||
ba1c813a FW |
182 | /* |
183 | * Add a pinned breakpoint for the given task in our constraint table | |
184 | */ | |
7ab71f32 | 185 | static void toggle_bp_task_slot(struct perf_event *bp, int cpu, |
f93a2054 | 186 | enum bp_type_idx type, int weight) |
ba1c813a | 187 | { |
bde96030 | 188 | unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned; |
e1ebe862 ON |
189 | int old_idx, new_idx; |
190 | ||
191 | old_idx = task_bp_pinned(cpu, bp, type) - 1; | |
7ab71f32 | 192 | new_idx = old_idx + weight; |
e1ebe862 ON |
193 | |
194 | if (old_idx >= 0) | |
195 | tsk_pinned[old_idx]--; | |
196 | if (new_idx >= 0) | |
197 | tsk_pinned[new_idx]++; | |
ba1c813a FW |
198 | } |
199 | ||
200 | /* | |
201 | * Add/remove the given breakpoint in our constraint table | |
202 | */ | |
0102752e | 203 | static void |
f93a2054 FW |
204 | toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, |
205 | int weight) | |
ba1c813a | 206 | { |
1c10adbb ON |
207 | const struct cpumask *cpumask = cpumask_of_bp(bp); |
208 | int cpu; | |
ba1c813a | 209 | |
7ab71f32 ON |
210 | if (!enable) |
211 | weight = -weight; | |
212 | ||
45a73372 | 213 | /* Pinned counter cpu profiling */ |
50f16a8b | 214 | if (!bp->hw.target) { |
bde96030 | 215 | get_bp_info(bp->cpu, type)->cpu_pinned += weight; |
45a73372 FW |
216 | return; |
217 | } | |
218 | ||
ba1c813a | 219 | /* Pinned counter task profiling */ |
1c10adbb | 220 | for_each_cpu(cpu, cpumask) |
7ab71f32 | 221 | toggle_bp_task_slot(bp, cpu, type, weight); |
ba1c813a | 222 | |
ba1c813a | 223 | if (enable) |
45a73372 | 224 | list_add_tail(&bp->hw.bp_list, &bp_task_head); |
e1ebe862 ON |
225 | else |
226 | list_del(&bp->hw.bp_list); | |
ba1c813a FW |
227 | } |
228 | ||
f7136c51 P |
229 | /* |
230 | * Function to perform processor-specific cleanup during unregistration | |
231 | */ | |
232 | __weak void arch_unregister_hw_breakpoint(struct perf_event *bp) | |
233 | { | |
234 | /* | |
235 | * A weak stub function here for those archs that don't define | |
236 | * it inside arch/.../kernel/hw_breakpoint.c | |
237 | */ | |
238 | } | |
239 | ||
ba1c813a FW |
240 | /* |
241 | * Contraints to check before allowing this new breakpoint counter: | |
242 | * | |
243 | * == Non-pinned counter == (Considered as pinned for now) | |
244 | * | |
245 | * - If attached to a single cpu, check: | |
246 | * | |
bde96030 ON |
247 | * (per_cpu(info->flexible, cpu) || (per_cpu(info->cpu_pinned, cpu) |
248 | * + max(per_cpu(info->tsk_pinned, cpu)))) < HBP_NUM | |
ba1c813a FW |
249 | * |
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. | |
255 | * | |
256 | * - If attached to every cpus, check: | |
257 | * | |
bde96030 ON |
258 | * (per_cpu(info->flexible, *) || (max(per_cpu(info->cpu_pinned, *)) |
259 | * + max(per_cpu(info->tsk_pinned, *)))) < HBP_NUM | |
ba1c813a FW |
260 | * |
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. | |
264 | * | |
265 | * | |
266 | * == Pinned counter == | |
267 | * | |
268 | * - If attached to a single cpu, check: | |
269 | * | |
bde96030 ON |
270 | * ((per_cpu(info->flexible, cpu) > 1) + per_cpu(info->cpu_pinned, cpu) |
271 | * + max(per_cpu(info->tsk_pinned, cpu))) < HBP_NUM | |
ba1c813a | 272 | * |
bde96030 | 273 | * -> Same checks as before. But now the info->flexible, if any, must keep |
ba1c813a FW |
274 | * one register at least (or they will never be fed). |
275 | * | |
276 | * - If attached to every cpus, check: | |
277 | * | |
bde96030 ON |
278 | * ((per_cpu(info->flexible, *) > 1) + max(per_cpu(info->cpu_pinned, *)) |
279 | * + max(per_cpu(info->tsk_pinned, *))) < HBP_NUM | |
ba1c813a | 280 | */ |
1ad9ff7d | 281 | static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type) |
ba1c813a FW |
282 | { |
283 | struct bp_busy_slots slots = {0}; | |
0102752e | 284 | enum bp_type_idx type; |
f93a2054 | 285 | int weight; |
ba1c813a | 286 | |
feef47d0 FW |
287 | /* We couldn't initialize breakpoint constraints on boot */ |
288 | if (!constraints_initialized) | |
289 | return -ENOMEM; | |
290 | ||
0102752e | 291 | /* Basic checks */ |
1ad9ff7d JO |
292 | if (bp_type == HW_BREAKPOINT_EMPTY || |
293 | bp_type == HW_BREAKPOINT_INVALID) | |
0102752e FW |
294 | return -EINVAL; |
295 | ||
1ad9ff7d | 296 | type = find_slot_idx(bp_type); |
f93a2054 FW |
297 | weight = hw_breakpoint_weight(bp); |
298 | ||
0102752e | 299 | fetch_bp_busy_slots(&slots, bp, type); |
45a73372 FW |
300 | /* |
301 | * Simulate the addition of this breakpoint to the constraints | |
302 | * and see the result. | |
303 | */ | |
f93a2054 | 304 | fetch_this_slot(&slots, weight); |
ba1c813a FW |
305 | |
306 | /* Flexible counters need to keep at least one slot */ | |
feef47d0 | 307 | if (slots.pinned + (!!slots.flexible) > nr_slots[type]) |
5352ae63 | 308 | return -ENOSPC; |
ba1c813a | 309 | |
f93a2054 | 310 | toggle_bp_slot(bp, true, type, weight); |
ba1c813a | 311 | |
5352ae63 JW |
312 | return 0; |
313 | } | |
314 | ||
315 | int reserve_bp_slot(struct perf_event *bp) | |
316 | { | |
317 | int ret; | |
318 | ||
319 | mutex_lock(&nr_bp_mutex); | |
320 | ||
1ad9ff7d | 321 | ret = __reserve_bp_slot(bp, bp->attr.bp_type); |
5352ae63 | 322 | |
ba1c813a FW |
323 | mutex_unlock(&nr_bp_mutex); |
324 | ||
325 | return ret; | |
326 | } | |
327 | ||
1ad9ff7d | 328 | static void __release_bp_slot(struct perf_event *bp, u64 bp_type) |
5352ae63 | 329 | { |
0102752e | 330 | enum bp_type_idx type; |
f93a2054 | 331 | int weight; |
0102752e | 332 | |
1ad9ff7d | 333 | type = find_slot_idx(bp_type); |
f93a2054 FW |
334 | weight = hw_breakpoint_weight(bp); |
335 | toggle_bp_slot(bp, false, type, weight); | |
5352ae63 JW |
336 | } |
337 | ||
24f1e32c | 338 | void release_bp_slot(struct perf_event *bp) |
62a038d3 | 339 | { |
ba1c813a FW |
340 | mutex_lock(&nr_bp_mutex); |
341 | ||
f7136c51 | 342 | arch_unregister_hw_breakpoint(bp); |
1ad9ff7d | 343 | __release_bp_slot(bp, bp->attr.bp_type); |
ba1c813a FW |
344 | |
345 | mutex_unlock(&nr_bp_mutex); | |
62a038d3 P |
346 | } |
347 | ||
cb8b7881 | 348 | static int __modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type) |
ea6a9d53 JO |
349 | { |
350 | int err; | |
351 | ||
352 | __release_bp_slot(bp, old_type); | |
353 | ||
cb8b7881 | 354 | err = __reserve_bp_slot(bp, new_type); |
ea6a9d53 JO |
355 | if (err) { |
356 | /* | |
357 | * Reserve the old_type slot back in case | |
358 | * there's no space for the new type. | |
359 | * | |
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. | |
363 | */ | |
364 | WARN_ON(__reserve_bp_slot(bp, old_type)); | |
365 | } | |
366 | ||
367 | return err; | |
368 | } | |
369 | ||
cb8b7881 | 370 | static int modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type) |
ea6a9d53 JO |
371 | { |
372 | int ret; | |
373 | ||
374 | mutex_lock(&nr_bp_mutex); | |
cb8b7881 | 375 | ret = __modify_bp_slot(bp, old_type, new_type); |
ea6a9d53 JO |
376 | mutex_unlock(&nr_bp_mutex); |
377 | return ret; | |
378 | } | |
379 | ||
5352ae63 JW |
380 | /* |
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. | |
384 | */ | |
385 | int dbg_reserve_bp_slot(struct perf_event *bp) | |
386 | { | |
387 | if (mutex_is_locked(&nr_bp_mutex)) | |
388 | return -1; | |
389 | ||
1ad9ff7d | 390 | return __reserve_bp_slot(bp, bp->attr.bp_type); |
5352ae63 JW |
391 | } |
392 | ||
393 | int dbg_release_bp_slot(struct perf_event *bp) | |
394 | { | |
395 | if (mutex_is_locked(&nr_bp_mutex)) | |
396 | return -1; | |
397 | ||
1ad9ff7d | 398 | __release_bp_slot(bp, bp->attr.bp_type); |
5352ae63 JW |
399 | |
400 | return 0; | |
401 | } | |
ba1c813a | 402 | |
9a4903dd FW |
403 | static int hw_breakpoint_parse(struct perf_event *bp, |
404 | const struct perf_event_attr *attr, | |
405 | struct arch_hw_breakpoint *hw) | |
406 | { | |
407 | int err; | |
b2812d03 | 408 | |
9a4903dd FW |
409 | err = hw_breakpoint_arch_parse(bp, attr, hw); |
410 | if (err) | |
411 | return err; | |
b2812d03 | 412 | |
8e983ff9 | 413 | if (arch_check_bp_in_kernelspace(hw)) { |
9a4903dd | 414 | if (attr->exclude_kernel) |
b2812d03 FW |
415 | return -EINVAL; |
416 | /* | |
417 | * Don't let unprivileged users set a breakpoint in the trap | |
418 | * path to avoid trap recursion attacks. | |
419 | */ | |
420 | if (!capable(CAP_SYS_ADMIN)) | |
421 | return -EPERM; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
b326e956 | 427 | int register_perf_hw_breakpoint(struct perf_event *bp) |
62a038d3 | 428 | { |
9a4903dd FW |
429 | struct arch_hw_breakpoint hw; |
430 | int err; | |
62a038d3 | 431 | |
9a4903dd FW |
432 | err = reserve_bp_slot(bp); |
433 | if (err) | |
434 | return err; | |
62a038d3 | 435 | |
9a4903dd FW |
436 | err = hw_breakpoint_parse(bp, &bp->attr, &hw); |
437 | if (err) { | |
b23ff0e9 | 438 | release_bp_slot(bp); |
9a4903dd FW |
439 | return err; |
440 | } | |
b23ff0e9 | 441 | |
9a4903dd FW |
442 | bp->hw.info = hw; |
443 | ||
444 | return 0; | |
24f1e32c | 445 | } |
62a038d3 | 446 | |
62a038d3 P |
447 | /** |
448 | * register_user_hw_breakpoint - register a hardware breakpoint for user space | |
5fa10b28 | 449 | * @attr: breakpoint attributes |
24f1e32c | 450 | * @triggered: callback to trigger when we hit the breakpoint |
62a038d3 | 451 | * @tsk: pointer to 'task_struct' of the process to which the address belongs |
62a038d3 | 452 | */ |
24f1e32c | 453 | struct perf_event * |
5fa10b28 | 454 | register_user_hw_breakpoint(struct perf_event_attr *attr, |
b326e956 | 455 | perf_overflow_handler_t triggered, |
4dc0da86 | 456 | void *context, |
5fa10b28 | 457 | struct task_struct *tsk) |
62a038d3 | 458 | { |
4dc0da86 AK |
459 | return perf_event_create_kernel_counter(attr, -1, tsk, triggered, |
460 | context); | |
62a038d3 P |
461 | } |
462 | EXPORT_SYMBOL_GPL(register_user_hw_breakpoint); | |
463 | ||
26c6ccdf FW |
464 | static void hw_breakpoint_copy_attr(struct perf_event_attr *to, |
465 | struct perf_event_attr *from) | |
466 | { | |
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; | |
471 | } | |
472 | ||
32ff77e8 | 473 | int |
705feaf3 JO |
474 | modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr, |
475 | bool check) | |
18ff57b2 | 476 | { |
9a4903dd | 477 | struct arch_hw_breakpoint hw; |
26c6ccdf | 478 | int err; |
18ff57b2 | 479 | |
26c6ccdf FW |
480 | err = hw_breakpoint_parse(bp, attr, &hw); |
481 | if (err) | |
482 | return err; | |
18ff57b2 | 483 | |
26c6ccdf FW |
484 | if (check) { |
485 | struct perf_event_attr old_attr; | |
705feaf3 | 486 | |
26c6ccdf FW |
487 | old_attr = bp->attr; |
488 | hw_breakpoint_copy_attr(&old_attr, attr); | |
489 | if (memcmp(&old_attr, attr, sizeof(*attr))) | |
490 | return -EINVAL; | |
491 | } | |
18ff57b2 | 492 | |
26c6ccdf FW |
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; | |
18ff57b2 JO |
497 | } |
498 | ||
26c6ccdf | 499 | hw_breakpoint_copy_attr(&bp->attr, attr); |
9a4903dd | 500 | bp->hw.info = hw; |
9a4903dd | 501 | |
18ff57b2 JO |
502 | return 0; |
503 | } | |
504 | ||
62a038d3 P |
505 | /** |
506 | * modify_user_hw_breakpoint - modify a user-space hardware breakpoint | |
24f1e32c | 507 | * @bp: the breakpoint structure to modify |
5fa10b28 | 508 | * @attr: new breakpoint attributes |
62a038d3 | 509 | */ |
44234adc | 510 | int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr) |
62a038d3 | 511 | { |
bd14406b JO |
512 | int err; |
513 | ||
500ad2d8 P |
514 | /* |
515 | * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it | |
516 | * will not be possible to raise IPIs that invoke __perf_event_disable. | |
517 | * So call the function directly after making sure we are targeting the | |
518 | * current task. | |
519 | */ | |
520 | if (irqs_disabled() && bp->ctx && bp->ctx->task == current) | |
fae3fde6 | 521 | perf_event_disable_local(bp); |
500ad2d8 P |
522 | else |
523 | perf_event_disable(bp); | |
44234adc | 524 | |
bd14406b | 525 | err = modify_user_hw_breakpoint_check(bp, attr, false); |
44234adc | 526 | |
96955837 | 527 | if (!bp->attr.disabled) |
ea6a9d53 | 528 | perf_event_enable(bp); |
cb45302d | 529 | |
96955837 | 530 | return err; |
62a038d3 P |
531 | } |
532 | EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint); | |
533 | ||
534 | /** | |
24f1e32c | 535 | * unregister_hw_breakpoint - unregister a user-space hardware breakpoint |
62a038d3 | 536 | * @bp: the breakpoint structure to unregister |
62a038d3 | 537 | */ |
24f1e32c | 538 | void unregister_hw_breakpoint(struct perf_event *bp) |
62a038d3 | 539 | { |
24f1e32c FW |
540 | if (!bp) |
541 | return; | |
542 | perf_event_release_kernel(bp); | |
543 | } | |
544 | EXPORT_SYMBOL_GPL(unregister_hw_breakpoint); | |
545 | ||
62a038d3 | 546 | /** |
24f1e32c | 547 | * register_wide_hw_breakpoint - register a wide breakpoint in the kernel |
dd1853c3 | 548 | * @attr: breakpoint attributes |
24f1e32c | 549 | * @triggered: callback to trigger when we hit the breakpoint |
62a038d3 | 550 | * |
24f1e32c | 551 | * @return a set of per_cpu pointers to perf events |
62a038d3 | 552 | */ |
44ee6358 | 553 | struct perf_event * __percpu * |
dd1853c3 | 554 | register_wide_hw_breakpoint(struct perf_event_attr *attr, |
4dc0da86 AK |
555 | perf_overflow_handler_t triggered, |
556 | void *context) | |
62a038d3 | 557 | { |
e12cbc10 ON |
558 | struct perf_event * __percpu *cpu_events, *bp; |
559 | long err = 0; | |
24f1e32c FW |
560 | int cpu; |
561 | ||
562 | cpu_events = alloc_percpu(typeof(*cpu_events)); | |
563 | if (!cpu_events) | |
44ee6358 | 564 | return (void __percpu __force *)ERR_PTR(-ENOMEM); |
62a038d3 | 565 | |
88f7a890 LZ |
566 | get_online_cpus(); |
567 | for_each_online_cpu(cpu) { | |
4dc0da86 AK |
568 | bp = perf_event_create_kernel_counter(attr, cpu, NULL, |
569 | triggered, context); | |
605bfaee | 570 | if (IS_ERR(bp)) { |
24f1e32c | 571 | err = PTR_ERR(bp); |
e12cbc10 | 572 | break; |
24f1e32c | 573 | } |
24f1e32c | 574 | |
e12cbc10 | 575 | per_cpu(*cpu_events, cpu) = bp; |
24f1e32c | 576 | } |
88f7a890 LZ |
577 | put_online_cpus(); |
578 | ||
e12cbc10 ON |
579 | if (likely(!err)) |
580 | return cpu_events; | |
581 | ||
582 | unregister_wide_hw_breakpoint(cpu_events); | |
44ee6358 | 583 | return (void __percpu __force *)ERR_PTR(err); |
62a038d3 | 584 | } |
f60d24d2 | 585 | EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint); |
62a038d3 P |
586 | |
587 | /** | |
24f1e32c FW |
588 | * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel |
589 | * @cpu_events: the per cpu set of events to unregister | |
62a038d3 | 590 | */ |
44ee6358 | 591 | void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) |
62a038d3 | 592 | { |
24f1e32c | 593 | int cpu; |
62a038d3 | 594 | |
e12cbc10 ON |
595 | for_each_possible_cpu(cpu) |
596 | unregister_hw_breakpoint(per_cpu(*cpu_events, cpu)); | |
597 | ||
24f1e32c | 598 | free_percpu(cpu_events); |
62a038d3 | 599 | } |
f60d24d2 | 600 | EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint); |
62a038d3 P |
601 | |
602 | static struct notifier_block hw_breakpoint_exceptions_nb = { | |
603 | .notifier_call = hw_breakpoint_exceptions_notify, | |
604 | /* we need to be notified first */ | |
605 | .priority = 0x7fffffff | |
606 | }; | |
607 | ||
b0a873eb PZ |
608 | static void bp_perf_event_destroy(struct perf_event *event) |
609 | { | |
610 | release_bp_slot(event); | |
611 | } | |
612 | ||
613 | static int hw_breakpoint_event_init(struct perf_event *bp) | |
614 | { | |
615 | int err; | |
616 | ||
617 | if (bp->attr.type != PERF_TYPE_BREAKPOINT) | |
618 | return -ENOENT; | |
619 | ||
2481c5fa SE |
620 | /* |
621 | * no branch sampling for breakpoint events | |
622 | */ | |
623 | if (has_branch_stack(bp)) | |
624 | return -EOPNOTSUPP; | |
625 | ||
b0a873eb PZ |
626 | err = register_perf_hw_breakpoint(bp); |
627 | if (err) | |
628 | return err; | |
629 | ||
630 | bp->destroy = bp_perf_event_destroy; | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
a4eaf7f1 PZ |
635 | static int hw_breakpoint_add(struct perf_event *bp, int flags) |
636 | { | |
637 | if (!(flags & PERF_EF_START)) | |
638 | bp->hw.state = PERF_HES_STOPPED; | |
639 | ||
ab573844 JO |
640 | if (is_sampling_event(bp)) { |
641 | bp->hw.last_period = bp->hw.sample_period; | |
642 | perf_swevent_set_period(bp); | |
643 | } | |
644 | ||
a4eaf7f1 PZ |
645 | return arch_install_hw_breakpoint(bp); |
646 | } | |
647 | ||
648 | static void hw_breakpoint_del(struct perf_event *bp, int flags) | |
649 | { | |
650 | arch_uninstall_hw_breakpoint(bp); | |
651 | } | |
652 | ||
653 | static void hw_breakpoint_start(struct perf_event *bp, int flags) | |
654 | { | |
655 | bp->hw.state = 0; | |
656 | } | |
657 | ||
658 | static void hw_breakpoint_stop(struct perf_event *bp, int flags) | |
659 | { | |
660 | bp->hw.state = PERF_HES_STOPPED; | |
661 | } | |
662 | ||
b0a873eb | 663 | static struct pmu perf_breakpoint = { |
89a1e187 PZ |
664 | .task_ctx_nr = perf_sw_context, /* could eventually get its own */ |
665 | ||
b0a873eb | 666 | .event_init = hw_breakpoint_event_init, |
a4eaf7f1 PZ |
667 | .add = hw_breakpoint_add, |
668 | .del = hw_breakpoint_del, | |
669 | .start = hw_breakpoint_start, | |
670 | .stop = hw_breakpoint_stop, | |
b0a873eb PZ |
671 | .read = hw_breakpoint_pmu_read, |
672 | }; | |
673 | ||
3c502e7a | 674 | int __init init_hw_breakpoint(void) |
62a038d3 | 675 | { |
feef47d0 FW |
676 | int cpu, err_cpu; |
677 | int i; | |
678 | ||
679 | for (i = 0; i < TYPE_MAX; i++) | |
680 | nr_slots[i] = hw_breakpoint_slots(i); | |
681 | ||
682 | for_each_possible_cpu(cpu) { | |
683 | for (i = 0; i < TYPE_MAX; i++) { | |
bde96030 ON |
684 | struct bp_cpuinfo *info = get_bp_info(cpu, i); |
685 | ||
686 | info->tsk_pinned = kcalloc(nr_slots[i], sizeof(int), | |
687 | GFP_KERNEL); | |
688 | if (!info->tsk_pinned) | |
feef47d0 FW |
689 | goto err_alloc; |
690 | } | |
691 | } | |
692 | ||
693 | constraints_initialized = 1; | |
694 | ||
2e80a82a | 695 | perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT); |
b0a873eb | 696 | |
62a038d3 | 697 | return register_die_notifier(&hw_breakpoint_exceptions_nb); |
feef47d0 FW |
698 | |
699 | err_alloc: | |
700 | for_each_possible_cpu(err_cpu) { | |
feef47d0 | 701 | for (i = 0; i < TYPE_MAX; i++) |
bde96030 | 702 | kfree(get_bp_info(err_cpu, i)->tsk_pinned); |
30ce2f7e NK |
703 | if (err_cpu == cpu) |
704 | break; | |
feef47d0 FW |
705 | } |
706 | ||
707 | return -ENOMEM; | |
62a038d3 | 708 | } |
24f1e32c FW |
709 | |
710 |