]>
Commit | Line | Data |
---|---|---|
0793a61d | 1 | /* |
57c0c15b | 2 | * Performance events core code: |
0793a61d | 3 | * |
98144511 | 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
e7e7ee2e | 5 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
90eec103 | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
d36b6910 | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
7b732a75 | 8 | * |
57c0c15b | 9 | * For licensing details see kernel-base/COPYING |
0793a61d TG |
10 | */ |
11 | ||
12 | #include <linux/fs.h> | |
b9cacc7b | 13 | #include <linux/mm.h> |
0793a61d TG |
14 | #include <linux/cpu.h> |
15 | #include <linux/smp.h> | |
2e80a82a | 16 | #include <linux/idr.h> |
04289bb9 | 17 | #include <linux/file.h> |
0793a61d | 18 | #include <linux/poll.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
76e1d904 | 20 | #include <linux/hash.h> |
12351ef8 | 21 | #include <linux/tick.h> |
0793a61d | 22 | #include <linux/sysfs.h> |
22a4f650 | 23 | #include <linux/dcache.h> |
0793a61d | 24 | #include <linux/percpu.h> |
22a4f650 | 25 | #include <linux/ptrace.h> |
c277443c | 26 | #include <linux/reboot.h> |
b9cacc7b | 27 | #include <linux/vmstat.h> |
abe43400 | 28 | #include <linux/device.h> |
6e5fdeed | 29 | #include <linux/export.h> |
906010b2 | 30 | #include <linux/vmalloc.h> |
b9cacc7b PZ |
31 | #include <linux/hardirq.h> |
32 | #include <linux/rculist.h> | |
0793a61d TG |
33 | #include <linux/uaccess.h> |
34 | #include <linux/syscalls.h> | |
35 | #include <linux/anon_inodes.h> | |
aa9c4c0f | 36 | #include <linux/kernel_stat.h> |
39bed6cb | 37 | #include <linux/cgroup.h> |
cdd6c482 | 38 | #include <linux/perf_event.h> |
af658dca | 39 | #include <linux/trace_events.h> |
3c502e7a | 40 | #include <linux/hw_breakpoint.h> |
c5ebcedb | 41 | #include <linux/mm_types.h> |
c464c76e | 42 | #include <linux/module.h> |
f972eb63 | 43 | #include <linux/mman.h> |
b3f20785 | 44 | #include <linux/compat.h> |
2541517c AS |
45 | #include <linux/bpf.h> |
46 | #include <linux/filter.h> | |
375637bc AS |
47 | #include <linux/namei.h> |
48 | #include <linux/parser.h> | |
e6017571 | 49 | #include <linux/sched/clock.h> |
6e84f315 | 50 | #include <linux/sched/mm.h> |
e4222673 HB |
51 | #include <linux/proc_ns.h> |
52 | #include <linux/mount.h> | |
0793a61d | 53 | |
76369139 FW |
54 | #include "internal.h" |
55 | ||
4e193bd4 TB |
56 | #include <asm/irq_regs.h> |
57 | ||
272325c4 PZ |
58 | typedef int (*remote_function_f)(void *); |
59 | ||
fe4b04fa | 60 | struct remote_function_call { |
e7e7ee2e | 61 | struct task_struct *p; |
272325c4 | 62 | remote_function_f func; |
e7e7ee2e IM |
63 | void *info; |
64 | int ret; | |
fe4b04fa PZ |
65 | }; |
66 | ||
67 | static void remote_function(void *data) | |
68 | { | |
69 | struct remote_function_call *tfc = data; | |
70 | struct task_struct *p = tfc->p; | |
71 | ||
72 | if (p) { | |
0da4cf3e PZ |
73 | /* -EAGAIN */ |
74 | if (task_cpu(p) != smp_processor_id()) | |
75 | return; | |
76 | ||
77 | /* | |
78 | * Now that we're on right CPU with IRQs disabled, we can test | |
79 | * if we hit the right task without races. | |
80 | */ | |
81 | ||
82 | tfc->ret = -ESRCH; /* No such (running) process */ | |
83 | if (p != current) | |
fe4b04fa PZ |
84 | return; |
85 | } | |
86 | ||
87 | tfc->ret = tfc->func(tfc->info); | |
88 | } | |
89 | ||
90 | /** | |
91 | * task_function_call - call a function on the cpu on which a task runs | |
92 | * @p: the task to evaluate | |
93 | * @func: the function to be called | |
94 | * @info: the function call argument | |
95 | * | |
96 | * Calls the function @func when the task is currently running. This might | |
97 | * be on the current CPU, which just calls the function directly | |
98 | * | |
99 | * returns: @func return value, or | |
100 | * -ESRCH - when the process isn't running | |
101 | * -EAGAIN - when the process moved away | |
102 | */ | |
103 | static int | |
272325c4 | 104 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
105 | { |
106 | struct remote_function_call data = { | |
e7e7ee2e IM |
107 | .p = p, |
108 | .func = func, | |
109 | .info = info, | |
0da4cf3e | 110 | .ret = -EAGAIN, |
fe4b04fa | 111 | }; |
0da4cf3e | 112 | int ret; |
fe4b04fa | 113 | |
0da4cf3e PZ |
114 | do { |
115 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); | |
116 | if (!ret) | |
117 | ret = data.ret; | |
118 | } while (ret == -EAGAIN); | |
fe4b04fa | 119 | |
0da4cf3e | 120 | return ret; |
fe4b04fa PZ |
121 | } |
122 | ||
123 | /** | |
124 | * cpu_function_call - call a function on the cpu | |
125 | * @func: the function to be called | |
126 | * @info: the function call argument | |
127 | * | |
128 | * Calls the function @func on the remote cpu. | |
129 | * | |
130 | * returns: @func return value or -ENXIO when the cpu is offline | |
131 | */ | |
272325c4 | 132 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
133 | { |
134 | struct remote_function_call data = { | |
e7e7ee2e IM |
135 | .p = NULL, |
136 | .func = func, | |
137 | .info = info, | |
138 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
139 | }; |
140 | ||
141 | smp_call_function_single(cpu, remote_function, &data, 1); | |
142 | ||
143 | return data.ret; | |
144 | } | |
145 | ||
fae3fde6 PZ |
146 | static inline struct perf_cpu_context * |
147 | __get_cpu_context(struct perf_event_context *ctx) | |
148 | { | |
149 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); | |
150 | } | |
151 | ||
152 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, | |
153 | struct perf_event_context *ctx) | |
0017960f | 154 | { |
fae3fde6 PZ |
155 | raw_spin_lock(&cpuctx->ctx.lock); |
156 | if (ctx) | |
157 | raw_spin_lock(&ctx->lock); | |
158 | } | |
159 | ||
160 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
161 | struct perf_event_context *ctx) | |
162 | { | |
163 | if (ctx) | |
164 | raw_spin_unlock(&ctx->lock); | |
165 | raw_spin_unlock(&cpuctx->ctx.lock); | |
166 | } | |
167 | ||
63b6da39 PZ |
168 | #define TASK_TOMBSTONE ((void *)-1L) |
169 | ||
170 | static bool is_kernel_event(struct perf_event *event) | |
171 | { | |
f47c02c0 | 172 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
173 | } |
174 | ||
39a43640 PZ |
175 | /* |
176 | * On task ctx scheduling... | |
177 | * | |
178 | * When !ctx->nr_events a task context will not be scheduled. This means | |
179 | * we can disable the scheduler hooks (for performance) without leaving | |
180 | * pending task ctx state. | |
181 | * | |
182 | * This however results in two special cases: | |
183 | * | |
184 | * - removing the last event from a task ctx; this is relatively straight | |
185 | * forward and is done in __perf_remove_from_context. | |
186 | * | |
187 | * - adding the first event to a task ctx; this is tricky because we cannot | |
188 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
189 | * See perf_install_in_context(). | |
190 | * | |
39a43640 PZ |
191 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
192 | */ | |
193 | ||
fae3fde6 PZ |
194 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
195 | struct perf_event_context *, void *); | |
196 | ||
197 | struct event_function_struct { | |
198 | struct perf_event *event; | |
199 | event_f func; | |
200 | void *data; | |
201 | }; | |
202 | ||
203 | static int event_function(void *info) | |
204 | { | |
205 | struct event_function_struct *efs = info; | |
206 | struct perf_event *event = efs->event; | |
0017960f | 207 | struct perf_event_context *ctx = event->ctx; |
fae3fde6 PZ |
208 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
209 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
63b6da39 | 210 | int ret = 0; |
fae3fde6 | 211 | |
16444645 | 212 | lockdep_assert_irqs_disabled(); |
fae3fde6 | 213 | |
63b6da39 | 214 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
215 | /* |
216 | * Since we do the IPI call without holding ctx->lock things can have | |
217 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
218 | */ |
219 | if (ctx->task) { | |
63b6da39 | 220 | if (ctx->task != current) { |
0da4cf3e | 221 | ret = -ESRCH; |
63b6da39 PZ |
222 | goto unlock; |
223 | } | |
fae3fde6 | 224 | |
fae3fde6 PZ |
225 | /* |
226 | * We only use event_function_call() on established contexts, | |
227 | * and event_function() is only ever called when active (or | |
228 | * rather, we'll have bailed in task_function_call() or the | |
229 | * above ctx->task != current test), therefore we must have | |
230 | * ctx->is_active here. | |
231 | */ | |
232 | WARN_ON_ONCE(!ctx->is_active); | |
233 | /* | |
234 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
235 | * match. | |
236 | */ | |
63b6da39 PZ |
237 | WARN_ON_ONCE(task_ctx != ctx); |
238 | } else { | |
239 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 240 | } |
63b6da39 | 241 | |
fae3fde6 | 242 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 243 | unlock: |
fae3fde6 PZ |
244 | perf_ctx_unlock(cpuctx, task_ctx); |
245 | ||
63b6da39 | 246 | return ret; |
fae3fde6 PZ |
247 | } |
248 | ||
fae3fde6 | 249 | static void event_function_call(struct perf_event *event, event_f func, void *data) |
0017960f PZ |
250 | { |
251 | struct perf_event_context *ctx = event->ctx; | |
63b6da39 | 252 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
fae3fde6 PZ |
253 | struct event_function_struct efs = { |
254 | .event = event, | |
255 | .func = func, | |
256 | .data = data, | |
257 | }; | |
0017960f | 258 | |
c97f4736 PZ |
259 | if (!event->parent) { |
260 | /* | |
261 | * If this is a !child event, we must hold ctx::mutex to | |
262 | * stabilize the the event->ctx relation. See | |
263 | * perf_event_ctx_lock(). | |
264 | */ | |
265 | lockdep_assert_held(&ctx->mutex); | |
266 | } | |
0017960f PZ |
267 | |
268 | if (!task) { | |
fae3fde6 | 269 | cpu_function_call(event->cpu, event_function, &efs); |
0017960f PZ |
270 | return; |
271 | } | |
272 | ||
63b6da39 PZ |
273 | if (task == TASK_TOMBSTONE) |
274 | return; | |
275 | ||
a096309b | 276 | again: |
fae3fde6 | 277 | if (!task_function_call(task, event_function, &efs)) |
0017960f PZ |
278 | return; |
279 | ||
280 | raw_spin_lock_irq(&ctx->lock); | |
63b6da39 PZ |
281 | /* |
282 | * Reload the task pointer, it might have been changed by | |
283 | * a concurrent perf_event_context_sched_out(). | |
284 | */ | |
285 | task = ctx->task; | |
a096309b PZ |
286 | if (task == TASK_TOMBSTONE) { |
287 | raw_spin_unlock_irq(&ctx->lock); | |
288 | return; | |
0017960f | 289 | } |
a096309b PZ |
290 | if (ctx->is_active) { |
291 | raw_spin_unlock_irq(&ctx->lock); | |
292 | goto again; | |
293 | } | |
294 | func(event, NULL, ctx, data); | |
0017960f PZ |
295 | raw_spin_unlock_irq(&ctx->lock); |
296 | } | |
297 | ||
cca20946 PZ |
298 | /* |
299 | * Similar to event_function_call() + event_function(), but hard assumes IRQs | |
300 | * are already disabled and we're on the right CPU. | |
301 | */ | |
302 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
303 | { | |
304 | struct perf_event_context *ctx = event->ctx; | |
305 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
306 | struct task_struct *task = READ_ONCE(ctx->task); | |
307 | struct perf_event_context *task_ctx = NULL; | |
308 | ||
16444645 | 309 | lockdep_assert_irqs_disabled(); |
cca20946 PZ |
310 | |
311 | if (task) { | |
312 | if (task == TASK_TOMBSTONE) | |
313 | return; | |
314 | ||
315 | task_ctx = ctx; | |
316 | } | |
317 | ||
318 | perf_ctx_lock(cpuctx, task_ctx); | |
319 | ||
320 | task = ctx->task; | |
321 | if (task == TASK_TOMBSTONE) | |
322 | goto unlock; | |
323 | ||
324 | if (task) { | |
325 | /* | |
326 | * We must be either inactive or active and the right task, | |
327 | * otherwise we're screwed, since we cannot IPI to somewhere | |
328 | * else. | |
329 | */ | |
330 | if (ctx->is_active) { | |
331 | if (WARN_ON_ONCE(task != current)) | |
332 | goto unlock; | |
333 | ||
334 | if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) | |
335 | goto unlock; | |
336 | } | |
337 | } else { | |
338 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
339 | } | |
340 | ||
341 | func(event, cpuctx, ctx, data); | |
342 | unlock: | |
343 | perf_ctx_unlock(cpuctx, task_ctx); | |
344 | } | |
345 | ||
e5d1367f SE |
346 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
347 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
348 | PERF_FLAG_PID_CGROUP |\ |
349 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 350 | |
bce38cd5 SE |
351 | /* |
352 | * branch priv levels that need permission checks | |
353 | */ | |
354 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
355 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
356 | PERF_SAMPLE_BRANCH_HV) | |
357 | ||
0b3fcf17 SE |
358 | enum event_type_t { |
359 | EVENT_FLEXIBLE = 0x1, | |
360 | EVENT_PINNED = 0x2, | |
3cbaa590 | 361 | EVENT_TIME = 0x4, |
487f05e1 AS |
362 | /* see ctx_resched() for details */ |
363 | EVENT_CPU = 0x8, | |
0b3fcf17 SE |
364 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
365 | }; | |
366 | ||
e5d1367f SE |
367 | /* |
368 | * perf_sched_events : >0 events exist | |
369 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu | |
370 | */ | |
9107c89e PZ |
371 | |
372 | static void perf_sched_delayed(struct work_struct *work); | |
373 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
374 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
375 | static DEFINE_MUTEX(perf_sched_mutex); | |
376 | static atomic_t perf_sched_count; | |
377 | ||
e5d1367f | 378 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
ba532500 | 379 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
f2fb6bef | 380 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
e5d1367f | 381 | |
cdd6c482 IM |
382 | static atomic_t nr_mmap_events __read_mostly; |
383 | static atomic_t nr_comm_events __read_mostly; | |
e4222673 | 384 | static atomic_t nr_namespaces_events __read_mostly; |
cdd6c482 | 385 | static atomic_t nr_task_events __read_mostly; |
948b26b6 | 386 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 387 | static atomic_t nr_switch_events __read_mostly; |
9ee318a7 | 388 | |
108b02cf PZ |
389 | static LIST_HEAD(pmus); |
390 | static DEFINE_MUTEX(pmus_lock); | |
391 | static struct srcu_struct pmus_srcu; | |
a63fbed7 | 392 | static cpumask_var_t perf_online_mask; |
108b02cf | 393 | |
0764771d | 394 | /* |
cdd6c482 | 395 | * perf event paranoia level: |
0fbdea19 IM |
396 | * -1 - not paranoid at all |
397 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 398 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 399 | * 2 - disallow kernel profiling for unpriv |
d48d0c64 | 400 | * 3 - disallow all unpriv perf event use |
0764771d | 401 | */ |
d48d0c64 BH |
402 | #ifdef CONFIG_SECURITY_PERF_EVENTS_RESTRICT |
403 | int sysctl_perf_event_paranoid __read_mostly = 3; | |
404 | #else | |
405 | int sysctl_perf_event_paranoid __read_mostly = 1; | |
406 | #endif | |
0764771d | 407 | |
20443384 FW |
408 | /* Minimum for 512 kiB + 1 user control page */ |
409 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
410 | |
411 | /* | |
cdd6c482 | 412 | * max perf event sample rate |
df58ab24 | 413 | */ |
14c63f17 DH |
414 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
415 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
416 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
417 | ||
418 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
419 | ||
420 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
421 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
422 | ||
d9494cb4 PZ |
423 | static int perf_sample_allowed_ns __read_mostly = |
424 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 425 | |
18ab2cd3 | 426 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
427 | { |
428 | u64 tmp = perf_sample_period_ns; | |
429 | ||
430 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
431 | tmp = div_u64(tmp, 100); |
432 | if (!tmp) | |
433 | tmp = 1; | |
434 | ||
435 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 436 | } |
163ec435 | 437 | |
9e630205 SE |
438 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
439 | ||
163ec435 PZ |
440 | int perf_proc_update_handler(struct ctl_table *table, int write, |
441 | void __user *buffer, size_t *lenp, | |
442 | loff_t *ppos) | |
443 | { | |
723478c8 | 444 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
163ec435 PZ |
445 | |
446 | if (ret || !write) | |
447 | return ret; | |
448 | ||
ab7fdefb KL |
449 | /* |
450 | * If throttling is disabled don't allow the write: | |
451 | */ | |
452 | if (sysctl_perf_cpu_time_max_percent == 100 || | |
453 | sysctl_perf_cpu_time_max_percent == 0) | |
454 | return -EINVAL; | |
455 | ||
163ec435 | 456 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
14c63f17 DH |
457 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
458 | update_perf_cpu_limits(); | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
464 | ||
465 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
466 | void __user *buffer, size_t *lenp, | |
467 | loff_t *ppos) | |
468 | { | |
1572e45a | 469 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
14c63f17 DH |
470 | |
471 | if (ret || !write) | |
472 | return ret; | |
473 | ||
b303e7c1 PZ |
474 | if (sysctl_perf_cpu_time_max_percent == 100 || |
475 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
476 | printk(KERN_WARNING |
477 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
478 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
479 | } else { | |
480 | update_perf_cpu_limits(); | |
481 | } | |
163ec435 PZ |
482 | |
483 | return 0; | |
484 | } | |
1ccd1549 | 485 | |
14c63f17 DH |
486 | /* |
487 | * perf samples are done in some very critical code paths (NMIs). | |
488 | * If they take too much CPU time, the system can lock up and not | |
489 | * get any real work done. This will drop the sample rate when | |
490 | * we detect that events are taking too long. | |
491 | */ | |
492 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 493 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 494 | |
91a612ee PZ |
495 | static u64 __report_avg; |
496 | static u64 __report_allowed; | |
497 | ||
6a02ad66 | 498 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 499 | { |
0d87d7ec | 500 | printk_ratelimited(KERN_INFO |
91a612ee PZ |
501 | "perf: interrupt took too long (%lld > %lld), lowering " |
502 | "kernel.perf_event_max_sample_rate to %d\n", | |
503 | __report_avg, __report_allowed, | |
504 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
505 | } |
506 | ||
507 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
508 | ||
509 | void perf_sample_event_took(u64 sample_len_ns) | |
510 | { | |
91a612ee PZ |
511 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
512 | u64 running_len; | |
513 | u64 avg_len; | |
514 | u32 max; | |
14c63f17 | 515 | |
91a612ee | 516 | if (max_len == 0) |
14c63f17 DH |
517 | return; |
518 | ||
91a612ee PZ |
519 | /* Decay the counter by 1 average sample. */ |
520 | running_len = __this_cpu_read(running_sample_length); | |
521 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
522 | running_len += sample_len_ns; | |
523 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
524 | |
525 | /* | |
91a612ee PZ |
526 | * Note: this will be biased artifically low until we have |
527 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
528 | * from having to maintain a count. |
529 | */ | |
91a612ee PZ |
530 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
531 | if (avg_len <= max_len) | |
14c63f17 DH |
532 | return; |
533 | ||
91a612ee PZ |
534 | __report_avg = avg_len; |
535 | __report_allowed = max_len; | |
14c63f17 | 536 | |
91a612ee PZ |
537 | /* |
538 | * Compute a throttle threshold 25% below the current duration. | |
539 | */ | |
540 | avg_len += avg_len / 4; | |
541 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
542 | if (avg_len < max) | |
543 | max /= (u32)avg_len; | |
544 | else | |
545 | max = 1; | |
14c63f17 | 546 | |
91a612ee PZ |
547 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
548 | WRITE_ONCE(max_samples_per_tick, max); | |
549 | ||
550 | sysctl_perf_event_sample_rate = max * HZ; | |
551 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 552 | |
cd578abb | 553 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 554 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 555 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 556 | __report_avg, __report_allowed, |
cd578abb PZ |
557 | sysctl_perf_event_sample_rate); |
558 | } | |
14c63f17 DH |
559 | } |
560 | ||
cdd6c482 | 561 | static atomic64_t perf_event_id; |
a96bbc16 | 562 | |
0b3fcf17 SE |
563 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
564 | enum event_type_t event_type); | |
565 | ||
566 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
567 | enum event_type_t event_type, |
568 | struct task_struct *task); | |
569 | ||
570 | static void update_context_time(struct perf_event_context *ctx); | |
571 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 572 | |
cdd6c482 | 573 | void __weak perf_event_print_debug(void) { } |
0793a61d | 574 | |
84c79910 | 575 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 576 | { |
84c79910 | 577 | return "pmu"; |
0793a61d TG |
578 | } |
579 | ||
0b3fcf17 SE |
580 | static inline u64 perf_clock(void) |
581 | { | |
582 | return local_clock(); | |
583 | } | |
584 | ||
34f43927 PZ |
585 | static inline u64 perf_event_clock(struct perf_event *event) |
586 | { | |
587 | return event->clock(); | |
588 | } | |
589 | ||
0d3d73aa PZ |
590 | /* |
591 | * State based event timekeeping... | |
592 | * | |
593 | * The basic idea is to use event->state to determine which (if any) time | |
594 | * fields to increment with the current delta. This means we only need to | |
595 | * update timestamps when we change state or when they are explicitly requested | |
596 | * (read). | |
597 | * | |
598 | * Event groups make things a little more complicated, but not terribly so. The | |
599 | * rules for a group are that if the group leader is OFF the entire group is | |
600 | * OFF, irrespecive of what the group member states are. This results in | |
601 | * __perf_effective_state(). | |
602 | * | |
603 | * A futher ramification is that when a group leader flips between OFF and | |
604 | * !OFF, we need to update all group member times. | |
605 | * | |
606 | * | |
607 | * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we | |
608 | * need to make sure the relevant context time is updated before we try and | |
609 | * update our timestamps. | |
610 | */ | |
611 | ||
612 | static __always_inline enum perf_event_state | |
613 | __perf_effective_state(struct perf_event *event) | |
614 | { | |
615 | struct perf_event *leader = event->group_leader; | |
616 | ||
617 | if (leader->state <= PERF_EVENT_STATE_OFF) | |
618 | return leader->state; | |
619 | ||
620 | return event->state; | |
621 | } | |
622 | ||
623 | static __always_inline void | |
624 | __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running) | |
625 | { | |
626 | enum perf_event_state state = __perf_effective_state(event); | |
627 | u64 delta = now - event->tstamp; | |
628 | ||
629 | *enabled = event->total_time_enabled; | |
630 | if (state >= PERF_EVENT_STATE_INACTIVE) | |
631 | *enabled += delta; | |
632 | ||
633 | *running = event->total_time_running; | |
634 | if (state >= PERF_EVENT_STATE_ACTIVE) | |
635 | *running += delta; | |
636 | } | |
637 | ||
638 | static void perf_event_update_time(struct perf_event *event) | |
639 | { | |
640 | u64 now = perf_event_time(event); | |
641 | ||
642 | __perf_update_times(event, now, &event->total_time_enabled, | |
643 | &event->total_time_running); | |
644 | event->tstamp = now; | |
645 | } | |
646 | ||
647 | static void perf_event_update_sibling_time(struct perf_event *leader) | |
648 | { | |
649 | struct perf_event *sibling; | |
650 | ||
651 | list_for_each_entry(sibling, &leader->sibling_list, group_entry) | |
652 | perf_event_update_time(sibling); | |
653 | } | |
654 | ||
655 | static void | |
656 | perf_event_set_state(struct perf_event *event, enum perf_event_state state) | |
657 | { | |
658 | if (event->state == state) | |
659 | return; | |
660 | ||
661 | perf_event_update_time(event); | |
662 | /* | |
663 | * If a group leader gets enabled/disabled all its siblings | |
664 | * are affected too. | |
665 | */ | |
666 | if ((event->state < 0) ^ (state < 0)) | |
667 | perf_event_update_sibling_time(event); | |
668 | ||
669 | WRITE_ONCE(event->state, state); | |
670 | } | |
671 | ||
e5d1367f SE |
672 | #ifdef CONFIG_CGROUP_PERF |
673 | ||
e5d1367f SE |
674 | static inline bool |
675 | perf_cgroup_match(struct perf_event *event) | |
676 | { | |
677 | struct perf_event_context *ctx = event->ctx; | |
678 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
679 | ||
ef824fa1 TH |
680 | /* @event doesn't care about cgroup */ |
681 | if (!event->cgrp) | |
682 | return true; | |
683 | ||
684 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
685 | if (!cpuctx->cgrp) | |
686 | return false; | |
687 | ||
688 | /* | |
689 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
690 | * also enabled for all its descendant cgroups. If @cpuctx's | |
691 | * cgroup is a descendant of @event's (the test covers identity | |
692 | * case), it's a match. | |
693 | */ | |
694 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
695 | event->cgrp->css.cgroup); | |
e5d1367f SE |
696 | } |
697 | ||
e5d1367f SE |
698 | static inline void perf_detach_cgroup(struct perf_event *event) |
699 | { | |
4e2ba650 | 700 | css_put(&event->cgrp->css); |
e5d1367f SE |
701 | event->cgrp = NULL; |
702 | } | |
703 | ||
704 | static inline int is_cgroup_event(struct perf_event *event) | |
705 | { | |
706 | return event->cgrp != NULL; | |
707 | } | |
708 | ||
709 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
710 | { | |
711 | struct perf_cgroup_info *t; | |
712 | ||
713 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
714 | return t->time; | |
715 | } | |
716 | ||
717 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
718 | { | |
719 | struct perf_cgroup_info *info; | |
720 | u64 now; | |
721 | ||
722 | now = perf_clock(); | |
723 | ||
724 | info = this_cpu_ptr(cgrp->info); | |
725 | ||
726 | info->time += now - info->timestamp; | |
727 | info->timestamp = now; | |
728 | } | |
729 | ||
730 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
731 | { | |
732 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
733 | if (cgrp_out) | |
734 | __update_cgrp_time(cgrp_out); | |
735 | } | |
736 | ||
737 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
738 | { | |
3f7cce3c SE |
739 | struct perf_cgroup *cgrp; |
740 | ||
e5d1367f | 741 | /* |
3f7cce3c SE |
742 | * ensure we access cgroup data only when needed and |
743 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 744 | */ |
3f7cce3c | 745 | if (!is_cgroup_event(event)) |
e5d1367f SE |
746 | return; |
747 | ||
614e4c4e | 748 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
749 | /* |
750 | * Do not update time when cgroup is not active | |
751 | */ | |
e6a52033 | 752 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
3f7cce3c | 753 | __update_cgrp_time(event->cgrp); |
e5d1367f SE |
754 | } |
755 | ||
756 | static inline void | |
3f7cce3c SE |
757 | perf_cgroup_set_timestamp(struct task_struct *task, |
758 | struct perf_event_context *ctx) | |
e5d1367f SE |
759 | { |
760 | struct perf_cgroup *cgrp; | |
761 | struct perf_cgroup_info *info; | |
762 | ||
3f7cce3c SE |
763 | /* |
764 | * ctx->lock held by caller | |
765 | * ensure we do not access cgroup data | |
766 | * unless we have the cgroup pinned (css_get) | |
767 | */ | |
768 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
769 | return; |
770 | ||
614e4c4e | 771 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 772 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 773 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
774 | } |
775 | ||
058fe1c0 DCC |
776 | static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list); |
777 | ||
e5d1367f SE |
778 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
779 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
780 | ||
781 | /* | |
782 | * reschedule events based on the cgroup constraint of task. | |
783 | * | |
784 | * mode SWOUT : schedule out everything | |
785 | * mode SWIN : schedule in based on cgroup for next | |
786 | */ | |
18ab2cd3 | 787 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
788 | { |
789 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 790 | struct list_head *list; |
e5d1367f SE |
791 | unsigned long flags; |
792 | ||
793 | /* | |
058fe1c0 DCC |
794 | * Disable interrupts and preemption to avoid this CPU's |
795 | * cgrp_cpuctx_entry to change under us. | |
e5d1367f SE |
796 | */ |
797 | local_irq_save(flags); | |
798 | ||
058fe1c0 DCC |
799 | list = this_cpu_ptr(&cgrp_cpuctx_list); |
800 | list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) { | |
801 | WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); | |
e5d1367f | 802 | |
058fe1c0 DCC |
803 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
804 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f | 805 | |
058fe1c0 DCC |
806 | if (mode & PERF_CGROUP_SWOUT) { |
807 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
808 | /* | |
809 | * must not be done before ctxswout due | |
810 | * to event_filter_match() in event_sched_out() | |
811 | */ | |
812 | cpuctx->cgrp = NULL; | |
813 | } | |
e5d1367f | 814 | |
058fe1c0 DCC |
815 | if (mode & PERF_CGROUP_SWIN) { |
816 | WARN_ON_ONCE(cpuctx->cgrp); | |
817 | /* | |
818 | * set cgrp before ctxsw in to allow | |
819 | * event_filter_match() to not have to pass | |
820 | * task around | |
821 | * we pass the cpuctx->ctx to perf_cgroup_from_task() | |
822 | * because cgorup events are only per-cpu | |
823 | */ | |
824 | cpuctx->cgrp = perf_cgroup_from_task(task, | |
825 | &cpuctx->ctx); | |
826 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); | |
e5d1367f | 827 | } |
058fe1c0 DCC |
828 | perf_pmu_enable(cpuctx->ctx.pmu); |
829 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f SE |
830 | } |
831 | ||
e5d1367f SE |
832 | local_irq_restore(flags); |
833 | } | |
834 | ||
a8d757ef SE |
835 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
836 | struct task_struct *next) | |
e5d1367f | 837 | { |
a8d757ef SE |
838 | struct perf_cgroup *cgrp1; |
839 | struct perf_cgroup *cgrp2 = NULL; | |
840 | ||
ddaaf4e2 | 841 | rcu_read_lock(); |
a8d757ef SE |
842 | /* |
843 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
844 | * we do not need to pass the ctx here because we know |
845 | * we are holding the rcu lock | |
a8d757ef | 846 | */ |
614e4c4e | 847 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 848 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
849 | |
850 | /* | |
851 | * only schedule out current cgroup events if we know | |
852 | * that we are switching to a different cgroup. Otherwise, | |
853 | * do no touch the cgroup events. | |
854 | */ | |
855 | if (cgrp1 != cgrp2) | |
856 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
857 | |
858 | rcu_read_unlock(); | |
e5d1367f SE |
859 | } |
860 | ||
a8d757ef SE |
861 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
862 | struct task_struct *task) | |
e5d1367f | 863 | { |
a8d757ef SE |
864 | struct perf_cgroup *cgrp1; |
865 | struct perf_cgroup *cgrp2 = NULL; | |
866 | ||
ddaaf4e2 | 867 | rcu_read_lock(); |
a8d757ef SE |
868 | /* |
869 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
870 | * we do not need to pass the ctx here because we know |
871 | * we are holding the rcu lock | |
a8d757ef | 872 | */ |
614e4c4e | 873 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 874 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
875 | |
876 | /* | |
877 | * only need to schedule in cgroup events if we are changing | |
878 | * cgroup during ctxsw. Cgroup events were not scheduled | |
879 | * out of ctxsw out if that was not the case. | |
880 | */ | |
881 | if (cgrp1 != cgrp2) | |
882 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
883 | |
884 | rcu_read_unlock(); | |
e5d1367f SE |
885 | } |
886 | ||
887 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
888 | struct perf_event_attr *attr, | |
889 | struct perf_event *group_leader) | |
890 | { | |
891 | struct perf_cgroup *cgrp; | |
892 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
893 | struct fd f = fdget(fd); |
894 | int ret = 0; | |
e5d1367f | 895 | |
2903ff01 | 896 | if (!f.file) |
e5d1367f SE |
897 | return -EBADF; |
898 | ||
b583043e | 899 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 900 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
901 | if (IS_ERR(css)) { |
902 | ret = PTR_ERR(css); | |
903 | goto out; | |
904 | } | |
e5d1367f SE |
905 | |
906 | cgrp = container_of(css, struct perf_cgroup, css); | |
907 | event->cgrp = cgrp; | |
908 | ||
909 | /* | |
910 | * all events in a group must monitor | |
911 | * the same cgroup because a task belongs | |
912 | * to only one perf cgroup at a time | |
913 | */ | |
914 | if (group_leader && group_leader->cgrp != cgrp) { | |
915 | perf_detach_cgroup(event); | |
916 | ret = -EINVAL; | |
e5d1367f | 917 | } |
3db272c0 | 918 | out: |
2903ff01 | 919 | fdput(f); |
e5d1367f SE |
920 | return ret; |
921 | } | |
922 | ||
923 | static inline void | |
924 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
925 | { | |
926 | struct perf_cgroup_info *t; | |
927 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
928 | event->shadow_ctx_time = now - t->timestamp; | |
929 | } | |
930 | ||
db4a8356 DCC |
931 | /* |
932 | * Update cpuctx->cgrp so that it is set when first cgroup event is added and | |
933 | * cleared when last cgroup event is removed. | |
934 | */ | |
935 | static inline void | |
936 | list_update_cgroup_event(struct perf_event *event, | |
937 | struct perf_event_context *ctx, bool add) | |
938 | { | |
939 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 940 | struct list_head *cpuctx_entry; |
db4a8356 DCC |
941 | |
942 | if (!is_cgroup_event(event)) | |
943 | return; | |
944 | ||
945 | if (add && ctx->nr_cgroups++) | |
946 | return; | |
947 | else if (!add && --ctx->nr_cgroups) | |
948 | return; | |
949 | /* | |
950 | * Because cgroup events are always per-cpu events, | |
951 | * this will always be called from the right CPU. | |
952 | */ | |
953 | cpuctx = __get_cpu_context(ctx); | |
058fe1c0 DCC |
954 | cpuctx_entry = &cpuctx->cgrp_cpuctx_entry; |
955 | /* cpuctx->cgrp is NULL unless a cgroup event is active in this CPU .*/ | |
956 | if (add) { | |
be96b316 TH |
957 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); |
958 | ||
058fe1c0 | 959 | list_add(cpuctx_entry, this_cpu_ptr(&cgrp_cpuctx_list)); |
be96b316 TH |
960 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
961 | cpuctx->cgrp = cgrp; | |
058fe1c0 DCC |
962 | } else { |
963 | list_del(cpuctx_entry); | |
8fc31ce8 | 964 | cpuctx->cgrp = NULL; |
058fe1c0 | 965 | } |
db4a8356 DCC |
966 | } |
967 | ||
e5d1367f SE |
968 | #else /* !CONFIG_CGROUP_PERF */ |
969 | ||
970 | static inline bool | |
971 | perf_cgroup_match(struct perf_event *event) | |
972 | { | |
973 | return true; | |
974 | } | |
975 | ||
976 | static inline void perf_detach_cgroup(struct perf_event *event) | |
977 | {} | |
978 | ||
979 | static inline int is_cgroup_event(struct perf_event *event) | |
980 | { | |
981 | return 0; | |
982 | } | |
983 | ||
e5d1367f SE |
984 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
985 | { | |
986 | } | |
987 | ||
988 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
989 | { | |
990 | } | |
991 | ||
a8d757ef SE |
992 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
993 | struct task_struct *next) | |
e5d1367f SE |
994 | { |
995 | } | |
996 | ||
a8d757ef SE |
997 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
998 | struct task_struct *task) | |
e5d1367f SE |
999 | { |
1000 | } | |
1001 | ||
1002 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
1003 | struct perf_event_attr *attr, | |
1004 | struct perf_event *group_leader) | |
1005 | { | |
1006 | return -EINVAL; | |
1007 | } | |
1008 | ||
1009 | static inline void | |
3f7cce3c SE |
1010 | perf_cgroup_set_timestamp(struct task_struct *task, |
1011 | struct perf_event_context *ctx) | |
e5d1367f SE |
1012 | { |
1013 | } | |
1014 | ||
1015 | void | |
1016 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
1017 | { | |
1018 | } | |
1019 | ||
1020 | static inline void | |
1021 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
1022 | { | |
1023 | } | |
1024 | ||
1025 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
1026 | { | |
1027 | return 0; | |
1028 | } | |
1029 | ||
db4a8356 DCC |
1030 | static inline void |
1031 | list_update_cgroup_event(struct perf_event *event, | |
1032 | struct perf_event_context *ctx, bool add) | |
1033 | { | |
1034 | } | |
1035 | ||
e5d1367f SE |
1036 | #endif |
1037 | ||
9e630205 SE |
1038 | /* |
1039 | * set default to be dependent on timer tick just | |
1040 | * like original code | |
1041 | */ | |
1042 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
1043 | /* | |
8a1115ff | 1044 | * function must be called with interrupts disabled |
9e630205 | 1045 | */ |
272325c4 | 1046 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
1047 | { |
1048 | struct perf_cpu_context *cpuctx; | |
9e630205 SE |
1049 | int rotations = 0; |
1050 | ||
16444645 | 1051 | lockdep_assert_irqs_disabled(); |
9e630205 SE |
1052 | |
1053 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
1054 | rotations = perf_rotate_context(cpuctx); |
1055 | ||
4cfafd30 PZ |
1056 | raw_spin_lock(&cpuctx->hrtimer_lock); |
1057 | if (rotations) | |
9e630205 | 1058 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
1059 | else |
1060 | cpuctx->hrtimer_active = 0; | |
1061 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 1062 | |
4cfafd30 | 1063 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
1064 | } |
1065 | ||
272325c4 | 1066 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 1067 | { |
272325c4 | 1068 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1069 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 1070 | u64 interval; |
9e630205 SE |
1071 | |
1072 | /* no multiplexing needed for SW PMU */ | |
1073 | if (pmu->task_ctx_nr == perf_sw_context) | |
1074 | return; | |
1075 | ||
62b85639 SE |
1076 | /* |
1077 | * check default is sane, if not set then force to | |
1078 | * default interval (1/tick) | |
1079 | */ | |
272325c4 PZ |
1080 | interval = pmu->hrtimer_interval_ms; |
1081 | if (interval < 1) | |
1082 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 1083 | |
272325c4 | 1084 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 1085 | |
4cfafd30 PZ |
1086 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
1087 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 1088 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
1089 | } |
1090 | ||
272325c4 | 1091 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 1092 | { |
272325c4 | 1093 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1094 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 1095 | unsigned long flags; |
9e630205 SE |
1096 | |
1097 | /* not for SW PMU */ | |
1098 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 1099 | return 0; |
9e630205 | 1100 | |
4cfafd30 PZ |
1101 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
1102 | if (!cpuctx->hrtimer_active) { | |
1103 | cpuctx->hrtimer_active = 1; | |
1104 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
1105 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
1106 | } | |
1107 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 1108 | |
272325c4 | 1109 | return 0; |
9e630205 SE |
1110 | } |
1111 | ||
33696fc0 | 1112 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1113 | { |
33696fc0 PZ |
1114 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1115 | if (!(*count)++) | |
1116 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1117 | } |
9e35ad38 | 1118 | |
33696fc0 | 1119 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1120 | { |
33696fc0 PZ |
1121 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1122 | if (!--(*count)) | |
1123 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1124 | } |
9e35ad38 | 1125 | |
2fde4f94 | 1126 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1127 | |
1128 | /* | |
2fde4f94 MR |
1129 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1130 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1131 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1132 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1133 | */ |
2fde4f94 | 1134 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1135 | { |
2fde4f94 | 1136 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1137 | |
16444645 | 1138 | lockdep_assert_irqs_disabled(); |
b5ab4cd5 | 1139 | |
2fde4f94 MR |
1140 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1141 | ||
1142 | list_add(&ctx->active_ctx_list, head); | |
1143 | } | |
1144 | ||
1145 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1146 | { | |
16444645 | 1147 | lockdep_assert_irqs_disabled(); |
2fde4f94 MR |
1148 | |
1149 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1150 | ||
1151 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1152 | } |
9e35ad38 | 1153 | |
cdd6c482 | 1154 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1155 | { |
e5289d4a | 1156 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1157 | } |
1158 | ||
4af57ef2 YZ |
1159 | static void free_ctx(struct rcu_head *head) |
1160 | { | |
1161 | struct perf_event_context *ctx; | |
1162 | ||
1163 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1164 | kfree(ctx->task_ctx_data); | |
1165 | kfree(ctx); | |
1166 | } | |
1167 | ||
cdd6c482 | 1168 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1169 | { |
564c2b21 PM |
1170 | if (atomic_dec_and_test(&ctx->refcount)) { |
1171 | if (ctx->parent_ctx) | |
1172 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1173 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1174 | put_task_struct(ctx->task); |
4af57ef2 | 1175 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1176 | } |
a63eaf34 PM |
1177 | } |
1178 | ||
f63a8daa PZ |
1179 | /* |
1180 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1181 | * perf_pmu_migrate_context() we need some magic. | |
1182 | * | |
1183 | * Those places that change perf_event::ctx will hold both | |
1184 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1185 | * | |
8b10c5e2 PZ |
1186 | * Lock ordering is by mutex address. There are two other sites where |
1187 | * perf_event_context::mutex nests and those are: | |
1188 | * | |
1189 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1190 | * perf_event_exit_event() |
1191 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1192 | * |
1193 | * - perf_event_init_context() [ parent, 0 ] | |
1194 | * inherit_task_group() | |
1195 | * inherit_group() | |
1196 | * inherit_event() | |
1197 | * perf_event_alloc() | |
1198 | * perf_init_event() | |
1199 | * perf_try_init_event() [ child , 1 ] | |
1200 | * | |
1201 | * While it appears there is an obvious deadlock here -- the parent and child | |
1202 | * nesting levels are inverted between the two. This is in fact safe because | |
1203 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1204 | * spawning task cannot (yet) exit. | |
1205 | * | |
1206 | * But remember that that these are parent<->child context relations, and | |
1207 | * migration does not affect children, therefore these two orderings should not | |
1208 | * interact. | |
f63a8daa PZ |
1209 | * |
1210 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1211 | * because the sys_perf_event_open() case will install a new event and break | |
1212 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1213 | * concerned with cpuctx and that doesn't have children. | |
1214 | * | |
1215 | * The places that change perf_event::ctx will issue: | |
1216 | * | |
1217 | * perf_remove_from_context(); | |
1218 | * synchronize_rcu(); | |
1219 | * perf_install_in_context(); | |
1220 | * | |
1221 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1222 | * quiesce the event, after which we can install it in the new location. This | |
1223 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1224 | * while in transit. Therefore all such accessors should also acquire | |
1225 | * perf_event_context::mutex to serialize against this. | |
1226 | * | |
1227 | * However; because event->ctx can change while we're waiting to acquire | |
1228 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1229 | * function. | |
1230 | * | |
1231 | * Lock order: | |
79c9ce57 | 1232 | * cred_guard_mutex |
f63a8daa PZ |
1233 | * task_struct::perf_event_mutex |
1234 | * perf_event_context::mutex | |
f63a8daa | 1235 | * perf_event::child_mutex; |
07c4a776 | 1236 | * perf_event_context::lock |
f63a8daa PZ |
1237 | * perf_event::mmap_mutex |
1238 | * mmap_sem | |
82d94856 PZ |
1239 | * |
1240 | * cpu_hotplug_lock | |
1241 | * pmus_lock | |
1242 | * cpuctx->mutex / perf_event_context::mutex | |
f63a8daa | 1243 | */ |
a83fe28e PZ |
1244 | static struct perf_event_context * |
1245 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1246 | { |
1247 | struct perf_event_context *ctx; | |
1248 | ||
1249 | again: | |
1250 | rcu_read_lock(); | |
6aa7de05 | 1251 | ctx = READ_ONCE(event->ctx); |
f63a8daa PZ |
1252 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
1253 | rcu_read_unlock(); | |
1254 | goto again; | |
1255 | } | |
1256 | rcu_read_unlock(); | |
1257 | ||
a83fe28e | 1258 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1259 | if (event->ctx != ctx) { |
1260 | mutex_unlock(&ctx->mutex); | |
1261 | put_ctx(ctx); | |
1262 | goto again; | |
1263 | } | |
1264 | ||
1265 | return ctx; | |
1266 | } | |
1267 | ||
a83fe28e PZ |
1268 | static inline struct perf_event_context * |
1269 | perf_event_ctx_lock(struct perf_event *event) | |
1270 | { | |
1271 | return perf_event_ctx_lock_nested(event, 0); | |
1272 | } | |
1273 | ||
f63a8daa PZ |
1274 | static void perf_event_ctx_unlock(struct perf_event *event, |
1275 | struct perf_event_context *ctx) | |
1276 | { | |
1277 | mutex_unlock(&ctx->mutex); | |
1278 | put_ctx(ctx); | |
1279 | } | |
1280 | ||
211de6eb PZ |
1281 | /* |
1282 | * This must be done under the ctx->lock, such as to serialize against | |
1283 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1284 | * calling scheduler related locks and ctx->lock nests inside those. | |
1285 | */ | |
1286 | static __must_check struct perf_event_context * | |
1287 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1288 | { |
211de6eb PZ |
1289 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1290 | ||
1291 | lockdep_assert_held(&ctx->lock); | |
1292 | ||
1293 | if (parent_ctx) | |
71a851b4 | 1294 | ctx->parent_ctx = NULL; |
5a3126d4 | 1295 | ctx->generation++; |
211de6eb PZ |
1296 | |
1297 | return parent_ctx; | |
71a851b4 PZ |
1298 | } |
1299 | ||
1d953111 ON |
1300 | static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, |
1301 | enum pid_type type) | |
6844c09d | 1302 | { |
1d953111 | 1303 | u32 nr; |
6844c09d ACM |
1304 | /* |
1305 | * only top level events have the pid namespace they were created in | |
1306 | */ | |
1307 | if (event->parent) | |
1308 | event = event->parent; | |
1309 | ||
1d953111 ON |
1310 | nr = __task_pid_nr_ns(p, type, event->ns); |
1311 | /* avoid -1 if it is idle thread or runs in another ns */ | |
1312 | if (!nr && !pid_alive(p)) | |
1313 | nr = -1; | |
1314 | return nr; | |
6844c09d ACM |
1315 | } |
1316 | ||
1d953111 | 1317 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
6844c09d | 1318 | { |
1d953111 ON |
1319 | return perf_event_pid_type(event, p, __PIDTYPE_TGID); |
1320 | } | |
6844c09d | 1321 | |
1d953111 ON |
1322 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
1323 | { | |
1324 | return perf_event_pid_type(event, p, PIDTYPE_PID); | |
6844c09d ACM |
1325 | } |
1326 | ||
7f453c24 | 1327 | /* |
cdd6c482 | 1328 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1329 | * to userspace. |
1330 | */ | |
cdd6c482 | 1331 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1332 | { |
cdd6c482 | 1333 | u64 id = event->id; |
7f453c24 | 1334 | |
cdd6c482 IM |
1335 | if (event->parent) |
1336 | id = event->parent->id; | |
7f453c24 PZ |
1337 | |
1338 | return id; | |
1339 | } | |
1340 | ||
25346b93 | 1341 | /* |
cdd6c482 | 1342 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1343 | * |
25346b93 PM |
1344 | * This has to cope with with the fact that until it is locked, |
1345 | * the context could get moved to another task. | |
1346 | */ | |
cdd6c482 | 1347 | static struct perf_event_context * |
8dc85d54 | 1348 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1349 | { |
cdd6c482 | 1350 | struct perf_event_context *ctx; |
25346b93 | 1351 | |
9ed6060d | 1352 | retry: |
058ebd0e PZ |
1353 | /* |
1354 | * One of the few rules of preemptible RCU is that one cannot do | |
1355 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1356 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1357 | * rcu_read_unlock_special(). |
1358 | * | |
1359 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1360 | * side critical section has interrupts disabled. |
058ebd0e | 1361 | */ |
2fd59077 | 1362 | local_irq_save(*flags); |
058ebd0e | 1363 | rcu_read_lock(); |
8dc85d54 | 1364 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1365 | if (ctx) { |
1366 | /* | |
1367 | * If this context is a clone of another, it might | |
1368 | * get swapped for another underneath us by | |
cdd6c482 | 1369 | * perf_event_task_sched_out, though the |
25346b93 PM |
1370 | * rcu_read_lock() protects us from any context |
1371 | * getting freed. Lock the context and check if it | |
1372 | * got swapped before we could get the lock, and retry | |
1373 | * if so. If we locked the right context, then it | |
1374 | * can't get swapped on us any more. | |
1375 | */ | |
2fd59077 | 1376 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1377 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1378 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1379 | rcu_read_unlock(); |
2fd59077 | 1380 | local_irq_restore(*flags); |
25346b93 PM |
1381 | goto retry; |
1382 | } | |
b49a9e7e | 1383 | |
63b6da39 PZ |
1384 | if (ctx->task == TASK_TOMBSTONE || |
1385 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1386 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1387 | ctx = NULL; |
828b6f0e PZ |
1388 | } else { |
1389 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1390 | } |
25346b93 PM |
1391 | } |
1392 | rcu_read_unlock(); | |
2fd59077 PM |
1393 | if (!ctx) |
1394 | local_irq_restore(*flags); | |
25346b93 PM |
1395 | return ctx; |
1396 | } | |
1397 | ||
1398 | /* | |
1399 | * Get the context for a task and increment its pin_count so it | |
1400 | * can't get swapped to another task. This also increments its | |
1401 | * reference count so that the context can't get freed. | |
1402 | */ | |
8dc85d54 PZ |
1403 | static struct perf_event_context * |
1404 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1405 | { |
cdd6c482 | 1406 | struct perf_event_context *ctx; |
25346b93 PM |
1407 | unsigned long flags; |
1408 | ||
8dc85d54 | 1409 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1410 | if (ctx) { |
1411 | ++ctx->pin_count; | |
e625cce1 | 1412 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1413 | } |
1414 | return ctx; | |
1415 | } | |
1416 | ||
cdd6c482 | 1417 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1418 | { |
1419 | unsigned long flags; | |
1420 | ||
e625cce1 | 1421 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1422 | --ctx->pin_count; |
e625cce1 | 1423 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1424 | } |
1425 | ||
f67218c3 PZ |
1426 | /* |
1427 | * Update the record of the current time in a context. | |
1428 | */ | |
1429 | static void update_context_time(struct perf_event_context *ctx) | |
1430 | { | |
1431 | u64 now = perf_clock(); | |
1432 | ||
1433 | ctx->time += now - ctx->timestamp; | |
1434 | ctx->timestamp = now; | |
1435 | } | |
1436 | ||
4158755d SE |
1437 | static u64 perf_event_time(struct perf_event *event) |
1438 | { | |
1439 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1440 | |
1441 | if (is_cgroup_event(event)) | |
1442 | return perf_cgroup_event_time(event); | |
1443 | ||
4158755d SE |
1444 | return ctx ? ctx->time : 0; |
1445 | } | |
1446 | ||
487f05e1 AS |
1447 | static enum event_type_t get_event_type(struct perf_event *event) |
1448 | { | |
1449 | struct perf_event_context *ctx = event->ctx; | |
1450 | enum event_type_t event_type; | |
1451 | ||
1452 | lockdep_assert_held(&ctx->lock); | |
1453 | ||
3bda69c1 AS |
1454 | /* |
1455 | * It's 'group type', really, because if our group leader is | |
1456 | * pinned, so are we. | |
1457 | */ | |
1458 | if (event->group_leader != event) | |
1459 | event = event->group_leader; | |
1460 | ||
487f05e1 AS |
1461 | event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; |
1462 | if (!ctx->task) | |
1463 | event_type |= EVENT_CPU; | |
1464 | ||
1465 | return event_type; | |
1466 | } | |
1467 | ||
889ff015 FW |
1468 | static struct list_head * |
1469 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) | |
1470 | { | |
1471 | if (event->attr.pinned) | |
1472 | return &ctx->pinned_groups; | |
1473 | else | |
1474 | return &ctx->flexible_groups; | |
1475 | } | |
1476 | ||
fccc714b | 1477 | /* |
cdd6c482 | 1478 | * Add a event from the lists for its context. |
fccc714b PZ |
1479 | * Must be called with ctx->mutex and ctx->lock held. |
1480 | */ | |
04289bb9 | 1481 | static void |
cdd6c482 | 1482 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1483 | { |
c994d613 PZ |
1484 | lockdep_assert_held(&ctx->lock); |
1485 | ||
8a49542c PZ |
1486 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1487 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 | 1488 | |
0d3d73aa PZ |
1489 | event->tstamp = perf_event_time(event); |
1490 | ||
04289bb9 | 1491 | /* |
8a49542c PZ |
1492 | * If we're a stand alone event or group leader, we go to the context |
1493 | * list, group events are kept attached to the group so that | |
1494 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1495 | */ |
8a49542c | 1496 | if (event->group_leader == event) { |
889ff015 FW |
1497 | struct list_head *list; |
1498 | ||
4ff6a8de | 1499 | event->group_caps = event->event_caps; |
d6f962b5 | 1500 | |
889ff015 FW |
1501 | list = ctx_group_list(event, ctx); |
1502 | list_add_tail(&event->group_entry, list); | |
5c148194 | 1503 | } |
592903cd | 1504 | |
db4a8356 | 1505 | list_update_cgroup_event(event, ctx, true); |
e5d1367f | 1506 | |
cdd6c482 IM |
1507 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1508 | ctx->nr_events++; | |
1509 | if (event->attr.inherit_stat) | |
bfbd3381 | 1510 | ctx->nr_stat++; |
5a3126d4 PZ |
1511 | |
1512 | ctx->generation++; | |
04289bb9 IM |
1513 | } |
1514 | ||
0231bb53 JO |
1515 | /* |
1516 | * Initialize event state based on the perf_event_attr::disabled. | |
1517 | */ | |
1518 | static inline void perf_event__state_init(struct perf_event *event) | |
1519 | { | |
1520 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1521 | PERF_EVENT_STATE_INACTIVE; | |
1522 | } | |
1523 | ||
a723968c | 1524 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1525 | { |
1526 | int entry = sizeof(u64); /* value */ | |
1527 | int size = 0; | |
1528 | int nr = 1; | |
1529 | ||
1530 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1531 | size += sizeof(u64); | |
1532 | ||
1533 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1534 | size += sizeof(u64); | |
1535 | ||
1536 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1537 | entry += sizeof(u64); | |
1538 | ||
1539 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1540 | nr += nr_siblings; |
c320c7b7 ACM |
1541 | size += sizeof(u64); |
1542 | } | |
1543 | ||
1544 | size += entry * nr; | |
1545 | event->read_size = size; | |
1546 | } | |
1547 | ||
a723968c | 1548 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1549 | { |
1550 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1551 | u16 size = 0; |
1552 | ||
c320c7b7 ACM |
1553 | if (sample_type & PERF_SAMPLE_IP) |
1554 | size += sizeof(data->ip); | |
1555 | ||
6844c09d ACM |
1556 | if (sample_type & PERF_SAMPLE_ADDR) |
1557 | size += sizeof(data->addr); | |
1558 | ||
1559 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1560 | size += sizeof(data->period); | |
1561 | ||
c3feedf2 AK |
1562 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1563 | size += sizeof(data->weight); | |
1564 | ||
6844c09d ACM |
1565 | if (sample_type & PERF_SAMPLE_READ) |
1566 | size += event->read_size; | |
1567 | ||
d6be9ad6 SE |
1568 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1569 | size += sizeof(data->data_src.val); | |
1570 | ||
fdfbbd07 AK |
1571 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1572 | size += sizeof(data->txn); | |
1573 | ||
fc7ce9c7 KL |
1574 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
1575 | size += sizeof(data->phys_addr); | |
1576 | ||
6844c09d ACM |
1577 | event->header_size = size; |
1578 | } | |
1579 | ||
a723968c PZ |
1580 | /* |
1581 | * Called at perf_event creation and when events are attached/detached from a | |
1582 | * group. | |
1583 | */ | |
1584 | static void perf_event__header_size(struct perf_event *event) | |
1585 | { | |
1586 | __perf_event_read_size(event, | |
1587 | event->group_leader->nr_siblings); | |
1588 | __perf_event_header_size(event, event->attr.sample_type); | |
1589 | } | |
1590 | ||
6844c09d ACM |
1591 | static void perf_event__id_header_size(struct perf_event *event) |
1592 | { | |
1593 | struct perf_sample_data *data; | |
1594 | u64 sample_type = event->attr.sample_type; | |
1595 | u16 size = 0; | |
1596 | ||
c320c7b7 ACM |
1597 | if (sample_type & PERF_SAMPLE_TID) |
1598 | size += sizeof(data->tid_entry); | |
1599 | ||
1600 | if (sample_type & PERF_SAMPLE_TIME) | |
1601 | size += sizeof(data->time); | |
1602 | ||
ff3d527c AH |
1603 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1604 | size += sizeof(data->id); | |
1605 | ||
c320c7b7 ACM |
1606 | if (sample_type & PERF_SAMPLE_ID) |
1607 | size += sizeof(data->id); | |
1608 | ||
1609 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1610 | size += sizeof(data->stream_id); | |
1611 | ||
1612 | if (sample_type & PERF_SAMPLE_CPU) | |
1613 | size += sizeof(data->cpu_entry); | |
1614 | ||
6844c09d | 1615 | event->id_header_size = size; |
c320c7b7 ACM |
1616 | } |
1617 | ||
a723968c PZ |
1618 | static bool perf_event_validate_size(struct perf_event *event) |
1619 | { | |
1620 | /* | |
1621 | * The values computed here will be over-written when we actually | |
1622 | * attach the event. | |
1623 | */ | |
1624 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1625 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1626 | perf_event__id_header_size(event); | |
1627 | ||
1628 | /* | |
1629 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1630 | * Conservative limit to allow for callchains and other variable fields. | |
1631 | */ | |
1632 | if (event->read_size + event->header_size + | |
1633 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1634 | return false; | |
1635 | ||
1636 | return true; | |
1637 | } | |
1638 | ||
8a49542c PZ |
1639 | static void perf_group_attach(struct perf_event *event) |
1640 | { | |
c320c7b7 | 1641 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1642 | |
a76a82a3 PZ |
1643 | lockdep_assert_held(&event->ctx->lock); |
1644 | ||
74c3337c PZ |
1645 | /* |
1646 | * We can have double attach due to group movement in perf_event_open. | |
1647 | */ | |
1648 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1649 | return; | |
1650 | ||
8a49542c PZ |
1651 | event->attach_state |= PERF_ATTACH_GROUP; |
1652 | ||
1653 | if (group_leader == event) | |
1654 | return; | |
1655 | ||
652884fe PZ |
1656 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1657 | ||
4ff6a8de | 1658 | group_leader->group_caps &= event->event_caps; |
8a49542c PZ |
1659 | |
1660 | list_add_tail(&event->group_entry, &group_leader->sibling_list); | |
1661 | group_leader->nr_siblings++; | |
c320c7b7 ACM |
1662 | |
1663 | perf_event__header_size(group_leader); | |
1664 | ||
1665 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) | |
1666 | perf_event__header_size(pos); | |
8a49542c PZ |
1667 | } |
1668 | ||
a63eaf34 | 1669 | /* |
cdd6c482 | 1670 | * Remove a event from the lists for its context. |
fccc714b | 1671 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1672 | */ |
04289bb9 | 1673 | static void |
cdd6c482 | 1674 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1675 | { |
652884fe PZ |
1676 | WARN_ON_ONCE(event->ctx != ctx); |
1677 | lockdep_assert_held(&ctx->lock); | |
1678 | ||
8a49542c PZ |
1679 | /* |
1680 | * We can have double detach due to exit/hot-unplug + close. | |
1681 | */ | |
1682 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1683 | return; |
8a49542c PZ |
1684 | |
1685 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1686 | ||
db4a8356 | 1687 | list_update_cgroup_event(event, ctx, false); |
e5d1367f | 1688 | |
cdd6c482 IM |
1689 | ctx->nr_events--; |
1690 | if (event->attr.inherit_stat) | |
bfbd3381 | 1691 | ctx->nr_stat--; |
8bc20959 | 1692 | |
cdd6c482 | 1693 | list_del_rcu(&event->event_entry); |
04289bb9 | 1694 | |
8a49542c PZ |
1695 | if (event->group_leader == event) |
1696 | list_del_init(&event->group_entry); | |
5c148194 | 1697 | |
b2e74a26 SE |
1698 | /* |
1699 | * If event was in error state, then keep it | |
1700 | * that way, otherwise bogus counts will be | |
1701 | * returned on read(). The only way to get out | |
1702 | * of error state is by explicit re-enabling | |
1703 | * of the event | |
1704 | */ | |
1705 | if (event->state > PERF_EVENT_STATE_OFF) | |
0d3d73aa | 1706 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); |
5a3126d4 PZ |
1707 | |
1708 | ctx->generation++; | |
050735b0 PZ |
1709 | } |
1710 | ||
8a49542c | 1711 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1712 | { |
1713 | struct perf_event *sibling, *tmp; | |
8a49542c PZ |
1714 | struct list_head *list = NULL; |
1715 | ||
a76a82a3 PZ |
1716 | lockdep_assert_held(&event->ctx->lock); |
1717 | ||
8a49542c PZ |
1718 | /* |
1719 | * We can have double detach due to exit/hot-unplug + close. | |
1720 | */ | |
1721 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1722 | return; | |
1723 | ||
1724 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1725 | ||
1726 | /* | |
1727 | * If this is a sibling, remove it from its group. | |
1728 | */ | |
1729 | if (event->group_leader != event) { | |
1730 | list_del_init(&event->group_entry); | |
1731 | event->group_leader->nr_siblings--; | |
c320c7b7 | 1732 | goto out; |
8a49542c PZ |
1733 | } |
1734 | ||
1735 | if (!list_empty(&event->group_entry)) | |
1736 | list = &event->group_entry; | |
2e2af50b | 1737 | |
04289bb9 | 1738 | /* |
cdd6c482 IM |
1739 | * If this was a group event with sibling events then |
1740 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1741 | * to whatever list we are on. |
04289bb9 | 1742 | */ |
cdd6c482 | 1743 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
8a49542c PZ |
1744 | if (list) |
1745 | list_move_tail(&sibling->group_entry, list); | |
04289bb9 | 1746 | sibling->group_leader = sibling; |
d6f962b5 FW |
1747 | |
1748 | /* Inherit group flags from the previous leader */ | |
4ff6a8de | 1749 | sibling->group_caps = event->group_caps; |
652884fe PZ |
1750 | |
1751 | WARN_ON_ONCE(sibling->ctx != event->ctx); | |
04289bb9 | 1752 | } |
c320c7b7 ACM |
1753 | |
1754 | out: | |
1755 | perf_event__header_size(event->group_leader); | |
1756 | ||
1757 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) | |
1758 | perf_event__header_size(tmp); | |
04289bb9 IM |
1759 | } |
1760 | ||
fadfe7be JO |
1761 | static bool is_orphaned_event(struct perf_event *event) |
1762 | { | |
a69b0ca4 | 1763 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1764 | } |
1765 | ||
2c81a647 | 1766 | static inline int __pmu_filter_match(struct perf_event *event) |
66eb579e MR |
1767 | { |
1768 | struct pmu *pmu = event->pmu; | |
1769 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1770 | } | |
1771 | ||
2c81a647 MR |
1772 | /* |
1773 | * Check whether we should attempt to schedule an event group based on | |
1774 | * PMU-specific filtering. An event group can consist of HW and SW events, | |
1775 | * potentially with a SW leader, so we must check all the filters, to | |
1776 | * determine whether a group is schedulable: | |
1777 | */ | |
1778 | static inline int pmu_filter_match(struct perf_event *event) | |
1779 | { | |
1780 | struct perf_event *child; | |
1781 | ||
1782 | if (!__pmu_filter_match(event)) | |
1783 | return 0; | |
1784 | ||
1785 | list_for_each_entry(child, &event->sibling_list, group_entry) { | |
1786 | if (!__pmu_filter_match(child)) | |
1787 | return 0; | |
1788 | } | |
1789 | ||
1790 | return 1; | |
1791 | } | |
1792 | ||
fa66f07a SE |
1793 | static inline int |
1794 | event_filter_match(struct perf_event *event) | |
1795 | { | |
0b8f1e2e PZ |
1796 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
1797 | perf_cgroup_match(event) && pmu_filter_match(event); | |
fa66f07a SE |
1798 | } |
1799 | ||
9ffcfa6f SE |
1800 | static void |
1801 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1802 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1803 | struct perf_event_context *ctx) |
3b6f9e5c | 1804 | { |
0d3d73aa | 1805 | enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; |
652884fe PZ |
1806 | |
1807 | WARN_ON_ONCE(event->ctx != ctx); | |
1808 | lockdep_assert_held(&ctx->lock); | |
1809 | ||
cdd6c482 | 1810 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1811 | return; |
3b6f9e5c | 1812 | |
44377277 AS |
1813 | perf_pmu_disable(event->pmu); |
1814 | ||
28a967c3 PZ |
1815 | event->pmu->del(event, 0); |
1816 | event->oncpu = -1; | |
0d3d73aa | 1817 | |
cdd6c482 IM |
1818 | if (event->pending_disable) { |
1819 | event->pending_disable = 0; | |
0d3d73aa | 1820 | state = PERF_EVENT_STATE_OFF; |
970892a9 | 1821 | } |
0d3d73aa | 1822 | perf_event_set_state(event, state); |
3b6f9e5c | 1823 | |
cdd6c482 | 1824 | if (!is_software_event(event)) |
3b6f9e5c | 1825 | cpuctx->active_oncpu--; |
2fde4f94 MR |
1826 | if (!--ctx->nr_active) |
1827 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
1828 | if (event->attr.freq && event->attr.sample_freq) |
1829 | ctx->nr_freq--; | |
cdd6c482 | 1830 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 1831 | cpuctx->exclusive = 0; |
44377277 AS |
1832 | |
1833 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
1834 | } |
1835 | ||
d859e29f | 1836 | static void |
cdd6c482 | 1837 | group_sched_out(struct perf_event *group_event, |
d859e29f | 1838 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1839 | struct perf_event_context *ctx) |
d859e29f | 1840 | { |
cdd6c482 | 1841 | struct perf_event *event; |
0d3d73aa PZ |
1842 | |
1843 | if (group_event->state != PERF_EVENT_STATE_ACTIVE) | |
1844 | return; | |
d859e29f | 1845 | |
3f005e7d MR |
1846 | perf_pmu_disable(ctx->pmu); |
1847 | ||
cdd6c482 | 1848 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
1849 | |
1850 | /* | |
1851 | * Schedule out siblings (if any): | |
1852 | */ | |
cdd6c482 IM |
1853 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
1854 | event_sched_out(event, cpuctx, ctx); | |
d859e29f | 1855 | |
3f005e7d MR |
1856 | perf_pmu_enable(ctx->pmu); |
1857 | ||
0d3d73aa | 1858 | if (group_event->attr.exclusive) |
d859e29f PM |
1859 | cpuctx->exclusive = 0; |
1860 | } | |
1861 | ||
45a0e07a | 1862 | #define DETACH_GROUP 0x01UL |
0017960f | 1863 | |
0793a61d | 1864 | /* |
cdd6c482 | 1865 | * Cross CPU call to remove a performance event |
0793a61d | 1866 | * |
cdd6c482 | 1867 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
1868 | * remove it from the context list. |
1869 | */ | |
fae3fde6 PZ |
1870 | static void |
1871 | __perf_remove_from_context(struct perf_event *event, | |
1872 | struct perf_cpu_context *cpuctx, | |
1873 | struct perf_event_context *ctx, | |
1874 | void *info) | |
0793a61d | 1875 | { |
45a0e07a | 1876 | unsigned long flags = (unsigned long)info; |
0793a61d | 1877 | |
3c5c8711 PZ |
1878 | if (ctx->is_active & EVENT_TIME) { |
1879 | update_context_time(ctx); | |
1880 | update_cgrp_time_from_cpuctx(cpuctx); | |
1881 | } | |
1882 | ||
cdd6c482 | 1883 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 1884 | if (flags & DETACH_GROUP) |
46ce0fe9 | 1885 | perf_group_detach(event); |
cdd6c482 | 1886 | list_del_event(event, ctx); |
39a43640 PZ |
1887 | |
1888 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 1889 | ctx->is_active = 0; |
39a43640 PZ |
1890 | if (ctx->task) { |
1891 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
1892 | cpuctx->task_ctx = NULL; | |
1893 | } | |
64ce3126 | 1894 | } |
0793a61d TG |
1895 | } |
1896 | ||
0793a61d | 1897 | /* |
cdd6c482 | 1898 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 1899 | * |
cdd6c482 IM |
1900 | * If event->ctx is a cloned context, callers must make sure that |
1901 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
1902 | * remains valid. This is OK when called from perf_release since |
1903 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 1904 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 1905 | * context has been detached from its task. |
0793a61d | 1906 | */ |
45a0e07a | 1907 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 1908 | { |
a76a82a3 PZ |
1909 | struct perf_event_context *ctx = event->ctx; |
1910 | ||
1911 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 1912 | |
45a0e07a | 1913 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
a76a82a3 PZ |
1914 | |
1915 | /* | |
1916 | * The above event_function_call() can NO-OP when it hits | |
1917 | * TASK_TOMBSTONE. In that case we must already have been detached | |
1918 | * from the context (by perf_event_exit_event()) but the grouping | |
1919 | * might still be in-tact. | |
1920 | */ | |
1921 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); | |
1922 | if ((flags & DETACH_GROUP) && | |
1923 | (event->attach_state & PERF_ATTACH_GROUP)) { | |
1924 | /* | |
1925 | * Since in that case we cannot possibly be scheduled, simply | |
1926 | * detach now. | |
1927 | */ | |
1928 | raw_spin_lock_irq(&ctx->lock); | |
1929 | perf_group_detach(event); | |
1930 | raw_spin_unlock_irq(&ctx->lock); | |
1931 | } | |
0793a61d TG |
1932 | } |
1933 | ||
d859e29f | 1934 | /* |
cdd6c482 | 1935 | * Cross CPU call to disable a performance event |
d859e29f | 1936 | */ |
fae3fde6 PZ |
1937 | static void __perf_event_disable(struct perf_event *event, |
1938 | struct perf_cpu_context *cpuctx, | |
1939 | struct perf_event_context *ctx, | |
1940 | void *info) | |
7b648018 | 1941 | { |
fae3fde6 PZ |
1942 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
1943 | return; | |
7b648018 | 1944 | |
3c5c8711 PZ |
1945 | if (ctx->is_active & EVENT_TIME) { |
1946 | update_context_time(ctx); | |
1947 | update_cgrp_time_from_event(event); | |
1948 | } | |
1949 | ||
fae3fde6 PZ |
1950 | if (event == event->group_leader) |
1951 | group_sched_out(event, cpuctx, ctx); | |
1952 | else | |
1953 | event_sched_out(event, cpuctx, ctx); | |
0d3d73aa PZ |
1954 | |
1955 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); | |
7b648018 PZ |
1956 | } |
1957 | ||
d859e29f | 1958 | /* |
cdd6c482 | 1959 | * Disable a event. |
c93f7669 | 1960 | * |
cdd6c482 IM |
1961 | * If event->ctx is a cloned context, callers must make sure that |
1962 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 1963 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
1964 | * perf_event_for_each_child or perf_event_for_each because they |
1965 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
1966 | * goes to exit will block in perf_event_exit_event(). |
1967 | * | |
cdd6c482 | 1968 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 1969 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 1970 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 1971 | */ |
f63a8daa | 1972 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 1973 | { |
cdd6c482 | 1974 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 1975 | |
e625cce1 | 1976 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 1977 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 1978 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1979 | return; |
53cfbf59 | 1980 | } |
e625cce1 | 1981 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1982 | |
fae3fde6 PZ |
1983 | event_function_call(event, __perf_event_disable, NULL); |
1984 | } | |
1985 | ||
1986 | void perf_event_disable_local(struct perf_event *event) | |
1987 | { | |
1988 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 1989 | } |
f63a8daa PZ |
1990 | |
1991 | /* | |
1992 | * Strictly speaking kernel users cannot create groups and therefore this | |
1993 | * interface does not need the perf_event_ctx_lock() magic. | |
1994 | */ | |
1995 | void perf_event_disable(struct perf_event *event) | |
1996 | { | |
1997 | struct perf_event_context *ctx; | |
1998 | ||
1999 | ctx = perf_event_ctx_lock(event); | |
2000 | _perf_event_disable(event); | |
2001 | perf_event_ctx_unlock(event, ctx); | |
2002 | } | |
dcfce4a0 | 2003 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2004 | |
5aab90ce JO |
2005 | void perf_event_disable_inatomic(struct perf_event *event) |
2006 | { | |
2007 | event->pending_disable = 1; | |
2008 | irq_work_queue(&event->pending); | |
2009 | } | |
2010 | ||
e5d1367f | 2011 | static void perf_set_shadow_time(struct perf_event *event, |
0d3d73aa | 2012 | struct perf_event_context *ctx) |
e5d1367f SE |
2013 | { |
2014 | /* | |
2015 | * use the correct time source for the time snapshot | |
2016 | * | |
2017 | * We could get by without this by leveraging the | |
2018 | * fact that to get to this function, the caller | |
2019 | * has most likely already called update_context_time() | |
2020 | * and update_cgrp_time_xx() and thus both timestamp | |
2021 | * are identical (or very close). Given that tstamp is, | |
2022 | * already adjusted for cgroup, we could say that: | |
2023 | * tstamp - ctx->timestamp | |
2024 | * is equivalent to | |
2025 | * tstamp - cgrp->timestamp. | |
2026 | * | |
2027 | * Then, in perf_output_read(), the calculation would | |
2028 | * work with no changes because: | |
2029 | * - event is guaranteed scheduled in | |
2030 | * - no scheduled out in between | |
2031 | * - thus the timestamp would be the same | |
2032 | * | |
2033 | * But this is a bit hairy. | |
2034 | * | |
2035 | * So instead, we have an explicit cgroup call to remain | |
2036 | * within the time time source all along. We believe it | |
2037 | * is cleaner and simpler to understand. | |
2038 | */ | |
2039 | if (is_cgroup_event(event)) | |
0d3d73aa | 2040 | perf_cgroup_set_shadow_time(event, event->tstamp); |
e5d1367f | 2041 | else |
0d3d73aa | 2042 | event->shadow_ctx_time = event->tstamp - ctx->timestamp; |
e5d1367f SE |
2043 | } |
2044 | ||
4fe757dd PZ |
2045 | #define MAX_INTERRUPTS (~0ULL) |
2046 | ||
2047 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2048 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2049 | |
235c7fc7 | 2050 | static int |
9ffcfa6f | 2051 | event_sched_in(struct perf_event *event, |
235c7fc7 | 2052 | struct perf_cpu_context *cpuctx, |
6e37738a | 2053 | struct perf_event_context *ctx) |
235c7fc7 | 2054 | { |
44377277 | 2055 | int ret = 0; |
4158755d | 2056 | |
63342411 PZ |
2057 | lockdep_assert_held(&ctx->lock); |
2058 | ||
cdd6c482 | 2059 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2060 | return 0; |
2061 | ||
95ff4ca2 AS |
2062 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2063 | /* | |
0c1cbc18 PZ |
2064 | * Order event::oncpu write to happen before the ACTIVE state is |
2065 | * visible. This allows perf_event_{stop,read}() to observe the correct | |
2066 | * ->oncpu if it sees ACTIVE. | |
95ff4ca2 AS |
2067 | */ |
2068 | smp_wmb(); | |
0d3d73aa | 2069 | perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); |
4fe757dd PZ |
2070 | |
2071 | /* | |
2072 | * Unthrottle events, since we scheduled we might have missed several | |
2073 | * ticks already, also for a heavily scheduling task there is little | |
2074 | * guarantee it'll get a tick in a timely manner. | |
2075 | */ | |
2076 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2077 | perf_log_throttle(event, 1); | |
2078 | event->hw.interrupts = 0; | |
2079 | } | |
2080 | ||
44377277 AS |
2081 | perf_pmu_disable(event->pmu); |
2082 | ||
0d3d73aa | 2083 | perf_set_shadow_time(event, ctx); |
72f669c0 | 2084 | |
ec0d7729 AS |
2085 | perf_log_itrace_start(event); |
2086 | ||
a4eaf7f1 | 2087 | if (event->pmu->add(event, PERF_EF_START)) { |
0d3d73aa | 2088 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
cdd6c482 | 2089 | event->oncpu = -1; |
44377277 AS |
2090 | ret = -EAGAIN; |
2091 | goto out; | |
235c7fc7 IM |
2092 | } |
2093 | ||
cdd6c482 | 2094 | if (!is_software_event(event)) |
3b6f9e5c | 2095 | cpuctx->active_oncpu++; |
2fde4f94 MR |
2096 | if (!ctx->nr_active++) |
2097 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
2098 | if (event->attr.freq && event->attr.sample_freq) |
2099 | ctx->nr_freq++; | |
235c7fc7 | 2100 | |
cdd6c482 | 2101 | if (event->attr.exclusive) |
3b6f9e5c PM |
2102 | cpuctx->exclusive = 1; |
2103 | ||
44377277 AS |
2104 | out: |
2105 | perf_pmu_enable(event->pmu); | |
2106 | ||
2107 | return ret; | |
235c7fc7 IM |
2108 | } |
2109 | ||
6751b71e | 2110 | static int |
cdd6c482 | 2111 | group_sched_in(struct perf_event *group_event, |
6751b71e | 2112 | struct perf_cpu_context *cpuctx, |
6e37738a | 2113 | struct perf_event_context *ctx) |
6751b71e | 2114 | { |
6bde9b6c | 2115 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 2116 | struct pmu *pmu = ctx->pmu; |
6751b71e | 2117 | |
cdd6c482 | 2118 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2119 | return 0; |
2120 | ||
fbbe0701 | 2121 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2122 | |
9ffcfa6f | 2123 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2124 | pmu->cancel_txn(pmu); |
272325c4 | 2125 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2126 | return -EAGAIN; |
90151c35 | 2127 | } |
6751b71e PM |
2128 | |
2129 | /* | |
2130 | * Schedule in siblings as one group (if any): | |
2131 | */ | |
cdd6c482 | 2132 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
9ffcfa6f | 2133 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2134 | partial_group = event; |
6751b71e PM |
2135 | goto group_error; |
2136 | } | |
2137 | } | |
2138 | ||
9ffcfa6f | 2139 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2140 | return 0; |
9ffcfa6f | 2141 | |
6751b71e PM |
2142 | group_error: |
2143 | /* | |
2144 | * Groups can be scheduled in as one unit only, so undo any | |
2145 | * partial group before returning: | |
0d3d73aa | 2146 | * The events up to the failed event are scheduled out normally. |
6751b71e | 2147 | */ |
cdd6c482 IM |
2148 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
2149 | if (event == partial_group) | |
0d3d73aa | 2150 | break; |
d7842da4 | 2151 | |
0d3d73aa | 2152 | event_sched_out(event, cpuctx, ctx); |
6751b71e | 2153 | } |
9ffcfa6f | 2154 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2155 | |
ad5133b7 | 2156 | pmu->cancel_txn(pmu); |
90151c35 | 2157 | |
272325c4 | 2158 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2159 | |
6751b71e PM |
2160 | return -EAGAIN; |
2161 | } | |
2162 | ||
3b6f9e5c | 2163 | /* |
cdd6c482 | 2164 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2165 | */ |
cdd6c482 | 2166 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2167 | struct perf_cpu_context *cpuctx, |
2168 | int can_add_hw) | |
2169 | { | |
2170 | /* | |
cdd6c482 | 2171 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2172 | */ |
4ff6a8de | 2173 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2174 | return 1; |
2175 | /* | |
2176 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2177 | * events can go on. |
3b6f9e5c PM |
2178 | */ |
2179 | if (cpuctx->exclusive) | |
2180 | return 0; | |
2181 | /* | |
2182 | * If this group is exclusive and there are already | |
cdd6c482 | 2183 | * events on the CPU, it can't go on. |
3b6f9e5c | 2184 | */ |
cdd6c482 | 2185 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2186 | return 0; |
2187 | /* | |
2188 | * Otherwise, try to add it if all previous groups were able | |
2189 | * to go on. | |
2190 | */ | |
2191 | return can_add_hw; | |
2192 | } | |
2193 | ||
cdd6c482 IM |
2194 | static void add_event_to_ctx(struct perf_event *event, |
2195 | struct perf_event_context *ctx) | |
53cfbf59 | 2196 | { |
cdd6c482 | 2197 | list_add_event(event, ctx); |
8a49542c | 2198 | perf_group_attach(event); |
53cfbf59 PM |
2199 | } |
2200 | ||
bd2afa49 PZ |
2201 | static void ctx_sched_out(struct perf_event_context *ctx, |
2202 | struct perf_cpu_context *cpuctx, | |
2203 | enum event_type_t event_type); | |
2c29ef0f PZ |
2204 | static void |
2205 | ctx_sched_in(struct perf_event_context *ctx, | |
2206 | struct perf_cpu_context *cpuctx, | |
2207 | enum event_type_t event_type, | |
2208 | struct task_struct *task); | |
fe4b04fa | 2209 | |
bd2afa49 | 2210 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2211 | struct perf_event_context *ctx, |
2212 | enum event_type_t event_type) | |
bd2afa49 PZ |
2213 | { |
2214 | if (!cpuctx->task_ctx) | |
2215 | return; | |
2216 | ||
2217 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2218 | return; | |
2219 | ||
487f05e1 | 2220 | ctx_sched_out(ctx, cpuctx, event_type); |
bd2afa49 PZ |
2221 | } |
2222 | ||
dce5855b PZ |
2223 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2224 | struct perf_event_context *ctx, | |
2225 | struct task_struct *task) | |
2226 | { | |
2227 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2228 | if (ctx) | |
2229 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2230 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2231 | if (ctx) | |
2232 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2233 | } | |
2234 | ||
487f05e1 AS |
2235 | /* |
2236 | * We want to maintain the following priority of scheduling: | |
2237 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2238 | * - task pinned (EVENT_PINNED) | |
2239 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2240 | * - task flexible (EVENT_FLEXIBLE). | |
2241 | * | |
2242 | * In order to avoid unscheduling and scheduling back in everything every | |
2243 | * time an event is added, only do it for the groups of equal priority and | |
2244 | * below. | |
2245 | * | |
2246 | * This can be called after a batch operation on task events, in which case | |
2247 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2248 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2249 | */ | |
3e349507 | 2250 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2251 | struct perf_event_context *task_ctx, |
2252 | enum event_type_t event_type) | |
0017960f | 2253 | { |
732fd871 | 2254 | enum event_type_t ctx_event_type; |
487f05e1 AS |
2255 | bool cpu_event = !!(event_type & EVENT_CPU); |
2256 | ||
2257 | /* | |
2258 | * If pinned groups are involved, flexible groups also need to be | |
2259 | * scheduled out. | |
2260 | */ | |
2261 | if (event_type & EVENT_PINNED) | |
2262 | event_type |= EVENT_FLEXIBLE; | |
2263 | ||
732fd871 SL |
2264 | ctx_event_type = event_type & EVENT_ALL; |
2265 | ||
3e349507 PZ |
2266 | perf_pmu_disable(cpuctx->ctx.pmu); |
2267 | if (task_ctx) | |
487f05e1 AS |
2268 | task_ctx_sched_out(cpuctx, task_ctx, event_type); |
2269 | ||
2270 | /* | |
2271 | * Decide which cpu ctx groups to schedule out based on the types | |
2272 | * of events that caused rescheduling: | |
2273 | * - EVENT_CPU: schedule out corresponding groups; | |
2274 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2275 | * - otherwise, do nothing more. | |
2276 | */ | |
2277 | if (cpu_event) | |
2278 | cpu_ctx_sched_out(cpuctx, ctx_event_type); | |
2279 | else if (ctx_event_type & EVENT_PINNED) | |
2280 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
2281 | ||
3e349507 PZ |
2282 | perf_event_sched_in(cpuctx, task_ctx, current); |
2283 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2284 | } |
2285 | ||
0793a61d | 2286 | /* |
cdd6c482 | 2287 | * Cross CPU call to install and enable a performance event |
682076ae | 2288 | * |
a096309b PZ |
2289 | * Very similar to remote_function() + event_function() but cannot assume that |
2290 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2291 | */ |
fe4b04fa | 2292 | static int __perf_install_in_context(void *info) |
0793a61d | 2293 | { |
a096309b PZ |
2294 | struct perf_event *event = info; |
2295 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2296 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2297 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2298 | bool reprogram = true; |
a096309b | 2299 | int ret = 0; |
0793a61d | 2300 | |
63b6da39 | 2301 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2302 | if (ctx->task) { |
b58f6b0d PZ |
2303 | raw_spin_lock(&ctx->lock); |
2304 | task_ctx = ctx; | |
a096309b | 2305 | |
63cae12b | 2306 | reprogram = (ctx->task == current); |
b58f6b0d | 2307 | |
39a43640 | 2308 | /* |
63cae12b PZ |
2309 | * If the task is running, it must be running on this CPU, |
2310 | * otherwise we cannot reprogram things. | |
2311 | * | |
2312 | * If its not running, we don't care, ctx->lock will | |
2313 | * serialize against it becoming runnable. | |
39a43640 | 2314 | */ |
63cae12b PZ |
2315 | if (task_curr(ctx->task) && !reprogram) { |
2316 | ret = -ESRCH; | |
2317 | goto unlock; | |
2318 | } | |
a096309b | 2319 | |
63cae12b | 2320 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2321 | } else if (task_ctx) { |
2322 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2323 | } |
b58f6b0d | 2324 | |
63cae12b | 2325 | if (reprogram) { |
a096309b PZ |
2326 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
2327 | add_event_to_ctx(event, ctx); | |
487f05e1 | 2328 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2329 | } else { |
2330 | add_event_to_ctx(event, ctx); | |
2331 | } | |
2332 | ||
63b6da39 | 2333 | unlock: |
2c29ef0f | 2334 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2335 | |
a096309b | 2336 | return ret; |
0793a61d TG |
2337 | } |
2338 | ||
2339 | /* | |
a096309b PZ |
2340 | * Attach a performance event to a context. |
2341 | * | |
2342 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2343 | */ |
2344 | static void | |
cdd6c482 IM |
2345 | perf_install_in_context(struct perf_event_context *ctx, |
2346 | struct perf_event *event, | |
0793a61d TG |
2347 | int cpu) |
2348 | { | |
a096309b | 2349 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2350 | |
fe4b04fa PZ |
2351 | lockdep_assert_held(&ctx->mutex); |
2352 | ||
0cda4c02 YZ |
2353 | if (event->cpu != -1) |
2354 | event->cpu = cpu; | |
c3f00c70 | 2355 | |
0b8f1e2e PZ |
2356 | /* |
2357 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2358 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2359 | */ | |
2360 | smp_store_release(&event->ctx, ctx); | |
2361 | ||
a096309b PZ |
2362 | if (!task) { |
2363 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2364 | return; | |
2365 | } | |
2366 | ||
2367 | /* | |
2368 | * Should not happen, we validate the ctx is still alive before calling. | |
2369 | */ | |
2370 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2371 | return; | |
2372 | ||
39a43640 PZ |
2373 | /* |
2374 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2375 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2376 | * |
2377 | * Instead we use task_curr(), which tells us if the task is running. | |
2378 | * However, since we use task_curr() outside of rq::lock, we can race | |
2379 | * against the actual state. This means the result can be wrong. | |
2380 | * | |
2381 | * If we get a false positive, we retry, this is harmless. | |
2382 | * | |
2383 | * If we get a false negative, things are complicated. If we are after | |
2384 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2385 | * value must be correct. If we're before, it doesn't matter since | |
2386 | * perf_event_context_sched_in() will program the counter. | |
2387 | * | |
2388 | * However, this hinges on the remote context switch having observed | |
2389 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2390 | * ctx::lock in perf_event_context_sched_in(). | |
2391 | * | |
2392 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2393 | * we know any future context switch of task must see the | |
2394 | * perf_event_ctpx[] store. | |
39a43640 | 2395 | */ |
63cae12b | 2396 | |
63b6da39 | 2397 | /* |
63cae12b PZ |
2398 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2399 | * task_cpu() load, such that if the IPI then does not find the task | |
2400 | * running, a future context switch of that task must observe the | |
2401 | * store. | |
63b6da39 | 2402 | */ |
63cae12b PZ |
2403 | smp_mb(); |
2404 | again: | |
2405 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2406 | return; |
2407 | ||
2408 | raw_spin_lock_irq(&ctx->lock); | |
2409 | task = ctx->task; | |
84c4e620 | 2410 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2411 | /* |
2412 | * Cannot happen because we already checked above (which also | |
2413 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2414 | * against perf_event_exit_task_context(). | |
2415 | */ | |
63b6da39 PZ |
2416 | raw_spin_unlock_irq(&ctx->lock); |
2417 | return; | |
2418 | } | |
39a43640 | 2419 | /* |
63cae12b PZ |
2420 | * If the task is not running, ctx->lock will avoid it becoming so, |
2421 | * thus we can safely install the event. | |
39a43640 | 2422 | */ |
63cae12b PZ |
2423 | if (task_curr(task)) { |
2424 | raw_spin_unlock_irq(&ctx->lock); | |
2425 | goto again; | |
2426 | } | |
2427 | add_event_to_ctx(event, ctx); | |
2428 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2429 | } |
2430 | ||
d859e29f | 2431 | /* |
cdd6c482 | 2432 | * Cross CPU call to enable a performance event |
d859e29f | 2433 | */ |
fae3fde6 PZ |
2434 | static void __perf_event_enable(struct perf_event *event, |
2435 | struct perf_cpu_context *cpuctx, | |
2436 | struct perf_event_context *ctx, | |
2437 | void *info) | |
04289bb9 | 2438 | { |
cdd6c482 | 2439 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2440 | struct perf_event_context *task_ctx; |
04289bb9 | 2441 | |
6e801e01 PZ |
2442 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2443 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2444 | return; |
3cbed429 | 2445 | |
bd2afa49 PZ |
2446 | if (ctx->is_active) |
2447 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2448 | ||
0d3d73aa | 2449 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
04289bb9 | 2450 | |
fae3fde6 PZ |
2451 | if (!ctx->is_active) |
2452 | return; | |
2453 | ||
e5d1367f | 2454 | if (!event_filter_match(event)) { |
bd2afa49 | 2455 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2456 | return; |
e5d1367f | 2457 | } |
f4c4176f | 2458 | |
04289bb9 | 2459 | /* |
cdd6c482 | 2460 | * If the event is in a group and isn't the group leader, |
d859e29f | 2461 | * then don't put it on unless the group is on. |
04289bb9 | 2462 | */ |
bd2afa49 PZ |
2463 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2464 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2465 | return; |
bd2afa49 | 2466 | } |
fe4b04fa | 2467 | |
fae3fde6 PZ |
2468 | task_ctx = cpuctx->task_ctx; |
2469 | if (ctx->task) | |
2470 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2471 | |
487f05e1 | 2472 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2473 | } |
2474 | ||
d859e29f | 2475 | /* |
cdd6c482 | 2476 | * Enable a event. |
c93f7669 | 2477 | * |
cdd6c482 IM |
2478 | * If event->ctx is a cloned context, callers must make sure that |
2479 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2480 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2481 | * perf_event_for_each_child or perf_event_for_each as described |
2482 | * for perf_event_disable. | |
d859e29f | 2483 | */ |
f63a8daa | 2484 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2485 | { |
cdd6c482 | 2486 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2487 | |
7b648018 | 2488 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2489 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2490 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2491 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2492 | return; |
2493 | } | |
2494 | ||
d859e29f | 2495 | /* |
cdd6c482 | 2496 | * If the event is in error state, clear that first. |
7b648018 PZ |
2497 | * |
2498 | * That way, if we see the event in error state below, we know that it | |
2499 | * has gone back into error state, as distinct from the task having | |
2500 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2501 | */ |
cdd6c482 IM |
2502 | if (event->state == PERF_EVENT_STATE_ERROR) |
2503 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2504 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2505 | |
fae3fde6 | 2506 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2507 | } |
f63a8daa PZ |
2508 | |
2509 | /* | |
2510 | * See perf_event_disable(); | |
2511 | */ | |
2512 | void perf_event_enable(struct perf_event *event) | |
2513 | { | |
2514 | struct perf_event_context *ctx; | |
2515 | ||
2516 | ctx = perf_event_ctx_lock(event); | |
2517 | _perf_event_enable(event); | |
2518 | perf_event_ctx_unlock(event, ctx); | |
2519 | } | |
dcfce4a0 | 2520 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2521 | |
375637bc AS |
2522 | struct stop_event_data { |
2523 | struct perf_event *event; | |
2524 | unsigned int restart; | |
2525 | }; | |
2526 | ||
95ff4ca2 AS |
2527 | static int __perf_event_stop(void *info) |
2528 | { | |
375637bc AS |
2529 | struct stop_event_data *sd = info; |
2530 | struct perf_event *event = sd->event; | |
95ff4ca2 | 2531 | |
375637bc | 2532 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
2533 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
2534 | return 0; | |
2535 | ||
2536 | /* matches smp_wmb() in event_sched_in() */ | |
2537 | smp_rmb(); | |
2538 | ||
2539 | /* | |
2540 | * There is a window with interrupts enabled before we get here, | |
2541 | * so we need to check again lest we try to stop another CPU's event. | |
2542 | */ | |
2543 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2544 | return -EAGAIN; | |
2545 | ||
2546 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2547 | ||
375637bc AS |
2548 | /* |
2549 | * May race with the actual stop (through perf_pmu_output_stop()), | |
2550 | * but it is only used for events with AUX ring buffer, and such | |
2551 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
2552 | * see comments in perf_aux_output_begin(). | |
2553 | * | |
2554 | * Since this is happening on a event-local CPU, no trace is lost | |
2555 | * while restarting. | |
2556 | */ | |
2557 | if (sd->restart) | |
c9bbdd48 | 2558 | event->pmu->start(event, 0); |
375637bc | 2559 | |
95ff4ca2 AS |
2560 | return 0; |
2561 | } | |
2562 | ||
767ae086 | 2563 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
2564 | { |
2565 | struct stop_event_data sd = { | |
2566 | .event = event, | |
767ae086 | 2567 | .restart = restart, |
375637bc AS |
2568 | }; |
2569 | int ret = 0; | |
2570 | ||
2571 | do { | |
2572 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2573 | return 0; | |
2574 | ||
2575 | /* matches smp_wmb() in event_sched_in() */ | |
2576 | smp_rmb(); | |
2577 | ||
2578 | /* | |
2579 | * We only want to restart ACTIVE events, so if the event goes | |
2580 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
2581 | * fall through with ret==-ENXIO. | |
2582 | */ | |
2583 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
2584 | __perf_event_stop, &sd); | |
2585 | } while (ret == -EAGAIN); | |
2586 | ||
2587 | return ret; | |
2588 | } | |
2589 | ||
2590 | /* | |
2591 | * In order to contain the amount of racy and tricky in the address filter | |
2592 | * configuration management, it is a two part process: | |
2593 | * | |
2594 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
2595 | * we update the addresses of corresponding vmas in | |
2596 | * event::addr_filters_offs array and bump the event::addr_filters_gen; | |
2597 | * (p2) when an event is scheduled in (pmu::add), it calls | |
2598 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
2599 | * if the generation has changed since the previous call. | |
2600 | * | |
2601 | * If (p1) happens while the event is active, we restart it to force (p2). | |
2602 | * | |
2603 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
2604 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
2605 | * ioctl; | |
2606 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
2607 | * registered mapping, called for every new mmap(), with mm::mmap_sem down | |
2608 | * for reading; | |
2609 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
2610 | * of exec. | |
2611 | */ | |
2612 | void perf_event_addr_filters_sync(struct perf_event *event) | |
2613 | { | |
2614 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
2615 | ||
2616 | if (!has_addr_filter(event)) | |
2617 | return; | |
2618 | ||
2619 | raw_spin_lock(&ifh->lock); | |
2620 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
2621 | event->pmu->addr_filters_sync(event); | |
2622 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
2623 | } | |
2624 | raw_spin_unlock(&ifh->lock); | |
2625 | } | |
2626 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
2627 | ||
f63a8daa | 2628 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2629 | { |
2023b359 | 2630 | /* |
cdd6c482 | 2631 | * not supported on inherited events |
2023b359 | 2632 | */ |
2e939d1d | 2633 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2634 | return -EINVAL; |
2635 | ||
cdd6c482 | 2636 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2637 | _perf_event_enable(event); |
2023b359 PZ |
2638 | |
2639 | return 0; | |
79f14641 | 2640 | } |
f63a8daa PZ |
2641 | |
2642 | /* | |
2643 | * See perf_event_disable() | |
2644 | */ | |
2645 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2646 | { | |
2647 | struct perf_event_context *ctx; | |
2648 | int ret; | |
2649 | ||
2650 | ctx = perf_event_ctx_lock(event); | |
2651 | ret = _perf_event_refresh(event, refresh); | |
2652 | perf_event_ctx_unlock(event, ctx); | |
2653 | ||
2654 | return ret; | |
2655 | } | |
26ca5c11 | 2656 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2657 | |
5b0311e1 FW |
2658 | static void ctx_sched_out(struct perf_event_context *ctx, |
2659 | struct perf_cpu_context *cpuctx, | |
2660 | enum event_type_t event_type) | |
235c7fc7 | 2661 | { |
db24d33e | 2662 | int is_active = ctx->is_active; |
c994d613 | 2663 | struct perf_event *event; |
235c7fc7 | 2664 | |
c994d613 | 2665 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2666 | |
39a43640 PZ |
2667 | if (likely(!ctx->nr_events)) { |
2668 | /* | |
2669 | * See __perf_remove_from_context(). | |
2670 | */ | |
2671 | WARN_ON_ONCE(ctx->is_active); | |
2672 | if (ctx->task) | |
2673 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2674 | return; |
39a43640 PZ |
2675 | } |
2676 | ||
db24d33e | 2677 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2678 | if (!(ctx->is_active & EVENT_ALL)) |
2679 | ctx->is_active = 0; | |
2680 | ||
63e30d3e PZ |
2681 | if (ctx->task) { |
2682 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2683 | if (!ctx->is_active) | |
2684 | cpuctx->task_ctx = NULL; | |
2685 | } | |
facc4307 | 2686 | |
8fdc6539 PZ |
2687 | /* |
2688 | * Always update time if it was set; not only when it changes. | |
2689 | * Otherwise we can 'forget' to update time for any but the last | |
2690 | * context we sched out. For example: | |
2691 | * | |
2692 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2693 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2694 | * | |
2695 | * would only update time for the pinned events. | |
2696 | */ | |
3cbaa590 PZ |
2697 | if (is_active & EVENT_TIME) { |
2698 | /* update (and stop) ctx time */ | |
2699 | update_context_time(ctx); | |
2700 | update_cgrp_time_from_cpuctx(cpuctx); | |
2701 | } | |
2702 | ||
8fdc6539 PZ |
2703 | is_active ^= ctx->is_active; /* changed bits */ |
2704 | ||
3cbaa590 | 2705 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2706 | return; |
5b0311e1 | 2707 | |
075e0b00 | 2708 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2709 | if (is_active & EVENT_PINNED) { |
889ff015 FW |
2710 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
2711 | group_sched_out(event, cpuctx, ctx); | |
9ed6060d | 2712 | } |
889ff015 | 2713 | |
3cbaa590 | 2714 | if (is_active & EVENT_FLEXIBLE) { |
889ff015 | 2715 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
8c9ed8e1 | 2716 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2717 | } |
1b9a644f | 2718 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2719 | } |
2720 | ||
564c2b21 | 2721 | /* |
5a3126d4 PZ |
2722 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2723 | * cloned from the same version of the same context. | |
2724 | * | |
2725 | * Equivalence is measured using a generation number in the context that is | |
2726 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2727 | * and list_del_event(). | |
564c2b21 | 2728 | */ |
cdd6c482 IM |
2729 | static int context_equiv(struct perf_event_context *ctx1, |
2730 | struct perf_event_context *ctx2) | |
564c2b21 | 2731 | { |
211de6eb PZ |
2732 | lockdep_assert_held(&ctx1->lock); |
2733 | lockdep_assert_held(&ctx2->lock); | |
2734 | ||
5a3126d4 PZ |
2735 | /* Pinning disables the swap optimization */ |
2736 | if (ctx1->pin_count || ctx2->pin_count) | |
2737 | return 0; | |
2738 | ||
2739 | /* If ctx1 is the parent of ctx2 */ | |
2740 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2741 | return 1; | |
2742 | ||
2743 | /* If ctx2 is the parent of ctx1 */ | |
2744 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2745 | return 1; | |
2746 | ||
2747 | /* | |
2748 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2749 | * hierarchy, see perf_event_init_context(). | |
2750 | */ | |
2751 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2752 | ctx1->parent_gen == ctx2->parent_gen) | |
2753 | return 1; | |
2754 | ||
2755 | /* Unmatched */ | |
2756 | return 0; | |
564c2b21 PM |
2757 | } |
2758 | ||
cdd6c482 IM |
2759 | static void __perf_event_sync_stat(struct perf_event *event, |
2760 | struct perf_event *next_event) | |
bfbd3381 PZ |
2761 | { |
2762 | u64 value; | |
2763 | ||
cdd6c482 | 2764 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2765 | return; |
2766 | ||
2767 | /* | |
cdd6c482 | 2768 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2769 | * because we're in the middle of a context switch and have IRQs |
2770 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2771 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2772 | * don't need to use it. |
2773 | */ | |
0d3d73aa | 2774 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
3dbebf15 | 2775 | event->pmu->read(event); |
bfbd3381 | 2776 | |
0d3d73aa | 2777 | perf_event_update_time(event); |
bfbd3381 PZ |
2778 | |
2779 | /* | |
cdd6c482 | 2780 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2781 | * values when we flip the contexts. |
2782 | */ | |
e7850595 PZ |
2783 | value = local64_read(&next_event->count); |
2784 | value = local64_xchg(&event->count, value); | |
2785 | local64_set(&next_event->count, value); | |
bfbd3381 | 2786 | |
cdd6c482 IM |
2787 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2788 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2789 | |
bfbd3381 | 2790 | /* |
19d2e755 | 2791 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2792 | */ |
cdd6c482 IM |
2793 | perf_event_update_userpage(event); |
2794 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2795 | } |
2796 | ||
cdd6c482 IM |
2797 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2798 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2799 | { |
cdd6c482 | 2800 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2801 | |
2802 | if (!ctx->nr_stat) | |
2803 | return; | |
2804 | ||
02ffdbc8 PZ |
2805 | update_context_time(ctx); |
2806 | ||
cdd6c482 IM |
2807 | event = list_first_entry(&ctx->event_list, |
2808 | struct perf_event, event_entry); | |
bfbd3381 | 2809 | |
cdd6c482 IM |
2810 | next_event = list_first_entry(&next_ctx->event_list, |
2811 | struct perf_event, event_entry); | |
bfbd3381 | 2812 | |
cdd6c482 IM |
2813 | while (&event->event_entry != &ctx->event_list && |
2814 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 2815 | |
cdd6c482 | 2816 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 2817 | |
cdd6c482 IM |
2818 | event = list_next_entry(event, event_entry); |
2819 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
2820 | } |
2821 | } | |
2822 | ||
fe4b04fa PZ |
2823 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
2824 | struct task_struct *next) | |
0793a61d | 2825 | { |
8dc85d54 | 2826 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 2827 | struct perf_event_context *next_ctx; |
5a3126d4 | 2828 | struct perf_event_context *parent, *next_parent; |
108b02cf | 2829 | struct perf_cpu_context *cpuctx; |
c93f7669 | 2830 | int do_switch = 1; |
0793a61d | 2831 | |
108b02cf PZ |
2832 | if (likely(!ctx)) |
2833 | return; | |
10989fb2 | 2834 | |
108b02cf PZ |
2835 | cpuctx = __get_cpu_context(ctx); |
2836 | if (!cpuctx->task_ctx) | |
0793a61d TG |
2837 | return; |
2838 | ||
c93f7669 | 2839 | rcu_read_lock(); |
8dc85d54 | 2840 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
2841 | if (!next_ctx) |
2842 | goto unlock; | |
2843 | ||
2844 | parent = rcu_dereference(ctx->parent_ctx); | |
2845 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
2846 | ||
2847 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 2848 | if (!parent && !next_parent) |
5a3126d4 PZ |
2849 | goto unlock; |
2850 | ||
2851 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
2852 | /* |
2853 | * Looks like the two contexts are clones, so we might be | |
2854 | * able to optimize the context switch. We lock both | |
2855 | * contexts and check that they are clones under the | |
2856 | * lock (including re-checking that neither has been | |
2857 | * uncloned in the meantime). It doesn't matter which | |
2858 | * order we take the locks because no other cpu could | |
2859 | * be trying to lock both of these tasks. | |
2860 | */ | |
e625cce1 TG |
2861 | raw_spin_lock(&ctx->lock); |
2862 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 2863 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
2864 | WRITE_ONCE(ctx->task, next); |
2865 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
2866 | |
2867 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
2868 | ||
63b6da39 PZ |
2869 | /* |
2870 | * RCU_INIT_POINTER here is safe because we've not | |
2871 | * modified the ctx and the above modification of | |
2872 | * ctx->task and ctx->task_ctx_data are immaterial | |
2873 | * since those values are always verified under | |
2874 | * ctx->lock which we're now holding. | |
2875 | */ | |
2876 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
2877 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
2878 | ||
c93f7669 | 2879 | do_switch = 0; |
bfbd3381 | 2880 | |
cdd6c482 | 2881 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 2882 | } |
e625cce1 TG |
2883 | raw_spin_unlock(&next_ctx->lock); |
2884 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 2885 | } |
5a3126d4 | 2886 | unlock: |
c93f7669 | 2887 | rcu_read_unlock(); |
564c2b21 | 2888 | |
c93f7669 | 2889 | if (do_switch) { |
facc4307 | 2890 | raw_spin_lock(&ctx->lock); |
487f05e1 | 2891 | task_ctx_sched_out(cpuctx, ctx, EVENT_ALL); |
facc4307 | 2892 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 2893 | } |
0793a61d TG |
2894 | } |
2895 | ||
e48c1788 PZ |
2896 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
2897 | ||
ba532500 YZ |
2898 | void perf_sched_cb_dec(struct pmu *pmu) |
2899 | { | |
e48c1788 PZ |
2900 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
2901 | ||
ba532500 | 2902 | this_cpu_dec(perf_sched_cb_usages); |
e48c1788 PZ |
2903 | |
2904 | if (!--cpuctx->sched_cb_usage) | |
2905 | list_del(&cpuctx->sched_cb_entry); | |
ba532500 YZ |
2906 | } |
2907 | ||
e48c1788 | 2908 | |
ba532500 YZ |
2909 | void perf_sched_cb_inc(struct pmu *pmu) |
2910 | { | |
e48c1788 PZ |
2911 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
2912 | ||
2913 | if (!cpuctx->sched_cb_usage++) | |
2914 | list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
2915 | ||
ba532500 YZ |
2916 | this_cpu_inc(perf_sched_cb_usages); |
2917 | } | |
2918 | ||
2919 | /* | |
2920 | * This function provides the context switch callback to the lower code | |
2921 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
2922 | * |
2923 | * This callback is relevant even to per-cpu events; for example multi event | |
2924 | * PEBS requires this to provide PID/TID information. This requires we flush | |
2925 | * all queued PEBS records before we context switch to a new task. | |
ba532500 YZ |
2926 | */ |
2927 | static void perf_pmu_sched_task(struct task_struct *prev, | |
2928 | struct task_struct *next, | |
2929 | bool sched_in) | |
2930 | { | |
2931 | struct perf_cpu_context *cpuctx; | |
2932 | struct pmu *pmu; | |
ba532500 YZ |
2933 | |
2934 | if (prev == next) | |
2935 | return; | |
2936 | ||
e48c1788 | 2937 | list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) { |
1fd7e416 | 2938 | pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */ |
ba532500 | 2939 | |
e48c1788 PZ |
2940 | if (WARN_ON_ONCE(!pmu->sched_task)) |
2941 | continue; | |
ba532500 | 2942 | |
e48c1788 PZ |
2943 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
2944 | perf_pmu_disable(pmu); | |
ba532500 | 2945 | |
e48c1788 | 2946 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
ba532500 | 2947 | |
e48c1788 PZ |
2948 | perf_pmu_enable(pmu); |
2949 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
ba532500 | 2950 | } |
ba532500 YZ |
2951 | } |
2952 | ||
45ac1403 AH |
2953 | static void perf_event_switch(struct task_struct *task, |
2954 | struct task_struct *next_prev, bool sched_in); | |
2955 | ||
8dc85d54 PZ |
2956 | #define for_each_task_context_nr(ctxn) \ |
2957 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
2958 | ||
2959 | /* | |
2960 | * Called from scheduler to remove the events of the current task, | |
2961 | * with interrupts disabled. | |
2962 | * | |
2963 | * We stop each event and update the event value in event->count. | |
2964 | * | |
2965 | * This does not protect us against NMI, but disable() | |
2966 | * sets the disabled bit in the control field of event _before_ | |
2967 | * accessing the event control register. If a NMI hits, then it will | |
2968 | * not restart the event. | |
2969 | */ | |
ab0cce56 JO |
2970 | void __perf_event_task_sched_out(struct task_struct *task, |
2971 | struct task_struct *next) | |
8dc85d54 PZ |
2972 | { |
2973 | int ctxn; | |
2974 | ||
ba532500 YZ |
2975 | if (__this_cpu_read(perf_sched_cb_usages)) |
2976 | perf_pmu_sched_task(task, next, false); | |
2977 | ||
45ac1403 AH |
2978 | if (atomic_read(&nr_switch_events)) |
2979 | perf_event_switch(task, next, false); | |
2980 | ||
8dc85d54 PZ |
2981 | for_each_task_context_nr(ctxn) |
2982 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
2983 | |
2984 | /* | |
2985 | * if cgroup events exist on this CPU, then we need | |
2986 | * to check if we have to switch out PMU state. | |
2987 | * cgroup event are system-wide mode only | |
2988 | */ | |
4a32fea9 | 2989 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 2990 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
2991 | } |
2992 | ||
5b0311e1 FW |
2993 | /* |
2994 | * Called with IRQs disabled | |
2995 | */ | |
2996 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
2997 | enum event_type_t event_type) | |
2998 | { | |
2999 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
3000 | } |
3001 | ||
235c7fc7 | 3002 | static void |
5b0311e1 | 3003 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
6e37738a | 3004 | struct perf_cpu_context *cpuctx) |
0793a61d | 3005 | { |
cdd6c482 | 3006 | struct perf_event *event; |
0793a61d | 3007 | |
889ff015 FW |
3008 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
3009 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 3010 | continue; |
5632ab12 | 3011 | if (!event_filter_match(event)) |
3b6f9e5c PM |
3012 | continue; |
3013 | ||
8c9ed8e1 | 3014 | if (group_can_go_on(event, cpuctx, 1)) |
6e37738a | 3015 | group_sched_in(event, cpuctx, ctx); |
3b6f9e5c PM |
3016 | |
3017 | /* | |
3018 | * If this pinned group hasn't been scheduled, | |
3019 | * put it in error state. | |
3020 | */ | |
0d3d73aa PZ |
3021 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
3022 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); | |
3b6f9e5c | 3023 | } |
5b0311e1 FW |
3024 | } |
3025 | ||
3026 | static void | |
3027 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
6e37738a | 3028 | struct perf_cpu_context *cpuctx) |
5b0311e1 FW |
3029 | { |
3030 | struct perf_event *event; | |
3031 | int can_add_hw = 1; | |
3b6f9e5c | 3032 | |
889ff015 FW |
3033 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
3034 | /* Ignore events in OFF or ERROR state */ | |
3035 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 3036 | continue; |
04289bb9 IM |
3037 | /* |
3038 | * Listen to the 'cpu' scheduling filter constraint | |
cdd6c482 | 3039 | * of events: |
04289bb9 | 3040 | */ |
5632ab12 | 3041 | if (!event_filter_match(event)) |
0793a61d TG |
3042 | continue; |
3043 | ||
9ed6060d | 3044 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
6e37738a | 3045 | if (group_sched_in(event, cpuctx, ctx)) |
dd0e6ba2 | 3046 | can_add_hw = 0; |
9ed6060d | 3047 | } |
0793a61d | 3048 | } |
5b0311e1 FW |
3049 | } |
3050 | ||
3051 | static void | |
3052 | ctx_sched_in(struct perf_event_context *ctx, | |
3053 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
3054 | enum event_type_t event_type, |
3055 | struct task_struct *task) | |
5b0311e1 | 3056 | { |
db24d33e | 3057 | int is_active = ctx->is_active; |
c994d613 PZ |
3058 | u64 now; |
3059 | ||
3060 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3061 | |
5b0311e1 | 3062 | if (likely(!ctx->nr_events)) |
facc4307 | 3063 | return; |
5b0311e1 | 3064 | |
3cbaa590 | 3065 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3066 | if (ctx->task) { |
3067 | if (!is_active) | |
3068 | cpuctx->task_ctx = ctx; | |
3069 | else | |
3070 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3071 | } | |
3072 | ||
3cbaa590 PZ |
3073 | is_active ^= ctx->is_active; /* changed bits */ |
3074 | ||
3075 | if (is_active & EVENT_TIME) { | |
3076 | /* start ctx time */ | |
3077 | now = perf_clock(); | |
3078 | ctx->timestamp = now; | |
3079 | perf_cgroup_set_timestamp(task, ctx); | |
3080 | } | |
3081 | ||
5b0311e1 FW |
3082 | /* |
3083 | * First go through the list and put on any pinned groups | |
3084 | * in order to give them the best chance of going on. | |
3085 | */ | |
3cbaa590 | 3086 | if (is_active & EVENT_PINNED) |
6e37738a | 3087 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
3088 | |
3089 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3090 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 3091 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
3092 | } |
3093 | ||
329c0e01 | 3094 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
3095 | enum event_type_t event_type, |
3096 | struct task_struct *task) | |
329c0e01 FW |
3097 | { |
3098 | struct perf_event_context *ctx = &cpuctx->ctx; | |
3099 | ||
e5d1367f | 3100 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
3101 | } |
3102 | ||
e5d1367f SE |
3103 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
3104 | struct task_struct *task) | |
235c7fc7 | 3105 | { |
108b02cf | 3106 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 3107 | |
108b02cf | 3108 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
3109 | if (cpuctx->task_ctx == ctx) |
3110 | return; | |
3111 | ||
facc4307 | 3112 | perf_ctx_lock(cpuctx, ctx); |
fdccc3fb | 3113 | /* |
3114 | * We must check ctx->nr_events while holding ctx->lock, such | |
3115 | * that we serialize against perf_install_in_context(). | |
3116 | */ | |
3117 | if (!ctx->nr_events) | |
3118 | goto unlock; | |
3119 | ||
1b9a644f | 3120 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
3121 | /* |
3122 | * We want to keep the following priority order: | |
3123 | * cpu pinned (that don't need to move), task pinned, | |
3124 | * cpu flexible, task flexible. | |
fe45bafb AS |
3125 | * |
3126 | * However, if task's ctx is not carrying any pinned | |
3127 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3128 | */ |
fe45bafb AS |
3129 | if (!list_empty(&ctx->pinned_groups)) |
3130 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
63e30d3e | 3131 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 | 3132 | perf_pmu_enable(ctx->pmu); |
fdccc3fb | 3133 | |
3134 | unlock: | |
facc4307 | 3135 | perf_ctx_unlock(cpuctx, ctx); |
235c7fc7 IM |
3136 | } |
3137 | ||
8dc85d54 PZ |
3138 | /* |
3139 | * Called from scheduler to add the events of the current task | |
3140 | * with interrupts disabled. | |
3141 | * | |
3142 | * We restore the event value and then enable it. | |
3143 | * | |
3144 | * This does not protect us against NMI, but enable() | |
3145 | * sets the enabled bit in the control field of event _before_ | |
3146 | * accessing the event control register. If a NMI hits, then it will | |
3147 | * keep the event running. | |
3148 | */ | |
ab0cce56 JO |
3149 | void __perf_event_task_sched_in(struct task_struct *prev, |
3150 | struct task_struct *task) | |
8dc85d54 PZ |
3151 | { |
3152 | struct perf_event_context *ctx; | |
3153 | int ctxn; | |
3154 | ||
7e41d177 PZ |
3155 | /* |
3156 | * If cgroup events exist on this CPU, then we need to check if we have | |
3157 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3158 | * | |
3159 | * Since cgroup events are CPU events, we must schedule these in before | |
3160 | * we schedule in the task events. | |
3161 | */ | |
3162 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3163 | perf_cgroup_sched_in(prev, task); | |
3164 | ||
8dc85d54 PZ |
3165 | for_each_task_context_nr(ctxn) { |
3166 | ctx = task->perf_event_ctxp[ctxn]; | |
3167 | if (likely(!ctx)) | |
3168 | continue; | |
3169 | ||
e5d1367f | 3170 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3171 | } |
d010b332 | 3172 | |
45ac1403 AH |
3173 | if (atomic_read(&nr_switch_events)) |
3174 | perf_event_switch(task, prev, true); | |
3175 | ||
ba532500 YZ |
3176 | if (__this_cpu_read(perf_sched_cb_usages)) |
3177 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3178 | } |
3179 | ||
abd50713 PZ |
3180 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3181 | { | |
3182 | u64 frequency = event->attr.sample_freq; | |
3183 | u64 sec = NSEC_PER_SEC; | |
3184 | u64 divisor, dividend; | |
3185 | ||
3186 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3187 | ||
3188 | count_fls = fls64(count); | |
3189 | nsec_fls = fls64(nsec); | |
3190 | frequency_fls = fls64(frequency); | |
3191 | sec_fls = 30; | |
3192 | ||
3193 | /* | |
3194 | * We got @count in @nsec, with a target of sample_freq HZ | |
3195 | * the target period becomes: | |
3196 | * | |
3197 | * @count * 10^9 | |
3198 | * period = ------------------- | |
3199 | * @nsec * sample_freq | |
3200 | * | |
3201 | */ | |
3202 | ||
3203 | /* | |
3204 | * Reduce accuracy by one bit such that @a and @b converge | |
3205 | * to a similar magnitude. | |
3206 | */ | |
fe4b04fa | 3207 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3208 | do { \ |
3209 | if (a##_fls > b##_fls) { \ | |
3210 | a >>= 1; \ | |
3211 | a##_fls--; \ | |
3212 | } else { \ | |
3213 | b >>= 1; \ | |
3214 | b##_fls--; \ | |
3215 | } \ | |
3216 | } while (0) | |
3217 | ||
3218 | /* | |
3219 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3220 | * the other, so that finally we can do a u64/u64 division. | |
3221 | */ | |
3222 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3223 | REDUCE_FLS(nsec, frequency); | |
3224 | REDUCE_FLS(sec, count); | |
3225 | } | |
3226 | ||
3227 | if (count_fls + sec_fls > 64) { | |
3228 | divisor = nsec * frequency; | |
3229 | ||
3230 | while (count_fls + sec_fls > 64) { | |
3231 | REDUCE_FLS(count, sec); | |
3232 | divisor >>= 1; | |
3233 | } | |
3234 | ||
3235 | dividend = count * sec; | |
3236 | } else { | |
3237 | dividend = count * sec; | |
3238 | ||
3239 | while (nsec_fls + frequency_fls > 64) { | |
3240 | REDUCE_FLS(nsec, frequency); | |
3241 | dividend >>= 1; | |
3242 | } | |
3243 | ||
3244 | divisor = nsec * frequency; | |
3245 | } | |
3246 | ||
f6ab91ad PZ |
3247 | if (!divisor) |
3248 | return dividend; | |
3249 | ||
abd50713 PZ |
3250 | return div64_u64(dividend, divisor); |
3251 | } | |
3252 | ||
e050e3f0 SE |
3253 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3254 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3255 | ||
f39d47ff | 3256 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3257 | { |
cdd6c482 | 3258 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3259 | s64 period, sample_period; |
bd2b5b12 PZ |
3260 | s64 delta; |
3261 | ||
abd50713 | 3262 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3263 | |
3264 | delta = (s64)(period - hwc->sample_period); | |
3265 | delta = (delta + 7) / 8; /* low pass filter */ | |
3266 | ||
3267 | sample_period = hwc->sample_period + delta; | |
3268 | ||
3269 | if (!sample_period) | |
3270 | sample_period = 1; | |
3271 | ||
bd2b5b12 | 3272 | hwc->sample_period = sample_period; |
abd50713 | 3273 | |
e7850595 | 3274 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3275 | if (disable) |
3276 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3277 | ||
e7850595 | 3278 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3279 | |
3280 | if (disable) | |
3281 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3282 | } |
bd2b5b12 PZ |
3283 | } |
3284 | ||
e050e3f0 SE |
3285 | /* |
3286 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3287 | * events. At the same time, make sure, having freq events does not change | |
3288 | * the rate of unthrottling as that would introduce bias. | |
3289 | */ | |
3290 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3291 | int needs_unthr) | |
60db5e09 | 3292 | { |
cdd6c482 IM |
3293 | struct perf_event *event; |
3294 | struct hw_perf_event *hwc; | |
e050e3f0 | 3295 | u64 now, period = TICK_NSEC; |
abd50713 | 3296 | s64 delta; |
60db5e09 | 3297 | |
e050e3f0 SE |
3298 | /* |
3299 | * only need to iterate over all events iff: | |
3300 | * - context have events in frequency mode (needs freq adjust) | |
3301 | * - there are events to unthrottle on this cpu | |
3302 | */ | |
3303 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3304 | return; |
3305 | ||
e050e3f0 | 3306 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3307 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3308 | |
03541f8b | 3309 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3310 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3311 | continue; |
3312 | ||
5632ab12 | 3313 | if (!event_filter_match(event)) |
5d27c23d PZ |
3314 | continue; |
3315 | ||
44377277 AS |
3316 | perf_pmu_disable(event->pmu); |
3317 | ||
cdd6c482 | 3318 | hwc = &event->hw; |
6a24ed6c | 3319 | |
ae23bff1 | 3320 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3321 | hwc->interrupts = 0; |
cdd6c482 | 3322 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3323 | event->pmu->start(event, 0); |
a78ac325 PZ |
3324 | } |
3325 | ||
cdd6c482 | 3326 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3327 | goto next; |
60db5e09 | 3328 | |
e050e3f0 SE |
3329 | /* |
3330 | * stop the event and update event->count | |
3331 | */ | |
3332 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3333 | ||
e7850595 | 3334 | now = local64_read(&event->count); |
abd50713 PZ |
3335 | delta = now - hwc->freq_count_stamp; |
3336 | hwc->freq_count_stamp = now; | |
60db5e09 | 3337 | |
e050e3f0 SE |
3338 | /* |
3339 | * restart the event | |
3340 | * reload only if value has changed | |
f39d47ff SE |
3341 | * we have stopped the event so tell that |
3342 | * to perf_adjust_period() to avoid stopping it | |
3343 | * twice. | |
e050e3f0 | 3344 | */ |
abd50713 | 3345 | if (delta > 0) |
f39d47ff | 3346 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3347 | |
3348 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3349 | next: |
3350 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3351 | } |
e050e3f0 | 3352 | |
f39d47ff | 3353 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3354 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3355 | } |
3356 | ||
235c7fc7 | 3357 | /* |
cdd6c482 | 3358 | * Round-robin a context's events: |
235c7fc7 | 3359 | */ |
cdd6c482 | 3360 | static void rotate_ctx(struct perf_event_context *ctx) |
0793a61d | 3361 | { |
dddd3379 TG |
3362 | /* |
3363 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3364 | * disabled by the inheritance code. | |
3365 | */ | |
3366 | if (!ctx->rotate_disable) | |
3367 | list_rotate_left(&ctx->flexible_groups); | |
235c7fc7 IM |
3368 | } |
3369 | ||
9e630205 | 3370 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
235c7fc7 | 3371 | { |
8dc85d54 | 3372 | struct perf_event_context *ctx = NULL; |
2fde4f94 | 3373 | int rotate = 0; |
7fc23a53 | 3374 | |
b5ab4cd5 | 3375 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 PZ |
3376 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
3377 | rotate = 1; | |
3378 | } | |
235c7fc7 | 3379 | |
8dc85d54 | 3380 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3381 | if (ctx && ctx->nr_events) { |
b5ab4cd5 PZ |
3382 | if (ctx->nr_events != ctx->nr_active) |
3383 | rotate = 1; | |
3384 | } | |
9717e6cd | 3385 | |
e050e3f0 | 3386 | if (!rotate) |
0f5a2601 PZ |
3387 | goto done; |
3388 | ||
facc4307 | 3389 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3390 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3391 | |
e050e3f0 SE |
3392 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
3393 | if (ctx) | |
3394 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3395 | |
e050e3f0 SE |
3396 | rotate_ctx(&cpuctx->ctx); |
3397 | if (ctx) | |
3398 | rotate_ctx(ctx); | |
235c7fc7 | 3399 | |
e050e3f0 | 3400 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3401 | |
0f5a2601 PZ |
3402 | perf_pmu_enable(cpuctx->ctx.pmu); |
3403 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
b5ab4cd5 | 3404 | done: |
9e630205 SE |
3405 | |
3406 | return rotate; | |
e9d2b064 PZ |
3407 | } |
3408 | ||
3409 | void perf_event_task_tick(void) | |
3410 | { | |
2fde4f94 MR |
3411 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3412 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3413 | int throttled; |
b5ab4cd5 | 3414 | |
16444645 | 3415 | lockdep_assert_irqs_disabled(); |
e9d2b064 | 3416 | |
e050e3f0 SE |
3417 | __this_cpu_inc(perf_throttled_seq); |
3418 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3419 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3420 | |
2fde4f94 | 3421 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3422 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3423 | } |
3424 | ||
889ff015 FW |
3425 | static int event_enable_on_exec(struct perf_event *event, |
3426 | struct perf_event_context *ctx) | |
3427 | { | |
3428 | if (!event->attr.enable_on_exec) | |
3429 | return 0; | |
3430 | ||
3431 | event->attr.enable_on_exec = 0; | |
3432 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3433 | return 0; | |
3434 | ||
0d3d73aa | 3435 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
889ff015 FW |
3436 | |
3437 | return 1; | |
3438 | } | |
3439 | ||
57e7986e | 3440 | /* |
cdd6c482 | 3441 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3442 | * This expects task == current. |
3443 | */ | |
c1274499 | 3444 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3445 | { |
c1274499 | 3446 | struct perf_event_context *ctx, *clone_ctx = NULL; |
487f05e1 | 3447 | enum event_type_t event_type = 0; |
3e349507 | 3448 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3449 | struct perf_event *event; |
57e7986e PM |
3450 | unsigned long flags; |
3451 | int enabled = 0; | |
3452 | ||
3453 | local_irq_save(flags); | |
c1274499 | 3454 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3455 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3456 | goto out; |
3457 | ||
3e349507 PZ |
3458 | cpuctx = __get_cpu_context(ctx); |
3459 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3460 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
487f05e1 | 3461 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 3462 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
3463 | event_type |= get_event_type(event); |
3464 | } | |
57e7986e PM |
3465 | |
3466 | /* | |
3e349507 | 3467 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3468 | */ |
3e349507 | 3469 | if (enabled) { |
211de6eb | 3470 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 3471 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb PZ |
3472 | } else { |
3473 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
3e349507 PZ |
3474 | } |
3475 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3476 | |
9ed6060d | 3477 | out: |
57e7986e | 3478 | local_irq_restore(flags); |
211de6eb PZ |
3479 | |
3480 | if (clone_ctx) | |
3481 | put_ctx(clone_ctx); | |
57e7986e PM |
3482 | } |
3483 | ||
0492d4c5 PZ |
3484 | struct perf_read_data { |
3485 | struct perf_event *event; | |
3486 | bool group; | |
7d88962e | 3487 | int ret; |
0492d4c5 PZ |
3488 | }; |
3489 | ||
451d24d1 | 3490 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 3491 | { |
d6a2f903 DCC |
3492 | u16 local_pkg, event_pkg; |
3493 | ||
3494 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { | |
451d24d1 PZ |
3495 | int local_cpu = smp_processor_id(); |
3496 | ||
3497 | event_pkg = topology_physical_package_id(event_cpu); | |
3498 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
3499 | |
3500 | if (event_pkg == local_pkg) | |
3501 | return local_cpu; | |
3502 | } | |
3503 | ||
3504 | return event_cpu; | |
3505 | } | |
3506 | ||
0793a61d | 3507 | /* |
cdd6c482 | 3508 | * Cross CPU call to read the hardware event |
0793a61d | 3509 | */ |
cdd6c482 | 3510 | static void __perf_event_read(void *info) |
0793a61d | 3511 | { |
0492d4c5 PZ |
3512 | struct perf_read_data *data = info; |
3513 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3514 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3515 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3516 | struct pmu *pmu = event->pmu; |
621a01ea | 3517 | |
e1ac3614 PM |
3518 | /* |
3519 | * If this is a task context, we need to check whether it is | |
3520 | * the current task context of this cpu. If not it has been | |
3521 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3522 | * event->count would have been updated to a recent sample |
3523 | * when the event was scheduled out. | |
e1ac3614 PM |
3524 | */ |
3525 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3526 | return; | |
3527 | ||
e625cce1 | 3528 | raw_spin_lock(&ctx->lock); |
0c1cbc18 | 3529 | if (ctx->is_active & EVENT_TIME) { |
542e72fc | 3530 | update_context_time(ctx); |
e5d1367f SE |
3531 | update_cgrp_time_from_event(event); |
3532 | } | |
0492d4c5 | 3533 | |
0d3d73aa PZ |
3534 | perf_event_update_time(event); |
3535 | if (data->group) | |
3536 | perf_event_update_sibling_time(event); | |
0c1cbc18 | 3537 | |
4a00c16e SB |
3538 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3539 | goto unlock; | |
0492d4c5 | 3540 | |
4a00c16e SB |
3541 | if (!data->group) { |
3542 | pmu->read(event); | |
3543 | data->ret = 0; | |
0492d4c5 | 3544 | goto unlock; |
4a00c16e SB |
3545 | } |
3546 | ||
3547 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3548 | ||
3549 | pmu->read(event); | |
0492d4c5 PZ |
3550 | |
3551 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
4a00c16e SB |
3552 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3553 | /* | |
3554 | * Use sibling's PMU rather than @event's since | |
3555 | * sibling could be on different (eg: software) PMU. | |
3556 | */ | |
0492d4c5 | 3557 | sub->pmu->read(sub); |
4a00c16e | 3558 | } |
0492d4c5 | 3559 | } |
4a00c16e SB |
3560 | |
3561 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3562 | |
3563 | unlock: | |
e625cce1 | 3564 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3565 | } |
3566 | ||
b5e58793 PZ |
3567 | static inline u64 perf_event_count(struct perf_event *event) |
3568 | { | |
c39a0e2c | 3569 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
3570 | } |
3571 | ||
ffe8690c KX |
3572 | /* |
3573 | * NMI-safe method to read a local event, that is an event that | |
3574 | * is: | |
3575 | * - either for the current task, or for this CPU | |
3576 | * - does not have inherit set, for inherited task events | |
3577 | * will not be local and we cannot read them atomically | |
3578 | * - must not have a pmu::count method | |
3579 | */ | |
7d9285e8 YS |
3580 | int perf_event_read_local(struct perf_event *event, u64 *value, |
3581 | u64 *enabled, u64 *running) | |
ffe8690c KX |
3582 | { |
3583 | unsigned long flags; | |
f91840a3 | 3584 | int ret = 0; |
ffe8690c KX |
3585 | |
3586 | /* | |
3587 | * Disabling interrupts avoids all counter scheduling (context | |
3588 | * switches, timer based rotation and IPIs). | |
3589 | */ | |
3590 | local_irq_save(flags); | |
3591 | ||
ffe8690c KX |
3592 | /* |
3593 | * It must not be an event with inherit set, we cannot read | |
3594 | * all child counters from atomic context. | |
3595 | */ | |
f91840a3 AS |
3596 | if (event->attr.inherit) { |
3597 | ret = -EOPNOTSUPP; | |
3598 | goto out; | |
3599 | } | |
ffe8690c | 3600 | |
f91840a3 AS |
3601 | /* If this is a per-task event, it must be for current */ |
3602 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
3603 | event->hw.target != current) { | |
3604 | ret = -EINVAL; | |
3605 | goto out; | |
3606 | } | |
3607 | ||
3608 | /* If this is a per-CPU event, it must be for this CPU */ | |
3609 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
3610 | event->cpu != smp_processor_id()) { | |
3611 | ret = -EINVAL; | |
3612 | goto out; | |
3613 | } | |
ffe8690c KX |
3614 | |
3615 | /* | |
3616 | * If the event is currently on this CPU, its either a per-task event, | |
3617 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3618 | * oncpu == -1). | |
3619 | */ | |
3620 | if (event->oncpu == smp_processor_id()) | |
3621 | event->pmu->read(event); | |
3622 | ||
f91840a3 | 3623 | *value = local64_read(&event->count); |
0d3d73aa PZ |
3624 | if (enabled || running) { |
3625 | u64 now = event->shadow_ctx_time + perf_clock(); | |
3626 | u64 __enabled, __running; | |
3627 | ||
3628 | __perf_update_times(event, now, &__enabled, &__running); | |
3629 | if (enabled) | |
3630 | *enabled = __enabled; | |
3631 | if (running) | |
3632 | *running = __running; | |
3633 | } | |
f91840a3 | 3634 | out: |
ffe8690c KX |
3635 | local_irq_restore(flags); |
3636 | ||
f91840a3 | 3637 | return ret; |
ffe8690c KX |
3638 | } |
3639 | ||
7d88962e | 3640 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3641 | { |
0c1cbc18 | 3642 | enum perf_event_state state = READ_ONCE(event->state); |
451d24d1 | 3643 | int event_cpu, ret = 0; |
7d88962e | 3644 | |
0793a61d | 3645 | /* |
cdd6c482 IM |
3646 | * If event is enabled and currently active on a CPU, update the |
3647 | * value in the event structure: | |
0793a61d | 3648 | */ |
0c1cbc18 PZ |
3649 | again: |
3650 | if (state == PERF_EVENT_STATE_ACTIVE) { | |
3651 | struct perf_read_data data; | |
3652 | ||
3653 | /* | |
3654 | * Orders the ->state and ->oncpu loads such that if we see | |
3655 | * ACTIVE we must also see the right ->oncpu. | |
3656 | * | |
3657 | * Matches the smp_wmb() from event_sched_in(). | |
3658 | */ | |
3659 | smp_rmb(); | |
d6a2f903 | 3660 | |
451d24d1 PZ |
3661 | event_cpu = READ_ONCE(event->oncpu); |
3662 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
3663 | return 0; | |
3664 | ||
0c1cbc18 PZ |
3665 | data = (struct perf_read_data){ |
3666 | .event = event, | |
3667 | .group = group, | |
3668 | .ret = 0, | |
3669 | }; | |
3670 | ||
451d24d1 PZ |
3671 | preempt_disable(); |
3672 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 3673 | |
58763148 PZ |
3674 | /* |
3675 | * Purposely ignore the smp_call_function_single() return | |
3676 | * value. | |
3677 | * | |
451d24d1 | 3678 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
3679 | * scheduled out and that will have updated the event count. |
3680 | * | |
3681 | * Therefore, either way, we'll have an up-to-date event count | |
3682 | * after this. | |
3683 | */ | |
451d24d1 PZ |
3684 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
3685 | preempt_enable(); | |
58763148 | 3686 | ret = data.ret; |
0c1cbc18 PZ |
3687 | |
3688 | } else if (state == PERF_EVENT_STATE_INACTIVE) { | |
2b8988c9 PZ |
3689 | struct perf_event_context *ctx = event->ctx; |
3690 | unsigned long flags; | |
3691 | ||
e625cce1 | 3692 | raw_spin_lock_irqsave(&ctx->lock, flags); |
0c1cbc18 PZ |
3693 | state = event->state; |
3694 | if (state != PERF_EVENT_STATE_INACTIVE) { | |
3695 | raw_spin_unlock_irqrestore(&ctx->lock, flags); | |
3696 | goto again; | |
3697 | } | |
3698 | ||
c530ccd9 | 3699 | /* |
0c1cbc18 PZ |
3700 | * May read while context is not active (e.g., thread is |
3701 | * blocked), in that case we cannot update context time | |
c530ccd9 | 3702 | */ |
0c1cbc18 | 3703 | if (ctx->is_active & EVENT_TIME) { |
c530ccd9 | 3704 | update_context_time(ctx); |
e5d1367f SE |
3705 | update_cgrp_time_from_event(event); |
3706 | } | |
0c1cbc18 | 3707 | |
0d3d73aa | 3708 | perf_event_update_time(event); |
0492d4c5 | 3709 | if (group) |
0d3d73aa | 3710 | perf_event_update_sibling_time(event); |
e625cce1 | 3711 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3712 | } |
7d88962e SB |
3713 | |
3714 | return ret; | |
0793a61d TG |
3715 | } |
3716 | ||
a63eaf34 | 3717 | /* |
cdd6c482 | 3718 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3719 | */ |
eb184479 | 3720 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 3721 | { |
e625cce1 | 3722 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 3723 | mutex_init(&ctx->mutex); |
2fde4f94 | 3724 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
889ff015 FW |
3725 | INIT_LIST_HEAD(&ctx->pinned_groups); |
3726 | INIT_LIST_HEAD(&ctx->flexible_groups); | |
a63eaf34 PM |
3727 | INIT_LIST_HEAD(&ctx->event_list); |
3728 | atomic_set(&ctx->refcount, 1); | |
eb184479 PZ |
3729 | } |
3730 | ||
3731 | static struct perf_event_context * | |
3732 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
3733 | { | |
3734 | struct perf_event_context *ctx; | |
3735 | ||
3736 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
3737 | if (!ctx) | |
3738 | return NULL; | |
3739 | ||
3740 | __perf_event_init_context(ctx); | |
3741 | if (task) { | |
3742 | ctx->task = task; | |
3743 | get_task_struct(task); | |
0793a61d | 3744 | } |
eb184479 PZ |
3745 | ctx->pmu = pmu; |
3746 | ||
3747 | return ctx; | |
a63eaf34 PM |
3748 | } |
3749 | ||
2ebd4ffb MH |
3750 | static struct task_struct * |
3751 | find_lively_task_by_vpid(pid_t vpid) | |
3752 | { | |
3753 | struct task_struct *task; | |
0793a61d TG |
3754 | |
3755 | rcu_read_lock(); | |
2ebd4ffb | 3756 | if (!vpid) |
0793a61d TG |
3757 | task = current; |
3758 | else | |
2ebd4ffb | 3759 | task = find_task_by_vpid(vpid); |
0793a61d TG |
3760 | if (task) |
3761 | get_task_struct(task); | |
3762 | rcu_read_unlock(); | |
3763 | ||
3764 | if (!task) | |
3765 | return ERR_PTR(-ESRCH); | |
3766 | ||
2ebd4ffb | 3767 | return task; |
2ebd4ffb MH |
3768 | } |
3769 | ||
fe4b04fa PZ |
3770 | /* |
3771 | * Returns a matching context with refcount and pincount. | |
3772 | */ | |
108b02cf | 3773 | static struct perf_event_context * |
4af57ef2 YZ |
3774 | find_get_context(struct pmu *pmu, struct task_struct *task, |
3775 | struct perf_event *event) | |
0793a61d | 3776 | { |
211de6eb | 3777 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 3778 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 3779 | void *task_ctx_data = NULL; |
25346b93 | 3780 | unsigned long flags; |
8dc85d54 | 3781 | int ctxn, err; |
4af57ef2 | 3782 | int cpu = event->cpu; |
0793a61d | 3783 | |
22a4ec72 | 3784 | if (!task) { |
cdd6c482 | 3785 | /* Must be root to operate on a CPU event: */ |
0764771d | 3786 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
3787 | return ERR_PTR(-EACCES); |
3788 | ||
108b02cf | 3789 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 3790 | ctx = &cpuctx->ctx; |
c93f7669 | 3791 | get_ctx(ctx); |
fe4b04fa | 3792 | ++ctx->pin_count; |
0793a61d | 3793 | |
0793a61d TG |
3794 | return ctx; |
3795 | } | |
3796 | ||
8dc85d54 PZ |
3797 | err = -EINVAL; |
3798 | ctxn = pmu->task_ctx_nr; | |
3799 | if (ctxn < 0) | |
3800 | goto errout; | |
3801 | ||
4af57ef2 YZ |
3802 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
3803 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
3804 | if (!task_ctx_data) { | |
3805 | err = -ENOMEM; | |
3806 | goto errout; | |
3807 | } | |
3808 | } | |
3809 | ||
9ed6060d | 3810 | retry: |
8dc85d54 | 3811 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 3812 | if (ctx) { |
211de6eb | 3813 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 3814 | ++ctx->pin_count; |
4af57ef2 YZ |
3815 | |
3816 | if (task_ctx_data && !ctx->task_ctx_data) { | |
3817 | ctx->task_ctx_data = task_ctx_data; | |
3818 | task_ctx_data = NULL; | |
3819 | } | |
e625cce1 | 3820 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
3821 | |
3822 | if (clone_ctx) | |
3823 | put_ctx(clone_ctx); | |
9137fb28 | 3824 | } else { |
eb184479 | 3825 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
3826 | err = -ENOMEM; |
3827 | if (!ctx) | |
3828 | goto errout; | |
eb184479 | 3829 | |
4af57ef2 YZ |
3830 | if (task_ctx_data) { |
3831 | ctx->task_ctx_data = task_ctx_data; | |
3832 | task_ctx_data = NULL; | |
3833 | } | |
3834 | ||
dbe08d82 ON |
3835 | err = 0; |
3836 | mutex_lock(&task->perf_event_mutex); | |
3837 | /* | |
3838 | * If it has already passed perf_event_exit_task(). | |
3839 | * we must see PF_EXITING, it takes this mutex too. | |
3840 | */ | |
3841 | if (task->flags & PF_EXITING) | |
3842 | err = -ESRCH; | |
3843 | else if (task->perf_event_ctxp[ctxn]) | |
3844 | err = -EAGAIN; | |
fe4b04fa | 3845 | else { |
9137fb28 | 3846 | get_ctx(ctx); |
fe4b04fa | 3847 | ++ctx->pin_count; |
dbe08d82 | 3848 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 3849 | } |
dbe08d82 ON |
3850 | mutex_unlock(&task->perf_event_mutex); |
3851 | ||
3852 | if (unlikely(err)) { | |
9137fb28 | 3853 | put_ctx(ctx); |
dbe08d82 ON |
3854 | |
3855 | if (err == -EAGAIN) | |
3856 | goto retry; | |
3857 | goto errout; | |
a63eaf34 PM |
3858 | } |
3859 | } | |
3860 | ||
4af57ef2 | 3861 | kfree(task_ctx_data); |
0793a61d | 3862 | return ctx; |
c93f7669 | 3863 | |
9ed6060d | 3864 | errout: |
4af57ef2 | 3865 | kfree(task_ctx_data); |
c93f7669 | 3866 | return ERR_PTR(err); |
0793a61d TG |
3867 | } |
3868 | ||
6fb2915d | 3869 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 3870 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 3871 | |
cdd6c482 | 3872 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 3873 | { |
cdd6c482 | 3874 | struct perf_event *event; |
592903cd | 3875 | |
cdd6c482 IM |
3876 | event = container_of(head, struct perf_event, rcu_head); |
3877 | if (event->ns) | |
3878 | put_pid_ns(event->ns); | |
6fb2915d | 3879 | perf_event_free_filter(event); |
cdd6c482 | 3880 | kfree(event); |
592903cd PZ |
3881 | } |
3882 | ||
b69cf536 PZ |
3883 | static void ring_buffer_attach(struct perf_event *event, |
3884 | struct ring_buffer *rb); | |
925d519a | 3885 | |
f2fb6bef KL |
3886 | static void detach_sb_event(struct perf_event *event) |
3887 | { | |
3888 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
3889 | ||
3890 | raw_spin_lock(&pel->lock); | |
3891 | list_del_rcu(&event->sb_list); | |
3892 | raw_spin_unlock(&pel->lock); | |
3893 | } | |
3894 | ||
a4f144eb | 3895 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 3896 | { |
a4f144eb DCC |
3897 | struct perf_event_attr *attr = &event->attr; |
3898 | ||
f2fb6bef | 3899 | if (event->parent) |
a4f144eb | 3900 | return false; |
f2fb6bef KL |
3901 | |
3902 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 3903 | return false; |
f2fb6bef | 3904 | |
a4f144eb DCC |
3905 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
3906 | attr->comm || attr->comm_exec || | |
3907 | attr->task || | |
3908 | attr->context_switch) | |
3909 | return true; | |
3910 | return false; | |
3911 | } | |
3912 | ||
3913 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
3914 | { | |
3915 | if (is_sb_event(event)) | |
3916 | detach_sb_event(event); | |
f2fb6bef KL |
3917 | } |
3918 | ||
4beb31f3 | 3919 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 3920 | { |
4beb31f3 FW |
3921 | if (event->parent) |
3922 | return; | |
3923 | ||
4beb31f3 FW |
3924 | if (is_cgroup_event(event)) |
3925 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
3926 | } | |
925d519a | 3927 | |
555e0c1e FW |
3928 | #ifdef CONFIG_NO_HZ_FULL |
3929 | static DEFINE_SPINLOCK(nr_freq_lock); | |
3930 | #endif | |
3931 | ||
3932 | static void unaccount_freq_event_nohz(void) | |
3933 | { | |
3934 | #ifdef CONFIG_NO_HZ_FULL | |
3935 | spin_lock(&nr_freq_lock); | |
3936 | if (atomic_dec_and_test(&nr_freq_events)) | |
3937 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
3938 | spin_unlock(&nr_freq_lock); | |
3939 | #endif | |
3940 | } | |
3941 | ||
3942 | static void unaccount_freq_event(void) | |
3943 | { | |
3944 | if (tick_nohz_full_enabled()) | |
3945 | unaccount_freq_event_nohz(); | |
3946 | else | |
3947 | atomic_dec(&nr_freq_events); | |
3948 | } | |
3949 | ||
4beb31f3 FW |
3950 | static void unaccount_event(struct perf_event *event) |
3951 | { | |
25432ae9 PZ |
3952 | bool dec = false; |
3953 | ||
4beb31f3 FW |
3954 | if (event->parent) |
3955 | return; | |
3956 | ||
3957 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 3958 | dec = true; |
4beb31f3 FW |
3959 | if (event->attr.mmap || event->attr.mmap_data) |
3960 | atomic_dec(&nr_mmap_events); | |
3961 | if (event->attr.comm) | |
3962 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
3963 | if (event->attr.namespaces) |
3964 | atomic_dec(&nr_namespaces_events); | |
4beb31f3 FW |
3965 | if (event->attr.task) |
3966 | atomic_dec(&nr_task_events); | |
948b26b6 | 3967 | if (event->attr.freq) |
555e0c1e | 3968 | unaccount_freq_event(); |
45ac1403 | 3969 | if (event->attr.context_switch) { |
25432ae9 | 3970 | dec = true; |
45ac1403 AH |
3971 | atomic_dec(&nr_switch_events); |
3972 | } | |
4beb31f3 | 3973 | if (is_cgroup_event(event)) |
25432ae9 | 3974 | dec = true; |
4beb31f3 | 3975 | if (has_branch_stack(event)) |
25432ae9 PZ |
3976 | dec = true; |
3977 | ||
9107c89e PZ |
3978 | if (dec) { |
3979 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
3980 | schedule_delayed_work(&perf_sched_work, HZ); | |
3981 | } | |
4beb31f3 FW |
3982 | |
3983 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
3984 | |
3985 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 3986 | } |
925d519a | 3987 | |
9107c89e PZ |
3988 | static void perf_sched_delayed(struct work_struct *work) |
3989 | { | |
3990 | mutex_lock(&perf_sched_mutex); | |
3991 | if (atomic_dec_and_test(&perf_sched_count)) | |
3992 | static_branch_disable(&perf_sched_events); | |
3993 | mutex_unlock(&perf_sched_mutex); | |
3994 | } | |
3995 | ||
bed5b25a AS |
3996 | /* |
3997 | * The following implement mutual exclusion of events on "exclusive" pmus | |
3998 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
3999 | * at a time, so we disallow creating events that might conflict, namely: | |
4000 | * | |
4001 | * 1) cpu-wide events in the presence of per-task events, | |
4002 | * 2) per-task events in the presence of cpu-wide events, | |
4003 | * 3) two matching events on the same context. | |
4004 | * | |
4005 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 4006 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
4007 | */ |
4008 | static int exclusive_event_init(struct perf_event *event) | |
4009 | { | |
4010 | struct pmu *pmu = event->pmu; | |
4011 | ||
4012 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4013 | return 0; | |
4014 | ||
4015 | /* | |
4016 | * Prevent co-existence of per-task and cpu-wide events on the | |
4017 | * same exclusive pmu. | |
4018 | * | |
4019 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
4020 | * events on this "exclusive" pmu, positive means there are | |
4021 | * per-task events. | |
4022 | * | |
4023 | * Since this is called in perf_event_alloc() path, event::ctx | |
4024 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
4025 | * to mean "per-task event", because unlike other attach states it | |
4026 | * never gets cleared. | |
4027 | */ | |
4028 | if (event->attach_state & PERF_ATTACH_TASK) { | |
4029 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
4030 | return -EBUSY; | |
4031 | } else { | |
4032 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
4033 | return -EBUSY; | |
4034 | } | |
4035 | ||
4036 | return 0; | |
4037 | } | |
4038 | ||
4039 | static void exclusive_event_destroy(struct perf_event *event) | |
4040 | { | |
4041 | struct pmu *pmu = event->pmu; | |
4042 | ||
4043 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4044 | return; | |
4045 | ||
4046 | /* see comment in exclusive_event_init() */ | |
4047 | if (event->attach_state & PERF_ATTACH_TASK) | |
4048 | atomic_dec(&pmu->exclusive_cnt); | |
4049 | else | |
4050 | atomic_inc(&pmu->exclusive_cnt); | |
4051 | } | |
4052 | ||
4053 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
4054 | { | |
3bf6215a | 4055 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
4056 | (e1->cpu == e2->cpu || |
4057 | e1->cpu == -1 || | |
4058 | e2->cpu == -1)) | |
4059 | return true; | |
4060 | return false; | |
4061 | } | |
4062 | ||
4063 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
4064 | static bool exclusive_event_installable(struct perf_event *event, | |
4065 | struct perf_event_context *ctx) | |
4066 | { | |
4067 | struct perf_event *iter_event; | |
4068 | struct pmu *pmu = event->pmu; | |
4069 | ||
4070 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4071 | return true; | |
4072 | ||
4073 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
4074 | if (exclusive_event_match(iter_event, event)) | |
4075 | return false; | |
4076 | } | |
4077 | ||
4078 | return true; | |
4079 | } | |
4080 | ||
375637bc AS |
4081 | static void perf_addr_filters_splice(struct perf_event *event, |
4082 | struct list_head *head); | |
4083 | ||
683ede43 | 4084 | static void _free_event(struct perf_event *event) |
f1600952 | 4085 | { |
e360adbe | 4086 | irq_work_sync(&event->pending); |
925d519a | 4087 | |
4beb31f3 | 4088 | unaccount_event(event); |
9ee318a7 | 4089 | |
76369139 | 4090 | if (event->rb) { |
9bb5d40c PZ |
4091 | /* |
4092 | * Can happen when we close an event with re-directed output. | |
4093 | * | |
4094 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
4095 | * over us; possibly making our ring_buffer_put() the last. | |
4096 | */ | |
4097 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 4098 | ring_buffer_attach(event, NULL); |
9bb5d40c | 4099 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
4100 | } |
4101 | ||
e5d1367f SE |
4102 | if (is_cgroup_event(event)) |
4103 | perf_detach_cgroup(event); | |
4104 | ||
a0733e69 PZ |
4105 | if (!event->parent) { |
4106 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
4107 | put_callchain_buffers(); | |
4108 | } | |
4109 | ||
4110 | perf_event_free_bpf_prog(event); | |
375637bc AS |
4111 | perf_addr_filters_splice(event, NULL); |
4112 | kfree(event->addr_filters_offs); | |
a0733e69 PZ |
4113 | |
4114 | if (event->destroy) | |
4115 | event->destroy(event); | |
4116 | ||
4117 | if (event->ctx) | |
4118 | put_ctx(event->ctx); | |
4119 | ||
ae162419 PB |
4120 | if (event->hw.target) |
4121 | put_task_struct(event->hw.target); | |
4122 | ||
62a92c8f AS |
4123 | exclusive_event_destroy(event); |
4124 | module_put(event->pmu->module); | |
a0733e69 PZ |
4125 | |
4126 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
4127 | } |
4128 | ||
683ede43 PZ |
4129 | /* |
4130 | * Used to free events which have a known refcount of 1, such as in error paths | |
4131 | * where the event isn't exposed yet and inherited events. | |
4132 | */ | |
4133 | static void free_event(struct perf_event *event) | |
0793a61d | 4134 | { |
683ede43 PZ |
4135 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
4136 | "unexpected event refcount: %ld; ptr=%p\n", | |
4137 | atomic_long_read(&event->refcount), event)) { | |
4138 | /* leak to avoid use-after-free */ | |
4139 | return; | |
4140 | } | |
0793a61d | 4141 | |
683ede43 | 4142 | _free_event(event); |
0793a61d TG |
4143 | } |
4144 | ||
a66a3052 | 4145 | /* |
f8697762 | 4146 | * Remove user event from the owner task. |
a66a3052 | 4147 | */ |
f8697762 | 4148 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 4149 | { |
8882135b | 4150 | struct task_struct *owner; |
fb0459d7 | 4151 | |
8882135b | 4152 | rcu_read_lock(); |
8882135b | 4153 | /* |
f47c02c0 PZ |
4154 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
4155 | * observe !owner it means the list deletion is complete and we can | |
4156 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
4157 | * owner->perf_event_mutex. |
4158 | */ | |
506458ef | 4159 | owner = READ_ONCE(event->owner); |
8882135b PZ |
4160 | if (owner) { |
4161 | /* | |
4162 | * Since delayed_put_task_struct() also drops the last | |
4163 | * task reference we can safely take a new reference | |
4164 | * while holding the rcu_read_lock(). | |
4165 | */ | |
4166 | get_task_struct(owner); | |
4167 | } | |
4168 | rcu_read_unlock(); | |
4169 | ||
4170 | if (owner) { | |
f63a8daa PZ |
4171 | /* |
4172 | * If we're here through perf_event_exit_task() we're already | |
4173 | * holding ctx->mutex which would be an inversion wrt. the | |
4174 | * normal lock order. | |
4175 | * | |
4176 | * However we can safely take this lock because its the child | |
4177 | * ctx->mutex. | |
4178 | */ | |
4179 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
4180 | ||
8882135b PZ |
4181 | /* |
4182 | * We have to re-check the event->owner field, if it is cleared | |
4183 | * we raced with perf_event_exit_task(), acquiring the mutex | |
4184 | * ensured they're done, and we can proceed with freeing the | |
4185 | * event. | |
4186 | */ | |
f47c02c0 | 4187 | if (event->owner) { |
8882135b | 4188 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
4189 | smp_store_release(&event->owner, NULL); |
4190 | } | |
8882135b PZ |
4191 | mutex_unlock(&owner->perf_event_mutex); |
4192 | put_task_struct(owner); | |
4193 | } | |
f8697762 JO |
4194 | } |
4195 | ||
f8697762 JO |
4196 | static void put_event(struct perf_event *event) |
4197 | { | |
f8697762 JO |
4198 | if (!atomic_long_dec_and_test(&event->refcount)) |
4199 | return; | |
4200 | ||
c6e5b732 PZ |
4201 | _free_event(event); |
4202 | } | |
4203 | ||
4204 | /* | |
4205 | * Kill an event dead; while event:refcount will preserve the event | |
4206 | * object, it will not preserve its functionality. Once the last 'user' | |
4207 | * gives up the object, we'll destroy the thing. | |
4208 | */ | |
4209 | int perf_event_release_kernel(struct perf_event *event) | |
4210 | { | |
a4f4bb6d | 4211 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 | 4212 | struct perf_event *child, *tmp; |
82d94856 | 4213 | LIST_HEAD(free_list); |
c6e5b732 | 4214 | |
a4f4bb6d PZ |
4215 | /* |
4216 | * If we got here through err_file: fput(event_file); we will not have | |
4217 | * attached to a context yet. | |
4218 | */ | |
4219 | if (!ctx) { | |
4220 | WARN_ON_ONCE(event->attach_state & | |
4221 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
4222 | goto no_ctx; | |
4223 | } | |
4224 | ||
f8697762 JO |
4225 | if (!is_kernel_event(event)) |
4226 | perf_remove_from_owner(event); | |
8882135b | 4227 | |
5fa7c8ec | 4228 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 4229 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 4230 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 4231 | |
a69b0ca4 | 4232 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 4233 | /* |
d8a8cfc7 | 4234 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 4235 | * anymore. |
683ede43 | 4236 | * |
a69b0ca4 PZ |
4237 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4238 | * also see this, most importantly inherit_event() which will avoid | |
4239 | * placing more children on the list. | |
683ede43 | 4240 | * |
c6e5b732 PZ |
4241 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4242 | * child events. | |
683ede43 | 4243 | */ |
a69b0ca4 PZ |
4244 | event->state = PERF_EVENT_STATE_DEAD; |
4245 | raw_spin_unlock_irq(&ctx->lock); | |
4246 | ||
4247 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4248 | |
c6e5b732 PZ |
4249 | again: |
4250 | mutex_lock(&event->child_mutex); | |
4251 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4252 | |
c6e5b732 PZ |
4253 | /* |
4254 | * Cannot change, child events are not migrated, see the | |
4255 | * comment with perf_event_ctx_lock_nested(). | |
4256 | */ | |
506458ef | 4257 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
4258 | /* |
4259 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4260 | * through hoops. We start by grabbing a reference on the ctx. | |
4261 | * | |
4262 | * Since the event cannot get freed while we hold the | |
4263 | * child_mutex, the context must also exist and have a !0 | |
4264 | * reference count. | |
4265 | */ | |
4266 | get_ctx(ctx); | |
4267 | ||
4268 | /* | |
4269 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4270 | * acquire ctx::mutex without fear of it going away. Then we | |
4271 | * can re-acquire child_mutex. | |
4272 | */ | |
4273 | mutex_unlock(&event->child_mutex); | |
4274 | mutex_lock(&ctx->mutex); | |
4275 | mutex_lock(&event->child_mutex); | |
4276 | ||
4277 | /* | |
4278 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4279 | * state, if child is still the first entry, it didn't get freed | |
4280 | * and we can continue doing so. | |
4281 | */ | |
4282 | tmp = list_first_entry_or_null(&event->child_list, | |
4283 | struct perf_event, child_list); | |
4284 | if (tmp == child) { | |
4285 | perf_remove_from_context(child, DETACH_GROUP); | |
82d94856 | 4286 | list_move(&child->child_list, &free_list); |
c6e5b732 PZ |
4287 | /* |
4288 | * This matches the refcount bump in inherit_event(); | |
4289 | * this can't be the last reference. | |
4290 | */ | |
4291 | put_event(event); | |
4292 | } | |
4293 | ||
4294 | mutex_unlock(&event->child_mutex); | |
4295 | mutex_unlock(&ctx->mutex); | |
4296 | put_ctx(ctx); | |
4297 | goto again; | |
4298 | } | |
4299 | mutex_unlock(&event->child_mutex); | |
4300 | ||
82d94856 PZ |
4301 | list_for_each_entry_safe(child, tmp, &free_list, child_list) { |
4302 | list_del(&child->child_list); | |
4303 | free_event(child); | |
4304 | } | |
4305 | ||
a4f4bb6d PZ |
4306 | no_ctx: |
4307 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
4308 | return 0; |
4309 | } | |
4310 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
4311 | ||
8b10c5e2 PZ |
4312 | /* |
4313 | * Called when the last reference to the file is gone. | |
4314 | */ | |
a6fa941d AV |
4315 | static int perf_release(struct inode *inode, struct file *file) |
4316 | { | |
c6e5b732 | 4317 | perf_event_release_kernel(file->private_data); |
a6fa941d | 4318 | return 0; |
fb0459d7 | 4319 | } |
fb0459d7 | 4320 | |
ca0dd44c | 4321 | static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 4322 | { |
cdd6c482 | 4323 | struct perf_event *child; |
e53c0994 PZ |
4324 | u64 total = 0; |
4325 | ||
59ed446f PZ |
4326 | *enabled = 0; |
4327 | *running = 0; | |
4328 | ||
6f10581a | 4329 | mutex_lock(&event->child_mutex); |
01add3ea | 4330 | |
7d88962e | 4331 | (void)perf_event_read(event, false); |
01add3ea SB |
4332 | total += perf_event_count(event); |
4333 | ||
59ed446f PZ |
4334 | *enabled += event->total_time_enabled + |
4335 | atomic64_read(&event->child_total_time_enabled); | |
4336 | *running += event->total_time_running + | |
4337 | atomic64_read(&event->child_total_time_running); | |
4338 | ||
4339 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4340 | (void)perf_event_read(child, false); |
01add3ea | 4341 | total += perf_event_count(child); |
59ed446f PZ |
4342 | *enabled += child->total_time_enabled; |
4343 | *running += child->total_time_running; | |
4344 | } | |
6f10581a | 4345 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4346 | |
4347 | return total; | |
4348 | } | |
ca0dd44c PZ |
4349 | |
4350 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) | |
4351 | { | |
4352 | struct perf_event_context *ctx; | |
4353 | u64 count; | |
4354 | ||
4355 | ctx = perf_event_ctx_lock(event); | |
4356 | count = __perf_event_read_value(event, enabled, running); | |
4357 | perf_event_ctx_unlock(event, ctx); | |
4358 | ||
4359 | return count; | |
4360 | } | |
fb0459d7 | 4361 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4362 | |
7d88962e | 4363 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4364 | u64 read_format, u64 *values) |
3dab77fb | 4365 | { |
2aeb1883 | 4366 | struct perf_event_context *ctx = leader->ctx; |
fa8c2693 | 4367 | struct perf_event *sub; |
2aeb1883 | 4368 | unsigned long flags; |
fa8c2693 | 4369 | int n = 1; /* skip @nr */ |
7d88962e | 4370 | int ret; |
f63a8daa | 4371 | |
7d88962e SB |
4372 | ret = perf_event_read(leader, true); |
4373 | if (ret) | |
4374 | return ret; | |
abf4868b | 4375 | |
a9cd8194 PZ |
4376 | raw_spin_lock_irqsave(&ctx->lock, flags); |
4377 | ||
fa8c2693 PZ |
4378 | /* |
4379 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4380 | * will be identical to those of the leader, so we only publish one | |
4381 | * set. | |
4382 | */ | |
4383 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4384 | values[n++] += leader->total_time_enabled + | |
4385 | atomic64_read(&leader->child_total_time_enabled); | |
4386 | } | |
3dab77fb | 4387 | |
fa8c2693 PZ |
4388 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4389 | values[n++] += leader->total_time_running + | |
4390 | atomic64_read(&leader->child_total_time_running); | |
4391 | } | |
4392 | ||
4393 | /* | |
4394 | * Write {count,id} tuples for every sibling. | |
4395 | */ | |
4396 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4397 | if (read_format & PERF_FORMAT_ID) |
4398 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4399 | |
fa8c2693 PZ |
4400 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
4401 | values[n++] += perf_event_count(sub); | |
4402 | if (read_format & PERF_FORMAT_ID) | |
4403 | values[n++] = primary_event_id(sub); | |
4404 | } | |
7d88962e | 4405 | |
2aeb1883 | 4406 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
7d88962e | 4407 | return 0; |
fa8c2693 | 4408 | } |
3dab77fb | 4409 | |
fa8c2693 PZ |
4410 | static int perf_read_group(struct perf_event *event, |
4411 | u64 read_format, char __user *buf) | |
4412 | { | |
4413 | struct perf_event *leader = event->group_leader, *child; | |
4414 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4415 | int ret; |
fa8c2693 | 4416 | u64 *values; |
3dab77fb | 4417 | |
fa8c2693 | 4418 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4419 | |
fa8c2693 PZ |
4420 | values = kzalloc(event->read_size, GFP_KERNEL); |
4421 | if (!values) | |
4422 | return -ENOMEM; | |
3dab77fb | 4423 | |
fa8c2693 PZ |
4424 | values[0] = 1 + leader->nr_siblings; |
4425 | ||
4426 | /* | |
4427 | * By locking the child_mutex of the leader we effectively | |
4428 | * lock the child list of all siblings.. XXX explain how. | |
4429 | */ | |
4430 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4431 | |
7d88962e SB |
4432 | ret = __perf_read_group_add(leader, read_format, values); |
4433 | if (ret) | |
4434 | goto unlock; | |
4435 | ||
4436 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4437 | ret = __perf_read_group_add(child, read_format, values); | |
4438 | if (ret) | |
4439 | goto unlock; | |
4440 | } | |
abf4868b | 4441 | |
fa8c2693 | 4442 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4443 | |
7d88962e | 4444 | ret = event->read_size; |
fa8c2693 PZ |
4445 | if (copy_to_user(buf, values, event->read_size)) |
4446 | ret = -EFAULT; | |
7d88962e | 4447 | goto out; |
fa8c2693 | 4448 | |
7d88962e SB |
4449 | unlock: |
4450 | mutex_unlock(&leader->child_mutex); | |
4451 | out: | |
fa8c2693 | 4452 | kfree(values); |
abf4868b | 4453 | return ret; |
3dab77fb PZ |
4454 | } |
4455 | ||
b15f495b | 4456 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4457 | u64 read_format, char __user *buf) |
4458 | { | |
59ed446f | 4459 | u64 enabled, running; |
3dab77fb PZ |
4460 | u64 values[4]; |
4461 | int n = 0; | |
4462 | ||
ca0dd44c | 4463 | values[n++] = __perf_event_read_value(event, &enabled, &running); |
59ed446f PZ |
4464 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
4465 | values[n++] = enabled; | |
4466 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4467 | values[n++] = running; | |
3dab77fb | 4468 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4469 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4470 | |
4471 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4472 | return -EFAULT; | |
4473 | ||
4474 | return n * sizeof(u64); | |
4475 | } | |
4476 | ||
dc633982 JO |
4477 | static bool is_event_hup(struct perf_event *event) |
4478 | { | |
4479 | bool no_children; | |
4480 | ||
a69b0ca4 | 4481 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4482 | return false; |
4483 | ||
4484 | mutex_lock(&event->child_mutex); | |
4485 | no_children = list_empty(&event->child_list); | |
4486 | mutex_unlock(&event->child_mutex); | |
4487 | return no_children; | |
4488 | } | |
4489 | ||
0793a61d | 4490 | /* |
cdd6c482 | 4491 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4492 | */ |
4493 | static ssize_t | |
b15f495b | 4494 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4495 | { |
cdd6c482 | 4496 | u64 read_format = event->attr.read_format; |
3dab77fb | 4497 | int ret; |
0793a61d | 4498 | |
3b6f9e5c | 4499 | /* |
cdd6c482 | 4500 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4501 | * error state (i.e. because it was pinned but it couldn't be |
4502 | * scheduled on to the CPU at some point). | |
4503 | */ | |
cdd6c482 | 4504 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4505 | return 0; |
4506 | ||
c320c7b7 | 4507 | if (count < event->read_size) |
3dab77fb PZ |
4508 | return -ENOSPC; |
4509 | ||
cdd6c482 | 4510 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4511 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4512 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4513 | else |
b15f495b | 4514 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4515 | |
3dab77fb | 4516 | return ret; |
0793a61d TG |
4517 | } |
4518 | ||
0793a61d TG |
4519 | static ssize_t |
4520 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4521 | { | |
cdd6c482 | 4522 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4523 | struct perf_event_context *ctx; |
4524 | int ret; | |
0793a61d | 4525 | |
f63a8daa | 4526 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4527 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4528 | perf_event_ctx_unlock(event, ctx); |
4529 | ||
4530 | return ret; | |
0793a61d TG |
4531 | } |
4532 | ||
4533 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
4534 | { | |
cdd6c482 | 4535 | struct perf_event *event = file->private_data; |
76369139 | 4536 | struct ring_buffer *rb; |
61b67684 | 4537 | unsigned int events = POLLHUP; |
c7138f37 | 4538 | |
e708d7ad | 4539 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4540 | |
dc633982 | 4541 | if (is_event_hup(event)) |
179033b3 | 4542 | return events; |
c7138f37 | 4543 | |
10c6db11 | 4544 | /* |
9bb5d40c PZ |
4545 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4546 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4547 | */ |
4548 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4549 | rb = event->rb; |
4550 | if (rb) | |
76369139 | 4551 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4552 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4553 | return events; |
4554 | } | |
4555 | ||
f63a8daa | 4556 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4557 | { |
7d88962e | 4558 | (void)perf_event_read(event, false); |
e7850595 | 4559 | local64_set(&event->count, 0); |
cdd6c482 | 4560 | perf_event_update_userpage(event); |
3df5edad PZ |
4561 | } |
4562 | ||
c93f7669 | 4563 | /* |
cdd6c482 IM |
4564 | * Holding the top-level event's child_mutex means that any |
4565 | * descendant process that has inherited this event will block | |
8ba289b8 | 4566 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4567 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4568 | */ |
cdd6c482 IM |
4569 | static void perf_event_for_each_child(struct perf_event *event, |
4570 | void (*func)(struct perf_event *)) | |
3df5edad | 4571 | { |
cdd6c482 | 4572 | struct perf_event *child; |
3df5edad | 4573 | |
cdd6c482 | 4574 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4575 | |
cdd6c482 IM |
4576 | mutex_lock(&event->child_mutex); |
4577 | func(event); | |
4578 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4579 | func(child); |
cdd6c482 | 4580 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4581 | } |
4582 | ||
cdd6c482 IM |
4583 | static void perf_event_for_each(struct perf_event *event, |
4584 | void (*func)(struct perf_event *)) | |
3df5edad | 4585 | { |
cdd6c482 IM |
4586 | struct perf_event_context *ctx = event->ctx; |
4587 | struct perf_event *sibling; | |
3df5edad | 4588 | |
f63a8daa PZ |
4589 | lockdep_assert_held(&ctx->mutex); |
4590 | ||
cdd6c482 | 4591 | event = event->group_leader; |
75f937f2 | 4592 | |
cdd6c482 | 4593 | perf_event_for_each_child(event, func); |
cdd6c482 | 4594 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
724b6daa | 4595 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4596 | } |
4597 | ||
fae3fde6 PZ |
4598 | static void __perf_event_period(struct perf_event *event, |
4599 | struct perf_cpu_context *cpuctx, | |
4600 | struct perf_event_context *ctx, | |
4601 | void *info) | |
c7999c6f | 4602 | { |
fae3fde6 | 4603 | u64 value = *((u64 *)info); |
c7999c6f | 4604 | bool active; |
08247e31 | 4605 | |
cdd6c482 | 4606 | if (event->attr.freq) { |
cdd6c482 | 4607 | event->attr.sample_freq = value; |
08247e31 | 4608 | } else { |
cdd6c482 IM |
4609 | event->attr.sample_period = value; |
4610 | event->hw.sample_period = value; | |
08247e31 | 4611 | } |
bad7192b PZ |
4612 | |
4613 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4614 | if (active) { | |
4615 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4616 | /* |
4617 | * We could be throttled; unthrottle now to avoid the tick | |
4618 | * trying to unthrottle while we already re-started the event. | |
4619 | */ | |
4620 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4621 | event->hw.interrupts = 0; | |
4622 | perf_log_throttle(event, 1); | |
4623 | } | |
bad7192b PZ |
4624 | event->pmu->stop(event, PERF_EF_UPDATE); |
4625 | } | |
4626 | ||
4627 | local64_set(&event->hw.period_left, 0); | |
4628 | ||
4629 | if (active) { | |
4630 | event->pmu->start(event, PERF_EF_RELOAD); | |
4631 | perf_pmu_enable(ctx->pmu); | |
4632 | } | |
c7999c6f PZ |
4633 | } |
4634 | ||
4635 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4636 | { | |
c7999c6f PZ |
4637 | u64 value; |
4638 | ||
4639 | if (!is_sampling_event(event)) | |
4640 | return -EINVAL; | |
4641 | ||
4642 | if (copy_from_user(&value, arg, sizeof(value))) | |
4643 | return -EFAULT; | |
4644 | ||
4645 | if (!value) | |
4646 | return -EINVAL; | |
4647 | ||
4648 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4649 | return -EINVAL; | |
4650 | ||
fae3fde6 | 4651 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4652 | |
c7999c6f | 4653 | return 0; |
08247e31 PZ |
4654 | } |
4655 | ||
ac9721f3 PZ |
4656 | static const struct file_operations perf_fops; |
4657 | ||
2903ff01 | 4658 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4659 | { |
2903ff01 AV |
4660 | struct fd f = fdget(fd); |
4661 | if (!f.file) | |
4662 | return -EBADF; | |
ac9721f3 | 4663 | |
2903ff01 AV |
4664 | if (f.file->f_op != &perf_fops) { |
4665 | fdput(f); | |
4666 | return -EBADF; | |
ac9721f3 | 4667 | } |
2903ff01 AV |
4668 | *p = f; |
4669 | return 0; | |
ac9721f3 PZ |
4670 | } |
4671 | ||
4672 | static int perf_event_set_output(struct perf_event *event, | |
4673 | struct perf_event *output_event); | |
6fb2915d | 4674 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4675 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4676 | |
f63a8daa | 4677 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4678 | { |
cdd6c482 | 4679 | void (*func)(struct perf_event *); |
3df5edad | 4680 | u32 flags = arg; |
d859e29f PM |
4681 | |
4682 | switch (cmd) { | |
cdd6c482 | 4683 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4684 | func = _perf_event_enable; |
d859e29f | 4685 | break; |
cdd6c482 | 4686 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4687 | func = _perf_event_disable; |
79f14641 | 4688 | break; |
cdd6c482 | 4689 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4690 | func = _perf_event_reset; |
6de6a7b9 | 4691 | break; |
3df5edad | 4692 | |
cdd6c482 | 4693 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4694 | return _perf_event_refresh(event, arg); |
08247e31 | 4695 | |
cdd6c482 IM |
4696 | case PERF_EVENT_IOC_PERIOD: |
4697 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4698 | |
cf4957f1 JO |
4699 | case PERF_EVENT_IOC_ID: |
4700 | { | |
4701 | u64 id = primary_event_id(event); | |
4702 | ||
4703 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4704 | return -EFAULT; | |
4705 | return 0; | |
4706 | } | |
4707 | ||
cdd6c482 | 4708 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4709 | { |
ac9721f3 | 4710 | int ret; |
ac9721f3 | 4711 | if (arg != -1) { |
2903ff01 AV |
4712 | struct perf_event *output_event; |
4713 | struct fd output; | |
4714 | ret = perf_fget_light(arg, &output); | |
4715 | if (ret) | |
4716 | return ret; | |
4717 | output_event = output.file->private_data; | |
4718 | ret = perf_event_set_output(event, output_event); | |
4719 | fdput(output); | |
4720 | } else { | |
4721 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 4722 | } |
ac9721f3 PZ |
4723 | return ret; |
4724 | } | |
a4be7c27 | 4725 | |
6fb2915d LZ |
4726 | case PERF_EVENT_IOC_SET_FILTER: |
4727 | return perf_event_set_filter(event, (void __user *)arg); | |
4728 | ||
2541517c AS |
4729 | case PERF_EVENT_IOC_SET_BPF: |
4730 | return perf_event_set_bpf_prog(event, arg); | |
4731 | ||
86e7972f WN |
4732 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
4733 | struct ring_buffer *rb; | |
4734 | ||
4735 | rcu_read_lock(); | |
4736 | rb = rcu_dereference(event->rb); | |
4737 | if (!rb || !rb->nr_pages) { | |
4738 | rcu_read_unlock(); | |
4739 | return -EINVAL; | |
4740 | } | |
4741 | rb_toggle_paused(rb, !!arg); | |
4742 | rcu_read_unlock(); | |
4743 | return 0; | |
4744 | } | |
d859e29f | 4745 | default: |
3df5edad | 4746 | return -ENOTTY; |
d859e29f | 4747 | } |
3df5edad PZ |
4748 | |
4749 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 4750 | perf_event_for_each(event, func); |
3df5edad | 4751 | else |
cdd6c482 | 4752 | perf_event_for_each_child(event, func); |
3df5edad PZ |
4753 | |
4754 | return 0; | |
d859e29f PM |
4755 | } |
4756 | ||
f63a8daa PZ |
4757 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4758 | { | |
4759 | struct perf_event *event = file->private_data; | |
4760 | struct perf_event_context *ctx; | |
4761 | long ret; | |
4762 | ||
4763 | ctx = perf_event_ctx_lock(event); | |
4764 | ret = _perf_ioctl(event, cmd, arg); | |
4765 | perf_event_ctx_unlock(event, ctx); | |
4766 | ||
4767 | return ret; | |
4768 | } | |
4769 | ||
b3f20785 PM |
4770 | #ifdef CONFIG_COMPAT |
4771 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
4772 | unsigned long arg) | |
4773 | { | |
4774 | switch (_IOC_NR(cmd)) { | |
4775 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
4776 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
4777 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
4778 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
4779 | cmd &= ~IOCSIZE_MASK; | |
4780 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
4781 | } | |
4782 | break; | |
4783 | } | |
4784 | return perf_ioctl(file, cmd, arg); | |
4785 | } | |
4786 | #else | |
4787 | # define perf_compat_ioctl NULL | |
4788 | #endif | |
4789 | ||
cdd6c482 | 4790 | int perf_event_task_enable(void) |
771d7cde | 4791 | { |
f63a8daa | 4792 | struct perf_event_context *ctx; |
cdd6c482 | 4793 | struct perf_event *event; |
771d7cde | 4794 | |
cdd6c482 | 4795 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4796 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4797 | ctx = perf_event_ctx_lock(event); | |
4798 | perf_event_for_each_child(event, _perf_event_enable); | |
4799 | perf_event_ctx_unlock(event, ctx); | |
4800 | } | |
cdd6c482 | 4801 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4802 | |
4803 | return 0; | |
4804 | } | |
4805 | ||
cdd6c482 | 4806 | int perf_event_task_disable(void) |
771d7cde | 4807 | { |
f63a8daa | 4808 | struct perf_event_context *ctx; |
cdd6c482 | 4809 | struct perf_event *event; |
771d7cde | 4810 | |
cdd6c482 | 4811 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4812 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4813 | ctx = perf_event_ctx_lock(event); | |
4814 | perf_event_for_each_child(event, _perf_event_disable); | |
4815 | perf_event_ctx_unlock(event, ctx); | |
4816 | } | |
cdd6c482 | 4817 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4818 | |
4819 | return 0; | |
4820 | } | |
4821 | ||
cdd6c482 | 4822 | static int perf_event_index(struct perf_event *event) |
194002b2 | 4823 | { |
a4eaf7f1 PZ |
4824 | if (event->hw.state & PERF_HES_STOPPED) |
4825 | return 0; | |
4826 | ||
cdd6c482 | 4827 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
4828 | return 0; |
4829 | ||
35edc2a5 | 4830 | return event->pmu->event_idx(event); |
194002b2 PZ |
4831 | } |
4832 | ||
c4794295 | 4833 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 4834 | u64 *now, |
7f310a5d EM |
4835 | u64 *enabled, |
4836 | u64 *running) | |
c4794295 | 4837 | { |
e3f3541c | 4838 | u64 ctx_time; |
c4794295 | 4839 | |
e3f3541c PZ |
4840 | *now = perf_clock(); |
4841 | ctx_time = event->shadow_ctx_time + *now; | |
0d3d73aa | 4842 | __perf_update_times(event, ctx_time, enabled, running); |
c4794295 EM |
4843 | } |
4844 | ||
fa731587 PZ |
4845 | static void perf_event_init_userpage(struct perf_event *event) |
4846 | { | |
4847 | struct perf_event_mmap_page *userpg; | |
4848 | struct ring_buffer *rb; | |
4849 | ||
4850 | rcu_read_lock(); | |
4851 | rb = rcu_dereference(event->rb); | |
4852 | if (!rb) | |
4853 | goto unlock; | |
4854 | ||
4855 | userpg = rb->user_page; | |
4856 | ||
4857 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
4858 | userpg->cap_bit0_is_deprecated = 1; | |
4859 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
4860 | userpg->data_offset = PAGE_SIZE; |
4861 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
4862 | |
4863 | unlock: | |
4864 | rcu_read_unlock(); | |
4865 | } | |
4866 | ||
c1317ec2 AL |
4867 | void __weak arch_perf_update_userpage( |
4868 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
4869 | { |
4870 | } | |
4871 | ||
38ff667b PZ |
4872 | /* |
4873 | * Callers need to ensure there can be no nesting of this function, otherwise | |
4874 | * the seqlock logic goes bad. We can not serialize this because the arch | |
4875 | * code calls this from NMI context. | |
4876 | */ | |
cdd6c482 | 4877 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 4878 | { |
cdd6c482 | 4879 | struct perf_event_mmap_page *userpg; |
76369139 | 4880 | struct ring_buffer *rb; |
e3f3541c | 4881 | u64 enabled, running, now; |
38ff667b PZ |
4882 | |
4883 | rcu_read_lock(); | |
5ec4c599 PZ |
4884 | rb = rcu_dereference(event->rb); |
4885 | if (!rb) | |
4886 | goto unlock; | |
4887 | ||
0d641208 EM |
4888 | /* |
4889 | * compute total_time_enabled, total_time_running | |
4890 | * based on snapshot values taken when the event | |
4891 | * was last scheduled in. | |
4892 | * | |
4893 | * we cannot simply called update_context_time() | |
4894 | * because of locking issue as we can be called in | |
4895 | * NMI context | |
4896 | */ | |
e3f3541c | 4897 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 4898 | |
76369139 | 4899 | userpg = rb->user_page; |
7b732a75 PZ |
4900 | /* |
4901 | * Disable preemption so as to not let the corresponding user-space | |
4902 | * spin too long if we get preempted. | |
4903 | */ | |
4904 | preempt_disable(); | |
37d81828 | 4905 | ++userpg->lock; |
92f22a38 | 4906 | barrier(); |
cdd6c482 | 4907 | userpg->index = perf_event_index(event); |
b5e58793 | 4908 | userpg->offset = perf_event_count(event); |
365a4038 | 4909 | if (userpg->index) |
e7850595 | 4910 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 4911 | |
0d641208 | 4912 | userpg->time_enabled = enabled + |
cdd6c482 | 4913 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 4914 | |
0d641208 | 4915 | userpg->time_running = running + |
cdd6c482 | 4916 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 4917 | |
c1317ec2 | 4918 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 4919 | |
92f22a38 | 4920 | barrier(); |
37d81828 | 4921 | ++userpg->lock; |
7b732a75 | 4922 | preempt_enable(); |
38ff667b | 4923 | unlock: |
7b732a75 | 4924 | rcu_read_unlock(); |
37d81828 PM |
4925 | } |
4926 | ||
11bac800 | 4927 | static int perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 4928 | { |
11bac800 | 4929 | struct perf_event *event = vmf->vma->vm_file->private_data; |
76369139 | 4930 | struct ring_buffer *rb; |
906010b2 PZ |
4931 | int ret = VM_FAULT_SIGBUS; |
4932 | ||
4933 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
4934 | if (vmf->pgoff == 0) | |
4935 | ret = 0; | |
4936 | return ret; | |
4937 | } | |
4938 | ||
4939 | rcu_read_lock(); | |
76369139 FW |
4940 | rb = rcu_dereference(event->rb); |
4941 | if (!rb) | |
906010b2 PZ |
4942 | goto unlock; |
4943 | ||
4944 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
4945 | goto unlock; | |
4946 | ||
76369139 | 4947 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
4948 | if (!vmf->page) |
4949 | goto unlock; | |
4950 | ||
4951 | get_page(vmf->page); | |
11bac800 | 4952 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
4953 | vmf->page->index = vmf->pgoff; |
4954 | ||
4955 | ret = 0; | |
4956 | unlock: | |
4957 | rcu_read_unlock(); | |
4958 | ||
4959 | return ret; | |
4960 | } | |
4961 | ||
10c6db11 PZ |
4962 | static void ring_buffer_attach(struct perf_event *event, |
4963 | struct ring_buffer *rb) | |
4964 | { | |
b69cf536 | 4965 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
4966 | unsigned long flags; |
4967 | ||
b69cf536 PZ |
4968 | if (event->rb) { |
4969 | /* | |
4970 | * Should be impossible, we set this when removing | |
4971 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
4972 | */ | |
4973 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 4974 | |
b69cf536 | 4975 | old_rb = event->rb; |
b69cf536 PZ |
4976 | spin_lock_irqsave(&old_rb->event_lock, flags); |
4977 | list_del_rcu(&event->rb_entry); | |
4978 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 4979 | |
2f993cf0 ON |
4980 | event->rcu_batches = get_state_synchronize_rcu(); |
4981 | event->rcu_pending = 1; | |
b69cf536 | 4982 | } |
10c6db11 | 4983 | |
b69cf536 | 4984 | if (rb) { |
2f993cf0 ON |
4985 | if (event->rcu_pending) { |
4986 | cond_synchronize_rcu(event->rcu_batches); | |
4987 | event->rcu_pending = 0; | |
4988 | } | |
4989 | ||
b69cf536 PZ |
4990 | spin_lock_irqsave(&rb->event_lock, flags); |
4991 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
4992 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
4993 | } | |
4994 | ||
767ae086 AS |
4995 | /* |
4996 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
4997 | * before swizzling the event::rb pointer; if it's getting | |
4998 | * unmapped, its aux_mmap_count will be 0 and it won't | |
4999 | * restart. See the comment in __perf_pmu_output_stop(). | |
5000 | * | |
5001 | * Data will inevitably be lost when set_output is done in | |
5002 | * mid-air, but then again, whoever does it like this is | |
5003 | * not in for the data anyway. | |
5004 | */ | |
5005 | if (has_aux(event)) | |
5006 | perf_event_stop(event, 0); | |
5007 | ||
b69cf536 PZ |
5008 | rcu_assign_pointer(event->rb, rb); |
5009 | ||
5010 | if (old_rb) { | |
5011 | ring_buffer_put(old_rb); | |
5012 | /* | |
5013 | * Since we detached before setting the new rb, so that we | |
5014 | * could attach the new rb, we could have missed a wakeup. | |
5015 | * Provide it now. | |
5016 | */ | |
5017 | wake_up_all(&event->waitq); | |
5018 | } | |
10c6db11 PZ |
5019 | } |
5020 | ||
5021 | static void ring_buffer_wakeup(struct perf_event *event) | |
5022 | { | |
5023 | struct ring_buffer *rb; | |
5024 | ||
5025 | rcu_read_lock(); | |
5026 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
5027 | if (rb) { |
5028 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
5029 | wake_up_all(&event->waitq); | |
5030 | } | |
10c6db11 PZ |
5031 | rcu_read_unlock(); |
5032 | } | |
5033 | ||
fdc26706 | 5034 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 5035 | { |
76369139 | 5036 | struct ring_buffer *rb; |
7b732a75 | 5037 | |
ac9721f3 | 5038 | rcu_read_lock(); |
76369139 FW |
5039 | rb = rcu_dereference(event->rb); |
5040 | if (rb) { | |
5041 | if (!atomic_inc_not_zero(&rb->refcount)) | |
5042 | rb = NULL; | |
ac9721f3 PZ |
5043 | } |
5044 | rcu_read_unlock(); | |
5045 | ||
76369139 | 5046 | return rb; |
ac9721f3 PZ |
5047 | } |
5048 | ||
fdc26706 | 5049 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 5050 | { |
76369139 | 5051 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 5052 | return; |
7b732a75 | 5053 | |
9bb5d40c | 5054 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 5055 | |
76369139 | 5056 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
5057 | } |
5058 | ||
5059 | static void perf_mmap_open(struct vm_area_struct *vma) | |
5060 | { | |
cdd6c482 | 5061 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5062 | |
cdd6c482 | 5063 | atomic_inc(&event->mmap_count); |
9bb5d40c | 5064 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 5065 | |
45bfb2e5 PZ |
5066 | if (vma->vm_pgoff) |
5067 | atomic_inc(&event->rb->aux_mmap_count); | |
5068 | ||
1e0fb9ec | 5069 | if (event->pmu->event_mapped) |
bfe33492 | 5070 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
5071 | } |
5072 | ||
95ff4ca2 AS |
5073 | static void perf_pmu_output_stop(struct perf_event *event); |
5074 | ||
9bb5d40c PZ |
5075 | /* |
5076 | * A buffer can be mmap()ed multiple times; either directly through the same | |
5077 | * event, or through other events by use of perf_event_set_output(). | |
5078 | * | |
5079 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
5080 | * the buffer here, where we still have a VM context. This means we need | |
5081 | * to detach all events redirecting to us. | |
5082 | */ | |
7b732a75 PZ |
5083 | static void perf_mmap_close(struct vm_area_struct *vma) |
5084 | { | |
cdd6c482 | 5085 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5086 | |
b69cf536 | 5087 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
5088 | struct user_struct *mmap_user = rb->mmap_user; |
5089 | int mmap_locked = rb->mmap_locked; | |
5090 | unsigned long size = perf_data_size(rb); | |
789f90fc | 5091 | |
1e0fb9ec | 5092 | if (event->pmu->event_unmapped) |
bfe33492 | 5093 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 5094 | |
45bfb2e5 PZ |
5095 | /* |
5096 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
5097 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
5098 | * serialize with perf_mmap here. | |
5099 | */ | |
5100 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
5101 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
5102 | /* |
5103 | * Stop all AUX events that are writing to this buffer, | |
5104 | * so that we can free its AUX pages and corresponding PMU | |
5105 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
5106 | * they won't start any more (see perf_aux_output_begin()). | |
5107 | */ | |
5108 | perf_pmu_output_stop(event); | |
5109 | ||
5110 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
5111 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
5112 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
5113 | ||
95ff4ca2 | 5114 | /* this has to be the last one */ |
45bfb2e5 | 5115 | rb_free_aux(rb); |
95ff4ca2 AS |
5116 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
5117 | ||
45bfb2e5 PZ |
5118 | mutex_unlock(&event->mmap_mutex); |
5119 | } | |
5120 | ||
9bb5d40c PZ |
5121 | atomic_dec(&rb->mmap_count); |
5122 | ||
5123 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 5124 | goto out_put; |
9bb5d40c | 5125 | |
b69cf536 | 5126 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
5127 | mutex_unlock(&event->mmap_mutex); |
5128 | ||
5129 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
5130 | if (atomic_read(&rb->mmap_count)) |
5131 | goto out_put; | |
ac9721f3 | 5132 | |
9bb5d40c PZ |
5133 | /* |
5134 | * No other mmap()s, detach from all other events that might redirect | |
5135 | * into the now unreachable buffer. Somewhat complicated by the | |
5136 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
5137 | */ | |
5138 | again: | |
5139 | rcu_read_lock(); | |
5140 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
5141 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
5142 | /* | |
5143 | * This event is en-route to free_event() which will | |
5144 | * detach it and remove it from the list. | |
5145 | */ | |
5146 | continue; | |
5147 | } | |
5148 | rcu_read_unlock(); | |
789f90fc | 5149 | |
9bb5d40c PZ |
5150 | mutex_lock(&event->mmap_mutex); |
5151 | /* | |
5152 | * Check we didn't race with perf_event_set_output() which can | |
5153 | * swizzle the rb from under us while we were waiting to | |
5154 | * acquire mmap_mutex. | |
5155 | * | |
5156 | * If we find a different rb; ignore this event, a next | |
5157 | * iteration will no longer find it on the list. We have to | |
5158 | * still restart the iteration to make sure we're not now | |
5159 | * iterating the wrong list. | |
5160 | */ | |
b69cf536 PZ |
5161 | if (event->rb == rb) |
5162 | ring_buffer_attach(event, NULL); | |
5163 | ||
cdd6c482 | 5164 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 5165 | put_event(event); |
ac9721f3 | 5166 | |
9bb5d40c PZ |
5167 | /* |
5168 | * Restart the iteration; either we're on the wrong list or | |
5169 | * destroyed its integrity by doing a deletion. | |
5170 | */ | |
5171 | goto again; | |
7b732a75 | 5172 | } |
9bb5d40c PZ |
5173 | rcu_read_unlock(); |
5174 | ||
5175 | /* | |
5176 | * It could be there's still a few 0-ref events on the list; they'll | |
5177 | * get cleaned up by free_event() -- they'll also still have their | |
5178 | * ref on the rb and will free it whenever they are done with it. | |
5179 | * | |
5180 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
5181 | * undo the VM accounting. | |
5182 | */ | |
5183 | ||
5184 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
5185 | vma->vm_mm->pinned_vm -= mmap_locked; | |
5186 | free_uid(mmap_user); | |
5187 | ||
b69cf536 | 5188 | out_put: |
9bb5d40c | 5189 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
5190 | } |
5191 | ||
f0f37e2f | 5192 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 5193 | .open = perf_mmap_open, |
45bfb2e5 | 5194 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
5195 | .fault = perf_mmap_fault, |
5196 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
5197 | }; |
5198 | ||
5199 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
5200 | { | |
cdd6c482 | 5201 | struct perf_event *event = file->private_data; |
22a4f650 | 5202 | unsigned long user_locked, user_lock_limit; |
789f90fc | 5203 | struct user_struct *user = current_user(); |
22a4f650 | 5204 | unsigned long locked, lock_limit; |
45bfb2e5 | 5205 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
5206 | unsigned long vma_size; |
5207 | unsigned long nr_pages; | |
45bfb2e5 | 5208 | long user_extra = 0, extra = 0; |
d57e34fd | 5209 | int ret = 0, flags = 0; |
37d81828 | 5210 | |
c7920614 PZ |
5211 | /* |
5212 | * Don't allow mmap() of inherited per-task counters. This would | |
5213 | * create a performance issue due to all children writing to the | |
76369139 | 5214 | * same rb. |
c7920614 PZ |
5215 | */ |
5216 | if (event->cpu == -1 && event->attr.inherit) | |
5217 | return -EINVAL; | |
5218 | ||
43a21ea8 | 5219 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 5220 | return -EINVAL; |
7b732a75 PZ |
5221 | |
5222 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
5223 | |
5224 | if (vma->vm_pgoff == 0) { | |
5225 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
5226 | } else { | |
5227 | /* | |
5228 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
5229 | * mapped, all subsequent mappings should have the same size | |
5230 | * and offset. Must be above the normal perf buffer. | |
5231 | */ | |
5232 | u64 aux_offset, aux_size; | |
5233 | ||
5234 | if (!event->rb) | |
5235 | return -EINVAL; | |
5236 | ||
5237 | nr_pages = vma_size / PAGE_SIZE; | |
5238 | ||
5239 | mutex_lock(&event->mmap_mutex); | |
5240 | ret = -EINVAL; | |
5241 | ||
5242 | rb = event->rb; | |
5243 | if (!rb) | |
5244 | goto aux_unlock; | |
5245 | ||
6aa7de05 MR |
5246 | aux_offset = READ_ONCE(rb->user_page->aux_offset); |
5247 | aux_size = READ_ONCE(rb->user_page->aux_size); | |
45bfb2e5 PZ |
5248 | |
5249 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
5250 | goto aux_unlock; | |
5251 | ||
5252 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
5253 | goto aux_unlock; | |
5254 | ||
5255 | /* already mapped with a different offset */ | |
5256 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
5257 | goto aux_unlock; | |
5258 | ||
5259 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
5260 | goto aux_unlock; | |
5261 | ||
5262 | /* already mapped with a different size */ | |
5263 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
5264 | goto aux_unlock; | |
5265 | ||
5266 | if (!is_power_of_2(nr_pages)) | |
5267 | goto aux_unlock; | |
5268 | ||
5269 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
5270 | goto aux_unlock; | |
5271 | ||
5272 | if (rb_has_aux(rb)) { | |
5273 | atomic_inc(&rb->aux_mmap_count); | |
5274 | ret = 0; | |
5275 | goto unlock; | |
5276 | } | |
5277 | ||
5278 | atomic_set(&rb->aux_mmap_count, 1); | |
5279 | user_extra = nr_pages; | |
5280 | ||
5281 | goto accounting; | |
5282 | } | |
7b732a75 | 5283 | |
7730d865 | 5284 | /* |
76369139 | 5285 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
5286 | * can do bitmasks instead of modulo. |
5287 | */ | |
2ed11312 | 5288 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
5289 | return -EINVAL; |
5290 | ||
7b732a75 | 5291 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
5292 | return -EINVAL; |
5293 | ||
cdd6c482 | 5294 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 5295 | again: |
cdd6c482 | 5296 | mutex_lock(&event->mmap_mutex); |
76369139 | 5297 | if (event->rb) { |
9bb5d40c | 5298 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 5299 | ret = -EINVAL; |
9bb5d40c PZ |
5300 | goto unlock; |
5301 | } | |
5302 | ||
5303 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
5304 | /* | |
5305 | * Raced against perf_mmap_close() through | |
5306 | * perf_event_set_output(). Try again, hope for better | |
5307 | * luck. | |
5308 | */ | |
5309 | mutex_unlock(&event->mmap_mutex); | |
5310 | goto again; | |
5311 | } | |
5312 | ||
ebb3c4c4 PZ |
5313 | goto unlock; |
5314 | } | |
5315 | ||
789f90fc | 5316 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
5317 | |
5318 | accounting: | |
cdd6c482 | 5319 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
5320 | |
5321 | /* | |
5322 | * Increase the limit linearly with more CPUs: | |
5323 | */ | |
5324 | user_lock_limit *= num_online_cpus(); | |
5325 | ||
789f90fc | 5326 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 5327 | |
789f90fc PZ |
5328 | if (user_locked > user_lock_limit) |
5329 | extra = user_locked - user_lock_limit; | |
7b732a75 | 5330 | |
78d7d407 | 5331 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 5332 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 5333 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 5334 | |
459ec28a IM |
5335 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
5336 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
5337 | ret = -EPERM; |
5338 | goto unlock; | |
5339 | } | |
7b732a75 | 5340 | |
45bfb2e5 | 5341 | WARN_ON(!rb && event->rb); |
906010b2 | 5342 | |
d57e34fd | 5343 | if (vma->vm_flags & VM_WRITE) |
76369139 | 5344 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 5345 | |
76369139 | 5346 | if (!rb) { |
45bfb2e5 PZ |
5347 | rb = rb_alloc(nr_pages, |
5348 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
5349 | event->cpu, flags); | |
26cb63ad | 5350 | |
45bfb2e5 PZ |
5351 | if (!rb) { |
5352 | ret = -ENOMEM; | |
5353 | goto unlock; | |
5354 | } | |
43a21ea8 | 5355 | |
45bfb2e5 PZ |
5356 | atomic_set(&rb->mmap_count, 1); |
5357 | rb->mmap_user = get_current_user(); | |
5358 | rb->mmap_locked = extra; | |
26cb63ad | 5359 | |
45bfb2e5 | 5360 | ring_buffer_attach(event, rb); |
ac9721f3 | 5361 | |
45bfb2e5 PZ |
5362 | perf_event_init_userpage(event); |
5363 | perf_event_update_userpage(event); | |
5364 | } else { | |
1a594131 AS |
5365 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5366 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5367 | if (!ret) |
5368 | rb->aux_mmap_locked = extra; | |
5369 | } | |
9a0f05cb | 5370 | |
ebb3c4c4 | 5371 | unlock: |
45bfb2e5 PZ |
5372 | if (!ret) { |
5373 | atomic_long_add(user_extra, &user->locked_vm); | |
5374 | vma->vm_mm->pinned_vm += extra; | |
5375 | ||
ac9721f3 | 5376 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5377 | } else if (rb) { |
5378 | atomic_dec(&rb->mmap_count); | |
5379 | } | |
5380 | aux_unlock: | |
cdd6c482 | 5381 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5382 | |
9bb5d40c PZ |
5383 | /* |
5384 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5385 | * vma. | |
5386 | */ | |
26cb63ad | 5387 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5388 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5389 | |
1e0fb9ec | 5390 | if (event->pmu->event_mapped) |
bfe33492 | 5391 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 5392 | |
7b732a75 | 5393 | return ret; |
37d81828 PM |
5394 | } |
5395 | ||
3c446b3d PZ |
5396 | static int perf_fasync(int fd, struct file *filp, int on) |
5397 | { | |
496ad9aa | 5398 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5399 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5400 | int retval; |
5401 | ||
5955102c | 5402 | inode_lock(inode); |
cdd6c482 | 5403 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5404 | inode_unlock(inode); |
3c446b3d PZ |
5405 | |
5406 | if (retval < 0) | |
5407 | return retval; | |
5408 | ||
5409 | return 0; | |
5410 | } | |
5411 | ||
0793a61d | 5412 | static const struct file_operations perf_fops = { |
3326c1ce | 5413 | .llseek = no_llseek, |
0793a61d TG |
5414 | .release = perf_release, |
5415 | .read = perf_read, | |
5416 | .poll = perf_poll, | |
d859e29f | 5417 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5418 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5419 | .mmap = perf_mmap, |
3c446b3d | 5420 | .fasync = perf_fasync, |
0793a61d TG |
5421 | }; |
5422 | ||
925d519a | 5423 | /* |
cdd6c482 | 5424 | * Perf event wakeup |
925d519a PZ |
5425 | * |
5426 | * If there's data, ensure we set the poll() state and publish everything | |
5427 | * to user-space before waking everybody up. | |
5428 | */ | |
5429 | ||
fed66e2c PZ |
5430 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5431 | { | |
5432 | /* only the parent has fasync state */ | |
5433 | if (event->parent) | |
5434 | event = event->parent; | |
5435 | return &event->fasync; | |
5436 | } | |
5437 | ||
cdd6c482 | 5438 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5439 | { |
10c6db11 | 5440 | ring_buffer_wakeup(event); |
4c9e2542 | 5441 | |
cdd6c482 | 5442 | if (event->pending_kill) { |
fed66e2c | 5443 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5444 | event->pending_kill = 0; |
4c9e2542 | 5445 | } |
925d519a PZ |
5446 | } |
5447 | ||
e360adbe | 5448 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5449 | { |
cdd6c482 IM |
5450 | struct perf_event *event = container_of(entry, |
5451 | struct perf_event, pending); | |
d525211f PZ |
5452 | int rctx; |
5453 | ||
5454 | rctx = perf_swevent_get_recursion_context(); | |
5455 | /* | |
5456 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5457 | * and we won't recurse 'further'. | |
5458 | */ | |
79f14641 | 5459 | |
cdd6c482 IM |
5460 | if (event->pending_disable) { |
5461 | event->pending_disable = 0; | |
fae3fde6 | 5462 | perf_event_disable_local(event); |
79f14641 PZ |
5463 | } |
5464 | ||
cdd6c482 IM |
5465 | if (event->pending_wakeup) { |
5466 | event->pending_wakeup = 0; | |
5467 | perf_event_wakeup(event); | |
79f14641 | 5468 | } |
d525211f PZ |
5469 | |
5470 | if (rctx >= 0) | |
5471 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5472 | } |
5473 | ||
39447b38 ZY |
5474 | /* |
5475 | * We assume there is only KVM supporting the callbacks. | |
5476 | * Later on, we might change it to a list if there is | |
5477 | * another virtualization implementation supporting the callbacks. | |
5478 | */ | |
5479 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5480 | ||
5481 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5482 | { | |
5483 | perf_guest_cbs = cbs; | |
5484 | return 0; | |
5485 | } | |
5486 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5487 | ||
5488 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5489 | { | |
5490 | perf_guest_cbs = NULL; | |
5491 | return 0; | |
5492 | } | |
5493 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5494 | ||
4018994f JO |
5495 | static void |
5496 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5497 | struct pt_regs *regs, u64 mask) | |
5498 | { | |
5499 | int bit; | |
29dd3288 | 5500 | DECLARE_BITMAP(_mask, 64); |
4018994f | 5501 | |
29dd3288 MS |
5502 | bitmap_from_u64(_mask, mask); |
5503 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
5504 | u64 val; |
5505 | ||
5506 | val = perf_reg_value(regs, bit); | |
5507 | perf_output_put(handle, val); | |
5508 | } | |
5509 | } | |
5510 | ||
60e2364e | 5511 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5512 | struct pt_regs *regs, |
5513 | struct pt_regs *regs_user_copy) | |
4018994f | 5514 | { |
88a7c26a AL |
5515 | if (user_mode(regs)) { |
5516 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5517 | regs_user->regs = regs; |
88a7c26a AL |
5518 | } else if (current->mm) { |
5519 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5520 | } else { |
5521 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5522 | regs_user->regs = NULL; | |
4018994f JO |
5523 | } |
5524 | } | |
5525 | ||
60e2364e SE |
5526 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5527 | struct pt_regs *regs) | |
5528 | { | |
5529 | regs_intr->regs = regs; | |
5530 | regs_intr->abi = perf_reg_abi(current); | |
5531 | } | |
5532 | ||
5533 | ||
c5ebcedb JO |
5534 | /* |
5535 | * Get remaining task size from user stack pointer. | |
5536 | * | |
5537 | * It'd be better to take stack vma map and limit this more | |
5538 | * precisly, but there's no way to get it safely under interrupt, | |
5539 | * so using TASK_SIZE as limit. | |
5540 | */ | |
5541 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5542 | { | |
5543 | unsigned long addr = perf_user_stack_pointer(regs); | |
5544 | ||
5545 | if (!addr || addr >= TASK_SIZE) | |
5546 | return 0; | |
5547 | ||
5548 | return TASK_SIZE - addr; | |
5549 | } | |
5550 | ||
5551 | static u16 | |
5552 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5553 | struct pt_regs *regs) | |
5554 | { | |
5555 | u64 task_size; | |
5556 | ||
5557 | /* No regs, no stack pointer, no dump. */ | |
5558 | if (!regs) | |
5559 | return 0; | |
5560 | ||
5561 | /* | |
5562 | * Check if we fit in with the requested stack size into the: | |
5563 | * - TASK_SIZE | |
5564 | * If we don't, we limit the size to the TASK_SIZE. | |
5565 | * | |
5566 | * - remaining sample size | |
5567 | * If we don't, we customize the stack size to | |
5568 | * fit in to the remaining sample size. | |
5569 | */ | |
5570 | ||
5571 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5572 | stack_size = min(stack_size, (u16) task_size); | |
5573 | ||
5574 | /* Current header size plus static size and dynamic size. */ | |
5575 | header_size += 2 * sizeof(u64); | |
5576 | ||
5577 | /* Do we fit in with the current stack dump size? */ | |
5578 | if ((u16) (header_size + stack_size) < header_size) { | |
5579 | /* | |
5580 | * If we overflow the maximum size for the sample, | |
5581 | * we customize the stack dump size to fit in. | |
5582 | */ | |
5583 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5584 | stack_size = round_up(stack_size, sizeof(u64)); | |
5585 | } | |
5586 | ||
5587 | return stack_size; | |
5588 | } | |
5589 | ||
5590 | static void | |
5591 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5592 | struct pt_regs *regs) | |
5593 | { | |
5594 | /* Case of a kernel thread, nothing to dump */ | |
5595 | if (!regs) { | |
5596 | u64 size = 0; | |
5597 | perf_output_put(handle, size); | |
5598 | } else { | |
5599 | unsigned long sp; | |
5600 | unsigned int rem; | |
5601 | u64 dyn_size; | |
5602 | ||
5603 | /* | |
5604 | * We dump: | |
5605 | * static size | |
5606 | * - the size requested by user or the best one we can fit | |
5607 | * in to the sample max size | |
5608 | * data | |
5609 | * - user stack dump data | |
5610 | * dynamic size | |
5611 | * - the actual dumped size | |
5612 | */ | |
5613 | ||
5614 | /* Static size. */ | |
5615 | perf_output_put(handle, dump_size); | |
5616 | ||
5617 | /* Data. */ | |
5618 | sp = perf_user_stack_pointer(regs); | |
5619 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5620 | dyn_size = dump_size - rem; | |
5621 | ||
5622 | perf_output_skip(handle, rem); | |
5623 | ||
5624 | /* Dynamic size. */ | |
5625 | perf_output_put(handle, dyn_size); | |
5626 | } | |
5627 | } | |
5628 | ||
c980d109 ACM |
5629 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5630 | struct perf_sample_data *data, | |
5631 | struct perf_event *event) | |
6844c09d ACM |
5632 | { |
5633 | u64 sample_type = event->attr.sample_type; | |
5634 | ||
5635 | data->type = sample_type; | |
5636 | header->size += event->id_header_size; | |
5637 | ||
5638 | if (sample_type & PERF_SAMPLE_TID) { | |
5639 | /* namespace issues */ | |
5640 | data->tid_entry.pid = perf_event_pid(event, current); | |
5641 | data->tid_entry.tid = perf_event_tid(event, current); | |
5642 | } | |
5643 | ||
5644 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5645 | data->time = perf_event_clock(event); |
6844c09d | 5646 | |
ff3d527c | 5647 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5648 | data->id = primary_event_id(event); |
5649 | ||
5650 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5651 | data->stream_id = event->id; | |
5652 | ||
5653 | if (sample_type & PERF_SAMPLE_CPU) { | |
5654 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5655 | data->cpu_entry.reserved = 0; | |
5656 | } | |
5657 | } | |
5658 | ||
76369139 FW |
5659 | void perf_event_header__init_id(struct perf_event_header *header, |
5660 | struct perf_sample_data *data, | |
5661 | struct perf_event *event) | |
c980d109 ACM |
5662 | { |
5663 | if (event->attr.sample_id_all) | |
5664 | __perf_event_header__init_id(header, data, event); | |
5665 | } | |
5666 | ||
5667 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5668 | struct perf_sample_data *data) | |
5669 | { | |
5670 | u64 sample_type = data->type; | |
5671 | ||
5672 | if (sample_type & PERF_SAMPLE_TID) | |
5673 | perf_output_put(handle, data->tid_entry); | |
5674 | ||
5675 | if (sample_type & PERF_SAMPLE_TIME) | |
5676 | perf_output_put(handle, data->time); | |
5677 | ||
5678 | if (sample_type & PERF_SAMPLE_ID) | |
5679 | perf_output_put(handle, data->id); | |
5680 | ||
5681 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5682 | perf_output_put(handle, data->stream_id); | |
5683 | ||
5684 | if (sample_type & PERF_SAMPLE_CPU) | |
5685 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5686 | |
5687 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5688 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5689 | } |
5690 | ||
76369139 FW |
5691 | void perf_event__output_id_sample(struct perf_event *event, |
5692 | struct perf_output_handle *handle, | |
5693 | struct perf_sample_data *sample) | |
c980d109 ACM |
5694 | { |
5695 | if (event->attr.sample_id_all) | |
5696 | __perf_event__output_id_sample(handle, sample); | |
5697 | } | |
5698 | ||
3dab77fb | 5699 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5700 | struct perf_event *event, |
5701 | u64 enabled, u64 running) | |
3dab77fb | 5702 | { |
cdd6c482 | 5703 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5704 | u64 values[4]; |
5705 | int n = 0; | |
5706 | ||
b5e58793 | 5707 | values[n++] = perf_event_count(event); |
3dab77fb | 5708 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5709 | values[n++] = enabled + |
cdd6c482 | 5710 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5711 | } |
5712 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5713 | values[n++] = running + |
cdd6c482 | 5714 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5715 | } |
5716 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 5717 | values[n++] = primary_event_id(event); |
3dab77fb | 5718 | |
76369139 | 5719 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5720 | } |
5721 | ||
3dab77fb | 5722 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
5723 | struct perf_event *event, |
5724 | u64 enabled, u64 running) | |
3dab77fb | 5725 | { |
cdd6c482 IM |
5726 | struct perf_event *leader = event->group_leader, *sub; |
5727 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
5728 | u64 values[5]; |
5729 | int n = 0; | |
5730 | ||
5731 | values[n++] = 1 + leader->nr_siblings; | |
5732 | ||
5733 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 5734 | values[n++] = enabled; |
3dab77fb PZ |
5735 | |
5736 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 5737 | values[n++] = running; |
3dab77fb | 5738 | |
cdd6c482 | 5739 | if (leader != event) |
3dab77fb PZ |
5740 | leader->pmu->read(leader); |
5741 | ||
b5e58793 | 5742 | values[n++] = perf_event_count(leader); |
3dab77fb | 5743 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5744 | values[n++] = primary_event_id(leader); |
3dab77fb | 5745 | |
76369139 | 5746 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 5747 | |
65abc865 | 5748 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
3dab77fb PZ |
5749 | n = 0; |
5750 | ||
6f5ab001 JO |
5751 | if ((sub != event) && |
5752 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
5753 | sub->pmu->read(sub); |
5754 | ||
b5e58793 | 5755 | values[n++] = perf_event_count(sub); |
3dab77fb | 5756 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5757 | values[n++] = primary_event_id(sub); |
3dab77fb | 5758 | |
76369139 | 5759 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5760 | } |
5761 | } | |
5762 | ||
eed01528 SE |
5763 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
5764 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5765 | ||
ba5213ae PZ |
5766 | /* |
5767 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
5768 | * | |
5769 | * The problem is that its both hard and excessively expensive to iterate the | |
5770 | * child list, not to mention that its impossible to IPI the children running | |
5771 | * on another CPU, from interrupt/NMI context. | |
5772 | */ | |
3dab77fb | 5773 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 5774 | struct perf_event *event) |
3dab77fb | 5775 | { |
e3f3541c | 5776 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
5777 | u64 read_format = event->attr.read_format; |
5778 | ||
5779 | /* | |
5780 | * compute total_time_enabled, total_time_running | |
5781 | * based on snapshot values taken when the event | |
5782 | * was last scheduled in. | |
5783 | * | |
5784 | * we cannot simply called update_context_time() | |
5785 | * because of locking issue as we are called in | |
5786 | * NMI context | |
5787 | */ | |
c4794295 | 5788 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 5789 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 5790 | |
cdd6c482 | 5791 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 5792 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 5793 | else |
eed01528 | 5794 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
5795 | } |
5796 | ||
5622f295 MM |
5797 | void perf_output_sample(struct perf_output_handle *handle, |
5798 | struct perf_event_header *header, | |
5799 | struct perf_sample_data *data, | |
cdd6c482 | 5800 | struct perf_event *event) |
5622f295 MM |
5801 | { |
5802 | u64 sample_type = data->type; | |
5803 | ||
5804 | perf_output_put(handle, *header); | |
5805 | ||
ff3d527c AH |
5806 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
5807 | perf_output_put(handle, data->id); | |
5808 | ||
5622f295 MM |
5809 | if (sample_type & PERF_SAMPLE_IP) |
5810 | perf_output_put(handle, data->ip); | |
5811 | ||
5812 | if (sample_type & PERF_SAMPLE_TID) | |
5813 | perf_output_put(handle, data->tid_entry); | |
5814 | ||
5815 | if (sample_type & PERF_SAMPLE_TIME) | |
5816 | perf_output_put(handle, data->time); | |
5817 | ||
5818 | if (sample_type & PERF_SAMPLE_ADDR) | |
5819 | perf_output_put(handle, data->addr); | |
5820 | ||
5821 | if (sample_type & PERF_SAMPLE_ID) | |
5822 | perf_output_put(handle, data->id); | |
5823 | ||
5824 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5825 | perf_output_put(handle, data->stream_id); | |
5826 | ||
5827 | if (sample_type & PERF_SAMPLE_CPU) | |
5828 | perf_output_put(handle, data->cpu_entry); | |
5829 | ||
5830 | if (sample_type & PERF_SAMPLE_PERIOD) | |
5831 | perf_output_put(handle, data->period); | |
5832 | ||
5833 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 5834 | perf_output_read(handle, event); |
5622f295 MM |
5835 | |
5836 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
5837 | if (data->callchain) { | |
5838 | int size = 1; | |
5839 | ||
5840 | if (data->callchain) | |
5841 | size += data->callchain->nr; | |
5842 | ||
5843 | size *= sizeof(u64); | |
5844 | ||
76369139 | 5845 | __output_copy(handle, data->callchain, size); |
5622f295 MM |
5846 | } else { |
5847 | u64 nr = 0; | |
5848 | perf_output_put(handle, nr); | |
5849 | } | |
5850 | } | |
5851 | ||
5852 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
5853 | struct perf_raw_record *raw = data->raw; |
5854 | ||
5855 | if (raw) { | |
5856 | struct perf_raw_frag *frag = &raw->frag; | |
5857 | ||
5858 | perf_output_put(handle, raw->size); | |
5859 | do { | |
5860 | if (frag->copy) { | |
5861 | __output_custom(handle, frag->copy, | |
5862 | frag->data, frag->size); | |
5863 | } else { | |
5864 | __output_copy(handle, frag->data, | |
5865 | frag->size); | |
5866 | } | |
5867 | if (perf_raw_frag_last(frag)) | |
5868 | break; | |
5869 | frag = frag->next; | |
5870 | } while (1); | |
5871 | if (frag->pad) | |
5872 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
5873 | } else { |
5874 | struct { | |
5875 | u32 size; | |
5876 | u32 data; | |
5877 | } raw = { | |
5878 | .size = sizeof(u32), | |
5879 | .data = 0, | |
5880 | }; | |
5881 | perf_output_put(handle, raw); | |
5882 | } | |
5883 | } | |
a7ac67ea | 5884 | |
bce38cd5 SE |
5885 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
5886 | if (data->br_stack) { | |
5887 | size_t size; | |
5888 | ||
5889 | size = data->br_stack->nr | |
5890 | * sizeof(struct perf_branch_entry); | |
5891 | ||
5892 | perf_output_put(handle, data->br_stack->nr); | |
5893 | perf_output_copy(handle, data->br_stack->entries, size); | |
5894 | } else { | |
5895 | /* | |
5896 | * we always store at least the value of nr | |
5897 | */ | |
5898 | u64 nr = 0; | |
5899 | perf_output_put(handle, nr); | |
5900 | } | |
5901 | } | |
4018994f JO |
5902 | |
5903 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
5904 | u64 abi = data->regs_user.abi; | |
5905 | ||
5906 | /* | |
5907 | * If there are no regs to dump, notice it through | |
5908 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5909 | */ | |
5910 | perf_output_put(handle, abi); | |
5911 | ||
5912 | if (abi) { | |
5913 | u64 mask = event->attr.sample_regs_user; | |
5914 | perf_output_sample_regs(handle, | |
5915 | data->regs_user.regs, | |
5916 | mask); | |
5917 | } | |
5918 | } | |
c5ebcedb | 5919 | |
a5cdd40c | 5920 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
5921 | perf_output_sample_ustack(handle, |
5922 | data->stack_user_size, | |
5923 | data->regs_user.regs); | |
a5cdd40c | 5924 | } |
c3feedf2 AK |
5925 | |
5926 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
5927 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
5928 | |
5929 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
5930 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 5931 | |
fdfbbd07 AK |
5932 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
5933 | perf_output_put(handle, data->txn); | |
5934 | ||
60e2364e SE |
5935 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
5936 | u64 abi = data->regs_intr.abi; | |
5937 | /* | |
5938 | * If there are no regs to dump, notice it through | |
5939 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5940 | */ | |
5941 | perf_output_put(handle, abi); | |
5942 | ||
5943 | if (abi) { | |
5944 | u64 mask = event->attr.sample_regs_intr; | |
5945 | ||
5946 | perf_output_sample_regs(handle, | |
5947 | data->regs_intr.regs, | |
5948 | mask); | |
5949 | } | |
5950 | } | |
5951 | ||
fc7ce9c7 KL |
5952 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
5953 | perf_output_put(handle, data->phys_addr); | |
5954 | ||
a5cdd40c PZ |
5955 | if (!event->attr.watermark) { |
5956 | int wakeup_events = event->attr.wakeup_events; | |
5957 | ||
5958 | if (wakeup_events) { | |
5959 | struct ring_buffer *rb = handle->rb; | |
5960 | int events = local_inc_return(&rb->events); | |
5961 | ||
5962 | if (events >= wakeup_events) { | |
5963 | local_sub(wakeup_events, &rb->events); | |
5964 | local_inc(&rb->wakeup); | |
5965 | } | |
5966 | } | |
5967 | } | |
5622f295 MM |
5968 | } |
5969 | ||
fc7ce9c7 KL |
5970 | static u64 perf_virt_to_phys(u64 virt) |
5971 | { | |
5972 | u64 phys_addr = 0; | |
5973 | struct page *p = NULL; | |
5974 | ||
5975 | if (!virt) | |
5976 | return 0; | |
5977 | ||
5978 | if (virt >= TASK_SIZE) { | |
5979 | /* If it's vmalloc()d memory, leave phys_addr as 0 */ | |
5980 | if (virt_addr_valid((void *)(uintptr_t)virt) && | |
5981 | !(virt >= VMALLOC_START && virt < VMALLOC_END)) | |
5982 | phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); | |
5983 | } else { | |
5984 | /* | |
5985 | * Walking the pages tables for user address. | |
5986 | * Interrupts are disabled, so it prevents any tear down | |
5987 | * of the page tables. | |
5988 | * Try IRQ-safe __get_user_pages_fast first. | |
5989 | * If failed, leave phys_addr as 0. | |
5990 | */ | |
5991 | if ((current->mm != NULL) && | |
5992 | (__get_user_pages_fast(virt, 1, 0, &p) == 1)) | |
5993 | phys_addr = page_to_phys(p) + virt % PAGE_SIZE; | |
5994 | ||
5995 | if (p) | |
5996 | put_page(p); | |
5997 | } | |
5998 | ||
5999 | return phys_addr; | |
6000 | } | |
6001 | ||
5622f295 MM |
6002 | void perf_prepare_sample(struct perf_event_header *header, |
6003 | struct perf_sample_data *data, | |
cdd6c482 | 6004 | struct perf_event *event, |
5622f295 | 6005 | struct pt_regs *regs) |
7b732a75 | 6006 | { |
cdd6c482 | 6007 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 6008 | |
cdd6c482 | 6009 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 6010 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
6011 | |
6012 | header->misc = 0; | |
6013 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 6014 | |
c980d109 | 6015 | __perf_event_header__init_id(header, data, event); |
6844c09d | 6016 | |
c320c7b7 | 6017 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
6018 | data->ip = perf_instruction_pointer(regs); |
6019 | ||
b23f3325 | 6020 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 6021 | int size = 1; |
394ee076 | 6022 | |
e6dab5ff | 6023 | data->callchain = perf_callchain(event, regs); |
5622f295 MM |
6024 | |
6025 | if (data->callchain) | |
6026 | size += data->callchain->nr; | |
6027 | ||
6028 | header->size += size * sizeof(u64); | |
394ee076 PZ |
6029 | } |
6030 | ||
3a43ce68 | 6031 | if (sample_type & PERF_SAMPLE_RAW) { |
7e3f977e DB |
6032 | struct perf_raw_record *raw = data->raw; |
6033 | int size; | |
6034 | ||
6035 | if (raw) { | |
6036 | struct perf_raw_frag *frag = &raw->frag; | |
6037 | u32 sum = 0; | |
6038 | ||
6039 | do { | |
6040 | sum += frag->size; | |
6041 | if (perf_raw_frag_last(frag)) | |
6042 | break; | |
6043 | frag = frag->next; | |
6044 | } while (1); | |
6045 | ||
6046 | size = round_up(sum + sizeof(u32), sizeof(u64)); | |
6047 | raw->size = size - sizeof(u32); | |
6048 | frag->pad = raw->size - sum; | |
6049 | } else { | |
6050 | size = sizeof(u64); | |
6051 | } | |
a044560c | 6052 | |
7e3f977e | 6053 | header->size += size; |
7f453c24 | 6054 | } |
bce38cd5 SE |
6055 | |
6056 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
6057 | int size = sizeof(u64); /* nr */ | |
6058 | if (data->br_stack) { | |
6059 | size += data->br_stack->nr | |
6060 | * sizeof(struct perf_branch_entry); | |
6061 | } | |
6062 | header->size += size; | |
6063 | } | |
4018994f | 6064 | |
2565711f | 6065 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
6066 | perf_sample_regs_user(&data->regs_user, regs, |
6067 | &data->regs_user_copy); | |
2565711f | 6068 | |
4018994f JO |
6069 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
6070 | /* regs dump ABI info */ | |
6071 | int size = sizeof(u64); | |
6072 | ||
4018994f JO |
6073 | if (data->regs_user.regs) { |
6074 | u64 mask = event->attr.sample_regs_user; | |
6075 | size += hweight64(mask) * sizeof(u64); | |
6076 | } | |
6077 | ||
6078 | header->size += size; | |
6079 | } | |
c5ebcedb JO |
6080 | |
6081 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
6082 | /* | |
6083 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
6084 | * processed as the last one or have additional check added | |
6085 | * in case new sample type is added, because we could eat | |
6086 | * up the rest of the sample size. | |
6087 | */ | |
c5ebcedb JO |
6088 | u16 stack_size = event->attr.sample_stack_user; |
6089 | u16 size = sizeof(u64); | |
6090 | ||
c5ebcedb | 6091 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 6092 | data->regs_user.regs); |
c5ebcedb JO |
6093 | |
6094 | /* | |
6095 | * If there is something to dump, add space for the dump | |
6096 | * itself and for the field that tells the dynamic size, | |
6097 | * which is how many have been actually dumped. | |
6098 | */ | |
6099 | if (stack_size) | |
6100 | size += sizeof(u64) + stack_size; | |
6101 | ||
6102 | data->stack_user_size = stack_size; | |
6103 | header->size += size; | |
6104 | } | |
60e2364e SE |
6105 | |
6106 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
6107 | /* regs dump ABI info */ | |
6108 | int size = sizeof(u64); | |
6109 | ||
6110 | perf_sample_regs_intr(&data->regs_intr, regs); | |
6111 | ||
6112 | if (data->regs_intr.regs) { | |
6113 | u64 mask = event->attr.sample_regs_intr; | |
6114 | ||
6115 | size += hweight64(mask) * sizeof(u64); | |
6116 | } | |
6117 | ||
6118 | header->size += size; | |
6119 | } | |
fc7ce9c7 KL |
6120 | |
6121 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) | |
6122 | data->phys_addr = perf_virt_to_phys(data->addr); | |
5622f295 | 6123 | } |
7f453c24 | 6124 | |
9ecda41a WN |
6125 | static void __always_inline |
6126 | __perf_event_output(struct perf_event *event, | |
6127 | struct perf_sample_data *data, | |
6128 | struct pt_regs *regs, | |
6129 | int (*output_begin)(struct perf_output_handle *, | |
6130 | struct perf_event *, | |
6131 | unsigned int)) | |
5622f295 MM |
6132 | { |
6133 | struct perf_output_handle handle; | |
6134 | struct perf_event_header header; | |
689802b2 | 6135 | |
927c7a9e FW |
6136 | /* protect the callchain buffers */ |
6137 | rcu_read_lock(); | |
6138 | ||
cdd6c482 | 6139 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 6140 | |
9ecda41a | 6141 | if (output_begin(&handle, event, header.size)) |
927c7a9e | 6142 | goto exit; |
0322cd6e | 6143 | |
cdd6c482 | 6144 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 6145 | |
8a057d84 | 6146 | perf_output_end(&handle); |
927c7a9e FW |
6147 | |
6148 | exit: | |
6149 | rcu_read_unlock(); | |
0322cd6e PZ |
6150 | } |
6151 | ||
9ecda41a WN |
6152 | void |
6153 | perf_event_output_forward(struct perf_event *event, | |
6154 | struct perf_sample_data *data, | |
6155 | struct pt_regs *regs) | |
6156 | { | |
6157 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
6158 | } | |
6159 | ||
6160 | void | |
6161 | perf_event_output_backward(struct perf_event *event, | |
6162 | struct perf_sample_data *data, | |
6163 | struct pt_regs *regs) | |
6164 | { | |
6165 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
6166 | } | |
6167 | ||
6168 | void | |
6169 | perf_event_output(struct perf_event *event, | |
6170 | struct perf_sample_data *data, | |
6171 | struct pt_regs *regs) | |
6172 | { | |
6173 | __perf_event_output(event, data, regs, perf_output_begin); | |
6174 | } | |
6175 | ||
38b200d6 | 6176 | /* |
cdd6c482 | 6177 | * read event_id |
38b200d6 PZ |
6178 | */ |
6179 | ||
6180 | struct perf_read_event { | |
6181 | struct perf_event_header header; | |
6182 | ||
6183 | u32 pid; | |
6184 | u32 tid; | |
38b200d6 PZ |
6185 | }; |
6186 | ||
6187 | static void | |
cdd6c482 | 6188 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
6189 | struct task_struct *task) |
6190 | { | |
6191 | struct perf_output_handle handle; | |
c980d109 | 6192 | struct perf_sample_data sample; |
dfc65094 | 6193 | struct perf_read_event read_event = { |
38b200d6 | 6194 | .header = { |
cdd6c482 | 6195 | .type = PERF_RECORD_READ, |
38b200d6 | 6196 | .misc = 0, |
c320c7b7 | 6197 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 6198 | }, |
cdd6c482 IM |
6199 | .pid = perf_event_pid(event, task), |
6200 | .tid = perf_event_tid(event, task), | |
38b200d6 | 6201 | }; |
3dab77fb | 6202 | int ret; |
38b200d6 | 6203 | |
c980d109 | 6204 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 6205 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
6206 | if (ret) |
6207 | return; | |
6208 | ||
dfc65094 | 6209 | perf_output_put(&handle, read_event); |
cdd6c482 | 6210 | perf_output_read(&handle, event); |
c980d109 | 6211 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 6212 | |
38b200d6 PZ |
6213 | perf_output_end(&handle); |
6214 | } | |
6215 | ||
aab5b71e | 6216 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
6217 | |
6218 | static void | |
aab5b71e PZ |
6219 | perf_iterate_ctx(struct perf_event_context *ctx, |
6220 | perf_iterate_f output, | |
b73e4fef | 6221 | void *data, bool all) |
52d857a8 JO |
6222 | { |
6223 | struct perf_event *event; | |
6224 | ||
6225 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
6226 | if (!all) { |
6227 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
6228 | continue; | |
6229 | if (!event_filter_match(event)) | |
6230 | continue; | |
6231 | } | |
6232 | ||
67516844 | 6233 | output(event, data); |
52d857a8 JO |
6234 | } |
6235 | } | |
6236 | ||
aab5b71e | 6237 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
6238 | { |
6239 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
6240 | struct perf_event *event; | |
6241 | ||
6242 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
6243 | /* |
6244 | * Skip events that are not fully formed yet; ensure that | |
6245 | * if we observe event->ctx, both event and ctx will be | |
6246 | * complete enough. See perf_install_in_context(). | |
6247 | */ | |
6248 | if (!smp_load_acquire(&event->ctx)) | |
6249 | continue; | |
6250 | ||
f2fb6bef KL |
6251 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
6252 | continue; | |
6253 | if (!event_filter_match(event)) | |
6254 | continue; | |
6255 | output(event, data); | |
6256 | } | |
6257 | } | |
6258 | ||
aab5b71e PZ |
6259 | /* |
6260 | * Iterate all events that need to receive side-band events. | |
6261 | * | |
6262 | * For new callers; ensure that account_pmu_sb_event() includes | |
6263 | * your event, otherwise it might not get delivered. | |
6264 | */ | |
52d857a8 | 6265 | static void |
aab5b71e | 6266 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
6267 | struct perf_event_context *task_ctx) |
6268 | { | |
52d857a8 | 6269 | struct perf_event_context *ctx; |
52d857a8 JO |
6270 | int ctxn; |
6271 | ||
aab5b71e PZ |
6272 | rcu_read_lock(); |
6273 | preempt_disable(); | |
6274 | ||
4e93ad60 | 6275 | /* |
aab5b71e PZ |
6276 | * If we have task_ctx != NULL we only notify the task context itself. |
6277 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
6278 | * context. |
6279 | */ | |
6280 | if (task_ctx) { | |
aab5b71e PZ |
6281 | perf_iterate_ctx(task_ctx, output, data, false); |
6282 | goto done; | |
4e93ad60 JO |
6283 | } |
6284 | ||
aab5b71e | 6285 | perf_iterate_sb_cpu(output, data); |
f2fb6bef KL |
6286 | |
6287 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
6288 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
6289 | if (ctx) | |
aab5b71e | 6290 | perf_iterate_ctx(ctx, output, data, false); |
52d857a8 | 6291 | } |
aab5b71e | 6292 | done: |
f2fb6bef | 6293 | preempt_enable(); |
52d857a8 | 6294 | rcu_read_unlock(); |
95ff4ca2 AS |
6295 | } |
6296 | ||
375637bc AS |
6297 | /* |
6298 | * Clear all file-based filters at exec, they'll have to be | |
6299 | * re-instated when/if these objects are mmapped again. | |
6300 | */ | |
6301 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
6302 | { | |
6303 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6304 | struct perf_addr_filter *filter; | |
6305 | unsigned int restart = 0, count = 0; | |
6306 | unsigned long flags; | |
6307 | ||
6308 | if (!has_addr_filter(event)) | |
6309 | return; | |
6310 | ||
6311 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
6312 | list_for_each_entry(filter, &ifh->list, entry) { | |
6313 | if (filter->inode) { | |
6314 | event->addr_filters_offs[count] = 0; | |
6315 | restart++; | |
6316 | } | |
6317 | ||
6318 | count++; | |
6319 | } | |
6320 | ||
6321 | if (restart) | |
6322 | event->addr_filters_gen++; | |
6323 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
6324 | ||
6325 | if (restart) | |
767ae086 | 6326 | perf_event_stop(event, 1); |
375637bc AS |
6327 | } |
6328 | ||
6329 | void perf_event_exec(void) | |
6330 | { | |
6331 | struct perf_event_context *ctx; | |
6332 | int ctxn; | |
6333 | ||
6334 | rcu_read_lock(); | |
6335 | for_each_task_context_nr(ctxn) { | |
6336 | ctx = current->perf_event_ctxp[ctxn]; | |
6337 | if (!ctx) | |
6338 | continue; | |
6339 | ||
6340 | perf_event_enable_on_exec(ctxn); | |
6341 | ||
aab5b71e | 6342 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
375637bc AS |
6343 | true); |
6344 | } | |
6345 | rcu_read_unlock(); | |
6346 | } | |
6347 | ||
95ff4ca2 AS |
6348 | struct remote_output { |
6349 | struct ring_buffer *rb; | |
6350 | int err; | |
6351 | }; | |
6352 | ||
6353 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
6354 | { | |
6355 | struct perf_event *parent = event->parent; | |
6356 | struct remote_output *ro = data; | |
6357 | struct ring_buffer *rb = ro->rb; | |
375637bc AS |
6358 | struct stop_event_data sd = { |
6359 | .event = event, | |
6360 | }; | |
95ff4ca2 AS |
6361 | |
6362 | if (!has_aux(event)) | |
6363 | return; | |
6364 | ||
6365 | if (!parent) | |
6366 | parent = event; | |
6367 | ||
6368 | /* | |
6369 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
6370 | * ring-buffer, but it will be the child that's actually using it. |
6371 | * | |
6372 | * We are using event::rb to determine if the event should be stopped, | |
6373 | * however this may race with ring_buffer_attach() (through set_output), | |
6374 | * which will make us skip the event that actually needs to be stopped. | |
6375 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
6376 | * its rb pointer. | |
95ff4ca2 AS |
6377 | */ |
6378 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 6379 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
6380 | } |
6381 | ||
6382 | static int __perf_pmu_output_stop(void *info) | |
6383 | { | |
6384 | struct perf_event *event = info; | |
6385 | struct pmu *pmu = event->pmu; | |
8b6a3fe8 | 6386 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95ff4ca2 AS |
6387 | struct remote_output ro = { |
6388 | .rb = event->rb, | |
6389 | }; | |
6390 | ||
6391 | rcu_read_lock(); | |
aab5b71e | 6392 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 6393 | if (cpuctx->task_ctx) |
aab5b71e | 6394 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 6395 | &ro, false); |
95ff4ca2 AS |
6396 | rcu_read_unlock(); |
6397 | ||
6398 | return ro.err; | |
6399 | } | |
6400 | ||
6401 | static void perf_pmu_output_stop(struct perf_event *event) | |
6402 | { | |
6403 | struct perf_event *iter; | |
6404 | int err, cpu; | |
6405 | ||
6406 | restart: | |
6407 | rcu_read_lock(); | |
6408 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
6409 | /* | |
6410 | * For per-CPU events, we need to make sure that neither they | |
6411 | * nor their children are running; for cpu==-1 events it's | |
6412 | * sufficient to stop the event itself if it's active, since | |
6413 | * it can't have children. | |
6414 | */ | |
6415 | cpu = iter->cpu; | |
6416 | if (cpu == -1) | |
6417 | cpu = READ_ONCE(iter->oncpu); | |
6418 | ||
6419 | if (cpu == -1) | |
6420 | continue; | |
6421 | ||
6422 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
6423 | if (err == -EAGAIN) { | |
6424 | rcu_read_unlock(); | |
6425 | goto restart; | |
6426 | } | |
6427 | } | |
6428 | rcu_read_unlock(); | |
52d857a8 JO |
6429 | } |
6430 | ||
60313ebe | 6431 | /* |
9f498cc5 PZ |
6432 | * task tracking -- fork/exit |
6433 | * | |
13d7a241 | 6434 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
6435 | */ |
6436 | ||
9f498cc5 | 6437 | struct perf_task_event { |
3a80b4a3 | 6438 | struct task_struct *task; |
cdd6c482 | 6439 | struct perf_event_context *task_ctx; |
60313ebe PZ |
6440 | |
6441 | struct { | |
6442 | struct perf_event_header header; | |
6443 | ||
6444 | u32 pid; | |
6445 | u32 ppid; | |
9f498cc5 PZ |
6446 | u32 tid; |
6447 | u32 ptid; | |
393b2ad8 | 6448 | u64 time; |
cdd6c482 | 6449 | } event_id; |
60313ebe PZ |
6450 | }; |
6451 | ||
67516844 JO |
6452 | static int perf_event_task_match(struct perf_event *event) |
6453 | { | |
13d7a241 SE |
6454 | return event->attr.comm || event->attr.mmap || |
6455 | event->attr.mmap2 || event->attr.mmap_data || | |
6456 | event->attr.task; | |
67516844 JO |
6457 | } |
6458 | ||
cdd6c482 | 6459 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 6460 | void *data) |
60313ebe | 6461 | { |
52d857a8 | 6462 | struct perf_task_event *task_event = data; |
60313ebe | 6463 | struct perf_output_handle handle; |
c980d109 | 6464 | struct perf_sample_data sample; |
9f498cc5 | 6465 | struct task_struct *task = task_event->task; |
c980d109 | 6466 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 6467 | |
67516844 JO |
6468 | if (!perf_event_task_match(event)) |
6469 | return; | |
6470 | ||
c980d109 | 6471 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 6472 | |
c980d109 | 6473 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 6474 | task_event->event_id.header.size); |
ef60777c | 6475 | if (ret) |
c980d109 | 6476 | goto out; |
60313ebe | 6477 | |
cdd6c482 IM |
6478 | task_event->event_id.pid = perf_event_pid(event, task); |
6479 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 6480 | |
cdd6c482 IM |
6481 | task_event->event_id.tid = perf_event_tid(event, task); |
6482 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 6483 | |
34f43927 PZ |
6484 | task_event->event_id.time = perf_event_clock(event); |
6485 | ||
cdd6c482 | 6486 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 6487 | |
c980d109 ACM |
6488 | perf_event__output_id_sample(event, &handle, &sample); |
6489 | ||
60313ebe | 6490 | perf_output_end(&handle); |
c980d109 ACM |
6491 | out: |
6492 | task_event->event_id.header.size = size; | |
60313ebe PZ |
6493 | } |
6494 | ||
cdd6c482 IM |
6495 | static void perf_event_task(struct task_struct *task, |
6496 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 6497 | int new) |
60313ebe | 6498 | { |
9f498cc5 | 6499 | struct perf_task_event task_event; |
60313ebe | 6500 | |
cdd6c482 IM |
6501 | if (!atomic_read(&nr_comm_events) && |
6502 | !atomic_read(&nr_mmap_events) && | |
6503 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
6504 | return; |
6505 | ||
9f498cc5 | 6506 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
6507 | .task = task, |
6508 | .task_ctx = task_ctx, | |
cdd6c482 | 6509 | .event_id = { |
60313ebe | 6510 | .header = { |
cdd6c482 | 6511 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 6512 | .misc = 0, |
cdd6c482 | 6513 | .size = sizeof(task_event.event_id), |
60313ebe | 6514 | }, |
573402db PZ |
6515 | /* .pid */ |
6516 | /* .ppid */ | |
9f498cc5 PZ |
6517 | /* .tid */ |
6518 | /* .ptid */ | |
34f43927 | 6519 | /* .time */ |
60313ebe PZ |
6520 | }, |
6521 | }; | |
6522 | ||
aab5b71e | 6523 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
6524 | &task_event, |
6525 | task_ctx); | |
9f498cc5 PZ |
6526 | } |
6527 | ||
cdd6c482 | 6528 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 6529 | { |
cdd6c482 | 6530 | perf_event_task(task, NULL, 1); |
e4222673 | 6531 | perf_event_namespaces(task); |
60313ebe PZ |
6532 | } |
6533 | ||
8d1b2d93 PZ |
6534 | /* |
6535 | * comm tracking | |
6536 | */ | |
6537 | ||
6538 | struct perf_comm_event { | |
22a4f650 IM |
6539 | struct task_struct *task; |
6540 | char *comm; | |
8d1b2d93 PZ |
6541 | int comm_size; |
6542 | ||
6543 | struct { | |
6544 | struct perf_event_header header; | |
6545 | ||
6546 | u32 pid; | |
6547 | u32 tid; | |
cdd6c482 | 6548 | } event_id; |
8d1b2d93 PZ |
6549 | }; |
6550 | ||
67516844 JO |
6551 | static int perf_event_comm_match(struct perf_event *event) |
6552 | { | |
6553 | return event->attr.comm; | |
6554 | } | |
6555 | ||
cdd6c482 | 6556 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6557 | void *data) |
8d1b2d93 | 6558 | { |
52d857a8 | 6559 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6560 | struct perf_output_handle handle; |
c980d109 | 6561 | struct perf_sample_data sample; |
cdd6c482 | 6562 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6563 | int ret; |
6564 | ||
67516844 JO |
6565 | if (!perf_event_comm_match(event)) |
6566 | return; | |
6567 | ||
c980d109 ACM |
6568 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6569 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6570 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6571 | |
6572 | if (ret) | |
c980d109 | 6573 | goto out; |
8d1b2d93 | 6574 | |
cdd6c482 IM |
6575 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6576 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6577 | |
cdd6c482 | 6578 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6579 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6580 | comm_event->comm_size); |
c980d109 ACM |
6581 | |
6582 | perf_event__output_id_sample(event, &handle, &sample); | |
6583 | ||
8d1b2d93 | 6584 | perf_output_end(&handle); |
c980d109 ACM |
6585 | out: |
6586 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6587 | } |
6588 | ||
cdd6c482 | 6589 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6590 | { |
413ee3b4 | 6591 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6592 | unsigned int size; |
8d1b2d93 | 6593 | |
413ee3b4 | 6594 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6595 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6596 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6597 | |
6598 | comm_event->comm = comm; | |
6599 | comm_event->comm_size = size; | |
6600 | ||
cdd6c482 | 6601 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6602 | |
aab5b71e | 6603 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
6604 | comm_event, |
6605 | NULL); | |
8d1b2d93 PZ |
6606 | } |
6607 | ||
82b89778 | 6608 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6609 | { |
9ee318a7 PZ |
6610 | struct perf_comm_event comm_event; |
6611 | ||
cdd6c482 | 6612 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6613 | return; |
a63eaf34 | 6614 | |
9ee318a7 | 6615 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6616 | .task = task, |
573402db PZ |
6617 | /* .comm */ |
6618 | /* .comm_size */ | |
cdd6c482 | 6619 | .event_id = { |
573402db | 6620 | .header = { |
cdd6c482 | 6621 | .type = PERF_RECORD_COMM, |
82b89778 | 6622 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6623 | /* .size */ |
6624 | }, | |
6625 | /* .pid */ | |
6626 | /* .tid */ | |
8d1b2d93 PZ |
6627 | }, |
6628 | }; | |
6629 | ||
cdd6c482 | 6630 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6631 | } |
6632 | ||
e4222673 HB |
6633 | /* |
6634 | * namespaces tracking | |
6635 | */ | |
6636 | ||
6637 | struct perf_namespaces_event { | |
6638 | struct task_struct *task; | |
6639 | ||
6640 | struct { | |
6641 | struct perf_event_header header; | |
6642 | ||
6643 | u32 pid; | |
6644 | u32 tid; | |
6645 | u64 nr_namespaces; | |
6646 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
6647 | } event_id; | |
6648 | }; | |
6649 | ||
6650 | static int perf_event_namespaces_match(struct perf_event *event) | |
6651 | { | |
6652 | return event->attr.namespaces; | |
6653 | } | |
6654 | ||
6655 | static void perf_event_namespaces_output(struct perf_event *event, | |
6656 | void *data) | |
6657 | { | |
6658 | struct perf_namespaces_event *namespaces_event = data; | |
6659 | struct perf_output_handle handle; | |
6660 | struct perf_sample_data sample; | |
34900ec5 | 6661 | u16 header_size = namespaces_event->event_id.header.size; |
e4222673 HB |
6662 | int ret; |
6663 | ||
6664 | if (!perf_event_namespaces_match(event)) | |
6665 | return; | |
6666 | ||
6667 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
6668 | &sample, event); | |
6669 | ret = perf_output_begin(&handle, event, | |
6670 | namespaces_event->event_id.header.size); | |
6671 | if (ret) | |
34900ec5 | 6672 | goto out; |
e4222673 HB |
6673 | |
6674 | namespaces_event->event_id.pid = perf_event_pid(event, | |
6675 | namespaces_event->task); | |
6676 | namespaces_event->event_id.tid = perf_event_tid(event, | |
6677 | namespaces_event->task); | |
6678 | ||
6679 | perf_output_put(&handle, namespaces_event->event_id); | |
6680 | ||
6681 | perf_event__output_id_sample(event, &handle, &sample); | |
6682 | ||
6683 | perf_output_end(&handle); | |
34900ec5 JO |
6684 | out: |
6685 | namespaces_event->event_id.header.size = header_size; | |
e4222673 HB |
6686 | } |
6687 | ||
6688 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
6689 | struct task_struct *task, | |
6690 | const struct proc_ns_operations *ns_ops) | |
6691 | { | |
6692 | struct path ns_path; | |
6693 | struct inode *ns_inode; | |
6694 | void *error; | |
6695 | ||
6696 | error = ns_get_path(&ns_path, task, ns_ops); | |
6697 | if (!error) { | |
6698 | ns_inode = ns_path.dentry->d_inode; | |
6699 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
6700 | ns_link_info->ino = ns_inode->i_ino; | |
0e18dd12 | 6701 | path_put(&ns_path); |
e4222673 HB |
6702 | } |
6703 | } | |
6704 | ||
6705 | void perf_event_namespaces(struct task_struct *task) | |
6706 | { | |
6707 | struct perf_namespaces_event namespaces_event; | |
6708 | struct perf_ns_link_info *ns_link_info; | |
6709 | ||
6710 | if (!atomic_read(&nr_namespaces_events)) | |
6711 | return; | |
6712 | ||
6713 | namespaces_event = (struct perf_namespaces_event){ | |
6714 | .task = task, | |
6715 | .event_id = { | |
6716 | .header = { | |
6717 | .type = PERF_RECORD_NAMESPACES, | |
6718 | .misc = 0, | |
6719 | .size = sizeof(namespaces_event.event_id), | |
6720 | }, | |
6721 | /* .pid */ | |
6722 | /* .tid */ | |
6723 | .nr_namespaces = NR_NAMESPACES, | |
6724 | /* .link_info[NR_NAMESPACES] */ | |
6725 | }, | |
6726 | }; | |
6727 | ||
6728 | ns_link_info = namespaces_event.event_id.link_info; | |
6729 | ||
6730 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
6731 | task, &mntns_operations); | |
6732 | ||
6733 | #ifdef CONFIG_USER_NS | |
6734 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
6735 | task, &userns_operations); | |
6736 | #endif | |
6737 | #ifdef CONFIG_NET_NS | |
6738 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
6739 | task, &netns_operations); | |
6740 | #endif | |
6741 | #ifdef CONFIG_UTS_NS | |
6742 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
6743 | task, &utsns_operations); | |
6744 | #endif | |
6745 | #ifdef CONFIG_IPC_NS | |
6746 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
6747 | task, &ipcns_operations); | |
6748 | #endif | |
6749 | #ifdef CONFIG_PID_NS | |
6750 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
6751 | task, &pidns_operations); | |
6752 | #endif | |
6753 | #ifdef CONFIG_CGROUPS | |
6754 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
6755 | task, &cgroupns_operations); | |
6756 | #endif | |
6757 | ||
6758 | perf_iterate_sb(perf_event_namespaces_output, | |
6759 | &namespaces_event, | |
6760 | NULL); | |
6761 | } | |
6762 | ||
0a4a9391 PZ |
6763 | /* |
6764 | * mmap tracking | |
6765 | */ | |
6766 | ||
6767 | struct perf_mmap_event { | |
089dd79d PZ |
6768 | struct vm_area_struct *vma; |
6769 | ||
6770 | const char *file_name; | |
6771 | int file_size; | |
13d7a241 SE |
6772 | int maj, min; |
6773 | u64 ino; | |
6774 | u64 ino_generation; | |
f972eb63 | 6775 | u32 prot, flags; |
0a4a9391 PZ |
6776 | |
6777 | struct { | |
6778 | struct perf_event_header header; | |
6779 | ||
6780 | u32 pid; | |
6781 | u32 tid; | |
6782 | u64 start; | |
6783 | u64 len; | |
6784 | u64 pgoff; | |
cdd6c482 | 6785 | } event_id; |
0a4a9391 PZ |
6786 | }; |
6787 | ||
67516844 JO |
6788 | static int perf_event_mmap_match(struct perf_event *event, |
6789 | void *data) | |
6790 | { | |
6791 | struct perf_mmap_event *mmap_event = data; | |
6792 | struct vm_area_struct *vma = mmap_event->vma; | |
6793 | int executable = vma->vm_flags & VM_EXEC; | |
6794 | ||
6795 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 6796 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
6797 | } |
6798 | ||
cdd6c482 | 6799 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 6800 | void *data) |
0a4a9391 | 6801 | { |
52d857a8 | 6802 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 6803 | struct perf_output_handle handle; |
c980d109 | 6804 | struct perf_sample_data sample; |
cdd6c482 | 6805 | int size = mmap_event->event_id.header.size; |
c980d109 | 6806 | int ret; |
0a4a9391 | 6807 | |
67516844 JO |
6808 | if (!perf_event_mmap_match(event, data)) |
6809 | return; | |
6810 | ||
13d7a241 SE |
6811 | if (event->attr.mmap2) { |
6812 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
6813 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
6814 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
6815 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 6816 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
6817 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
6818 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
6819 | } |
6820 | ||
c980d109 ACM |
6821 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
6822 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6823 | mmap_event->event_id.header.size); |
0a4a9391 | 6824 | if (ret) |
c980d109 | 6825 | goto out; |
0a4a9391 | 6826 | |
cdd6c482 IM |
6827 | mmap_event->event_id.pid = perf_event_pid(event, current); |
6828 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 6829 | |
cdd6c482 | 6830 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
6831 | |
6832 | if (event->attr.mmap2) { | |
6833 | perf_output_put(&handle, mmap_event->maj); | |
6834 | perf_output_put(&handle, mmap_event->min); | |
6835 | perf_output_put(&handle, mmap_event->ino); | |
6836 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
6837 | perf_output_put(&handle, mmap_event->prot); |
6838 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
6839 | } |
6840 | ||
76369139 | 6841 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 6842 | mmap_event->file_size); |
c980d109 ACM |
6843 | |
6844 | perf_event__output_id_sample(event, &handle, &sample); | |
6845 | ||
78d613eb | 6846 | perf_output_end(&handle); |
c980d109 ACM |
6847 | out: |
6848 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
6849 | } |
6850 | ||
cdd6c482 | 6851 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 6852 | { |
089dd79d PZ |
6853 | struct vm_area_struct *vma = mmap_event->vma; |
6854 | struct file *file = vma->vm_file; | |
13d7a241 SE |
6855 | int maj = 0, min = 0; |
6856 | u64 ino = 0, gen = 0; | |
f972eb63 | 6857 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
6858 | unsigned int size; |
6859 | char tmp[16]; | |
6860 | char *buf = NULL; | |
2c42cfbf | 6861 | char *name; |
413ee3b4 | 6862 | |
0b3589be PZ |
6863 | if (vma->vm_flags & VM_READ) |
6864 | prot |= PROT_READ; | |
6865 | if (vma->vm_flags & VM_WRITE) | |
6866 | prot |= PROT_WRITE; | |
6867 | if (vma->vm_flags & VM_EXEC) | |
6868 | prot |= PROT_EXEC; | |
6869 | ||
6870 | if (vma->vm_flags & VM_MAYSHARE) | |
6871 | flags = MAP_SHARED; | |
6872 | else | |
6873 | flags = MAP_PRIVATE; | |
6874 | ||
6875 | if (vma->vm_flags & VM_DENYWRITE) | |
6876 | flags |= MAP_DENYWRITE; | |
6877 | if (vma->vm_flags & VM_MAYEXEC) | |
6878 | flags |= MAP_EXECUTABLE; | |
6879 | if (vma->vm_flags & VM_LOCKED) | |
6880 | flags |= MAP_LOCKED; | |
6881 | if (vma->vm_flags & VM_HUGETLB) | |
6882 | flags |= MAP_HUGETLB; | |
6883 | ||
0a4a9391 | 6884 | if (file) { |
13d7a241 SE |
6885 | struct inode *inode; |
6886 | dev_t dev; | |
3ea2f2b9 | 6887 | |
2c42cfbf | 6888 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 6889 | if (!buf) { |
c7e548b4 ON |
6890 | name = "//enomem"; |
6891 | goto cpy_name; | |
0a4a9391 | 6892 | } |
413ee3b4 | 6893 | /* |
3ea2f2b9 | 6894 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
6895 | * need to add enough zero bytes after the string to handle |
6896 | * the 64bit alignment we do later. | |
6897 | */ | |
9bf39ab2 | 6898 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 6899 | if (IS_ERR(name)) { |
c7e548b4 ON |
6900 | name = "//toolong"; |
6901 | goto cpy_name; | |
0a4a9391 | 6902 | } |
13d7a241 SE |
6903 | inode = file_inode(vma->vm_file); |
6904 | dev = inode->i_sb->s_dev; | |
6905 | ino = inode->i_ino; | |
6906 | gen = inode->i_generation; | |
6907 | maj = MAJOR(dev); | |
6908 | min = MINOR(dev); | |
f972eb63 | 6909 | |
c7e548b4 | 6910 | goto got_name; |
0a4a9391 | 6911 | } else { |
fbe26abe JO |
6912 | if (vma->vm_ops && vma->vm_ops->name) { |
6913 | name = (char *) vma->vm_ops->name(vma); | |
6914 | if (name) | |
6915 | goto cpy_name; | |
6916 | } | |
6917 | ||
2c42cfbf | 6918 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
6919 | if (name) |
6920 | goto cpy_name; | |
089dd79d | 6921 | |
32c5fb7e | 6922 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 6923 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
6924 | name = "[heap]"; |
6925 | goto cpy_name; | |
32c5fb7e ON |
6926 | } |
6927 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 6928 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
6929 | name = "[stack]"; |
6930 | goto cpy_name; | |
089dd79d PZ |
6931 | } |
6932 | ||
c7e548b4 ON |
6933 | name = "//anon"; |
6934 | goto cpy_name; | |
0a4a9391 PZ |
6935 | } |
6936 | ||
c7e548b4 ON |
6937 | cpy_name: |
6938 | strlcpy(tmp, name, sizeof(tmp)); | |
6939 | name = tmp; | |
0a4a9391 | 6940 | got_name: |
2c42cfbf PZ |
6941 | /* |
6942 | * Since our buffer works in 8 byte units we need to align our string | |
6943 | * size to a multiple of 8. However, we must guarantee the tail end is | |
6944 | * zero'd out to avoid leaking random bits to userspace. | |
6945 | */ | |
6946 | size = strlen(name)+1; | |
6947 | while (!IS_ALIGNED(size, sizeof(u64))) | |
6948 | name[size++] = '\0'; | |
0a4a9391 PZ |
6949 | |
6950 | mmap_event->file_name = name; | |
6951 | mmap_event->file_size = size; | |
13d7a241 SE |
6952 | mmap_event->maj = maj; |
6953 | mmap_event->min = min; | |
6954 | mmap_event->ino = ino; | |
6955 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
6956 | mmap_event->prot = prot; |
6957 | mmap_event->flags = flags; | |
0a4a9391 | 6958 | |
2fe85427 SE |
6959 | if (!(vma->vm_flags & VM_EXEC)) |
6960 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
6961 | ||
cdd6c482 | 6962 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 6963 | |
aab5b71e | 6964 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
6965 | mmap_event, |
6966 | NULL); | |
665c2142 | 6967 | |
0a4a9391 PZ |
6968 | kfree(buf); |
6969 | } | |
6970 | ||
375637bc AS |
6971 | /* |
6972 | * Check whether inode and address range match filter criteria. | |
6973 | */ | |
6974 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
6975 | struct file *file, unsigned long offset, | |
6976 | unsigned long size) | |
6977 | { | |
45063097 | 6978 | if (filter->inode != file_inode(file)) |
375637bc AS |
6979 | return false; |
6980 | ||
6981 | if (filter->offset > offset + size) | |
6982 | return false; | |
6983 | ||
6984 | if (filter->offset + filter->size < offset) | |
6985 | return false; | |
6986 | ||
6987 | return true; | |
6988 | } | |
6989 | ||
6990 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) | |
6991 | { | |
6992 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6993 | struct vm_area_struct *vma = data; | |
6994 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; | |
6995 | struct file *file = vma->vm_file; | |
6996 | struct perf_addr_filter *filter; | |
6997 | unsigned int restart = 0, count = 0; | |
6998 | ||
6999 | if (!has_addr_filter(event)) | |
7000 | return; | |
7001 | ||
7002 | if (!file) | |
7003 | return; | |
7004 | ||
7005 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7006 | list_for_each_entry(filter, &ifh->list, entry) { | |
7007 | if (perf_addr_filter_match(filter, file, off, | |
7008 | vma->vm_end - vma->vm_start)) { | |
7009 | event->addr_filters_offs[count] = vma->vm_start; | |
7010 | restart++; | |
7011 | } | |
7012 | ||
7013 | count++; | |
7014 | } | |
7015 | ||
7016 | if (restart) | |
7017 | event->addr_filters_gen++; | |
7018 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7019 | ||
7020 | if (restart) | |
767ae086 | 7021 | perf_event_stop(event, 1); |
375637bc AS |
7022 | } |
7023 | ||
7024 | /* | |
7025 | * Adjust all task's events' filters to the new vma | |
7026 | */ | |
7027 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
7028 | { | |
7029 | struct perf_event_context *ctx; | |
7030 | int ctxn; | |
7031 | ||
12b40a23 MP |
7032 | /* |
7033 | * Data tracing isn't supported yet and as such there is no need | |
7034 | * to keep track of anything that isn't related to executable code: | |
7035 | */ | |
7036 | if (!(vma->vm_flags & VM_EXEC)) | |
7037 | return; | |
7038 | ||
375637bc AS |
7039 | rcu_read_lock(); |
7040 | for_each_task_context_nr(ctxn) { | |
7041 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
7042 | if (!ctx) | |
7043 | continue; | |
7044 | ||
aab5b71e | 7045 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
7046 | } |
7047 | rcu_read_unlock(); | |
7048 | } | |
7049 | ||
3af9e859 | 7050 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 7051 | { |
9ee318a7 PZ |
7052 | struct perf_mmap_event mmap_event; |
7053 | ||
cdd6c482 | 7054 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
7055 | return; |
7056 | ||
7057 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 7058 | .vma = vma, |
573402db PZ |
7059 | /* .file_name */ |
7060 | /* .file_size */ | |
cdd6c482 | 7061 | .event_id = { |
573402db | 7062 | .header = { |
cdd6c482 | 7063 | .type = PERF_RECORD_MMAP, |
39447b38 | 7064 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
7065 | /* .size */ |
7066 | }, | |
7067 | /* .pid */ | |
7068 | /* .tid */ | |
089dd79d PZ |
7069 | .start = vma->vm_start, |
7070 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 7071 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 7072 | }, |
13d7a241 SE |
7073 | /* .maj (attr_mmap2 only) */ |
7074 | /* .min (attr_mmap2 only) */ | |
7075 | /* .ino (attr_mmap2 only) */ | |
7076 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
7077 | /* .prot (attr_mmap2 only) */ |
7078 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
7079 | }; |
7080 | ||
375637bc | 7081 | perf_addr_filters_adjust(vma); |
cdd6c482 | 7082 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
7083 | } |
7084 | ||
68db7e98 AS |
7085 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
7086 | unsigned long size, u64 flags) | |
7087 | { | |
7088 | struct perf_output_handle handle; | |
7089 | struct perf_sample_data sample; | |
7090 | struct perf_aux_event { | |
7091 | struct perf_event_header header; | |
7092 | u64 offset; | |
7093 | u64 size; | |
7094 | u64 flags; | |
7095 | } rec = { | |
7096 | .header = { | |
7097 | .type = PERF_RECORD_AUX, | |
7098 | .misc = 0, | |
7099 | .size = sizeof(rec), | |
7100 | }, | |
7101 | .offset = head, | |
7102 | .size = size, | |
7103 | .flags = flags, | |
7104 | }; | |
7105 | int ret; | |
7106 | ||
7107 | perf_event_header__init_id(&rec.header, &sample, event); | |
7108 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7109 | ||
7110 | if (ret) | |
7111 | return; | |
7112 | ||
7113 | perf_output_put(&handle, rec); | |
7114 | perf_event__output_id_sample(event, &handle, &sample); | |
7115 | ||
7116 | perf_output_end(&handle); | |
7117 | } | |
7118 | ||
f38b0dbb KL |
7119 | /* |
7120 | * Lost/dropped samples logging | |
7121 | */ | |
7122 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
7123 | { | |
7124 | struct perf_output_handle handle; | |
7125 | struct perf_sample_data sample; | |
7126 | int ret; | |
7127 | ||
7128 | struct { | |
7129 | struct perf_event_header header; | |
7130 | u64 lost; | |
7131 | } lost_samples_event = { | |
7132 | .header = { | |
7133 | .type = PERF_RECORD_LOST_SAMPLES, | |
7134 | .misc = 0, | |
7135 | .size = sizeof(lost_samples_event), | |
7136 | }, | |
7137 | .lost = lost, | |
7138 | }; | |
7139 | ||
7140 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
7141 | ||
7142 | ret = perf_output_begin(&handle, event, | |
7143 | lost_samples_event.header.size); | |
7144 | if (ret) | |
7145 | return; | |
7146 | ||
7147 | perf_output_put(&handle, lost_samples_event); | |
7148 | perf_event__output_id_sample(event, &handle, &sample); | |
7149 | perf_output_end(&handle); | |
7150 | } | |
7151 | ||
45ac1403 AH |
7152 | /* |
7153 | * context_switch tracking | |
7154 | */ | |
7155 | ||
7156 | struct perf_switch_event { | |
7157 | struct task_struct *task; | |
7158 | struct task_struct *next_prev; | |
7159 | ||
7160 | struct { | |
7161 | struct perf_event_header header; | |
7162 | u32 next_prev_pid; | |
7163 | u32 next_prev_tid; | |
7164 | } event_id; | |
7165 | }; | |
7166 | ||
7167 | static int perf_event_switch_match(struct perf_event *event) | |
7168 | { | |
7169 | return event->attr.context_switch; | |
7170 | } | |
7171 | ||
7172 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
7173 | { | |
7174 | struct perf_switch_event *se = data; | |
7175 | struct perf_output_handle handle; | |
7176 | struct perf_sample_data sample; | |
7177 | int ret; | |
7178 | ||
7179 | if (!perf_event_switch_match(event)) | |
7180 | return; | |
7181 | ||
7182 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
7183 | if (event->ctx->task) { | |
7184 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
7185 | se->event_id.header.size = sizeof(se->event_id.header); | |
7186 | } else { | |
7187 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
7188 | se->event_id.header.size = sizeof(se->event_id); | |
7189 | se->event_id.next_prev_pid = | |
7190 | perf_event_pid(event, se->next_prev); | |
7191 | se->event_id.next_prev_tid = | |
7192 | perf_event_tid(event, se->next_prev); | |
7193 | } | |
7194 | ||
7195 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
7196 | ||
7197 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
7198 | if (ret) | |
7199 | return; | |
7200 | ||
7201 | if (event->ctx->task) | |
7202 | perf_output_put(&handle, se->event_id.header); | |
7203 | else | |
7204 | perf_output_put(&handle, se->event_id); | |
7205 | ||
7206 | perf_event__output_id_sample(event, &handle, &sample); | |
7207 | ||
7208 | perf_output_end(&handle); | |
7209 | } | |
7210 | ||
7211 | static void perf_event_switch(struct task_struct *task, | |
7212 | struct task_struct *next_prev, bool sched_in) | |
7213 | { | |
7214 | struct perf_switch_event switch_event; | |
7215 | ||
7216 | /* N.B. caller checks nr_switch_events != 0 */ | |
7217 | ||
7218 | switch_event = (struct perf_switch_event){ | |
7219 | .task = task, | |
7220 | .next_prev = next_prev, | |
7221 | .event_id = { | |
7222 | .header = { | |
7223 | /* .type */ | |
7224 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
7225 | /* .size */ | |
7226 | }, | |
7227 | /* .next_prev_pid */ | |
7228 | /* .next_prev_tid */ | |
7229 | }, | |
7230 | }; | |
7231 | ||
aab5b71e | 7232 | perf_iterate_sb(perf_event_switch_output, |
45ac1403 AH |
7233 | &switch_event, |
7234 | NULL); | |
7235 | } | |
7236 | ||
a78ac325 PZ |
7237 | /* |
7238 | * IRQ throttle logging | |
7239 | */ | |
7240 | ||
cdd6c482 | 7241 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
7242 | { |
7243 | struct perf_output_handle handle; | |
c980d109 | 7244 | struct perf_sample_data sample; |
a78ac325 PZ |
7245 | int ret; |
7246 | ||
7247 | struct { | |
7248 | struct perf_event_header header; | |
7249 | u64 time; | |
cca3f454 | 7250 | u64 id; |
7f453c24 | 7251 | u64 stream_id; |
a78ac325 PZ |
7252 | } throttle_event = { |
7253 | .header = { | |
cdd6c482 | 7254 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
7255 | .misc = 0, |
7256 | .size = sizeof(throttle_event), | |
7257 | }, | |
34f43927 | 7258 | .time = perf_event_clock(event), |
cdd6c482 IM |
7259 | .id = primary_event_id(event), |
7260 | .stream_id = event->id, | |
a78ac325 PZ |
7261 | }; |
7262 | ||
966ee4d6 | 7263 | if (enable) |
cdd6c482 | 7264 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 7265 | |
c980d109 ACM |
7266 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
7267 | ||
7268 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7269 | throttle_event.header.size); |
a78ac325 PZ |
7270 | if (ret) |
7271 | return; | |
7272 | ||
7273 | perf_output_put(&handle, throttle_event); | |
c980d109 | 7274 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
7275 | perf_output_end(&handle); |
7276 | } | |
7277 | ||
8d4e6c4c AS |
7278 | void perf_event_itrace_started(struct perf_event *event) |
7279 | { | |
7280 | event->attach_state |= PERF_ATTACH_ITRACE; | |
7281 | } | |
7282 | ||
ec0d7729 AS |
7283 | static void perf_log_itrace_start(struct perf_event *event) |
7284 | { | |
7285 | struct perf_output_handle handle; | |
7286 | struct perf_sample_data sample; | |
7287 | struct perf_aux_event { | |
7288 | struct perf_event_header header; | |
7289 | u32 pid; | |
7290 | u32 tid; | |
7291 | } rec; | |
7292 | int ret; | |
7293 | ||
7294 | if (event->parent) | |
7295 | event = event->parent; | |
7296 | ||
7297 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
8d4e6c4c | 7298 | event->attach_state & PERF_ATTACH_ITRACE) |
ec0d7729 AS |
7299 | return; |
7300 | ||
ec0d7729 AS |
7301 | rec.header.type = PERF_RECORD_ITRACE_START; |
7302 | rec.header.misc = 0; | |
7303 | rec.header.size = sizeof(rec); | |
7304 | rec.pid = perf_event_pid(event, current); | |
7305 | rec.tid = perf_event_tid(event, current); | |
7306 | ||
7307 | perf_event_header__init_id(&rec.header, &sample, event); | |
7308 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7309 | ||
7310 | if (ret) | |
7311 | return; | |
7312 | ||
7313 | perf_output_put(&handle, rec); | |
7314 | perf_event__output_id_sample(event, &handle, &sample); | |
7315 | ||
7316 | perf_output_end(&handle); | |
7317 | } | |
7318 | ||
475113d9 JO |
7319 | static int |
7320 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 7321 | { |
cdd6c482 | 7322 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 7323 | int ret = 0; |
475113d9 | 7324 | u64 seq; |
96398826 | 7325 | |
e050e3f0 SE |
7326 | seq = __this_cpu_read(perf_throttled_seq); |
7327 | if (seq != hwc->interrupts_seq) { | |
7328 | hwc->interrupts_seq = seq; | |
7329 | hwc->interrupts = 1; | |
7330 | } else { | |
7331 | hwc->interrupts++; | |
7332 | if (unlikely(throttle | |
7333 | && hwc->interrupts >= max_samples_per_tick)) { | |
7334 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 7335 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
7336 | hwc->interrupts = MAX_INTERRUPTS; |
7337 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
7338 | ret = 1; |
7339 | } | |
e050e3f0 | 7340 | } |
60db5e09 | 7341 | |
cdd6c482 | 7342 | if (event->attr.freq) { |
def0a9b2 | 7343 | u64 now = perf_clock(); |
abd50713 | 7344 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 7345 | |
abd50713 | 7346 | hwc->freq_time_stamp = now; |
bd2b5b12 | 7347 | |
abd50713 | 7348 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 7349 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
7350 | } |
7351 | ||
475113d9 JO |
7352 | return ret; |
7353 | } | |
7354 | ||
7355 | int perf_event_account_interrupt(struct perf_event *event) | |
7356 | { | |
7357 | return __perf_event_account_interrupt(event, 1); | |
7358 | } | |
7359 | ||
7360 | /* | |
7361 | * Generic event overflow handling, sampling. | |
7362 | */ | |
7363 | ||
7364 | static int __perf_event_overflow(struct perf_event *event, | |
7365 | int throttle, struct perf_sample_data *data, | |
7366 | struct pt_regs *regs) | |
7367 | { | |
7368 | int events = atomic_read(&event->event_limit); | |
7369 | int ret = 0; | |
7370 | ||
7371 | /* | |
7372 | * Non-sampling counters might still use the PMI to fold short | |
7373 | * hardware counters, ignore those. | |
7374 | */ | |
7375 | if (unlikely(!is_sampling_event(event))) | |
7376 | return 0; | |
7377 | ||
7378 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 7379 | |
2023b359 PZ |
7380 | /* |
7381 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 7382 | * events |
2023b359 PZ |
7383 | */ |
7384 | ||
cdd6c482 IM |
7385 | event->pending_kill = POLL_IN; |
7386 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 7387 | ret = 1; |
cdd6c482 | 7388 | event->pending_kill = POLL_HUP; |
5aab90ce JO |
7389 | |
7390 | perf_event_disable_inatomic(event); | |
79f14641 PZ |
7391 | } |
7392 | ||
aa6a5f3c | 7393 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 7394 | |
fed66e2c | 7395 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
7396 | event->pending_wakeup = 1; |
7397 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
7398 | } |
7399 | ||
79f14641 | 7400 | return ret; |
f6c7d5fe PZ |
7401 | } |
7402 | ||
a8b0ca17 | 7403 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
7404 | struct perf_sample_data *data, |
7405 | struct pt_regs *regs) | |
850bc73f | 7406 | { |
a8b0ca17 | 7407 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
7408 | } |
7409 | ||
15dbf27c | 7410 | /* |
cdd6c482 | 7411 | * Generic software event infrastructure |
15dbf27c PZ |
7412 | */ |
7413 | ||
b28ab83c PZ |
7414 | struct swevent_htable { |
7415 | struct swevent_hlist *swevent_hlist; | |
7416 | struct mutex hlist_mutex; | |
7417 | int hlist_refcount; | |
7418 | ||
7419 | /* Recursion avoidance in each contexts */ | |
7420 | int recursion[PERF_NR_CONTEXTS]; | |
7421 | }; | |
7422 | ||
7423 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
7424 | ||
7b4b6658 | 7425 | /* |
cdd6c482 IM |
7426 | * We directly increment event->count and keep a second value in |
7427 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
7428 | * is kept in the range [-sample_period, 0] so that we can use the |
7429 | * sign as trigger. | |
7430 | */ | |
7431 | ||
ab573844 | 7432 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 7433 | { |
cdd6c482 | 7434 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
7435 | u64 period = hwc->last_period; |
7436 | u64 nr, offset; | |
7437 | s64 old, val; | |
7438 | ||
7439 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
7440 | |
7441 | again: | |
e7850595 | 7442 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
7443 | if (val < 0) |
7444 | return 0; | |
15dbf27c | 7445 | |
7b4b6658 PZ |
7446 | nr = div64_u64(period + val, period); |
7447 | offset = nr * period; | |
7448 | val -= offset; | |
e7850595 | 7449 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 7450 | goto again; |
15dbf27c | 7451 | |
7b4b6658 | 7452 | return nr; |
15dbf27c PZ |
7453 | } |
7454 | ||
0cff784a | 7455 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 7456 | struct perf_sample_data *data, |
5622f295 | 7457 | struct pt_regs *regs) |
15dbf27c | 7458 | { |
cdd6c482 | 7459 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 7460 | int throttle = 0; |
15dbf27c | 7461 | |
0cff784a PZ |
7462 | if (!overflow) |
7463 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 7464 | |
7b4b6658 PZ |
7465 | if (hwc->interrupts == MAX_INTERRUPTS) |
7466 | return; | |
15dbf27c | 7467 | |
7b4b6658 | 7468 | for (; overflow; overflow--) { |
a8b0ca17 | 7469 | if (__perf_event_overflow(event, throttle, |
5622f295 | 7470 | data, regs)) { |
7b4b6658 PZ |
7471 | /* |
7472 | * We inhibit the overflow from happening when | |
7473 | * hwc->interrupts == MAX_INTERRUPTS. | |
7474 | */ | |
7475 | break; | |
7476 | } | |
cf450a73 | 7477 | throttle = 1; |
7b4b6658 | 7478 | } |
15dbf27c PZ |
7479 | } |
7480 | ||
a4eaf7f1 | 7481 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 7482 | struct perf_sample_data *data, |
5622f295 | 7483 | struct pt_regs *regs) |
7b4b6658 | 7484 | { |
cdd6c482 | 7485 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 7486 | |
e7850595 | 7487 | local64_add(nr, &event->count); |
d6d020e9 | 7488 | |
0cff784a PZ |
7489 | if (!regs) |
7490 | return; | |
7491 | ||
6c7e550f | 7492 | if (!is_sampling_event(event)) |
7b4b6658 | 7493 | return; |
d6d020e9 | 7494 | |
5d81e5cf AV |
7495 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
7496 | data->period = nr; | |
7497 | return perf_swevent_overflow(event, 1, data, regs); | |
7498 | } else | |
7499 | data->period = event->hw.last_period; | |
7500 | ||
0cff784a | 7501 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 7502 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 7503 | |
e7850595 | 7504 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 7505 | return; |
df1a132b | 7506 | |
a8b0ca17 | 7507 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
7508 | } |
7509 | ||
f5ffe02e FW |
7510 | static int perf_exclude_event(struct perf_event *event, |
7511 | struct pt_regs *regs) | |
7512 | { | |
a4eaf7f1 | 7513 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 7514 | return 1; |
a4eaf7f1 | 7515 | |
f5ffe02e FW |
7516 | if (regs) { |
7517 | if (event->attr.exclude_user && user_mode(regs)) | |
7518 | return 1; | |
7519 | ||
7520 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
7521 | return 1; | |
7522 | } | |
7523 | ||
7524 | return 0; | |
7525 | } | |
7526 | ||
cdd6c482 | 7527 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 7528 | enum perf_type_id type, |
6fb2915d LZ |
7529 | u32 event_id, |
7530 | struct perf_sample_data *data, | |
7531 | struct pt_regs *regs) | |
15dbf27c | 7532 | { |
cdd6c482 | 7533 | if (event->attr.type != type) |
a21ca2ca | 7534 | return 0; |
f5ffe02e | 7535 | |
cdd6c482 | 7536 | if (event->attr.config != event_id) |
15dbf27c PZ |
7537 | return 0; |
7538 | ||
f5ffe02e FW |
7539 | if (perf_exclude_event(event, regs)) |
7540 | return 0; | |
15dbf27c PZ |
7541 | |
7542 | return 1; | |
7543 | } | |
7544 | ||
76e1d904 FW |
7545 | static inline u64 swevent_hash(u64 type, u32 event_id) |
7546 | { | |
7547 | u64 val = event_id | (type << 32); | |
7548 | ||
7549 | return hash_64(val, SWEVENT_HLIST_BITS); | |
7550 | } | |
7551 | ||
49f135ed FW |
7552 | static inline struct hlist_head * |
7553 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 7554 | { |
49f135ed FW |
7555 | u64 hash = swevent_hash(type, event_id); |
7556 | ||
7557 | return &hlist->heads[hash]; | |
7558 | } | |
76e1d904 | 7559 | |
49f135ed FW |
7560 | /* For the read side: events when they trigger */ |
7561 | static inline struct hlist_head * | |
b28ab83c | 7562 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
7563 | { |
7564 | struct swevent_hlist *hlist; | |
76e1d904 | 7565 | |
b28ab83c | 7566 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
7567 | if (!hlist) |
7568 | return NULL; | |
7569 | ||
49f135ed FW |
7570 | return __find_swevent_head(hlist, type, event_id); |
7571 | } | |
7572 | ||
7573 | /* For the event head insertion and removal in the hlist */ | |
7574 | static inline struct hlist_head * | |
b28ab83c | 7575 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
7576 | { |
7577 | struct swevent_hlist *hlist; | |
7578 | u32 event_id = event->attr.config; | |
7579 | u64 type = event->attr.type; | |
7580 | ||
7581 | /* | |
7582 | * Event scheduling is always serialized against hlist allocation | |
7583 | * and release. Which makes the protected version suitable here. | |
7584 | * The context lock guarantees that. | |
7585 | */ | |
b28ab83c | 7586 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
7587 | lockdep_is_held(&event->ctx->lock)); |
7588 | if (!hlist) | |
7589 | return NULL; | |
7590 | ||
7591 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
7592 | } |
7593 | ||
7594 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 7595 | u64 nr, |
76e1d904 FW |
7596 | struct perf_sample_data *data, |
7597 | struct pt_regs *regs) | |
15dbf27c | 7598 | { |
4a32fea9 | 7599 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7600 | struct perf_event *event; |
76e1d904 | 7601 | struct hlist_head *head; |
15dbf27c | 7602 | |
76e1d904 | 7603 | rcu_read_lock(); |
b28ab83c | 7604 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
7605 | if (!head) |
7606 | goto end; | |
7607 | ||
b67bfe0d | 7608 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 7609 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 7610 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 7611 | } |
76e1d904 FW |
7612 | end: |
7613 | rcu_read_unlock(); | |
15dbf27c PZ |
7614 | } |
7615 | ||
86038c5e PZI |
7616 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
7617 | ||
4ed7c92d | 7618 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 7619 | { |
4a32fea9 | 7620 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 7621 | |
b28ab83c | 7622 | return get_recursion_context(swhash->recursion); |
96f6d444 | 7623 | } |
645e8cc0 | 7624 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 7625 | |
98b5c2c6 | 7626 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 7627 | { |
4a32fea9 | 7628 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 7629 | |
b28ab83c | 7630 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 7631 | } |
15dbf27c | 7632 | |
86038c5e | 7633 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 7634 | { |
a4234bfc | 7635 | struct perf_sample_data data; |
4ed7c92d | 7636 | |
86038c5e | 7637 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 7638 | return; |
a4234bfc | 7639 | |
fd0d000b | 7640 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 7641 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
7642 | } |
7643 | ||
7644 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
7645 | { | |
7646 | int rctx; | |
7647 | ||
7648 | preempt_disable_notrace(); | |
7649 | rctx = perf_swevent_get_recursion_context(); | |
7650 | if (unlikely(rctx < 0)) | |
7651 | goto fail; | |
7652 | ||
7653 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
7654 | |
7655 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 7656 | fail: |
1c024eca | 7657 | preempt_enable_notrace(); |
b8e83514 PZ |
7658 | } |
7659 | ||
cdd6c482 | 7660 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 7661 | { |
15dbf27c PZ |
7662 | } |
7663 | ||
a4eaf7f1 | 7664 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 7665 | { |
4a32fea9 | 7666 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7667 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
7668 | struct hlist_head *head; |
7669 | ||
6c7e550f | 7670 | if (is_sampling_event(event)) { |
7b4b6658 | 7671 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 7672 | perf_swevent_set_period(event); |
7b4b6658 | 7673 | } |
76e1d904 | 7674 | |
a4eaf7f1 PZ |
7675 | hwc->state = !(flags & PERF_EF_START); |
7676 | ||
b28ab83c | 7677 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 7678 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
7679 | return -EINVAL; |
7680 | ||
7681 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 7682 | perf_event_update_userpage(event); |
76e1d904 | 7683 | |
15dbf27c PZ |
7684 | return 0; |
7685 | } | |
7686 | ||
a4eaf7f1 | 7687 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 7688 | { |
76e1d904 | 7689 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
7690 | } |
7691 | ||
a4eaf7f1 | 7692 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 7693 | { |
a4eaf7f1 | 7694 | event->hw.state = 0; |
d6d020e9 | 7695 | } |
aa9c4c0f | 7696 | |
a4eaf7f1 | 7697 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 7698 | { |
a4eaf7f1 | 7699 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
7700 | } |
7701 | ||
49f135ed FW |
7702 | /* Deref the hlist from the update side */ |
7703 | static inline struct swevent_hlist * | |
b28ab83c | 7704 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 7705 | { |
b28ab83c PZ |
7706 | return rcu_dereference_protected(swhash->swevent_hlist, |
7707 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
7708 | } |
7709 | ||
b28ab83c | 7710 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 7711 | { |
b28ab83c | 7712 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 7713 | |
49f135ed | 7714 | if (!hlist) |
76e1d904 FW |
7715 | return; |
7716 | ||
70691d4a | 7717 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 7718 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
7719 | } |
7720 | ||
3b364d7b | 7721 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 7722 | { |
b28ab83c | 7723 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 7724 | |
b28ab83c | 7725 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 7726 | |
b28ab83c PZ |
7727 | if (!--swhash->hlist_refcount) |
7728 | swevent_hlist_release(swhash); | |
76e1d904 | 7729 | |
b28ab83c | 7730 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7731 | } |
7732 | ||
3b364d7b | 7733 | static void swevent_hlist_put(void) |
76e1d904 FW |
7734 | { |
7735 | int cpu; | |
7736 | ||
76e1d904 | 7737 | for_each_possible_cpu(cpu) |
3b364d7b | 7738 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7739 | } |
7740 | ||
3b364d7b | 7741 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 7742 | { |
b28ab83c | 7743 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
7744 | int err = 0; |
7745 | ||
b28ab83c | 7746 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
7747 | if (!swevent_hlist_deref(swhash) && |
7748 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
7749 | struct swevent_hlist *hlist; |
7750 | ||
7751 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
7752 | if (!hlist) { | |
7753 | err = -ENOMEM; | |
7754 | goto exit; | |
7755 | } | |
b28ab83c | 7756 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 7757 | } |
b28ab83c | 7758 | swhash->hlist_refcount++; |
9ed6060d | 7759 | exit: |
b28ab83c | 7760 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7761 | |
7762 | return err; | |
7763 | } | |
7764 | ||
3b364d7b | 7765 | static int swevent_hlist_get(void) |
76e1d904 | 7766 | { |
3b364d7b | 7767 | int err, cpu, failed_cpu; |
76e1d904 | 7768 | |
a63fbed7 | 7769 | mutex_lock(&pmus_lock); |
76e1d904 | 7770 | for_each_possible_cpu(cpu) { |
3b364d7b | 7771 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
7772 | if (err) { |
7773 | failed_cpu = cpu; | |
7774 | goto fail; | |
7775 | } | |
7776 | } | |
a63fbed7 | 7777 | mutex_unlock(&pmus_lock); |
76e1d904 | 7778 | return 0; |
9ed6060d | 7779 | fail: |
76e1d904 FW |
7780 | for_each_possible_cpu(cpu) { |
7781 | if (cpu == failed_cpu) | |
7782 | break; | |
3b364d7b | 7783 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 7784 | } |
a63fbed7 | 7785 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
7786 | return err; |
7787 | } | |
7788 | ||
c5905afb | 7789 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 7790 | |
b0a873eb PZ |
7791 | static void sw_perf_event_destroy(struct perf_event *event) |
7792 | { | |
7793 | u64 event_id = event->attr.config; | |
95476b64 | 7794 | |
b0a873eb PZ |
7795 | WARN_ON(event->parent); |
7796 | ||
c5905afb | 7797 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 7798 | swevent_hlist_put(); |
b0a873eb PZ |
7799 | } |
7800 | ||
7801 | static int perf_swevent_init(struct perf_event *event) | |
7802 | { | |
8176cced | 7803 | u64 event_id = event->attr.config; |
b0a873eb PZ |
7804 | |
7805 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7806 | return -ENOENT; | |
7807 | ||
2481c5fa SE |
7808 | /* |
7809 | * no branch sampling for software events | |
7810 | */ | |
7811 | if (has_branch_stack(event)) | |
7812 | return -EOPNOTSUPP; | |
7813 | ||
b0a873eb PZ |
7814 | switch (event_id) { |
7815 | case PERF_COUNT_SW_CPU_CLOCK: | |
7816 | case PERF_COUNT_SW_TASK_CLOCK: | |
7817 | return -ENOENT; | |
7818 | ||
7819 | default: | |
7820 | break; | |
7821 | } | |
7822 | ||
ce677831 | 7823 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
7824 | return -ENOENT; |
7825 | ||
7826 | if (!event->parent) { | |
7827 | int err; | |
7828 | ||
3b364d7b | 7829 | err = swevent_hlist_get(); |
b0a873eb PZ |
7830 | if (err) |
7831 | return err; | |
7832 | ||
c5905afb | 7833 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
7834 | event->destroy = sw_perf_event_destroy; |
7835 | } | |
7836 | ||
7837 | return 0; | |
7838 | } | |
7839 | ||
7840 | static struct pmu perf_swevent = { | |
89a1e187 | 7841 | .task_ctx_nr = perf_sw_context, |
95476b64 | 7842 | |
34f43927 PZ |
7843 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7844 | ||
b0a873eb | 7845 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
7846 | .add = perf_swevent_add, |
7847 | .del = perf_swevent_del, | |
7848 | .start = perf_swevent_start, | |
7849 | .stop = perf_swevent_stop, | |
1c024eca | 7850 | .read = perf_swevent_read, |
1c024eca PZ |
7851 | }; |
7852 | ||
b0a873eb PZ |
7853 | #ifdef CONFIG_EVENT_TRACING |
7854 | ||
1c024eca PZ |
7855 | static int perf_tp_filter_match(struct perf_event *event, |
7856 | struct perf_sample_data *data) | |
7857 | { | |
7e3f977e | 7858 | void *record = data->raw->frag.data; |
1c024eca | 7859 | |
b71b437e PZ |
7860 | /* only top level events have filters set */ |
7861 | if (event->parent) | |
7862 | event = event->parent; | |
7863 | ||
1c024eca PZ |
7864 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
7865 | return 1; | |
7866 | return 0; | |
7867 | } | |
7868 | ||
7869 | static int perf_tp_event_match(struct perf_event *event, | |
7870 | struct perf_sample_data *data, | |
7871 | struct pt_regs *regs) | |
7872 | { | |
a0f7d0f7 FW |
7873 | if (event->hw.state & PERF_HES_STOPPED) |
7874 | return 0; | |
580d607c PZ |
7875 | /* |
7876 | * All tracepoints are from kernel-space. | |
7877 | */ | |
7878 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
7879 | return 0; |
7880 | ||
7881 | if (!perf_tp_filter_match(event, data)) | |
7882 | return 0; | |
7883 | ||
7884 | return 1; | |
7885 | } | |
7886 | ||
85b67bcb AS |
7887 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
7888 | struct trace_event_call *call, u64 count, | |
7889 | struct pt_regs *regs, struct hlist_head *head, | |
7890 | struct task_struct *task) | |
7891 | { | |
e87c6bc3 | 7892 | if (bpf_prog_array_valid(call)) { |
85b67bcb | 7893 | *(struct pt_regs **)raw_data = regs; |
e87c6bc3 | 7894 | if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { |
85b67bcb AS |
7895 | perf_swevent_put_recursion_context(rctx); |
7896 | return; | |
7897 | } | |
7898 | } | |
7899 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
8fd0fbbe | 7900 | rctx, task); |
85b67bcb AS |
7901 | } |
7902 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
7903 | ||
1e1dcd93 | 7904 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 7905 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
8fd0fbbe | 7906 | struct task_struct *task) |
95476b64 FW |
7907 | { |
7908 | struct perf_sample_data data; | |
8fd0fbbe | 7909 | struct perf_event *event; |
1c024eca | 7910 | |
95476b64 | 7911 | struct perf_raw_record raw = { |
7e3f977e DB |
7912 | .frag = { |
7913 | .size = entry_size, | |
7914 | .data = record, | |
7915 | }, | |
95476b64 FW |
7916 | }; |
7917 | ||
1e1dcd93 | 7918 | perf_sample_data_init(&data, 0, 0); |
95476b64 FW |
7919 | data.raw = &raw; |
7920 | ||
1e1dcd93 AS |
7921 | perf_trace_buf_update(record, event_type); |
7922 | ||
8fd0fbbe | 7923 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 7924 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 7925 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 7926 | } |
ecc55f84 | 7927 | |
e6dab5ff AV |
7928 | /* |
7929 | * If we got specified a target task, also iterate its context and | |
7930 | * deliver this event there too. | |
7931 | */ | |
7932 | if (task && task != current) { | |
7933 | struct perf_event_context *ctx; | |
7934 | struct trace_entry *entry = record; | |
7935 | ||
7936 | rcu_read_lock(); | |
7937 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
7938 | if (!ctx) | |
7939 | goto unlock; | |
7940 | ||
7941 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
7942 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7943 | continue; | |
7944 | if (event->attr.config != entry->type) | |
7945 | continue; | |
7946 | if (perf_tp_event_match(event, &data, regs)) | |
7947 | perf_swevent_event(event, count, &data, regs); | |
7948 | } | |
7949 | unlock: | |
7950 | rcu_read_unlock(); | |
7951 | } | |
7952 | ||
ecc55f84 | 7953 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
7954 | } |
7955 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
7956 | ||
cdd6c482 | 7957 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 7958 | { |
1c024eca | 7959 | perf_trace_destroy(event); |
e077df4f PZ |
7960 | } |
7961 | ||
b0a873eb | 7962 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 7963 | { |
76e1d904 FW |
7964 | int err; |
7965 | ||
b0a873eb PZ |
7966 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
7967 | return -ENOENT; | |
7968 | ||
2481c5fa SE |
7969 | /* |
7970 | * no branch sampling for tracepoint events | |
7971 | */ | |
7972 | if (has_branch_stack(event)) | |
7973 | return -EOPNOTSUPP; | |
7974 | ||
1c024eca PZ |
7975 | err = perf_trace_init(event); |
7976 | if (err) | |
b0a873eb | 7977 | return err; |
e077df4f | 7978 | |
cdd6c482 | 7979 | event->destroy = tp_perf_event_destroy; |
e077df4f | 7980 | |
b0a873eb PZ |
7981 | return 0; |
7982 | } | |
7983 | ||
7984 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
7985 | .task_ctx_nr = perf_sw_context, |
7986 | ||
b0a873eb | 7987 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
7988 | .add = perf_trace_add, |
7989 | .del = perf_trace_del, | |
7990 | .start = perf_swevent_start, | |
7991 | .stop = perf_swevent_stop, | |
b0a873eb | 7992 | .read = perf_swevent_read, |
b0a873eb PZ |
7993 | }; |
7994 | ||
7995 | static inline void perf_tp_register(void) | |
7996 | { | |
2e80a82a | 7997 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e077df4f | 7998 | } |
6fb2915d | 7999 | |
6fb2915d LZ |
8000 | static void perf_event_free_filter(struct perf_event *event) |
8001 | { | |
8002 | ftrace_profile_free_filter(event); | |
8003 | } | |
8004 | ||
aa6a5f3c AS |
8005 | #ifdef CONFIG_BPF_SYSCALL |
8006 | static void bpf_overflow_handler(struct perf_event *event, | |
8007 | struct perf_sample_data *data, | |
8008 | struct pt_regs *regs) | |
8009 | { | |
8010 | struct bpf_perf_event_data_kern ctx = { | |
8011 | .data = data, | |
7d9285e8 | 8012 | .event = event, |
aa6a5f3c AS |
8013 | }; |
8014 | int ret = 0; | |
8015 | ||
c895f6f7 | 8016 | ctx.regs = perf_arch_bpf_user_pt_regs(regs); |
aa6a5f3c AS |
8017 | preempt_disable(); |
8018 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) | |
8019 | goto out; | |
8020 | rcu_read_lock(); | |
88575199 | 8021 | ret = BPF_PROG_RUN(event->prog, &ctx); |
aa6a5f3c AS |
8022 | rcu_read_unlock(); |
8023 | out: | |
8024 | __this_cpu_dec(bpf_prog_active); | |
8025 | preempt_enable(); | |
8026 | if (!ret) | |
8027 | return; | |
8028 | ||
8029 | event->orig_overflow_handler(event, data, regs); | |
8030 | } | |
8031 | ||
8032 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8033 | { | |
8034 | struct bpf_prog *prog; | |
8035 | ||
8036 | if (event->overflow_handler_context) | |
8037 | /* hw breakpoint or kernel counter */ | |
8038 | return -EINVAL; | |
8039 | ||
8040 | if (event->prog) | |
8041 | return -EEXIST; | |
8042 | ||
8043 | prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); | |
8044 | if (IS_ERR(prog)) | |
8045 | return PTR_ERR(prog); | |
8046 | ||
8047 | event->prog = prog; | |
8048 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); | |
8049 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
8050 | return 0; | |
8051 | } | |
8052 | ||
8053 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8054 | { | |
8055 | struct bpf_prog *prog = event->prog; | |
8056 | ||
8057 | if (!prog) | |
8058 | return; | |
8059 | ||
8060 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
8061 | event->prog = NULL; | |
8062 | bpf_prog_put(prog); | |
8063 | } | |
8064 | #else | |
8065 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8066 | { | |
8067 | return -EOPNOTSUPP; | |
8068 | } | |
8069 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8070 | { | |
8071 | } | |
8072 | #endif | |
8073 | ||
2541517c AS |
8074 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8075 | { | |
cf5f5cea | 8076 | bool is_kprobe, is_tracepoint, is_syscall_tp; |
2541517c | 8077 | struct bpf_prog *prog; |
e87c6bc3 | 8078 | int ret; |
2541517c AS |
8079 | |
8080 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
f91840a3 | 8081 | return perf_event_set_bpf_handler(event, prog_fd); |
2541517c | 8082 | |
98b5c2c6 AS |
8083 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; |
8084 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; | |
cf5f5cea YS |
8085 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
8086 | if (!is_kprobe && !is_tracepoint && !is_syscall_tp) | |
98b5c2c6 | 8087 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
2541517c AS |
8088 | return -EINVAL; |
8089 | ||
8090 | prog = bpf_prog_get(prog_fd); | |
8091 | if (IS_ERR(prog)) | |
8092 | return PTR_ERR(prog); | |
8093 | ||
98b5c2c6 | 8094 | if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || |
cf5f5cea YS |
8095 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || |
8096 | (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { | |
2541517c AS |
8097 | /* valid fd, but invalid bpf program type */ |
8098 | bpf_prog_put(prog); | |
8099 | return -EINVAL; | |
8100 | } | |
8101 | ||
cf5f5cea | 8102 | if (is_tracepoint || is_syscall_tp) { |
32bbe007 AS |
8103 | int off = trace_event_get_offsets(event->tp_event); |
8104 | ||
8105 | if (prog->aux->max_ctx_offset > off) { | |
8106 | bpf_prog_put(prog); | |
8107 | return -EACCES; | |
8108 | } | |
8109 | } | |
2541517c | 8110 | |
e87c6bc3 YS |
8111 | ret = perf_event_attach_bpf_prog(event, prog); |
8112 | if (ret) | |
8113 | bpf_prog_put(prog); | |
8114 | return ret; | |
2541517c AS |
8115 | } |
8116 | ||
8117 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8118 | { | |
0b4c6841 YS |
8119 | if (event->attr.type != PERF_TYPE_TRACEPOINT) { |
8120 | perf_event_free_bpf_handler(event); | |
2541517c | 8121 | return; |
2541517c | 8122 | } |
e87c6bc3 | 8123 | perf_event_detach_bpf_prog(event); |
2541517c AS |
8124 | } |
8125 | ||
e077df4f | 8126 | #else |
6fb2915d | 8127 | |
b0a873eb | 8128 | static inline void perf_tp_register(void) |
e077df4f | 8129 | { |
e077df4f | 8130 | } |
6fb2915d | 8131 | |
6fb2915d LZ |
8132 | static void perf_event_free_filter(struct perf_event *event) |
8133 | { | |
8134 | } | |
8135 | ||
2541517c AS |
8136 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8137 | { | |
8138 | return -ENOENT; | |
8139 | } | |
8140 | ||
8141 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8142 | { | |
8143 | } | |
07b139c8 | 8144 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 8145 | |
24f1e32c | 8146 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 8147 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 8148 | { |
f5ffe02e FW |
8149 | struct perf_sample_data sample; |
8150 | struct pt_regs *regs = data; | |
8151 | ||
fd0d000b | 8152 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 8153 | |
a4eaf7f1 | 8154 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 8155 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
8156 | } |
8157 | #endif | |
8158 | ||
375637bc AS |
8159 | /* |
8160 | * Allocate a new address filter | |
8161 | */ | |
8162 | static struct perf_addr_filter * | |
8163 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
8164 | { | |
8165 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
8166 | struct perf_addr_filter *filter; | |
8167 | ||
8168 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
8169 | if (!filter) | |
8170 | return NULL; | |
8171 | ||
8172 | INIT_LIST_HEAD(&filter->entry); | |
8173 | list_add_tail(&filter->entry, filters); | |
8174 | ||
8175 | return filter; | |
8176 | } | |
8177 | ||
8178 | static void free_filters_list(struct list_head *filters) | |
8179 | { | |
8180 | struct perf_addr_filter *filter, *iter; | |
8181 | ||
8182 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
8183 | if (filter->inode) | |
8184 | iput(filter->inode); | |
8185 | list_del(&filter->entry); | |
8186 | kfree(filter); | |
8187 | } | |
8188 | } | |
8189 | ||
8190 | /* | |
8191 | * Free existing address filters and optionally install new ones | |
8192 | */ | |
8193 | static void perf_addr_filters_splice(struct perf_event *event, | |
8194 | struct list_head *head) | |
8195 | { | |
8196 | unsigned long flags; | |
8197 | LIST_HEAD(list); | |
8198 | ||
8199 | if (!has_addr_filter(event)) | |
8200 | return; | |
8201 | ||
8202 | /* don't bother with children, they don't have their own filters */ | |
8203 | if (event->parent) | |
8204 | return; | |
8205 | ||
8206 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
8207 | ||
8208 | list_splice_init(&event->addr_filters.list, &list); | |
8209 | if (head) | |
8210 | list_splice(head, &event->addr_filters.list); | |
8211 | ||
8212 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
8213 | ||
8214 | free_filters_list(&list); | |
8215 | } | |
8216 | ||
8217 | /* | |
8218 | * Scan through mm's vmas and see if one of them matches the | |
8219 | * @filter; if so, adjust filter's address range. | |
8220 | * Called with mm::mmap_sem down for reading. | |
8221 | */ | |
8222 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, | |
8223 | struct mm_struct *mm) | |
8224 | { | |
8225 | struct vm_area_struct *vma; | |
8226 | ||
8227 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
8228 | struct file *file = vma->vm_file; | |
8229 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8230 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8231 | ||
8232 | if (!file) | |
8233 | continue; | |
8234 | ||
8235 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8236 | continue; | |
8237 | ||
8238 | return vma->vm_start; | |
8239 | } | |
8240 | ||
8241 | return 0; | |
8242 | } | |
8243 | ||
8244 | /* | |
8245 | * Update event's address range filters based on the | |
8246 | * task's existing mappings, if any. | |
8247 | */ | |
8248 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
8249 | { | |
8250 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8251 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
8252 | struct perf_addr_filter *filter; | |
8253 | struct mm_struct *mm = NULL; | |
8254 | unsigned int count = 0; | |
8255 | unsigned long flags; | |
8256 | ||
8257 | /* | |
8258 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
8259 | * will stop on the parent's child_mutex that our caller is also holding | |
8260 | */ | |
8261 | if (task == TASK_TOMBSTONE) | |
8262 | return; | |
8263 | ||
6ce77bfd AS |
8264 | if (!ifh->nr_file_filters) |
8265 | return; | |
8266 | ||
375637bc AS |
8267 | mm = get_task_mm(event->ctx->task); |
8268 | if (!mm) | |
8269 | goto restart; | |
8270 | ||
8271 | down_read(&mm->mmap_sem); | |
8272 | ||
8273 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8274 | list_for_each_entry(filter, &ifh->list, entry) { | |
8275 | event->addr_filters_offs[count] = 0; | |
8276 | ||
99f5bc9b MP |
8277 | /* |
8278 | * Adjust base offset if the filter is associated to a binary | |
8279 | * that needs to be mapped: | |
8280 | */ | |
8281 | if (filter->inode) | |
375637bc AS |
8282 | event->addr_filters_offs[count] = |
8283 | perf_addr_filter_apply(filter, mm); | |
8284 | ||
8285 | count++; | |
8286 | } | |
8287 | ||
8288 | event->addr_filters_gen++; | |
8289 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8290 | ||
8291 | up_read(&mm->mmap_sem); | |
8292 | ||
8293 | mmput(mm); | |
8294 | ||
8295 | restart: | |
767ae086 | 8296 | perf_event_stop(event, 1); |
375637bc AS |
8297 | } |
8298 | ||
8299 | /* | |
8300 | * Address range filtering: limiting the data to certain | |
8301 | * instruction address ranges. Filters are ioctl()ed to us from | |
8302 | * userspace as ascii strings. | |
8303 | * | |
8304 | * Filter string format: | |
8305 | * | |
8306 | * ACTION RANGE_SPEC | |
8307 | * where ACTION is one of the | |
8308 | * * "filter": limit the trace to this region | |
8309 | * * "start": start tracing from this address | |
8310 | * * "stop": stop tracing at this address/region; | |
8311 | * RANGE_SPEC is | |
8312 | * * for kernel addresses: <start address>[/<size>] | |
8313 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
8314 | * | |
8315 | * if <size> is not specified, the range is treated as a single address. | |
8316 | */ | |
8317 | enum { | |
e96271f3 | 8318 | IF_ACT_NONE = -1, |
375637bc AS |
8319 | IF_ACT_FILTER, |
8320 | IF_ACT_START, | |
8321 | IF_ACT_STOP, | |
8322 | IF_SRC_FILE, | |
8323 | IF_SRC_KERNEL, | |
8324 | IF_SRC_FILEADDR, | |
8325 | IF_SRC_KERNELADDR, | |
8326 | }; | |
8327 | ||
8328 | enum { | |
8329 | IF_STATE_ACTION = 0, | |
8330 | IF_STATE_SOURCE, | |
8331 | IF_STATE_END, | |
8332 | }; | |
8333 | ||
8334 | static const match_table_t if_tokens = { | |
8335 | { IF_ACT_FILTER, "filter" }, | |
8336 | { IF_ACT_START, "start" }, | |
8337 | { IF_ACT_STOP, "stop" }, | |
8338 | { IF_SRC_FILE, "%u/%u@%s" }, | |
8339 | { IF_SRC_KERNEL, "%u/%u" }, | |
8340 | { IF_SRC_FILEADDR, "%u@%s" }, | |
8341 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 8342 | { IF_ACT_NONE, NULL }, |
375637bc AS |
8343 | }; |
8344 | ||
8345 | /* | |
8346 | * Address filter string parser | |
8347 | */ | |
8348 | static int | |
8349 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
8350 | struct list_head *filters) | |
8351 | { | |
8352 | struct perf_addr_filter *filter = NULL; | |
8353 | char *start, *orig, *filename = NULL; | |
8354 | struct path path; | |
8355 | substring_t args[MAX_OPT_ARGS]; | |
8356 | int state = IF_STATE_ACTION, token; | |
8357 | unsigned int kernel = 0; | |
8358 | int ret = -EINVAL; | |
8359 | ||
8360 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
8361 | if (!fstr) | |
8362 | return -ENOMEM; | |
8363 | ||
8364 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
8365 | ret = -EINVAL; | |
8366 | ||
8367 | if (!*start) | |
8368 | continue; | |
8369 | ||
8370 | /* filter definition begins */ | |
8371 | if (state == IF_STATE_ACTION) { | |
8372 | filter = perf_addr_filter_new(event, filters); | |
8373 | if (!filter) | |
8374 | goto fail; | |
8375 | } | |
8376 | ||
8377 | token = match_token(start, if_tokens, args); | |
8378 | switch (token) { | |
8379 | case IF_ACT_FILTER: | |
8380 | case IF_ACT_START: | |
8381 | filter->filter = 1; | |
8382 | ||
8383 | case IF_ACT_STOP: | |
8384 | if (state != IF_STATE_ACTION) | |
8385 | goto fail; | |
8386 | ||
8387 | state = IF_STATE_SOURCE; | |
8388 | break; | |
8389 | ||
8390 | case IF_SRC_KERNELADDR: | |
8391 | case IF_SRC_KERNEL: | |
8392 | kernel = 1; | |
8393 | ||
8394 | case IF_SRC_FILEADDR: | |
8395 | case IF_SRC_FILE: | |
8396 | if (state != IF_STATE_SOURCE) | |
8397 | goto fail; | |
8398 | ||
8399 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) | |
8400 | filter->range = 1; | |
8401 | ||
8402 | *args[0].to = 0; | |
8403 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
8404 | if (ret) | |
8405 | goto fail; | |
8406 | ||
8407 | if (filter->range) { | |
8408 | *args[1].to = 0; | |
8409 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
8410 | if (ret) | |
8411 | goto fail; | |
8412 | } | |
8413 | ||
4059ffd0 MP |
8414 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
8415 | int fpos = filter->range ? 2 : 1; | |
8416 | ||
8417 | filename = match_strdup(&args[fpos]); | |
375637bc AS |
8418 | if (!filename) { |
8419 | ret = -ENOMEM; | |
8420 | goto fail; | |
8421 | } | |
8422 | } | |
8423 | ||
8424 | state = IF_STATE_END; | |
8425 | break; | |
8426 | ||
8427 | default: | |
8428 | goto fail; | |
8429 | } | |
8430 | ||
8431 | /* | |
8432 | * Filter definition is fully parsed, validate and install it. | |
8433 | * Make sure that it doesn't contradict itself or the event's | |
8434 | * attribute. | |
8435 | */ | |
8436 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 8437 | ret = -EINVAL; |
375637bc AS |
8438 | if (kernel && event->attr.exclude_kernel) |
8439 | goto fail; | |
8440 | ||
8441 | if (!kernel) { | |
8442 | if (!filename) | |
8443 | goto fail; | |
8444 | ||
6ce77bfd AS |
8445 | /* |
8446 | * For now, we only support file-based filters | |
8447 | * in per-task events; doing so for CPU-wide | |
8448 | * events requires additional context switching | |
8449 | * trickery, since same object code will be | |
8450 | * mapped at different virtual addresses in | |
8451 | * different processes. | |
8452 | */ | |
8453 | ret = -EOPNOTSUPP; | |
8454 | if (!event->ctx->task) | |
8455 | goto fail_free_name; | |
8456 | ||
375637bc AS |
8457 | /* look up the path and grab its inode */ |
8458 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
8459 | if (ret) | |
8460 | goto fail_free_name; | |
8461 | ||
8462 | filter->inode = igrab(d_inode(path.dentry)); | |
8463 | path_put(&path); | |
8464 | kfree(filename); | |
8465 | filename = NULL; | |
8466 | ||
8467 | ret = -EINVAL; | |
8468 | if (!filter->inode || | |
8469 | !S_ISREG(filter->inode->i_mode)) | |
8470 | /* free_filters_list() will iput() */ | |
8471 | goto fail; | |
6ce77bfd AS |
8472 | |
8473 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
8474 | } |
8475 | ||
8476 | /* ready to consume more filters */ | |
8477 | state = IF_STATE_ACTION; | |
8478 | filter = NULL; | |
8479 | } | |
8480 | } | |
8481 | ||
8482 | if (state != IF_STATE_ACTION) | |
8483 | goto fail; | |
8484 | ||
8485 | kfree(orig); | |
8486 | ||
8487 | return 0; | |
8488 | ||
8489 | fail_free_name: | |
8490 | kfree(filename); | |
8491 | fail: | |
8492 | free_filters_list(filters); | |
8493 | kfree(orig); | |
8494 | ||
8495 | return ret; | |
8496 | } | |
8497 | ||
8498 | static int | |
8499 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
8500 | { | |
8501 | LIST_HEAD(filters); | |
8502 | int ret; | |
8503 | ||
8504 | /* | |
8505 | * Since this is called in perf_ioctl() path, we're already holding | |
8506 | * ctx::mutex. | |
8507 | */ | |
8508 | lockdep_assert_held(&event->ctx->mutex); | |
8509 | ||
8510 | if (WARN_ON_ONCE(event->parent)) | |
8511 | return -EINVAL; | |
8512 | ||
375637bc AS |
8513 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
8514 | if (ret) | |
6ce77bfd | 8515 | goto fail_clear_files; |
375637bc AS |
8516 | |
8517 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
8518 | if (ret) |
8519 | goto fail_free_filters; | |
375637bc AS |
8520 | |
8521 | /* remove existing filters, if any */ | |
8522 | perf_addr_filters_splice(event, &filters); | |
8523 | ||
8524 | /* install new filters */ | |
8525 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
8526 | ||
6ce77bfd AS |
8527 | return ret; |
8528 | ||
8529 | fail_free_filters: | |
8530 | free_filters_list(&filters); | |
8531 | ||
8532 | fail_clear_files: | |
8533 | event->addr_filters.nr_file_filters = 0; | |
8534 | ||
375637bc AS |
8535 | return ret; |
8536 | } | |
8537 | ||
43fa87f7 PZ |
8538 | static int |
8539 | perf_tracepoint_set_filter(struct perf_event *event, char *filter_str) | |
8540 | { | |
8541 | struct perf_event_context *ctx = event->ctx; | |
8542 | int ret; | |
8543 | ||
8544 | /* | |
8545 | * Beware, here be dragons!! | |
8546 | * | |
8547 | * the tracepoint muck will deadlock against ctx->mutex, but the tracepoint | |
8548 | * stuff does not actually need it. So temporarily drop ctx->mutex. As per | |
8549 | * perf_event_ctx_lock() we already have a reference on ctx. | |
8550 | * | |
8551 | * This can result in event getting moved to a different ctx, but that | |
8552 | * does not affect the tracepoint state. | |
8553 | */ | |
8554 | mutex_unlock(&ctx->mutex); | |
8555 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
8556 | mutex_lock(&ctx->mutex); | |
8557 | ||
8558 | return ret; | |
8559 | } | |
8560 | ||
c796bbbe AS |
8561 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
8562 | { | |
8563 | char *filter_str; | |
8564 | int ret = -EINVAL; | |
8565 | ||
375637bc AS |
8566 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
8567 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && | |
8568 | !has_addr_filter(event)) | |
c796bbbe AS |
8569 | return -EINVAL; |
8570 | ||
8571 | filter_str = strndup_user(arg, PAGE_SIZE); | |
8572 | if (IS_ERR(filter_str)) | |
8573 | return PTR_ERR(filter_str); | |
8574 | ||
8575 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && | |
8576 | event->attr.type == PERF_TYPE_TRACEPOINT) | |
43fa87f7 | 8577 | ret = perf_tracepoint_set_filter(event, filter_str); |
375637bc AS |
8578 | else if (has_addr_filter(event)) |
8579 | ret = perf_event_set_addr_filter(event, filter_str); | |
c796bbbe AS |
8580 | |
8581 | kfree(filter_str); | |
8582 | return ret; | |
8583 | } | |
8584 | ||
b0a873eb PZ |
8585 | /* |
8586 | * hrtimer based swevent callback | |
8587 | */ | |
f29ac756 | 8588 | |
b0a873eb | 8589 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 8590 | { |
b0a873eb PZ |
8591 | enum hrtimer_restart ret = HRTIMER_RESTART; |
8592 | struct perf_sample_data data; | |
8593 | struct pt_regs *regs; | |
8594 | struct perf_event *event; | |
8595 | u64 period; | |
f29ac756 | 8596 | |
b0a873eb | 8597 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
8598 | |
8599 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
8600 | return HRTIMER_NORESTART; | |
8601 | ||
b0a873eb | 8602 | event->pmu->read(event); |
f344011c | 8603 | |
fd0d000b | 8604 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
8605 | regs = get_irq_regs(); |
8606 | ||
8607 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 8608 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 8609 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
8610 | ret = HRTIMER_NORESTART; |
8611 | } | |
24f1e32c | 8612 | |
b0a873eb PZ |
8613 | period = max_t(u64, 10000, event->hw.sample_period); |
8614 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 8615 | |
b0a873eb | 8616 | return ret; |
f29ac756 PZ |
8617 | } |
8618 | ||
b0a873eb | 8619 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 8620 | { |
b0a873eb | 8621 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
8622 | s64 period; |
8623 | ||
8624 | if (!is_sampling_event(event)) | |
8625 | return; | |
f5ffe02e | 8626 | |
5d508e82 FBH |
8627 | period = local64_read(&hwc->period_left); |
8628 | if (period) { | |
8629 | if (period < 0) | |
8630 | period = 10000; | |
fa407f35 | 8631 | |
5d508e82 FBH |
8632 | local64_set(&hwc->period_left, 0); |
8633 | } else { | |
8634 | period = max_t(u64, 10000, hwc->sample_period); | |
8635 | } | |
3497d206 TG |
8636 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
8637 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 8638 | } |
b0a873eb PZ |
8639 | |
8640 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 8641 | { |
b0a873eb PZ |
8642 | struct hw_perf_event *hwc = &event->hw; |
8643 | ||
6c7e550f | 8644 | if (is_sampling_event(event)) { |
b0a873eb | 8645 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 8646 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
8647 | |
8648 | hrtimer_cancel(&hwc->hrtimer); | |
8649 | } | |
24f1e32c FW |
8650 | } |
8651 | ||
ba3dd36c PZ |
8652 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
8653 | { | |
8654 | struct hw_perf_event *hwc = &event->hw; | |
8655 | ||
8656 | if (!is_sampling_event(event)) | |
8657 | return; | |
8658 | ||
8659 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
8660 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
8661 | ||
8662 | /* | |
8663 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
8664 | * mapping and avoid the whole period adjust feedback stuff. | |
8665 | */ | |
8666 | if (event->attr.freq) { | |
8667 | long freq = event->attr.sample_freq; | |
8668 | ||
8669 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
8670 | hwc->sample_period = event->attr.sample_period; | |
8671 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 8672 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
8673 | event->attr.freq = 0; |
8674 | } | |
8675 | } | |
8676 | ||
b0a873eb PZ |
8677 | /* |
8678 | * Software event: cpu wall time clock | |
8679 | */ | |
8680 | ||
8681 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 8682 | { |
b0a873eb PZ |
8683 | s64 prev; |
8684 | u64 now; | |
8685 | ||
a4eaf7f1 | 8686 | now = local_clock(); |
b0a873eb PZ |
8687 | prev = local64_xchg(&event->hw.prev_count, now); |
8688 | local64_add(now - prev, &event->count); | |
24f1e32c | 8689 | } |
24f1e32c | 8690 | |
a4eaf7f1 | 8691 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8692 | { |
a4eaf7f1 | 8693 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 8694 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8695 | } |
8696 | ||
a4eaf7f1 | 8697 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 8698 | { |
b0a873eb PZ |
8699 | perf_swevent_cancel_hrtimer(event); |
8700 | cpu_clock_event_update(event); | |
8701 | } | |
f29ac756 | 8702 | |
a4eaf7f1 PZ |
8703 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
8704 | { | |
8705 | if (flags & PERF_EF_START) | |
8706 | cpu_clock_event_start(event, flags); | |
6a694a60 | 8707 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
8708 | |
8709 | return 0; | |
8710 | } | |
8711 | ||
8712 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
8713 | { | |
8714 | cpu_clock_event_stop(event, flags); | |
8715 | } | |
8716 | ||
b0a873eb PZ |
8717 | static void cpu_clock_event_read(struct perf_event *event) |
8718 | { | |
8719 | cpu_clock_event_update(event); | |
8720 | } | |
f344011c | 8721 | |
b0a873eb PZ |
8722 | static int cpu_clock_event_init(struct perf_event *event) |
8723 | { | |
8724 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
8725 | return -ENOENT; | |
8726 | ||
8727 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
8728 | return -ENOENT; | |
8729 | ||
2481c5fa SE |
8730 | /* |
8731 | * no branch sampling for software events | |
8732 | */ | |
8733 | if (has_branch_stack(event)) | |
8734 | return -EOPNOTSUPP; | |
8735 | ||
ba3dd36c PZ |
8736 | perf_swevent_init_hrtimer(event); |
8737 | ||
b0a873eb | 8738 | return 0; |
f29ac756 PZ |
8739 | } |
8740 | ||
b0a873eb | 8741 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
8742 | .task_ctx_nr = perf_sw_context, |
8743 | ||
34f43927 PZ |
8744 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8745 | ||
b0a873eb | 8746 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
8747 | .add = cpu_clock_event_add, |
8748 | .del = cpu_clock_event_del, | |
8749 | .start = cpu_clock_event_start, | |
8750 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
8751 | .read = cpu_clock_event_read, |
8752 | }; | |
8753 | ||
8754 | /* | |
8755 | * Software event: task time clock | |
8756 | */ | |
8757 | ||
8758 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 8759 | { |
b0a873eb PZ |
8760 | u64 prev; |
8761 | s64 delta; | |
5c92d124 | 8762 | |
b0a873eb PZ |
8763 | prev = local64_xchg(&event->hw.prev_count, now); |
8764 | delta = now - prev; | |
8765 | local64_add(delta, &event->count); | |
8766 | } | |
5c92d124 | 8767 | |
a4eaf7f1 | 8768 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8769 | { |
a4eaf7f1 | 8770 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 8771 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8772 | } |
8773 | ||
a4eaf7f1 | 8774 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
8775 | { |
8776 | perf_swevent_cancel_hrtimer(event); | |
8777 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
8778 | } |
8779 | ||
8780 | static int task_clock_event_add(struct perf_event *event, int flags) | |
8781 | { | |
8782 | if (flags & PERF_EF_START) | |
8783 | task_clock_event_start(event, flags); | |
6a694a60 | 8784 | perf_event_update_userpage(event); |
b0a873eb | 8785 | |
a4eaf7f1 PZ |
8786 | return 0; |
8787 | } | |
8788 | ||
8789 | static void task_clock_event_del(struct perf_event *event, int flags) | |
8790 | { | |
8791 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
8792 | } |
8793 | ||
8794 | static void task_clock_event_read(struct perf_event *event) | |
8795 | { | |
768a06e2 PZ |
8796 | u64 now = perf_clock(); |
8797 | u64 delta = now - event->ctx->timestamp; | |
8798 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
8799 | |
8800 | task_clock_event_update(event, time); | |
8801 | } | |
8802 | ||
8803 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 8804 | { |
b0a873eb PZ |
8805 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
8806 | return -ENOENT; | |
8807 | ||
8808 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
8809 | return -ENOENT; | |
8810 | ||
2481c5fa SE |
8811 | /* |
8812 | * no branch sampling for software events | |
8813 | */ | |
8814 | if (has_branch_stack(event)) | |
8815 | return -EOPNOTSUPP; | |
8816 | ||
ba3dd36c PZ |
8817 | perf_swevent_init_hrtimer(event); |
8818 | ||
b0a873eb | 8819 | return 0; |
6fb2915d LZ |
8820 | } |
8821 | ||
b0a873eb | 8822 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
8823 | .task_ctx_nr = perf_sw_context, |
8824 | ||
34f43927 PZ |
8825 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8826 | ||
b0a873eb | 8827 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
8828 | .add = task_clock_event_add, |
8829 | .del = task_clock_event_del, | |
8830 | .start = task_clock_event_start, | |
8831 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
8832 | .read = task_clock_event_read, |
8833 | }; | |
6fb2915d | 8834 | |
ad5133b7 | 8835 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 8836 | { |
e077df4f | 8837 | } |
6fb2915d | 8838 | |
fbbe0701 SB |
8839 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
8840 | { | |
8841 | } | |
8842 | ||
ad5133b7 | 8843 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 8844 | { |
ad5133b7 | 8845 | return 0; |
6fb2915d LZ |
8846 | } |
8847 | ||
18ab2cd3 | 8848 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
8849 | |
8850 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 8851 | { |
fbbe0701 SB |
8852 | __this_cpu_write(nop_txn_flags, flags); |
8853 | ||
8854 | if (flags & ~PERF_PMU_TXN_ADD) | |
8855 | return; | |
8856 | ||
ad5133b7 | 8857 | perf_pmu_disable(pmu); |
6fb2915d LZ |
8858 | } |
8859 | ||
ad5133b7 PZ |
8860 | static int perf_pmu_commit_txn(struct pmu *pmu) |
8861 | { | |
fbbe0701 SB |
8862 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8863 | ||
8864 | __this_cpu_write(nop_txn_flags, 0); | |
8865 | ||
8866 | if (flags & ~PERF_PMU_TXN_ADD) | |
8867 | return 0; | |
8868 | ||
ad5133b7 PZ |
8869 | perf_pmu_enable(pmu); |
8870 | return 0; | |
8871 | } | |
e077df4f | 8872 | |
ad5133b7 | 8873 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 8874 | { |
fbbe0701 SB |
8875 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8876 | ||
8877 | __this_cpu_write(nop_txn_flags, 0); | |
8878 | ||
8879 | if (flags & ~PERF_PMU_TXN_ADD) | |
8880 | return; | |
8881 | ||
ad5133b7 | 8882 | perf_pmu_enable(pmu); |
24f1e32c FW |
8883 | } |
8884 | ||
35edc2a5 PZ |
8885 | static int perf_event_idx_default(struct perf_event *event) |
8886 | { | |
c719f560 | 8887 | return 0; |
35edc2a5 PZ |
8888 | } |
8889 | ||
8dc85d54 PZ |
8890 | /* |
8891 | * Ensures all contexts with the same task_ctx_nr have the same | |
8892 | * pmu_cpu_context too. | |
8893 | */ | |
9e317041 | 8894 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 8895 | { |
8dc85d54 | 8896 | struct pmu *pmu; |
b326e956 | 8897 | |
8dc85d54 PZ |
8898 | if (ctxn < 0) |
8899 | return NULL; | |
24f1e32c | 8900 | |
8dc85d54 PZ |
8901 | list_for_each_entry(pmu, &pmus, entry) { |
8902 | if (pmu->task_ctx_nr == ctxn) | |
8903 | return pmu->pmu_cpu_context; | |
8904 | } | |
24f1e32c | 8905 | |
8dc85d54 | 8906 | return NULL; |
24f1e32c FW |
8907 | } |
8908 | ||
51676957 PZ |
8909 | static void free_pmu_context(struct pmu *pmu) |
8910 | { | |
df0062b2 WD |
8911 | /* |
8912 | * Static contexts such as perf_sw_context have a global lifetime | |
8913 | * and may be shared between different PMUs. Avoid freeing them | |
8914 | * when a single PMU is going away. | |
8915 | */ | |
8916 | if (pmu->task_ctx_nr > perf_invalid_context) | |
8917 | return; | |
8918 | ||
8dc85d54 | 8919 | mutex_lock(&pmus_lock); |
51676957 | 8920 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 | 8921 | mutex_unlock(&pmus_lock); |
24f1e32c | 8922 | } |
6e855cd4 AS |
8923 | |
8924 | /* | |
8925 | * Let userspace know that this PMU supports address range filtering: | |
8926 | */ | |
8927 | static ssize_t nr_addr_filters_show(struct device *dev, | |
8928 | struct device_attribute *attr, | |
8929 | char *page) | |
8930 | { | |
8931 | struct pmu *pmu = dev_get_drvdata(dev); | |
8932 | ||
8933 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
8934 | } | |
8935 | DEVICE_ATTR_RO(nr_addr_filters); | |
8936 | ||
2e80a82a | 8937 | static struct idr pmu_idr; |
d6d020e9 | 8938 | |
abe43400 PZ |
8939 | static ssize_t |
8940 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
8941 | { | |
8942 | struct pmu *pmu = dev_get_drvdata(dev); | |
8943 | ||
8944 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
8945 | } | |
90826ca7 | 8946 | static DEVICE_ATTR_RO(type); |
abe43400 | 8947 | |
62b85639 SE |
8948 | static ssize_t |
8949 | perf_event_mux_interval_ms_show(struct device *dev, | |
8950 | struct device_attribute *attr, | |
8951 | char *page) | |
8952 | { | |
8953 | struct pmu *pmu = dev_get_drvdata(dev); | |
8954 | ||
8955 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
8956 | } | |
8957 | ||
272325c4 PZ |
8958 | static DEFINE_MUTEX(mux_interval_mutex); |
8959 | ||
62b85639 SE |
8960 | static ssize_t |
8961 | perf_event_mux_interval_ms_store(struct device *dev, | |
8962 | struct device_attribute *attr, | |
8963 | const char *buf, size_t count) | |
8964 | { | |
8965 | struct pmu *pmu = dev_get_drvdata(dev); | |
8966 | int timer, cpu, ret; | |
8967 | ||
8968 | ret = kstrtoint(buf, 0, &timer); | |
8969 | if (ret) | |
8970 | return ret; | |
8971 | ||
8972 | if (timer < 1) | |
8973 | return -EINVAL; | |
8974 | ||
8975 | /* same value, noting to do */ | |
8976 | if (timer == pmu->hrtimer_interval_ms) | |
8977 | return count; | |
8978 | ||
272325c4 | 8979 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
8980 | pmu->hrtimer_interval_ms = timer; |
8981 | ||
8982 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 8983 | cpus_read_lock(); |
272325c4 | 8984 | for_each_online_cpu(cpu) { |
62b85639 SE |
8985 | struct perf_cpu_context *cpuctx; |
8986 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8987 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
8988 | ||
272325c4 PZ |
8989 | cpu_function_call(cpu, |
8990 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 8991 | } |
a63fbed7 | 8992 | cpus_read_unlock(); |
272325c4 | 8993 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
8994 | |
8995 | return count; | |
8996 | } | |
90826ca7 | 8997 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 8998 | |
90826ca7 GKH |
8999 | static struct attribute *pmu_dev_attrs[] = { |
9000 | &dev_attr_type.attr, | |
9001 | &dev_attr_perf_event_mux_interval_ms.attr, | |
9002 | NULL, | |
abe43400 | 9003 | }; |
90826ca7 | 9004 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
9005 | |
9006 | static int pmu_bus_running; | |
9007 | static struct bus_type pmu_bus = { | |
9008 | .name = "event_source", | |
90826ca7 | 9009 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
9010 | }; |
9011 | ||
9012 | static void pmu_dev_release(struct device *dev) | |
9013 | { | |
9014 | kfree(dev); | |
9015 | } | |
9016 | ||
9017 | static int pmu_dev_alloc(struct pmu *pmu) | |
9018 | { | |
9019 | int ret = -ENOMEM; | |
9020 | ||
9021 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
9022 | if (!pmu->dev) | |
9023 | goto out; | |
9024 | ||
0c9d42ed | 9025 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
9026 | device_initialize(pmu->dev); |
9027 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
9028 | if (ret) | |
9029 | goto free_dev; | |
9030 | ||
9031 | dev_set_drvdata(pmu->dev, pmu); | |
9032 | pmu->dev->bus = &pmu_bus; | |
9033 | pmu->dev->release = pmu_dev_release; | |
9034 | ret = device_add(pmu->dev); | |
9035 | if (ret) | |
9036 | goto free_dev; | |
9037 | ||
6e855cd4 AS |
9038 | /* For PMUs with address filters, throw in an extra attribute: */ |
9039 | if (pmu->nr_addr_filters) | |
9040 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9041 | ||
9042 | if (ret) | |
9043 | goto del_dev; | |
9044 | ||
abe43400 PZ |
9045 | out: |
9046 | return ret; | |
9047 | ||
6e855cd4 AS |
9048 | del_dev: |
9049 | device_del(pmu->dev); | |
9050 | ||
abe43400 PZ |
9051 | free_dev: |
9052 | put_device(pmu->dev); | |
9053 | goto out; | |
9054 | } | |
9055 | ||
547e9fd7 | 9056 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 9057 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 9058 | |
03d8e80b | 9059 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 9060 | { |
108b02cf | 9061 | int cpu, ret; |
24f1e32c | 9062 | |
b0a873eb | 9063 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
9064 | ret = -ENOMEM; |
9065 | pmu->pmu_disable_count = alloc_percpu(int); | |
9066 | if (!pmu->pmu_disable_count) | |
9067 | goto unlock; | |
f29ac756 | 9068 | |
2e80a82a PZ |
9069 | pmu->type = -1; |
9070 | if (!name) | |
9071 | goto skip_type; | |
9072 | pmu->name = name; | |
9073 | ||
9074 | if (type < 0) { | |
0e9c3be2 TH |
9075 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
9076 | if (type < 0) { | |
9077 | ret = type; | |
2e80a82a PZ |
9078 | goto free_pdc; |
9079 | } | |
9080 | } | |
9081 | pmu->type = type; | |
9082 | ||
abe43400 PZ |
9083 | if (pmu_bus_running) { |
9084 | ret = pmu_dev_alloc(pmu); | |
9085 | if (ret) | |
9086 | goto free_idr; | |
9087 | } | |
9088 | ||
2e80a82a | 9089 | skip_type: |
26657848 PZ |
9090 | if (pmu->task_ctx_nr == perf_hw_context) { |
9091 | static int hw_context_taken = 0; | |
9092 | ||
5101ef20 MR |
9093 | /* |
9094 | * Other than systems with heterogeneous CPUs, it never makes | |
9095 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
9096 | * uncore must use perf_invalid_context. | |
9097 | */ | |
9098 | if (WARN_ON_ONCE(hw_context_taken && | |
9099 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
9100 | pmu->task_ctx_nr = perf_invalid_context; |
9101 | ||
9102 | hw_context_taken = 1; | |
9103 | } | |
9104 | ||
8dc85d54 PZ |
9105 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
9106 | if (pmu->pmu_cpu_context) | |
9107 | goto got_cpu_context; | |
f29ac756 | 9108 | |
c4814202 | 9109 | ret = -ENOMEM; |
108b02cf PZ |
9110 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
9111 | if (!pmu->pmu_cpu_context) | |
abe43400 | 9112 | goto free_dev; |
f344011c | 9113 | |
108b02cf PZ |
9114 | for_each_possible_cpu(cpu) { |
9115 | struct perf_cpu_context *cpuctx; | |
9116 | ||
9117 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 9118 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 9119 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 9120 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 9121 | cpuctx->ctx.pmu = pmu; |
a63fbed7 | 9122 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); |
9e630205 | 9123 | |
272325c4 | 9124 | __perf_mux_hrtimer_init(cpuctx, cpu); |
108b02cf | 9125 | } |
76e1d904 | 9126 | |
8dc85d54 | 9127 | got_cpu_context: |
ad5133b7 PZ |
9128 | if (!pmu->start_txn) { |
9129 | if (pmu->pmu_enable) { | |
9130 | /* | |
9131 | * If we have pmu_enable/pmu_disable calls, install | |
9132 | * transaction stubs that use that to try and batch | |
9133 | * hardware accesses. | |
9134 | */ | |
9135 | pmu->start_txn = perf_pmu_start_txn; | |
9136 | pmu->commit_txn = perf_pmu_commit_txn; | |
9137 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
9138 | } else { | |
fbbe0701 | 9139 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
9140 | pmu->commit_txn = perf_pmu_nop_int; |
9141 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 9142 | } |
5c92d124 | 9143 | } |
15dbf27c | 9144 | |
ad5133b7 PZ |
9145 | if (!pmu->pmu_enable) { |
9146 | pmu->pmu_enable = perf_pmu_nop_void; | |
9147 | pmu->pmu_disable = perf_pmu_nop_void; | |
9148 | } | |
9149 | ||
35edc2a5 PZ |
9150 | if (!pmu->event_idx) |
9151 | pmu->event_idx = perf_event_idx_default; | |
9152 | ||
b0a873eb | 9153 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 9154 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
9155 | ret = 0; |
9156 | unlock: | |
b0a873eb PZ |
9157 | mutex_unlock(&pmus_lock); |
9158 | ||
33696fc0 | 9159 | return ret; |
108b02cf | 9160 | |
abe43400 PZ |
9161 | free_dev: |
9162 | device_del(pmu->dev); | |
9163 | put_device(pmu->dev); | |
9164 | ||
2e80a82a PZ |
9165 | free_idr: |
9166 | if (pmu->type >= PERF_TYPE_MAX) | |
9167 | idr_remove(&pmu_idr, pmu->type); | |
9168 | ||
108b02cf PZ |
9169 | free_pdc: |
9170 | free_percpu(pmu->pmu_disable_count); | |
9171 | goto unlock; | |
f29ac756 | 9172 | } |
c464c76e | 9173 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 9174 | |
b0a873eb | 9175 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 9176 | { |
0933840a JO |
9177 | int remove_device; |
9178 | ||
b0a873eb | 9179 | mutex_lock(&pmus_lock); |
0933840a | 9180 | remove_device = pmu_bus_running; |
b0a873eb PZ |
9181 | list_del_rcu(&pmu->entry); |
9182 | mutex_unlock(&pmus_lock); | |
5c92d124 | 9183 | |
0475f9ea | 9184 | /* |
cde8e884 PZ |
9185 | * We dereference the pmu list under both SRCU and regular RCU, so |
9186 | * synchronize against both of those. | |
0475f9ea | 9187 | */ |
b0a873eb | 9188 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 9189 | synchronize_rcu(); |
d6d020e9 | 9190 | |
33696fc0 | 9191 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
9192 | if (pmu->type >= PERF_TYPE_MAX) |
9193 | idr_remove(&pmu_idr, pmu->type); | |
0933840a JO |
9194 | if (remove_device) { |
9195 | if (pmu->nr_addr_filters) | |
9196 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9197 | device_del(pmu->dev); | |
9198 | put_device(pmu->dev); | |
9199 | } | |
51676957 | 9200 | free_pmu_context(pmu); |
b0a873eb | 9201 | } |
c464c76e | 9202 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 9203 | |
cc34b98b MR |
9204 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
9205 | { | |
ccd41c86 | 9206 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
9207 | int ret; |
9208 | ||
9209 | if (!try_module_get(pmu->module)) | |
9210 | return -ENODEV; | |
ccd41c86 | 9211 | |
0c7296ca PZ |
9212 | /* |
9213 | * A number of pmu->event_init() methods iterate the sibling_list to, | |
9214 | * for example, validate if the group fits on the PMU. Therefore, | |
9215 | * if this is a sibling event, acquire the ctx->mutex to protect | |
9216 | * the sibling_list. | |
9217 | */ | |
9218 | if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { | |
8b10c5e2 PZ |
9219 | /* |
9220 | * This ctx->mutex can nest when we're called through | |
9221 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
9222 | */ | |
9223 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
9224 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
9225 | BUG_ON(!ctx); |
9226 | } | |
9227 | ||
cc34b98b MR |
9228 | event->pmu = pmu; |
9229 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
9230 | |
9231 | if (ctx) | |
9232 | perf_event_ctx_unlock(event->group_leader, ctx); | |
9233 | ||
cc34b98b MR |
9234 | if (ret) |
9235 | module_put(pmu->module); | |
9236 | ||
9237 | return ret; | |
9238 | } | |
9239 | ||
18ab2cd3 | 9240 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 9241 | { |
85c617ab | 9242 | struct pmu *pmu; |
b0a873eb | 9243 | int idx; |
940c5b29 | 9244 | int ret; |
b0a873eb PZ |
9245 | |
9246 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 9247 | |
40999312 KL |
9248 | /* Try parent's PMU first: */ |
9249 | if (event->parent && event->parent->pmu) { | |
9250 | pmu = event->parent->pmu; | |
9251 | ret = perf_try_init_event(pmu, event); | |
9252 | if (!ret) | |
9253 | goto unlock; | |
9254 | } | |
9255 | ||
2e80a82a PZ |
9256 | rcu_read_lock(); |
9257 | pmu = idr_find(&pmu_idr, event->attr.type); | |
9258 | rcu_read_unlock(); | |
940c5b29 | 9259 | if (pmu) { |
cc34b98b | 9260 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
9261 | if (ret) |
9262 | pmu = ERR_PTR(ret); | |
2e80a82a | 9263 | goto unlock; |
940c5b29 | 9264 | } |
2e80a82a | 9265 | |
b0a873eb | 9266 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 9267 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 9268 | if (!ret) |
e5f4d339 | 9269 | goto unlock; |
76e1d904 | 9270 | |
b0a873eb PZ |
9271 | if (ret != -ENOENT) { |
9272 | pmu = ERR_PTR(ret); | |
e5f4d339 | 9273 | goto unlock; |
f344011c | 9274 | } |
5c92d124 | 9275 | } |
e5f4d339 PZ |
9276 | pmu = ERR_PTR(-ENOENT); |
9277 | unlock: | |
b0a873eb | 9278 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 9279 | |
4aeb0b42 | 9280 | return pmu; |
5c92d124 IM |
9281 | } |
9282 | ||
f2fb6bef KL |
9283 | static void attach_sb_event(struct perf_event *event) |
9284 | { | |
9285 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
9286 | ||
9287 | raw_spin_lock(&pel->lock); | |
9288 | list_add_rcu(&event->sb_list, &pel->list); | |
9289 | raw_spin_unlock(&pel->lock); | |
9290 | } | |
9291 | ||
aab5b71e PZ |
9292 | /* |
9293 | * We keep a list of all !task (and therefore per-cpu) events | |
9294 | * that need to receive side-band records. | |
9295 | * | |
9296 | * This avoids having to scan all the various PMU per-cpu contexts | |
9297 | * looking for them. | |
9298 | */ | |
f2fb6bef KL |
9299 | static void account_pmu_sb_event(struct perf_event *event) |
9300 | { | |
a4f144eb | 9301 | if (is_sb_event(event)) |
f2fb6bef KL |
9302 | attach_sb_event(event); |
9303 | } | |
9304 | ||
4beb31f3 FW |
9305 | static void account_event_cpu(struct perf_event *event, int cpu) |
9306 | { | |
9307 | if (event->parent) | |
9308 | return; | |
9309 | ||
4beb31f3 FW |
9310 | if (is_cgroup_event(event)) |
9311 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
9312 | } | |
9313 | ||
555e0c1e FW |
9314 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
9315 | static void account_freq_event_nohz(void) | |
9316 | { | |
9317 | #ifdef CONFIG_NO_HZ_FULL | |
9318 | /* Lock so we don't race with concurrent unaccount */ | |
9319 | spin_lock(&nr_freq_lock); | |
9320 | if (atomic_inc_return(&nr_freq_events) == 1) | |
9321 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
9322 | spin_unlock(&nr_freq_lock); | |
9323 | #endif | |
9324 | } | |
9325 | ||
9326 | static void account_freq_event(void) | |
9327 | { | |
9328 | if (tick_nohz_full_enabled()) | |
9329 | account_freq_event_nohz(); | |
9330 | else | |
9331 | atomic_inc(&nr_freq_events); | |
9332 | } | |
9333 | ||
9334 | ||
766d6c07 FW |
9335 | static void account_event(struct perf_event *event) |
9336 | { | |
25432ae9 PZ |
9337 | bool inc = false; |
9338 | ||
4beb31f3 FW |
9339 | if (event->parent) |
9340 | return; | |
9341 | ||
766d6c07 | 9342 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 9343 | inc = true; |
766d6c07 FW |
9344 | if (event->attr.mmap || event->attr.mmap_data) |
9345 | atomic_inc(&nr_mmap_events); | |
9346 | if (event->attr.comm) | |
9347 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
9348 | if (event->attr.namespaces) |
9349 | atomic_inc(&nr_namespaces_events); | |
766d6c07 FW |
9350 | if (event->attr.task) |
9351 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
9352 | if (event->attr.freq) |
9353 | account_freq_event(); | |
45ac1403 AH |
9354 | if (event->attr.context_switch) { |
9355 | atomic_inc(&nr_switch_events); | |
25432ae9 | 9356 | inc = true; |
45ac1403 | 9357 | } |
4beb31f3 | 9358 | if (has_branch_stack(event)) |
25432ae9 | 9359 | inc = true; |
4beb31f3 | 9360 | if (is_cgroup_event(event)) |
25432ae9 PZ |
9361 | inc = true; |
9362 | ||
9107c89e | 9363 | if (inc) { |
5bce9db1 AS |
9364 | /* |
9365 | * We need the mutex here because static_branch_enable() | |
9366 | * must complete *before* the perf_sched_count increment | |
9367 | * becomes visible. | |
9368 | */ | |
9107c89e PZ |
9369 | if (atomic_inc_not_zero(&perf_sched_count)) |
9370 | goto enabled; | |
9371 | ||
9372 | mutex_lock(&perf_sched_mutex); | |
9373 | if (!atomic_read(&perf_sched_count)) { | |
9374 | static_branch_enable(&perf_sched_events); | |
9375 | /* | |
9376 | * Guarantee that all CPUs observe they key change and | |
9377 | * call the perf scheduling hooks before proceeding to | |
9378 | * install events that need them. | |
9379 | */ | |
9380 | synchronize_sched(); | |
9381 | } | |
9382 | /* | |
9383 | * Now that we have waited for the sync_sched(), allow further | |
9384 | * increments to by-pass the mutex. | |
9385 | */ | |
9386 | atomic_inc(&perf_sched_count); | |
9387 | mutex_unlock(&perf_sched_mutex); | |
9388 | } | |
9389 | enabled: | |
4beb31f3 FW |
9390 | |
9391 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
9392 | |
9393 | account_pmu_sb_event(event); | |
766d6c07 FW |
9394 | } |
9395 | ||
0793a61d | 9396 | /* |
cdd6c482 | 9397 | * Allocate and initialize a event structure |
0793a61d | 9398 | */ |
cdd6c482 | 9399 | static struct perf_event * |
c3f00c70 | 9400 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
9401 | struct task_struct *task, |
9402 | struct perf_event *group_leader, | |
9403 | struct perf_event *parent_event, | |
4dc0da86 | 9404 | perf_overflow_handler_t overflow_handler, |
79dff51e | 9405 | void *context, int cgroup_fd) |
0793a61d | 9406 | { |
51b0fe39 | 9407 | struct pmu *pmu; |
cdd6c482 IM |
9408 | struct perf_event *event; |
9409 | struct hw_perf_event *hwc; | |
90983b16 | 9410 | long err = -EINVAL; |
0793a61d | 9411 | |
66832eb4 ON |
9412 | if ((unsigned)cpu >= nr_cpu_ids) { |
9413 | if (!task || cpu != -1) | |
9414 | return ERR_PTR(-EINVAL); | |
9415 | } | |
9416 | ||
c3f00c70 | 9417 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 9418 | if (!event) |
d5d2bc0d | 9419 | return ERR_PTR(-ENOMEM); |
0793a61d | 9420 | |
04289bb9 | 9421 | /* |
cdd6c482 | 9422 | * Single events are their own group leaders, with an |
04289bb9 IM |
9423 | * empty sibling list: |
9424 | */ | |
9425 | if (!group_leader) | |
cdd6c482 | 9426 | group_leader = event; |
04289bb9 | 9427 | |
cdd6c482 IM |
9428 | mutex_init(&event->child_mutex); |
9429 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 9430 | |
cdd6c482 IM |
9431 | INIT_LIST_HEAD(&event->group_entry); |
9432 | INIT_LIST_HEAD(&event->event_entry); | |
9433 | INIT_LIST_HEAD(&event->sibling_list); | |
10c6db11 | 9434 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 9435 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 9436 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
9437 | INIT_HLIST_NODE(&event->hlist_entry); |
9438 | ||
10c6db11 | 9439 | |
cdd6c482 | 9440 | init_waitqueue_head(&event->waitq); |
e360adbe | 9441 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 9442 | |
cdd6c482 | 9443 | mutex_init(&event->mmap_mutex); |
375637bc | 9444 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 9445 | |
a6fa941d | 9446 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
9447 | event->cpu = cpu; |
9448 | event->attr = *attr; | |
9449 | event->group_leader = group_leader; | |
9450 | event->pmu = NULL; | |
cdd6c482 | 9451 | event->oncpu = -1; |
a96bbc16 | 9452 | |
cdd6c482 | 9453 | event->parent = parent_event; |
b84fbc9f | 9454 | |
17cf22c3 | 9455 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 9456 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 9457 | |
cdd6c482 | 9458 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 9459 | |
d580ff86 PZ |
9460 | if (task) { |
9461 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 9462 | /* |
50f16a8b PZ |
9463 | * XXX pmu::event_init needs to know what task to account to |
9464 | * and we cannot use the ctx information because we need the | |
9465 | * pmu before we get a ctx. | |
d580ff86 | 9466 | */ |
ae162419 | 9467 | get_task_struct(task); |
50f16a8b | 9468 | event->hw.target = task; |
d580ff86 PZ |
9469 | } |
9470 | ||
34f43927 PZ |
9471 | event->clock = &local_clock; |
9472 | if (parent_event) | |
9473 | event->clock = parent_event->clock; | |
9474 | ||
4dc0da86 | 9475 | if (!overflow_handler && parent_event) { |
b326e956 | 9476 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 9477 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 9478 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c AS |
9479 | if (overflow_handler == bpf_overflow_handler) { |
9480 | struct bpf_prog *prog = bpf_prog_inc(parent_event->prog); | |
9481 | ||
9482 | if (IS_ERR(prog)) { | |
9483 | err = PTR_ERR(prog); | |
9484 | goto err_ns; | |
9485 | } | |
9486 | event->prog = prog; | |
9487 | event->orig_overflow_handler = | |
9488 | parent_event->orig_overflow_handler; | |
9489 | } | |
9490 | #endif | |
4dc0da86 | 9491 | } |
66832eb4 | 9492 | |
1879445d WN |
9493 | if (overflow_handler) { |
9494 | event->overflow_handler = overflow_handler; | |
9495 | event->overflow_handler_context = context; | |
9ecda41a WN |
9496 | } else if (is_write_backward(event)){ |
9497 | event->overflow_handler = perf_event_output_backward; | |
9498 | event->overflow_handler_context = NULL; | |
1879445d | 9499 | } else { |
9ecda41a | 9500 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
9501 | event->overflow_handler_context = NULL; |
9502 | } | |
97eaf530 | 9503 | |
0231bb53 | 9504 | perf_event__state_init(event); |
a86ed508 | 9505 | |
4aeb0b42 | 9506 | pmu = NULL; |
b8e83514 | 9507 | |
cdd6c482 | 9508 | hwc = &event->hw; |
bd2b5b12 | 9509 | hwc->sample_period = attr->sample_period; |
0d48696f | 9510 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 9511 | hwc->sample_period = 1; |
eced1dfc | 9512 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 9513 | |
e7850595 | 9514 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 9515 | |
2023b359 | 9516 | /* |
ba5213ae PZ |
9517 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
9518 | * See perf_output_read(). | |
2023b359 | 9519 | */ |
ba5213ae | 9520 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 9521 | goto err_ns; |
a46a2300 YZ |
9522 | |
9523 | if (!has_branch_stack(event)) | |
9524 | event->attr.branch_sample_type = 0; | |
2023b359 | 9525 | |
79dff51e MF |
9526 | if (cgroup_fd != -1) { |
9527 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
9528 | if (err) | |
9529 | goto err_ns; | |
9530 | } | |
9531 | ||
b0a873eb | 9532 | pmu = perf_init_event(event); |
85c617ab | 9533 | if (IS_ERR(pmu)) { |
4aeb0b42 | 9534 | err = PTR_ERR(pmu); |
90983b16 | 9535 | goto err_ns; |
621a01ea | 9536 | } |
d5d2bc0d | 9537 | |
bed5b25a AS |
9538 | err = exclusive_event_init(event); |
9539 | if (err) | |
9540 | goto err_pmu; | |
9541 | ||
375637bc AS |
9542 | if (has_addr_filter(event)) { |
9543 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, | |
9544 | sizeof(unsigned long), | |
9545 | GFP_KERNEL); | |
36cc2b92 DC |
9546 | if (!event->addr_filters_offs) { |
9547 | err = -ENOMEM; | |
375637bc | 9548 | goto err_per_task; |
36cc2b92 | 9549 | } |
375637bc AS |
9550 | |
9551 | /* force hw sync on the address filters */ | |
9552 | event->addr_filters_gen = 1; | |
9553 | } | |
9554 | ||
cdd6c482 | 9555 | if (!event->parent) { |
927c7a9e | 9556 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 9557 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 9558 | if (err) |
375637bc | 9559 | goto err_addr_filters; |
d010b332 | 9560 | } |
f344011c | 9561 | } |
9ee318a7 | 9562 | |
927a5570 AS |
9563 | /* symmetric to unaccount_event() in _free_event() */ |
9564 | account_event(event); | |
9565 | ||
cdd6c482 | 9566 | return event; |
90983b16 | 9567 | |
375637bc AS |
9568 | err_addr_filters: |
9569 | kfree(event->addr_filters_offs); | |
9570 | ||
bed5b25a AS |
9571 | err_per_task: |
9572 | exclusive_event_destroy(event); | |
9573 | ||
90983b16 FW |
9574 | err_pmu: |
9575 | if (event->destroy) | |
9576 | event->destroy(event); | |
c464c76e | 9577 | module_put(pmu->module); |
90983b16 | 9578 | err_ns: |
79dff51e MF |
9579 | if (is_cgroup_event(event)) |
9580 | perf_detach_cgroup(event); | |
90983b16 FW |
9581 | if (event->ns) |
9582 | put_pid_ns(event->ns); | |
ae162419 PB |
9583 | if (event->hw.target) |
9584 | put_task_struct(event->hw.target); | |
90983b16 FW |
9585 | kfree(event); |
9586 | ||
9587 | return ERR_PTR(err); | |
0793a61d TG |
9588 | } |
9589 | ||
cdd6c482 IM |
9590 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
9591 | struct perf_event_attr *attr) | |
974802ea | 9592 | { |
974802ea | 9593 | u32 size; |
cdf8073d | 9594 | int ret; |
974802ea PZ |
9595 | |
9596 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
9597 | return -EFAULT; | |
9598 | ||
9599 | /* | |
9600 | * zero the full structure, so that a short copy will be nice. | |
9601 | */ | |
9602 | memset(attr, 0, sizeof(*attr)); | |
9603 | ||
9604 | ret = get_user(size, &uattr->size); | |
9605 | if (ret) | |
9606 | return ret; | |
9607 | ||
9608 | if (size > PAGE_SIZE) /* silly large */ | |
9609 | goto err_size; | |
9610 | ||
9611 | if (!size) /* abi compat */ | |
9612 | size = PERF_ATTR_SIZE_VER0; | |
9613 | ||
9614 | if (size < PERF_ATTR_SIZE_VER0) | |
9615 | goto err_size; | |
9616 | ||
9617 | /* | |
9618 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
9619 | * ensure all the unknown bits are 0 - i.e. new |
9620 | * user-space does not rely on any kernel feature | |
9621 | * extensions we dont know about yet. | |
974802ea PZ |
9622 | */ |
9623 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
9624 | unsigned char __user *addr; |
9625 | unsigned char __user *end; | |
9626 | unsigned char val; | |
974802ea | 9627 | |
cdf8073d IS |
9628 | addr = (void __user *)uattr + sizeof(*attr); |
9629 | end = (void __user *)uattr + size; | |
974802ea | 9630 | |
cdf8073d | 9631 | for (; addr < end; addr++) { |
974802ea PZ |
9632 | ret = get_user(val, addr); |
9633 | if (ret) | |
9634 | return ret; | |
9635 | if (val) | |
9636 | goto err_size; | |
9637 | } | |
b3e62e35 | 9638 | size = sizeof(*attr); |
974802ea PZ |
9639 | } |
9640 | ||
9641 | ret = copy_from_user(attr, uattr, size); | |
9642 | if (ret) | |
9643 | return -EFAULT; | |
9644 | ||
f12f42ac MX |
9645 | attr->size = size; |
9646 | ||
cd757645 | 9647 | if (attr->__reserved_1) |
974802ea PZ |
9648 | return -EINVAL; |
9649 | ||
9650 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
9651 | return -EINVAL; | |
9652 | ||
9653 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
9654 | return -EINVAL; | |
9655 | ||
bce38cd5 SE |
9656 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
9657 | u64 mask = attr->branch_sample_type; | |
9658 | ||
9659 | /* only using defined bits */ | |
9660 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
9661 | return -EINVAL; | |
9662 | ||
9663 | /* at least one branch bit must be set */ | |
9664 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
9665 | return -EINVAL; | |
9666 | ||
bce38cd5 SE |
9667 | /* propagate priv level, when not set for branch */ |
9668 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
9669 | ||
9670 | /* exclude_kernel checked on syscall entry */ | |
9671 | if (!attr->exclude_kernel) | |
9672 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
9673 | ||
9674 | if (!attr->exclude_user) | |
9675 | mask |= PERF_SAMPLE_BRANCH_USER; | |
9676 | ||
9677 | if (!attr->exclude_hv) | |
9678 | mask |= PERF_SAMPLE_BRANCH_HV; | |
9679 | /* | |
9680 | * adjust user setting (for HW filter setup) | |
9681 | */ | |
9682 | attr->branch_sample_type = mask; | |
9683 | } | |
e712209a SE |
9684 | /* privileged levels capture (kernel, hv): check permissions */ |
9685 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
9686 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
9687 | return -EACCES; | |
bce38cd5 | 9688 | } |
4018994f | 9689 | |
c5ebcedb | 9690 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 9691 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
9692 | if (ret) |
9693 | return ret; | |
9694 | } | |
9695 | ||
9696 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
9697 | if (!arch_perf_have_user_stack_dump()) | |
9698 | return -ENOSYS; | |
9699 | ||
9700 | /* | |
9701 | * We have __u32 type for the size, but so far | |
9702 | * we can only use __u16 as maximum due to the | |
9703 | * __u16 sample size limit. | |
9704 | */ | |
9705 | if (attr->sample_stack_user >= USHRT_MAX) | |
0b105afa | 9706 | return -EINVAL; |
c5ebcedb | 9707 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
0b105afa | 9708 | return -EINVAL; |
c5ebcedb | 9709 | } |
4018994f | 9710 | |
60e2364e SE |
9711 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
9712 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
9713 | out: |
9714 | return ret; | |
9715 | ||
9716 | err_size: | |
9717 | put_user(sizeof(*attr), &uattr->size); | |
9718 | ret = -E2BIG; | |
9719 | goto out; | |
9720 | } | |
9721 | ||
ac9721f3 PZ |
9722 | static int |
9723 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 9724 | { |
b69cf536 | 9725 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
9726 | int ret = -EINVAL; |
9727 | ||
ac9721f3 | 9728 | if (!output_event) |
a4be7c27 PZ |
9729 | goto set; |
9730 | ||
ac9721f3 PZ |
9731 | /* don't allow circular references */ |
9732 | if (event == output_event) | |
a4be7c27 PZ |
9733 | goto out; |
9734 | ||
0f139300 PZ |
9735 | /* |
9736 | * Don't allow cross-cpu buffers | |
9737 | */ | |
9738 | if (output_event->cpu != event->cpu) | |
9739 | goto out; | |
9740 | ||
9741 | /* | |
76369139 | 9742 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
9743 | */ |
9744 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
9745 | goto out; | |
9746 | ||
34f43927 PZ |
9747 | /* |
9748 | * Mixing clocks in the same buffer is trouble you don't need. | |
9749 | */ | |
9750 | if (output_event->clock != event->clock) | |
9751 | goto out; | |
9752 | ||
9ecda41a WN |
9753 | /* |
9754 | * Either writing ring buffer from beginning or from end. | |
9755 | * Mixing is not allowed. | |
9756 | */ | |
9757 | if (is_write_backward(output_event) != is_write_backward(event)) | |
9758 | goto out; | |
9759 | ||
45bfb2e5 PZ |
9760 | /* |
9761 | * If both events generate aux data, they must be on the same PMU | |
9762 | */ | |
9763 | if (has_aux(event) && has_aux(output_event) && | |
9764 | event->pmu != output_event->pmu) | |
9765 | goto out; | |
9766 | ||
a4be7c27 | 9767 | set: |
cdd6c482 | 9768 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
9769 | /* Can't redirect output if we've got an active mmap() */ |
9770 | if (atomic_read(&event->mmap_count)) | |
9771 | goto unlock; | |
a4be7c27 | 9772 | |
ac9721f3 | 9773 | if (output_event) { |
76369139 FW |
9774 | /* get the rb we want to redirect to */ |
9775 | rb = ring_buffer_get(output_event); | |
9776 | if (!rb) | |
ac9721f3 | 9777 | goto unlock; |
a4be7c27 PZ |
9778 | } |
9779 | ||
b69cf536 | 9780 | ring_buffer_attach(event, rb); |
9bb5d40c | 9781 | |
a4be7c27 | 9782 | ret = 0; |
ac9721f3 PZ |
9783 | unlock: |
9784 | mutex_unlock(&event->mmap_mutex); | |
9785 | ||
a4be7c27 | 9786 | out: |
a4be7c27 PZ |
9787 | return ret; |
9788 | } | |
9789 | ||
f63a8daa PZ |
9790 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
9791 | { | |
9792 | if (b < a) | |
9793 | swap(a, b); | |
9794 | ||
9795 | mutex_lock(a); | |
9796 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
9797 | } | |
9798 | ||
34f43927 PZ |
9799 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
9800 | { | |
9801 | bool nmi_safe = false; | |
9802 | ||
9803 | switch (clk_id) { | |
9804 | case CLOCK_MONOTONIC: | |
9805 | event->clock = &ktime_get_mono_fast_ns; | |
9806 | nmi_safe = true; | |
9807 | break; | |
9808 | ||
9809 | case CLOCK_MONOTONIC_RAW: | |
9810 | event->clock = &ktime_get_raw_fast_ns; | |
9811 | nmi_safe = true; | |
9812 | break; | |
9813 | ||
9814 | case CLOCK_REALTIME: | |
9815 | event->clock = &ktime_get_real_ns; | |
9816 | break; | |
9817 | ||
9818 | case CLOCK_BOOTTIME: | |
9819 | event->clock = &ktime_get_boot_ns; | |
9820 | break; | |
9821 | ||
9822 | case CLOCK_TAI: | |
9823 | event->clock = &ktime_get_tai_ns; | |
9824 | break; | |
9825 | ||
9826 | default: | |
9827 | return -EINVAL; | |
9828 | } | |
9829 | ||
9830 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
9831 | return -EINVAL; | |
9832 | ||
9833 | return 0; | |
9834 | } | |
9835 | ||
321027c1 PZ |
9836 | /* |
9837 | * Variation on perf_event_ctx_lock_nested(), except we take two context | |
9838 | * mutexes. | |
9839 | */ | |
9840 | static struct perf_event_context * | |
9841 | __perf_event_ctx_lock_double(struct perf_event *group_leader, | |
9842 | struct perf_event_context *ctx) | |
9843 | { | |
9844 | struct perf_event_context *gctx; | |
9845 | ||
9846 | again: | |
9847 | rcu_read_lock(); | |
9848 | gctx = READ_ONCE(group_leader->ctx); | |
9849 | if (!atomic_inc_not_zero(&gctx->refcount)) { | |
9850 | rcu_read_unlock(); | |
9851 | goto again; | |
9852 | } | |
9853 | rcu_read_unlock(); | |
9854 | ||
9855 | mutex_lock_double(&gctx->mutex, &ctx->mutex); | |
9856 | ||
9857 | if (group_leader->ctx != gctx) { | |
9858 | mutex_unlock(&ctx->mutex); | |
9859 | mutex_unlock(&gctx->mutex); | |
9860 | put_ctx(gctx); | |
9861 | goto again; | |
9862 | } | |
9863 | ||
9864 | return gctx; | |
9865 | } | |
9866 | ||
0793a61d | 9867 | /** |
cdd6c482 | 9868 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 9869 | * |
cdd6c482 | 9870 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 9871 | * @pid: target pid |
9f66a381 | 9872 | * @cpu: target cpu |
cdd6c482 | 9873 | * @group_fd: group leader event fd |
0793a61d | 9874 | */ |
cdd6c482 IM |
9875 | SYSCALL_DEFINE5(perf_event_open, |
9876 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 9877 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 9878 | { |
b04243ef PZ |
9879 | struct perf_event *group_leader = NULL, *output_event = NULL; |
9880 | struct perf_event *event, *sibling; | |
cdd6c482 | 9881 | struct perf_event_attr attr; |
f63a8daa | 9882 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 9883 | struct file *event_file = NULL; |
2903ff01 | 9884 | struct fd group = {NULL, 0}; |
38a81da2 | 9885 | struct task_struct *task = NULL; |
89a1e187 | 9886 | struct pmu *pmu; |
ea635c64 | 9887 | int event_fd; |
b04243ef | 9888 | int move_group = 0; |
dc86cabe | 9889 | int err; |
a21b0b35 | 9890 | int f_flags = O_RDWR; |
79dff51e | 9891 | int cgroup_fd = -1; |
0793a61d | 9892 | |
2743a5b0 | 9893 | /* for future expandability... */ |
e5d1367f | 9894 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
9895 | return -EINVAL; |
9896 | ||
d48d0c64 BH |
9897 | if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN)) |
9898 | return -EACCES; | |
9899 | ||
dc86cabe IM |
9900 | err = perf_copy_attr(attr_uptr, &attr); |
9901 | if (err) | |
9902 | return err; | |
eab656ae | 9903 | |
0764771d PZ |
9904 | if (!attr.exclude_kernel) { |
9905 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
9906 | return -EACCES; | |
9907 | } | |
9908 | ||
e4222673 HB |
9909 | if (attr.namespaces) { |
9910 | if (!capable(CAP_SYS_ADMIN)) | |
9911 | return -EACCES; | |
9912 | } | |
9913 | ||
df58ab24 | 9914 | if (attr.freq) { |
cdd6c482 | 9915 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 9916 | return -EINVAL; |
0819b2e3 PZ |
9917 | } else { |
9918 | if (attr.sample_period & (1ULL << 63)) | |
9919 | return -EINVAL; | |
df58ab24 PZ |
9920 | } |
9921 | ||
fc7ce9c7 KL |
9922 | /* Only privileged users can get physical addresses */ |
9923 | if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) && | |
9924 | perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
9925 | return -EACCES; | |
9926 | ||
97c79a38 ACM |
9927 | if (!attr.sample_max_stack) |
9928 | attr.sample_max_stack = sysctl_perf_event_max_stack; | |
9929 | ||
e5d1367f SE |
9930 | /* |
9931 | * In cgroup mode, the pid argument is used to pass the fd | |
9932 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
9933 | * designates the cpu on which to monitor threads from that | |
9934 | * cgroup. | |
9935 | */ | |
9936 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
9937 | return -EINVAL; | |
9938 | ||
a21b0b35 YD |
9939 | if (flags & PERF_FLAG_FD_CLOEXEC) |
9940 | f_flags |= O_CLOEXEC; | |
9941 | ||
9942 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
9943 | if (event_fd < 0) |
9944 | return event_fd; | |
9945 | ||
ac9721f3 | 9946 | if (group_fd != -1) { |
2903ff01 AV |
9947 | err = perf_fget_light(group_fd, &group); |
9948 | if (err) | |
d14b12d7 | 9949 | goto err_fd; |
2903ff01 | 9950 | group_leader = group.file->private_data; |
ac9721f3 PZ |
9951 | if (flags & PERF_FLAG_FD_OUTPUT) |
9952 | output_event = group_leader; | |
9953 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
9954 | group_leader = NULL; | |
9955 | } | |
9956 | ||
e5d1367f | 9957 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
9958 | task = find_lively_task_by_vpid(pid); |
9959 | if (IS_ERR(task)) { | |
9960 | err = PTR_ERR(task); | |
9961 | goto err_group_fd; | |
9962 | } | |
9963 | } | |
9964 | ||
1f4ee503 PZ |
9965 | if (task && group_leader && |
9966 | group_leader->attr.inherit != attr.inherit) { | |
9967 | err = -EINVAL; | |
9968 | goto err_task; | |
9969 | } | |
9970 | ||
79c9ce57 PZ |
9971 | if (task) { |
9972 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); | |
9973 | if (err) | |
e5aeee51 | 9974 | goto err_task; |
79c9ce57 PZ |
9975 | |
9976 | /* | |
9977 | * Reuse ptrace permission checks for now. | |
9978 | * | |
9979 | * We must hold cred_guard_mutex across this and any potential | |
9980 | * perf_install_in_context() call for this new event to | |
9981 | * serialize against exec() altering our credentials (and the | |
9982 | * perf_event_exit_task() that could imply). | |
9983 | */ | |
9984 | err = -EACCES; | |
9985 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
9986 | goto err_cred; | |
9987 | } | |
9988 | ||
79dff51e MF |
9989 | if (flags & PERF_FLAG_PID_CGROUP) |
9990 | cgroup_fd = pid; | |
9991 | ||
4dc0da86 | 9992 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 9993 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
9994 | if (IS_ERR(event)) { |
9995 | err = PTR_ERR(event); | |
79c9ce57 | 9996 | goto err_cred; |
d14b12d7 SE |
9997 | } |
9998 | ||
53b25335 VW |
9999 | if (is_sampling_event(event)) { |
10000 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 10001 | err = -EOPNOTSUPP; |
53b25335 VW |
10002 | goto err_alloc; |
10003 | } | |
10004 | } | |
10005 | ||
89a1e187 PZ |
10006 | /* |
10007 | * Special case software events and allow them to be part of | |
10008 | * any hardware group. | |
10009 | */ | |
10010 | pmu = event->pmu; | |
b04243ef | 10011 | |
34f43927 PZ |
10012 | if (attr.use_clockid) { |
10013 | err = perf_event_set_clock(event, attr.clockid); | |
10014 | if (err) | |
10015 | goto err_alloc; | |
10016 | } | |
10017 | ||
4ff6a8de DCC |
10018 | if (pmu->task_ctx_nr == perf_sw_context) |
10019 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
10020 | ||
b04243ef PZ |
10021 | if (group_leader && |
10022 | (is_software_event(event) != is_software_event(group_leader))) { | |
10023 | if (is_software_event(event)) { | |
10024 | /* | |
10025 | * If event and group_leader are not both a software | |
10026 | * event, and event is, then group leader is not. | |
10027 | * | |
10028 | * Allow the addition of software events to !software | |
10029 | * groups, this is safe because software events never | |
10030 | * fail to schedule. | |
10031 | */ | |
10032 | pmu = group_leader->pmu; | |
10033 | } else if (is_software_event(group_leader) && | |
4ff6a8de | 10034 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
b04243ef PZ |
10035 | /* |
10036 | * In case the group is a pure software group, and we | |
10037 | * try to add a hardware event, move the whole group to | |
10038 | * the hardware context. | |
10039 | */ | |
10040 | move_group = 1; | |
10041 | } | |
10042 | } | |
89a1e187 PZ |
10043 | |
10044 | /* | |
10045 | * Get the target context (task or percpu): | |
10046 | */ | |
4af57ef2 | 10047 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
10048 | if (IS_ERR(ctx)) { |
10049 | err = PTR_ERR(ctx); | |
c6be5a5c | 10050 | goto err_alloc; |
89a1e187 PZ |
10051 | } |
10052 | ||
bed5b25a AS |
10053 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
10054 | err = -EBUSY; | |
10055 | goto err_context; | |
10056 | } | |
10057 | ||
ccff286d | 10058 | /* |
cdd6c482 | 10059 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 10060 | */ |
ac9721f3 | 10061 | if (group_leader) { |
dc86cabe | 10062 | err = -EINVAL; |
04289bb9 | 10063 | |
04289bb9 | 10064 | /* |
ccff286d IM |
10065 | * Do not allow a recursive hierarchy (this new sibling |
10066 | * becoming part of another group-sibling): | |
10067 | */ | |
10068 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 10069 | goto err_context; |
34f43927 PZ |
10070 | |
10071 | /* All events in a group should have the same clock */ | |
10072 | if (group_leader->clock != event->clock) | |
10073 | goto err_context; | |
10074 | ||
ccff286d | 10075 | /* |
64aee2a9 MR |
10076 | * Make sure we're both events for the same CPU; |
10077 | * grouping events for different CPUs is broken; since | |
10078 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 10079 | */ |
64aee2a9 MR |
10080 | if (group_leader->cpu != event->cpu) |
10081 | goto err_context; | |
c3c87e77 | 10082 | |
64aee2a9 MR |
10083 | /* |
10084 | * Make sure we're both on the same task, or both | |
10085 | * per-CPU events. | |
10086 | */ | |
10087 | if (group_leader->ctx->task != ctx->task) | |
10088 | goto err_context; | |
10089 | ||
10090 | /* | |
10091 | * Do not allow to attach to a group in a different task | |
10092 | * or CPU context. If we're moving SW events, we'll fix | |
10093 | * this up later, so allow that. | |
10094 | */ | |
10095 | if (!move_group && group_leader->ctx != ctx) | |
10096 | goto err_context; | |
b04243ef | 10097 | |
3b6f9e5c PM |
10098 | /* |
10099 | * Only a group leader can be exclusive or pinned | |
10100 | */ | |
0d48696f | 10101 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 10102 | goto err_context; |
ac9721f3 PZ |
10103 | } |
10104 | ||
10105 | if (output_event) { | |
10106 | err = perf_event_set_output(event, output_event); | |
10107 | if (err) | |
c3f00c70 | 10108 | goto err_context; |
ac9721f3 | 10109 | } |
0793a61d | 10110 | |
a21b0b35 YD |
10111 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
10112 | f_flags); | |
ea635c64 AV |
10113 | if (IS_ERR(event_file)) { |
10114 | err = PTR_ERR(event_file); | |
201c2f85 | 10115 | event_file = NULL; |
c3f00c70 | 10116 | goto err_context; |
ea635c64 | 10117 | } |
9b51f66d | 10118 | |
b04243ef | 10119 | if (move_group) { |
321027c1 PZ |
10120 | gctx = __perf_event_ctx_lock_double(group_leader, ctx); |
10121 | ||
84c4e620 PZ |
10122 | if (gctx->task == TASK_TOMBSTONE) { |
10123 | err = -ESRCH; | |
10124 | goto err_locked; | |
10125 | } | |
321027c1 PZ |
10126 | |
10127 | /* | |
10128 | * Check if we raced against another sys_perf_event_open() call | |
10129 | * moving the software group underneath us. | |
10130 | */ | |
10131 | if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
10132 | /* | |
10133 | * If someone moved the group out from under us, check | |
10134 | * if this new event wound up on the same ctx, if so | |
10135 | * its the regular !move_group case, otherwise fail. | |
10136 | */ | |
10137 | if (gctx != ctx) { | |
10138 | err = -EINVAL; | |
10139 | goto err_locked; | |
10140 | } else { | |
10141 | perf_event_ctx_unlock(group_leader, gctx); | |
10142 | move_group = 0; | |
10143 | } | |
10144 | } | |
f55fc2a5 PZ |
10145 | } else { |
10146 | mutex_lock(&ctx->mutex); | |
10147 | } | |
10148 | ||
84c4e620 PZ |
10149 | if (ctx->task == TASK_TOMBSTONE) { |
10150 | err = -ESRCH; | |
10151 | goto err_locked; | |
10152 | } | |
10153 | ||
a723968c PZ |
10154 | if (!perf_event_validate_size(event)) { |
10155 | err = -E2BIG; | |
10156 | goto err_locked; | |
10157 | } | |
10158 | ||
a63fbed7 TG |
10159 | if (!task) { |
10160 | /* | |
10161 | * Check if the @cpu we're creating an event for is online. | |
10162 | * | |
10163 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10164 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10165 | */ | |
10166 | struct perf_cpu_context *cpuctx = | |
10167 | container_of(ctx, struct perf_cpu_context, ctx); | |
10168 | ||
10169 | if (!cpuctx->online) { | |
10170 | err = -ENODEV; | |
10171 | goto err_locked; | |
10172 | } | |
10173 | } | |
10174 | ||
10175 | ||
f55fc2a5 PZ |
10176 | /* |
10177 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
10178 | * because we need to serialize with concurrent event creation. | |
10179 | */ | |
10180 | if (!exclusive_event_installable(event, ctx)) { | |
10181 | /* exclusive and group stuff are assumed mutually exclusive */ | |
10182 | WARN_ON_ONCE(move_group); | |
f63a8daa | 10183 | |
f55fc2a5 PZ |
10184 | err = -EBUSY; |
10185 | goto err_locked; | |
10186 | } | |
f63a8daa | 10187 | |
f55fc2a5 PZ |
10188 | WARN_ON_ONCE(ctx->parent_ctx); |
10189 | ||
79c9ce57 PZ |
10190 | /* |
10191 | * This is the point on no return; we cannot fail hereafter. This is | |
10192 | * where we start modifying current state. | |
10193 | */ | |
10194 | ||
f55fc2a5 | 10195 | if (move_group) { |
f63a8daa PZ |
10196 | /* |
10197 | * See perf_event_ctx_lock() for comments on the details | |
10198 | * of swizzling perf_event::ctx. | |
10199 | */ | |
45a0e07a | 10200 | perf_remove_from_context(group_leader, 0); |
279b5165 | 10201 | put_ctx(gctx); |
0231bb53 | 10202 | |
b04243ef PZ |
10203 | list_for_each_entry(sibling, &group_leader->sibling_list, |
10204 | group_entry) { | |
45a0e07a | 10205 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
10206 | put_ctx(gctx); |
10207 | } | |
b04243ef | 10208 | |
f63a8daa PZ |
10209 | /* |
10210 | * Wait for everybody to stop referencing the events through | |
10211 | * the old lists, before installing it on new lists. | |
10212 | */ | |
0cda4c02 | 10213 | synchronize_rcu(); |
f63a8daa | 10214 | |
8f95b435 PZI |
10215 | /* |
10216 | * Install the group siblings before the group leader. | |
10217 | * | |
10218 | * Because a group leader will try and install the entire group | |
10219 | * (through the sibling list, which is still in-tact), we can | |
10220 | * end up with siblings installed in the wrong context. | |
10221 | * | |
10222 | * By installing siblings first we NO-OP because they're not | |
10223 | * reachable through the group lists. | |
10224 | */ | |
b04243ef PZ |
10225 | list_for_each_entry(sibling, &group_leader->sibling_list, |
10226 | group_entry) { | |
8f95b435 | 10227 | perf_event__state_init(sibling); |
9fc81d87 | 10228 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
10229 | get_ctx(ctx); |
10230 | } | |
8f95b435 PZI |
10231 | |
10232 | /* | |
10233 | * Removing from the context ends up with disabled | |
10234 | * event. What we want here is event in the initial | |
10235 | * startup state, ready to be add into new context. | |
10236 | */ | |
10237 | perf_event__state_init(group_leader); | |
10238 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
10239 | get_ctx(ctx); | |
bed5b25a AS |
10240 | } |
10241 | ||
f73e22ab PZ |
10242 | /* |
10243 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
10244 | * that we're serialized against further additions and before | |
10245 | * perf_install_in_context() which is the point the event is active and | |
10246 | * can use these values. | |
10247 | */ | |
10248 | perf_event__header_size(event); | |
10249 | perf_event__id_header_size(event); | |
10250 | ||
78cd2c74 PZ |
10251 | event->owner = current; |
10252 | ||
e2d37cd2 | 10253 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 10254 | perf_unpin_context(ctx); |
f63a8daa | 10255 | |
f55fc2a5 | 10256 | if (move_group) |
321027c1 | 10257 | perf_event_ctx_unlock(group_leader, gctx); |
d859e29f | 10258 | mutex_unlock(&ctx->mutex); |
9b51f66d | 10259 | |
79c9ce57 PZ |
10260 | if (task) { |
10261 | mutex_unlock(&task->signal->cred_guard_mutex); | |
10262 | put_task_struct(task); | |
10263 | } | |
10264 | ||
cdd6c482 IM |
10265 | mutex_lock(¤t->perf_event_mutex); |
10266 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
10267 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 10268 | |
8a49542c PZ |
10269 | /* |
10270 | * Drop the reference on the group_event after placing the | |
10271 | * new event on the sibling_list. This ensures destruction | |
10272 | * of the group leader will find the pointer to itself in | |
10273 | * perf_group_detach(). | |
10274 | */ | |
2903ff01 | 10275 | fdput(group); |
ea635c64 AV |
10276 | fd_install(event_fd, event_file); |
10277 | return event_fd; | |
0793a61d | 10278 | |
f55fc2a5 PZ |
10279 | err_locked: |
10280 | if (move_group) | |
321027c1 | 10281 | perf_event_ctx_unlock(group_leader, gctx); |
f55fc2a5 PZ |
10282 | mutex_unlock(&ctx->mutex); |
10283 | /* err_file: */ | |
10284 | fput(event_file); | |
c3f00c70 | 10285 | err_context: |
fe4b04fa | 10286 | perf_unpin_context(ctx); |
ea635c64 | 10287 | put_ctx(ctx); |
c6be5a5c | 10288 | err_alloc: |
13005627 PZ |
10289 | /* |
10290 | * If event_file is set, the fput() above will have called ->release() | |
10291 | * and that will take care of freeing the event. | |
10292 | */ | |
10293 | if (!event_file) | |
10294 | free_event(event); | |
79c9ce57 PZ |
10295 | err_cred: |
10296 | if (task) | |
10297 | mutex_unlock(&task->signal->cred_guard_mutex); | |
1f4ee503 | 10298 | err_task: |
e7d0bc04 PZ |
10299 | if (task) |
10300 | put_task_struct(task); | |
89a1e187 | 10301 | err_group_fd: |
2903ff01 | 10302 | fdput(group); |
ea635c64 AV |
10303 | err_fd: |
10304 | put_unused_fd(event_fd); | |
dc86cabe | 10305 | return err; |
0793a61d TG |
10306 | } |
10307 | ||
fb0459d7 AV |
10308 | /** |
10309 | * perf_event_create_kernel_counter | |
10310 | * | |
10311 | * @attr: attributes of the counter to create | |
10312 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 10313 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
10314 | */ |
10315 | struct perf_event * | |
10316 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 10317 | struct task_struct *task, |
4dc0da86 AK |
10318 | perf_overflow_handler_t overflow_handler, |
10319 | void *context) | |
fb0459d7 | 10320 | { |
fb0459d7 | 10321 | struct perf_event_context *ctx; |
c3f00c70 | 10322 | struct perf_event *event; |
fb0459d7 | 10323 | int err; |
d859e29f | 10324 | |
fb0459d7 AV |
10325 | /* |
10326 | * Get the target context (task or percpu): | |
10327 | */ | |
d859e29f | 10328 | |
4dc0da86 | 10329 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 10330 | overflow_handler, context, -1); |
c3f00c70 PZ |
10331 | if (IS_ERR(event)) { |
10332 | err = PTR_ERR(event); | |
10333 | goto err; | |
10334 | } | |
d859e29f | 10335 | |
f8697762 | 10336 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 10337 | event->owner = TASK_TOMBSTONE; |
f8697762 | 10338 | |
4af57ef2 | 10339 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
10340 | if (IS_ERR(ctx)) { |
10341 | err = PTR_ERR(ctx); | |
c3f00c70 | 10342 | goto err_free; |
d859e29f | 10343 | } |
fb0459d7 | 10344 | |
fb0459d7 AV |
10345 | WARN_ON_ONCE(ctx->parent_ctx); |
10346 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
10347 | if (ctx->task == TASK_TOMBSTONE) { |
10348 | err = -ESRCH; | |
10349 | goto err_unlock; | |
10350 | } | |
10351 | ||
a63fbed7 TG |
10352 | if (!task) { |
10353 | /* | |
10354 | * Check if the @cpu we're creating an event for is online. | |
10355 | * | |
10356 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10357 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10358 | */ | |
10359 | struct perf_cpu_context *cpuctx = | |
10360 | container_of(ctx, struct perf_cpu_context, ctx); | |
10361 | if (!cpuctx->online) { | |
10362 | err = -ENODEV; | |
10363 | goto err_unlock; | |
10364 | } | |
10365 | } | |
10366 | ||
bed5b25a | 10367 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 10368 | err = -EBUSY; |
84c4e620 | 10369 | goto err_unlock; |
bed5b25a AS |
10370 | } |
10371 | ||
fb0459d7 | 10372 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 10373 | perf_unpin_context(ctx); |
fb0459d7 AV |
10374 | mutex_unlock(&ctx->mutex); |
10375 | ||
fb0459d7 AV |
10376 | return event; |
10377 | ||
84c4e620 PZ |
10378 | err_unlock: |
10379 | mutex_unlock(&ctx->mutex); | |
10380 | perf_unpin_context(ctx); | |
10381 | put_ctx(ctx); | |
c3f00c70 PZ |
10382 | err_free: |
10383 | free_event(event); | |
10384 | err: | |
c6567f64 | 10385 | return ERR_PTR(err); |
9b51f66d | 10386 | } |
fb0459d7 | 10387 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 10388 | |
0cda4c02 YZ |
10389 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
10390 | { | |
10391 | struct perf_event_context *src_ctx; | |
10392 | struct perf_event_context *dst_ctx; | |
10393 | struct perf_event *event, *tmp; | |
10394 | LIST_HEAD(events); | |
10395 | ||
10396 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
10397 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
10398 | ||
f63a8daa PZ |
10399 | /* |
10400 | * See perf_event_ctx_lock() for comments on the details | |
10401 | * of swizzling perf_event::ctx. | |
10402 | */ | |
10403 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
10404 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
10405 | event_entry) { | |
45a0e07a | 10406 | perf_remove_from_context(event, 0); |
9a545de0 | 10407 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 10408 | put_ctx(src_ctx); |
9886167d | 10409 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 10410 | } |
0cda4c02 | 10411 | |
8f95b435 PZI |
10412 | /* |
10413 | * Wait for the events to quiesce before re-instating them. | |
10414 | */ | |
0cda4c02 YZ |
10415 | synchronize_rcu(); |
10416 | ||
8f95b435 PZI |
10417 | /* |
10418 | * Re-instate events in 2 passes. | |
10419 | * | |
10420 | * Skip over group leaders and only install siblings on this first | |
10421 | * pass, siblings will not get enabled without a leader, however a | |
10422 | * leader will enable its siblings, even if those are still on the old | |
10423 | * context. | |
10424 | */ | |
10425 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
10426 | if (event->group_leader == event) | |
10427 | continue; | |
10428 | ||
10429 | list_del(&event->migrate_entry); | |
10430 | if (event->state >= PERF_EVENT_STATE_OFF) | |
10431 | event->state = PERF_EVENT_STATE_INACTIVE; | |
10432 | account_event_cpu(event, dst_cpu); | |
10433 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
10434 | get_ctx(dst_ctx); | |
10435 | } | |
10436 | ||
10437 | /* | |
10438 | * Once all the siblings are setup properly, install the group leaders | |
10439 | * to make it go. | |
10440 | */ | |
9886167d PZ |
10441 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
10442 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
10443 | if (event->state >= PERF_EVENT_STATE_OFF) |
10444 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 10445 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
10446 | perf_install_in_context(dst_ctx, event, dst_cpu); |
10447 | get_ctx(dst_ctx); | |
10448 | } | |
10449 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 10450 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
10451 | } |
10452 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
10453 | ||
cdd6c482 | 10454 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 10455 | struct task_struct *child) |
d859e29f | 10456 | { |
cdd6c482 | 10457 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 10458 | u64 child_val; |
d859e29f | 10459 | |
cdd6c482 IM |
10460 | if (child_event->attr.inherit_stat) |
10461 | perf_event_read_event(child_event, child); | |
38b200d6 | 10462 | |
b5e58793 | 10463 | child_val = perf_event_count(child_event); |
d859e29f PM |
10464 | |
10465 | /* | |
10466 | * Add back the child's count to the parent's count: | |
10467 | */ | |
a6e6dea6 | 10468 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
10469 | atomic64_add(child_event->total_time_enabled, |
10470 | &parent_event->child_total_time_enabled); | |
10471 | atomic64_add(child_event->total_time_running, | |
10472 | &parent_event->child_total_time_running); | |
d859e29f PM |
10473 | } |
10474 | ||
9b51f66d | 10475 | static void |
8ba289b8 PZ |
10476 | perf_event_exit_event(struct perf_event *child_event, |
10477 | struct perf_event_context *child_ctx, | |
10478 | struct task_struct *child) | |
9b51f66d | 10479 | { |
8ba289b8 PZ |
10480 | struct perf_event *parent_event = child_event->parent; |
10481 | ||
1903d50c PZ |
10482 | /* |
10483 | * Do not destroy the 'original' grouping; because of the context | |
10484 | * switch optimization the original events could've ended up in a | |
10485 | * random child task. | |
10486 | * | |
10487 | * If we were to destroy the original group, all group related | |
10488 | * operations would cease to function properly after this random | |
10489 | * child dies. | |
10490 | * | |
10491 | * Do destroy all inherited groups, we don't care about those | |
10492 | * and being thorough is better. | |
10493 | */ | |
32132a3d PZ |
10494 | raw_spin_lock_irq(&child_ctx->lock); |
10495 | WARN_ON_ONCE(child_ctx->is_active); | |
10496 | ||
8ba289b8 | 10497 | if (parent_event) |
32132a3d PZ |
10498 | perf_group_detach(child_event); |
10499 | list_del_event(child_event, child_ctx); | |
0d3d73aa | 10500 | perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */ |
32132a3d | 10501 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 10502 | |
9b51f66d | 10503 | /* |
8ba289b8 | 10504 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 10505 | */ |
8ba289b8 | 10506 | if (!parent_event) { |
179033b3 | 10507 | perf_event_wakeup(child_event); |
8ba289b8 | 10508 | return; |
4bcf349a | 10509 | } |
8ba289b8 PZ |
10510 | /* |
10511 | * Child events can be cleaned up. | |
10512 | */ | |
10513 | ||
10514 | sync_child_event(child_event, child); | |
10515 | ||
10516 | /* | |
10517 | * Remove this event from the parent's list | |
10518 | */ | |
10519 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
10520 | mutex_lock(&parent_event->child_mutex); | |
10521 | list_del_init(&child_event->child_list); | |
10522 | mutex_unlock(&parent_event->child_mutex); | |
10523 | ||
10524 | /* | |
10525 | * Kick perf_poll() for is_event_hup(). | |
10526 | */ | |
10527 | perf_event_wakeup(parent_event); | |
10528 | free_event(child_event); | |
10529 | put_event(parent_event); | |
9b51f66d IM |
10530 | } |
10531 | ||
8dc85d54 | 10532 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 10533 | { |
211de6eb | 10534 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 10535 | struct perf_event *child_event, *next; |
63b6da39 PZ |
10536 | |
10537 | WARN_ON_ONCE(child != current); | |
9b51f66d | 10538 | |
6a3351b6 | 10539 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 10540 | if (!child_ctx) |
9b51f66d IM |
10541 | return; |
10542 | ||
ad3a37de | 10543 | /* |
6a3351b6 PZ |
10544 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
10545 | * ctx::mutex over the entire thing. This serializes against almost | |
10546 | * everything that wants to access the ctx. | |
10547 | * | |
10548 | * The exception is sys_perf_event_open() / | |
10549 | * perf_event_create_kernel_count() which does find_get_context() | |
10550 | * without ctx::mutex (it cannot because of the move_group double mutex | |
10551 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 10552 | */ |
6a3351b6 | 10553 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
10554 | |
10555 | /* | |
6a3351b6 PZ |
10556 | * In a single ctx::lock section, de-schedule the events and detach the |
10557 | * context from the task such that we cannot ever get it scheduled back | |
10558 | * in. | |
c93f7669 | 10559 | */ |
6a3351b6 | 10560 | raw_spin_lock_irq(&child_ctx->lock); |
487f05e1 | 10561 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL); |
4a1c0f26 | 10562 | |
71a851b4 | 10563 | /* |
63b6da39 PZ |
10564 | * Now that the context is inactive, destroy the task <-> ctx relation |
10565 | * and mark the context dead. | |
71a851b4 | 10566 | */ |
63b6da39 PZ |
10567 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
10568 | put_ctx(child_ctx); /* cannot be last */ | |
10569 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
10570 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 10571 | |
211de6eb | 10572 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 10573 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 10574 | |
211de6eb PZ |
10575 | if (clone_ctx) |
10576 | put_ctx(clone_ctx); | |
4a1c0f26 | 10577 | |
9f498cc5 | 10578 | /* |
cdd6c482 IM |
10579 | * Report the task dead after unscheduling the events so that we |
10580 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
10581 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 10582 | */ |
cdd6c482 | 10583 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 10584 | |
ebf905fc | 10585 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 10586 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 10587 | |
a63eaf34 PM |
10588 | mutex_unlock(&child_ctx->mutex); |
10589 | ||
10590 | put_ctx(child_ctx); | |
9b51f66d IM |
10591 | } |
10592 | ||
8dc85d54 PZ |
10593 | /* |
10594 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 PZ |
10595 | * |
10596 | * Can be called with cred_guard_mutex held when called from | |
10597 | * install_exec_creds(). | |
8dc85d54 PZ |
10598 | */ |
10599 | void perf_event_exit_task(struct task_struct *child) | |
10600 | { | |
8882135b | 10601 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
10602 | int ctxn; |
10603 | ||
8882135b PZ |
10604 | mutex_lock(&child->perf_event_mutex); |
10605 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
10606 | owner_entry) { | |
10607 | list_del_init(&event->owner_entry); | |
10608 | ||
10609 | /* | |
10610 | * Ensure the list deletion is visible before we clear | |
10611 | * the owner, closes a race against perf_release() where | |
10612 | * we need to serialize on the owner->perf_event_mutex. | |
10613 | */ | |
f47c02c0 | 10614 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
10615 | } |
10616 | mutex_unlock(&child->perf_event_mutex); | |
10617 | ||
8dc85d54 PZ |
10618 | for_each_task_context_nr(ctxn) |
10619 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
10620 | |
10621 | /* | |
10622 | * The perf_event_exit_task_context calls perf_event_task | |
10623 | * with child's task_ctx, which generates EXIT events for | |
10624 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
10625 | * At this point we need to send EXIT events to cpu contexts. | |
10626 | */ | |
10627 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
10628 | } |
10629 | ||
889ff015 FW |
10630 | static void perf_free_event(struct perf_event *event, |
10631 | struct perf_event_context *ctx) | |
10632 | { | |
10633 | struct perf_event *parent = event->parent; | |
10634 | ||
10635 | if (WARN_ON_ONCE(!parent)) | |
10636 | return; | |
10637 | ||
10638 | mutex_lock(&parent->child_mutex); | |
10639 | list_del_init(&event->child_list); | |
10640 | mutex_unlock(&parent->child_mutex); | |
10641 | ||
a6fa941d | 10642 | put_event(parent); |
889ff015 | 10643 | |
652884fe | 10644 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 10645 | perf_group_detach(event); |
889ff015 | 10646 | list_del_event(event, ctx); |
652884fe | 10647 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
10648 | free_event(event); |
10649 | } | |
10650 | ||
bbbee908 | 10651 | /* |
652884fe | 10652 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 10653 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
10654 | * |
10655 | * Not all locks are strictly required, but take them anyway to be nice and | |
10656 | * help out with the lockdep assertions. | |
bbbee908 | 10657 | */ |
cdd6c482 | 10658 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 10659 | { |
8dc85d54 | 10660 | struct perf_event_context *ctx; |
cdd6c482 | 10661 | struct perf_event *event, *tmp; |
8dc85d54 | 10662 | int ctxn; |
bbbee908 | 10663 | |
8dc85d54 PZ |
10664 | for_each_task_context_nr(ctxn) { |
10665 | ctx = task->perf_event_ctxp[ctxn]; | |
10666 | if (!ctx) | |
10667 | continue; | |
bbbee908 | 10668 | |
8dc85d54 | 10669 | mutex_lock(&ctx->mutex); |
e552a838 PZ |
10670 | raw_spin_lock_irq(&ctx->lock); |
10671 | /* | |
10672 | * Destroy the task <-> ctx relation and mark the context dead. | |
10673 | * | |
10674 | * This is important because even though the task hasn't been | |
10675 | * exposed yet the context has been (through child_list). | |
10676 | */ | |
10677 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); | |
10678 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
10679 | put_task_struct(task); /* cannot be last */ | |
10680 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 10681 | |
15121c78 | 10682 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
8dc85d54 | 10683 | perf_free_event(event, ctx); |
bbbee908 | 10684 | |
8dc85d54 | 10685 | mutex_unlock(&ctx->mutex); |
8dc85d54 PZ |
10686 | put_ctx(ctx); |
10687 | } | |
889ff015 FW |
10688 | } |
10689 | ||
4e231c79 PZ |
10690 | void perf_event_delayed_put(struct task_struct *task) |
10691 | { | |
10692 | int ctxn; | |
10693 | ||
10694 | for_each_task_context_nr(ctxn) | |
10695 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
10696 | } | |
10697 | ||
e03e7ee3 | 10698 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 10699 | { |
e03e7ee3 | 10700 | struct file *file; |
ffe8690c | 10701 | |
e03e7ee3 AS |
10702 | file = fget_raw(fd); |
10703 | if (!file) | |
10704 | return ERR_PTR(-EBADF); | |
ffe8690c | 10705 | |
e03e7ee3 AS |
10706 | if (file->f_op != &perf_fops) { |
10707 | fput(file); | |
10708 | return ERR_PTR(-EBADF); | |
10709 | } | |
ffe8690c | 10710 | |
e03e7ee3 | 10711 | return file; |
ffe8690c KX |
10712 | } |
10713 | ||
10714 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
10715 | { | |
10716 | if (!event) | |
10717 | return ERR_PTR(-EINVAL); | |
10718 | ||
10719 | return &event->attr; | |
10720 | } | |
10721 | ||
97dee4f3 | 10722 | /* |
d8a8cfc7 PZ |
10723 | * Inherit a event from parent task to child task. |
10724 | * | |
10725 | * Returns: | |
10726 | * - valid pointer on success | |
10727 | * - NULL for orphaned events | |
10728 | * - IS_ERR() on error | |
97dee4f3 PZ |
10729 | */ |
10730 | static struct perf_event * | |
10731 | inherit_event(struct perf_event *parent_event, | |
10732 | struct task_struct *parent, | |
10733 | struct perf_event_context *parent_ctx, | |
10734 | struct task_struct *child, | |
10735 | struct perf_event *group_leader, | |
10736 | struct perf_event_context *child_ctx) | |
10737 | { | |
8ca2bd41 | 10738 | enum perf_event_state parent_state = parent_event->state; |
97dee4f3 | 10739 | struct perf_event *child_event; |
cee010ec | 10740 | unsigned long flags; |
97dee4f3 PZ |
10741 | |
10742 | /* | |
10743 | * Instead of creating recursive hierarchies of events, | |
10744 | * we link inherited events back to the original parent, | |
10745 | * which has a filp for sure, which we use as the reference | |
10746 | * count: | |
10747 | */ | |
10748 | if (parent_event->parent) | |
10749 | parent_event = parent_event->parent; | |
10750 | ||
10751 | child_event = perf_event_alloc(&parent_event->attr, | |
10752 | parent_event->cpu, | |
d580ff86 | 10753 | child, |
97dee4f3 | 10754 | group_leader, parent_event, |
79dff51e | 10755 | NULL, NULL, -1); |
97dee4f3 PZ |
10756 | if (IS_ERR(child_event)) |
10757 | return child_event; | |
a6fa941d | 10758 | |
c6e5b732 PZ |
10759 | /* |
10760 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
10761 | * must be under the same lock in order to serialize against | |
10762 | * perf_event_release_kernel(), such that either we must observe | |
10763 | * is_orphaned_event() or they will observe us on the child_list. | |
10764 | */ | |
10765 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
10766 | if (is_orphaned_event(parent_event) || |
10767 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 10768 | mutex_unlock(&parent_event->child_mutex); |
a6fa941d AV |
10769 | free_event(child_event); |
10770 | return NULL; | |
10771 | } | |
10772 | ||
97dee4f3 PZ |
10773 | get_ctx(child_ctx); |
10774 | ||
10775 | /* | |
10776 | * Make the child state follow the state of the parent event, | |
10777 | * not its attr.disabled bit. We hold the parent's mutex, | |
10778 | * so we won't race with perf_event_{en, dis}able_family. | |
10779 | */ | |
1929def9 | 10780 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
10781 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
10782 | else | |
10783 | child_event->state = PERF_EVENT_STATE_OFF; | |
10784 | ||
10785 | if (parent_event->attr.freq) { | |
10786 | u64 sample_period = parent_event->hw.sample_period; | |
10787 | struct hw_perf_event *hwc = &child_event->hw; | |
10788 | ||
10789 | hwc->sample_period = sample_period; | |
10790 | hwc->last_period = sample_period; | |
10791 | ||
10792 | local64_set(&hwc->period_left, sample_period); | |
10793 | } | |
10794 | ||
10795 | child_event->ctx = child_ctx; | |
10796 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
10797 | child_event->overflow_handler_context |
10798 | = parent_event->overflow_handler_context; | |
97dee4f3 | 10799 | |
614b6780 TG |
10800 | /* |
10801 | * Precalculate sample_data sizes | |
10802 | */ | |
10803 | perf_event__header_size(child_event); | |
6844c09d | 10804 | perf_event__id_header_size(child_event); |
614b6780 | 10805 | |
97dee4f3 PZ |
10806 | /* |
10807 | * Link it up in the child's context: | |
10808 | */ | |
cee010ec | 10809 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 10810 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 10811 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 10812 | |
97dee4f3 PZ |
10813 | /* |
10814 | * Link this into the parent event's child list | |
10815 | */ | |
97dee4f3 PZ |
10816 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
10817 | mutex_unlock(&parent_event->child_mutex); | |
10818 | ||
10819 | return child_event; | |
10820 | } | |
10821 | ||
d8a8cfc7 PZ |
10822 | /* |
10823 | * Inherits an event group. | |
10824 | * | |
10825 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
10826 | * This matches with perf_event_release_kernel() removing all child events. | |
10827 | * | |
10828 | * Returns: | |
10829 | * - 0 on success | |
10830 | * - <0 on error | |
10831 | */ | |
97dee4f3 PZ |
10832 | static int inherit_group(struct perf_event *parent_event, |
10833 | struct task_struct *parent, | |
10834 | struct perf_event_context *parent_ctx, | |
10835 | struct task_struct *child, | |
10836 | struct perf_event_context *child_ctx) | |
10837 | { | |
10838 | struct perf_event *leader; | |
10839 | struct perf_event *sub; | |
10840 | struct perf_event *child_ctr; | |
10841 | ||
10842 | leader = inherit_event(parent_event, parent, parent_ctx, | |
10843 | child, NULL, child_ctx); | |
10844 | if (IS_ERR(leader)) | |
10845 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
10846 | /* |
10847 | * @leader can be NULL here because of is_orphaned_event(). In this | |
10848 | * case inherit_event() will create individual events, similar to what | |
10849 | * perf_group_detach() would do anyway. | |
10850 | */ | |
97dee4f3 PZ |
10851 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
10852 | child_ctr = inherit_event(sub, parent, parent_ctx, | |
10853 | child, leader, child_ctx); | |
10854 | if (IS_ERR(child_ctr)) | |
10855 | return PTR_ERR(child_ctr); | |
10856 | } | |
10857 | return 0; | |
889ff015 FW |
10858 | } |
10859 | ||
d8a8cfc7 PZ |
10860 | /* |
10861 | * Creates the child task context and tries to inherit the event-group. | |
10862 | * | |
10863 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
10864 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
10865 | * consistent with perf_event_release_kernel() removing all child events. | |
10866 | * | |
10867 | * Returns: | |
10868 | * - 0 on success | |
10869 | * - <0 on error | |
10870 | */ | |
889ff015 FW |
10871 | static int |
10872 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
10873 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 10874 | struct task_struct *child, int ctxn, |
889ff015 FW |
10875 | int *inherited_all) |
10876 | { | |
10877 | int ret; | |
8dc85d54 | 10878 | struct perf_event_context *child_ctx; |
889ff015 FW |
10879 | |
10880 | if (!event->attr.inherit) { | |
10881 | *inherited_all = 0; | |
10882 | return 0; | |
bbbee908 PZ |
10883 | } |
10884 | ||
fe4b04fa | 10885 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
10886 | if (!child_ctx) { |
10887 | /* | |
10888 | * This is executed from the parent task context, so | |
10889 | * inherit events that have been marked for cloning. | |
10890 | * First allocate and initialize a context for the | |
10891 | * child. | |
10892 | */ | |
734df5ab | 10893 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
10894 | if (!child_ctx) |
10895 | return -ENOMEM; | |
bbbee908 | 10896 | |
8dc85d54 | 10897 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
10898 | } |
10899 | ||
10900 | ret = inherit_group(event, parent, parent_ctx, | |
10901 | child, child_ctx); | |
10902 | ||
10903 | if (ret) | |
10904 | *inherited_all = 0; | |
10905 | ||
10906 | return ret; | |
bbbee908 PZ |
10907 | } |
10908 | ||
9b51f66d | 10909 | /* |
cdd6c482 | 10910 | * Initialize the perf_event context in task_struct |
9b51f66d | 10911 | */ |
985c8dcb | 10912 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 10913 | { |
889ff015 | 10914 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
10915 | struct perf_event_context *cloned_ctx; |
10916 | struct perf_event *event; | |
9b51f66d | 10917 | struct task_struct *parent = current; |
564c2b21 | 10918 | int inherited_all = 1; |
dddd3379 | 10919 | unsigned long flags; |
6ab423e0 | 10920 | int ret = 0; |
9b51f66d | 10921 | |
8dc85d54 | 10922 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
10923 | return 0; |
10924 | ||
ad3a37de | 10925 | /* |
25346b93 PM |
10926 | * If the parent's context is a clone, pin it so it won't get |
10927 | * swapped under us. | |
ad3a37de | 10928 | */ |
8dc85d54 | 10929 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
10930 | if (!parent_ctx) |
10931 | return 0; | |
25346b93 | 10932 | |
ad3a37de PM |
10933 | /* |
10934 | * No need to check if parent_ctx != NULL here; since we saw | |
10935 | * it non-NULL earlier, the only reason for it to become NULL | |
10936 | * is if we exit, and since we're currently in the middle of | |
10937 | * a fork we can't be exiting at the same time. | |
10938 | */ | |
ad3a37de | 10939 | |
9b51f66d IM |
10940 | /* |
10941 | * Lock the parent list. No need to lock the child - not PID | |
10942 | * hashed yet and not running, so nobody can access it. | |
10943 | */ | |
d859e29f | 10944 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
10945 | |
10946 | /* | |
10947 | * We dont have to disable NMIs - we are only looking at | |
10948 | * the list, not manipulating it: | |
10949 | */ | |
889ff015 | 10950 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
8dc85d54 PZ |
10951 | ret = inherit_task_group(event, parent, parent_ctx, |
10952 | child, ctxn, &inherited_all); | |
889ff015 | 10953 | if (ret) |
e7cc4865 | 10954 | goto out_unlock; |
889ff015 | 10955 | } |
b93f7978 | 10956 | |
dddd3379 TG |
10957 | /* |
10958 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
10959 | * to allocations, but we need to prevent rotation because | |
10960 | * rotate_ctx() will change the list from interrupt context. | |
10961 | */ | |
10962 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
10963 | parent_ctx->rotate_disable = 1; | |
10964 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
10965 | ||
889ff015 | 10966 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
8dc85d54 PZ |
10967 | ret = inherit_task_group(event, parent, parent_ctx, |
10968 | child, ctxn, &inherited_all); | |
889ff015 | 10969 | if (ret) |
e7cc4865 | 10970 | goto out_unlock; |
564c2b21 PM |
10971 | } |
10972 | ||
dddd3379 TG |
10973 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
10974 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 10975 | |
8dc85d54 | 10976 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 10977 | |
05cbaa28 | 10978 | if (child_ctx && inherited_all) { |
564c2b21 PM |
10979 | /* |
10980 | * Mark the child context as a clone of the parent | |
10981 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
10982 | * |
10983 | * Note that if the parent is a clone, the holding of | |
10984 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 10985 | */ |
c5ed5145 | 10986 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
10987 | if (cloned_ctx) { |
10988 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 10989 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
10990 | } else { |
10991 | child_ctx->parent_ctx = parent_ctx; | |
10992 | child_ctx->parent_gen = parent_ctx->generation; | |
10993 | } | |
10994 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
10995 | } |
10996 | ||
c5ed5145 | 10997 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 10998 | out_unlock: |
d859e29f | 10999 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 11000 | |
25346b93 | 11001 | perf_unpin_context(parent_ctx); |
fe4b04fa | 11002 | put_ctx(parent_ctx); |
ad3a37de | 11003 | |
6ab423e0 | 11004 | return ret; |
9b51f66d IM |
11005 | } |
11006 | ||
8dc85d54 PZ |
11007 | /* |
11008 | * Initialize the perf_event context in task_struct | |
11009 | */ | |
11010 | int perf_event_init_task(struct task_struct *child) | |
11011 | { | |
11012 | int ctxn, ret; | |
11013 | ||
8550d7cb ON |
11014 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
11015 | mutex_init(&child->perf_event_mutex); | |
11016 | INIT_LIST_HEAD(&child->perf_event_list); | |
11017 | ||
8dc85d54 PZ |
11018 | for_each_task_context_nr(ctxn) { |
11019 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
11020 | if (ret) { |
11021 | perf_event_free_task(child); | |
8dc85d54 | 11022 | return ret; |
6c72e350 | 11023 | } |
8dc85d54 PZ |
11024 | } |
11025 | ||
11026 | return 0; | |
11027 | } | |
11028 | ||
220b140b PM |
11029 | static void __init perf_event_init_all_cpus(void) |
11030 | { | |
b28ab83c | 11031 | struct swevent_htable *swhash; |
220b140b | 11032 | int cpu; |
220b140b | 11033 | |
a63fbed7 TG |
11034 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
11035 | ||
220b140b | 11036 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
11037 | swhash = &per_cpu(swevent_htable, cpu); |
11038 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 11039 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
11040 | |
11041 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
11042 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 11043 | |
058fe1c0 DCC |
11044 | #ifdef CONFIG_CGROUP_PERF |
11045 | INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu)); | |
11046 | #endif | |
e48c1788 | 11047 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
220b140b PM |
11048 | } |
11049 | } | |
11050 | ||
a63fbed7 | 11051 | void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 11052 | { |
108b02cf | 11053 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 11054 | |
b28ab83c | 11055 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 11056 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
11057 | struct swevent_hlist *hlist; |
11058 | ||
b28ab83c PZ |
11059 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
11060 | WARN_ON(!hlist); | |
11061 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 11062 | } |
b28ab83c | 11063 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
11064 | } |
11065 | ||
2965faa5 | 11066 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 11067 | static void __perf_event_exit_context(void *__info) |
0793a61d | 11068 | { |
108b02cf | 11069 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
11070 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
11071 | struct perf_event *event; | |
0793a61d | 11072 | |
fae3fde6 | 11073 | raw_spin_lock(&ctx->lock); |
0ee098c9 | 11074 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
fae3fde6 | 11075 | list_for_each_entry(event, &ctx->event_list, event_entry) |
45a0e07a | 11076 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 11077 | raw_spin_unlock(&ctx->lock); |
0793a61d | 11078 | } |
108b02cf PZ |
11079 | |
11080 | static void perf_event_exit_cpu_context(int cpu) | |
11081 | { | |
a63fbed7 | 11082 | struct perf_cpu_context *cpuctx; |
108b02cf PZ |
11083 | struct perf_event_context *ctx; |
11084 | struct pmu *pmu; | |
108b02cf | 11085 | |
a63fbed7 TG |
11086 | mutex_lock(&pmus_lock); |
11087 | list_for_each_entry(pmu, &pmus, entry) { | |
11088 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11089 | ctx = &cpuctx->ctx; | |
108b02cf PZ |
11090 | |
11091 | mutex_lock(&ctx->mutex); | |
11092 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
a63fbed7 | 11093 | cpuctx->online = 0; |
108b02cf PZ |
11094 | mutex_unlock(&ctx->mutex); |
11095 | } | |
a63fbed7 TG |
11096 | cpumask_clear_cpu(cpu, perf_online_mask); |
11097 | mutex_unlock(&pmus_lock); | |
108b02cf | 11098 | } |
00e16c3d TG |
11099 | #else |
11100 | ||
11101 | static void perf_event_exit_cpu_context(int cpu) { } | |
11102 | ||
11103 | #endif | |
108b02cf | 11104 | |
a63fbed7 TG |
11105 | int perf_event_init_cpu(unsigned int cpu) |
11106 | { | |
11107 | struct perf_cpu_context *cpuctx; | |
11108 | struct perf_event_context *ctx; | |
11109 | struct pmu *pmu; | |
11110 | ||
11111 | perf_swevent_init_cpu(cpu); | |
11112 | ||
11113 | mutex_lock(&pmus_lock); | |
11114 | cpumask_set_cpu(cpu, perf_online_mask); | |
11115 | list_for_each_entry(pmu, &pmus, entry) { | |
11116 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11117 | ctx = &cpuctx->ctx; | |
11118 | ||
11119 | mutex_lock(&ctx->mutex); | |
11120 | cpuctx->online = 1; | |
11121 | mutex_unlock(&ctx->mutex); | |
11122 | } | |
11123 | mutex_unlock(&pmus_lock); | |
11124 | ||
11125 | return 0; | |
11126 | } | |
11127 | ||
00e16c3d | 11128 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 11129 | { |
e3703f8c | 11130 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 11131 | return 0; |
0793a61d | 11132 | } |
0793a61d | 11133 | |
c277443c PZ |
11134 | static int |
11135 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
11136 | { | |
11137 | int cpu; | |
11138 | ||
11139 | for_each_online_cpu(cpu) | |
11140 | perf_event_exit_cpu(cpu); | |
11141 | ||
11142 | return NOTIFY_OK; | |
11143 | } | |
11144 | ||
11145 | /* | |
11146 | * Run the perf reboot notifier at the very last possible moment so that | |
11147 | * the generic watchdog code runs as long as possible. | |
11148 | */ | |
11149 | static struct notifier_block perf_reboot_notifier = { | |
11150 | .notifier_call = perf_reboot, | |
11151 | .priority = INT_MIN, | |
11152 | }; | |
11153 | ||
cdd6c482 | 11154 | void __init perf_event_init(void) |
0793a61d | 11155 | { |
3c502e7a JW |
11156 | int ret; |
11157 | ||
2e80a82a PZ |
11158 | idr_init(&pmu_idr); |
11159 | ||
220b140b | 11160 | perf_event_init_all_cpus(); |
b0a873eb | 11161 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
11162 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
11163 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
11164 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb | 11165 | perf_tp_register(); |
00e16c3d | 11166 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 11167 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
11168 | |
11169 | ret = init_hw_breakpoint(); | |
11170 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 11171 | |
b01c3a00 JO |
11172 | /* |
11173 | * Build time assertion that we keep the data_head at the intended | |
11174 | * location. IOW, validation we got the __reserved[] size right. | |
11175 | */ | |
11176 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
11177 | != 1024); | |
0793a61d | 11178 | } |
abe43400 | 11179 | |
fd979c01 CS |
11180 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
11181 | char *page) | |
11182 | { | |
11183 | struct perf_pmu_events_attr *pmu_attr = | |
11184 | container_of(attr, struct perf_pmu_events_attr, attr); | |
11185 | ||
11186 | if (pmu_attr->event_str) | |
11187 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
11188 | ||
11189 | return 0; | |
11190 | } | |
675965b0 | 11191 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 11192 | |
abe43400 PZ |
11193 | static int __init perf_event_sysfs_init(void) |
11194 | { | |
11195 | struct pmu *pmu; | |
11196 | int ret; | |
11197 | ||
11198 | mutex_lock(&pmus_lock); | |
11199 | ||
11200 | ret = bus_register(&pmu_bus); | |
11201 | if (ret) | |
11202 | goto unlock; | |
11203 | ||
11204 | list_for_each_entry(pmu, &pmus, entry) { | |
11205 | if (!pmu->name || pmu->type < 0) | |
11206 | continue; | |
11207 | ||
11208 | ret = pmu_dev_alloc(pmu); | |
11209 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
11210 | } | |
11211 | pmu_bus_running = 1; | |
11212 | ret = 0; | |
11213 | ||
11214 | unlock: | |
11215 | mutex_unlock(&pmus_lock); | |
11216 | ||
11217 | return ret; | |
11218 | } | |
11219 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
11220 | |
11221 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
11222 | static struct cgroup_subsys_state * |
11223 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
11224 | { |
11225 | struct perf_cgroup *jc; | |
e5d1367f | 11226 | |
1b15d055 | 11227 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
11228 | if (!jc) |
11229 | return ERR_PTR(-ENOMEM); | |
11230 | ||
e5d1367f SE |
11231 | jc->info = alloc_percpu(struct perf_cgroup_info); |
11232 | if (!jc->info) { | |
11233 | kfree(jc); | |
11234 | return ERR_PTR(-ENOMEM); | |
11235 | } | |
11236 | ||
e5d1367f SE |
11237 | return &jc->css; |
11238 | } | |
11239 | ||
eb95419b | 11240 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 11241 | { |
eb95419b TH |
11242 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
11243 | ||
e5d1367f SE |
11244 | free_percpu(jc->info); |
11245 | kfree(jc); | |
11246 | } | |
11247 | ||
11248 | static int __perf_cgroup_move(void *info) | |
11249 | { | |
11250 | struct task_struct *task = info; | |
ddaaf4e2 | 11251 | rcu_read_lock(); |
e5d1367f | 11252 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 11253 | rcu_read_unlock(); |
e5d1367f SE |
11254 | return 0; |
11255 | } | |
11256 | ||
1f7dd3e5 | 11257 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 11258 | { |
bb9d97b6 | 11259 | struct task_struct *task; |
1f7dd3e5 | 11260 | struct cgroup_subsys_state *css; |
bb9d97b6 | 11261 | |
1f7dd3e5 | 11262 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 11263 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
11264 | } |
11265 | ||
073219e9 | 11266 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
11267 | .css_alloc = perf_cgroup_css_alloc, |
11268 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 11269 | .attach = perf_cgroup_attach, |
968ebff1 TH |
11270 | /* |
11271 | * Implicitly enable on dfl hierarchy so that perf events can | |
11272 | * always be filtered by cgroup2 path as long as perf_event | |
11273 | * controller is not mounted on a legacy hierarchy. | |
11274 | */ | |
11275 | .implicit_on_dfl = true, | |
8cfd8147 | 11276 | .threaded = true, |
e5d1367f SE |
11277 | }; |
11278 | #endif /* CONFIG_CGROUP_PERF */ |