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--; |
0f5a2601 PZ |
2305 | if (event->attr.freq && event->attr.sample_freq) |
2306 | ctx->nr_freq--; | |
bd275681 PZ |
2307 | if (event->attr.exclusive || !cpc->active_oncpu) |
2308 | cpc->exclusive = 0; | |
44377277 AS |
2309 | |
2310 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
2311 | } |
2312 | ||
d859e29f | 2313 | static void |
bd275681 | 2314 | group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx) |
d859e29f | 2315 | { |
cdd6c482 | 2316 | struct perf_event *event; |
0d3d73aa PZ |
2317 | |
2318 | if (group_event->state != PERF_EVENT_STATE_ACTIVE) | |
2319 | return; | |
d859e29f | 2320 | |
bd275681 | 2321 | perf_assert_pmu_disabled(group_event->pmu_ctx->pmu); |
3f005e7d | 2322 | |
bd275681 | 2323 | event_sched_out(group_event, ctx); |
d859e29f PM |
2324 | |
2325 | /* | |
2326 | * Schedule out siblings (if any): | |
2327 | */ | |
edb39592 | 2328 | for_each_sibling_event(event, group_event) |
bd275681 | 2329 | event_sched_out(event, ctx); |
d859e29f PM |
2330 | } |
2331 | ||
45a0e07a | 2332 | #define DETACH_GROUP 0x01UL |
ef54c1a4 | 2333 | #define DETACH_CHILD 0x02UL |
517e6a30 | 2334 | #define DETACH_DEAD 0x04UL |
0017960f | 2335 | |
0793a61d | 2336 | /* |
cdd6c482 | 2337 | * Cross CPU call to remove a performance event |
0793a61d | 2338 | * |
cdd6c482 | 2339 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
2340 | * remove it from the context list. |
2341 | */ | |
fae3fde6 PZ |
2342 | static void |
2343 | __perf_remove_from_context(struct perf_event *event, | |
2344 | struct perf_cpu_context *cpuctx, | |
2345 | struct perf_event_context *ctx, | |
2346 | void *info) | |
0793a61d | 2347 | { |
bd275681 | 2348 | struct perf_event_pmu_context *pmu_ctx = event->pmu_ctx; |
45a0e07a | 2349 | unsigned long flags = (unsigned long)info; |
0793a61d | 2350 | |
3c5c8711 PZ |
2351 | if (ctx->is_active & EVENT_TIME) { |
2352 | update_context_time(ctx); | |
09f5e7dc | 2353 | update_cgrp_time_from_cpuctx(cpuctx, false); |
3c5c8711 PZ |
2354 | } |
2355 | ||
517e6a30 PZ |
2356 | /* |
2357 | * Ensure event_sched_out() switches to OFF, at the very least | |
2358 | * this avoids raising perf_pending_task() at this time. | |
2359 | */ | |
2360 | if (flags & DETACH_DEAD) | |
2361 | event->pending_disable = 1; | |
bd275681 | 2362 | event_sched_out(event, ctx); |
45a0e07a | 2363 | if (flags & DETACH_GROUP) |
46ce0fe9 | 2364 | perf_group_detach(event); |
ef54c1a4 PZ |
2365 | if (flags & DETACH_CHILD) |
2366 | perf_child_detach(event); | |
cdd6c482 | 2367 | list_del_event(event, ctx); |
517e6a30 PZ |
2368 | if (flags & DETACH_DEAD) |
2369 | event->state = PERF_EVENT_STATE_DEAD; | |
39a43640 | 2370 | |
bd275681 PZ |
2371 | if (!pmu_ctx->nr_events) { |
2372 | pmu_ctx->rotate_necessary = 0; | |
2373 | ||
2374 | if (ctx->task && ctx->is_active) { | |
2375 | struct perf_cpu_pmu_context *cpc; | |
2376 | ||
2377 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
2378 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
2379 | cpc->task_epc = NULL; | |
2380 | } | |
2381 | } | |
2382 | ||
39a43640 | 2383 | if (!ctx->nr_events && ctx->is_active) { |
09f5e7dc PZ |
2384 | if (ctx == &cpuctx->ctx) |
2385 | update_cgrp_time_from_cpuctx(cpuctx, true); | |
2386 | ||
64ce3126 | 2387 | ctx->is_active = 0; |
39a43640 PZ |
2388 | if (ctx->task) { |
2389 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2390 | cpuctx->task_ctx = NULL; | |
2391 | } | |
64ce3126 | 2392 | } |
0793a61d TG |
2393 | } |
2394 | ||
0793a61d | 2395 | /* |
cdd6c482 | 2396 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 2397 | * |
cdd6c482 IM |
2398 | * If event->ctx is a cloned context, callers must make sure that |
2399 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
2400 | * remains valid. This is OK when called from perf_release since |
2401 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 2402 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 2403 | * context has been detached from its task. |
0793a61d | 2404 | */ |
45a0e07a | 2405 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 2406 | { |
a76a82a3 PZ |
2407 | struct perf_event_context *ctx = event->ctx; |
2408 | ||
2409 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 2410 | |
a76a82a3 | 2411 | /* |
ef54c1a4 PZ |
2412 | * Because of perf_event_exit_task(), perf_remove_from_context() ought |
2413 | * to work in the face of TASK_TOMBSTONE, unlike every other | |
2414 | * event_function_call() user. | |
a76a82a3 | 2415 | */ |
ef54c1a4 | 2416 | raw_spin_lock_irq(&ctx->lock); |
bd275681 PZ |
2417 | if (!ctx->is_active) { |
2418 | __perf_remove_from_context(event, this_cpu_ptr(&perf_cpu_context), | |
ef54c1a4 | 2419 | ctx, (void *)flags); |
a76a82a3 | 2420 | raw_spin_unlock_irq(&ctx->lock); |
ef54c1a4 | 2421 | return; |
a76a82a3 | 2422 | } |
ef54c1a4 PZ |
2423 | raw_spin_unlock_irq(&ctx->lock); |
2424 | ||
2425 | event_function_call(event, __perf_remove_from_context, (void *)flags); | |
0793a61d TG |
2426 | } |
2427 | ||
d859e29f | 2428 | /* |
cdd6c482 | 2429 | * Cross CPU call to disable a performance event |
d859e29f | 2430 | */ |
fae3fde6 PZ |
2431 | static void __perf_event_disable(struct perf_event *event, |
2432 | struct perf_cpu_context *cpuctx, | |
2433 | struct perf_event_context *ctx, | |
2434 | void *info) | |
7b648018 | 2435 | { |
fae3fde6 PZ |
2436 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
2437 | return; | |
7b648018 | 2438 | |
3c5c8711 PZ |
2439 | if (ctx->is_active & EVENT_TIME) { |
2440 | update_context_time(ctx); | |
2441 | update_cgrp_time_from_event(event); | |
2442 | } | |
2443 | ||
bd275681 PZ |
2444 | perf_pmu_disable(event->pmu_ctx->pmu); |
2445 | ||
fae3fde6 | 2446 | if (event == event->group_leader) |
bd275681 | 2447 | group_sched_out(event, ctx); |
fae3fde6 | 2448 | else |
bd275681 | 2449 | event_sched_out(event, ctx); |
0d3d73aa PZ |
2450 | |
2451 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); | |
33238c50 | 2452 | perf_cgroup_event_disable(event, ctx); |
bd275681 PZ |
2453 | |
2454 | perf_pmu_enable(event->pmu_ctx->pmu); | |
7b648018 PZ |
2455 | } |
2456 | ||
d859e29f | 2457 | /* |
788faab7 | 2458 | * Disable an event. |
c93f7669 | 2459 | * |
cdd6c482 IM |
2460 | * If event->ctx is a cloned context, callers must make sure that |
2461 | * every task struct that event->ctx->task could possibly point to | |
9f014e3a | 2462 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2463 | * perf_event_for_each_child or perf_event_for_each because they |
2464 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
2465 | * goes to exit will block in perf_event_exit_event(). |
2466 | * | |
ca6c2132 | 2467 | * When called from perf_pending_irq it's OK because event->ctx |
c93f7669 | 2468 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 2469 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 2470 | */ |
f63a8daa | 2471 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 2472 | { |
cdd6c482 | 2473 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2474 | |
e625cce1 | 2475 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 2476 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 2477 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2478 | return; |
53cfbf59 | 2479 | } |
e625cce1 | 2480 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2481 | |
fae3fde6 PZ |
2482 | event_function_call(event, __perf_event_disable, NULL); |
2483 | } | |
2484 | ||
2485 | void perf_event_disable_local(struct perf_event *event) | |
2486 | { | |
2487 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 2488 | } |
f63a8daa PZ |
2489 | |
2490 | /* | |
2491 | * Strictly speaking kernel users cannot create groups and therefore this | |
2492 | * interface does not need the perf_event_ctx_lock() magic. | |
2493 | */ | |
2494 | void perf_event_disable(struct perf_event *event) | |
2495 | { | |
2496 | struct perf_event_context *ctx; | |
2497 | ||
2498 | ctx = perf_event_ctx_lock(event); | |
2499 | _perf_event_disable(event); | |
2500 | perf_event_ctx_unlock(event, ctx); | |
2501 | } | |
dcfce4a0 | 2502 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2503 | |
5aab90ce JO |
2504 | void perf_event_disable_inatomic(struct perf_event *event) |
2505 | { | |
ca6c2132 PZ |
2506 | event->pending_disable = 1; |
2507 | irq_work_queue(&event->pending_irq); | |
5aab90ce JO |
2508 | } |
2509 | ||
4fe757dd PZ |
2510 | #define MAX_INTERRUPTS (~0ULL) |
2511 | ||
2512 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2513 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2514 | |
235c7fc7 | 2515 | static int |
bd275681 | 2516 | event_sched_in(struct perf_event *event, struct perf_event_context *ctx) |
235c7fc7 | 2517 | { |
bd275681 PZ |
2518 | struct perf_event_pmu_context *epc = event->pmu_ctx; |
2519 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); | |
44377277 | 2520 | int ret = 0; |
4158755d | 2521 | |
ab6f824c PZ |
2522 | WARN_ON_ONCE(event->ctx != ctx); |
2523 | ||
63342411 PZ |
2524 | lockdep_assert_held(&ctx->lock); |
2525 | ||
cdd6c482 | 2526 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2527 | return 0; |
2528 | ||
95ff4ca2 AS |
2529 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2530 | /* | |
0c1cbc18 PZ |
2531 | * Order event::oncpu write to happen before the ACTIVE state is |
2532 | * visible. This allows perf_event_{stop,read}() to observe the correct | |
2533 | * ->oncpu if it sees ACTIVE. | |
95ff4ca2 AS |
2534 | */ |
2535 | smp_wmb(); | |
0d3d73aa | 2536 | perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); |
4fe757dd PZ |
2537 | |
2538 | /* | |
2539 | * Unthrottle events, since we scheduled we might have missed several | |
2540 | * ticks already, also for a heavily scheduling task there is little | |
2541 | * guarantee it'll get a tick in a timely manner. | |
2542 | */ | |
2543 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2544 | perf_log_throttle(event, 1); | |
2545 | event->hw.interrupts = 0; | |
2546 | } | |
2547 | ||
44377277 AS |
2548 | perf_pmu_disable(event->pmu); |
2549 | ||
ec0d7729 AS |
2550 | perf_log_itrace_start(event); |
2551 | ||
a4eaf7f1 | 2552 | if (event->pmu->add(event, PERF_EF_START)) { |
0d3d73aa | 2553 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
cdd6c482 | 2554 | event->oncpu = -1; |
44377277 AS |
2555 | ret = -EAGAIN; |
2556 | goto out; | |
235c7fc7 IM |
2557 | } |
2558 | ||
cdd6c482 | 2559 | if (!is_software_event(event)) |
bd275681 | 2560 | cpc->active_oncpu++; |
0f5a2601 PZ |
2561 | if (event->attr.freq && event->attr.sample_freq) |
2562 | ctx->nr_freq++; | |
235c7fc7 | 2563 | |
cdd6c482 | 2564 | if (event->attr.exclusive) |
bd275681 | 2565 | cpc->exclusive = 1; |
3b6f9e5c | 2566 | |
44377277 AS |
2567 | out: |
2568 | perf_pmu_enable(event->pmu); | |
2569 | ||
2570 | return ret; | |
235c7fc7 IM |
2571 | } |
2572 | ||
6751b71e | 2573 | static int |
bd275681 | 2574 | group_sched_in(struct perf_event *group_event, struct perf_event_context *ctx) |
6751b71e | 2575 | { |
6bde9b6c | 2576 | struct perf_event *event, *partial_group = NULL; |
bd275681 | 2577 | struct pmu *pmu = group_event->pmu_ctx->pmu; |
6751b71e | 2578 | |
cdd6c482 | 2579 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2580 | return 0; |
2581 | ||
fbbe0701 | 2582 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2583 | |
bd275681 | 2584 | if (event_sched_in(group_event, ctx)) |
251ff2d4 | 2585 | goto error; |
6751b71e PM |
2586 | |
2587 | /* | |
2588 | * Schedule in siblings as one group (if any): | |
2589 | */ | |
edb39592 | 2590 | for_each_sibling_event(event, group_event) { |
bd275681 | 2591 | if (event_sched_in(event, ctx)) { |
cdd6c482 | 2592 | partial_group = event; |
6751b71e PM |
2593 | goto group_error; |
2594 | } | |
2595 | } | |
2596 | ||
9ffcfa6f | 2597 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2598 | return 0; |
9ffcfa6f | 2599 | |
6751b71e PM |
2600 | group_error: |
2601 | /* | |
2602 | * Groups can be scheduled in as one unit only, so undo any | |
2603 | * partial group before returning: | |
0d3d73aa | 2604 | * The events up to the failed event are scheduled out normally. |
6751b71e | 2605 | */ |
edb39592 | 2606 | for_each_sibling_event(event, group_event) { |
cdd6c482 | 2607 | if (event == partial_group) |
0d3d73aa | 2608 | break; |
d7842da4 | 2609 | |
bd275681 | 2610 | event_sched_out(event, ctx); |
6751b71e | 2611 | } |
bd275681 | 2612 | event_sched_out(group_event, ctx); |
6751b71e | 2613 | |
251ff2d4 | 2614 | error: |
ad5133b7 | 2615 | pmu->cancel_txn(pmu); |
6751b71e PM |
2616 | return -EAGAIN; |
2617 | } | |
2618 | ||
3b6f9e5c | 2619 | /* |
cdd6c482 | 2620 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2621 | */ |
bd275681 | 2622 | static int group_can_go_on(struct perf_event *event, int can_add_hw) |
3b6f9e5c | 2623 | { |
bd275681 PZ |
2624 | struct perf_event_pmu_context *epc = event->pmu_ctx; |
2625 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); | |
2626 | ||
3b6f9e5c | 2627 | /* |
cdd6c482 | 2628 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2629 | */ |
4ff6a8de | 2630 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2631 | return 1; |
2632 | /* | |
2633 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2634 | * events can go on. |
3b6f9e5c | 2635 | */ |
bd275681 | 2636 | if (cpc->exclusive) |
3b6f9e5c PM |
2637 | return 0; |
2638 | /* | |
2639 | * If this group is exclusive and there are already | |
cdd6c482 | 2640 | * events on the CPU, it can't go on. |
3b6f9e5c | 2641 | */ |
1908dc91 | 2642 | if (event->attr.exclusive && !list_empty(get_event_list(event))) |
3b6f9e5c PM |
2643 | return 0; |
2644 | /* | |
2645 | * Otherwise, try to add it if all previous groups were able | |
2646 | * to go on. | |
2647 | */ | |
2648 | return can_add_hw; | |
2649 | } | |
2650 | ||
cdd6c482 IM |
2651 | static void add_event_to_ctx(struct perf_event *event, |
2652 | struct perf_event_context *ctx) | |
53cfbf59 | 2653 | { |
cdd6c482 | 2654 | list_add_event(event, ctx); |
8a49542c | 2655 | perf_group_attach(event); |
53cfbf59 PM |
2656 | } |
2657 | ||
bd275681 PZ |
2658 | static void task_ctx_sched_out(struct perf_event_context *ctx, |
2659 | enum event_type_t event_type) | |
bd2afa49 | 2660 | { |
bd275681 PZ |
2661 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
2662 | ||
bd2afa49 PZ |
2663 | if (!cpuctx->task_ctx) |
2664 | return; | |
2665 | ||
2666 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2667 | return; | |
2668 | ||
bd275681 | 2669 | ctx_sched_out(ctx, event_type); |
bd2afa49 PZ |
2670 | } |
2671 | ||
dce5855b | 2672 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
a0827713 | 2673 | struct perf_event_context *ctx) |
dce5855b | 2674 | { |
bd275681 | 2675 | ctx_sched_in(&cpuctx->ctx, EVENT_PINNED); |
dce5855b | 2676 | if (ctx) |
bd275681 PZ |
2677 | ctx_sched_in(ctx, EVENT_PINNED); |
2678 | ctx_sched_in(&cpuctx->ctx, EVENT_FLEXIBLE); | |
dce5855b | 2679 | if (ctx) |
bd275681 | 2680 | ctx_sched_in(ctx, EVENT_FLEXIBLE); |
dce5855b PZ |
2681 | } |
2682 | ||
487f05e1 AS |
2683 | /* |
2684 | * We want to maintain the following priority of scheduling: | |
2685 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2686 | * - task pinned (EVENT_PINNED) | |
2687 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2688 | * - task flexible (EVENT_FLEXIBLE). | |
2689 | * | |
2690 | * In order to avoid unscheduling and scheduling back in everything every | |
2691 | * time an event is added, only do it for the groups of equal priority and | |
2692 | * below. | |
2693 | * | |
2694 | * This can be called after a batch operation on task events, in which case | |
2695 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2696 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2697 | */ | |
bd275681 PZ |
2698 | /* |
2699 | * XXX: ctx_resched() reschedule entire perf_event_context while adding new | |
2700 | * event to the context or enabling existing event in the context. We can | |
2701 | * probably optimize it by rescheduling only affected pmu_ctx. | |
2702 | */ | |
3e349507 | 2703 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2704 | struct perf_event_context *task_ctx, |
2705 | enum event_type_t event_type) | |
0017960f | 2706 | { |
487f05e1 AS |
2707 | bool cpu_event = !!(event_type & EVENT_CPU); |
2708 | ||
2709 | /* | |
2710 | * If pinned groups are involved, flexible groups also need to be | |
2711 | * scheduled out. | |
2712 | */ | |
2713 | if (event_type & EVENT_PINNED) | |
2714 | event_type |= EVENT_FLEXIBLE; | |
2715 | ||
bd275681 | 2716 | event_type &= EVENT_ALL; |
bd903afe | 2717 | |
f06cc667 | 2718 | perf_ctx_disable(&cpuctx->ctx, false); |
bd275681 | 2719 | if (task_ctx) { |
f06cc667 | 2720 | perf_ctx_disable(task_ctx, false); |
bd275681 PZ |
2721 | task_ctx_sched_out(task_ctx, event_type); |
2722 | } | |
487f05e1 AS |
2723 | |
2724 | /* | |
2725 | * Decide which cpu ctx groups to schedule out based on the types | |
2726 | * of events that caused rescheduling: | |
2727 | * - EVENT_CPU: schedule out corresponding groups; | |
2728 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2729 | * - otherwise, do nothing more. | |
2730 | */ | |
2731 | if (cpu_event) | |
bd275681 PZ |
2732 | ctx_sched_out(&cpuctx->ctx, event_type); |
2733 | else if (event_type & EVENT_PINNED) | |
2734 | ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); | |
487f05e1 | 2735 | |
a0827713 | 2736 | perf_event_sched_in(cpuctx, task_ctx); |
bd275681 | 2737 | |
f06cc667 | 2738 | perf_ctx_enable(&cpuctx->ctx, false); |
bd275681 | 2739 | if (task_ctx) |
f06cc667 | 2740 | perf_ctx_enable(task_ctx, false); |
0017960f PZ |
2741 | } |
2742 | ||
c68d224e SE |
2743 | void perf_pmu_resched(struct pmu *pmu) |
2744 | { | |
bd275681 | 2745 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
c68d224e SE |
2746 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
2747 | ||
2748 | perf_ctx_lock(cpuctx, task_ctx); | |
2749 | ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU); | |
2750 | perf_ctx_unlock(cpuctx, task_ctx); | |
2751 | } | |
2752 | ||
0793a61d | 2753 | /* |
cdd6c482 | 2754 | * Cross CPU call to install and enable a performance event |
682076ae | 2755 | * |
a096309b PZ |
2756 | * Very similar to remote_function() + event_function() but cannot assume that |
2757 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2758 | */ |
fe4b04fa | 2759 | static int __perf_install_in_context(void *info) |
0793a61d | 2760 | { |
a096309b PZ |
2761 | struct perf_event *event = info; |
2762 | struct perf_event_context *ctx = event->ctx; | |
bd275681 | 2763 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
2c29ef0f | 2764 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2765 | bool reprogram = true; |
a096309b | 2766 | int ret = 0; |
0793a61d | 2767 | |
63b6da39 | 2768 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2769 | if (ctx->task) { |
b58f6b0d PZ |
2770 | raw_spin_lock(&ctx->lock); |
2771 | task_ctx = ctx; | |
a096309b | 2772 | |
63cae12b | 2773 | reprogram = (ctx->task == current); |
b58f6b0d | 2774 | |
39a43640 | 2775 | /* |
63cae12b PZ |
2776 | * If the task is running, it must be running on this CPU, |
2777 | * otherwise we cannot reprogram things. | |
2778 | * | |
2779 | * If its not running, we don't care, ctx->lock will | |
2780 | * serialize against it becoming runnable. | |
39a43640 | 2781 | */ |
63cae12b PZ |
2782 | if (task_curr(ctx->task) && !reprogram) { |
2783 | ret = -ESRCH; | |
2784 | goto unlock; | |
2785 | } | |
a096309b | 2786 | |
63cae12b | 2787 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2788 | } else if (task_ctx) { |
2789 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2790 | } |
b58f6b0d | 2791 | |
33801b94 | 2792 | #ifdef CONFIG_CGROUP_PERF |
33238c50 | 2793 | if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) { |
33801b94 | 2794 | /* |
2795 | * If the current cgroup doesn't match the event's | |
2796 | * cgroup, we should not try to schedule it. | |
2797 | */ | |
2798 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); | |
2799 | reprogram = cgroup_is_descendant(cgrp->css.cgroup, | |
2800 | event->cgrp->css.cgroup); | |
2801 | } | |
2802 | #endif | |
2803 | ||
63cae12b | 2804 | if (reprogram) { |
bd275681 | 2805 | ctx_sched_out(ctx, EVENT_TIME); |
a096309b | 2806 | add_event_to_ctx(event, ctx); |
487f05e1 | 2807 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2808 | } else { |
2809 | add_event_to_ctx(event, ctx); | |
2810 | } | |
2811 | ||
63b6da39 | 2812 | unlock: |
2c29ef0f | 2813 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2814 | |
a096309b | 2815 | return ret; |
0793a61d TG |
2816 | } |
2817 | ||
8a58ddae AS |
2818 | static bool exclusive_event_installable(struct perf_event *event, |
2819 | struct perf_event_context *ctx); | |
2820 | ||
0793a61d | 2821 | /* |
a096309b PZ |
2822 | * Attach a performance event to a context. |
2823 | * | |
2824 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2825 | */ |
2826 | static void | |
cdd6c482 IM |
2827 | perf_install_in_context(struct perf_event_context *ctx, |
2828 | struct perf_event *event, | |
0793a61d TG |
2829 | int cpu) |
2830 | { | |
a096309b | 2831 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2832 | |
fe4b04fa PZ |
2833 | lockdep_assert_held(&ctx->mutex); |
2834 | ||
8a58ddae AS |
2835 | WARN_ON_ONCE(!exclusive_event_installable(event, ctx)); |
2836 | ||
0cda4c02 | 2837 | if (event->cpu != -1) |
bd275681 | 2838 | WARN_ON_ONCE(event->cpu != cpu); |
c3f00c70 | 2839 | |
0b8f1e2e PZ |
2840 | /* |
2841 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2842 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2843 | */ | |
2844 | smp_store_release(&event->ctx, ctx); | |
2845 | ||
db0503e4 PZ |
2846 | /* |
2847 | * perf_event_attr::disabled events will not run and can be initialized | |
2848 | * without IPI. Except when this is the first event for the context, in | |
2849 | * that case we need the magic of the IPI to set ctx->is_active. | |
2850 | * | |
2851 | * The IOC_ENABLE that is sure to follow the creation of a disabled | |
2852 | * event will issue the IPI and reprogram the hardware. | |
2853 | */ | |
c5de60cd NK |
2854 | if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && |
2855 | ctx->nr_events && !is_cgroup_event(event)) { | |
db0503e4 PZ |
2856 | raw_spin_lock_irq(&ctx->lock); |
2857 | if (ctx->task == TASK_TOMBSTONE) { | |
2858 | raw_spin_unlock_irq(&ctx->lock); | |
2859 | return; | |
2860 | } | |
2861 | add_event_to_ctx(event, ctx); | |
2862 | raw_spin_unlock_irq(&ctx->lock); | |
2863 | return; | |
2864 | } | |
2865 | ||
a096309b PZ |
2866 | if (!task) { |
2867 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2868 | return; | |
2869 | } | |
2870 | ||
2871 | /* | |
2872 | * Should not happen, we validate the ctx is still alive before calling. | |
2873 | */ | |
2874 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2875 | return; | |
2876 | ||
39a43640 PZ |
2877 | /* |
2878 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2879 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2880 | * |
2881 | * Instead we use task_curr(), which tells us if the task is running. | |
2882 | * However, since we use task_curr() outside of rq::lock, we can race | |
2883 | * against the actual state. This means the result can be wrong. | |
2884 | * | |
2885 | * If we get a false positive, we retry, this is harmless. | |
2886 | * | |
2887 | * If we get a false negative, things are complicated. If we are after | |
2888 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2889 | * value must be correct. If we're before, it doesn't matter since | |
2890 | * perf_event_context_sched_in() will program the counter. | |
2891 | * | |
2892 | * However, this hinges on the remote context switch having observed | |
2893 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2894 | * ctx::lock in perf_event_context_sched_in(). | |
2895 | * | |
2896 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2897 | * we know any future context switch of task must see the | |
2898 | * perf_event_ctpx[] store. | |
39a43640 | 2899 | */ |
63cae12b | 2900 | |
63b6da39 | 2901 | /* |
63cae12b PZ |
2902 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2903 | * task_cpu() load, such that if the IPI then does not find the task | |
2904 | * running, a future context switch of that task must observe the | |
2905 | * store. | |
63b6da39 | 2906 | */ |
63cae12b PZ |
2907 | smp_mb(); |
2908 | again: | |
2909 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2910 | return; |
2911 | ||
2912 | raw_spin_lock_irq(&ctx->lock); | |
2913 | task = ctx->task; | |
84c4e620 | 2914 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2915 | /* |
2916 | * Cannot happen because we already checked above (which also | |
2917 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2918 | * against perf_event_exit_task_context(). | |
2919 | */ | |
63b6da39 PZ |
2920 | raw_spin_unlock_irq(&ctx->lock); |
2921 | return; | |
2922 | } | |
39a43640 | 2923 | /* |
63cae12b PZ |
2924 | * If the task is not running, ctx->lock will avoid it becoming so, |
2925 | * thus we can safely install the event. | |
39a43640 | 2926 | */ |
63cae12b PZ |
2927 | if (task_curr(task)) { |
2928 | raw_spin_unlock_irq(&ctx->lock); | |
2929 | goto again; | |
2930 | } | |
2931 | add_event_to_ctx(event, ctx); | |
2932 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2933 | } |
2934 | ||
d859e29f | 2935 | /* |
cdd6c482 | 2936 | * Cross CPU call to enable a performance event |
d859e29f | 2937 | */ |
fae3fde6 PZ |
2938 | static void __perf_event_enable(struct perf_event *event, |
2939 | struct perf_cpu_context *cpuctx, | |
2940 | struct perf_event_context *ctx, | |
2941 | void *info) | |
04289bb9 | 2942 | { |
cdd6c482 | 2943 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2944 | struct perf_event_context *task_ctx; |
04289bb9 | 2945 | |
6e801e01 PZ |
2946 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2947 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2948 | return; |
3cbed429 | 2949 | |
bd2afa49 | 2950 | if (ctx->is_active) |
bd275681 | 2951 | ctx_sched_out(ctx, EVENT_TIME); |
bd2afa49 | 2952 | |
0d3d73aa | 2953 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
33238c50 | 2954 | perf_cgroup_event_enable(event, ctx); |
04289bb9 | 2955 | |
fae3fde6 PZ |
2956 | if (!ctx->is_active) |
2957 | return; | |
2958 | ||
e5d1367f | 2959 | if (!event_filter_match(event)) { |
bd275681 | 2960 | ctx_sched_in(ctx, EVENT_TIME); |
fae3fde6 | 2961 | return; |
e5d1367f | 2962 | } |
f4c4176f | 2963 | |
04289bb9 | 2964 | /* |
cdd6c482 | 2965 | * If the event is in a group and isn't the group leader, |
d859e29f | 2966 | * then don't put it on unless the group is on. |
04289bb9 | 2967 | */ |
bd2afa49 | 2968 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
bd275681 | 2969 | ctx_sched_in(ctx, EVENT_TIME); |
fae3fde6 | 2970 | return; |
bd2afa49 | 2971 | } |
fe4b04fa | 2972 | |
fae3fde6 PZ |
2973 | task_ctx = cpuctx->task_ctx; |
2974 | if (ctx->task) | |
2975 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2976 | |
487f05e1 | 2977 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2978 | } |
2979 | ||
d859e29f | 2980 | /* |
788faab7 | 2981 | * Enable an event. |
c93f7669 | 2982 | * |
cdd6c482 IM |
2983 | * If event->ctx is a cloned context, callers must make sure that |
2984 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2985 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2986 | * perf_event_for_each_child or perf_event_for_each as described |
2987 | * for perf_event_disable. | |
d859e29f | 2988 | */ |
f63a8daa | 2989 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2990 | { |
cdd6c482 | 2991 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2992 | |
7b648018 | 2993 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2994 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2995 | event->state < PERF_EVENT_STATE_ERROR) { | |
9f0c4fa1 | 2996 | out: |
7b648018 | 2997 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2998 | return; |
2999 | } | |
3000 | ||
d859e29f | 3001 | /* |
cdd6c482 | 3002 | * If the event is in error state, clear that first. |
7b648018 PZ |
3003 | * |
3004 | * That way, if we see the event in error state below, we know that it | |
3005 | * has gone back into error state, as distinct from the task having | |
3006 | * been scheduled away before the cross-call arrived. | |
d859e29f | 3007 | */ |
9f0c4fa1 KL |
3008 | if (event->state == PERF_EVENT_STATE_ERROR) { |
3009 | /* | |
3010 | * Detached SIBLING events cannot leave ERROR state. | |
3011 | */ | |
3012 | if (event->event_caps & PERF_EV_CAP_SIBLING && | |
3013 | event->group_leader == event) | |
3014 | goto out; | |
3015 | ||
cdd6c482 | 3016 | event->state = PERF_EVENT_STATE_OFF; |
9f0c4fa1 | 3017 | } |
e625cce1 | 3018 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 3019 | |
fae3fde6 | 3020 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 3021 | } |
f63a8daa PZ |
3022 | |
3023 | /* | |
3024 | * See perf_event_disable(); | |
3025 | */ | |
3026 | void perf_event_enable(struct perf_event *event) | |
3027 | { | |
3028 | struct perf_event_context *ctx; | |
3029 | ||
3030 | ctx = perf_event_ctx_lock(event); | |
3031 | _perf_event_enable(event); | |
3032 | perf_event_ctx_unlock(event, ctx); | |
3033 | } | |
dcfce4a0 | 3034 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 3035 | |
375637bc AS |
3036 | struct stop_event_data { |
3037 | struct perf_event *event; | |
3038 | unsigned int restart; | |
3039 | }; | |
3040 | ||
95ff4ca2 AS |
3041 | static int __perf_event_stop(void *info) |
3042 | { | |
375637bc AS |
3043 | struct stop_event_data *sd = info; |
3044 | struct perf_event *event = sd->event; | |
95ff4ca2 | 3045 | |
375637bc | 3046 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
3047 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
3048 | return 0; | |
3049 | ||
3050 | /* matches smp_wmb() in event_sched_in() */ | |
3051 | smp_rmb(); | |
3052 | ||
3053 | /* | |
3054 | * There is a window with interrupts enabled before we get here, | |
3055 | * so we need to check again lest we try to stop another CPU's event. | |
3056 | */ | |
3057 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
3058 | return -EAGAIN; | |
3059 | ||
3060 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3061 | ||
375637bc AS |
3062 | /* |
3063 | * May race with the actual stop (through perf_pmu_output_stop()), | |
3064 | * but it is only used for events with AUX ring buffer, and such | |
3065 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
3066 | * see comments in perf_aux_output_begin(). | |
3067 | * | |
788faab7 | 3068 | * Since this is happening on an event-local CPU, no trace is lost |
375637bc AS |
3069 | * while restarting. |
3070 | */ | |
3071 | if (sd->restart) | |
c9bbdd48 | 3072 | event->pmu->start(event, 0); |
375637bc | 3073 | |
95ff4ca2 AS |
3074 | return 0; |
3075 | } | |
3076 | ||
767ae086 | 3077 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
3078 | { |
3079 | struct stop_event_data sd = { | |
3080 | .event = event, | |
767ae086 | 3081 | .restart = restart, |
375637bc AS |
3082 | }; |
3083 | int ret = 0; | |
3084 | ||
3085 | do { | |
3086 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
3087 | return 0; | |
3088 | ||
3089 | /* matches smp_wmb() in event_sched_in() */ | |
3090 | smp_rmb(); | |
3091 | ||
3092 | /* | |
3093 | * We only want to restart ACTIVE events, so if the event goes | |
3094 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
3095 | * fall through with ret==-ENXIO. | |
3096 | */ | |
3097 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
3098 | __perf_event_stop, &sd); | |
3099 | } while (ret == -EAGAIN); | |
3100 | ||
3101 | return ret; | |
3102 | } | |
3103 | ||
3104 | /* | |
3105 | * In order to contain the amount of racy and tricky in the address filter | |
3106 | * configuration management, it is a two part process: | |
3107 | * | |
3108 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
3109 | * we update the addresses of corresponding vmas in | |
c60f83b8 | 3110 | * event::addr_filter_ranges array and bump the event::addr_filters_gen; |
375637bc AS |
3111 | * (p2) when an event is scheduled in (pmu::add), it calls |
3112 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
3113 | * if the generation has changed since the previous call. | |
3114 | * | |
3115 | * If (p1) happens while the event is active, we restart it to force (p2). | |
3116 | * | |
3117 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
3118 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
3119 | * ioctl; | |
3120 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
c1e8d7c6 | 3121 | * registered mapping, called for every new mmap(), with mm::mmap_lock down |
375637bc AS |
3122 | * for reading; |
3123 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
3124 | * of exec. | |
3125 | */ | |
3126 | void perf_event_addr_filters_sync(struct perf_event *event) | |
3127 | { | |
3128 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
3129 | ||
3130 | if (!has_addr_filter(event)) | |
3131 | return; | |
3132 | ||
3133 | raw_spin_lock(&ifh->lock); | |
3134 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
3135 | event->pmu->addr_filters_sync(event); | |
3136 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
3137 | } | |
3138 | raw_spin_unlock(&ifh->lock); | |
3139 | } | |
3140 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
3141 | ||
f63a8daa | 3142 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 3143 | { |
2023b359 | 3144 | /* |
cdd6c482 | 3145 | * not supported on inherited events |
2023b359 | 3146 | */ |
2e939d1d | 3147 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
3148 | return -EINVAL; |
3149 | ||
cdd6c482 | 3150 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 3151 | _perf_event_enable(event); |
2023b359 PZ |
3152 | |
3153 | return 0; | |
79f14641 | 3154 | } |
f63a8daa PZ |
3155 | |
3156 | /* | |
3157 | * See perf_event_disable() | |
3158 | */ | |
3159 | int perf_event_refresh(struct perf_event *event, int refresh) | |
3160 | { | |
3161 | struct perf_event_context *ctx; | |
3162 | int ret; | |
3163 | ||
3164 | ctx = perf_event_ctx_lock(event); | |
3165 | ret = _perf_event_refresh(event, refresh); | |
3166 | perf_event_ctx_unlock(event, ctx); | |
3167 | ||
3168 | return ret; | |
3169 | } | |
26ca5c11 | 3170 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 3171 | |
32ff77e8 MC |
3172 | static int perf_event_modify_breakpoint(struct perf_event *bp, |
3173 | struct perf_event_attr *attr) | |
3174 | { | |
3175 | int err; | |
3176 | ||
3177 | _perf_event_disable(bp); | |
3178 | ||
3179 | err = modify_user_hw_breakpoint_check(bp, attr, true); | |
32ff77e8 | 3180 | |
bf06278c | 3181 | if (!bp->attr.disabled) |
32ff77e8 | 3182 | _perf_event_enable(bp); |
bf06278c JO |
3183 | |
3184 | return err; | |
32ff77e8 MC |
3185 | } |
3186 | ||
3c25fc97 ME |
3187 | /* |
3188 | * Copy event-type-independent attributes that may be modified. | |
3189 | */ | |
3190 | static void perf_event_modify_copy_attr(struct perf_event_attr *to, | |
3191 | const struct perf_event_attr *from) | |
3192 | { | |
3193 | to->sig_data = from->sig_data; | |
3194 | } | |
3195 | ||
32ff77e8 MC |
3196 | static int perf_event_modify_attr(struct perf_event *event, |
3197 | struct perf_event_attr *attr) | |
3198 | { | |
47f661ec ME |
3199 | int (*func)(struct perf_event *, struct perf_event_attr *); |
3200 | struct perf_event *child; | |
3201 | int err; | |
3202 | ||
32ff77e8 MC |
3203 | if (event->attr.type != attr->type) |
3204 | return -EINVAL; | |
3205 | ||
3206 | switch (event->attr.type) { | |
3207 | case PERF_TYPE_BREAKPOINT: | |
47f661ec ME |
3208 | func = perf_event_modify_breakpoint; |
3209 | break; | |
32ff77e8 MC |
3210 | default: |
3211 | /* Place holder for future additions. */ | |
3212 | return -EOPNOTSUPP; | |
3213 | } | |
47f661ec ME |
3214 | |
3215 | WARN_ON_ONCE(event->ctx->parent_ctx); | |
3216 | ||
3217 | mutex_lock(&event->child_mutex); | |
3c25fc97 ME |
3218 | /* |
3219 | * Event-type-independent attributes must be copied before event-type | |
3220 | * modification, which will validate that final attributes match the | |
3221 | * source attributes after all relevant attributes have been copied. | |
3222 | */ | |
3223 | perf_event_modify_copy_attr(&event->attr, attr); | |
47f661ec ME |
3224 | err = func(event, attr); |
3225 | if (err) | |
3226 | goto out; | |
3227 | list_for_each_entry(child, &event->child_list, child_list) { | |
3c25fc97 | 3228 | perf_event_modify_copy_attr(&child->attr, attr); |
47f661ec ME |
3229 | err = func(child, attr); |
3230 | if (err) | |
3231 | goto out; | |
3232 | } | |
3233 | out: | |
3234 | mutex_unlock(&event->child_mutex); | |
3235 | return err; | |
32ff77e8 MC |
3236 | } |
3237 | ||
bd275681 PZ |
3238 | static void __pmu_ctx_sched_out(struct perf_event_pmu_context *pmu_ctx, |
3239 | enum event_type_t event_type) | |
235c7fc7 | 3240 | { |
bd275681 | 3241 | struct perf_event_context *ctx = pmu_ctx->ctx; |
6668128a | 3242 | struct perf_event *event, *tmp; |
bd275681 PZ |
3243 | struct pmu *pmu = pmu_ctx->pmu; |
3244 | ||
3245 | if (ctx->task && !ctx->is_active) { | |
3246 | struct perf_cpu_pmu_context *cpc; | |
3247 | ||
3248 | cpc = this_cpu_ptr(pmu->cpu_pmu_context); | |
3249 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
3250 | cpc->task_epc = NULL; | |
3251 | } | |
3252 | ||
3253 | if (!event_type) | |
3254 | return; | |
3255 | ||
3256 | perf_pmu_disable(pmu); | |
3257 | if (event_type & EVENT_PINNED) { | |
3258 | list_for_each_entry_safe(event, tmp, | |
3259 | &pmu_ctx->pinned_active, | |
3260 | active_list) | |
3261 | group_sched_out(event, ctx); | |
3262 | } | |
3263 | ||
3264 | if (event_type & EVENT_FLEXIBLE) { | |
3265 | list_for_each_entry_safe(event, tmp, | |
3266 | &pmu_ctx->flexible_active, | |
3267 | active_list) | |
3268 | group_sched_out(event, ctx); | |
3269 | /* | |
3270 | * Since we cleared EVENT_FLEXIBLE, also clear | |
3271 | * rotate_necessary, is will be reset by | |
3272 | * ctx_flexible_sched_in() when needed. | |
3273 | */ | |
3274 | pmu_ctx->rotate_necessary = 0; | |
3275 | } | |
3276 | perf_pmu_enable(pmu); | |
3277 | } | |
3278 | ||
3279 | static void | |
3280 | ctx_sched_out(struct perf_event_context *ctx, enum event_type_t event_type) | |
3281 | { | |
3282 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); | |
3283 | struct perf_event_pmu_context *pmu_ctx; | |
db24d33e | 3284 | int is_active = ctx->is_active; |
f06cc667 PZ |
3285 | bool cgroup = event_type & EVENT_CGROUP; |
3286 | ||
3287 | event_type &= ~EVENT_CGROUP; | |
235c7fc7 | 3288 | |
c994d613 | 3289 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 3290 | |
39a43640 PZ |
3291 | if (likely(!ctx->nr_events)) { |
3292 | /* | |
3293 | * See __perf_remove_from_context(). | |
3294 | */ | |
3295 | WARN_ON_ONCE(ctx->is_active); | |
3296 | if (ctx->task) | |
3297 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 3298 | return; |
39a43640 PZ |
3299 | } |
3300 | ||
8fdc6539 PZ |
3301 | /* |
3302 | * Always update time if it was set; not only when it changes. | |
3303 | * Otherwise we can 'forget' to update time for any but the last | |
3304 | * context we sched out. For example: | |
3305 | * | |
3306 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
3307 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
3308 | * | |
3309 | * would only update time for the pinned events. | |
3310 | */ | |
3cbaa590 PZ |
3311 | if (is_active & EVENT_TIME) { |
3312 | /* update (and stop) ctx time */ | |
3313 | update_context_time(ctx); | |
09f5e7dc PZ |
3314 | update_cgrp_time_from_cpuctx(cpuctx, ctx == &cpuctx->ctx); |
3315 | /* | |
3316 | * CPU-release for the below ->is_active store, | |
3317 | * see __load_acquire() in perf_event_time_now() | |
3318 | */ | |
3319 | barrier(); | |
3320 | } | |
3321 | ||
3322 | ctx->is_active &= ~event_type; | |
3323 | if (!(ctx->is_active & EVENT_ALL)) | |
3324 | ctx->is_active = 0; | |
3325 | ||
3326 | if (ctx->task) { | |
3327 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3328 | if (!ctx->is_active) | |
3329 | cpuctx->task_ctx = NULL; | |
3cbaa590 PZ |
3330 | } |
3331 | ||
8fdc6539 PZ |
3332 | is_active ^= ctx->is_active; /* changed bits */ |
3333 | ||
f06cc667 PZ |
3334 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
3335 | if (cgroup && !pmu_ctx->nr_cgroups) | |
3336 | continue; | |
bd275681 | 3337 | __pmu_ctx_sched_out(pmu_ctx, is_active); |
f06cc667 | 3338 | } |
235c7fc7 IM |
3339 | } |
3340 | ||
564c2b21 | 3341 | /* |
5a3126d4 PZ |
3342 | * Test whether two contexts are equivalent, i.e. whether they have both been |
3343 | * cloned from the same version of the same context. | |
3344 | * | |
3345 | * Equivalence is measured using a generation number in the context that is | |
3346 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
3347 | * and list_del_event(). | |
564c2b21 | 3348 | */ |
cdd6c482 IM |
3349 | static int context_equiv(struct perf_event_context *ctx1, |
3350 | struct perf_event_context *ctx2) | |
564c2b21 | 3351 | { |
211de6eb PZ |
3352 | lockdep_assert_held(&ctx1->lock); |
3353 | lockdep_assert_held(&ctx2->lock); | |
3354 | ||
5a3126d4 PZ |
3355 | /* Pinning disables the swap optimization */ |
3356 | if (ctx1->pin_count || ctx2->pin_count) | |
3357 | return 0; | |
3358 | ||
3359 | /* If ctx1 is the parent of ctx2 */ | |
3360 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
3361 | return 1; | |
3362 | ||
3363 | /* If ctx2 is the parent of ctx1 */ | |
3364 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
3365 | return 1; | |
3366 | ||
3367 | /* | |
3368 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
3369 | * hierarchy, see perf_event_init_context(). | |
3370 | */ | |
3371 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
3372 | ctx1->parent_gen == ctx2->parent_gen) | |
3373 | return 1; | |
3374 | ||
3375 | /* Unmatched */ | |
3376 | return 0; | |
564c2b21 PM |
3377 | } |
3378 | ||
cdd6c482 IM |
3379 | static void __perf_event_sync_stat(struct perf_event *event, |
3380 | struct perf_event *next_event) | |
bfbd3381 PZ |
3381 | { |
3382 | u64 value; | |
3383 | ||
cdd6c482 | 3384 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
3385 | return; |
3386 | ||
3387 | /* | |
cdd6c482 | 3388 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
3389 | * because we're in the middle of a context switch and have IRQs |
3390 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 3391 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
3392 | * don't need to use it. |
3393 | */ | |
0d3d73aa | 3394 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
3dbebf15 | 3395 | event->pmu->read(event); |
bfbd3381 | 3396 | |
0d3d73aa | 3397 | perf_event_update_time(event); |
bfbd3381 PZ |
3398 | |
3399 | /* | |
cdd6c482 | 3400 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
3401 | * values when we flip the contexts. |
3402 | */ | |
e7850595 PZ |
3403 | value = local64_read(&next_event->count); |
3404 | value = local64_xchg(&event->count, value); | |
3405 | local64_set(&next_event->count, value); | |
bfbd3381 | 3406 | |
cdd6c482 IM |
3407 | swap(event->total_time_enabled, next_event->total_time_enabled); |
3408 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 3409 | |
bfbd3381 | 3410 | /* |
19d2e755 | 3411 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 3412 | */ |
cdd6c482 IM |
3413 | perf_event_update_userpage(event); |
3414 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
3415 | } |
3416 | ||
cdd6c482 IM |
3417 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
3418 | struct perf_event_context *next_ctx) | |
bfbd3381 | 3419 | { |
cdd6c482 | 3420 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
3421 | |
3422 | if (!ctx->nr_stat) | |
3423 | return; | |
3424 | ||
02ffdbc8 PZ |
3425 | update_context_time(ctx); |
3426 | ||
cdd6c482 IM |
3427 | event = list_first_entry(&ctx->event_list, |
3428 | struct perf_event, event_entry); | |
bfbd3381 | 3429 | |
cdd6c482 IM |
3430 | next_event = list_first_entry(&next_ctx->event_list, |
3431 | struct perf_event, event_entry); | |
bfbd3381 | 3432 | |
cdd6c482 IM |
3433 | while (&event->event_entry != &ctx->event_list && |
3434 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 3435 | |
cdd6c482 | 3436 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 3437 | |
cdd6c482 IM |
3438 | event = list_next_entry(event, event_entry); |
3439 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
3440 | } |
3441 | } | |
3442 | ||
bd275681 PZ |
3443 | #define double_list_for_each_entry(pos1, pos2, head1, head2, member) \ |
3444 | for (pos1 = list_first_entry(head1, typeof(*pos1), member), \ | |
3445 | pos2 = list_first_entry(head2, typeof(*pos2), member); \ | |
3446 | !list_entry_is_head(pos1, head1, member) && \ | |
3447 | !list_entry_is_head(pos2, head2, member); \ | |
3448 | pos1 = list_next_entry(pos1, member), \ | |
3449 | pos2 = list_next_entry(pos2, member)) | |
3450 | ||
3451 | static void perf_event_swap_task_ctx_data(struct perf_event_context *prev_ctx, | |
3452 | struct perf_event_context *next_ctx) | |
3453 | { | |
3454 | struct perf_event_pmu_context *prev_epc, *next_epc; | |
3455 | ||
3456 | if (!prev_ctx->nr_task_data) | |
3457 | return; | |
3458 | ||
3459 | double_list_for_each_entry(prev_epc, next_epc, | |
3460 | &prev_ctx->pmu_ctx_list, &next_ctx->pmu_ctx_list, | |
3461 | pmu_ctx_entry) { | |
3462 | ||
3463 | if (WARN_ON_ONCE(prev_epc->pmu != next_epc->pmu)) | |
3464 | continue; | |
3465 | ||
3466 | /* | |
3467 | * PMU specific parts of task perf context can require | |
3468 | * additional synchronization. As an example of such | |
3469 | * synchronization see implementation details of Intel | |
3470 | * LBR call stack data profiling; | |
3471 | */ | |
3472 | if (prev_epc->pmu->swap_task_ctx) | |
3473 | prev_epc->pmu->swap_task_ctx(prev_epc, next_epc); | |
3474 | else | |
3475 | swap(prev_epc->task_ctx_data, next_epc->task_ctx_data); | |
3476 | } | |
3477 | } | |
3478 | ||
3479 | static void perf_ctx_sched_task_cb(struct perf_event_context *ctx, bool sched_in) | |
3480 | { | |
3481 | struct perf_event_pmu_context *pmu_ctx; | |
3482 | struct perf_cpu_pmu_context *cpc; | |
3483 | ||
3484 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { | |
3485 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
3486 | ||
3487 | if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) | |
3488 | pmu_ctx->pmu->sched_task(pmu_ctx, sched_in); | |
3489 | } | |
3490 | } | |
3491 | ||
3492 | static void | |
3493 | perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) | |
0793a61d | 3494 | { |
bd275681 | 3495 | struct perf_event_context *ctx = task->perf_event_ctxp; |
cdd6c482 | 3496 | struct perf_event_context *next_ctx; |
5a3126d4 | 3497 | struct perf_event_context *parent, *next_parent; |
c93f7669 | 3498 | int do_switch = 1; |
0793a61d | 3499 | |
108b02cf PZ |
3500 | if (likely(!ctx)) |
3501 | return; | |
10989fb2 | 3502 | |
c93f7669 | 3503 | rcu_read_lock(); |
bd275681 | 3504 | next_ctx = rcu_dereference(next->perf_event_ctxp); |
5a3126d4 PZ |
3505 | if (!next_ctx) |
3506 | goto unlock; | |
3507 | ||
3508 | parent = rcu_dereference(ctx->parent_ctx); | |
3509 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
3510 | ||
3511 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 3512 | if (!parent && !next_parent) |
5a3126d4 PZ |
3513 | goto unlock; |
3514 | ||
3515 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
3516 | /* |
3517 | * Looks like the two contexts are clones, so we might be | |
3518 | * able to optimize the context switch. We lock both | |
3519 | * contexts and check that they are clones under the | |
3520 | * lock (including re-checking that neither has been | |
3521 | * uncloned in the meantime). It doesn't matter which | |
3522 | * order we take the locks because no other cpu could | |
3523 | * be trying to lock both of these tasks. | |
3524 | */ | |
e625cce1 TG |
3525 | raw_spin_lock(&ctx->lock); |
3526 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 3527 | if (context_equiv(ctx, next_ctx)) { |
c2b98a86 | 3528 | |
f06cc667 | 3529 | perf_ctx_disable(ctx, false); |
ca6c2132 PZ |
3530 | |
3531 | /* PMIs are disabled; ctx->nr_pending is stable. */ | |
3532 | if (local_read(&ctx->nr_pending) || | |
3533 | local_read(&next_ctx->nr_pending)) { | |
3534 | /* | |
3535 | * Must not swap out ctx when there's pending | |
3536 | * events that rely on the ctx->task relation. | |
3537 | */ | |
3538 | raw_spin_unlock(&next_ctx->lock); | |
3539 | rcu_read_unlock(); | |
3540 | goto inside_switch; | |
3541 | } | |
3542 | ||
63b6da39 PZ |
3543 | WRITE_ONCE(ctx->task, next); |
3544 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c | 3545 | |
bd275681 PZ |
3546 | perf_ctx_sched_task_cb(ctx, false); |
3547 | perf_event_swap_task_ctx_data(ctx, next_ctx); | |
5a158c3c | 3548 | |
f06cc667 | 3549 | perf_ctx_enable(ctx, false); |
44fae179 | 3550 | |
63b6da39 PZ |
3551 | /* |
3552 | * RCU_INIT_POINTER here is safe because we've not | |
3553 | * modified the ctx and the above modification of | |
3554 | * ctx->task and ctx->task_ctx_data are immaterial | |
3555 | * since those values are always verified under | |
3556 | * ctx->lock which we're now holding. | |
3557 | */ | |
bd275681 PZ |
3558 | RCU_INIT_POINTER(task->perf_event_ctxp, next_ctx); |
3559 | RCU_INIT_POINTER(next->perf_event_ctxp, ctx); | |
63b6da39 | 3560 | |
c93f7669 | 3561 | do_switch = 0; |
bfbd3381 | 3562 | |
cdd6c482 | 3563 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 3564 | } |
e625cce1 TG |
3565 | raw_spin_unlock(&next_ctx->lock); |
3566 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 3567 | } |
5a3126d4 | 3568 | unlock: |
c93f7669 | 3569 | rcu_read_unlock(); |
564c2b21 | 3570 | |
c93f7669 | 3571 | if (do_switch) { |
facc4307 | 3572 | raw_spin_lock(&ctx->lock); |
f06cc667 | 3573 | perf_ctx_disable(ctx, false); |
44fae179 | 3574 | |
ca6c2132 | 3575 | inside_switch: |
bd275681 PZ |
3576 | perf_ctx_sched_task_cb(ctx, false); |
3577 | task_ctx_sched_out(ctx, EVENT_ALL); | |
44fae179 | 3578 | |
f06cc667 | 3579 | perf_ctx_enable(ctx, false); |
facc4307 | 3580 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 3581 | } |
0793a61d TG |
3582 | } |
3583 | ||
a5398bff | 3584 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
bd275681 | 3585 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
a5398bff | 3586 | |
ba532500 YZ |
3587 | void perf_sched_cb_dec(struct pmu *pmu) |
3588 | { | |
bd275681 | 3589 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); |
e48c1788 | 3590 | |
a5398bff | 3591 | this_cpu_dec(perf_sched_cb_usages); |
bd275681 | 3592 | barrier(); |
a5398bff | 3593 | |
bd275681 PZ |
3594 | if (!--cpc->sched_cb_usage) |
3595 | list_del(&cpc->sched_cb_entry); | |
ba532500 YZ |
3596 | } |
3597 | ||
e48c1788 | 3598 | |
ba532500 YZ |
3599 | void perf_sched_cb_inc(struct pmu *pmu) |
3600 | { | |
bd275681 | 3601 | struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); |
e48c1788 | 3602 | |
bd275681 PZ |
3603 | if (!cpc->sched_cb_usage++) |
3604 | list_add(&cpc->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
a5398bff | 3605 | |
bd275681 | 3606 | barrier(); |
a5398bff | 3607 | this_cpu_inc(perf_sched_cb_usages); |
ba532500 YZ |
3608 | } |
3609 | ||
3610 | /* | |
3611 | * This function provides the context switch callback to the lower code | |
3612 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
3613 | * |
3614 | * This callback is relevant even to per-cpu events; for example multi event | |
3615 | * PEBS requires this to provide PID/TID information. This requires we flush | |
3616 | * all queued PEBS records before we context switch to a new task. | |
ba532500 | 3617 | */ |
bd275681 | 3618 | static void __perf_pmu_sched_task(struct perf_cpu_pmu_context *cpc, bool sched_in) |
556cccad | 3619 | { |
bd275681 | 3620 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
556cccad KL |
3621 | struct pmu *pmu; |
3622 | ||
bd275681 | 3623 | pmu = cpc->epc.pmu; |
556cccad | 3624 | |
bd275681 | 3625 | /* software PMUs will not have sched_task */ |
556cccad KL |
3626 | if (WARN_ON_ONCE(!pmu->sched_task)) |
3627 | return; | |
3628 | ||
3629 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); | |
3630 | perf_pmu_disable(pmu); | |
3631 | ||
bd275681 | 3632 | pmu->sched_task(cpc->task_epc, sched_in); |
556cccad KL |
3633 | |
3634 | perf_pmu_enable(pmu); | |
3635 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
3636 | } | |
3637 | ||
a5398bff KL |
3638 | static void perf_pmu_sched_task(struct task_struct *prev, |
3639 | struct task_struct *next, | |
3640 | bool sched_in) | |
3641 | { | |
bd275681 PZ |
3642 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
3643 | struct perf_cpu_pmu_context *cpc; | |
a5398bff | 3644 | |
bd275681 PZ |
3645 | /* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ |
3646 | if (prev == next || cpuctx->task_ctx) | |
a5398bff KL |
3647 | return; |
3648 | ||
bd275681 PZ |
3649 | list_for_each_entry(cpc, this_cpu_ptr(&sched_cb_list), sched_cb_entry) |
3650 | __perf_pmu_sched_task(cpc, sched_in); | |
a5398bff KL |
3651 | } |
3652 | ||
45ac1403 AH |
3653 | static void perf_event_switch(struct task_struct *task, |
3654 | struct task_struct *next_prev, bool sched_in); | |
3655 | ||
8dc85d54 PZ |
3656 | /* |
3657 | * Called from scheduler to remove the events of the current task, | |
3658 | * with interrupts disabled. | |
3659 | * | |
3660 | * We stop each event and update the event value in event->count. | |
3661 | * | |
3662 | * This does not protect us against NMI, but disable() | |
3663 | * sets the disabled bit in the control field of event _before_ | |
3664 | * accessing the event control register. If a NMI hits, then it will | |
3665 | * not restart the event. | |
3666 | */ | |
ab0cce56 JO |
3667 | void __perf_event_task_sched_out(struct task_struct *task, |
3668 | struct task_struct *next) | |
8dc85d54 | 3669 | { |
a5398bff KL |
3670 | if (__this_cpu_read(perf_sched_cb_usages)) |
3671 | perf_pmu_sched_task(task, next, false); | |
3672 | ||
45ac1403 AH |
3673 | if (atomic_read(&nr_switch_events)) |
3674 | perf_event_switch(task, next, false); | |
3675 | ||
bd275681 | 3676 | perf_event_context_sched_out(task, next); |
e5d1367f SE |
3677 | |
3678 | /* | |
3679 | * if cgroup events exist on this CPU, then we need | |
3680 | * to check if we have to switch out PMU state. | |
3681 | * cgroup event are system-wide mode only | |
3682 | */ | |
f841b682 | 3683 | perf_cgroup_switch(next); |
8dc85d54 PZ |
3684 | } |
3685 | ||
6eef8a71 | 3686 | static bool perf_less_group_idx(const void *l, const void *r) |
0793a61d | 3687 | { |
24fb6b8e IR |
3688 | const struct perf_event *le = *(const struct perf_event **)l; |
3689 | const struct perf_event *re = *(const struct perf_event **)r; | |
6eef8a71 IR |
3690 | |
3691 | return le->group_index < re->group_index; | |
3692 | } | |
3693 | ||
3694 | static void swap_ptr(void *l, void *r) | |
3695 | { | |
3696 | void **lp = l, **rp = r; | |
3697 | ||
3698 | swap(*lp, *rp); | |
3699 | } | |
3700 | ||
3701 | static const struct min_heap_callbacks perf_min_heap = { | |
3702 | .elem_size = sizeof(struct perf_event *), | |
3703 | .less = perf_less_group_idx, | |
3704 | .swp = swap_ptr, | |
3705 | }; | |
3706 | ||
3707 | static void __heap_add(struct min_heap *heap, struct perf_event *event) | |
3708 | { | |
3709 | struct perf_event **itrs = heap->data; | |
3710 | ||
3711 | if (event) { | |
3712 | itrs[heap->nr] = event; | |
3713 | heap->nr++; | |
3714 | } | |
3715 | } | |
3716 | ||
bd275681 PZ |
3717 | static void __link_epc(struct perf_event_pmu_context *pmu_ctx) |
3718 | { | |
3719 | struct perf_cpu_pmu_context *cpc; | |
3720 | ||
3721 | if (!pmu_ctx->ctx->task) | |
3722 | return; | |
3723 | ||
3724 | cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); | |
3725 | WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); | |
3726 | cpc->task_epc = pmu_ctx; | |
3727 | } | |
3728 | ||
3729 | static noinline int visit_groups_merge(struct perf_event_context *ctx, | |
836196be | 3730 | struct perf_event_groups *groups, int cpu, |
bd275681 | 3731 | struct pmu *pmu, |
6eef8a71 IR |
3732 | int (*func)(struct perf_event *, void *), |
3733 | void *data) | |
3734 | { | |
95ed6c70 IR |
3735 | #ifdef CONFIG_CGROUP_PERF |
3736 | struct cgroup_subsys_state *css = NULL; | |
3737 | #endif | |
bd275681 | 3738 | struct perf_cpu_context *cpuctx = NULL; |
6eef8a71 IR |
3739 | /* Space for per CPU and/or any CPU event iterators. */ |
3740 | struct perf_event *itrs[2]; | |
836196be IR |
3741 | struct min_heap event_heap; |
3742 | struct perf_event **evt; | |
1cac7b1a | 3743 | int ret; |
8e1a2031 | 3744 | |
bd275681 PZ |
3745 | if (pmu->filter && pmu->filter(pmu, cpu)) |
3746 | return 0; | |
3747 | ||
3748 | if (!ctx->task) { | |
3749 | cpuctx = this_cpu_ptr(&perf_cpu_context); | |
836196be IR |
3750 | event_heap = (struct min_heap){ |
3751 | .data = cpuctx->heap, | |
3752 | .nr = 0, | |
3753 | .size = cpuctx->heap_size, | |
3754 | }; | |
c2283c93 IR |
3755 | |
3756 | lockdep_assert_held(&cpuctx->ctx.lock); | |
95ed6c70 IR |
3757 | |
3758 | #ifdef CONFIG_CGROUP_PERF | |
3759 | if (cpuctx->cgrp) | |
3760 | css = &cpuctx->cgrp->css; | |
3761 | #endif | |
836196be IR |
3762 | } else { |
3763 | event_heap = (struct min_heap){ | |
3764 | .data = itrs, | |
3765 | .nr = 0, | |
3766 | .size = ARRAY_SIZE(itrs), | |
3767 | }; | |
3768 | /* Events not within a CPU context may be on any CPU. */ | |
bd275681 | 3769 | __heap_add(&event_heap, perf_event_groups_first(groups, -1, pmu, NULL)); |
836196be IR |
3770 | } |
3771 | evt = event_heap.data; | |
3772 | ||
bd275681 | 3773 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, NULL)); |
95ed6c70 IR |
3774 | |
3775 | #ifdef CONFIG_CGROUP_PERF | |
3776 | for (; css; css = css->parent) | |
bd275681 | 3777 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, css->cgroup)); |
95ed6c70 | 3778 | #endif |
1cac7b1a | 3779 | |
bd275681 PZ |
3780 | if (event_heap.nr) { |
3781 | __link_epc((*evt)->pmu_ctx); | |
3782 | perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); | |
3783 | } | |
3784 | ||
6eef8a71 | 3785 | min_heapify_all(&event_heap, &perf_min_heap); |
1cac7b1a | 3786 | |
6eef8a71 | 3787 | while (event_heap.nr) { |
1cac7b1a PZ |
3788 | ret = func(*evt, data); |
3789 | if (ret) | |
3790 | return ret; | |
3791 | ||
bd275681 | 3792 | *evt = perf_event_groups_next(*evt, pmu); |
6eef8a71 IR |
3793 | if (*evt) |
3794 | min_heapify(&event_heap, 0, &perf_min_heap); | |
3795 | else | |
3796 | min_heap_pop(&event_heap, &perf_min_heap); | |
8e1a2031 | 3797 | } |
0793a61d | 3798 | |
1cac7b1a PZ |
3799 | return 0; |
3800 | } | |
3801 | ||
09f5e7dc PZ |
3802 | /* |
3803 | * Because the userpage is strictly per-event (there is no concept of context, | |
3804 | * so there cannot be a context indirection), every userpage must be updated | |
3805 | * when context time starts :-( | |
3806 | * | |
3807 | * IOW, we must not miss EVENT_TIME edges. | |
3808 | */ | |
f7925653 SL |
3809 | static inline bool event_update_userpage(struct perf_event *event) |
3810 | { | |
3811 | if (likely(!atomic_read(&event->mmap_count))) | |
3812 | return false; | |
3813 | ||
3814 | perf_event_update_time(event); | |
f7925653 SL |
3815 | perf_event_update_userpage(event); |
3816 | ||
3817 | return true; | |
3818 | } | |
3819 | ||
3820 | static inline void group_update_userpage(struct perf_event *group_event) | |
3821 | { | |
3822 | struct perf_event *event; | |
3823 | ||
3824 | if (!event_update_userpage(group_event)) | |
3825 | return; | |
3826 | ||
3827 | for_each_sibling_event(event, group_event) | |
3828 | event_update_userpage(event); | |
3829 | } | |
3830 | ||
ab6f824c | 3831 | static int merge_sched_in(struct perf_event *event, void *data) |
1cac7b1a | 3832 | { |
2c2366c7 | 3833 | struct perf_event_context *ctx = event->ctx; |
2c2366c7 | 3834 | int *can_add_hw = data; |
ab6f824c | 3835 | |
1cac7b1a PZ |
3836 | if (event->state <= PERF_EVENT_STATE_OFF) |
3837 | return 0; | |
3838 | ||
3839 | if (!event_filter_match(event)) | |
3840 | return 0; | |
3841 | ||
bd275681 PZ |
3842 | if (group_can_go_on(event, *can_add_hw)) { |
3843 | if (!group_sched_in(event, ctx)) | |
ab6f824c | 3844 | list_add_tail(&event->active_list, get_event_list(event)); |
6668128a | 3845 | } |
1cac7b1a | 3846 | |
ab6f824c | 3847 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
f7925653 | 3848 | *can_add_hw = 0; |
33238c50 PZ |
3849 | if (event->attr.pinned) { |
3850 | perf_cgroup_event_disable(event, ctx); | |
ab6f824c | 3851 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); |
f7925653 | 3852 | } else { |
bd275681 PZ |
3853 | struct perf_cpu_pmu_context *cpc; |
3854 | ||
3855 | event->pmu_ctx->rotate_necessary = 1; | |
3856 | cpc = this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context); | |
3857 | perf_mux_hrtimer_restart(cpc); | |
f7925653 | 3858 | group_update_userpage(event); |
33238c50 | 3859 | } |
3b6f9e5c | 3860 | } |
1cac7b1a PZ |
3861 | |
3862 | return 0; | |
5b0311e1 FW |
3863 | } |
3864 | ||
f06cc667 PZ |
3865 | static void pmu_groups_sched_in(struct perf_event_context *ctx, |
3866 | struct perf_event_groups *groups, | |
3867 | struct pmu *pmu) | |
5b0311e1 | 3868 | { |
2c2366c7 | 3869 | int can_add_hw = 1; |
f06cc667 PZ |
3870 | visit_groups_merge(ctx, groups, smp_processor_id(), pmu, |
3871 | merge_sched_in, &can_add_hw); | |
1cac7b1a | 3872 | } |
8e1a2031 | 3873 | |
f06cc667 PZ |
3874 | static void ctx_groups_sched_in(struct perf_event_context *ctx, |
3875 | struct perf_event_groups *groups, | |
3876 | bool cgroup) | |
1cac7b1a | 3877 | { |
bd275681 | 3878 | struct perf_event_pmu_context *pmu_ctx; |
0793a61d | 3879 | |
f06cc667 PZ |
3880 | list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { |
3881 | if (cgroup && !pmu_ctx->nr_cgroups) | |
3882 | continue; | |
3883 | pmu_groups_sched_in(ctx, groups, pmu_ctx->pmu); | |
bd275681 PZ |
3884 | } |
3885 | } | |
836196be | 3886 | |
f06cc667 PZ |
3887 | static void __pmu_ctx_sched_in(struct perf_event_context *ctx, |
3888 | struct pmu *pmu) | |
bd275681 | 3889 | { |
f06cc667 | 3890 | pmu_groups_sched_in(ctx, &ctx->flexible_groups, pmu); |
5b0311e1 FW |
3891 | } |
3892 | ||
3893 | static void | |
bd275681 | 3894 | ctx_sched_in(struct perf_event_context *ctx, enum event_type_t event_type) |
5b0311e1 | 3895 | { |
bd275681 | 3896 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
db24d33e | 3897 | int is_active = ctx->is_active; |
f06cc667 PZ |
3898 | bool cgroup = event_type & EVENT_CGROUP; |
3899 | ||
3900 | event_type &= ~EVENT_CGROUP; | |
c994d613 PZ |
3901 | |
3902 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3903 | |
5b0311e1 | 3904 | if (likely(!ctx->nr_events)) |
facc4307 | 3905 | return; |
5b0311e1 | 3906 | |
baf1b12a | 3907 | if (!(is_active & EVENT_TIME)) { |
09f5e7dc PZ |
3908 | /* start ctx time */ |
3909 | __update_context_time(ctx, false); | |
a0827713 | 3910 | perf_cgroup_set_timestamp(cpuctx); |
09f5e7dc PZ |
3911 | /* |
3912 | * CPU-release for the below ->is_active store, | |
3913 | * see __load_acquire() in perf_event_time_now() | |
3914 | */ | |
3915 | barrier(); | |
3916 | } | |
3917 | ||
3cbaa590 | 3918 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3919 | if (ctx->task) { |
3920 | if (!is_active) | |
3921 | cpuctx->task_ctx = ctx; | |
3922 | else | |
3923 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3924 | } | |
3925 | ||
3cbaa590 PZ |
3926 | is_active ^= ctx->is_active; /* changed bits */ |
3927 | ||
5b0311e1 FW |
3928 | /* |
3929 | * First go through the list and put on any pinned groups | |
3930 | * in order to give them the best chance of going on. | |
3931 | */ | |
3cbaa590 | 3932 | if (is_active & EVENT_PINNED) |
f06cc667 | 3933 | ctx_groups_sched_in(ctx, &ctx->pinned_groups, cgroup); |
5b0311e1 FW |
3934 | |
3935 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3936 | if (is_active & EVENT_FLEXIBLE) |
f06cc667 | 3937 | ctx_groups_sched_in(ctx, &ctx->flexible_groups, cgroup); |
235c7fc7 IM |
3938 | } |
3939 | ||
bd275681 | 3940 | static void perf_event_context_sched_in(struct task_struct *task) |
329c0e01 | 3941 | { |
bd275681 PZ |
3942 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
3943 | struct perf_event_context *ctx; | |
329c0e01 | 3944 | |
bd275681 PZ |
3945 | rcu_read_lock(); |
3946 | ctx = rcu_dereference(task->perf_event_ctxp); | |
3947 | if (!ctx) | |
3948 | goto rcu_unlock; | |
235c7fc7 | 3949 | |
bd275681 PZ |
3950 | if (cpuctx->task_ctx == ctx) { |
3951 | perf_ctx_lock(cpuctx, ctx); | |
f06cc667 | 3952 | perf_ctx_disable(ctx, false); |
012669c7 | 3953 | |
bd275681 | 3954 | perf_ctx_sched_task_cb(ctx, true); |
012669c7 | 3955 | |
f06cc667 | 3956 | perf_ctx_enable(ctx, false); |
bd275681 PZ |
3957 | perf_ctx_unlock(cpuctx, ctx); |
3958 | goto rcu_unlock; | |
556cccad | 3959 | } |
329c0e01 | 3960 | |
facc4307 | 3961 | perf_ctx_lock(cpuctx, ctx); |
fdccc3fb | 3962 | /* |
3963 | * We must check ctx->nr_events while holding ctx->lock, such | |
3964 | * that we serialize against perf_install_in_context(). | |
3965 | */ | |
3966 | if (!ctx->nr_events) | |
3967 | goto unlock; | |
3968 | ||
f06cc667 | 3969 | perf_ctx_disable(ctx, false); |
329c0e01 FW |
3970 | /* |
3971 | * We want to keep the following priority order: | |
3972 | * cpu pinned (that don't need to move), task pinned, | |
3973 | * cpu flexible, task flexible. | |
fe45bafb AS |
3974 | * |
3975 | * However, if task's ctx is not carrying any pinned | |
3976 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3977 | */ |
bd275681 | 3978 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) { |
f06cc667 | 3979 | perf_ctx_disable(&cpuctx->ctx, false); |
bd275681 PZ |
3980 | ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); |
3981 | } | |
3982 | ||
a0827713 | 3983 | perf_event_sched_in(cpuctx, ctx); |
556cccad | 3984 | |
bd275681 | 3985 | perf_ctx_sched_task_cb(cpuctx->task_ctx, true); |
556cccad | 3986 | |
bd275681 | 3987 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) |
f06cc667 | 3988 | perf_ctx_enable(&cpuctx->ctx, false); |
bd275681 | 3989 | |
f06cc667 | 3990 | perf_ctx_enable(ctx, false); |
fdccc3fb | 3991 | |
3992 | unlock: | |
facc4307 | 3993 | perf_ctx_unlock(cpuctx, ctx); |
bd275681 PZ |
3994 | rcu_unlock: |
3995 | rcu_read_unlock(); | |
235c7fc7 IM |
3996 | } |
3997 | ||
8dc85d54 PZ |
3998 | /* |
3999 | * Called from scheduler to add the events of the current task | |
4000 | * with interrupts disabled. | |
4001 | * | |
4002 | * We restore the event value and then enable it. | |
4003 | * | |
4004 | * This does not protect us against NMI, but enable() | |
4005 | * sets the enabled bit in the control field of event _before_ | |
4006 | * accessing the event control register. If a NMI hits, then it will | |
4007 | * keep the event running. | |
4008 | */ | |
ab0cce56 JO |
4009 | void __perf_event_task_sched_in(struct task_struct *prev, |
4010 | struct task_struct *task) | |
8dc85d54 | 4011 | { |
bd275681 | 4012 | perf_event_context_sched_in(task); |
d010b332 | 4013 | |
45ac1403 AH |
4014 | if (atomic_read(&nr_switch_events)) |
4015 | perf_event_switch(task, prev, true); | |
a5398bff KL |
4016 | |
4017 | if (__this_cpu_read(perf_sched_cb_usages)) | |
4018 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
4019 | } |
4020 | ||
abd50713 PZ |
4021 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
4022 | { | |
4023 | u64 frequency = event->attr.sample_freq; | |
4024 | u64 sec = NSEC_PER_SEC; | |
4025 | u64 divisor, dividend; | |
4026 | ||
4027 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
4028 | ||
4029 | count_fls = fls64(count); | |
4030 | nsec_fls = fls64(nsec); | |
4031 | frequency_fls = fls64(frequency); | |
4032 | sec_fls = 30; | |
4033 | ||
4034 | /* | |
4035 | * We got @count in @nsec, with a target of sample_freq HZ | |
4036 | * the target period becomes: | |
4037 | * | |
4038 | * @count * 10^9 | |
4039 | * period = ------------------- | |
4040 | * @nsec * sample_freq | |
4041 | * | |
4042 | */ | |
4043 | ||
4044 | /* | |
4045 | * Reduce accuracy by one bit such that @a and @b converge | |
4046 | * to a similar magnitude. | |
4047 | */ | |
fe4b04fa | 4048 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
4049 | do { \ |
4050 | if (a##_fls > b##_fls) { \ | |
4051 | a >>= 1; \ | |
4052 | a##_fls--; \ | |
4053 | } else { \ | |
4054 | b >>= 1; \ | |
4055 | b##_fls--; \ | |
4056 | } \ | |
4057 | } while (0) | |
4058 | ||
4059 | /* | |
4060 | * Reduce accuracy until either term fits in a u64, then proceed with | |
4061 | * the other, so that finally we can do a u64/u64 division. | |
4062 | */ | |
4063 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
4064 | REDUCE_FLS(nsec, frequency); | |
4065 | REDUCE_FLS(sec, count); | |
4066 | } | |
4067 | ||
4068 | if (count_fls + sec_fls > 64) { | |
4069 | divisor = nsec * frequency; | |
4070 | ||
4071 | while (count_fls + sec_fls > 64) { | |
4072 | REDUCE_FLS(count, sec); | |
4073 | divisor >>= 1; | |
4074 | } | |
4075 | ||
4076 | dividend = count * sec; | |
4077 | } else { | |
4078 | dividend = count * sec; | |
4079 | ||
4080 | while (nsec_fls + frequency_fls > 64) { | |
4081 | REDUCE_FLS(nsec, frequency); | |
4082 | dividend >>= 1; | |
4083 | } | |
4084 | ||
4085 | divisor = nsec * frequency; | |
4086 | } | |
4087 | ||
f6ab91ad PZ |
4088 | if (!divisor) |
4089 | return dividend; | |
4090 | ||
abd50713 PZ |
4091 | return div64_u64(dividend, divisor); |
4092 | } | |
4093 | ||
e050e3f0 SE |
4094 | static DEFINE_PER_CPU(int, perf_throttled_count); |
4095 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
4096 | ||
f39d47ff | 4097 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 4098 | { |
cdd6c482 | 4099 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 4100 | s64 period, sample_period; |
bd2b5b12 PZ |
4101 | s64 delta; |
4102 | ||
abd50713 | 4103 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
4104 | |
4105 | delta = (s64)(period - hwc->sample_period); | |
4106 | delta = (delta + 7) / 8; /* low pass filter */ | |
4107 | ||
4108 | sample_period = hwc->sample_period + delta; | |
4109 | ||
4110 | if (!sample_period) | |
4111 | sample_period = 1; | |
4112 | ||
bd2b5b12 | 4113 | hwc->sample_period = sample_period; |
abd50713 | 4114 | |
e7850595 | 4115 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
4116 | if (disable) |
4117 | event->pmu->stop(event, PERF_EF_UPDATE); | |
4118 | ||
e7850595 | 4119 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
4120 | |
4121 | if (disable) | |
4122 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 4123 | } |
bd2b5b12 PZ |
4124 | } |
4125 | ||
e050e3f0 SE |
4126 | /* |
4127 | * combine freq adjustment with unthrottling to avoid two passes over the | |
4128 | * events. At the same time, make sure, having freq events does not change | |
4129 | * the rate of unthrottling as that would introduce bias. | |
4130 | */ | |
bd275681 PZ |
4131 | static void |
4132 | perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) | |
60db5e09 | 4133 | { |
cdd6c482 IM |
4134 | struct perf_event *event; |
4135 | struct hw_perf_event *hwc; | |
e050e3f0 | 4136 | u64 now, period = TICK_NSEC; |
abd50713 | 4137 | s64 delta; |
60db5e09 | 4138 | |
e050e3f0 SE |
4139 | /* |
4140 | * only need to iterate over all events iff: | |
4141 | * - context have events in frequency mode (needs freq adjust) | |
4142 | * - there are events to unthrottle on this cpu | |
4143 | */ | |
bd275681 | 4144 | if (!(ctx->nr_freq || unthrottle)) |
0f5a2601 PZ |
4145 | return; |
4146 | ||
e050e3f0 SE |
4147 | raw_spin_lock(&ctx->lock); |
4148 | ||
03541f8b | 4149 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 4150 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
4151 | continue; |
4152 | ||
bd275681 | 4153 | // XXX use visit thingy to avoid the -1,cpu match |
5632ab12 | 4154 | if (!event_filter_match(event)) |
5d27c23d PZ |
4155 | continue; |
4156 | ||
44377277 AS |
4157 | perf_pmu_disable(event->pmu); |
4158 | ||
cdd6c482 | 4159 | hwc = &event->hw; |
6a24ed6c | 4160 | |
ae23bff1 | 4161 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 4162 | hwc->interrupts = 0; |
cdd6c482 | 4163 | perf_log_throttle(event, 1); |
a4eaf7f1 | 4164 | event->pmu->start(event, 0); |
a78ac325 PZ |
4165 | } |
4166 | ||
cdd6c482 | 4167 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 4168 | goto next; |
60db5e09 | 4169 | |
e050e3f0 SE |
4170 | /* |
4171 | * stop the event and update event->count | |
4172 | */ | |
4173 | event->pmu->stop(event, PERF_EF_UPDATE); | |
4174 | ||
e7850595 | 4175 | now = local64_read(&event->count); |
abd50713 PZ |
4176 | delta = now - hwc->freq_count_stamp; |
4177 | hwc->freq_count_stamp = now; | |
60db5e09 | 4178 | |
e050e3f0 SE |
4179 | /* |
4180 | * restart the event | |
4181 | * reload only if value has changed | |
f39d47ff SE |
4182 | * we have stopped the event so tell that |
4183 | * to perf_adjust_period() to avoid stopping it | |
4184 | * twice. | |
e050e3f0 | 4185 | */ |
abd50713 | 4186 | if (delta > 0) |
f39d47ff | 4187 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
4188 | |
4189 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
4190 | next: |
4191 | perf_pmu_enable(event->pmu); | |
60db5e09 | 4192 | } |
e050e3f0 SE |
4193 | |
4194 | raw_spin_unlock(&ctx->lock); | |
60db5e09 PZ |
4195 | } |
4196 | ||
235c7fc7 | 4197 | /* |
8703a7cf | 4198 | * Move @event to the tail of the @ctx's elegible events. |
235c7fc7 | 4199 | */ |
8703a7cf | 4200 | static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event) |
0793a61d | 4201 | { |
dddd3379 TG |
4202 | /* |
4203 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
4204 | * disabled by the inheritance code. | |
4205 | */ | |
8703a7cf PZ |
4206 | if (ctx->rotate_disable) |
4207 | return; | |
8e1a2031 | 4208 | |
8703a7cf PZ |
4209 | perf_event_groups_delete(&ctx->flexible_groups, event); |
4210 | perf_event_groups_insert(&ctx->flexible_groups, event); | |
235c7fc7 IM |
4211 | } |
4212 | ||
7fa343b7 | 4213 | /* pick an event from the flexible_groups to rotate */ |
8d5bce0c | 4214 | static inline struct perf_event * |
bd275681 | 4215 | ctx_event_to_rotate(struct perf_event_pmu_context *pmu_ctx) |
235c7fc7 | 4216 | { |
7fa343b7 | 4217 | struct perf_event *event; |
bd275681 PZ |
4218 | struct rb_node *node; |
4219 | struct rb_root *tree; | |
4220 | struct __group_key key = { | |
4221 | .pmu = pmu_ctx->pmu, | |
4222 | }; | |
7fa343b7 SL |
4223 | |
4224 | /* pick the first active flexible event */ | |
bd275681 | 4225 | event = list_first_entry_or_null(&pmu_ctx->flexible_active, |
7fa343b7 | 4226 | struct perf_event, active_list); |
bd275681 PZ |
4227 | if (event) |
4228 | goto out; | |
7fa343b7 SL |
4229 | |
4230 | /* if no active flexible event, pick the first event */ | |
bd275681 | 4231 | tree = &pmu_ctx->ctx->flexible_groups.tree; |
7fa343b7 | 4232 | |
bd275681 PZ |
4233 | if (!pmu_ctx->ctx->task) { |
4234 | key.cpu = smp_processor_id(); | |
4235 | ||
4236 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4237 | if (node) | |
4238 | event = __node_2_pe(node); | |
4239 | goto out; | |
7fa343b7 SL |
4240 | } |
4241 | ||
bd275681 PZ |
4242 | key.cpu = -1; |
4243 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4244 | if (node) { | |
4245 | event = __node_2_pe(node); | |
4246 | goto out; | |
4247 | } | |
4248 | ||
4249 | key.cpu = smp_processor_id(); | |
4250 | node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); | |
4251 | if (node) | |
4252 | event = __node_2_pe(node); | |
4253 | ||
4254 | out: | |
90c91dfb PZ |
4255 | /* |
4256 | * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in() | |
4257 | * finds there are unschedulable events, it will set it again. | |
4258 | */ | |
bd275681 | 4259 | pmu_ctx->rotate_necessary = 0; |
90c91dfb | 4260 | |
7fa343b7 | 4261 | return event; |
8d5bce0c PZ |
4262 | } |
4263 | ||
bd275681 | 4264 | static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc) |
8d5bce0c | 4265 | { |
bd275681 PZ |
4266 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4267 | struct perf_event_pmu_context *cpu_epc, *task_epc = NULL; | |
8d5bce0c | 4268 | struct perf_event *cpu_event = NULL, *task_event = NULL; |
fd7d5517 | 4269 | int cpu_rotate, task_rotate; |
bd275681 | 4270 | struct pmu *pmu; |
8d5bce0c PZ |
4271 | |
4272 | /* | |
4273 | * Since we run this from IRQ context, nobody can install new | |
4274 | * events, thus the event count values are stable. | |
4275 | */ | |
7fc23a53 | 4276 | |
bd275681 PZ |
4277 | cpu_epc = &cpc->epc; |
4278 | pmu = cpu_epc->pmu; | |
4279 | task_epc = cpc->task_epc; | |
4280 | ||
4281 | cpu_rotate = cpu_epc->rotate_necessary; | |
bd275681 | 4282 | task_rotate = task_epc ? task_epc->rotate_necessary : 0; |
9717e6cd | 4283 | |
8d5bce0c PZ |
4284 | if (!(cpu_rotate || task_rotate)) |
4285 | return false; | |
0f5a2601 | 4286 | |
facc4307 | 4287 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
bd275681 | 4288 | perf_pmu_disable(pmu); |
60db5e09 | 4289 | |
8d5bce0c | 4290 | if (task_rotate) |
bd275681 | 4291 | task_event = ctx_event_to_rotate(task_epc); |
8d5bce0c | 4292 | if (cpu_rotate) |
bd275681 | 4293 | cpu_event = ctx_event_to_rotate(cpu_epc); |
8703a7cf | 4294 | |
8d5bce0c PZ |
4295 | /* |
4296 | * As per the order given at ctx_resched() first 'pop' task flexible | |
4297 | * and then, if needed CPU flexible. | |
4298 | */ | |
bd275681 PZ |
4299 | if (task_event || (task_epc && cpu_event)) { |
4300 | update_context_time(task_epc->ctx); | |
4301 | __pmu_ctx_sched_out(task_epc, EVENT_FLEXIBLE); | |
4302 | } | |
0793a61d | 4303 | |
bd275681 PZ |
4304 | if (cpu_event) { |
4305 | update_context_time(&cpuctx->ctx); | |
4306 | __pmu_ctx_sched_out(cpu_epc, EVENT_FLEXIBLE); | |
8d5bce0c | 4307 | rotate_ctx(&cpuctx->ctx, cpu_event); |
bd275681 PZ |
4308 | __pmu_ctx_sched_in(&cpuctx->ctx, pmu); |
4309 | } | |
235c7fc7 | 4310 | |
bd275681 PZ |
4311 | if (task_event) |
4312 | rotate_ctx(task_epc->ctx, task_event); | |
235c7fc7 | 4313 | |
bd275681 PZ |
4314 | if (task_event || (task_epc && cpu_event)) |
4315 | __pmu_ctx_sched_in(task_epc->ctx, pmu); | |
235c7fc7 | 4316 | |
bd275681 | 4317 | perf_pmu_enable(pmu); |
0f5a2601 | 4318 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
9e630205 | 4319 | |
8d5bce0c | 4320 | return true; |
e9d2b064 PZ |
4321 | } |
4322 | ||
4323 | void perf_event_task_tick(void) | |
4324 | { | |
bd275681 PZ |
4325 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4326 | struct perf_event_context *ctx; | |
e050e3f0 | 4327 | int throttled; |
b5ab4cd5 | 4328 | |
16444645 | 4329 | lockdep_assert_irqs_disabled(); |
e9d2b064 | 4330 | |
e050e3f0 SE |
4331 | __this_cpu_inc(perf_throttled_seq); |
4332 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 4333 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 4334 | |
bd275681 PZ |
4335 | perf_adjust_freq_unthr_context(&cpuctx->ctx, !!throttled); |
4336 | ||
4337 | rcu_read_lock(); | |
4338 | ctx = rcu_dereference(current->perf_event_ctxp); | |
4339 | if (ctx) | |
4340 | perf_adjust_freq_unthr_context(ctx, !!throttled); | |
4341 | rcu_read_unlock(); | |
0793a61d TG |
4342 | } |
4343 | ||
889ff015 FW |
4344 | static int event_enable_on_exec(struct perf_event *event, |
4345 | struct perf_event_context *ctx) | |
4346 | { | |
4347 | if (!event->attr.enable_on_exec) | |
4348 | return 0; | |
4349 | ||
4350 | event->attr.enable_on_exec = 0; | |
4351 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
4352 | return 0; | |
4353 | ||
0d3d73aa | 4354 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
889ff015 FW |
4355 | |
4356 | return 1; | |
4357 | } | |
4358 | ||
57e7986e | 4359 | /* |
cdd6c482 | 4360 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
4361 | * This expects task == current. |
4362 | */ | |
bd275681 | 4363 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
57e7986e | 4364 | { |
bd275681 | 4365 | struct perf_event_context *clone_ctx = NULL; |
487f05e1 | 4366 | enum event_type_t event_type = 0; |
3e349507 | 4367 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 4368 | struct perf_event *event; |
57e7986e PM |
4369 | unsigned long flags; |
4370 | int enabled = 0; | |
4371 | ||
4372 | local_irq_save(flags); | |
bd275681 PZ |
4373 | if (WARN_ON_ONCE(current->perf_event_ctxp != ctx)) |
4374 | goto out; | |
4375 | ||
4376 | if (!ctx->nr_events) | |
57e7986e PM |
4377 | goto out; |
4378 | ||
bd275681 | 4379 | cpuctx = this_cpu_ptr(&perf_cpu_context); |
3e349507 | 4380 | perf_ctx_lock(cpuctx, ctx); |
bd275681 PZ |
4381 | ctx_sched_out(ctx, EVENT_TIME); |
4382 | ||
487f05e1 | 4383 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 4384 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
4385 | event_type |= get_event_type(event); |
4386 | } | |
57e7986e PM |
4387 | |
4388 | /* | |
3e349507 | 4389 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 4390 | */ |
3e349507 | 4391 | if (enabled) { |
211de6eb | 4392 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 4393 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb | 4394 | } else { |
bd275681 | 4395 | ctx_sched_in(ctx, EVENT_TIME); |
3e349507 PZ |
4396 | } |
4397 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 4398 | |
9ed6060d | 4399 | out: |
57e7986e | 4400 | local_irq_restore(flags); |
211de6eb PZ |
4401 | |
4402 | if (clone_ctx) | |
4403 | put_ctx(clone_ctx); | |
57e7986e PM |
4404 | } |
4405 | ||
2e498d0a ME |
4406 | static void perf_remove_from_owner(struct perf_event *event); |
4407 | static void perf_event_exit_event(struct perf_event *event, | |
4408 | struct perf_event_context *ctx); | |
4409 | ||
4410 | /* | |
4411 | * Removes all events from the current task that have been marked | |
4412 | * remove-on-exec, and feeds their values back to parent events. | |
4413 | */ | |
bd275681 | 4414 | static void perf_event_remove_on_exec(struct perf_event_context *ctx) |
2e498d0a | 4415 | { |
bd275681 | 4416 | struct perf_event_context *clone_ctx = NULL; |
2e498d0a | 4417 | struct perf_event *event, *next; |
2e498d0a ME |
4418 | unsigned long flags; |
4419 | bool modified = false; | |
4420 | ||
2e498d0a ME |
4421 | mutex_lock(&ctx->mutex); |
4422 | ||
4423 | if (WARN_ON_ONCE(ctx->task != current)) | |
4424 | goto unlock; | |
4425 | ||
4426 | list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) { | |
4427 | if (!event->attr.remove_on_exec) | |
4428 | continue; | |
4429 | ||
4430 | if (!is_kernel_event(event)) | |
4431 | perf_remove_from_owner(event); | |
4432 | ||
4433 | modified = true; | |
4434 | ||
4435 | perf_event_exit_event(event, ctx); | |
4436 | } | |
4437 | ||
4438 | raw_spin_lock_irqsave(&ctx->lock, flags); | |
4439 | if (modified) | |
4440 | clone_ctx = unclone_ctx(ctx); | |
2e498d0a ME |
4441 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
4442 | ||
4443 | unlock: | |
4444 | mutex_unlock(&ctx->mutex); | |
4445 | ||
2e498d0a ME |
4446 | if (clone_ctx) |
4447 | put_ctx(clone_ctx); | |
4448 | } | |
4449 | ||
0492d4c5 PZ |
4450 | struct perf_read_data { |
4451 | struct perf_event *event; | |
4452 | bool group; | |
7d88962e | 4453 | int ret; |
0492d4c5 PZ |
4454 | }; |
4455 | ||
451d24d1 | 4456 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 4457 | { |
d6a2f903 DCC |
4458 | u16 local_pkg, event_pkg; |
4459 | ||
1765bb61 TK |
4460 | if ((unsigned)event_cpu >= nr_cpu_ids) |
4461 | return event_cpu; | |
4462 | ||
d6a2f903 | 4463 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { |
451d24d1 PZ |
4464 | int local_cpu = smp_processor_id(); |
4465 | ||
4466 | event_pkg = topology_physical_package_id(event_cpu); | |
4467 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
4468 | |
4469 | if (event_pkg == local_pkg) | |
4470 | return local_cpu; | |
4471 | } | |
4472 | ||
4473 | return event_cpu; | |
4474 | } | |
4475 | ||
0793a61d | 4476 | /* |
cdd6c482 | 4477 | * Cross CPU call to read the hardware event |
0793a61d | 4478 | */ |
cdd6c482 | 4479 | static void __perf_event_read(void *info) |
0793a61d | 4480 | { |
0492d4c5 PZ |
4481 | struct perf_read_data *data = info; |
4482 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 4483 | struct perf_event_context *ctx = event->ctx; |
bd275681 | 4484 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
4a00c16e | 4485 | struct pmu *pmu = event->pmu; |
621a01ea | 4486 | |
e1ac3614 PM |
4487 | /* |
4488 | * If this is a task context, we need to check whether it is | |
4489 | * the current task context of this cpu. If not it has been | |
4490 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
4491 | * event->count would have been updated to a recent sample |
4492 | * when the event was scheduled out. | |
e1ac3614 PM |
4493 | */ |
4494 | if (ctx->task && cpuctx->task_ctx != ctx) | |
4495 | return; | |
4496 | ||
e625cce1 | 4497 | raw_spin_lock(&ctx->lock); |
0c1cbc18 | 4498 | if (ctx->is_active & EVENT_TIME) { |
542e72fc | 4499 | update_context_time(ctx); |
e5d1367f SE |
4500 | update_cgrp_time_from_event(event); |
4501 | } | |
0492d4c5 | 4502 | |
0d3d73aa PZ |
4503 | perf_event_update_time(event); |
4504 | if (data->group) | |
4505 | perf_event_update_sibling_time(event); | |
0c1cbc18 | 4506 | |
4a00c16e SB |
4507 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
4508 | goto unlock; | |
0492d4c5 | 4509 | |
4a00c16e SB |
4510 | if (!data->group) { |
4511 | pmu->read(event); | |
4512 | data->ret = 0; | |
0492d4c5 | 4513 | goto unlock; |
4a00c16e SB |
4514 | } |
4515 | ||
4516 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
4517 | ||
4518 | pmu->read(event); | |
0492d4c5 | 4519 | |
edb39592 | 4520 | for_each_sibling_event(sub, event) { |
4a00c16e SB |
4521 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
4522 | /* | |
4523 | * Use sibling's PMU rather than @event's since | |
4524 | * sibling could be on different (eg: software) PMU. | |
4525 | */ | |
0492d4c5 | 4526 | sub->pmu->read(sub); |
4a00c16e | 4527 | } |
0492d4c5 | 4528 | } |
4a00c16e SB |
4529 | |
4530 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
4531 | |
4532 | unlock: | |
e625cce1 | 4533 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
4534 | } |
4535 | ||
b5e58793 PZ |
4536 | static inline u64 perf_event_count(struct perf_event *event) |
4537 | { | |
c39a0e2c | 4538 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
4539 | } |
4540 | ||
09f5e7dc PZ |
4541 | static void calc_timer_values(struct perf_event *event, |
4542 | u64 *now, | |
4543 | u64 *enabled, | |
4544 | u64 *running) | |
4545 | { | |
4546 | u64 ctx_time; | |
4547 | ||
4548 | *now = perf_clock(); | |
4549 | ctx_time = perf_event_time_now(event, *now); | |
4550 | __perf_update_times(event, ctx_time, enabled, running); | |
4551 | } | |
4552 | ||
ffe8690c KX |
4553 | /* |
4554 | * NMI-safe method to read a local event, that is an event that | |
4555 | * is: | |
4556 | * - either for the current task, or for this CPU | |
4557 | * - does not have inherit set, for inherited task events | |
4558 | * will not be local and we cannot read them atomically | |
4559 | * - must not have a pmu::count method | |
4560 | */ | |
7d9285e8 YS |
4561 | int perf_event_read_local(struct perf_event *event, u64 *value, |
4562 | u64 *enabled, u64 *running) | |
ffe8690c KX |
4563 | { |
4564 | unsigned long flags; | |
1765bb61 TK |
4565 | int event_oncpu; |
4566 | int event_cpu; | |
f91840a3 | 4567 | int ret = 0; |
ffe8690c KX |
4568 | |
4569 | /* | |
4570 | * Disabling interrupts avoids all counter scheduling (context | |
4571 | * switches, timer based rotation and IPIs). | |
4572 | */ | |
4573 | local_irq_save(flags); | |
4574 | ||
ffe8690c KX |
4575 | /* |
4576 | * It must not be an event with inherit set, we cannot read | |
4577 | * all child counters from atomic context. | |
4578 | */ | |
f91840a3 AS |
4579 | if (event->attr.inherit) { |
4580 | ret = -EOPNOTSUPP; | |
4581 | goto out; | |
4582 | } | |
ffe8690c | 4583 | |
f91840a3 AS |
4584 | /* If this is a per-task event, it must be for current */ |
4585 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
4586 | event->hw.target != current) { | |
4587 | ret = -EINVAL; | |
4588 | goto out; | |
4589 | } | |
4590 | ||
1765bb61 TK |
4591 | /* |
4592 | * Get the event CPU numbers, and adjust them to local if the event is | |
4593 | * a per-package event that can be read locally | |
4594 | */ | |
4595 | event_oncpu = __perf_event_read_cpu(event, event->oncpu); | |
4596 | event_cpu = __perf_event_read_cpu(event, event->cpu); | |
4597 | ||
f91840a3 AS |
4598 | /* If this is a per-CPU event, it must be for this CPU */ |
4599 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
1765bb61 | 4600 | event_cpu != smp_processor_id()) { |
f91840a3 AS |
4601 | ret = -EINVAL; |
4602 | goto out; | |
4603 | } | |
ffe8690c | 4604 | |
befb1b3c | 4605 | /* If this is a pinned event it must be running on this CPU */ |
1765bb61 | 4606 | if (event->attr.pinned && event_oncpu != smp_processor_id()) { |
befb1b3c RC |
4607 | ret = -EBUSY; |
4608 | goto out; | |
4609 | } | |
4610 | ||
ffe8690c KX |
4611 | /* |
4612 | * If the event is currently on this CPU, its either a per-task event, | |
4613 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
4614 | * oncpu == -1). | |
4615 | */ | |
1765bb61 | 4616 | if (event_oncpu == smp_processor_id()) |
ffe8690c KX |
4617 | event->pmu->read(event); |
4618 | ||
f91840a3 | 4619 | *value = local64_read(&event->count); |
0d3d73aa | 4620 | if (enabled || running) { |
99643bab | 4621 | u64 __enabled, __running, __now; |
0d3d73aa | 4622 | |
09f5e7dc | 4623 | calc_timer_values(event, &__now, &__enabled, &__running); |
0d3d73aa PZ |
4624 | if (enabled) |
4625 | *enabled = __enabled; | |
4626 | if (running) | |
4627 | *running = __running; | |
4628 | } | |
f91840a3 | 4629 | out: |
ffe8690c KX |
4630 | local_irq_restore(flags); |
4631 | ||
f91840a3 | 4632 | return ret; |
ffe8690c KX |
4633 | } |
4634 | ||
7d88962e | 4635 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 4636 | { |
0c1cbc18 | 4637 | enum perf_event_state state = READ_ONCE(event->state); |
451d24d1 | 4638 | int event_cpu, ret = 0; |
7d88962e | 4639 | |
0793a61d | 4640 | /* |
cdd6c482 IM |
4641 | * If event is enabled and currently active on a CPU, update the |
4642 | * value in the event structure: | |
0793a61d | 4643 | */ |
0c1cbc18 PZ |
4644 | again: |
4645 | if (state == PERF_EVENT_STATE_ACTIVE) { | |
4646 | struct perf_read_data data; | |
4647 | ||
4648 | /* | |
4649 | * Orders the ->state and ->oncpu loads such that if we see | |
4650 | * ACTIVE we must also see the right ->oncpu. | |
4651 | * | |
4652 | * Matches the smp_wmb() from event_sched_in(). | |
4653 | */ | |
4654 | smp_rmb(); | |
d6a2f903 | 4655 | |
451d24d1 PZ |
4656 | event_cpu = READ_ONCE(event->oncpu); |
4657 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
4658 | return 0; | |
4659 | ||
0c1cbc18 PZ |
4660 | data = (struct perf_read_data){ |
4661 | .event = event, | |
4662 | .group = group, | |
4663 | .ret = 0, | |
4664 | }; | |
4665 | ||
451d24d1 PZ |
4666 | preempt_disable(); |
4667 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 4668 | |
58763148 PZ |
4669 | /* |
4670 | * Purposely ignore the smp_call_function_single() return | |
4671 | * value. | |
4672 | * | |
451d24d1 | 4673 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
4674 | * scheduled out and that will have updated the event count. |
4675 | * | |
4676 | * Therefore, either way, we'll have an up-to-date event count | |
4677 | * after this. | |
4678 | */ | |
451d24d1 PZ |
4679 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
4680 | preempt_enable(); | |
58763148 | 4681 | ret = data.ret; |
0c1cbc18 PZ |
4682 | |
4683 | } else if (state == PERF_EVENT_STATE_INACTIVE) { | |
2b8988c9 PZ |
4684 | struct perf_event_context *ctx = event->ctx; |
4685 | unsigned long flags; | |
4686 | ||
e625cce1 | 4687 | raw_spin_lock_irqsave(&ctx->lock, flags); |
0c1cbc18 PZ |
4688 | state = event->state; |
4689 | if (state != PERF_EVENT_STATE_INACTIVE) { | |
4690 | raw_spin_unlock_irqrestore(&ctx->lock, flags); | |
4691 | goto again; | |
4692 | } | |
4693 | ||
c530ccd9 | 4694 | /* |
0c1cbc18 PZ |
4695 | * May read while context is not active (e.g., thread is |
4696 | * blocked), in that case we cannot update context time | |
c530ccd9 | 4697 | */ |
0c1cbc18 | 4698 | if (ctx->is_active & EVENT_TIME) { |
c530ccd9 | 4699 | update_context_time(ctx); |
e5d1367f SE |
4700 | update_cgrp_time_from_event(event); |
4701 | } | |
0c1cbc18 | 4702 | |
0d3d73aa | 4703 | perf_event_update_time(event); |
0492d4c5 | 4704 | if (group) |
0d3d73aa | 4705 | perf_event_update_sibling_time(event); |
e625cce1 | 4706 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 4707 | } |
7d88962e SB |
4708 | |
4709 | return ret; | |
0793a61d TG |
4710 | } |
4711 | ||
a63eaf34 | 4712 | /* |
cdd6c482 | 4713 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 4714 | */ |
eb184479 | 4715 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 4716 | { |
e625cce1 | 4717 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 4718 | mutex_init(&ctx->mutex); |
bd275681 | 4719 | INIT_LIST_HEAD(&ctx->pmu_ctx_list); |
8e1a2031 AB |
4720 | perf_event_groups_init(&ctx->pinned_groups); |
4721 | perf_event_groups_init(&ctx->flexible_groups); | |
a63eaf34 | 4722 | INIT_LIST_HEAD(&ctx->event_list); |
8c94abbb | 4723 | refcount_set(&ctx->refcount, 1); |
eb184479 PZ |
4724 | } |
4725 | ||
bd275681 PZ |
4726 | static void |
4727 | __perf_init_event_pmu_context(struct perf_event_pmu_context *epc, struct pmu *pmu) | |
4728 | { | |
4729 | epc->pmu = pmu; | |
4730 | INIT_LIST_HEAD(&epc->pmu_ctx_entry); | |
4731 | INIT_LIST_HEAD(&epc->pinned_active); | |
4732 | INIT_LIST_HEAD(&epc->flexible_active); | |
4733 | atomic_set(&epc->refcount, 1); | |
4734 | } | |
4735 | ||
eb184479 | 4736 | static struct perf_event_context * |
bd275681 | 4737 | alloc_perf_context(struct task_struct *task) |
eb184479 PZ |
4738 | { |
4739 | struct perf_event_context *ctx; | |
4740 | ||
4741 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
4742 | if (!ctx) | |
4743 | return NULL; | |
4744 | ||
4745 | __perf_event_init_context(ctx); | |
7b3c92b8 MWO |
4746 | if (task) |
4747 | ctx->task = get_task_struct(task); | |
eb184479 PZ |
4748 | |
4749 | return ctx; | |
a63eaf34 PM |
4750 | } |
4751 | ||
2ebd4ffb MH |
4752 | static struct task_struct * |
4753 | find_lively_task_by_vpid(pid_t vpid) | |
4754 | { | |
4755 | struct task_struct *task; | |
0793a61d TG |
4756 | |
4757 | rcu_read_lock(); | |
2ebd4ffb | 4758 | if (!vpid) |
0793a61d TG |
4759 | task = current; |
4760 | else | |
2ebd4ffb | 4761 | task = find_task_by_vpid(vpid); |
0793a61d TG |
4762 | if (task) |
4763 | get_task_struct(task); | |
4764 | rcu_read_unlock(); | |
4765 | ||
4766 | if (!task) | |
4767 | return ERR_PTR(-ESRCH); | |
4768 | ||
2ebd4ffb | 4769 | return task; |
2ebd4ffb MH |
4770 | } |
4771 | ||
fe4b04fa PZ |
4772 | /* |
4773 | * Returns a matching context with refcount and pincount. | |
4774 | */ | |
108b02cf | 4775 | static struct perf_event_context * |
bd275681 | 4776 | find_get_context(struct task_struct *task, struct perf_event *event) |
0793a61d | 4777 | { |
211de6eb | 4778 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 4779 | struct perf_cpu_context *cpuctx; |
25346b93 | 4780 | unsigned long flags; |
bd275681 | 4781 | int err; |
0793a61d | 4782 | |
22a4ec72 | 4783 | if (!task) { |
cdd6c482 | 4784 | /* Must be root to operate on a CPU event: */ |
da97e184 JFG |
4785 | err = perf_allow_cpu(&event->attr); |
4786 | if (err) | |
4787 | return ERR_PTR(err); | |
0793a61d | 4788 | |
bd275681 | 4789 | cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); |
0793a61d | 4790 | ctx = &cpuctx->ctx; |
c93f7669 | 4791 | get_ctx(ctx); |
6c605f83 | 4792 | raw_spin_lock_irqsave(&ctx->lock, flags); |
fe4b04fa | 4793 | ++ctx->pin_count; |
6c605f83 | 4794 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 4795 | |
0793a61d TG |
4796 | return ctx; |
4797 | } | |
4798 | ||
8dc85d54 | 4799 | err = -EINVAL; |
9ed6060d | 4800 | retry: |
bd275681 | 4801 | ctx = perf_lock_task_context(task, &flags); |
c93f7669 | 4802 | if (ctx) { |
211de6eb | 4803 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 4804 | ++ctx->pin_count; |
4af57ef2 | 4805 | |
e625cce1 | 4806 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
4807 | |
4808 | if (clone_ctx) | |
4809 | put_ctx(clone_ctx); | |
9137fb28 | 4810 | } else { |
bd275681 | 4811 | ctx = alloc_perf_context(task); |
c93f7669 PM |
4812 | err = -ENOMEM; |
4813 | if (!ctx) | |
4814 | goto errout; | |
eb184479 | 4815 | |
dbe08d82 ON |
4816 | err = 0; |
4817 | mutex_lock(&task->perf_event_mutex); | |
4818 | /* | |
4819 | * If it has already passed perf_event_exit_task(). | |
4820 | * we must see PF_EXITING, it takes this mutex too. | |
4821 | */ | |
4822 | if (task->flags & PF_EXITING) | |
4823 | err = -ESRCH; | |
bd275681 | 4824 | else if (task->perf_event_ctxp) |
dbe08d82 | 4825 | err = -EAGAIN; |
fe4b04fa | 4826 | else { |
9137fb28 | 4827 | get_ctx(ctx); |
fe4b04fa | 4828 | ++ctx->pin_count; |
bd275681 | 4829 | rcu_assign_pointer(task->perf_event_ctxp, ctx); |
fe4b04fa | 4830 | } |
dbe08d82 ON |
4831 | mutex_unlock(&task->perf_event_mutex); |
4832 | ||
4833 | if (unlikely(err)) { | |
9137fb28 | 4834 | put_ctx(ctx); |
dbe08d82 ON |
4835 | |
4836 | if (err == -EAGAIN) | |
4837 | goto retry; | |
4838 | goto errout; | |
a63eaf34 PM |
4839 | } |
4840 | } | |
4841 | ||
0793a61d | 4842 | return ctx; |
c93f7669 | 4843 | |
9ed6060d | 4844 | errout: |
c93f7669 | 4845 | return ERR_PTR(err); |
0793a61d TG |
4846 | } |
4847 | ||
bd275681 PZ |
4848 | static struct perf_event_pmu_context * |
4849 | find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx, | |
4850 | struct perf_event *event) | |
4851 | { | |
4852 | struct perf_event_pmu_context *new = NULL, *epc; | |
4853 | void *task_ctx_data = NULL; | |
4854 | ||
4855 | if (!ctx->task) { | |
889c58b3 PZ |
4856 | /* |
4857 | * perf_pmu_migrate_context() / __perf_pmu_install_event() | |
4858 | * relies on the fact that find_get_pmu_context() cannot fail | |
4859 | * for CPU contexts. | |
4860 | */ | |
bd275681 PZ |
4861 | struct perf_cpu_pmu_context *cpc; |
4862 | ||
4863 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu); | |
4864 | epc = &cpc->epc; | |
4f64a6c9 | 4865 | raw_spin_lock_irq(&ctx->lock); |
bd275681 PZ |
4866 | if (!epc->ctx) { |
4867 | atomic_set(&epc->refcount, 1); | |
4868 | epc->embedded = 1; | |
bd275681 PZ |
4869 | list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); |
4870 | epc->ctx = ctx; | |
bd275681 PZ |
4871 | } else { |
4872 | WARN_ON_ONCE(epc->ctx != ctx); | |
4873 | atomic_inc(&epc->refcount); | |
4874 | } | |
4f64a6c9 | 4875 | raw_spin_unlock_irq(&ctx->lock); |
bd275681 PZ |
4876 | return epc; |
4877 | } | |
4878 | ||
4879 | new = kzalloc(sizeof(*epc), GFP_KERNEL); | |
4880 | if (!new) | |
4881 | return ERR_PTR(-ENOMEM); | |
4882 | ||
4883 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { | |
4884 | task_ctx_data = alloc_task_ctx_data(pmu); | |
4885 | if (!task_ctx_data) { | |
4886 | kfree(new); | |
4887 | return ERR_PTR(-ENOMEM); | |
4888 | } | |
4889 | } | |
4890 | ||
4891 | __perf_init_event_pmu_context(new, pmu); | |
4892 | ||
4893 | /* | |
4894 | * XXX | |
4895 | * | |
4896 | * lockdep_assert_held(&ctx->mutex); | |
4897 | * | |
4898 | * can't because perf_event_init_task() doesn't actually hold the | |
4899 | * child_ctx->mutex. | |
4900 | */ | |
4901 | ||
4902 | raw_spin_lock_irq(&ctx->lock); | |
4903 | list_for_each_entry(epc, &ctx->pmu_ctx_list, pmu_ctx_entry) { | |
4904 | if (epc->pmu == pmu) { | |
4905 | WARN_ON_ONCE(epc->ctx != ctx); | |
4906 | atomic_inc(&epc->refcount); | |
4907 | goto found_epc; | |
4908 | } | |
4909 | } | |
4910 | ||
4911 | epc = new; | |
4912 | new = NULL; | |
4913 | ||
4914 | list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); | |
4915 | epc->ctx = ctx; | |
4916 | ||
4917 | found_epc: | |
4918 | if (task_ctx_data && !epc->task_ctx_data) { | |
4919 | epc->task_ctx_data = task_ctx_data; | |
4920 | task_ctx_data = NULL; | |
4921 | ctx->nr_task_data++; | |
4922 | } | |
4923 | raw_spin_unlock_irq(&ctx->lock); | |
4924 | ||
4925 | free_task_ctx_data(pmu, task_ctx_data); | |
4926 | kfree(new); | |
4927 | ||
4928 | return epc; | |
4929 | } | |
4930 | ||
4931 | static void get_pmu_ctx(struct perf_event_pmu_context *epc) | |
4932 | { | |
4933 | WARN_ON_ONCE(!atomic_inc_not_zero(&epc->refcount)); | |
4934 | } | |
4935 | ||
4936 | static void free_epc_rcu(struct rcu_head *head) | |
4937 | { | |
4938 | struct perf_event_pmu_context *epc = container_of(head, typeof(*epc), rcu_head); | |
4939 | ||
4940 | kfree(epc->task_ctx_data); | |
4941 | kfree(epc); | |
4942 | } | |
4943 | ||
4944 | static void put_pmu_ctx(struct perf_event_pmu_context *epc) | |
4945 | { | |
4f64a6c9 | 4946 | struct perf_event_context *ctx = epc->ctx; |
bd275681 PZ |
4947 | unsigned long flags; |
4948 | ||
4f64a6c9 JC |
4949 | /* |
4950 | * XXX | |
4951 | * | |
4952 | * lockdep_assert_held(&ctx->mutex); | |
4953 | * | |
4954 | * can't because of the call-site in _free_event()/put_event() | |
4955 | * which isn't always called under ctx->mutex. | |
4956 | */ | |
4957 | if (!atomic_dec_and_raw_lock_irqsave(&epc->refcount, &ctx->lock, flags)) | |
bd275681 PZ |
4958 | return; |
4959 | ||
4f64a6c9 | 4960 | WARN_ON_ONCE(list_empty(&epc->pmu_ctx_entry)); |
bd275681 | 4961 | |
4f64a6c9 JC |
4962 | list_del_init(&epc->pmu_ctx_entry); |
4963 | epc->ctx = NULL; | |
bd275681 PZ |
4964 | |
4965 | WARN_ON_ONCE(!list_empty(&epc->pinned_active)); | |
4966 | WARN_ON_ONCE(!list_empty(&epc->flexible_active)); | |
4967 | ||
4f64a6c9 JC |
4968 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
4969 | ||
bd275681 PZ |
4970 | if (epc->embedded) |
4971 | return; | |
4972 | ||
4973 | call_rcu(&epc->rcu_head, free_epc_rcu); | |
4974 | } | |
4975 | ||
6fb2915d LZ |
4976 | static void perf_event_free_filter(struct perf_event *event); |
4977 | ||
cdd6c482 | 4978 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 4979 | { |
bd275681 | 4980 | struct perf_event *event = container_of(head, typeof(*event), rcu_head); |
592903cd | 4981 | |
cdd6c482 IM |
4982 | if (event->ns) |
4983 | put_pid_ns(event->ns); | |
6fb2915d | 4984 | perf_event_free_filter(event); |
bdacfaf2 | 4985 | kmem_cache_free(perf_event_cache, event); |
592903cd PZ |
4986 | } |
4987 | ||
b69cf536 | 4988 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 4989 | struct perf_buffer *rb); |
925d519a | 4990 | |
f2fb6bef KL |
4991 | static void detach_sb_event(struct perf_event *event) |
4992 | { | |
4993 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
4994 | ||
4995 | raw_spin_lock(&pel->lock); | |
4996 | list_del_rcu(&event->sb_list); | |
4997 | raw_spin_unlock(&pel->lock); | |
4998 | } | |
4999 | ||
a4f144eb | 5000 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 5001 | { |
a4f144eb DCC |
5002 | struct perf_event_attr *attr = &event->attr; |
5003 | ||
f2fb6bef | 5004 | if (event->parent) |
a4f144eb | 5005 | return false; |
f2fb6bef KL |
5006 | |
5007 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 5008 | return false; |
f2fb6bef | 5009 | |
a4f144eb DCC |
5010 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
5011 | attr->comm || attr->comm_exec || | |
76193a94 | 5012 | attr->task || attr->ksymbol || |
e17d43b9 | 5013 | attr->context_switch || attr->text_poke || |
21038f2b | 5014 | attr->bpf_event) |
a4f144eb DCC |
5015 | return true; |
5016 | return false; | |
5017 | } | |
5018 | ||
5019 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
5020 | { | |
5021 | if (is_sb_event(event)) | |
5022 | detach_sb_event(event); | |
f2fb6bef KL |
5023 | } |
5024 | ||
555e0c1e FW |
5025 | #ifdef CONFIG_NO_HZ_FULL |
5026 | static DEFINE_SPINLOCK(nr_freq_lock); | |
5027 | #endif | |
5028 | ||
5029 | static void unaccount_freq_event_nohz(void) | |
5030 | { | |
5031 | #ifdef CONFIG_NO_HZ_FULL | |
5032 | spin_lock(&nr_freq_lock); | |
5033 | if (atomic_dec_and_test(&nr_freq_events)) | |
5034 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
5035 | spin_unlock(&nr_freq_lock); | |
5036 | #endif | |
5037 | } | |
5038 | ||
5039 | static void unaccount_freq_event(void) | |
5040 | { | |
5041 | if (tick_nohz_full_enabled()) | |
5042 | unaccount_freq_event_nohz(); | |
5043 | else | |
5044 | atomic_dec(&nr_freq_events); | |
5045 | } | |
5046 | ||
4beb31f3 FW |
5047 | static void unaccount_event(struct perf_event *event) |
5048 | { | |
25432ae9 PZ |
5049 | bool dec = false; |
5050 | ||
4beb31f3 FW |
5051 | if (event->parent) |
5052 | return; | |
5053 | ||
a5398bff | 5054 | if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) |
25432ae9 | 5055 | dec = true; |
4beb31f3 FW |
5056 | if (event->attr.mmap || event->attr.mmap_data) |
5057 | atomic_dec(&nr_mmap_events); | |
88a16a13 JO |
5058 | if (event->attr.build_id) |
5059 | atomic_dec(&nr_build_id_events); | |
4beb31f3 FW |
5060 | if (event->attr.comm) |
5061 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
5062 | if (event->attr.namespaces) |
5063 | atomic_dec(&nr_namespaces_events); | |
96aaab68 NK |
5064 | if (event->attr.cgroup) |
5065 | atomic_dec(&nr_cgroup_events); | |
4beb31f3 FW |
5066 | if (event->attr.task) |
5067 | atomic_dec(&nr_task_events); | |
948b26b6 | 5068 | if (event->attr.freq) |
555e0c1e | 5069 | unaccount_freq_event(); |
45ac1403 | 5070 | if (event->attr.context_switch) { |
25432ae9 | 5071 | dec = true; |
45ac1403 AH |
5072 | atomic_dec(&nr_switch_events); |
5073 | } | |
4beb31f3 | 5074 | if (is_cgroup_event(event)) |
25432ae9 | 5075 | dec = true; |
4beb31f3 | 5076 | if (has_branch_stack(event)) |
25432ae9 | 5077 | dec = true; |
76193a94 SL |
5078 | if (event->attr.ksymbol) |
5079 | atomic_dec(&nr_ksymbol_events); | |
6ee52e2a SL |
5080 | if (event->attr.bpf_event) |
5081 | atomic_dec(&nr_bpf_events); | |
e17d43b9 AH |
5082 | if (event->attr.text_poke) |
5083 | atomic_dec(&nr_text_poke_events); | |
25432ae9 | 5084 | |
9107c89e PZ |
5085 | if (dec) { |
5086 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
5087 | schedule_delayed_work(&perf_sched_work, HZ); | |
5088 | } | |
4beb31f3 | 5089 | |
f2fb6bef | 5090 | unaccount_pmu_sb_event(event); |
4beb31f3 | 5091 | } |
925d519a | 5092 | |
9107c89e PZ |
5093 | static void perf_sched_delayed(struct work_struct *work) |
5094 | { | |
5095 | mutex_lock(&perf_sched_mutex); | |
5096 | if (atomic_dec_and_test(&perf_sched_count)) | |
5097 | static_branch_disable(&perf_sched_events); | |
5098 | mutex_unlock(&perf_sched_mutex); | |
5099 | } | |
5100 | ||
bed5b25a AS |
5101 | /* |
5102 | * The following implement mutual exclusion of events on "exclusive" pmus | |
5103 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
5104 | * at a time, so we disallow creating events that might conflict, namely: | |
5105 | * | |
5106 | * 1) cpu-wide events in the presence of per-task events, | |
5107 | * 2) per-task events in the presence of cpu-wide events, | |
bd275681 | 5108 | * 3) two matching events on the same perf_event_context. |
bed5b25a AS |
5109 | * |
5110 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 5111 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
5112 | */ |
5113 | static int exclusive_event_init(struct perf_event *event) | |
5114 | { | |
5115 | struct pmu *pmu = event->pmu; | |
5116 | ||
8a58ddae | 5117 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
5118 | return 0; |
5119 | ||
5120 | /* | |
5121 | * Prevent co-existence of per-task and cpu-wide events on the | |
5122 | * same exclusive pmu. | |
5123 | * | |
5124 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
5125 | * events on this "exclusive" pmu, positive means there are | |
5126 | * per-task events. | |
5127 | * | |
5128 | * Since this is called in perf_event_alloc() path, event::ctx | |
5129 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
5130 | * to mean "per-task event", because unlike other attach states it | |
5131 | * never gets cleared. | |
5132 | */ | |
5133 | if (event->attach_state & PERF_ATTACH_TASK) { | |
5134 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
5135 | return -EBUSY; | |
5136 | } else { | |
5137 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
5138 | return -EBUSY; | |
5139 | } | |
5140 | ||
5141 | return 0; | |
5142 | } | |
5143 | ||
5144 | static void exclusive_event_destroy(struct perf_event *event) | |
5145 | { | |
5146 | struct pmu *pmu = event->pmu; | |
5147 | ||
8a58ddae | 5148 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
5149 | return; |
5150 | ||
5151 | /* see comment in exclusive_event_init() */ | |
5152 | if (event->attach_state & PERF_ATTACH_TASK) | |
5153 | atomic_dec(&pmu->exclusive_cnt); | |
5154 | else | |
5155 | atomic_inc(&pmu->exclusive_cnt); | |
5156 | } | |
5157 | ||
5158 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
5159 | { | |
3bf6215a | 5160 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
5161 | (e1->cpu == e2->cpu || |
5162 | e1->cpu == -1 || | |
5163 | e2->cpu == -1)) | |
5164 | return true; | |
5165 | return false; | |
5166 | } | |
5167 | ||
bed5b25a AS |
5168 | static bool exclusive_event_installable(struct perf_event *event, |
5169 | struct perf_event_context *ctx) | |
5170 | { | |
5171 | struct perf_event *iter_event; | |
5172 | struct pmu *pmu = event->pmu; | |
5173 | ||
8a58ddae AS |
5174 | lockdep_assert_held(&ctx->mutex); |
5175 | ||
5176 | if (!is_exclusive_pmu(pmu)) | |
bed5b25a AS |
5177 | return true; |
5178 | ||
5179 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
5180 | if (exclusive_event_match(iter_event, event)) | |
5181 | return false; | |
5182 | } | |
5183 | ||
5184 | return true; | |
5185 | } | |
5186 | ||
375637bc AS |
5187 | static void perf_addr_filters_splice(struct perf_event *event, |
5188 | struct list_head *head); | |
5189 | ||
683ede43 | 5190 | static void _free_event(struct perf_event *event) |
f1600952 | 5191 | { |
ca6c2132 | 5192 | irq_work_sync(&event->pending_irq); |
925d519a | 5193 | |
4beb31f3 | 5194 | unaccount_event(event); |
9ee318a7 | 5195 | |
da97e184 JFG |
5196 | security_perf_event_free(event); |
5197 | ||
76369139 | 5198 | if (event->rb) { |
9bb5d40c PZ |
5199 | /* |
5200 | * Can happen when we close an event with re-directed output. | |
5201 | * | |
5202 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
5203 | * over us; possibly making our ring_buffer_put() the last. | |
5204 | */ | |
5205 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 5206 | ring_buffer_attach(event, NULL); |
9bb5d40c | 5207 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
5208 | } |
5209 | ||
e5d1367f SE |
5210 | if (is_cgroup_event(event)) |
5211 | perf_detach_cgroup(event); | |
5212 | ||
a0733e69 PZ |
5213 | if (!event->parent) { |
5214 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
5215 | put_callchain_buffers(); | |
5216 | } | |
5217 | ||
5218 | perf_event_free_bpf_prog(event); | |
375637bc | 5219 | perf_addr_filters_splice(event, NULL); |
c60f83b8 | 5220 | kfree(event->addr_filter_ranges); |
a0733e69 PZ |
5221 | |
5222 | if (event->destroy) | |
5223 | event->destroy(event); | |
5224 | ||
1cf8dfe8 PZ |
5225 | /* |
5226 | * Must be after ->destroy(), due to uprobe_perf_close() using | |
5227 | * hw.target. | |
5228 | */ | |
621b6d2e PB |
5229 | if (event->hw.target) |
5230 | put_task_struct(event->hw.target); | |
5231 | ||
bd275681 PZ |
5232 | if (event->pmu_ctx) |
5233 | put_pmu_ctx(event->pmu_ctx); | |
5234 | ||
1cf8dfe8 PZ |
5235 | /* |
5236 | * perf_event_free_task() relies on put_ctx() being 'last', in particular | |
5237 | * all task references must be cleaned up. | |
5238 | */ | |
5239 | if (event->ctx) | |
5240 | put_ctx(event->ctx); | |
5241 | ||
62a92c8f AS |
5242 | exclusive_event_destroy(event); |
5243 | module_put(event->pmu->module); | |
a0733e69 PZ |
5244 | |
5245 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
5246 | } |
5247 | ||
683ede43 PZ |
5248 | /* |
5249 | * Used to free events which have a known refcount of 1, such as in error paths | |
5250 | * where the event isn't exposed yet and inherited events. | |
5251 | */ | |
5252 | static void free_event(struct perf_event *event) | |
0793a61d | 5253 | { |
683ede43 PZ |
5254 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
5255 | "unexpected event refcount: %ld; ptr=%p\n", | |
5256 | atomic_long_read(&event->refcount), event)) { | |
5257 | /* leak to avoid use-after-free */ | |
5258 | return; | |
5259 | } | |
0793a61d | 5260 | |
683ede43 | 5261 | _free_event(event); |
0793a61d TG |
5262 | } |
5263 | ||
a66a3052 | 5264 | /* |
f8697762 | 5265 | * Remove user event from the owner task. |
a66a3052 | 5266 | */ |
f8697762 | 5267 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 5268 | { |
8882135b | 5269 | struct task_struct *owner; |
fb0459d7 | 5270 | |
8882135b | 5271 | rcu_read_lock(); |
8882135b | 5272 | /* |
f47c02c0 PZ |
5273 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
5274 | * observe !owner it means the list deletion is complete and we can | |
5275 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
5276 | * owner->perf_event_mutex. |
5277 | */ | |
506458ef | 5278 | owner = READ_ONCE(event->owner); |
8882135b PZ |
5279 | if (owner) { |
5280 | /* | |
5281 | * Since delayed_put_task_struct() also drops the last | |
5282 | * task reference we can safely take a new reference | |
5283 | * while holding the rcu_read_lock(). | |
5284 | */ | |
5285 | get_task_struct(owner); | |
5286 | } | |
5287 | rcu_read_unlock(); | |
5288 | ||
5289 | if (owner) { | |
f63a8daa PZ |
5290 | /* |
5291 | * If we're here through perf_event_exit_task() we're already | |
5292 | * holding ctx->mutex which would be an inversion wrt. the | |
5293 | * normal lock order. | |
5294 | * | |
5295 | * However we can safely take this lock because its the child | |
5296 | * ctx->mutex. | |
5297 | */ | |
5298 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
5299 | ||
8882135b PZ |
5300 | /* |
5301 | * We have to re-check the event->owner field, if it is cleared | |
5302 | * we raced with perf_event_exit_task(), acquiring the mutex | |
5303 | * ensured they're done, and we can proceed with freeing the | |
5304 | * event. | |
5305 | */ | |
f47c02c0 | 5306 | if (event->owner) { |
8882135b | 5307 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
5308 | smp_store_release(&event->owner, NULL); |
5309 | } | |
8882135b PZ |
5310 | mutex_unlock(&owner->perf_event_mutex); |
5311 | put_task_struct(owner); | |
5312 | } | |
f8697762 JO |
5313 | } |
5314 | ||
f8697762 JO |
5315 | static void put_event(struct perf_event *event) |
5316 | { | |
f8697762 JO |
5317 | if (!atomic_long_dec_and_test(&event->refcount)) |
5318 | return; | |
5319 | ||
c6e5b732 PZ |
5320 | _free_event(event); |
5321 | } | |
5322 | ||
5323 | /* | |
5324 | * Kill an event dead; while event:refcount will preserve the event | |
5325 | * object, it will not preserve its functionality. Once the last 'user' | |
5326 | * gives up the object, we'll destroy the thing. | |
5327 | */ | |
5328 | int perf_event_release_kernel(struct perf_event *event) | |
5329 | { | |
a4f4bb6d | 5330 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 | 5331 | struct perf_event *child, *tmp; |
82d94856 | 5332 | LIST_HEAD(free_list); |
c6e5b732 | 5333 | |
a4f4bb6d | 5334 | /* |
bd275681 PZ |
5335 | * If we got here through err_alloc: free_event(event); we will not |
5336 | * have attached to a context yet. | |
a4f4bb6d PZ |
5337 | */ |
5338 | if (!ctx) { | |
5339 | WARN_ON_ONCE(event->attach_state & | |
5340 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
5341 | goto no_ctx; | |
5342 | } | |
5343 | ||
f8697762 JO |
5344 | if (!is_kernel_event(event)) |
5345 | perf_remove_from_owner(event); | |
8882135b | 5346 | |
5fa7c8ec | 5347 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 5348 | WARN_ON_ONCE(ctx->parent_ctx); |
683ede43 | 5349 | |
683ede43 | 5350 | /* |
d8a8cfc7 | 5351 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 5352 | * anymore. |
683ede43 | 5353 | * |
a69b0ca4 PZ |
5354 | * Anybody acquiring event->child_mutex after the below loop _must_ |
5355 | * also see this, most importantly inherit_event() which will avoid | |
5356 | * placing more children on the list. | |
683ede43 | 5357 | * |
c6e5b732 PZ |
5358 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
5359 | * child events. | |
683ede43 | 5360 | */ |
517e6a30 | 5361 | perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD); |
a69b0ca4 PZ |
5362 | |
5363 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 5364 | |
c6e5b732 PZ |
5365 | again: |
5366 | mutex_lock(&event->child_mutex); | |
5367 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 5368 | |
c6e5b732 PZ |
5369 | /* |
5370 | * Cannot change, child events are not migrated, see the | |
5371 | * comment with perf_event_ctx_lock_nested(). | |
5372 | */ | |
506458ef | 5373 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
5374 | /* |
5375 | * Since child_mutex nests inside ctx::mutex, we must jump | |
5376 | * through hoops. We start by grabbing a reference on the ctx. | |
5377 | * | |
5378 | * Since the event cannot get freed while we hold the | |
5379 | * child_mutex, the context must also exist and have a !0 | |
5380 | * reference count. | |
5381 | */ | |
5382 | get_ctx(ctx); | |
5383 | ||
5384 | /* | |
5385 | * Now that we have a ctx ref, we can drop child_mutex, and | |
5386 | * acquire ctx::mutex without fear of it going away. Then we | |
5387 | * can re-acquire child_mutex. | |
5388 | */ | |
5389 | mutex_unlock(&event->child_mutex); | |
5390 | mutex_lock(&ctx->mutex); | |
5391 | mutex_lock(&event->child_mutex); | |
5392 | ||
5393 | /* | |
5394 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
5395 | * state, if child is still the first entry, it didn't get freed | |
5396 | * and we can continue doing so. | |
5397 | */ | |
5398 | tmp = list_first_entry_or_null(&event->child_list, | |
5399 | struct perf_event, child_list); | |
5400 | if (tmp == child) { | |
5401 | perf_remove_from_context(child, DETACH_GROUP); | |
82d94856 | 5402 | list_move(&child->child_list, &free_list); |
c6e5b732 PZ |
5403 | /* |
5404 | * This matches the refcount bump in inherit_event(); | |
5405 | * this can't be the last reference. | |
5406 | */ | |
5407 | put_event(event); | |
5408 | } | |
5409 | ||
5410 | mutex_unlock(&event->child_mutex); | |
5411 | mutex_unlock(&ctx->mutex); | |
5412 | put_ctx(ctx); | |
5413 | goto again; | |
5414 | } | |
5415 | mutex_unlock(&event->child_mutex); | |
5416 | ||
82d94856 | 5417 | list_for_each_entry_safe(child, tmp, &free_list, child_list) { |
1cf8dfe8 PZ |
5418 | void *var = &child->ctx->refcount; |
5419 | ||
82d94856 PZ |
5420 | list_del(&child->child_list); |
5421 | free_event(child); | |
1cf8dfe8 PZ |
5422 | |
5423 | /* | |
5424 | * Wake any perf_event_free_task() waiting for this event to be | |
5425 | * freed. | |
5426 | */ | |
5427 | smp_mb(); /* pairs with wait_var_event() */ | |
5428 | wake_up_var(var); | |
82d94856 PZ |
5429 | } |
5430 | ||
a4f4bb6d PZ |
5431 | no_ctx: |
5432 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
5433 | return 0; |
5434 | } | |
5435 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
5436 | ||
8b10c5e2 PZ |
5437 | /* |
5438 | * Called when the last reference to the file is gone. | |
5439 | */ | |
a6fa941d AV |
5440 | static int perf_release(struct inode *inode, struct file *file) |
5441 | { | |
c6e5b732 | 5442 | perf_event_release_kernel(file->private_data); |
a6fa941d | 5443 | return 0; |
fb0459d7 | 5444 | } |
fb0459d7 | 5445 | |
ca0dd44c | 5446 | static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 5447 | { |
cdd6c482 | 5448 | struct perf_event *child; |
e53c0994 PZ |
5449 | u64 total = 0; |
5450 | ||
59ed446f PZ |
5451 | *enabled = 0; |
5452 | *running = 0; | |
5453 | ||
6f10581a | 5454 | mutex_lock(&event->child_mutex); |
01add3ea | 5455 | |
7d88962e | 5456 | (void)perf_event_read(event, false); |
01add3ea SB |
5457 | total += perf_event_count(event); |
5458 | ||
59ed446f PZ |
5459 | *enabled += event->total_time_enabled + |
5460 | atomic64_read(&event->child_total_time_enabled); | |
5461 | *running += event->total_time_running + | |
5462 | atomic64_read(&event->child_total_time_running); | |
5463 | ||
5464 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 5465 | (void)perf_event_read(child, false); |
01add3ea | 5466 | total += perf_event_count(child); |
59ed446f PZ |
5467 | *enabled += child->total_time_enabled; |
5468 | *running += child->total_time_running; | |
5469 | } | |
6f10581a | 5470 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
5471 | |
5472 | return total; | |
5473 | } | |
ca0dd44c PZ |
5474 | |
5475 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) | |
5476 | { | |
5477 | struct perf_event_context *ctx; | |
5478 | u64 count; | |
5479 | ||
5480 | ctx = perf_event_ctx_lock(event); | |
5481 | count = __perf_event_read_value(event, enabled, running); | |
5482 | perf_event_ctx_unlock(event, ctx); | |
5483 | ||
5484 | return count; | |
5485 | } | |
fb0459d7 | 5486 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 5487 | |
7d88962e | 5488 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 5489 | u64 read_format, u64 *values) |
3dab77fb | 5490 | { |
2aeb1883 | 5491 | struct perf_event_context *ctx = leader->ctx; |
32671e37 | 5492 | struct perf_event *sub, *parent; |
2aeb1883 | 5493 | unsigned long flags; |
fa8c2693 | 5494 | int n = 1; /* skip @nr */ |
7d88962e | 5495 | int ret; |
f63a8daa | 5496 | |
7d88962e SB |
5497 | ret = perf_event_read(leader, true); |
5498 | if (ret) | |
5499 | return ret; | |
abf4868b | 5500 | |
a9cd8194 | 5501 | raw_spin_lock_irqsave(&ctx->lock, flags); |
32671e37 PZ |
5502 | /* |
5503 | * Verify the grouping between the parent and child (inherited) | |
5504 | * events is still in tact. | |
5505 | * | |
5506 | * Specifically: | |
5507 | * - leader->ctx->lock pins leader->sibling_list | |
5508 | * - parent->child_mutex pins parent->child_list | |
5509 | * - parent->ctx->mutex pins parent->sibling_list | |
5510 | * | |
5511 | * Because parent->ctx != leader->ctx (and child_list nests inside | |
5512 | * ctx->mutex), group destruction is not atomic between children, also | |
5513 | * see perf_event_release_kernel(). Additionally, parent can grow the | |
5514 | * group. | |
5515 | * | |
5516 | * Therefore it is possible to have parent and child groups in a | |
5517 | * different configuration and summing over such a beast makes no sense | |
5518 | * what so ever. | |
5519 | * | |
5520 | * Reject this. | |
5521 | */ | |
5522 | parent = leader->parent; | |
5523 | if (parent && | |
5524 | (parent->group_generation != leader->group_generation || | |
5525 | parent->nr_siblings != leader->nr_siblings)) { | |
5526 | ret = -ECHILD; | |
5527 | goto unlock; | |
5528 | } | |
a9cd8194 | 5529 | |
fa8c2693 PZ |
5530 | /* |
5531 | * Since we co-schedule groups, {enabled,running} times of siblings | |
5532 | * will be identical to those of the leader, so we only publish one | |
5533 | * set. | |
5534 | */ | |
5535 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
5536 | values[n++] += leader->total_time_enabled + | |
5537 | atomic64_read(&leader->child_total_time_enabled); | |
5538 | } | |
3dab77fb | 5539 | |
fa8c2693 PZ |
5540 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
5541 | values[n++] += leader->total_time_running + | |
5542 | atomic64_read(&leader->child_total_time_running); | |
5543 | } | |
5544 | ||
5545 | /* | |
5546 | * Write {count,id} tuples for every sibling. | |
5547 | */ | |
5548 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
5549 | if (read_format & PERF_FORMAT_ID) |
5550 | values[n++] = primary_event_id(leader); | |
119a784c NK |
5551 | if (read_format & PERF_FORMAT_LOST) |
5552 | values[n++] = atomic64_read(&leader->lost_samples); | |
3dab77fb | 5553 | |
edb39592 | 5554 | for_each_sibling_event(sub, leader) { |
fa8c2693 PZ |
5555 | values[n++] += perf_event_count(sub); |
5556 | if (read_format & PERF_FORMAT_ID) | |
5557 | values[n++] = primary_event_id(sub); | |
119a784c NK |
5558 | if (read_format & PERF_FORMAT_LOST) |
5559 | values[n++] = atomic64_read(&sub->lost_samples); | |
fa8c2693 | 5560 | } |
7d88962e | 5561 | |
32671e37 | 5562 | unlock: |
2aeb1883 | 5563 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
32671e37 | 5564 | return ret; |
fa8c2693 | 5565 | } |
3dab77fb | 5566 | |
fa8c2693 PZ |
5567 | static int perf_read_group(struct perf_event *event, |
5568 | u64 read_format, char __user *buf) | |
5569 | { | |
5570 | struct perf_event *leader = event->group_leader, *child; | |
5571 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 5572 | int ret; |
fa8c2693 | 5573 | u64 *values; |
3dab77fb | 5574 | |
fa8c2693 | 5575 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 5576 | |
fa8c2693 PZ |
5577 | values = kzalloc(event->read_size, GFP_KERNEL); |
5578 | if (!values) | |
5579 | return -ENOMEM; | |
3dab77fb | 5580 | |
fa8c2693 PZ |
5581 | values[0] = 1 + leader->nr_siblings; |
5582 | ||
fa8c2693 | 5583 | mutex_lock(&leader->child_mutex); |
abf4868b | 5584 | |
7d88962e SB |
5585 | ret = __perf_read_group_add(leader, read_format, values); |
5586 | if (ret) | |
5587 | goto unlock; | |
5588 | ||
5589 | list_for_each_entry(child, &leader->child_list, child_list) { | |
5590 | ret = __perf_read_group_add(child, read_format, values); | |
5591 | if (ret) | |
5592 | goto unlock; | |
5593 | } | |
abf4868b | 5594 | |
fa8c2693 | 5595 | mutex_unlock(&leader->child_mutex); |
abf4868b | 5596 | |
7d88962e | 5597 | ret = event->read_size; |
fa8c2693 PZ |
5598 | if (copy_to_user(buf, values, event->read_size)) |
5599 | ret = -EFAULT; | |
7d88962e | 5600 | goto out; |
fa8c2693 | 5601 | |
7d88962e SB |
5602 | unlock: |
5603 | mutex_unlock(&leader->child_mutex); | |
5604 | out: | |
fa8c2693 | 5605 | kfree(values); |
abf4868b | 5606 | return ret; |
3dab77fb PZ |
5607 | } |
5608 | ||
b15f495b | 5609 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
5610 | u64 read_format, char __user *buf) |
5611 | { | |
59ed446f | 5612 | u64 enabled, running; |
119a784c | 5613 | u64 values[5]; |
3dab77fb PZ |
5614 | int n = 0; |
5615 | ||
ca0dd44c | 5616 | values[n++] = __perf_event_read_value(event, &enabled, &running); |
59ed446f PZ |
5617 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
5618 | values[n++] = enabled; | |
5619 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5620 | values[n++] = running; | |
3dab77fb | 5621 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5622 | values[n++] = primary_event_id(event); |
119a784c NK |
5623 | if (read_format & PERF_FORMAT_LOST) |
5624 | values[n++] = atomic64_read(&event->lost_samples); | |
3dab77fb PZ |
5625 | |
5626 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
5627 | return -EFAULT; | |
5628 | ||
5629 | return n * sizeof(u64); | |
5630 | } | |
5631 | ||
dc633982 JO |
5632 | static bool is_event_hup(struct perf_event *event) |
5633 | { | |
5634 | bool no_children; | |
5635 | ||
a69b0ca4 | 5636 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
5637 | return false; |
5638 | ||
5639 | mutex_lock(&event->child_mutex); | |
5640 | no_children = list_empty(&event->child_list); | |
5641 | mutex_unlock(&event->child_mutex); | |
5642 | return no_children; | |
5643 | } | |
5644 | ||
0793a61d | 5645 | /* |
cdd6c482 | 5646 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
5647 | */ |
5648 | static ssize_t | |
b15f495b | 5649 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 5650 | { |
cdd6c482 | 5651 | u64 read_format = event->attr.read_format; |
3dab77fb | 5652 | int ret; |
0793a61d | 5653 | |
3b6f9e5c | 5654 | /* |
788faab7 | 5655 | * Return end-of-file for a read on an event that is in |
3b6f9e5c PM |
5656 | * error state (i.e. because it was pinned but it couldn't be |
5657 | * scheduled on to the CPU at some point). | |
5658 | */ | |
cdd6c482 | 5659 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
5660 | return 0; |
5661 | ||
c320c7b7 | 5662 | if (count < event->read_size) |
3dab77fb PZ |
5663 | return -ENOSPC; |
5664 | ||
cdd6c482 | 5665 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 5666 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 5667 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 5668 | else |
b15f495b | 5669 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 5670 | |
3dab77fb | 5671 | return ret; |
0793a61d TG |
5672 | } |
5673 | ||
0793a61d TG |
5674 | static ssize_t |
5675 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
5676 | { | |
cdd6c482 | 5677 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
5678 | struct perf_event_context *ctx; |
5679 | int ret; | |
0793a61d | 5680 | |
da97e184 JFG |
5681 | ret = security_perf_event_read(event); |
5682 | if (ret) | |
5683 | return ret; | |
5684 | ||
f63a8daa | 5685 | ctx = perf_event_ctx_lock(event); |
b15f495b | 5686 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
5687 | perf_event_ctx_unlock(event, ctx); |
5688 | ||
5689 | return ret; | |
0793a61d TG |
5690 | } |
5691 | ||
9dd95748 | 5692 | static __poll_t perf_poll(struct file *file, poll_table *wait) |
0793a61d | 5693 | { |
cdd6c482 | 5694 | struct perf_event *event = file->private_data; |
56de4e8f | 5695 | struct perf_buffer *rb; |
a9a08845 | 5696 | __poll_t events = EPOLLHUP; |
c7138f37 | 5697 | |
e708d7ad | 5698 | poll_wait(file, &event->waitq, wait); |
179033b3 | 5699 | |
dc633982 | 5700 | if (is_event_hup(event)) |
179033b3 | 5701 | return events; |
c7138f37 | 5702 | |
10c6db11 | 5703 | /* |
9bb5d40c PZ |
5704 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
5705 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
5706 | */ |
5707 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
5708 | rb = event->rb; |
5709 | if (rb) | |
76369139 | 5710 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 5711 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
5712 | return events; |
5713 | } | |
5714 | ||
f63a8daa | 5715 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 5716 | { |
7d88962e | 5717 | (void)perf_event_read(event, false); |
e7850595 | 5718 | local64_set(&event->count, 0); |
cdd6c482 | 5719 | perf_event_update_userpage(event); |
3df5edad PZ |
5720 | } |
5721 | ||
52ba4b0b LX |
5722 | /* Assume it's not an event with inherit set. */ |
5723 | u64 perf_event_pause(struct perf_event *event, bool reset) | |
5724 | { | |
5725 | struct perf_event_context *ctx; | |
5726 | u64 count; | |
5727 | ||
5728 | ctx = perf_event_ctx_lock(event); | |
5729 | WARN_ON_ONCE(event->attr.inherit); | |
5730 | _perf_event_disable(event); | |
5731 | count = local64_read(&event->count); | |
5732 | if (reset) | |
5733 | local64_set(&event->count, 0); | |
5734 | perf_event_ctx_unlock(event, ctx); | |
5735 | ||
5736 | return count; | |
5737 | } | |
5738 | EXPORT_SYMBOL_GPL(perf_event_pause); | |
5739 | ||
c93f7669 | 5740 | /* |
cdd6c482 IM |
5741 | * Holding the top-level event's child_mutex means that any |
5742 | * descendant process that has inherited this event will block | |
8ba289b8 | 5743 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 5744 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 5745 | */ |
cdd6c482 IM |
5746 | static void perf_event_for_each_child(struct perf_event *event, |
5747 | void (*func)(struct perf_event *)) | |
3df5edad | 5748 | { |
cdd6c482 | 5749 | struct perf_event *child; |
3df5edad | 5750 | |
cdd6c482 | 5751 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 5752 | |
cdd6c482 IM |
5753 | mutex_lock(&event->child_mutex); |
5754 | func(event); | |
5755 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 5756 | func(child); |
cdd6c482 | 5757 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
5758 | } |
5759 | ||
cdd6c482 IM |
5760 | static void perf_event_for_each(struct perf_event *event, |
5761 | void (*func)(struct perf_event *)) | |
3df5edad | 5762 | { |
cdd6c482 IM |
5763 | struct perf_event_context *ctx = event->ctx; |
5764 | struct perf_event *sibling; | |
3df5edad | 5765 | |
f63a8daa PZ |
5766 | lockdep_assert_held(&ctx->mutex); |
5767 | ||
cdd6c482 | 5768 | event = event->group_leader; |
75f937f2 | 5769 | |
cdd6c482 | 5770 | perf_event_for_each_child(event, func); |
edb39592 | 5771 | for_each_sibling_event(sibling, event) |
724b6daa | 5772 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
5773 | } |
5774 | ||
fae3fde6 PZ |
5775 | static void __perf_event_period(struct perf_event *event, |
5776 | struct perf_cpu_context *cpuctx, | |
5777 | struct perf_event_context *ctx, | |
5778 | void *info) | |
c7999c6f | 5779 | { |
fae3fde6 | 5780 | u64 value = *((u64 *)info); |
c7999c6f | 5781 | bool active; |
08247e31 | 5782 | |
cdd6c482 | 5783 | if (event->attr.freq) { |
cdd6c482 | 5784 | event->attr.sample_freq = value; |
08247e31 | 5785 | } else { |
cdd6c482 IM |
5786 | event->attr.sample_period = value; |
5787 | event->hw.sample_period = value; | |
08247e31 | 5788 | } |
bad7192b PZ |
5789 | |
5790 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
5791 | if (active) { | |
bd275681 | 5792 | perf_pmu_disable(event->pmu); |
1e02cd40 PZ |
5793 | /* |
5794 | * We could be throttled; unthrottle now to avoid the tick | |
5795 | * trying to unthrottle while we already re-started the event. | |
5796 | */ | |
5797 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
5798 | event->hw.interrupts = 0; | |
5799 | perf_log_throttle(event, 1); | |
5800 | } | |
bad7192b PZ |
5801 | event->pmu->stop(event, PERF_EF_UPDATE); |
5802 | } | |
5803 | ||
5804 | local64_set(&event->hw.period_left, 0); | |
5805 | ||
5806 | if (active) { | |
5807 | event->pmu->start(event, PERF_EF_RELOAD); | |
bd275681 | 5808 | perf_pmu_enable(event->pmu); |
bad7192b | 5809 | } |
c7999c6f PZ |
5810 | } |
5811 | ||
81ec3f3c JO |
5812 | static int perf_event_check_period(struct perf_event *event, u64 value) |
5813 | { | |
5814 | return event->pmu->check_period(event, value); | |
5815 | } | |
5816 | ||
3ca270fc | 5817 | static int _perf_event_period(struct perf_event *event, u64 value) |
c7999c6f | 5818 | { |
c7999c6f PZ |
5819 | if (!is_sampling_event(event)) |
5820 | return -EINVAL; | |
5821 | ||
c7999c6f PZ |
5822 | if (!value) |
5823 | return -EINVAL; | |
5824 | ||
5825 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
5826 | return -EINVAL; | |
5827 | ||
81ec3f3c JO |
5828 | if (perf_event_check_period(event, value)) |
5829 | return -EINVAL; | |
5830 | ||
913a90bc RB |
5831 | if (!event->attr.freq && (value & (1ULL << 63))) |
5832 | return -EINVAL; | |
5833 | ||
fae3fde6 | 5834 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 5835 | |
c7999c6f | 5836 | return 0; |
08247e31 PZ |
5837 | } |
5838 | ||
3ca270fc LX |
5839 | int perf_event_period(struct perf_event *event, u64 value) |
5840 | { | |
5841 | struct perf_event_context *ctx; | |
5842 | int ret; | |
5843 | ||
5844 | ctx = perf_event_ctx_lock(event); | |
5845 | ret = _perf_event_period(event, value); | |
5846 | perf_event_ctx_unlock(event, ctx); | |
5847 | ||
5848 | return ret; | |
5849 | } | |
5850 | EXPORT_SYMBOL_GPL(perf_event_period); | |
5851 | ||
ac9721f3 PZ |
5852 | static const struct file_operations perf_fops; |
5853 | ||
2903ff01 | 5854 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 5855 | { |
2903ff01 AV |
5856 | struct fd f = fdget(fd); |
5857 | if (!f.file) | |
5858 | return -EBADF; | |
ac9721f3 | 5859 | |
2903ff01 AV |
5860 | if (f.file->f_op != &perf_fops) { |
5861 | fdput(f); | |
5862 | return -EBADF; | |
ac9721f3 | 5863 | } |
2903ff01 AV |
5864 | *p = f; |
5865 | return 0; | |
ac9721f3 PZ |
5866 | } |
5867 | ||
5868 | static int perf_event_set_output(struct perf_event *event, | |
5869 | struct perf_event *output_event); | |
6fb2915d | 5870 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
32ff77e8 MC |
5871 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
5872 | struct perf_event_attr *attr); | |
a4be7c27 | 5873 | |
f63a8daa | 5874 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 5875 | { |
cdd6c482 | 5876 | void (*func)(struct perf_event *); |
3df5edad | 5877 | u32 flags = arg; |
d859e29f PM |
5878 | |
5879 | switch (cmd) { | |
cdd6c482 | 5880 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 5881 | func = _perf_event_enable; |
d859e29f | 5882 | break; |
cdd6c482 | 5883 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 5884 | func = _perf_event_disable; |
79f14641 | 5885 | break; |
cdd6c482 | 5886 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 5887 | func = _perf_event_reset; |
6de6a7b9 | 5888 | break; |
3df5edad | 5889 | |
cdd6c482 | 5890 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 5891 | return _perf_event_refresh(event, arg); |
08247e31 | 5892 | |
cdd6c482 | 5893 | case PERF_EVENT_IOC_PERIOD: |
3ca270fc LX |
5894 | { |
5895 | u64 value; | |
08247e31 | 5896 | |
3ca270fc LX |
5897 | if (copy_from_user(&value, (u64 __user *)arg, sizeof(value))) |
5898 | return -EFAULT; | |
08247e31 | 5899 | |
3ca270fc LX |
5900 | return _perf_event_period(event, value); |
5901 | } | |
cf4957f1 JO |
5902 | case PERF_EVENT_IOC_ID: |
5903 | { | |
5904 | u64 id = primary_event_id(event); | |
5905 | ||
5906 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
5907 | return -EFAULT; | |
5908 | return 0; | |
5909 | } | |
5910 | ||
cdd6c482 | 5911 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 5912 | { |
ac9721f3 | 5913 | int ret; |
ac9721f3 | 5914 | if (arg != -1) { |
2903ff01 AV |
5915 | struct perf_event *output_event; |
5916 | struct fd output; | |
5917 | ret = perf_fget_light(arg, &output); | |
5918 | if (ret) | |
5919 | return ret; | |
5920 | output_event = output.file->private_data; | |
5921 | ret = perf_event_set_output(event, output_event); | |
5922 | fdput(output); | |
5923 | } else { | |
5924 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 5925 | } |
ac9721f3 PZ |
5926 | return ret; |
5927 | } | |
a4be7c27 | 5928 | |
6fb2915d LZ |
5929 | case PERF_EVENT_IOC_SET_FILTER: |
5930 | return perf_event_set_filter(event, (void __user *)arg); | |
5931 | ||
2541517c | 5932 | case PERF_EVENT_IOC_SET_BPF: |
652c1b17 AN |
5933 | { |
5934 | struct bpf_prog *prog; | |
5935 | int err; | |
5936 | ||
5937 | prog = bpf_prog_get(arg); | |
5938 | if (IS_ERR(prog)) | |
5939 | return PTR_ERR(prog); | |
5940 | ||
82e6b1ee | 5941 | err = perf_event_set_bpf_prog(event, prog, 0); |
652c1b17 AN |
5942 | if (err) { |
5943 | bpf_prog_put(prog); | |
5944 | return err; | |
5945 | } | |
5946 | ||
5947 | return 0; | |
5948 | } | |
2541517c | 5949 | |
86e7972f | 5950 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
56de4e8f | 5951 | struct perf_buffer *rb; |
86e7972f WN |
5952 | |
5953 | rcu_read_lock(); | |
5954 | rb = rcu_dereference(event->rb); | |
5955 | if (!rb || !rb->nr_pages) { | |
5956 | rcu_read_unlock(); | |
5957 | return -EINVAL; | |
5958 | } | |
5959 | rb_toggle_paused(rb, !!arg); | |
5960 | rcu_read_unlock(); | |
5961 | return 0; | |
5962 | } | |
f371b304 YS |
5963 | |
5964 | case PERF_EVENT_IOC_QUERY_BPF: | |
f4e2298e | 5965 | return perf_event_query_prog_array(event, (void __user *)arg); |
32ff77e8 MC |
5966 | |
5967 | case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: { | |
5968 | struct perf_event_attr new_attr; | |
5969 | int err = perf_copy_attr((struct perf_event_attr __user *)arg, | |
5970 | &new_attr); | |
5971 | ||
5972 | if (err) | |
5973 | return err; | |
5974 | ||
5975 | return perf_event_modify_attr(event, &new_attr); | |
5976 | } | |
d859e29f | 5977 | default: |
3df5edad | 5978 | return -ENOTTY; |
d859e29f | 5979 | } |
3df5edad PZ |
5980 | |
5981 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 5982 | perf_event_for_each(event, func); |
3df5edad | 5983 | else |
cdd6c482 | 5984 | perf_event_for_each_child(event, func); |
3df5edad PZ |
5985 | |
5986 | return 0; | |
d859e29f PM |
5987 | } |
5988 | ||
f63a8daa PZ |
5989 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
5990 | { | |
5991 | struct perf_event *event = file->private_data; | |
5992 | struct perf_event_context *ctx; | |
5993 | long ret; | |
5994 | ||
da97e184 JFG |
5995 | /* Treat ioctl like writes as it is likely a mutating operation. */ |
5996 | ret = security_perf_event_write(event); | |
5997 | if (ret) | |
5998 | return ret; | |
5999 | ||
f63a8daa PZ |
6000 | ctx = perf_event_ctx_lock(event); |
6001 | ret = _perf_ioctl(event, cmd, arg); | |
6002 | perf_event_ctx_unlock(event, ctx); | |
6003 | ||
6004 | return ret; | |
6005 | } | |
6006 | ||
b3f20785 PM |
6007 | #ifdef CONFIG_COMPAT |
6008 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
6009 | unsigned long arg) | |
6010 | { | |
6011 | switch (_IOC_NR(cmd)) { | |
6012 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
6013 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
82489c5f ES |
6014 | case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF): |
6015 | case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES): | |
b3f20785 PM |
6016 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
6017 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
6018 | cmd &= ~IOCSIZE_MASK; | |
6019 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
6020 | } | |
6021 | break; | |
6022 | } | |
6023 | return perf_ioctl(file, cmd, arg); | |
6024 | } | |
6025 | #else | |
6026 | # define perf_compat_ioctl NULL | |
6027 | #endif | |
6028 | ||
cdd6c482 | 6029 | int perf_event_task_enable(void) |
771d7cde | 6030 | { |
f63a8daa | 6031 | struct perf_event_context *ctx; |
cdd6c482 | 6032 | struct perf_event *event; |
771d7cde | 6033 | |
cdd6c482 | 6034 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
6035 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
6036 | ctx = perf_event_ctx_lock(event); | |
6037 | perf_event_for_each_child(event, _perf_event_enable); | |
6038 | perf_event_ctx_unlock(event, ctx); | |
6039 | } | |
cdd6c482 | 6040 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
6041 | |
6042 | return 0; | |
6043 | } | |
6044 | ||
cdd6c482 | 6045 | int perf_event_task_disable(void) |
771d7cde | 6046 | { |
f63a8daa | 6047 | struct perf_event_context *ctx; |
cdd6c482 | 6048 | struct perf_event *event; |
771d7cde | 6049 | |
cdd6c482 | 6050 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
6051 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
6052 | ctx = perf_event_ctx_lock(event); | |
6053 | perf_event_for_each_child(event, _perf_event_disable); | |
6054 | perf_event_ctx_unlock(event, ctx); | |
6055 | } | |
cdd6c482 | 6056 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
6057 | |
6058 | return 0; | |
6059 | } | |
6060 | ||
cdd6c482 | 6061 | static int perf_event_index(struct perf_event *event) |
194002b2 | 6062 | { |
a4eaf7f1 PZ |
6063 | if (event->hw.state & PERF_HES_STOPPED) |
6064 | return 0; | |
6065 | ||
cdd6c482 | 6066 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
6067 | return 0; |
6068 | ||
35edc2a5 | 6069 | return event->pmu->event_idx(event); |
194002b2 PZ |
6070 | } |
6071 | ||
fa731587 PZ |
6072 | static void perf_event_init_userpage(struct perf_event *event) |
6073 | { | |
6074 | struct perf_event_mmap_page *userpg; | |
56de4e8f | 6075 | struct perf_buffer *rb; |
fa731587 PZ |
6076 | |
6077 | rcu_read_lock(); | |
6078 | rb = rcu_dereference(event->rb); | |
6079 | if (!rb) | |
6080 | goto unlock; | |
6081 | ||
6082 | userpg = rb->user_page; | |
6083 | ||
6084 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
6085 | userpg->cap_bit0_is_deprecated = 1; | |
6086 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
6087 | userpg->data_offset = PAGE_SIZE; |
6088 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
6089 | |
6090 | unlock: | |
6091 | rcu_read_unlock(); | |
6092 | } | |
6093 | ||
c1317ec2 AL |
6094 | void __weak arch_perf_update_userpage( |
6095 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
6096 | { |
6097 | } | |
6098 | ||
38ff667b PZ |
6099 | /* |
6100 | * Callers need to ensure there can be no nesting of this function, otherwise | |
6101 | * the seqlock logic goes bad. We can not serialize this because the arch | |
6102 | * code calls this from NMI context. | |
6103 | */ | |
cdd6c482 | 6104 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 6105 | { |
cdd6c482 | 6106 | struct perf_event_mmap_page *userpg; |
56de4e8f | 6107 | struct perf_buffer *rb; |
e3f3541c | 6108 | u64 enabled, running, now; |
38ff667b PZ |
6109 | |
6110 | rcu_read_lock(); | |
5ec4c599 PZ |
6111 | rb = rcu_dereference(event->rb); |
6112 | if (!rb) | |
6113 | goto unlock; | |
6114 | ||
0d641208 EM |
6115 | /* |
6116 | * compute total_time_enabled, total_time_running | |
6117 | * based on snapshot values taken when the event | |
6118 | * was last scheduled in. | |
6119 | * | |
6120 | * we cannot simply called update_context_time() | |
6121 | * because of locking issue as we can be called in | |
6122 | * NMI context | |
6123 | */ | |
e3f3541c | 6124 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 6125 | |
76369139 | 6126 | userpg = rb->user_page; |
7b732a75 | 6127 | /* |
9d2dcc8f MF |
6128 | * Disable preemption to guarantee consistent time stamps are stored to |
6129 | * the user page. | |
7b732a75 PZ |
6130 | */ |
6131 | preempt_disable(); | |
37d81828 | 6132 | ++userpg->lock; |
92f22a38 | 6133 | barrier(); |
cdd6c482 | 6134 | userpg->index = perf_event_index(event); |
b5e58793 | 6135 | userpg->offset = perf_event_count(event); |
365a4038 | 6136 | if (userpg->index) |
e7850595 | 6137 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 6138 | |
0d641208 | 6139 | userpg->time_enabled = enabled + |
cdd6c482 | 6140 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 6141 | |
0d641208 | 6142 | userpg->time_running = running + |
cdd6c482 | 6143 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 6144 | |
c1317ec2 | 6145 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 6146 | |
92f22a38 | 6147 | barrier(); |
37d81828 | 6148 | ++userpg->lock; |
7b732a75 | 6149 | preempt_enable(); |
38ff667b | 6150 | unlock: |
7b732a75 | 6151 | rcu_read_unlock(); |
37d81828 | 6152 | } |
82975c46 | 6153 | EXPORT_SYMBOL_GPL(perf_event_update_userpage); |
37d81828 | 6154 | |
9e3ed2d7 | 6155 | static vm_fault_t perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 6156 | { |
11bac800 | 6157 | struct perf_event *event = vmf->vma->vm_file->private_data; |
56de4e8f | 6158 | struct perf_buffer *rb; |
9e3ed2d7 | 6159 | vm_fault_t ret = VM_FAULT_SIGBUS; |
906010b2 PZ |
6160 | |
6161 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
6162 | if (vmf->pgoff == 0) | |
6163 | ret = 0; | |
6164 | return ret; | |
6165 | } | |
6166 | ||
6167 | rcu_read_lock(); | |
76369139 FW |
6168 | rb = rcu_dereference(event->rb); |
6169 | if (!rb) | |
906010b2 PZ |
6170 | goto unlock; |
6171 | ||
6172 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
6173 | goto unlock; | |
6174 | ||
76369139 | 6175 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
6176 | if (!vmf->page) |
6177 | goto unlock; | |
6178 | ||
6179 | get_page(vmf->page); | |
11bac800 | 6180 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
6181 | vmf->page->index = vmf->pgoff; |
6182 | ||
6183 | ret = 0; | |
6184 | unlock: | |
6185 | rcu_read_unlock(); | |
6186 | ||
6187 | return ret; | |
6188 | } | |
6189 | ||
10c6db11 | 6190 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 6191 | struct perf_buffer *rb) |
10c6db11 | 6192 | { |
56de4e8f | 6193 | struct perf_buffer *old_rb = NULL; |
10c6db11 PZ |
6194 | unsigned long flags; |
6195 | ||
961c3912 JC |
6196 | WARN_ON_ONCE(event->parent); |
6197 | ||
b69cf536 PZ |
6198 | if (event->rb) { |
6199 | /* | |
6200 | * Should be impossible, we set this when removing | |
6201 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
6202 | */ | |
6203 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 6204 | |
b69cf536 | 6205 | old_rb = event->rb; |
b69cf536 PZ |
6206 | spin_lock_irqsave(&old_rb->event_lock, flags); |
6207 | list_del_rcu(&event->rb_entry); | |
6208 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 6209 | |
2f993cf0 ON |
6210 | event->rcu_batches = get_state_synchronize_rcu(); |
6211 | event->rcu_pending = 1; | |
b69cf536 | 6212 | } |
10c6db11 | 6213 | |
b69cf536 | 6214 | if (rb) { |
2f993cf0 ON |
6215 | if (event->rcu_pending) { |
6216 | cond_synchronize_rcu(event->rcu_batches); | |
6217 | event->rcu_pending = 0; | |
6218 | } | |
6219 | ||
b69cf536 PZ |
6220 | spin_lock_irqsave(&rb->event_lock, flags); |
6221 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
6222 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
6223 | } | |
6224 | ||
767ae086 AS |
6225 | /* |
6226 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
6227 | * before swizzling the event::rb pointer; if it's getting | |
6228 | * unmapped, its aux_mmap_count will be 0 and it won't | |
6229 | * restart. See the comment in __perf_pmu_output_stop(). | |
6230 | * | |
6231 | * Data will inevitably be lost when set_output is done in | |
6232 | * mid-air, but then again, whoever does it like this is | |
6233 | * not in for the data anyway. | |
6234 | */ | |
6235 | if (has_aux(event)) | |
6236 | perf_event_stop(event, 0); | |
6237 | ||
b69cf536 PZ |
6238 | rcu_assign_pointer(event->rb, rb); |
6239 | ||
6240 | if (old_rb) { | |
6241 | ring_buffer_put(old_rb); | |
6242 | /* | |
6243 | * Since we detached before setting the new rb, so that we | |
6244 | * could attach the new rb, we could have missed a wakeup. | |
6245 | * Provide it now. | |
6246 | */ | |
6247 | wake_up_all(&event->waitq); | |
6248 | } | |
10c6db11 PZ |
6249 | } |
6250 | ||
6251 | static void ring_buffer_wakeup(struct perf_event *event) | |
6252 | { | |
56de4e8f | 6253 | struct perf_buffer *rb; |
10c6db11 | 6254 | |
961c3912 JC |
6255 | if (event->parent) |
6256 | event = event->parent; | |
6257 | ||
10c6db11 PZ |
6258 | rcu_read_lock(); |
6259 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
6260 | if (rb) { |
6261 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
6262 | wake_up_all(&event->waitq); | |
6263 | } | |
10c6db11 PZ |
6264 | rcu_read_unlock(); |
6265 | } | |
6266 | ||
56de4e8f | 6267 | struct perf_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 6268 | { |
56de4e8f | 6269 | struct perf_buffer *rb; |
7b732a75 | 6270 | |
961c3912 JC |
6271 | if (event->parent) |
6272 | event = event->parent; | |
6273 | ||
ac9721f3 | 6274 | rcu_read_lock(); |
76369139 FW |
6275 | rb = rcu_dereference(event->rb); |
6276 | if (rb) { | |
fecb8ed2 | 6277 | if (!refcount_inc_not_zero(&rb->refcount)) |
76369139 | 6278 | rb = NULL; |
ac9721f3 PZ |
6279 | } |
6280 | rcu_read_unlock(); | |
6281 | ||
76369139 | 6282 | return rb; |
ac9721f3 PZ |
6283 | } |
6284 | ||
56de4e8f | 6285 | void ring_buffer_put(struct perf_buffer *rb) |
ac9721f3 | 6286 | { |
fecb8ed2 | 6287 | if (!refcount_dec_and_test(&rb->refcount)) |
ac9721f3 | 6288 | return; |
7b732a75 | 6289 | |
9bb5d40c | 6290 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 6291 | |
76369139 | 6292 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
6293 | } |
6294 | ||
6295 | static void perf_mmap_open(struct vm_area_struct *vma) | |
6296 | { | |
cdd6c482 | 6297 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 6298 | |
cdd6c482 | 6299 | atomic_inc(&event->mmap_count); |
9bb5d40c | 6300 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 6301 | |
45bfb2e5 PZ |
6302 | if (vma->vm_pgoff) |
6303 | atomic_inc(&event->rb->aux_mmap_count); | |
6304 | ||
1e0fb9ec | 6305 | if (event->pmu->event_mapped) |
bfe33492 | 6306 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
6307 | } |
6308 | ||
95ff4ca2 AS |
6309 | static void perf_pmu_output_stop(struct perf_event *event); |
6310 | ||
9bb5d40c PZ |
6311 | /* |
6312 | * A buffer can be mmap()ed multiple times; either directly through the same | |
6313 | * event, or through other events by use of perf_event_set_output(). | |
6314 | * | |
6315 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
6316 | * the buffer here, where we still have a VM context. This means we need | |
6317 | * to detach all events redirecting to us. | |
6318 | */ | |
7b732a75 PZ |
6319 | static void perf_mmap_close(struct vm_area_struct *vma) |
6320 | { | |
cdd6c482 | 6321 | struct perf_event *event = vma->vm_file->private_data; |
56de4e8f | 6322 | struct perf_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
6323 | struct user_struct *mmap_user = rb->mmap_user; |
6324 | int mmap_locked = rb->mmap_locked; | |
6325 | unsigned long size = perf_data_size(rb); | |
f91072ed | 6326 | bool detach_rest = false; |
789f90fc | 6327 | |
1e0fb9ec | 6328 | if (event->pmu->event_unmapped) |
bfe33492 | 6329 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 6330 | |
45bfb2e5 PZ |
6331 | /* |
6332 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
6333 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
6334 | * serialize with perf_mmap here. | |
6335 | */ | |
6336 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
6337 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
6338 | /* |
6339 | * Stop all AUX events that are writing to this buffer, | |
6340 | * so that we can free its AUX pages and corresponding PMU | |
6341 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
6342 | * they won't start any more (see perf_aux_output_begin()). | |
6343 | */ | |
6344 | perf_pmu_output_stop(event); | |
6345 | ||
6346 | /* now it's safe to free the pages */ | |
36b3db03 AS |
6347 | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); |
6348 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
45bfb2e5 | 6349 | |
95ff4ca2 | 6350 | /* this has to be the last one */ |
45bfb2e5 | 6351 | rb_free_aux(rb); |
ca3bb3d0 | 6352 | WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); |
95ff4ca2 | 6353 | |
45bfb2e5 PZ |
6354 | mutex_unlock(&event->mmap_mutex); |
6355 | } | |
6356 | ||
f91072ed JO |
6357 | if (atomic_dec_and_test(&rb->mmap_count)) |
6358 | detach_rest = true; | |
9bb5d40c PZ |
6359 | |
6360 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 6361 | goto out_put; |
9bb5d40c | 6362 | |
b69cf536 | 6363 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
6364 | mutex_unlock(&event->mmap_mutex); |
6365 | ||
6366 | /* If there's still other mmap()s of this buffer, we're done. */ | |
f91072ed | 6367 | if (!detach_rest) |
b69cf536 | 6368 | goto out_put; |
ac9721f3 | 6369 | |
9bb5d40c PZ |
6370 | /* |
6371 | * No other mmap()s, detach from all other events that might redirect | |
6372 | * into the now unreachable buffer. Somewhat complicated by the | |
6373 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
6374 | */ | |
6375 | again: | |
6376 | rcu_read_lock(); | |
6377 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
6378 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
6379 | /* | |
6380 | * This event is en-route to free_event() which will | |
6381 | * detach it and remove it from the list. | |
6382 | */ | |
6383 | continue; | |
6384 | } | |
6385 | rcu_read_unlock(); | |
789f90fc | 6386 | |
9bb5d40c PZ |
6387 | mutex_lock(&event->mmap_mutex); |
6388 | /* | |
6389 | * Check we didn't race with perf_event_set_output() which can | |
6390 | * swizzle the rb from under us while we were waiting to | |
6391 | * acquire mmap_mutex. | |
6392 | * | |
6393 | * If we find a different rb; ignore this event, a next | |
6394 | * iteration will no longer find it on the list. We have to | |
6395 | * still restart the iteration to make sure we're not now | |
6396 | * iterating the wrong list. | |
6397 | */ | |
b69cf536 PZ |
6398 | if (event->rb == rb) |
6399 | ring_buffer_attach(event, NULL); | |
6400 | ||
cdd6c482 | 6401 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 6402 | put_event(event); |
ac9721f3 | 6403 | |
9bb5d40c PZ |
6404 | /* |
6405 | * Restart the iteration; either we're on the wrong list or | |
6406 | * destroyed its integrity by doing a deletion. | |
6407 | */ | |
6408 | goto again; | |
7b732a75 | 6409 | } |
9bb5d40c PZ |
6410 | rcu_read_unlock(); |
6411 | ||
6412 | /* | |
6413 | * It could be there's still a few 0-ref events on the list; they'll | |
6414 | * get cleaned up by free_event() -- they'll also still have their | |
6415 | * ref on the rb and will free it whenever they are done with it. | |
6416 | * | |
6417 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
6418 | * undo the VM accounting. | |
6419 | */ | |
6420 | ||
d44248a4 SL |
6421 | atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked, |
6422 | &mmap_user->locked_vm); | |
70f8a3ca | 6423 | atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm); |
9bb5d40c PZ |
6424 | free_uid(mmap_user); |
6425 | ||
b69cf536 | 6426 | out_put: |
9bb5d40c | 6427 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
6428 | } |
6429 | ||
f0f37e2f | 6430 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 6431 | .open = perf_mmap_open, |
fca0c116 | 6432 | .close = perf_mmap_close, /* non mergeable */ |
43a21ea8 PZ |
6433 | .fault = perf_mmap_fault, |
6434 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
6435 | }; |
6436 | ||
6437 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
6438 | { | |
cdd6c482 | 6439 | struct perf_event *event = file->private_data; |
22a4f650 | 6440 | unsigned long user_locked, user_lock_limit; |
789f90fc | 6441 | struct user_struct *user = current_user(); |
56de4e8f | 6442 | struct perf_buffer *rb = NULL; |
22a4f650 | 6443 | unsigned long locked, lock_limit; |
7b732a75 PZ |
6444 | unsigned long vma_size; |
6445 | unsigned long nr_pages; | |
45bfb2e5 | 6446 | long user_extra = 0, extra = 0; |
d57e34fd | 6447 | int ret = 0, flags = 0; |
37d81828 | 6448 | |
c7920614 PZ |
6449 | /* |
6450 | * Don't allow mmap() of inherited per-task counters. This would | |
6451 | * create a performance issue due to all children writing to the | |
76369139 | 6452 | * same rb. |
c7920614 PZ |
6453 | */ |
6454 | if (event->cpu == -1 && event->attr.inherit) | |
6455 | return -EINVAL; | |
6456 | ||
43a21ea8 | 6457 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 6458 | return -EINVAL; |
7b732a75 | 6459 | |
da97e184 JFG |
6460 | ret = security_perf_event_read(event); |
6461 | if (ret) | |
6462 | return ret; | |
6463 | ||
7b732a75 | 6464 | vma_size = vma->vm_end - vma->vm_start; |
45bfb2e5 PZ |
6465 | |
6466 | if (vma->vm_pgoff == 0) { | |
6467 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
6468 | } else { | |
6469 | /* | |
6470 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
6471 | * mapped, all subsequent mappings should have the same size | |
6472 | * and offset. Must be above the normal perf buffer. | |
6473 | */ | |
6474 | u64 aux_offset, aux_size; | |
6475 | ||
6476 | if (!event->rb) | |
6477 | return -EINVAL; | |
6478 | ||
6479 | nr_pages = vma_size / PAGE_SIZE; | |
6480 | ||
6481 | mutex_lock(&event->mmap_mutex); | |
6482 | ret = -EINVAL; | |
6483 | ||
6484 | rb = event->rb; | |
6485 | if (!rb) | |
6486 | goto aux_unlock; | |
6487 | ||
6aa7de05 MR |
6488 | aux_offset = READ_ONCE(rb->user_page->aux_offset); |
6489 | aux_size = READ_ONCE(rb->user_page->aux_size); | |
45bfb2e5 PZ |
6490 | |
6491 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
6492 | goto aux_unlock; | |
6493 | ||
6494 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
6495 | goto aux_unlock; | |
6496 | ||
6497 | /* already mapped with a different offset */ | |
6498 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
6499 | goto aux_unlock; | |
6500 | ||
6501 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
6502 | goto aux_unlock; | |
6503 | ||
6504 | /* already mapped with a different size */ | |
6505 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
6506 | goto aux_unlock; | |
6507 | ||
6508 | if (!is_power_of_2(nr_pages)) | |
6509 | goto aux_unlock; | |
6510 | ||
6511 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
6512 | goto aux_unlock; | |
6513 | ||
6514 | if (rb_has_aux(rb)) { | |
6515 | atomic_inc(&rb->aux_mmap_count); | |
6516 | ret = 0; | |
6517 | goto unlock; | |
6518 | } | |
6519 | ||
6520 | atomic_set(&rb->aux_mmap_count, 1); | |
6521 | user_extra = nr_pages; | |
6522 | ||
6523 | goto accounting; | |
6524 | } | |
7b732a75 | 6525 | |
7730d865 | 6526 | /* |
76369139 | 6527 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
6528 | * can do bitmasks instead of modulo. |
6529 | */ | |
2ed11312 | 6530 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
6531 | return -EINVAL; |
6532 | ||
7b732a75 | 6533 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
6534 | return -EINVAL; |
6535 | ||
cdd6c482 | 6536 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 6537 | again: |
cdd6c482 | 6538 | mutex_lock(&event->mmap_mutex); |
76369139 | 6539 | if (event->rb) { |
60490e79 | 6540 | if (data_page_nr(event->rb) != nr_pages) { |
ebb3c4c4 | 6541 | ret = -EINVAL; |
9bb5d40c PZ |
6542 | goto unlock; |
6543 | } | |
6544 | ||
6545 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
6546 | /* | |
68e3c698 PZ |
6547 | * Raced against perf_mmap_close(); remove the |
6548 | * event and try again. | |
9bb5d40c | 6549 | */ |
68e3c698 | 6550 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
6551 | mutex_unlock(&event->mmap_mutex); |
6552 | goto again; | |
6553 | } | |
6554 | ||
ebb3c4c4 PZ |
6555 | goto unlock; |
6556 | } | |
6557 | ||
789f90fc | 6558 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
6559 | |
6560 | accounting: | |
cdd6c482 | 6561 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
6562 | |
6563 | /* | |
6564 | * Increase the limit linearly with more CPUs: | |
6565 | */ | |
6566 | user_lock_limit *= num_online_cpus(); | |
6567 | ||
00346155 SL |
6568 | user_locked = atomic_long_read(&user->locked_vm); |
6569 | ||
6570 | /* | |
6571 | * sysctl_perf_event_mlock may have changed, so that | |
6572 | * user->locked_vm > user_lock_limit | |
6573 | */ | |
6574 | if (user_locked > user_lock_limit) | |
6575 | user_locked = user_lock_limit; | |
6576 | user_locked += user_extra; | |
c5078f78 | 6577 | |
c4b75479 | 6578 | if (user_locked > user_lock_limit) { |
d44248a4 SL |
6579 | /* |
6580 | * charge locked_vm until it hits user_lock_limit; | |
6581 | * charge the rest from pinned_vm | |
6582 | */ | |
789f90fc | 6583 | extra = user_locked - user_lock_limit; |
d44248a4 SL |
6584 | user_extra -= extra; |
6585 | } | |
7b732a75 | 6586 | |
78d7d407 | 6587 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 6588 | lock_limit >>= PAGE_SHIFT; |
70f8a3ca | 6589 | locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; |
7b732a75 | 6590 | |
da97e184 | 6591 | if ((locked > lock_limit) && perf_is_paranoid() && |
459ec28a | 6592 | !capable(CAP_IPC_LOCK)) { |
ebb3c4c4 PZ |
6593 | ret = -EPERM; |
6594 | goto unlock; | |
6595 | } | |
7b732a75 | 6596 | |
45bfb2e5 | 6597 | WARN_ON(!rb && event->rb); |
906010b2 | 6598 | |
d57e34fd | 6599 | if (vma->vm_flags & VM_WRITE) |
76369139 | 6600 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 6601 | |
76369139 | 6602 | if (!rb) { |
45bfb2e5 PZ |
6603 | rb = rb_alloc(nr_pages, |
6604 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
6605 | event->cpu, flags); | |
26cb63ad | 6606 | |
45bfb2e5 PZ |
6607 | if (!rb) { |
6608 | ret = -ENOMEM; | |
6609 | goto unlock; | |
6610 | } | |
43a21ea8 | 6611 | |
45bfb2e5 PZ |
6612 | atomic_set(&rb->mmap_count, 1); |
6613 | rb->mmap_user = get_current_user(); | |
6614 | rb->mmap_locked = extra; | |
26cb63ad | 6615 | |
45bfb2e5 | 6616 | ring_buffer_attach(event, rb); |
ac9721f3 | 6617 | |
f7925653 | 6618 | perf_event_update_time(event); |
45bfb2e5 PZ |
6619 | perf_event_init_userpage(event); |
6620 | perf_event_update_userpage(event); | |
6621 | } else { | |
1a594131 AS |
6622 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
6623 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
6624 | if (!ret) |
6625 | rb->aux_mmap_locked = extra; | |
6626 | } | |
9a0f05cb | 6627 | |
ebb3c4c4 | 6628 | unlock: |
45bfb2e5 PZ |
6629 | if (!ret) { |
6630 | atomic_long_add(user_extra, &user->locked_vm); | |
70f8a3ca | 6631 | atomic64_add(extra, &vma->vm_mm->pinned_vm); |
45bfb2e5 | 6632 | |
ac9721f3 | 6633 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
6634 | } else if (rb) { |
6635 | atomic_dec(&rb->mmap_count); | |
6636 | } | |
6637 | aux_unlock: | |
cdd6c482 | 6638 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 6639 | |
9bb5d40c PZ |
6640 | /* |
6641 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
6642 | * vma. | |
6643 | */ | |
1c71222e | 6644 | vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); |
37d81828 | 6645 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 6646 | |
1e0fb9ec | 6647 | if (event->pmu->event_mapped) |
bfe33492 | 6648 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 6649 | |
7b732a75 | 6650 | return ret; |
37d81828 PM |
6651 | } |
6652 | ||
3c446b3d PZ |
6653 | static int perf_fasync(int fd, struct file *filp, int on) |
6654 | { | |
496ad9aa | 6655 | struct inode *inode = file_inode(filp); |
cdd6c482 | 6656 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
6657 | int retval; |
6658 | ||
5955102c | 6659 | inode_lock(inode); |
cdd6c482 | 6660 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 6661 | inode_unlock(inode); |
3c446b3d PZ |
6662 | |
6663 | if (retval < 0) | |
6664 | return retval; | |
6665 | ||
6666 | return 0; | |
6667 | } | |
6668 | ||
0793a61d | 6669 | static const struct file_operations perf_fops = { |
3326c1ce | 6670 | .llseek = no_llseek, |
0793a61d TG |
6671 | .release = perf_release, |
6672 | .read = perf_read, | |
6673 | .poll = perf_poll, | |
d859e29f | 6674 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 6675 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 6676 | .mmap = perf_mmap, |
3c446b3d | 6677 | .fasync = perf_fasync, |
0793a61d TG |
6678 | }; |
6679 | ||
925d519a | 6680 | /* |
cdd6c482 | 6681 | * Perf event wakeup |
925d519a PZ |
6682 | * |
6683 | * If there's data, ensure we set the poll() state and publish everything | |
6684 | * to user-space before waking everybody up. | |
6685 | */ | |
6686 | ||
fed66e2c PZ |
6687 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
6688 | { | |
6689 | /* only the parent has fasync state */ | |
6690 | if (event->parent) | |
6691 | event = event->parent; | |
6692 | return &event->fasync; | |
6693 | } | |
6694 | ||
cdd6c482 | 6695 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 6696 | { |
10c6db11 | 6697 | ring_buffer_wakeup(event); |
4c9e2542 | 6698 | |
cdd6c482 | 6699 | if (event->pending_kill) { |
fed66e2c | 6700 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 6701 | event->pending_kill = 0; |
4c9e2542 | 6702 | } |
925d519a PZ |
6703 | } |
6704 | ||
97ba62b2 ME |
6705 | static void perf_sigtrap(struct perf_event *event) |
6706 | { | |
97ba62b2 ME |
6707 | /* |
6708 | * We'd expect this to only occur if the irq_work is delayed and either | |
6709 | * ctx->task or current has changed in the meantime. This can be the | |
6710 | * case on architectures that do not implement arch_irq_work_raise(). | |
6711 | */ | |
6712 | if (WARN_ON_ONCE(event->ctx->task != current)) | |
6713 | return; | |
6714 | ||
6715 | /* | |
ca6c2132 PZ |
6716 | * Both perf_pending_task() and perf_pending_irq() can race with the |
6717 | * task exiting. | |
97ba62b2 ME |
6718 | */ |
6719 | if (current->flags & PF_EXITING) | |
6720 | return; | |
6721 | ||
78ed93d7 | 6722 | send_sig_perf((void __user *)event->pending_addr, |
0d6d062c | 6723 | event->orig_type, event->attr.sig_data); |
97ba62b2 ME |
6724 | } |
6725 | ||
ca6c2132 PZ |
6726 | /* |
6727 | * Deliver the pending work in-event-context or follow the context. | |
6728 | */ | |
6729 | static void __perf_pending_irq(struct perf_event *event) | |
1d54ad94 | 6730 | { |
ca6c2132 | 6731 | int cpu = READ_ONCE(event->oncpu); |
1d54ad94 | 6732 | |
ca6c2132 PZ |
6733 | /* |
6734 | * If the event isn't running; we done. event_sched_out() will have | |
6735 | * taken care of things. | |
6736 | */ | |
1d54ad94 PZ |
6737 | if (cpu < 0) |
6738 | return; | |
6739 | ||
ca6c2132 PZ |
6740 | /* |
6741 | * Yay, we hit home and are in the context of the event. | |
6742 | */ | |
1d54ad94 | 6743 | if (cpu == smp_processor_id()) { |
ca6c2132 PZ |
6744 | if (event->pending_sigtrap) { |
6745 | event->pending_sigtrap = 0; | |
97ba62b2 | 6746 | perf_sigtrap(event); |
ca6c2132 PZ |
6747 | local_dec(&event->ctx->nr_pending); |
6748 | } | |
6749 | if (event->pending_disable) { | |
6750 | event->pending_disable = 0; | |
6751 | perf_event_disable_local(event); | |
97ba62b2 | 6752 | } |
1d54ad94 PZ |
6753 | return; |
6754 | } | |
6755 | ||
6756 | /* | |
6757 | * CPU-A CPU-B | |
6758 | * | |
6759 | * perf_event_disable_inatomic() | |
6760 | * @pending_disable = CPU-A; | |
6761 | * irq_work_queue(); | |
6762 | * | |
6763 | * sched-out | |
6764 | * @pending_disable = -1; | |
6765 | * | |
6766 | * sched-in | |
6767 | * perf_event_disable_inatomic() | |
6768 | * @pending_disable = CPU-B; | |
6769 | * irq_work_queue(); // FAILS | |
6770 | * | |
6771 | * irq_work_run() | |
ca6c2132 | 6772 | * perf_pending_irq() |
1d54ad94 PZ |
6773 | * |
6774 | * But the event runs on CPU-B and wants disabling there. | |
6775 | */ | |
ca6c2132 | 6776 | irq_work_queue_on(&event->pending_irq, cpu); |
1d54ad94 PZ |
6777 | } |
6778 | ||
ca6c2132 | 6779 | static void perf_pending_irq(struct irq_work *entry) |
79f14641 | 6780 | { |
ca6c2132 | 6781 | struct perf_event *event = container_of(entry, struct perf_event, pending_irq); |
d525211f PZ |
6782 | int rctx; |
6783 | ||
d525211f PZ |
6784 | /* |
6785 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
6786 | * and we won't recurse 'further'. | |
6787 | */ | |
ca6c2132 | 6788 | rctx = perf_swevent_get_recursion_context(); |
79f14641 | 6789 | |
ca6c2132 PZ |
6790 | /* |
6791 | * The wakeup isn't bound to the context of the event -- it can happen | |
6792 | * irrespective of where the event is. | |
6793 | */ | |
cdd6c482 IM |
6794 | if (event->pending_wakeup) { |
6795 | event->pending_wakeup = 0; | |
6796 | perf_event_wakeup(event); | |
79f14641 | 6797 | } |
d525211f | 6798 | |
ca6c2132 PZ |
6799 | __perf_pending_irq(event); |
6800 | ||
d525211f PZ |
6801 | if (rctx >= 0) |
6802 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
6803 | } |
6804 | ||
ca6c2132 PZ |
6805 | static void perf_pending_task(struct callback_head *head) |
6806 | { | |
6807 | struct perf_event *event = container_of(head, struct perf_event, pending_task); | |
6808 | int rctx; | |
6809 | ||
6810 | /* | |
6811 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
6812 | * and we won't recurse 'further'. | |
6813 | */ | |
6814 | preempt_disable_notrace(); | |
6815 | rctx = perf_swevent_get_recursion_context(); | |
6816 | ||
6817 | if (event->pending_work) { | |
6818 | event->pending_work = 0; | |
6819 | perf_sigtrap(event); | |
6820 | local_dec(&event->ctx->nr_pending); | |
6821 | } | |
6822 | ||
6823 | if (rctx >= 0) | |
6824 | perf_swevent_put_recursion_context(rctx); | |
6825 | preempt_enable_notrace(); | |
517e6a30 PZ |
6826 | |
6827 | put_event(event); | |
ca6c2132 PZ |
6828 | } |
6829 | ||
2aef6f30 | 6830 | #ifdef CONFIG_GUEST_PERF_EVENTS |
ff083a2d | 6831 | struct perf_guest_info_callbacks __rcu *perf_guest_cbs; |
39447b38 | 6832 | |
87b940a0 SC |
6833 | DEFINE_STATIC_CALL_RET0(__perf_guest_state, *perf_guest_cbs->state); |
6834 | DEFINE_STATIC_CALL_RET0(__perf_guest_get_ip, *perf_guest_cbs->get_ip); | |
6835 | DEFINE_STATIC_CALL_RET0(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); | |
39447b38 | 6836 | |
2934e3d0 | 6837 | void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
39447b38 | 6838 | { |
ff083a2d | 6839 | if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs))) |
2934e3d0 | 6840 | return; |
ff083a2d SC |
6841 | |
6842 | rcu_assign_pointer(perf_guest_cbs, cbs); | |
87b940a0 SC |
6843 | static_call_update(__perf_guest_state, cbs->state); |
6844 | static_call_update(__perf_guest_get_ip, cbs->get_ip); | |
6845 | ||
6846 | /* Implementing ->handle_intel_pt_intr is optional. */ | |
6847 | if (cbs->handle_intel_pt_intr) | |
6848 | static_call_update(__perf_guest_handle_intel_pt_intr, | |
6849 | cbs->handle_intel_pt_intr); | |
39447b38 ZY |
6850 | } |
6851 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
6852 | ||
2934e3d0 | 6853 | void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
39447b38 | 6854 | { |
ff083a2d | 6855 | if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs) != cbs)) |
2934e3d0 | 6856 | return; |
ff083a2d SC |
6857 | |
6858 | rcu_assign_pointer(perf_guest_cbs, NULL); | |
87b940a0 SC |
6859 | static_call_update(__perf_guest_state, (void *)&__static_call_return0); |
6860 | static_call_update(__perf_guest_get_ip, (void *)&__static_call_return0); | |
6861 | static_call_update(__perf_guest_handle_intel_pt_intr, | |
6862 | (void *)&__static_call_return0); | |
ff083a2d | 6863 | synchronize_rcu(); |
39447b38 ZY |
6864 | } |
6865 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
2aef6f30 | 6866 | #endif |
39447b38 | 6867 | |
4018994f JO |
6868 | static void |
6869 | perf_output_sample_regs(struct perf_output_handle *handle, | |
6870 | struct pt_regs *regs, u64 mask) | |
6871 | { | |
6872 | int bit; | |
29dd3288 | 6873 | DECLARE_BITMAP(_mask, 64); |
4018994f | 6874 | |
29dd3288 MS |
6875 | bitmap_from_u64(_mask, mask); |
6876 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
6877 | u64 val; |
6878 | ||
6879 | val = perf_reg_value(regs, bit); | |
6880 | perf_output_put(handle, val); | |
6881 | } | |
6882 | } | |
6883 | ||
60e2364e | 6884 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
76a4efa8 | 6885 | struct pt_regs *regs) |
4018994f | 6886 | { |
88a7c26a AL |
6887 | if (user_mode(regs)) { |
6888 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 6889 | regs_user->regs = regs; |
085ebfe9 | 6890 | } else if (!(current->flags & PF_KTHREAD)) { |
76a4efa8 | 6891 | perf_get_regs_user(regs_user, regs); |
2565711f PZ |
6892 | } else { |
6893 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
6894 | regs_user->regs = NULL; | |
4018994f JO |
6895 | } |
6896 | } | |
6897 | ||
60e2364e SE |
6898 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
6899 | struct pt_regs *regs) | |
6900 | { | |
6901 | regs_intr->regs = regs; | |
6902 | regs_intr->abi = perf_reg_abi(current); | |
6903 | } | |
6904 | ||
6905 | ||
c5ebcedb JO |
6906 | /* |
6907 | * Get remaining task size from user stack pointer. | |
6908 | * | |
6909 | * It'd be better to take stack vma map and limit this more | |
9f014e3a | 6910 | * precisely, but there's no way to get it safely under interrupt, |
c5ebcedb JO |
6911 | * so using TASK_SIZE as limit. |
6912 | */ | |
6913 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
6914 | { | |
6915 | unsigned long addr = perf_user_stack_pointer(regs); | |
6916 | ||
6917 | if (!addr || addr >= TASK_SIZE) | |
6918 | return 0; | |
6919 | ||
6920 | return TASK_SIZE - addr; | |
6921 | } | |
6922 | ||
6923 | static u16 | |
6924 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
6925 | struct pt_regs *regs) | |
6926 | { | |
6927 | u64 task_size; | |
6928 | ||
6929 | /* No regs, no stack pointer, no dump. */ | |
6930 | if (!regs) | |
6931 | return 0; | |
6932 | ||
6933 | /* | |
6934 | * Check if we fit in with the requested stack size into the: | |
6935 | * - TASK_SIZE | |
6936 | * If we don't, we limit the size to the TASK_SIZE. | |
6937 | * | |
6938 | * - remaining sample size | |
6939 | * If we don't, we customize the stack size to | |
6940 | * fit in to the remaining sample size. | |
6941 | */ | |
6942 | ||
6943 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
6944 | stack_size = min(stack_size, (u16) task_size); | |
6945 | ||
6946 | /* Current header size plus static size and dynamic size. */ | |
6947 | header_size += 2 * sizeof(u64); | |
6948 | ||
6949 | /* Do we fit in with the current stack dump size? */ | |
6950 | if ((u16) (header_size + stack_size) < header_size) { | |
6951 | /* | |
6952 | * If we overflow the maximum size for the sample, | |
6953 | * we customize the stack dump size to fit in. | |
6954 | */ | |
6955 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
6956 | stack_size = round_up(stack_size, sizeof(u64)); | |
6957 | } | |
6958 | ||
6959 | return stack_size; | |
6960 | } | |
6961 | ||
6962 | static void | |
6963 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
6964 | struct pt_regs *regs) | |
6965 | { | |
6966 | /* Case of a kernel thread, nothing to dump */ | |
6967 | if (!regs) { | |
6968 | u64 size = 0; | |
6969 | perf_output_put(handle, size); | |
6970 | } else { | |
6971 | unsigned long sp; | |
6972 | unsigned int rem; | |
6973 | u64 dyn_size; | |
6974 | ||
6975 | /* | |
6976 | * We dump: | |
6977 | * static size | |
6978 | * - the size requested by user or the best one we can fit | |
6979 | * in to the sample max size | |
6980 | * data | |
6981 | * - user stack dump data | |
6982 | * dynamic size | |
6983 | * - the actual dumped size | |
6984 | */ | |
6985 | ||
6986 | /* Static size. */ | |
6987 | perf_output_put(handle, dump_size); | |
6988 | ||
6989 | /* Data. */ | |
6990 | sp = perf_user_stack_pointer(regs); | |
6991 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
6992 | dyn_size = dump_size - rem; | |
6993 | ||
6994 | perf_output_skip(handle, rem); | |
6995 | ||
6996 | /* Dynamic size. */ | |
6997 | perf_output_put(handle, dyn_size); | |
6998 | } | |
6999 | } | |
7000 | ||
a4faf00d AS |
7001 | static unsigned long perf_prepare_sample_aux(struct perf_event *event, |
7002 | struct perf_sample_data *data, | |
7003 | size_t size) | |
7004 | { | |
7005 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 7006 | struct perf_buffer *rb; |
a4faf00d AS |
7007 | |
7008 | data->aux_size = 0; | |
7009 | ||
7010 | if (!sampler) | |
7011 | goto out; | |
7012 | ||
7013 | if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE)) | |
7014 | goto out; | |
7015 | ||
7016 | if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id())) | |
7017 | goto out; | |
7018 | ||
961c3912 | 7019 | rb = ring_buffer_get(sampler); |
a4faf00d AS |
7020 | if (!rb) |
7021 | goto out; | |
7022 | ||
7023 | /* | |
7024 | * If this is an NMI hit inside sampling code, don't take | |
7025 | * the sample. See also perf_aux_sample_output(). | |
7026 | */ | |
7027 | if (READ_ONCE(rb->aux_in_sampling)) { | |
7028 | data->aux_size = 0; | |
7029 | } else { | |
7030 | size = min_t(size_t, size, perf_aux_size(rb)); | |
7031 | data->aux_size = ALIGN(size, sizeof(u64)); | |
7032 | } | |
7033 | ring_buffer_put(rb); | |
7034 | ||
7035 | out: | |
7036 | return data->aux_size; | |
7037 | } | |
7038 | ||
32961aec HX |
7039 | static long perf_pmu_snapshot_aux(struct perf_buffer *rb, |
7040 | struct perf_event *event, | |
7041 | struct perf_output_handle *handle, | |
7042 | unsigned long size) | |
a4faf00d AS |
7043 | { |
7044 | unsigned long flags; | |
7045 | long ret; | |
7046 | ||
7047 | /* | |
7048 | * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler | |
7049 | * paths. If we start calling them in NMI context, they may race with | |
7050 | * the IRQ ones, that is, for example, re-starting an event that's just | |
7051 | * been stopped, which is why we're using a separate callback that | |
7052 | * doesn't change the event state. | |
7053 | * | |
7054 | * IRQs need to be disabled to prevent IPIs from racing with us. | |
7055 | */ | |
7056 | local_irq_save(flags); | |
7057 | /* | |
7058 | * Guard against NMI hits inside the critical section; | |
7059 | * see also perf_prepare_sample_aux(). | |
7060 | */ | |
7061 | WRITE_ONCE(rb->aux_in_sampling, 1); | |
7062 | barrier(); | |
7063 | ||
7064 | ret = event->pmu->snapshot_aux(event, handle, size); | |
7065 | ||
7066 | barrier(); | |
7067 | WRITE_ONCE(rb->aux_in_sampling, 0); | |
7068 | local_irq_restore(flags); | |
7069 | ||
7070 | return ret; | |
7071 | } | |
7072 | ||
7073 | static void perf_aux_sample_output(struct perf_event *event, | |
7074 | struct perf_output_handle *handle, | |
7075 | struct perf_sample_data *data) | |
7076 | { | |
7077 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 7078 | struct perf_buffer *rb; |
a4faf00d | 7079 | unsigned long pad; |
a4faf00d AS |
7080 | long size; |
7081 | ||
7082 | if (WARN_ON_ONCE(!sampler || !data->aux_size)) | |
7083 | return; | |
7084 | ||
961c3912 | 7085 | rb = ring_buffer_get(sampler); |
a4faf00d AS |
7086 | if (!rb) |
7087 | return; | |
7088 | ||
7089 | size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size); | |
7090 | ||
7091 | /* | |
7092 | * An error here means that perf_output_copy() failed (returned a | |
7093 | * non-zero surplus that it didn't copy), which in its current | |
7094 | * enlightened implementation is not possible. If that changes, we'd | |
7095 | * like to know. | |
7096 | */ | |
7097 | if (WARN_ON_ONCE(size < 0)) | |
7098 | goto out_put; | |
7099 | ||
7100 | /* | |
7101 | * The pad comes from ALIGN()ing data->aux_size up to u64 in | |
7102 | * perf_prepare_sample_aux(), so should not be more than that. | |
7103 | */ | |
7104 | pad = data->aux_size - size; | |
7105 | if (WARN_ON_ONCE(pad >= sizeof(u64))) | |
7106 | pad = 8; | |
7107 | ||
7108 | if (pad) { | |
7109 | u64 zero = 0; | |
7110 | perf_output_copy(handle, &zero, pad); | |
7111 | } | |
7112 | ||
7113 | out_put: | |
7114 | ring_buffer_put(rb); | |
7115 | } | |
7116 | ||
bb447c27 NK |
7117 | /* |
7118 | * A set of common sample data types saved even for non-sample records | |
7119 | * when event->attr.sample_id_all is set. | |
7120 | */ | |
7121 | #define PERF_SAMPLE_ID_ALL (PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ | |
7122 | PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \ | |
7123 | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER) | |
7124 | ||
a7c8d0da | 7125 | static void __perf_event_header__init_id(struct perf_sample_data *data, |
3aac580d KL |
7126 | struct perf_event *event, |
7127 | u64 sample_type) | |
6844c09d | 7128 | { |
3aac580d | 7129 | data->type = event->attr.sample_type; |
bb447c27 | 7130 | data->sample_flags |= data->type & PERF_SAMPLE_ID_ALL; |
6844c09d ACM |
7131 | |
7132 | if (sample_type & PERF_SAMPLE_TID) { | |
7133 | /* namespace issues */ | |
7134 | data->tid_entry.pid = perf_event_pid(event, current); | |
7135 | data->tid_entry.tid = perf_event_tid(event, current); | |
7136 | } | |
7137 | ||
7138 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 7139 | data->time = perf_event_clock(event); |
6844c09d | 7140 | |
ff3d527c | 7141 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
7142 | data->id = primary_event_id(event); |
7143 | ||
7144 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7145 | data->stream_id = event->id; | |
7146 | ||
7147 | if (sample_type & PERF_SAMPLE_CPU) { | |
7148 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
7149 | data->cpu_entry.reserved = 0; | |
7150 | } | |
7151 | } | |
7152 | ||
76369139 FW |
7153 | void perf_event_header__init_id(struct perf_event_header *header, |
7154 | struct perf_sample_data *data, | |
7155 | struct perf_event *event) | |
c980d109 | 7156 | { |
a7c8d0da NK |
7157 | if (event->attr.sample_id_all) { |
7158 | header->size += event->id_header_size; | |
7159 | __perf_event_header__init_id(data, event, event->attr.sample_type); | |
7160 | } | |
c980d109 ACM |
7161 | } |
7162 | ||
7163 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
7164 | struct perf_sample_data *data) | |
7165 | { | |
7166 | u64 sample_type = data->type; | |
7167 | ||
7168 | if (sample_type & PERF_SAMPLE_TID) | |
7169 | perf_output_put(handle, data->tid_entry); | |
7170 | ||
7171 | if (sample_type & PERF_SAMPLE_TIME) | |
7172 | perf_output_put(handle, data->time); | |
7173 | ||
7174 | if (sample_type & PERF_SAMPLE_ID) | |
7175 | perf_output_put(handle, data->id); | |
7176 | ||
7177 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7178 | perf_output_put(handle, data->stream_id); | |
7179 | ||
7180 | if (sample_type & PERF_SAMPLE_CPU) | |
7181 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
7182 | |
7183 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
7184 | perf_output_put(handle, data->id); | |
c980d109 ACM |
7185 | } |
7186 | ||
76369139 FW |
7187 | void perf_event__output_id_sample(struct perf_event *event, |
7188 | struct perf_output_handle *handle, | |
7189 | struct perf_sample_data *sample) | |
c980d109 ACM |
7190 | { |
7191 | if (event->attr.sample_id_all) | |
7192 | __perf_event__output_id_sample(handle, sample); | |
7193 | } | |
7194 | ||
3dab77fb | 7195 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
7196 | struct perf_event *event, |
7197 | u64 enabled, u64 running) | |
3dab77fb | 7198 | { |
cdd6c482 | 7199 | u64 read_format = event->attr.read_format; |
119a784c | 7200 | u64 values[5]; |
3dab77fb PZ |
7201 | int n = 0; |
7202 | ||
b5e58793 | 7203 | values[n++] = perf_event_count(event); |
3dab77fb | 7204 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 7205 | values[n++] = enabled + |
cdd6c482 | 7206 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
7207 | } |
7208 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 7209 | values[n++] = running + |
cdd6c482 | 7210 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
7211 | } |
7212 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 7213 | values[n++] = primary_event_id(event); |
119a784c NK |
7214 | if (read_format & PERF_FORMAT_LOST) |
7215 | values[n++] = atomic64_read(&event->lost_samples); | |
3dab77fb | 7216 | |
76369139 | 7217 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
7218 | } |
7219 | ||
3dab77fb | 7220 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
7221 | struct perf_event *event, |
7222 | u64 enabled, u64 running) | |
3dab77fb | 7223 | { |
cdd6c482 IM |
7224 | struct perf_event *leader = event->group_leader, *sub; |
7225 | u64 read_format = event->attr.read_format; | |
6b959ba2 | 7226 | unsigned long flags; |
119a784c | 7227 | u64 values[6]; |
3dab77fb PZ |
7228 | int n = 0; |
7229 | ||
6b959ba2 YJ |
7230 | /* |
7231 | * Disabling interrupts avoids all counter scheduling | |
7232 | * (context switches, timer based rotation and IPIs). | |
7233 | */ | |
7234 | local_irq_save(flags); | |
7235 | ||
3dab77fb PZ |
7236 | values[n++] = 1 + leader->nr_siblings; |
7237 | ||
7238 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 7239 | values[n++] = enabled; |
3dab77fb PZ |
7240 | |
7241 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 7242 | values[n++] = running; |
3dab77fb | 7243 | |
9e5b127d PZ |
7244 | if ((leader != event) && |
7245 | (leader->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
7246 | leader->pmu->read(leader); |
7247 | ||
b5e58793 | 7248 | values[n++] = perf_event_count(leader); |
3dab77fb | 7249 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 7250 | values[n++] = primary_event_id(leader); |
119a784c NK |
7251 | if (read_format & PERF_FORMAT_LOST) |
7252 | values[n++] = atomic64_read(&leader->lost_samples); | |
3dab77fb | 7253 | |
76369139 | 7254 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 7255 | |
edb39592 | 7256 | for_each_sibling_event(sub, leader) { |
3dab77fb PZ |
7257 | n = 0; |
7258 | ||
6f5ab001 JO |
7259 | if ((sub != event) && |
7260 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
7261 | sub->pmu->read(sub); |
7262 | ||
b5e58793 | 7263 | values[n++] = perf_event_count(sub); |
3dab77fb | 7264 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 7265 | values[n++] = primary_event_id(sub); |
119a784c NK |
7266 | if (read_format & PERF_FORMAT_LOST) |
7267 | values[n++] = atomic64_read(&sub->lost_samples); | |
3dab77fb | 7268 | |
76369139 | 7269 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 7270 | } |
6b959ba2 YJ |
7271 | |
7272 | local_irq_restore(flags); | |
3dab77fb PZ |
7273 | } |
7274 | ||
eed01528 SE |
7275 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
7276 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
7277 | ||
ba5213ae PZ |
7278 | /* |
7279 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
7280 | * | |
7281 | * The problem is that its both hard and excessively expensive to iterate the | |
7282 | * child list, not to mention that its impossible to IPI the children running | |
7283 | * on another CPU, from interrupt/NMI context. | |
7284 | */ | |
3dab77fb | 7285 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 7286 | struct perf_event *event) |
3dab77fb | 7287 | { |
e3f3541c | 7288 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
7289 | u64 read_format = event->attr.read_format; |
7290 | ||
7291 | /* | |
7292 | * compute total_time_enabled, total_time_running | |
7293 | * based on snapshot values taken when the event | |
7294 | * was last scheduled in. | |
7295 | * | |
7296 | * we cannot simply called update_context_time() | |
7297 | * because of locking issue as we are called in | |
7298 | * NMI context | |
7299 | */ | |
c4794295 | 7300 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 7301 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 7302 | |
cdd6c482 | 7303 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 7304 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 7305 | else |
eed01528 | 7306 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
7307 | } |
7308 | ||
5622f295 MM |
7309 | void perf_output_sample(struct perf_output_handle *handle, |
7310 | struct perf_event_header *header, | |
7311 | struct perf_sample_data *data, | |
cdd6c482 | 7312 | struct perf_event *event) |
5622f295 MM |
7313 | { |
7314 | u64 sample_type = data->type; | |
7315 | ||
7316 | perf_output_put(handle, *header); | |
7317 | ||
ff3d527c AH |
7318 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
7319 | perf_output_put(handle, data->id); | |
7320 | ||
5622f295 MM |
7321 | if (sample_type & PERF_SAMPLE_IP) |
7322 | perf_output_put(handle, data->ip); | |
7323 | ||
7324 | if (sample_type & PERF_SAMPLE_TID) | |
7325 | perf_output_put(handle, data->tid_entry); | |
7326 | ||
7327 | if (sample_type & PERF_SAMPLE_TIME) | |
7328 | perf_output_put(handle, data->time); | |
7329 | ||
7330 | if (sample_type & PERF_SAMPLE_ADDR) | |
7331 | perf_output_put(handle, data->addr); | |
7332 | ||
7333 | if (sample_type & PERF_SAMPLE_ID) | |
7334 | perf_output_put(handle, data->id); | |
7335 | ||
7336 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
7337 | perf_output_put(handle, data->stream_id); | |
7338 | ||
7339 | if (sample_type & PERF_SAMPLE_CPU) | |
7340 | perf_output_put(handle, data->cpu_entry); | |
7341 | ||
7342 | if (sample_type & PERF_SAMPLE_PERIOD) | |
7343 | perf_output_put(handle, data->period); | |
7344 | ||
7345 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 7346 | perf_output_read(handle, event); |
5622f295 MM |
7347 | |
7348 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
99e818cc | 7349 | int size = 1; |
5622f295 | 7350 | |
99e818cc JO |
7351 | size += data->callchain->nr; |
7352 | size *= sizeof(u64); | |
7353 | __output_copy(handle, data->callchain, size); | |
5622f295 MM |
7354 | } |
7355 | ||
7356 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
7357 | struct perf_raw_record *raw = data->raw; |
7358 | ||
7359 | if (raw) { | |
7360 | struct perf_raw_frag *frag = &raw->frag; | |
7361 | ||
7362 | perf_output_put(handle, raw->size); | |
7363 | do { | |
7364 | if (frag->copy) { | |
7365 | __output_custom(handle, frag->copy, | |
7366 | frag->data, frag->size); | |
7367 | } else { | |
7368 | __output_copy(handle, frag->data, | |
7369 | frag->size); | |
7370 | } | |
7371 | if (perf_raw_frag_last(frag)) | |
7372 | break; | |
7373 | frag = frag->next; | |
7374 | } while (1); | |
7375 | if (frag->pad) | |
7376 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
7377 | } else { |
7378 | struct { | |
7379 | u32 size; | |
7380 | u32 data; | |
7381 | } raw = { | |
7382 | .size = sizeof(u32), | |
7383 | .data = 0, | |
7384 | }; | |
7385 | perf_output_put(handle, raw); | |
7386 | } | |
7387 | } | |
a7ac67ea | 7388 | |
bce38cd5 | 7389 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
eb55b455 | 7390 | if (data->br_stack) { |
bce38cd5 SE |
7391 | size_t size; |
7392 | ||
7393 | size = data->br_stack->nr | |
7394 | * sizeof(struct perf_branch_entry); | |
7395 | ||
7396 | perf_output_put(handle, data->br_stack->nr); | |
03b02db9 | 7397 | if (branch_sample_hw_index(event)) |
bbfd5e4f | 7398 | perf_output_put(handle, data->br_stack->hw_idx); |
bce38cd5 | 7399 | perf_output_copy(handle, data->br_stack->entries, size); |
571d91dc KL |
7400 | /* |
7401 | * Add the extension space which is appended | |
7402 | * right after the struct perf_branch_stack. | |
7403 | */ | |
7404 | if (data->br_stack_cntr) { | |
7405 | size = data->br_stack->nr * sizeof(u64); | |
7406 | perf_output_copy(handle, data->br_stack_cntr, size); | |
7407 | } | |
bce38cd5 SE |
7408 | } else { |
7409 | /* | |
7410 | * we always store at least the value of nr | |
7411 | */ | |
7412 | u64 nr = 0; | |
7413 | perf_output_put(handle, nr); | |
7414 | } | |
7415 | } | |
4018994f JO |
7416 | |
7417 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
7418 | u64 abi = data->regs_user.abi; | |
7419 | ||
7420 | /* | |
7421 | * If there are no regs to dump, notice it through | |
7422 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
7423 | */ | |
7424 | perf_output_put(handle, abi); | |
7425 | ||
7426 | if (abi) { | |
7427 | u64 mask = event->attr.sample_regs_user; | |
7428 | perf_output_sample_regs(handle, | |
7429 | data->regs_user.regs, | |
7430 | mask); | |
7431 | } | |
7432 | } | |
c5ebcedb | 7433 | |
a5cdd40c | 7434 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
7435 | perf_output_sample_ustack(handle, |
7436 | data->stack_user_size, | |
7437 | data->regs_user.regs); | |
a5cdd40c | 7438 | } |
c3feedf2 | 7439 | |
2a6c6b7d KL |
7440 | if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) |
7441 | perf_output_put(handle, data->weight.full); | |
d6be9ad6 SE |
7442 | |
7443 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
7444 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 7445 | |
fdfbbd07 AK |
7446 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
7447 | perf_output_put(handle, data->txn); | |
7448 | ||
60e2364e SE |
7449 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
7450 | u64 abi = data->regs_intr.abi; | |
7451 | /* | |
7452 | * If there are no regs to dump, notice it through | |
7453 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
7454 | */ | |
7455 | perf_output_put(handle, abi); | |
7456 | ||
7457 | if (abi) { | |
7458 | u64 mask = event->attr.sample_regs_intr; | |
7459 | ||
7460 | perf_output_sample_regs(handle, | |
7461 | data->regs_intr.regs, | |
7462 | mask); | |
7463 | } | |
7464 | } | |
7465 | ||
fc7ce9c7 KL |
7466 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
7467 | perf_output_put(handle, data->phys_addr); | |
7468 | ||
6546b19f NK |
7469 | if (sample_type & PERF_SAMPLE_CGROUP) |
7470 | perf_output_put(handle, data->cgroup); | |
7471 | ||
8d97e718 KL |
7472 | if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) |
7473 | perf_output_put(handle, data->data_page_size); | |
7474 | ||
995f088e SE |
7475 | if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) |
7476 | perf_output_put(handle, data->code_page_size); | |
7477 | ||
a4faf00d AS |
7478 | if (sample_type & PERF_SAMPLE_AUX) { |
7479 | perf_output_put(handle, data->aux_size); | |
7480 | ||
7481 | if (data->aux_size) | |
7482 | perf_aux_sample_output(event, handle, data); | |
7483 | } | |
7484 | ||
a5cdd40c PZ |
7485 | if (!event->attr.watermark) { |
7486 | int wakeup_events = event->attr.wakeup_events; | |
7487 | ||
7488 | if (wakeup_events) { | |
56de4e8f | 7489 | struct perf_buffer *rb = handle->rb; |
a5cdd40c PZ |
7490 | int events = local_inc_return(&rb->events); |
7491 | ||
7492 | if (events >= wakeup_events) { | |
7493 | local_sub(wakeup_events, &rb->events); | |
7494 | local_inc(&rb->wakeup); | |
7495 | } | |
7496 | } | |
7497 | } | |
5622f295 MM |
7498 | } |
7499 | ||
fc7ce9c7 KL |
7500 | static u64 perf_virt_to_phys(u64 virt) |
7501 | { | |
7502 | u64 phys_addr = 0; | |
fc7ce9c7 KL |
7503 | |
7504 | if (!virt) | |
7505 | return 0; | |
7506 | ||
7507 | if (virt >= TASK_SIZE) { | |
7508 | /* If it's vmalloc()d memory, leave phys_addr as 0 */ | |
7509 | if (virt_addr_valid((void *)(uintptr_t)virt) && | |
7510 | !(virt >= VMALLOC_START && virt < VMALLOC_END)) | |
7511 | phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); | |
7512 | } else { | |
7513 | /* | |
7514 | * Walking the pages tables for user address. | |
7515 | * Interrupts are disabled, so it prevents any tear down | |
7516 | * of the page tables. | |
dadbb612 | 7517 | * Try IRQ-safe get_user_page_fast_only first. |
fc7ce9c7 KL |
7518 | * If failed, leave phys_addr as 0. |
7519 | */ | |
d3296fb3 | 7520 | if (current->mm != NULL) { |
4716023a GT |
7521 | struct page *p; |
7522 | ||
d3296fb3 | 7523 | pagefault_disable(); |
4716023a | 7524 | if (get_user_page_fast_only(virt, 0, &p)) { |
d3296fb3 | 7525 | phys_addr = page_to_phys(p) + virt % PAGE_SIZE; |
4716023a GT |
7526 | put_page(p); |
7527 | } | |
d3296fb3 JO |
7528 | pagefault_enable(); |
7529 | } | |
fc7ce9c7 KL |
7530 | } |
7531 | ||
7532 | return phys_addr; | |
7533 | } | |
7534 | ||
8d97e718 | 7535 | /* |
8af26be0 | 7536 | * Return the pagetable size of a given virtual address. |
8d97e718 | 7537 | */ |
8af26be0 | 7538 | static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) |
8d97e718 | 7539 | { |
8af26be0 | 7540 | u64 size = 0; |
8d97e718 | 7541 | |
8af26be0 PZ |
7542 | #ifdef CONFIG_HAVE_FAST_GUP |
7543 | pgd_t *pgdp, pgd; | |
7544 | p4d_t *p4dp, p4d; | |
7545 | pud_t *pudp, pud; | |
7546 | pmd_t *pmdp, pmd; | |
7547 | pte_t *ptep, pte; | |
8d97e718 | 7548 | |
8af26be0 PZ |
7549 | pgdp = pgd_offset(mm, addr); |
7550 | pgd = READ_ONCE(*pgdp); | |
7551 | if (pgd_none(pgd)) | |
8d97e718 KL |
7552 | return 0; |
7553 | ||
8af26be0 PZ |
7554 | if (pgd_leaf(pgd)) |
7555 | return pgd_leaf_size(pgd); | |
8d97e718 | 7556 | |
8af26be0 PZ |
7557 | p4dp = p4d_offset_lockless(pgdp, pgd, addr); |
7558 | p4d = READ_ONCE(*p4dp); | |
7559 | if (!p4d_present(p4d)) | |
8d97e718 KL |
7560 | return 0; |
7561 | ||
8af26be0 PZ |
7562 | if (p4d_leaf(p4d)) |
7563 | return p4d_leaf_size(p4d); | |
8d97e718 | 7564 | |
8af26be0 PZ |
7565 | pudp = pud_offset_lockless(p4dp, p4d, addr); |
7566 | pud = READ_ONCE(*pudp); | |
7567 | if (!pud_present(pud)) | |
8d97e718 KL |
7568 | return 0; |
7569 | ||
8af26be0 PZ |
7570 | if (pud_leaf(pud)) |
7571 | return pud_leaf_size(pud); | |
8d97e718 | 7572 | |
8af26be0 | 7573 | pmdp = pmd_offset_lockless(pudp, pud, addr); |
a92cbb82 | 7574 | again: |
1180e732 | 7575 | pmd = pmdp_get_lockless(pmdp); |
8af26be0 | 7576 | if (!pmd_present(pmd)) |
8d97e718 | 7577 | return 0; |
8d97e718 | 7578 | |
8af26be0 PZ |
7579 | if (pmd_leaf(pmd)) |
7580 | return pmd_leaf_size(pmd); | |
51b646b2 | 7581 | |
8af26be0 | 7582 | ptep = pte_offset_map(&pmd, addr); |
a92cbb82 HD |
7583 | if (!ptep) |
7584 | goto again; | |
7585 | ||
8af26be0 PZ |
7586 | pte = ptep_get_lockless(ptep); |
7587 | if (pte_present(pte)) | |
7588 | size = pte_leaf_size(pte); | |
7589 | pte_unmap(ptep); | |
7590 | #endif /* CONFIG_HAVE_FAST_GUP */ | |
8d97e718 | 7591 | |
8af26be0 | 7592 | return size; |
8d97e718 KL |
7593 | } |
7594 | ||
8d97e718 KL |
7595 | static u64 perf_get_page_size(unsigned long addr) |
7596 | { | |
7597 | struct mm_struct *mm; | |
7598 | unsigned long flags; | |
7599 | u64 size; | |
7600 | ||
7601 | if (!addr) | |
7602 | return 0; | |
7603 | ||
7604 | /* | |
7605 | * Software page-table walkers must disable IRQs, | |
7606 | * which prevents any tear down of the page tables. | |
7607 | */ | |
7608 | local_irq_save(flags); | |
7609 | ||
7610 | mm = current->mm; | |
7611 | if (!mm) { | |
7612 | /* | |
7613 | * For kernel threads and the like, use init_mm so that | |
7614 | * we can find kernel memory. | |
7615 | */ | |
7616 | mm = &init_mm; | |
7617 | } | |
7618 | ||
8af26be0 | 7619 | size = perf_get_pgtable_size(mm, addr); |
8d97e718 KL |
7620 | |
7621 | local_irq_restore(flags); | |
7622 | ||
7623 | return size; | |
7624 | } | |
7625 | ||
99e818cc JO |
7626 | static struct perf_callchain_entry __empty_callchain = { .nr = 0, }; |
7627 | ||
6cbc304f | 7628 | struct perf_callchain_entry * |
8cf7e0e2 JO |
7629 | perf_callchain(struct perf_event *event, struct pt_regs *regs) |
7630 | { | |
7631 | bool kernel = !event->attr.exclude_callchain_kernel; | |
7632 | bool user = !event->attr.exclude_callchain_user; | |
7633 | /* Disallow cross-task user callchains. */ | |
7634 | bool crosstask = event->ctx->task && event->ctx->task != current; | |
7635 | const u32 max_stack = event->attr.sample_max_stack; | |
99e818cc | 7636 | struct perf_callchain_entry *callchain; |
8cf7e0e2 JO |
7637 | |
7638 | if (!kernel && !user) | |
99e818cc | 7639 | return &__empty_callchain; |
8cf7e0e2 | 7640 | |
99e818cc JO |
7641 | callchain = get_perf_callchain(regs, 0, kernel, user, |
7642 | max_stack, crosstask, true); | |
7643 | return callchain ?: &__empty_callchain; | |
8cf7e0e2 JO |
7644 | } |
7645 | ||
bb447c27 NK |
7646 | static __always_inline u64 __cond_set(u64 flags, u64 s, u64 d) |
7647 | { | |
7648 | return d * !!(flags & s); | |
7649 | } | |
7650 | ||
f6e70715 | 7651 | void perf_prepare_sample(struct perf_sample_data *data, |
cdd6c482 | 7652 | struct perf_event *event, |
5622f295 | 7653 | struct pt_regs *regs) |
7b732a75 | 7654 | { |
cdd6c482 | 7655 | u64 sample_type = event->attr.sample_type; |
3aac580d | 7656 | u64 filtered_sample_type; |
7b732a75 | 7657 | |
3aac580d | 7658 | /* |
bb447c27 NK |
7659 | * Add the sample flags that are dependent to others. And clear the |
7660 | * sample flags that have already been done by the PMU driver. | |
3aac580d | 7661 | */ |
bb447c27 NK |
7662 | filtered_sample_type = sample_type; |
7663 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_CODE_PAGE_SIZE, | |
7664 | PERF_SAMPLE_IP); | |
7665 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_DATA_PAGE_SIZE | | |
7666 | PERF_SAMPLE_PHYS_ADDR, PERF_SAMPLE_ADDR); | |
7667 | filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_STACK_USER, | |
7668 | PERF_SAMPLE_REGS_USER); | |
7669 | filtered_sample_type &= ~data->sample_flags; | |
6844c09d | 7670 | |
f6e70715 NK |
7671 | if (filtered_sample_type == 0) { |
7672 | /* Make sure it has the correct data->type for output */ | |
7673 | data->type = event->attr.sample_type; | |
7674 | return; | |
394ee076 PZ |
7675 | } |
7676 | ||
a7c8d0da | 7677 | __perf_event_header__init_id(data, event, filtered_sample_type); |
7e3f977e | 7678 | |
bb447c27 | 7679 | if (filtered_sample_type & PERF_SAMPLE_IP) { |
5622f295 | 7680 | data->ip = perf_instruction_pointer(regs); |
bb447c27 NK |
7681 | data->sample_flags |= PERF_SAMPLE_IP; |
7682 | } | |
7e3f977e | 7683 | |
31046500 NK |
7684 | if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN) |
7685 | perf_sample_save_callchain(data, event, regs); | |
a044560c | 7686 | |
0a9081cf NK |
7687 | if (filtered_sample_type & PERF_SAMPLE_RAW) { |
7688 | data->raw = NULL; | |
7689 | data->dyn_size += sizeof(u64); | |
7690 | data->sample_flags |= PERF_SAMPLE_RAW; | |
7f453c24 | 7691 | } |
bce38cd5 | 7692 | |
eb55b455 NK |
7693 | if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) { |
7694 | data->br_stack = NULL; | |
7695 | data->dyn_size += sizeof(u64); | |
7696 | data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; | |
bce38cd5 | 7697 | } |
4018994f | 7698 | |
bb447c27 | 7699 | if (filtered_sample_type & PERF_SAMPLE_REGS_USER) |
76a4efa8 | 7700 | perf_sample_regs_user(&data->regs_user, regs); |
2565711f | 7701 | |
bb447c27 NK |
7702 | /* |
7703 | * It cannot use the filtered_sample_type here as REGS_USER can be set | |
7704 | * by STACK_USER (using __cond_set() above) and we don't want to update | |
7705 | * the dyn_size if it's not requested by users. | |
7706 | */ | |
7707 | if ((sample_type & ~data->sample_flags) & PERF_SAMPLE_REGS_USER) { | |
4018994f JO |
7708 | /* regs dump ABI info */ |
7709 | int size = sizeof(u64); | |
7710 | ||
4018994f JO |
7711 | if (data->regs_user.regs) { |
7712 | u64 mask = event->attr.sample_regs_user; | |
7713 | size += hweight64(mask) * sizeof(u64); | |
7714 | } | |
7715 | ||
4cf7a136 | 7716 | data->dyn_size += size; |
bb447c27 | 7717 | data->sample_flags |= PERF_SAMPLE_REGS_USER; |
4018994f | 7718 | } |
c5ebcedb | 7719 | |
bb447c27 | 7720 | if (filtered_sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb | 7721 | /* |
9f014e3a | 7722 | * Either we need PERF_SAMPLE_STACK_USER bit to be always |
c5ebcedb JO |
7723 | * processed as the last one or have additional check added |
7724 | * in case new sample type is added, because we could eat | |
7725 | * up the rest of the sample size. | |
7726 | */ | |
c5ebcedb | 7727 | u16 stack_size = event->attr.sample_stack_user; |
f6e70715 | 7728 | u16 header_size = perf_sample_data_size(data, event); |
c5ebcedb JO |
7729 | u16 size = sizeof(u64); |
7730 | ||
f6e70715 | 7731 | stack_size = perf_sample_ustack_size(stack_size, header_size, |
2565711f | 7732 | data->regs_user.regs); |
c5ebcedb JO |
7733 | |
7734 | /* | |
7735 | * If there is something to dump, add space for the dump | |
7736 | * itself and for the field that tells the dynamic size, | |
7737 | * which is how many have been actually dumped. | |
7738 | */ | |
7739 | if (stack_size) | |
7740 | size += sizeof(u64) + stack_size; | |
7741 | ||
7742 | data->stack_user_size = stack_size; | |
4cf7a136 | 7743 | data->dyn_size += size; |
bb447c27 | 7744 | data->sample_flags |= PERF_SAMPLE_STACK_USER; |
c5ebcedb | 7745 | } |
60e2364e | 7746 | |
bb447c27 | 7747 | if (filtered_sample_type & PERF_SAMPLE_WEIGHT_TYPE) { |
2abe681d | 7748 | data->weight.full = 0; |
bb447c27 NK |
7749 | data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; |
7750 | } | |
2abe681d | 7751 | |
bb447c27 | 7752 | if (filtered_sample_type & PERF_SAMPLE_DATA_SRC) { |
e16fd7f2 | 7753 | data->data_src.val = PERF_MEM_NA; |
bb447c27 NK |
7754 | data->sample_flags |= PERF_SAMPLE_DATA_SRC; |
7755 | } | |
e16fd7f2 | 7756 | |
bb447c27 | 7757 | if (filtered_sample_type & PERF_SAMPLE_TRANSACTION) { |
ee9db0e1 | 7758 | data->txn = 0; |
bb447c27 NK |
7759 | data->sample_flags |= PERF_SAMPLE_TRANSACTION; |
7760 | } | |
ee9db0e1 | 7761 | |
bb447c27 NK |
7762 | if (filtered_sample_type & PERF_SAMPLE_ADDR) { |
7763 | data->addr = 0; | |
7764 | data->sample_flags |= PERF_SAMPLE_ADDR; | |
7b084630 NK |
7765 | } |
7766 | ||
bb447c27 | 7767 | if (filtered_sample_type & PERF_SAMPLE_REGS_INTR) { |
60e2364e SE |
7768 | /* regs dump ABI info */ |
7769 | int size = sizeof(u64); | |
7770 | ||
7771 | perf_sample_regs_intr(&data->regs_intr, regs); | |
7772 | ||
7773 | if (data->regs_intr.regs) { | |
7774 | u64 mask = event->attr.sample_regs_intr; | |
7775 | ||
7776 | size += hweight64(mask) * sizeof(u64); | |
7777 | } | |
7778 | ||
4cf7a136 | 7779 | data->dyn_size += size; |
bb447c27 | 7780 | data->sample_flags |= PERF_SAMPLE_REGS_INTR; |
60e2364e | 7781 | } |
fc7ce9c7 | 7782 | |
bb447c27 | 7783 | if (filtered_sample_type & PERF_SAMPLE_PHYS_ADDR) { |
fc7ce9c7 | 7784 | data->phys_addr = perf_virt_to_phys(data->addr); |
bb447c27 NK |
7785 | data->sample_flags |= PERF_SAMPLE_PHYS_ADDR; |
7786 | } | |
a4faf00d | 7787 | |
6546b19f | 7788 | #ifdef CONFIG_CGROUP_PERF |
bb447c27 | 7789 | if (filtered_sample_type & PERF_SAMPLE_CGROUP) { |
6546b19f NK |
7790 | struct cgroup *cgrp; |
7791 | ||
7792 | /* protected by RCU */ | |
7793 | cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup; | |
7794 | data->cgroup = cgroup_id(cgrp); | |
bb447c27 | 7795 | data->sample_flags |= PERF_SAMPLE_CGROUP; |
6546b19f NK |
7796 | } |
7797 | #endif | |
7798 | ||
8d97e718 KL |
7799 | /* |
7800 | * PERF_DATA_PAGE_SIZE requires PERF_SAMPLE_ADDR. If the user doesn't | |
7801 | * require PERF_SAMPLE_ADDR, kernel implicitly retrieve the data->addr, | |
7802 | * but the value will not dump to the userspace. | |
7803 | */ | |
bb447c27 | 7804 | if (filtered_sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) { |
8d97e718 | 7805 | data->data_page_size = perf_get_page_size(data->addr); |
bb447c27 NK |
7806 | data->sample_flags |= PERF_SAMPLE_DATA_PAGE_SIZE; |
7807 | } | |
8d97e718 | 7808 | |
bb447c27 | 7809 | if (filtered_sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) { |
995f088e | 7810 | data->code_page_size = perf_get_page_size(data->ip); |
bb447c27 NK |
7811 | data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE; |
7812 | } | |
995f088e | 7813 | |
bb447c27 | 7814 | if (filtered_sample_type & PERF_SAMPLE_AUX) { |
a4faf00d | 7815 | u64 size; |
f6e70715 | 7816 | u16 header_size = perf_sample_data_size(data, event); |
a4faf00d | 7817 | |
f6e70715 | 7818 | header_size += sizeof(u64); /* size */ |
a4faf00d AS |
7819 | |
7820 | /* | |
7821 | * Given the 16bit nature of header::size, an AUX sample can | |
7822 | * easily overflow it, what with all the preceding sample bits. | |
7823 | * Make sure this doesn't happen by using up to U16_MAX bytes | |
7824 | * per sample in total (rounded down to 8 byte boundary). | |
7825 | */ | |
f6e70715 | 7826 | size = min_t(size_t, U16_MAX - header_size, |
a4faf00d AS |
7827 | event->attr.aux_sample_size); |
7828 | size = rounddown(size, 8); | |
7829 | size = perf_prepare_sample_aux(event, data, size); | |
7830 | ||
f6e70715 | 7831 | WARN_ON_ONCE(size + header_size > U16_MAX); |
4cf7a136 | 7832 | data->dyn_size += size + sizeof(u64); /* size above */ |
bb447c27 | 7833 | data->sample_flags |= PERF_SAMPLE_AUX; |
a4faf00d | 7834 | } |
f6e70715 | 7835 | } |
4cf7a136 | 7836 | |
f6e70715 NK |
7837 | void perf_prepare_header(struct perf_event_header *header, |
7838 | struct perf_sample_data *data, | |
7839 | struct perf_event *event, | |
7840 | struct pt_regs *regs) | |
7841 | { | |
7842 | header->type = PERF_RECORD_SAMPLE; | |
7843 | header->size = perf_sample_data_size(data, event); | |
7844 | header->misc = perf_misc_flags(regs); | |
4cf7a136 | 7845 | |
a4faf00d AS |
7846 | /* |
7847 | * If you're adding more sample types here, you likely need to do | |
7848 | * something about the overflowing header::size, like repurpose the | |
7849 | * lowest 3 bits of size, which should be always zero at the moment. | |
7850 | * This raises a more important question, do we really need 512k sized | |
7851 | * samples and why, so good argumentation is in order for whatever you | |
7852 | * do here next. | |
7853 | */ | |
7854 | WARN_ON_ONCE(header->size & 7); | |
5622f295 | 7855 | } |
7f453c24 | 7856 | |
56201969 | 7857 | static __always_inline int |
9ecda41a WN |
7858 | __perf_event_output(struct perf_event *event, |
7859 | struct perf_sample_data *data, | |
7860 | struct pt_regs *regs, | |
7861 | int (*output_begin)(struct perf_output_handle *, | |
267fb273 | 7862 | struct perf_sample_data *, |
9ecda41a WN |
7863 | struct perf_event *, |
7864 | unsigned int)) | |
5622f295 MM |
7865 | { |
7866 | struct perf_output_handle handle; | |
7867 | struct perf_event_header header; | |
56201969 | 7868 | int err; |
689802b2 | 7869 | |
927c7a9e FW |
7870 | /* protect the callchain buffers */ |
7871 | rcu_read_lock(); | |
7872 | ||
f6e70715 NK |
7873 | perf_prepare_sample(data, event, regs); |
7874 | perf_prepare_header(&header, data, event, regs); | |
5c148194 | 7875 | |
267fb273 | 7876 | err = output_begin(&handle, data, event, header.size); |
56201969 | 7877 | if (err) |
927c7a9e | 7878 | goto exit; |
0322cd6e | 7879 | |
cdd6c482 | 7880 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 7881 | |
8a057d84 | 7882 | perf_output_end(&handle); |
927c7a9e FW |
7883 | |
7884 | exit: | |
7885 | rcu_read_unlock(); | |
56201969 | 7886 | return err; |
0322cd6e PZ |
7887 | } |
7888 | ||
9ecda41a WN |
7889 | void |
7890 | perf_event_output_forward(struct perf_event *event, | |
7891 | struct perf_sample_data *data, | |
7892 | struct pt_regs *regs) | |
7893 | { | |
7894 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
7895 | } | |
7896 | ||
7897 | void | |
7898 | perf_event_output_backward(struct perf_event *event, | |
7899 | struct perf_sample_data *data, | |
7900 | struct pt_regs *regs) | |
7901 | { | |
7902 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
7903 | } | |
7904 | ||
56201969 | 7905 | int |
9ecda41a WN |
7906 | perf_event_output(struct perf_event *event, |
7907 | struct perf_sample_data *data, | |
7908 | struct pt_regs *regs) | |
7909 | { | |
56201969 | 7910 | return __perf_event_output(event, data, regs, perf_output_begin); |
9ecda41a WN |
7911 | } |
7912 | ||
38b200d6 | 7913 | /* |
cdd6c482 | 7914 | * read event_id |
38b200d6 PZ |
7915 | */ |
7916 | ||
7917 | struct perf_read_event { | |
7918 | struct perf_event_header header; | |
7919 | ||
7920 | u32 pid; | |
7921 | u32 tid; | |
38b200d6 PZ |
7922 | }; |
7923 | ||
7924 | static void | |
cdd6c482 | 7925 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
7926 | struct task_struct *task) |
7927 | { | |
7928 | struct perf_output_handle handle; | |
c980d109 | 7929 | struct perf_sample_data sample; |
dfc65094 | 7930 | struct perf_read_event read_event = { |
38b200d6 | 7931 | .header = { |
cdd6c482 | 7932 | .type = PERF_RECORD_READ, |
38b200d6 | 7933 | .misc = 0, |
c320c7b7 | 7934 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 7935 | }, |
cdd6c482 IM |
7936 | .pid = perf_event_pid(event, task), |
7937 | .tid = perf_event_tid(event, task), | |
38b200d6 | 7938 | }; |
3dab77fb | 7939 | int ret; |
38b200d6 | 7940 | |
c980d109 | 7941 | perf_event_header__init_id(&read_event.header, &sample, event); |
267fb273 | 7942 | ret = perf_output_begin(&handle, &sample, event, read_event.header.size); |
38b200d6 PZ |
7943 | if (ret) |
7944 | return; | |
7945 | ||
dfc65094 | 7946 | perf_output_put(&handle, read_event); |
cdd6c482 | 7947 | perf_output_read(&handle, event); |
c980d109 | 7948 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 7949 | |
38b200d6 PZ |
7950 | perf_output_end(&handle); |
7951 | } | |
7952 | ||
aab5b71e | 7953 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
7954 | |
7955 | static void | |
aab5b71e PZ |
7956 | perf_iterate_ctx(struct perf_event_context *ctx, |
7957 | perf_iterate_f output, | |
b73e4fef | 7958 | void *data, bool all) |
52d857a8 JO |
7959 | { |
7960 | struct perf_event *event; | |
7961 | ||
7962 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
7963 | if (!all) { |
7964 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
7965 | continue; | |
7966 | if (!event_filter_match(event)) | |
7967 | continue; | |
7968 | } | |
7969 | ||
67516844 | 7970 | output(event, data); |
52d857a8 JO |
7971 | } |
7972 | } | |
7973 | ||
aab5b71e | 7974 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
7975 | { |
7976 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
7977 | struct perf_event *event; | |
7978 | ||
7979 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
7980 | /* |
7981 | * Skip events that are not fully formed yet; ensure that | |
7982 | * if we observe event->ctx, both event and ctx will be | |
7983 | * complete enough. See perf_install_in_context(). | |
7984 | */ | |
7985 | if (!smp_load_acquire(&event->ctx)) | |
7986 | continue; | |
7987 | ||
f2fb6bef KL |
7988 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
7989 | continue; | |
7990 | if (!event_filter_match(event)) | |
7991 | continue; | |
7992 | output(event, data); | |
7993 | } | |
7994 | } | |
7995 | ||
aab5b71e PZ |
7996 | /* |
7997 | * Iterate all events that need to receive side-band events. | |
7998 | * | |
7999 | * For new callers; ensure that account_pmu_sb_event() includes | |
8000 | * your event, otherwise it might not get delivered. | |
8001 | */ | |
52d857a8 | 8002 | static void |
aab5b71e | 8003 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
8004 | struct perf_event_context *task_ctx) |
8005 | { | |
52d857a8 | 8006 | struct perf_event_context *ctx; |
52d857a8 | 8007 | |
aab5b71e PZ |
8008 | rcu_read_lock(); |
8009 | preempt_disable(); | |
8010 | ||
4e93ad60 | 8011 | /* |
aab5b71e PZ |
8012 | * If we have task_ctx != NULL we only notify the task context itself. |
8013 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
8014 | * context. |
8015 | */ | |
8016 | if (task_ctx) { | |
aab5b71e PZ |
8017 | perf_iterate_ctx(task_ctx, output, data, false); |
8018 | goto done; | |
4e93ad60 JO |
8019 | } |
8020 | ||
aab5b71e | 8021 | perf_iterate_sb_cpu(output, data); |
f2fb6bef | 8022 | |
bd275681 PZ |
8023 | ctx = rcu_dereference(current->perf_event_ctxp); |
8024 | if (ctx) | |
8025 | perf_iterate_ctx(ctx, output, data, false); | |
aab5b71e | 8026 | done: |
f2fb6bef | 8027 | preempt_enable(); |
52d857a8 | 8028 | rcu_read_unlock(); |
95ff4ca2 AS |
8029 | } |
8030 | ||
375637bc AS |
8031 | /* |
8032 | * Clear all file-based filters at exec, they'll have to be | |
8033 | * re-instated when/if these objects are mmapped again. | |
8034 | */ | |
8035 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
8036 | { | |
8037 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8038 | struct perf_addr_filter *filter; | |
8039 | unsigned int restart = 0, count = 0; | |
8040 | unsigned long flags; | |
8041 | ||
8042 | if (!has_addr_filter(event)) | |
8043 | return; | |
8044 | ||
8045 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8046 | list_for_each_entry(filter, &ifh->list, entry) { | |
9511bce9 | 8047 | if (filter->path.dentry) { |
c60f83b8 AS |
8048 | event->addr_filter_ranges[count].start = 0; |
8049 | event->addr_filter_ranges[count].size = 0; | |
375637bc AS |
8050 | restart++; |
8051 | } | |
8052 | ||
8053 | count++; | |
8054 | } | |
8055 | ||
8056 | if (restart) | |
8057 | event->addr_filters_gen++; | |
8058 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8059 | ||
8060 | if (restart) | |
767ae086 | 8061 | perf_event_stop(event, 1); |
375637bc AS |
8062 | } |
8063 | ||
8064 | void perf_event_exec(void) | |
8065 | { | |
8066 | struct perf_event_context *ctx; | |
375637bc | 8067 | |
bd275681 PZ |
8068 | ctx = perf_pin_task_context(current); |
8069 | if (!ctx) | |
8070 | return; | |
375637bc | 8071 | |
bd275681 PZ |
8072 | perf_event_enable_on_exec(ctx); |
8073 | perf_event_remove_on_exec(ctx); | |
8074 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); | |
375637bc | 8075 | |
bd275681 PZ |
8076 | perf_unpin_context(ctx); |
8077 | put_ctx(ctx); | |
375637bc AS |
8078 | } |
8079 | ||
95ff4ca2 | 8080 | struct remote_output { |
56de4e8f | 8081 | struct perf_buffer *rb; |
95ff4ca2 AS |
8082 | int err; |
8083 | }; | |
8084 | ||
8085 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
8086 | { | |
8087 | struct perf_event *parent = event->parent; | |
8088 | struct remote_output *ro = data; | |
56de4e8f | 8089 | struct perf_buffer *rb = ro->rb; |
375637bc AS |
8090 | struct stop_event_data sd = { |
8091 | .event = event, | |
8092 | }; | |
95ff4ca2 AS |
8093 | |
8094 | if (!has_aux(event)) | |
8095 | return; | |
8096 | ||
8097 | if (!parent) | |
8098 | parent = event; | |
8099 | ||
8100 | /* | |
8101 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
8102 | * ring-buffer, but it will be the child that's actually using it. |
8103 | * | |
8104 | * We are using event::rb to determine if the event should be stopped, | |
8105 | * however this may race with ring_buffer_attach() (through set_output), | |
8106 | * which will make us skip the event that actually needs to be stopped. | |
8107 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
8108 | * its rb pointer. | |
95ff4ca2 AS |
8109 | */ |
8110 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 8111 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
8112 | } |
8113 | ||
8114 | static int __perf_pmu_output_stop(void *info) | |
8115 | { | |
8116 | struct perf_event *event = info; | |
bd275681 | 8117 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
95ff4ca2 AS |
8118 | struct remote_output ro = { |
8119 | .rb = event->rb, | |
8120 | }; | |
8121 | ||
8122 | rcu_read_lock(); | |
aab5b71e | 8123 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 8124 | if (cpuctx->task_ctx) |
aab5b71e | 8125 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 8126 | &ro, false); |
95ff4ca2 AS |
8127 | rcu_read_unlock(); |
8128 | ||
8129 | return ro.err; | |
8130 | } | |
8131 | ||
8132 | static void perf_pmu_output_stop(struct perf_event *event) | |
8133 | { | |
8134 | struct perf_event *iter; | |
8135 | int err, cpu; | |
8136 | ||
8137 | restart: | |
8138 | rcu_read_lock(); | |
8139 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
8140 | /* | |
8141 | * For per-CPU events, we need to make sure that neither they | |
8142 | * nor their children are running; for cpu==-1 events it's | |
8143 | * sufficient to stop the event itself if it's active, since | |
8144 | * it can't have children. | |
8145 | */ | |
8146 | cpu = iter->cpu; | |
8147 | if (cpu == -1) | |
8148 | cpu = READ_ONCE(iter->oncpu); | |
8149 | ||
8150 | if (cpu == -1) | |
8151 | continue; | |
8152 | ||
8153 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
8154 | if (err == -EAGAIN) { | |
8155 | rcu_read_unlock(); | |
8156 | goto restart; | |
8157 | } | |
8158 | } | |
8159 | rcu_read_unlock(); | |
52d857a8 JO |
8160 | } |
8161 | ||
60313ebe | 8162 | /* |
9f498cc5 PZ |
8163 | * task tracking -- fork/exit |
8164 | * | |
13d7a241 | 8165 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
8166 | */ |
8167 | ||
9f498cc5 | 8168 | struct perf_task_event { |
3a80b4a3 | 8169 | struct task_struct *task; |
cdd6c482 | 8170 | struct perf_event_context *task_ctx; |
60313ebe PZ |
8171 | |
8172 | struct { | |
8173 | struct perf_event_header header; | |
8174 | ||
8175 | u32 pid; | |
8176 | u32 ppid; | |
9f498cc5 PZ |
8177 | u32 tid; |
8178 | u32 ptid; | |
393b2ad8 | 8179 | u64 time; |
cdd6c482 | 8180 | } event_id; |
60313ebe PZ |
8181 | }; |
8182 | ||
67516844 JO |
8183 | static int perf_event_task_match(struct perf_event *event) |
8184 | { | |
13d7a241 SE |
8185 | return event->attr.comm || event->attr.mmap || |
8186 | event->attr.mmap2 || event->attr.mmap_data || | |
8187 | event->attr.task; | |
67516844 JO |
8188 | } |
8189 | ||
cdd6c482 | 8190 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 8191 | void *data) |
60313ebe | 8192 | { |
52d857a8 | 8193 | struct perf_task_event *task_event = data; |
60313ebe | 8194 | struct perf_output_handle handle; |
c980d109 | 8195 | struct perf_sample_data sample; |
9f498cc5 | 8196 | struct task_struct *task = task_event->task; |
c980d109 | 8197 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 8198 | |
67516844 JO |
8199 | if (!perf_event_task_match(event)) |
8200 | return; | |
8201 | ||
c980d109 | 8202 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 8203 | |
267fb273 | 8204 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8205 | task_event->event_id.header.size); |
ef60777c | 8206 | if (ret) |
c980d109 | 8207 | goto out; |
60313ebe | 8208 | |
cdd6c482 | 8209 | task_event->event_id.pid = perf_event_pid(event, task); |
cdd6c482 | 8210 | task_event->event_id.tid = perf_event_tid(event, task); |
f3bed55e IR |
8211 | |
8212 | if (task_event->event_id.header.type == PERF_RECORD_EXIT) { | |
8213 | task_event->event_id.ppid = perf_event_pid(event, | |
8214 | task->real_parent); | |
8215 | task_event->event_id.ptid = perf_event_pid(event, | |
8216 | task->real_parent); | |
8217 | } else { /* PERF_RECORD_FORK */ | |
8218 | task_event->event_id.ppid = perf_event_pid(event, current); | |
8219 | task_event->event_id.ptid = perf_event_tid(event, current); | |
8220 | } | |
9f498cc5 | 8221 | |
34f43927 PZ |
8222 | task_event->event_id.time = perf_event_clock(event); |
8223 | ||
cdd6c482 | 8224 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 8225 | |
c980d109 ACM |
8226 | perf_event__output_id_sample(event, &handle, &sample); |
8227 | ||
60313ebe | 8228 | perf_output_end(&handle); |
c980d109 ACM |
8229 | out: |
8230 | task_event->event_id.header.size = size; | |
60313ebe PZ |
8231 | } |
8232 | ||
cdd6c482 IM |
8233 | static void perf_event_task(struct task_struct *task, |
8234 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 8235 | int new) |
60313ebe | 8236 | { |
9f498cc5 | 8237 | struct perf_task_event task_event; |
60313ebe | 8238 | |
cdd6c482 IM |
8239 | if (!atomic_read(&nr_comm_events) && |
8240 | !atomic_read(&nr_mmap_events) && | |
8241 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
8242 | return; |
8243 | ||
9f498cc5 | 8244 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
8245 | .task = task, |
8246 | .task_ctx = task_ctx, | |
cdd6c482 | 8247 | .event_id = { |
60313ebe | 8248 | .header = { |
cdd6c482 | 8249 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 8250 | .misc = 0, |
cdd6c482 | 8251 | .size = sizeof(task_event.event_id), |
60313ebe | 8252 | }, |
573402db PZ |
8253 | /* .pid */ |
8254 | /* .ppid */ | |
9f498cc5 PZ |
8255 | /* .tid */ |
8256 | /* .ptid */ | |
34f43927 | 8257 | /* .time */ |
60313ebe PZ |
8258 | }, |
8259 | }; | |
8260 | ||
aab5b71e | 8261 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
8262 | &task_event, |
8263 | task_ctx); | |
9f498cc5 PZ |
8264 | } |
8265 | ||
cdd6c482 | 8266 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 8267 | { |
cdd6c482 | 8268 | perf_event_task(task, NULL, 1); |
e4222673 | 8269 | perf_event_namespaces(task); |
60313ebe PZ |
8270 | } |
8271 | ||
8d1b2d93 PZ |
8272 | /* |
8273 | * comm tracking | |
8274 | */ | |
8275 | ||
8276 | struct perf_comm_event { | |
22a4f650 IM |
8277 | struct task_struct *task; |
8278 | char *comm; | |
8d1b2d93 PZ |
8279 | int comm_size; |
8280 | ||
8281 | struct { | |
8282 | struct perf_event_header header; | |
8283 | ||
8284 | u32 pid; | |
8285 | u32 tid; | |
cdd6c482 | 8286 | } event_id; |
8d1b2d93 PZ |
8287 | }; |
8288 | ||
67516844 JO |
8289 | static int perf_event_comm_match(struct perf_event *event) |
8290 | { | |
8291 | return event->attr.comm; | |
8292 | } | |
8293 | ||
cdd6c482 | 8294 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 8295 | void *data) |
8d1b2d93 | 8296 | { |
52d857a8 | 8297 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 8298 | struct perf_output_handle handle; |
c980d109 | 8299 | struct perf_sample_data sample; |
cdd6c482 | 8300 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
8301 | int ret; |
8302 | ||
67516844 JO |
8303 | if (!perf_event_comm_match(event)) |
8304 | return; | |
8305 | ||
c980d109 | 8306 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
267fb273 | 8307 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8308 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
8309 | |
8310 | if (ret) | |
c980d109 | 8311 | goto out; |
8d1b2d93 | 8312 | |
cdd6c482 IM |
8313 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
8314 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 8315 | |
cdd6c482 | 8316 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 8317 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 8318 | comm_event->comm_size); |
c980d109 ACM |
8319 | |
8320 | perf_event__output_id_sample(event, &handle, &sample); | |
8321 | ||
8d1b2d93 | 8322 | perf_output_end(&handle); |
c980d109 ACM |
8323 | out: |
8324 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
8325 | } |
8326 | ||
cdd6c482 | 8327 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 8328 | { |
413ee3b4 | 8329 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 8330 | unsigned int size; |
8d1b2d93 | 8331 | |
413ee3b4 | 8332 | memset(comm, 0, sizeof(comm)); |
c9732f14 | 8333 | strscpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 8334 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
8335 | |
8336 | comm_event->comm = comm; | |
8337 | comm_event->comm_size = size; | |
8338 | ||
cdd6c482 | 8339 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 8340 | |
aab5b71e | 8341 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
8342 | comm_event, |
8343 | NULL); | |
8d1b2d93 PZ |
8344 | } |
8345 | ||
82b89778 | 8346 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 8347 | { |
9ee318a7 PZ |
8348 | struct perf_comm_event comm_event; |
8349 | ||
cdd6c482 | 8350 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 8351 | return; |
a63eaf34 | 8352 | |
9ee318a7 | 8353 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 8354 | .task = task, |
573402db PZ |
8355 | /* .comm */ |
8356 | /* .comm_size */ | |
cdd6c482 | 8357 | .event_id = { |
573402db | 8358 | .header = { |
cdd6c482 | 8359 | .type = PERF_RECORD_COMM, |
82b89778 | 8360 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
8361 | /* .size */ |
8362 | }, | |
8363 | /* .pid */ | |
8364 | /* .tid */ | |
8d1b2d93 PZ |
8365 | }, |
8366 | }; | |
8367 | ||
cdd6c482 | 8368 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
8369 | } |
8370 | ||
e4222673 HB |
8371 | /* |
8372 | * namespaces tracking | |
8373 | */ | |
8374 | ||
8375 | struct perf_namespaces_event { | |
8376 | struct task_struct *task; | |
8377 | ||
8378 | struct { | |
8379 | struct perf_event_header header; | |
8380 | ||
8381 | u32 pid; | |
8382 | u32 tid; | |
8383 | u64 nr_namespaces; | |
8384 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
8385 | } event_id; | |
8386 | }; | |
8387 | ||
8388 | static int perf_event_namespaces_match(struct perf_event *event) | |
8389 | { | |
8390 | return event->attr.namespaces; | |
8391 | } | |
8392 | ||
8393 | static void perf_event_namespaces_output(struct perf_event *event, | |
8394 | void *data) | |
8395 | { | |
8396 | struct perf_namespaces_event *namespaces_event = data; | |
8397 | struct perf_output_handle handle; | |
8398 | struct perf_sample_data sample; | |
34900ec5 | 8399 | u16 header_size = namespaces_event->event_id.header.size; |
e4222673 HB |
8400 | int ret; |
8401 | ||
8402 | if (!perf_event_namespaces_match(event)) | |
8403 | return; | |
8404 | ||
8405 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
8406 | &sample, event); | |
267fb273 | 8407 | ret = perf_output_begin(&handle, &sample, event, |
e4222673 HB |
8408 | namespaces_event->event_id.header.size); |
8409 | if (ret) | |
34900ec5 | 8410 | goto out; |
e4222673 HB |
8411 | |
8412 | namespaces_event->event_id.pid = perf_event_pid(event, | |
8413 | namespaces_event->task); | |
8414 | namespaces_event->event_id.tid = perf_event_tid(event, | |
8415 | namespaces_event->task); | |
8416 | ||
8417 | perf_output_put(&handle, namespaces_event->event_id); | |
8418 | ||
8419 | perf_event__output_id_sample(event, &handle, &sample); | |
8420 | ||
8421 | perf_output_end(&handle); | |
34900ec5 JO |
8422 | out: |
8423 | namespaces_event->event_id.header.size = header_size; | |
e4222673 HB |
8424 | } |
8425 | ||
8426 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
8427 | struct task_struct *task, | |
8428 | const struct proc_ns_operations *ns_ops) | |
8429 | { | |
8430 | struct path ns_path; | |
8431 | struct inode *ns_inode; | |
ce623f89 | 8432 | int error; |
e4222673 HB |
8433 | |
8434 | error = ns_get_path(&ns_path, task, ns_ops); | |
8435 | if (!error) { | |
8436 | ns_inode = ns_path.dentry->d_inode; | |
8437 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
8438 | ns_link_info->ino = ns_inode->i_ino; | |
0e18dd12 | 8439 | path_put(&ns_path); |
e4222673 HB |
8440 | } |
8441 | } | |
8442 | ||
8443 | void perf_event_namespaces(struct task_struct *task) | |
8444 | { | |
8445 | struct perf_namespaces_event namespaces_event; | |
8446 | struct perf_ns_link_info *ns_link_info; | |
8447 | ||
8448 | if (!atomic_read(&nr_namespaces_events)) | |
8449 | return; | |
8450 | ||
8451 | namespaces_event = (struct perf_namespaces_event){ | |
8452 | .task = task, | |
8453 | .event_id = { | |
8454 | .header = { | |
8455 | .type = PERF_RECORD_NAMESPACES, | |
8456 | .misc = 0, | |
8457 | .size = sizeof(namespaces_event.event_id), | |
8458 | }, | |
8459 | /* .pid */ | |
8460 | /* .tid */ | |
8461 | .nr_namespaces = NR_NAMESPACES, | |
8462 | /* .link_info[NR_NAMESPACES] */ | |
8463 | }, | |
8464 | }; | |
8465 | ||
8466 | ns_link_info = namespaces_event.event_id.link_info; | |
8467 | ||
8468 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
8469 | task, &mntns_operations); | |
8470 | ||
8471 | #ifdef CONFIG_USER_NS | |
8472 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
8473 | task, &userns_operations); | |
8474 | #endif | |
8475 | #ifdef CONFIG_NET_NS | |
8476 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
8477 | task, &netns_operations); | |
8478 | #endif | |
8479 | #ifdef CONFIG_UTS_NS | |
8480 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
8481 | task, &utsns_operations); | |
8482 | #endif | |
8483 | #ifdef CONFIG_IPC_NS | |
8484 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
8485 | task, &ipcns_operations); | |
8486 | #endif | |
8487 | #ifdef CONFIG_PID_NS | |
8488 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
8489 | task, &pidns_operations); | |
8490 | #endif | |
8491 | #ifdef CONFIG_CGROUPS | |
8492 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
8493 | task, &cgroupns_operations); | |
8494 | #endif | |
8495 | ||
8496 | perf_iterate_sb(perf_event_namespaces_output, | |
8497 | &namespaces_event, | |
8498 | NULL); | |
8499 | } | |
8500 | ||
96aaab68 NK |
8501 | /* |
8502 | * cgroup tracking | |
8503 | */ | |
8504 | #ifdef CONFIG_CGROUP_PERF | |
8505 | ||
8506 | struct perf_cgroup_event { | |
8507 | char *path; | |
8508 | int path_size; | |
8509 | struct { | |
8510 | struct perf_event_header header; | |
8511 | u64 id; | |
8512 | char path[]; | |
8513 | } event_id; | |
8514 | }; | |
8515 | ||
8516 | static int perf_event_cgroup_match(struct perf_event *event) | |
8517 | { | |
8518 | return event->attr.cgroup; | |
8519 | } | |
8520 | ||
8521 | static void perf_event_cgroup_output(struct perf_event *event, void *data) | |
8522 | { | |
8523 | struct perf_cgroup_event *cgroup_event = data; | |
8524 | struct perf_output_handle handle; | |
8525 | struct perf_sample_data sample; | |
8526 | u16 header_size = cgroup_event->event_id.header.size; | |
8527 | int ret; | |
8528 | ||
8529 | if (!perf_event_cgroup_match(event)) | |
8530 | return; | |
8531 | ||
8532 | perf_event_header__init_id(&cgroup_event->event_id.header, | |
8533 | &sample, event); | |
267fb273 | 8534 | ret = perf_output_begin(&handle, &sample, event, |
96aaab68 NK |
8535 | cgroup_event->event_id.header.size); |
8536 | if (ret) | |
8537 | goto out; | |
8538 | ||
8539 | perf_output_put(&handle, cgroup_event->event_id); | |
8540 | __output_copy(&handle, cgroup_event->path, cgroup_event->path_size); | |
8541 | ||
8542 | perf_event__output_id_sample(event, &handle, &sample); | |
8543 | ||
8544 | perf_output_end(&handle); | |
8545 | out: | |
8546 | cgroup_event->event_id.header.size = header_size; | |
8547 | } | |
8548 | ||
8549 | static void perf_event_cgroup(struct cgroup *cgrp) | |
8550 | { | |
8551 | struct perf_cgroup_event cgroup_event; | |
8552 | char path_enomem[16] = "//enomem"; | |
8553 | char *pathname; | |
8554 | size_t size; | |
8555 | ||
8556 | if (!atomic_read(&nr_cgroup_events)) | |
8557 | return; | |
8558 | ||
8559 | cgroup_event = (struct perf_cgroup_event){ | |
8560 | .event_id = { | |
8561 | .header = { | |
8562 | .type = PERF_RECORD_CGROUP, | |
8563 | .misc = 0, | |
8564 | .size = sizeof(cgroup_event.event_id), | |
8565 | }, | |
8566 | .id = cgroup_id(cgrp), | |
8567 | }, | |
8568 | }; | |
8569 | ||
8570 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); | |
8571 | if (pathname == NULL) { | |
8572 | cgroup_event.path = path_enomem; | |
8573 | } else { | |
8574 | /* just to be sure to have enough space for alignment */ | |
8575 | cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64)); | |
8576 | cgroup_event.path = pathname; | |
8577 | } | |
8578 | ||
8579 | /* | |
8580 | * Since our buffer works in 8 byte units we need to align our string | |
8581 | * size to a multiple of 8. However, we must guarantee the tail end is | |
8582 | * zero'd out to avoid leaking random bits to userspace. | |
8583 | */ | |
8584 | size = strlen(cgroup_event.path) + 1; | |
8585 | while (!IS_ALIGNED(size, sizeof(u64))) | |
8586 | cgroup_event.path[size++] = '\0'; | |
8587 | ||
8588 | cgroup_event.event_id.header.size += size; | |
8589 | cgroup_event.path_size = size; | |
8590 | ||
8591 | perf_iterate_sb(perf_event_cgroup_output, | |
8592 | &cgroup_event, | |
8593 | NULL); | |
8594 | ||
8595 | kfree(pathname); | |
8596 | } | |
8597 | ||
8598 | #endif | |
8599 | ||
0a4a9391 PZ |
8600 | /* |
8601 | * mmap tracking | |
8602 | */ | |
8603 | ||
8604 | struct perf_mmap_event { | |
089dd79d PZ |
8605 | struct vm_area_struct *vma; |
8606 | ||
8607 | const char *file_name; | |
8608 | int file_size; | |
13d7a241 SE |
8609 | int maj, min; |
8610 | u64 ino; | |
8611 | u64 ino_generation; | |
f972eb63 | 8612 | u32 prot, flags; |
88a16a13 JO |
8613 | u8 build_id[BUILD_ID_SIZE_MAX]; |
8614 | u32 build_id_size; | |
0a4a9391 PZ |
8615 | |
8616 | struct { | |
8617 | struct perf_event_header header; | |
8618 | ||
8619 | u32 pid; | |
8620 | u32 tid; | |
8621 | u64 start; | |
8622 | u64 len; | |
8623 | u64 pgoff; | |
cdd6c482 | 8624 | } event_id; |
0a4a9391 PZ |
8625 | }; |
8626 | ||
67516844 JO |
8627 | static int perf_event_mmap_match(struct perf_event *event, |
8628 | void *data) | |
8629 | { | |
8630 | struct perf_mmap_event *mmap_event = data; | |
8631 | struct vm_area_struct *vma = mmap_event->vma; | |
8632 | int executable = vma->vm_flags & VM_EXEC; | |
8633 | ||
8634 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 8635 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
8636 | } |
8637 | ||
cdd6c482 | 8638 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 8639 | void *data) |
0a4a9391 | 8640 | { |
52d857a8 | 8641 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 8642 | struct perf_output_handle handle; |
c980d109 | 8643 | struct perf_sample_data sample; |
cdd6c482 | 8644 | int size = mmap_event->event_id.header.size; |
d9c1bb2f | 8645 | u32 type = mmap_event->event_id.header.type; |
88a16a13 | 8646 | bool use_build_id; |
c980d109 | 8647 | int ret; |
0a4a9391 | 8648 | |
67516844 JO |
8649 | if (!perf_event_mmap_match(event, data)) |
8650 | return; | |
8651 | ||
13d7a241 SE |
8652 | if (event->attr.mmap2) { |
8653 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
8654 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
8655 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
8656 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 8657 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
8658 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
8659 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
8660 | } |
8661 | ||
c980d109 | 8662 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
267fb273 | 8663 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 8664 | mmap_event->event_id.header.size); |
0a4a9391 | 8665 | if (ret) |
c980d109 | 8666 | goto out; |
0a4a9391 | 8667 | |
cdd6c482 IM |
8668 | mmap_event->event_id.pid = perf_event_pid(event, current); |
8669 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 8670 | |
88a16a13 JO |
8671 | use_build_id = event->attr.build_id && mmap_event->build_id_size; |
8672 | ||
8673 | if (event->attr.mmap2 && use_build_id) | |
8674 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID; | |
8675 | ||
cdd6c482 | 8676 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
8677 | |
8678 | if (event->attr.mmap2) { | |
88a16a13 JO |
8679 | if (use_build_id) { |
8680 | u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 }; | |
8681 | ||
8682 | __output_copy(&handle, size, 4); | |
8683 | __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX); | |
8684 | } else { | |
8685 | perf_output_put(&handle, mmap_event->maj); | |
8686 | perf_output_put(&handle, mmap_event->min); | |
8687 | perf_output_put(&handle, mmap_event->ino); | |
8688 | perf_output_put(&handle, mmap_event->ino_generation); | |
8689 | } | |
f972eb63 PZ |
8690 | perf_output_put(&handle, mmap_event->prot); |
8691 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
8692 | } |
8693 | ||
76369139 | 8694 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 8695 | mmap_event->file_size); |
c980d109 ACM |
8696 | |
8697 | perf_event__output_id_sample(event, &handle, &sample); | |
8698 | ||
78d613eb | 8699 | perf_output_end(&handle); |
c980d109 ACM |
8700 | out: |
8701 | mmap_event->event_id.header.size = size; | |
d9c1bb2f | 8702 | mmap_event->event_id.header.type = type; |
0a4a9391 PZ |
8703 | } |
8704 | ||
cdd6c482 | 8705 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 8706 | { |
089dd79d PZ |
8707 | struct vm_area_struct *vma = mmap_event->vma; |
8708 | struct file *file = vma->vm_file; | |
13d7a241 SE |
8709 | int maj = 0, min = 0; |
8710 | u64 ino = 0, gen = 0; | |
f972eb63 | 8711 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
8712 | unsigned int size; |
8713 | char tmp[16]; | |
8714 | char *buf = NULL; | |
549f5c77 | 8715 | char *name = NULL; |
413ee3b4 | 8716 | |
0b3589be PZ |
8717 | if (vma->vm_flags & VM_READ) |
8718 | prot |= PROT_READ; | |
8719 | if (vma->vm_flags & VM_WRITE) | |
8720 | prot |= PROT_WRITE; | |
8721 | if (vma->vm_flags & VM_EXEC) | |
8722 | prot |= PROT_EXEC; | |
8723 | ||
8724 | if (vma->vm_flags & VM_MAYSHARE) | |
8725 | flags = MAP_SHARED; | |
8726 | else | |
8727 | flags = MAP_PRIVATE; | |
8728 | ||
0b3589be PZ |
8729 | if (vma->vm_flags & VM_LOCKED) |
8730 | flags |= MAP_LOCKED; | |
03911132 | 8731 | if (is_vm_hugetlb_page(vma)) |
0b3589be PZ |
8732 | flags |= MAP_HUGETLB; |
8733 | ||
0a4a9391 | 8734 | if (file) { |
13d7a241 SE |
8735 | struct inode *inode; |
8736 | dev_t dev; | |
3ea2f2b9 | 8737 | |
2c42cfbf | 8738 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 8739 | if (!buf) { |
c7e548b4 ON |
8740 | name = "//enomem"; |
8741 | goto cpy_name; | |
0a4a9391 | 8742 | } |
413ee3b4 | 8743 | /* |
3ea2f2b9 | 8744 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
8745 | * need to add enough zero bytes after the string to handle |
8746 | * the 64bit alignment we do later. | |
8747 | */ | |
9bf39ab2 | 8748 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 8749 | if (IS_ERR(name)) { |
c7e548b4 ON |
8750 | name = "//toolong"; |
8751 | goto cpy_name; | |
0a4a9391 | 8752 | } |
13d7a241 SE |
8753 | inode = file_inode(vma->vm_file); |
8754 | dev = inode->i_sb->s_dev; | |
8755 | ino = inode->i_ino; | |
8756 | gen = inode->i_generation; | |
8757 | maj = MAJOR(dev); | |
8758 | min = MINOR(dev); | |
f972eb63 | 8759 | |
c7e548b4 | 8760 | goto got_name; |
0a4a9391 | 8761 | } else { |
549f5c77 | 8762 | if (vma->vm_ops && vma->vm_ops->name) |
fbe26abe | 8763 | name = (char *) vma->vm_ops->name(vma); |
549f5c77 KW |
8764 | if (!name) |
8765 | name = (char *)arch_vma_name(vma); | |
8766 | if (!name) { | |
8767 | if (vma_is_initial_heap(vma)) | |
8768 | name = "[heap]"; | |
8769 | else if (vma_is_initial_stack(vma)) | |
8770 | name = "[stack]"; | |
8771 | else | |
8772 | name = "//anon"; | |
fbe26abe | 8773 | } |
0a4a9391 PZ |
8774 | } |
8775 | ||
c7e548b4 | 8776 | cpy_name: |
c9732f14 | 8777 | strscpy(tmp, name, sizeof(tmp)); |
c7e548b4 | 8778 | name = tmp; |
0a4a9391 | 8779 | got_name: |
2c42cfbf PZ |
8780 | /* |
8781 | * Since our buffer works in 8 byte units we need to align our string | |
8782 | * size to a multiple of 8. However, we must guarantee the tail end is | |
8783 | * zero'd out to avoid leaking random bits to userspace. | |
8784 | */ | |
8785 | size = strlen(name)+1; | |
8786 | while (!IS_ALIGNED(size, sizeof(u64))) | |
8787 | name[size++] = '\0'; | |
0a4a9391 PZ |
8788 | |
8789 | mmap_event->file_name = name; | |
8790 | mmap_event->file_size = size; | |
13d7a241 SE |
8791 | mmap_event->maj = maj; |
8792 | mmap_event->min = min; | |
8793 | mmap_event->ino = ino; | |
8794 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
8795 | mmap_event->prot = prot; |
8796 | mmap_event->flags = flags; | |
0a4a9391 | 8797 | |
2fe85427 SE |
8798 | if (!(vma->vm_flags & VM_EXEC)) |
8799 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
8800 | ||
cdd6c482 | 8801 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 8802 | |
88a16a13 JO |
8803 | if (atomic_read(&nr_build_id_events)) |
8804 | build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size); | |
8805 | ||
aab5b71e | 8806 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
8807 | mmap_event, |
8808 | NULL); | |
665c2142 | 8809 | |
0a4a9391 PZ |
8810 | kfree(buf); |
8811 | } | |
8812 | ||
375637bc AS |
8813 | /* |
8814 | * Check whether inode and address range match filter criteria. | |
8815 | */ | |
8816 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
8817 | struct file *file, unsigned long offset, | |
8818 | unsigned long size) | |
8819 | { | |
7f635ff1 MP |
8820 | /* d_inode(NULL) won't be equal to any mapped user-space file */ |
8821 | if (!filter->path.dentry) | |
8822 | return false; | |
8823 | ||
9511bce9 | 8824 | if (d_inode(filter->path.dentry) != file_inode(file)) |
375637bc AS |
8825 | return false; |
8826 | ||
8827 | if (filter->offset > offset + size) | |
8828 | return false; | |
8829 | ||
8830 | if (filter->offset + filter->size < offset) | |
8831 | return false; | |
8832 | ||
8833 | return true; | |
8834 | } | |
8835 | ||
c60f83b8 AS |
8836 | static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter, |
8837 | struct vm_area_struct *vma, | |
8838 | struct perf_addr_filter_range *fr) | |
8839 | { | |
8840 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8841 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8842 | struct file *file = vma->vm_file; | |
8843 | ||
8844 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8845 | return false; | |
8846 | ||
8847 | if (filter->offset < off) { | |
8848 | fr->start = vma->vm_start; | |
8849 | fr->size = min(vma_size, filter->size - (off - filter->offset)); | |
8850 | } else { | |
8851 | fr->start = vma->vm_start + filter->offset - off; | |
8852 | fr->size = min(vma->vm_end - fr->start, filter->size); | |
8853 | } | |
8854 | ||
8855 | return true; | |
8856 | } | |
8857 | ||
375637bc AS |
8858 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
8859 | { | |
8860 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8861 | struct vm_area_struct *vma = data; | |
375637bc AS |
8862 | struct perf_addr_filter *filter; |
8863 | unsigned int restart = 0, count = 0; | |
c60f83b8 | 8864 | unsigned long flags; |
375637bc AS |
8865 | |
8866 | if (!has_addr_filter(event)) | |
8867 | return; | |
8868 | ||
c60f83b8 | 8869 | if (!vma->vm_file) |
375637bc AS |
8870 | return; |
8871 | ||
8872 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8873 | list_for_each_entry(filter, &ifh->list, entry) { | |
c60f83b8 AS |
8874 | if (perf_addr_filter_vma_adjust(filter, vma, |
8875 | &event->addr_filter_ranges[count])) | |
375637bc | 8876 | restart++; |
375637bc AS |
8877 | |
8878 | count++; | |
8879 | } | |
8880 | ||
8881 | if (restart) | |
8882 | event->addr_filters_gen++; | |
8883 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8884 | ||
8885 | if (restart) | |
767ae086 | 8886 | perf_event_stop(event, 1); |
375637bc AS |
8887 | } |
8888 | ||
8889 | /* | |
8890 | * Adjust all task's events' filters to the new vma | |
8891 | */ | |
8892 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
8893 | { | |
8894 | struct perf_event_context *ctx; | |
375637bc | 8895 | |
12b40a23 MP |
8896 | /* |
8897 | * Data tracing isn't supported yet and as such there is no need | |
8898 | * to keep track of anything that isn't related to executable code: | |
8899 | */ | |
8900 | if (!(vma->vm_flags & VM_EXEC)) | |
8901 | return; | |
8902 | ||
375637bc | 8903 | rcu_read_lock(); |
bd275681 PZ |
8904 | ctx = rcu_dereference(current->perf_event_ctxp); |
8905 | if (ctx) | |
aab5b71e | 8906 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
8907 | rcu_read_unlock(); |
8908 | } | |
8909 | ||
3af9e859 | 8910 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 8911 | { |
9ee318a7 PZ |
8912 | struct perf_mmap_event mmap_event; |
8913 | ||
cdd6c482 | 8914 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
8915 | return; |
8916 | ||
8917 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 8918 | .vma = vma, |
573402db PZ |
8919 | /* .file_name */ |
8920 | /* .file_size */ | |
cdd6c482 | 8921 | .event_id = { |
573402db | 8922 | .header = { |
cdd6c482 | 8923 | .type = PERF_RECORD_MMAP, |
39447b38 | 8924 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
8925 | /* .size */ |
8926 | }, | |
8927 | /* .pid */ | |
8928 | /* .tid */ | |
089dd79d PZ |
8929 | .start = vma->vm_start, |
8930 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 8931 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 8932 | }, |
13d7a241 SE |
8933 | /* .maj (attr_mmap2 only) */ |
8934 | /* .min (attr_mmap2 only) */ | |
8935 | /* .ino (attr_mmap2 only) */ | |
8936 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
8937 | /* .prot (attr_mmap2 only) */ |
8938 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
8939 | }; |
8940 | ||
375637bc | 8941 | perf_addr_filters_adjust(vma); |
cdd6c482 | 8942 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
8943 | } |
8944 | ||
68db7e98 AS |
8945 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
8946 | unsigned long size, u64 flags) | |
8947 | { | |
8948 | struct perf_output_handle handle; | |
8949 | struct perf_sample_data sample; | |
8950 | struct perf_aux_event { | |
8951 | struct perf_event_header header; | |
8952 | u64 offset; | |
8953 | u64 size; | |
8954 | u64 flags; | |
8955 | } rec = { | |
8956 | .header = { | |
8957 | .type = PERF_RECORD_AUX, | |
8958 | .misc = 0, | |
8959 | .size = sizeof(rec), | |
8960 | }, | |
8961 | .offset = head, | |
8962 | .size = size, | |
8963 | .flags = flags, | |
8964 | }; | |
8965 | int ret; | |
8966 | ||
8967 | perf_event_header__init_id(&rec.header, &sample, event); | |
267fb273 | 8968 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); |
68db7e98 AS |
8969 | |
8970 | if (ret) | |
8971 | return; | |
8972 | ||
8973 | perf_output_put(&handle, rec); | |
8974 | perf_event__output_id_sample(event, &handle, &sample); | |
8975 | ||
8976 | perf_output_end(&handle); | |
8977 | } | |
8978 | ||
f38b0dbb KL |
8979 | /* |
8980 | * Lost/dropped samples logging | |
8981 | */ | |
8982 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
8983 | { | |
8984 | struct perf_output_handle handle; | |
8985 | struct perf_sample_data sample; | |
8986 | int ret; | |
8987 | ||
8988 | struct { | |
8989 | struct perf_event_header header; | |
8990 | u64 lost; | |
8991 | } lost_samples_event = { | |
8992 | .header = { | |
8993 | .type = PERF_RECORD_LOST_SAMPLES, | |
8994 | .misc = 0, | |
8995 | .size = sizeof(lost_samples_event), | |
8996 | }, | |
8997 | .lost = lost, | |
8998 | }; | |
8999 | ||
9000 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
9001 | ||
267fb273 | 9002 | ret = perf_output_begin(&handle, &sample, event, |
f38b0dbb KL |
9003 | lost_samples_event.header.size); |
9004 | if (ret) | |
9005 | return; | |
9006 | ||
9007 | perf_output_put(&handle, lost_samples_event); | |
9008 | perf_event__output_id_sample(event, &handle, &sample); | |
9009 | perf_output_end(&handle); | |
9010 | } | |
9011 | ||
45ac1403 AH |
9012 | /* |
9013 | * context_switch tracking | |
9014 | */ | |
9015 | ||
9016 | struct perf_switch_event { | |
9017 | struct task_struct *task; | |
9018 | struct task_struct *next_prev; | |
9019 | ||
9020 | struct { | |
9021 | struct perf_event_header header; | |
9022 | u32 next_prev_pid; | |
9023 | u32 next_prev_tid; | |
9024 | } event_id; | |
9025 | }; | |
9026 | ||
9027 | static int perf_event_switch_match(struct perf_event *event) | |
9028 | { | |
9029 | return event->attr.context_switch; | |
9030 | } | |
9031 | ||
9032 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
9033 | { | |
9034 | struct perf_switch_event *se = data; | |
9035 | struct perf_output_handle handle; | |
9036 | struct perf_sample_data sample; | |
9037 | int ret; | |
9038 | ||
9039 | if (!perf_event_switch_match(event)) | |
9040 | return; | |
9041 | ||
9042 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
9043 | if (event->ctx->task) { | |
9044 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
9045 | se->event_id.header.size = sizeof(se->event_id.header); | |
9046 | } else { | |
9047 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
9048 | se->event_id.header.size = sizeof(se->event_id); | |
9049 | se->event_id.next_prev_pid = | |
9050 | perf_event_pid(event, se->next_prev); | |
9051 | se->event_id.next_prev_tid = | |
9052 | perf_event_tid(event, se->next_prev); | |
9053 | } | |
9054 | ||
9055 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
9056 | ||
267fb273 | 9057 | ret = perf_output_begin(&handle, &sample, event, se->event_id.header.size); |
45ac1403 AH |
9058 | if (ret) |
9059 | return; | |
9060 | ||
9061 | if (event->ctx->task) | |
9062 | perf_output_put(&handle, se->event_id.header); | |
9063 | else | |
9064 | perf_output_put(&handle, se->event_id); | |
9065 | ||
9066 | perf_event__output_id_sample(event, &handle, &sample); | |
9067 | ||
9068 | perf_output_end(&handle); | |
9069 | } | |
9070 | ||
9071 | static void perf_event_switch(struct task_struct *task, | |
9072 | struct task_struct *next_prev, bool sched_in) | |
9073 | { | |
9074 | struct perf_switch_event switch_event; | |
9075 | ||
9076 | /* N.B. caller checks nr_switch_events != 0 */ | |
9077 | ||
9078 | switch_event = (struct perf_switch_event){ | |
9079 | .task = task, | |
9080 | .next_prev = next_prev, | |
9081 | .event_id = { | |
9082 | .header = { | |
9083 | /* .type */ | |
9084 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
9085 | /* .size */ | |
9086 | }, | |
9087 | /* .next_prev_pid */ | |
9088 | /* .next_prev_tid */ | |
9089 | }, | |
9090 | }; | |
9091 | ||
3ba9f93b | 9092 | if (!sched_in && task->on_rq) { |
101592b4 AB |
9093 | switch_event.event_id.header.misc |= |
9094 | PERF_RECORD_MISC_SWITCH_OUT_PREEMPT; | |
3ba9f93b | 9095 | } |
101592b4 | 9096 | |
3ba9f93b | 9097 | perf_iterate_sb(perf_event_switch_output, &switch_event, NULL); |
45ac1403 AH |
9098 | } |
9099 | ||
a78ac325 PZ |
9100 | /* |
9101 | * IRQ throttle logging | |
9102 | */ | |
9103 | ||
cdd6c482 | 9104 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
9105 | { |
9106 | struct perf_output_handle handle; | |
c980d109 | 9107 | struct perf_sample_data sample; |
a78ac325 PZ |
9108 | int ret; |
9109 | ||
9110 | struct { | |
9111 | struct perf_event_header header; | |
9112 | u64 time; | |
cca3f454 | 9113 | u64 id; |
7f453c24 | 9114 | u64 stream_id; |
a78ac325 PZ |
9115 | } throttle_event = { |
9116 | .header = { | |
cdd6c482 | 9117 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
9118 | .misc = 0, |
9119 | .size = sizeof(throttle_event), | |
9120 | }, | |
34f43927 | 9121 | .time = perf_event_clock(event), |
cdd6c482 IM |
9122 | .id = primary_event_id(event), |
9123 | .stream_id = event->id, | |
a78ac325 PZ |
9124 | }; |
9125 | ||
966ee4d6 | 9126 | if (enable) |
cdd6c482 | 9127 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 9128 | |
c980d109 ACM |
9129 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
9130 | ||
267fb273 | 9131 | ret = perf_output_begin(&handle, &sample, event, |
a7ac67ea | 9132 | throttle_event.header.size); |
a78ac325 PZ |
9133 | if (ret) |
9134 | return; | |
9135 | ||
9136 | perf_output_put(&handle, throttle_event); | |
c980d109 | 9137 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
9138 | perf_output_end(&handle); |
9139 | } | |
9140 | ||
76193a94 SL |
9141 | /* |
9142 | * ksymbol register/unregister tracking | |
9143 | */ | |
9144 | ||
9145 | struct perf_ksymbol_event { | |
9146 | const char *name; | |
9147 | int name_len; | |
9148 | struct { | |
9149 | struct perf_event_header header; | |
9150 | u64 addr; | |
9151 | u32 len; | |
9152 | u16 ksym_type; | |
9153 | u16 flags; | |
9154 | } event_id; | |
9155 | }; | |
9156 | ||
9157 | static int perf_event_ksymbol_match(struct perf_event *event) | |
9158 | { | |
9159 | return event->attr.ksymbol; | |
9160 | } | |
9161 | ||
9162 | static void perf_event_ksymbol_output(struct perf_event *event, void *data) | |
9163 | { | |
9164 | struct perf_ksymbol_event *ksymbol_event = data; | |
9165 | struct perf_output_handle handle; | |
9166 | struct perf_sample_data sample; | |
9167 | int ret; | |
9168 | ||
9169 | if (!perf_event_ksymbol_match(event)) | |
9170 | return; | |
9171 | ||
9172 | perf_event_header__init_id(&ksymbol_event->event_id.header, | |
9173 | &sample, event); | |
267fb273 | 9174 | ret = perf_output_begin(&handle, &sample, event, |
76193a94 SL |
9175 | ksymbol_event->event_id.header.size); |
9176 | if (ret) | |
9177 | return; | |
9178 | ||
9179 | perf_output_put(&handle, ksymbol_event->event_id); | |
9180 | __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len); | |
9181 | perf_event__output_id_sample(event, &handle, &sample); | |
9182 | ||
9183 | perf_output_end(&handle); | |
9184 | } | |
9185 | ||
9186 | void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister, | |
9187 | const char *sym) | |
9188 | { | |
9189 | struct perf_ksymbol_event ksymbol_event; | |
9190 | char name[KSYM_NAME_LEN]; | |
9191 | u16 flags = 0; | |
9192 | int name_len; | |
9193 | ||
9194 | if (!atomic_read(&nr_ksymbol_events)) | |
9195 | return; | |
9196 | ||
9197 | if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX || | |
9198 | ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN) | |
9199 | goto err; | |
9200 | ||
c9732f14 | 9201 | strscpy(name, sym, KSYM_NAME_LEN); |
76193a94 SL |
9202 | name_len = strlen(name) + 1; |
9203 | while (!IS_ALIGNED(name_len, sizeof(u64))) | |
9204 | name[name_len++] = '\0'; | |
9205 | BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64)); | |
9206 | ||
9207 | if (unregister) | |
9208 | flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER; | |
9209 | ||
9210 | ksymbol_event = (struct perf_ksymbol_event){ | |
9211 | .name = name, | |
9212 | .name_len = name_len, | |
9213 | .event_id = { | |
9214 | .header = { | |
9215 | .type = PERF_RECORD_KSYMBOL, | |
9216 | .size = sizeof(ksymbol_event.event_id) + | |
9217 | name_len, | |
9218 | }, | |
9219 | .addr = addr, | |
9220 | .len = len, | |
9221 | .ksym_type = ksym_type, | |
9222 | .flags = flags, | |
9223 | }, | |
9224 | }; | |
9225 | ||
9226 | perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL); | |
9227 | return; | |
9228 | err: | |
9229 | WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type); | |
9230 | } | |
9231 | ||
6ee52e2a SL |
9232 | /* |
9233 | * bpf program load/unload tracking | |
9234 | */ | |
9235 | ||
9236 | struct perf_bpf_event { | |
9237 | struct bpf_prog *prog; | |
9238 | struct { | |
9239 | struct perf_event_header header; | |
9240 | u16 type; | |
9241 | u16 flags; | |
9242 | u32 id; | |
9243 | u8 tag[BPF_TAG_SIZE]; | |
9244 | } event_id; | |
9245 | }; | |
9246 | ||
9247 | static int perf_event_bpf_match(struct perf_event *event) | |
9248 | { | |
9249 | return event->attr.bpf_event; | |
9250 | } | |
9251 | ||
9252 | static void perf_event_bpf_output(struct perf_event *event, void *data) | |
9253 | { | |
9254 | struct perf_bpf_event *bpf_event = data; | |
9255 | struct perf_output_handle handle; | |
9256 | struct perf_sample_data sample; | |
9257 | int ret; | |
9258 | ||
9259 | if (!perf_event_bpf_match(event)) | |
9260 | return; | |
9261 | ||
9262 | perf_event_header__init_id(&bpf_event->event_id.header, | |
9263 | &sample, event); | |
eb81a2ed | 9264 | ret = perf_output_begin(&handle, &sample, event, |
6ee52e2a SL |
9265 | bpf_event->event_id.header.size); |
9266 | if (ret) | |
9267 | return; | |
9268 | ||
9269 | perf_output_put(&handle, bpf_event->event_id); | |
9270 | perf_event__output_id_sample(event, &handle, &sample); | |
9271 | ||
9272 | perf_output_end(&handle); | |
9273 | } | |
9274 | ||
9275 | static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog, | |
9276 | enum perf_bpf_event_type type) | |
9277 | { | |
9278 | bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD; | |
6ee52e2a SL |
9279 | int i; |
9280 | ||
9281 | if (prog->aux->func_cnt == 0) { | |
6ee52e2a SL |
9282 | perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, |
9283 | (u64)(unsigned long)prog->bpf_func, | |
bfea9a85 JO |
9284 | prog->jited_len, unregister, |
9285 | prog->aux->ksym.name); | |
6ee52e2a SL |
9286 | } else { |
9287 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
9288 | struct bpf_prog *subprog = prog->aux->func[i]; | |
9289 | ||
6ee52e2a SL |
9290 | perf_event_ksymbol( |
9291 | PERF_RECORD_KSYMBOL_TYPE_BPF, | |
9292 | (u64)(unsigned long)subprog->bpf_func, | |
bfea9a85 | 9293 | subprog->jited_len, unregister, |
47df8a2f | 9294 | subprog->aux->ksym.name); |
6ee52e2a SL |
9295 | } |
9296 | } | |
9297 | } | |
9298 | ||
9299 | void perf_event_bpf_event(struct bpf_prog *prog, | |
9300 | enum perf_bpf_event_type type, | |
9301 | u16 flags) | |
9302 | { | |
9303 | struct perf_bpf_event bpf_event; | |
9304 | ||
6ee52e2a SL |
9305 | switch (type) { |
9306 | case PERF_BPF_EVENT_PROG_LOAD: | |
9307 | case PERF_BPF_EVENT_PROG_UNLOAD: | |
9308 | if (atomic_read(&nr_ksymbol_events)) | |
9309 | perf_event_bpf_emit_ksymbols(prog, type); | |
9310 | break; | |
9311 | default: | |
aecaa3ed | 9312 | return; |
6ee52e2a SL |
9313 | } |
9314 | ||
9315 | if (!atomic_read(&nr_bpf_events)) | |
9316 | return; | |
9317 | ||
9318 | bpf_event = (struct perf_bpf_event){ | |
9319 | .prog = prog, | |
9320 | .event_id = { | |
9321 | .header = { | |
9322 | .type = PERF_RECORD_BPF_EVENT, | |
9323 | .size = sizeof(bpf_event.event_id), | |
9324 | }, | |
9325 | .type = type, | |
9326 | .flags = flags, | |
9327 | .id = prog->aux->id, | |
9328 | }, | |
9329 | }; | |
9330 | ||
9331 | BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64)); | |
9332 | ||
9333 | memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE); | |
9334 | perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL); | |
9335 | } | |
9336 | ||
e17d43b9 AH |
9337 | struct perf_text_poke_event { |
9338 | const void *old_bytes; | |
9339 | const void *new_bytes; | |
9340 | size_t pad; | |
9341 | u16 old_len; | |
9342 | u16 new_len; | |
9343 | ||
9344 | struct { | |
9345 | struct perf_event_header header; | |
9346 | ||
9347 | u64 addr; | |
9348 | } event_id; | |
9349 | }; | |
9350 | ||
9351 | static int perf_event_text_poke_match(struct perf_event *event) | |
9352 | { | |
9353 | return event->attr.text_poke; | |
9354 | } | |
9355 | ||
9356 | static void perf_event_text_poke_output(struct perf_event *event, void *data) | |
9357 | { | |
9358 | struct perf_text_poke_event *text_poke_event = data; | |
9359 | struct perf_output_handle handle; | |
9360 | struct perf_sample_data sample; | |
9361 | u64 padding = 0; | |
9362 | int ret; | |
9363 | ||
9364 | if (!perf_event_text_poke_match(event)) | |
9365 | return; | |
9366 | ||
9367 | perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event); | |
9368 | ||
267fb273 PZ |
9369 | ret = perf_output_begin(&handle, &sample, event, |
9370 | text_poke_event->event_id.header.size); | |
e17d43b9 AH |
9371 | if (ret) |
9372 | return; | |
9373 | ||
9374 | perf_output_put(&handle, text_poke_event->event_id); | |
9375 | perf_output_put(&handle, text_poke_event->old_len); | |
9376 | perf_output_put(&handle, text_poke_event->new_len); | |
9377 | ||
9378 | __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len); | |
9379 | __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len); | |
9380 | ||
9381 | if (text_poke_event->pad) | |
9382 | __output_copy(&handle, &padding, text_poke_event->pad); | |
9383 | ||
9384 | perf_event__output_id_sample(event, &handle, &sample); | |
9385 | ||
9386 | perf_output_end(&handle); | |
9387 | } | |
9388 | ||
9389 | void perf_event_text_poke(const void *addr, const void *old_bytes, | |
9390 | size_t old_len, const void *new_bytes, size_t new_len) | |
9391 | { | |
9392 | struct perf_text_poke_event text_poke_event; | |
9393 | size_t tot, pad; | |
9394 | ||
9395 | if (!atomic_read(&nr_text_poke_events)) | |
9396 | return; | |
9397 | ||
9398 | tot = sizeof(text_poke_event.old_len) + old_len; | |
9399 | tot += sizeof(text_poke_event.new_len) + new_len; | |
9400 | pad = ALIGN(tot, sizeof(u64)) - tot; | |
9401 | ||
9402 | text_poke_event = (struct perf_text_poke_event){ | |
9403 | .old_bytes = old_bytes, | |
9404 | .new_bytes = new_bytes, | |
9405 | .pad = pad, | |
9406 | .old_len = old_len, | |
9407 | .new_len = new_len, | |
9408 | .event_id = { | |
9409 | .header = { | |
9410 | .type = PERF_RECORD_TEXT_POKE, | |
9411 | .misc = PERF_RECORD_MISC_KERNEL, | |
9412 | .size = sizeof(text_poke_event.event_id) + tot + pad, | |
9413 | }, | |
9414 | .addr = (unsigned long)addr, | |
9415 | }, | |
9416 | }; | |
9417 | ||
9418 | perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL); | |
9419 | } | |
9420 | ||
8d4e6c4c AS |
9421 | void perf_event_itrace_started(struct perf_event *event) |
9422 | { | |
9423 | event->attach_state |= PERF_ATTACH_ITRACE; | |
9424 | } | |
9425 | ||
ec0d7729 AS |
9426 | static void perf_log_itrace_start(struct perf_event *event) |
9427 | { | |
9428 | struct perf_output_handle handle; | |
9429 | struct perf_sample_data sample; | |
9430 | struct perf_aux_event { | |
9431 | struct perf_event_header header; | |
9432 | u32 pid; | |
9433 | u32 tid; | |
9434 | } rec; | |
9435 | int ret; | |
9436 | ||
9437 | if (event->parent) | |
9438 | event = event->parent; | |
9439 | ||
9440 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
8d4e6c4c | 9441 | event->attach_state & PERF_ATTACH_ITRACE) |
ec0d7729 AS |
9442 | return; |
9443 | ||
ec0d7729 AS |
9444 | rec.header.type = PERF_RECORD_ITRACE_START; |
9445 | rec.header.misc = 0; | |
9446 | rec.header.size = sizeof(rec); | |
9447 | rec.pid = perf_event_pid(event, current); | |
9448 | rec.tid = perf_event_tid(event, current); | |
9449 | ||
9450 | perf_event_header__init_id(&rec.header, &sample, event); | |
267fb273 | 9451 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); |
ec0d7729 AS |
9452 | |
9453 | if (ret) | |
9454 | return; | |
9455 | ||
9456 | perf_output_put(&handle, rec); | |
9457 | perf_event__output_id_sample(event, &handle, &sample); | |
9458 | ||
9459 | perf_output_end(&handle); | |
9460 | } | |
9461 | ||
8b8ff8cc AH |
9462 | void perf_report_aux_output_id(struct perf_event *event, u64 hw_id) |
9463 | { | |
9464 | struct perf_output_handle handle; | |
9465 | struct perf_sample_data sample; | |
9466 | struct perf_aux_event { | |
9467 | struct perf_event_header header; | |
9468 | u64 hw_id; | |
9469 | } rec; | |
9470 | int ret; | |
9471 | ||
9472 | if (event->parent) | |
9473 | event = event->parent; | |
9474 | ||
9475 | rec.header.type = PERF_RECORD_AUX_OUTPUT_HW_ID; | |
9476 | rec.header.misc = 0; | |
9477 | rec.header.size = sizeof(rec); | |
9478 | rec.hw_id = hw_id; | |
9479 | ||
9480 | perf_event_header__init_id(&rec.header, &sample, event); | |
9481 | ret = perf_output_begin(&handle, &sample, event, rec.header.size); | |
9482 | ||
9483 | if (ret) | |
9484 | return; | |
9485 | ||
9486 | perf_output_put(&handle, rec); | |
9487 | perf_event__output_id_sample(event, &handle, &sample); | |
9488 | ||
9489 | perf_output_end(&handle); | |
9490 | } | |
7d30d480 | 9491 | EXPORT_SYMBOL_GPL(perf_report_aux_output_id); |
8b8ff8cc | 9492 | |
475113d9 JO |
9493 | static int |
9494 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 9495 | { |
cdd6c482 | 9496 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 9497 | int ret = 0; |
475113d9 | 9498 | u64 seq; |
96398826 | 9499 | |
e050e3f0 SE |
9500 | seq = __this_cpu_read(perf_throttled_seq); |
9501 | if (seq != hwc->interrupts_seq) { | |
9502 | hwc->interrupts_seq = seq; | |
9503 | hwc->interrupts = 1; | |
9504 | } else { | |
9505 | hwc->interrupts++; | |
15def34e YJ |
9506 | if (unlikely(throttle && |
9507 | hwc->interrupts > max_samples_per_tick)) { | |
e050e3f0 | 9508 | __this_cpu_inc(perf_throttled_count); |
555e0c1e | 9509 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
9510 | hwc->interrupts = MAX_INTERRUPTS; |
9511 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
9512 | ret = 1; |
9513 | } | |
e050e3f0 | 9514 | } |
60db5e09 | 9515 | |
cdd6c482 | 9516 | if (event->attr.freq) { |
def0a9b2 | 9517 | u64 now = perf_clock(); |
abd50713 | 9518 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 9519 | |
abd50713 | 9520 | hwc->freq_time_stamp = now; |
bd2b5b12 | 9521 | |
abd50713 | 9522 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 9523 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
9524 | } |
9525 | ||
475113d9 JO |
9526 | return ret; |
9527 | } | |
9528 | ||
9529 | int perf_event_account_interrupt(struct perf_event *event) | |
9530 | { | |
9531 | return __perf_event_account_interrupt(event, 1); | |
9532 | } | |
9533 | ||
030a976e PZ |
9534 | static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs) |
9535 | { | |
9536 | /* | |
9537 | * Due to interrupt latency (AKA "skid"), we may enter the | |
9538 | * kernel before taking an overflow, even if the PMU is only | |
9539 | * counting user events. | |
9540 | */ | |
9541 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
9542 | return false; | |
9543 | ||
9544 | return true; | |
9545 | } | |
9546 | ||
475113d9 JO |
9547 | /* |
9548 | * Generic event overflow handling, sampling. | |
9549 | */ | |
9550 | ||
9551 | static int __perf_event_overflow(struct perf_event *event, | |
ca6c2132 PZ |
9552 | int throttle, struct perf_sample_data *data, |
9553 | struct pt_regs *regs) | |
475113d9 JO |
9554 | { |
9555 | int events = atomic_read(&event->event_limit); | |
9556 | int ret = 0; | |
9557 | ||
9558 | /* | |
9559 | * Non-sampling counters might still use the PMI to fold short | |
9560 | * hardware counters, ignore those. | |
9561 | */ | |
9562 | if (unlikely(!is_sampling_event(event))) | |
9563 | return 0; | |
9564 | ||
9565 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 9566 | |
2023b359 PZ |
9567 | /* |
9568 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 9569 | * events |
2023b359 PZ |
9570 | */ |
9571 | ||
cdd6c482 IM |
9572 | event->pending_kill = POLL_IN; |
9573 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 9574 | ret = 1; |
cdd6c482 | 9575 | event->pending_kill = POLL_HUP; |
5aab90ce | 9576 | perf_event_disable_inatomic(event); |
79f14641 PZ |
9577 | } |
9578 | ||
ca6c2132 | 9579 | if (event->attr.sigtrap) { |
030a976e PZ |
9580 | /* |
9581 | * The desired behaviour of sigtrap vs invalid samples is a bit | |
9582 | * tricky; on the one hand, one should not loose the SIGTRAP if | |
9583 | * it is the first event, on the other hand, we should also not | |
9584 | * trigger the WARN or override the data address. | |
9585 | */ | |
9586 | bool valid_sample = sample_is_allowed(event, regs); | |
bb88f969 ME |
9587 | unsigned int pending_id = 1; |
9588 | ||
9589 | if (regs) | |
9590 | pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1; | |
ca6c2132 | 9591 | if (!event->pending_sigtrap) { |
bb88f969 | 9592 | event->pending_sigtrap = pending_id; |
ca6c2132 | 9593 | local_inc(&event->ctx->nr_pending); |
030a976e | 9594 | } else if (event->attr.exclude_kernel && valid_sample) { |
bb88f969 ME |
9595 | /* |
9596 | * Should not be able to return to user space without | |
9597 | * consuming pending_sigtrap; with exceptions: | |
9598 | * | |
9599 | * 1. Where !exclude_kernel, events can overflow again | |
9600 | * in the kernel without returning to user space. | |
9601 | * | |
9602 | * 2. Events that can overflow again before the IRQ- | |
9603 | * work without user space progress (e.g. hrtimer). | |
9604 | * To approximate progress (with false negatives), | |
9605 | * check 32-bit hash of the current IP. | |
9606 | */ | |
9607 | WARN_ON_ONCE(event->pending_sigtrap != pending_id); | |
ca6c2132 | 9608 | } |
af169b77 PZ |
9609 | |
9610 | event->pending_addr = 0; | |
030a976e | 9611 | if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) |
af169b77 | 9612 | event->pending_addr = data->addr; |
ca6c2132 PZ |
9613 | irq_work_queue(&event->pending_irq); |
9614 | } | |
9615 | ||
aa6a5f3c | 9616 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 9617 | |
fed66e2c | 9618 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 | 9619 | event->pending_wakeup = 1; |
ca6c2132 | 9620 | irq_work_queue(&event->pending_irq); |
f506b3dc PZ |
9621 | } |
9622 | ||
79f14641 | 9623 | return ret; |
f6c7d5fe PZ |
9624 | } |
9625 | ||
a8b0ca17 | 9626 | int perf_event_overflow(struct perf_event *event, |
ca6c2132 PZ |
9627 | struct perf_sample_data *data, |
9628 | struct pt_regs *regs) | |
850bc73f | 9629 | { |
a8b0ca17 | 9630 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
9631 | } |
9632 | ||
15dbf27c | 9633 | /* |
cdd6c482 | 9634 | * Generic software event infrastructure |
15dbf27c PZ |
9635 | */ |
9636 | ||
b28ab83c PZ |
9637 | struct swevent_htable { |
9638 | struct swevent_hlist *swevent_hlist; | |
9639 | struct mutex hlist_mutex; | |
9640 | int hlist_refcount; | |
9641 | ||
9642 | /* Recursion avoidance in each contexts */ | |
9643 | int recursion[PERF_NR_CONTEXTS]; | |
9644 | }; | |
9645 | ||
9646 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
9647 | ||
7b4b6658 | 9648 | /* |
cdd6c482 IM |
9649 | * We directly increment event->count and keep a second value in |
9650 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
9651 | * is kept in the range [-sample_period, 0] so that we can use the |
9652 | * sign as trigger. | |
9653 | */ | |
9654 | ||
ab573844 | 9655 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 9656 | { |
cdd6c482 | 9657 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
9658 | u64 period = hwc->last_period; |
9659 | u64 nr, offset; | |
9660 | s64 old, val; | |
9661 | ||
9662 | hwc->last_period = hwc->sample_period; | |
15dbf27c | 9663 | |
28fd85a1 UB |
9664 | old = local64_read(&hwc->period_left); |
9665 | do { | |
9666 | val = old; | |
9667 | if (val < 0) | |
9668 | return 0; | |
15dbf27c | 9669 | |
28fd85a1 UB |
9670 | nr = div64_u64(period + val, period); |
9671 | offset = nr * period; | |
9672 | val -= offset; | |
9673 | } while (!local64_try_cmpxchg(&hwc->period_left, &old, val)); | |
15dbf27c | 9674 | |
7b4b6658 | 9675 | return nr; |
15dbf27c PZ |
9676 | } |
9677 | ||
0cff784a | 9678 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 9679 | struct perf_sample_data *data, |
5622f295 | 9680 | struct pt_regs *regs) |
15dbf27c | 9681 | { |
cdd6c482 | 9682 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 9683 | int throttle = 0; |
15dbf27c | 9684 | |
0cff784a PZ |
9685 | if (!overflow) |
9686 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 9687 | |
7b4b6658 PZ |
9688 | if (hwc->interrupts == MAX_INTERRUPTS) |
9689 | return; | |
15dbf27c | 9690 | |
7b4b6658 | 9691 | for (; overflow; overflow--) { |
a8b0ca17 | 9692 | if (__perf_event_overflow(event, throttle, |
5622f295 | 9693 | data, regs)) { |
7b4b6658 PZ |
9694 | /* |
9695 | * We inhibit the overflow from happening when | |
9696 | * hwc->interrupts == MAX_INTERRUPTS. | |
9697 | */ | |
9698 | break; | |
9699 | } | |
cf450a73 | 9700 | throttle = 1; |
7b4b6658 | 9701 | } |
15dbf27c PZ |
9702 | } |
9703 | ||
a4eaf7f1 | 9704 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 9705 | struct perf_sample_data *data, |
5622f295 | 9706 | struct pt_regs *regs) |
7b4b6658 | 9707 | { |
cdd6c482 | 9708 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 9709 | |
e7850595 | 9710 | local64_add(nr, &event->count); |
d6d020e9 | 9711 | |
0cff784a PZ |
9712 | if (!regs) |
9713 | return; | |
9714 | ||
6c7e550f | 9715 | if (!is_sampling_event(event)) |
7b4b6658 | 9716 | return; |
d6d020e9 | 9717 | |
5d81e5cf AV |
9718 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
9719 | data->period = nr; | |
9720 | return perf_swevent_overflow(event, 1, data, regs); | |
9721 | } else | |
9722 | data->period = event->hw.last_period; | |
9723 | ||
0cff784a | 9724 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 9725 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 9726 | |
e7850595 | 9727 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 9728 | return; |
df1a132b | 9729 | |
a8b0ca17 | 9730 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
9731 | } |
9732 | ||
f5ffe02e FW |
9733 | static int perf_exclude_event(struct perf_event *event, |
9734 | struct pt_regs *regs) | |
9735 | { | |
a4eaf7f1 | 9736 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 9737 | return 1; |
a4eaf7f1 | 9738 | |
f5ffe02e FW |
9739 | if (regs) { |
9740 | if (event->attr.exclude_user && user_mode(regs)) | |
9741 | return 1; | |
9742 | ||
9743 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
9744 | return 1; | |
9745 | } | |
9746 | ||
9747 | return 0; | |
9748 | } | |
9749 | ||
cdd6c482 | 9750 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 9751 | enum perf_type_id type, |
6fb2915d LZ |
9752 | u32 event_id, |
9753 | struct perf_sample_data *data, | |
9754 | struct pt_regs *regs) | |
15dbf27c | 9755 | { |
cdd6c482 | 9756 | if (event->attr.type != type) |
a21ca2ca | 9757 | return 0; |
f5ffe02e | 9758 | |
cdd6c482 | 9759 | if (event->attr.config != event_id) |
15dbf27c PZ |
9760 | return 0; |
9761 | ||
f5ffe02e FW |
9762 | if (perf_exclude_event(event, regs)) |
9763 | return 0; | |
15dbf27c PZ |
9764 | |
9765 | return 1; | |
9766 | } | |
9767 | ||
76e1d904 FW |
9768 | static inline u64 swevent_hash(u64 type, u32 event_id) |
9769 | { | |
9770 | u64 val = event_id | (type << 32); | |
9771 | ||
9772 | return hash_64(val, SWEVENT_HLIST_BITS); | |
9773 | } | |
9774 | ||
49f135ed FW |
9775 | static inline struct hlist_head * |
9776 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 9777 | { |
49f135ed FW |
9778 | u64 hash = swevent_hash(type, event_id); |
9779 | ||
9780 | return &hlist->heads[hash]; | |
9781 | } | |
76e1d904 | 9782 | |
49f135ed FW |
9783 | /* For the read side: events when they trigger */ |
9784 | static inline struct hlist_head * | |
b28ab83c | 9785 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
9786 | { |
9787 | struct swevent_hlist *hlist; | |
76e1d904 | 9788 | |
b28ab83c | 9789 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
9790 | if (!hlist) |
9791 | return NULL; | |
9792 | ||
49f135ed FW |
9793 | return __find_swevent_head(hlist, type, event_id); |
9794 | } | |
9795 | ||
9796 | /* For the event head insertion and removal in the hlist */ | |
9797 | static inline struct hlist_head * | |
b28ab83c | 9798 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
9799 | { |
9800 | struct swevent_hlist *hlist; | |
9801 | u32 event_id = event->attr.config; | |
9802 | u64 type = event->attr.type; | |
9803 | ||
9804 | /* | |
9805 | * Event scheduling is always serialized against hlist allocation | |
9806 | * and release. Which makes the protected version suitable here. | |
9807 | * The context lock guarantees that. | |
9808 | */ | |
b28ab83c | 9809 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
9810 | lockdep_is_held(&event->ctx->lock)); |
9811 | if (!hlist) | |
9812 | return NULL; | |
9813 | ||
9814 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
9815 | } |
9816 | ||
9817 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 9818 | u64 nr, |
76e1d904 FW |
9819 | struct perf_sample_data *data, |
9820 | struct pt_regs *regs) | |
15dbf27c | 9821 | { |
4a32fea9 | 9822 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9823 | struct perf_event *event; |
76e1d904 | 9824 | struct hlist_head *head; |
15dbf27c | 9825 | |
76e1d904 | 9826 | rcu_read_lock(); |
b28ab83c | 9827 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
9828 | if (!head) |
9829 | goto end; | |
9830 | ||
b67bfe0d | 9831 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 9832 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 9833 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 9834 | } |
76e1d904 FW |
9835 | end: |
9836 | rcu_read_unlock(); | |
15dbf27c PZ |
9837 | } |
9838 | ||
86038c5e PZI |
9839 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
9840 | ||
4ed7c92d | 9841 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 9842 | { |
4a32fea9 | 9843 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 9844 | |
b28ab83c | 9845 | return get_recursion_context(swhash->recursion); |
96f6d444 | 9846 | } |
645e8cc0 | 9847 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 9848 | |
98b5c2c6 | 9849 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 9850 | { |
4a32fea9 | 9851 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 9852 | |
b28ab83c | 9853 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 9854 | } |
15dbf27c | 9855 | |
86038c5e | 9856 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 9857 | { |
a4234bfc | 9858 | struct perf_sample_data data; |
4ed7c92d | 9859 | |
86038c5e | 9860 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 9861 | return; |
a4234bfc | 9862 | |
fd0d000b | 9863 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 9864 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
9865 | } |
9866 | ||
9867 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
9868 | { | |
9869 | int rctx; | |
9870 | ||
9871 | preempt_disable_notrace(); | |
9872 | rctx = perf_swevent_get_recursion_context(); | |
9873 | if (unlikely(rctx < 0)) | |
9874 | goto fail; | |
9875 | ||
9876 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
9877 | |
9878 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 9879 | fail: |
1c024eca | 9880 | preempt_enable_notrace(); |
b8e83514 PZ |
9881 | } |
9882 | ||
cdd6c482 | 9883 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 9884 | { |
15dbf27c PZ |
9885 | } |
9886 | ||
a4eaf7f1 | 9887 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 9888 | { |
4a32fea9 | 9889 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9890 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
9891 | struct hlist_head *head; |
9892 | ||
6c7e550f | 9893 | if (is_sampling_event(event)) { |
7b4b6658 | 9894 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 9895 | perf_swevent_set_period(event); |
7b4b6658 | 9896 | } |
76e1d904 | 9897 | |
a4eaf7f1 PZ |
9898 | hwc->state = !(flags & PERF_EF_START); |
9899 | ||
b28ab83c | 9900 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 9901 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
9902 | return -EINVAL; |
9903 | ||
9904 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 9905 | perf_event_update_userpage(event); |
76e1d904 | 9906 | |
15dbf27c PZ |
9907 | return 0; |
9908 | } | |
9909 | ||
a4eaf7f1 | 9910 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 9911 | { |
76e1d904 | 9912 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
9913 | } |
9914 | ||
a4eaf7f1 | 9915 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 9916 | { |
a4eaf7f1 | 9917 | event->hw.state = 0; |
d6d020e9 | 9918 | } |
aa9c4c0f | 9919 | |
a4eaf7f1 | 9920 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 9921 | { |
a4eaf7f1 | 9922 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
9923 | } |
9924 | ||
49f135ed FW |
9925 | /* Deref the hlist from the update side */ |
9926 | static inline struct swevent_hlist * | |
b28ab83c | 9927 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 9928 | { |
b28ab83c PZ |
9929 | return rcu_dereference_protected(swhash->swevent_hlist, |
9930 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
9931 | } |
9932 | ||
b28ab83c | 9933 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 9934 | { |
b28ab83c | 9935 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 9936 | |
49f135ed | 9937 | if (!hlist) |
76e1d904 FW |
9938 | return; |
9939 | ||
70691d4a | 9940 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 9941 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
9942 | } |
9943 | ||
3b364d7b | 9944 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 9945 | { |
b28ab83c | 9946 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 9947 | |
b28ab83c | 9948 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 9949 | |
b28ab83c PZ |
9950 | if (!--swhash->hlist_refcount) |
9951 | swevent_hlist_release(swhash); | |
76e1d904 | 9952 | |
b28ab83c | 9953 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
9954 | } |
9955 | ||
3b364d7b | 9956 | static void swevent_hlist_put(void) |
76e1d904 FW |
9957 | { |
9958 | int cpu; | |
9959 | ||
76e1d904 | 9960 | for_each_possible_cpu(cpu) |
3b364d7b | 9961 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
9962 | } |
9963 | ||
3b364d7b | 9964 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 9965 | { |
b28ab83c | 9966 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
9967 | int err = 0; |
9968 | ||
b28ab83c | 9969 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
9970 | if (!swevent_hlist_deref(swhash) && |
9971 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
9972 | struct swevent_hlist *hlist; |
9973 | ||
9974 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
9975 | if (!hlist) { | |
9976 | err = -ENOMEM; | |
9977 | goto exit; | |
9978 | } | |
b28ab83c | 9979 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 9980 | } |
b28ab83c | 9981 | swhash->hlist_refcount++; |
9ed6060d | 9982 | exit: |
b28ab83c | 9983 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
9984 | |
9985 | return err; | |
9986 | } | |
9987 | ||
3b364d7b | 9988 | static int swevent_hlist_get(void) |
76e1d904 | 9989 | { |
3b364d7b | 9990 | int err, cpu, failed_cpu; |
76e1d904 | 9991 | |
a63fbed7 | 9992 | mutex_lock(&pmus_lock); |
76e1d904 | 9993 | for_each_possible_cpu(cpu) { |
3b364d7b | 9994 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
9995 | if (err) { |
9996 | failed_cpu = cpu; | |
9997 | goto fail; | |
9998 | } | |
9999 | } | |
a63fbed7 | 10000 | mutex_unlock(&pmus_lock); |
76e1d904 | 10001 | return 0; |
9ed6060d | 10002 | fail: |
76e1d904 FW |
10003 | for_each_possible_cpu(cpu) { |
10004 | if (cpu == failed_cpu) | |
10005 | break; | |
3b364d7b | 10006 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 10007 | } |
a63fbed7 | 10008 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
10009 | return err; |
10010 | } | |
10011 | ||
c5905afb | 10012 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 10013 | |
b0a873eb PZ |
10014 | static void sw_perf_event_destroy(struct perf_event *event) |
10015 | { | |
10016 | u64 event_id = event->attr.config; | |
95476b64 | 10017 | |
b0a873eb PZ |
10018 | WARN_ON(event->parent); |
10019 | ||
c5905afb | 10020 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 10021 | swevent_hlist_put(); |
b0a873eb PZ |
10022 | } |
10023 | ||
0d6d062c RB |
10024 | static struct pmu perf_cpu_clock; /* fwd declaration */ |
10025 | static struct pmu perf_task_clock; | |
10026 | ||
b0a873eb PZ |
10027 | static int perf_swevent_init(struct perf_event *event) |
10028 | { | |
8176cced | 10029 | u64 event_id = event->attr.config; |
b0a873eb PZ |
10030 | |
10031 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
10032 | return -ENOENT; | |
10033 | ||
2481c5fa SE |
10034 | /* |
10035 | * no branch sampling for software events | |
10036 | */ | |
10037 | if (has_branch_stack(event)) | |
10038 | return -EOPNOTSUPP; | |
10039 | ||
b0a873eb PZ |
10040 | switch (event_id) { |
10041 | case PERF_COUNT_SW_CPU_CLOCK: | |
0d6d062c RB |
10042 | event->attr.type = perf_cpu_clock.type; |
10043 | return -ENOENT; | |
b0a873eb | 10044 | case PERF_COUNT_SW_TASK_CLOCK: |
0d6d062c | 10045 | event->attr.type = perf_task_clock.type; |
b0a873eb PZ |
10046 | return -ENOENT; |
10047 | ||
10048 | default: | |
10049 | break; | |
10050 | } | |
10051 | ||
ce677831 | 10052 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
10053 | return -ENOENT; |
10054 | ||
10055 | if (!event->parent) { | |
10056 | int err; | |
10057 | ||
3b364d7b | 10058 | err = swevent_hlist_get(); |
b0a873eb PZ |
10059 | if (err) |
10060 | return err; | |
10061 | ||
c5905afb | 10062 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
10063 | event->destroy = sw_perf_event_destroy; |
10064 | } | |
10065 | ||
10066 | return 0; | |
10067 | } | |
10068 | ||
10069 | static struct pmu perf_swevent = { | |
89a1e187 | 10070 | .task_ctx_nr = perf_sw_context, |
95476b64 | 10071 | |
34f43927 PZ |
10072 | .capabilities = PERF_PMU_CAP_NO_NMI, |
10073 | ||
b0a873eb | 10074 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
10075 | .add = perf_swevent_add, |
10076 | .del = perf_swevent_del, | |
10077 | .start = perf_swevent_start, | |
10078 | .stop = perf_swevent_stop, | |
1c024eca | 10079 | .read = perf_swevent_read, |
1c024eca PZ |
10080 | }; |
10081 | ||
b0a873eb PZ |
10082 | #ifdef CONFIG_EVENT_TRACING |
10083 | ||
571f97f7 RB |
10084 | static void tp_perf_event_destroy(struct perf_event *event) |
10085 | { | |
10086 | perf_trace_destroy(event); | |
10087 | } | |
10088 | ||
10089 | static int perf_tp_event_init(struct perf_event *event) | |
10090 | { | |
10091 | int err; | |
10092 | ||
10093 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
10094 | return -ENOENT; | |
10095 | ||
10096 | /* | |
10097 | * no branch sampling for tracepoint events | |
10098 | */ | |
10099 | if (has_branch_stack(event)) | |
10100 | return -EOPNOTSUPP; | |
10101 | ||
10102 | err = perf_trace_init(event); | |
10103 | if (err) | |
10104 | return err; | |
10105 | ||
10106 | event->destroy = tp_perf_event_destroy; | |
10107 | ||
10108 | return 0; | |
10109 | } | |
10110 | ||
10111 | static struct pmu perf_tracepoint = { | |
10112 | .task_ctx_nr = perf_sw_context, | |
10113 | ||
10114 | .event_init = perf_tp_event_init, | |
10115 | .add = perf_trace_add, | |
10116 | .del = perf_trace_del, | |
10117 | .start = perf_swevent_start, | |
10118 | .stop = perf_swevent_stop, | |
10119 | .read = perf_swevent_read, | |
10120 | }; | |
10121 | ||
1c024eca PZ |
10122 | static int perf_tp_filter_match(struct perf_event *event, |
10123 | struct perf_sample_data *data) | |
10124 | { | |
7e3f977e | 10125 | void *record = data->raw->frag.data; |
1c024eca | 10126 | |
b71b437e PZ |
10127 | /* only top level events have filters set */ |
10128 | if (event->parent) | |
10129 | event = event->parent; | |
10130 | ||
1c024eca PZ |
10131 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
10132 | return 1; | |
10133 | return 0; | |
10134 | } | |
10135 | ||
10136 | static int perf_tp_event_match(struct perf_event *event, | |
10137 | struct perf_sample_data *data, | |
10138 | struct pt_regs *regs) | |
10139 | { | |
a0f7d0f7 FW |
10140 | if (event->hw.state & PERF_HES_STOPPED) |
10141 | return 0; | |
580d607c | 10142 | /* |
9fd2e48b | 10143 | * If exclude_kernel, only trace user-space tracepoints (uprobes) |
580d607c | 10144 | */ |
9fd2e48b | 10145 | if (event->attr.exclude_kernel && !user_mode(regs)) |
1c024eca PZ |
10146 | return 0; |
10147 | ||
10148 | if (!perf_tp_filter_match(event, data)) | |
10149 | return 0; | |
10150 | ||
10151 | return 1; | |
10152 | } | |
10153 | ||
85b67bcb AS |
10154 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
10155 | struct trace_event_call *call, u64 count, | |
10156 | struct pt_regs *regs, struct hlist_head *head, | |
10157 | struct task_struct *task) | |
10158 | { | |
e87c6bc3 | 10159 | if (bpf_prog_array_valid(call)) { |
85b67bcb | 10160 | *(struct pt_regs **)raw_data = regs; |
e87c6bc3 | 10161 | if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { |
85b67bcb AS |
10162 | perf_swevent_put_recursion_context(rctx); |
10163 | return; | |
10164 | } | |
10165 | } | |
10166 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
8fd0fbbe | 10167 | rctx, task); |
85b67bcb AS |
10168 | } |
10169 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
10170 | ||
571f97f7 RB |
10171 | static void __perf_tp_event_target_task(u64 count, void *record, |
10172 | struct pt_regs *regs, | |
10173 | struct perf_sample_data *data, | |
10174 | struct perf_event *event) | |
10175 | { | |
10176 | struct trace_entry *entry = record; | |
10177 | ||
10178 | if (event->attr.config != entry->type) | |
10179 | return; | |
10180 | /* Cannot deliver synchronous signal to other task. */ | |
10181 | if (event->attr.sigtrap) | |
10182 | return; | |
10183 | if (perf_tp_event_match(event, data, regs)) | |
10184 | perf_swevent_event(event, count, data, regs); | |
10185 | } | |
10186 | ||
10187 | static void perf_tp_event_target_task(u64 count, void *record, | |
10188 | struct pt_regs *regs, | |
10189 | struct perf_sample_data *data, | |
10190 | struct perf_event_context *ctx) | |
10191 | { | |
10192 | unsigned int cpu = smp_processor_id(); | |
10193 | struct pmu *pmu = &perf_tracepoint; | |
10194 | struct perf_event *event, *sibling; | |
10195 | ||
10196 | perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { | |
10197 | __perf_tp_event_target_task(count, record, regs, data, event); | |
10198 | for_each_sibling_event(sibling, event) | |
10199 | __perf_tp_event_target_task(count, record, regs, data, sibling); | |
10200 | } | |
10201 | ||
10202 | perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { | |
10203 | __perf_tp_event_target_task(count, record, regs, data, event); | |
10204 | for_each_sibling_event(sibling, event) | |
10205 | __perf_tp_event_target_task(count, record, regs, data, sibling); | |
10206 | } | |
10207 | } | |
10208 | ||
1e1dcd93 | 10209 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 10210 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
8fd0fbbe | 10211 | struct task_struct *task) |
95476b64 FW |
10212 | { |
10213 | struct perf_sample_data data; | |
8fd0fbbe | 10214 | struct perf_event *event; |
1c024eca | 10215 | |
95476b64 | 10216 | struct perf_raw_record raw = { |
7e3f977e DB |
10217 | .frag = { |
10218 | .size = entry_size, | |
10219 | .data = record, | |
10220 | }, | |
95476b64 FW |
10221 | }; |
10222 | ||
1e1dcd93 | 10223 | perf_sample_data_init(&data, 0, 0); |
0a9081cf | 10224 | perf_sample_save_raw_data(&data, &raw); |
95476b64 | 10225 | |
1e1dcd93 AS |
10226 | perf_trace_buf_update(record, event_type); |
10227 | ||
8fd0fbbe | 10228 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1d1bfe30 | 10229 | if (perf_tp_event_match(event, &data, regs)) { |
a8b0ca17 | 10230 | perf_swevent_event(event, count, &data, regs); |
1d1bfe30 YJ |
10231 | |
10232 | /* | |
10233 | * Here use the same on-stack perf_sample_data, | |
10234 | * some members in data are event-specific and | |
10235 | * need to be re-computed for different sweveents. | |
10236 | * Re-initialize data->sample_flags safely to avoid | |
10237 | * the problem that next event skips preparing data | |
10238 | * because data->sample_flags is set. | |
10239 | */ | |
10240 | perf_sample_data_init(&data, 0, 0); | |
10241 | perf_sample_save_raw_data(&data, &raw); | |
10242 | } | |
4f41c013 | 10243 | } |
ecc55f84 | 10244 | |
e6dab5ff AV |
10245 | /* |
10246 | * If we got specified a target task, also iterate its context and | |
10247 | * deliver this event there too. | |
10248 | */ | |
10249 | if (task && task != current) { | |
10250 | struct perf_event_context *ctx; | |
e6dab5ff AV |
10251 | |
10252 | rcu_read_lock(); | |
bd275681 | 10253 | ctx = rcu_dereference(task->perf_event_ctxp); |
e6dab5ff AV |
10254 | if (!ctx) |
10255 | goto unlock; | |
10256 | ||
571f97f7 RB |
10257 | raw_spin_lock(&ctx->lock); |
10258 | perf_tp_event_target_task(count, record, regs, &data, ctx); | |
10259 | raw_spin_unlock(&ctx->lock); | |
e6dab5ff AV |
10260 | unlock: |
10261 | rcu_read_unlock(); | |
10262 | } | |
10263 | ||
ecc55f84 | 10264 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
10265 | } |
10266 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
10267 | ||
33ea4b24 | 10268 | #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) |
e12f03d7 SL |
10269 | /* |
10270 | * Flags in config, used by dynamic PMU kprobe and uprobe | |
10271 | * The flags should match following PMU_FORMAT_ATTR(). | |
10272 | * | |
10273 | * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe | |
10274 | * if not set, create kprobe/uprobe | |
a6ca88b2 SL |
10275 | * |
10276 | * The following values specify a reference counter (or semaphore in the | |
10277 | * terminology of tools like dtrace, systemtap, etc.) Userspace Statically | |
10278 | * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset. | |
10279 | * | |
10280 | * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset | |
10281 | * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left | |
e12f03d7 SL |
10282 | */ |
10283 | enum perf_probe_config { | |
10284 | PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */ | |
a6ca88b2 SL |
10285 | PERF_UPROBE_REF_CTR_OFFSET_BITS = 32, |
10286 | PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS, | |
e12f03d7 SL |
10287 | }; |
10288 | ||
10289 | PMU_FORMAT_ATTR(retprobe, "config:0"); | |
a6ca88b2 | 10290 | #endif |
e12f03d7 | 10291 | |
a6ca88b2 SL |
10292 | #ifdef CONFIG_KPROBE_EVENTS |
10293 | static struct attribute *kprobe_attrs[] = { | |
e12f03d7 SL |
10294 | &format_attr_retprobe.attr, |
10295 | NULL, | |
10296 | }; | |
10297 | ||
a6ca88b2 | 10298 | static struct attribute_group kprobe_format_group = { |
e12f03d7 | 10299 | .name = "format", |
a6ca88b2 | 10300 | .attrs = kprobe_attrs, |
e12f03d7 SL |
10301 | }; |
10302 | ||
a6ca88b2 SL |
10303 | static const struct attribute_group *kprobe_attr_groups[] = { |
10304 | &kprobe_format_group, | |
e12f03d7 SL |
10305 | NULL, |
10306 | }; | |
10307 | ||
10308 | static int perf_kprobe_event_init(struct perf_event *event); | |
10309 | static struct pmu perf_kprobe = { | |
10310 | .task_ctx_nr = perf_sw_context, | |
10311 | .event_init = perf_kprobe_event_init, | |
10312 | .add = perf_trace_add, | |
10313 | .del = perf_trace_del, | |
10314 | .start = perf_swevent_start, | |
10315 | .stop = perf_swevent_stop, | |
10316 | .read = perf_swevent_read, | |
a6ca88b2 | 10317 | .attr_groups = kprobe_attr_groups, |
e12f03d7 SL |
10318 | }; |
10319 | ||
10320 | static int perf_kprobe_event_init(struct perf_event *event) | |
10321 | { | |
10322 | int err; | |
10323 | bool is_retprobe; | |
10324 | ||
10325 | if (event->attr.type != perf_kprobe.type) | |
10326 | return -ENOENT; | |
32e6e967 | 10327 | |
c9e0924e | 10328 | if (!perfmon_capable()) |
32e6e967 SL |
10329 | return -EACCES; |
10330 | ||
e12f03d7 SL |
10331 | /* |
10332 | * no branch sampling for probe events | |
10333 | */ | |
10334 | if (has_branch_stack(event)) | |
10335 | return -EOPNOTSUPP; | |
10336 | ||
10337 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
10338 | err = perf_kprobe_init(event, is_retprobe); | |
10339 | if (err) | |
10340 | return err; | |
10341 | ||
10342 | event->destroy = perf_kprobe_destroy; | |
10343 | ||
10344 | return 0; | |
10345 | } | |
10346 | #endif /* CONFIG_KPROBE_EVENTS */ | |
10347 | ||
33ea4b24 | 10348 | #ifdef CONFIG_UPROBE_EVENTS |
a6ca88b2 SL |
10349 | PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63"); |
10350 | ||
10351 | static struct attribute *uprobe_attrs[] = { | |
10352 | &format_attr_retprobe.attr, | |
10353 | &format_attr_ref_ctr_offset.attr, | |
10354 | NULL, | |
10355 | }; | |
10356 | ||
10357 | static struct attribute_group uprobe_format_group = { | |
10358 | .name = "format", | |
10359 | .attrs = uprobe_attrs, | |
10360 | }; | |
10361 | ||
10362 | static const struct attribute_group *uprobe_attr_groups[] = { | |
10363 | &uprobe_format_group, | |
10364 | NULL, | |
10365 | }; | |
10366 | ||
33ea4b24 SL |
10367 | static int perf_uprobe_event_init(struct perf_event *event); |
10368 | static struct pmu perf_uprobe = { | |
10369 | .task_ctx_nr = perf_sw_context, | |
10370 | .event_init = perf_uprobe_event_init, | |
10371 | .add = perf_trace_add, | |
10372 | .del = perf_trace_del, | |
10373 | .start = perf_swevent_start, | |
10374 | .stop = perf_swevent_stop, | |
10375 | .read = perf_swevent_read, | |
a6ca88b2 | 10376 | .attr_groups = uprobe_attr_groups, |
33ea4b24 SL |
10377 | }; |
10378 | ||
10379 | static int perf_uprobe_event_init(struct perf_event *event) | |
10380 | { | |
10381 | int err; | |
a6ca88b2 | 10382 | unsigned long ref_ctr_offset; |
33ea4b24 SL |
10383 | bool is_retprobe; |
10384 | ||
10385 | if (event->attr.type != perf_uprobe.type) | |
10386 | return -ENOENT; | |
32e6e967 | 10387 | |
c9e0924e | 10388 | if (!perfmon_capable()) |
32e6e967 SL |
10389 | return -EACCES; |
10390 | ||
33ea4b24 SL |
10391 | /* |
10392 | * no branch sampling for probe events | |
10393 | */ | |
10394 | if (has_branch_stack(event)) | |
10395 | return -EOPNOTSUPP; | |
10396 | ||
10397 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
a6ca88b2 SL |
10398 | ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
10399 | err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe); | |
33ea4b24 SL |
10400 | if (err) |
10401 | return err; | |
10402 | ||
10403 | event->destroy = perf_uprobe_destroy; | |
10404 | ||
10405 | return 0; | |
10406 | } | |
10407 | #endif /* CONFIG_UPROBE_EVENTS */ | |
10408 | ||
b0a873eb PZ |
10409 | static inline void perf_tp_register(void) |
10410 | { | |
2e80a82a | 10411 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e12f03d7 SL |
10412 | #ifdef CONFIG_KPROBE_EVENTS |
10413 | perf_pmu_register(&perf_kprobe, "kprobe", -1); | |
10414 | #endif | |
33ea4b24 SL |
10415 | #ifdef CONFIG_UPROBE_EVENTS |
10416 | perf_pmu_register(&perf_uprobe, "uprobe", -1); | |
10417 | #endif | |
e077df4f | 10418 | } |
6fb2915d | 10419 | |
6fb2915d LZ |
10420 | static void perf_event_free_filter(struct perf_event *event) |
10421 | { | |
10422 | ftrace_profile_free_filter(event); | |
10423 | } | |
10424 | ||
aa6a5f3c AS |
10425 | #ifdef CONFIG_BPF_SYSCALL |
10426 | static void bpf_overflow_handler(struct perf_event *event, | |
10427 | struct perf_sample_data *data, | |
10428 | struct pt_regs *regs) | |
10429 | { | |
10430 | struct bpf_perf_event_data_kern ctx = { | |
10431 | .data = data, | |
7d9285e8 | 10432 | .event = event, |
aa6a5f3c | 10433 | }; |
594286b7 | 10434 | struct bpf_prog *prog; |
aa6a5f3c AS |
10435 | int ret = 0; |
10436 | ||
c895f6f7 | 10437 | ctx.regs = perf_arch_bpf_user_pt_regs(regs); |
aa6a5f3c AS |
10438 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) |
10439 | goto out; | |
10440 | rcu_read_lock(); | |
594286b7 | 10441 | prog = READ_ONCE(event->prog); |
16817ad7 | 10442 | if (prog) { |
0eed2822 | 10443 | perf_prepare_sample(data, event, regs); |
594286b7 | 10444 | ret = bpf_prog_run(prog, &ctx); |
16817ad7 | 10445 | } |
aa6a5f3c AS |
10446 | rcu_read_unlock(); |
10447 | out: | |
10448 | __this_cpu_dec(bpf_prog_active); | |
aa6a5f3c AS |
10449 | if (!ret) |
10450 | return; | |
10451 | ||
10452 | event->orig_overflow_handler(event, data, regs); | |
10453 | } | |
10454 | ||
82e6b1ee AN |
10455 | static int perf_event_set_bpf_handler(struct perf_event *event, |
10456 | struct bpf_prog *prog, | |
10457 | u64 bpf_cookie) | |
aa6a5f3c | 10458 | { |
aa6a5f3c AS |
10459 | if (event->overflow_handler_context) |
10460 | /* hw breakpoint or kernel counter */ | |
10461 | return -EINVAL; | |
10462 | ||
10463 | if (event->prog) | |
10464 | return -EEXIST; | |
10465 | ||
652c1b17 AN |
10466 | if (prog->type != BPF_PROG_TYPE_PERF_EVENT) |
10467 | return -EINVAL; | |
aa6a5f3c | 10468 | |
5d99cb2c SL |
10469 | if (event->attr.precise_ip && |
10470 | prog->call_get_stack && | |
16817ad7 | 10471 | (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || |
5d99cb2c SL |
10472 | event->attr.exclude_callchain_kernel || |
10473 | event->attr.exclude_callchain_user)) { | |
10474 | /* | |
10475 | * On perf_event with precise_ip, calling bpf_get_stack() | |
10476 | * may trigger unwinder warnings and occasional crashes. | |
10477 | * bpf_get_[stack|stackid] works around this issue by using | |
10478 | * callchain attached to perf_sample_data. If the | |
10479 | * perf_event does not full (kernel and user) callchain | |
10480 | * attached to perf_sample_data, do not allow attaching BPF | |
10481 | * program that calls bpf_get_[stack|stackid]. | |
10482 | */ | |
5d99cb2c SL |
10483 | return -EPROTO; |
10484 | } | |
10485 | ||
aa6a5f3c | 10486 | event->prog = prog; |
82e6b1ee | 10487 | event->bpf_cookie = bpf_cookie; |
aa6a5f3c AS |
10488 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); |
10489 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
10490 | return 0; | |
10491 | } | |
10492 | ||
10493 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
10494 | { | |
10495 | struct bpf_prog *prog = event->prog; | |
10496 | ||
10497 | if (!prog) | |
10498 | return; | |
10499 | ||
10500 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
10501 | event->prog = NULL; | |
10502 | bpf_prog_put(prog); | |
10503 | } | |
10504 | #else | |
82e6b1ee AN |
10505 | static int perf_event_set_bpf_handler(struct perf_event *event, |
10506 | struct bpf_prog *prog, | |
10507 | u64 bpf_cookie) | |
aa6a5f3c AS |
10508 | { |
10509 | return -EOPNOTSUPP; | |
10510 | } | |
10511 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
10512 | { | |
10513 | } | |
10514 | #endif | |
10515 | ||
e12f03d7 SL |
10516 | /* |
10517 | * returns true if the event is a tracepoint, or a kprobe/upprobe created | |
10518 | * with perf_event_open() | |
10519 | */ | |
10520 | static inline bool perf_event_is_tracing(struct perf_event *event) | |
10521 | { | |
10522 | if (event->pmu == &perf_tracepoint) | |
10523 | return true; | |
10524 | #ifdef CONFIG_KPROBE_EVENTS | |
10525 | if (event->pmu == &perf_kprobe) | |
10526 | return true; | |
33ea4b24 SL |
10527 | #endif |
10528 | #ifdef CONFIG_UPROBE_EVENTS | |
10529 | if (event->pmu == &perf_uprobe) | |
10530 | return true; | |
e12f03d7 SL |
10531 | #endif |
10532 | return false; | |
10533 | } | |
10534 | ||
82e6b1ee AN |
10535 | int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, |
10536 | u64 bpf_cookie) | |
2541517c | 10537 | { |
64ad7556 | 10538 | bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp; |
2541517c | 10539 | |
e12f03d7 | 10540 | if (!perf_event_is_tracing(event)) |
82e6b1ee | 10541 | return perf_event_set_bpf_handler(event, prog, bpf_cookie); |
2541517c | 10542 | |
64ad7556 DK |
10543 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; |
10544 | is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; | |
98b5c2c6 | 10545 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; |
cf5f5cea | 10546 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
64ad7556 | 10547 | if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp) |
98b5c2c6 | 10548 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
2541517c AS |
10549 | return -EINVAL; |
10550 | ||
64ad7556 | 10551 | if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) || |
cf5f5cea | 10552 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || |
652c1b17 | 10553 | (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) |
2541517c | 10554 | return -EINVAL; |
2541517c | 10555 | |
64ad7556 DK |
10556 | if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable && !is_uprobe) |
10557 | /* only uprobe programs are allowed to be sleepable */ | |
10558 | return -EINVAL; | |
10559 | ||
9802d865 | 10560 | /* Kprobe override only works for kprobes, not uprobes. */ |
64ad7556 | 10561 | if (prog->kprobe_override && !is_kprobe) |
9802d865 | 10562 | return -EINVAL; |
9802d865 | 10563 | |
cf5f5cea | 10564 | if (is_tracepoint || is_syscall_tp) { |
32bbe007 AS |
10565 | int off = trace_event_get_offsets(event->tp_event); |
10566 | ||
652c1b17 | 10567 | if (prog->aux->max_ctx_offset > off) |
32bbe007 | 10568 | return -EACCES; |
32bbe007 | 10569 | } |
2541517c | 10570 | |
82e6b1ee | 10571 | return perf_event_attach_bpf_prog(event, prog, bpf_cookie); |
2541517c AS |
10572 | } |
10573 | ||
b89fbfbb | 10574 | void perf_event_free_bpf_prog(struct perf_event *event) |
2541517c | 10575 | { |
e12f03d7 | 10576 | if (!perf_event_is_tracing(event)) { |
0b4c6841 | 10577 | perf_event_free_bpf_handler(event); |
2541517c | 10578 | return; |
2541517c | 10579 | } |
e87c6bc3 | 10580 | perf_event_detach_bpf_prog(event); |
2541517c AS |
10581 | } |
10582 | ||
e077df4f | 10583 | #else |
6fb2915d | 10584 | |
b0a873eb | 10585 | static inline void perf_tp_register(void) |
e077df4f | 10586 | { |
e077df4f | 10587 | } |
6fb2915d | 10588 | |
6fb2915d LZ |
10589 | static void perf_event_free_filter(struct perf_event *event) |
10590 | { | |
10591 | } | |
10592 | ||
82e6b1ee AN |
10593 | int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, |
10594 | u64 bpf_cookie) | |
2541517c AS |
10595 | { |
10596 | return -ENOENT; | |
10597 | } | |
10598 | ||
b89fbfbb | 10599 | void perf_event_free_bpf_prog(struct perf_event *event) |
2541517c AS |
10600 | { |
10601 | } | |
07b139c8 | 10602 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 10603 | |
24f1e32c | 10604 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 10605 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 10606 | { |
f5ffe02e FW |
10607 | struct perf_sample_data sample; |
10608 | struct pt_regs *regs = data; | |
10609 | ||
fd0d000b | 10610 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 10611 | |
a4eaf7f1 | 10612 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 10613 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
10614 | } |
10615 | #endif | |
10616 | ||
375637bc AS |
10617 | /* |
10618 | * Allocate a new address filter | |
10619 | */ | |
10620 | static struct perf_addr_filter * | |
10621 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
10622 | { | |
10623 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
10624 | struct perf_addr_filter *filter; | |
10625 | ||
10626 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
10627 | if (!filter) | |
10628 | return NULL; | |
10629 | ||
10630 | INIT_LIST_HEAD(&filter->entry); | |
10631 | list_add_tail(&filter->entry, filters); | |
10632 | ||
10633 | return filter; | |
10634 | } | |
10635 | ||
10636 | static void free_filters_list(struct list_head *filters) | |
10637 | { | |
10638 | struct perf_addr_filter *filter, *iter; | |
10639 | ||
10640 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
9511bce9 | 10641 | path_put(&filter->path); |
375637bc AS |
10642 | list_del(&filter->entry); |
10643 | kfree(filter); | |
10644 | } | |
10645 | } | |
10646 | ||
10647 | /* | |
10648 | * Free existing address filters and optionally install new ones | |
10649 | */ | |
10650 | static void perf_addr_filters_splice(struct perf_event *event, | |
10651 | struct list_head *head) | |
10652 | { | |
10653 | unsigned long flags; | |
10654 | LIST_HEAD(list); | |
10655 | ||
10656 | if (!has_addr_filter(event)) | |
10657 | return; | |
10658 | ||
10659 | /* don't bother with children, they don't have their own filters */ | |
10660 | if (event->parent) | |
10661 | return; | |
10662 | ||
10663 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
10664 | ||
10665 | list_splice_init(&event->addr_filters.list, &list); | |
10666 | if (head) | |
10667 | list_splice(head, &event->addr_filters.list); | |
10668 | ||
10669 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
10670 | ||
10671 | free_filters_list(&list); | |
10672 | } | |
10673 | ||
10674 | /* | |
10675 | * Scan through mm's vmas and see if one of them matches the | |
10676 | * @filter; if so, adjust filter's address range. | |
c1e8d7c6 | 10677 | * Called with mm::mmap_lock down for reading. |
375637bc | 10678 | */ |
c60f83b8 AS |
10679 | static void perf_addr_filter_apply(struct perf_addr_filter *filter, |
10680 | struct mm_struct *mm, | |
10681 | struct perf_addr_filter_range *fr) | |
375637bc AS |
10682 | { |
10683 | struct vm_area_struct *vma; | |
fcb72a58 | 10684 | VMA_ITERATOR(vmi, mm, 0); |
375637bc | 10685 | |
fcb72a58 | 10686 | for_each_vma(vmi, vma) { |
c60f83b8 | 10687 | if (!vma->vm_file) |
375637bc AS |
10688 | continue; |
10689 | ||
c60f83b8 AS |
10690 | if (perf_addr_filter_vma_adjust(filter, vma, fr)) |
10691 | return; | |
375637bc | 10692 | } |
375637bc AS |
10693 | } |
10694 | ||
10695 | /* | |
10696 | * Update event's address range filters based on the | |
10697 | * task's existing mappings, if any. | |
10698 | */ | |
10699 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
10700 | { | |
10701 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
10702 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
10703 | struct perf_addr_filter *filter; | |
10704 | struct mm_struct *mm = NULL; | |
10705 | unsigned int count = 0; | |
10706 | unsigned long flags; | |
10707 | ||
10708 | /* | |
10709 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
10710 | * will stop on the parent's child_mutex that our caller is also holding | |
10711 | */ | |
10712 | if (task == TASK_TOMBSTONE) | |
10713 | return; | |
10714 | ||
52a44f83 | 10715 | if (ifh->nr_file_filters) { |
b89a05b2 | 10716 | mm = get_task_mm(task); |
52a44f83 AS |
10717 | if (!mm) |
10718 | goto restart; | |
375637bc | 10719 | |
d8ed45c5 | 10720 | mmap_read_lock(mm); |
52a44f83 | 10721 | } |
375637bc AS |
10722 | |
10723 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
10724 | list_for_each_entry(filter, &ifh->list, entry) { | |
52a44f83 AS |
10725 | if (filter->path.dentry) { |
10726 | /* | |
10727 | * Adjust base offset if the filter is associated to a | |
10728 | * binary that needs to be mapped: | |
10729 | */ | |
10730 | event->addr_filter_ranges[count].start = 0; | |
10731 | event->addr_filter_ranges[count].size = 0; | |
375637bc | 10732 | |
c60f83b8 | 10733 | perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]); |
52a44f83 AS |
10734 | } else { |
10735 | event->addr_filter_ranges[count].start = filter->offset; | |
10736 | event->addr_filter_ranges[count].size = filter->size; | |
10737 | } | |
375637bc AS |
10738 | |
10739 | count++; | |
10740 | } | |
10741 | ||
10742 | event->addr_filters_gen++; | |
10743 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
10744 | ||
52a44f83 | 10745 | if (ifh->nr_file_filters) { |
d8ed45c5 | 10746 | mmap_read_unlock(mm); |
375637bc | 10747 | |
52a44f83 AS |
10748 | mmput(mm); |
10749 | } | |
375637bc AS |
10750 | |
10751 | restart: | |
767ae086 | 10752 | perf_event_stop(event, 1); |
375637bc AS |
10753 | } |
10754 | ||
10755 | /* | |
10756 | * Address range filtering: limiting the data to certain | |
10757 | * instruction address ranges. Filters are ioctl()ed to us from | |
10758 | * userspace as ascii strings. | |
10759 | * | |
10760 | * Filter string format: | |
10761 | * | |
10762 | * ACTION RANGE_SPEC | |
10763 | * where ACTION is one of the | |
10764 | * * "filter": limit the trace to this region | |
10765 | * * "start": start tracing from this address | |
10766 | * * "stop": stop tracing at this address/region; | |
10767 | * RANGE_SPEC is | |
10768 | * * for kernel addresses: <start address>[/<size>] | |
10769 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
10770 | * | |
6ed70cf3 AS |
10771 | * if <size> is not specified or is zero, the range is treated as a single |
10772 | * address; not valid for ACTION=="filter". | |
375637bc AS |
10773 | */ |
10774 | enum { | |
e96271f3 | 10775 | IF_ACT_NONE = -1, |
375637bc AS |
10776 | IF_ACT_FILTER, |
10777 | IF_ACT_START, | |
10778 | IF_ACT_STOP, | |
10779 | IF_SRC_FILE, | |
10780 | IF_SRC_KERNEL, | |
10781 | IF_SRC_FILEADDR, | |
10782 | IF_SRC_KERNELADDR, | |
10783 | }; | |
10784 | ||
10785 | enum { | |
10786 | IF_STATE_ACTION = 0, | |
10787 | IF_STATE_SOURCE, | |
10788 | IF_STATE_END, | |
10789 | }; | |
10790 | ||
10791 | static const match_table_t if_tokens = { | |
10792 | { IF_ACT_FILTER, "filter" }, | |
10793 | { IF_ACT_START, "start" }, | |
10794 | { IF_ACT_STOP, "stop" }, | |
10795 | { IF_SRC_FILE, "%u/%u@%s" }, | |
10796 | { IF_SRC_KERNEL, "%u/%u" }, | |
10797 | { IF_SRC_FILEADDR, "%u@%s" }, | |
10798 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 10799 | { IF_ACT_NONE, NULL }, |
375637bc AS |
10800 | }; |
10801 | ||
10802 | /* | |
10803 | * Address filter string parser | |
10804 | */ | |
10805 | static int | |
10806 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
10807 | struct list_head *filters) | |
10808 | { | |
10809 | struct perf_addr_filter *filter = NULL; | |
10810 | char *start, *orig, *filename = NULL; | |
375637bc AS |
10811 | substring_t args[MAX_OPT_ARGS]; |
10812 | int state = IF_STATE_ACTION, token; | |
10813 | unsigned int kernel = 0; | |
10814 | int ret = -EINVAL; | |
10815 | ||
10816 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
10817 | if (!fstr) | |
10818 | return -ENOMEM; | |
10819 | ||
10820 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
6ed70cf3 AS |
10821 | static const enum perf_addr_filter_action_t actions[] = { |
10822 | [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER, | |
10823 | [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START, | |
10824 | [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP, | |
10825 | }; | |
375637bc AS |
10826 | ret = -EINVAL; |
10827 | ||
10828 | if (!*start) | |
10829 | continue; | |
10830 | ||
10831 | /* filter definition begins */ | |
10832 | if (state == IF_STATE_ACTION) { | |
10833 | filter = perf_addr_filter_new(event, filters); | |
10834 | if (!filter) | |
10835 | goto fail; | |
10836 | } | |
10837 | ||
10838 | token = match_token(start, if_tokens, args); | |
10839 | switch (token) { | |
10840 | case IF_ACT_FILTER: | |
10841 | case IF_ACT_START: | |
375637bc AS |
10842 | case IF_ACT_STOP: |
10843 | if (state != IF_STATE_ACTION) | |
10844 | goto fail; | |
10845 | ||
6ed70cf3 | 10846 | filter->action = actions[token]; |
375637bc AS |
10847 | state = IF_STATE_SOURCE; |
10848 | break; | |
10849 | ||
10850 | case IF_SRC_KERNELADDR: | |
10851 | case IF_SRC_KERNEL: | |
10852 | kernel = 1; | |
df561f66 | 10853 | fallthrough; |
375637bc AS |
10854 | |
10855 | case IF_SRC_FILEADDR: | |
10856 | case IF_SRC_FILE: | |
10857 | if (state != IF_STATE_SOURCE) | |
10858 | goto fail; | |
10859 | ||
375637bc AS |
10860 | *args[0].to = 0; |
10861 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
10862 | if (ret) | |
10863 | goto fail; | |
10864 | ||
6ed70cf3 | 10865 | if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) { |
375637bc AS |
10866 | *args[1].to = 0; |
10867 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
10868 | if (ret) | |
10869 | goto fail; | |
10870 | } | |
10871 | ||
4059ffd0 | 10872 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
6ed70cf3 | 10873 | int fpos = token == IF_SRC_FILE ? 2 : 1; |
4059ffd0 | 10874 | |
7bdb157c | 10875 | kfree(filename); |
4059ffd0 | 10876 | filename = match_strdup(&args[fpos]); |
375637bc AS |
10877 | if (!filename) { |
10878 | ret = -ENOMEM; | |
10879 | goto fail; | |
10880 | } | |
10881 | } | |
10882 | ||
10883 | state = IF_STATE_END; | |
10884 | break; | |
10885 | ||
10886 | default: | |
10887 | goto fail; | |
10888 | } | |
10889 | ||
10890 | /* | |
10891 | * Filter definition is fully parsed, validate and install it. | |
10892 | * Make sure that it doesn't contradict itself or the event's | |
10893 | * attribute. | |
10894 | */ | |
10895 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 10896 | ret = -EINVAL; |
375637bc | 10897 | |
6ed70cf3 AS |
10898 | /* |
10899 | * ACTION "filter" must have a non-zero length region | |
10900 | * specified. | |
10901 | */ | |
10902 | if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER && | |
10903 | !filter->size) | |
10904 | goto fail; | |
10905 | ||
375637bc AS |
10906 | if (!kernel) { |
10907 | if (!filename) | |
10908 | goto fail; | |
10909 | ||
6ce77bfd AS |
10910 | /* |
10911 | * For now, we only support file-based filters | |
10912 | * in per-task events; doing so for CPU-wide | |
10913 | * events requires additional context switching | |
10914 | * trickery, since same object code will be | |
10915 | * mapped at different virtual addresses in | |
10916 | * different processes. | |
10917 | */ | |
10918 | ret = -EOPNOTSUPP; | |
10919 | if (!event->ctx->task) | |
7bdb157c | 10920 | goto fail; |
6ce77bfd | 10921 | |
375637bc | 10922 | /* look up the path and grab its inode */ |
9511bce9 SL |
10923 | ret = kern_path(filename, LOOKUP_FOLLOW, |
10924 | &filter->path); | |
375637bc | 10925 | if (ret) |
7bdb157c | 10926 | goto fail; |
375637bc AS |
10927 | |
10928 | ret = -EINVAL; | |
9511bce9 SL |
10929 | if (!filter->path.dentry || |
10930 | !S_ISREG(d_inode(filter->path.dentry) | |
10931 | ->i_mode)) | |
375637bc | 10932 | goto fail; |
6ce77bfd AS |
10933 | |
10934 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
10935 | } |
10936 | ||
10937 | /* ready to consume more filters */ | |
d680ff24 AH |
10938 | kfree(filename); |
10939 | filename = NULL; | |
375637bc AS |
10940 | state = IF_STATE_ACTION; |
10941 | filter = NULL; | |
d680ff24 | 10942 | kernel = 0; |
375637bc AS |
10943 | } |
10944 | } | |
10945 | ||
10946 | if (state != IF_STATE_ACTION) | |
10947 | goto fail; | |
10948 | ||
7bdb157c | 10949 | kfree(filename); |
375637bc AS |
10950 | kfree(orig); |
10951 | ||
10952 | return 0; | |
10953 | ||
375637bc | 10954 | fail: |
7bdb157c | 10955 | kfree(filename); |
375637bc AS |
10956 | free_filters_list(filters); |
10957 | kfree(orig); | |
10958 | ||
10959 | return ret; | |
10960 | } | |
10961 | ||
10962 | static int | |
10963 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
10964 | { | |
10965 | LIST_HEAD(filters); | |
10966 | int ret; | |
10967 | ||
10968 | /* | |
10969 | * Since this is called in perf_ioctl() path, we're already holding | |
10970 | * ctx::mutex. | |
10971 | */ | |
10972 | lockdep_assert_held(&event->ctx->mutex); | |
10973 | ||
10974 | if (WARN_ON_ONCE(event->parent)) | |
10975 | return -EINVAL; | |
10976 | ||
375637bc AS |
10977 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
10978 | if (ret) | |
6ce77bfd | 10979 | goto fail_clear_files; |
375637bc AS |
10980 | |
10981 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
10982 | if (ret) |
10983 | goto fail_free_filters; | |
375637bc AS |
10984 | |
10985 | /* remove existing filters, if any */ | |
10986 | perf_addr_filters_splice(event, &filters); | |
10987 | ||
10988 | /* install new filters */ | |
10989 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
10990 | ||
6ce77bfd AS |
10991 | return ret; |
10992 | ||
10993 | fail_free_filters: | |
10994 | free_filters_list(&filters); | |
10995 | ||
10996 | fail_clear_files: | |
10997 | event->addr_filters.nr_file_filters = 0; | |
10998 | ||
375637bc AS |
10999 | return ret; |
11000 | } | |
11001 | ||
c796bbbe AS |
11002 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
11003 | { | |
c796bbbe | 11004 | int ret = -EINVAL; |
e12f03d7 | 11005 | char *filter_str; |
c796bbbe AS |
11006 | |
11007 | filter_str = strndup_user(arg, PAGE_SIZE); | |
11008 | if (IS_ERR(filter_str)) | |
11009 | return PTR_ERR(filter_str); | |
11010 | ||
e12f03d7 SL |
11011 | #ifdef CONFIG_EVENT_TRACING |
11012 | if (perf_event_is_tracing(event)) { | |
11013 | struct perf_event_context *ctx = event->ctx; | |
11014 | ||
11015 | /* | |
11016 | * Beware, here be dragons!! | |
11017 | * | |
11018 | * the tracepoint muck will deadlock against ctx->mutex, but | |
11019 | * the tracepoint stuff does not actually need it. So | |
11020 | * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we | |
11021 | * already have a reference on ctx. | |
11022 | * | |
11023 | * This can result in event getting moved to a different ctx, | |
11024 | * but that does not affect the tracepoint state. | |
11025 | */ | |
11026 | mutex_unlock(&ctx->mutex); | |
11027 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
11028 | mutex_lock(&ctx->mutex); | |
11029 | } else | |
11030 | #endif | |
11031 | if (has_addr_filter(event)) | |
375637bc | 11032 | ret = perf_event_set_addr_filter(event, filter_str); |
c796bbbe AS |
11033 | |
11034 | kfree(filter_str); | |
11035 | return ret; | |
11036 | } | |
11037 | ||
b0a873eb PZ |
11038 | /* |
11039 | * hrtimer based swevent callback | |
11040 | */ | |
f29ac756 | 11041 | |
b0a873eb | 11042 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 11043 | { |
b0a873eb PZ |
11044 | enum hrtimer_restart ret = HRTIMER_RESTART; |
11045 | struct perf_sample_data data; | |
11046 | struct pt_regs *regs; | |
11047 | struct perf_event *event; | |
11048 | u64 period; | |
f29ac756 | 11049 | |
b0a873eb | 11050 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
11051 | |
11052 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
11053 | return HRTIMER_NORESTART; | |
11054 | ||
b0a873eb | 11055 | event->pmu->read(event); |
f344011c | 11056 | |
fd0d000b | 11057 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
11058 | regs = get_irq_regs(); |
11059 | ||
11060 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 11061 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 11062 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
11063 | ret = HRTIMER_NORESTART; |
11064 | } | |
24f1e32c | 11065 | |
b0a873eb PZ |
11066 | period = max_t(u64, 10000, event->hw.sample_period); |
11067 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 11068 | |
b0a873eb | 11069 | return ret; |
f29ac756 PZ |
11070 | } |
11071 | ||
b0a873eb | 11072 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 11073 | { |
b0a873eb | 11074 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
11075 | s64 period; |
11076 | ||
11077 | if (!is_sampling_event(event)) | |
11078 | return; | |
f5ffe02e | 11079 | |
5d508e82 FBH |
11080 | period = local64_read(&hwc->period_left); |
11081 | if (period) { | |
11082 | if (period < 0) | |
11083 | period = 10000; | |
fa407f35 | 11084 | |
5d508e82 FBH |
11085 | local64_set(&hwc->period_left, 0); |
11086 | } else { | |
11087 | period = max_t(u64, 10000, hwc->sample_period); | |
11088 | } | |
3497d206 | 11089 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
30f9028b | 11090 | HRTIMER_MODE_REL_PINNED_HARD); |
24f1e32c | 11091 | } |
b0a873eb PZ |
11092 | |
11093 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 11094 | { |
b0a873eb PZ |
11095 | struct hw_perf_event *hwc = &event->hw; |
11096 | ||
6c7e550f | 11097 | if (is_sampling_event(event)) { |
b0a873eb | 11098 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 11099 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
11100 | |
11101 | hrtimer_cancel(&hwc->hrtimer); | |
11102 | } | |
24f1e32c FW |
11103 | } |
11104 | ||
ba3dd36c PZ |
11105 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
11106 | { | |
11107 | struct hw_perf_event *hwc = &event->hw; | |
11108 | ||
11109 | if (!is_sampling_event(event)) | |
11110 | return; | |
11111 | ||
30f9028b | 11112 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
ba3dd36c PZ |
11113 | hwc->hrtimer.function = perf_swevent_hrtimer; |
11114 | ||
11115 | /* | |
11116 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
11117 | * mapping and avoid the whole period adjust feedback stuff. | |
11118 | */ | |
11119 | if (event->attr.freq) { | |
11120 | long freq = event->attr.sample_freq; | |
11121 | ||
11122 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
11123 | hwc->sample_period = event->attr.sample_period; | |
11124 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 11125 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
11126 | event->attr.freq = 0; |
11127 | } | |
11128 | } | |
11129 | ||
b0a873eb PZ |
11130 | /* |
11131 | * Software event: cpu wall time clock | |
11132 | */ | |
11133 | ||
11134 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 11135 | { |
b0a873eb PZ |
11136 | s64 prev; |
11137 | u64 now; | |
11138 | ||
a4eaf7f1 | 11139 | now = local_clock(); |
b0a873eb PZ |
11140 | prev = local64_xchg(&event->hw.prev_count, now); |
11141 | local64_add(now - prev, &event->count); | |
24f1e32c | 11142 | } |
24f1e32c | 11143 | |
a4eaf7f1 | 11144 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 11145 | { |
a4eaf7f1 | 11146 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 11147 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
11148 | } |
11149 | ||
a4eaf7f1 | 11150 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 11151 | { |
b0a873eb PZ |
11152 | perf_swevent_cancel_hrtimer(event); |
11153 | cpu_clock_event_update(event); | |
11154 | } | |
f29ac756 | 11155 | |
a4eaf7f1 PZ |
11156 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
11157 | { | |
11158 | if (flags & PERF_EF_START) | |
11159 | cpu_clock_event_start(event, flags); | |
6a694a60 | 11160 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
11161 | |
11162 | return 0; | |
11163 | } | |
11164 | ||
11165 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
11166 | { | |
11167 | cpu_clock_event_stop(event, flags); | |
11168 | } | |
11169 | ||
b0a873eb PZ |
11170 | static void cpu_clock_event_read(struct perf_event *event) |
11171 | { | |
11172 | cpu_clock_event_update(event); | |
11173 | } | |
f344011c | 11174 | |
b0a873eb PZ |
11175 | static int cpu_clock_event_init(struct perf_event *event) |
11176 | { | |
0d6d062c | 11177 | if (event->attr.type != perf_cpu_clock.type) |
b0a873eb PZ |
11178 | return -ENOENT; |
11179 | ||
11180 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
11181 | return -ENOENT; | |
11182 | ||
2481c5fa SE |
11183 | /* |
11184 | * no branch sampling for software events | |
11185 | */ | |
11186 | if (has_branch_stack(event)) | |
11187 | return -EOPNOTSUPP; | |
11188 | ||
ba3dd36c PZ |
11189 | perf_swevent_init_hrtimer(event); |
11190 | ||
b0a873eb | 11191 | return 0; |
f29ac756 PZ |
11192 | } |
11193 | ||
b0a873eb | 11194 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
11195 | .task_ctx_nr = perf_sw_context, |
11196 | ||
34f43927 | 11197 | .capabilities = PERF_PMU_CAP_NO_NMI, |
0d6d062c | 11198 | .dev = PMU_NULL_DEV, |
34f43927 | 11199 | |
b0a873eb | 11200 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
11201 | .add = cpu_clock_event_add, |
11202 | .del = cpu_clock_event_del, | |
11203 | .start = cpu_clock_event_start, | |
11204 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
11205 | .read = cpu_clock_event_read, |
11206 | }; | |
11207 | ||
11208 | /* | |
11209 | * Software event: task time clock | |
11210 | */ | |
11211 | ||
11212 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 11213 | { |
b0a873eb PZ |
11214 | u64 prev; |
11215 | s64 delta; | |
5c92d124 | 11216 | |
b0a873eb PZ |
11217 | prev = local64_xchg(&event->hw.prev_count, now); |
11218 | delta = now - prev; | |
11219 | local64_add(delta, &event->count); | |
11220 | } | |
5c92d124 | 11221 | |
a4eaf7f1 | 11222 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 11223 | { |
a4eaf7f1 | 11224 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 11225 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
11226 | } |
11227 | ||
a4eaf7f1 | 11228 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
11229 | { |
11230 | perf_swevent_cancel_hrtimer(event); | |
11231 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
11232 | } |
11233 | ||
11234 | static int task_clock_event_add(struct perf_event *event, int flags) | |
11235 | { | |
11236 | if (flags & PERF_EF_START) | |
11237 | task_clock_event_start(event, flags); | |
6a694a60 | 11238 | perf_event_update_userpage(event); |
b0a873eb | 11239 | |
a4eaf7f1 PZ |
11240 | return 0; |
11241 | } | |
11242 | ||
11243 | static void task_clock_event_del(struct perf_event *event, int flags) | |
11244 | { | |
11245 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
11246 | } |
11247 | ||
11248 | static void task_clock_event_read(struct perf_event *event) | |
11249 | { | |
768a06e2 PZ |
11250 | u64 now = perf_clock(); |
11251 | u64 delta = now - event->ctx->timestamp; | |
11252 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
11253 | |
11254 | task_clock_event_update(event, time); | |
11255 | } | |
11256 | ||
11257 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 11258 | { |
0d6d062c | 11259 | if (event->attr.type != perf_task_clock.type) |
b0a873eb PZ |
11260 | return -ENOENT; |
11261 | ||
11262 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
11263 | return -ENOENT; | |
11264 | ||
2481c5fa SE |
11265 | /* |
11266 | * no branch sampling for software events | |
11267 | */ | |
11268 | if (has_branch_stack(event)) | |
11269 | return -EOPNOTSUPP; | |
11270 | ||
ba3dd36c PZ |
11271 | perf_swevent_init_hrtimer(event); |
11272 | ||
b0a873eb | 11273 | return 0; |
6fb2915d LZ |
11274 | } |
11275 | ||
b0a873eb | 11276 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
11277 | .task_ctx_nr = perf_sw_context, |
11278 | ||
34f43927 | 11279 | .capabilities = PERF_PMU_CAP_NO_NMI, |
0d6d062c | 11280 | .dev = PMU_NULL_DEV, |
34f43927 | 11281 | |
b0a873eb | 11282 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
11283 | .add = task_clock_event_add, |
11284 | .del = task_clock_event_del, | |
11285 | .start = task_clock_event_start, | |
11286 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
11287 | .read = task_clock_event_read, |
11288 | }; | |
6fb2915d | 11289 | |
ad5133b7 | 11290 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 11291 | { |
e077df4f | 11292 | } |
6fb2915d | 11293 | |
fbbe0701 SB |
11294 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
11295 | { | |
11296 | } | |
11297 | ||
ad5133b7 | 11298 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 11299 | { |
ad5133b7 | 11300 | return 0; |
6fb2915d LZ |
11301 | } |
11302 | ||
81ec3f3c JO |
11303 | static int perf_event_nop_int(struct perf_event *event, u64 value) |
11304 | { | |
11305 | return 0; | |
11306 | } | |
11307 | ||
18ab2cd3 | 11308 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
11309 | |
11310 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 11311 | { |
fbbe0701 SB |
11312 | __this_cpu_write(nop_txn_flags, flags); |
11313 | ||
11314 | if (flags & ~PERF_PMU_TXN_ADD) | |
11315 | return; | |
11316 | ||
ad5133b7 | 11317 | perf_pmu_disable(pmu); |
6fb2915d LZ |
11318 | } |
11319 | ||
ad5133b7 PZ |
11320 | static int perf_pmu_commit_txn(struct pmu *pmu) |
11321 | { | |
fbbe0701 SB |
11322 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
11323 | ||
11324 | __this_cpu_write(nop_txn_flags, 0); | |
11325 | ||
11326 | if (flags & ~PERF_PMU_TXN_ADD) | |
11327 | return 0; | |
11328 | ||
ad5133b7 PZ |
11329 | perf_pmu_enable(pmu); |
11330 | return 0; | |
11331 | } | |
e077df4f | 11332 | |
ad5133b7 | 11333 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 11334 | { |
fbbe0701 SB |
11335 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
11336 | ||
11337 | __this_cpu_write(nop_txn_flags, 0); | |
11338 | ||
11339 | if (flags & ~PERF_PMU_TXN_ADD) | |
11340 | return; | |
11341 | ||
ad5133b7 | 11342 | perf_pmu_enable(pmu); |
24f1e32c FW |
11343 | } |
11344 | ||
35edc2a5 PZ |
11345 | static int perf_event_idx_default(struct perf_event *event) |
11346 | { | |
c719f560 | 11347 | return 0; |
35edc2a5 PZ |
11348 | } |
11349 | ||
51676957 PZ |
11350 | static void free_pmu_context(struct pmu *pmu) |
11351 | { | |
bd275681 | 11352 | free_percpu(pmu->cpu_pmu_context); |
24f1e32c | 11353 | } |
6e855cd4 | 11354 | |
8dc85d54 | 11355 | /* |
6e855cd4 | 11356 | * Let userspace know that this PMU supports address range filtering: |
8dc85d54 | 11357 | */ |
6e855cd4 AS |
11358 | static ssize_t nr_addr_filters_show(struct device *dev, |
11359 | struct device_attribute *attr, | |
11360 | char *page) | |
24f1e32c | 11361 | { |
6e855cd4 AS |
11362 | struct pmu *pmu = dev_get_drvdata(dev); |
11363 | ||
dca6344d | 11364 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); |
6e855cd4 AS |
11365 | } |
11366 | DEVICE_ATTR_RO(nr_addr_filters); | |
11367 | ||
2e80a82a | 11368 | static struct idr pmu_idr; |
d6d020e9 | 11369 | |
abe43400 PZ |
11370 | static ssize_t |
11371 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
11372 | { | |
11373 | struct pmu *pmu = dev_get_drvdata(dev); | |
11374 | ||
dca6344d | 11375 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->type); |
abe43400 | 11376 | } |
90826ca7 | 11377 | static DEVICE_ATTR_RO(type); |
abe43400 | 11378 | |
62b85639 SE |
11379 | static ssize_t |
11380 | perf_event_mux_interval_ms_show(struct device *dev, | |
11381 | struct device_attribute *attr, | |
11382 | char *page) | |
11383 | { | |
11384 | struct pmu *pmu = dev_get_drvdata(dev); | |
11385 | ||
dca6344d | 11386 | return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->hrtimer_interval_ms); |
62b85639 SE |
11387 | } |
11388 | ||
272325c4 PZ |
11389 | static DEFINE_MUTEX(mux_interval_mutex); |
11390 | ||
62b85639 SE |
11391 | static ssize_t |
11392 | perf_event_mux_interval_ms_store(struct device *dev, | |
11393 | struct device_attribute *attr, | |
11394 | const char *buf, size_t count) | |
11395 | { | |
11396 | struct pmu *pmu = dev_get_drvdata(dev); | |
11397 | int timer, cpu, ret; | |
11398 | ||
11399 | ret = kstrtoint(buf, 0, &timer); | |
11400 | if (ret) | |
11401 | return ret; | |
11402 | ||
11403 | if (timer < 1) | |
11404 | return -EINVAL; | |
11405 | ||
11406 | /* same value, noting to do */ | |
11407 | if (timer == pmu->hrtimer_interval_ms) | |
11408 | return count; | |
11409 | ||
272325c4 | 11410 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
11411 | pmu->hrtimer_interval_ms = timer; |
11412 | ||
11413 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 11414 | cpus_read_lock(); |
272325c4 | 11415 | for_each_online_cpu(cpu) { |
bd275681 PZ |
11416 | struct perf_cpu_pmu_context *cpc; |
11417 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); | |
11418 | cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
62b85639 | 11419 | |
1af6239d | 11420 | cpu_function_call(cpu, perf_mux_hrtimer_restart_ipi, cpc); |
62b85639 | 11421 | } |
a63fbed7 | 11422 | cpus_read_unlock(); |
272325c4 | 11423 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
11424 | |
11425 | return count; | |
11426 | } | |
90826ca7 | 11427 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 11428 | |
90826ca7 GKH |
11429 | static struct attribute *pmu_dev_attrs[] = { |
11430 | &dev_attr_type.attr, | |
11431 | &dev_attr_perf_event_mux_interval_ms.attr, | |
652ffc21 GK |
11432 | &dev_attr_nr_addr_filters.attr, |
11433 | NULL, | |
11434 | }; | |
11435 | ||
11436 | static umode_t pmu_dev_is_visible(struct kobject *kobj, struct attribute *a, int n) | |
11437 | { | |
11438 | struct device *dev = kobj_to_dev(kobj); | |
11439 | struct pmu *pmu = dev_get_drvdata(dev); | |
11440 | ||
388a1fb7 | 11441 | if (n == 2 && !pmu->nr_addr_filters) |
652ffc21 GK |
11442 | return 0; |
11443 | ||
11444 | return a->mode; | |
652ffc21 GK |
11445 | } |
11446 | ||
11447 | static struct attribute_group pmu_dev_attr_group = { | |
11448 | .is_visible = pmu_dev_is_visible, | |
11449 | .attrs = pmu_dev_attrs, | |
11450 | }; | |
11451 | ||
11452 | static const struct attribute_group *pmu_dev_groups[] = { | |
11453 | &pmu_dev_attr_group, | |
90826ca7 | 11454 | NULL, |
abe43400 PZ |
11455 | }; |
11456 | ||
11457 | static int pmu_bus_running; | |
11458 | static struct bus_type pmu_bus = { | |
11459 | .name = "event_source", | |
90826ca7 | 11460 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
11461 | }; |
11462 | ||
11463 | static void pmu_dev_release(struct device *dev) | |
11464 | { | |
11465 | kfree(dev); | |
11466 | } | |
11467 | ||
11468 | static int pmu_dev_alloc(struct pmu *pmu) | |
11469 | { | |
11470 | int ret = -ENOMEM; | |
11471 | ||
11472 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
11473 | if (!pmu->dev) | |
11474 | goto out; | |
11475 | ||
0c9d42ed | 11476 | pmu->dev->groups = pmu->attr_groups; |
abe43400 | 11477 | device_initialize(pmu->dev); |
abe43400 PZ |
11478 | |
11479 | dev_set_drvdata(pmu->dev, pmu); | |
11480 | pmu->dev->bus = &pmu_bus; | |
143f83e2 | 11481 | pmu->dev->parent = pmu->parent; |
abe43400 | 11482 | pmu->dev->release = pmu_dev_release; |
e8d7a90c CZ |
11483 | |
11484 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
11485 | if (ret) | |
11486 | goto free_dev; | |
11487 | ||
abe43400 PZ |
11488 | ret = device_add(pmu->dev); |
11489 | if (ret) | |
11490 | goto free_dev; | |
11491 | ||
652ffc21 | 11492 | if (pmu->attr_update) { |
f3a3a825 | 11493 | ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update); |
652ffc21 GK |
11494 | if (ret) |
11495 | goto del_dev; | |
11496 | } | |
f3a3a825 | 11497 | |
abe43400 PZ |
11498 | out: |
11499 | return ret; | |
11500 | ||
6e855cd4 AS |
11501 | del_dev: |
11502 | device_del(pmu->dev); | |
11503 | ||
abe43400 PZ |
11504 | free_dev: |
11505 | put_device(pmu->dev); | |
11506 | goto out; | |
11507 | } | |
11508 | ||
547e9fd7 | 11509 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 11510 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 11511 | |
03d8e80b | 11512 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 11513 | { |
66d258c5 | 11514 | int cpu, ret, max = PERF_TYPE_MAX; |
24f1e32c | 11515 | |
b0a873eb | 11516 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
11517 | ret = -ENOMEM; |
11518 | pmu->pmu_disable_count = alloc_percpu(int); | |
11519 | if (!pmu->pmu_disable_count) | |
11520 | goto unlock; | |
f29ac756 | 11521 | |
2e80a82a | 11522 | pmu->type = -1; |
0d6d062c RB |
11523 | if (WARN_ONCE(!name, "Can not register anonymous pmu.\n")) { |
11524 | ret = -EINVAL; | |
11525 | goto free_pdc; | |
11526 | } | |
11527 | ||
2e80a82a PZ |
11528 | pmu->name = name; |
11529 | ||
0d6d062c RB |
11530 | if (type >= 0) |
11531 | max = type; | |
66d258c5 | 11532 | |
0d6d062c RB |
11533 | ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL); |
11534 | if (ret < 0) | |
11535 | goto free_pdc; | |
66d258c5 | 11536 | |
0d6d062c | 11537 | WARN_ON(type >= 0 && ret != type); |
66d258c5 | 11538 | |
0d6d062c | 11539 | type = ret; |
2e80a82a PZ |
11540 | pmu->type = type; |
11541 | ||
0d6d062c | 11542 | if (pmu_bus_running && !pmu->dev) { |
abe43400 PZ |
11543 | ret = pmu_dev_alloc(pmu); |
11544 | if (ret) | |
11545 | goto free_idr; | |
11546 | } | |
11547 | ||
c4814202 | 11548 | ret = -ENOMEM; |
bd275681 PZ |
11549 | pmu->cpu_pmu_context = alloc_percpu(struct perf_cpu_pmu_context); |
11550 | if (!pmu->cpu_pmu_context) | |
abe43400 | 11551 | goto free_dev; |
f344011c | 11552 | |
108b02cf | 11553 | for_each_possible_cpu(cpu) { |
bd275681 | 11554 | struct perf_cpu_pmu_context *cpc; |
9e630205 | 11555 | |
bd275681 PZ |
11556 | cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); |
11557 | __perf_init_event_pmu_context(&cpc->epc, pmu); | |
11558 | __perf_mux_hrtimer_init(cpc, cpu); | |
108b02cf | 11559 | } |
76e1d904 | 11560 | |
ad5133b7 PZ |
11561 | if (!pmu->start_txn) { |
11562 | if (pmu->pmu_enable) { | |
11563 | /* | |
11564 | * If we have pmu_enable/pmu_disable calls, install | |
11565 | * transaction stubs that use that to try and batch | |
11566 | * hardware accesses. | |
11567 | */ | |
11568 | pmu->start_txn = perf_pmu_start_txn; | |
11569 | pmu->commit_txn = perf_pmu_commit_txn; | |
11570 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
11571 | } else { | |
fbbe0701 | 11572 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
11573 | pmu->commit_txn = perf_pmu_nop_int; |
11574 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 11575 | } |
5c92d124 | 11576 | } |
15dbf27c | 11577 | |
ad5133b7 PZ |
11578 | if (!pmu->pmu_enable) { |
11579 | pmu->pmu_enable = perf_pmu_nop_void; | |
11580 | pmu->pmu_disable = perf_pmu_nop_void; | |
11581 | } | |
11582 | ||
81ec3f3c JO |
11583 | if (!pmu->check_period) |
11584 | pmu->check_period = perf_event_nop_int; | |
11585 | ||
35edc2a5 PZ |
11586 | if (!pmu->event_idx) |
11587 | pmu->event_idx = perf_event_idx_default; | |
11588 | ||
0d6d062c | 11589 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 11590 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
11591 | ret = 0; |
11592 | unlock: | |
b0a873eb PZ |
11593 | mutex_unlock(&pmus_lock); |
11594 | ||
33696fc0 | 11595 | return ret; |
108b02cf | 11596 | |
abe43400 | 11597 | free_dev: |
0d6d062c RB |
11598 | if (pmu->dev && pmu->dev != PMU_NULL_DEV) { |
11599 | device_del(pmu->dev); | |
11600 | put_device(pmu->dev); | |
11601 | } | |
abe43400 | 11602 | |
2e80a82a | 11603 | free_idr: |
0d6d062c | 11604 | idr_remove(&pmu_idr, pmu->type); |
2e80a82a | 11605 | |
108b02cf PZ |
11606 | free_pdc: |
11607 | free_percpu(pmu->pmu_disable_count); | |
11608 | goto unlock; | |
f29ac756 | 11609 | } |
c464c76e | 11610 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 11611 | |
b0a873eb | 11612 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 11613 | { |
b0a873eb PZ |
11614 | mutex_lock(&pmus_lock); |
11615 | list_del_rcu(&pmu->entry); | |
5c92d124 | 11616 | |
0475f9ea | 11617 | /* |
cde8e884 PZ |
11618 | * We dereference the pmu list under both SRCU and regular RCU, so |
11619 | * synchronize against both of those. | |
0475f9ea | 11620 | */ |
b0a873eb | 11621 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 11622 | synchronize_rcu(); |
d6d020e9 | 11623 | |
33696fc0 | 11624 | free_percpu(pmu->pmu_disable_count); |
0d6d062c RB |
11625 | idr_remove(&pmu_idr, pmu->type); |
11626 | if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) { | |
0933840a JO |
11627 | if (pmu->nr_addr_filters) |
11628 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
11629 | device_del(pmu->dev); | |
11630 | put_device(pmu->dev); | |
11631 | } | |
51676957 | 11632 | free_pmu_context(pmu); |
a9f97721 | 11633 | mutex_unlock(&pmus_lock); |
b0a873eb | 11634 | } |
c464c76e | 11635 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 11636 | |
e321d02d KL |
11637 | static inline bool has_extended_regs(struct perf_event *event) |
11638 | { | |
11639 | return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || | |
11640 | (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); | |
11641 | } | |
11642 | ||
cc34b98b MR |
11643 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
11644 | { | |
ccd41c86 | 11645 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
11646 | int ret; |
11647 | ||
11648 | if (!try_module_get(pmu->module)) | |
11649 | return -ENODEV; | |
ccd41c86 | 11650 | |
0c7296ca PZ |
11651 | /* |
11652 | * A number of pmu->event_init() methods iterate the sibling_list to, | |
11653 | * for example, validate if the group fits on the PMU. Therefore, | |
11654 | * if this is a sibling event, acquire the ctx->mutex to protect | |
11655 | * the sibling_list. | |
11656 | */ | |
11657 | if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { | |
8b10c5e2 PZ |
11658 | /* |
11659 | * This ctx->mutex can nest when we're called through | |
11660 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
11661 | */ | |
11662 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
11663 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
11664 | BUG_ON(!ctx); |
11665 | } | |
11666 | ||
cc34b98b MR |
11667 | event->pmu = pmu; |
11668 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
11669 | |
11670 | if (ctx) | |
11671 | perf_event_ctx_unlock(event->group_leader, ctx); | |
11672 | ||
cc6795ae | 11673 | if (!ret) { |
e321d02d KL |
11674 | if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && |
11675 | has_extended_regs(event)) | |
11676 | ret = -EOPNOTSUPP; | |
11677 | ||
cc6795ae | 11678 | if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE && |
e321d02d | 11679 | event_has_any_exclude_flag(event)) |
cc6795ae | 11680 | ret = -EINVAL; |
e321d02d KL |
11681 | |
11682 | if (ret && event->destroy) | |
11683 | event->destroy(event); | |
cc6795ae AM |
11684 | } |
11685 | ||
cc34b98b MR |
11686 | if (ret) |
11687 | module_put(pmu->module); | |
11688 | ||
11689 | return ret; | |
11690 | } | |
11691 | ||
18ab2cd3 | 11692 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 11693 | { |
55bcf6ef | 11694 | bool extended_type = false; |
66d258c5 | 11695 | int idx, type, ret; |
85c617ab | 11696 | struct pmu *pmu; |
b0a873eb PZ |
11697 | |
11698 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 11699 | |
0d6d062c RB |
11700 | /* |
11701 | * Save original type before calling pmu->event_init() since certain | |
11702 | * pmus overwrites event->attr.type to forward event to another pmu. | |
11703 | */ | |
11704 | event->orig_type = event->attr.type; | |
11705 | ||
40999312 KL |
11706 | /* Try parent's PMU first: */ |
11707 | if (event->parent && event->parent->pmu) { | |
11708 | pmu = event->parent->pmu; | |
11709 | ret = perf_try_init_event(pmu, event); | |
11710 | if (!ret) | |
11711 | goto unlock; | |
11712 | } | |
11713 | ||
66d258c5 PZ |
11714 | /* |
11715 | * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE | |
11716 | * are often aliases for PERF_TYPE_RAW. | |
11717 | */ | |
11718 | type = event->attr.type; | |
55bcf6ef KL |
11719 | if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) { |
11720 | type = event->attr.config >> PERF_PMU_TYPE_SHIFT; | |
11721 | if (!type) { | |
11722 | type = PERF_TYPE_RAW; | |
11723 | } else { | |
11724 | extended_type = true; | |
11725 | event->attr.config &= PERF_HW_EVENT_MASK; | |
11726 | } | |
11727 | } | |
66d258c5 PZ |
11728 | |
11729 | again: | |
2e80a82a | 11730 | rcu_read_lock(); |
66d258c5 | 11731 | pmu = idr_find(&pmu_idr, type); |
2e80a82a | 11732 | rcu_read_unlock(); |
940c5b29 | 11733 | if (pmu) { |
55bcf6ef KL |
11734 | if (event->attr.type != type && type != PERF_TYPE_RAW && |
11735 | !(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE)) | |
11736 | goto fail; | |
11737 | ||
cc34b98b | 11738 | ret = perf_try_init_event(pmu, event); |
55bcf6ef | 11739 | if (ret == -ENOENT && event->attr.type != type && !extended_type) { |
66d258c5 PZ |
11740 | type = event->attr.type; |
11741 | goto again; | |
11742 | } | |
11743 | ||
940c5b29 LM |
11744 | if (ret) |
11745 | pmu = ERR_PTR(ret); | |
66d258c5 | 11746 | |
2e80a82a | 11747 | goto unlock; |
940c5b29 | 11748 | } |
2e80a82a | 11749 | |
9f0bff11 | 11750 | list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) { |
cc34b98b | 11751 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 11752 | if (!ret) |
e5f4d339 | 11753 | goto unlock; |
76e1d904 | 11754 | |
b0a873eb PZ |
11755 | if (ret != -ENOENT) { |
11756 | pmu = ERR_PTR(ret); | |
e5f4d339 | 11757 | goto unlock; |
f344011c | 11758 | } |
5c92d124 | 11759 | } |
55bcf6ef | 11760 | fail: |
e5f4d339 PZ |
11761 | pmu = ERR_PTR(-ENOENT); |
11762 | unlock: | |
b0a873eb | 11763 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 11764 | |
4aeb0b42 | 11765 | return pmu; |
5c92d124 IM |
11766 | } |
11767 | ||
f2fb6bef KL |
11768 | static void attach_sb_event(struct perf_event *event) |
11769 | { | |
11770 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
11771 | ||
11772 | raw_spin_lock(&pel->lock); | |
11773 | list_add_rcu(&event->sb_list, &pel->list); | |
11774 | raw_spin_unlock(&pel->lock); | |
11775 | } | |
11776 | ||
aab5b71e PZ |
11777 | /* |
11778 | * We keep a list of all !task (and therefore per-cpu) events | |
11779 | * that need to receive side-band records. | |
11780 | * | |
11781 | * This avoids having to scan all the various PMU per-cpu contexts | |
11782 | * looking for them. | |
11783 | */ | |
f2fb6bef KL |
11784 | static void account_pmu_sb_event(struct perf_event *event) |
11785 | { | |
a4f144eb | 11786 | if (is_sb_event(event)) |
f2fb6bef KL |
11787 | attach_sb_event(event); |
11788 | } | |
11789 | ||
555e0c1e FW |
11790 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
11791 | static void account_freq_event_nohz(void) | |
11792 | { | |
11793 | #ifdef CONFIG_NO_HZ_FULL | |
11794 | /* Lock so we don't race with concurrent unaccount */ | |
11795 | spin_lock(&nr_freq_lock); | |
11796 | if (atomic_inc_return(&nr_freq_events) == 1) | |
11797 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
11798 | spin_unlock(&nr_freq_lock); | |
11799 | #endif | |
11800 | } | |
11801 | ||
11802 | static void account_freq_event(void) | |
11803 | { | |
11804 | if (tick_nohz_full_enabled()) | |
11805 | account_freq_event_nohz(); | |
11806 | else | |
11807 | atomic_inc(&nr_freq_events); | |
11808 | } | |
11809 | ||
11810 | ||
766d6c07 FW |
11811 | static void account_event(struct perf_event *event) |
11812 | { | |
25432ae9 PZ |
11813 | bool inc = false; |
11814 | ||
4beb31f3 FW |
11815 | if (event->parent) |
11816 | return; | |
11817 | ||
a5398bff | 11818 | if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) |
25432ae9 | 11819 | inc = true; |
766d6c07 FW |
11820 | if (event->attr.mmap || event->attr.mmap_data) |
11821 | atomic_inc(&nr_mmap_events); | |
88a16a13 JO |
11822 | if (event->attr.build_id) |
11823 | atomic_inc(&nr_build_id_events); | |
766d6c07 FW |
11824 | if (event->attr.comm) |
11825 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
11826 | if (event->attr.namespaces) |
11827 | atomic_inc(&nr_namespaces_events); | |
96aaab68 NK |
11828 | if (event->attr.cgroup) |
11829 | atomic_inc(&nr_cgroup_events); | |
766d6c07 FW |
11830 | if (event->attr.task) |
11831 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
11832 | if (event->attr.freq) |
11833 | account_freq_event(); | |
45ac1403 AH |
11834 | if (event->attr.context_switch) { |
11835 | atomic_inc(&nr_switch_events); | |
25432ae9 | 11836 | inc = true; |
45ac1403 | 11837 | } |
4beb31f3 | 11838 | if (has_branch_stack(event)) |
25432ae9 | 11839 | inc = true; |
4beb31f3 | 11840 | if (is_cgroup_event(event)) |
25432ae9 | 11841 | inc = true; |
76193a94 SL |
11842 | if (event->attr.ksymbol) |
11843 | atomic_inc(&nr_ksymbol_events); | |
6ee52e2a SL |
11844 | if (event->attr.bpf_event) |
11845 | atomic_inc(&nr_bpf_events); | |
e17d43b9 AH |
11846 | if (event->attr.text_poke) |
11847 | atomic_inc(&nr_text_poke_events); | |
25432ae9 | 11848 | |
9107c89e | 11849 | if (inc) { |
5bce9db1 AS |
11850 | /* |
11851 | * We need the mutex here because static_branch_enable() | |
11852 | * must complete *before* the perf_sched_count increment | |
11853 | * becomes visible. | |
11854 | */ | |
9107c89e PZ |
11855 | if (atomic_inc_not_zero(&perf_sched_count)) |
11856 | goto enabled; | |
11857 | ||
11858 | mutex_lock(&perf_sched_mutex); | |
11859 | if (!atomic_read(&perf_sched_count)) { | |
11860 | static_branch_enable(&perf_sched_events); | |
11861 | /* | |
11862 | * Guarantee that all CPUs observe they key change and | |
11863 | * call the perf scheduling hooks before proceeding to | |
11864 | * install events that need them. | |
11865 | */ | |
0809d954 | 11866 | synchronize_rcu(); |
9107c89e PZ |
11867 | } |
11868 | /* | |
11869 | * Now that we have waited for the sync_sched(), allow further | |
11870 | * increments to by-pass the mutex. | |
11871 | */ | |
11872 | atomic_inc(&perf_sched_count); | |
11873 | mutex_unlock(&perf_sched_mutex); | |
11874 | } | |
11875 | enabled: | |
4beb31f3 | 11876 | |
f2fb6bef | 11877 | account_pmu_sb_event(event); |
766d6c07 FW |
11878 | } |
11879 | ||
0793a61d | 11880 | /* |
788faab7 | 11881 | * Allocate and initialize an event structure |
0793a61d | 11882 | */ |
cdd6c482 | 11883 | static struct perf_event * |
c3f00c70 | 11884 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
11885 | struct task_struct *task, |
11886 | struct perf_event *group_leader, | |
11887 | struct perf_event *parent_event, | |
4dc0da86 | 11888 | perf_overflow_handler_t overflow_handler, |
79dff51e | 11889 | void *context, int cgroup_fd) |
0793a61d | 11890 | { |
51b0fe39 | 11891 | struct pmu *pmu; |
cdd6c482 IM |
11892 | struct perf_event *event; |
11893 | struct hw_perf_event *hwc; | |
90983b16 | 11894 | long err = -EINVAL; |
ff65338e | 11895 | int node; |
0793a61d | 11896 | |
66832eb4 ON |
11897 | if ((unsigned)cpu >= nr_cpu_ids) { |
11898 | if (!task || cpu != -1) | |
11899 | return ERR_PTR(-EINVAL); | |
11900 | } | |
97ba62b2 ME |
11901 | if (attr->sigtrap && !task) { |
11902 | /* Requires a task: avoid signalling random tasks. */ | |
11903 | return ERR_PTR(-EINVAL); | |
11904 | } | |
66832eb4 | 11905 | |
ff65338e NK |
11906 | node = (cpu >= 0) ? cpu_to_node(cpu) : -1; |
11907 | event = kmem_cache_alloc_node(perf_event_cache, GFP_KERNEL | __GFP_ZERO, | |
11908 | node); | |
cdd6c482 | 11909 | if (!event) |
d5d2bc0d | 11910 | return ERR_PTR(-ENOMEM); |
0793a61d | 11911 | |
04289bb9 | 11912 | /* |
cdd6c482 | 11913 | * Single events are their own group leaders, with an |
04289bb9 IM |
11914 | * empty sibling list: |
11915 | */ | |
11916 | if (!group_leader) | |
cdd6c482 | 11917 | group_leader = event; |
04289bb9 | 11918 | |
cdd6c482 IM |
11919 | mutex_init(&event->child_mutex); |
11920 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 11921 | |
cdd6c482 IM |
11922 | INIT_LIST_HEAD(&event->event_entry); |
11923 | INIT_LIST_HEAD(&event->sibling_list); | |
6668128a | 11924 | INIT_LIST_HEAD(&event->active_list); |
8e1a2031 | 11925 | init_event_group(event); |
10c6db11 | 11926 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 11927 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 11928 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
11929 | INIT_HLIST_NODE(&event->hlist_entry); |
11930 | ||
10c6db11 | 11931 | |
cdd6c482 | 11932 | init_waitqueue_head(&event->waitq); |
ca6c2132 PZ |
11933 | init_irq_work(&event->pending_irq, perf_pending_irq); |
11934 | init_task_work(&event->pending_task, perf_pending_task); | |
0793a61d | 11935 | |
cdd6c482 | 11936 | mutex_init(&event->mmap_mutex); |
375637bc | 11937 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 11938 | |
a6fa941d | 11939 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
11940 | event->cpu = cpu; |
11941 | event->attr = *attr; | |
11942 | event->group_leader = group_leader; | |
11943 | event->pmu = NULL; | |
cdd6c482 | 11944 | event->oncpu = -1; |
a96bbc16 | 11945 | |
cdd6c482 | 11946 | event->parent = parent_event; |
b84fbc9f | 11947 | |
17cf22c3 | 11948 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 11949 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 11950 | |
cdd6c482 | 11951 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 11952 | |
e3265a43 NK |
11953 | if (parent_event) |
11954 | event->event_caps = parent_event->event_caps; | |
11955 | ||
d580ff86 PZ |
11956 | if (task) { |
11957 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 11958 | /* |
50f16a8b PZ |
11959 | * XXX pmu::event_init needs to know what task to account to |
11960 | * and we cannot use the ctx information because we need the | |
11961 | * pmu before we get a ctx. | |
d580ff86 | 11962 | */ |
7b3c92b8 | 11963 | event->hw.target = get_task_struct(task); |
d580ff86 PZ |
11964 | } |
11965 | ||
34f43927 PZ |
11966 | event->clock = &local_clock; |
11967 | if (parent_event) | |
11968 | event->clock = parent_event->clock; | |
11969 | ||
4dc0da86 | 11970 | if (!overflow_handler && parent_event) { |
b326e956 | 11971 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 11972 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 11973 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c | 11974 | if (overflow_handler == bpf_overflow_handler) { |
85192dbf | 11975 | struct bpf_prog *prog = parent_event->prog; |
aa6a5f3c | 11976 | |
85192dbf | 11977 | bpf_prog_inc(prog); |
aa6a5f3c AS |
11978 | event->prog = prog; |
11979 | event->orig_overflow_handler = | |
11980 | parent_event->orig_overflow_handler; | |
11981 | } | |
11982 | #endif | |
4dc0da86 | 11983 | } |
66832eb4 | 11984 | |
1879445d WN |
11985 | if (overflow_handler) { |
11986 | event->overflow_handler = overflow_handler; | |
11987 | event->overflow_handler_context = context; | |
9ecda41a WN |
11988 | } else if (is_write_backward(event)){ |
11989 | event->overflow_handler = perf_event_output_backward; | |
11990 | event->overflow_handler_context = NULL; | |
1879445d | 11991 | } else { |
9ecda41a | 11992 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
11993 | event->overflow_handler_context = NULL; |
11994 | } | |
97eaf530 | 11995 | |
0231bb53 | 11996 | perf_event__state_init(event); |
a86ed508 | 11997 | |
4aeb0b42 | 11998 | pmu = NULL; |
b8e83514 | 11999 | |
cdd6c482 | 12000 | hwc = &event->hw; |
bd2b5b12 | 12001 | hwc->sample_period = attr->sample_period; |
0d48696f | 12002 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 12003 | hwc->sample_period = 1; |
eced1dfc | 12004 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 12005 | |
e7850595 | 12006 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 12007 | |
2023b359 | 12008 | /* |
ba5213ae PZ |
12009 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
12010 | * See perf_output_read(). | |
2023b359 | 12011 | */ |
ba5213ae | 12012 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 12013 | goto err_ns; |
a46a2300 YZ |
12014 | |
12015 | if (!has_branch_stack(event)) | |
12016 | event->attr.branch_sample_type = 0; | |
2023b359 | 12017 | |
b0a873eb | 12018 | pmu = perf_init_event(event); |
85c617ab | 12019 | if (IS_ERR(pmu)) { |
4aeb0b42 | 12020 | err = PTR_ERR(pmu); |
90983b16 | 12021 | goto err_ns; |
621a01ea | 12022 | } |
d5d2bc0d | 12023 | |
09f4e8f0 | 12024 | /* |
bd275681 PZ |
12025 | * Disallow uncore-task events. Similarly, disallow uncore-cgroup |
12026 | * events (they don't make sense as the cgroup will be different | |
12027 | * on other CPUs in the uncore mask). | |
09f4e8f0 | 12028 | */ |
bd275681 | 12029 | if (pmu->task_ctx_nr == perf_invalid_context && (task || cgroup_fd != -1)) { |
09f4e8f0 PZ |
12030 | err = -EINVAL; |
12031 | goto err_pmu; | |
12032 | } | |
12033 | ||
ab43762e AS |
12034 | if (event->attr.aux_output && |
12035 | !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) { | |
12036 | err = -EOPNOTSUPP; | |
12037 | goto err_pmu; | |
12038 | } | |
12039 | ||
98add2af PZ |
12040 | if (cgroup_fd != -1) { |
12041 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
12042 | if (err) | |
12043 | goto err_pmu; | |
12044 | } | |
12045 | ||
bed5b25a AS |
12046 | err = exclusive_event_init(event); |
12047 | if (err) | |
12048 | goto err_pmu; | |
12049 | ||
375637bc | 12050 | if (has_addr_filter(event)) { |
c60f83b8 AS |
12051 | event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters, |
12052 | sizeof(struct perf_addr_filter_range), | |
12053 | GFP_KERNEL); | |
12054 | if (!event->addr_filter_ranges) { | |
36cc2b92 | 12055 | err = -ENOMEM; |
375637bc | 12056 | goto err_per_task; |
36cc2b92 | 12057 | } |
375637bc | 12058 | |
18736eef AS |
12059 | /* |
12060 | * Clone the parent's vma offsets: they are valid until exec() | |
12061 | * even if the mm is not shared with the parent. | |
12062 | */ | |
12063 | if (event->parent) { | |
12064 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
12065 | ||
12066 | raw_spin_lock_irq(&ifh->lock); | |
c60f83b8 AS |
12067 | memcpy(event->addr_filter_ranges, |
12068 | event->parent->addr_filter_ranges, | |
12069 | pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range)); | |
18736eef AS |
12070 | raw_spin_unlock_irq(&ifh->lock); |
12071 | } | |
12072 | ||
375637bc AS |
12073 | /* force hw sync on the address filters */ |
12074 | event->addr_filters_gen = 1; | |
12075 | } | |
12076 | ||
cdd6c482 | 12077 | if (!event->parent) { |
927c7a9e | 12078 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 12079 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 12080 | if (err) |
375637bc | 12081 | goto err_addr_filters; |
d010b332 | 12082 | } |
f344011c | 12083 | } |
9ee318a7 | 12084 | |
da97e184 JFG |
12085 | err = security_perf_event_alloc(event); |
12086 | if (err) | |
12087 | goto err_callchain_buffer; | |
12088 | ||
927a5570 AS |
12089 | /* symmetric to unaccount_event() in _free_event() */ |
12090 | account_event(event); | |
12091 | ||
cdd6c482 | 12092 | return event; |
90983b16 | 12093 | |
da97e184 JFG |
12094 | err_callchain_buffer: |
12095 | if (!event->parent) { | |
12096 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
12097 | put_callchain_buffers(); | |
12098 | } | |
375637bc | 12099 | err_addr_filters: |
c60f83b8 | 12100 | kfree(event->addr_filter_ranges); |
375637bc | 12101 | |
bed5b25a AS |
12102 | err_per_task: |
12103 | exclusive_event_destroy(event); | |
12104 | ||
90983b16 | 12105 | err_pmu: |
98add2af PZ |
12106 | if (is_cgroup_event(event)) |
12107 | perf_detach_cgroup(event); | |
90983b16 FW |
12108 | if (event->destroy) |
12109 | event->destroy(event); | |
c464c76e | 12110 | module_put(pmu->module); |
90983b16 | 12111 | err_ns: |
621b6d2e PB |
12112 | if (event->hw.target) |
12113 | put_task_struct(event->hw.target); | |
4674ffe2 | 12114 | call_rcu(&event->rcu_head, free_event_rcu); |
90983b16 FW |
12115 | |
12116 | return ERR_PTR(err); | |
0793a61d TG |
12117 | } |
12118 | ||
cdd6c482 IM |
12119 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
12120 | struct perf_event_attr *attr) | |
974802ea | 12121 | { |
974802ea | 12122 | u32 size; |
cdf8073d | 12123 | int ret; |
974802ea | 12124 | |
c2ba8f41 | 12125 | /* Zero the full structure, so that a short copy will be nice. */ |
974802ea PZ |
12126 | memset(attr, 0, sizeof(*attr)); |
12127 | ||
12128 | ret = get_user(size, &uattr->size); | |
12129 | if (ret) | |
12130 | return ret; | |
12131 | ||
c2ba8f41 AS |
12132 | /* ABI compatibility quirk: */ |
12133 | if (!size) | |
974802ea | 12134 | size = PERF_ATTR_SIZE_VER0; |
c2ba8f41 | 12135 | if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
974802ea PZ |
12136 | goto err_size; |
12137 | ||
c2ba8f41 AS |
12138 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
12139 | if (ret) { | |
12140 | if (ret == -E2BIG) | |
12141 | goto err_size; | |
12142 | return ret; | |
974802ea PZ |
12143 | } |
12144 | ||
f12f42ac MX |
12145 | attr->size = size; |
12146 | ||
a4faf00d | 12147 | if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) |
974802ea PZ |
12148 | return -EINVAL; |
12149 | ||
12150 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
12151 | return -EINVAL; | |
12152 | ||
12153 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
12154 | return -EINVAL; | |
12155 | ||
bce38cd5 SE |
12156 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
12157 | u64 mask = attr->branch_sample_type; | |
12158 | ||
12159 | /* only using defined bits */ | |
12160 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
12161 | return -EINVAL; | |
12162 | ||
12163 | /* at least one branch bit must be set */ | |
12164 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
12165 | return -EINVAL; | |
12166 | ||
bce38cd5 SE |
12167 | /* propagate priv level, when not set for branch */ |
12168 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
12169 | ||
12170 | /* exclude_kernel checked on syscall entry */ | |
12171 | if (!attr->exclude_kernel) | |
12172 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
12173 | ||
12174 | if (!attr->exclude_user) | |
12175 | mask |= PERF_SAMPLE_BRANCH_USER; | |
12176 | ||
12177 | if (!attr->exclude_hv) | |
12178 | mask |= PERF_SAMPLE_BRANCH_HV; | |
12179 | /* | |
12180 | * adjust user setting (for HW filter setup) | |
12181 | */ | |
12182 | attr->branch_sample_type = mask; | |
12183 | } | |
e712209a | 12184 | /* privileged levels capture (kernel, hv): check permissions */ |
da97e184 JFG |
12185 | if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) { |
12186 | ret = perf_allow_kernel(attr); | |
12187 | if (ret) | |
12188 | return ret; | |
12189 | } | |
bce38cd5 | 12190 | } |
4018994f | 12191 | |
c5ebcedb | 12192 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 12193 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
12194 | if (ret) |
12195 | return ret; | |
12196 | } | |
12197 | ||
12198 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
12199 | if (!arch_perf_have_user_stack_dump()) | |
12200 | return -ENOSYS; | |
12201 | ||
12202 | /* | |
12203 | * We have __u32 type for the size, but so far | |
12204 | * we can only use __u16 as maximum due to the | |
12205 | * __u16 sample size limit. | |
12206 | */ | |
12207 | if (attr->sample_stack_user >= USHRT_MAX) | |
78b562fb | 12208 | return -EINVAL; |
c5ebcedb | 12209 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
78b562fb | 12210 | return -EINVAL; |
c5ebcedb | 12211 | } |
4018994f | 12212 | |
5f970521 JO |
12213 | if (!attr->sample_max_stack) |
12214 | attr->sample_max_stack = sysctl_perf_event_max_stack; | |
12215 | ||
60e2364e SE |
12216 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
12217 | ret = perf_reg_validate(attr->sample_regs_intr); | |
6546b19f NK |
12218 | |
12219 | #ifndef CONFIG_CGROUP_PERF | |
12220 | if (attr->sample_type & PERF_SAMPLE_CGROUP) | |
12221 | return -EINVAL; | |
12222 | #endif | |
2a6c6b7d KL |
12223 | if ((attr->sample_type & PERF_SAMPLE_WEIGHT) && |
12224 | (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) | |
12225 | return -EINVAL; | |
6546b19f | 12226 | |
2b26f0aa ME |
12227 | if (!attr->inherit && attr->inherit_thread) |
12228 | return -EINVAL; | |
12229 | ||
2e498d0a ME |
12230 | if (attr->remove_on_exec && attr->enable_on_exec) |
12231 | return -EINVAL; | |
12232 | ||
97ba62b2 ME |
12233 | if (attr->sigtrap && !attr->remove_on_exec) |
12234 | return -EINVAL; | |
12235 | ||
974802ea PZ |
12236 | out: |
12237 | return ret; | |
12238 | ||
12239 | err_size: | |
12240 | put_user(sizeof(*attr), &uattr->size); | |
12241 | ret = -E2BIG; | |
12242 | goto out; | |
12243 | } | |
12244 | ||
68e3c698 PZ |
12245 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
12246 | { | |
12247 | if (b < a) | |
12248 | swap(a, b); | |
12249 | ||
12250 | mutex_lock(a); | |
12251 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
12252 | } | |
12253 | ||
ac9721f3 PZ |
12254 | static int |
12255 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 12256 | { |
56de4e8f | 12257 | struct perf_buffer *rb = NULL; |
a4be7c27 PZ |
12258 | int ret = -EINVAL; |
12259 | ||
68e3c698 PZ |
12260 | if (!output_event) { |
12261 | mutex_lock(&event->mmap_mutex); | |
a4be7c27 | 12262 | goto set; |
68e3c698 | 12263 | } |
a4be7c27 | 12264 | |
ac9721f3 PZ |
12265 | /* don't allow circular references */ |
12266 | if (event == output_event) | |
a4be7c27 PZ |
12267 | goto out; |
12268 | ||
0f139300 PZ |
12269 | /* |
12270 | * Don't allow cross-cpu buffers | |
12271 | */ | |
12272 | if (output_event->cpu != event->cpu) | |
12273 | goto out; | |
12274 | ||
12275 | /* | |
76369139 | 12276 | * If its not a per-cpu rb, it must be the same task. |
0f139300 | 12277 | */ |
24d3ae2f | 12278 | if (output_event->cpu == -1 && output_event->hw.target != event->hw.target) |
0f139300 PZ |
12279 | goto out; |
12280 | ||
34f43927 PZ |
12281 | /* |
12282 | * Mixing clocks in the same buffer is trouble you don't need. | |
12283 | */ | |
12284 | if (output_event->clock != event->clock) | |
12285 | goto out; | |
12286 | ||
9ecda41a WN |
12287 | /* |
12288 | * Either writing ring buffer from beginning or from end. | |
12289 | * Mixing is not allowed. | |
12290 | */ | |
12291 | if (is_write_backward(output_event) != is_write_backward(event)) | |
12292 | goto out; | |
12293 | ||
45bfb2e5 PZ |
12294 | /* |
12295 | * If both events generate aux data, they must be on the same PMU | |
12296 | */ | |
12297 | if (has_aux(event) && has_aux(output_event) && | |
12298 | event->pmu != output_event->pmu) | |
12299 | goto out; | |
12300 | ||
68e3c698 PZ |
12301 | /* |
12302 | * Hold both mmap_mutex to serialize against perf_mmap_close(). Since | |
12303 | * output_event is already on rb->event_list, and the list iteration | |
12304 | * restarts after every removal, it is guaranteed this new event is | |
12305 | * observed *OR* if output_event is already removed, it's guaranteed we | |
12306 | * observe !rb->mmap_count. | |
12307 | */ | |
12308 | mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex); | |
a4be7c27 | 12309 | set: |
ac9721f3 PZ |
12310 | /* Can't redirect output if we've got an active mmap() */ |
12311 | if (atomic_read(&event->mmap_count)) | |
12312 | goto unlock; | |
a4be7c27 | 12313 | |
ac9721f3 | 12314 | if (output_event) { |
76369139 FW |
12315 | /* get the rb we want to redirect to */ |
12316 | rb = ring_buffer_get(output_event); | |
12317 | if (!rb) | |
ac9721f3 | 12318 | goto unlock; |
68e3c698 PZ |
12319 | |
12320 | /* did we race against perf_mmap_close() */ | |
12321 | if (!atomic_read(&rb->mmap_count)) { | |
12322 | ring_buffer_put(rb); | |
12323 | goto unlock; | |
12324 | } | |
a4be7c27 PZ |
12325 | } |
12326 | ||
b69cf536 | 12327 | ring_buffer_attach(event, rb); |
9bb5d40c | 12328 | |
a4be7c27 | 12329 | ret = 0; |
ac9721f3 PZ |
12330 | unlock: |
12331 | mutex_unlock(&event->mmap_mutex); | |
68e3c698 PZ |
12332 | if (output_event) |
12333 | mutex_unlock(&output_event->mmap_mutex); | |
ac9721f3 | 12334 | |
a4be7c27 | 12335 | out: |
a4be7c27 PZ |
12336 | return ret; |
12337 | } | |
12338 | ||
34f43927 PZ |
12339 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
12340 | { | |
12341 | bool nmi_safe = false; | |
12342 | ||
12343 | switch (clk_id) { | |
12344 | case CLOCK_MONOTONIC: | |
12345 | event->clock = &ktime_get_mono_fast_ns; | |
12346 | nmi_safe = true; | |
12347 | break; | |
12348 | ||
12349 | case CLOCK_MONOTONIC_RAW: | |
12350 | event->clock = &ktime_get_raw_fast_ns; | |
12351 | nmi_safe = true; | |
12352 | break; | |
12353 | ||
12354 | case CLOCK_REALTIME: | |
12355 | event->clock = &ktime_get_real_ns; | |
12356 | break; | |
12357 | ||
12358 | case CLOCK_BOOTTIME: | |
9285ec4c | 12359 | event->clock = &ktime_get_boottime_ns; |
34f43927 PZ |
12360 | break; |
12361 | ||
12362 | case CLOCK_TAI: | |
9285ec4c | 12363 | event->clock = &ktime_get_clocktai_ns; |
34f43927 PZ |
12364 | break; |
12365 | ||
12366 | default: | |
12367 | return -EINVAL; | |
12368 | } | |
12369 | ||
12370 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
12371 | return -EINVAL; | |
12372 | ||
12373 | return 0; | |
12374 | } | |
12375 | ||
b068fc04 ME |
12376 | static bool |
12377 | perf_check_permission(struct perf_event_attr *attr, struct task_struct *task) | |
12378 | { | |
12379 | unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS; | |
12380 | bool is_capable = perfmon_capable(); | |
12381 | ||
12382 | if (attr->sigtrap) { | |
12383 | /* | |
12384 | * perf_event_attr::sigtrap sends signals to the other task. | |
12385 | * Require the current task to also have CAP_KILL. | |
12386 | */ | |
12387 | rcu_read_lock(); | |
12388 | is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL); | |
12389 | rcu_read_unlock(); | |
12390 | ||
12391 | /* | |
12392 | * If the required capabilities aren't available, checks for | |
12393 | * ptrace permissions: upgrade to ATTACH, since sending signals | |
12394 | * can effectively change the target task. | |
12395 | */ | |
12396 | ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS; | |
12397 | } | |
12398 | ||
12399 | /* | |
12400 | * Preserve ptrace permission check for backwards compatibility. The | |
12401 | * ptrace check also includes checks that the current task and other | |
12402 | * task have matching uids, and is therefore not done here explicitly. | |
12403 | */ | |
12404 | return is_capable || ptrace_may_access(task, ptrace_mode); | |
12405 | } | |
12406 | ||
0793a61d | 12407 | /** |
cdd6c482 | 12408 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 12409 | * |
cdd6c482 | 12410 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 12411 | * @pid: target pid |
9f66a381 | 12412 | * @cpu: target cpu |
cdd6c482 | 12413 | * @group_fd: group leader event fd |
a1ddf524 | 12414 | * @flags: perf event open flags |
0793a61d | 12415 | */ |
cdd6c482 IM |
12416 | SYSCALL_DEFINE5(perf_event_open, |
12417 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 12418 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 12419 | { |
b04243ef | 12420 | struct perf_event *group_leader = NULL, *output_event = NULL; |
bd275681 | 12421 | struct perf_event_pmu_context *pmu_ctx; |
b04243ef | 12422 | struct perf_event *event, *sibling; |
cdd6c482 | 12423 | struct perf_event_attr attr; |
bd275681 | 12424 | struct perf_event_context *ctx; |
cdd6c482 | 12425 | struct file *event_file = NULL; |
2903ff01 | 12426 | struct fd group = {NULL, 0}; |
38a81da2 | 12427 | struct task_struct *task = NULL; |
89a1e187 | 12428 | struct pmu *pmu; |
ea635c64 | 12429 | int event_fd; |
b04243ef | 12430 | int move_group = 0; |
dc86cabe | 12431 | int err; |
a21b0b35 | 12432 | int f_flags = O_RDWR; |
79dff51e | 12433 | int cgroup_fd = -1; |
0793a61d | 12434 | |
2743a5b0 | 12435 | /* for future expandability... */ |
e5d1367f | 12436 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
12437 | return -EINVAL; |
12438 | ||
0a041ebc | 12439 | err = perf_copy_attr(attr_uptr, &attr); |
da97e184 JFG |
12440 | if (err) |
12441 | return err; | |
12442 | ||
0a041ebc NK |
12443 | /* Do we allow access to perf_event_open(2) ? */ |
12444 | err = security_perf_event_open(&attr, PERF_SECURITY_OPEN); | |
dc86cabe IM |
12445 | if (err) |
12446 | return err; | |
eab656ae | 12447 | |
0764771d | 12448 | if (!attr.exclude_kernel) { |
da97e184 JFG |
12449 | err = perf_allow_kernel(&attr); |
12450 | if (err) | |
12451 | return err; | |
0764771d PZ |
12452 | } |
12453 | ||
e4222673 | 12454 | if (attr.namespaces) { |
18aa1856 | 12455 | if (!perfmon_capable()) |
e4222673 HB |
12456 | return -EACCES; |
12457 | } | |
12458 | ||
df58ab24 | 12459 | if (attr.freq) { |
cdd6c482 | 12460 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 12461 | return -EINVAL; |
0819b2e3 PZ |
12462 | } else { |
12463 | if (attr.sample_period & (1ULL << 63)) | |
12464 | return -EINVAL; | |
df58ab24 PZ |
12465 | } |
12466 | ||
fc7ce9c7 | 12467 | /* Only privileged users can get physical addresses */ |
da97e184 JFG |
12468 | if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { |
12469 | err = perf_allow_kernel(&attr); | |
12470 | if (err) | |
12471 | return err; | |
12472 | } | |
fc7ce9c7 | 12473 | |
08ef1af4 OM |
12474 | /* REGS_INTR can leak data, lockdown must prevent this */ |
12475 | if (attr.sample_type & PERF_SAMPLE_REGS_INTR) { | |
12476 | err = security_locked_down(LOCKDOWN_PERF); | |
12477 | if (err) | |
12478 | return err; | |
12479 | } | |
b0c8fdc7 | 12480 | |
e5d1367f SE |
12481 | /* |
12482 | * In cgroup mode, the pid argument is used to pass the fd | |
12483 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
12484 | * designates the cpu on which to monitor threads from that | |
12485 | * cgroup. | |
12486 | */ | |
12487 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
12488 | return -EINVAL; | |
12489 | ||
a21b0b35 YD |
12490 | if (flags & PERF_FLAG_FD_CLOEXEC) |
12491 | f_flags |= O_CLOEXEC; | |
12492 | ||
12493 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
12494 | if (event_fd < 0) |
12495 | return event_fd; | |
12496 | ||
ac9721f3 | 12497 | if (group_fd != -1) { |
2903ff01 AV |
12498 | err = perf_fget_light(group_fd, &group); |
12499 | if (err) | |
d14b12d7 | 12500 | goto err_fd; |
2903ff01 | 12501 | group_leader = group.file->private_data; |
ac9721f3 PZ |
12502 | if (flags & PERF_FLAG_FD_OUTPUT) |
12503 | output_event = group_leader; | |
12504 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
12505 | group_leader = NULL; | |
12506 | } | |
12507 | ||
e5d1367f | 12508 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
12509 | task = find_lively_task_by_vpid(pid); |
12510 | if (IS_ERR(task)) { | |
12511 | err = PTR_ERR(task); | |
12512 | goto err_group_fd; | |
12513 | } | |
12514 | } | |
12515 | ||
1f4ee503 PZ |
12516 | if (task && group_leader && |
12517 | group_leader->attr.inherit != attr.inherit) { | |
12518 | err = -EINVAL; | |
12519 | goto err_task; | |
12520 | } | |
12521 | ||
79dff51e MF |
12522 | if (flags & PERF_FLAG_PID_CGROUP) |
12523 | cgroup_fd = pid; | |
12524 | ||
4dc0da86 | 12525 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 12526 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
12527 | if (IS_ERR(event)) { |
12528 | err = PTR_ERR(event); | |
78af4dc9 | 12529 | goto err_task; |
d14b12d7 SE |
12530 | } |
12531 | ||
53b25335 VW |
12532 | if (is_sampling_event(event)) { |
12533 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 12534 | err = -EOPNOTSUPP; |
53b25335 VW |
12535 | goto err_alloc; |
12536 | } | |
12537 | } | |
12538 | ||
89a1e187 PZ |
12539 | /* |
12540 | * Special case software events and allow them to be part of | |
12541 | * any hardware group. | |
12542 | */ | |
12543 | pmu = event->pmu; | |
b04243ef | 12544 | |
34f43927 PZ |
12545 | if (attr.use_clockid) { |
12546 | err = perf_event_set_clock(event, attr.clockid); | |
12547 | if (err) | |
12548 | goto err_alloc; | |
12549 | } | |
12550 | ||
4ff6a8de DCC |
12551 | if (pmu->task_ctx_nr == perf_sw_context) |
12552 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
12553 | ||
bd275681 PZ |
12554 | if (task) { |
12555 | err = down_read_interruptible(&task->signal->exec_update_lock); | |
12556 | if (err) | |
12557 | goto err_alloc; | |
12558 | ||
12559 | /* | |
12560 | * We must hold exec_update_lock across this and any potential | |
12561 | * perf_install_in_context() call for this new event to | |
12562 | * serialize against exec() altering our credentials (and the | |
12563 | * perf_event_exit_task() that could imply). | |
12564 | */ | |
12565 | err = -EACCES; | |
12566 | if (!perf_check_permission(&attr, task)) | |
12567 | goto err_cred; | |
b04243ef | 12568 | } |
89a1e187 PZ |
12569 | |
12570 | /* | |
12571 | * Get the target context (task or percpu): | |
12572 | */ | |
bd275681 | 12573 | ctx = find_get_context(task, event); |
89a1e187 PZ |
12574 | if (IS_ERR(ctx)) { |
12575 | err = PTR_ERR(ctx); | |
bd275681 PZ |
12576 | goto err_cred; |
12577 | } | |
12578 | ||
12579 | mutex_lock(&ctx->mutex); | |
12580 | ||
12581 | if (ctx->task == TASK_TOMBSTONE) { | |
12582 | err = -ESRCH; | |
12583 | goto err_locked; | |
12584 | } | |
12585 | ||
12586 | if (!task) { | |
12587 | /* | |
12588 | * Check if the @cpu we're creating an event for is online. | |
12589 | * | |
12590 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
12591 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
12592 | */ | |
12593 | struct perf_cpu_context *cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); | |
12594 | ||
12595 | if (!cpuctx->online) { | |
12596 | err = -ENODEV; | |
12597 | goto err_locked; | |
12598 | } | |
89a1e187 PZ |
12599 | } |
12600 | ||
ac9721f3 | 12601 | if (group_leader) { |
dc86cabe | 12602 | err = -EINVAL; |
04289bb9 | 12603 | |
04289bb9 | 12604 | /* |
ccff286d IM |
12605 | * Do not allow a recursive hierarchy (this new sibling |
12606 | * becoming part of another group-sibling): | |
12607 | */ | |
12608 | if (group_leader->group_leader != group_leader) | |
bd275681 | 12609 | goto err_locked; |
34f43927 PZ |
12610 | |
12611 | /* All events in a group should have the same clock */ | |
12612 | if (group_leader->clock != event->clock) | |
bd275681 | 12613 | goto err_locked; |
34f43927 | 12614 | |
ccff286d | 12615 | /* |
64aee2a9 MR |
12616 | * Make sure we're both events for the same CPU; |
12617 | * grouping events for different CPUs is broken; since | |
12618 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 12619 | */ |
64aee2a9 | 12620 | if (group_leader->cpu != event->cpu) |
bd275681 | 12621 | goto err_locked; |
64aee2a9 MR |
12622 | |
12623 | /* | |
bd275681 | 12624 | * Make sure we're both on the same context; either task or cpu. |
64aee2a9 | 12625 | */ |
bd275681 PZ |
12626 | if (group_leader->ctx != ctx) |
12627 | goto err_locked; | |
b04243ef | 12628 | |
3b6f9e5c PM |
12629 | /* |
12630 | * Only a group leader can be exclusive or pinned | |
12631 | */ | |
0d48696f | 12632 | if (attr.exclusive || attr.pinned) |
84c4e620 | 12633 | goto err_locked; |
321027c1 | 12634 | |
bd275681 PZ |
12635 | if (is_software_event(event) && |
12636 | !in_software_context(group_leader)) { | |
321027c1 | 12637 | /* |
bd275681 PZ |
12638 | * If the event is a sw event, but the group_leader |
12639 | * is on hw context. | |
12640 | * | |
12641 | * Allow the addition of software events to hw | |
12642 | * groups, this is safe because software events | |
12643 | * never fail to schedule. | |
12644 | * | |
12645 | * Note the comment that goes with struct | |
12646 | * perf_event_pmu_context. | |
321027c1 | 12647 | */ |
bd275681 | 12648 | pmu = group_leader->pmu_ctx->pmu; |
bf480f93 RB |
12649 | } else if (!is_software_event(event)) { |
12650 | if (is_software_event(group_leader) && | |
12651 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
12652 | /* | |
12653 | * In case the group is a pure software group, and we | |
12654 | * try to add a hardware event, move the whole group to | |
12655 | * the hardware context. | |
12656 | */ | |
12657 | move_group = 1; | |
321027c1 | 12658 | } |
8a58ddae | 12659 | |
bf480f93 RB |
12660 | /* Don't allow group of multiple hw events from different pmus */ |
12661 | if (!in_software_context(group_leader) && | |
12662 | group_leader->pmu_ctx->pmu != pmu) | |
8a58ddae AS |
12663 | goto err_locked; |
12664 | } | |
f55fc2a5 PZ |
12665 | } |
12666 | ||
bd275681 PZ |
12667 | /* |
12668 | * Now that we're certain of the pmu; find the pmu_ctx. | |
12669 | */ | |
12670 | pmu_ctx = find_get_pmu_context(pmu, ctx, event); | |
12671 | if (IS_ERR(pmu_ctx)) { | |
12672 | err = PTR_ERR(pmu_ctx); | |
84c4e620 PZ |
12673 | goto err_locked; |
12674 | } | |
bd275681 | 12675 | event->pmu_ctx = pmu_ctx; |
84c4e620 | 12676 | |
bd275681 PZ |
12677 | if (output_event) { |
12678 | err = perf_event_set_output(event, output_event); | |
12679 | if (err) | |
12680 | goto err_context; | |
a723968c PZ |
12681 | } |
12682 | ||
bd275681 PZ |
12683 | if (!perf_event_validate_size(event)) { |
12684 | err = -E2BIG; | |
12685 | goto err_context; | |
a63fbed7 TG |
12686 | } |
12687 | ||
da9ec3d3 MR |
12688 | if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) { |
12689 | err = -EINVAL; | |
bd275681 | 12690 | goto err_context; |
da9ec3d3 | 12691 | } |
a63fbed7 | 12692 | |
f55fc2a5 PZ |
12693 | /* |
12694 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
12695 | * because we need to serialize with concurrent event creation. | |
12696 | */ | |
12697 | if (!exclusive_event_installable(event, ctx)) { | |
f55fc2a5 | 12698 | err = -EBUSY; |
bd275681 | 12699 | goto err_context; |
f55fc2a5 | 12700 | } |
f63a8daa | 12701 | |
f55fc2a5 PZ |
12702 | WARN_ON_ONCE(ctx->parent_ctx); |
12703 | ||
bd275681 PZ |
12704 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, f_flags); |
12705 | if (IS_ERR(event_file)) { | |
12706 | err = PTR_ERR(event_file); | |
12707 | event_file = NULL; | |
12708 | goto err_context; | |
12709 | } | |
12710 | ||
79c9ce57 PZ |
12711 | /* |
12712 | * This is the point on no return; we cannot fail hereafter. This is | |
12713 | * where we start modifying current state. | |
12714 | */ | |
12715 | ||
f55fc2a5 | 12716 | if (move_group) { |
45a0e07a | 12717 | perf_remove_from_context(group_leader, 0); |
bd275681 | 12718 | put_pmu_ctx(group_leader->pmu_ctx); |
0231bb53 | 12719 | |
edb39592 | 12720 | for_each_sibling_event(sibling, group_leader) { |
45a0e07a | 12721 | perf_remove_from_context(sibling, 0); |
bd275681 | 12722 | put_pmu_ctx(sibling->pmu_ctx); |
b04243ef | 12723 | } |
b04243ef | 12724 | |
8f95b435 PZI |
12725 | /* |
12726 | * Install the group siblings before the group leader. | |
12727 | * | |
12728 | * Because a group leader will try and install the entire group | |
12729 | * (through the sibling list, which is still in-tact), we can | |
12730 | * end up with siblings installed in the wrong context. | |
12731 | * | |
12732 | * By installing siblings first we NO-OP because they're not | |
12733 | * reachable through the group lists. | |
12734 | */ | |
edb39592 | 12735 | for_each_sibling_event(sibling, group_leader) { |
bd275681 PZ |
12736 | sibling->pmu_ctx = pmu_ctx; |
12737 | get_pmu_ctx(pmu_ctx); | |
8f95b435 | 12738 | perf_event__state_init(sibling); |
9fc81d87 | 12739 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef | 12740 | } |
8f95b435 PZI |
12741 | |
12742 | /* | |
12743 | * Removing from the context ends up with disabled | |
12744 | * event. What we want here is event in the initial | |
12745 | * startup state, ready to be add into new context. | |
12746 | */ | |
bd275681 PZ |
12747 | group_leader->pmu_ctx = pmu_ctx; |
12748 | get_pmu_ctx(pmu_ctx); | |
8f95b435 PZI |
12749 | perf_event__state_init(group_leader); |
12750 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
bed5b25a AS |
12751 | } |
12752 | ||
f73e22ab PZ |
12753 | /* |
12754 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
12755 | * that we're serialized against further additions and before | |
12756 | * perf_install_in_context() which is the point the event is active and | |
12757 | * can use these values. | |
12758 | */ | |
12759 | perf_event__header_size(event); | |
12760 | perf_event__id_header_size(event); | |
12761 | ||
78cd2c74 PZ |
12762 | event->owner = current; |
12763 | ||
e2d37cd2 | 12764 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 12765 | perf_unpin_context(ctx); |
f63a8daa | 12766 | |
d859e29f | 12767 | mutex_unlock(&ctx->mutex); |
9b51f66d | 12768 | |
79c9ce57 | 12769 | if (task) { |
f7cfd871 | 12770 | up_read(&task->signal->exec_update_lock); |
79c9ce57 PZ |
12771 | put_task_struct(task); |
12772 | } | |
12773 | ||
cdd6c482 IM |
12774 | mutex_lock(¤t->perf_event_mutex); |
12775 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
12776 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 12777 | |
8a49542c PZ |
12778 | /* |
12779 | * Drop the reference on the group_event after placing the | |
12780 | * new event on the sibling_list. This ensures destruction | |
12781 | * of the group leader will find the pointer to itself in | |
12782 | * perf_group_detach(). | |
12783 | */ | |
2903ff01 | 12784 | fdput(group); |
ea635c64 AV |
12785 | fd_install(event_fd, event_file); |
12786 | return event_fd; | |
0793a61d | 12787 | |
bd275681 | 12788 | err_context: |
a551844e PZ |
12789 | put_pmu_ctx(event->pmu_ctx); |
12790 | event->pmu_ctx = NULL; /* _free_event() */ | |
f55fc2a5 | 12791 | err_locked: |
f55fc2a5 | 12792 | mutex_unlock(&ctx->mutex); |
bd275681 PZ |
12793 | perf_unpin_context(ctx); |
12794 | put_ctx(ctx); | |
78af4dc9 | 12795 | err_cred: |
12796 | if (task) | |
d01e7f10 | 12797 | up_read(&task->signal->exec_update_lock); |
c6be5a5c | 12798 | err_alloc: |
bd275681 | 12799 | free_event(event); |
1f4ee503 | 12800 | err_task: |
e7d0bc04 PZ |
12801 | if (task) |
12802 | put_task_struct(task); | |
89a1e187 | 12803 | err_group_fd: |
2903ff01 | 12804 | fdput(group); |
ea635c64 AV |
12805 | err_fd: |
12806 | put_unused_fd(event_fd); | |
dc86cabe | 12807 | return err; |
0793a61d TG |
12808 | } |
12809 | ||
fb0459d7 AV |
12810 | /** |
12811 | * perf_event_create_kernel_counter | |
12812 | * | |
12813 | * @attr: attributes of the counter to create | |
12814 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 12815 | * @task: task to profile (NULL for percpu) |
a1ddf524 HX |
12816 | * @overflow_handler: callback to trigger when we hit the event |
12817 | * @context: context data could be used in overflow_handler callback | |
fb0459d7 AV |
12818 | */ |
12819 | struct perf_event * | |
12820 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 12821 | struct task_struct *task, |
4dc0da86 AK |
12822 | perf_overflow_handler_t overflow_handler, |
12823 | void *context) | |
fb0459d7 | 12824 | { |
bd275681 | 12825 | struct perf_event_pmu_context *pmu_ctx; |
fb0459d7 | 12826 | struct perf_event_context *ctx; |
c3f00c70 | 12827 | struct perf_event *event; |
bd275681 | 12828 | struct pmu *pmu; |
fb0459d7 | 12829 | int err; |
d859e29f | 12830 | |
dce5affb AS |
12831 | /* |
12832 | * Grouping is not supported for kernel events, neither is 'AUX', | |
12833 | * make sure the caller's intentions are adjusted. | |
12834 | */ | |
12835 | if (attr->aux_output) | |
12836 | return ERR_PTR(-EINVAL); | |
12837 | ||
4dc0da86 | 12838 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 12839 | overflow_handler, context, -1); |
c3f00c70 PZ |
12840 | if (IS_ERR(event)) { |
12841 | err = PTR_ERR(event); | |
12842 | goto err; | |
12843 | } | |
d859e29f | 12844 | |
f8697762 | 12845 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 12846 | event->owner = TASK_TOMBSTONE; |
bd275681 PZ |
12847 | pmu = event->pmu; |
12848 | ||
12849 | if (pmu->task_ctx_nr == perf_sw_context) | |
12850 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
f8697762 | 12851 | |
f25d8ba9 AS |
12852 | /* |
12853 | * Get the target context (task or percpu): | |
12854 | */ | |
bd275681 | 12855 | ctx = find_get_context(task, event); |
c6567f64 FW |
12856 | if (IS_ERR(ctx)) { |
12857 | err = PTR_ERR(ctx); | |
bd275681 | 12858 | goto err_alloc; |
d859e29f | 12859 | } |
fb0459d7 | 12860 | |
fb0459d7 AV |
12861 | WARN_ON_ONCE(ctx->parent_ctx); |
12862 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
12863 | if (ctx->task == TASK_TOMBSTONE) { |
12864 | err = -ESRCH; | |
12865 | goto err_unlock; | |
12866 | } | |
12867 | ||
bd275681 PZ |
12868 | pmu_ctx = find_get_pmu_context(pmu, ctx, event); |
12869 | if (IS_ERR(pmu_ctx)) { | |
12870 | err = PTR_ERR(pmu_ctx); | |
12871 | goto err_unlock; | |
12872 | } | |
12873 | event->pmu_ctx = pmu_ctx; | |
12874 | ||
a63fbed7 TG |
12875 | if (!task) { |
12876 | /* | |
12877 | * Check if the @cpu we're creating an event for is online. | |
12878 | * | |
12879 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
12880 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
12881 | */ | |
12882 | struct perf_cpu_context *cpuctx = | |
12883 | container_of(ctx, struct perf_cpu_context, ctx); | |
12884 | if (!cpuctx->online) { | |
12885 | err = -ENODEV; | |
bd275681 | 12886 | goto err_pmu_ctx; |
a63fbed7 TG |
12887 | } |
12888 | } | |
12889 | ||
bed5b25a | 12890 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 12891 | err = -EBUSY; |
bd275681 | 12892 | goto err_pmu_ctx; |
bed5b25a AS |
12893 | } |
12894 | ||
4ce54af8 | 12895 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 12896 | perf_unpin_context(ctx); |
fb0459d7 AV |
12897 | mutex_unlock(&ctx->mutex); |
12898 | ||
fb0459d7 AV |
12899 | return event; |
12900 | ||
bd275681 PZ |
12901 | err_pmu_ctx: |
12902 | put_pmu_ctx(pmu_ctx); | |
a551844e | 12903 | event->pmu_ctx = NULL; /* _free_event() */ |
84c4e620 PZ |
12904 | err_unlock: |
12905 | mutex_unlock(&ctx->mutex); | |
12906 | perf_unpin_context(ctx); | |
12907 | put_ctx(ctx); | |
bd275681 | 12908 | err_alloc: |
c3f00c70 PZ |
12909 | free_event(event); |
12910 | err: | |
c6567f64 | 12911 | return ERR_PTR(err); |
9b51f66d | 12912 | } |
fb0459d7 | 12913 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 12914 | |
bd275681 PZ |
12915 | static void __perf_pmu_remove(struct perf_event_context *ctx, |
12916 | int cpu, struct pmu *pmu, | |
12917 | struct perf_event_groups *groups, | |
12918 | struct list_head *events) | |
0cda4c02 | 12919 | { |
bd275681 | 12920 | struct perf_event *event, *sibling; |
0cda4c02 | 12921 | |
bd275681 | 12922 | perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) { |
45a0e07a | 12923 | perf_remove_from_context(event, 0); |
bd275681 PZ |
12924 | put_pmu_ctx(event->pmu_ctx); |
12925 | list_add(&event->migrate_entry, events); | |
12926 | ||
12927 | for_each_sibling_event(sibling, event) { | |
12928 | perf_remove_from_context(sibling, 0); | |
bd275681 PZ |
12929 | put_pmu_ctx(sibling->pmu_ctx); |
12930 | list_add(&sibling->migrate_entry, events); | |
12931 | } | |
0cda4c02 | 12932 | } |
bd275681 | 12933 | } |
0cda4c02 | 12934 | |
bd275681 PZ |
12935 | static void __perf_pmu_install_event(struct pmu *pmu, |
12936 | struct perf_event_context *ctx, | |
12937 | int cpu, struct perf_event *event) | |
12938 | { | |
12939 | struct perf_event_pmu_context *epc; | |
889c58b3 PZ |
12940 | struct perf_event_context *old_ctx = event->ctx; |
12941 | ||
12942 | get_ctx(ctx); /* normally find_get_context() */ | |
bd275681 PZ |
12943 | |
12944 | event->cpu = cpu; | |
12945 | epc = find_get_pmu_context(pmu, ctx, event); | |
12946 | event->pmu_ctx = epc; | |
12947 | ||
12948 | if (event->state >= PERF_EVENT_STATE_OFF) | |
12949 | event->state = PERF_EVENT_STATE_INACTIVE; | |
bd275681 | 12950 | perf_install_in_context(ctx, event, cpu); |
889c58b3 PZ |
12951 | |
12952 | /* | |
12953 | * Now that event->ctx is updated and visible, put the old ctx. | |
12954 | */ | |
12955 | put_ctx(old_ctx); | |
bd275681 PZ |
12956 | } |
12957 | ||
12958 | static void __perf_pmu_install(struct perf_event_context *ctx, | |
12959 | int cpu, struct pmu *pmu, struct list_head *events) | |
12960 | { | |
12961 | struct perf_event *event, *tmp; | |
0cda4c02 | 12962 | |
8f95b435 PZI |
12963 | /* |
12964 | * Re-instate events in 2 passes. | |
12965 | * | |
12966 | * Skip over group leaders and only install siblings on this first | |
12967 | * pass, siblings will not get enabled without a leader, however a | |
12968 | * leader will enable its siblings, even if those are still on the old | |
12969 | * context. | |
12970 | */ | |
bd275681 | 12971 | list_for_each_entry_safe(event, tmp, events, migrate_entry) { |
8f95b435 PZI |
12972 | if (event->group_leader == event) |
12973 | continue; | |
12974 | ||
12975 | list_del(&event->migrate_entry); | |
bd275681 | 12976 | __perf_pmu_install_event(pmu, ctx, cpu, event); |
8f95b435 PZI |
12977 | } |
12978 | ||
12979 | /* | |
12980 | * Once all the siblings are setup properly, install the group leaders | |
12981 | * to make it go. | |
12982 | */ | |
bd275681 | 12983 | list_for_each_entry_safe(event, tmp, events, migrate_entry) { |
9886167d | 12984 | list_del(&event->migrate_entry); |
bd275681 | 12985 | __perf_pmu_install_event(pmu, ctx, cpu, event); |
0cda4c02 | 12986 | } |
bd275681 PZ |
12987 | } |
12988 | ||
12989 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) | |
12990 | { | |
12991 | struct perf_event_context *src_ctx, *dst_ctx; | |
12992 | LIST_HEAD(events); | |
12993 | ||
889c58b3 PZ |
12994 | /* |
12995 | * Since per-cpu context is persistent, no need to grab an extra | |
12996 | * reference. | |
12997 | */ | |
bd275681 PZ |
12998 | src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx; |
12999 | dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx; | |
13000 | ||
13001 | /* | |
13002 | * See perf_event_ctx_lock() for comments on the details | |
13003 | * of swizzling perf_event::ctx. | |
13004 | */ | |
13005 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
13006 | ||
13007 | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events); | |
13008 | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events); | |
13009 | ||
b1680989 PZ |
13010 | if (!list_empty(&events)) { |
13011 | /* | |
13012 | * Wait for the events to quiesce before re-instating them. | |
13013 | */ | |
13014 | synchronize_rcu(); | |
bd275681 | 13015 | |
b1680989 PZ |
13016 | __perf_pmu_install(dst_ctx, dst_cpu, pmu, &events); |
13017 | } | |
bd275681 | 13018 | |
0cda4c02 | 13019 | mutex_unlock(&dst_ctx->mutex); |
f63a8daa | 13020 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
13021 | } |
13022 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
13023 | ||
ef54c1a4 | 13024 | static void sync_child_event(struct perf_event *child_event) |
d859e29f | 13025 | { |
cdd6c482 | 13026 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 13027 | u64 child_val; |
d859e29f | 13028 | |
ef54c1a4 PZ |
13029 | if (child_event->attr.inherit_stat) { |
13030 | struct task_struct *task = child_event->ctx->task; | |
13031 | ||
13032 | if (task && task != TASK_TOMBSTONE) | |
13033 | perf_event_read_event(child_event, task); | |
13034 | } | |
38b200d6 | 13035 | |
b5e58793 | 13036 | child_val = perf_event_count(child_event); |
d859e29f PM |
13037 | |
13038 | /* | |
13039 | * Add back the child's count to the parent's count: | |
13040 | */ | |
a6e6dea6 | 13041 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
13042 | atomic64_add(child_event->total_time_enabled, |
13043 | &parent_event->child_total_time_enabled); | |
13044 | atomic64_add(child_event->total_time_running, | |
13045 | &parent_event->child_total_time_running); | |
d859e29f PM |
13046 | } |
13047 | ||
9b51f66d | 13048 | static void |
ef54c1a4 | 13049 | perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx) |
9b51f66d | 13050 | { |
ef54c1a4 PZ |
13051 | struct perf_event *parent_event = event->parent; |
13052 | unsigned long detach_flags = 0; | |
8ba289b8 | 13053 | |
ef54c1a4 PZ |
13054 | if (parent_event) { |
13055 | /* | |
13056 | * Do not destroy the 'original' grouping; because of the | |
13057 | * context switch optimization the original events could've | |
13058 | * ended up in a random child task. | |
13059 | * | |
13060 | * If we were to destroy the original group, all group related | |
13061 | * operations would cease to function properly after this | |
13062 | * random child dies. | |
13063 | * | |
13064 | * Do destroy all inherited groups, we don't care about those | |
13065 | * and being thorough is better. | |
13066 | */ | |
13067 | detach_flags = DETACH_GROUP | DETACH_CHILD; | |
13068 | mutex_lock(&parent_event->child_mutex); | |
13069 | } | |
32132a3d | 13070 | |
ef54c1a4 PZ |
13071 | perf_remove_from_context(event, detach_flags); |
13072 | ||
13073 | raw_spin_lock_irq(&ctx->lock); | |
13074 | if (event->state > PERF_EVENT_STATE_EXIT) | |
13075 | perf_event_set_state(event, PERF_EVENT_STATE_EXIT); | |
13076 | raw_spin_unlock_irq(&ctx->lock); | |
0cc0c027 | 13077 | |
9b51f66d | 13078 | /* |
ef54c1a4 | 13079 | * Child events can be freed. |
9b51f66d | 13080 | */ |
ef54c1a4 PZ |
13081 | if (parent_event) { |
13082 | mutex_unlock(&parent_event->child_mutex); | |
13083 | /* | |
13084 | * Kick perf_poll() for is_event_hup(); | |
13085 | */ | |
13086 | perf_event_wakeup(parent_event); | |
13087 | free_event(event); | |
13088 | put_event(parent_event); | |
8ba289b8 | 13089 | return; |
4bcf349a | 13090 | } |
8ba289b8 PZ |
13091 | |
13092 | /* | |
ef54c1a4 | 13093 | * Parent events are governed by their filedesc, retain them. |
8ba289b8 | 13094 | */ |
ef54c1a4 | 13095 | perf_event_wakeup(event); |
9b51f66d IM |
13096 | } |
13097 | ||
bd275681 | 13098 | static void perf_event_exit_task_context(struct task_struct *child) |
9b51f66d | 13099 | { |
211de6eb | 13100 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 13101 | struct perf_event *child_event, *next; |
63b6da39 PZ |
13102 | |
13103 | WARN_ON_ONCE(child != current); | |
9b51f66d | 13104 | |
bd275681 | 13105 | child_ctx = perf_pin_task_context(child); |
63b6da39 | 13106 | if (!child_ctx) |
9b51f66d IM |
13107 | return; |
13108 | ||
ad3a37de | 13109 | /* |
6a3351b6 PZ |
13110 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
13111 | * ctx::mutex over the entire thing. This serializes against almost | |
13112 | * everything that wants to access the ctx. | |
13113 | * | |
13114 | * The exception is sys_perf_event_open() / | |
13115 | * perf_event_create_kernel_count() which does find_get_context() | |
13116 | * without ctx::mutex (it cannot because of the move_group double mutex | |
13117 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 13118 | */ |
6a3351b6 | 13119 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
13120 | |
13121 | /* | |
6a3351b6 PZ |
13122 | * In a single ctx::lock section, de-schedule the events and detach the |
13123 | * context from the task such that we cannot ever get it scheduled back | |
13124 | * in. | |
c93f7669 | 13125 | */ |
6a3351b6 | 13126 | raw_spin_lock_irq(&child_ctx->lock); |
bd275681 | 13127 | task_ctx_sched_out(child_ctx, EVENT_ALL); |
4a1c0f26 | 13128 | |
71a851b4 | 13129 | /* |
63b6da39 PZ |
13130 | * Now that the context is inactive, destroy the task <-> ctx relation |
13131 | * and mark the context dead. | |
71a851b4 | 13132 | */ |
bd275681 | 13133 | RCU_INIT_POINTER(child->perf_event_ctxp, NULL); |
63b6da39 PZ |
13134 | put_ctx(child_ctx); /* cannot be last */ |
13135 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
13136 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 13137 | |
211de6eb | 13138 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 13139 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 13140 | |
211de6eb PZ |
13141 | if (clone_ctx) |
13142 | put_ctx(clone_ctx); | |
4a1c0f26 | 13143 | |
9f498cc5 | 13144 | /* |
cdd6c482 IM |
13145 | * Report the task dead after unscheduling the events so that we |
13146 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
13147 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 13148 | */ |
cdd6c482 | 13149 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 13150 | |
ebf905fc | 13151 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
ef54c1a4 | 13152 | perf_event_exit_event(child_event, child_ctx); |
8bc20959 | 13153 | |
a63eaf34 PM |
13154 | mutex_unlock(&child_ctx->mutex); |
13155 | ||
13156 | put_ctx(child_ctx); | |
9b51f66d IM |
13157 | } |
13158 | ||
8dc85d54 PZ |
13159 | /* |
13160 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 | 13161 | * |
f7cfd871 | 13162 | * Can be called with exec_update_lock held when called from |
96ecee29 | 13163 | * setup_new_exec(). |
8dc85d54 PZ |
13164 | */ |
13165 | void perf_event_exit_task(struct task_struct *child) | |
13166 | { | |
8882135b | 13167 | struct perf_event *event, *tmp; |
8dc85d54 | 13168 | |
8882135b PZ |
13169 | mutex_lock(&child->perf_event_mutex); |
13170 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
13171 | owner_entry) { | |
13172 | list_del_init(&event->owner_entry); | |
13173 | ||
13174 | /* | |
13175 | * Ensure the list deletion is visible before we clear | |
13176 | * the owner, closes a race against perf_release() where | |
13177 | * we need to serialize on the owner->perf_event_mutex. | |
13178 | */ | |
f47c02c0 | 13179 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
13180 | } |
13181 | mutex_unlock(&child->perf_event_mutex); | |
13182 | ||
bd275681 | 13183 | perf_event_exit_task_context(child); |
4e93ad60 JO |
13184 | |
13185 | /* | |
13186 | * The perf_event_exit_task_context calls perf_event_task | |
13187 | * with child's task_ctx, which generates EXIT events for | |
13188 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
13189 | * At this point we need to send EXIT events to cpu contexts. | |
13190 | */ | |
13191 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
13192 | } |
13193 | ||
889ff015 FW |
13194 | static void perf_free_event(struct perf_event *event, |
13195 | struct perf_event_context *ctx) | |
13196 | { | |
13197 | struct perf_event *parent = event->parent; | |
13198 | ||
13199 | if (WARN_ON_ONCE(!parent)) | |
13200 | return; | |
13201 | ||
13202 | mutex_lock(&parent->child_mutex); | |
13203 | list_del_init(&event->child_list); | |
13204 | mutex_unlock(&parent->child_mutex); | |
13205 | ||
a6fa941d | 13206 | put_event(parent); |
889ff015 | 13207 | |
652884fe | 13208 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 13209 | perf_group_detach(event); |
889ff015 | 13210 | list_del_event(event, ctx); |
652884fe | 13211 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
13212 | free_event(event); |
13213 | } | |
13214 | ||
bbbee908 | 13215 | /* |
1cf8dfe8 PZ |
13216 | * Free a context as created by inheritance by perf_event_init_task() below, |
13217 | * used by fork() in case of fail. | |
652884fe | 13218 | * |
1cf8dfe8 PZ |
13219 | * Even though the task has never lived, the context and events have been |
13220 | * exposed through the child_list, so we must take care tearing it all down. | |
bbbee908 | 13221 | */ |
cdd6c482 | 13222 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 13223 | { |
8dc85d54 | 13224 | struct perf_event_context *ctx; |
cdd6c482 | 13225 | struct perf_event *event, *tmp; |
bbbee908 | 13226 | |
bd275681 PZ |
13227 | ctx = rcu_access_pointer(task->perf_event_ctxp); |
13228 | if (!ctx) | |
13229 | return; | |
bbbee908 | 13230 | |
bd275681 PZ |
13231 | mutex_lock(&ctx->mutex); |
13232 | raw_spin_lock_irq(&ctx->lock); | |
13233 | /* | |
13234 | * Destroy the task <-> ctx relation and mark the context dead. | |
13235 | * | |
13236 | * This is important because even though the task hasn't been | |
13237 | * exposed yet the context has been (through child_list). | |
13238 | */ | |
13239 | RCU_INIT_POINTER(task->perf_event_ctxp, NULL); | |
13240 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
13241 | put_task_struct(task); /* cannot be last */ | |
13242 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 13243 | |
bbbee908 | 13244 | |
bd275681 PZ |
13245 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
13246 | perf_free_event(event, ctx); | |
1cf8dfe8 | 13247 | |
bd275681 PZ |
13248 | mutex_unlock(&ctx->mutex); |
13249 | ||
13250 | /* | |
13251 | * perf_event_release_kernel() could've stolen some of our | |
13252 | * child events and still have them on its free_list. In that | |
13253 | * case we must wait for these events to have been freed (in | |
13254 | * particular all their references to this task must've been | |
13255 | * dropped). | |
13256 | * | |
13257 | * Without this copy_process() will unconditionally free this | |
13258 | * task (irrespective of its reference count) and | |
13259 | * _free_event()'s put_task_struct(event->hw.target) will be a | |
13260 | * use-after-free. | |
13261 | * | |
13262 | * Wait for all events to drop their context reference. | |
13263 | */ | |
13264 | wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1); | |
13265 | put_ctx(ctx); /* must be last */ | |
889ff015 FW |
13266 | } |
13267 | ||
4e231c79 PZ |
13268 | void perf_event_delayed_put(struct task_struct *task) |
13269 | { | |
bd275681 | 13270 | WARN_ON_ONCE(task->perf_event_ctxp); |
4e231c79 PZ |
13271 | } |
13272 | ||
e03e7ee3 | 13273 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 13274 | { |
02e5ad97 | 13275 | struct file *file = fget(fd); |
e03e7ee3 AS |
13276 | if (!file) |
13277 | return ERR_PTR(-EBADF); | |
ffe8690c | 13278 | |
e03e7ee3 AS |
13279 | if (file->f_op != &perf_fops) { |
13280 | fput(file); | |
13281 | return ERR_PTR(-EBADF); | |
13282 | } | |
ffe8690c | 13283 | |
e03e7ee3 | 13284 | return file; |
ffe8690c KX |
13285 | } |
13286 | ||
f8d959a5 YS |
13287 | const struct perf_event *perf_get_event(struct file *file) |
13288 | { | |
13289 | if (file->f_op != &perf_fops) | |
13290 | return ERR_PTR(-EINVAL); | |
13291 | ||
13292 | return file->private_data; | |
13293 | } | |
13294 | ||
ffe8690c KX |
13295 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
13296 | { | |
13297 | if (!event) | |
13298 | return ERR_PTR(-EINVAL); | |
13299 | ||
13300 | return &event->attr; | |
13301 | } | |
13302 | ||
97dee4f3 | 13303 | /* |
788faab7 | 13304 | * Inherit an event from parent task to child task. |
d8a8cfc7 PZ |
13305 | * |
13306 | * Returns: | |
13307 | * - valid pointer on success | |
13308 | * - NULL for orphaned events | |
13309 | * - IS_ERR() on error | |
97dee4f3 PZ |
13310 | */ |
13311 | static struct perf_event * | |
13312 | inherit_event(struct perf_event *parent_event, | |
13313 | struct task_struct *parent, | |
13314 | struct perf_event_context *parent_ctx, | |
13315 | struct task_struct *child, | |
13316 | struct perf_event *group_leader, | |
13317 | struct perf_event_context *child_ctx) | |
13318 | { | |
8ca2bd41 | 13319 | enum perf_event_state parent_state = parent_event->state; |
bd275681 | 13320 | struct perf_event_pmu_context *pmu_ctx; |
97dee4f3 | 13321 | struct perf_event *child_event; |
cee010ec | 13322 | unsigned long flags; |
97dee4f3 PZ |
13323 | |
13324 | /* | |
13325 | * Instead of creating recursive hierarchies of events, | |
13326 | * we link inherited events back to the original parent, | |
13327 | * which has a filp for sure, which we use as the reference | |
13328 | * count: | |
13329 | */ | |
13330 | if (parent_event->parent) | |
13331 | parent_event = parent_event->parent; | |
13332 | ||
13333 | child_event = perf_event_alloc(&parent_event->attr, | |
13334 | parent_event->cpu, | |
d580ff86 | 13335 | child, |
97dee4f3 | 13336 | group_leader, parent_event, |
79dff51e | 13337 | NULL, NULL, -1); |
97dee4f3 PZ |
13338 | if (IS_ERR(child_event)) |
13339 | return child_event; | |
a6fa941d | 13340 | |
bd275681 | 13341 | pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event); |
c55bfbb3 | 13342 | if (IS_ERR(pmu_ctx)) { |
bd275681 | 13343 | free_event(child_event); |
e2d37148 | 13344 | return ERR_CAST(pmu_ctx); |
313ccb96 | 13345 | } |
bd275681 | 13346 | child_event->pmu_ctx = pmu_ctx; |
313ccb96 | 13347 | |
c6e5b732 PZ |
13348 | /* |
13349 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
13350 | * must be under the same lock in order to serialize against | |
13351 | * perf_event_release_kernel(), such that either we must observe | |
13352 | * is_orphaned_event() or they will observe us on the child_list. | |
13353 | */ | |
13354 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
13355 | if (is_orphaned_event(parent_event) || |
13356 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 13357 | mutex_unlock(&parent_event->child_mutex); |
313ccb96 | 13358 | /* task_ctx_data is freed with child_ctx */ |
a6fa941d AV |
13359 | free_event(child_event); |
13360 | return NULL; | |
13361 | } | |
13362 | ||
97dee4f3 PZ |
13363 | get_ctx(child_ctx); |
13364 | ||
13365 | /* | |
13366 | * Make the child state follow the state of the parent event, | |
13367 | * not its attr.disabled bit. We hold the parent's mutex, | |
13368 | * so we won't race with perf_event_{en, dis}able_family. | |
13369 | */ | |
1929def9 | 13370 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
13371 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
13372 | else | |
13373 | child_event->state = PERF_EVENT_STATE_OFF; | |
13374 | ||
13375 | if (parent_event->attr.freq) { | |
13376 | u64 sample_period = parent_event->hw.sample_period; | |
13377 | struct hw_perf_event *hwc = &child_event->hw; | |
13378 | ||
13379 | hwc->sample_period = sample_period; | |
13380 | hwc->last_period = sample_period; | |
13381 | ||
13382 | local64_set(&hwc->period_left, sample_period); | |
13383 | } | |
13384 | ||
13385 | child_event->ctx = child_ctx; | |
13386 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
13387 | child_event->overflow_handler_context |
13388 | = parent_event->overflow_handler_context; | |
97dee4f3 | 13389 | |
614b6780 TG |
13390 | /* |
13391 | * Precalculate sample_data sizes | |
13392 | */ | |
13393 | perf_event__header_size(child_event); | |
6844c09d | 13394 | perf_event__id_header_size(child_event); |
614b6780 | 13395 | |
97dee4f3 PZ |
13396 | /* |
13397 | * Link it up in the child's context: | |
13398 | */ | |
cee010ec | 13399 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 13400 | add_event_to_ctx(child_event, child_ctx); |
ef54c1a4 | 13401 | child_event->attach_state |= PERF_ATTACH_CHILD; |
cee010ec | 13402 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 13403 | |
97dee4f3 PZ |
13404 | /* |
13405 | * Link this into the parent event's child list | |
13406 | */ | |
97dee4f3 PZ |
13407 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
13408 | mutex_unlock(&parent_event->child_mutex); | |
13409 | ||
13410 | return child_event; | |
13411 | } | |
13412 | ||
d8a8cfc7 PZ |
13413 | /* |
13414 | * Inherits an event group. | |
13415 | * | |
13416 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
13417 | * This matches with perf_event_release_kernel() removing all child events. | |
13418 | * | |
13419 | * Returns: | |
13420 | * - 0 on success | |
13421 | * - <0 on error | |
13422 | */ | |
97dee4f3 PZ |
13423 | static int inherit_group(struct perf_event *parent_event, |
13424 | struct task_struct *parent, | |
13425 | struct perf_event_context *parent_ctx, | |
13426 | struct task_struct *child, | |
13427 | struct perf_event_context *child_ctx) | |
13428 | { | |
13429 | struct perf_event *leader; | |
13430 | struct perf_event *sub; | |
13431 | struct perf_event *child_ctr; | |
13432 | ||
13433 | leader = inherit_event(parent_event, parent, parent_ctx, | |
13434 | child, NULL, child_ctx); | |
13435 | if (IS_ERR(leader)) | |
13436 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
13437 | /* |
13438 | * @leader can be NULL here because of is_orphaned_event(). In this | |
13439 | * case inherit_event() will create individual events, similar to what | |
13440 | * perf_group_detach() would do anyway. | |
13441 | */ | |
edb39592 | 13442 | for_each_sibling_event(sub, parent_event) { |
97dee4f3 PZ |
13443 | child_ctr = inherit_event(sub, parent, parent_ctx, |
13444 | child, leader, child_ctx); | |
13445 | if (IS_ERR(child_ctr)) | |
13446 | return PTR_ERR(child_ctr); | |
f733c6b5 | 13447 | |
00496fe5 | 13448 | if (sub->aux_event == parent_event && child_ctr && |
f733c6b5 AS |
13449 | !perf_get_aux_event(child_ctr, leader)) |
13450 | return -EINVAL; | |
97dee4f3 | 13451 | } |
a71ef314 PZ |
13452 | if (leader) |
13453 | leader->group_generation = parent_event->group_generation; | |
97dee4f3 | 13454 | return 0; |
889ff015 FW |
13455 | } |
13456 | ||
d8a8cfc7 PZ |
13457 | /* |
13458 | * Creates the child task context and tries to inherit the event-group. | |
13459 | * | |
13460 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
13461 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
13462 | * consistent with perf_event_release_kernel() removing all child events. | |
13463 | * | |
13464 | * Returns: | |
13465 | * - 0 on success | |
13466 | * - <0 on error | |
13467 | */ | |
889ff015 FW |
13468 | static int |
13469 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
13470 | struct perf_event_context *parent_ctx, | |
bd275681 | 13471 | struct task_struct *child, |
2b26f0aa | 13472 | u64 clone_flags, int *inherited_all) |
889ff015 | 13473 | { |
8dc85d54 | 13474 | struct perf_event_context *child_ctx; |
bd275681 | 13475 | int ret; |
889ff015 | 13476 | |
2b26f0aa | 13477 | if (!event->attr.inherit || |
97ba62b2 ME |
13478 | (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) || |
13479 | /* Do not inherit if sigtrap and signal handlers were cleared. */ | |
13480 | (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) { | |
889ff015 FW |
13481 | *inherited_all = 0; |
13482 | return 0; | |
bbbee908 PZ |
13483 | } |
13484 | ||
bd275681 | 13485 | child_ctx = child->perf_event_ctxp; |
889ff015 FW |
13486 | if (!child_ctx) { |
13487 | /* | |
13488 | * This is executed from the parent task context, so | |
13489 | * inherit events that have been marked for cloning. | |
13490 | * First allocate and initialize a context for the | |
13491 | * child. | |
13492 | */ | |
bd275681 | 13493 | child_ctx = alloc_perf_context(child); |
889ff015 FW |
13494 | if (!child_ctx) |
13495 | return -ENOMEM; | |
bbbee908 | 13496 | |
bd275681 | 13497 | child->perf_event_ctxp = child_ctx; |
889ff015 FW |
13498 | } |
13499 | ||
bd275681 | 13500 | ret = inherit_group(event, parent, parent_ctx, child, child_ctx); |
889ff015 FW |
13501 | if (ret) |
13502 | *inherited_all = 0; | |
13503 | ||
13504 | return ret; | |
bbbee908 PZ |
13505 | } |
13506 | ||
9b51f66d | 13507 | /* |
cdd6c482 | 13508 | * Initialize the perf_event context in task_struct |
9b51f66d | 13509 | */ |
bd275681 | 13510 | static int perf_event_init_context(struct task_struct *child, u64 clone_flags) |
9b51f66d | 13511 | { |
889ff015 | 13512 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
13513 | struct perf_event_context *cloned_ctx; |
13514 | struct perf_event *event; | |
9b51f66d | 13515 | struct task_struct *parent = current; |
564c2b21 | 13516 | int inherited_all = 1; |
dddd3379 | 13517 | unsigned long flags; |
6ab423e0 | 13518 | int ret = 0; |
9b51f66d | 13519 | |
bd275681 | 13520 | if (likely(!parent->perf_event_ctxp)) |
6ab423e0 PZ |
13521 | return 0; |
13522 | ||
ad3a37de | 13523 | /* |
25346b93 PM |
13524 | * If the parent's context is a clone, pin it so it won't get |
13525 | * swapped under us. | |
ad3a37de | 13526 | */ |
bd275681 | 13527 | parent_ctx = perf_pin_task_context(parent); |
ffb4ef21 PZ |
13528 | if (!parent_ctx) |
13529 | return 0; | |
25346b93 | 13530 | |
ad3a37de PM |
13531 | /* |
13532 | * No need to check if parent_ctx != NULL here; since we saw | |
13533 | * it non-NULL earlier, the only reason for it to become NULL | |
13534 | * is if we exit, and since we're currently in the middle of | |
13535 | * a fork we can't be exiting at the same time. | |
13536 | */ | |
ad3a37de | 13537 | |
9b51f66d IM |
13538 | /* |
13539 | * Lock the parent list. No need to lock the child - not PID | |
13540 | * hashed yet and not running, so nobody can access it. | |
13541 | */ | |
d859e29f | 13542 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
13543 | |
13544 | /* | |
13545 | * We dont have to disable NMIs - we are only looking at | |
13546 | * the list, not manipulating it: | |
13547 | */ | |
6e6804d2 | 13548 | perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { |
8dc85d54 | 13549 | ret = inherit_task_group(event, parent, parent_ctx, |
bd275681 | 13550 | child, clone_flags, &inherited_all); |
889ff015 | 13551 | if (ret) |
e7cc4865 | 13552 | goto out_unlock; |
889ff015 | 13553 | } |
b93f7978 | 13554 | |
dddd3379 TG |
13555 | /* |
13556 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
13557 | * to allocations, but we need to prevent rotation because | |
13558 | * rotate_ctx() will change the list from interrupt context. | |
13559 | */ | |
13560 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
13561 | parent_ctx->rotate_disable = 1; | |
13562 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
13563 | ||
6e6804d2 | 13564 | perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { |
8dc85d54 | 13565 | ret = inherit_task_group(event, parent, parent_ctx, |
bd275681 | 13566 | child, clone_flags, &inherited_all); |
889ff015 | 13567 | if (ret) |
e7cc4865 | 13568 | goto out_unlock; |
564c2b21 PM |
13569 | } |
13570 | ||
dddd3379 TG |
13571 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
13572 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 13573 | |
bd275681 | 13574 | child_ctx = child->perf_event_ctxp; |
889ff015 | 13575 | |
05cbaa28 | 13576 | if (child_ctx && inherited_all) { |
564c2b21 PM |
13577 | /* |
13578 | * Mark the child context as a clone of the parent | |
13579 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
13580 | * |
13581 | * Note that if the parent is a clone, the holding of | |
13582 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 13583 | */ |
c5ed5145 | 13584 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
13585 | if (cloned_ctx) { |
13586 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 13587 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
13588 | } else { |
13589 | child_ctx->parent_ctx = parent_ctx; | |
13590 | child_ctx->parent_gen = parent_ctx->generation; | |
13591 | } | |
13592 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
13593 | } |
13594 | ||
c5ed5145 | 13595 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 13596 | out_unlock: |
d859e29f | 13597 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 13598 | |
25346b93 | 13599 | perf_unpin_context(parent_ctx); |
fe4b04fa | 13600 | put_ctx(parent_ctx); |
ad3a37de | 13601 | |
6ab423e0 | 13602 | return ret; |
9b51f66d IM |
13603 | } |
13604 | ||
8dc85d54 PZ |
13605 | /* |
13606 | * Initialize the perf_event context in task_struct | |
13607 | */ | |
2b26f0aa | 13608 | int perf_event_init_task(struct task_struct *child, u64 clone_flags) |
8dc85d54 | 13609 | { |
bd275681 | 13610 | int ret; |
8dc85d54 | 13611 | |
bd275681 | 13612 | child->perf_event_ctxp = NULL; |
8550d7cb ON |
13613 | mutex_init(&child->perf_event_mutex); |
13614 | INIT_LIST_HEAD(&child->perf_event_list); | |
13615 | ||
bd275681 PZ |
13616 | ret = perf_event_init_context(child, clone_flags); |
13617 | if (ret) { | |
13618 | perf_event_free_task(child); | |
13619 | return ret; | |
8dc85d54 PZ |
13620 | } |
13621 | ||
13622 | return 0; | |
13623 | } | |
13624 | ||
220b140b PM |
13625 | static void __init perf_event_init_all_cpus(void) |
13626 | { | |
b28ab83c | 13627 | struct swevent_htable *swhash; |
bd275681 | 13628 | struct perf_cpu_context *cpuctx; |
220b140b | 13629 | int cpu; |
220b140b | 13630 | |
a63fbed7 TG |
13631 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
13632 | ||
220b140b | 13633 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
13634 | swhash = &per_cpu(swevent_htable, cpu); |
13635 | mutex_init(&swhash->hlist_mutex); | |
f2fb6bef KL |
13636 | |
13637 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
13638 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 13639 | |
a5398bff | 13640 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
bd275681 PZ |
13641 | |
13642 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); | |
13643 | __perf_event_init_context(&cpuctx->ctx); | |
13644 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); | |
13645 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); | |
13646 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); | |
13647 | cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default); | |
13648 | cpuctx->heap = cpuctx->heap_default; | |
220b140b PM |
13649 | } |
13650 | } | |
13651 | ||
d18bf422 | 13652 | static void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 13653 | { |
108b02cf | 13654 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 13655 | |
b28ab83c | 13656 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 13657 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
13658 | struct swevent_hlist *hlist; |
13659 | ||
b28ab83c PZ |
13660 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
13661 | WARN_ON(!hlist); | |
13662 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 13663 | } |
b28ab83c | 13664 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
13665 | } |
13666 | ||
2965faa5 | 13667 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 13668 | static void __perf_event_exit_context(void *__info) |
0793a61d | 13669 | { |
bd275681 | 13670 | struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); |
108b02cf | 13671 | struct perf_event_context *ctx = __info; |
fae3fde6 | 13672 | struct perf_event *event; |
0793a61d | 13673 | |
fae3fde6 | 13674 | raw_spin_lock(&ctx->lock); |
bd275681 | 13675 | ctx_sched_out(ctx, EVENT_TIME); |
fae3fde6 | 13676 | list_for_each_entry(event, &ctx->event_list, event_entry) |
45a0e07a | 13677 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 13678 | raw_spin_unlock(&ctx->lock); |
0793a61d | 13679 | } |
108b02cf PZ |
13680 | |
13681 | static void perf_event_exit_cpu_context(int cpu) | |
13682 | { | |
a63fbed7 | 13683 | struct perf_cpu_context *cpuctx; |
108b02cf | 13684 | struct perf_event_context *ctx; |
108b02cf | 13685 | |
bd275681 | 13686 | // XXX simplify cpuctx->online |
a63fbed7 | 13687 | mutex_lock(&pmus_lock); |
bd275681 PZ |
13688 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); |
13689 | ctx = &cpuctx->ctx; | |
108b02cf | 13690 | |
bd275681 PZ |
13691 | mutex_lock(&ctx->mutex); |
13692 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
13693 | cpuctx->online = 0; | |
13694 | mutex_unlock(&ctx->mutex); | |
a63fbed7 TG |
13695 | cpumask_clear_cpu(cpu, perf_online_mask); |
13696 | mutex_unlock(&pmus_lock); | |
108b02cf | 13697 | } |
00e16c3d TG |
13698 | #else |
13699 | ||
13700 | static void perf_event_exit_cpu_context(int cpu) { } | |
13701 | ||
13702 | #endif | |
108b02cf | 13703 | |
a63fbed7 TG |
13704 | int perf_event_init_cpu(unsigned int cpu) |
13705 | { | |
13706 | struct perf_cpu_context *cpuctx; | |
13707 | struct perf_event_context *ctx; | |
a63fbed7 TG |
13708 | |
13709 | perf_swevent_init_cpu(cpu); | |
13710 | ||
13711 | mutex_lock(&pmus_lock); | |
13712 | cpumask_set_cpu(cpu, perf_online_mask); | |
bd275681 PZ |
13713 | cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); |
13714 | ctx = &cpuctx->ctx; | |
a63fbed7 | 13715 | |
bd275681 PZ |
13716 | mutex_lock(&ctx->mutex); |
13717 | cpuctx->online = 1; | |
13718 | mutex_unlock(&ctx->mutex); | |
a63fbed7 TG |
13719 | mutex_unlock(&pmus_lock); |
13720 | ||
13721 | return 0; | |
13722 | } | |
13723 | ||
00e16c3d | 13724 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 13725 | { |
e3703f8c | 13726 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 13727 | return 0; |
0793a61d | 13728 | } |
0793a61d | 13729 | |
c277443c PZ |
13730 | static int |
13731 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
13732 | { | |
13733 | int cpu; | |
13734 | ||
13735 | for_each_online_cpu(cpu) | |
13736 | perf_event_exit_cpu(cpu); | |
13737 | ||
13738 | return NOTIFY_OK; | |
13739 | } | |
13740 | ||
13741 | /* | |
13742 | * Run the perf reboot notifier at the very last possible moment so that | |
13743 | * the generic watchdog code runs as long as possible. | |
13744 | */ | |
13745 | static struct notifier_block perf_reboot_notifier = { | |
13746 | .notifier_call = perf_reboot, | |
13747 | .priority = INT_MIN, | |
13748 | }; | |
13749 | ||
cdd6c482 | 13750 | void __init perf_event_init(void) |
0793a61d | 13751 | { |
3c502e7a JW |
13752 | int ret; |
13753 | ||
2e80a82a PZ |
13754 | idr_init(&pmu_idr); |
13755 | ||
220b140b | 13756 | perf_event_init_all_cpus(); |
b0a873eb | 13757 | init_srcu_struct(&pmus_srcu); |
2e80a82a | 13758 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
0d6d062c RB |
13759 | perf_pmu_register(&perf_cpu_clock, "cpu_clock", -1); |
13760 | perf_pmu_register(&perf_task_clock, "task_clock", -1); | |
b0a873eb | 13761 | perf_tp_register(); |
00e16c3d | 13762 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 13763 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
13764 | |
13765 | ret = init_hw_breakpoint(); | |
13766 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 13767 | |
bdacfaf2 NK |
13768 | perf_event_cache = KMEM_CACHE(perf_event, SLAB_PANIC); |
13769 | ||
b01c3a00 JO |
13770 | /* |
13771 | * Build time assertion that we keep the data_head at the intended | |
13772 | * location. IOW, validation we got the __reserved[] size right. | |
13773 | */ | |
13774 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
13775 | != 1024); | |
0793a61d | 13776 | } |
abe43400 | 13777 | |
fd979c01 CS |
13778 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
13779 | char *page) | |
13780 | { | |
13781 | struct perf_pmu_events_attr *pmu_attr = | |
13782 | container_of(attr, struct perf_pmu_events_attr, attr); | |
13783 | ||
13784 | if (pmu_attr->event_str) | |
13785 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
13786 | ||
13787 | return 0; | |
13788 | } | |
675965b0 | 13789 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 13790 | |
abe43400 PZ |
13791 | static int __init perf_event_sysfs_init(void) |
13792 | { | |
13793 | struct pmu *pmu; | |
13794 | int ret; | |
13795 | ||
13796 | mutex_lock(&pmus_lock); | |
13797 | ||
13798 | ret = bus_register(&pmu_bus); | |
13799 | if (ret) | |
13800 | goto unlock; | |
13801 | ||
13802 | list_for_each_entry(pmu, &pmus, entry) { | |
0d6d062c | 13803 | if (pmu->dev) |
abe43400 PZ |
13804 | continue; |
13805 | ||
13806 | ret = pmu_dev_alloc(pmu); | |
13807 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
13808 | } | |
13809 | pmu_bus_running = 1; | |
13810 | ret = 0; | |
13811 | ||
13812 | unlock: | |
13813 | mutex_unlock(&pmus_lock); | |
13814 | ||
13815 | return ret; | |
13816 | } | |
13817 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
13818 | |
13819 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
13820 | static struct cgroup_subsys_state * |
13821 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
13822 | { |
13823 | struct perf_cgroup *jc; | |
e5d1367f | 13824 | |
1b15d055 | 13825 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
13826 | if (!jc) |
13827 | return ERR_PTR(-ENOMEM); | |
13828 | ||
e5d1367f SE |
13829 | jc->info = alloc_percpu(struct perf_cgroup_info); |
13830 | if (!jc->info) { | |
13831 | kfree(jc); | |
13832 | return ERR_PTR(-ENOMEM); | |
13833 | } | |
13834 | ||
e5d1367f SE |
13835 | return &jc->css; |
13836 | } | |
13837 | ||
eb95419b | 13838 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 13839 | { |
eb95419b TH |
13840 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
13841 | ||
e5d1367f SE |
13842 | free_percpu(jc->info); |
13843 | kfree(jc); | |
13844 | } | |
13845 | ||
96aaab68 NK |
13846 | static int perf_cgroup_css_online(struct cgroup_subsys_state *css) |
13847 | { | |
13848 | perf_event_cgroup(css->cgroup); | |
13849 | return 0; | |
13850 | } | |
13851 | ||
e5d1367f SE |
13852 | static int __perf_cgroup_move(void *info) |
13853 | { | |
13854 | struct task_struct *task = info; | |
bd275681 PZ |
13855 | |
13856 | preempt_disable(); | |
f841b682 | 13857 | perf_cgroup_switch(task); |
bd275681 PZ |
13858 | preempt_enable(); |
13859 | ||
e5d1367f SE |
13860 | return 0; |
13861 | } | |
13862 | ||
1f7dd3e5 | 13863 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 13864 | { |
bb9d97b6 | 13865 | struct task_struct *task; |
1f7dd3e5 | 13866 | struct cgroup_subsys_state *css; |
bb9d97b6 | 13867 | |
1f7dd3e5 | 13868 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 13869 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
13870 | } |
13871 | ||
073219e9 | 13872 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
13873 | .css_alloc = perf_cgroup_css_alloc, |
13874 | .css_free = perf_cgroup_css_free, | |
96aaab68 | 13875 | .css_online = perf_cgroup_css_online, |
bb9d97b6 | 13876 | .attach = perf_cgroup_attach, |
968ebff1 TH |
13877 | /* |
13878 | * Implicitly enable on dfl hierarchy so that perf events can | |
13879 | * always be filtered by cgroup2 path as long as perf_event | |
13880 | * controller is not mounted on a legacy hierarchy. | |
13881 | */ | |
13882 | .implicit_on_dfl = true, | |
8cfd8147 | 13883 | .threaded = true, |
e5d1367f SE |
13884 | }; |
13885 | #endif /* CONFIG_CGROUP_PERF */ | |
c22ac2a3 SL |
13886 | |
13887 | DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t); |