]>
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 PZ |
211 | |
212 | WARN_ON_ONCE(!irqs_disabled()); | |
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 | ||
309 | WARN_ON_ONCE(!irqs_disabled()); | |
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 |
cc58fdf5 | 400 | * 3 - disallow all unpriv perf event use |
0764771d | 401 | */ |
cc58fdf5 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 | ||
e5d1367f SE |
590 | #ifdef CONFIG_CGROUP_PERF |
591 | ||
e5d1367f SE |
592 | static inline bool |
593 | perf_cgroup_match(struct perf_event *event) | |
594 | { | |
595 | struct perf_event_context *ctx = event->ctx; | |
596 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
597 | ||
ef824fa1 TH |
598 | /* @event doesn't care about cgroup */ |
599 | if (!event->cgrp) | |
600 | return true; | |
601 | ||
602 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
603 | if (!cpuctx->cgrp) | |
604 | return false; | |
605 | ||
606 | /* | |
607 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
608 | * also enabled for all its descendant cgroups. If @cpuctx's | |
609 | * cgroup is a descendant of @event's (the test covers identity | |
610 | * case), it's a match. | |
611 | */ | |
612 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
613 | event->cgrp->css.cgroup); | |
e5d1367f SE |
614 | } |
615 | ||
e5d1367f SE |
616 | static inline void perf_detach_cgroup(struct perf_event *event) |
617 | { | |
4e2ba650 | 618 | css_put(&event->cgrp->css); |
e5d1367f SE |
619 | event->cgrp = NULL; |
620 | } | |
621 | ||
622 | static inline int is_cgroup_event(struct perf_event *event) | |
623 | { | |
624 | return event->cgrp != NULL; | |
625 | } | |
626 | ||
627 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
628 | { | |
629 | struct perf_cgroup_info *t; | |
630 | ||
631 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
632 | return t->time; | |
633 | } | |
634 | ||
635 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
636 | { | |
637 | struct perf_cgroup_info *info; | |
638 | u64 now; | |
639 | ||
640 | now = perf_clock(); | |
641 | ||
642 | info = this_cpu_ptr(cgrp->info); | |
643 | ||
644 | info->time += now - info->timestamp; | |
645 | info->timestamp = now; | |
646 | } | |
647 | ||
648 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
649 | { | |
650 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
651 | if (cgrp_out) | |
652 | __update_cgrp_time(cgrp_out); | |
653 | } | |
654 | ||
655 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
656 | { | |
3f7cce3c SE |
657 | struct perf_cgroup *cgrp; |
658 | ||
e5d1367f | 659 | /* |
3f7cce3c SE |
660 | * ensure we access cgroup data only when needed and |
661 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 662 | */ |
3f7cce3c | 663 | if (!is_cgroup_event(event)) |
e5d1367f SE |
664 | return; |
665 | ||
614e4c4e | 666 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
667 | /* |
668 | * Do not update time when cgroup is not active | |
669 | */ | |
670 | if (cgrp == event->cgrp) | |
671 | __update_cgrp_time(event->cgrp); | |
e5d1367f SE |
672 | } |
673 | ||
674 | static inline void | |
3f7cce3c SE |
675 | perf_cgroup_set_timestamp(struct task_struct *task, |
676 | struct perf_event_context *ctx) | |
e5d1367f SE |
677 | { |
678 | struct perf_cgroup *cgrp; | |
679 | struct perf_cgroup_info *info; | |
680 | ||
3f7cce3c SE |
681 | /* |
682 | * ctx->lock held by caller | |
683 | * ensure we do not access cgroup data | |
684 | * unless we have the cgroup pinned (css_get) | |
685 | */ | |
686 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
687 | return; |
688 | ||
614e4c4e | 689 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 690 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 691 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
692 | } |
693 | ||
058fe1c0 DCC |
694 | static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list); |
695 | ||
e5d1367f SE |
696 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
697 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
698 | ||
699 | /* | |
700 | * reschedule events based on the cgroup constraint of task. | |
701 | * | |
702 | * mode SWOUT : schedule out everything | |
703 | * mode SWIN : schedule in based on cgroup for next | |
704 | */ | |
18ab2cd3 | 705 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
706 | { |
707 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 708 | struct list_head *list; |
e5d1367f SE |
709 | unsigned long flags; |
710 | ||
711 | /* | |
058fe1c0 DCC |
712 | * Disable interrupts and preemption to avoid this CPU's |
713 | * cgrp_cpuctx_entry to change under us. | |
e5d1367f SE |
714 | */ |
715 | local_irq_save(flags); | |
716 | ||
058fe1c0 DCC |
717 | list = this_cpu_ptr(&cgrp_cpuctx_list); |
718 | list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) { | |
719 | WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); | |
e5d1367f | 720 | |
058fe1c0 DCC |
721 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
722 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f | 723 | |
058fe1c0 DCC |
724 | if (mode & PERF_CGROUP_SWOUT) { |
725 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
726 | /* | |
727 | * must not be done before ctxswout due | |
728 | * to event_filter_match() in event_sched_out() | |
729 | */ | |
730 | cpuctx->cgrp = NULL; | |
731 | } | |
e5d1367f | 732 | |
058fe1c0 DCC |
733 | if (mode & PERF_CGROUP_SWIN) { |
734 | WARN_ON_ONCE(cpuctx->cgrp); | |
735 | /* | |
736 | * set cgrp before ctxsw in to allow | |
737 | * event_filter_match() to not have to pass | |
738 | * task around | |
739 | * we pass the cpuctx->ctx to perf_cgroup_from_task() | |
740 | * because cgorup events are only per-cpu | |
741 | */ | |
742 | cpuctx->cgrp = perf_cgroup_from_task(task, | |
743 | &cpuctx->ctx); | |
744 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); | |
e5d1367f | 745 | } |
058fe1c0 DCC |
746 | perf_pmu_enable(cpuctx->ctx.pmu); |
747 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f SE |
748 | } |
749 | ||
e5d1367f SE |
750 | local_irq_restore(flags); |
751 | } | |
752 | ||
a8d757ef SE |
753 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
754 | struct task_struct *next) | |
e5d1367f | 755 | { |
a8d757ef SE |
756 | struct perf_cgroup *cgrp1; |
757 | struct perf_cgroup *cgrp2 = NULL; | |
758 | ||
ddaaf4e2 | 759 | rcu_read_lock(); |
a8d757ef SE |
760 | /* |
761 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
762 | * we do not need to pass the ctx here because we know |
763 | * we are holding the rcu lock | |
a8d757ef | 764 | */ |
614e4c4e | 765 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 766 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
767 | |
768 | /* | |
769 | * only schedule out current cgroup events if we know | |
770 | * that we are switching to a different cgroup. Otherwise, | |
771 | * do no touch the cgroup events. | |
772 | */ | |
773 | if (cgrp1 != cgrp2) | |
774 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
775 | |
776 | rcu_read_unlock(); | |
e5d1367f SE |
777 | } |
778 | ||
a8d757ef SE |
779 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
780 | struct task_struct *task) | |
e5d1367f | 781 | { |
a8d757ef SE |
782 | struct perf_cgroup *cgrp1; |
783 | struct perf_cgroup *cgrp2 = NULL; | |
784 | ||
ddaaf4e2 | 785 | rcu_read_lock(); |
a8d757ef SE |
786 | /* |
787 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
788 | * we do not need to pass the ctx here because we know |
789 | * we are holding the rcu lock | |
a8d757ef | 790 | */ |
614e4c4e | 791 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 792 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
793 | |
794 | /* | |
795 | * only need to schedule in cgroup events if we are changing | |
796 | * cgroup during ctxsw. Cgroup events were not scheduled | |
797 | * out of ctxsw out if that was not the case. | |
798 | */ | |
799 | if (cgrp1 != cgrp2) | |
800 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
801 | |
802 | rcu_read_unlock(); | |
e5d1367f SE |
803 | } |
804 | ||
805 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
806 | struct perf_event_attr *attr, | |
807 | struct perf_event *group_leader) | |
808 | { | |
809 | struct perf_cgroup *cgrp; | |
810 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
811 | struct fd f = fdget(fd); |
812 | int ret = 0; | |
e5d1367f | 813 | |
2903ff01 | 814 | if (!f.file) |
e5d1367f SE |
815 | return -EBADF; |
816 | ||
b583043e | 817 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 818 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
819 | if (IS_ERR(css)) { |
820 | ret = PTR_ERR(css); | |
821 | goto out; | |
822 | } | |
e5d1367f SE |
823 | |
824 | cgrp = container_of(css, struct perf_cgroup, css); | |
825 | event->cgrp = cgrp; | |
826 | ||
827 | /* | |
828 | * all events in a group must monitor | |
829 | * the same cgroup because a task belongs | |
830 | * to only one perf cgroup at a time | |
831 | */ | |
832 | if (group_leader && group_leader->cgrp != cgrp) { | |
833 | perf_detach_cgroup(event); | |
834 | ret = -EINVAL; | |
e5d1367f | 835 | } |
3db272c0 | 836 | out: |
2903ff01 | 837 | fdput(f); |
e5d1367f SE |
838 | return ret; |
839 | } | |
840 | ||
841 | static inline void | |
842 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
843 | { | |
844 | struct perf_cgroup_info *t; | |
845 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
846 | event->shadow_ctx_time = now - t->timestamp; | |
847 | } | |
848 | ||
849 | static inline void | |
850 | perf_cgroup_defer_enabled(struct perf_event *event) | |
851 | { | |
852 | /* | |
853 | * when the current task's perf cgroup does not match | |
854 | * the event's, we need to remember to call the | |
855 | * perf_mark_enable() function the first time a task with | |
856 | * a matching perf cgroup is scheduled in. | |
857 | */ | |
858 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) | |
859 | event->cgrp_defer_enabled = 1; | |
860 | } | |
861 | ||
862 | static inline void | |
863 | perf_cgroup_mark_enabled(struct perf_event *event, | |
864 | struct perf_event_context *ctx) | |
865 | { | |
866 | struct perf_event *sub; | |
867 | u64 tstamp = perf_event_time(event); | |
868 | ||
869 | if (!event->cgrp_defer_enabled) | |
870 | return; | |
871 | ||
872 | event->cgrp_defer_enabled = 0; | |
873 | ||
874 | event->tstamp_enabled = tstamp - event->total_time_enabled; | |
875 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
876 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { | |
877 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
878 | sub->cgrp_defer_enabled = 0; | |
879 | } | |
880 | } | |
881 | } | |
db4a8356 DCC |
882 | |
883 | /* | |
884 | * Update cpuctx->cgrp so that it is set when first cgroup event is added and | |
885 | * cleared when last cgroup event is removed. | |
886 | */ | |
887 | static inline void | |
888 | list_update_cgroup_event(struct perf_event *event, | |
889 | struct perf_event_context *ctx, bool add) | |
890 | { | |
891 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 892 | struct list_head *cpuctx_entry; |
db4a8356 DCC |
893 | |
894 | if (!is_cgroup_event(event)) | |
895 | return; | |
896 | ||
897 | if (add && ctx->nr_cgroups++) | |
898 | return; | |
899 | else if (!add && --ctx->nr_cgroups) | |
900 | return; | |
901 | /* | |
902 | * Because cgroup events are always per-cpu events, | |
903 | * this will always be called from the right CPU. | |
904 | */ | |
905 | cpuctx = __get_cpu_context(ctx); | |
058fe1c0 DCC |
906 | cpuctx_entry = &cpuctx->cgrp_cpuctx_entry; |
907 | /* cpuctx->cgrp is NULL unless a cgroup event is active in this CPU .*/ | |
908 | if (add) { | |
1986b138 TH |
909 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); |
910 | ||
058fe1c0 | 911 | list_add(cpuctx_entry, this_cpu_ptr(&cgrp_cpuctx_list)); |
1986b138 TH |
912 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
913 | cpuctx->cgrp = cgrp; | |
058fe1c0 DCC |
914 | } else { |
915 | list_del(cpuctx_entry); | |
8fc31ce8 | 916 | cpuctx->cgrp = NULL; |
058fe1c0 | 917 | } |
db4a8356 DCC |
918 | } |
919 | ||
e5d1367f SE |
920 | #else /* !CONFIG_CGROUP_PERF */ |
921 | ||
922 | static inline bool | |
923 | perf_cgroup_match(struct perf_event *event) | |
924 | { | |
925 | return true; | |
926 | } | |
927 | ||
928 | static inline void perf_detach_cgroup(struct perf_event *event) | |
929 | {} | |
930 | ||
931 | static inline int is_cgroup_event(struct perf_event *event) | |
932 | { | |
933 | return 0; | |
934 | } | |
935 | ||
e5d1367f SE |
936 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
937 | { | |
938 | } | |
939 | ||
940 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
941 | { | |
942 | } | |
943 | ||
a8d757ef SE |
944 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
945 | struct task_struct *next) | |
e5d1367f SE |
946 | { |
947 | } | |
948 | ||
a8d757ef SE |
949 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
950 | struct task_struct *task) | |
e5d1367f SE |
951 | { |
952 | } | |
953 | ||
954 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
955 | struct perf_event_attr *attr, | |
956 | struct perf_event *group_leader) | |
957 | { | |
958 | return -EINVAL; | |
959 | } | |
960 | ||
961 | static inline void | |
3f7cce3c SE |
962 | perf_cgroup_set_timestamp(struct task_struct *task, |
963 | struct perf_event_context *ctx) | |
e5d1367f SE |
964 | { |
965 | } | |
966 | ||
967 | void | |
968 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
969 | { | |
970 | } | |
971 | ||
972 | static inline void | |
973 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
974 | { | |
975 | } | |
976 | ||
977 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
978 | { | |
979 | return 0; | |
980 | } | |
981 | ||
982 | static inline void | |
983 | perf_cgroup_defer_enabled(struct perf_event *event) | |
984 | { | |
985 | } | |
986 | ||
987 | static inline void | |
988 | perf_cgroup_mark_enabled(struct perf_event *event, | |
989 | struct perf_event_context *ctx) | |
990 | { | |
991 | } | |
db4a8356 DCC |
992 | |
993 | static inline void | |
994 | list_update_cgroup_event(struct perf_event *event, | |
995 | struct perf_event_context *ctx, bool add) | |
996 | { | |
997 | } | |
998 | ||
e5d1367f SE |
999 | #endif |
1000 | ||
9e630205 SE |
1001 | /* |
1002 | * set default to be dependent on timer tick just | |
1003 | * like original code | |
1004 | */ | |
1005 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
1006 | /* | |
8a1115ff | 1007 | * function must be called with interrupts disabled |
9e630205 | 1008 | */ |
272325c4 | 1009 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
1010 | { |
1011 | struct perf_cpu_context *cpuctx; | |
9e630205 SE |
1012 | int rotations = 0; |
1013 | ||
1014 | WARN_ON(!irqs_disabled()); | |
1015 | ||
1016 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
1017 | rotations = perf_rotate_context(cpuctx); |
1018 | ||
4cfafd30 PZ |
1019 | raw_spin_lock(&cpuctx->hrtimer_lock); |
1020 | if (rotations) | |
9e630205 | 1021 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
1022 | else |
1023 | cpuctx->hrtimer_active = 0; | |
1024 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 1025 | |
4cfafd30 | 1026 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
1027 | } |
1028 | ||
272325c4 | 1029 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 1030 | { |
272325c4 | 1031 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1032 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 1033 | u64 interval; |
9e630205 SE |
1034 | |
1035 | /* no multiplexing needed for SW PMU */ | |
1036 | if (pmu->task_ctx_nr == perf_sw_context) | |
1037 | return; | |
1038 | ||
62b85639 SE |
1039 | /* |
1040 | * check default is sane, if not set then force to | |
1041 | * default interval (1/tick) | |
1042 | */ | |
272325c4 PZ |
1043 | interval = pmu->hrtimer_interval_ms; |
1044 | if (interval < 1) | |
1045 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 1046 | |
272325c4 | 1047 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 1048 | |
4cfafd30 PZ |
1049 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
1050 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 1051 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
1052 | } |
1053 | ||
272325c4 | 1054 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 1055 | { |
272325c4 | 1056 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1057 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 1058 | unsigned long flags; |
9e630205 SE |
1059 | |
1060 | /* not for SW PMU */ | |
1061 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 1062 | return 0; |
9e630205 | 1063 | |
4cfafd30 PZ |
1064 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
1065 | if (!cpuctx->hrtimer_active) { | |
1066 | cpuctx->hrtimer_active = 1; | |
1067 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
1068 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
1069 | } | |
1070 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 1071 | |
272325c4 | 1072 | return 0; |
9e630205 SE |
1073 | } |
1074 | ||
33696fc0 | 1075 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1076 | { |
33696fc0 PZ |
1077 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1078 | if (!(*count)++) | |
1079 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1080 | } |
9e35ad38 | 1081 | |
33696fc0 | 1082 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1083 | { |
33696fc0 PZ |
1084 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1085 | if (!--(*count)) | |
1086 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1087 | } |
9e35ad38 | 1088 | |
2fde4f94 | 1089 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1090 | |
1091 | /* | |
2fde4f94 MR |
1092 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1093 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1094 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1095 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1096 | */ |
2fde4f94 | 1097 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1098 | { |
2fde4f94 | 1099 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1100 | |
e9d2b064 | 1101 | WARN_ON(!irqs_disabled()); |
b5ab4cd5 | 1102 | |
2fde4f94 MR |
1103 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1104 | ||
1105 | list_add(&ctx->active_ctx_list, head); | |
1106 | } | |
1107 | ||
1108 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1109 | { | |
1110 | WARN_ON(!irqs_disabled()); | |
1111 | ||
1112 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1113 | ||
1114 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1115 | } |
9e35ad38 | 1116 | |
cdd6c482 | 1117 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1118 | { |
e5289d4a | 1119 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1120 | } |
1121 | ||
4af57ef2 YZ |
1122 | static void free_ctx(struct rcu_head *head) |
1123 | { | |
1124 | struct perf_event_context *ctx; | |
1125 | ||
1126 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1127 | kfree(ctx->task_ctx_data); | |
1128 | kfree(ctx); | |
1129 | } | |
1130 | ||
cdd6c482 | 1131 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1132 | { |
564c2b21 PM |
1133 | if (atomic_dec_and_test(&ctx->refcount)) { |
1134 | if (ctx->parent_ctx) | |
1135 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1136 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1137 | put_task_struct(ctx->task); |
4af57ef2 | 1138 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1139 | } |
a63eaf34 PM |
1140 | } |
1141 | ||
f63a8daa PZ |
1142 | /* |
1143 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1144 | * perf_pmu_migrate_context() we need some magic. | |
1145 | * | |
1146 | * Those places that change perf_event::ctx will hold both | |
1147 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1148 | * | |
8b10c5e2 PZ |
1149 | * Lock ordering is by mutex address. There are two other sites where |
1150 | * perf_event_context::mutex nests and those are: | |
1151 | * | |
1152 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1153 | * perf_event_exit_event() |
1154 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1155 | * |
1156 | * - perf_event_init_context() [ parent, 0 ] | |
1157 | * inherit_task_group() | |
1158 | * inherit_group() | |
1159 | * inherit_event() | |
1160 | * perf_event_alloc() | |
1161 | * perf_init_event() | |
1162 | * perf_try_init_event() [ child , 1 ] | |
1163 | * | |
1164 | * While it appears there is an obvious deadlock here -- the parent and child | |
1165 | * nesting levels are inverted between the two. This is in fact safe because | |
1166 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1167 | * spawning task cannot (yet) exit. | |
1168 | * | |
1169 | * But remember that that these are parent<->child context relations, and | |
1170 | * migration does not affect children, therefore these two orderings should not | |
1171 | * interact. | |
f63a8daa PZ |
1172 | * |
1173 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1174 | * because the sys_perf_event_open() case will install a new event and break | |
1175 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1176 | * concerned with cpuctx and that doesn't have children. | |
1177 | * | |
1178 | * The places that change perf_event::ctx will issue: | |
1179 | * | |
1180 | * perf_remove_from_context(); | |
1181 | * synchronize_rcu(); | |
1182 | * perf_install_in_context(); | |
1183 | * | |
1184 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1185 | * quiesce the event, after which we can install it in the new location. This | |
1186 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1187 | * while in transit. Therefore all such accessors should also acquire | |
1188 | * perf_event_context::mutex to serialize against this. | |
1189 | * | |
1190 | * However; because event->ctx can change while we're waiting to acquire | |
1191 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1192 | * function. | |
1193 | * | |
1194 | * Lock order: | |
79c9ce57 | 1195 | * cred_guard_mutex |
f63a8daa PZ |
1196 | * task_struct::perf_event_mutex |
1197 | * perf_event_context::mutex | |
f63a8daa | 1198 | * perf_event::child_mutex; |
07c4a776 | 1199 | * perf_event_context::lock |
f63a8daa PZ |
1200 | * perf_event::mmap_mutex |
1201 | * mmap_sem | |
1202 | */ | |
a83fe28e PZ |
1203 | static struct perf_event_context * |
1204 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1205 | { |
1206 | struct perf_event_context *ctx; | |
1207 | ||
1208 | again: | |
1209 | rcu_read_lock(); | |
1210 | ctx = ACCESS_ONCE(event->ctx); | |
1211 | if (!atomic_inc_not_zero(&ctx->refcount)) { | |
1212 | rcu_read_unlock(); | |
1213 | goto again; | |
1214 | } | |
1215 | rcu_read_unlock(); | |
1216 | ||
a83fe28e | 1217 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1218 | if (event->ctx != ctx) { |
1219 | mutex_unlock(&ctx->mutex); | |
1220 | put_ctx(ctx); | |
1221 | goto again; | |
1222 | } | |
1223 | ||
1224 | return ctx; | |
1225 | } | |
1226 | ||
a83fe28e PZ |
1227 | static inline struct perf_event_context * |
1228 | perf_event_ctx_lock(struct perf_event *event) | |
1229 | { | |
1230 | return perf_event_ctx_lock_nested(event, 0); | |
1231 | } | |
1232 | ||
f63a8daa PZ |
1233 | static void perf_event_ctx_unlock(struct perf_event *event, |
1234 | struct perf_event_context *ctx) | |
1235 | { | |
1236 | mutex_unlock(&ctx->mutex); | |
1237 | put_ctx(ctx); | |
1238 | } | |
1239 | ||
211de6eb PZ |
1240 | /* |
1241 | * This must be done under the ctx->lock, such as to serialize against | |
1242 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1243 | * calling scheduler related locks and ctx->lock nests inside those. | |
1244 | */ | |
1245 | static __must_check struct perf_event_context * | |
1246 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1247 | { |
211de6eb PZ |
1248 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1249 | ||
1250 | lockdep_assert_held(&ctx->lock); | |
1251 | ||
1252 | if (parent_ctx) | |
71a851b4 | 1253 | ctx->parent_ctx = NULL; |
5a3126d4 | 1254 | ctx->generation++; |
211de6eb PZ |
1255 | |
1256 | return parent_ctx; | |
71a851b4 PZ |
1257 | } |
1258 | ||
6844c09d ACM |
1259 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
1260 | { | |
1261 | /* | |
1262 | * only top level events have the pid namespace they were created in | |
1263 | */ | |
1264 | if (event->parent) | |
1265 | event = event->parent; | |
1266 | ||
1267 | return task_tgid_nr_ns(p, event->ns); | |
1268 | } | |
1269 | ||
1270 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) | |
1271 | { | |
1272 | /* | |
1273 | * only top level events have the pid namespace they were created in | |
1274 | */ | |
1275 | if (event->parent) | |
1276 | event = event->parent; | |
1277 | ||
1278 | return task_pid_nr_ns(p, event->ns); | |
1279 | } | |
1280 | ||
7f453c24 | 1281 | /* |
cdd6c482 | 1282 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1283 | * to userspace. |
1284 | */ | |
cdd6c482 | 1285 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1286 | { |
cdd6c482 | 1287 | u64 id = event->id; |
7f453c24 | 1288 | |
cdd6c482 IM |
1289 | if (event->parent) |
1290 | id = event->parent->id; | |
7f453c24 PZ |
1291 | |
1292 | return id; | |
1293 | } | |
1294 | ||
25346b93 | 1295 | /* |
cdd6c482 | 1296 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1297 | * |
25346b93 PM |
1298 | * This has to cope with with the fact that until it is locked, |
1299 | * the context could get moved to another task. | |
1300 | */ | |
cdd6c482 | 1301 | static struct perf_event_context * |
8dc85d54 | 1302 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1303 | { |
cdd6c482 | 1304 | struct perf_event_context *ctx; |
25346b93 | 1305 | |
9ed6060d | 1306 | retry: |
058ebd0e PZ |
1307 | /* |
1308 | * One of the few rules of preemptible RCU is that one cannot do | |
1309 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1310 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1311 | * rcu_read_unlock_special(). |
1312 | * | |
1313 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1314 | * side critical section has interrupts disabled. |
058ebd0e | 1315 | */ |
2fd59077 | 1316 | local_irq_save(*flags); |
058ebd0e | 1317 | rcu_read_lock(); |
8dc85d54 | 1318 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1319 | if (ctx) { |
1320 | /* | |
1321 | * If this context is a clone of another, it might | |
1322 | * get swapped for another underneath us by | |
cdd6c482 | 1323 | * perf_event_task_sched_out, though the |
25346b93 PM |
1324 | * rcu_read_lock() protects us from any context |
1325 | * getting freed. Lock the context and check if it | |
1326 | * got swapped before we could get the lock, and retry | |
1327 | * if so. If we locked the right context, then it | |
1328 | * can't get swapped on us any more. | |
1329 | */ | |
2fd59077 | 1330 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1331 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1332 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1333 | rcu_read_unlock(); |
2fd59077 | 1334 | local_irq_restore(*flags); |
25346b93 PM |
1335 | goto retry; |
1336 | } | |
b49a9e7e | 1337 | |
63b6da39 PZ |
1338 | if (ctx->task == TASK_TOMBSTONE || |
1339 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1340 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1341 | ctx = NULL; |
828b6f0e PZ |
1342 | } else { |
1343 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1344 | } |
25346b93 PM |
1345 | } |
1346 | rcu_read_unlock(); | |
2fd59077 PM |
1347 | if (!ctx) |
1348 | local_irq_restore(*flags); | |
25346b93 PM |
1349 | return ctx; |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Get the context for a task and increment its pin_count so it | |
1354 | * can't get swapped to another task. This also increments its | |
1355 | * reference count so that the context can't get freed. | |
1356 | */ | |
8dc85d54 PZ |
1357 | static struct perf_event_context * |
1358 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1359 | { |
cdd6c482 | 1360 | struct perf_event_context *ctx; |
25346b93 PM |
1361 | unsigned long flags; |
1362 | ||
8dc85d54 | 1363 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1364 | if (ctx) { |
1365 | ++ctx->pin_count; | |
e625cce1 | 1366 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1367 | } |
1368 | return ctx; | |
1369 | } | |
1370 | ||
cdd6c482 | 1371 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1372 | { |
1373 | unsigned long flags; | |
1374 | ||
e625cce1 | 1375 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1376 | --ctx->pin_count; |
e625cce1 | 1377 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1378 | } |
1379 | ||
f67218c3 PZ |
1380 | /* |
1381 | * Update the record of the current time in a context. | |
1382 | */ | |
1383 | static void update_context_time(struct perf_event_context *ctx) | |
1384 | { | |
1385 | u64 now = perf_clock(); | |
1386 | ||
1387 | ctx->time += now - ctx->timestamp; | |
1388 | ctx->timestamp = now; | |
1389 | } | |
1390 | ||
4158755d SE |
1391 | static u64 perf_event_time(struct perf_event *event) |
1392 | { | |
1393 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1394 | |
1395 | if (is_cgroup_event(event)) | |
1396 | return perf_cgroup_event_time(event); | |
1397 | ||
4158755d SE |
1398 | return ctx ? ctx->time : 0; |
1399 | } | |
1400 | ||
f67218c3 PZ |
1401 | /* |
1402 | * Update the total_time_enabled and total_time_running fields for a event. | |
1403 | */ | |
1404 | static void update_event_times(struct perf_event *event) | |
1405 | { | |
1406 | struct perf_event_context *ctx = event->ctx; | |
1407 | u64 run_end; | |
1408 | ||
3cbaa590 PZ |
1409 | lockdep_assert_held(&ctx->lock); |
1410 | ||
f67218c3 PZ |
1411 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
1412 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) | |
1413 | return; | |
3cbaa590 | 1414 | |
e5d1367f SE |
1415 | /* |
1416 | * in cgroup mode, time_enabled represents | |
1417 | * the time the event was enabled AND active | |
1418 | * tasks were in the monitored cgroup. This is | |
1419 | * independent of the activity of the context as | |
1420 | * there may be a mix of cgroup and non-cgroup events. | |
1421 | * | |
1422 | * That is why we treat cgroup events differently | |
1423 | * here. | |
1424 | */ | |
1425 | if (is_cgroup_event(event)) | |
46cd6a7f | 1426 | run_end = perf_cgroup_event_time(event); |
e5d1367f SE |
1427 | else if (ctx->is_active) |
1428 | run_end = ctx->time; | |
acd1d7c1 PZ |
1429 | else |
1430 | run_end = event->tstamp_stopped; | |
1431 | ||
1432 | event->total_time_enabled = run_end - event->tstamp_enabled; | |
f67218c3 PZ |
1433 | |
1434 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
1435 | run_end = event->tstamp_stopped; | |
1436 | else | |
4158755d | 1437 | run_end = perf_event_time(event); |
f67218c3 PZ |
1438 | |
1439 | event->total_time_running = run_end - event->tstamp_running; | |
e5d1367f | 1440 | |
f67218c3 PZ |
1441 | } |
1442 | ||
96c21a46 PZ |
1443 | /* |
1444 | * Update total_time_enabled and total_time_running for all events in a group. | |
1445 | */ | |
1446 | static void update_group_times(struct perf_event *leader) | |
1447 | { | |
1448 | struct perf_event *event; | |
1449 | ||
1450 | update_event_times(leader); | |
1451 | list_for_each_entry(event, &leader->sibling_list, group_entry) | |
1452 | update_event_times(event); | |
1453 | } | |
1454 | ||
487f05e1 AS |
1455 | static enum event_type_t get_event_type(struct perf_event *event) |
1456 | { | |
1457 | struct perf_event_context *ctx = event->ctx; | |
1458 | enum event_type_t event_type; | |
1459 | ||
1460 | lockdep_assert_held(&ctx->lock); | |
1461 | ||
3bda69c1 AS |
1462 | /* |
1463 | * It's 'group type', really, because if our group leader is | |
1464 | * pinned, so are we. | |
1465 | */ | |
1466 | if (event->group_leader != event) | |
1467 | event = event->group_leader; | |
1468 | ||
487f05e1 AS |
1469 | event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; |
1470 | if (!ctx->task) | |
1471 | event_type |= EVENT_CPU; | |
1472 | ||
1473 | return event_type; | |
1474 | } | |
1475 | ||
889ff015 FW |
1476 | static struct list_head * |
1477 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) | |
1478 | { | |
1479 | if (event->attr.pinned) | |
1480 | return &ctx->pinned_groups; | |
1481 | else | |
1482 | return &ctx->flexible_groups; | |
1483 | } | |
1484 | ||
fccc714b | 1485 | /* |
cdd6c482 | 1486 | * Add a event from the lists for its context. |
fccc714b PZ |
1487 | * Must be called with ctx->mutex and ctx->lock held. |
1488 | */ | |
04289bb9 | 1489 | static void |
cdd6c482 | 1490 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1491 | { |
c994d613 PZ |
1492 | lockdep_assert_held(&ctx->lock); |
1493 | ||
8a49542c PZ |
1494 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1495 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 IM |
1496 | |
1497 | /* | |
8a49542c PZ |
1498 | * If we're a stand alone event or group leader, we go to the context |
1499 | * list, group events are kept attached to the group so that | |
1500 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1501 | */ |
8a49542c | 1502 | if (event->group_leader == event) { |
889ff015 FW |
1503 | struct list_head *list; |
1504 | ||
4ff6a8de | 1505 | event->group_caps = event->event_caps; |
d6f962b5 | 1506 | |
889ff015 FW |
1507 | list = ctx_group_list(event, ctx); |
1508 | list_add_tail(&event->group_entry, list); | |
5c148194 | 1509 | } |
592903cd | 1510 | |
db4a8356 | 1511 | list_update_cgroup_event(event, ctx, true); |
e5d1367f | 1512 | |
cdd6c482 IM |
1513 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1514 | ctx->nr_events++; | |
1515 | if (event->attr.inherit_stat) | |
bfbd3381 | 1516 | ctx->nr_stat++; |
5a3126d4 PZ |
1517 | |
1518 | ctx->generation++; | |
04289bb9 IM |
1519 | } |
1520 | ||
0231bb53 JO |
1521 | /* |
1522 | * Initialize event state based on the perf_event_attr::disabled. | |
1523 | */ | |
1524 | static inline void perf_event__state_init(struct perf_event *event) | |
1525 | { | |
1526 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1527 | PERF_EVENT_STATE_INACTIVE; | |
1528 | } | |
1529 | ||
a723968c | 1530 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1531 | { |
1532 | int entry = sizeof(u64); /* value */ | |
1533 | int size = 0; | |
1534 | int nr = 1; | |
1535 | ||
1536 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1537 | size += sizeof(u64); | |
1538 | ||
1539 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1540 | size += sizeof(u64); | |
1541 | ||
1542 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1543 | entry += sizeof(u64); | |
1544 | ||
1545 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1546 | nr += nr_siblings; |
c320c7b7 ACM |
1547 | size += sizeof(u64); |
1548 | } | |
1549 | ||
1550 | size += entry * nr; | |
1551 | event->read_size = size; | |
1552 | } | |
1553 | ||
a723968c | 1554 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1555 | { |
1556 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1557 | u16 size = 0; |
1558 | ||
c320c7b7 ACM |
1559 | if (sample_type & PERF_SAMPLE_IP) |
1560 | size += sizeof(data->ip); | |
1561 | ||
6844c09d ACM |
1562 | if (sample_type & PERF_SAMPLE_ADDR) |
1563 | size += sizeof(data->addr); | |
1564 | ||
1565 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1566 | size += sizeof(data->period); | |
1567 | ||
c3feedf2 AK |
1568 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1569 | size += sizeof(data->weight); | |
1570 | ||
6844c09d ACM |
1571 | if (sample_type & PERF_SAMPLE_READ) |
1572 | size += event->read_size; | |
1573 | ||
d6be9ad6 SE |
1574 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1575 | size += sizeof(data->data_src.val); | |
1576 | ||
fdfbbd07 AK |
1577 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1578 | size += sizeof(data->txn); | |
1579 | ||
6844c09d ACM |
1580 | event->header_size = size; |
1581 | } | |
1582 | ||
a723968c PZ |
1583 | /* |
1584 | * Called at perf_event creation and when events are attached/detached from a | |
1585 | * group. | |
1586 | */ | |
1587 | static void perf_event__header_size(struct perf_event *event) | |
1588 | { | |
1589 | __perf_event_read_size(event, | |
1590 | event->group_leader->nr_siblings); | |
1591 | __perf_event_header_size(event, event->attr.sample_type); | |
1592 | } | |
1593 | ||
6844c09d ACM |
1594 | static void perf_event__id_header_size(struct perf_event *event) |
1595 | { | |
1596 | struct perf_sample_data *data; | |
1597 | u64 sample_type = event->attr.sample_type; | |
1598 | u16 size = 0; | |
1599 | ||
c320c7b7 ACM |
1600 | if (sample_type & PERF_SAMPLE_TID) |
1601 | size += sizeof(data->tid_entry); | |
1602 | ||
1603 | if (sample_type & PERF_SAMPLE_TIME) | |
1604 | size += sizeof(data->time); | |
1605 | ||
ff3d527c AH |
1606 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1607 | size += sizeof(data->id); | |
1608 | ||
c320c7b7 ACM |
1609 | if (sample_type & PERF_SAMPLE_ID) |
1610 | size += sizeof(data->id); | |
1611 | ||
1612 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1613 | size += sizeof(data->stream_id); | |
1614 | ||
1615 | if (sample_type & PERF_SAMPLE_CPU) | |
1616 | size += sizeof(data->cpu_entry); | |
1617 | ||
6844c09d | 1618 | event->id_header_size = size; |
c320c7b7 ACM |
1619 | } |
1620 | ||
a723968c PZ |
1621 | static bool perf_event_validate_size(struct perf_event *event) |
1622 | { | |
1623 | /* | |
1624 | * The values computed here will be over-written when we actually | |
1625 | * attach the event. | |
1626 | */ | |
1627 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1628 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1629 | perf_event__id_header_size(event); | |
1630 | ||
1631 | /* | |
1632 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1633 | * Conservative limit to allow for callchains and other variable fields. | |
1634 | */ | |
1635 | if (event->read_size + event->header_size + | |
1636 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1637 | return false; | |
1638 | ||
1639 | return true; | |
1640 | } | |
1641 | ||
8a49542c PZ |
1642 | static void perf_group_attach(struct perf_event *event) |
1643 | { | |
c320c7b7 | 1644 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1645 | |
a76a82a3 PZ |
1646 | lockdep_assert_held(&event->ctx->lock); |
1647 | ||
74c3337c PZ |
1648 | /* |
1649 | * We can have double attach due to group movement in perf_event_open. | |
1650 | */ | |
1651 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1652 | return; | |
1653 | ||
8a49542c PZ |
1654 | event->attach_state |= PERF_ATTACH_GROUP; |
1655 | ||
1656 | if (group_leader == event) | |
1657 | return; | |
1658 | ||
652884fe PZ |
1659 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1660 | ||
4ff6a8de | 1661 | group_leader->group_caps &= event->event_caps; |
8a49542c PZ |
1662 | |
1663 | list_add_tail(&event->group_entry, &group_leader->sibling_list); | |
1664 | group_leader->nr_siblings++; | |
c320c7b7 ACM |
1665 | |
1666 | perf_event__header_size(group_leader); | |
1667 | ||
1668 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) | |
1669 | perf_event__header_size(pos); | |
8a49542c PZ |
1670 | } |
1671 | ||
a63eaf34 | 1672 | /* |
cdd6c482 | 1673 | * Remove a event from the lists for its context. |
fccc714b | 1674 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1675 | */ |
04289bb9 | 1676 | static void |
cdd6c482 | 1677 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1678 | { |
652884fe PZ |
1679 | WARN_ON_ONCE(event->ctx != ctx); |
1680 | lockdep_assert_held(&ctx->lock); | |
1681 | ||
8a49542c PZ |
1682 | /* |
1683 | * We can have double detach due to exit/hot-unplug + close. | |
1684 | */ | |
1685 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1686 | return; |
8a49542c PZ |
1687 | |
1688 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1689 | ||
db4a8356 | 1690 | list_update_cgroup_event(event, ctx, false); |
e5d1367f | 1691 | |
cdd6c482 IM |
1692 | ctx->nr_events--; |
1693 | if (event->attr.inherit_stat) | |
bfbd3381 | 1694 | ctx->nr_stat--; |
8bc20959 | 1695 | |
cdd6c482 | 1696 | list_del_rcu(&event->event_entry); |
04289bb9 | 1697 | |
8a49542c PZ |
1698 | if (event->group_leader == event) |
1699 | list_del_init(&event->group_entry); | |
5c148194 | 1700 | |
96c21a46 | 1701 | update_group_times(event); |
b2e74a26 SE |
1702 | |
1703 | /* | |
1704 | * If event was in error state, then keep it | |
1705 | * that way, otherwise bogus counts will be | |
1706 | * returned on read(). The only way to get out | |
1707 | * of error state is by explicit re-enabling | |
1708 | * of the event | |
1709 | */ | |
1710 | if (event->state > PERF_EVENT_STATE_OFF) | |
1711 | event->state = PERF_EVENT_STATE_OFF; | |
5a3126d4 PZ |
1712 | |
1713 | ctx->generation++; | |
050735b0 PZ |
1714 | } |
1715 | ||
8a49542c | 1716 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1717 | { |
1718 | struct perf_event *sibling, *tmp; | |
8a49542c PZ |
1719 | struct list_head *list = NULL; |
1720 | ||
a76a82a3 PZ |
1721 | lockdep_assert_held(&event->ctx->lock); |
1722 | ||
8a49542c PZ |
1723 | /* |
1724 | * We can have double detach due to exit/hot-unplug + close. | |
1725 | */ | |
1726 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1727 | return; | |
1728 | ||
1729 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1730 | ||
1731 | /* | |
1732 | * If this is a sibling, remove it from its group. | |
1733 | */ | |
1734 | if (event->group_leader != event) { | |
1735 | list_del_init(&event->group_entry); | |
1736 | event->group_leader->nr_siblings--; | |
c320c7b7 | 1737 | goto out; |
8a49542c PZ |
1738 | } |
1739 | ||
1740 | if (!list_empty(&event->group_entry)) | |
1741 | list = &event->group_entry; | |
2e2af50b | 1742 | |
04289bb9 | 1743 | /* |
cdd6c482 IM |
1744 | * If this was a group event with sibling events then |
1745 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1746 | * to whatever list we are on. |
04289bb9 | 1747 | */ |
cdd6c482 | 1748 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
8a49542c PZ |
1749 | if (list) |
1750 | list_move_tail(&sibling->group_entry, list); | |
04289bb9 | 1751 | sibling->group_leader = sibling; |
d6f962b5 FW |
1752 | |
1753 | /* Inherit group flags from the previous leader */ | |
4ff6a8de | 1754 | sibling->group_caps = event->group_caps; |
652884fe PZ |
1755 | |
1756 | WARN_ON_ONCE(sibling->ctx != event->ctx); | |
04289bb9 | 1757 | } |
c320c7b7 ACM |
1758 | |
1759 | out: | |
1760 | perf_event__header_size(event->group_leader); | |
1761 | ||
1762 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) | |
1763 | perf_event__header_size(tmp); | |
04289bb9 IM |
1764 | } |
1765 | ||
fadfe7be JO |
1766 | static bool is_orphaned_event(struct perf_event *event) |
1767 | { | |
a69b0ca4 | 1768 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1769 | } |
1770 | ||
2c81a647 | 1771 | static inline int __pmu_filter_match(struct perf_event *event) |
66eb579e MR |
1772 | { |
1773 | struct pmu *pmu = event->pmu; | |
1774 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1775 | } | |
1776 | ||
2c81a647 MR |
1777 | /* |
1778 | * Check whether we should attempt to schedule an event group based on | |
1779 | * PMU-specific filtering. An event group can consist of HW and SW events, | |
1780 | * potentially with a SW leader, so we must check all the filters, to | |
1781 | * determine whether a group is schedulable: | |
1782 | */ | |
1783 | static inline int pmu_filter_match(struct perf_event *event) | |
1784 | { | |
1785 | struct perf_event *child; | |
1786 | ||
1787 | if (!__pmu_filter_match(event)) | |
1788 | return 0; | |
1789 | ||
1790 | list_for_each_entry(child, &event->sibling_list, group_entry) { | |
1791 | if (!__pmu_filter_match(child)) | |
1792 | return 0; | |
1793 | } | |
1794 | ||
1795 | return 1; | |
1796 | } | |
1797 | ||
fa66f07a SE |
1798 | static inline int |
1799 | event_filter_match(struct perf_event *event) | |
1800 | { | |
0b8f1e2e PZ |
1801 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
1802 | perf_cgroup_match(event) && pmu_filter_match(event); | |
fa66f07a SE |
1803 | } |
1804 | ||
9ffcfa6f SE |
1805 | static void |
1806 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1807 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1808 | struct perf_event_context *ctx) |
3b6f9e5c | 1809 | { |
4158755d | 1810 | u64 tstamp = perf_event_time(event); |
fa66f07a | 1811 | u64 delta; |
652884fe PZ |
1812 | |
1813 | WARN_ON_ONCE(event->ctx != ctx); | |
1814 | lockdep_assert_held(&ctx->lock); | |
1815 | ||
fa66f07a SE |
1816 | /* |
1817 | * An event which could not be activated because of | |
1818 | * filter mismatch still needs to have its timings | |
1819 | * maintained, otherwise bogus information is return | |
1820 | * via read() for time_enabled, time_running: | |
1821 | */ | |
0b8f1e2e PZ |
1822 | if (event->state == PERF_EVENT_STATE_INACTIVE && |
1823 | !event_filter_match(event)) { | |
e5d1367f | 1824 | delta = tstamp - event->tstamp_stopped; |
fa66f07a | 1825 | event->tstamp_running += delta; |
4158755d | 1826 | event->tstamp_stopped = tstamp; |
fa66f07a SE |
1827 | } |
1828 | ||
cdd6c482 | 1829 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1830 | return; |
3b6f9e5c | 1831 | |
44377277 AS |
1832 | perf_pmu_disable(event->pmu); |
1833 | ||
28a967c3 PZ |
1834 | event->tstamp_stopped = tstamp; |
1835 | event->pmu->del(event, 0); | |
1836 | event->oncpu = -1; | |
cdd6c482 IM |
1837 | event->state = PERF_EVENT_STATE_INACTIVE; |
1838 | if (event->pending_disable) { | |
1839 | event->pending_disable = 0; | |
1840 | event->state = PERF_EVENT_STATE_OFF; | |
970892a9 | 1841 | } |
3b6f9e5c | 1842 | |
cdd6c482 | 1843 | if (!is_software_event(event)) |
3b6f9e5c | 1844 | cpuctx->active_oncpu--; |
2fde4f94 MR |
1845 | if (!--ctx->nr_active) |
1846 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
1847 | if (event->attr.freq && event->attr.sample_freq) |
1848 | ctx->nr_freq--; | |
cdd6c482 | 1849 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 1850 | cpuctx->exclusive = 0; |
44377277 AS |
1851 | |
1852 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
1853 | } |
1854 | ||
d859e29f | 1855 | static void |
cdd6c482 | 1856 | group_sched_out(struct perf_event *group_event, |
d859e29f | 1857 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1858 | struct perf_event_context *ctx) |
d859e29f | 1859 | { |
cdd6c482 | 1860 | struct perf_event *event; |
fa66f07a | 1861 | int state = group_event->state; |
d859e29f | 1862 | |
3f005e7d MR |
1863 | perf_pmu_disable(ctx->pmu); |
1864 | ||
cdd6c482 | 1865 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
1866 | |
1867 | /* | |
1868 | * Schedule out siblings (if any): | |
1869 | */ | |
cdd6c482 IM |
1870 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
1871 | event_sched_out(event, cpuctx, ctx); | |
d859e29f | 1872 | |
3f005e7d MR |
1873 | perf_pmu_enable(ctx->pmu); |
1874 | ||
fa66f07a | 1875 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
d859e29f PM |
1876 | cpuctx->exclusive = 0; |
1877 | } | |
1878 | ||
45a0e07a | 1879 | #define DETACH_GROUP 0x01UL |
0017960f | 1880 | |
0793a61d | 1881 | /* |
cdd6c482 | 1882 | * Cross CPU call to remove a performance event |
0793a61d | 1883 | * |
cdd6c482 | 1884 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
1885 | * remove it from the context list. |
1886 | */ | |
fae3fde6 PZ |
1887 | static void |
1888 | __perf_remove_from_context(struct perf_event *event, | |
1889 | struct perf_cpu_context *cpuctx, | |
1890 | struct perf_event_context *ctx, | |
1891 | void *info) | |
0793a61d | 1892 | { |
45a0e07a | 1893 | unsigned long flags = (unsigned long)info; |
0793a61d | 1894 | |
cdd6c482 | 1895 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 1896 | if (flags & DETACH_GROUP) |
46ce0fe9 | 1897 | perf_group_detach(event); |
cdd6c482 | 1898 | list_del_event(event, ctx); |
39a43640 PZ |
1899 | |
1900 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 1901 | ctx->is_active = 0; |
39a43640 PZ |
1902 | if (ctx->task) { |
1903 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
1904 | cpuctx->task_ctx = NULL; | |
1905 | } | |
64ce3126 | 1906 | } |
0793a61d TG |
1907 | } |
1908 | ||
0793a61d | 1909 | /* |
cdd6c482 | 1910 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 1911 | * |
cdd6c482 IM |
1912 | * If event->ctx is a cloned context, callers must make sure that |
1913 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
1914 | * remains valid. This is OK when called from perf_release since |
1915 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 1916 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 1917 | * context has been detached from its task. |
0793a61d | 1918 | */ |
45a0e07a | 1919 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 1920 | { |
a76a82a3 PZ |
1921 | struct perf_event_context *ctx = event->ctx; |
1922 | ||
1923 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 1924 | |
45a0e07a | 1925 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
a76a82a3 PZ |
1926 | |
1927 | /* | |
1928 | * The above event_function_call() can NO-OP when it hits | |
1929 | * TASK_TOMBSTONE. In that case we must already have been detached | |
1930 | * from the context (by perf_event_exit_event()) but the grouping | |
1931 | * might still be in-tact. | |
1932 | */ | |
1933 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); | |
1934 | if ((flags & DETACH_GROUP) && | |
1935 | (event->attach_state & PERF_ATTACH_GROUP)) { | |
1936 | /* | |
1937 | * Since in that case we cannot possibly be scheduled, simply | |
1938 | * detach now. | |
1939 | */ | |
1940 | raw_spin_lock_irq(&ctx->lock); | |
1941 | perf_group_detach(event); | |
1942 | raw_spin_unlock_irq(&ctx->lock); | |
1943 | } | |
0793a61d TG |
1944 | } |
1945 | ||
d859e29f | 1946 | /* |
cdd6c482 | 1947 | * Cross CPU call to disable a performance event |
d859e29f | 1948 | */ |
fae3fde6 PZ |
1949 | static void __perf_event_disable(struct perf_event *event, |
1950 | struct perf_cpu_context *cpuctx, | |
1951 | struct perf_event_context *ctx, | |
1952 | void *info) | |
7b648018 | 1953 | { |
fae3fde6 PZ |
1954 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
1955 | return; | |
7b648018 | 1956 | |
fae3fde6 PZ |
1957 | update_context_time(ctx); |
1958 | update_cgrp_time_from_event(event); | |
1959 | update_group_times(event); | |
1960 | if (event == event->group_leader) | |
1961 | group_sched_out(event, cpuctx, ctx); | |
1962 | else | |
1963 | event_sched_out(event, cpuctx, ctx); | |
1964 | event->state = PERF_EVENT_STATE_OFF; | |
7b648018 PZ |
1965 | } |
1966 | ||
d859e29f | 1967 | /* |
cdd6c482 | 1968 | * Disable a event. |
c93f7669 | 1969 | * |
cdd6c482 IM |
1970 | * If event->ctx is a cloned context, callers must make sure that |
1971 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 1972 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
1973 | * perf_event_for_each_child or perf_event_for_each because they |
1974 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
1975 | * goes to exit will block in perf_event_exit_event(). |
1976 | * | |
cdd6c482 | 1977 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 1978 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 1979 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 1980 | */ |
f63a8daa | 1981 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 1982 | { |
cdd6c482 | 1983 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 1984 | |
e625cce1 | 1985 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 1986 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 1987 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1988 | return; |
53cfbf59 | 1989 | } |
e625cce1 | 1990 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1991 | |
fae3fde6 PZ |
1992 | event_function_call(event, __perf_event_disable, NULL); |
1993 | } | |
1994 | ||
1995 | void perf_event_disable_local(struct perf_event *event) | |
1996 | { | |
1997 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 1998 | } |
f63a8daa PZ |
1999 | |
2000 | /* | |
2001 | * Strictly speaking kernel users cannot create groups and therefore this | |
2002 | * interface does not need the perf_event_ctx_lock() magic. | |
2003 | */ | |
2004 | void perf_event_disable(struct perf_event *event) | |
2005 | { | |
2006 | struct perf_event_context *ctx; | |
2007 | ||
2008 | ctx = perf_event_ctx_lock(event); | |
2009 | _perf_event_disable(event); | |
2010 | perf_event_ctx_unlock(event, ctx); | |
2011 | } | |
dcfce4a0 | 2012 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2013 | |
5aab90ce JO |
2014 | void perf_event_disable_inatomic(struct perf_event *event) |
2015 | { | |
2016 | event->pending_disable = 1; | |
2017 | irq_work_queue(&event->pending); | |
2018 | } | |
2019 | ||
e5d1367f SE |
2020 | static void perf_set_shadow_time(struct perf_event *event, |
2021 | struct perf_event_context *ctx, | |
2022 | u64 tstamp) | |
2023 | { | |
2024 | /* | |
2025 | * use the correct time source for the time snapshot | |
2026 | * | |
2027 | * We could get by without this by leveraging the | |
2028 | * fact that to get to this function, the caller | |
2029 | * has most likely already called update_context_time() | |
2030 | * and update_cgrp_time_xx() and thus both timestamp | |
2031 | * are identical (or very close). Given that tstamp is, | |
2032 | * already adjusted for cgroup, we could say that: | |
2033 | * tstamp - ctx->timestamp | |
2034 | * is equivalent to | |
2035 | * tstamp - cgrp->timestamp. | |
2036 | * | |
2037 | * Then, in perf_output_read(), the calculation would | |
2038 | * work with no changes because: | |
2039 | * - event is guaranteed scheduled in | |
2040 | * - no scheduled out in between | |
2041 | * - thus the timestamp would be the same | |
2042 | * | |
2043 | * But this is a bit hairy. | |
2044 | * | |
2045 | * So instead, we have an explicit cgroup call to remain | |
2046 | * within the time time source all along. We believe it | |
2047 | * is cleaner and simpler to understand. | |
2048 | */ | |
2049 | if (is_cgroup_event(event)) | |
2050 | perf_cgroup_set_shadow_time(event, tstamp); | |
2051 | else | |
2052 | event->shadow_ctx_time = tstamp - ctx->timestamp; | |
2053 | } | |
2054 | ||
4fe757dd PZ |
2055 | #define MAX_INTERRUPTS (~0ULL) |
2056 | ||
2057 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2058 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2059 | |
235c7fc7 | 2060 | static int |
9ffcfa6f | 2061 | event_sched_in(struct perf_event *event, |
235c7fc7 | 2062 | struct perf_cpu_context *cpuctx, |
6e37738a | 2063 | struct perf_event_context *ctx) |
235c7fc7 | 2064 | { |
4158755d | 2065 | u64 tstamp = perf_event_time(event); |
44377277 | 2066 | int ret = 0; |
4158755d | 2067 | |
63342411 PZ |
2068 | lockdep_assert_held(&ctx->lock); |
2069 | ||
cdd6c482 | 2070 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2071 | return 0; |
2072 | ||
95ff4ca2 AS |
2073 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2074 | /* | |
2075 | * Order event::oncpu write to happen before the ACTIVE state | |
2076 | * is visible. | |
2077 | */ | |
2078 | smp_wmb(); | |
2079 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); | |
4fe757dd PZ |
2080 | |
2081 | /* | |
2082 | * Unthrottle events, since we scheduled we might have missed several | |
2083 | * ticks already, also for a heavily scheduling task there is little | |
2084 | * guarantee it'll get a tick in a timely manner. | |
2085 | */ | |
2086 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2087 | perf_log_throttle(event, 1); | |
2088 | event->hw.interrupts = 0; | |
2089 | } | |
2090 | ||
235c7fc7 IM |
2091 | /* |
2092 | * The new state must be visible before we turn it on in the hardware: | |
2093 | */ | |
2094 | smp_wmb(); | |
2095 | ||
44377277 AS |
2096 | perf_pmu_disable(event->pmu); |
2097 | ||
72f669c0 SL |
2098 | perf_set_shadow_time(event, ctx, tstamp); |
2099 | ||
ec0d7729 AS |
2100 | perf_log_itrace_start(event); |
2101 | ||
a4eaf7f1 | 2102 | if (event->pmu->add(event, PERF_EF_START)) { |
cdd6c482 IM |
2103 | event->state = PERF_EVENT_STATE_INACTIVE; |
2104 | event->oncpu = -1; | |
44377277 AS |
2105 | ret = -EAGAIN; |
2106 | goto out; | |
235c7fc7 IM |
2107 | } |
2108 | ||
00a2916f PZ |
2109 | event->tstamp_running += tstamp - event->tstamp_stopped; |
2110 | ||
cdd6c482 | 2111 | if (!is_software_event(event)) |
3b6f9e5c | 2112 | cpuctx->active_oncpu++; |
2fde4f94 MR |
2113 | if (!ctx->nr_active++) |
2114 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
2115 | if (event->attr.freq && event->attr.sample_freq) |
2116 | ctx->nr_freq++; | |
235c7fc7 | 2117 | |
cdd6c482 | 2118 | if (event->attr.exclusive) |
3b6f9e5c PM |
2119 | cpuctx->exclusive = 1; |
2120 | ||
44377277 AS |
2121 | out: |
2122 | perf_pmu_enable(event->pmu); | |
2123 | ||
2124 | return ret; | |
235c7fc7 IM |
2125 | } |
2126 | ||
6751b71e | 2127 | static int |
cdd6c482 | 2128 | group_sched_in(struct perf_event *group_event, |
6751b71e | 2129 | struct perf_cpu_context *cpuctx, |
6e37738a | 2130 | struct perf_event_context *ctx) |
6751b71e | 2131 | { |
6bde9b6c | 2132 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 2133 | struct pmu *pmu = ctx->pmu; |
d7842da4 SE |
2134 | u64 now = ctx->time; |
2135 | bool simulate = false; | |
6751b71e | 2136 | |
cdd6c482 | 2137 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2138 | return 0; |
2139 | ||
fbbe0701 | 2140 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2141 | |
9ffcfa6f | 2142 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2143 | pmu->cancel_txn(pmu); |
272325c4 | 2144 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2145 | return -EAGAIN; |
90151c35 | 2146 | } |
6751b71e PM |
2147 | |
2148 | /* | |
2149 | * Schedule in siblings as one group (if any): | |
2150 | */ | |
cdd6c482 | 2151 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
9ffcfa6f | 2152 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2153 | partial_group = event; |
6751b71e PM |
2154 | goto group_error; |
2155 | } | |
2156 | } | |
2157 | ||
9ffcfa6f | 2158 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2159 | return 0; |
9ffcfa6f | 2160 | |
6751b71e PM |
2161 | group_error: |
2162 | /* | |
2163 | * Groups can be scheduled in as one unit only, so undo any | |
2164 | * partial group before returning: | |
d7842da4 SE |
2165 | * The events up to the failed event are scheduled out normally, |
2166 | * tstamp_stopped will be updated. | |
2167 | * | |
2168 | * The failed events and the remaining siblings need to have | |
2169 | * their timings updated as if they had gone thru event_sched_in() | |
2170 | * and event_sched_out(). This is required to get consistent timings | |
2171 | * across the group. This also takes care of the case where the group | |
2172 | * could never be scheduled by ensuring tstamp_stopped is set to mark | |
2173 | * the time the event was actually stopped, such that time delta | |
2174 | * calculation in update_event_times() is correct. | |
6751b71e | 2175 | */ |
cdd6c482 IM |
2176 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
2177 | if (event == partial_group) | |
d7842da4 SE |
2178 | simulate = true; |
2179 | ||
2180 | if (simulate) { | |
2181 | event->tstamp_running += now - event->tstamp_stopped; | |
2182 | event->tstamp_stopped = now; | |
2183 | } else { | |
2184 | event_sched_out(event, cpuctx, ctx); | |
2185 | } | |
6751b71e | 2186 | } |
9ffcfa6f | 2187 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2188 | |
ad5133b7 | 2189 | pmu->cancel_txn(pmu); |
90151c35 | 2190 | |
272325c4 | 2191 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2192 | |
6751b71e PM |
2193 | return -EAGAIN; |
2194 | } | |
2195 | ||
3b6f9e5c | 2196 | /* |
cdd6c482 | 2197 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2198 | */ |
cdd6c482 | 2199 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2200 | struct perf_cpu_context *cpuctx, |
2201 | int can_add_hw) | |
2202 | { | |
2203 | /* | |
cdd6c482 | 2204 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2205 | */ |
4ff6a8de | 2206 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2207 | return 1; |
2208 | /* | |
2209 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2210 | * events can go on. |
3b6f9e5c PM |
2211 | */ |
2212 | if (cpuctx->exclusive) | |
2213 | return 0; | |
2214 | /* | |
2215 | * If this group is exclusive and there are already | |
cdd6c482 | 2216 | * events on the CPU, it can't go on. |
3b6f9e5c | 2217 | */ |
cdd6c482 | 2218 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2219 | return 0; |
2220 | /* | |
2221 | * Otherwise, try to add it if all previous groups were able | |
2222 | * to go on. | |
2223 | */ | |
2224 | return can_add_hw; | |
2225 | } | |
2226 | ||
9b231d9f PZ |
2227 | /* |
2228 | * Complement to update_event_times(). This computes the tstamp_* values to | |
2229 | * continue 'enabled' state from @now, and effectively discards the time | |
2230 | * between the prior tstamp_stopped and now (as we were in the OFF state, or | |
2231 | * just switched (context) time base). | |
2232 | * | |
2233 | * This further assumes '@event->state == INACTIVE' (we just came from OFF) and | |
2234 | * cannot have been scheduled in yet. And going into INACTIVE state means | |
2235 | * '@event->tstamp_stopped = @now'. | |
2236 | * | |
2237 | * Thus given the rules of update_event_times(): | |
2238 | * | |
2239 | * total_time_enabled = tstamp_stopped - tstamp_enabled | |
2240 | * total_time_running = tstamp_stopped - tstamp_running | |
2241 | * | |
2242 | * We can insert 'tstamp_stopped == now' and reverse them to compute new | |
2243 | * tstamp_* values. | |
2244 | */ | |
2245 | static void __perf_event_enable_time(struct perf_event *event, u64 now) | |
2246 | { | |
2247 | WARN_ON_ONCE(event->state != PERF_EVENT_STATE_INACTIVE); | |
2248 | ||
2249 | event->tstamp_stopped = now; | |
2250 | event->tstamp_enabled = now - event->total_time_enabled; | |
2251 | event->tstamp_running = now - event->total_time_running; | |
2252 | } | |
2253 | ||
cdd6c482 IM |
2254 | static void add_event_to_ctx(struct perf_event *event, |
2255 | struct perf_event_context *ctx) | |
53cfbf59 | 2256 | { |
4158755d SE |
2257 | u64 tstamp = perf_event_time(event); |
2258 | ||
cdd6c482 | 2259 | list_add_event(event, ctx); |
8a49542c | 2260 | perf_group_attach(event); |
9b231d9f PZ |
2261 | /* |
2262 | * We can be called with event->state == STATE_OFF when we create with | |
2263 | * .disabled = 1. In that case the IOC_ENABLE will call this function. | |
2264 | */ | |
2265 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
2266 | __perf_event_enable_time(event, tstamp); | |
53cfbf59 PM |
2267 | } |
2268 | ||
bd2afa49 PZ |
2269 | static void ctx_sched_out(struct perf_event_context *ctx, |
2270 | struct perf_cpu_context *cpuctx, | |
2271 | enum event_type_t event_type); | |
2c29ef0f PZ |
2272 | static void |
2273 | ctx_sched_in(struct perf_event_context *ctx, | |
2274 | struct perf_cpu_context *cpuctx, | |
2275 | enum event_type_t event_type, | |
2276 | struct task_struct *task); | |
fe4b04fa | 2277 | |
bd2afa49 | 2278 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2279 | struct perf_event_context *ctx, |
2280 | enum event_type_t event_type) | |
bd2afa49 PZ |
2281 | { |
2282 | if (!cpuctx->task_ctx) | |
2283 | return; | |
2284 | ||
2285 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2286 | return; | |
2287 | ||
487f05e1 | 2288 | ctx_sched_out(ctx, cpuctx, event_type); |
bd2afa49 PZ |
2289 | } |
2290 | ||
dce5855b PZ |
2291 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2292 | struct perf_event_context *ctx, | |
2293 | struct task_struct *task) | |
2294 | { | |
2295 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2296 | if (ctx) | |
2297 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2298 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2299 | if (ctx) | |
2300 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2301 | } | |
2302 | ||
487f05e1 AS |
2303 | /* |
2304 | * We want to maintain the following priority of scheduling: | |
2305 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2306 | * - task pinned (EVENT_PINNED) | |
2307 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2308 | * - task flexible (EVENT_FLEXIBLE). | |
2309 | * | |
2310 | * In order to avoid unscheduling and scheduling back in everything every | |
2311 | * time an event is added, only do it for the groups of equal priority and | |
2312 | * below. | |
2313 | * | |
2314 | * This can be called after a batch operation on task events, in which case | |
2315 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2316 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2317 | */ | |
3e349507 | 2318 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2319 | struct perf_event_context *task_ctx, |
2320 | enum event_type_t event_type) | |
0017960f | 2321 | { |
487f05e1 AS |
2322 | enum event_type_t ctx_event_type = event_type & EVENT_ALL; |
2323 | bool cpu_event = !!(event_type & EVENT_CPU); | |
2324 | ||
2325 | /* | |
2326 | * If pinned groups are involved, flexible groups also need to be | |
2327 | * scheduled out. | |
2328 | */ | |
2329 | if (event_type & EVENT_PINNED) | |
2330 | event_type |= EVENT_FLEXIBLE; | |
2331 | ||
3e349507 PZ |
2332 | perf_pmu_disable(cpuctx->ctx.pmu); |
2333 | if (task_ctx) | |
487f05e1 AS |
2334 | task_ctx_sched_out(cpuctx, task_ctx, event_type); |
2335 | ||
2336 | /* | |
2337 | * Decide which cpu ctx groups to schedule out based on the types | |
2338 | * of events that caused rescheduling: | |
2339 | * - EVENT_CPU: schedule out corresponding groups; | |
2340 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2341 | * - otherwise, do nothing more. | |
2342 | */ | |
2343 | if (cpu_event) | |
2344 | cpu_ctx_sched_out(cpuctx, ctx_event_type); | |
2345 | else if (ctx_event_type & EVENT_PINNED) | |
2346 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
2347 | ||
3e349507 PZ |
2348 | perf_event_sched_in(cpuctx, task_ctx, current); |
2349 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2350 | } |
2351 | ||
0793a61d | 2352 | /* |
cdd6c482 | 2353 | * Cross CPU call to install and enable a performance event |
682076ae | 2354 | * |
a096309b PZ |
2355 | * Very similar to remote_function() + event_function() but cannot assume that |
2356 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2357 | */ |
fe4b04fa | 2358 | static int __perf_install_in_context(void *info) |
0793a61d | 2359 | { |
a096309b PZ |
2360 | struct perf_event *event = info; |
2361 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2362 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2363 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2364 | bool reprogram = true; |
a096309b | 2365 | int ret = 0; |
0793a61d | 2366 | |
63b6da39 | 2367 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2368 | if (ctx->task) { |
b58f6b0d PZ |
2369 | raw_spin_lock(&ctx->lock); |
2370 | task_ctx = ctx; | |
a096309b | 2371 | |
63cae12b | 2372 | reprogram = (ctx->task == current); |
b58f6b0d | 2373 | |
39a43640 | 2374 | /* |
63cae12b PZ |
2375 | * If the task is running, it must be running on this CPU, |
2376 | * otherwise we cannot reprogram things. | |
2377 | * | |
2378 | * If its not running, we don't care, ctx->lock will | |
2379 | * serialize against it becoming runnable. | |
39a43640 | 2380 | */ |
63cae12b PZ |
2381 | if (task_curr(ctx->task) && !reprogram) { |
2382 | ret = -ESRCH; | |
2383 | goto unlock; | |
2384 | } | |
a096309b | 2385 | |
63cae12b | 2386 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2387 | } else if (task_ctx) { |
2388 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2389 | } |
b58f6b0d | 2390 | |
63cae12b | 2391 | if (reprogram) { |
a096309b PZ |
2392 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
2393 | add_event_to_ctx(event, ctx); | |
487f05e1 | 2394 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2395 | } else { |
2396 | add_event_to_ctx(event, ctx); | |
2397 | } | |
2398 | ||
63b6da39 | 2399 | unlock: |
2c29ef0f | 2400 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2401 | |
a096309b | 2402 | return ret; |
0793a61d TG |
2403 | } |
2404 | ||
2405 | /* | |
a096309b PZ |
2406 | * Attach a performance event to a context. |
2407 | * | |
2408 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2409 | */ |
2410 | static void | |
cdd6c482 IM |
2411 | perf_install_in_context(struct perf_event_context *ctx, |
2412 | struct perf_event *event, | |
0793a61d TG |
2413 | int cpu) |
2414 | { | |
a096309b | 2415 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2416 | |
fe4b04fa PZ |
2417 | lockdep_assert_held(&ctx->mutex); |
2418 | ||
0cda4c02 YZ |
2419 | if (event->cpu != -1) |
2420 | event->cpu = cpu; | |
c3f00c70 | 2421 | |
0b8f1e2e PZ |
2422 | /* |
2423 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2424 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2425 | */ | |
2426 | smp_store_release(&event->ctx, ctx); | |
2427 | ||
a096309b PZ |
2428 | if (!task) { |
2429 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2430 | return; | |
2431 | } | |
2432 | ||
2433 | /* | |
2434 | * Should not happen, we validate the ctx is still alive before calling. | |
2435 | */ | |
2436 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2437 | return; | |
2438 | ||
39a43640 PZ |
2439 | /* |
2440 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2441 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2442 | * |
2443 | * Instead we use task_curr(), which tells us if the task is running. | |
2444 | * However, since we use task_curr() outside of rq::lock, we can race | |
2445 | * against the actual state. This means the result can be wrong. | |
2446 | * | |
2447 | * If we get a false positive, we retry, this is harmless. | |
2448 | * | |
2449 | * If we get a false negative, things are complicated. If we are after | |
2450 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2451 | * value must be correct. If we're before, it doesn't matter since | |
2452 | * perf_event_context_sched_in() will program the counter. | |
2453 | * | |
2454 | * However, this hinges on the remote context switch having observed | |
2455 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2456 | * ctx::lock in perf_event_context_sched_in(). | |
2457 | * | |
2458 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2459 | * we know any future context switch of task must see the | |
2460 | * perf_event_ctpx[] store. | |
39a43640 | 2461 | */ |
63cae12b | 2462 | |
63b6da39 | 2463 | /* |
63cae12b PZ |
2464 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2465 | * task_cpu() load, such that if the IPI then does not find the task | |
2466 | * running, a future context switch of that task must observe the | |
2467 | * store. | |
63b6da39 | 2468 | */ |
63cae12b PZ |
2469 | smp_mb(); |
2470 | again: | |
2471 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2472 | return; |
2473 | ||
2474 | raw_spin_lock_irq(&ctx->lock); | |
2475 | task = ctx->task; | |
84c4e620 | 2476 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2477 | /* |
2478 | * Cannot happen because we already checked above (which also | |
2479 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2480 | * against perf_event_exit_task_context(). | |
2481 | */ | |
63b6da39 PZ |
2482 | raw_spin_unlock_irq(&ctx->lock); |
2483 | return; | |
2484 | } | |
39a43640 | 2485 | /* |
63cae12b PZ |
2486 | * If the task is not running, ctx->lock will avoid it becoming so, |
2487 | * thus we can safely install the event. | |
39a43640 | 2488 | */ |
63cae12b PZ |
2489 | if (task_curr(task)) { |
2490 | raw_spin_unlock_irq(&ctx->lock); | |
2491 | goto again; | |
2492 | } | |
2493 | add_event_to_ctx(event, ctx); | |
2494 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2495 | } |
2496 | ||
fa289bec | 2497 | /* |
cdd6c482 | 2498 | * Put a event into inactive state and update time fields. |
fa289bec PM |
2499 | * Enabling the leader of a group effectively enables all |
2500 | * the group members that aren't explicitly disabled, so we | |
2501 | * have to update their ->tstamp_enabled also. | |
2502 | * Note: this works for group members as well as group leaders | |
2503 | * since the non-leader members' sibling_lists will be empty. | |
2504 | */ | |
1d9b482e | 2505 | static void __perf_event_mark_enabled(struct perf_event *event) |
fa289bec | 2506 | { |
cdd6c482 | 2507 | struct perf_event *sub; |
4158755d | 2508 | u64 tstamp = perf_event_time(event); |
fa289bec | 2509 | |
cdd6c482 | 2510 | event->state = PERF_EVENT_STATE_INACTIVE; |
9b231d9f | 2511 | __perf_event_enable_time(event, tstamp); |
9ed6060d | 2512 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
9b231d9f | 2513 | /* XXX should not be > INACTIVE if event isn't */ |
4158755d | 2514 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
9b231d9f | 2515 | __perf_event_enable_time(sub, tstamp); |
9ed6060d | 2516 | } |
fa289bec PM |
2517 | } |
2518 | ||
d859e29f | 2519 | /* |
cdd6c482 | 2520 | * Cross CPU call to enable a performance event |
d859e29f | 2521 | */ |
fae3fde6 PZ |
2522 | static void __perf_event_enable(struct perf_event *event, |
2523 | struct perf_cpu_context *cpuctx, | |
2524 | struct perf_event_context *ctx, | |
2525 | void *info) | |
04289bb9 | 2526 | { |
cdd6c482 | 2527 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2528 | struct perf_event_context *task_ctx; |
04289bb9 | 2529 | |
6e801e01 PZ |
2530 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2531 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2532 | return; |
3cbed429 | 2533 | |
bd2afa49 PZ |
2534 | if (ctx->is_active) |
2535 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2536 | ||
1d9b482e | 2537 | __perf_event_mark_enabled(event); |
04289bb9 | 2538 | |
fae3fde6 PZ |
2539 | if (!ctx->is_active) |
2540 | return; | |
2541 | ||
e5d1367f | 2542 | if (!event_filter_match(event)) { |
bd2afa49 | 2543 | if (is_cgroup_event(event)) |
e5d1367f | 2544 | perf_cgroup_defer_enabled(event); |
bd2afa49 | 2545 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2546 | return; |
e5d1367f | 2547 | } |
f4c4176f | 2548 | |
04289bb9 | 2549 | /* |
cdd6c482 | 2550 | * If the event is in a group and isn't the group leader, |
d859e29f | 2551 | * then don't put it on unless the group is on. |
04289bb9 | 2552 | */ |
bd2afa49 PZ |
2553 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2554 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2555 | return; |
bd2afa49 | 2556 | } |
fe4b04fa | 2557 | |
fae3fde6 PZ |
2558 | task_ctx = cpuctx->task_ctx; |
2559 | if (ctx->task) | |
2560 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2561 | |
487f05e1 | 2562 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2563 | } |
2564 | ||
d859e29f | 2565 | /* |
cdd6c482 | 2566 | * Enable a event. |
c93f7669 | 2567 | * |
cdd6c482 IM |
2568 | * If event->ctx is a cloned context, callers must make sure that |
2569 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2570 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2571 | * perf_event_for_each_child or perf_event_for_each as described |
2572 | * for perf_event_disable. | |
d859e29f | 2573 | */ |
f63a8daa | 2574 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2575 | { |
cdd6c482 | 2576 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2577 | |
7b648018 | 2578 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2579 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2580 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2581 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2582 | return; |
2583 | } | |
2584 | ||
d859e29f | 2585 | /* |
cdd6c482 | 2586 | * If the event is in error state, clear that first. |
7b648018 PZ |
2587 | * |
2588 | * That way, if we see the event in error state below, we know that it | |
2589 | * has gone back into error state, as distinct from the task having | |
2590 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2591 | */ |
cdd6c482 IM |
2592 | if (event->state == PERF_EVENT_STATE_ERROR) |
2593 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2594 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2595 | |
fae3fde6 | 2596 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2597 | } |
f63a8daa PZ |
2598 | |
2599 | /* | |
2600 | * See perf_event_disable(); | |
2601 | */ | |
2602 | void perf_event_enable(struct perf_event *event) | |
2603 | { | |
2604 | struct perf_event_context *ctx; | |
2605 | ||
2606 | ctx = perf_event_ctx_lock(event); | |
2607 | _perf_event_enable(event); | |
2608 | perf_event_ctx_unlock(event, ctx); | |
2609 | } | |
dcfce4a0 | 2610 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2611 | |
375637bc AS |
2612 | struct stop_event_data { |
2613 | struct perf_event *event; | |
2614 | unsigned int restart; | |
2615 | }; | |
2616 | ||
95ff4ca2 AS |
2617 | static int __perf_event_stop(void *info) |
2618 | { | |
375637bc AS |
2619 | struct stop_event_data *sd = info; |
2620 | struct perf_event *event = sd->event; | |
95ff4ca2 | 2621 | |
375637bc | 2622 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
2623 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
2624 | return 0; | |
2625 | ||
2626 | /* matches smp_wmb() in event_sched_in() */ | |
2627 | smp_rmb(); | |
2628 | ||
2629 | /* | |
2630 | * There is a window with interrupts enabled before we get here, | |
2631 | * so we need to check again lest we try to stop another CPU's event. | |
2632 | */ | |
2633 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2634 | return -EAGAIN; | |
2635 | ||
2636 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2637 | ||
375637bc AS |
2638 | /* |
2639 | * May race with the actual stop (through perf_pmu_output_stop()), | |
2640 | * but it is only used for events with AUX ring buffer, and such | |
2641 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
2642 | * see comments in perf_aux_output_begin(). | |
2643 | * | |
2644 | * Since this is happening on a event-local CPU, no trace is lost | |
2645 | * while restarting. | |
2646 | */ | |
2647 | if (sd->restart) | |
c9bbdd48 | 2648 | event->pmu->start(event, 0); |
375637bc | 2649 | |
95ff4ca2 AS |
2650 | return 0; |
2651 | } | |
2652 | ||
767ae086 | 2653 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
2654 | { |
2655 | struct stop_event_data sd = { | |
2656 | .event = event, | |
767ae086 | 2657 | .restart = restart, |
375637bc AS |
2658 | }; |
2659 | int ret = 0; | |
2660 | ||
2661 | do { | |
2662 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2663 | return 0; | |
2664 | ||
2665 | /* matches smp_wmb() in event_sched_in() */ | |
2666 | smp_rmb(); | |
2667 | ||
2668 | /* | |
2669 | * We only want to restart ACTIVE events, so if the event goes | |
2670 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
2671 | * fall through with ret==-ENXIO. | |
2672 | */ | |
2673 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
2674 | __perf_event_stop, &sd); | |
2675 | } while (ret == -EAGAIN); | |
2676 | ||
2677 | return ret; | |
2678 | } | |
2679 | ||
2680 | /* | |
2681 | * In order to contain the amount of racy and tricky in the address filter | |
2682 | * configuration management, it is a two part process: | |
2683 | * | |
2684 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
2685 | * we update the addresses of corresponding vmas in | |
2686 | * event::addr_filters_offs array and bump the event::addr_filters_gen; | |
2687 | * (p2) when an event is scheduled in (pmu::add), it calls | |
2688 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
2689 | * if the generation has changed since the previous call. | |
2690 | * | |
2691 | * If (p1) happens while the event is active, we restart it to force (p2). | |
2692 | * | |
2693 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
2694 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
2695 | * ioctl; | |
2696 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
2697 | * registered mapping, called for every new mmap(), with mm::mmap_sem down | |
2698 | * for reading; | |
2699 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
2700 | * of exec. | |
2701 | */ | |
2702 | void perf_event_addr_filters_sync(struct perf_event *event) | |
2703 | { | |
2704 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
2705 | ||
2706 | if (!has_addr_filter(event)) | |
2707 | return; | |
2708 | ||
2709 | raw_spin_lock(&ifh->lock); | |
2710 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
2711 | event->pmu->addr_filters_sync(event); | |
2712 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
2713 | } | |
2714 | raw_spin_unlock(&ifh->lock); | |
2715 | } | |
2716 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
2717 | ||
f63a8daa | 2718 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2719 | { |
2023b359 | 2720 | /* |
cdd6c482 | 2721 | * not supported on inherited events |
2023b359 | 2722 | */ |
2e939d1d | 2723 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2724 | return -EINVAL; |
2725 | ||
cdd6c482 | 2726 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2727 | _perf_event_enable(event); |
2023b359 PZ |
2728 | |
2729 | return 0; | |
79f14641 | 2730 | } |
f63a8daa PZ |
2731 | |
2732 | /* | |
2733 | * See perf_event_disable() | |
2734 | */ | |
2735 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2736 | { | |
2737 | struct perf_event_context *ctx; | |
2738 | int ret; | |
2739 | ||
2740 | ctx = perf_event_ctx_lock(event); | |
2741 | ret = _perf_event_refresh(event, refresh); | |
2742 | perf_event_ctx_unlock(event, ctx); | |
2743 | ||
2744 | return ret; | |
2745 | } | |
26ca5c11 | 2746 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2747 | |
5b0311e1 FW |
2748 | static void ctx_sched_out(struct perf_event_context *ctx, |
2749 | struct perf_cpu_context *cpuctx, | |
2750 | enum event_type_t event_type) | |
235c7fc7 | 2751 | { |
db24d33e | 2752 | int is_active = ctx->is_active; |
c994d613 | 2753 | struct perf_event *event; |
235c7fc7 | 2754 | |
c994d613 | 2755 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2756 | |
39a43640 PZ |
2757 | if (likely(!ctx->nr_events)) { |
2758 | /* | |
2759 | * See __perf_remove_from_context(). | |
2760 | */ | |
2761 | WARN_ON_ONCE(ctx->is_active); | |
2762 | if (ctx->task) | |
2763 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2764 | return; |
39a43640 PZ |
2765 | } |
2766 | ||
db24d33e | 2767 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2768 | if (!(ctx->is_active & EVENT_ALL)) |
2769 | ctx->is_active = 0; | |
2770 | ||
63e30d3e PZ |
2771 | if (ctx->task) { |
2772 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2773 | if (!ctx->is_active) | |
2774 | cpuctx->task_ctx = NULL; | |
2775 | } | |
facc4307 | 2776 | |
8fdc6539 PZ |
2777 | /* |
2778 | * Always update time if it was set; not only when it changes. | |
2779 | * Otherwise we can 'forget' to update time for any but the last | |
2780 | * context we sched out. For example: | |
2781 | * | |
2782 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2783 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2784 | * | |
2785 | * would only update time for the pinned events. | |
2786 | */ | |
3cbaa590 PZ |
2787 | if (is_active & EVENT_TIME) { |
2788 | /* update (and stop) ctx time */ | |
2789 | update_context_time(ctx); | |
2790 | update_cgrp_time_from_cpuctx(cpuctx); | |
2791 | } | |
2792 | ||
8fdc6539 PZ |
2793 | is_active ^= ctx->is_active; /* changed bits */ |
2794 | ||
3cbaa590 | 2795 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2796 | return; |
5b0311e1 | 2797 | |
075e0b00 | 2798 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2799 | if (is_active & EVENT_PINNED) { |
889ff015 FW |
2800 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
2801 | group_sched_out(event, cpuctx, ctx); | |
9ed6060d | 2802 | } |
889ff015 | 2803 | |
3cbaa590 | 2804 | if (is_active & EVENT_FLEXIBLE) { |
889ff015 | 2805 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
8c9ed8e1 | 2806 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2807 | } |
1b9a644f | 2808 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2809 | } |
2810 | ||
564c2b21 | 2811 | /* |
5a3126d4 PZ |
2812 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2813 | * cloned from the same version of the same context. | |
2814 | * | |
2815 | * Equivalence is measured using a generation number in the context that is | |
2816 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2817 | * and list_del_event(). | |
564c2b21 | 2818 | */ |
cdd6c482 IM |
2819 | static int context_equiv(struct perf_event_context *ctx1, |
2820 | struct perf_event_context *ctx2) | |
564c2b21 | 2821 | { |
211de6eb PZ |
2822 | lockdep_assert_held(&ctx1->lock); |
2823 | lockdep_assert_held(&ctx2->lock); | |
2824 | ||
5a3126d4 PZ |
2825 | /* Pinning disables the swap optimization */ |
2826 | if (ctx1->pin_count || ctx2->pin_count) | |
2827 | return 0; | |
2828 | ||
2829 | /* If ctx1 is the parent of ctx2 */ | |
2830 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2831 | return 1; | |
2832 | ||
2833 | /* If ctx2 is the parent of ctx1 */ | |
2834 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2835 | return 1; | |
2836 | ||
2837 | /* | |
2838 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2839 | * hierarchy, see perf_event_init_context(). | |
2840 | */ | |
2841 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2842 | ctx1->parent_gen == ctx2->parent_gen) | |
2843 | return 1; | |
2844 | ||
2845 | /* Unmatched */ | |
2846 | return 0; | |
564c2b21 PM |
2847 | } |
2848 | ||
cdd6c482 IM |
2849 | static void __perf_event_sync_stat(struct perf_event *event, |
2850 | struct perf_event *next_event) | |
bfbd3381 PZ |
2851 | { |
2852 | u64 value; | |
2853 | ||
cdd6c482 | 2854 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2855 | return; |
2856 | ||
2857 | /* | |
cdd6c482 | 2858 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2859 | * because we're in the middle of a context switch and have IRQs |
2860 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2861 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2862 | * don't need to use it. |
2863 | */ | |
cdd6c482 IM |
2864 | switch (event->state) { |
2865 | case PERF_EVENT_STATE_ACTIVE: | |
3dbebf15 PZ |
2866 | event->pmu->read(event); |
2867 | /* fall-through */ | |
bfbd3381 | 2868 | |
cdd6c482 IM |
2869 | case PERF_EVENT_STATE_INACTIVE: |
2870 | update_event_times(event); | |
bfbd3381 PZ |
2871 | break; |
2872 | ||
2873 | default: | |
2874 | break; | |
2875 | } | |
2876 | ||
2877 | /* | |
cdd6c482 | 2878 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2879 | * values when we flip the contexts. |
2880 | */ | |
e7850595 PZ |
2881 | value = local64_read(&next_event->count); |
2882 | value = local64_xchg(&event->count, value); | |
2883 | local64_set(&next_event->count, value); | |
bfbd3381 | 2884 | |
cdd6c482 IM |
2885 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2886 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2887 | |
bfbd3381 | 2888 | /* |
19d2e755 | 2889 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2890 | */ |
cdd6c482 IM |
2891 | perf_event_update_userpage(event); |
2892 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2893 | } |
2894 | ||
cdd6c482 IM |
2895 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2896 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2897 | { |
cdd6c482 | 2898 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2899 | |
2900 | if (!ctx->nr_stat) | |
2901 | return; | |
2902 | ||
02ffdbc8 PZ |
2903 | update_context_time(ctx); |
2904 | ||
cdd6c482 IM |
2905 | event = list_first_entry(&ctx->event_list, |
2906 | struct perf_event, event_entry); | |
bfbd3381 | 2907 | |
cdd6c482 IM |
2908 | next_event = list_first_entry(&next_ctx->event_list, |
2909 | struct perf_event, event_entry); | |
bfbd3381 | 2910 | |
cdd6c482 IM |
2911 | while (&event->event_entry != &ctx->event_list && |
2912 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 2913 | |
cdd6c482 | 2914 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 2915 | |
cdd6c482 IM |
2916 | event = list_next_entry(event, event_entry); |
2917 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
2918 | } |
2919 | } | |
2920 | ||
fe4b04fa PZ |
2921 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
2922 | struct task_struct *next) | |
0793a61d | 2923 | { |
8dc85d54 | 2924 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 2925 | struct perf_event_context *next_ctx; |
5a3126d4 | 2926 | struct perf_event_context *parent, *next_parent; |
108b02cf | 2927 | struct perf_cpu_context *cpuctx; |
c93f7669 | 2928 | int do_switch = 1; |
0793a61d | 2929 | |
108b02cf PZ |
2930 | if (likely(!ctx)) |
2931 | return; | |
10989fb2 | 2932 | |
108b02cf PZ |
2933 | cpuctx = __get_cpu_context(ctx); |
2934 | if (!cpuctx->task_ctx) | |
0793a61d TG |
2935 | return; |
2936 | ||
c93f7669 | 2937 | rcu_read_lock(); |
8dc85d54 | 2938 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
2939 | if (!next_ctx) |
2940 | goto unlock; | |
2941 | ||
2942 | parent = rcu_dereference(ctx->parent_ctx); | |
2943 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
2944 | ||
2945 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 2946 | if (!parent && !next_parent) |
5a3126d4 PZ |
2947 | goto unlock; |
2948 | ||
2949 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
2950 | /* |
2951 | * Looks like the two contexts are clones, so we might be | |
2952 | * able to optimize the context switch. We lock both | |
2953 | * contexts and check that they are clones under the | |
2954 | * lock (including re-checking that neither has been | |
2955 | * uncloned in the meantime). It doesn't matter which | |
2956 | * order we take the locks because no other cpu could | |
2957 | * be trying to lock both of these tasks. | |
2958 | */ | |
e625cce1 TG |
2959 | raw_spin_lock(&ctx->lock); |
2960 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 2961 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
2962 | WRITE_ONCE(ctx->task, next); |
2963 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
2964 | |
2965 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
2966 | ||
63b6da39 PZ |
2967 | /* |
2968 | * RCU_INIT_POINTER here is safe because we've not | |
2969 | * modified the ctx and the above modification of | |
2970 | * ctx->task and ctx->task_ctx_data are immaterial | |
2971 | * since those values are always verified under | |
2972 | * ctx->lock which we're now holding. | |
2973 | */ | |
2974 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
2975 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
2976 | ||
c93f7669 | 2977 | do_switch = 0; |
bfbd3381 | 2978 | |
cdd6c482 | 2979 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 2980 | } |
e625cce1 TG |
2981 | raw_spin_unlock(&next_ctx->lock); |
2982 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 2983 | } |
5a3126d4 | 2984 | unlock: |
c93f7669 | 2985 | rcu_read_unlock(); |
564c2b21 | 2986 | |
c93f7669 | 2987 | if (do_switch) { |
facc4307 | 2988 | raw_spin_lock(&ctx->lock); |
487f05e1 | 2989 | task_ctx_sched_out(cpuctx, ctx, EVENT_ALL); |
facc4307 | 2990 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 2991 | } |
0793a61d TG |
2992 | } |
2993 | ||
e48c1788 PZ |
2994 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
2995 | ||
ba532500 YZ |
2996 | void perf_sched_cb_dec(struct pmu *pmu) |
2997 | { | |
e48c1788 PZ |
2998 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
2999 | ||
ba532500 | 3000 | this_cpu_dec(perf_sched_cb_usages); |
e48c1788 PZ |
3001 | |
3002 | if (!--cpuctx->sched_cb_usage) | |
3003 | list_del(&cpuctx->sched_cb_entry); | |
ba532500 YZ |
3004 | } |
3005 | ||
e48c1788 | 3006 | |
ba532500 YZ |
3007 | void perf_sched_cb_inc(struct pmu *pmu) |
3008 | { | |
e48c1788 PZ |
3009 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
3010 | ||
3011 | if (!cpuctx->sched_cb_usage++) | |
3012 | list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
3013 | ||
ba532500 YZ |
3014 | this_cpu_inc(perf_sched_cb_usages); |
3015 | } | |
3016 | ||
3017 | /* | |
3018 | * This function provides the context switch callback to the lower code | |
3019 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
3020 | * |
3021 | * This callback is relevant even to per-cpu events; for example multi event | |
3022 | * PEBS requires this to provide PID/TID information. This requires we flush | |
3023 | * all queued PEBS records before we context switch to a new task. | |
ba532500 YZ |
3024 | */ |
3025 | static void perf_pmu_sched_task(struct task_struct *prev, | |
3026 | struct task_struct *next, | |
3027 | bool sched_in) | |
3028 | { | |
3029 | struct perf_cpu_context *cpuctx; | |
3030 | struct pmu *pmu; | |
ba532500 YZ |
3031 | |
3032 | if (prev == next) | |
3033 | return; | |
3034 | ||
e48c1788 | 3035 | list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) { |
1fd7e416 | 3036 | pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */ |
ba532500 | 3037 | |
e48c1788 PZ |
3038 | if (WARN_ON_ONCE(!pmu->sched_task)) |
3039 | continue; | |
ba532500 | 3040 | |
e48c1788 PZ |
3041 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
3042 | perf_pmu_disable(pmu); | |
ba532500 | 3043 | |
e48c1788 | 3044 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
ba532500 | 3045 | |
e48c1788 PZ |
3046 | perf_pmu_enable(pmu); |
3047 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
ba532500 | 3048 | } |
ba532500 YZ |
3049 | } |
3050 | ||
45ac1403 AH |
3051 | static void perf_event_switch(struct task_struct *task, |
3052 | struct task_struct *next_prev, bool sched_in); | |
3053 | ||
8dc85d54 PZ |
3054 | #define for_each_task_context_nr(ctxn) \ |
3055 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
3056 | ||
3057 | /* | |
3058 | * Called from scheduler to remove the events of the current task, | |
3059 | * with interrupts disabled. | |
3060 | * | |
3061 | * We stop each event and update the event value in event->count. | |
3062 | * | |
3063 | * This does not protect us against NMI, but disable() | |
3064 | * sets the disabled bit in the control field of event _before_ | |
3065 | * accessing the event control register. If a NMI hits, then it will | |
3066 | * not restart the event. | |
3067 | */ | |
ab0cce56 JO |
3068 | void __perf_event_task_sched_out(struct task_struct *task, |
3069 | struct task_struct *next) | |
8dc85d54 PZ |
3070 | { |
3071 | int ctxn; | |
3072 | ||
ba532500 YZ |
3073 | if (__this_cpu_read(perf_sched_cb_usages)) |
3074 | perf_pmu_sched_task(task, next, false); | |
3075 | ||
45ac1403 AH |
3076 | if (atomic_read(&nr_switch_events)) |
3077 | perf_event_switch(task, next, false); | |
3078 | ||
8dc85d54 PZ |
3079 | for_each_task_context_nr(ctxn) |
3080 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
3081 | |
3082 | /* | |
3083 | * if cgroup events exist on this CPU, then we need | |
3084 | * to check if we have to switch out PMU state. | |
3085 | * cgroup event are system-wide mode only | |
3086 | */ | |
4a32fea9 | 3087 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 3088 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
3089 | } |
3090 | ||
5b0311e1 FW |
3091 | /* |
3092 | * Called with IRQs disabled | |
3093 | */ | |
3094 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
3095 | enum event_type_t event_type) | |
3096 | { | |
3097 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
3098 | } |
3099 | ||
235c7fc7 | 3100 | static void |
5b0311e1 | 3101 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
6e37738a | 3102 | struct perf_cpu_context *cpuctx) |
0793a61d | 3103 | { |
cdd6c482 | 3104 | struct perf_event *event; |
0793a61d | 3105 | |
889ff015 FW |
3106 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
3107 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 3108 | continue; |
5632ab12 | 3109 | if (!event_filter_match(event)) |
3b6f9e5c PM |
3110 | continue; |
3111 | ||
e5d1367f SE |
3112 | /* may need to reset tstamp_enabled */ |
3113 | if (is_cgroup_event(event)) | |
3114 | perf_cgroup_mark_enabled(event, ctx); | |
3115 | ||
8c9ed8e1 | 3116 | if (group_can_go_on(event, cpuctx, 1)) |
6e37738a | 3117 | group_sched_in(event, cpuctx, ctx); |
3b6f9e5c PM |
3118 | |
3119 | /* | |
3120 | * If this pinned group hasn't been scheduled, | |
3121 | * put it in error state. | |
3122 | */ | |
cdd6c482 IM |
3123 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
3124 | update_group_times(event); | |
3125 | event->state = PERF_EVENT_STATE_ERROR; | |
53cfbf59 | 3126 | } |
3b6f9e5c | 3127 | } |
5b0311e1 FW |
3128 | } |
3129 | ||
3130 | static void | |
3131 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
6e37738a | 3132 | struct perf_cpu_context *cpuctx) |
5b0311e1 FW |
3133 | { |
3134 | struct perf_event *event; | |
3135 | int can_add_hw = 1; | |
3b6f9e5c | 3136 | |
889ff015 FW |
3137 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
3138 | /* Ignore events in OFF or ERROR state */ | |
3139 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 3140 | continue; |
04289bb9 IM |
3141 | /* |
3142 | * Listen to the 'cpu' scheduling filter constraint | |
cdd6c482 | 3143 | * of events: |
04289bb9 | 3144 | */ |
5632ab12 | 3145 | if (!event_filter_match(event)) |
0793a61d TG |
3146 | continue; |
3147 | ||
e5d1367f SE |
3148 | /* may need to reset tstamp_enabled */ |
3149 | if (is_cgroup_event(event)) | |
3150 | perf_cgroup_mark_enabled(event, ctx); | |
3151 | ||
9ed6060d | 3152 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
6e37738a | 3153 | if (group_sched_in(event, cpuctx, ctx)) |
dd0e6ba2 | 3154 | can_add_hw = 0; |
9ed6060d | 3155 | } |
0793a61d | 3156 | } |
5b0311e1 FW |
3157 | } |
3158 | ||
3159 | static void | |
3160 | ctx_sched_in(struct perf_event_context *ctx, | |
3161 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
3162 | enum event_type_t event_type, |
3163 | struct task_struct *task) | |
5b0311e1 | 3164 | { |
db24d33e | 3165 | int is_active = ctx->is_active; |
c994d613 PZ |
3166 | u64 now; |
3167 | ||
3168 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3169 | |
5b0311e1 | 3170 | if (likely(!ctx->nr_events)) |
facc4307 | 3171 | return; |
5b0311e1 | 3172 | |
3cbaa590 | 3173 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3174 | if (ctx->task) { |
3175 | if (!is_active) | |
3176 | cpuctx->task_ctx = ctx; | |
3177 | else | |
3178 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3179 | } | |
3180 | ||
3cbaa590 PZ |
3181 | is_active ^= ctx->is_active; /* changed bits */ |
3182 | ||
3183 | if (is_active & EVENT_TIME) { | |
3184 | /* start ctx time */ | |
3185 | now = perf_clock(); | |
3186 | ctx->timestamp = now; | |
3187 | perf_cgroup_set_timestamp(task, ctx); | |
3188 | } | |
3189 | ||
5b0311e1 FW |
3190 | /* |
3191 | * First go through the list and put on any pinned groups | |
3192 | * in order to give them the best chance of going on. | |
3193 | */ | |
3cbaa590 | 3194 | if (is_active & EVENT_PINNED) |
6e37738a | 3195 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
3196 | |
3197 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3198 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 3199 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
3200 | } |
3201 | ||
329c0e01 | 3202 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
3203 | enum event_type_t event_type, |
3204 | struct task_struct *task) | |
329c0e01 FW |
3205 | { |
3206 | struct perf_event_context *ctx = &cpuctx->ctx; | |
3207 | ||
e5d1367f | 3208 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
3209 | } |
3210 | ||
e5d1367f SE |
3211 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
3212 | struct task_struct *task) | |
235c7fc7 | 3213 | { |
108b02cf | 3214 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 3215 | |
108b02cf | 3216 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
3217 | if (cpuctx->task_ctx == ctx) |
3218 | return; | |
3219 | ||
facc4307 | 3220 | perf_ctx_lock(cpuctx, ctx); |
1b9a644f | 3221 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
3222 | /* |
3223 | * We want to keep the following priority order: | |
3224 | * cpu pinned (that don't need to move), task pinned, | |
3225 | * cpu flexible, task flexible. | |
fe45bafb AS |
3226 | * |
3227 | * However, if task's ctx is not carrying any pinned | |
3228 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3229 | */ |
fe45bafb AS |
3230 | if (!list_empty(&ctx->pinned_groups)) |
3231 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
63e30d3e | 3232 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 PZ |
3233 | perf_pmu_enable(ctx->pmu); |
3234 | perf_ctx_unlock(cpuctx, ctx); | |
235c7fc7 IM |
3235 | } |
3236 | ||
8dc85d54 PZ |
3237 | /* |
3238 | * Called from scheduler to add the events of the current task | |
3239 | * with interrupts disabled. | |
3240 | * | |
3241 | * We restore the event value and then enable it. | |
3242 | * | |
3243 | * This does not protect us against NMI, but enable() | |
3244 | * sets the enabled bit in the control field of event _before_ | |
3245 | * accessing the event control register. If a NMI hits, then it will | |
3246 | * keep the event running. | |
3247 | */ | |
ab0cce56 JO |
3248 | void __perf_event_task_sched_in(struct task_struct *prev, |
3249 | struct task_struct *task) | |
8dc85d54 PZ |
3250 | { |
3251 | struct perf_event_context *ctx; | |
3252 | int ctxn; | |
3253 | ||
7e41d177 PZ |
3254 | /* |
3255 | * If cgroup events exist on this CPU, then we need to check if we have | |
3256 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3257 | * | |
3258 | * Since cgroup events are CPU events, we must schedule these in before | |
3259 | * we schedule in the task events. | |
3260 | */ | |
3261 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3262 | perf_cgroup_sched_in(prev, task); | |
3263 | ||
8dc85d54 PZ |
3264 | for_each_task_context_nr(ctxn) { |
3265 | ctx = task->perf_event_ctxp[ctxn]; | |
3266 | if (likely(!ctx)) | |
3267 | continue; | |
3268 | ||
e5d1367f | 3269 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3270 | } |
d010b332 | 3271 | |
45ac1403 AH |
3272 | if (atomic_read(&nr_switch_events)) |
3273 | perf_event_switch(task, prev, true); | |
3274 | ||
ba532500 YZ |
3275 | if (__this_cpu_read(perf_sched_cb_usages)) |
3276 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3277 | } |
3278 | ||
abd50713 PZ |
3279 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3280 | { | |
3281 | u64 frequency = event->attr.sample_freq; | |
3282 | u64 sec = NSEC_PER_SEC; | |
3283 | u64 divisor, dividend; | |
3284 | ||
3285 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3286 | ||
3287 | count_fls = fls64(count); | |
3288 | nsec_fls = fls64(nsec); | |
3289 | frequency_fls = fls64(frequency); | |
3290 | sec_fls = 30; | |
3291 | ||
3292 | /* | |
3293 | * We got @count in @nsec, with a target of sample_freq HZ | |
3294 | * the target period becomes: | |
3295 | * | |
3296 | * @count * 10^9 | |
3297 | * period = ------------------- | |
3298 | * @nsec * sample_freq | |
3299 | * | |
3300 | */ | |
3301 | ||
3302 | /* | |
3303 | * Reduce accuracy by one bit such that @a and @b converge | |
3304 | * to a similar magnitude. | |
3305 | */ | |
fe4b04fa | 3306 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3307 | do { \ |
3308 | if (a##_fls > b##_fls) { \ | |
3309 | a >>= 1; \ | |
3310 | a##_fls--; \ | |
3311 | } else { \ | |
3312 | b >>= 1; \ | |
3313 | b##_fls--; \ | |
3314 | } \ | |
3315 | } while (0) | |
3316 | ||
3317 | /* | |
3318 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3319 | * the other, so that finally we can do a u64/u64 division. | |
3320 | */ | |
3321 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3322 | REDUCE_FLS(nsec, frequency); | |
3323 | REDUCE_FLS(sec, count); | |
3324 | } | |
3325 | ||
3326 | if (count_fls + sec_fls > 64) { | |
3327 | divisor = nsec * frequency; | |
3328 | ||
3329 | while (count_fls + sec_fls > 64) { | |
3330 | REDUCE_FLS(count, sec); | |
3331 | divisor >>= 1; | |
3332 | } | |
3333 | ||
3334 | dividend = count * sec; | |
3335 | } else { | |
3336 | dividend = count * sec; | |
3337 | ||
3338 | while (nsec_fls + frequency_fls > 64) { | |
3339 | REDUCE_FLS(nsec, frequency); | |
3340 | dividend >>= 1; | |
3341 | } | |
3342 | ||
3343 | divisor = nsec * frequency; | |
3344 | } | |
3345 | ||
f6ab91ad PZ |
3346 | if (!divisor) |
3347 | return dividend; | |
3348 | ||
abd50713 PZ |
3349 | return div64_u64(dividend, divisor); |
3350 | } | |
3351 | ||
e050e3f0 SE |
3352 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3353 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3354 | ||
f39d47ff | 3355 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3356 | { |
cdd6c482 | 3357 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3358 | s64 period, sample_period; |
bd2b5b12 PZ |
3359 | s64 delta; |
3360 | ||
abd50713 | 3361 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3362 | |
3363 | delta = (s64)(period - hwc->sample_period); | |
3364 | delta = (delta + 7) / 8; /* low pass filter */ | |
3365 | ||
3366 | sample_period = hwc->sample_period + delta; | |
3367 | ||
3368 | if (!sample_period) | |
3369 | sample_period = 1; | |
3370 | ||
bd2b5b12 | 3371 | hwc->sample_period = sample_period; |
abd50713 | 3372 | |
e7850595 | 3373 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3374 | if (disable) |
3375 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3376 | ||
e7850595 | 3377 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3378 | |
3379 | if (disable) | |
3380 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3381 | } |
bd2b5b12 PZ |
3382 | } |
3383 | ||
e050e3f0 SE |
3384 | /* |
3385 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3386 | * events. At the same time, make sure, having freq events does not change | |
3387 | * the rate of unthrottling as that would introduce bias. | |
3388 | */ | |
3389 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3390 | int needs_unthr) | |
60db5e09 | 3391 | { |
cdd6c482 IM |
3392 | struct perf_event *event; |
3393 | struct hw_perf_event *hwc; | |
e050e3f0 | 3394 | u64 now, period = TICK_NSEC; |
abd50713 | 3395 | s64 delta; |
60db5e09 | 3396 | |
e050e3f0 SE |
3397 | /* |
3398 | * only need to iterate over all events iff: | |
3399 | * - context have events in frequency mode (needs freq adjust) | |
3400 | * - there are events to unthrottle on this cpu | |
3401 | */ | |
3402 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3403 | return; |
3404 | ||
e050e3f0 | 3405 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3406 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3407 | |
03541f8b | 3408 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3409 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3410 | continue; |
3411 | ||
5632ab12 | 3412 | if (!event_filter_match(event)) |
5d27c23d PZ |
3413 | continue; |
3414 | ||
44377277 AS |
3415 | perf_pmu_disable(event->pmu); |
3416 | ||
cdd6c482 | 3417 | hwc = &event->hw; |
6a24ed6c | 3418 | |
ae23bff1 | 3419 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3420 | hwc->interrupts = 0; |
cdd6c482 | 3421 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3422 | event->pmu->start(event, 0); |
a78ac325 PZ |
3423 | } |
3424 | ||
cdd6c482 | 3425 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3426 | goto next; |
60db5e09 | 3427 | |
e050e3f0 SE |
3428 | /* |
3429 | * stop the event and update event->count | |
3430 | */ | |
3431 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3432 | ||
e7850595 | 3433 | now = local64_read(&event->count); |
abd50713 PZ |
3434 | delta = now - hwc->freq_count_stamp; |
3435 | hwc->freq_count_stamp = now; | |
60db5e09 | 3436 | |
e050e3f0 SE |
3437 | /* |
3438 | * restart the event | |
3439 | * reload only if value has changed | |
f39d47ff SE |
3440 | * we have stopped the event so tell that |
3441 | * to perf_adjust_period() to avoid stopping it | |
3442 | * twice. | |
e050e3f0 | 3443 | */ |
abd50713 | 3444 | if (delta > 0) |
f39d47ff | 3445 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3446 | |
3447 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3448 | next: |
3449 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3450 | } |
e050e3f0 | 3451 | |
f39d47ff | 3452 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3453 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3454 | } |
3455 | ||
235c7fc7 | 3456 | /* |
cdd6c482 | 3457 | * Round-robin a context's events: |
235c7fc7 | 3458 | */ |
cdd6c482 | 3459 | static void rotate_ctx(struct perf_event_context *ctx) |
0793a61d | 3460 | { |
dddd3379 TG |
3461 | /* |
3462 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3463 | * disabled by the inheritance code. | |
3464 | */ | |
3465 | if (!ctx->rotate_disable) | |
3466 | list_rotate_left(&ctx->flexible_groups); | |
235c7fc7 IM |
3467 | } |
3468 | ||
9e630205 | 3469 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
235c7fc7 | 3470 | { |
8dc85d54 | 3471 | struct perf_event_context *ctx = NULL; |
2fde4f94 | 3472 | int rotate = 0; |
7fc23a53 | 3473 | |
b5ab4cd5 | 3474 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 PZ |
3475 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
3476 | rotate = 1; | |
3477 | } | |
235c7fc7 | 3478 | |
8dc85d54 | 3479 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3480 | if (ctx && ctx->nr_events) { |
b5ab4cd5 PZ |
3481 | if (ctx->nr_events != ctx->nr_active) |
3482 | rotate = 1; | |
3483 | } | |
9717e6cd | 3484 | |
e050e3f0 | 3485 | if (!rotate) |
0f5a2601 PZ |
3486 | goto done; |
3487 | ||
facc4307 | 3488 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3489 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3490 | |
e050e3f0 SE |
3491 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
3492 | if (ctx) | |
3493 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3494 | |
e050e3f0 SE |
3495 | rotate_ctx(&cpuctx->ctx); |
3496 | if (ctx) | |
3497 | rotate_ctx(ctx); | |
235c7fc7 | 3498 | |
e050e3f0 | 3499 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3500 | |
0f5a2601 PZ |
3501 | perf_pmu_enable(cpuctx->ctx.pmu); |
3502 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
b5ab4cd5 | 3503 | done: |
9e630205 SE |
3504 | |
3505 | return rotate; | |
e9d2b064 PZ |
3506 | } |
3507 | ||
3508 | void perf_event_task_tick(void) | |
3509 | { | |
2fde4f94 MR |
3510 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3511 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3512 | int throttled; |
b5ab4cd5 | 3513 | |
e9d2b064 PZ |
3514 | WARN_ON(!irqs_disabled()); |
3515 | ||
e050e3f0 SE |
3516 | __this_cpu_inc(perf_throttled_seq); |
3517 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3518 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3519 | |
2fde4f94 | 3520 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3521 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3522 | } |
3523 | ||
889ff015 FW |
3524 | static int event_enable_on_exec(struct perf_event *event, |
3525 | struct perf_event_context *ctx) | |
3526 | { | |
3527 | if (!event->attr.enable_on_exec) | |
3528 | return 0; | |
3529 | ||
3530 | event->attr.enable_on_exec = 0; | |
3531 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3532 | return 0; | |
3533 | ||
1d9b482e | 3534 | __perf_event_mark_enabled(event); |
889ff015 FW |
3535 | |
3536 | return 1; | |
3537 | } | |
3538 | ||
57e7986e | 3539 | /* |
cdd6c482 | 3540 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3541 | * This expects task == current. |
3542 | */ | |
c1274499 | 3543 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3544 | { |
c1274499 | 3545 | struct perf_event_context *ctx, *clone_ctx = NULL; |
487f05e1 | 3546 | enum event_type_t event_type = 0; |
3e349507 | 3547 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3548 | struct perf_event *event; |
57e7986e PM |
3549 | unsigned long flags; |
3550 | int enabled = 0; | |
3551 | ||
3552 | local_irq_save(flags); | |
c1274499 | 3553 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3554 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3555 | goto out; |
3556 | ||
3e349507 PZ |
3557 | cpuctx = __get_cpu_context(ctx); |
3558 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3559 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
487f05e1 | 3560 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 3561 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
3562 | event_type |= get_event_type(event); |
3563 | } | |
57e7986e PM |
3564 | |
3565 | /* | |
3e349507 | 3566 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3567 | */ |
3e349507 | 3568 | if (enabled) { |
211de6eb | 3569 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 3570 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb PZ |
3571 | } else { |
3572 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
3e349507 PZ |
3573 | } |
3574 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3575 | |
9ed6060d | 3576 | out: |
57e7986e | 3577 | local_irq_restore(flags); |
211de6eb PZ |
3578 | |
3579 | if (clone_ctx) | |
3580 | put_ctx(clone_ctx); | |
57e7986e PM |
3581 | } |
3582 | ||
0492d4c5 PZ |
3583 | struct perf_read_data { |
3584 | struct perf_event *event; | |
3585 | bool group; | |
7d88962e | 3586 | int ret; |
0492d4c5 PZ |
3587 | }; |
3588 | ||
451d24d1 | 3589 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 3590 | { |
d6a2f903 DCC |
3591 | u16 local_pkg, event_pkg; |
3592 | ||
3593 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { | |
451d24d1 PZ |
3594 | int local_cpu = smp_processor_id(); |
3595 | ||
3596 | event_pkg = topology_physical_package_id(event_cpu); | |
3597 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
3598 | |
3599 | if (event_pkg == local_pkg) | |
3600 | return local_cpu; | |
3601 | } | |
3602 | ||
3603 | return event_cpu; | |
3604 | } | |
3605 | ||
0793a61d | 3606 | /* |
cdd6c482 | 3607 | * Cross CPU call to read the hardware event |
0793a61d | 3608 | */ |
cdd6c482 | 3609 | static void __perf_event_read(void *info) |
0793a61d | 3610 | { |
0492d4c5 PZ |
3611 | struct perf_read_data *data = info; |
3612 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3613 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3614 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3615 | struct pmu *pmu = event->pmu; |
621a01ea | 3616 | |
e1ac3614 PM |
3617 | /* |
3618 | * If this is a task context, we need to check whether it is | |
3619 | * the current task context of this cpu. If not it has been | |
3620 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3621 | * event->count would have been updated to a recent sample |
3622 | * when the event was scheduled out. | |
e1ac3614 PM |
3623 | */ |
3624 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3625 | return; | |
3626 | ||
e625cce1 | 3627 | raw_spin_lock(&ctx->lock); |
e5d1367f | 3628 | if (ctx->is_active) { |
542e72fc | 3629 | update_context_time(ctx); |
e5d1367f SE |
3630 | update_cgrp_time_from_event(event); |
3631 | } | |
0492d4c5 | 3632 | |
cdd6c482 | 3633 | update_event_times(event); |
4a00c16e SB |
3634 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3635 | goto unlock; | |
0492d4c5 | 3636 | |
4a00c16e SB |
3637 | if (!data->group) { |
3638 | pmu->read(event); | |
3639 | data->ret = 0; | |
0492d4c5 | 3640 | goto unlock; |
4a00c16e SB |
3641 | } |
3642 | ||
3643 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3644 | ||
3645 | pmu->read(event); | |
0492d4c5 PZ |
3646 | |
3647 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
3648 | update_event_times(sub); | |
4a00c16e SB |
3649 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3650 | /* | |
3651 | * Use sibling's PMU rather than @event's since | |
3652 | * sibling could be on different (eg: software) PMU. | |
3653 | */ | |
0492d4c5 | 3654 | sub->pmu->read(sub); |
4a00c16e | 3655 | } |
0492d4c5 | 3656 | } |
4a00c16e SB |
3657 | |
3658 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3659 | |
3660 | unlock: | |
e625cce1 | 3661 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3662 | } |
3663 | ||
b5e58793 PZ |
3664 | static inline u64 perf_event_count(struct perf_event *event) |
3665 | { | |
3c97b924 | 3666 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
3667 | } |
3668 | ||
ffe8690c KX |
3669 | /* |
3670 | * NMI-safe method to read a local event, that is an event that | |
3671 | * is: | |
3672 | * - either for the current task, or for this CPU | |
3673 | * - does not have inherit set, for inherited task events | |
3674 | * will not be local and we cannot read them atomically | |
3675 | * - must not have a pmu::count method | |
3676 | */ | |
f91840a3 | 3677 | int perf_event_read_local(struct perf_event *event, u64 *value) |
ffe8690c KX |
3678 | { |
3679 | unsigned long flags; | |
f91840a3 | 3680 | int ret = 0; |
ffe8690c KX |
3681 | |
3682 | /* | |
3683 | * Disabling interrupts avoids all counter scheduling (context | |
3684 | * switches, timer based rotation and IPIs). | |
3685 | */ | |
3686 | local_irq_save(flags); | |
3687 | ||
ffe8690c KX |
3688 | /* |
3689 | * It must not be an event with inherit set, we cannot read | |
3690 | * all child counters from atomic context. | |
3691 | */ | |
f91840a3 AS |
3692 | if (event->attr.inherit) { |
3693 | ret = -EOPNOTSUPP; | |
3694 | goto out; | |
3695 | } | |
ffe8690c | 3696 | |
f91840a3 AS |
3697 | /* If this is a per-task event, it must be for current */ |
3698 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
3699 | event->hw.target != current) { | |
3700 | ret = -EINVAL; | |
3701 | goto out; | |
3702 | } | |
3703 | ||
3704 | /* If this is a per-CPU event, it must be for this CPU */ | |
3705 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
3706 | event->cpu != smp_processor_id()) { | |
3707 | ret = -EINVAL; | |
3708 | goto out; | |
3709 | } | |
ffe8690c KX |
3710 | |
3711 | /* | |
3712 | * If the event is currently on this CPU, its either a per-task event, | |
3713 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3714 | * oncpu == -1). | |
3715 | */ | |
3716 | if (event->oncpu == smp_processor_id()) | |
3717 | event->pmu->read(event); | |
3718 | ||
f91840a3 AS |
3719 | *value = local64_read(&event->count); |
3720 | out: | |
ffe8690c KX |
3721 | local_irq_restore(flags); |
3722 | ||
f91840a3 | 3723 | return ret; |
ffe8690c KX |
3724 | } |
3725 | ||
7d88962e | 3726 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3727 | { |
451d24d1 | 3728 | int event_cpu, ret = 0; |
7d88962e | 3729 | |
0793a61d | 3730 | /* |
cdd6c482 IM |
3731 | * If event is enabled and currently active on a CPU, update the |
3732 | * value in the event structure: | |
0793a61d | 3733 | */ |
cdd6c482 | 3734 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
0492d4c5 PZ |
3735 | struct perf_read_data data = { |
3736 | .event = event, | |
3737 | .group = group, | |
7d88962e | 3738 | .ret = 0, |
0492d4c5 | 3739 | }; |
d6a2f903 | 3740 | |
451d24d1 PZ |
3741 | event_cpu = READ_ONCE(event->oncpu); |
3742 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
3743 | return 0; | |
3744 | ||
3745 | preempt_disable(); | |
3746 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 3747 | |
58763148 PZ |
3748 | /* |
3749 | * Purposely ignore the smp_call_function_single() return | |
3750 | * value. | |
3751 | * | |
451d24d1 | 3752 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
3753 | * scheduled out and that will have updated the event count. |
3754 | * | |
3755 | * Therefore, either way, we'll have an up-to-date event count | |
3756 | * after this. | |
3757 | */ | |
451d24d1 PZ |
3758 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
3759 | preempt_enable(); | |
58763148 | 3760 | ret = data.ret; |
cdd6c482 | 3761 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2b8988c9 PZ |
3762 | struct perf_event_context *ctx = event->ctx; |
3763 | unsigned long flags; | |
3764 | ||
e625cce1 | 3765 | raw_spin_lock_irqsave(&ctx->lock, flags); |
c530ccd9 SE |
3766 | /* |
3767 | * may read while context is not active | |
3768 | * (e.g., thread is blocked), in that case | |
3769 | * we cannot update context time | |
3770 | */ | |
e5d1367f | 3771 | if (ctx->is_active) { |
c530ccd9 | 3772 | update_context_time(ctx); |
e5d1367f SE |
3773 | update_cgrp_time_from_event(event); |
3774 | } | |
0492d4c5 PZ |
3775 | if (group) |
3776 | update_group_times(event); | |
3777 | else | |
3778 | update_event_times(event); | |
e625cce1 | 3779 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3780 | } |
7d88962e SB |
3781 | |
3782 | return ret; | |
0793a61d TG |
3783 | } |
3784 | ||
a63eaf34 | 3785 | /* |
cdd6c482 | 3786 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3787 | */ |
eb184479 | 3788 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 3789 | { |
e625cce1 | 3790 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 3791 | mutex_init(&ctx->mutex); |
2fde4f94 | 3792 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
889ff015 FW |
3793 | INIT_LIST_HEAD(&ctx->pinned_groups); |
3794 | INIT_LIST_HEAD(&ctx->flexible_groups); | |
a63eaf34 PM |
3795 | INIT_LIST_HEAD(&ctx->event_list); |
3796 | atomic_set(&ctx->refcount, 1); | |
eb184479 PZ |
3797 | } |
3798 | ||
3799 | static struct perf_event_context * | |
3800 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
3801 | { | |
3802 | struct perf_event_context *ctx; | |
3803 | ||
3804 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
3805 | if (!ctx) | |
3806 | return NULL; | |
3807 | ||
3808 | __perf_event_init_context(ctx); | |
3809 | if (task) { | |
3810 | ctx->task = task; | |
3811 | get_task_struct(task); | |
0793a61d | 3812 | } |
eb184479 PZ |
3813 | ctx->pmu = pmu; |
3814 | ||
3815 | return ctx; | |
a63eaf34 PM |
3816 | } |
3817 | ||
2ebd4ffb MH |
3818 | static struct task_struct * |
3819 | find_lively_task_by_vpid(pid_t vpid) | |
3820 | { | |
3821 | struct task_struct *task; | |
0793a61d TG |
3822 | |
3823 | rcu_read_lock(); | |
2ebd4ffb | 3824 | if (!vpid) |
0793a61d TG |
3825 | task = current; |
3826 | else | |
2ebd4ffb | 3827 | task = find_task_by_vpid(vpid); |
0793a61d TG |
3828 | if (task) |
3829 | get_task_struct(task); | |
3830 | rcu_read_unlock(); | |
3831 | ||
3832 | if (!task) | |
3833 | return ERR_PTR(-ESRCH); | |
3834 | ||
2ebd4ffb | 3835 | return task; |
2ebd4ffb MH |
3836 | } |
3837 | ||
fe4b04fa PZ |
3838 | /* |
3839 | * Returns a matching context with refcount and pincount. | |
3840 | */ | |
108b02cf | 3841 | static struct perf_event_context * |
4af57ef2 YZ |
3842 | find_get_context(struct pmu *pmu, struct task_struct *task, |
3843 | struct perf_event *event) | |
0793a61d | 3844 | { |
211de6eb | 3845 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 3846 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 3847 | void *task_ctx_data = NULL; |
25346b93 | 3848 | unsigned long flags; |
8dc85d54 | 3849 | int ctxn, err; |
4af57ef2 | 3850 | int cpu = event->cpu; |
0793a61d | 3851 | |
22a4ec72 | 3852 | if (!task) { |
cdd6c482 | 3853 | /* Must be root to operate on a CPU event: */ |
0764771d | 3854 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
3855 | return ERR_PTR(-EACCES); |
3856 | ||
108b02cf | 3857 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 3858 | ctx = &cpuctx->ctx; |
c93f7669 | 3859 | get_ctx(ctx); |
fe4b04fa | 3860 | ++ctx->pin_count; |
0793a61d | 3861 | |
0793a61d TG |
3862 | return ctx; |
3863 | } | |
3864 | ||
8dc85d54 PZ |
3865 | err = -EINVAL; |
3866 | ctxn = pmu->task_ctx_nr; | |
3867 | if (ctxn < 0) | |
3868 | goto errout; | |
3869 | ||
4af57ef2 YZ |
3870 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
3871 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
3872 | if (!task_ctx_data) { | |
3873 | err = -ENOMEM; | |
3874 | goto errout; | |
3875 | } | |
3876 | } | |
3877 | ||
9ed6060d | 3878 | retry: |
8dc85d54 | 3879 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 3880 | if (ctx) { |
211de6eb | 3881 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 3882 | ++ctx->pin_count; |
4af57ef2 YZ |
3883 | |
3884 | if (task_ctx_data && !ctx->task_ctx_data) { | |
3885 | ctx->task_ctx_data = task_ctx_data; | |
3886 | task_ctx_data = NULL; | |
3887 | } | |
e625cce1 | 3888 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
3889 | |
3890 | if (clone_ctx) | |
3891 | put_ctx(clone_ctx); | |
9137fb28 | 3892 | } else { |
eb184479 | 3893 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
3894 | err = -ENOMEM; |
3895 | if (!ctx) | |
3896 | goto errout; | |
eb184479 | 3897 | |
4af57ef2 YZ |
3898 | if (task_ctx_data) { |
3899 | ctx->task_ctx_data = task_ctx_data; | |
3900 | task_ctx_data = NULL; | |
3901 | } | |
3902 | ||
dbe08d82 ON |
3903 | err = 0; |
3904 | mutex_lock(&task->perf_event_mutex); | |
3905 | /* | |
3906 | * If it has already passed perf_event_exit_task(). | |
3907 | * we must see PF_EXITING, it takes this mutex too. | |
3908 | */ | |
3909 | if (task->flags & PF_EXITING) | |
3910 | err = -ESRCH; | |
3911 | else if (task->perf_event_ctxp[ctxn]) | |
3912 | err = -EAGAIN; | |
fe4b04fa | 3913 | else { |
9137fb28 | 3914 | get_ctx(ctx); |
fe4b04fa | 3915 | ++ctx->pin_count; |
dbe08d82 | 3916 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 3917 | } |
dbe08d82 ON |
3918 | mutex_unlock(&task->perf_event_mutex); |
3919 | ||
3920 | if (unlikely(err)) { | |
9137fb28 | 3921 | put_ctx(ctx); |
dbe08d82 ON |
3922 | |
3923 | if (err == -EAGAIN) | |
3924 | goto retry; | |
3925 | goto errout; | |
a63eaf34 PM |
3926 | } |
3927 | } | |
3928 | ||
4af57ef2 | 3929 | kfree(task_ctx_data); |
0793a61d | 3930 | return ctx; |
c93f7669 | 3931 | |
9ed6060d | 3932 | errout: |
4af57ef2 | 3933 | kfree(task_ctx_data); |
c93f7669 | 3934 | return ERR_PTR(err); |
0793a61d TG |
3935 | } |
3936 | ||
6fb2915d | 3937 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 3938 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 3939 | |
cdd6c482 | 3940 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 3941 | { |
cdd6c482 | 3942 | struct perf_event *event; |
592903cd | 3943 | |
cdd6c482 IM |
3944 | event = container_of(head, struct perf_event, rcu_head); |
3945 | if (event->ns) | |
3946 | put_pid_ns(event->ns); | |
6fb2915d | 3947 | perf_event_free_filter(event); |
cdd6c482 | 3948 | kfree(event); |
592903cd PZ |
3949 | } |
3950 | ||
b69cf536 PZ |
3951 | static void ring_buffer_attach(struct perf_event *event, |
3952 | struct ring_buffer *rb); | |
925d519a | 3953 | |
f2fb6bef KL |
3954 | static void detach_sb_event(struct perf_event *event) |
3955 | { | |
3956 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
3957 | ||
3958 | raw_spin_lock(&pel->lock); | |
3959 | list_del_rcu(&event->sb_list); | |
3960 | raw_spin_unlock(&pel->lock); | |
3961 | } | |
3962 | ||
a4f144eb | 3963 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 3964 | { |
a4f144eb DCC |
3965 | struct perf_event_attr *attr = &event->attr; |
3966 | ||
f2fb6bef | 3967 | if (event->parent) |
a4f144eb | 3968 | return false; |
f2fb6bef KL |
3969 | |
3970 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 3971 | return false; |
f2fb6bef | 3972 | |
a4f144eb DCC |
3973 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
3974 | attr->comm || attr->comm_exec || | |
3975 | attr->task || | |
3976 | attr->context_switch) | |
3977 | return true; | |
3978 | return false; | |
3979 | } | |
3980 | ||
3981 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
3982 | { | |
3983 | if (is_sb_event(event)) | |
3984 | detach_sb_event(event); | |
f2fb6bef KL |
3985 | } |
3986 | ||
4beb31f3 | 3987 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 3988 | { |
4beb31f3 FW |
3989 | if (event->parent) |
3990 | return; | |
3991 | ||
4beb31f3 FW |
3992 | if (is_cgroup_event(event)) |
3993 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
3994 | } | |
925d519a | 3995 | |
555e0c1e FW |
3996 | #ifdef CONFIG_NO_HZ_FULL |
3997 | static DEFINE_SPINLOCK(nr_freq_lock); | |
3998 | #endif | |
3999 | ||
4000 | static void unaccount_freq_event_nohz(void) | |
4001 | { | |
4002 | #ifdef CONFIG_NO_HZ_FULL | |
4003 | spin_lock(&nr_freq_lock); | |
4004 | if (atomic_dec_and_test(&nr_freq_events)) | |
4005 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
4006 | spin_unlock(&nr_freq_lock); | |
4007 | #endif | |
4008 | } | |
4009 | ||
4010 | static void unaccount_freq_event(void) | |
4011 | { | |
4012 | if (tick_nohz_full_enabled()) | |
4013 | unaccount_freq_event_nohz(); | |
4014 | else | |
4015 | atomic_dec(&nr_freq_events); | |
4016 | } | |
4017 | ||
4beb31f3 FW |
4018 | static void unaccount_event(struct perf_event *event) |
4019 | { | |
25432ae9 PZ |
4020 | bool dec = false; |
4021 | ||
4beb31f3 FW |
4022 | if (event->parent) |
4023 | return; | |
4024 | ||
4025 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 4026 | dec = true; |
4beb31f3 FW |
4027 | if (event->attr.mmap || event->attr.mmap_data) |
4028 | atomic_dec(&nr_mmap_events); | |
4029 | if (event->attr.comm) | |
4030 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
4031 | if (event->attr.namespaces) |
4032 | atomic_dec(&nr_namespaces_events); | |
4beb31f3 FW |
4033 | if (event->attr.task) |
4034 | atomic_dec(&nr_task_events); | |
948b26b6 | 4035 | if (event->attr.freq) |
555e0c1e | 4036 | unaccount_freq_event(); |
45ac1403 | 4037 | if (event->attr.context_switch) { |
25432ae9 | 4038 | dec = true; |
45ac1403 AH |
4039 | atomic_dec(&nr_switch_events); |
4040 | } | |
4beb31f3 | 4041 | if (is_cgroup_event(event)) |
25432ae9 | 4042 | dec = true; |
4beb31f3 | 4043 | if (has_branch_stack(event)) |
25432ae9 PZ |
4044 | dec = true; |
4045 | ||
9107c89e PZ |
4046 | if (dec) { |
4047 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
4048 | schedule_delayed_work(&perf_sched_work, HZ); | |
4049 | } | |
4beb31f3 FW |
4050 | |
4051 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
4052 | |
4053 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 4054 | } |
925d519a | 4055 | |
9107c89e PZ |
4056 | static void perf_sched_delayed(struct work_struct *work) |
4057 | { | |
4058 | mutex_lock(&perf_sched_mutex); | |
4059 | if (atomic_dec_and_test(&perf_sched_count)) | |
4060 | static_branch_disable(&perf_sched_events); | |
4061 | mutex_unlock(&perf_sched_mutex); | |
4062 | } | |
4063 | ||
bed5b25a AS |
4064 | /* |
4065 | * The following implement mutual exclusion of events on "exclusive" pmus | |
4066 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
4067 | * at a time, so we disallow creating events that might conflict, namely: | |
4068 | * | |
4069 | * 1) cpu-wide events in the presence of per-task events, | |
4070 | * 2) per-task events in the presence of cpu-wide events, | |
4071 | * 3) two matching events on the same context. | |
4072 | * | |
4073 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 4074 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
4075 | */ |
4076 | static int exclusive_event_init(struct perf_event *event) | |
4077 | { | |
4078 | struct pmu *pmu = event->pmu; | |
4079 | ||
4080 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4081 | return 0; | |
4082 | ||
4083 | /* | |
4084 | * Prevent co-existence of per-task and cpu-wide events on the | |
4085 | * same exclusive pmu. | |
4086 | * | |
4087 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
4088 | * events on this "exclusive" pmu, positive means there are | |
4089 | * per-task events. | |
4090 | * | |
4091 | * Since this is called in perf_event_alloc() path, event::ctx | |
4092 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
4093 | * to mean "per-task event", because unlike other attach states it | |
4094 | * never gets cleared. | |
4095 | */ | |
4096 | if (event->attach_state & PERF_ATTACH_TASK) { | |
4097 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
4098 | return -EBUSY; | |
4099 | } else { | |
4100 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
4101 | return -EBUSY; | |
4102 | } | |
4103 | ||
4104 | return 0; | |
4105 | } | |
4106 | ||
4107 | static void exclusive_event_destroy(struct perf_event *event) | |
4108 | { | |
4109 | struct pmu *pmu = event->pmu; | |
4110 | ||
4111 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4112 | return; | |
4113 | ||
4114 | /* see comment in exclusive_event_init() */ | |
4115 | if (event->attach_state & PERF_ATTACH_TASK) | |
4116 | atomic_dec(&pmu->exclusive_cnt); | |
4117 | else | |
4118 | atomic_inc(&pmu->exclusive_cnt); | |
4119 | } | |
4120 | ||
4121 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
4122 | { | |
3bf6215a | 4123 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
4124 | (e1->cpu == e2->cpu || |
4125 | e1->cpu == -1 || | |
4126 | e2->cpu == -1)) | |
4127 | return true; | |
4128 | return false; | |
4129 | } | |
4130 | ||
4131 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
4132 | static bool exclusive_event_installable(struct perf_event *event, | |
4133 | struct perf_event_context *ctx) | |
4134 | { | |
4135 | struct perf_event *iter_event; | |
4136 | struct pmu *pmu = event->pmu; | |
4137 | ||
4138 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4139 | return true; | |
4140 | ||
4141 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
4142 | if (exclusive_event_match(iter_event, event)) | |
4143 | return false; | |
4144 | } | |
4145 | ||
4146 | return true; | |
4147 | } | |
4148 | ||
375637bc AS |
4149 | static void perf_addr_filters_splice(struct perf_event *event, |
4150 | struct list_head *head); | |
4151 | ||
683ede43 | 4152 | static void _free_event(struct perf_event *event) |
f1600952 | 4153 | { |
e360adbe | 4154 | irq_work_sync(&event->pending); |
925d519a | 4155 | |
4beb31f3 | 4156 | unaccount_event(event); |
9ee318a7 | 4157 | |
76369139 | 4158 | if (event->rb) { |
9bb5d40c PZ |
4159 | /* |
4160 | * Can happen when we close an event with re-directed output. | |
4161 | * | |
4162 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
4163 | * over us; possibly making our ring_buffer_put() the last. | |
4164 | */ | |
4165 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 4166 | ring_buffer_attach(event, NULL); |
9bb5d40c | 4167 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
4168 | } |
4169 | ||
e5d1367f SE |
4170 | if (is_cgroup_event(event)) |
4171 | perf_detach_cgroup(event); | |
4172 | ||
a0733e69 PZ |
4173 | if (!event->parent) { |
4174 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
4175 | put_callchain_buffers(); | |
4176 | } | |
4177 | ||
4178 | perf_event_free_bpf_prog(event); | |
375637bc AS |
4179 | perf_addr_filters_splice(event, NULL); |
4180 | kfree(event->addr_filters_offs); | |
a0733e69 PZ |
4181 | |
4182 | if (event->destroy) | |
4183 | event->destroy(event); | |
4184 | ||
4185 | if (event->ctx) | |
4186 | put_ctx(event->ctx); | |
4187 | ||
62a92c8f AS |
4188 | exclusive_event_destroy(event); |
4189 | module_put(event->pmu->module); | |
a0733e69 PZ |
4190 | |
4191 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
4192 | } |
4193 | ||
683ede43 PZ |
4194 | /* |
4195 | * Used to free events which have a known refcount of 1, such as in error paths | |
4196 | * where the event isn't exposed yet and inherited events. | |
4197 | */ | |
4198 | static void free_event(struct perf_event *event) | |
0793a61d | 4199 | { |
683ede43 PZ |
4200 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
4201 | "unexpected event refcount: %ld; ptr=%p\n", | |
4202 | atomic_long_read(&event->refcount), event)) { | |
4203 | /* leak to avoid use-after-free */ | |
4204 | return; | |
4205 | } | |
0793a61d | 4206 | |
683ede43 | 4207 | _free_event(event); |
0793a61d TG |
4208 | } |
4209 | ||
a66a3052 | 4210 | /* |
f8697762 | 4211 | * Remove user event from the owner task. |
a66a3052 | 4212 | */ |
f8697762 | 4213 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 4214 | { |
8882135b | 4215 | struct task_struct *owner; |
fb0459d7 | 4216 | |
8882135b | 4217 | rcu_read_lock(); |
8882135b | 4218 | /* |
f47c02c0 PZ |
4219 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
4220 | * observe !owner it means the list deletion is complete and we can | |
4221 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
4222 | * owner->perf_event_mutex. |
4223 | */ | |
7252704b | 4224 | owner = READ_ONCE(event->owner); |
8882135b PZ |
4225 | if (owner) { |
4226 | /* | |
4227 | * Since delayed_put_task_struct() also drops the last | |
4228 | * task reference we can safely take a new reference | |
4229 | * while holding the rcu_read_lock(). | |
4230 | */ | |
4231 | get_task_struct(owner); | |
4232 | } | |
4233 | rcu_read_unlock(); | |
4234 | ||
4235 | if (owner) { | |
f63a8daa PZ |
4236 | /* |
4237 | * If we're here through perf_event_exit_task() we're already | |
4238 | * holding ctx->mutex which would be an inversion wrt. the | |
4239 | * normal lock order. | |
4240 | * | |
4241 | * However we can safely take this lock because its the child | |
4242 | * ctx->mutex. | |
4243 | */ | |
4244 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
4245 | ||
8882135b PZ |
4246 | /* |
4247 | * We have to re-check the event->owner field, if it is cleared | |
4248 | * we raced with perf_event_exit_task(), acquiring the mutex | |
4249 | * ensured they're done, and we can proceed with freeing the | |
4250 | * event. | |
4251 | */ | |
f47c02c0 | 4252 | if (event->owner) { |
8882135b | 4253 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
4254 | smp_store_release(&event->owner, NULL); |
4255 | } | |
8882135b PZ |
4256 | mutex_unlock(&owner->perf_event_mutex); |
4257 | put_task_struct(owner); | |
4258 | } | |
f8697762 JO |
4259 | } |
4260 | ||
f8697762 JO |
4261 | static void put_event(struct perf_event *event) |
4262 | { | |
f8697762 JO |
4263 | if (!atomic_long_dec_and_test(&event->refcount)) |
4264 | return; | |
4265 | ||
c6e5b732 PZ |
4266 | _free_event(event); |
4267 | } | |
4268 | ||
4269 | /* | |
4270 | * Kill an event dead; while event:refcount will preserve the event | |
4271 | * object, it will not preserve its functionality. Once the last 'user' | |
4272 | * gives up the object, we'll destroy the thing. | |
4273 | */ | |
4274 | int perf_event_release_kernel(struct perf_event *event) | |
4275 | { | |
a4f4bb6d | 4276 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 PZ |
4277 | struct perf_event *child, *tmp; |
4278 | ||
a4f4bb6d PZ |
4279 | /* |
4280 | * If we got here through err_file: fput(event_file); we will not have | |
4281 | * attached to a context yet. | |
4282 | */ | |
4283 | if (!ctx) { | |
4284 | WARN_ON_ONCE(event->attach_state & | |
4285 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
4286 | goto no_ctx; | |
4287 | } | |
4288 | ||
f8697762 JO |
4289 | if (!is_kernel_event(event)) |
4290 | perf_remove_from_owner(event); | |
8882135b | 4291 | |
5fa7c8ec | 4292 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 4293 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 4294 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 4295 | |
a69b0ca4 | 4296 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 4297 | /* |
d8a8cfc7 | 4298 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 4299 | * anymore. |
683ede43 | 4300 | * |
a69b0ca4 PZ |
4301 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4302 | * also see this, most importantly inherit_event() which will avoid | |
4303 | * placing more children on the list. | |
683ede43 | 4304 | * |
c6e5b732 PZ |
4305 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4306 | * child events. | |
683ede43 | 4307 | */ |
a69b0ca4 PZ |
4308 | event->state = PERF_EVENT_STATE_DEAD; |
4309 | raw_spin_unlock_irq(&ctx->lock); | |
4310 | ||
4311 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4312 | |
c6e5b732 PZ |
4313 | again: |
4314 | mutex_lock(&event->child_mutex); | |
4315 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4316 | |
c6e5b732 PZ |
4317 | /* |
4318 | * Cannot change, child events are not migrated, see the | |
4319 | * comment with perf_event_ctx_lock_nested(). | |
4320 | */ | |
7252704b | 4321 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
4322 | /* |
4323 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4324 | * through hoops. We start by grabbing a reference on the ctx. | |
4325 | * | |
4326 | * Since the event cannot get freed while we hold the | |
4327 | * child_mutex, the context must also exist and have a !0 | |
4328 | * reference count. | |
4329 | */ | |
4330 | get_ctx(ctx); | |
4331 | ||
4332 | /* | |
4333 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4334 | * acquire ctx::mutex without fear of it going away. Then we | |
4335 | * can re-acquire child_mutex. | |
4336 | */ | |
4337 | mutex_unlock(&event->child_mutex); | |
4338 | mutex_lock(&ctx->mutex); | |
4339 | mutex_lock(&event->child_mutex); | |
4340 | ||
4341 | /* | |
4342 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4343 | * state, if child is still the first entry, it didn't get freed | |
4344 | * and we can continue doing so. | |
4345 | */ | |
4346 | tmp = list_first_entry_or_null(&event->child_list, | |
4347 | struct perf_event, child_list); | |
4348 | if (tmp == child) { | |
4349 | perf_remove_from_context(child, DETACH_GROUP); | |
4350 | list_del(&child->child_list); | |
4351 | free_event(child); | |
4352 | /* | |
4353 | * This matches the refcount bump in inherit_event(); | |
4354 | * this can't be the last reference. | |
4355 | */ | |
4356 | put_event(event); | |
4357 | } | |
4358 | ||
4359 | mutex_unlock(&event->child_mutex); | |
4360 | mutex_unlock(&ctx->mutex); | |
4361 | put_ctx(ctx); | |
4362 | goto again; | |
4363 | } | |
4364 | mutex_unlock(&event->child_mutex); | |
4365 | ||
a4f4bb6d PZ |
4366 | no_ctx: |
4367 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
4368 | return 0; |
4369 | } | |
4370 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
4371 | ||
8b10c5e2 PZ |
4372 | /* |
4373 | * Called when the last reference to the file is gone. | |
4374 | */ | |
a6fa941d AV |
4375 | static int perf_release(struct inode *inode, struct file *file) |
4376 | { | |
c6e5b732 | 4377 | perf_event_release_kernel(file->private_data); |
a6fa941d | 4378 | return 0; |
fb0459d7 | 4379 | } |
fb0459d7 | 4380 | |
59ed446f | 4381 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 4382 | { |
cdd6c482 | 4383 | struct perf_event *child; |
e53c0994 PZ |
4384 | u64 total = 0; |
4385 | ||
59ed446f PZ |
4386 | *enabled = 0; |
4387 | *running = 0; | |
4388 | ||
6f10581a | 4389 | mutex_lock(&event->child_mutex); |
01add3ea | 4390 | |
7d88962e | 4391 | (void)perf_event_read(event, false); |
01add3ea SB |
4392 | total += perf_event_count(event); |
4393 | ||
59ed446f PZ |
4394 | *enabled += event->total_time_enabled + |
4395 | atomic64_read(&event->child_total_time_enabled); | |
4396 | *running += event->total_time_running + | |
4397 | atomic64_read(&event->child_total_time_running); | |
4398 | ||
4399 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4400 | (void)perf_event_read(child, false); |
01add3ea | 4401 | total += perf_event_count(child); |
59ed446f PZ |
4402 | *enabled += child->total_time_enabled; |
4403 | *running += child->total_time_running; | |
4404 | } | |
6f10581a | 4405 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4406 | |
4407 | return total; | |
4408 | } | |
fb0459d7 | 4409 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4410 | |
7d88962e | 4411 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4412 | u64 read_format, u64 *values) |
3dab77fb | 4413 | { |
2aeb1883 | 4414 | struct perf_event_context *ctx = leader->ctx; |
fa8c2693 | 4415 | struct perf_event *sub; |
2aeb1883 | 4416 | unsigned long flags; |
fa8c2693 | 4417 | int n = 1; /* skip @nr */ |
7d88962e | 4418 | int ret; |
f63a8daa | 4419 | |
7d88962e SB |
4420 | ret = perf_event_read(leader, true); |
4421 | if (ret) | |
4422 | return ret; | |
abf4868b | 4423 | |
fa8c2693 PZ |
4424 | /* |
4425 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4426 | * will be identical to those of the leader, so we only publish one | |
4427 | * set. | |
4428 | */ | |
4429 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4430 | values[n++] += leader->total_time_enabled + | |
4431 | atomic64_read(&leader->child_total_time_enabled); | |
4432 | } | |
3dab77fb | 4433 | |
fa8c2693 PZ |
4434 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4435 | values[n++] += leader->total_time_running + | |
4436 | atomic64_read(&leader->child_total_time_running); | |
4437 | } | |
4438 | ||
4439 | /* | |
4440 | * Write {count,id} tuples for every sibling. | |
4441 | */ | |
4442 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4443 | if (read_format & PERF_FORMAT_ID) |
4444 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4445 | |
2aeb1883 JO |
4446 | raw_spin_lock_irqsave(&ctx->lock, flags); |
4447 | ||
fa8c2693 PZ |
4448 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
4449 | values[n++] += perf_event_count(sub); | |
4450 | if (read_format & PERF_FORMAT_ID) | |
4451 | values[n++] = primary_event_id(sub); | |
4452 | } | |
7d88962e | 4453 | |
2aeb1883 | 4454 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
7d88962e | 4455 | return 0; |
fa8c2693 | 4456 | } |
3dab77fb | 4457 | |
fa8c2693 PZ |
4458 | static int perf_read_group(struct perf_event *event, |
4459 | u64 read_format, char __user *buf) | |
4460 | { | |
4461 | struct perf_event *leader = event->group_leader, *child; | |
4462 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4463 | int ret; |
fa8c2693 | 4464 | u64 *values; |
3dab77fb | 4465 | |
fa8c2693 | 4466 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4467 | |
fa8c2693 PZ |
4468 | values = kzalloc(event->read_size, GFP_KERNEL); |
4469 | if (!values) | |
4470 | return -ENOMEM; | |
3dab77fb | 4471 | |
fa8c2693 PZ |
4472 | values[0] = 1 + leader->nr_siblings; |
4473 | ||
4474 | /* | |
4475 | * By locking the child_mutex of the leader we effectively | |
4476 | * lock the child list of all siblings.. XXX explain how. | |
4477 | */ | |
4478 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4479 | |
7d88962e SB |
4480 | ret = __perf_read_group_add(leader, read_format, values); |
4481 | if (ret) | |
4482 | goto unlock; | |
4483 | ||
4484 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4485 | ret = __perf_read_group_add(child, read_format, values); | |
4486 | if (ret) | |
4487 | goto unlock; | |
4488 | } | |
abf4868b | 4489 | |
fa8c2693 | 4490 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4491 | |
7d88962e | 4492 | ret = event->read_size; |
fa8c2693 PZ |
4493 | if (copy_to_user(buf, values, event->read_size)) |
4494 | ret = -EFAULT; | |
7d88962e | 4495 | goto out; |
fa8c2693 | 4496 | |
7d88962e SB |
4497 | unlock: |
4498 | mutex_unlock(&leader->child_mutex); | |
4499 | out: | |
fa8c2693 | 4500 | kfree(values); |
abf4868b | 4501 | return ret; |
3dab77fb PZ |
4502 | } |
4503 | ||
b15f495b | 4504 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4505 | u64 read_format, char __user *buf) |
4506 | { | |
59ed446f | 4507 | u64 enabled, running; |
3dab77fb PZ |
4508 | u64 values[4]; |
4509 | int n = 0; | |
4510 | ||
59ed446f PZ |
4511 | values[n++] = perf_event_read_value(event, &enabled, &running); |
4512 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
4513 | values[n++] = enabled; | |
4514 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4515 | values[n++] = running; | |
3dab77fb | 4516 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4517 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4518 | |
4519 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4520 | return -EFAULT; | |
4521 | ||
4522 | return n * sizeof(u64); | |
4523 | } | |
4524 | ||
dc633982 JO |
4525 | static bool is_event_hup(struct perf_event *event) |
4526 | { | |
4527 | bool no_children; | |
4528 | ||
a69b0ca4 | 4529 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4530 | return false; |
4531 | ||
4532 | mutex_lock(&event->child_mutex); | |
4533 | no_children = list_empty(&event->child_list); | |
4534 | mutex_unlock(&event->child_mutex); | |
4535 | return no_children; | |
4536 | } | |
4537 | ||
0793a61d | 4538 | /* |
cdd6c482 | 4539 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4540 | */ |
4541 | static ssize_t | |
b15f495b | 4542 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4543 | { |
cdd6c482 | 4544 | u64 read_format = event->attr.read_format; |
3dab77fb | 4545 | int ret; |
0793a61d | 4546 | |
3b6f9e5c | 4547 | /* |
cdd6c482 | 4548 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4549 | * error state (i.e. because it was pinned but it couldn't be |
4550 | * scheduled on to the CPU at some point). | |
4551 | */ | |
cdd6c482 | 4552 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4553 | return 0; |
4554 | ||
c320c7b7 | 4555 | if (count < event->read_size) |
3dab77fb PZ |
4556 | return -ENOSPC; |
4557 | ||
cdd6c482 | 4558 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4559 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4560 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4561 | else |
b15f495b | 4562 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4563 | |
3dab77fb | 4564 | return ret; |
0793a61d TG |
4565 | } |
4566 | ||
0793a61d TG |
4567 | static ssize_t |
4568 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4569 | { | |
cdd6c482 | 4570 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4571 | struct perf_event_context *ctx; |
4572 | int ret; | |
0793a61d | 4573 | |
f63a8daa | 4574 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4575 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4576 | perf_event_ctx_unlock(event, ctx); |
4577 | ||
4578 | return ret; | |
0793a61d TG |
4579 | } |
4580 | ||
4581 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
4582 | { | |
cdd6c482 | 4583 | struct perf_event *event = file->private_data; |
76369139 | 4584 | struct ring_buffer *rb; |
61b67684 | 4585 | unsigned int events = POLLHUP; |
c7138f37 | 4586 | |
e708d7ad | 4587 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4588 | |
dc633982 | 4589 | if (is_event_hup(event)) |
179033b3 | 4590 | return events; |
c7138f37 | 4591 | |
10c6db11 | 4592 | /* |
9bb5d40c PZ |
4593 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4594 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4595 | */ |
4596 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4597 | rb = event->rb; |
4598 | if (rb) | |
76369139 | 4599 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4600 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4601 | return events; |
4602 | } | |
4603 | ||
f63a8daa | 4604 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4605 | { |
7d88962e | 4606 | (void)perf_event_read(event, false); |
e7850595 | 4607 | local64_set(&event->count, 0); |
cdd6c482 | 4608 | perf_event_update_userpage(event); |
3df5edad PZ |
4609 | } |
4610 | ||
c93f7669 | 4611 | /* |
cdd6c482 IM |
4612 | * Holding the top-level event's child_mutex means that any |
4613 | * descendant process that has inherited this event will block | |
8ba289b8 | 4614 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4615 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4616 | */ |
cdd6c482 IM |
4617 | static void perf_event_for_each_child(struct perf_event *event, |
4618 | void (*func)(struct perf_event *)) | |
3df5edad | 4619 | { |
cdd6c482 | 4620 | struct perf_event *child; |
3df5edad | 4621 | |
cdd6c482 | 4622 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4623 | |
cdd6c482 IM |
4624 | mutex_lock(&event->child_mutex); |
4625 | func(event); | |
4626 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4627 | func(child); |
cdd6c482 | 4628 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4629 | } |
4630 | ||
cdd6c482 IM |
4631 | static void perf_event_for_each(struct perf_event *event, |
4632 | void (*func)(struct perf_event *)) | |
3df5edad | 4633 | { |
cdd6c482 IM |
4634 | struct perf_event_context *ctx = event->ctx; |
4635 | struct perf_event *sibling; | |
3df5edad | 4636 | |
f63a8daa PZ |
4637 | lockdep_assert_held(&ctx->mutex); |
4638 | ||
cdd6c482 | 4639 | event = event->group_leader; |
75f937f2 | 4640 | |
cdd6c482 | 4641 | perf_event_for_each_child(event, func); |
cdd6c482 | 4642 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
724b6daa | 4643 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4644 | } |
4645 | ||
fae3fde6 PZ |
4646 | static void __perf_event_period(struct perf_event *event, |
4647 | struct perf_cpu_context *cpuctx, | |
4648 | struct perf_event_context *ctx, | |
4649 | void *info) | |
c7999c6f | 4650 | { |
fae3fde6 | 4651 | u64 value = *((u64 *)info); |
c7999c6f | 4652 | bool active; |
08247e31 | 4653 | |
cdd6c482 | 4654 | if (event->attr.freq) { |
cdd6c482 | 4655 | event->attr.sample_freq = value; |
08247e31 | 4656 | } else { |
cdd6c482 IM |
4657 | event->attr.sample_period = value; |
4658 | event->hw.sample_period = value; | |
08247e31 | 4659 | } |
bad7192b PZ |
4660 | |
4661 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4662 | if (active) { | |
4663 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4664 | /* |
4665 | * We could be throttled; unthrottle now to avoid the tick | |
4666 | * trying to unthrottle while we already re-started the event. | |
4667 | */ | |
4668 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4669 | event->hw.interrupts = 0; | |
4670 | perf_log_throttle(event, 1); | |
4671 | } | |
bad7192b PZ |
4672 | event->pmu->stop(event, PERF_EF_UPDATE); |
4673 | } | |
4674 | ||
4675 | local64_set(&event->hw.period_left, 0); | |
4676 | ||
4677 | if (active) { | |
4678 | event->pmu->start(event, PERF_EF_RELOAD); | |
4679 | perf_pmu_enable(ctx->pmu); | |
4680 | } | |
c7999c6f PZ |
4681 | } |
4682 | ||
4683 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4684 | { | |
c7999c6f PZ |
4685 | u64 value; |
4686 | ||
4687 | if (!is_sampling_event(event)) | |
4688 | return -EINVAL; | |
4689 | ||
4690 | if (copy_from_user(&value, arg, sizeof(value))) | |
4691 | return -EFAULT; | |
4692 | ||
4693 | if (!value) | |
4694 | return -EINVAL; | |
4695 | ||
4696 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4697 | return -EINVAL; | |
4698 | ||
fae3fde6 | 4699 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4700 | |
c7999c6f | 4701 | return 0; |
08247e31 PZ |
4702 | } |
4703 | ||
ac9721f3 PZ |
4704 | static const struct file_operations perf_fops; |
4705 | ||
2903ff01 | 4706 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4707 | { |
2903ff01 AV |
4708 | struct fd f = fdget(fd); |
4709 | if (!f.file) | |
4710 | return -EBADF; | |
ac9721f3 | 4711 | |
2903ff01 AV |
4712 | if (f.file->f_op != &perf_fops) { |
4713 | fdput(f); | |
4714 | return -EBADF; | |
ac9721f3 | 4715 | } |
2903ff01 AV |
4716 | *p = f; |
4717 | return 0; | |
ac9721f3 PZ |
4718 | } |
4719 | ||
4720 | static int perf_event_set_output(struct perf_event *event, | |
4721 | struct perf_event *output_event); | |
6fb2915d | 4722 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4723 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4724 | |
f63a8daa | 4725 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4726 | { |
cdd6c482 | 4727 | void (*func)(struct perf_event *); |
3df5edad | 4728 | u32 flags = arg; |
d859e29f PM |
4729 | |
4730 | switch (cmd) { | |
cdd6c482 | 4731 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4732 | func = _perf_event_enable; |
d859e29f | 4733 | break; |
cdd6c482 | 4734 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4735 | func = _perf_event_disable; |
79f14641 | 4736 | break; |
cdd6c482 | 4737 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4738 | func = _perf_event_reset; |
6de6a7b9 | 4739 | break; |
3df5edad | 4740 | |
cdd6c482 | 4741 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4742 | return _perf_event_refresh(event, arg); |
08247e31 | 4743 | |
cdd6c482 IM |
4744 | case PERF_EVENT_IOC_PERIOD: |
4745 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4746 | |
cf4957f1 JO |
4747 | case PERF_EVENT_IOC_ID: |
4748 | { | |
4749 | u64 id = primary_event_id(event); | |
4750 | ||
4751 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4752 | return -EFAULT; | |
4753 | return 0; | |
4754 | } | |
4755 | ||
cdd6c482 | 4756 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4757 | { |
ac9721f3 | 4758 | int ret; |
ac9721f3 | 4759 | if (arg != -1) { |
2903ff01 AV |
4760 | struct perf_event *output_event; |
4761 | struct fd output; | |
4762 | ret = perf_fget_light(arg, &output); | |
4763 | if (ret) | |
4764 | return ret; | |
4765 | output_event = output.file->private_data; | |
4766 | ret = perf_event_set_output(event, output_event); | |
4767 | fdput(output); | |
4768 | } else { | |
4769 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 4770 | } |
ac9721f3 PZ |
4771 | return ret; |
4772 | } | |
a4be7c27 | 4773 | |
6fb2915d LZ |
4774 | case PERF_EVENT_IOC_SET_FILTER: |
4775 | return perf_event_set_filter(event, (void __user *)arg); | |
4776 | ||
2541517c AS |
4777 | case PERF_EVENT_IOC_SET_BPF: |
4778 | return perf_event_set_bpf_prog(event, arg); | |
4779 | ||
86e7972f WN |
4780 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
4781 | struct ring_buffer *rb; | |
4782 | ||
4783 | rcu_read_lock(); | |
4784 | rb = rcu_dereference(event->rb); | |
4785 | if (!rb || !rb->nr_pages) { | |
4786 | rcu_read_unlock(); | |
4787 | return -EINVAL; | |
4788 | } | |
4789 | rb_toggle_paused(rb, !!arg); | |
4790 | rcu_read_unlock(); | |
4791 | return 0; | |
4792 | } | |
d859e29f | 4793 | default: |
3df5edad | 4794 | return -ENOTTY; |
d859e29f | 4795 | } |
3df5edad PZ |
4796 | |
4797 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 4798 | perf_event_for_each(event, func); |
3df5edad | 4799 | else |
cdd6c482 | 4800 | perf_event_for_each_child(event, func); |
3df5edad PZ |
4801 | |
4802 | return 0; | |
d859e29f PM |
4803 | } |
4804 | ||
f63a8daa PZ |
4805 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4806 | { | |
4807 | struct perf_event *event = file->private_data; | |
4808 | struct perf_event_context *ctx; | |
4809 | long ret; | |
4810 | ||
4811 | ctx = perf_event_ctx_lock(event); | |
4812 | ret = _perf_ioctl(event, cmd, arg); | |
4813 | perf_event_ctx_unlock(event, ctx); | |
4814 | ||
4815 | return ret; | |
4816 | } | |
4817 | ||
b3f20785 PM |
4818 | #ifdef CONFIG_COMPAT |
4819 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
4820 | unsigned long arg) | |
4821 | { | |
4822 | switch (_IOC_NR(cmd)) { | |
4823 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
4824 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
4825 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
4826 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
4827 | cmd &= ~IOCSIZE_MASK; | |
4828 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
4829 | } | |
4830 | break; | |
4831 | } | |
4832 | return perf_ioctl(file, cmd, arg); | |
4833 | } | |
4834 | #else | |
4835 | # define perf_compat_ioctl NULL | |
4836 | #endif | |
4837 | ||
cdd6c482 | 4838 | int perf_event_task_enable(void) |
771d7cde | 4839 | { |
f63a8daa | 4840 | struct perf_event_context *ctx; |
cdd6c482 | 4841 | struct perf_event *event; |
771d7cde | 4842 | |
cdd6c482 | 4843 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4844 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4845 | ctx = perf_event_ctx_lock(event); | |
4846 | perf_event_for_each_child(event, _perf_event_enable); | |
4847 | perf_event_ctx_unlock(event, ctx); | |
4848 | } | |
cdd6c482 | 4849 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4850 | |
4851 | return 0; | |
4852 | } | |
4853 | ||
cdd6c482 | 4854 | int perf_event_task_disable(void) |
771d7cde | 4855 | { |
f63a8daa | 4856 | struct perf_event_context *ctx; |
cdd6c482 | 4857 | struct perf_event *event; |
771d7cde | 4858 | |
cdd6c482 | 4859 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4860 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4861 | ctx = perf_event_ctx_lock(event); | |
4862 | perf_event_for_each_child(event, _perf_event_disable); | |
4863 | perf_event_ctx_unlock(event, ctx); | |
4864 | } | |
cdd6c482 | 4865 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4866 | |
4867 | return 0; | |
4868 | } | |
4869 | ||
cdd6c482 | 4870 | static int perf_event_index(struct perf_event *event) |
194002b2 | 4871 | { |
a4eaf7f1 PZ |
4872 | if (event->hw.state & PERF_HES_STOPPED) |
4873 | return 0; | |
4874 | ||
cdd6c482 | 4875 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
4876 | return 0; |
4877 | ||
35edc2a5 | 4878 | return event->pmu->event_idx(event); |
194002b2 PZ |
4879 | } |
4880 | ||
c4794295 | 4881 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 4882 | u64 *now, |
7f310a5d EM |
4883 | u64 *enabled, |
4884 | u64 *running) | |
c4794295 | 4885 | { |
e3f3541c | 4886 | u64 ctx_time; |
c4794295 | 4887 | |
e3f3541c PZ |
4888 | *now = perf_clock(); |
4889 | ctx_time = event->shadow_ctx_time + *now; | |
c4794295 EM |
4890 | *enabled = ctx_time - event->tstamp_enabled; |
4891 | *running = ctx_time - event->tstamp_running; | |
4892 | } | |
4893 | ||
fa731587 PZ |
4894 | static void perf_event_init_userpage(struct perf_event *event) |
4895 | { | |
4896 | struct perf_event_mmap_page *userpg; | |
4897 | struct ring_buffer *rb; | |
4898 | ||
4899 | rcu_read_lock(); | |
4900 | rb = rcu_dereference(event->rb); | |
4901 | if (!rb) | |
4902 | goto unlock; | |
4903 | ||
4904 | userpg = rb->user_page; | |
4905 | ||
4906 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
4907 | userpg->cap_bit0_is_deprecated = 1; | |
4908 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
4909 | userpg->data_offset = PAGE_SIZE; |
4910 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
4911 | |
4912 | unlock: | |
4913 | rcu_read_unlock(); | |
4914 | } | |
4915 | ||
c1317ec2 AL |
4916 | void __weak arch_perf_update_userpage( |
4917 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
4918 | { |
4919 | } | |
4920 | ||
38ff667b PZ |
4921 | /* |
4922 | * Callers need to ensure there can be no nesting of this function, otherwise | |
4923 | * the seqlock logic goes bad. We can not serialize this because the arch | |
4924 | * code calls this from NMI context. | |
4925 | */ | |
cdd6c482 | 4926 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 4927 | { |
cdd6c482 | 4928 | struct perf_event_mmap_page *userpg; |
76369139 | 4929 | struct ring_buffer *rb; |
e3f3541c | 4930 | u64 enabled, running, now; |
38ff667b PZ |
4931 | |
4932 | rcu_read_lock(); | |
5ec4c599 PZ |
4933 | rb = rcu_dereference(event->rb); |
4934 | if (!rb) | |
4935 | goto unlock; | |
4936 | ||
0d641208 EM |
4937 | /* |
4938 | * compute total_time_enabled, total_time_running | |
4939 | * based on snapshot values taken when the event | |
4940 | * was last scheduled in. | |
4941 | * | |
4942 | * we cannot simply called update_context_time() | |
4943 | * because of locking issue as we can be called in | |
4944 | * NMI context | |
4945 | */ | |
e3f3541c | 4946 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 4947 | |
76369139 | 4948 | userpg = rb->user_page; |
7b732a75 PZ |
4949 | /* |
4950 | * Disable preemption so as to not let the corresponding user-space | |
4951 | * spin too long if we get preempted. | |
4952 | */ | |
4953 | preempt_disable(); | |
37d81828 | 4954 | ++userpg->lock; |
92f22a38 | 4955 | barrier(); |
cdd6c482 | 4956 | userpg->index = perf_event_index(event); |
b5e58793 | 4957 | userpg->offset = perf_event_count(event); |
365a4038 | 4958 | if (userpg->index) |
e7850595 | 4959 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 4960 | |
0d641208 | 4961 | userpg->time_enabled = enabled + |
cdd6c482 | 4962 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 4963 | |
0d641208 | 4964 | userpg->time_running = running + |
cdd6c482 | 4965 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 4966 | |
c1317ec2 | 4967 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 4968 | |
92f22a38 | 4969 | barrier(); |
37d81828 | 4970 | ++userpg->lock; |
7b732a75 | 4971 | preempt_enable(); |
38ff667b | 4972 | unlock: |
7b732a75 | 4973 | rcu_read_unlock(); |
37d81828 PM |
4974 | } |
4975 | ||
11bac800 | 4976 | static int perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 4977 | { |
11bac800 | 4978 | struct perf_event *event = vmf->vma->vm_file->private_data; |
76369139 | 4979 | struct ring_buffer *rb; |
906010b2 PZ |
4980 | int ret = VM_FAULT_SIGBUS; |
4981 | ||
4982 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
4983 | if (vmf->pgoff == 0) | |
4984 | ret = 0; | |
4985 | return ret; | |
4986 | } | |
4987 | ||
4988 | rcu_read_lock(); | |
76369139 FW |
4989 | rb = rcu_dereference(event->rb); |
4990 | if (!rb) | |
906010b2 PZ |
4991 | goto unlock; |
4992 | ||
4993 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
4994 | goto unlock; | |
4995 | ||
76369139 | 4996 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
4997 | if (!vmf->page) |
4998 | goto unlock; | |
4999 | ||
5000 | get_page(vmf->page); | |
11bac800 | 5001 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
5002 | vmf->page->index = vmf->pgoff; |
5003 | ||
5004 | ret = 0; | |
5005 | unlock: | |
5006 | rcu_read_unlock(); | |
5007 | ||
5008 | return ret; | |
5009 | } | |
5010 | ||
10c6db11 PZ |
5011 | static void ring_buffer_attach(struct perf_event *event, |
5012 | struct ring_buffer *rb) | |
5013 | { | |
b69cf536 | 5014 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
5015 | unsigned long flags; |
5016 | ||
b69cf536 PZ |
5017 | if (event->rb) { |
5018 | /* | |
5019 | * Should be impossible, we set this when removing | |
5020 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
5021 | */ | |
5022 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 5023 | |
b69cf536 | 5024 | old_rb = event->rb; |
b69cf536 PZ |
5025 | spin_lock_irqsave(&old_rb->event_lock, flags); |
5026 | list_del_rcu(&event->rb_entry); | |
5027 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 5028 | |
2f993cf0 ON |
5029 | event->rcu_batches = get_state_synchronize_rcu(); |
5030 | event->rcu_pending = 1; | |
b69cf536 | 5031 | } |
10c6db11 | 5032 | |
b69cf536 | 5033 | if (rb) { |
2f993cf0 ON |
5034 | if (event->rcu_pending) { |
5035 | cond_synchronize_rcu(event->rcu_batches); | |
5036 | event->rcu_pending = 0; | |
5037 | } | |
5038 | ||
b69cf536 PZ |
5039 | spin_lock_irqsave(&rb->event_lock, flags); |
5040 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
5041 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
5042 | } | |
5043 | ||
767ae086 AS |
5044 | /* |
5045 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
5046 | * before swizzling the event::rb pointer; if it's getting | |
5047 | * unmapped, its aux_mmap_count will be 0 and it won't | |
5048 | * restart. See the comment in __perf_pmu_output_stop(). | |
5049 | * | |
5050 | * Data will inevitably be lost when set_output is done in | |
5051 | * mid-air, but then again, whoever does it like this is | |
5052 | * not in for the data anyway. | |
5053 | */ | |
5054 | if (has_aux(event)) | |
5055 | perf_event_stop(event, 0); | |
5056 | ||
b69cf536 PZ |
5057 | rcu_assign_pointer(event->rb, rb); |
5058 | ||
5059 | if (old_rb) { | |
5060 | ring_buffer_put(old_rb); | |
5061 | /* | |
5062 | * Since we detached before setting the new rb, so that we | |
5063 | * could attach the new rb, we could have missed a wakeup. | |
5064 | * Provide it now. | |
5065 | */ | |
5066 | wake_up_all(&event->waitq); | |
5067 | } | |
10c6db11 PZ |
5068 | } |
5069 | ||
5070 | static void ring_buffer_wakeup(struct perf_event *event) | |
5071 | { | |
5072 | struct ring_buffer *rb; | |
5073 | ||
5074 | rcu_read_lock(); | |
5075 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
5076 | if (rb) { |
5077 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
5078 | wake_up_all(&event->waitq); | |
5079 | } | |
10c6db11 PZ |
5080 | rcu_read_unlock(); |
5081 | } | |
5082 | ||
fdc26706 | 5083 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 5084 | { |
76369139 | 5085 | struct ring_buffer *rb; |
7b732a75 | 5086 | |
ac9721f3 | 5087 | rcu_read_lock(); |
76369139 FW |
5088 | rb = rcu_dereference(event->rb); |
5089 | if (rb) { | |
5090 | if (!atomic_inc_not_zero(&rb->refcount)) | |
5091 | rb = NULL; | |
ac9721f3 PZ |
5092 | } |
5093 | rcu_read_unlock(); | |
5094 | ||
76369139 | 5095 | return rb; |
ac9721f3 PZ |
5096 | } |
5097 | ||
fdc26706 | 5098 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 5099 | { |
76369139 | 5100 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 5101 | return; |
7b732a75 | 5102 | |
9bb5d40c | 5103 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 5104 | |
76369139 | 5105 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
5106 | } |
5107 | ||
5108 | static void perf_mmap_open(struct vm_area_struct *vma) | |
5109 | { | |
cdd6c482 | 5110 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5111 | |
cdd6c482 | 5112 | atomic_inc(&event->mmap_count); |
9bb5d40c | 5113 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 5114 | |
45bfb2e5 PZ |
5115 | if (vma->vm_pgoff) |
5116 | atomic_inc(&event->rb->aux_mmap_count); | |
5117 | ||
1e0fb9ec | 5118 | if (event->pmu->event_mapped) |
bfe33492 | 5119 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
5120 | } |
5121 | ||
95ff4ca2 AS |
5122 | static void perf_pmu_output_stop(struct perf_event *event); |
5123 | ||
9bb5d40c PZ |
5124 | /* |
5125 | * A buffer can be mmap()ed multiple times; either directly through the same | |
5126 | * event, or through other events by use of perf_event_set_output(). | |
5127 | * | |
5128 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
5129 | * the buffer here, where we still have a VM context. This means we need | |
5130 | * to detach all events redirecting to us. | |
5131 | */ | |
7b732a75 PZ |
5132 | static void perf_mmap_close(struct vm_area_struct *vma) |
5133 | { | |
cdd6c482 | 5134 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5135 | |
b69cf536 | 5136 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
5137 | struct user_struct *mmap_user = rb->mmap_user; |
5138 | int mmap_locked = rb->mmap_locked; | |
5139 | unsigned long size = perf_data_size(rb); | |
789f90fc | 5140 | |
1e0fb9ec | 5141 | if (event->pmu->event_unmapped) |
bfe33492 | 5142 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 5143 | |
45bfb2e5 PZ |
5144 | /* |
5145 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
5146 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
5147 | * serialize with perf_mmap here. | |
5148 | */ | |
5149 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
5150 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
5151 | /* |
5152 | * Stop all AUX events that are writing to this buffer, | |
5153 | * so that we can free its AUX pages and corresponding PMU | |
5154 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
5155 | * they won't start any more (see perf_aux_output_begin()). | |
5156 | */ | |
5157 | perf_pmu_output_stop(event); | |
5158 | ||
5159 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
5160 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
5161 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
5162 | ||
95ff4ca2 | 5163 | /* this has to be the last one */ |
45bfb2e5 | 5164 | rb_free_aux(rb); |
95ff4ca2 AS |
5165 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
5166 | ||
45bfb2e5 PZ |
5167 | mutex_unlock(&event->mmap_mutex); |
5168 | } | |
5169 | ||
9bb5d40c PZ |
5170 | atomic_dec(&rb->mmap_count); |
5171 | ||
5172 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 5173 | goto out_put; |
9bb5d40c | 5174 | |
b69cf536 | 5175 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
5176 | mutex_unlock(&event->mmap_mutex); |
5177 | ||
5178 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
5179 | if (atomic_read(&rb->mmap_count)) |
5180 | goto out_put; | |
ac9721f3 | 5181 | |
9bb5d40c PZ |
5182 | /* |
5183 | * No other mmap()s, detach from all other events that might redirect | |
5184 | * into the now unreachable buffer. Somewhat complicated by the | |
5185 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
5186 | */ | |
5187 | again: | |
5188 | rcu_read_lock(); | |
5189 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
5190 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
5191 | /* | |
5192 | * This event is en-route to free_event() which will | |
5193 | * detach it and remove it from the list. | |
5194 | */ | |
5195 | continue; | |
5196 | } | |
5197 | rcu_read_unlock(); | |
789f90fc | 5198 | |
9bb5d40c PZ |
5199 | mutex_lock(&event->mmap_mutex); |
5200 | /* | |
5201 | * Check we didn't race with perf_event_set_output() which can | |
5202 | * swizzle the rb from under us while we were waiting to | |
5203 | * acquire mmap_mutex. | |
5204 | * | |
5205 | * If we find a different rb; ignore this event, a next | |
5206 | * iteration will no longer find it on the list. We have to | |
5207 | * still restart the iteration to make sure we're not now | |
5208 | * iterating the wrong list. | |
5209 | */ | |
b69cf536 PZ |
5210 | if (event->rb == rb) |
5211 | ring_buffer_attach(event, NULL); | |
5212 | ||
cdd6c482 | 5213 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 5214 | put_event(event); |
ac9721f3 | 5215 | |
9bb5d40c PZ |
5216 | /* |
5217 | * Restart the iteration; either we're on the wrong list or | |
5218 | * destroyed its integrity by doing a deletion. | |
5219 | */ | |
5220 | goto again; | |
7b732a75 | 5221 | } |
9bb5d40c PZ |
5222 | rcu_read_unlock(); |
5223 | ||
5224 | /* | |
5225 | * It could be there's still a few 0-ref events on the list; they'll | |
5226 | * get cleaned up by free_event() -- they'll also still have their | |
5227 | * ref on the rb and will free it whenever they are done with it. | |
5228 | * | |
5229 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
5230 | * undo the VM accounting. | |
5231 | */ | |
5232 | ||
5233 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
5234 | vma->vm_mm->pinned_vm -= mmap_locked; | |
5235 | free_uid(mmap_user); | |
5236 | ||
b69cf536 | 5237 | out_put: |
9bb5d40c | 5238 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
5239 | } |
5240 | ||
f0f37e2f | 5241 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 5242 | .open = perf_mmap_open, |
45bfb2e5 | 5243 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
5244 | .fault = perf_mmap_fault, |
5245 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
5246 | }; |
5247 | ||
5248 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
5249 | { | |
cdd6c482 | 5250 | struct perf_event *event = file->private_data; |
22a4f650 | 5251 | unsigned long user_locked, user_lock_limit; |
789f90fc | 5252 | struct user_struct *user = current_user(); |
22a4f650 | 5253 | unsigned long locked, lock_limit; |
45bfb2e5 | 5254 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
5255 | unsigned long vma_size; |
5256 | unsigned long nr_pages; | |
45bfb2e5 | 5257 | long user_extra = 0, extra = 0; |
d57e34fd | 5258 | int ret = 0, flags = 0; |
37d81828 | 5259 | |
c7920614 PZ |
5260 | /* |
5261 | * Don't allow mmap() of inherited per-task counters. This would | |
5262 | * create a performance issue due to all children writing to the | |
76369139 | 5263 | * same rb. |
c7920614 PZ |
5264 | */ |
5265 | if (event->cpu == -1 && event->attr.inherit) | |
5266 | return -EINVAL; | |
5267 | ||
43a21ea8 | 5268 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 5269 | return -EINVAL; |
7b732a75 PZ |
5270 | |
5271 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
5272 | |
5273 | if (vma->vm_pgoff == 0) { | |
5274 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
5275 | } else { | |
5276 | /* | |
5277 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
5278 | * mapped, all subsequent mappings should have the same size | |
5279 | * and offset. Must be above the normal perf buffer. | |
5280 | */ | |
5281 | u64 aux_offset, aux_size; | |
5282 | ||
5283 | if (!event->rb) | |
5284 | return -EINVAL; | |
5285 | ||
5286 | nr_pages = vma_size / PAGE_SIZE; | |
5287 | ||
5288 | mutex_lock(&event->mmap_mutex); | |
5289 | ret = -EINVAL; | |
5290 | ||
5291 | rb = event->rb; | |
5292 | if (!rb) | |
5293 | goto aux_unlock; | |
5294 | ||
5295 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); | |
5296 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); | |
5297 | ||
5298 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
5299 | goto aux_unlock; | |
5300 | ||
5301 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
5302 | goto aux_unlock; | |
5303 | ||
5304 | /* already mapped with a different offset */ | |
5305 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
5306 | goto aux_unlock; | |
5307 | ||
5308 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
5309 | goto aux_unlock; | |
5310 | ||
5311 | /* already mapped with a different size */ | |
5312 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
5313 | goto aux_unlock; | |
5314 | ||
5315 | if (!is_power_of_2(nr_pages)) | |
5316 | goto aux_unlock; | |
5317 | ||
5318 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
5319 | goto aux_unlock; | |
5320 | ||
5321 | if (rb_has_aux(rb)) { | |
5322 | atomic_inc(&rb->aux_mmap_count); | |
5323 | ret = 0; | |
5324 | goto unlock; | |
5325 | } | |
5326 | ||
5327 | atomic_set(&rb->aux_mmap_count, 1); | |
5328 | user_extra = nr_pages; | |
5329 | ||
5330 | goto accounting; | |
5331 | } | |
7b732a75 | 5332 | |
7730d865 | 5333 | /* |
76369139 | 5334 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
5335 | * can do bitmasks instead of modulo. |
5336 | */ | |
2ed11312 | 5337 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
5338 | return -EINVAL; |
5339 | ||
7b732a75 | 5340 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
5341 | return -EINVAL; |
5342 | ||
cdd6c482 | 5343 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 5344 | again: |
cdd6c482 | 5345 | mutex_lock(&event->mmap_mutex); |
76369139 | 5346 | if (event->rb) { |
9bb5d40c | 5347 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 5348 | ret = -EINVAL; |
9bb5d40c PZ |
5349 | goto unlock; |
5350 | } | |
5351 | ||
5352 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
5353 | /* | |
5354 | * Raced against perf_mmap_close() through | |
5355 | * perf_event_set_output(). Try again, hope for better | |
5356 | * luck. | |
5357 | */ | |
5358 | mutex_unlock(&event->mmap_mutex); | |
5359 | goto again; | |
5360 | } | |
5361 | ||
ebb3c4c4 PZ |
5362 | goto unlock; |
5363 | } | |
5364 | ||
789f90fc | 5365 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
5366 | |
5367 | accounting: | |
cdd6c482 | 5368 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
5369 | |
5370 | /* | |
5371 | * Increase the limit linearly with more CPUs: | |
5372 | */ | |
5373 | user_lock_limit *= num_online_cpus(); | |
5374 | ||
789f90fc | 5375 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 5376 | |
789f90fc PZ |
5377 | if (user_locked > user_lock_limit) |
5378 | extra = user_locked - user_lock_limit; | |
7b732a75 | 5379 | |
78d7d407 | 5380 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 5381 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 5382 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 5383 | |
459ec28a IM |
5384 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
5385 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
5386 | ret = -EPERM; |
5387 | goto unlock; | |
5388 | } | |
7b732a75 | 5389 | |
45bfb2e5 | 5390 | WARN_ON(!rb && event->rb); |
906010b2 | 5391 | |
d57e34fd | 5392 | if (vma->vm_flags & VM_WRITE) |
76369139 | 5393 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 5394 | |
76369139 | 5395 | if (!rb) { |
45bfb2e5 PZ |
5396 | rb = rb_alloc(nr_pages, |
5397 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
5398 | event->cpu, flags); | |
26cb63ad | 5399 | |
45bfb2e5 PZ |
5400 | if (!rb) { |
5401 | ret = -ENOMEM; | |
5402 | goto unlock; | |
5403 | } | |
43a21ea8 | 5404 | |
45bfb2e5 PZ |
5405 | atomic_set(&rb->mmap_count, 1); |
5406 | rb->mmap_user = get_current_user(); | |
5407 | rb->mmap_locked = extra; | |
26cb63ad | 5408 | |
45bfb2e5 | 5409 | ring_buffer_attach(event, rb); |
ac9721f3 | 5410 | |
45bfb2e5 PZ |
5411 | perf_event_init_userpage(event); |
5412 | perf_event_update_userpage(event); | |
5413 | } else { | |
1a594131 AS |
5414 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5415 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5416 | if (!ret) |
5417 | rb->aux_mmap_locked = extra; | |
5418 | } | |
9a0f05cb | 5419 | |
ebb3c4c4 | 5420 | unlock: |
45bfb2e5 PZ |
5421 | if (!ret) { |
5422 | atomic_long_add(user_extra, &user->locked_vm); | |
5423 | vma->vm_mm->pinned_vm += extra; | |
5424 | ||
ac9721f3 | 5425 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5426 | } else if (rb) { |
5427 | atomic_dec(&rb->mmap_count); | |
5428 | } | |
5429 | aux_unlock: | |
cdd6c482 | 5430 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5431 | |
9bb5d40c PZ |
5432 | /* |
5433 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5434 | * vma. | |
5435 | */ | |
26cb63ad | 5436 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5437 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5438 | |
1e0fb9ec | 5439 | if (event->pmu->event_mapped) |
bfe33492 | 5440 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 5441 | |
7b732a75 | 5442 | return ret; |
37d81828 PM |
5443 | } |
5444 | ||
3c446b3d PZ |
5445 | static int perf_fasync(int fd, struct file *filp, int on) |
5446 | { | |
496ad9aa | 5447 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5448 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5449 | int retval; |
5450 | ||
5955102c | 5451 | inode_lock(inode); |
cdd6c482 | 5452 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5453 | inode_unlock(inode); |
3c446b3d PZ |
5454 | |
5455 | if (retval < 0) | |
5456 | return retval; | |
5457 | ||
5458 | return 0; | |
5459 | } | |
5460 | ||
0793a61d | 5461 | static const struct file_operations perf_fops = { |
3326c1ce | 5462 | .llseek = no_llseek, |
0793a61d TG |
5463 | .release = perf_release, |
5464 | .read = perf_read, | |
5465 | .poll = perf_poll, | |
d859e29f | 5466 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5467 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5468 | .mmap = perf_mmap, |
3c446b3d | 5469 | .fasync = perf_fasync, |
0793a61d TG |
5470 | }; |
5471 | ||
925d519a | 5472 | /* |
cdd6c482 | 5473 | * Perf event wakeup |
925d519a PZ |
5474 | * |
5475 | * If there's data, ensure we set the poll() state and publish everything | |
5476 | * to user-space before waking everybody up. | |
5477 | */ | |
5478 | ||
fed66e2c PZ |
5479 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5480 | { | |
5481 | /* only the parent has fasync state */ | |
5482 | if (event->parent) | |
5483 | event = event->parent; | |
5484 | return &event->fasync; | |
5485 | } | |
5486 | ||
cdd6c482 | 5487 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5488 | { |
10c6db11 | 5489 | ring_buffer_wakeup(event); |
4c9e2542 | 5490 | |
cdd6c482 | 5491 | if (event->pending_kill) { |
fed66e2c | 5492 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5493 | event->pending_kill = 0; |
4c9e2542 | 5494 | } |
925d519a PZ |
5495 | } |
5496 | ||
e360adbe | 5497 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5498 | { |
cdd6c482 IM |
5499 | struct perf_event *event = container_of(entry, |
5500 | struct perf_event, pending); | |
d525211f PZ |
5501 | int rctx; |
5502 | ||
5503 | rctx = perf_swevent_get_recursion_context(); | |
5504 | /* | |
5505 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5506 | * and we won't recurse 'further'. | |
5507 | */ | |
79f14641 | 5508 | |
cdd6c482 IM |
5509 | if (event->pending_disable) { |
5510 | event->pending_disable = 0; | |
fae3fde6 | 5511 | perf_event_disable_local(event); |
79f14641 PZ |
5512 | } |
5513 | ||
cdd6c482 IM |
5514 | if (event->pending_wakeup) { |
5515 | event->pending_wakeup = 0; | |
5516 | perf_event_wakeup(event); | |
79f14641 | 5517 | } |
d525211f PZ |
5518 | |
5519 | if (rctx >= 0) | |
5520 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5521 | } |
5522 | ||
39447b38 ZY |
5523 | /* |
5524 | * We assume there is only KVM supporting the callbacks. | |
5525 | * Later on, we might change it to a list if there is | |
5526 | * another virtualization implementation supporting the callbacks. | |
5527 | */ | |
5528 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5529 | ||
5530 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5531 | { | |
5532 | perf_guest_cbs = cbs; | |
5533 | return 0; | |
5534 | } | |
5535 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5536 | ||
5537 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5538 | { | |
5539 | perf_guest_cbs = NULL; | |
5540 | return 0; | |
5541 | } | |
5542 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5543 | ||
4018994f JO |
5544 | static void |
5545 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5546 | struct pt_regs *regs, u64 mask) | |
5547 | { | |
5548 | int bit; | |
29dd3288 | 5549 | DECLARE_BITMAP(_mask, 64); |
4018994f | 5550 | |
29dd3288 MS |
5551 | bitmap_from_u64(_mask, mask); |
5552 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
5553 | u64 val; |
5554 | ||
5555 | val = perf_reg_value(regs, bit); | |
5556 | perf_output_put(handle, val); | |
5557 | } | |
5558 | } | |
5559 | ||
60e2364e | 5560 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5561 | struct pt_regs *regs, |
5562 | struct pt_regs *regs_user_copy) | |
4018994f | 5563 | { |
88a7c26a AL |
5564 | if (user_mode(regs)) { |
5565 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5566 | regs_user->regs = regs; |
88a7c26a AL |
5567 | } else if (current->mm) { |
5568 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5569 | } else { |
5570 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5571 | regs_user->regs = NULL; | |
4018994f JO |
5572 | } |
5573 | } | |
5574 | ||
60e2364e SE |
5575 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5576 | struct pt_regs *regs) | |
5577 | { | |
5578 | regs_intr->regs = regs; | |
5579 | regs_intr->abi = perf_reg_abi(current); | |
5580 | } | |
5581 | ||
5582 | ||
c5ebcedb JO |
5583 | /* |
5584 | * Get remaining task size from user stack pointer. | |
5585 | * | |
5586 | * It'd be better to take stack vma map and limit this more | |
5587 | * precisly, but there's no way to get it safely under interrupt, | |
5588 | * so using TASK_SIZE as limit. | |
5589 | */ | |
5590 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5591 | { | |
5592 | unsigned long addr = perf_user_stack_pointer(regs); | |
5593 | ||
5594 | if (!addr || addr >= TASK_SIZE) | |
5595 | return 0; | |
5596 | ||
5597 | return TASK_SIZE - addr; | |
5598 | } | |
5599 | ||
5600 | static u16 | |
5601 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5602 | struct pt_regs *regs) | |
5603 | { | |
5604 | u64 task_size; | |
5605 | ||
5606 | /* No regs, no stack pointer, no dump. */ | |
5607 | if (!regs) | |
5608 | return 0; | |
5609 | ||
5610 | /* | |
5611 | * Check if we fit in with the requested stack size into the: | |
5612 | * - TASK_SIZE | |
5613 | * If we don't, we limit the size to the TASK_SIZE. | |
5614 | * | |
5615 | * - remaining sample size | |
5616 | * If we don't, we customize the stack size to | |
5617 | * fit in to the remaining sample size. | |
5618 | */ | |
5619 | ||
5620 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5621 | stack_size = min(stack_size, (u16) task_size); | |
5622 | ||
5623 | /* Current header size plus static size and dynamic size. */ | |
5624 | header_size += 2 * sizeof(u64); | |
5625 | ||
5626 | /* Do we fit in with the current stack dump size? */ | |
5627 | if ((u16) (header_size + stack_size) < header_size) { | |
5628 | /* | |
5629 | * If we overflow the maximum size for the sample, | |
5630 | * we customize the stack dump size to fit in. | |
5631 | */ | |
5632 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5633 | stack_size = round_up(stack_size, sizeof(u64)); | |
5634 | } | |
5635 | ||
5636 | return stack_size; | |
5637 | } | |
5638 | ||
5639 | static void | |
5640 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5641 | struct pt_regs *regs) | |
5642 | { | |
5643 | /* Case of a kernel thread, nothing to dump */ | |
5644 | if (!regs) { | |
5645 | u64 size = 0; | |
5646 | perf_output_put(handle, size); | |
5647 | } else { | |
5648 | unsigned long sp; | |
5649 | unsigned int rem; | |
5650 | u64 dyn_size; | |
5651 | ||
5652 | /* | |
5653 | * We dump: | |
5654 | * static size | |
5655 | * - the size requested by user or the best one we can fit | |
5656 | * in to the sample max size | |
5657 | * data | |
5658 | * - user stack dump data | |
5659 | * dynamic size | |
5660 | * - the actual dumped size | |
5661 | */ | |
5662 | ||
5663 | /* Static size. */ | |
5664 | perf_output_put(handle, dump_size); | |
5665 | ||
5666 | /* Data. */ | |
5667 | sp = perf_user_stack_pointer(regs); | |
5668 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5669 | dyn_size = dump_size - rem; | |
5670 | ||
5671 | perf_output_skip(handle, rem); | |
5672 | ||
5673 | /* Dynamic size. */ | |
5674 | perf_output_put(handle, dyn_size); | |
5675 | } | |
5676 | } | |
5677 | ||
c980d109 ACM |
5678 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5679 | struct perf_sample_data *data, | |
5680 | struct perf_event *event) | |
6844c09d ACM |
5681 | { |
5682 | u64 sample_type = event->attr.sample_type; | |
5683 | ||
5684 | data->type = sample_type; | |
5685 | header->size += event->id_header_size; | |
5686 | ||
5687 | if (sample_type & PERF_SAMPLE_TID) { | |
5688 | /* namespace issues */ | |
5689 | data->tid_entry.pid = perf_event_pid(event, current); | |
5690 | data->tid_entry.tid = perf_event_tid(event, current); | |
5691 | } | |
5692 | ||
5693 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5694 | data->time = perf_event_clock(event); |
6844c09d | 5695 | |
ff3d527c | 5696 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5697 | data->id = primary_event_id(event); |
5698 | ||
5699 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5700 | data->stream_id = event->id; | |
5701 | ||
5702 | if (sample_type & PERF_SAMPLE_CPU) { | |
5703 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5704 | data->cpu_entry.reserved = 0; | |
5705 | } | |
5706 | } | |
5707 | ||
76369139 FW |
5708 | void perf_event_header__init_id(struct perf_event_header *header, |
5709 | struct perf_sample_data *data, | |
5710 | struct perf_event *event) | |
c980d109 ACM |
5711 | { |
5712 | if (event->attr.sample_id_all) | |
5713 | __perf_event_header__init_id(header, data, event); | |
5714 | } | |
5715 | ||
5716 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5717 | struct perf_sample_data *data) | |
5718 | { | |
5719 | u64 sample_type = data->type; | |
5720 | ||
5721 | if (sample_type & PERF_SAMPLE_TID) | |
5722 | perf_output_put(handle, data->tid_entry); | |
5723 | ||
5724 | if (sample_type & PERF_SAMPLE_TIME) | |
5725 | perf_output_put(handle, data->time); | |
5726 | ||
5727 | if (sample_type & PERF_SAMPLE_ID) | |
5728 | perf_output_put(handle, data->id); | |
5729 | ||
5730 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5731 | perf_output_put(handle, data->stream_id); | |
5732 | ||
5733 | if (sample_type & PERF_SAMPLE_CPU) | |
5734 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5735 | |
5736 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5737 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5738 | } |
5739 | ||
76369139 FW |
5740 | void perf_event__output_id_sample(struct perf_event *event, |
5741 | struct perf_output_handle *handle, | |
5742 | struct perf_sample_data *sample) | |
c980d109 ACM |
5743 | { |
5744 | if (event->attr.sample_id_all) | |
5745 | __perf_event__output_id_sample(handle, sample); | |
5746 | } | |
5747 | ||
3dab77fb | 5748 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5749 | struct perf_event *event, |
5750 | u64 enabled, u64 running) | |
3dab77fb | 5751 | { |
cdd6c482 | 5752 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5753 | u64 values[4]; |
5754 | int n = 0; | |
5755 | ||
b5e58793 | 5756 | values[n++] = perf_event_count(event); |
3dab77fb | 5757 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5758 | values[n++] = enabled + |
cdd6c482 | 5759 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5760 | } |
5761 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5762 | values[n++] = running + |
cdd6c482 | 5763 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5764 | } |
5765 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 5766 | values[n++] = primary_event_id(event); |
3dab77fb | 5767 | |
76369139 | 5768 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5769 | } |
5770 | ||
3dab77fb | 5771 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
5772 | struct perf_event *event, |
5773 | u64 enabled, u64 running) | |
3dab77fb | 5774 | { |
cdd6c482 IM |
5775 | struct perf_event *leader = event->group_leader, *sub; |
5776 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
5777 | u64 values[5]; |
5778 | int n = 0; | |
5779 | ||
5780 | values[n++] = 1 + leader->nr_siblings; | |
5781 | ||
5782 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 5783 | values[n++] = enabled; |
3dab77fb PZ |
5784 | |
5785 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 5786 | values[n++] = running; |
3dab77fb | 5787 | |
cdd6c482 | 5788 | if (leader != event) |
3dab77fb PZ |
5789 | leader->pmu->read(leader); |
5790 | ||
b5e58793 | 5791 | values[n++] = perf_event_count(leader); |
3dab77fb | 5792 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5793 | values[n++] = primary_event_id(leader); |
3dab77fb | 5794 | |
76369139 | 5795 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 5796 | |
65abc865 | 5797 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
3dab77fb PZ |
5798 | n = 0; |
5799 | ||
6f5ab001 JO |
5800 | if ((sub != event) && |
5801 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
5802 | sub->pmu->read(sub); |
5803 | ||
b5e58793 | 5804 | values[n++] = perf_event_count(sub); |
3dab77fb | 5805 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5806 | values[n++] = primary_event_id(sub); |
3dab77fb | 5807 | |
76369139 | 5808 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5809 | } |
5810 | } | |
5811 | ||
eed01528 SE |
5812 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
5813 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5814 | ||
ba5213ae PZ |
5815 | /* |
5816 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
5817 | * | |
5818 | * The problem is that its both hard and excessively expensive to iterate the | |
5819 | * child list, not to mention that its impossible to IPI the children running | |
5820 | * on another CPU, from interrupt/NMI context. | |
5821 | */ | |
3dab77fb | 5822 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 5823 | struct perf_event *event) |
3dab77fb | 5824 | { |
e3f3541c | 5825 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
5826 | u64 read_format = event->attr.read_format; |
5827 | ||
5828 | /* | |
5829 | * compute total_time_enabled, total_time_running | |
5830 | * based on snapshot values taken when the event | |
5831 | * was last scheduled in. | |
5832 | * | |
5833 | * we cannot simply called update_context_time() | |
5834 | * because of locking issue as we are called in | |
5835 | * NMI context | |
5836 | */ | |
c4794295 | 5837 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 5838 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 5839 | |
cdd6c482 | 5840 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 5841 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 5842 | else |
eed01528 | 5843 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
5844 | } |
5845 | ||
5622f295 MM |
5846 | void perf_output_sample(struct perf_output_handle *handle, |
5847 | struct perf_event_header *header, | |
5848 | struct perf_sample_data *data, | |
cdd6c482 | 5849 | struct perf_event *event) |
5622f295 MM |
5850 | { |
5851 | u64 sample_type = data->type; | |
5852 | ||
5853 | perf_output_put(handle, *header); | |
5854 | ||
ff3d527c AH |
5855 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
5856 | perf_output_put(handle, data->id); | |
5857 | ||
5622f295 MM |
5858 | if (sample_type & PERF_SAMPLE_IP) |
5859 | perf_output_put(handle, data->ip); | |
5860 | ||
5861 | if (sample_type & PERF_SAMPLE_TID) | |
5862 | perf_output_put(handle, data->tid_entry); | |
5863 | ||
5864 | if (sample_type & PERF_SAMPLE_TIME) | |
5865 | perf_output_put(handle, data->time); | |
5866 | ||
5867 | if (sample_type & PERF_SAMPLE_ADDR) | |
5868 | perf_output_put(handle, data->addr); | |
5869 | ||
5870 | if (sample_type & PERF_SAMPLE_ID) | |
5871 | perf_output_put(handle, data->id); | |
5872 | ||
5873 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5874 | perf_output_put(handle, data->stream_id); | |
5875 | ||
5876 | if (sample_type & PERF_SAMPLE_CPU) | |
5877 | perf_output_put(handle, data->cpu_entry); | |
5878 | ||
5879 | if (sample_type & PERF_SAMPLE_PERIOD) | |
5880 | perf_output_put(handle, data->period); | |
5881 | ||
5882 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 5883 | perf_output_read(handle, event); |
5622f295 MM |
5884 | |
5885 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
5886 | if (data->callchain) { | |
5887 | int size = 1; | |
5888 | ||
5889 | if (data->callchain) | |
5890 | size += data->callchain->nr; | |
5891 | ||
5892 | size *= sizeof(u64); | |
5893 | ||
76369139 | 5894 | __output_copy(handle, data->callchain, size); |
5622f295 MM |
5895 | } else { |
5896 | u64 nr = 0; | |
5897 | perf_output_put(handle, nr); | |
5898 | } | |
5899 | } | |
5900 | ||
5901 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
5902 | struct perf_raw_record *raw = data->raw; |
5903 | ||
5904 | if (raw) { | |
5905 | struct perf_raw_frag *frag = &raw->frag; | |
5906 | ||
5907 | perf_output_put(handle, raw->size); | |
5908 | do { | |
5909 | if (frag->copy) { | |
5910 | __output_custom(handle, frag->copy, | |
5911 | frag->data, frag->size); | |
5912 | } else { | |
5913 | __output_copy(handle, frag->data, | |
5914 | frag->size); | |
5915 | } | |
5916 | if (perf_raw_frag_last(frag)) | |
5917 | break; | |
5918 | frag = frag->next; | |
5919 | } while (1); | |
5920 | if (frag->pad) | |
5921 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
5922 | } else { |
5923 | struct { | |
5924 | u32 size; | |
5925 | u32 data; | |
5926 | } raw = { | |
5927 | .size = sizeof(u32), | |
5928 | .data = 0, | |
5929 | }; | |
5930 | perf_output_put(handle, raw); | |
5931 | } | |
5932 | } | |
a7ac67ea | 5933 | |
bce38cd5 SE |
5934 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
5935 | if (data->br_stack) { | |
5936 | size_t size; | |
5937 | ||
5938 | size = data->br_stack->nr | |
5939 | * sizeof(struct perf_branch_entry); | |
5940 | ||
5941 | perf_output_put(handle, data->br_stack->nr); | |
5942 | perf_output_copy(handle, data->br_stack->entries, size); | |
5943 | } else { | |
5944 | /* | |
5945 | * we always store at least the value of nr | |
5946 | */ | |
5947 | u64 nr = 0; | |
5948 | perf_output_put(handle, nr); | |
5949 | } | |
5950 | } | |
4018994f JO |
5951 | |
5952 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
5953 | u64 abi = data->regs_user.abi; | |
5954 | ||
5955 | /* | |
5956 | * If there are no regs to dump, notice it through | |
5957 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5958 | */ | |
5959 | perf_output_put(handle, abi); | |
5960 | ||
5961 | if (abi) { | |
5962 | u64 mask = event->attr.sample_regs_user; | |
5963 | perf_output_sample_regs(handle, | |
5964 | data->regs_user.regs, | |
5965 | mask); | |
5966 | } | |
5967 | } | |
c5ebcedb | 5968 | |
a5cdd40c | 5969 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
5970 | perf_output_sample_ustack(handle, |
5971 | data->stack_user_size, | |
5972 | data->regs_user.regs); | |
a5cdd40c | 5973 | } |
c3feedf2 AK |
5974 | |
5975 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
5976 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
5977 | |
5978 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
5979 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 5980 | |
fdfbbd07 AK |
5981 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
5982 | perf_output_put(handle, data->txn); | |
5983 | ||
60e2364e SE |
5984 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
5985 | u64 abi = data->regs_intr.abi; | |
5986 | /* | |
5987 | * If there are no regs to dump, notice it through | |
5988 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5989 | */ | |
5990 | perf_output_put(handle, abi); | |
5991 | ||
5992 | if (abi) { | |
5993 | u64 mask = event->attr.sample_regs_intr; | |
5994 | ||
5995 | perf_output_sample_regs(handle, | |
5996 | data->regs_intr.regs, | |
5997 | mask); | |
5998 | } | |
5999 | } | |
6000 | ||
a5cdd40c PZ |
6001 | if (!event->attr.watermark) { |
6002 | int wakeup_events = event->attr.wakeup_events; | |
6003 | ||
6004 | if (wakeup_events) { | |
6005 | struct ring_buffer *rb = handle->rb; | |
6006 | int events = local_inc_return(&rb->events); | |
6007 | ||
6008 | if (events >= wakeup_events) { | |
6009 | local_sub(wakeup_events, &rb->events); | |
6010 | local_inc(&rb->wakeup); | |
6011 | } | |
6012 | } | |
6013 | } | |
5622f295 MM |
6014 | } |
6015 | ||
6016 | void perf_prepare_sample(struct perf_event_header *header, | |
6017 | struct perf_sample_data *data, | |
cdd6c482 | 6018 | struct perf_event *event, |
5622f295 | 6019 | struct pt_regs *regs) |
7b732a75 | 6020 | { |
cdd6c482 | 6021 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 6022 | |
cdd6c482 | 6023 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 6024 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
6025 | |
6026 | header->misc = 0; | |
6027 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 6028 | |
c980d109 | 6029 | __perf_event_header__init_id(header, data, event); |
6844c09d | 6030 | |
c320c7b7 | 6031 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
6032 | data->ip = perf_instruction_pointer(regs); |
6033 | ||
b23f3325 | 6034 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 6035 | int size = 1; |
394ee076 | 6036 | |
e6dab5ff | 6037 | data->callchain = perf_callchain(event, regs); |
5622f295 MM |
6038 | |
6039 | if (data->callchain) | |
6040 | size += data->callchain->nr; | |
6041 | ||
6042 | header->size += size * sizeof(u64); | |
394ee076 PZ |
6043 | } |
6044 | ||
3a43ce68 | 6045 | if (sample_type & PERF_SAMPLE_RAW) { |
7e3f977e DB |
6046 | struct perf_raw_record *raw = data->raw; |
6047 | int size; | |
6048 | ||
6049 | if (raw) { | |
6050 | struct perf_raw_frag *frag = &raw->frag; | |
6051 | u32 sum = 0; | |
6052 | ||
6053 | do { | |
6054 | sum += frag->size; | |
6055 | if (perf_raw_frag_last(frag)) | |
6056 | break; | |
6057 | frag = frag->next; | |
6058 | } while (1); | |
6059 | ||
6060 | size = round_up(sum + sizeof(u32), sizeof(u64)); | |
6061 | raw->size = size - sizeof(u32); | |
6062 | frag->pad = raw->size - sum; | |
6063 | } else { | |
6064 | size = sizeof(u64); | |
6065 | } | |
a044560c | 6066 | |
7e3f977e | 6067 | header->size += size; |
7f453c24 | 6068 | } |
bce38cd5 SE |
6069 | |
6070 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
6071 | int size = sizeof(u64); /* nr */ | |
6072 | if (data->br_stack) { | |
6073 | size += data->br_stack->nr | |
6074 | * sizeof(struct perf_branch_entry); | |
6075 | } | |
6076 | header->size += size; | |
6077 | } | |
4018994f | 6078 | |
2565711f | 6079 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
6080 | perf_sample_regs_user(&data->regs_user, regs, |
6081 | &data->regs_user_copy); | |
2565711f | 6082 | |
4018994f JO |
6083 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
6084 | /* regs dump ABI info */ | |
6085 | int size = sizeof(u64); | |
6086 | ||
4018994f JO |
6087 | if (data->regs_user.regs) { |
6088 | u64 mask = event->attr.sample_regs_user; | |
6089 | size += hweight64(mask) * sizeof(u64); | |
6090 | } | |
6091 | ||
6092 | header->size += size; | |
6093 | } | |
c5ebcedb JO |
6094 | |
6095 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
6096 | /* | |
6097 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
6098 | * processed as the last one or have additional check added | |
6099 | * in case new sample type is added, because we could eat | |
6100 | * up the rest of the sample size. | |
6101 | */ | |
c5ebcedb JO |
6102 | u16 stack_size = event->attr.sample_stack_user; |
6103 | u16 size = sizeof(u64); | |
6104 | ||
c5ebcedb | 6105 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 6106 | data->regs_user.regs); |
c5ebcedb JO |
6107 | |
6108 | /* | |
6109 | * If there is something to dump, add space for the dump | |
6110 | * itself and for the field that tells the dynamic size, | |
6111 | * which is how many have been actually dumped. | |
6112 | */ | |
6113 | if (stack_size) | |
6114 | size += sizeof(u64) + stack_size; | |
6115 | ||
6116 | data->stack_user_size = stack_size; | |
6117 | header->size += size; | |
6118 | } | |
60e2364e SE |
6119 | |
6120 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
6121 | /* regs dump ABI info */ | |
6122 | int size = sizeof(u64); | |
6123 | ||
6124 | perf_sample_regs_intr(&data->regs_intr, regs); | |
6125 | ||
6126 | if (data->regs_intr.regs) { | |
6127 | u64 mask = event->attr.sample_regs_intr; | |
6128 | ||
6129 | size += hweight64(mask) * sizeof(u64); | |
6130 | } | |
6131 | ||
6132 | header->size += size; | |
6133 | } | |
5622f295 | 6134 | } |
7f453c24 | 6135 | |
9ecda41a WN |
6136 | static void __always_inline |
6137 | __perf_event_output(struct perf_event *event, | |
6138 | struct perf_sample_data *data, | |
6139 | struct pt_regs *regs, | |
6140 | int (*output_begin)(struct perf_output_handle *, | |
6141 | struct perf_event *, | |
6142 | unsigned int)) | |
5622f295 MM |
6143 | { |
6144 | struct perf_output_handle handle; | |
6145 | struct perf_event_header header; | |
689802b2 | 6146 | |
927c7a9e FW |
6147 | /* protect the callchain buffers */ |
6148 | rcu_read_lock(); | |
6149 | ||
cdd6c482 | 6150 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 6151 | |
9ecda41a | 6152 | if (output_begin(&handle, event, header.size)) |
927c7a9e | 6153 | goto exit; |
0322cd6e | 6154 | |
cdd6c482 | 6155 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 6156 | |
8a057d84 | 6157 | perf_output_end(&handle); |
927c7a9e FW |
6158 | |
6159 | exit: | |
6160 | rcu_read_unlock(); | |
0322cd6e PZ |
6161 | } |
6162 | ||
9ecda41a WN |
6163 | void |
6164 | perf_event_output_forward(struct perf_event *event, | |
6165 | struct perf_sample_data *data, | |
6166 | struct pt_regs *regs) | |
6167 | { | |
6168 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
6169 | } | |
6170 | ||
6171 | void | |
6172 | perf_event_output_backward(struct perf_event *event, | |
6173 | struct perf_sample_data *data, | |
6174 | struct pt_regs *regs) | |
6175 | { | |
6176 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
6177 | } | |
6178 | ||
6179 | void | |
6180 | perf_event_output(struct perf_event *event, | |
6181 | struct perf_sample_data *data, | |
6182 | struct pt_regs *regs) | |
6183 | { | |
6184 | __perf_event_output(event, data, regs, perf_output_begin); | |
6185 | } | |
6186 | ||
38b200d6 | 6187 | /* |
cdd6c482 | 6188 | * read event_id |
38b200d6 PZ |
6189 | */ |
6190 | ||
6191 | struct perf_read_event { | |
6192 | struct perf_event_header header; | |
6193 | ||
6194 | u32 pid; | |
6195 | u32 tid; | |
38b200d6 PZ |
6196 | }; |
6197 | ||
6198 | static void | |
cdd6c482 | 6199 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
6200 | struct task_struct *task) |
6201 | { | |
6202 | struct perf_output_handle handle; | |
c980d109 | 6203 | struct perf_sample_data sample; |
dfc65094 | 6204 | struct perf_read_event read_event = { |
38b200d6 | 6205 | .header = { |
cdd6c482 | 6206 | .type = PERF_RECORD_READ, |
38b200d6 | 6207 | .misc = 0, |
c320c7b7 | 6208 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 6209 | }, |
cdd6c482 IM |
6210 | .pid = perf_event_pid(event, task), |
6211 | .tid = perf_event_tid(event, task), | |
38b200d6 | 6212 | }; |
3dab77fb | 6213 | int ret; |
38b200d6 | 6214 | |
c980d109 | 6215 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 6216 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
6217 | if (ret) |
6218 | return; | |
6219 | ||
dfc65094 | 6220 | perf_output_put(&handle, read_event); |
cdd6c482 | 6221 | perf_output_read(&handle, event); |
c980d109 | 6222 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 6223 | |
38b200d6 PZ |
6224 | perf_output_end(&handle); |
6225 | } | |
6226 | ||
aab5b71e | 6227 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
6228 | |
6229 | static void | |
aab5b71e PZ |
6230 | perf_iterate_ctx(struct perf_event_context *ctx, |
6231 | perf_iterate_f output, | |
b73e4fef | 6232 | void *data, bool all) |
52d857a8 JO |
6233 | { |
6234 | struct perf_event *event; | |
6235 | ||
6236 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
6237 | if (!all) { |
6238 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
6239 | continue; | |
6240 | if (!event_filter_match(event)) | |
6241 | continue; | |
6242 | } | |
6243 | ||
67516844 | 6244 | output(event, data); |
52d857a8 JO |
6245 | } |
6246 | } | |
6247 | ||
aab5b71e | 6248 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
6249 | { |
6250 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
6251 | struct perf_event *event; | |
6252 | ||
6253 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
6254 | /* |
6255 | * Skip events that are not fully formed yet; ensure that | |
6256 | * if we observe event->ctx, both event and ctx will be | |
6257 | * complete enough. See perf_install_in_context(). | |
6258 | */ | |
6259 | if (!smp_load_acquire(&event->ctx)) | |
6260 | continue; | |
6261 | ||
f2fb6bef KL |
6262 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
6263 | continue; | |
6264 | if (!event_filter_match(event)) | |
6265 | continue; | |
6266 | output(event, data); | |
6267 | } | |
6268 | } | |
6269 | ||
aab5b71e PZ |
6270 | /* |
6271 | * Iterate all events that need to receive side-band events. | |
6272 | * | |
6273 | * For new callers; ensure that account_pmu_sb_event() includes | |
6274 | * your event, otherwise it might not get delivered. | |
6275 | */ | |
52d857a8 | 6276 | static void |
aab5b71e | 6277 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
6278 | struct perf_event_context *task_ctx) |
6279 | { | |
52d857a8 | 6280 | struct perf_event_context *ctx; |
52d857a8 JO |
6281 | int ctxn; |
6282 | ||
aab5b71e PZ |
6283 | rcu_read_lock(); |
6284 | preempt_disable(); | |
6285 | ||
4e93ad60 | 6286 | /* |
aab5b71e PZ |
6287 | * If we have task_ctx != NULL we only notify the task context itself. |
6288 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
6289 | * context. |
6290 | */ | |
6291 | if (task_ctx) { | |
aab5b71e PZ |
6292 | perf_iterate_ctx(task_ctx, output, data, false); |
6293 | goto done; | |
4e93ad60 JO |
6294 | } |
6295 | ||
aab5b71e | 6296 | perf_iterate_sb_cpu(output, data); |
f2fb6bef KL |
6297 | |
6298 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
6299 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
6300 | if (ctx) | |
aab5b71e | 6301 | perf_iterate_ctx(ctx, output, data, false); |
52d857a8 | 6302 | } |
aab5b71e | 6303 | done: |
f2fb6bef | 6304 | preempt_enable(); |
52d857a8 | 6305 | rcu_read_unlock(); |
95ff4ca2 AS |
6306 | } |
6307 | ||
375637bc AS |
6308 | /* |
6309 | * Clear all file-based filters at exec, they'll have to be | |
6310 | * re-instated when/if these objects are mmapped again. | |
6311 | */ | |
6312 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
6313 | { | |
6314 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6315 | struct perf_addr_filter *filter; | |
6316 | unsigned int restart = 0, count = 0; | |
6317 | unsigned long flags; | |
6318 | ||
6319 | if (!has_addr_filter(event)) | |
6320 | return; | |
6321 | ||
6322 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
6323 | list_for_each_entry(filter, &ifh->list, entry) { | |
6324 | if (filter->inode) { | |
6325 | event->addr_filters_offs[count] = 0; | |
6326 | restart++; | |
6327 | } | |
6328 | ||
6329 | count++; | |
6330 | } | |
6331 | ||
6332 | if (restart) | |
6333 | event->addr_filters_gen++; | |
6334 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
6335 | ||
6336 | if (restart) | |
767ae086 | 6337 | perf_event_stop(event, 1); |
375637bc AS |
6338 | } |
6339 | ||
6340 | void perf_event_exec(void) | |
6341 | { | |
6342 | struct perf_event_context *ctx; | |
6343 | int ctxn; | |
6344 | ||
6345 | rcu_read_lock(); | |
6346 | for_each_task_context_nr(ctxn) { | |
6347 | ctx = current->perf_event_ctxp[ctxn]; | |
6348 | if (!ctx) | |
6349 | continue; | |
6350 | ||
6351 | perf_event_enable_on_exec(ctxn); | |
6352 | ||
aab5b71e | 6353 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
375637bc AS |
6354 | true); |
6355 | } | |
6356 | rcu_read_unlock(); | |
6357 | } | |
6358 | ||
95ff4ca2 AS |
6359 | struct remote_output { |
6360 | struct ring_buffer *rb; | |
6361 | int err; | |
6362 | }; | |
6363 | ||
6364 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
6365 | { | |
6366 | struct perf_event *parent = event->parent; | |
6367 | struct remote_output *ro = data; | |
6368 | struct ring_buffer *rb = ro->rb; | |
375637bc AS |
6369 | struct stop_event_data sd = { |
6370 | .event = event, | |
6371 | }; | |
95ff4ca2 AS |
6372 | |
6373 | if (!has_aux(event)) | |
6374 | return; | |
6375 | ||
6376 | if (!parent) | |
6377 | parent = event; | |
6378 | ||
6379 | /* | |
6380 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
6381 | * ring-buffer, but it will be the child that's actually using it. |
6382 | * | |
6383 | * We are using event::rb to determine if the event should be stopped, | |
6384 | * however this may race with ring_buffer_attach() (through set_output), | |
6385 | * which will make us skip the event that actually needs to be stopped. | |
6386 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
6387 | * its rb pointer. | |
95ff4ca2 AS |
6388 | */ |
6389 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 6390 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
6391 | } |
6392 | ||
6393 | static int __perf_pmu_output_stop(void *info) | |
6394 | { | |
6395 | struct perf_event *event = info; | |
6396 | struct pmu *pmu = event->pmu; | |
8b6a3fe8 | 6397 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95ff4ca2 AS |
6398 | struct remote_output ro = { |
6399 | .rb = event->rb, | |
6400 | }; | |
6401 | ||
6402 | rcu_read_lock(); | |
aab5b71e | 6403 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 6404 | if (cpuctx->task_ctx) |
aab5b71e | 6405 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 6406 | &ro, false); |
95ff4ca2 AS |
6407 | rcu_read_unlock(); |
6408 | ||
6409 | return ro.err; | |
6410 | } | |
6411 | ||
6412 | static void perf_pmu_output_stop(struct perf_event *event) | |
6413 | { | |
6414 | struct perf_event *iter; | |
6415 | int err, cpu; | |
6416 | ||
6417 | restart: | |
6418 | rcu_read_lock(); | |
6419 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
6420 | /* | |
6421 | * For per-CPU events, we need to make sure that neither they | |
6422 | * nor their children are running; for cpu==-1 events it's | |
6423 | * sufficient to stop the event itself if it's active, since | |
6424 | * it can't have children. | |
6425 | */ | |
6426 | cpu = iter->cpu; | |
6427 | if (cpu == -1) | |
6428 | cpu = READ_ONCE(iter->oncpu); | |
6429 | ||
6430 | if (cpu == -1) | |
6431 | continue; | |
6432 | ||
6433 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
6434 | if (err == -EAGAIN) { | |
6435 | rcu_read_unlock(); | |
6436 | goto restart; | |
6437 | } | |
6438 | } | |
6439 | rcu_read_unlock(); | |
52d857a8 JO |
6440 | } |
6441 | ||
60313ebe | 6442 | /* |
9f498cc5 PZ |
6443 | * task tracking -- fork/exit |
6444 | * | |
13d7a241 | 6445 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
6446 | */ |
6447 | ||
9f498cc5 | 6448 | struct perf_task_event { |
3a80b4a3 | 6449 | struct task_struct *task; |
cdd6c482 | 6450 | struct perf_event_context *task_ctx; |
60313ebe PZ |
6451 | |
6452 | struct { | |
6453 | struct perf_event_header header; | |
6454 | ||
6455 | u32 pid; | |
6456 | u32 ppid; | |
9f498cc5 PZ |
6457 | u32 tid; |
6458 | u32 ptid; | |
393b2ad8 | 6459 | u64 time; |
cdd6c482 | 6460 | } event_id; |
60313ebe PZ |
6461 | }; |
6462 | ||
67516844 JO |
6463 | static int perf_event_task_match(struct perf_event *event) |
6464 | { | |
13d7a241 SE |
6465 | return event->attr.comm || event->attr.mmap || |
6466 | event->attr.mmap2 || event->attr.mmap_data || | |
6467 | event->attr.task; | |
67516844 JO |
6468 | } |
6469 | ||
cdd6c482 | 6470 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 6471 | void *data) |
60313ebe | 6472 | { |
52d857a8 | 6473 | struct perf_task_event *task_event = data; |
60313ebe | 6474 | struct perf_output_handle handle; |
c980d109 | 6475 | struct perf_sample_data sample; |
9f498cc5 | 6476 | struct task_struct *task = task_event->task; |
c980d109 | 6477 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 6478 | |
67516844 JO |
6479 | if (!perf_event_task_match(event)) |
6480 | return; | |
6481 | ||
c980d109 | 6482 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 6483 | |
c980d109 | 6484 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 6485 | task_event->event_id.header.size); |
ef60777c | 6486 | if (ret) |
c980d109 | 6487 | goto out; |
60313ebe | 6488 | |
cdd6c482 IM |
6489 | task_event->event_id.pid = perf_event_pid(event, task); |
6490 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 6491 | |
cdd6c482 IM |
6492 | task_event->event_id.tid = perf_event_tid(event, task); |
6493 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 6494 | |
34f43927 PZ |
6495 | task_event->event_id.time = perf_event_clock(event); |
6496 | ||
cdd6c482 | 6497 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 6498 | |
c980d109 ACM |
6499 | perf_event__output_id_sample(event, &handle, &sample); |
6500 | ||
60313ebe | 6501 | perf_output_end(&handle); |
c980d109 ACM |
6502 | out: |
6503 | task_event->event_id.header.size = size; | |
60313ebe PZ |
6504 | } |
6505 | ||
cdd6c482 IM |
6506 | static void perf_event_task(struct task_struct *task, |
6507 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 6508 | int new) |
60313ebe | 6509 | { |
9f498cc5 | 6510 | struct perf_task_event task_event; |
60313ebe | 6511 | |
cdd6c482 IM |
6512 | if (!atomic_read(&nr_comm_events) && |
6513 | !atomic_read(&nr_mmap_events) && | |
6514 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
6515 | return; |
6516 | ||
9f498cc5 | 6517 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
6518 | .task = task, |
6519 | .task_ctx = task_ctx, | |
cdd6c482 | 6520 | .event_id = { |
60313ebe | 6521 | .header = { |
cdd6c482 | 6522 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 6523 | .misc = 0, |
cdd6c482 | 6524 | .size = sizeof(task_event.event_id), |
60313ebe | 6525 | }, |
573402db PZ |
6526 | /* .pid */ |
6527 | /* .ppid */ | |
9f498cc5 PZ |
6528 | /* .tid */ |
6529 | /* .ptid */ | |
34f43927 | 6530 | /* .time */ |
60313ebe PZ |
6531 | }, |
6532 | }; | |
6533 | ||
aab5b71e | 6534 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
6535 | &task_event, |
6536 | task_ctx); | |
9f498cc5 PZ |
6537 | } |
6538 | ||
cdd6c482 | 6539 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 6540 | { |
cdd6c482 | 6541 | perf_event_task(task, NULL, 1); |
e4222673 | 6542 | perf_event_namespaces(task); |
60313ebe PZ |
6543 | } |
6544 | ||
8d1b2d93 PZ |
6545 | /* |
6546 | * comm tracking | |
6547 | */ | |
6548 | ||
6549 | struct perf_comm_event { | |
22a4f650 IM |
6550 | struct task_struct *task; |
6551 | char *comm; | |
8d1b2d93 PZ |
6552 | int comm_size; |
6553 | ||
6554 | struct { | |
6555 | struct perf_event_header header; | |
6556 | ||
6557 | u32 pid; | |
6558 | u32 tid; | |
cdd6c482 | 6559 | } event_id; |
8d1b2d93 PZ |
6560 | }; |
6561 | ||
67516844 JO |
6562 | static int perf_event_comm_match(struct perf_event *event) |
6563 | { | |
6564 | return event->attr.comm; | |
6565 | } | |
6566 | ||
cdd6c482 | 6567 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6568 | void *data) |
8d1b2d93 | 6569 | { |
52d857a8 | 6570 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6571 | struct perf_output_handle handle; |
c980d109 | 6572 | struct perf_sample_data sample; |
cdd6c482 | 6573 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6574 | int ret; |
6575 | ||
67516844 JO |
6576 | if (!perf_event_comm_match(event)) |
6577 | return; | |
6578 | ||
c980d109 ACM |
6579 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6580 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6581 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6582 | |
6583 | if (ret) | |
c980d109 | 6584 | goto out; |
8d1b2d93 | 6585 | |
cdd6c482 IM |
6586 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6587 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6588 | |
cdd6c482 | 6589 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6590 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6591 | comm_event->comm_size); |
c980d109 ACM |
6592 | |
6593 | perf_event__output_id_sample(event, &handle, &sample); | |
6594 | ||
8d1b2d93 | 6595 | perf_output_end(&handle); |
c980d109 ACM |
6596 | out: |
6597 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6598 | } |
6599 | ||
cdd6c482 | 6600 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6601 | { |
413ee3b4 | 6602 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6603 | unsigned int size; |
8d1b2d93 | 6604 | |
413ee3b4 | 6605 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6606 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6607 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6608 | |
6609 | comm_event->comm = comm; | |
6610 | comm_event->comm_size = size; | |
6611 | ||
cdd6c482 | 6612 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6613 | |
aab5b71e | 6614 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
6615 | comm_event, |
6616 | NULL); | |
8d1b2d93 PZ |
6617 | } |
6618 | ||
82b89778 | 6619 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6620 | { |
9ee318a7 PZ |
6621 | struct perf_comm_event comm_event; |
6622 | ||
cdd6c482 | 6623 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6624 | return; |
a63eaf34 | 6625 | |
9ee318a7 | 6626 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6627 | .task = task, |
573402db PZ |
6628 | /* .comm */ |
6629 | /* .comm_size */ | |
cdd6c482 | 6630 | .event_id = { |
573402db | 6631 | .header = { |
cdd6c482 | 6632 | .type = PERF_RECORD_COMM, |
82b89778 | 6633 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6634 | /* .size */ |
6635 | }, | |
6636 | /* .pid */ | |
6637 | /* .tid */ | |
8d1b2d93 PZ |
6638 | }, |
6639 | }; | |
6640 | ||
cdd6c482 | 6641 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6642 | } |
6643 | ||
e4222673 HB |
6644 | /* |
6645 | * namespaces tracking | |
6646 | */ | |
6647 | ||
6648 | struct perf_namespaces_event { | |
6649 | struct task_struct *task; | |
6650 | ||
6651 | struct { | |
6652 | struct perf_event_header header; | |
6653 | ||
6654 | u32 pid; | |
6655 | u32 tid; | |
6656 | u64 nr_namespaces; | |
6657 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
6658 | } event_id; | |
6659 | }; | |
6660 | ||
6661 | static int perf_event_namespaces_match(struct perf_event *event) | |
6662 | { | |
6663 | return event->attr.namespaces; | |
6664 | } | |
6665 | ||
6666 | static void perf_event_namespaces_output(struct perf_event *event, | |
6667 | void *data) | |
6668 | { | |
6669 | struct perf_namespaces_event *namespaces_event = data; | |
6670 | struct perf_output_handle handle; | |
6671 | struct perf_sample_data sample; | |
6672 | int ret; | |
6673 | ||
6674 | if (!perf_event_namespaces_match(event)) | |
6675 | return; | |
6676 | ||
6677 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
6678 | &sample, event); | |
6679 | ret = perf_output_begin(&handle, event, | |
6680 | namespaces_event->event_id.header.size); | |
6681 | if (ret) | |
6682 | return; | |
6683 | ||
6684 | namespaces_event->event_id.pid = perf_event_pid(event, | |
6685 | namespaces_event->task); | |
6686 | namespaces_event->event_id.tid = perf_event_tid(event, | |
6687 | namespaces_event->task); | |
6688 | ||
6689 | perf_output_put(&handle, namespaces_event->event_id); | |
6690 | ||
6691 | perf_event__output_id_sample(event, &handle, &sample); | |
6692 | ||
6693 | perf_output_end(&handle); | |
6694 | } | |
6695 | ||
6696 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
6697 | struct task_struct *task, | |
6698 | const struct proc_ns_operations *ns_ops) | |
6699 | { | |
6700 | struct path ns_path; | |
6701 | struct inode *ns_inode; | |
6702 | void *error; | |
6703 | ||
6704 | error = ns_get_path(&ns_path, task, ns_ops); | |
6705 | if (!error) { | |
6706 | ns_inode = ns_path.dentry->d_inode; | |
6707 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
6708 | ns_link_info->ino = ns_inode->i_ino; | |
6709 | } | |
6710 | } | |
6711 | ||
6712 | void perf_event_namespaces(struct task_struct *task) | |
6713 | { | |
6714 | struct perf_namespaces_event namespaces_event; | |
6715 | struct perf_ns_link_info *ns_link_info; | |
6716 | ||
6717 | if (!atomic_read(&nr_namespaces_events)) | |
6718 | return; | |
6719 | ||
6720 | namespaces_event = (struct perf_namespaces_event){ | |
6721 | .task = task, | |
6722 | .event_id = { | |
6723 | .header = { | |
6724 | .type = PERF_RECORD_NAMESPACES, | |
6725 | .misc = 0, | |
6726 | .size = sizeof(namespaces_event.event_id), | |
6727 | }, | |
6728 | /* .pid */ | |
6729 | /* .tid */ | |
6730 | .nr_namespaces = NR_NAMESPACES, | |
6731 | /* .link_info[NR_NAMESPACES] */ | |
6732 | }, | |
6733 | }; | |
6734 | ||
6735 | ns_link_info = namespaces_event.event_id.link_info; | |
6736 | ||
6737 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
6738 | task, &mntns_operations); | |
6739 | ||
6740 | #ifdef CONFIG_USER_NS | |
6741 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
6742 | task, &userns_operations); | |
6743 | #endif | |
6744 | #ifdef CONFIG_NET_NS | |
6745 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
6746 | task, &netns_operations); | |
6747 | #endif | |
6748 | #ifdef CONFIG_UTS_NS | |
6749 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
6750 | task, &utsns_operations); | |
6751 | #endif | |
6752 | #ifdef CONFIG_IPC_NS | |
6753 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
6754 | task, &ipcns_operations); | |
6755 | #endif | |
6756 | #ifdef CONFIG_PID_NS | |
6757 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
6758 | task, &pidns_operations); | |
6759 | #endif | |
6760 | #ifdef CONFIG_CGROUPS | |
6761 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
6762 | task, &cgroupns_operations); | |
6763 | #endif | |
6764 | ||
6765 | perf_iterate_sb(perf_event_namespaces_output, | |
6766 | &namespaces_event, | |
6767 | NULL); | |
6768 | } | |
6769 | ||
0a4a9391 PZ |
6770 | /* |
6771 | * mmap tracking | |
6772 | */ | |
6773 | ||
6774 | struct perf_mmap_event { | |
089dd79d PZ |
6775 | struct vm_area_struct *vma; |
6776 | ||
6777 | const char *file_name; | |
6778 | int file_size; | |
13d7a241 SE |
6779 | int maj, min; |
6780 | u64 ino; | |
6781 | u64 ino_generation; | |
f972eb63 | 6782 | u32 prot, flags; |
0a4a9391 PZ |
6783 | |
6784 | struct { | |
6785 | struct perf_event_header header; | |
6786 | ||
6787 | u32 pid; | |
6788 | u32 tid; | |
6789 | u64 start; | |
6790 | u64 len; | |
6791 | u64 pgoff; | |
cdd6c482 | 6792 | } event_id; |
0a4a9391 PZ |
6793 | }; |
6794 | ||
67516844 JO |
6795 | static int perf_event_mmap_match(struct perf_event *event, |
6796 | void *data) | |
6797 | { | |
6798 | struct perf_mmap_event *mmap_event = data; | |
6799 | struct vm_area_struct *vma = mmap_event->vma; | |
6800 | int executable = vma->vm_flags & VM_EXEC; | |
6801 | ||
6802 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 6803 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
6804 | } |
6805 | ||
cdd6c482 | 6806 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 6807 | void *data) |
0a4a9391 | 6808 | { |
52d857a8 | 6809 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 6810 | struct perf_output_handle handle; |
c980d109 | 6811 | struct perf_sample_data sample; |
cdd6c482 | 6812 | int size = mmap_event->event_id.header.size; |
c980d109 | 6813 | int ret; |
0a4a9391 | 6814 | |
67516844 JO |
6815 | if (!perf_event_mmap_match(event, data)) |
6816 | return; | |
6817 | ||
13d7a241 SE |
6818 | if (event->attr.mmap2) { |
6819 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
6820 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
6821 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
6822 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 6823 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
6824 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
6825 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
6826 | } |
6827 | ||
c980d109 ACM |
6828 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
6829 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6830 | mmap_event->event_id.header.size); |
0a4a9391 | 6831 | if (ret) |
c980d109 | 6832 | goto out; |
0a4a9391 | 6833 | |
cdd6c482 IM |
6834 | mmap_event->event_id.pid = perf_event_pid(event, current); |
6835 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 6836 | |
cdd6c482 | 6837 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
6838 | |
6839 | if (event->attr.mmap2) { | |
6840 | perf_output_put(&handle, mmap_event->maj); | |
6841 | perf_output_put(&handle, mmap_event->min); | |
6842 | perf_output_put(&handle, mmap_event->ino); | |
6843 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
6844 | perf_output_put(&handle, mmap_event->prot); |
6845 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
6846 | } |
6847 | ||
76369139 | 6848 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 6849 | mmap_event->file_size); |
c980d109 ACM |
6850 | |
6851 | perf_event__output_id_sample(event, &handle, &sample); | |
6852 | ||
78d613eb | 6853 | perf_output_end(&handle); |
c980d109 ACM |
6854 | out: |
6855 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
6856 | } |
6857 | ||
cdd6c482 | 6858 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 6859 | { |
089dd79d PZ |
6860 | struct vm_area_struct *vma = mmap_event->vma; |
6861 | struct file *file = vma->vm_file; | |
13d7a241 SE |
6862 | int maj = 0, min = 0; |
6863 | u64 ino = 0, gen = 0; | |
f972eb63 | 6864 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
6865 | unsigned int size; |
6866 | char tmp[16]; | |
6867 | char *buf = NULL; | |
2c42cfbf | 6868 | char *name; |
413ee3b4 | 6869 | |
0b3589be PZ |
6870 | if (vma->vm_flags & VM_READ) |
6871 | prot |= PROT_READ; | |
6872 | if (vma->vm_flags & VM_WRITE) | |
6873 | prot |= PROT_WRITE; | |
6874 | if (vma->vm_flags & VM_EXEC) | |
6875 | prot |= PROT_EXEC; | |
6876 | ||
6877 | if (vma->vm_flags & VM_MAYSHARE) | |
6878 | flags = MAP_SHARED; | |
6879 | else | |
6880 | flags = MAP_PRIVATE; | |
6881 | ||
6882 | if (vma->vm_flags & VM_DENYWRITE) | |
6883 | flags |= MAP_DENYWRITE; | |
6884 | if (vma->vm_flags & VM_MAYEXEC) | |
6885 | flags |= MAP_EXECUTABLE; | |
6886 | if (vma->vm_flags & VM_LOCKED) | |
6887 | flags |= MAP_LOCKED; | |
6888 | if (vma->vm_flags & VM_HUGETLB) | |
6889 | flags |= MAP_HUGETLB; | |
6890 | ||
0a4a9391 | 6891 | if (file) { |
13d7a241 SE |
6892 | struct inode *inode; |
6893 | dev_t dev; | |
3ea2f2b9 | 6894 | |
2c42cfbf | 6895 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 6896 | if (!buf) { |
c7e548b4 ON |
6897 | name = "//enomem"; |
6898 | goto cpy_name; | |
0a4a9391 | 6899 | } |
413ee3b4 | 6900 | /* |
3ea2f2b9 | 6901 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
6902 | * need to add enough zero bytes after the string to handle |
6903 | * the 64bit alignment we do later. | |
6904 | */ | |
9bf39ab2 | 6905 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 6906 | if (IS_ERR(name)) { |
c7e548b4 ON |
6907 | name = "//toolong"; |
6908 | goto cpy_name; | |
0a4a9391 | 6909 | } |
13d7a241 SE |
6910 | inode = file_inode(vma->vm_file); |
6911 | dev = inode->i_sb->s_dev; | |
6912 | ino = inode->i_ino; | |
6913 | gen = inode->i_generation; | |
6914 | maj = MAJOR(dev); | |
6915 | min = MINOR(dev); | |
f972eb63 | 6916 | |
c7e548b4 | 6917 | goto got_name; |
0a4a9391 | 6918 | } else { |
fbe26abe JO |
6919 | if (vma->vm_ops && vma->vm_ops->name) { |
6920 | name = (char *) vma->vm_ops->name(vma); | |
6921 | if (name) | |
6922 | goto cpy_name; | |
6923 | } | |
6924 | ||
2c42cfbf | 6925 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
6926 | if (name) |
6927 | goto cpy_name; | |
089dd79d | 6928 | |
32c5fb7e | 6929 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 6930 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
6931 | name = "[heap]"; |
6932 | goto cpy_name; | |
32c5fb7e ON |
6933 | } |
6934 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 6935 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
6936 | name = "[stack]"; |
6937 | goto cpy_name; | |
089dd79d PZ |
6938 | } |
6939 | ||
c7e548b4 ON |
6940 | name = "//anon"; |
6941 | goto cpy_name; | |
0a4a9391 PZ |
6942 | } |
6943 | ||
c7e548b4 ON |
6944 | cpy_name: |
6945 | strlcpy(tmp, name, sizeof(tmp)); | |
6946 | name = tmp; | |
0a4a9391 | 6947 | got_name: |
2c42cfbf PZ |
6948 | /* |
6949 | * Since our buffer works in 8 byte units we need to align our string | |
6950 | * size to a multiple of 8. However, we must guarantee the tail end is | |
6951 | * zero'd out to avoid leaking random bits to userspace. | |
6952 | */ | |
6953 | size = strlen(name)+1; | |
6954 | while (!IS_ALIGNED(size, sizeof(u64))) | |
6955 | name[size++] = '\0'; | |
0a4a9391 PZ |
6956 | |
6957 | mmap_event->file_name = name; | |
6958 | mmap_event->file_size = size; | |
13d7a241 SE |
6959 | mmap_event->maj = maj; |
6960 | mmap_event->min = min; | |
6961 | mmap_event->ino = ino; | |
6962 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
6963 | mmap_event->prot = prot; |
6964 | mmap_event->flags = flags; | |
0a4a9391 | 6965 | |
2fe85427 SE |
6966 | if (!(vma->vm_flags & VM_EXEC)) |
6967 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
6968 | ||
cdd6c482 | 6969 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 6970 | |
aab5b71e | 6971 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
6972 | mmap_event, |
6973 | NULL); | |
665c2142 | 6974 | |
0a4a9391 PZ |
6975 | kfree(buf); |
6976 | } | |
6977 | ||
375637bc AS |
6978 | /* |
6979 | * Check whether inode and address range match filter criteria. | |
6980 | */ | |
6981 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
6982 | struct file *file, unsigned long offset, | |
6983 | unsigned long size) | |
6984 | { | |
45063097 | 6985 | if (filter->inode != file_inode(file)) |
375637bc AS |
6986 | return false; |
6987 | ||
6988 | if (filter->offset > offset + size) | |
6989 | return false; | |
6990 | ||
6991 | if (filter->offset + filter->size < offset) | |
6992 | return false; | |
6993 | ||
6994 | return true; | |
6995 | } | |
6996 | ||
6997 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) | |
6998 | { | |
6999 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
7000 | struct vm_area_struct *vma = data; | |
7001 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; | |
7002 | struct file *file = vma->vm_file; | |
7003 | struct perf_addr_filter *filter; | |
7004 | unsigned int restart = 0, count = 0; | |
7005 | ||
7006 | if (!has_addr_filter(event)) | |
7007 | return; | |
7008 | ||
7009 | if (!file) | |
7010 | return; | |
7011 | ||
7012 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7013 | list_for_each_entry(filter, &ifh->list, entry) { | |
7014 | if (perf_addr_filter_match(filter, file, off, | |
7015 | vma->vm_end - vma->vm_start)) { | |
7016 | event->addr_filters_offs[count] = vma->vm_start; | |
7017 | restart++; | |
7018 | } | |
7019 | ||
7020 | count++; | |
7021 | } | |
7022 | ||
7023 | if (restart) | |
7024 | event->addr_filters_gen++; | |
7025 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7026 | ||
7027 | if (restart) | |
767ae086 | 7028 | perf_event_stop(event, 1); |
375637bc AS |
7029 | } |
7030 | ||
7031 | /* | |
7032 | * Adjust all task's events' filters to the new vma | |
7033 | */ | |
7034 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
7035 | { | |
7036 | struct perf_event_context *ctx; | |
7037 | int ctxn; | |
7038 | ||
12b40a23 MP |
7039 | /* |
7040 | * Data tracing isn't supported yet and as such there is no need | |
7041 | * to keep track of anything that isn't related to executable code: | |
7042 | */ | |
7043 | if (!(vma->vm_flags & VM_EXEC)) | |
7044 | return; | |
7045 | ||
375637bc AS |
7046 | rcu_read_lock(); |
7047 | for_each_task_context_nr(ctxn) { | |
7048 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
7049 | if (!ctx) | |
7050 | continue; | |
7051 | ||
aab5b71e | 7052 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
7053 | } |
7054 | rcu_read_unlock(); | |
7055 | } | |
7056 | ||
3af9e859 | 7057 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 7058 | { |
9ee318a7 PZ |
7059 | struct perf_mmap_event mmap_event; |
7060 | ||
cdd6c482 | 7061 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
7062 | return; |
7063 | ||
7064 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 7065 | .vma = vma, |
573402db PZ |
7066 | /* .file_name */ |
7067 | /* .file_size */ | |
cdd6c482 | 7068 | .event_id = { |
573402db | 7069 | .header = { |
cdd6c482 | 7070 | .type = PERF_RECORD_MMAP, |
39447b38 | 7071 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
7072 | /* .size */ |
7073 | }, | |
7074 | /* .pid */ | |
7075 | /* .tid */ | |
089dd79d PZ |
7076 | .start = vma->vm_start, |
7077 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 7078 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 7079 | }, |
13d7a241 SE |
7080 | /* .maj (attr_mmap2 only) */ |
7081 | /* .min (attr_mmap2 only) */ | |
7082 | /* .ino (attr_mmap2 only) */ | |
7083 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
7084 | /* .prot (attr_mmap2 only) */ |
7085 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
7086 | }; |
7087 | ||
375637bc | 7088 | perf_addr_filters_adjust(vma); |
cdd6c482 | 7089 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
7090 | } |
7091 | ||
68db7e98 AS |
7092 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
7093 | unsigned long size, u64 flags) | |
7094 | { | |
7095 | struct perf_output_handle handle; | |
7096 | struct perf_sample_data sample; | |
7097 | struct perf_aux_event { | |
7098 | struct perf_event_header header; | |
7099 | u64 offset; | |
7100 | u64 size; | |
7101 | u64 flags; | |
7102 | } rec = { | |
7103 | .header = { | |
7104 | .type = PERF_RECORD_AUX, | |
7105 | .misc = 0, | |
7106 | .size = sizeof(rec), | |
7107 | }, | |
7108 | .offset = head, | |
7109 | .size = size, | |
7110 | .flags = flags, | |
7111 | }; | |
7112 | int ret; | |
7113 | ||
7114 | perf_event_header__init_id(&rec.header, &sample, event); | |
7115 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7116 | ||
7117 | if (ret) | |
7118 | return; | |
7119 | ||
7120 | perf_output_put(&handle, rec); | |
7121 | perf_event__output_id_sample(event, &handle, &sample); | |
7122 | ||
7123 | perf_output_end(&handle); | |
7124 | } | |
7125 | ||
f38b0dbb KL |
7126 | /* |
7127 | * Lost/dropped samples logging | |
7128 | */ | |
7129 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
7130 | { | |
7131 | struct perf_output_handle handle; | |
7132 | struct perf_sample_data sample; | |
7133 | int ret; | |
7134 | ||
7135 | struct { | |
7136 | struct perf_event_header header; | |
7137 | u64 lost; | |
7138 | } lost_samples_event = { | |
7139 | .header = { | |
7140 | .type = PERF_RECORD_LOST_SAMPLES, | |
7141 | .misc = 0, | |
7142 | .size = sizeof(lost_samples_event), | |
7143 | }, | |
7144 | .lost = lost, | |
7145 | }; | |
7146 | ||
7147 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
7148 | ||
7149 | ret = perf_output_begin(&handle, event, | |
7150 | lost_samples_event.header.size); | |
7151 | if (ret) | |
7152 | return; | |
7153 | ||
7154 | perf_output_put(&handle, lost_samples_event); | |
7155 | perf_event__output_id_sample(event, &handle, &sample); | |
7156 | perf_output_end(&handle); | |
7157 | } | |
7158 | ||
45ac1403 AH |
7159 | /* |
7160 | * context_switch tracking | |
7161 | */ | |
7162 | ||
7163 | struct perf_switch_event { | |
7164 | struct task_struct *task; | |
7165 | struct task_struct *next_prev; | |
7166 | ||
7167 | struct { | |
7168 | struct perf_event_header header; | |
7169 | u32 next_prev_pid; | |
7170 | u32 next_prev_tid; | |
7171 | } event_id; | |
7172 | }; | |
7173 | ||
7174 | static int perf_event_switch_match(struct perf_event *event) | |
7175 | { | |
7176 | return event->attr.context_switch; | |
7177 | } | |
7178 | ||
7179 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
7180 | { | |
7181 | struct perf_switch_event *se = data; | |
7182 | struct perf_output_handle handle; | |
7183 | struct perf_sample_data sample; | |
7184 | int ret; | |
7185 | ||
7186 | if (!perf_event_switch_match(event)) | |
7187 | return; | |
7188 | ||
7189 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
7190 | if (event->ctx->task) { | |
7191 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
7192 | se->event_id.header.size = sizeof(se->event_id.header); | |
7193 | } else { | |
7194 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
7195 | se->event_id.header.size = sizeof(se->event_id); | |
7196 | se->event_id.next_prev_pid = | |
7197 | perf_event_pid(event, se->next_prev); | |
7198 | se->event_id.next_prev_tid = | |
7199 | perf_event_tid(event, se->next_prev); | |
7200 | } | |
7201 | ||
7202 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
7203 | ||
7204 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
7205 | if (ret) | |
7206 | return; | |
7207 | ||
7208 | if (event->ctx->task) | |
7209 | perf_output_put(&handle, se->event_id.header); | |
7210 | else | |
7211 | perf_output_put(&handle, se->event_id); | |
7212 | ||
7213 | perf_event__output_id_sample(event, &handle, &sample); | |
7214 | ||
7215 | perf_output_end(&handle); | |
7216 | } | |
7217 | ||
7218 | static void perf_event_switch(struct task_struct *task, | |
7219 | struct task_struct *next_prev, bool sched_in) | |
7220 | { | |
7221 | struct perf_switch_event switch_event; | |
7222 | ||
7223 | /* N.B. caller checks nr_switch_events != 0 */ | |
7224 | ||
7225 | switch_event = (struct perf_switch_event){ | |
7226 | .task = task, | |
7227 | .next_prev = next_prev, | |
7228 | .event_id = { | |
7229 | .header = { | |
7230 | /* .type */ | |
7231 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
7232 | /* .size */ | |
7233 | }, | |
7234 | /* .next_prev_pid */ | |
7235 | /* .next_prev_tid */ | |
7236 | }, | |
7237 | }; | |
7238 | ||
aab5b71e | 7239 | perf_iterate_sb(perf_event_switch_output, |
45ac1403 AH |
7240 | &switch_event, |
7241 | NULL); | |
7242 | } | |
7243 | ||
a78ac325 PZ |
7244 | /* |
7245 | * IRQ throttle logging | |
7246 | */ | |
7247 | ||
cdd6c482 | 7248 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
7249 | { |
7250 | struct perf_output_handle handle; | |
c980d109 | 7251 | struct perf_sample_data sample; |
a78ac325 PZ |
7252 | int ret; |
7253 | ||
7254 | struct { | |
7255 | struct perf_event_header header; | |
7256 | u64 time; | |
cca3f454 | 7257 | u64 id; |
7f453c24 | 7258 | u64 stream_id; |
a78ac325 PZ |
7259 | } throttle_event = { |
7260 | .header = { | |
cdd6c482 | 7261 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
7262 | .misc = 0, |
7263 | .size = sizeof(throttle_event), | |
7264 | }, | |
34f43927 | 7265 | .time = perf_event_clock(event), |
cdd6c482 IM |
7266 | .id = primary_event_id(event), |
7267 | .stream_id = event->id, | |
a78ac325 PZ |
7268 | }; |
7269 | ||
966ee4d6 | 7270 | if (enable) |
cdd6c482 | 7271 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 7272 | |
c980d109 ACM |
7273 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
7274 | ||
7275 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7276 | throttle_event.header.size); |
a78ac325 PZ |
7277 | if (ret) |
7278 | return; | |
7279 | ||
7280 | perf_output_put(&handle, throttle_event); | |
c980d109 | 7281 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
7282 | perf_output_end(&handle); |
7283 | } | |
7284 | ||
ec0d7729 AS |
7285 | static void perf_log_itrace_start(struct perf_event *event) |
7286 | { | |
7287 | struct perf_output_handle handle; | |
7288 | struct perf_sample_data sample; | |
7289 | struct perf_aux_event { | |
7290 | struct perf_event_header header; | |
7291 | u32 pid; | |
7292 | u32 tid; | |
7293 | } rec; | |
7294 | int ret; | |
7295 | ||
7296 | if (event->parent) | |
7297 | event = event->parent; | |
7298 | ||
7299 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
7300 | event->hw.itrace_started) | |
7301 | return; | |
7302 | ||
ec0d7729 AS |
7303 | rec.header.type = PERF_RECORD_ITRACE_START; |
7304 | rec.header.misc = 0; | |
7305 | rec.header.size = sizeof(rec); | |
7306 | rec.pid = perf_event_pid(event, current); | |
7307 | rec.tid = perf_event_tid(event, current); | |
7308 | ||
7309 | perf_event_header__init_id(&rec.header, &sample, event); | |
7310 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7311 | ||
7312 | if (ret) | |
7313 | return; | |
7314 | ||
7315 | perf_output_put(&handle, rec); | |
7316 | perf_event__output_id_sample(event, &handle, &sample); | |
7317 | ||
7318 | perf_output_end(&handle); | |
7319 | } | |
7320 | ||
475113d9 JO |
7321 | static int |
7322 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 7323 | { |
cdd6c482 | 7324 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 7325 | int ret = 0; |
475113d9 | 7326 | u64 seq; |
96398826 | 7327 | |
e050e3f0 SE |
7328 | seq = __this_cpu_read(perf_throttled_seq); |
7329 | if (seq != hwc->interrupts_seq) { | |
7330 | hwc->interrupts_seq = seq; | |
7331 | hwc->interrupts = 1; | |
7332 | } else { | |
7333 | hwc->interrupts++; | |
7334 | if (unlikely(throttle | |
7335 | && hwc->interrupts >= max_samples_per_tick)) { | |
7336 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 7337 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
7338 | hwc->interrupts = MAX_INTERRUPTS; |
7339 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
7340 | ret = 1; |
7341 | } | |
e050e3f0 | 7342 | } |
60db5e09 | 7343 | |
cdd6c482 | 7344 | if (event->attr.freq) { |
def0a9b2 | 7345 | u64 now = perf_clock(); |
abd50713 | 7346 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 7347 | |
abd50713 | 7348 | hwc->freq_time_stamp = now; |
bd2b5b12 | 7349 | |
abd50713 | 7350 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 7351 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
7352 | } |
7353 | ||
475113d9 JO |
7354 | return ret; |
7355 | } | |
7356 | ||
7357 | int perf_event_account_interrupt(struct perf_event *event) | |
7358 | { | |
7359 | return __perf_event_account_interrupt(event, 1); | |
7360 | } | |
7361 | ||
7362 | /* | |
7363 | * Generic event overflow handling, sampling. | |
7364 | */ | |
7365 | ||
7366 | static int __perf_event_overflow(struct perf_event *event, | |
7367 | int throttle, struct perf_sample_data *data, | |
7368 | struct pt_regs *regs) | |
7369 | { | |
7370 | int events = atomic_read(&event->event_limit); | |
7371 | int ret = 0; | |
7372 | ||
7373 | /* | |
7374 | * Non-sampling counters might still use the PMI to fold short | |
7375 | * hardware counters, ignore those. | |
7376 | */ | |
7377 | if (unlikely(!is_sampling_event(event))) | |
7378 | return 0; | |
7379 | ||
7380 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 7381 | |
2023b359 PZ |
7382 | /* |
7383 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 7384 | * events |
2023b359 PZ |
7385 | */ |
7386 | ||
cdd6c482 IM |
7387 | event->pending_kill = POLL_IN; |
7388 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 7389 | ret = 1; |
cdd6c482 | 7390 | event->pending_kill = POLL_HUP; |
5aab90ce JO |
7391 | |
7392 | perf_event_disable_inatomic(event); | |
79f14641 PZ |
7393 | } |
7394 | ||
aa6a5f3c | 7395 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 7396 | |
fed66e2c | 7397 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
7398 | event->pending_wakeup = 1; |
7399 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
7400 | } |
7401 | ||
79f14641 | 7402 | return ret; |
f6c7d5fe PZ |
7403 | } |
7404 | ||
a8b0ca17 | 7405 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
7406 | struct perf_sample_data *data, |
7407 | struct pt_regs *regs) | |
850bc73f | 7408 | { |
a8b0ca17 | 7409 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
7410 | } |
7411 | ||
15dbf27c | 7412 | /* |
cdd6c482 | 7413 | * Generic software event infrastructure |
15dbf27c PZ |
7414 | */ |
7415 | ||
b28ab83c PZ |
7416 | struct swevent_htable { |
7417 | struct swevent_hlist *swevent_hlist; | |
7418 | struct mutex hlist_mutex; | |
7419 | int hlist_refcount; | |
7420 | ||
7421 | /* Recursion avoidance in each contexts */ | |
7422 | int recursion[PERF_NR_CONTEXTS]; | |
7423 | }; | |
7424 | ||
7425 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
7426 | ||
7b4b6658 | 7427 | /* |
cdd6c482 IM |
7428 | * We directly increment event->count and keep a second value in |
7429 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
7430 | * is kept in the range [-sample_period, 0] so that we can use the |
7431 | * sign as trigger. | |
7432 | */ | |
7433 | ||
ab573844 | 7434 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 7435 | { |
cdd6c482 | 7436 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
7437 | u64 period = hwc->last_period; |
7438 | u64 nr, offset; | |
7439 | s64 old, val; | |
7440 | ||
7441 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
7442 | |
7443 | again: | |
e7850595 | 7444 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
7445 | if (val < 0) |
7446 | return 0; | |
15dbf27c | 7447 | |
7b4b6658 PZ |
7448 | nr = div64_u64(period + val, period); |
7449 | offset = nr * period; | |
7450 | val -= offset; | |
e7850595 | 7451 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 7452 | goto again; |
15dbf27c | 7453 | |
7b4b6658 | 7454 | return nr; |
15dbf27c PZ |
7455 | } |
7456 | ||
0cff784a | 7457 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 7458 | struct perf_sample_data *data, |
5622f295 | 7459 | struct pt_regs *regs) |
15dbf27c | 7460 | { |
cdd6c482 | 7461 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 7462 | int throttle = 0; |
15dbf27c | 7463 | |
0cff784a PZ |
7464 | if (!overflow) |
7465 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 7466 | |
7b4b6658 PZ |
7467 | if (hwc->interrupts == MAX_INTERRUPTS) |
7468 | return; | |
15dbf27c | 7469 | |
7b4b6658 | 7470 | for (; overflow; overflow--) { |
a8b0ca17 | 7471 | if (__perf_event_overflow(event, throttle, |
5622f295 | 7472 | data, regs)) { |
7b4b6658 PZ |
7473 | /* |
7474 | * We inhibit the overflow from happening when | |
7475 | * hwc->interrupts == MAX_INTERRUPTS. | |
7476 | */ | |
7477 | break; | |
7478 | } | |
cf450a73 | 7479 | throttle = 1; |
7b4b6658 | 7480 | } |
15dbf27c PZ |
7481 | } |
7482 | ||
a4eaf7f1 | 7483 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 7484 | struct perf_sample_data *data, |
5622f295 | 7485 | struct pt_regs *regs) |
7b4b6658 | 7486 | { |
cdd6c482 | 7487 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 7488 | |
e7850595 | 7489 | local64_add(nr, &event->count); |
d6d020e9 | 7490 | |
0cff784a PZ |
7491 | if (!regs) |
7492 | return; | |
7493 | ||
6c7e550f | 7494 | if (!is_sampling_event(event)) |
7b4b6658 | 7495 | return; |
d6d020e9 | 7496 | |
5d81e5cf AV |
7497 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
7498 | data->period = nr; | |
7499 | return perf_swevent_overflow(event, 1, data, regs); | |
7500 | } else | |
7501 | data->period = event->hw.last_period; | |
7502 | ||
0cff784a | 7503 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 7504 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 7505 | |
e7850595 | 7506 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 7507 | return; |
df1a132b | 7508 | |
a8b0ca17 | 7509 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
7510 | } |
7511 | ||
f5ffe02e FW |
7512 | static int perf_exclude_event(struct perf_event *event, |
7513 | struct pt_regs *regs) | |
7514 | { | |
a4eaf7f1 | 7515 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 7516 | return 1; |
a4eaf7f1 | 7517 | |
f5ffe02e FW |
7518 | if (regs) { |
7519 | if (event->attr.exclude_user && user_mode(regs)) | |
7520 | return 1; | |
7521 | ||
7522 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
7523 | return 1; | |
7524 | } | |
7525 | ||
7526 | return 0; | |
7527 | } | |
7528 | ||
cdd6c482 | 7529 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 7530 | enum perf_type_id type, |
6fb2915d LZ |
7531 | u32 event_id, |
7532 | struct perf_sample_data *data, | |
7533 | struct pt_regs *regs) | |
15dbf27c | 7534 | { |
cdd6c482 | 7535 | if (event->attr.type != type) |
a21ca2ca | 7536 | return 0; |
f5ffe02e | 7537 | |
cdd6c482 | 7538 | if (event->attr.config != event_id) |
15dbf27c PZ |
7539 | return 0; |
7540 | ||
f5ffe02e FW |
7541 | if (perf_exclude_event(event, regs)) |
7542 | return 0; | |
15dbf27c PZ |
7543 | |
7544 | return 1; | |
7545 | } | |
7546 | ||
76e1d904 FW |
7547 | static inline u64 swevent_hash(u64 type, u32 event_id) |
7548 | { | |
7549 | u64 val = event_id | (type << 32); | |
7550 | ||
7551 | return hash_64(val, SWEVENT_HLIST_BITS); | |
7552 | } | |
7553 | ||
49f135ed FW |
7554 | static inline struct hlist_head * |
7555 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 7556 | { |
49f135ed FW |
7557 | u64 hash = swevent_hash(type, event_id); |
7558 | ||
7559 | return &hlist->heads[hash]; | |
7560 | } | |
76e1d904 | 7561 | |
49f135ed FW |
7562 | /* For the read side: events when they trigger */ |
7563 | static inline struct hlist_head * | |
b28ab83c | 7564 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
7565 | { |
7566 | struct swevent_hlist *hlist; | |
76e1d904 | 7567 | |
b28ab83c | 7568 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
7569 | if (!hlist) |
7570 | return NULL; | |
7571 | ||
49f135ed FW |
7572 | return __find_swevent_head(hlist, type, event_id); |
7573 | } | |
7574 | ||
7575 | /* For the event head insertion and removal in the hlist */ | |
7576 | static inline struct hlist_head * | |
b28ab83c | 7577 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
7578 | { |
7579 | struct swevent_hlist *hlist; | |
7580 | u32 event_id = event->attr.config; | |
7581 | u64 type = event->attr.type; | |
7582 | ||
7583 | /* | |
7584 | * Event scheduling is always serialized against hlist allocation | |
7585 | * and release. Which makes the protected version suitable here. | |
7586 | * The context lock guarantees that. | |
7587 | */ | |
b28ab83c | 7588 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
7589 | lockdep_is_held(&event->ctx->lock)); |
7590 | if (!hlist) | |
7591 | return NULL; | |
7592 | ||
7593 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
7594 | } |
7595 | ||
7596 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 7597 | u64 nr, |
76e1d904 FW |
7598 | struct perf_sample_data *data, |
7599 | struct pt_regs *regs) | |
15dbf27c | 7600 | { |
4a32fea9 | 7601 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7602 | struct perf_event *event; |
76e1d904 | 7603 | struct hlist_head *head; |
15dbf27c | 7604 | |
76e1d904 | 7605 | rcu_read_lock(); |
b28ab83c | 7606 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
7607 | if (!head) |
7608 | goto end; | |
7609 | ||
b67bfe0d | 7610 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 7611 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 7612 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 7613 | } |
76e1d904 FW |
7614 | end: |
7615 | rcu_read_unlock(); | |
15dbf27c PZ |
7616 | } |
7617 | ||
86038c5e PZI |
7618 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
7619 | ||
4ed7c92d | 7620 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 7621 | { |
4a32fea9 | 7622 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 7623 | |
b28ab83c | 7624 | return get_recursion_context(swhash->recursion); |
96f6d444 | 7625 | } |
645e8cc0 | 7626 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 7627 | |
98b5c2c6 | 7628 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 7629 | { |
4a32fea9 | 7630 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 7631 | |
b28ab83c | 7632 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 7633 | } |
15dbf27c | 7634 | |
86038c5e | 7635 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 7636 | { |
a4234bfc | 7637 | struct perf_sample_data data; |
4ed7c92d | 7638 | |
86038c5e | 7639 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 7640 | return; |
a4234bfc | 7641 | |
fd0d000b | 7642 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 7643 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
7644 | } |
7645 | ||
7646 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
7647 | { | |
7648 | int rctx; | |
7649 | ||
7650 | preempt_disable_notrace(); | |
7651 | rctx = perf_swevent_get_recursion_context(); | |
7652 | if (unlikely(rctx < 0)) | |
7653 | goto fail; | |
7654 | ||
7655 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
7656 | |
7657 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 7658 | fail: |
1c024eca | 7659 | preempt_enable_notrace(); |
b8e83514 PZ |
7660 | } |
7661 | ||
cdd6c482 | 7662 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 7663 | { |
15dbf27c PZ |
7664 | } |
7665 | ||
a4eaf7f1 | 7666 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 7667 | { |
4a32fea9 | 7668 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7669 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
7670 | struct hlist_head *head; |
7671 | ||
6c7e550f | 7672 | if (is_sampling_event(event)) { |
7b4b6658 | 7673 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 7674 | perf_swevent_set_period(event); |
7b4b6658 | 7675 | } |
76e1d904 | 7676 | |
a4eaf7f1 PZ |
7677 | hwc->state = !(flags & PERF_EF_START); |
7678 | ||
b28ab83c | 7679 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 7680 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
7681 | return -EINVAL; |
7682 | ||
7683 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 7684 | perf_event_update_userpage(event); |
76e1d904 | 7685 | |
15dbf27c PZ |
7686 | return 0; |
7687 | } | |
7688 | ||
a4eaf7f1 | 7689 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 7690 | { |
76e1d904 | 7691 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
7692 | } |
7693 | ||
a4eaf7f1 | 7694 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 7695 | { |
a4eaf7f1 | 7696 | event->hw.state = 0; |
d6d020e9 | 7697 | } |
aa9c4c0f | 7698 | |
a4eaf7f1 | 7699 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 7700 | { |
a4eaf7f1 | 7701 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
7702 | } |
7703 | ||
49f135ed FW |
7704 | /* Deref the hlist from the update side */ |
7705 | static inline struct swevent_hlist * | |
b28ab83c | 7706 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 7707 | { |
b28ab83c PZ |
7708 | return rcu_dereference_protected(swhash->swevent_hlist, |
7709 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
7710 | } |
7711 | ||
b28ab83c | 7712 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 7713 | { |
b28ab83c | 7714 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 7715 | |
49f135ed | 7716 | if (!hlist) |
76e1d904 FW |
7717 | return; |
7718 | ||
70691d4a | 7719 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 7720 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
7721 | } |
7722 | ||
3b364d7b | 7723 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 7724 | { |
b28ab83c | 7725 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 7726 | |
b28ab83c | 7727 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 7728 | |
b28ab83c PZ |
7729 | if (!--swhash->hlist_refcount) |
7730 | swevent_hlist_release(swhash); | |
76e1d904 | 7731 | |
b28ab83c | 7732 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7733 | } |
7734 | ||
3b364d7b | 7735 | static void swevent_hlist_put(void) |
76e1d904 FW |
7736 | { |
7737 | int cpu; | |
7738 | ||
76e1d904 | 7739 | for_each_possible_cpu(cpu) |
3b364d7b | 7740 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7741 | } |
7742 | ||
3b364d7b | 7743 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 7744 | { |
b28ab83c | 7745 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
7746 | int err = 0; |
7747 | ||
b28ab83c | 7748 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
7749 | if (!swevent_hlist_deref(swhash) && |
7750 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
7751 | struct swevent_hlist *hlist; |
7752 | ||
7753 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
7754 | if (!hlist) { | |
7755 | err = -ENOMEM; | |
7756 | goto exit; | |
7757 | } | |
b28ab83c | 7758 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 7759 | } |
b28ab83c | 7760 | swhash->hlist_refcount++; |
9ed6060d | 7761 | exit: |
b28ab83c | 7762 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7763 | |
7764 | return err; | |
7765 | } | |
7766 | ||
3b364d7b | 7767 | static int swevent_hlist_get(void) |
76e1d904 | 7768 | { |
3b364d7b | 7769 | int err, cpu, failed_cpu; |
76e1d904 | 7770 | |
a63fbed7 | 7771 | mutex_lock(&pmus_lock); |
76e1d904 | 7772 | for_each_possible_cpu(cpu) { |
3b364d7b | 7773 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
7774 | if (err) { |
7775 | failed_cpu = cpu; | |
7776 | goto fail; | |
7777 | } | |
7778 | } | |
a63fbed7 | 7779 | mutex_unlock(&pmus_lock); |
76e1d904 | 7780 | return 0; |
9ed6060d | 7781 | fail: |
76e1d904 FW |
7782 | for_each_possible_cpu(cpu) { |
7783 | if (cpu == failed_cpu) | |
7784 | break; | |
3b364d7b | 7785 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 7786 | } |
a63fbed7 | 7787 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
7788 | return err; |
7789 | } | |
7790 | ||
c5905afb | 7791 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 7792 | |
b0a873eb PZ |
7793 | static void sw_perf_event_destroy(struct perf_event *event) |
7794 | { | |
7795 | u64 event_id = event->attr.config; | |
95476b64 | 7796 | |
b0a873eb PZ |
7797 | WARN_ON(event->parent); |
7798 | ||
c5905afb | 7799 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 7800 | swevent_hlist_put(); |
b0a873eb PZ |
7801 | } |
7802 | ||
7803 | static int perf_swevent_init(struct perf_event *event) | |
7804 | { | |
8176cced | 7805 | u64 event_id = event->attr.config; |
b0a873eb PZ |
7806 | |
7807 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7808 | return -ENOENT; | |
7809 | ||
2481c5fa SE |
7810 | /* |
7811 | * no branch sampling for software events | |
7812 | */ | |
7813 | if (has_branch_stack(event)) | |
7814 | return -EOPNOTSUPP; | |
7815 | ||
b0a873eb PZ |
7816 | switch (event_id) { |
7817 | case PERF_COUNT_SW_CPU_CLOCK: | |
7818 | case PERF_COUNT_SW_TASK_CLOCK: | |
7819 | return -ENOENT; | |
7820 | ||
7821 | default: | |
7822 | break; | |
7823 | } | |
7824 | ||
ce677831 | 7825 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
7826 | return -ENOENT; |
7827 | ||
7828 | if (!event->parent) { | |
7829 | int err; | |
7830 | ||
3b364d7b | 7831 | err = swevent_hlist_get(); |
b0a873eb PZ |
7832 | if (err) |
7833 | return err; | |
7834 | ||
c5905afb | 7835 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
7836 | event->destroy = sw_perf_event_destroy; |
7837 | } | |
7838 | ||
7839 | return 0; | |
7840 | } | |
7841 | ||
7842 | static struct pmu perf_swevent = { | |
89a1e187 | 7843 | .task_ctx_nr = perf_sw_context, |
95476b64 | 7844 | |
34f43927 PZ |
7845 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7846 | ||
b0a873eb | 7847 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
7848 | .add = perf_swevent_add, |
7849 | .del = perf_swevent_del, | |
7850 | .start = perf_swevent_start, | |
7851 | .stop = perf_swevent_stop, | |
1c024eca | 7852 | .read = perf_swevent_read, |
1c024eca PZ |
7853 | }; |
7854 | ||
b0a873eb PZ |
7855 | #ifdef CONFIG_EVENT_TRACING |
7856 | ||
1c024eca PZ |
7857 | static int perf_tp_filter_match(struct perf_event *event, |
7858 | struct perf_sample_data *data) | |
7859 | { | |
7e3f977e | 7860 | void *record = data->raw->frag.data; |
1c024eca | 7861 | |
b71b437e PZ |
7862 | /* only top level events have filters set */ |
7863 | if (event->parent) | |
7864 | event = event->parent; | |
7865 | ||
1c024eca PZ |
7866 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
7867 | return 1; | |
7868 | return 0; | |
7869 | } | |
7870 | ||
7871 | static int perf_tp_event_match(struct perf_event *event, | |
7872 | struct perf_sample_data *data, | |
7873 | struct pt_regs *regs) | |
7874 | { | |
a0f7d0f7 FW |
7875 | if (event->hw.state & PERF_HES_STOPPED) |
7876 | return 0; | |
580d607c PZ |
7877 | /* |
7878 | * All tracepoints are from kernel-space. | |
7879 | */ | |
7880 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
7881 | return 0; |
7882 | ||
7883 | if (!perf_tp_filter_match(event, data)) | |
7884 | return 0; | |
7885 | ||
7886 | return 1; | |
7887 | } | |
7888 | ||
85b67bcb AS |
7889 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
7890 | struct trace_event_call *call, u64 count, | |
7891 | struct pt_regs *regs, struct hlist_head *head, | |
7892 | struct task_struct *task) | |
7893 | { | |
7894 | struct bpf_prog *prog = call->prog; | |
7895 | ||
7896 | if (prog) { | |
7897 | *(struct pt_regs **)raw_data = regs; | |
7898 | if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) { | |
7899 | perf_swevent_put_recursion_context(rctx); | |
7900 | return; | |
7901 | } | |
7902 | } | |
7903 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
75e83876 | 7904 | rctx, task, NULL); |
85b67bcb AS |
7905 | } |
7906 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
7907 | ||
1e1dcd93 | 7908 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 7909 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
75e83876 | 7910 | struct task_struct *task, struct perf_event *event) |
95476b64 FW |
7911 | { |
7912 | struct perf_sample_data data; | |
1c024eca | 7913 | |
95476b64 | 7914 | struct perf_raw_record raw = { |
7e3f977e DB |
7915 | .frag = { |
7916 | .size = entry_size, | |
7917 | .data = record, | |
7918 | }, | |
95476b64 FW |
7919 | }; |
7920 | ||
1e1dcd93 | 7921 | perf_sample_data_init(&data, 0, 0); |
95476b64 FW |
7922 | data.raw = &raw; |
7923 | ||
1e1dcd93 AS |
7924 | perf_trace_buf_update(record, event_type); |
7925 | ||
75e83876 ZC |
7926 | /* Use the given event instead of the hlist */ |
7927 | if (event) { | |
1c024eca | 7928 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 7929 | perf_swevent_event(event, count, &data, regs); |
75e83876 ZC |
7930 | } else { |
7931 | hlist_for_each_entry_rcu(event, head, hlist_entry) { | |
7932 | if (perf_tp_event_match(event, &data, regs)) | |
7933 | perf_swevent_event(event, count, &data, regs); | |
7934 | } | |
4f41c013 | 7935 | } |
ecc55f84 | 7936 | |
e6dab5ff AV |
7937 | /* |
7938 | * If we got specified a target task, also iterate its context and | |
7939 | * deliver this event there too. | |
7940 | */ | |
7941 | if (task && task != current) { | |
7942 | struct perf_event_context *ctx; | |
7943 | struct trace_entry *entry = record; | |
7944 | ||
7945 | rcu_read_lock(); | |
7946 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
7947 | if (!ctx) | |
7948 | goto unlock; | |
7949 | ||
7950 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
7951 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7952 | continue; | |
7953 | if (event->attr.config != entry->type) | |
7954 | continue; | |
7955 | if (perf_tp_event_match(event, &data, regs)) | |
7956 | perf_swevent_event(event, count, &data, regs); | |
7957 | } | |
7958 | unlock: | |
7959 | rcu_read_unlock(); | |
7960 | } | |
7961 | ||
ecc55f84 | 7962 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
7963 | } |
7964 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
7965 | ||
cdd6c482 | 7966 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 7967 | { |
1c024eca | 7968 | perf_trace_destroy(event); |
e077df4f PZ |
7969 | } |
7970 | ||
b0a873eb | 7971 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 7972 | { |
76e1d904 FW |
7973 | int err; |
7974 | ||
b0a873eb PZ |
7975 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
7976 | return -ENOENT; | |
7977 | ||
2481c5fa SE |
7978 | /* |
7979 | * no branch sampling for tracepoint events | |
7980 | */ | |
7981 | if (has_branch_stack(event)) | |
7982 | return -EOPNOTSUPP; | |
7983 | ||
1c024eca PZ |
7984 | err = perf_trace_init(event); |
7985 | if (err) | |
b0a873eb | 7986 | return err; |
e077df4f | 7987 | |
cdd6c482 | 7988 | event->destroy = tp_perf_event_destroy; |
e077df4f | 7989 | |
b0a873eb PZ |
7990 | return 0; |
7991 | } | |
7992 | ||
7993 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
7994 | .task_ctx_nr = perf_sw_context, |
7995 | ||
b0a873eb | 7996 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
7997 | .add = perf_trace_add, |
7998 | .del = perf_trace_del, | |
7999 | .start = perf_swevent_start, | |
8000 | .stop = perf_swevent_stop, | |
b0a873eb | 8001 | .read = perf_swevent_read, |
b0a873eb PZ |
8002 | }; |
8003 | ||
8004 | static inline void perf_tp_register(void) | |
8005 | { | |
2e80a82a | 8006 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e077df4f | 8007 | } |
6fb2915d | 8008 | |
6fb2915d LZ |
8009 | static void perf_event_free_filter(struct perf_event *event) |
8010 | { | |
8011 | ftrace_profile_free_filter(event); | |
8012 | } | |
8013 | ||
aa6a5f3c AS |
8014 | #ifdef CONFIG_BPF_SYSCALL |
8015 | static void bpf_overflow_handler(struct perf_event *event, | |
8016 | struct perf_sample_data *data, | |
8017 | struct pt_regs *regs) | |
8018 | { | |
8019 | struct bpf_perf_event_data_kern ctx = { | |
8020 | .data = data, | |
8021 | .regs = regs, | |
8022 | }; | |
8023 | int ret = 0; | |
8024 | ||
8025 | preempt_disable(); | |
8026 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) | |
8027 | goto out; | |
8028 | rcu_read_lock(); | |
88575199 | 8029 | ret = BPF_PROG_RUN(event->prog, &ctx); |
aa6a5f3c AS |
8030 | rcu_read_unlock(); |
8031 | out: | |
8032 | __this_cpu_dec(bpf_prog_active); | |
8033 | preempt_enable(); | |
8034 | if (!ret) | |
8035 | return; | |
8036 | ||
8037 | event->orig_overflow_handler(event, data, regs); | |
8038 | } | |
8039 | ||
8040 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8041 | { | |
8042 | struct bpf_prog *prog; | |
8043 | ||
8044 | if (event->overflow_handler_context) | |
8045 | /* hw breakpoint or kernel counter */ | |
8046 | return -EINVAL; | |
8047 | ||
8048 | if (event->prog) | |
8049 | return -EEXIST; | |
8050 | ||
8051 | prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); | |
8052 | if (IS_ERR(prog)) | |
8053 | return PTR_ERR(prog); | |
8054 | ||
8055 | event->prog = prog; | |
8056 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); | |
8057 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
8058 | return 0; | |
8059 | } | |
8060 | ||
8061 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8062 | { | |
8063 | struct bpf_prog *prog = event->prog; | |
8064 | ||
8065 | if (!prog) | |
8066 | return; | |
8067 | ||
8068 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
8069 | event->prog = NULL; | |
8070 | bpf_prog_put(prog); | |
8071 | } | |
8072 | #else | |
8073 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8074 | { | |
8075 | return -EOPNOTSUPP; | |
8076 | } | |
8077 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8078 | { | |
8079 | } | |
8080 | #endif | |
8081 | ||
2541517c AS |
8082 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8083 | { | |
98b5c2c6 | 8084 | bool is_kprobe, is_tracepoint; |
2541517c AS |
8085 | struct bpf_prog *prog; |
8086 | ||
8087 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
f91840a3 | 8088 | return perf_event_set_bpf_handler(event, prog_fd); |
2541517c AS |
8089 | |
8090 | if (event->tp_event->prog) | |
8091 | return -EEXIST; | |
8092 | ||
98b5c2c6 AS |
8093 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; |
8094 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; | |
8095 | if (!is_kprobe && !is_tracepoint) | |
8096 | /* bpf programs can only be attached to u/kprobe or tracepoint */ | |
2541517c AS |
8097 | return -EINVAL; |
8098 | ||
8099 | prog = bpf_prog_get(prog_fd); | |
8100 | if (IS_ERR(prog)) | |
8101 | return PTR_ERR(prog); | |
8102 | ||
98b5c2c6 AS |
8103 | if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || |
8104 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { | |
2541517c AS |
8105 | /* valid fd, but invalid bpf program type */ |
8106 | bpf_prog_put(prog); | |
8107 | return -EINVAL; | |
8108 | } | |
8109 | ||
32bbe007 AS |
8110 | if (is_tracepoint) { |
8111 | int off = trace_event_get_offsets(event->tp_event); | |
8112 | ||
8113 | if (prog->aux->max_ctx_offset > off) { | |
8114 | bpf_prog_put(prog); | |
8115 | return -EACCES; | |
8116 | } | |
8117 | } | |
2541517c | 8118 | event->tp_event->prog = prog; |
7dc21ce0 | 8119 | event->tp_event->bpf_prog_owner = event; |
2541517c AS |
8120 | |
8121 | return 0; | |
8122 | } | |
8123 | ||
8124 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8125 | { | |
8126 | struct bpf_prog *prog; | |
8127 | ||
aa6a5f3c AS |
8128 | perf_event_free_bpf_handler(event); |
8129 | ||
2541517c AS |
8130 | if (!event->tp_event) |
8131 | return; | |
8132 | ||
8133 | prog = event->tp_event->prog; | |
7dc21ce0 | 8134 | if (prog && event->tp_event->bpf_prog_owner == event) { |
2541517c | 8135 | event->tp_event->prog = NULL; |
1aacde3d | 8136 | bpf_prog_put(prog); |
2541517c AS |
8137 | } |
8138 | } | |
8139 | ||
e077df4f | 8140 | #else |
6fb2915d | 8141 | |
b0a873eb | 8142 | static inline void perf_tp_register(void) |
e077df4f | 8143 | { |
e077df4f | 8144 | } |
6fb2915d | 8145 | |
6fb2915d LZ |
8146 | static void perf_event_free_filter(struct perf_event *event) |
8147 | { | |
8148 | } | |
8149 | ||
2541517c AS |
8150 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8151 | { | |
8152 | return -ENOENT; | |
8153 | } | |
8154 | ||
8155 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8156 | { | |
8157 | } | |
07b139c8 | 8158 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 8159 | |
24f1e32c | 8160 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 8161 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 8162 | { |
f5ffe02e FW |
8163 | struct perf_sample_data sample; |
8164 | struct pt_regs *regs = data; | |
8165 | ||
fd0d000b | 8166 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 8167 | |
a4eaf7f1 | 8168 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 8169 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
8170 | } |
8171 | #endif | |
8172 | ||
375637bc AS |
8173 | /* |
8174 | * Allocate a new address filter | |
8175 | */ | |
8176 | static struct perf_addr_filter * | |
8177 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
8178 | { | |
8179 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
8180 | struct perf_addr_filter *filter; | |
8181 | ||
8182 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
8183 | if (!filter) | |
8184 | return NULL; | |
8185 | ||
8186 | INIT_LIST_HEAD(&filter->entry); | |
8187 | list_add_tail(&filter->entry, filters); | |
8188 | ||
8189 | return filter; | |
8190 | } | |
8191 | ||
8192 | static void free_filters_list(struct list_head *filters) | |
8193 | { | |
8194 | struct perf_addr_filter *filter, *iter; | |
8195 | ||
8196 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
8197 | if (filter->inode) | |
8198 | iput(filter->inode); | |
8199 | list_del(&filter->entry); | |
8200 | kfree(filter); | |
8201 | } | |
8202 | } | |
8203 | ||
8204 | /* | |
8205 | * Free existing address filters and optionally install new ones | |
8206 | */ | |
8207 | static void perf_addr_filters_splice(struct perf_event *event, | |
8208 | struct list_head *head) | |
8209 | { | |
8210 | unsigned long flags; | |
8211 | LIST_HEAD(list); | |
8212 | ||
8213 | if (!has_addr_filter(event)) | |
8214 | return; | |
8215 | ||
8216 | /* don't bother with children, they don't have their own filters */ | |
8217 | if (event->parent) | |
8218 | return; | |
8219 | ||
8220 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
8221 | ||
8222 | list_splice_init(&event->addr_filters.list, &list); | |
8223 | if (head) | |
8224 | list_splice(head, &event->addr_filters.list); | |
8225 | ||
8226 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
8227 | ||
8228 | free_filters_list(&list); | |
8229 | } | |
8230 | ||
8231 | /* | |
8232 | * Scan through mm's vmas and see if one of them matches the | |
8233 | * @filter; if so, adjust filter's address range. | |
8234 | * Called with mm::mmap_sem down for reading. | |
8235 | */ | |
8236 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, | |
8237 | struct mm_struct *mm) | |
8238 | { | |
8239 | struct vm_area_struct *vma; | |
8240 | ||
8241 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
8242 | struct file *file = vma->vm_file; | |
8243 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8244 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8245 | ||
8246 | if (!file) | |
8247 | continue; | |
8248 | ||
8249 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8250 | continue; | |
8251 | ||
8252 | return vma->vm_start; | |
8253 | } | |
8254 | ||
8255 | return 0; | |
8256 | } | |
8257 | ||
8258 | /* | |
8259 | * Update event's address range filters based on the | |
8260 | * task's existing mappings, if any. | |
8261 | */ | |
8262 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
8263 | { | |
8264 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8265 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
8266 | struct perf_addr_filter *filter; | |
8267 | struct mm_struct *mm = NULL; | |
8268 | unsigned int count = 0; | |
8269 | unsigned long flags; | |
8270 | ||
8271 | /* | |
8272 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
8273 | * will stop on the parent's child_mutex that our caller is also holding | |
8274 | */ | |
8275 | if (task == TASK_TOMBSTONE) | |
8276 | return; | |
8277 | ||
6ce77bfd AS |
8278 | if (!ifh->nr_file_filters) |
8279 | return; | |
8280 | ||
375637bc AS |
8281 | mm = get_task_mm(event->ctx->task); |
8282 | if (!mm) | |
8283 | goto restart; | |
8284 | ||
8285 | down_read(&mm->mmap_sem); | |
8286 | ||
8287 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8288 | list_for_each_entry(filter, &ifh->list, entry) { | |
8289 | event->addr_filters_offs[count] = 0; | |
8290 | ||
99f5bc9b MP |
8291 | /* |
8292 | * Adjust base offset if the filter is associated to a binary | |
8293 | * that needs to be mapped: | |
8294 | */ | |
8295 | if (filter->inode) | |
375637bc AS |
8296 | event->addr_filters_offs[count] = |
8297 | perf_addr_filter_apply(filter, mm); | |
8298 | ||
8299 | count++; | |
8300 | } | |
8301 | ||
8302 | event->addr_filters_gen++; | |
8303 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8304 | ||
8305 | up_read(&mm->mmap_sem); | |
8306 | ||
8307 | mmput(mm); | |
8308 | ||
8309 | restart: | |
767ae086 | 8310 | perf_event_stop(event, 1); |
375637bc AS |
8311 | } |
8312 | ||
8313 | /* | |
8314 | * Address range filtering: limiting the data to certain | |
8315 | * instruction address ranges. Filters are ioctl()ed to us from | |
8316 | * userspace as ascii strings. | |
8317 | * | |
8318 | * Filter string format: | |
8319 | * | |
8320 | * ACTION RANGE_SPEC | |
8321 | * where ACTION is one of the | |
8322 | * * "filter": limit the trace to this region | |
8323 | * * "start": start tracing from this address | |
8324 | * * "stop": stop tracing at this address/region; | |
8325 | * RANGE_SPEC is | |
8326 | * * for kernel addresses: <start address>[/<size>] | |
8327 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
8328 | * | |
8329 | * if <size> is not specified, the range is treated as a single address. | |
8330 | */ | |
8331 | enum { | |
e96271f3 | 8332 | IF_ACT_NONE = -1, |
375637bc AS |
8333 | IF_ACT_FILTER, |
8334 | IF_ACT_START, | |
8335 | IF_ACT_STOP, | |
8336 | IF_SRC_FILE, | |
8337 | IF_SRC_KERNEL, | |
8338 | IF_SRC_FILEADDR, | |
8339 | IF_SRC_KERNELADDR, | |
8340 | }; | |
8341 | ||
8342 | enum { | |
8343 | IF_STATE_ACTION = 0, | |
8344 | IF_STATE_SOURCE, | |
8345 | IF_STATE_END, | |
8346 | }; | |
8347 | ||
8348 | static const match_table_t if_tokens = { | |
8349 | { IF_ACT_FILTER, "filter" }, | |
8350 | { IF_ACT_START, "start" }, | |
8351 | { IF_ACT_STOP, "stop" }, | |
8352 | { IF_SRC_FILE, "%u/%u@%s" }, | |
8353 | { IF_SRC_KERNEL, "%u/%u" }, | |
8354 | { IF_SRC_FILEADDR, "%u@%s" }, | |
8355 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 8356 | { IF_ACT_NONE, NULL }, |
375637bc AS |
8357 | }; |
8358 | ||
8359 | /* | |
8360 | * Address filter string parser | |
8361 | */ | |
8362 | static int | |
8363 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
8364 | struct list_head *filters) | |
8365 | { | |
8366 | struct perf_addr_filter *filter = NULL; | |
8367 | char *start, *orig, *filename = NULL; | |
8368 | struct path path; | |
8369 | substring_t args[MAX_OPT_ARGS]; | |
8370 | int state = IF_STATE_ACTION, token; | |
8371 | unsigned int kernel = 0; | |
8372 | int ret = -EINVAL; | |
8373 | ||
8374 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
8375 | if (!fstr) | |
8376 | return -ENOMEM; | |
8377 | ||
8378 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
8379 | ret = -EINVAL; | |
8380 | ||
8381 | if (!*start) | |
8382 | continue; | |
8383 | ||
8384 | /* filter definition begins */ | |
8385 | if (state == IF_STATE_ACTION) { | |
8386 | filter = perf_addr_filter_new(event, filters); | |
8387 | if (!filter) | |
8388 | goto fail; | |
8389 | } | |
8390 | ||
8391 | token = match_token(start, if_tokens, args); | |
8392 | switch (token) { | |
8393 | case IF_ACT_FILTER: | |
8394 | case IF_ACT_START: | |
8395 | filter->filter = 1; | |
8396 | ||
8397 | case IF_ACT_STOP: | |
8398 | if (state != IF_STATE_ACTION) | |
8399 | goto fail; | |
8400 | ||
8401 | state = IF_STATE_SOURCE; | |
8402 | break; | |
8403 | ||
8404 | case IF_SRC_KERNELADDR: | |
8405 | case IF_SRC_KERNEL: | |
8406 | kernel = 1; | |
8407 | ||
8408 | case IF_SRC_FILEADDR: | |
8409 | case IF_SRC_FILE: | |
8410 | if (state != IF_STATE_SOURCE) | |
8411 | goto fail; | |
8412 | ||
8413 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) | |
8414 | filter->range = 1; | |
8415 | ||
8416 | *args[0].to = 0; | |
8417 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
8418 | if (ret) | |
8419 | goto fail; | |
8420 | ||
8421 | if (filter->range) { | |
8422 | *args[1].to = 0; | |
8423 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
8424 | if (ret) | |
8425 | goto fail; | |
8426 | } | |
8427 | ||
4059ffd0 MP |
8428 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
8429 | int fpos = filter->range ? 2 : 1; | |
8430 | ||
8431 | filename = match_strdup(&args[fpos]); | |
375637bc AS |
8432 | if (!filename) { |
8433 | ret = -ENOMEM; | |
8434 | goto fail; | |
8435 | } | |
8436 | } | |
8437 | ||
8438 | state = IF_STATE_END; | |
8439 | break; | |
8440 | ||
8441 | default: | |
8442 | goto fail; | |
8443 | } | |
8444 | ||
8445 | /* | |
8446 | * Filter definition is fully parsed, validate and install it. | |
8447 | * Make sure that it doesn't contradict itself or the event's | |
8448 | * attribute. | |
8449 | */ | |
8450 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 8451 | ret = -EINVAL; |
375637bc AS |
8452 | if (kernel && event->attr.exclude_kernel) |
8453 | goto fail; | |
8454 | ||
8455 | if (!kernel) { | |
8456 | if (!filename) | |
8457 | goto fail; | |
8458 | ||
6ce77bfd AS |
8459 | /* |
8460 | * For now, we only support file-based filters | |
8461 | * in per-task events; doing so for CPU-wide | |
8462 | * events requires additional context switching | |
8463 | * trickery, since same object code will be | |
8464 | * mapped at different virtual addresses in | |
8465 | * different processes. | |
8466 | */ | |
8467 | ret = -EOPNOTSUPP; | |
8468 | if (!event->ctx->task) | |
8469 | goto fail_free_name; | |
8470 | ||
375637bc AS |
8471 | /* look up the path and grab its inode */ |
8472 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
8473 | if (ret) | |
8474 | goto fail_free_name; | |
8475 | ||
8476 | filter->inode = igrab(d_inode(path.dentry)); | |
8477 | path_put(&path); | |
8478 | kfree(filename); | |
8479 | filename = NULL; | |
8480 | ||
8481 | ret = -EINVAL; | |
8482 | if (!filter->inode || | |
8483 | !S_ISREG(filter->inode->i_mode)) | |
8484 | /* free_filters_list() will iput() */ | |
8485 | goto fail; | |
6ce77bfd AS |
8486 | |
8487 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
8488 | } |
8489 | ||
8490 | /* ready to consume more filters */ | |
8491 | state = IF_STATE_ACTION; | |
8492 | filter = NULL; | |
8493 | } | |
8494 | } | |
8495 | ||
8496 | if (state != IF_STATE_ACTION) | |
8497 | goto fail; | |
8498 | ||
8499 | kfree(orig); | |
8500 | ||
8501 | return 0; | |
8502 | ||
8503 | fail_free_name: | |
8504 | kfree(filename); | |
8505 | fail: | |
8506 | free_filters_list(filters); | |
8507 | kfree(orig); | |
8508 | ||
8509 | return ret; | |
8510 | } | |
8511 | ||
8512 | static int | |
8513 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
8514 | { | |
8515 | LIST_HEAD(filters); | |
8516 | int ret; | |
8517 | ||
8518 | /* | |
8519 | * Since this is called in perf_ioctl() path, we're already holding | |
8520 | * ctx::mutex. | |
8521 | */ | |
8522 | lockdep_assert_held(&event->ctx->mutex); | |
8523 | ||
8524 | if (WARN_ON_ONCE(event->parent)) | |
8525 | return -EINVAL; | |
8526 | ||
375637bc AS |
8527 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
8528 | if (ret) | |
6ce77bfd | 8529 | goto fail_clear_files; |
375637bc AS |
8530 | |
8531 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
8532 | if (ret) |
8533 | goto fail_free_filters; | |
375637bc AS |
8534 | |
8535 | /* remove existing filters, if any */ | |
8536 | perf_addr_filters_splice(event, &filters); | |
8537 | ||
8538 | /* install new filters */ | |
8539 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
8540 | ||
6ce77bfd AS |
8541 | return ret; |
8542 | ||
8543 | fail_free_filters: | |
8544 | free_filters_list(&filters); | |
8545 | ||
8546 | fail_clear_files: | |
8547 | event->addr_filters.nr_file_filters = 0; | |
8548 | ||
375637bc AS |
8549 | return ret; |
8550 | } | |
8551 | ||
c796bbbe AS |
8552 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
8553 | { | |
8554 | char *filter_str; | |
8555 | int ret = -EINVAL; | |
8556 | ||
375637bc AS |
8557 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
8558 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && | |
8559 | !has_addr_filter(event)) | |
c796bbbe AS |
8560 | return -EINVAL; |
8561 | ||
8562 | filter_str = strndup_user(arg, PAGE_SIZE); | |
8563 | if (IS_ERR(filter_str)) | |
8564 | return PTR_ERR(filter_str); | |
8565 | ||
8566 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && | |
8567 | event->attr.type == PERF_TYPE_TRACEPOINT) | |
8568 | ret = ftrace_profile_set_filter(event, event->attr.config, | |
8569 | filter_str); | |
375637bc AS |
8570 | else if (has_addr_filter(event)) |
8571 | ret = perf_event_set_addr_filter(event, filter_str); | |
c796bbbe AS |
8572 | |
8573 | kfree(filter_str); | |
8574 | return ret; | |
8575 | } | |
8576 | ||
b0a873eb PZ |
8577 | /* |
8578 | * hrtimer based swevent callback | |
8579 | */ | |
f29ac756 | 8580 | |
b0a873eb | 8581 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 8582 | { |
b0a873eb PZ |
8583 | enum hrtimer_restart ret = HRTIMER_RESTART; |
8584 | struct perf_sample_data data; | |
8585 | struct pt_regs *regs; | |
8586 | struct perf_event *event; | |
8587 | u64 period; | |
f29ac756 | 8588 | |
b0a873eb | 8589 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
8590 | |
8591 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
8592 | return HRTIMER_NORESTART; | |
8593 | ||
b0a873eb | 8594 | event->pmu->read(event); |
f344011c | 8595 | |
fd0d000b | 8596 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
8597 | regs = get_irq_regs(); |
8598 | ||
8599 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 8600 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 8601 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
8602 | ret = HRTIMER_NORESTART; |
8603 | } | |
24f1e32c | 8604 | |
b0a873eb PZ |
8605 | period = max_t(u64, 10000, event->hw.sample_period); |
8606 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 8607 | |
b0a873eb | 8608 | return ret; |
f29ac756 PZ |
8609 | } |
8610 | ||
b0a873eb | 8611 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 8612 | { |
b0a873eb | 8613 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
8614 | s64 period; |
8615 | ||
8616 | if (!is_sampling_event(event)) | |
8617 | return; | |
f5ffe02e | 8618 | |
5d508e82 FBH |
8619 | period = local64_read(&hwc->period_left); |
8620 | if (period) { | |
8621 | if (period < 0) | |
8622 | period = 10000; | |
fa407f35 | 8623 | |
5d508e82 FBH |
8624 | local64_set(&hwc->period_left, 0); |
8625 | } else { | |
8626 | period = max_t(u64, 10000, hwc->sample_period); | |
8627 | } | |
3497d206 TG |
8628 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
8629 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 8630 | } |
b0a873eb PZ |
8631 | |
8632 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 8633 | { |
b0a873eb PZ |
8634 | struct hw_perf_event *hwc = &event->hw; |
8635 | ||
6c7e550f | 8636 | if (is_sampling_event(event)) { |
b0a873eb | 8637 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 8638 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
8639 | |
8640 | hrtimer_cancel(&hwc->hrtimer); | |
8641 | } | |
24f1e32c FW |
8642 | } |
8643 | ||
ba3dd36c PZ |
8644 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
8645 | { | |
8646 | struct hw_perf_event *hwc = &event->hw; | |
8647 | ||
8648 | if (!is_sampling_event(event)) | |
8649 | return; | |
8650 | ||
8651 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
8652 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
8653 | ||
8654 | /* | |
8655 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
8656 | * mapping and avoid the whole period adjust feedback stuff. | |
8657 | */ | |
8658 | if (event->attr.freq) { | |
8659 | long freq = event->attr.sample_freq; | |
8660 | ||
8661 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
8662 | hwc->sample_period = event->attr.sample_period; | |
8663 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 8664 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
8665 | event->attr.freq = 0; |
8666 | } | |
8667 | } | |
8668 | ||
b0a873eb PZ |
8669 | /* |
8670 | * Software event: cpu wall time clock | |
8671 | */ | |
8672 | ||
8673 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 8674 | { |
b0a873eb PZ |
8675 | s64 prev; |
8676 | u64 now; | |
8677 | ||
a4eaf7f1 | 8678 | now = local_clock(); |
b0a873eb PZ |
8679 | prev = local64_xchg(&event->hw.prev_count, now); |
8680 | local64_add(now - prev, &event->count); | |
24f1e32c | 8681 | } |
24f1e32c | 8682 | |
a4eaf7f1 | 8683 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8684 | { |
a4eaf7f1 | 8685 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 8686 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8687 | } |
8688 | ||
a4eaf7f1 | 8689 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 8690 | { |
b0a873eb PZ |
8691 | perf_swevent_cancel_hrtimer(event); |
8692 | cpu_clock_event_update(event); | |
8693 | } | |
f29ac756 | 8694 | |
a4eaf7f1 PZ |
8695 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
8696 | { | |
8697 | if (flags & PERF_EF_START) | |
8698 | cpu_clock_event_start(event, flags); | |
6a694a60 | 8699 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
8700 | |
8701 | return 0; | |
8702 | } | |
8703 | ||
8704 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
8705 | { | |
8706 | cpu_clock_event_stop(event, flags); | |
8707 | } | |
8708 | ||
b0a873eb PZ |
8709 | static void cpu_clock_event_read(struct perf_event *event) |
8710 | { | |
8711 | cpu_clock_event_update(event); | |
8712 | } | |
f344011c | 8713 | |
b0a873eb PZ |
8714 | static int cpu_clock_event_init(struct perf_event *event) |
8715 | { | |
8716 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
8717 | return -ENOENT; | |
8718 | ||
8719 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
8720 | return -ENOENT; | |
8721 | ||
2481c5fa SE |
8722 | /* |
8723 | * no branch sampling for software events | |
8724 | */ | |
8725 | if (has_branch_stack(event)) | |
8726 | return -EOPNOTSUPP; | |
8727 | ||
ba3dd36c PZ |
8728 | perf_swevent_init_hrtimer(event); |
8729 | ||
b0a873eb | 8730 | return 0; |
f29ac756 PZ |
8731 | } |
8732 | ||
b0a873eb | 8733 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
8734 | .task_ctx_nr = perf_sw_context, |
8735 | ||
34f43927 PZ |
8736 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8737 | ||
b0a873eb | 8738 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
8739 | .add = cpu_clock_event_add, |
8740 | .del = cpu_clock_event_del, | |
8741 | .start = cpu_clock_event_start, | |
8742 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
8743 | .read = cpu_clock_event_read, |
8744 | }; | |
8745 | ||
8746 | /* | |
8747 | * Software event: task time clock | |
8748 | */ | |
8749 | ||
8750 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 8751 | { |
b0a873eb PZ |
8752 | u64 prev; |
8753 | s64 delta; | |
5c92d124 | 8754 | |
b0a873eb PZ |
8755 | prev = local64_xchg(&event->hw.prev_count, now); |
8756 | delta = now - prev; | |
8757 | local64_add(delta, &event->count); | |
8758 | } | |
5c92d124 | 8759 | |
a4eaf7f1 | 8760 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8761 | { |
a4eaf7f1 | 8762 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 8763 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8764 | } |
8765 | ||
a4eaf7f1 | 8766 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
8767 | { |
8768 | perf_swevent_cancel_hrtimer(event); | |
8769 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
8770 | } |
8771 | ||
8772 | static int task_clock_event_add(struct perf_event *event, int flags) | |
8773 | { | |
8774 | if (flags & PERF_EF_START) | |
8775 | task_clock_event_start(event, flags); | |
6a694a60 | 8776 | perf_event_update_userpage(event); |
b0a873eb | 8777 | |
a4eaf7f1 PZ |
8778 | return 0; |
8779 | } | |
8780 | ||
8781 | static void task_clock_event_del(struct perf_event *event, int flags) | |
8782 | { | |
8783 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
8784 | } |
8785 | ||
8786 | static void task_clock_event_read(struct perf_event *event) | |
8787 | { | |
768a06e2 PZ |
8788 | u64 now = perf_clock(); |
8789 | u64 delta = now - event->ctx->timestamp; | |
8790 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
8791 | |
8792 | task_clock_event_update(event, time); | |
8793 | } | |
8794 | ||
8795 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 8796 | { |
b0a873eb PZ |
8797 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
8798 | return -ENOENT; | |
8799 | ||
8800 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
8801 | return -ENOENT; | |
8802 | ||
2481c5fa SE |
8803 | /* |
8804 | * no branch sampling for software events | |
8805 | */ | |
8806 | if (has_branch_stack(event)) | |
8807 | return -EOPNOTSUPP; | |
8808 | ||
ba3dd36c PZ |
8809 | perf_swevent_init_hrtimer(event); |
8810 | ||
b0a873eb | 8811 | return 0; |
6fb2915d LZ |
8812 | } |
8813 | ||
b0a873eb | 8814 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
8815 | .task_ctx_nr = perf_sw_context, |
8816 | ||
34f43927 PZ |
8817 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8818 | ||
b0a873eb | 8819 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
8820 | .add = task_clock_event_add, |
8821 | .del = task_clock_event_del, | |
8822 | .start = task_clock_event_start, | |
8823 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
8824 | .read = task_clock_event_read, |
8825 | }; | |
6fb2915d | 8826 | |
ad5133b7 | 8827 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 8828 | { |
e077df4f | 8829 | } |
6fb2915d | 8830 | |
fbbe0701 SB |
8831 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
8832 | { | |
8833 | } | |
8834 | ||
ad5133b7 | 8835 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 8836 | { |
ad5133b7 | 8837 | return 0; |
6fb2915d LZ |
8838 | } |
8839 | ||
18ab2cd3 | 8840 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
8841 | |
8842 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 8843 | { |
fbbe0701 SB |
8844 | __this_cpu_write(nop_txn_flags, flags); |
8845 | ||
8846 | if (flags & ~PERF_PMU_TXN_ADD) | |
8847 | return; | |
8848 | ||
ad5133b7 | 8849 | perf_pmu_disable(pmu); |
6fb2915d LZ |
8850 | } |
8851 | ||
ad5133b7 PZ |
8852 | static int perf_pmu_commit_txn(struct pmu *pmu) |
8853 | { | |
fbbe0701 SB |
8854 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8855 | ||
8856 | __this_cpu_write(nop_txn_flags, 0); | |
8857 | ||
8858 | if (flags & ~PERF_PMU_TXN_ADD) | |
8859 | return 0; | |
8860 | ||
ad5133b7 PZ |
8861 | perf_pmu_enable(pmu); |
8862 | return 0; | |
8863 | } | |
e077df4f | 8864 | |
ad5133b7 | 8865 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 8866 | { |
fbbe0701 SB |
8867 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8868 | ||
8869 | __this_cpu_write(nop_txn_flags, 0); | |
8870 | ||
8871 | if (flags & ~PERF_PMU_TXN_ADD) | |
8872 | return; | |
8873 | ||
ad5133b7 | 8874 | perf_pmu_enable(pmu); |
24f1e32c FW |
8875 | } |
8876 | ||
35edc2a5 PZ |
8877 | static int perf_event_idx_default(struct perf_event *event) |
8878 | { | |
c719f560 | 8879 | return 0; |
35edc2a5 PZ |
8880 | } |
8881 | ||
8dc85d54 PZ |
8882 | /* |
8883 | * Ensures all contexts with the same task_ctx_nr have the same | |
8884 | * pmu_cpu_context too. | |
8885 | */ | |
9e317041 | 8886 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 8887 | { |
8dc85d54 | 8888 | struct pmu *pmu; |
b326e956 | 8889 | |
8dc85d54 PZ |
8890 | if (ctxn < 0) |
8891 | return NULL; | |
24f1e32c | 8892 | |
8dc85d54 PZ |
8893 | list_for_each_entry(pmu, &pmus, entry) { |
8894 | if (pmu->task_ctx_nr == ctxn) | |
8895 | return pmu->pmu_cpu_context; | |
8896 | } | |
24f1e32c | 8897 | |
8dc85d54 | 8898 | return NULL; |
24f1e32c FW |
8899 | } |
8900 | ||
51676957 PZ |
8901 | static void free_pmu_context(struct pmu *pmu) |
8902 | { | |
8dc85d54 | 8903 | mutex_lock(&pmus_lock); |
51676957 | 8904 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 | 8905 | mutex_unlock(&pmus_lock); |
24f1e32c | 8906 | } |
6e855cd4 AS |
8907 | |
8908 | /* | |
8909 | * Let userspace know that this PMU supports address range filtering: | |
8910 | */ | |
8911 | static ssize_t nr_addr_filters_show(struct device *dev, | |
8912 | struct device_attribute *attr, | |
8913 | char *page) | |
8914 | { | |
8915 | struct pmu *pmu = dev_get_drvdata(dev); | |
8916 | ||
8917 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
8918 | } | |
8919 | DEVICE_ATTR_RO(nr_addr_filters); | |
8920 | ||
2e80a82a | 8921 | static struct idr pmu_idr; |
d6d020e9 | 8922 | |
abe43400 PZ |
8923 | static ssize_t |
8924 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
8925 | { | |
8926 | struct pmu *pmu = dev_get_drvdata(dev); | |
8927 | ||
8928 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
8929 | } | |
90826ca7 | 8930 | static DEVICE_ATTR_RO(type); |
abe43400 | 8931 | |
62b85639 SE |
8932 | static ssize_t |
8933 | perf_event_mux_interval_ms_show(struct device *dev, | |
8934 | struct device_attribute *attr, | |
8935 | char *page) | |
8936 | { | |
8937 | struct pmu *pmu = dev_get_drvdata(dev); | |
8938 | ||
8939 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
8940 | } | |
8941 | ||
272325c4 PZ |
8942 | static DEFINE_MUTEX(mux_interval_mutex); |
8943 | ||
62b85639 SE |
8944 | static ssize_t |
8945 | perf_event_mux_interval_ms_store(struct device *dev, | |
8946 | struct device_attribute *attr, | |
8947 | const char *buf, size_t count) | |
8948 | { | |
8949 | struct pmu *pmu = dev_get_drvdata(dev); | |
8950 | int timer, cpu, ret; | |
8951 | ||
8952 | ret = kstrtoint(buf, 0, &timer); | |
8953 | if (ret) | |
8954 | return ret; | |
8955 | ||
8956 | if (timer < 1) | |
8957 | return -EINVAL; | |
8958 | ||
8959 | /* same value, noting to do */ | |
8960 | if (timer == pmu->hrtimer_interval_ms) | |
8961 | return count; | |
8962 | ||
272325c4 | 8963 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
8964 | pmu->hrtimer_interval_ms = timer; |
8965 | ||
8966 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 8967 | cpus_read_lock(); |
272325c4 | 8968 | for_each_online_cpu(cpu) { |
62b85639 SE |
8969 | struct perf_cpu_context *cpuctx; |
8970 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8971 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
8972 | ||
272325c4 PZ |
8973 | cpu_function_call(cpu, |
8974 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 8975 | } |
a63fbed7 | 8976 | cpus_read_unlock(); |
272325c4 | 8977 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
8978 | |
8979 | return count; | |
8980 | } | |
90826ca7 | 8981 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 8982 | |
90826ca7 GKH |
8983 | static struct attribute *pmu_dev_attrs[] = { |
8984 | &dev_attr_type.attr, | |
8985 | &dev_attr_perf_event_mux_interval_ms.attr, | |
8986 | NULL, | |
abe43400 | 8987 | }; |
90826ca7 | 8988 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
8989 | |
8990 | static int pmu_bus_running; | |
8991 | static struct bus_type pmu_bus = { | |
8992 | .name = "event_source", | |
90826ca7 | 8993 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
8994 | }; |
8995 | ||
8996 | static void pmu_dev_release(struct device *dev) | |
8997 | { | |
8998 | kfree(dev); | |
8999 | } | |
9000 | ||
9001 | static int pmu_dev_alloc(struct pmu *pmu) | |
9002 | { | |
9003 | int ret = -ENOMEM; | |
9004 | ||
9005 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
9006 | if (!pmu->dev) | |
9007 | goto out; | |
9008 | ||
0c9d42ed | 9009 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
9010 | device_initialize(pmu->dev); |
9011 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
9012 | if (ret) | |
9013 | goto free_dev; | |
9014 | ||
9015 | dev_set_drvdata(pmu->dev, pmu); | |
9016 | pmu->dev->bus = &pmu_bus; | |
9017 | pmu->dev->release = pmu_dev_release; | |
9018 | ret = device_add(pmu->dev); | |
9019 | if (ret) | |
9020 | goto free_dev; | |
9021 | ||
6e855cd4 AS |
9022 | /* For PMUs with address filters, throw in an extra attribute: */ |
9023 | if (pmu->nr_addr_filters) | |
9024 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9025 | ||
9026 | if (ret) | |
9027 | goto del_dev; | |
9028 | ||
abe43400 PZ |
9029 | out: |
9030 | return ret; | |
9031 | ||
6e855cd4 AS |
9032 | del_dev: |
9033 | device_del(pmu->dev); | |
9034 | ||
abe43400 PZ |
9035 | free_dev: |
9036 | put_device(pmu->dev); | |
9037 | goto out; | |
9038 | } | |
9039 | ||
547e9fd7 | 9040 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 9041 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 9042 | |
03d8e80b | 9043 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 9044 | { |
108b02cf | 9045 | int cpu, ret; |
24f1e32c | 9046 | |
b0a873eb | 9047 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
9048 | ret = -ENOMEM; |
9049 | pmu->pmu_disable_count = alloc_percpu(int); | |
9050 | if (!pmu->pmu_disable_count) | |
9051 | goto unlock; | |
f29ac756 | 9052 | |
2e80a82a PZ |
9053 | pmu->type = -1; |
9054 | if (!name) | |
9055 | goto skip_type; | |
9056 | pmu->name = name; | |
9057 | ||
9058 | if (type < 0) { | |
0e9c3be2 TH |
9059 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
9060 | if (type < 0) { | |
9061 | ret = type; | |
2e80a82a PZ |
9062 | goto free_pdc; |
9063 | } | |
9064 | } | |
9065 | pmu->type = type; | |
9066 | ||
abe43400 PZ |
9067 | if (pmu_bus_running) { |
9068 | ret = pmu_dev_alloc(pmu); | |
9069 | if (ret) | |
9070 | goto free_idr; | |
9071 | } | |
9072 | ||
2e80a82a | 9073 | skip_type: |
26657848 PZ |
9074 | if (pmu->task_ctx_nr == perf_hw_context) { |
9075 | static int hw_context_taken = 0; | |
9076 | ||
5101ef20 MR |
9077 | /* |
9078 | * Other than systems with heterogeneous CPUs, it never makes | |
9079 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
9080 | * uncore must use perf_invalid_context. | |
9081 | */ | |
9082 | if (WARN_ON_ONCE(hw_context_taken && | |
9083 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
9084 | pmu->task_ctx_nr = perf_invalid_context; |
9085 | ||
9086 | hw_context_taken = 1; | |
9087 | } | |
9088 | ||
8dc85d54 PZ |
9089 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
9090 | if (pmu->pmu_cpu_context) | |
9091 | goto got_cpu_context; | |
f29ac756 | 9092 | |
c4814202 | 9093 | ret = -ENOMEM; |
108b02cf PZ |
9094 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
9095 | if (!pmu->pmu_cpu_context) | |
abe43400 | 9096 | goto free_dev; |
f344011c | 9097 | |
108b02cf PZ |
9098 | for_each_possible_cpu(cpu) { |
9099 | struct perf_cpu_context *cpuctx; | |
9100 | ||
9101 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 9102 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 9103 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 9104 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 9105 | cpuctx->ctx.pmu = pmu; |
a63fbed7 | 9106 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); |
9e630205 | 9107 | |
272325c4 | 9108 | __perf_mux_hrtimer_init(cpuctx, cpu); |
108b02cf | 9109 | } |
76e1d904 | 9110 | |
8dc85d54 | 9111 | got_cpu_context: |
ad5133b7 PZ |
9112 | if (!pmu->start_txn) { |
9113 | if (pmu->pmu_enable) { | |
9114 | /* | |
9115 | * If we have pmu_enable/pmu_disable calls, install | |
9116 | * transaction stubs that use that to try and batch | |
9117 | * hardware accesses. | |
9118 | */ | |
9119 | pmu->start_txn = perf_pmu_start_txn; | |
9120 | pmu->commit_txn = perf_pmu_commit_txn; | |
9121 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
9122 | } else { | |
fbbe0701 | 9123 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
9124 | pmu->commit_txn = perf_pmu_nop_int; |
9125 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 9126 | } |
5c92d124 | 9127 | } |
15dbf27c | 9128 | |
ad5133b7 PZ |
9129 | if (!pmu->pmu_enable) { |
9130 | pmu->pmu_enable = perf_pmu_nop_void; | |
9131 | pmu->pmu_disable = perf_pmu_nop_void; | |
9132 | } | |
9133 | ||
35edc2a5 PZ |
9134 | if (!pmu->event_idx) |
9135 | pmu->event_idx = perf_event_idx_default; | |
9136 | ||
b0a873eb | 9137 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 9138 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
9139 | ret = 0; |
9140 | unlock: | |
b0a873eb PZ |
9141 | mutex_unlock(&pmus_lock); |
9142 | ||
33696fc0 | 9143 | return ret; |
108b02cf | 9144 | |
abe43400 PZ |
9145 | free_dev: |
9146 | device_del(pmu->dev); | |
9147 | put_device(pmu->dev); | |
9148 | ||
2e80a82a PZ |
9149 | free_idr: |
9150 | if (pmu->type >= PERF_TYPE_MAX) | |
9151 | idr_remove(&pmu_idr, pmu->type); | |
9152 | ||
108b02cf PZ |
9153 | free_pdc: |
9154 | free_percpu(pmu->pmu_disable_count); | |
9155 | goto unlock; | |
f29ac756 | 9156 | } |
c464c76e | 9157 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 9158 | |
b0a873eb | 9159 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 9160 | { |
0933840a JO |
9161 | int remove_device; |
9162 | ||
b0a873eb | 9163 | mutex_lock(&pmus_lock); |
0933840a | 9164 | remove_device = pmu_bus_running; |
b0a873eb PZ |
9165 | list_del_rcu(&pmu->entry); |
9166 | mutex_unlock(&pmus_lock); | |
5c92d124 | 9167 | |
0475f9ea | 9168 | /* |
cde8e884 PZ |
9169 | * We dereference the pmu list under both SRCU and regular RCU, so |
9170 | * synchronize against both of those. | |
0475f9ea | 9171 | */ |
b0a873eb | 9172 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 9173 | synchronize_rcu(); |
d6d020e9 | 9174 | |
33696fc0 | 9175 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
9176 | if (pmu->type >= PERF_TYPE_MAX) |
9177 | idr_remove(&pmu_idr, pmu->type); | |
0933840a JO |
9178 | if (remove_device) { |
9179 | if (pmu->nr_addr_filters) | |
9180 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9181 | device_del(pmu->dev); | |
9182 | put_device(pmu->dev); | |
9183 | } | |
51676957 | 9184 | free_pmu_context(pmu); |
b0a873eb | 9185 | } |
c464c76e | 9186 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 9187 | |
cc34b98b MR |
9188 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
9189 | { | |
ccd41c86 | 9190 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
9191 | int ret; |
9192 | ||
9193 | if (!try_module_get(pmu->module)) | |
9194 | return -ENODEV; | |
ccd41c86 PZ |
9195 | |
9196 | if (event->group_leader != event) { | |
8b10c5e2 PZ |
9197 | /* |
9198 | * This ctx->mutex can nest when we're called through | |
9199 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
9200 | */ | |
9201 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
9202 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
9203 | BUG_ON(!ctx); |
9204 | } | |
9205 | ||
cc34b98b MR |
9206 | event->pmu = pmu; |
9207 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
9208 | |
9209 | if (ctx) | |
9210 | perf_event_ctx_unlock(event->group_leader, ctx); | |
9211 | ||
cc34b98b MR |
9212 | if (ret) |
9213 | module_put(pmu->module); | |
9214 | ||
9215 | return ret; | |
9216 | } | |
9217 | ||
18ab2cd3 | 9218 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 9219 | { |
85c617ab | 9220 | struct pmu *pmu; |
b0a873eb | 9221 | int idx; |
940c5b29 | 9222 | int ret; |
b0a873eb PZ |
9223 | |
9224 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 9225 | |
40999312 KL |
9226 | /* Try parent's PMU first: */ |
9227 | if (event->parent && event->parent->pmu) { | |
9228 | pmu = event->parent->pmu; | |
9229 | ret = perf_try_init_event(pmu, event); | |
9230 | if (!ret) | |
9231 | goto unlock; | |
9232 | } | |
9233 | ||
2e80a82a PZ |
9234 | rcu_read_lock(); |
9235 | pmu = idr_find(&pmu_idr, event->attr.type); | |
9236 | rcu_read_unlock(); | |
940c5b29 | 9237 | if (pmu) { |
cc34b98b | 9238 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
9239 | if (ret) |
9240 | pmu = ERR_PTR(ret); | |
2e80a82a | 9241 | goto unlock; |
940c5b29 | 9242 | } |
2e80a82a | 9243 | |
b0a873eb | 9244 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 9245 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 9246 | if (!ret) |
e5f4d339 | 9247 | goto unlock; |
76e1d904 | 9248 | |
b0a873eb PZ |
9249 | if (ret != -ENOENT) { |
9250 | pmu = ERR_PTR(ret); | |
e5f4d339 | 9251 | goto unlock; |
f344011c | 9252 | } |
5c92d124 | 9253 | } |
e5f4d339 PZ |
9254 | pmu = ERR_PTR(-ENOENT); |
9255 | unlock: | |
b0a873eb | 9256 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 9257 | |
4aeb0b42 | 9258 | return pmu; |
5c92d124 IM |
9259 | } |
9260 | ||
f2fb6bef KL |
9261 | static void attach_sb_event(struct perf_event *event) |
9262 | { | |
9263 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
9264 | ||
9265 | raw_spin_lock(&pel->lock); | |
9266 | list_add_rcu(&event->sb_list, &pel->list); | |
9267 | raw_spin_unlock(&pel->lock); | |
9268 | } | |
9269 | ||
aab5b71e PZ |
9270 | /* |
9271 | * We keep a list of all !task (and therefore per-cpu) events | |
9272 | * that need to receive side-band records. | |
9273 | * | |
9274 | * This avoids having to scan all the various PMU per-cpu contexts | |
9275 | * looking for them. | |
9276 | */ | |
f2fb6bef KL |
9277 | static void account_pmu_sb_event(struct perf_event *event) |
9278 | { | |
a4f144eb | 9279 | if (is_sb_event(event)) |
f2fb6bef KL |
9280 | attach_sb_event(event); |
9281 | } | |
9282 | ||
4beb31f3 FW |
9283 | static void account_event_cpu(struct perf_event *event, int cpu) |
9284 | { | |
9285 | if (event->parent) | |
9286 | return; | |
9287 | ||
4beb31f3 FW |
9288 | if (is_cgroup_event(event)) |
9289 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
9290 | } | |
9291 | ||
555e0c1e FW |
9292 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
9293 | static void account_freq_event_nohz(void) | |
9294 | { | |
9295 | #ifdef CONFIG_NO_HZ_FULL | |
9296 | /* Lock so we don't race with concurrent unaccount */ | |
9297 | spin_lock(&nr_freq_lock); | |
9298 | if (atomic_inc_return(&nr_freq_events) == 1) | |
9299 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
9300 | spin_unlock(&nr_freq_lock); | |
9301 | #endif | |
9302 | } | |
9303 | ||
9304 | static void account_freq_event(void) | |
9305 | { | |
9306 | if (tick_nohz_full_enabled()) | |
9307 | account_freq_event_nohz(); | |
9308 | else | |
9309 | atomic_inc(&nr_freq_events); | |
9310 | } | |
9311 | ||
9312 | ||
766d6c07 FW |
9313 | static void account_event(struct perf_event *event) |
9314 | { | |
25432ae9 PZ |
9315 | bool inc = false; |
9316 | ||
4beb31f3 FW |
9317 | if (event->parent) |
9318 | return; | |
9319 | ||
766d6c07 | 9320 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 9321 | inc = true; |
766d6c07 FW |
9322 | if (event->attr.mmap || event->attr.mmap_data) |
9323 | atomic_inc(&nr_mmap_events); | |
9324 | if (event->attr.comm) | |
9325 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
9326 | if (event->attr.namespaces) |
9327 | atomic_inc(&nr_namespaces_events); | |
766d6c07 FW |
9328 | if (event->attr.task) |
9329 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
9330 | if (event->attr.freq) |
9331 | account_freq_event(); | |
45ac1403 AH |
9332 | if (event->attr.context_switch) { |
9333 | atomic_inc(&nr_switch_events); | |
25432ae9 | 9334 | inc = true; |
45ac1403 | 9335 | } |
4beb31f3 | 9336 | if (has_branch_stack(event)) |
25432ae9 | 9337 | inc = true; |
4beb31f3 | 9338 | if (is_cgroup_event(event)) |
25432ae9 PZ |
9339 | inc = true; |
9340 | ||
9107c89e PZ |
9341 | if (inc) { |
9342 | if (atomic_inc_not_zero(&perf_sched_count)) | |
9343 | goto enabled; | |
9344 | ||
9345 | mutex_lock(&perf_sched_mutex); | |
9346 | if (!atomic_read(&perf_sched_count)) { | |
9347 | static_branch_enable(&perf_sched_events); | |
9348 | /* | |
9349 | * Guarantee that all CPUs observe they key change and | |
9350 | * call the perf scheduling hooks before proceeding to | |
9351 | * install events that need them. | |
9352 | */ | |
9353 | synchronize_sched(); | |
9354 | } | |
9355 | /* | |
9356 | * Now that we have waited for the sync_sched(), allow further | |
9357 | * increments to by-pass the mutex. | |
9358 | */ | |
9359 | atomic_inc(&perf_sched_count); | |
9360 | mutex_unlock(&perf_sched_mutex); | |
9361 | } | |
9362 | enabled: | |
4beb31f3 FW |
9363 | |
9364 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
9365 | |
9366 | account_pmu_sb_event(event); | |
766d6c07 FW |
9367 | } |
9368 | ||
0793a61d | 9369 | /* |
cdd6c482 | 9370 | * Allocate and initialize a event structure |
0793a61d | 9371 | */ |
cdd6c482 | 9372 | static struct perf_event * |
c3f00c70 | 9373 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
9374 | struct task_struct *task, |
9375 | struct perf_event *group_leader, | |
9376 | struct perf_event *parent_event, | |
4dc0da86 | 9377 | perf_overflow_handler_t overflow_handler, |
79dff51e | 9378 | void *context, int cgroup_fd) |
0793a61d | 9379 | { |
51b0fe39 | 9380 | struct pmu *pmu; |
cdd6c482 IM |
9381 | struct perf_event *event; |
9382 | struct hw_perf_event *hwc; | |
90983b16 | 9383 | long err = -EINVAL; |
0793a61d | 9384 | |
66832eb4 ON |
9385 | if ((unsigned)cpu >= nr_cpu_ids) { |
9386 | if (!task || cpu != -1) | |
9387 | return ERR_PTR(-EINVAL); | |
9388 | } | |
9389 | ||
c3f00c70 | 9390 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 9391 | if (!event) |
d5d2bc0d | 9392 | return ERR_PTR(-ENOMEM); |
0793a61d | 9393 | |
04289bb9 | 9394 | /* |
cdd6c482 | 9395 | * Single events are their own group leaders, with an |
04289bb9 IM |
9396 | * empty sibling list: |
9397 | */ | |
9398 | if (!group_leader) | |
cdd6c482 | 9399 | group_leader = event; |
04289bb9 | 9400 | |
cdd6c482 IM |
9401 | mutex_init(&event->child_mutex); |
9402 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 9403 | |
cdd6c482 IM |
9404 | INIT_LIST_HEAD(&event->group_entry); |
9405 | INIT_LIST_HEAD(&event->event_entry); | |
9406 | INIT_LIST_HEAD(&event->sibling_list); | |
10c6db11 | 9407 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 9408 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 9409 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
9410 | INIT_HLIST_NODE(&event->hlist_entry); |
9411 | ||
10c6db11 | 9412 | |
cdd6c482 | 9413 | init_waitqueue_head(&event->waitq); |
e360adbe | 9414 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 9415 | |
cdd6c482 | 9416 | mutex_init(&event->mmap_mutex); |
375637bc | 9417 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 9418 | |
a6fa941d | 9419 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
9420 | event->cpu = cpu; |
9421 | event->attr = *attr; | |
9422 | event->group_leader = group_leader; | |
9423 | event->pmu = NULL; | |
cdd6c482 | 9424 | event->oncpu = -1; |
a96bbc16 | 9425 | |
cdd6c482 | 9426 | event->parent = parent_event; |
b84fbc9f | 9427 | |
17cf22c3 | 9428 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 9429 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 9430 | |
cdd6c482 | 9431 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 9432 | |
d580ff86 PZ |
9433 | if (task) { |
9434 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 9435 | /* |
50f16a8b PZ |
9436 | * XXX pmu::event_init needs to know what task to account to |
9437 | * and we cannot use the ctx information because we need the | |
9438 | * pmu before we get a ctx. | |
d580ff86 | 9439 | */ |
50f16a8b | 9440 | event->hw.target = task; |
d580ff86 PZ |
9441 | } |
9442 | ||
34f43927 PZ |
9443 | event->clock = &local_clock; |
9444 | if (parent_event) | |
9445 | event->clock = parent_event->clock; | |
9446 | ||
4dc0da86 | 9447 | if (!overflow_handler && parent_event) { |
b326e956 | 9448 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 9449 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 9450 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c AS |
9451 | if (overflow_handler == bpf_overflow_handler) { |
9452 | struct bpf_prog *prog = bpf_prog_inc(parent_event->prog); | |
9453 | ||
9454 | if (IS_ERR(prog)) { | |
9455 | err = PTR_ERR(prog); | |
9456 | goto err_ns; | |
9457 | } | |
9458 | event->prog = prog; | |
9459 | event->orig_overflow_handler = | |
9460 | parent_event->orig_overflow_handler; | |
9461 | } | |
9462 | #endif | |
4dc0da86 | 9463 | } |
66832eb4 | 9464 | |
1879445d WN |
9465 | if (overflow_handler) { |
9466 | event->overflow_handler = overflow_handler; | |
9467 | event->overflow_handler_context = context; | |
9ecda41a WN |
9468 | } else if (is_write_backward(event)){ |
9469 | event->overflow_handler = perf_event_output_backward; | |
9470 | event->overflow_handler_context = NULL; | |
1879445d | 9471 | } else { |
9ecda41a | 9472 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
9473 | event->overflow_handler_context = NULL; |
9474 | } | |
97eaf530 | 9475 | |
0231bb53 | 9476 | perf_event__state_init(event); |
a86ed508 | 9477 | |
4aeb0b42 | 9478 | pmu = NULL; |
b8e83514 | 9479 | |
cdd6c482 | 9480 | hwc = &event->hw; |
bd2b5b12 | 9481 | hwc->sample_period = attr->sample_period; |
0d48696f | 9482 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 9483 | hwc->sample_period = 1; |
eced1dfc | 9484 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 9485 | |
e7850595 | 9486 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 9487 | |
2023b359 | 9488 | /* |
ba5213ae PZ |
9489 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
9490 | * See perf_output_read(). | |
2023b359 | 9491 | */ |
ba5213ae | 9492 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 9493 | goto err_ns; |
a46a2300 YZ |
9494 | |
9495 | if (!has_branch_stack(event)) | |
9496 | event->attr.branch_sample_type = 0; | |
2023b359 | 9497 | |
79dff51e MF |
9498 | if (cgroup_fd != -1) { |
9499 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
9500 | if (err) | |
9501 | goto err_ns; | |
9502 | } | |
9503 | ||
b0a873eb | 9504 | pmu = perf_init_event(event); |
85c617ab | 9505 | if (IS_ERR(pmu)) { |
4aeb0b42 | 9506 | err = PTR_ERR(pmu); |
90983b16 | 9507 | goto err_ns; |
621a01ea | 9508 | } |
d5d2bc0d | 9509 | |
bed5b25a AS |
9510 | err = exclusive_event_init(event); |
9511 | if (err) | |
9512 | goto err_pmu; | |
9513 | ||
375637bc AS |
9514 | if (has_addr_filter(event)) { |
9515 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, | |
9516 | sizeof(unsigned long), | |
9517 | GFP_KERNEL); | |
36cc2b92 DC |
9518 | if (!event->addr_filters_offs) { |
9519 | err = -ENOMEM; | |
375637bc | 9520 | goto err_per_task; |
36cc2b92 | 9521 | } |
375637bc AS |
9522 | |
9523 | /* force hw sync on the address filters */ | |
9524 | event->addr_filters_gen = 1; | |
9525 | } | |
9526 | ||
cdd6c482 | 9527 | if (!event->parent) { |
927c7a9e | 9528 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 9529 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 9530 | if (err) |
375637bc | 9531 | goto err_addr_filters; |
d010b332 | 9532 | } |
f344011c | 9533 | } |
9ee318a7 | 9534 | |
927a5570 AS |
9535 | /* symmetric to unaccount_event() in _free_event() */ |
9536 | account_event(event); | |
9537 | ||
cdd6c482 | 9538 | return event; |
90983b16 | 9539 | |
375637bc AS |
9540 | err_addr_filters: |
9541 | kfree(event->addr_filters_offs); | |
9542 | ||
bed5b25a AS |
9543 | err_per_task: |
9544 | exclusive_event_destroy(event); | |
9545 | ||
90983b16 FW |
9546 | err_pmu: |
9547 | if (event->destroy) | |
9548 | event->destroy(event); | |
c464c76e | 9549 | module_put(pmu->module); |
90983b16 | 9550 | err_ns: |
79dff51e MF |
9551 | if (is_cgroup_event(event)) |
9552 | perf_detach_cgroup(event); | |
90983b16 FW |
9553 | if (event->ns) |
9554 | put_pid_ns(event->ns); | |
9555 | kfree(event); | |
9556 | ||
9557 | return ERR_PTR(err); | |
0793a61d TG |
9558 | } |
9559 | ||
cdd6c482 IM |
9560 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
9561 | struct perf_event_attr *attr) | |
974802ea | 9562 | { |
974802ea | 9563 | u32 size; |
cdf8073d | 9564 | int ret; |
974802ea PZ |
9565 | |
9566 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
9567 | return -EFAULT; | |
9568 | ||
9569 | /* | |
9570 | * zero the full structure, so that a short copy will be nice. | |
9571 | */ | |
9572 | memset(attr, 0, sizeof(*attr)); | |
9573 | ||
9574 | ret = get_user(size, &uattr->size); | |
9575 | if (ret) | |
9576 | return ret; | |
9577 | ||
9578 | if (size > PAGE_SIZE) /* silly large */ | |
9579 | goto err_size; | |
9580 | ||
9581 | if (!size) /* abi compat */ | |
9582 | size = PERF_ATTR_SIZE_VER0; | |
9583 | ||
9584 | if (size < PERF_ATTR_SIZE_VER0) | |
9585 | goto err_size; | |
9586 | ||
9587 | /* | |
9588 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
9589 | * ensure all the unknown bits are 0 - i.e. new |
9590 | * user-space does not rely on any kernel feature | |
9591 | * extensions we dont know about yet. | |
974802ea PZ |
9592 | */ |
9593 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
9594 | unsigned char __user *addr; |
9595 | unsigned char __user *end; | |
9596 | unsigned char val; | |
974802ea | 9597 | |
cdf8073d IS |
9598 | addr = (void __user *)uattr + sizeof(*attr); |
9599 | end = (void __user *)uattr + size; | |
974802ea | 9600 | |
cdf8073d | 9601 | for (; addr < end; addr++) { |
974802ea PZ |
9602 | ret = get_user(val, addr); |
9603 | if (ret) | |
9604 | return ret; | |
9605 | if (val) | |
9606 | goto err_size; | |
9607 | } | |
b3e62e35 | 9608 | size = sizeof(*attr); |
974802ea PZ |
9609 | } |
9610 | ||
9611 | ret = copy_from_user(attr, uattr, size); | |
9612 | if (ret) | |
9613 | return -EFAULT; | |
9614 | ||
f12f42ac MX |
9615 | attr->size = size; |
9616 | ||
cd757645 | 9617 | if (attr->__reserved_1) |
974802ea PZ |
9618 | return -EINVAL; |
9619 | ||
9620 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
9621 | return -EINVAL; | |
9622 | ||
9623 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
9624 | return -EINVAL; | |
9625 | ||
bce38cd5 SE |
9626 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
9627 | u64 mask = attr->branch_sample_type; | |
9628 | ||
9629 | /* only using defined bits */ | |
9630 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
9631 | return -EINVAL; | |
9632 | ||
9633 | /* at least one branch bit must be set */ | |
9634 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
9635 | return -EINVAL; | |
9636 | ||
bce38cd5 SE |
9637 | /* propagate priv level, when not set for branch */ |
9638 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
9639 | ||
9640 | /* exclude_kernel checked on syscall entry */ | |
9641 | if (!attr->exclude_kernel) | |
9642 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
9643 | ||
9644 | if (!attr->exclude_user) | |
9645 | mask |= PERF_SAMPLE_BRANCH_USER; | |
9646 | ||
9647 | if (!attr->exclude_hv) | |
9648 | mask |= PERF_SAMPLE_BRANCH_HV; | |
9649 | /* | |
9650 | * adjust user setting (for HW filter setup) | |
9651 | */ | |
9652 | attr->branch_sample_type = mask; | |
9653 | } | |
e712209a SE |
9654 | /* privileged levels capture (kernel, hv): check permissions */ |
9655 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
9656 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
9657 | return -EACCES; | |
bce38cd5 | 9658 | } |
4018994f | 9659 | |
c5ebcedb | 9660 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 9661 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
9662 | if (ret) |
9663 | return ret; | |
9664 | } | |
9665 | ||
9666 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
9667 | if (!arch_perf_have_user_stack_dump()) | |
9668 | return -ENOSYS; | |
9669 | ||
9670 | /* | |
9671 | * We have __u32 type for the size, but so far | |
9672 | * we can only use __u16 as maximum due to the | |
9673 | * __u16 sample size limit. | |
9674 | */ | |
9675 | if (attr->sample_stack_user >= USHRT_MAX) | |
9676 | ret = -EINVAL; | |
9677 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) | |
9678 | ret = -EINVAL; | |
9679 | } | |
4018994f | 9680 | |
60e2364e SE |
9681 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
9682 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
9683 | out: |
9684 | return ret; | |
9685 | ||
9686 | err_size: | |
9687 | put_user(sizeof(*attr), &uattr->size); | |
9688 | ret = -E2BIG; | |
9689 | goto out; | |
9690 | } | |
9691 | ||
ac9721f3 PZ |
9692 | static int |
9693 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 9694 | { |
b69cf536 | 9695 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
9696 | int ret = -EINVAL; |
9697 | ||
ac9721f3 | 9698 | if (!output_event) |
a4be7c27 PZ |
9699 | goto set; |
9700 | ||
ac9721f3 PZ |
9701 | /* don't allow circular references */ |
9702 | if (event == output_event) | |
a4be7c27 PZ |
9703 | goto out; |
9704 | ||
0f139300 PZ |
9705 | /* |
9706 | * Don't allow cross-cpu buffers | |
9707 | */ | |
9708 | if (output_event->cpu != event->cpu) | |
9709 | goto out; | |
9710 | ||
9711 | /* | |
76369139 | 9712 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
9713 | */ |
9714 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
9715 | goto out; | |
9716 | ||
34f43927 PZ |
9717 | /* |
9718 | * Mixing clocks in the same buffer is trouble you don't need. | |
9719 | */ | |
9720 | if (output_event->clock != event->clock) | |
9721 | goto out; | |
9722 | ||
9ecda41a WN |
9723 | /* |
9724 | * Either writing ring buffer from beginning or from end. | |
9725 | * Mixing is not allowed. | |
9726 | */ | |
9727 | if (is_write_backward(output_event) != is_write_backward(event)) | |
9728 | goto out; | |
9729 | ||
45bfb2e5 PZ |
9730 | /* |
9731 | * If both events generate aux data, they must be on the same PMU | |
9732 | */ | |
9733 | if (has_aux(event) && has_aux(output_event) && | |
9734 | event->pmu != output_event->pmu) | |
9735 | goto out; | |
9736 | ||
a4be7c27 | 9737 | set: |
cdd6c482 | 9738 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
9739 | /* Can't redirect output if we've got an active mmap() */ |
9740 | if (atomic_read(&event->mmap_count)) | |
9741 | goto unlock; | |
a4be7c27 | 9742 | |
ac9721f3 | 9743 | if (output_event) { |
76369139 FW |
9744 | /* get the rb we want to redirect to */ |
9745 | rb = ring_buffer_get(output_event); | |
9746 | if (!rb) | |
ac9721f3 | 9747 | goto unlock; |
a4be7c27 PZ |
9748 | } |
9749 | ||
b69cf536 | 9750 | ring_buffer_attach(event, rb); |
9bb5d40c | 9751 | |
a4be7c27 | 9752 | ret = 0; |
ac9721f3 PZ |
9753 | unlock: |
9754 | mutex_unlock(&event->mmap_mutex); | |
9755 | ||
a4be7c27 | 9756 | out: |
a4be7c27 PZ |
9757 | return ret; |
9758 | } | |
9759 | ||
f63a8daa PZ |
9760 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
9761 | { | |
9762 | if (b < a) | |
9763 | swap(a, b); | |
9764 | ||
9765 | mutex_lock(a); | |
9766 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
9767 | } | |
9768 | ||
34f43927 PZ |
9769 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
9770 | { | |
9771 | bool nmi_safe = false; | |
9772 | ||
9773 | switch (clk_id) { | |
9774 | case CLOCK_MONOTONIC: | |
9775 | event->clock = &ktime_get_mono_fast_ns; | |
9776 | nmi_safe = true; | |
9777 | break; | |
9778 | ||
9779 | case CLOCK_MONOTONIC_RAW: | |
9780 | event->clock = &ktime_get_raw_fast_ns; | |
9781 | nmi_safe = true; | |
9782 | break; | |
9783 | ||
9784 | case CLOCK_REALTIME: | |
9785 | event->clock = &ktime_get_real_ns; | |
9786 | break; | |
9787 | ||
9788 | case CLOCK_BOOTTIME: | |
9789 | event->clock = &ktime_get_boot_ns; | |
9790 | break; | |
9791 | ||
9792 | case CLOCK_TAI: | |
9793 | event->clock = &ktime_get_tai_ns; | |
9794 | break; | |
9795 | ||
9796 | default: | |
9797 | return -EINVAL; | |
9798 | } | |
9799 | ||
9800 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
9801 | return -EINVAL; | |
9802 | ||
9803 | return 0; | |
9804 | } | |
9805 | ||
321027c1 PZ |
9806 | /* |
9807 | * Variation on perf_event_ctx_lock_nested(), except we take two context | |
9808 | * mutexes. | |
9809 | */ | |
9810 | static struct perf_event_context * | |
9811 | __perf_event_ctx_lock_double(struct perf_event *group_leader, | |
9812 | struct perf_event_context *ctx) | |
9813 | { | |
9814 | struct perf_event_context *gctx; | |
9815 | ||
9816 | again: | |
9817 | rcu_read_lock(); | |
9818 | gctx = READ_ONCE(group_leader->ctx); | |
9819 | if (!atomic_inc_not_zero(&gctx->refcount)) { | |
9820 | rcu_read_unlock(); | |
9821 | goto again; | |
9822 | } | |
9823 | rcu_read_unlock(); | |
9824 | ||
9825 | mutex_lock_double(&gctx->mutex, &ctx->mutex); | |
9826 | ||
9827 | if (group_leader->ctx != gctx) { | |
9828 | mutex_unlock(&ctx->mutex); | |
9829 | mutex_unlock(&gctx->mutex); | |
9830 | put_ctx(gctx); | |
9831 | goto again; | |
9832 | } | |
9833 | ||
9834 | return gctx; | |
9835 | } | |
9836 | ||
0793a61d | 9837 | /** |
cdd6c482 | 9838 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 9839 | * |
cdd6c482 | 9840 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 9841 | * @pid: target pid |
9f66a381 | 9842 | * @cpu: target cpu |
cdd6c482 | 9843 | * @group_fd: group leader event fd |
0793a61d | 9844 | */ |
cdd6c482 IM |
9845 | SYSCALL_DEFINE5(perf_event_open, |
9846 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 9847 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 9848 | { |
b04243ef PZ |
9849 | struct perf_event *group_leader = NULL, *output_event = NULL; |
9850 | struct perf_event *event, *sibling; | |
cdd6c482 | 9851 | struct perf_event_attr attr; |
f63a8daa | 9852 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 9853 | struct file *event_file = NULL; |
2903ff01 | 9854 | struct fd group = {NULL, 0}; |
38a81da2 | 9855 | struct task_struct *task = NULL; |
89a1e187 | 9856 | struct pmu *pmu; |
ea635c64 | 9857 | int event_fd; |
b04243ef | 9858 | int move_group = 0; |
dc86cabe | 9859 | int err; |
a21b0b35 | 9860 | int f_flags = O_RDWR; |
79dff51e | 9861 | int cgroup_fd = -1; |
0793a61d | 9862 | |
2743a5b0 | 9863 | /* for future expandability... */ |
e5d1367f | 9864 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
9865 | return -EINVAL; |
9866 | ||
cc58fdf5 BH |
9867 | if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN)) |
9868 | return -EACCES; | |
9869 | ||
dc86cabe IM |
9870 | err = perf_copy_attr(attr_uptr, &attr); |
9871 | if (err) | |
9872 | return err; | |
eab656ae | 9873 | |
0764771d PZ |
9874 | if (!attr.exclude_kernel) { |
9875 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
9876 | return -EACCES; | |
9877 | } | |
9878 | ||
e4222673 HB |
9879 | if (attr.namespaces) { |
9880 | if (!capable(CAP_SYS_ADMIN)) | |
9881 | return -EACCES; | |
9882 | } | |
9883 | ||
df58ab24 | 9884 | if (attr.freq) { |
cdd6c482 | 9885 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 9886 | return -EINVAL; |
0819b2e3 PZ |
9887 | } else { |
9888 | if (attr.sample_period & (1ULL << 63)) | |
9889 | return -EINVAL; | |
df58ab24 PZ |
9890 | } |
9891 | ||
97c79a38 ACM |
9892 | if (!attr.sample_max_stack) |
9893 | attr.sample_max_stack = sysctl_perf_event_max_stack; | |
9894 | ||
e5d1367f SE |
9895 | /* |
9896 | * In cgroup mode, the pid argument is used to pass the fd | |
9897 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
9898 | * designates the cpu on which to monitor threads from that | |
9899 | * cgroup. | |
9900 | */ | |
9901 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
9902 | return -EINVAL; | |
9903 | ||
a21b0b35 YD |
9904 | if (flags & PERF_FLAG_FD_CLOEXEC) |
9905 | f_flags |= O_CLOEXEC; | |
9906 | ||
9907 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
9908 | if (event_fd < 0) |
9909 | return event_fd; | |
9910 | ||
ac9721f3 | 9911 | if (group_fd != -1) { |
2903ff01 AV |
9912 | err = perf_fget_light(group_fd, &group); |
9913 | if (err) | |
d14b12d7 | 9914 | goto err_fd; |
2903ff01 | 9915 | group_leader = group.file->private_data; |
ac9721f3 PZ |
9916 | if (flags & PERF_FLAG_FD_OUTPUT) |
9917 | output_event = group_leader; | |
9918 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
9919 | group_leader = NULL; | |
9920 | } | |
9921 | ||
e5d1367f | 9922 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
9923 | task = find_lively_task_by_vpid(pid); |
9924 | if (IS_ERR(task)) { | |
9925 | err = PTR_ERR(task); | |
9926 | goto err_group_fd; | |
9927 | } | |
9928 | } | |
9929 | ||
1f4ee503 PZ |
9930 | if (task && group_leader && |
9931 | group_leader->attr.inherit != attr.inherit) { | |
9932 | err = -EINVAL; | |
9933 | goto err_task; | |
9934 | } | |
9935 | ||
79c9ce57 PZ |
9936 | if (task) { |
9937 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); | |
9938 | if (err) | |
e5aeee51 | 9939 | goto err_task; |
79c9ce57 PZ |
9940 | |
9941 | /* | |
9942 | * Reuse ptrace permission checks for now. | |
9943 | * | |
9944 | * We must hold cred_guard_mutex across this and any potential | |
9945 | * perf_install_in_context() call for this new event to | |
9946 | * serialize against exec() altering our credentials (and the | |
9947 | * perf_event_exit_task() that could imply). | |
9948 | */ | |
9949 | err = -EACCES; | |
9950 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
9951 | goto err_cred; | |
9952 | } | |
9953 | ||
79dff51e MF |
9954 | if (flags & PERF_FLAG_PID_CGROUP) |
9955 | cgroup_fd = pid; | |
9956 | ||
4dc0da86 | 9957 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 9958 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
9959 | if (IS_ERR(event)) { |
9960 | err = PTR_ERR(event); | |
79c9ce57 | 9961 | goto err_cred; |
d14b12d7 SE |
9962 | } |
9963 | ||
53b25335 VW |
9964 | if (is_sampling_event(event)) { |
9965 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 9966 | err = -EOPNOTSUPP; |
53b25335 VW |
9967 | goto err_alloc; |
9968 | } | |
9969 | } | |
9970 | ||
89a1e187 PZ |
9971 | /* |
9972 | * Special case software events and allow them to be part of | |
9973 | * any hardware group. | |
9974 | */ | |
9975 | pmu = event->pmu; | |
b04243ef | 9976 | |
34f43927 PZ |
9977 | if (attr.use_clockid) { |
9978 | err = perf_event_set_clock(event, attr.clockid); | |
9979 | if (err) | |
9980 | goto err_alloc; | |
9981 | } | |
9982 | ||
4ff6a8de DCC |
9983 | if (pmu->task_ctx_nr == perf_sw_context) |
9984 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
9985 | ||
b04243ef PZ |
9986 | if (group_leader && |
9987 | (is_software_event(event) != is_software_event(group_leader))) { | |
9988 | if (is_software_event(event)) { | |
9989 | /* | |
9990 | * If event and group_leader are not both a software | |
9991 | * event, and event is, then group leader is not. | |
9992 | * | |
9993 | * Allow the addition of software events to !software | |
9994 | * groups, this is safe because software events never | |
9995 | * fail to schedule. | |
9996 | */ | |
9997 | pmu = group_leader->pmu; | |
9998 | } else if (is_software_event(group_leader) && | |
4ff6a8de | 9999 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
b04243ef PZ |
10000 | /* |
10001 | * In case the group is a pure software group, and we | |
10002 | * try to add a hardware event, move the whole group to | |
10003 | * the hardware context. | |
10004 | */ | |
10005 | move_group = 1; | |
10006 | } | |
10007 | } | |
89a1e187 PZ |
10008 | |
10009 | /* | |
10010 | * Get the target context (task or percpu): | |
10011 | */ | |
4af57ef2 | 10012 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
10013 | if (IS_ERR(ctx)) { |
10014 | err = PTR_ERR(ctx); | |
c6be5a5c | 10015 | goto err_alloc; |
89a1e187 PZ |
10016 | } |
10017 | ||
bed5b25a AS |
10018 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
10019 | err = -EBUSY; | |
10020 | goto err_context; | |
10021 | } | |
10022 | ||
ccff286d | 10023 | /* |
cdd6c482 | 10024 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 10025 | */ |
ac9721f3 | 10026 | if (group_leader) { |
dc86cabe | 10027 | err = -EINVAL; |
04289bb9 | 10028 | |
04289bb9 | 10029 | /* |
ccff286d IM |
10030 | * Do not allow a recursive hierarchy (this new sibling |
10031 | * becoming part of another group-sibling): | |
10032 | */ | |
10033 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 10034 | goto err_context; |
34f43927 PZ |
10035 | |
10036 | /* All events in a group should have the same clock */ | |
10037 | if (group_leader->clock != event->clock) | |
10038 | goto err_context; | |
10039 | ||
ccff286d | 10040 | /* |
64aee2a9 MR |
10041 | * Make sure we're both events for the same CPU; |
10042 | * grouping events for different CPUs is broken; since | |
10043 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 10044 | */ |
64aee2a9 MR |
10045 | if (group_leader->cpu != event->cpu) |
10046 | goto err_context; | |
c3c87e77 | 10047 | |
64aee2a9 MR |
10048 | /* |
10049 | * Make sure we're both on the same task, or both | |
10050 | * per-CPU events. | |
10051 | */ | |
10052 | if (group_leader->ctx->task != ctx->task) | |
10053 | goto err_context; | |
10054 | ||
10055 | /* | |
10056 | * Do not allow to attach to a group in a different task | |
10057 | * or CPU context. If we're moving SW events, we'll fix | |
10058 | * this up later, so allow that. | |
10059 | */ | |
10060 | if (!move_group && group_leader->ctx != ctx) | |
10061 | goto err_context; | |
b04243ef | 10062 | |
3b6f9e5c PM |
10063 | /* |
10064 | * Only a group leader can be exclusive or pinned | |
10065 | */ | |
0d48696f | 10066 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 10067 | goto err_context; |
ac9721f3 PZ |
10068 | } |
10069 | ||
10070 | if (output_event) { | |
10071 | err = perf_event_set_output(event, output_event); | |
10072 | if (err) | |
c3f00c70 | 10073 | goto err_context; |
ac9721f3 | 10074 | } |
0793a61d | 10075 | |
a21b0b35 YD |
10076 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
10077 | f_flags); | |
ea635c64 AV |
10078 | if (IS_ERR(event_file)) { |
10079 | err = PTR_ERR(event_file); | |
201c2f85 | 10080 | event_file = NULL; |
c3f00c70 | 10081 | goto err_context; |
ea635c64 | 10082 | } |
9b51f66d | 10083 | |
b04243ef | 10084 | if (move_group) { |
321027c1 PZ |
10085 | gctx = __perf_event_ctx_lock_double(group_leader, ctx); |
10086 | ||
84c4e620 PZ |
10087 | if (gctx->task == TASK_TOMBSTONE) { |
10088 | err = -ESRCH; | |
10089 | goto err_locked; | |
10090 | } | |
321027c1 PZ |
10091 | |
10092 | /* | |
10093 | * Check if we raced against another sys_perf_event_open() call | |
10094 | * moving the software group underneath us. | |
10095 | */ | |
10096 | if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
10097 | /* | |
10098 | * If someone moved the group out from under us, check | |
10099 | * if this new event wound up on the same ctx, if so | |
10100 | * its the regular !move_group case, otherwise fail. | |
10101 | */ | |
10102 | if (gctx != ctx) { | |
10103 | err = -EINVAL; | |
10104 | goto err_locked; | |
10105 | } else { | |
10106 | perf_event_ctx_unlock(group_leader, gctx); | |
10107 | move_group = 0; | |
10108 | } | |
10109 | } | |
f55fc2a5 PZ |
10110 | } else { |
10111 | mutex_lock(&ctx->mutex); | |
10112 | } | |
10113 | ||
84c4e620 PZ |
10114 | if (ctx->task == TASK_TOMBSTONE) { |
10115 | err = -ESRCH; | |
10116 | goto err_locked; | |
10117 | } | |
10118 | ||
a723968c PZ |
10119 | if (!perf_event_validate_size(event)) { |
10120 | err = -E2BIG; | |
10121 | goto err_locked; | |
10122 | } | |
10123 | ||
a63fbed7 TG |
10124 | if (!task) { |
10125 | /* | |
10126 | * Check if the @cpu we're creating an event for is online. | |
10127 | * | |
10128 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10129 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10130 | */ | |
10131 | struct perf_cpu_context *cpuctx = | |
10132 | container_of(ctx, struct perf_cpu_context, ctx); | |
10133 | ||
10134 | if (!cpuctx->online) { | |
10135 | err = -ENODEV; | |
10136 | goto err_locked; | |
10137 | } | |
10138 | } | |
10139 | ||
10140 | ||
f55fc2a5 PZ |
10141 | /* |
10142 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
10143 | * because we need to serialize with concurrent event creation. | |
10144 | */ | |
10145 | if (!exclusive_event_installable(event, ctx)) { | |
10146 | /* exclusive and group stuff are assumed mutually exclusive */ | |
10147 | WARN_ON_ONCE(move_group); | |
f63a8daa | 10148 | |
f55fc2a5 PZ |
10149 | err = -EBUSY; |
10150 | goto err_locked; | |
10151 | } | |
f63a8daa | 10152 | |
f55fc2a5 PZ |
10153 | WARN_ON_ONCE(ctx->parent_ctx); |
10154 | ||
79c9ce57 PZ |
10155 | /* |
10156 | * This is the point on no return; we cannot fail hereafter. This is | |
10157 | * where we start modifying current state. | |
10158 | */ | |
10159 | ||
f55fc2a5 | 10160 | if (move_group) { |
f63a8daa PZ |
10161 | /* |
10162 | * See perf_event_ctx_lock() for comments on the details | |
10163 | * of swizzling perf_event::ctx. | |
10164 | */ | |
45a0e07a | 10165 | perf_remove_from_context(group_leader, 0); |
279b5165 | 10166 | put_ctx(gctx); |
0231bb53 | 10167 | |
b04243ef PZ |
10168 | list_for_each_entry(sibling, &group_leader->sibling_list, |
10169 | group_entry) { | |
45a0e07a | 10170 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
10171 | put_ctx(gctx); |
10172 | } | |
b04243ef | 10173 | |
f63a8daa PZ |
10174 | /* |
10175 | * Wait for everybody to stop referencing the events through | |
10176 | * the old lists, before installing it on new lists. | |
10177 | */ | |
0cda4c02 | 10178 | synchronize_rcu(); |
f63a8daa | 10179 | |
8f95b435 PZI |
10180 | /* |
10181 | * Install the group siblings before the group leader. | |
10182 | * | |
10183 | * Because a group leader will try and install the entire group | |
10184 | * (through the sibling list, which is still in-tact), we can | |
10185 | * end up with siblings installed in the wrong context. | |
10186 | * | |
10187 | * By installing siblings first we NO-OP because they're not | |
10188 | * reachable through the group lists. | |
10189 | */ | |
b04243ef PZ |
10190 | list_for_each_entry(sibling, &group_leader->sibling_list, |
10191 | group_entry) { | |
8f95b435 | 10192 | perf_event__state_init(sibling); |
9fc81d87 | 10193 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
10194 | get_ctx(ctx); |
10195 | } | |
8f95b435 PZI |
10196 | |
10197 | /* | |
10198 | * Removing from the context ends up with disabled | |
10199 | * event. What we want here is event in the initial | |
10200 | * startup state, ready to be add into new context. | |
10201 | */ | |
10202 | perf_event__state_init(group_leader); | |
10203 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
10204 | get_ctx(ctx); | |
bed5b25a AS |
10205 | } |
10206 | ||
f73e22ab PZ |
10207 | /* |
10208 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
10209 | * that we're serialized against further additions and before | |
10210 | * perf_install_in_context() which is the point the event is active and | |
10211 | * can use these values. | |
10212 | */ | |
10213 | perf_event__header_size(event); | |
10214 | perf_event__id_header_size(event); | |
10215 | ||
78cd2c74 PZ |
10216 | event->owner = current; |
10217 | ||
e2d37cd2 | 10218 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 10219 | perf_unpin_context(ctx); |
f63a8daa | 10220 | |
f55fc2a5 | 10221 | if (move_group) |
321027c1 | 10222 | perf_event_ctx_unlock(group_leader, gctx); |
d859e29f | 10223 | mutex_unlock(&ctx->mutex); |
9b51f66d | 10224 | |
79c9ce57 PZ |
10225 | if (task) { |
10226 | mutex_unlock(&task->signal->cred_guard_mutex); | |
10227 | put_task_struct(task); | |
10228 | } | |
10229 | ||
cdd6c482 IM |
10230 | mutex_lock(¤t->perf_event_mutex); |
10231 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
10232 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 10233 | |
8a49542c PZ |
10234 | /* |
10235 | * Drop the reference on the group_event after placing the | |
10236 | * new event on the sibling_list. This ensures destruction | |
10237 | * of the group leader will find the pointer to itself in | |
10238 | * perf_group_detach(). | |
10239 | */ | |
2903ff01 | 10240 | fdput(group); |
ea635c64 AV |
10241 | fd_install(event_fd, event_file); |
10242 | return event_fd; | |
0793a61d | 10243 | |
f55fc2a5 PZ |
10244 | err_locked: |
10245 | if (move_group) | |
321027c1 | 10246 | perf_event_ctx_unlock(group_leader, gctx); |
f55fc2a5 PZ |
10247 | mutex_unlock(&ctx->mutex); |
10248 | /* err_file: */ | |
10249 | fput(event_file); | |
c3f00c70 | 10250 | err_context: |
fe4b04fa | 10251 | perf_unpin_context(ctx); |
ea635c64 | 10252 | put_ctx(ctx); |
c6be5a5c | 10253 | err_alloc: |
13005627 PZ |
10254 | /* |
10255 | * If event_file is set, the fput() above will have called ->release() | |
10256 | * and that will take care of freeing the event. | |
10257 | */ | |
10258 | if (!event_file) | |
10259 | free_event(event); | |
79c9ce57 PZ |
10260 | err_cred: |
10261 | if (task) | |
10262 | mutex_unlock(&task->signal->cred_guard_mutex); | |
1f4ee503 | 10263 | err_task: |
e7d0bc04 PZ |
10264 | if (task) |
10265 | put_task_struct(task); | |
89a1e187 | 10266 | err_group_fd: |
2903ff01 | 10267 | fdput(group); |
ea635c64 AV |
10268 | err_fd: |
10269 | put_unused_fd(event_fd); | |
dc86cabe | 10270 | return err; |
0793a61d TG |
10271 | } |
10272 | ||
fb0459d7 AV |
10273 | /** |
10274 | * perf_event_create_kernel_counter | |
10275 | * | |
10276 | * @attr: attributes of the counter to create | |
10277 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 10278 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
10279 | */ |
10280 | struct perf_event * | |
10281 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 10282 | struct task_struct *task, |
4dc0da86 AK |
10283 | perf_overflow_handler_t overflow_handler, |
10284 | void *context) | |
fb0459d7 | 10285 | { |
fb0459d7 | 10286 | struct perf_event_context *ctx; |
c3f00c70 | 10287 | struct perf_event *event; |
fb0459d7 | 10288 | int err; |
d859e29f | 10289 | |
fb0459d7 AV |
10290 | /* |
10291 | * Get the target context (task or percpu): | |
10292 | */ | |
d859e29f | 10293 | |
4dc0da86 | 10294 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 10295 | overflow_handler, context, -1); |
c3f00c70 PZ |
10296 | if (IS_ERR(event)) { |
10297 | err = PTR_ERR(event); | |
10298 | goto err; | |
10299 | } | |
d859e29f | 10300 | |
f8697762 | 10301 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 10302 | event->owner = TASK_TOMBSTONE; |
f8697762 | 10303 | |
4af57ef2 | 10304 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
10305 | if (IS_ERR(ctx)) { |
10306 | err = PTR_ERR(ctx); | |
c3f00c70 | 10307 | goto err_free; |
d859e29f | 10308 | } |
fb0459d7 | 10309 | |
fb0459d7 AV |
10310 | WARN_ON_ONCE(ctx->parent_ctx); |
10311 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
10312 | if (ctx->task == TASK_TOMBSTONE) { |
10313 | err = -ESRCH; | |
10314 | goto err_unlock; | |
10315 | } | |
10316 | ||
a63fbed7 TG |
10317 | if (!task) { |
10318 | /* | |
10319 | * Check if the @cpu we're creating an event for is online. | |
10320 | * | |
10321 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10322 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10323 | */ | |
10324 | struct perf_cpu_context *cpuctx = | |
10325 | container_of(ctx, struct perf_cpu_context, ctx); | |
10326 | if (!cpuctx->online) { | |
10327 | err = -ENODEV; | |
10328 | goto err_unlock; | |
10329 | } | |
10330 | } | |
10331 | ||
bed5b25a | 10332 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 10333 | err = -EBUSY; |
84c4e620 | 10334 | goto err_unlock; |
bed5b25a AS |
10335 | } |
10336 | ||
fb0459d7 | 10337 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 10338 | perf_unpin_context(ctx); |
fb0459d7 AV |
10339 | mutex_unlock(&ctx->mutex); |
10340 | ||
fb0459d7 AV |
10341 | return event; |
10342 | ||
84c4e620 PZ |
10343 | err_unlock: |
10344 | mutex_unlock(&ctx->mutex); | |
10345 | perf_unpin_context(ctx); | |
10346 | put_ctx(ctx); | |
c3f00c70 PZ |
10347 | err_free: |
10348 | free_event(event); | |
10349 | err: | |
c6567f64 | 10350 | return ERR_PTR(err); |
9b51f66d | 10351 | } |
fb0459d7 | 10352 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 10353 | |
0cda4c02 YZ |
10354 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
10355 | { | |
10356 | struct perf_event_context *src_ctx; | |
10357 | struct perf_event_context *dst_ctx; | |
10358 | struct perf_event *event, *tmp; | |
10359 | LIST_HEAD(events); | |
10360 | ||
10361 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
10362 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
10363 | ||
f63a8daa PZ |
10364 | /* |
10365 | * See perf_event_ctx_lock() for comments on the details | |
10366 | * of swizzling perf_event::ctx. | |
10367 | */ | |
10368 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
10369 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
10370 | event_entry) { | |
45a0e07a | 10371 | perf_remove_from_context(event, 0); |
9a545de0 | 10372 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 10373 | put_ctx(src_ctx); |
9886167d | 10374 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 10375 | } |
0cda4c02 | 10376 | |
8f95b435 PZI |
10377 | /* |
10378 | * Wait for the events to quiesce before re-instating them. | |
10379 | */ | |
0cda4c02 YZ |
10380 | synchronize_rcu(); |
10381 | ||
8f95b435 PZI |
10382 | /* |
10383 | * Re-instate events in 2 passes. | |
10384 | * | |
10385 | * Skip over group leaders and only install siblings on this first | |
10386 | * pass, siblings will not get enabled without a leader, however a | |
10387 | * leader will enable its siblings, even if those are still on the old | |
10388 | * context. | |
10389 | */ | |
10390 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
10391 | if (event->group_leader == event) | |
10392 | continue; | |
10393 | ||
10394 | list_del(&event->migrate_entry); | |
10395 | if (event->state >= PERF_EVENT_STATE_OFF) | |
10396 | event->state = PERF_EVENT_STATE_INACTIVE; | |
10397 | account_event_cpu(event, dst_cpu); | |
10398 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
10399 | get_ctx(dst_ctx); | |
10400 | } | |
10401 | ||
10402 | /* | |
10403 | * Once all the siblings are setup properly, install the group leaders | |
10404 | * to make it go. | |
10405 | */ | |
9886167d PZ |
10406 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
10407 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
10408 | if (event->state >= PERF_EVENT_STATE_OFF) |
10409 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 10410 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
10411 | perf_install_in_context(dst_ctx, event, dst_cpu); |
10412 | get_ctx(dst_ctx); | |
10413 | } | |
10414 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 10415 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
10416 | } |
10417 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
10418 | ||
cdd6c482 | 10419 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 10420 | struct task_struct *child) |
d859e29f | 10421 | { |
cdd6c482 | 10422 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 10423 | u64 child_val; |
d859e29f | 10424 | |
cdd6c482 IM |
10425 | if (child_event->attr.inherit_stat) |
10426 | perf_event_read_event(child_event, child); | |
38b200d6 | 10427 | |
b5e58793 | 10428 | child_val = perf_event_count(child_event); |
d859e29f PM |
10429 | |
10430 | /* | |
10431 | * Add back the child's count to the parent's count: | |
10432 | */ | |
a6e6dea6 | 10433 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
10434 | atomic64_add(child_event->total_time_enabled, |
10435 | &parent_event->child_total_time_enabled); | |
10436 | atomic64_add(child_event->total_time_running, | |
10437 | &parent_event->child_total_time_running); | |
d859e29f PM |
10438 | } |
10439 | ||
9b51f66d | 10440 | static void |
8ba289b8 PZ |
10441 | perf_event_exit_event(struct perf_event *child_event, |
10442 | struct perf_event_context *child_ctx, | |
10443 | struct task_struct *child) | |
9b51f66d | 10444 | { |
8ba289b8 PZ |
10445 | struct perf_event *parent_event = child_event->parent; |
10446 | ||
1903d50c PZ |
10447 | /* |
10448 | * Do not destroy the 'original' grouping; because of the context | |
10449 | * switch optimization the original events could've ended up in a | |
10450 | * random child task. | |
10451 | * | |
10452 | * If we were to destroy the original group, all group related | |
10453 | * operations would cease to function properly after this random | |
10454 | * child dies. | |
10455 | * | |
10456 | * Do destroy all inherited groups, we don't care about those | |
10457 | * and being thorough is better. | |
10458 | */ | |
32132a3d PZ |
10459 | raw_spin_lock_irq(&child_ctx->lock); |
10460 | WARN_ON_ONCE(child_ctx->is_active); | |
10461 | ||
8ba289b8 | 10462 | if (parent_event) |
32132a3d PZ |
10463 | perf_group_detach(child_event); |
10464 | list_del_event(child_event, child_ctx); | |
a69b0ca4 | 10465 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
32132a3d | 10466 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 10467 | |
9b51f66d | 10468 | /* |
8ba289b8 | 10469 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 10470 | */ |
8ba289b8 | 10471 | if (!parent_event) { |
179033b3 | 10472 | perf_event_wakeup(child_event); |
8ba289b8 | 10473 | return; |
4bcf349a | 10474 | } |
8ba289b8 PZ |
10475 | /* |
10476 | * Child events can be cleaned up. | |
10477 | */ | |
10478 | ||
10479 | sync_child_event(child_event, child); | |
10480 | ||
10481 | /* | |
10482 | * Remove this event from the parent's list | |
10483 | */ | |
10484 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
10485 | mutex_lock(&parent_event->child_mutex); | |
10486 | list_del_init(&child_event->child_list); | |
10487 | mutex_unlock(&parent_event->child_mutex); | |
10488 | ||
10489 | /* | |
10490 | * Kick perf_poll() for is_event_hup(). | |
10491 | */ | |
10492 | perf_event_wakeup(parent_event); | |
10493 | free_event(child_event); | |
10494 | put_event(parent_event); | |
9b51f66d IM |
10495 | } |
10496 | ||
8dc85d54 | 10497 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 10498 | { |
211de6eb | 10499 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 10500 | struct perf_event *child_event, *next; |
63b6da39 PZ |
10501 | |
10502 | WARN_ON_ONCE(child != current); | |
9b51f66d | 10503 | |
6a3351b6 | 10504 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 10505 | if (!child_ctx) |
9b51f66d IM |
10506 | return; |
10507 | ||
ad3a37de | 10508 | /* |
6a3351b6 PZ |
10509 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
10510 | * ctx::mutex over the entire thing. This serializes against almost | |
10511 | * everything that wants to access the ctx. | |
10512 | * | |
10513 | * The exception is sys_perf_event_open() / | |
10514 | * perf_event_create_kernel_count() which does find_get_context() | |
10515 | * without ctx::mutex (it cannot because of the move_group double mutex | |
10516 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 10517 | */ |
6a3351b6 | 10518 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
10519 | |
10520 | /* | |
6a3351b6 PZ |
10521 | * In a single ctx::lock section, de-schedule the events and detach the |
10522 | * context from the task such that we cannot ever get it scheduled back | |
10523 | * in. | |
c93f7669 | 10524 | */ |
6a3351b6 | 10525 | raw_spin_lock_irq(&child_ctx->lock); |
487f05e1 | 10526 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL); |
4a1c0f26 | 10527 | |
71a851b4 | 10528 | /* |
63b6da39 PZ |
10529 | * Now that the context is inactive, destroy the task <-> ctx relation |
10530 | * and mark the context dead. | |
71a851b4 | 10531 | */ |
63b6da39 PZ |
10532 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
10533 | put_ctx(child_ctx); /* cannot be last */ | |
10534 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
10535 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 10536 | |
211de6eb | 10537 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 10538 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 10539 | |
211de6eb PZ |
10540 | if (clone_ctx) |
10541 | put_ctx(clone_ctx); | |
4a1c0f26 | 10542 | |
9f498cc5 | 10543 | /* |
cdd6c482 IM |
10544 | * Report the task dead after unscheduling the events so that we |
10545 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
10546 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 10547 | */ |
cdd6c482 | 10548 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 10549 | |
ebf905fc | 10550 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 10551 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 10552 | |
a63eaf34 PM |
10553 | mutex_unlock(&child_ctx->mutex); |
10554 | ||
10555 | put_ctx(child_ctx); | |
9b51f66d IM |
10556 | } |
10557 | ||
8dc85d54 PZ |
10558 | /* |
10559 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 PZ |
10560 | * |
10561 | * Can be called with cred_guard_mutex held when called from | |
10562 | * install_exec_creds(). | |
8dc85d54 PZ |
10563 | */ |
10564 | void perf_event_exit_task(struct task_struct *child) | |
10565 | { | |
8882135b | 10566 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
10567 | int ctxn; |
10568 | ||
8882135b PZ |
10569 | mutex_lock(&child->perf_event_mutex); |
10570 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
10571 | owner_entry) { | |
10572 | list_del_init(&event->owner_entry); | |
10573 | ||
10574 | /* | |
10575 | * Ensure the list deletion is visible before we clear | |
10576 | * the owner, closes a race against perf_release() where | |
10577 | * we need to serialize on the owner->perf_event_mutex. | |
10578 | */ | |
f47c02c0 | 10579 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
10580 | } |
10581 | mutex_unlock(&child->perf_event_mutex); | |
10582 | ||
8dc85d54 PZ |
10583 | for_each_task_context_nr(ctxn) |
10584 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
10585 | |
10586 | /* | |
10587 | * The perf_event_exit_task_context calls perf_event_task | |
10588 | * with child's task_ctx, which generates EXIT events for | |
10589 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
10590 | * At this point we need to send EXIT events to cpu contexts. | |
10591 | */ | |
10592 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
10593 | } |
10594 | ||
889ff015 FW |
10595 | static void perf_free_event(struct perf_event *event, |
10596 | struct perf_event_context *ctx) | |
10597 | { | |
10598 | struct perf_event *parent = event->parent; | |
10599 | ||
10600 | if (WARN_ON_ONCE(!parent)) | |
10601 | return; | |
10602 | ||
10603 | mutex_lock(&parent->child_mutex); | |
10604 | list_del_init(&event->child_list); | |
10605 | mutex_unlock(&parent->child_mutex); | |
10606 | ||
a6fa941d | 10607 | put_event(parent); |
889ff015 | 10608 | |
652884fe | 10609 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 10610 | perf_group_detach(event); |
889ff015 | 10611 | list_del_event(event, ctx); |
652884fe | 10612 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
10613 | free_event(event); |
10614 | } | |
10615 | ||
bbbee908 | 10616 | /* |
652884fe | 10617 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 10618 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
10619 | * |
10620 | * Not all locks are strictly required, but take them anyway to be nice and | |
10621 | * help out with the lockdep assertions. | |
bbbee908 | 10622 | */ |
cdd6c482 | 10623 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 10624 | { |
8dc85d54 | 10625 | struct perf_event_context *ctx; |
cdd6c482 | 10626 | struct perf_event *event, *tmp; |
8dc85d54 | 10627 | int ctxn; |
bbbee908 | 10628 | |
8dc85d54 PZ |
10629 | for_each_task_context_nr(ctxn) { |
10630 | ctx = task->perf_event_ctxp[ctxn]; | |
10631 | if (!ctx) | |
10632 | continue; | |
bbbee908 | 10633 | |
8dc85d54 | 10634 | mutex_lock(&ctx->mutex); |
e552a838 PZ |
10635 | raw_spin_lock_irq(&ctx->lock); |
10636 | /* | |
10637 | * Destroy the task <-> ctx relation and mark the context dead. | |
10638 | * | |
10639 | * This is important because even though the task hasn't been | |
10640 | * exposed yet the context has been (through child_list). | |
10641 | */ | |
10642 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); | |
10643 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
10644 | put_task_struct(task); /* cannot be last */ | |
10645 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 10646 | |
15121c78 | 10647 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
8dc85d54 | 10648 | perf_free_event(event, ctx); |
bbbee908 | 10649 | |
8dc85d54 | 10650 | mutex_unlock(&ctx->mutex); |
8dc85d54 PZ |
10651 | put_ctx(ctx); |
10652 | } | |
889ff015 FW |
10653 | } |
10654 | ||
4e231c79 PZ |
10655 | void perf_event_delayed_put(struct task_struct *task) |
10656 | { | |
10657 | int ctxn; | |
10658 | ||
10659 | for_each_task_context_nr(ctxn) | |
10660 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
10661 | } | |
10662 | ||
e03e7ee3 | 10663 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 10664 | { |
e03e7ee3 | 10665 | struct file *file; |
ffe8690c | 10666 | |
e03e7ee3 AS |
10667 | file = fget_raw(fd); |
10668 | if (!file) | |
10669 | return ERR_PTR(-EBADF); | |
ffe8690c | 10670 | |
e03e7ee3 AS |
10671 | if (file->f_op != &perf_fops) { |
10672 | fput(file); | |
10673 | return ERR_PTR(-EBADF); | |
10674 | } | |
ffe8690c | 10675 | |
e03e7ee3 | 10676 | return file; |
ffe8690c KX |
10677 | } |
10678 | ||
10679 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
10680 | { | |
10681 | if (!event) | |
10682 | return ERR_PTR(-EINVAL); | |
10683 | ||
10684 | return &event->attr; | |
10685 | } | |
10686 | ||
97dee4f3 | 10687 | /* |
d8a8cfc7 PZ |
10688 | * Inherit a event from parent task to child task. |
10689 | * | |
10690 | * Returns: | |
10691 | * - valid pointer on success | |
10692 | * - NULL for orphaned events | |
10693 | * - IS_ERR() on error | |
97dee4f3 PZ |
10694 | */ |
10695 | static struct perf_event * | |
10696 | inherit_event(struct perf_event *parent_event, | |
10697 | struct task_struct *parent, | |
10698 | struct perf_event_context *parent_ctx, | |
10699 | struct task_struct *child, | |
10700 | struct perf_event *group_leader, | |
10701 | struct perf_event_context *child_ctx) | |
10702 | { | |
1929def9 | 10703 | enum perf_event_active_state parent_state = parent_event->state; |
97dee4f3 | 10704 | struct perf_event *child_event; |
cee010ec | 10705 | unsigned long flags; |
97dee4f3 PZ |
10706 | |
10707 | /* | |
10708 | * Instead of creating recursive hierarchies of events, | |
10709 | * we link inherited events back to the original parent, | |
10710 | * which has a filp for sure, which we use as the reference | |
10711 | * count: | |
10712 | */ | |
10713 | if (parent_event->parent) | |
10714 | parent_event = parent_event->parent; | |
10715 | ||
10716 | child_event = perf_event_alloc(&parent_event->attr, | |
10717 | parent_event->cpu, | |
d580ff86 | 10718 | child, |
97dee4f3 | 10719 | group_leader, parent_event, |
79dff51e | 10720 | NULL, NULL, -1); |
97dee4f3 PZ |
10721 | if (IS_ERR(child_event)) |
10722 | return child_event; | |
a6fa941d | 10723 | |
c6e5b732 PZ |
10724 | /* |
10725 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
10726 | * must be under the same lock in order to serialize against | |
10727 | * perf_event_release_kernel(), such that either we must observe | |
10728 | * is_orphaned_event() or they will observe us on the child_list. | |
10729 | */ | |
10730 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
10731 | if (is_orphaned_event(parent_event) || |
10732 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 10733 | mutex_unlock(&parent_event->child_mutex); |
a6fa941d AV |
10734 | free_event(child_event); |
10735 | return NULL; | |
10736 | } | |
10737 | ||
97dee4f3 PZ |
10738 | get_ctx(child_ctx); |
10739 | ||
10740 | /* | |
10741 | * Make the child state follow the state of the parent event, | |
10742 | * not its attr.disabled bit. We hold the parent's mutex, | |
10743 | * so we won't race with perf_event_{en, dis}able_family. | |
10744 | */ | |
1929def9 | 10745 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
10746 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
10747 | else | |
10748 | child_event->state = PERF_EVENT_STATE_OFF; | |
10749 | ||
10750 | if (parent_event->attr.freq) { | |
10751 | u64 sample_period = parent_event->hw.sample_period; | |
10752 | struct hw_perf_event *hwc = &child_event->hw; | |
10753 | ||
10754 | hwc->sample_period = sample_period; | |
10755 | hwc->last_period = sample_period; | |
10756 | ||
10757 | local64_set(&hwc->period_left, sample_period); | |
10758 | } | |
10759 | ||
10760 | child_event->ctx = child_ctx; | |
10761 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
10762 | child_event->overflow_handler_context |
10763 | = parent_event->overflow_handler_context; | |
97dee4f3 | 10764 | |
614b6780 TG |
10765 | /* |
10766 | * Precalculate sample_data sizes | |
10767 | */ | |
10768 | perf_event__header_size(child_event); | |
6844c09d | 10769 | perf_event__id_header_size(child_event); |
614b6780 | 10770 | |
97dee4f3 PZ |
10771 | /* |
10772 | * Link it up in the child's context: | |
10773 | */ | |
cee010ec | 10774 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 10775 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 10776 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 10777 | |
97dee4f3 PZ |
10778 | /* |
10779 | * Link this into the parent event's child list | |
10780 | */ | |
97dee4f3 PZ |
10781 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
10782 | mutex_unlock(&parent_event->child_mutex); | |
10783 | ||
10784 | return child_event; | |
10785 | } | |
10786 | ||
d8a8cfc7 PZ |
10787 | /* |
10788 | * Inherits an event group. | |
10789 | * | |
10790 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
10791 | * This matches with perf_event_release_kernel() removing all child events. | |
10792 | * | |
10793 | * Returns: | |
10794 | * - 0 on success | |
10795 | * - <0 on error | |
10796 | */ | |
97dee4f3 PZ |
10797 | static int inherit_group(struct perf_event *parent_event, |
10798 | struct task_struct *parent, | |
10799 | struct perf_event_context *parent_ctx, | |
10800 | struct task_struct *child, | |
10801 | struct perf_event_context *child_ctx) | |
10802 | { | |
10803 | struct perf_event *leader; | |
10804 | struct perf_event *sub; | |
10805 | struct perf_event *child_ctr; | |
10806 | ||
10807 | leader = inherit_event(parent_event, parent, parent_ctx, | |
10808 | child, NULL, child_ctx); | |
10809 | if (IS_ERR(leader)) | |
10810 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
10811 | /* |
10812 | * @leader can be NULL here because of is_orphaned_event(). In this | |
10813 | * case inherit_event() will create individual events, similar to what | |
10814 | * perf_group_detach() would do anyway. | |
10815 | */ | |
97dee4f3 PZ |
10816 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
10817 | child_ctr = inherit_event(sub, parent, parent_ctx, | |
10818 | child, leader, child_ctx); | |
10819 | if (IS_ERR(child_ctr)) | |
10820 | return PTR_ERR(child_ctr); | |
10821 | } | |
10822 | return 0; | |
889ff015 FW |
10823 | } |
10824 | ||
d8a8cfc7 PZ |
10825 | /* |
10826 | * Creates the child task context and tries to inherit the event-group. | |
10827 | * | |
10828 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
10829 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
10830 | * consistent with perf_event_release_kernel() removing all child events. | |
10831 | * | |
10832 | * Returns: | |
10833 | * - 0 on success | |
10834 | * - <0 on error | |
10835 | */ | |
889ff015 FW |
10836 | static int |
10837 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
10838 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 10839 | struct task_struct *child, int ctxn, |
889ff015 FW |
10840 | int *inherited_all) |
10841 | { | |
10842 | int ret; | |
8dc85d54 | 10843 | struct perf_event_context *child_ctx; |
889ff015 FW |
10844 | |
10845 | if (!event->attr.inherit) { | |
10846 | *inherited_all = 0; | |
10847 | return 0; | |
bbbee908 PZ |
10848 | } |
10849 | ||
fe4b04fa | 10850 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
10851 | if (!child_ctx) { |
10852 | /* | |
10853 | * This is executed from the parent task context, so | |
10854 | * inherit events that have been marked for cloning. | |
10855 | * First allocate and initialize a context for the | |
10856 | * child. | |
10857 | */ | |
734df5ab | 10858 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
10859 | if (!child_ctx) |
10860 | return -ENOMEM; | |
bbbee908 | 10861 | |
8dc85d54 | 10862 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
10863 | } |
10864 | ||
10865 | ret = inherit_group(event, parent, parent_ctx, | |
10866 | child, child_ctx); | |
10867 | ||
10868 | if (ret) | |
10869 | *inherited_all = 0; | |
10870 | ||
10871 | return ret; | |
bbbee908 PZ |
10872 | } |
10873 | ||
9b51f66d | 10874 | /* |
cdd6c482 | 10875 | * Initialize the perf_event context in task_struct |
9b51f66d | 10876 | */ |
985c8dcb | 10877 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 10878 | { |
889ff015 | 10879 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
10880 | struct perf_event_context *cloned_ctx; |
10881 | struct perf_event *event; | |
9b51f66d | 10882 | struct task_struct *parent = current; |
564c2b21 | 10883 | int inherited_all = 1; |
dddd3379 | 10884 | unsigned long flags; |
6ab423e0 | 10885 | int ret = 0; |
9b51f66d | 10886 | |
8dc85d54 | 10887 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
10888 | return 0; |
10889 | ||
ad3a37de | 10890 | /* |
25346b93 PM |
10891 | * If the parent's context is a clone, pin it so it won't get |
10892 | * swapped under us. | |
ad3a37de | 10893 | */ |
8dc85d54 | 10894 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
10895 | if (!parent_ctx) |
10896 | return 0; | |
25346b93 | 10897 | |
ad3a37de PM |
10898 | /* |
10899 | * No need to check if parent_ctx != NULL here; since we saw | |
10900 | * it non-NULL earlier, the only reason for it to become NULL | |
10901 | * is if we exit, and since we're currently in the middle of | |
10902 | * a fork we can't be exiting at the same time. | |
10903 | */ | |
ad3a37de | 10904 | |
9b51f66d IM |
10905 | /* |
10906 | * Lock the parent list. No need to lock the child - not PID | |
10907 | * hashed yet and not running, so nobody can access it. | |
10908 | */ | |
d859e29f | 10909 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
10910 | |
10911 | /* | |
10912 | * We dont have to disable NMIs - we are only looking at | |
10913 | * the list, not manipulating it: | |
10914 | */ | |
889ff015 | 10915 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
8dc85d54 PZ |
10916 | ret = inherit_task_group(event, parent, parent_ctx, |
10917 | child, ctxn, &inherited_all); | |
889ff015 | 10918 | if (ret) |
e7cc4865 | 10919 | goto out_unlock; |
889ff015 | 10920 | } |
b93f7978 | 10921 | |
dddd3379 TG |
10922 | /* |
10923 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
10924 | * to allocations, but we need to prevent rotation because | |
10925 | * rotate_ctx() will change the list from interrupt context. | |
10926 | */ | |
10927 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
10928 | parent_ctx->rotate_disable = 1; | |
10929 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
10930 | ||
889ff015 | 10931 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
8dc85d54 PZ |
10932 | ret = inherit_task_group(event, parent, parent_ctx, |
10933 | child, ctxn, &inherited_all); | |
889ff015 | 10934 | if (ret) |
e7cc4865 | 10935 | goto out_unlock; |
564c2b21 PM |
10936 | } |
10937 | ||
dddd3379 TG |
10938 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
10939 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 10940 | |
8dc85d54 | 10941 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 10942 | |
05cbaa28 | 10943 | if (child_ctx && inherited_all) { |
564c2b21 PM |
10944 | /* |
10945 | * Mark the child context as a clone of the parent | |
10946 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
10947 | * |
10948 | * Note that if the parent is a clone, the holding of | |
10949 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 10950 | */ |
c5ed5145 | 10951 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
10952 | if (cloned_ctx) { |
10953 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 10954 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
10955 | } else { |
10956 | child_ctx->parent_ctx = parent_ctx; | |
10957 | child_ctx->parent_gen = parent_ctx->generation; | |
10958 | } | |
10959 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
10960 | } |
10961 | ||
c5ed5145 | 10962 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 10963 | out_unlock: |
d859e29f | 10964 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 10965 | |
25346b93 | 10966 | perf_unpin_context(parent_ctx); |
fe4b04fa | 10967 | put_ctx(parent_ctx); |
ad3a37de | 10968 | |
6ab423e0 | 10969 | return ret; |
9b51f66d IM |
10970 | } |
10971 | ||
8dc85d54 PZ |
10972 | /* |
10973 | * Initialize the perf_event context in task_struct | |
10974 | */ | |
10975 | int perf_event_init_task(struct task_struct *child) | |
10976 | { | |
10977 | int ctxn, ret; | |
10978 | ||
8550d7cb ON |
10979 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
10980 | mutex_init(&child->perf_event_mutex); | |
10981 | INIT_LIST_HEAD(&child->perf_event_list); | |
10982 | ||
8dc85d54 PZ |
10983 | for_each_task_context_nr(ctxn) { |
10984 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
10985 | if (ret) { |
10986 | perf_event_free_task(child); | |
8dc85d54 | 10987 | return ret; |
6c72e350 | 10988 | } |
8dc85d54 PZ |
10989 | } |
10990 | ||
10991 | return 0; | |
10992 | } | |
10993 | ||
220b140b PM |
10994 | static void __init perf_event_init_all_cpus(void) |
10995 | { | |
b28ab83c | 10996 | struct swevent_htable *swhash; |
220b140b | 10997 | int cpu; |
220b140b | 10998 | |
a63fbed7 TG |
10999 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
11000 | ||
220b140b | 11001 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
11002 | swhash = &per_cpu(swevent_htable, cpu); |
11003 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 11004 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
11005 | |
11006 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
11007 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 11008 | |
058fe1c0 DCC |
11009 | #ifdef CONFIG_CGROUP_PERF |
11010 | INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu)); | |
11011 | #endif | |
e48c1788 | 11012 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
220b140b PM |
11013 | } |
11014 | } | |
11015 | ||
a63fbed7 | 11016 | void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 11017 | { |
108b02cf | 11018 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 11019 | |
b28ab83c | 11020 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 11021 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
11022 | struct swevent_hlist *hlist; |
11023 | ||
b28ab83c PZ |
11024 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
11025 | WARN_ON(!hlist); | |
11026 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 11027 | } |
b28ab83c | 11028 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
11029 | } |
11030 | ||
2965faa5 | 11031 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 11032 | static void __perf_event_exit_context(void *__info) |
0793a61d | 11033 | { |
108b02cf | 11034 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
11035 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
11036 | struct perf_event *event; | |
0793a61d | 11037 | |
fae3fde6 PZ |
11038 | raw_spin_lock(&ctx->lock); |
11039 | list_for_each_entry(event, &ctx->event_list, event_entry) | |
45a0e07a | 11040 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 11041 | raw_spin_unlock(&ctx->lock); |
0793a61d | 11042 | } |
108b02cf PZ |
11043 | |
11044 | static void perf_event_exit_cpu_context(int cpu) | |
11045 | { | |
a63fbed7 | 11046 | struct perf_cpu_context *cpuctx; |
108b02cf PZ |
11047 | struct perf_event_context *ctx; |
11048 | struct pmu *pmu; | |
108b02cf | 11049 | |
a63fbed7 TG |
11050 | mutex_lock(&pmus_lock); |
11051 | list_for_each_entry(pmu, &pmus, entry) { | |
11052 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11053 | ctx = &cpuctx->ctx; | |
108b02cf PZ |
11054 | |
11055 | mutex_lock(&ctx->mutex); | |
11056 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
a63fbed7 | 11057 | cpuctx->online = 0; |
108b02cf PZ |
11058 | mutex_unlock(&ctx->mutex); |
11059 | } | |
a63fbed7 TG |
11060 | cpumask_clear_cpu(cpu, perf_online_mask); |
11061 | mutex_unlock(&pmus_lock); | |
108b02cf | 11062 | } |
00e16c3d TG |
11063 | #else |
11064 | ||
11065 | static void perf_event_exit_cpu_context(int cpu) { } | |
11066 | ||
11067 | #endif | |
108b02cf | 11068 | |
a63fbed7 TG |
11069 | int perf_event_init_cpu(unsigned int cpu) |
11070 | { | |
11071 | struct perf_cpu_context *cpuctx; | |
11072 | struct perf_event_context *ctx; | |
11073 | struct pmu *pmu; | |
11074 | ||
11075 | perf_swevent_init_cpu(cpu); | |
11076 | ||
11077 | mutex_lock(&pmus_lock); | |
11078 | cpumask_set_cpu(cpu, perf_online_mask); | |
11079 | list_for_each_entry(pmu, &pmus, entry) { | |
11080 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11081 | ctx = &cpuctx->ctx; | |
11082 | ||
11083 | mutex_lock(&ctx->mutex); | |
11084 | cpuctx->online = 1; | |
11085 | mutex_unlock(&ctx->mutex); | |
11086 | } | |
11087 | mutex_unlock(&pmus_lock); | |
11088 | ||
11089 | return 0; | |
11090 | } | |
11091 | ||
00e16c3d | 11092 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 11093 | { |
e3703f8c | 11094 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 11095 | return 0; |
0793a61d | 11096 | } |
0793a61d | 11097 | |
c277443c PZ |
11098 | static int |
11099 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
11100 | { | |
11101 | int cpu; | |
11102 | ||
11103 | for_each_online_cpu(cpu) | |
11104 | perf_event_exit_cpu(cpu); | |
11105 | ||
11106 | return NOTIFY_OK; | |
11107 | } | |
11108 | ||
11109 | /* | |
11110 | * Run the perf reboot notifier at the very last possible moment so that | |
11111 | * the generic watchdog code runs as long as possible. | |
11112 | */ | |
11113 | static struct notifier_block perf_reboot_notifier = { | |
11114 | .notifier_call = perf_reboot, | |
11115 | .priority = INT_MIN, | |
11116 | }; | |
11117 | ||
cdd6c482 | 11118 | void __init perf_event_init(void) |
0793a61d | 11119 | { |
3c502e7a JW |
11120 | int ret; |
11121 | ||
2e80a82a PZ |
11122 | idr_init(&pmu_idr); |
11123 | ||
220b140b | 11124 | perf_event_init_all_cpus(); |
b0a873eb | 11125 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
11126 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
11127 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
11128 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb | 11129 | perf_tp_register(); |
00e16c3d | 11130 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 11131 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
11132 | |
11133 | ret = init_hw_breakpoint(); | |
11134 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 11135 | |
b01c3a00 JO |
11136 | /* |
11137 | * Build time assertion that we keep the data_head at the intended | |
11138 | * location. IOW, validation we got the __reserved[] size right. | |
11139 | */ | |
11140 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
11141 | != 1024); | |
0793a61d | 11142 | } |
abe43400 | 11143 | |
fd979c01 CS |
11144 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
11145 | char *page) | |
11146 | { | |
11147 | struct perf_pmu_events_attr *pmu_attr = | |
11148 | container_of(attr, struct perf_pmu_events_attr, attr); | |
11149 | ||
11150 | if (pmu_attr->event_str) | |
11151 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
11152 | ||
11153 | return 0; | |
11154 | } | |
675965b0 | 11155 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 11156 | |
abe43400 PZ |
11157 | static int __init perf_event_sysfs_init(void) |
11158 | { | |
11159 | struct pmu *pmu; | |
11160 | int ret; | |
11161 | ||
11162 | mutex_lock(&pmus_lock); | |
11163 | ||
11164 | ret = bus_register(&pmu_bus); | |
11165 | if (ret) | |
11166 | goto unlock; | |
11167 | ||
11168 | list_for_each_entry(pmu, &pmus, entry) { | |
11169 | if (!pmu->name || pmu->type < 0) | |
11170 | continue; | |
11171 | ||
11172 | ret = pmu_dev_alloc(pmu); | |
11173 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
11174 | } | |
11175 | pmu_bus_running = 1; | |
11176 | ret = 0; | |
11177 | ||
11178 | unlock: | |
11179 | mutex_unlock(&pmus_lock); | |
11180 | ||
11181 | return ret; | |
11182 | } | |
11183 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
11184 | |
11185 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
11186 | static struct cgroup_subsys_state * |
11187 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
11188 | { |
11189 | struct perf_cgroup *jc; | |
e5d1367f | 11190 | |
1b15d055 | 11191 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
11192 | if (!jc) |
11193 | return ERR_PTR(-ENOMEM); | |
11194 | ||
e5d1367f SE |
11195 | jc->info = alloc_percpu(struct perf_cgroup_info); |
11196 | if (!jc->info) { | |
11197 | kfree(jc); | |
11198 | return ERR_PTR(-ENOMEM); | |
11199 | } | |
11200 | ||
e5d1367f SE |
11201 | return &jc->css; |
11202 | } | |
11203 | ||
eb95419b | 11204 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 11205 | { |
eb95419b TH |
11206 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
11207 | ||
e5d1367f SE |
11208 | free_percpu(jc->info); |
11209 | kfree(jc); | |
11210 | } | |
11211 | ||
11212 | static int __perf_cgroup_move(void *info) | |
11213 | { | |
11214 | struct task_struct *task = info; | |
ddaaf4e2 | 11215 | rcu_read_lock(); |
e5d1367f | 11216 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 11217 | rcu_read_unlock(); |
e5d1367f SE |
11218 | return 0; |
11219 | } | |
11220 | ||
1f7dd3e5 | 11221 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 11222 | { |
bb9d97b6 | 11223 | struct task_struct *task; |
1f7dd3e5 | 11224 | struct cgroup_subsys_state *css; |
bb9d97b6 | 11225 | |
1f7dd3e5 | 11226 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 11227 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
11228 | } |
11229 | ||
073219e9 | 11230 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
11231 | .css_alloc = perf_cgroup_css_alloc, |
11232 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 11233 | .attach = perf_cgroup_attach, |
968ebff1 TH |
11234 | /* |
11235 | * Implicitly enable on dfl hierarchy so that perf events can | |
11236 | * always be filtered by cgroup2 path as long as perf_event | |
11237 | * controller is not mounted on a legacy hierarchy. | |
11238 | */ | |
11239 | .implicit_on_dfl = true, | |
e5d1367f SE |
11240 | }; |
11241 | #endif /* CONFIG_CGROUP_PERF */ |