blob: f32320ac02fd8b79fef8c1333a4e167f27b4200e [file] [log] [blame]
Thomas Gleixner469eb322019-01-16 12:11:00 +01001// SPDX-License-Identifier: GPL-2.0+
K.Prasad62a038d2009-06-01 23:43:33 +05302/*
K.Prasad62a038d2009-06-01 23:43:33 +05303 * Copyright (C) 2007 Alan Stern
4 * Copyright (C) IBM Corporation, 2009
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +02006 *
7 * Thanks to Ingo Molnar for his many suggestions.
K.Prasadba6909b2009-11-23 21:17:13 +05308 *
9 * Authors: Alan Stern <stern@rowland.harvard.edu>
10 * K.Prasad <prasad@linux.vnet.ibm.com>
11 * Frederic Weisbecker <fweisbec@gmail.com>
K.Prasad62a038d2009-06-01 23:43:33 +053012 */
13
14/*
15 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
16 * using the CPU's debug registers.
17 * This file contains the arch-independent routines.
18 */
19
20#include <linux/irqflags.h>
21#include <linux/kallsyms.h>
22#include <linux/notifier.h>
23#include <linux/kprobes.h>
24#include <linux/kdebug.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/percpu.h>
28#include <linux/sched.h>
29#include <linux/init.h>
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +020030#include <linux/slab.h>
Frederic Weisbecker45a73372010-06-23 23:00:37 +020031#include <linux/list.h>
Li Zefan88f7a8902009-12-30 14:22:22 +080032#include <linux/cpu.h>
K.Prasad62a038d2009-06-01 23:43:33 +053033#include <linux/smp.h>
Jiri Olsaea6a9d52018-03-12 14:45:43 +010034#include <linux/bug.h>
K.Prasad62a038d2009-06-01 23:43:33 +053035
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020036#include <linux/hw_breakpoint.h>
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020037/*
38 * Constraints data
39 */
Oleg Nesterovbde96032013-06-20 17:50:20 +020040struct bp_cpuinfo {
41 /* Number of pinned cpu breakpoints in a cpu */
42 unsigned int cpu_pinned;
43 /* tsk_pinned[n] is the number of tasks having n+1 breakpoints */
44 unsigned int *tsk_pinned;
45 /* Number of non-pinned cpu/task breakpoints in a cpu */
46 unsigned int flexible; /* XXX: placeholder, see fetch_this_slot() */
47};
K.Prasad62a038d2009-06-01 23:43:33 +053048
Oleg Nesterovbde96032013-06-20 17:50:20 +020049static DEFINE_PER_CPU(struct bp_cpuinfo, bp_cpuinfo[TYPE_MAX]);
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +020050static int nr_slots[TYPE_MAX];
51
Oleg Nesterovbde96032013-06-20 17:50:20 +020052static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type)
53{
54 return per_cpu_ptr(bp_cpuinfo + type, cpu);
55}
56
Frederic Weisbecker45a73372010-06-23 23:00:37 +020057/* Keep track of the breakpoints attached to tasks */
58static LIST_HEAD(bp_task_head);
59
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +020060static int constraints_initialized;
61
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020062/* Gather the number of total pinned and un-pinned bp in a cpuset */
63struct bp_busy_slots {
64 unsigned int pinned;
65 unsigned int flexible;
66};
67
68/* Serialize accesses to the above constraints */
69static DEFINE_MUTEX(nr_bp_mutex);
70
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +020071__weak int hw_breakpoint_weight(struct perf_event *bp)
72{
73 return 1;
74}
75
Jiri Olsacbd9d9f2018-03-12 14:45:41 +010076static inline enum bp_type_idx find_slot_idx(u64 bp_type)
Frederic Weisbecker01027522010-04-11 18:55:56 +020077{
Jiri Olsacbd9d9f2018-03-12 14:45:41 +010078 if (bp_type & HW_BREAKPOINT_RW)
Frederic Weisbecker01027522010-04-11 18:55:56 +020079 return TYPE_DATA;
80
81 return TYPE_INST;
82}
83
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020084/*
85 * Report the maximum number of pinned breakpoints a task
86 * have in this cpu
87 */
Frederic Weisbecker01027522010-04-11 18:55:56 +020088static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
K.Prasad62a038d2009-06-01 23:43:33 +053089{
Oleg Nesterovbde96032013-06-20 17:50:20 +020090 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020091 int i;
K.Prasad62a038d2009-06-01 23:43:33 +053092
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +020093 for (i = nr_slots[type] - 1; i >= 0; i--) {
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020094 if (tsk_pinned[i] > 0)
95 return i + 1;
K.Prasad62a038d2009-06-01 23:43:33 +053096 }
97
K.Prasad62a038d2009-06-01 23:43:33 +053098 return 0;
99}
100
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200101/*
102 * Count the number of breakpoints of the same type and same task.
103 * The given event must be not on the list.
104 */
Michael Neuling0d855352012-10-26 18:28:56 +0200105static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type)
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200106{
Peter Zijlstra50f16a82015-03-05 22:10:19 +0100107 struct task_struct *tsk = bp->hw.target;
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200108 struct perf_event *iter;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100109 int count = 0;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200110
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200111 list_for_each_entry(iter, &bp_task_head, hw.bp_list) {
Peter Zijlstra50f16a82015-03-05 22:10:19 +0100112 if (iter->hw.target == tsk &&
Jiri Olsacbd9d9f2018-03-12 14:45:41 +0100113 find_slot_idx(iter->attr.bp_type) == type &&
Oleg Nesterov8b4d8012013-06-20 17:50:06 +0200114 (iter->cpu < 0 || cpu == iter->cpu))
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200115 count += hw_breakpoint_weight(iter);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200116 }
117
Frederic Weisbecker56053172009-12-07 06:46:48 +0100118 return count;
119}
120
Oleg Nesterov1c10adb2013-06-20 17:50:15 +0200121static const struct cpumask *cpumask_of_bp(struct perf_event *bp)
122{
123 if (bp->cpu >= 0)
124 return cpumask_of(bp->cpu);
125 return cpu_possible_mask;
126}
127
Frederic Weisbecker56053172009-12-07 06:46:48 +0100128/*
129 * Report the number of pinned/un-pinned breakpoints we have in
130 * a given cpu (cpu > -1) or in all of them (cpu = -1).
131 */
132static void
Frederic Weisbecker01027522010-04-11 18:55:56 +0200133fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp,
134 enum bp_type_idx type)
Frederic Weisbecker56053172009-12-07 06:46:48 +0100135{
Oleg Nesterov1c10adb2013-06-20 17:50:15 +0200136 const struct cpumask *cpumask = cpumask_of_bp(bp);
137 int cpu;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100138
Oleg Nesterov1c10adb2013-06-20 17:50:15 +0200139 for_each_cpu(cpu, cpumask) {
Oleg Nesterovbde96032013-06-20 17:50:20 +0200140 struct bp_cpuinfo *info = get_bp_info(cpu, type);
141 int nr;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100142
Oleg Nesterovbde96032013-06-20 17:50:20 +0200143 nr = info->cpu_pinned;
Peter Zijlstra50f16a82015-03-05 22:10:19 +0100144 if (!bp->hw.target)
Frederic Weisbecker01027522010-04-11 18:55:56 +0200145 nr += max_task_bp_pinned(cpu, type);
Frederic Weisbecker56053172009-12-07 06:46:48 +0100146 else
Michael Neuling0d855352012-10-26 18:28:56 +0200147 nr += task_bp_pinned(cpu, bp, type);
Frederic Weisbecker56053172009-12-07 06:46:48 +0100148
149 if (nr > slots->pinned)
150 slots->pinned = nr;
151
Oleg Nesterovbde96032013-06-20 17:50:20 +0200152 nr = info->flexible;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100153 if (nr > slots->flexible)
154 slots->flexible = nr;
155 }
156}
157
158/*
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200159 * For now, continue to consider flexible as pinned, until we can
160 * ensure no flexible event can ever be scheduled before a pinned event
161 * in a same cpu.
162 */
163static void
164fetch_this_slot(struct bp_busy_slots *slots, int weight)
165{
166 slots->pinned += weight;
167}
168
169/*
Frederic Weisbecker56053172009-12-07 06:46:48 +0100170 * Add a pinned breakpoint for the given task in our constraint table
171 */
Oleg Nesterov7ab71f32013-06-20 17:50:13 +0200172static void toggle_bp_task_slot(struct perf_event *bp, int cpu,
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200173 enum bp_type_idx type, int weight)
Frederic Weisbecker56053172009-12-07 06:46:48 +0100174{
Oleg Nesterovbde96032013-06-20 17:50:20 +0200175 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
Oleg Nesterove1ebe862013-06-20 17:50:11 +0200176 int old_idx, new_idx;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100177
Oleg Nesterove1ebe862013-06-20 17:50:11 +0200178 old_idx = task_bp_pinned(cpu, bp, type) - 1;
Oleg Nesterov7ab71f32013-06-20 17:50:13 +0200179 new_idx = old_idx + weight;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200180
Oleg Nesterove1ebe862013-06-20 17:50:11 +0200181 if (old_idx >= 0)
182 tsk_pinned[old_idx]--;
183 if (new_idx >= 0)
184 tsk_pinned[new_idx]++;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200185}
186
187/*
188 * Add/remove the given breakpoint in our constraint table
189 */
Frederic Weisbecker01027522010-04-11 18:55:56 +0200190static void
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200191toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type,
192 int weight)
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200193{
Oleg Nesterov1c10adb2013-06-20 17:50:15 +0200194 const struct cpumask *cpumask = cpumask_of_bp(bp);
195 int cpu;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200196
Oleg Nesterov7ab71f32013-06-20 17:50:13 +0200197 if (!enable)
198 weight = -weight;
199
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200200 /* Pinned counter cpu profiling */
Peter Zijlstra50f16a82015-03-05 22:10:19 +0100201 if (!bp->hw.target) {
Oleg Nesterovbde96032013-06-20 17:50:20 +0200202 get_bp_info(bp->cpu, type)->cpu_pinned += weight;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200203 return;
204 }
205
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200206 /* Pinned counter task profiling */
Oleg Nesterov1c10adb2013-06-20 17:50:15 +0200207 for_each_cpu(cpu, cpumask)
Oleg Nesterov7ab71f32013-06-20 17:50:13 +0200208 toggle_bp_task_slot(bp, cpu, type, weight);
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200209
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200210 if (enable)
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200211 list_add_tail(&bp->hw.bp_list, &bp_task_head);
Oleg Nesterove1ebe862013-06-20 17:50:11 +0200212 else
213 list_del(&bp->hw.bp_list);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200214}
215
Ravi Bangoria29da4f92020-05-14 16:47:39 +0530216__weak int arch_reserve_bp_slot(struct perf_event *bp)
217{
218 return 0;
219}
220
221__weak void arch_release_bp_slot(struct perf_event *bp)
222{
223}
224
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200225/*
K.Prasadf7136c52010-06-15 11:34:34 +0530226 * Function to perform processor-specific cleanup during unregistration
227 */
228__weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
229{
230 /*
231 * A weak stub function here for those archs that don't define
232 * it inside arch/.../kernel/hw_breakpoint.c
233 */
234}
235
236/*
Ingo Molnarfca0c112018-12-03 10:52:21 +0100237 * Constraints to check before allowing this new breakpoint counter:
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200238 *
239 * == Non-pinned counter == (Considered as pinned for now)
240 *
241 * - If attached to a single cpu, check:
242 *
Oleg Nesterovbde96032013-06-20 17:50:20 +0200243 * (per_cpu(info->flexible, cpu) || (per_cpu(info->cpu_pinned, cpu)
244 * + max(per_cpu(info->tsk_pinned, cpu)))) < HBP_NUM
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200245 *
246 * -> If there are already non-pinned counters in this cpu, it means
247 * there is already a free slot for them.
248 * Otherwise, we check that the maximum number of per task
249 * breakpoints (for this cpu) plus the number of per cpu breakpoint
250 * (for this cpu) doesn't cover every registers.
251 *
252 * - If attached to every cpus, check:
253 *
Oleg Nesterovbde96032013-06-20 17:50:20 +0200254 * (per_cpu(info->flexible, *) || (max(per_cpu(info->cpu_pinned, *))
255 * + max(per_cpu(info->tsk_pinned, *)))) < HBP_NUM
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200256 *
257 * -> This is roughly the same, except we check the number of per cpu
258 * bp for every cpu and we keep the max one. Same for the per tasks
259 * breakpoints.
260 *
261 *
262 * == Pinned counter ==
263 *
264 * - If attached to a single cpu, check:
265 *
Oleg Nesterovbde96032013-06-20 17:50:20 +0200266 * ((per_cpu(info->flexible, cpu) > 1) + per_cpu(info->cpu_pinned, cpu)
267 * + max(per_cpu(info->tsk_pinned, cpu))) < HBP_NUM
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200268 *
Oleg Nesterovbde96032013-06-20 17:50:20 +0200269 * -> Same checks as before. But now the info->flexible, if any, must keep
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200270 * one register at least (or they will never be fed).
271 *
272 * - If attached to every cpus, check:
273 *
Oleg Nesterovbde96032013-06-20 17:50:20 +0200274 * ((per_cpu(info->flexible, *) > 1) + max(per_cpu(info->cpu_pinned, *))
275 * + max(per_cpu(info->tsk_pinned, *))) < HBP_NUM
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200276 */
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100277static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type)
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200278{
279 struct bp_busy_slots slots = {0};
Frederic Weisbecker01027522010-04-11 18:55:56 +0200280 enum bp_type_idx type;
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200281 int weight;
Ravi Bangoria29da4f92020-05-14 16:47:39 +0530282 int ret;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200283
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200284 /* We couldn't initialize breakpoint constraints on boot */
285 if (!constraints_initialized)
286 return -ENOMEM;
287
Frederic Weisbecker01027522010-04-11 18:55:56 +0200288 /* Basic checks */
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100289 if (bp_type == HW_BREAKPOINT_EMPTY ||
290 bp_type == HW_BREAKPOINT_INVALID)
Frederic Weisbecker01027522010-04-11 18:55:56 +0200291 return -EINVAL;
292
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100293 type = find_slot_idx(bp_type);
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200294 weight = hw_breakpoint_weight(bp);
295
Frederic Weisbecker01027522010-04-11 18:55:56 +0200296 fetch_bp_busy_slots(&slots, bp, type);
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200297 /*
298 * Simulate the addition of this breakpoint to the constraints
299 * and see the result.
300 */
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200301 fetch_this_slot(&slots, weight);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200302
303 /* Flexible counters need to keep at least one slot */
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200304 if (slots.pinned + (!!slots.flexible) > nr_slots[type])
Jason Wessel5352ae632010-01-28 17:04:43 -0600305 return -ENOSPC;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200306
Ravi Bangoria29da4f92020-05-14 16:47:39 +0530307 ret = arch_reserve_bp_slot(bp);
308 if (ret)
309 return ret;
310
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200311 toggle_bp_slot(bp, true, type, weight);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200312
Jason Wessel5352ae632010-01-28 17:04:43 -0600313 return 0;
314}
315
316int reserve_bp_slot(struct perf_event *bp)
317{
318 int ret;
319
320 mutex_lock(&nr_bp_mutex);
321
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100322 ret = __reserve_bp_slot(bp, bp->attr.bp_type);
Jason Wessel5352ae632010-01-28 17:04:43 -0600323
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200324 mutex_unlock(&nr_bp_mutex);
325
326 return ret;
327}
328
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100329static void __release_bp_slot(struct perf_event *bp, u64 bp_type)
Jason Wessel5352ae632010-01-28 17:04:43 -0600330{
Frederic Weisbecker01027522010-04-11 18:55:56 +0200331 enum bp_type_idx type;
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200332 int weight;
Frederic Weisbecker01027522010-04-11 18:55:56 +0200333
Ravi Bangoria29da4f92020-05-14 16:47:39 +0530334 arch_release_bp_slot(bp);
335
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100336 type = find_slot_idx(bp_type);
Frederic Weisbeckerf93a2052010-04-13 00:32:30 +0200337 weight = hw_breakpoint_weight(bp);
338 toggle_bp_slot(bp, false, type, weight);
Jason Wessel5352ae632010-01-28 17:04:43 -0600339}
340
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200341void release_bp_slot(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530342{
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200343 mutex_lock(&nr_bp_mutex);
344
K.Prasadf7136c52010-06-15 11:34:34 +0530345 arch_unregister_hw_breakpoint(bp);
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100346 __release_bp_slot(bp, bp->attr.bp_type);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200347
348 mutex_unlock(&nr_bp_mutex);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200349}
K.Prasad62a038d2009-06-01 23:43:33 +0530350
Frederic Weisbeckercb8b7882018-06-26 04:58:58 +0200351static int __modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
Jiri Olsaea6a9d52018-03-12 14:45:43 +0100352{
353 int err;
354
355 __release_bp_slot(bp, old_type);
356
Frederic Weisbeckercb8b7882018-06-26 04:58:58 +0200357 err = __reserve_bp_slot(bp, new_type);
Jiri Olsaea6a9d52018-03-12 14:45:43 +0100358 if (err) {
359 /*
360 * Reserve the old_type slot back in case
361 * there's no space for the new type.
362 *
363 * This must succeed, because we just released
364 * the old_type slot in the __release_bp_slot
365 * call above. If not, something is broken.
366 */
367 WARN_ON(__reserve_bp_slot(bp, old_type));
368 }
369
370 return err;
371}
372
Frederic Weisbeckercb8b7882018-06-26 04:58:58 +0200373static int modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
Jiri Olsaea6a9d52018-03-12 14:45:43 +0100374{
375 int ret;
376
377 mutex_lock(&nr_bp_mutex);
Frederic Weisbeckercb8b7882018-06-26 04:58:58 +0200378 ret = __modify_bp_slot(bp, old_type, new_type);
Jiri Olsaea6a9d52018-03-12 14:45:43 +0100379 mutex_unlock(&nr_bp_mutex);
380 return ret;
381}
382
Jason Wessel5352ae632010-01-28 17:04:43 -0600383/*
384 * Allow the kernel debugger to reserve breakpoint slots without
385 * taking a lock using the dbg_* variant of for the reserve and
386 * release breakpoint slots.
387 */
388int dbg_reserve_bp_slot(struct perf_event *bp)
389{
390 if (mutex_is_locked(&nr_bp_mutex))
391 return -1;
392
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100393 return __reserve_bp_slot(bp, bp->attr.bp_type);
Jason Wessel5352ae632010-01-28 17:04:43 -0600394}
395
396int dbg_release_bp_slot(struct perf_event *bp)
397{
398 if (mutex_is_locked(&nr_bp_mutex))
399 return -1;
400
Jiri Olsa1ad9ff72018-03-12 14:45:42 +0100401 __release_bp_slot(bp, bp->attr.bp_type);
Jason Wessel5352ae632010-01-28 17:04:43 -0600402
403 return 0;
404}
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200405
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200406static int hw_breakpoint_parse(struct perf_event *bp,
407 const struct perf_event_attr *attr,
408 struct arch_hw_breakpoint *hw)
409{
410 int err;
411
412 err = hw_breakpoint_arch_parse(bp, attr, hw);
413 if (err)
414 return err;
Frederic Weisbeckerb2812d02010-04-18 18:11:53 +0200415
Frederic Weisbecker8e983ff2018-06-26 04:58:49 +0200416 if (arch_check_bp_in_kernelspace(hw)) {
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200417 if (attr->exclude_kernel)
Frederic Weisbeckerb2812d02010-04-18 18:11:53 +0200418 return -EINVAL;
419 /*
420 * Don't let unprivileged users set a breakpoint in the trap
421 * path to avoid trap recursion attacks.
422 */
423 if (!capable(CAP_SYS_ADMIN))
424 return -EPERM;
425 }
426
427 return 0;
428}
429
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100430int register_perf_hw_breakpoint(struct perf_event *bp)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200431{
Mark-PK Tsai310aa0a2019-09-06 14:01:16 +0800432 struct arch_hw_breakpoint hw = { };
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200433 int err;
K.Prasad62a038d2009-06-01 23:43:33 +0530434
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200435 err = reserve_bp_slot(bp);
436 if (err)
437 return err;
K.Prasad62a038d2009-06-01 23:43:33 +0530438
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200439 err = hw_breakpoint_parse(bp, &bp->attr, &hw);
440 if (err) {
Mahesh Salgaonkarb23ff0e2010-01-21 18:25:16 +0530441 release_bp_slot(bp);
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200442 return err;
443 }
Mahesh Salgaonkarb23ff0e2010-01-21 18:25:16 +0530444
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200445 bp->hw.info = hw;
446
447 return 0;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200448}
K.Prasad62a038d2009-06-01 23:43:33 +0530449
K.Prasad62a038d2009-06-01 23:43:33 +0530450/**
451 * register_user_hw_breakpoint - register a hardware breakpoint for user space
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100452 * @attr: breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200453 * @triggered: callback to trigger when we hit the breakpoint
Haocheng Xie875dd7b2021-05-27 11:19:47 +0800454 * @context: context data could be used in the triggered callback
K.Prasad62a038d2009-06-01 23:43:33 +0530455 * @tsk: pointer to 'task_struct' of the process to which the address belongs
K.Prasad62a038d2009-06-01 23:43:33 +0530456 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200457struct perf_event *
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100458register_user_hw_breakpoint(struct perf_event_attr *attr,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100459 perf_overflow_handler_t triggered,
Avi Kivity4dc0da82011-06-29 18:42:35 +0300460 void *context,
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100461 struct task_struct *tsk)
K.Prasad62a038d2009-06-01 23:43:33 +0530462{
Avi Kivity4dc0da82011-06-29 18:42:35 +0300463 return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
464 context);
K.Prasad62a038d2009-06-01 23:43:33 +0530465}
466EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
467
Frederic Weisbecker26c6ccd2018-06-26 04:58:59 +0200468static void hw_breakpoint_copy_attr(struct perf_event_attr *to,
469 struct perf_event_attr *from)
470{
471 to->bp_addr = from->bp_addr;
472 to->bp_type = from->bp_type;
473 to->bp_len = from->bp_len;
474 to->disabled = from->disabled;
475}
476
Milind Chabbi32ff77e2018-03-12 14:45:47 +0100477int
Jiri Olsa705feaf2018-03-12 14:45:45 +0100478modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
479 bool check)
Jiri Olsa18ff57b2018-03-12 14:45:44 +0100480{
Mark-PK Tsai310aa0a2019-09-06 14:01:16 +0800481 struct arch_hw_breakpoint hw = { };
Frederic Weisbecker26c6ccd2018-06-26 04:58:59 +0200482 int err;
Jiri Olsa705feaf2018-03-12 14:45:45 +0100483
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200484 err = hw_breakpoint_parse(bp, attr, &hw);
Frederic Weisbecker26c6ccd2018-06-26 04:58:59 +0200485 if (err)
Jiri Olsa18ff57b2018-03-12 14:45:44 +0100486 return err;
Frederic Weisbecker26c6ccd2018-06-26 04:58:59 +0200487
488 if (check) {
489 struct perf_event_attr old_attr;
490
491 old_attr = bp->attr;
492 hw_breakpoint_copy_attr(&old_attr, attr);
493 if (memcmp(&old_attr, attr, sizeof(*attr)))
494 return -EINVAL;
Jiri Olsa18ff57b2018-03-12 14:45:44 +0100495 }
496
Frederic Weisbecker26c6ccd2018-06-26 04:58:59 +0200497 if (bp->attr.bp_type != attr->bp_type) {
498 err = modify_bp_slot(bp, bp->attr.bp_type, attr->bp_type);
499 if (err)
500 return err;
501 }
502
503 hw_breakpoint_copy_attr(&bp->attr, attr);
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200504 bp->hw.info = hw;
Frederic Weisbecker9a4903d2018-06-26 04:58:48 +0200505
Jiri Olsa18ff57b2018-03-12 14:45:44 +0100506 return 0;
507}
508
K.Prasad62a038d2009-06-01 23:43:33 +0530509/**
510 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200511 * @bp: the breakpoint structure to modify
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100512 * @attr: new breakpoint attributes
K.Prasad62a038d2009-06-01 23:43:33 +0530513 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100514int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
K.Prasad62a038d2009-06-01 23:43:33 +0530515{
Jiri Olsabd144062018-08-27 11:12:25 +0200516 int err;
517
K.Prasad500ad2d2012-08-02 13:46:35 +0530518 /*
519 * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it
520 * will not be possible to raise IPIs that invoke __perf_event_disable.
521 * So call the function directly after making sure we are targeting the
522 * current task.
523 */
524 if (irqs_disabled() && bp->ctx && bp->ctx->task == current)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100525 perf_event_disable_local(bp);
K.Prasad500ad2d2012-08-02 13:46:35 +0530526 else
527 perf_event_disable(bp);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100528
Jiri Olsabd144062018-08-27 11:12:25 +0200529 err = modify_user_hw_breakpoint_check(bp, attr, false);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100530
Jiri Olsa96955832018-08-27 11:12:27 +0200531 if (!bp->attr.disabled)
Jiri Olsaea6a9d52018-03-12 14:45:43 +0100532 perf_event_enable(bp);
Jiri Olsacb453022018-08-27 11:12:26 +0200533
Jiri Olsa96955832018-08-27 11:12:27 +0200534 return err;
K.Prasad62a038d2009-06-01 23:43:33 +0530535}
536EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
537
538/**
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200539 * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530540 * @bp: the breakpoint structure to unregister
K.Prasad62a038d2009-06-01 23:43:33 +0530541 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200542void unregister_hw_breakpoint(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530543{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200544 if (!bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530545 return;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200546 perf_event_release_kernel(bp);
547}
548EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
549
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200550/**
551 * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100552 * @attr: breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200553 * @triggered: callback to trigger when we hit the breakpoint
Haocheng Xie875dd7b2021-05-27 11:19:47 +0800554 * @context: context data could be used in the triggered callback
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200555 *
556 * @return a set of per_cpu pointers to perf events
557 */
Tejun Heo44ee6352010-02-17 10:50:50 +0900558struct perf_event * __percpu *
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100559register_wide_hw_breakpoint(struct perf_event_attr *attr,
Avi Kivity4dc0da82011-06-29 18:42:35 +0300560 perf_overflow_handler_t triggered,
561 void *context)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200562{
Oleg Nesterove12cbc12013-06-20 17:50:18 +0200563 struct perf_event * __percpu *cpu_events, *bp;
564 long err = 0;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200565 int cpu;
566
567 cpu_events = alloc_percpu(typeof(*cpu_events));
568 if (!cpu_events)
Tejun Heo44ee6352010-02-17 10:50:50 +0900569 return (void __percpu __force *)ERR_PTR(-ENOMEM);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200570
Sebastian Andrzej Siewiorffec09f2021-08-03 16:15:54 +0200571 cpus_read_lock();
Li Zefan88f7a8902009-12-30 14:22:22 +0800572 for_each_online_cpu(cpu) {
Avi Kivity4dc0da82011-06-29 18:42:35 +0300573 bp = perf_event_create_kernel_counter(attr, cpu, NULL,
574 triggered, context);
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100575 if (IS_ERR(bp)) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200576 err = PTR_ERR(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200577 break;
Oleg Nesterove12cbc12013-06-20 17:50:18 +0200578 }
579
580 per_cpu(*cpu_events, cpu) = bp;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200581 }
Sebastian Andrzej Siewiorffec09f2021-08-03 16:15:54 +0200582 cpus_read_unlock();
Li Zefan88f7a8902009-12-30 14:22:22 +0800583
Oleg Nesterove12cbc12013-06-20 17:50:18 +0200584 if (likely(!err))
585 return cpu_events;
586
587 unregister_wide_hw_breakpoint(cpu_events);
Tejun Heo44ee6352010-02-17 10:50:50 +0900588 return (void __percpu __force *)ERR_PTR(err);
K.Prasad62a038d2009-06-01 23:43:33 +0530589}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100590EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200591
592/**
593 * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
594 * @cpu_events: the per cpu set of events to unregister
595 */
Tejun Heo44ee6352010-02-17 10:50:50 +0900596void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200597{
598 int cpu;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200599
Oleg Nesterove12cbc12013-06-20 17:50:18 +0200600 for_each_possible_cpu(cpu)
601 unregister_hw_breakpoint(per_cpu(*cpu_events, cpu));
602
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200603 free_percpu(cpu_events);
604}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100605EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
K.Prasad62a038d2009-06-01 23:43:33 +0530606
607static struct notifier_block hw_breakpoint_exceptions_nb = {
608 .notifier_call = hw_breakpoint_exceptions_notify,
609 /* we need to be notified first */
610 .priority = 0x7fffffff
611};
612
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200613static void bp_perf_event_destroy(struct perf_event *event)
614{
615 release_bp_slot(event);
616}
617
618static int hw_breakpoint_event_init(struct perf_event *bp)
619{
620 int err;
621
622 if (bp->attr.type != PERF_TYPE_BREAKPOINT)
623 return -ENOENT;
624
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100625 /*
626 * no branch sampling for breakpoint events
627 */
628 if (has_branch_stack(bp))
629 return -EOPNOTSUPP;
630
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200631 err = register_perf_hw_breakpoint(bp);
632 if (err)
633 return err;
634
635 bp->destroy = bp_perf_event_destroy;
636
637 return 0;
638}
639
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200640static int hw_breakpoint_add(struct perf_event *bp, int flags)
641{
642 if (!(flags & PERF_EF_START))
643 bp->hw.state = PERF_HES_STOPPED;
644
Jiri Olsaab573842013-05-01 17:25:44 +0200645 if (is_sampling_event(bp)) {
646 bp->hw.last_period = bp->hw.sample_period;
647 perf_swevent_set_period(bp);
648 }
649
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200650 return arch_install_hw_breakpoint(bp);
651}
652
653static void hw_breakpoint_del(struct perf_event *bp, int flags)
654{
655 arch_uninstall_hw_breakpoint(bp);
656}
657
658static void hw_breakpoint_start(struct perf_event *bp, int flags)
659{
660 bp->hw.state = 0;
661}
662
663static void hw_breakpoint_stop(struct perf_event *bp, int flags)
664{
665 bp->hw.state = PERF_HES_STOPPED;
666}
667
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200668static struct pmu perf_breakpoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +0200669 .task_ctx_nr = perf_sw_context, /* could eventually get its own */
670
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200671 .event_init = hw_breakpoint_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200672 .add = hw_breakpoint_add,
673 .del = hw_breakpoint_del,
674 .start = hw_breakpoint_start,
675 .stop = hw_breakpoint_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200676 .read = hw_breakpoint_pmu_read,
677};
678
Jason Wessel3c502e72010-11-04 17:33:01 -0500679int __init init_hw_breakpoint(void)
K.Prasad62a038d2009-06-01 23:43:33 +0530680{
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200681 int cpu, err_cpu;
682 int i;
683
684 for (i = 0; i < TYPE_MAX; i++)
685 nr_slots[i] = hw_breakpoint_slots(i);
686
687 for_each_possible_cpu(cpu) {
688 for (i = 0; i < TYPE_MAX; i++) {
Oleg Nesterovbde96032013-06-20 17:50:20 +0200689 struct bp_cpuinfo *info = get_bp_info(cpu, i);
690
691 info->tsk_pinned = kcalloc(nr_slots[i], sizeof(int),
692 GFP_KERNEL);
693 if (!info->tsk_pinned)
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200694 goto err_alloc;
695 }
696 }
697
698 constraints_initialized = 1;
699
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100700 perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200701
K.Prasad62a038d2009-06-01 23:43:33 +0530702 return register_die_notifier(&hw_breakpoint_exceptions_nb);
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200703
704 err_alloc:
705 for_each_possible_cpu(err_cpu) {
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200706 for (i = 0; i < TYPE_MAX; i++)
Oleg Nesterovbde96032013-06-20 17:50:20 +0200707 kfree(get_bp_info(err_cpu, i)->tsk_pinned);
Namhyung Kim30ce2f72012-02-28 10:19:38 +0900708 if (err_cpu == cpu)
709 break;
Frederic Weisbeckerfeef47d2010-04-23 05:59:55 +0200710 }
711
712 return -ENOMEM;
K.Prasad62a038d2009-06-01 23:43:33 +0530713}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200714
715