Commit | Line | Data |
---|---|---|
0793a61d | 1 | /* |
57c0c15b | 2 | * Performance events core code: |
0793a61d | 3 | * |
98144511 | 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
e7e7ee2e | 5 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
90eec103 | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
d36b6910 | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
7b732a75 | 8 | * |
57c0c15b | 9 | * For licensing details see kernel-base/COPYING |
0793a61d TG |
10 | */ |
11 | ||
12 | #include <linux/fs.h> | |
b9cacc7b | 13 | #include <linux/mm.h> |
0793a61d TG |
14 | #include <linux/cpu.h> |
15 | #include <linux/smp.h> | |
2e80a82a | 16 | #include <linux/idr.h> |
04289bb9 | 17 | #include <linux/file.h> |
0793a61d | 18 | #include <linux/poll.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
76e1d904 | 20 | #include <linux/hash.h> |
12351ef8 | 21 | #include <linux/tick.h> |
0793a61d | 22 | #include <linux/sysfs.h> |
22a4f650 | 23 | #include <linux/dcache.h> |
0793a61d | 24 | #include <linux/percpu.h> |
22a4f650 | 25 | #include <linux/ptrace.h> |
c277443c | 26 | #include <linux/reboot.h> |
b9cacc7b | 27 | #include <linux/vmstat.h> |
abe43400 | 28 | #include <linux/device.h> |
6e5fdeed | 29 | #include <linux/export.h> |
906010b2 | 30 | #include <linux/vmalloc.h> |
b9cacc7b PZ |
31 | #include <linux/hardirq.h> |
32 | #include <linux/rculist.h> | |
0793a61d TG |
33 | #include <linux/uaccess.h> |
34 | #include <linux/syscalls.h> | |
35 | #include <linux/anon_inodes.h> | |
aa9c4c0f | 36 | #include <linux/kernel_stat.h> |
39bed6cb | 37 | #include <linux/cgroup.h> |
cdd6c482 | 38 | #include <linux/perf_event.h> |
af658dca | 39 | #include <linux/trace_events.h> |
3c502e7a | 40 | #include <linux/hw_breakpoint.h> |
c5ebcedb | 41 | #include <linux/mm_types.h> |
c464c76e | 42 | #include <linux/module.h> |
f972eb63 | 43 | #include <linux/mman.h> |
b3f20785 | 44 | #include <linux/compat.h> |
2541517c AS |
45 | #include <linux/bpf.h> |
46 | #include <linux/filter.h> | |
375637bc AS |
47 | #include <linux/namei.h> |
48 | #include <linux/parser.h> | |
e6017571 | 49 | #include <linux/sched/clock.h> |
6e84f315 | 50 | #include <linux/sched/mm.h> |
e4222673 HB |
51 | #include <linux/proc_ns.h> |
52 | #include <linux/mount.h> | |
0793a61d | 53 | |
76369139 FW |
54 | #include "internal.h" |
55 | ||
4e193bd4 TB |
56 | #include <asm/irq_regs.h> |
57 | ||
272325c4 PZ |
58 | typedef int (*remote_function_f)(void *); |
59 | ||
fe4b04fa | 60 | struct remote_function_call { |
e7e7ee2e | 61 | struct task_struct *p; |
272325c4 | 62 | remote_function_f func; |
e7e7ee2e IM |
63 | void *info; |
64 | int ret; | |
fe4b04fa PZ |
65 | }; |
66 | ||
67 | static void remote_function(void *data) | |
68 | { | |
69 | struct remote_function_call *tfc = data; | |
70 | struct task_struct *p = tfc->p; | |
71 | ||
72 | if (p) { | |
0da4cf3e PZ |
73 | /* -EAGAIN */ |
74 | if (task_cpu(p) != smp_processor_id()) | |
75 | return; | |
76 | ||
77 | /* | |
78 | * Now that we're on right CPU with IRQs disabled, we can test | |
79 | * if we hit the right task without races. | |
80 | */ | |
81 | ||
82 | tfc->ret = -ESRCH; /* No such (running) process */ | |
83 | if (p != current) | |
fe4b04fa PZ |
84 | return; |
85 | } | |
86 | ||
87 | tfc->ret = tfc->func(tfc->info); | |
88 | } | |
89 | ||
90 | /** | |
91 | * task_function_call - call a function on the cpu on which a task runs | |
92 | * @p: the task to evaluate | |
93 | * @func: the function to be called | |
94 | * @info: the function call argument | |
95 | * | |
96 | * Calls the function @func when the task is currently running. This might | |
97 | * be on the current CPU, which just calls the function directly | |
98 | * | |
99 | * returns: @func return value, or | |
100 | * -ESRCH - when the process isn't running | |
101 | * -EAGAIN - when the process moved away | |
102 | */ | |
103 | static int | |
272325c4 | 104 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
105 | { |
106 | struct remote_function_call data = { | |
e7e7ee2e IM |
107 | .p = p, |
108 | .func = func, | |
109 | .info = info, | |
0da4cf3e | 110 | .ret = -EAGAIN, |
fe4b04fa | 111 | }; |
0da4cf3e | 112 | int ret; |
fe4b04fa | 113 | |
0da4cf3e PZ |
114 | do { |
115 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); | |
116 | if (!ret) | |
117 | ret = data.ret; | |
118 | } while (ret == -EAGAIN); | |
fe4b04fa | 119 | |
0da4cf3e | 120 | return ret; |
fe4b04fa PZ |
121 | } |
122 | ||
123 | /** | |
124 | * cpu_function_call - call a function on the cpu | |
125 | * @func: the function to be called | |
126 | * @info: the function call argument | |
127 | * | |
128 | * Calls the function @func on the remote cpu. | |
129 | * | |
130 | * returns: @func return value or -ENXIO when the cpu is offline | |
131 | */ | |
272325c4 | 132 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
133 | { |
134 | struct remote_function_call data = { | |
e7e7ee2e IM |
135 | .p = NULL, |
136 | .func = func, | |
137 | .info = info, | |
138 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
139 | }; |
140 | ||
141 | smp_call_function_single(cpu, remote_function, &data, 1); | |
142 | ||
143 | return data.ret; | |
144 | } | |
145 | ||
fae3fde6 PZ |
146 | static inline struct perf_cpu_context * |
147 | __get_cpu_context(struct perf_event_context *ctx) | |
148 | { | |
149 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); | |
150 | } | |
151 | ||
152 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, | |
153 | struct perf_event_context *ctx) | |
0017960f | 154 | { |
fae3fde6 PZ |
155 | raw_spin_lock(&cpuctx->ctx.lock); |
156 | if (ctx) | |
157 | raw_spin_lock(&ctx->lock); | |
158 | } | |
159 | ||
160 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
161 | struct perf_event_context *ctx) | |
162 | { | |
163 | if (ctx) | |
164 | raw_spin_unlock(&ctx->lock); | |
165 | raw_spin_unlock(&cpuctx->ctx.lock); | |
166 | } | |
167 | ||
63b6da39 PZ |
168 | #define TASK_TOMBSTONE ((void *)-1L) |
169 | ||
170 | static bool is_kernel_event(struct perf_event *event) | |
171 | { | |
f47c02c0 | 172 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
173 | } |
174 | ||
39a43640 PZ |
175 | /* |
176 | * On task ctx scheduling... | |
177 | * | |
178 | * When !ctx->nr_events a task context will not be scheduled. This means | |
179 | * we can disable the scheduler hooks (for performance) without leaving | |
180 | * pending task ctx state. | |
181 | * | |
182 | * This however results in two special cases: | |
183 | * | |
184 | * - removing the last event from a task ctx; this is relatively straight | |
185 | * forward and is done in __perf_remove_from_context. | |
186 | * | |
187 | * - adding the first event to a task ctx; this is tricky because we cannot | |
188 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
189 | * See perf_install_in_context(). | |
190 | * | |
39a43640 PZ |
191 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
192 | */ | |
193 | ||
fae3fde6 PZ |
194 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
195 | struct perf_event_context *, void *); | |
196 | ||
197 | struct event_function_struct { | |
198 | struct perf_event *event; | |
199 | event_f func; | |
200 | void *data; | |
201 | }; | |
202 | ||
203 | static int event_function(void *info) | |
204 | { | |
205 | struct event_function_struct *efs = info; | |
206 | struct perf_event *event = efs->event; | |
0017960f | 207 | struct perf_event_context *ctx = event->ctx; |
fae3fde6 PZ |
208 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
209 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
63b6da39 | 210 | int ret = 0; |
fae3fde6 | 211 | |
16444645 | 212 | lockdep_assert_irqs_disabled(); |
fae3fde6 | 213 | |
63b6da39 | 214 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
215 | /* |
216 | * Since we do the IPI call without holding ctx->lock things can have | |
217 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
218 | */ |
219 | if (ctx->task) { | |
63b6da39 | 220 | if (ctx->task != current) { |
0da4cf3e | 221 | ret = -ESRCH; |
63b6da39 PZ |
222 | goto unlock; |
223 | } | |
fae3fde6 | 224 | |
fae3fde6 PZ |
225 | /* |
226 | * We only use event_function_call() on established contexts, | |
227 | * and event_function() is only ever called when active (or | |
228 | * rather, we'll have bailed in task_function_call() or the | |
229 | * above ctx->task != current test), therefore we must have | |
230 | * ctx->is_active here. | |
231 | */ | |
232 | WARN_ON_ONCE(!ctx->is_active); | |
233 | /* | |
234 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
235 | * match. | |
236 | */ | |
63b6da39 PZ |
237 | WARN_ON_ONCE(task_ctx != ctx); |
238 | } else { | |
239 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 240 | } |
63b6da39 | 241 | |
fae3fde6 | 242 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 243 | unlock: |
fae3fde6 PZ |
244 | perf_ctx_unlock(cpuctx, task_ctx); |
245 | ||
63b6da39 | 246 | return ret; |
fae3fde6 PZ |
247 | } |
248 | ||
fae3fde6 | 249 | static void event_function_call(struct perf_event *event, event_f func, void *data) |
0017960f PZ |
250 | { |
251 | struct perf_event_context *ctx = event->ctx; | |
63b6da39 | 252 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
fae3fde6 PZ |
253 | struct event_function_struct efs = { |
254 | .event = event, | |
255 | .func = func, | |
256 | .data = data, | |
257 | }; | |
0017960f | 258 | |
c97f4736 PZ |
259 | if (!event->parent) { |
260 | /* | |
261 | * If this is a !child event, we must hold ctx::mutex to | |
262 | * stabilize the the event->ctx relation. See | |
263 | * perf_event_ctx_lock(). | |
264 | */ | |
265 | lockdep_assert_held(&ctx->mutex); | |
266 | } | |
0017960f PZ |
267 | |
268 | if (!task) { | |
fae3fde6 | 269 | cpu_function_call(event->cpu, event_function, &efs); |
0017960f PZ |
270 | return; |
271 | } | |
272 | ||
63b6da39 PZ |
273 | if (task == TASK_TOMBSTONE) |
274 | return; | |
275 | ||
a096309b | 276 | again: |
fae3fde6 | 277 | if (!task_function_call(task, event_function, &efs)) |
0017960f PZ |
278 | return; |
279 | ||
280 | raw_spin_lock_irq(&ctx->lock); | |
63b6da39 PZ |
281 | /* |
282 | * Reload the task pointer, it might have been changed by | |
283 | * a concurrent perf_event_context_sched_out(). | |
284 | */ | |
285 | task = ctx->task; | |
a096309b PZ |
286 | if (task == TASK_TOMBSTONE) { |
287 | raw_spin_unlock_irq(&ctx->lock); | |
288 | return; | |
0017960f | 289 | } |
a096309b PZ |
290 | if (ctx->is_active) { |
291 | raw_spin_unlock_irq(&ctx->lock); | |
292 | goto again; | |
293 | } | |
294 | func(event, NULL, ctx, data); | |
0017960f PZ |
295 | raw_spin_unlock_irq(&ctx->lock); |
296 | } | |
297 | ||
cca20946 PZ |
298 | /* |
299 | * Similar to event_function_call() + event_function(), but hard assumes IRQs | |
300 | * are already disabled and we're on the right CPU. | |
301 | */ | |
302 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
303 | { | |
304 | struct perf_event_context *ctx = event->ctx; | |
305 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
306 | struct task_struct *task = READ_ONCE(ctx->task); | |
307 | struct perf_event_context *task_ctx = NULL; | |
308 | ||
16444645 | 309 | lockdep_assert_irqs_disabled(); |
cca20946 PZ |
310 | |
311 | if (task) { | |
312 | if (task == TASK_TOMBSTONE) | |
313 | return; | |
314 | ||
315 | task_ctx = ctx; | |
316 | } | |
317 | ||
318 | perf_ctx_lock(cpuctx, task_ctx); | |
319 | ||
320 | task = ctx->task; | |
321 | if (task == TASK_TOMBSTONE) | |
322 | goto unlock; | |
323 | ||
324 | if (task) { | |
325 | /* | |
326 | * We must be either inactive or active and the right task, | |
327 | * otherwise we're screwed, since we cannot IPI to somewhere | |
328 | * else. | |
329 | */ | |
330 | if (ctx->is_active) { | |
331 | if (WARN_ON_ONCE(task != current)) | |
332 | goto unlock; | |
333 | ||
334 | if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) | |
335 | goto unlock; | |
336 | } | |
337 | } else { | |
338 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
339 | } | |
340 | ||
341 | func(event, cpuctx, ctx, data); | |
342 | unlock: | |
343 | perf_ctx_unlock(cpuctx, task_ctx); | |
344 | } | |
345 | ||
e5d1367f SE |
346 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
347 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
348 | PERF_FLAG_PID_CGROUP |\ |
349 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 350 | |
bce38cd5 SE |
351 | /* |
352 | * branch priv levels that need permission checks | |
353 | */ | |
354 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
355 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
356 | PERF_SAMPLE_BRANCH_HV) | |
357 | ||
0b3fcf17 SE |
358 | enum event_type_t { |
359 | EVENT_FLEXIBLE = 0x1, | |
360 | EVENT_PINNED = 0x2, | |
3cbaa590 | 361 | EVENT_TIME = 0x4, |
487f05e1 AS |
362 | /* see ctx_resched() for details */ |
363 | EVENT_CPU = 0x8, | |
0b3fcf17 SE |
364 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
365 | }; | |
366 | ||
e5d1367f SE |
367 | /* |
368 | * perf_sched_events : >0 events exist | |
369 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu | |
370 | */ | |
9107c89e PZ |
371 | |
372 | static void perf_sched_delayed(struct work_struct *work); | |
373 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
374 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
375 | static DEFINE_MUTEX(perf_sched_mutex); | |
376 | static atomic_t perf_sched_count; | |
377 | ||
e5d1367f | 378 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
ba532500 | 379 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
f2fb6bef | 380 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
e5d1367f | 381 | |
cdd6c482 IM |
382 | static atomic_t nr_mmap_events __read_mostly; |
383 | static atomic_t nr_comm_events __read_mostly; | |
e4222673 | 384 | static atomic_t nr_namespaces_events __read_mostly; |
cdd6c482 | 385 | static atomic_t nr_task_events __read_mostly; |
948b26b6 | 386 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 387 | static atomic_t nr_switch_events __read_mostly; |
9ee318a7 | 388 | |
108b02cf PZ |
389 | static LIST_HEAD(pmus); |
390 | static DEFINE_MUTEX(pmus_lock); | |
391 | static struct srcu_struct pmus_srcu; | |
a63fbed7 | 392 | static cpumask_var_t perf_online_mask; |
108b02cf | 393 | |
0764771d | 394 | /* |
cdd6c482 | 395 | * perf event paranoia level: |
0fbdea19 IM |
396 | * -1 - not paranoid at all |
397 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 398 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 399 | * 2 - disallow kernel profiling for unpriv |
0764771d | 400 | */ |
0161028b | 401 | int sysctl_perf_event_paranoid __read_mostly = 2; |
0764771d | 402 | |
20443384 FW |
403 | /* Minimum for 512 kiB + 1 user control page */ |
404 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
405 | |
406 | /* | |
cdd6c482 | 407 | * max perf event sample rate |
df58ab24 | 408 | */ |
14c63f17 DH |
409 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
410 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
411 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
412 | ||
413 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
414 | ||
415 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
416 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
417 | ||
d9494cb4 PZ |
418 | static int perf_sample_allowed_ns __read_mostly = |
419 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 420 | |
18ab2cd3 | 421 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
422 | { |
423 | u64 tmp = perf_sample_period_ns; | |
424 | ||
425 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
426 | tmp = div_u64(tmp, 100); |
427 | if (!tmp) | |
428 | tmp = 1; | |
429 | ||
430 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 431 | } |
163ec435 | 432 | |
8d5bce0c | 433 | static bool perf_rotate_context(struct perf_cpu_context *cpuctx); |
9e630205 | 434 | |
163ec435 PZ |
435 | int perf_proc_update_handler(struct ctl_table *table, int write, |
436 | void __user *buffer, size_t *lenp, | |
437 | loff_t *ppos) | |
438 | { | |
723478c8 | 439 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
163ec435 PZ |
440 | |
441 | if (ret || !write) | |
442 | return ret; | |
443 | ||
ab7fdefb KL |
444 | /* |
445 | * If throttling is disabled don't allow the write: | |
446 | */ | |
447 | if (sysctl_perf_cpu_time_max_percent == 100 || | |
448 | sysctl_perf_cpu_time_max_percent == 0) | |
449 | return -EINVAL; | |
450 | ||
163ec435 | 451 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
14c63f17 DH |
452 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
453 | update_perf_cpu_limits(); | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
459 | ||
460 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
461 | void __user *buffer, size_t *lenp, | |
462 | loff_t *ppos) | |
463 | { | |
1572e45a | 464 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
14c63f17 DH |
465 | |
466 | if (ret || !write) | |
467 | return ret; | |
468 | ||
b303e7c1 PZ |
469 | if (sysctl_perf_cpu_time_max_percent == 100 || |
470 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
471 | printk(KERN_WARNING |
472 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
473 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
474 | } else { | |
475 | update_perf_cpu_limits(); | |
476 | } | |
163ec435 PZ |
477 | |
478 | return 0; | |
479 | } | |
1ccd1549 | 480 | |
14c63f17 DH |
481 | /* |
482 | * perf samples are done in some very critical code paths (NMIs). | |
483 | * If they take too much CPU time, the system can lock up and not | |
484 | * get any real work done. This will drop the sample rate when | |
485 | * we detect that events are taking too long. | |
486 | */ | |
487 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 488 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 489 | |
91a612ee PZ |
490 | static u64 __report_avg; |
491 | static u64 __report_allowed; | |
492 | ||
6a02ad66 | 493 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 494 | { |
0d87d7ec | 495 | printk_ratelimited(KERN_INFO |
91a612ee PZ |
496 | "perf: interrupt took too long (%lld > %lld), lowering " |
497 | "kernel.perf_event_max_sample_rate to %d\n", | |
498 | __report_avg, __report_allowed, | |
499 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
500 | } |
501 | ||
502 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
503 | ||
504 | void perf_sample_event_took(u64 sample_len_ns) | |
505 | { | |
91a612ee PZ |
506 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
507 | u64 running_len; | |
508 | u64 avg_len; | |
509 | u32 max; | |
14c63f17 | 510 | |
91a612ee | 511 | if (max_len == 0) |
14c63f17 DH |
512 | return; |
513 | ||
91a612ee PZ |
514 | /* Decay the counter by 1 average sample. */ |
515 | running_len = __this_cpu_read(running_sample_length); | |
516 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
517 | running_len += sample_len_ns; | |
518 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
519 | |
520 | /* | |
91a612ee PZ |
521 | * Note: this will be biased artifically low until we have |
522 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
523 | * from having to maintain a count. |
524 | */ | |
91a612ee PZ |
525 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
526 | if (avg_len <= max_len) | |
14c63f17 DH |
527 | return; |
528 | ||
91a612ee PZ |
529 | __report_avg = avg_len; |
530 | __report_allowed = max_len; | |
14c63f17 | 531 | |
91a612ee PZ |
532 | /* |
533 | * Compute a throttle threshold 25% below the current duration. | |
534 | */ | |
535 | avg_len += avg_len / 4; | |
536 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
537 | if (avg_len < max) | |
538 | max /= (u32)avg_len; | |
539 | else | |
540 | max = 1; | |
14c63f17 | 541 | |
91a612ee PZ |
542 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
543 | WRITE_ONCE(max_samples_per_tick, max); | |
544 | ||
545 | sysctl_perf_event_sample_rate = max * HZ; | |
546 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 547 | |
cd578abb | 548 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 549 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 550 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 551 | __report_avg, __report_allowed, |
cd578abb PZ |
552 | sysctl_perf_event_sample_rate); |
553 | } | |
14c63f17 DH |
554 | } |
555 | ||
cdd6c482 | 556 | static atomic64_t perf_event_id; |
a96bbc16 | 557 | |
0b3fcf17 SE |
558 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
559 | enum event_type_t event_type); | |
560 | ||
561 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
562 | enum event_type_t event_type, |
563 | struct task_struct *task); | |
564 | ||
565 | static void update_context_time(struct perf_event_context *ctx); | |
566 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 567 | |
cdd6c482 | 568 | void __weak perf_event_print_debug(void) { } |
0793a61d | 569 | |
84c79910 | 570 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 571 | { |
84c79910 | 572 | return "pmu"; |
0793a61d TG |
573 | } |
574 | ||
0b3fcf17 SE |
575 | static inline u64 perf_clock(void) |
576 | { | |
577 | return local_clock(); | |
578 | } | |
579 | ||
34f43927 PZ |
580 | static inline u64 perf_event_clock(struct perf_event *event) |
581 | { | |
582 | return event->clock(); | |
583 | } | |
584 | ||
0d3d73aa PZ |
585 | /* |
586 | * State based event timekeeping... | |
587 | * | |
588 | * The basic idea is to use event->state to determine which (if any) time | |
589 | * fields to increment with the current delta. This means we only need to | |
590 | * update timestamps when we change state or when they are explicitly requested | |
591 | * (read). | |
592 | * | |
593 | * Event groups make things a little more complicated, but not terribly so. The | |
594 | * rules for a group are that if the group leader is OFF the entire group is | |
595 | * OFF, irrespecive of what the group member states are. This results in | |
596 | * __perf_effective_state(). | |
597 | * | |
598 | * A futher ramification is that when a group leader flips between OFF and | |
599 | * !OFF, we need to update all group member times. | |
600 | * | |
601 | * | |
602 | * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we | |
603 | * need to make sure the relevant context time is updated before we try and | |
604 | * update our timestamps. | |
605 | */ | |
606 | ||
607 | static __always_inline enum perf_event_state | |
608 | __perf_effective_state(struct perf_event *event) | |
609 | { | |
610 | struct perf_event *leader = event->group_leader; | |
611 | ||
612 | if (leader->state <= PERF_EVENT_STATE_OFF) | |
613 | return leader->state; | |
614 | ||
615 | return event->state; | |
616 | } | |
617 | ||
618 | static __always_inline void | |
619 | __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running) | |
620 | { | |
621 | enum perf_event_state state = __perf_effective_state(event); | |
622 | u64 delta = now - event->tstamp; | |
623 | ||
624 | *enabled = event->total_time_enabled; | |
625 | if (state >= PERF_EVENT_STATE_INACTIVE) | |
626 | *enabled += delta; | |
627 | ||
628 | *running = event->total_time_running; | |
629 | if (state >= PERF_EVENT_STATE_ACTIVE) | |
630 | *running += delta; | |
631 | } | |
632 | ||
633 | static void perf_event_update_time(struct perf_event *event) | |
634 | { | |
635 | u64 now = perf_event_time(event); | |
636 | ||
637 | __perf_update_times(event, now, &event->total_time_enabled, | |
638 | &event->total_time_running); | |
639 | event->tstamp = now; | |
640 | } | |
641 | ||
642 | static void perf_event_update_sibling_time(struct perf_event *leader) | |
643 | { | |
644 | struct perf_event *sibling; | |
645 | ||
8343aae6 | 646 | list_for_each_entry(sibling, &leader->sibling_list, sibling_list) |
0d3d73aa PZ |
647 | perf_event_update_time(sibling); |
648 | } | |
649 | ||
650 | static void | |
651 | perf_event_set_state(struct perf_event *event, enum perf_event_state state) | |
652 | { | |
653 | if (event->state == state) | |
654 | return; | |
655 | ||
656 | perf_event_update_time(event); | |
657 | /* | |
658 | * If a group leader gets enabled/disabled all its siblings | |
659 | * are affected too. | |
660 | */ | |
661 | if ((event->state < 0) ^ (state < 0)) | |
662 | perf_event_update_sibling_time(event); | |
663 | ||
664 | WRITE_ONCE(event->state, state); | |
665 | } | |
666 | ||
e5d1367f SE |
667 | #ifdef CONFIG_CGROUP_PERF |
668 | ||
e5d1367f SE |
669 | static inline bool |
670 | perf_cgroup_match(struct perf_event *event) | |
671 | { | |
672 | struct perf_event_context *ctx = event->ctx; | |
673 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
674 | ||
ef824fa1 TH |
675 | /* @event doesn't care about cgroup */ |
676 | if (!event->cgrp) | |
677 | return true; | |
678 | ||
679 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
680 | if (!cpuctx->cgrp) | |
681 | return false; | |
682 | ||
683 | /* | |
684 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
685 | * also enabled for all its descendant cgroups. If @cpuctx's | |
686 | * cgroup is a descendant of @event's (the test covers identity | |
687 | * case), it's a match. | |
688 | */ | |
689 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
690 | event->cgrp->css.cgroup); | |
e5d1367f SE |
691 | } |
692 | ||
e5d1367f SE |
693 | static inline void perf_detach_cgroup(struct perf_event *event) |
694 | { | |
4e2ba650 | 695 | css_put(&event->cgrp->css); |
e5d1367f SE |
696 | event->cgrp = NULL; |
697 | } | |
698 | ||
699 | static inline int is_cgroup_event(struct perf_event *event) | |
700 | { | |
701 | return event->cgrp != NULL; | |
702 | } | |
703 | ||
704 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
705 | { | |
706 | struct perf_cgroup_info *t; | |
707 | ||
708 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
709 | return t->time; | |
710 | } | |
711 | ||
712 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
713 | { | |
714 | struct perf_cgroup_info *info; | |
715 | u64 now; | |
716 | ||
717 | now = perf_clock(); | |
718 | ||
719 | info = this_cpu_ptr(cgrp->info); | |
720 | ||
721 | info->time += now - info->timestamp; | |
722 | info->timestamp = now; | |
723 | } | |
724 | ||
725 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
726 | { | |
727 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
728 | if (cgrp_out) | |
729 | __update_cgrp_time(cgrp_out); | |
730 | } | |
731 | ||
732 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
733 | { | |
3f7cce3c SE |
734 | struct perf_cgroup *cgrp; |
735 | ||
e5d1367f | 736 | /* |
3f7cce3c SE |
737 | * ensure we access cgroup data only when needed and |
738 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 739 | */ |
3f7cce3c | 740 | if (!is_cgroup_event(event)) |
e5d1367f SE |
741 | return; |
742 | ||
614e4c4e | 743 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
744 | /* |
745 | * Do not update time when cgroup is not active | |
746 | */ | |
e6a52033 | 747 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
3f7cce3c | 748 | __update_cgrp_time(event->cgrp); |
e5d1367f SE |
749 | } |
750 | ||
751 | static inline void | |
3f7cce3c SE |
752 | perf_cgroup_set_timestamp(struct task_struct *task, |
753 | struct perf_event_context *ctx) | |
e5d1367f SE |
754 | { |
755 | struct perf_cgroup *cgrp; | |
756 | struct perf_cgroup_info *info; | |
757 | ||
3f7cce3c SE |
758 | /* |
759 | * ctx->lock held by caller | |
760 | * ensure we do not access cgroup data | |
761 | * unless we have the cgroup pinned (css_get) | |
762 | */ | |
763 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
764 | return; |
765 | ||
614e4c4e | 766 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 767 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 768 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
769 | } |
770 | ||
058fe1c0 DCC |
771 | static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list); |
772 | ||
e5d1367f SE |
773 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
774 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
775 | ||
776 | /* | |
777 | * reschedule events based on the cgroup constraint of task. | |
778 | * | |
779 | * mode SWOUT : schedule out everything | |
780 | * mode SWIN : schedule in based on cgroup for next | |
781 | */ | |
18ab2cd3 | 782 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
783 | { |
784 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 785 | struct list_head *list; |
e5d1367f SE |
786 | unsigned long flags; |
787 | ||
788 | /* | |
058fe1c0 DCC |
789 | * Disable interrupts and preemption to avoid this CPU's |
790 | * cgrp_cpuctx_entry to change under us. | |
e5d1367f SE |
791 | */ |
792 | local_irq_save(flags); | |
793 | ||
058fe1c0 DCC |
794 | list = this_cpu_ptr(&cgrp_cpuctx_list); |
795 | list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) { | |
796 | WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); | |
e5d1367f | 797 | |
058fe1c0 DCC |
798 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
799 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f | 800 | |
058fe1c0 DCC |
801 | if (mode & PERF_CGROUP_SWOUT) { |
802 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
803 | /* | |
804 | * must not be done before ctxswout due | |
805 | * to event_filter_match() in event_sched_out() | |
806 | */ | |
807 | cpuctx->cgrp = NULL; | |
808 | } | |
e5d1367f | 809 | |
058fe1c0 DCC |
810 | if (mode & PERF_CGROUP_SWIN) { |
811 | WARN_ON_ONCE(cpuctx->cgrp); | |
812 | /* | |
813 | * set cgrp before ctxsw in to allow | |
814 | * event_filter_match() to not have to pass | |
815 | * task around | |
816 | * we pass the cpuctx->ctx to perf_cgroup_from_task() | |
817 | * because cgorup events are only per-cpu | |
818 | */ | |
819 | cpuctx->cgrp = perf_cgroup_from_task(task, | |
820 | &cpuctx->ctx); | |
821 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); | |
e5d1367f | 822 | } |
058fe1c0 DCC |
823 | perf_pmu_enable(cpuctx->ctx.pmu); |
824 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f SE |
825 | } |
826 | ||
e5d1367f SE |
827 | local_irq_restore(flags); |
828 | } | |
829 | ||
a8d757ef SE |
830 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
831 | struct task_struct *next) | |
e5d1367f | 832 | { |
a8d757ef SE |
833 | struct perf_cgroup *cgrp1; |
834 | struct perf_cgroup *cgrp2 = NULL; | |
835 | ||
ddaaf4e2 | 836 | rcu_read_lock(); |
a8d757ef SE |
837 | /* |
838 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
839 | * we do not need to pass the ctx here because we know |
840 | * we are holding the rcu lock | |
a8d757ef | 841 | */ |
614e4c4e | 842 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 843 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
844 | |
845 | /* | |
846 | * only schedule out current cgroup events if we know | |
847 | * that we are switching to a different cgroup. Otherwise, | |
848 | * do no touch the cgroup events. | |
849 | */ | |
850 | if (cgrp1 != cgrp2) | |
851 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
852 | |
853 | rcu_read_unlock(); | |
e5d1367f SE |
854 | } |
855 | ||
a8d757ef SE |
856 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
857 | struct task_struct *task) | |
e5d1367f | 858 | { |
a8d757ef SE |
859 | struct perf_cgroup *cgrp1; |
860 | struct perf_cgroup *cgrp2 = NULL; | |
861 | ||
ddaaf4e2 | 862 | rcu_read_lock(); |
a8d757ef SE |
863 | /* |
864 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
865 | * we do not need to pass the ctx here because we know |
866 | * we are holding the rcu lock | |
a8d757ef | 867 | */ |
614e4c4e | 868 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 869 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
870 | |
871 | /* | |
872 | * only need to schedule in cgroup events if we are changing | |
873 | * cgroup during ctxsw. Cgroup events were not scheduled | |
874 | * out of ctxsw out if that was not the case. | |
875 | */ | |
876 | if (cgrp1 != cgrp2) | |
877 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
878 | |
879 | rcu_read_unlock(); | |
e5d1367f SE |
880 | } |
881 | ||
882 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
883 | struct perf_event_attr *attr, | |
884 | struct perf_event *group_leader) | |
885 | { | |
886 | struct perf_cgroup *cgrp; | |
887 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
888 | struct fd f = fdget(fd); |
889 | int ret = 0; | |
e5d1367f | 890 | |
2903ff01 | 891 | if (!f.file) |
e5d1367f SE |
892 | return -EBADF; |
893 | ||
b583043e | 894 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 895 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
896 | if (IS_ERR(css)) { |
897 | ret = PTR_ERR(css); | |
898 | goto out; | |
899 | } | |
e5d1367f SE |
900 | |
901 | cgrp = container_of(css, struct perf_cgroup, css); | |
902 | event->cgrp = cgrp; | |
903 | ||
904 | /* | |
905 | * all events in a group must monitor | |
906 | * the same cgroup because a task belongs | |
907 | * to only one perf cgroup at a time | |
908 | */ | |
909 | if (group_leader && group_leader->cgrp != cgrp) { | |
910 | perf_detach_cgroup(event); | |
911 | ret = -EINVAL; | |
e5d1367f | 912 | } |
3db272c0 | 913 | out: |
2903ff01 | 914 | fdput(f); |
e5d1367f SE |
915 | return ret; |
916 | } | |
917 | ||
918 | static inline void | |
919 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
920 | { | |
921 | struct perf_cgroup_info *t; | |
922 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
923 | event->shadow_ctx_time = now - t->timestamp; | |
924 | } | |
925 | ||
db4a8356 DCC |
926 | /* |
927 | * Update cpuctx->cgrp so that it is set when first cgroup event is added and | |
928 | * cleared when last cgroup event is removed. | |
929 | */ | |
930 | static inline void | |
931 | list_update_cgroup_event(struct perf_event *event, | |
932 | struct perf_event_context *ctx, bool add) | |
933 | { | |
934 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 935 | struct list_head *cpuctx_entry; |
db4a8356 DCC |
936 | |
937 | if (!is_cgroup_event(event)) | |
938 | return; | |
939 | ||
db4a8356 DCC |
940 | /* |
941 | * Because cgroup events are always per-cpu events, | |
942 | * this will always be called from the right CPU. | |
943 | */ | |
944 | cpuctx = __get_cpu_context(ctx); | |
33801b94 | 945 | |
946 | /* | |
947 | * Since setting cpuctx->cgrp is conditional on the current @cgrp | |
948 | * matching the event's cgroup, we must do this for every new event, | |
949 | * because if the first would mismatch, the second would not try again | |
950 | * and we would leave cpuctx->cgrp unset. | |
951 | */ | |
952 | if (add && !cpuctx->cgrp) { | |
be96b316 TH |
953 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); |
954 | ||
be96b316 TH |
955 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
956 | cpuctx->cgrp = cgrp; | |
058fe1c0 | 957 | } |
33801b94 | 958 | |
959 | if (add && ctx->nr_cgroups++) | |
960 | return; | |
961 | else if (!add && --ctx->nr_cgroups) | |
962 | return; | |
963 | ||
964 | /* no cgroup running */ | |
965 | if (!add) | |
966 | cpuctx->cgrp = NULL; | |
967 | ||
968 | cpuctx_entry = &cpuctx->cgrp_cpuctx_entry; | |
969 | if (add) | |
970 | list_add(cpuctx_entry, this_cpu_ptr(&cgrp_cpuctx_list)); | |
971 | else | |
972 | list_del(cpuctx_entry); | |
db4a8356 DCC |
973 | } |
974 | ||
e5d1367f SE |
975 | #else /* !CONFIG_CGROUP_PERF */ |
976 | ||
977 | static inline bool | |
978 | perf_cgroup_match(struct perf_event *event) | |
979 | { | |
980 | return true; | |
981 | } | |
982 | ||
983 | static inline void perf_detach_cgroup(struct perf_event *event) | |
984 | {} | |
985 | ||
986 | static inline int is_cgroup_event(struct perf_event *event) | |
987 | { | |
988 | return 0; | |
989 | } | |
990 | ||
e5d1367f SE |
991 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
992 | { | |
993 | } | |
994 | ||
995 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
996 | { | |
997 | } | |
998 | ||
a8d757ef SE |
999 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
1000 | struct task_struct *next) | |
e5d1367f SE |
1001 | { |
1002 | } | |
1003 | ||
a8d757ef SE |
1004 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
1005 | struct task_struct *task) | |
e5d1367f SE |
1006 | { |
1007 | } | |
1008 | ||
1009 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
1010 | struct perf_event_attr *attr, | |
1011 | struct perf_event *group_leader) | |
1012 | { | |
1013 | return -EINVAL; | |
1014 | } | |
1015 | ||
1016 | static inline void | |
3f7cce3c SE |
1017 | perf_cgroup_set_timestamp(struct task_struct *task, |
1018 | struct perf_event_context *ctx) | |
e5d1367f SE |
1019 | { |
1020 | } | |
1021 | ||
1022 | void | |
1023 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
1024 | { | |
1025 | } | |
1026 | ||
1027 | static inline void | |
1028 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
1029 | { | |
1030 | } | |
1031 | ||
1032 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
1033 | { | |
1034 | return 0; | |
1035 | } | |
1036 | ||
db4a8356 DCC |
1037 | static inline void |
1038 | list_update_cgroup_event(struct perf_event *event, | |
1039 | struct perf_event_context *ctx, bool add) | |
1040 | { | |
1041 | } | |
1042 | ||
e5d1367f SE |
1043 | #endif |
1044 | ||
9e630205 SE |
1045 | /* |
1046 | * set default to be dependent on timer tick just | |
1047 | * like original code | |
1048 | */ | |
1049 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
1050 | /* | |
8a1115ff | 1051 | * function must be called with interrupts disabled |
9e630205 | 1052 | */ |
272325c4 | 1053 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
1054 | { |
1055 | struct perf_cpu_context *cpuctx; | |
8d5bce0c | 1056 | bool rotations; |
9e630205 | 1057 | |
16444645 | 1058 | lockdep_assert_irqs_disabled(); |
9e630205 SE |
1059 | |
1060 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
1061 | rotations = perf_rotate_context(cpuctx); |
1062 | ||
4cfafd30 PZ |
1063 | raw_spin_lock(&cpuctx->hrtimer_lock); |
1064 | if (rotations) | |
9e630205 | 1065 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
1066 | else |
1067 | cpuctx->hrtimer_active = 0; | |
1068 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 1069 | |
4cfafd30 | 1070 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
1071 | } |
1072 | ||
272325c4 | 1073 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 1074 | { |
272325c4 | 1075 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1076 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 1077 | u64 interval; |
9e630205 SE |
1078 | |
1079 | /* no multiplexing needed for SW PMU */ | |
1080 | if (pmu->task_ctx_nr == perf_sw_context) | |
1081 | return; | |
1082 | ||
62b85639 SE |
1083 | /* |
1084 | * check default is sane, if not set then force to | |
1085 | * default interval (1/tick) | |
1086 | */ | |
272325c4 PZ |
1087 | interval = pmu->hrtimer_interval_ms; |
1088 | if (interval < 1) | |
1089 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 1090 | |
272325c4 | 1091 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 1092 | |
4cfafd30 PZ |
1093 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
1094 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 1095 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
1096 | } |
1097 | ||
272325c4 | 1098 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 1099 | { |
272325c4 | 1100 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1101 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 1102 | unsigned long flags; |
9e630205 SE |
1103 | |
1104 | /* not for SW PMU */ | |
1105 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 1106 | return 0; |
9e630205 | 1107 | |
4cfafd30 PZ |
1108 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
1109 | if (!cpuctx->hrtimer_active) { | |
1110 | cpuctx->hrtimer_active = 1; | |
1111 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
1112 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
1113 | } | |
1114 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 1115 | |
272325c4 | 1116 | return 0; |
9e630205 SE |
1117 | } |
1118 | ||
33696fc0 | 1119 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1120 | { |
33696fc0 PZ |
1121 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1122 | if (!(*count)++) | |
1123 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1124 | } |
9e35ad38 | 1125 | |
33696fc0 | 1126 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1127 | { |
33696fc0 PZ |
1128 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1129 | if (!--(*count)) | |
1130 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1131 | } |
9e35ad38 | 1132 | |
2fde4f94 | 1133 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1134 | |
1135 | /* | |
2fde4f94 MR |
1136 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1137 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1138 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1139 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1140 | */ |
2fde4f94 | 1141 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1142 | { |
2fde4f94 | 1143 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1144 | |
16444645 | 1145 | lockdep_assert_irqs_disabled(); |
b5ab4cd5 | 1146 | |
2fde4f94 MR |
1147 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1148 | ||
1149 | list_add(&ctx->active_ctx_list, head); | |
1150 | } | |
1151 | ||
1152 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1153 | { | |
16444645 | 1154 | lockdep_assert_irqs_disabled(); |
2fde4f94 MR |
1155 | |
1156 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1157 | ||
1158 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1159 | } |
9e35ad38 | 1160 | |
cdd6c482 | 1161 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1162 | { |
e5289d4a | 1163 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1164 | } |
1165 | ||
4af57ef2 YZ |
1166 | static void free_ctx(struct rcu_head *head) |
1167 | { | |
1168 | struct perf_event_context *ctx; | |
1169 | ||
1170 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1171 | kfree(ctx->task_ctx_data); | |
1172 | kfree(ctx); | |
1173 | } | |
1174 | ||
cdd6c482 | 1175 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1176 | { |
564c2b21 PM |
1177 | if (atomic_dec_and_test(&ctx->refcount)) { |
1178 | if (ctx->parent_ctx) | |
1179 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1180 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1181 | put_task_struct(ctx->task); |
4af57ef2 | 1182 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1183 | } |
a63eaf34 PM |
1184 | } |
1185 | ||
f63a8daa PZ |
1186 | /* |
1187 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1188 | * perf_pmu_migrate_context() we need some magic. | |
1189 | * | |
1190 | * Those places that change perf_event::ctx will hold both | |
1191 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1192 | * | |
8b10c5e2 PZ |
1193 | * Lock ordering is by mutex address. There are two other sites where |
1194 | * perf_event_context::mutex nests and those are: | |
1195 | * | |
1196 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1197 | * perf_event_exit_event() |
1198 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1199 | * |
1200 | * - perf_event_init_context() [ parent, 0 ] | |
1201 | * inherit_task_group() | |
1202 | * inherit_group() | |
1203 | * inherit_event() | |
1204 | * perf_event_alloc() | |
1205 | * perf_init_event() | |
1206 | * perf_try_init_event() [ child , 1 ] | |
1207 | * | |
1208 | * While it appears there is an obvious deadlock here -- the parent and child | |
1209 | * nesting levels are inverted between the two. This is in fact safe because | |
1210 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1211 | * spawning task cannot (yet) exit. | |
1212 | * | |
1213 | * But remember that that these are parent<->child context relations, and | |
1214 | * migration does not affect children, therefore these two orderings should not | |
1215 | * interact. | |
f63a8daa PZ |
1216 | * |
1217 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1218 | * because the sys_perf_event_open() case will install a new event and break | |
1219 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1220 | * concerned with cpuctx and that doesn't have children. | |
1221 | * | |
1222 | * The places that change perf_event::ctx will issue: | |
1223 | * | |
1224 | * perf_remove_from_context(); | |
1225 | * synchronize_rcu(); | |
1226 | * perf_install_in_context(); | |
1227 | * | |
1228 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1229 | * quiesce the event, after which we can install it in the new location. This | |
1230 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1231 | * while in transit. Therefore all such accessors should also acquire | |
1232 | * perf_event_context::mutex to serialize against this. | |
1233 | * | |
1234 | * However; because event->ctx can change while we're waiting to acquire | |
1235 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1236 | * function. | |
1237 | * | |
1238 | * Lock order: | |
79c9ce57 | 1239 | * cred_guard_mutex |
f63a8daa PZ |
1240 | * task_struct::perf_event_mutex |
1241 | * perf_event_context::mutex | |
f63a8daa | 1242 | * perf_event::child_mutex; |
07c4a776 | 1243 | * perf_event_context::lock |
f63a8daa PZ |
1244 | * perf_event::mmap_mutex |
1245 | * mmap_sem | |
82d94856 PZ |
1246 | * |
1247 | * cpu_hotplug_lock | |
1248 | * pmus_lock | |
1249 | * cpuctx->mutex / perf_event_context::mutex | |
f63a8daa | 1250 | */ |
a83fe28e PZ |
1251 | static struct perf_event_context * |
1252 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1253 | { |
1254 | struct perf_event_context *ctx; | |
1255 | ||
1256 | again: | |
1257 | rcu_read_lock(); | |
6aa7de05 | 1258 | ctx = READ_ONCE(event->ctx); |
f63a8daa PZ |
1259 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
1260 | rcu_read_unlock(); | |
1261 | goto again; | |
1262 | } | |
1263 | rcu_read_unlock(); | |
1264 | ||
a83fe28e | 1265 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1266 | if (event->ctx != ctx) { |
1267 | mutex_unlock(&ctx->mutex); | |
1268 | put_ctx(ctx); | |
1269 | goto again; | |
1270 | } | |
1271 | ||
1272 | return ctx; | |
1273 | } | |
1274 | ||
a83fe28e PZ |
1275 | static inline struct perf_event_context * |
1276 | perf_event_ctx_lock(struct perf_event *event) | |
1277 | { | |
1278 | return perf_event_ctx_lock_nested(event, 0); | |
1279 | } | |
1280 | ||
f63a8daa PZ |
1281 | static void perf_event_ctx_unlock(struct perf_event *event, |
1282 | struct perf_event_context *ctx) | |
1283 | { | |
1284 | mutex_unlock(&ctx->mutex); | |
1285 | put_ctx(ctx); | |
1286 | } | |
1287 | ||
211de6eb PZ |
1288 | /* |
1289 | * This must be done under the ctx->lock, such as to serialize against | |
1290 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1291 | * calling scheduler related locks and ctx->lock nests inside those. | |
1292 | */ | |
1293 | static __must_check struct perf_event_context * | |
1294 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1295 | { |
211de6eb PZ |
1296 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1297 | ||
1298 | lockdep_assert_held(&ctx->lock); | |
1299 | ||
1300 | if (parent_ctx) | |
71a851b4 | 1301 | ctx->parent_ctx = NULL; |
5a3126d4 | 1302 | ctx->generation++; |
211de6eb PZ |
1303 | |
1304 | return parent_ctx; | |
71a851b4 PZ |
1305 | } |
1306 | ||
1d953111 ON |
1307 | static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, |
1308 | enum pid_type type) | |
6844c09d | 1309 | { |
1d953111 | 1310 | u32 nr; |
6844c09d ACM |
1311 | /* |
1312 | * only top level events have the pid namespace they were created in | |
1313 | */ | |
1314 | if (event->parent) | |
1315 | event = event->parent; | |
1316 | ||
1d953111 ON |
1317 | nr = __task_pid_nr_ns(p, type, event->ns); |
1318 | /* avoid -1 if it is idle thread or runs in another ns */ | |
1319 | if (!nr && !pid_alive(p)) | |
1320 | nr = -1; | |
1321 | return nr; | |
6844c09d ACM |
1322 | } |
1323 | ||
1d953111 | 1324 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
6844c09d | 1325 | { |
1d953111 ON |
1326 | return perf_event_pid_type(event, p, __PIDTYPE_TGID); |
1327 | } | |
6844c09d | 1328 | |
1d953111 ON |
1329 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
1330 | { | |
1331 | return perf_event_pid_type(event, p, PIDTYPE_PID); | |
6844c09d ACM |
1332 | } |
1333 | ||
7f453c24 | 1334 | /* |
cdd6c482 | 1335 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1336 | * to userspace. |
1337 | */ | |
cdd6c482 | 1338 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1339 | { |
cdd6c482 | 1340 | u64 id = event->id; |
7f453c24 | 1341 | |
cdd6c482 IM |
1342 | if (event->parent) |
1343 | id = event->parent->id; | |
7f453c24 PZ |
1344 | |
1345 | return id; | |
1346 | } | |
1347 | ||
25346b93 | 1348 | /* |
cdd6c482 | 1349 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1350 | * |
25346b93 PM |
1351 | * This has to cope with with the fact that until it is locked, |
1352 | * the context could get moved to another task. | |
1353 | */ | |
cdd6c482 | 1354 | static struct perf_event_context * |
8dc85d54 | 1355 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1356 | { |
cdd6c482 | 1357 | struct perf_event_context *ctx; |
25346b93 | 1358 | |
9ed6060d | 1359 | retry: |
058ebd0e PZ |
1360 | /* |
1361 | * One of the few rules of preemptible RCU is that one cannot do | |
1362 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1363 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1364 | * rcu_read_unlock_special(). |
1365 | * | |
1366 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1367 | * side critical section has interrupts disabled. |
058ebd0e | 1368 | */ |
2fd59077 | 1369 | local_irq_save(*flags); |
058ebd0e | 1370 | rcu_read_lock(); |
8dc85d54 | 1371 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1372 | if (ctx) { |
1373 | /* | |
1374 | * If this context is a clone of another, it might | |
1375 | * get swapped for another underneath us by | |
cdd6c482 | 1376 | * perf_event_task_sched_out, though the |
25346b93 PM |
1377 | * rcu_read_lock() protects us from any context |
1378 | * getting freed. Lock the context and check if it | |
1379 | * got swapped before we could get the lock, and retry | |
1380 | * if so. If we locked the right context, then it | |
1381 | * can't get swapped on us any more. | |
1382 | */ | |
2fd59077 | 1383 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1384 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1385 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1386 | rcu_read_unlock(); |
2fd59077 | 1387 | local_irq_restore(*flags); |
25346b93 PM |
1388 | goto retry; |
1389 | } | |
b49a9e7e | 1390 | |
63b6da39 PZ |
1391 | if (ctx->task == TASK_TOMBSTONE || |
1392 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1393 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1394 | ctx = NULL; |
828b6f0e PZ |
1395 | } else { |
1396 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1397 | } |
25346b93 PM |
1398 | } |
1399 | rcu_read_unlock(); | |
2fd59077 PM |
1400 | if (!ctx) |
1401 | local_irq_restore(*flags); | |
25346b93 PM |
1402 | return ctx; |
1403 | } | |
1404 | ||
1405 | /* | |
1406 | * Get the context for a task and increment its pin_count so it | |
1407 | * can't get swapped to another task. This also increments its | |
1408 | * reference count so that the context can't get freed. | |
1409 | */ | |
8dc85d54 PZ |
1410 | static struct perf_event_context * |
1411 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1412 | { |
cdd6c482 | 1413 | struct perf_event_context *ctx; |
25346b93 PM |
1414 | unsigned long flags; |
1415 | ||
8dc85d54 | 1416 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1417 | if (ctx) { |
1418 | ++ctx->pin_count; | |
e625cce1 | 1419 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1420 | } |
1421 | return ctx; | |
1422 | } | |
1423 | ||
cdd6c482 | 1424 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1425 | { |
1426 | unsigned long flags; | |
1427 | ||
e625cce1 | 1428 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1429 | --ctx->pin_count; |
e625cce1 | 1430 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1431 | } |
1432 | ||
f67218c3 PZ |
1433 | /* |
1434 | * Update the record of the current time in a context. | |
1435 | */ | |
1436 | static void update_context_time(struct perf_event_context *ctx) | |
1437 | { | |
1438 | u64 now = perf_clock(); | |
1439 | ||
1440 | ctx->time += now - ctx->timestamp; | |
1441 | ctx->timestamp = now; | |
1442 | } | |
1443 | ||
4158755d SE |
1444 | static u64 perf_event_time(struct perf_event *event) |
1445 | { | |
1446 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1447 | |
1448 | if (is_cgroup_event(event)) | |
1449 | return perf_cgroup_event_time(event); | |
1450 | ||
4158755d SE |
1451 | return ctx ? ctx->time : 0; |
1452 | } | |
1453 | ||
487f05e1 AS |
1454 | static enum event_type_t get_event_type(struct perf_event *event) |
1455 | { | |
1456 | struct perf_event_context *ctx = event->ctx; | |
1457 | enum event_type_t event_type; | |
1458 | ||
1459 | lockdep_assert_held(&ctx->lock); | |
1460 | ||
3bda69c1 AS |
1461 | /* |
1462 | * It's 'group type', really, because if our group leader is | |
1463 | * pinned, so are we. | |
1464 | */ | |
1465 | if (event->group_leader != event) | |
1466 | event = event->group_leader; | |
1467 | ||
487f05e1 AS |
1468 | event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; |
1469 | if (!ctx->task) | |
1470 | event_type |= EVENT_CPU; | |
1471 | ||
1472 | return event_type; | |
1473 | } | |
1474 | ||
8e1a2031 | 1475 | /* |
161c85fa | 1476 | * Helper function to initialize event group nodes. |
8e1a2031 | 1477 | */ |
161c85fa | 1478 | static void init_event_group(struct perf_event *event) |
8e1a2031 AB |
1479 | { |
1480 | RB_CLEAR_NODE(&event->group_node); | |
1481 | event->group_index = 0; | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * Extract pinned or flexible groups from the context | |
161c85fa | 1486 | * based on event attrs bits. |
8e1a2031 AB |
1487 | */ |
1488 | static struct perf_event_groups * | |
1489 | get_event_groups(struct perf_event *event, struct perf_event_context *ctx) | |
889ff015 FW |
1490 | { |
1491 | if (event->attr.pinned) | |
1492 | return &ctx->pinned_groups; | |
1493 | else | |
1494 | return &ctx->flexible_groups; | |
1495 | } | |
1496 | ||
8e1a2031 | 1497 | /* |
161c85fa | 1498 | * Helper function to initializes perf_event_group trees. |
8e1a2031 | 1499 | */ |
161c85fa | 1500 | static void perf_event_groups_init(struct perf_event_groups *groups) |
8e1a2031 AB |
1501 | { |
1502 | groups->tree = RB_ROOT; | |
1503 | groups->index = 0; | |
1504 | } | |
1505 | ||
1506 | /* | |
1507 | * Compare function for event groups; | |
161c85fa PZ |
1508 | * |
1509 | * Implements complex key that first sorts by CPU and then by virtual index | |
1510 | * which provides ordering when rotating groups for the same CPU. | |
8e1a2031 | 1511 | */ |
161c85fa PZ |
1512 | static bool |
1513 | perf_event_groups_less(struct perf_event *left, struct perf_event *right) | |
8e1a2031 | 1514 | { |
161c85fa PZ |
1515 | if (left->cpu < right->cpu) |
1516 | return true; | |
1517 | if (left->cpu > right->cpu) | |
1518 | return false; | |
1519 | ||
1520 | if (left->group_index < right->group_index) | |
1521 | return true; | |
1522 | if (left->group_index > right->group_index) | |
1523 | return false; | |
1524 | ||
1525 | return false; | |
8e1a2031 AB |
1526 | } |
1527 | ||
1528 | /* | |
161c85fa PZ |
1529 | * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for |
1530 | * key (see perf_event_groups_less). This places it last inside the CPU | |
1531 | * subtree. | |
8e1a2031 AB |
1532 | */ |
1533 | static void | |
1534 | perf_event_groups_insert(struct perf_event_groups *groups, | |
161c85fa | 1535 | struct perf_event *event) |
8e1a2031 AB |
1536 | { |
1537 | struct perf_event *node_event; | |
1538 | struct rb_node *parent; | |
1539 | struct rb_node **node; | |
1540 | ||
1541 | event->group_index = ++groups->index; | |
1542 | ||
1543 | node = &groups->tree.rb_node; | |
1544 | parent = *node; | |
1545 | ||
1546 | while (*node) { | |
1547 | parent = *node; | |
161c85fa | 1548 | node_event = container_of(*node, struct perf_event, group_node); |
8e1a2031 AB |
1549 | |
1550 | if (perf_event_groups_less(event, node_event)) | |
1551 | node = &parent->rb_left; | |
1552 | else | |
1553 | node = &parent->rb_right; | |
1554 | } | |
1555 | ||
1556 | rb_link_node(&event->group_node, parent, node); | |
1557 | rb_insert_color(&event->group_node, &groups->tree); | |
1558 | } | |
1559 | ||
1560 | /* | |
161c85fa | 1561 | * Helper function to insert event into the pinned or flexible groups. |
8e1a2031 AB |
1562 | */ |
1563 | static void | |
1564 | add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1565 | { | |
1566 | struct perf_event_groups *groups; | |
1567 | ||
1568 | groups = get_event_groups(event, ctx); | |
1569 | perf_event_groups_insert(groups, event); | |
1570 | } | |
1571 | ||
1572 | /* | |
161c85fa | 1573 | * Delete a group from a tree. |
8e1a2031 AB |
1574 | */ |
1575 | static void | |
1576 | perf_event_groups_delete(struct perf_event_groups *groups, | |
161c85fa | 1577 | struct perf_event *event) |
8e1a2031 | 1578 | { |
161c85fa PZ |
1579 | WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) || |
1580 | RB_EMPTY_ROOT(&groups->tree)); | |
8e1a2031 | 1581 | |
161c85fa | 1582 | rb_erase(&event->group_node, &groups->tree); |
8e1a2031 AB |
1583 | init_event_group(event); |
1584 | } | |
1585 | ||
1586 | /* | |
161c85fa | 1587 | * Helper function to delete event from its groups. |
8e1a2031 AB |
1588 | */ |
1589 | static void | |
1590 | del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1591 | { | |
1592 | struct perf_event_groups *groups; | |
1593 | ||
1594 | groups = get_event_groups(event, ctx); | |
1595 | perf_event_groups_delete(groups, event); | |
1596 | } | |
1597 | ||
1598 | /* | |
161c85fa | 1599 | * Get the leftmost event in the @cpu subtree. |
8e1a2031 AB |
1600 | */ |
1601 | static struct perf_event * | |
1602 | perf_event_groups_first(struct perf_event_groups *groups, int cpu) | |
1603 | { | |
1604 | struct perf_event *node_event = NULL, *match = NULL; | |
1605 | struct rb_node *node = groups->tree.rb_node; | |
1606 | ||
1607 | while (node) { | |
161c85fa | 1608 | node_event = container_of(node, struct perf_event, group_node); |
8e1a2031 AB |
1609 | |
1610 | if (cpu < node_event->cpu) { | |
1611 | node = node->rb_left; | |
1612 | } else if (cpu > node_event->cpu) { | |
1613 | node = node->rb_right; | |
1614 | } else { | |
1615 | match = node_event; | |
1616 | node = node->rb_left; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | return match; | |
1621 | } | |
1622 | ||
1cac7b1a PZ |
1623 | /* |
1624 | * Like rb_entry_next_safe() for the @cpu subtree. | |
1625 | */ | |
1626 | static struct perf_event * | |
1627 | perf_event_groups_next(struct perf_event *event) | |
1628 | { | |
1629 | struct perf_event *next; | |
1630 | ||
1631 | next = rb_entry_safe(rb_next(&event->group_node), typeof(*event), group_node); | |
1632 | if (next && next->cpu == event->cpu) | |
1633 | return next; | |
1634 | ||
1635 | return NULL; | |
1636 | } | |
1637 | ||
8e1a2031 | 1638 | /* |
161c85fa | 1639 | * Iterate through the whole groups tree. |
8e1a2031 | 1640 | */ |
6e6804d2 PZ |
1641 | #define perf_event_groups_for_each(event, groups) \ |
1642 | for (event = rb_entry_safe(rb_first(&((groups)->tree)), \ | |
1643 | typeof(*event), group_node); event; \ | |
1644 | event = rb_entry_safe(rb_next(&event->group_node), \ | |
1645 | typeof(*event), group_node)) | |
8e1a2031 | 1646 | |
fccc714b | 1647 | /* |
cdd6c482 | 1648 | * Add a event from the lists for its context. |
fccc714b PZ |
1649 | * Must be called with ctx->mutex and ctx->lock held. |
1650 | */ | |
04289bb9 | 1651 | static void |
cdd6c482 | 1652 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1653 | { |
c994d613 PZ |
1654 | lockdep_assert_held(&ctx->lock); |
1655 | ||
8a49542c PZ |
1656 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1657 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 | 1658 | |
0d3d73aa PZ |
1659 | event->tstamp = perf_event_time(event); |
1660 | ||
04289bb9 | 1661 | /* |
8a49542c PZ |
1662 | * If we're a stand alone event or group leader, we go to the context |
1663 | * list, group events are kept attached to the group so that | |
1664 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1665 | */ |
8a49542c | 1666 | if (event->group_leader == event) { |
4ff6a8de | 1667 | event->group_caps = event->event_caps; |
8e1a2031 | 1668 | add_event_to_groups(event, ctx); |
5c148194 | 1669 | } |
592903cd | 1670 | |
db4a8356 | 1671 | list_update_cgroup_event(event, ctx, true); |
e5d1367f | 1672 | |
cdd6c482 IM |
1673 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1674 | ctx->nr_events++; | |
1675 | if (event->attr.inherit_stat) | |
bfbd3381 | 1676 | ctx->nr_stat++; |
5a3126d4 PZ |
1677 | |
1678 | ctx->generation++; | |
04289bb9 IM |
1679 | } |
1680 | ||
0231bb53 JO |
1681 | /* |
1682 | * Initialize event state based on the perf_event_attr::disabled. | |
1683 | */ | |
1684 | static inline void perf_event__state_init(struct perf_event *event) | |
1685 | { | |
1686 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1687 | PERF_EVENT_STATE_INACTIVE; | |
1688 | } | |
1689 | ||
a723968c | 1690 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1691 | { |
1692 | int entry = sizeof(u64); /* value */ | |
1693 | int size = 0; | |
1694 | int nr = 1; | |
1695 | ||
1696 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1697 | size += sizeof(u64); | |
1698 | ||
1699 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1700 | size += sizeof(u64); | |
1701 | ||
1702 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1703 | entry += sizeof(u64); | |
1704 | ||
1705 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1706 | nr += nr_siblings; |
c320c7b7 ACM |
1707 | size += sizeof(u64); |
1708 | } | |
1709 | ||
1710 | size += entry * nr; | |
1711 | event->read_size = size; | |
1712 | } | |
1713 | ||
a723968c | 1714 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1715 | { |
1716 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1717 | u16 size = 0; |
1718 | ||
c320c7b7 ACM |
1719 | if (sample_type & PERF_SAMPLE_IP) |
1720 | size += sizeof(data->ip); | |
1721 | ||
6844c09d ACM |
1722 | if (sample_type & PERF_SAMPLE_ADDR) |
1723 | size += sizeof(data->addr); | |
1724 | ||
1725 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1726 | size += sizeof(data->period); | |
1727 | ||
c3feedf2 AK |
1728 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1729 | size += sizeof(data->weight); | |
1730 | ||
6844c09d ACM |
1731 | if (sample_type & PERF_SAMPLE_READ) |
1732 | size += event->read_size; | |
1733 | ||
d6be9ad6 SE |
1734 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1735 | size += sizeof(data->data_src.val); | |
1736 | ||
fdfbbd07 AK |
1737 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1738 | size += sizeof(data->txn); | |
1739 | ||
fc7ce9c7 KL |
1740 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
1741 | size += sizeof(data->phys_addr); | |
1742 | ||
6844c09d ACM |
1743 | event->header_size = size; |
1744 | } | |
1745 | ||
a723968c PZ |
1746 | /* |
1747 | * Called at perf_event creation and when events are attached/detached from a | |
1748 | * group. | |
1749 | */ | |
1750 | static void perf_event__header_size(struct perf_event *event) | |
1751 | { | |
1752 | __perf_event_read_size(event, | |
1753 | event->group_leader->nr_siblings); | |
1754 | __perf_event_header_size(event, event->attr.sample_type); | |
1755 | } | |
1756 | ||
6844c09d ACM |
1757 | static void perf_event__id_header_size(struct perf_event *event) |
1758 | { | |
1759 | struct perf_sample_data *data; | |
1760 | u64 sample_type = event->attr.sample_type; | |
1761 | u16 size = 0; | |
1762 | ||
c320c7b7 ACM |
1763 | if (sample_type & PERF_SAMPLE_TID) |
1764 | size += sizeof(data->tid_entry); | |
1765 | ||
1766 | if (sample_type & PERF_SAMPLE_TIME) | |
1767 | size += sizeof(data->time); | |
1768 | ||
ff3d527c AH |
1769 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1770 | size += sizeof(data->id); | |
1771 | ||
c320c7b7 ACM |
1772 | if (sample_type & PERF_SAMPLE_ID) |
1773 | size += sizeof(data->id); | |
1774 | ||
1775 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1776 | size += sizeof(data->stream_id); | |
1777 | ||
1778 | if (sample_type & PERF_SAMPLE_CPU) | |
1779 | size += sizeof(data->cpu_entry); | |
1780 | ||
6844c09d | 1781 | event->id_header_size = size; |
c320c7b7 ACM |
1782 | } |
1783 | ||
a723968c PZ |
1784 | static bool perf_event_validate_size(struct perf_event *event) |
1785 | { | |
1786 | /* | |
1787 | * The values computed here will be over-written when we actually | |
1788 | * attach the event. | |
1789 | */ | |
1790 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1791 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1792 | perf_event__id_header_size(event); | |
1793 | ||
1794 | /* | |
1795 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1796 | * Conservative limit to allow for callchains and other variable fields. | |
1797 | */ | |
1798 | if (event->read_size + event->header_size + | |
1799 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1800 | return false; | |
1801 | ||
1802 | return true; | |
1803 | } | |
1804 | ||
8a49542c PZ |
1805 | static void perf_group_attach(struct perf_event *event) |
1806 | { | |
c320c7b7 | 1807 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1808 | |
a76a82a3 PZ |
1809 | lockdep_assert_held(&event->ctx->lock); |
1810 | ||
74c3337c PZ |
1811 | /* |
1812 | * We can have double attach due to group movement in perf_event_open. | |
1813 | */ | |
1814 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1815 | return; | |
1816 | ||
8a49542c PZ |
1817 | event->attach_state |= PERF_ATTACH_GROUP; |
1818 | ||
1819 | if (group_leader == event) | |
1820 | return; | |
1821 | ||
652884fe PZ |
1822 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1823 | ||
4ff6a8de | 1824 | group_leader->group_caps &= event->event_caps; |
8a49542c | 1825 | |
8343aae6 | 1826 | list_add_tail(&event->sibling_list, &group_leader->sibling_list); |
8a49542c | 1827 | group_leader->nr_siblings++; |
c320c7b7 ACM |
1828 | |
1829 | perf_event__header_size(group_leader); | |
1830 | ||
8343aae6 | 1831 | list_for_each_entry(pos, &group_leader->sibling_list, sibling_list) |
c320c7b7 | 1832 | perf_event__header_size(pos); |
8a49542c PZ |
1833 | } |
1834 | ||
a63eaf34 | 1835 | /* |
cdd6c482 | 1836 | * Remove a event from the lists for its context. |
fccc714b | 1837 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1838 | */ |
04289bb9 | 1839 | static void |
cdd6c482 | 1840 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1841 | { |
652884fe PZ |
1842 | WARN_ON_ONCE(event->ctx != ctx); |
1843 | lockdep_assert_held(&ctx->lock); | |
1844 | ||
8a49542c PZ |
1845 | /* |
1846 | * We can have double detach due to exit/hot-unplug + close. | |
1847 | */ | |
1848 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1849 | return; |
8a49542c PZ |
1850 | |
1851 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1852 | ||
db4a8356 | 1853 | list_update_cgroup_event(event, ctx, false); |
e5d1367f | 1854 | |
cdd6c482 IM |
1855 | ctx->nr_events--; |
1856 | if (event->attr.inherit_stat) | |
bfbd3381 | 1857 | ctx->nr_stat--; |
8bc20959 | 1858 | |
cdd6c482 | 1859 | list_del_rcu(&event->event_entry); |
04289bb9 | 1860 | |
8a49542c | 1861 | if (event->group_leader == event) |
8e1a2031 | 1862 | del_event_from_groups(event, ctx); |
5c148194 | 1863 | |
b2e74a26 SE |
1864 | /* |
1865 | * If event was in error state, then keep it | |
1866 | * that way, otherwise bogus counts will be | |
1867 | * returned on read(). The only way to get out | |
1868 | * of error state is by explicit re-enabling | |
1869 | * of the event | |
1870 | */ | |
1871 | if (event->state > PERF_EVENT_STATE_OFF) | |
0d3d73aa | 1872 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); |
5a3126d4 PZ |
1873 | |
1874 | ctx->generation++; | |
050735b0 PZ |
1875 | } |
1876 | ||
8a49542c | 1877 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1878 | { |
1879 | struct perf_event *sibling, *tmp; | |
6668128a | 1880 | struct perf_event_context *ctx = event->ctx; |
8a49542c | 1881 | |
6668128a | 1882 | lockdep_assert_held(&ctx->lock); |
a76a82a3 | 1883 | |
8a49542c PZ |
1884 | /* |
1885 | * We can have double detach due to exit/hot-unplug + close. | |
1886 | */ | |
1887 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1888 | return; | |
1889 | ||
1890 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1891 | ||
1892 | /* | |
1893 | * If this is a sibling, remove it from its group. | |
1894 | */ | |
1895 | if (event->group_leader != event) { | |
8343aae6 | 1896 | list_del_init(&event->sibling_list); |
8a49542c | 1897 | event->group_leader->nr_siblings--; |
c320c7b7 | 1898 | goto out; |
8a49542c PZ |
1899 | } |
1900 | ||
04289bb9 | 1901 | /* |
cdd6c482 IM |
1902 | * If this was a group event with sibling events then |
1903 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1904 | * to whatever list we are on. |
04289bb9 | 1905 | */ |
8343aae6 | 1906 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { |
8e1a2031 | 1907 | |
04289bb9 | 1908 | sibling->group_leader = sibling; |
d6f962b5 FW |
1909 | |
1910 | /* Inherit group flags from the previous leader */ | |
4ff6a8de | 1911 | sibling->group_caps = event->group_caps; |
652884fe | 1912 | |
8e1a2031 | 1913 | if (!RB_EMPTY_NODE(&event->group_node)) { |
8343aae6 | 1914 | list_del_init(&sibling->sibling_list); |
8e1a2031 | 1915 | add_event_to_groups(sibling, event->ctx); |
6668128a PZ |
1916 | |
1917 | if (sibling->state == PERF_EVENT_STATE_ACTIVE) { | |
1918 | struct list_head *list = sibling->attr.pinned ? | |
1919 | &ctx->pinned_active : &ctx->flexible_active; | |
1920 | ||
1921 | list_add_tail(&sibling->active_list, list); | |
1922 | } | |
8e1a2031 AB |
1923 | } |
1924 | ||
652884fe | 1925 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
04289bb9 | 1926 | } |
c320c7b7 ACM |
1927 | |
1928 | out: | |
1929 | perf_event__header_size(event->group_leader); | |
1930 | ||
8343aae6 | 1931 | list_for_each_entry(tmp, &event->group_leader->sibling_list, sibling_list) |
c320c7b7 | 1932 | perf_event__header_size(tmp); |
04289bb9 IM |
1933 | } |
1934 | ||
fadfe7be JO |
1935 | static bool is_orphaned_event(struct perf_event *event) |
1936 | { | |
a69b0ca4 | 1937 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1938 | } |
1939 | ||
2c81a647 | 1940 | static inline int __pmu_filter_match(struct perf_event *event) |
66eb579e MR |
1941 | { |
1942 | struct pmu *pmu = event->pmu; | |
1943 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1944 | } | |
1945 | ||
2c81a647 MR |
1946 | /* |
1947 | * Check whether we should attempt to schedule an event group based on | |
1948 | * PMU-specific filtering. An event group can consist of HW and SW events, | |
1949 | * potentially with a SW leader, so we must check all the filters, to | |
1950 | * determine whether a group is schedulable: | |
1951 | */ | |
1952 | static inline int pmu_filter_match(struct perf_event *event) | |
1953 | { | |
1954 | struct perf_event *child; | |
1955 | ||
1956 | if (!__pmu_filter_match(event)) | |
1957 | return 0; | |
1958 | ||
8343aae6 | 1959 | list_for_each_entry(child, &event->sibling_list, sibling_list) { |
2c81a647 MR |
1960 | if (!__pmu_filter_match(child)) |
1961 | return 0; | |
1962 | } | |
1963 | ||
1964 | return 1; | |
1965 | } | |
1966 | ||
fa66f07a SE |
1967 | static inline int |
1968 | event_filter_match(struct perf_event *event) | |
1969 | { | |
0b8f1e2e PZ |
1970 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
1971 | perf_cgroup_match(event) && pmu_filter_match(event); | |
fa66f07a SE |
1972 | } |
1973 | ||
9ffcfa6f SE |
1974 | static void |
1975 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1976 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1977 | struct perf_event_context *ctx) |
3b6f9e5c | 1978 | { |
0d3d73aa | 1979 | enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; |
652884fe PZ |
1980 | |
1981 | WARN_ON_ONCE(event->ctx != ctx); | |
1982 | lockdep_assert_held(&ctx->lock); | |
1983 | ||
cdd6c482 | 1984 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1985 | return; |
3b6f9e5c | 1986 | |
6668128a PZ |
1987 | /* |
1988 | * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but | |
1989 | * we can schedule events _OUT_ individually through things like | |
1990 | * __perf_remove_from_context(). | |
1991 | */ | |
1992 | list_del_init(&event->active_list); | |
1993 | ||
44377277 AS |
1994 | perf_pmu_disable(event->pmu); |
1995 | ||
28a967c3 PZ |
1996 | event->pmu->del(event, 0); |
1997 | event->oncpu = -1; | |
0d3d73aa | 1998 | |
cdd6c482 IM |
1999 | if (event->pending_disable) { |
2000 | event->pending_disable = 0; | |
0d3d73aa | 2001 | state = PERF_EVENT_STATE_OFF; |
970892a9 | 2002 | } |
0d3d73aa | 2003 | perf_event_set_state(event, state); |
3b6f9e5c | 2004 | |
cdd6c482 | 2005 | if (!is_software_event(event)) |
3b6f9e5c | 2006 | cpuctx->active_oncpu--; |
2fde4f94 MR |
2007 | if (!--ctx->nr_active) |
2008 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
2009 | if (event->attr.freq && event->attr.sample_freq) |
2010 | ctx->nr_freq--; | |
cdd6c482 | 2011 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 2012 | cpuctx->exclusive = 0; |
44377277 AS |
2013 | |
2014 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
2015 | } |
2016 | ||
d859e29f | 2017 | static void |
cdd6c482 | 2018 | group_sched_out(struct perf_event *group_event, |
d859e29f | 2019 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 2020 | struct perf_event_context *ctx) |
d859e29f | 2021 | { |
cdd6c482 | 2022 | struct perf_event *event; |
0d3d73aa PZ |
2023 | |
2024 | if (group_event->state != PERF_EVENT_STATE_ACTIVE) | |
2025 | return; | |
d859e29f | 2026 | |
3f005e7d MR |
2027 | perf_pmu_disable(ctx->pmu); |
2028 | ||
cdd6c482 | 2029 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
2030 | |
2031 | /* | |
2032 | * Schedule out siblings (if any): | |
2033 | */ | |
8343aae6 | 2034 | list_for_each_entry(event, &group_event->sibling_list, sibling_list) |
cdd6c482 | 2035 | event_sched_out(event, cpuctx, ctx); |
d859e29f | 2036 | |
3f005e7d MR |
2037 | perf_pmu_enable(ctx->pmu); |
2038 | ||
0d3d73aa | 2039 | if (group_event->attr.exclusive) |
d859e29f PM |
2040 | cpuctx->exclusive = 0; |
2041 | } | |
2042 | ||
45a0e07a | 2043 | #define DETACH_GROUP 0x01UL |
0017960f | 2044 | |
0793a61d | 2045 | /* |
cdd6c482 | 2046 | * Cross CPU call to remove a performance event |
0793a61d | 2047 | * |
cdd6c482 | 2048 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
2049 | * remove it from the context list. |
2050 | */ | |
fae3fde6 PZ |
2051 | static void |
2052 | __perf_remove_from_context(struct perf_event *event, | |
2053 | struct perf_cpu_context *cpuctx, | |
2054 | struct perf_event_context *ctx, | |
2055 | void *info) | |
0793a61d | 2056 | { |
45a0e07a | 2057 | unsigned long flags = (unsigned long)info; |
0793a61d | 2058 | |
3c5c8711 PZ |
2059 | if (ctx->is_active & EVENT_TIME) { |
2060 | update_context_time(ctx); | |
2061 | update_cgrp_time_from_cpuctx(cpuctx); | |
2062 | } | |
2063 | ||
cdd6c482 | 2064 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 2065 | if (flags & DETACH_GROUP) |
46ce0fe9 | 2066 | perf_group_detach(event); |
cdd6c482 | 2067 | list_del_event(event, ctx); |
39a43640 PZ |
2068 | |
2069 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 2070 | ctx->is_active = 0; |
39a43640 PZ |
2071 | if (ctx->task) { |
2072 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2073 | cpuctx->task_ctx = NULL; | |
2074 | } | |
64ce3126 | 2075 | } |
0793a61d TG |
2076 | } |
2077 | ||
0793a61d | 2078 | /* |
cdd6c482 | 2079 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 2080 | * |
cdd6c482 IM |
2081 | * If event->ctx is a cloned context, callers must make sure that |
2082 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
2083 | * remains valid. This is OK when called from perf_release since |
2084 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 2085 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 2086 | * context has been detached from its task. |
0793a61d | 2087 | */ |
45a0e07a | 2088 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 2089 | { |
a76a82a3 PZ |
2090 | struct perf_event_context *ctx = event->ctx; |
2091 | ||
2092 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 2093 | |
45a0e07a | 2094 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
a76a82a3 PZ |
2095 | |
2096 | /* | |
2097 | * The above event_function_call() can NO-OP when it hits | |
2098 | * TASK_TOMBSTONE. In that case we must already have been detached | |
2099 | * from the context (by perf_event_exit_event()) but the grouping | |
2100 | * might still be in-tact. | |
2101 | */ | |
2102 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); | |
2103 | if ((flags & DETACH_GROUP) && | |
2104 | (event->attach_state & PERF_ATTACH_GROUP)) { | |
2105 | /* | |
2106 | * Since in that case we cannot possibly be scheduled, simply | |
2107 | * detach now. | |
2108 | */ | |
2109 | raw_spin_lock_irq(&ctx->lock); | |
2110 | perf_group_detach(event); | |
2111 | raw_spin_unlock_irq(&ctx->lock); | |
2112 | } | |
0793a61d TG |
2113 | } |
2114 | ||
d859e29f | 2115 | /* |
cdd6c482 | 2116 | * Cross CPU call to disable a performance event |
d859e29f | 2117 | */ |
fae3fde6 PZ |
2118 | static void __perf_event_disable(struct perf_event *event, |
2119 | struct perf_cpu_context *cpuctx, | |
2120 | struct perf_event_context *ctx, | |
2121 | void *info) | |
7b648018 | 2122 | { |
fae3fde6 PZ |
2123 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
2124 | return; | |
7b648018 | 2125 | |
3c5c8711 PZ |
2126 | if (ctx->is_active & EVENT_TIME) { |
2127 | update_context_time(ctx); | |
2128 | update_cgrp_time_from_event(event); | |
2129 | } | |
2130 | ||
fae3fde6 PZ |
2131 | if (event == event->group_leader) |
2132 | group_sched_out(event, cpuctx, ctx); | |
2133 | else | |
2134 | event_sched_out(event, cpuctx, ctx); | |
0d3d73aa PZ |
2135 | |
2136 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); | |
7b648018 PZ |
2137 | } |
2138 | ||
d859e29f | 2139 | /* |
cdd6c482 | 2140 | * Disable a event. |
c93f7669 | 2141 | * |
cdd6c482 IM |
2142 | * If event->ctx is a cloned context, callers must make sure that |
2143 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2144 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
2145 | * perf_event_for_each_child or perf_event_for_each because they |
2146 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
2147 | * goes to exit will block in perf_event_exit_event(). |
2148 | * | |
cdd6c482 | 2149 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 2150 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 2151 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 2152 | */ |
f63a8daa | 2153 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 2154 | { |
cdd6c482 | 2155 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2156 | |
e625cce1 | 2157 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 2158 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 2159 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2160 | return; |
53cfbf59 | 2161 | } |
e625cce1 | 2162 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2163 | |
fae3fde6 PZ |
2164 | event_function_call(event, __perf_event_disable, NULL); |
2165 | } | |
2166 | ||
2167 | void perf_event_disable_local(struct perf_event *event) | |
2168 | { | |
2169 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 2170 | } |
f63a8daa PZ |
2171 | |
2172 | /* | |
2173 | * Strictly speaking kernel users cannot create groups and therefore this | |
2174 | * interface does not need the perf_event_ctx_lock() magic. | |
2175 | */ | |
2176 | void perf_event_disable(struct perf_event *event) | |
2177 | { | |
2178 | struct perf_event_context *ctx; | |
2179 | ||
2180 | ctx = perf_event_ctx_lock(event); | |
2181 | _perf_event_disable(event); | |
2182 | perf_event_ctx_unlock(event, ctx); | |
2183 | } | |
dcfce4a0 | 2184 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2185 | |
5aab90ce JO |
2186 | void perf_event_disable_inatomic(struct perf_event *event) |
2187 | { | |
2188 | event->pending_disable = 1; | |
2189 | irq_work_queue(&event->pending); | |
2190 | } | |
2191 | ||
e5d1367f | 2192 | static void perf_set_shadow_time(struct perf_event *event, |
0d3d73aa | 2193 | struct perf_event_context *ctx) |
e5d1367f SE |
2194 | { |
2195 | /* | |
2196 | * use the correct time source for the time snapshot | |
2197 | * | |
2198 | * We could get by without this by leveraging the | |
2199 | * fact that to get to this function, the caller | |
2200 | * has most likely already called update_context_time() | |
2201 | * and update_cgrp_time_xx() and thus both timestamp | |
2202 | * are identical (or very close). Given that tstamp is, | |
2203 | * already adjusted for cgroup, we could say that: | |
2204 | * tstamp - ctx->timestamp | |
2205 | * is equivalent to | |
2206 | * tstamp - cgrp->timestamp. | |
2207 | * | |
2208 | * Then, in perf_output_read(), the calculation would | |
2209 | * work with no changes because: | |
2210 | * - event is guaranteed scheduled in | |
2211 | * - no scheduled out in between | |
2212 | * - thus the timestamp would be the same | |
2213 | * | |
2214 | * But this is a bit hairy. | |
2215 | * | |
2216 | * So instead, we have an explicit cgroup call to remain | |
2217 | * within the time time source all along. We believe it | |
2218 | * is cleaner and simpler to understand. | |
2219 | */ | |
2220 | if (is_cgroup_event(event)) | |
0d3d73aa | 2221 | perf_cgroup_set_shadow_time(event, event->tstamp); |
e5d1367f | 2222 | else |
0d3d73aa | 2223 | event->shadow_ctx_time = event->tstamp - ctx->timestamp; |
e5d1367f SE |
2224 | } |
2225 | ||
4fe757dd PZ |
2226 | #define MAX_INTERRUPTS (~0ULL) |
2227 | ||
2228 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2229 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2230 | |
235c7fc7 | 2231 | static int |
9ffcfa6f | 2232 | event_sched_in(struct perf_event *event, |
235c7fc7 | 2233 | struct perf_cpu_context *cpuctx, |
6e37738a | 2234 | struct perf_event_context *ctx) |
235c7fc7 | 2235 | { |
44377277 | 2236 | int ret = 0; |
4158755d | 2237 | |
63342411 PZ |
2238 | lockdep_assert_held(&ctx->lock); |
2239 | ||
cdd6c482 | 2240 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2241 | return 0; |
2242 | ||
95ff4ca2 AS |
2243 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2244 | /* | |
0c1cbc18 PZ |
2245 | * Order event::oncpu write to happen before the ACTIVE state is |
2246 | * visible. This allows perf_event_{stop,read}() to observe the correct | |
2247 | * ->oncpu if it sees ACTIVE. | |
95ff4ca2 AS |
2248 | */ |
2249 | smp_wmb(); | |
0d3d73aa | 2250 | perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); |
4fe757dd PZ |
2251 | |
2252 | /* | |
2253 | * Unthrottle events, since we scheduled we might have missed several | |
2254 | * ticks already, also for a heavily scheduling task there is little | |
2255 | * guarantee it'll get a tick in a timely manner. | |
2256 | */ | |
2257 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2258 | perf_log_throttle(event, 1); | |
2259 | event->hw.interrupts = 0; | |
2260 | } | |
2261 | ||
44377277 AS |
2262 | perf_pmu_disable(event->pmu); |
2263 | ||
0d3d73aa | 2264 | perf_set_shadow_time(event, ctx); |
72f669c0 | 2265 | |
ec0d7729 AS |
2266 | perf_log_itrace_start(event); |
2267 | ||
a4eaf7f1 | 2268 | if (event->pmu->add(event, PERF_EF_START)) { |
0d3d73aa | 2269 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
cdd6c482 | 2270 | event->oncpu = -1; |
44377277 AS |
2271 | ret = -EAGAIN; |
2272 | goto out; | |
235c7fc7 IM |
2273 | } |
2274 | ||
cdd6c482 | 2275 | if (!is_software_event(event)) |
3b6f9e5c | 2276 | cpuctx->active_oncpu++; |
2fde4f94 MR |
2277 | if (!ctx->nr_active++) |
2278 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
2279 | if (event->attr.freq && event->attr.sample_freq) |
2280 | ctx->nr_freq++; | |
235c7fc7 | 2281 | |
cdd6c482 | 2282 | if (event->attr.exclusive) |
3b6f9e5c PM |
2283 | cpuctx->exclusive = 1; |
2284 | ||
44377277 AS |
2285 | out: |
2286 | perf_pmu_enable(event->pmu); | |
2287 | ||
2288 | return ret; | |
235c7fc7 IM |
2289 | } |
2290 | ||
6751b71e | 2291 | static int |
cdd6c482 | 2292 | group_sched_in(struct perf_event *group_event, |
6751b71e | 2293 | struct perf_cpu_context *cpuctx, |
6e37738a | 2294 | struct perf_event_context *ctx) |
6751b71e | 2295 | { |
6bde9b6c | 2296 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 2297 | struct pmu *pmu = ctx->pmu; |
6751b71e | 2298 | |
cdd6c482 | 2299 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2300 | return 0; |
2301 | ||
fbbe0701 | 2302 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2303 | |
9ffcfa6f | 2304 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2305 | pmu->cancel_txn(pmu); |
272325c4 | 2306 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2307 | return -EAGAIN; |
90151c35 | 2308 | } |
6751b71e PM |
2309 | |
2310 | /* | |
2311 | * Schedule in siblings as one group (if any): | |
2312 | */ | |
8343aae6 | 2313 | list_for_each_entry(event, &group_event->sibling_list, sibling_list) { |
9ffcfa6f | 2314 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2315 | partial_group = event; |
6751b71e PM |
2316 | goto group_error; |
2317 | } | |
2318 | } | |
2319 | ||
9ffcfa6f | 2320 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2321 | return 0; |
9ffcfa6f | 2322 | |
6751b71e PM |
2323 | group_error: |
2324 | /* | |
2325 | * Groups can be scheduled in as one unit only, so undo any | |
2326 | * partial group before returning: | |
0d3d73aa | 2327 | * The events up to the failed event are scheduled out normally. |
6751b71e | 2328 | */ |
8343aae6 | 2329 | list_for_each_entry(event, &group_event->sibling_list, sibling_list) { |
cdd6c482 | 2330 | if (event == partial_group) |
0d3d73aa | 2331 | break; |
d7842da4 | 2332 | |
0d3d73aa | 2333 | event_sched_out(event, cpuctx, ctx); |
6751b71e | 2334 | } |
9ffcfa6f | 2335 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2336 | |
ad5133b7 | 2337 | pmu->cancel_txn(pmu); |
90151c35 | 2338 | |
272325c4 | 2339 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2340 | |
6751b71e PM |
2341 | return -EAGAIN; |
2342 | } | |
2343 | ||
3b6f9e5c | 2344 | /* |
cdd6c482 | 2345 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2346 | */ |
cdd6c482 | 2347 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2348 | struct perf_cpu_context *cpuctx, |
2349 | int can_add_hw) | |
2350 | { | |
2351 | /* | |
cdd6c482 | 2352 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2353 | */ |
4ff6a8de | 2354 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2355 | return 1; |
2356 | /* | |
2357 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2358 | * events can go on. |
3b6f9e5c PM |
2359 | */ |
2360 | if (cpuctx->exclusive) | |
2361 | return 0; | |
2362 | /* | |
2363 | * If this group is exclusive and there are already | |
cdd6c482 | 2364 | * events on the CPU, it can't go on. |
3b6f9e5c | 2365 | */ |
cdd6c482 | 2366 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2367 | return 0; |
2368 | /* | |
2369 | * Otherwise, try to add it if all previous groups were able | |
2370 | * to go on. | |
2371 | */ | |
2372 | return can_add_hw; | |
2373 | } | |
2374 | ||
cdd6c482 IM |
2375 | static void add_event_to_ctx(struct perf_event *event, |
2376 | struct perf_event_context *ctx) | |
53cfbf59 | 2377 | { |
cdd6c482 | 2378 | list_add_event(event, ctx); |
8a49542c | 2379 | perf_group_attach(event); |
53cfbf59 PM |
2380 | } |
2381 | ||
bd2afa49 PZ |
2382 | static void ctx_sched_out(struct perf_event_context *ctx, |
2383 | struct perf_cpu_context *cpuctx, | |
2384 | enum event_type_t event_type); | |
2c29ef0f PZ |
2385 | static void |
2386 | ctx_sched_in(struct perf_event_context *ctx, | |
2387 | struct perf_cpu_context *cpuctx, | |
2388 | enum event_type_t event_type, | |
2389 | struct task_struct *task); | |
fe4b04fa | 2390 | |
bd2afa49 | 2391 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2392 | struct perf_event_context *ctx, |
2393 | enum event_type_t event_type) | |
bd2afa49 PZ |
2394 | { |
2395 | if (!cpuctx->task_ctx) | |
2396 | return; | |
2397 | ||
2398 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2399 | return; | |
2400 | ||
487f05e1 | 2401 | ctx_sched_out(ctx, cpuctx, event_type); |
bd2afa49 PZ |
2402 | } |
2403 | ||
dce5855b PZ |
2404 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2405 | struct perf_event_context *ctx, | |
2406 | struct task_struct *task) | |
2407 | { | |
2408 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2409 | if (ctx) | |
2410 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2411 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2412 | if (ctx) | |
2413 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2414 | } | |
2415 | ||
487f05e1 AS |
2416 | /* |
2417 | * We want to maintain the following priority of scheduling: | |
2418 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2419 | * - task pinned (EVENT_PINNED) | |
2420 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2421 | * - task flexible (EVENT_FLEXIBLE). | |
2422 | * | |
2423 | * In order to avoid unscheduling and scheduling back in everything every | |
2424 | * time an event is added, only do it for the groups of equal priority and | |
2425 | * below. | |
2426 | * | |
2427 | * This can be called after a batch operation on task events, in which case | |
2428 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2429 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2430 | */ | |
3e349507 | 2431 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2432 | struct perf_event_context *task_ctx, |
2433 | enum event_type_t event_type) | |
0017960f | 2434 | { |
487f05e1 AS |
2435 | enum event_type_t ctx_event_type = event_type & EVENT_ALL; |
2436 | bool cpu_event = !!(event_type & EVENT_CPU); | |
2437 | ||
2438 | /* | |
2439 | * If pinned groups are involved, flexible groups also need to be | |
2440 | * scheduled out. | |
2441 | */ | |
2442 | if (event_type & EVENT_PINNED) | |
2443 | event_type |= EVENT_FLEXIBLE; | |
2444 | ||
3e349507 PZ |
2445 | perf_pmu_disable(cpuctx->ctx.pmu); |
2446 | if (task_ctx) | |
487f05e1 AS |
2447 | task_ctx_sched_out(cpuctx, task_ctx, event_type); |
2448 | ||
2449 | /* | |
2450 | * Decide which cpu ctx groups to schedule out based on the types | |
2451 | * of events that caused rescheduling: | |
2452 | * - EVENT_CPU: schedule out corresponding groups; | |
2453 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2454 | * - otherwise, do nothing more. | |
2455 | */ | |
2456 | if (cpu_event) | |
2457 | cpu_ctx_sched_out(cpuctx, ctx_event_type); | |
2458 | else if (ctx_event_type & EVENT_PINNED) | |
2459 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
2460 | ||
3e349507 PZ |
2461 | perf_event_sched_in(cpuctx, task_ctx, current); |
2462 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2463 | } |
2464 | ||
0793a61d | 2465 | /* |
cdd6c482 | 2466 | * Cross CPU call to install and enable a performance event |
682076ae | 2467 | * |
a096309b PZ |
2468 | * Very similar to remote_function() + event_function() but cannot assume that |
2469 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2470 | */ |
fe4b04fa | 2471 | static int __perf_install_in_context(void *info) |
0793a61d | 2472 | { |
a096309b PZ |
2473 | struct perf_event *event = info; |
2474 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2475 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2476 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2477 | bool reprogram = true; |
a096309b | 2478 | int ret = 0; |
0793a61d | 2479 | |
63b6da39 | 2480 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2481 | if (ctx->task) { |
b58f6b0d PZ |
2482 | raw_spin_lock(&ctx->lock); |
2483 | task_ctx = ctx; | |
a096309b | 2484 | |
63cae12b | 2485 | reprogram = (ctx->task == current); |
b58f6b0d | 2486 | |
39a43640 | 2487 | /* |
63cae12b PZ |
2488 | * If the task is running, it must be running on this CPU, |
2489 | * otherwise we cannot reprogram things. | |
2490 | * | |
2491 | * If its not running, we don't care, ctx->lock will | |
2492 | * serialize against it becoming runnable. | |
39a43640 | 2493 | */ |
63cae12b PZ |
2494 | if (task_curr(ctx->task) && !reprogram) { |
2495 | ret = -ESRCH; | |
2496 | goto unlock; | |
2497 | } | |
a096309b | 2498 | |
63cae12b | 2499 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2500 | } else if (task_ctx) { |
2501 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2502 | } |
b58f6b0d | 2503 | |
33801b94 | 2504 | #ifdef CONFIG_CGROUP_PERF |
2505 | if (is_cgroup_event(event)) { | |
2506 | /* | |
2507 | * If the current cgroup doesn't match the event's | |
2508 | * cgroup, we should not try to schedule it. | |
2509 | */ | |
2510 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); | |
2511 | reprogram = cgroup_is_descendant(cgrp->css.cgroup, | |
2512 | event->cgrp->css.cgroup); | |
2513 | } | |
2514 | #endif | |
2515 | ||
63cae12b | 2516 | if (reprogram) { |
a096309b PZ |
2517 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
2518 | add_event_to_ctx(event, ctx); | |
487f05e1 | 2519 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2520 | } else { |
2521 | add_event_to_ctx(event, ctx); | |
2522 | } | |
2523 | ||
63b6da39 | 2524 | unlock: |
2c29ef0f | 2525 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2526 | |
a096309b | 2527 | return ret; |
0793a61d TG |
2528 | } |
2529 | ||
2530 | /* | |
a096309b PZ |
2531 | * Attach a performance event to a context. |
2532 | * | |
2533 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2534 | */ |
2535 | static void | |
cdd6c482 IM |
2536 | perf_install_in_context(struct perf_event_context *ctx, |
2537 | struct perf_event *event, | |
0793a61d TG |
2538 | int cpu) |
2539 | { | |
a096309b | 2540 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2541 | |
fe4b04fa PZ |
2542 | lockdep_assert_held(&ctx->mutex); |
2543 | ||
0cda4c02 YZ |
2544 | if (event->cpu != -1) |
2545 | event->cpu = cpu; | |
c3f00c70 | 2546 | |
0b8f1e2e PZ |
2547 | /* |
2548 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2549 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2550 | */ | |
2551 | smp_store_release(&event->ctx, ctx); | |
2552 | ||
a096309b PZ |
2553 | if (!task) { |
2554 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2555 | return; | |
2556 | } | |
2557 | ||
2558 | /* | |
2559 | * Should not happen, we validate the ctx is still alive before calling. | |
2560 | */ | |
2561 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2562 | return; | |
2563 | ||
39a43640 PZ |
2564 | /* |
2565 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2566 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2567 | * |
2568 | * Instead we use task_curr(), which tells us if the task is running. | |
2569 | * However, since we use task_curr() outside of rq::lock, we can race | |
2570 | * against the actual state. This means the result can be wrong. | |
2571 | * | |
2572 | * If we get a false positive, we retry, this is harmless. | |
2573 | * | |
2574 | * If we get a false negative, things are complicated. If we are after | |
2575 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2576 | * value must be correct. If we're before, it doesn't matter since | |
2577 | * perf_event_context_sched_in() will program the counter. | |
2578 | * | |
2579 | * However, this hinges on the remote context switch having observed | |
2580 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2581 | * ctx::lock in perf_event_context_sched_in(). | |
2582 | * | |
2583 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2584 | * we know any future context switch of task must see the | |
2585 | * perf_event_ctpx[] store. | |
39a43640 | 2586 | */ |
63cae12b | 2587 | |
63b6da39 | 2588 | /* |
63cae12b PZ |
2589 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2590 | * task_cpu() load, such that if the IPI then does not find the task | |
2591 | * running, a future context switch of that task must observe the | |
2592 | * store. | |
63b6da39 | 2593 | */ |
63cae12b PZ |
2594 | smp_mb(); |
2595 | again: | |
2596 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2597 | return; |
2598 | ||
2599 | raw_spin_lock_irq(&ctx->lock); | |
2600 | task = ctx->task; | |
84c4e620 | 2601 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2602 | /* |
2603 | * Cannot happen because we already checked above (which also | |
2604 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2605 | * against perf_event_exit_task_context(). | |
2606 | */ | |
63b6da39 PZ |
2607 | raw_spin_unlock_irq(&ctx->lock); |
2608 | return; | |
2609 | } | |
39a43640 | 2610 | /* |
63cae12b PZ |
2611 | * If the task is not running, ctx->lock will avoid it becoming so, |
2612 | * thus we can safely install the event. | |
39a43640 | 2613 | */ |
63cae12b PZ |
2614 | if (task_curr(task)) { |
2615 | raw_spin_unlock_irq(&ctx->lock); | |
2616 | goto again; | |
2617 | } | |
2618 | add_event_to_ctx(event, ctx); | |
2619 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2620 | } |
2621 | ||
d859e29f | 2622 | /* |
cdd6c482 | 2623 | * Cross CPU call to enable a performance event |
d859e29f | 2624 | */ |
fae3fde6 PZ |
2625 | static void __perf_event_enable(struct perf_event *event, |
2626 | struct perf_cpu_context *cpuctx, | |
2627 | struct perf_event_context *ctx, | |
2628 | void *info) | |
04289bb9 | 2629 | { |
cdd6c482 | 2630 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2631 | struct perf_event_context *task_ctx; |
04289bb9 | 2632 | |
6e801e01 PZ |
2633 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2634 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2635 | return; |
3cbed429 | 2636 | |
bd2afa49 PZ |
2637 | if (ctx->is_active) |
2638 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2639 | ||
0d3d73aa | 2640 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
04289bb9 | 2641 | |
fae3fde6 PZ |
2642 | if (!ctx->is_active) |
2643 | return; | |
2644 | ||
e5d1367f | 2645 | if (!event_filter_match(event)) { |
bd2afa49 | 2646 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2647 | return; |
e5d1367f | 2648 | } |
f4c4176f | 2649 | |
04289bb9 | 2650 | /* |
cdd6c482 | 2651 | * If the event is in a group and isn't the group leader, |
d859e29f | 2652 | * then don't put it on unless the group is on. |
04289bb9 | 2653 | */ |
bd2afa49 PZ |
2654 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2655 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2656 | return; |
bd2afa49 | 2657 | } |
fe4b04fa | 2658 | |
fae3fde6 PZ |
2659 | task_ctx = cpuctx->task_ctx; |
2660 | if (ctx->task) | |
2661 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2662 | |
487f05e1 | 2663 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2664 | } |
2665 | ||
d859e29f | 2666 | /* |
cdd6c482 | 2667 | * Enable a event. |
c93f7669 | 2668 | * |
cdd6c482 IM |
2669 | * If event->ctx is a cloned context, callers must make sure that |
2670 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2671 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2672 | * perf_event_for_each_child or perf_event_for_each as described |
2673 | * for perf_event_disable. | |
d859e29f | 2674 | */ |
f63a8daa | 2675 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2676 | { |
cdd6c482 | 2677 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2678 | |
7b648018 | 2679 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2680 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2681 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2682 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2683 | return; |
2684 | } | |
2685 | ||
d859e29f | 2686 | /* |
cdd6c482 | 2687 | * If the event is in error state, clear that first. |
7b648018 PZ |
2688 | * |
2689 | * That way, if we see the event in error state below, we know that it | |
2690 | * has gone back into error state, as distinct from the task having | |
2691 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2692 | */ |
cdd6c482 IM |
2693 | if (event->state == PERF_EVENT_STATE_ERROR) |
2694 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2695 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2696 | |
fae3fde6 | 2697 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2698 | } |
f63a8daa PZ |
2699 | |
2700 | /* | |
2701 | * See perf_event_disable(); | |
2702 | */ | |
2703 | void perf_event_enable(struct perf_event *event) | |
2704 | { | |
2705 | struct perf_event_context *ctx; | |
2706 | ||
2707 | ctx = perf_event_ctx_lock(event); | |
2708 | _perf_event_enable(event); | |
2709 | perf_event_ctx_unlock(event, ctx); | |
2710 | } | |
dcfce4a0 | 2711 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2712 | |
375637bc AS |
2713 | struct stop_event_data { |
2714 | struct perf_event *event; | |
2715 | unsigned int restart; | |
2716 | }; | |
2717 | ||
95ff4ca2 AS |
2718 | static int __perf_event_stop(void *info) |
2719 | { | |
375637bc AS |
2720 | struct stop_event_data *sd = info; |
2721 | struct perf_event *event = sd->event; | |
95ff4ca2 | 2722 | |
375637bc | 2723 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
2724 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
2725 | return 0; | |
2726 | ||
2727 | /* matches smp_wmb() in event_sched_in() */ | |
2728 | smp_rmb(); | |
2729 | ||
2730 | /* | |
2731 | * There is a window with interrupts enabled before we get here, | |
2732 | * so we need to check again lest we try to stop another CPU's event. | |
2733 | */ | |
2734 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2735 | return -EAGAIN; | |
2736 | ||
2737 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2738 | ||
375637bc AS |
2739 | /* |
2740 | * May race with the actual stop (through perf_pmu_output_stop()), | |
2741 | * but it is only used for events with AUX ring buffer, and such | |
2742 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
2743 | * see comments in perf_aux_output_begin(). | |
2744 | * | |
2745 | * Since this is happening on a event-local CPU, no trace is lost | |
2746 | * while restarting. | |
2747 | */ | |
2748 | if (sd->restart) | |
c9bbdd48 | 2749 | event->pmu->start(event, 0); |
375637bc | 2750 | |
95ff4ca2 AS |
2751 | return 0; |
2752 | } | |
2753 | ||
767ae086 | 2754 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
2755 | { |
2756 | struct stop_event_data sd = { | |
2757 | .event = event, | |
767ae086 | 2758 | .restart = restart, |
375637bc AS |
2759 | }; |
2760 | int ret = 0; | |
2761 | ||
2762 | do { | |
2763 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2764 | return 0; | |
2765 | ||
2766 | /* matches smp_wmb() in event_sched_in() */ | |
2767 | smp_rmb(); | |
2768 | ||
2769 | /* | |
2770 | * We only want to restart ACTIVE events, so if the event goes | |
2771 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
2772 | * fall through with ret==-ENXIO. | |
2773 | */ | |
2774 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
2775 | __perf_event_stop, &sd); | |
2776 | } while (ret == -EAGAIN); | |
2777 | ||
2778 | return ret; | |
2779 | } | |
2780 | ||
2781 | /* | |
2782 | * In order to contain the amount of racy and tricky in the address filter | |
2783 | * configuration management, it is a two part process: | |
2784 | * | |
2785 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
2786 | * we update the addresses of corresponding vmas in | |
2787 | * event::addr_filters_offs array and bump the event::addr_filters_gen; | |
2788 | * (p2) when an event is scheduled in (pmu::add), it calls | |
2789 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
2790 | * if the generation has changed since the previous call. | |
2791 | * | |
2792 | * If (p1) happens while the event is active, we restart it to force (p2). | |
2793 | * | |
2794 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
2795 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
2796 | * ioctl; | |
2797 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
2798 | * registered mapping, called for every new mmap(), with mm::mmap_sem down | |
2799 | * for reading; | |
2800 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
2801 | * of exec. | |
2802 | */ | |
2803 | void perf_event_addr_filters_sync(struct perf_event *event) | |
2804 | { | |
2805 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
2806 | ||
2807 | if (!has_addr_filter(event)) | |
2808 | return; | |
2809 | ||
2810 | raw_spin_lock(&ifh->lock); | |
2811 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
2812 | event->pmu->addr_filters_sync(event); | |
2813 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
2814 | } | |
2815 | raw_spin_unlock(&ifh->lock); | |
2816 | } | |
2817 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
2818 | ||
f63a8daa | 2819 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2820 | { |
2023b359 | 2821 | /* |
cdd6c482 | 2822 | * not supported on inherited events |
2023b359 | 2823 | */ |
2e939d1d | 2824 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2825 | return -EINVAL; |
2826 | ||
cdd6c482 | 2827 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2828 | _perf_event_enable(event); |
2023b359 PZ |
2829 | |
2830 | return 0; | |
79f14641 | 2831 | } |
f63a8daa PZ |
2832 | |
2833 | /* | |
2834 | * See perf_event_disable() | |
2835 | */ | |
2836 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2837 | { | |
2838 | struct perf_event_context *ctx; | |
2839 | int ret; | |
2840 | ||
2841 | ctx = perf_event_ctx_lock(event); | |
2842 | ret = _perf_event_refresh(event, refresh); | |
2843 | perf_event_ctx_unlock(event, ctx); | |
2844 | ||
2845 | return ret; | |
2846 | } | |
26ca5c11 | 2847 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2848 | |
5b0311e1 FW |
2849 | static void ctx_sched_out(struct perf_event_context *ctx, |
2850 | struct perf_cpu_context *cpuctx, | |
2851 | enum event_type_t event_type) | |
235c7fc7 | 2852 | { |
6668128a | 2853 | struct perf_event *event, *tmp; |
db24d33e | 2854 | int is_active = ctx->is_active; |
235c7fc7 | 2855 | |
c994d613 | 2856 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2857 | |
39a43640 PZ |
2858 | if (likely(!ctx->nr_events)) { |
2859 | /* | |
2860 | * See __perf_remove_from_context(). | |
2861 | */ | |
2862 | WARN_ON_ONCE(ctx->is_active); | |
2863 | if (ctx->task) | |
2864 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2865 | return; |
39a43640 PZ |
2866 | } |
2867 | ||
db24d33e | 2868 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2869 | if (!(ctx->is_active & EVENT_ALL)) |
2870 | ctx->is_active = 0; | |
2871 | ||
63e30d3e PZ |
2872 | if (ctx->task) { |
2873 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2874 | if (!ctx->is_active) | |
2875 | cpuctx->task_ctx = NULL; | |
2876 | } | |
facc4307 | 2877 | |
8fdc6539 PZ |
2878 | /* |
2879 | * Always update time if it was set; not only when it changes. | |
2880 | * Otherwise we can 'forget' to update time for any but the last | |
2881 | * context we sched out. For example: | |
2882 | * | |
2883 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2884 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2885 | * | |
2886 | * would only update time for the pinned events. | |
2887 | */ | |
3cbaa590 PZ |
2888 | if (is_active & EVENT_TIME) { |
2889 | /* update (and stop) ctx time */ | |
2890 | update_context_time(ctx); | |
2891 | update_cgrp_time_from_cpuctx(cpuctx); | |
2892 | } | |
2893 | ||
8fdc6539 PZ |
2894 | is_active ^= ctx->is_active; /* changed bits */ |
2895 | ||
3cbaa590 | 2896 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2897 | return; |
5b0311e1 | 2898 | |
075e0b00 | 2899 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2900 | if (is_active & EVENT_PINNED) { |
6668128a | 2901 | list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) |
889ff015 | 2902 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2903 | } |
889ff015 | 2904 | |
3cbaa590 | 2905 | if (is_active & EVENT_FLEXIBLE) { |
6668128a | 2906 | list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list) |
8c9ed8e1 | 2907 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2908 | } |
1b9a644f | 2909 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2910 | } |
2911 | ||
564c2b21 | 2912 | /* |
5a3126d4 PZ |
2913 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2914 | * cloned from the same version of the same context. | |
2915 | * | |
2916 | * Equivalence is measured using a generation number in the context that is | |
2917 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2918 | * and list_del_event(). | |
564c2b21 | 2919 | */ |
cdd6c482 IM |
2920 | static int context_equiv(struct perf_event_context *ctx1, |
2921 | struct perf_event_context *ctx2) | |
564c2b21 | 2922 | { |
211de6eb PZ |
2923 | lockdep_assert_held(&ctx1->lock); |
2924 | lockdep_assert_held(&ctx2->lock); | |
2925 | ||
5a3126d4 PZ |
2926 | /* Pinning disables the swap optimization */ |
2927 | if (ctx1->pin_count || ctx2->pin_count) | |
2928 | return 0; | |
2929 | ||
2930 | /* If ctx1 is the parent of ctx2 */ | |
2931 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2932 | return 1; | |
2933 | ||
2934 | /* If ctx2 is the parent of ctx1 */ | |
2935 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2936 | return 1; | |
2937 | ||
2938 | /* | |
2939 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2940 | * hierarchy, see perf_event_init_context(). | |
2941 | */ | |
2942 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2943 | ctx1->parent_gen == ctx2->parent_gen) | |
2944 | return 1; | |
2945 | ||
2946 | /* Unmatched */ | |
2947 | return 0; | |
564c2b21 PM |
2948 | } |
2949 | ||
cdd6c482 IM |
2950 | static void __perf_event_sync_stat(struct perf_event *event, |
2951 | struct perf_event *next_event) | |
bfbd3381 PZ |
2952 | { |
2953 | u64 value; | |
2954 | ||
cdd6c482 | 2955 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2956 | return; |
2957 | ||
2958 | /* | |
cdd6c482 | 2959 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2960 | * because we're in the middle of a context switch and have IRQs |
2961 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2962 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2963 | * don't need to use it. |
2964 | */ | |
0d3d73aa | 2965 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
3dbebf15 | 2966 | event->pmu->read(event); |
bfbd3381 | 2967 | |
0d3d73aa | 2968 | perf_event_update_time(event); |
bfbd3381 PZ |
2969 | |
2970 | /* | |
cdd6c482 | 2971 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2972 | * values when we flip the contexts. |
2973 | */ | |
e7850595 PZ |
2974 | value = local64_read(&next_event->count); |
2975 | value = local64_xchg(&event->count, value); | |
2976 | local64_set(&next_event->count, value); | |
bfbd3381 | 2977 | |
cdd6c482 IM |
2978 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2979 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2980 | |
bfbd3381 | 2981 | /* |
19d2e755 | 2982 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2983 | */ |
cdd6c482 IM |
2984 | perf_event_update_userpage(event); |
2985 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2986 | } |
2987 | ||
cdd6c482 IM |
2988 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2989 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2990 | { |
cdd6c482 | 2991 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2992 | |
2993 | if (!ctx->nr_stat) | |
2994 | return; | |
2995 | ||
02ffdbc8 PZ |
2996 | update_context_time(ctx); |
2997 | ||
cdd6c482 IM |
2998 | event = list_first_entry(&ctx->event_list, |
2999 | struct perf_event, event_entry); | |
bfbd3381 | 3000 | |
cdd6c482 IM |
3001 | next_event = list_first_entry(&next_ctx->event_list, |
3002 | struct perf_event, event_entry); | |
bfbd3381 | 3003 | |
cdd6c482 IM |
3004 | while (&event->event_entry != &ctx->event_list && |
3005 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 3006 | |
cdd6c482 | 3007 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 3008 | |
cdd6c482 IM |
3009 | event = list_next_entry(event, event_entry); |
3010 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
3011 | } |
3012 | } | |
3013 | ||
fe4b04fa PZ |
3014 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
3015 | struct task_struct *next) | |
0793a61d | 3016 | { |
8dc85d54 | 3017 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 3018 | struct perf_event_context *next_ctx; |
5a3126d4 | 3019 | struct perf_event_context *parent, *next_parent; |
108b02cf | 3020 | struct perf_cpu_context *cpuctx; |
c93f7669 | 3021 | int do_switch = 1; |
0793a61d | 3022 | |
108b02cf PZ |
3023 | if (likely(!ctx)) |
3024 | return; | |
10989fb2 | 3025 | |
108b02cf PZ |
3026 | cpuctx = __get_cpu_context(ctx); |
3027 | if (!cpuctx->task_ctx) | |
0793a61d TG |
3028 | return; |
3029 | ||
c93f7669 | 3030 | rcu_read_lock(); |
8dc85d54 | 3031 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
3032 | if (!next_ctx) |
3033 | goto unlock; | |
3034 | ||
3035 | parent = rcu_dereference(ctx->parent_ctx); | |
3036 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
3037 | ||
3038 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 3039 | if (!parent && !next_parent) |
5a3126d4 PZ |
3040 | goto unlock; |
3041 | ||
3042 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
3043 | /* |
3044 | * Looks like the two contexts are clones, so we might be | |
3045 | * able to optimize the context switch. We lock both | |
3046 | * contexts and check that they are clones under the | |
3047 | * lock (including re-checking that neither has been | |
3048 | * uncloned in the meantime). It doesn't matter which | |
3049 | * order we take the locks because no other cpu could | |
3050 | * be trying to lock both of these tasks. | |
3051 | */ | |
e625cce1 TG |
3052 | raw_spin_lock(&ctx->lock); |
3053 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 3054 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
3055 | WRITE_ONCE(ctx->task, next); |
3056 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
3057 | |
3058 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
3059 | ||
63b6da39 PZ |
3060 | /* |
3061 | * RCU_INIT_POINTER here is safe because we've not | |
3062 | * modified the ctx and the above modification of | |
3063 | * ctx->task and ctx->task_ctx_data are immaterial | |
3064 | * since those values are always verified under | |
3065 | * ctx->lock which we're now holding. | |
3066 | */ | |
3067 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
3068 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
3069 | ||
c93f7669 | 3070 | do_switch = 0; |
bfbd3381 | 3071 | |
cdd6c482 | 3072 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 3073 | } |
e625cce1 TG |
3074 | raw_spin_unlock(&next_ctx->lock); |
3075 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 3076 | } |
5a3126d4 | 3077 | unlock: |
c93f7669 | 3078 | rcu_read_unlock(); |
564c2b21 | 3079 | |
c93f7669 | 3080 | if (do_switch) { |
facc4307 | 3081 | raw_spin_lock(&ctx->lock); |
487f05e1 | 3082 | task_ctx_sched_out(cpuctx, ctx, EVENT_ALL); |
facc4307 | 3083 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 3084 | } |
0793a61d TG |
3085 | } |
3086 | ||
e48c1788 PZ |
3087 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
3088 | ||
ba532500 YZ |
3089 | void perf_sched_cb_dec(struct pmu *pmu) |
3090 | { | |
e48c1788 PZ |
3091 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
3092 | ||
ba532500 | 3093 | this_cpu_dec(perf_sched_cb_usages); |
e48c1788 PZ |
3094 | |
3095 | if (!--cpuctx->sched_cb_usage) | |
3096 | list_del(&cpuctx->sched_cb_entry); | |
ba532500 YZ |
3097 | } |
3098 | ||
e48c1788 | 3099 | |
ba532500 YZ |
3100 | void perf_sched_cb_inc(struct pmu *pmu) |
3101 | { | |
e48c1788 PZ |
3102 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
3103 | ||
3104 | if (!cpuctx->sched_cb_usage++) | |
3105 | list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
3106 | ||
ba532500 YZ |
3107 | this_cpu_inc(perf_sched_cb_usages); |
3108 | } | |
3109 | ||
3110 | /* | |
3111 | * This function provides the context switch callback to the lower code | |
3112 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
3113 | * |
3114 | * This callback is relevant even to per-cpu events; for example multi event | |
3115 | * PEBS requires this to provide PID/TID information. This requires we flush | |
3116 | * all queued PEBS records before we context switch to a new task. | |
ba532500 YZ |
3117 | */ |
3118 | static void perf_pmu_sched_task(struct task_struct *prev, | |
3119 | struct task_struct *next, | |
3120 | bool sched_in) | |
3121 | { | |
3122 | struct perf_cpu_context *cpuctx; | |
3123 | struct pmu *pmu; | |
ba532500 YZ |
3124 | |
3125 | if (prev == next) | |
3126 | return; | |
3127 | ||
e48c1788 | 3128 | list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) { |
1fd7e416 | 3129 | pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */ |
ba532500 | 3130 | |
e48c1788 PZ |
3131 | if (WARN_ON_ONCE(!pmu->sched_task)) |
3132 | continue; | |
ba532500 | 3133 | |
e48c1788 PZ |
3134 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
3135 | perf_pmu_disable(pmu); | |
ba532500 | 3136 | |
e48c1788 | 3137 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
ba532500 | 3138 | |
e48c1788 PZ |
3139 | perf_pmu_enable(pmu); |
3140 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
ba532500 | 3141 | } |
ba532500 YZ |
3142 | } |
3143 | ||
45ac1403 AH |
3144 | static void perf_event_switch(struct task_struct *task, |
3145 | struct task_struct *next_prev, bool sched_in); | |
3146 | ||
8dc85d54 PZ |
3147 | #define for_each_task_context_nr(ctxn) \ |
3148 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
3149 | ||
3150 | /* | |
3151 | * Called from scheduler to remove the events of the current task, | |
3152 | * with interrupts disabled. | |
3153 | * | |
3154 | * We stop each event and update the event value in event->count. | |
3155 | * | |
3156 | * This does not protect us against NMI, but disable() | |
3157 | * sets the disabled bit in the control field of event _before_ | |
3158 | * accessing the event control register. If a NMI hits, then it will | |
3159 | * not restart the event. | |
3160 | */ | |
ab0cce56 JO |
3161 | void __perf_event_task_sched_out(struct task_struct *task, |
3162 | struct task_struct *next) | |
8dc85d54 PZ |
3163 | { |
3164 | int ctxn; | |
3165 | ||
ba532500 YZ |
3166 | if (__this_cpu_read(perf_sched_cb_usages)) |
3167 | perf_pmu_sched_task(task, next, false); | |
3168 | ||
45ac1403 AH |
3169 | if (atomic_read(&nr_switch_events)) |
3170 | perf_event_switch(task, next, false); | |
3171 | ||
8dc85d54 PZ |
3172 | for_each_task_context_nr(ctxn) |
3173 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
3174 | |
3175 | /* | |
3176 | * if cgroup events exist on this CPU, then we need | |
3177 | * to check if we have to switch out PMU state. | |
3178 | * cgroup event are system-wide mode only | |
3179 | */ | |
4a32fea9 | 3180 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 3181 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
3182 | } |
3183 | ||
5b0311e1 FW |
3184 | /* |
3185 | * Called with IRQs disabled | |
3186 | */ | |
3187 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
3188 | enum event_type_t event_type) | |
3189 | { | |
3190 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
3191 | } |
3192 | ||
1cac7b1a PZ |
3193 | static int visit_groups_merge(struct perf_event_groups *groups, int cpu, |
3194 | int (*func)(struct perf_event *, void *), void *data) | |
0793a61d | 3195 | { |
1cac7b1a PZ |
3196 | struct perf_event **evt, *evt1, *evt2; |
3197 | int ret; | |
8e1a2031 | 3198 | |
1cac7b1a PZ |
3199 | evt1 = perf_event_groups_first(groups, -1); |
3200 | evt2 = perf_event_groups_first(groups, cpu); | |
3201 | ||
3202 | while (evt1 || evt2) { | |
3203 | if (evt1 && evt2) { | |
3204 | if (evt1->group_index < evt2->group_index) | |
3205 | evt = &evt1; | |
3206 | else | |
3207 | evt = &evt2; | |
3208 | } else if (evt1) { | |
3209 | evt = &evt1; | |
3210 | } else { | |
3211 | evt = &evt2; | |
8e1a2031 | 3212 | } |
1cac7b1a PZ |
3213 | |
3214 | ret = func(*evt, data); | |
3215 | if (ret) | |
3216 | return ret; | |
3217 | ||
3218 | *evt = perf_event_groups_next(*evt); | |
8e1a2031 | 3219 | } |
0793a61d | 3220 | |
1cac7b1a PZ |
3221 | return 0; |
3222 | } | |
3223 | ||
3224 | struct sched_in_data { | |
3225 | struct perf_event_context *ctx; | |
3226 | struct perf_cpu_context *cpuctx; | |
3227 | int can_add_hw; | |
3228 | }; | |
3229 | ||
3230 | static int pinned_sched_in(struct perf_event *event, void *data) | |
3231 | { | |
3232 | struct sched_in_data *sid = data; | |
3233 | ||
3234 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3235 | return 0; | |
3236 | ||
3237 | if (!event_filter_match(event)) | |
3238 | return 0; | |
3239 | ||
6668128a PZ |
3240 | if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) { |
3241 | if (!group_sched_in(event, sid->cpuctx, sid->ctx)) | |
3242 | list_add_tail(&event->active_list, &sid->ctx->pinned_active); | |
3243 | } | |
1cac7b1a PZ |
3244 | |
3245 | /* | |
3246 | * If this pinned group hasn't been scheduled, | |
3247 | * put it in error state. | |
3248 | */ | |
3249 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
3250 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); | |
3251 | ||
3252 | return 0; | |
3253 | } | |
3254 | ||
3255 | static int flexible_sched_in(struct perf_event *event, void *data) | |
3256 | { | |
3257 | struct sched_in_data *sid = data; | |
3258 | ||
3259 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3260 | return 0; | |
3261 | ||
3262 | if (!event_filter_match(event)) | |
3263 | return 0; | |
3264 | ||
3265 | if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) { | |
6668128a PZ |
3266 | if (!group_sched_in(event, sid->cpuctx, sid->ctx)) |
3267 | list_add_tail(&event->active_list, &sid->ctx->flexible_active); | |
3268 | else | |
1cac7b1a | 3269 | sid->can_add_hw = 0; |
3b6f9e5c | 3270 | } |
1cac7b1a PZ |
3271 | |
3272 | return 0; | |
5b0311e1 FW |
3273 | } |
3274 | ||
3275 | static void | |
1cac7b1a PZ |
3276 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
3277 | struct perf_cpu_context *cpuctx) | |
5b0311e1 | 3278 | { |
1cac7b1a PZ |
3279 | struct sched_in_data sid = { |
3280 | .ctx = ctx, | |
3281 | .cpuctx = cpuctx, | |
3282 | .can_add_hw = 1, | |
3283 | }; | |
3b6f9e5c | 3284 | |
1cac7b1a PZ |
3285 | visit_groups_merge(&ctx->pinned_groups, |
3286 | smp_processor_id(), | |
3287 | pinned_sched_in, &sid); | |
3288 | } | |
8e1a2031 | 3289 | |
1cac7b1a PZ |
3290 | static void |
3291 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
3292 | struct perf_cpu_context *cpuctx) | |
3293 | { | |
3294 | struct sched_in_data sid = { | |
3295 | .ctx = ctx, | |
3296 | .cpuctx = cpuctx, | |
3297 | .can_add_hw = 1, | |
3298 | }; | |
0793a61d | 3299 | |
1cac7b1a PZ |
3300 | visit_groups_merge(&ctx->flexible_groups, |
3301 | smp_processor_id(), | |
3302 | flexible_sched_in, &sid); | |
5b0311e1 FW |
3303 | } |
3304 | ||
3305 | static void | |
3306 | ctx_sched_in(struct perf_event_context *ctx, | |
3307 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
3308 | enum event_type_t event_type, |
3309 | struct task_struct *task) | |
5b0311e1 | 3310 | { |
db24d33e | 3311 | int is_active = ctx->is_active; |
c994d613 PZ |
3312 | u64 now; |
3313 | ||
3314 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3315 | |
5b0311e1 | 3316 | if (likely(!ctx->nr_events)) |
facc4307 | 3317 | return; |
5b0311e1 | 3318 | |
3cbaa590 | 3319 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3320 | if (ctx->task) { |
3321 | if (!is_active) | |
3322 | cpuctx->task_ctx = ctx; | |
3323 | else | |
3324 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3325 | } | |
3326 | ||
3cbaa590 PZ |
3327 | is_active ^= ctx->is_active; /* changed bits */ |
3328 | ||
3329 | if (is_active & EVENT_TIME) { | |
3330 | /* start ctx time */ | |
3331 | now = perf_clock(); | |
3332 | ctx->timestamp = now; | |
3333 | perf_cgroup_set_timestamp(task, ctx); | |
3334 | } | |
3335 | ||
5b0311e1 FW |
3336 | /* |
3337 | * First go through the list and put on any pinned groups | |
3338 | * in order to give them the best chance of going on. | |
3339 | */ | |
3cbaa590 | 3340 | if (is_active & EVENT_PINNED) |
6e37738a | 3341 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
3342 | |
3343 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3344 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 3345 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
3346 | } |
3347 | ||
329c0e01 | 3348 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
3349 | enum event_type_t event_type, |
3350 | struct task_struct *task) | |
329c0e01 FW |
3351 | { |
3352 | struct perf_event_context *ctx = &cpuctx->ctx; | |
3353 | ||
e5d1367f | 3354 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
3355 | } |
3356 | ||
e5d1367f SE |
3357 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
3358 | struct task_struct *task) | |
235c7fc7 | 3359 | { |
108b02cf | 3360 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 3361 | |
108b02cf | 3362 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
3363 | if (cpuctx->task_ctx == ctx) |
3364 | return; | |
3365 | ||
facc4307 | 3366 | perf_ctx_lock(cpuctx, ctx); |
fdccc3fb | 3367 | /* |
3368 | * We must check ctx->nr_events while holding ctx->lock, such | |
3369 | * that we serialize against perf_install_in_context(). | |
3370 | */ | |
3371 | if (!ctx->nr_events) | |
3372 | goto unlock; | |
3373 | ||
1b9a644f | 3374 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
3375 | /* |
3376 | * We want to keep the following priority order: | |
3377 | * cpu pinned (that don't need to move), task pinned, | |
3378 | * cpu flexible, task flexible. | |
fe45bafb AS |
3379 | * |
3380 | * However, if task's ctx is not carrying any pinned | |
3381 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3382 | */ |
8e1a2031 | 3383 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) |
fe45bafb | 3384 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
63e30d3e | 3385 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 | 3386 | perf_pmu_enable(ctx->pmu); |
fdccc3fb | 3387 | |
3388 | unlock: | |
facc4307 | 3389 | perf_ctx_unlock(cpuctx, ctx); |
235c7fc7 IM |
3390 | } |
3391 | ||
8dc85d54 PZ |
3392 | /* |
3393 | * Called from scheduler to add the events of the current task | |
3394 | * with interrupts disabled. | |
3395 | * | |
3396 | * We restore the event value and then enable it. | |
3397 | * | |
3398 | * This does not protect us against NMI, but enable() | |
3399 | * sets the enabled bit in the control field of event _before_ | |
3400 | * accessing the event control register. If a NMI hits, then it will | |
3401 | * keep the event running. | |
3402 | */ | |
ab0cce56 JO |
3403 | void __perf_event_task_sched_in(struct task_struct *prev, |
3404 | struct task_struct *task) | |
8dc85d54 PZ |
3405 | { |
3406 | struct perf_event_context *ctx; | |
3407 | int ctxn; | |
3408 | ||
7e41d177 PZ |
3409 | /* |
3410 | * If cgroup events exist on this CPU, then we need to check if we have | |
3411 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3412 | * | |
3413 | * Since cgroup events are CPU events, we must schedule these in before | |
3414 | * we schedule in the task events. | |
3415 | */ | |
3416 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3417 | perf_cgroup_sched_in(prev, task); | |
3418 | ||
8dc85d54 PZ |
3419 | for_each_task_context_nr(ctxn) { |
3420 | ctx = task->perf_event_ctxp[ctxn]; | |
3421 | if (likely(!ctx)) | |
3422 | continue; | |
3423 | ||
e5d1367f | 3424 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3425 | } |
d010b332 | 3426 | |
45ac1403 AH |
3427 | if (atomic_read(&nr_switch_events)) |
3428 | perf_event_switch(task, prev, true); | |
3429 | ||
ba532500 YZ |
3430 | if (__this_cpu_read(perf_sched_cb_usages)) |
3431 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3432 | } |
3433 | ||
abd50713 PZ |
3434 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3435 | { | |
3436 | u64 frequency = event->attr.sample_freq; | |
3437 | u64 sec = NSEC_PER_SEC; | |
3438 | u64 divisor, dividend; | |
3439 | ||
3440 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3441 | ||
3442 | count_fls = fls64(count); | |
3443 | nsec_fls = fls64(nsec); | |
3444 | frequency_fls = fls64(frequency); | |
3445 | sec_fls = 30; | |
3446 | ||
3447 | /* | |
3448 | * We got @count in @nsec, with a target of sample_freq HZ | |
3449 | * the target period becomes: | |
3450 | * | |
3451 | * @count * 10^9 | |
3452 | * period = ------------------- | |
3453 | * @nsec * sample_freq | |
3454 | * | |
3455 | */ | |
3456 | ||
3457 | /* | |
3458 | * Reduce accuracy by one bit such that @a and @b converge | |
3459 | * to a similar magnitude. | |
3460 | */ | |
fe4b04fa | 3461 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3462 | do { \ |
3463 | if (a##_fls > b##_fls) { \ | |
3464 | a >>= 1; \ | |
3465 | a##_fls--; \ | |
3466 | } else { \ | |
3467 | b >>= 1; \ | |
3468 | b##_fls--; \ | |
3469 | } \ | |
3470 | } while (0) | |
3471 | ||
3472 | /* | |
3473 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3474 | * the other, so that finally we can do a u64/u64 division. | |
3475 | */ | |
3476 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3477 | REDUCE_FLS(nsec, frequency); | |
3478 | REDUCE_FLS(sec, count); | |
3479 | } | |
3480 | ||
3481 | if (count_fls + sec_fls > 64) { | |
3482 | divisor = nsec * frequency; | |
3483 | ||
3484 | while (count_fls + sec_fls > 64) { | |
3485 | REDUCE_FLS(count, sec); | |
3486 | divisor >>= 1; | |
3487 | } | |
3488 | ||
3489 | dividend = count * sec; | |
3490 | } else { | |
3491 | dividend = count * sec; | |
3492 | ||
3493 | while (nsec_fls + frequency_fls > 64) { | |
3494 | REDUCE_FLS(nsec, frequency); | |
3495 | dividend >>= 1; | |
3496 | } | |
3497 | ||
3498 | divisor = nsec * frequency; | |
3499 | } | |
3500 | ||
f6ab91ad PZ |
3501 | if (!divisor) |
3502 | return dividend; | |
3503 | ||
abd50713 PZ |
3504 | return div64_u64(dividend, divisor); |
3505 | } | |
3506 | ||
e050e3f0 SE |
3507 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3508 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3509 | ||
f39d47ff | 3510 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3511 | { |
cdd6c482 | 3512 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3513 | s64 period, sample_period; |
bd2b5b12 PZ |
3514 | s64 delta; |
3515 | ||
abd50713 | 3516 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3517 | |
3518 | delta = (s64)(period - hwc->sample_period); | |
3519 | delta = (delta + 7) / 8; /* low pass filter */ | |
3520 | ||
3521 | sample_period = hwc->sample_period + delta; | |
3522 | ||
3523 | if (!sample_period) | |
3524 | sample_period = 1; | |
3525 | ||
bd2b5b12 | 3526 | hwc->sample_period = sample_period; |
abd50713 | 3527 | |
e7850595 | 3528 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3529 | if (disable) |
3530 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3531 | ||
e7850595 | 3532 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3533 | |
3534 | if (disable) | |
3535 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3536 | } |
bd2b5b12 PZ |
3537 | } |
3538 | ||
e050e3f0 SE |
3539 | /* |
3540 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3541 | * events. At the same time, make sure, having freq events does not change | |
3542 | * the rate of unthrottling as that would introduce bias. | |
3543 | */ | |
3544 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3545 | int needs_unthr) | |
60db5e09 | 3546 | { |
cdd6c482 IM |
3547 | struct perf_event *event; |
3548 | struct hw_perf_event *hwc; | |
e050e3f0 | 3549 | u64 now, period = TICK_NSEC; |
abd50713 | 3550 | s64 delta; |
60db5e09 | 3551 | |
e050e3f0 SE |
3552 | /* |
3553 | * only need to iterate over all events iff: | |
3554 | * - context have events in frequency mode (needs freq adjust) | |
3555 | * - there are events to unthrottle on this cpu | |
3556 | */ | |
3557 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3558 | return; |
3559 | ||
e050e3f0 | 3560 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3561 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3562 | |
03541f8b | 3563 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3564 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3565 | continue; |
3566 | ||
5632ab12 | 3567 | if (!event_filter_match(event)) |
5d27c23d PZ |
3568 | continue; |
3569 | ||
44377277 AS |
3570 | perf_pmu_disable(event->pmu); |
3571 | ||
cdd6c482 | 3572 | hwc = &event->hw; |
6a24ed6c | 3573 | |
ae23bff1 | 3574 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3575 | hwc->interrupts = 0; |
cdd6c482 | 3576 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3577 | event->pmu->start(event, 0); |
a78ac325 PZ |
3578 | } |
3579 | ||
cdd6c482 | 3580 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3581 | goto next; |
60db5e09 | 3582 | |
e050e3f0 SE |
3583 | /* |
3584 | * stop the event and update event->count | |
3585 | */ | |
3586 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3587 | ||
e7850595 | 3588 | now = local64_read(&event->count); |
abd50713 PZ |
3589 | delta = now - hwc->freq_count_stamp; |
3590 | hwc->freq_count_stamp = now; | |
60db5e09 | 3591 | |
e050e3f0 SE |
3592 | /* |
3593 | * restart the event | |
3594 | * reload only if value has changed | |
f39d47ff SE |
3595 | * we have stopped the event so tell that |
3596 | * to perf_adjust_period() to avoid stopping it | |
3597 | * twice. | |
e050e3f0 | 3598 | */ |
abd50713 | 3599 | if (delta > 0) |
f39d47ff | 3600 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3601 | |
3602 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3603 | next: |
3604 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3605 | } |
e050e3f0 | 3606 | |
f39d47ff | 3607 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3608 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3609 | } |
3610 | ||
235c7fc7 | 3611 | /* |
8703a7cf | 3612 | * Move @event to the tail of the @ctx's elegible events. |
235c7fc7 | 3613 | */ |
8703a7cf | 3614 | static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event) |
0793a61d | 3615 | { |
dddd3379 TG |
3616 | /* |
3617 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3618 | * disabled by the inheritance code. | |
3619 | */ | |
8703a7cf PZ |
3620 | if (ctx->rotate_disable) |
3621 | return; | |
8e1a2031 | 3622 | |
8703a7cf PZ |
3623 | perf_event_groups_delete(&ctx->flexible_groups, event); |
3624 | perf_event_groups_insert(&ctx->flexible_groups, event); | |
235c7fc7 IM |
3625 | } |
3626 | ||
8d5bce0c PZ |
3627 | static inline struct perf_event * |
3628 | ctx_first_active(struct perf_event_context *ctx) | |
235c7fc7 | 3629 | { |
8d5bce0c PZ |
3630 | return list_first_entry_or_null(&ctx->flexible_active, |
3631 | struct perf_event, active_list); | |
3632 | } | |
3633 | ||
3634 | static bool perf_rotate_context(struct perf_cpu_context *cpuctx) | |
3635 | { | |
3636 | struct perf_event *cpu_event = NULL, *task_event = NULL; | |
3637 | bool cpu_rotate = false, task_rotate = false; | |
8dc85d54 | 3638 | struct perf_event_context *ctx = NULL; |
8d5bce0c PZ |
3639 | |
3640 | /* | |
3641 | * Since we run this from IRQ context, nobody can install new | |
3642 | * events, thus the event count values are stable. | |
3643 | */ | |
7fc23a53 | 3644 | |
b5ab4cd5 | 3645 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 | 3646 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
8d5bce0c | 3647 | cpu_rotate = true; |
b5ab4cd5 | 3648 | } |
235c7fc7 | 3649 | |
8dc85d54 | 3650 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3651 | if (ctx && ctx->nr_events) { |
b5ab4cd5 | 3652 | if (ctx->nr_events != ctx->nr_active) |
8d5bce0c | 3653 | task_rotate = true; |
b5ab4cd5 | 3654 | } |
9717e6cd | 3655 | |
8d5bce0c PZ |
3656 | if (!(cpu_rotate || task_rotate)) |
3657 | return false; | |
0f5a2601 | 3658 | |
facc4307 | 3659 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3660 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3661 | |
8d5bce0c PZ |
3662 | if (task_rotate) |
3663 | task_event = ctx_first_active(ctx); | |
3664 | if (cpu_rotate) | |
3665 | cpu_event = ctx_first_active(&cpuctx->ctx); | |
8703a7cf | 3666 | |
8d5bce0c PZ |
3667 | /* |
3668 | * As per the order given at ctx_resched() first 'pop' task flexible | |
3669 | * and then, if needed CPU flexible. | |
3670 | */ | |
3671 | if (task_event || (ctx && cpu_event)) | |
e050e3f0 | 3672 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
8d5bce0c PZ |
3673 | if (cpu_event) |
3674 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3675 | |
8d5bce0c PZ |
3676 | if (task_event) |
3677 | rotate_ctx(ctx, task_event); | |
3678 | if (cpu_event) | |
3679 | rotate_ctx(&cpuctx->ctx, cpu_event); | |
235c7fc7 | 3680 | |
e050e3f0 | 3681 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3682 | |
0f5a2601 PZ |
3683 | perf_pmu_enable(cpuctx->ctx.pmu); |
3684 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
9e630205 | 3685 | |
8d5bce0c | 3686 | return true; |
e9d2b064 PZ |
3687 | } |
3688 | ||
3689 | void perf_event_task_tick(void) | |
3690 | { | |
2fde4f94 MR |
3691 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3692 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3693 | int throttled; |
b5ab4cd5 | 3694 | |
16444645 | 3695 | lockdep_assert_irqs_disabled(); |
e9d2b064 | 3696 | |
e050e3f0 SE |
3697 | __this_cpu_inc(perf_throttled_seq); |
3698 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3699 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3700 | |
2fde4f94 | 3701 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3702 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3703 | } |
3704 | ||
889ff015 FW |
3705 | static int event_enable_on_exec(struct perf_event *event, |
3706 | struct perf_event_context *ctx) | |
3707 | { | |
3708 | if (!event->attr.enable_on_exec) | |
3709 | return 0; | |
3710 | ||
3711 | event->attr.enable_on_exec = 0; | |
3712 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3713 | return 0; | |
3714 | ||
0d3d73aa | 3715 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
889ff015 FW |
3716 | |
3717 | return 1; | |
3718 | } | |
3719 | ||
57e7986e | 3720 | /* |
cdd6c482 | 3721 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3722 | * This expects task == current. |
3723 | */ | |
c1274499 | 3724 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3725 | { |
c1274499 | 3726 | struct perf_event_context *ctx, *clone_ctx = NULL; |
487f05e1 | 3727 | enum event_type_t event_type = 0; |
3e349507 | 3728 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3729 | struct perf_event *event; |
57e7986e PM |
3730 | unsigned long flags; |
3731 | int enabled = 0; | |
3732 | ||
3733 | local_irq_save(flags); | |
c1274499 | 3734 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3735 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3736 | goto out; |
3737 | ||
3e349507 PZ |
3738 | cpuctx = __get_cpu_context(ctx); |
3739 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3740 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
487f05e1 | 3741 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 3742 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
3743 | event_type |= get_event_type(event); |
3744 | } | |
57e7986e PM |
3745 | |
3746 | /* | |
3e349507 | 3747 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3748 | */ |
3e349507 | 3749 | if (enabled) { |
211de6eb | 3750 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 3751 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb PZ |
3752 | } else { |
3753 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
3e349507 PZ |
3754 | } |
3755 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3756 | |
9ed6060d | 3757 | out: |
57e7986e | 3758 | local_irq_restore(flags); |
211de6eb PZ |
3759 | |
3760 | if (clone_ctx) | |
3761 | put_ctx(clone_ctx); | |
57e7986e PM |
3762 | } |
3763 | ||
0492d4c5 PZ |
3764 | struct perf_read_data { |
3765 | struct perf_event *event; | |
3766 | bool group; | |
7d88962e | 3767 | int ret; |
0492d4c5 PZ |
3768 | }; |
3769 | ||
451d24d1 | 3770 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 3771 | { |
d6a2f903 DCC |
3772 | u16 local_pkg, event_pkg; |
3773 | ||
3774 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { | |
451d24d1 PZ |
3775 | int local_cpu = smp_processor_id(); |
3776 | ||
3777 | event_pkg = topology_physical_package_id(event_cpu); | |
3778 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
3779 | |
3780 | if (event_pkg == local_pkg) | |
3781 | return local_cpu; | |
3782 | } | |
3783 | ||
3784 | return event_cpu; | |
3785 | } | |
3786 | ||
0793a61d | 3787 | /* |
cdd6c482 | 3788 | * Cross CPU call to read the hardware event |
0793a61d | 3789 | */ |
cdd6c482 | 3790 | static void __perf_event_read(void *info) |
0793a61d | 3791 | { |
0492d4c5 PZ |
3792 | struct perf_read_data *data = info; |
3793 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3794 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3795 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3796 | struct pmu *pmu = event->pmu; |
621a01ea | 3797 | |
e1ac3614 PM |
3798 | /* |
3799 | * If this is a task context, we need to check whether it is | |
3800 | * the current task context of this cpu. If not it has been | |
3801 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3802 | * event->count would have been updated to a recent sample |
3803 | * when the event was scheduled out. | |
e1ac3614 PM |
3804 | */ |
3805 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3806 | return; | |
3807 | ||
e625cce1 | 3808 | raw_spin_lock(&ctx->lock); |
0c1cbc18 | 3809 | if (ctx->is_active & EVENT_TIME) { |
542e72fc | 3810 | update_context_time(ctx); |
e5d1367f SE |
3811 | update_cgrp_time_from_event(event); |
3812 | } | |
0492d4c5 | 3813 | |
0d3d73aa PZ |
3814 | perf_event_update_time(event); |
3815 | if (data->group) | |
3816 | perf_event_update_sibling_time(event); | |
0c1cbc18 | 3817 | |
4a00c16e SB |
3818 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3819 | goto unlock; | |
0492d4c5 | 3820 | |
4a00c16e SB |
3821 | if (!data->group) { |
3822 | pmu->read(event); | |
3823 | data->ret = 0; | |
0492d4c5 | 3824 | goto unlock; |
4a00c16e SB |
3825 | } |
3826 | ||
3827 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3828 | ||
3829 | pmu->read(event); | |
0492d4c5 | 3830 | |
8343aae6 | 3831 | list_for_each_entry(sub, &event->sibling_list, sibling_list) { |
4a00c16e SB |
3832 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3833 | /* | |
3834 | * Use sibling's PMU rather than @event's since | |
3835 | * sibling could be on different (eg: software) PMU. | |
3836 | */ | |
0492d4c5 | 3837 | sub->pmu->read(sub); |
4a00c16e | 3838 | } |
0492d4c5 | 3839 | } |
4a00c16e SB |
3840 | |
3841 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3842 | |
3843 | unlock: | |
e625cce1 | 3844 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3845 | } |
3846 | ||
b5e58793 PZ |
3847 | static inline u64 perf_event_count(struct perf_event *event) |
3848 | { | |
c39a0e2c | 3849 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
3850 | } |
3851 | ||
ffe8690c KX |
3852 | /* |
3853 | * NMI-safe method to read a local event, that is an event that | |
3854 | * is: | |
3855 | * - either for the current task, or for this CPU | |
3856 | * - does not have inherit set, for inherited task events | |
3857 | * will not be local and we cannot read them atomically | |
3858 | * - must not have a pmu::count method | |
3859 | */ | |
7d9285e8 YS |
3860 | int perf_event_read_local(struct perf_event *event, u64 *value, |
3861 | u64 *enabled, u64 *running) | |
ffe8690c KX |
3862 | { |
3863 | unsigned long flags; | |
f91840a3 | 3864 | int ret = 0; |
ffe8690c KX |
3865 | |
3866 | /* | |
3867 | * Disabling interrupts avoids all counter scheduling (context | |
3868 | * switches, timer based rotation and IPIs). | |
3869 | */ | |
3870 | local_irq_save(flags); | |
3871 | ||
ffe8690c KX |
3872 | /* |
3873 | * It must not be an event with inherit set, we cannot read | |
3874 | * all child counters from atomic context. | |
3875 | */ | |
f91840a3 AS |
3876 | if (event->attr.inherit) { |
3877 | ret = -EOPNOTSUPP; | |
3878 | goto out; | |
3879 | } | |
ffe8690c | 3880 | |
f91840a3 AS |
3881 | /* If this is a per-task event, it must be for current */ |
3882 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
3883 | event->hw.target != current) { | |
3884 | ret = -EINVAL; | |
3885 | goto out; | |
3886 | } | |
3887 | ||
3888 | /* If this is a per-CPU event, it must be for this CPU */ | |
3889 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
3890 | event->cpu != smp_processor_id()) { | |
3891 | ret = -EINVAL; | |
3892 | goto out; | |
3893 | } | |
ffe8690c KX |
3894 | |
3895 | /* | |
3896 | * If the event is currently on this CPU, its either a per-task event, | |
3897 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3898 | * oncpu == -1). | |
3899 | */ | |
3900 | if (event->oncpu == smp_processor_id()) | |
3901 | event->pmu->read(event); | |
3902 | ||
f91840a3 | 3903 | *value = local64_read(&event->count); |
0d3d73aa PZ |
3904 | if (enabled || running) { |
3905 | u64 now = event->shadow_ctx_time + perf_clock(); | |
3906 | u64 __enabled, __running; | |
3907 | ||
3908 | __perf_update_times(event, now, &__enabled, &__running); | |
3909 | if (enabled) | |
3910 | *enabled = __enabled; | |
3911 | if (running) | |
3912 | *running = __running; | |
3913 | } | |
f91840a3 | 3914 | out: |
ffe8690c KX |
3915 | local_irq_restore(flags); |
3916 | ||
f91840a3 | 3917 | return ret; |
ffe8690c KX |
3918 | } |
3919 | ||
7d88962e | 3920 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3921 | { |
0c1cbc18 | 3922 | enum perf_event_state state = READ_ONCE(event->state); |
451d24d1 | 3923 | int event_cpu, ret = 0; |
7d88962e | 3924 | |
0793a61d | 3925 | /* |
cdd6c482 IM |
3926 | * If event is enabled and currently active on a CPU, update the |
3927 | * value in the event structure: | |
0793a61d | 3928 | */ |
0c1cbc18 PZ |
3929 | again: |
3930 | if (state == PERF_EVENT_STATE_ACTIVE) { | |
3931 | struct perf_read_data data; | |
3932 | ||
3933 | /* | |
3934 | * Orders the ->state and ->oncpu loads such that if we see | |
3935 | * ACTIVE we must also see the right ->oncpu. | |
3936 | * | |
3937 | * Matches the smp_wmb() from event_sched_in(). | |
3938 | */ | |
3939 | smp_rmb(); | |
d6a2f903 | 3940 | |
451d24d1 PZ |
3941 | event_cpu = READ_ONCE(event->oncpu); |
3942 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
3943 | return 0; | |
3944 | ||
0c1cbc18 PZ |
3945 | data = (struct perf_read_data){ |
3946 | .event = event, | |
3947 | .group = group, | |
3948 | .ret = 0, | |
3949 | }; | |
3950 | ||
451d24d1 PZ |
3951 | preempt_disable(); |
3952 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 3953 | |
58763148 PZ |
3954 | /* |
3955 | * Purposely ignore the smp_call_function_single() return | |
3956 | * value. | |
3957 | * | |
451d24d1 | 3958 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
3959 | * scheduled out and that will have updated the event count. |
3960 | * | |
3961 | * Therefore, either way, we'll have an up-to-date event count | |
3962 | * after this. | |
3963 | */ | |
451d24d1 PZ |
3964 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
3965 | preempt_enable(); | |
58763148 | 3966 | ret = data.ret; |
0c1cbc18 PZ |
3967 | |
3968 | } else if (state == PERF_EVENT_STATE_INACTIVE) { | |
2b8988c9 PZ |
3969 | struct perf_event_context *ctx = event->ctx; |
3970 | unsigned long flags; | |
3971 | ||
e625cce1 | 3972 | raw_spin_lock_irqsave(&ctx->lock, flags); |
0c1cbc18 PZ |
3973 | state = event->state; |
3974 | if (state != PERF_EVENT_STATE_INACTIVE) { | |
3975 | raw_spin_unlock_irqrestore(&ctx->lock, flags); | |
3976 | goto again; | |
3977 | } | |
3978 | ||
c530ccd9 | 3979 | /* |
0c1cbc18 PZ |
3980 | * May read while context is not active (e.g., thread is |
3981 | * blocked), in that case we cannot update context time | |
c530ccd9 | 3982 | */ |
0c1cbc18 | 3983 | if (ctx->is_active & EVENT_TIME) { |
c530ccd9 | 3984 | update_context_time(ctx); |
e5d1367f SE |
3985 | update_cgrp_time_from_event(event); |
3986 | } | |
0c1cbc18 | 3987 | |
0d3d73aa | 3988 | perf_event_update_time(event); |
0492d4c5 | 3989 | if (group) |
0d3d73aa | 3990 | perf_event_update_sibling_time(event); |
e625cce1 | 3991 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3992 | } |
7d88962e SB |
3993 | |
3994 | return ret; | |
0793a61d TG |
3995 | } |
3996 | ||
a63eaf34 | 3997 | /* |
cdd6c482 | 3998 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3999 | */ |
eb184479 | 4000 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 4001 | { |
e625cce1 | 4002 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 4003 | mutex_init(&ctx->mutex); |
2fde4f94 | 4004 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
8e1a2031 AB |
4005 | perf_event_groups_init(&ctx->pinned_groups); |
4006 | perf_event_groups_init(&ctx->flexible_groups); | |
a63eaf34 | 4007 | INIT_LIST_HEAD(&ctx->event_list); |
6668128a PZ |
4008 | INIT_LIST_HEAD(&ctx->pinned_active); |
4009 | INIT_LIST_HEAD(&ctx->flexible_active); | |
a63eaf34 | 4010 | atomic_set(&ctx->refcount, 1); |
eb184479 PZ |
4011 | } |
4012 | ||
4013 | static struct perf_event_context * | |
4014 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
4015 | { | |
4016 | struct perf_event_context *ctx; | |
4017 | ||
4018 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
4019 | if (!ctx) | |
4020 | return NULL; | |
4021 | ||
4022 | __perf_event_init_context(ctx); | |
4023 | if (task) { | |
4024 | ctx->task = task; | |
4025 | get_task_struct(task); | |
0793a61d | 4026 | } |
eb184479 PZ |
4027 | ctx->pmu = pmu; |
4028 | ||
4029 | return ctx; | |
a63eaf34 PM |
4030 | } |
4031 | ||
2ebd4ffb MH |
4032 | static struct task_struct * |
4033 | find_lively_task_by_vpid(pid_t vpid) | |
4034 | { | |
4035 | struct task_struct *task; | |
0793a61d TG |
4036 | |
4037 | rcu_read_lock(); | |
2ebd4ffb | 4038 | if (!vpid) |
0793a61d TG |
4039 | task = current; |
4040 | else | |
2ebd4ffb | 4041 | task = find_task_by_vpid(vpid); |
0793a61d TG |
4042 | if (task) |
4043 | get_task_struct(task); | |
4044 | rcu_read_unlock(); | |
4045 | ||
4046 | if (!task) | |
4047 | return ERR_PTR(-ESRCH); | |
4048 | ||
2ebd4ffb | 4049 | return task; |
2ebd4ffb MH |
4050 | } |
4051 | ||
fe4b04fa PZ |
4052 | /* |
4053 | * Returns a matching context with refcount and pincount. | |
4054 | */ | |
108b02cf | 4055 | static struct perf_event_context * |
4af57ef2 YZ |
4056 | find_get_context(struct pmu *pmu, struct task_struct *task, |
4057 | struct perf_event *event) | |
0793a61d | 4058 | { |
211de6eb | 4059 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 4060 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 4061 | void *task_ctx_data = NULL; |
25346b93 | 4062 | unsigned long flags; |
8dc85d54 | 4063 | int ctxn, err; |
4af57ef2 | 4064 | int cpu = event->cpu; |
0793a61d | 4065 | |
22a4ec72 | 4066 | if (!task) { |
cdd6c482 | 4067 | /* Must be root to operate on a CPU event: */ |
0764771d | 4068 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
4069 | return ERR_PTR(-EACCES); |
4070 | ||
108b02cf | 4071 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 4072 | ctx = &cpuctx->ctx; |
c93f7669 | 4073 | get_ctx(ctx); |
fe4b04fa | 4074 | ++ctx->pin_count; |
0793a61d | 4075 | |
0793a61d TG |
4076 | return ctx; |
4077 | } | |
4078 | ||
8dc85d54 PZ |
4079 | err = -EINVAL; |
4080 | ctxn = pmu->task_ctx_nr; | |
4081 | if (ctxn < 0) | |
4082 | goto errout; | |
4083 | ||
4af57ef2 YZ |
4084 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
4085 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
4086 | if (!task_ctx_data) { | |
4087 | err = -ENOMEM; | |
4088 | goto errout; | |
4089 | } | |
4090 | } | |
4091 | ||
9ed6060d | 4092 | retry: |
8dc85d54 | 4093 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 4094 | if (ctx) { |
211de6eb | 4095 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 4096 | ++ctx->pin_count; |
4af57ef2 YZ |
4097 | |
4098 | if (task_ctx_data && !ctx->task_ctx_data) { | |
4099 | ctx->task_ctx_data = task_ctx_data; | |
4100 | task_ctx_data = NULL; | |
4101 | } | |
e625cce1 | 4102 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
4103 | |
4104 | if (clone_ctx) | |
4105 | put_ctx(clone_ctx); | |
9137fb28 | 4106 | } else { |
eb184479 | 4107 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
4108 | err = -ENOMEM; |
4109 | if (!ctx) | |
4110 | goto errout; | |
eb184479 | 4111 | |
4af57ef2 YZ |
4112 | if (task_ctx_data) { |
4113 | ctx->task_ctx_data = task_ctx_data; | |
4114 | task_ctx_data = NULL; | |
4115 | } | |
4116 | ||
dbe08d82 ON |
4117 | err = 0; |
4118 | mutex_lock(&task->perf_event_mutex); | |
4119 | /* | |
4120 | * If it has already passed perf_event_exit_task(). | |
4121 | * we must see PF_EXITING, it takes this mutex too. | |
4122 | */ | |
4123 | if (task->flags & PF_EXITING) | |
4124 | err = -ESRCH; | |
4125 | else if (task->perf_event_ctxp[ctxn]) | |
4126 | err = -EAGAIN; | |
fe4b04fa | 4127 | else { |
9137fb28 | 4128 | get_ctx(ctx); |
fe4b04fa | 4129 | ++ctx->pin_count; |
dbe08d82 | 4130 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 4131 | } |
dbe08d82 ON |
4132 | mutex_unlock(&task->perf_event_mutex); |
4133 | ||
4134 | if (unlikely(err)) { | |
9137fb28 | 4135 | put_ctx(ctx); |
dbe08d82 ON |
4136 | |
4137 | if (err == -EAGAIN) | |
4138 | goto retry; | |
4139 | goto errout; | |
a63eaf34 PM |
4140 | } |
4141 | } | |
4142 | ||
4af57ef2 | 4143 | kfree(task_ctx_data); |
0793a61d | 4144 | return ctx; |
c93f7669 | 4145 | |
9ed6060d | 4146 | errout: |
4af57ef2 | 4147 | kfree(task_ctx_data); |
c93f7669 | 4148 | return ERR_PTR(err); |
0793a61d TG |
4149 | } |
4150 | ||
6fb2915d | 4151 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 4152 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 4153 | |
cdd6c482 | 4154 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 4155 | { |
cdd6c482 | 4156 | struct perf_event *event; |
592903cd | 4157 | |
cdd6c482 IM |
4158 | event = container_of(head, struct perf_event, rcu_head); |
4159 | if (event->ns) | |
4160 | put_pid_ns(event->ns); | |
6fb2915d | 4161 | perf_event_free_filter(event); |
cdd6c482 | 4162 | kfree(event); |
592903cd PZ |
4163 | } |
4164 | ||
b69cf536 PZ |
4165 | static void ring_buffer_attach(struct perf_event *event, |
4166 | struct ring_buffer *rb); | |
925d519a | 4167 | |
f2fb6bef KL |
4168 | static void detach_sb_event(struct perf_event *event) |
4169 | { | |
4170 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
4171 | ||
4172 | raw_spin_lock(&pel->lock); | |
4173 | list_del_rcu(&event->sb_list); | |
4174 | raw_spin_unlock(&pel->lock); | |
4175 | } | |
4176 | ||
a4f144eb | 4177 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 4178 | { |
a4f144eb DCC |
4179 | struct perf_event_attr *attr = &event->attr; |
4180 | ||
f2fb6bef | 4181 | if (event->parent) |
a4f144eb | 4182 | return false; |
f2fb6bef KL |
4183 | |
4184 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 4185 | return false; |
f2fb6bef | 4186 | |
a4f144eb DCC |
4187 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
4188 | attr->comm || attr->comm_exec || | |
4189 | attr->task || | |
4190 | attr->context_switch) | |
4191 | return true; | |
4192 | return false; | |
4193 | } | |
4194 | ||
4195 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
4196 | { | |
4197 | if (is_sb_event(event)) | |
4198 | detach_sb_event(event); | |
f2fb6bef KL |
4199 | } |
4200 | ||
4beb31f3 | 4201 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 4202 | { |
4beb31f3 FW |
4203 | if (event->parent) |
4204 | return; | |
4205 | ||
4beb31f3 FW |
4206 | if (is_cgroup_event(event)) |
4207 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
4208 | } | |
925d519a | 4209 | |
555e0c1e FW |
4210 | #ifdef CONFIG_NO_HZ_FULL |
4211 | static DEFINE_SPINLOCK(nr_freq_lock); | |
4212 | #endif | |
4213 | ||
4214 | static void unaccount_freq_event_nohz(void) | |
4215 | { | |
4216 | #ifdef CONFIG_NO_HZ_FULL | |
4217 | spin_lock(&nr_freq_lock); | |
4218 | if (atomic_dec_and_test(&nr_freq_events)) | |
4219 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
4220 | spin_unlock(&nr_freq_lock); | |
4221 | #endif | |
4222 | } | |
4223 | ||
4224 | static void unaccount_freq_event(void) | |
4225 | { | |
4226 | if (tick_nohz_full_enabled()) | |
4227 | unaccount_freq_event_nohz(); | |
4228 | else | |
4229 | atomic_dec(&nr_freq_events); | |
4230 | } | |
4231 | ||
4beb31f3 FW |
4232 | static void unaccount_event(struct perf_event *event) |
4233 | { | |
25432ae9 PZ |
4234 | bool dec = false; |
4235 | ||
4beb31f3 FW |
4236 | if (event->parent) |
4237 | return; | |
4238 | ||
4239 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 4240 | dec = true; |
4beb31f3 FW |
4241 | if (event->attr.mmap || event->attr.mmap_data) |
4242 | atomic_dec(&nr_mmap_events); | |
4243 | if (event->attr.comm) | |
4244 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
4245 | if (event->attr.namespaces) |
4246 | atomic_dec(&nr_namespaces_events); | |
4beb31f3 FW |
4247 | if (event->attr.task) |
4248 | atomic_dec(&nr_task_events); | |
948b26b6 | 4249 | if (event->attr.freq) |
555e0c1e | 4250 | unaccount_freq_event(); |
45ac1403 | 4251 | if (event->attr.context_switch) { |
25432ae9 | 4252 | dec = true; |
45ac1403 AH |
4253 | atomic_dec(&nr_switch_events); |
4254 | } | |
4beb31f3 | 4255 | if (is_cgroup_event(event)) |
25432ae9 | 4256 | dec = true; |
4beb31f3 | 4257 | if (has_branch_stack(event)) |
25432ae9 PZ |
4258 | dec = true; |
4259 | ||
9107c89e PZ |
4260 | if (dec) { |
4261 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
4262 | schedule_delayed_work(&perf_sched_work, HZ); | |
4263 | } | |
4beb31f3 FW |
4264 | |
4265 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
4266 | |
4267 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 4268 | } |
925d519a | 4269 | |
9107c89e PZ |
4270 | static void perf_sched_delayed(struct work_struct *work) |
4271 | { | |
4272 | mutex_lock(&perf_sched_mutex); | |
4273 | if (atomic_dec_and_test(&perf_sched_count)) | |
4274 | static_branch_disable(&perf_sched_events); | |
4275 | mutex_unlock(&perf_sched_mutex); | |
4276 | } | |
4277 | ||
bed5b25a AS |
4278 | /* |
4279 | * The following implement mutual exclusion of events on "exclusive" pmus | |
4280 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
4281 | * at a time, so we disallow creating events that might conflict, namely: | |
4282 | * | |
4283 | * 1) cpu-wide events in the presence of per-task events, | |
4284 | * 2) per-task events in the presence of cpu-wide events, | |
4285 | * 3) two matching events on the same context. | |
4286 | * | |
4287 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 4288 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
4289 | */ |
4290 | static int exclusive_event_init(struct perf_event *event) | |
4291 | { | |
4292 | struct pmu *pmu = event->pmu; | |
4293 | ||
4294 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4295 | return 0; | |
4296 | ||
4297 | /* | |
4298 | * Prevent co-existence of per-task and cpu-wide events on the | |
4299 | * same exclusive pmu. | |
4300 | * | |
4301 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
4302 | * events on this "exclusive" pmu, positive means there are | |
4303 | * per-task events. | |
4304 | * | |
4305 | * Since this is called in perf_event_alloc() path, event::ctx | |
4306 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
4307 | * to mean "per-task event", because unlike other attach states it | |
4308 | * never gets cleared. | |
4309 | */ | |
4310 | if (event->attach_state & PERF_ATTACH_TASK) { | |
4311 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
4312 | return -EBUSY; | |
4313 | } else { | |
4314 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
4315 | return -EBUSY; | |
4316 | } | |
4317 | ||
4318 | return 0; | |
4319 | } | |
4320 | ||
4321 | static void exclusive_event_destroy(struct perf_event *event) | |
4322 | { | |
4323 | struct pmu *pmu = event->pmu; | |
4324 | ||
4325 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4326 | return; | |
4327 | ||
4328 | /* see comment in exclusive_event_init() */ | |
4329 | if (event->attach_state & PERF_ATTACH_TASK) | |
4330 | atomic_dec(&pmu->exclusive_cnt); | |
4331 | else | |
4332 | atomic_inc(&pmu->exclusive_cnt); | |
4333 | } | |
4334 | ||
4335 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
4336 | { | |
3bf6215a | 4337 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
4338 | (e1->cpu == e2->cpu || |
4339 | e1->cpu == -1 || | |
4340 | e2->cpu == -1)) | |
4341 | return true; | |
4342 | return false; | |
4343 | } | |
4344 | ||
4345 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
4346 | static bool exclusive_event_installable(struct perf_event *event, | |
4347 | struct perf_event_context *ctx) | |
4348 | { | |
4349 | struct perf_event *iter_event; | |
4350 | struct pmu *pmu = event->pmu; | |
4351 | ||
4352 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
4353 | return true; | |
4354 | ||
4355 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
4356 | if (exclusive_event_match(iter_event, event)) | |
4357 | return false; | |
4358 | } | |
4359 | ||
4360 | return true; | |
4361 | } | |
4362 | ||
375637bc AS |
4363 | static void perf_addr_filters_splice(struct perf_event *event, |
4364 | struct list_head *head); | |
4365 | ||
683ede43 | 4366 | static void _free_event(struct perf_event *event) |
f1600952 | 4367 | { |
e360adbe | 4368 | irq_work_sync(&event->pending); |
925d519a | 4369 | |
4beb31f3 | 4370 | unaccount_event(event); |
9ee318a7 | 4371 | |
76369139 | 4372 | if (event->rb) { |
9bb5d40c PZ |
4373 | /* |
4374 | * Can happen when we close an event with re-directed output. | |
4375 | * | |
4376 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
4377 | * over us; possibly making our ring_buffer_put() the last. | |
4378 | */ | |
4379 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 4380 | ring_buffer_attach(event, NULL); |
9bb5d40c | 4381 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
4382 | } |
4383 | ||
e5d1367f SE |
4384 | if (is_cgroup_event(event)) |
4385 | perf_detach_cgroup(event); | |
4386 | ||
a0733e69 PZ |
4387 | if (!event->parent) { |
4388 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
4389 | put_callchain_buffers(); | |
4390 | } | |
4391 | ||
4392 | perf_event_free_bpf_prog(event); | |
375637bc AS |
4393 | perf_addr_filters_splice(event, NULL); |
4394 | kfree(event->addr_filters_offs); | |
a0733e69 PZ |
4395 | |
4396 | if (event->destroy) | |
4397 | event->destroy(event); | |
4398 | ||
4399 | if (event->ctx) | |
4400 | put_ctx(event->ctx); | |
4401 | ||
62a92c8f AS |
4402 | exclusive_event_destroy(event); |
4403 | module_put(event->pmu->module); | |
a0733e69 PZ |
4404 | |
4405 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
4406 | } |
4407 | ||
683ede43 PZ |
4408 | /* |
4409 | * Used to free events which have a known refcount of 1, such as in error paths | |
4410 | * where the event isn't exposed yet and inherited events. | |
4411 | */ | |
4412 | static void free_event(struct perf_event *event) | |
0793a61d | 4413 | { |
683ede43 PZ |
4414 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
4415 | "unexpected event refcount: %ld; ptr=%p\n", | |
4416 | atomic_long_read(&event->refcount), event)) { | |
4417 | /* leak to avoid use-after-free */ | |
4418 | return; | |
4419 | } | |
0793a61d | 4420 | |
683ede43 | 4421 | _free_event(event); |
0793a61d TG |
4422 | } |
4423 | ||
a66a3052 | 4424 | /* |
f8697762 | 4425 | * Remove user event from the owner task. |
a66a3052 | 4426 | */ |
f8697762 | 4427 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 4428 | { |
8882135b | 4429 | struct task_struct *owner; |
fb0459d7 | 4430 | |
8882135b | 4431 | rcu_read_lock(); |
8882135b | 4432 | /* |
f47c02c0 PZ |
4433 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
4434 | * observe !owner it means the list deletion is complete and we can | |
4435 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
4436 | * owner->perf_event_mutex. |
4437 | */ | |
506458ef | 4438 | owner = READ_ONCE(event->owner); |
8882135b PZ |
4439 | if (owner) { |
4440 | /* | |
4441 | * Since delayed_put_task_struct() also drops the last | |
4442 | * task reference we can safely take a new reference | |
4443 | * while holding the rcu_read_lock(). | |
4444 | */ | |
4445 | get_task_struct(owner); | |
4446 | } | |
4447 | rcu_read_unlock(); | |
4448 | ||
4449 | if (owner) { | |
f63a8daa PZ |
4450 | /* |
4451 | * If we're here through perf_event_exit_task() we're already | |
4452 | * holding ctx->mutex which would be an inversion wrt. the | |
4453 | * normal lock order. | |
4454 | * | |
4455 | * However we can safely take this lock because its the child | |
4456 | * ctx->mutex. | |
4457 | */ | |
4458 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
4459 | ||
8882135b PZ |
4460 | /* |
4461 | * We have to re-check the event->owner field, if it is cleared | |
4462 | * we raced with perf_event_exit_task(), acquiring the mutex | |
4463 | * ensured they're done, and we can proceed with freeing the | |
4464 | * event. | |
4465 | */ | |
f47c02c0 | 4466 | if (event->owner) { |
8882135b | 4467 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
4468 | smp_store_release(&event->owner, NULL); |
4469 | } | |
8882135b PZ |
4470 | mutex_unlock(&owner->perf_event_mutex); |
4471 | put_task_struct(owner); | |
4472 | } | |
f8697762 JO |
4473 | } |
4474 | ||
f8697762 JO |
4475 | static void put_event(struct perf_event *event) |
4476 | { | |
f8697762 JO |
4477 | if (!atomic_long_dec_and_test(&event->refcount)) |
4478 | return; | |
4479 | ||
c6e5b732 PZ |
4480 | _free_event(event); |
4481 | } | |
4482 | ||
4483 | /* | |
4484 | * Kill an event dead; while event:refcount will preserve the event | |
4485 | * object, it will not preserve its functionality. Once the last 'user' | |
4486 | * gives up the object, we'll destroy the thing. | |
4487 | */ | |
4488 | int perf_event_release_kernel(struct perf_event *event) | |
4489 | { | |
a4f4bb6d | 4490 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 | 4491 | struct perf_event *child, *tmp; |
82d94856 | 4492 | LIST_HEAD(free_list); |
c6e5b732 | 4493 | |
a4f4bb6d PZ |
4494 | /* |
4495 | * If we got here through err_file: fput(event_file); we will not have | |
4496 | * attached to a context yet. | |
4497 | */ | |
4498 | if (!ctx) { | |
4499 | WARN_ON_ONCE(event->attach_state & | |
4500 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
4501 | goto no_ctx; | |
4502 | } | |
4503 | ||
f8697762 JO |
4504 | if (!is_kernel_event(event)) |
4505 | perf_remove_from_owner(event); | |
8882135b | 4506 | |
5fa7c8ec | 4507 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 4508 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 4509 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 4510 | |
a69b0ca4 | 4511 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 4512 | /* |
d8a8cfc7 | 4513 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 4514 | * anymore. |
683ede43 | 4515 | * |
a69b0ca4 PZ |
4516 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4517 | * also see this, most importantly inherit_event() which will avoid | |
4518 | * placing more children on the list. | |
683ede43 | 4519 | * |
c6e5b732 PZ |
4520 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4521 | * child events. | |
683ede43 | 4522 | */ |
a69b0ca4 PZ |
4523 | event->state = PERF_EVENT_STATE_DEAD; |
4524 | raw_spin_unlock_irq(&ctx->lock); | |
4525 | ||
4526 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4527 | |
c6e5b732 PZ |
4528 | again: |
4529 | mutex_lock(&event->child_mutex); | |
4530 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4531 | |
c6e5b732 PZ |
4532 | /* |
4533 | * Cannot change, child events are not migrated, see the | |
4534 | * comment with perf_event_ctx_lock_nested(). | |
4535 | */ | |
506458ef | 4536 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
4537 | /* |
4538 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4539 | * through hoops. We start by grabbing a reference on the ctx. | |
4540 | * | |
4541 | * Since the event cannot get freed while we hold the | |
4542 | * child_mutex, the context must also exist and have a !0 | |
4543 | * reference count. | |
4544 | */ | |
4545 | get_ctx(ctx); | |
4546 | ||
4547 | /* | |
4548 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4549 | * acquire ctx::mutex without fear of it going away. Then we | |
4550 | * can re-acquire child_mutex. | |
4551 | */ | |
4552 | mutex_unlock(&event->child_mutex); | |
4553 | mutex_lock(&ctx->mutex); | |
4554 | mutex_lock(&event->child_mutex); | |
4555 | ||
4556 | /* | |
4557 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4558 | * state, if child is still the first entry, it didn't get freed | |
4559 | * and we can continue doing so. | |
4560 | */ | |
4561 | tmp = list_first_entry_or_null(&event->child_list, | |
4562 | struct perf_event, child_list); | |
4563 | if (tmp == child) { | |
4564 | perf_remove_from_context(child, DETACH_GROUP); | |
82d94856 | 4565 | list_move(&child->child_list, &free_list); |
c6e5b732 PZ |
4566 | /* |
4567 | * This matches the refcount bump in inherit_event(); | |
4568 | * this can't be the last reference. | |
4569 | */ | |
4570 | put_event(event); | |
4571 | } | |
4572 | ||
4573 | mutex_unlock(&event->child_mutex); | |
4574 | mutex_unlock(&ctx->mutex); | |
4575 | put_ctx(ctx); | |
4576 | goto again; | |
4577 | } | |
4578 | mutex_unlock(&event->child_mutex); | |
4579 | ||
82d94856 PZ |
4580 | list_for_each_entry_safe(child, tmp, &free_list, child_list) { |
4581 | list_del(&child->child_list); | |
4582 | free_event(child); | |
4583 | } | |
4584 | ||
a4f4bb6d PZ |
4585 | no_ctx: |
4586 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
4587 | return 0; |
4588 | } | |
4589 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
4590 | ||
8b10c5e2 PZ |
4591 | /* |
4592 | * Called when the last reference to the file is gone. | |
4593 | */ | |
a6fa941d AV |
4594 | static int perf_release(struct inode *inode, struct file *file) |
4595 | { | |
c6e5b732 | 4596 | perf_event_release_kernel(file->private_data); |
a6fa941d | 4597 | return 0; |
fb0459d7 | 4598 | } |
fb0459d7 | 4599 | |
ca0dd44c | 4600 | static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 4601 | { |
cdd6c482 | 4602 | struct perf_event *child; |
e53c0994 PZ |
4603 | u64 total = 0; |
4604 | ||
59ed446f PZ |
4605 | *enabled = 0; |
4606 | *running = 0; | |
4607 | ||
6f10581a | 4608 | mutex_lock(&event->child_mutex); |
01add3ea | 4609 | |
7d88962e | 4610 | (void)perf_event_read(event, false); |
01add3ea SB |
4611 | total += perf_event_count(event); |
4612 | ||
59ed446f PZ |
4613 | *enabled += event->total_time_enabled + |
4614 | atomic64_read(&event->child_total_time_enabled); | |
4615 | *running += event->total_time_running + | |
4616 | atomic64_read(&event->child_total_time_running); | |
4617 | ||
4618 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4619 | (void)perf_event_read(child, false); |
01add3ea | 4620 | total += perf_event_count(child); |
59ed446f PZ |
4621 | *enabled += child->total_time_enabled; |
4622 | *running += child->total_time_running; | |
4623 | } | |
6f10581a | 4624 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4625 | |
4626 | return total; | |
4627 | } | |
ca0dd44c PZ |
4628 | |
4629 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) | |
4630 | { | |
4631 | struct perf_event_context *ctx; | |
4632 | u64 count; | |
4633 | ||
4634 | ctx = perf_event_ctx_lock(event); | |
4635 | count = __perf_event_read_value(event, enabled, running); | |
4636 | perf_event_ctx_unlock(event, ctx); | |
4637 | ||
4638 | return count; | |
4639 | } | |
fb0459d7 | 4640 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4641 | |
7d88962e | 4642 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4643 | u64 read_format, u64 *values) |
3dab77fb | 4644 | { |
2aeb1883 | 4645 | struct perf_event_context *ctx = leader->ctx; |
fa8c2693 | 4646 | struct perf_event *sub; |
2aeb1883 | 4647 | unsigned long flags; |
fa8c2693 | 4648 | int n = 1; /* skip @nr */ |
7d88962e | 4649 | int ret; |
f63a8daa | 4650 | |
7d88962e SB |
4651 | ret = perf_event_read(leader, true); |
4652 | if (ret) | |
4653 | return ret; | |
abf4868b | 4654 | |
a9cd8194 PZ |
4655 | raw_spin_lock_irqsave(&ctx->lock, flags); |
4656 | ||
fa8c2693 PZ |
4657 | /* |
4658 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4659 | * will be identical to those of the leader, so we only publish one | |
4660 | * set. | |
4661 | */ | |
4662 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4663 | values[n++] += leader->total_time_enabled + | |
4664 | atomic64_read(&leader->child_total_time_enabled); | |
4665 | } | |
3dab77fb | 4666 | |
fa8c2693 PZ |
4667 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4668 | values[n++] += leader->total_time_running + | |
4669 | atomic64_read(&leader->child_total_time_running); | |
4670 | } | |
4671 | ||
4672 | /* | |
4673 | * Write {count,id} tuples for every sibling. | |
4674 | */ | |
4675 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4676 | if (read_format & PERF_FORMAT_ID) |
4677 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4678 | |
8343aae6 | 4679 | list_for_each_entry(sub, &leader->sibling_list, sibling_list) { |
fa8c2693 PZ |
4680 | values[n++] += perf_event_count(sub); |
4681 | if (read_format & PERF_FORMAT_ID) | |
4682 | values[n++] = primary_event_id(sub); | |
4683 | } | |
7d88962e | 4684 | |
2aeb1883 | 4685 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
7d88962e | 4686 | return 0; |
fa8c2693 | 4687 | } |
3dab77fb | 4688 | |
fa8c2693 PZ |
4689 | static int perf_read_group(struct perf_event *event, |
4690 | u64 read_format, char __user *buf) | |
4691 | { | |
4692 | struct perf_event *leader = event->group_leader, *child; | |
4693 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4694 | int ret; |
fa8c2693 | 4695 | u64 *values; |
3dab77fb | 4696 | |
fa8c2693 | 4697 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4698 | |
fa8c2693 PZ |
4699 | values = kzalloc(event->read_size, GFP_KERNEL); |
4700 | if (!values) | |
4701 | return -ENOMEM; | |
3dab77fb | 4702 | |
fa8c2693 PZ |
4703 | values[0] = 1 + leader->nr_siblings; |
4704 | ||
4705 | /* | |
4706 | * By locking the child_mutex of the leader we effectively | |
4707 | * lock the child list of all siblings.. XXX explain how. | |
4708 | */ | |
4709 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4710 | |
7d88962e SB |
4711 | ret = __perf_read_group_add(leader, read_format, values); |
4712 | if (ret) | |
4713 | goto unlock; | |
4714 | ||
4715 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4716 | ret = __perf_read_group_add(child, read_format, values); | |
4717 | if (ret) | |
4718 | goto unlock; | |
4719 | } | |
abf4868b | 4720 | |
fa8c2693 | 4721 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4722 | |
7d88962e | 4723 | ret = event->read_size; |
fa8c2693 PZ |
4724 | if (copy_to_user(buf, values, event->read_size)) |
4725 | ret = -EFAULT; | |
7d88962e | 4726 | goto out; |
fa8c2693 | 4727 | |
7d88962e SB |
4728 | unlock: |
4729 | mutex_unlock(&leader->child_mutex); | |
4730 | out: | |
fa8c2693 | 4731 | kfree(values); |
abf4868b | 4732 | return ret; |
3dab77fb PZ |
4733 | } |
4734 | ||
b15f495b | 4735 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4736 | u64 read_format, char __user *buf) |
4737 | { | |
59ed446f | 4738 | u64 enabled, running; |
3dab77fb PZ |
4739 | u64 values[4]; |
4740 | int n = 0; | |
4741 | ||
ca0dd44c | 4742 | values[n++] = __perf_event_read_value(event, &enabled, &running); |
59ed446f PZ |
4743 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
4744 | values[n++] = enabled; | |
4745 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4746 | values[n++] = running; | |
3dab77fb | 4747 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4748 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4749 | |
4750 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4751 | return -EFAULT; | |
4752 | ||
4753 | return n * sizeof(u64); | |
4754 | } | |
4755 | ||
dc633982 JO |
4756 | static bool is_event_hup(struct perf_event *event) |
4757 | { | |
4758 | bool no_children; | |
4759 | ||
a69b0ca4 | 4760 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4761 | return false; |
4762 | ||
4763 | mutex_lock(&event->child_mutex); | |
4764 | no_children = list_empty(&event->child_list); | |
4765 | mutex_unlock(&event->child_mutex); | |
4766 | return no_children; | |
4767 | } | |
4768 | ||
0793a61d | 4769 | /* |
cdd6c482 | 4770 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4771 | */ |
4772 | static ssize_t | |
b15f495b | 4773 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4774 | { |
cdd6c482 | 4775 | u64 read_format = event->attr.read_format; |
3dab77fb | 4776 | int ret; |
0793a61d | 4777 | |
3b6f9e5c | 4778 | /* |
cdd6c482 | 4779 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4780 | * error state (i.e. because it was pinned but it couldn't be |
4781 | * scheduled on to the CPU at some point). | |
4782 | */ | |
cdd6c482 | 4783 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4784 | return 0; |
4785 | ||
c320c7b7 | 4786 | if (count < event->read_size) |
3dab77fb PZ |
4787 | return -ENOSPC; |
4788 | ||
cdd6c482 | 4789 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4790 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4791 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4792 | else |
b15f495b | 4793 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4794 | |
3dab77fb | 4795 | return ret; |
0793a61d TG |
4796 | } |
4797 | ||
0793a61d TG |
4798 | static ssize_t |
4799 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4800 | { | |
cdd6c482 | 4801 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4802 | struct perf_event_context *ctx; |
4803 | int ret; | |
0793a61d | 4804 | |
f63a8daa | 4805 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4806 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4807 | perf_event_ctx_unlock(event, ctx); |
4808 | ||
4809 | return ret; | |
0793a61d TG |
4810 | } |
4811 | ||
9dd95748 | 4812 | static __poll_t perf_poll(struct file *file, poll_table *wait) |
0793a61d | 4813 | { |
cdd6c482 | 4814 | struct perf_event *event = file->private_data; |
76369139 | 4815 | struct ring_buffer *rb; |
a9a08845 | 4816 | __poll_t events = EPOLLHUP; |
c7138f37 | 4817 | |
e708d7ad | 4818 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4819 | |
dc633982 | 4820 | if (is_event_hup(event)) |
179033b3 | 4821 | return events; |
c7138f37 | 4822 | |
10c6db11 | 4823 | /* |
9bb5d40c PZ |
4824 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4825 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4826 | */ |
4827 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4828 | rb = event->rb; |
4829 | if (rb) | |
76369139 | 4830 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4831 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4832 | return events; |
4833 | } | |
4834 | ||
f63a8daa | 4835 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4836 | { |
7d88962e | 4837 | (void)perf_event_read(event, false); |
e7850595 | 4838 | local64_set(&event->count, 0); |
cdd6c482 | 4839 | perf_event_update_userpage(event); |
3df5edad PZ |
4840 | } |
4841 | ||
c93f7669 | 4842 | /* |
cdd6c482 IM |
4843 | * Holding the top-level event's child_mutex means that any |
4844 | * descendant process that has inherited this event will block | |
8ba289b8 | 4845 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4846 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4847 | */ |
cdd6c482 IM |
4848 | static void perf_event_for_each_child(struct perf_event *event, |
4849 | void (*func)(struct perf_event *)) | |
3df5edad | 4850 | { |
cdd6c482 | 4851 | struct perf_event *child; |
3df5edad | 4852 | |
cdd6c482 | 4853 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4854 | |
cdd6c482 IM |
4855 | mutex_lock(&event->child_mutex); |
4856 | func(event); | |
4857 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4858 | func(child); |
cdd6c482 | 4859 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4860 | } |
4861 | ||
cdd6c482 IM |
4862 | static void perf_event_for_each(struct perf_event *event, |
4863 | void (*func)(struct perf_event *)) | |
3df5edad | 4864 | { |
cdd6c482 IM |
4865 | struct perf_event_context *ctx = event->ctx; |
4866 | struct perf_event *sibling; | |
3df5edad | 4867 | |
f63a8daa PZ |
4868 | lockdep_assert_held(&ctx->mutex); |
4869 | ||
cdd6c482 | 4870 | event = event->group_leader; |
75f937f2 | 4871 | |
cdd6c482 | 4872 | perf_event_for_each_child(event, func); |
8343aae6 | 4873 | list_for_each_entry(sibling, &event->sibling_list, sibling_list) |
724b6daa | 4874 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4875 | } |
4876 | ||
fae3fde6 PZ |
4877 | static void __perf_event_period(struct perf_event *event, |
4878 | struct perf_cpu_context *cpuctx, | |
4879 | struct perf_event_context *ctx, | |
4880 | void *info) | |
c7999c6f | 4881 | { |
fae3fde6 | 4882 | u64 value = *((u64 *)info); |
c7999c6f | 4883 | bool active; |
08247e31 | 4884 | |
cdd6c482 | 4885 | if (event->attr.freq) { |
cdd6c482 | 4886 | event->attr.sample_freq = value; |
08247e31 | 4887 | } else { |
cdd6c482 IM |
4888 | event->attr.sample_period = value; |
4889 | event->hw.sample_period = value; | |
08247e31 | 4890 | } |
bad7192b PZ |
4891 | |
4892 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4893 | if (active) { | |
4894 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4895 | /* |
4896 | * We could be throttled; unthrottle now to avoid the tick | |
4897 | * trying to unthrottle while we already re-started the event. | |
4898 | */ | |
4899 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4900 | event->hw.interrupts = 0; | |
4901 | perf_log_throttle(event, 1); | |
4902 | } | |
bad7192b PZ |
4903 | event->pmu->stop(event, PERF_EF_UPDATE); |
4904 | } | |
4905 | ||
4906 | local64_set(&event->hw.period_left, 0); | |
4907 | ||
4908 | if (active) { | |
4909 | event->pmu->start(event, PERF_EF_RELOAD); | |
4910 | perf_pmu_enable(ctx->pmu); | |
4911 | } | |
c7999c6f PZ |
4912 | } |
4913 | ||
4914 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4915 | { | |
c7999c6f PZ |
4916 | u64 value; |
4917 | ||
4918 | if (!is_sampling_event(event)) | |
4919 | return -EINVAL; | |
4920 | ||
4921 | if (copy_from_user(&value, arg, sizeof(value))) | |
4922 | return -EFAULT; | |
4923 | ||
4924 | if (!value) | |
4925 | return -EINVAL; | |
4926 | ||
4927 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4928 | return -EINVAL; | |
4929 | ||
fae3fde6 | 4930 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4931 | |
c7999c6f | 4932 | return 0; |
08247e31 PZ |
4933 | } |
4934 | ||
ac9721f3 PZ |
4935 | static const struct file_operations perf_fops; |
4936 | ||
2903ff01 | 4937 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4938 | { |
2903ff01 AV |
4939 | struct fd f = fdget(fd); |
4940 | if (!f.file) | |
4941 | return -EBADF; | |
ac9721f3 | 4942 | |
2903ff01 AV |
4943 | if (f.file->f_op != &perf_fops) { |
4944 | fdput(f); | |
4945 | return -EBADF; | |
ac9721f3 | 4946 | } |
2903ff01 AV |
4947 | *p = f; |
4948 | return 0; | |
ac9721f3 PZ |
4949 | } |
4950 | ||
4951 | static int perf_event_set_output(struct perf_event *event, | |
4952 | struct perf_event *output_event); | |
6fb2915d | 4953 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4954 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4955 | |
f63a8daa | 4956 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4957 | { |
cdd6c482 | 4958 | void (*func)(struct perf_event *); |
3df5edad | 4959 | u32 flags = arg; |
d859e29f PM |
4960 | |
4961 | switch (cmd) { | |
cdd6c482 | 4962 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4963 | func = _perf_event_enable; |
d859e29f | 4964 | break; |
cdd6c482 | 4965 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4966 | func = _perf_event_disable; |
79f14641 | 4967 | break; |
cdd6c482 | 4968 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4969 | func = _perf_event_reset; |
6de6a7b9 | 4970 | break; |
3df5edad | 4971 | |
cdd6c482 | 4972 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4973 | return _perf_event_refresh(event, arg); |
08247e31 | 4974 | |
cdd6c482 IM |
4975 | case PERF_EVENT_IOC_PERIOD: |
4976 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4977 | |
cf4957f1 JO |
4978 | case PERF_EVENT_IOC_ID: |
4979 | { | |
4980 | u64 id = primary_event_id(event); | |
4981 | ||
4982 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4983 | return -EFAULT; | |
4984 | return 0; | |
4985 | } | |
4986 | ||
cdd6c482 | 4987 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4988 | { |
ac9721f3 | 4989 | int ret; |
ac9721f3 | 4990 | if (arg != -1) { |
2903ff01 AV |
4991 | struct perf_event *output_event; |
4992 | struct fd output; | |
4993 | ret = perf_fget_light(arg, &output); | |
4994 | if (ret) | |
4995 | return ret; | |
4996 | output_event = output.file->private_data; | |
4997 | ret = perf_event_set_output(event, output_event); | |
4998 | fdput(output); | |
4999 | } else { | |
5000 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 5001 | } |
ac9721f3 PZ |
5002 | return ret; |
5003 | } | |
a4be7c27 | 5004 | |
6fb2915d LZ |
5005 | case PERF_EVENT_IOC_SET_FILTER: |
5006 | return perf_event_set_filter(event, (void __user *)arg); | |
5007 | ||
2541517c AS |
5008 | case PERF_EVENT_IOC_SET_BPF: |
5009 | return perf_event_set_bpf_prog(event, arg); | |
5010 | ||
86e7972f WN |
5011 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
5012 | struct ring_buffer *rb; | |
5013 | ||
5014 | rcu_read_lock(); | |
5015 | rb = rcu_dereference(event->rb); | |
5016 | if (!rb || !rb->nr_pages) { | |
5017 | rcu_read_unlock(); | |
5018 | return -EINVAL; | |
5019 | } | |
5020 | rb_toggle_paused(rb, !!arg); | |
5021 | rcu_read_unlock(); | |
5022 | return 0; | |
5023 | } | |
f371b304 YS |
5024 | |
5025 | case PERF_EVENT_IOC_QUERY_BPF: | |
f4e2298e | 5026 | return perf_event_query_prog_array(event, (void __user *)arg); |
d859e29f | 5027 | default: |
3df5edad | 5028 | return -ENOTTY; |
d859e29f | 5029 | } |
3df5edad PZ |
5030 | |
5031 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 5032 | perf_event_for_each(event, func); |
3df5edad | 5033 | else |
cdd6c482 | 5034 | perf_event_for_each_child(event, func); |
3df5edad PZ |
5035 | |
5036 | return 0; | |
d859e29f PM |
5037 | } |
5038 | ||
f63a8daa PZ |
5039 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
5040 | { | |
5041 | struct perf_event *event = file->private_data; | |
5042 | struct perf_event_context *ctx; | |
5043 | long ret; | |
5044 | ||
5045 | ctx = perf_event_ctx_lock(event); | |
5046 | ret = _perf_ioctl(event, cmd, arg); | |
5047 | perf_event_ctx_unlock(event, ctx); | |
5048 | ||
5049 | return ret; | |
5050 | } | |
5051 | ||
b3f20785 PM |
5052 | #ifdef CONFIG_COMPAT |
5053 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
5054 | unsigned long arg) | |
5055 | { | |
5056 | switch (_IOC_NR(cmd)) { | |
5057 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
5058 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
5059 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
5060 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
5061 | cmd &= ~IOCSIZE_MASK; | |
5062 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
5063 | } | |
5064 | break; | |
5065 | } | |
5066 | return perf_ioctl(file, cmd, arg); | |
5067 | } | |
5068 | #else | |
5069 | # define perf_compat_ioctl NULL | |
5070 | #endif | |
5071 | ||
cdd6c482 | 5072 | int perf_event_task_enable(void) |
771d7cde | 5073 | { |
f63a8daa | 5074 | struct perf_event_context *ctx; |
cdd6c482 | 5075 | struct perf_event *event; |
771d7cde | 5076 | |
cdd6c482 | 5077 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
5078 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
5079 | ctx = perf_event_ctx_lock(event); | |
5080 | perf_event_for_each_child(event, _perf_event_enable); | |
5081 | perf_event_ctx_unlock(event, ctx); | |
5082 | } | |
cdd6c482 | 5083 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
5084 | |
5085 | return 0; | |
5086 | } | |
5087 | ||
cdd6c482 | 5088 | int perf_event_task_disable(void) |
771d7cde | 5089 | { |
f63a8daa | 5090 | struct perf_event_context *ctx; |
cdd6c482 | 5091 | struct perf_event *event; |
771d7cde | 5092 | |
cdd6c482 | 5093 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
5094 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
5095 | ctx = perf_event_ctx_lock(event); | |
5096 | perf_event_for_each_child(event, _perf_event_disable); | |
5097 | perf_event_ctx_unlock(event, ctx); | |
5098 | } | |
cdd6c482 | 5099 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
5100 | |
5101 | return 0; | |
5102 | } | |
5103 | ||
cdd6c482 | 5104 | static int perf_event_index(struct perf_event *event) |
194002b2 | 5105 | { |
a4eaf7f1 PZ |
5106 | if (event->hw.state & PERF_HES_STOPPED) |
5107 | return 0; | |
5108 | ||
cdd6c482 | 5109 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
5110 | return 0; |
5111 | ||
35edc2a5 | 5112 | return event->pmu->event_idx(event); |
194002b2 PZ |
5113 | } |
5114 | ||
c4794295 | 5115 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 5116 | u64 *now, |
7f310a5d EM |
5117 | u64 *enabled, |
5118 | u64 *running) | |
c4794295 | 5119 | { |
e3f3541c | 5120 | u64 ctx_time; |
c4794295 | 5121 | |
e3f3541c PZ |
5122 | *now = perf_clock(); |
5123 | ctx_time = event->shadow_ctx_time + *now; | |
0d3d73aa | 5124 | __perf_update_times(event, ctx_time, enabled, running); |
c4794295 EM |
5125 | } |
5126 | ||
fa731587 PZ |
5127 | static void perf_event_init_userpage(struct perf_event *event) |
5128 | { | |
5129 | struct perf_event_mmap_page *userpg; | |
5130 | struct ring_buffer *rb; | |
5131 | ||
5132 | rcu_read_lock(); | |
5133 | rb = rcu_dereference(event->rb); | |
5134 | if (!rb) | |
5135 | goto unlock; | |
5136 | ||
5137 | userpg = rb->user_page; | |
5138 | ||
5139 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
5140 | userpg->cap_bit0_is_deprecated = 1; | |
5141 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
5142 | userpg->data_offset = PAGE_SIZE; |
5143 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
5144 | |
5145 | unlock: | |
5146 | rcu_read_unlock(); | |
5147 | } | |
5148 | ||
c1317ec2 AL |
5149 | void __weak arch_perf_update_userpage( |
5150 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
5151 | { |
5152 | } | |
5153 | ||
38ff667b PZ |
5154 | /* |
5155 | * Callers need to ensure there can be no nesting of this function, otherwise | |
5156 | * the seqlock logic goes bad. We can not serialize this because the arch | |
5157 | * code calls this from NMI context. | |
5158 | */ | |
cdd6c482 | 5159 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 5160 | { |
cdd6c482 | 5161 | struct perf_event_mmap_page *userpg; |
76369139 | 5162 | struct ring_buffer *rb; |
e3f3541c | 5163 | u64 enabled, running, now; |
38ff667b PZ |
5164 | |
5165 | rcu_read_lock(); | |
5ec4c599 PZ |
5166 | rb = rcu_dereference(event->rb); |
5167 | if (!rb) | |
5168 | goto unlock; | |
5169 | ||
0d641208 EM |
5170 | /* |
5171 | * compute total_time_enabled, total_time_running | |
5172 | * based on snapshot values taken when the event | |
5173 | * was last scheduled in. | |
5174 | * | |
5175 | * we cannot simply called update_context_time() | |
5176 | * because of locking issue as we can be called in | |
5177 | * NMI context | |
5178 | */ | |
e3f3541c | 5179 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 5180 | |
76369139 | 5181 | userpg = rb->user_page; |
7b732a75 PZ |
5182 | /* |
5183 | * Disable preemption so as to not let the corresponding user-space | |
5184 | * spin too long if we get preempted. | |
5185 | */ | |
5186 | preempt_disable(); | |
37d81828 | 5187 | ++userpg->lock; |
92f22a38 | 5188 | barrier(); |
cdd6c482 | 5189 | userpg->index = perf_event_index(event); |
b5e58793 | 5190 | userpg->offset = perf_event_count(event); |
365a4038 | 5191 | if (userpg->index) |
e7850595 | 5192 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 5193 | |
0d641208 | 5194 | userpg->time_enabled = enabled + |
cdd6c482 | 5195 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 5196 | |
0d641208 | 5197 | userpg->time_running = running + |
cdd6c482 | 5198 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 5199 | |
c1317ec2 | 5200 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 5201 | |
92f22a38 | 5202 | barrier(); |
37d81828 | 5203 | ++userpg->lock; |
7b732a75 | 5204 | preempt_enable(); |
38ff667b | 5205 | unlock: |
7b732a75 | 5206 | rcu_read_unlock(); |
37d81828 | 5207 | } |
82975c46 | 5208 | EXPORT_SYMBOL_GPL(perf_event_update_userpage); |
37d81828 | 5209 | |
11bac800 | 5210 | static int perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 5211 | { |
11bac800 | 5212 | struct perf_event *event = vmf->vma->vm_file->private_data; |
76369139 | 5213 | struct ring_buffer *rb; |
906010b2 PZ |
5214 | int ret = VM_FAULT_SIGBUS; |
5215 | ||
5216 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
5217 | if (vmf->pgoff == 0) | |
5218 | ret = 0; | |
5219 | return ret; | |
5220 | } | |
5221 | ||
5222 | rcu_read_lock(); | |
76369139 FW |
5223 | rb = rcu_dereference(event->rb); |
5224 | if (!rb) | |
906010b2 PZ |
5225 | goto unlock; |
5226 | ||
5227 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
5228 | goto unlock; | |
5229 | ||
76369139 | 5230 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
5231 | if (!vmf->page) |
5232 | goto unlock; | |
5233 | ||
5234 | get_page(vmf->page); | |
11bac800 | 5235 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
5236 | vmf->page->index = vmf->pgoff; |
5237 | ||
5238 | ret = 0; | |
5239 | unlock: | |
5240 | rcu_read_unlock(); | |
5241 | ||
5242 | return ret; | |
5243 | } | |
5244 | ||
10c6db11 PZ |
5245 | static void ring_buffer_attach(struct perf_event *event, |
5246 | struct ring_buffer *rb) | |
5247 | { | |
b69cf536 | 5248 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
5249 | unsigned long flags; |
5250 | ||
b69cf536 PZ |
5251 | if (event->rb) { |
5252 | /* | |
5253 | * Should be impossible, we set this when removing | |
5254 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
5255 | */ | |
5256 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 5257 | |
b69cf536 | 5258 | old_rb = event->rb; |
b69cf536 PZ |
5259 | spin_lock_irqsave(&old_rb->event_lock, flags); |
5260 | list_del_rcu(&event->rb_entry); | |
5261 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 5262 | |
2f993cf0 ON |
5263 | event->rcu_batches = get_state_synchronize_rcu(); |
5264 | event->rcu_pending = 1; | |
b69cf536 | 5265 | } |
10c6db11 | 5266 | |
b69cf536 | 5267 | if (rb) { |
2f993cf0 ON |
5268 | if (event->rcu_pending) { |
5269 | cond_synchronize_rcu(event->rcu_batches); | |
5270 | event->rcu_pending = 0; | |
5271 | } | |
5272 | ||
b69cf536 PZ |
5273 | spin_lock_irqsave(&rb->event_lock, flags); |
5274 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
5275 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
5276 | } | |
5277 | ||
767ae086 AS |
5278 | /* |
5279 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
5280 | * before swizzling the event::rb pointer; if it's getting | |
5281 | * unmapped, its aux_mmap_count will be 0 and it won't | |
5282 | * restart. See the comment in __perf_pmu_output_stop(). | |
5283 | * | |
5284 | * Data will inevitably be lost when set_output is done in | |
5285 | * mid-air, but then again, whoever does it like this is | |
5286 | * not in for the data anyway. | |
5287 | */ | |
5288 | if (has_aux(event)) | |
5289 | perf_event_stop(event, 0); | |
5290 | ||
b69cf536 PZ |
5291 | rcu_assign_pointer(event->rb, rb); |
5292 | ||
5293 | if (old_rb) { | |
5294 | ring_buffer_put(old_rb); | |
5295 | /* | |
5296 | * Since we detached before setting the new rb, so that we | |
5297 | * could attach the new rb, we could have missed a wakeup. | |
5298 | * Provide it now. | |
5299 | */ | |
5300 | wake_up_all(&event->waitq); | |
5301 | } | |
10c6db11 PZ |
5302 | } |
5303 | ||
5304 | static void ring_buffer_wakeup(struct perf_event *event) | |
5305 | { | |
5306 | struct ring_buffer *rb; | |
5307 | ||
5308 | rcu_read_lock(); | |
5309 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
5310 | if (rb) { |
5311 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
5312 | wake_up_all(&event->waitq); | |
5313 | } | |
10c6db11 PZ |
5314 | rcu_read_unlock(); |
5315 | } | |
5316 | ||
fdc26706 | 5317 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 5318 | { |
76369139 | 5319 | struct ring_buffer *rb; |
7b732a75 | 5320 | |
ac9721f3 | 5321 | rcu_read_lock(); |
76369139 FW |
5322 | rb = rcu_dereference(event->rb); |
5323 | if (rb) { | |
5324 | if (!atomic_inc_not_zero(&rb->refcount)) | |
5325 | rb = NULL; | |
ac9721f3 PZ |
5326 | } |
5327 | rcu_read_unlock(); | |
5328 | ||
76369139 | 5329 | return rb; |
ac9721f3 PZ |
5330 | } |
5331 | ||
fdc26706 | 5332 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 5333 | { |
76369139 | 5334 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 5335 | return; |
7b732a75 | 5336 | |
9bb5d40c | 5337 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 5338 | |
76369139 | 5339 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
5340 | } |
5341 | ||
5342 | static void perf_mmap_open(struct vm_area_struct *vma) | |
5343 | { | |
cdd6c482 | 5344 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5345 | |
cdd6c482 | 5346 | atomic_inc(&event->mmap_count); |
9bb5d40c | 5347 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 5348 | |
45bfb2e5 PZ |
5349 | if (vma->vm_pgoff) |
5350 | atomic_inc(&event->rb->aux_mmap_count); | |
5351 | ||
1e0fb9ec | 5352 | if (event->pmu->event_mapped) |
bfe33492 | 5353 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
5354 | } |
5355 | ||
95ff4ca2 AS |
5356 | static void perf_pmu_output_stop(struct perf_event *event); |
5357 | ||
9bb5d40c PZ |
5358 | /* |
5359 | * A buffer can be mmap()ed multiple times; either directly through the same | |
5360 | * event, or through other events by use of perf_event_set_output(). | |
5361 | * | |
5362 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
5363 | * the buffer here, where we still have a VM context. This means we need | |
5364 | * to detach all events redirecting to us. | |
5365 | */ | |
7b732a75 PZ |
5366 | static void perf_mmap_close(struct vm_area_struct *vma) |
5367 | { | |
cdd6c482 | 5368 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5369 | |
b69cf536 | 5370 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
5371 | struct user_struct *mmap_user = rb->mmap_user; |
5372 | int mmap_locked = rb->mmap_locked; | |
5373 | unsigned long size = perf_data_size(rb); | |
789f90fc | 5374 | |
1e0fb9ec | 5375 | if (event->pmu->event_unmapped) |
bfe33492 | 5376 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 5377 | |
45bfb2e5 PZ |
5378 | /* |
5379 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
5380 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
5381 | * serialize with perf_mmap here. | |
5382 | */ | |
5383 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
5384 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
5385 | /* |
5386 | * Stop all AUX events that are writing to this buffer, | |
5387 | * so that we can free its AUX pages and corresponding PMU | |
5388 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
5389 | * they won't start any more (see perf_aux_output_begin()). | |
5390 | */ | |
5391 | perf_pmu_output_stop(event); | |
5392 | ||
5393 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
5394 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
5395 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
5396 | ||
95ff4ca2 | 5397 | /* this has to be the last one */ |
45bfb2e5 | 5398 | rb_free_aux(rb); |
95ff4ca2 AS |
5399 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
5400 | ||
45bfb2e5 PZ |
5401 | mutex_unlock(&event->mmap_mutex); |
5402 | } | |
5403 | ||
9bb5d40c PZ |
5404 | atomic_dec(&rb->mmap_count); |
5405 | ||
5406 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 5407 | goto out_put; |
9bb5d40c | 5408 | |
b69cf536 | 5409 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
5410 | mutex_unlock(&event->mmap_mutex); |
5411 | ||
5412 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
5413 | if (atomic_read(&rb->mmap_count)) |
5414 | goto out_put; | |
ac9721f3 | 5415 | |
9bb5d40c PZ |
5416 | /* |
5417 | * No other mmap()s, detach from all other events that might redirect | |
5418 | * into the now unreachable buffer. Somewhat complicated by the | |
5419 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
5420 | */ | |
5421 | again: | |
5422 | rcu_read_lock(); | |
5423 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
5424 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
5425 | /* | |
5426 | * This event is en-route to free_event() which will | |
5427 | * detach it and remove it from the list. | |
5428 | */ | |
5429 | continue; | |
5430 | } | |
5431 | rcu_read_unlock(); | |
789f90fc | 5432 | |
9bb5d40c PZ |
5433 | mutex_lock(&event->mmap_mutex); |
5434 | /* | |
5435 | * Check we didn't race with perf_event_set_output() which can | |
5436 | * swizzle the rb from under us while we were waiting to | |
5437 | * acquire mmap_mutex. | |
5438 | * | |
5439 | * If we find a different rb; ignore this event, a next | |
5440 | * iteration will no longer find it on the list. We have to | |
5441 | * still restart the iteration to make sure we're not now | |
5442 | * iterating the wrong list. | |
5443 | */ | |
b69cf536 PZ |
5444 | if (event->rb == rb) |
5445 | ring_buffer_attach(event, NULL); | |
5446 | ||
cdd6c482 | 5447 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 5448 | put_event(event); |
ac9721f3 | 5449 | |
9bb5d40c PZ |
5450 | /* |
5451 | * Restart the iteration; either we're on the wrong list or | |
5452 | * destroyed its integrity by doing a deletion. | |
5453 | */ | |
5454 | goto again; | |
7b732a75 | 5455 | } |
9bb5d40c PZ |
5456 | rcu_read_unlock(); |
5457 | ||
5458 | /* | |
5459 | * It could be there's still a few 0-ref events on the list; they'll | |
5460 | * get cleaned up by free_event() -- they'll also still have their | |
5461 | * ref on the rb and will free it whenever they are done with it. | |
5462 | * | |
5463 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
5464 | * undo the VM accounting. | |
5465 | */ | |
5466 | ||
5467 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
5468 | vma->vm_mm->pinned_vm -= mmap_locked; | |
5469 | free_uid(mmap_user); | |
5470 | ||
b69cf536 | 5471 | out_put: |
9bb5d40c | 5472 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
5473 | } |
5474 | ||
f0f37e2f | 5475 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 5476 | .open = perf_mmap_open, |
45bfb2e5 | 5477 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
5478 | .fault = perf_mmap_fault, |
5479 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
5480 | }; |
5481 | ||
5482 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
5483 | { | |
cdd6c482 | 5484 | struct perf_event *event = file->private_data; |
22a4f650 | 5485 | unsigned long user_locked, user_lock_limit; |
789f90fc | 5486 | struct user_struct *user = current_user(); |
22a4f650 | 5487 | unsigned long locked, lock_limit; |
45bfb2e5 | 5488 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
5489 | unsigned long vma_size; |
5490 | unsigned long nr_pages; | |
45bfb2e5 | 5491 | long user_extra = 0, extra = 0; |
d57e34fd | 5492 | int ret = 0, flags = 0; |
37d81828 | 5493 | |
c7920614 PZ |
5494 | /* |
5495 | * Don't allow mmap() of inherited per-task counters. This would | |
5496 | * create a performance issue due to all children writing to the | |
76369139 | 5497 | * same rb. |
c7920614 PZ |
5498 | */ |
5499 | if (event->cpu == -1 && event->attr.inherit) | |
5500 | return -EINVAL; | |
5501 | ||
43a21ea8 | 5502 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 5503 | return -EINVAL; |
7b732a75 PZ |
5504 | |
5505 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
5506 | |
5507 | if (vma->vm_pgoff == 0) { | |
5508 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
5509 | } else { | |
5510 | /* | |
5511 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
5512 | * mapped, all subsequent mappings should have the same size | |
5513 | * and offset. Must be above the normal perf buffer. | |
5514 | */ | |
5515 | u64 aux_offset, aux_size; | |
5516 | ||
5517 | if (!event->rb) | |
5518 | return -EINVAL; | |
5519 | ||
5520 | nr_pages = vma_size / PAGE_SIZE; | |
5521 | ||
5522 | mutex_lock(&event->mmap_mutex); | |
5523 | ret = -EINVAL; | |
5524 | ||
5525 | rb = event->rb; | |
5526 | if (!rb) | |
5527 | goto aux_unlock; | |
5528 | ||
6aa7de05 MR |
5529 | aux_offset = READ_ONCE(rb->user_page->aux_offset); |
5530 | aux_size = READ_ONCE(rb->user_page->aux_size); | |
45bfb2e5 PZ |
5531 | |
5532 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
5533 | goto aux_unlock; | |
5534 | ||
5535 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
5536 | goto aux_unlock; | |
5537 | ||
5538 | /* already mapped with a different offset */ | |
5539 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
5540 | goto aux_unlock; | |
5541 | ||
5542 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
5543 | goto aux_unlock; | |
5544 | ||
5545 | /* already mapped with a different size */ | |
5546 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
5547 | goto aux_unlock; | |
5548 | ||
5549 | if (!is_power_of_2(nr_pages)) | |
5550 | goto aux_unlock; | |
5551 | ||
5552 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
5553 | goto aux_unlock; | |
5554 | ||
5555 | if (rb_has_aux(rb)) { | |
5556 | atomic_inc(&rb->aux_mmap_count); | |
5557 | ret = 0; | |
5558 | goto unlock; | |
5559 | } | |
5560 | ||
5561 | atomic_set(&rb->aux_mmap_count, 1); | |
5562 | user_extra = nr_pages; | |
5563 | ||
5564 | goto accounting; | |
5565 | } | |
7b732a75 | 5566 | |
7730d865 | 5567 | /* |
76369139 | 5568 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
5569 | * can do bitmasks instead of modulo. |
5570 | */ | |
2ed11312 | 5571 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
5572 | return -EINVAL; |
5573 | ||
7b732a75 | 5574 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
5575 | return -EINVAL; |
5576 | ||
cdd6c482 | 5577 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 5578 | again: |
cdd6c482 | 5579 | mutex_lock(&event->mmap_mutex); |
76369139 | 5580 | if (event->rb) { |
9bb5d40c | 5581 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 5582 | ret = -EINVAL; |
9bb5d40c PZ |
5583 | goto unlock; |
5584 | } | |
5585 | ||
5586 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
5587 | /* | |
5588 | * Raced against perf_mmap_close() through | |
5589 | * perf_event_set_output(). Try again, hope for better | |
5590 | * luck. | |
5591 | */ | |
5592 | mutex_unlock(&event->mmap_mutex); | |
5593 | goto again; | |
5594 | } | |
5595 | ||
ebb3c4c4 PZ |
5596 | goto unlock; |
5597 | } | |
5598 | ||
789f90fc | 5599 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
5600 | |
5601 | accounting: | |
cdd6c482 | 5602 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
5603 | |
5604 | /* | |
5605 | * Increase the limit linearly with more CPUs: | |
5606 | */ | |
5607 | user_lock_limit *= num_online_cpus(); | |
5608 | ||
789f90fc | 5609 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 5610 | |
789f90fc PZ |
5611 | if (user_locked > user_lock_limit) |
5612 | extra = user_locked - user_lock_limit; | |
7b732a75 | 5613 | |
78d7d407 | 5614 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 5615 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 5616 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 5617 | |
459ec28a IM |
5618 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
5619 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
5620 | ret = -EPERM; |
5621 | goto unlock; | |
5622 | } | |
7b732a75 | 5623 | |
45bfb2e5 | 5624 | WARN_ON(!rb && event->rb); |
906010b2 | 5625 | |
d57e34fd | 5626 | if (vma->vm_flags & VM_WRITE) |
76369139 | 5627 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 5628 | |
76369139 | 5629 | if (!rb) { |
45bfb2e5 PZ |
5630 | rb = rb_alloc(nr_pages, |
5631 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
5632 | event->cpu, flags); | |
26cb63ad | 5633 | |
45bfb2e5 PZ |
5634 | if (!rb) { |
5635 | ret = -ENOMEM; | |
5636 | goto unlock; | |
5637 | } | |
43a21ea8 | 5638 | |
45bfb2e5 PZ |
5639 | atomic_set(&rb->mmap_count, 1); |
5640 | rb->mmap_user = get_current_user(); | |
5641 | rb->mmap_locked = extra; | |
26cb63ad | 5642 | |
45bfb2e5 | 5643 | ring_buffer_attach(event, rb); |
ac9721f3 | 5644 | |
45bfb2e5 PZ |
5645 | perf_event_init_userpage(event); |
5646 | perf_event_update_userpage(event); | |
5647 | } else { | |
1a594131 AS |
5648 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5649 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5650 | if (!ret) |
5651 | rb->aux_mmap_locked = extra; | |
5652 | } | |
9a0f05cb | 5653 | |
ebb3c4c4 | 5654 | unlock: |
45bfb2e5 PZ |
5655 | if (!ret) { |
5656 | atomic_long_add(user_extra, &user->locked_vm); | |
5657 | vma->vm_mm->pinned_vm += extra; | |
5658 | ||
ac9721f3 | 5659 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5660 | } else if (rb) { |
5661 | atomic_dec(&rb->mmap_count); | |
5662 | } | |
5663 | aux_unlock: | |
cdd6c482 | 5664 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5665 | |
9bb5d40c PZ |
5666 | /* |
5667 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5668 | * vma. | |
5669 | */ | |
26cb63ad | 5670 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5671 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5672 | |
1e0fb9ec | 5673 | if (event->pmu->event_mapped) |
bfe33492 | 5674 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 5675 | |
7b732a75 | 5676 | return ret; |
37d81828 PM |
5677 | } |
5678 | ||
3c446b3d PZ |
5679 | static int perf_fasync(int fd, struct file *filp, int on) |
5680 | { | |
496ad9aa | 5681 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5682 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5683 | int retval; |
5684 | ||
5955102c | 5685 | inode_lock(inode); |
cdd6c482 | 5686 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5687 | inode_unlock(inode); |
3c446b3d PZ |
5688 | |
5689 | if (retval < 0) | |
5690 | return retval; | |
5691 | ||
5692 | return 0; | |
5693 | } | |
5694 | ||
0793a61d | 5695 | static const struct file_operations perf_fops = { |
3326c1ce | 5696 | .llseek = no_llseek, |
0793a61d TG |
5697 | .release = perf_release, |
5698 | .read = perf_read, | |
5699 | .poll = perf_poll, | |
d859e29f | 5700 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5701 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5702 | .mmap = perf_mmap, |
3c446b3d | 5703 | .fasync = perf_fasync, |
0793a61d TG |
5704 | }; |
5705 | ||
925d519a | 5706 | /* |
cdd6c482 | 5707 | * Perf event wakeup |
925d519a PZ |
5708 | * |
5709 | * If there's data, ensure we set the poll() state and publish everything | |
5710 | * to user-space before waking everybody up. | |
5711 | */ | |
5712 | ||
fed66e2c PZ |
5713 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5714 | { | |
5715 | /* only the parent has fasync state */ | |
5716 | if (event->parent) | |
5717 | event = event->parent; | |
5718 | return &event->fasync; | |
5719 | } | |
5720 | ||
cdd6c482 | 5721 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5722 | { |
10c6db11 | 5723 | ring_buffer_wakeup(event); |
4c9e2542 | 5724 | |
cdd6c482 | 5725 | if (event->pending_kill) { |
fed66e2c | 5726 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5727 | event->pending_kill = 0; |
4c9e2542 | 5728 | } |
925d519a PZ |
5729 | } |
5730 | ||
e360adbe | 5731 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5732 | { |
cdd6c482 IM |
5733 | struct perf_event *event = container_of(entry, |
5734 | struct perf_event, pending); | |
d525211f PZ |
5735 | int rctx; |
5736 | ||
5737 | rctx = perf_swevent_get_recursion_context(); | |
5738 | /* | |
5739 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5740 | * and we won't recurse 'further'. | |
5741 | */ | |
79f14641 | 5742 | |
cdd6c482 IM |
5743 | if (event->pending_disable) { |
5744 | event->pending_disable = 0; | |
fae3fde6 | 5745 | perf_event_disable_local(event); |
79f14641 PZ |
5746 | } |
5747 | ||
cdd6c482 IM |
5748 | if (event->pending_wakeup) { |
5749 | event->pending_wakeup = 0; | |
5750 | perf_event_wakeup(event); | |
79f14641 | 5751 | } |
d525211f PZ |
5752 | |
5753 | if (rctx >= 0) | |
5754 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5755 | } |
5756 | ||
39447b38 ZY |
5757 | /* |
5758 | * We assume there is only KVM supporting the callbacks. | |
5759 | * Later on, we might change it to a list if there is | |
5760 | * another virtualization implementation supporting the callbacks. | |
5761 | */ | |
5762 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5763 | ||
5764 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5765 | { | |
5766 | perf_guest_cbs = cbs; | |
5767 | return 0; | |
5768 | } | |
5769 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5770 | ||
5771 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5772 | { | |
5773 | perf_guest_cbs = NULL; | |
5774 | return 0; | |
5775 | } | |
5776 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5777 | ||
4018994f JO |
5778 | static void |
5779 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5780 | struct pt_regs *regs, u64 mask) | |
5781 | { | |
5782 | int bit; | |
29dd3288 | 5783 | DECLARE_BITMAP(_mask, 64); |
4018994f | 5784 | |
29dd3288 MS |
5785 | bitmap_from_u64(_mask, mask); |
5786 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
5787 | u64 val; |
5788 | ||
5789 | val = perf_reg_value(regs, bit); | |
5790 | perf_output_put(handle, val); | |
5791 | } | |
5792 | } | |
5793 | ||
60e2364e | 5794 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5795 | struct pt_regs *regs, |
5796 | struct pt_regs *regs_user_copy) | |
4018994f | 5797 | { |
88a7c26a AL |
5798 | if (user_mode(regs)) { |
5799 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5800 | regs_user->regs = regs; |
88a7c26a AL |
5801 | } else if (current->mm) { |
5802 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5803 | } else { |
5804 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5805 | regs_user->regs = NULL; | |
4018994f JO |
5806 | } |
5807 | } | |
5808 | ||
60e2364e SE |
5809 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5810 | struct pt_regs *regs) | |
5811 | { | |
5812 | regs_intr->regs = regs; | |
5813 | regs_intr->abi = perf_reg_abi(current); | |
5814 | } | |
5815 | ||
5816 | ||
c5ebcedb JO |
5817 | /* |
5818 | * Get remaining task size from user stack pointer. | |
5819 | * | |
5820 | * It'd be better to take stack vma map and limit this more | |
5821 | * precisly, but there's no way to get it safely under interrupt, | |
5822 | * so using TASK_SIZE as limit. | |
5823 | */ | |
5824 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5825 | { | |
5826 | unsigned long addr = perf_user_stack_pointer(regs); | |
5827 | ||
5828 | if (!addr || addr >= TASK_SIZE) | |
5829 | return 0; | |
5830 | ||
5831 | return TASK_SIZE - addr; | |
5832 | } | |
5833 | ||
5834 | static u16 | |
5835 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5836 | struct pt_regs *regs) | |
5837 | { | |
5838 | u64 task_size; | |
5839 | ||
5840 | /* No regs, no stack pointer, no dump. */ | |
5841 | if (!regs) | |
5842 | return 0; | |
5843 | ||
5844 | /* | |
5845 | * Check if we fit in with the requested stack size into the: | |
5846 | * - TASK_SIZE | |
5847 | * If we don't, we limit the size to the TASK_SIZE. | |
5848 | * | |
5849 | * - remaining sample size | |
5850 | * If we don't, we customize the stack size to | |
5851 | * fit in to the remaining sample size. | |
5852 | */ | |
5853 | ||
5854 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5855 | stack_size = min(stack_size, (u16) task_size); | |
5856 | ||
5857 | /* Current header size plus static size and dynamic size. */ | |
5858 | header_size += 2 * sizeof(u64); | |
5859 | ||
5860 | /* Do we fit in with the current stack dump size? */ | |
5861 | if ((u16) (header_size + stack_size) < header_size) { | |
5862 | /* | |
5863 | * If we overflow the maximum size for the sample, | |
5864 | * we customize the stack dump size to fit in. | |
5865 | */ | |
5866 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5867 | stack_size = round_up(stack_size, sizeof(u64)); | |
5868 | } | |
5869 | ||
5870 | return stack_size; | |
5871 | } | |
5872 | ||
5873 | static void | |
5874 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5875 | struct pt_regs *regs) | |
5876 | { | |
5877 | /* Case of a kernel thread, nothing to dump */ | |
5878 | if (!regs) { | |
5879 | u64 size = 0; | |
5880 | perf_output_put(handle, size); | |
5881 | } else { | |
5882 | unsigned long sp; | |
5883 | unsigned int rem; | |
5884 | u64 dyn_size; | |
5885 | ||
5886 | /* | |
5887 | * We dump: | |
5888 | * static size | |
5889 | * - the size requested by user or the best one we can fit | |
5890 | * in to the sample max size | |
5891 | * data | |
5892 | * - user stack dump data | |
5893 | * dynamic size | |
5894 | * - the actual dumped size | |
5895 | */ | |
5896 | ||
5897 | /* Static size. */ | |
5898 | perf_output_put(handle, dump_size); | |
5899 | ||
5900 | /* Data. */ | |
5901 | sp = perf_user_stack_pointer(regs); | |
5902 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5903 | dyn_size = dump_size - rem; | |
5904 | ||
5905 | perf_output_skip(handle, rem); | |
5906 | ||
5907 | /* Dynamic size. */ | |
5908 | perf_output_put(handle, dyn_size); | |
5909 | } | |
5910 | } | |
5911 | ||
c980d109 ACM |
5912 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5913 | struct perf_sample_data *data, | |
5914 | struct perf_event *event) | |
6844c09d ACM |
5915 | { |
5916 | u64 sample_type = event->attr.sample_type; | |
5917 | ||
5918 | data->type = sample_type; | |
5919 | header->size += event->id_header_size; | |
5920 | ||
5921 | if (sample_type & PERF_SAMPLE_TID) { | |
5922 | /* namespace issues */ | |
5923 | data->tid_entry.pid = perf_event_pid(event, current); | |
5924 | data->tid_entry.tid = perf_event_tid(event, current); | |
5925 | } | |
5926 | ||
5927 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5928 | data->time = perf_event_clock(event); |
6844c09d | 5929 | |
ff3d527c | 5930 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5931 | data->id = primary_event_id(event); |
5932 | ||
5933 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5934 | data->stream_id = event->id; | |
5935 | ||
5936 | if (sample_type & PERF_SAMPLE_CPU) { | |
5937 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5938 | data->cpu_entry.reserved = 0; | |
5939 | } | |
5940 | } | |
5941 | ||
76369139 FW |
5942 | void perf_event_header__init_id(struct perf_event_header *header, |
5943 | struct perf_sample_data *data, | |
5944 | struct perf_event *event) | |
c980d109 ACM |
5945 | { |
5946 | if (event->attr.sample_id_all) | |
5947 | __perf_event_header__init_id(header, data, event); | |
5948 | } | |
5949 | ||
5950 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5951 | struct perf_sample_data *data) | |
5952 | { | |
5953 | u64 sample_type = data->type; | |
5954 | ||
5955 | if (sample_type & PERF_SAMPLE_TID) | |
5956 | perf_output_put(handle, data->tid_entry); | |
5957 | ||
5958 | if (sample_type & PERF_SAMPLE_TIME) | |
5959 | perf_output_put(handle, data->time); | |
5960 | ||
5961 | if (sample_type & PERF_SAMPLE_ID) | |
5962 | perf_output_put(handle, data->id); | |
5963 | ||
5964 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5965 | perf_output_put(handle, data->stream_id); | |
5966 | ||
5967 | if (sample_type & PERF_SAMPLE_CPU) | |
5968 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5969 | |
5970 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5971 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5972 | } |
5973 | ||
76369139 FW |
5974 | void perf_event__output_id_sample(struct perf_event *event, |
5975 | struct perf_output_handle *handle, | |
5976 | struct perf_sample_data *sample) | |
c980d109 ACM |
5977 | { |
5978 | if (event->attr.sample_id_all) | |
5979 | __perf_event__output_id_sample(handle, sample); | |
5980 | } | |
5981 | ||
3dab77fb | 5982 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5983 | struct perf_event *event, |
5984 | u64 enabled, u64 running) | |
3dab77fb | 5985 | { |
cdd6c482 | 5986 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5987 | u64 values[4]; |
5988 | int n = 0; | |
5989 | ||
b5e58793 | 5990 | values[n++] = perf_event_count(event); |
3dab77fb | 5991 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5992 | values[n++] = enabled + |
cdd6c482 | 5993 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5994 | } |
5995 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5996 | values[n++] = running + |
cdd6c482 | 5997 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5998 | } |
5999 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 6000 | values[n++] = primary_event_id(event); |
3dab77fb | 6001 | |
76369139 | 6002 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
6003 | } |
6004 | ||
3dab77fb | 6005 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
6006 | struct perf_event *event, |
6007 | u64 enabled, u64 running) | |
3dab77fb | 6008 | { |
cdd6c482 IM |
6009 | struct perf_event *leader = event->group_leader, *sub; |
6010 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
6011 | u64 values[5]; |
6012 | int n = 0; | |
6013 | ||
6014 | values[n++] = 1 + leader->nr_siblings; | |
6015 | ||
6016 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 6017 | values[n++] = enabled; |
3dab77fb PZ |
6018 | |
6019 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 6020 | values[n++] = running; |
3dab77fb | 6021 | |
9e5b127d PZ |
6022 | if ((leader != event) && |
6023 | (leader->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
6024 | leader->pmu->read(leader); |
6025 | ||
b5e58793 | 6026 | values[n++] = perf_event_count(leader); |
3dab77fb | 6027 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 6028 | values[n++] = primary_event_id(leader); |
3dab77fb | 6029 | |
76369139 | 6030 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 6031 | |
8343aae6 | 6032 | list_for_each_entry(sub, &leader->sibling_list, sibling_list) { |
3dab77fb PZ |
6033 | n = 0; |
6034 | ||
6f5ab001 JO |
6035 | if ((sub != event) && |
6036 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
6037 | sub->pmu->read(sub); |
6038 | ||
b5e58793 | 6039 | values[n++] = perf_event_count(sub); |
3dab77fb | 6040 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 6041 | values[n++] = primary_event_id(sub); |
3dab77fb | 6042 | |
76369139 | 6043 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
6044 | } |
6045 | } | |
6046 | ||
eed01528 SE |
6047 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
6048 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
6049 | ||
ba5213ae PZ |
6050 | /* |
6051 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
6052 | * | |
6053 | * The problem is that its both hard and excessively expensive to iterate the | |
6054 | * child list, not to mention that its impossible to IPI the children running | |
6055 | * on another CPU, from interrupt/NMI context. | |
6056 | */ | |
3dab77fb | 6057 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 6058 | struct perf_event *event) |
3dab77fb | 6059 | { |
e3f3541c | 6060 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
6061 | u64 read_format = event->attr.read_format; |
6062 | ||
6063 | /* | |
6064 | * compute total_time_enabled, total_time_running | |
6065 | * based on snapshot values taken when the event | |
6066 | * was last scheduled in. | |
6067 | * | |
6068 | * we cannot simply called update_context_time() | |
6069 | * because of locking issue as we are called in | |
6070 | * NMI context | |
6071 | */ | |
c4794295 | 6072 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 6073 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 6074 | |
cdd6c482 | 6075 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 6076 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 6077 | else |
eed01528 | 6078 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
6079 | } |
6080 | ||
5622f295 MM |
6081 | void perf_output_sample(struct perf_output_handle *handle, |
6082 | struct perf_event_header *header, | |
6083 | struct perf_sample_data *data, | |
cdd6c482 | 6084 | struct perf_event *event) |
5622f295 MM |
6085 | { |
6086 | u64 sample_type = data->type; | |
6087 | ||
6088 | perf_output_put(handle, *header); | |
6089 | ||
ff3d527c AH |
6090 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
6091 | perf_output_put(handle, data->id); | |
6092 | ||
5622f295 MM |
6093 | if (sample_type & PERF_SAMPLE_IP) |
6094 | perf_output_put(handle, data->ip); | |
6095 | ||
6096 | if (sample_type & PERF_SAMPLE_TID) | |
6097 | perf_output_put(handle, data->tid_entry); | |
6098 | ||
6099 | if (sample_type & PERF_SAMPLE_TIME) | |
6100 | perf_output_put(handle, data->time); | |
6101 | ||
6102 | if (sample_type & PERF_SAMPLE_ADDR) | |
6103 | perf_output_put(handle, data->addr); | |
6104 | ||
6105 | if (sample_type & PERF_SAMPLE_ID) | |
6106 | perf_output_put(handle, data->id); | |
6107 | ||
6108 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
6109 | perf_output_put(handle, data->stream_id); | |
6110 | ||
6111 | if (sample_type & PERF_SAMPLE_CPU) | |
6112 | perf_output_put(handle, data->cpu_entry); | |
6113 | ||
6114 | if (sample_type & PERF_SAMPLE_PERIOD) | |
6115 | perf_output_put(handle, data->period); | |
6116 | ||
6117 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 6118 | perf_output_read(handle, event); |
5622f295 MM |
6119 | |
6120 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
99e818cc | 6121 | int size = 1; |
5622f295 | 6122 | |
99e818cc JO |
6123 | size += data->callchain->nr; |
6124 | size *= sizeof(u64); | |
6125 | __output_copy(handle, data->callchain, size); | |
5622f295 MM |
6126 | } |
6127 | ||
6128 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
6129 | struct perf_raw_record *raw = data->raw; |
6130 | ||
6131 | if (raw) { | |
6132 | struct perf_raw_frag *frag = &raw->frag; | |
6133 | ||
6134 | perf_output_put(handle, raw->size); | |
6135 | do { | |
6136 | if (frag->copy) { | |
6137 | __output_custom(handle, frag->copy, | |
6138 | frag->data, frag->size); | |
6139 | } else { | |
6140 | __output_copy(handle, frag->data, | |
6141 | frag->size); | |
6142 | } | |
6143 | if (perf_raw_frag_last(frag)) | |
6144 | break; | |
6145 | frag = frag->next; | |
6146 | } while (1); | |
6147 | if (frag->pad) | |
6148 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
6149 | } else { |
6150 | struct { | |
6151 | u32 size; | |
6152 | u32 data; | |
6153 | } raw = { | |
6154 | .size = sizeof(u32), | |
6155 | .data = 0, | |
6156 | }; | |
6157 | perf_output_put(handle, raw); | |
6158 | } | |
6159 | } | |
a7ac67ea | 6160 | |
bce38cd5 SE |
6161 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
6162 | if (data->br_stack) { | |
6163 | size_t size; | |
6164 | ||
6165 | size = data->br_stack->nr | |
6166 | * sizeof(struct perf_branch_entry); | |
6167 | ||
6168 | perf_output_put(handle, data->br_stack->nr); | |
6169 | perf_output_copy(handle, data->br_stack->entries, size); | |
6170 | } else { | |
6171 | /* | |
6172 | * we always store at least the value of nr | |
6173 | */ | |
6174 | u64 nr = 0; | |
6175 | perf_output_put(handle, nr); | |
6176 | } | |
6177 | } | |
4018994f JO |
6178 | |
6179 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
6180 | u64 abi = data->regs_user.abi; | |
6181 | ||
6182 | /* | |
6183 | * If there are no regs to dump, notice it through | |
6184 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
6185 | */ | |
6186 | perf_output_put(handle, abi); | |
6187 | ||
6188 | if (abi) { | |
6189 | u64 mask = event->attr.sample_regs_user; | |
6190 | perf_output_sample_regs(handle, | |
6191 | data->regs_user.regs, | |
6192 | mask); | |
6193 | } | |
6194 | } | |
c5ebcedb | 6195 | |
a5cdd40c | 6196 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
6197 | perf_output_sample_ustack(handle, |
6198 | data->stack_user_size, | |
6199 | data->regs_user.regs); | |
a5cdd40c | 6200 | } |
c3feedf2 AK |
6201 | |
6202 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
6203 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
6204 | |
6205 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
6206 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 6207 | |
fdfbbd07 AK |
6208 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
6209 | perf_output_put(handle, data->txn); | |
6210 | ||
60e2364e SE |
6211 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
6212 | u64 abi = data->regs_intr.abi; | |
6213 | /* | |
6214 | * If there are no regs to dump, notice it through | |
6215 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
6216 | */ | |
6217 | perf_output_put(handle, abi); | |
6218 | ||
6219 | if (abi) { | |
6220 | u64 mask = event->attr.sample_regs_intr; | |
6221 | ||
6222 | perf_output_sample_regs(handle, | |
6223 | data->regs_intr.regs, | |
6224 | mask); | |
6225 | } | |
6226 | } | |
6227 | ||
fc7ce9c7 KL |
6228 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
6229 | perf_output_put(handle, data->phys_addr); | |
6230 | ||
a5cdd40c PZ |
6231 | if (!event->attr.watermark) { |
6232 | int wakeup_events = event->attr.wakeup_events; | |
6233 | ||
6234 | if (wakeup_events) { | |
6235 | struct ring_buffer *rb = handle->rb; | |
6236 | int events = local_inc_return(&rb->events); | |
6237 | ||
6238 | if (events >= wakeup_events) { | |
6239 | local_sub(wakeup_events, &rb->events); | |
6240 | local_inc(&rb->wakeup); | |
6241 | } | |
6242 | } | |
6243 | } | |
5622f295 MM |
6244 | } |
6245 | ||
fc7ce9c7 KL |
6246 | static u64 perf_virt_to_phys(u64 virt) |
6247 | { | |
6248 | u64 phys_addr = 0; | |
6249 | struct page *p = NULL; | |
6250 | ||
6251 | if (!virt) | |
6252 | return 0; | |
6253 | ||
6254 | if (virt >= TASK_SIZE) { | |
6255 | /* If it's vmalloc()d memory, leave phys_addr as 0 */ | |
6256 | if (virt_addr_valid((void *)(uintptr_t)virt) && | |
6257 | !(virt >= VMALLOC_START && virt < VMALLOC_END)) | |
6258 | phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); | |
6259 | } else { | |
6260 | /* | |
6261 | * Walking the pages tables for user address. | |
6262 | * Interrupts are disabled, so it prevents any tear down | |
6263 | * of the page tables. | |
6264 | * Try IRQ-safe __get_user_pages_fast first. | |
6265 | * If failed, leave phys_addr as 0. | |
6266 | */ | |
6267 | if ((current->mm != NULL) && | |
6268 | (__get_user_pages_fast(virt, 1, 0, &p) == 1)) | |
6269 | phys_addr = page_to_phys(p) + virt % PAGE_SIZE; | |
6270 | ||
6271 | if (p) | |
6272 | put_page(p); | |
6273 | } | |
6274 | ||
6275 | return phys_addr; | |
6276 | } | |
6277 | ||
99e818cc JO |
6278 | static struct perf_callchain_entry __empty_callchain = { .nr = 0, }; |
6279 | ||
8cf7e0e2 JO |
6280 | static struct perf_callchain_entry * |
6281 | perf_callchain(struct perf_event *event, struct pt_regs *regs) | |
6282 | { | |
6283 | bool kernel = !event->attr.exclude_callchain_kernel; | |
6284 | bool user = !event->attr.exclude_callchain_user; | |
6285 | /* Disallow cross-task user callchains. */ | |
6286 | bool crosstask = event->ctx->task && event->ctx->task != current; | |
6287 | const u32 max_stack = event->attr.sample_max_stack; | |
99e818cc | 6288 | struct perf_callchain_entry *callchain; |
8cf7e0e2 JO |
6289 | |
6290 | if (!kernel && !user) | |
99e818cc | 6291 | return &__empty_callchain; |
8cf7e0e2 | 6292 | |
99e818cc JO |
6293 | callchain = get_perf_callchain(regs, 0, kernel, user, |
6294 | max_stack, crosstask, true); | |
6295 | return callchain ?: &__empty_callchain; | |
8cf7e0e2 JO |
6296 | } |
6297 | ||
5622f295 MM |
6298 | void perf_prepare_sample(struct perf_event_header *header, |
6299 | struct perf_sample_data *data, | |
cdd6c482 | 6300 | struct perf_event *event, |
5622f295 | 6301 | struct pt_regs *regs) |
7b732a75 | 6302 | { |
cdd6c482 | 6303 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 6304 | |
cdd6c482 | 6305 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 6306 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
6307 | |
6308 | header->misc = 0; | |
6309 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 6310 | |
c980d109 | 6311 | __perf_event_header__init_id(header, data, event); |
6844c09d | 6312 | |
c320c7b7 | 6313 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
6314 | data->ip = perf_instruction_pointer(regs); |
6315 | ||
b23f3325 | 6316 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 6317 | int size = 1; |
394ee076 | 6318 | |
e6dab5ff | 6319 | data->callchain = perf_callchain(event, regs); |
99e818cc | 6320 | size += data->callchain->nr; |
5622f295 MM |
6321 | |
6322 | header->size += size * sizeof(u64); | |
394ee076 PZ |
6323 | } |
6324 | ||
3a43ce68 | 6325 | if (sample_type & PERF_SAMPLE_RAW) { |
7e3f977e DB |
6326 | struct perf_raw_record *raw = data->raw; |
6327 | int size; | |
6328 | ||
6329 | if (raw) { | |
6330 | struct perf_raw_frag *frag = &raw->frag; | |
6331 | u32 sum = 0; | |
6332 | ||
6333 | do { | |
6334 | sum += frag->size; | |
6335 | if (perf_raw_frag_last(frag)) | |
6336 | break; | |
6337 | frag = frag->next; | |
6338 | } while (1); | |
6339 | ||
6340 | size = round_up(sum + sizeof(u32), sizeof(u64)); | |
6341 | raw->size = size - sizeof(u32); | |
6342 | frag->pad = raw->size - sum; | |
6343 | } else { | |
6344 | size = sizeof(u64); | |
6345 | } | |
a044560c | 6346 | |
7e3f977e | 6347 | header->size += size; |
7f453c24 | 6348 | } |
bce38cd5 SE |
6349 | |
6350 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
6351 | int size = sizeof(u64); /* nr */ | |
6352 | if (data->br_stack) { | |
6353 | size += data->br_stack->nr | |
6354 | * sizeof(struct perf_branch_entry); | |
6355 | } | |
6356 | header->size += size; | |
6357 | } | |
4018994f | 6358 | |
2565711f | 6359 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
6360 | perf_sample_regs_user(&data->regs_user, regs, |
6361 | &data->regs_user_copy); | |
2565711f | 6362 | |
4018994f JO |
6363 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
6364 | /* regs dump ABI info */ | |
6365 | int size = sizeof(u64); | |
6366 | ||
4018994f JO |
6367 | if (data->regs_user.regs) { |
6368 | u64 mask = event->attr.sample_regs_user; | |
6369 | size += hweight64(mask) * sizeof(u64); | |
6370 | } | |
6371 | ||
6372 | header->size += size; | |
6373 | } | |
c5ebcedb JO |
6374 | |
6375 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
6376 | /* | |
6377 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
6378 | * processed as the last one or have additional check added | |
6379 | * in case new sample type is added, because we could eat | |
6380 | * up the rest of the sample size. | |
6381 | */ | |
c5ebcedb JO |
6382 | u16 stack_size = event->attr.sample_stack_user; |
6383 | u16 size = sizeof(u64); | |
6384 | ||
c5ebcedb | 6385 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 6386 | data->regs_user.regs); |
c5ebcedb JO |
6387 | |
6388 | /* | |
6389 | * If there is something to dump, add space for the dump | |
6390 | * itself and for the field that tells the dynamic size, | |
6391 | * which is how many have been actually dumped. | |
6392 | */ | |
6393 | if (stack_size) | |
6394 | size += sizeof(u64) + stack_size; | |
6395 | ||
6396 | data->stack_user_size = stack_size; | |
6397 | header->size += size; | |
6398 | } | |
60e2364e SE |
6399 | |
6400 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
6401 | /* regs dump ABI info */ | |
6402 | int size = sizeof(u64); | |
6403 | ||
6404 | perf_sample_regs_intr(&data->regs_intr, regs); | |
6405 | ||
6406 | if (data->regs_intr.regs) { | |
6407 | u64 mask = event->attr.sample_regs_intr; | |
6408 | ||
6409 | size += hweight64(mask) * sizeof(u64); | |
6410 | } | |
6411 | ||
6412 | header->size += size; | |
6413 | } | |
fc7ce9c7 KL |
6414 | |
6415 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) | |
6416 | data->phys_addr = perf_virt_to_phys(data->addr); | |
5622f295 | 6417 | } |
7f453c24 | 6418 | |
9ecda41a WN |
6419 | static void __always_inline |
6420 | __perf_event_output(struct perf_event *event, | |
6421 | struct perf_sample_data *data, | |
6422 | struct pt_regs *regs, | |
6423 | int (*output_begin)(struct perf_output_handle *, | |
6424 | struct perf_event *, | |
6425 | unsigned int)) | |
5622f295 MM |
6426 | { |
6427 | struct perf_output_handle handle; | |
6428 | struct perf_event_header header; | |
689802b2 | 6429 | |
927c7a9e FW |
6430 | /* protect the callchain buffers */ |
6431 | rcu_read_lock(); | |
6432 | ||
cdd6c482 | 6433 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 6434 | |
9ecda41a | 6435 | if (output_begin(&handle, event, header.size)) |
927c7a9e | 6436 | goto exit; |
0322cd6e | 6437 | |
cdd6c482 | 6438 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 6439 | |
8a057d84 | 6440 | perf_output_end(&handle); |
927c7a9e FW |
6441 | |
6442 | exit: | |
6443 | rcu_read_unlock(); | |
0322cd6e PZ |
6444 | } |
6445 | ||
9ecda41a WN |
6446 | void |
6447 | perf_event_output_forward(struct perf_event *event, | |
6448 | struct perf_sample_data *data, | |
6449 | struct pt_regs *regs) | |
6450 | { | |
6451 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
6452 | } | |
6453 | ||
6454 | void | |
6455 | perf_event_output_backward(struct perf_event *event, | |
6456 | struct perf_sample_data *data, | |
6457 | struct pt_regs *regs) | |
6458 | { | |
6459 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
6460 | } | |
6461 | ||
6462 | void | |
6463 | perf_event_output(struct perf_event *event, | |
6464 | struct perf_sample_data *data, | |
6465 | struct pt_regs *regs) | |
6466 | { | |
6467 | __perf_event_output(event, data, regs, perf_output_begin); | |
6468 | } | |
6469 | ||
38b200d6 | 6470 | /* |
cdd6c482 | 6471 | * read event_id |
38b200d6 PZ |
6472 | */ |
6473 | ||
6474 | struct perf_read_event { | |
6475 | struct perf_event_header header; | |
6476 | ||
6477 | u32 pid; | |
6478 | u32 tid; | |
38b200d6 PZ |
6479 | }; |
6480 | ||
6481 | static void | |
cdd6c482 | 6482 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
6483 | struct task_struct *task) |
6484 | { | |
6485 | struct perf_output_handle handle; | |
c980d109 | 6486 | struct perf_sample_data sample; |
dfc65094 | 6487 | struct perf_read_event read_event = { |
38b200d6 | 6488 | .header = { |
cdd6c482 | 6489 | .type = PERF_RECORD_READ, |
38b200d6 | 6490 | .misc = 0, |
c320c7b7 | 6491 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 6492 | }, |
cdd6c482 IM |
6493 | .pid = perf_event_pid(event, task), |
6494 | .tid = perf_event_tid(event, task), | |
38b200d6 | 6495 | }; |
3dab77fb | 6496 | int ret; |
38b200d6 | 6497 | |
c980d109 | 6498 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 6499 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
6500 | if (ret) |
6501 | return; | |
6502 | ||
dfc65094 | 6503 | perf_output_put(&handle, read_event); |
cdd6c482 | 6504 | perf_output_read(&handle, event); |
c980d109 | 6505 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 6506 | |
38b200d6 PZ |
6507 | perf_output_end(&handle); |
6508 | } | |
6509 | ||
aab5b71e | 6510 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
6511 | |
6512 | static void | |
aab5b71e PZ |
6513 | perf_iterate_ctx(struct perf_event_context *ctx, |
6514 | perf_iterate_f output, | |
b73e4fef | 6515 | void *data, bool all) |
52d857a8 JO |
6516 | { |
6517 | struct perf_event *event; | |
6518 | ||
6519 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
6520 | if (!all) { |
6521 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
6522 | continue; | |
6523 | if (!event_filter_match(event)) | |
6524 | continue; | |
6525 | } | |
6526 | ||
67516844 | 6527 | output(event, data); |
52d857a8 JO |
6528 | } |
6529 | } | |
6530 | ||
aab5b71e | 6531 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
6532 | { |
6533 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
6534 | struct perf_event *event; | |
6535 | ||
6536 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
6537 | /* |
6538 | * Skip events that are not fully formed yet; ensure that | |
6539 | * if we observe event->ctx, both event and ctx will be | |
6540 | * complete enough. See perf_install_in_context(). | |
6541 | */ | |
6542 | if (!smp_load_acquire(&event->ctx)) | |
6543 | continue; | |
6544 | ||
f2fb6bef KL |
6545 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
6546 | continue; | |
6547 | if (!event_filter_match(event)) | |
6548 | continue; | |
6549 | output(event, data); | |
6550 | } | |
6551 | } | |
6552 | ||
aab5b71e PZ |
6553 | /* |
6554 | * Iterate all events that need to receive side-band events. | |
6555 | * | |
6556 | * For new callers; ensure that account_pmu_sb_event() includes | |
6557 | * your event, otherwise it might not get delivered. | |
6558 | */ | |
52d857a8 | 6559 | static void |
aab5b71e | 6560 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
6561 | struct perf_event_context *task_ctx) |
6562 | { | |
52d857a8 | 6563 | struct perf_event_context *ctx; |
52d857a8 JO |
6564 | int ctxn; |
6565 | ||
aab5b71e PZ |
6566 | rcu_read_lock(); |
6567 | preempt_disable(); | |
6568 | ||
4e93ad60 | 6569 | /* |
aab5b71e PZ |
6570 | * If we have task_ctx != NULL we only notify the task context itself. |
6571 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
6572 | * context. |
6573 | */ | |
6574 | if (task_ctx) { | |
aab5b71e PZ |
6575 | perf_iterate_ctx(task_ctx, output, data, false); |
6576 | goto done; | |
4e93ad60 JO |
6577 | } |
6578 | ||
aab5b71e | 6579 | perf_iterate_sb_cpu(output, data); |
f2fb6bef KL |
6580 | |
6581 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
6582 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
6583 | if (ctx) | |
aab5b71e | 6584 | perf_iterate_ctx(ctx, output, data, false); |
52d857a8 | 6585 | } |
aab5b71e | 6586 | done: |
f2fb6bef | 6587 | preempt_enable(); |
52d857a8 | 6588 | rcu_read_unlock(); |
95ff4ca2 AS |
6589 | } |
6590 | ||
375637bc AS |
6591 | /* |
6592 | * Clear all file-based filters at exec, they'll have to be | |
6593 | * re-instated when/if these objects are mmapped again. | |
6594 | */ | |
6595 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
6596 | { | |
6597 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6598 | struct perf_addr_filter *filter; | |
6599 | unsigned int restart = 0, count = 0; | |
6600 | unsigned long flags; | |
6601 | ||
6602 | if (!has_addr_filter(event)) | |
6603 | return; | |
6604 | ||
6605 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
6606 | list_for_each_entry(filter, &ifh->list, entry) { | |
6607 | if (filter->inode) { | |
6608 | event->addr_filters_offs[count] = 0; | |
6609 | restart++; | |
6610 | } | |
6611 | ||
6612 | count++; | |
6613 | } | |
6614 | ||
6615 | if (restart) | |
6616 | event->addr_filters_gen++; | |
6617 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
6618 | ||
6619 | if (restart) | |
767ae086 | 6620 | perf_event_stop(event, 1); |
375637bc AS |
6621 | } |
6622 | ||
6623 | void perf_event_exec(void) | |
6624 | { | |
6625 | struct perf_event_context *ctx; | |
6626 | int ctxn; | |
6627 | ||
6628 | rcu_read_lock(); | |
6629 | for_each_task_context_nr(ctxn) { | |
6630 | ctx = current->perf_event_ctxp[ctxn]; | |
6631 | if (!ctx) | |
6632 | continue; | |
6633 | ||
6634 | perf_event_enable_on_exec(ctxn); | |
6635 | ||
aab5b71e | 6636 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
375637bc AS |
6637 | true); |
6638 | } | |
6639 | rcu_read_unlock(); | |
6640 | } | |
6641 | ||
95ff4ca2 AS |
6642 | struct remote_output { |
6643 | struct ring_buffer *rb; | |
6644 | int err; | |
6645 | }; | |
6646 | ||
6647 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
6648 | { | |
6649 | struct perf_event *parent = event->parent; | |
6650 | struct remote_output *ro = data; | |
6651 | struct ring_buffer *rb = ro->rb; | |
375637bc AS |
6652 | struct stop_event_data sd = { |
6653 | .event = event, | |
6654 | }; | |
95ff4ca2 AS |
6655 | |
6656 | if (!has_aux(event)) | |
6657 | return; | |
6658 | ||
6659 | if (!parent) | |
6660 | parent = event; | |
6661 | ||
6662 | /* | |
6663 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
6664 | * ring-buffer, but it will be the child that's actually using it. |
6665 | * | |
6666 | * We are using event::rb to determine if the event should be stopped, | |
6667 | * however this may race with ring_buffer_attach() (through set_output), | |
6668 | * which will make us skip the event that actually needs to be stopped. | |
6669 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
6670 | * its rb pointer. | |
95ff4ca2 AS |
6671 | */ |
6672 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 6673 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
6674 | } |
6675 | ||
6676 | static int __perf_pmu_output_stop(void *info) | |
6677 | { | |
6678 | struct perf_event *event = info; | |
6679 | struct pmu *pmu = event->pmu; | |
8b6a3fe8 | 6680 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95ff4ca2 AS |
6681 | struct remote_output ro = { |
6682 | .rb = event->rb, | |
6683 | }; | |
6684 | ||
6685 | rcu_read_lock(); | |
aab5b71e | 6686 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 6687 | if (cpuctx->task_ctx) |
aab5b71e | 6688 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 6689 | &ro, false); |
95ff4ca2 AS |
6690 | rcu_read_unlock(); |
6691 | ||
6692 | return ro.err; | |
6693 | } | |
6694 | ||
6695 | static void perf_pmu_output_stop(struct perf_event *event) | |
6696 | { | |
6697 | struct perf_event *iter; | |
6698 | int err, cpu; | |
6699 | ||
6700 | restart: | |
6701 | rcu_read_lock(); | |
6702 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
6703 | /* | |
6704 | * For per-CPU events, we need to make sure that neither they | |
6705 | * nor their children are running; for cpu==-1 events it's | |
6706 | * sufficient to stop the event itself if it's active, since | |
6707 | * it can't have children. | |
6708 | */ | |
6709 | cpu = iter->cpu; | |
6710 | if (cpu == -1) | |
6711 | cpu = READ_ONCE(iter->oncpu); | |
6712 | ||
6713 | if (cpu == -1) | |
6714 | continue; | |
6715 | ||
6716 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
6717 | if (err == -EAGAIN) { | |
6718 | rcu_read_unlock(); | |
6719 | goto restart; | |
6720 | } | |
6721 | } | |
6722 | rcu_read_unlock(); | |
52d857a8 JO |
6723 | } |
6724 | ||
60313ebe | 6725 | /* |
9f498cc5 PZ |
6726 | * task tracking -- fork/exit |
6727 | * | |
13d7a241 | 6728 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
6729 | */ |
6730 | ||
9f498cc5 | 6731 | struct perf_task_event { |
3a80b4a3 | 6732 | struct task_struct *task; |
cdd6c482 | 6733 | struct perf_event_context *task_ctx; |
60313ebe PZ |
6734 | |
6735 | struct { | |
6736 | struct perf_event_header header; | |
6737 | ||
6738 | u32 pid; | |
6739 | u32 ppid; | |
9f498cc5 PZ |
6740 | u32 tid; |
6741 | u32 ptid; | |
393b2ad8 | 6742 | u64 time; |
cdd6c482 | 6743 | } event_id; |
60313ebe PZ |
6744 | }; |
6745 | ||
67516844 JO |
6746 | static int perf_event_task_match(struct perf_event *event) |
6747 | { | |
13d7a241 SE |
6748 | return event->attr.comm || event->attr.mmap || |
6749 | event->attr.mmap2 || event->attr.mmap_data || | |
6750 | event->attr.task; | |
67516844 JO |
6751 | } |
6752 | ||
cdd6c482 | 6753 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 6754 | void *data) |
60313ebe | 6755 | { |
52d857a8 | 6756 | struct perf_task_event *task_event = data; |
60313ebe | 6757 | struct perf_output_handle handle; |
c980d109 | 6758 | struct perf_sample_data sample; |
9f498cc5 | 6759 | struct task_struct *task = task_event->task; |
c980d109 | 6760 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 6761 | |
67516844 JO |
6762 | if (!perf_event_task_match(event)) |
6763 | return; | |
6764 | ||
c980d109 | 6765 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 6766 | |
c980d109 | 6767 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 6768 | task_event->event_id.header.size); |
ef60777c | 6769 | if (ret) |
c980d109 | 6770 | goto out; |
60313ebe | 6771 | |
cdd6c482 IM |
6772 | task_event->event_id.pid = perf_event_pid(event, task); |
6773 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 6774 | |
cdd6c482 IM |
6775 | task_event->event_id.tid = perf_event_tid(event, task); |
6776 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 6777 | |
34f43927 PZ |
6778 | task_event->event_id.time = perf_event_clock(event); |
6779 | ||
cdd6c482 | 6780 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 6781 | |
c980d109 ACM |
6782 | perf_event__output_id_sample(event, &handle, &sample); |
6783 | ||
60313ebe | 6784 | perf_output_end(&handle); |
c980d109 ACM |
6785 | out: |
6786 | task_event->event_id.header.size = size; | |
60313ebe PZ |
6787 | } |
6788 | ||
cdd6c482 IM |
6789 | static void perf_event_task(struct task_struct *task, |
6790 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 6791 | int new) |
60313ebe | 6792 | { |
9f498cc5 | 6793 | struct perf_task_event task_event; |
60313ebe | 6794 | |
cdd6c482 IM |
6795 | if (!atomic_read(&nr_comm_events) && |
6796 | !atomic_read(&nr_mmap_events) && | |
6797 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
6798 | return; |
6799 | ||
9f498cc5 | 6800 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
6801 | .task = task, |
6802 | .task_ctx = task_ctx, | |
cdd6c482 | 6803 | .event_id = { |
60313ebe | 6804 | .header = { |
cdd6c482 | 6805 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 6806 | .misc = 0, |
cdd6c482 | 6807 | .size = sizeof(task_event.event_id), |
60313ebe | 6808 | }, |
573402db PZ |
6809 | /* .pid */ |
6810 | /* .ppid */ | |
9f498cc5 PZ |
6811 | /* .tid */ |
6812 | /* .ptid */ | |
34f43927 | 6813 | /* .time */ |
60313ebe PZ |
6814 | }, |
6815 | }; | |
6816 | ||
aab5b71e | 6817 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
6818 | &task_event, |
6819 | task_ctx); | |
9f498cc5 PZ |
6820 | } |
6821 | ||
cdd6c482 | 6822 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 6823 | { |
cdd6c482 | 6824 | perf_event_task(task, NULL, 1); |
e4222673 | 6825 | perf_event_namespaces(task); |
60313ebe PZ |
6826 | } |
6827 | ||
8d1b2d93 PZ |
6828 | /* |
6829 | * comm tracking | |
6830 | */ | |
6831 | ||
6832 | struct perf_comm_event { | |
22a4f650 IM |
6833 | struct task_struct *task; |
6834 | char *comm; | |
8d1b2d93 PZ |
6835 | int comm_size; |
6836 | ||
6837 | struct { | |
6838 | struct perf_event_header header; | |
6839 | ||
6840 | u32 pid; | |
6841 | u32 tid; | |
cdd6c482 | 6842 | } event_id; |
8d1b2d93 PZ |
6843 | }; |
6844 | ||
67516844 JO |
6845 | static int perf_event_comm_match(struct perf_event *event) |
6846 | { | |
6847 | return event->attr.comm; | |
6848 | } | |
6849 | ||
cdd6c482 | 6850 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6851 | void *data) |
8d1b2d93 | 6852 | { |
52d857a8 | 6853 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6854 | struct perf_output_handle handle; |
c980d109 | 6855 | struct perf_sample_data sample; |
cdd6c482 | 6856 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6857 | int ret; |
6858 | ||
67516844 JO |
6859 | if (!perf_event_comm_match(event)) |
6860 | return; | |
6861 | ||
c980d109 ACM |
6862 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6863 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6864 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6865 | |
6866 | if (ret) | |
c980d109 | 6867 | goto out; |
8d1b2d93 | 6868 | |
cdd6c482 IM |
6869 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6870 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6871 | |
cdd6c482 | 6872 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6873 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6874 | comm_event->comm_size); |
c980d109 ACM |
6875 | |
6876 | perf_event__output_id_sample(event, &handle, &sample); | |
6877 | ||
8d1b2d93 | 6878 | perf_output_end(&handle); |
c980d109 ACM |
6879 | out: |
6880 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6881 | } |
6882 | ||
cdd6c482 | 6883 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6884 | { |
413ee3b4 | 6885 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6886 | unsigned int size; |
8d1b2d93 | 6887 | |
413ee3b4 | 6888 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6889 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6890 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6891 | |
6892 | comm_event->comm = comm; | |
6893 | comm_event->comm_size = size; | |
6894 | ||
cdd6c482 | 6895 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6896 | |
aab5b71e | 6897 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
6898 | comm_event, |
6899 | NULL); | |
8d1b2d93 PZ |
6900 | } |
6901 | ||
82b89778 | 6902 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6903 | { |
9ee318a7 PZ |
6904 | struct perf_comm_event comm_event; |
6905 | ||
cdd6c482 | 6906 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6907 | return; |
a63eaf34 | 6908 | |
9ee318a7 | 6909 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6910 | .task = task, |
573402db PZ |
6911 | /* .comm */ |
6912 | /* .comm_size */ | |
cdd6c482 | 6913 | .event_id = { |
573402db | 6914 | .header = { |
cdd6c482 | 6915 | .type = PERF_RECORD_COMM, |
82b89778 | 6916 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6917 | /* .size */ |
6918 | }, | |
6919 | /* .pid */ | |
6920 | /* .tid */ | |
8d1b2d93 PZ |
6921 | }, |
6922 | }; | |
6923 | ||
cdd6c482 | 6924 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6925 | } |
6926 | ||
e4222673 HB |
6927 | /* |
6928 | * namespaces tracking | |
6929 | */ | |
6930 | ||
6931 | struct perf_namespaces_event { | |
6932 | struct task_struct *task; | |
6933 | ||
6934 | struct { | |
6935 | struct perf_event_header header; | |
6936 | ||
6937 | u32 pid; | |
6938 | u32 tid; | |
6939 | u64 nr_namespaces; | |
6940 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
6941 | } event_id; | |
6942 | }; | |
6943 | ||
6944 | static int perf_event_namespaces_match(struct perf_event *event) | |
6945 | { | |
6946 | return event->attr.namespaces; | |
6947 | } | |
6948 | ||
6949 | static void perf_event_namespaces_output(struct perf_event *event, | |
6950 | void *data) | |
6951 | { | |
6952 | struct perf_namespaces_event *namespaces_event = data; | |
6953 | struct perf_output_handle handle; | |
6954 | struct perf_sample_data sample; | |
34900ec5 | 6955 | u16 header_size = namespaces_event->event_id.header.size; |
e4222673 HB |
6956 | int ret; |
6957 | ||
6958 | if (!perf_event_namespaces_match(event)) | |
6959 | return; | |
6960 | ||
6961 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
6962 | &sample, event); | |
6963 | ret = perf_output_begin(&handle, event, | |
6964 | namespaces_event->event_id.header.size); | |
6965 | if (ret) | |
34900ec5 | 6966 | goto out; |
e4222673 HB |
6967 | |
6968 | namespaces_event->event_id.pid = perf_event_pid(event, | |
6969 | namespaces_event->task); | |
6970 | namespaces_event->event_id.tid = perf_event_tid(event, | |
6971 | namespaces_event->task); | |
6972 | ||
6973 | perf_output_put(&handle, namespaces_event->event_id); | |
6974 | ||
6975 | perf_event__output_id_sample(event, &handle, &sample); | |
6976 | ||
6977 | perf_output_end(&handle); | |
34900ec5 JO |
6978 | out: |
6979 | namespaces_event->event_id.header.size = header_size; | |
e4222673 HB |
6980 | } |
6981 | ||
6982 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
6983 | struct task_struct *task, | |
6984 | const struct proc_ns_operations *ns_ops) | |
6985 | { | |
6986 | struct path ns_path; | |
6987 | struct inode *ns_inode; | |
6988 | void *error; | |
6989 | ||
6990 | error = ns_get_path(&ns_path, task, ns_ops); | |
6991 | if (!error) { | |
6992 | ns_inode = ns_path.dentry->d_inode; | |
6993 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
6994 | ns_link_info->ino = ns_inode->i_ino; | |
0e18dd12 | 6995 | path_put(&ns_path); |
e4222673 HB |
6996 | } |
6997 | } | |
6998 | ||
6999 | void perf_event_namespaces(struct task_struct *task) | |
7000 | { | |
7001 | struct perf_namespaces_event namespaces_event; | |
7002 | struct perf_ns_link_info *ns_link_info; | |
7003 | ||
7004 | if (!atomic_read(&nr_namespaces_events)) | |
7005 | return; | |
7006 | ||
7007 | namespaces_event = (struct perf_namespaces_event){ | |
7008 | .task = task, | |
7009 | .event_id = { | |
7010 | .header = { | |
7011 | .type = PERF_RECORD_NAMESPACES, | |
7012 | .misc = 0, | |
7013 | .size = sizeof(namespaces_event.event_id), | |
7014 | }, | |
7015 | /* .pid */ | |
7016 | /* .tid */ | |
7017 | .nr_namespaces = NR_NAMESPACES, | |
7018 | /* .link_info[NR_NAMESPACES] */ | |
7019 | }, | |
7020 | }; | |
7021 | ||
7022 | ns_link_info = namespaces_event.event_id.link_info; | |
7023 | ||
7024 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
7025 | task, &mntns_operations); | |
7026 | ||
7027 | #ifdef CONFIG_USER_NS | |
7028 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
7029 | task, &userns_operations); | |
7030 | #endif | |
7031 | #ifdef CONFIG_NET_NS | |
7032 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
7033 | task, &netns_operations); | |
7034 | #endif | |
7035 | #ifdef CONFIG_UTS_NS | |
7036 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
7037 | task, &utsns_operations); | |
7038 | #endif | |
7039 | #ifdef CONFIG_IPC_NS | |
7040 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
7041 | task, &ipcns_operations); | |
7042 | #endif | |
7043 | #ifdef CONFIG_PID_NS | |
7044 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
7045 | task, &pidns_operations); | |
7046 | #endif | |
7047 | #ifdef CONFIG_CGROUPS | |
7048 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
7049 | task, &cgroupns_operations); | |
7050 | #endif | |
7051 | ||
7052 | perf_iterate_sb(perf_event_namespaces_output, | |
7053 | &namespaces_event, | |
7054 | NULL); | |
7055 | } | |
7056 | ||
0a4a9391 PZ |
7057 | /* |
7058 | * mmap tracking | |
7059 | */ | |
7060 | ||
7061 | struct perf_mmap_event { | |
089dd79d PZ |
7062 | struct vm_area_struct *vma; |
7063 | ||
7064 | const char *file_name; | |
7065 | int file_size; | |
13d7a241 SE |
7066 | int maj, min; |
7067 | u64 ino; | |
7068 | u64 ino_generation; | |
f972eb63 | 7069 | u32 prot, flags; |
0a4a9391 PZ |
7070 | |
7071 | struct { | |
7072 | struct perf_event_header header; | |
7073 | ||
7074 | u32 pid; | |
7075 | u32 tid; | |
7076 | u64 start; | |
7077 | u64 len; | |
7078 | u64 pgoff; | |
cdd6c482 | 7079 | } event_id; |
0a4a9391 PZ |
7080 | }; |
7081 | ||
67516844 JO |
7082 | static int perf_event_mmap_match(struct perf_event *event, |
7083 | void *data) | |
7084 | { | |
7085 | struct perf_mmap_event *mmap_event = data; | |
7086 | struct vm_area_struct *vma = mmap_event->vma; | |
7087 | int executable = vma->vm_flags & VM_EXEC; | |
7088 | ||
7089 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 7090 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
7091 | } |
7092 | ||
cdd6c482 | 7093 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 7094 | void *data) |
0a4a9391 | 7095 | { |
52d857a8 | 7096 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 7097 | struct perf_output_handle handle; |
c980d109 | 7098 | struct perf_sample_data sample; |
cdd6c482 | 7099 | int size = mmap_event->event_id.header.size; |
c980d109 | 7100 | int ret; |
0a4a9391 | 7101 | |
67516844 JO |
7102 | if (!perf_event_mmap_match(event, data)) |
7103 | return; | |
7104 | ||
13d7a241 SE |
7105 | if (event->attr.mmap2) { |
7106 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
7107 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
7108 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
7109 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 7110 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
7111 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
7112 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
7113 | } |
7114 | ||
c980d109 ACM |
7115 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
7116 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7117 | mmap_event->event_id.header.size); |
0a4a9391 | 7118 | if (ret) |
c980d109 | 7119 | goto out; |
0a4a9391 | 7120 | |
cdd6c482 IM |
7121 | mmap_event->event_id.pid = perf_event_pid(event, current); |
7122 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 7123 | |
cdd6c482 | 7124 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
7125 | |
7126 | if (event->attr.mmap2) { | |
7127 | perf_output_put(&handle, mmap_event->maj); | |
7128 | perf_output_put(&handle, mmap_event->min); | |
7129 | perf_output_put(&handle, mmap_event->ino); | |
7130 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
7131 | perf_output_put(&handle, mmap_event->prot); |
7132 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
7133 | } |
7134 | ||
76369139 | 7135 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 7136 | mmap_event->file_size); |
c980d109 ACM |
7137 | |
7138 | perf_event__output_id_sample(event, &handle, &sample); | |
7139 | ||
78d613eb | 7140 | perf_output_end(&handle); |
c980d109 ACM |
7141 | out: |
7142 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
7143 | } |
7144 | ||
cdd6c482 | 7145 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 7146 | { |
089dd79d PZ |
7147 | struct vm_area_struct *vma = mmap_event->vma; |
7148 | struct file *file = vma->vm_file; | |
13d7a241 SE |
7149 | int maj = 0, min = 0; |
7150 | u64 ino = 0, gen = 0; | |
f972eb63 | 7151 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
7152 | unsigned int size; |
7153 | char tmp[16]; | |
7154 | char *buf = NULL; | |
2c42cfbf | 7155 | char *name; |
413ee3b4 | 7156 | |
0b3589be PZ |
7157 | if (vma->vm_flags & VM_READ) |
7158 | prot |= PROT_READ; | |
7159 | if (vma->vm_flags & VM_WRITE) | |
7160 | prot |= PROT_WRITE; | |
7161 | if (vma->vm_flags & VM_EXEC) | |
7162 | prot |= PROT_EXEC; | |
7163 | ||
7164 | if (vma->vm_flags & VM_MAYSHARE) | |
7165 | flags = MAP_SHARED; | |
7166 | else | |
7167 | flags = MAP_PRIVATE; | |
7168 | ||
7169 | if (vma->vm_flags & VM_DENYWRITE) | |
7170 | flags |= MAP_DENYWRITE; | |
7171 | if (vma->vm_flags & VM_MAYEXEC) | |
7172 | flags |= MAP_EXECUTABLE; | |
7173 | if (vma->vm_flags & VM_LOCKED) | |
7174 | flags |= MAP_LOCKED; | |
7175 | if (vma->vm_flags & VM_HUGETLB) | |
7176 | flags |= MAP_HUGETLB; | |
7177 | ||
0a4a9391 | 7178 | if (file) { |
13d7a241 SE |
7179 | struct inode *inode; |
7180 | dev_t dev; | |
3ea2f2b9 | 7181 | |
2c42cfbf | 7182 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 7183 | if (!buf) { |
c7e548b4 ON |
7184 | name = "//enomem"; |
7185 | goto cpy_name; | |
0a4a9391 | 7186 | } |
413ee3b4 | 7187 | /* |
3ea2f2b9 | 7188 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
7189 | * need to add enough zero bytes after the string to handle |
7190 | * the 64bit alignment we do later. | |
7191 | */ | |
9bf39ab2 | 7192 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 7193 | if (IS_ERR(name)) { |
c7e548b4 ON |
7194 | name = "//toolong"; |
7195 | goto cpy_name; | |
0a4a9391 | 7196 | } |
13d7a241 SE |
7197 | inode = file_inode(vma->vm_file); |
7198 | dev = inode->i_sb->s_dev; | |
7199 | ino = inode->i_ino; | |
7200 | gen = inode->i_generation; | |
7201 | maj = MAJOR(dev); | |
7202 | min = MINOR(dev); | |
f972eb63 | 7203 | |
c7e548b4 | 7204 | goto got_name; |
0a4a9391 | 7205 | } else { |
fbe26abe JO |
7206 | if (vma->vm_ops && vma->vm_ops->name) { |
7207 | name = (char *) vma->vm_ops->name(vma); | |
7208 | if (name) | |
7209 | goto cpy_name; | |
7210 | } | |
7211 | ||
2c42cfbf | 7212 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
7213 | if (name) |
7214 | goto cpy_name; | |
089dd79d | 7215 | |
32c5fb7e | 7216 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 7217 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
7218 | name = "[heap]"; |
7219 | goto cpy_name; | |
32c5fb7e ON |
7220 | } |
7221 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 7222 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
7223 | name = "[stack]"; |
7224 | goto cpy_name; | |
089dd79d PZ |
7225 | } |
7226 | ||
c7e548b4 ON |
7227 | name = "//anon"; |
7228 | goto cpy_name; | |
0a4a9391 PZ |
7229 | } |
7230 | ||
c7e548b4 ON |
7231 | cpy_name: |
7232 | strlcpy(tmp, name, sizeof(tmp)); | |
7233 | name = tmp; | |
0a4a9391 | 7234 | got_name: |
2c42cfbf PZ |
7235 | /* |
7236 | * Since our buffer works in 8 byte units we need to align our string | |
7237 | * size to a multiple of 8. However, we must guarantee the tail end is | |
7238 | * zero'd out to avoid leaking random bits to userspace. | |
7239 | */ | |
7240 | size = strlen(name)+1; | |
7241 | while (!IS_ALIGNED(size, sizeof(u64))) | |
7242 | name[size++] = '\0'; | |
0a4a9391 PZ |
7243 | |
7244 | mmap_event->file_name = name; | |
7245 | mmap_event->file_size = size; | |
13d7a241 SE |
7246 | mmap_event->maj = maj; |
7247 | mmap_event->min = min; | |
7248 | mmap_event->ino = ino; | |
7249 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
7250 | mmap_event->prot = prot; |
7251 | mmap_event->flags = flags; | |
0a4a9391 | 7252 | |
2fe85427 SE |
7253 | if (!(vma->vm_flags & VM_EXEC)) |
7254 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
7255 | ||
cdd6c482 | 7256 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 7257 | |
aab5b71e | 7258 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
7259 | mmap_event, |
7260 | NULL); | |
665c2142 | 7261 | |
0a4a9391 PZ |
7262 | kfree(buf); |
7263 | } | |
7264 | ||
375637bc AS |
7265 | /* |
7266 | * Check whether inode and address range match filter criteria. | |
7267 | */ | |
7268 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
7269 | struct file *file, unsigned long offset, | |
7270 | unsigned long size) | |
7271 | { | |
45063097 | 7272 | if (filter->inode != file_inode(file)) |
375637bc AS |
7273 | return false; |
7274 | ||
7275 | if (filter->offset > offset + size) | |
7276 | return false; | |
7277 | ||
7278 | if (filter->offset + filter->size < offset) | |
7279 | return false; | |
7280 | ||
7281 | return true; | |
7282 | } | |
7283 | ||
7284 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) | |
7285 | { | |
7286 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
7287 | struct vm_area_struct *vma = data; | |
7288 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; | |
7289 | struct file *file = vma->vm_file; | |
7290 | struct perf_addr_filter *filter; | |
7291 | unsigned int restart = 0, count = 0; | |
7292 | ||
7293 | if (!has_addr_filter(event)) | |
7294 | return; | |
7295 | ||
7296 | if (!file) | |
7297 | return; | |
7298 | ||
7299 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7300 | list_for_each_entry(filter, &ifh->list, entry) { | |
7301 | if (perf_addr_filter_match(filter, file, off, | |
7302 | vma->vm_end - vma->vm_start)) { | |
7303 | event->addr_filters_offs[count] = vma->vm_start; | |
7304 | restart++; | |
7305 | } | |
7306 | ||
7307 | count++; | |
7308 | } | |
7309 | ||
7310 | if (restart) | |
7311 | event->addr_filters_gen++; | |
7312 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7313 | ||
7314 | if (restart) | |
767ae086 | 7315 | perf_event_stop(event, 1); |
375637bc AS |
7316 | } |
7317 | ||
7318 | /* | |
7319 | * Adjust all task's events' filters to the new vma | |
7320 | */ | |
7321 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
7322 | { | |
7323 | struct perf_event_context *ctx; | |
7324 | int ctxn; | |
7325 | ||
12b40a23 MP |
7326 | /* |
7327 | * Data tracing isn't supported yet and as such there is no need | |
7328 | * to keep track of anything that isn't related to executable code: | |
7329 | */ | |
7330 | if (!(vma->vm_flags & VM_EXEC)) | |
7331 | return; | |
7332 | ||
375637bc AS |
7333 | rcu_read_lock(); |
7334 | for_each_task_context_nr(ctxn) { | |
7335 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
7336 | if (!ctx) | |
7337 | continue; | |
7338 | ||
aab5b71e | 7339 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
7340 | } |
7341 | rcu_read_unlock(); | |
7342 | } | |
7343 | ||
3af9e859 | 7344 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 7345 | { |
9ee318a7 PZ |
7346 | struct perf_mmap_event mmap_event; |
7347 | ||
cdd6c482 | 7348 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
7349 | return; |
7350 | ||
7351 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 7352 | .vma = vma, |
573402db PZ |
7353 | /* .file_name */ |
7354 | /* .file_size */ | |
cdd6c482 | 7355 | .event_id = { |
573402db | 7356 | .header = { |
cdd6c482 | 7357 | .type = PERF_RECORD_MMAP, |
39447b38 | 7358 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
7359 | /* .size */ |
7360 | }, | |
7361 | /* .pid */ | |
7362 | /* .tid */ | |
089dd79d PZ |
7363 | .start = vma->vm_start, |
7364 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 7365 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 7366 | }, |
13d7a241 SE |
7367 | /* .maj (attr_mmap2 only) */ |
7368 | /* .min (attr_mmap2 only) */ | |
7369 | /* .ino (attr_mmap2 only) */ | |
7370 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
7371 | /* .prot (attr_mmap2 only) */ |
7372 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
7373 | }; |
7374 | ||
375637bc | 7375 | perf_addr_filters_adjust(vma); |
cdd6c482 | 7376 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
7377 | } |
7378 | ||
68db7e98 AS |
7379 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
7380 | unsigned long size, u64 flags) | |
7381 | { | |
7382 | struct perf_output_handle handle; | |
7383 | struct perf_sample_data sample; | |
7384 | struct perf_aux_event { | |
7385 | struct perf_event_header header; | |
7386 | u64 offset; | |
7387 | u64 size; | |
7388 | u64 flags; | |
7389 | } rec = { | |
7390 | .header = { | |
7391 | .type = PERF_RECORD_AUX, | |
7392 | .misc = 0, | |
7393 | .size = sizeof(rec), | |
7394 | }, | |
7395 | .offset = head, | |
7396 | .size = size, | |
7397 | .flags = flags, | |
7398 | }; | |
7399 | int ret; | |
7400 | ||
7401 | perf_event_header__init_id(&rec.header, &sample, event); | |
7402 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7403 | ||
7404 | if (ret) | |
7405 | return; | |
7406 | ||
7407 | perf_output_put(&handle, rec); | |
7408 | perf_event__output_id_sample(event, &handle, &sample); | |
7409 | ||
7410 | perf_output_end(&handle); | |
7411 | } | |
7412 | ||
f38b0dbb KL |
7413 | /* |
7414 | * Lost/dropped samples logging | |
7415 | */ | |
7416 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
7417 | { | |
7418 | struct perf_output_handle handle; | |
7419 | struct perf_sample_data sample; | |
7420 | int ret; | |
7421 | ||
7422 | struct { | |
7423 | struct perf_event_header header; | |
7424 | u64 lost; | |
7425 | } lost_samples_event = { | |
7426 | .header = { | |
7427 | .type = PERF_RECORD_LOST_SAMPLES, | |
7428 | .misc = 0, | |
7429 | .size = sizeof(lost_samples_event), | |
7430 | }, | |
7431 | .lost = lost, | |
7432 | }; | |
7433 | ||
7434 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
7435 | ||
7436 | ret = perf_output_begin(&handle, event, | |
7437 | lost_samples_event.header.size); | |
7438 | if (ret) | |
7439 | return; | |
7440 | ||
7441 | perf_output_put(&handle, lost_samples_event); | |
7442 | perf_event__output_id_sample(event, &handle, &sample); | |
7443 | perf_output_end(&handle); | |
7444 | } | |
7445 | ||
45ac1403 AH |
7446 | /* |
7447 | * context_switch tracking | |
7448 | */ | |
7449 | ||
7450 | struct perf_switch_event { | |
7451 | struct task_struct *task; | |
7452 | struct task_struct *next_prev; | |
7453 | ||
7454 | struct { | |
7455 | struct perf_event_header header; | |
7456 | u32 next_prev_pid; | |
7457 | u32 next_prev_tid; | |
7458 | } event_id; | |
7459 | }; | |
7460 | ||
7461 | static int perf_event_switch_match(struct perf_event *event) | |
7462 | { | |
7463 | return event->attr.context_switch; | |
7464 | } | |
7465 | ||
7466 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
7467 | { | |
7468 | struct perf_switch_event *se = data; | |
7469 | struct perf_output_handle handle; | |
7470 | struct perf_sample_data sample; | |
7471 | int ret; | |
7472 | ||
7473 | if (!perf_event_switch_match(event)) | |
7474 | return; | |
7475 | ||
7476 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
7477 | if (event->ctx->task) { | |
7478 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
7479 | se->event_id.header.size = sizeof(se->event_id.header); | |
7480 | } else { | |
7481 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
7482 | se->event_id.header.size = sizeof(se->event_id); | |
7483 | se->event_id.next_prev_pid = | |
7484 | perf_event_pid(event, se->next_prev); | |
7485 | se->event_id.next_prev_tid = | |
7486 | perf_event_tid(event, se->next_prev); | |
7487 | } | |
7488 | ||
7489 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
7490 | ||
7491 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
7492 | if (ret) | |
7493 | return; | |
7494 | ||
7495 | if (event->ctx->task) | |
7496 | perf_output_put(&handle, se->event_id.header); | |
7497 | else | |
7498 | perf_output_put(&handle, se->event_id); | |
7499 | ||
7500 | perf_event__output_id_sample(event, &handle, &sample); | |
7501 | ||
7502 | perf_output_end(&handle); | |
7503 | } | |
7504 | ||
7505 | static void perf_event_switch(struct task_struct *task, | |
7506 | struct task_struct *next_prev, bool sched_in) | |
7507 | { | |
7508 | struct perf_switch_event switch_event; | |
7509 | ||
7510 | /* N.B. caller checks nr_switch_events != 0 */ | |
7511 | ||
7512 | switch_event = (struct perf_switch_event){ | |
7513 | .task = task, | |
7514 | .next_prev = next_prev, | |
7515 | .event_id = { | |
7516 | .header = { | |
7517 | /* .type */ | |
7518 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
7519 | /* .size */ | |
7520 | }, | |
7521 | /* .next_prev_pid */ | |
7522 | /* .next_prev_tid */ | |
7523 | }, | |
7524 | }; | |
7525 | ||
aab5b71e | 7526 | perf_iterate_sb(perf_event_switch_output, |
45ac1403 AH |
7527 | &switch_event, |
7528 | NULL); | |
7529 | } | |
7530 | ||
a78ac325 PZ |
7531 | /* |
7532 | * IRQ throttle logging | |
7533 | */ | |
7534 | ||
cdd6c482 | 7535 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
7536 | { |
7537 | struct perf_output_handle handle; | |
c980d109 | 7538 | struct perf_sample_data sample; |
a78ac325 PZ |
7539 | int ret; |
7540 | ||
7541 | struct { | |
7542 | struct perf_event_header header; | |
7543 | u64 time; | |
cca3f454 | 7544 | u64 id; |
7f453c24 | 7545 | u64 stream_id; |
a78ac325 PZ |
7546 | } throttle_event = { |
7547 | .header = { | |
cdd6c482 | 7548 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
7549 | .misc = 0, |
7550 | .size = sizeof(throttle_event), | |
7551 | }, | |
34f43927 | 7552 | .time = perf_event_clock(event), |
cdd6c482 IM |
7553 | .id = primary_event_id(event), |
7554 | .stream_id = event->id, | |
a78ac325 PZ |
7555 | }; |
7556 | ||
966ee4d6 | 7557 | if (enable) |
cdd6c482 | 7558 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 7559 | |
c980d109 ACM |
7560 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
7561 | ||
7562 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7563 | throttle_event.header.size); |
a78ac325 PZ |
7564 | if (ret) |
7565 | return; | |
7566 | ||
7567 | perf_output_put(&handle, throttle_event); | |
c980d109 | 7568 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
7569 | perf_output_end(&handle); |
7570 | } | |
7571 | ||
8d4e6c4c AS |
7572 | void perf_event_itrace_started(struct perf_event *event) |
7573 | { | |
7574 | event->attach_state |= PERF_ATTACH_ITRACE; | |
7575 | } | |
7576 | ||
ec0d7729 AS |
7577 | static void perf_log_itrace_start(struct perf_event *event) |
7578 | { | |
7579 | struct perf_output_handle handle; | |
7580 | struct perf_sample_data sample; | |
7581 | struct perf_aux_event { | |
7582 | struct perf_event_header header; | |
7583 | u32 pid; | |
7584 | u32 tid; | |
7585 | } rec; | |
7586 | int ret; | |
7587 | ||
7588 | if (event->parent) | |
7589 | event = event->parent; | |
7590 | ||
7591 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
8d4e6c4c | 7592 | event->attach_state & PERF_ATTACH_ITRACE) |
ec0d7729 AS |
7593 | return; |
7594 | ||
ec0d7729 AS |
7595 | rec.header.type = PERF_RECORD_ITRACE_START; |
7596 | rec.header.misc = 0; | |
7597 | rec.header.size = sizeof(rec); | |
7598 | rec.pid = perf_event_pid(event, current); | |
7599 | rec.tid = perf_event_tid(event, current); | |
7600 | ||
7601 | perf_event_header__init_id(&rec.header, &sample, event); | |
7602 | ret = perf_output_begin(&handle, event, rec.header.size); | |
7603 | ||
7604 | if (ret) | |
7605 | return; | |
7606 | ||
7607 | perf_output_put(&handle, rec); | |
7608 | perf_event__output_id_sample(event, &handle, &sample); | |
7609 | ||
7610 | perf_output_end(&handle); | |
7611 | } | |
7612 | ||
475113d9 JO |
7613 | static int |
7614 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 7615 | { |
cdd6c482 | 7616 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 7617 | int ret = 0; |
475113d9 | 7618 | u64 seq; |
96398826 | 7619 | |
e050e3f0 SE |
7620 | seq = __this_cpu_read(perf_throttled_seq); |
7621 | if (seq != hwc->interrupts_seq) { | |
7622 | hwc->interrupts_seq = seq; | |
7623 | hwc->interrupts = 1; | |
7624 | } else { | |
7625 | hwc->interrupts++; | |
7626 | if (unlikely(throttle | |
7627 | && hwc->interrupts >= max_samples_per_tick)) { | |
7628 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 7629 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
7630 | hwc->interrupts = MAX_INTERRUPTS; |
7631 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
7632 | ret = 1; |
7633 | } | |
e050e3f0 | 7634 | } |
60db5e09 | 7635 | |
cdd6c482 | 7636 | if (event->attr.freq) { |
def0a9b2 | 7637 | u64 now = perf_clock(); |
abd50713 | 7638 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 7639 | |
abd50713 | 7640 | hwc->freq_time_stamp = now; |
bd2b5b12 | 7641 | |
abd50713 | 7642 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 7643 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
7644 | } |
7645 | ||
475113d9 JO |
7646 | return ret; |
7647 | } | |
7648 | ||
7649 | int perf_event_account_interrupt(struct perf_event *event) | |
7650 | { | |
7651 | return __perf_event_account_interrupt(event, 1); | |
7652 | } | |
7653 | ||
7654 | /* | |
7655 | * Generic event overflow handling, sampling. | |
7656 | */ | |
7657 | ||
7658 | static int __perf_event_overflow(struct perf_event *event, | |
7659 | int throttle, struct perf_sample_data *data, | |
7660 | struct pt_regs *regs) | |
7661 | { | |
7662 | int events = atomic_read(&event->event_limit); | |
7663 | int ret = 0; | |
7664 | ||
7665 | /* | |
7666 | * Non-sampling counters might still use the PMI to fold short | |
7667 | * hardware counters, ignore those. | |
7668 | */ | |
7669 | if (unlikely(!is_sampling_event(event))) | |
7670 | return 0; | |
7671 | ||
7672 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 7673 | |
2023b359 PZ |
7674 | /* |
7675 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 7676 | * events |
2023b359 PZ |
7677 | */ |
7678 | ||
cdd6c482 IM |
7679 | event->pending_kill = POLL_IN; |
7680 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 7681 | ret = 1; |
cdd6c482 | 7682 | event->pending_kill = POLL_HUP; |
5aab90ce JO |
7683 | |
7684 | perf_event_disable_inatomic(event); | |
79f14641 PZ |
7685 | } |
7686 | ||
aa6a5f3c | 7687 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 7688 | |
fed66e2c | 7689 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
7690 | event->pending_wakeup = 1; |
7691 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
7692 | } |
7693 | ||
79f14641 | 7694 | return ret; |
f6c7d5fe PZ |
7695 | } |
7696 | ||
a8b0ca17 | 7697 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
7698 | struct perf_sample_data *data, |
7699 | struct pt_regs *regs) | |
850bc73f | 7700 | { |
a8b0ca17 | 7701 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
7702 | } |
7703 | ||
15dbf27c | 7704 | /* |
cdd6c482 | 7705 | * Generic software event infrastructure |
15dbf27c PZ |
7706 | */ |
7707 | ||
b28ab83c PZ |
7708 | struct swevent_htable { |
7709 | struct swevent_hlist *swevent_hlist; | |
7710 | struct mutex hlist_mutex; | |
7711 | int hlist_refcount; | |
7712 | ||
7713 | /* Recursion avoidance in each contexts */ | |
7714 | int recursion[PERF_NR_CONTEXTS]; | |
7715 | }; | |
7716 | ||
7717 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
7718 | ||
7b4b6658 | 7719 | /* |
cdd6c482 IM |
7720 | * We directly increment event->count and keep a second value in |
7721 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
7722 | * is kept in the range [-sample_period, 0] so that we can use the |
7723 | * sign as trigger. | |
7724 | */ | |
7725 | ||
ab573844 | 7726 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 7727 | { |
cdd6c482 | 7728 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
7729 | u64 period = hwc->last_period; |
7730 | u64 nr, offset; | |
7731 | s64 old, val; | |
7732 | ||
7733 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
7734 | |
7735 | again: | |
e7850595 | 7736 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
7737 | if (val < 0) |
7738 | return 0; | |
15dbf27c | 7739 | |
7b4b6658 PZ |
7740 | nr = div64_u64(period + val, period); |
7741 | offset = nr * period; | |
7742 | val -= offset; | |
e7850595 | 7743 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 7744 | goto again; |
15dbf27c | 7745 | |
7b4b6658 | 7746 | return nr; |
15dbf27c PZ |
7747 | } |
7748 | ||
0cff784a | 7749 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 7750 | struct perf_sample_data *data, |
5622f295 | 7751 | struct pt_regs *regs) |
15dbf27c | 7752 | { |
cdd6c482 | 7753 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 7754 | int throttle = 0; |
15dbf27c | 7755 | |
0cff784a PZ |
7756 | if (!overflow) |
7757 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 7758 | |
7b4b6658 PZ |
7759 | if (hwc->interrupts == MAX_INTERRUPTS) |
7760 | return; | |
15dbf27c | 7761 | |
7b4b6658 | 7762 | for (; overflow; overflow--) { |
a8b0ca17 | 7763 | if (__perf_event_overflow(event, throttle, |
5622f295 | 7764 | data, regs)) { |
7b4b6658 PZ |
7765 | /* |
7766 | * We inhibit the overflow from happening when | |
7767 | * hwc->interrupts == MAX_INTERRUPTS. | |
7768 | */ | |
7769 | break; | |
7770 | } | |
cf450a73 | 7771 | throttle = 1; |
7b4b6658 | 7772 | } |
15dbf27c PZ |
7773 | } |
7774 | ||
a4eaf7f1 | 7775 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 7776 | struct perf_sample_data *data, |
5622f295 | 7777 | struct pt_regs *regs) |
7b4b6658 | 7778 | { |
cdd6c482 | 7779 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 7780 | |
e7850595 | 7781 | local64_add(nr, &event->count); |
d6d020e9 | 7782 | |
0cff784a PZ |
7783 | if (!regs) |
7784 | return; | |
7785 | ||
6c7e550f | 7786 | if (!is_sampling_event(event)) |
7b4b6658 | 7787 | return; |
d6d020e9 | 7788 | |
5d81e5cf AV |
7789 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
7790 | data->period = nr; | |
7791 | return perf_swevent_overflow(event, 1, data, regs); | |
7792 | } else | |
7793 | data->period = event->hw.last_period; | |
7794 | ||
0cff784a | 7795 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 7796 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 7797 | |
e7850595 | 7798 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 7799 | return; |
df1a132b | 7800 | |
a8b0ca17 | 7801 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
7802 | } |
7803 | ||
f5ffe02e FW |
7804 | static int perf_exclude_event(struct perf_event *event, |
7805 | struct pt_regs *regs) | |
7806 | { | |
a4eaf7f1 | 7807 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 7808 | return 1; |
a4eaf7f1 | 7809 | |
f5ffe02e FW |
7810 | if (regs) { |
7811 | if (event->attr.exclude_user && user_mode(regs)) | |
7812 | return 1; | |
7813 | ||
7814 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
7815 | return 1; | |
7816 | } | |
7817 | ||
7818 | return 0; | |
7819 | } | |
7820 | ||
cdd6c482 | 7821 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 7822 | enum perf_type_id type, |
6fb2915d LZ |
7823 | u32 event_id, |
7824 | struct perf_sample_data *data, | |
7825 | struct pt_regs *regs) | |
15dbf27c | 7826 | { |
cdd6c482 | 7827 | if (event->attr.type != type) |
a21ca2ca | 7828 | return 0; |
f5ffe02e | 7829 | |
cdd6c482 | 7830 | if (event->attr.config != event_id) |
15dbf27c PZ |
7831 | return 0; |
7832 | ||
f5ffe02e FW |
7833 | if (perf_exclude_event(event, regs)) |
7834 | return 0; | |
15dbf27c PZ |
7835 | |
7836 | return 1; | |
7837 | } | |
7838 | ||
76e1d904 FW |
7839 | static inline u64 swevent_hash(u64 type, u32 event_id) |
7840 | { | |
7841 | u64 val = event_id | (type << 32); | |
7842 | ||
7843 | return hash_64(val, SWEVENT_HLIST_BITS); | |
7844 | } | |
7845 | ||
49f135ed FW |
7846 | static inline struct hlist_head * |
7847 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 7848 | { |
49f135ed FW |
7849 | u64 hash = swevent_hash(type, event_id); |
7850 | ||
7851 | return &hlist->heads[hash]; | |
7852 | } | |
76e1d904 | 7853 | |
49f135ed FW |
7854 | /* For the read side: events when they trigger */ |
7855 | static inline struct hlist_head * | |
b28ab83c | 7856 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
7857 | { |
7858 | struct swevent_hlist *hlist; | |
76e1d904 | 7859 | |
b28ab83c | 7860 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
7861 | if (!hlist) |
7862 | return NULL; | |
7863 | ||
49f135ed FW |
7864 | return __find_swevent_head(hlist, type, event_id); |
7865 | } | |
7866 | ||
7867 | /* For the event head insertion and removal in the hlist */ | |
7868 | static inline struct hlist_head * | |
b28ab83c | 7869 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
7870 | { |
7871 | struct swevent_hlist *hlist; | |
7872 | u32 event_id = event->attr.config; | |
7873 | u64 type = event->attr.type; | |
7874 | ||
7875 | /* | |
7876 | * Event scheduling is always serialized against hlist allocation | |
7877 | * and release. Which makes the protected version suitable here. | |
7878 | * The context lock guarantees that. | |
7879 | */ | |
b28ab83c | 7880 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
7881 | lockdep_is_held(&event->ctx->lock)); |
7882 | if (!hlist) | |
7883 | return NULL; | |
7884 | ||
7885 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
7886 | } |
7887 | ||
7888 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 7889 | u64 nr, |
76e1d904 FW |
7890 | struct perf_sample_data *data, |
7891 | struct pt_regs *regs) | |
15dbf27c | 7892 | { |
4a32fea9 | 7893 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7894 | struct perf_event *event; |
76e1d904 | 7895 | struct hlist_head *head; |
15dbf27c | 7896 | |
76e1d904 | 7897 | rcu_read_lock(); |
b28ab83c | 7898 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
7899 | if (!head) |
7900 | goto end; | |
7901 | ||
b67bfe0d | 7902 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 7903 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 7904 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 7905 | } |
76e1d904 FW |
7906 | end: |
7907 | rcu_read_unlock(); | |
15dbf27c PZ |
7908 | } |
7909 | ||
86038c5e PZI |
7910 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
7911 | ||
4ed7c92d | 7912 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 7913 | { |
4a32fea9 | 7914 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 7915 | |
b28ab83c | 7916 | return get_recursion_context(swhash->recursion); |
96f6d444 | 7917 | } |
645e8cc0 | 7918 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 7919 | |
98b5c2c6 | 7920 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 7921 | { |
4a32fea9 | 7922 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 7923 | |
b28ab83c | 7924 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 7925 | } |
15dbf27c | 7926 | |
86038c5e | 7927 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 7928 | { |
a4234bfc | 7929 | struct perf_sample_data data; |
4ed7c92d | 7930 | |
86038c5e | 7931 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 7932 | return; |
a4234bfc | 7933 | |
fd0d000b | 7934 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 7935 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
7936 | } |
7937 | ||
7938 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
7939 | { | |
7940 | int rctx; | |
7941 | ||
7942 | preempt_disable_notrace(); | |
7943 | rctx = perf_swevent_get_recursion_context(); | |
7944 | if (unlikely(rctx < 0)) | |
7945 | goto fail; | |
7946 | ||
7947 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
7948 | |
7949 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 7950 | fail: |
1c024eca | 7951 | preempt_enable_notrace(); |
b8e83514 PZ |
7952 | } |
7953 | ||
cdd6c482 | 7954 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 7955 | { |
15dbf27c PZ |
7956 | } |
7957 | ||
a4eaf7f1 | 7958 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 7959 | { |
4a32fea9 | 7960 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7961 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
7962 | struct hlist_head *head; |
7963 | ||
6c7e550f | 7964 | if (is_sampling_event(event)) { |
7b4b6658 | 7965 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 7966 | perf_swevent_set_period(event); |
7b4b6658 | 7967 | } |
76e1d904 | 7968 | |
a4eaf7f1 PZ |
7969 | hwc->state = !(flags & PERF_EF_START); |
7970 | ||
b28ab83c | 7971 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 7972 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
7973 | return -EINVAL; |
7974 | ||
7975 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 7976 | perf_event_update_userpage(event); |
76e1d904 | 7977 | |
15dbf27c PZ |
7978 | return 0; |
7979 | } | |
7980 | ||
a4eaf7f1 | 7981 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 7982 | { |
76e1d904 | 7983 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
7984 | } |
7985 | ||
a4eaf7f1 | 7986 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 7987 | { |
a4eaf7f1 | 7988 | event->hw.state = 0; |
d6d020e9 | 7989 | } |
aa9c4c0f | 7990 | |
a4eaf7f1 | 7991 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 7992 | { |
a4eaf7f1 | 7993 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
7994 | } |
7995 | ||
49f135ed FW |
7996 | /* Deref the hlist from the update side */ |
7997 | static inline struct swevent_hlist * | |
b28ab83c | 7998 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 7999 | { |
b28ab83c PZ |
8000 | return rcu_dereference_protected(swhash->swevent_hlist, |
8001 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
8002 | } |
8003 | ||
b28ab83c | 8004 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 8005 | { |
b28ab83c | 8006 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 8007 | |
49f135ed | 8008 | if (!hlist) |
76e1d904 FW |
8009 | return; |
8010 | ||
70691d4a | 8011 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 8012 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
8013 | } |
8014 | ||
3b364d7b | 8015 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 8016 | { |
b28ab83c | 8017 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 8018 | |
b28ab83c | 8019 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 8020 | |
b28ab83c PZ |
8021 | if (!--swhash->hlist_refcount) |
8022 | swevent_hlist_release(swhash); | |
76e1d904 | 8023 | |
b28ab83c | 8024 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
8025 | } |
8026 | ||
3b364d7b | 8027 | static void swevent_hlist_put(void) |
76e1d904 FW |
8028 | { |
8029 | int cpu; | |
8030 | ||
76e1d904 | 8031 | for_each_possible_cpu(cpu) |
3b364d7b | 8032 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
8033 | } |
8034 | ||
3b364d7b | 8035 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 8036 | { |
b28ab83c | 8037 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
8038 | int err = 0; |
8039 | ||
b28ab83c | 8040 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
8041 | if (!swevent_hlist_deref(swhash) && |
8042 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
8043 | struct swevent_hlist *hlist; |
8044 | ||
8045 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
8046 | if (!hlist) { | |
8047 | err = -ENOMEM; | |
8048 | goto exit; | |
8049 | } | |
b28ab83c | 8050 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 8051 | } |
b28ab83c | 8052 | swhash->hlist_refcount++; |
9ed6060d | 8053 | exit: |
b28ab83c | 8054 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
8055 | |
8056 | return err; | |
8057 | } | |
8058 | ||
3b364d7b | 8059 | static int swevent_hlist_get(void) |
76e1d904 | 8060 | { |
3b364d7b | 8061 | int err, cpu, failed_cpu; |
76e1d904 | 8062 | |
a63fbed7 | 8063 | mutex_lock(&pmus_lock); |
76e1d904 | 8064 | for_each_possible_cpu(cpu) { |
3b364d7b | 8065 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
8066 | if (err) { |
8067 | failed_cpu = cpu; | |
8068 | goto fail; | |
8069 | } | |
8070 | } | |
a63fbed7 | 8071 | mutex_unlock(&pmus_lock); |
76e1d904 | 8072 | return 0; |
9ed6060d | 8073 | fail: |
76e1d904 FW |
8074 | for_each_possible_cpu(cpu) { |
8075 | if (cpu == failed_cpu) | |
8076 | break; | |
3b364d7b | 8077 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 8078 | } |
a63fbed7 | 8079 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
8080 | return err; |
8081 | } | |
8082 | ||
c5905afb | 8083 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 8084 | |
b0a873eb PZ |
8085 | static void sw_perf_event_destroy(struct perf_event *event) |
8086 | { | |
8087 | u64 event_id = event->attr.config; | |
95476b64 | 8088 | |
b0a873eb PZ |
8089 | WARN_ON(event->parent); |
8090 | ||
c5905afb | 8091 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 8092 | swevent_hlist_put(); |
b0a873eb PZ |
8093 | } |
8094 | ||
8095 | static int perf_swevent_init(struct perf_event *event) | |
8096 | { | |
8176cced | 8097 | u64 event_id = event->attr.config; |
b0a873eb PZ |
8098 | |
8099 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
8100 | return -ENOENT; | |
8101 | ||
2481c5fa SE |
8102 | /* |
8103 | * no branch sampling for software events | |
8104 | */ | |
8105 | if (has_branch_stack(event)) | |
8106 | return -EOPNOTSUPP; | |
8107 | ||
b0a873eb PZ |
8108 | switch (event_id) { |
8109 | case PERF_COUNT_SW_CPU_CLOCK: | |
8110 | case PERF_COUNT_SW_TASK_CLOCK: | |
8111 | return -ENOENT; | |
8112 | ||
8113 | default: | |
8114 | break; | |
8115 | } | |
8116 | ||
ce677831 | 8117 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
8118 | return -ENOENT; |
8119 | ||
8120 | if (!event->parent) { | |
8121 | int err; | |
8122 | ||
3b364d7b | 8123 | err = swevent_hlist_get(); |
b0a873eb PZ |
8124 | if (err) |
8125 | return err; | |
8126 | ||
c5905afb | 8127 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
8128 | event->destroy = sw_perf_event_destroy; |
8129 | } | |
8130 | ||
8131 | return 0; | |
8132 | } | |
8133 | ||
8134 | static struct pmu perf_swevent = { | |
89a1e187 | 8135 | .task_ctx_nr = perf_sw_context, |
95476b64 | 8136 | |
34f43927 PZ |
8137 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8138 | ||
b0a873eb | 8139 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
8140 | .add = perf_swevent_add, |
8141 | .del = perf_swevent_del, | |
8142 | .start = perf_swevent_start, | |
8143 | .stop = perf_swevent_stop, | |
1c024eca | 8144 | .read = perf_swevent_read, |
1c024eca PZ |
8145 | }; |
8146 | ||
b0a873eb PZ |
8147 | #ifdef CONFIG_EVENT_TRACING |
8148 | ||
1c024eca PZ |
8149 | static int perf_tp_filter_match(struct perf_event *event, |
8150 | struct perf_sample_data *data) | |
8151 | { | |
7e3f977e | 8152 | void *record = data->raw->frag.data; |
1c024eca | 8153 | |
b71b437e PZ |
8154 | /* only top level events have filters set */ |
8155 | if (event->parent) | |
8156 | event = event->parent; | |
8157 | ||
1c024eca PZ |
8158 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
8159 | return 1; | |
8160 | return 0; | |
8161 | } | |
8162 | ||
8163 | static int perf_tp_event_match(struct perf_event *event, | |
8164 | struct perf_sample_data *data, | |
8165 | struct pt_regs *regs) | |
8166 | { | |
a0f7d0f7 FW |
8167 | if (event->hw.state & PERF_HES_STOPPED) |
8168 | return 0; | |
580d607c PZ |
8169 | /* |
8170 | * All tracepoints are from kernel-space. | |
8171 | */ | |
8172 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
8173 | return 0; |
8174 | ||
8175 | if (!perf_tp_filter_match(event, data)) | |
8176 | return 0; | |
8177 | ||
8178 | return 1; | |
8179 | } | |
8180 | ||
85b67bcb AS |
8181 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
8182 | struct trace_event_call *call, u64 count, | |
8183 | struct pt_regs *regs, struct hlist_head *head, | |
8184 | struct task_struct *task) | |
8185 | { | |
e87c6bc3 | 8186 | if (bpf_prog_array_valid(call)) { |
85b67bcb | 8187 | *(struct pt_regs **)raw_data = regs; |
e87c6bc3 | 8188 | if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { |
85b67bcb AS |
8189 | perf_swevent_put_recursion_context(rctx); |
8190 | return; | |
8191 | } | |
8192 | } | |
8193 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
8fd0fbbe | 8194 | rctx, task); |
85b67bcb AS |
8195 | } |
8196 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
8197 | ||
1e1dcd93 | 8198 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 8199 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
8fd0fbbe | 8200 | struct task_struct *task) |
95476b64 FW |
8201 | { |
8202 | struct perf_sample_data data; | |
8fd0fbbe | 8203 | struct perf_event *event; |
1c024eca | 8204 | |
95476b64 | 8205 | struct perf_raw_record raw = { |
7e3f977e DB |
8206 | .frag = { |
8207 | .size = entry_size, | |
8208 | .data = record, | |
8209 | }, | |
95476b64 FW |
8210 | }; |
8211 | ||
1e1dcd93 | 8212 | perf_sample_data_init(&data, 0, 0); |
95476b64 FW |
8213 | data.raw = &raw; |
8214 | ||
1e1dcd93 AS |
8215 | perf_trace_buf_update(record, event_type); |
8216 | ||
8fd0fbbe | 8217 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 8218 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 8219 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 8220 | } |
ecc55f84 | 8221 | |
e6dab5ff AV |
8222 | /* |
8223 | * If we got specified a target task, also iterate its context and | |
8224 | * deliver this event there too. | |
8225 | */ | |
8226 | if (task && task != current) { | |
8227 | struct perf_event_context *ctx; | |
8228 | struct trace_entry *entry = record; | |
8229 | ||
8230 | rcu_read_lock(); | |
8231 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
8232 | if (!ctx) | |
8233 | goto unlock; | |
8234 | ||
8235 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
8236 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
8237 | continue; | |
8238 | if (event->attr.config != entry->type) | |
8239 | continue; | |
8240 | if (perf_tp_event_match(event, &data, regs)) | |
8241 | perf_swevent_event(event, count, &data, regs); | |
8242 | } | |
8243 | unlock: | |
8244 | rcu_read_unlock(); | |
8245 | } | |
8246 | ||
ecc55f84 | 8247 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
8248 | } |
8249 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
8250 | ||
cdd6c482 | 8251 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 8252 | { |
1c024eca | 8253 | perf_trace_destroy(event); |
e077df4f PZ |
8254 | } |
8255 | ||
b0a873eb | 8256 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 8257 | { |
76e1d904 FW |
8258 | int err; |
8259 | ||
b0a873eb PZ |
8260 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
8261 | return -ENOENT; | |
8262 | ||
2481c5fa SE |
8263 | /* |
8264 | * no branch sampling for tracepoint events | |
8265 | */ | |
8266 | if (has_branch_stack(event)) | |
8267 | return -EOPNOTSUPP; | |
8268 | ||
1c024eca PZ |
8269 | err = perf_trace_init(event); |
8270 | if (err) | |
b0a873eb | 8271 | return err; |
e077df4f | 8272 | |
cdd6c482 | 8273 | event->destroy = tp_perf_event_destroy; |
e077df4f | 8274 | |
b0a873eb PZ |
8275 | return 0; |
8276 | } | |
8277 | ||
8278 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
8279 | .task_ctx_nr = perf_sw_context, |
8280 | ||
b0a873eb | 8281 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
8282 | .add = perf_trace_add, |
8283 | .del = perf_trace_del, | |
8284 | .start = perf_swevent_start, | |
8285 | .stop = perf_swevent_stop, | |
b0a873eb | 8286 | .read = perf_swevent_read, |
b0a873eb PZ |
8287 | }; |
8288 | ||
33ea4b24 | 8289 | #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) |
e12f03d7 SL |
8290 | /* |
8291 | * Flags in config, used by dynamic PMU kprobe and uprobe | |
8292 | * The flags should match following PMU_FORMAT_ATTR(). | |
8293 | * | |
8294 | * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe | |
8295 | * if not set, create kprobe/uprobe | |
8296 | */ | |
8297 | enum perf_probe_config { | |
8298 | PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */ | |
8299 | }; | |
8300 | ||
8301 | PMU_FORMAT_ATTR(retprobe, "config:0"); | |
8302 | ||
8303 | static struct attribute *probe_attrs[] = { | |
8304 | &format_attr_retprobe.attr, | |
8305 | NULL, | |
8306 | }; | |
8307 | ||
8308 | static struct attribute_group probe_format_group = { | |
8309 | .name = "format", | |
8310 | .attrs = probe_attrs, | |
8311 | }; | |
8312 | ||
8313 | static const struct attribute_group *probe_attr_groups[] = { | |
8314 | &probe_format_group, | |
8315 | NULL, | |
8316 | }; | |
33ea4b24 | 8317 | #endif |
e12f03d7 | 8318 | |
33ea4b24 | 8319 | #ifdef CONFIG_KPROBE_EVENTS |
e12f03d7 SL |
8320 | static int perf_kprobe_event_init(struct perf_event *event); |
8321 | static struct pmu perf_kprobe = { | |
8322 | .task_ctx_nr = perf_sw_context, | |
8323 | .event_init = perf_kprobe_event_init, | |
8324 | .add = perf_trace_add, | |
8325 | .del = perf_trace_del, | |
8326 | .start = perf_swevent_start, | |
8327 | .stop = perf_swevent_stop, | |
8328 | .read = perf_swevent_read, | |
8329 | .attr_groups = probe_attr_groups, | |
8330 | }; | |
8331 | ||
8332 | static int perf_kprobe_event_init(struct perf_event *event) | |
8333 | { | |
8334 | int err; | |
8335 | bool is_retprobe; | |
8336 | ||
8337 | if (event->attr.type != perf_kprobe.type) | |
8338 | return -ENOENT; | |
8339 | /* | |
8340 | * no branch sampling for probe events | |
8341 | */ | |
8342 | if (has_branch_stack(event)) | |
8343 | return -EOPNOTSUPP; | |
8344 | ||
8345 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
8346 | err = perf_kprobe_init(event, is_retprobe); | |
8347 | if (err) | |
8348 | return err; | |
8349 | ||
8350 | event->destroy = perf_kprobe_destroy; | |
8351 | ||
8352 | return 0; | |
8353 | } | |
8354 | #endif /* CONFIG_KPROBE_EVENTS */ | |
8355 | ||
33ea4b24 SL |
8356 | #ifdef CONFIG_UPROBE_EVENTS |
8357 | static int perf_uprobe_event_init(struct perf_event *event); | |
8358 | static struct pmu perf_uprobe = { | |
8359 | .task_ctx_nr = perf_sw_context, | |
8360 | .event_init = perf_uprobe_event_init, | |
8361 | .add = perf_trace_add, | |
8362 | .del = perf_trace_del, | |
8363 | .start = perf_swevent_start, | |
8364 | .stop = perf_swevent_stop, | |
8365 | .read = perf_swevent_read, | |
8366 | .attr_groups = probe_attr_groups, | |
8367 | }; | |
8368 | ||
8369 | static int perf_uprobe_event_init(struct perf_event *event) | |
8370 | { | |
8371 | int err; | |
8372 | bool is_retprobe; | |
8373 | ||
8374 | if (event->attr.type != perf_uprobe.type) | |
8375 | return -ENOENT; | |
8376 | /* | |
8377 | * no branch sampling for probe events | |
8378 | */ | |
8379 | if (has_branch_stack(event)) | |
8380 | return -EOPNOTSUPP; | |
8381 | ||
8382 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
8383 | err = perf_uprobe_init(event, is_retprobe); | |
8384 | if (err) | |
8385 | return err; | |
8386 | ||
8387 | event->destroy = perf_uprobe_destroy; | |
8388 | ||
8389 | return 0; | |
8390 | } | |
8391 | #endif /* CONFIG_UPROBE_EVENTS */ | |
8392 | ||
b0a873eb PZ |
8393 | static inline void perf_tp_register(void) |
8394 | { | |
2e80a82a | 8395 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e12f03d7 SL |
8396 | #ifdef CONFIG_KPROBE_EVENTS |
8397 | perf_pmu_register(&perf_kprobe, "kprobe", -1); | |
8398 | #endif | |
33ea4b24 SL |
8399 | #ifdef CONFIG_UPROBE_EVENTS |
8400 | perf_pmu_register(&perf_uprobe, "uprobe", -1); | |
8401 | #endif | |
e077df4f | 8402 | } |
6fb2915d | 8403 | |
6fb2915d LZ |
8404 | static void perf_event_free_filter(struct perf_event *event) |
8405 | { | |
8406 | ftrace_profile_free_filter(event); | |
8407 | } | |
8408 | ||
aa6a5f3c AS |
8409 | #ifdef CONFIG_BPF_SYSCALL |
8410 | static void bpf_overflow_handler(struct perf_event *event, | |
8411 | struct perf_sample_data *data, | |
8412 | struct pt_regs *regs) | |
8413 | { | |
8414 | struct bpf_perf_event_data_kern ctx = { | |
8415 | .data = data, | |
7d9285e8 | 8416 | .event = event, |
aa6a5f3c AS |
8417 | }; |
8418 | int ret = 0; | |
8419 | ||
c895f6f7 | 8420 | ctx.regs = perf_arch_bpf_user_pt_regs(regs); |
aa6a5f3c AS |
8421 | preempt_disable(); |
8422 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) | |
8423 | goto out; | |
8424 | rcu_read_lock(); | |
88575199 | 8425 | ret = BPF_PROG_RUN(event->prog, &ctx); |
aa6a5f3c AS |
8426 | rcu_read_unlock(); |
8427 | out: | |
8428 | __this_cpu_dec(bpf_prog_active); | |
8429 | preempt_enable(); | |
8430 | if (!ret) | |
8431 | return; | |
8432 | ||
8433 | event->orig_overflow_handler(event, data, regs); | |
8434 | } | |
8435 | ||
8436 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8437 | { | |
8438 | struct bpf_prog *prog; | |
8439 | ||
8440 | if (event->overflow_handler_context) | |
8441 | /* hw breakpoint or kernel counter */ | |
8442 | return -EINVAL; | |
8443 | ||
8444 | if (event->prog) | |
8445 | return -EEXIST; | |
8446 | ||
8447 | prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); | |
8448 | if (IS_ERR(prog)) | |
8449 | return PTR_ERR(prog); | |
8450 | ||
8451 | event->prog = prog; | |
8452 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); | |
8453 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
8454 | return 0; | |
8455 | } | |
8456 | ||
8457 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8458 | { | |
8459 | struct bpf_prog *prog = event->prog; | |
8460 | ||
8461 | if (!prog) | |
8462 | return; | |
8463 | ||
8464 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
8465 | event->prog = NULL; | |
8466 | bpf_prog_put(prog); | |
8467 | } | |
8468 | #else | |
8469 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
8470 | { | |
8471 | return -EOPNOTSUPP; | |
8472 | } | |
8473 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
8474 | { | |
8475 | } | |
8476 | #endif | |
8477 | ||
e12f03d7 SL |
8478 | /* |
8479 | * returns true if the event is a tracepoint, or a kprobe/upprobe created | |
8480 | * with perf_event_open() | |
8481 | */ | |
8482 | static inline bool perf_event_is_tracing(struct perf_event *event) | |
8483 | { | |
8484 | if (event->pmu == &perf_tracepoint) | |
8485 | return true; | |
8486 | #ifdef CONFIG_KPROBE_EVENTS | |
8487 | if (event->pmu == &perf_kprobe) | |
8488 | return true; | |
33ea4b24 SL |
8489 | #endif |
8490 | #ifdef CONFIG_UPROBE_EVENTS | |
8491 | if (event->pmu == &perf_uprobe) | |
8492 | return true; | |
e12f03d7 SL |
8493 | #endif |
8494 | return false; | |
8495 | } | |
8496 | ||
2541517c AS |
8497 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8498 | { | |
cf5f5cea | 8499 | bool is_kprobe, is_tracepoint, is_syscall_tp; |
2541517c | 8500 | struct bpf_prog *prog; |
e87c6bc3 | 8501 | int ret; |
2541517c | 8502 | |
e12f03d7 | 8503 | if (!perf_event_is_tracing(event)) |
f91840a3 | 8504 | return perf_event_set_bpf_handler(event, prog_fd); |
2541517c | 8505 | |
98b5c2c6 AS |
8506 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; |
8507 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; | |
cf5f5cea YS |
8508 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
8509 | if (!is_kprobe && !is_tracepoint && !is_syscall_tp) | |
98b5c2c6 | 8510 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
2541517c AS |
8511 | return -EINVAL; |
8512 | ||
8513 | prog = bpf_prog_get(prog_fd); | |
8514 | if (IS_ERR(prog)) | |
8515 | return PTR_ERR(prog); | |
8516 | ||
98b5c2c6 | 8517 | if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || |
cf5f5cea YS |
8518 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || |
8519 | (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { | |
2541517c AS |
8520 | /* valid fd, but invalid bpf program type */ |
8521 | bpf_prog_put(prog); | |
8522 | return -EINVAL; | |
8523 | } | |
8524 | ||
9802d865 JB |
8525 | /* Kprobe override only works for kprobes, not uprobes. */ |
8526 | if (prog->kprobe_override && | |
8527 | !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) { | |
8528 | bpf_prog_put(prog); | |
8529 | return -EINVAL; | |
8530 | } | |
8531 | ||
cf5f5cea | 8532 | if (is_tracepoint || is_syscall_tp) { |
32bbe007 AS |
8533 | int off = trace_event_get_offsets(event->tp_event); |
8534 | ||
8535 | if (prog->aux->max_ctx_offset > off) { | |
8536 | bpf_prog_put(prog); | |
8537 | return -EACCES; | |
8538 | } | |
8539 | } | |
2541517c | 8540 | |
e87c6bc3 YS |
8541 | ret = perf_event_attach_bpf_prog(event, prog); |
8542 | if (ret) | |
8543 | bpf_prog_put(prog); | |
8544 | return ret; | |
2541517c AS |
8545 | } |
8546 | ||
8547 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8548 | { | |
e12f03d7 | 8549 | if (!perf_event_is_tracing(event)) { |
0b4c6841 | 8550 | perf_event_free_bpf_handler(event); |
2541517c | 8551 | return; |
2541517c | 8552 | } |
e87c6bc3 | 8553 | perf_event_detach_bpf_prog(event); |
2541517c AS |
8554 | } |
8555 | ||
e077df4f | 8556 | #else |
6fb2915d | 8557 | |
b0a873eb | 8558 | static inline void perf_tp_register(void) |
e077df4f | 8559 | { |
e077df4f | 8560 | } |
6fb2915d | 8561 | |
6fb2915d LZ |
8562 | static void perf_event_free_filter(struct perf_event *event) |
8563 | { | |
8564 | } | |
8565 | ||
2541517c AS |
8566 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
8567 | { | |
8568 | return -ENOENT; | |
8569 | } | |
8570 | ||
8571 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
8572 | { | |
8573 | } | |
07b139c8 | 8574 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 8575 | |
24f1e32c | 8576 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 8577 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 8578 | { |
f5ffe02e FW |
8579 | struct perf_sample_data sample; |
8580 | struct pt_regs *regs = data; | |
8581 | ||
fd0d000b | 8582 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 8583 | |
a4eaf7f1 | 8584 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 8585 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
8586 | } |
8587 | #endif | |
8588 | ||
375637bc AS |
8589 | /* |
8590 | * Allocate a new address filter | |
8591 | */ | |
8592 | static struct perf_addr_filter * | |
8593 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
8594 | { | |
8595 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
8596 | struct perf_addr_filter *filter; | |
8597 | ||
8598 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
8599 | if (!filter) | |
8600 | return NULL; | |
8601 | ||
8602 | INIT_LIST_HEAD(&filter->entry); | |
8603 | list_add_tail(&filter->entry, filters); | |
8604 | ||
8605 | return filter; | |
8606 | } | |
8607 | ||
8608 | static void free_filters_list(struct list_head *filters) | |
8609 | { | |
8610 | struct perf_addr_filter *filter, *iter; | |
8611 | ||
8612 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
8613 | if (filter->inode) | |
8614 | iput(filter->inode); | |
8615 | list_del(&filter->entry); | |
8616 | kfree(filter); | |
8617 | } | |
8618 | } | |
8619 | ||
8620 | /* | |
8621 | * Free existing address filters and optionally install new ones | |
8622 | */ | |
8623 | static void perf_addr_filters_splice(struct perf_event *event, | |
8624 | struct list_head *head) | |
8625 | { | |
8626 | unsigned long flags; | |
8627 | LIST_HEAD(list); | |
8628 | ||
8629 | if (!has_addr_filter(event)) | |
8630 | return; | |
8631 | ||
8632 | /* don't bother with children, they don't have their own filters */ | |
8633 | if (event->parent) | |
8634 | return; | |
8635 | ||
8636 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
8637 | ||
8638 | list_splice_init(&event->addr_filters.list, &list); | |
8639 | if (head) | |
8640 | list_splice(head, &event->addr_filters.list); | |
8641 | ||
8642 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
8643 | ||
8644 | free_filters_list(&list); | |
8645 | } | |
8646 | ||
8647 | /* | |
8648 | * Scan through mm's vmas and see if one of them matches the | |
8649 | * @filter; if so, adjust filter's address range. | |
8650 | * Called with mm::mmap_sem down for reading. | |
8651 | */ | |
8652 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, | |
8653 | struct mm_struct *mm) | |
8654 | { | |
8655 | struct vm_area_struct *vma; | |
8656 | ||
8657 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
8658 | struct file *file = vma->vm_file; | |
8659 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8660 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8661 | ||
8662 | if (!file) | |
8663 | continue; | |
8664 | ||
8665 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8666 | continue; | |
8667 | ||
8668 | return vma->vm_start; | |
8669 | } | |
8670 | ||
8671 | return 0; | |
8672 | } | |
8673 | ||
8674 | /* | |
8675 | * Update event's address range filters based on the | |
8676 | * task's existing mappings, if any. | |
8677 | */ | |
8678 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
8679 | { | |
8680 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8681 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
8682 | struct perf_addr_filter *filter; | |
8683 | struct mm_struct *mm = NULL; | |
8684 | unsigned int count = 0; | |
8685 | unsigned long flags; | |
8686 | ||
8687 | /* | |
8688 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
8689 | * will stop on the parent's child_mutex that our caller is also holding | |
8690 | */ | |
8691 | if (task == TASK_TOMBSTONE) | |
8692 | return; | |
8693 | ||
6ce77bfd AS |
8694 | if (!ifh->nr_file_filters) |
8695 | return; | |
8696 | ||
375637bc AS |
8697 | mm = get_task_mm(event->ctx->task); |
8698 | if (!mm) | |
8699 | goto restart; | |
8700 | ||
8701 | down_read(&mm->mmap_sem); | |
8702 | ||
8703 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8704 | list_for_each_entry(filter, &ifh->list, entry) { | |
8705 | event->addr_filters_offs[count] = 0; | |
8706 | ||
99f5bc9b MP |
8707 | /* |
8708 | * Adjust base offset if the filter is associated to a binary | |
8709 | * that needs to be mapped: | |
8710 | */ | |
8711 | if (filter->inode) | |
375637bc AS |
8712 | event->addr_filters_offs[count] = |
8713 | perf_addr_filter_apply(filter, mm); | |
8714 | ||
8715 | count++; | |
8716 | } | |
8717 | ||
8718 | event->addr_filters_gen++; | |
8719 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8720 | ||
8721 | up_read(&mm->mmap_sem); | |
8722 | ||
8723 | mmput(mm); | |
8724 | ||
8725 | restart: | |
767ae086 | 8726 | perf_event_stop(event, 1); |
375637bc AS |
8727 | } |
8728 | ||
8729 | /* | |
8730 | * Address range filtering: limiting the data to certain | |
8731 | * instruction address ranges. Filters are ioctl()ed to us from | |
8732 | * userspace as ascii strings. | |
8733 | * | |
8734 | * Filter string format: | |
8735 | * | |
8736 | * ACTION RANGE_SPEC | |
8737 | * where ACTION is one of the | |
8738 | * * "filter": limit the trace to this region | |
8739 | * * "start": start tracing from this address | |
8740 | * * "stop": stop tracing at this address/region; | |
8741 | * RANGE_SPEC is | |
8742 | * * for kernel addresses: <start address>[/<size>] | |
8743 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
8744 | * | |
8745 | * if <size> is not specified, the range is treated as a single address. | |
8746 | */ | |
8747 | enum { | |
e96271f3 | 8748 | IF_ACT_NONE = -1, |
375637bc AS |
8749 | IF_ACT_FILTER, |
8750 | IF_ACT_START, | |
8751 | IF_ACT_STOP, | |
8752 | IF_SRC_FILE, | |
8753 | IF_SRC_KERNEL, | |
8754 | IF_SRC_FILEADDR, | |
8755 | IF_SRC_KERNELADDR, | |
8756 | }; | |
8757 | ||
8758 | enum { | |
8759 | IF_STATE_ACTION = 0, | |
8760 | IF_STATE_SOURCE, | |
8761 | IF_STATE_END, | |
8762 | }; | |
8763 | ||
8764 | static const match_table_t if_tokens = { | |
8765 | { IF_ACT_FILTER, "filter" }, | |
8766 | { IF_ACT_START, "start" }, | |
8767 | { IF_ACT_STOP, "stop" }, | |
8768 | { IF_SRC_FILE, "%u/%u@%s" }, | |
8769 | { IF_SRC_KERNEL, "%u/%u" }, | |
8770 | { IF_SRC_FILEADDR, "%u@%s" }, | |
8771 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 8772 | { IF_ACT_NONE, NULL }, |
375637bc AS |
8773 | }; |
8774 | ||
8775 | /* | |
8776 | * Address filter string parser | |
8777 | */ | |
8778 | static int | |
8779 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
8780 | struct list_head *filters) | |
8781 | { | |
8782 | struct perf_addr_filter *filter = NULL; | |
8783 | char *start, *orig, *filename = NULL; | |
8784 | struct path path; | |
8785 | substring_t args[MAX_OPT_ARGS]; | |
8786 | int state = IF_STATE_ACTION, token; | |
8787 | unsigned int kernel = 0; | |
8788 | int ret = -EINVAL; | |
8789 | ||
8790 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
8791 | if (!fstr) | |
8792 | return -ENOMEM; | |
8793 | ||
8794 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
8795 | ret = -EINVAL; | |
8796 | ||
8797 | if (!*start) | |
8798 | continue; | |
8799 | ||
8800 | /* filter definition begins */ | |
8801 | if (state == IF_STATE_ACTION) { | |
8802 | filter = perf_addr_filter_new(event, filters); | |
8803 | if (!filter) | |
8804 | goto fail; | |
8805 | } | |
8806 | ||
8807 | token = match_token(start, if_tokens, args); | |
8808 | switch (token) { | |
8809 | case IF_ACT_FILTER: | |
8810 | case IF_ACT_START: | |
8811 | filter->filter = 1; | |
8812 | ||
8813 | case IF_ACT_STOP: | |
8814 | if (state != IF_STATE_ACTION) | |
8815 | goto fail; | |
8816 | ||
8817 | state = IF_STATE_SOURCE; | |
8818 | break; | |
8819 | ||
8820 | case IF_SRC_KERNELADDR: | |
8821 | case IF_SRC_KERNEL: | |
8822 | kernel = 1; | |
8823 | ||
8824 | case IF_SRC_FILEADDR: | |
8825 | case IF_SRC_FILE: | |
8826 | if (state != IF_STATE_SOURCE) | |
8827 | goto fail; | |
8828 | ||
8829 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) | |
8830 | filter->range = 1; | |
8831 | ||
8832 | *args[0].to = 0; | |
8833 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
8834 | if (ret) | |
8835 | goto fail; | |
8836 | ||
8837 | if (filter->range) { | |
8838 | *args[1].to = 0; | |
8839 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
8840 | if (ret) | |
8841 | goto fail; | |
8842 | } | |
8843 | ||
4059ffd0 MP |
8844 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
8845 | int fpos = filter->range ? 2 : 1; | |
8846 | ||
8847 | filename = match_strdup(&args[fpos]); | |
375637bc AS |
8848 | if (!filename) { |
8849 | ret = -ENOMEM; | |
8850 | goto fail; | |
8851 | } | |
8852 | } | |
8853 | ||
8854 | state = IF_STATE_END; | |
8855 | break; | |
8856 | ||
8857 | default: | |
8858 | goto fail; | |
8859 | } | |
8860 | ||
8861 | /* | |
8862 | * Filter definition is fully parsed, validate and install it. | |
8863 | * Make sure that it doesn't contradict itself or the event's | |
8864 | * attribute. | |
8865 | */ | |
8866 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 8867 | ret = -EINVAL; |
375637bc AS |
8868 | if (kernel && event->attr.exclude_kernel) |
8869 | goto fail; | |
8870 | ||
8871 | if (!kernel) { | |
8872 | if (!filename) | |
8873 | goto fail; | |
8874 | ||
6ce77bfd AS |
8875 | /* |
8876 | * For now, we only support file-based filters | |
8877 | * in per-task events; doing so for CPU-wide | |
8878 | * events requires additional context switching | |
8879 | * trickery, since same object code will be | |
8880 | * mapped at different virtual addresses in | |
8881 | * different processes. | |
8882 | */ | |
8883 | ret = -EOPNOTSUPP; | |
8884 | if (!event->ctx->task) | |
8885 | goto fail_free_name; | |
8886 | ||
375637bc AS |
8887 | /* look up the path and grab its inode */ |
8888 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
8889 | if (ret) | |
8890 | goto fail_free_name; | |
8891 | ||
8892 | filter->inode = igrab(d_inode(path.dentry)); | |
8893 | path_put(&path); | |
8894 | kfree(filename); | |
8895 | filename = NULL; | |
8896 | ||
8897 | ret = -EINVAL; | |
8898 | if (!filter->inode || | |
8899 | !S_ISREG(filter->inode->i_mode)) | |
8900 | /* free_filters_list() will iput() */ | |
8901 | goto fail; | |
6ce77bfd AS |
8902 | |
8903 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
8904 | } |
8905 | ||
8906 | /* ready to consume more filters */ | |
8907 | state = IF_STATE_ACTION; | |
8908 | filter = NULL; | |
8909 | } | |
8910 | } | |
8911 | ||
8912 | if (state != IF_STATE_ACTION) | |
8913 | goto fail; | |
8914 | ||
8915 | kfree(orig); | |
8916 | ||
8917 | return 0; | |
8918 | ||
8919 | fail_free_name: | |
8920 | kfree(filename); | |
8921 | fail: | |
8922 | free_filters_list(filters); | |
8923 | kfree(orig); | |
8924 | ||
8925 | return ret; | |
8926 | } | |
8927 | ||
8928 | static int | |
8929 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
8930 | { | |
8931 | LIST_HEAD(filters); | |
8932 | int ret; | |
8933 | ||
8934 | /* | |
8935 | * Since this is called in perf_ioctl() path, we're already holding | |
8936 | * ctx::mutex. | |
8937 | */ | |
8938 | lockdep_assert_held(&event->ctx->mutex); | |
8939 | ||
8940 | if (WARN_ON_ONCE(event->parent)) | |
8941 | return -EINVAL; | |
8942 | ||
375637bc AS |
8943 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
8944 | if (ret) | |
6ce77bfd | 8945 | goto fail_clear_files; |
375637bc AS |
8946 | |
8947 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
8948 | if (ret) |
8949 | goto fail_free_filters; | |
375637bc AS |
8950 | |
8951 | /* remove existing filters, if any */ | |
8952 | perf_addr_filters_splice(event, &filters); | |
8953 | ||
8954 | /* install new filters */ | |
8955 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
8956 | ||
6ce77bfd AS |
8957 | return ret; |
8958 | ||
8959 | fail_free_filters: | |
8960 | free_filters_list(&filters); | |
8961 | ||
8962 | fail_clear_files: | |
8963 | event->addr_filters.nr_file_filters = 0; | |
8964 | ||
375637bc AS |
8965 | return ret; |
8966 | } | |
8967 | ||
c796bbbe AS |
8968 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
8969 | { | |
c796bbbe | 8970 | int ret = -EINVAL; |
e12f03d7 | 8971 | char *filter_str; |
c796bbbe AS |
8972 | |
8973 | filter_str = strndup_user(arg, PAGE_SIZE); | |
8974 | if (IS_ERR(filter_str)) | |
8975 | return PTR_ERR(filter_str); | |
8976 | ||
e12f03d7 SL |
8977 | #ifdef CONFIG_EVENT_TRACING |
8978 | if (perf_event_is_tracing(event)) { | |
8979 | struct perf_event_context *ctx = event->ctx; | |
8980 | ||
8981 | /* | |
8982 | * Beware, here be dragons!! | |
8983 | * | |
8984 | * the tracepoint muck will deadlock against ctx->mutex, but | |
8985 | * the tracepoint stuff does not actually need it. So | |
8986 | * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we | |
8987 | * already have a reference on ctx. | |
8988 | * | |
8989 | * This can result in event getting moved to a different ctx, | |
8990 | * but that does not affect the tracepoint state. | |
8991 | */ | |
8992 | mutex_unlock(&ctx->mutex); | |
8993 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
8994 | mutex_lock(&ctx->mutex); | |
8995 | } else | |
8996 | #endif | |
8997 | if (has_addr_filter(event)) | |
375637bc | 8998 | ret = perf_event_set_addr_filter(event, filter_str); |
c796bbbe AS |
8999 | |
9000 | kfree(filter_str); | |
9001 | return ret; | |
9002 | } | |
9003 | ||
b0a873eb PZ |
9004 | /* |
9005 | * hrtimer based swevent callback | |
9006 | */ | |
f29ac756 | 9007 | |
b0a873eb | 9008 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 9009 | { |
b0a873eb PZ |
9010 | enum hrtimer_restart ret = HRTIMER_RESTART; |
9011 | struct perf_sample_data data; | |
9012 | struct pt_regs *regs; | |
9013 | struct perf_event *event; | |
9014 | u64 period; | |
f29ac756 | 9015 | |
b0a873eb | 9016 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
9017 | |
9018 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
9019 | return HRTIMER_NORESTART; | |
9020 | ||
b0a873eb | 9021 | event->pmu->read(event); |
f344011c | 9022 | |
fd0d000b | 9023 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
9024 | regs = get_irq_regs(); |
9025 | ||
9026 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 9027 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 9028 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
9029 | ret = HRTIMER_NORESTART; |
9030 | } | |
24f1e32c | 9031 | |
b0a873eb PZ |
9032 | period = max_t(u64, 10000, event->hw.sample_period); |
9033 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 9034 | |
b0a873eb | 9035 | return ret; |
f29ac756 PZ |
9036 | } |
9037 | ||
b0a873eb | 9038 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 9039 | { |
b0a873eb | 9040 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
9041 | s64 period; |
9042 | ||
9043 | if (!is_sampling_event(event)) | |
9044 | return; | |
f5ffe02e | 9045 | |
5d508e82 FBH |
9046 | period = local64_read(&hwc->period_left); |
9047 | if (period) { | |
9048 | if (period < 0) | |
9049 | period = 10000; | |
fa407f35 | 9050 | |
5d508e82 FBH |
9051 | local64_set(&hwc->period_left, 0); |
9052 | } else { | |
9053 | period = max_t(u64, 10000, hwc->sample_period); | |
9054 | } | |
3497d206 TG |
9055 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
9056 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 9057 | } |
b0a873eb PZ |
9058 | |
9059 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 9060 | { |
b0a873eb PZ |
9061 | struct hw_perf_event *hwc = &event->hw; |
9062 | ||
6c7e550f | 9063 | if (is_sampling_event(event)) { |
b0a873eb | 9064 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 9065 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
9066 | |
9067 | hrtimer_cancel(&hwc->hrtimer); | |
9068 | } | |
24f1e32c FW |
9069 | } |
9070 | ||
ba3dd36c PZ |
9071 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
9072 | { | |
9073 | struct hw_perf_event *hwc = &event->hw; | |
9074 | ||
9075 | if (!is_sampling_event(event)) | |
9076 | return; | |
9077 | ||
9078 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
9079 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
9080 | ||
9081 | /* | |
9082 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
9083 | * mapping and avoid the whole period adjust feedback stuff. | |
9084 | */ | |
9085 | if (event->attr.freq) { | |
9086 | long freq = event->attr.sample_freq; | |
9087 | ||
9088 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
9089 | hwc->sample_period = event->attr.sample_period; | |
9090 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 9091 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
9092 | event->attr.freq = 0; |
9093 | } | |
9094 | } | |
9095 | ||
b0a873eb PZ |
9096 | /* |
9097 | * Software event: cpu wall time clock | |
9098 | */ | |
9099 | ||
9100 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 9101 | { |
b0a873eb PZ |
9102 | s64 prev; |
9103 | u64 now; | |
9104 | ||
a4eaf7f1 | 9105 | now = local_clock(); |
b0a873eb PZ |
9106 | prev = local64_xchg(&event->hw.prev_count, now); |
9107 | local64_add(now - prev, &event->count); | |
24f1e32c | 9108 | } |
24f1e32c | 9109 | |
a4eaf7f1 | 9110 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 9111 | { |
a4eaf7f1 | 9112 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 9113 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
9114 | } |
9115 | ||
a4eaf7f1 | 9116 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 9117 | { |
b0a873eb PZ |
9118 | perf_swevent_cancel_hrtimer(event); |
9119 | cpu_clock_event_update(event); | |
9120 | } | |
f29ac756 | 9121 | |
a4eaf7f1 PZ |
9122 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
9123 | { | |
9124 | if (flags & PERF_EF_START) | |
9125 | cpu_clock_event_start(event, flags); | |
6a694a60 | 9126 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
9127 | |
9128 | return 0; | |
9129 | } | |
9130 | ||
9131 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
9132 | { | |
9133 | cpu_clock_event_stop(event, flags); | |
9134 | } | |
9135 | ||
b0a873eb PZ |
9136 | static void cpu_clock_event_read(struct perf_event *event) |
9137 | { | |
9138 | cpu_clock_event_update(event); | |
9139 | } | |
f344011c | 9140 | |
b0a873eb PZ |
9141 | static int cpu_clock_event_init(struct perf_event *event) |
9142 | { | |
9143 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
9144 | return -ENOENT; | |
9145 | ||
9146 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
9147 | return -ENOENT; | |
9148 | ||
2481c5fa SE |
9149 | /* |
9150 | * no branch sampling for software events | |
9151 | */ | |
9152 | if (has_branch_stack(event)) | |
9153 | return -EOPNOTSUPP; | |
9154 | ||
ba3dd36c PZ |
9155 | perf_swevent_init_hrtimer(event); |
9156 | ||
b0a873eb | 9157 | return 0; |
f29ac756 PZ |
9158 | } |
9159 | ||
b0a873eb | 9160 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
9161 | .task_ctx_nr = perf_sw_context, |
9162 | ||
34f43927 PZ |
9163 | .capabilities = PERF_PMU_CAP_NO_NMI, |
9164 | ||
b0a873eb | 9165 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
9166 | .add = cpu_clock_event_add, |
9167 | .del = cpu_clock_event_del, | |
9168 | .start = cpu_clock_event_start, | |
9169 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
9170 | .read = cpu_clock_event_read, |
9171 | }; | |
9172 | ||
9173 | /* | |
9174 | * Software event: task time clock | |
9175 | */ | |
9176 | ||
9177 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 9178 | { |
b0a873eb PZ |
9179 | u64 prev; |
9180 | s64 delta; | |
5c92d124 | 9181 | |
b0a873eb PZ |
9182 | prev = local64_xchg(&event->hw.prev_count, now); |
9183 | delta = now - prev; | |
9184 | local64_add(delta, &event->count); | |
9185 | } | |
5c92d124 | 9186 | |
a4eaf7f1 | 9187 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 9188 | { |
a4eaf7f1 | 9189 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 9190 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
9191 | } |
9192 | ||
a4eaf7f1 | 9193 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
9194 | { |
9195 | perf_swevent_cancel_hrtimer(event); | |
9196 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
9197 | } |
9198 | ||
9199 | static int task_clock_event_add(struct perf_event *event, int flags) | |
9200 | { | |
9201 | if (flags & PERF_EF_START) | |
9202 | task_clock_event_start(event, flags); | |
6a694a60 | 9203 | perf_event_update_userpage(event); |
b0a873eb | 9204 | |
a4eaf7f1 PZ |
9205 | return 0; |
9206 | } | |
9207 | ||
9208 | static void task_clock_event_del(struct perf_event *event, int flags) | |
9209 | { | |
9210 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
9211 | } |
9212 | ||
9213 | static void task_clock_event_read(struct perf_event *event) | |
9214 | { | |
768a06e2 PZ |
9215 | u64 now = perf_clock(); |
9216 | u64 delta = now - event->ctx->timestamp; | |
9217 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
9218 | |
9219 | task_clock_event_update(event, time); | |
9220 | } | |
9221 | ||
9222 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 9223 | { |
b0a873eb PZ |
9224 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
9225 | return -ENOENT; | |
9226 | ||
9227 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
9228 | return -ENOENT; | |
9229 | ||
2481c5fa SE |
9230 | /* |
9231 | * no branch sampling for software events | |
9232 | */ | |
9233 | if (has_branch_stack(event)) | |
9234 | return -EOPNOTSUPP; | |
9235 | ||
ba3dd36c PZ |
9236 | perf_swevent_init_hrtimer(event); |
9237 | ||
b0a873eb | 9238 | return 0; |
6fb2915d LZ |
9239 | } |
9240 | ||
b0a873eb | 9241 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
9242 | .task_ctx_nr = perf_sw_context, |
9243 | ||
34f43927 PZ |
9244 | .capabilities = PERF_PMU_CAP_NO_NMI, |
9245 | ||
b0a873eb | 9246 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
9247 | .add = task_clock_event_add, |
9248 | .del = task_clock_event_del, | |
9249 | .start = task_clock_event_start, | |
9250 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
9251 | .read = task_clock_event_read, |
9252 | }; | |
6fb2915d | 9253 | |
ad5133b7 | 9254 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 9255 | { |
e077df4f | 9256 | } |
6fb2915d | 9257 | |
fbbe0701 SB |
9258 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
9259 | { | |
9260 | } | |
9261 | ||
ad5133b7 | 9262 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 9263 | { |
ad5133b7 | 9264 | return 0; |
6fb2915d LZ |
9265 | } |
9266 | ||
18ab2cd3 | 9267 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
9268 | |
9269 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 9270 | { |
fbbe0701 SB |
9271 | __this_cpu_write(nop_txn_flags, flags); |
9272 | ||
9273 | if (flags & ~PERF_PMU_TXN_ADD) | |
9274 | return; | |
9275 | ||
ad5133b7 | 9276 | perf_pmu_disable(pmu); |
6fb2915d LZ |
9277 | } |
9278 | ||
ad5133b7 PZ |
9279 | static int perf_pmu_commit_txn(struct pmu *pmu) |
9280 | { | |
fbbe0701 SB |
9281 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
9282 | ||
9283 | __this_cpu_write(nop_txn_flags, 0); | |
9284 | ||
9285 | if (flags & ~PERF_PMU_TXN_ADD) | |
9286 | return 0; | |
9287 | ||
ad5133b7 PZ |
9288 | perf_pmu_enable(pmu); |
9289 | return 0; | |
9290 | } | |
e077df4f | 9291 | |
ad5133b7 | 9292 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 9293 | { |
fbbe0701 SB |
9294 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
9295 | ||
9296 | __this_cpu_write(nop_txn_flags, 0); | |
9297 | ||
9298 | if (flags & ~PERF_PMU_TXN_ADD) | |
9299 | return; | |
9300 | ||
ad5133b7 | 9301 | perf_pmu_enable(pmu); |
24f1e32c FW |
9302 | } |
9303 | ||
35edc2a5 PZ |
9304 | static int perf_event_idx_default(struct perf_event *event) |
9305 | { | |
c719f560 | 9306 | return 0; |
35edc2a5 PZ |
9307 | } |
9308 | ||
8dc85d54 PZ |
9309 | /* |
9310 | * Ensures all contexts with the same task_ctx_nr have the same | |
9311 | * pmu_cpu_context too. | |
9312 | */ | |
9e317041 | 9313 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 9314 | { |
8dc85d54 | 9315 | struct pmu *pmu; |
b326e956 | 9316 | |
8dc85d54 PZ |
9317 | if (ctxn < 0) |
9318 | return NULL; | |
24f1e32c | 9319 | |
8dc85d54 PZ |
9320 | list_for_each_entry(pmu, &pmus, entry) { |
9321 | if (pmu->task_ctx_nr == ctxn) | |
9322 | return pmu->pmu_cpu_context; | |
9323 | } | |
24f1e32c | 9324 | |
8dc85d54 | 9325 | return NULL; |
24f1e32c FW |
9326 | } |
9327 | ||
51676957 PZ |
9328 | static void free_pmu_context(struct pmu *pmu) |
9329 | { | |
df0062b2 WD |
9330 | /* |
9331 | * Static contexts such as perf_sw_context have a global lifetime | |
9332 | * and may be shared between different PMUs. Avoid freeing them | |
9333 | * when a single PMU is going away. | |
9334 | */ | |
9335 | if (pmu->task_ctx_nr > perf_invalid_context) | |
9336 | return; | |
9337 | ||
8dc85d54 | 9338 | mutex_lock(&pmus_lock); |
51676957 | 9339 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 | 9340 | mutex_unlock(&pmus_lock); |
24f1e32c | 9341 | } |
6e855cd4 AS |
9342 | |
9343 | /* | |
9344 | * Let userspace know that this PMU supports address range filtering: | |
9345 | */ | |
9346 | static ssize_t nr_addr_filters_show(struct device *dev, | |
9347 | struct device_attribute *attr, | |
9348 | char *page) | |
9349 | { | |
9350 | struct pmu *pmu = dev_get_drvdata(dev); | |
9351 | ||
9352 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
9353 | } | |
9354 | DEVICE_ATTR_RO(nr_addr_filters); | |
9355 | ||
2e80a82a | 9356 | static struct idr pmu_idr; |
d6d020e9 | 9357 | |
abe43400 PZ |
9358 | static ssize_t |
9359 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
9360 | { | |
9361 | struct pmu *pmu = dev_get_drvdata(dev); | |
9362 | ||
9363 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
9364 | } | |
90826ca7 | 9365 | static DEVICE_ATTR_RO(type); |
abe43400 | 9366 | |
62b85639 SE |
9367 | static ssize_t |
9368 | perf_event_mux_interval_ms_show(struct device *dev, | |
9369 | struct device_attribute *attr, | |
9370 | char *page) | |
9371 | { | |
9372 | struct pmu *pmu = dev_get_drvdata(dev); | |
9373 | ||
9374 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
9375 | } | |
9376 | ||
272325c4 PZ |
9377 | static DEFINE_MUTEX(mux_interval_mutex); |
9378 | ||
62b85639 SE |
9379 | static ssize_t |
9380 | perf_event_mux_interval_ms_store(struct device *dev, | |
9381 | struct device_attribute *attr, | |
9382 | const char *buf, size_t count) | |
9383 | { | |
9384 | struct pmu *pmu = dev_get_drvdata(dev); | |
9385 | int timer, cpu, ret; | |
9386 | ||
9387 | ret = kstrtoint(buf, 0, &timer); | |
9388 | if (ret) | |
9389 | return ret; | |
9390 | ||
9391 | if (timer < 1) | |
9392 | return -EINVAL; | |
9393 | ||
9394 | /* same value, noting to do */ | |
9395 | if (timer == pmu->hrtimer_interval_ms) | |
9396 | return count; | |
9397 | ||
272325c4 | 9398 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
9399 | pmu->hrtimer_interval_ms = timer; |
9400 | ||
9401 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 9402 | cpus_read_lock(); |
272325c4 | 9403 | for_each_online_cpu(cpu) { |
62b85639 SE |
9404 | struct perf_cpu_context *cpuctx; |
9405 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
9406 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
9407 | ||
272325c4 PZ |
9408 | cpu_function_call(cpu, |
9409 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 9410 | } |
a63fbed7 | 9411 | cpus_read_unlock(); |
272325c4 | 9412 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
9413 | |
9414 | return count; | |
9415 | } | |
90826ca7 | 9416 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 9417 | |
90826ca7 GKH |
9418 | static struct attribute *pmu_dev_attrs[] = { |
9419 | &dev_attr_type.attr, | |
9420 | &dev_attr_perf_event_mux_interval_ms.attr, | |
9421 | NULL, | |
abe43400 | 9422 | }; |
90826ca7 | 9423 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
9424 | |
9425 | static int pmu_bus_running; | |
9426 | static struct bus_type pmu_bus = { | |
9427 | .name = "event_source", | |
90826ca7 | 9428 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
9429 | }; |
9430 | ||
9431 | static void pmu_dev_release(struct device *dev) | |
9432 | { | |
9433 | kfree(dev); | |
9434 | } | |
9435 | ||
9436 | static int pmu_dev_alloc(struct pmu *pmu) | |
9437 | { | |
9438 | int ret = -ENOMEM; | |
9439 | ||
9440 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
9441 | if (!pmu->dev) | |
9442 | goto out; | |
9443 | ||
0c9d42ed | 9444 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
9445 | device_initialize(pmu->dev); |
9446 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
9447 | if (ret) | |
9448 | goto free_dev; | |
9449 | ||
9450 | dev_set_drvdata(pmu->dev, pmu); | |
9451 | pmu->dev->bus = &pmu_bus; | |
9452 | pmu->dev->release = pmu_dev_release; | |
9453 | ret = device_add(pmu->dev); | |
9454 | if (ret) | |
9455 | goto free_dev; | |
9456 | ||
6e855cd4 AS |
9457 | /* For PMUs with address filters, throw in an extra attribute: */ |
9458 | if (pmu->nr_addr_filters) | |
9459 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9460 | ||
9461 | if (ret) | |
9462 | goto del_dev; | |
9463 | ||
abe43400 PZ |
9464 | out: |
9465 | return ret; | |
9466 | ||
6e855cd4 AS |
9467 | del_dev: |
9468 | device_del(pmu->dev); | |
9469 | ||
abe43400 PZ |
9470 | free_dev: |
9471 | put_device(pmu->dev); | |
9472 | goto out; | |
9473 | } | |
9474 | ||
547e9fd7 | 9475 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 9476 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 9477 | |
03d8e80b | 9478 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 9479 | { |
108b02cf | 9480 | int cpu, ret; |
24f1e32c | 9481 | |
b0a873eb | 9482 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
9483 | ret = -ENOMEM; |
9484 | pmu->pmu_disable_count = alloc_percpu(int); | |
9485 | if (!pmu->pmu_disable_count) | |
9486 | goto unlock; | |
f29ac756 | 9487 | |
2e80a82a PZ |
9488 | pmu->type = -1; |
9489 | if (!name) | |
9490 | goto skip_type; | |
9491 | pmu->name = name; | |
9492 | ||
9493 | if (type < 0) { | |
0e9c3be2 TH |
9494 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
9495 | if (type < 0) { | |
9496 | ret = type; | |
2e80a82a PZ |
9497 | goto free_pdc; |
9498 | } | |
9499 | } | |
9500 | pmu->type = type; | |
9501 | ||
abe43400 PZ |
9502 | if (pmu_bus_running) { |
9503 | ret = pmu_dev_alloc(pmu); | |
9504 | if (ret) | |
9505 | goto free_idr; | |
9506 | } | |
9507 | ||
2e80a82a | 9508 | skip_type: |
26657848 PZ |
9509 | if (pmu->task_ctx_nr == perf_hw_context) { |
9510 | static int hw_context_taken = 0; | |
9511 | ||
5101ef20 MR |
9512 | /* |
9513 | * Other than systems with heterogeneous CPUs, it never makes | |
9514 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
9515 | * uncore must use perf_invalid_context. | |
9516 | */ | |
9517 | if (WARN_ON_ONCE(hw_context_taken && | |
9518 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
9519 | pmu->task_ctx_nr = perf_invalid_context; |
9520 | ||
9521 | hw_context_taken = 1; | |
9522 | } | |
9523 | ||
8dc85d54 PZ |
9524 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
9525 | if (pmu->pmu_cpu_context) | |
9526 | goto got_cpu_context; | |
f29ac756 | 9527 | |
c4814202 | 9528 | ret = -ENOMEM; |
108b02cf PZ |
9529 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
9530 | if (!pmu->pmu_cpu_context) | |
abe43400 | 9531 | goto free_dev; |
f344011c | 9532 | |
108b02cf PZ |
9533 | for_each_possible_cpu(cpu) { |
9534 | struct perf_cpu_context *cpuctx; | |
9535 | ||
9536 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 9537 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 9538 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 9539 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 9540 | cpuctx->ctx.pmu = pmu; |
a63fbed7 | 9541 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); |
9e630205 | 9542 | |
272325c4 | 9543 | __perf_mux_hrtimer_init(cpuctx, cpu); |
108b02cf | 9544 | } |
76e1d904 | 9545 | |
8dc85d54 | 9546 | got_cpu_context: |
ad5133b7 PZ |
9547 | if (!pmu->start_txn) { |
9548 | if (pmu->pmu_enable) { | |
9549 | /* | |
9550 | * If we have pmu_enable/pmu_disable calls, install | |
9551 | * transaction stubs that use that to try and batch | |
9552 | * hardware accesses. | |
9553 | */ | |
9554 | pmu->start_txn = perf_pmu_start_txn; | |
9555 | pmu->commit_txn = perf_pmu_commit_txn; | |
9556 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
9557 | } else { | |
fbbe0701 | 9558 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
9559 | pmu->commit_txn = perf_pmu_nop_int; |
9560 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 9561 | } |
5c92d124 | 9562 | } |
15dbf27c | 9563 | |
ad5133b7 PZ |
9564 | if (!pmu->pmu_enable) { |
9565 | pmu->pmu_enable = perf_pmu_nop_void; | |
9566 | pmu->pmu_disable = perf_pmu_nop_void; | |
9567 | } | |
9568 | ||
35edc2a5 PZ |
9569 | if (!pmu->event_idx) |
9570 | pmu->event_idx = perf_event_idx_default; | |
9571 | ||
b0a873eb | 9572 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 9573 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
9574 | ret = 0; |
9575 | unlock: | |
b0a873eb PZ |
9576 | mutex_unlock(&pmus_lock); |
9577 | ||
33696fc0 | 9578 | return ret; |
108b02cf | 9579 | |
abe43400 PZ |
9580 | free_dev: |
9581 | device_del(pmu->dev); | |
9582 | put_device(pmu->dev); | |
9583 | ||
2e80a82a PZ |
9584 | free_idr: |
9585 | if (pmu->type >= PERF_TYPE_MAX) | |
9586 | idr_remove(&pmu_idr, pmu->type); | |
9587 | ||
108b02cf PZ |
9588 | free_pdc: |
9589 | free_percpu(pmu->pmu_disable_count); | |
9590 | goto unlock; | |
f29ac756 | 9591 | } |
c464c76e | 9592 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 9593 | |
b0a873eb | 9594 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 9595 | { |
0933840a JO |
9596 | int remove_device; |
9597 | ||
b0a873eb | 9598 | mutex_lock(&pmus_lock); |
0933840a | 9599 | remove_device = pmu_bus_running; |
b0a873eb PZ |
9600 | list_del_rcu(&pmu->entry); |
9601 | mutex_unlock(&pmus_lock); | |
5c92d124 | 9602 | |
0475f9ea | 9603 | /* |
cde8e884 PZ |
9604 | * We dereference the pmu list under both SRCU and regular RCU, so |
9605 | * synchronize against both of those. | |
0475f9ea | 9606 | */ |
b0a873eb | 9607 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 9608 | synchronize_rcu(); |
d6d020e9 | 9609 | |
33696fc0 | 9610 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
9611 | if (pmu->type >= PERF_TYPE_MAX) |
9612 | idr_remove(&pmu_idr, pmu->type); | |
0933840a JO |
9613 | if (remove_device) { |
9614 | if (pmu->nr_addr_filters) | |
9615 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
9616 | device_del(pmu->dev); | |
9617 | put_device(pmu->dev); | |
9618 | } | |
51676957 | 9619 | free_pmu_context(pmu); |
b0a873eb | 9620 | } |
c464c76e | 9621 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 9622 | |
cc34b98b MR |
9623 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
9624 | { | |
ccd41c86 | 9625 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
9626 | int ret; |
9627 | ||
9628 | if (!try_module_get(pmu->module)) | |
9629 | return -ENODEV; | |
ccd41c86 | 9630 | |
0c7296ca PZ |
9631 | /* |
9632 | * A number of pmu->event_init() methods iterate the sibling_list to, | |
9633 | * for example, validate if the group fits on the PMU. Therefore, | |
9634 | * if this is a sibling event, acquire the ctx->mutex to protect | |
9635 | * the sibling_list. | |
9636 | */ | |
9637 | if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { | |
8b10c5e2 PZ |
9638 | /* |
9639 | * This ctx->mutex can nest when we're called through | |
9640 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
9641 | */ | |
9642 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
9643 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
9644 | BUG_ON(!ctx); |
9645 | } | |
9646 | ||
cc34b98b MR |
9647 | event->pmu = pmu; |
9648 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
9649 | |
9650 | if (ctx) | |
9651 | perf_event_ctx_unlock(event->group_leader, ctx); | |
9652 | ||
cc34b98b MR |
9653 | if (ret) |
9654 | module_put(pmu->module); | |
9655 | ||
9656 | return ret; | |
9657 | } | |
9658 | ||
18ab2cd3 | 9659 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 9660 | { |
85c617ab | 9661 | struct pmu *pmu; |
b0a873eb | 9662 | int idx; |
940c5b29 | 9663 | int ret; |
b0a873eb PZ |
9664 | |
9665 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 9666 | |
40999312 KL |
9667 | /* Try parent's PMU first: */ |
9668 | if (event->parent && event->parent->pmu) { | |
9669 | pmu = event->parent->pmu; | |
9670 | ret = perf_try_init_event(pmu, event); | |
9671 | if (!ret) | |
9672 | goto unlock; | |
9673 | } | |
9674 | ||
2e80a82a PZ |
9675 | rcu_read_lock(); |
9676 | pmu = idr_find(&pmu_idr, event->attr.type); | |
9677 | rcu_read_unlock(); | |
940c5b29 | 9678 | if (pmu) { |
cc34b98b | 9679 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
9680 | if (ret) |
9681 | pmu = ERR_PTR(ret); | |
2e80a82a | 9682 | goto unlock; |
940c5b29 | 9683 | } |
2e80a82a | 9684 | |
b0a873eb | 9685 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 9686 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 9687 | if (!ret) |
e5f4d339 | 9688 | goto unlock; |
76e1d904 | 9689 | |
b0a873eb PZ |
9690 | if (ret != -ENOENT) { |
9691 | pmu = ERR_PTR(ret); | |
e5f4d339 | 9692 | goto unlock; |
f344011c | 9693 | } |
5c92d124 | 9694 | } |
e5f4d339 PZ |
9695 | pmu = ERR_PTR(-ENOENT); |
9696 | unlock: | |
b0a873eb | 9697 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 9698 | |
4aeb0b42 | 9699 | return pmu; |
5c92d124 IM |
9700 | } |
9701 | ||
f2fb6bef KL |
9702 | static void attach_sb_event(struct perf_event *event) |
9703 | { | |
9704 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
9705 | ||
9706 | raw_spin_lock(&pel->lock); | |
9707 | list_add_rcu(&event->sb_list, &pel->list); | |
9708 | raw_spin_unlock(&pel->lock); | |
9709 | } | |
9710 | ||
aab5b71e PZ |
9711 | /* |
9712 | * We keep a list of all !task (and therefore per-cpu) events | |
9713 | * that need to receive side-band records. | |
9714 | * | |
9715 | * This avoids having to scan all the various PMU per-cpu contexts | |
9716 | * looking for them. | |
9717 | */ | |
f2fb6bef KL |
9718 | static void account_pmu_sb_event(struct perf_event *event) |
9719 | { | |
a4f144eb | 9720 | if (is_sb_event(event)) |
f2fb6bef KL |
9721 | attach_sb_event(event); |
9722 | } | |
9723 | ||
4beb31f3 FW |
9724 | static void account_event_cpu(struct perf_event *event, int cpu) |
9725 | { | |
9726 | if (event->parent) | |
9727 | return; | |
9728 | ||
4beb31f3 FW |
9729 | if (is_cgroup_event(event)) |
9730 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
9731 | } | |
9732 | ||
555e0c1e FW |
9733 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
9734 | static void account_freq_event_nohz(void) | |
9735 | { | |
9736 | #ifdef CONFIG_NO_HZ_FULL | |
9737 | /* Lock so we don't race with concurrent unaccount */ | |
9738 | spin_lock(&nr_freq_lock); | |
9739 | if (atomic_inc_return(&nr_freq_events) == 1) | |
9740 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
9741 | spin_unlock(&nr_freq_lock); | |
9742 | #endif | |
9743 | } | |
9744 | ||
9745 | static void account_freq_event(void) | |
9746 | { | |
9747 | if (tick_nohz_full_enabled()) | |
9748 | account_freq_event_nohz(); | |
9749 | else | |
9750 | atomic_inc(&nr_freq_events); | |
9751 | } | |
9752 | ||
9753 | ||
766d6c07 FW |
9754 | static void account_event(struct perf_event *event) |
9755 | { | |
25432ae9 PZ |
9756 | bool inc = false; |
9757 | ||
4beb31f3 FW |
9758 | if (event->parent) |
9759 | return; | |
9760 | ||
766d6c07 | 9761 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 9762 | inc = true; |
766d6c07 FW |
9763 | if (event->attr.mmap || event->attr.mmap_data) |
9764 | atomic_inc(&nr_mmap_events); | |
9765 | if (event->attr.comm) | |
9766 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
9767 | if (event->attr.namespaces) |
9768 | atomic_inc(&nr_namespaces_events); | |
766d6c07 FW |
9769 | if (event->attr.task) |
9770 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
9771 | if (event->attr.freq) |
9772 | account_freq_event(); | |
45ac1403 AH |
9773 | if (event->attr.context_switch) { |
9774 | atomic_inc(&nr_switch_events); | |
25432ae9 | 9775 | inc = true; |
45ac1403 | 9776 | } |
4beb31f3 | 9777 | if (has_branch_stack(event)) |
25432ae9 | 9778 | inc = true; |
4beb31f3 | 9779 | if (is_cgroup_event(event)) |
25432ae9 PZ |
9780 | inc = true; |
9781 | ||
9107c89e | 9782 | if (inc) { |
5bce9db1 AS |
9783 | /* |
9784 | * We need the mutex here because static_branch_enable() | |
9785 | * must complete *before* the perf_sched_count increment | |
9786 | * becomes visible. | |
9787 | */ | |
9107c89e PZ |
9788 | if (atomic_inc_not_zero(&perf_sched_count)) |
9789 | goto enabled; | |
9790 | ||
9791 | mutex_lock(&perf_sched_mutex); | |
9792 | if (!atomic_read(&perf_sched_count)) { | |
9793 | static_branch_enable(&perf_sched_events); | |
9794 | /* | |
9795 | * Guarantee that all CPUs observe they key change and | |
9796 | * call the perf scheduling hooks before proceeding to | |
9797 | * install events that need them. | |
9798 | */ | |
9799 | synchronize_sched(); | |
9800 | } | |
9801 | /* | |
9802 | * Now that we have waited for the sync_sched(), allow further | |
9803 | * increments to by-pass the mutex. | |
9804 | */ | |
9805 | atomic_inc(&perf_sched_count); | |
9806 | mutex_unlock(&perf_sched_mutex); | |
9807 | } | |
9808 | enabled: | |
4beb31f3 FW |
9809 | |
9810 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
9811 | |
9812 | account_pmu_sb_event(event); | |
766d6c07 FW |
9813 | } |
9814 | ||
0793a61d | 9815 | /* |
cdd6c482 | 9816 | * Allocate and initialize a event structure |
0793a61d | 9817 | */ |
cdd6c482 | 9818 | static struct perf_event * |
c3f00c70 | 9819 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
9820 | struct task_struct *task, |
9821 | struct perf_event *group_leader, | |
9822 | struct perf_event *parent_event, | |
4dc0da86 | 9823 | perf_overflow_handler_t overflow_handler, |
79dff51e | 9824 | void *context, int cgroup_fd) |
0793a61d | 9825 | { |
51b0fe39 | 9826 | struct pmu *pmu; |
cdd6c482 IM |
9827 | struct perf_event *event; |
9828 | struct hw_perf_event *hwc; | |
90983b16 | 9829 | long err = -EINVAL; |
0793a61d | 9830 | |
66832eb4 ON |
9831 | if ((unsigned)cpu >= nr_cpu_ids) { |
9832 | if (!task || cpu != -1) | |
9833 | return ERR_PTR(-EINVAL); | |
9834 | } | |
9835 | ||
c3f00c70 | 9836 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 9837 | if (!event) |
d5d2bc0d | 9838 | return ERR_PTR(-ENOMEM); |
0793a61d | 9839 | |
04289bb9 | 9840 | /* |
cdd6c482 | 9841 | * Single events are their own group leaders, with an |
04289bb9 IM |
9842 | * empty sibling list: |
9843 | */ | |
9844 | if (!group_leader) | |
cdd6c482 | 9845 | group_leader = event; |
04289bb9 | 9846 | |
cdd6c482 IM |
9847 | mutex_init(&event->child_mutex); |
9848 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 9849 | |
cdd6c482 IM |
9850 | INIT_LIST_HEAD(&event->event_entry); |
9851 | INIT_LIST_HEAD(&event->sibling_list); | |
6668128a | 9852 | INIT_LIST_HEAD(&event->active_list); |
8e1a2031 | 9853 | init_event_group(event); |
10c6db11 | 9854 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 9855 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 9856 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
9857 | INIT_HLIST_NODE(&event->hlist_entry); |
9858 | ||
10c6db11 | 9859 | |
cdd6c482 | 9860 | init_waitqueue_head(&event->waitq); |
e360adbe | 9861 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 9862 | |
cdd6c482 | 9863 | mutex_init(&event->mmap_mutex); |
375637bc | 9864 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 9865 | |
a6fa941d | 9866 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
9867 | event->cpu = cpu; |
9868 | event->attr = *attr; | |
9869 | event->group_leader = group_leader; | |
9870 | event->pmu = NULL; | |
cdd6c482 | 9871 | event->oncpu = -1; |
a96bbc16 | 9872 | |
cdd6c482 | 9873 | event->parent = parent_event; |
b84fbc9f | 9874 | |
17cf22c3 | 9875 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 9876 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 9877 | |
cdd6c482 | 9878 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 9879 | |
d580ff86 PZ |
9880 | if (task) { |
9881 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 9882 | /* |
50f16a8b PZ |
9883 | * XXX pmu::event_init needs to know what task to account to |
9884 | * and we cannot use the ctx information because we need the | |
9885 | * pmu before we get a ctx. | |
d580ff86 | 9886 | */ |
50f16a8b | 9887 | event->hw.target = task; |
d580ff86 PZ |
9888 | } |
9889 | ||
34f43927 PZ |
9890 | event->clock = &local_clock; |
9891 | if (parent_event) | |
9892 | event->clock = parent_event->clock; | |
9893 | ||
4dc0da86 | 9894 | if (!overflow_handler && parent_event) { |
b326e956 | 9895 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 9896 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 9897 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c AS |
9898 | if (overflow_handler == bpf_overflow_handler) { |
9899 | struct bpf_prog *prog = bpf_prog_inc(parent_event->prog); | |
9900 | ||
9901 | if (IS_ERR(prog)) { | |
9902 | err = PTR_ERR(prog); | |
9903 | goto err_ns; | |
9904 | } | |
9905 | event->prog = prog; | |
9906 | event->orig_overflow_handler = | |
9907 | parent_event->orig_overflow_handler; | |
9908 | } | |
9909 | #endif | |
4dc0da86 | 9910 | } |
66832eb4 | 9911 | |
1879445d WN |
9912 | if (overflow_handler) { |
9913 | event->overflow_handler = overflow_handler; | |
9914 | event->overflow_handler_context = context; | |
9ecda41a WN |
9915 | } else if (is_write_backward(event)){ |
9916 | event->overflow_handler = perf_event_output_backward; | |
9917 | event->overflow_handler_context = NULL; | |
1879445d | 9918 | } else { |
9ecda41a | 9919 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
9920 | event->overflow_handler_context = NULL; |
9921 | } | |
97eaf530 | 9922 | |
0231bb53 | 9923 | perf_event__state_init(event); |
a86ed508 | 9924 | |
4aeb0b42 | 9925 | pmu = NULL; |
b8e83514 | 9926 | |
cdd6c482 | 9927 | hwc = &event->hw; |
bd2b5b12 | 9928 | hwc->sample_period = attr->sample_period; |
0d48696f | 9929 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 9930 | hwc->sample_period = 1; |
eced1dfc | 9931 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 9932 | |
e7850595 | 9933 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 9934 | |
2023b359 | 9935 | /* |
ba5213ae PZ |
9936 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
9937 | * See perf_output_read(). | |
2023b359 | 9938 | */ |
ba5213ae | 9939 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 9940 | goto err_ns; |
a46a2300 YZ |
9941 | |
9942 | if (!has_branch_stack(event)) | |
9943 | event->attr.branch_sample_type = 0; | |
2023b359 | 9944 | |
79dff51e MF |
9945 | if (cgroup_fd != -1) { |
9946 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
9947 | if (err) | |
9948 | goto err_ns; | |
9949 | } | |
9950 | ||
b0a873eb | 9951 | pmu = perf_init_event(event); |
85c617ab | 9952 | if (IS_ERR(pmu)) { |
4aeb0b42 | 9953 | err = PTR_ERR(pmu); |
90983b16 | 9954 | goto err_ns; |
621a01ea | 9955 | } |
d5d2bc0d | 9956 | |
bed5b25a AS |
9957 | err = exclusive_event_init(event); |
9958 | if (err) | |
9959 | goto err_pmu; | |
9960 | ||
375637bc AS |
9961 | if (has_addr_filter(event)) { |
9962 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, | |
9963 | sizeof(unsigned long), | |
9964 | GFP_KERNEL); | |
36cc2b92 DC |
9965 | if (!event->addr_filters_offs) { |
9966 | err = -ENOMEM; | |
375637bc | 9967 | goto err_per_task; |
36cc2b92 | 9968 | } |
375637bc AS |
9969 | |
9970 | /* force hw sync on the address filters */ | |
9971 | event->addr_filters_gen = 1; | |
9972 | } | |
9973 | ||
cdd6c482 | 9974 | if (!event->parent) { |
927c7a9e | 9975 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 9976 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 9977 | if (err) |
375637bc | 9978 | goto err_addr_filters; |
d010b332 | 9979 | } |
f344011c | 9980 | } |
9ee318a7 | 9981 | |
927a5570 AS |
9982 | /* symmetric to unaccount_event() in _free_event() */ |
9983 | account_event(event); | |
9984 | ||
cdd6c482 | 9985 | return event; |
90983b16 | 9986 | |
375637bc AS |
9987 | err_addr_filters: |
9988 | kfree(event->addr_filters_offs); | |
9989 | ||
bed5b25a AS |
9990 | err_per_task: |
9991 | exclusive_event_destroy(event); | |
9992 | ||
90983b16 FW |
9993 | err_pmu: |
9994 | if (event->destroy) | |
9995 | event->destroy(event); | |
c464c76e | 9996 | module_put(pmu->module); |
90983b16 | 9997 | err_ns: |
79dff51e MF |
9998 | if (is_cgroup_event(event)) |
9999 | perf_detach_cgroup(event); | |
90983b16 FW |
10000 | if (event->ns) |
10001 | put_pid_ns(event->ns); | |
10002 | kfree(event); | |
10003 | ||
10004 | return ERR_PTR(err); | |
0793a61d TG |
10005 | } |
10006 | ||
cdd6c482 IM |
10007 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
10008 | struct perf_event_attr *attr) | |
974802ea | 10009 | { |
974802ea | 10010 | u32 size; |
cdf8073d | 10011 | int ret; |
974802ea PZ |
10012 | |
10013 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
10014 | return -EFAULT; | |
10015 | ||
10016 | /* | |
10017 | * zero the full structure, so that a short copy will be nice. | |
10018 | */ | |
10019 | memset(attr, 0, sizeof(*attr)); | |
10020 | ||
10021 | ret = get_user(size, &uattr->size); | |
10022 | if (ret) | |
10023 | return ret; | |
10024 | ||
10025 | if (size > PAGE_SIZE) /* silly large */ | |
10026 | goto err_size; | |
10027 | ||
10028 | if (!size) /* abi compat */ | |
10029 | size = PERF_ATTR_SIZE_VER0; | |
10030 | ||
10031 | if (size < PERF_ATTR_SIZE_VER0) | |
10032 | goto err_size; | |
10033 | ||
10034 | /* | |
10035 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
10036 | * ensure all the unknown bits are 0 - i.e. new |
10037 | * user-space does not rely on any kernel feature | |
10038 | * extensions we dont know about yet. | |
974802ea PZ |
10039 | */ |
10040 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
10041 | unsigned char __user *addr; |
10042 | unsigned char __user *end; | |
10043 | unsigned char val; | |
974802ea | 10044 | |
cdf8073d IS |
10045 | addr = (void __user *)uattr + sizeof(*attr); |
10046 | end = (void __user *)uattr + size; | |
974802ea | 10047 | |
cdf8073d | 10048 | for (; addr < end; addr++) { |
974802ea PZ |
10049 | ret = get_user(val, addr); |
10050 | if (ret) | |
10051 | return ret; | |
10052 | if (val) | |
10053 | goto err_size; | |
10054 | } | |
b3e62e35 | 10055 | size = sizeof(*attr); |
974802ea PZ |
10056 | } |
10057 | ||
10058 | ret = copy_from_user(attr, uattr, size); | |
10059 | if (ret) | |
10060 | return -EFAULT; | |
10061 | ||
f12f42ac MX |
10062 | attr->size = size; |
10063 | ||
cd757645 | 10064 | if (attr->__reserved_1) |
974802ea PZ |
10065 | return -EINVAL; |
10066 | ||
10067 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
10068 | return -EINVAL; | |
10069 | ||
10070 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
10071 | return -EINVAL; | |
10072 | ||
bce38cd5 SE |
10073 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
10074 | u64 mask = attr->branch_sample_type; | |
10075 | ||
10076 | /* only using defined bits */ | |
10077 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
10078 | return -EINVAL; | |
10079 | ||
10080 | /* at least one branch bit must be set */ | |
10081 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
10082 | return -EINVAL; | |
10083 | ||
bce38cd5 SE |
10084 | /* propagate priv level, when not set for branch */ |
10085 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
10086 | ||
10087 | /* exclude_kernel checked on syscall entry */ | |
10088 | if (!attr->exclude_kernel) | |
10089 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
10090 | ||
10091 | if (!attr->exclude_user) | |
10092 | mask |= PERF_SAMPLE_BRANCH_USER; | |
10093 | ||
10094 | if (!attr->exclude_hv) | |
10095 | mask |= PERF_SAMPLE_BRANCH_HV; | |
10096 | /* | |
10097 | * adjust user setting (for HW filter setup) | |
10098 | */ | |
10099 | attr->branch_sample_type = mask; | |
10100 | } | |
e712209a SE |
10101 | /* privileged levels capture (kernel, hv): check permissions */ |
10102 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
10103 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
10104 | return -EACCES; | |
bce38cd5 | 10105 | } |
4018994f | 10106 | |
c5ebcedb | 10107 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 10108 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
10109 | if (ret) |
10110 | return ret; | |
10111 | } | |
10112 | ||
10113 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
10114 | if (!arch_perf_have_user_stack_dump()) | |
10115 | return -ENOSYS; | |
10116 | ||
10117 | /* | |
10118 | * We have __u32 type for the size, but so far | |
10119 | * we can only use __u16 as maximum due to the | |
10120 | * __u16 sample size limit. | |
10121 | */ | |
10122 | if (attr->sample_stack_user >= USHRT_MAX) | |
10123 | ret = -EINVAL; | |
10124 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) | |
10125 | ret = -EINVAL; | |
10126 | } | |
4018994f | 10127 | |
5f970521 JO |
10128 | if (!attr->sample_max_stack) |
10129 | attr->sample_max_stack = sysctl_perf_event_max_stack; | |
10130 | ||
60e2364e SE |
10131 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
10132 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
10133 | out: |
10134 | return ret; | |
10135 | ||
10136 | err_size: | |
10137 | put_user(sizeof(*attr), &uattr->size); | |
10138 | ret = -E2BIG; | |
10139 | goto out; | |
10140 | } | |
10141 | ||
ac9721f3 PZ |
10142 | static int |
10143 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 10144 | { |
b69cf536 | 10145 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
10146 | int ret = -EINVAL; |
10147 | ||
ac9721f3 | 10148 | if (!output_event) |
a4be7c27 PZ |
10149 | goto set; |
10150 | ||
ac9721f3 PZ |
10151 | /* don't allow circular references */ |
10152 | if (event == output_event) | |
a4be7c27 PZ |
10153 | goto out; |
10154 | ||
0f139300 PZ |
10155 | /* |
10156 | * Don't allow cross-cpu buffers | |
10157 | */ | |
10158 | if (output_event->cpu != event->cpu) | |
10159 | goto out; | |
10160 | ||
10161 | /* | |
76369139 | 10162 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
10163 | */ |
10164 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
10165 | goto out; | |
10166 | ||
34f43927 PZ |
10167 | /* |
10168 | * Mixing clocks in the same buffer is trouble you don't need. | |
10169 | */ | |
10170 | if (output_event->clock != event->clock) | |
10171 | goto out; | |
10172 | ||
9ecda41a WN |
10173 | /* |
10174 | * Either writing ring buffer from beginning or from end. | |
10175 | * Mixing is not allowed. | |
10176 | */ | |
10177 | if (is_write_backward(output_event) != is_write_backward(event)) | |
10178 | goto out; | |
10179 | ||
45bfb2e5 PZ |
10180 | /* |
10181 | * If both events generate aux data, they must be on the same PMU | |
10182 | */ | |
10183 | if (has_aux(event) && has_aux(output_event) && | |
10184 | event->pmu != output_event->pmu) | |
10185 | goto out; | |
10186 | ||
a4be7c27 | 10187 | set: |
cdd6c482 | 10188 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
10189 | /* Can't redirect output if we've got an active mmap() */ |
10190 | if (atomic_read(&event->mmap_count)) | |
10191 | goto unlock; | |
a4be7c27 | 10192 | |
ac9721f3 | 10193 | if (output_event) { |
76369139 FW |
10194 | /* get the rb we want to redirect to */ |
10195 | rb = ring_buffer_get(output_event); | |
10196 | if (!rb) | |
ac9721f3 | 10197 | goto unlock; |
a4be7c27 PZ |
10198 | } |
10199 | ||
b69cf536 | 10200 | ring_buffer_attach(event, rb); |
9bb5d40c | 10201 | |
a4be7c27 | 10202 | ret = 0; |
ac9721f3 PZ |
10203 | unlock: |
10204 | mutex_unlock(&event->mmap_mutex); | |
10205 | ||
a4be7c27 | 10206 | out: |
a4be7c27 PZ |
10207 | return ret; |
10208 | } | |
10209 | ||
f63a8daa PZ |
10210 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
10211 | { | |
10212 | if (b < a) | |
10213 | swap(a, b); | |
10214 | ||
10215 | mutex_lock(a); | |
10216 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
10217 | } | |
10218 | ||
34f43927 PZ |
10219 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
10220 | { | |
10221 | bool nmi_safe = false; | |
10222 | ||
10223 | switch (clk_id) { | |
10224 | case CLOCK_MONOTONIC: | |
10225 | event->clock = &ktime_get_mono_fast_ns; | |
10226 | nmi_safe = true; | |
10227 | break; | |
10228 | ||
10229 | case CLOCK_MONOTONIC_RAW: | |
10230 | event->clock = &ktime_get_raw_fast_ns; | |
10231 | nmi_safe = true; | |
10232 | break; | |
10233 | ||
10234 | case CLOCK_REALTIME: | |
10235 | event->clock = &ktime_get_real_ns; | |
10236 | break; | |
10237 | ||
10238 | case CLOCK_BOOTTIME: | |
10239 | event->clock = &ktime_get_boot_ns; | |
10240 | break; | |
10241 | ||
10242 | case CLOCK_TAI: | |
10243 | event->clock = &ktime_get_tai_ns; | |
10244 | break; | |
10245 | ||
10246 | default: | |
10247 | return -EINVAL; | |
10248 | } | |
10249 | ||
10250 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
10251 | return -EINVAL; | |
10252 | ||
10253 | return 0; | |
10254 | } | |
10255 | ||
321027c1 PZ |
10256 | /* |
10257 | * Variation on perf_event_ctx_lock_nested(), except we take two context | |
10258 | * mutexes. | |
10259 | */ | |
10260 | static struct perf_event_context * | |
10261 | __perf_event_ctx_lock_double(struct perf_event *group_leader, | |
10262 | struct perf_event_context *ctx) | |
10263 | { | |
10264 | struct perf_event_context *gctx; | |
10265 | ||
10266 | again: | |
10267 | rcu_read_lock(); | |
10268 | gctx = READ_ONCE(group_leader->ctx); | |
10269 | if (!atomic_inc_not_zero(&gctx->refcount)) { | |
10270 | rcu_read_unlock(); | |
10271 | goto again; | |
10272 | } | |
10273 | rcu_read_unlock(); | |
10274 | ||
10275 | mutex_lock_double(&gctx->mutex, &ctx->mutex); | |
10276 | ||
10277 | if (group_leader->ctx != gctx) { | |
10278 | mutex_unlock(&ctx->mutex); | |
10279 | mutex_unlock(&gctx->mutex); | |
10280 | put_ctx(gctx); | |
10281 | goto again; | |
10282 | } | |
10283 | ||
10284 | return gctx; | |
10285 | } | |
10286 | ||
0793a61d | 10287 | /** |
cdd6c482 | 10288 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 10289 | * |
cdd6c482 | 10290 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 10291 | * @pid: target pid |
9f66a381 | 10292 | * @cpu: target cpu |
cdd6c482 | 10293 | * @group_fd: group leader event fd |
0793a61d | 10294 | */ |
cdd6c482 IM |
10295 | SYSCALL_DEFINE5(perf_event_open, |
10296 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 10297 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 10298 | { |
b04243ef PZ |
10299 | struct perf_event *group_leader = NULL, *output_event = NULL; |
10300 | struct perf_event *event, *sibling; | |
cdd6c482 | 10301 | struct perf_event_attr attr; |
f63a8daa | 10302 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 10303 | struct file *event_file = NULL; |
2903ff01 | 10304 | struct fd group = {NULL, 0}; |
38a81da2 | 10305 | struct task_struct *task = NULL; |
89a1e187 | 10306 | struct pmu *pmu; |
ea635c64 | 10307 | int event_fd; |
b04243ef | 10308 | int move_group = 0; |
dc86cabe | 10309 | int err; |
a21b0b35 | 10310 | int f_flags = O_RDWR; |
79dff51e | 10311 | int cgroup_fd = -1; |
0793a61d | 10312 | |
2743a5b0 | 10313 | /* for future expandability... */ |
e5d1367f | 10314 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
10315 | return -EINVAL; |
10316 | ||
dc86cabe IM |
10317 | err = perf_copy_attr(attr_uptr, &attr); |
10318 | if (err) | |
10319 | return err; | |
eab656ae | 10320 | |
0764771d PZ |
10321 | if (!attr.exclude_kernel) { |
10322 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
10323 | return -EACCES; | |
10324 | } | |
10325 | ||
e4222673 HB |
10326 | if (attr.namespaces) { |
10327 | if (!capable(CAP_SYS_ADMIN)) | |
10328 | return -EACCES; | |
10329 | } | |
10330 | ||
df58ab24 | 10331 | if (attr.freq) { |
cdd6c482 | 10332 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 10333 | return -EINVAL; |
0819b2e3 PZ |
10334 | } else { |
10335 | if (attr.sample_period & (1ULL << 63)) | |
10336 | return -EINVAL; | |
df58ab24 PZ |
10337 | } |
10338 | ||
fc7ce9c7 KL |
10339 | /* Only privileged users can get physical addresses */ |
10340 | if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) && | |
10341 | perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
10342 | return -EACCES; | |
10343 | ||
e5d1367f SE |
10344 | /* |
10345 | * In cgroup mode, the pid argument is used to pass the fd | |
10346 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
10347 | * designates the cpu on which to monitor threads from that | |
10348 | * cgroup. | |
10349 | */ | |
10350 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
10351 | return -EINVAL; | |
10352 | ||
a21b0b35 YD |
10353 | if (flags & PERF_FLAG_FD_CLOEXEC) |
10354 | f_flags |= O_CLOEXEC; | |
10355 | ||
10356 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
10357 | if (event_fd < 0) |
10358 | return event_fd; | |
10359 | ||
ac9721f3 | 10360 | if (group_fd != -1) { |
2903ff01 AV |
10361 | err = perf_fget_light(group_fd, &group); |
10362 | if (err) | |
d14b12d7 | 10363 | goto err_fd; |
2903ff01 | 10364 | group_leader = group.file->private_data; |
ac9721f3 PZ |
10365 | if (flags & PERF_FLAG_FD_OUTPUT) |
10366 | output_event = group_leader; | |
10367 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
10368 | group_leader = NULL; | |
10369 | } | |
10370 | ||
e5d1367f | 10371 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
10372 | task = find_lively_task_by_vpid(pid); |
10373 | if (IS_ERR(task)) { | |
10374 | err = PTR_ERR(task); | |
10375 | goto err_group_fd; | |
10376 | } | |
10377 | } | |
10378 | ||
1f4ee503 PZ |
10379 | if (task && group_leader && |
10380 | group_leader->attr.inherit != attr.inherit) { | |
10381 | err = -EINVAL; | |
10382 | goto err_task; | |
10383 | } | |
10384 | ||
79c9ce57 PZ |
10385 | if (task) { |
10386 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); | |
10387 | if (err) | |
e5aeee51 | 10388 | goto err_task; |
79c9ce57 PZ |
10389 | |
10390 | /* | |
10391 | * Reuse ptrace permission checks for now. | |
10392 | * | |
10393 | * We must hold cred_guard_mutex across this and any potential | |
10394 | * perf_install_in_context() call for this new event to | |
10395 | * serialize against exec() altering our credentials (and the | |
10396 | * perf_event_exit_task() that could imply). | |
10397 | */ | |
10398 | err = -EACCES; | |
10399 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
10400 | goto err_cred; | |
10401 | } | |
10402 | ||
79dff51e MF |
10403 | if (flags & PERF_FLAG_PID_CGROUP) |
10404 | cgroup_fd = pid; | |
10405 | ||
4dc0da86 | 10406 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 10407 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
10408 | if (IS_ERR(event)) { |
10409 | err = PTR_ERR(event); | |
79c9ce57 | 10410 | goto err_cred; |
d14b12d7 SE |
10411 | } |
10412 | ||
53b25335 VW |
10413 | if (is_sampling_event(event)) { |
10414 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 10415 | err = -EOPNOTSUPP; |
53b25335 VW |
10416 | goto err_alloc; |
10417 | } | |
10418 | } | |
10419 | ||
89a1e187 PZ |
10420 | /* |
10421 | * Special case software events and allow them to be part of | |
10422 | * any hardware group. | |
10423 | */ | |
10424 | pmu = event->pmu; | |
b04243ef | 10425 | |
34f43927 PZ |
10426 | if (attr.use_clockid) { |
10427 | err = perf_event_set_clock(event, attr.clockid); | |
10428 | if (err) | |
10429 | goto err_alloc; | |
10430 | } | |
10431 | ||
4ff6a8de DCC |
10432 | if (pmu->task_ctx_nr == perf_sw_context) |
10433 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
10434 | ||
b04243ef PZ |
10435 | if (group_leader && |
10436 | (is_software_event(event) != is_software_event(group_leader))) { | |
10437 | if (is_software_event(event)) { | |
10438 | /* | |
10439 | * If event and group_leader are not both a software | |
10440 | * event, and event is, then group leader is not. | |
10441 | * | |
10442 | * Allow the addition of software events to !software | |
10443 | * groups, this is safe because software events never | |
10444 | * fail to schedule. | |
10445 | */ | |
10446 | pmu = group_leader->pmu; | |
10447 | } else if (is_software_event(group_leader) && | |
4ff6a8de | 10448 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
b04243ef PZ |
10449 | /* |
10450 | * In case the group is a pure software group, and we | |
10451 | * try to add a hardware event, move the whole group to | |
10452 | * the hardware context. | |
10453 | */ | |
10454 | move_group = 1; | |
10455 | } | |
10456 | } | |
89a1e187 PZ |
10457 | |
10458 | /* | |
10459 | * Get the target context (task or percpu): | |
10460 | */ | |
4af57ef2 | 10461 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
10462 | if (IS_ERR(ctx)) { |
10463 | err = PTR_ERR(ctx); | |
c6be5a5c | 10464 | goto err_alloc; |
89a1e187 PZ |
10465 | } |
10466 | ||
bed5b25a AS |
10467 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
10468 | err = -EBUSY; | |
10469 | goto err_context; | |
10470 | } | |
10471 | ||
ccff286d | 10472 | /* |
cdd6c482 | 10473 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 10474 | */ |
ac9721f3 | 10475 | if (group_leader) { |
dc86cabe | 10476 | err = -EINVAL; |
04289bb9 | 10477 | |
04289bb9 | 10478 | /* |
ccff286d IM |
10479 | * Do not allow a recursive hierarchy (this new sibling |
10480 | * becoming part of another group-sibling): | |
10481 | */ | |
10482 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 10483 | goto err_context; |
34f43927 PZ |
10484 | |
10485 | /* All events in a group should have the same clock */ | |
10486 | if (group_leader->clock != event->clock) | |
10487 | goto err_context; | |
10488 | ||
ccff286d | 10489 | /* |
64aee2a9 MR |
10490 | * Make sure we're both events for the same CPU; |
10491 | * grouping events for different CPUs is broken; since | |
10492 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 10493 | */ |
64aee2a9 MR |
10494 | if (group_leader->cpu != event->cpu) |
10495 | goto err_context; | |
c3c87e77 | 10496 | |
64aee2a9 MR |
10497 | /* |
10498 | * Make sure we're both on the same task, or both | |
10499 | * per-CPU events. | |
10500 | */ | |
10501 | if (group_leader->ctx->task != ctx->task) | |
10502 | goto err_context; | |
10503 | ||
10504 | /* | |
10505 | * Do not allow to attach to a group in a different task | |
10506 | * or CPU context. If we're moving SW events, we'll fix | |
10507 | * this up later, so allow that. | |
10508 | */ | |
10509 | if (!move_group && group_leader->ctx != ctx) | |
10510 | goto err_context; | |
b04243ef | 10511 | |
3b6f9e5c PM |
10512 | /* |
10513 | * Only a group leader can be exclusive or pinned | |
10514 | */ | |
0d48696f | 10515 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 10516 | goto err_context; |
ac9721f3 PZ |
10517 | } |
10518 | ||
10519 | if (output_event) { | |
10520 | err = perf_event_set_output(event, output_event); | |
10521 | if (err) | |
c3f00c70 | 10522 | goto err_context; |
ac9721f3 | 10523 | } |
0793a61d | 10524 | |
a21b0b35 YD |
10525 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
10526 | f_flags); | |
ea635c64 AV |
10527 | if (IS_ERR(event_file)) { |
10528 | err = PTR_ERR(event_file); | |
201c2f85 | 10529 | event_file = NULL; |
c3f00c70 | 10530 | goto err_context; |
ea635c64 | 10531 | } |
9b51f66d | 10532 | |
b04243ef | 10533 | if (move_group) { |
321027c1 PZ |
10534 | gctx = __perf_event_ctx_lock_double(group_leader, ctx); |
10535 | ||
84c4e620 PZ |
10536 | if (gctx->task == TASK_TOMBSTONE) { |
10537 | err = -ESRCH; | |
10538 | goto err_locked; | |
10539 | } | |
321027c1 PZ |
10540 | |
10541 | /* | |
10542 | * Check if we raced against another sys_perf_event_open() call | |
10543 | * moving the software group underneath us. | |
10544 | */ | |
10545 | if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
10546 | /* | |
10547 | * If someone moved the group out from under us, check | |
10548 | * if this new event wound up on the same ctx, if so | |
10549 | * its the regular !move_group case, otherwise fail. | |
10550 | */ | |
10551 | if (gctx != ctx) { | |
10552 | err = -EINVAL; | |
10553 | goto err_locked; | |
10554 | } else { | |
10555 | perf_event_ctx_unlock(group_leader, gctx); | |
10556 | move_group = 0; | |
10557 | } | |
10558 | } | |
f55fc2a5 PZ |
10559 | } else { |
10560 | mutex_lock(&ctx->mutex); | |
10561 | } | |
10562 | ||
84c4e620 PZ |
10563 | if (ctx->task == TASK_TOMBSTONE) { |
10564 | err = -ESRCH; | |
10565 | goto err_locked; | |
10566 | } | |
10567 | ||
a723968c PZ |
10568 | if (!perf_event_validate_size(event)) { |
10569 | err = -E2BIG; | |
10570 | goto err_locked; | |
10571 | } | |
10572 | ||
a63fbed7 TG |
10573 | if (!task) { |
10574 | /* | |
10575 | * Check if the @cpu we're creating an event for is online. | |
10576 | * | |
10577 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10578 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10579 | */ | |
10580 | struct perf_cpu_context *cpuctx = | |
10581 | container_of(ctx, struct perf_cpu_context, ctx); | |
10582 | ||
10583 | if (!cpuctx->online) { | |
10584 | err = -ENODEV; | |
10585 | goto err_locked; | |
10586 | } | |
10587 | } | |
10588 | ||
10589 | ||
f55fc2a5 PZ |
10590 | /* |
10591 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
10592 | * because we need to serialize with concurrent event creation. | |
10593 | */ | |
10594 | if (!exclusive_event_installable(event, ctx)) { | |
10595 | /* exclusive and group stuff are assumed mutually exclusive */ | |
10596 | WARN_ON_ONCE(move_group); | |
f63a8daa | 10597 | |
f55fc2a5 PZ |
10598 | err = -EBUSY; |
10599 | goto err_locked; | |
10600 | } | |
f63a8daa | 10601 | |
f55fc2a5 PZ |
10602 | WARN_ON_ONCE(ctx->parent_ctx); |
10603 | ||
79c9ce57 PZ |
10604 | /* |
10605 | * This is the point on no return; we cannot fail hereafter. This is | |
10606 | * where we start modifying current state. | |
10607 | */ | |
10608 | ||
f55fc2a5 | 10609 | if (move_group) { |
f63a8daa PZ |
10610 | /* |
10611 | * See perf_event_ctx_lock() for comments on the details | |
10612 | * of swizzling perf_event::ctx. | |
10613 | */ | |
45a0e07a | 10614 | perf_remove_from_context(group_leader, 0); |
279b5165 | 10615 | put_ctx(gctx); |
0231bb53 | 10616 | |
b04243ef | 10617 | list_for_each_entry(sibling, &group_leader->sibling_list, |
8343aae6 | 10618 | sibling_list) { |
45a0e07a | 10619 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
10620 | put_ctx(gctx); |
10621 | } | |
b04243ef | 10622 | |
f63a8daa PZ |
10623 | /* |
10624 | * Wait for everybody to stop referencing the events through | |
10625 | * the old lists, before installing it on new lists. | |
10626 | */ | |
0cda4c02 | 10627 | synchronize_rcu(); |
f63a8daa | 10628 | |
8f95b435 PZI |
10629 | /* |
10630 | * Install the group siblings before the group leader. | |
10631 | * | |
10632 | * Because a group leader will try and install the entire group | |
10633 | * (through the sibling list, which is still in-tact), we can | |
10634 | * end up with siblings installed in the wrong context. | |
10635 | * | |
10636 | * By installing siblings first we NO-OP because they're not | |
10637 | * reachable through the group lists. | |
10638 | */ | |
b04243ef | 10639 | list_for_each_entry(sibling, &group_leader->sibling_list, |
8343aae6 | 10640 | sibling_list) { |
8f95b435 | 10641 | perf_event__state_init(sibling); |
9fc81d87 | 10642 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
10643 | get_ctx(ctx); |
10644 | } | |
8f95b435 PZI |
10645 | |
10646 | /* | |
10647 | * Removing from the context ends up with disabled | |
10648 | * event. What we want here is event in the initial | |
10649 | * startup state, ready to be add into new context. | |
10650 | */ | |
10651 | perf_event__state_init(group_leader); | |
10652 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
10653 | get_ctx(ctx); | |
bed5b25a AS |
10654 | } |
10655 | ||
f73e22ab PZ |
10656 | /* |
10657 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
10658 | * that we're serialized against further additions and before | |
10659 | * perf_install_in_context() which is the point the event is active and | |
10660 | * can use these values. | |
10661 | */ | |
10662 | perf_event__header_size(event); | |
10663 | perf_event__id_header_size(event); | |
10664 | ||
78cd2c74 PZ |
10665 | event->owner = current; |
10666 | ||
e2d37cd2 | 10667 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 10668 | perf_unpin_context(ctx); |
f63a8daa | 10669 | |
f55fc2a5 | 10670 | if (move_group) |
321027c1 | 10671 | perf_event_ctx_unlock(group_leader, gctx); |
d859e29f | 10672 | mutex_unlock(&ctx->mutex); |
9b51f66d | 10673 | |
79c9ce57 PZ |
10674 | if (task) { |
10675 | mutex_unlock(&task->signal->cred_guard_mutex); | |
10676 | put_task_struct(task); | |
10677 | } | |
10678 | ||
cdd6c482 IM |
10679 | mutex_lock(¤t->perf_event_mutex); |
10680 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
10681 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 10682 | |
8a49542c PZ |
10683 | /* |
10684 | * Drop the reference on the group_event after placing the | |
10685 | * new event on the sibling_list. This ensures destruction | |
10686 | * of the group leader will find the pointer to itself in | |
10687 | * perf_group_detach(). | |
10688 | */ | |
2903ff01 | 10689 | fdput(group); |
ea635c64 AV |
10690 | fd_install(event_fd, event_file); |
10691 | return event_fd; | |
0793a61d | 10692 | |
f55fc2a5 PZ |
10693 | err_locked: |
10694 | if (move_group) | |
321027c1 | 10695 | perf_event_ctx_unlock(group_leader, gctx); |
f55fc2a5 PZ |
10696 | mutex_unlock(&ctx->mutex); |
10697 | /* err_file: */ | |
10698 | fput(event_file); | |
c3f00c70 | 10699 | err_context: |
fe4b04fa | 10700 | perf_unpin_context(ctx); |
ea635c64 | 10701 | put_ctx(ctx); |
c6be5a5c | 10702 | err_alloc: |
13005627 PZ |
10703 | /* |
10704 | * If event_file is set, the fput() above will have called ->release() | |
10705 | * and that will take care of freeing the event. | |
10706 | */ | |
10707 | if (!event_file) | |
10708 | free_event(event); | |
79c9ce57 PZ |
10709 | err_cred: |
10710 | if (task) | |
10711 | mutex_unlock(&task->signal->cred_guard_mutex); | |
1f4ee503 | 10712 | err_task: |
e7d0bc04 PZ |
10713 | if (task) |
10714 | put_task_struct(task); | |
89a1e187 | 10715 | err_group_fd: |
2903ff01 | 10716 | fdput(group); |
ea635c64 AV |
10717 | err_fd: |
10718 | put_unused_fd(event_fd); | |
dc86cabe | 10719 | return err; |
0793a61d TG |
10720 | } |
10721 | ||
fb0459d7 AV |
10722 | /** |
10723 | * perf_event_create_kernel_counter | |
10724 | * | |
10725 | * @attr: attributes of the counter to create | |
10726 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 10727 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
10728 | */ |
10729 | struct perf_event * | |
10730 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 10731 | struct task_struct *task, |
4dc0da86 AK |
10732 | perf_overflow_handler_t overflow_handler, |
10733 | void *context) | |
fb0459d7 | 10734 | { |
fb0459d7 | 10735 | struct perf_event_context *ctx; |
c3f00c70 | 10736 | struct perf_event *event; |
fb0459d7 | 10737 | int err; |
d859e29f | 10738 | |
fb0459d7 AV |
10739 | /* |
10740 | * Get the target context (task or percpu): | |
10741 | */ | |
d859e29f | 10742 | |
4dc0da86 | 10743 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 10744 | overflow_handler, context, -1); |
c3f00c70 PZ |
10745 | if (IS_ERR(event)) { |
10746 | err = PTR_ERR(event); | |
10747 | goto err; | |
10748 | } | |
d859e29f | 10749 | |
f8697762 | 10750 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 10751 | event->owner = TASK_TOMBSTONE; |
f8697762 | 10752 | |
4af57ef2 | 10753 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
10754 | if (IS_ERR(ctx)) { |
10755 | err = PTR_ERR(ctx); | |
c3f00c70 | 10756 | goto err_free; |
d859e29f | 10757 | } |
fb0459d7 | 10758 | |
fb0459d7 AV |
10759 | WARN_ON_ONCE(ctx->parent_ctx); |
10760 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
10761 | if (ctx->task == TASK_TOMBSTONE) { |
10762 | err = -ESRCH; | |
10763 | goto err_unlock; | |
10764 | } | |
10765 | ||
a63fbed7 TG |
10766 | if (!task) { |
10767 | /* | |
10768 | * Check if the @cpu we're creating an event for is online. | |
10769 | * | |
10770 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
10771 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
10772 | */ | |
10773 | struct perf_cpu_context *cpuctx = | |
10774 | container_of(ctx, struct perf_cpu_context, ctx); | |
10775 | if (!cpuctx->online) { | |
10776 | err = -ENODEV; | |
10777 | goto err_unlock; | |
10778 | } | |
10779 | } | |
10780 | ||
bed5b25a | 10781 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 10782 | err = -EBUSY; |
84c4e620 | 10783 | goto err_unlock; |
bed5b25a AS |
10784 | } |
10785 | ||
fb0459d7 | 10786 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 10787 | perf_unpin_context(ctx); |
fb0459d7 AV |
10788 | mutex_unlock(&ctx->mutex); |
10789 | ||
fb0459d7 AV |
10790 | return event; |
10791 | ||
84c4e620 PZ |
10792 | err_unlock: |
10793 | mutex_unlock(&ctx->mutex); | |
10794 | perf_unpin_context(ctx); | |
10795 | put_ctx(ctx); | |
c3f00c70 PZ |
10796 | err_free: |
10797 | free_event(event); | |
10798 | err: | |
c6567f64 | 10799 | return ERR_PTR(err); |
9b51f66d | 10800 | } |
fb0459d7 | 10801 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 10802 | |
0cda4c02 YZ |
10803 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
10804 | { | |
10805 | struct perf_event_context *src_ctx; | |
10806 | struct perf_event_context *dst_ctx; | |
10807 | struct perf_event *event, *tmp; | |
10808 | LIST_HEAD(events); | |
10809 | ||
10810 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
10811 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
10812 | ||
f63a8daa PZ |
10813 | /* |
10814 | * See perf_event_ctx_lock() for comments on the details | |
10815 | * of swizzling perf_event::ctx. | |
10816 | */ | |
10817 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
10818 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
10819 | event_entry) { | |
45a0e07a | 10820 | perf_remove_from_context(event, 0); |
9a545de0 | 10821 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 10822 | put_ctx(src_ctx); |
9886167d | 10823 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 10824 | } |
0cda4c02 | 10825 | |
8f95b435 PZI |
10826 | /* |
10827 | * Wait for the events to quiesce before re-instating them. | |
10828 | */ | |
0cda4c02 YZ |
10829 | synchronize_rcu(); |
10830 | ||
8f95b435 PZI |
10831 | /* |
10832 | * Re-instate events in 2 passes. | |
10833 | * | |
10834 | * Skip over group leaders and only install siblings on this first | |
10835 | * pass, siblings will not get enabled without a leader, however a | |
10836 | * leader will enable its siblings, even if those are still on the old | |
10837 | * context. | |
10838 | */ | |
10839 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
10840 | if (event->group_leader == event) | |
10841 | continue; | |
10842 | ||
10843 | list_del(&event->migrate_entry); | |
10844 | if (event->state >= PERF_EVENT_STATE_OFF) | |
10845 | event->state = PERF_EVENT_STATE_INACTIVE; | |
10846 | account_event_cpu(event, dst_cpu); | |
10847 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
10848 | get_ctx(dst_ctx); | |
10849 | } | |
10850 | ||
10851 | /* | |
10852 | * Once all the siblings are setup properly, install the group leaders | |
10853 | * to make it go. | |
10854 | */ | |
9886167d PZ |
10855 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
10856 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
10857 | if (event->state >= PERF_EVENT_STATE_OFF) |
10858 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 10859 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
10860 | perf_install_in_context(dst_ctx, event, dst_cpu); |
10861 | get_ctx(dst_ctx); | |
10862 | } | |
10863 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 10864 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
10865 | } |
10866 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
10867 | ||
cdd6c482 | 10868 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 10869 | struct task_struct *child) |
d859e29f | 10870 | { |
cdd6c482 | 10871 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 10872 | u64 child_val; |
d859e29f | 10873 | |
cdd6c482 IM |
10874 | if (child_event->attr.inherit_stat) |
10875 | perf_event_read_event(child_event, child); | |
38b200d6 | 10876 | |
b5e58793 | 10877 | child_val = perf_event_count(child_event); |
d859e29f PM |
10878 | |
10879 | /* | |
10880 | * Add back the child's count to the parent's count: | |
10881 | */ | |
a6e6dea6 | 10882 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
10883 | atomic64_add(child_event->total_time_enabled, |
10884 | &parent_event->child_total_time_enabled); | |
10885 | atomic64_add(child_event->total_time_running, | |
10886 | &parent_event->child_total_time_running); | |
d859e29f PM |
10887 | } |
10888 | ||
9b51f66d | 10889 | static void |
8ba289b8 PZ |
10890 | perf_event_exit_event(struct perf_event *child_event, |
10891 | struct perf_event_context *child_ctx, | |
10892 | struct task_struct *child) | |
9b51f66d | 10893 | { |
8ba289b8 PZ |
10894 | struct perf_event *parent_event = child_event->parent; |
10895 | ||
1903d50c PZ |
10896 | /* |
10897 | * Do not destroy the 'original' grouping; because of the context | |
10898 | * switch optimization the original events could've ended up in a | |
10899 | * random child task. | |
10900 | * | |
10901 | * If we were to destroy the original group, all group related | |
10902 | * operations would cease to function properly after this random | |
10903 | * child dies. | |
10904 | * | |
10905 | * Do destroy all inherited groups, we don't care about those | |
10906 | * and being thorough is better. | |
10907 | */ | |
32132a3d PZ |
10908 | raw_spin_lock_irq(&child_ctx->lock); |
10909 | WARN_ON_ONCE(child_ctx->is_active); | |
10910 | ||
8ba289b8 | 10911 | if (parent_event) |
32132a3d PZ |
10912 | perf_group_detach(child_event); |
10913 | list_del_event(child_event, child_ctx); | |
0d3d73aa | 10914 | perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */ |
32132a3d | 10915 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 10916 | |
9b51f66d | 10917 | /* |
8ba289b8 | 10918 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 10919 | */ |
8ba289b8 | 10920 | if (!parent_event) { |
179033b3 | 10921 | perf_event_wakeup(child_event); |
8ba289b8 | 10922 | return; |
4bcf349a | 10923 | } |
8ba289b8 PZ |
10924 | /* |
10925 | * Child events can be cleaned up. | |
10926 | */ | |
10927 | ||
10928 | sync_child_event(child_event, child); | |
10929 | ||
10930 | /* | |
10931 | * Remove this event from the parent's list | |
10932 | */ | |
10933 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
10934 | mutex_lock(&parent_event->child_mutex); | |
10935 | list_del_init(&child_event->child_list); | |
10936 | mutex_unlock(&parent_event->child_mutex); | |
10937 | ||
10938 | /* | |
10939 | * Kick perf_poll() for is_event_hup(). | |
10940 | */ | |
10941 | perf_event_wakeup(parent_event); | |
10942 | free_event(child_event); | |
10943 | put_event(parent_event); | |
9b51f66d IM |
10944 | } |
10945 | ||
8dc85d54 | 10946 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 10947 | { |
211de6eb | 10948 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 10949 | struct perf_event *child_event, *next; |
63b6da39 PZ |
10950 | |
10951 | WARN_ON_ONCE(child != current); | |
9b51f66d | 10952 | |
6a3351b6 | 10953 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 10954 | if (!child_ctx) |
9b51f66d IM |
10955 | return; |
10956 | ||
ad3a37de | 10957 | /* |
6a3351b6 PZ |
10958 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
10959 | * ctx::mutex over the entire thing. This serializes against almost | |
10960 | * everything that wants to access the ctx. | |
10961 | * | |
10962 | * The exception is sys_perf_event_open() / | |
10963 | * perf_event_create_kernel_count() which does find_get_context() | |
10964 | * without ctx::mutex (it cannot because of the move_group double mutex | |
10965 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 10966 | */ |
6a3351b6 | 10967 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
10968 | |
10969 | /* | |
6a3351b6 PZ |
10970 | * In a single ctx::lock section, de-schedule the events and detach the |
10971 | * context from the task such that we cannot ever get it scheduled back | |
10972 | * in. | |
c93f7669 | 10973 | */ |
6a3351b6 | 10974 | raw_spin_lock_irq(&child_ctx->lock); |
487f05e1 | 10975 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL); |
4a1c0f26 | 10976 | |
71a851b4 | 10977 | /* |
63b6da39 PZ |
10978 | * Now that the context is inactive, destroy the task <-> ctx relation |
10979 | * and mark the context dead. | |
71a851b4 | 10980 | */ |
63b6da39 PZ |
10981 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
10982 | put_ctx(child_ctx); /* cannot be last */ | |
10983 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
10984 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 10985 | |
211de6eb | 10986 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 10987 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 10988 | |
211de6eb PZ |
10989 | if (clone_ctx) |
10990 | put_ctx(clone_ctx); | |
4a1c0f26 | 10991 | |
9f498cc5 | 10992 | /* |
cdd6c482 IM |
10993 | * Report the task dead after unscheduling the events so that we |
10994 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
10995 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 10996 | */ |
cdd6c482 | 10997 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 10998 | |
ebf905fc | 10999 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 11000 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 11001 | |
a63eaf34 PM |
11002 | mutex_unlock(&child_ctx->mutex); |
11003 | ||
11004 | put_ctx(child_ctx); | |
9b51f66d IM |
11005 | } |
11006 | ||
8dc85d54 PZ |
11007 | /* |
11008 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 PZ |
11009 | * |
11010 | * Can be called with cred_guard_mutex held when called from | |
11011 | * install_exec_creds(). | |
8dc85d54 PZ |
11012 | */ |
11013 | void perf_event_exit_task(struct task_struct *child) | |
11014 | { | |
8882135b | 11015 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
11016 | int ctxn; |
11017 | ||
8882135b PZ |
11018 | mutex_lock(&child->perf_event_mutex); |
11019 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
11020 | owner_entry) { | |
11021 | list_del_init(&event->owner_entry); | |
11022 | ||
11023 | /* | |
11024 | * Ensure the list deletion is visible before we clear | |
11025 | * the owner, closes a race against perf_release() where | |
11026 | * we need to serialize on the owner->perf_event_mutex. | |
11027 | */ | |
f47c02c0 | 11028 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
11029 | } |
11030 | mutex_unlock(&child->perf_event_mutex); | |
11031 | ||
8dc85d54 PZ |
11032 | for_each_task_context_nr(ctxn) |
11033 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
11034 | |
11035 | /* | |
11036 | * The perf_event_exit_task_context calls perf_event_task | |
11037 | * with child's task_ctx, which generates EXIT events for | |
11038 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
11039 | * At this point we need to send EXIT events to cpu contexts. | |
11040 | */ | |
11041 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
11042 | } |
11043 | ||
889ff015 FW |
11044 | static void perf_free_event(struct perf_event *event, |
11045 | struct perf_event_context *ctx) | |
11046 | { | |
11047 | struct perf_event *parent = event->parent; | |
11048 | ||
11049 | if (WARN_ON_ONCE(!parent)) | |
11050 | return; | |
11051 | ||
11052 | mutex_lock(&parent->child_mutex); | |
11053 | list_del_init(&event->child_list); | |
11054 | mutex_unlock(&parent->child_mutex); | |
11055 | ||
a6fa941d | 11056 | put_event(parent); |
889ff015 | 11057 | |
652884fe | 11058 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 11059 | perf_group_detach(event); |
889ff015 | 11060 | list_del_event(event, ctx); |
652884fe | 11061 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
11062 | free_event(event); |
11063 | } | |
11064 | ||
bbbee908 | 11065 | /* |
652884fe | 11066 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 11067 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
11068 | * |
11069 | * Not all locks are strictly required, but take them anyway to be nice and | |
11070 | * help out with the lockdep assertions. | |
bbbee908 | 11071 | */ |
cdd6c482 | 11072 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 11073 | { |
8dc85d54 | 11074 | struct perf_event_context *ctx; |
cdd6c482 | 11075 | struct perf_event *event, *tmp; |
8dc85d54 | 11076 | int ctxn; |
bbbee908 | 11077 | |
8dc85d54 PZ |
11078 | for_each_task_context_nr(ctxn) { |
11079 | ctx = task->perf_event_ctxp[ctxn]; | |
11080 | if (!ctx) | |
11081 | continue; | |
bbbee908 | 11082 | |
8dc85d54 | 11083 | mutex_lock(&ctx->mutex); |
e552a838 PZ |
11084 | raw_spin_lock_irq(&ctx->lock); |
11085 | /* | |
11086 | * Destroy the task <-> ctx relation and mark the context dead. | |
11087 | * | |
11088 | * This is important because even though the task hasn't been | |
11089 | * exposed yet the context has been (through child_list). | |
11090 | */ | |
11091 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); | |
11092 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
11093 | put_task_struct(task); /* cannot be last */ | |
11094 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 11095 | |
15121c78 | 11096 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
8dc85d54 | 11097 | perf_free_event(event, ctx); |
bbbee908 | 11098 | |
8dc85d54 | 11099 | mutex_unlock(&ctx->mutex); |
8dc85d54 PZ |
11100 | put_ctx(ctx); |
11101 | } | |
889ff015 FW |
11102 | } |
11103 | ||
4e231c79 PZ |
11104 | void perf_event_delayed_put(struct task_struct *task) |
11105 | { | |
11106 | int ctxn; | |
11107 | ||
11108 | for_each_task_context_nr(ctxn) | |
11109 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
11110 | } | |
11111 | ||
e03e7ee3 | 11112 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 11113 | { |
e03e7ee3 | 11114 | struct file *file; |
ffe8690c | 11115 | |
e03e7ee3 AS |
11116 | file = fget_raw(fd); |
11117 | if (!file) | |
11118 | return ERR_PTR(-EBADF); | |
ffe8690c | 11119 | |
e03e7ee3 AS |
11120 | if (file->f_op != &perf_fops) { |
11121 | fput(file); | |
11122 | return ERR_PTR(-EBADF); | |
11123 | } | |
ffe8690c | 11124 | |
e03e7ee3 | 11125 | return file; |
ffe8690c KX |
11126 | } |
11127 | ||
11128 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
11129 | { | |
11130 | if (!event) | |
11131 | return ERR_PTR(-EINVAL); | |
11132 | ||
11133 | return &event->attr; | |
11134 | } | |
11135 | ||
97dee4f3 | 11136 | /* |
d8a8cfc7 PZ |
11137 | * Inherit a event from parent task to child task. |
11138 | * | |
11139 | * Returns: | |
11140 | * - valid pointer on success | |
11141 | * - NULL for orphaned events | |
11142 | * - IS_ERR() on error | |
97dee4f3 PZ |
11143 | */ |
11144 | static struct perf_event * | |
11145 | inherit_event(struct perf_event *parent_event, | |
11146 | struct task_struct *parent, | |
11147 | struct perf_event_context *parent_ctx, | |
11148 | struct task_struct *child, | |
11149 | struct perf_event *group_leader, | |
11150 | struct perf_event_context *child_ctx) | |
11151 | { | |
8ca2bd41 | 11152 | enum perf_event_state parent_state = parent_event->state; |
97dee4f3 | 11153 | struct perf_event *child_event; |
cee010ec | 11154 | unsigned long flags; |
97dee4f3 PZ |
11155 | |
11156 | /* | |
11157 | * Instead of creating recursive hierarchies of events, | |
11158 | * we link inherited events back to the original parent, | |
11159 | * which has a filp for sure, which we use as the reference | |
11160 | * count: | |
11161 | */ | |
11162 | if (parent_event->parent) | |
11163 | parent_event = parent_event->parent; | |
11164 | ||
11165 | child_event = perf_event_alloc(&parent_event->attr, | |
11166 | parent_event->cpu, | |
d580ff86 | 11167 | child, |
97dee4f3 | 11168 | group_leader, parent_event, |
79dff51e | 11169 | NULL, NULL, -1); |
97dee4f3 PZ |
11170 | if (IS_ERR(child_event)) |
11171 | return child_event; | |
a6fa941d | 11172 | |
313ccb96 JO |
11173 | |
11174 | if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) && | |
11175 | !child_ctx->task_ctx_data) { | |
11176 | struct pmu *pmu = child_event->pmu; | |
11177 | ||
11178 | child_ctx->task_ctx_data = kzalloc(pmu->task_ctx_size, | |
11179 | GFP_KERNEL); | |
11180 | if (!child_ctx->task_ctx_data) { | |
11181 | free_event(child_event); | |
11182 | return NULL; | |
11183 | } | |
11184 | } | |
11185 | ||
c6e5b732 PZ |
11186 | /* |
11187 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
11188 | * must be under the same lock in order to serialize against | |
11189 | * perf_event_release_kernel(), such that either we must observe | |
11190 | * is_orphaned_event() or they will observe us on the child_list. | |
11191 | */ | |
11192 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
11193 | if (is_orphaned_event(parent_event) || |
11194 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 11195 | mutex_unlock(&parent_event->child_mutex); |
313ccb96 | 11196 | /* task_ctx_data is freed with child_ctx */ |
a6fa941d AV |
11197 | free_event(child_event); |
11198 | return NULL; | |
11199 | } | |
11200 | ||
97dee4f3 PZ |
11201 | get_ctx(child_ctx); |
11202 | ||
11203 | /* | |
11204 | * Make the child state follow the state of the parent event, | |
11205 | * not its attr.disabled bit. We hold the parent's mutex, | |
11206 | * so we won't race with perf_event_{en, dis}able_family. | |
11207 | */ | |
1929def9 | 11208 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
11209 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
11210 | else | |
11211 | child_event->state = PERF_EVENT_STATE_OFF; | |
11212 | ||
11213 | if (parent_event->attr.freq) { | |
11214 | u64 sample_period = parent_event->hw.sample_period; | |
11215 | struct hw_perf_event *hwc = &child_event->hw; | |
11216 | ||
11217 | hwc->sample_period = sample_period; | |
11218 | hwc->last_period = sample_period; | |
11219 | ||
11220 | local64_set(&hwc->period_left, sample_period); | |
11221 | } | |
11222 | ||
11223 | child_event->ctx = child_ctx; | |
11224 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
11225 | child_event->overflow_handler_context |
11226 | = parent_event->overflow_handler_context; | |
97dee4f3 | 11227 | |
614b6780 TG |
11228 | /* |
11229 | * Precalculate sample_data sizes | |
11230 | */ | |
11231 | perf_event__header_size(child_event); | |
6844c09d | 11232 | perf_event__id_header_size(child_event); |
614b6780 | 11233 | |
97dee4f3 PZ |
11234 | /* |
11235 | * Link it up in the child's context: | |
11236 | */ | |
cee010ec | 11237 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 11238 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 11239 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 11240 | |
97dee4f3 PZ |
11241 | /* |
11242 | * Link this into the parent event's child list | |
11243 | */ | |
97dee4f3 PZ |
11244 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
11245 | mutex_unlock(&parent_event->child_mutex); | |
11246 | ||
11247 | return child_event; | |
11248 | } | |
11249 | ||
d8a8cfc7 PZ |
11250 | /* |
11251 | * Inherits an event group. | |
11252 | * | |
11253 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
11254 | * This matches with perf_event_release_kernel() removing all child events. | |
11255 | * | |
11256 | * Returns: | |
11257 | * - 0 on success | |
11258 | * - <0 on error | |
11259 | */ | |
97dee4f3 PZ |
11260 | static int inherit_group(struct perf_event *parent_event, |
11261 | struct task_struct *parent, | |
11262 | struct perf_event_context *parent_ctx, | |
11263 | struct task_struct *child, | |
11264 | struct perf_event_context *child_ctx) | |
11265 | { | |
11266 | struct perf_event *leader; | |
11267 | struct perf_event *sub; | |
11268 | struct perf_event *child_ctr; | |
11269 | ||
11270 | leader = inherit_event(parent_event, parent, parent_ctx, | |
11271 | child, NULL, child_ctx); | |
11272 | if (IS_ERR(leader)) | |
11273 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
11274 | /* |
11275 | * @leader can be NULL here because of is_orphaned_event(). In this | |
11276 | * case inherit_event() will create individual events, similar to what | |
11277 | * perf_group_detach() would do anyway. | |
11278 | */ | |
8343aae6 | 11279 | list_for_each_entry(sub, &parent_event->sibling_list, sibling_list) { |
97dee4f3 PZ |
11280 | child_ctr = inherit_event(sub, parent, parent_ctx, |
11281 | child, leader, child_ctx); | |
11282 | if (IS_ERR(child_ctr)) | |
11283 | return PTR_ERR(child_ctr); | |
11284 | } | |
11285 | return 0; | |
889ff015 FW |
11286 | } |
11287 | ||
d8a8cfc7 PZ |
11288 | /* |
11289 | * Creates the child task context and tries to inherit the event-group. | |
11290 | * | |
11291 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
11292 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
11293 | * consistent with perf_event_release_kernel() removing all child events. | |
11294 | * | |
11295 | * Returns: | |
11296 | * - 0 on success | |
11297 | * - <0 on error | |
11298 | */ | |
889ff015 FW |
11299 | static int |
11300 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
11301 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 11302 | struct task_struct *child, int ctxn, |
889ff015 FW |
11303 | int *inherited_all) |
11304 | { | |
11305 | int ret; | |
8dc85d54 | 11306 | struct perf_event_context *child_ctx; |
889ff015 FW |
11307 | |
11308 | if (!event->attr.inherit) { | |
11309 | *inherited_all = 0; | |
11310 | return 0; | |
bbbee908 PZ |
11311 | } |
11312 | ||
fe4b04fa | 11313 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
11314 | if (!child_ctx) { |
11315 | /* | |
11316 | * This is executed from the parent task context, so | |
11317 | * inherit events that have been marked for cloning. | |
11318 | * First allocate and initialize a context for the | |
11319 | * child. | |
11320 | */ | |
734df5ab | 11321 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
11322 | if (!child_ctx) |
11323 | return -ENOMEM; | |
bbbee908 | 11324 | |
8dc85d54 | 11325 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
11326 | } |
11327 | ||
11328 | ret = inherit_group(event, parent, parent_ctx, | |
11329 | child, child_ctx); | |
11330 | ||
11331 | if (ret) | |
11332 | *inherited_all = 0; | |
11333 | ||
11334 | return ret; | |
bbbee908 PZ |
11335 | } |
11336 | ||
9b51f66d | 11337 | /* |
cdd6c482 | 11338 | * Initialize the perf_event context in task_struct |
9b51f66d | 11339 | */ |
985c8dcb | 11340 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 11341 | { |
889ff015 | 11342 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
11343 | struct perf_event_context *cloned_ctx; |
11344 | struct perf_event *event; | |
9b51f66d | 11345 | struct task_struct *parent = current; |
564c2b21 | 11346 | int inherited_all = 1; |
dddd3379 | 11347 | unsigned long flags; |
6ab423e0 | 11348 | int ret = 0; |
9b51f66d | 11349 | |
8dc85d54 | 11350 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
11351 | return 0; |
11352 | ||
ad3a37de | 11353 | /* |
25346b93 PM |
11354 | * If the parent's context is a clone, pin it so it won't get |
11355 | * swapped under us. | |
ad3a37de | 11356 | */ |
8dc85d54 | 11357 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
11358 | if (!parent_ctx) |
11359 | return 0; | |
25346b93 | 11360 | |
ad3a37de PM |
11361 | /* |
11362 | * No need to check if parent_ctx != NULL here; since we saw | |
11363 | * it non-NULL earlier, the only reason for it to become NULL | |
11364 | * is if we exit, and since we're currently in the middle of | |
11365 | * a fork we can't be exiting at the same time. | |
11366 | */ | |
ad3a37de | 11367 | |
9b51f66d IM |
11368 | /* |
11369 | * Lock the parent list. No need to lock the child - not PID | |
11370 | * hashed yet and not running, so nobody can access it. | |
11371 | */ | |
d859e29f | 11372 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
11373 | |
11374 | /* | |
11375 | * We dont have to disable NMIs - we are only looking at | |
11376 | * the list, not manipulating it: | |
11377 | */ | |
6e6804d2 | 11378 | perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { |
8dc85d54 PZ |
11379 | ret = inherit_task_group(event, parent, parent_ctx, |
11380 | child, ctxn, &inherited_all); | |
889ff015 | 11381 | if (ret) |
e7cc4865 | 11382 | goto out_unlock; |
889ff015 | 11383 | } |
b93f7978 | 11384 | |
dddd3379 TG |
11385 | /* |
11386 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
11387 | * to allocations, but we need to prevent rotation because | |
11388 | * rotate_ctx() will change the list from interrupt context. | |
11389 | */ | |
11390 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
11391 | parent_ctx->rotate_disable = 1; | |
11392 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
11393 | ||
6e6804d2 | 11394 | perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { |
8dc85d54 PZ |
11395 | ret = inherit_task_group(event, parent, parent_ctx, |
11396 | child, ctxn, &inherited_all); | |
889ff015 | 11397 | if (ret) |
e7cc4865 | 11398 | goto out_unlock; |
564c2b21 PM |
11399 | } |
11400 | ||
dddd3379 TG |
11401 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
11402 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 11403 | |
8dc85d54 | 11404 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 11405 | |
05cbaa28 | 11406 | if (child_ctx && inherited_all) { |
564c2b21 PM |
11407 | /* |
11408 | * Mark the child context as a clone of the parent | |
11409 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
11410 | * |
11411 | * Note that if the parent is a clone, the holding of | |
11412 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 11413 | */ |
c5ed5145 | 11414 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
11415 | if (cloned_ctx) { |
11416 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 11417 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
11418 | } else { |
11419 | child_ctx->parent_ctx = parent_ctx; | |
11420 | child_ctx->parent_gen = parent_ctx->generation; | |
11421 | } | |
11422 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
11423 | } |
11424 | ||
c5ed5145 | 11425 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 11426 | out_unlock: |
d859e29f | 11427 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 11428 | |
25346b93 | 11429 | perf_unpin_context(parent_ctx); |
fe4b04fa | 11430 | put_ctx(parent_ctx); |
ad3a37de | 11431 | |
6ab423e0 | 11432 | return ret; |
9b51f66d IM |
11433 | } |
11434 | ||
8dc85d54 PZ |
11435 | /* |
11436 | * Initialize the perf_event context in task_struct | |
11437 | */ | |
11438 | int perf_event_init_task(struct task_struct *child) | |
11439 | { | |
11440 | int ctxn, ret; | |
11441 | ||
8550d7cb ON |
11442 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
11443 | mutex_init(&child->perf_event_mutex); | |
11444 | INIT_LIST_HEAD(&child->perf_event_list); | |
11445 | ||
8dc85d54 PZ |
11446 | for_each_task_context_nr(ctxn) { |
11447 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
11448 | if (ret) { |
11449 | perf_event_free_task(child); | |
8dc85d54 | 11450 | return ret; |
6c72e350 | 11451 | } |
8dc85d54 PZ |
11452 | } |
11453 | ||
11454 | return 0; | |
11455 | } | |
11456 | ||
220b140b PM |
11457 | static void __init perf_event_init_all_cpus(void) |
11458 | { | |
b28ab83c | 11459 | struct swevent_htable *swhash; |
220b140b | 11460 | int cpu; |
220b140b | 11461 | |
a63fbed7 TG |
11462 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
11463 | ||
220b140b | 11464 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
11465 | swhash = &per_cpu(swevent_htable, cpu); |
11466 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 11467 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
11468 | |
11469 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
11470 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 11471 | |
058fe1c0 DCC |
11472 | #ifdef CONFIG_CGROUP_PERF |
11473 | INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu)); | |
11474 | #endif | |
e48c1788 | 11475 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
220b140b PM |
11476 | } |
11477 | } | |
11478 | ||
a63fbed7 | 11479 | void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 11480 | { |
108b02cf | 11481 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 11482 | |
b28ab83c | 11483 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 11484 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
11485 | struct swevent_hlist *hlist; |
11486 | ||
b28ab83c PZ |
11487 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
11488 | WARN_ON(!hlist); | |
11489 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 11490 | } |
b28ab83c | 11491 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
11492 | } |
11493 | ||
2965faa5 | 11494 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 11495 | static void __perf_event_exit_context(void *__info) |
0793a61d | 11496 | { |
108b02cf | 11497 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
11498 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
11499 | struct perf_event *event; | |
0793a61d | 11500 | |
fae3fde6 | 11501 | raw_spin_lock(&ctx->lock); |
0ee098c9 | 11502 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
fae3fde6 | 11503 | list_for_each_entry(event, &ctx->event_list, event_entry) |
45a0e07a | 11504 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 11505 | raw_spin_unlock(&ctx->lock); |
0793a61d | 11506 | } |
108b02cf PZ |
11507 | |
11508 | static void perf_event_exit_cpu_context(int cpu) | |
11509 | { | |
a63fbed7 | 11510 | struct perf_cpu_context *cpuctx; |
108b02cf PZ |
11511 | struct perf_event_context *ctx; |
11512 | struct pmu *pmu; | |
108b02cf | 11513 | |
a63fbed7 TG |
11514 | mutex_lock(&pmus_lock); |
11515 | list_for_each_entry(pmu, &pmus, entry) { | |
11516 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11517 | ctx = &cpuctx->ctx; | |
108b02cf PZ |
11518 | |
11519 | mutex_lock(&ctx->mutex); | |
11520 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
a63fbed7 | 11521 | cpuctx->online = 0; |
108b02cf PZ |
11522 | mutex_unlock(&ctx->mutex); |
11523 | } | |
a63fbed7 TG |
11524 | cpumask_clear_cpu(cpu, perf_online_mask); |
11525 | mutex_unlock(&pmus_lock); | |
108b02cf | 11526 | } |
00e16c3d TG |
11527 | #else |
11528 | ||
11529 | static void perf_event_exit_cpu_context(int cpu) { } | |
11530 | ||
11531 | #endif | |
108b02cf | 11532 | |
a63fbed7 TG |
11533 | int perf_event_init_cpu(unsigned int cpu) |
11534 | { | |
11535 | struct perf_cpu_context *cpuctx; | |
11536 | struct perf_event_context *ctx; | |
11537 | struct pmu *pmu; | |
11538 | ||
11539 | perf_swevent_init_cpu(cpu); | |
11540 | ||
11541 | mutex_lock(&pmus_lock); | |
11542 | cpumask_set_cpu(cpu, perf_online_mask); | |
11543 | list_for_each_entry(pmu, &pmus, entry) { | |
11544 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
11545 | ctx = &cpuctx->ctx; | |
11546 | ||
11547 | mutex_lock(&ctx->mutex); | |
11548 | cpuctx->online = 1; | |
11549 | mutex_unlock(&ctx->mutex); | |
11550 | } | |
11551 | mutex_unlock(&pmus_lock); | |
11552 | ||
11553 | return 0; | |
11554 | } | |
11555 | ||
00e16c3d | 11556 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 11557 | { |
e3703f8c | 11558 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 11559 | return 0; |
0793a61d | 11560 | } |
0793a61d | 11561 | |
c277443c PZ |
11562 | static int |
11563 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
11564 | { | |
11565 | int cpu; | |
11566 | ||
11567 | for_each_online_cpu(cpu) | |
11568 | perf_event_exit_cpu(cpu); | |
11569 | ||
11570 | return NOTIFY_OK; | |
11571 | } | |
11572 | ||
11573 | /* | |
11574 | * Run the perf reboot notifier at the very last possible moment so that | |
11575 | * the generic watchdog code runs as long as possible. | |
11576 | */ | |
11577 | static struct notifier_block perf_reboot_notifier = { | |
11578 | .notifier_call = perf_reboot, | |
11579 | .priority = INT_MIN, | |
11580 | }; | |
11581 | ||
cdd6c482 | 11582 | void __init perf_event_init(void) |
0793a61d | 11583 | { |
3c502e7a JW |
11584 | int ret; |
11585 | ||
2e80a82a PZ |
11586 | idr_init(&pmu_idr); |
11587 | ||
220b140b | 11588 | perf_event_init_all_cpus(); |
b0a873eb | 11589 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
11590 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
11591 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
11592 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb | 11593 | perf_tp_register(); |
00e16c3d | 11594 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 11595 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
11596 | |
11597 | ret = init_hw_breakpoint(); | |
11598 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 11599 | |
b01c3a00 JO |
11600 | /* |
11601 | * Build time assertion that we keep the data_head at the intended | |
11602 | * location. IOW, validation we got the __reserved[] size right. | |
11603 | */ | |
11604 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
11605 | != 1024); | |
0793a61d | 11606 | } |
abe43400 | 11607 | |
fd979c01 CS |
11608 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
11609 | char *page) | |
11610 | { | |
11611 | struct perf_pmu_events_attr *pmu_attr = | |
11612 | container_of(attr, struct perf_pmu_events_attr, attr); | |
11613 | ||
11614 | if (pmu_attr->event_str) | |
11615 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
11616 | ||
11617 | return 0; | |
11618 | } | |
675965b0 | 11619 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 11620 | |
abe43400 PZ |
11621 | static int __init perf_event_sysfs_init(void) |
11622 | { | |
11623 | struct pmu *pmu; | |
11624 | int ret; | |
11625 | ||
11626 | mutex_lock(&pmus_lock); | |
11627 | ||
11628 | ret = bus_register(&pmu_bus); | |
11629 | if (ret) | |
11630 | goto unlock; | |
11631 | ||
11632 | list_for_each_entry(pmu, &pmus, entry) { | |
11633 | if (!pmu->name || pmu->type < 0) | |
11634 | continue; | |
11635 | ||
11636 | ret = pmu_dev_alloc(pmu); | |
11637 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
11638 | } | |
11639 | pmu_bus_running = 1; | |
11640 | ret = 0; | |
11641 | ||
11642 | unlock: | |
11643 | mutex_unlock(&pmus_lock); | |
11644 | ||
11645 | return ret; | |
11646 | } | |
11647 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
11648 | |
11649 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
11650 | static struct cgroup_subsys_state * |
11651 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
11652 | { |
11653 | struct perf_cgroup *jc; | |
e5d1367f | 11654 | |
1b15d055 | 11655 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
11656 | if (!jc) |
11657 | return ERR_PTR(-ENOMEM); | |
11658 | ||
e5d1367f SE |
11659 | jc->info = alloc_percpu(struct perf_cgroup_info); |
11660 | if (!jc->info) { | |
11661 | kfree(jc); | |
11662 | return ERR_PTR(-ENOMEM); | |
11663 | } | |
11664 | ||
e5d1367f SE |
11665 | return &jc->css; |
11666 | } | |
11667 | ||
eb95419b | 11668 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 11669 | { |
eb95419b TH |
11670 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
11671 | ||
e5d1367f SE |
11672 | free_percpu(jc->info); |
11673 | kfree(jc); | |
11674 | } | |
11675 | ||
11676 | static int __perf_cgroup_move(void *info) | |
11677 | { | |
11678 | struct task_struct *task = info; | |
ddaaf4e2 | 11679 | rcu_read_lock(); |
e5d1367f | 11680 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 11681 | rcu_read_unlock(); |
e5d1367f SE |
11682 | return 0; |
11683 | } | |
11684 | ||
1f7dd3e5 | 11685 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 11686 | { |
bb9d97b6 | 11687 | struct task_struct *task; |
1f7dd3e5 | 11688 | struct cgroup_subsys_state *css; |
bb9d97b6 | 11689 | |
1f7dd3e5 | 11690 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 11691 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
11692 | } |
11693 | ||
073219e9 | 11694 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
11695 | .css_alloc = perf_cgroup_css_alloc, |
11696 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 11697 | .attach = perf_cgroup_attach, |
968ebff1 TH |
11698 | /* |
11699 | * Implicitly enable on dfl hierarchy so that perf events can | |
11700 | * always be filtered by cgroup2 path as long as perf_event | |
11701 | * controller is not mounted on a legacy hierarchy. | |
11702 | */ | |
11703 | .implicit_on_dfl = true, | |
8cfd8147 | 11704 | .threaded = true, |
e5d1367f SE |
11705 | }; |
11706 | #endif /* CONFIG_CGROUP_PERF */ |