Commit | Line | Data |
---|---|---|
8e86e015 | 1 | // SPDX-License-Identifier: GPL-2.0 |
0793a61d | 2 | /* |
57c0c15b | 3 | * Performance events core code: |
0793a61d | 4 | * |
98144511 | 5 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
e7e7ee2e | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
90eec103 | 7 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
d36b6910 | 8 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
0793a61d TG |
9 | */ |
10 | ||
11 | #include <linux/fs.h> | |
b9cacc7b | 12 | #include <linux/mm.h> |
0793a61d TG |
13 | #include <linux/cpu.h> |
14 | #include <linux/smp.h> | |
2e80a82a | 15 | #include <linux/idr.h> |
04289bb9 | 16 | #include <linux/file.h> |
0793a61d | 17 | #include <linux/poll.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
76e1d904 | 19 | #include <linux/hash.h> |
12351ef8 | 20 | #include <linux/tick.h> |
0793a61d | 21 | #include <linux/sysfs.h> |
22a4f650 | 22 | #include <linux/dcache.h> |
0793a61d | 23 | #include <linux/percpu.h> |
22a4f650 | 24 | #include <linux/ptrace.h> |
c277443c | 25 | #include <linux/reboot.h> |
b9cacc7b | 26 | #include <linux/vmstat.h> |
abe43400 | 27 | #include <linux/device.h> |
6e5fdeed | 28 | #include <linux/export.h> |
906010b2 | 29 | #include <linux/vmalloc.h> |
b9cacc7b | 30 | #include <linux/hardirq.h> |
03911132 | 31 | #include <linux/hugetlb.h> |
b9cacc7b | 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> | |
6eef8a71 | 53 | #include <linux/min_heap.h> |
8d97e718 | 54 | #include <linux/highmem.h> |
8af26be0 | 55 | #include <linux/pgtable.h> |
88a16a13 | 56 | #include <linux/buildid.h> |
ca6c2132 | 57 | #include <linux/task_work.h> |
0793a61d | 58 | |
76369139 FW |
59 | #include "internal.h" |
60 | ||
4e193bd4 TB |
61 | #include <asm/irq_regs.h> |
62 | ||
272325c4 PZ |
63 | typedef int (*remote_function_f)(void *); |
64 | ||
fe4b04fa | 65 | struct remote_function_call { |
e7e7ee2e | 66 | struct task_struct *p; |
272325c4 | 67 | remote_function_f func; |
e7e7ee2e IM |
68 | void *info; |
69 | int ret; | |
fe4b04fa PZ |
70 | }; |
71 | ||
72 | static void remote_function(void *data) | |
73 | { | |
74 | struct remote_function_call *tfc = data; | |
75 | struct task_struct *p = tfc->p; | |
76 | ||
77 | if (p) { | |
0da4cf3e PZ |
78 | /* -EAGAIN */ |
79 | if (task_cpu(p) != smp_processor_id()) | |
80 | return; | |
81 | ||
82 | /* | |
83 | * Now that we're on right CPU with IRQs disabled, we can test | |
84 | * if we hit the right task without races. | |
85 | */ | |
86 | ||
87 | tfc->ret = -ESRCH; /* No such (running) process */ | |
88 | if (p != current) | |
fe4b04fa PZ |
89 | return; |
90 | } | |
91 | ||
92 | tfc->ret = tfc->func(tfc->info); | |
93 | } | |
94 | ||
95 | /** | |
96 | * task_function_call - call a function on the cpu on which a task runs | |
97 | * @p: the task to evaluate | |
98 | * @func: the function to be called | |
99 | * @info: the function call argument | |
100 | * | |
101 | * Calls the function @func when the task is currently running. This might | |
2ed6edd3 BR |
102 | * be on the current CPU, which just calls the function directly. This will |
103 | * retry due to any failures in smp_call_function_single(), such as if the | |
104 | * task_cpu() goes offline concurrently. | |
fe4b04fa | 105 | * |
6d6b8b9f | 106 | * returns @func return value or -ESRCH or -ENXIO when the process isn't running |
fe4b04fa PZ |
107 | */ |
108 | static int | |
272325c4 | 109 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
110 | { |
111 | struct remote_function_call data = { | |
e7e7ee2e IM |
112 | .p = p, |
113 | .func = func, | |
114 | .info = info, | |
0da4cf3e | 115 | .ret = -EAGAIN, |
fe4b04fa | 116 | }; |
0da4cf3e | 117 | int ret; |
fe4b04fa | 118 | |
2ed6edd3 BR |
119 | for (;;) { |
120 | ret = smp_call_function_single(task_cpu(p), remote_function, | |
121 | &data, 1); | |
6d6b8b9f KJ |
122 | if (!ret) |
123 | ret = data.ret; | |
2ed6edd3 BR |
124 | |
125 | if (ret != -EAGAIN) | |
126 | break; | |
127 | ||
128 | cond_resched(); | |
129 | } | |
fe4b04fa | 130 | |
0da4cf3e | 131 | return ret; |
fe4b04fa PZ |
132 | } |
133 | ||
134 | /** | |
135 | * cpu_function_call - call a function on the cpu | |
a1ddf524 | 136 | * @cpu: target cpu to queue this function |
fe4b04fa PZ |
137 | * @func: the function to be called |
138 | * @info: the function call argument | |
139 | * | |
140 | * Calls the function @func on the remote cpu. | |
141 | * | |
142 | * returns: @func return value or -ENXIO when the cpu is offline | |
143 | */ | |
272325c4 | 144 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
145 | { |
146 | struct remote_function_call data = { | |
e7e7ee2e IM |
147 | .p = NULL, |
148 | .func = func, | |
149 | .info = info, | |
150 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
151 | }; |
152 | ||
153 | smp_call_function_single(cpu, remote_function, &data, 1); | |
154 | ||
155 | return data.ret; | |
156 | } | |
157 | ||
fae3fde6 PZ |
158 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
159 | struct perf_event_context *ctx) | |
0017960f | 160 | { |
fae3fde6 PZ |
161 | raw_spin_lock(&cpuctx->ctx.lock); |
162 | if (ctx) | |
163 | raw_spin_lock(&ctx->lock); | |
164 | } | |
165 | ||
166 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
167 | struct perf_event_context *ctx) | |
168 | { | |
169 | if (ctx) | |
170 | raw_spin_unlock(&ctx->lock); | |
171 | raw_spin_unlock(&cpuctx->ctx.lock); | |
172 | } | |
173 | ||
63b6da39 PZ |
174 | #define TASK_TOMBSTONE ((void *)-1L) |
175 | ||
176 | static bool is_kernel_event(struct perf_event *event) | |
177 | { | |
f47c02c0 | 178 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
179 | } |
180 | ||
bd275681 PZ |
181 | static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
182 | ||
183 | struct perf_event_context *perf_cpu_task_ctx(void) | |
184 | { | |
185 | lockdep_assert_irqs_disabled(); | |
186 | return this_cpu_ptr(&perf_cpu_context)->task_ctx; | |
187 | } | |
188 | ||
39a43640 PZ |
189 | /* |
190 | * On task ctx scheduling... | |
191 | * | |
192 | * When !ctx->nr_events a task context will not be scheduled. This means | |
193 | * we can disable the scheduler hooks (for performance) without leaving | |
194 | * pending task ctx state. | |
195 | * | |
196 | * This however results in two special cases: | |
197 | * | |
198 | * - removing the last event from a task ctx; this is relatively straight | |
199 | * forward and is done in __perf_remove_from_context. | |
200 | * | |
201 | * - adding the first event to a task ctx; this is tricky because we cannot | |
202 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
203 | * See perf_install_in_context(). | |
204 | * | |
39a43640 PZ |
205 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
206 | */ | |
207 | ||
fae3fde6 PZ |
208 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
209 | struct perf_event_context *, void *); | |
210 | ||
211 | struct event_function_struct { | |
212 | struct perf_event *event; | |
213 | event_f func; | |
214 | void *data; | |
215 | }; | |
216 | ||
217 | static int event_function(void *info) | |
218 | { | |
219 | struct event_function_struct *efs = info; | |
220 | struct perf_event *event = efs->event; | |
0017960f | 221 | struct perf_event_context *ctx = event->ctx; |
bd275681 | 222 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
fae3fde6 | 223 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63b6da39 | 224 | int ret = 0; |
fae3fde6 | 225 | |
16444645 | 226 | lockdep_assert_irqs_disabled(); |
fae3fde6 | 227 | |
63b6da39 | 228 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
229 | /* |
230 | * Since we do the IPI call without holding ctx->lock things can have | |
231 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
232 | */ |
233 | if (ctx->task) { | |
63b6da39 | 234 | if (ctx->task != current) { |
0da4cf3e | 235 | ret = -ESRCH; |
63b6da39 PZ |
236 | goto unlock; |
237 | } | |
fae3fde6 | 238 | |
fae3fde6 PZ |
239 | /* |
240 | * We only use event_function_call() on established contexts, | |
241 | * and event_function() is only ever called when active (or | |
242 | * rather, we'll have bailed in task_function_call() or the | |
243 | * above ctx->task != current test), therefore we must have | |
244 | * ctx->is_active here. | |
245 | */ | |
246 | WARN_ON_ONCE(!ctx->is_active); | |
247 | /* | |
248 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
249 | * match. | |
250 | */ | |
63b6da39 PZ |
251 | WARN_ON_ONCE(task_ctx != ctx); |
252 | } else { | |
253 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 254 | } |
63b6da39 | 255 | |
fae3fde6 | 256 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 257 | unlock: |
fae3fde6 PZ |
258 | perf_ctx_unlock(cpuctx, task_ctx); |
259 | ||
63b6da39 | 260 | return ret; |
fae3fde6 PZ |
261 | } |
262 | ||
fae3fde6 | 263 | static void event_function_call(struct perf_event *event, event_f func, void *data) |
0017960f PZ |
264 | { |
265 | struct perf_event_context *ctx = event->ctx; | |
63b6da39 | 266 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
fae3fde6 PZ |
267 | struct event_function_struct efs = { |
268 | .event = event, | |
269 | .func = func, | |
270 | .data = data, | |
271 | }; | |
0017960f | 272 | |
c97f4736 PZ |
273 | if (!event->parent) { |
274 | /* | |
275 | * If this is a !child event, we must hold ctx::mutex to | |
c034f48e | 276 | * stabilize the event->ctx relation. See |
c97f4736 PZ |
277 | * perf_event_ctx_lock(). |
278 | */ | |
279 | lockdep_assert_held(&ctx->mutex); | |
280 | } | |
0017960f PZ |
281 | |
282 | if (!task) { | |
fae3fde6 | 283 | cpu_function_call(event->cpu, event_function, &efs); |
0017960f PZ |
284 | return; |
285 | } | |
286 | ||
63b6da39 PZ |
287 | if (task == TASK_TOMBSTONE) |
288 | return; | |
289 | ||
a096309b | 290 | again: |
fae3fde6 | 291 | if (!task_function_call(task, event_function, &efs)) |
0017960f PZ |
292 | return; |
293 | ||
294 | raw_spin_lock_irq(&ctx->lock); | |
63b6da39 PZ |
295 | /* |
296 | * Reload the task pointer, it might have been changed by | |
297 | * a concurrent perf_event_context_sched_out(). | |
298 | */ | |
299 | task = ctx->task; | |
a096309b PZ |
300 | if (task == TASK_TOMBSTONE) { |
301 | raw_spin_unlock_irq(&ctx->lock); | |
302 | return; | |
0017960f | 303 | } |
a096309b PZ |
304 | if (ctx->is_active) { |
305 | raw_spin_unlock_irq(&ctx->lock); | |
306 | goto again; | |
307 | } | |
308 | func(event, NULL, ctx, data); | |
0017960f PZ |
309 | raw_spin_unlock_irq(&ctx->lock); |
310 | } | |
311 | ||
cca20946 PZ |
312 | /* |
313 | * Similar to event_function_call() + event_function(), but hard assumes IRQs | |
314 | * are already disabled and we're on the right CPU. | |
315 | */ | |
316 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
317 | { | |
318 | struct perf_event_context *ctx = event->ctx; | |
bd275681 | 319 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
cca20946 PZ |
320 | struct task_struct *task = READ_ONCE(ctx->task); |
321 | struct perf_event_context *task_ctx = NULL; | |
322 | ||
16444645 | 323 | lockdep_assert_irqs_disabled(); |
cca20946 PZ |
324 | |
325 | if (task) { | |
326 | if (task == TASK_TOMBSTONE) | |
327 | return; | |
328 | ||
329 | task_ctx = ctx; | |
330 | } | |
331 | ||
332 | perf_ctx_lock(cpuctx, task_ctx); | |
333 | ||
334 | task = ctx->task; | |
335 | if (task == TASK_TOMBSTONE) | |
336 | goto unlock; | |
337 | ||
338 | if (task) { | |
339 | /* | |
340 | * We must be either inactive or active and the right task, | |
341 | * otherwise we're screwed, since we cannot IPI to somewhere | |
342 | * else. | |
343 | */ | |
344 | if (ctx->is_active) { | |
345 | if (WARN_ON_ONCE(task != current)) | |
346 | goto unlock; | |
347 | ||
348 | if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) | |
349 | goto unlock; | |
350 | } | |
351 | } else { | |
352 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
353 | } | |
354 | ||
355 | func(event, cpuctx, ctx, data); | |
356 | unlock: | |
357 | perf_ctx_unlock(cpuctx, task_ctx); | |
358 | } | |
359 | ||
e5d1367f SE |
360 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
361 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
362 | PERF_FLAG_PID_CGROUP |\ |
363 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 364 | |
bce38cd5 SE |
365 | /* |
366 | * branch priv levels that need permission checks | |
367 | */ | |
368 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
369 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
370 | PERF_SAMPLE_BRANCH_HV) | |
371 | ||
0b3fcf17 SE |
372 | enum event_type_t { |
373 | EVENT_FLEXIBLE = 0x1, | |
374 | EVENT_PINNED = 0x2, | |
3cbaa590 | 375 | EVENT_TIME = 0x4, |
487f05e1 AS |
376 | /* see ctx_resched() for details */ |
377 | EVENT_CPU = 0x8, | |
f06cc667 | 378 | EVENT_CGROUP = 0x10, |
0b3fcf17 SE |
379 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
380 | }; | |
381 | ||
e5d1367f SE |
382 | /* |
383 | * perf_sched_events : >0 events exist | |
e5d1367f | 384 | */ |
9107c89e PZ |
385 | |
386 | static void perf_sched_delayed(struct work_struct *work); | |
387 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
388 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
389 | static DEFINE_MUTEX(perf_sched_mutex); | |
390 | static atomic_t perf_sched_count; | |
391 | ||
f2fb6bef | 392 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
e5d1367f | 393 | |
cdd6c482 IM |
394 | static atomic_t nr_mmap_events __read_mostly; |
395 | static atomic_t nr_comm_events __read_mostly; | |
e4222673 | 396 | static atomic_t nr_namespaces_events __read_mostly; |
cdd6c482 | 397 | static atomic_t nr_task_events __read_mostly; |
948b26b6 | 398 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 399 | static atomic_t nr_switch_events __read_mostly; |
76193a94 | 400 | static atomic_t nr_ksymbol_events __read_mostly; |
6ee52e2a | 401 | static atomic_t nr_bpf_events __read_mostly; |
96aaab68 | 402 | static atomic_t nr_cgroup_events __read_mostly; |
e17d43b9 | 403 | static atomic_t nr_text_poke_events __read_mostly; |
88a16a13 | 404 | static atomic_t nr_build_id_events __read_mostly; |
9ee318a7 | 405 | |
108b02cf PZ |
406 | static LIST_HEAD(pmus); |
407 | static DEFINE_MUTEX(pmus_lock); | |
408 | static struct srcu_struct pmus_srcu; | |
a63fbed7 | 409 | static cpumask_var_t perf_online_mask; |
bdacfaf2 | 410 | static struct kmem_cache *perf_event_cache; |
108b02cf | 411 | |
0764771d | 412 | /* |
cdd6c482 | 413 | * perf event paranoia level: |
0fbdea19 IM |
414 | * -1 - not paranoid at all |
415 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 416 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 417 | * 2 - disallow kernel profiling for unpriv |
0764771d | 418 | */ |
0161028b | 419 | int sysctl_perf_event_paranoid __read_mostly = 2; |
0764771d | 420 | |
20443384 FW |
421 | /* Minimum for 512 kiB + 1 user control page */ |
422 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
423 | |
424 | /* | |
cdd6c482 | 425 | * max perf event sample rate |
df58ab24 | 426 | */ |
14c63f17 DH |
427 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
428 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
429 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
430 | ||
431 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
432 | ||
433 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
434 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
435 | ||
d9494cb4 PZ |
436 | static int perf_sample_allowed_ns __read_mostly = |
437 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 438 | |
18ab2cd3 | 439 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
440 | { |
441 | u64 tmp = perf_sample_period_ns; | |
442 | ||
443 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
444 | tmp = div_u64(tmp, 100); |
445 | if (!tmp) | |
446 | tmp = 1; | |
447 | ||
448 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 449 | } |
163ec435 | 450 | |
bd275681 | 451 | static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc); |
9e630205 | 452 | |
e6814ec3 XJ |
453 | int perf_event_max_sample_rate_handler(struct ctl_table *table, int write, |
454 | void *buffer, size_t *lenp, loff_t *ppos) | |
163ec435 | 455 | { |
1a51c5da SE |
456 | int ret; |
457 | int perf_cpu = sysctl_perf_cpu_time_max_percent; | |
ab7fdefb KL |
458 | /* |
459 | * If throttling is disabled don't allow the write: | |
460 | */ | |
1a51c5da | 461 | if (write && (perf_cpu == 100 || perf_cpu == 0)) |
ab7fdefb KL |
462 | return -EINVAL; |
463 | ||
1a51c5da SE |
464 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
465 | if (ret || !write) | |
466 | return ret; | |
467 | ||
163ec435 | 468 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
14c63f17 DH |
469 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
470 | update_perf_cpu_limits(); | |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
475 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
476 | ||
477 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
32927393 | 478 | void *buffer, size_t *lenp, loff_t *ppos) |
14c63f17 | 479 | { |
1572e45a | 480 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
14c63f17 DH |
481 | |
482 | if (ret || !write) | |
483 | return ret; | |
484 | ||
b303e7c1 PZ |
485 | if (sysctl_perf_cpu_time_max_percent == 100 || |
486 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
487 | printk(KERN_WARNING |
488 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
489 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
490 | } else { | |
491 | update_perf_cpu_limits(); | |
492 | } | |
163ec435 PZ |
493 | |
494 | return 0; | |
495 | } | |
1ccd1549 | 496 | |
14c63f17 DH |
497 | /* |
498 | * perf samples are done in some very critical code paths (NMIs). | |
499 | * If they take too much CPU time, the system can lock up and not | |
500 | * get any real work done. This will drop the sample rate when | |
501 | * we detect that events are taking too long. | |
502 | */ | |
503 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 504 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 505 | |
91a612ee PZ |
506 | static u64 __report_avg; |
507 | static u64 __report_allowed; | |
508 | ||
6a02ad66 | 509 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 510 | { |
0d87d7ec | 511 | printk_ratelimited(KERN_INFO |
91a612ee PZ |
512 | "perf: interrupt took too long (%lld > %lld), lowering " |
513 | "kernel.perf_event_max_sample_rate to %d\n", | |
514 | __report_avg, __report_allowed, | |
515 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
516 | } |
517 | ||
518 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
519 | ||
520 | void perf_sample_event_took(u64 sample_len_ns) | |
521 | { | |
91a612ee PZ |
522 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
523 | u64 running_len; | |
524 | u64 avg_len; | |
525 | u32 max; | |
14c63f17 | 526 | |
91a612ee | 527 | if (max_len == 0) |
14c63f17 DH |
528 | return; |
529 | ||
91a612ee PZ |
530 | /* Decay the counter by 1 average sample. */ |
531 | running_len = __this_cpu_read(running_sample_length); | |
532 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
533 | running_len += sample_len_ns; | |
534 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
535 | |
536 | /* | |
91a612ee PZ |
537 | * Note: this will be biased artifically low until we have |
538 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
539 | * from having to maintain a count. |
540 | */ | |
91a612ee PZ |
541 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
542 | if (avg_len <= max_len) | |
14c63f17 DH |
543 | return; |
544 | ||
91a612ee PZ |
545 | __report_avg = avg_len; |
546 | __report_allowed = max_len; | |
14c63f17 | 547 | |
91a612ee PZ |
548 | /* |
549 | * Compute a throttle threshold 25% below the current duration. | |
550 | */ | |
551 | avg_len += avg_len / 4; | |
552 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
553 | if (avg_len < max) | |
554 | max /= (u32)avg_len; | |
555 | else | |
556 | max = 1; | |
14c63f17 | 557 | |
91a612ee PZ |
558 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
559 | WRITE_ONCE(max_samples_per_tick, max); | |
560 | ||
561 | sysctl_perf_event_sample_rate = max * HZ; | |
562 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 563 | |
cd578abb | 564 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 565 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 566 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 567 | __report_avg, __report_allowed, |
cd578abb PZ |
568 | sysctl_perf_event_sample_rate); |
569 | } | |
14c63f17 DH |
570 | } |
571 | ||
cdd6c482 | 572 | static atomic64_t perf_event_id; |
a96bbc16 | 573 | |
e5d1367f SE |
574 | static void update_context_time(struct perf_event_context *ctx); |
575 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 576 | |
cdd6c482 | 577 | void __weak perf_event_print_debug(void) { } |
0793a61d | 578 | |
0b3fcf17 SE |
579 | static inline u64 perf_clock(void) |
580 | { | |
581 | return local_clock(); | |
582 | } | |
583 | ||
34f43927 PZ |
584 | static inline u64 perf_event_clock(struct perf_event *event) |
585 | { | |
586 | return event->clock(); | |
587 | } | |
588 | ||
0d3d73aa PZ |
589 | /* |
590 | * State based event timekeeping... | |
591 | * | |
592 | * The basic idea is to use event->state to determine which (if any) time | |
593 | * fields to increment with the current delta. This means we only need to | |
594 | * update timestamps when we change state or when they are explicitly requested | |
595 | * (read). | |
596 | * | |
597 | * Event groups make things a little more complicated, but not terribly so. The | |
598 | * rules for a group are that if the group leader is OFF the entire group is | |
599 | * OFF, irrespecive of what the group member states are. This results in | |
600 | * __perf_effective_state(). | |
601 | * | |
602 | * A futher ramification is that when a group leader flips between OFF and | |
603 | * !OFF, we need to update all group member times. | |
604 | * | |
605 | * | |
606 | * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we | |
607 | * need to make sure the relevant context time is updated before we try and | |
608 | * update our timestamps. | |
609 | */ | |
610 | ||
611 | static __always_inline enum perf_event_state | |
612 | __perf_effective_state(struct perf_event *event) | |
613 | { | |
614 | struct perf_event *leader = event->group_leader; | |
615 | ||
616 | if (leader->state <= PERF_EVENT_STATE_OFF) | |
617 | return leader->state; | |
618 | ||
619 | return event->state; | |
620 | } | |
621 | ||
622 | static __always_inline void | |
623 | __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running) | |
624 | { | |
625 | enum perf_event_state state = __perf_effective_state(event); | |
626 | u64 delta = now - event->tstamp; | |
627 | ||
628 | *enabled = event->total_time_enabled; | |
629 | if (state >= PERF_EVENT_STATE_INACTIVE) | |
630 | *enabled += delta; | |
631 | ||
632 | *running = event->total_time_running; | |
633 | if (state >= PERF_EVENT_STATE_ACTIVE) | |
634 | *running += delta; | |
635 | } | |
636 | ||
637 | static void perf_event_update_time(struct perf_event *event) | |
638 | { | |
639 | u64 now = perf_event_time(event); | |
640 | ||
641 | __perf_update_times(event, now, &event->total_time_enabled, | |
642 | &event->total_time_running); | |
643 | event->tstamp = now; | |
644 | } | |
645 | ||
646 | static void perf_event_update_sibling_time(struct perf_event *leader) | |
647 | { | |
648 | struct perf_event *sibling; | |
649 | ||
edb39592 | 650 | for_each_sibling_event(sibling, leader) |
0d3d73aa PZ |
651 | perf_event_update_time(sibling); |
652 | } | |
653 | ||
654 | static void | |
655 | perf_event_set_state(struct perf_event *event, enum perf_event_state state) | |
656 | { | |
657 | if (event->state == state) | |
658 | return; | |
659 | ||
660 | perf_event_update_time(event); | |
661 | /* | |
662 | * If a group leader gets enabled/disabled all its siblings | |
663 | * are affected too. | |
664 | */ | |
665 | if ((event->state < 0) ^ (state < 0)) | |
666 | perf_event_update_sibling_time(event); | |
667 | ||
668 | WRITE_ONCE(event->state, state); | |
669 | } | |
670 | ||
09f5e7dc PZ |
671 | /* |
672 | * UP store-release, load-acquire | |
673 | */ | |
674 | ||
675 | #define __store_release(ptr, val) \ | |
676 | do { \ | |
677 | barrier(); \ | |
678 | WRITE_ONCE(*(ptr), (val)); \ | |
679 | } while (0) | |
680 | ||
681 | #define __load_acquire(ptr) \ | |
682 | ({ \ | |
683 | __unqual_scalar_typeof(*(ptr)) ___p = READ_ONCE(*(ptr)); \ | |
684 | barrier(); \ | |
685 | ___p; \ | |
686 | }) | |
687 | ||
f06cc667 | 688 | static void perf_ctx_disable(struct perf_event_context *ctx, bool cgroup) |
bd275681 PZ |
689 | { |
690 | struct perf_event_pmu_context *pmu_ctx; | |
691 | ||
f06cc667 PZ |
692 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
693 | if (cgroup && !pmu_ctx->nr_cgroups) | |
694 | continue; | |
bd275681 | 695 | perf_pmu_disable(pmu_ctx->pmu); |
f06cc667 | 696 | } |
bd275681 PZ |
697 | } |
698 | ||
f06cc667 | 699 | static void perf_ctx_enable(struct perf_event_context *ctx, bool cgroup) |
bd275681 PZ |
700 | { |
701 | struct perf_event_pmu_context *pmu_ctx; | |
702 | ||
f06cc667 PZ |
703 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
704 | if (cgroup && !pmu_ctx->nr_cgroups) | |
705 | continue; | |
bd275681 | 706 | perf_pmu_enable(pmu_ctx->pmu); |
f06cc667 | 707 | } |
bd275681 PZ |
708 | } |
709 | ||
710 | static void ctx_sched_out(struct perf_event_context *ctx, enum event_type_t event_type); | |
711 | static void ctx_sched_in(struct perf_event_context *ctx, enum event_type_t event_type); | |
712 | ||
e5d1367f SE |
713 | #ifdef CONFIG_CGROUP_PERF |
714 | ||
e5d1367f SE |
715 | static inline bool |
716 | perf_cgroup_match(struct perf_event *event) | |
717 | { | |
bd275681 | 718 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
e5d1367f | 719 | |
ef824fa1 TH |
720 | /* @event doesn't care about cgroup */ |
721 | if (!event->cgrp) | |
722 | return true; | |
723 | ||
724 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
725 | if (!cpuctx->cgrp) | |
726 | return false; | |
727 | ||
728 | /* | |
729 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
730 | * also enabled for all its descendant cgroups. If @cpuctx's | |
731 | * cgroup is a descendant of @event's (the test covers identity | |
732 | * case), it's a match. | |
733 | */ | |
734 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
735 | event->cgrp->css.cgroup); | |
e5d1367f SE |
736 | } |
737 | ||
e5d1367f SE |
738 | static inline void perf_detach_cgroup(struct perf_event *event) |
739 | { | |
4e2ba650 | 740 | css_put(&event->cgrp->css); |
e5d1367f SE |
741 | event->cgrp = NULL; |
742 | } | |
743 | ||
744 | static inline int is_cgroup_event(struct perf_event *event) | |
745 | { | |
746 | return event->cgrp != NULL; | |
747 | } | |
748 | ||
749 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
750 | { | |
751 | struct perf_cgroup_info *t; | |
752 | ||
753 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
754 | return t->time; | |
755 | } | |
756 | ||
09f5e7dc | 757 | static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now) |
e5d1367f | 758 | { |
09f5e7dc | 759 | struct perf_cgroup_info *t; |
e5d1367f | 760 | |
09f5e7dc PZ |
761 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
762 | if (!__load_acquire(&t->active)) | |
763 | return t->time; | |
764 | now += READ_ONCE(t->timeoffset); | |
765 | return now; | |
766 | } | |
e5d1367f | 767 | |
09f5e7dc PZ |
768 | static inline void __update_cgrp_time(struct perf_cgroup_info *info, u64 now, bool adv) |
769 | { | |
770 | if (adv) | |
771 | info->time += now - info->timestamp; | |
e5d1367f | 772 | info->timestamp = now; |
09f5e7dc PZ |
773 | /* |
774 | * see update_context_time() | |
775 | */ | |
776 | WRITE_ONCE(info->timeoffset, info->time - info->timestamp); | |
e5d1367f SE |
777 | } |
778 | ||
09f5e7dc | 779 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx, bool final) |
e5d1367f | 780 | { |
c917e0f2 SL |
781 | struct perf_cgroup *cgrp = cpuctx->cgrp; |
782 | struct cgroup_subsys_state *css; | |
09f5e7dc | 783 | struct perf_cgroup_info *info; |
c917e0f2 SL |
784 | |
785 | if (cgrp) { | |
09f5e7dc PZ |
786 | u64 now = perf_clock(); |
787 | ||
c917e0f2 SL |
788 | for (css = &cgrp->css; css; css = css->parent) { |
789 | cgrp = container_of(css, struct perf_cgroup, css); | |
09f5e7dc PZ |
790 | info = this_cpu_ptr(cgrp->info); |
791 | ||
792 | __update_cgrp_time(info, now, true); | |
793 | if (final) | |
794 | __store_release(&info->active, 0); | |
c917e0f2 SL |
795 | } |
796 | } | |
e5d1367f SE |
797 | } |
798 | ||
799 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
800 | { | |
09f5e7dc | 801 | struct perf_cgroup_info *info; |
3f7cce3c | 802 | |
e5d1367f | 803 | /* |
3f7cce3c SE |
804 | * ensure we access cgroup data only when needed and |
805 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 806 | */ |
3f7cce3c | 807 | if (!is_cgroup_event(event)) |
e5d1367f SE |
808 | return; |
809 | ||
6875186a | 810 | info = this_cpu_ptr(event->cgrp->info); |
3f7cce3c SE |
811 | /* |
812 | * Do not update time when cgroup is not active | |
813 | */ | |
6875186a | 814 | if (info->active) |
09f5e7dc | 815 | __update_cgrp_time(info, perf_clock(), true); |
e5d1367f SE |
816 | } |
817 | ||
818 | static inline void | |
a0827713 | 819 | perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) |
e5d1367f | 820 | { |
a0827713 CZ |
821 | struct perf_event_context *ctx = &cpuctx->ctx; |
822 | struct perf_cgroup *cgrp = cpuctx->cgrp; | |
e5d1367f | 823 | struct perf_cgroup_info *info; |
c917e0f2 | 824 | struct cgroup_subsys_state *css; |
e5d1367f | 825 | |
3f7cce3c SE |
826 | /* |
827 | * ctx->lock held by caller | |
828 | * ensure we do not access cgroup data | |
829 | * unless we have the cgroup pinned (css_get) | |
830 | */ | |
a0827713 | 831 | if (!cgrp) |
e5d1367f SE |
832 | return; |
833 | ||
a0827713 | 834 | WARN_ON_ONCE(!ctx->nr_cgroups); |
c917e0f2 SL |
835 | |
836 | for (css = &cgrp->css; css; css = css->parent) { | |
837 | cgrp = container_of(css, struct perf_cgroup, css); | |
838 | info = this_cpu_ptr(cgrp->info); | |
09f5e7dc PZ |
839 | __update_cgrp_time(info, ctx->timestamp, false); |
840 | __store_release(&info->active, 1); | |
c917e0f2 | 841 | } |
e5d1367f SE |
842 | } |
843 | ||
e5d1367f SE |
844 | /* |
845 | * reschedule events based on the cgroup constraint of task. | |
e5d1367f | 846 | */ |
96492a6c | 847 | static void perf_cgroup_switch(struct task_struct *task) |
e5d1367f | 848 | { |
bd275681 | 849 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
96492a6c | 850 | struct perf_cgroup *cgrp; |
e5d1367f | 851 | |
f841b682 CZ |
852 | /* |
853 | * cpuctx->cgrp is set when the first cgroup event enabled, | |
854 | * and is cleared when the last cgroup event disabled. | |
855 | */ | |
856 | if (READ_ONCE(cpuctx->cgrp) == NULL) | |
857 | return; | |
96492a6c | 858 | |
bd275681 | 859 | WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); |
f841b682 CZ |
860 | |
861 | cgrp = perf_cgroup_from_task(task, NULL); | |
bd275681 PZ |
862 | if (READ_ONCE(cpuctx->cgrp) == cgrp) |
863 | return; | |
e5d1367f | 864 | |
bd275681 | 865 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
f06cc667 | 866 | perf_ctx_disable(&cpuctx->ctx, true); |
e5d1367f | 867 | |
f06cc667 | 868 | ctx_sched_out(&cpuctx->ctx, EVENT_ALL|EVENT_CGROUP); |
bd275681 PZ |
869 | /* |
870 | * must not be done before ctxswout due | |
871 | * to update_cgrp_time_from_cpuctx() in | |
872 | * ctx_sched_out() | |
873 | */ | |
874 | cpuctx->cgrp = cgrp; | |
875 | /* | |
876 | * set cgrp before ctxsw in to allow | |
877 | * perf_cgroup_set_timestamp() in ctx_sched_in() | |
878 | * to not have to pass task around | |
879 | */ | |
f06cc667 | 880 | ctx_sched_in(&cpuctx->ctx, EVENT_ALL|EVENT_CGROUP); |
e5d1367f | 881 | |
f06cc667 | 882 | perf_ctx_enable(&cpuctx->ctx, true); |
bd275681 | 883 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
e5d1367f SE |
884 | } |
885 | ||
c2283c93 IR |
886 | static int perf_cgroup_ensure_storage(struct perf_event *event, |
887 | struct cgroup_subsys_state *css) | |
888 | { | |
889 | struct perf_cpu_context *cpuctx; | |
890 | struct perf_event **storage; | |
891 | int cpu, heap_size, ret = 0; | |
892 | ||
893 | /* | |
894 | * Allow storage to have sufficent space for an iterator for each | |
895 | * possibly nested cgroup plus an iterator for events with no cgroup. | |
896 | */ | |
897 | for (heap_size = 1; css; css = css->parent) | |
898 | heap_size++; | |
899 | ||
900 | for_each_possible_cpu(cpu) { | |
bd275681 | 901 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); |
c2283c93 IR |
902 | if (heap_size <= cpuctx->heap_size) |
903 | continue; | |
904 | ||
905 | storage = kmalloc_node(heap_size * sizeof(struct perf_event *), | |
906 | GFP_KERNEL, cpu_to_node(cpu)); | |
907 | if (!storage) { | |
908 | ret = -ENOMEM; | |
909 | break; | |
910 | } | |
911 | ||
912 | raw_spin_lock_irq(&cpuctx->ctx.lock); | |
913 | if (cpuctx->heap_size < heap_size) { | |
914 | swap(cpuctx->heap, storage); | |
915 | if (storage == cpuctx->heap_default) | |
916 | storage = NULL; | |
917 | cpuctx->heap_size = heap_size; | |
918 | } | |
919 | raw_spin_unlock_irq(&cpuctx->ctx.lock); | |
920 | ||
921 | kfree(storage); | |
922 | } | |
923 | ||
924 | return ret; | |
925 | } | |
926 | ||
e5d1367f SE |
927 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
928 | struct perf_event_attr *attr, | |
929 | struct perf_event *group_leader) | |
930 | { | |
931 | struct perf_cgroup *cgrp; | |
932 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
933 | struct fd f = fdget(fd); |
934 | int ret = 0; | |
e5d1367f | 935 | |
2903ff01 | 936 | if (!f.file) |
e5d1367f SE |
937 | return -EBADF; |
938 | ||
b583043e | 939 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 940 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
941 | if (IS_ERR(css)) { |
942 | ret = PTR_ERR(css); | |
943 | goto out; | |
944 | } | |
e5d1367f | 945 | |
c2283c93 IR |
946 | ret = perf_cgroup_ensure_storage(event, css); |
947 | if (ret) | |
948 | goto out; | |
949 | ||
e5d1367f SE |
950 | cgrp = container_of(css, struct perf_cgroup, css); |
951 | event->cgrp = cgrp; | |
952 | ||
953 | /* | |
954 | * all events in a group must monitor | |
955 | * the same cgroup because a task belongs | |
956 | * to only one perf cgroup at a time | |
957 | */ | |
958 | if (group_leader && group_leader->cgrp != cgrp) { | |
959 | perf_detach_cgroup(event); | |
960 | ret = -EINVAL; | |
e5d1367f | 961 | } |
3db272c0 | 962 | out: |
2903ff01 | 963 | fdput(f); |
e5d1367f SE |
964 | return ret; |
965 | } | |
966 | ||
db4a8356 | 967 | static inline void |
33238c50 | 968 | perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) |
db4a8356 DCC |
969 | { |
970 | struct perf_cpu_context *cpuctx; | |
971 | ||
972 | if (!is_cgroup_event(event)) | |
973 | return; | |
974 | ||
f06cc667 PZ |
975 | event->pmu_ctx->nr_cgroups++; |
976 | ||
db4a8356 DCC |
977 | /* |
978 | * Because cgroup events are always per-cpu events, | |
07c59729 | 979 | * @ctx == &cpuctx->ctx. |
db4a8356 | 980 | */ |
07c59729 | 981 | cpuctx = container_of(ctx, struct perf_cpu_context, ctx); |
33801b94 | 982 | |
33238c50 | 983 | if (ctx->nr_cgroups++) |
33801b94 | 984 | return; |
33238c50 | 985 | |
e19cd0b6 | 986 | cpuctx->cgrp = perf_cgroup_from_task(current, ctx); |
33238c50 PZ |
987 | } |
988 | ||
989 | static inline void | |
990 | perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) | |
991 | { | |
992 | struct perf_cpu_context *cpuctx; | |
993 | ||
994 | if (!is_cgroup_event(event)) | |
33801b94 | 995 | return; |
996 | ||
f06cc667 PZ |
997 | event->pmu_ctx->nr_cgroups--; |
998 | ||
33238c50 PZ |
999 | /* |
1000 | * Because cgroup events are always per-cpu events, | |
1001 | * @ctx == &cpuctx->ctx. | |
1002 | */ | |
1003 | cpuctx = container_of(ctx, struct perf_cpu_context, ctx); | |
1004 | ||
1005 | if (--ctx->nr_cgroups) | |
1006 | return; | |
1007 | ||
e19cd0b6 | 1008 | cpuctx->cgrp = NULL; |
db4a8356 DCC |
1009 | } |
1010 | ||
e5d1367f SE |
1011 | #else /* !CONFIG_CGROUP_PERF */ |
1012 | ||
1013 | static inline bool | |
1014 | perf_cgroup_match(struct perf_event *event) | |
1015 | { | |
1016 | return true; | |
1017 | } | |
1018 | ||
1019 | static inline void perf_detach_cgroup(struct perf_event *event) | |
1020 | {} | |
1021 | ||
1022 | static inline int is_cgroup_event(struct perf_event *event) | |
1023 | { | |
1024 | return 0; | |
1025 | } | |
1026 | ||
e5d1367f SE |
1027 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
1028 | { | |
1029 | } | |
1030 | ||
09f5e7dc PZ |
1031 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx, |
1032 | bool final) | |
e5d1367f SE |
1033 | { |
1034 | } | |
1035 | ||
e5d1367f SE |
1036 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
1037 | struct perf_event_attr *attr, | |
1038 | struct perf_event *group_leader) | |
1039 | { | |
1040 | return -EINVAL; | |
1041 | } | |
1042 | ||
1043 | static inline void | |
a0827713 | 1044 | perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) |
e5d1367f SE |
1045 | { |
1046 | } | |
1047 | ||
09f5e7dc | 1048 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
e5d1367f | 1049 | { |
09f5e7dc | 1050 | return 0; |
e5d1367f SE |
1051 | } |
1052 | ||
09f5e7dc | 1053 | static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now) |
e5d1367f SE |
1054 | { |
1055 | return 0; | |
1056 | } | |
1057 | ||
db4a8356 | 1058 | static inline void |
33238c50 | 1059 | perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) |
db4a8356 DCC |
1060 | { |
1061 | } | |
1062 | ||
33238c50 PZ |
1063 | static inline void |
1064 | perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) | |
1065 | { | |
1066 | } | |
96492a6c CZ |
1067 | |
1068 | static void perf_cgroup_switch(struct task_struct *task) | |
1069 | { | |
1070 | } | |
e5d1367f SE |
1071 | #endif |
1072 | ||
9e630205 SE |
1073 | /* |
1074 | * set default to be dependent on timer tick just | |
1075 | * like original code | |
1076 | */ | |
1077 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
1078 | /* | |
8a1115ff | 1079 | * function must be called with interrupts disabled |
9e630205 | 1080 | */ |
272325c4 | 1081 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 | 1082 | { |
bd275681 | 1083 | struct perf_cpu_pmu_context *cpc; |
8d5bce0c | 1084 | bool rotations; |
9e630205 | 1085 | |
16444645 | 1086 | lockdep_assert_irqs_disabled(); |
9e630205 | 1087 | |
bd275681 PZ |
1088 | cpc = container_of(hr, struct perf_cpu_pmu_context, hrtimer); |
1089 | rotations = perf_rotate_context(cpc); | |
9e630205 | 1090 | |
bd275681 | 1091 | raw_spin_lock(&cpc->hrtimer_lock); |
4cfafd30 | 1092 | if (rotations) |
bd275681 | 1093 | hrtimer_forward_now(hr, cpc->hrtimer_interval); |
4cfafd30 | 1094 | else |
bd275681 PZ |
1095 | cpc->hrtimer_active = 0; |
1096 | raw_spin_unlock(&cpc->hrtimer_lock); | |
9e630205 | 1097 | |
4cfafd30 | 1098 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
1099 | } |
1100 | ||
bd275681 | 1101 | static void __perf_mux_hrtimer_init(struct perf_cpu_pmu_context *cpc, int cpu) |
9e630205 | 1102 | { |
bd275681 PZ |
1103 | struct hrtimer *timer = &cpc->hrtimer; |
1104 | struct pmu *pmu = cpc->epc.pmu; | |
272325c4 | 1105 | u64 interval; |
9e630205 | 1106 | |
62b85639 SE |
1107 | /* |
1108 | * check default is sane, if not set then force to | |
1109 | * default interval (1/tick) | |
1110 | */ | |
272325c4 PZ |
1111 | interval = pmu->hrtimer_interval_ms; |
1112 | if (interval < 1) | |
1113 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 1114 | |
bd275681 | 1115 | cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 1116 | |
bd275681 | 1117 | raw_spin_lock_init(&cpc->hrtimer_lock); |
30f9028b | 1118 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); |
272325c4 | 1119 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
1120 | } |
1121 | ||
bd275681 | 1122 | static int perf_mux_hrtimer_restart(struct perf_cpu_pmu_context *cpc) |
9e630205 | 1123 | { |
bd275681 | 1124 | struct hrtimer *timer = &cpc->hrtimer; |
4cfafd30 | 1125 | unsigned long flags; |
9e630205 | 1126 | |
bd275681 PZ |
1127 | raw_spin_lock_irqsave(&cpc->hrtimer_lock, flags); |
1128 | if (!cpc->hrtimer_active) { | |
1129 | cpc->hrtimer_active = 1; | |
1130 | hrtimer_forward_now(timer, cpc->hrtimer_interval); | |
30f9028b | 1131 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); |
4cfafd30 | 1132 | } |
bd275681 | 1133 | raw_spin_unlock_irqrestore(&cpc->hrtimer_lock, flags); |
9e630205 | 1134 | |
272325c4 | 1135 | return 0; |
9e630205 SE |
1136 | } |
1137 | ||
1af6239d PZ |
1138 | static int perf_mux_hrtimer_restart_ipi(void *arg) |
1139 | { | |
1140 | return perf_mux_hrtimer_restart(arg); | |
1141 | } | |
1142 | ||
33696fc0 | 1143 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1144 | { |
33696fc0 PZ |
1145 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1146 | if (!(*count)++) | |
1147 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1148 | } |
9e35ad38 | 1149 | |
33696fc0 | 1150 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1151 | { |
33696fc0 PZ |
1152 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1153 | if (!--(*count)) | |
1154 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1155 | } |
9e35ad38 | 1156 | |
bd275681 | 1157 | static void perf_assert_pmu_disabled(struct pmu *pmu) |
2fde4f94 | 1158 | { |
bd275681 | 1159 | WARN_ON_ONCE(*this_cpu_ptr(pmu->pmu_disable_count) == 0); |
9e35ad38 | 1160 | } |
9e35ad38 | 1161 | |
cdd6c482 | 1162 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1163 | { |
8c94abbb | 1164 | refcount_inc(&ctx->refcount); |
a63eaf34 PM |
1165 | } |
1166 | ||
ff9ff926 KL |
1167 | static void *alloc_task_ctx_data(struct pmu *pmu) |
1168 | { | |
217c2a63 KL |
1169 | if (pmu->task_ctx_cache) |
1170 | return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL); | |
1171 | ||
5a09928d | 1172 | return NULL; |
ff9ff926 KL |
1173 | } |
1174 | ||
1175 | static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data) | |
1176 | { | |
217c2a63 KL |
1177 | if (pmu->task_ctx_cache && task_ctx_data) |
1178 | kmem_cache_free(pmu->task_ctx_cache, task_ctx_data); | |
ff9ff926 KL |
1179 | } |
1180 | ||
4af57ef2 YZ |
1181 | static void free_ctx(struct rcu_head *head) |
1182 | { | |
1183 | struct perf_event_context *ctx; | |
1184 | ||
1185 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
4af57ef2 YZ |
1186 | kfree(ctx); |
1187 | } | |
1188 | ||
cdd6c482 | 1189 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1190 | { |
8c94abbb | 1191 | if (refcount_dec_and_test(&ctx->refcount)) { |
564c2b21 PM |
1192 | if (ctx->parent_ctx) |
1193 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1194 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1195 | put_task_struct(ctx->task); |
4af57ef2 | 1196 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1197 | } |
a63eaf34 PM |
1198 | } |
1199 | ||
f63a8daa PZ |
1200 | /* |
1201 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1202 | * perf_pmu_migrate_context() we need some magic. | |
1203 | * | |
1204 | * Those places that change perf_event::ctx will hold both | |
1205 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1206 | * | |
8b10c5e2 PZ |
1207 | * Lock ordering is by mutex address. There are two other sites where |
1208 | * perf_event_context::mutex nests and those are: | |
1209 | * | |
1210 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1211 | * perf_event_exit_event() |
1212 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1213 | * |
1214 | * - perf_event_init_context() [ parent, 0 ] | |
1215 | * inherit_task_group() | |
1216 | * inherit_group() | |
1217 | * inherit_event() | |
1218 | * perf_event_alloc() | |
1219 | * perf_init_event() | |
1220 | * perf_try_init_event() [ child , 1 ] | |
1221 | * | |
1222 | * While it appears there is an obvious deadlock here -- the parent and child | |
1223 | * nesting levels are inverted between the two. This is in fact safe because | |
1224 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1225 | * spawning task cannot (yet) exit. | |
1226 | * | |
c034f48e | 1227 | * But remember that these are parent<->child context relations, and |
8b10c5e2 PZ |
1228 | * migration does not affect children, therefore these two orderings should not |
1229 | * interact. | |
f63a8daa PZ |
1230 | * |
1231 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1232 | * because the sys_perf_event_open() case will install a new event and break | |
1233 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1234 | * concerned with cpuctx and that doesn't have children. | |
1235 | * | |
1236 | * The places that change perf_event::ctx will issue: | |
1237 | * | |
1238 | * perf_remove_from_context(); | |
1239 | * synchronize_rcu(); | |
1240 | * perf_install_in_context(); | |
1241 | * | |
1242 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1243 | * quiesce the event, after which we can install it in the new location. This | |
1244 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1245 | * while in transit. Therefore all such accessors should also acquire | |
1246 | * perf_event_context::mutex to serialize against this. | |
1247 | * | |
1248 | * However; because event->ctx can change while we're waiting to acquire | |
1249 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1250 | * function. | |
1251 | * | |
1252 | * Lock order: | |
f7cfd871 | 1253 | * exec_update_lock |
f63a8daa PZ |
1254 | * task_struct::perf_event_mutex |
1255 | * perf_event_context::mutex | |
f63a8daa | 1256 | * perf_event::child_mutex; |
07c4a776 | 1257 | * perf_event_context::lock |
f63a8daa | 1258 | * perf_event::mmap_mutex |
c1e8d7c6 | 1259 | * mmap_lock |
18736eef | 1260 | * perf_addr_filters_head::lock |
82d94856 PZ |
1261 | * |
1262 | * cpu_hotplug_lock | |
1263 | * pmus_lock | |
1264 | * cpuctx->mutex / perf_event_context::mutex | |
f63a8daa | 1265 | */ |
a83fe28e PZ |
1266 | static struct perf_event_context * |
1267 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1268 | { |
1269 | struct perf_event_context *ctx; | |
1270 | ||
1271 | again: | |
1272 | rcu_read_lock(); | |
6aa7de05 | 1273 | ctx = READ_ONCE(event->ctx); |
8c94abbb | 1274 | if (!refcount_inc_not_zero(&ctx->refcount)) { |
f63a8daa PZ |
1275 | rcu_read_unlock(); |
1276 | goto again; | |
1277 | } | |
1278 | rcu_read_unlock(); | |
1279 | ||
a83fe28e | 1280 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1281 | if (event->ctx != ctx) { |
1282 | mutex_unlock(&ctx->mutex); | |
1283 | put_ctx(ctx); | |
1284 | goto again; | |
1285 | } | |
1286 | ||
1287 | return ctx; | |
1288 | } | |
1289 | ||
a83fe28e PZ |
1290 | static inline struct perf_event_context * |
1291 | perf_event_ctx_lock(struct perf_event *event) | |
1292 | { | |
1293 | return perf_event_ctx_lock_nested(event, 0); | |
1294 | } | |
1295 | ||
f63a8daa PZ |
1296 | static void perf_event_ctx_unlock(struct perf_event *event, |
1297 | struct perf_event_context *ctx) | |
1298 | { | |
1299 | mutex_unlock(&ctx->mutex); | |
1300 | put_ctx(ctx); | |
1301 | } | |
1302 | ||
211de6eb PZ |
1303 | /* |
1304 | * This must be done under the ctx->lock, such as to serialize against | |
1305 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1306 | * calling scheduler related locks and ctx->lock nests inside those. | |
1307 | */ | |
1308 | static __must_check struct perf_event_context * | |
1309 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1310 | { |
211de6eb PZ |
1311 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1312 | ||
1313 | lockdep_assert_held(&ctx->lock); | |
1314 | ||
1315 | if (parent_ctx) | |
71a851b4 | 1316 | ctx->parent_ctx = NULL; |
5a3126d4 | 1317 | ctx->generation++; |
211de6eb PZ |
1318 | |
1319 | return parent_ctx; | |
71a851b4 PZ |
1320 | } |
1321 | ||
1d953111 ON |
1322 | static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, |
1323 | enum pid_type type) | |
6844c09d | 1324 | { |
1d953111 | 1325 | u32 nr; |
6844c09d ACM |
1326 | /* |
1327 | * only top level events have the pid namespace they were created in | |
1328 | */ | |
1329 | if (event->parent) | |
1330 | event = event->parent; | |
1331 | ||
1d953111 ON |
1332 | nr = __task_pid_nr_ns(p, type, event->ns); |
1333 | /* avoid -1 if it is idle thread or runs in another ns */ | |
1334 | if (!nr && !pid_alive(p)) | |
1335 | nr = -1; | |
1336 | return nr; | |
6844c09d ACM |
1337 | } |
1338 | ||
1d953111 | 1339 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
6844c09d | 1340 | { |
6883f81a | 1341 | return perf_event_pid_type(event, p, PIDTYPE_TGID); |
1d953111 | 1342 | } |
6844c09d | 1343 | |
1d953111 ON |
1344 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
1345 | { | |
1346 | return perf_event_pid_type(event, p, PIDTYPE_PID); | |
6844c09d ACM |
1347 | } |
1348 | ||
7f453c24 | 1349 | /* |
cdd6c482 | 1350 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1351 | * to userspace. |
1352 | */ | |
cdd6c482 | 1353 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1354 | { |
cdd6c482 | 1355 | u64 id = event->id; |
7f453c24 | 1356 | |
cdd6c482 IM |
1357 | if (event->parent) |
1358 | id = event->parent->id; | |
7f453c24 PZ |
1359 | |
1360 | return id; | |
1361 | } | |
1362 | ||
25346b93 | 1363 | /* |
cdd6c482 | 1364 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1365 | * |
c034f48e | 1366 | * This has to cope with the fact that until it is locked, |
25346b93 PM |
1367 | * the context could get moved to another task. |
1368 | */ | |
cdd6c482 | 1369 | static struct perf_event_context * |
bd275681 | 1370 | perf_lock_task_context(struct task_struct *task, unsigned long *flags) |
25346b93 | 1371 | { |
cdd6c482 | 1372 | struct perf_event_context *ctx; |
25346b93 | 1373 | |
9ed6060d | 1374 | retry: |
058ebd0e PZ |
1375 | /* |
1376 | * One of the few rules of preemptible RCU is that one cannot do | |
1377 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1378 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1379 | * rcu_read_unlock_special(). |
1380 | * | |
1381 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1382 | * side critical section has interrupts disabled. |
058ebd0e | 1383 | */ |
2fd59077 | 1384 | local_irq_save(*flags); |
058ebd0e | 1385 | rcu_read_lock(); |
bd275681 | 1386 | ctx = rcu_dereference(task->perf_event_ctxp); |
25346b93 PM |
1387 | if (ctx) { |
1388 | /* | |
1389 | * If this context is a clone of another, it might | |
1390 | * get swapped for another underneath us by | |
cdd6c482 | 1391 | * perf_event_task_sched_out, though the |
25346b93 PM |
1392 | * rcu_read_lock() protects us from any context |
1393 | * getting freed. Lock the context and check if it | |
1394 | * got swapped before we could get the lock, and retry | |
1395 | * if so. If we locked the right context, then it | |
1396 | * can't get swapped on us any more. | |
1397 | */ | |
2fd59077 | 1398 | raw_spin_lock(&ctx->lock); |
bd275681 | 1399 | if (ctx != rcu_dereference(task->perf_event_ctxp)) { |
2fd59077 | 1400 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1401 | rcu_read_unlock(); |
2fd59077 | 1402 | local_irq_restore(*flags); |
25346b93 PM |
1403 | goto retry; |
1404 | } | |
b49a9e7e | 1405 | |
63b6da39 | 1406 | if (ctx->task == TASK_TOMBSTONE || |
8c94abbb | 1407 | !refcount_inc_not_zero(&ctx->refcount)) { |
2fd59077 | 1408 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1409 | ctx = NULL; |
828b6f0e PZ |
1410 | } else { |
1411 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1412 | } |
25346b93 PM |
1413 | } |
1414 | rcu_read_unlock(); | |
2fd59077 PM |
1415 | if (!ctx) |
1416 | local_irq_restore(*flags); | |
25346b93 PM |
1417 | return ctx; |
1418 | } | |
1419 | ||
1420 | /* | |
1421 | * Get the context for a task and increment its pin_count so it | |
1422 | * can't get swapped to another task. This also increments its | |
1423 | * reference count so that the context can't get freed. | |
1424 | */ | |
8dc85d54 | 1425 | static struct perf_event_context * |
bd275681 | 1426 | perf_pin_task_context(struct task_struct *task) |
25346b93 | 1427 | { |
cdd6c482 | 1428 | struct perf_event_context *ctx; |
25346b93 PM |
1429 | unsigned long flags; |
1430 | ||
bd275681 | 1431 | ctx = perf_lock_task_context(task, &flags); |
25346b93 PM |
1432 | if (ctx) { |
1433 | ++ctx->pin_count; | |
e625cce1 | 1434 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1435 | } |
1436 | return ctx; | |
1437 | } | |
1438 | ||
cdd6c482 | 1439 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1440 | { |
1441 | unsigned long flags; | |
1442 | ||
e625cce1 | 1443 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1444 | --ctx->pin_count; |
e625cce1 | 1445 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1446 | } |
1447 | ||
f67218c3 PZ |
1448 | /* |
1449 | * Update the record of the current time in a context. | |
1450 | */ | |
09f5e7dc | 1451 | static void __update_context_time(struct perf_event_context *ctx, bool adv) |
f67218c3 PZ |
1452 | { |
1453 | u64 now = perf_clock(); | |
1454 | ||
f3c0eba2 PZ |
1455 | lockdep_assert_held(&ctx->lock); |
1456 | ||
09f5e7dc PZ |
1457 | if (adv) |
1458 | ctx->time += now - ctx->timestamp; | |
f67218c3 | 1459 | ctx->timestamp = now; |
09f5e7dc PZ |
1460 | |
1461 | /* | |
1462 | * The above: time' = time + (now - timestamp), can be re-arranged | |
1463 | * into: time` = now + (time - timestamp), which gives a single value | |
1464 | * offset to compute future time without locks on. | |
1465 | * | |
1466 | * See perf_event_time_now(), which can be used from NMI context where | |
1467 | * it's (obviously) not possible to acquire ctx->lock in order to read | |
1468 | * both the above values in a consistent manner. | |
1469 | */ | |
1470 | WRITE_ONCE(ctx->timeoffset, ctx->time - ctx->timestamp); | |
1471 | } | |
1472 | ||
1473 | static void update_context_time(struct perf_event_context *ctx) | |
1474 | { | |
1475 | __update_context_time(ctx, true); | |
f67218c3 PZ |
1476 | } |
1477 | ||
4158755d SE |
1478 | static u64 perf_event_time(struct perf_event *event) |
1479 | { | |
1480 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f | 1481 | |
09f5e7dc PZ |
1482 | if (unlikely(!ctx)) |
1483 | return 0; | |
1484 | ||
e5d1367f SE |
1485 | if (is_cgroup_event(event)) |
1486 | return perf_cgroup_event_time(event); | |
1487 | ||
09f5e7dc PZ |
1488 | return ctx->time; |
1489 | } | |
1490 | ||
1491 | static u64 perf_event_time_now(struct perf_event *event, u64 now) | |
1492 | { | |
1493 | struct perf_event_context *ctx = event->ctx; | |
1494 | ||
1495 | if (unlikely(!ctx)) | |
1496 | return 0; | |
1497 | ||
1498 | if (is_cgroup_event(event)) | |
1499 | return perf_cgroup_event_time_now(event, now); | |
1500 | ||
1501 | if (!(__load_acquire(&ctx->is_active) & EVENT_TIME)) | |
1502 | return ctx->time; | |
1503 | ||
1504 | now += READ_ONCE(ctx->timeoffset); | |
1505 | return now; | |
4158755d SE |
1506 | } |
1507 | ||
487f05e1 AS |
1508 | static enum event_type_t get_event_type(struct perf_event *event) |
1509 | { | |
1510 | struct perf_event_context *ctx = event->ctx; | |
1511 | enum event_type_t event_type; | |
1512 | ||
1513 | lockdep_assert_held(&ctx->lock); | |
1514 | ||
3bda69c1 AS |
1515 | /* |
1516 | * It's 'group type', really, because if our group leader is | |
1517 | * pinned, so are we. | |
1518 | */ | |
1519 | if (event->group_leader != event) | |
1520 | event = event->group_leader; | |
1521 | ||
487f05e1 AS |
1522 | event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; |
1523 | if (!ctx->task) | |
1524 | event_type |= EVENT_CPU; | |
1525 | ||
1526 | return event_type; | |
1527 | } | |
1528 | ||
8e1a2031 | 1529 | /* |
161c85fa | 1530 | * Helper function to initialize event group nodes. |
8e1a2031 | 1531 | */ |
161c85fa | 1532 | static void init_event_group(struct perf_event *event) |
8e1a2031 AB |
1533 | { |
1534 | RB_CLEAR_NODE(&event->group_node); | |
1535 | event->group_index = 0; | |
1536 | } | |
1537 | ||
1538 | /* | |
1539 | * Extract pinned or flexible groups from the context | |
161c85fa | 1540 | * based on event attrs bits. |
8e1a2031 AB |
1541 | */ |
1542 | static struct perf_event_groups * | |
1543 | get_event_groups(struct perf_event *event, struct perf_event_context *ctx) | |
889ff015 FW |
1544 | { |
1545 | if (event->attr.pinned) | |
1546 | return &ctx->pinned_groups; | |
1547 | else | |
1548 | return &ctx->flexible_groups; | |
1549 | } | |
1550 | ||
8e1a2031 | 1551 | /* |
161c85fa | 1552 | * Helper function to initializes perf_event_group trees. |
8e1a2031 | 1553 | */ |
161c85fa | 1554 | static void perf_event_groups_init(struct perf_event_groups *groups) |
8e1a2031 AB |
1555 | { |
1556 | groups->tree = RB_ROOT; | |
1557 | groups->index = 0; | |
1558 | } | |
1559 | ||
a3b89864 PZ |
1560 | static inline struct cgroup *event_cgroup(const struct perf_event *event) |
1561 | { | |
1562 | struct cgroup *cgroup = NULL; | |
1563 | ||
1564 | #ifdef CONFIG_CGROUP_PERF | |
1565 | if (event->cgrp) | |
1566 | cgroup = event->cgrp->css.cgroup; | |
1567 | #endif | |
1568 | ||
1569 | return cgroup; | |
1570 | } | |
1571 | ||
8e1a2031 AB |
1572 | /* |
1573 | * Compare function for event groups; | |
161c85fa PZ |
1574 | * |
1575 | * Implements complex key that first sorts by CPU and then by virtual index | |
1576 | * which provides ordering when rotating groups for the same CPU. | |
8e1a2031 | 1577 | */ |
a3b89864 | 1578 | static __always_inline int |
bd275681 PZ |
1579 | perf_event_groups_cmp(const int left_cpu, const struct pmu *left_pmu, |
1580 | const struct cgroup *left_cgroup, const u64 left_group_index, | |
1581 | const struct perf_event *right) | |
8e1a2031 | 1582 | { |
a3b89864 PZ |
1583 | if (left_cpu < right->cpu) |
1584 | return -1; | |
1585 | if (left_cpu > right->cpu) | |
1586 | return 1; | |
161c85fa | 1587 | |
bd275681 PZ |
1588 | if (left_pmu) { |
1589 | if (left_pmu < right->pmu_ctx->pmu) | |
1590 | return -1; | |
1591 | if (left_pmu > right->pmu_ctx->pmu) | |
1592 | return 1; | |
1593 | } | |
1594 | ||
95ed6c70 | 1595 | #ifdef CONFIG_CGROUP_PERF |
a3b89864 PZ |
1596 | { |
1597 | const struct cgroup *right_cgroup = event_cgroup(right); | |
1598 | ||
1599 | if (left_cgroup != right_cgroup) { | |
1600 | if (!left_cgroup) { | |
1601 | /* | |
1602 | * Left has no cgroup but right does, no | |
1603 | * cgroups come first. | |
1604 | */ | |
1605 | return -1; | |
1606 | } | |
1607 | if (!right_cgroup) { | |
1608 | /* | |
1609 | * Right has no cgroup but left does, no | |
1610 | * cgroups come first. | |
1611 | */ | |
1612 | return 1; | |
1613 | } | |
1614 | /* Two dissimilar cgroups, order by id. */ | |
1615 | if (cgroup_id(left_cgroup) < cgroup_id(right_cgroup)) | |
1616 | return -1; | |
1617 | ||
1618 | return 1; | |
95ed6c70 | 1619 | } |
95ed6c70 IR |
1620 | } |
1621 | #endif | |
1622 | ||
a3b89864 PZ |
1623 | if (left_group_index < right->group_index) |
1624 | return -1; | |
1625 | if (left_group_index > right->group_index) | |
1626 | return 1; | |
1627 | ||
1628 | return 0; | |
1629 | } | |
161c85fa | 1630 | |
a3b89864 PZ |
1631 | #define __node_2_pe(node) \ |
1632 | rb_entry((node), struct perf_event, group_node) | |
1633 | ||
1634 | static inline bool __group_less(struct rb_node *a, const struct rb_node *b) | |
1635 | { | |
1636 | struct perf_event *e = __node_2_pe(a); | |
bd275681 PZ |
1637 | return perf_event_groups_cmp(e->cpu, e->pmu_ctx->pmu, event_cgroup(e), |
1638 | e->group_index, __node_2_pe(b)) < 0; | |
a3b89864 PZ |
1639 | } |
1640 | ||
1641 | struct __group_key { | |
1642 | int cpu; | |
bd275681 | 1643 | struct pmu *pmu; |
a3b89864 PZ |
1644 | struct cgroup *cgroup; |
1645 | }; | |
1646 | ||
1647 | static inline int __group_cmp(const void *key, const struct rb_node *node) | |
1648 | { | |
1649 | const struct __group_key *a = key; | |
1650 | const struct perf_event *b = __node_2_pe(node); | |
1651 | ||
bd275681 PZ |
1652 | /* partial/subtree match: @cpu, @pmu, @cgroup; ignore: @group_index */ |
1653 | return perf_event_groups_cmp(a->cpu, a->pmu, a->cgroup, b->group_index, b); | |
1654 | } | |
1655 | ||
1656 | static inline int | |
1657 | __group_cmp_ignore_cgroup(const void *key, const struct rb_node *node) | |
1658 | { | |
1659 | const struct __group_key *a = key; | |
1660 | const struct perf_event *b = __node_2_pe(node); | |
1661 | ||
1662 | /* partial/subtree match: @cpu, @pmu, ignore: @cgroup, @group_index */ | |
1663 | return perf_event_groups_cmp(a->cpu, a->pmu, event_cgroup(b), | |
1664 | b->group_index, b); | |
8e1a2031 AB |
1665 | } |
1666 | ||
1667 | /* | |
bd275681 PZ |
1668 | * Insert @event into @groups' tree; using |
1669 | * {@event->cpu, @event->pmu_ctx->pmu, event_cgroup(@event), ++@groups->index} | |
1670 | * as key. This places it last inside the {cpu,pmu,cgroup} subtree. | |
8e1a2031 AB |
1671 | */ |
1672 | static void | |
1673 | perf_event_groups_insert(struct perf_event_groups *groups, | |
161c85fa | 1674 | struct perf_event *event) |
8e1a2031 | 1675 | { |
8e1a2031 AB |
1676 | event->group_index = ++groups->index; |
1677 | ||
a3b89864 | 1678 | rb_add(&event->group_node, &groups->tree, __group_less); |
8e1a2031 AB |
1679 | } |
1680 | ||
1681 | /* | |
161c85fa | 1682 | * Helper function to insert event into the pinned or flexible groups. |
8e1a2031 AB |
1683 | */ |
1684 | static void | |
1685 | add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1686 | { | |
1687 | struct perf_event_groups *groups; | |
1688 | ||
1689 | groups = get_event_groups(event, ctx); | |
1690 | perf_event_groups_insert(groups, event); | |
1691 | } | |
1692 | ||
1693 | /* | |
161c85fa | 1694 | * Delete a group from a tree. |
8e1a2031 AB |
1695 | */ |
1696 | static void | |
1697 | perf_event_groups_delete(struct perf_event_groups *groups, | |
161c85fa | 1698 | struct perf_event *event) |
8e1a2031 | 1699 | { |
161c85fa PZ |
1700 | WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) || |
1701 | RB_EMPTY_ROOT(&groups->tree)); | |
8e1a2031 | 1702 | |
161c85fa | 1703 | rb_erase(&event->group_node, &groups->tree); |
8e1a2031 AB |
1704 | init_event_group(event); |
1705 | } | |
1706 | ||
1707 | /* | |
161c85fa | 1708 | * Helper function to delete event from its groups. |
8e1a2031 AB |
1709 | */ |
1710 | static void | |
1711 | del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1712 | { | |
1713 | struct perf_event_groups *groups; | |
1714 | ||
1715 | groups = get_event_groups(event, ctx); | |
1716 | perf_event_groups_delete(groups, event); | |
1717 | } | |
1718 | ||
1719 | /* | |
bd275681 | 1720 | * Get the leftmost event in the {cpu,pmu,cgroup} subtree. |
8e1a2031 AB |
1721 | */ |
1722 | static struct perf_event * | |
95ed6c70 | 1723 | perf_event_groups_first(struct perf_event_groups *groups, int cpu, |
bd275681 | 1724 | struct pmu *pmu, struct cgroup *cgrp) |
8e1a2031 | 1725 | { |
a3b89864 PZ |
1726 | struct __group_key key = { |
1727 | .cpu = cpu, | |
bd275681 | 1728 | .pmu = pmu, |
a3b89864 PZ |
1729 | .cgroup = cgrp, |
1730 | }; | |
1731 | struct rb_node *node; | |
95ed6c70 | 1732 | |
a3b89864 PZ |
1733 | node = rb_find_first(&key, &groups->tree, __group_cmp); |
1734 | if (node) | |
1735 | return __node_2_pe(node); | |
8e1a2031 | 1736 | |
a3b89864 | 1737 | return NULL; |
8e1a2031 AB |
1738 | } |
1739 | ||
1cac7b1a | 1740 | static struct perf_event * |
bd275681 | 1741 | perf_event_groups_next(struct perf_event *event, struct pmu *pmu) |
1cac7b1a | 1742 | { |
a3b89864 PZ |
1743 | struct __group_key key = { |
1744 | .cpu = event->cpu, | |
bd275681 | 1745 | .pmu = pmu, |
a3b89864 PZ |
1746 | .cgroup = event_cgroup(event), |
1747 | }; | |
1748 | struct rb_node *next; | |
1cac7b1a | 1749 | |
a3b89864 PZ |
1750 | next = rb_next_match(&key, &event->group_node, __group_cmp); |
1751 | if (next) | |
1752 | return __node_2_pe(next); | |
95ed6c70 | 1753 | |
a3b89864 | 1754 | return NULL; |
1cac7b1a PZ |
1755 | } |
1756 | ||
bd275681 PZ |
1757 | #define perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) \ |
1758 | for (event = perf_event_groups_first(groups, cpu, pmu, NULL); \ | |
1759 | event; event = perf_event_groups_next(event, pmu)) | |
1760 | ||
8e1a2031 | 1761 | /* |
161c85fa | 1762 | * Iterate through the whole groups tree. |
8e1a2031 | 1763 | */ |
6e6804d2 PZ |
1764 | #define perf_event_groups_for_each(event, groups) \ |
1765 | for (event = rb_entry_safe(rb_first(&((groups)->tree)), \ | |
1766 | typeof(*event), group_node); event; \ | |
1767 | event = rb_entry_safe(rb_next(&event->group_node), \ | |
1768 | typeof(*event), group_node)) | |
8e1a2031 | 1769 | |
fccc714b | 1770 | /* |
788faab7 | 1771 | * Add an event from the lists for its context. |
fccc714b PZ |
1772 | * Must be called with ctx->mutex and ctx->lock held. |
1773 | */ | |
04289bb9 | 1774 | static void |
cdd6c482 | 1775 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1776 | { |
c994d613 PZ |
1777 | lockdep_assert_held(&ctx->lock); |
1778 | ||
8a49542c PZ |
1779 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1780 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 | 1781 | |
0d3d73aa PZ |
1782 | event->tstamp = perf_event_time(event); |
1783 | ||
04289bb9 | 1784 | /* |
8a49542c PZ |
1785 | * If we're a stand alone event or group leader, we go to the context |
1786 | * list, group events are kept attached to the group so that | |
1787 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1788 | */ |
8a49542c | 1789 | if (event->group_leader == event) { |
4ff6a8de | 1790 | event->group_caps = event->event_caps; |
8e1a2031 | 1791 | add_event_to_groups(event, ctx); |
5c148194 | 1792 | } |
592903cd | 1793 | |
cdd6c482 IM |
1794 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1795 | ctx->nr_events++; | |
82ff0c02 RH |
1796 | if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) |
1797 | ctx->nr_user++; | |
cdd6c482 | 1798 | if (event->attr.inherit_stat) |
bfbd3381 | 1799 | ctx->nr_stat++; |
5a3126d4 | 1800 | |
33238c50 PZ |
1801 | if (event->state > PERF_EVENT_STATE_OFF) |
1802 | perf_cgroup_event_enable(event, ctx); | |
1803 | ||
5a3126d4 | 1804 | ctx->generation++; |
bd275681 | 1805 | event->pmu_ctx->nr_events++; |
04289bb9 IM |
1806 | } |
1807 | ||
0231bb53 JO |
1808 | /* |
1809 | * Initialize event state based on the perf_event_attr::disabled. | |
1810 | */ | |
1811 | static inline void perf_event__state_init(struct perf_event *event) | |
1812 | { | |
1813 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1814 | PERF_EVENT_STATE_INACTIVE; | |
1815 | } | |
1816 | ||
382c27f4 | 1817 | static int __perf_event_read_size(u64 read_format, int nr_siblings) |
c320c7b7 ACM |
1818 | { |
1819 | int entry = sizeof(u64); /* value */ | |
1820 | int size = 0; | |
1821 | int nr = 1; | |
1822 | ||
382c27f4 | 1823 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
c320c7b7 ACM |
1824 | size += sizeof(u64); |
1825 | ||
382c27f4 | 1826 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
c320c7b7 ACM |
1827 | size += sizeof(u64); |
1828 | ||
382c27f4 | 1829 | if (read_format & PERF_FORMAT_ID) |
c320c7b7 ACM |
1830 | entry += sizeof(u64); |
1831 | ||
382c27f4 | 1832 | if (read_format & PERF_FORMAT_LOST) |
119a784c NK |
1833 | entry += sizeof(u64); |
1834 | ||
382c27f4 | 1835 | if (read_format & PERF_FORMAT_GROUP) { |
a723968c | 1836 | nr += nr_siblings; |
c320c7b7 ACM |
1837 | size += sizeof(u64); |
1838 | } | |
1839 | ||
382c27f4 PZ |
1840 | /* |
1841 | * Since perf_event_validate_size() limits this to 16k and inhibits | |
1842 | * adding more siblings, this will never overflow. | |
1843 | */ | |
1844 | return size + nr * entry; | |
c320c7b7 ACM |
1845 | } |
1846 | ||
a723968c | 1847 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1848 | { |
1849 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1850 | u16 size = 0; |
1851 | ||
c320c7b7 ACM |
1852 | if (sample_type & PERF_SAMPLE_IP) |
1853 | size += sizeof(data->ip); | |
1854 | ||
6844c09d ACM |
1855 | if (sample_type & PERF_SAMPLE_ADDR) |
1856 | size += sizeof(data->addr); | |
1857 | ||
1858 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1859 | size += sizeof(data->period); | |
1860 | ||
2a6c6b7d KL |
1861 | if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) |
1862 | size += sizeof(data->weight.full); | |
c3feedf2 | 1863 | |
6844c09d ACM |
1864 | if (sample_type & PERF_SAMPLE_READ) |
1865 | size += event->read_size; | |
1866 | ||
d6be9ad6 SE |
1867 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1868 | size += sizeof(data->data_src.val); | |
1869 | ||
fdfbbd07 AK |
1870 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1871 | size += sizeof(data->txn); | |
1872 | ||
fc7ce9c7 KL |
1873 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
1874 | size += sizeof(data->phys_addr); | |
1875 | ||
6546b19f NK |
1876 | if (sample_type & PERF_SAMPLE_CGROUP) |
1877 | size += sizeof(data->cgroup); | |
1878 | ||
8d97e718 KL |
1879 | if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) |
1880 | size += sizeof(data->data_page_size); | |
1881 | ||
995f088e SE |
1882 | if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) |
1883 | size += sizeof(data->code_page_size); | |
1884 | ||
6844c09d ACM |
1885 | event->header_size = size; |
1886 | } | |
1887 | ||
a723968c PZ |
1888 | /* |
1889 | * Called at perf_event creation and when events are attached/detached from a | |
1890 | * group. | |
1891 | */ | |
1892 | static void perf_event__header_size(struct perf_event *event) | |
1893 | { | |
382c27f4 PZ |
1894 | event->read_size = |
1895 | __perf_event_read_size(event->attr.read_format, | |
1896 | event->group_leader->nr_siblings); | |
a723968c PZ |
1897 | __perf_event_header_size(event, event->attr.sample_type); |
1898 | } | |
1899 | ||
6844c09d ACM |
1900 | static void perf_event__id_header_size(struct perf_event *event) |
1901 | { | |
1902 | struct perf_sample_data *data; | |
1903 | u64 sample_type = event->attr.sample_type; | |
1904 | u16 size = 0; | |
1905 | ||
c320c7b7 ACM |
1906 | if (sample_type & PERF_SAMPLE_TID) |
1907 | size += sizeof(data->tid_entry); | |
1908 | ||
1909 | if (sample_type & PERF_SAMPLE_TIME) | |
1910 | size += sizeof(data->time); | |
1911 | ||
ff3d527c AH |
1912 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1913 | size += sizeof(data->id); | |
1914 | ||
c320c7b7 ACM |
1915 | if (sample_type & PERF_SAMPLE_ID) |
1916 | size += sizeof(data->id); | |
1917 | ||
1918 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1919 | size += sizeof(data->stream_id); | |
1920 | ||
1921 | if (sample_type & PERF_SAMPLE_CPU) | |
1922 | size += sizeof(data->cpu_entry); | |
1923 | ||
6844c09d | 1924 | event->id_header_size = size; |
c320c7b7 ACM |
1925 | } |
1926 | ||
382c27f4 PZ |
1927 | /* |
1928 | * Check that adding an event to the group does not result in anybody | |
1929 | * overflowing the 64k event limit imposed by the output buffer. | |
1930 | * | |
1931 | * Specifically, check that the read_size for the event does not exceed 16k, | |
1932 | * read_size being the one term that grows with groups size. Since read_size | |
1933 | * depends on per-event read_format, also (re)check the existing events. | |
1934 | * | |
1935 | * This leaves 48k for the constant size fields and things like callchains, | |
1936 | * branch stacks and register sets. | |
1937 | */ | |
a723968c PZ |
1938 | static bool perf_event_validate_size(struct perf_event *event) |
1939 | { | |
382c27f4 | 1940 | struct perf_event *sibling, *group_leader = event->group_leader; |
a723968c | 1941 | |
382c27f4 PZ |
1942 | if (__perf_event_read_size(event->attr.read_format, |
1943 | group_leader->nr_siblings + 1) > 16*1024) | |
a723968c PZ |
1944 | return false; |
1945 | ||
382c27f4 PZ |
1946 | if (__perf_event_read_size(group_leader->attr.read_format, |
1947 | group_leader->nr_siblings + 1) > 16*1024) | |
1948 | return false; | |
1949 | ||
7e2c1e4b MR |
1950 | /* |
1951 | * When creating a new group leader, group_leader->ctx is initialized | |
1952 | * after the size has been validated, but we cannot safely use | |
1953 | * for_each_sibling_event() until group_leader->ctx is set. A new group | |
1954 | * leader cannot have any siblings yet, so we can safely skip checking | |
1955 | * the non-existent siblings. | |
1956 | */ | |
1957 | if (event == group_leader) | |
1958 | return true; | |
1959 | ||
382c27f4 PZ |
1960 | for_each_sibling_event(sibling, group_leader) { |
1961 | if (__perf_event_read_size(sibling->attr.read_format, | |
1962 | group_leader->nr_siblings + 1) > 16*1024) | |
1963 | return false; | |
1964 | } | |
1965 | ||
a723968c PZ |
1966 | return true; |
1967 | } | |
1968 | ||
8a49542c PZ |
1969 | static void perf_group_attach(struct perf_event *event) |
1970 | { | |
c320c7b7 | 1971 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1972 | |
a76a82a3 PZ |
1973 | lockdep_assert_held(&event->ctx->lock); |
1974 | ||
74c3337c | 1975 | /* |
bd275681 PZ |
1976 | * We can have double attach due to group movement (move_group) in |
1977 | * perf_event_open(). | |
74c3337c PZ |
1978 | */ |
1979 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1980 | return; | |
1981 | ||
8a49542c PZ |
1982 | event->attach_state |= PERF_ATTACH_GROUP; |
1983 | ||
1984 | if (group_leader == event) | |
1985 | return; | |
1986 | ||
652884fe PZ |
1987 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1988 | ||
4ff6a8de | 1989 | group_leader->group_caps &= event->event_caps; |
8a49542c | 1990 | |
8343aae6 | 1991 | list_add_tail(&event->sibling_list, &group_leader->sibling_list); |
8a49542c | 1992 | group_leader->nr_siblings++; |
32671e37 | 1993 | group_leader->group_generation++; |
c320c7b7 ACM |
1994 | |
1995 | perf_event__header_size(group_leader); | |
1996 | ||
edb39592 | 1997 | for_each_sibling_event(pos, group_leader) |
c320c7b7 | 1998 | perf_event__header_size(pos); |
8a49542c PZ |
1999 | } |
2000 | ||
a63eaf34 | 2001 | /* |
788faab7 | 2002 | * Remove an event from the lists for its context. |
fccc714b | 2003 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 2004 | */ |
04289bb9 | 2005 | static void |
cdd6c482 | 2006 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 2007 | { |
652884fe PZ |
2008 | WARN_ON_ONCE(event->ctx != ctx); |
2009 | lockdep_assert_held(&ctx->lock); | |
2010 | ||
8a49542c PZ |
2011 | /* |
2012 | * We can have double detach due to exit/hot-unplug + close. | |
2013 | */ | |
2014 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 2015 | return; |
8a49542c PZ |
2016 | |
2017 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
2018 | ||
cdd6c482 | 2019 | ctx->nr_events--; |
82ff0c02 RH |
2020 | if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) |
2021 | ctx->nr_user--; | |
cdd6c482 | 2022 | if (event->attr.inherit_stat) |
bfbd3381 | 2023 | ctx->nr_stat--; |
8bc20959 | 2024 | |
cdd6c482 | 2025 | list_del_rcu(&event->event_entry); |
04289bb9 | 2026 | |
8a49542c | 2027 | if (event->group_leader == event) |
8e1a2031 | 2028 | del_event_from_groups(event, ctx); |
5c148194 | 2029 | |
b2e74a26 SE |
2030 | /* |
2031 | * If event was in error state, then keep it | |
2032 | * that way, otherwise bogus counts will be | |
2033 | * returned on read(). The only way to get out | |
2034 | * of error state is by explicit re-enabling | |
2035 | * of the event | |
2036 | */ | |
33238c50 PZ |
2037 | if (event->state > PERF_EVENT_STATE_OFF) { |
2038 | perf_cgroup_event_disable(event, ctx); | |
0d3d73aa | 2039 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); |
33238c50 | 2040 | } |
5a3126d4 PZ |
2041 | |
2042 | ctx->generation++; | |
bd275681 | 2043 | event->pmu_ctx->nr_events--; |
050735b0 PZ |
2044 | } |
2045 | ||
ab43762e AS |
2046 | static int |
2047 | perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event) | |
2048 | { | |
2049 | if (!has_aux(aux_event)) | |
2050 | return 0; | |
2051 | ||
2052 | if (!event->pmu->aux_output_match) | |
2053 | return 0; | |
2054 | ||
2055 | return event->pmu->aux_output_match(aux_event); | |
2056 | } | |
2057 | ||
2058 | static void put_event(struct perf_event *event); | |
2059 | static void event_sched_out(struct perf_event *event, | |
ab43762e AS |
2060 | struct perf_event_context *ctx); |
2061 | ||
2062 | static void perf_put_aux_event(struct perf_event *event) | |
2063 | { | |
2064 | struct perf_event_context *ctx = event->ctx; | |
ab43762e AS |
2065 | struct perf_event *iter; |
2066 | ||
2067 | /* | |
2068 | * If event uses aux_event tear down the link | |
2069 | */ | |
2070 | if (event->aux_event) { | |
2071 | iter = event->aux_event; | |
2072 | event->aux_event = NULL; | |
2073 | put_event(iter); | |
2074 | return; | |
2075 | } | |
2076 | ||
2077 | /* | |
2078 | * If the event is an aux_event, tear down all links to | |
2079 | * it from other events. | |
2080 | */ | |
2081 | for_each_sibling_event(iter, event->group_leader) { | |
2082 | if (iter->aux_event != event) | |
2083 | continue; | |
2084 | ||
2085 | iter->aux_event = NULL; | |
2086 | put_event(event); | |
2087 | ||
2088 | /* | |
2089 | * If it's ACTIVE, schedule it out and put it into ERROR | |
2090 | * state so that we don't try to schedule it again. Note | |
2091 | * that perf_event_enable() will clear the ERROR status. | |
2092 | */ | |
bd275681 | 2093 | event_sched_out(iter, ctx); |
ab43762e AS |
2094 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); |
2095 | } | |
2096 | } | |
2097 | ||
a4faf00d AS |
2098 | static bool perf_need_aux_event(struct perf_event *event) |
2099 | { | |
2100 | return !!event->attr.aux_output || !!event->attr.aux_sample_size; | |
2101 | } | |
2102 | ||
ab43762e AS |
2103 | static int perf_get_aux_event(struct perf_event *event, |
2104 | struct perf_event *group_leader) | |
2105 | { | |
2106 | /* | |
2107 | * Our group leader must be an aux event if we want to be | |
2108 | * an aux_output. This way, the aux event will precede its | |
2109 | * aux_output events in the group, and therefore will always | |
2110 | * schedule first. | |
2111 | */ | |
2112 | if (!group_leader) | |
2113 | return 0; | |
2114 | ||
a4faf00d AS |
2115 | /* |
2116 | * aux_output and aux_sample_size are mutually exclusive. | |
2117 | */ | |
2118 | if (event->attr.aux_output && event->attr.aux_sample_size) | |
2119 | return 0; | |
2120 | ||
2121 | if (event->attr.aux_output && | |
2122 | !perf_aux_output_match(event, group_leader)) | |
2123 | return 0; | |
2124 | ||
2125 | if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux) | |
ab43762e AS |
2126 | return 0; |
2127 | ||
2128 | if (!atomic_long_inc_not_zero(&group_leader->refcount)) | |
2129 | return 0; | |
2130 | ||
2131 | /* | |
2132 | * Link aux_outputs to their aux event; this is undone in | |
2133 | * perf_group_detach() by perf_put_aux_event(). When the | |
2134 | * group in torn down, the aux_output events loose their | |
2135 | * link to the aux_event and can't schedule any more. | |
2136 | */ | |
2137 | event->aux_event = group_leader; | |
2138 | ||
2139 | return 1; | |
2140 | } | |
2141 | ||
ab6f824c PZ |
2142 | static inline struct list_head *get_event_list(struct perf_event *event) |
2143 | { | |
bd275681 PZ |
2144 | return event->attr.pinned ? &event->pmu_ctx->pinned_active : |
2145 | &event->pmu_ctx->flexible_active; | |
ab6f824c PZ |
2146 | } |
2147 | ||
9f0c4fa1 KL |
2148 | /* |
2149 | * Events that have PERF_EV_CAP_SIBLING require being part of a group and | |
2150 | * cannot exist on their own, schedule them out and move them into the ERROR | |
2151 | * state. Also see _perf_event_enable(), it will not be able to recover | |
2152 | * this ERROR state. | |
2153 | */ | |
2154 | static inline void perf_remove_sibling_event(struct perf_event *event) | |
2155 | { | |
bd275681 | 2156 | event_sched_out(event, event->ctx); |
9f0c4fa1 KL |
2157 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); |
2158 | } | |
2159 | ||
8a49542c | 2160 | static void perf_group_detach(struct perf_event *event) |
050735b0 | 2161 | { |
9f0c4fa1 | 2162 | struct perf_event *leader = event->group_leader; |
050735b0 | 2163 | struct perf_event *sibling, *tmp; |
6668128a | 2164 | struct perf_event_context *ctx = event->ctx; |
8a49542c | 2165 | |
6668128a | 2166 | lockdep_assert_held(&ctx->lock); |
a76a82a3 | 2167 | |
8a49542c PZ |
2168 | /* |
2169 | * We can have double detach due to exit/hot-unplug + close. | |
2170 | */ | |
2171 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
2172 | return; | |
2173 | ||
2174 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
2175 | ||
ab43762e AS |
2176 | perf_put_aux_event(event); |
2177 | ||
8a49542c PZ |
2178 | /* |
2179 | * If this is a sibling, remove it from its group. | |
2180 | */ | |
9f0c4fa1 | 2181 | if (leader != event) { |
8343aae6 | 2182 | list_del_init(&event->sibling_list); |
8a49542c | 2183 | event->group_leader->nr_siblings--; |
32671e37 | 2184 | event->group_leader->group_generation++; |
c320c7b7 | 2185 | goto out; |
8a49542c PZ |
2186 | } |
2187 | ||
04289bb9 | 2188 | /* |
cdd6c482 IM |
2189 | * If this was a group event with sibling events then |
2190 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 2191 | * to whatever list we are on. |
04289bb9 | 2192 | */ |
8343aae6 | 2193 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { |
8e1a2031 | 2194 | |
9f0c4fa1 KL |
2195 | if (sibling->event_caps & PERF_EV_CAP_SIBLING) |
2196 | perf_remove_sibling_event(sibling); | |
2197 | ||
04289bb9 | 2198 | sibling->group_leader = sibling; |
24868367 | 2199 | list_del_init(&sibling->sibling_list); |
d6f962b5 FW |
2200 | |
2201 | /* Inherit group flags from the previous leader */ | |
4ff6a8de | 2202 | sibling->group_caps = event->group_caps; |
652884fe | 2203 | |
fd0815f6 | 2204 | if (sibling->attach_state & PERF_ATTACH_CONTEXT) { |
8e1a2031 | 2205 | add_event_to_groups(sibling, event->ctx); |
6668128a | 2206 | |
ab6f824c PZ |
2207 | if (sibling->state == PERF_EVENT_STATE_ACTIVE) |
2208 | list_add_tail(&sibling->active_list, get_event_list(sibling)); | |
8e1a2031 AB |
2209 | } |
2210 | ||
652884fe | 2211 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
04289bb9 | 2212 | } |
c320c7b7 ACM |
2213 | |
2214 | out: | |
9f0c4fa1 | 2215 | for_each_sibling_event(tmp, leader) |
c320c7b7 | 2216 | perf_event__header_size(tmp); |
9f0c4fa1 KL |
2217 | |
2218 | perf_event__header_size(leader); | |
04289bb9 IM |
2219 | } |
2220 | ||
ef54c1a4 PZ |
2221 | static void sync_child_event(struct perf_event *child_event); |
2222 | ||
2223 | static void perf_child_detach(struct perf_event *event) | |
2224 | { | |
2225 | struct perf_event *parent_event = event->parent; | |
2226 | ||
2227 | if (!(event->attach_state & PERF_ATTACH_CHILD)) | |
2228 | return; | |
2229 | ||
2230 | event->attach_state &= ~PERF_ATTACH_CHILD; | |
2231 | ||
2232 | if (WARN_ON_ONCE(!parent_event)) | |
2233 | return; | |
2234 | ||
2235 | lockdep_assert_held(&parent_event->child_mutex); | |
2236 | ||
2237 | sync_child_event(event); | |
2238 | list_del_init(&event->child_list); | |
2239 | } | |
2240 | ||
fadfe7be JO |
2241 | static bool is_orphaned_event(struct perf_event *event) |
2242 | { | |
a69b0ca4 | 2243 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
2244 | } |
2245 | ||
fa66f07a SE |
2246 | static inline int |
2247 | event_filter_match(struct perf_event *event) | |
2248 | { | |
0b8f1e2e | 2249 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
bd275681 | 2250 | perf_cgroup_match(event); |
fa66f07a SE |
2251 | } |
2252 | ||
9ffcfa6f | 2253 | static void |
bd275681 | 2254 | event_sched_out(struct perf_event *event, struct perf_event_context *ctx) |
3b6f9e5c | 2255 | { |
bd275681 PZ |
2256 | struct perf_event_pmu_context *epc = event->pmu_ctx; |
2257 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); | |
0d3d73aa | 2258 | enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; |
652884fe | 2259 | |
bd275681 PZ |
2260 | // XXX cpc serialization, probably per-cpu IRQ disabled |
2261 | ||
652884fe PZ |
2262 | WARN_ON_ONCE(event->ctx != ctx); |
2263 | lockdep_assert_held(&ctx->lock); | |
2264 | ||
cdd6c482 | 2265 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 2266 | return; |
3b6f9e5c | 2267 | |
6668128a PZ |
2268 | /* |
2269 | * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but | |
2270 | * we can schedule events _OUT_ individually through things like | |
2271 | * __perf_remove_from_context(). | |
2272 | */ | |
2273 | list_del_init(&event->active_list); | |
2274 | ||
44377277 AS |
2275 | perf_pmu_disable(event->pmu); |
2276 | ||
28a967c3 PZ |
2277 | event->pmu->del(event, 0); |
2278 | event->oncpu = -1; | |
0d3d73aa | 2279 | |
ca6c2132 PZ |
2280 | if (event->pending_disable) { |
2281 | event->pending_disable = 0; | |
33238c50 | 2282 | perf_cgroup_event_disable(event, ctx); |
0d3d73aa | 2283 | state = PERF_EVENT_STATE_OFF; |
970892a9 | 2284 | } |
ca6c2132 PZ |
2285 | |
2286 | if (event->pending_sigtrap) { | |
2287 | bool dec = true; | |
2288 | ||
2289 | event->pending_sigtrap = 0; | |
2290 | if (state != PERF_EVENT_STATE_OFF && | |
2291 | !event->pending_work) { | |
2292 | event->pending_work = 1; | |
2293 | dec = false; | |
517e6a30 | 2294 | WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); |
ca6c2132 PZ |
2295 | task_work_add(current, &event->pending_task, TWA_RESUME); |
2296 | } | |
2297 | if (dec) | |
2298 | local_dec(&event->ctx->nr_pending); | |
2299 | } | |
2300 | ||
0d3d73aa | 2301 | perf_event_set_state(event, state); |
3b6f9e5c | 2302 | |
cdd6c482 | 2303 | if (!is_software_event(event)) |
bd275681 | 2304 | cpc->active_oncpu--; |
0259bf63 | 2305 | if (event->attr.freq && event->attr.sample_freq) { |
0f5a2601 | 2306 | ctx->nr_freq--; |
0259bf63 NK |
2307 | epc->nr_freq--; |
2308 | } | |
bd275681 PZ |
2309 | if (event->attr.exclusive || !cpc->active_oncpu) |
2310 | cpc->exclusive = 0; | |
44377277 AS |
2311 | |
2312 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
2313 | } |
2314 | ||
d859e29f | 2315 | static void |
bd275681 | 2316 | group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx) |
d859e29f | 2317 | { |
cdd6c482 | 2318 | struct perf_event *event; |
0d3d73aa PZ |
2319 | |
2320 | if (group_event->state != PERF_EVENT_STATE_ACTIVE) | |
2321 | return; | |
d859e29f | 2322 | |
bd275681 | 2323 | perf_assert_pmu_disabled(group_event->pmu_ctx->pmu); |
3f005e7d | 2324 | |
bd275681 | 2325 | event_sched_out(group_event, ctx); |
d859e29f PM |
2326 | |
2327 | /* | |
2328 | * Schedule out siblings (if any): | |
2329 | */ | |
edb39592 | 2330 | for_each_sibling_event(event, group_event) |
bd275681 | 2331 | event_sched_out(event, ctx); |
d859e29f PM |
2332 | } |
2333 | ||
45a0e07a | 2334 | #define DETACH_GROUP 0x01UL |
ef54c1a4 | 2335 | #define DETACH_CHILD 0x02UL |
517e6a30 | 2336 | #define DETACH_DEAD 0x04UL |
0017960f | 2337 | |
0793a61d | 2338 | /* |
cdd6c482 | 2339 | * Cross CPU call to remove a performance event |
0793a61d | 2340 | * |
cdd6c482 | 2341 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
2342 | * remove it from the context list. |
2343 | */ | |
fae3fde6 PZ |
2344 | static void |
2345 | __perf_remove_from_context(struct perf_event *event, | |
2346 | struct perf_cpu_context *cpuctx, | |
2347 | struct perf_event_context *ctx, | |
2348 | void *info) | |
0793a61d | 2349 | { |
bd275681 | 2350 | struct perf_event_pmu_context *pmu_ctx = event->pmu_ctx; |
45a0e07a | 2351 | unsigned long flags = (unsigned long)info; |
0793a61d | 2352 | |
3c5c8711 PZ |
2353 | if (ctx->is_active & EVENT_TIME) { |
2354 | update_context_time(ctx); | |
09f5e7dc | 2355 | update_cgrp_time_from_cpuctx(cpuctx, false); |
3c5c8711 PZ |
2356 | } |
2357 | ||
517e6a30 PZ |
2358 | /* |
2359 | * Ensure event_sched_out() switches to OFF, at the very least | |
2360 | * this avoids raising perf_pending_task() at this time. | |
2361 | */ | |
2362 | if (flags & DETACH_DEAD) | |
2363 | event->pending_disable = 1; | |
bd275681 | 2364 | event_sched_out(event, ctx); |
45a0e07a | 2365 | if (flags & DETACH_GROUP) |
46ce0fe9 | 2366 | perf_group_detach(event); |
ef54c1a4 PZ |
2367 | if (flags & DETACH_CHILD) |
2368 | perf_child_detach(event); | |
cdd6c482 | 2369 | list_del_event(event, ctx); |
517e6a30 PZ |
2370 | if (flags & DETACH_DEAD) |
2371 | event->state = PERF_EVENT_STATE_DEAD; | |
39a43640 | 2372 | |
bd275681 PZ |
2373 | if (!pmu_ctx->nr_events) { |
2374 | pmu_ctx->rotate_necessary = 0; | |
2375 | ||
2376 | if (ctx->task && ctx->is_active) { | |
2377 | struct perf_cpu_pmu_context *cpc; | |
2378 | ||
2379 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
2380 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
2381 | cpc->task_epc = NULL; | |
2382 | } | |
2383 | } | |
2384 | ||
39a43640 | 2385 | if (!ctx->nr_events && ctx->is_active) { |
09f5e7dc PZ |
2386 | if (ctx == &cpuctx->ctx) |
2387 | update_cgrp_time_from_cpuctx(cpuctx, true); | |
2388 | ||
64ce3126 | 2389 | ctx->is_active = 0; |
39a43640 PZ |
2390 | if (ctx->task) { |
2391 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2392 | cpuctx->task_ctx = NULL; | |
2393 | } | |
64ce3126 | 2394 | } |
0793a61d TG |
2395 | } |
2396 | ||
0793a61d | 2397 | /* |
cdd6c482 | 2398 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 2399 | * |
cdd6c482 IM |
2400 | * If event->ctx is a cloned context, callers must make sure that |
2401 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
2402 | * remains valid. This is OK when called from perf_release since |
2403 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 2404 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 2405 | * context has been detached from its task. |
0793a61d | 2406 | */ |
45a0e07a | 2407 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 2408 | { |
a76a82a3 PZ |
2409 | struct perf_event_context *ctx = event->ctx; |
2410 | ||
2411 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 2412 | |
a76a82a3 | 2413 | /* |
ef54c1a4 PZ |
2414 | * Because of perf_event_exit_task(), perf_remove_from_context() ought |
2415 | * to work in the face of TASK_TOMBSTONE, unlike every other | |
2416 | * event_function_call() user. | |
a76a82a3 | 2417 | */ |
ef54c1a4 | 2418 | raw_spin_lock_irq(&ctx->lock); |
bd275681 PZ |
2419 | if (!ctx->is_active) { |
2420 | __perf_remove_from_context(event, this_cpu_ptr(&perf_cpu_context), | |
ef54c1a4 | 2421 | ctx, (void *)flags); |
a76a82a3 | 2422 | raw_spin_unlock_irq(&ctx->lock); |
ef54c1a4 | 2423 | return; |
a76a82a3 | 2424 | } |
ef54c1a4 PZ |
2425 | raw_spin_unlock_irq(&ctx->lock); |
2426 | ||
2427 | event_function_call(event, __perf_remove_from_context, (void *)flags); | |
0793a61d TG |
2428 | } |
2429 | ||
d859e29f | 2430 | /* |
cdd6c482 | 2431 | * Cross CPU call to disable a performance event |
d859e29f | 2432 | */ |
fae3fde6 PZ |
2433 | static void __perf_event_disable(struct perf_event *event, |
2434 | struct perf_cpu_context *cpuctx, | |
2435 | struct perf_event_context *ctx, | |
2436 | void *info) | |
7b648018 | 2437 | { |
fae3fde6 PZ |
2438 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
2439 | return; | |
7b648018 | 2440 | |
3c5c8711 PZ |
2441 | if (ctx->is_active & EVENT_TIME) { |
2442 | update_context_time(ctx); | |
2443 | update_cgrp_time_from_event(event); | |
2444 | } | |
2445 | ||
bd275681 PZ |
2446 | perf_pmu_disable(event->pmu_ctx->pmu); |
2447 | ||
fae3fde6 | 2448 | if (event == event->group_leader) |
bd275681 | 2449 | group_sched_out(event, ctx); |
fae3fde6 | 2450 | else |
bd275681 | 2451 | event_sched_out(event, ctx); |
0d3d73aa PZ |
2452 | |
2453 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); | |
33238c50 | 2454 | perf_cgroup_event_disable(event, ctx); |
bd275681 PZ |
2455 | |
2456 | perf_pmu_enable(event->pmu_ctx->pmu); | |
7b648018 PZ |
2457 | } |
2458 | ||
d859e29f | 2459 | /* |
788faab7 | 2460 | * Disable an event. |
c93f7669 | 2461 | * |
cdd6c482 IM |
2462 | * If event->ctx is a cloned context, callers must make sure that |
2463 | * every task struct that event->ctx->task could possibly point to | |
9f014e3a | 2464 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2465 | * perf_event_for_each_child or perf_event_for_each because they |
2466 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
2467 | * goes to exit will block in perf_event_exit_event(). |
2468 | * | |
ca6c2132 | 2469 | * When called from perf_pending_irq it's OK because event->ctx |
c93f7669 | 2470 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 2471 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 2472 | */ |
f63a8daa | 2473 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 2474 | { |
cdd6c482 | 2475 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2476 | |
e625cce1 | 2477 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 2478 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 2479 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2480 | return; |
53cfbf59 | 2481 | } |
e625cce1 | 2482 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2483 | |
fae3fde6 PZ |
2484 | event_function_call(event, __perf_event_disable, NULL); |
2485 | } | |
2486 | ||
2487 | void perf_event_disable_local(struct perf_event *event) | |
2488 | { | |
2489 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 2490 | } |
f63a8daa PZ |
2491 | |
2492 | /* | |
2493 | * Strictly speaking kernel users cannot create groups and therefore this | |
2494 | * interface does not need the perf_event_ctx_lock() magic. | |
2495 | */ | |
2496 | void perf_event_disable(struct perf_event *event) | |
2497 | { | |
2498 | struct perf_event_context *ctx; | |
2499 | ||
2500 | ctx = perf_event_ctx_lock(event); | |
2501 | _perf_event_disable(event); | |
2502 | perf_event_ctx_unlock(event, ctx); | |
2503 | } | |
dcfce4a0 | 2504 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2505 | |
5aab90ce JO |
2506 | void perf_event_disable_inatomic(struct perf_event *event) |
2507 | { | |
ca6c2132 PZ |
2508 | event->pending_disable = 1; |
2509 | irq_work_queue(&event->pending_irq); | |
5aab90ce JO |
2510 | } |
2511 | ||
4fe757dd PZ |
2512 | #define MAX_INTERRUPTS (~0ULL) |
2513 | ||
2514 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2515 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2516 | |
235c7fc7 | 2517 | static int |
bd275681 | 2518 | event_sched_in(struct perf_event *event, struct perf_event_context *ctx) |
235c7fc7 | 2519 | { |
bd275681 PZ |
2520 | struct perf_event_pmu_context *epc = event->pmu_ctx; |
2521 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); | |
44377277 | 2522 | int ret = 0; |
4158755d | 2523 | |
ab6f824c PZ |
2524 | WARN_ON_ONCE(event->ctx != ctx); |
2525 | ||
63342411 PZ |
2526 | lockdep_assert_held(&ctx->lock); |
2527 | ||
cdd6c482 | 2528 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2529 | return 0; |
2530 | ||
95ff4ca2 AS |
2531 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2532 | /* | |
0c1cbc18 PZ |
2533 | * Order event::oncpu write to happen before the ACTIVE state is |
2534 | * visible. This allows perf_event_{stop,read}() to observe the correct | |
2535 | * ->oncpu if it sees ACTIVE. | |
95ff4ca2 AS |
2536 | */ |
2537 | smp_wmb(); | |
0d3d73aa | 2538 | perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); |
4fe757dd PZ |
2539 | |
2540 | /* | |
2541 | * Unthrottle events, since we scheduled we might have missed several | |
2542 | * ticks already, also for a heavily scheduling task there is little | |
2543 | * guarantee it'll get a tick in a timely manner. | |
2544 | */ | |
2545 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2546 | perf_log_throttle(event, 1); | |
2547 | event->hw.interrupts = 0; | |
2548 | } | |
2549 | ||
44377277 AS |
2550 | perf_pmu_disable(event->pmu); |
2551 | ||
ec0d7729 AS |
2552 | perf_log_itrace_start(event); |
2553 | ||
a4eaf7f1 | 2554 | if (event->pmu->add(event, PERF_EF_START)) { |
0d3d73aa | 2555 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
cdd6c482 | 2556 | event->oncpu = -1; |
44377277 AS |
2557 | ret = -EAGAIN; |
2558 | goto out; | |
235c7fc7 IM |
2559 | } |
2560 | ||
cdd6c482 | 2561 | if (!is_software_event(event)) |
bd275681 | 2562 | cpc->active_oncpu++; |
0259bf63 | 2563 | if (event->attr.freq && event->attr.sample_freq) { |
0f5a2601 | 2564 | ctx->nr_freq++; |
0259bf63 NK |
2565 | epc->nr_freq++; |
2566 | } | |
cdd6c482 | 2567 | if (event->attr.exclusive) |
bd275681 | 2568 | cpc->exclusive = 1; |
3b6f9e5c | 2569 | |
44377277 AS |
2570 | out: |
2571 | perf_pmu_enable(event->pmu); | |
2572 | ||
2573 | return ret; | |
235c7fc7 IM |
2574 | } |
2575 | ||
6751b71e | 2576 | static int |
bd275681 | 2577 | group_sched_in(struct perf_event *group_event, struct perf_event_context *ctx) |
6751b71e | 2578 | { |
6bde9b6c | 2579 | struct perf_event *event, *partial_group = NULL; |
bd275681 | 2580 | struct pmu *pmu = group_event->pmu_ctx->pmu; |
6751b71e | 2581 | |
cdd6c482 | 2582 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2583 | return 0; |
2584 | ||
fbbe0701 | 2585 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2586 | |
bd275681 | 2587 | if (event_sched_in(group_event, ctx)) |
251ff2d4 | 2588 | goto error; |
6751b71e PM |
2589 | |
2590 | /* | |
2591 | * Schedule in siblings as one group (if any): | |
2592 | */ | |
edb39592 | 2593 | for_each_sibling_event(event, group_event) { |
bd275681 | 2594 | if (event_sched_in(event, ctx)) { |
cdd6c482 | 2595 | partial_group = event; |
6751b71e PM |
2596 | goto group_error; |
2597 | } | |
2598 | } | |
2599 | ||
9ffcfa6f | 2600 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2601 | return 0; |
9ffcfa6f | 2602 | |
6751b71e PM |
2603 | group_error: |
2604 | /* | |
2605 | * Groups can be scheduled in as one unit only, so undo any | |
2606 | * partial group before returning: | |
0d3d73aa | 2607 | * The events up to the failed event are scheduled out normally. |
6751b71e | 2608 | */ |
edb39592 | 2609 | for_each_sibling_event(event, group_event) { |
cdd6c482 | 2610 | if (event == partial_group) |
0d3d73aa | 2611 | break; |
d7842da4 | 2612 | |
bd275681 | 2613 | event_sched_out(event, ctx); |
6751b71e | 2614 | } |
bd275681 | 2615 | event_sched_out(group_event, ctx); |
6751b71e | 2616 | |
251ff2d4 | 2617 | error: |
ad5133b7 | 2618 | pmu->cancel_txn(pmu); |
6751b71e PM |
2619 | return -EAGAIN; |
2620 | } | |
2621 | ||
3b6f9e5c | 2622 | /* |
cdd6c482 | 2623 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2624 | */ |
bd275681 | 2625 | static int group_can_go_on(struct perf_event *event, int can_add_hw) |
3b6f9e5c | 2626 | { |
bd275681 PZ |
2627 | struct perf_event_pmu_context *epc = event->pmu_ctx; |
2628 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); | |
2629 | ||
3b6f9e5c | 2630 | /* |
cdd6c482 | 2631 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2632 | */ |
4ff6a8de | 2633 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2634 | return 1; |
2635 | /* | |
2636 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2637 | * events can go on. |
3b6f9e5c | 2638 | */ |
bd275681 | 2639 | if (cpc->exclusive) |
3b6f9e5c PM |
2640 | return 0; |
2641 | /* | |
2642 | * If this group is exclusive and there are already | |
cdd6c482 | 2643 | * events on the CPU, it can't go on. |
3b6f9e5c | 2644 | */ |
1908dc91 | 2645 | if (event->attr.exclusive && !list_empty(get_event_list(event))) |
3b6f9e5c PM |
2646 | return 0; |
2647 | /* | |
2648 | * Otherwise, try to add it if all previous groups were able | |
2649 | * to go on. | |
2650 | */ | |
2651 | return can_add_hw; | |
2652 | } | |
2653 | ||
cdd6c482 IM |
2654 | static void add_event_to_ctx(struct perf_event *event, |
2655 | struct perf_event_context *ctx) | |
53cfbf59 | 2656 | { |
cdd6c482 | 2657 | list_add_event(event, ctx); |
8a49542c | 2658 | perf_group_attach(event); |
53cfbf59 PM |
2659 | } |
2660 | ||
bd275681 PZ |
2661 | static void task_ctx_sched_out(struct perf_event_context *ctx, |
2662 | enum event_type_t event_type) | |
bd2afa49 | 2663 | { |
bd275681 PZ |
2664 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
2665 | ||
bd2afa49 PZ |
2666 | if (!cpuctx->task_ctx) |
2667 | return; | |
2668 | ||
2669 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2670 | return; | |
2671 | ||
bd275681 | 2672 | ctx_sched_out(ctx, event_type); |
bd2afa49 PZ |
2673 | } |
2674 | ||
dce5855b | 2675 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
a0827713 | 2676 | struct perf_event_context *ctx) |
dce5855b | 2677 | { |
bd275681 | 2678 | ctx_sched_in(&cpuctx->ctx, EVENT_PINNED); |
dce5855b | 2679 | if (ctx) |
bd275681 PZ |
2680 | ctx_sched_in(ctx, EVENT_PINNED); |
2681 | ctx_sched_in(&cpuctx->ctx, EVENT_FLEXIBLE); | |
dce5855b | 2682 | if (ctx) |
bd275681 | 2683 | ctx_sched_in(ctx, EVENT_FLEXIBLE); |
dce5855b PZ |
2684 | } |
2685 | ||
487f05e1 AS |
2686 | /* |
2687 | * We want to maintain the following priority of scheduling: | |
2688 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2689 | * - task pinned (EVENT_PINNED) | |
2690 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2691 | * - task flexible (EVENT_FLEXIBLE). | |
2692 | * | |
2693 | * In order to avoid unscheduling and scheduling back in everything every | |
2694 | * time an event is added, only do it for the groups of equal priority and | |
2695 | * below. | |
2696 | * | |
2697 | * This can be called after a batch operation on task events, in which case | |
2698 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2699 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2700 | */ | |
bd275681 PZ |
2701 | /* |
2702 | * XXX: ctx_resched() reschedule entire perf_event_context while adding new | |
2703 | * event to the context or enabling existing event in the context. We can | |
2704 | * probably optimize it by rescheduling only affected pmu_ctx. | |
2705 | */ | |
3e349507 | 2706 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2707 | struct perf_event_context *task_ctx, |
2708 | enum event_type_t event_type) | |
0017960f | 2709 | { |
487f05e1 AS |
2710 | bool cpu_event = !!(event_type & EVENT_CPU); |
2711 | ||
2712 | /* | |
2713 | * If pinned groups are involved, flexible groups also need to be | |
2714 | * scheduled out. | |
2715 | */ | |
2716 | if (event_type & EVENT_PINNED) | |
2717 | event_type |= EVENT_FLEXIBLE; | |
2718 | ||
bd275681 | 2719 | event_type &= EVENT_ALL; |
bd903afe | 2720 | |
f06cc667 | 2721 | perf_ctx_disable(&cpuctx->ctx, false); |
bd275681 | 2722 | if (task_ctx) { |
f06cc667 | 2723 | perf_ctx_disable(task_ctx, false); |
bd275681 PZ |
2724 | task_ctx_sched_out(task_ctx, event_type); |
2725 | } | |
487f05e1 AS |
2726 | |
2727 | /* | |
2728 | * Decide which cpu ctx groups to schedule out based on the types | |
2729 | * of events that caused rescheduling: | |
2730 | * - EVENT_CPU: schedule out corresponding groups; | |
2731 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2732 | * - otherwise, do nothing more. | |
2733 | */ | |
2734 | if (cpu_event) | |
bd275681 PZ |
2735 | ctx_sched_out(&cpuctx->ctx, event_type); |
2736 | else if (event_type & EVENT_PINNED) | |
2737 | ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); | |
487f05e1 | 2738 | |
a0827713 | 2739 | perf_event_sched_in(cpuctx, task_ctx); |
bd275681 | 2740 | |
f06cc667 | 2741 | perf_ctx_enable(&cpuctx->ctx, false); |
bd275681 | 2742 | if (task_ctx) |
f06cc667 | 2743 | perf_ctx_enable(task_ctx, false); |
0017960f PZ |
2744 | } |
2745 | ||
c68d224e SE |
2746 | void perf_pmu_resched(struct pmu *pmu) |
2747 | { | |
bd275681 | 2748 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
c68d224e SE |
2749 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
2750 | ||
2751 | perf_ctx_lock(cpuctx, task_ctx); | |
2752 | ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU); | |
2753 | perf_ctx_unlock(cpuctx, task_ctx); | |
2754 | } | |
2755 | ||
0793a61d | 2756 | /* |
cdd6c482 | 2757 | * Cross CPU call to install and enable a performance event |
682076ae | 2758 | * |
a096309b PZ |
2759 | * Very similar to remote_function() + event_function() but cannot assume that |
2760 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2761 | */ |
fe4b04fa | 2762 | static int __perf_install_in_context(void *info) |
0793a61d | 2763 | { |
a096309b PZ |
2764 | struct perf_event *event = info; |
2765 | struct perf_event_context *ctx = event->ctx; | |
bd275681 | 2766 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
2c29ef0f | 2767 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2768 | bool reprogram = true; |
a096309b | 2769 | int ret = 0; |
0793a61d | 2770 | |
63b6da39 | 2771 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2772 | if (ctx->task) { |
b58f6b0d PZ |
2773 | raw_spin_lock(&ctx->lock); |
2774 | task_ctx = ctx; | |
a096309b | 2775 | |
63cae12b | 2776 | reprogram = (ctx->task == current); |
b58f6b0d | 2777 | |
39a43640 | 2778 | /* |
63cae12b PZ |
2779 | * If the task is running, it must be running on this CPU, |
2780 | * otherwise we cannot reprogram things. | |
2781 | * | |
2782 | * If its not running, we don't care, ctx->lock will | |
2783 | * serialize against it becoming runnable. | |
39a43640 | 2784 | */ |
63cae12b PZ |
2785 | if (task_curr(ctx->task) && !reprogram) { |
2786 | ret = -ESRCH; | |
2787 | goto unlock; | |
2788 | } | |
a096309b | 2789 | |
63cae12b | 2790 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2791 | } else if (task_ctx) { |
2792 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2793 | } |
b58f6b0d | 2794 | |
33801b94 | 2795 | #ifdef CONFIG_CGROUP_PERF |
33238c50 | 2796 | if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) { |
33801b94 | 2797 | /* |
2798 | * If the current cgroup doesn't match the event's | |
2799 | * cgroup, we should not try to schedule it. | |
2800 | */ | |
2801 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); | |
2802 | reprogram = cgroup_is_descendant(cgrp->css.cgroup, | |
2803 | event->cgrp->css.cgroup); | |
2804 | } | |
2805 | #endif | |
2806 | ||
63cae12b | 2807 | if (reprogram) { |
bd275681 | 2808 | ctx_sched_out(ctx, EVENT_TIME); |
a096309b | 2809 | add_event_to_ctx(event, ctx); |
487f05e1 | 2810 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2811 | } else { |
2812 | add_event_to_ctx(event, ctx); | |
2813 | } | |
2814 | ||
63b6da39 | 2815 | unlock: |
2c29ef0f | 2816 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2817 | |
a096309b | 2818 | return ret; |
0793a61d TG |
2819 | } |
2820 | ||
8a58ddae AS |
2821 | static bool exclusive_event_installable(struct perf_event *event, |
2822 | struct perf_event_context *ctx); | |
2823 | ||
0793a61d | 2824 | /* |
a096309b PZ |
2825 | * Attach a performance event to a context. |
2826 | * | |
2827 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2828 | */ |
2829 | static void | |
cdd6c482 IM |
2830 | perf_install_in_context(struct perf_event_context *ctx, |
2831 | struct perf_event *event, | |
0793a61d TG |
2832 | int cpu) |
2833 | { | |
a096309b | 2834 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2835 | |
fe4b04fa PZ |
2836 | lockdep_assert_held(&ctx->mutex); |
2837 | ||
8a58ddae AS |
2838 | WARN_ON_ONCE(!exclusive_event_installable(event, ctx)); |
2839 | ||
0cda4c02 | 2840 | if (event->cpu != -1) |
bd275681 | 2841 | WARN_ON_ONCE(event->cpu != cpu); |
c3f00c70 | 2842 | |
0b8f1e2e PZ |
2843 | /* |
2844 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2845 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2846 | */ | |
2847 | smp_store_release(&event->ctx, ctx); | |
2848 | ||
db0503e4 PZ |
2849 | /* |
2850 | * perf_event_attr::disabled events will not run and can be initialized | |
2851 | * without IPI. Except when this is the first event for the context, in | |
2852 | * that case we need the magic of the IPI to set ctx->is_active. | |
2853 | * | |
2854 | * The IOC_ENABLE that is sure to follow the creation of a disabled | |
2855 | * event will issue the IPI and reprogram the hardware. | |
2856 | */ | |
c5de60cd NK |
2857 | if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && |
2858 | ctx->nr_events && !is_cgroup_event(event)) { | |
db0503e4 PZ |
2859 | raw_spin_lock_irq(&ctx->lock); |
2860 | if (ctx->task == TASK_TOMBSTONE) { | |
2861 | raw_spin_unlock_irq(&ctx->lock); | |
2862 | return; | |
2863 | } | |
2864 | add_event_to_ctx(event, ctx); | |
2865 | raw_spin_unlock_irq(&ctx->lock); | |
2866 | return; | |
2867 | } | |
2868 | ||
a096309b PZ |
2869 | if (!task) { |
2870 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2871 | return; | |
2872 | } | |
2873 | ||
2874 | /* | |
2875 | * Should not happen, we validate the ctx is still alive before calling. | |
2876 | */ | |
2877 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2878 | return; | |
2879 | ||
39a43640 PZ |
2880 | /* |
2881 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2882 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2883 | * |
2884 | * Instead we use task_curr(), which tells us if the task is running. | |
2885 | * However, since we use task_curr() outside of rq::lock, we can race | |
2886 | * against the actual state. This means the result can be wrong. | |
2887 | * | |
2888 | * If we get a false positive, we retry, this is harmless. | |
2889 | * | |
2890 | * If we get a false negative, things are complicated. If we are after | |
2891 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2892 | * value must be correct. If we're before, it doesn't matter since | |
2893 | * perf_event_context_sched_in() will program the counter. | |
2894 | * | |
2895 | * However, this hinges on the remote context switch having observed | |
2896 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2897 | * ctx::lock in perf_event_context_sched_in(). | |
2898 | * | |
2899 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2900 | * we know any future context switch of task must see the | |
2901 | * perf_event_ctpx[] store. | |
39a43640 | 2902 | */ |
63cae12b | 2903 | |
63b6da39 | 2904 | /* |
63cae12b PZ |
2905 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2906 | * task_cpu() load, such that if the IPI then does not find the task | |
2907 | * running, a future context switch of that task must observe the | |
2908 | * store. | |
63b6da39 | 2909 | */ |
63cae12b PZ |
2910 | smp_mb(); |
2911 | again: | |
2912 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2913 | return; |
2914 | ||
2915 | raw_spin_lock_irq(&ctx->lock); | |
2916 | task = ctx->task; | |
84c4e620 | 2917 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2918 | /* |
2919 | * Cannot happen because we already checked above (which also | |
2920 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2921 | * against perf_event_exit_task_context(). | |
2922 | */ | |
63b6da39 PZ |
2923 | raw_spin_unlock_irq(&ctx->lock); |
2924 | return; | |
2925 | } | |
39a43640 | 2926 | /* |
63cae12b PZ |
2927 | * If the task is not running, ctx->lock will avoid it becoming so, |
2928 | * thus we can safely install the event. | |
39a43640 | 2929 | */ |
63cae12b PZ |
2930 | if (task_curr(task)) { |
2931 | raw_spin_unlock_irq(&ctx->lock); | |
2932 | goto again; | |
2933 | } | |
2934 | add_event_to_ctx(event, ctx); | |
2935 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2936 | } |
2937 | ||
d859e29f | 2938 | /* |
cdd6c482 | 2939 | * Cross CPU call to enable a performance event |
d859e29f | 2940 | */ |
fae3fde6 PZ |
2941 | static void __perf_event_enable(struct perf_event *event, |
2942 | struct perf_cpu_context *cpuctx, | |
2943 | struct perf_event_context *ctx, | |
2944 | void *info) | |
04289bb9 | 2945 | { |
cdd6c482 | 2946 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2947 | struct perf_event_context *task_ctx; |
04289bb9 | 2948 | |
6e801e01 PZ |
2949 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2950 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2951 | return; |
3cbed429 | 2952 | |
bd2afa49 | 2953 | if (ctx->is_active) |
bd275681 | 2954 | ctx_sched_out(ctx, EVENT_TIME); |
bd2afa49 | 2955 | |
0d3d73aa | 2956 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
33238c50 | 2957 | perf_cgroup_event_enable(event, ctx); |
04289bb9 | 2958 | |
fae3fde6 PZ |
2959 | if (!ctx->is_active) |
2960 | return; | |
2961 | ||
e5d1367f | 2962 | if (!event_filter_match(event)) { |
bd275681 | 2963 | ctx_sched_in(ctx, EVENT_TIME); |
fae3fde6 | 2964 | return; |
e5d1367f | 2965 | } |
f4c4176f | 2966 | |
04289bb9 | 2967 | /* |
cdd6c482 | 2968 | * If the event is in a group and isn't the group leader, |
d859e29f | 2969 | * then don't put it on unless the group is on. |
04289bb9 | 2970 | */ |
bd2afa49 | 2971 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
bd275681 | 2972 | ctx_sched_in(ctx, EVENT_TIME); |
fae3fde6 | 2973 | return; |
bd2afa49 | 2974 | } |
fe4b04fa | 2975 | |
fae3fde6 PZ |
2976 | task_ctx = cpuctx->task_ctx; |
2977 | if (ctx->task) | |
2978 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2979 | |
487f05e1 | 2980 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2981 | } |
2982 | ||
d859e29f | 2983 | /* |
788faab7 | 2984 | * Enable an event. |
c93f7669 | 2985 | * |
cdd6c482 IM |
2986 | * If event->ctx is a cloned context, callers must make sure that |
2987 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2988 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2989 | * perf_event_for_each_child or perf_event_for_each as described |
2990 | * for perf_event_disable. | |
d859e29f | 2991 | */ |
f63a8daa | 2992 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2993 | { |
cdd6c482 | 2994 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2995 | |
7b648018 | 2996 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2997 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2998 | event->state < PERF_EVENT_STATE_ERROR) { | |
9f0c4fa1 | 2999 | out: |
7b648018 | 3000 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
3001 | return; |
3002 | } | |
3003 | ||
d859e29f | 3004 | /* |
cdd6c482 | 3005 | * If the event is in error state, clear that first. |
7b648018 PZ |
3006 | * |
3007 | * That way, if we see the event in error state below, we know that it | |
3008 | * has gone back into error state, as distinct from the task having | |
3009 | * been scheduled away before the cross-call arrived. | |
d859e29f | 3010 | */ |
9f0c4fa1 KL |
3011 | if (event->state == PERF_EVENT_STATE_ERROR) { |
3012 | /* | |
3013 | * Detached SIBLING events cannot leave ERROR state. | |
3014 | */ | |
3015 | if (event->event_caps & PERF_EV_CAP_SIBLING && | |
3016 | event->group_leader == event) | |
3017 | goto out; | |
3018 | ||
cdd6c482 | 3019 | event->state = PERF_EVENT_STATE_OFF; |
9f0c4fa1 | 3020 | } |
e625cce1 | 3021 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 3022 | |
fae3fde6 | 3023 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 3024 | } |
f63a8daa PZ |
3025 | |
3026 | /* | |
3027 | * See perf_event_disable(); | |
3028 | */ | |
3029 | void perf_event_enable(struct perf_event *event) | |
3030 | { | |
3031 | struct perf_event_context *ctx; | |
3032 | ||
3033 | ctx = perf_event_ctx_lock(event); | |
3034 | _perf_event_enable(event); | |
3035 | perf_event_ctx_unlock(event, ctx); | |
3036 | } | |
dcfce4a0 | 3037 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 3038 | |
375637bc AS |
3039 | struct stop_event_data { |
3040 | struct perf_event *event; | |
3041 | unsigned int restart; | |
3042 | }; | |
3043 | ||
95ff4ca2 AS |
3044 | static int __perf_event_stop(void *info) |
3045 | { | |
375637bc AS |
3046 | struct stop_event_data *sd = info; |
3047 | struct perf_event *event = sd->event; | |
95ff4ca2 | 3048 | |
375637bc | 3049 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
3050 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
3051 | return 0; | |
3052 | ||
3053 | /* matches smp_wmb() in event_sched_in() */ | |
3054 | smp_rmb(); | |
3055 | ||
3056 | /* | |
3057 | * There is a window with interrupts enabled before we get here, | |
3058 | * so we need to check again lest we try to stop another CPU's event. | |
3059 | */ | |
3060 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
3061 | return -EAGAIN; | |
3062 | ||
3063 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3064 | ||
375637bc AS |
3065 | /* |
3066 | * May race with the actual stop (through perf_pmu_output_stop()), | |
3067 | * but it is only used for events with AUX ring buffer, and such | |
3068 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
3069 | * see comments in perf_aux_output_begin(). | |
3070 | * | |
788faab7 | 3071 | * Since this is happening on an event-local CPU, no trace is lost |
375637bc AS |
3072 | * while restarting. |
3073 | */ | |
3074 | if (sd->restart) | |
c9bbdd48 | 3075 | event->pmu->start(event, 0); |
375637bc | 3076 | |
95ff4ca2 AS |
3077 | return 0; |
3078 | } | |
3079 | ||
767ae086 | 3080 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
3081 | { |
3082 | struct stop_event_data sd = { | |
3083 | .event = event, | |
767ae086 | 3084 | .restart = restart, |
375637bc AS |
3085 | }; |
3086 | int ret = 0; | |
3087 | ||
3088 | do { | |
3089 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
3090 | return 0; | |
3091 | ||
3092 | /* matches smp_wmb() in event_sched_in() */ | |
3093 | smp_rmb(); | |
3094 | ||
3095 | /* | |
3096 | * We only want to restart ACTIVE events, so if the event goes | |
3097 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
3098 | * fall through with ret==-ENXIO. | |
3099 | */ | |
3100 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
3101 | __perf_event_stop, &sd); | |
3102 | } while (ret == -EAGAIN); | |
3103 | ||
3104 | return ret; | |
3105 | } | |
3106 | ||
3107 | /* | |
3108 | * In order to contain the amount of racy and tricky in the address filter | |
3109 | * configuration management, it is a two part process: | |
3110 | * | |
3111 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
3112 | * we update the addresses of corresponding vmas in | |
c60f83b8 | 3113 | * event::addr_filter_ranges array and bump the event::addr_filters_gen; |
375637bc AS |
3114 | * (p2) when an event is scheduled in (pmu::add), it calls |
3115 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
3116 | * if the generation has changed since the previous call. | |
3117 | * | |
3118 | * If (p1) happens while the event is active, we restart it to force (p2). | |
3119 | * | |
3120 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
3121 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
3122 | * ioctl; | |
3123 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
c1e8d7c6 | 3124 | * registered mapping, called for every new mmap(), with mm::mmap_lock down |
375637bc AS |
3125 | * for reading; |
3126 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
3127 | * of exec. | |
3128 | */ | |
3129 | void perf_event_addr_filters_sync(struct perf_event *event) | |
3130 | { | |
3131 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
3132 | ||
3133 | if (!has_addr_filter(event)) | |
3134 | return; | |
3135 | ||
3136 | raw_spin_lock(&ifh->lock); | |
3137 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
3138 | event->pmu->addr_filters_sync(event); | |
3139 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
3140 | } | |
3141 | raw_spin_unlock(&ifh->lock); | |
3142 | } | |
3143 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
3144 | ||
f63a8daa | 3145 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 3146 | { |
2023b359 | 3147 | /* |
cdd6c482 | 3148 | * not supported on inherited events |
2023b359 | 3149 | */ |
2e939d1d | 3150 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
3151 | return -EINVAL; |
3152 | ||
cdd6c482 | 3153 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 3154 | _perf_event_enable(event); |
2023b359 PZ |
3155 | |
3156 | return 0; | |
79f14641 | 3157 | } |
f63a8daa PZ |
3158 | |
3159 | /* | |
3160 | * See perf_event_disable() | |
3161 | */ | |
3162 | int perf_event_refresh(struct perf_event *event, int refresh) | |
3163 | { | |
3164 | struct perf_event_context *ctx; | |
3165 | int ret; | |
3166 | ||
3167 | ctx = perf_event_ctx_lock(event); | |
3168 | ret = _perf_event_refresh(event, refresh); | |
3169 | perf_event_ctx_unlock(event, ctx); | |
3170 | ||
3171 | return ret; | |
3172 | } | |
26ca5c11 | 3173 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 3174 | |
32ff77e8 MC |
3175 | static int perf_event_modify_breakpoint(struct perf_event *bp, |
3176 | struct perf_event_attr *attr) | |
3177 | { | |
3178 | int err; | |
3179 | ||
3180 | _perf_event_disable(bp); | |
3181 | ||
3182 | err = modify_user_hw_breakpoint_check(bp, attr, true); | |
32ff77e8 | 3183 | |
bf06278c | 3184 | if (!bp->attr.disabled) |
32ff77e8 | 3185 | _perf_event_enable(bp); |
bf06278c JO |
3186 | |
3187 | return err; | |
32ff77e8 MC |
3188 | } |
3189 | ||
3c25fc97 ME |
3190 | /* |
3191 | * Copy event-type-independent attributes that may be modified. | |
3192 | */ | |
3193 | static void perf_event_modify_copy_attr(struct perf_event_attr *to, | |
3194 | const struct perf_event_attr *from) | |
3195 | { | |
3196 | to->sig_data = from->sig_data; | |
3197 | } | |
3198 | ||
32ff77e8 MC |
3199 | static int perf_event_modify_attr(struct perf_event *event, |
3200 | struct perf_event_attr *attr) | |
3201 | { | |
47f661ec ME |
3202 | int (*func)(struct perf_event *, struct perf_event_attr *); |
3203 | struct perf_event *child; | |
3204 | int err; | |
3205 | ||
32ff77e8 MC |
3206 | if (event->attr.type != attr->type) |
3207 | return -EINVAL; | |
3208 | ||
3209 | switch (event->attr.type) { | |
3210 | case PERF_TYPE_BREAKPOINT: | |
47f661ec ME |
3211 | func = perf_event_modify_breakpoint; |
3212 | break; | |
32ff77e8 MC |
3213 | default: |
3214 | /* Place holder for future additions. */ | |
3215 | return -EOPNOTSUPP; | |
3216 | } | |
47f661ec ME |
3217 | |
3218 | WARN_ON_ONCE(event->ctx->parent_ctx); | |
3219 | ||
3220 | mutex_lock(&event->child_mutex); | |
3c25fc97 ME |
3221 | /* |
3222 | * Event-type-independent attributes must be copied before event-type | |
3223 | * modification, which will validate that final attributes match the | |
3224 | * source attributes after all relevant attributes have been copied. | |
3225 | */ | |
3226 | perf_event_modify_copy_attr(&event->attr, attr); | |
47f661ec ME |
3227 | err = func(event, attr); |
3228 | if (err) | |
3229 | goto out; | |
3230 | list_for_each_entry(child, &event->child_list, child_list) { | |
3c25fc97 | 3231 | perf_event_modify_copy_attr(&child->attr, attr); |
47f661ec ME |
3232 | err = func(child, attr); |
3233 | if (err) | |
3234 | goto out; | |
3235 | } | |
3236 | out: | |
3237 | mutex_unlock(&event->child_mutex); | |
3238 | return err; | |
32ff77e8 MC |
3239 | } |
3240 | ||
bd275681 PZ |
3241 | static void __pmu_ctx_sched_out(struct perf_event_pmu_context *pmu_ctx, |
3242 | enum event_type_t event_type) | |
235c7fc7 | 3243 | { |
bd275681 | 3244 | struct perf_event_context *ctx = pmu_ctx->ctx; |
6668128a | 3245 | struct perf_event *event, *tmp; |
bd275681 PZ |
3246 | struct pmu *pmu = pmu_ctx->pmu; |
3247 | ||
3248 | if (ctx->task && !ctx->is_active) { | |
3249 | struct perf_cpu_pmu_context *cpc; | |
3250 | ||
3251 | cpc = this_cpu_ptr(pmu->cpu_pmu_context); | |
3252 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
3253 | cpc->task_epc = NULL; | |
3254 | } | |
3255 | ||
3256 | if (!event_type) | |
3257 | return; | |
3258 | ||
3259 | perf_pmu_disable(pmu); | |
3260 | if (event_type & EVENT_PINNED) { | |
3261 | list_for_each_entry_safe(event, tmp, | |
3262 | &pmu_ctx->pinned_active, | |
3263 | active_list) | |
3264 | group_sched_out(event, ctx); | |
3265 | } | |
3266 | ||
3267 | if (event_type & EVENT_FLEXIBLE) { | |
3268 | list_for_each_entry_safe(event, tmp, | |
3269 | &pmu_ctx->flexible_active, | |
3270 | active_list) | |
3271 | group_sched_out(event, ctx); | |
3272 | /* | |
3273 | * Since we cleared EVENT_FLEXIBLE, also clear | |
3274 | * rotate_necessary, is will be reset by | |
3275 | * ctx_flexible_sched_in() when needed. | |
3276 | */ | |
3277 | pmu_ctx->rotate_necessary = 0; | |
3278 | } | |
3279 | perf_pmu_enable(pmu); | |
3280 | } | |
3281 | ||
3282 | static void | |
3283 | ctx_sched_out(struct perf_event_context *ctx, enum event_type_t event_type) | |
3284 | { | |
3285 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); | |
3286 | struct perf_event_pmu_context *pmu_ctx; | |
db24d33e | 3287 | int is_active = ctx->is_active; |
f06cc667 PZ |
3288 | bool cgroup = event_type & EVENT_CGROUP; |
3289 | ||
3290 | event_type &= ~EVENT_CGROUP; | |
235c7fc7 | 3291 | |
c994d613 | 3292 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 3293 | |
39a43640 PZ |
3294 | if (likely(!ctx->nr_events)) { |
3295 | /* | |
3296 | * See __perf_remove_from_context(). | |
3297 | */ | |
3298 | WARN_ON_ONCE(ctx->is_active); | |
3299 | if (ctx->task) | |
3300 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 3301 | return; |
39a43640 PZ |
3302 | } |
3303 | ||
8fdc6539 PZ |
3304 | /* |
3305 | * Always update time if it was set; not only when it changes. | |
3306 | * Otherwise we can 'forget' to update time for any but the last | |
3307 | * context we sched out. For example: | |
3308 | * | |
3309 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
3310 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
3311 | * | |
3312 | * would only update time for the pinned events. | |
3313 | */ | |
3cbaa590 PZ |
3314 | if (is_active & EVENT_TIME) { |
3315 | /* update (and stop) ctx time */ | |
3316 | update_context_time(ctx); | |
09f5e7dc PZ |
3317 | update_cgrp_time_from_cpuctx(cpuctx, ctx == &cpuctx->ctx); |
3318 | /* | |
3319 | * CPU-release for the below ->is_active store, | |
3320 | * see __load_acquire() in perf_event_time_now() | |
3321 | */ | |
3322 | barrier(); | |
3323 | } | |
3324 | ||
3325 | ctx->is_active &= ~event_type; | |
3326 | if (!(ctx->is_active & EVENT_ALL)) | |
3327 | ctx->is_active = 0; | |
3328 | ||
3329 | if (ctx->task) { | |
3330 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3331 | if (!ctx->is_active) | |
3332 | cpuctx->task_ctx = NULL; | |
3cbaa590 PZ |
3333 | } |
3334 | ||
8fdc6539 PZ |
3335 | is_active ^= ctx->is_active; /* changed bits */ |
3336 | ||
f06cc667 PZ |
3337 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
3338 | if (cgroup && !pmu_ctx->nr_cgroups) | |
3339 | continue; | |
bd275681 | 3340 | __pmu_ctx_sched_out(pmu_ctx, is_active); |
f06cc667 | 3341 | } |
235c7fc7 IM |
3342 | } |
3343 | ||
564c2b21 | 3344 | /* |
5a3126d4 PZ |
3345 | * Test whether two contexts are equivalent, i.e. whether they have both been |
3346 | * cloned from the same version of the same context. | |
3347 | * | |
3348 | * Equivalence is measured using a generation number in the context that is | |
3349 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
3350 | * and list_del_event(). | |
564c2b21 | 3351 | */ |
cdd6c482 IM |
3352 | static int context_equiv(struct perf_event_context *ctx1, |
3353 | struct perf_event_context *ctx2) | |
564c2b21 | 3354 | { |
211de6eb PZ |
3355 | lockdep_assert_held(&ctx1->lock); |
3356 | lockdep_assert_held(&ctx2->lock); | |
3357 | ||
5a3126d4 PZ |
3358 | /* Pinning disables the swap optimization */ |
3359 | if (ctx1->pin_count || ctx2->pin_count) | |
3360 | return 0; | |
3361 | ||
3362 | /* If ctx1 is the parent of ctx2 */ | |
3363 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
3364 | return 1; | |
3365 | ||
3366 | /* If ctx2 is the parent of ctx1 */ | |
3367 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
3368 | return 1; | |
3369 | ||
3370 | /* | |
3371 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
3372 | * hierarchy, see perf_event_init_context(). | |
3373 | */ | |
3374 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
3375 | ctx1->parent_gen == ctx2->parent_gen) | |
3376 | return 1; | |
3377 | ||
3378 | /* Unmatched */ | |
3379 | return 0; | |
564c2b21 PM |
3380 | } |
3381 | ||
cdd6c482 IM |
3382 | static void __perf_event_sync_stat(struct perf_event *event, |
3383 | struct perf_event *next_event) | |
bfbd3381 PZ |
3384 | { |
3385 | u64 value; | |
3386 | ||
cdd6c482 | 3387 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
3388 | return; |
3389 | ||
3390 | /* | |
cdd6c482 | 3391 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
3392 | * because we're in the middle of a context switch and have IRQs |
3393 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 3394 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
3395 | * don't need to use it. |
3396 | */ | |
0d3d73aa | 3397 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
3dbebf15 | 3398 | event->pmu->read(event); |
bfbd3381 | 3399 | |
0d3d73aa | 3400 | perf_event_update_time(event); |
bfbd3381 PZ |
3401 | |
3402 | /* | |
cdd6c482 | 3403 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
3404 | * values when we flip the contexts. |
3405 | */ | |
e7850595 PZ |
3406 | value = local64_read(&next_event->count); |
3407 | value = local64_xchg(&event->count, value); | |
3408 | local64_set(&next_event->count, value); | |
bfbd3381 | 3409 | |
cdd6c482 IM |
3410 | swap(event->total_time_enabled, next_event->total_time_enabled); |
3411 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 3412 | |
bfbd3381 | 3413 | /* |
19d2e755 | 3414 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 3415 | */ |
cdd6c482 IM |
3416 | perf_event_update_userpage(event); |
3417 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
3418 | } |
3419 | ||
cdd6c482 IM |
3420 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
3421 | struct perf_event_context *next_ctx) | |
bfbd3381 | 3422 | { |
cdd6c482 | 3423 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
3424 | |
3425 | if (!ctx->nr_stat) | |
3426 | return; | |
3427 | ||
02ffdbc8 PZ |
3428 | update_context_time(ctx); |
3429 | ||
cdd6c482 IM |
3430 | event = list_first_entry(&ctx->event_list, |
3431 | struct perf_event, event_entry); | |
bfbd3381 | 3432 | |
cdd6c482 IM |
3433 | next_event = list_first_entry(&next_ctx->event_list, |
3434 | struct perf_event, event_entry); | |
bfbd3381 | 3435 | |
cdd6c482 IM |
3436 | while (&event->event_entry != &ctx->event_list && |
3437 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 3438 | |
cdd6c482 | 3439 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 3440 | |
cdd6c482 IM |
3441 | event = list_next_entry(event, event_entry); |
3442 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
3443 | } |
3444 | } | |
3445 | ||
bd275681 PZ |
3446 | #define double_list_for_each_entry(pos1, pos2, head1, head2, member) \ |
3447 | for (pos1 = list_first_entry(head1, typeof(*pos1), member), \ | |
3448 | pos2 = list_first_entry(head2, typeof(*pos2), member); \ | |
3449 | !list_entry_is_head(pos1, head1, member) && \ | |
3450 | !list_entry_is_head(pos2, head2, member); \ | |
3451 | pos1 = list_next_entry(pos1, member), \ | |
3452 | pos2 = list_next_entry(pos2, member)) | |
3453 | ||
3454 | static void perf_event_swap_task_ctx_data(struct perf_event_context *prev_ctx, | |
3455 | struct perf_event_context *next_ctx) | |
3456 | { | |
3457 | struct perf_event_pmu_context *prev_epc, *next_epc; | |
3458 | ||
3459 | if (!prev_ctx->nr_task_data) | |
3460 | return; | |
3461 | ||
3462 | double_list_for_each_entry(prev_epc, next_epc, | |
3463 | &prev_ctx->pmu_ctx_list, &next_ctx->pmu_ctx_list, | |
3464 | pmu_ctx_entry) { | |
3465 | ||
3466 | if (WARN_ON_ONCE(prev_epc->pmu != next_epc->pmu)) | |
3467 | continue; | |
3468 | ||
3469 | /* | |
3470 | * PMU specific parts of task perf context can require | |
3471 | * additional synchronization. As an example of such | |
3472 | * synchronization see implementation details of Intel | |
3473 | * LBR call stack data profiling; | |
3474 | */ | |
3475 | if (prev_epc->pmu->swap_task_ctx) | |
3476 | prev_epc->pmu->swap_task_ctx(prev_epc, next_epc); | |
3477 | else | |
3478 | swap(prev_epc->task_ctx_data, next_epc->task_ctx_data); | |
3479 | } | |
3480 | } | |
3481 | ||
3482 | static void perf_ctx_sched_task_cb(struct perf_event_context *ctx, bool sched_in) | |
3483 | { | |
3484 | struct perf_event_pmu_context *pmu_ctx; | |
3485 | struct perf_cpu_pmu_context *cpc; | |
3486 | ||
3487 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { | |
3488 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
3489 | ||
3490 | if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) | |
3491 | pmu_ctx->pmu->sched_task(pmu_ctx, sched_in); | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | static void | |
3496 | perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) | |
0793a61d | 3497 | { |
bd275681 | 3498 | struct perf_event_context *ctx = task->perf_event_ctxp; |
cdd6c482 | 3499 | struct perf_event_context *next_ctx; |
5a3126d4 | 3500 | struct perf_event_context *parent, *next_parent; |
c93f7669 | 3501 | int do_switch = 1; |
0793a61d | 3502 | |
108b02cf PZ |
3503 | if (likely(!ctx)) |
3504 | return; | |
10989fb2 | 3505 | |
c93f7669 | 3506 | rcu_read_lock(); |
bd275681 | 3507 | next_ctx = rcu_dereference(next->perf_event_ctxp); |
5a3126d4 PZ |
3508 | if (!next_ctx) |
3509 | goto unlock; | |
3510 | ||
3511 | parent = rcu_dereference(ctx->parent_ctx); | |
3512 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
3513 | ||
3514 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 3515 | if (!parent && !next_parent) |
5a3126d4 PZ |
3516 | goto unlock; |
3517 | ||
3518 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
3519 | /* |
3520 | * Looks like the two contexts are clones, so we might be | |
3521 | * able to optimize the context switch. We lock both | |
3522 | * contexts and check that they are clones under the | |
3523 | * lock (including re-checking that neither has been | |
3524 | * uncloned in the meantime). It doesn't matter which | |
3525 | * order we take the locks because no other cpu could | |
3526 | * be trying to lock both of these tasks. | |
3527 | */ | |
e625cce1 TG |
3528 | raw_spin_lock(&ctx->lock); |
3529 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 3530 | if (context_equiv(ctx, next_ctx)) { |
c2b98a86 | 3531 | |
f06cc667 | 3532 | perf_ctx_disable(ctx, false); |
ca6c2132 PZ |
3533 | |
3534 | /* PMIs are disabled; ctx->nr_pending is stable. */ | |
3535 | if (local_read(&ctx->nr_pending) || | |
3536 | local_read(&next_ctx->nr_pending)) { | |
3537 | /* | |
3538 | * Must not swap out ctx when there's pending | |
3539 | * events that rely on the ctx->task relation. | |
3540 | */ | |
3541 | raw_spin_unlock(&next_ctx->lock); | |
3542 | rcu_read_unlock(); | |
3543 | goto inside_switch; | |
3544 | } | |
3545 | ||
63b6da39 PZ |
3546 | WRITE_ONCE(ctx->task, next); |
3547 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c | 3548 | |
bd275681 PZ |
3549 | perf_ctx_sched_task_cb(ctx, false); |
3550 | perf_event_swap_task_ctx_data(ctx, next_ctx); | |
5a158c3c | 3551 | |
f06cc667 | 3552 | perf_ctx_enable(ctx, false); |
44fae179 | 3553 | |
63b6da39 PZ |
3554 | /* |
3555 | * RCU_INIT_POINTER here is safe because we've not | |
3556 | * modified the ctx and the above modification of | |
3557 | * ctx->task and ctx->task_ctx_data are immaterial | |
3558 | * since those values are always verified under | |
3559 | * ctx->lock which we're now holding. | |
3560 | */ | |
bd275681 PZ |
3561 | RCU_INIT_POINTER(task->perf_event_ctxp, next_ctx); |
3562 | RCU_INIT_POINTER(next->perf_event_ctxp, ctx); | |
63b6da39 | 3563 | |
c93f7669 | 3564 | do_switch = 0; |
bfbd3381 | 3565 | |
cdd6c482 | 3566 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 3567 | } |
e625cce1 TG |
3568 | raw_spin_unlock(&next_ctx->lock); |
3569 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 3570 | } |
5a3126d4 | 3571 | unlock: |
c93f7669 | 3572 | rcu_read_unlock(); |
564c2b21 | 3573 | |
c93f7669 | 3574 | if (do_switch) { |
facc4307 | 3575 | raw_spin_lock(&ctx->lock); |
f06cc667 | 3576 | perf_ctx_disable(ctx, false); |
44fae179 | 3577 | |
ca6c2132 | 3578 | inside_switch: |
bd275681 PZ |
3579 | perf_ctx_sched_task_cb(ctx, false); |
3580 | task_ctx_sched_out(ctx, EVENT_ALL); | |
44fae179 | 3581 | |
f06cc667 | 3582 | perf_ctx_enable(ctx, false); |
facc4307 | 3583 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 3584 | } |
0793a61d TG |
3585 | } |
3586 | ||
a5398bff | 3587 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
bd275681 | 3588 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
a5398bff | 3589 | |
ba532500 YZ |
3590 | void perf_sched_cb_dec(struct pmu *pmu) |
3591 | { | |
bd275681 | 3592 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); |
e48c1788 | 3593 | |
a5398bff | 3594 | this_cpu_dec(perf_sched_cb_usages); |
bd275681 | 3595 | barrier(); |
a5398bff | 3596 | |
bd275681 PZ |
3597 | if (!--cpc->sched_cb_usage) |
3598 | list_del(&cpc->sched_cb_entry); | |
ba532500 YZ |
3599 | } |
3600 | ||
e48c1788 | 3601 | |
ba532500 YZ |
3602 | void perf_sched_cb_inc(struct pmu *pmu) |
3603 | { | |
bd275681 | 3604 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); |
e48c1788 | 3605 | |
bd275681 PZ |
3606 | if (!cpc->sched_cb_usage++) |
3607 | list_add(&cpc->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
a5398bff | 3608 | |
bd275681 | 3609 | barrier(); |
a5398bff | 3610 | this_cpu_inc(perf_sched_cb_usages); |
ba532500 YZ |
3611 | } |
3612 | ||
3613 | /* | |
3614 | * This function provides the context switch callback to the lower code | |
3615 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
3616 | * |
3617 | * This callback is relevant even to per-cpu events; for example multi event | |
3618 | * PEBS requires this to provide PID/TID information. This requires we flush | |
3619 | * all queued PEBS records before we context switch to a new task. | |
ba532500 | 3620 | */ |
bd275681 | 3621 | static void __perf_pmu_sched_task(struct perf_cpu_pmu_context *cpc, bool sched_in) |
556cccad | 3622 | { |
bd275681 | 3623 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
556cccad KL |
3624 | struct pmu *pmu; |
3625 | ||
bd275681 | 3626 | pmu = cpc->epc.pmu; |
556cccad | 3627 | |
bd275681 | 3628 | /* software PMUs will not have sched_task */ |
556cccad KL |
3629 | if (WARN_ON_ONCE(!pmu->sched_task)) |
3630 | return; | |
3631 | ||
3632 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); | |
3633 | perf_pmu_disable(pmu); | |
3634 | ||
bd275681 | 3635 | pmu->sched_task(cpc->task_epc, sched_in); |
556cccad KL |
3636 | |
3637 | perf_pmu_enable(pmu); | |
3638 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
3639 | } | |
3640 | ||
a5398bff KL |
3641 | static void perf_pmu_sched_task(struct task_struct *prev, |
3642 | struct task_struct *next, | |
3643 | bool sched_in) | |
3644 | { | |
bd275681 PZ |
3645 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
3646 | struct perf_cpu_pmu_context *cpc; | |
a5398bff | 3647 | |
bd275681 PZ |
3648 | /* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ |
3649 | if (prev == next || cpuctx->task_ctx) | |
a5398bff KL |
3650 | return; |
3651 | ||
bd275681 PZ |
3652 | list_for_each_entry(cpc, this_cpu_ptr(&sched_cb_list), sched_cb_entry) |
3653 | __perf_pmu_sched_task(cpc, sched_in); | |
a5398bff KL |
3654 | } |
3655 | ||
45ac1403 AH |
3656 | static void perf_event_switch(struct task_struct *task, |
3657 | struct task_struct *next_prev, bool sched_in); | |
3658 | ||
8dc85d54 PZ |
3659 | /* |
3660 | * Called from scheduler to remove the events of the current task, | |
3661 | * with interrupts disabled. | |
3662 | * | |
3663 | * We stop each event and update the event value in event->count. | |
3664 | * | |
3665 | * This does not protect us against NMI, but disable() | |
3666 | * sets the disabled bit in the control field of event _before_ | |
3667 | * accessing the event control register. If a NMI hits, then it will | |
3668 | * not restart the event. | |
3669 | */ | |
ab0cce56 JO |
3670 | void __perf_event_task_sched_out(struct task_struct *task, |
3671 | struct task_struct *next) | |
8dc85d54 | 3672 | { |
a5398bff KL |
3673 | if (__this_cpu_read(perf_sched_cb_usages)) |
3674 | perf_pmu_sched_task(task, next, false); | |
3675 | ||
45ac1403 AH |
3676 | if (atomic_read(&nr_switch_events)) |
3677 | perf_event_switch(task, next, false); | |
3678 | ||
bd275681 | 3679 | perf_event_context_sched_out(task, next); |
e5d1367f SE |
3680 | |
3681 | /* | |
3682 | * if cgroup events exist on this CPU, then we need | |
3683 | * to check if we have to switch out PMU state. | |
3684 | * cgroup event are system-wide mode only | |
3685 | */ | |
f841b682 | 3686 | perf_cgroup_switch(next); |
8dc85d54 PZ |
3687 | } |
3688 | ||
6eef8a71 | 3689 | static bool perf_less_group_idx(const void *l, const void *r) |
0793a61d | 3690 | { |
24fb6b8e IR |
3691 | const struct perf_event *le = *(const struct perf_event **)l; |
3692 | const struct perf_event *re = *(const struct perf_event **)r; | |
6eef8a71 IR |
3693 | |
3694 | return le->group_index < re->group_index; | |
3695 | } | |
3696 | ||
3697 | static void swap_ptr(void *l, void *r) | |
3698 | { | |
3699 | void **lp = l, **rp = r; | |
3700 | ||
3701 | swap(*lp, *rp); | |
3702 | } | |
3703 | ||
3704 | static const struct min_heap_callbacks perf_min_heap = { | |
3705 | .elem_size = sizeof(struct perf_event *), | |
3706 | .less = perf_less_group_idx, | |
3707 | .swp = swap_ptr, | |
3708 | }; | |
3709 | ||
3710 | static void __heap_add(struct min_heap *heap, struct perf_event *event) | |
3711 | { | |
3712 | struct perf_event **itrs = heap->data; | |
3713 | ||
3714 | if (event) { | |
3715 | itrs[heap->nr] = event; | |
3716 | heap->nr++; | |
3717 | } | |
3718 | } | |
3719 | ||
bd275681 PZ |
3720 | static void __link_epc(struct perf_event_pmu_context *pmu_ctx) |
3721 | { | |
3722 | struct perf_cpu_pmu_context *cpc; | |
3723 | ||
3724 | if (!pmu_ctx->ctx->task) | |
3725 | return; | |
3726 | ||
3727 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
3728 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
3729 | cpc->task_epc = pmu_ctx; | |
3730 | } | |
3731 | ||
3732 | static noinline int visit_groups_merge(struct perf_event_context *ctx, | |
836196be | 3733 | struct perf_event_groups *groups, int cpu, |
bd275681 | 3734 | struct pmu *pmu, |
6eef8a71 IR |
3735 | int (*func)(struct perf_event *, void *), |
3736 | void *data) | |
3737 | { | |
95ed6c70 IR |
3738 | #ifdef CONFIG_CGROUP_PERF |
3739 | struct cgroup_subsys_state *css = NULL; | |
3740 | #endif | |
bd275681 | 3741 | struct perf_cpu_context *cpuctx = NULL; |
6eef8a71 IR |
3742 | /* Space for per CPU and/or any CPU event iterators. */ |
3743 | struct perf_event *itrs[2]; | |
836196be IR |
3744 | struct min_heap event_heap; |
3745 | struct perf_event **evt; | |
1cac7b1a | 3746 | int ret; |
8e1a2031 | 3747 | |
bd275681 PZ |
3748 | if (pmu->filter && pmu->filter(pmu, cpu)) |
3749 | return 0; | |
3750 | ||
3751 | if (!ctx->task) { | |
3752 | cpuctx = this_cpu_ptr(&perf_cpu_context); | |
836196be IR |
3753 | event_heap = (struct min_heap){ |
3754 | .data = cpuctx->heap, | |
3755 | .nr = 0, | |
3756 | .size = cpuctx->heap_size, | |
3757 | }; | |
c2283c93 IR |
3758 | |
3759 | lockdep_assert_held(&cpuctx->ctx.lock); | |
95ed6c70 IR |
3760 | |
3761 | #ifdef CONFIG_CGROUP_PERF | |
3762 | if (cpuctx->cgrp) | |
3763 | css = &cpuctx->cgrp->css; | |
3764 | #endif | |
836196be IR |
3765 | } else { |
3766 | event_heap = (struct min_heap){ | |
3767 | .data = itrs, | |
3768 | .nr = 0, | |
3769 | .size = ARRAY_SIZE(itrs), | |
3770 | }; | |
3771 | /* Events not within a CPU context may be on any CPU. */ | |
bd275681 | 3772 | __heap_add(&event_heap, perf_event_groups_first(groups, -1, pmu, NULL)); |
836196be IR |
3773 | } |
3774 | evt = event_heap.data; | |
3775 | ||
bd275681 | 3776 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, NULL)); |
95ed6c70 IR |
3777 | |
3778 | #ifdef CONFIG_CGROUP_PERF | |
3779 | for (; css; css = css->parent) | |
bd275681 | 3780 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, css->cgroup)); |
95ed6c70 | 3781 | #endif |
1cac7b1a | 3782 | |
bd275681 PZ |
3783 | if (event_heap.nr) { |
3784 | __link_epc((*evt)->pmu_ctx); | |
3785 | perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); | |
3786 | } | |
3787 | ||
6eef8a71 | 3788 | min_heapify_all(&event_heap, &perf_min_heap); |
1cac7b1a | 3789 | |
6eef8a71 | 3790 | while (event_heap.nr) { |
1cac7b1a PZ |
3791 | ret = func(*evt, data); |
3792 | if (ret) | |
3793 | return ret; | |
3794 | ||
bd275681 | 3795 | *evt = perf_event_groups_next(*evt, pmu); |
6eef8a71 IR |
3796 | if (*evt) |
3797 | min_heapify(&event_heap, 0, &perf_min_heap); | |
3798 | else | |
3799 | min_heap_pop(&event_heap, &perf_min_heap); | |
8e1a2031 | 3800 | } |
0793a61d | 3801 | |
1cac7b1a PZ |
3802 | return 0; |
3803 | } | |
3804 | ||
09f5e7dc PZ |
3805 | /* |
3806 | * Because the userpage is strictly per-event (there is no concept of context, | |
3807 | * so there cannot be a context indirection), every userpage must be updated | |
3808 | * when context time starts :-( | |
3809 | * | |
3810 | * IOW, we must not miss EVENT_TIME edges. | |
3811 | */ | |
f7925653 SL |
3812 | static inline bool event_update_userpage(struct perf_event *event) |
3813 | { | |
3814 | if (likely(!atomic_read(&event->mmap_count))) | |
3815 | return false; | |
3816 | ||
3817 | perf_event_update_time(event); | |
f7925653 SL |
3818 | perf_event_update_userpage(event); |
3819 | ||
3820 | return true; | |
3821 | } | |
3822 | ||
3823 | static inline void group_update_userpage(struct perf_event *group_event) | |
3824 | { | |
3825 | struct perf_event *event; | |
3826 | ||
3827 | if (!event_update_userpage(group_event)) | |
3828 | return; | |
3829 | ||
3830 | for_each_sibling_event(event, group_event) | |
3831 | event_update_userpage(event); | |
3832 | } | |
3833 | ||
ab6f824c | 3834 | static int merge_sched_in(struct perf_event *event, void *data) |
1cac7b1a | 3835 | { |
2c2366c7 | 3836 | struct perf_event_context *ctx = event->ctx; |
2c2366c7 | 3837 | int *can_add_hw = data; |
ab6f824c | 3838 | |
1cac7b1a PZ |
3839 | if (event->state <= PERF_EVENT_STATE_OFF) |
3840 | return 0; | |
3841 | ||
3842 | if (!event_filter_match(event)) | |
3843 | return 0; | |
3844 | ||
bd275681 PZ |
3845 | if (group_can_go_on(event, *can_add_hw)) { |
3846 | if (!group_sched_in(event, ctx)) | |
ab6f824c | 3847 | list_add_tail(&event->active_list, get_event_list(event)); |
6668128a | 3848 | } |
1cac7b1a | 3849 | |
ab6f824c | 3850 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
f7925653 | 3851 | *can_add_hw = 0; |
33238c50 PZ |
3852 | if (event->attr.pinned) { |
3853 | perf_cgroup_event_disable(event, ctx); | |
ab6f824c | 3854 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); |
f7925653 | 3855 | } else { |
bd275681 PZ |
3856 | struct perf_cpu_pmu_context *cpc; |
3857 | ||
3858 | event->pmu_ctx->rotate_necessary = 1; | |
3859 | cpc = this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context); | |
3860 | perf_mux_hrtimer_restart(cpc); | |
f7925653 | 3861 | group_update_userpage(event); |
33238c50 | 3862 | } |
3b6f9e5c | 3863 | } |
1cac7b1a PZ |
3864 | |
3865 | return 0; | |
5b0311e1 FW |
3866 | } |
3867 | ||
f06cc667 PZ |
3868 | static void pmu_groups_sched_in(struct perf_event_context *ctx, |
3869 | struct perf_event_groups *groups, | |
3870 | struct pmu *pmu) | |
5b0311e1 | 3871 | { |
2c2366c7 | 3872 | int can_add_hw = 1; |
f06cc667 PZ |
3873 | visit_groups_merge(ctx, groups, smp_processor_id(), pmu, |
3874 | merge_sched_in, &can_add_hw); | |
1cac7b1a | 3875 | } |
8e1a2031 | 3876 | |
f06cc667 PZ |
3877 | static void ctx_groups_sched_in(struct perf_event_context *ctx, |
3878 | struct perf_event_groups *groups, | |
3879 | bool cgroup) | |
1cac7b1a | 3880 | { |
bd275681 | 3881 | struct perf_event_pmu_context *pmu_ctx; |
0793a61d | 3882 | |
f06cc667 PZ |
3883 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
3884 | if (cgroup && !pmu_ctx->nr_cgroups) | |
3885 | continue; | |
3886 | pmu_groups_sched_in(ctx, groups, pmu_ctx->pmu); | |
bd275681 PZ |
3887 | } |
3888 | } | |
836196be | 3889 | |
f06cc667 PZ |
3890 | static void __pmu_ctx_sched_in(struct perf_event_context *ctx, |
3891 | struct pmu *pmu) | |
bd275681 | 3892 | { |
f06cc667 | 3893 | pmu_groups_sched_in(ctx, &ctx->flexible_groups, pmu); |
5b0311e1 FW |
3894 | } |
3895 | ||
3896 | static void | |
bd275681 | 3897 | ctx_sched_in(struct perf_event_context *ctx, enum event_type_t event_type) |
5b0311e1 | 3898 | { |
bd275681 | 3899 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
db24d33e | 3900 | int is_active = ctx->is_active; |
f06cc667 PZ |
3901 | bool cgroup = event_type & EVENT_CGROUP; |
3902 | ||
3903 | event_type &= ~EVENT_CGROUP; | |
c994d613 PZ |
3904 | |
3905 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3906 | |
5b0311e1 | 3907 | if (likely(!ctx->nr_events)) |
facc4307 | 3908 | return; |
5b0311e1 | 3909 | |
baf1b12a | 3910 | if (!(is_active & EVENT_TIME)) { |
09f5e7dc PZ |
3911 | /* start ctx time */ |
3912 | __update_context_time(ctx, false); | |
a0827713 | 3913 | perf_cgroup_set_timestamp(cpuctx); |
09f5e7dc PZ |
3914 | /* |
3915 | * CPU-release for the below ->is_active store, | |
3916 | * see __load_acquire() in perf_event_time_now() | |
3917 | */ | |
3918 | barrier(); | |
3919 | } | |
3920 | ||
3cbaa590 | 3921 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3922 | if (ctx->task) { |
3923 | if (!is_active) | |
3924 | cpuctx->task_ctx = ctx; | |
3925 | else | |
3926 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3927 | } | |
3928 | ||
3cbaa590 PZ |
3929 | is_active ^= ctx->is_active; /* changed bits */ |
3930 | ||
5b0311e1 FW |
3931 | /* |
3932 | * First go through the list and put on any pinned groups | |
3933 | * in order to give them the best chance of going on. | |
3934 | */ | |
3cbaa590 | 3935 | if (is_active & EVENT_PINNED) |
f06cc667 | 3936 | ctx_groups_sched_in(ctx, &ctx->pinned_groups, cgroup); |
5b0311e1 FW |
3937 | |
3938 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3939 | if (is_active & EVENT_FLEXIBLE) |
f06cc667 | 3940 | ctx_groups_sched_in(ctx, &ctx->flexible_groups, cgroup); |
235c7fc7 IM |
3941 | } |
3942 | ||
bd275681 | 3943 | static void perf_event_context_sched_in(struct task_struct *task) |
329c0e01 | 3944 | { |
bd275681 PZ |
3945 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
3946 | struct perf_event_context *ctx; | |
329c0e01 | 3947 | |
bd275681 PZ |
3948 | rcu_read_lock(); |
3949 | ctx = rcu_dereference(task->perf_event_ctxp); | |
3950 | if (!ctx) | |
3951 | goto rcu_unlock; | |
235c7fc7 | 3952 | |
bd275681 PZ |
3953 | if (cpuctx->task_ctx == ctx) { |
3954 | perf_ctx_lock(cpuctx, ctx); | |
f06cc667 | 3955 | perf_ctx_disable(ctx, false); |
012669c7 | 3956 | |
bd275681 | 3957 | perf_ctx_sched_task_cb(ctx, true); |
012669c7 | 3958 | |
f06cc667 | 3959 | perf_ctx_enable(ctx, false); |
bd275681 PZ |
3960 | perf_ctx_unlock(cpuctx, ctx); |
3961 | goto rcu_unlock; | |
556cccad | 3962 | } |
329c0e01 | 3963 | |
facc4307 | 3964 | perf_ctx_lock(cpuctx, ctx); |
fdccc3fb | 3965 | /* |
3966 | * We must check ctx->nr_events while holding ctx->lock, such | |
3967 | * that we serialize against perf_install_in_context(). | |
3968 | */ | |
3969 | if (!ctx->nr_events) | |
3970 | goto unlock; | |
3971 | ||
f06cc667 | 3972 | perf_ctx_disable(ctx, false); |
329c0e01 FW |
3973 | /* |
3974 | * We want to keep the following priority order: | |
3975 | * cpu pinned (that don't need to move), task pinned, | |
3976 | * cpu flexible, task flexible. | |
fe45bafb AS |
3977 | * |
3978 | * However, if task's ctx is not carrying any pinned | |
3979 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3980 | */ |
bd275681 | 3981 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) { |
f06cc667 | 3982 | perf_ctx_disable(&cpuctx->ctx, false); |
bd275681 PZ |
3983 | ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); |
3984 | } | |
3985 | ||
a0827713 | 3986 | perf_event_sched_in(cpuctx, ctx); |
556cccad | 3987 | |
bd275681 | 3988 | perf_ctx_sched_task_cb(cpuctx->task_ctx, true); |
556cccad | 3989 | |
bd275681 | 3990 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) |
f06cc667 | 3991 | perf_ctx_enable(&cpuctx->ctx, false); |
bd275681 | 3992 | |
f06cc667 | 3993 | perf_ctx_enable(ctx, false); |
fdccc3fb | 3994 | |
3995 | unlock: | |
facc4307 | 3996 | perf_ctx_unlock(cpuctx, ctx); |
bd275681 PZ |
3997 | rcu_unlock: |
3998 | rcu_read_unlock(); | |
235c7fc7 IM |
3999 | } |
4000 | ||
8dc85d54 PZ |
4001 | /* |
4002 | * Called from scheduler to add the events of the current task | |
4003 | * with interrupts disabled. | |
4004 | * | |
4005 | * We restore the event value and then enable it. | |
4006 | * | |
4007 | * This does not protect us against NMI, but enable() | |
4008 | * sets the enabled bit in the control field of event _before_ | |
4009 | * accessing the event control register. If a NMI hits, then it will | |
4010 | * keep the event running. | |
4011 | */ | |
ab0cce56 JO |
4012 | void __perf_event_task_sched_in(struct task_struct *prev, |
4013 | struct task_struct *task) | |
8dc85d54 | 4014 | { |
bd275681 | 4015 | perf_event_context_sched_in(task); |
d010b332 | 4016 | |
45ac1403 AH |
4017 | if (atomic_read(&nr_switch_events)) |
4018 | perf_event_switch(task, prev, true); | |
a5398bff KL |
4019 | |
4020 | if (__this_cpu_read(perf_sched_cb_usages)) | |
4021 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
4022 | } |
4023 | ||
abd50713 PZ |
4024 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
4025 | { | |
4026 | u64 frequency = event->attr.sample_freq; | |
4027 | u64 sec = NSEC_PER_SEC; | |
4028 | u64 divisor, dividend; | |
4029 | ||
4030 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
4031 | ||
4032 | count_fls = fls64(count); | |
4033 | nsec_fls = fls64(nsec); | |
4034 | frequency_fls = fls64(frequency); | |
4035 | sec_fls = 30; | |
4036 | ||
4037 | /* | |
4038 | * We got @count in @nsec, with a target of sample_freq HZ | |
4039 | * the target period becomes: | |
4040 | * | |
4041 | * @count * 10^9 | |
4042 | * period = ------------------- | |
4043 | * @nsec * sample_freq | |
4044 | * | |
4045 | */ | |
4046 | ||
4047 | /* | |
4048 | * Reduce accuracy by one bit such that @a and @b converge | |
4049 | * to a similar magnitude. | |
4050 | */ | |
fe4b04fa | 4051 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
4052 | do { \ |
4053 | if (a##_fls > b##_fls) { \ | |
4054 | a >>= 1; \ | |
4055 | a##_fls--; \ | |
4056 | } else { \ | |
4057 | b >>= 1; \ | |
4058 | b##_fls--; \ | |
4059 | } \ | |
4060 | } while (0) | |
4061 | ||
4062 | /* | |
4063 | * Reduce accuracy until either term fits in a u64, then proceed with | |
4064 | * the other, so that finally we can do a u64/u64 division. | |
4065 | */ | |
4066 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
4067 | REDUCE_FLS(nsec, frequency); | |
4068 | REDUCE_FLS(sec, count); | |
4069 | } | |
4070 | ||
4071 | if (count_fls + sec_fls > 64) { | |
4072 | divisor = nsec * frequency; | |
4073 | ||
4074 | while (count_fls + sec_fls > 64) { | |
4075 | REDUCE_FLS(count, sec); | |
4076 | divisor >>= 1; | |
4077 | } | |
4078 | ||
4079 | dividend = count * sec; | |
4080 | } else { | |
4081 | dividend = count * sec; | |
4082 | ||
4083 | while (nsec_fls + frequency_fls > 64) { | |
4084 | REDUCE_FLS(nsec, frequency); | |
4085 | dividend >>= 1; | |
4086 | } | |
4087 | ||
4088 | divisor = nsec * frequency; | |
4089 | } | |
4090 | ||
f6ab91ad PZ |
4091 | if (!divisor) |
4092 | return dividend; | |
4093 | ||
abd50713 PZ |
4094 | return div64_u64(dividend, divisor); |
4095 | } | |
4096 | ||
e050e3f0 SE |
4097 | static DEFINE_PER_CPU(int, perf_throttled_count); |
4098 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
4099 | ||
f39d47ff | 4100 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 4101 | { |
cdd6c482 | 4102 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 4103 | s64 period, sample_period; |
bd2b5b12 PZ |
4104 | s64 delta; |
4105 | ||
abd50713 | 4106 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
4107 | |
4108 | delta = (s64)(period - hwc->sample_period); | |
4109 | delta = (delta + 7) / 8; /* low pass filter */ | |
4110 | ||
4111 | sample_period = hwc->sample_period + delta; | |
4112 | ||
4113 | if (!sample_period) | |
4114 | sample_period = 1; | |
4115 | ||
bd2b5b12 | 4116 | hwc->sample_period = sample_period; |
abd50713 | 4117 | |
e7850595 | 4118 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
4119 | if (disable) |
4120 | event->pmu->stop(event, PERF_EF_UPDATE); | |
4121 | ||
e7850595 | 4122 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
4123 | |
4124 | if (disable) | |
4125 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 4126 | } |
bd2b5b12 PZ |
4127 | } |
4128 | ||
0259bf63 | 4129 | static void perf_adjust_freq_unthr_events(struct list_head *event_list) |
60db5e09 | 4130 | { |
cdd6c482 IM |
4131 | struct perf_event *event; |
4132 | struct hw_perf_event *hwc; | |
e050e3f0 | 4133 | u64 now, period = TICK_NSEC; |
abd50713 | 4134 | s64 delta; |
60db5e09 | 4135 | |
0259bf63 | 4136 | list_for_each_entry(event, event_list, active_list) { |
cdd6c482 | 4137 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
4138 | continue; |
4139 | ||
bd275681 | 4140 | // XXX use visit thingy to avoid the -1,cpu match |
5632ab12 | 4141 | if (!event_filter_match(event)) |
5d27c23d PZ |
4142 | continue; |
4143 | ||
cdd6c482 | 4144 | hwc = &event->hw; |
6a24ed6c | 4145 | |
ae23bff1 | 4146 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 4147 | hwc->interrupts = 0; |
cdd6c482 | 4148 | perf_log_throttle(event, 1); |
f38628b0 NK |
4149 | if (!event->attr.freq || !event->attr.sample_freq) |
4150 | event->pmu->start(event, 0); | |
a78ac325 PZ |
4151 | } |
4152 | ||
cdd6c482 | 4153 | if (!event->attr.freq || !event->attr.sample_freq) |
0259bf63 | 4154 | continue; |
60db5e09 | 4155 | |
e050e3f0 SE |
4156 | /* |
4157 | * stop the event and update event->count | |
4158 | */ | |
4159 | event->pmu->stop(event, PERF_EF_UPDATE); | |
4160 | ||
e7850595 | 4161 | now = local64_read(&event->count); |
abd50713 PZ |
4162 | delta = now - hwc->freq_count_stamp; |
4163 | hwc->freq_count_stamp = now; | |
60db5e09 | 4164 | |
e050e3f0 SE |
4165 | /* |
4166 | * restart the event | |
4167 | * reload only if value has changed | |
f39d47ff SE |
4168 | * we have stopped the event so tell that |
4169 | * to perf_adjust_period() to avoid stopping it | |
4170 | * twice. | |
e050e3f0 | 4171 | */ |
abd50713 | 4172 | if (delta > 0) |
f39d47ff | 4173 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
4174 | |
4175 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
0259bf63 NK |
4176 | } |
4177 | } | |
4178 | ||
4179 | /* | |
4180 | * combine freq adjustment with unthrottling to avoid two passes over the | |
4181 | * events. At the same time, make sure, having freq events does not change | |
4182 | * the rate of unthrottling as that would introduce bias. | |
4183 | */ | |
4184 | static void | |
4185 | perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) | |
4186 | { | |
4187 | struct perf_event_pmu_context *pmu_ctx; | |
4188 | ||
4189 | /* | |
4190 | * only need to iterate over all events iff: | |
4191 | * - context have events in frequency mode (needs freq adjust) | |
4192 | * - there are events to unthrottle on this cpu | |
4193 | */ | |
4194 | if (!(ctx->nr_freq || unthrottle)) | |
4195 | return; | |
4196 | ||
4197 | raw_spin_lock(&ctx->lock); | |
4198 | ||
4199 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { | |
4200 | if (!(pmu_ctx->nr_freq || unthrottle)) | |
4201 | continue; | |
4202 | if (!perf_pmu_ctx_is_active(pmu_ctx)) | |
4203 | continue; | |
4204 | if (pmu_ctx->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) | |
4205 | continue; | |
4206 | ||
4207 | perf_pmu_disable(pmu_ctx->pmu); | |
4208 | perf_adjust_freq_unthr_events(&pmu_ctx->pinned_active); | |
4209 | perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active); | |
4210 | perf_pmu_enable(pmu_ctx->pmu); | |
60db5e09 | 4211 | } |
e050e3f0 SE |
4212 | |
4213 | raw_spin_unlock(&ctx->lock); | |
60db5e09 PZ |
4214 | } |
4215 | ||
235c7fc7 | 4216 | /* |
8703a7cf | 4217 | * Move @event to the tail of the @ctx's elegible events. |
235c7fc7 | 4218 | */ |
8703a7cf | 4219 | static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event) |
0793a61d | 4220 | { |
dddd3379 TG |
4221 | /* |
4222 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
4223 | * disabled by the inheritance code. | |
4224 | */ | |
8703a7cf PZ |
4225 | if (ctx->rotate_disable) |
4226 | return; | |
8e1a2031 | 4227 | |
8703a7cf PZ |
4228 | perf_event_groups_delete(&ctx->flexible_groups, event); |
4229 | perf_event_groups_insert(&ctx->flexible_groups, event); | |
235c7fc7 IM |
4230 | } |
4231 | ||
7fa343b7 | 4232 | /* pick an event from the flexible_groups to rotate */ |
8d5bce0c | 4233 | static inline struct perf_event * |
bd275681 | 4234 | ctx_event_to_rotate(struct perf_event_pmu_context *pmu_ctx) |
235c7fc7 | 4235 | { |
7fa343b7 | 4236 | struct perf_event *event; |
bd275681 PZ |
4237 | struct rb_node *node; |
4238 | struct rb_root *tree; | |
4239 | struct __group_key key = { | |
4240 | .pmu = pmu_ctx->pmu, | |
4241 | }; | |
7fa343b7 SL |
4242 | |
4243 | /* pick the first active flexible event */ | |
bd275681 | 4244 | event = list_first_entry_or_null(&pmu_ctx->flexible_active, |
7fa343b7 | 4245 | struct perf_event, active_list); |
bd275681 PZ |
4246 | if (event) |
4247 | goto out; | |
7fa343b7 SL |
4248 | |
4249 | /* if no active flexible event, pick the first event */ | |
bd275681 | 4250 | tree = &pmu_ctx->ctx->flexible_groups.tree; |
7fa343b7 | 4251 | |
bd275681 PZ |
4252 | if (!pmu_ctx->ctx->task) { |
4253 | key.cpu = smp_processor_id(); | |
4254 | ||
4255 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4256 | if (node) | |
4257 | event = __node_2_pe(node); | |
4258 | goto out; | |
7fa343b7 SL |
4259 | } |
4260 | ||
bd275681 PZ |
4261 | key.cpu = -1; |
4262 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4263 | if (node) { | |
4264 | event = __node_2_pe(node); | |
4265 | goto out; | |
4266 | } | |
4267 | ||
4268 | key.cpu = smp_processor_id(); | |
4269 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4270 | if (node) | |
4271 | event = __node_2_pe(node); | |
4272 | ||
4273 | out: | |
90c91dfb PZ |
4274 | /* |
4275 | * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in() | |
4276 | * finds there are unschedulable events, it will set it again. | |
4277 | */ | |
bd275681 | 4278 | pmu_ctx->rotate_necessary = 0; |
90c91dfb | 4279 | |
7fa343b7 | 4280 | return event; |
8d5bce0c PZ |
4281 | } |
4282 | ||
bd275681 | 4283 | static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc) |
8d5bce0c | 4284 | { |
bd275681 PZ |
4285 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4286 | struct perf_event_pmu_context *cpu_epc, *task_epc = NULL; | |
8d5bce0c | 4287 | struct perf_event *cpu_event = NULL, *task_event = NULL; |
fd7d5517 | 4288 | int cpu_rotate, task_rotate; |
bd275681 | 4289 | struct pmu *pmu; |
8d5bce0c PZ |
4290 | |
4291 | /* | |
4292 | * Since we run this from IRQ context, nobody can install new | |
4293 | * events, thus the event count values are stable. | |
4294 | */ | |
7fc23a53 | 4295 | |
bd275681 PZ |
4296 | cpu_epc = &cpc->epc; |
4297 | pmu = cpu_epc->pmu; | |
4298 | task_epc = cpc->task_epc; | |
4299 | ||
4300 | cpu_rotate = cpu_epc->rotate_necessary; | |
bd275681 | 4301 | task_rotate = task_epc ? task_epc->rotate_necessary : 0; |
9717e6cd | 4302 | |
8d5bce0c PZ |
4303 | if (!(cpu_rotate || task_rotate)) |
4304 | return false; | |
0f5a2601 | 4305 | |
facc4307 | 4306 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
bd275681 | 4307 | perf_pmu_disable(pmu); |
60db5e09 | 4308 | |
8d5bce0c | 4309 | if (task_rotate) |
bd275681 | 4310 | task_event = ctx_event_to_rotate(task_epc); |
8d5bce0c | 4311 | if (cpu_rotate) |
bd275681 | 4312 | cpu_event = ctx_event_to_rotate(cpu_epc); |
8703a7cf | 4313 | |
8d5bce0c PZ |
4314 | /* |
4315 | * As per the order given at ctx_resched() first 'pop' task flexible | |
4316 | * and then, if needed CPU flexible. | |
4317 | */ | |
bd275681 PZ |
4318 | if (task_event || (task_epc && cpu_event)) { |
4319 | update_context_time(task_epc->ctx); | |
4320 | __pmu_ctx_sched_out(task_epc, EVENT_FLEXIBLE); | |
4321 | } | |
0793a61d | 4322 | |
bd275681 PZ |
4323 | if (cpu_event) { |
4324 | update_context_time(&cpuctx->ctx); | |
4325 | __pmu_ctx_sched_out(cpu_epc, EVENT_FLEXIBLE); | |
8d5bce0c | 4326 | rotate_ctx(&cpuctx->ctx, cpu_event); |
bd275681 PZ |
4327 | __pmu_ctx_sched_in(&cpuctx->ctx, pmu); |
4328 | } | |
235c7fc7 | 4329 | |
bd275681 PZ |
4330 | if (task_event) |
4331 | rotate_ctx(task_epc->ctx, task_event); | |
235c7fc7 | 4332 | |
bd275681 PZ |
4333 | if (task_event || (task_epc && cpu_event)) |
4334 | __pmu_ctx_sched_in(task_epc->ctx, pmu); | |
235c7fc7 | 4335 | |
bd275681 | 4336 | perf_pmu_enable(pmu); |
0f5a2601 | 4337 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
9e630205 | 4338 | |
8d5bce0c | 4339 | return true; |
e9d2b064 PZ |
4340 | } |
4341 | ||
4342 | void perf_event_task_tick(void) | |
4343 | { | |
bd275681 PZ |
4344 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4345 | struct perf_event_context *ctx; | |
e050e3f0 | 4346 | int throttled; |
b5ab4cd5 | 4347 | |
16444645 | 4348 | lockdep_assert_irqs_disabled(); |
e9d2b064 | 4349 | |
e050e3f0 SE |
4350 | __this_cpu_inc(perf_throttled_seq); |
4351 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 4352 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 4353 | |
bd275681 PZ |
4354 | perf_adjust_freq_unthr_context(&cpuctx->ctx, !!throttled); |
4355 | ||
4356 | rcu_read_lock(); | |
4357 | ctx = rcu_dereference(current->perf_event_ctxp); | |
4358 | if (ctx) | |
4359 | perf_adjust_freq_unthr_context(ctx, !!throttled); | |
4360 | rcu_read_unlock(); | |
0793a61d TG |
4361 | } |
4362 | ||
889ff015 FW |
4363 | static int event_enable_on_exec(struct perf_event *event, |
4364 | struct perf_event_context *ctx) | |
4365 | { | |
4366 | if (!event->attr.enable_on_exec) | |
4367 | return 0; | |
4368 | ||
4369 | event->attr.enable_on_exec = 0; | |
4370 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
4371 | return 0; | |
4372 | ||
0d3d73aa | 4373 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
889ff015 FW |
4374 | |
4375 | return 1; | |
4376 | } | |
4377 | ||
57e7986e | 4378 | /* |
cdd6c482 | 4379 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
4380 | * This expects task == current. |
4381 | */ | |
bd275681 | 4382 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
57e7986e | 4383 | { |
bd275681 | 4384 | struct perf_event_context *clone_ctx = NULL; |
487f05e1 | 4385 | enum event_type_t event_type = 0; |
3e349507 | 4386 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 4387 | struct perf_event *event; |
57e7986e PM |
4388 | unsigned long flags; |
4389 | int enabled = 0; | |
4390 | ||
4391 | local_irq_save(flags); | |
bd275681 PZ |
4392 | if (WARN_ON_ONCE(current->perf_event_ctxp != ctx)) |
4393 | goto out; | |
4394 | ||
4395 | if (!ctx->nr_events) | |
57e7986e PM |
4396 | goto out; |
4397 | ||
bd275681 | 4398 | cpuctx = this_cpu_ptr(&perf_cpu_context); |
3e349507 | 4399 | perf_ctx_lock(cpuctx, ctx); |
bd275681 PZ |
4400 | ctx_sched_out(ctx, EVENT_TIME); |
4401 | ||
487f05e1 | 4402 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 4403 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
4404 | event_type |= get_event_type(event); |
4405 | } | |
57e7986e PM |
4406 | |
4407 | /* | |
3e349507 | 4408 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 4409 | */ |
3e349507 | 4410 | if (enabled) { |
211de6eb | 4411 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 4412 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb | 4413 | } else { |
bd275681 | 4414 | ctx_sched_in(ctx, EVENT_TIME); |
3e349507 PZ |
4415 | } |
4416 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 4417 | |
9ed6060d | 4418 | out: |
57e7986e | 4419 | local_irq_restore(flags); |
211de6eb PZ |
4420 | |
4421 | if (clone_ctx) | |
4422 | put_ctx(clone_ctx); | |
57e7986e PM |
4423 | } |
4424 | ||
2e498d0a ME |
4425 | static void perf_remove_from_owner(struct perf_event *event); |
4426 | static void perf_event_exit_event(struct perf_event *event, | |
4427 | struct perf_event_context *ctx); | |
4428 | ||
4429 | /* | |
4430 | * Removes all events from the current task that have been marked | |
4431 | * remove-on-exec, and feeds their values back to parent events. | |
4432 | */ | |
bd275681 | 4433 | static void perf_event_remove_on_exec(struct perf_event_context *ctx) |
2e498d0a | 4434 | { |
bd275681 | 4435 | struct perf_event_context *clone_ctx = NULL; |
2e498d0a | 4436 | struct perf_event *event, *next; |
2e498d0a ME |
4437 | unsigned long flags; |
4438 | bool modified = false; | |
4439 | ||
2e498d0a ME |
4440 | mutex_lock(&ctx->mutex); |
4441 | ||
4442 | if (WARN_ON_ONCE(ctx->task != current)) | |
4443 | goto unlock; | |
4444 | ||
4445 | list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) { | |
4446 | if (!event->attr.remove_on_exec) | |
4447 | continue; | |
4448 | ||
4449 | if (!is_kernel_event(event)) | |
4450 | perf_remove_from_owner(event); | |
4451 | ||
4452 | modified = true; | |
4453 | ||
4454 | perf_event_exit_event(event, ctx); | |
4455 | } | |
4456 | ||
4457 | raw_spin_lock_irqsave(&ctx->lock, flags); | |
4458 | if (modified) | |
4459 | clone_ctx = unclone_ctx(ctx); | |
2e498d0a ME |
4460 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
4461 | ||
4462 | unlock: | |
4463 | mutex_unlock(&ctx->mutex); | |
4464 | ||
2e498d0a ME |
4465 | if (clone_ctx) |
4466 | put_ctx(clone_ctx); | |
4467 | } | |
4468 | ||
0492d4c5 PZ |
4469 | struct perf_read_data { |
4470 | struct perf_event *event; | |
4471 | bool group; | |
7d88962e | 4472 | int ret; |
0492d4c5 PZ |
4473 | }; |
4474 | ||
451d24d1 | 4475 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 4476 | { |
d6a2f903 DCC |
4477 | u16 local_pkg, event_pkg; |
4478 | ||
1765bb61 TK |
4479 | if ((unsigned)event_cpu >= nr_cpu_ids) |
4480 | return event_cpu; | |
4481 | ||
d6a2f903 | 4482 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { |
451d24d1 PZ |
4483 | int local_cpu = smp_processor_id(); |
4484 | ||
4485 | event_pkg = topology_physical_package_id(event_cpu); | |
4486 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
4487 | |
4488 | if (event_pkg == local_pkg) | |
4489 | return local_cpu; | |
4490 | } | |
4491 | ||
4492 | return event_cpu; | |
4493 | } | |
4494 | ||
0793a61d | 4495 | /* |
cdd6c482 | 4496 | * Cross CPU call to read the hardware event |
0793a61d | 4497 | */ |
cdd6c482 | 4498 | static void __perf_event_read(void *info) |
0793a61d | 4499 | { |
0492d4c5 PZ |
4500 | struct perf_read_data *data = info; |
4501 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 4502 | struct perf_event_context *ctx = event->ctx; |
bd275681 | 4503 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4a00c16e | 4504 | struct pmu *pmu = event->pmu; |
621a01ea | 4505 | |
e1ac3614 PM |
4506 | /* |
4507 | * If this is a task context, we need to check whether it is | |
4508 | * the current task context of this cpu. If not it has been | |
4509 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
4510 | * event->count would have been updated to a recent sample |
4511 | * when the event was scheduled out. | |
e1ac3614 PM |
4512 | */ |
4513 | if (ctx->task && cpuctx->task_ctx != ctx) | |
4514 | return; | |
4515 | ||
e625cce1 | 4516 | raw_spin_lock(&ctx->lock); |
0c1cbc18 | 4517 | if (ctx->is_active & EVENT_TIME) { |
542e72fc | 4518 | update_context_time(ctx); |
e5d1367f SE |
4519 | update_cgrp_time_from_event(event); |
4520 | } | |
0492d4c5 | 4521 | |
0d3d73aa PZ |
4522 | perf_event_update_time(event); |
4523 | if (data->group) | |
4524 | perf_event_update_sibling_time(event); | |
0c1cbc18 | 4525 | |
4a00c16e SB |
4526 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
4527 | goto unlock; | |
0492d4c5 | 4528 | |
4a00c16e SB |
4529 | if (!data->group) { |
4530 | pmu->read(event); | |
4531 | data->ret = 0; | |
0492d4c5 | 4532 | goto unlock; |
4a00c16e SB |
4533 | } |
4534 | ||
4535 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
4536 | ||
4537 | pmu->read(event); | |
0492d4c5 | 4538 | |
edb39592 | 4539 | for_each_sibling_event(sub, event) { |
4a00c16e SB |
4540 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
4541 | /* | |
4542 | * Use sibling's PMU rather than @event's since | |
4543 | * sibling could be on different (eg: software) PMU. | |
4544 | */ | |
0492d4c5 | 4545 | sub->pmu->read(sub); |
4a00c16e | 4546 | } |
0492d4c5 | 4547 | } |
4a00c16e SB |
4548 | |
4549 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
4550 | |
4551 | unlock: | |
e625cce1 | 4552 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
4553 | } |
4554 | ||
b5e58793 PZ |
4555 | static inline u64 perf_event_count(struct perf_event *event) |
4556 | { | |
c39a0e2c | 4557 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
4558 | } |
4559 | ||
09f5e7dc PZ |
4560 | static void calc_timer_values(struct perf_event *event, |
4561 | u64 *now, | |
4562 | u64 *enabled, | |
4563 | u64 *running) | |
4564 | { | |
4565 | u64 ctx_time; | |
4566 | ||
4567 | *now = perf_clock(); | |
4568 | ctx_time = perf_event_time_now(event, *now); | |
4569 | __perf_update_times(event, ctx_time, enabled, running); | |
4570 | } | |
4571 | ||
ffe8690c KX |
4572 | /* |
4573 | * NMI-safe method to read a local event, that is an event that | |
4574 | * is: | |
4575 | * - either for the current task, or for this CPU | |
4576 | * - does not have inherit set, for inherited task events | |
4577 | * will not be local and we cannot read them atomically | |
4578 | * - must not have a pmu::count method | |
4579 | */ | |
7d9285e8 YS |
4580 | int perf_event_read_local(struct perf_event *event, u64 *value, |
4581 | u64 *enabled, u64 *running) | |
ffe8690c KX |
4582 | { |
4583 | unsigned long flags; | |
1765bb61 TK |
4584 | int event_oncpu; |
4585 | int event_cpu; | |
f91840a3 | 4586 | int ret = 0; |
ffe8690c KX |
4587 | |
4588 | /* | |
4589 | * Disabling interrupts avoids all counter scheduling (context | |
4590 | * switches, timer based rotation and IPIs). | |
4591 | */ | |
4592 | local_irq_save(flags); | |
4593 | ||
ffe8690c KX |
4594 | /* |
4595 | * It must not be an event with inherit set, we cannot read | |
4596 | * all child counters from atomic context. | |
4597 | */ | |
f91840a3 AS |
4598 | if (event->attr.inherit) { |
4599 | ret = -EOPNOTSUPP; | |
4600 | goto out; | |
4601 | } | |
ffe8690c | 4602 | |
f91840a3 AS |
4603 | /* If this is a per-task event, it must be for current */ |
4604 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
4605 | event->hw.target != current) { | |
4606 | ret = -EINVAL; | |
4607 | goto out; | |
4608 | } | |
4609 | ||
1765bb61 TK |
4610 | /* |
4611 | * Get the event CPU numbers, and adjust them to local if the event is | |
4612 | * a per-package event that can be read locally | |
4613 | */ | |
4614 | event_oncpu = __perf_event_read_cpu(event, event->oncpu); | |
4615 | event_cpu = __perf_event_read_cpu(event, event->cpu); | |
4616 | ||
f91840a3 AS |
4617 | /* If this is a per-CPU event, it must be for this CPU */ |
4618 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
1765bb61 | 4619 | event_cpu != smp_processor_id()) { |
f91840a3 AS |
4620 | ret = -EINVAL; |
4621 | goto out; | |
4622 | } | |
ffe8690c | 4623 | |
befb1b3c | 4624 | /* If this is a pinned event it must be running on this CPU */ |
1765bb61 | 4625 | if (event->attr.pinned && event_oncpu != smp_processor_id()) { |
befb1b3c RC |
4626 | ret = -EBUSY; |
4627 | goto out; | |
4628 | } | |
4629 | ||
ffe8690c KX |
4630 | /* |
4631 | * If the event is currently on this CPU, its either a per-task event, | |
4632 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
4633 | * oncpu == -1). | |
4634 | */ | |
1765bb61 | 4635 | if (event_oncpu == smp_processor_id()) |
ffe8690c KX |
4636 | event->pmu->read(event); |
4637 | ||
f91840a3 | 4638 | *value = local64_read(&event->count); |
0d3d73aa | 4639 | if (enabled || running) { |
99643bab | 4640 | u64 __enabled, __running, __now; |
0d3d73aa | 4641 | |
09f5e7dc | 4642 | calc_timer_values(event, &__now, &__enabled, &__running); |
0d3d73aa PZ |
4643 | if (enabled) |
4644 | *enabled = __enabled; | |
4645 | if (running) | |
4646 | *running = __running; | |
4647 | } | |
f91840a3 | 4648 | out: |
ffe8690c KX |
4649 | local_irq_restore(flags); |
4650 | ||
f91840a3 | 4651 | return ret; |
ffe8690c KX |
4652 | } |
4653 | ||
7d88962e | 4654 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 4655 | { |
0c1cbc18 | 4656 | enum perf_event_state state = READ_ONCE(event->state); |
451d24d1 | 4657 | int event_cpu, ret = 0; |
7d88962e | 4658 | |
0793a61d | 4659 | /* |
cdd6c482 IM |
4660 | * If event is enabled and currently active on a CPU, update the |
4661 | * value in the event structure: | |
0793a61d | 4662 | */ |
0c1cbc18 PZ |
4663 | again: |
4664 | if (state == PERF_EVENT_STATE_ACTIVE) { | |
4665 | struct perf_read_data data; | |
4666 | ||
4667 | /* | |
4668 | * Orders the ->state and ->oncpu loads such that if we see | |
4669 | * ACTIVE we must also see the right ->oncpu. | |
4670 | * | |
4671 | * Matches the smp_wmb() from event_sched_in(). | |
4672 | */ | |
4673 | smp_rmb(); | |
d6a2f903 | 4674 | |
451d24d1 PZ |
4675 | event_cpu = READ_ONCE(event->oncpu); |
4676 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
4677 | return 0; | |
4678 | ||
0c1cbc18 PZ |
4679 | data = (struct perf_read_data){ |
4680 | .event = event, | |
4681 | .group = group, | |
4682 | .ret = 0, | |
4683 | }; | |
4684 | ||
451d24d1 PZ |
4685 | preempt_disable(); |
4686 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 4687 | |
58763148 PZ |
4688 | /* |
4689 | * Purposely ignore the smp_call_function_single() return | |
4690 | * value. | |
4691 | * | |
451d24d1 | 4692 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
4693 | * scheduled out and that will have updated the event count. |
4694 | * | |
4695 | * Therefore, either way, we'll have an up-to-date event count | |
4696 | * after this. | |
4697 | */ | |
451d24d1 PZ |
4698 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
4699 | preempt_enable(); | |
58763148 | 4700 | ret = data.ret; |
0c1cbc18 PZ |
4701 | |
4702 | } else if (state == PERF_EVENT_STATE_INACTIVE) { | |
2b8988c9 PZ |
4703 | struct perf_event_context *ctx = event->ctx; |
4704 | unsigned long flags; | |
4705 | ||
e625cce1 | 4706 | raw_spin_lock_irqsave(&ctx->lock, flags); |
0c1cbc18 PZ |
4707 | state = event->state; |
4708 | if (state != PERF_EVENT_STATE_INACTIVE) { | |
4709 | raw_spin_unlock_irqrestore(&ctx->lock, flags); | |
4710 | goto again; | |
4711 | } | |
4712 | ||
c530ccd9 | 4713 | /* |
0c1cbc18 PZ |
4714 | * May read while context is not active (e.g., thread is |
4715 | * blocked), in that case we cannot update context time | |
c530ccd9 | 4716 | */ |
0c1cbc18 | 4717 | if (ctx->is_active & EVENT_TIME) { |
c530ccd9 | 4718 | update_context_time(ctx); |
e5d1367f SE |
4719 | update_cgrp_time_from_event(event); |
4720 | } | |
0c1cbc18 | 4721 | |
0d3d73aa | 4722 | perf_event_update_time(event); |
0492d4c5 | 4723 | if (group) |
0d3d73aa | 4724 | perf_event_update_sibling_time(event); |
e625cce1 | 4725 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 4726 | } |
7d88962e SB |
4727 | |
4728 | return ret; | |
0793a61d TG |
4729 | } |
4730 | ||
a63eaf34 | 4731 | /* |
cdd6c482 | 4732 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 4733 | */ |
eb184479 | 4734 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 4735 | { |
e625cce1 | 4736 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 4737 | mutex_init(&ctx->mutex); |
bd275681 | 4738 | INIT_LIST_HEAD(&ctx->pmu_ctx_list); |
8e1a2031 AB |
4739 | perf_event_groups_init(&ctx->pinned_groups); |
4740 | perf_event_groups_init(&ctx->flexible_groups); | |
a63eaf34 | 4741 | INIT_LIST_HEAD(&ctx->event_list); |
8c94abbb | 4742 | refcount_set(&ctx->refcount, 1); |
eb184479 PZ |
4743 | } |
4744 | ||
bd275681 PZ |
4745 | static void |
4746 | __perf_init_event_pmu_context(struct perf_event_pmu_context *epc, struct pmu *pmu) | |
4747 | { | |
4748 | epc->pmu = pmu; | |
4749 | INIT_LIST_HEAD(&epc->pmu_ctx_entry); | |
4750 | INIT_LIST_HEAD(&epc->pinned_active); | |
4751 | INIT_LIST_HEAD(&epc->flexible_active); | |
4752 | atomic_set(&epc->refcount, 1); | |
4753 | } | |
4754 | ||
eb184479 | 4755 | static struct perf_event_context * |
bd275681 | 4756 | alloc_perf_context(struct task_struct *task) |
eb184479 PZ |
4757 | { |
4758 | struct perf_event_context *ctx; | |
4759 | ||
4760 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
4761 | if (!ctx) | |
4762 | return NULL; | |
4763 | ||
4764 | __perf_event_init_context(ctx); | |
7b3c92b8 MWO |
4765 | if (task) |
4766 | ctx->task = get_task_struct(task); | |
eb184479 PZ |
4767 | |
4768 | return ctx; | |
a63eaf34 PM |
4769 | } |
4770 | ||
2ebd4ffb MH |
4771 | static struct task_struct * |
4772 | find_lively_task_by_vpid(pid_t vpid) | |
4773 | { | |
4774 | struct task_struct *task; | |
0793a61d TG |
4775 | |
4776 | rcu_read_lock(); | |
2ebd4ffb | 4777 | if (!vpid) |
0793a61d TG |
4778 | task = current; |
4779 | else | |
2ebd4ffb | 4780 | task = find_task_by_vpid(vpid); |
0793a61d TG |
4781 | if (task) |
4782 | get_task_struct(task); | |
4783 | rcu_read_unlock(); | |
4784 | ||
4785 | if (!task) | |
4786 | return ERR_PTR(-ESRCH); | |
4787 | ||
2ebd4ffb | 4788 | return task; |
2ebd4ffb MH |
4789 | } |
4790 | ||
fe4b04fa PZ |
4791 | /* |
4792 | * Returns a matching context with refcount and pincount. | |
4793 | */ | |
108b02cf | 4794 | static struct perf_event_context * |
bd275681 | 4795 | find_get_context(struct task_struct *task, struct perf_event *event) |
0793a61d | 4796 | { |
211de6eb | 4797 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 4798 | struct perf_cpu_context *cpuctx; |
25346b93 | 4799 | unsigned long flags; |
bd275681 | 4800 | int err; |
0793a61d | 4801 | |
22a4ec72 | 4802 | if (!task) { |
cdd6c482 | 4803 | /* Must be root to operate on a CPU event: */ |
da97e184 JFG |
4804 | err = perf_allow_cpu(&event->attr); |
4805 | if (err) | |
4806 | return ERR_PTR(err); | |
0793a61d | 4807 | |
bd275681 | 4808 | cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); |
0793a61d | 4809 | ctx = &cpuctx->ctx; |
c93f7669 | 4810 | get_ctx(ctx); |
6c605f83 | 4811 | raw_spin_lock_irqsave(&ctx->lock, flags); |
fe4b04fa | 4812 | ++ctx->pin_count; |
6c605f83 | 4813 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 4814 | |
0793a61d TG |
4815 | return ctx; |
4816 | } | |
4817 | ||
8dc85d54 | 4818 | err = -EINVAL; |
9ed6060d | 4819 | retry: |
bd275681 | 4820 | ctx = perf_lock_task_context(task, &flags); |
c93f7669 | 4821 | if (ctx) { |
211de6eb | 4822 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 4823 | ++ctx->pin_count; |
4af57ef2 | 4824 | |
e625cce1 | 4825 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
4826 | |
4827 | if (clone_ctx) | |
4828 | put_ctx(clone_ctx); | |
9137fb28 | 4829 | } else { |
bd275681 | 4830 | ctx = alloc_perf_context(task); |
c93f7669 PM |
4831 | err = -ENOMEM; |
4832 | if (!ctx) | |
4833 | goto errout; | |
eb184479 | 4834 | |
dbe08d82 ON |
4835 | err = 0; |
4836 | mutex_lock(&task->perf_event_mutex); | |
4837 | /* | |
4838 | * If it has already passed perf_event_exit_task(). | |
4839 | * we must see PF_EXITING, it takes this mutex too. | |
4840 | */ | |
4841 | if (task->flags & PF_EXITING) | |
4842 | err = -ESRCH; | |
bd275681 | 4843 | else if (task->perf_event_ctxp) |
dbe08d82 | 4844 | err = -EAGAIN; |
fe4b04fa | 4845 | else { |
9137fb28 | 4846 | get_ctx(ctx); |
fe4b04fa | 4847 | ++ctx->pin_count; |
bd275681 | 4848 | rcu_assign_pointer(task->perf_event_ctxp, ctx); |
fe4b04fa | 4849 | } |
dbe08d82 ON |
4850 | mutex_unlock(&task->perf_event_mutex); |
4851 | ||
4852 | if (unlikely(err)) { | |
9137fb28 | 4853 | put_ctx(ctx); |
dbe08d82 ON |
4854 | |
4855 | if (err == -EAGAIN) | |
4856 | goto retry; | |
4857 | goto errout; | |
a63eaf34 PM |
4858 | } |
4859 | } | |
4860 | ||
0793a61d | 4861 | return ctx; |
c93f7669 | 4862 | |
9ed6060d | 4863 | errout: |
c93f7669 | 4864 | return ERR_PTR(err); |
0793a61d TG |
4865 | } |
4866 | ||
bd275681 PZ |
4867 | static struct perf_event_pmu_context * |
4868 | find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx, | |
4869 | struct perf_event *event) | |
4870 | { | |
4871 | struct perf_event_pmu_context *new = NULL, *epc; | |
4872 | void *task_ctx_data = NULL; | |
4873 | ||
4874 | if (!ctx->task) { | |
889c58b3 PZ |
4875 | /* |
4876 | * perf_pmu_migrate_context() / __perf_pmu_install_event() | |
4877 | * relies on the fact that find_get_pmu_context() cannot fail | |
4878 | * for CPU contexts. | |
4879 | */ | |
bd275681 PZ |
4880 | struct perf_cpu_pmu_context *cpc; |
4881 | ||
4882 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu); | |
4883 | epc = &cpc->epc; | |
4f64a6c9 | 4884 | raw_spin_lock_irq(&ctx->lock); |
bd275681 PZ |
4885 | if (!epc->ctx) { |
4886 | atomic_set(&epc->refcount, 1); | |
4887 | epc->embedded = 1; | |
bd275681 PZ |
4888 | list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); |
4889 | epc->ctx = ctx; | |
bd275681 PZ |
4890 | } else { |
4891 | WARN_ON_ONCE(epc->ctx != ctx); | |
4892 | atomic_inc(&epc->refcount); | |
4893 | } | |
4f64a6c9 | 4894 | raw_spin_unlock_irq(&ctx->lock); |
bd275681 PZ |
4895 | return epc; |
4896 | } | |
4897 | ||
4898 | new = kzalloc(sizeof(*epc), GFP_KERNEL); | |
4899 | if (!new) | |
4900 | return ERR_PTR(-ENOMEM); | |
4901 | ||
4902 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { | |
4903 | task_ctx_data = alloc_task_ctx_data(pmu); | |
4904 | if (!task_ctx_data) { | |
4905 | kfree(new); | |
4906 | return ERR_PTR(-ENOMEM); | |
4907 | } | |
4908 | } | |
4909 | ||
4910 | __perf_init_event_pmu_context(new, pmu); | |
4911 | ||
4912 | /* | |
4913 | * XXX | |
4914 | * | |
4915 | * lockdep_assert_held(&ctx->mutex); | |
4916 | * | |
4917 | * can't because perf_event_init_task() doesn't actually hold the | |
4918 | * child_ctx->mutex. | |
4919 | */ | |
4920 | ||
4921 | raw_spin_lock_irq(&ctx->lock); | |
4922 | list_for_each_entry(epc, &ctx->pmu_ctx_list, pmu_ctx_entry) { | |
4923 | if (epc->pmu == pmu) { | |
4924 | WARN_ON_ONCE(epc->ctx != ctx); | |
4925 | atomic_inc(&epc->refcount); | |
4926 | goto found_epc; | |
4927 | } | |
4928 | } | |
4929 | ||
4930 | epc = new; | |
4931 | new = NULL; | |
4932 | ||
4933 | list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); | |
4934 | epc->ctx = ctx; | |
4935 | ||
4936 | found_epc: | |
4937 | if (task_ctx_data && !epc->task_ctx_data) { | |
4938 | epc->task_ctx_data = task_ctx_data; | |
4939 | task_ctx_data = NULL; | |
4940 | ctx->nr_task_data++; | |
4941 | } | |
4942 | raw_spin_unlock_irq(&ctx->lock); | |
4943 | ||
4944 | free_task_ctx_data(pmu, task_ctx_data); | |
4945 | kfree(new); | |
4946 | ||
4947 | return epc; | |
4948 | } | |
4949 | ||
4950 | static void get_pmu_ctx(struct perf_event_pmu_context *epc) | |
4951 | { | |
4952 | WARN_ON_ONCE(!atomic_inc_not_zero(&epc->refcount)); | |
4953 | } | |
4954 | ||
4955 | static void free_epc_rcu(struct rcu_head *head) | |
4956 | { | |
4957 | struct perf_event_pmu_context *epc = container_of(head, typeof(*epc), rcu_head); | |
4958 | ||
4959 | kfree(epc->task_ctx_data); | |
4960 | kfree(epc); | |
4961 | } | |
4962 | ||
4963 | static void put_pmu_ctx(struct perf_event_pmu_context *epc) | |
4964 | { | |
4f64a6c9 | 4965 | struct perf_event_context *ctx = epc->ctx; |
bd275681 PZ |
4966 | unsigned long flags; |
4967 | ||
4f64a6c9 JC |
4968 | /* |
4969 | * XXX | |
4970 | * | |
4971 | * lockdep_assert_held(&ctx->mutex); | |
4972 | * | |
4973 | * can't because of the call-site in _free_event()/put_event() | |
4974 | * which isn't always called under ctx->mutex. | |
4975 | */ | |
4976 | if (!atomic_dec_and_raw_lock_irqsave(&epc->refcount, &ctx->lock, flags)) | |
bd275681 PZ |
4977 | return; |
4978 | ||
4f64a6c9 | 4979 | WARN_ON_ONCE(list_empty(&epc->pmu_ctx_entry)); |
bd275681 | 4980 | |
4f64a6c9 JC |
4981 | list_del_init(&epc->pmu_ctx_entry); |
4982 | epc->ctx = NULL; | |
bd275681 PZ |
4983 | |
4984 | WARN_ON_ONCE(!list_empty(&epc->pinned_active)); | |
4985 | WARN_ON_ONCE(!list_empty(&epc->flexible_active)); | |
4986 | ||
4f64a6c9 JC |
4987 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
4988 | ||
bd275681 PZ |
4989 | if (epc->embedded) |
4990 | return; | |
4991 | ||
4992 | call_rcu(&epc->rcu_head, free_epc_rcu); | |
4993 | } | |
4994 | ||
6fb2915d LZ |
4995 | static void perf_event_free_filter(struct perf_event *event); |
4996 | ||
cdd6c482 | 4997 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 4998 | { |
bd275681 | 4999 | struct perf_event *event = container_of(head, typeof(*event), rcu_head); |
592903cd | 5000 | |
cdd6c482 IM |
5001 | if (event->ns) |
5002 | put_pid_ns(event->ns); | |
6fb2915d | 5003 | perf_event_free_filter(event); |
bdacfaf2 | 5004 | kmem_cache_free(perf_event_cache, event); |
592903cd PZ |
5005 | } |
5006 | ||
b69cf536 | 5007 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 5008 | struct perf_buffer *rb); |
925d519a | 5009 | |
f2fb6bef KL |
5010 | static void detach_sb_event(struct perf_event *event) |
5011 | { | |
5012 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
5013 | ||
5014 | raw_spin_lock(&pel->lock); | |
5015 | list_del_rcu(&event->sb_list); | |
5016 | raw_spin_unlock(&pel->lock); | |
5017 | } | |
5018 | ||
a4f144eb | 5019 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 5020 | { |
a4f144eb DCC |
5021 | struct perf_event_attr *attr = &event->attr; |
5022 | ||
f2fb6bef | 5023 | if (event->parent) |
a4f144eb | 5024 | return false; |
f2fb6bef KL |
5025 | |
5026 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 5027 | return false; |
f2fb6bef | 5028 | |
a4f144eb DCC |
5029 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
5030 | attr->comm || attr->comm_exec || | |
76193a94 | 5031 | attr->task || attr->ksymbol || |
e17d43b9 | 5032 | attr->context_switch || attr->text_poke || |
21038f2b | 5033 | attr->bpf_event) |
a4f144eb DCC |
5034 | return true; |
5035 | return false; | |
5036 | } | |
5037 | ||
5038 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
5039 | { | |
5040 | if (is_sb_event(event)) | |
5041 | detach_sb_event(event); | |
f2fb6bef KL |
5042 | } |
5043 | ||
555e0c1e FW |
5044 | #ifdef CONFIG_NO_HZ_FULL |
5045 | static DEFINE_SPINLOCK(nr_freq_lock); | |
5046 | #endif | |
5047 | ||
5048 | static void unaccount_freq_event_nohz(void) | |
5049 | { | |
5050 | #ifdef CONFIG_NO_HZ_FULL | |
5051 | spin_lock(&nr_freq_lock); | |
5052 | if (atomic_dec_and_test(&nr_freq_events)) | |
5053 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
5054 | spin_unlock(&nr_freq_lock); | |
5055 | #endif | |
5056 | } | |
5057 | ||
5058 | static void unaccount_freq_event(void) | |
5059 | { | |
5060 | if (tick_nohz_full_enabled()) | |
5061 | unaccount_freq_event_nohz(); | |
5062 | else | |
5063 | atomic_dec(&nr_freq_events); | |
5064 | } | |
5065 | ||
4beb31f3 FW |
5066 | static void unaccount_event(struct perf_event *event) |
5067 | { | |
25432ae9 PZ |
5068 | bool dec = false; |
5069 | ||
4beb31f3 FW |
5070 | if (event->parent) |
5071 | return; | |
5072 | ||
a5398bff | 5073 | if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) |
25432ae9 | 5074 | dec = true; |
4beb31f3 FW |
5075 | if (event->attr.mmap || event->attr.mmap_data) |
5076 | atomic_dec(&nr_mmap_events); | |
88a16a13 JO |
5077 | if (event->attr.build_id) |
5078 | atomic_dec(&nr_build_id_events); | |
4beb31f3 FW |
5079 | if (event->attr.comm) |
5080 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
5081 | if (event->attr.namespaces) |
5082 | atomic_dec(&nr_namespaces_events); | |
96aaab68 NK |
5083 | if (event->attr.cgroup) |
5084 | atomic_dec(&nr_cgroup_events); | |
4beb31f3 FW |
5085 | if (event->attr.task) |
5086 | atomic_dec(&nr_task_events); | |
948b26b6 | 5087 | if (event->attr.freq) |
555e0c1e | 5088 | unaccount_freq_event(); |
45ac1403 | 5089 | if (event->attr.context_switch) { |
25432ae9 | 5090 | dec = true; |
45ac1403 AH |
5091 | atomic_dec(&nr_switch_events); |
5092 | } | |
4beb31f3 | 5093 | if (is_cgroup_event(event)) |
25432ae9 | 5094 | dec = true; |
4beb31f3 | 5095 | if (has_branch_stack(event)) |
25432ae9 | 5096 | dec = true; |
76193a94 SL |
5097 | if (event->attr.ksymbol) |
5098 | atomic_dec(&nr_ksymbol_events); | |
6ee52e2a SL |
5099 | if (event->attr.bpf_event) |
5100 | atomic_dec(&nr_bpf_events); | |
e17d43b9 AH |
5101 | if (event->attr.text_poke) |
5102 | atomic_dec(&nr_text_poke_events); | |
25432ae9 | 5103 | |
9107c89e PZ |
5104 | if (dec) { |
5105 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
5106 | schedule_delayed_work(&perf_sched_work, HZ); | |
5107 | } | |
4beb31f3 | 5108 | |
f2fb6bef | 5109 | unaccount_pmu_sb_event(event); |
4beb31f3 | 5110 | } |
925d519a | 5111 | |
9107c89e PZ |
5112 | static void perf_sched_delayed(struct work_struct *work) |
5113 | { | |
5114 | mutex_lock(&perf_sched_mutex); | |
5115 | if (atomic_dec_and_test(&perf_sched_count)) | |
5116 | static_branch_disable(&perf_sched_events); | |
5117 | mutex_unlock(&perf_sched_mutex); | |
5118 | } | |
5119 | ||
bed5b25a AS |
5120 | /* |
5121 | * The following implement mutual exclusion of events on "exclusive" pmus | |
5122 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
5123 | * at a time, so we disallow creating events that might conflict, namely: | |
5124 | * | |
5125 | * 1) cpu-wide events in the presence of per-task events, | |
5126 | * 2) per-task events in the presence of cpu-wide events, | |
bd275681 | 5127 | * 3) two matching events on the same perf_event_context. |
bed5b25a AS |
5128 | * |
5129 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 5130 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
5131 | */ |
5132 | static int exclusive_event_init(struct perf_event *event) | |
5133 | { | |
5134 | struct pmu *pmu = event->pmu; | |
5135 | ||
8a58ddae | 5136 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
5137 | return 0; |
5138 | ||
5139 | /* | |
5140 | * Prevent co-existence of per-task and cpu-wide events on the | |
5141 | * same exclusive pmu. | |
5142 | * | |
5143 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
5144 | * events on this "exclusive" pmu, positive means there are | |
5145 | * per-task events. | |
5146 | * | |
5147 | * Since this is called in perf_event_alloc() path, event::ctx | |
5148 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
5149 | * to mean "per-task event", because unlike other attach states it | |
5150 | * never gets cleared. | |
5151 | */ | |
5152 | if (event->attach_state & PERF_ATTACH_TASK) { | |
5153 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
5154 | return -EBUSY; | |
5155 | } else { | |
5156 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
5157 | return -EBUSY; | |
5158 | } | |
5159 | ||
5160 | return 0; | |
5161 | } | |
5162 | ||
5163 | static void exclusive_event_destroy(struct perf_event *event) | |
5164 | { | |
5165 | struct pmu *pmu = event->pmu; | |
5166 | ||
8a58ddae | 5167 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
5168 | return; |
5169 | ||
5170 | /* see comment in exclusive_event_init() */ | |
5171 | if (event->attach_state & PERF_ATTACH_TASK) | |
5172 | atomic_dec(&pmu->exclusive_cnt); | |
5173 | else | |
5174 | atomic_inc(&pmu->exclusive_cnt); | |
5175 | } | |
5176 | ||
5177 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
5178 | { | |
3bf6215a | 5179 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
5180 | (e1->cpu == e2->cpu || |
5181 | e1->cpu == -1 || | |
5182 | e2->cpu == -1)) | |
5183 | return true; | |
5184 | return false; | |
5185 | } | |
5186 | ||
bed5b25a AS |
5187 | static bool exclusive_event_installable(struct perf_event *event, |
5188 | struct perf_event_context *ctx) | |
5189 | { | |
5190 | struct perf_event *iter_event; | |
5191 | struct pmu *pmu = event->pmu; | |
5192 | ||
8a58ddae AS |
5193 | lockdep_assert_held(&ctx->mutex); |
5194 | ||
5195 | if (!is_exclusive_pmu(pmu)) | |
bed5b25a AS |
5196 | return true; |
5197 | ||
5198 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
5199 | if (exclusive_event_match(iter_event, event)) | |
5200 | return false; | |
5201 | } | |
5202 | ||
5203 | return true; | |
5204 | } | |
5205 | ||
375637bc AS |
5206 | static void perf_addr_filters_splice(struct perf_event *event, |
5207 | struct list_head *head); | |
5208 | ||
683ede43 | 5209 | static void _free_event(struct perf_event *event) |
f1600952 | 5210 | { |
ca6c2132 | 5211 | irq_work_sync(&event->pending_irq); |
925d519a | 5212 | |
4beb31f3 | 5213 | unaccount_event(event); |
9ee318a7 | 5214 | |
da97e184 JFG |
5215 | security_perf_event_free(event); |
5216 | ||
76369139 | 5217 | if (event->rb) { |
9bb5d40c PZ |
5218 | /* |
5219 | * Can happen when we close an event with re-directed output. | |
5220 | * | |
5221 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
5222 | * over us; possibly making our ring_buffer_put() the last. | |
5223 | */ | |
5224 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 5225 | ring_buffer_attach(event, NULL); |
9bb5d40c | 5226 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
5227 | } |
5228 | ||
e5d1367f SE |
5229 | if (is_cgroup_event(event)) |
5230 | perf_detach_cgroup(event); | |
5231 | ||
a0733e69 PZ |
5232 | if (!event->parent) { |
5233 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
5234 | put_callchain_buffers(); | |
5235 | } | |
5236 | ||
5237 | perf_event_free_bpf_prog(event); | |
375637bc | 5238 | perf_addr_filters_splice(event, NULL); |
c60f83b8 | 5239 | kfree(event->addr_filter_ranges); |
a0733e69 PZ |
5240 | |
5241 | if (event->destroy) | |
5242 | event->destroy(event); | |
5243 | ||
1cf8dfe8 PZ |
5244 | /* |
5245 | * Must be after ->destroy(), due to uprobe_perf_close() using | |
5246 | * hw.target. | |
5247 | */ | |
621b6d2e PB |
5248 | if (event->hw.target) |
5249 | put_task_struct(event->hw.target); | |
5250 | ||
bd275681 PZ |
5251 | if (event->pmu_ctx) |
5252 | put_pmu_ctx(event->pmu_ctx); | |
5253 | ||
1cf8dfe8 PZ |
5254 | /* |
5255 | * perf_event_free_task() relies on put_ctx() being 'last', in particular | |
5256 | * all task references must be cleaned up. | |
5257 | */ | |
5258 | if (event->ctx) | |
5259 | put_ctx(event->ctx); | |
5260 | ||
62a92c8f AS |
5261 | exclusive_event_destroy(event); |
5262 | module_put(event->pmu->module); | |
a0733e69 PZ |
5263 | |
5264 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
5265 | } |
5266 | ||
683ede43 PZ |
5267 | /* |
5268 | * Used to free events which have a known refcount of 1, such as in error paths | |
5269 | * where the event isn't exposed yet and inherited events. | |
5270 | */ | |
5271 | static void free_event(struct perf_event *event) | |
0793a61d | 5272 | { |
683ede43 PZ |
5273 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
5274 | "unexpected event refcount: %ld; ptr=%p\n", | |
5275 | atomic_long_read(&event->refcount), event)) { | |
5276 | /* leak to avoid use-after-free */ | |
5277 | return; | |
5278 | } | |
0793a61d | 5279 | |
683ede43 | 5280 | _free_event(event); |
0793a61d TG |
5281 | } |
5282 | ||
a66a3052 | 5283 | /* |
f8697762 | 5284 | * Remove user event from the owner task. |
a66a3052 | 5285 | */ |
f8697762 | 5286 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 5287 | { |
8882135b | 5288 | struct task_struct *owner; |
fb0459d7 | 5289 | |
8882135b | 5290 | rcu_read_lock(); |
8882135b | 5291 | /* |
f47c02c0 PZ |
5292 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
5293 | * observe !owner it means the list deletion is complete and we can | |
5294 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
5295 | * owner->perf_event_mutex. |
5296 | */ | |
506458ef | 5297 | owner = READ_ONCE(event->owner); |
8882135b PZ |
5298 | if (owner) { |
5299 | /* | |
5300 | * Since delayed_put_task_struct() also drops the last | |
5301 | * task reference we can safely take a new reference | |
5302 | * while holding the rcu_read_lock(). | |
5303 | */ | |
5304 | get_task_struct(owner); | |
5305 | } | |
5306 | rcu_read_unlock(); | |
5307 | ||
5308 | if (owner) { | |
f63a8daa PZ |
5309 | /* |
5310 | * If we're here through perf_event_exit_task() we're already | |
5311 | * holding ctx->mutex which would be an inversion wrt. the | |
5312 | * normal lock order. | |
5313 | * | |
5314 | * However we can safely take this lock because its the child | |
5315 | * ctx->mutex. | |
5316 | */ | |
5317 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
5318 | ||
8882135b PZ |
5319 | /* |
5320 | * We have to re-check the event->owner field, if it is cleared | |
5321 | * we raced with perf_event_exit_task(), acquiring the mutex | |
5322 | * ensured they're done, and we can proceed with freeing the | |
5323 | * event. | |
5324 | */ | |
f47c02c0 | 5325 | if (event->owner) { |
8882135b | 5326 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
5327 | smp_store_release(&event->owner, NULL); |
5328 | } | |
8882135b PZ |
5329 | mutex_unlock(&owner->perf_event_mutex); |
5330 | put_task_struct(owner); | |
5331 | } | |
f8697762 JO |
5332 | } |
5333 | ||
f8697762 JO |
5334 | static void put_event(struct perf_event *event) |
5335 | { | |
f8697762 JO |
5336 | if (!atomic_long_dec_and_test(&event->refcount)) |
5337 | return; | |
5338 | ||
c6e5b732 PZ |
5339 | _free_event(event); |
5340 | } | |
5341 | ||
5342 | /* | |
5343 | * Kill an event dead; while event:refcount will preserve the event | |
5344 | * object, it will not preserve its functionality. Once the last 'user' | |
5345 | * gives up the object, we'll destroy the thing. | |
5346 | */ | |
5347 | int perf_event_release_kernel(struct perf_event *event) | |
5348 | { | |
a4f4bb6d | 5349 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 | 5350 | struct perf_event *child, *tmp; |
82d94856 | 5351 | LIST_HEAD(free_list); |
c6e5b732 | 5352 | |
a4f4bb6d | 5353 | /* |
bd275681 PZ |
5354 | * If we got here through err_alloc: free_event(event); we will not |
5355 | * have attached to a context yet. | |
a4f4bb6d PZ |
5356 | */ |
5357 | if (!ctx) { | |
5358 | WARN_ON_ONCE(event->attach_state & | |
5359 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
5360 | goto no_ctx; | |
5361 | } | |
5362 | ||
f8697762 JO |
5363 | if (!is_kernel_event(event)) |
5364 | perf_remove_from_owner(event); | |
8882135b | 5365 | |
5fa7c8ec | 5366 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 5367 | WARN_ON_ONCE(ctx->parent_ctx); |
683ede43 | 5368 | |
683ede43 | 5369 | /* |
d8a8cfc7 | 5370 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 5371 | * anymore. |
683ede43 | 5372 | * |
a69b0ca4 PZ |
5373 | * Anybody acquiring event->child_mutex after the below loop _must_ |
5374 | * also see this, most importantly inherit_event() which will avoid | |
5375 | * placing more children on the list. | |
683ede43 | 5376 | * |
c6e5b732 PZ |
5377 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
5378 | * child events. | |
683ede43 | 5379 | */ |
517e6a30 | 5380 | perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD); |
a69b0ca4 PZ |
5381 | |
5382 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 5383 | |
c6e5b732 PZ |
5384 | again: |
5385 | mutex_lock(&event->child_mutex); | |
5386 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 5387 | |
c6e5b732 PZ |
5388 | /* |
5389 | * Cannot change, child events are not migrated, see the | |
5390 | * comment with perf_event_ctx_lock_nested(). | |
5391 | */ | |
506458ef | 5392 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
5393 | /* |
5394 | * Since child_mutex nests inside ctx::mutex, we must jump | |
5395 | * through hoops. We start by grabbing a reference on the ctx. | |
5396 | * | |
5397 | * Since the event cannot get freed while we hold the | |
5398 | * child_mutex, the context must also exist and have a !0 | |
5399 | * reference count. | |
5400 | */ | |
5401 | get_ctx(ctx); | |
5402 | ||
5403 | /* | |
5404 | * Now that we have a ctx ref, we can drop child_mutex, and | |
5405 | * acquire ctx::mutex without fear of it going away. Then we | |
5406 | * can re-acquire child_mutex. | |
5407 | */ | |
5408 | mutex_unlock(&event->child_mutex); | |
5409 | mutex_lock(&ctx->mutex); | |
5410 | mutex_lock(&event->child_mutex); | |
5411 | ||
5412 | /* | |
5413 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
5414 | * state, if child is still the first entry, it didn't get freed | |
5415 | * and we can continue doing so. | |
5416 | */ | |
5417 | tmp = list_first_entry_or_null(&event->child_list, | |
5418 | struct perf_event, child_list); | |
5419 | if (tmp == child) { | |
5420 | perf_remove_from_context(child, DETACH_GROUP); | |
82d94856 | 5421 | list_move(&child->child_list, &free_list); |
c6e5b732 PZ |
5422 | /* |
5423 | * This matches the refcount bump in inherit_event(); | |
5424 | * this can't be the last reference. | |
5425 | */ | |
5426 | put_event(event); | |
5427 | } | |
5428 | ||
5429 | mutex_unlock(&event->child_mutex); | |
5430 | mutex_unlock(&ctx->mutex); | |
5431 | put_ctx(ctx); | |
5432 | goto again; | |
5433 | } | |
5434 | mutex_unlock(&event->child_mutex); | |
5435 | ||
82d94856 | 5436 | list_for_each_entry_safe(child, tmp, &free_list, child_list) { |
1cf8dfe8 PZ |
5437 | void *var = &child->ctx->refcount; |
5438 | ||
82d94856 PZ |
5439 | list_del(&child->child_list); |
5440 | free_event(child); | |
1cf8dfe8 PZ |
5441 | |
5442 | /* | |
5443 | * Wake any perf_event_free_task() waiting for this event to be | |
5444 | * freed. | |
5445 | */ | |
5446 | smp_mb(); /* pairs with wait_var_event() */ | |
5447 | wake_up_var(var); | |
82d94856 PZ |
5448 | } |
5449 | ||
a4f4bb6d PZ |
5450 | no_ctx: |
5451 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
5452 | return 0; |
5453 | } | |
5454 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
5455 | ||
8b10c5e2 PZ |
5456 | /* |
5457 | * Called when the last reference to the file is gone. | |
5458 | */ | |
a6fa941d AV |
5459 | static int perf_release(struct inode *inode, struct file *file) |
5460 | { | |
c6e5b732 | 5461 | perf_event_release_kernel(file->private_data); |
a6fa941d | 5462 | return 0; |
fb0459d7 | 5463 | } |
fb0459d7 | 5464 | |
ca0dd44c | 5465 | static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 5466 | { |
cdd6c482 | 5467 | struct perf_event *child; |
e53c0994 PZ |
5468 | u64 total = 0; |
5469 | ||
59ed446f PZ |
5470 | *enabled = 0; |
5471 | *running = 0; | |
5472 | ||
6f10581a | 5473 | mutex_lock(&event->child_mutex); |
01add3ea | 5474 | |
7d88962e | 5475 | (void)perf_event_read(event, false); |
01add3ea SB |
5476 | total += perf_event_count(event); |
5477 | ||
59ed446f PZ |
5478 | *enabled += event->total_time_enabled + |
5479 | atomic64_read(&event->child_total_time_enabled); | |
5480 | *running += event->total_time_running + | |
5481 | atomic64_read(&event->child_total_time_running); | |
5482 | ||
5483 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 5484 | (void)perf_event_read(child, false); |
01add3ea | 5485 | total += perf_event_count(child); |
59ed446f PZ |
5486 | *enabled += child->total_time_enabled; |
5487 | *running += child->total_time_running; | |
5488 | } | |
6f10581a | 5489 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
5490 | |
5491 | return total; | |
5492 | } | |
ca0dd44c PZ |
5493 | |
5494 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) | |
5495 | { | |
5496 | struct perf_event_context *ctx; | |
5497 | u64 count; | |
5498 | ||
5499 | ctx = perf_event_ctx_lock(event); | |
5500 | count = __perf_event_read_value(event, enabled, running); | |
5501 | perf_event_ctx_unlock(event, ctx); | |
5502 | ||
5503 | return count; | |
5504 | } | |
fb0459d7 | 5505 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 5506 | |
7d88962e | 5507 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 5508 | u64 read_format, u64 *values) |
3dab77fb | 5509 | { |
2aeb1883 | 5510 | struct perf_event_context *ctx = leader->ctx; |
32671e37 | 5511 | struct perf_event *sub, *parent; |
2aeb1883 | 5512 | unsigned long flags; |
fa8c2693 | 5513 | int n = 1; /* skip @nr */ |
7d88962e | 5514 | int ret; |
f63a8daa | 5515 | |
7d88962e SB |
5516 | ret = perf_event_read(leader, true); |
5517 | if (ret) | |
5518 | return ret; | |
abf4868b | 5519 | |
a9cd8194 | 5520 | raw_spin_lock_irqsave(&ctx->lock, flags); |
32671e37 PZ |
5521 | /* |
5522 | * Verify the grouping between the parent and child (inherited) | |
5523 | * events is still in tact. | |
5524 | * | |
5525 | * Specifically: | |
5526 | * - leader->ctx->lock pins leader->sibling_list | |
5527 | * - parent->child_mutex pins parent->child_list | |
5528 | * - parent->ctx->mutex pins parent->sibling_list | |
5529 | * | |
5530 | * Because parent->ctx != leader->ctx (and child_list nests inside | |
5531 | * ctx->mutex), group destruction is not atomic between children, also | |
5532 | * see perf_event_release_kernel(). Additionally, parent can grow the | |
5533 | * group. | |
5534 | * | |
5535 | * Therefore it is possible to have parent and child groups in a | |
5536 | * different configuration and summing over such a beast makes no sense | |
5537 | * what so ever. | |
5538 | * | |
5539 | * Reject this. | |
5540 | */ | |
5541 | parent = leader->parent; | |
5542 | if (parent && | |
5543 | (parent->group_generation != leader->group_generation || | |
5544 | parent->nr_siblings != leader->nr_siblings)) { | |
5545 | ret = -ECHILD; | |
5546 | goto unlock; | |
5547 | } | |
a9cd8194 | 5548 | |
fa8c2693 PZ |
5549 | /* |
5550 | * Since we co-schedule groups, {enabled,running} times of siblings | |
5551 | * will be identical to those of the leader, so we only publish one | |
5552 | * set. | |
5553 | */ | |
5554 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
5555 | values[n++] += leader->total_time_enabled + | |
5556 | atomic64_read(&leader->child_total_time_enabled); | |
5557 | } | |
3dab77fb | 5558 | |
fa8c2693 PZ |
5559 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
5560 | values[n++] += leader->total_time_running + | |
5561 | atomic64_read(&leader->child_total_time_running); | |
5562 | } | |
5563 | ||
5564 | /* | |
5565 | * Write {count,id} tuples for every sibling. | |
5566 | */ | |
5567 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
5568 | if (read_format & PERF_FORMAT_ID) |
5569 | values[n++] = primary_event_id(leader); | |
119a784c NK |
5570 | if (read_format & PERF_FORMAT_LOST) |
5571 | values[n++] = atomic64_read(&leader->lost_samples); | |
3dab77fb | 5572 | |
edb39592 | 5573 | for_each_sibling_event(sub, leader) { |
fa8c2693 PZ |
5574 | values[n++] += perf_event_count(sub); |
5575 | if (read_format & PERF_FORMAT_ID) | |
5576 | values[n++] = primary_event_id(sub); | |
119a784c NK |
5577 | if (read_format & PERF_FORMAT_LOST) |
5578 | values[n++] = atomic64_read(&sub->lost_samples); | |
fa8c2693 | 5579 | } |
7d88962e | 5580 | |
32671e37 | 5581 | unlock: |
2aeb1883 | 5582 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
32671e37 | 5583 | return ret; |
fa8c2693 | 5584 | } |
3dab77fb | 5585 | |
fa8c2693 PZ |
5586 | static int perf_read_group(struct perf_event *event, |
5587 | u64 read_format, char __user *buf) | |
5588 | { | |
5589 | struct perf_event *leader = event->group_leader, *child; | |
5590 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 5591 | int ret; |
fa8c2693 | 5592 | u64 *values; |
3dab77fb | 5593 | |
fa8c2693 | 5594 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 5595 | |
fa8c2693 PZ |
5596 | values = kzalloc(event->read_size, GFP_KERNEL); |
5597 | if (!values) | |
5598 | return -ENOMEM; | |
3dab77fb | 5599 | |
fa8c2693 PZ |
5600 | values[0] = 1 + leader->nr_siblings; |
5601 | ||
fa8c2693 | 5602 | mutex_lock(&leader->child_mutex); |
abf4868b | 5603 | |
7d88962e SB |
5604 | ret = __perf_read_group_add(leader, read_format, values); |
5605 | if (ret) | |
5606 | goto unlock; | |
5607 | ||
5608 | list_for_each_entry(child, &leader->child_list, child_list) { | |
5609 | ret = __perf_read_group_add(child, read_format, values); | |
5610 | if (ret) | |
5611 | goto unlock; | |
5612 | } | |
abf4868b | 5613 | |
fa8c2693 | 5614 | mutex_unlock(&leader->child_mutex); |
abf4868b | 5615 | |
7d88962e | 5616 | ret = event->read_size; |
fa8c2693 PZ |
5617 | if (copy_to_user(buf, values, event->read_size)) |
5618 | ret = -EFAULT; | |
7d88962e | 5619 | goto out; |
fa8c2693 | 5620 | |
7d88962e SB |
5621 | unlock: |
5622 | mutex_unlock(&leader->child_mutex); | |
5623 | out: | |
fa8c2693 | 5624 | kfree(values); |
abf4868b | 5625 | return ret; |
3dab77fb PZ |
5626 | } |
5627 | ||
b15f495b | 5628 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
5629 | u64 read_format, char __user *buf) |
5630 | { | |
59ed446f | 5631 | u64 enabled, running; |
119a784c | 5632 | u64 values[5]; |
3dab77fb PZ |
5633 | int n = 0; |
5634 | ||
ca0dd44c | 5635 | values[n++] = __perf_event_read_value(event, &enabled, &running); |
59ed446f PZ |
5636 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
5637 | values[n++] = enabled; | |
5638 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5639 | values[n++] = running; | |
3dab77fb | 5640 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5641 | values[n++] = primary_event_id(event); |
119a784c NK |
5642 | if (read_format & PERF_FORMAT_LOST) |
5643 | values[n++] = atomic64_read(&event->lost_samples); | |
3dab77fb PZ |
5644 | |
5645 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
5646 | return -EFAULT; | |
5647 | ||
5648 | return n * sizeof(u64); | |
5649 | } | |
5650 | ||
dc633982 JO |
5651 | static bool is_event_hup(struct perf_event *event) |
5652 | { | |
5653 | bool no_children; | |
5654 | ||
a69b0ca4 | 5655 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
5656 | return false; |
5657 | ||
5658 | mutex_lock(&event->child_mutex); | |
5659 | no_children = list_empty(&event->child_list); | |
5660 | mutex_unlock(&event->child_mutex); | |
5661 | return no_children; | |
5662 | } | |
5663 | ||
0793a61d | 5664 | /* |
cdd6c482 | 5665 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
5666 | */ |
5667 | static ssize_t | |
b15f495b | 5668 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 5669 | { |
cdd6c482 | 5670 | u64 read_format = event->attr.read_format; |
3dab77fb | 5671 | int ret; |
0793a61d | 5672 | |
3b6f9e5c | 5673 | /* |
788faab7 | 5674 | * Return end-of-file for a read on an event that is in |
3b6f9e5c PM |
5675 | * error state (i.e. because it was pinned but it couldn't be |
5676 | * scheduled on to the CPU at some point). | |
5677 | */ | |
cdd6c482 | 5678 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
5679 | return 0; |
5680 | ||
c320c7b7 | 5681 | if (count < event->read_size) |
3dab77fb PZ |
5682 | return -ENOSPC; |
5683 | ||
cdd6c482 | 5684 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 5685 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 5686 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 5687 | else |
b15f495b | 5688 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 5689 | |
3dab77fb | 5690 | return ret; |
0793a61d TG |
5691 | } |
5692 | ||
0793a61d TG |
5693 | static ssize_t |
5694 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
5695 | { | |
cdd6c482 | 5696 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
5697 | struct perf_event_context *ctx; |
5698 | int ret; | |
0793a61d | 5699 | |
da97e184 JFG |
5700 | ret = security_perf_event_read(event); |
5701 | if (ret) | |
5702 | return ret; | |
5703 | ||
f63a8daa | 5704 | ctx = perf_event_ctx_lock(event); |
b15f495b | 5705 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
5706 | perf_event_ctx_unlock(event, ctx); |
5707 | ||
5708 | return ret; | |
0793a61d TG |
5709 | } |
5710 | ||
9dd95748 | 5711 | static __poll_t perf_poll(struct file *file, poll_table *wait) |
0793a61d | 5712 | { |
cdd6c482 | 5713 | struct perf_event *event = file->private_data; |
56de4e8f | 5714 | struct perf_buffer *rb; |
a9a08845 | 5715 | __poll_t events = EPOLLHUP; |
c7138f37 | 5716 | |
e708d7ad | 5717 | poll_wait(file, &event->waitq, wait); |
179033b3 | 5718 | |
dc633982 | 5719 | if (is_event_hup(event)) |
179033b3 | 5720 | return events; |
c7138f37 | 5721 | |
10c6db11 | 5722 | /* |
9bb5d40c PZ |
5723 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
5724 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
5725 | */ |
5726 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
5727 | rb = event->rb; |
5728 | if (rb) | |
76369139 | 5729 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 5730 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
5731 | return events; |
5732 | } | |
5733 | ||
f63a8daa | 5734 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 5735 | { |
7d88962e | 5736 | (void)perf_event_read(event, false); |
e7850595 | 5737 | local64_set(&event->count, 0); |
cdd6c482 | 5738 | perf_event_update_userpage(event); |
3df5edad PZ |
5739 | } |
5740 | ||
52ba4b0b LX |
5741 | /* Assume it's not an event with inherit set. */ |
5742 | u64 perf_event_pause(struct perf_event *event, bool reset) | |
5743 | { | |
5744 | struct perf_event_context *ctx; | |
5745 | u64 count; | |
5746 | ||
5747 | ctx = perf_event_ctx_lock(event); | |
5748 | WARN_ON_ONCE(event->attr.inherit); | |
5749 | _perf_event_disable(event); | |
5750 | count = local64_read(&event->count); | |
5751 | if (reset) | |
5752 | local64_set(&event->count, 0); | |
5753 | perf_event_ctx_unlock(event, ctx); | |
5754 | ||
5755 | return count; | |
5756 | } | |
5757 | EXPORT_SYMBOL_GPL(perf_event_pause); | |
5758 | ||
c93f7669 | 5759 | /* |
cdd6c482 IM |
5760 | * Holding the top-level event's child_mutex means that any |
5761 | * descendant process that has inherited this event will block | |
8ba289b8 | 5762 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 5763 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 5764 | */ |
cdd6c482 IM |
5765 | static void perf_event_for_each_child(struct perf_event *event, |
5766 | void (*func)(struct perf_event *)) | |
3df5edad | 5767 | { |
cdd6c482 | 5768 | struct perf_event *child; |
3df5edad | 5769 | |
cdd6c482 | 5770 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 5771 | |
cdd6c482 IM |
5772 | mutex_lock(&event->child_mutex); |
5773 | func(event); | |
5774 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 5775 | func(child); |
cdd6c482 | 5776 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
5777 | } |
5778 | ||
cdd6c482 IM |
5779 | static void perf_event_for_each(struct perf_event *event, |
5780 | void (*func)(struct perf_event *)) | |
3df5edad | 5781 | { |
cdd6c482 IM |
5782 | struct perf_event_context *ctx = event->ctx; |
5783 | struct perf_event *sibling; | |
3df5edad | 5784 | |
f63a8daa PZ |
5785 | lockdep_assert_held(&ctx->mutex); |
5786 | ||
cdd6c482 | 5787 | event = event->group_leader; |
75f937f2 | 5788 | |
cdd6c482 | 5789 | perf_event_for_each_child(event, func); |
edb39592 | 5790 | for_each_sibling_event(sibling, event) |
724b6daa | 5791 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
5792 | } |
5793 | ||
fae3fde6 PZ |
5794 | static void __perf_event_period(struct perf_event *event, |
5795 | struct perf_cpu_context *cpuctx, | |
5796 | struct perf_event_context *ctx, | |
5797 | void *info) | |
c7999c6f | 5798 | { |
fae3fde6 | 5799 | u64 value = *((u64 *)info); |
c7999c6f | 5800 | bool active; |
08247e31 | 5801 | |
cdd6c482 | 5802 | if (event->attr.freq) { |
cdd6c482 | 5803 | event->attr.sample_freq = value; |
08247e31 | 5804 | } else { |
cdd6c482 IM |
5805 | event->attr.sample_period = value; |
5806 | event->hw.sample_period = value; | |
08247e31 | 5807 | } |
bad7192b PZ |
5808 | |
5809 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
5810 | if (active) { | |
bd275681 | 5811 | perf_pmu_disable(event->pmu); |
1e02cd40 PZ |
5812 | /* |
5813 | * We could be throttled; unthrottle now to avoid the tick | |
5814 | * trying to unthrottle while we already re-started the event. | |
5815 | */ | |
5816 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
5817 | event->hw.interrupts = 0; | |
5818 | perf_log_throttle(event, 1); | |
5819 | } | |
bad7192b PZ |
5820 | event->pmu->stop(event, PERF_EF_UPDATE); |
5821 | } | |
5822 | ||
5823 | local64_set(&event->hw.period_left, 0); | |
5824 | ||
5825 | if (active) { | |
5826 | event->pmu->start(event, PERF_EF_RELOAD); | |
bd275681 | 5827 | perf_pmu_enable(event->pmu); |
bad7192b | 5828 | } |
c7999c6f PZ |
5829 | } |
5830 | ||
81ec3f3c JO |
5831 | static int perf_event_check_period(struct perf_event *event, u64 value) |
5832 | { | |
5833 | return event->pmu->check_period(event, value); | |
5834 | } | |
5835 | ||
3ca270fc | 5836 | static int _perf_event_period(struct perf_event *event, u64 value) |
c7999c6f | 5837 | { |
c7999c6f PZ |
5838 | if (!is_sampling_event(event)) |
5839 | return -EINVAL; | |
5840 | ||
c7999c6f PZ |
5841 | if (!value) |
5842 | return -EINVAL; | |
5843 | ||
5844 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
5845 | return -EINVAL; | |
5846 | ||
81ec3f3c JO |
5847 | if (perf_event_check_period(event, value)) |
5848 | return -EINVAL; | |
5849 | ||
913a90bc RB |
5850 | if (!event->attr.freq && (value & (1ULL << 63))) |
5851 | return -EINVAL; | |
5852 | ||
fae3fde6 | 5853 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 5854 | |
c7999c6f | 5855 | return 0; |
08247e31 PZ |
5856 | } |
5857 | ||
3ca270fc LX |
5858 | int perf_event_period(struct perf_event *event, u64 value) |
5859 | { | |
5860 | struct perf_event_context *ctx; | |
5861 | int ret; | |
5862 | ||
5863 | ctx = perf_event_ctx_lock(event); | |
5864 | ret = _perf_event_period(event, value); | |
5865 | perf_event_ctx_unlock(event, ctx); | |
5866 | ||
5867 | return ret; | |
5868 | } | |
5869 | EXPORT_SYMBOL_GPL(perf_event_period); | |
5870 | ||
ac9721f3 PZ |
5871 | static const struct file_operations perf_fops; |
5872 | ||
2903ff01 | 5873 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 5874 | { |
2903ff01 AV |
5875 | struct fd f = fdget(fd); |
5876 | if (!f.file) | |
5877 | return -EBADF; | |
ac9721f3 | 5878 | |
2903ff01 AV |
5879 | if (f.file->f_op != &perf_fops) { |
5880 | fdput(f); | |
5881 | return -EBADF; | |
ac9721f3 | 5882 | } |
2903ff01 AV |
5883 | *p = f; |
5884 | return 0; | |
ac9721f3 PZ |
5885 | } |
5886 | ||
5887 | static int perf_event_set_output(struct perf_event *event, | |
5888 | struct perf_event *output_event); | |
6fb2915d | 5889 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
32ff77e8 MC |
5890 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
5891 | struct perf_event_attr *attr); | |
a4be7c27 | 5892 | |
f63a8daa | 5893 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 5894 | { |
cdd6c482 | 5895 | void (*func)(struct perf_event *); |
3df5edad | 5896 | u32 flags = arg; |
d859e29f PM |
5897 | |
5898 | switch (cmd) { | |
cdd6c482 | 5899 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 5900 | func = _perf_event_enable; |
d859e29f | 5901 | break; |
cdd6c482 | 5902 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 5903 | func = _perf_event_disable; |
79f14641 | 5904 | break; |
cdd6c482 | 5905 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 5906 | func = _perf_event_reset; |
6de6a7b9 | 5907 | break; |
3df5edad | 5908 | |
cdd6c482 | 5909 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 5910 | return _perf_event_refresh(event, arg); |
08247e31 | 5911 | |
cdd6c482 | 5912 | case PERF_EVENT_IOC_PERIOD: |
3ca270fc LX |
5913 | { |
5914 | u64 value; | |
08247e31 | 5915 | |
3ca270fc LX |
5916 | if (copy_from_user(&value, (u64 __user *)arg, sizeof(value))) |
5917 | return -EFAULT; | |
08247e31 | 5918 | |
3ca270fc LX |
5919 | return _perf_event_period(event, value); |
5920 | } | |
cf4957f1 JO |
5921 | case PERF_EVENT_IOC_ID: |
5922 | { | |
5923 | u64 id = primary_event_id(event); | |
5924 | ||
5925 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
5926 | return -EFAULT; | |
5927 | return 0; | |
5928 | } | |
5929 | ||
cdd6c482 | 5930 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 5931 | { |
ac9721f3 | 5932 | int ret; |
ac9721f3 | 5933 | if (arg != -1) { |
2903ff01 AV |
5934 | struct perf_event *output_event; |
5935 | struct fd output; | |
5936 | ret = perf_fget_light(arg, &output); | |
5937 | if (ret) | |
5938 | return ret; | |
5939 | output_event = output.file->private_data; | |
5940 | ret = perf_event_set_output(event, output_event); | |
5941 | fdput(output); | |
5942 | } else { | |
5943 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 5944 | } |
ac9721f3 PZ |
5945 | return ret; |
5946 | } | |
a4be7c27 | 5947 | |
6fb2915d LZ |
5948 | case PERF_EVENT_IOC_SET_FILTER: |
5949 | return perf_event_set_filter(event, (void __user *)arg); | |
5950 | ||
2541517c | 5951 | case PERF_EVENT_IOC_SET_BPF: |
652c1b17 AN |
5952 | { |
5953 | struct bpf_prog *prog; | |
5954 | int err; | |
5955 | ||
5956 | prog = bpf_prog_get(arg); | |
5957 | if (IS_ERR(prog)) | |
5958 | return PTR_ERR(prog); | |
5959 | ||
82e6b1ee | 5960 | err = perf_event_set_bpf_prog(event, prog, 0); |
652c1b17 AN |
5961 | if (err) { |
5962 | bpf_prog_put(prog); | |
5963 | return err; | |
5964 | } | |
5965 | ||
5966 | return 0; | |
5967 | } | |
2541517c | 5968 | |
86e7972f | 5969 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
56de4e8f | 5970 | struct perf_buffer *rb; |
86e7972f WN |
5971 | |
5972 | rcu_read_lock(); | |
5973 | rb = rcu_dereference(event->rb); | |
5974 | if (!rb || !rb->nr_pages) { | |
5975 | rcu_read_unlock(); | |
5976 | return -EINVAL; | |
5977 | } | |
5978 | rb_toggle_paused(rb, !!arg); | |
5979 | rcu_read_unlock(); | |
5980 | return 0; | |
5981 | } | |
f371b304 YS |
5982 | |
5983 | case PERF_EVENT_IOC_QUERY_BPF: | |
f4e2298e | 5984 | return perf_event_query_prog_array(event, (void __user *)arg); |
32ff77e8 MC |
5985 | |
5986 | case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: { | |
5987 | struct perf_event_attr new_attr; | |
5988 | int err = perf_copy_attr((struct perf_event_attr __user *)arg, | |
5989 | &new_attr); | |
5990 | ||
5991 | if (err) | |
5992 | return err; | |
5993 | ||
5994 | return perf_event_modify_attr(event, &new_attr); | |
5995 | } | |
d859e29f | 5996 | default: |
3df5edad | 5997 | return -ENOTTY; |
d859e29f | 5998 | } |
3df5edad PZ |
5999 | |
6000 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 6001 | perf_event_for_each(event, func); |
3df5edad | 6002 | else |
cdd6c482 | 6003 | perf_event_for_each_child(event, func); |
3df5edad PZ |
6004 | |
6005 | return 0; | |
d859e29f PM |
6006 | } |
6007 | ||
f63a8daa PZ |
6008 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
6009 | { | |
6010 | struct perf_event *event = file->private_data; | |
6011 | struct perf_event_context *ctx; | |
6012 | long ret; | |
6013 | ||
da97e184 JFG |
6014 | /* Treat ioctl like writes as it is likely a mutating operation. */ |
6015 | ret = security_perf_event_write(event); | |
6016 | if (ret) | |
6017 | return ret; | |
6018 | ||
f63a8daa PZ |
6019 | ctx = perf_event_ctx_lock(event); |
6020 | ret = _perf_ioctl(event, cmd, arg); | |
6021 | perf_event_ctx_unlock(event, ctx); | |
6022 | ||
6023 | return ret; | |
6024 | } | |
6025 | ||
b3f20785 PM |
6026 | #ifdef CONFIG_COMPAT |
6027 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
6028 | unsigned long arg) | |
6029 | { | |
6030 | switch (_IOC_NR(cmd)) { | |
6031 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
6032 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
82489c5f ES |
6033 | case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF): |
6034 | case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES): | |
b3f20785 PM |
6035 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
6036 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
6037 | cmd &= ~IOCSIZE_MASK; | |
6038 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
6039 | } | |
6040 | break; | |
6041 | } | |
6042 | return perf_ioctl(file, cmd, arg); | |
6043 | } | |
6044 | #else | |
6045 | # define perf_compat_ioctl NULL | |
6046 | #endif | |
6047 | ||
cdd6c482 | 6048 | int perf_event_task_enable(void) |
771d7cde | 6049 | { |
f63a8daa | 6050 | struct perf_event_context *ctx; |
cdd6c482 | 6051 | struct perf_event *event; |
771d7cde | 6052 | |
cdd6c482 | 6053 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
6054 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
6055 | ctx = perf_event_ctx_lock(event); | |
6056 | perf_event_for_each_child(event, _perf_event_enable); | |
6057 | perf_event_ctx_unlock(event, ctx); | |
6058 | } | |
cdd6c482 | 6059 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
6060 | |
6061 | return 0; | |
6062 | } | |
6063 | ||
cdd6c482 | 6064 | int perf_event_task_disable(void) |
771d7cde | 6065 | { |
f63a8daa | 6066 | struct perf_event_context *ctx; |
cdd6c482 | 6067 | struct perf_event *event; |
771d7cde | 6068 | |
cdd6c482 | 6069 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
6070 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
6071 | ctx = perf_event_ctx_lock(event); | |
6072 | perf_event_for_each_child(event, _perf_event_disable); | |
6073 | perf_event_ctx_unlock(event, ctx); | |
6074 | } | |
cdd6c482 | 6075 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
6076 | |
6077 | return 0; | |
6078 | } | |
6079 | ||
cdd6c482 | 6080 | static int perf_event_index(struct perf_event *event) |
194002b2 | 6081 | { |
a4eaf7f1 PZ |
6082 | if (event->hw.state & PERF_HES_STOPPED) |
6083 | return 0; | |
6084 | ||
cdd6c482 | 6085 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
6086 | return 0; |
6087 | ||
35edc2a5 | 6088 | return event->pmu->event_idx(event); |
194002b2 PZ |
6089 | } |
6090 | ||
fa731587 PZ |
6091 | static void perf_event_init_userpage(struct perf_event *event) |
6092 | { | |
6093 | struct perf_event_mmap_page *userpg; | |
56de4e8f | 6094 | struct perf_buffer *rb; |
fa731587 PZ |
6095 | |
6096 | rcu_read_lock(); | |
6097 | rb = rcu_dereference(event->rb); | |
6098 | if (!rb) | |
6099 | goto unlock; | |
6100 | ||
6101 | userpg = rb->user_page; | |
6102 | ||
6103 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
6104 | userpg->cap_bit0_is_deprecated = 1; | |
6105 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
6106 | userpg->data_offset = PAGE_SIZE; |
6107 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
6108 | |
6109 | unlock: | |
6110 | rcu_read_unlock(); | |
6111 | } | |
6112 | ||
c1317ec2 AL |
6113 | void __weak arch_perf_update_userpage( |
6114 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
6115 | { |
6116 | } | |
6117 | ||
38ff667b PZ |
6118 | /* |
6119 | * Callers need to ensure there can be no nesting of this function, otherwise | |
6120 | * the seqlock logic goes bad. We can not serialize this because the arch | |
6121 | * code calls this from NMI context. | |
6122 | */ | |
cdd6c482 | 6123 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 6124 | { |
cdd6c482 | 6125 | struct perf_event_mmap_page *userpg; |
56de4e8f | 6126 | struct perf_buffer *rb; |
e3f3541c | 6127 | u64 enabled, running, now; |
38ff667b PZ |
6128 | |
6129 | rcu_read_lock(); | |
5ec4c599 PZ |
6130 | rb = rcu_dereference(event->rb); |
6131 | if (!rb) | |
6132 | goto unlock; | |
6133 | ||
0d641208 EM |
6134 | /* |
6135 | * compute total_time_enabled, total_time_running | |
6136 | * based on snapshot values taken when the event | |
6137 | * was last scheduled in. | |
6138 | * | |
6139 | * we cannot simply called update_context_time() | |
6140 | * because of locking issue as we can be called in | |
6141 | * NMI context | |
6142 | */ | |
e3f3541c | 6143 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 6144 | |
76369139 | 6145 | userpg = rb->user_page; |
7b732a75 | 6146 | /* |
9d2dcc8f MF |
6147 | * Disable preemption to guarantee consistent time stamps are stored to |
6148 | * the user page. | |
7b732a75 PZ |
6149 | */ |
6150 | preempt_disable(); | |
37d81828 | 6151 | ++userpg->lock; |
92f22a38 | 6152 | barrier(); |
cdd6c482 | 6153 | userpg->index = perf_event_index(event); |
b5e58793 | 6154 | userpg->offset = perf_event_count(event); |
365a4038 | 6155 | if (userpg->index) |
e7850595 | 6156 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 6157 | |
0d641208 | 6158 | userpg->time_enabled = enabled + |
cdd6c482 | 6159 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 6160 | |
0d641208 | 6161 | userpg->time_running = running + |
cdd6c482 | 6162 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 6163 | |
c1317ec2 | 6164 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 6165 | |
92f22a38 | 6166 | barrier(); |
37d81828 | 6167 | ++userpg->lock; |
7b732a75 | 6168 | preempt_enable(); |
38ff667b | 6169 | unlock: |
7b732a75 | 6170 | rcu_read_unlock(); |
37d81828 | 6171 | } |
82975c46 | 6172 | EXPORT_SYMBOL_GPL(perf_event_update_userpage); |
37d81828 | 6173 | |
9e3ed2d7 | 6174 | static vm_fault_t perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 6175 | { |
11bac800 | 6176 | struct perf_event *event = vmf->vma->vm_file->private_data; |
56de4e8f | 6177 | struct perf_buffer *rb; |
9e3ed2d7 | 6178 | vm_fault_t ret = VM_FAULT_SIGBUS; |
906010b2 PZ |
6179 | |
6180 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
6181 | if (vmf->pgoff == 0) | |
6182 | ret = 0; | |
6183 | return ret; | |
6184 | } | |
6185 | ||
6186 | rcu_read_lock(); | |
76369139 FW |
6187 | rb = rcu_dereference(event->rb); |
6188 | if (!rb) | |
906010b2 PZ |
6189 | goto unlock; |
6190 | ||
6191 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
6192 | goto unlock; | |
6193 | ||
76369139 | 6194 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
6195 | if (!vmf->page) |
6196 | goto unlock; | |
6197 | ||
6198 | get_page(vmf->page); | |
11bac800 | 6199 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
6200 | vmf->page->index = vmf->pgoff; |
6201 | ||
6202 | ret = 0; | |
6203 | unlock: | |
6204 | rcu_read_unlock(); | |
6205 | ||
6206 | return ret; | |
6207 | } | |
6208 | ||
10c6db11 | 6209 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 6210 | struct perf_buffer *rb) |
10c6db11 | 6211 | { |
56de4e8f | 6212 | struct perf_buffer *old_rb = NULL; |
10c6db11 PZ |
6213 | unsigned long flags; |
6214 | ||
961c3912 JC |
6215 | WARN_ON_ONCE(event->parent); |
6216 | ||
b69cf536 PZ |
6217 | if (event->rb) { |
6218 | /* | |
6219 | * Should be impossible, we set this when removing | |
6220 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
6221 | */ | |
6222 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 6223 | |
b69cf536 | 6224 | old_rb = event->rb; |
b69cf536 PZ |
6225 | spin_lock_irqsave(&old_rb->event_lock, flags); |
6226 | list_del_rcu(&event->rb_entry); | |
6227 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 6228 | |
2f993cf0 ON |
6229 | event->rcu_batches = get_state_synchronize_rcu(); |
6230 | event->rcu_pending = 1; | |
b69cf536 | 6231 | } |
10c6db11 | 6232 | |
b69cf536 | 6233 | if (rb) { |
2f993cf0 ON |
6234 | if (event->rcu_pending) { |
6235 | cond_synchronize_rcu(event->rcu_batches); | |
6236 | event->rcu_pending = 0; | |
6237 | } | |
6238 | ||
b69cf536 PZ |
6239 | spin_lock_irqsave(&rb->event_lock, flags); |
6240 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
6241 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
6242 | } | |
6243 | ||
767ae086 AS |
6244 | /* |
6245 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
6246 | * before swizzling the event::rb pointer; if it's getting | |
6247 | * unmapped, its aux_mmap_count will be 0 and it won't | |
6248 | * restart. See the comment in __perf_pmu_output_stop(). | |
6249 | * | |
6250 | * Data will inevitably be lost when set_output is done in | |
6251 | * mid-air, but then again, whoever does it like this is | |
6252 | * not in for the data anyway. | |
6253 | */ | |
6254 | if (has_aux(event)) | |
6255 | perf_event_stop(event, 0); | |
6256 | ||
b69cf536 PZ |
6257 | rcu_assign_pointer(event->rb, rb); |
6258 | ||
6259 | if (old_rb) { | |
6260 | ring_buffer_put(old_rb); | |
6261 | /* | |
6262 | * Since we detached before setting the new rb, so that we | |
6263 | * could attach the new rb, we could have missed a wakeup. | |
6264 | * Provide it now. | |
6265 | */ | |
6266 | wake_up_all(&event->waitq); | |
6267 | } | |
10c6db11 PZ |
6268 | } |
6269 | ||
6270 | static void ring_buffer_wakeup(struct perf_event *event) | |
6271 | { | |
56de4e8f | 6272 | struct perf_buffer *rb; |
10c6db11 | 6273 | |
961c3912 JC |
6274 | if (event->parent) |
6275 | event = event->parent; | |
6276 | ||
10c6db11 PZ |
6277 | rcu_read_lock(); |
6278 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
6279 | if (rb) { |
6280 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
6281 | wake_up_all(&event->waitq); | |
6282 | } | |
10c6db11 PZ |
6283 | rcu_read_unlock(); |
6284 | } | |
6285 | ||
56de4e8f | 6286 | struct perf_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 6287 | { |
56de4e8f | 6288 | struct perf_buffer *rb; |
7b732a75 | 6289 | |
961c3912 JC |
6290 | if (event->parent) |
6291 | event = event->parent; | |
6292 | ||
ac9721f3 | 6293 | rcu_read_lock(); |
76369139 FW |
6294 | rb = rcu_dereference(event->rb); |
6295 | if (rb) { | |
fecb8ed2 | 6296 | if (!refcount_inc_not_zero(&rb->refcount)) |
76369139 | 6297 | rb = NULL; |
ac9721f3 PZ |
6298 | } |
6299 | rcu_read_unlock(); | |
6300 | ||
76369139 | 6301 | return rb; |
ac9721f3 PZ |
6302 | } |
6303 | ||
56de4e8f | 6304 | void ring_buffer_put(struct perf_buffer *rb) |
ac9721f3 | 6305 | { |
fecb8ed2 | 6306 | if (!refcount_dec_and_test(&rb->refcount)) |
ac9721f3 | 6307 | return; |
7b732a75 | 6308 | |
9bb5d40c | 6309 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 6310 | |
76369139 | 6311 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
6312 | } |
6313 | ||
6314 | static void perf_mmap_open(struct vm_area_struct *vma) | |
6315 | { | |
cdd6c482 | 6316 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 6317 | |
cdd6c482 | 6318 | atomic_inc(&event->mmap_count); |
9bb5d40c | 6319 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 6320 | |
45bfb2e5 PZ |
6321 | if (vma->vm_pgoff) |
6322 | atomic_inc(&event->rb->aux_mmap_count); | |
6323 | ||
1e0fb9ec | 6324 | if (event->pmu->event_mapped) |
bfe33492 | 6325 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
6326 | } |
6327 | ||
95ff4ca2 AS |
6328 | static void perf_pmu_output_stop(struct perf_event *event); |
6329 | ||
9bb5d40c PZ |
6330 | /* |
6331 | * A buffer can be mmap()ed multiple times; either directly through the same | |
6332 | * event, or through other events by use of perf_event_set_output(). | |
6333 | * | |
6334 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
6335 | * the buffer here, where we still have a VM context. This means we need | |
6336 | * to detach all events redirecting to us. | |
6337 | */ | |
7b732a75 PZ |
6338 | static void perf_mmap_close(struct vm_area_struct *vma) |
6339 | { | |
cdd6c482 | 6340 | struct perf_event *event = vma->vm_file->private_data; |
56de4e8f | 6341 | struct perf_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
6342 | struct user_struct *mmap_user = rb->mmap_user; |
6343 | int mmap_locked = rb->mmap_locked; | |
6344 | unsigned long size = perf_data_size(rb); | |
f91072ed | 6345 | bool detach_rest = false; |
789f90fc | 6346 | |
1e0fb9ec | 6347 | if (event->pmu->event_unmapped) |
bfe33492 | 6348 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 6349 | |
45bfb2e5 PZ |
6350 | /* |
6351 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
6352 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
6353 | * serialize with perf_mmap here. | |
6354 | */ | |
6355 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
6356 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
6357 | /* |
6358 | * Stop all AUX events that are writing to this buffer, | |
6359 | * so that we can free its AUX pages and corresponding PMU | |
6360 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
6361 | * they won't start any more (see perf_aux_output_begin()). | |
6362 | */ | |
6363 | perf_pmu_output_stop(event); | |
6364 | ||
6365 | /* now it's safe to free the pages */ | |
36b3db03 AS |
6366 | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); |
6367 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
45bfb2e5 | 6368 | |
95ff4ca2 | 6369 | /* this has to be the last one */ |
45bfb2e5 | 6370 | rb_free_aux(rb); |
ca3bb3d0 | 6371 | WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); |
95ff4ca2 | 6372 | |
45bfb2e5 PZ |
6373 | mutex_unlock(&event->mmap_mutex); |
6374 | } | |
6375 | ||
f91072ed JO |
6376 | if (atomic_dec_and_test(&rb->mmap_count)) |
6377 | detach_rest = true; | |
9bb5d40c PZ |
6378 | |
6379 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 6380 | goto out_put; |
9bb5d40c | 6381 | |
b69cf536 | 6382 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
6383 | mutex_unlock(&event->mmap_mutex); |
6384 | ||
6385 | /* If there's still other mmap()s of this buffer, we're done. */ | |
f91072ed | 6386 | if (!detach_rest) |
b69cf536 | 6387 | goto out_put; |
ac9721f3 | 6388 | |
9bb5d40c PZ |
6389 | /* |
6390 | * No other mmap()s, detach from all other events that might redirect | |
6391 | * into the now unreachable buffer. Somewhat complicated by the | |
6392 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
6393 | */ | |
6394 | again: | |
6395 | rcu_read_lock(); | |
6396 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
6397 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
6398 | /* | |
6399 | * This event is en-route to free_event() which will | |
6400 | * detach it and remove it from the list. | |
6401 | */ | |
6402 | continue; | |
6403 | } | |
6404 | rcu_read_unlock(); | |
789f90fc | 6405 | |
9bb5d40c PZ |
6406 | mutex_lock(&event->mmap_mutex); |
6407 | /* | |
6408 | * Check we didn't race with perf_event_set_output() which can | |
6409 | * swizzle the rb from under us while we were waiting to | |
6410 | * acquire mmap_mutex. | |
6411 | * | |
6412 | * If we find a different rb; ignore this event, a next | |
6413 | * iteration will no longer find it on the list. We have to | |
6414 | * still restart the iteration to make sure we're not now | |
6415 | * iterating the wrong list. | |
6416 | */ | |
b69cf536 PZ |
6417 | if (event->rb == rb) |
6418 | ring_buffer_attach(event, NULL); | |
6419 | ||
cdd6c482 | 6420 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 6421 | put_event(event); |
ac9721f3 | 6422 | |
9bb5d40c PZ |
6423 | /* |
6424 | * Restart the iteration; either we're on the wrong list or | |
6425 | * destroyed its integrity by doing a deletion. | |
6426 | */ | |
6427 | goto again; | |
7b732a75 | 6428 | } |
9bb5d40c PZ |
6429 | rcu_read_unlock(); |
6430 | ||
6431 | /* | |
6432 | * It could be there's still a few 0-ref events on the list; they'll | |
6433 | * get cleaned up by free_event() -- they'll also still have their | |
6434 | * ref on the rb and will free it whenever they are done with it. | |
6435 | * | |
6436 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
6437 | * undo the VM accounting. | |
6438 | */ | |
6439 | ||
d44248a4 SL |
6440 | atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked, |
6441 | &mmap_user->locked_vm); | |
70f8a3ca | 6442 | atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm); |
9bb5d40c PZ |
6443 | free_uid(mmap_user); |
6444 | ||
b69cf536 | 6445 | out_put: |
9bb5d40c | 6446 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
6447 | } |
6448 | ||
f0f37e2f | 6449 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 6450 | .open = perf_mmap_open, |
fca0c116 | 6451 | .close = perf_mmap_close, /* non mergeable */ |
43a21ea8 PZ |
6452 | .fault = perf_mmap_fault, |
6453 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
6454 | }; |
6455 | ||
6456 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
6457 | { | |
cdd6c482 | 6458 | struct perf_event *event = file->private_data; |
22a4f650 | 6459 | unsigned long user_locked, user_lock_limit; |
789f90fc | 6460 | struct user_struct *user = current_user(); |
56de4e8f | 6461 | struct perf_buffer *rb = NULL; |
22a4f650 | 6462 | unsigned long locked, lock_limit; |
7b732a75 PZ |
6463 | unsigned long vma_size; |
6464 | unsigned long nr_pages; | |
45bfb2e5 | 6465 | long user_extra = 0, extra = 0; |
d57e34fd | 6466 | int ret = 0, flags = 0; |
37d81828 | 6467 | |
c7920614 PZ |
6468 | /* |
6469 | * Don't allow mmap() of inherited per-task counters. This would | |
6470 | * create a performance issue due to all children writing to the | |
76369139 | 6471 | * same rb. |
c7920614 PZ |
6472 | */ |
6473 | if (event->cpu == -1 && event->attr.inherit) | |
6474 | return -EINVAL; | |
6475 | ||
43a21ea8 | 6476 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 6477 | return -EINVAL; |
7b732a75 | 6478 | |
da97e184 JFG |
6479 | ret = security_perf_event_read(event); |
6480 | if (ret) | |
6481 | return ret; | |
6482 | ||
7b732a75 | 6483 | vma_size = vma->vm_end - vma->vm_start; |
45bfb2e5 PZ |
6484 | |
6485 | if (vma->vm_pgoff == 0) { | |
6486 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
6487 | } else { | |
6488 | /* | |
6489 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
6490 | * mapped, all subsequent mappings should have the same size | |
6491 | * and offset. Must be above the normal perf buffer. | |
6492 | */ | |
6493 | u64 aux_offset, aux_size; | |
6494 | ||
6495 | if (!event->rb) | |
6496 | return -EINVAL; | |
6497 | ||
6498 | nr_pages = vma_size / PAGE_SIZE; | |
6499 | ||
6500 | mutex_lock(&event->mmap_mutex); | |
6501 | ret = -EINVAL; | |
6502 | ||
6503 | rb = event->rb; | |
6504 | if (!rb) | |
6505 | goto aux_unlock; | |
6506 | ||
6aa7de05 MR |
6507 | aux_offset = READ_ONCE(rb->user_page->aux_offset); |
6508 | aux_size = READ_ONCE(rb->user_page->aux_size); | |
45bfb2e5 PZ |
6509 | |
6510 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
6511 | goto aux_unlock; | |
6512 | ||
6513 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
6514 | goto aux_unlock; | |
6515 | ||
6516 | /* already mapped with a different offset */ | |
6517 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
6518 | goto aux_unlock; | |
6519 | ||
6520 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
6521 | goto aux_unlock; | |
6522 | ||
6523 | /* already mapped with a different size */ | |
6524 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
6525 | goto aux_unlock; | |
6526 | ||
6527 | if (!is_power_of_2(nr_pages)) | |
6528 | goto aux_unlock; | |
6529 | ||
6530 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
6531 | goto aux_unlock; | |
6532 | ||
6533 | if (rb_has_aux(rb)) { | |
6534 | atomic_inc(&rb->aux_mmap_count); | |
6535 | ret = 0; | |
6536 | goto unlock; | |
6537 | } | |
6538 | ||
6539 | atomic_set(&rb->aux_mmap_count, 1); | |
6540 | user_extra = nr_pages; | |
6541 | ||
6542 | goto accounting; | |
6543 | } | |
7b732a75 | 6544 | |
7730d865 | 6545 | /* |
76369139 | 6546 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
6547 | * can do bitmasks instead of modulo. |
6548 | */ | |
2ed11312 | 6549 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
6550 | return -EINVAL; |
6551 | ||
7b732a75 | 6552 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
6553 | return -EINVAL; |
6554 | ||
cdd6c482 | 6555 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 6556 | again: |
cdd6c482 | 6557 | mutex_lock(&event->mmap_mutex); |
76369139 | 6558 | if (event->rb) { |
60490e79 | 6559 | if (data_page_nr(event->rb) != nr_pages) { |
ebb3c4c4 | 6560 | ret = -EINVAL; |
9bb5d40c PZ |
6561 | goto unlock; |
6562 | } | |
6563 | ||
6564 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
6565 | /* | |
68e3c698 PZ |
6566 | * Raced against perf_mmap_close(); remove the |
6567 | * event and try again. | |
9bb5d40c | 6568 | */ |
68e3c698 | 6569 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
6570 | mutex_unlock(&event->mmap_mutex); |
6571 | goto again; | |
6572 | } | |
6573 | ||
ebb3c4c4 PZ |
6574 | goto unlock; |
6575 | } | |
6576 | ||
789f90fc | 6577 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
6578 | |
6579 | accounting: | |
cdd6c482 | 6580 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
6581 | |
6582 | /* | |
6583 | * Increase the limit linearly with more CPUs: | |
6584 | */ | |
6585 | user_lock_limit *= num_online_cpus(); | |
6586 | ||
00346155 SL |
6587 | user_locked = atomic_long_read(&user->locked_vm); |
6588 | ||
6589 | /* | |
6590 | * sysctl_perf_event_mlock may have changed, so that | |
6591 | * user->locked_vm > user_lock_limit | |
6592 | */ | |
6593 | if (user_locked > user_lock_limit) | |
6594 | user_locked = user_lock_limit; | |
6595 | user_locked += user_extra; | |
c5078f78 | 6596 | |
c4b75479 | 6597 | if (user_locked > user_lock_limit) { |
d44248a4 SL |
6598 | /* |
6599 | * charge locked_vm until it hits user_lock_limit; | |
6600 | * charge the rest from pinned_vm | |
6601 | */ | |
789f90fc | 6602 | extra = user_locked - user_lock_limit; |
d44248a4 SL |
6603 | user_extra -= extra; |
6604 | } | |
7b732a75 | 6605 | |
78d7d407 | 6606 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 6607 | lock_limit >>= PAGE_SHIFT; |
70f8a3ca | 6608 | locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; |
7b732a75 | 6609 | |
da97e184 | 6610 | if ((locked > lock_limit) && perf_is_paranoid() && |
459ec28a | 6611 | !capable(CAP_IPC_LOCK)) { |
ebb3c4c4 PZ |
6612 | ret = -EPERM; |
6613 | goto unlock; | |
6614 | } | |
7b732a75 | 6615 | |
45bfb2e5 | 6616 | WARN_ON(!rb && event->rb); |
906010b2 | 6617 | |
d57e34fd | 6618 | if (vma->vm_flags & VM_WRITE) |
76369139 | 6619 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 6620 | |
76369139 | 6621 | if (!rb) { |
45bfb2e5 PZ |
6622 | rb = rb_alloc(nr_pages, |
6623 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
6624 | event->cpu, flags); | |
26cb63ad | 6625 | |
45bfb2e5 PZ |
6626 | if (!rb) { |
6627 | ret = -ENOMEM; | |
6628 | goto unlock; | |
6629 | } | |
43a21ea8 | 6630 | |
45bfb2e5 PZ |
6631 | atomic_set(&rb->mmap_count, 1); |
6632 | rb->mmap_user = get_current_user(); | |
6633 | rb->mmap_locked = extra; | |
26cb63ad | 6634 | |
45bfb2e5 | 6635 | ring_buffer_attach(event, rb); |
ac9721f3 | 6636 | |
f7925653 | 6637 | perf_event_update_time(event); |
45bfb2e5 PZ |
6638 | perf_event_init_userpage(event); |
6639 | perf_event_update_userpage(event); | |
6640 | } else { | |
1a594131 AS |
6641 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
6642 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
6643 | if (!ret) |
6644 | rb->aux_mmap_locked = extra; | |
6645 | } | |
9a0f05cb | 6646 | |
ebb3c4c4 | 6647 | unlock: |
45bfb2e5 PZ |
6648 | if (!ret) { |
6649 | atomic_long_add(user_extra, &user->locked_vm); | |
70f8a3ca | 6650 | atomic64_add(extra, &vma->vm_mm->pinned_vm); |
45bfb2e5 | 6651 | |
ac9721f3 | 6652 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
6653 | } else if (rb) { |
6654 | atomic_dec(&rb->mmap_count); | |
6655 | } | |
6656 | aux_unlock: | |
cdd6c482 | 6657 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 6658 | |
9bb5d40c PZ |
6659 | /* |
6660 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
6661 | * vma. | |
6662 | */ | |
1c71222e | 6663 | vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); |
37d81828 | 6664 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 6665 | |
1e0fb9ec | 6666 | if (event->pmu->event_mapped) |
bfe33492 | 6667 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 6668 | |
7b732a75 | 6669 | return ret; |
37d81828 PM |
6670 | } |
6671 | ||
3c446b3d PZ |
6672 | static int perf_fasync(int fd, struct file *filp, int on) |
6673 | { | |
496ad9aa | 6674 | struct inode *inode = file_inode(filp); |
cdd6c482 | 6675 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
6676 | int retval; |
6677 | ||
5955102c | 6678 | inode_lock(inode); |
cdd6c482 | 6679 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 6680 | inode_unlock(inode); |
3c446b3d PZ |
6681 | |
6682 | if (retval < 0) | |
6683 | return retval; | |
6684 | ||
6685 | return 0; | |
6686 | } | |
6687 | ||
0793a61d | 6688 | static const struct file_operations perf_fops = { |
3326c1ce | 6689 | .llseek = no_llseek, |
0793a61d TG |
6690 | .release = perf_release, |
6691 | .read = perf_read, | |
6692 | .poll = perf_poll, | |
d859e29f | 6693 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 6694 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 6695 | .mmap = perf_mmap, |
3c446b3d | 6696 | .fasync = perf_fasync, |
0793a61d TG |
6697 | }; |
6698 | ||
925d519a | 6699 | /* |
cdd6c482 | 6700 | * Perf event wakeup |
925d519a PZ |
6701 | * |
6702 | * If there's data, ensure we set the poll() state and publish everything | |
6703 | * to user-space before waking everybody up. | |
6704 | */ | |
6705 | ||
fed66e2c PZ |
6706 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
6707 | { | |
6708 | /* only the parent has fasync state */ | |
6709 | if (event->parent) | |
6710 | event = event->parent; | |
6711 | return &event->fasync; | |
6712 | } | |
6713 | ||
cdd6c482 | 6714 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 6715 | { |
10c6db11 | 6716 | ring_buffer_wakeup(event); |
4c9e2542 | 6717 | |
cdd6c482 | 6718 | if (event->pending_kill) { |
fed66e2c | 6719 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 6720 | event->pending_kill = 0; |
4c9e2542 | 6721 | } |
925d519a PZ |
6722 | } |
6723 | ||
97ba62b2 ME |
6724 | static void perf_sigtrap(struct perf_event *event) |
6725 | { | |
97ba62b2 ME |
6726 | /* |
6727 | * We'd expect this to only occur if the irq_work is delayed and either | |
6728 | * ctx->task or current has changed in the meantime. This can be the | |
6729 | * case on architectures that do not implement arch_irq_work_raise(). | |
6730 | */ | |
6731 | if (WARN_ON_ONCE(event->ctx->task != current)) | |
6732 | return; | |
6733 | ||
6734 | /* | |
ca6c2132 PZ |
6735 | * Both perf_pending_task() and perf_pending_irq() can race with the |
6736 | * task exiting. | |
97ba62b2 ME |
6737 | */ |
6738 | if (current->flags & PF_EXITING) | |
6739 | return; | |
6740 | ||
78ed93d7 | 6741 | send_sig_perf((void __user *)event->pending_addr, |
0d6d062c | 6742 | event->orig_type, event->attr.sig_data); |
97ba62b2 ME |
6743 | } |
6744 | ||
ca6c2132 PZ |
6745 | /* |
6746 | * Deliver the pending work in-event-context or follow the context. | |
6747 | */ | |
6748 | static void __perf_pending_irq(struct perf_event *event) | |
1d54ad94 | 6749 | { |
ca6c2132 | 6750 | int cpu = READ_ONCE(event->oncpu); |
1d54ad94 | 6751 | |
ca6c2132 PZ |
6752 | /* |
6753 | * If the event isn't running; we done. event_sched_out() will have | |
6754 | * taken care of things. | |
6755 | */ | |
1d54ad94 PZ |
6756 | if (cpu < 0) |
6757 | return; | |
6758 | ||
ca6c2132 PZ |
6759 | /* |
6760 | * Yay, we hit home and are in the context of the event. | |
6761 | */ | |
1d54ad94 | 6762 | if (cpu == smp_processor_id()) { |
ca6c2132 PZ |
6763 | if (event->pending_sigtrap) { |
6764 | event->pending_sigtrap = 0; | |
97ba62b2 | 6765 | perf_sigtrap(event); |
ca6c2132 PZ |
6766 | local_dec(&event->ctx->nr_pending); |
6767 | } | |
6768 | if (event->pending_disable) { | |
6769 | event->pending_disable = 0; | |
6770 | perf_event_disable_local(event); | |
97ba62b2 | 6771 | } |
1d54ad94 PZ |
6772 | return; |
6773 | } | |
6774 | ||
6775 | /* | |
6776 | * CPU-A CPU-B | |
6777 | * | |
6778 | * perf_event_disable_inatomic() | |
6779 | * @pending_disable = CPU-A; | |
6780 | * irq_work_queue(); | |
6781 | * | |
6782 | * sched-out | |
6783 | * @pending_disable = -1; | |
6784 | * | |
6785 | * sched-in | |
6786 | * perf_event_disable_inatomic() | |
6787 | * @pending_disable = CPU-B; | |
6788 | * irq_work_queue(); // FAILS | |
6789 | * | |
6790 | * irq_work_run() | |
ca6c2132 | 6791 | * perf_pending_irq() |
1d54ad94 PZ |
6792 | * |
6793 | * But the event runs on CPU-B and wants disabling there. | |
6794 | */ | |
ca6c2132 | 6795 | irq_work_queue_on(&event->pending_irq, cpu); |
1d54ad94 PZ |
6796 | } |
6797 | ||
ca6c2132 | 6798 | static void perf_pending_irq(struct irq_work *entry) |
79f14641 | 6799 | { |
ca6c2132 | 6800 | struct perf_event *event = container_of(entry, struct perf_event, pending_irq); |
d525211f PZ |
6801 | int rctx; |
6802 | ||
d525211f PZ |
6803 | /* |
6804 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
6805 | * and we won't recurse 'further'. | |
6806 | */ | |
ca6c2132 | 6807 | rctx = perf_swevent_get_recursion_context(); |
79f14641 | 6808 | |
ca6c2132 PZ |
6809 | /* |
6810 | * The wakeup isn't bound to the context of the event -- it can happen | |
6811 | * irrespective of where the event is. | |
6812 | */ | |
cdd6c482 IM |
6813 | if (event->pending_wakeup) { |
6814 | event->pending_wakeup = 0; | |
6815 | perf_event_wakeup(event); | |
79f14641 | 6816 | } |
d525211f | 6817 | |
ca6c2132 PZ |
6818 | __perf_pending_irq(event); |
6819 | ||
d525211f PZ |
6820 | if (rctx >= 0) |
6821 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
6822 | } |
6823 | ||
ca6c2132 PZ |
6824 | static void perf_pending_task(struct callback_head *head) |
6825 | { | |
6826 | struct perf_event *event = container_of(head, struct perf_event, pending_task); | |
6827 | int rctx; | |
6828 | ||
6829 | /* | |
6830 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
6831 | * and we won't recurse 'further'. | |
6832 | */ | |
6833 | preempt_disable_notrace(); | |
6834 | rctx = perf_swevent_get_recursion_context(); | |
6835 | ||
6836 | if (event->pending_work) { | |
6837 | event->pending_work = 0; | |
6838 | perf_sigtrap(event); | |
6839 | local_dec(&event->ctx->nr_pending); | |
6840 | } | |
6841 | ||
6842 | if (rctx >= 0) | |
6843 | perf_swevent_put_recursion_context(rctx); | |
6844 | preempt_enable_notrace(); | |
517e6a30 PZ |
6845 | |
6846 | put_event(event); | |
ca6c2132 PZ |
6847 | } |
6848 | ||
2aef6f30 | 6849 | #ifdef CONFIG_GUEST_PERF_EVENTS |
ff083a2d | 6850 | struct perf_guest_info_callbacks __rcu *perf_guest_cbs; |
39447b38 | 6851 | |
87b940a0 SC |
6852 | DEFINE_STATIC_CALL_RET0(__perf_guest_state, *perf_guest_cbs->state); |
6853 | DEFINE_STATIC_CALL_RET0(__perf_guest_get_ip, *perf_guest_cbs->get_ip); | |
6854 | DEFINE_STATIC_CALL_RET0(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); | |
39447b38 | 6855 | |
2934e3d0 | 6856 | void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
39447b38 | 6857 | { |
ff083a2d | 6858 | if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs))) |
2934e3d0 | 6859 | return; |
ff083a2d SC |
6860 | |
6861 | rcu_assign_pointer(perf_guest_cbs, cbs); | |
87b940a0 SC |
6862 | static_call_update(__perf_guest_state, cbs->state); |
6863 | static_call_update(__perf_guest_get_ip, cbs->get_ip); | |
6864 | ||
6865 | /* Implementing ->handle_intel_pt_intr is optional. */ | |
6866 | if (cbs->handle_intel_pt_intr) | |
6867 | static_call_update(__perf_guest_handle_intel_pt_intr, | |
6868 | cbs->handle_intel_pt_intr); | |
39447b38 ZY |
6869 | } |
6870 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
6871 | ||
2934e3d0 | 6872 | void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
39447b38 | 6873 | { |
ff083a2d | 6874 | if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs) != cbs)) |
2934e3d0 | 6875 | return; |
ff083a2d SC |
6876 | |
6877 | rcu_assign_pointer(perf_guest_cbs, NULL); | |
87b940a0 SC |
6878 | static_call_update(__perf_guest_state, (void *)&__static_call_return0); |
6879 | static_call_update(__perf_guest_get_ip, (void *)&__static_call_return0); | |
6880 | static_call_update(__perf_guest_handle_intel_pt_intr, | |
6881 | (void *)&__static_call_return0); | |
ff083a2d | 6882 | synchronize_rcu(); |
39447b38 ZY |
6883 | } |
6884 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
2aef6f30 | 6885 | #endif |
39447b38 | 6886 | |
4018994f JO |
6887 | static void |
6888 | perf_output_sample_regs(struct perf_output_handle *handle, | |
6889 | struct pt_regs *regs, u64 mask) | |
6890 | { | |
6891 | int bit; | |
29dd3288 | 6892 | DECLARE_BITMAP(_mask, 64); |
4018994f | 6893 | |
29dd3288 MS |
6894 | bitmap_from_u64(_mask, mask); |
6895 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
6896 | u64 val; |
6897 | ||
6898 | val = perf_reg_value(regs, bit); | |
6899 | perf_output_put(handle, val); | |
6900 | } | |
6901 | } | |
6902 | ||
60e2364e | 6903 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
76a4efa8 | 6904 | struct pt_regs *regs) |
4018994f | 6905 | { |
88a7c26a AL |
6906 | if (user_mode(regs)) { |
6907 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 6908 | regs_user->regs = regs; |
085ebfe9 | 6909 | } else if (!(current->flags & PF_KTHREAD)) { |
76a4efa8 | 6910 | perf_get_regs_user(regs_user, regs); |
2565711f PZ |
6911 | } else { |
6912 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
6913 | regs_user->regs = NULL; | |
4018994f JO |
6914 | } |
6915 | } | |
6916 | ||
60e2364e SE |
6917 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
6918 | struct pt_regs *regs) | |
6919 | { | |
6920 | regs_intr->regs = regs; | |
6921 | regs_intr->abi = perf_reg_abi(current); | |
6922 | } | |
6923 | ||
6924 | ||
c5ebcedb JO |
6925 | /* |
6926 | * Get remaining task size from user stack pointer. | |
6927 | * | |
6928 | * It'd be better to take stack vma map and limit this more | |
9f014e3a | 6929 | * precisely, but there's no way to get it safely under interrupt, |
c5ebcedb JO |
6930 | * so using TASK_SIZE as limit. |
6931 | */ | |
6932 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
6933 | { | |
6934 | unsigned long addr = perf_user_stack_pointer(regs); | |
6935 | ||
6936 | if (!addr || addr >= TASK_SIZE) | |
6937 | return 0; | |
6938 | ||
6939 | return TASK_SIZE - addr; | |
6940 | } | |
6941 | ||
6942 | static u16 | |
6943 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
6944 | struct pt_regs *regs) | |
6945 | { | |
6946 | u64 task_size; | |
6947 | ||
6948 | /* No regs, no stack pointer, no dump. */ | |
6949 | if (!regs) | |
6950 | return 0; | |
6951 | ||
6952 | /* | |
6953 | * Check if we fit in with the requested stack size into the: | |
6954 | * - TASK_SIZE | |
6955 | * If we don't, we limit the size to the TASK_SIZE. | |
6956 | * | |
6957 | * - remaining sample size | |
6958 | * If we don't, we customize the stack size to | |
6959 | * fit in to the remaining sample size. | |
6960 | */ | |
6961 | ||
6962 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
6963 | stack_size = min(stack_size, (u16) task_size); | |
6964 | ||
6965 | /* Current header size plus static size and dynamic size. */ | |
6966 | header_size += 2 * sizeof(u64); | |
6967 | ||
6968 | /* Do we fit in with the current stack dump size? */ | |
6969 | if ((u16) (header_size + stack_size) < header_size) { | |
6970 | /* | |
6971 | * If we overflow the maximum size for the sample, | |
6972 | * we customize the stack dump size to fit in. | |
6973 | */ | |
6974 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
6975 | stack_size = round_up(stack_size, sizeof(u64)); | |
6976 | } | |
6977 | ||
6978 | return stack_size; | |
6979 | } | |
6980 | ||
6981 | static void | |
6982 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
6983 | struct pt_regs *regs) | |
6984 | { | |
6985 | /* Case of a kernel thread, nothing to dump */ | |
6986 | if (!regs) { | |
6987 | u64 size = 0; | |
6988 | perf_output_put(handle, size); | |
6989 | } else { | |
6990 | unsigned long sp; | |
6991 | unsigned int rem; | |
6992 | u64 dyn_size; | |
6993 | ||
6994 | /* | |
6995 | * We dump: | |
6996 | * static size | |
6997 | * - the size requested by user or the best one we can fit | |
6998 | * in to the sample max size | |
6999 | * data | |
7000 | * - user stack dump data | |
7001 | * dynamic size | |
7002 | * - the actual dumped size | |
7003 | */ | |
7004 | ||
7005 | /* Static size. */ | |
7006 | perf_output_put(handle, dump_size); | |
7007 | ||
7008 | /* Data. */ | |
7009 | sp = perf_user_stack_pointer(regs); | |
7010 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
7011 | dyn_size = dump_size - rem; | |
7012 | ||
7013 | perf_output_skip(handle, rem); | |
7014 | ||
7015 | /* Dynamic size. */ | |
7016 | perf_output_put(handle, dyn_size); | |
7017 | } | |
7018 | } | |
7019 | ||
a4faf00d AS |
7020 | static unsigned long perf_prepare_sample_aux(struct perf_event *event, |
7021 | struct perf_sample_data *data, | |
7022 | size_t size) | |
7023 | { | |
7024 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 7025 | struct perf_buffer *rb; |
a4faf00d AS |
7026 | |
7027 | data->aux_size = 0; | |
7028 | ||
7029 | if (!sampler) | |
7030 | goto out; | |
7031 | ||
7032 | if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE)) | |
7033 | goto out; | |
7034 | ||
7035 | if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id())) | |
7036 | goto out; | |
7037 | ||
961c3912 | 7038 | rb = ring_buffer_get(sampler); |
a4faf00d AS |
7039 | if (!rb) |
7040 | goto out; | |
7041 | ||
7042 | /* | |
7043 | * If this is an NMI hit inside sampling code, don't take | |
7044 | * the sample. See also perf_aux_sample_output(). | |
7045 | */ | |
7046 | if (READ_ONCE(rb->aux_in_sampling)) { | |
7047 | data->aux_size = 0; | |
7048 | } else { | |
7049 | size = min_t(size_t, size, perf_aux_size(rb)); | |
7050 | data->aux_size = ALIGN(size, sizeof(u64)); | |
7051 | } | |
7052 | ring_buffer_put(rb); | |
7053 | ||
7054 | out: | |
7055 | return data->aux_size; | |
7056 | } | |
7057 | ||
32961aec HX |
7058 | static long perf_pmu_snapshot_aux(struct perf_buffer *rb, |
7059 | struct perf_event *event, | |
7060 | struct perf_output_handle *handle, | |
7061 | unsigned long size) | |
a4faf00d AS |
7062 | { |
7063 | unsigned long flags; | |
7064 | long ret; | |
7065 | ||
7066 | /* | |
7067 | * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler | |
7068 | * paths. If we start calling them in NMI context, they may race with | |
7069 | * the IRQ ones, that is, for example, re-starting an event that's just | |
7070 | * been stopped, which is why we're using a separate callback that | |
7071 | * doesn't change the event state. | |
7072 | * | |
7073 | * IRQs need to be disabled to prevent IPIs from racing with us. | |
7074 | */ | |
7075 | local_irq_save(flags); | |
7076 | /* | |
7077 | * Guard against NMI hits inside the critical section; | |
7078 | * see also perf_prepare_sample_aux(). | |
7079 | */ | |
7080 | WRITE_ONCE(rb->aux_in_sampling, 1); | |
7081 | barrier(); | |
7082 | ||
7083 | ret = event->pmu->snapshot_aux(event, handle, size); | |
7084 | ||
7085 | barrier(); | |
7086 | WRITE_ONCE(rb->aux_in_sampling, 0); | |
7087 | local_irq_restore(flags); | |
7088 | ||
7089 | return ret; | |
7090 | } | |
7091 | ||
7092 | static void perf_aux_sample_output(struct perf_event *event, | |
7093 | struct perf_output_handle *handle, | |
7094 | struct perf_sample_data *data) | |
7095 | { | |
7096 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 7097 | struct perf_buffer *rb; |
a4faf00d | 7098 | unsigned long pad; |
a4faf00d AS |
7099 | long size; |
7100 | ||
7101 | if (WARN_ON_ONCE(!sampler || !data->aux_size)) | |
7102 | return; | |
7103 | ||
961c3912 | 7104 | rb = ring_buffer_get(sampler); |
a4faf00d AS |
7105 | if (!rb) |
7106 | return; | |
7107 | ||
7108 | size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size); | |
7109 | ||
7110 | /* | |
7111 | * An error here means that perf_output_copy() failed (returned a | |
7112 | * non-zero surplus that it didn't copy), which in its current | |
7113 | * enlightened implementation is not possible. If that changes, we'd | |
7114 | * like to know. | |
7115 | */ | |
7116 | if (WARN_ON_ONCE(size < 0)) | |
7117 | goto out_put; | |
7118 | ||
7119 | /* | |
7120 | * The pad comes from ALIGN()ing data->aux_size up to u64 in | |
7121 | * perf_prepare_sample_aux(), so should not be more than that. | |
7122 | */ | |
7123 | pad = data->aux_size - size; | |
7124 | if (WARN_ON_ONCE(pad >= sizeof(u64))) | |
7125 | pad = 8; | |
7126 | ||
7127 | if (pad) { | |
7128 | u64 zero = 0; | |
7129 | perf_output_copy(handle, &zero, pad); | |
7130 | } | |
7131 | ||
7132 | out_put: | |
7133 | ring_buffer_put(rb); | |
7134 | } | |
7135 | ||
bb447c27 NK |
7136 | /* |
7137 | * A set of common sample data types saved even for non-sample records | |
7138 | * when event->attr.sample_id_all is set. | |
7139 | */ | |
7140 | #define PERF_SAMPLE_ID_ALL (PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ | |
7141 | PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \ | |
7142 | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER) | |
7143 | ||
a7c8d0da | 7144 | static void __perf_event_header__init_id(struct perf_sample_data *data, |
3aac580d KL |
7145 | struct perf_event *event, |
7146 | u64 sample_type) | |
6844c09d | 7147 | { |
3aac580d | 7148 | data->type = event->attr.sample_type; |
bb447c27 | 7149 | data->sample_flags |= data->type & PERF_SAMPLE_ID_ALL; |
6844c09d ACM |
7150 | |
7151 | if (sample_type & PERF_SAMPLE_TID) { | |
7152 | /* namespace issues */ | |
7153 | data->tid_entry.pid = perf_event_pid(event, current); | |
7154 | data->tid_entry.tid = perf_event_tid(event, current); | |
7155 | } | |
7156 | ||
7157 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 7158 | data->time = perf_event_clock(event); |
6844c09d | 7159 | |
ff3d527c | 7160 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
7161 | data->id = primary_event_id(event); |
7162 | ||
7163 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7164 | data->stream_id = event->id; | |
7165 | ||
7166 | if (sample_type & PERF_SAMPLE_CPU) { | |
7167 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
7168 | data->cpu_entry.reserved = 0; | |
7169 | } | |
7170 | } | |
7171 | ||
76369139 FW |
7172 | void perf_event_header__init_id(struct perf_event_header *header, |
7173 | struct perf_sample_data *data, | |
7174 | struct perf_event *event) | |
c980d109 | 7175 | { |
a7c8d0da NK |
7176 | if (event->attr.sample_id_all) { |
7177 | header->size += event->id_header_size; | |
7178 | __perf_event_header__init_id(data, event, event->attr.sample_type); | |
7179 | } | |
c980d109 ACM |
7180 | } |
7181 | ||
7182 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
7183 | struct perf_sample_data *data) | |
7184 | { | |
7185 | u64 sample_type = data->type; | |
7186 | ||
7187 | if (sample_type & PERF_SAMPLE_TID) | |
7188 | perf_output_put(handle, data->tid_entry); | |
7189 | ||
7190 | if (sample_type & PERF_SAMPLE_TIME) | |
7191 | perf_output_put(handle, data->time); | |
7192 | ||
7193 | if (sample_type & PERF_SAMPLE_ID) | |
7194 | perf_output_put(handle, data->id); | |
7195 | ||
7196 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7197 | perf_output_put(handle, data->stream_id); | |
7198 | ||
7199 | if (sample_type & PERF_SAMPLE_CPU) | |
7200 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
7201 | |
7202 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
7203 | perf_output_put(handle, data->id); | |
c980d109 ACM |
7204 | } |
7205 | ||
76369139 FW |
7206 | void perf_event__output_id_sample(struct perf_event *event, |
7207 | struct perf_output_handle *handle, | |
7208 | struct perf_sample_data *sample) | |
c980d109 ACM |
7209 | { |
7210 | if (event->attr.sample_id_all) | |
7211 | __perf_event__output_id_sample(handle, sample); | |
7212 | } | |
7213 | ||
3dab77fb | 7214 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
7215 | struct perf_event *event, |
7216 | u64 enabled, u64 running) | |
3dab77fb | 7217 | { |
cdd6c482 | 7218 | u64 read_format = event->attr.read_format; |
119a784c | 7219 | u64 values[5]; |
3dab77fb PZ |
7220 | int n = 0; |
7221 | ||
b5e58793 | 7222 | values[n++] = perf_event_count(event); |
3dab77fb | 7223 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 7224 | values[n++] = enabled + |
cdd6c482 | 7225 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
7226 | } |
7227 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 7228 | values[n++] = running + |
cdd6c482 | 7229 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
7230 | } |
7231 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 7232 | values[n++] = primary_event_id(event); |
119a784c NK |
7233 | if (read_format & PERF_FORMAT_LOST) |
7234 | values[n++] = atomic64_read(&event->lost_samples); | |
3dab77fb | 7235 | |
76369139 | 7236 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
7237 | } |
7238 | ||
3dab77fb | 7239 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
7240 | struct perf_event *event, |
7241 | u64 enabled, u64 running) | |
3dab77fb | 7242 | { |
cdd6c482 IM |
7243 | struct perf_event *leader = event->group_leader, *sub; |
7244 | u64 read_format = event->attr.read_format; | |
6b959ba2 | 7245 | unsigned long flags; |
119a784c | 7246 | u64 values[6]; |
3dab77fb PZ |
7247 | int n = 0; |
7248 | ||
6b959ba2 YJ |
7249 | /* |
7250 | * Disabling interrupts avoids all counter scheduling | |
7251 | * (context switches, timer based rotation and IPIs). | |
7252 | */ | |
7253 | local_irq_save(flags); | |
7254 | ||
3dab77fb PZ |
7255 | values[n++] = 1 + leader->nr_siblings; |
7256 | ||
7257 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 7258 | values[n++] = enabled; |
3dab77fb PZ |
7259 | |
7260 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 7261 | values[n++] = running; |
3dab77fb | 7262 | |
9e5b127d PZ |
7263 | if ((leader != event) && |
7264 | (leader->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
7265 | leader->pmu->read(leader); |
7266 | ||
b5e58793 | 7267 | values[n++] = perf_event_count(leader); |
3dab77fb | 7268 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 7269 | values[n++] = primary_event_id(leader); |
119a784c NK |
7270 | if (read_format & PERF_FORMAT_LOST) |
7271 | values[n++] = atomic64_read(&leader->lost_samples); | |
3dab77fb | 7272 | |
76369139 | 7273 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 7274 | |
edb39592 | 7275 | for_each_sibling_event(sub, leader) { |
3dab77fb PZ |
7276 | n = 0; |
7277 | ||
6f5ab001 JO |
7278 | if ((sub != event) && |
7279 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
7280 | sub->pmu->read(sub); |
7281 | ||
b5e58793 | 7282 | values[n++] = perf_event_count(sub); |
3dab77fb | 7283 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 7284 | values[n++] = primary_event_id(sub); |
119a784c NK |
7285 | if (read_format & PERF_FORMAT_LOST) |
7286 | values[n++] = atomic64_read(&sub->lost_samples); | |
3dab77fb | 7287 | |
76369139 | 7288 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 7289 | } |
6b959ba2 YJ |
7290 | |
7291 | local_irq_restore(flags); | |
3dab77fb PZ |
7292 | } |
7293 | ||
eed01528 SE |
7294 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
7295 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
7296 | ||
ba5213ae PZ |
7297 | /* |
7298 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
7299 | * | |
7300 | * The problem is that its both hard and excessively expensive to iterate the | |
7301 | * child list, not to mention that its impossible to IPI the children running | |
7302 | * on another CPU, from interrupt/NMI context. | |
7303 | */ | |
3dab77fb | 7304 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 7305 | struct perf_event *event) |
3dab77fb | 7306 | { |
e3f3541c | 7307 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
7308 | u64 read_format = event->attr.read_format; |
7309 | ||
7310 | /* | |
7311 | * compute total_time_enabled, total_time_running | |
7312 | * based on snapshot values taken when the event | |
7313 | * was last scheduled in. | |
7314 | * | |
7315 | * we cannot simply called update_context_time() | |
7316 | * because of locking issue as we are called in | |
7317 | * NMI context | |
7318 | */ | |
c4794295 | 7319 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 7320 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 7321 | |
cdd6c482 | 7322 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 7323 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 7324 | else |
eed01528 | 7325 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
7326 | } |
7327 | ||
5622f295 MM |
7328 | void perf_output_sample(struct perf_output_handle *handle, |
7329 | struct perf_event_header *header, | |
7330 | struct perf_sample_data *data, | |
cdd6c482 | 7331 | struct perf_event *event) |
5622f295 MM |
7332 | { |
7333 | u64 sample_type = data->type; | |
7334 | ||
7335 | perf_output_put(handle, *header); | |
7336 | ||
ff3d527c AH |
7337 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
7338 | perf_output_put(handle, data->id); | |
7339 | ||
5622f295 MM |
7340 | if (sample_type & PERF_SAMPLE_IP) |
7341 | perf_output_put(handle, data->ip); | |
7342 | ||
7343 | if (sample_type & PERF_SAMPLE_TID) | |
7344 | perf_output_put(handle, data->tid_entry); | |
7345 | ||
7346 | if (sample_type & PERF_SAMPLE_TIME) | |
7347 | perf_output_put(handle, data->time); | |
7348 | ||
7349 | if (sample_type & PERF_SAMPLE_ADDR) | |
7350 | perf_output_put(handle, data->addr); | |
7351 | ||
7352 | if (sample_type & PERF_SAMPLE_ID) | |
7353 | perf_output_put(handle, data->id); | |
7354 | ||
7355 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7356 | perf_output_put(handle, data->stream_id); | |
7357 | ||
7358 | if (sample_type & PERF_SAMPLE_CPU) | |
7359 | perf_output_put(handle, data->cpu_entry); | |
7360 | ||
7361 | if (sample_type & PERF_SAMPLE_PERIOD) | |
7362 | perf_output_put(handle, data->period); | |
7363 | ||
7364 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 7365 | perf_output_read(handle, event); |
5622f295 MM |
7366 | |
7367 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
99e818cc | 7368 | int size = 1; |
5622f295 | 7369 | |
99e818cc JO |
7370 | size += data->callchain->nr; |
7371 | size *= sizeof(u64); | |
7372 | __output_copy(handle, data->callchain, size); | |
5622f295 MM |
7373 | } |
7374 | ||
7375 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
7376 | struct perf_raw_record *raw = data->raw; |
7377 | ||
7378 | if (raw) { | |
7379 | struct perf_raw_frag *frag = &raw->frag; | |
7380 | ||
7381 | perf_output_put(handle, raw->size); | |
7382 | do { | |
7383 | if (frag->copy) { | |
7384 | __output_custom(handle, frag->copy, | |
7385 | frag->data, frag->size); | |
7386 | } else { | |
7387 | __output_copy(handle, frag->data, | |
7388 | frag->size); | |
7389 | } | |
7390 | if (perf_raw_frag_last(frag)) | |
7391 | break; | |
7392 | frag = frag->next; | |
7393 | } while (1); | |
7394 | if (frag->pad) | |
7395 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
7396 | } else { |
7397 | struct { | |
7398 | u32 size; | |
7399 | u32 data; | |
7400 | } raw = { | |
7401 | .size = sizeof(u32), | |
7402 | .data = 0, | |
7403 | }; | |
7404 | perf_output_put(handle, raw); | |
7405 | } | |
7406 | } | |
a7ac67ea | 7407 | |
bce38cd5 | 7408 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
eb55b455 | 7409 | if (data->br_stack) { |
bce38cd5 SE |
7410 | size_t size; |
7411 | ||
7412 | size = data->br_stack->nr | |
7413 | * sizeof(struct perf_branch_entry); | |
7414 | ||
7415 | perf_output_put(handle, data->br_stack->nr); | |
03b02db9 | 7416 | if (branch_sample_hw_index(event)) |
bbfd5e4f | 7417 | perf_output_put(handle, data->br_stack->hw_idx); |
bce38cd5 | 7418 | perf_output_copy(handle, data->br_stack->entries, size); |
571d91dc KL |
7419 | /* |
7420 | * Add the extension space which is appended | |
7421 | * right after the struct perf_branch_stack. | |
7422 | */ | |
7423 | if (data->br_stack_cntr) { | |
7424 | size = data->br_stack->nr * sizeof(u64); | |
7425 | perf_output_copy(handle, data->br_stack_cntr, size); | |
7426 | } | |
bce38cd5 SE |
7427 | } else { |
7428 | /* | |
7429 | * we always store at least the value of nr | |
7430 | */ | |
7431 | u64 nr = 0; | |
7432 | perf_output_put(handle, nr); | |
7433 | } | |
7434 | } | |
4018994f JO |
7435 | |
7436 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
7437 | u64 abi = data->regs_user.abi; | |
7438 | ||
7439 | /* | |
7440 | * If there are no regs to dump, notice it through | |
7441 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
7442 | */ | |
7443 | perf_output_put(handle, abi); | |
7444 | ||
7445 | if (abi) { | |
7446 | u64 mask = event->attr.sample_regs_user; | |
7447 | perf_output_sample_regs(handle, | |
7448 | data->regs_user.regs, | |
7449 | mask); | |
7450 | } | |
7451 | } | |
c5ebcedb | 7452 | |
a5cdd40c | 7453 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
7454 | perf_output_sample_ustack(handle, |
7455 | data->stack_user_size, | |
7456 | data->regs_user.regs); | |
a5cdd40c | 7457 | } |
c3feedf2 | 7458 | |
2a6c6b7d KL |
7459 | if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) |
7460 | perf_output_put(handle, data->weight.full); | |
d6be9ad6 SE |
7461 | |
7462 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
7463 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 7464 | |
fdfbbd07 AK |
7465 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
7466 | perf_output_put(handle, data->txn); | |
7467 | ||
60e2364e SE |
7468 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
7469 | u64 abi = data->regs_intr.abi; | |
7470 | /* | |
7471 | * If there are no regs to dump, notice it through | |
7472 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
7473 | */ | |
7474 | perf_output_put(handle, abi); | |
7475 | ||
7476 | if (abi) { | |
7477 | u64 mask = event->attr.sample_regs_intr; | |
7478 | ||
7479 | perf_output_sample_regs(handle, | |
7480 | data->regs_intr.regs, | |
7481 | mask); | |
7482 | } | |
7483 | } | |
7484 | ||
fc7ce9c7 KL |
7485 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
7486 | perf_output_put(handle, data->phys_addr); | |
7487 | ||
6546b19f NK |
7488 | if (sample_type & PERF_SAMPLE_CGROUP) |
7489 | perf_output_put(handle, data->cgroup); | |
7490 | ||
8d97e718 KL |
7491 | if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) |
7492 | perf_output_put(handle, data->data_page_size); | |
7493 | ||
995f088e SE |
7494 | if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) |
7495 | perf_output_put(handle, data->code_page_size); | |
7496 | ||
a4faf00d AS |
7497 | if (sample_type & PERF_SAMPLE_AUX) { |
7498 | perf_output_put(handle, data->aux_size); | |
7499 | ||
7500 | if (data->aux_size) | |
7501 | perf_aux_sample_output(event, handle, data); | |
7502 | } | |
7503 | ||
a5cdd40c PZ |
7504 | if (!event->attr.watermark) { |
7505 | int wakeup_events = event->attr.wakeup_events; | |
7506 | ||
7507 | if (wakeup_events) { | |
56de4e8f | 7508 | struct perf_buffer *rb = handle->rb; |
a5cdd40c PZ |
7509 | int events = local_inc_return(&rb->events); |
7510 | ||
7511 | if (events >= wakeup_events) { | |
7512 | local_sub(wakeup_events, &rb->events); | |
7513 | local_inc(&rb->wakeup); | |
7514 | } | |
7515 | } | |
7516 | } | |
5622f295 MM |
7517 | } |
7518 | ||
fc7ce9c7 KL |
7519 | static u64 perf_virt_to_phys(u64 virt) |
7520 | { | |
7521 | u64 phys_addr = 0; | |
fc7ce9c7 KL |
7522 | |
7523 | if (!virt) | |
7524 | return 0; | |
7525 | ||
7526 | if (virt >= TASK_SIZE) { | |
7527 | /* If it's vmalloc()d memory, leave phys_addr as 0 */ | |
7528 | if (virt_addr_valid((void *)(uintptr_t)virt) && | |
7529 | !(virt >= VMALLOC_START && virt < VMALLOC_END)) | |
7530 | phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); | |
7531 | } else { | |
7532 | /* | |
7533 | * Walking the pages tables for user address. | |
7534 | * Interrupts are disabled, so it prevents any tear down | |
7535 | * of the page tables. | |
dadbb612 | 7536 | * Try IRQ-safe get_user_page_fast_only first. |
fc7ce9c7 KL |
7537 | * If failed, leave phys_addr as 0. |
7538 | */ | |
d3296fb3 | 7539 | if (current->mm != NULL) { |
4716023a GT |
7540 | struct page *p; |
7541 | ||
d3296fb3 | 7542 | pagefault_disable(); |
4716023a | 7543 | if (get_user_page_fast_only(virt, 0, &p)) { |
d3296fb3 | 7544 | phys_addr = page_to_phys(p) + virt % PAGE_SIZE; |
4716023a GT |
7545 | put_page(p); |
7546 | } | |
d3296fb3 JO |
7547 | pagefault_enable(); |
7548 | } | |
fc7ce9c7 KL |
7549 | } |
7550 | ||
7551 | return phys_addr; | |
7552 | } | |
7553 | ||
8d97e718 | 7554 | /* |
8af26be0 | 7555 | * Return the pagetable size of a given virtual address. |
8d97e718 | 7556 | */ |
8af26be0 | 7557 | static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) |
8d97e718 | 7558 | { |
8af26be0 | 7559 | u64 size = 0; |
8d97e718 | 7560 | |
8af26be0 PZ |
7561 | #ifdef CONFIG_HAVE_FAST_GUP |
7562 | pgd_t *pgdp, pgd; | |
7563 | p4d_t *p4dp, p4d; | |
7564 | pud_t *pudp, pud; | |
7565 | pmd_t *pmdp, pmd; | |
7566 | pte_t *ptep, pte; | |
8d97e718 | 7567 | |
8af26be0 PZ |
7568 | pgdp = pgd_offset(mm, addr); |
7569 | pgd = READ_ONCE(*pgdp); | |
7570 | if (pgd_none(pgd)) | |
8d97e718 KL |
7571 | return 0; |
7572 | ||
8af26be0 PZ |
7573 | if (pgd_leaf(pgd)) |
7574 | return pgd_leaf_size(pgd); | |
8d97e718 | 7575 | |
8af26be0 PZ |
7576 | p4dp = p4d_offset_lockless(pgdp, pgd, addr); |
7577 | p4d = READ_ONCE(*p4dp); | |
7578 | if (!p4d_present(p4d)) | |
8d97e718 KL |
7579 | return 0; |
7580 | ||
8af26be0 PZ |
7581 | if (p4d_leaf(p4d)) |
7582 | return p4d_leaf_size(p4d); | |
8d97e718 | 7583 | |
8af26be0 PZ |
7584 | pudp = pud_offset_lockless(p4dp, p4d, addr); |
7585 | pud = READ_ONCE(*pudp); | |
7586 | if (!pud_present(pud)) | |
8d97e718 KL |
7587 | return 0; |
7588 | ||
8af26be0 PZ |
7589 | if (pud_leaf(pud)) |
7590 | return pud_leaf_size(pud); | |
8d97e718 | 7591 | |
8af26be0 | 7592 | pmdp = pmd_offset_lockless(pudp, pud, addr); |
a92cbb82 | 7593 | again: |
1180e732 | 7594 | pmd = pmdp_get_lockless(pmdp); |
8af26be0 | 7595 | if (!pmd_present(pmd)) |
8d97e718 | 7596 | return 0; |
8d97e718 | 7597 | |
8af26be0 PZ |
7598 | if (pmd_leaf(pmd)) |
7599 | return pmd_leaf_size(pmd); | |
51b646b2 | 7600 | |
8af26be0 | 7601 | ptep = pte_offset_map(&pmd, addr); |
a92cbb82 HD |
7602 | if (!ptep) |
7603 | goto again; | |
7604 | ||
8af26be0 PZ |
7605 | pte = ptep_get_lockless(ptep); |
7606 | if (pte_present(pte)) | |
7607 | size = pte_leaf_size(pte); | |
7608 | pte_unmap(ptep); | |
7609 | #endif /* CONFIG_HAVE_FAST_GUP */ | |
8d97e718 | 7610 | |
8af26be0 | 7611 | return size; |
8d97e718 KL |
7612 | } |
7613 | ||
8d97e718 KL |
7614 | static u64 perf_get_page_size(unsigned long addr) |
7615 | { | |
7616 | struct mm_struct *mm; | |
7617 | unsigned long flags; | |
7618 | u64 size; | |
7619 | ||
7620 | if (!addr) | |
7621 | return 0; | |
7622 | ||
7623 | /* | |
7624 | * Software page-table walkers must disable IRQs, | |
7625 | * which prevents any tear down of the page tables. | |
7626 | */ | |
7627 | local_irq_save(flags); | |
7628 | ||
7629 | mm = current->mm; | |
7630 | if (!mm) { | |
7631 | /* | |
7632 | * For kernel threads and the like, use init_mm so that | |
7633 | * we can find kernel memory. | |
7634 | */ | |
7635 | mm = &init_mm; | |
7636 | } | |
7637 | ||
8af26be0 | 7638 | size = perf_get_pgtable_size(mm, addr); |
8d97e718 KL |
7639 | |
7640 | local_irq_restore(flags); | |
7641 | ||
7642 | return size; | |
7643 | } | |
7644 | ||
99e818cc JO |
7645 | static struct perf_callchain_entry __empty_callchain = { .nr = 0, }; |
7646 | ||
6cbc304f | 7647 | struct perf_callchain_entry * |
8cf7e0e2 JO |
7648 | perf_callchain(struct perf_event *event, struct pt_regs *regs) |
7649 | { | |
7650 | bool kernel = !event->attr.exclude_callchain_kernel; | |
7651 | bool user = !event->attr.exclude_callchain_user; | |
7652 | /* Disallow cross-task user callchains. */ | |
7653 | bool crosstask = event->ctx->task && event->ctx->task != current; | |
7654 | const u32 max_stack = event->attr.sample_max_stack; | |
99e818cc | 7655 | struct perf_callchain_entry *callchain; |
8cf7e0e2 JO |
7656 | |
7657 | if (!kernel && !user) | |
99e818cc | 7658 | return &__empty_callchain; |
8cf7e0e2 | 7659 | |
99e818cc JO |
7660 | callchain = get_perf_callchain(regs, 0, kernel, user, |
7661 | max_stack, crosstask, true); | |
7662 | return callchain ?: &__empty_callchain; | |
8cf7e0e2 JO |
7663 | } |
7664 | ||
bb447c27 NK |
7665 | static __always_inline u64 __cond_set(u64 flags, u64 s, u64 d) |
7666 | { | |
7667 | return d * !!(flags & s); | |
7668 | } | |
7669 | ||
f6e70715 | 7670 | void perf_prepare_sample(struct perf_sample_data *data, |
cdd6c482 | 7671 | struct perf_event *event, |
5622f295 | 7672 | struct pt_regs *regs) |
7b732a75 | 7673 | { |
cdd6c482 | 7674 | u64 sample_type = event->attr.sample_type; |
3aac580d | 7675 | u64 filtered_sample_type; |
7b732a75 | 7676 | |
3aac580d | 7677 | /* |
bb447c27 NK |
7678 | * Add the sample flags that are dependent to others. And clear the |
7679 | * sample flags that have already been done by the PMU driver. | |
3aac580d | 7680 | */ |
bb447c27 NK |
7681 | filtered_sample_type = sample_type; |
7682 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_CODE_PAGE_SIZE, | |
7683 | PERF_SAMPLE_IP); | |
7684 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_DATA_PAGE_SIZE | | |
7685 | PERF_SAMPLE_PHYS_ADDR, PERF_SAMPLE_ADDR); | |
7686 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_STACK_USER, | |
7687 | PERF_SAMPLE_REGS_USER); | |
7688 | filtered_sample_type &= ~data->sample_flags; | |
6844c09d | 7689 | |
f6e70715 NK |
7690 | if (filtered_sample_type == 0) { |
7691 | /* Make sure it has the correct data->type for output */ | |
7692 | data->type = event->attr.sample_type; | |
7693 | return; | |
394ee076 PZ |
7694 | } |
7695 | ||
a7c8d0da | 7696 | __perf_event_header__init_id(data, event, filtered_sample_type); |
7e3f977e | 7697 | |
bb447c27 | 7698 | if (filtered_sample_type & PERF_SAMPLE_IP) { |
5622f295 | 7699 | data->ip = perf_instruction_pointer(regs); |
bb447c27 NK |
7700 | data->sample_flags |= PERF_SAMPLE_IP; |
7701 | } | |
7e3f977e | 7702 | |
31046500 NK |
7703 | if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN) |
7704 | perf_sample_save_callchain(data, event, regs); | |
a044560c | 7705 | |
0a9081cf NK |
7706 | if (filtered_sample_type & PERF_SAMPLE_RAW) { |
7707 | data->raw = NULL; | |
7708 | data->dyn_size += sizeof(u64); | |
7709 | data->sample_flags |= PERF_SAMPLE_RAW; | |
7f453c24 | 7710 | } |
bce38cd5 | 7711 | |
eb55b455 NK |
7712 | if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) { |
7713 | data->br_stack = NULL; | |
7714 | data->dyn_size += sizeof(u64); | |
7715 | data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; | |
bce38cd5 | 7716 | } |
4018994f | 7717 | |
bb447c27 | 7718 | if (filtered_sample_type & PERF_SAMPLE_REGS_USER) |
76a4efa8 | 7719 | perf_sample_regs_user(&data->regs_user, regs); |
2565711f | 7720 | |
bb447c27 NK |
7721 | /* |
7722 | * It cannot use the filtered_sample_type here as REGS_USER can be set | |
7723 | * by STACK_USER (using __cond_set() above) and we don't want to update | |
7724 | * the dyn_size if it's not requested by users. | |
7725 | */ | |
7726 | if ((sample_type & ~data->sample_flags) & PERF_SAMPLE_REGS_USER) { | |
4018994f JO |
7727 | /* regs dump ABI info */ |
7728 | int size = sizeof(u64); | |
7729 | ||
4018994f JO |
7730 | if (data->regs_user.regs) { |
7731 | u64 mask = event->attr.sample_regs_user; | |
7732 | size += hweight64(mask) * sizeof(u64); | |
7733 | } | |
7734 | ||
4cf7a136 | 7735 | data->dyn_size += size; |
bb447c27 | 7736 | data->sample_flags |= PERF_SAMPLE_REGS_USER; |
4018994f | 7737 | } |
c5ebcedb | 7738 | |
bb447c27 | 7739 | if (filtered_sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb | 7740 | /* |
9f014e3a | 7741 | * Either we need PERF_SAMPLE_STACK_USER bit to be always |
c5ebcedb JO |
7742 | * processed as the last one or have additional check added |
7743 | * in case new sample type is added, because we could eat | |
7744 | * up the rest of the sample size. | |
7745 | */ | |
c5ebcedb | 7746 | u16 stack_size = event->attr.sample_stack_user; |
f6e70715 | 7747 | u16 header_size = perf_sample_data_size(data, event); |
c5ebcedb JO |
7748 | u16 size = sizeof(u64); |
7749 | ||
f6e70715 | 7750 | stack_size = perf_sample_ustack_size(stack_size, header_size, |
2565711f | 7751 | data->regs_user.regs); |
c5ebcedb JO |
7752 | |
7753 | /* | |
7754 | * If there is something to dump, add space for the dump | |
7755 | * itself and for the field that tells the dynamic size, | |
7756 | * which is how many have been actually dumped. | |
7757 | */ | |
7758 | if (stack_size) | |
7759 | size += sizeof(u64) + stack_size; | |
7760 | ||
7761 | data->stack_user_size = stack_size; | |
4cf7a136 | 7762 | data->dyn_size += size; |
bb447c27 | 7763 | data->sample_flags |= PERF_SAMPLE_STACK_USER; |
c5ebcedb | 7764 | } |
60e2364e | 7765 | |
bb447c27 | 7766 | if (filtered_sample_type & PERF_SAMPLE_WEIGHT_TYPE) { |
2abe681d | 7767 | data->weight.full = 0; |
bb447c27 NK |
7768 | data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; |
7769 | } | |
2abe681d | 7770 | |
bb447c27 | 7771 | if (filtered_sample_type & PERF_SAMPLE_DATA_SRC) { |
e16fd7f2 | 7772 | data->data_src.val = PERF_MEM_NA; |
bb447c27 NK |
7773 | data->sample_flags |= PERF_SAMPLE_DATA_SRC; |
7774 | } | |
e16fd7f2 | 7775 | |
bb447c27 | 7776 | if (filtered_sample_type & PERF_SAMPLE_TRANSACTION) { |
ee9db0e1 | 7777 | data->txn = 0; |
bb447c27 NK |
7778 | data->sample_flags |= PERF_SAMPLE_TRANSACTION; |
7779 | } | |
ee9db0e1 | 7780 | |
bb447c27 NK |
7781 | if (filtered_sample_type & PERF_SAMPLE_ADDR) { |
7782 | data->addr = 0; | |
7783 | data->sample_flags |= PERF_SAMPLE_ADDR; | |
7b084630 NK |
7784 | } |
7785 | ||
bb447c27 | 7786 | if (filtered_sample_type & PERF_SAMPLE_REGS_INTR) { |
60e2364e SE |
7787 | /* regs dump ABI info */ |
7788 | int size = sizeof(u64); | |
7789 | ||
7790 | perf_sample_regs_intr(&data->regs_intr, regs); | |
7791 | ||
7792 | if (data->regs_intr.regs) { | |
7793 | u64 mask = event->attr.sample_regs_intr; | |
7794 | ||
7795 | size += hweight64(mask) * sizeof(u64); | |
7796 | } | |
7797 | ||
4cf7a136 | 7798 | data->dyn_size += size; |
bb447c27 | 7799 | data->sample_flags |= PERF_SAMPLE_REGS_INTR; |
60e2364e | 7800 | } |
fc7ce9c7 | 7801 | |
bb447c27 | 7802 | if (filtered_sample_type & PERF_SAMPLE_PHYS_ADDR) { |
fc7ce9c7 | 7803 | data->phys_addr = perf_virt_to_phys(data->addr); |
bb447c27 NK |
7804 | data->sample_flags |= PERF_SAMPLE_PHYS_ADDR; |
7805 | } | |
a4faf00d | 7806 | |
6546b19f | 7807 | #ifdef CONFIG_CGROUP_PERF |
bb447c27 | 7808 | if (filtered_sample_type & PERF_SAMPLE_CGROUP) { |
6546b19f NK |
7809 | struct cgroup *cgrp; |
7810 | ||
7811 | /* protected by RCU */ | |
7812 | cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup; | |
7813 | data->cgroup = cgroup_id(cgrp); | |
bb447c27 | 7814 | data->sample_flags |= PERF_SAMPLE_CGROUP; |
6546b19f NK |
7815 | } |
7816 | #endif | |
7817 | ||
8d97e718 KL |
7818 | /* |
7819 | * PERF_DATA_PAGE_SIZE requires PERF_SAMPLE_ADDR. If the user doesn't | |
7820 | * require PERF_SAMPLE_ADDR, kernel implicitly retrieve the data->addr, | |
7821 | * but the value will not dump to the userspace. | |
7822 | */ | |
bb447c27 | 7823 | if (filtered_sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) { |
8d97e718 | 7824 | data->data_page_size = perf_get_page_size(data->addr); |
bb447c27 NK |
7825 | data->sample_flags |= PERF_SAMPLE_DATA_PAGE_SIZE; |
7826 | } | |
8d97e718 | 7827 | |
bb447c27 | 7828 | if (filtered_sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) { |
995f088e | 7829 | data->code_page_size = perf_get_page_size(data->ip); |
bb447c27 NK |
7830 | data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE; |
7831 | } | |
995f088e | 7832 | |
bb447c27 | 7833 | if (filtered_sample_type & PERF_SAMPLE_AUX) { |
a4faf00d | 7834 | u64 size; |
f6e70715 | 7835 | u16 header_size = perf_sample_data_size(data, event); |
a4faf00d | 7836 | |
f6e70715 | 7837 | header_size += sizeof(u64); /* size */ |
a4faf00d AS |
7838 | |
7839 | /* | |
7840 | * Given the 16bit nature of header::size, an AUX sample can | |
7841 | * easily overflow it, what with all the preceding sample bits. | |
7842 | * Make sure this doesn't happen by using up to U16_MAX bytes | |
7843 | * per sample in total (rounded down to 8 byte boundary). | |
7844 | */ | |
f6e70715 | 7845 | size = min_t(size_t, U16_MAX - header_size, |
a4faf00d AS |
7846 | event->attr.aux_sample_size); |
7847 | size = rounddown(size, 8); | |
7848 | size = perf_prepare_sample_aux(event, data, size); | |
7849 | ||
f6e70715 | 7850 | WARN_ON_ONCE(size + header_size > U16_MAX); |
4cf7a136 | 7851 | data->dyn_size += size + sizeof(u64); /* size above */ |
bb447c27 | 7852 | data->sample_flags |= PERF_SAMPLE_AUX; |
a4faf00d | 7853 | } |
f6e70715 | 7854 | } |
4cf7a136 | 7855 | |
f6e70715 NK |
7856 | void perf_prepare_header(struct perf_event_header *header, |
7857 | struct perf_sample_data *data, | |
7858 | struct perf_event *event, | |
7859 | struct pt_regs *regs) | |
7860 | { | |
7861 | header->type = PERF_RECORD_SAMPLE; | |
7862 | header->size = perf_sample_data_size(data, event); | |
7863 | header->misc = perf_misc_flags(regs); | |
4cf7a136 | 7864 | |
a4faf00d AS |
7865 | /* |
7866 | * If you're adding more sample types here, you likely need to do | |
7867 | * something about the overflowing header::size, like repurpose the | |
7868 | * lowest 3 bits of size, which should be always zero at the moment. | |
7869 | * This raises a more important question, do we really need 512k sized | |
7870 | * samples and why, so good argumentation is in order for whatever you | |
7871 | * do here next. | |
7872 | */ | |
7873 | WARN_ON_ONCE(header->size & 7); | |
5622f295 | 7874 | } |
7f453c24 | 7875 | |
56201969 | 7876 | static __always_inline int |
9ecda41a WN |
7877 | __perf_event_output(struct perf_event *event, |
7878 | struct perf_sample_data *data, | |
7879 | struct pt_regs *regs, | |
7880 | int (*output_begin)(struct perf_output_handle *, | |
267fb273 | 7881 | struct perf_sample_data *, |
9ecda41a WN |
7882 | struct perf_event *, |
7883 | unsigned int)) | |
5622f295 MM |
7884 | { |
7885 | struct perf_output_handle handle; | |
7886 | struct perf_event_header header; | |
56201969 | 7887 | int err; |
689802b2 | 7888 | |
927c7a9e FW |
7889 | /* protect the callchain buffers */ |
7890 | rcu_read_lock(); | |
7891 | ||
f6e70715 NK |
7892 | perf_prepare_sample(data, event, regs); |
7893 | perf_prepare_header(&header, data, event, regs); | |
5c148194 | 7894 | |
267fb273 | 7895 | err = output_begin(&handle, data, event, header.size); |
56201969 | 7896 | if (err) |
927c7a9e | 7897 | goto exit; |
0322cd6e | 7898 | |
cdd6c482 | 7899 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 7900 | |
8a057d84 | 7901 | perf_output_end(&handle); |
927c7a9e FW |
7902 | |
7903 | exit: | |
7904 | rcu_read_unlock(); | |
56201969 | 7905 | return err; |
0322cd6e PZ |
7906 | } |
7907 | ||
9ecda41a WN |
7908 | void |
7909 | perf_event_output_forward(struct perf_event *event, | |
7910 | struct perf_sample_data *data, | |
7911 | struct pt_regs *regs) | |
7912 | { | |
7913 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
7914 | } | |
7915 | ||
7916 | void | |
7917 | perf_event_output_backward(struct perf_event *event, | |
7918 | struct perf_sample_data *data, | |
7919 | struct pt_regs *regs) | |
7920 | { | |
7921 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
7922 | } | |
7923 | ||
56201969 | 7924 | int |
9ecda41a WN |
7925 | perf_event_output(struct perf_event *event, |
7926 | struct perf_sample_data *data, | |
7927 | struct pt_regs *regs) | |
7928 | { | |
56201969 | 7929 | return __perf_event_output(event, data, regs, perf_output_begin); |
9ecda41a WN |
7930 | } |
7931 | ||
38b200d6 | 7932 | /* |
cdd6c482 | 7933 | * read event_id |
38b200d6 PZ |
7934 | */ |
7935 | ||
7936 | struct perf_read_event { | |
7937 | struct perf_event_header header; | |
7938 | ||
7939 | u32 pid; | |
7940 | u32 tid; | |
38b200d6 PZ |
7941 | }; |
7942 | ||
7943 | static void | |
cdd6c482 | 7944 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
7945 | struct task_struct *task) |
7946 | { | |
7947 | struct perf_output_handle handle; | |
c980d109 | 7948 | struct perf_sample_data sample; |
dfc65094 | 7949 | struct perf_read_event read_event = { |
38b200d6 | 7950 | .header = { |
cdd6c482 | 7951 | .type = PERF_RECORD_READ, |
38b200d6 | 7952 | .misc = 0, |
c320c7b7 | 7953 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 7954 | }, |
cdd6c482 IM |
7955 | .pid = perf_event_pid(event, task), |
7956 | .tid = perf_event_tid(event, task), | |
38b200d6 | 7957 | }; |
3dab77fb | 7958 | int ret; |
38b200d6 | 7959 | |
c980d109 | 7960 | perf_event_header__init_id(&read_event.header, &sample, event); |
267fb273 | 7961 | ret = perf_output_begin(&handle, &sample, event, read_event.header.size); |
38b200d6 PZ |
7962 | if (ret) |
7963 | return; | |
7964 | ||
dfc65094 | 7965 | perf_output_put(&handle, read_event); |
cdd6c482 | 7966 | perf_output_read(&handle, event); |
c980d109 | 7967 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 7968 | |
38b200d6 PZ |
7969 | perf_output_end(&handle); |
7970 | } | |
7971 | ||
aab5b71e | 7972 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
7973 | |
7974 | static void | |
aab5b71e PZ |
7975 | perf_iterate_ctx(struct perf_event_context *ctx, |
7976 | perf_iterate_f output, | |
b73e4fef | 7977 | void *data, bool all) |
52d857a8 JO |
7978 | { |
7979 | struct perf_event *event; | |
7980 | ||
7981 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
7982 | if (!all) { |
7983 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
7984 | continue; | |
7985 | if (!event_filter_match(event)) | |
7986 | continue; | |
7987 | } | |
7988 | ||
67516844 | 7989 | output(event, data); |
52d857a8 JO |
7990 | } |
7991 | } | |
7992 | ||
aab5b71e | 7993 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
7994 | { |
7995 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
7996 | struct perf_event *event; | |
7997 | ||
7998 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
7999 | /* |
8000 | * Skip events that are not fully formed yet; ensure that | |
8001 | * if we observe event->ctx, both event and ctx will be | |
8002 | * complete enough. See perf_install_in_context(). | |
8003 | */ | |
8004 | if (!smp_load_acquire(&event->ctx)) | |
8005 | continue; | |
8006 | ||
f2fb6bef KL |
8007 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
8008 | continue; | |
8009 | if (!event_filter_match(event)) | |
8010 | continue; | |
8011 | output(event, data); | |
8012 | } | |
8013 | } | |
8014 | ||
aab5b71e PZ |
8015 | /* |
8016 | * Iterate all events that need to receive side-band events. | |
8017 | * | |
8018 | * For new callers; ensure that account_pmu_sb_event() includes | |
8019 | * your event, otherwise it might not get delivered. | |
8020 | */ | |
52d857a8 | 8021 | static void |
aab5b71e | 8022 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
8023 | struct perf_event_context *task_ctx) |
8024 | { | |
52d857a8 | 8025 | struct perf_event_context *ctx; |
52d857a8 | 8026 | |
aab5b71e PZ |
8027 | rcu_read_lock(); |
8028 | preempt_disable(); | |
8029 | ||
4e93ad60 | 8030 | /* |
aab5b71e PZ |
8031 | * If we have task_ctx != NULL we only notify the task context itself. |
8032 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
8033 | * context. |
8034 | */ | |
8035 | if (task_ctx) { | |
aab5b71e PZ |
8036 | perf_iterate_ctx(task_ctx, output, data, false); |
8037 | goto done; | |
4e93ad60 JO |
8038 | } |
8039 | ||
aab5b71e | 8040 | perf_iterate_sb_cpu(output, data); |
f2fb6bef | 8041 | |
bd275681 PZ |
8042 | ctx = rcu_dereference(current->perf_event_ctxp); |
8043 | if (ctx) | |
8044 | perf_iterate_ctx(ctx, output, data, false); | |
aab5b71e | 8045 | done: |
f2fb6bef | 8046 | preempt_enable(); |
52d857a8 | 8047 | rcu_read_unlock(); |
95ff4ca2 AS |
8048 | } |
8049 | ||
375637bc AS |
8050 | /* |
8051 | * Clear all file-based filters at exec, they'll have to be | |
8052 | * re-instated when/if these objects are mmapped again. | |
8053 | */ | |
8054 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
8055 | { | |
8056 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8057 | struct perf_addr_filter *filter; | |
8058 | unsigned int restart = 0, count = 0; | |
8059 | unsigned long flags; | |
8060 | ||
8061 | if (!has_addr_filter(event)) | |
8062 | return; | |
8063 | ||
8064 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8065 | list_for_each_entry(filter, &ifh->list, entry) { | |
9511bce9 | 8066 | if (filter->path.dentry) { |
c60f83b8 AS |
8067 | event->addr_filter_ranges[count].start = 0; |
8068 | event->addr_filter_ranges[count].size = 0; | |
375637bc AS |
8069 | restart++; |
8070 | } | |
8071 | ||
8072 | count++; | |
8073 | } | |
8074 | ||
8075 | if (restart) | |
8076 | event->addr_filters_gen++; | |
8077 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8078 | ||
8079 | if (restart) | |
767ae086 | 8080 | perf_event_stop(event, 1); |
375637bc AS |
8081 | } |
8082 | ||
8083 | void perf_event_exec(void) | |
8084 | { | |
8085 | struct perf_event_context *ctx; | |
375637bc | 8086 | |
bd275681 PZ |
8087 | ctx = perf_pin_task_context(current); |
8088 | if (!ctx) | |
8089 | return; | |
375637bc | 8090 | |
bd275681 PZ |
8091 | perf_event_enable_on_exec(ctx); |
8092 | perf_event_remove_on_exec(ctx); | |
8093 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); | |
375637bc | 8094 | |
bd275681 PZ |
8095 | perf_unpin_context(ctx); |
8096 | put_ctx(ctx); | |
375637bc AS |
8097 | } |
8098 | ||
95ff4ca2 | 8099 | struct remote_output { |
56de4e8f | 8100 | struct perf_buffer *rb; |
95ff4ca2 AS |
8101 | int err; |
8102 | }; | |
8103 | ||
8104 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
8105 | { | |
8106 | struct perf_event *parent = event->parent; | |
8107 | struct remote_output *ro = data; | |
56de4e8f | 8108 | struct perf_buffer *rb = ro->rb; |
375637bc AS |
8109 | struct stop_event_data sd = { |
8110 | .event = event, | |
8111 | }; | |
95ff4ca2 AS |
8112 | |
8113 | if (!has_aux(event)) | |
8114 | return; | |
8115 | ||
8116 | if (!parent) | |
8117 | parent = event; | |
8118 | ||
8119 | /* | |
8120 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
8121 | * ring-buffer, but it will be the child that's actually using it. |
8122 | * | |
8123 | * We are using event::rb to determine if the event should be stopped, | |
8124 | * however this may race with ring_buffer_attach() (through set_output), | |
8125 | * which will make us skip the event that actually needs to be stopped. | |
8126 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
8127 | * its rb pointer. | |
95ff4ca2 AS |
8128 | */ |
8129 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 8130 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
8131 | } |
8132 | ||
8133 | static int __perf_pmu_output_stop(void *info) | |
8134 | { | |
8135 | struct perf_event *event = info; | |
bd275681 | 8136 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
95ff4ca2 AS |
8137 | struct remote_output ro = { |
8138 | .rb = event->rb, | |
8139 | }; | |
8140 | ||
8141 | rcu_read_lock(); | |
aab5b71e | 8142 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 8143 | if (cpuctx->task_ctx) |
aab5b71e | 8144 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 8145 | &ro, false); |
95ff4ca2 AS |
8146 | rcu_read_unlock(); |
8147 | ||
8148 | return ro.err; | |
8149 | } | |
8150 | ||
8151 | static void perf_pmu_output_stop(struct perf_event *event) | |
8152 | { | |
8153 | struct perf_event *iter; | |
8154 | int err, cpu; | |
8155 | ||
8156 | restart: | |
8157 | rcu_read_lock(); | |
8158 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
8159 | /* | |
8160 | * For per-CPU events, we need to make sure that neither they | |
8161 | * nor their children are running; for cpu==-1 events it's | |
8162 | * sufficient to stop the event itself if it's active, since | |
8163 | * it can't have children. | |
8164 | */ | |
8165 | cpu = iter->cpu; | |
8166 | if (cpu == -1) | |
8167 | cpu = READ_ONCE(iter->oncpu); | |
8168 | ||
8169 | if (cpu == -1) | |
8170 | continue; | |
8171 | ||
8172 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
8173 | if (err == -EAGAIN) { | |
8174 | rcu_read_unlock(); | |
8175 | goto restart; | |
8176 | } | |
8177 | } | |
8178 | rcu_read_unlock(); | |
52d857a8 JO |
8179 | } |
8180 | ||
60313ebe | 8181 | /* |
9f498cc5 PZ |
8182 | * task tracking -- fork/exit |
8183 | * | |
13d7a241 | 8184 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
8185 | */ |
8186 | ||
9f498cc5 | 8187 | struct perf_task_event { |
3a80b4a3 | 8188 | struct task_struct *task; |
cdd6c482 | 8189 | struct perf_event_context *task_ctx; |
60313ebe PZ |
8190 | |
8191 | struct { | |
8192 | struct perf_event_header header; | |
8193 | ||
8194 | u32 pid; | |
8195 | u32 ppid; | |
9f498cc5 PZ |
8196 | u32 tid; |
8197 | u32 ptid; | |
393b2ad8 | 8198 | u64 time; |
cdd6c482 | 8199 | } event_id; |
60313ebe PZ |
8200 | }; |
8201 | ||
67516844 JO |
8202 | static int perf_event_task_match(struct perf_event *event) |
8203 | { | |
13d7a241 SE |
8204 | return event->attr.comm || event->attr.mmap || |
8205 | event->attr.mmap2 || event->attr.mmap_data || | |
8206 | event->attr.task; | |
67516844 JO |
8207 | } |
8208 | ||
cdd6c482 | 8209 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 8210 | void *data) |
60313ebe | 8211 | { |
52d857a8 | 8212 | struct perf_task_event *task_event = data; |
60313ebe | 8213 | struct perf_output_handle handle; |
c980d109 | 8214 | struct perf_sample_data sample; |
9f498cc5 | 8215 | struct task_struct *task = task_event->task; |
c980d109 | 8216 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 8217 | |
67516844 JO |
8218 | if (!perf_event_task_match(event)) |
8219 | return; | |
8220 | ||
c980d109 | 8221 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 8222 | |
267fb273 | 8223 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8224 | task_event->event_id.header.size); |
ef60777c | 8225 | if (ret) |
c980d109 | 8226 | goto out; |
60313ebe | 8227 | |
cdd6c482 | 8228 | task_event->event_id.pid = perf_event_pid(event, task); |
cdd6c482 | 8229 | task_event->event_id.tid = perf_event_tid(event, task); |
f3bed55e IR |
8230 | |
8231 | if (task_event->event_id.header.type == PERF_RECORD_EXIT) { | |
8232 | task_event->event_id.ppid = perf_event_pid(event, | |
8233 | task->real_parent); | |
8234 | task_event->event_id.ptid = perf_event_pid(event, | |
8235 | task->real_parent); | |
8236 | } else { /* PERF_RECORD_FORK */ | |
8237 | task_event->event_id.ppid = perf_event_pid(event, current); | |
8238 | task_event->event_id.ptid = perf_event_tid(event, current); | |
8239 | } | |
9f498cc5 | 8240 | |
34f43927 PZ |
8241 | task_event->event_id.time = perf_event_clock(event); |
8242 | ||
cdd6c482 | 8243 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 8244 | |
c980d109 ACM |
8245 | perf_event__output_id_sample(event, &handle, &sample); |
8246 | ||
60313ebe | 8247 | perf_output_end(&handle); |
c980d109 ACM |
8248 | out: |
8249 | task_event->event_id.header.size = size; | |
60313ebe PZ |
8250 | } |
8251 | ||
cdd6c482 IM |
8252 | static void perf_event_task(struct task_struct *task, |
8253 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 8254 | int new) |
60313ebe | 8255 | { |
9f498cc5 | 8256 | struct perf_task_event task_event; |
60313ebe | 8257 | |
cdd6c482 IM |
8258 | if (!atomic_read(&nr_comm_events) && |
8259 | !atomic_read(&nr_mmap_events) && | |
8260 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
8261 | return; |
8262 | ||
9f498cc5 | 8263 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
8264 | .task = task, |
8265 | .task_ctx = task_ctx, | |
cdd6c482 | 8266 | .event_id = { |
60313ebe | 8267 | .header = { |
cdd6c482 | 8268 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 8269 | .misc = 0, |
cdd6c482 | 8270 | .size = sizeof(task_event.event_id), |
60313ebe | 8271 | }, |
573402db PZ |
8272 | /* .pid */ |
8273 | /* .ppid */ | |
9f498cc5 PZ |
8274 | /* .tid */ |
8275 | /* .ptid */ | |
34f43927 | 8276 | /* .time */ |
60313ebe PZ |
8277 | }, |
8278 | }; | |
8279 | ||
aab5b71e | 8280 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
8281 | &task_event, |
8282 | task_ctx); | |
9f498cc5 PZ |
8283 | } |
8284 | ||
cdd6c482 | 8285 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 8286 | { |
cdd6c482 | 8287 | perf_event_task(task, NULL, 1); |
e4222673 | 8288 | perf_event_namespaces(task); |
60313ebe PZ |
8289 | } |
8290 | ||
8d1b2d93 PZ |
8291 | /* |
8292 | * comm tracking | |
8293 | */ | |
8294 | ||
8295 | struct perf_comm_event { | |
22a4f650 IM |
8296 | struct task_struct *task; |
8297 | char *comm; | |
8d1b2d93 PZ |
8298 | int comm_size; |
8299 | ||
8300 | struct { | |
8301 | struct perf_event_header header; | |
8302 | ||
8303 | u32 pid; | |
8304 | u32 tid; | |
cdd6c482 | 8305 | } event_id; |
8d1b2d93 PZ |
8306 | }; |
8307 | ||
67516844 JO |
8308 | static int perf_event_comm_match(struct perf_event *event) |
8309 | { | |
8310 | return event->attr.comm; | |
8311 | } | |
8312 | ||
cdd6c482 | 8313 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 8314 | void *data) |
8d1b2d93 | 8315 | { |
52d857a8 | 8316 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 8317 | struct perf_output_handle handle; |
c980d109 | 8318 | struct perf_sample_data sample; |
cdd6c482 | 8319 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
8320 | int ret; |
8321 | ||
67516844 JO |
8322 | if (!perf_event_comm_match(event)) |
8323 | return; | |
8324 | ||
c980d109 | 8325 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
267fb273 | 8326 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8327 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
8328 | |
8329 | if (ret) | |
c980d109 | 8330 | goto out; |
8d1b2d93 | 8331 | |
cdd6c482 IM |
8332 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
8333 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 8334 | |
cdd6c482 | 8335 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 8336 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 8337 | comm_event->comm_size); |
c980d109 ACM |
8338 | |
8339 | perf_event__output_id_sample(event, &handle, &sample); | |
8340 | ||
8d1b2d93 | 8341 | perf_output_end(&handle); |
c980d109 ACM |
8342 | out: |
8343 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
8344 | } |
8345 | ||
cdd6c482 | 8346 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 8347 | { |
413ee3b4 | 8348 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 8349 | unsigned int size; |
8d1b2d93 | 8350 | |
413ee3b4 | 8351 | memset(comm, 0, sizeof(comm)); |
c9732f14 | 8352 | strscpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 8353 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
8354 | |
8355 | comm_event->comm = comm; | |
8356 | comm_event->comm_size = size; | |
8357 | ||
cdd6c482 | 8358 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 8359 | |
aab5b71e | 8360 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
8361 | comm_event, |
8362 | NULL); | |
8d1b2d93 PZ |
8363 | } |
8364 | ||
82b89778 | 8365 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 8366 | { |
9ee318a7 PZ |
8367 | struct perf_comm_event comm_event; |
8368 | ||
cdd6c482 | 8369 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 8370 | return; |
a63eaf34 | 8371 | |
9ee318a7 | 8372 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 8373 | .task = task, |
573402db PZ |
8374 | /* .comm */ |
8375 | /* .comm_size */ | |
cdd6c482 | 8376 | .event_id = { |
573402db | 8377 | .header = { |
cdd6c482 | 8378 | .type = PERF_RECORD_COMM, |
82b89778 | 8379 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
8380 | /* .size */ |
8381 | }, | |
8382 | /* .pid */ | |
8383 | /* .tid */ | |
8d1b2d93 PZ |
8384 | }, |
8385 | }; | |
8386 | ||
cdd6c482 | 8387 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
8388 | } |
8389 | ||
e4222673 HB |
8390 | /* |
8391 | * namespaces tracking | |
8392 | */ | |
8393 | ||
8394 | struct perf_namespaces_event { | |
8395 | struct task_struct *task; | |
8396 | ||
8397 | struct { | |
8398 | struct perf_event_header header; | |
8399 | ||
8400 | u32 pid; | |
8401 | u32 tid; | |
8402 | u64 nr_namespaces; | |
8403 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
8404 | } event_id; | |
8405 | }; | |
8406 | ||
8407 | static int perf_event_namespaces_match(struct perf_event *event) | |
8408 | { | |
8409 | return event->attr.namespaces; | |
8410 | } | |
8411 | ||
8412 | static void perf_event_namespaces_output(struct perf_event *event, | |
8413 | void *data) | |
8414 | { | |
8415 | struct perf_namespaces_event *namespaces_event = data; | |
8416 | struct perf_output_handle handle; | |
8417 | struct perf_sample_data sample; | |
34900ec5 | 8418 | u16 header_size = namespaces_event->event_id.header.size; |
e4222673 HB |
8419 | int ret; |
8420 | ||
8421 | if (!perf_event_namespaces_match(event)) | |
8422 | return; | |
8423 | ||
8424 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
8425 | &sample, event); | |
267fb273 | 8426 | ret = perf_output_begin(&handle, &sample, event, |
e4222673 HB |
8427 | namespaces_event->event_id.header.size); |
8428 | if (ret) | |
34900ec5 | 8429 | goto out; |
e4222673 HB |
8430 | |
8431 | namespaces_event->event_id.pid = perf_event_pid(event, | |
8432 | namespaces_event->task); | |
8433 | namespaces_event->event_id.tid = perf_event_tid(event, | |
8434 | namespaces_event->task); | |
8435 | ||
8436 | perf_output_put(&handle, namespaces_event->event_id); | |
8437 | ||
8438 | perf_event__output_id_sample(event, &handle, &sample); | |
8439 | ||
8440 | perf_output_end(&handle); | |
34900ec5 JO |
8441 | out: |
8442 | namespaces_event->event_id.header.size = header_size; | |
e4222673 HB |
8443 | } |
8444 | ||
8445 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
8446 | struct task_struct *task, | |
8447 | const struct proc_ns_operations *ns_ops) | |
8448 | { | |
8449 | struct path ns_path; | |
8450 | struct inode *ns_inode; | |
ce623f89 | 8451 | int error; |
e4222673 HB |
8452 | |
8453 | error = ns_get_path(&ns_path, task, ns_ops); | |
8454 | if (!error) { | |
8455 | ns_inode = ns_path.dentry->d_inode; | |
8456 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
8457 | ns_link_info->ino = ns_inode->i_ino; | |
0e18dd12 | 8458 | path_put(&ns_path); |
e4222673 HB |
8459 | } |
8460 | } | |
8461 | ||
8462 | void perf_event_namespaces(struct task_struct *task) | |
8463 | { | |
8464 | struct perf_namespaces_event namespaces_event; | |
8465 | struct perf_ns_link_info *ns_link_info; | |
8466 | ||
8467 | if (!atomic_read(&nr_namespaces_events)) | |
8468 | return; | |
8469 | ||
8470 | namespaces_event = (struct perf_namespaces_event){ | |
8471 | .task = task, | |
8472 | .event_id = { | |
8473 | .header = { | |
8474 | .type = PERF_RECORD_NAMESPACES, | |
8475 | .misc = 0, | |
8476 | .size = sizeof(namespaces_event.event_id), | |
8477 | }, | |
8478 | /* .pid */ | |
8479 | /* .tid */ | |
8480 | .nr_namespaces = NR_NAMESPACES, | |
8481 | /* .link_info[NR_NAMESPACES] */ | |
8482 | }, | |
8483 | }; | |
8484 | ||
8485 | ns_link_info = namespaces_event.event_id.link_info; | |
8486 | ||
8487 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
8488 | task, &mntns_operations); | |
8489 | ||
8490 | #ifdef CONFIG_USER_NS | |
8491 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
8492 | task, &userns_operations); | |
8493 | #endif | |
8494 | #ifdef CONFIG_NET_NS | |
8495 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
8496 | task, &netns_operations); | |
8497 | #endif | |
8498 | #ifdef CONFIG_UTS_NS | |
8499 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
8500 | task, &utsns_operations); | |
8501 | #endif | |
8502 | #ifdef CONFIG_IPC_NS | |
8503 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
8504 | task, &ipcns_operations); | |
8505 | #endif | |
8506 | #ifdef CONFIG_PID_NS | |
8507 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
8508 | task, &pidns_operations); | |
8509 | #endif | |
8510 | #ifdef CONFIG_CGROUPS | |
8511 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
8512 | task, &cgroupns_operations); | |
8513 | #endif | |
8514 | ||
8515 | perf_iterate_sb(perf_event_namespaces_output, | |
8516 | &namespaces_event, | |
8517 | NULL); | |
8518 | } | |
8519 | ||
96aaab68 NK |
8520 | /* |
8521 | * cgroup tracking | |
8522 | */ | |
8523 | #ifdef CONFIG_CGROUP_PERF | |
8524 | ||
8525 | struct perf_cgroup_event { | |
8526 | char *path; | |
8527 | int path_size; | |
8528 | struct { | |
8529 | struct perf_event_header header; | |
8530 | u64 id; | |
8531 | char path[]; | |
8532 | } event_id; | |
8533 | }; | |
8534 | ||
8535 | static int perf_event_cgroup_match(struct perf_event *event) | |
8536 | { | |
8537 | return event->attr.cgroup; | |
8538 | } | |
8539 | ||
8540 | static void perf_event_cgroup_output(struct perf_event *event, void *data) | |
8541 | { | |
8542 | struct perf_cgroup_event *cgroup_event = data; | |
8543 | struct perf_output_handle handle; | |
8544 | struct perf_sample_data sample; | |
8545 | u16 header_size = cgroup_event->event_id.header.size; | |
8546 | int ret; | |
8547 | ||
8548 | if (!perf_event_cgroup_match(event)) | |
8549 | return; | |
8550 | ||
8551 | perf_event_header__init_id(&cgroup_event->event_id.header, | |
8552 | &sample, event); | |
267fb273 | 8553 | ret = perf_output_begin(&handle, &sample, event, |
96aaab68 NK |
8554 | cgroup_event->event_id.header.size); |
8555 | if (ret) | |
8556 | goto out; | |
8557 | ||
8558 | perf_output_put(&handle, cgroup_event->event_id); | |
8559 | __output_copy(&handle, cgroup_event->path, cgroup_event->path_size); | |
8560 | ||
8561 | perf_event__output_id_sample(event, &handle, &sample); | |
8562 | ||
8563 | perf_output_end(&handle); | |
8564 | out: | |
8565 | cgroup_event->event_id.header.size = header_size; | |
8566 | } | |
8567 | ||
8568 | static void perf_event_cgroup(struct cgroup *cgrp) | |
8569 | { | |
8570 | struct perf_cgroup_event cgroup_event; | |
8571 | char path_enomem[16] = "//enomem"; | |
8572 | char *pathname; | |
8573 | size_t size; | |
8574 | ||
8575 | if (!atomic_read(&nr_cgroup_events)) | |
8576 | return; | |
8577 | ||
8578 | cgroup_event = (struct perf_cgroup_event){ | |
8579 | .event_id = { | |
8580 | .header = { | |
8581 | .type = PERF_RECORD_CGROUP, | |
8582 | .misc = 0, | |
8583 | .size = sizeof(cgroup_event.event_id), | |
8584 | }, | |
8585 | .id = cgroup_id(cgrp), | |
8586 | }, | |
8587 | }; | |
8588 | ||
8589 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); | |
8590 | if (pathname == NULL) { | |
8591 | cgroup_event.path = path_enomem; | |
8592 | } else { | |
8593 | /* just to be sure to have enough space for alignment */ | |
8594 | cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64)); | |
8595 | cgroup_event.path = pathname; | |
8596 | } | |
8597 | ||
8598 | /* | |
8599 | * Since our buffer works in 8 byte units we need to align our string | |
8600 | * size to a multiple of 8. However, we must guarantee the tail end is | |
8601 | * zero'd out to avoid leaking random bits to userspace. | |
8602 | */ | |
8603 | size = strlen(cgroup_event.path) + 1; | |
8604 | while (!IS_ALIGNED(size, sizeof(u64))) | |
8605 | cgroup_event.path[size++] = '\0'; | |
8606 | ||
8607 | cgroup_event.event_id.header.size += size; | |
8608 | cgroup_event.path_size = size; | |
8609 | ||
8610 | perf_iterate_sb(perf_event_cgroup_output, | |
8611 | &cgroup_event, | |
8612 | NULL); | |
8613 | ||
8614 | kfree(pathname); | |
8615 | } | |
8616 | ||
8617 | #endif | |
8618 | ||
0a4a9391 PZ |
8619 | /* |
8620 | * mmap tracking | |
8621 | */ | |
8622 | ||
8623 | struct perf_mmap_event { | |
089dd79d PZ |
8624 | struct vm_area_struct *vma; |
8625 | ||
8626 | const char *file_name; | |
8627 | int file_size; | |
13d7a241 SE |
8628 | int maj, min; |
8629 | u64 ino; | |
8630 | u64 ino_generation; | |
f972eb63 | 8631 | u32 prot, flags; |
88a16a13 JO |
8632 | u8 build_id[BUILD_ID_SIZE_MAX]; |
8633 | u32 build_id_size; | |
0a4a9391 PZ |
8634 | |
8635 | struct { | |
8636 | struct perf_event_header header; | |
8637 | ||
8638 | u32 pid; | |
8639 | u32 tid; | |
8640 | u64 start; | |
8641 | u64 len; | |
8642 | u64 pgoff; | |
cdd6c482 | 8643 | } event_id; |
0a4a9391 PZ |
8644 | }; |
8645 | ||
67516844 JO |
8646 | static int perf_event_mmap_match(struct perf_event *event, |
8647 | void *data) | |
8648 | { | |
8649 | struct perf_mmap_event *mmap_event = data; | |
8650 | struct vm_area_struct *vma = mmap_event->vma; | |
8651 | int executable = vma->vm_flags & VM_EXEC; | |
8652 | ||
8653 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 8654 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
8655 | } |
8656 | ||
cdd6c482 | 8657 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 8658 | void *data) |
0a4a9391 | 8659 | { |
52d857a8 | 8660 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 8661 | struct perf_output_handle handle; |
c980d109 | 8662 | struct perf_sample_data sample; |
cdd6c482 | 8663 | int size = mmap_event->event_id.header.size; |
d9c1bb2f | 8664 | u32 type = mmap_event->event_id.header.type; |
88a16a13 | 8665 | bool use_build_id; |
c980d109 | 8666 | int ret; |
0a4a9391 | 8667 | |
67516844 JO |
8668 | if (!perf_event_mmap_match(event, data)) |
8669 | return; | |
8670 | ||
13d7a241 SE |
8671 | if (event->attr.mmap2) { |
8672 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
8673 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
8674 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
8675 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 8676 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
8677 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
8678 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
8679 | } |
8680 | ||
c980d109 | 8681 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
267fb273 | 8682 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8683 | mmap_event->event_id.header.size); |
0a4a9391 | 8684 | if (ret) |
c980d109 | 8685 | goto out; |
0a4a9391 | 8686 | |
cdd6c482 IM |
8687 | mmap_event->event_id.pid = perf_event_pid(event, current); |
8688 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 8689 | |
88a16a13 JO |
8690 | use_build_id = event->attr.build_id && mmap_event->build_id_size; |
8691 | ||
8692 | if (event->attr.mmap2 && use_build_id) | |
8693 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID; | |
8694 | ||
cdd6c482 | 8695 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
8696 | |
8697 | if (event->attr.mmap2) { | |
88a16a13 JO |
8698 | if (use_build_id) { |
8699 | u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 }; | |
8700 | ||
8701 | __output_copy(&handle, size, 4); | |
8702 | __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX); | |
8703 | } else { | |
8704 | perf_output_put(&handle, mmap_event->maj); | |
8705 | perf_output_put(&handle, mmap_event->min); | |
8706 | perf_output_put(&handle, mmap_event->ino); | |
8707 | perf_output_put(&handle, mmap_event->ino_generation); | |
8708 | } | |
f972eb63 PZ |
8709 | perf_output_put(&handle, mmap_event->prot); |
8710 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
8711 | } |
8712 | ||
76369139 | 8713 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 8714 | mmap_event->file_size); |
c980d109 ACM |
8715 | |
8716 | perf_event__output_id_sample(event, &handle, &sample); | |
8717 | ||
78d613eb | 8718 | perf_output_end(&handle); |
c980d109 ACM |
8719 | out: |
8720 | mmap_event->event_id.header.size = size; | |
d9c1bb2f | 8721 | mmap_event->event_id.header.type = type; |
0a4a9391 PZ |
8722 | } |
8723 | ||
cdd6c482 | 8724 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 8725 | { |
089dd79d PZ |
8726 | struct vm_area_struct *vma = mmap_event->vma; |
8727 | struct file *file = vma->vm_file; | |
13d7a241 SE |
8728 | int maj = 0, min = 0; |
8729 | u64 ino = 0, gen = 0; | |
f972eb63 | 8730 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
8731 | unsigned int size; |
8732 | char tmp[16]; | |
8733 | char *buf = NULL; | |
549f5c77 | 8734 | char *name = NULL; |
413ee3b4 | 8735 | |
0b3589be PZ |
8736 | if (vma->vm_flags & VM_READ) |
8737 | prot |= PROT_READ; | |
8738 | if (vma->vm_flags & VM_WRITE) | |
8739 | prot |= PROT_WRITE; | |
8740 | if (vma->vm_flags & VM_EXEC) | |
8741 | prot |= PROT_EXEC; | |
8742 | ||
8743 | if (vma->vm_flags & VM_MAYSHARE) | |
8744 | flags = MAP_SHARED; | |
8745 | else | |
8746 | flags = MAP_PRIVATE; | |
8747 | ||
0b3589be PZ |
8748 | if (vma->vm_flags & VM_LOCKED) |
8749 | flags |= MAP_LOCKED; | |
03911132 | 8750 | if (is_vm_hugetlb_page(vma)) |
0b3589be PZ |
8751 | flags |= MAP_HUGETLB; |
8752 | ||
0a4a9391 | 8753 | if (file) { |
13d7a241 SE |
8754 | struct inode *inode; |
8755 | dev_t dev; | |
3ea2f2b9 | 8756 | |
2c42cfbf | 8757 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 8758 | if (!buf) { |
c7e548b4 ON |
8759 | name = "//enomem"; |
8760 | goto cpy_name; | |
0a4a9391 | 8761 | } |
413ee3b4 | 8762 | /* |
3ea2f2b9 | 8763 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
8764 | * need to add enough zero bytes after the string to handle |
8765 | * the 64bit alignment we do later. | |
8766 | */ | |
9bf39ab2 | 8767 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 8768 | if (IS_ERR(name)) { |
c7e548b4 ON |
8769 | name = "//toolong"; |
8770 | goto cpy_name; | |
0a4a9391 | 8771 | } |
13d7a241 SE |
8772 | inode = file_inode(vma->vm_file); |
8773 | dev = inode->i_sb->s_dev; | |
8774 | ino = inode->i_ino; | |
8775 | gen = inode->i_generation; | |
8776 | maj = MAJOR(dev); | |
8777 | min = MINOR(dev); | |
f972eb63 | 8778 | |
c7e548b4 | 8779 | goto got_name; |
0a4a9391 | 8780 | } else { |
549f5c77 | 8781 | if (vma->vm_ops && vma->vm_ops->name) |
fbe26abe | 8782 | name = (char *) vma->vm_ops->name(vma); |
549f5c77 KW |
8783 | if (!name) |
8784 | name = (char *)arch_vma_name(vma); | |
8785 | if (!name) { | |
8786 | if (vma_is_initial_heap(vma)) | |
8787 | name = "[heap]"; | |
8788 | else if (vma_is_initial_stack(vma)) | |
8789 | name = "[stack]"; | |
8790 | else | |
8791 | name = "//anon"; | |
fbe26abe | 8792 | } |
0a4a9391 PZ |
8793 | } |
8794 | ||
c7e548b4 | 8795 | cpy_name: |
c9732f14 | 8796 | strscpy(tmp, name, sizeof(tmp)); |
c7e548b4 | 8797 | name = tmp; |
0a4a9391 | 8798 | got_name: |
2c42cfbf PZ |
8799 | /* |
8800 | * Since our buffer works in 8 byte units we need to align our string | |
8801 | * size to a multiple of 8. However, we must guarantee the tail end is | |
8802 | * zero'd out to avoid leaking random bits to userspace. | |
8803 | */ | |
8804 | size = strlen(name)+1; | |
8805 | while (!IS_ALIGNED(size, sizeof(u64))) | |
8806 | name[size++] = '\0'; | |
0a4a9391 PZ |
8807 | |
8808 | mmap_event->file_name = name; | |
8809 | mmap_event->file_size = size; | |
13d7a241 SE |
8810 | mmap_event->maj = maj; |
8811 | mmap_event->min = min; | |
8812 | mmap_event->ino = ino; | |
8813 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
8814 | mmap_event->prot = prot; |
8815 | mmap_event->flags = flags; | |
0a4a9391 | 8816 | |
2fe85427 SE |
8817 | if (!(vma->vm_flags & VM_EXEC)) |
8818 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
8819 | ||
cdd6c482 | 8820 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 8821 | |
88a16a13 JO |
8822 | if (atomic_read(&nr_build_id_events)) |
8823 | build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size); | |
8824 | ||
aab5b71e | 8825 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
8826 | mmap_event, |
8827 | NULL); | |
665c2142 | 8828 | |
0a4a9391 PZ |
8829 | kfree(buf); |
8830 | } | |
8831 | ||
375637bc AS |
8832 | /* |
8833 | * Check whether inode and address range match filter criteria. | |
8834 | */ | |
8835 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
8836 | struct file *file, unsigned long offset, | |
8837 | unsigned long size) | |
8838 | { | |
7f635ff1 MP |
8839 | /* d_inode(NULL) won't be equal to any mapped user-space file */ |
8840 | if (!filter->path.dentry) | |
8841 | return false; | |
8842 | ||
9511bce9 | 8843 | if (d_inode(filter->path.dentry) != file_inode(file)) |
375637bc AS |
8844 | return false; |
8845 | ||
8846 | if (filter->offset > offset + size) | |
8847 | return false; | |
8848 | ||
8849 | if (filter->offset + filter->size < offset) | |
8850 | return false; | |
8851 | ||
8852 | return true; | |
8853 | } | |
8854 | ||
c60f83b8 AS |
8855 | static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter, |
8856 | struct vm_area_struct *vma, | |
8857 | struct perf_addr_filter_range *fr) | |
8858 | { | |
8859 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8860 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8861 | struct file *file = vma->vm_file; | |
8862 | ||
8863 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8864 | return false; | |
8865 | ||
8866 | if (filter->offset < off) { | |
8867 | fr->start = vma->vm_start; | |
8868 | fr->size = min(vma_size, filter->size - (off - filter->offset)); | |
8869 | } else { | |
8870 | fr->start = vma->vm_start + filter->offset - off; | |
8871 | fr->size = min(vma->vm_end - fr->start, filter->size); | |
8872 | } | |
8873 | ||
8874 | return true; | |
8875 | } | |
8876 | ||
375637bc AS |
8877 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
8878 | { | |
8879 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8880 | struct vm_area_struct *vma = data; | |
375637bc AS |
8881 | struct perf_addr_filter *filter; |
8882 | unsigned int restart = 0, count = 0; | |
c60f83b8 | 8883 | unsigned long flags; |
375637bc AS |
8884 | |
8885 | if (!has_addr_filter(event)) | |
8886 | return; | |
8887 | ||
c60f83b8 | 8888 | if (!vma->vm_file) |
375637bc AS |
8889 | return; |
8890 | ||
8891 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8892 | list_for_each_entry(filter, &ifh->list, entry) { | |
c60f83b8 AS |
8893 | if (perf_addr_filter_vma_adjust(filter, vma, |
8894 | &event->addr_filter_ranges[count])) | |
375637bc | 8895 | restart++; |
375637bc AS |
8896 | |
8897 | count++; | |
8898 | } | |
8899 | ||
8900 | if (restart) | |
8901 | event->addr_filters_gen++; | |
8902 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8903 | ||
8904 | if (restart) | |
767ae086 | 8905 | perf_event_stop(event, 1); |
375637bc AS |
8906 | } |
8907 | ||
8908 | /* | |
8909 | * Adjust all task's events' filters to the new vma | |
8910 | */ | |
8911 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
8912 | { | |
8913 | struct perf_event_context *ctx; | |
375637bc | 8914 | |
12b40a23 MP |
8915 | /* |
8916 | * Data tracing isn't supported yet and as such there is no need | |
8917 | * to keep track of anything that isn't related to executable code: | |
8918 | */ | |
8919 | if (!(vma->vm_flags & VM_EXEC)) | |
8920 | return; | |
8921 | ||
375637bc | 8922 | rcu_read_lock(); |
bd275681 PZ |
8923 | ctx = rcu_dereference(current->perf_event_ctxp); |
8924 | if (ctx) | |
aab5b71e | 8925 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
8926 | rcu_read_unlock(); |
8927 | } | |
8928 | ||
3af9e859 | 8929 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 8930 | { |
9ee318a7 PZ |
8931 | struct perf_mmap_event mmap_event; |
8932 | ||
cdd6c482 | 8933 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
8934 | return; |
8935 | ||
8936 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 8937 | .vma = vma, |
573402db PZ |
8938 | /* .file_name */ |
8939 | /* .file_size */ | |
cdd6c482 | 8940 | .event_id = { |
573402db | 8941 | .header = { |
cdd6c482 | 8942 | .type = PERF_RECORD_MMAP, |
39447b38 | 8943 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
8944 | /* .size */ |
8945 | }, | |
8946 | /* .pid */ | |
8947 | /* .tid */ | |
089dd79d PZ |
8948 | .start = vma->vm_start, |
8949 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 8950 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 8951 | }, |
13d7a241 SE |
8952 | /* .maj (attr_mmap2 only) */ |
8953 | /* .min (attr_mmap2 only) */ | |
8954 | /* .ino (attr_mmap2 only) */ | |
8955 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
8956 | /* .prot (attr_mmap2 only) */ |
8957 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
8958 | }; |
8959 | ||
375637bc | 8960 | perf_addr_filters_adjust(vma); |
cdd6c482 | 8961 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
8962 | } |
8963 | ||
68db7e98 AS |
8964 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
8965 | unsigned long size, u64 flags) | |
8966 | { | |
8967 | struct perf_output_handle handle; | |
8968 | struct perf_sample_data sample; | |
8969 | struct perf_aux_event { | |
8970 | struct perf_event_header header; | |
8971 | u64 offset; | |
8972 | u64 size; | |
8973 | u64 flags; | |
8974 | } rec = { | |
8975 | .header = { | |
8976 | .type = PERF_RECORD_AUX, | |
8977 | .misc = 0, | |
8978 | .size = sizeof(rec), | |
8979 | }, | |
8980 | .offset = head, | |
8981 | .size = size, | |
8982 | .flags = flags, | |
8983 | }; | |
8984 | int ret; | |
8985 | ||
8986 | perf_event_header__init_id(&rec.header, &sample, event); | |
267fb273 | 8987 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); |
68db7e98 AS |
8988 | |
8989 | if (ret) | |
8990 | return; | |
8991 | ||
8992 | perf_output_put(&handle, rec); | |
8993 | perf_event__output_id_sample(event, &handle, &sample); | |
8994 | ||
8995 | perf_output_end(&handle); | |
8996 | } | |
8997 | ||
f38b0dbb KL |
8998 | /* |
8999 | * Lost/dropped samples logging | |
9000 | */ | |
9001 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
9002 | { | |
9003 | struct perf_output_handle handle; | |
9004 | struct perf_sample_data sample; | |
9005 | int ret; | |
9006 | ||
9007 | struct { | |
9008 | struct perf_event_header header; | |
9009 | u64 lost; | |
9010 | } lost_samples_event = { | |
9011 | .header = { | |
9012 | .type = PERF_RECORD_LOST_SAMPLES, | |
9013 | .misc = 0, | |
9014 | .size = sizeof(lost_samples_event), | |
9015 | }, | |
9016 | .lost = lost, | |
9017 | }; | |
9018 | ||
9019 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
9020 | ||
267fb273 | 9021 | ret = perf_output_begin(&handle, &sample, event, |
f38b0dbb KL |
9022 | lost_samples_event.header.size); |
9023 | if (ret) | |
9024 | return; | |
9025 | ||
9026 | perf_output_put(&handle, lost_samples_event); | |
9027 | perf_event__output_id_sample(event, &handle, &sample); | |
9028 | perf_output_end(&handle); | |
9029 | } | |
9030 | ||
45ac1403 AH |
9031 | /* |
9032 | * context_switch tracking | |
9033 | */ | |
9034 | ||
9035 | struct perf_switch_event { | |
9036 | struct task_struct *task; | |
9037 | struct task_struct *next_prev; | |
9038 | ||
9039 | struct { | |
9040 | struct perf_event_header header; | |
9041 | u32 next_prev_pid; | |
9042 | u32 next_prev_tid; | |
9043 | } event_id; | |
9044 | }; | |
9045 | ||
9046 | static int perf_event_switch_match(struct perf_event *event) | |
9047 | { | |
9048 | return event->attr.context_switch; | |
9049 | } | |
9050 | ||
9051 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
9052 | { | |
9053 | struct perf_switch_event *se = data; | |
9054 | struct perf_output_handle handle; | |
9055 | struct perf_sample_data sample; | |
9056 | int ret; | |
9057 | ||
9058 | if (!perf_event_switch_match(event)) | |
9059 | return; | |
9060 | ||
9061 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
9062 | if (event->ctx->task) { | |
9063 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
9064 | se->event_id.header.size = sizeof(se->event_id.header); | |
9065 | } else { | |
9066 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
9067 | se->event_id.header.size = sizeof(se->event_id); | |
9068 | se->event_id.next_prev_pid = | |
9069 | perf_event_pid(event, se->next_prev); | |
9070 | se->event_id.next_prev_tid = | |
9071 | perf_event_tid(event, se->next_prev); | |
9072 | } | |
9073 | ||
9074 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
9075 | ||
267fb273 | 9076 | ret = perf_output_begin(&handle, &sample, event, se->event_id.header.size); |
45ac1403 AH |
9077 | if (ret) |
9078 | return; | |
9079 | ||
9080 | if (event->ctx->task) | |
9081 | perf_output_put(&handle, se->event_id.header); | |
9082 | else | |
9083 | perf_output_put(&handle, se->event_id); | |
9084 | ||
9085 | perf_event__output_id_sample(event, &handle, &sample); | |
9086 | ||
9087 | perf_output_end(&handle); | |
9088 | } | |
9089 | ||
9090 | static void perf_event_switch(struct task_struct *task, | |
9091 | struct task_struct *next_prev, bool sched_in) | |
9092 | { | |
9093 | struct perf_switch_event switch_event; | |
9094 | ||
9095 | /* N.B. caller checks nr_switch_events != 0 */ | |
9096 | ||
9097 | switch_event = (struct perf_switch_event){ | |
9098 | .task = task, | |
9099 | .next_prev = next_prev, | |
9100 | .event_id = { | |
9101 | .header = { | |
9102 | /* .type */ | |
9103 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
9104 | /* .size */ | |
9105 | }, | |
9106 | /* .next_prev_pid */ | |
9107 | /* .next_prev_tid */ | |
9108 | }, | |
9109 | }; | |
9110 | ||
3ba9f93b | 9111 | if (!sched_in && task->on_rq) { |
101592b4 AB |
9112 | switch_event.event_id.header.misc |= |
9113 | PERF_RECORD_MISC_SWITCH_OUT_PREEMPT; | |
3ba9f93b | 9114 | } |
101592b4 | 9115 | |
3ba9f93b | 9116 | perf_iterate_sb(perf_event_switch_output, &switch_event, NULL); |
45ac1403 AH |
9117 | } |
9118 | ||
a78ac325 PZ |
9119 | /* |
9120 | * IRQ throttle logging | |
9121 | */ | |
9122 | ||
cdd6c482 | 9123 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
9124 | { |
9125 | struct perf_output_handle handle; | |
c980d109 | 9126 | struct perf_sample_data sample; |
a78ac325 PZ |
9127 | int ret; |
9128 | ||
9129 | struct { | |
9130 | struct perf_event_header header; | |
9131 | u64 time; | |
cca3f454 | 9132 | u64 id; |
7f453c24 | 9133 | u64 stream_id; |
a78ac325 PZ |
9134 | } throttle_event = { |
9135 | .header = { | |
cdd6c482 | 9136 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
9137 | .misc = 0, |
9138 | .size = sizeof(throttle_event), | |
9139 | }, | |
34f43927 | 9140 | .time = perf_event_clock(event), |
cdd6c482 IM |
9141 | .id = primary_event_id(event), |
9142 | .stream_id = event->id, | |
a78ac325 PZ |
9143 | }; |
9144 | ||
966ee4d6 | 9145 | if (enable) |
cdd6c482 | 9146 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 9147 | |
c980d109 ACM |
9148 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
9149 | ||
267fb273 | 9150 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 9151 | throttle_event.header.size); |
a78ac325 PZ |
9152 | if (ret) |
9153 | return; | |
9154 | ||
9155 | perf_output_put(&handle, throttle_event); | |
c980d109 | 9156 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
9157 | perf_output_end(&handle); |
9158 | } | |
9159 | ||
76193a94 SL |
9160 | /* |
9161 | * ksymbol register/unregister tracking | |
9162 | */ | |
9163 | ||
9164 | struct perf_ksymbol_event { | |
9165 | const char *name; | |
9166 | int name_len; | |
9167 | struct { | |
9168 | struct perf_event_header header; | |
9169 | u64 addr; | |
9170 | u32 len; | |
9171 | u16 ksym_type; | |
9172 | u16 flags; | |
9173 | } event_id; | |
9174 | }; | |
9175 | ||
9176 | static int perf_event_ksymbol_match(struct perf_event *event) | |
9177 | { | |
9178 | return event->attr.ksymbol; | |
9179 | } | |
9180 | ||
9181 | static void perf_event_ksymbol_output(struct perf_event *event, void *data) | |
9182 | { | |
9183 | struct perf_ksymbol_event *ksymbol_event = data; | |
9184 | struct perf_output_handle handle; | |
9185 | struct perf_sample_data sample; | |
9186 | int ret; | |
9187 | ||
9188 | if (!perf_event_ksymbol_match(event)) | |
9189 | return; | |
9190 | ||
9191 | perf_event_header__init_id(&ksymbol_event->event_id.header, | |
9192 | &sample, event); | |
267fb273 | 9193 | ret = perf_output_begin(&handle, &sample, event, |
76193a94 SL |
9194 | ksymbol_event->event_id.header.size); |
9195 | if (ret) | |
9196 | return; | |
9197 | ||
9198 | perf_output_put(&handle, ksymbol_event->event_id); | |
9199 | __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len); | |
9200 | perf_event__output_id_sample(event, &handle, &sample); | |
9201 | ||
9202 | perf_output_end(&handle); | |
9203 | } | |
9204 | ||
9205 | void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister, | |
9206 | const char *sym) | |
9207 | { | |
9208 | struct perf_ksymbol_event ksymbol_event; | |
9209 | char name[KSYM_NAME_LEN]; | |
9210 | u16 flags = 0; | |
9211 | int name_len; | |
9212 | ||
9213 | if (!atomic_read(&nr_ksymbol_events)) | |
9214 | return; | |
9215 | ||
9216 | if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX || | |
9217 | ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN) | |
9218 | goto err; | |
9219 | ||
c9732f14 | 9220 | strscpy(name, sym, KSYM_NAME_LEN); |
76193a94 SL |
9221 | name_len = strlen(name) + 1; |
9222 | while (!IS_ALIGNED(name_len, sizeof(u64))) | |
9223 | name[name_len++] = '\0'; | |
9224 | BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64)); | |
9225 | ||
9226 | if (unregister) | |
9227 | flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER; | |
9228 | ||
9229 | ksymbol_event = (struct perf_ksymbol_event){ | |
9230 | .name = name, | |
9231 | .name_len = name_len, | |
9232 | .event_id = { | |
9233 | .header = { | |
9234 | .type = PERF_RECORD_KSYMBOL, | |
9235 | .size = sizeof(ksymbol_event.event_id) + | |
9236 | name_len, | |
9237 | }, | |
9238 | .addr = addr, | |
9239 | .len = len, | |
9240 | .ksym_type = ksym_type, | |
9241 | .flags = flags, | |
9242 | }, | |
9243 | }; | |
9244 | ||
9245 | perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL); | |
9246 | return; | |
9247 | err: | |
9248 | WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type); | |
9249 | } | |
9250 | ||
6ee52e2a SL |
9251 | /* |
9252 | * bpf program load/unload tracking | |
9253 | */ | |
9254 | ||
9255 | struct perf_bpf_event { | |
9256 | struct bpf_prog *prog; | |
9257 | struct { | |
9258 | struct perf_event_header header; | |
9259 | u16 type; | |
9260 | u16 flags; | |
9261 | u32 id; | |
9262 | u8 tag[BPF_TAG_SIZE]; | |
9263 | } event_id; | |
9264 | }; | |
9265 | ||
9266 | static int perf_event_bpf_match(struct perf_event *event) | |
9267 | { | |
9268 | return event->attr.bpf_event; | |
9269 | } | |
9270 | ||
9271 | static void perf_event_bpf_output(struct perf_event *event, void *data) | |
9272 | { | |
9273 | struct perf_bpf_event *bpf_event = data; | |
9274 | struct perf_output_handle handle; | |
9275 | struct perf_sample_data sample; | |
9276 | int ret; | |
9277 | ||
9278 | if (!perf_event_bpf_match(event)) | |
9279 | return; | |
9280 | ||
9281 | perf_event_header__init_id(&bpf_event->event_id.header, | |
9282 | &sample, event); | |
eb81a2ed | 9283 | ret = perf_output_begin(&handle, &sample, event, |
6ee52e2a SL |
9284 | bpf_event->event_id.header.size); |
9285 | if (ret) | |
9286 | return; | |
9287 | ||
9288 | perf_output_put(&handle, bpf_event->event_id); | |
9289 | perf_event__output_id_sample(event, &handle, &sample); | |
9290 | ||
9291 | perf_output_end(&handle); | |
9292 | } | |
9293 | ||
9294 | static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog, | |
9295 | enum perf_bpf_event_type type) | |
9296 | { | |
9297 | bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD; | |
6ee52e2a SL |
9298 | int i; |
9299 | ||
9300 | if (prog->aux->func_cnt == 0) { | |
6ee52e2a SL |
9301 | perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, |
9302 | (u64)(unsigned long)prog->bpf_func, | |
bfea9a85 JO |
9303 | prog->jited_len, unregister, |
9304 | prog->aux->ksym.name); | |
6ee52e2a SL |
9305 | } else { |
9306 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
9307 | struct bpf_prog *subprog = prog->aux->func[i]; | |
9308 | ||
6ee52e2a SL |
9309 | perf_event_ksymbol( |
9310 | PERF_RECORD_KSYMBOL_TYPE_BPF, | |
9311 | (u64)(unsigned long)subprog->bpf_func, | |
bfea9a85 | 9312 | subprog->jited_len, unregister, |
47df8a2f | 9313 | subprog->aux->ksym.name); |
6ee52e2a SL |
9314 | } |
9315 | } | |
9316 | } | |
9317 | ||
9318 | void perf_event_bpf_event(struct bpf_prog *prog, | |
9319 | enum perf_bpf_event_type type, | |
9320 | u16 flags) | |
9321 | { | |
9322 | struct perf_bpf_event bpf_event; | |
9323 | ||
6ee52e2a SL |
9324 | switch (type) { |
9325 | case PERF_BPF_EVENT_PROG_LOAD: | |
9326 | case PERF_BPF_EVENT_PROG_UNLOAD: | |
9327 | if (atomic_read(&nr_ksymbol_events)) | |
9328 | perf_event_bpf_emit_ksymbols(prog, type); | |
9329 | break; | |
9330 | default: | |
aecaa3ed | 9331 | return; |
6ee52e2a SL |
9332 | } |
9333 | ||
9334 | if (!atomic_read(&nr_bpf_events)) | |
9335 | return; | |
9336 | ||
9337 | bpf_event = (struct perf_bpf_event){ | |
9338 | .prog = prog, | |
9339 | .event_id = { | |
9340 | .header = { | |
9341 | .type = PERF_RECORD_BPF_EVENT, | |
9342 | .size = sizeof(bpf_event.event_id), | |
9343 | }, | |
9344 | .type = type, | |
9345 | .flags = flags, | |
9346 | .id = prog->aux->id, | |
9347 | }, | |
9348 | }; | |
9349 | ||
9350 | BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64)); | |
9351 | ||
9352 | memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE); | |
9353 | perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL); | |
9354 | } | |
9355 | ||
e17d43b9 AH |
9356 | struct perf_text_poke_event { |
9357 | const void *old_bytes; | |
9358 | const void *new_bytes; | |
9359 | size_t pad; | |
9360 | u16 old_len; | |
9361 | u16 new_len; | |
9362 | ||
9363 | struct { | |
9364 | struct perf_event_header header; | |
9365 | ||
9366 | u64 addr; | |
9367 | } event_id; | |
9368 | }; | |
9369 | ||
9370 | static int perf_event_text_poke_match(struct perf_event *event) | |
9371 | { | |
9372 | return event->attr.text_poke; | |
9373 | } | |
9374 | ||
9375 | static void perf_event_text_poke_output(struct perf_event *event, void *data) | |
9376 | { | |
9377 | struct perf_text_poke_event *text_poke_event = data; | |
9378 | struct perf_output_handle handle; | |
9379 | struct perf_sample_data sample; | |
9380 | u64 padding = 0; | |
9381 | int ret; | |
9382 | ||
9383 | if (!perf_event_text_poke_match(event)) | |
9384 | return; | |
9385 | ||
9386 | perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event); | |
9387 | ||
267fb273 PZ |
9388 | ret = perf_output_begin(&handle, &sample, event, |
9389 | text_poke_event->event_id.header.size); | |
e17d43b9 AH |
9390 | if (ret) |
9391 | return; | |
9392 | ||
9393 | perf_output_put(&handle, text_poke_event->event_id); | |
9394 | perf_output_put(&handle, text_poke_event->old_len); | |
9395 | perf_output_put(&handle, text_poke_event->new_len); | |
9396 | ||
9397 | __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len); | |
9398 | __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len); | |
9399 | ||
9400 | if (text_poke_event->pad) | |
9401 | __output_copy(&handle, &padding, text_poke_event->pad); | |
9402 | ||
9403 | perf_event__output_id_sample(event, &handle, &sample); | |
9404 | ||
9405 | perf_output_end(&handle); | |
9406 | } | |
9407 | ||
9408 | void perf_event_text_poke(const void *addr, const void *old_bytes, | |
9409 | size_t old_len, const void *new_bytes, size_t new_len) | |
9410 | { | |
9411 | struct perf_text_poke_event text_poke_event; | |
9412 | size_t tot, pad; | |
9413 | ||
9414 | if (!atomic_read(&nr_text_poke_events)) | |
9415 | return; | |
9416 | ||
9417 | tot = sizeof(text_poke_event.old_len) + old_len; | |
9418 | tot += sizeof(text_poke_event.new_len) + new_len; | |
9419 | pad = ALIGN(tot, sizeof(u64)) - tot; | |
9420 | ||
9421 | text_poke_event = (struct perf_text_poke_event){ | |
9422 | .old_bytes = old_bytes, | |
9423 | .new_bytes = new_bytes, | |
9424 | .pad = pad, | |
9425 | .old_len = old_len, | |
9426 | .new_len = new_len, | |
9427 | .event_id = { | |
9428 | .header = { | |
9429 | .type = PERF_RECORD_TEXT_POKE, | |
9430 | .misc = PERF_RECORD_MISC_KERNEL, | |
9431 | .size = sizeof(text_poke_event.event_id) + tot + pad, | |
9432 | }, | |
9433 | .addr = (unsigned long)addr, | |
9434 | }, | |
9435 | }; | |
9436 | ||
9437 | perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL); | |
9438 | } | |
9439 | ||
8d4e6c4c AS |
9440 | void perf_event_itrace_started(struct perf_event *event) |
9441 | { | |
9442 | event->attach_state |= PERF_ATTACH_ITRACE; | |
9443 | } | |
9444 | ||
ec0d7729 AS |
9445 | static void perf_log_itrace_start(struct perf_event *event) |
9446 | { | |
9447 | struct perf_output_handle handle; | |
9448 | struct perf_sample_data sample; | |
9449 | struct perf_aux_event { | |
9450 | struct perf_event_header header; | |
9451 | u32 pid; | |
9452 | u32 tid; | |
9453 | } rec; | |
9454 | int ret; | |
9455 | ||
9456 | if (event->parent) | |
9457 | event = event->parent; | |
9458 | ||
9459 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
8d4e6c4c | 9460 | event->attach_state & PERF_ATTACH_ITRACE) |
ec0d7729 AS |
9461 | return; |
9462 | ||
ec0d7729 AS |
9463 | rec.header.type = PERF_RECORD_ITRACE_START; |
9464 | rec.header.misc = 0; | |
9465 | rec.header.size = sizeof(rec); | |
9466 | rec.pid = perf_event_pid(event, current); | |
9467 | rec.tid = perf_event_tid(event, current); | |
9468 | ||
9469 | perf_event_header__init_id(&rec.header, &sample, event); | |
267fb273 | 9470 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); |
ec0d7729 AS |
9471 | |
9472 | if (ret) | |
9473 | return; | |
9474 | ||
9475 | perf_output_put(&handle, rec); | |
9476 | perf_event__output_id_sample(event, &handle, &sample); | |
9477 | ||
9478 | perf_output_end(&handle); | |
9479 | } | |
9480 | ||
8b8ff8cc AH |
9481 | void perf_report_aux_output_id(struct perf_event *event, u64 hw_id) |
9482 | { | |
9483 | struct perf_output_handle handle; | |
9484 | struct perf_sample_data sample; | |
9485 | struct perf_aux_event { | |
9486 | struct perf_event_header header; | |
9487 | u64 hw_id; | |
9488 | } rec; | |
9489 | int ret; | |
9490 | ||
9491 | if (event->parent) | |
9492 | event = event->parent; | |
9493 | ||
9494 | rec.header.type = PERF_RECORD_AUX_OUTPUT_HW_ID; | |
9495 | rec.header.misc = 0; | |
9496 | rec.header.size = sizeof(rec); | |
9497 | rec.hw_id = hw_id; | |
9498 | ||
9499 | perf_event_header__init_id(&rec.header, &sample, event); | |
9500 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); | |
9501 | ||
9502 | if (ret) | |
9503 | return; | |
9504 | ||
9505 | perf_output_put(&handle, rec); | |
9506 | perf_event__output_id_sample(event, &handle, &sample); | |
9507 | ||
9508 | perf_output_end(&handle); | |
9509 | } | |
7d30d480 | 9510 | EXPORT_SYMBOL_GPL(perf_report_aux_output_id); |
8b8ff8cc | 9511 | |
475113d9 JO |
9512 | static int |
9513 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 9514 | { |
cdd6c482 | 9515 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 9516 | int ret = 0; |
475113d9 | 9517 | u64 seq; |
96398826 | 9518 | |
e050e3f0 SE |
9519 | seq = __this_cpu_read(perf_throttled_seq); |
9520 | if (seq != hwc->interrupts_seq) { | |
9521 | hwc->interrupts_seq = seq; | |
9522 | hwc->interrupts = 1; | |
9523 | } else { | |
9524 | hwc->interrupts++; | |
15def34e YJ |
9525 | if (unlikely(throttle && |
9526 | hwc->interrupts > max_samples_per_tick)) { | |
e050e3f0 | 9527 | __this_cpu_inc(perf_throttled_count); |
555e0c1e | 9528 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
9529 | hwc->interrupts = MAX_INTERRUPTS; |
9530 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
9531 | ret = 1; |
9532 | } | |
e050e3f0 | 9533 | } |
60db5e09 | 9534 | |
cdd6c482 | 9535 | if (event->attr.freq) { |
def0a9b2 | 9536 | u64 now = perf_clock(); |
abd50713 | 9537 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 9538 | |
abd50713 | 9539 | hwc->freq_time_stamp = now; |
bd2b5b12 | 9540 | |
abd50713 | 9541 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 9542 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
9543 | } |
9544 | ||
475113d9 JO |
9545 | return ret; |
9546 | } | |
9547 | ||
9548 | int perf_event_account_interrupt(struct perf_event *event) | |
9549 | { | |
9550 | return __perf_event_account_interrupt(event, 1); | |
9551 | } | |
9552 | ||
030a976e PZ |
9553 | static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs) |
9554 | { | |
9555 | /* | |
9556 | * Due to interrupt latency (AKA "skid"), we may enter the | |
9557 | * kernel before taking an overflow, even if the PMU is only | |
9558 | * counting user events. | |
9559 | */ | |
9560 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
9561 | return false; | |
9562 | ||
9563 | return true; | |
9564 | } | |
9565 | ||
475113d9 JO |
9566 | /* |
9567 | * Generic event overflow handling, sampling. | |
9568 | */ | |
9569 | ||
9570 | static int __perf_event_overflow(struct perf_event *event, | |
ca6c2132 PZ |
9571 | int throttle, struct perf_sample_data *data, |
9572 | struct pt_regs *regs) | |
475113d9 JO |
9573 | { |
9574 | int events = atomic_read(&event->event_limit); | |
9575 | int ret = 0; | |
9576 | ||
9577 | /* | |
9578 | * Non-sampling counters might still use the PMI to fold short | |
9579 | * hardware counters, ignore those. | |
9580 | */ | |
9581 | if (unlikely(!is_sampling_event(event))) | |
9582 | return 0; | |
9583 | ||
9584 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 9585 | |
2023b359 PZ |
9586 | /* |
9587 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 9588 | * events |
2023b359 PZ |
9589 | */ |
9590 | ||
cdd6c482 IM |
9591 | event->pending_kill = POLL_IN; |
9592 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 9593 | ret = 1; |
cdd6c482 | 9594 | event->pending_kill = POLL_HUP; |
5aab90ce | 9595 | perf_event_disable_inatomic(event); |
79f14641 PZ |
9596 | } |
9597 | ||
ca6c2132 | 9598 | if (event->attr.sigtrap) { |
030a976e PZ |
9599 | /* |
9600 | * The desired behaviour of sigtrap vs invalid samples is a bit | |
9601 | * tricky; on the one hand, one should not loose the SIGTRAP if | |
9602 | * it is the first event, on the other hand, we should also not | |
9603 | * trigger the WARN or override the data address. | |
9604 | */ | |
9605 | bool valid_sample = sample_is_allowed(event, regs); | |
bb88f969 ME |
9606 | unsigned int pending_id = 1; |
9607 | ||
9608 | if (regs) | |
9609 | pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1; | |
ca6c2132 | 9610 | if (!event->pending_sigtrap) { |
bb88f969 | 9611 | event->pending_sigtrap = pending_id; |
ca6c2132 | 9612 | local_inc(&event->ctx->nr_pending); |
030a976e | 9613 | } else if (event->attr.exclude_kernel && valid_sample) { |
bb88f969 ME |
9614 | /* |
9615 | * Should not be able to return to user space without | |
9616 | * consuming pending_sigtrap; with exceptions: | |
9617 | * | |
9618 | * 1. Where !exclude_kernel, events can overflow again | |
9619 | * in the kernel without returning to user space. | |
9620 | * | |
9621 | * 2. Events that can overflow again before the IRQ- | |
9622 | * work without user space progress (e.g. hrtimer). | |
9623 | * To approximate progress (with false negatives), | |
9624 | * check 32-bit hash of the current IP. | |
9625 | */ | |
9626 | WARN_ON_ONCE(event->pending_sigtrap != pending_id); | |
ca6c2132 | 9627 | } |
af169b77 PZ |
9628 | |
9629 | event->pending_addr = 0; | |
030a976e | 9630 | if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) |
af169b77 | 9631 | event->pending_addr = data->addr; |
ca6c2132 PZ |
9632 | irq_work_queue(&event->pending_irq); |
9633 | } | |
9634 | ||
aa6a5f3c | 9635 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 9636 | |
fed66e2c | 9637 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 | 9638 | event->pending_wakeup = 1; |
ca6c2132 | 9639 | irq_work_queue(&event->pending_irq); |
f506b3dc PZ |
9640 | } |
9641 | ||
79f14641 | 9642 | return ret; |
f6c7d5fe PZ |
9643 | } |
9644 | ||
a8b0ca17 | 9645 | int perf_event_overflow(struct perf_event *event, |
ca6c2132 PZ |
9646 | struct perf_sample_data *data, |
9647 | struct pt_regs *regs) | |
850bc73f | 9648 | { |
a8b0ca17 | 9649 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
9650 | } |
9651 | ||
15dbf27c | 9652 | /* |
cdd6c482 | 9653 | * Generic software event infrastructure |
15dbf27c PZ |
9654 | */ |
9655 | ||
b28ab83c PZ |
9656 | struct swevent_htable { |
9657 | struct swevent_hlist *swevent_hlist; | |
9658 | struct mutex hlist_mutex; | |
9659 | int hlist_refcount; | |
9660 | ||
9661 | /* Recursion avoidance in each contexts */ | |
9662 | int recursion[PERF_NR_CONTEXTS]; | |
9663 | }; | |
9664 | ||
9665 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
9666 | ||
7b4b6658 | 9667 | /* |
cdd6c482 IM |
9668 | * We directly increment event->count and keep a second value in |
9669 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
9670 | * is kept in the range [-sample_period, 0] so that we can use the |
9671 | * sign as trigger. | |
9672 | */ | |
9673 | ||
ab573844 | 9674 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 9675 | { |
cdd6c482 | 9676 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
9677 | u64 period = hwc->last_period; |
9678 | u64 nr, offset; | |
9679 | s64 old, val; | |
9680 | ||
9681 | hwc->last_period = hwc->sample_period; | |
15dbf27c | 9682 | |
28fd85a1 UB |
9683 | old = local64_read(&hwc->period_left); |
9684 | do { | |
9685 | val = old; | |
9686 | if (val < 0) | |
9687 | return 0; | |
15dbf27c | 9688 | |
28fd85a1 UB |
9689 | nr = div64_u64(period + val, period); |
9690 | offset = nr * period; | |
9691 | val -= offset; | |
9692 | } while (!local64_try_cmpxchg(&hwc->period_left, &old, val)); | |
15dbf27c | 9693 | |
7b4b6658 | 9694 | return nr; |
15dbf27c PZ |
9695 | } |
9696 | ||
0cff784a | 9697 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 9698 | struct perf_sample_data *data, |
5622f295 | 9699 | struct pt_regs *regs) |
15dbf27c | 9700 | { |
cdd6c482 | 9701 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 9702 | int throttle = 0; |
15dbf27c | 9703 | |
0cff784a PZ |
9704 | if (!overflow) |
9705 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 9706 | |
7b4b6658 PZ |
9707 | if (hwc->interrupts == MAX_INTERRUPTS) |
9708 | return; | |
15dbf27c | 9709 | |
7b4b6658 | 9710 | for (; overflow; overflow--) { |
a8b0ca17 | 9711 | if (__perf_event_overflow(event, throttle, |
5622f295 | 9712 | data, regs)) { |
7b4b6658 PZ |
9713 | /* |
9714 | * We inhibit the overflow from happening when | |
9715 | * hwc->interrupts == MAX_INTERRUPTS. | |
9716 | */ | |
9717 | break; | |
9718 | } | |
cf450a73 | 9719 | throttle = 1; |
7b4b6658 | 9720 | } |
15dbf27c PZ |
9721 | } |
9722 | ||
a4eaf7f1 | 9723 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 9724 | struct perf_sample_data *data, |
5622f295 | 9725 | struct pt_regs *regs) |
7b4b6658 | 9726 | { |
cdd6c482 | 9727 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 9728 | |
e7850595 | 9729 | local64_add(nr, &event->count); |
d6d020e9 | 9730 | |
0cff784a PZ |
9731 | if (!regs) |
9732 | return; | |
9733 | ||
6c7e550f | 9734 | if (!is_sampling_event(event)) |
7b4b6658 | 9735 | return; |
d6d020e9 | 9736 | |
5d81e5cf AV |
9737 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
9738 | data->period = nr; | |
9739 | return perf_swevent_overflow(event, 1, data, regs); | |
9740 | } else | |
9741 | data->period = event->hw.last_period; | |
9742 | ||
0cff784a | 9743 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 9744 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 9745 | |
e7850595 | 9746 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 9747 | return; |
df1a132b | 9748 | |
a8b0ca17 | 9749 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
9750 | } |
9751 | ||
f5ffe02e FW |
9752 | static int perf_exclude_event(struct perf_event *event, |
9753 | struct pt_regs *regs) | |
9754 | { | |
a4eaf7f1 | 9755 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 9756 | return 1; |
a4eaf7f1 | 9757 | |
f5ffe02e FW |
9758 | if (regs) { |
9759 | if (event->attr.exclude_user && user_mode(regs)) | |
9760 | return 1; | |
9761 | ||
9762 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
9763 | return 1; | |
9764 | } | |
9765 | ||
9766 | return 0; | |
9767 | } | |
9768 | ||
cdd6c482 | 9769 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 9770 | enum perf_type_id type, |
6fb2915d LZ |
9771 | u32 event_id, |
9772 | struct perf_sample_data *data, | |
9773 | struct pt_regs *regs) | |
15dbf27c | 9774 | { |
cdd6c482 | 9775 | if (event->attr.type != type) |
a21ca2ca | 9776 | return 0; |
f5ffe02e | 9777 | |
cdd6c482 | 9778 | if (event->attr.config != event_id) |
15dbf27c PZ |
9779 | return 0; |
9780 | ||
f5ffe02e FW |
9781 | if (perf_exclude_event(event, regs)) |
9782 | return 0; | |
15dbf27c PZ |
9783 | |
9784 | return 1; | |
9785 | } | |
9786 | ||
76e1d904 FW |
9787 | static inline u64 swevent_hash(u64 type, u32 event_id) |
9788 | { | |
9789 | u64 val = event_id | (type << 32); | |
9790 | ||
9791 | return hash_64(val, SWEVENT_HLIST_BITS); | |
9792 | } | |
9793 | ||
49f135ed FW |
9794 | static inline struct hlist_head * |
9795 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 9796 | { |
49f135ed FW |
9797 | u64 hash = swevent_hash(type, event_id); |
9798 | ||
9799 | return &hlist->heads[hash]; | |
9800 | } | |
76e1d904 | 9801 | |
49f135ed FW |
9802 | /* For the read side: events when they trigger */ |
9803 | static inline struct hlist_head * | |
b28ab83c | 9804 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
9805 | { |
9806 | struct swevent_hlist *hlist; | |
76e1d904 | 9807 | |
b28ab83c | 9808 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
9809 | if (!hlist) |
9810 | return NULL; | |
9811 | ||
49f135ed FW |
9812 | return __find_swevent_head(hlist, type, event_id); |
9813 | } | |
9814 | ||
9815 | /* For the event head insertion and removal in the hlist */ | |
9816 | static inline struct hlist_head * | |
b28ab83c | 9817 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
9818 | { |
9819 | struct swevent_hlist *hlist; | |
9820 | u32 event_id = event->attr.config; | |
9821 | u64 type = event->attr.type; | |
9822 | ||
9823 | /* | |
9824 | * Event scheduling is always serialized against hlist allocation | |
9825 | * and release. Which makes the protected version suitable here. | |
9826 | * The context lock guarantees that. | |
9827 | */ | |
b28ab83c | 9828 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
9829 | lockdep_is_held(&event->ctx->lock)); |
9830 | if (!hlist) | |
9831 | return NULL; | |
9832 | ||
9833 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
9834 | } |
9835 | ||
9836 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 9837 | u64 nr, |
76e1d904 FW |
9838 | struct perf_sample_data *data, |
9839 | struct pt_regs *regs) | |
15dbf27c | 9840 | { |
4a32fea9 | 9841 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9842 | struct perf_event *event; |
76e1d904 | 9843 | struct hlist_head *head; |
15dbf27c | 9844 | |
76e1d904 | 9845 | rcu_read_lock(); |
b28ab83c | 9846 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
9847 | if (!head) |
9848 | goto end; | |
9849 | ||
b67bfe0d | 9850 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 9851 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 9852 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 9853 | } |
76e1d904 FW |
9854 | end: |
9855 | rcu_read_unlock(); | |
15dbf27c PZ |
9856 | } |
9857 | ||
86038c5e PZI |
9858 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
9859 | ||
4ed7c92d | 9860 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 9861 | { |
4a32fea9 | 9862 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 9863 | |
b28ab83c | 9864 | return get_recursion_context(swhash->recursion); |
96f6d444 | 9865 | } |
645e8cc0 | 9866 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 9867 | |
98b5c2c6 | 9868 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 9869 | { |
4a32fea9 | 9870 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 9871 | |
b28ab83c | 9872 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 9873 | } |
15dbf27c | 9874 | |
86038c5e | 9875 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 9876 | { |
a4234bfc | 9877 | struct perf_sample_data data; |
4ed7c92d | 9878 | |
86038c5e | 9879 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 9880 | return; |
a4234bfc | 9881 | |
fd0d000b | 9882 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 9883 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
9884 | } |
9885 | ||
9886 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
9887 | { | |
9888 | int rctx; | |
9889 | ||
9890 | preempt_disable_notrace(); | |
9891 | rctx = perf_swevent_get_recursion_context(); | |
9892 | if (unlikely(rctx < 0)) | |
9893 | goto fail; | |
9894 | ||
9895 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
9896 | |
9897 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 9898 | fail: |
1c024eca | 9899 | preempt_enable_notrace(); |
b8e83514 PZ |
9900 | } |
9901 | ||
cdd6c482 | 9902 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 9903 | { |
15dbf27c PZ |
9904 | } |
9905 | ||
a4eaf7f1 | 9906 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 9907 | { |
4a32fea9 | 9908 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9909 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
9910 | struct hlist_head *head; |
9911 | ||
6c7e550f | 9912 | if (is_sampling_event(event)) { |
7b4b6658 | 9913 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 9914 | perf_swevent_set_period(event); |
7b4b6658 | 9915 | } |
76e1d904 | 9916 | |
a4eaf7f1 PZ |
9917 | hwc->state = !(flags & PERF_EF_START); |
9918 | ||
b28ab83c | 9919 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 9920 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
9921 | return -EINVAL; |
9922 | ||
9923 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 9924 | perf_event_update_userpage(event); |
76e1d904 | 9925 | |
15dbf27c PZ |
9926 | return 0; |
9927 | } | |
9928 | ||
a4eaf7f1 | 9929 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 9930 | { |
76e1d904 | 9931 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
9932 | } |
9933 | ||
a4eaf7f1 | 9934 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 9935 | { |
a4eaf7f1 | 9936 | event->hw.state = 0; |
d6d020e9 | 9937 | } |
aa9c4c0f | 9938 | |
a4eaf7f1 | 9939 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 9940 | { |
a4eaf7f1 | 9941 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
9942 | } |
9943 | ||
49f135ed FW |
9944 | /* Deref the hlist from the update side */ |
9945 | static inline struct swevent_hlist * | |
b28ab83c | 9946 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 9947 | { |
b28ab83c PZ |
9948 | return rcu_dereference_protected(swhash->swevent_hlist, |
9949 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
9950 | } |
9951 | ||
b28ab83c | 9952 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 9953 | { |
b28ab83c | 9954 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 9955 | |
49f135ed | 9956 | if (!hlist) |
76e1d904 FW |
9957 | return; |
9958 | ||
70691d4a | 9959 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 9960 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
9961 | } |
9962 | ||
3b364d7b | 9963 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 9964 | { |
b28ab83c | 9965 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 9966 | |
b28ab83c | 9967 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 9968 | |
b28ab83c PZ |
9969 | if (!--swhash->hlist_refcount) |
9970 | swevent_hlist_release(swhash); | |
76e1d904 | 9971 | |
b28ab83c | 9972 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
9973 | } |
9974 | ||
3b364d7b | 9975 | static void swevent_hlist_put(void) |
76e1d904 FW |
9976 | { |
9977 | int cpu; | |
9978 | ||
76e1d904 | 9979 | for_each_possible_cpu(cpu) |
3b364d7b | 9980 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
9981 | } |
9982 | ||
3b364d7b | 9983 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 9984 | { |
b28ab83c | 9985 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
9986 | int err = 0; |
9987 | ||
b28ab83c | 9988 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
9989 | if (!swevent_hlist_deref(swhash) && |
9990 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
9991 | struct swevent_hlist *hlist; |
9992 | ||
9993 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
9994 | if (!hlist) { | |
9995 | err = -ENOMEM; | |
9996 | goto exit; | |
9997 | } | |
b28ab83c | 9998 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 9999 | } |
b28ab83c | 10000 | swhash->hlist_refcount++; |
9ed6060d | 10001 | exit: |
b28ab83c | 10002 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
10003 | |
10004 | return err; | |
10005 | } | |
10006 | ||
3b364d7b | 10007 | static int swevent_hlist_get(void) |
76e1d904 | 10008 | { |
3b364d7b | 10009 | int err, cpu, failed_cpu; |
76e1d904 | 10010 | |
a63fbed7 | 10011 | mutex_lock(&pmus_lock); |
76e1d904 | 10012 | for_each_possible_cpu(cpu) { |
3b364d7b | 10013 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
10014 | if (err) { |
10015 | failed_cpu = cpu; | |
10016 | goto fail; | |
10017 | } | |
10018 | } | |
a63fbed7 | 10019 | mutex_unlock(&pmus_lock); |
76e1d904 | 10020 | return 0; |
9ed6060d | 10021 | fail: |
76e1d904 FW |
10022 | for_each_possible_cpu(cpu) { |
10023 | if (cpu == failed_cpu) | |
10024 | break; | |
3b364d7b | 10025 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 10026 | } |
a63fbed7 | 10027 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
10028 | return err; |
10029 | } | |
10030 | ||
c5905afb | 10031 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 10032 | |
b0a873eb PZ |
10033 | static void sw_perf_event_destroy(struct perf_event *event) |
10034 | { | |
10035 | u64 event_id = event->attr.config; | |
95476b64 | 10036 | |
b0a873eb PZ |
10037 | WARN_ON(event->parent); |
10038 | ||
c5905afb | 10039 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 10040 | swevent_hlist_put(); |
b0a873eb PZ |
10041 | } |
10042 | ||
0d6d062c RB |
10043 | static struct pmu perf_cpu_clock; /* fwd declaration */ |
10044 | static struct pmu perf_task_clock; | |
10045 | ||
b0a873eb PZ |
10046 | static int perf_swevent_init(struct perf_event *event) |
10047 | { | |
8176cced | 10048 | u64 event_id = event->attr.config; |
b0a873eb PZ |
10049 | |
10050 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
10051 | return -ENOENT; | |
10052 | ||
2481c5fa SE |
10053 | /* |
10054 | * no branch sampling for software events | |
10055 | */ | |
10056 | if (has_branch_stack(event)) | |
10057 | return -EOPNOTSUPP; | |
10058 | ||
b0a873eb PZ |
10059 | switch (event_id) { |
10060 | case PERF_COUNT_SW_CPU_CLOCK: | |
0d6d062c RB |
10061 | event->attr.type = perf_cpu_clock.type; |
10062 | return -ENOENT; | |
b0a873eb | 10063 | case PERF_COUNT_SW_TASK_CLOCK: |
0d6d062c | 10064 | event->attr.type = perf_task_clock.type; |
b0a873eb PZ |
10065 | return -ENOENT; |
10066 | ||
10067 | default: | |
10068 | break; | |
10069 | } | |
10070 | ||
ce677831 | 10071 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
10072 | return -ENOENT; |
10073 | ||
10074 | if (!event->parent) { | |
10075 | int err; | |
10076 | ||
3b364d7b | 10077 | err = swevent_hlist_get(); |
b0a873eb PZ |
10078 | if (err) |
10079 | return err; | |
10080 | ||
c5905afb | 10081 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
10082 | event->destroy = sw_perf_event_destroy; |
10083 | } | |
10084 | ||
10085 | return 0; | |
10086 | } | |
10087 | ||
10088 | static struct pmu perf_swevent = { | |
89a1e187 | 10089 | .task_ctx_nr = perf_sw_context, |
95476b64 | 10090 | |
34f43927 PZ |
10091 | .capabilities = PERF_PMU_CAP_NO_NMI, |
10092 | ||
b0a873eb | 10093 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
10094 | .add = perf_swevent_add, |
10095 | .del = perf_swevent_del, | |
10096 | .start = perf_swevent_start, | |
10097 | .stop = perf_swevent_stop, | |
1c024eca | 10098 | .read = perf_swevent_read, |
1c024eca PZ |
10099 | }; |
10100 | ||
b0a873eb PZ |
10101 | #ifdef CONFIG_EVENT_TRACING |
10102 | ||
571f97f7 RB |
10103 | static void tp_perf_event_destroy(struct perf_event *event) |
10104 | { | |
10105 | perf_trace_destroy(event); | |
10106 | } | |
10107 | ||
10108 | static int perf_tp_event_init(struct perf_event *event) | |
10109 | { | |
10110 | int err; | |
10111 | ||
10112 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
10113 | return -ENOENT; | |
10114 | ||
10115 | /* | |
10116 | * no branch sampling for tracepoint events | |
10117 | */ | |
10118 | if (has_branch_stack(event)) | |
10119 | return -EOPNOTSUPP; | |
10120 | ||
10121 | err = perf_trace_init(event); | |
10122 | if (err) | |
10123 | return err; | |
10124 | ||
10125 | event->destroy = tp_perf_event_destroy; | |
10126 | ||
10127 | return 0; | |
10128 | } | |
10129 | ||
10130 | static struct pmu perf_tracepoint = { | |
10131 | .task_ctx_nr = perf_sw_context, | |
10132 | ||
10133 | .event_init = perf_tp_event_init, | |
10134 | .add = perf_trace_add, | |
10135 | .del = perf_trace_del, | |
10136 | .start = perf_swevent_start, | |
10137 | .stop = perf_swevent_stop, | |
10138 | .read = perf_swevent_read, | |
10139 | }; | |
10140 | ||
1c024eca PZ |
10141 | static int perf_tp_filter_match(struct perf_event *event, |
10142 | struct perf_sample_data *data) | |
10143 | { | |
7e3f977e | 10144 | void *record = data->raw->frag.data; |
1c024eca | 10145 | |
b71b437e PZ |
10146 | /* only top level events have filters set */ |
10147 | if (event->parent) | |
10148 | event = event->parent; | |
10149 | ||
1c024eca PZ |
10150 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
10151 | return 1; | |
10152 | return 0; | |
10153 | } | |
10154 | ||
10155 | static int perf_tp_event_match(struct perf_event *event, | |
10156 | struct perf_sample_data *data, | |
10157 | struct pt_regs *regs) | |
10158 | { | |
a0f7d0f7 FW |
10159 | if (event->hw.state & PERF_HES_STOPPED) |
10160 | return 0; | |
580d607c | 10161 | /* |
9fd2e48b | 10162 | * If exclude_kernel, only trace user-space tracepoints (uprobes) |
580d607c | 10163 | */ |
9fd2e48b | 10164 | if (event->attr.exclude_kernel && !user_mode(regs)) |
1c024eca PZ |
10165 | return 0; |
10166 | ||
10167 | if (!perf_tp_filter_match(event, data)) | |
10168 | return 0; | |
10169 | ||
10170 | return 1; | |
10171 | } | |
10172 | ||
85b67bcb AS |
10173 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
10174 | struct trace_event_call *call, u64 count, | |
10175 | struct pt_regs *regs, struct hlist_head *head, | |
10176 | struct task_struct *task) | |
10177 | { | |
e87c6bc3 | 10178 | if (bpf_prog_array_valid(call)) { |
85b67bcb | 10179 | *(struct pt_regs **)raw_data = regs; |
e87c6bc3 | 10180 | if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { |
85b67bcb AS |
10181 | perf_swevent_put_recursion_context(rctx); |
10182 | return; | |
10183 | } | |
10184 | } | |
10185 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
8fd0fbbe | 10186 | rctx, task); |
85b67bcb AS |
10187 | } |
10188 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
10189 | ||
571f97f7 RB |
10190 | static void __perf_tp_event_target_task(u64 count, void *record, |
10191 | struct pt_regs *regs, | |
10192 | struct perf_sample_data *data, | |
10193 | struct perf_event *event) | |
10194 | { | |
10195 | struct trace_entry *entry = record; | |
10196 | ||
10197 | if (event->attr.config != entry->type) | |
10198 | return; | |
10199 | /* Cannot deliver synchronous signal to other task. */ | |
10200 | if (event->attr.sigtrap) | |
10201 | return; | |
10202 | if (perf_tp_event_match(event, data, regs)) | |
10203 | perf_swevent_event(event, count, data, regs); | |
10204 | } | |
10205 | ||
10206 | static void perf_tp_event_target_task(u64 count, void *record, | |
10207 | struct pt_regs *regs, | |
10208 | struct perf_sample_data *data, | |
10209 | struct perf_event_context *ctx) | |
10210 | { | |
10211 | unsigned int cpu = smp_processor_id(); | |
10212 | struct pmu *pmu = &perf_tracepoint; | |
10213 | struct perf_event *event, *sibling; | |
10214 | ||
10215 | perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { | |
10216 | __perf_tp_event_target_task(count, record, regs, data, event); | |
10217 | for_each_sibling_event(sibling, event) | |
10218 | __perf_tp_event_target_task(count, record, regs, data, sibling); | |
10219 | } | |
10220 | ||
10221 | perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { | |
10222 | __perf_tp_event_target_task(count, record, regs, data, event); | |
10223 | for_each_sibling_event(sibling, event) | |
10224 | __perf_tp_event_target_task(count, record, regs, data, sibling); | |
10225 | } | |
10226 | } | |
10227 | ||
1e1dcd93 | 10228 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 10229 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
8fd0fbbe | 10230 | struct task_struct *task) |
95476b64 FW |
10231 | { |
10232 | struct perf_sample_data data; | |
8fd0fbbe | 10233 | struct perf_event *event; |
1c024eca | 10234 | |
95476b64 | 10235 | struct perf_raw_record raw = { |
7e3f977e DB |
10236 | .frag = { |
10237 | .size = entry_size, | |
10238 | .data = record, | |
10239 | }, | |
95476b64 FW |
10240 | }; |
10241 | ||
1e1dcd93 | 10242 | perf_sample_data_init(&data, 0, 0); |
0a9081cf | 10243 | perf_sample_save_raw_data(&data, &raw); |
95476b64 | 10244 | |
1e1dcd93 AS |
10245 | perf_trace_buf_update(record, event_type); |
10246 | ||
8fd0fbbe | 10247 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1d1bfe30 | 10248 | if (perf_tp_event_match(event, &data, regs)) { |
a8b0ca17 | 10249 | perf_swevent_event(event, count, &data, regs); |
1d1bfe30 YJ |
10250 | |
10251 | /* | |
10252 | * Here use the same on-stack perf_sample_data, | |
10253 | * some members in data are event-specific and | |
10254 | * need to be re-computed for different sweveents. | |
10255 | * Re-initialize data->sample_flags safely to avoid | |
10256 | * the problem that next event skips preparing data | |
10257 | * because data->sample_flags is set. | |
10258 | */ | |
10259 | perf_sample_data_init(&data, 0, 0); | |
10260 | perf_sample_save_raw_data(&data, &raw); | |
10261 | } | |
4f41c013 | 10262 | } |
ecc55f84 | 10263 | |
e6dab5ff AV |
10264 | /* |
10265 | * If we got specified a target task, also iterate its context and | |
10266 | * deliver this event there too. | |
10267 | */ | |
10268 | if (task && task != current) { | |
10269 | struct perf_event_context *ctx; | |
e6dab5ff AV |
10270 | |
10271 | rcu_read_lock(); | |
bd275681 | 10272 | ctx = rcu_dereference(task->perf_event_ctxp); |
e6dab5ff AV |
10273 | if (!ctx) |
10274 | goto unlock; | |
10275 | ||
571f97f7 RB |
10276 | raw_spin_lock(&ctx->lock); |
10277 | perf_tp_event_target_task(count, record, regs, &data, ctx); | |
10278 | raw_spin_unlock(&ctx->lock); | |
e6dab5ff AV |
10279 | unlock: |
10280 | rcu_read_unlock(); | |
10281 | } | |
10282 | ||
ecc55f84 | 10283 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
10284 | } |
10285 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
10286 | ||
33ea4b24 | 10287 | #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) |
e12f03d7 SL |
10288 | /* |
10289 | * Flags in config, used by dynamic PMU kprobe and uprobe | |
10290 | * The flags should match following PMU_FORMAT_ATTR(). | |
10291 | * | |
10292 | * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe | |
10293 | * if not set, create kprobe/uprobe | |
a6ca88b2 SL |
10294 | * |
10295 | * The following values specify a reference counter (or semaphore in the | |
10296 | * terminology of tools like dtrace, systemtap, etc.) Userspace Statically | |
10297 | * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset. | |
10298 | * | |
10299 | * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset | |
10300 | * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left | |
e12f03d7 SL |
10301 | */ |
10302 | enum perf_probe_config { | |
10303 | PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */ | |
a6ca88b2 SL |
10304 | PERF_UPROBE_REF_CTR_OFFSET_BITS = 32, |
10305 | PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS, | |
e12f03d7 SL |
10306 | }; |
10307 | ||
10308 | PMU_FORMAT_ATTR(retprobe, "config:0"); | |
a6ca88b2 | 10309 | #endif |
e12f03d7 | 10310 | |
a6ca88b2 SL |
10311 | #ifdef CONFIG_KPROBE_EVENTS |
10312 | static struct attribute *kprobe_attrs[] = { | |
e12f03d7 SL |
10313 | &format_attr_retprobe.attr, |
10314 | NULL, | |
10315 | }; | |
10316 | ||
a6ca88b2 | 10317 | static struct attribute_group kprobe_format_group = { |
e12f03d7 | 10318 | .name = "format", |
a6ca88b2 | 10319 | .attrs = kprobe_attrs, |
e12f03d7 SL |
10320 | }; |
10321 | ||
a6ca88b2 SL |
10322 | static const struct attribute_group *kprobe_attr_groups[] = { |
10323 | &kprobe_format_group, | |
e12f03d7 SL |
10324 | NULL, |
10325 | }; | |
10326 | ||
10327 | static int perf_kprobe_event_init(struct perf_event *event); | |
10328 | static struct pmu perf_kprobe = { | |
10329 | .task_ctx_nr = perf_sw_context, | |
10330 | .event_init = perf_kprobe_event_init, | |
10331 | .add = perf_trace_add, | |
10332 | .del = perf_trace_del, | |
10333 | .start = perf_swevent_start, | |
10334 | .stop = perf_swevent_stop, | |
10335 | .read = perf_swevent_read, | |
a6ca88b2 | 10336 | .attr_groups = kprobe_attr_groups, |
e12f03d7 SL |
10337 | }; |
10338 | ||
10339 | static int perf_kprobe_event_init(struct perf_event *event) | |
10340 | { | |
10341 | int err; | |
10342 | bool is_retprobe; | |
10343 | ||
10344 | if (event->attr.type != perf_kprobe.type) | |
10345 | return -ENOENT; | |
32e6e967 | 10346 | |
c9e0924e | 10347 | if (!perfmon_capable()) |
32e6e967 SL |
10348 | return -EACCES; |
10349 | ||
e12f03d7 SL |
10350 | /* |
10351 | * no branch sampling for probe events | |
10352 | */ | |
10353 | if (has_branch_stack(event)) | |
10354 | return -EOPNOTSUPP; | |
10355 | ||
10356 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
10357 | err = perf_kprobe_init(event, is_retprobe); | |
10358 | if (err) | |
10359 | return err; | |
10360 | ||
10361 | event->destroy = perf_kprobe_destroy; | |
10362 | ||
10363 | return 0; | |
10364 | } | |
10365 | #endif /* CONFIG_KPROBE_EVENTS */ | |
10366 | ||
33ea4b24 | 10367 | #ifdef CONFIG_UPROBE_EVENTS |
a6ca88b2 SL |
10368 | PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63"); |
10369 | ||
10370 | static struct attribute *uprobe_attrs[] = { | |
10371 | &format_attr_retprobe.attr, | |
10372 | &format_attr_ref_ctr_offset.attr, | |
10373 | NULL, | |
10374 | }; | |
10375 | ||
10376 | static struct attribute_group uprobe_format_group = { | |
10377 | .name = "format", | |
10378 | .attrs = uprobe_attrs, | |
10379 | }; | |
10380 | ||
10381 | static const struct attribute_group *uprobe_attr_groups[] = { | |
10382 | &uprobe_format_group, | |
10383 | NULL, | |
10384 | }; | |
10385 | ||
33ea4b24 SL |
10386 | static int perf_uprobe_event_init(struct perf_event *event); |
10387 | static struct pmu perf_uprobe = { | |
10388 | .task_ctx_nr = perf_sw_context, | |
10389 | .event_init = perf_uprobe_event_init, | |
10390 | .add = perf_trace_add, | |
10391 | .del = perf_trace_del, | |
10392 | .start = perf_swevent_start, | |
10393 | .stop = perf_swevent_stop, | |
10394 | .read = perf_swevent_read, | |
a6ca88b2 | 10395 | .attr_groups = uprobe_attr_groups, |
33ea4b24 SL |
10396 | }; |
10397 | ||
10398 | static int perf_uprobe_event_init(struct perf_event *event) | |
10399 | { | |
10400 | int err; | |
a6ca88b2 | 10401 | unsigned long ref_ctr_offset; |
33ea4b24 SL |
10402 | bool is_retprobe; |
10403 | ||
10404 | if (event->attr.type != perf_uprobe.type) | |
10405 | return -ENOENT; | |
32e6e967 | 10406 | |
c9e0924e | 10407 | if (!perfmon_capable()) |
32e6e967 SL |
10408 | return -EACCES; |
10409 | ||
33ea4b24 SL |
10410 | /* |
10411 | * no branch sampling for probe events | |
10412 | */ | |
10413 | if (has_branch_stack(event)) | |
10414 | return -EOPNOTSUPP; | |
10415 | ||
10416 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
a6ca88b2 SL |
10417 | ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
10418 | err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe); | |
33ea4b24 SL |
10419 | if (err) |
10420 | return err; | |
10421 | ||
10422 | event->destroy = perf_uprobe_destroy; | |
10423 | ||
10424 | return 0; | |
10425 | } | |
10426 | #endif /* CONFIG_UPROBE_EVENTS */ | |
10427 | ||
b0a873eb PZ |
10428 | static inline void perf_tp_register(void) |
10429 | { | |
2e80a82a | 10430 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e12f03d7 SL |
10431 | #ifdef CONFIG_KPROBE_EVENTS |
10432 | perf_pmu_register(&perf_kprobe, "kprobe", -1); | |
10433 | #endif | |
33ea4b24 SL |
10434 | #ifdef CONFIG_UPROBE_EVENTS |
10435 | perf_pmu_register(&perf_uprobe, "uprobe", -1); | |
10436 | #endif | |
e077df4f | 10437 | } |
6fb2915d | 10438 | |
6fb2915d LZ |
10439 | static void perf_event_free_filter(struct perf_event *event) |
10440 | { | |
10441 | ftrace_profile_free_filter(event); | |
10442 | } | |
10443 | ||
aa6a5f3c AS |
10444 | #ifdef CONFIG_BPF_SYSCALL |
10445 | static void bpf_overflow_handler(struct perf_event *event, | |
10446 | struct perf_sample_data *data, | |
10447 | struct pt_regs *regs) | |
10448 | { | |
10449 | struct bpf_perf_event_data_kern ctx = { | |
10450 | .data = data, | |
7d9285e8 | 10451 | .event = event, |
aa6a5f3c | 10452 | }; |
594286b7 | 10453 | struct bpf_prog *prog; |
aa6a5f3c AS |
10454 | int ret = 0; |
10455 | ||
c895f6f7 | 10456 | ctx.regs = perf_arch_bpf_user_pt_regs(regs); |
aa6a5f3c AS |
10457 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) |
10458 | goto out; | |
10459 | rcu_read_lock(); | |
594286b7 | 10460 | prog = READ_ONCE(event->prog); |
16817ad7 | 10461 | if (prog) { |
0eed2822 | 10462 | perf_prepare_sample(data, event, regs); |
594286b7 | 10463 | ret = bpf_prog_run(prog, &ctx); |
16817ad7 | 10464 | } |
aa6a5f3c AS |
10465 | rcu_read_unlock(); |
10466 | out: | |
10467 | __this_cpu_dec(bpf_prog_active); | |
aa6a5f3c AS |
10468 | if (!ret) |
10469 | return; | |
10470 | ||
10471 | event->orig_overflow_handler(event, data, regs); | |
10472 | } | |
10473 | ||
82e6b1ee AN |
10474 | static int perf_event_set_bpf_handler(struct perf_event *event, |
10475 | struct bpf_prog *prog, | |
10476 | u64 bpf_cookie) | |
aa6a5f3c | 10477 | { |
aa6a5f3c AS |
10478 | if (event->overflow_handler_context) |
10479 | /* hw breakpoint or kernel counter */ | |
10480 | return -EINVAL; | |
10481 | ||
10482 | if (event->prog) | |
10483 | return -EEXIST; | |
10484 | ||
652c1b17 AN |
10485 | if (prog->type != BPF_PROG_TYPE_PERF_EVENT) |
10486 | return -EINVAL; | |
aa6a5f3c | 10487 | |
5d99cb2c SL |
10488 | if (event->attr.precise_ip && |
10489 | prog->call_get_stack && | |
16817ad7 | 10490 | (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || |
5d99cb2c SL |
10491 | event->attr.exclude_callchain_kernel || |
10492 | event->attr.exclude_callchain_user)) { | |
10493 | /* | |
10494 | * On perf_event with precise_ip, calling bpf_get_stack() | |
10495 | * may trigger unwinder warnings and occasional crashes. | |
10496 | * bpf_get_[stack|stackid] works around this issue by using | |
10497 | * callchain attached to perf_sample_data. If the | |
10498 | * perf_event does not full (kernel and user) callchain | |
10499 | * attached to perf_sample_data, do not allow attaching BPF | |
10500 | * program that calls bpf_get_[stack|stackid]. | |
10501 | */ | |
5d99cb2c SL |
10502 | return -EPROTO; |
10503 | } | |
10504 | ||
aa6a5f3c | 10505 | event->prog = prog; |
82e6b1ee | 10506 | event->bpf_cookie = bpf_cookie; |
aa6a5f3c AS |
10507 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); |
10508 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
10509 | return 0; | |
10510 | } | |
10511 | ||
10512 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
10513 | { | |
10514 | struct bpf_prog *prog = event->prog; | |
10515 | ||
10516 | if (!prog) | |
10517 | return; | |
10518 | ||
10519 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
10520 | event->prog = NULL; | |
10521 | bpf_prog_put(prog); | |
10522 | } | |
10523 | #else | |
82e6b1ee AN |
10524 | static int perf_event_set_bpf_handler(struct perf_event *event, |
10525 | struct bpf_prog *prog, | |
10526 | u64 bpf_cookie) | |
aa6a5f3c AS |
10527 | { |
10528 | return -EOPNOTSUPP; | |
10529 | } | |
10530 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
10531 | { | |
10532 | } | |
10533 | #endif | |
10534 | ||
e12f03d7 SL |
10535 | /* |
10536 | * returns true if the event is a tracepoint, or a kprobe/upprobe created | |
10537 | * with perf_event_open() | |
10538 | */ | |
10539 | static inline bool perf_event_is_tracing(struct perf_event *event) | |
10540 | { | |
10541 | if (event->pmu == &perf_tracepoint) | |
10542 | return true; | |
10543 | #ifdef CONFIG_KPROBE_EVENTS | |
10544 | if (event->pmu == &perf_kprobe) | |
10545 | return true; | |
33ea4b24 SL |
10546 | #endif |
10547 | #ifdef CONFIG_UPROBE_EVENTS | |
10548 | if (event->pmu == &perf_uprobe) | |
10549 | return true; | |
e12f03d7 SL |
10550 | #endif |
10551 | return false; | |
10552 | } | |
10553 | ||
82e6b1ee AN |
10554 | int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, |
10555 | u64 bpf_cookie) | |
2541517c | 10556 | { |
64ad7556 | 10557 | bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp; |
2541517c | 10558 | |
e12f03d7 | 10559 | if (!perf_event_is_tracing(event)) |
82e6b1ee | 10560 | return perf_event_set_bpf_handler(event, prog, bpf_cookie); |
2541517c | 10561 | |
64ad7556 DK |
10562 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; |
10563 | is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; | |
98b5c2c6 | 10564 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; |
cf5f5cea | 10565 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
64ad7556 | 10566 | if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp) |
98b5c2c6 | 10567 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
2541517c AS |
10568 | return -EINVAL; |
10569 | ||
64ad7556 | 10570 | if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) || |
cf5f5cea | 10571 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || |
652c1b17 | 10572 | (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) |
2541517c | 10573 | return -EINVAL; |
2541517c | 10574 | |
66c84731 | 10575 | if (prog->type == BPF_PROG_TYPE_KPROBE && prog->sleepable && !is_uprobe) |
64ad7556 DK |
10576 | /* only uprobe programs are allowed to be sleepable */ |
10577 | return -EINVAL; | |
10578 | ||
9802d865 | 10579 | /* Kprobe override only works for kprobes, not uprobes. */ |
64ad7556 | 10580 | if (prog->kprobe_override && !is_kprobe) |
9802d865 | 10581 | return -EINVAL; |
9802d865 | 10582 | |
cf5f5cea | 10583 | if (is_tracepoint || is_syscall_tp) { |
32bbe007 AS |
10584 | int off = trace_event_get_offsets(event->tp_event); |
10585 | ||
652c1b17 | 10586 | if (prog->aux->max_ctx_offset > off) |
32bbe007 | 10587 | return -EACCES; |
32bbe007 | 10588 | } |
2541517c | 10589 | |
82e6b1ee | 10590 | return perf_event_attach_bpf_prog(event, prog, bpf_cookie); |
2541517c AS |
10591 | } |
10592 | ||
b89fbfbb | 10593 | void perf_event_free_bpf_prog(struct perf_event *event) |
2541517c | 10594 | { |
e12f03d7 | 10595 | if (!perf_event_is_tracing(event)) { |
0b4c6841 | 10596 | perf_event_free_bpf_handler(event); |
2541517c | 10597 | return; |
2541517c | 10598 | } |
e87c6bc3 | 10599 | perf_event_detach_bpf_prog(event); |
2541517c AS |
10600 | } |
10601 | ||
e077df4f | 10602 | #else |
6fb2915d | 10603 | |
b0a873eb | 10604 | static inline void perf_tp_register(void) |
e077df4f | 10605 | { |
e077df4f | 10606 | } |
6fb2915d | 10607 | |
6fb2915d LZ |
10608 | static void perf_event_free_filter(struct perf_event *event) |
10609 | { | |
10610 | } | |
10611 | ||
82e6b1ee AN |
10612 | int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, |
10613 | u64 bpf_cookie) | |
2541517c AS |
10614 | { |
10615 | return -ENOENT; | |
10616 | } | |
10617 | ||
b89fbfbb | 10618 | void perf_event_free_bpf_prog(struct perf_event *event) |
2541517c AS |
10619 | { |
10620 | } | |
07b139c8 | 10621 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 10622 | |
24f1e32c | 10623 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 10624 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 10625 | { |
f5ffe02e FW |
10626 | struct perf_sample_data sample; |
10627 | struct pt_regs *regs = data; | |
10628 | ||
fd0d000b | 10629 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 10630 | |
a4eaf7f1 | 10631 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 10632 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
10633 | } |
10634 | #endif | |
10635 | ||
375637bc AS |
10636 | /* |
10637 | * Allocate a new address filter | |
10638 | */ | |
10639 | static struct perf_addr_filter * | |
10640 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
10641 | { | |
10642 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
10643 | struct perf_addr_filter *filter; | |
10644 | ||
10645 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
10646 | if (!filter) | |
10647 | return NULL; | |
10648 | ||
10649 | INIT_LIST_HEAD(&filter->entry); | |
10650 | list_add_tail(&filter->entry, filters); | |
10651 | ||
10652 | return filter; | |
10653 | } | |
10654 | ||
10655 | static void free_filters_list(struct list_head *filters) | |
10656 | { | |
10657 | struct perf_addr_filter *filter, *iter; | |
10658 | ||
10659 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
9511bce9 | 10660 | path_put(&filter->path); |
375637bc AS |
10661 | list_del(&filter->entry); |
10662 | kfree(filter); | |
10663 | } | |
10664 | } | |
10665 | ||
10666 | /* | |
10667 | * Free existing address filters and optionally install new ones | |
10668 | */ | |
10669 | static void perf_addr_filters_splice(struct perf_event *event, | |
10670 | struct list_head *head) | |
10671 | { | |
10672 | unsigned long flags; | |
10673 | LIST_HEAD(list); | |
10674 | ||
10675 | if (!has_addr_filter(event)) | |
10676 | return; | |
10677 | ||
10678 | /* don't bother with children, they don't have their own filters */ | |
10679 | if (event->parent) | |
10680 | return; | |
10681 | ||
10682 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
10683 | ||
10684 | list_splice_init(&event->addr_filters.list, &list); | |
10685 | if (head) | |
10686 | list_splice(head, &event->addr_filters.list); | |
10687 | ||
10688 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
10689 | ||
10690 | free_filters_list(&list); | |
10691 | } | |
10692 | ||
10693 | /* | |
10694 | * Scan through mm's vmas and see if one of them matches the | |
10695 | * @filter; if so, adjust filter's address range. | |
c1e8d7c6 | 10696 | * Called with mm::mmap_lock down for reading. |
375637bc | 10697 | */ |
c60f83b8 AS |
10698 | static void perf_addr_filter_apply(struct perf_addr_filter *filter, |
10699 | struct mm_struct *mm, | |
10700 | struct perf_addr_filter_range *fr) | |
375637bc AS |
10701 | { |
10702 | struct vm_area_struct *vma; | |
fcb72a58 | 10703 | VMA_ITERATOR(vmi, mm, 0); |
375637bc | 10704 | |
fcb72a58 | 10705 | for_each_vma(vmi, vma) { |
c60f83b8 | 10706 | if (!vma->vm_file) |
375637bc AS |
10707 | continue; |
10708 | ||
c60f83b8 AS |
10709 | if (perf_addr_filter_vma_adjust(filter, vma, fr)) |
10710 | return; | |
375637bc | 10711 | } |
375637bc AS |
10712 | } |
10713 | ||
10714 | /* | |
10715 | * Update event's address range filters based on the | |
10716 | * task's existing mappings, if any. | |
10717 | */ | |
10718 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
10719 | { | |
10720 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
10721 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
10722 | struct perf_addr_filter *filter; | |
10723 | struct mm_struct *mm = NULL; | |
10724 | unsigned int count = 0; | |
10725 | unsigned long flags; | |
10726 | ||
10727 | /* | |
10728 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
10729 | * will stop on the parent's child_mutex that our caller is also holding | |
10730 | */ | |
10731 | if (task == TASK_TOMBSTONE) | |
10732 | return; | |
10733 | ||
52a44f83 | 10734 | if (ifh->nr_file_filters) { |
b89a05b2 | 10735 | mm = get_task_mm(task); |
52a44f83 AS |
10736 | if (!mm) |
10737 | goto restart; | |
375637bc | 10738 | |
d8ed45c5 | 10739 | mmap_read_lock(mm); |
52a44f83 | 10740 | } |
375637bc AS |
10741 | |
10742 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
10743 | list_for_each_entry(filter, &ifh->list, entry) { | |
52a44f83 AS |
10744 | if (filter->path.dentry) { |
10745 | /* | |
10746 | * Adjust base offset if the filter is associated to a | |
10747 | * binary that needs to be mapped: | |
10748 | */ | |
10749 | event->addr_filter_ranges[count].start = 0; | |
10750 | event->addr_filter_ranges[count].size = 0; | |
375637bc | 10751 | |
c60f83b8 | 10752 | perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]); |
52a44f83 AS |
10753 | } else { |
10754 | event->addr_filter_ranges[count].start = filter->offset; | |
10755 | event->addr_filter_ranges[count].size = filter->size; | |
10756 | } | |
375637bc AS |
10757 | |
10758 | count++; | |
10759 | } | |
10760 | ||
10761 | event->addr_filters_gen++; | |
10762 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
10763 | ||
52a44f83 | 10764 | if (ifh->nr_file_filters) { |
d8ed45c5 | 10765 | mmap_read_unlock(mm); |
375637bc | 10766 | |
52a44f83 AS |
10767 | mmput(mm); |
10768 | } | |
375637bc AS |
10769 | |
10770 | restart: | |
767ae086 | 10771 | perf_event_stop(event, 1); |
375637bc AS |
10772 | } |
10773 | ||
10774 | /* | |
10775 | * Address range filtering: limiting the data to certain | |
10776 | * instruction address ranges. Filters are ioctl()ed to us from | |
10777 | * userspace as ascii strings. | |
10778 | * | |
10779 | * Filter string format: | |
10780 | * | |
10781 | * ACTION RANGE_SPEC | |
10782 | * where ACTION is one of the | |
10783 | * * "filter": limit the trace to this region | |
10784 | * * "start": start tracing from this address | |
10785 | * * "stop": stop tracing at this address/region; | |
10786 | * RANGE_SPEC is | |
10787 | * * for kernel addresses: <start address>[/<size>] | |
10788 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
10789 | * | |
6ed70cf3 AS |
10790 | * if <size> is not specified or is zero, the range is treated as a single |
10791 | * address; not valid for ACTION=="filter". | |
375637bc AS |
10792 | */ |
10793 | enum { | |
e96271f3 | 10794 | IF_ACT_NONE = -1, |
375637bc AS |
10795 | IF_ACT_FILTER, |
10796 | IF_ACT_START, | |
10797 | IF_ACT_STOP, | |
10798 | IF_SRC_FILE, | |
10799 | IF_SRC_KERNEL, | |
10800 | IF_SRC_FILEADDR, | |
10801 | IF_SRC_KERNELADDR, | |
10802 | }; | |
10803 | ||
10804 | enum { | |
10805 | IF_STATE_ACTION = 0, | |
10806 | IF_STATE_SOURCE, | |
10807 | IF_STATE_END, | |
10808 | }; | |
10809 | ||
10810 | static const match_table_t if_tokens = { | |
10811 | { IF_ACT_FILTER, "filter" }, | |
10812 | { IF_ACT_START, "start" }, | |
10813 | { IF_ACT_STOP, "stop" }, | |
10814 | { IF_SRC_FILE, "%u/%u@%s" }, | |
10815 | { IF_SRC_KERNEL, "%u/%u" }, | |
10816 | { IF_SRC_FILEADDR, "%u@%s" }, | |
10817 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 10818 | { IF_ACT_NONE, NULL }, |
375637bc AS |
10819 | }; |
10820 | ||
10821 | /* | |
10822 | * Address filter string parser | |
10823 | */ | |
10824 | static int | |
10825 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
10826 | struct list_head *filters) | |
10827 | { | |
10828 | struct perf_addr_filter *filter = NULL; | |
10829 | char *start, *orig, *filename = NULL; | |
375637bc AS |
10830 | substring_t args[MAX_OPT_ARGS]; |
10831 | int state = IF_STATE_ACTION, token; | |
10832 | unsigned int kernel = 0; | |
10833 | int ret = -EINVAL; | |
10834 | ||
10835 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
10836 | if (!fstr) | |
10837 | return -ENOMEM; | |
10838 | ||
10839 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
6ed70cf3 AS |
10840 | static const enum perf_addr_filter_action_t actions[] = { |
10841 | [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER, | |
10842 | [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START, | |
10843 | [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP, | |
10844 | }; | |
375637bc AS |
10845 | ret = -EINVAL; |
10846 | ||
10847 | if (!*start) | |
10848 | continue; | |
10849 | ||
10850 | /* filter definition begins */ | |
10851 | if (state == IF_STATE_ACTION) { | |
10852 | filter = perf_addr_filter_new(event, filters); | |
10853 | if (!filter) | |
10854 | goto fail; | |
10855 | } | |
10856 | ||
10857 | token = match_token(start, if_tokens, args); | |
10858 | switch (token) { | |
10859 | case IF_ACT_FILTER: | |
10860 | case IF_ACT_START: | |
375637bc AS |
10861 | case IF_ACT_STOP: |
10862 | if (state != IF_STATE_ACTION) | |
10863 | goto fail; | |
10864 | ||
6ed70cf3 | 10865 | filter->action = actions[token]; |
375637bc AS |
10866 | state = IF_STATE_SOURCE; |
10867 | break; | |
10868 | ||
10869 | case IF_SRC_KERNELADDR: | |
10870 | case IF_SRC_KERNEL: | |
10871 | kernel = 1; | |
df561f66 | 10872 | fallthrough; |
375637bc AS |
10873 | |
10874 | case IF_SRC_FILEADDR: | |
10875 | case IF_SRC_FILE: | |
10876 | if (state != IF_STATE_SOURCE) | |
10877 | goto fail; | |
10878 | ||
375637bc AS |
10879 | *args[0].to = 0; |
10880 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
10881 | if (ret) | |
10882 | goto fail; | |
10883 | ||
6ed70cf3 | 10884 | if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) { |
375637bc AS |
10885 | *args[1].to = 0; |
10886 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
10887 | if (ret) | |
10888 | goto fail; | |
10889 | } | |
10890 | ||
4059ffd0 | 10891 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
6ed70cf3 | 10892 | int fpos = token == IF_SRC_FILE ? 2 : 1; |
4059ffd0 | 10893 | |
7bdb157c | 10894 | kfree(filename); |
4059ffd0 | 10895 | filename = match_strdup(&args[fpos]); |
375637bc AS |
10896 | if (!filename) { |
10897 | ret = -ENOMEM; | |
10898 | goto fail; | |
10899 | } | |
10900 | } | |
10901 | ||
10902 | state = IF_STATE_END; | |
10903 | break; | |
10904 | ||
10905 | default: | |
10906 | goto fail; | |
10907 | } | |
10908 | ||
10909 | /* | |
10910 | * Filter definition is fully parsed, validate and install it. | |
10911 | * Make sure that it doesn't contradict itself or the event's | |
10912 | * attribute. | |
10913 | */ | |
10914 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 10915 | ret = -EINVAL; |
375637bc | 10916 | |
6ed70cf3 AS |
10917 | /* |
10918 | * ACTION "filter" must have a non-zero length region | |
10919 | * specified. | |
10920 | */ | |
10921 | if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER && | |
10922 | !filter->size) | |
10923 | goto fail; | |
10924 | ||
375637bc AS |
10925 | if (!kernel) { |
10926 | if (!filename) | |
10927 | goto fail; | |
10928 | ||
6ce77bfd AS |
10929 | /* |
10930 | * For now, we only support file-based filters | |
10931 | * in per-task events; doing so for CPU-wide | |
10932 | * events requires additional context switching | |
10933 | * trickery, since same object code will be | |
10934 | * mapped at different virtual addresses in | |
10935 | * different processes. | |
10936 | */ | |
10937 | ret = -EOPNOTSUPP; | |
10938 | if (!event->ctx->task) | |
7bdb157c | 10939 | goto fail; |
6ce77bfd | 10940 | |
375637bc | 10941 | /* look up the path and grab its inode */ |
9511bce9 SL |
10942 | ret = kern_path(filename, LOOKUP_FOLLOW, |
10943 | &filter->path); | |
375637bc | 10944 | if (ret) |
7bdb157c | 10945 | goto fail; |
375637bc AS |
10946 | |
10947 | ret = -EINVAL; | |
9511bce9 SL |
10948 | if (!filter->path.dentry || |
10949 | !S_ISREG(d_inode(filter->path.dentry) | |
10950 | ->i_mode)) | |
375637bc | 10951 | goto fail; |
6ce77bfd AS |
10952 | |
10953 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
10954 | } |
10955 | ||
10956 | /* ready to consume more filters */ | |
d680ff24 AH |
10957 | kfree(filename); |
10958 | filename = NULL; | |
375637bc AS |
10959 | state = IF_STATE_ACTION; |
10960 | filter = NULL; | |
d680ff24 | 10961 | kernel = 0; |
375637bc AS |
10962 | } |
10963 | } | |
10964 | ||
10965 | if (state != IF_STATE_ACTION) | |
10966 | goto fail; | |
10967 | ||
7bdb157c | 10968 | kfree(filename); |
375637bc AS |
10969 | kfree(orig); |
10970 | ||
10971 | return 0; | |
10972 | ||
375637bc | 10973 | fail: |
7bdb157c | 10974 | kfree(filename); |
375637bc AS |
10975 | free_filters_list(filters); |
10976 | kfree(orig); | |
10977 | ||
10978 | return ret; | |
10979 | } | |
10980 | ||
10981 | static int | |
10982 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
10983 | { | |
10984 | LIST_HEAD(filters); | |
10985 | int ret; | |
10986 | ||
10987 | /* | |
10988 | * Since this is called in perf_ioctl() path, we're already holding | |
10989 | * ctx::mutex. | |
10990 | */ | |
10991 | lockdep_assert_held(&event->ctx->mutex); | |
10992 | ||
10993 | if (WARN_ON_ONCE(event->parent)) | |
10994 | return -EINVAL; | |
10995 | ||
375637bc AS |
10996 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
10997 | if (ret) | |
6ce77bfd | 10998 | goto fail_clear_files; |
375637bc AS |
10999 | |
11000 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
11001 | if (ret) |
11002 | goto fail_free_filters; | |
375637bc AS |
11003 | |
11004 | /* remove existing filters, if any */ | |
11005 | perf_addr_filters_splice(event, &filters); | |
11006 | ||
11007 | /* install new filters */ | |
11008 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
11009 | ||
6ce77bfd AS |
11010 | return ret; |
11011 | ||
11012 | fail_free_filters: | |
11013 | free_filters_list(&filters); | |
11014 | ||
11015 | fail_clear_files: | |
11016 | event->addr_filters.nr_file_filters = 0; | |
11017 | ||
375637bc AS |
11018 | return ret; |
11019 | } | |
11020 | ||
c796bbbe AS |
11021 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
11022 | { | |
c796bbbe | 11023 | int ret = -EINVAL; |
e12f03d7 | 11024 | char *filter_str; |
c796bbbe AS |
11025 | |
11026 | filter_str = strndup_user(arg, PAGE_SIZE); | |
11027 | if (IS_ERR(filter_str)) | |
11028 | return PTR_ERR(filter_str); | |
11029 | ||
e12f03d7 SL |
11030 | #ifdef CONFIG_EVENT_TRACING |
11031 | if (perf_event_is_tracing(event)) { | |
11032 | struct perf_event_context *ctx = event->ctx; | |
11033 | ||
11034 | /* | |
11035 | * Beware, here be dragons!! | |
11036 | * | |
11037 | * the tracepoint muck will deadlock against ctx->mutex, but | |
11038 | * the tracepoint stuff does not actually need it. So | |
11039 | * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we | |
11040 | * already have a reference on ctx. | |
11041 | * | |
11042 | * This can result in event getting moved to a different ctx, | |
11043 | * but that does not affect the tracepoint state. | |
11044 | */ | |
11045 | mutex_unlock(&ctx->mutex); | |
11046 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
11047 | mutex_lock(&ctx->mutex); | |
11048 | } else | |
11049 | #endif | |
11050 | if (has_addr_filter(event)) | |
375637bc | 11051 | ret = perf_event_set_addr_filter(event, filter_str); |
c796bbbe AS |
11052 | |
11053 | kfree(filter_str); | |
11054 | return ret; | |
11055 | } | |
11056 | ||
b0a873eb PZ |
11057 | /* |
11058 | * hrtimer based swevent callback | |
11059 | */ | |
f29ac756 | 11060 | |
b0a873eb | 11061 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 11062 | { |
b0a873eb PZ |
11063 | enum hrtimer_restart ret = HRTIMER_RESTART; |
11064 | struct perf_sample_data data; | |
11065 | struct pt_regs *regs; | |
11066 | struct perf_event *event; | |
11067 | u64 period; | |
f29ac756 | 11068 | |
b0a873eb | 11069 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
11070 | |
11071 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
11072 | return HRTIMER_NORESTART; | |
11073 | ||
b0a873eb | 11074 | event->pmu->read(event); |
f344011c | 11075 | |
fd0d000b | 11076 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
11077 | regs = get_irq_regs(); |
11078 | ||
11079 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 11080 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 11081 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
11082 | ret = HRTIMER_NORESTART; |
11083 | } | |
24f1e32c | 11084 | |
b0a873eb PZ |
11085 | period = max_t(u64, 10000, event->hw.sample_period); |
11086 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 11087 | |
b0a873eb | 11088 | return ret; |
f29ac756 PZ |
11089 | } |
11090 | ||
b0a873eb | 11091 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 11092 | { |
b0a873eb | 11093 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
11094 | s64 period; |
11095 | ||
11096 | if (!is_sampling_event(event)) | |
11097 | return; | |
f5ffe02e | 11098 | |
5d508e82 FBH |
11099 | period = local64_read(&hwc->period_left); |
11100 | if (period) { | |
11101 | if (period < 0) | |
11102 | period = 10000; | |
fa407f35 | 11103 | |
5d508e82 FBH |
11104 | local64_set(&hwc->period_left, 0); |
11105 | } else { | |
11106 | period = max_t(u64, 10000, hwc->sample_period); | |
11107 | } | |
3497d206 | 11108 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
30f9028b | 11109 | HRTIMER_MODE_REL_PINNED_HARD); |
24f1e32c | 11110 | } |
b0a873eb PZ |
11111 | |
11112 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 11113 | { |
b0a873eb PZ |
11114 | struct hw_perf_event *hwc = &event->hw; |
11115 | ||
6c7e550f | 11116 | if (is_sampling_event(event)) { |
b0a873eb | 11117 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 11118 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
11119 | |
11120 | hrtimer_cancel(&hwc->hrtimer); | |
11121 | } | |
24f1e32c FW |
11122 | } |
11123 | ||
ba3dd36c PZ |
11124 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
11125 | { | |
11126 | struct hw_perf_event *hwc = &event->hw; | |
11127 | ||
11128 | if (!is_sampling_event(event)) | |
11129 | return; | |
11130 | ||
30f9028b | 11131 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
ba3dd36c PZ |
11132 | hwc->hrtimer.function = perf_swevent_hrtimer; |
11133 | ||
11134 | /* | |
11135 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
11136 | * mapping and avoid the whole period adjust feedback stuff. | |
11137 | */ | |
11138 | if (event->attr.freq) { | |
11139 | long freq = event->attr.sample_freq; | |
11140 | ||
11141 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
11142 | hwc->sample_period = event->attr.sample_period; | |
11143 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 11144 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
11145 | event->attr.freq = 0; |
11146 | } | |
11147 | } | |
11148 | ||
b0a873eb PZ |
11149 | /* |
11150 | * Software event: cpu wall time clock | |
11151 | */ | |
11152 | ||
11153 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 11154 | { |
b0a873eb PZ |
11155 | s64 prev; |
11156 | u64 now; | |
11157 | ||
a4eaf7f1 | 11158 | now = local_clock(); |
b0a873eb PZ |
11159 | prev = local64_xchg(&event->hw.prev_count, now); |
11160 | local64_add(now - prev, &event->count); | |
24f1e32c | 11161 | } |
24f1e32c | 11162 | |
a4eaf7f1 | 11163 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 11164 | { |
a4eaf7f1 | 11165 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 11166 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
11167 | } |
11168 | ||
a4eaf7f1 | 11169 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 11170 | { |
b0a873eb PZ |
11171 | perf_swevent_cancel_hrtimer(event); |
11172 | cpu_clock_event_update(event); | |
11173 | } | |
f29ac756 | 11174 | |
a4eaf7f1 PZ |
11175 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
11176 | { | |
11177 | if (flags & PERF_EF_START) | |
11178 | cpu_clock_event_start(event, flags); | |
6a694a60 | 11179 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
11180 | |
11181 | return 0; | |
11182 | } | |
11183 | ||
11184 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
11185 | { | |
11186 | cpu_clock_event_stop(event, flags); | |
11187 | } | |
11188 | ||
b0a873eb PZ |
11189 | static void cpu_clock_event_read(struct perf_event *event) |
11190 | { | |
11191 | cpu_clock_event_update(event); | |
11192 | } | |
f344011c | 11193 | |
b0a873eb PZ |
11194 | static int cpu_clock_event_init(struct perf_event *event) |
11195 | { | |
0d6d062c | 11196 | if (event->attr.type != perf_cpu_clock.type) |
b0a873eb PZ |
11197 | return -ENOENT; |
11198 | ||
11199 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
11200 | return -ENOENT; | |
11201 | ||
2481c5fa SE |
11202 | /* |
11203 | * no branch sampling for software events | |
11204 | */ | |
11205 | if (has_branch_stack(event)) | |
11206 | return -EOPNOTSUPP; | |
11207 | ||
ba3dd36c PZ |
11208 | perf_swevent_init_hrtimer(event); |
11209 | ||
b0a873eb | 11210 | return 0; |
f29ac756 PZ |
11211 | } |
11212 | ||
b0a873eb | 11213 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
11214 | .task_ctx_nr = perf_sw_context, |
11215 | ||
34f43927 | 11216 | .capabilities = PERF_PMU_CAP_NO_NMI, |
0d6d062c | 11217 | .dev = PMU_NULL_DEV, |
34f43927 | 11218 | |
b0a873eb | 11219 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
11220 | .add = cpu_clock_event_add, |
11221 | .del = cpu_clock_event_del, | |
11222 | .start = cpu_clock_event_start, | |
11223 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
11224 | .read = cpu_clock_event_read, |
11225 | }; | |
11226 | ||
11227 | /* | |
11228 | * Software event: task time clock | |
11229 | */ | |
11230 | ||
11231 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 11232 | { |
b0a873eb PZ |
11233 | u64 prev; |
11234 | s64 delta; | |
5c92d124 | 11235 | |
b0a873eb PZ |
11236 | prev = local64_xchg(&event->hw.prev_count, now); |
11237 | delta = now - prev; | |
11238 | local64_add(delta, &event->count); | |
11239 | } | |
5c92d124 | 11240 | |
a4eaf7f1 | 11241 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 11242 | { |
a4eaf7f1 | 11243 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 11244 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
11245 | } |
11246 | ||
a4eaf7f1 | 11247 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
11248 | { |
11249 | perf_swevent_cancel_hrtimer(event); | |
11250 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
11251 | } |
11252 | ||
11253 | static int task_clock_event_add(struct perf_event *event, int flags) | |
11254 | { | |
11255 | if (flags & PERF_EF_START) | |
11256 | task_clock_event_start(event, flags); | |
6a694a60 | 11257 | perf_event_update_userpage(event); |
b0a873eb | 11258 | |
a4eaf7f1 PZ |
11259 | return 0; |
11260 | } | |
11261 | ||
11262 | static void task_clock_event_del(struct perf_event *event, int flags) | |
11263 | { | |
11264 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
11265 | } |
11266 | ||
11267 | static void task_clock_event_read(struct perf_event *event) | |
11268 | { | |
768a06e2 PZ |
11269 | u64 now = perf_clock(); |
11270 | u64 delta = now - event->ctx->timestamp; | |
11271 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
11272 | |
11273 | task_clock_event_update(event, time); | |
11274 | } | |
11275 | ||
11276 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 11277 | { |
0d6d062c | 11278 | if (event->attr.type != perf_task_clock.type) |
b0a873eb PZ |
11279 | return -ENOENT; |
11280 | ||
11281 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
11282 | return -ENOENT; | |
11283 | ||
2481c5fa SE |
11284 | /* |
11285 | * no branch sampling for software events | |
11286 | */ | |
11287 | if (has_branch_stack(event)) | |
11288 | return -EOPNOTSUPP; | |
11289 | ||
ba3dd36c PZ |
11290 | perf_swevent_init_hrtimer(event); |
11291 | ||
b0a873eb | 11292 | return 0; |
6fb2915d LZ |
11293 | } |
11294 | ||
b0a873eb | 11295 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
11296 | .task_ctx_nr = perf_sw_context, |
11297 | ||
34f43927 | 11298 | .capabilities = PERF_PMU_CAP_NO_NMI, |
0d6d062c | 11299 | .dev = PMU_NULL_DEV, |
34f43927 | 11300 | |
b0a873eb | 11301 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
11302 | .add = task_clock_event_add, |
11303 | .del = task_clock_event_del, | |
11304 | .start = task_clock_event_start, | |
11305 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
11306 | .read = task_clock_event_read, |
11307 | }; | |
6fb2915d | 11308 | |
ad5133b7 | 11309 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 11310 | { |
e077df4f | 11311 | } |
6fb2915d | 11312 | |
fbbe0701 SB |
11313 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
11314 | { | |
11315 | } | |
11316 | ||
ad5133b7 | 11317 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 11318 | { |
ad5133b7 | 11319 | return 0; |
6fb2915d LZ |
11320 | } |
11321 | ||
81ec3f3c JO |
11322 | static int perf_event_nop_int(struct perf_event *event, u64 value) |
11323 | { | |
11324 | return 0; | |
11325 | } | |
11326 | ||
18ab2cd3 | 11327 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
11328 | |
11329 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 11330 | { |
fbbe0701 SB |
11331 | __this_cpu_write(nop_txn_flags, flags); |
11332 | ||
11333 | if (flags & ~PERF_PMU_TXN_ADD) | |
11334 | return; | |
11335 | ||
ad5133b7 | 11336 | perf_pmu_disable(pmu); |
6fb2915d LZ |
11337 | } |
11338 | ||
ad5133b7 PZ |
11339 | static int perf_pmu_commit_txn(struct pmu *pmu) |
11340 | { | |
fbbe0701 SB |
11341 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
11342 | ||
11343 | __this_cpu_write(nop_txn_flags, 0); | |
11344 | ||
11345 | if (flags & ~PERF_PMU_TXN_ADD) | |
11346 | return 0; | |
11347 | ||
ad5133b7 PZ |
11348 | perf_pmu_enable(pmu); |
11349 | return 0; | |
11350 | } | |
e077df4f | 11351 | |
ad5133b7 | 11352 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 11353 | { |
fbbe0701 SB |
11354 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
11355 | ||
11356 | __this_cpu_write(nop_txn_flags, 0); | |
11357 | ||
11358 | if (flags & ~PERF_PMU_TXN_ADD) | |
11359 | return; | |
11360 | ||
ad5133b7 | 11361 | perf_pmu_enable(pmu); |
24f1e32c FW |
11362 | } |
11363 | ||
35edc2a5 PZ |
11364 | static int perf_event_idx_default(struct perf_event *event) |
11365 | { | |
c719f560 | 11366 | return 0; |
35edc2a5 PZ |
11367 | } |
11368 | ||
51676957 PZ |
11369 | static void free_pmu_context(struct pmu *pmu) |
11370 | { | |
bd275681 | 11371 | free_percpu(pmu->cpu_pmu_context); |
24f1e32c | 11372 | } |
6e855cd4 | 11373 | |
8dc85d54 | 11374 | /* |
6e855cd4 | 11375 | * Let userspace know that this PMU supports address range filtering: |
8dc85d54 | 11376 | */ |
6e855cd4 AS |
11377 | static ssize_t nr_addr_filters_show(struct device *dev, |
11378 | struct device_attribute *attr, | |
11379 | char *page) | |
24f1e32c | 11380 | { |
6e855cd4 AS |
11381 | struct pmu *pmu = dev_get_drvdata(dev); |
11382 | ||
dca6344d | 11383 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); |
6e855cd4 AS |
11384 | } |
11385 | DEVICE_ATTR_RO(nr_addr_filters); | |
11386 | ||
2e80a82a | 11387 | static struct idr pmu_idr; |
d6d020e9 | 11388 | |
abe43400 PZ |
11389 | static ssize_t |
11390 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
11391 | { | |
11392 | struct pmu *pmu = dev_get_drvdata(dev); | |
11393 | ||
dca6344d | 11394 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->type); |
abe43400 | 11395 | } |
90826ca7 | 11396 | static DEVICE_ATTR_RO(type); |
abe43400 | 11397 | |
62b85639 SE |
11398 | static ssize_t |
11399 | perf_event_mux_interval_ms_show(struct device *dev, | |
11400 | struct device_attribute *attr, | |
11401 | char *page) | |
11402 | { | |
11403 | struct pmu *pmu = dev_get_drvdata(dev); | |
11404 | ||
dca6344d | 11405 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->hrtimer_interval_ms); |
62b85639 SE |
11406 | } |
11407 | ||
272325c4 PZ |
11408 | static DEFINE_MUTEX(mux_interval_mutex); |
11409 | ||
62b85639 SE |
11410 | static ssize_t |
11411 | perf_event_mux_interval_ms_store(struct device *dev, | |
11412 | struct device_attribute *attr, | |
11413 | const char *buf, size_t count) | |
11414 | { | |
11415 | struct pmu *pmu = dev_get_drvdata(dev); | |
11416 | int timer, cpu, ret; | |
11417 | ||
11418 | ret = kstrtoint(buf, 0, &timer); | |
11419 | if (ret) | |
11420 | return ret; | |
11421 | ||
11422 | if (timer < 1) | |
11423 | return -EINVAL; | |
11424 | ||
11425 | /* same value, noting to do */ | |
11426 | if (timer == pmu->hrtimer_interval_ms) | |
11427 | return count; | |
11428 | ||
272325c4 | 11429 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
11430 | pmu->hrtimer_interval_ms = timer; |
11431 | ||
11432 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 11433 | cpus_read_lock(); |
272325c4 | 11434 | for_each_online_cpu(cpu) { |
bd275681 PZ |
11435 | struct perf_cpu_pmu_context *cpc; |
11436 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); | |
11437 | cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
62b85639 | 11438 | |
1af6239d | 11439 | cpu_function_call(cpu, perf_mux_hrtimer_restart_ipi, cpc); |
62b85639 | 11440 | } |
a63fbed7 | 11441 | cpus_read_unlock(); |
272325c4 | 11442 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
11443 | |
11444 | return count; | |
11445 | } | |
90826ca7 | 11446 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 11447 | |
90826ca7 GKH |
11448 | static struct attribute *pmu_dev_attrs[] = { |
11449 | &dev_attr_type.attr, | |
11450 | &dev_attr_perf_event_mux_interval_ms.attr, | |
652ffc21 GK |
11451 | &dev_attr_nr_addr_filters.attr, |
11452 | NULL, | |
11453 | }; | |
11454 | ||
11455 | static umode_t pmu_dev_is_visible(struct kobject *kobj, struct attribute *a, int n) | |
11456 | { | |
11457 | struct device *dev = kobj_to_dev(kobj); | |
11458 | struct pmu *pmu = dev_get_drvdata(dev); | |
11459 | ||
388a1fb7 | 11460 | if (n == 2 && !pmu->nr_addr_filters) |
652ffc21 GK |
11461 | return 0; |
11462 | ||
11463 | return a->mode; | |
652ffc21 GK |
11464 | } |
11465 | ||
11466 | static struct attribute_group pmu_dev_attr_group = { | |
11467 | .is_visible = pmu_dev_is_visible, | |
11468 | .attrs = pmu_dev_attrs, | |
11469 | }; | |
11470 | ||
11471 | static const struct attribute_group *pmu_dev_groups[] = { | |
11472 | &pmu_dev_attr_group, | |
90826ca7 | 11473 | NULL, |
abe43400 PZ |
11474 | }; |
11475 | ||
11476 | static int pmu_bus_running; | |
11477 | static struct bus_type pmu_bus = { | |
11478 | .name = "event_source", | |
90826ca7 | 11479 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
11480 | }; |
11481 | ||
11482 | static void pmu_dev_release(struct device *dev) | |
11483 | { | |
11484 | kfree(dev); | |
11485 | } | |
11486 | ||
11487 | static int pmu_dev_alloc(struct pmu *pmu) | |
11488 | { | |
11489 | int ret = -ENOMEM; | |
11490 | ||
11491 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
11492 | if (!pmu->dev) | |
11493 | goto out; | |
11494 | ||
0c9d42ed | 11495 | pmu->dev->groups = pmu->attr_groups; |
abe43400 | 11496 | device_initialize(pmu->dev); |
abe43400 PZ |
11497 | |
11498 | dev_set_drvdata(pmu->dev, pmu); | |
11499 | pmu->dev->bus = &pmu_bus; | |
143f83e2 | 11500 | pmu->dev->parent = pmu->parent; |
abe43400 | 11501 | pmu->dev->release = pmu_dev_release; |
e8d7a90c CZ |
11502 | |
11503 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
11504 | if (ret) | |
11505 | goto free_dev; | |
11506 | ||
abe43400 PZ |
11507 | ret = device_add(pmu->dev); |
11508 | if (ret) | |
11509 | goto free_dev; | |
11510 | ||
652ffc21 | 11511 | if (pmu->attr_update) { |
f3a3a825 | 11512 | ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update); |
652ffc21 GK |
11513 | if (ret) |
11514 | goto del_dev; | |
11515 | } | |
f3a3a825 | 11516 | |
abe43400 PZ |
11517 | out: |
11518 | return ret; | |
11519 | ||
6e855cd4 AS |
11520 | del_dev: |
11521 | device_del(pmu->dev); | |
11522 | ||
abe43400 PZ |
11523 | free_dev: |
11524 | put_device(pmu->dev); | |
11525 | goto out; | |
11526 | } | |
11527 | ||
547e9fd7 | 11528 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 11529 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 11530 | |
03d8e80b | 11531 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 11532 | { |
66d258c5 | 11533 | int cpu, ret, max = PERF_TYPE_MAX; |
24f1e32c | 11534 | |
b0a873eb | 11535 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
11536 | ret = -ENOMEM; |
11537 | pmu->pmu_disable_count = alloc_percpu(int); | |
11538 | if (!pmu->pmu_disable_count) | |
11539 | goto unlock; | |
f29ac756 | 11540 | |
2e80a82a | 11541 | pmu->type = -1; |
0d6d062c RB |
11542 | if (WARN_ONCE(!name, "Can not register anonymous pmu.\n")) { |
11543 | ret = -EINVAL; | |
11544 | goto free_pdc; | |
11545 | } | |
11546 | ||
2e80a82a PZ |
11547 | pmu->name = name; |
11548 | ||
0d6d062c RB |
11549 | if (type >= 0) |
11550 | max = type; | |
66d258c5 | 11551 | |
0d6d062c RB |
11552 | ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL); |
11553 | if (ret < 0) | |
11554 | goto free_pdc; | |
66d258c5 | 11555 | |
0d6d062c | 11556 | WARN_ON(type >= 0 && ret != type); |
66d258c5 | 11557 | |
0d6d062c | 11558 | type = ret; |
2e80a82a PZ |
11559 | pmu->type = type; |
11560 | ||
0d6d062c | 11561 | if (pmu_bus_running && !pmu->dev) { |
abe43400 PZ |
11562 | ret = pmu_dev_alloc(pmu); |
11563 | if (ret) | |
11564 | goto free_idr; | |
11565 | } | |
11566 | ||
c4814202 | 11567 | ret = -ENOMEM; |
bd275681 PZ |
11568 | pmu->cpu_pmu_context = alloc_percpu(struct perf_cpu_pmu_context); |
11569 | if (!pmu->cpu_pmu_context) | |
abe43400 | 11570 | goto free_dev; |
f344011c | 11571 | |
108b02cf | 11572 | for_each_possible_cpu(cpu) { |
bd275681 | 11573 | struct perf_cpu_pmu_context *cpc; |
9e630205 | 11574 | |
bd275681 PZ |
11575 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); |
11576 | __perf_init_event_pmu_context(&cpc->epc, pmu); | |
11577 | __perf_mux_hrtimer_init(cpc, cpu); | |
108b02cf | 11578 | } |
76e1d904 | 11579 | |
ad5133b7 PZ |
11580 | if (!pmu->start_txn) { |
11581 | if (pmu->pmu_enable) { | |
11582 | /* | |
11583 | * If we have pmu_enable/pmu_disable calls, install | |
11584 | * transaction stubs that use that to try and batch | |
11585 | * hardware accesses. | |
11586 | */ | |
11587 | pmu->start_txn = perf_pmu_start_txn; | |
11588 | pmu->commit_txn = perf_pmu_commit_txn; | |
11589 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
11590 | } else { | |
fbbe0701 | 11591 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
11592 | pmu->commit_txn = perf_pmu_nop_int; |
11593 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 11594 | } |
5c92d124 | 11595 | } |
15dbf27c | 11596 | |
ad5133b7 PZ |
11597 | if (!pmu->pmu_enable) { |
11598 | pmu->pmu_enable = perf_pmu_nop_void; | |
11599 | pmu->pmu_disable = perf_pmu_nop_void; | |
11600 | } | |
11601 | ||
81ec3f3c JO |
11602 | if (!pmu->check_period) |
11603 | pmu->check_period = perf_event_nop_int; | |
11604 | ||
35edc2a5 PZ |
11605 | if (!pmu->event_idx) |
11606 | pmu->event_idx = perf_event_idx_default; | |
11607 | ||
0d6d062c | 11608 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 11609 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
11610 | ret = 0; |
11611 | unlock: | |
b0a873eb PZ |
11612 | mutex_unlock(&pmus_lock); |
11613 | ||
33696fc0 | 11614 | return ret; |
108b02cf | 11615 | |
abe43400 | 11616 | free_dev: |
0d6d062c RB |
11617 | if (pmu->dev && pmu->dev != PMU_NULL_DEV) { |
11618 | device_del(pmu->dev); | |
11619 | put_device(pmu->dev); | |
11620 | } | |
abe43400 | 11621 | |
2e80a82a | 11622 | free_idr: |
0d6d062c | 11623 | idr_remove(&pmu_idr, pmu->type); |
2e80a82a | 11624 | |
108b02cf PZ |
11625 | free_pdc: |
11626 | free_percpu(pmu->pmu_disable_count); | |
11627 | goto unlock; | |
f29ac756 | 11628 | } |
c464c76e | 11629 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 11630 | |
b0a873eb | 11631 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 11632 | { |
b0a873eb PZ |
11633 | mutex_lock(&pmus_lock); |
11634 | list_del_rcu(&pmu->entry); | |
5c92d124 | 11635 | |
0475f9ea | 11636 | /* |
cde8e884 PZ |
11637 | * We dereference the pmu list under both SRCU and regular RCU, so |
11638 | * synchronize against both of those. | |
0475f9ea | 11639 | */ |
b0a873eb | 11640 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 11641 | synchronize_rcu(); |
d6d020e9 | 11642 | |
33696fc0 | 11643 | free_percpu(pmu->pmu_disable_count); |
0d6d062c RB |
11644 | idr_remove(&pmu_idr, pmu->type); |
11645 | if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) { | |
0933840a JO |
11646 | if (pmu->nr_addr_filters) |
11647 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
11648 | device_del(pmu->dev); | |
11649 | put_device(pmu->dev); | |
11650 | } | |
51676957 | 11651 | free_pmu_context(pmu); |
a9f97721 | 11652 | mutex_unlock(&pmus_lock); |
b0a873eb | 11653 | } |
c464c76e | 11654 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 11655 | |
e321d02d KL |
11656 | static inline bool has_extended_regs(struct perf_event *event) |
11657 | { | |
11658 | return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || | |
11659 | (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); | |
11660 | } | |
11661 | ||
cc34b98b MR |
11662 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
11663 | { | |
ccd41c86 | 11664 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
11665 | int ret; |
11666 | ||
11667 | if (!try_module_get(pmu->module)) | |
11668 | return -ENODEV; | |
ccd41c86 | 11669 | |
0c7296ca PZ |
11670 | /* |
11671 | * A number of pmu->event_init() methods iterate the sibling_list to, | |
11672 | * for example, validate if the group fits on the PMU. Therefore, | |
11673 | * if this is a sibling event, acquire the ctx->mutex to protect | |
11674 | * the sibling_list. | |
11675 | */ | |
11676 | if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { | |
8b10c5e2 PZ |
11677 | /* |
11678 | * This ctx->mutex can nest when we're called through | |
11679 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
11680 | */ | |
11681 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
11682 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
11683 | BUG_ON(!ctx); |
11684 | } | |
11685 | ||
cc34b98b MR |
11686 | event->pmu = pmu; |
11687 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
11688 | |
11689 | if (ctx) | |
11690 | perf_event_ctx_unlock(event->group_leader, ctx); | |
11691 | ||
cc6795ae | 11692 | if (!ret) { |
e321d02d KL |
11693 | if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && |
11694 | has_extended_regs(event)) | |
11695 | ret = -EOPNOTSUPP; | |
11696 | ||
cc6795ae | 11697 | if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE && |
e321d02d | 11698 | event_has_any_exclude_flag(event)) |
cc6795ae | 11699 | ret = -EINVAL; |
e321d02d KL |
11700 | |
11701 | if (ret && event->destroy) | |
11702 | event->destroy(event); | |
cc6795ae AM |
11703 | } |
11704 | ||
cc34b98b MR |
11705 | if (ret) |
11706 | module_put(pmu->module); | |
11707 | ||
11708 | return ret; | |
11709 | } | |
11710 | ||
18ab2cd3 | 11711 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 11712 | { |
55bcf6ef | 11713 | bool extended_type = false; |
66d258c5 | 11714 | int idx, type, ret; |
85c617ab | 11715 | struct pmu *pmu; |
b0a873eb PZ |
11716 | |
11717 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 11718 | |
0d6d062c RB |
11719 | /* |
11720 | * Save original type before calling pmu->event_init() since certain | |
11721 | * pmus overwrites event->attr.type to forward event to another pmu. | |
11722 | */ | |
11723 | event->orig_type = event->attr.type; | |
11724 | ||
40999312 KL |
11725 | /* Try parent's PMU first: */ |
11726 | if (event->parent && event->parent->pmu) { | |
11727 | pmu = event->parent->pmu; | |
11728 | ret = perf_try_init_event(pmu, event); | |
11729 | if (!ret) | |
11730 | goto unlock; | |
11731 | } | |
11732 | ||
66d258c5 PZ |
11733 | /* |
11734 | * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE | |
11735 | * are often aliases for PERF_TYPE_RAW. | |
11736 | */ | |
11737 | type = event->attr.type; | |
55bcf6ef KL |
11738 | if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) { |
11739 | type = event->attr.config >> PERF_PMU_TYPE_SHIFT; | |
11740 | if (!type) { | |
11741 | type = PERF_TYPE_RAW; | |
11742 | } else { | |
11743 | extended_type = true; | |
11744 | event->attr.config &= PERF_HW_EVENT_MASK; | |
11745 | } | |
11746 | } | |
66d258c5 PZ |
11747 | |
11748 | again: | |
2e80a82a | 11749 | rcu_read_lock(); |
66d258c5 | 11750 | pmu = idr_find(&pmu_idr, type); |
2e80a82a | 11751 | rcu_read_unlock(); |
940c5b29 | 11752 | if (pmu) { |
55bcf6ef KL |
11753 | if (event->attr.type != type && type != PERF_TYPE_RAW && |
11754 | !(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE)) | |
11755 | goto fail; | |
11756 | ||
cc34b98b | 11757 | ret = perf_try_init_event(pmu, event); |
55bcf6ef | 11758 | if (ret == -ENOENT && event->attr.type != type && !extended_type) { |
66d258c5 PZ |
11759 | type = event->attr.type; |
11760 | goto again; | |
11761 | } | |
11762 | ||
940c5b29 LM |
11763 | if (ret) |
11764 | pmu = ERR_PTR(ret); | |
66d258c5 | 11765 | |
2e80a82a | 11766 | goto unlock; |
940c5b29 | 11767 | } |
2e80a82a | 11768 | |
9f0bff11 | 11769 | list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) { |
cc34b98b | 11770 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 11771 | if (!ret) |
e5f4d339 | 11772 | goto unlock; |
76e1d904 | 11773 | |
b0a873eb PZ |
11774 | if (ret != -ENOENT) { |
11775 | pmu = ERR_PTR(ret); | |
e5f4d339 | 11776 | goto unlock; |
f344011c | 11777 | } |
5c92d124 | 11778 | } |
55bcf6ef | 11779 | fail: |
e5f4d339 PZ |
11780 | pmu = ERR_PTR(-ENOENT); |
11781 | unlock: | |
b0a873eb | 11782 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 11783 | |
4aeb0b42 | 11784 | return pmu; |
5c92d124 IM |
11785 | } |
11786 | ||
f2fb6bef KL |
11787 | static void attach_sb_event(struct perf_event *event) |
11788 | { | |
11789 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
11790 | ||
11791 | raw_spin_lock(&pel->lock); | |
11792 | list_add_rcu(&event->sb_list, &pel->list); | |
11793 | raw_spin_unlock(&pel->lock); | |
11794 | } | |
11795 | ||
aab5b71e PZ |
11796 | /* |
11797 | * We keep a list of all !task (and therefore per-cpu) events | |
11798 | * that need to receive side-band records. | |
11799 | * | |
11800 | * This avoids having to scan all the various PMU per-cpu contexts | |
11801 | * looking for them. | |
11802 | */ | |
f2fb6bef KL |
11803 | static void account_pmu_sb_event(struct perf_event *event) |
11804 | { | |
a4f144eb | 11805 | if (is_sb_event(event)) |
f2fb6bef KL |
11806 | attach_sb_event(event); |
11807 | } | |
11808 | ||
555e0c1e FW |
11809 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
11810 | static void account_freq_event_nohz(void) | |
11811 | { | |
11812 | #ifdef CONFIG_NO_HZ_FULL | |
11813 | /* Lock so we don't race with concurrent unaccount */ | |
11814 | spin_lock(&nr_freq_lock); | |
11815 | if (atomic_inc_return(&nr_freq_events) == 1) | |
11816 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
11817 | spin_unlock(&nr_freq_lock); | |
11818 | #endif | |
11819 | } | |
11820 | ||
11821 | static void account_freq_event(void) | |
11822 | { | |
11823 | if (tick_nohz_full_enabled()) | |
11824 | account_freq_event_nohz(); | |
11825 | else | |
11826 | atomic_inc(&nr_freq_events); | |
11827 | } | |
11828 | ||
11829 | ||
766d6c07 FW |
11830 | static void account_event(struct perf_event *event) |
11831 | { | |
25432ae9 PZ |
11832 | bool inc = false; |
11833 | ||
4beb31f3 FW |
11834 | if (event->parent) |
11835 | return; | |
11836 | ||
a5398bff | 11837 | if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) |
25432ae9 | 11838 | inc = true; |
766d6c07 FW |
11839 | if (event->attr.mmap || event->attr.mmap_data) |
11840 | atomic_inc(&nr_mmap_events); | |
88a16a13 JO |
11841 | if (event->attr.build_id) |
11842 | atomic_inc(&nr_build_id_events); | |
766d6c07 FW |
11843 | if (event->attr.comm) |
11844 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
11845 | if (event->attr.namespaces) |
11846 | atomic_inc(&nr_namespaces_events); | |
96aaab68 NK |
11847 | if (event->attr.cgroup) |
11848 | atomic_inc(&nr_cgroup_events); | |
766d6c07 FW |
11849 | if (event->attr.task) |
11850 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
11851 | if (event->attr.freq) |
11852 | account_freq_event(); | |
45ac1403 AH |
11853 | if (event->attr.context_switch) { |
11854 | atomic_inc(&nr_switch_events); | |
25432ae9 | 11855 | inc = true; |
45ac1403 | 11856 | } |
4beb31f3 | 11857 | if (has_branch_stack(event)) |
25432ae9 | 11858 | inc = true; |
4beb31f3 | 11859 | if (is_cgroup_event(event)) |
25432ae9 | 11860 | inc = true; |
76193a94 SL |
11861 | if (event->attr.ksymbol) |
11862 | atomic_inc(&nr_ksymbol_events); | |
6ee52e2a SL |
11863 | if (event->attr.bpf_event) |
11864 | atomic_inc(&nr_bpf_events); | |
e17d43b9 AH |
11865 | if (event->attr.text_poke) |
11866 | atomic_inc(&nr_text_poke_events); | |
25432ae9 | 11867 | |
9107c89e | 11868 | if (inc) { |
5bce9db1 AS |
11869 | /* |
11870 | * We need the mutex here because static_branch_enable() | |
11871 | * must complete *before* the perf_sched_count increment | |
11872 | * becomes visible. | |
11873 | */ | |
9107c89e PZ |
11874 | if (atomic_inc_not_zero(&perf_sched_count)) |
11875 | goto enabled; | |
11876 | ||
11877 | mutex_lock(&perf_sched_mutex); | |
11878 | if (!atomic_read(&perf_sched_count)) { | |
11879 | static_branch_enable(&perf_sched_events); | |
11880 | /* | |
11881 | * Guarantee that all CPUs observe they key change and | |
11882 | * call the perf scheduling hooks before proceeding to | |
11883 | * install events that need them. | |
11884 | */ | |
0809d954 | 11885 | synchronize_rcu(); |
9107c89e PZ |
11886 | } |
11887 | /* | |
11888 | * Now that we have waited for the sync_sched(), allow further | |
11889 | * increments to by-pass the mutex. | |
11890 | */ | |
11891 | atomic_inc(&perf_sched_count); | |
11892 | mutex_unlock(&perf_sched_mutex); | |
11893 | } | |
11894 | enabled: | |
4beb31f3 | 11895 | |
f2fb6bef | 11896 | account_pmu_sb_event(event); |
766d6c07 FW |
11897 | } |
11898 | ||
0793a61d | 11899 | /* |
788faab7 | 11900 | * Allocate and initialize an event structure |
0793a61d | 11901 | */ |
cdd6c482 | 11902 | static struct perf_event * |
c3f00c70 | 11903 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
11904 | struct task_struct *task, |
11905 | struct perf_event *group_leader, | |
11906 | struct perf_event *parent_event, | |
4dc0da86 | 11907 | perf_overflow_handler_t overflow_handler, |
79dff51e | 11908 | void *context, int cgroup_fd) |
0793a61d | 11909 | { |
51b0fe39 | 11910 | struct pmu *pmu; |
cdd6c482 IM |
11911 | struct perf_event *event; |
11912 | struct hw_perf_event *hwc; | |
90983b16 | 11913 | long err = -EINVAL; |
ff65338e | 11914 | int node; |
0793a61d | 11915 | |
66832eb4 ON |
11916 | if ((unsigned)cpu >= nr_cpu_ids) { |
11917 | if (!task || cpu != -1) | |
11918 | return ERR_PTR(-EINVAL); | |
11919 | } | |
97ba62b2 ME |
11920 | if (attr->sigtrap && !task) { |
11921 | /* Requires a task: avoid signalling random tasks. */ | |
11922 | return ERR_PTR(-EINVAL); | |
11923 | } | |
66832eb4 | 11924 | |
ff65338e NK |
11925 | node = (cpu >= 0) ? cpu_to_node(cpu) : -1; |
11926 | event = kmem_cache_alloc_node(perf_event_cache, GFP_KERNEL | __GFP_ZERO, | |
11927 | node); | |
cdd6c482 | 11928 | if (!event) |
d5d2bc0d | 11929 | return ERR_PTR(-ENOMEM); |
0793a61d | 11930 | |
04289bb9 | 11931 | /* |
cdd6c482 | 11932 | * Single events are their own group leaders, with an |
04289bb9 IM |
11933 | * empty sibling list: |
11934 | */ | |
11935 | if (!group_leader) | |
cdd6c482 | 11936 | group_leader = event; |
04289bb9 | 11937 | |
cdd6c482 IM |
11938 | mutex_init(&event->child_mutex); |
11939 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 11940 | |
cdd6c482 IM |
11941 | INIT_LIST_HEAD(&event->event_entry); |
11942 | INIT_LIST_HEAD(&event->sibling_list); | |
6668128a | 11943 | INIT_LIST_HEAD(&event->active_list); |
8e1a2031 | 11944 | init_event_group(event); |
10c6db11 | 11945 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 11946 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 11947 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
11948 | INIT_HLIST_NODE(&event->hlist_entry); |
11949 | ||
10c6db11 | 11950 | |
cdd6c482 | 11951 | init_waitqueue_head(&event->waitq); |
ca6c2132 PZ |
11952 | init_irq_work(&event->pending_irq, perf_pending_irq); |
11953 | init_task_work(&event->pending_task, perf_pending_task); | |
0793a61d | 11954 | |
cdd6c482 | 11955 | mutex_init(&event->mmap_mutex); |
375637bc | 11956 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 11957 | |
a6fa941d | 11958 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
11959 | event->cpu = cpu; |
11960 | event->attr = *attr; | |
11961 | event->group_leader = group_leader; | |
11962 | event->pmu = NULL; | |
cdd6c482 | 11963 | event->oncpu = -1; |
a96bbc16 | 11964 | |
cdd6c482 | 11965 | event->parent = parent_event; |
b84fbc9f | 11966 | |
17cf22c3 | 11967 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 11968 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 11969 | |
cdd6c482 | 11970 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 11971 | |
e3265a43 NK |
11972 | if (parent_event) |
11973 | event->event_caps = parent_event->event_caps; | |
11974 | ||
d580ff86 PZ |
11975 | if (task) { |
11976 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 11977 | /* |
50f16a8b PZ |
11978 | * XXX pmu::event_init needs to know what task to account to |
11979 | * and we cannot use the ctx information because we need the | |
11980 | * pmu before we get a ctx. | |
d580ff86 | 11981 | */ |
7b3c92b8 | 11982 | event->hw.target = get_task_struct(task); |
d580ff86 PZ |
11983 | } |
11984 | ||
34f43927 PZ |
11985 | event->clock = &local_clock; |
11986 | if (parent_event) | |
11987 | event->clock = parent_event->clock; | |
11988 | ||
4dc0da86 | 11989 | if (!overflow_handler && parent_event) { |
b326e956 | 11990 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 11991 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 11992 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c | 11993 | if (overflow_handler == bpf_overflow_handler) { |
85192dbf | 11994 | struct bpf_prog *prog = parent_event->prog; |
aa6a5f3c | 11995 | |
85192dbf | 11996 | bpf_prog_inc(prog); |
aa6a5f3c AS |
11997 | event->prog = prog; |
11998 | event->orig_overflow_handler = | |
11999 | parent_event->orig_overflow_handler; | |
12000 | } | |
12001 | #endif | |
4dc0da86 | 12002 | } |
66832eb4 | 12003 | |
1879445d WN |
12004 | if (overflow_handler) { |
12005 | event->overflow_handler = overflow_handler; | |
12006 | event->overflow_handler_context = context; | |
9ecda41a WN |
12007 | } else if (is_write_backward(event)){ |
12008 | event->overflow_handler = perf_event_output_backward; | |
12009 | event->overflow_handler_context = NULL; | |
1879445d | 12010 | } else { |
9ecda41a | 12011 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
12012 | event->overflow_handler_context = NULL; |
12013 | } | |
97eaf530 | 12014 | |
0231bb53 | 12015 | perf_event__state_init(event); |
a86ed508 | 12016 | |
4aeb0b42 | 12017 | pmu = NULL; |
b8e83514 | 12018 | |
cdd6c482 | 12019 | hwc = &event->hw; |
bd2b5b12 | 12020 | hwc->sample_period = attr->sample_period; |
0d48696f | 12021 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 12022 | hwc->sample_period = 1; |
eced1dfc | 12023 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 12024 | |
e7850595 | 12025 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 12026 | |
2023b359 | 12027 | /* |
ba5213ae PZ |
12028 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
12029 | * See perf_output_read(). | |
2023b359 | 12030 | */ |
ba5213ae | 12031 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 12032 | goto err_ns; |
a46a2300 YZ |
12033 | |
12034 | if (!has_branch_stack(event)) | |
12035 | event->attr.branch_sample_type = 0; | |
2023b359 | 12036 | |
b0a873eb | 12037 | pmu = perf_init_event(event); |
85c617ab | 12038 | if (IS_ERR(pmu)) { |
4aeb0b42 | 12039 | err = PTR_ERR(pmu); |
90983b16 | 12040 | goto err_ns; |
621a01ea | 12041 | } |
d5d2bc0d | 12042 | |
09f4e8f0 | 12043 | /* |
bd275681 PZ |
12044 | * Disallow uncore-task events. Similarly, disallow uncore-cgroup |
12045 | * events (they don't make sense as the cgroup will be different | |
12046 | * on other CPUs in the uncore mask). | |
09f4e8f0 | 12047 | */ |
bd275681 | 12048 | if (pmu->task_ctx_nr == perf_invalid_context && (task || cgroup_fd != -1)) { |
09f4e8f0 PZ |
12049 | err = -EINVAL; |
12050 | goto err_pmu; | |
12051 | } | |
12052 | ||
ab43762e AS |
12053 | if (event->attr.aux_output && |
12054 | !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) { | |
12055 | err = -EOPNOTSUPP; | |
12056 | goto err_pmu; | |
12057 | } | |
12058 | ||
98add2af PZ |
12059 | if (cgroup_fd != -1) { |
12060 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
12061 | if (err) | |
12062 | goto err_pmu; | |
12063 | } | |
12064 | ||
bed5b25a AS |
12065 | err = exclusive_event_init(event); |
12066 | if (err) | |
12067 | goto err_pmu; | |
12068 | ||
375637bc | 12069 | if (has_addr_filter(event)) { |
c60f83b8 AS |
12070 | event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters, |
12071 | sizeof(struct perf_addr_filter_range), | |
12072 | GFP_KERNEL); | |
12073 | if (!event->addr_filter_ranges) { | |
36cc2b92 | 12074 | err = -ENOMEM; |
375637bc | 12075 | goto err_per_task; |
36cc2b92 | 12076 | } |
375637bc | 12077 | |
18736eef AS |
12078 | /* |
12079 | * Clone the parent's vma offsets: they are valid until exec() | |
12080 | * even if the mm is not shared with the parent. | |
12081 | */ | |
12082 | if (event->parent) { | |
12083 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
12084 | ||
12085 | raw_spin_lock_irq(&ifh->lock); | |
c60f83b8 AS |
12086 | memcpy(event->addr_filter_ranges, |
12087 | event->parent->addr_filter_ranges, | |
12088 | pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range)); | |
18736eef AS |
12089 | raw_spin_unlock_irq(&ifh->lock); |
12090 | } | |
12091 | ||
375637bc AS |
12092 | /* force hw sync on the address filters */ |
12093 | event->addr_filters_gen = 1; | |
12094 | } | |
12095 | ||
cdd6c482 | 12096 | if (!event->parent) { |
927c7a9e | 12097 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 12098 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 12099 | if (err) |
375637bc | 12100 | goto err_addr_filters; |
d010b332 | 12101 | } |
f344011c | 12102 | } |
9ee318a7 | 12103 | |
da97e184 JFG |
12104 | err = security_perf_event_alloc(event); |
12105 | if (err) | |
12106 | goto err_callchain_buffer; | |
12107 | ||
927a5570 AS |
12108 | /* symmetric to unaccount_event() in _free_event() */ |
12109 | account_event(event); | |
12110 | ||
cdd6c482 | 12111 | return event; |
90983b16 | 12112 | |
da97e184 JFG |
12113 | err_callchain_buffer: |
12114 | if (!event->parent) { | |
12115 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
12116 | put_callchain_buffers(); | |
12117 | } | |
375637bc | 12118 | err_addr_filters: |
c60f83b8 | 12119 | kfree(event->addr_filter_ranges); |
375637bc | 12120 | |
bed5b25a AS |
12121 | err_per_task: |
12122 | exclusive_event_destroy(event); | |
12123 | ||
90983b16 | 12124 | err_pmu: |
98add2af PZ |
12125 | if (is_cgroup_event(event)) |
12126 | perf_detach_cgroup(event); | |
90983b16 FW |
12127 | if (event->destroy) |
12128 | event->destroy(event); | |
c464c76e | 12129 | module_put(pmu->module); |
90983b16 | 12130 | err_ns: |
621b6d2e PB |
12131 | if (event->hw.target) |
12132 | put_task_struct(event->hw.target); | |
4674ffe2 | 12133 | call_rcu(&event->rcu_head, free_event_rcu); |
90983b16 FW |
12134 | |
12135 | return ERR_PTR(err); | |
0793a61d TG |
12136 | } |
12137 | ||
cdd6c482 IM |
12138 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
12139 | struct perf_event_attr *attr) | |
974802ea | 12140 | { |
974802ea | 12141 | u32 size; |
cdf8073d | 12142 | int ret; |
974802ea | 12143 | |
c2ba8f41 | 12144 | /* Zero the full structure, so that a short copy will be nice. */ |
974802ea PZ |
12145 | memset(attr, 0, sizeof(*attr)); |
12146 | ||
12147 | ret = get_user(size, &uattr->size); | |
12148 | if (ret) | |
12149 | return ret; | |
12150 | ||
c2ba8f41 AS |
12151 | /* ABI compatibility quirk: */ |
12152 | if (!size) | |
974802ea | 12153 | size = PERF_ATTR_SIZE_VER0; |
c2ba8f41 | 12154 | if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
974802ea PZ |
12155 | goto err_size; |
12156 | ||
c2ba8f41 AS |
12157 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
12158 | if (ret) { | |
12159 | if (ret == -E2BIG) | |
12160 | goto err_size; | |
12161 | return ret; | |
974802ea PZ |
12162 | } |
12163 | ||
f12f42ac MX |
12164 | attr->size = size; |
12165 | ||
a4faf00d | 12166 | if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) |
974802ea PZ |
12167 | return -EINVAL; |
12168 | ||
12169 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
12170 | return -EINVAL; | |
12171 | ||
12172 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
12173 | return -EINVAL; | |
12174 | ||
bce38cd5 SE |
12175 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
12176 | u64 mask = attr->branch_sample_type; | |
12177 | ||
12178 | /* only using defined bits */ | |
12179 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
12180 | return -EINVAL; | |
12181 | ||
12182 | /* at least one branch bit must be set */ | |
12183 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
12184 | return -EINVAL; | |
12185 | ||
bce38cd5 SE |
12186 | /* propagate priv level, when not set for branch */ |
12187 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
12188 | ||
12189 | /* exclude_kernel checked on syscall entry */ | |
12190 | if (!attr->exclude_kernel) | |
12191 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
12192 | ||
12193 | if (!attr->exclude_user) | |
12194 | mask |= PERF_SAMPLE_BRANCH_USER; | |
12195 | ||
12196 | if (!attr->exclude_hv) | |
12197 | mask |= PERF_SAMPLE_BRANCH_HV; | |
12198 | /* | |
12199 | * adjust user setting (for HW filter setup) | |
12200 | */ | |
12201 | attr->branch_sample_type = mask; | |
12202 | } | |
e712209a | 12203 | /* privileged levels capture (kernel, hv): check permissions */ |
da97e184 JFG |
12204 | if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) { |
12205 | ret = perf_allow_kernel(attr); | |
12206 | if (ret) | |
12207 | return ret; | |
12208 | } | |
bce38cd5 | 12209 | } |
4018994f | 12210 | |
c5ebcedb | 12211 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 12212 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
12213 | if (ret) |
12214 | return ret; | |
12215 | } | |
12216 | ||
12217 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
12218 | if (!arch_perf_have_user_stack_dump()) | |
12219 | return -ENOSYS; | |
12220 | ||
12221 | /* | |
12222 | * We have __u32 type for the size, but so far | |
12223 | * we can only use __u16 as maximum due to the | |
12224 | * __u16 sample size limit. | |
12225 | */ | |
12226 | if (attr->sample_stack_user >= USHRT_MAX) | |
78b562fb | 12227 | return -EINVAL; |
c5ebcedb | 12228 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
78b562fb | 12229 | return -EINVAL; |
c5ebcedb | 12230 | } |
4018994f | 12231 | |
5f970521 JO |
12232 | if (!attr->sample_max_stack) |
12233 | attr->sample_max_stack = sysctl_perf_event_max_stack; | |
12234 | ||
60e2364e SE |
12235 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
12236 | ret = perf_reg_validate(attr->sample_regs_intr); | |
6546b19f NK |
12237 | |
12238 | #ifndef CONFIG_CGROUP_PERF | |
12239 | if (attr->sample_type & PERF_SAMPLE_CGROUP) | |
12240 | return -EINVAL; | |
12241 | #endif | |
2a6c6b7d KL |
12242 | if ((attr->sample_type & PERF_SAMPLE_WEIGHT) && |
12243 | (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) | |
12244 | return -EINVAL; | |
6546b19f | 12245 | |
2b26f0aa ME |
12246 | if (!attr->inherit && attr->inherit_thread) |
12247 | return -EINVAL; | |
12248 | ||
2e498d0a ME |
12249 | if (attr->remove_on_exec && attr->enable_on_exec) |
12250 | return -EINVAL; | |
12251 | ||
97ba62b2 ME |
12252 | if (attr->sigtrap && !attr->remove_on_exec) |
12253 | return -EINVAL; | |
12254 | ||
974802ea PZ |
12255 | out: |
12256 | return ret; | |
12257 | ||
12258 | err_size: | |
12259 | put_user(sizeof(*attr), &uattr->size); | |
12260 | ret = -E2BIG; | |
12261 | goto out; | |
12262 | } | |
12263 | ||
68e3c698 PZ |
12264 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
12265 | { | |
12266 | if (b < a) | |
12267 | swap(a, b); | |
12268 | ||
12269 | mutex_lock(a); | |
12270 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
12271 | } | |
12272 | ||
ac9721f3 PZ |
12273 | static int |
12274 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 12275 | { |
56de4e8f | 12276 | struct perf_buffer *rb = NULL; |
a4be7c27 PZ |
12277 | int ret = -EINVAL; |
12278 | ||
68e3c698 PZ |
12279 | if (!output_event) { |
12280 | mutex_lock(&event->mmap_mutex); | |
a4be7c27 | 12281 | goto set; |
68e3c698 | 12282 | } |
a4be7c27 | 12283 | |
ac9721f3 PZ |
12284 | /* don't allow circular references */ |
12285 | if (event == output_event) | |
a4be7c27 PZ |
12286 | goto out; |
12287 | ||
0f139300 PZ |
12288 | /* |
12289 | * Don't allow cross-cpu buffers | |
12290 | */ | |
12291 | if (output_event->cpu != event->cpu) | |
12292 | goto out; | |
12293 | ||
12294 | /* | |
76369139 | 12295 | * If its not a per-cpu rb, it must be the same task. |
0f139300 | 12296 | */ |
24d3ae2f | 12297 | if (output_event->cpu == -1 && output_event->hw.target != event->hw.target) |
0f139300 PZ |
12298 | goto out; |
12299 | ||
34f43927 PZ |
12300 | /* |
12301 | * Mixing clocks in the same buffer is trouble you don't need. | |
12302 | */ | |
12303 | if (output_event->clock != event->clock) | |
12304 | goto out; | |
12305 | ||
9ecda41a WN |
12306 | /* |
12307 | * Either writing ring buffer from beginning or from end. | |
12308 | * Mixing is not allowed. | |
12309 | */ | |
12310 | if (is_write_backward(output_event) != is_write_backward(event)) | |
12311 | goto out; | |
12312 | ||
45bfb2e5 PZ |
12313 | /* |
12314 | * If both events generate aux data, they must be on the same PMU | |
12315 | */ | |
12316 | if (has_aux(event) && has_aux(output_event) && | |
12317 | event->pmu != output_event->pmu) | |
12318 | goto out; | |
12319 | ||
68e3c698 PZ |
12320 | /* |
12321 | * Hold both mmap_mutex to serialize against perf_mmap_close(). Since | |
12322 | * output_event is already on rb->event_list, and the list iteration | |
12323 | * restarts after every removal, it is guaranteed this new event is | |
12324 | * observed *OR* if output_event is already removed, it's guaranteed we | |
12325 | * observe !rb->mmap_count. | |
12326 | */ | |
12327 | mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex); | |
a4be7c27 | 12328 | set: |
ac9721f3 PZ |
12329 | /* Can't redirect output if we've got an active mmap() */ |
12330 | if (atomic_read(&event->mmap_count)) | |
12331 | goto unlock; | |
a4be7c27 | 12332 | |
ac9721f3 | 12333 | if (output_event) { |
76369139 FW |
12334 | /* get the rb we want to redirect to */ |
12335 | rb = ring_buffer_get(output_event); | |
12336 | if (!rb) | |
ac9721f3 | 12337 | goto unlock; |
68e3c698 PZ |
12338 | |
12339 | /* did we race against perf_mmap_close() */ | |
12340 | if (!atomic_read(&rb->mmap_count)) { | |
12341 | ring_buffer_put(rb); | |
12342 | goto unlock; | |
12343 | } | |
a4be7c27 PZ |
12344 | } |
12345 | ||
b69cf536 | 12346 | ring_buffer_attach(event, rb); |
9bb5d40c | 12347 | |
a4be7c27 | 12348 | ret = 0; |
ac9721f3 PZ |
12349 | unlock: |
12350 | mutex_unlock(&event->mmap_mutex); | |
68e3c698 PZ |
12351 | if (output_event) |
12352 | mutex_unlock(&output_event->mmap_mutex); | |
ac9721f3 | 12353 | |
a4be7c27 | 12354 | out: |
a4be7c27 PZ |
12355 | return ret; |
12356 | } | |
12357 | ||
34f43927 PZ |
12358 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
12359 | { | |
12360 | bool nmi_safe = false; | |
12361 | ||
12362 | switch (clk_id) { | |
12363 | case CLOCK_MONOTONIC: | |
12364 | event->clock = &ktime_get_mono_fast_ns; | |
12365 | nmi_safe = true; | |
12366 | break; | |
12367 | ||
12368 | case CLOCK_MONOTONIC_RAW: | |
12369 | event->clock = &ktime_get_raw_fast_ns; | |
12370 | nmi_safe = true; | |
12371 | break; | |
12372 | ||
12373 | case CLOCK_REALTIME: | |
12374 | event->clock = &ktime_get_real_ns; | |
12375 | break; | |
12376 | ||
12377 | case CLOCK_BOOTTIME: | |
9285ec4c | 12378 | event->clock = &ktime_get_boottime_ns; |
34f43927 PZ |
12379 | break; |
12380 | ||
12381 | case CLOCK_TAI: | |
9285ec4c | 12382 | event->clock = &ktime_get_clocktai_ns; |
34f43927 PZ |
12383 | break; |
12384 | ||
12385 | default: | |
12386 | return -EINVAL; | |
12387 | } | |
12388 | ||
12389 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
12390 | return -EINVAL; | |
12391 | ||
12392 | return 0; | |
12393 | } | |
12394 | ||
b068fc04 ME |
12395 | static bool |
12396 | perf_check_permission(struct perf_event_attr *attr, struct task_struct *task) | |
12397 | { | |
12398 | unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS; | |
12399 | bool is_capable = perfmon_capable(); | |
12400 | ||
12401 | if (attr->sigtrap) { | |
12402 | /* | |
12403 | * perf_event_attr::sigtrap sends signals to the other task. | |
12404 | * Require the current task to also have CAP_KILL. | |
12405 | */ | |
12406 | rcu_read_lock(); | |
12407 | is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL); | |
12408 | rcu_read_unlock(); | |
12409 | ||
12410 | /* | |
12411 | * If the required capabilities aren't available, checks for | |
12412 | * ptrace permissions: upgrade to ATTACH, since sending signals | |
12413 | * can effectively change the target task. | |
12414 | */ | |
12415 | ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS; | |
12416 | } | |
12417 | ||
12418 | /* | |
12419 | * Preserve ptrace permission check for backwards compatibility. The | |
12420 | * ptrace check also includes checks that the current task and other | |
12421 | * task have matching uids, and is therefore not done here explicitly. | |
12422 | */ | |
12423 | return is_capable || ptrace_may_access(task, ptrace_mode); | |
12424 | } | |
12425 | ||
0793a61d | 12426 | /** |
cdd6c482 | 12427 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 12428 | * |
cdd6c482 | 12429 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 12430 | * @pid: target pid |
9f66a381 | 12431 | * @cpu: target cpu |
cdd6c482 | 12432 | * @group_fd: group leader event fd |
a1ddf524 | 12433 | * @flags: perf event open flags |
0793a61d | 12434 | */ |
cdd6c482 IM |
12435 | SYSCALL_DEFINE5(perf_event_open, |
12436 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 12437 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 12438 | { |
b04243ef | 12439 | struct perf_event *group_leader = NULL, *output_event = NULL; |
bd275681 | 12440 | struct perf_event_pmu_context *pmu_ctx; |
b04243ef | 12441 | struct perf_event *event, *sibling; |
cdd6c482 | 12442 | struct perf_event_attr attr; |
bd275681 | 12443 | struct perf_event_context *ctx; |
cdd6c482 | 12444 | struct file *event_file = NULL; |
2903ff01 | 12445 | struct fd group = {NULL, 0}; |
38a81da2 | 12446 | struct task_struct *task = NULL; |
89a1e187 | 12447 | struct pmu *pmu; |
ea635c64 | 12448 | int event_fd; |
b04243ef | 12449 | int move_group = 0; |
dc86cabe | 12450 | int err; |
a21b0b35 | 12451 | int f_flags = O_RDWR; |
79dff51e | 12452 | int cgroup_fd = -1; |
0793a61d | 12453 | |
2743a5b0 | 12454 | /* for future expandability... */ |
e5d1367f | 12455 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
12456 | return -EINVAL; |
12457 | ||
0a041ebc | 12458 | err = perf_copy_attr(attr_uptr, &attr); |
da97e184 JFG |
12459 | if (err) |
12460 | return err; | |
12461 | ||
0a041ebc NK |
12462 | /* Do we allow access to perf_event_open(2) ? */ |
12463 | err = security_perf_event_open(&attr, PERF_SECURITY_OPEN); | |
dc86cabe IM |
12464 | if (err) |
12465 | return err; | |
eab656ae | 12466 | |
0764771d | 12467 | if (!attr.exclude_kernel) { |
da97e184 JFG |
12468 | err = perf_allow_kernel(&attr); |
12469 | if (err) | |
12470 | return err; | |
0764771d PZ |
12471 | } |
12472 | ||
e4222673 | 12473 | if (attr.namespaces) { |
18aa1856 | 12474 | if (!perfmon_capable()) |
e4222673 HB |
12475 | return -EACCES; |
12476 | } | |
12477 | ||
df58ab24 | 12478 | if (attr.freq) { |
cdd6c482 | 12479 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 12480 | return -EINVAL; |
0819b2e3 PZ |
12481 | } else { |
12482 | if (attr.sample_period & (1ULL << 63)) | |
12483 | return -EINVAL; | |
df58ab24 PZ |
12484 | } |
12485 | ||
fc7ce9c7 | 12486 | /* Only privileged users can get physical addresses */ |
da97e184 JFG |
12487 | if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { |
12488 | err = perf_allow_kernel(&attr); | |
12489 | if (err) | |
12490 | return err; | |
12491 | } | |
fc7ce9c7 | 12492 | |
08ef1af4 OM |
12493 | /* REGS_INTR can leak data, lockdown must prevent this */ |
12494 | if (attr.sample_type & PERF_SAMPLE_REGS_INTR) { | |
12495 | err = security_locked_down(LOCKDOWN_PERF); | |
12496 | if (err) | |
12497 | return err; | |
12498 | } | |
b0c8fdc7 | 12499 | |
e5d1367f SE |
12500 | /* |
12501 | * In cgroup mode, the pid argument is used to pass the fd | |
12502 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
12503 | * designates the cpu on which to monitor threads from that | |
12504 | * cgroup. | |
12505 | */ | |
12506 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
12507 | return -EINVAL; | |
12508 | ||
a21b0b35 YD |
12509 | if (flags & PERF_FLAG_FD_CLOEXEC) |
12510 | f_flags |= O_CLOEXEC; | |
12511 | ||
12512 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
12513 | if (event_fd < 0) |
12514 | return event_fd; | |
12515 | ||
ac9721f3 | 12516 | if (group_fd != -1) { |
2903ff01 AV |
12517 | err = perf_fget_light(group_fd, &group); |
12518 | if (err) | |
d14b12d7 | 12519 | goto err_fd; |
2903ff01 | 12520 | group_leader = group.file->private_data; |
ac9721f3 PZ |
12521 | if (flags & PERF_FLAG_FD_OUTPUT) |
12522 | output_event = group_leader; | |
12523 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
12524 | group_leader = NULL; | |
12525 | } | |
12526 | ||
e5d1367f | 12527 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
12528 | task = find_lively_task_by_vpid(pid); |
12529 | if (IS_ERR(task)) { | |
12530 | err = PTR_ERR(task); | |
12531 | goto err_group_fd; | |
12532 | } | |
12533 | } | |
12534 | ||
1f4ee503 PZ |
12535 | if (task && group_leader && |
12536 | group_leader->attr.inherit != attr.inherit) { | |
12537 | err = -EINVAL; | |
12538 | goto err_task; | |
12539 | } | |
12540 | ||
79dff51e MF |
12541 | if (flags & PERF_FLAG_PID_CGROUP) |
12542 | cgroup_fd = pid; | |
12543 | ||
4dc0da86 | 12544 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 12545 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
12546 | if (IS_ERR(event)) { |
12547 | err = PTR_ERR(event); | |
78af4dc9 | 12548 | goto err_task; |
d14b12d7 SE |
12549 | } |
12550 | ||
53b25335 VW |
12551 | if (is_sampling_event(event)) { |
12552 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 12553 | err = -EOPNOTSUPP; |
53b25335 VW |
12554 | goto err_alloc; |
12555 | } | |
12556 | } | |
12557 | ||
89a1e187 PZ |
12558 | /* |
12559 | * Special case software events and allow them to be part of | |
12560 | * any hardware group. | |
12561 | */ | |
12562 | pmu = event->pmu; | |
b04243ef | 12563 | |
34f43927 PZ |
12564 | if (attr.use_clockid) { |
12565 | err = perf_event_set_clock(event, attr.clockid); | |
12566 | if (err) | |
12567 | goto err_alloc; | |
12568 | } | |
12569 | ||
4ff6a8de DCC |
12570 | if (pmu->task_ctx_nr == perf_sw_context) |
12571 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
12572 | ||
bd275681 PZ |
12573 | if (task) { |
12574 | err = down_read_interruptible(&task->signal->exec_update_lock); | |
12575 | if (err) | |
12576 | goto err_alloc; | |
12577 | ||
12578 | /* | |
12579 | * We must hold exec_update_lock across this and any potential | |
12580 | * perf_install_in_context() call for this new event to | |
12581 | * serialize against exec() altering our credentials (and the | |
12582 | * perf_event_exit_task() that could imply). | |
12583 | */ | |
12584 | err = -EACCES; | |
12585 | if (!perf_check_permission(&attr, task)) | |
12586 | goto err_cred; | |
b04243ef | 12587 | } |
89a1e187 PZ |
12588 | |
12589 | /* | |
12590 | * Get the target context (task or percpu): | |
12591 | */ | |
bd275681 | 12592 | ctx = find_get_context(task, event); |
89a1e187 PZ |
12593 | if (IS_ERR(ctx)) { |
12594 | err = PTR_ERR(ctx); | |
bd275681 PZ |
12595 | goto err_cred; |
12596 | } | |
12597 | ||
12598 | mutex_lock(&ctx->mutex); | |
12599 | ||
12600 | if (ctx->task == TASK_TOMBSTONE) { | |
12601 | err = -ESRCH; | |
12602 | goto err_locked; | |
12603 | } | |
12604 | ||
12605 | if (!task) { | |
12606 | /* | |
12607 | * Check if the @cpu we're creating an event for is online. | |
12608 | * | |
12609 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
12610 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
12611 | */ | |
12612 | struct perf_cpu_context *cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); | |
12613 | ||
12614 | if (!cpuctx->online) { | |
12615 | err = -ENODEV; | |
12616 | goto err_locked; | |
12617 | } | |
89a1e187 PZ |
12618 | } |
12619 | ||
ac9721f3 | 12620 | if (group_leader) { |
dc86cabe | 12621 | err = -EINVAL; |
04289bb9 | 12622 | |
04289bb9 | 12623 | /* |
ccff286d IM |
12624 | * Do not allow a recursive hierarchy (this new sibling |
12625 | * becoming part of another group-sibling): | |
12626 | */ | |
12627 | if (group_leader->group_leader != group_leader) | |
bd275681 | 12628 | goto err_locked; |
34f43927 PZ |
12629 | |
12630 | /* All events in a group should have the same clock */ | |
12631 | if (group_leader->clock != event->clock) | |
bd275681 | 12632 | goto err_locked; |
34f43927 | 12633 | |
ccff286d | 12634 | /* |
64aee2a9 MR |
12635 | * Make sure we're both events for the same CPU; |
12636 | * grouping events for different CPUs is broken; since | |
12637 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 12638 | */ |
64aee2a9 | 12639 | if (group_leader->cpu != event->cpu) |
bd275681 | 12640 | goto err_locked; |
64aee2a9 MR |
12641 | |
12642 | /* | |
bd275681 | 12643 | * Make sure we're both on the same context; either task or cpu. |
64aee2a9 | 12644 | */ |
bd275681 PZ |
12645 | if (group_leader->ctx != ctx) |
12646 | goto err_locked; | |
b04243ef | 12647 | |
3b6f9e5c PM |
12648 | /* |
12649 | * Only a group leader can be exclusive or pinned | |
12650 | */ | |
0d48696f | 12651 | if (attr.exclusive || attr.pinned) |
84c4e620 | 12652 | goto err_locked; |
321027c1 | 12653 | |
bd275681 PZ |
12654 | if (is_software_event(event) && |
12655 | !in_software_context(group_leader)) { | |
321027c1 | 12656 | /* |
bd275681 PZ |
12657 | * If the event is a sw event, but the group_leader |
12658 | * is on hw context. | |
12659 | * | |
12660 | * Allow the addition of software events to hw | |
12661 | * groups, this is safe because software events | |
12662 | * never fail to schedule. | |
12663 | * | |
12664 | * Note the comment that goes with struct | |
12665 | * perf_event_pmu_context. | |
321027c1 | 12666 | */ |
bd275681 | 12667 | pmu = group_leader->pmu_ctx->pmu; |
bf480f93 RB |
12668 | } else if (!is_software_event(event)) { |
12669 | if (is_software_event(group_leader) && | |
12670 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
12671 | /* | |
12672 | * In case the group is a pure software group, and we | |
12673 | * try to add a hardware event, move the whole group to | |
12674 | * the hardware context. | |
12675 | */ | |
12676 | move_group = 1; | |
321027c1 | 12677 | } |
8a58ddae | 12678 | |
bf480f93 RB |
12679 | /* Don't allow group of multiple hw events from different pmus */ |
12680 | if (!in_software_context(group_leader) && | |
12681 | group_leader->pmu_ctx->pmu != pmu) | |
8a58ddae AS |
12682 | goto err_locked; |
12683 | } | |
f55fc2a5 PZ |
12684 | } |
12685 | ||
bd275681 PZ |
12686 | /* |
12687 | * Now that we're certain of the pmu; find the pmu_ctx. | |
12688 | */ | |
12689 | pmu_ctx = find_get_pmu_context(pmu, ctx, event); | |
12690 | if (IS_ERR(pmu_ctx)) { | |
12691 | err = PTR_ERR(pmu_ctx); | |
84c4e620 PZ |
12692 | goto err_locked; |
12693 | } | |
bd275681 | 12694 | event->pmu_ctx = pmu_ctx; |
84c4e620 | 12695 | |
bd275681 PZ |
12696 | if (output_event) { |
12697 | err = perf_event_set_output(event, output_event); | |
12698 | if (err) | |
12699 | goto err_context; | |
a723968c PZ |
12700 | } |
12701 | ||
bd275681 PZ |
12702 | if (!perf_event_validate_size(event)) { |
12703 | err = -E2BIG; | |
12704 | goto err_context; | |
a63fbed7 TG |
12705 | } |
12706 | ||
da9ec3d3 MR |
12707 | if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) { |
12708 | err = -EINVAL; | |
bd275681 | 12709 | goto err_context; |
da9ec3d3 | 12710 | } |
a63fbed7 | 12711 | |
f55fc2a5 PZ |
12712 | /* |
12713 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
12714 | * because we need to serialize with concurrent event creation. | |
12715 | */ | |
12716 | if (!exclusive_event_installable(event, ctx)) { | |
f55fc2a5 | 12717 | err = -EBUSY; |
bd275681 | 12718 | goto err_context; |
f55fc2a5 | 12719 | } |
f63a8daa | 12720 | |
f55fc2a5 PZ |
12721 | WARN_ON_ONCE(ctx->parent_ctx); |
12722 | ||
bd275681 PZ |
12723 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, f_flags); |
12724 | if (IS_ERR(event_file)) { | |
12725 | err = PTR_ERR(event_file); | |
12726 | event_file = NULL; | |
12727 | goto err_context; | |
12728 | } | |
12729 | ||
79c9ce57 PZ |
12730 | /* |
12731 | * This is the point on no return; we cannot fail hereafter. This is | |
12732 | * where we start modifying current state. | |
12733 | */ | |
12734 | ||
f55fc2a5 | 12735 | if (move_group) { |
45a0e07a | 12736 | perf_remove_from_context(group_leader, 0); |
bd275681 | 12737 | put_pmu_ctx(group_leader->pmu_ctx); |
0231bb53 | 12738 | |
edb39592 | 12739 | for_each_sibling_event(sibling, group_leader) { |
45a0e07a | 12740 | perf_remove_from_context(sibling, 0); |
bd275681 | 12741 | put_pmu_ctx(sibling->pmu_ctx); |
b04243ef | 12742 | } |
b04243ef | 12743 | |
8f95b435 PZI |
12744 | /* |
12745 | * Install the group siblings before the group leader. | |
12746 | * | |
12747 | * Because a group leader will try and install the entire group | |
12748 | * (through the sibling list, which is still in-tact), we can | |
12749 | * end up with siblings installed in the wrong context. | |
12750 | * | |
12751 | * By installing siblings first we NO-OP because they're not | |
12752 | * reachable through the group lists. | |
12753 | */ | |
edb39592 | 12754 | for_each_sibling_event(sibling, group_leader) { |
bd275681 PZ |
12755 | sibling->pmu_ctx = pmu_ctx; |
12756 | get_pmu_ctx(pmu_ctx); | |
8f95b435 | 12757 | perf_event__state_init(sibling); |
9fc81d87 | 12758 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef | 12759 | } |
8f95b435 PZI |
12760 | |
12761 | /* | |
12762 | * Removing from the context ends up with disabled | |
12763 | * event. What we want here is event in the initial | |
12764 | * startup state, ready to be add into new context. | |
12765 | */ | |
bd275681 PZ |
12766 | group_leader->pmu_ctx = pmu_ctx; |
12767 | get_pmu_ctx(pmu_ctx); | |
8f95b435 PZI |
12768 | perf_event__state_init(group_leader); |
12769 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
bed5b25a AS |
12770 | } |
12771 | ||
f73e22ab PZ |
12772 | /* |
12773 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
12774 | * that we're serialized against further additions and before | |
12775 | * perf_install_in_context() which is the point the event is active and | |
12776 | * can use these values. | |
12777 | */ | |
12778 | perf_event__header_size(event); | |
12779 | perf_event__id_header_size(event); | |
12780 | ||
78cd2c74 PZ |
12781 | event->owner = current; |
12782 | ||
e2d37cd2 | 12783 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 12784 | perf_unpin_context(ctx); |
f63a8daa | 12785 | |
d859e29f | 12786 | mutex_unlock(&ctx->mutex); |
9b51f66d | 12787 | |
79c9ce57 | 12788 | if (task) { |
f7cfd871 | 12789 | up_read(&task->signal->exec_update_lock); |
79c9ce57 PZ |
12790 | put_task_struct(task); |
12791 | } | |
12792 | ||
cdd6c482 IM |
12793 | mutex_lock(¤t->perf_event_mutex); |
12794 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
12795 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 12796 | |
8a49542c PZ |
12797 | /* |
12798 | * Drop the reference on the group_event after placing the | |
12799 | * new event on the sibling_list. This ensures destruction | |
12800 | * of the group leader will find the pointer to itself in | |
12801 | * perf_group_detach(). | |
12802 | */ | |
2903ff01 | 12803 | fdput(group); |
ea635c64 AV |
12804 | fd_install(event_fd, event_file); |
12805 | return event_fd; | |
0793a61d | 12806 | |
bd275681 | 12807 | err_context: |
a551844e PZ |
12808 | put_pmu_ctx(event->pmu_ctx); |
12809 | event->pmu_ctx = NULL; /* _free_event() */ | |
f55fc2a5 | 12810 | err_locked: |
f55fc2a5 | 12811 | mutex_unlock(&ctx->mutex); |
bd275681 PZ |
12812 | perf_unpin_context(ctx); |
12813 | put_ctx(ctx); | |
78af4dc9 | 12814 | err_cred: |
12815 | if (task) | |
d01e7f10 | 12816 | up_read(&task->signal->exec_update_lock); |
c6be5a5c | 12817 | err_alloc: |
bd275681 | 12818 | free_event(event); |
1f4ee503 | 12819 | err_task: |
e7d0bc04 PZ |
12820 | if (task) |
12821 | put_task_struct(task); | |
89a1e187 | 12822 | err_group_fd: |
2903ff01 | 12823 | fdput(group); |
ea635c64 AV |
12824 | err_fd: |
12825 | put_unused_fd(event_fd); | |
dc86cabe | 12826 | return err; |
0793a61d TG |
12827 | } |
12828 | ||
fb0459d7 AV |
12829 | /** |
12830 | * perf_event_create_kernel_counter | |
12831 | * | |
12832 | * @attr: attributes of the counter to create | |
12833 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 12834 | * @task: task to profile (NULL for percpu) |
a1ddf524 HX |
12835 | * @overflow_handler: callback to trigger when we hit the event |
12836 | * @context: context data could be used in overflow_handler callback | |
fb0459d7 AV |
12837 | */ |
12838 | struct perf_event * | |
12839 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 12840 | struct task_struct *task, |
4dc0da86 AK |
12841 | perf_overflow_handler_t overflow_handler, |
12842 | void *context) | |
fb0459d7 | 12843 | { |
bd275681 | 12844 | struct perf_event_pmu_context *pmu_ctx; |
fb0459d7 | 12845 | struct perf_event_context *ctx; |
c3f00c70 | 12846 | struct perf_event *event; |
bd275681 | 12847 | struct pmu *pmu; |
fb0459d7 | 12848 | int err; |
d859e29f | 12849 | |
dce5affb AS |
12850 | /* |
12851 | * Grouping is not supported for kernel events, neither is 'AUX', | |
12852 | * make sure the caller's intentions are adjusted. | |
12853 | */ | |
12854 | if (attr->aux_output) | |
12855 | return ERR_PTR(-EINVAL); | |
12856 | ||
4dc0da86 | 12857 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 12858 | overflow_handler, context, -1); |
c3f00c70 PZ |
12859 | if (IS_ERR(event)) { |
12860 | err = PTR_ERR(event); | |
12861 | goto err; | |
12862 | } | |
d859e29f | 12863 | |
f8697762 | 12864 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 12865 | event->owner = TASK_TOMBSTONE; |
bd275681 PZ |
12866 | pmu = event->pmu; |
12867 | ||
12868 | if (pmu->task_ctx_nr == perf_sw_context) | |
12869 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
f8697762 | 12870 | |
f25d8ba9 AS |
12871 | /* |
12872 | * Get the target context (task or percpu): | |
12873 | */ | |
bd275681 | 12874 | ctx = find_get_context(task, event); |
c6567f64 FW |
12875 | if (IS_ERR(ctx)) { |
12876 | err = PTR_ERR(ctx); | |
bd275681 | 12877 | goto err_alloc; |
d859e29f | 12878 | } |
fb0459d7 | 12879 | |
fb0459d7 AV |
12880 | WARN_ON_ONCE(ctx->parent_ctx); |
12881 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
12882 | if (ctx->task == TASK_TOMBSTONE) { |
12883 | err = -ESRCH; | |
12884 | goto err_unlock; | |
12885 | } | |
12886 | ||
bd275681 PZ |
12887 | pmu_ctx = find_get_pmu_context(pmu, ctx, event); |
12888 | if (IS_ERR(pmu_ctx)) { | |
12889 | err = PTR_ERR(pmu_ctx); | |
12890 | goto err_unlock; | |
12891 | } | |
12892 | event->pmu_ctx = pmu_ctx; | |
12893 | ||
a63fbed7 TG |
12894 | if (!task) { |
12895 | /* | |
12896 | * Check if the @cpu we're creating an event for is online. | |
12897 | * | |
12898 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
12899 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
12900 | */ | |
12901 | struct perf_cpu_context *cpuctx = | |
12902 | container_of(ctx, struct perf_cpu_context, ctx); | |
12903 | if (!cpuctx->online) { | |
12904 | err = -ENODEV; | |
bd275681 | 12905 | goto err_pmu_ctx; |
a63fbed7 TG |
12906 | } |
12907 | } | |
12908 | ||
bed5b25a | 12909 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 12910 | err = -EBUSY; |
bd275681 | 12911 | goto err_pmu_ctx; |
bed5b25a AS |
12912 | } |
12913 | ||
4ce54af8 | 12914 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 12915 | perf_unpin_context(ctx); |
fb0459d7 AV |
12916 | mutex_unlock(&ctx->mutex); |
12917 | ||
fb0459d7 AV |
12918 | return event; |
12919 | ||
bd275681 PZ |
12920 | err_pmu_ctx: |
12921 | put_pmu_ctx(pmu_ctx); | |
a551844e | 12922 | event->pmu_ctx = NULL; /* _free_event() */ |
84c4e620 PZ |
12923 | err_unlock: |
12924 | mutex_unlock(&ctx->mutex); | |
12925 | perf_unpin_context(ctx); | |
12926 | put_ctx(ctx); | |
bd275681 | 12927 | err_alloc: |
c3f00c70 PZ |
12928 | free_event(event); |
12929 | err: | |
c6567f64 | 12930 | return ERR_PTR(err); |
9b51f66d | 12931 | } |
fb0459d7 | 12932 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 12933 | |
bd275681 PZ |
12934 | static void __perf_pmu_remove(struct perf_event_context *ctx, |
12935 | int cpu, struct pmu *pmu, | |
12936 | struct perf_event_groups *groups, | |
12937 | struct list_head *events) | |
0cda4c02 | 12938 | { |
bd275681 | 12939 | struct perf_event *event, *sibling; |
0cda4c02 | 12940 | |
bd275681 | 12941 | perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) { |
45a0e07a | 12942 | perf_remove_from_context(event, 0); |
bd275681 PZ |
12943 | put_pmu_ctx(event->pmu_ctx); |
12944 | list_add(&event->migrate_entry, events); | |
12945 | ||
12946 | for_each_sibling_event(sibling, event) { | |
12947 | perf_remove_from_context(sibling, 0); | |
bd275681 PZ |
12948 | put_pmu_ctx(sibling->pmu_ctx); |
12949 | list_add(&sibling->migrate_entry, events); | |
12950 | } | |
0cda4c02 | 12951 | } |
bd275681 | 12952 | } |
0cda4c02 | 12953 | |
bd275681 PZ |
12954 | static void __perf_pmu_install_event(struct pmu *pmu, |
12955 | struct perf_event_context *ctx, | |
12956 | int cpu, struct perf_event *event) | |
12957 | { | |
12958 | struct perf_event_pmu_context *epc; | |
889c58b3 PZ |
12959 | struct perf_event_context *old_ctx = event->ctx; |
12960 | ||
12961 | get_ctx(ctx); /* normally find_get_context() */ | |
bd275681 PZ |
12962 | |
12963 | event->cpu = cpu; | |
12964 | epc = find_get_pmu_context(pmu, ctx, event); | |
12965 | event->pmu_ctx = epc; | |
12966 | ||
12967 | if (event->state >= PERF_EVENT_STATE_OFF) | |
12968 | event->state = PERF_EVENT_STATE_INACTIVE; | |
bd275681 | 12969 | perf_install_in_context(ctx, event, cpu); |
889c58b3 PZ |
12970 | |
12971 | /* | |
12972 | * Now that event->ctx is updated and visible, put the old ctx. | |
12973 | */ | |
12974 | put_ctx(old_ctx); | |
bd275681 PZ |
12975 | } |
12976 | ||
12977 | static void __perf_pmu_install(struct perf_event_context *ctx, | |
12978 | int cpu, struct pmu *pmu, struct list_head *events) | |
12979 | { | |
12980 | struct perf_event *event, *tmp; | |
0cda4c02 | 12981 | |
8f95b435 PZI |
12982 | /* |
12983 | * Re-instate events in 2 passes. | |
12984 | * | |
12985 | * Skip over group leaders and only install siblings on this first | |
12986 | * pass, siblings will not get enabled without a leader, however a | |
12987 | * leader will enable its siblings, even if those are still on the old | |
12988 | * context. | |
12989 | */ | |
bd275681 | 12990 | list_for_each_entry_safe(event, tmp, events, migrate_entry) { |
8f95b435 PZI |
12991 | if (event->group_leader == event) |
12992 | continue; | |
12993 | ||
12994 | list_del(&event->migrate_entry); | |
bd275681 | 12995 | __perf_pmu_install_event(pmu, ctx, cpu, event); |
8f95b435 PZI |
12996 | } |
12997 | ||
12998 | /* | |
12999 | * Once all the siblings are setup properly, install the group leaders | |
13000 | * to make it go. | |
13001 | */ | |
bd275681 | 13002 | list_for_each_entry_safe(event, tmp, events, migrate_entry) { |
9886167d | 13003 | list_del(&event->migrate_entry); |
bd275681 | 13004 | __perf_pmu_install_event(pmu, ctx, cpu, event); |
0cda4c02 | 13005 | } |
bd275681 PZ |
13006 | } |
13007 | ||
13008 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) | |
13009 | { | |
13010 | struct perf_event_context *src_ctx, *dst_ctx; | |
13011 | LIST_HEAD(events); | |
13012 | ||
889c58b3 PZ |
13013 | /* |
13014 | * Since per-cpu context is persistent, no need to grab an extra | |
13015 | * reference. | |
13016 | */ | |
bd275681 PZ |
13017 | src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx; |
13018 | dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx; | |
13019 | ||
13020 | /* | |
13021 | * See perf_event_ctx_lock() for comments on the details | |
13022 | * of swizzling perf_event::ctx. | |
13023 | */ | |
13024 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
13025 | ||
13026 | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events); | |
13027 | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events); | |
13028 | ||
b1680989 PZ |
13029 | if (!list_empty(&events)) { |
13030 | /* | |
13031 | * Wait for the events to quiesce before re-instating them. | |
13032 | */ | |
13033 | synchronize_rcu(); | |
bd275681 | 13034 | |
b1680989 PZ |
13035 | __perf_pmu_install(dst_ctx, dst_cpu, pmu, &events); |
13036 | } | |
bd275681 | 13037 | |
0cda4c02 | 13038 | mutex_unlock(&dst_ctx->mutex); |
f63a8daa | 13039 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
13040 | } |
13041 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
13042 | ||
ef54c1a4 | 13043 | static void sync_child_event(struct perf_event *child_event) |
d859e29f | 13044 | { |
cdd6c482 | 13045 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 13046 | u64 child_val; |
d859e29f | 13047 | |
ef54c1a4 PZ |
13048 | if (child_event->attr.inherit_stat) { |
13049 | struct task_struct *task = child_event->ctx->task; | |
13050 | ||
13051 | if (task && task != TASK_TOMBSTONE) | |
13052 | perf_event_read_event(child_event, task); | |
13053 | } | |
38b200d6 | 13054 | |
b5e58793 | 13055 | child_val = perf_event_count(child_event); |
d859e29f PM |
13056 | |
13057 | /* | |
13058 | * Add back the child's count to the parent's count: | |
13059 | */ | |
a6e6dea6 | 13060 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
13061 | atomic64_add(child_event->total_time_enabled, |
13062 | &parent_event->child_total_time_enabled); | |
13063 | atomic64_add(child_event->total_time_running, | |
13064 | &parent_event->child_total_time_running); | |
d859e29f PM |
13065 | } |
13066 | ||
9b51f66d | 13067 | static void |
ef54c1a4 | 13068 | perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx) |
9b51f66d | 13069 | { |
ef54c1a4 PZ |
13070 | struct perf_event *parent_event = event->parent; |
13071 | unsigned long detach_flags = 0; | |
8ba289b8 | 13072 | |
ef54c1a4 PZ |
13073 | if (parent_event) { |
13074 | /* | |
13075 | * Do not destroy the 'original' grouping; because of the | |
13076 | * context switch optimization the original events could've | |
13077 | * ended up in a random child task. | |
13078 | * | |
13079 | * If we were to destroy the original group, all group related | |
13080 | * operations would cease to function properly after this | |
13081 | * random child dies. | |
13082 | * | |
13083 | * Do destroy all inherited groups, we don't care about those | |
13084 | * and being thorough is better. | |
13085 | */ | |
13086 | detach_flags = DETACH_GROUP | DETACH_CHILD; | |
13087 | mutex_lock(&parent_event->child_mutex); | |
13088 | } | |
32132a3d | 13089 | |
ef54c1a4 PZ |
13090 | perf_remove_from_context(event, detach_flags); |
13091 | ||
13092 | raw_spin_lock_irq(&ctx->lock); | |
13093 | if (event->state > PERF_EVENT_STATE_EXIT) | |
13094 | perf_event_set_state(event, PERF_EVENT_STATE_EXIT); | |
13095 | raw_spin_unlock_irq(&ctx->lock); | |
0cc0c027 | 13096 | |
9b51f66d | 13097 | /* |
ef54c1a4 | 13098 | * Child events can be freed. |
9b51f66d | 13099 | */ |
ef54c1a4 PZ |
13100 | if (parent_event) { |
13101 | mutex_unlock(&parent_event->child_mutex); | |
13102 | /* | |
13103 | * Kick perf_poll() for is_event_hup(); | |
13104 | */ | |
13105 | perf_event_wakeup(parent_event); | |
13106 | free_event(event); | |
13107 | put_event(parent_event); | |
8ba289b8 | 13108 | return; |
4bcf349a | 13109 | } |
8ba289b8 PZ |
13110 | |
13111 | /* | |
ef54c1a4 | 13112 | * Parent events are governed by their filedesc, retain them. |
8ba289b8 | 13113 | */ |
ef54c1a4 | 13114 | perf_event_wakeup(event); |
9b51f66d IM |
13115 | } |
13116 | ||
bd275681 | 13117 | static void perf_event_exit_task_context(struct task_struct *child) |
9b51f66d | 13118 | { |
211de6eb | 13119 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 13120 | struct perf_event *child_event, *next; |
63b6da39 PZ |
13121 | |
13122 | WARN_ON_ONCE(child != current); | |
9b51f66d | 13123 | |
bd275681 | 13124 | child_ctx = perf_pin_task_context(child); |
63b6da39 | 13125 | if (!child_ctx) |
9b51f66d IM |
13126 | return; |
13127 | ||
ad3a37de | 13128 | /* |
6a3351b6 PZ |
13129 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
13130 | * ctx::mutex over the entire thing. This serializes against almost | |
13131 | * everything that wants to access the ctx. | |
13132 | * | |
13133 | * The exception is sys_perf_event_open() / | |
13134 | * perf_event_create_kernel_count() which does find_get_context() | |
13135 | * without ctx::mutex (it cannot because of the move_group double mutex | |
13136 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 13137 | */ |
6a3351b6 | 13138 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
13139 | |
13140 | /* | |
6a3351b6 PZ |
13141 | * In a single ctx::lock section, de-schedule the events and detach the |
13142 | * context from the task such that we cannot ever get it scheduled back | |
13143 | * in. | |
c93f7669 | 13144 | */ |
6a3351b6 | 13145 | raw_spin_lock_irq(&child_ctx->lock); |
bd275681 | 13146 | task_ctx_sched_out(child_ctx, EVENT_ALL); |
4a1c0f26 | 13147 | |
71a851b4 | 13148 | /* |
63b6da39 PZ |
13149 | * Now that the context is inactive, destroy the task <-> ctx relation |
13150 | * and mark the context dead. | |
71a851b4 | 13151 | */ |
bd275681 | 13152 | RCU_INIT_POINTER(child->perf_event_ctxp, NULL); |
63b6da39 PZ |
13153 | put_ctx(child_ctx); /* cannot be last */ |
13154 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
13155 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 13156 | |
211de6eb | 13157 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 13158 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 13159 | |
211de6eb PZ |
13160 | if (clone_ctx) |
13161 | put_ctx(clone_ctx); | |
4a1c0f26 | 13162 | |
9f498cc5 | 13163 | /* |
cdd6c482 IM |
13164 | * Report the task dead after unscheduling the events so that we |
13165 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
13166 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 13167 | */ |
cdd6c482 | 13168 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 13169 | |
ebf905fc | 13170 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
ef54c1a4 | 13171 | perf_event_exit_event(child_event, child_ctx); |
8bc20959 | 13172 | |
a63eaf34 PM |
13173 | mutex_unlock(&child_ctx->mutex); |
13174 | ||
13175 | put_ctx(child_ctx); | |
9b51f66d IM |
13176 | } |
13177 | ||
8dc85d54 PZ |
13178 | /* |
13179 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 | 13180 | * |
f7cfd871 | 13181 | * Can be called with exec_update_lock held when called from |
96ecee29 | 13182 | * setup_new_exec(). |
8dc85d54 PZ |
13183 | */ |
13184 | void perf_event_exit_task(struct task_struct *child) | |
13185 | { | |
8882135b | 13186 | struct perf_event *event, *tmp; |
8dc85d54 | 13187 | |
8882135b PZ |
13188 | mutex_lock(&child->perf_event_mutex); |
13189 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
13190 | owner_entry) { | |
13191 | list_del_init(&event->owner_entry); | |
13192 | ||
13193 | /* | |
13194 | * Ensure the list deletion is visible before we clear | |
13195 | * the owner, closes a race against perf_release() where | |
13196 | * we need to serialize on the owner->perf_event_mutex. | |
13197 | */ | |
f47c02c0 | 13198 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
13199 | } |
13200 | mutex_unlock(&child->perf_event_mutex); | |
13201 | ||
bd275681 | 13202 | perf_event_exit_task_context(child); |
4e93ad60 JO |
13203 | |
13204 | /* | |
13205 | * The perf_event_exit_task_context calls perf_event_task | |
13206 | * with child's task_ctx, which generates EXIT events for | |
13207 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
13208 | * At this point we need to send EXIT events to cpu contexts. | |
13209 | */ | |
13210 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
13211 | } |
13212 | ||
889ff015 FW |
13213 | static void perf_free_event(struct perf_event *event, |
13214 | struct perf_event_context *ctx) | |
13215 | { | |
13216 | struct perf_event *parent = event->parent; | |
13217 | ||
13218 | if (WARN_ON_ONCE(!parent)) | |
13219 | return; | |
13220 | ||
13221 | mutex_lock(&parent->child_mutex); | |
13222 | list_del_init(&event->child_list); | |
13223 | mutex_unlock(&parent->child_mutex); | |
13224 | ||
a6fa941d | 13225 | put_event(parent); |
889ff015 | 13226 | |
652884fe | 13227 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 13228 | perf_group_detach(event); |
889ff015 | 13229 | list_del_event(event, ctx); |
652884fe | 13230 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
13231 | free_event(event); |
13232 | } | |
13233 | ||
bbbee908 | 13234 | /* |
1cf8dfe8 PZ |
13235 | * Free a context as created by inheritance by perf_event_init_task() below, |
13236 | * used by fork() in case of fail. | |
652884fe | 13237 | * |
1cf8dfe8 PZ |
13238 | * Even though the task has never lived, the context and events have been |
13239 | * exposed through the child_list, so we must take care tearing it all down. | |
bbbee908 | 13240 | */ |
cdd6c482 | 13241 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 13242 | { |
8dc85d54 | 13243 | struct perf_event_context *ctx; |
cdd6c482 | 13244 | struct perf_event *event, *tmp; |
bbbee908 | 13245 | |
bd275681 PZ |
13246 | ctx = rcu_access_pointer(task->perf_event_ctxp); |
13247 | if (!ctx) | |
13248 | return; | |
bbbee908 | 13249 | |
bd275681 PZ |
13250 | mutex_lock(&ctx->mutex); |
13251 | raw_spin_lock_irq(&ctx->lock); | |
13252 | /* | |
13253 | * Destroy the task <-> ctx relation and mark the context dead. | |
13254 | * | |
13255 | * This is important because even though the task hasn't been | |
13256 | * exposed yet the context has been (through child_list). | |
13257 | */ | |
13258 | RCU_INIT_POINTER(task->perf_event_ctxp, NULL); | |
13259 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
13260 | put_task_struct(task); /* cannot be last */ | |
13261 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 13262 | |
bbbee908 | 13263 | |
bd275681 PZ |
13264 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
13265 | perf_free_event(event, ctx); | |
1cf8dfe8 | 13266 | |
bd275681 PZ |
13267 | mutex_unlock(&ctx->mutex); |
13268 | ||
13269 | /* | |
13270 | * perf_event_release_kernel() could've stolen some of our | |
13271 | * child events and still have them on its free_list. In that | |
13272 | * case we must wait for these events to have been freed (in | |
13273 | * particular all their references to this task must've been | |
13274 | * dropped). | |
13275 | * | |
13276 | * Without this copy_process() will unconditionally free this | |
13277 | * task (irrespective of its reference count) and | |
13278 | * _free_event()'s put_task_struct(event->hw.target) will be a | |
13279 | * use-after-free. | |
13280 | * | |
13281 | * Wait for all events to drop their context reference. | |
13282 | */ | |
13283 | wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1); | |
13284 | put_ctx(ctx); /* must be last */ | |
889ff015 FW |
13285 | } |
13286 | ||
4e231c79 PZ |
13287 | void perf_event_delayed_put(struct task_struct *task) |
13288 | { | |
bd275681 | 13289 | WARN_ON_ONCE(task->perf_event_ctxp); |
4e231c79 PZ |
13290 | } |
13291 | ||
e03e7ee3 | 13292 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 13293 | { |
02e5ad97 | 13294 | struct file *file = fget(fd); |
e03e7ee3 AS |
13295 | if (!file) |
13296 | return ERR_PTR(-EBADF); | |
ffe8690c | 13297 | |
e03e7ee3 AS |
13298 | if (file->f_op != &perf_fops) { |
13299 | fput(file); | |
13300 | return ERR_PTR(-EBADF); | |
13301 | } | |
ffe8690c | 13302 | |
e03e7ee3 | 13303 | return file; |
ffe8690c KX |
13304 | } |
13305 | ||
f8d959a5 YS |
13306 | const struct perf_event *perf_get_event(struct file *file) |
13307 | { | |
13308 | if (file->f_op != &perf_fops) | |
13309 | return ERR_PTR(-EINVAL); | |
13310 | ||
13311 | return file->private_data; | |
13312 | } | |
13313 | ||
ffe8690c KX |
13314 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
13315 | { | |
13316 | if (!event) | |
13317 | return ERR_PTR(-EINVAL); | |
13318 | ||
13319 | return &event->attr; | |
13320 | } | |
13321 | ||
97dee4f3 | 13322 | /* |
788faab7 | 13323 | * Inherit an event from parent task to child task. |
d8a8cfc7 PZ |
13324 | * |
13325 | * Returns: | |
13326 | * - valid pointer on success | |
13327 | * - NULL for orphaned events | |
13328 | * - IS_ERR() on error | |
97dee4f3 PZ |
13329 | */ |
13330 | static struct perf_event * | |
13331 | inherit_event(struct perf_event *parent_event, | |
13332 | struct task_struct *parent, | |
13333 | struct perf_event_context *parent_ctx, | |
13334 | struct task_struct *child, | |
13335 | struct perf_event *group_leader, | |
13336 | struct perf_event_context *child_ctx) | |
13337 | { | |
8ca2bd41 | 13338 | enum perf_event_state parent_state = parent_event->state; |
bd275681 | 13339 | struct perf_event_pmu_context *pmu_ctx; |
97dee4f3 | 13340 | struct perf_event *child_event; |
cee010ec | 13341 | unsigned long flags; |
97dee4f3 PZ |
13342 | |
13343 | /* | |
13344 | * Instead of creating recursive hierarchies of events, | |
13345 | * we link inherited events back to the original parent, | |
13346 | * which has a filp for sure, which we use as the reference | |
13347 | * count: | |
13348 | */ | |
13349 | if (parent_event->parent) | |
13350 | parent_event = parent_event->parent; | |
13351 | ||
13352 | child_event = perf_event_alloc(&parent_event->attr, | |
13353 | parent_event->cpu, | |
d580ff86 | 13354 | child, |
97dee4f3 | 13355 | group_leader, parent_event, |
79dff51e | 13356 | NULL, NULL, -1); |
97dee4f3 PZ |
13357 | if (IS_ERR(child_event)) |
13358 | return child_event; | |
a6fa941d | 13359 | |
bd275681 | 13360 | pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event); |
c55bfbb3 | 13361 | if (IS_ERR(pmu_ctx)) { |
bd275681 | 13362 | free_event(child_event); |
e2d37148 | 13363 | return ERR_CAST(pmu_ctx); |
313ccb96 | 13364 | } |
bd275681 | 13365 | child_event->pmu_ctx = pmu_ctx; |
313ccb96 | 13366 | |
c6e5b732 PZ |
13367 | /* |
13368 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
13369 | * must be under the same lock in order to serialize against | |
13370 | * perf_event_release_kernel(), such that either we must observe | |
13371 | * is_orphaned_event() or they will observe us on the child_list. | |
13372 | */ | |
13373 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
13374 | if (is_orphaned_event(parent_event) || |
13375 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 13376 | mutex_unlock(&parent_event->child_mutex); |
313ccb96 | 13377 | /* task_ctx_data is freed with child_ctx */ |
a6fa941d AV |
13378 | free_event(child_event); |
13379 | return NULL; | |
13380 | } | |
13381 | ||
97dee4f3 PZ |
13382 | get_ctx(child_ctx); |
13383 | ||
13384 | /* | |
13385 | * Make the child state follow the state of the parent event, | |
13386 | * not its attr.disabled bit. We hold the parent's mutex, | |
13387 | * so we won't race with perf_event_{en, dis}able_family. | |
13388 | */ | |
1929def9 | 13389 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
13390 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
13391 | else | |
13392 | child_event->state = PERF_EVENT_STATE_OFF; | |
13393 | ||
13394 | if (parent_event->attr.freq) { | |
13395 | u64 sample_period = parent_event->hw.sample_period; | |
13396 | struct hw_perf_event *hwc = &child_event->hw; | |
13397 | ||
13398 | hwc->sample_period = sample_period; | |
13399 | hwc->last_period = sample_period; | |
13400 | ||
13401 | local64_set(&hwc->period_left, sample_period); | |
13402 | } | |
13403 | ||
13404 | child_event->ctx = child_ctx; | |
13405 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
13406 | child_event->overflow_handler_context |
13407 | = parent_event->overflow_handler_context; | |
97dee4f3 | 13408 | |
614b6780 TG |
13409 | /* |
13410 | * Precalculate sample_data sizes | |
13411 | */ | |
13412 | perf_event__header_size(child_event); | |
6844c09d | 13413 | perf_event__id_header_size(child_event); |
614b6780 | 13414 | |
97dee4f3 PZ |
13415 | /* |
13416 | * Link it up in the child's context: | |
13417 | */ | |
cee010ec | 13418 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 13419 | add_event_to_ctx(child_event, child_ctx); |
ef54c1a4 | 13420 | child_event->attach_state |= PERF_ATTACH_CHILD; |
cee010ec | 13421 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 13422 | |
97dee4f3 PZ |
13423 | /* |
13424 | * Link this into the parent event's child list | |
13425 | */ | |
97dee4f3 PZ |
13426 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
13427 | mutex_unlock(&parent_event->child_mutex); | |
13428 | ||
13429 | return child_event; | |
13430 | } | |
13431 | ||
d8a8cfc7 PZ |
13432 | /* |
13433 | * Inherits an event group. | |
13434 | * | |
13435 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
13436 | * This matches with perf_event_release_kernel() removing all child events. | |
13437 | * | |
13438 | * Returns: | |
13439 | * - 0 on success | |
13440 | * - <0 on error | |
13441 | */ | |
97dee4f3 PZ |
13442 | static int inherit_group(struct perf_event *parent_event, |
13443 | struct task_struct *parent, | |
13444 | struct perf_event_context *parent_ctx, | |
13445 | struct task_struct *child, | |
13446 | struct perf_event_context *child_ctx) | |
13447 | { | |
13448 | struct perf_event *leader; | |
13449 | struct perf_event *sub; | |
13450 | struct perf_event *child_ctr; | |
13451 | ||
13452 | leader = inherit_event(parent_event, parent, parent_ctx, | |
13453 | child, NULL, child_ctx); | |
13454 | if (IS_ERR(leader)) | |
13455 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
13456 | /* |
13457 | * @leader can be NULL here because of is_orphaned_event(). In this | |
13458 | * case inherit_event() will create individual events, similar to what | |
13459 | * perf_group_detach() would do anyway. | |
13460 | */ | |
edb39592 | 13461 | for_each_sibling_event(sub, parent_event) { |
97dee4f3 PZ |
13462 | child_ctr = inherit_event(sub, parent, parent_ctx, |
13463 | child, leader, child_ctx); | |
13464 | if (IS_ERR(child_ctr)) | |
13465 | return PTR_ERR(child_ctr); | |
f733c6b5 | 13466 | |
00496fe5 | 13467 | if (sub->aux_event == parent_event && child_ctr && |
f733c6b5 AS |
13468 | !perf_get_aux_event(child_ctr, leader)) |
13469 | return -EINVAL; | |
97dee4f3 | 13470 | } |
a71ef314 PZ |
13471 | if (leader) |
13472 | leader->group_generation = parent_event->group_generation; | |
97dee4f3 | 13473 | return 0; |
889ff015 FW |
13474 | } |
13475 | ||
d8a8cfc7 PZ |
13476 | /* |
13477 | * Creates the child task context and tries to inherit the event-group. | |
13478 | * | |
13479 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
13480 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
13481 | * consistent with perf_event_release_kernel() removing all child events. | |
13482 | * | |
13483 | * Returns: | |
13484 | * - 0 on success | |
13485 | * - <0 on error | |
13486 | */ | |
889ff015 FW |
13487 | static int |
13488 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
13489 | struct perf_event_context *parent_ctx, | |
bd275681 | 13490 | struct task_struct *child, |
2b26f0aa | 13491 | u64 clone_flags, int *inherited_all) |
889ff015 | 13492 | { |
8dc85d54 | 13493 | struct perf_event_context *child_ctx; |
bd275681 | 13494 | int ret; |
889ff015 | 13495 | |
2b26f0aa | 13496 | if (!event->attr.inherit || |
97ba62b2 ME |
13497 | (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) || |
13498 | /* Do not inherit if sigtrap and signal handlers were cleared. */ | |
13499 | (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) { | |
889ff015 FW |
13500 | *inherited_all = 0; |
13501 | return 0; | |
bbbee908 PZ |
13502 | } |
13503 | ||
bd275681 | 13504 | child_ctx = child->perf_event_ctxp; |
889ff015 FW |
13505 | if (!child_ctx) { |
13506 | /* | |
13507 | * This is executed from the parent task context, so | |
13508 | * inherit events that have been marked for cloning. | |
13509 | * First allocate and initialize a context for the | |
13510 | * child. | |
13511 | */ | |
bd275681 | 13512 | child_ctx = alloc_perf_context(child); |
889ff015 FW |
13513 | if (!child_ctx) |
13514 | return -ENOMEM; | |
bbbee908 | 13515 | |
bd275681 | 13516 | child->perf_event_ctxp = child_ctx; |
889ff015 FW |
13517 | } |
13518 | ||
bd275681 | 13519 | ret = inherit_group(event, parent, parent_ctx, child, child_ctx); |
889ff015 FW |
13520 | if (ret) |
13521 | *inherited_all = 0; | |
13522 | ||
13523 | return ret; | |
bbbee908 PZ |
13524 | } |
13525 | ||
9b51f66d | 13526 | /* |
cdd6c482 | 13527 | * Initialize the perf_event context in task_struct |
9b51f66d | 13528 | */ |
bd275681 | 13529 | static int perf_event_init_context(struct task_struct *child, u64 clone_flags) |
9b51f66d | 13530 | { |
889ff015 | 13531 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
13532 | struct perf_event_context *cloned_ctx; |
13533 | struct perf_event *event; | |
9b51f66d | 13534 | struct task_struct *parent = current; |
564c2b21 | 13535 | int inherited_all = 1; |
dddd3379 | 13536 | unsigned long flags; |
6ab423e0 | 13537 | int ret = 0; |
9b51f66d | 13538 | |
bd275681 | 13539 | if (likely(!parent->perf_event_ctxp)) |
6ab423e0 PZ |
13540 | return 0; |
13541 | ||
ad3a37de | 13542 | /* |
25346b93 PM |
13543 | * If the parent's context is a clone, pin it so it won't get |
13544 | * swapped under us. | |
ad3a37de | 13545 | */ |
bd275681 | 13546 | parent_ctx = perf_pin_task_context(parent); |
ffb4ef21 PZ |
13547 | if (!parent_ctx) |
13548 | return 0; | |
25346b93 | 13549 | |
ad3a37de PM |
13550 | /* |
13551 | * No need to check if parent_ctx != NULL here; since we saw | |
13552 | * it non-NULL earlier, the only reason for it to become NULL | |
13553 | * is if we exit, and since we're currently in the middle of | |
13554 | * a fork we can't be exiting at the same time. | |
13555 | */ | |
ad3a37de | 13556 | |
9b51f66d IM |
13557 | /* |
13558 | * Lock the parent list. No need to lock the child - not PID | |
13559 | * hashed yet and not running, so nobody can access it. | |
13560 | */ | |
d859e29f | 13561 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
13562 | |
13563 | /* | |
13564 | * We dont have to disable NMIs - we are only looking at | |
13565 | * the list, not manipulating it: | |
13566 | */ | |
6e6804d2 | 13567 | perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { |
8dc85d54 | 13568 | ret = inherit_task_group(event, parent, parent_ctx, |
bd275681 | 13569 | child, clone_flags, &inherited_all); |
889ff015 | 13570 | if (ret) |
e7cc4865 | 13571 | goto out_unlock; |
889ff015 | 13572 | } |
b93f7978 | 13573 | |
dddd3379 TG |
13574 | /* |
13575 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
13576 | * to allocations, but we need to prevent rotation because | |
13577 | * rotate_ctx() will change the list from interrupt context. | |
13578 | */ | |
13579 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
13580 | parent_ctx->rotate_disable = 1; | |
13581 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
13582 | ||
6e6804d2 | 13583 | perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { |
8dc85d54 | 13584 | ret = inherit_task_group(event, parent, parent_ctx, |
bd275681 | 13585 | child, clone_flags, &inherited_all); |
889ff015 | 13586 | if (ret) |
e7cc4865 | 13587 | goto out_unlock; |
564c2b21 PM |
13588 | } |
13589 | ||
dddd3379 TG |
13590 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
13591 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 13592 | |
bd275681 | 13593 | child_ctx = child->perf_event_ctxp; |
889ff015 | 13594 | |
05cbaa28 | 13595 | if (child_ctx && inherited_all) { |
564c2b21 PM |
13596 | /* |
13597 | * Mark the child context as a clone of the parent | |
13598 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
13599 | * |
13600 | * Note that if the parent is a clone, the holding of | |
13601 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 13602 | */ |
c5ed5145 | 13603 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
13604 | if (cloned_ctx) { |
13605 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 13606 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
13607 | } else { |
13608 | child_ctx->parent_ctx = parent_ctx; | |
13609 | child_ctx->parent_gen = parent_ctx->generation; | |
13610 | } | |
13611 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
13612 | } |
13613 | ||
c5ed5145 | 13614 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 13615 | out_unlock: |
d859e29f | 13616 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 13617 | |
25346b93 | 13618 | perf_unpin_context(parent_ctx); |
fe4b04fa | 13619 | put_ctx(parent_ctx); |
ad3a37de | 13620 | |
6ab423e0 | 13621 | return ret; |
9b51f66d IM |
13622 | } |
13623 | ||
8dc85d54 PZ |
13624 | /* |
13625 | * Initialize the perf_event context in task_struct | |
13626 | */ | |
2b26f0aa | 13627 | int perf_event_init_task(struct task_struct *child, u64 clone_flags) |
8dc85d54 | 13628 | { |
bd275681 | 13629 | int ret; |
8dc85d54 | 13630 | |
bd275681 | 13631 | child->perf_event_ctxp = NULL; |
8550d7cb ON |
13632 | mutex_init(&child->perf_event_mutex); |
13633 | INIT_LIST_HEAD(&child->perf_event_list); | |
13634 | ||
bd275681 PZ |
13635 | ret = perf_event_init_context(child, clone_flags); |
13636 | if (ret) { | |
13637 | perf_event_free_task(child); | |
13638 | return ret; | |
8dc85d54 PZ |
13639 | } |
13640 | ||
13641 | return 0; | |
13642 | } | |
13643 | ||
220b140b PM |
13644 | static void __init perf_event_init_all_cpus(void) |
13645 | { | |
b28ab83c | 13646 | struct swevent_htable *swhash; |
bd275681 | 13647 | struct perf_cpu_context *cpuctx; |
220b140b | 13648 | int cpu; |
220b140b | 13649 | |
a63fbed7 TG |
13650 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
13651 | ||
220b140b | 13652 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
13653 | swhash = &per_cpu(swevent_htable, cpu); |
13654 | mutex_init(&swhash->hlist_mutex); | |
f2fb6bef KL |
13655 | |
13656 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
13657 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 13658 | |
a5398bff | 13659 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
bd275681 PZ |
13660 | |
13661 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); | |
13662 | __perf_event_init_context(&cpuctx->ctx); | |
13663 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); | |
13664 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); | |
13665 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); | |
13666 | cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default); | |
13667 | cpuctx->heap = cpuctx->heap_default; | |
220b140b PM |
13668 | } |
13669 | } | |
13670 | ||
d18bf422 | 13671 | static void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 13672 | { |
108b02cf | 13673 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 13674 | |
b28ab83c | 13675 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 13676 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
13677 | struct swevent_hlist *hlist; |
13678 | ||
b28ab83c PZ |
13679 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
13680 | WARN_ON(!hlist); | |
13681 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 13682 | } |
b28ab83c | 13683 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
13684 | } |
13685 | ||
2965faa5 | 13686 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 13687 | static void __perf_event_exit_context(void *__info) |
0793a61d | 13688 | { |
bd275681 | 13689 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
108b02cf | 13690 | struct perf_event_context *ctx = __info; |
fae3fde6 | 13691 | struct perf_event *event; |
0793a61d | 13692 | |
fae3fde6 | 13693 | raw_spin_lock(&ctx->lock); |
bd275681 | 13694 | ctx_sched_out(ctx, EVENT_TIME); |
fae3fde6 | 13695 | list_for_each_entry(event, &ctx->event_list, event_entry) |
45a0e07a | 13696 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 13697 | raw_spin_unlock(&ctx->lock); |
0793a61d | 13698 | } |
108b02cf PZ |
13699 | |
13700 | static void perf_event_exit_cpu_context(int cpu) | |
13701 | { | |
a63fbed7 | 13702 | struct perf_cpu_context *cpuctx; |
108b02cf | 13703 | struct perf_event_context *ctx; |
108b02cf | 13704 | |
bd275681 | 13705 | // XXX simplify cpuctx->online |
a63fbed7 | 13706 | mutex_lock(&pmus_lock); |
bd275681 PZ |
13707 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); |
13708 | ctx = &cpuctx->ctx; | |
108b02cf | 13709 | |
bd275681 PZ |
13710 | mutex_lock(&ctx->mutex); |
13711 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
13712 | cpuctx->online = 0; | |
13713 | mutex_unlock(&ctx->mutex); | |
a63fbed7 TG |
13714 | cpumask_clear_cpu(cpu, perf_online_mask); |
13715 | mutex_unlock(&pmus_lock); | |
108b02cf | 13716 | } |
00e16c3d TG |
13717 | #else |
13718 | ||
13719 | static void perf_event_exit_cpu_context(int cpu) { } | |
13720 | ||
13721 | #endif | |
108b02cf | 13722 | |
a63fbed7 TG |
13723 | int perf_event_init_cpu(unsigned int cpu) |
13724 | { | |
13725 | struct perf_cpu_context *cpuctx; | |
13726 | struct perf_event_context *ctx; | |
a63fbed7 TG |
13727 | |
13728 | perf_swevent_init_cpu(cpu); | |
13729 | ||
13730 | mutex_lock(&pmus_lock); | |
13731 | cpumask_set_cpu(cpu, perf_online_mask); | |
bd275681 PZ |
13732 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); |
13733 | ctx = &cpuctx->ctx; | |
a63fbed7 | 13734 | |
bd275681 PZ |
13735 | mutex_lock(&ctx->mutex); |
13736 | cpuctx->online = 1; | |
13737 | mutex_unlock(&ctx->mutex); | |
a63fbed7 TG |
13738 | mutex_unlock(&pmus_lock); |
13739 | ||
13740 | return 0; | |
13741 | } | |
13742 | ||
00e16c3d | 13743 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 13744 | { |
e3703f8c | 13745 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 13746 | return 0; |
0793a61d | 13747 | } |
0793a61d | 13748 | |
c277443c PZ |
13749 | static int |
13750 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
13751 | { | |
13752 | int cpu; | |
13753 | ||
13754 | for_each_online_cpu(cpu) | |
13755 | perf_event_exit_cpu(cpu); | |
13756 | ||
13757 | return NOTIFY_OK; | |
13758 | } | |
13759 | ||
13760 | /* | |
13761 | * Run the perf reboot notifier at the very last possible moment so that | |
13762 | * the generic watchdog code runs as long as possible. | |
13763 | */ | |
13764 | static struct notifier_block perf_reboot_notifier = { | |
13765 | .notifier_call = perf_reboot, | |
13766 | .priority = INT_MIN, | |
13767 | }; | |
13768 | ||
cdd6c482 | 13769 | void __init perf_event_init(void) |
0793a61d | 13770 | { |
3c502e7a JW |
13771 | int ret; |
13772 | ||
2e80a82a PZ |
13773 | idr_init(&pmu_idr); |
13774 | ||
220b140b | 13775 | perf_event_init_all_cpus(); |
b0a873eb | 13776 | init_srcu_struct(&pmus_srcu); |
2e80a82a | 13777 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
0d6d062c RB |
13778 | perf_pmu_register(&perf_cpu_clock, "cpu_clock", -1); |
13779 | perf_pmu_register(&perf_task_clock, "task_clock", -1); | |
b0a873eb | 13780 | perf_tp_register(); |
00e16c3d | 13781 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 13782 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
13783 | |
13784 | ret = init_hw_breakpoint(); | |
13785 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 13786 | |
bdacfaf2 NK |
13787 | perf_event_cache = KMEM_CACHE(perf_event, SLAB_PANIC); |
13788 | ||
b01c3a00 JO |
13789 | /* |
13790 | * Build time assertion that we keep the data_head at the intended | |
13791 | * location. IOW, validation we got the __reserved[] size right. | |
13792 | */ | |
13793 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
13794 | != 1024); | |
0793a61d | 13795 | } |
abe43400 | 13796 | |
fd979c01 CS |
13797 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
13798 | char *page) | |
13799 | { | |
13800 | struct perf_pmu_events_attr *pmu_attr = | |
13801 | container_of(attr, struct perf_pmu_events_attr, attr); | |
13802 | ||
13803 | if (pmu_attr->event_str) | |
13804 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
13805 | ||
13806 | return 0; | |
13807 | } | |
675965b0 | 13808 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 13809 | |
abe43400 PZ |
13810 | static int __init perf_event_sysfs_init(void) |
13811 | { | |
13812 | struct pmu *pmu; | |
13813 | int ret; | |
13814 | ||
13815 | mutex_lock(&pmus_lock); | |
13816 | ||
13817 | ret = bus_register(&pmu_bus); | |
13818 | if (ret) | |
13819 | goto unlock; | |
13820 | ||
13821 | list_for_each_entry(pmu, &pmus, entry) { | |
0d6d062c | 13822 | if (pmu->dev) |
abe43400 PZ |
13823 | continue; |
13824 | ||
13825 | ret = pmu_dev_alloc(pmu); | |
13826 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
13827 | } | |
13828 | pmu_bus_running = 1; | |
13829 | ret = 0; | |
13830 | ||
13831 | unlock: | |
13832 | mutex_unlock(&pmus_lock); | |
13833 | ||
13834 | return ret; | |
13835 | } | |
13836 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
13837 | |
13838 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
13839 | static struct cgroup_subsys_state * |
13840 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
13841 | { |
13842 | struct perf_cgroup *jc; | |
e5d1367f | 13843 | |
1b15d055 | 13844 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
13845 | if (!jc) |
13846 | return ERR_PTR(-ENOMEM); | |
13847 | ||
e5d1367f SE |
13848 | jc->info = alloc_percpu(struct perf_cgroup_info); |
13849 | if (!jc->info) { | |
13850 | kfree(jc); | |
13851 | return ERR_PTR(-ENOMEM); | |
13852 | } | |
13853 | ||
e5d1367f SE |
13854 | return &jc->css; |
13855 | } | |
13856 | ||
eb95419b | 13857 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 13858 | { |
eb95419b TH |
13859 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
13860 | ||
e5d1367f SE |
13861 | free_percpu(jc->info); |
13862 | kfree(jc); | |
13863 | } | |
13864 | ||
96aaab68 NK |
13865 | static int perf_cgroup_css_online(struct cgroup_subsys_state *css) |
13866 | { | |
13867 | perf_event_cgroup(css->cgroup); | |
13868 | return 0; | |
13869 | } | |
13870 | ||
e5d1367f SE |
13871 | static int __perf_cgroup_move(void *info) |
13872 | { | |
13873 | struct task_struct *task = info; | |
bd275681 PZ |
13874 | |
13875 | preempt_disable(); | |
f841b682 | 13876 | perf_cgroup_switch(task); |
bd275681 PZ |
13877 | preempt_enable(); |
13878 | ||
e5d1367f SE |
13879 | return 0; |
13880 | } | |
13881 | ||
1f7dd3e5 | 13882 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 13883 | { |
bb9d97b6 | 13884 | struct task_struct *task; |
1f7dd3e5 | 13885 | struct cgroup_subsys_state *css; |
bb9d97b6 | 13886 | |
1f7dd3e5 | 13887 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 13888 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
13889 | } |
13890 | ||
073219e9 | 13891 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
13892 | .css_alloc = perf_cgroup_css_alloc, |
13893 | .css_free = perf_cgroup_css_free, | |
96aaab68 | 13894 | .css_online = perf_cgroup_css_online, |
bb9d97b6 | 13895 | .attach = perf_cgroup_attach, |
968ebff1 TH |
13896 | /* |
13897 | * Implicitly enable on dfl hierarchy so that perf events can | |
13898 | * always be filtered by cgroup2 path as long as perf_event | |
13899 | * controller is not mounted on a legacy hierarchy. | |
13900 | */ | |
13901 | .implicit_on_dfl = true, | |
8cfd8147 | 13902 | .threaded = true, |
e5d1367f SE |
13903 | }; |
13904 | #endif /* CONFIG_CGROUP_PERF */ | |
c22ac2a3 SL |
13905 | |
13906 | DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t); |