]>
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> | |
0793a61d | 49 | |
76369139 FW |
50 | #include "internal.h" |
51 | ||
4e193bd4 TB |
52 | #include <asm/irq_regs.h> |
53 | ||
272325c4 PZ |
54 | typedef int (*remote_function_f)(void *); |
55 | ||
fe4b04fa | 56 | struct remote_function_call { |
e7e7ee2e | 57 | struct task_struct *p; |
272325c4 | 58 | remote_function_f func; |
e7e7ee2e IM |
59 | void *info; |
60 | int ret; | |
fe4b04fa PZ |
61 | }; |
62 | ||
63 | static void remote_function(void *data) | |
64 | { | |
65 | struct remote_function_call *tfc = data; | |
66 | struct task_struct *p = tfc->p; | |
67 | ||
68 | if (p) { | |
0da4cf3e PZ |
69 | /* -EAGAIN */ |
70 | if (task_cpu(p) != smp_processor_id()) | |
71 | return; | |
72 | ||
73 | /* | |
74 | * Now that we're on right CPU with IRQs disabled, we can test | |
75 | * if we hit the right task without races. | |
76 | */ | |
77 | ||
78 | tfc->ret = -ESRCH; /* No such (running) process */ | |
79 | if (p != current) | |
fe4b04fa PZ |
80 | return; |
81 | } | |
82 | ||
83 | tfc->ret = tfc->func(tfc->info); | |
84 | } | |
85 | ||
86 | /** | |
87 | * task_function_call - call a function on the cpu on which a task runs | |
88 | * @p: the task to evaluate | |
89 | * @func: the function to be called | |
90 | * @info: the function call argument | |
91 | * | |
92 | * Calls the function @func when the task is currently running. This might | |
93 | * be on the current CPU, which just calls the function directly | |
94 | * | |
95 | * returns: @func return value, or | |
96 | * -ESRCH - when the process isn't running | |
97 | * -EAGAIN - when the process moved away | |
98 | */ | |
99 | static int | |
272325c4 | 100 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
101 | { |
102 | struct remote_function_call data = { | |
e7e7ee2e IM |
103 | .p = p, |
104 | .func = func, | |
105 | .info = info, | |
0da4cf3e | 106 | .ret = -EAGAIN, |
fe4b04fa | 107 | }; |
0da4cf3e | 108 | int ret; |
fe4b04fa | 109 | |
0da4cf3e PZ |
110 | do { |
111 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); | |
112 | if (!ret) | |
113 | ret = data.ret; | |
114 | } while (ret == -EAGAIN); | |
fe4b04fa | 115 | |
0da4cf3e | 116 | return ret; |
fe4b04fa PZ |
117 | } |
118 | ||
119 | /** | |
120 | * cpu_function_call - call a function on the cpu | |
121 | * @func: the function to be called | |
122 | * @info: the function call argument | |
123 | * | |
124 | * Calls the function @func on the remote cpu. | |
125 | * | |
126 | * returns: @func return value or -ENXIO when the cpu is offline | |
127 | */ | |
272325c4 | 128 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
129 | { |
130 | struct remote_function_call data = { | |
e7e7ee2e IM |
131 | .p = NULL, |
132 | .func = func, | |
133 | .info = info, | |
134 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
135 | }; |
136 | ||
137 | smp_call_function_single(cpu, remote_function, &data, 1); | |
138 | ||
139 | return data.ret; | |
140 | } | |
141 | ||
fae3fde6 PZ |
142 | static inline struct perf_cpu_context * |
143 | __get_cpu_context(struct perf_event_context *ctx) | |
144 | { | |
145 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); | |
146 | } | |
147 | ||
148 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, | |
149 | struct perf_event_context *ctx) | |
0017960f | 150 | { |
fae3fde6 PZ |
151 | raw_spin_lock(&cpuctx->ctx.lock); |
152 | if (ctx) | |
153 | raw_spin_lock(&ctx->lock); | |
154 | } | |
155 | ||
156 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
157 | struct perf_event_context *ctx) | |
158 | { | |
159 | if (ctx) | |
160 | raw_spin_unlock(&ctx->lock); | |
161 | raw_spin_unlock(&cpuctx->ctx.lock); | |
162 | } | |
163 | ||
63b6da39 PZ |
164 | #define TASK_TOMBSTONE ((void *)-1L) |
165 | ||
166 | static bool is_kernel_event(struct perf_event *event) | |
167 | { | |
f47c02c0 | 168 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
169 | } |
170 | ||
39a43640 PZ |
171 | /* |
172 | * On task ctx scheduling... | |
173 | * | |
174 | * When !ctx->nr_events a task context will not be scheduled. This means | |
175 | * we can disable the scheduler hooks (for performance) without leaving | |
176 | * pending task ctx state. | |
177 | * | |
178 | * This however results in two special cases: | |
179 | * | |
180 | * - removing the last event from a task ctx; this is relatively straight | |
181 | * forward and is done in __perf_remove_from_context. | |
182 | * | |
183 | * - adding the first event to a task ctx; this is tricky because we cannot | |
184 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
185 | * See perf_install_in_context(). | |
186 | * | |
39a43640 PZ |
187 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
188 | */ | |
189 | ||
fae3fde6 PZ |
190 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
191 | struct perf_event_context *, void *); | |
192 | ||
193 | struct event_function_struct { | |
194 | struct perf_event *event; | |
195 | event_f func; | |
196 | void *data; | |
197 | }; | |
198 | ||
199 | static int event_function(void *info) | |
200 | { | |
201 | struct event_function_struct *efs = info; | |
202 | struct perf_event *event = efs->event; | |
0017960f | 203 | struct perf_event_context *ctx = event->ctx; |
fae3fde6 PZ |
204 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
205 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
63b6da39 | 206 | int ret = 0; |
fae3fde6 PZ |
207 | |
208 | WARN_ON_ONCE(!irqs_disabled()); | |
209 | ||
63b6da39 | 210 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
211 | /* |
212 | * Since we do the IPI call without holding ctx->lock things can have | |
213 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
214 | */ |
215 | if (ctx->task) { | |
63b6da39 | 216 | if (ctx->task != current) { |
0da4cf3e | 217 | ret = -ESRCH; |
63b6da39 PZ |
218 | goto unlock; |
219 | } | |
fae3fde6 | 220 | |
fae3fde6 PZ |
221 | /* |
222 | * We only use event_function_call() on established contexts, | |
223 | * and event_function() is only ever called when active (or | |
224 | * rather, we'll have bailed in task_function_call() or the | |
225 | * above ctx->task != current test), therefore we must have | |
226 | * ctx->is_active here. | |
227 | */ | |
228 | WARN_ON_ONCE(!ctx->is_active); | |
229 | /* | |
230 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
231 | * match. | |
232 | */ | |
63b6da39 PZ |
233 | WARN_ON_ONCE(task_ctx != ctx); |
234 | } else { | |
235 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 236 | } |
63b6da39 | 237 | |
fae3fde6 | 238 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 239 | unlock: |
fae3fde6 PZ |
240 | perf_ctx_unlock(cpuctx, task_ctx); |
241 | ||
63b6da39 | 242 | return ret; |
fae3fde6 PZ |
243 | } |
244 | ||
245 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
246 | { | |
247 | struct event_function_struct efs = { | |
248 | .event = event, | |
249 | .func = func, | |
250 | .data = data, | |
251 | }; | |
252 | ||
253 | int ret = event_function(&efs); | |
254 | WARN_ON_ONCE(ret); | |
255 | } | |
256 | ||
257 | static void event_function_call(struct perf_event *event, event_f func, void *data) | |
0017960f PZ |
258 | { |
259 | struct perf_event_context *ctx = event->ctx; | |
63b6da39 | 260 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
fae3fde6 PZ |
261 | struct event_function_struct efs = { |
262 | .event = event, | |
263 | .func = func, | |
264 | .data = data, | |
265 | }; | |
0017960f | 266 | |
c97f4736 PZ |
267 | if (!event->parent) { |
268 | /* | |
269 | * If this is a !child event, we must hold ctx::mutex to | |
270 | * stabilize the the event->ctx relation. See | |
271 | * perf_event_ctx_lock(). | |
272 | */ | |
273 | lockdep_assert_held(&ctx->mutex); | |
274 | } | |
0017960f PZ |
275 | |
276 | if (!task) { | |
fae3fde6 | 277 | cpu_function_call(event->cpu, event_function, &efs); |
0017960f PZ |
278 | return; |
279 | } | |
280 | ||
63b6da39 PZ |
281 | if (task == TASK_TOMBSTONE) |
282 | return; | |
283 | ||
a096309b | 284 | again: |
fae3fde6 | 285 | if (!task_function_call(task, event_function, &efs)) |
0017960f PZ |
286 | return; |
287 | ||
288 | raw_spin_lock_irq(&ctx->lock); | |
63b6da39 PZ |
289 | /* |
290 | * Reload the task pointer, it might have been changed by | |
291 | * a concurrent perf_event_context_sched_out(). | |
292 | */ | |
293 | task = ctx->task; | |
a096309b PZ |
294 | if (task == TASK_TOMBSTONE) { |
295 | raw_spin_unlock_irq(&ctx->lock); | |
296 | return; | |
0017960f | 297 | } |
a096309b PZ |
298 | if (ctx->is_active) { |
299 | raw_spin_unlock_irq(&ctx->lock); | |
300 | goto again; | |
301 | } | |
302 | func(event, NULL, ctx, data); | |
0017960f PZ |
303 | raw_spin_unlock_irq(&ctx->lock); |
304 | } | |
305 | ||
e5d1367f SE |
306 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
307 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
308 | PERF_FLAG_PID_CGROUP |\ |
309 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 310 | |
bce38cd5 SE |
311 | /* |
312 | * branch priv levels that need permission checks | |
313 | */ | |
314 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
315 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
316 | PERF_SAMPLE_BRANCH_HV) | |
317 | ||
0b3fcf17 SE |
318 | enum event_type_t { |
319 | EVENT_FLEXIBLE = 0x1, | |
320 | EVENT_PINNED = 0x2, | |
3cbaa590 | 321 | EVENT_TIME = 0x4, |
0b3fcf17 SE |
322 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
323 | }; | |
324 | ||
e5d1367f SE |
325 | /* |
326 | * perf_sched_events : >0 events exist | |
327 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu | |
328 | */ | |
9107c89e PZ |
329 | |
330 | static void perf_sched_delayed(struct work_struct *work); | |
331 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
332 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
333 | static DEFINE_MUTEX(perf_sched_mutex); | |
334 | static atomic_t perf_sched_count; | |
335 | ||
e5d1367f | 336 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
ba532500 | 337 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
f2fb6bef | 338 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
e5d1367f | 339 | |
cdd6c482 IM |
340 | static atomic_t nr_mmap_events __read_mostly; |
341 | static atomic_t nr_comm_events __read_mostly; | |
342 | static atomic_t nr_task_events __read_mostly; | |
948b26b6 | 343 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 344 | static atomic_t nr_switch_events __read_mostly; |
9ee318a7 | 345 | |
108b02cf PZ |
346 | static LIST_HEAD(pmus); |
347 | static DEFINE_MUTEX(pmus_lock); | |
348 | static struct srcu_struct pmus_srcu; | |
349 | ||
0764771d | 350 | /* |
cdd6c482 | 351 | * perf event paranoia level: |
0fbdea19 IM |
352 | * -1 - not paranoid at all |
353 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 354 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 355 | * 2 - disallow kernel profiling for unpriv |
0764771d | 356 | */ |
0161028b | 357 | int sysctl_perf_event_paranoid __read_mostly = 2; |
0764771d | 358 | |
20443384 FW |
359 | /* Minimum for 512 kiB + 1 user control page */ |
360 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
361 | |
362 | /* | |
cdd6c482 | 363 | * max perf event sample rate |
df58ab24 | 364 | */ |
14c63f17 DH |
365 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
366 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
367 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
368 | ||
369 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
370 | ||
371 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
372 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
373 | ||
d9494cb4 PZ |
374 | static int perf_sample_allowed_ns __read_mostly = |
375 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 376 | |
18ab2cd3 | 377 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
378 | { |
379 | u64 tmp = perf_sample_period_ns; | |
380 | ||
381 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
382 | tmp = div_u64(tmp, 100); |
383 | if (!tmp) | |
384 | tmp = 1; | |
385 | ||
386 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 387 | } |
163ec435 | 388 | |
9e630205 SE |
389 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
390 | ||
163ec435 PZ |
391 | int perf_proc_update_handler(struct ctl_table *table, int write, |
392 | void __user *buffer, size_t *lenp, | |
393 | loff_t *ppos) | |
394 | { | |
723478c8 | 395 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
163ec435 PZ |
396 | |
397 | if (ret || !write) | |
398 | return ret; | |
399 | ||
ab7fdefb KL |
400 | /* |
401 | * If throttling is disabled don't allow the write: | |
402 | */ | |
403 | if (sysctl_perf_cpu_time_max_percent == 100 || | |
404 | sysctl_perf_cpu_time_max_percent == 0) | |
405 | return -EINVAL; | |
406 | ||
163ec435 | 407 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
14c63f17 DH |
408 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
409 | update_perf_cpu_limits(); | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
415 | ||
416 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
417 | void __user *buffer, size_t *lenp, | |
418 | loff_t *ppos) | |
419 | { | |
420 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
421 | ||
422 | if (ret || !write) | |
423 | return ret; | |
424 | ||
b303e7c1 PZ |
425 | if (sysctl_perf_cpu_time_max_percent == 100 || |
426 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
427 | printk(KERN_WARNING |
428 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
429 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
430 | } else { | |
431 | update_perf_cpu_limits(); | |
432 | } | |
163ec435 PZ |
433 | |
434 | return 0; | |
435 | } | |
1ccd1549 | 436 | |
14c63f17 DH |
437 | /* |
438 | * perf samples are done in some very critical code paths (NMIs). | |
439 | * If they take too much CPU time, the system can lock up and not | |
440 | * get any real work done. This will drop the sample rate when | |
441 | * we detect that events are taking too long. | |
442 | */ | |
443 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 444 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 445 | |
91a612ee PZ |
446 | static u64 __report_avg; |
447 | static u64 __report_allowed; | |
448 | ||
6a02ad66 | 449 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 450 | { |
6a02ad66 | 451 | printk_ratelimited(KERN_WARNING |
91a612ee PZ |
452 | "perf: interrupt took too long (%lld > %lld), lowering " |
453 | "kernel.perf_event_max_sample_rate to %d\n", | |
454 | __report_avg, __report_allowed, | |
455 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
456 | } |
457 | ||
458 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
459 | ||
460 | void perf_sample_event_took(u64 sample_len_ns) | |
461 | { | |
91a612ee PZ |
462 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
463 | u64 running_len; | |
464 | u64 avg_len; | |
465 | u32 max; | |
14c63f17 | 466 | |
91a612ee | 467 | if (max_len == 0) |
14c63f17 DH |
468 | return; |
469 | ||
91a612ee PZ |
470 | /* Decay the counter by 1 average sample. */ |
471 | running_len = __this_cpu_read(running_sample_length); | |
472 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
473 | running_len += sample_len_ns; | |
474 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
475 | |
476 | /* | |
91a612ee PZ |
477 | * Note: this will be biased artifically low until we have |
478 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
479 | * from having to maintain a count. |
480 | */ | |
91a612ee PZ |
481 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
482 | if (avg_len <= max_len) | |
14c63f17 DH |
483 | return; |
484 | ||
91a612ee PZ |
485 | __report_avg = avg_len; |
486 | __report_allowed = max_len; | |
14c63f17 | 487 | |
91a612ee PZ |
488 | /* |
489 | * Compute a throttle threshold 25% below the current duration. | |
490 | */ | |
491 | avg_len += avg_len / 4; | |
492 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
493 | if (avg_len < max) | |
494 | max /= (u32)avg_len; | |
495 | else | |
496 | max = 1; | |
14c63f17 | 497 | |
91a612ee PZ |
498 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
499 | WRITE_ONCE(max_samples_per_tick, max); | |
500 | ||
501 | sysctl_perf_event_sample_rate = max * HZ; | |
502 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 503 | |
cd578abb | 504 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 505 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 506 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 507 | __report_avg, __report_allowed, |
cd578abb PZ |
508 | sysctl_perf_event_sample_rate); |
509 | } | |
14c63f17 DH |
510 | } |
511 | ||
cdd6c482 | 512 | static atomic64_t perf_event_id; |
a96bbc16 | 513 | |
0b3fcf17 SE |
514 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
515 | enum event_type_t event_type); | |
516 | ||
517 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
518 | enum event_type_t event_type, |
519 | struct task_struct *task); | |
520 | ||
521 | static void update_context_time(struct perf_event_context *ctx); | |
522 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 523 | |
cdd6c482 | 524 | void __weak perf_event_print_debug(void) { } |
0793a61d | 525 | |
84c79910 | 526 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 527 | { |
84c79910 | 528 | return "pmu"; |
0793a61d TG |
529 | } |
530 | ||
0b3fcf17 SE |
531 | static inline u64 perf_clock(void) |
532 | { | |
533 | return local_clock(); | |
534 | } | |
535 | ||
34f43927 PZ |
536 | static inline u64 perf_event_clock(struct perf_event *event) |
537 | { | |
538 | return event->clock(); | |
539 | } | |
540 | ||
e5d1367f SE |
541 | #ifdef CONFIG_CGROUP_PERF |
542 | ||
e5d1367f SE |
543 | static inline bool |
544 | perf_cgroup_match(struct perf_event *event) | |
545 | { | |
546 | struct perf_event_context *ctx = event->ctx; | |
547 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
548 | ||
ef824fa1 TH |
549 | /* @event doesn't care about cgroup */ |
550 | if (!event->cgrp) | |
551 | return true; | |
552 | ||
553 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
554 | if (!cpuctx->cgrp) | |
555 | return false; | |
556 | ||
557 | /* | |
558 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
559 | * also enabled for all its descendant cgroups. If @cpuctx's | |
560 | * cgroup is a descendant of @event's (the test covers identity | |
561 | * case), it's a match. | |
562 | */ | |
563 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
564 | event->cgrp->css.cgroup); | |
e5d1367f SE |
565 | } |
566 | ||
e5d1367f SE |
567 | static inline void perf_detach_cgroup(struct perf_event *event) |
568 | { | |
4e2ba650 | 569 | css_put(&event->cgrp->css); |
e5d1367f SE |
570 | event->cgrp = NULL; |
571 | } | |
572 | ||
573 | static inline int is_cgroup_event(struct perf_event *event) | |
574 | { | |
575 | return event->cgrp != NULL; | |
576 | } | |
577 | ||
578 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
579 | { | |
580 | struct perf_cgroup_info *t; | |
581 | ||
582 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
583 | return t->time; | |
584 | } | |
585 | ||
586 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
587 | { | |
588 | struct perf_cgroup_info *info; | |
589 | u64 now; | |
590 | ||
591 | now = perf_clock(); | |
592 | ||
593 | info = this_cpu_ptr(cgrp->info); | |
594 | ||
595 | info->time += now - info->timestamp; | |
596 | info->timestamp = now; | |
597 | } | |
598 | ||
599 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
600 | { | |
601 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
602 | if (cgrp_out) | |
603 | __update_cgrp_time(cgrp_out); | |
604 | } | |
605 | ||
606 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
607 | { | |
3f7cce3c SE |
608 | struct perf_cgroup *cgrp; |
609 | ||
e5d1367f | 610 | /* |
3f7cce3c SE |
611 | * ensure we access cgroup data only when needed and |
612 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 613 | */ |
3f7cce3c | 614 | if (!is_cgroup_event(event)) |
e5d1367f SE |
615 | return; |
616 | ||
614e4c4e | 617 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
618 | /* |
619 | * Do not update time when cgroup is not active | |
620 | */ | |
621 | if (cgrp == event->cgrp) | |
622 | __update_cgrp_time(event->cgrp); | |
e5d1367f SE |
623 | } |
624 | ||
625 | static inline void | |
3f7cce3c SE |
626 | perf_cgroup_set_timestamp(struct task_struct *task, |
627 | struct perf_event_context *ctx) | |
e5d1367f SE |
628 | { |
629 | struct perf_cgroup *cgrp; | |
630 | struct perf_cgroup_info *info; | |
631 | ||
3f7cce3c SE |
632 | /* |
633 | * ctx->lock held by caller | |
634 | * ensure we do not access cgroup data | |
635 | * unless we have the cgroup pinned (css_get) | |
636 | */ | |
637 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
638 | return; |
639 | ||
614e4c4e | 640 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 641 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 642 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
643 | } |
644 | ||
645 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ | |
646 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
647 | ||
648 | /* | |
649 | * reschedule events based on the cgroup constraint of task. | |
650 | * | |
651 | * mode SWOUT : schedule out everything | |
652 | * mode SWIN : schedule in based on cgroup for next | |
653 | */ | |
18ab2cd3 | 654 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
655 | { |
656 | struct perf_cpu_context *cpuctx; | |
657 | struct pmu *pmu; | |
658 | unsigned long flags; | |
659 | ||
660 | /* | |
661 | * disable interrupts to avoid geting nr_cgroup | |
662 | * changes via __perf_event_disable(). Also | |
663 | * avoids preemption. | |
664 | */ | |
665 | local_irq_save(flags); | |
666 | ||
667 | /* | |
668 | * we reschedule only in the presence of cgroup | |
669 | * constrained events. | |
670 | */ | |
e5d1367f SE |
671 | |
672 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
e5d1367f | 673 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95cf59ea PZ |
674 | if (cpuctx->unique_pmu != pmu) |
675 | continue; /* ensure we process each cpuctx once */ | |
e5d1367f | 676 | |
e5d1367f SE |
677 | /* |
678 | * perf_cgroup_events says at least one | |
679 | * context on this CPU has cgroup events. | |
680 | * | |
681 | * ctx->nr_cgroups reports the number of cgroup | |
682 | * events for a context. | |
683 | */ | |
684 | if (cpuctx->ctx.nr_cgroups > 0) { | |
facc4307 PZ |
685 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
686 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f SE |
687 | |
688 | if (mode & PERF_CGROUP_SWOUT) { | |
689 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
690 | /* | |
691 | * must not be done before ctxswout due | |
692 | * to event_filter_match() in event_sched_out() | |
693 | */ | |
694 | cpuctx->cgrp = NULL; | |
695 | } | |
696 | ||
697 | if (mode & PERF_CGROUP_SWIN) { | |
e566b76e | 698 | WARN_ON_ONCE(cpuctx->cgrp); |
95cf59ea PZ |
699 | /* |
700 | * set cgrp before ctxsw in to allow | |
701 | * event_filter_match() to not have to pass | |
702 | * task around | |
614e4c4e SE |
703 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
704 | * because cgorup events are only per-cpu | |
e5d1367f | 705 | */ |
614e4c4e | 706 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
e5d1367f SE |
707 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
708 | } | |
facc4307 PZ |
709 | perf_pmu_enable(cpuctx->ctx.pmu); |
710 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f | 711 | } |
e5d1367f SE |
712 | } |
713 | ||
e5d1367f SE |
714 | local_irq_restore(flags); |
715 | } | |
716 | ||
a8d757ef SE |
717 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
718 | struct task_struct *next) | |
e5d1367f | 719 | { |
a8d757ef SE |
720 | struct perf_cgroup *cgrp1; |
721 | struct perf_cgroup *cgrp2 = NULL; | |
722 | ||
ddaaf4e2 | 723 | rcu_read_lock(); |
a8d757ef SE |
724 | /* |
725 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
726 | * we do not need to pass the ctx here because we know |
727 | * we are holding the rcu lock | |
a8d757ef | 728 | */ |
614e4c4e | 729 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 730 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
731 | |
732 | /* | |
733 | * only schedule out current cgroup events if we know | |
734 | * that we are switching to a different cgroup. Otherwise, | |
735 | * do no touch the cgroup events. | |
736 | */ | |
737 | if (cgrp1 != cgrp2) | |
738 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
739 | |
740 | rcu_read_unlock(); | |
e5d1367f SE |
741 | } |
742 | ||
a8d757ef SE |
743 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
744 | struct task_struct *task) | |
e5d1367f | 745 | { |
a8d757ef SE |
746 | struct perf_cgroup *cgrp1; |
747 | struct perf_cgroup *cgrp2 = NULL; | |
748 | ||
ddaaf4e2 | 749 | rcu_read_lock(); |
a8d757ef SE |
750 | /* |
751 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
752 | * we do not need to pass the ctx here because we know |
753 | * we are holding the rcu lock | |
a8d757ef | 754 | */ |
614e4c4e | 755 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 756 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
757 | |
758 | /* | |
759 | * only need to schedule in cgroup events if we are changing | |
760 | * cgroup during ctxsw. Cgroup events were not scheduled | |
761 | * out of ctxsw out if that was not the case. | |
762 | */ | |
763 | if (cgrp1 != cgrp2) | |
764 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
765 | |
766 | rcu_read_unlock(); | |
e5d1367f SE |
767 | } |
768 | ||
769 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
770 | struct perf_event_attr *attr, | |
771 | struct perf_event *group_leader) | |
772 | { | |
773 | struct perf_cgroup *cgrp; | |
774 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
775 | struct fd f = fdget(fd); |
776 | int ret = 0; | |
e5d1367f | 777 | |
2903ff01 | 778 | if (!f.file) |
e5d1367f SE |
779 | return -EBADF; |
780 | ||
b583043e | 781 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 782 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
783 | if (IS_ERR(css)) { |
784 | ret = PTR_ERR(css); | |
785 | goto out; | |
786 | } | |
e5d1367f SE |
787 | |
788 | cgrp = container_of(css, struct perf_cgroup, css); | |
789 | event->cgrp = cgrp; | |
790 | ||
791 | /* | |
792 | * all events in a group must monitor | |
793 | * the same cgroup because a task belongs | |
794 | * to only one perf cgroup at a time | |
795 | */ | |
796 | if (group_leader && group_leader->cgrp != cgrp) { | |
797 | perf_detach_cgroup(event); | |
798 | ret = -EINVAL; | |
e5d1367f | 799 | } |
3db272c0 | 800 | out: |
2903ff01 | 801 | fdput(f); |
e5d1367f SE |
802 | return ret; |
803 | } | |
804 | ||
805 | static inline void | |
806 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
807 | { | |
808 | struct perf_cgroup_info *t; | |
809 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
810 | event->shadow_ctx_time = now - t->timestamp; | |
811 | } | |
812 | ||
813 | static inline void | |
814 | perf_cgroup_defer_enabled(struct perf_event *event) | |
815 | { | |
816 | /* | |
817 | * when the current task's perf cgroup does not match | |
818 | * the event's, we need to remember to call the | |
819 | * perf_mark_enable() function the first time a task with | |
820 | * a matching perf cgroup is scheduled in. | |
821 | */ | |
822 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) | |
823 | event->cgrp_defer_enabled = 1; | |
824 | } | |
825 | ||
826 | static inline void | |
827 | perf_cgroup_mark_enabled(struct perf_event *event, | |
828 | struct perf_event_context *ctx) | |
829 | { | |
830 | struct perf_event *sub; | |
831 | u64 tstamp = perf_event_time(event); | |
832 | ||
833 | if (!event->cgrp_defer_enabled) | |
834 | return; | |
835 | ||
836 | event->cgrp_defer_enabled = 0; | |
837 | ||
838 | event->tstamp_enabled = tstamp - event->total_time_enabled; | |
839 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
840 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { | |
841 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
842 | sub->cgrp_defer_enabled = 0; | |
843 | } | |
844 | } | |
845 | } | |
846 | #else /* !CONFIG_CGROUP_PERF */ | |
847 | ||
848 | static inline bool | |
849 | perf_cgroup_match(struct perf_event *event) | |
850 | { | |
851 | return true; | |
852 | } | |
853 | ||
854 | static inline void perf_detach_cgroup(struct perf_event *event) | |
855 | {} | |
856 | ||
857 | static inline int is_cgroup_event(struct perf_event *event) | |
858 | { | |
859 | return 0; | |
860 | } | |
861 | ||
862 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) | |
863 | { | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
868 | { | |
869 | } | |
870 | ||
871 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
872 | { | |
873 | } | |
874 | ||
a8d757ef SE |
875 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
876 | struct task_struct *next) | |
e5d1367f SE |
877 | { |
878 | } | |
879 | ||
a8d757ef SE |
880 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
881 | struct task_struct *task) | |
e5d1367f SE |
882 | { |
883 | } | |
884 | ||
885 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
886 | struct perf_event_attr *attr, | |
887 | struct perf_event *group_leader) | |
888 | { | |
889 | return -EINVAL; | |
890 | } | |
891 | ||
892 | static inline void | |
3f7cce3c SE |
893 | perf_cgroup_set_timestamp(struct task_struct *task, |
894 | struct perf_event_context *ctx) | |
e5d1367f SE |
895 | { |
896 | } | |
897 | ||
898 | void | |
899 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
900 | { | |
901 | } | |
902 | ||
903 | static inline void | |
904 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
905 | { | |
906 | } | |
907 | ||
908 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
909 | { | |
910 | return 0; | |
911 | } | |
912 | ||
913 | static inline void | |
914 | perf_cgroup_defer_enabled(struct perf_event *event) | |
915 | { | |
916 | } | |
917 | ||
918 | static inline void | |
919 | perf_cgroup_mark_enabled(struct perf_event *event, | |
920 | struct perf_event_context *ctx) | |
921 | { | |
922 | } | |
923 | #endif | |
924 | ||
9e630205 SE |
925 | /* |
926 | * set default to be dependent on timer tick just | |
927 | * like original code | |
928 | */ | |
929 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
930 | /* | |
931 | * function must be called with interrupts disbled | |
932 | */ | |
272325c4 | 933 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
934 | { |
935 | struct perf_cpu_context *cpuctx; | |
9e630205 SE |
936 | int rotations = 0; |
937 | ||
938 | WARN_ON(!irqs_disabled()); | |
939 | ||
940 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
941 | rotations = perf_rotate_context(cpuctx); |
942 | ||
4cfafd30 PZ |
943 | raw_spin_lock(&cpuctx->hrtimer_lock); |
944 | if (rotations) | |
9e630205 | 945 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
946 | else |
947 | cpuctx->hrtimer_active = 0; | |
948 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 949 | |
4cfafd30 | 950 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
951 | } |
952 | ||
272325c4 | 953 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 954 | { |
272325c4 | 955 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 956 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 957 | u64 interval; |
9e630205 SE |
958 | |
959 | /* no multiplexing needed for SW PMU */ | |
960 | if (pmu->task_ctx_nr == perf_sw_context) | |
961 | return; | |
962 | ||
62b85639 SE |
963 | /* |
964 | * check default is sane, if not set then force to | |
965 | * default interval (1/tick) | |
966 | */ | |
272325c4 PZ |
967 | interval = pmu->hrtimer_interval_ms; |
968 | if (interval < 1) | |
969 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 970 | |
272325c4 | 971 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 972 | |
4cfafd30 PZ |
973 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
974 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 975 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
976 | } |
977 | ||
272325c4 | 978 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 979 | { |
272325c4 | 980 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 981 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 982 | unsigned long flags; |
9e630205 SE |
983 | |
984 | /* not for SW PMU */ | |
985 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 986 | return 0; |
9e630205 | 987 | |
4cfafd30 PZ |
988 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
989 | if (!cpuctx->hrtimer_active) { | |
990 | cpuctx->hrtimer_active = 1; | |
991 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
992 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
993 | } | |
994 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 995 | |
272325c4 | 996 | return 0; |
9e630205 SE |
997 | } |
998 | ||
33696fc0 | 999 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1000 | { |
33696fc0 PZ |
1001 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1002 | if (!(*count)++) | |
1003 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1004 | } |
9e35ad38 | 1005 | |
33696fc0 | 1006 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1007 | { |
33696fc0 PZ |
1008 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1009 | if (!--(*count)) | |
1010 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1011 | } |
9e35ad38 | 1012 | |
2fde4f94 | 1013 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1014 | |
1015 | /* | |
2fde4f94 MR |
1016 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1017 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1018 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1019 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1020 | */ |
2fde4f94 | 1021 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1022 | { |
2fde4f94 | 1023 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1024 | |
e9d2b064 | 1025 | WARN_ON(!irqs_disabled()); |
b5ab4cd5 | 1026 | |
2fde4f94 MR |
1027 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1028 | ||
1029 | list_add(&ctx->active_ctx_list, head); | |
1030 | } | |
1031 | ||
1032 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1033 | { | |
1034 | WARN_ON(!irqs_disabled()); | |
1035 | ||
1036 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1037 | ||
1038 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1039 | } |
9e35ad38 | 1040 | |
cdd6c482 | 1041 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1042 | { |
e5289d4a | 1043 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1044 | } |
1045 | ||
4af57ef2 YZ |
1046 | static void free_ctx(struct rcu_head *head) |
1047 | { | |
1048 | struct perf_event_context *ctx; | |
1049 | ||
1050 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1051 | kfree(ctx->task_ctx_data); | |
1052 | kfree(ctx); | |
1053 | } | |
1054 | ||
cdd6c482 | 1055 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1056 | { |
564c2b21 PM |
1057 | if (atomic_dec_and_test(&ctx->refcount)) { |
1058 | if (ctx->parent_ctx) | |
1059 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1060 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1061 | put_task_struct(ctx->task); |
4af57ef2 | 1062 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1063 | } |
a63eaf34 PM |
1064 | } |
1065 | ||
f63a8daa PZ |
1066 | /* |
1067 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1068 | * perf_pmu_migrate_context() we need some magic. | |
1069 | * | |
1070 | * Those places that change perf_event::ctx will hold both | |
1071 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1072 | * | |
8b10c5e2 PZ |
1073 | * Lock ordering is by mutex address. There are two other sites where |
1074 | * perf_event_context::mutex nests and those are: | |
1075 | * | |
1076 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1077 | * perf_event_exit_event() |
1078 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1079 | * |
1080 | * - perf_event_init_context() [ parent, 0 ] | |
1081 | * inherit_task_group() | |
1082 | * inherit_group() | |
1083 | * inherit_event() | |
1084 | * perf_event_alloc() | |
1085 | * perf_init_event() | |
1086 | * perf_try_init_event() [ child , 1 ] | |
1087 | * | |
1088 | * While it appears there is an obvious deadlock here -- the parent and child | |
1089 | * nesting levels are inverted between the two. This is in fact safe because | |
1090 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1091 | * spawning task cannot (yet) exit. | |
1092 | * | |
1093 | * But remember that that these are parent<->child context relations, and | |
1094 | * migration does not affect children, therefore these two orderings should not | |
1095 | * interact. | |
f63a8daa PZ |
1096 | * |
1097 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1098 | * because the sys_perf_event_open() case will install a new event and break | |
1099 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1100 | * concerned with cpuctx and that doesn't have children. | |
1101 | * | |
1102 | * The places that change perf_event::ctx will issue: | |
1103 | * | |
1104 | * perf_remove_from_context(); | |
1105 | * synchronize_rcu(); | |
1106 | * perf_install_in_context(); | |
1107 | * | |
1108 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1109 | * quiesce the event, after which we can install it in the new location. This | |
1110 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1111 | * while in transit. Therefore all such accessors should also acquire | |
1112 | * perf_event_context::mutex to serialize against this. | |
1113 | * | |
1114 | * However; because event->ctx can change while we're waiting to acquire | |
1115 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1116 | * function. | |
1117 | * | |
1118 | * Lock order: | |
79c9ce57 | 1119 | * cred_guard_mutex |
f63a8daa PZ |
1120 | * task_struct::perf_event_mutex |
1121 | * perf_event_context::mutex | |
f63a8daa | 1122 | * perf_event::child_mutex; |
07c4a776 | 1123 | * perf_event_context::lock |
f63a8daa PZ |
1124 | * perf_event::mmap_mutex |
1125 | * mmap_sem | |
1126 | */ | |
a83fe28e PZ |
1127 | static struct perf_event_context * |
1128 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1129 | { |
1130 | struct perf_event_context *ctx; | |
1131 | ||
1132 | again: | |
1133 | rcu_read_lock(); | |
1134 | ctx = ACCESS_ONCE(event->ctx); | |
1135 | if (!atomic_inc_not_zero(&ctx->refcount)) { | |
1136 | rcu_read_unlock(); | |
1137 | goto again; | |
1138 | } | |
1139 | rcu_read_unlock(); | |
1140 | ||
a83fe28e | 1141 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1142 | if (event->ctx != ctx) { |
1143 | mutex_unlock(&ctx->mutex); | |
1144 | put_ctx(ctx); | |
1145 | goto again; | |
1146 | } | |
1147 | ||
1148 | return ctx; | |
1149 | } | |
1150 | ||
a83fe28e PZ |
1151 | static inline struct perf_event_context * |
1152 | perf_event_ctx_lock(struct perf_event *event) | |
1153 | { | |
1154 | return perf_event_ctx_lock_nested(event, 0); | |
1155 | } | |
1156 | ||
f63a8daa PZ |
1157 | static void perf_event_ctx_unlock(struct perf_event *event, |
1158 | struct perf_event_context *ctx) | |
1159 | { | |
1160 | mutex_unlock(&ctx->mutex); | |
1161 | put_ctx(ctx); | |
1162 | } | |
1163 | ||
211de6eb PZ |
1164 | /* |
1165 | * This must be done under the ctx->lock, such as to serialize against | |
1166 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1167 | * calling scheduler related locks and ctx->lock nests inside those. | |
1168 | */ | |
1169 | static __must_check struct perf_event_context * | |
1170 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1171 | { |
211de6eb PZ |
1172 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1173 | ||
1174 | lockdep_assert_held(&ctx->lock); | |
1175 | ||
1176 | if (parent_ctx) | |
71a851b4 | 1177 | ctx->parent_ctx = NULL; |
5a3126d4 | 1178 | ctx->generation++; |
211de6eb PZ |
1179 | |
1180 | return parent_ctx; | |
71a851b4 PZ |
1181 | } |
1182 | ||
6844c09d ACM |
1183 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
1184 | { | |
1185 | /* | |
1186 | * only top level events have the pid namespace they were created in | |
1187 | */ | |
1188 | if (event->parent) | |
1189 | event = event->parent; | |
1190 | ||
1191 | return task_tgid_nr_ns(p, event->ns); | |
1192 | } | |
1193 | ||
1194 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) | |
1195 | { | |
1196 | /* | |
1197 | * only top level events have the pid namespace they were created in | |
1198 | */ | |
1199 | if (event->parent) | |
1200 | event = event->parent; | |
1201 | ||
1202 | return task_pid_nr_ns(p, event->ns); | |
1203 | } | |
1204 | ||
7f453c24 | 1205 | /* |
cdd6c482 | 1206 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1207 | * to userspace. |
1208 | */ | |
cdd6c482 | 1209 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1210 | { |
cdd6c482 | 1211 | u64 id = event->id; |
7f453c24 | 1212 | |
cdd6c482 IM |
1213 | if (event->parent) |
1214 | id = event->parent->id; | |
7f453c24 PZ |
1215 | |
1216 | return id; | |
1217 | } | |
1218 | ||
25346b93 | 1219 | /* |
cdd6c482 | 1220 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1221 | * |
25346b93 PM |
1222 | * This has to cope with with the fact that until it is locked, |
1223 | * the context could get moved to another task. | |
1224 | */ | |
cdd6c482 | 1225 | static struct perf_event_context * |
8dc85d54 | 1226 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1227 | { |
cdd6c482 | 1228 | struct perf_event_context *ctx; |
25346b93 | 1229 | |
9ed6060d | 1230 | retry: |
058ebd0e PZ |
1231 | /* |
1232 | * One of the few rules of preemptible RCU is that one cannot do | |
1233 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1234 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1235 | * rcu_read_unlock_special(). |
1236 | * | |
1237 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1238 | * side critical section has interrupts disabled. |
058ebd0e | 1239 | */ |
2fd59077 | 1240 | local_irq_save(*flags); |
058ebd0e | 1241 | rcu_read_lock(); |
8dc85d54 | 1242 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1243 | if (ctx) { |
1244 | /* | |
1245 | * If this context is a clone of another, it might | |
1246 | * get swapped for another underneath us by | |
cdd6c482 | 1247 | * perf_event_task_sched_out, though the |
25346b93 PM |
1248 | * rcu_read_lock() protects us from any context |
1249 | * getting freed. Lock the context and check if it | |
1250 | * got swapped before we could get the lock, and retry | |
1251 | * if so. If we locked the right context, then it | |
1252 | * can't get swapped on us any more. | |
1253 | */ | |
2fd59077 | 1254 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1255 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1256 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1257 | rcu_read_unlock(); |
2fd59077 | 1258 | local_irq_restore(*flags); |
25346b93 PM |
1259 | goto retry; |
1260 | } | |
b49a9e7e | 1261 | |
63b6da39 PZ |
1262 | if (ctx->task == TASK_TOMBSTONE || |
1263 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1264 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1265 | ctx = NULL; |
828b6f0e PZ |
1266 | } else { |
1267 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1268 | } |
25346b93 PM |
1269 | } |
1270 | rcu_read_unlock(); | |
2fd59077 PM |
1271 | if (!ctx) |
1272 | local_irq_restore(*flags); | |
25346b93 PM |
1273 | return ctx; |
1274 | } | |
1275 | ||
1276 | /* | |
1277 | * Get the context for a task and increment its pin_count so it | |
1278 | * can't get swapped to another task. This also increments its | |
1279 | * reference count so that the context can't get freed. | |
1280 | */ | |
8dc85d54 PZ |
1281 | static struct perf_event_context * |
1282 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1283 | { |
cdd6c482 | 1284 | struct perf_event_context *ctx; |
25346b93 PM |
1285 | unsigned long flags; |
1286 | ||
8dc85d54 | 1287 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1288 | if (ctx) { |
1289 | ++ctx->pin_count; | |
e625cce1 | 1290 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1291 | } |
1292 | return ctx; | |
1293 | } | |
1294 | ||
cdd6c482 | 1295 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1296 | { |
1297 | unsigned long flags; | |
1298 | ||
e625cce1 | 1299 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1300 | --ctx->pin_count; |
e625cce1 | 1301 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1302 | } |
1303 | ||
f67218c3 PZ |
1304 | /* |
1305 | * Update the record of the current time in a context. | |
1306 | */ | |
1307 | static void update_context_time(struct perf_event_context *ctx) | |
1308 | { | |
1309 | u64 now = perf_clock(); | |
1310 | ||
1311 | ctx->time += now - ctx->timestamp; | |
1312 | ctx->timestamp = now; | |
1313 | } | |
1314 | ||
4158755d SE |
1315 | static u64 perf_event_time(struct perf_event *event) |
1316 | { | |
1317 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1318 | |
1319 | if (is_cgroup_event(event)) | |
1320 | return perf_cgroup_event_time(event); | |
1321 | ||
4158755d SE |
1322 | return ctx ? ctx->time : 0; |
1323 | } | |
1324 | ||
f67218c3 PZ |
1325 | /* |
1326 | * Update the total_time_enabled and total_time_running fields for a event. | |
1327 | */ | |
1328 | static void update_event_times(struct perf_event *event) | |
1329 | { | |
1330 | struct perf_event_context *ctx = event->ctx; | |
1331 | u64 run_end; | |
1332 | ||
3cbaa590 PZ |
1333 | lockdep_assert_held(&ctx->lock); |
1334 | ||
f67218c3 PZ |
1335 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
1336 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) | |
1337 | return; | |
3cbaa590 | 1338 | |
e5d1367f SE |
1339 | /* |
1340 | * in cgroup mode, time_enabled represents | |
1341 | * the time the event was enabled AND active | |
1342 | * tasks were in the monitored cgroup. This is | |
1343 | * independent of the activity of the context as | |
1344 | * there may be a mix of cgroup and non-cgroup events. | |
1345 | * | |
1346 | * That is why we treat cgroup events differently | |
1347 | * here. | |
1348 | */ | |
1349 | if (is_cgroup_event(event)) | |
46cd6a7f | 1350 | run_end = perf_cgroup_event_time(event); |
e5d1367f SE |
1351 | else if (ctx->is_active) |
1352 | run_end = ctx->time; | |
acd1d7c1 PZ |
1353 | else |
1354 | run_end = event->tstamp_stopped; | |
1355 | ||
1356 | event->total_time_enabled = run_end - event->tstamp_enabled; | |
f67218c3 PZ |
1357 | |
1358 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
1359 | run_end = event->tstamp_stopped; | |
1360 | else | |
4158755d | 1361 | run_end = perf_event_time(event); |
f67218c3 PZ |
1362 | |
1363 | event->total_time_running = run_end - event->tstamp_running; | |
e5d1367f | 1364 | |
f67218c3 PZ |
1365 | } |
1366 | ||
96c21a46 PZ |
1367 | /* |
1368 | * Update total_time_enabled and total_time_running for all events in a group. | |
1369 | */ | |
1370 | static void update_group_times(struct perf_event *leader) | |
1371 | { | |
1372 | struct perf_event *event; | |
1373 | ||
1374 | update_event_times(leader); | |
1375 | list_for_each_entry(event, &leader->sibling_list, group_entry) | |
1376 | update_event_times(event); | |
1377 | } | |
1378 | ||
889ff015 FW |
1379 | static struct list_head * |
1380 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) | |
1381 | { | |
1382 | if (event->attr.pinned) | |
1383 | return &ctx->pinned_groups; | |
1384 | else | |
1385 | return &ctx->flexible_groups; | |
1386 | } | |
1387 | ||
fccc714b | 1388 | /* |
cdd6c482 | 1389 | * Add a event from the lists for its context. |
fccc714b PZ |
1390 | * Must be called with ctx->mutex and ctx->lock held. |
1391 | */ | |
04289bb9 | 1392 | static void |
cdd6c482 | 1393 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1394 | { |
c994d613 PZ |
1395 | lockdep_assert_held(&ctx->lock); |
1396 | ||
8a49542c PZ |
1397 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1398 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 IM |
1399 | |
1400 | /* | |
8a49542c PZ |
1401 | * If we're a stand alone event or group leader, we go to the context |
1402 | * list, group events are kept attached to the group so that | |
1403 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1404 | */ |
8a49542c | 1405 | if (event->group_leader == event) { |
889ff015 FW |
1406 | struct list_head *list; |
1407 | ||
d6f962b5 FW |
1408 | if (is_software_event(event)) |
1409 | event->group_flags |= PERF_GROUP_SOFTWARE; | |
1410 | ||
889ff015 FW |
1411 | list = ctx_group_list(event, ctx); |
1412 | list_add_tail(&event->group_entry, list); | |
5c148194 | 1413 | } |
592903cd | 1414 | |
08309379 | 1415 | if (is_cgroup_event(event)) |
e5d1367f | 1416 | ctx->nr_cgroups++; |
e5d1367f | 1417 | |
cdd6c482 IM |
1418 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1419 | ctx->nr_events++; | |
1420 | if (event->attr.inherit_stat) | |
bfbd3381 | 1421 | ctx->nr_stat++; |
5a3126d4 PZ |
1422 | |
1423 | ctx->generation++; | |
04289bb9 IM |
1424 | } |
1425 | ||
0231bb53 JO |
1426 | /* |
1427 | * Initialize event state based on the perf_event_attr::disabled. | |
1428 | */ | |
1429 | static inline void perf_event__state_init(struct perf_event *event) | |
1430 | { | |
1431 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1432 | PERF_EVENT_STATE_INACTIVE; | |
1433 | } | |
1434 | ||
a723968c | 1435 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1436 | { |
1437 | int entry = sizeof(u64); /* value */ | |
1438 | int size = 0; | |
1439 | int nr = 1; | |
1440 | ||
1441 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1442 | size += sizeof(u64); | |
1443 | ||
1444 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1445 | size += sizeof(u64); | |
1446 | ||
1447 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1448 | entry += sizeof(u64); | |
1449 | ||
1450 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1451 | nr += nr_siblings; |
c320c7b7 ACM |
1452 | size += sizeof(u64); |
1453 | } | |
1454 | ||
1455 | size += entry * nr; | |
1456 | event->read_size = size; | |
1457 | } | |
1458 | ||
a723968c | 1459 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1460 | { |
1461 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1462 | u16 size = 0; |
1463 | ||
c320c7b7 ACM |
1464 | if (sample_type & PERF_SAMPLE_IP) |
1465 | size += sizeof(data->ip); | |
1466 | ||
6844c09d ACM |
1467 | if (sample_type & PERF_SAMPLE_ADDR) |
1468 | size += sizeof(data->addr); | |
1469 | ||
1470 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1471 | size += sizeof(data->period); | |
1472 | ||
c3feedf2 AK |
1473 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1474 | size += sizeof(data->weight); | |
1475 | ||
6844c09d ACM |
1476 | if (sample_type & PERF_SAMPLE_READ) |
1477 | size += event->read_size; | |
1478 | ||
d6be9ad6 SE |
1479 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1480 | size += sizeof(data->data_src.val); | |
1481 | ||
fdfbbd07 AK |
1482 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1483 | size += sizeof(data->txn); | |
1484 | ||
6844c09d ACM |
1485 | event->header_size = size; |
1486 | } | |
1487 | ||
a723968c PZ |
1488 | /* |
1489 | * Called at perf_event creation and when events are attached/detached from a | |
1490 | * group. | |
1491 | */ | |
1492 | static void perf_event__header_size(struct perf_event *event) | |
1493 | { | |
1494 | __perf_event_read_size(event, | |
1495 | event->group_leader->nr_siblings); | |
1496 | __perf_event_header_size(event, event->attr.sample_type); | |
1497 | } | |
1498 | ||
6844c09d ACM |
1499 | static void perf_event__id_header_size(struct perf_event *event) |
1500 | { | |
1501 | struct perf_sample_data *data; | |
1502 | u64 sample_type = event->attr.sample_type; | |
1503 | u16 size = 0; | |
1504 | ||
c320c7b7 ACM |
1505 | if (sample_type & PERF_SAMPLE_TID) |
1506 | size += sizeof(data->tid_entry); | |
1507 | ||
1508 | if (sample_type & PERF_SAMPLE_TIME) | |
1509 | size += sizeof(data->time); | |
1510 | ||
ff3d527c AH |
1511 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1512 | size += sizeof(data->id); | |
1513 | ||
c320c7b7 ACM |
1514 | if (sample_type & PERF_SAMPLE_ID) |
1515 | size += sizeof(data->id); | |
1516 | ||
1517 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1518 | size += sizeof(data->stream_id); | |
1519 | ||
1520 | if (sample_type & PERF_SAMPLE_CPU) | |
1521 | size += sizeof(data->cpu_entry); | |
1522 | ||
6844c09d | 1523 | event->id_header_size = size; |
c320c7b7 ACM |
1524 | } |
1525 | ||
a723968c PZ |
1526 | static bool perf_event_validate_size(struct perf_event *event) |
1527 | { | |
1528 | /* | |
1529 | * The values computed here will be over-written when we actually | |
1530 | * attach the event. | |
1531 | */ | |
1532 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1533 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1534 | perf_event__id_header_size(event); | |
1535 | ||
1536 | /* | |
1537 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1538 | * Conservative limit to allow for callchains and other variable fields. | |
1539 | */ | |
1540 | if (event->read_size + event->header_size + | |
1541 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1542 | return false; | |
1543 | ||
1544 | return true; | |
1545 | } | |
1546 | ||
8a49542c PZ |
1547 | static void perf_group_attach(struct perf_event *event) |
1548 | { | |
c320c7b7 | 1549 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1550 | |
74c3337c PZ |
1551 | /* |
1552 | * We can have double attach due to group movement in perf_event_open. | |
1553 | */ | |
1554 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1555 | return; | |
1556 | ||
8a49542c PZ |
1557 | event->attach_state |= PERF_ATTACH_GROUP; |
1558 | ||
1559 | if (group_leader == event) | |
1560 | return; | |
1561 | ||
652884fe PZ |
1562 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1563 | ||
8a49542c PZ |
1564 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
1565 | !is_software_event(event)) | |
1566 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; | |
1567 | ||
1568 | list_add_tail(&event->group_entry, &group_leader->sibling_list); | |
1569 | group_leader->nr_siblings++; | |
c320c7b7 ACM |
1570 | |
1571 | perf_event__header_size(group_leader); | |
1572 | ||
1573 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) | |
1574 | perf_event__header_size(pos); | |
8a49542c PZ |
1575 | } |
1576 | ||
a63eaf34 | 1577 | /* |
cdd6c482 | 1578 | * Remove a event from the lists for its context. |
fccc714b | 1579 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1580 | */ |
04289bb9 | 1581 | static void |
cdd6c482 | 1582 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1583 | { |
68cacd29 | 1584 | struct perf_cpu_context *cpuctx; |
652884fe PZ |
1585 | |
1586 | WARN_ON_ONCE(event->ctx != ctx); | |
1587 | lockdep_assert_held(&ctx->lock); | |
1588 | ||
8a49542c PZ |
1589 | /* |
1590 | * We can have double detach due to exit/hot-unplug + close. | |
1591 | */ | |
1592 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1593 | return; |
8a49542c PZ |
1594 | |
1595 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1596 | ||
68cacd29 | 1597 | if (is_cgroup_event(event)) { |
e5d1367f | 1598 | ctx->nr_cgroups--; |
70a01657 PZ |
1599 | /* |
1600 | * Because cgroup events are always per-cpu events, this will | |
1601 | * always be called from the right CPU. | |
1602 | */ | |
68cacd29 SE |
1603 | cpuctx = __get_cpu_context(ctx); |
1604 | /* | |
70a01657 PZ |
1605 | * If there are no more cgroup events then clear cgrp to avoid |
1606 | * stale pointer in update_cgrp_time_from_cpuctx(). | |
68cacd29 SE |
1607 | */ |
1608 | if (!ctx->nr_cgroups) | |
1609 | cpuctx->cgrp = NULL; | |
1610 | } | |
e5d1367f | 1611 | |
cdd6c482 IM |
1612 | ctx->nr_events--; |
1613 | if (event->attr.inherit_stat) | |
bfbd3381 | 1614 | ctx->nr_stat--; |
8bc20959 | 1615 | |
cdd6c482 | 1616 | list_del_rcu(&event->event_entry); |
04289bb9 | 1617 | |
8a49542c PZ |
1618 | if (event->group_leader == event) |
1619 | list_del_init(&event->group_entry); | |
5c148194 | 1620 | |
96c21a46 | 1621 | update_group_times(event); |
b2e74a26 SE |
1622 | |
1623 | /* | |
1624 | * If event was in error state, then keep it | |
1625 | * that way, otherwise bogus counts will be | |
1626 | * returned on read(). The only way to get out | |
1627 | * of error state is by explicit re-enabling | |
1628 | * of the event | |
1629 | */ | |
1630 | if (event->state > PERF_EVENT_STATE_OFF) | |
1631 | event->state = PERF_EVENT_STATE_OFF; | |
5a3126d4 PZ |
1632 | |
1633 | ctx->generation++; | |
050735b0 PZ |
1634 | } |
1635 | ||
8a49542c | 1636 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1637 | { |
1638 | struct perf_event *sibling, *tmp; | |
8a49542c PZ |
1639 | struct list_head *list = NULL; |
1640 | ||
1641 | /* | |
1642 | * We can have double detach due to exit/hot-unplug + close. | |
1643 | */ | |
1644 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1645 | return; | |
1646 | ||
1647 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1648 | ||
1649 | /* | |
1650 | * If this is a sibling, remove it from its group. | |
1651 | */ | |
1652 | if (event->group_leader != event) { | |
1653 | list_del_init(&event->group_entry); | |
1654 | event->group_leader->nr_siblings--; | |
c320c7b7 | 1655 | goto out; |
8a49542c PZ |
1656 | } |
1657 | ||
1658 | if (!list_empty(&event->group_entry)) | |
1659 | list = &event->group_entry; | |
2e2af50b | 1660 | |
04289bb9 | 1661 | /* |
cdd6c482 IM |
1662 | * If this was a group event with sibling events then |
1663 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1664 | * to whatever list we are on. |
04289bb9 | 1665 | */ |
cdd6c482 | 1666 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
8a49542c PZ |
1667 | if (list) |
1668 | list_move_tail(&sibling->group_entry, list); | |
04289bb9 | 1669 | sibling->group_leader = sibling; |
d6f962b5 FW |
1670 | |
1671 | /* Inherit group flags from the previous leader */ | |
1672 | sibling->group_flags = event->group_flags; | |
652884fe PZ |
1673 | |
1674 | WARN_ON_ONCE(sibling->ctx != event->ctx); | |
04289bb9 | 1675 | } |
c320c7b7 ACM |
1676 | |
1677 | out: | |
1678 | perf_event__header_size(event->group_leader); | |
1679 | ||
1680 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) | |
1681 | perf_event__header_size(tmp); | |
04289bb9 IM |
1682 | } |
1683 | ||
fadfe7be JO |
1684 | static bool is_orphaned_event(struct perf_event *event) |
1685 | { | |
a69b0ca4 | 1686 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1687 | } |
1688 | ||
66eb579e MR |
1689 | static inline int pmu_filter_match(struct perf_event *event) |
1690 | { | |
1691 | struct pmu *pmu = event->pmu; | |
1692 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1693 | } | |
1694 | ||
fa66f07a SE |
1695 | static inline int |
1696 | event_filter_match(struct perf_event *event) | |
1697 | { | |
e5d1367f | 1698 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
66eb579e | 1699 | && perf_cgroup_match(event) && pmu_filter_match(event); |
fa66f07a SE |
1700 | } |
1701 | ||
9ffcfa6f SE |
1702 | static void |
1703 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1704 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1705 | struct perf_event_context *ctx) |
3b6f9e5c | 1706 | { |
4158755d | 1707 | u64 tstamp = perf_event_time(event); |
fa66f07a | 1708 | u64 delta; |
652884fe PZ |
1709 | |
1710 | WARN_ON_ONCE(event->ctx != ctx); | |
1711 | lockdep_assert_held(&ctx->lock); | |
1712 | ||
fa66f07a SE |
1713 | /* |
1714 | * An event which could not be activated because of | |
1715 | * filter mismatch still needs to have its timings | |
1716 | * maintained, otherwise bogus information is return | |
1717 | * via read() for time_enabled, time_running: | |
1718 | */ | |
1719 | if (event->state == PERF_EVENT_STATE_INACTIVE | |
1720 | && !event_filter_match(event)) { | |
e5d1367f | 1721 | delta = tstamp - event->tstamp_stopped; |
fa66f07a | 1722 | event->tstamp_running += delta; |
4158755d | 1723 | event->tstamp_stopped = tstamp; |
fa66f07a SE |
1724 | } |
1725 | ||
cdd6c482 | 1726 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1727 | return; |
3b6f9e5c | 1728 | |
44377277 AS |
1729 | perf_pmu_disable(event->pmu); |
1730 | ||
28a967c3 PZ |
1731 | event->tstamp_stopped = tstamp; |
1732 | event->pmu->del(event, 0); | |
1733 | event->oncpu = -1; | |
cdd6c482 IM |
1734 | event->state = PERF_EVENT_STATE_INACTIVE; |
1735 | if (event->pending_disable) { | |
1736 | event->pending_disable = 0; | |
1737 | event->state = PERF_EVENT_STATE_OFF; | |
970892a9 | 1738 | } |
3b6f9e5c | 1739 | |
cdd6c482 | 1740 | if (!is_software_event(event)) |
3b6f9e5c | 1741 | cpuctx->active_oncpu--; |
2fde4f94 MR |
1742 | if (!--ctx->nr_active) |
1743 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
1744 | if (event->attr.freq && event->attr.sample_freq) |
1745 | ctx->nr_freq--; | |
cdd6c482 | 1746 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 1747 | cpuctx->exclusive = 0; |
44377277 AS |
1748 | |
1749 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
1750 | } |
1751 | ||
d859e29f | 1752 | static void |
cdd6c482 | 1753 | group_sched_out(struct perf_event *group_event, |
d859e29f | 1754 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1755 | struct perf_event_context *ctx) |
d859e29f | 1756 | { |
cdd6c482 | 1757 | struct perf_event *event; |
fa66f07a | 1758 | int state = group_event->state; |
d859e29f | 1759 | |
cdd6c482 | 1760 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
1761 | |
1762 | /* | |
1763 | * Schedule out siblings (if any): | |
1764 | */ | |
cdd6c482 IM |
1765 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
1766 | event_sched_out(event, cpuctx, ctx); | |
d859e29f | 1767 | |
fa66f07a | 1768 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
d859e29f PM |
1769 | cpuctx->exclusive = 0; |
1770 | } | |
1771 | ||
45a0e07a | 1772 | #define DETACH_GROUP 0x01UL |
0017960f | 1773 | |
0793a61d | 1774 | /* |
cdd6c482 | 1775 | * Cross CPU call to remove a performance event |
0793a61d | 1776 | * |
cdd6c482 | 1777 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
1778 | * remove it from the context list. |
1779 | */ | |
fae3fde6 PZ |
1780 | static void |
1781 | __perf_remove_from_context(struct perf_event *event, | |
1782 | struct perf_cpu_context *cpuctx, | |
1783 | struct perf_event_context *ctx, | |
1784 | void *info) | |
0793a61d | 1785 | { |
45a0e07a | 1786 | unsigned long flags = (unsigned long)info; |
0793a61d | 1787 | |
cdd6c482 | 1788 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 1789 | if (flags & DETACH_GROUP) |
46ce0fe9 | 1790 | perf_group_detach(event); |
cdd6c482 | 1791 | list_del_event(event, ctx); |
39a43640 PZ |
1792 | |
1793 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 1794 | ctx->is_active = 0; |
39a43640 PZ |
1795 | if (ctx->task) { |
1796 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
1797 | cpuctx->task_ctx = NULL; | |
1798 | } | |
64ce3126 | 1799 | } |
0793a61d TG |
1800 | } |
1801 | ||
0793a61d | 1802 | /* |
cdd6c482 | 1803 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 1804 | * |
cdd6c482 IM |
1805 | * If event->ctx is a cloned context, callers must make sure that |
1806 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
1807 | * remains valid. This is OK when called from perf_release since |
1808 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 1809 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 1810 | * context has been detached from its task. |
0793a61d | 1811 | */ |
45a0e07a | 1812 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 1813 | { |
fae3fde6 | 1814 | lockdep_assert_held(&event->ctx->mutex); |
0793a61d | 1815 | |
45a0e07a | 1816 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
0793a61d TG |
1817 | } |
1818 | ||
d859e29f | 1819 | /* |
cdd6c482 | 1820 | * Cross CPU call to disable a performance event |
d859e29f | 1821 | */ |
fae3fde6 PZ |
1822 | static void __perf_event_disable(struct perf_event *event, |
1823 | struct perf_cpu_context *cpuctx, | |
1824 | struct perf_event_context *ctx, | |
1825 | void *info) | |
7b648018 | 1826 | { |
fae3fde6 PZ |
1827 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
1828 | return; | |
7b648018 | 1829 | |
fae3fde6 PZ |
1830 | update_context_time(ctx); |
1831 | update_cgrp_time_from_event(event); | |
1832 | update_group_times(event); | |
1833 | if (event == event->group_leader) | |
1834 | group_sched_out(event, cpuctx, ctx); | |
1835 | else | |
1836 | event_sched_out(event, cpuctx, ctx); | |
1837 | event->state = PERF_EVENT_STATE_OFF; | |
7b648018 PZ |
1838 | } |
1839 | ||
d859e29f | 1840 | /* |
cdd6c482 | 1841 | * Disable a event. |
c93f7669 | 1842 | * |
cdd6c482 IM |
1843 | * If event->ctx is a cloned context, callers must make sure that |
1844 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 1845 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
1846 | * perf_event_for_each_child or perf_event_for_each because they |
1847 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
1848 | * goes to exit will block in perf_event_exit_event(). |
1849 | * | |
cdd6c482 | 1850 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 1851 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 1852 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 1853 | */ |
f63a8daa | 1854 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 1855 | { |
cdd6c482 | 1856 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 1857 | |
e625cce1 | 1858 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 1859 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 1860 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1861 | return; |
53cfbf59 | 1862 | } |
e625cce1 | 1863 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1864 | |
fae3fde6 PZ |
1865 | event_function_call(event, __perf_event_disable, NULL); |
1866 | } | |
1867 | ||
1868 | void perf_event_disable_local(struct perf_event *event) | |
1869 | { | |
1870 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 1871 | } |
f63a8daa PZ |
1872 | |
1873 | /* | |
1874 | * Strictly speaking kernel users cannot create groups and therefore this | |
1875 | * interface does not need the perf_event_ctx_lock() magic. | |
1876 | */ | |
1877 | void perf_event_disable(struct perf_event *event) | |
1878 | { | |
1879 | struct perf_event_context *ctx; | |
1880 | ||
1881 | ctx = perf_event_ctx_lock(event); | |
1882 | _perf_event_disable(event); | |
1883 | perf_event_ctx_unlock(event, ctx); | |
1884 | } | |
dcfce4a0 | 1885 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 1886 | |
e5d1367f SE |
1887 | static void perf_set_shadow_time(struct perf_event *event, |
1888 | struct perf_event_context *ctx, | |
1889 | u64 tstamp) | |
1890 | { | |
1891 | /* | |
1892 | * use the correct time source for the time snapshot | |
1893 | * | |
1894 | * We could get by without this by leveraging the | |
1895 | * fact that to get to this function, the caller | |
1896 | * has most likely already called update_context_time() | |
1897 | * and update_cgrp_time_xx() and thus both timestamp | |
1898 | * are identical (or very close). Given that tstamp is, | |
1899 | * already adjusted for cgroup, we could say that: | |
1900 | * tstamp - ctx->timestamp | |
1901 | * is equivalent to | |
1902 | * tstamp - cgrp->timestamp. | |
1903 | * | |
1904 | * Then, in perf_output_read(), the calculation would | |
1905 | * work with no changes because: | |
1906 | * - event is guaranteed scheduled in | |
1907 | * - no scheduled out in between | |
1908 | * - thus the timestamp would be the same | |
1909 | * | |
1910 | * But this is a bit hairy. | |
1911 | * | |
1912 | * So instead, we have an explicit cgroup call to remain | |
1913 | * within the time time source all along. We believe it | |
1914 | * is cleaner and simpler to understand. | |
1915 | */ | |
1916 | if (is_cgroup_event(event)) | |
1917 | perf_cgroup_set_shadow_time(event, tstamp); | |
1918 | else | |
1919 | event->shadow_ctx_time = tstamp - ctx->timestamp; | |
1920 | } | |
1921 | ||
4fe757dd PZ |
1922 | #define MAX_INTERRUPTS (~0ULL) |
1923 | ||
1924 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 1925 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 1926 | |
235c7fc7 | 1927 | static int |
9ffcfa6f | 1928 | event_sched_in(struct perf_event *event, |
235c7fc7 | 1929 | struct perf_cpu_context *cpuctx, |
6e37738a | 1930 | struct perf_event_context *ctx) |
235c7fc7 | 1931 | { |
4158755d | 1932 | u64 tstamp = perf_event_time(event); |
44377277 | 1933 | int ret = 0; |
4158755d | 1934 | |
63342411 PZ |
1935 | lockdep_assert_held(&ctx->lock); |
1936 | ||
cdd6c482 | 1937 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
1938 | return 0; |
1939 | ||
95ff4ca2 AS |
1940 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
1941 | /* | |
1942 | * Order event::oncpu write to happen before the ACTIVE state | |
1943 | * is visible. | |
1944 | */ | |
1945 | smp_wmb(); | |
1946 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); | |
4fe757dd PZ |
1947 | |
1948 | /* | |
1949 | * Unthrottle events, since we scheduled we might have missed several | |
1950 | * ticks already, also for a heavily scheduling task there is little | |
1951 | * guarantee it'll get a tick in a timely manner. | |
1952 | */ | |
1953 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
1954 | perf_log_throttle(event, 1); | |
1955 | event->hw.interrupts = 0; | |
1956 | } | |
1957 | ||
235c7fc7 IM |
1958 | /* |
1959 | * The new state must be visible before we turn it on in the hardware: | |
1960 | */ | |
1961 | smp_wmb(); | |
1962 | ||
44377277 AS |
1963 | perf_pmu_disable(event->pmu); |
1964 | ||
72f669c0 SL |
1965 | perf_set_shadow_time(event, ctx, tstamp); |
1966 | ||
ec0d7729 AS |
1967 | perf_log_itrace_start(event); |
1968 | ||
a4eaf7f1 | 1969 | if (event->pmu->add(event, PERF_EF_START)) { |
cdd6c482 IM |
1970 | event->state = PERF_EVENT_STATE_INACTIVE; |
1971 | event->oncpu = -1; | |
44377277 AS |
1972 | ret = -EAGAIN; |
1973 | goto out; | |
235c7fc7 IM |
1974 | } |
1975 | ||
00a2916f PZ |
1976 | event->tstamp_running += tstamp - event->tstamp_stopped; |
1977 | ||
cdd6c482 | 1978 | if (!is_software_event(event)) |
3b6f9e5c | 1979 | cpuctx->active_oncpu++; |
2fde4f94 MR |
1980 | if (!ctx->nr_active++) |
1981 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
1982 | if (event->attr.freq && event->attr.sample_freq) |
1983 | ctx->nr_freq++; | |
235c7fc7 | 1984 | |
cdd6c482 | 1985 | if (event->attr.exclusive) |
3b6f9e5c PM |
1986 | cpuctx->exclusive = 1; |
1987 | ||
44377277 AS |
1988 | out: |
1989 | perf_pmu_enable(event->pmu); | |
1990 | ||
1991 | return ret; | |
235c7fc7 IM |
1992 | } |
1993 | ||
6751b71e | 1994 | static int |
cdd6c482 | 1995 | group_sched_in(struct perf_event *group_event, |
6751b71e | 1996 | struct perf_cpu_context *cpuctx, |
6e37738a | 1997 | struct perf_event_context *ctx) |
6751b71e | 1998 | { |
6bde9b6c | 1999 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 2000 | struct pmu *pmu = ctx->pmu; |
d7842da4 SE |
2001 | u64 now = ctx->time; |
2002 | bool simulate = false; | |
6751b71e | 2003 | |
cdd6c482 | 2004 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2005 | return 0; |
2006 | ||
fbbe0701 | 2007 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2008 | |
9ffcfa6f | 2009 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2010 | pmu->cancel_txn(pmu); |
272325c4 | 2011 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2012 | return -EAGAIN; |
90151c35 | 2013 | } |
6751b71e PM |
2014 | |
2015 | /* | |
2016 | * Schedule in siblings as one group (if any): | |
2017 | */ | |
cdd6c482 | 2018 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
9ffcfa6f | 2019 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2020 | partial_group = event; |
6751b71e PM |
2021 | goto group_error; |
2022 | } | |
2023 | } | |
2024 | ||
9ffcfa6f | 2025 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2026 | return 0; |
9ffcfa6f | 2027 | |
6751b71e PM |
2028 | group_error: |
2029 | /* | |
2030 | * Groups can be scheduled in as one unit only, so undo any | |
2031 | * partial group before returning: | |
d7842da4 SE |
2032 | * The events up to the failed event are scheduled out normally, |
2033 | * tstamp_stopped will be updated. | |
2034 | * | |
2035 | * The failed events and the remaining siblings need to have | |
2036 | * their timings updated as if they had gone thru event_sched_in() | |
2037 | * and event_sched_out(). This is required to get consistent timings | |
2038 | * across the group. This also takes care of the case where the group | |
2039 | * could never be scheduled by ensuring tstamp_stopped is set to mark | |
2040 | * the time the event was actually stopped, such that time delta | |
2041 | * calculation in update_event_times() is correct. | |
6751b71e | 2042 | */ |
cdd6c482 IM |
2043 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
2044 | if (event == partial_group) | |
d7842da4 SE |
2045 | simulate = true; |
2046 | ||
2047 | if (simulate) { | |
2048 | event->tstamp_running += now - event->tstamp_stopped; | |
2049 | event->tstamp_stopped = now; | |
2050 | } else { | |
2051 | event_sched_out(event, cpuctx, ctx); | |
2052 | } | |
6751b71e | 2053 | } |
9ffcfa6f | 2054 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2055 | |
ad5133b7 | 2056 | pmu->cancel_txn(pmu); |
90151c35 | 2057 | |
272325c4 | 2058 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2059 | |
6751b71e PM |
2060 | return -EAGAIN; |
2061 | } | |
2062 | ||
3b6f9e5c | 2063 | /* |
cdd6c482 | 2064 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2065 | */ |
cdd6c482 | 2066 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2067 | struct perf_cpu_context *cpuctx, |
2068 | int can_add_hw) | |
2069 | { | |
2070 | /* | |
cdd6c482 | 2071 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2072 | */ |
d6f962b5 | 2073 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
3b6f9e5c PM |
2074 | return 1; |
2075 | /* | |
2076 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2077 | * events can go on. |
3b6f9e5c PM |
2078 | */ |
2079 | if (cpuctx->exclusive) | |
2080 | return 0; | |
2081 | /* | |
2082 | * If this group is exclusive and there are already | |
cdd6c482 | 2083 | * events on the CPU, it can't go on. |
3b6f9e5c | 2084 | */ |
cdd6c482 | 2085 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2086 | return 0; |
2087 | /* | |
2088 | * Otherwise, try to add it if all previous groups were able | |
2089 | * to go on. | |
2090 | */ | |
2091 | return can_add_hw; | |
2092 | } | |
2093 | ||
cdd6c482 IM |
2094 | static void add_event_to_ctx(struct perf_event *event, |
2095 | struct perf_event_context *ctx) | |
53cfbf59 | 2096 | { |
4158755d SE |
2097 | u64 tstamp = perf_event_time(event); |
2098 | ||
cdd6c482 | 2099 | list_add_event(event, ctx); |
8a49542c | 2100 | perf_group_attach(event); |
4158755d SE |
2101 | event->tstamp_enabled = tstamp; |
2102 | event->tstamp_running = tstamp; | |
2103 | event->tstamp_stopped = tstamp; | |
53cfbf59 PM |
2104 | } |
2105 | ||
bd2afa49 PZ |
2106 | static void ctx_sched_out(struct perf_event_context *ctx, |
2107 | struct perf_cpu_context *cpuctx, | |
2108 | enum event_type_t event_type); | |
2c29ef0f PZ |
2109 | static void |
2110 | ctx_sched_in(struct perf_event_context *ctx, | |
2111 | struct perf_cpu_context *cpuctx, | |
2112 | enum event_type_t event_type, | |
2113 | struct task_struct *task); | |
fe4b04fa | 2114 | |
bd2afa49 PZ |
2115 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
2116 | struct perf_event_context *ctx) | |
2117 | { | |
2118 | if (!cpuctx->task_ctx) | |
2119 | return; | |
2120 | ||
2121 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2122 | return; | |
2123 | ||
2124 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); | |
2125 | } | |
2126 | ||
dce5855b PZ |
2127 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2128 | struct perf_event_context *ctx, | |
2129 | struct task_struct *task) | |
2130 | { | |
2131 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2132 | if (ctx) | |
2133 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2134 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2135 | if (ctx) | |
2136 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2137 | } | |
2138 | ||
3e349507 PZ |
2139 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
2140 | struct perf_event_context *task_ctx) | |
0017960f | 2141 | { |
3e349507 PZ |
2142 | perf_pmu_disable(cpuctx->ctx.pmu); |
2143 | if (task_ctx) | |
2144 | task_ctx_sched_out(cpuctx, task_ctx); | |
2145 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
2146 | perf_event_sched_in(cpuctx, task_ctx, current); | |
2147 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2148 | } |
2149 | ||
0793a61d | 2150 | /* |
cdd6c482 | 2151 | * Cross CPU call to install and enable a performance event |
682076ae | 2152 | * |
a096309b PZ |
2153 | * Very similar to remote_function() + event_function() but cannot assume that |
2154 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2155 | */ |
fe4b04fa | 2156 | static int __perf_install_in_context(void *info) |
0793a61d | 2157 | { |
a096309b PZ |
2158 | struct perf_event *event = info; |
2159 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2160 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2161 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
a096309b PZ |
2162 | bool activate = true; |
2163 | int ret = 0; | |
0793a61d | 2164 | |
63b6da39 | 2165 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2166 | if (ctx->task) { |
b58f6b0d PZ |
2167 | raw_spin_lock(&ctx->lock); |
2168 | task_ctx = ctx; | |
a096309b PZ |
2169 | |
2170 | /* If we're on the wrong CPU, try again */ | |
2171 | if (task_cpu(ctx->task) != smp_processor_id()) { | |
2172 | ret = -ESRCH; | |
63b6da39 | 2173 | goto unlock; |
a096309b | 2174 | } |
b58f6b0d | 2175 | |
39a43640 | 2176 | /* |
a096309b PZ |
2177 | * If we're on the right CPU, see if the task we target is |
2178 | * current, if not we don't have to activate the ctx, a future | |
2179 | * context switch will do that for us. | |
39a43640 | 2180 | */ |
a096309b PZ |
2181 | if (ctx->task != current) |
2182 | activate = false; | |
2183 | else | |
2184 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); | |
2185 | ||
63b6da39 PZ |
2186 | } else if (task_ctx) { |
2187 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2188 | } |
b58f6b0d | 2189 | |
a096309b PZ |
2190 | if (activate) { |
2191 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2192 | add_event_to_ctx(event, ctx); | |
2193 | ctx_resched(cpuctx, task_ctx); | |
2194 | } else { | |
2195 | add_event_to_ctx(event, ctx); | |
2196 | } | |
2197 | ||
63b6da39 | 2198 | unlock: |
2c29ef0f | 2199 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2200 | |
a096309b | 2201 | return ret; |
0793a61d TG |
2202 | } |
2203 | ||
2204 | /* | |
a096309b PZ |
2205 | * Attach a performance event to a context. |
2206 | * | |
2207 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2208 | */ |
2209 | static void | |
cdd6c482 IM |
2210 | perf_install_in_context(struct perf_event_context *ctx, |
2211 | struct perf_event *event, | |
0793a61d TG |
2212 | int cpu) |
2213 | { | |
a096309b | 2214 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2215 | |
fe4b04fa PZ |
2216 | lockdep_assert_held(&ctx->mutex); |
2217 | ||
c3f00c70 | 2218 | event->ctx = ctx; |
0cda4c02 YZ |
2219 | if (event->cpu != -1) |
2220 | event->cpu = cpu; | |
c3f00c70 | 2221 | |
a096309b PZ |
2222 | if (!task) { |
2223 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2224 | return; | |
2225 | } | |
2226 | ||
2227 | /* | |
2228 | * Should not happen, we validate the ctx is still alive before calling. | |
2229 | */ | |
2230 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2231 | return; | |
2232 | ||
39a43640 PZ |
2233 | /* |
2234 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2235 | * to be set in case this is the nr_events 0 -> 1 transition. | |
39a43640 | 2236 | */ |
a096309b | 2237 | again: |
63b6da39 | 2238 | /* |
a096309b PZ |
2239 | * Cannot use task_function_call() because we need to run on the task's |
2240 | * CPU regardless of whether its current or not. | |
63b6da39 | 2241 | */ |
a096309b PZ |
2242 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
2243 | return; | |
2244 | ||
2245 | raw_spin_lock_irq(&ctx->lock); | |
2246 | task = ctx->task; | |
84c4e620 | 2247 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2248 | /* |
2249 | * Cannot happen because we already checked above (which also | |
2250 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2251 | * against perf_event_exit_task_context(). | |
2252 | */ | |
63b6da39 PZ |
2253 | raw_spin_unlock_irq(&ctx->lock); |
2254 | return; | |
2255 | } | |
39a43640 | 2256 | raw_spin_unlock_irq(&ctx->lock); |
39a43640 | 2257 | /* |
a096309b PZ |
2258 | * Since !ctx->is_active doesn't mean anything, we must IPI |
2259 | * unconditionally. | |
39a43640 | 2260 | */ |
a096309b | 2261 | goto again; |
0793a61d TG |
2262 | } |
2263 | ||
fa289bec | 2264 | /* |
cdd6c482 | 2265 | * Put a event into inactive state and update time fields. |
fa289bec PM |
2266 | * Enabling the leader of a group effectively enables all |
2267 | * the group members that aren't explicitly disabled, so we | |
2268 | * have to update their ->tstamp_enabled also. | |
2269 | * Note: this works for group members as well as group leaders | |
2270 | * since the non-leader members' sibling_lists will be empty. | |
2271 | */ | |
1d9b482e | 2272 | static void __perf_event_mark_enabled(struct perf_event *event) |
fa289bec | 2273 | { |
cdd6c482 | 2274 | struct perf_event *sub; |
4158755d | 2275 | u64 tstamp = perf_event_time(event); |
fa289bec | 2276 | |
cdd6c482 | 2277 | event->state = PERF_EVENT_STATE_INACTIVE; |
4158755d | 2278 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
9ed6060d | 2279 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
4158755d SE |
2280 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
2281 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
9ed6060d | 2282 | } |
fa289bec PM |
2283 | } |
2284 | ||
d859e29f | 2285 | /* |
cdd6c482 | 2286 | * Cross CPU call to enable a performance event |
d859e29f | 2287 | */ |
fae3fde6 PZ |
2288 | static void __perf_event_enable(struct perf_event *event, |
2289 | struct perf_cpu_context *cpuctx, | |
2290 | struct perf_event_context *ctx, | |
2291 | void *info) | |
04289bb9 | 2292 | { |
cdd6c482 | 2293 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2294 | struct perf_event_context *task_ctx; |
04289bb9 | 2295 | |
6e801e01 PZ |
2296 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2297 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2298 | return; |
3cbed429 | 2299 | |
bd2afa49 PZ |
2300 | if (ctx->is_active) |
2301 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2302 | ||
1d9b482e | 2303 | __perf_event_mark_enabled(event); |
04289bb9 | 2304 | |
fae3fde6 PZ |
2305 | if (!ctx->is_active) |
2306 | return; | |
2307 | ||
e5d1367f | 2308 | if (!event_filter_match(event)) { |
bd2afa49 | 2309 | if (is_cgroup_event(event)) |
e5d1367f | 2310 | perf_cgroup_defer_enabled(event); |
bd2afa49 | 2311 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2312 | return; |
e5d1367f | 2313 | } |
f4c4176f | 2314 | |
04289bb9 | 2315 | /* |
cdd6c482 | 2316 | * If the event is in a group and isn't the group leader, |
d859e29f | 2317 | * then don't put it on unless the group is on. |
04289bb9 | 2318 | */ |
bd2afa49 PZ |
2319 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2320 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2321 | return; |
bd2afa49 | 2322 | } |
fe4b04fa | 2323 | |
fae3fde6 PZ |
2324 | task_ctx = cpuctx->task_ctx; |
2325 | if (ctx->task) | |
2326 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2327 | |
fae3fde6 | 2328 | ctx_resched(cpuctx, task_ctx); |
7b648018 PZ |
2329 | } |
2330 | ||
d859e29f | 2331 | /* |
cdd6c482 | 2332 | * Enable a event. |
c93f7669 | 2333 | * |
cdd6c482 IM |
2334 | * If event->ctx is a cloned context, callers must make sure that |
2335 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2336 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2337 | * perf_event_for_each_child or perf_event_for_each as described |
2338 | * for perf_event_disable. | |
d859e29f | 2339 | */ |
f63a8daa | 2340 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2341 | { |
cdd6c482 | 2342 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2343 | |
7b648018 | 2344 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2345 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2346 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2347 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2348 | return; |
2349 | } | |
2350 | ||
d859e29f | 2351 | /* |
cdd6c482 | 2352 | * If the event is in error state, clear that first. |
7b648018 PZ |
2353 | * |
2354 | * That way, if we see the event in error state below, we know that it | |
2355 | * has gone back into error state, as distinct from the task having | |
2356 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2357 | */ |
cdd6c482 IM |
2358 | if (event->state == PERF_EVENT_STATE_ERROR) |
2359 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2360 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2361 | |
fae3fde6 | 2362 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2363 | } |
f63a8daa PZ |
2364 | |
2365 | /* | |
2366 | * See perf_event_disable(); | |
2367 | */ | |
2368 | void perf_event_enable(struct perf_event *event) | |
2369 | { | |
2370 | struct perf_event_context *ctx; | |
2371 | ||
2372 | ctx = perf_event_ctx_lock(event); | |
2373 | _perf_event_enable(event); | |
2374 | perf_event_ctx_unlock(event, ctx); | |
2375 | } | |
dcfce4a0 | 2376 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2377 | |
375637bc AS |
2378 | struct stop_event_data { |
2379 | struct perf_event *event; | |
2380 | unsigned int restart; | |
2381 | }; | |
2382 | ||
95ff4ca2 AS |
2383 | static int __perf_event_stop(void *info) |
2384 | { | |
375637bc AS |
2385 | struct stop_event_data *sd = info; |
2386 | struct perf_event *event = sd->event; | |
95ff4ca2 | 2387 | |
375637bc | 2388 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
2389 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
2390 | return 0; | |
2391 | ||
2392 | /* matches smp_wmb() in event_sched_in() */ | |
2393 | smp_rmb(); | |
2394 | ||
2395 | /* | |
2396 | * There is a window with interrupts enabled before we get here, | |
2397 | * so we need to check again lest we try to stop another CPU's event. | |
2398 | */ | |
2399 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2400 | return -EAGAIN; | |
2401 | ||
2402 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2403 | ||
375637bc AS |
2404 | /* |
2405 | * May race with the actual stop (through perf_pmu_output_stop()), | |
2406 | * but it is only used for events with AUX ring buffer, and such | |
2407 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
2408 | * see comments in perf_aux_output_begin(). | |
2409 | * | |
2410 | * Since this is happening on a event-local CPU, no trace is lost | |
2411 | * while restarting. | |
2412 | */ | |
2413 | if (sd->restart) | |
2414 | event->pmu->start(event, PERF_EF_START); | |
2415 | ||
95ff4ca2 AS |
2416 | return 0; |
2417 | } | |
2418 | ||
375637bc AS |
2419 | static int perf_event_restart(struct perf_event *event) |
2420 | { | |
2421 | struct stop_event_data sd = { | |
2422 | .event = event, | |
2423 | .restart = 1, | |
2424 | }; | |
2425 | int ret = 0; | |
2426 | ||
2427 | do { | |
2428 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2429 | return 0; | |
2430 | ||
2431 | /* matches smp_wmb() in event_sched_in() */ | |
2432 | smp_rmb(); | |
2433 | ||
2434 | /* | |
2435 | * We only want to restart ACTIVE events, so if the event goes | |
2436 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
2437 | * fall through with ret==-ENXIO. | |
2438 | */ | |
2439 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
2440 | __perf_event_stop, &sd); | |
2441 | } while (ret == -EAGAIN); | |
2442 | ||
2443 | return ret; | |
2444 | } | |
2445 | ||
2446 | /* | |
2447 | * In order to contain the amount of racy and tricky in the address filter | |
2448 | * configuration management, it is a two part process: | |
2449 | * | |
2450 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
2451 | * we update the addresses of corresponding vmas in | |
2452 | * event::addr_filters_offs array and bump the event::addr_filters_gen; | |
2453 | * (p2) when an event is scheduled in (pmu::add), it calls | |
2454 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
2455 | * if the generation has changed since the previous call. | |
2456 | * | |
2457 | * If (p1) happens while the event is active, we restart it to force (p2). | |
2458 | * | |
2459 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
2460 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
2461 | * ioctl; | |
2462 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
2463 | * registered mapping, called for every new mmap(), with mm::mmap_sem down | |
2464 | * for reading; | |
2465 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
2466 | * of exec. | |
2467 | */ | |
2468 | void perf_event_addr_filters_sync(struct perf_event *event) | |
2469 | { | |
2470 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
2471 | ||
2472 | if (!has_addr_filter(event)) | |
2473 | return; | |
2474 | ||
2475 | raw_spin_lock(&ifh->lock); | |
2476 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
2477 | event->pmu->addr_filters_sync(event); | |
2478 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
2479 | } | |
2480 | raw_spin_unlock(&ifh->lock); | |
2481 | } | |
2482 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
2483 | ||
f63a8daa | 2484 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2485 | { |
2023b359 | 2486 | /* |
cdd6c482 | 2487 | * not supported on inherited events |
2023b359 | 2488 | */ |
2e939d1d | 2489 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2490 | return -EINVAL; |
2491 | ||
cdd6c482 | 2492 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2493 | _perf_event_enable(event); |
2023b359 PZ |
2494 | |
2495 | return 0; | |
79f14641 | 2496 | } |
f63a8daa PZ |
2497 | |
2498 | /* | |
2499 | * See perf_event_disable() | |
2500 | */ | |
2501 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2502 | { | |
2503 | struct perf_event_context *ctx; | |
2504 | int ret; | |
2505 | ||
2506 | ctx = perf_event_ctx_lock(event); | |
2507 | ret = _perf_event_refresh(event, refresh); | |
2508 | perf_event_ctx_unlock(event, ctx); | |
2509 | ||
2510 | return ret; | |
2511 | } | |
26ca5c11 | 2512 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2513 | |
5b0311e1 FW |
2514 | static void ctx_sched_out(struct perf_event_context *ctx, |
2515 | struct perf_cpu_context *cpuctx, | |
2516 | enum event_type_t event_type) | |
235c7fc7 | 2517 | { |
db24d33e | 2518 | int is_active = ctx->is_active; |
c994d613 | 2519 | struct perf_event *event; |
235c7fc7 | 2520 | |
c994d613 | 2521 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2522 | |
39a43640 PZ |
2523 | if (likely(!ctx->nr_events)) { |
2524 | /* | |
2525 | * See __perf_remove_from_context(). | |
2526 | */ | |
2527 | WARN_ON_ONCE(ctx->is_active); | |
2528 | if (ctx->task) | |
2529 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2530 | return; |
39a43640 PZ |
2531 | } |
2532 | ||
db24d33e | 2533 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2534 | if (!(ctx->is_active & EVENT_ALL)) |
2535 | ctx->is_active = 0; | |
2536 | ||
63e30d3e PZ |
2537 | if (ctx->task) { |
2538 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2539 | if (!ctx->is_active) | |
2540 | cpuctx->task_ctx = NULL; | |
2541 | } | |
facc4307 | 2542 | |
8fdc6539 PZ |
2543 | /* |
2544 | * Always update time if it was set; not only when it changes. | |
2545 | * Otherwise we can 'forget' to update time for any but the last | |
2546 | * context we sched out. For example: | |
2547 | * | |
2548 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2549 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2550 | * | |
2551 | * would only update time for the pinned events. | |
2552 | */ | |
3cbaa590 PZ |
2553 | if (is_active & EVENT_TIME) { |
2554 | /* update (and stop) ctx time */ | |
2555 | update_context_time(ctx); | |
2556 | update_cgrp_time_from_cpuctx(cpuctx); | |
2557 | } | |
2558 | ||
8fdc6539 PZ |
2559 | is_active ^= ctx->is_active; /* changed bits */ |
2560 | ||
3cbaa590 | 2561 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2562 | return; |
5b0311e1 | 2563 | |
075e0b00 | 2564 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2565 | if (is_active & EVENT_PINNED) { |
889ff015 FW |
2566 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
2567 | group_sched_out(event, cpuctx, ctx); | |
9ed6060d | 2568 | } |
889ff015 | 2569 | |
3cbaa590 | 2570 | if (is_active & EVENT_FLEXIBLE) { |
889ff015 | 2571 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
8c9ed8e1 | 2572 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2573 | } |
1b9a644f | 2574 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2575 | } |
2576 | ||
564c2b21 | 2577 | /* |
5a3126d4 PZ |
2578 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2579 | * cloned from the same version of the same context. | |
2580 | * | |
2581 | * Equivalence is measured using a generation number in the context that is | |
2582 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2583 | * and list_del_event(). | |
564c2b21 | 2584 | */ |
cdd6c482 IM |
2585 | static int context_equiv(struct perf_event_context *ctx1, |
2586 | struct perf_event_context *ctx2) | |
564c2b21 | 2587 | { |
211de6eb PZ |
2588 | lockdep_assert_held(&ctx1->lock); |
2589 | lockdep_assert_held(&ctx2->lock); | |
2590 | ||
5a3126d4 PZ |
2591 | /* Pinning disables the swap optimization */ |
2592 | if (ctx1->pin_count || ctx2->pin_count) | |
2593 | return 0; | |
2594 | ||
2595 | /* If ctx1 is the parent of ctx2 */ | |
2596 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2597 | return 1; | |
2598 | ||
2599 | /* If ctx2 is the parent of ctx1 */ | |
2600 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2601 | return 1; | |
2602 | ||
2603 | /* | |
2604 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2605 | * hierarchy, see perf_event_init_context(). | |
2606 | */ | |
2607 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2608 | ctx1->parent_gen == ctx2->parent_gen) | |
2609 | return 1; | |
2610 | ||
2611 | /* Unmatched */ | |
2612 | return 0; | |
564c2b21 PM |
2613 | } |
2614 | ||
cdd6c482 IM |
2615 | static void __perf_event_sync_stat(struct perf_event *event, |
2616 | struct perf_event *next_event) | |
bfbd3381 PZ |
2617 | { |
2618 | u64 value; | |
2619 | ||
cdd6c482 | 2620 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2621 | return; |
2622 | ||
2623 | /* | |
cdd6c482 | 2624 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2625 | * because we're in the middle of a context switch and have IRQs |
2626 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2627 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2628 | * don't need to use it. |
2629 | */ | |
cdd6c482 IM |
2630 | switch (event->state) { |
2631 | case PERF_EVENT_STATE_ACTIVE: | |
3dbebf15 PZ |
2632 | event->pmu->read(event); |
2633 | /* fall-through */ | |
bfbd3381 | 2634 | |
cdd6c482 IM |
2635 | case PERF_EVENT_STATE_INACTIVE: |
2636 | update_event_times(event); | |
bfbd3381 PZ |
2637 | break; |
2638 | ||
2639 | default: | |
2640 | break; | |
2641 | } | |
2642 | ||
2643 | /* | |
cdd6c482 | 2644 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2645 | * values when we flip the contexts. |
2646 | */ | |
e7850595 PZ |
2647 | value = local64_read(&next_event->count); |
2648 | value = local64_xchg(&event->count, value); | |
2649 | local64_set(&next_event->count, value); | |
bfbd3381 | 2650 | |
cdd6c482 IM |
2651 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2652 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2653 | |
bfbd3381 | 2654 | /* |
19d2e755 | 2655 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2656 | */ |
cdd6c482 IM |
2657 | perf_event_update_userpage(event); |
2658 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2659 | } |
2660 | ||
cdd6c482 IM |
2661 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2662 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2663 | { |
cdd6c482 | 2664 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2665 | |
2666 | if (!ctx->nr_stat) | |
2667 | return; | |
2668 | ||
02ffdbc8 PZ |
2669 | update_context_time(ctx); |
2670 | ||
cdd6c482 IM |
2671 | event = list_first_entry(&ctx->event_list, |
2672 | struct perf_event, event_entry); | |
bfbd3381 | 2673 | |
cdd6c482 IM |
2674 | next_event = list_first_entry(&next_ctx->event_list, |
2675 | struct perf_event, event_entry); | |
bfbd3381 | 2676 | |
cdd6c482 IM |
2677 | while (&event->event_entry != &ctx->event_list && |
2678 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 2679 | |
cdd6c482 | 2680 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 2681 | |
cdd6c482 IM |
2682 | event = list_next_entry(event, event_entry); |
2683 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
2684 | } |
2685 | } | |
2686 | ||
fe4b04fa PZ |
2687 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
2688 | struct task_struct *next) | |
0793a61d | 2689 | { |
8dc85d54 | 2690 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 2691 | struct perf_event_context *next_ctx; |
5a3126d4 | 2692 | struct perf_event_context *parent, *next_parent; |
108b02cf | 2693 | struct perf_cpu_context *cpuctx; |
c93f7669 | 2694 | int do_switch = 1; |
0793a61d | 2695 | |
108b02cf PZ |
2696 | if (likely(!ctx)) |
2697 | return; | |
10989fb2 | 2698 | |
108b02cf PZ |
2699 | cpuctx = __get_cpu_context(ctx); |
2700 | if (!cpuctx->task_ctx) | |
0793a61d TG |
2701 | return; |
2702 | ||
c93f7669 | 2703 | rcu_read_lock(); |
8dc85d54 | 2704 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
2705 | if (!next_ctx) |
2706 | goto unlock; | |
2707 | ||
2708 | parent = rcu_dereference(ctx->parent_ctx); | |
2709 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
2710 | ||
2711 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 2712 | if (!parent && !next_parent) |
5a3126d4 PZ |
2713 | goto unlock; |
2714 | ||
2715 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
2716 | /* |
2717 | * Looks like the two contexts are clones, so we might be | |
2718 | * able to optimize the context switch. We lock both | |
2719 | * contexts and check that they are clones under the | |
2720 | * lock (including re-checking that neither has been | |
2721 | * uncloned in the meantime). It doesn't matter which | |
2722 | * order we take the locks because no other cpu could | |
2723 | * be trying to lock both of these tasks. | |
2724 | */ | |
e625cce1 TG |
2725 | raw_spin_lock(&ctx->lock); |
2726 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 2727 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
2728 | WRITE_ONCE(ctx->task, next); |
2729 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
2730 | |
2731 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
2732 | ||
63b6da39 PZ |
2733 | /* |
2734 | * RCU_INIT_POINTER here is safe because we've not | |
2735 | * modified the ctx and the above modification of | |
2736 | * ctx->task and ctx->task_ctx_data are immaterial | |
2737 | * since those values are always verified under | |
2738 | * ctx->lock which we're now holding. | |
2739 | */ | |
2740 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
2741 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
2742 | ||
c93f7669 | 2743 | do_switch = 0; |
bfbd3381 | 2744 | |
cdd6c482 | 2745 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 2746 | } |
e625cce1 TG |
2747 | raw_spin_unlock(&next_ctx->lock); |
2748 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 2749 | } |
5a3126d4 | 2750 | unlock: |
c93f7669 | 2751 | rcu_read_unlock(); |
564c2b21 | 2752 | |
c93f7669 | 2753 | if (do_switch) { |
facc4307 | 2754 | raw_spin_lock(&ctx->lock); |
8833d0e2 | 2755 | task_ctx_sched_out(cpuctx, ctx); |
facc4307 | 2756 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 2757 | } |
0793a61d TG |
2758 | } |
2759 | ||
ba532500 YZ |
2760 | void perf_sched_cb_dec(struct pmu *pmu) |
2761 | { | |
2762 | this_cpu_dec(perf_sched_cb_usages); | |
2763 | } | |
2764 | ||
2765 | void perf_sched_cb_inc(struct pmu *pmu) | |
2766 | { | |
2767 | this_cpu_inc(perf_sched_cb_usages); | |
2768 | } | |
2769 | ||
2770 | /* | |
2771 | * This function provides the context switch callback to the lower code | |
2772 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
2773 | */ | |
2774 | static void perf_pmu_sched_task(struct task_struct *prev, | |
2775 | struct task_struct *next, | |
2776 | bool sched_in) | |
2777 | { | |
2778 | struct perf_cpu_context *cpuctx; | |
2779 | struct pmu *pmu; | |
2780 | unsigned long flags; | |
2781 | ||
2782 | if (prev == next) | |
2783 | return; | |
2784 | ||
2785 | local_irq_save(flags); | |
2786 | ||
2787 | rcu_read_lock(); | |
2788 | ||
2789 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
2790 | if (pmu->sched_task) { | |
2791 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); | |
2792 | ||
2793 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); | |
2794 | ||
2795 | perf_pmu_disable(pmu); | |
2796 | ||
2797 | pmu->sched_task(cpuctx->task_ctx, sched_in); | |
2798 | ||
2799 | perf_pmu_enable(pmu); | |
2800 | ||
2801 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
2802 | } | |
2803 | } | |
2804 | ||
2805 | rcu_read_unlock(); | |
2806 | ||
2807 | local_irq_restore(flags); | |
2808 | } | |
2809 | ||
45ac1403 AH |
2810 | static void perf_event_switch(struct task_struct *task, |
2811 | struct task_struct *next_prev, bool sched_in); | |
2812 | ||
8dc85d54 PZ |
2813 | #define for_each_task_context_nr(ctxn) \ |
2814 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
2815 | ||
2816 | /* | |
2817 | * Called from scheduler to remove the events of the current task, | |
2818 | * with interrupts disabled. | |
2819 | * | |
2820 | * We stop each event and update the event value in event->count. | |
2821 | * | |
2822 | * This does not protect us against NMI, but disable() | |
2823 | * sets the disabled bit in the control field of event _before_ | |
2824 | * accessing the event control register. If a NMI hits, then it will | |
2825 | * not restart the event. | |
2826 | */ | |
ab0cce56 JO |
2827 | void __perf_event_task_sched_out(struct task_struct *task, |
2828 | struct task_struct *next) | |
8dc85d54 PZ |
2829 | { |
2830 | int ctxn; | |
2831 | ||
ba532500 YZ |
2832 | if (__this_cpu_read(perf_sched_cb_usages)) |
2833 | perf_pmu_sched_task(task, next, false); | |
2834 | ||
45ac1403 AH |
2835 | if (atomic_read(&nr_switch_events)) |
2836 | perf_event_switch(task, next, false); | |
2837 | ||
8dc85d54 PZ |
2838 | for_each_task_context_nr(ctxn) |
2839 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
2840 | |
2841 | /* | |
2842 | * if cgroup events exist on this CPU, then we need | |
2843 | * to check if we have to switch out PMU state. | |
2844 | * cgroup event are system-wide mode only | |
2845 | */ | |
4a32fea9 | 2846 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 2847 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
2848 | } |
2849 | ||
5b0311e1 FW |
2850 | /* |
2851 | * Called with IRQs disabled | |
2852 | */ | |
2853 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
2854 | enum event_type_t event_type) | |
2855 | { | |
2856 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
2857 | } |
2858 | ||
235c7fc7 | 2859 | static void |
5b0311e1 | 2860 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
6e37738a | 2861 | struct perf_cpu_context *cpuctx) |
0793a61d | 2862 | { |
cdd6c482 | 2863 | struct perf_event *event; |
0793a61d | 2864 | |
889ff015 FW |
2865 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
2866 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2867 | continue; |
5632ab12 | 2868 | if (!event_filter_match(event)) |
3b6f9e5c PM |
2869 | continue; |
2870 | ||
e5d1367f SE |
2871 | /* may need to reset tstamp_enabled */ |
2872 | if (is_cgroup_event(event)) | |
2873 | perf_cgroup_mark_enabled(event, ctx); | |
2874 | ||
8c9ed8e1 | 2875 | if (group_can_go_on(event, cpuctx, 1)) |
6e37738a | 2876 | group_sched_in(event, cpuctx, ctx); |
3b6f9e5c PM |
2877 | |
2878 | /* | |
2879 | * If this pinned group hasn't been scheduled, | |
2880 | * put it in error state. | |
2881 | */ | |
cdd6c482 IM |
2882 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2883 | update_group_times(event); | |
2884 | event->state = PERF_EVENT_STATE_ERROR; | |
53cfbf59 | 2885 | } |
3b6f9e5c | 2886 | } |
5b0311e1 FW |
2887 | } |
2888 | ||
2889 | static void | |
2890 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
6e37738a | 2891 | struct perf_cpu_context *cpuctx) |
5b0311e1 FW |
2892 | { |
2893 | struct perf_event *event; | |
2894 | int can_add_hw = 1; | |
3b6f9e5c | 2895 | |
889ff015 FW |
2896 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
2897 | /* Ignore events in OFF or ERROR state */ | |
2898 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2899 | continue; |
04289bb9 IM |
2900 | /* |
2901 | * Listen to the 'cpu' scheduling filter constraint | |
cdd6c482 | 2902 | * of events: |
04289bb9 | 2903 | */ |
5632ab12 | 2904 | if (!event_filter_match(event)) |
0793a61d TG |
2905 | continue; |
2906 | ||
e5d1367f SE |
2907 | /* may need to reset tstamp_enabled */ |
2908 | if (is_cgroup_event(event)) | |
2909 | perf_cgroup_mark_enabled(event, ctx); | |
2910 | ||
9ed6060d | 2911 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
6e37738a | 2912 | if (group_sched_in(event, cpuctx, ctx)) |
dd0e6ba2 | 2913 | can_add_hw = 0; |
9ed6060d | 2914 | } |
0793a61d | 2915 | } |
5b0311e1 FW |
2916 | } |
2917 | ||
2918 | static void | |
2919 | ctx_sched_in(struct perf_event_context *ctx, | |
2920 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
2921 | enum event_type_t event_type, |
2922 | struct task_struct *task) | |
5b0311e1 | 2923 | { |
db24d33e | 2924 | int is_active = ctx->is_active; |
c994d613 PZ |
2925 | u64 now; |
2926 | ||
2927 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 2928 | |
5b0311e1 | 2929 | if (likely(!ctx->nr_events)) |
facc4307 | 2930 | return; |
5b0311e1 | 2931 | |
3cbaa590 | 2932 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
2933 | if (ctx->task) { |
2934 | if (!is_active) | |
2935 | cpuctx->task_ctx = ctx; | |
2936 | else | |
2937 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2938 | } | |
2939 | ||
3cbaa590 PZ |
2940 | is_active ^= ctx->is_active; /* changed bits */ |
2941 | ||
2942 | if (is_active & EVENT_TIME) { | |
2943 | /* start ctx time */ | |
2944 | now = perf_clock(); | |
2945 | ctx->timestamp = now; | |
2946 | perf_cgroup_set_timestamp(task, ctx); | |
2947 | } | |
2948 | ||
5b0311e1 FW |
2949 | /* |
2950 | * First go through the list and put on any pinned groups | |
2951 | * in order to give them the best chance of going on. | |
2952 | */ | |
3cbaa590 | 2953 | if (is_active & EVENT_PINNED) |
6e37738a | 2954 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
2955 | |
2956 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 2957 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 2958 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
2959 | } |
2960 | ||
329c0e01 | 2961 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
2962 | enum event_type_t event_type, |
2963 | struct task_struct *task) | |
329c0e01 FW |
2964 | { |
2965 | struct perf_event_context *ctx = &cpuctx->ctx; | |
2966 | ||
e5d1367f | 2967 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
2968 | } |
2969 | ||
e5d1367f SE |
2970 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
2971 | struct task_struct *task) | |
235c7fc7 | 2972 | { |
108b02cf | 2973 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 2974 | |
108b02cf | 2975 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
2976 | if (cpuctx->task_ctx == ctx) |
2977 | return; | |
2978 | ||
facc4307 | 2979 | perf_ctx_lock(cpuctx, ctx); |
1b9a644f | 2980 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
2981 | /* |
2982 | * We want to keep the following priority order: | |
2983 | * cpu pinned (that don't need to move), task pinned, | |
2984 | * cpu flexible, task flexible. | |
2985 | */ | |
2986 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
63e30d3e | 2987 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 PZ |
2988 | perf_pmu_enable(ctx->pmu); |
2989 | perf_ctx_unlock(cpuctx, ctx); | |
235c7fc7 IM |
2990 | } |
2991 | ||
8dc85d54 PZ |
2992 | /* |
2993 | * Called from scheduler to add the events of the current task | |
2994 | * with interrupts disabled. | |
2995 | * | |
2996 | * We restore the event value and then enable it. | |
2997 | * | |
2998 | * This does not protect us against NMI, but enable() | |
2999 | * sets the enabled bit in the control field of event _before_ | |
3000 | * accessing the event control register. If a NMI hits, then it will | |
3001 | * keep the event running. | |
3002 | */ | |
ab0cce56 JO |
3003 | void __perf_event_task_sched_in(struct task_struct *prev, |
3004 | struct task_struct *task) | |
8dc85d54 PZ |
3005 | { |
3006 | struct perf_event_context *ctx; | |
3007 | int ctxn; | |
3008 | ||
7e41d177 PZ |
3009 | /* |
3010 | * If cgroup events exist on this CPU, then we need to check if we have | |
3011 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3012 | * | |
3013 | * Since cgroup events are CPU events, we must schedule these in before | |
3014 | * we schedule in the task events. | |
3015 | */ | |
3016 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3017 | perf_cgroup_sched_in(prev, task); | |
3018 | ||
8dc85d54 PZ |
3019 | for_each_task_context_nr(ctxn) { |
3020 | ctx = task->perf_event_ctxp[ctxn]; | |
3021 | if (likely(!ctx)) | |
3022 | continue; | |
3023 | ||
e5d1367f | 3024 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3025 | } |
d010b332 | 3026 | |
45ac1403 AH |
3027 | if (atomic_read(&nr_switch_events)) |
3028 | perf_event_switch(task, prev, true); | |
3029 | ||
ba532500 YZ |
3030 | if (__this_cpu_read(perf_sched_cb_usages)) |
3031 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3032 | } |
3033 | ||
abd50713 PZ |
3034 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3035 | { | |
3036 | u64 frequency = event->attr.sample_freq; | |
3037 | u64 sec = NSEC_PER_SEC; | |
3038 | u64 divisor, dividend; | |
3039 | ||
3040 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3041 | ||
3042 | count_fls = fls64(count); | |
3043 | nsec_fls = fls64(nsec); | |
3044 | frequency_fls = fls64(frequency); | |
3045 | sec_fls = 30; | |
3046 | ||
3047 | /* | |
3048 | * We got @count in @nsec, with a target of sample_freq HZ | |
3049 | * the target period becomes: | |
3050 | * | |
3051 | * @count * 10^9 | |
3052 | * period = ------------------- | |
3053 | * @nsec * sample_freq | |
3054 | * | |
3055 | */ | |
3056 | ||
3057 | /* | |
3058 | * Reduce accuracy by one bit such that @a and @b converge | |
3059 | * to a similar magnitude. | |
3060 | */ | |
fe4b04fa | 3061 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3062 | do { \ |
3063 | if (a##_fls > b##_fls) { \ | |
3064 | a >>= 1; \ | |
3065 | a##_fls--; \ | |
3066 | } else { \ | |
3067 | b >>= 1; \ | |
3068 | b##_fls--; \ | |
3069 | } \ | |
3070 | } while (0) | |
3071 | ||
3072 | /* | |
3073 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3074 | * the other, so that finally we can do a u64/u64 division. | |
3075 | */ | |
3076 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3077 | REDUCE_FLS(nsec, frequency); | |
3078 | REDUCE_FLS(sec, count); | |
3079 | } | |
3080 | ||
3081 | if (count_fls + sec_fls > 64) { | |
3082 | divisor = nsec * frequency; | |
3083 | ||
3084 | while (count_fls + sec_fls > 64) { | |
3085 | REDUCE_FLS(count, sec); | |
3086 | divisor >>= 1; | |
3087 | } | |
3088 | ||
3089 | dividend = count * sec; | |
3090 | } else { | |
3091 | dividend = count * sec; | |
3092 | ||
3093 | while (nsec_fls + frequency_fls > 64) { | |
3094 | REDUCE_FLS(nsec, frequency); | |
3095 | dividend >>= 1; | |
3096 | } | |
3097 | ||
3098 | divisor = nsec * frequency; | |
3099 | } | |
3100 | ||
f6ab91ad PZ |
3101 | if (!divisor) |
3102 | return dividend; | |
3103 | ||
abd50713 PZ |
3104 | return div64_u64(dividend, divisor); |
3105 | } | |
3106 | ||
e050e3f0 SE |
3107 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3108 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3109 | ||
f39d47ff | 3110 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3111 | { |
cdd6c482 | 3112 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3113 | s64 period, sample_period; |
bd2b5b12 PZ |
3114 | s64 delta; |
3115 | ||
abd50713 | 3116 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3117 | |
3118 | delta = (s64)(period - hwc->sample_period); | |
3119 | delta = (delta + 7) / 8; /* low pass filter */ | |
3120 | ||
3121 | sample_period = hwc->sample_period + delta; | |
3122 | ||
3123 | if (!sample_period) | |
3124 | sample_period = 1; | |
3125 | ||
bd2b5b12 | 3126 | hwc->sample_period = sample_period; |
abd50713 | 3127 | |
e7850595 | 3128 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3129 | if (disable) |
3130 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3131 | ||
e7850595 | 3132 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3133 | |
3134 | if (disable) | |
3135 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3136 | } |
bd2b5b12 PZ |
3137 | } |
3138 | ||
e050e3f0 SE |
3139 | /* |
3140 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3141 | * events. At the same time, make sure, having freq events does not change | |
3142 | * the rate of unthrottling as that would introduce bias. | |
3143 | */ | |
3144 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3145 | int needs_unthr) | |
60db5e09 | 3146 | { |
cdd6c482 IM |
3147 | struct perf_event *event; |
3148 | struct hw_perf_event *hwc; | |
e050e3f0 | 3149 | u64 now, period = TICK_NSEC; |
abd50713 | 3150 | s64 delta; |
60db5e09 | 3151 | |
e050e3f0 SE |
3152 | /* |
3153 | * only need to iterate over all events iff: | |
3154 | * - context have events in frequency mode (needs freq adjust) | |
3155 | * - there are events to unthrottle on this cpu | |
3156 | */ | |
3157 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3158 | return; |
3159 | ||
e050e3f0 | 3160 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3161 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3162 | |
03541f8b | 3163 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3164 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3165 | continue; |
3166 | ||
5632ab12 | 3167 | if (!event_filter_match(event)) |
5d27c23d PZ |
3168 | continue; |
3169 | ||
44377277 AS |
3170 | perf_pmu_disable(event->pmu); |
3171 | ||
cdd6c482 | 3172 | hwc = &event->hw; |
6a24ed6c | 3173 | |
ae23bff1 | 3174 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3175 | hwc->interrupts = 0; |
cdd6c482 | 3176 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3177 | event->pmu->start(event, 0); |
a78ac325 PZ |
3178 | } |
3179 | ||
cdd6c482 | 3180 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3181 | goto next; |
60db5e09 | 3182 | |
e050e3f0 SE |
3183 | /* |
3184 | * stop the event and update event->count | |
3185 | */ | |
3186 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3187 | ||
e7850595 | 3188 | now = local64_read(&event->count); |
abd50713 PZ |
3189 | delta = now - hwc->freq_count_stamp; |
3190 | hwc->freq_count_stamp = now; | |
60db5e09 | 3191 | |
e050e3f0 SE |
3192 | /* |
3193 | * restart the event | |
3194 | * reload only if value has changed | |
f39d47ff SE |
3195 | * we have stopped the event so tell that |
3196 | * to perf_adjust_period() to avoid stopping it | |
3197 | * twice. | |
e050e3f0 | 3198 | */ |
abd50713 | 3199 | if (delta > 0) |
f39d47ff | 3200 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3201 | |
3202 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3203 | next: |
3204 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3205 | } |
e050e3f0 | 3206 | |
f39d47ff | 3207 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3208 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3209 | } |
3210 | ||
235c7fc7 | 3211 | /* |
cdd6c482 | 3212 | * Round-robin a context's events: |
235c7fc7 | 3213 | */ |
cdd6c482 | 3214 | static void rotate_ctx(struct perf_event_context *ctx) |
0793a61d | 3215 | { |
dddd3379 TG |
3216 | /* |
3217 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3218 | * disabled by the inheritance code. | |
3219 | */ | |
3220 | if (!ctx->rotate_disable) | |
3221 | list_rotate_left(&ctx->flexible_groups); | |
235c7fc7 IM |
3222 | } |
3223 | ||
9e630205 | 3224 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
235c7fc7 | 3225 | { |
8dc85d54 | 3226 | struct perf_event_context *ctx = NULL; |
2fde4f94 | 3227 | int rotate = 0; |
7fc23a53 | 3228 | |
b5ab4cd5 | 3229 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 PZ |
3230 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
3231 | rotate = 1; | |
3232 | } | |
235c7fc7 | 3233 | |
8dc85d54 | 3234 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3235 | if (ctx && ctx->nr_events) { |
b5ab4cd5 PZ |
3236 | if (ctx->nr_events != ctx->nr_active) |
3237 | rotate = 1; | |
3238 | } | |
9717e6cd | 3239 | |
e050e3f0 | 3240 | if (!rotate) |
0f5a2601 PZ |
3241 | goto done; |
3242 | ||
facc4307 | 3243 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3244 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3245 | |
e050e3f0 SE |
3246 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
3247 | if (ctx) | |
3248 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3249 | |
e050e3f0 SE |
3250 | rotate_ctx(&cpuctx->ctx); |
3251 | if (ctx) | |
3252 | rotate_ctx(ctx); | |
235c7fc7 | 3253 | |
e050e3f0 | 3254 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3255 | |
0f5a2601 PZ |
3256 | perf_pmu_enable(cpuctx->ctx.pmu); |
3257 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
b5ab4cd5 | 3258 | done: |
9e630205 SE |
3259 | |
3260 | return rotate; | |
e9d2b064 PZ |
3261 | } |
3262 | ||
3263 | void perf_event_task_tick(void) | |
3264 | { | |
2fde4f94 MR |
3265 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3266 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3267 | int throttled; |
b5ab4cd5 | 3268 | |
e9d2b064 PZ |
3269 | WARN_ON(!irqs_disabled()); |
3270 | ||
e050e3f0 SE |
3271 | __this_cpu_inc(perf_throttled_seq); |
3272 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3273 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3274 | |
2fde4f94 | 3275 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3276 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3277 | } |
3278 | ||
889ff015 FW |
3279 | static int event_enable_on_exec(struct perf_event *event, |
3280 | struct perf_event_context *ctx) | |
3281 | { | |
3282 | if (!event->attr.enable_on_exec) | |
3283 | return 0; | |
3284 | ||
3285 | event->attr.enable_on_exec = 0; | |
3286 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3287 | return 0; | |
3288 | ||
1d9b482e | 3289 | __perf_event_mark_enabled(event); |
889ff015 FW |
3290 | |
3291 | return 1; | |
3292 | } | |
3293 | ||
57e7986e | 3294 | /* |
cdd6c482 | 3295 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3296 | * This expects task == current. |
3297 | */ | |
c1274499 | 3298 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3299 | { |
c1274499 | 3300 | struct perf_event_context *ctx, *clone_ctx = NULL; |
3e349507 | 3301 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3302 | struct perf_event *event; |
57e7986e PM |
3303 | unsigned long flags; |
3304 | int enabled = 0; | |
3305 | ||
3306 | local_irq_save(flags); | |
c1274499 | 3307 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3308 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3309 | goto out; |
3310 | ||
3e349507 PZ |
3311 | cpuctx = __get_cpu_context(ctx); |
3312 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3313 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
3e349507 PZ |
3314 | list_for_each_entry(event, &ctx->event_list, event_entry) |
3315 | enabled |= event_enable_on_exec(event, ctx); | |
57e7986e PM |
3316 | |
3317 | /* | |
3e349507 | 3318 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3319 | */ |
3e349507 | 3320 | if (enabled) { |
211de6eb | 3321 | clone_ctx = unclone_ctx(ctx); |
3e349507 PZ |
3322 | ctx_resched(cpuctx, ctx); |
3323 | } | |
3324 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3325 | |
9ed6060d | 3326 | out: |
57e7986e | 3327 | local_irq_restore(flags); |
211de6eb PZ |
3328 | |
3329 | if (clone_ctx) | |
3330 | put_ctx(clone_ctx); | |
57e7986e PM |
3331 | } |
3332 | ||
0492d4c5 PZ |
3333 | struct perf_read_data { |
3334 | struct perf_event *event; | |
3335 | bool group; | |
7d88962e | 3336 | int ret; |
0492d4c5 PZ |
3337 | }; |
3338 | ||
0793a61d | 3339 | /* |
cdd6c482 | 3340 | * Cross CPU call to read the hardware event |
0793a61d | 3341 | */ |
cdd6c482 | 3342 | static void __perf_event_read(void *info) |
0793a61d | 3343 | { |
0492d4c5 PZ |
3344 | struct perf_read_data *data = info; |
3345 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3346 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3347 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3348 | struct pmu *pmu = event->pmu; |
621a01ea | 3349 | |
e1ac3614 PM |
3350 | /* |
3351 | * If this is a task context, we need to check whether it is | |
3352 | * the current task context of this cpu. If not it has been | |
3353 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3354 | * event->count would have been updated to a recent sample |
3355 | * when the event was scheduled out. | |
e1ac3614 PM |
3356 | */ |
3357 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3358 | return; | |
3359 | ||
e625cce1 | 3360 | raw_spin_lock(&ctx->lock); |
e5d1367f | 3361 | if (ctx->is_active) { |
542e72fc | 3362 | update_context_time(ctx); |
e5d1367f SE |
3363 | update_cgrp_time_from_event(event); |
3364 | } | |
0492d4c5 | 3365 | |
cdd6c482 | 3366 | update_event_times(event); |
4a00c16e SB |
3367 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3368 | goto unlock; | |
0492d4c5 | 3369 | |
4a00c16e SB |
3370 | if (!data->group) { |
3371 | pmu->read(event); | |
3372 | data->ret = 0; | |
0492d4c5 | 3373 | goto unlock; |
4a00c16e SB |
3374 | } |
3375 | ||
3376 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3377 | ||
3378 | pmu->read(event); | |
0492d4c5 PZ |
3379 | |
3380 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
3381 | update_event_times(sub); | |
4a00c16e SB |
3382 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3383 | /* | |
3384 | * Use sibling's PMU rather than @event's since | |
3385 | * sibling could be on different (eg: software) PMU. | |
3386 | */ | |
0492d4c5 | 3387 | sub->pmu->read(sub); |
4a00c16e | 3388 | } |
0492d4c5 | 3389 | } |
4a00c16e SB |
3390 | |
3391 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3392 | |
3393 | unlock: | |
e625cce1 | 3394 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3395 | } |
3396 | ||
b5e58793 PZ |
3397 | static inline u64 perf_event_count(struct perf_event *event) |
3398 | { | |
eacd3ecc MF |
3399 | if (event->pmu->count) |
3400 | return event->pmu->count(event); | |
3401 | ||
3402 | return __perf_event_count(event); | |
b5e58793 PZ |
3403 | } |
3404 | ||
ffe8690c KX |
3405 | /* |
3406 | * NMI-safe method to read a local event, that is an event that | |
3407 | * is: | |
3408 | * - either for the current task, or for this CPU | |
3409 | * - does not have inherit set, for inherited task events | |
3410 | * will not be local and we cannot read them atomically | |
3411 | * - must not have a pmu::count method | |
3412 | */ | |
3413 | u64 perf_event_read_local(struct perf_event *event) | |
3414 | { | |
3415 | unsigned long flags; | |
3416 | u64 val; | |
3417 | ||
3418 | /* | |
3419 | * Disabling interrupts avoids all counter scheduling (context | |
3420 | * switches, timer based rotation and IPIs). | |
3421 | */ | |
3422 | local_irq_save(flags); | |
3423 | ||
3424 | /* If this is a per-task event, it must be for current */ | |
3425 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && | |
3426 | event->hw.target != current); | |
3427 | ||
3428 | /* If this is a per-CPU event, it must be for this CPU */ | |
3429 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && | |
3430 | event->cpu != smp_processor_id()); | |
3431 | ||
3432 | /* | |
3433 | * It must not be an event with inherit set, we cannot read | |
3434 | * all child counters from atomic context. | |
3435 | */ | |
3436 | WARN_ON_ONCE(event->attr.inherit); | |
3437 | ||
3438 | /* | |
3439 | * It must not have a pmu::count method, those are not | |
3440 | * NMI safe. | |
3441 | */ | |
3442 | WARN_ON_ONCE(event->pmu->count); | |
3443 | ||
3444 | /* | |
3445 | * If the event is currently on this CPU, its either a per-task event, | |
3446 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3447 | * oncpu == -1). | |
3448 | */ | |
3449 | if (event->oncpu == smp_processor_id()) | |
3450 | event->pmu->read(event); | |
3451 | ||
3452 | val = local64_read(&event->count); | |
3453 | local_irq_restore(flags); | |
3454 | ||
3455 | return val; | |
3456 | } | |
3457 | ||
7d88962e | 3458 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3459 | { |
7d88962e SB |
3460 | int ret = 0; |
3461 | ||
0793a61d | 3462 | /* |
cdd6c482 IM |
3463 | * If event is enabled and currently active on a CPU, update the |
3464 | * value in the event structure: | |
0793a61d | 3465 | */ |
cdd6c482 | 3466 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
0492d4c5 PZ |
3467 | struct perf_read_data data = { |
3468 | .event = event, | |
3469 | .group = group, | |
7d88962e | 3470 | .ret = 0, |
0492d4c5 | 3471 | }; |
cdd6c482 | 3472 | smp_call_function_single(event->oncpu, |
0492d4c5 | 3473 | __perf_event_read, &data, 1); |
7d88962e | 3474 | ret = data.ret; |
cdd6c482 | 3475 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2b8988c9 PZ |
3476 | struct perf_event_context *ctx = event->ctx; |
3477 | unsigned long flags; | |
3478 | ||
e625cce1 | 3479 | raw_spin_lock_irqsave(&ctx->lock, flags); |
c530ccd9 SE |
3480 | /* |
3481 | * may read while context is not active | |
3482 | * (e.g., thread is blocked), in that case | |
3483 | * we cannot update context time | |
3484 | */ | |
e5d1367f | 3485 | if (ctx->is_active) { |
c530ccd9 | 3486 | update_context_time(ctx); |
e5d1367f SE |
3487 | update_cgrp_time_from_event(event); |
3488 | } | |
0492d4c5 PZ |
3489 | if (group) |
3490 | update_group_times(event); | |
3491 | else | |
3492 | update_event_times(event); | |
e625cce1 | 3493 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3494 | } |
7d88962e SB |
3495 | |
3496 | return ret; | |
0793a61d TG |
3497 | } |
3498 | ||
a63eaf34 | 3499 | /* |
cdd6c482 | 3500 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3501 | */ |
eb184479 | 3502 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 3503 | { |
e625cce1 | 3504 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 3505 | mutex_init(&ctx->mutex); |
2fde4f94 | 3506 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
889ff015 FW |
3507 | INIT_LIST_HEAD(&ctx->pinned_groups); |
3508 | INIT_LIST_HEAD(&ctx->flexible_groups); | |
a63eaf34 PM |
3509 | INIT_LIST_HEAD(&ctx->event_list); |
3510 | atomic_set(&ctx->refcount, 1); | |
eb184479 PZ |
3511 | } |
3512 | ||
3513 | static struct perf_event_context * | |
3514 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
3515 | { | |
3516 | struct perf_event_context *ctx; | |
3517 | ||
3518 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
3519 | if (!ctx) | |
3520 | return NULL; | |
3521 | ||
3522 | __perf_event_init_context(ctx); | |
3523 | if (task) { | |
3524 | ctx->task = task; | |
3525 | get_task_struct(task); | |
0793a61d | 3526 | } |
eb184479 PZ |
3527 | ctx->pmu = pmu; |
3528 | ||
3529 | return ctx; | |
a63eaf34 PM |
3530 | } |
3531 | ||
2ebd4ffb MH |
3532 | static struct task_struct * |
3533 | find_lively_task_by_vpid(pid_t vpid) | |
3534 | { | |
3535 | struct task_struct *task; | |
0793a61d TG |
3536 | |
3537 | rcu_read_lock(); | |
2ebd4ffb | 3538 | if (!vpid) |
0793a61d TG |
3539 | task = current; |
3540 | else | |
2ebd4ffb | 3541 | task = find_task_by_vpid(vpid); |
0793a61d TG |
3542 | if (task) |
3543 | get_task_struct(task); | |
3544 | rcu_read_unlock(); | |
3545 | ||
3546 | if (!task) | |
3547 | return ERR_PTR(-ESRCH); | |
3548 | ||
2ebd4ffb | 3549 | return task; |
2ebd4ffb MH |
3550 | } |
3551 | ||
fe4b04fa PZ |
3552 | /* |
3553 | * Returns a matching context with refcount and pincount. | |
3554 | */ | |
108b02cf | 3555 | static struct perf_event_context * |
4af57ef2 YZ |
3556 | find_get_context(struct pmu *pmu, struct task_struct *task, |
3557 | struct perf_event *event) | |
0793a61d | 3558 | { |
211de6eb | 3559 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 3560 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 3561 | void *task_ctx_data = NULL; |
25346b93 | 3562 | unsigned long flags; |
8dc85d54 | 3563 | int ctxn, err; |
4af57ef2 | 3564 | int cpu = event->cpu; |
0793a61d | 3565 | |
22a4ec72 | 3566 | if (!task) { |
cdd6c482 | 3567 | /* Must be root to operate on a CPU event: */ |
0764771d | 3568 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
3569 | return ERR_PTR(-EACCES); |
3570 | ||
0793a61d | 3571 | /* |
cdd6c482 | 3572 | * We could be clever and allow to attach a event to an |
0793a61d TG |
3573 | * offline CPU and activate it when the CPU comes up, but |
3574 | * that's for later. | |
3575 | */ | |
f6325e30 | 3576 | if (!cpu_online(cpu)) |
0793a61d TG |
3577 | return ERR_PTR(-ENODEV); |
3578 | ||
108b02cf | 3579 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 3580 | ctx = &cpuctx->ctx; |
c93f7669 | 3581 | get_ctx(ctx); |
fe4b04fa | 3582 | ++ctx->pin_count; |
0793a61d | 3583 | |
0793a61d TG |
3584 | return ctx; |
3585 | } | |
3586 | ||
8dc85d54 PZ |
3587 | err = -EINVAL; |
3588 | ctxn = pmu->task_ctx_nr; | |
3589 | if (ctxn < 0) | |
3590 | goto errout; | |
3591 | ||
4af57ef2 YZ |
3592 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
3593 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
3594 | if (!task_ctx_data) { | |
3595 | err = -ENOMEM; | |
3596 | goto errout; | |
3597 | } | |
3598 | } | |
3599 | ||
9ed6060d | 3600 | retry: |
8dc85d54 | 3601 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 3602 | if (ctx) { |
211de6eb | 3603 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 3604 | ++ctx->pin_count; |
4af57ef2 YZ |
3605 | |
3606 | if (task_ctx_data && !ctx->task_ctx_data) { | |
3607 | ctx->task_ctx_data = task_ctx_data; | |
3608 | task_ctx_data = NULL; | |
3609 | } | |
e625cce1 | 3610 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
3611 | |
3612 | if (clone_ctx) | |
3613 | put_ctx(clone_ctx); | |
9137fb28 | 3614 | } else { |
eb184479 | 3615 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
3616 | err = -ENOMEM; |
3617 | if (!ctx) | |
3618 | goto errout; | |
eb184479 | 3619 | |
4af57ef2 YZ |
3620 | if (task_ctx_data) { |
3621 | ctx->task_ctx_data = task_ctx_data; | |
3622 | task_ctx_data = NULL; | |
3623 | } | |
3624 | ||
dbe08d82 ON |
3625 | err = 0; |
3626 | mutex_lock(&task->perf_event_mutex); | |
3627 | /* | |
3628 | * If it has already passed perf_event_exit_task(). | |
3629 | * we must see PF_EXITING, it takes this mutex too. | |
3630 | */ | |
3631 | if (task->flags & PF_EXITING) | |
3632 | err = -ESRCH; | |
3633 | else if (task->perf_event_ctxp[ctxn]) | |
3634 | err = -EAGAIN; | |
fe4b04fa | 3635 | else { |
9137fb28 | 3636 | get_ctx(ctx); |
fe4b04fa | 3637 | ++ctx->pin_count; |
dbe08d82 | 3638 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 3639 | } |
dbe08d82 ON |
3640 | mutex_unlock(&task->perf_event_mutex); |
3641 | ||
3642 | if (unlikely(err)) { | |
9137fb28 | 3643 | put_ctx(ctx); |
dbe08d82 ON |
3644 | |
3645 | if (err == -EAGAIN) | |
3646 | goto retry; | |
3647 | goto errout; | |
a63eaf34 PM |
3648 | } |
3649 | } | |
3650 | ||
4af57ef2 | 3651 | kfree(task_ctx_data); |
0793a61d | 3652 | return ctx; |
c93f7669 | 3653 | |
9ed6060d | 3654 | errout: |
4af57ef2 | 3655 | kfree(task_ctx_data); |
c93f7669 | 3656 | return ERR_PTR(err); |
0793a61d TG |
3657 | } |
3658 | ||
6fb2915d | 3659 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 3660 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 3661 | |
cdd6c482 | 3662 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 3663 | { |
cdd6c482 | 3664 | struct perf_event *event; |
592903cd | 3665 | |
cdd6c482 IM |
3666 | event = container_of(head, struct perf_event, rcu_head); |
3667 | if (event->ns) | |
3668 | put_pid_ns(event->ns); | |
6fb2915d | 3669 | perf_event_free_filter(event); |
cdd6c482 | 3670 | kfree(event); |
592903cd PZ |
3671 | } |
3672 | ||
b69cf536 PZ |
3673 | static void ring_buffer_attach(struct perf_event *event, |
3674 | struct ring_buffer *rb); | |
925d519a | 3675 | |
f2fb6bef KL |
3676 | static void detach_sb_event(struct perf_event *event) |
3677 | { | |
3678 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
3679 | ||
3680 | raw_spin_lock(&pel->lock); | |
3681 | list_del_rcu(&event->sb_list); | |
3682 | raw_spin_unlock(&pel->lock); | |
3683 | } | |
3684 | ||
3685 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
3686 | { | |
3687 | if (event->parent) | |
3688 | return; | |
3689 | ||
3690 | if (event->attach_state & PERF_ATTACH_TASK) | |
3691 | return; | |
3692 | ||
3693 | detach_sb_event(event); | |
3694 | } | |
3695 | ||
4beb31f3 | 3696 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 3697 | { |
4beb31f3 FW |
3698 | if (event->parent) |
3699 | return; | |
3700 | ||
4beb31f3 FW |
3701 | if (is_cgroup_event(event)) |
3702 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
3703 | } | |
925d519a | 3704 | |
555e0c1e FW |
3705 | #ifdef CONFIG_NO_HZ_FULL |
3706 | static DEFINE_SPINLOCK(nr_freq_lock); | |
3707 | #endif | |
3708 | ||
3709 | static void unaccount_freq_event_nohz(void) | |
3710 | { | |
3711 | #ifdef CONFIG_NO_HZ_FULL | |
3712 | spin_lock(&nr_freq_lock); | |
3713 | if (atomic_dec_and_test(&nr_freq_events)) | |
3714 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
3715 | spin_unlock(&nr_freq_lock); | |
3716 | #endif | |
3717 | } | |
3718 | ||
3719 | static void unaccount_freq_event(void) | |
3720 | { | |
3721 | if (tick_nohz_full_enabled()) | |
3722 | unaccount_freq_event_nohz(); | |
3723 | else | |
3724 | atomic_dec(&nr_freq_events); | |
3725 | } | |
3726 | ||
4beb31f3 FW |
3727 | static void unaccount_event(struct perf_event *event) |
3728 | { | |
25432ae9 PZ |
3729 | bool dec = false; |
3730 | ||
4beb31f3 FW |
3731 | if (event->parent) |
3732 | return; | |
3733 | ||
3734 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 3735 | dec = true; |
4beb31f3 FW |
3736 | if (event->attr.mmap || event->attr.mmap_data) |
3737 | atomic_dec(&nr_mmap_events); | |
3738 | if (event->attr.comm) | |
3739 | atomic_dec(&nr_comm_events); | |
3740 | if (event->attr.task) | |
3741 | atomic_dec(&nr_task_events); | |
948b26b6 | 3742 | if (event->attr.freq) |
555e0c1e | 3743 | unaccount_freq_event(); |
45ac1403 | 3744 | if (event->attr.context_switch) { |
25432ae9 | 3745 | dec = true; |
45ac1403 AH |
3746 | atomic_dec(&nr_switch_events); |
3747 | } | |
4beb31f3 | 3748 | if (is_cgroup_event(event)) |
25432ae9 | 3749 | dec = true; |
4beb31f3 | 3750 | if (has_branch_stack(event)) |
25432ae9 PZ |
3751 | dec = true; |
3752 | ||
9107c89e PZ |
3753 | if (dec) { |
3754 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
3755 | schedule_delayed_work(&perf_sched_work, HZ); | |
3756 | } | |
4beb31f3 FW |
3757 | |
3758 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
3759 | |
3760 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 3761 | } |
925d519a | 3762 | |
9107c89e PZ |
3763 | static void perf_sched_delayed(struct work_struct *work) |
3764 | { | |
3765 | mutex_lock(&perf_sched_mutex); | |
3766 | if (atomic_dec_and_test(&perf_sched_count)) | |
3767 | static_branch_disable(&perf_sched_events); | |
3768 | mutex_unlock(&perf_sched_mutex); | |
3769 | } | |
3770 | ||
bed5b25a AS |
3771 | /* |
3772 | * The following implement mutual exclusion of events on "exclusive" pmus | |
3773 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
3774 | * at a time, so we disallow creating events that might conflict, namely: | |
3775 | * | |
3776 | * 1) cpu-wide events in the presence of per-task events, | |
3777 | * 2) per-task events in the presence of cpu-wide events, | |
3778 | * 3) two matching events on the same context. | |
3779 | * | |
3780 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 3781 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
3782 | */ |
3783 | static int exclusive_event_init(struct perf_event *event) | |
3784 | { | |
3785 | struct pmu *pmu = event->pmu; | |
3786 | ||
3787 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3788 | return 0; | |
3789 | ||
3790 | /* | |
3791 | * Prevent co-existence of per-task and cpu-wide events on the | |
3792 | * same exclusive pmu. | |
3793 | * | |
3794 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
3795 | * events on this "exclusive" pmu, positive means there are | |
3796 | * per-task events. | |
3797 | * | |
3798 | * Since this is called in perf_event_alloc() path, event::ctx | |
3799 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
3800 | * to mean "per-task event", because unlike other attach states it | |
3801 | * never gets cleared. | |
3802 | */ | |
3803 | if (event->attach_state & PERF_ATTACH_TASK) { | |
3804 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
3805 | return -EBUSY; | |
3806 | } else { | |
3807 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
3808 | return -EBUSY; | |
3809 | } | |
3810 | ||
3811 | return 0; | |
3812 | } | |
3813 | ||
3814 | static void exclusive_event_destroy(struct perf_event *event) | |
3815 | { | |
3816 | struct pmu *pmu = event->pmu; | |
3817 | ||
3818 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3819 | return; | |
3820 | ||
3821 | /* see comment in exclusive_event_init() */ | |
3822 | if (event->attach_state & PERF_ATTACH_TASK) | |
3823 | atomic_dec(&pmu->exclusive_cnt); | |
3824 | else | |
3825 | atomic_inc(&pmu->exclusive_cnt); | |
3826 | } | |
3827 | ||
3828 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
3829 | { | |
3830 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && | |
3831 | (e1->cpu == e2->cpu || | |
3832 | e1->cpu == -1 || | |
3833 | e2->cpu == -1)) | |
3834 | return true; | |
3835 | return false; | |
3836 | } | |
3837 | ||
3838 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
3839 | static bool exclusive_event_installable(struct perf_event *event, | |
3840 | struct perf_event_context *ctx) | |
3841 | { | |
3842 | struct perf_event *iter_event; | |
3843 | struct pmu *pmu = event->pmu; | |
3844 | ||
3845 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3846 | return true; | |
3847 | ||
3848 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
3849 | if (exclusive_event_match(iter_event, event)) | |
3850 | return false; | |
3851 | } | |
3852 | ||
3853 | return true; | |
3854 | } | |
3855 | ||
375637bc AS |
3856 | static void perf_addr_filters_splice(struct perf_event *event, |
3857 | struct list_head *head); | |
3858 | ||
683ede43 | 3859 | static void _free_event(struct perf_event *event) |
f1600952 | 3860 | { |
e360adbe | 3861 | irq_work_sync(&event->pending); |
925d519a | 3862 | |
4beb31f3 | 3863 | unaccount_event(event); |
9ee318a7 | 3864 | |
76369139 | 3865 | if (event->rb) { |
9bb5d40c PZ |
3866 | /* |
3867 | * Can happen when we close an event with re-directed output. | |
3868 | * | |
3869 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
3870 | * over us; possibly making our ring_buffer_put() the last. | |
3871 | */ | |
3872 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 3873 | ring_buffer_attach(event, NULL); |
9bb5d40c | 3874 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
3875 | } |
3876 | ||
e5d1367f SE |
3877 | if (is_cgroup_event(event)) |
3878 | perf_detach_cgroup(event); | |
3879 | ||
a0733e69 PZ |
3880 | if (!event->parent) { |
3881 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
3882 | put_callchain_buffers(); | |
3883 | } | |
3884 | ||
3885 | perf_event_free_bpf_prog(event); | |
375637bc AS |
3886 | perf_addr_filters_splice(event, NULL); |
3887 | kfree(event->addr_filters_offs); | |
a0733e69 PZ |
3888 | |
3889 | if (event->destroy) | |
3890 | event->destroy(event); | |
3891 | ||
3892 | if (event->ctx) | |
3893 | put_ctx(event->ctx); | |
3894 | ||
3895 | if (event->pmu) { | |
3896 | exclusive_event_destroy(event); | |
3897 | module_put(event->pmu->module); | |
3898 | } | |
3899 | ||
3900 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
3901 | } |
3902 | ||
683ede43 PZ |
3903 | /* |
3904 | * Used to free events which have a known refcount of 1, such as in error paths | |
3905 | * where the event isn't exposed yet and inherited events. | |
3906 | */ | |
3907 | static void free_event(struct perf_event *event) | |
0793a61d | 3908 | { |
683ede43 PZ |
3909 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
3910 | "unexpected event refcount: %ld; ptr=%p\n", | |
3911 | atomic_long_read(&event->refcount), event)) { | |
3912 | /* leak to avoid use-after-free */ | |
3913 | return; | |
3914 | } | |
0793a61d | 3915 | |
683ede43 | 3916 | _free_event(event); |
0793a61d TG |
3917 | } |
3918 | ||
a66a3052 | 3919 | /* |
f8697762 | 3920 | * Remove user event from the owner task. |
a66a3052 | 3921 | */ |
f8697762 | 3922 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 3923 | { |
8882135b | 3924 | struct task_struct *owner; |
fb0459d7 | 3925 | |
8882135b | 3926 | rcu_read_lock(); |
8882135b | 3927 | /* |
f47c02c0 PZ |
3928 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
3929 | * observe !owner it means the list deletion is complete and we can | |
3930 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
3931 | * owner->perf_event_mutex. |
3932 | */ | |
f47c02c0 | 3933 | owner = lockless_dereference(event->owner); |
8882135b PZ |
3934 | if (owner) { |
3935 | /* | |
3936 | * Since delayed_put_task_struct() also drops the last | |
3937 | * task reference we can safely take a new reference | |
3938 | * while holding the rcu_read_lock(). | |
3939 | */ | |
3940 | get_task_struct(owner); | |
3941 | } | |
3942 | rcu_read_unlock(); | |
3943 | ||
3944 | if (owner) { | |
f63a8daa PZ |
3945 | /* |
3946 | * If we're here through perf_event_exit_task() we're already | |
3947 | * holding ctx->mutex which would be an inversion wrt. the | |
3948 | * normal lock order. | |
3949 | * | |
3950 | * However we can safely take this lock because its the child | |
3951 | * ctx->mutex. | |
3952 | */ | |
3953 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
3954 | ||
8882135b PZ |
3955 | /* |
3956 | * We have to re-check the event->owner field, if it is cleared | |
3957 | * we raced with perf_event_exit_task(), acquiring the mutex | |
3958 | * ensured they're done, and we can proceed with freeing the | |
3959 | * event. | |
3960 | */ | |
f47c02c0 | 3961 | if (event->owner) { |
8882135b | 3962 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
3963 | smp_store_release(&event->owner, NULL); |
3964 | } | |
8882135b PZ |
3965 | mutex_unlock(&owner->perf_event_mutex); |
3966 | put_task_struct(owner); | |
3967 | } | |
f8697762 JO |
3968 | } |
3969 | ||
f8697762 JO |
3970 | static void put_event(struct perf_event *event) |
3971 | { | |
f8697762 JO |
3972 | if (!atomic_long_dec_and_test(&event->refcount)) |
3973 | return; | |
3974 | ||
c6e5b732 PZ |
3975 | _free_event(event); |
3976 | } | |
3977 | ||
3978 | /* | |
3979 | * Kill an event dead; while event:refcount will preserve the event | |
3980 | * object, it will not preserve its functionality. Once the last 'user' | |
3981 | * gives up the object, we'll destroy the thing. | |
3982 | */ | |
3983 | int perf_event_release_kernel(struct perf_event *event) | |
3984 | { | |
a4f4bb6d | 3985 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 PZ |
3986 | struct perf_event *child, *tmp; |
3987 | ||
a4f4bb6d PZ |
3988 | /* |
3989 | * If we got here through err_file: fput(event_file); we will not have | |
3990 | * attached to a context yet. | |
3991 | */ | |
3992 | if (!ctx) { | |
3993 | WARN_ON_ONCE(event->attach_state & | |
3994 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
3995 | goto no_ctx; | |
3996 | } | |
3997 | ||
f8697762 JO |
3998 | if (!is_kernel_event(event)) |
3999 | perf_remove_from_owner(event); | |
8882135b | 4000 | |
5fa7c8ec | 4001 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 4002 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 4003 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 4004 | |
a69b0ca4 | 4005 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 4006 | /* |
a69b0ca4 PZ |
4007 | * Mark this even as STATE_DEAD, there is no external reference to it |
4008 | * anymore. | |
683ede43 | 4009 | * |
a69b0ca4 PZ |
4010 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4011 | * also see this, most importantly inherit_event() which will avoid | |
4012 | * placing more children on the list. | |
683ede43 | 4013 | * |
c6e5b732 PZ |
4014 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4015 | * child events. | |
683ede43 | 4016 | */ |
a69b0ca4 PZ |
4017 | event->state = PERF_EVENT_STATE_DEAD; |
4018 | raw_spin_unlock_irq(&ctx->lock); | |
4019 | ||
4020 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4021 | |
c6e5b732 PZ |
4022 | again: |
4023 | mutex_lock(&event->child_mutex); | |
4024 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4025 | |
c6e5b732 PZ |
4026 | /* |
4027 | * Cannot change, child events are not migrated, see the | |
4028 | * comment with perf_event_ctx_lock_nested(). | |
4029 | */ | |
4030 | ctx = lockless_dereference(child->ctx); | |
4031 | /* | |
4032 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4033 | * through hoops. We start by grabbing a reference on the ctx. | |
4034 | * | |
4035 | * Since the event cannot get freed while we hold the | |
4036 | * child_mutex, the context must also exist and have a !0 | |
4037 | * reference count. | |
4038 | */ | |
4039 | get_ctx(ctx); | |
4040 | ||
4041 | /* | |
4042 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4043 | * acquire ctx::mutex without fear of it going away. Then we | |
4044 | * can re-acquire child_mutex. | |
4045 | */ | |
4046 | mutex_unlock(&event->child_mutex); | |
4047 | mutex_lock(&ctx->mutex); | |
4048 | mutex_lock(&event->child_mutex); | |
4049 | ||
4050 | /* | |
4051 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4052 | * state, if child is still the first entry, it didn't get freed | |
4053 | * and we can continue doing so. | |
4054 | */ | |
4055 | tmp = list_first_entry_or_null(&event->child_list, | |
4056 | struct perf_event, child_list); | |
4057 | if (tmp == child) { | |
4058 | perf_remove_from_context(child, DETACH_GROUP); | |
4059 | list_del(&child->child_list); | |
4060 | free_event(child); | |
4061 | /* | |
4062 | * This matches the refcount bump in inherit_event(); | |
4063 | * this can't be the last reference. | |
4064 | */ | |
4065 | put_event(event); | |
4066 | } | |
4067 | ||
4068 | mutex_unlock(&event->child_mutex); | |
4069 | mutex_unlock(&ctx->mutex); | |
4070 | put_ctx(ctx); | |
4071 | goto again; | |
4072 | } | |
4073 | mutex_unlock(&event->child_mutex); | |
4074 | ||
a4f4bb6d PZ |
4075 | no_ctx: |
4076 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
4077 | return 0; |
4078 | } | |
4079 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
4080 | ||
8b10c5e2 PZ |
4081 | /* |
4082 | * Called when the last reference to the file is gone. | |
4083 | */ | |
a6fa941d AV |
4084 | static int perf_release(struct inode *inode, struct file *file) |
4085 | { | |
c6e5b732 | 4086 | perf_event_release_kernel(file->private_data); |
a6fa941d | 4087 | return 0; |
fb0459d7 | 4088 | } |
fb0459d7 | 4089 | |
59ed446f | 4090 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 4091 | { |
cdd6c482 | 4092 | struct perf_event *child; |
e53c0994 PZ |
4093 | u64 total = 0; |
4094 | ||
59ed446f PZ |
4095 | *enabled = 0; |
4096 | *running = 0; | |
4097 | ||
6f10581a | 4098 | mutex_lock(&event->child_mutex); |
01add3ea | 4099 | |
7d88962e | 4100 | (void)perf_event_read(event, false); |
01add3ea SB |
4101 | total += perf_event_count(event); |
4102 | ||
59ed446f PZ |
4103 | *enabled += event->total_time_enabled + |
4104 | atomic64_read(&event->child_total_time_enabled); | |
4105 | *running += event->total_time_running + | |
4106 | atomic64_read(&event->child_total_time_running); | |
4107 | ||
4108 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4109 | (void)perf_event_read(child, false); |
01add3ea | 4110 | total += perf_event_count(child); |
59ed446f PZ |
4111 | *enabled += child->total_time_enabled; |
4112 | *running += child->total_time_running; | |
4113 | } | |
6f10581a | 4114 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4115 | |
4116 | return total; | |
4117 | } | |
fb0459d7 | 4118 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4119 | |
7d88962e | 4120 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4121 | u64 read_format, u64 *values) |
3dab77fb | 4122 | { |
fa8c2693 PZ |
4123 | struct perf_event *sub; |
4124 | int n = 1; /* skip @nr */ | |
7d88962e | 4125 | int ret; |
f63a8daa | 4126 | |
7d88962e SB |
4127 | ret = perf_event_read(leader, true); |
4128 | if (ret) | |
4129 | return ret; | |
abf4868b | 4130 | |
fa8c2693 PZ |
4131 | /* |
4132 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4133 | * will be identical to those of the leader, so we only publish one | |
4134 | * set. | |
4135 | */ | |
4136 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4137 | values[n++] += leader->total_time_enabled + | |
4138 | atomic64_read(&leader->child_total_time_enabled); | |
4139 | } | |
3dab77fb | 4140 | |
fa8c2693 PZ |
4141 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4142 | values[n++] += leader->total_time_running + | |
4143 | atomic64_read(&leader->child_total_time_running); | |
4144 | } | |
4145 | ||
4146 | /* | |
4147 | * Write {count,id} tuples for every sibling. | |
4148 | */ | |
4149 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4150 | if (read_format & PERF_FORMAT_ID) |
4151 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4152 | |
fa8c2693 PZ |
4153 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
4154 | values[n++] += perf_event_count(sub); | |
4155 | if (read_format & PERF_FORMAT_ID) | |
4156 | values[n++] = primary_event_id(sub); | |
4157 | } | |
7d88962e SB |
4158 | |
4159 | return 0; | |
fa8c2693 | 4160 | } |
3dab77fb | 4161 | |
fa8c2693 PZ |
4162 | static int perf_read_group(struct perf_event *event, |
4163 | u64 read_format, char __user *buf) | |
4164 | { | |
4165 | struct perf_event *leader = event->group_leader, *child; | |
4166 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4167 | int ret; |
fa8c2693 | 4168 | u64 *values; |
3dab77fb | 4169 | |
fa8c2693 | 4170 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4171 | |
fa8c2693 PZ |
4172 | values = kzalloc(event->read_size, GFP_KERNEL); |
4173 | if (!values) | |
4174 | return -ENOMEM; | |
3dab77fb | 4175 | |
fa8c2693 PZ |
4176 | values[0] = 1 + leader->nr_siblings; |
4177 | ||
4178 | /* | |
4179 | * By locking the child_mutex of the leader we effectively | |
4180 | * lock the child list of all siblings.. XXX explain how. | |
4181 | */ | |
4182 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4183 | |
7d88962e SB |
4184 | ret = __perf_read_group_add(leader, read_format, values); |
4185 | if (ret) | |
4186 | goto unlock; | |
4187 | ||
4188 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4189 | ret = __perf_read_group_add(child, read_format, values); | |
4190 | if (ret) | |
4191 | goto unlock; | |
4192 | } | |
abf4868b | 4193 | |
fa8c2693 | 4194 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4195 | |
7d88962e | 4196 | ret = event->read_size; |
fa8c2693 PZ |
4197 | if (copy_to_user(buf, values, event->read_size)) |
4198 | ret = -EFAULT; | |
7d88962e | 4199 | goto out; |
fa8c2693 | 4200 | |
7d88962e SB |
4201 | unlock: |
4202 | mutex_unlock(&leader->child_mutex); | |
4203 | out: | |
fa8c2693 | 4204 | kfree(values); |
abf4868b | 4205 | return ret; |
3dab77fb PZ |
4206 | } |
4207 | ||
b15f495b | 4208 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4209 | u64 read_format, char __user *buf) |
4210 | { | |
59ed446f | 4211 | u64 enabled, running; |
3dab77fb PZ |
4212 | u64 values[4]; |
4213 | int n = 0; | |
4214 | ||
59ed446f PZ |
4215 | values[n++] = perf_event_read_value(event, &enabled, &running); |
4216 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
4217 | values[n++] = enabled; | |
4218 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4219 | values[n++] = running; | |
3dab77fb | 4220 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4221 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4222 | |
4223 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4224 | return -EFAULT; | |
4225 | ||
4226 | return n * sizeof(u64); | |
4227 | } | |
4228 | ||
dc633982 JO |
4229 | static bool is_event_hup(struct perf_event *event) |
4230 | { | |
4231 | bool no_children; | |
4232 | ||
a69b0ca4 | 4233 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4234 | return false; |
4235 | ||
4236 | mutex_lock(&event->child_mutex); | |
4237 | no_children = list_empty(&event->child_list); | |
4238 | mutex_unlock(&event->child_mutex); | |
4239 | return no_children; | |
4240 | } | |
4241 | ||
0793a61d | 4242 | /* |
cdd6c482 | 4243 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4244 | */ |
4245 | static ssize_t | |
b15f495b | 4246 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4247 | { |
cdd6c482 | 4248 | u64 read_format = event->attr.read_format; |
3dab77fb | 4249 | int ret; |
0793a61d | 4250 | |
3b6f9e5c | 4251 | /* |
cdd6c482 | 4252 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4253 | * error state (i.e. because it was pinned but it couldn't be |
4254 | * scheduled on to the CPU at some point). | |
4255 | */ | |
cdd6c482 | 4256 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4257 | return 0; |
4258 | ||
c320c7b7 | 4259 | if (count < event->read_size) |
3dab77fb PZ |
4260 | return -ENOSPC; |
4261 | ||
cdd6c482 | 4262 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4263 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4264 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4265 | else |
b15f495b | 4266 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4267 | |
3dab77fb | 4268 | return ret; |
0793a61d TG |
4269 | } |
4270 | ||
0793a61d TG |
4271 | static ssize_t |
4272 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4273 | { | |
cdd6c482 | 4274 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4275 | struct perf_event_context *ctx; |
4276 | int ret; | |
0793a61d | 4277 | |
f63a8daa | 4278 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4279 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4280 | perf_event_ctx_unlock(event, ctx); |
4281 | ||
4282 | return ret; | |
0793a61d TG |
4283 | } |
4284 | ||
4285 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
4286 | { | |
cdd6c482 | 4287 | struct perf_event *event = file->private_data; |
76369139 | 4288 | struct ring_buffer *rb; |
61b67684 | 4289 | unsigned int events = POLLHUP; |
c7138f37 | 4290 | |
e708d7ad | 4291 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4292 | |
dc633982 | 4293 | if (is_event_hup(event)) |
179033b3 | 4294 | return events; |
c7138f37 | 4295 | |
10c6db11 | 4296 | /* |
9bb5d40c PZ |
4297 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4298 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4299 | */ |
4300 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4301 | rb = event->rb; |
4302 | if (rb) | |
76369139 | 4303 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4304 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4305 | return events; |
4306 | } | |
4307 | ||
f63a8daa | 4308 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4309 | { |
7d88962e | 4310 | (void)perf_event_read(event, false); |
e7850595 | 4311 | local64_set(&event->count, 0); |
cdd6c482 | 4312 | perf_event_update_userpage(event); |
3df5edad PZ |
4313 | } |
4314 | ||
c93f7669 | 4315 | /* |
cdd6c482 IM |
4316 | * Holding the top-level event's child_mutex means that any |
4317 | * descendant process that has inherited this event will block | |
8ba289b8 | 4318 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4319 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4320 | */ |
cdd6c482 IM |
4321 | static void perf_event_for_each_child(struct perf_event *event, |
4322 | void (*func)(struct perf_event *)) | |
3df5edad | 4323 | { |
cdd6c482 | 4324 | struct perf_event *child; |
3df5edad | 4325 | |
cdd6c482 | 4326 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4327 | |
cdd6c482 IM |
4328 | mutex_lock(&event->child_mutex); |
4329 | func(event); | |
4330 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4331 | func(child); |
cdd6c482 | 4332 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4333 | } |
4334 | ||
cdd6c482 IM |
4335 | static void perf_event_for_each(struct perf_event *event, |
4336 | void (*func)(struct perf_event *)) | |
3df5edad | 4337 | { |
cdd6c482 IM |
4338 | struct perf_event_context *ctx = event->ctx; |
4339 | struct perf_event *sibling; | |
3df5edad | 4340 | |
f63a8daa PZ |
4341 | lockdep_assert_held(&ctx->mutex); |
4342 | ||
cdd6c482 | 4343 | event = event->group_leader; |
75f937f2 | 4344 | |
cdd6c482 | 4345 | perf_event_for_each_child(event, func); |
cdd6c482 | 4346 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
724b6daa | 4347 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4348 | } |
4349 | ||
fae3fde6 PZ |
4350 | static void __perf_event_period(struct perf_event *event, |
4351 | struct perf_cpu_context *cpuctx, | |
4352 | struct perf_event_context *ctx, | |
4353 | void *info) | |
c7999c6f | 4354 | { |
fae3fde6 | 4355 | u64 value = *((u64 *)info); |
c7999c6f | 4356 | bool active; |
08247e31 | 4357 | |
cdd6c482 | 4358 | if (event->attr.freq) { |
cdd6c482 | 4359 | event->attr.sample_freq = value; |
08247e31 | 4360 | } else { |
cdd6c482 IM |
4361 | event->attr.sample_period = value; |
4362 | event->hw.sample_period = value; | |
08247e31 | 4363 | } |
bad7192b PZ |
4364 | |
4365 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4366 | if (active) { | |
4367 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4368 | /* |
4369 | * We could be throttled; unthrottle now to avoid the tick | |
4370 | * trying to unthrottle while we already re-started the event. | |
4371 | */ | |
4372 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4373 | event->hw.interrupts = 0; | |
4374 | perf_log_throttle(event, 1); | |
4375 | } | |
bad7192b PZ |
4376 | event->pmu->stop(event, PERF_EF_UPDATE); |
4377 | } | |
4378 | ||
4379 | local64_set(&event->hw.period_left, 0); | |
4380 | ||
4381 | if (active) { | |
4382 | event->pmu->start(event, PERF_EF_RELOAD); | |
4383 | perf_pmu_enable(ctx->pmu); | |
4384 | } | |
c7999c6f PZ |
4385 | } |
4386 | ||
4387 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4388 | { | |
c7999c6f PZ |
4389 | u64 value; |
4390 | ||
4391 | if (!is_sampling_event(event)) | |
4392 | return -EINVAL; | |
4393 | ||
4394 | if (copy_from_user(&value, arg, sizeof(value))) | |
4395 | return -EFAULT; | |
4396 | ||
4397 | if (!value) | |
4398 | return -EINVAL; | |
4399 | ||
4400 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4401 | return -EINVAL; | |
4402 | ||
fae3fde6 | 4403 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4404 | |
c7999c6f | 4405 | return 0; |
08247e31 PZ |
4406 | } |
4407 | ||
ac9721f3 PZ |
4408 | static const struct file_operations perf_fops; |
4409 | ||
2903ff01 | 4410 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4411 | { |
2903ff01 AV |
4412 | struct fd f = fdget(fd); |
4413 | if (!f.file) | |
4414 | return -EBADF; | |
ac9721f3 | 4415 | |
2903ff01 AV |
4416 | if (f.file->f_op != &perf_fops) { |
4417 | fdput(f); | |
4418 | return -EBADF; | |
ac9721f3 | 4419 | } |
2903ff01 AV |
4420 | *p = f; |
4421 | return 0; | |
ac9721f3 PZ |
4422 | } |
4423 | ||
4424 | static int perf_event_set_output(struct perf_event *event, | |
4425 | struct perf_event *output_event); | |
6fb2915d | 4426 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4427 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4428 | |
f63a8daa | 4429 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4430 | { |
cdd6c482 | 4431 | void (*func)(struct perf_event *); |
3df5edad | 4432 | u32 flags = arg; |
d859e29f PM |
4433 | |
4434 | switch (cmd) { | |
cdd6c482 | 4435 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4436 | func = _perf_event_enable; |
d859e29f | 4437 | break; |
cdd6c482 | 4438 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4439 | func = _perf_event_disable; |
79f14641 | 4440 | break; |
cdd6c482 | 4441 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4442 | func = _perf_event_reset; |
6de6a7b9 | 4443 | break; |
3df5edad | 4444 | |
cdd6c482 | 4445 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4446 | return _perf_event_refresh(event, arg); |
08247e31 | 4447 | |
cdd6c482 IM |
4448 | case PERF_EVENT_IOC_PERIOD: |
4449 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4450 | |
cf4957f1 JO |
4451 | case PERF_EVENT_IOC_ID: |
4452 | { | |
4453 | u64 id = primary_event_id(event); | |
4454 | ||
4455 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4456 | return -EFAULT; | |
4457 | return 0; | |
4458 | } | |
4459 | ||
cdd6c482 | 4460 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4461 | { |
ac9721f3 | 4462 | int ret; |
ac9721f3 | 4463 | if (arg != -1) { |
2903ff01 AV |
4464 | struct perf_event *output_event; |
4465 | struct fd output; | |
4466 | ret = perf_fget_light(arg, &output); | |
4467 | if (ret) | |
4468 | return ret; | |
4469 | output_event = output.file->private_data; | |
4470 | ret = perf_event_set_output(event, output_event); | |
4471 | fdput(output); | |
4472 | } else { | |
4473 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 4474 | } |
ac9721f3 PZ |
4475 | return ret; |
4476 | } | |
a4be7c27 | 4477 | |
6fb2915d LZ |
4478 | case PERF_EVENT_IOC_SET_FILTER: |
4479 | return perf_event_set_filter(event, (void __user *)arg); | |
4480 | ||
2541517c AS |
4481 | case PERF_EVENT_IOC_SET_BPF: |
4482 | return perf_event_set_bpf_prog(event, arg); | |
4483 | ||
86e7972f WN |
4484 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
4485 | struct ring_buffer *rb; | |
4486 | ||
4487 | rcu_read_lock(); | |
4488 | rb = rcu_dereference(event->rb); | |
4489 | if (!rb || !rb->nr_pages) { | |
4490 | rcu_read_unlock(); | |
4491 | return -EINVAL; | |
4492 | } | |
4493 | rb_toggle_paused(rb, !!arg); | |
4494 | rcu_read_unlock(); | |
4495 | return 0; | |
4496 | } | |
d859e29f | 4497 | default: |
3df5edad | 4498 | return -ENOTTY; |
d859e29f | 4499 | } |
3df5edad PZ |
4500 | |
4501 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 4502 | perf_event_for_each(event, func); |
3df5edad | 4503 | else |
cdd6c482 | 4504 | perf_event_for_each_child(event, func); |
3df5edad PZ |
4505 | |
4506 | return 0; | |
d859e29f PM |
4507 | } |
4508 | ||
f63a8daa PZ |
4509 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4510 | { | |
4511 | struct perf_event *event = file->private_data; | |
4512 | struct perf_event_context *ctx; | |
4513 | long ret; | |
4514 | ||
4515 | ctx = perf_event_ctx_lock(event); | |
4516 | ret = _perf_ioctl(event, cmd, arg); | |
4517 | perf_event_ctx_unlock(event, ctx); | |
4518 | ||
4519 | return ret; | |
4520 | } | |
4521 | ||
b3f20785 PM |
4522 | #ifdef CONFIG_COMPAT |
4523 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
4524 | unsigned long arg) | |
4525 | { | |
4526 | switch (_IOC_NR(cmd)) { | |
4527 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
4528 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
4529 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
4530 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
4531 | cmd &= ~IOCSIZE_MASK; | |
4532 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
4533 | } | |
4534 | break; | |
4535 | } | |
4536 | return perf_ioctl(file, cmd, arg); | |
4537 | } | |
4538 | #else | |
4539 | # define perf_compat_ioctl NULL | |
4540 | #endif | |
4541 | ||
cdd6c482 | 4542 | int perf_event_task_enable(void) |
771d7cde | 4543 | { |
f63a8daa | 4544 | struct perf_event_context *ctx; |
cdd6c482 | 4545 | struct perf_event *event; |
771d7cde | 4546 | |
cdd6c482 | 4547 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4548 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4549 | ctx = perf_event_ctx_lock(event); | |
4550 | perf_event_for_each_child(event, _perf_event_enable); | |
4551 | perf_event_ctx_unlock(event, ctx); | |
4552 | } | |
cdd6c482 | 4553 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4554 | |
4555 | return 0; | |
4556 | } | |
4557 | ||
cdd6c482 | 4558 | int perf_event_task_disable(void) |
771d7cde | 4559 | { |
f63a8daa | 4560 | struct perf_event_context *ctx; |
cdd6c482 | 4561 | struct perf_event *event; |
771d7cde | 4562 | |
cdd6c482 | 4563 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4564 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4565 | ctx = perf_event_ctx_lock(event); | |
4566 | perf_event_for_each_child(event, _perf_event_disable); | |
4567 | perf_event_ctx_unlock(event, ctx); | |
4568 | } | |
cdd6c482 | 4569 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4570 | |
4571 | return 0; | |
4572 | } | |
4573 | ||
cdd6c482 | 4574 | static int perf_event_index(struct perf_event *event) |
194002b2 | 4575 | { |
a4eaf7f1 PZ |
4576 | if (event->hw.state & PERF_HES_STOPPED) |
4577 | return 0; | |
4578 | ||
cdd6c482 | 4579 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
4580 | return 0; |
4581 | ||
35edc2a5 | 4582 | return event->pmu->event_idx(event); |
194002b2 PZ |
4583 | } |
4584 | ||
c4794295 | 4585 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 4586 | u64 *now, |
7f310a5d EM |
4587 | u64 *enabled, |
4588 | u64 *running) | |
c4794295 | 4589 | { |
e3f3541c | 4590 | u64 ctx_time; |
c4794295 | 4591 | |
e3f3541c PZ |
4592 | *now = perf_clock(); |
4593 | ctx_time = event->shadow_ctx_time + *now; | |
c4794295 EM |
4594 | *enabled = ctx_time - event->tstamp_enabled; |
4595 | *running = ctx_time - event->tstamp_running; | |
4596 | } | |
4597 | ||
fa731587 PZ |
4598 | static void perf_event_init_userpage(struct perf_event *event) |
4599 | { | |
4600 | struct perf_event_mmap_page *userpg; | |
4601 | struct ring_buffer *rb; | |
4602 | ||
4603 | rcu_read_lock(); | |
4604 | rb = rcu_dereference(event->rb); | |
4605 | if (!rb) | |
4606 | goto unlock; | |
4607 | ||
4608 | userpg = rb->user_page; | |
4609 | ||
4610 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
4611 | userpg->cap_bit0_is_deprecated = 1; | |
4612 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
4613 | userpg->data_offset = PAGE_SIZE; |
4614 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
4615 | |
4616 | unlock: | |
4617 | rcu_read_unlock(); | |
4618 | } | |
4619 | ||
c1317ec2 AL |
4620 | void __weak arch_perf_update_userpage( |
4621 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
4622 | { |
4623 | } | |
4624 | ||
38ff667b PZ |
4625 | /* |
4626 | * Callers need to ensure there can be no nesting of this function, otherwise | |
4627 | * the seqlock logic goes bad. We can not serialize this because the arch | |
4628 | * code calls this from NMI context. | |
4629 | */ | |
cdd6c482 | 4630 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 4631 | { |
cdd6c482 | 4632 | struct perf_event_mmap_page *userpg; |
76369139 | 4633 | struct ring_buffer *rb; |
e3f3541c | 4634 | u64 enabled, running, now; |
38ff667b PZ |
4635 | |
4636 | rcu_read_lock(); | |
5ec4c599 PZ |
4637 | rb = rcu_dereference(event->rb); |
4638 | if (!rb) | |
4639 | goto unlock; | |
4640 | ||
0d641208 EM |
4641 | /* |
4642 | * compute total_time_enabled, total_time_running | |
4643 | * based on snapshot values taken when the event | |
4644 | * was last scheduled in. | |
4645 | * | |
4646 | * we cannot simply called update_context_time() | |
4647 | * because of locking issue as we can be called in | |
4648 | * NMI context | |
4649 | */ | |
e3f3541c | 4650 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 4651 | |
76369139 | 4652 | userpg = rb->user_page; |
7b732a75 PZ |
4653 | /* |
4654 | * Disable preemption so as to not let the corresponding user-space | |
4655 | * spin too long if we get preempted. | |
4656 | */ | |
4657 | preempt_disable(); | |
37d81828 | 4658 | ++userpg->lock; |
92f22a38 | 4659 | barrier(); |
cdd6c482 | 4660 | userpg->index = perf_event_index(event); |
b5e58793 | 4661 | userpg->offset = perf_event_count(event); |
365a4038 | 4662 | if (userpg->index) |
e7850595 | 4663 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 4664 | |
0d641208 | 4665 | userpg->time_enabled = enabled + |
cdd6c482 | 4666 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 4667 | |
0d641208 | 4668 | userpg->time_running = running + |
cdd6c482 | 4669 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 4670 | |
c1317ec2 | 4671 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 4672 | |
92f22a38 | 4673 | barrier(); |
37d81828 | 4674 | ++userpg->lock; |
7b732a75 | 4675 | preempt_enable(); |
38ff667b | 4676 | unlock: |
7b732a75 | 4677 | rcu_read_unlock(); |
37d81828 PM |
4678 | } |
4679 | ||
906010b2 PZ |
4680 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
4681 | { | |
4682 | struct perf_event *event = vma->vm_file->private_data; | |
76369139 | 4683 | struct ring_buffer *rb; |
906010b2 PZ |
4684 | int ret = VM_FAULT_SIGBUS; |
4685 | ||
4686 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
4687 | if (vmf->pgoff == 0) | |
4688 | ret = 0; | |
4689 | return ret; | |
4690 | } | |
4691 | ||
4692 | rcu_read_lock(); | |
76369139 FW |
4693 | rb = rcu_dereference(event->rb); |
4694 | if (!rb) | |
906010b2 PZ |
4695 | goto unlock; |
4696 | ||
4697 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
4698 | goto unlock; | |
4699 | ||
76369139 | 4700 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
4701 | if (!vmf->page) |
4702 | goto unlock; | |
4703 | ||
4704 | get_page(vmf->page); | |
4705 | vmf->page->mapping = vma->vm_file->f_mapping; | |
4706 | vmf->page->index = vmf->pgoff; | |
4707 | ||
4708 | ret = 0; | |
4709 | unlock: | |
4710 | rcu_read_unlock(); | |
4711 | ||
4712 | return ret; | |
4713 | } | |
4714 | ||
10c6db11 PZ |
4715 | static void ring_buffer_attach(struct perf_event *event, |
4716 | struct ring_buffer *rb) | |
4717 | { | |
b69cf536 | 4718 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
4719 | unsigned long flags; |
4720 | ||
b69cf536 PZ |
4721 | if (event->rb) { |
4722 | /* | |
4723 | * Should be impossible, we set this when removing | |
4724 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
4725 | */ | |
4726 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 4727 | |
b69cf536 | 4728 | old_rb = event->rb; |
b69cf536 PZ |
4729 | spin_lock_irqsave(&old_rb->event_lock, flags); |
4730 | list_del_rcu(&event->rb_entry); | |
4731 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 4732 | |
2f993cf0 ON |
4733 | event->rcu_batches = get_state_synchronize_rcu(); |
4734 | event->rcu_pending = 1; | |
b69cf536 | 4735 | } |
10c6db11 | 4736 | |
b69cf536 | 4737 | if (rb) { |
2f993cf0 ON |
4738 | if (event->rcu_pending) { |
4739 | cond_synchronize_rcu(event->rcu_batches); | |
4740 | event->rcu_pending = 0; | |
4741 | } | |
4742 | ||
b69cf536 PZ |
4743 | spin_lock_irqsave(&rb->event_lock, flags); |
4744 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
4745 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
4746 | } | |
4747 | ||
4748 | rcu_assign_pointer(event->rb, rb); | |
4749 | ||
4750 | if (old_rb) { | |
4751 | ring_buffer_put(old_rb); | |
4752 | /* | |
4753 | * Since we detached before setting the new rb, so that we | |
4754 | * could attach the new rb, we could have missed a wakeup. | |
4755 | * Provide it now. | |
4756 | */ | |
4757 | wake_up_all(&event->waitq); | |
4758 | } | |
10c6db11 PZ |
4759 | } |
4760 | ||
4761 | static void ring_buffer_wakeup(struct perf_event *event) | |
4762 | { | |
4763 | struct ring_buffer *rb; | |
4764 | ||
4765 | rcu_read_lock(); | |
4766 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
4767 | if (rb) { |
4768 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
4769 | wake_up_all(&event->waitq); | |
4770 | } | |
10c6db11 PZ |
4771 | rcu_read_unlock(); |
4772 | } | |
4773 | ||
fdc26706 | 4774 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 4775 | { |
76369139 | 4776 | struct ring_buffer *rb; |
7b732a75 | 4777 | |
ac9721f3 | 4778 | rcu_read_lock(); |
76369139 FW |
4779 | rb = rcu_dereference(event->rb); |
4780 | if (rb) { | |
4781 | if (!atomic_inc_not_zero(&rb->refcount)) | |
4782 | rb = NULL; | |
ac9721f3 PZ |
4783 | } |
4784 | rcu_read_unlock(); | |
4785 | ||
76369139 | 4786 | return rb; |
ac9721f3 PZ |
4787 | } |
4788 | ||
fdc26706 | 4789 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 4790 | { |
76369139 | 4791 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 4792 | return; |
7b732a75 | 4793 | |
9bb5d40c | 4794 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 4795 | |
76369139 | 4796 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
4797 | } |
4798 | ||
4799 | static void perf_mmap_open(struct vm_area_struct *vma) | |
4800 | { | |
cdd6c482 | 4801 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4802 | |
cdd6c482 | 4803 | atomic_inc(&event->mmap_count); |
9bb5d40c | 4804 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 4805 | |
45bfb2e5 PZ |
4806 | if (vma->vm_pgoff) |
4807 | atomic_inc(&event->rb->aux_mmap_count); | |
4808 | ||
1e0fb9ec AL |
4809 | if (event->pmu->event_mapped) |
4810 | event->pmu->event_mapped(event); | |
7b732a75 PZ |
4811 | } |
4812 | ||
95ff4ca2 AS |
4813 | static void perf_pmu_output_stop(struct perf_event *event); |
4814 | ||
9bb5d40c PZ |
4815 | /* |
4816 | * A buffer can be mmap()ed multiple times; either directly through the same | |
4817 | * event, or through other events by use of perf_event_set_output(). | |
4818 | * | |
4819 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
4820 | * the buffer here, where we still have a VM context. This means we need | |
4821 | * to detach all events redirecting to us. | |
4822 | */ | |
7b732a75 PZ |
4823 | static void perf_mmap_close(struct vm_area_struct *vma) |
4824 | { | |
cdd6c482 | 4825 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4826 | |
b69cf536 | 4827 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
4828 | struct user_struct *mmap_user = rb->mmap_user; |
4829 | int mmap_locked = rb->mmap_locked; | |
4830 | unsigned long size = perf_data_size(rb); | |
789f90fc | 4831 | |
1e0fb9ec AL |
4832 | if (event->pmu->event_unmapped) |
4833 | event->pmu->event_unmapped(event); | |
4834 | ||
45bfb2e5 PZ |
4835 | /* |
4836 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
4837 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
4838 | * serialize with perf_mmap here. | |
4839 | */ | |
4840 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
4841 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
4842 | /* |
4843 | * Stop all AUX events that are writing to this buffer, | |
4844 | * so that we can free its AUX pages and corresponding PMU | |
4845 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
4846 | * they won't start any more (see perf_aux_output_begin()). | |
4847 | */ | |
4848 | perf_pmu_output_stop(event); | |
4849 | ||
4850 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
4851 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
4852 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
4853 | ||
95ff4ca2 | 4854 | /* this has to be the last one */ |
45bfb2e5 | 4855 | rb_free_aux(rb); |
95ff4ca2 AS |
4856 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
4857 | ||
45bfb2e5 PZ |
4858 | mutex_unlock(&event->mmap_mutex); |
4859 | } | |
4860 | ||
9bb5d40c PZ |
4861 | atomic_dec(&rb->mmap_count); |
4862 | ||
4863 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 4864 | goto out_put; |
9bb5d40c | 4865 | |
b69cf536 | 4866 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
4867 | mutex_unlock(&event->mmap_mutex); |
4868 | ||
4869 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
4870 | if (atomic_read(&rb->mmap_count)) |
4871 | goto out_put; | |
ac9721f3 | 4872 | |
9bb5d40c PZ |
4873 | /* |
4874 | * No other mmap()s, detach from all other events that might redirect | |
4875 | * into the now unreachable buffer. Somewhat complicated by the | |
4876 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
4877 | */ | |
4878 | again: | |
4879 | rcu_read_lock(); | |
4880 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
4881 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
4882 | /* | |
4883 | * This event is en-route to free_event() which will | |
4884 | * detach it and remove it from the list. | |
4885 | */ | |
4886 | continue; | |
4887 | } | |
4888 | rcu_read_unlock(); | |
789f90fc | 4889 | |
9bb5d40c PZ |
4890 | mutex_lock(&event->mmap_mutex); |
4891 | /* | |
4892 | * Check we didn't race with perf_event_set_output() which can | |
4893 | * swizzle the rb from under us while we were waiting to | |
4894 | * acquire mmap_mutex. | |
4895 | * | |
4896 | * If we find a different rb; ignore this event, a next | |
4897 | * iteration will no longer find it on the list. We have to | |
4898 | * still restart the iteration to make sure we're not now | |
4899 | * iterating the wrong list. | |
4900 | */ | |
b69cf536 PZ |
4901 | if (event->rb == rb) |
4902 | ring_buffer_attach(event, NULL); | |
4903 | ||
cdd6c482 | 4904 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 4905 | put_event(event); |
ac9721f3 | 4906 | |
9bb5d40c PZ |
4907 | /* |
4908 | * Restart the iteration; either we're on the wrong list or | |
4909 | * destroyed its integrity by doing a deletion. | |
4910 | */ | |
4911 | goto again; | |
7b732a75 | 4912 | } |
9bb5d40c PZ |
4913 | rcu_read_unlock(); |
4914 | ||
4915 | /* | |
4916 | * It could be there's still a few 0-ref events on the list; they'll | |
4917 | * get cleaned up by free_event() -- they'll also still have their | |
4918 | * ref on the rb and will free it whenever they are done with it. | |
4919 | * | |
4920 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
4921 | * undo the VM accounting. | |
4922 | */ | |
4923 | ||
4924 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
4925 | vma->vm_mm->pinned_vm -= mmap_locked; | |
4926 | free_uid(mmap_user); | |
4927 | ||
b69cf536 | 4928 | out_put: |
9bb5d40c | 4929 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
4930 | } |
4931 | ||
f0f37e2f | 4932 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 4933 | .open = perf_mmap_open, |
45bfb2e5 | 4934 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
4935 | .fault = perf_mmap_fault, |
4936 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
4937 | }; |
4938 | ||
4939 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
4940 | { | |
cdd6c482 | 4941 | struct perf_event *event = file->private_data; |
22a4f650 | 4942 | unsigned long user_locked, user_lock_limit; |
789f90fc | 4943 | struct user_struct *user = current_user(); |
22a4f650 | 4944 | unsigned long locked, lock_limit; |
45bfb2e5 | 4945 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
4946 | unsigned long vma_size; |
4947 | unsigned long nr_pages; | |
45bfb2e5 | 4948 | long user_extra = 0, extra = 0; |
d57e34fd | 4949 | int ret = 0, flags = 0; |
37d81828 | 4950 | |
c7920614 PZ |
4951 | /* |
4952 | * Don't allow mmap() of inherited per-task counters. This would | |
4953 | * create a performance issue due to all children writing to the | |
76369139 | 4954 | * same rb. |
c7920614 PZ |
4955 | */ |
4956 | if (event->cpu == -1 && event->attr.inherit) | |
4957 | return -EINVAL; | |
4958 | ||
43a21ea8 | 4959 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 4960 | return -EINVAL; |
7b732a75 PZ |
4961 | |
4962 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
4963 | |
4964 | if (vma->vm_pgoff == 0) { | |
4965 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
4966 | } else { | |
4967 | /* | |
4968 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
4969 | * mapped, all subsequent mappings should have the same size | |
4970 | * and offset. Must be above the normal perf buffer. | |
4971 | */ | |
4972 | u64 aux_offset, aux_size; | |
4973 | ||
4974 | if (!event->rb) | |
4975 | return -EINVAL; | |
4976 | ||
4977 | nr_pages = vma_size / PAGE_SIZE; | |
4978 | ||
4979 | mutex_lock(&event->mmap_mutex); | |
4980 | ret = -EINVAL; | |
4981 | ||
4982 | rb = event->rb; | |
4983 | if (!rb) | |
4984 | goto aux_unlock; | |
4985 | ||
4986 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); | |
4987 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); | |
4988 | ||
4989 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
4990 | goto aux_unlock; | |
4991 | ||
4992 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
4993 | goto aux_unlock; | |
4994 | ||
4995 | /* already mapped with a different offset */ | |
4996 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
4997 | goto aux_unlock; | |
4998 | ||
4999 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
5000 | goto aux_unlock; | |
5001 | ||
5002 | /* already mapped with a different size */ | |
5003 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
5004 | goto aux_unlock; | |
5005 | ||
5006 | if (!is_power_of_2(nr_pages)) | |
5007 | goto aux_unlock; | |
5008 | ||
5009 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
5010 | goto aux_unlock; | |
5011 | ||
5012 | if (rb_has_aux(rb)) { | |
5013 | atomic_inc(&rb->aux_mmap_count); | |
5014 | ret = 0; | |
5015 | goto unlock; | |
5016 | } | |
5017 | ||
5018 | atomic_set(&rb->aux_mmap_count, 1); | |
5019 | user_extra = nr_pages; | |
5020 | ||
5021 | goto accounting; | |
5022 | } | |
7b732a75 | 5023 | |
7730d865 | 5024 | /* |
76369139 | 5025 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
5026 | * can do bitmasks instead of modulo. |
5027 | */ | |
2ed11312 | 5028 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
5029 | return -EINVAL; |
5030 | ||
7b732a75 | 5031 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
5032 | return -EINVAL; |
5033 | ||
cdd6c482 | 5034 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 5035 | again: |
cdd6c482 | 5036 | mutex_lock(&event->mmap_mutex); |
76369139 | 5037 | if (event->rb) { |
9bb5d40c | 5038 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 5039 | ret = -EINVAL; |
9bb5d40c PZ |
5040 | goto unlock; |
5041 | } | |
5042 | ||
5043 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
5044 | /* | |
5045 | * Raced against perf_mmap_close() through | |
5046 | * perf_event_set_output(). Try again, hope for better | |
5047 | * luck. | |
5048 | */ | |
5049 | mutex_unlock(&event->mmap_mutex); | |
5050 | goto again; | |
5051 | } | |
5052 | ||
ebb3c4c4 PZ |
5053 | goto unlock; |
5054 | } | |
5055 | ||
789f90fc | 5056 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
5057 | |
5058 | accounting: | |
cdd6c482 | 5059 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
5060 | |
5061 | /* | |
5062 | * Increase the limit linearly with more CPUs: | |
5063 | */ | |
5064 | user_lock_limit *= num_online_cpus(); | |
5065 | ||
789f90fc | 5066 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 5067 | |
789f90fc PZ |
5068 | if (user_locked > user_lock_limit) |
5069 | extra = user_locked - user_lock_limit; | |
7b732a75 | 5070 | |
78d7d407 | 5071 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 5072 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 5073 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 5074 | |
459ec28a IM |
5075 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
5076 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
5077 | ret = -EPERM; |
5078 | goto unlock; | |
5079 | } | |
7b732a75 | 5080 | |
45bfb2e5 | 5081 | WARN_ON(!rb && event->rb); |
906010b2 | 5082 | |
d57e34fd | 5083 | if (vma->vm_flags & VM_WRITE) |
76369139 | 5084 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 5085 | |
76369139 | 5086 | if (!rb) { |
45bfb2e5 PZ |
5087 | rb = rb_alloc(nr_pages, |
5088 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
5089 | event->cpu, flags); | |
26cb63ad | 5090 | |
45bfb2e5 PZ |
5091 | if (!rb) { |
5092 | ret = -ENOMEM; | |
5093 | goto unlock; | |
5094 | } | |
43a21ea8 | 5095 | |
45bfb2e5 PZ |
5096 | atomic_set(&rb->mmap_count, 1); |
5097 | rb->mmap_user = get_current_user(); | |
5098 | rb->mmap_locked = extra; | |
26cb63ad | 5099 | |
45bfb2e5 | 5100 | ring_buffer_attach(event, rb); |
ac9721f3 | 5101 | |
45bfb2e5 PZ |
5102 | perf_event_init_userpage(event); |
5103 | perf_event_update_userpage(event); | |
5104 | } else { | |
1a594131 AS |
5105 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5106 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5107 | if (!ret) |
5108 | rb->aux_mmap_locked = extra; | |
5109 | } | |
9a0f05cb | 5110 | |
ebb3c4c4 | 5111 | unlock: |
45bfb2e5 PZ |
5112 | if (!ret) { |
5113 | atomic_long_add(user_extra, &user->locked_vm); | |
5114 | vma->vm_mm->pinned_vm += extra; | |
5115 | ||
ac9721f3 | 5116 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5117 | } else if (rb) { |
5118 | atomic_dec(&rb->mmap_count); | |
5119 | } | |
5120 | aux_unlock: | |
cdd6c482 | 5121 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5122 | |
9bb5d40c PZ |
5123 | /* |
5124 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5125 | * vma. | |
5126 | */ | |
26cb63ad | 5127 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5128 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5129 | |
1e0fb9ec AL |
5130 | if (event->pmu->event_mapped) |
5131 | event->pmu->event_mapped(event); | |
5132 | ||
7b732a75 | 5133 | return ret; |
37d81828 PM |
5134 | } |
5135 | ||
3c446b3d PZ |
5136 | static int perf_fasync(int fd, struct file *filp, int on) |
5137 | { | |
496ad9aa | 5138 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5139 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5140 | int retval; |
5141 | ||
5955102c | 5142 | inode_lock(inode); |
cdd6c482 | 5143 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5144 | inode_unlock(inode); |
3c446b3d PZ |
5145 | |
5146 | if (retval < 0) | |
5147 | return retval; | |
5148 | ||
5149 | return 0; | |
5150 | } | |
5151 | ||
0793a61d | 5152 | static const struct file_operations perf_fops = { |
3326c1ce | 5153 | .llseek = no_llseek, |
0793a61d TG |
5154 | .release = perf_release, |
5155 | .read = perf_read, | |
5156 | .poll = perf_poll, | |
d859e29f | 5157 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5158 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5159 | .mmap = perf_mmap, |
3c446b3d | 5160 | .fasync = perf_fasync, |
0793a61d TG |
5161 | }; |
5162 | ||
925d519a | 5163 | /* |
cdd6c482 | 5164 | * Perf event wakeup |
925d519a PZ |
5165 | * |
5166 | * If there's data, ensure we set the poll() state and publish everything | |
5167 | * to user-space before waking everybody up. | |
5168 | */ | |
5169 | ||
fed66e2c PZ |
5170 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5171 | { | |
5172 | /* only the parent has fasync state */ | |
5173 | if (event->parent) | |
5174 | event = event->parent; | |
5175 | return &event->fasync; | |
5176 | } | |
5177 | ||
cdd6c482 | 5178 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5179 | { |
10c6db11 | 5180 | ring_buffer_wakeup(event); |
4c9e2542 | 5181 | |
cdd6c482 | 5182 | if (event->pending_kill) { |
fed66e2c | 5183 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5184 | event->pending_kill = 0; |
4c9e2542 | 5185 | } |
925d519a PZ |
5186 | } |
5187 | ||
e360adbe | 5188 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5189 | { |
cdd6c482 IM |
5190 | struct perf_event *event = container_of(entry, |
5191 | struct perf_event, pending); | |
d525211f PZ |
5192 | int rctx; |
5193 | ||
5194 | rctx = perf_swevent_get_recursion_context(); | |
5195 | /* | |
5196 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5197 | * and we won't recurse 'further'. | |
5198 | */ | |
79f14641 | 5199 | |
cdd6c482 IM |
5200 | if (event->pending_disable) { |
5201 | event->pending_disable = 0; | |
fae3fde6 | 5202 | perf_event_disable_local(event); |
79f14641 PZ |
5203 | } |
5204 | ||
cdd6c482 IM |
5205 | if (event->pending_wakeup) { |
5206 | event->pending_wakeup = 0; | |
5207 | perf_event_wakeup(event); | |
79f14641 | 5208 | } |
d525211f PZ |
5209 | |
5210 | if (rctx >= 0) | |
5211 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5212 | } |
5213 | ||
39447b38 ZY |
5214 | /* |
5215 | * We assume there is only KVM supporting the callbacks. | |
5216 | * Later on, we might change it to a list if there is | |
5217 | * another virtualization implementation supporting the callbacks. | |
5218 | */ | |
5219 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5220 | ||
5221 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5222 | { | |
5223 | perf_guest_cbs = cbs; | |
5224 | return 0; | |
5225 | } | |
5226 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5227 | ||
5228 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5229 | { | |
5230 | perf_guest_cbs = NULL; | |
5231 | return 0; | |
5232 | } | |
5233 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5234 | ||
4018994f JO |
5235 | static void |
5236 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5237 | struct pt_regs *regs, u64 mask) | |
5238 | { | |
5239 | int bit; | |
5240 | ||
5241 | for_each_set_bit(bit, (const unsigned long *) &mask, | |
5242 | sizeof(mask) * BITS_PER_BYTE) { | |
5243 | u64 val; | |
5244 | ||
5245 | val = perf_reg_value(regs, bit); | |
5246 | perf_output_put(handle, val); | |
5247 | } | |
5248 | } | |
5249 | ||
60e2364e | 5250 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5251 | struct pt_regs *regs, |
5252 | struct pt_regs *regs_user_copy) | |
4018994f | 5253 | { |
88a7c26a AL |
5254 | if (user_mode(regs)) { |
5255 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5256 | regs_user->regs = regs; |
88a7c26a AL |
5257 | } else if (current->mm) { |
5258 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5259 | } else { |
5260 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5261 | regs_user->regs = NULL; | |
4018994f JO |
5262 | } |
5263 | } | |
5264 | ||
60e2364e SE |
5265 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5266 | struct pt_regs *regs) | |
5267 | { | |
5268 | regs_intr->regs = regs; | |
5269 | regs_intr->abi = perf_reg_abi(current); | |
5270 | } | |
5271 | ||
5272 | ||
c5ebcedb JO |
5273 | /* |
5274 | * Get remaining task size from user stack pointer. | |
5275 | * | |
5276 | * It'd be better to take stack vma map and limit this more | |
5277 | * precisly, but there's no way to get it safely under interrupt, | |
5278 | * so using TASK_SIZE as limit. | |
5279 | */ | |
5280 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5281 | { | |
5282 | unsigned long addr = perf_user_stack_pointer(regs); | |
5283 | ||
5284 | if (!addr || addr >= TASK_SIZE) | |
5285 | return 0; | |
5286 | ||
5287 | return TASK_SIZE - addr; | |
5288 | } | |
5289 | ||
5290 | static u16 | |
5291 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5292 | struct pt_regs *regs) | |
5293 | { | |
5294 | u64 task_size; | |
5295 | ||
5296 | /* No regs, no stack pointer, no dump. */ | |
5297 | if (!regs) | |
5298 | return 0; | |
5299 | ||
5300 | /* | |
5301 | * Check if we fit in with the requested stack size into the: | |
5302 | * - TASK_SIZE | |
5303 | * If we don't, we limit the size to the TASK_SIZE. | |
5304 | * | |
5305 | * - remaining sample size | |
5306 | * If we don't, we customize the stack size to | |
5307 | * fit in to the remaining sample size. | |
5308 | */ | |
5309 | ||
5310 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5311 | stack_size = min(stack_size, (u16) task_size); | |
5312 | ||
5313 | /* Current header size plus static size and dynamic size. */ | |
5314 | header_size += 2 * sizeof(u64); | |
5315 | ||
5316 | /* Do we fit in with the current stack dump size? */ | |
5317 | if ((u16) (header_size + stack_size) < header_size) { | |
5318 | /* | |
5319 | * If we overflow the maximum size for the sample, | |
5320 | * we customize the stack dump size to fit in. | |
5321 | */ | |
5322 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5323 | stack_size = round_up(stack_size, sizeof(u64)); | |
5324 | } | |
5325 | ||
5326 | return stack_size; | |
5327 | } | |
5328 | ||
5329 | static void | |
5330 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5331 | struct pt_regs *regs) | |
5332 | { | |
5333 | /* Case of a kernel thread, nothing to dump */ | |
5334 | if (!regs) { | |
5335 | u64 size = 0; | |
5336 | perf_output_put(handle, size); | |
5337 | } else { | |
5338 | unsigned long sp; | |
5339 | unsigned int rem; | |
5340 | u64 dyn_size; | |
5341 | ||
5342 | /* | |
5343 | * We dump: | |
5344 | * static size | |
5345 | * - the size requested by user or the best one we can fit | |
5346 | * in to the sample max size | |
5347 | * data | |
5348 | * - user stack dump data | |
5349 | * dynamic size | |
5350 | * - the actual dumped size | |
5351 | */ | |
5352 | ||
5353 | /* Static size. */ | |
5354 | perf_output_put(handle, dump_size); | |
5355 | ||
5356 | /* Data. */ | |
5357 | sp = perf_user_stack_pointer(regs); | |
5358 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5359 | dyn_size = dump_size - rem; | |
5360 | ||
5361 | perf_output_skip(handle, rem); | |
5362 | ||
5363 | /* Dynamic size. */ | |
5364 | perf_output_put(handle, dyn_size); | |
5365 | } | |
5366 | } | |
5367 | ||
c980d109 ACM |
5368 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5369 | struct perf_sample_data *data, | |
5370 | struct perf_event *event) | |
6844c09d ACM |
5371 | { |
5372 | u64 sample_type = event->attr.sample_type; | |
5373 | ||
5374 | data->type = sample_type; | |
5375 | header->size += event->id_header_size; | |
5376 | ||
5377 | if (sample_type & PERF_SAMPLE_TID) { | |
5378 | /* namespace issues */ | |
5379 | data->tid_entry.pid = perf_event_pid(event, current); | |
5380 | data->tid_entry.tid = perf_event_tid(event, current); | |
5381 | } | |
5382 | ||
5383 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5384 | data->time = perf_event_clock(event); |
6844c09d | 5385 | |
ff3d527c | 5386 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5387 | data->id = primary_event_id(event); |
5388 | ||
5389 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5390 | data->stream_id = event->id; | |
5391 | ||
5392 | if (sample_type & PERF_SAMPLE_CPU) { | |
5393 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5394 | data->cpu_entry.reserved = 0; | |
5395 | } | |
5396 | } | |
5397 | ||
76369139 FW |
5398 | void perf_event_header__init_id(struct perf_event_header *header, |
5399 | struct perf_sample_data *data, | |
5400 | struct perf_event *event) | |
c980d109 ACM |
5401 | { |
5402 | if (event->attr.sample_id_all) | |
5403 | __perf_event_header__init_id(header, data, event); | |
5404 | } | |
5405 | ||
5406 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5407 | struct perf_sample_data *data) | |
5408 | { | |
5409 | u64 sample_type = data->type; | |
5410 | ||
5411 | if (sample_type & PERF_SAMPLE_TID) | |
5412 | perf_output_put(handle, data->tid_entry); | |
5413 | ||
5414 | if (sample_type & PERF_SAMPLE_TIME) | |
5415 | perf_output_put(handle, data->time); | |
5416 | ||
5417 | if (sample_type & PERF_SAMPLE_ID) | |
5418 | perf_output_put(handle, data->id); | |
5419 | ||
5420 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5421 | perf_output_put(handle, data->stream_id); | |
5422 | ||
5423 | if (sample_type & PERF_SAMPLE_CPU) | |
5424 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5425 | |
5426 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5427 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5428 | } |
5429 | ||
76369139 FW |
5430 | void perf_event__output_id_sample(struct perf_event *event, |
5431 | struct perf_output_handle *handle, | |
5432 | struct perf_sample_data *sample) | |
c980d109 ACM |
5433 | { |
5434 | if (event->attr.sample_id_all) | |
5435 | __perf_event__output_id_sample(handle, sample); | |
5436 | } | |
5437 | ||
3dab77fb | 5438 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5439 | struct perf_event *event, |
5440 | u64 enabled, u64 running) | |
3dab77fb | 5441 | { |
cdd6c482 | 5442 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5443 | u64 values[4]; |
5444 | int n = 0; | |
5445 | ||
b5e58793 | 5446 | values[n++] = perf_event_count(event); |
3dab77fb | 5447 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5448 | values[n++] = enabled + |
cdd6c482 | 5449 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5450 | } |
5451 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5452 | values[n++] = running + |
cdd6c482 | 5453 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5454 | } |
5455 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 5456 | values[n++] = primary_event_id(event); |
3dab77fb | 5457 | |
76369139 | 5458 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5459 | } |
5460 | ||
5461 | /* | |
cdd6c482 | 5462 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
3dab77fb PZ |
5463 | */ |
5464 | static void perf_output_read_group(struct perf_output_handle *handle, | |
eed01528 SE |
5465 | struct perf_event *event, |
5466 | u64 enabled, u64 running) | |
3dab77fb | 5467 | { |
cdd6c482 IM |
5468 | struct perf_event *leader = event->group_leader, *sub; |
5469 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
5470 | u64 values[5]; |
5471 | int n = 0; | |
5472 | ||
5473 | values[n++] = 1 + leader->nr_siblings; | |
5474 | ||
5475 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 5476 | values[n++] = enabled; |
3dab77fb PZ |
5477 | |
5478 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 5479 | values[n++] = running; |
3dab77fb | 5480 | |
cdd6c482 | 5481 | if (leader != event) |
3dab77fb PZ |
5482 | leader->pmu->read(leader); |
5483 | ||
b5e58793 | 5484 | values[n++] = perf_event_count(leader); |
3dab77fb | 5485 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5486 | values[n++] = primary_event_id(leader); |
3dab77fb | 5487 | |
76369139 | 5488 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 5489 | |
65abc865 | 5490 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
3dab77fb PZ |
5491 | n = 0; |
5492 | ||
6f5ab001 JO |
5493 | if ((sub != event) && |
5494 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
5495 | sub->pmu->read(sub); |
5496 | ||
b5e58793 | 5497 | values[n++] = perf_event_count(sub); |
3dab77fb | 5498 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5499 | values[n++] = primary_event_id(sub); |
3dab77fb | 5500 | |
76369139 | 5501 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5502 | } |
5503 | } | |
5504 | ||
eed01528 SE |
5505 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
5506 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5507 | ||
3dab77fb | 5508 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 5509 | struct perf_event *event) |
3dab77fb | 5510 | { |
e3f3541c | 5511 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
5512 | u64 read_format = event->attr.read_format; |
5513 | ||
5514 | /* | |
5515 | * compute total_time_enabled, total_time_running | |
5516 | * based on snapshot values taken when the event | |
5517 | * was last scheduled in. | |
5518 | * | |
5519 | * we cannot simply called update_context_time() | |
5520 | * because of locking issue as we are called in | |
5521 | * NMI context | |
5522 | */ | |
c4794295 | 5523 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 5524 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 5525 | |
cdd6c482 | 5526 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 5527 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 5528 | else |
eed01528 | 5529 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
5530 | } |
5531 | ||
5622f295 MM |
5532 | void perf_output_sample(struct perf_output_handle *handle, |
5533 | struct perf_event_header *header, | |
5534 | struct perf_sample_data *data, | |
cdd6c482 | 5535 | struct perf_event *event) |
5622f295 MM |
5536 | { |
5537 | u64 sample_type = data->type; | |
5538 | ||
5539 | perf_output_put(handle, *header); | |
5540 | ||
ff3d527c AH |
5541 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
5542 | perf_output_put(handle, data->id); | |
5543 | ||
5622f295 MM |
5544 | if (sample_type & PERF_SAMPLE_IP) |
5545 | perf_output_put(handle, data->ip); | |
5546 | ||
5547 | if (sample_type & PERF_SAMPLE_TID) | |
5548 | perf_output_put(handle, data->tid_entry); | |
5549 | ||
5550 | if (sample_type & PERF_SAMPLE_TIME) | |
5551 | perf_output_put(handle, data->time); | |
5552 | ||
5553 | if (sample_type & PERF_SAMPLE_ADDR) | |
5554 | perf_output_put(handle, data->addr); | |
5555 | ||
5556 | if (sample_type & PERF_SAMPLE_ID) | |
5557 | perf_output_put(handle, data->id); | |
5558 | ||
5559 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5560 | perf_output_put(handle, data->stream_id); | |
5561 | ||
5562 | if (sample_type & PERF_SAMPLE_CPU) | |
5563 | perf_output_put(handle, data->cpu_entry); | |
5564 | ||
5565 | if (sample_type & PERF_SAMPLE_PERIOD) | |
5566 | perf_output_put(handle, data->period); | |
5567 | ||
5568 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 5569 | perf_output_read(handle, event); |
5622f295 MM |
5570 | |
5571 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
5572 | if (data->callchain) { | |
5573 | int size = 1; | |
5574 | ||
5575 | if (data->callchain) | |
5576 | size += data->callchain->nr; | |
5577 | ||
5578 | size *= sizeof(u64); | |
5579 | ||
76369139 | 5580 | __output_copy(handle, data->callchain, size); |
5622f295 MM |
5581 | } else { |
5582 | u64 nr = 0; | |
5583 | perf_output_put(handle, nr); | |
5584 | } | |
5585 | } | |
5586 | ||
5587 | if (sample_type & PERF_SAMPLE_RAW) { | |
5588 | if (data->raw) { | |
fa128e6a AS |
5589 | u32 raw_size = data->raw->size; |
5590 | u32 real_size = round_up(raw_size + sizeof(u32), | |
5591 | sizeof(u64)) - sizeof(u32); | |
5592 | u64 zero = 0; | |
5593 | ||
5594 | perf_output_put(handle, real_size); | |
5595 | __output_copy(handle, data->raw->data, raw_size); | |
5596 | if (real_size - raw_size) | |
5597 | __output_copy(handle, &zero, real_size - raw_size); | |
5622f295 MM |
5598 | } else { |
5599 | struct { | |
5600 | u32 size; | |
5601 | u32 data; | |
5602 | } raw = { | |
5603 | .size = sizeof(u32), | |
5604 | .data = 0, | |
5605 | }; | |
5606 | perf_output_put(handle, raw); | |
5607 | } | |
5608 | } | |
a7ac67ea | 5609 | |
bce38cd5 SE |
5610 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
5611 | if (data->br_stack) { | |
5612 | size_t size; | |
5613 | ||
5614 | size = data->br_stack->nr | |
5615 | * sizeof(struct perf_branch_entry); | |
5616 | ||
5617 | perf_output_put(handle, data->br_stack->nr); | |
5618 | perf_output_copy(handle, data->br_stack->entries, size); | |
5619 | } else { | |
5620 | /* | |
5621 | * we always store at least the value of nr | |
5622 | */ | |
5623 | u64 nr = 0; | |
5624 | perf_output_put(handle, nr); | |
5625 | } | |
5626 | } | |
4018994f JO |
5627 | |
5628 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
5629 | u64 abi = data->regs_user.abi; | |
5630 | ||
5631 | /* | |
5632 | * If there are no regs to dump, notice it through | |
5633 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5634 | */ | |
5635 | perf_output_put(handle, abi); | |
5636 | ||
5637 | if (abi) { | |
5638 | u64 mask = event->attr.sample_regs_user; | |
5639 | perf_output_sample_regs(handle, | |
5640 | data->regs_user.regs, | |
5641 | mask); | |
5642 | } | |
5643 | } | |
c5ebcedb | 5644 | |
a5cdd40c | 5645 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
5646 | perf_output_sample_ustack(handle, |
5647 | data->stack_user_size, | |
5648 | data->regs_user.regs); | |
a5cdd40c | 5649 | } |
c3feedf2 AK |
5650 | |
5651 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
5652 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
5653 | |
5654 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
5655 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 5656 | |
fdfbbd07 AK |
5657 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
5658 | perf_output_put(handle, data->txn); | |
5659 | ||
60e2364e SE |
5660 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
5661 | u64 abi = data->regs_intr.abi; | |
5662 | /* | |
5663 | * If there are no regs to dump, notice it through | |
5664 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5665 | */ | |
5666 | perf_output_put(handle, abi); | |
5667 | ||
5668 | if (abi) { | |
5669 | u64 mask = event->attr.sample_regs_intr; | |
5670 | ||
5671 | perf_output_sample_regs(handle, | |
5672 | data->regs_intr.regs, | |
5673 | mask); | |
5674 | } | |
5675 | } | |
5676 | ||
a5cdd40c PZ |
5677 | if (!event->attr.watermark) { |
5678 | int wakeup_events = event->attr.wakeup_events; | |
5679 | ||
5680 | if (wakeup_events) { | |
5681 | struct ring_buffer *rb = handle->rb; | |
5682 | int events = local_inc_return(&rb->events); | |
5683 | ||
5684 | if (events >= wakeup_events) { | |
5685 | local_sub(wakeup_events, &rb->events); | |
5686 | local_inc(&rb->wakeup); | |
5687 | } | |
5688 | } | |
5689 | } | |
5622f295 MM |
5690 | } |
5691 | ||
5692 | void perf_prepare_sample(struct perf_event_header *header, | |
5693 | struct perf_sample_data *data, | |
cdd6c482 | 5694 | struct perf_event *event, |
5622f295 | 5695 | struct pt_regs *regs) |
7b732a75 | 5696 | { |
cdd6c482 | 5697 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 5698 | |
cdd6c482 | 5699 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 5700 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
5701 | |
5702 | header->misc = 0; | |
5703 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 5704 | |
c980d109 | 5705 | __perf_event_header__init_id(header, data, event); |
6844c09d | 5706 | |
c320c7b7 | 5707 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
5708 | data->ip = perf_instruction_pointer(regs); |
5709 | ||
b23f3325 | 5710 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 5711 | int size = 1; |
394ee076 | 5712 | |
e6dab5ff | 5713 | data->callchain = perf_callchain(event, regs); |
5622f295 MM |
5714 | |
5715 | if (data->callchain) | |
5716 | size += data->callchain->nr; | |
5717 | ||
5718 | header->size += size * sizeof(u64); | |
394ee076 PZ |
5719 | } |
5720 | ||
3a43ce68 | 5721 | if (sample_type & PERF_SAMPLE_RAW) { |
a044560c PZ |
5722 | int size = sizeof(u32); |
5723 | ||
5724 | if (data->raw) | |
5725 | size += data->raw->size; | |
5726 | else | |
5727 | size += sizeof(u32); | |
5728 | ||
fa128e6a | 5729 | header->size += round_up(size, sizeof(u64)); |
7f453c24 | 5730 | } |
bce38cd5 SE |
5731 | |
5732 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
5733 | int size = sizeof(u64); /* nr */ | |
5734 | if (data->br_stack) { | |
5735 | size += data->br_stack->nr | |
5736 | * sizeof(struct perf_branch_entry); | |
5737 | } | |
5738 | header->size += size; | |
5739 | } | |
4018994f | 5740 | |
2565711f | 5741 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
5742 | perf_sample_regs_user(&data->regs_user, regs, |
5743 | &data->regs_user_copy); | |
2565711f | 5744 | |
4018994f JO |
5745 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
5746 | /* regs dump ABI info */ | |
5747 | int size = sizeof(u64); | |
5748 | ||
4018994f JO |
5749 | if (data->regs_user.regs) { |
5750 | u64 mask = event->attr.sample_regs_user; | |
5751 | size += hweight64(mask) * sizeof(u64); | |
5752 | } | |
5753 | ||
5754 | header->size += size; | |
5755 | } | |
c5ebcedb JO |
5756 | |
5757 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
5758 | /* | |
5759 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
5760 | * processed as the last one or have additional check added | |
5761 | * in case new sample type is added, because we could eat | |
5762 | * up the rest of the sample size. | |
5763 | */ | |
c5ebcedb JO |
5764 | u16 stack_size = event->attr.sample_stack_user; |
5765 | u16 size = sizeof(u64); | |
5766 | ||
c5ebcedb | 5767 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 5768 | data->regs_user.regs); |
c5ebcedb JO |
5769 | |
5770 | /* | |
5771 | * If there is something to dump, add space for the dump | |
5772 | * itself and for the field that tells the dynamic size, | |
5773 | * which is how many have been actually dumped. | |
5774 | */ | |
5775 | if (stack_size) | |
5776 | size += sizeof(u64) + stack_size; | |
5777 | ||
5778 | data->stack_user_size = stack_size; | |
5779 | header->size += size; | |
5780 | } | |
60e2364e SE |
5781 | |
5782 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
5783 | /* regs dump ABI info */ | |
5784 | int size = sizeof(u64); | |
5785 | ||
5786 | perf_sample_regs_intr(&data->regs_intr, regs); | |
5787 | ||
5788 | if (data->regs_intr.regs) { | |
5789 | u64 mask = event->attr.sample_regs_intr; | |
5790 | ||
5791 | size += hweight64(mask) * sizeof(u64); | |
5792 | } | |
5793 | ||
5794 | header->size += size; | |
5795 | } | |
5622f295 | 5796 | } |
7f453c24 | 5797 | |
9ecda41a WN |
5798 | static void __always_inline |
5799 | __perf_event_output(struct perf_event *event, | |
5800 | struct perf_sample_data *data, | |
5801 | struct pt_regs *regs, | |
5802 | int (*output_begin)(struct perf_output_handle *, | |
5803 | struct perf_event *, | |
5804 | unsigned int)) | |
5622f295 MM |
5805 | { |
5806 | struct perf_output_handle handle; | |
5807 | struct perf_event_header header; | |
689802b2 | 5808 | |
927c7a9e FW |
5809 | /* protect the callchain buffers */ |
5810 | rcu_read_lock(); | |
5811 | ||
cdd6c482 | 5812 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 5813 | |
9ecda41a | 5814 | if (output_begin(&handle, event, header.size)) |
927c7a9e | 5815 | goto exit; |
0322cd6e | 5816 | |
cdd6c482 | 5817 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 5818 | |
8a057d84 | 5819 | perf_output_end(&handle); |
927c7a9e FW |
5820 | |
5821 | exit: | |
5822 | rcu_read_unlock(); | |
0322cd6e PZ |
5823 | } |
5824 | ||
9ecda41a WN |
5825 | void |
5826 | perf_event_output_forward(struct perf_event *event, | |
5827 | struct perf_sample_data *data, | |
5828 | struct pt_regs *regs) | |
5829 | { | |
5830 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
5831 | } | |
5832 | ||
5833 | void | |
5834 | perf_event_output_backward(struct perf_event *event, | |
5835 | struct perf_sample_data *data, | |
5836 | struct pt_regs *regs) | |
5837 | { | |
5838 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
5839 | } | |
5840 | ||
5841 | void | |
5842 | perf_event_output(struct perf_event *event, | |
5843 | struct perf_sample_data *data, | |
5844 | struct pt_regs *regs) | |
5845 | { | |
5846 | __perf_event_output(event, data, regs, perf_output_begin); | |
5847 | } | |
5848 | ||
38b200d6 | 5849 | /* |
cdd6c482 | 5850 | * read event_id |
38b200d6 PZ |
5851 | */ |
5852 | ||
5853 | struct perf_read_event { | |
5854 | struct perf_event_header header; | |
5855 | ||
5856 | u32 pid; | |
5857 | u32 tid; | |
38b200d6 PZ |
5858 | }; |
5859 | ||
5860 | static void | |
cdd6c482 | 5861 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
5862 | struct task_struct *task) |
5863 | { | |
5864 | struct perf_output_handle handle; | |
c980d109 | 5865 | struct perf_sample_data sample; |
dfc65094 | 5866 | struct perf_read_event read_event = { |
38b200d6 | 5867 | .header = { |
cdd6c482 | 5868 | .type = PERF_RECORD_READ, |
38b200d6 | 5869 | .misc = 0, |
c320c7b7 | 5870 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 5871 | }, |
cdd6c482 IM |
5872 | .pid = perf_event_pid(event, task), |
5873 | .tid = perf_event_tid(event, task), | |
38b200d6 | 5874 | }; |
3dab77fb | 5875 | int ret; |
38b200d6 | 5876 | |
c980d109 | 5877 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 5878 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
5879 | if (ret) |
5880 | return; | |
5881 | ||
dfc65094 | 5882 | perf_output_put(&handle, read_event); |
cdd6c482 | 5883 | perf_output_read(&handle, event); |
c980d109 | 5884 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 5885 | |
38b200d6 PZ |
5886 | perf_output_end(&handle); |
5887 | } | |
5888 | ||
aab5b71e | 5889 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
5890 | |
5891 | static void | |
aab5b71e PZ |
5892 | perf_iterate_ctx(struct perf_event_context *ctx, |
5893 | perf_iterate_f output, | |
b73e4fef | 5894 | void *data, bool all) |
52d857a8 JO |
5895 | { |
5896 | struct perf_event *event; | |
5897 | ||
5898 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
5899 | if (!all) { |
5900 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
5901 | continue; | |
5902 | if (!event_filter_match(event)) | |
5903 | continue; | |
5904 | } | |
5905 | ||
67516844 | 5906 | output(event, data); |
52d857a8 JO |
5907 | } |
5908 | } | |
5909 | ||
aab5b71e | 5910 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
5911 | { |
5912 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
5913 | struct perf_event *event; | |
5914 | ||
5915 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
5916 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
5917 | continue; | |
5918 | if (!event_filter_match(event)) | |
5919 | continue; | |
5920 | output(event, data); | |
5921 | } | |
5922 | } | |
5923 | ||
aab5b71e PZ |
5924 | /* |
5925 | * Iterate all events that need to receive side-band events. | |
5926 | * | |
5927 | * For new callers; ensure that account_pmu_sb_event() includes | |
5928 | * your event, otherwise it might not get delivered. | |
5929 | */ | |
52d857a8 | 5930 | static void |
aab5b71e | 5931 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
5932 | struct perf_event_context *task_ctx) |
5933 | { | |
52d857a8 | 5934 | struct perf_event_context *ctx; |
52d857a8 JO |
5935 | int ctxn; |
5936 | ||
aab5b71e PZ |
5937 | rcu_read_lock(); |
5938 | preempt_disable(); | |
5939 | ||
4e93ad60 | 5940 | /* |
aab5b71e PZ |
5941 | * If we have task_ctx != NULL we only notify the task context itself. |
5942 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
5943 | * context. |
5944 | */ | |
5945 | if (task_ctx) { | |
aab5b71e PZ |
5946 | perf_iterate_ctx(task_ctx, output, data, false); |
5947 | goto done; | |
4e93ad60 JO |
5948 | } |
5949 | ||
aab5b71e | 5950 | perf_iterate_sb_cpu(output, data); |
f2fb6bef KL |
5951 | |
5952 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
5953 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
5954 | if (ctx) | |
aab5b71e | 5955 | perf_iterate_ctx(ctx, output, data, false); |
52d857a8 | 5956 | } |
aab5b71e | 5957 | done: |
f2fb6bef | 5958 | preempt_enable(); |
52d857a8 | 5959 | rcu_read_unlock(); |
95ff4ca2 AS |
5960 | } |
5961 | ||
375637bc AS |
5962 | /* |
5963 | * Clear all file-based filters at exec, they'll have to be | |
5964 | * re-instated when/if these objects are mmapped again. | |
5965 | */ | |
5966 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
5967 | { | |
5968 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
5969 | struct perf_addr_filter *filter; | |
5970 | unsigned int restart = 0, count = 0; | |
5971 | unsigned long flags; | |
5972 | ||
5973 | if (!has_addr_filter(event)) | |
5974 | return; | |
5975 | ||
5976 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
5977 | list_for_each_entry(filter, &ifh->list, entry) { | |
5978 | if (filter->inode) { | |
5979 | event->addr_filters_offs[count] = 0; | |
5980 | restart++; | |
5981 | } | |
5982 | ||
5983 | count++; | |
5984 | } | |
5985 | ||
5986 | if (restart) | |
5987 | event->addr_filters_gen++; | |
5988 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
5989 | ||
5990 | if (restart) | |
5991 | perf_event_restart(event); | |
5992 | } | |
5993 | ||
5994 | void perf_event_exec(void) | |
5995 | { | |
5996 | struct perf_event_context *ctx; | |
5997 | int ctxn; | |
5998 | ||
5999 | rcu_read_lock(); | |
6000 | for_each_task_context_nr(ctxn) { | |
6001 | ctx = current->perf_event_ctxp[ctxn]; | |
6002 | if (!ctx) | |
6003 | continue; | |
6004 | ||
6005 | perf_event_enable_on_exec(ctxn); | |
6006 | ||
aab5b71e | 6007 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
375637bc AS |
6008 | true); |
6009 | } | |
6010 | rcu_read_unlock(); | |
6011 | } | |
6012 | ||
95ff4ca2 AS |
6013 | struct remote_output { |
6014 | struct ring_buffer *rb; | |
6015 | int err; | |
6016 | }; | |
6017 | ||
6018 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
6019 | { | |
6020 | struct perf_event *parent = event->parent; | |
6021 | struct remote_output *ro = data; | |
6022 | struct ring_buffer *rb = ro->rb; | |
375637bc AS |
6023 | struct stop_event_data sd = { |
6024 | .event = event, | |
6025 | }; | |
95ff4ca2 AS |
6026 | |
6027 | if (!has_aux(event)) | |
6028 | return; | |
6029 | ||
6030 | if (!parent) | |
6031 | parent = event; | |
6032 | ||
6033 | /* | |
6034 | * In case of inheritance, it will be the parent that links to the | |
6035 | * ring-buffer, but it will be the child that's actually using it: | |
6036 | */ | |
6037 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 6038 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
6039 | } |
6040 | ||
6041 | static int __perf_pmu_output_stop(void *info) | |
6042 | { | |
6043 | struct perf_event *event = info; | |
6044 | struct pmu *pmu = event->pmu; | |
6045 | struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); | |
6046 | struct remote_output ro = { | |
6047 | .rb = event->rb, | |
6048 | }; | |
6049 | ||
6050 | rcu_read_lock(); | |
aab5b71e | 6051 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 6052 | if (cpuctx->task_ctx) |
aab5b71e | 6053 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 6054 | &ro, false); |
95ff4ca2 AS |
6055 | rcu_read_unlock(); |
6056 | ||
6057 | return ro.err; | |
6058 | } | |
6059 | ||
6060 | static void perf_pmu_output_stop(struct perf_event *event) | |
6061 | { | |
6062 | struct perf_event *iter; | |
6063 | int err, cpu; | |
6064 | ||
6065 | restart: | |
6066 | rcu_read_lock(); | |
6067 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
6068 | /* | |
6069 | * For per-CPU events, we need to make sure that neither they | |
6070 | * nor their children are running; for cpu==-1 events it's | |
6071 | * sufficient to stop the event itself if it's active, since | |
6072 | * it can't have children. | |
6073 | */ | |
6074 | cpu = iter->cpu; | |
6075 | if (cpu == -1) | |
6076 | cpu = READ_ONCE(iter->oncpu); | |
6077 | ||
6078 | if (cpu == -1) | |
6079 | continue; | |
6080 | ||
6081 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
6082 | if (err == -EAGAIN) { | |
6083 | rcu_read_unlock(); | |
6084 | goto restart; | |
6085 | } | |
6086 | } | |
6087 | rcu_read_unlock(); | |
52d857a8 JO |
6088 | } |
6089 | ||
60313ebe | 6090 | /* |
9f498cc5 PZ |
6091 | * task tracking -- fork/exit |
6092 | * | |
13d7a241 | 6093 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
6094 | */ |
6095 | ||
9f498cc5 | 6096 | struct perf_task_event { |
3a80b4a3 | 6097 | struct task_struct *task; |
cdd6c482 | 6098 | struct perf_event_context *task_ctx; |
60313ebe PZ |
6099 | |
6100 | struct { | |
6101 | struct perf_event_header header; | |
6102 | ||
6103 | u32 pid; | |
6104 | u32 ppid; | |
9f498cc5 PZ |
6105 | u32 tid; |
6106 | u32 ptid; | |
393b2ad8 | 6107 | u64 time; |
cdd6c482 | 6108 | } event_id; |
60313ebe PZ |
6109 | }; |
6110 | ||
67516844 JO |
6111 | static int perf_event_task_match(struct perf_event *event) |
6112 | { | |
13d7a241 SE |
6113 | return event->attr.comm || event->attr.mmap || |
6114 | event->attr.mmap2 || event->attr.mmap_data || | |
6115 | event->attr.task; | |
67516844 JO |
6116 | } |
6117 | ||
cdd6c482 | 6118 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 6119 | void *data) |
60313ebe | 6120 | { |
52d857a8 | 6121 | struct perf_task_event *task_event = data; |
60313ebe | 6122 | struct perf_output_handle handle; |
c980d109 | 6123 | struct perf_sample_data sample; |
9f498cc5 | 6124 | struct task_struct *task = task_event->task; |
c980d109 | 6125 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 6126 | |
67516844 JO |
6127 | if (!perf_event_task_match(event)) |
6128 | return; | |
6129 | ||
c980d109 | 6130 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 6131 | |
c980d109 | 6132 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 6133 | task_event->event_id.header.size); |
ef60777c | 6134 | if (ret) |
c980d109 | 6135 | goto out; |
60313ebe | 6136 | |
cdd6c482 IM |
6137 | task_event->event_id.pid = perf_event_pid(event, task); |
6138 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 6139 | |
cdd6c482 IM |
6140 | task_event->event_id.tid = perf_event_tid(event, task); |
6141 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 6142 | |
34f43927 PZ |
6143 | task_event->event_id.time = perf_event_clock(event); |
6144 | ||
cdd6c482 | 6145 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 6146 | |
c980d109 ACM |
6147 | perf_event__output_id_sample(event, &handle, &sample); |
6148 | ||
60313ebe | 6149 | perf_output_end(&handle); |
c980d109 ACM |
6150 | out: |
6151 | task_event->event_id.header.size = size; | |
60313ebe PZ |
6152 | } |
6153 | ||
cdd6c482 IM |
6154 | static void perf_event_task(struct task_struct *task, |
6155 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 6156 | int new) |
60313ebe | 6157 | { |
9f498cc5 | 6158 | struct perf_task_event task_event; |
60313ebe | 6159 | |
cdd6c482 IM |
6160 | if (!atomic_read(&nr_comm_events) && |
6161 | !atomic_read(&nr_mmap_events) && | |
6162 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
6163 | return; |
6164 | ||
9f498cc5 | 6165 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
6166 | .task = task, |
6167 | .task_ctx = task_ctx, | |
cdd6c482 | 6168 | .event_id = { |
60313ebe | 6169 | .header = { |
cdd6c482 | 6170 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 6171 | .misc = 0, |
cdd6c482 | 6172 | .size = sizeof(task_event.event_id), |
60313ebe | 6173 | }, |
573402db PZ |
6174 | /* .pid */ |
6175 | /* .ppid */ | |
9f498cc5 PZ |
6176 | /* .tid */ |
6177 | /* .ptid */ | |
34f43927 | 6178 | /* .time */ |
60313ebe PZ |
6179 | }, |
6180 | }; | |
6181 | ||
aab5b71e | 6182 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
6183 | &task_event, |
6184 | task_ctx); | |
9f498cc5 PZ |
6185 | } |
6186 | ||
cdd6c482 | 6187 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 6188 | { |
cdd6c482 | 6189 | perf_event_task(task, NULL, 1); |
60313ebe PZ |
6190 | } |
6191 | ||
8d1b2d93 PZ |
6192 | /* |
6193 | * comm tracking | |
6194 | */ | |
6195 | ||
6196 | struct perf_comm_event { | |
22a4f650 IM |
6197 | struct task_struct *task; |
6198 | char *comm; | |
8d1b2d93 PZ |
6199 | int comm_size; |
6200 | ||
6201 | struct { | |
6202 | struct perf_event_header header; | |
6203 | ||
6204 | u32 pid; | |
6205 | u32 tid; | |
cdd6c482 | 6206 | } event_id; |
8d1b2d93 PZ |
6207 | }; |
6208 | ||
67516844 JO |
6209 | static int perf_event_comm_match(struct perf_event *event) |
6210 | { | |
6211 | return event->attr.comm; | |
6212 | } | |
6213 | ||
cdd6c482 | 6214 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6215 | void *data) |
8d1b2d93 | 6216 | { |
52d857a8 | 6217 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6218 | struct perf_output_handle handle; |
c980d109 | 6219 | struct perf_sample_data sample; |
cdd6c482 | 6220 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6221 | int ret; |
6222 | ||
67516844 JO |
6223 | if (!perf_event_comm_match(event)) |
6224 | return; | |
6225 | ||
c980d109 ACM |
6226 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6227 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6228 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6229 | |
6230 | if (ret) | |
c980d109 | 6231 | goto out; |
8d1b2d93 | 6232 | |
cdd6c482 IM |
6233 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6234 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6235 | |
cdd6c482 | 6236 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6237 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6238 | comm_event->comm_size); |
c980d109 ACM |
6239 | |
6240 | perf_event__output_id_sample(event, &handle, &sample); | |
6241 | ||
8d1b2d93 | 6242 | perf_output_end(&handle); |
c980d109 ACM |
6243 | out: |
6244 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6245 | } |
6246 | ||
cdd6c482 | 6247 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6248 | { |
413ee3b4 | 6249 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6250 | unsigned int size; |
8d1b2d93 | 6251 | |
413ee3b4 | 6252 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6253 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6254 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6255 | |
6256 | comm_event->comm = comm; | |
6257 | comm_event->comm_size = size; | |
6258 | ||
cdd6c482 | 6259 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6260 | |
aab5b71e | 6261 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
6262 | comm_event, |
6263 | NULL); | |
8d1b2d93 PZ |
6264 | } |
6265 | ||
82b89778 | 6266 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6267 | { |
9ee318a7 PZ |
6268 | struct perf_comm_event comm_event; |
6269 | ||
cdd6c482 | 6270 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6271 | return; |
a63eaf34 | 6272 | |
9ee318a7 | 6273 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6274 | .task = task, |
573402db PZ |
6275 | /* .comm */ |
6276 | /* .comm_size */ | |
cdd6c482 | 6277 | .event_id = { |
573402db | 6278 | .header = { |
cdd6c482 | 6279 | .type = PERF_RECORD_COMM, |
82b89778 | 6280 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6281 | /* .size */ |
6282 | }, | |
6283 | /* .pid */ | |
6284 | /* .tid */ | |
8d1b2d93 PZ |
6285 | }, |
6286 | }; | |
6287 | ||
cdd6c482 | 6288 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6289 | } |
6290 | ||
0a4a9391 PZ |
6291 | /* |
6292 | * mmap tracking | |
6293 | */ | |
6294 | ||
6295 | struct perf_mmap_event { | |
089dd79d PZ |
6296 | struct vm_area_struct *vma; |
6297 | ||
6298 | const char *file_name; | |
6299 | int file_size; | |
13d7a241 SE |
6300 | int maj, min; |
6301 | u64 ino; | |
6302 | u64 ino_generation; | |
f972eb63 | 6303 | u32 prot, flags; |
0a4a9391 PZ |
6304 | |
6305 | struct { | |
6306 | struct perf_event_header header; | |
6307 | ||
6308 | u32 pid; | |
6309 | u32 tid; | |
6310 | u64 start; | |
6311 | u64 len; | |
6312 | u64 pgoff; | |
cdd6c482 | 6313 | } event_id; |
0a4a9391 PZ |
6314 | }; |
6315 | ||
67516844 JO |
6316 | static int perf_event_mmap_match(struct perf_event *event, |
6317 | void *data) | |
6318 | { | |
6319 | struct perf_mmap_event *mmap_event = data; | |
6320 | struct vm_area_struct *vma = mmap_event->vma; | |
6321 | int executable = vma->vm_flags & VM_EXEC; | |
6322 | ||
6323 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 6324 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
6325 | } |
6326 | ||
cdd6c482 | 6327 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 6328 | void *data) |
0a4a9391 | 6329 | { |
52d857a8 | 6330 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 6331 | struct perf_output_handle handle; |
c980d109 | 6332 | struct perf_sample_data sample; |
cdd6c482 | 6333 | int size = mmap_event->event_id.header.size; |
c980d109 | 6334 | int ret; |
0a4a9391 | 6335 | |
67516844 JO |
6336 | if (!perf_event_mmap_match(event, data)) |
6337 | return; | |
6338 | ||
13d7a241 SE |
6339 | if (event->attr.mmap2) { |
6340 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
6341 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
6342 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
6343 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 6344 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
6345 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
6346 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
6347 | } |
6348 | ||
c980d109 ACM |
6349 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
6350 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6351 | mmap_event->event_id.header.size); |
0a4a9391 | 6352 | if (ret) |
c980d109 | 6353 | goto out; |
0a4a9391 | 6354 | |
cdd6c482 IM |
6355 | mmap_event->event_id.pid = perf_event_pid(event, current); |
6356 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 6357 | |
cdd6c482 | 6358 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
6359 | |
6360 | if (event->attr.mmap2) { | |
6361 | perf_output_put(&handle, mmap_event->maj); | |
6362 | perf_output_put(&handle, mmap_event->min); | |
6363 | perf_output_put(&handle, mmap_event->ino); | |
6364 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
6365 | perf_output_put(&handle, mmap_event->prot); |
6366 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
6367 | } |
6368 | ||
76369139 | 6369 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 6370 | mmap_event->file_size); |
c980d109 ACM |
6371 | |
6372 | perf_event__output_id_sample(event, &handle, &sample); | |
6373 | ||
78d613eb | 6374 | perf_output_end(&handle); |
c980d109 ACM |
6375 | out: |
6376 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
6377 | } |
6378 | ||
cdd6c482 | 6379 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 6380 | { |
089dd79d PZ |
6381 | struct vm_area_struct *vma = mmap_event->vma; |
6382 | struct file *file = vma->vm_file; | |
13d7a241 SE |
6383 | int maj = 0, min = 0; |
6384 | u64 ino = 0, gen = 0; | |
f972eb63 | 6385 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
6386 | unsigned int size; |
6387 | char tmp[16]; | |
6388 | char *buf = NULL; | |
2c42cfbf | 6389 | char *name; |
413ee3b4 | 6390 | |
0a4a9391 | 6391 | if (file) { |
13d7a241 SE |
6392 | struct inode *inode; |
6393 | dev_t dev; | |
3ea2f2b9 | 6394 | |
2c42cfbf | 6395 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 6396 | if (!buf) { |
c7e548b4 ON |
6397 | name = "//enomem"; |
6398 | goto cpy_name; | |
0a4a9391 | 6399 | } |
413ee3b4 | 6400 | /* |
3ea2f2b9 | 6401 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
6402 | * need to add enough zero bytes after the string to handle |
6403 | * the 64bit alignment we do later. | |
6404 | */ | |
9bf39ab2 | 6405 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 6406 | if (IS_ERR(name)) { |
c7e548b4 ON |
6407 | name = "//toolong"; |
6408 | goto cpy_name; | |
0a4a9391 | 6409 | } |
13d7a241 SE |
6410 | inode = file_inode(vma->vm_file); |
6411 | dev = inode->i_sb->s_dev; | |
6412 | ino = inode->i_ino; | |
6413 | gen = inode->i_generation; | |
6414 | maj = MAJOR(dev); | |
6415 | min = MINOR(dev); | |
f972eb63 PZ |
6416 | |
6417 | if (vma->vm_flags & VM_READ) | |
6418 | prot |= PROT_READ; | |
6419 | if (vma->vm_flags & VM_WRITE) | |
6420 | prot |= PROT_WRITE; | |
6421 | if (vma->vm_flags & VM_EXEC) | |
6422 | prot |= PROT_EXEC; | |
6423 | ||
6424 | if (vma->vm_flags & VM_MAYSHARE) | |
6425 | flags = MAP_SHARED; | |
6426 | else | |
6427 | flags = MAP_PRIVATE; | |
6428 | ||
6429 | if (vma->vm_flags & VM_DENYWRITE) | |
6430 | flags |= MAP_DENYWRITE; | |
6431 | if (vma->vm_flags & VM_MAYEXEC) | |
6432 | flags |= MAP_EXECUTABLE; | |
6433 | if (vma->vm_flags & VM_LOCKED) | |
6434 | flags |= MAP_LOCKED; | |
6435 | if (vma->vm_flags & VM_HUGETLB) | |
6436 | flags |= MAP_HUGETLB; | |
6437 | ||
c7e548b4 | 6438 | goto got_name; |
0a4a9391 | 6439 | } else { |
fbe26abe JO |
6440 | if (vma->vm_ops && vma->vm_ops->name) { |
6441 | name = (char *) vma->vm_ops->name(vma); | |
6442 | if (name) | |
6443 | goto cpy_name; | |
6444 | } | |
6445 | ||
2c42cfbf | 6446 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
6447 | if (name) |
6448 | goto cpy_name; | |
089dd79d | 6449 | |
32c5fb7e | 6450 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 6451 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
6452 | name = "[heap]"; |
6453 | goto cpy_name; | |
32c5fb7e ON |
6454 | } |
6455 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 6456 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
6457 | name = "[stack]"; |
6458 | goto cpy_name; | |
089dd79d PZ |
6459 | } |
6460 | ||
c7e548b4 ON |
6461 | name = "//anon"; |
6462 | goto cpy_name; | |
0a4a9391 PZ |
6463 | } |
6464 | ||
c7e548b4 ON |
6465 | cpy_name: |
6466 | strlcpy(tmp, name, sizeof(tmp)); | |
6467 | name = tmp; | |
0a4a9391 | 6468 | got_name: |
2c42cfbf PZ |
6469 | /* |
6470 | * Since our buffer works in 8 byte units we need to align our string | |
6471 | * size to a multiple of 8. However, we must guarantee the tail end is | |
6472 | * zero'd out to avoid leaking random bits to userspace. | |
6473 | */ | |
6474 | size = strlen(name)+1; | |
6475 | while (!IS_ALIGNED(size, sizeof(u64))) | |
6476 | name[size++] = '\0'; | |
0a4a9391 PZ |
6477 | |
6478 | mmap_event->file_name = name; | |
6479 | mmap_event->file_size = size; | |
13d7a241 SE |
6480 | mmap_event->maj = maj; |
6481 | mmap_event->min = min; | |
6482 | mmap_event->ino = ino; | |
6483 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
6484 | mmap_event->prot = prot; |
6485 | mmap_event->flags = flags; | |
0a4a9391 | 6486 | |
2fe85427 SE |
6487 | if (!(vma->vm_flags & VM_EXEC)) |
6488 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
6489 | ||
cdd6c482 | 6490 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 6491 | |
aab5b71e | 6492 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
6493 | mmap_event, |
6494 | NULL); | |
665c2142 | 6495 | |
0a4a9391 PZ |
6496 | kfree(buf); |
6497 | } | |
6498 | ||
375637bc AS |
6499 | /* |
6500 | * Whether this @filter depends on a dynamic object which is not loaded | |
6501 | * yet or its load addresses are not known. | |
6502 | */ | |
6503 | static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter) | |
6504 | { | |
6505 | return filter->filter && filter->inode; | |
6506 | } | |
6507 | ||
6508 | /* | |
6509 | * Check whether inode and address range match filter criteria. | |
6510 | */ | |
6511 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
6512 | struct file *file, unsigned long offset, | |
6513 | unsigned long size) | |
6514 | { | |
6515 | if (filter->inode != file->f_inode) | |
6516 | return false; | |
6517 | ||
6518 | if (filter->offset > offset + size) | |
6519 | return false; | |
6520 | ||
6521 | if (filter->offset + filter->size < offset) | |
6522 | return false; | |
6523 | ||
6524 | return true; | |
6525 | } | |
6526 | ||
6527 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) | |
6528 | { | |
6529 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6530 | struct vm_area_struct *vma = data; | |
6531 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; | |
6532 | struct file *file = vma->vm_file; | |
6533 | struct perf_addr_filter *filter; | |
6534 | unsigned int restart = 0, count = 0; | |
6535 | ||
6536 | if (!has_addr_filter(event)) | |
6537 | return; | |
6538 | ||
6539 | if (!file) | |
6540 | return; | |
6541 | ||
6542 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
6543 | list_for_each_entry(filter, &ifh->list, entry) { | |
6544 | if (perf_addr_filter_match(filter, file, off, | |
6545 | vma->vm_end - vma->vm_start)) { | |
6546 | event->addr_filters_offs[count] = vma->vm_start; | |
6547 | restart++; | |
6548 | } | |
6549 | ||
6550 | count++; | |
6551 | } | |
6552 | ||
6553 | if (restart) | |
6554 | event->addr_filters_gen++; | |
6555 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
6556 | ||
6557 | if (restart) | |
6558 | perf_event_restart(event); | |
6559 | } | |
6560 | ||
6561 | /* | |
6562 | * Adjust all task's events' filters to the new vma | |
6563 | */ | |
6564 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
6565 | { | |
6566 | struct perf_event_context *ctx; | |
6567 | int ctxn; | |
6568 | ||
6569 | rcu_read_lock(); | |
6570 | for_each_task_context_nr(ctxn) { | |
6571 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
6572 | if (!ctx) | |
6573 | continue; | |
6574 | ||
aab5b71e | 6575 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
6576 | } |
6577 | rcu_read_unlock(); | |
6578 | } | |
6579 | ||
3af9e859 | 6580 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 6581 | { |
9ee318a7 PZ |
6582 | struct perf_mmap_event mmap_event; |
6583 | ||
cdd6c482 | 6584 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
6585 | return; |
6586 | ||
6587 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 6588 | .vma = vma, |
573402db PZ |
6589 | /* .file_name */ |
6590 | /* .file_size */ | |
cdd6c482 | 6591 | .event_id = { |
573402db | 6592 | .header = { |
cdd6c482 | 6593 | .type = PERF_RECORD_MMAP, |
39447b38 | 6594 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
6595 | /* .size */ |
6596 | }, | |
6597 | /* .pid */ | |
6598 | /* .tid */ | |
089dd79d PZ |
6599 | .start = vma->vm_start, |
6600 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 6601 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 6602 | }, |
13d7a241 SE |
6603 | /* .maj (attr_mmap2 only) */ |
6604 | /* .min (attr_mmap2 only) */ | |
6605 | /* .ino (attr_mmap2 only) */ | |
6606 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
6607 | /* .prot (attr_mmap2 only) */ |
6608 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
6609 | }; |
6610 | ||
375637bc | 6611 | perf_addr_filters_adjust(vma); |
cdd6c482 | 6612 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
6613 | } |
6614 | ||
68db7e98 AS |
6615 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
6616 | unsigned long size, u64 flags) | |
6617 | { | |
6618 | struct perf_output_handle handle; | |
6619 | struct perf_sample_data sample; | |
6620 | struct perf_aux_event { | |
6621 | struct perf_event_header header; | |
6622 | u64 offset; | |
6623 | u64 size; | |
6624 | u64 flags; | |
6625 | } rec = { | |
6626 | .header = { | |
6627 | .type = PERF_RECORD_AUX, | |
6628 | .misc = 0, | |
6629 | .size = sizeof(rec), | |
6630 | }, | |
6631 | .offset = head, | |
6632 | .size = size, | |
6633 | .flags = flags, | |
6634 | }; | |
6635 | int ret; | |
6636 | ||
6637 | perf_event_header__init_id(&rec.header, &sample, event); | |
6638 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6639 | ||
6640 | if (ret) | |
6641 | return; | |
6642 | ||
6643 | perf_output_put(&handle, rec); | |
6644 | perf_event__output_id_sample(event, &handle, &sample); | |
6645 | ||
6646 | perf_output_end(&handle); | |
6647 | } | |
6648 | ||
f38b0dbb KL |
6649 | /* |
6650 | * Lost/dropped samples logging | |
6651 | */ | |
6652 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
6653 | { | |
6654 | struct perf_output_handle handle; | |
6655 | struct perf_sample_data sample; | |
6656 | int ret; | |
6657 | ||
6658 | struct { | |
6659 | struct perf_event_header header; | |
6660 | u64 lost; | |
6661 | } lost_samples_event = { | |
6662 | .header = { | |
6663 | .type = PERF_RECORD_LOST_SAMPLES, | |
6664 | .misc = 0, | |
6665 | .size = sizeof(lost_samples_event), | |
6666 | }, | |
6667 | .lost = lost, | |
6668 | }; | |
6669 | ||
6670 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
6671 | ||
6672 | ret = perf_output_begin(&handle, event, | |
6673 | lost_samples_event.header.size); | |
6674 | if (ret) | |
6675 | return; | |
6676 | ||
6677 | perf_output_put(&handle, lost_samples_event); | |
6678 | perf_event__output_id_sample(event, &handle, &sample); | |
6679 | perf_output_end(&handle); | |
6680 | } | |
6681 | ||
45ac1403 AH |
6682 | /* |
6683 | * context_switch tracking | |
6684 | */ | |
6685 | ||
6686 | struct perf_switch_event { | |
6687 | struct task_struct *task; | |
6688 | struct task_struct *next_prev; | |
6689 | ||
6690 | struct { | |
6691 | struct perf_event_header header; | |
6692 | u32 next_prev_pid; | |
6693 | u32 next_prev_tid; | |
6694 | } event_id; | |
6695 | }; | |
6696 | ||
6697 | static int perf_event_switch_match(struct perf_event *event) | |
6698 | { | |
6699 | return event->attr.context_switch; | |
6700 | } | |
6701 | ||
6702 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
6703 | { | |
6704 | struct perf_switch_event *se = data; | |
6705 | struct perf_output_handle handle; | |
6706 | struct perf_sample_data sample; | |
6707 | int ret; | |
6708 | ||
6709 | if (!perf_event_switch_match(event)) | |
6710 | return; | |
6711 | ||
6712 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
6713 | if (event->ctx->task) { | |
6714 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
6715 | se->event_id.header.size = sizeof(se->event_id.header); | |
6716 | } else { | |
6717 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
6718 | se->event_id.header.size = sizeof(se->event_id); | |
6719 | se->event_id.next_prev_pid = | |
6720 | perf_event_pid(event, se->next_prev); | |
6721 | se->event_id.next_prev_tid = | |
6722 | perf_event_tid(event, se->next_prev); | |
6723 | } | |
6724 | ||
6725 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
6726 | ||
6727 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
6728 | if (ret) | |
6729 | return; | |
6730 | ||
6731 | if (event->ctx->task) | |
6732 | perf_output_put(&handle, se->event_id.header); | |
6733 | else | |
6734 | perf_output_put(&handle, se->event_id); | |
6735 | ||
6736 | perf_event__output_id_sample(event, &handle, &sample); | |
6737 | ||
6738 | perf_output_end(&handle); | |
6739 | } | |
6740 | ||
6741 | static void perf_event_switch(struct task_struct *task, | |
6742 | struct task_struct *next_prev, bool sched_in) | |
6743 | { | |
6744 | struct perf_switch_event switch_event; | |
6745 | ||
6746 | /* N.B. caller checks nr_switch_events != 0 */ | |
6747 | ||
6748 | switch_event = (struct perf_switch_event){ | |
6749 | .task = task, | |
6750 | .next_prev = next_prev, | |
6751 | .event_id = { | |
6752 | .header = { | |
6753 | /* .type */ | |
6754 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
6755 | /* .size */ | |
6756 | }, | |
6757 | /* .next_prev_pid */ | |
6758 | /* .next_prev_tid */ | |
6759 | }, | |
6760 | }; | |
6761 | ||
aab5b71e | 6762 | perf_iterate_sb(perf_event_switch_output, |
45ac1403 AH |
6763 | &switch_event, |
6764 | NULL); | |
6765 | } | |
6766 | ||
a78ac325 PZ |
6767 | /* |
6768 | * IRQ throttle logging | |
6769 | */ | |
6770 | ||
cdd6c482 | 6771 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
6772 | { |
6773 | struct perf_output_handle handle; | |
c980d109 | 6774 | struct perf_sample_data sample; |
a78ac325 PZ |
6775 | int ret; |
6776 | ||
6777 | struct { | |
6778 | struct perf_event_header header; | |
6779 | u64 time; | |
cca3f454 | 6780 | u64 id; |
7f453c24 | 6781 | u64 stream_id; |
a78ac325 PZ |
6782 | } throttle_event = { |
6783 | .header = { | |
cdd6c482 | 6784 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
6785 | .misc = 0, |
6786 | .size = sizeof(throttle_event), | |
6787 | }, | |
34f43927 | 6788 | .time = perf_event_clock(event), |
cdd6c482 IM |
6789 | .id = primary_event_id(event), |
6790 | .stream_id = event->id, | |
a78ac325 PZ |
6791 | }; |
6792 | ||
966ee4d6 | 6793 | if (enable) |
cdd6c482 | 6794 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 6795 | |
c980d109 ACM |
6796 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
6797 | ||
6798 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6799 | throttle_event.header.size); |
a78ac325 PZ |
6800 | if (ret) |
6801 | return; | |
6802 | ||
6803 | perf_output_put(&handle, throttle_event); | |
c980d109 | 6804 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
6805 | perf_output_end(&handle); |
6806 | } | |
6807 | ||
ec0d7729 AS |
6808 | static void perf_log_itrace_start(struct perf_event *event) |
6809 | { | |
6810 | struct perf_output_handle handle; | |
6811 | struct perf_sample_data sample; | |
6812 | struct perf_aux_event { | |
6813 | struct perf_event_header header; | |
6814 | u32 pid; | |
6815 | u32 tid; | |
6816 | } rec; | |
6817 | int ret; | |
6818 | ||
6819 | if (event->parent) | |
6820 | event = event->parent; | |
6821 | ||
6822 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
6823 | event->hw.itrace_started) | |
6824 | return; | |
6825 | ||
ec0d7729 AS |
6826 | rec.header.type = PERF_RECORD_ITRACE_START; |
6827 | rec.header.misc = 0; | |
6828 | rec.header.size = sizeof(rec); | |
6829 | rec.pid = perf_event_pid(event, current); | |
6830 | rec.tid = perf_event_tid(event, current); | |
6831 | ||
6832 | perf_event_header__init_id(&rec.header, &sample, event); | |
6833 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6834 | ||
6835 | if (ret) | |
6836 | return; | |
6837 | ||
6838 | perf_output_put(&handle, rec); | |
6839 | perf_event__output_id_sample(event, &handle, &sample); | |
6840 | ||
6841 | perf_output_end(&handle); | |
6842 | } | |
6843 | ||
f6c7d5fe | 6844 | /* |
cdd6c482 | 6845 | * Generic event overflow handling, sampling. |
f6c7d5fe PZ |
6846 | */ |
6847 | ||
a8b0ca17 | 6848 | static int __perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6849 | int throttle, struct perf_sample_data *data, |
6850 | struct pt_regs *regs) | |
f6c7d5fe | 6851 | { |
cdd6c482 IM |
6852 | int events = atomic_read(&event->event_limit); |
6853 | struct hw_perf_event *hwc = &event->hw; | |
e050e3f0 | 6854 | u64 seq; |
79f14641 PZ |
6855 | int ret = 0; |
6856 | ||
96398826 PZ |
6857 | /* |
6858 | * Non-sampling counters might still use the PMI to fold short | |
6859 | * hardware counters, ignore those. | |
6860 | */ | |
6861 | if (unlikely(!is_sampling_event(event))) | |
6862 | return 0; | |
6863 | ||
e050e3f0 SE |
6864 | seq = __this_cpu_read(perf_throttled_seq); |
6865 | if (seq != hwc->interrupts_seq) { | |
6866 | hwc->interrupts_seq = seq; | |
6867 | hwc->interrupts = 1; | |
6868 | } else { | |
6869 | hwc->interrupts++; | |
6870 | if (unlikely(throttle | |
6871 | && hwc->interrupts >= max_samples_per_tick)) { | |
6872 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 6873 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
6874 | hwc->interrupts = MAX_INTERRUPTS; |
6875 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
6876 | ret = 1; |
6877 | } | |
e050e3f0 | 6878 | } |
60db5e09 | 6879 | |
cdd6c482 | 6880 | if (event->attr.freq) { |
def0a9b2 | 6881 | u64 now = perf_clock(); |
abd50713 | 6882 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 6883 | |
abd50713 | 6884 | hwc->freq_time_stamp = now; |
bd2b5b12 | 6885 | |
abd50713 | 6886 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 6887 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
6888 | } |
6889 | ||
2023b359 PZ |
6890 | /* |
6891 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 6892 | * events |
2023b359 PZ |
6893 | */ |
6894 | ||
cdd6c482 IM |
6895 | event->pending_kill = POLL_IN; |
6896 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 6897 | ret = 1; |
cdd6c482 | 6898 | event->pending_kill = POLL_HUP; |
a8b0ca17 PZ |
6899 | event->pending_disable = 1; |
6900 | irq_work_queue(&event->pending); | |
79f14641 PZ |
6901 | } |
6902 | ||
1879445d | 6903 | event->overflow_handler(event, data, regs); |
453f19ee | 6904 | |
fed66e2c | 6905 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
6906 | event->pending_wakeup = 1; |
6907 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
6908 | } |
6909 | ||
79f14641 | 6910 | return ret; |
f6c7d5fe PZ |
6911 | } |
6912 | ||
a8b0ca17 | 6913 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6914 | struct perf_sample_data *data, |
6915 | struct pt_regs *regs) | |
850bc73f | 6916 | { |
a8b0ca17 | 6917 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
6918 | } |
6919 | ||
15dbf27c | 6920 | /* |
cdd6c482 | 6921 | * Generic software event infrastructure |
15dbf27c PZ |
6922 | */ |
6923 | ||
b28ab83c PZ |
6924 | struct swevent_htable { |
6925 | struct swevent_hlist *swevent_hlist; | |
6926 | struct mutex hlist_mutex; | |
6927 | int hlist_refcount; | |
6928 | ||
6929 | /* Recursion avoidance in each contexts */ | |
6930 | int recursion[PERF_NR_CONTEXTS]; | |
6931 | }; | |
6932 | ||
6933 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
6934 | ||
7b4b6658 | 6935 | /* |
cdd6c482 IM |
6936 | * We directly increment event->count and keep a second value in |
6937 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
6938 | * is kept in the range [-sample_period, 0] so that we can use the |
6939 | * sign as trigger. | |
6940 | */ | |
6941 | ||
ab573844 | 6942 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 6943 | { |
cdd6c482 | 6944 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
6945 | u64 period = hwc->last_period; |
6946 | u64 nr, offset; | |
6947 | s64 old, val; | |
6948 | ||
6949 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
6950 | |
6951 | again: | |
e7850595 | 6952 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
6953 | if (val < 0) |
6954 | return 0; | |
15dbf27c | 6955 | |
7b4b6658 PZ |
6956 | nr = div64_u64(period + val, period); |
6957 | offset = nr * period; | |
6958 | val -= offset; | |
e7850595 | 6959 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 6960 | goto again; |
15dbf27c | 6961 | |
7b4b6658 | 6962 | return nr; |
15dbf27c PZ |
6963 | } |
6964 | ||
0cff784a | 6965 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 6966 | struct perf_sample_data *data, |
5622f295 | 6967 | struct pt_regs *regs) |
15dbf27c | 6968 | { |
cdd6c482 | 6969 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 6970 | int throttle = 0; |
15dbf27c | 6971 | |
0cff784a PZ |
6972 | if (!overflow) |
6973 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 6974 | |
7b4b6658 PZ |
6975 | if (hwc->interrupts == MAX_INTERRUPTS) |
6976 | return; | |
15dbf27c | 6977 | |
7b4b6658 | 6978 | for (; overflow; overflow--) { |
a8b0ca17 | 6979 | if (__perf_event_overflow(event, throttle, |
5622f295 | 6980 | data, regs)) { |
7b4b6658 PZ |
6981 | /* |
6982 | * We inhibit the overflow from happening when | |
6983 | * hwc->interrupts == MAX_INTERRUPTS. | |
6984 | */ | |
6985 | break; | |
6986 | } | |
cf450a73 | 6987 | throttle = 1; |
7b4b6658 | 6988 | } |
15dbf27c PZ |
6989 | } |
6990 | ||
a4eaf7f1 | 6991 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 6992 | struct perf_sample_data *data, |
5622f295 | 6993 | struct pt_regs *regs) |
7b4b6658 | 6994 | { |
cdd6c482 | 6995 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 6996 | |
e7850595 | 6997 | local64_add(nr, &event->count); |
d6d020e9 | 6998 | |
0cff784a PZ |
6999 | if (!regs) |
7000 | return; | |
7001 | ||
6c7e550f | 7002 | if (!is_sampling_event(event)) |
7b4b6658 | 7003 | return; |
d6d020e9 | 7004 | |
5d81e5cf AV |
7005 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
7006 | data->period = nr; | |
7007 | return perf_swevent_overflow(event, 1, data, regs); | |
7008 | } else | |
7009 | data->period = event->hw.last_period; | |
7010 | ||
0cff784a | 7011 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 7012 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 7013 | |
e7850595 | 7014 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 7015 | return; |
df1a132b | 7016 | |
a8b0ca17 | 7017 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
7018 | } |
7019 | ||
f5ffe02e FW |
7020 | static int perf_exclude_event(struct perf_event *event, |
7021 | struct pt_regs *regs) | |
7022 | { | |
a4eaf7f1 | 7023 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 7024 | return 1; |
a4eaf7f1 | 7025 | |
f5ffe02e FW |
7026 | if (regs) { |
7027 | if (event->attr.exclude_user && user_mode(regs)) | |
7028 | return 1; | |
7029 | ||
7030 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
7031 | return 1; | |
7032 | } | |
7033 | ||
7034 | return 0; | |
7035 | } | |
7036 | ||
cdd6c482 | 7037 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 7038 | enum perf_type_id type, |
6fb2915d LZ |
7039 | u32 event_id, |
7040 | struct perf_sample_data *data, | |
7041 | struct pt_regs *regs) | |
15dbf27c | 7042 | { |
cdd6c482 | 7043 | if (event->attr.type != type) |
a21ca2ca | 7044 | return 0; |
f5ffe02e | 7045 | |
cdd6c482 | 7046 | if (event->attr.config != event_id) |
15dbf27c PZ |
7047 | return 0; |
7048 | ||
f5ffe02e FW |
7049 | if (perf_exclude_event(event, regs)) |
7050 | return 0; | |
15dbf27c PZ |
7051 | |
7052 | return 1; | |
7053 | } | |
7054 | ||
76e1d904 FW |
7055 | static inline u64 swevent_hash(u64 type, u32 event_id) |
7056 | { | |
7057 | u64 val = event_id | (type << 32); | |
7058 | ||
7059 | return hash_64(val, SWEVENT_HLIST_BITS); | |
7060 | } | |
7061 | ||
49f135ed FW |
7062 | static inline struct hlist_head * |
7063 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 7064 | { |
49f135ed FW |
7065 | u64 hash = swevent_hash(type, event_id); |
7066 | ||
7067 | return &hlist->heads[hash]; | |
7068 | } | |
76e1d904 | 7069 | |
49f135ed FW |
7070 | /* For the read side: events when they trigger */ |
7071 | static inline struct hlist_head * | |
b28ab83c | 7072 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
7073 | { |
7074 | struct swevent_hlist *hlist; | |
76e1d904 | 7075 | |
b28ab83c | 7076 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
7077 | if (!hlist) |
7078 | return NULL; | |
7079 | ||
49f135ed FW |
7080 | return __find_swevent_head(hlist, type, event_id); |
7081 | } | |
7082 | ||
7083 | /* For the event head insertion and removal in the hlist */ | |
7084 | static inline struct hlist_head * | |
b28ab83c | 7085 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
7086 | { |
7087 | struct swevent_hlist *hlist; | |
7088 | u32 event_id = event->attr.config; | |
7089 | u64 type = event->attr.type; | |
7090 | ||
7091 | /* | |
7092 | * Event scheduling is always serialized against hlist allocation | |
7093 | * and release. Which makes the protected version suitable here. | |
7094 | * The context lock guarantees that. | |
7095 | */ | |
b28ab83c | 7096 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
7097 | lockdep_is_held(&event->ctx->lock)); |
7098 | if (!hlist) | |
7099 | return NULL; | |
7100 | ||
7101 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
7102 | } |
7103 | ||
7104 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 7105 | u64 nr, |
76e1d904 FW |
7106 | struct perf_sample_data *data, |
7107 | struct pt_regs *regs) | |
15dbf27c | 7108 | { |
4a32fea9 | 7109 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7110 | struct perf_event *event; |
76e1d904 | 7111 | struct hlist_head *head; |
15dbf27c | 7112 | |
76e1d904 | 7113 | rcu_read_lock(); |
b28ab83c | 7114 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
7115 | if (!head) |
7116 | goto end; | |
7117 | ||
b67bfe0d | 7118 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 7119 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 7120 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 7121 | } |
76e1d904 FW |
7122 | end: |
7123 | rcu_read_unlock(); | |
15dbf27c PZ |
7124 | } |
7125 | ||
86038c5e PZI |
7126 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
7127 | ||
4ed7c92d | 7128 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 7129 | { |
4a32fea9 | 7130 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 7131 | |
b28ab83c | 7132 | return get_recursion_context(swhash->recursion); |
96f6d444 | 7133 | } |
645e8cc0 | 7134 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 7135 | |
fa9f90be | 7136 | inline void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 7137 | { |
4a32fea9 | 7138 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 7139 | |
b28ab83c | 7140 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 7141 | } |
15dbf27c | 7142 | |
86038c5e | 7143 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 7144 | { |
a4234bfc | 7145 | struct perf_sample_data data; |
4ed7c92d | 7146 | |
86038c5e | 7147 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 7148 | return; |
a4234bfc | 7149 | |
fd0d000b | 7150 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 7151 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
7152 | } |
7153 | ||
7154 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
7155 | { | |
7156 | int rctx; | |
7157 | ||
7158 | preempt_disable_notrace(); | |
7159 | rctx = perf_swevent_get_recursion_context(); | |
7160 | if (unlikely(rctx < 0)) | |
7161 | goto fail; | |
7162 | ||
7163 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
7164 | |
7165 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 7166 | fail: |
1c024eca | 7167 | preempt_enable_notrace(); |
b8e83514 PZ |
7168 | } |
7169 | ||
cdd6c482 | 7170 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 7171 | { |
15dbf27c PZ |
7172 | } |
7173 | ||
a4eaf7f1 | 7174 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 7175 | { |
4a32fea9 | 7176 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7177 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
7178 | struct hlist_head *head; |
7179 | ||
6c7e550f | 7180 | if (is_sampling_event(event)) { |
7b4b6658 | 7181 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 7182 | perf_swevent_set_period(event); |
7b4b6658 | 7183 | } |
76e1d904 | 7184 | |
a4eaf7f1 PZ |
7185 | hwc->state = !(flags & PERF_EF_START); |
7186 | ||
b28ab83c | 7187 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 7188 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
7189 | return -EINVAL; |
7190 | ||
7191 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 7192 | perf_event_update_userpage(event); |
76e1d904 | 7193 | |
15dbf27c PZ |
7194 | return 0; |
7195 | } | |
7196 | ||
a4eaf7f1 | 7197 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 7198 | { |
76e1d904 | 7199 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
7200 | } |
7201 | ||
a4eaf7f1 | 7202 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 7203 | { |
a4eaf7f1 | 7204 | event->hw.state = 0; |
d6d020e9 | 7205 | } |
aa9c4c0f | 7206 | |
a4eaf7f1 | 7207 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 7208 | { |
a4eaf7f1 | 7209 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
7210 | } |
7211 | ||
49f135ed FW |
7212 | /* Deref the hlist from the update side */ |
7213 | static inline struct swevent_hlist * | |
b28ab83c | 7214 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 7215 | { |
b28ab83c PZ |
7216 | return rcu_dereference_protected(swhash->swevent_hlist, |
7217 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
7218 | } |
7219 | ||
b28ab83c | 7220 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 7221 | { |
b28ab83c | 7222 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 7223 | |
49f135ed | 7224 | if (!hlist) |
76e1d904 FW |
7225 | return; |
7226 | ||
70691d4a | 7227 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 7228 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
7229 | } |
7230 | ||
3b364d7b | 7231 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 7232 | { |
b28ab83c | 7233 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 7234 | |
b28ab83c | 7235 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 7236 | |
b28ab83c PZ |
7237 | if (!--swhash->hlist_refcount) |
7238 | swevent_hlist_release(swhash); | |
76e1d904 | 7239 | |
b28ab83c | 7240 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7241 | } |
7242 | ||
3b364d7b | 7243 | static void swevent_hlist_put(void) |
76e1d904 FW |
7244 | { |
7245 | int cpu; | |
7246 | ||
76e1d904 | 7247 | for_each_possible_cpu(cpu) |
3b364d7b | 7248 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7249 | } |
7250 | ||
3b364d7b | 7251 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 7252 | { |
b28ab83c | 7253 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
7254 | int err = 0; |
7255 | ||
b28ab83c | 7256 | mutex_lock(&swhash->hlist_mutex); |
b28ab83c | 7257 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
76e1d904 FW |
7258 | struct swevent_hlist *hlist; |
7259 | ||
7260 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
7261 | if (!hlist) { | |
7262 | err = -ENOMEM; | |
7263 | goto exit; | |
7264 | } | |
b28ab83c | 7265 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 7266 | } |
b28ab83c | 7267 | swhash->hlist_refcount++; |
9ed6060d | 7268 | exit: |
b28ab83c | 7269 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7270 | |
7271 | return err; | |
7272 | } | |
7273 | ||
3b364d7b | 7274 | static int swevent_hlist_get(void) |
76e1d904 | 7275 | { |
3b364d7b | 7276 | int err, cpu, failed_cpu; |
76e1d904 | 7277 | |
76e1d904 FW |
7278 | get_online_cpus(); |
7279 | for_each_possible_cpu(cpu) { | |
3b364d7b | 7280 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
7281 | if (err) { |
7282 | failed_cpu = cpu; | |
7283 | goto fail; | |
7284 | } | |
7285 | } | |
7286 | put_online_cpus(); | |
7287 | ||
7288 | return 0; | |
9ed6060d | 7289 | fail: |
76e1d904 FW |
7290 | for_each_possible_cpu(cpu) { |
7291 | if (cpu == failed_cpu) | |
7292 | break; | |
3b364d7b | 7293 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7294 | } |
7295 | ||
7296 | put_online_cpus(); | |
7297 | return err; | |
7298 | } | |
7299 | ||
c5905afb | 7300 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 7301 | |
b0a873eb PZ |
7302 | static void sw_perf_event_destroy(struct perf_event *event) |
7303 | { | |
7304 | u64 event_id = event->attr.config; | |
95476b64 | 7305 | |
b0a873eb PZ |
7306 | WARN_ON(event->parent); |
7307 | ||
c5905afb | 7308 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 7309 | swevent_hlist_put(); |
b0a873eb PZ |
7310 | } |
7311 | ||
7312 | static int perf_swevent_init(struct perf_event *event) | |
7313 | { | |
8176cced | 7314 | u64 event_id = event->attr.config; |
b0a873eb PZ |
7315 | |
7316 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7317 | return -ENOENT; | |
7318 | ||
2481c5fa SE |
7319 | /* |
7320 | * no branch sampling for software events | |
7321 | */ | |
7322 | if (has_branch_stack(event)) | |
7323 | return -EOPNOTSUPP; | |
7324 | ||
b0a873eb PZ |
7325 | switch (event_id) { |
7326 | case PERF_COUNT_SW_CPU_CLOCK: | |
7327 | case PERF_COUNT_SW_TASK_CLOCK: | |
7328 | return -ENOENT; | |
7329 | ||
7330 | default: | |
7331 | break; | |
7332 | } | |
7333 | ||
ce677831 | 7334 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
7335 | return -ENOENT; |
7336 | ||
7337 | if (!event->parent) { | |
7338 | int err; | |
7339 | ||
3b364d7b | 7340 | err = swevent_hlist_get(); |
b0a873eb PZ |
7341 | if (err) |
7342 | return err; | |
7343 | ||
c5905afb | 7344 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
7345 | event->destroy = sw_perf_event_destroy; |
7346 | } | |
7347 | ||
7348 | return 0; | |
7349 | } | |
7350 | ||
7351 | static struct pmu perf_swevent = { | |
89a1e187 | 7352 | .task_ctx_nr = perf_sw_context, |
95476b64 | 7353 | |
34f43927 PZ |
7354 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7355 | ||
b0a873eb | 7356 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
7357 | .add = perf_swevent_add, |
7358 | .del = perf_swevent_del, | |
7359 | .start = perf_swevent_start, | |
7360 | .stop = perf_swevent_stop, | |
1c024eca | 7361 | .read = perf_swevent_read, |
1c024eca PZ |
7362 | }; |
7363 | ||
b0a873eb PZ |
7364 | #ifdef CONFIG_EVENT_TRACING |
7365 | ||
1c024eca PZ |
7366 | static int perf_tp_filter_match(struct perf_event *event, |
7367 | struct perf_sample_data *data) | |
7368 | { | |
7369 | void *record = data->raw->data; | |
7370 | ||
b71b437e PZ |
7371 | /* only top level events have filters set */ |
7372 | if (event->parent) | |
7373 | event = event->parent; | |
7374 | ||
1c024eca PZ |
7375 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
7376 | return 1; | |
7377 | return 0; | |
7378 | } | |
7379 | ||
7380 | static int perf_tp_event_match(struct perf_event *event, | |
7381 | struct perf_sample_data *data, | |
7382 | struct pt_regs *regs) | |
7383 | { | |
a0f7d0f7 FW |
7384 | if (event->hw.state & PERF_HES_STOPPED) |
7385 | return 0; | |
580d607c PZ |
7386 | /* |
7387 | * All tracepoints are from kernel-space. | |
7388 | */ | |
7389 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
7390 | return 0; |
7391 | ||
7392 | if (!perf_tp_filter_match(event, data)) | |
7393 | return 0; | |
7394 | ||
7395 | return 1; | |
7396 | } | |
7397 | ||
7398 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, | |
e6dab5ff AV |
7399 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
7400 | struct task_struct *task) | |
95476b64 FW |
7401 | { |
7402 | struct perf_sample_data data; | |
1c024eca | 7403 | struct perf_event *event; |
1c024eca | 7404 | |
95476b64 FW |
7405 | struct perf_raw_record raw = { |
7406 | .size = entry_size, | |
7407 | .data = record, | |
7408 | }; | |
7409 | ||
fd0d000b | 7410 | perf_sample_data_init(&data, addr, 0); |
95476b64 FW |
7411 | data.raw = &raw; |
7412 | ||
b67bfe0d | 7413 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 7414 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 7415 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 7416 | } |
ecc55f84 | 7417 | |
e6dab5ff AV |
7418 | /* |
7419 | * If we got specified a target task, also iterate its context and | |
7420 | * deliver this event there too. | |
7421 | */ | |
7422 | if (task && task != current) { | |
7423 | struct perf_event_context *ctx; | |
7424 | struct trace_entry *entry = record; | |
7425 | ||
7426 | rcu_read_lock(); | |
7427 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
7428 | if (!ctx) | |
7429 | goto unlock; | |
7430 | ||
7431 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
7432 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7433 | continue; | |
7434 | if (event->attr.config != entry->type) | |
7435 | continue; | |
7436 | if (perf_tp_event_match(event, &data, regs)) | |
7437 | perf_swevent_event(event, count, &data, regs); | |
7438 | } | |
7439 | unlock: | |
7440 | rcu_read_unlock(); | |
7441 | } | |
7442 | ||
ecc55f84 | 7443 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
7444 | } |
7445 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
7446 | ||
cdd6c482 | 7447 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 7448 | { |
1c024eca | 7449 | perf_trace_destroy(event); |
e077df4f PZ |
7450 | } |
7451 | ||
b0a873eb | 7452 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 7453 | { |
76e1d904 FW |
7454 | int err; |
7455 | ||
b0a873eb PZ |
7456 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
7457 | return -ENOENT; | |
7458 | ||
2481c5fa SE |
7459 | /* |
7460 | * no branch sampling for tracepoint events | |
7461 | */ | |
7462 | if (has_branch_stack(event)) | |
7463 | return -EOPNOTSUPP; | |
7464 | ||
1c024eca PZ |
7465 | err = perf_trace_init(event); |
7466 | if (err) | |
b0a873eb | 7467 | return err; |
e077df4f | 7468 | |
cdd6c482 | 7469 | event->destroy = tp_perf_event_destroy; |
e077df4f | 7470 | |
b0a873eb PZ |
7471 | return 0; |
7472 | } | |
7473 | ||
7474 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
7475 | .task_ctx_nr = perf_sw_context, |
7476 | ||
b0a873eb | 7477 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
7478 | .add = perf_trace_add, |
7479 | .del = perf_trace_del, | |
7480 | .start = perf_swevent_start, | |
7481 | .stop = perf_swevent_stop, | |
b0a873eb | 7482 | .read = perf_swevent_read, |
b0a873eb PZ |
7483 | }; |
7484 | ||
7485 | static inline void perf_tp_register(void) | |
7486 | { | |
2e80a82a | 7487 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e077df4f | 7488 | } |
6fb2915d | 7489 | |
6fb2915d LZ |
7490 | static void perf_event_free_filter(struct perf_event *event) |
7491 | { | |
7492 | ftrace_profile_free_filter(event); | |
7493 | } | |
7494 | ||
2541517c AS |
7495 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7496 | { | |
7497 | struct bpf_prog *prog; | |
7498 | ||
7499 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7500 | return -EINVAL; | |
7501 | ||
7502 | if (event->tp_event->prog) | |
7503 | return -EEXIST; | |
7504 | ||
04a22fae WN |
7505 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
7506 | /* bpf programs can only be attached to u/kprobes */ | |
2541517c AS |
7507 | return -EINVAL; |
7508 | ||
7509 | prog = bpf_prog_get(prog_fd); | |
7510 | if (IS_ERR(prog)) | |
7511 | return PTR_ERR(prog); | |
7512 | ||
6c373ca8 | 7513 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
2541517c AS |
7514 | /* valid fd, but invalid bpf program type */ |
7515 | bpf_prog_put(prog); | |
7516 | return -EINVAL; | |
7517 | } | |
7518 | ||
7519 | event->tp_event->prog = prog; | |
7520 | ||
7521 | return 0; | |
7522 | } | |
7523 | ||
7524 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7525 | { | |
7526 | struct bpf_prog *prog; | |
7527 | ||
7528 | if (!event->tp_event) | |
7529 | return; | |
7530 | ||
7531 | prog = event->tp_event->prog; | |
7532 | if (prog) { | |
7533 | event->tp_event->prog = NULL; | |
7534 | bpf_prog_put(prog); | |
7535 | } | |
7536 | } | |
7537 | ||
e077df4f | 7538 | #else |
6fb2915d | 7539 | |
b0a873eb | 7540 | static inline void perf_tp_register(void) |
e077df4f | 7541 | { |
e077df4f | 7542 | } |
6fb2915d | 7543 | |
6fb2915d LZ |
7544 | static void perf_event_free_filter(struct perf_event *event) |
7545 | { | |
7546 | } | |
7547 | ||
2541517c AS |
7548 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7549 | { | |
7550 | return -ENOENT; | |
7551 | } | |
7552 | ||
7553 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7554 | { | |
7555 | } | |
07b139c8 | 7556 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 7557 | |
24f1e32c | 7558 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 7559 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 7560 | { |
f5ffe02e FW |
7561 | struct perf_sample_data sample; |
7562 | struct pt_regs *regs = data; | |
7563 | ||
fd0d000b | 7564 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 7565 | |
a4eaf7f1 | 7566 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 7567 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
7568 | } |
7569 | #endif | |
7570 | ||
375637bc AS |
7571 | /* |
7572 | * Allocate a new address filter | |
7573 | */ | |
7574 | static struct perf_addr_filter * | |
7575 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
7576 | { | |
7577 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
7578 | struct perf_addr_filter *filter; | |
7579 | ||
7580 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
7581 | if (!filter) | |
7582 | return NULL; | |
7583 | ||
7584 | INIT_LIST_HEAD(&filter->entry); | |
7585 | list_add_tail(&filter->entry, filters); | |
7586 | ||
7587 | return filter; | |
7588 | } | |
7589 | ||
7590 | static void free_filters_list(struct list_head *filters) | |
7591 | { | |
7592 | struct perf_addr_filter *filter, *iter; | |
7593 | ||
7594 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
7595 | if (filter->inode) | |
7596 | iput(filter->inode); | |
7597 | list_del(&filter->entry); | |
7598 | kfree(filter); | |
7599 | } | |
7600 | } | |
7601 | ||
7602 | /* | |
7603 | * Free existing address filters and optionally install new ones | |
7604 | */ | |
7605 | static void perf_addr_filters_splice(struct perf_event *event, | |
7606 | struct list_head *head) | |
7607 | { | |
7608 | unsigned long flags; | |
7609 | LIST_HEAD(list); | |
7610 | ||
7611 | if (!has_addr_filter(event)) | |
7612 | return; | |
7613 | ||
7614 | /* don't bother with children, they don't have their own filters */ | |
7615 | if (event->parent) | |
7616 | return; | |
7617 | ||
7618 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
7619 | ||
7620 | list_splice_init(&event->addr_filters.list, &list); | |
7621 | if (head) | |
7622 | list_splice(head, &event->addr_filters.list); | |
7623 | ||
7624 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
7625 | ||
7626 | free_filters_list(&list); | |
7627 | } | |
7628 | ||
7629 | /* | |
7630 | * Scan through mm's vmas and see if one of them matches the | |
7631 | * @filter; if so, adjust filter's address range. | |
7632 | * Called with mm::mmap_sem down for reading. | |
7633 | */ | |
7634 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, | |
7635 | struct mm_struct *mm) | |
7636 | { | |
7637 | struct vm_area_struct *vma; | |
7638 | ||
7639 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
7640 | struct file *file = vma->vm_file; | |
7641 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
7642 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
7643 | ||
7644 | if (!file) | |
7645 | continue; | |
7646 | ||
7647 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
7648 | continue; | |
7649 | ||
7650 | return vma->vm_start; | |
7651 | } | |
7652 | ||
7653 | return 0; | |
7654 | } | |
7655 | ||
7656 | /* | |
7657 | * Update event's address range filters based on the | |
7658 | * task's existing mappings, if any. | |
7659 | */ | |
7660 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
7661 | { | |
7662 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
7663 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
7664 | struct perf_addr_filter *filter; | |
7665 | struct mm_struct *mm = NULL; | |
7666 | unsigned int count = 0; | |
7667 | unsigned long flags; | |
7668 | ||
7669 | /* | |
7670 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
7671 | * will stop on the parent's child_mutex that our caller is also holding | |
7672 | */ | |
7673 | if (task == TASK_TOMBSTONE) | |
7674 | return; | |
7675 | ||
7676 | mm = get_task_mm(event->ctx->task); | |
7677 | if (!mm) | |
7678 | goto restart; | |
7679 | ||
7680 | down_read(&mm->mmap_sem); | |
7681 | ||
7682 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7683 | list_for_each_entry(filter, &ifh->list, entry) { | |
7684 | event->addr_filters_offs[count] = 0; | |
7685 | ||
7686 | if (perf_addr_filter_needs_mmap(filter)) | |
7687 | event->addr_filters_offs[count] = | |
7688 | perf_addr_filter_apply(filter, mm); | |
7689 | ||
7690 | count++; | |
7691 | } | |
7692 | ||
7693 | event->addr_filters_gen++; | |
7694 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7695 | ||
7696 | up_read(&mm->mmap_sem); | |
7697 | ||
7698 | mmput(mm); | |
7699 | ||
7700 | restart: | |
7701 | perf_event_restart(event); | |
7702 | } | |
7703 | ||
7704 | /* | |
7705 | * Address range filtering: limiting the data to certain | |
7706 | * instruction address ranges. Filters are ioctl()ed to us from | |
7707 | * userspace as ascii strings. | |
7708 | * | |
7709 | * Filter string format: | |
7710 | * | |
7711 | * ACTION RANGE_SPEC | |
7712 | * where ACTION is one of the | |
7713 | * * "filter": limit the trace to this region | |
7714 | * * "start": start tracing from this address | |
7715 | * * "stop": stop tracing at this address/region; | |
7716 | * RANGE_SPEC is | |
7717 | * * for kernel addresses: <start address>[/<size>] | |
7718 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
7719 | * | |
7720 | * if <size> is not specified, the range is treated as a single address. | |
7721 | */ | |
7722 | enum { | |
7723 | IF_ACT_FILTER, | |
7724 | IF_ACT_START, | |
7725 | IF_ACT_STOP, | |
7726 | IF_SRC_FILE, | |
7727 | IF_SRC_KERNEL, | |
7728 | IF_SRC_FILEADDR, | |
7729 | IF_SRC_KERNELADDR, | |
7730 | }; | |
7731 | ||
7732 | enum { | |
7733 | IF_STATE_ACTION = 0, | |
7734 | IF_STATE_SOURCE, | |
7735 | IF_STATE_END, | |
7736 | }; | |
7737 | ||
7738 | static const match_table_t if_tokens = { | |
7739 | { IF_ACT_FILTER, "filter" }, | |
7740 | { IF_ACT_START, "start" }, | |
7741 | { IF_ACT_STOP, "stop" }, | |
7742 | { IF_SRC_FILE, "%u/%u@%s" }, | |
7743 | { IF_SRC_KERNEL, "%u/%u" }, | |
7744 | { IF_SRC_FILEADDR, "%u@%s" }, | |
7745 | { IF_SRC_KERNELADDR, "%u" }, | |
7746 | }; | |
7747 | ||
7748 | /* | |
7749 | * Address filter string parser | |
7750 | */ | |
7751 | static int | |
7752 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
7753 | struct list_head *filters) | |
7754 | { | |
7755 | struct perf_addr_filter *filter = NULL; | |
7756 | char *start, *orig, *filename = NULL; | |
7757 | struct path path; | |
7758 | substring_t args[MAX_OPT_ARGS]; | |
7759 | int state = IF_STATE_ACTION, token; | |
7760 | unsigned int kernel = 0; | |
7761 | int ret = -EINVAL; | |
7762 | ||
7763 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
7764 | if (!fstr) | |
7765 | return -ENOMEM; | |
7766 | ||
7767 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
7768 | ret = -EINVAL; | |
7769 | ||
7770 | if (!*start) | |
7771 | continue; | |
7772 | ||
7773 | /* filter definition begins */ | |
7774 | if (state == IF_STATE_ACTION) { | |
7775 | filter = perf_addr_filter_new(event, filters); | |
7776 | if (!filter) | |
7777 | goto fail; | |
7778 | } | |
7779 | ||
7780 | token = match_token(start, if_tokens, args); | |
7781 | switch (token) { | |
7782 | case IF_ACT_FILTER: | |
7783 | case IF_ACT_START: | |
7784 | filter->filter = 1; | |
7785 | ||
7786 | case IF_ACT_STOP: | |
7787 | if (state != IF_STATE_ACTION) | |
7788 | goto fail; | |
7789 | ||
7790 | state = IF_STATE_SOURCE; | |
7791 | break; | |
7792 | ||
7793 | case IF_SRC_KERNELADDR: | |
7794 | case IF_SRC_KERNEL: | |
7795 | kernel = 1; | |
7796 | ||
7797 | case IF_SRC_FILEADDR: | |
7798 | case IF_SRC_FILE: | |
7799 | if (state != IF_STATE_SOURCE) | |
7800 | goto fail; | |
7801 | ||
7802 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) | |
7803 | filter->range = 1; | |
7804 | ||
7805 | *args[0].to = 0; | |
7806 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
7807 | if (ret) | |
7808 | goto fail; | |
7809 | ||
7810 | if (filter->range) { | |
7811 | *args[1].to = 0; | |
7812 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
7813 | if (ret) | |
7814 | goto fail; | |
7815 | } | |
7816 | ||
7817 | if (token == IF_SRC_FILE) { | |
7818 | filename = match_strdup(&args[2]); | |
7819 | if (!filename) { | |
7820 | ret = -ENOMEM; | |
7821 | goto fail; | |
7822 | } | |
7823 | } | |
7824 | ||
7825 | state = IF_STATE_END; | |
7826 | break; | |
7827 | ||
7828 | default: | |
7829 | goto fail; | |
7830 | } | |
7831 | ||
7832 | /* | |
7833 | * Filter definition is fully parsed, validate and install it. | |
7834 | * Make sure that it doesn't contradict itself or the event's | |
7835 | * attribute. | |
7836 | */ | |
7837 | if (state == IF_STATE_END) { | |
7838 | if (kernel && event->attr.exclude_kernel) | |
7839 | goto fail; | |
7840 | ||
7841 | if (!kernel) { | |
7842 | if (!filename) | |
7843 | goto fail; | |
7844 | ||
7845 | /* look up the path and grab its inode */ | |
7846 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
7847 | if (ret) | |
7848 | goto fail_free_name; | |
7849 | ||
7850 | filter->inode = igrab(d_inode(path.dentry)); | |
7851 | path_put(&path); | |
7852 | kfree(filename); | |
7853 | filename = NULL; | |
7854 | ||
7855 | ret = -EINVAL; | |
7856 | if (!filter->inode || | |
7857 | !S_ISREG(filter->inode->i_mode)) | |
7858 | /* free_filters_list() will iput() */ | |
7859 | goto fail; | |
7860 | } | |
7861 | ||
7862 | /* ready to consume more filters */ | |
7863 | state = IF_STATE_ACTION; | |
7864 | filter = NULL; | |
7865 | } | |
7866 | } | |
7867 | ||
7868 | if (state != IF_STATE_ACTION) | |
7869 | goto fail; | |
7870 | ||
7871 | kfree(orig); | |
7872 | ||
7873 | return 0; | |
7874 | ||
7875 | fail_free_name: | |
7876 | kfree(filename); | |
7877 | fail: | |
7878 | free_filters_list(filters); | |
7879 | kfree(orig); | |
7880 | ||
7881 | return ret; | |
7882 | } | |
7883 | ||
7884 | static int | |
7885 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
7886 | { | |
7887 | LIST_HEAD(filters); | |
7888 | int ret; | |
7889 | ||
7890 | /* | |
7891 | * Since this is called in perf_ioctl() path, we're already holding | |
7892 | * ctx::mutex. | |
7893 | */ | |
7894 | lockdep_assert_held(&event->ctx->mutex); | |
7895 | ||
7896 | if (WARN_ON_ONCE(event->parent)) | |
7897 | return -EINVAL; | |
7898 | ||
7899 | /* | |
7900 | * For now, we only support filtering in per-task events; doing so | |
7901 | * for CPU-wide events requires additional context switching trickery, | |
7902 | * since same object code will be mapped at different virtual | |
7903 | * addresses in different processes. | |
7904 | */ | |
7905 | if (!event->ctx->task) | |
7906 | return -EOPNOTSUPP; | |
7907 | ||
7908 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); | |
7909 | if (ret) | |
7910 | return ret; | |
7911 | ||
7912 | ret = event->pmu->addr_filters_validate(&filters); | |
7913 | if (ret) { | |
7914 | free_filters_list(&filters); | |
7915 | return ret; | |
7916 | } | |
7917 | ||
7918 | /* remove existing filters, if any */ | |
7919 | perf_addr_filters_splice(event, &filters); | |
7920 | ||
7921 | /* install new filters */ | |
7922 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
7923 | ||
7924 | return ret; | |
7925 | } | |
7926 | ||
c796bbbe AS |
7927 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
7928 | { | |
7929 | char *filter_str; | |
7930 | int ret = -EINVAL; | |
7931 | ||
375637bc AS |
7932 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
7933 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && | |
7934 | !has_addr_filter(event)) | |
c796bbbe AS |
7935 | return -EINVAL; |
7936 | ||
7937 | filter_str = strndup_user(arg, PAGE_SIZE); | |
7938 | if (IS_ERR(filter_str)) | |
7939 | return PTR_ERR(filter_str); | |
7940 | ||
7941 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && | |
7942 | event->attr.type == PERF_TYPE_TRACEPOINT) | |
7943 | ret = ftrace_profile_set_filter(event, event->attr.config, | |
7944 | filter_str); | |
375637bc AS |
7945 | else if (has_addr_filter(event)) |
7946 | ret = perf_event_set_addr_filter(event, filter_str); | |
c796bbbe AS |
7947 | |
7948 | kfree(filter_str); | |
7949 | return ret; | |
7950 | } | |
7951 | ||
b0a873eb PZ |
7952 | /* |
7953 | * hrtimer based swevent callback | |
7954 | */ | |
f29ac756 | 7955 | |
b0a873eb | 7956 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 7957 | { |
b0a873eb PZ |
7958 | enum hrtimer_restart ret = HRTIMER_RESTART; |
7959 | struct perf_sample_data data; | |
7960 | struct pt_regs *regs; | |
7961 | struct perf_event *event; | |
7962 | u64 period; | |
f29ac756 | 7963 | |
b0a873eb | 7964 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
7965 | |
7966 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
7967 | return HRTIMER_NORESTART; | |
7968 | ||
b0a873eb | 7969 | event->pmu->read(event); |
f344011c | 7970 | |
fd0d000b | 7971 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
7972 | regs = get_irq_regs(); |
7973 | ||
7974 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 7975 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 7976 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
7977 | ret = HRTIMER_NORESTART; |
7978 | } | |
24f1e32c | 7979 | |
b0a873eb PZ |
7980 | period = max_t(u64, 10000, event->hw.sample_period); |
7981 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 7982 | |
b0a873eb | 7983 | return ret; |
f29ac756 PZ |
7984 | } |
7985 | ||
b0a873eb | 7986 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 7987 | { |
b0a873eb | 7988 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
7989 | s64 period; |
7990 | ||
7991 | if (!is_sampling_event(event)) | |
7992 | return; | |
f5ffe02e | 7993 | |
5d508e82 FBH |
7994 | period = local64_read(&hwc->period_left); |
7995 | if (period) { | |
7996 | if (period < 0) | |
7997 | period = 10000; | |
fa407f35 | 7998 | |
5d508e82 FBH |
7999 | local64_set(&hwc->period_left, 0); |
8000 | } else { | |
8001 | period = max_t(u64, 10000, hwc->sample_period); | |
8002 | } | |
3497d206 TG |
8003 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
8004 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 8005 | } |
b0a873eb PZ |
8006 | |
8007 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 8008 | { |
b0a873eb PZ |
8009 | struct hw_perf_event *hwc = &event->hw; |
8010 | ||
6c7e550f | 8011 | if (is_sampling_event(event)) { |
b0a873eb | 8012 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 8013 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
8014 | |
8015 | hrtimer_cancel(&hwc->hrtimer); | |
8016 | } | |
24f1e32c FW |
8017 | } |
8018 | ||
ba3dd36c PZ |
8019 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
8020 | { | |
8021 | struct hw_perf_event *hwc = &event->hw; | |
8022 | ||
8023 | if (!is_sampling_event(event)) | |
8024 | return; | |
8025 | ||
8026 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
8027 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
8028 | ||
8029 | /* | |
8030 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
8031 | * mapping and avoid the whole period adjust feedback stuff. | |
8032 | */ | |
8033 | if (event->attr.freq) { | |
8034 | long freq = event->attr.sample_freq; | |
8035 | ||
8036 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
8037 | hwc->sample_period = event->attr.sample_period; | |
8038 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 8039 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
8040 | event->attr.freq = 0; |
8041 | } | |
8042 | } | |
8043 | ||
b0a873eb PZ |
8044 | /* |
8045 | * Software event: cpu wall time clock | |
8046 | */ | |
8047 | ||
8048 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 8049 | { |
b0a873eb PZ |
8050 | s64 prev; |
8051 | u64 now; | |
8052 | ||
a4eaf7f1 | 8053 | now = local_clock(); |
b0a873eb PZ |
8054 | prev = local64_xchg(&event->hw.prev_count, now); |
8055 | local64_add(now - prev, &event->count); | |
24f1e32c | 8056 | } |
24f1e32c | 8057 | |
a4eaf7f1 | 8058 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8059 | { |
a4eaf7f1 | 8060 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 8061 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8062 | } |
8063 | ||
a4eaf7f1 | 8064 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 8065 | { |
b0a873eb PZ |
8066 | perf_swevent_cancel_hrtimer(event); |
8067 | cpu_clock_event_update(event); | |
8068 | } | |
f29ac756 | 8069 | |
a4eaf7f1 PZ |
8070 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
8071 | { | |
8072 | if (flags & PERF_EF_START) | |
8073 | cpu_clock_event_start(event, flags); | |
6a694a60 | 8074 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
8075 | |
8076 | return 0; | |
8077 | } | |
8078 | ||
8079 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
8080 | { | |
8081 | cpu_clock_event_stop(event, flags); | |
8082 | } | |
8083 | ||
b0a873eb PZ |
8084 | static void cpu_clock_event_read(struct perf_event *event) |
8085 | { | |
8086 | cpu_clock_event_update(event); | |
8087 | } | |
f344011c | 8088 | |
b0a873eb PZ |
8089 | static int cpu_clock_event_init(struct perf_event *event) |
8090 | { | |
8091 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
8092 | return -ENOENT; | |
8093 | ||
8094 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
8095 | return -ENOENT; | |
8096 | ||
2481c5fa SE |
8097 | /* |
8098 | * no branch sampling for software events | |
8099 | */ | |
8100 | if (has_branch_stack(event)) | |
8101 | return -EOPNOTSUPP; | |
8102 | ||
ba3dd36c PZ |
8103 | perf_swevent_init_hrtimer(event); |
8104 | ||
b0a873eb | 8105 | return 0; |
f29ac756 PZ |
8106 | } |
8107 | ||
b0a873eb | 8108 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
8109 | .task_ctx_nr = perf_sw_context, |
8110 | ||
34f43927 PZ |
8111 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8112 | ||
b0a873eb | 8113 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
8114 | .add = cpu_clock_event_add, |
8115 | .del = cpu_clock_event_del, | |
8116 | .start = cpu_clock_event_start, | |
8117 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
8118 | .read = cpu_clock_event_read, |
8119 | }; | |
8120 | ||
8121 | /* | |
8122 | * Software event: task time clock | |
8123 | */ | |
8124 | ||
8125 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 8126 | { |
b0a873eb PZ |
8127 | u64 prev; |
8128 | s64 delta; | |
5c92d124 | 8129 | |
b0a873eb PZ |
8130 | prev = local64_xchg(&event->hw.prev_count, now); |
8131 | delta = now - prev; | |
8132 | local64_add(delta, &event->count); | |
8133 | } | |
5c92d124 | 8134 | |
a4eaf7f1 | 8135 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8136 | { |
a4eaf7f1 | 8137 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 8138 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8139 | } |
8140 | ||
a4eaf7f1 | 8141 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
8142 | { |
8143 | perf_swevent_cancel_hrtimer(event); | |
8144 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
8145 | } |
8146 | ||
8147 | static int task_clock_event_add(struct perf_event *event, int flags) | |
8148 | { | |
8149 | if (flags & PERF_EF_START) | |
8150 | task_clock_event_start(event, flags); | |
6a694a60 | 8151 | perf_event_update_userpage(event); |
b0a873eb | 8152 | |
a4eaf7f1 PZ |
8153 | return 0; |
8154 | } | |
8155 | ||
8156 | static void task_clock_event_del(struct perf_event *event, int flags) | |
8157 | { | |
8158 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
8159 | } |
8160 | ||
8161 | static void task_clock_event_read(struct perf_event *event) | |
8162 | { | |
768a06e2 PZ |
8163 | u64 now = perf_clock(); |
8164 | u64 delta = now - event->ctx->timestamp; | |
8165 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
8166 | |
8167 | task_clock_event_update(event, time); | |
8168 | } | |
8169 | ||
8170 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 8171 | { |
b0a873eb PZ |
8172 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
8173 | return -ENOENT; | |
8174 | ||
8175 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
8176 | return -ENOENT; | |
8177 | ||
2481c5fa SE |
8178 | /* |
8179 | * no branch sampling for software events | |
8180 | */ | |
8181 | if (has_branch_stack(event)) | |
8182 | return -EOPNOTSUPP; | |
8183 | ||
ba3dd36c PZ |
8184 | perf_swevent_init_hrtimer(event); |
8185 | ||
b0a873eb | 8186 | return 0; |
6fb2915d LZ |
8187 | } |
8188 | ||
b0a873eb | 8189 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
8190 | .task_ctx_nr = perf_sw_context, |
8191 | ||
34f43927 PZ |
8192 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8193 | ||
b0a873eb | 8194 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
8195 | .add = task_clock_event_add, |
8196 | .del = task_clock_event_del, | |
8197 | .start = task_clock_event_start, | |
8198 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
8199 | .read = task_clock_event_read, |
8200 | }; | |
6fb2915d | 8201 | |
ad5133b7 | 8202 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 8203 | { |
e077df4f | 8204 | } |
6fb2915d | 8205 | |
fbbe0701 SB |
8206 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
8207 | { | |
8208 | } | |
8209 | ||
ad5133b7 | 8210 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 8211 | { |
ad5133b7 | 8212 | return 0; |
6fb2915d LZ |
8213 | } |
8214 | ||
18ab2cd3 | 8215 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
8216 | |
8217 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 8218 | { |
fbbe0701 SB |
8219 | __this_cpu_write(nop_txn_flags, flags); |
8220 | ||
8221 | if (flags & ~PERF_PMU_TXN_ADD) | |
8222 | return; | |
8223 | ||
ad5133b7 | 8224 | perf_pmu_disable(pmu); |
6fb2915d LZ |
8225 | } |
8226 | ||
ad5133b7 PZ |
8227 | static int perf_pmu_commit_txn(struct pmu *pmu) |
8228 | { | |
fbbe0701 SB |
8229 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8230 | ||
8231 | __this_cpu_write(nop_txn_flags, 0); | |
8232 | ||
8233 | if (flags & ~PERF_PMU_TXN_ADD) | |
8234 | return 0; | |
8235 | ||
ad5133b7 PZ |
8236 | perf_pmu_enable(pmu); |
8237 | return 0; | |
8238 | } | |
e077df4f | 8239 | |
ad5133b7 | 8240 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 8241 | { |
fbbe0701 SB |
8242 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8243 | ||
8244 | __this_cpu_write(nop_txn_flags, 0); | |
8245 | ||
8246 | if (flags & ~PERF_PMU_TXN_ADD) | |
8247 | return; | |
8248 | ||
ad5133b7 | 8249 | perf_pmu_enable(pmu); |
24f1e32c FW |
8250 | } |
8251 | ||
35edc2a5 PZ |
8252 | static int perf_event_idx_default(struct perf_event *event) |
8253 | { | |
c719f560 | 8254 | return 0; |
35edc2a5 PZ |
8255 | } |
8256 | ||
8dc85d54 PZ |
8257 | /* |
8258 | * Ensures all contexts with the same task_ctx_nr have the same | |
8259 | * pmu_cpu_context too. | |
8260 | */ | |
9e317041 | 8261 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 8262 | { |
8dc85d54 | 8263 | struct pmu *pmu; |
b326e956 | 8264 | |
8dc85d54 PZ |
8265 | if (ctxn < 0) |
8266 | return NULL; | |
24f1e32c | 8267 | |
8dc85d54 PZ |
8268 | list_for_each_entry(pmu, &pmus, entry) { |
8269 | if (pmu->task_ctx_nr == ctxn) | |
8270 | return pmu->pmu_cpu_context; | |
8271 | } | |
24f1e32c | 8272 | |
8dc85d54 | 8273 | return NULL; |
24f1e32c FW |
8274 | } |
8275 | ||
51676957 | 8276 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
24f1e32c | 8277 | { |
51676957 PZ |
8278 | int cpu; |
8279 | ||
8280 | for_each_possible_cpu(cpu) { | |
8281 | struct perf_cpu_context *cpuctx; | |
8282 | ||
8283 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8284 | ||
3f1f3320 PZ |
8285 | if (cpuctx->unique_pmu == old_pmu) |
8286 | cpuctx->unique_pmu = pmu; | |
51676957 PZ |
8287 | } |
8288 | } | |
8289 | ||
8290 | static void free_pmu_context(struct pmu *pmu) | |
8291 | { | |
8292 | struct pmu *i; | |
f5ffe02e | 8293 | |
8dc85d54 | 8294 | mutex_lock(&pmus_lock); |
0475f9ea | 8295 | /* |
8dc85d54 | 8296 | * Like a real lame refcount. |
0475f9ea | 8297 | */ |
51676957 PZ |
8298 | list_for_each_entry(i, &pmus, entry) { |
8299 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { | |
8300 | update_pmu_context(i, pmu); | |
8dc85d54 | 8301 | goto out; |
51676957 | 8302 | } |
8dc85d54 | 8303 | } |
d6d020e9 | 8304 | |
51676957 | 8305 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 PZ |
8306 | out: |
8307 | mutex_unlock(&pmus_lock); | |
24f1e32c | 8308 | } |
6e855cd4 AS |
8309 | |
8310 | /* | |
8311 | * Let userspace know that this PMU supports address range filtering: | |
8312 | */ | |
8313 | static ssize_t nr_addr_filters_show(struct device *dev, | |
8314 | struct device_attribute *attr, | |
8315 | char *page) | |
8316 | { | |
8317 | struct pmu *pmu = dev_get_drvdata(dev); | |
8318 | ||
8319 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
8320 | } | |
8321 | DEVICE_ATTR_RO(nr_addr_filters); | |
8322 | ||
2e80a82a | 8323 | static struct idr pmu_idr; |
d6d020e9 | 8324 | |
abe43400 PZ |
8325 | static ssize_t |
8326 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
8327 | { | |
8328 | struct pmu *pmu = dev_get_drvdata(dev); | |
8329 | ||
8330 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
8331 | } | |
90826ca7 | 8332 | static DEVICE_ATTR_RO(type); |
abe43400 | 8333 | |
62b85639 SE |
8334 | static ssize_t |
8335 | perf_event_mux_interval_ms_show(struct device *dev, | |
8336 | struct device_attribute *attr, | |
8337 | char *page) | |
8338 | { | |
8339 | struct pmu *pmu = dev_get_drvdata(dev); | |
8340 | ||
8341 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
8342 | } | |
8343 | ||
272325c4 PZ |
8344 | static DEFINE_MUTEX(mux_interval_mutex); |
8345 | ||
62b85639 SE |
8346 | static ssize_t |
8347 | perf_event_mux_interval_ms_store(struct device *dev, | |
8348 | struct device_attribute *attr, | |
8349 | const char *buf, size_t count) | |
8350 | { | |
8351 | struct pmu *pmu = dev_get_drvdata(dev); | |
8352 | int timer, cpu, ret; | |
8353 | ||
8354 | ret = kstrtoint(buf, 0, &timer); | |
8355 | if (ret) | |
8356 | return ret; | |
8357 | ||
8358 | if (timer < 1) | |
8359 | return -EINVAL; | |
8360 | ||
8361 | /* same value, noting to do */ | |
8362 | if (timer == pmu->hrtimer_interval_ms) | |
8363 | return count; | |
8364 | ||
272325c4 | 8365 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
8366 | pmu->hrtimer_interval_ms = timer; |
8367 | ||
8368 | /* update all cpuctx for this PMU */ | |
272325c4 PZ |
8369 | get_online_cpus(); |
8370 | for_each_online_cpu(cpu) { | |
62b85639 SE |
8371 | struct perf_cpu_context *cpuctx; |
8372 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8373 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
8374 | ||
272325c4 PZ |
8375 | cpu_function_call(cpu, |
8376 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 8377 | } |
272325c4 PZ |
8378 | put_online_cpus(); |
8379 | mutex_unlock(&mux_interval_mutex); | |
62b85639 SE |
8380 | |
8381 | return count; | |
8382 | } | |
90826ca7 | 8383 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 8384 | |
90826ca7 GKH |
8385 | static struct attribute *pmu_dev_attrs[] = { |
8386 | &dev_attr_type.attr, | |
8387 | &dev_attr_perf_event_mux_interval_ms.attr, | |
8388 | NULL, | |
abe43400 | 8389 | }; |
90826ca7 | 8390 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
8391 | |
8392 | static int pmu_bus_running; | |
8393 | static struct bus_type pmu_bus = { | |
8394 | .name = "event_source", | |
90826ca7 | 8395 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
8396 | }; |
8397 | ||
8398 | static void pmu_dev_release(struct device *dev) | |
8399 | { | |
8400 | kfree(dev); | |
8401 | } | |
8402 | ||
8403 | static int pmu_dev_alloc(struct pmu *pmu) | |
8404 | { | |
8405 | int ret = -ENOMEM; | |
8406 | ||
8407 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
8408 | if (!pmu->dev) | |
8409 | goto out; | |
8410 | ||
0c9d42ed | 8411 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
8412 | device_initialize(pmu->dev); |
8413 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
8414 | if (ret) | |
8415 | goto free_dev; | |
8416 | ||
8417 | dev_set_drvdata(pmu->dev, pmu); | |
8418 | pmu->dev->bus = &pmu_bus; | |
8419 | pmu->dev->release = pmu_dev_release; | |
8420 | ret = device_add(pmu->dev); | |
8421 | if (ret) | |
8422 | goto free_dev; | |
8423 | ||
6e855cd4 AS |
8424 | /* For PMUs with address filters, throw in an extra attribute: */ |
8425 | if (pmu->nr_addr_filters) | |
8426 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
8427 | ||
8428 | if (ret) | |
8429 | goto del_dev; | |
8430 | ||
abe43400 PZ |
8431 | out: |
8432 | return ret; | |
8433 | ||
6e855cd4 AS |
8434 | del_dev: |
8435 | device_del(pmu->dev); | |
8436 | ||
abe43400 PZ |
8437 | free_dev: |
8438 | put_device(pmu->dev); | |
8439 | goto out; | |
8440 | } | |
8441 | ||
547e9fd7 | 8442 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 8443 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 8444 | |
03d8e80b | 8445 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 8446 | { |
108b02cf | 8447 | int cpu, ret; |
24f1e32c | 8448 | |
b0a873eb | 8449 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
8450 | ret = -ENOMEM; |
8451 | pmu->pmu_disable_count = alloc_percpu(int); | |
8452 | if (!pmu->pmu_disable_count) | |
8453 | goto unlock; | |
f29ac756 | 8454 | |
2e80a82a PZ |
8455 | pmu->type = -1; |
8456 | if (!name) | |
8457 | goto skip_type; | |
8458 | pmu->name = name; | |
8459 | ||
8460 | if (type < 0) { | |
0e9c3be2 TH |
8461 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
8462 | if (type < 0) { | |
8463 | ret = type; | |
2e80a82a PZ |
8464 | goto free_pdc; |
8465 | } | |
8466 | } | |
8467 | pmu->type = type; | |
8468 | ||
abe43400 PZ |
8469 | if (pmu_bus_running) { |
8470 | ret = pmu_dev_alloc(pmu); | |
8471 | if (ret) | |
8472 | goto free_idr; | |
8473 | } | |
8474 | ||
2e80a82a | 8475 | skip_type: |
26657848 PZ |
8476 | if (pmu->task_ctx_nr == perf_hw_context) { |
8477 | static int hw_context_taken = 0; | |
8478 | ||
5101ef20 MR |
8479 | /* |
8480 | * Other than systems with heterogeneous CPUs, it never makes | |
8481 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
8482 | * uncore must use perf_invalid_context. | |
8483 | */ | |
8484 | if (WARN_ON_ONCE(hw_context_taken && | |
8485 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
8486 | pmu->task_ctx_nr = perf_invalid_context; |
8487 | ||
8488 | hw_context_taken = 1; | |
8489 | } | |
8490 | ||
8dc85d54 PZ |
8491 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
8492 | if (pmu->pmu_cpu_context) | |
8493 | goto got_cpu_context; | |
f29ac756 | 8494 | |
c4814202 | 8495 | ret = -ENOMEM; |
108b02cf PZ |
8496 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
8497 | if (!pmu->pmu_cpu_context) | |
abe43400 | 8498 | goto free_dev; |
f344011c | 8499 | |
108b02cf PZ |
8500 | for_each_possible_cpu(cpu) { |
8501 | struct perf_cpu_context *cpuctx; | |
8502 | ||
8503 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 8504 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 8505 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 8506 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 8507 | cpuctx->ctx.pmu = pmu; |
9e630205 | 8508 | |
272325c4 | 8509 | __perf_mux_hrtimer_init(cpuctx, cpu); |
9e630205 | 8510 | |
3f1f3320 | 8511 | cpuctx->unique_pmu = pmu; |
108b02cf | 8512 | } |
76e1d904 | 8513 | |
8dc85d54 | 8514 | got_cpu_context: |
ad5133b7 PZ |
8515 | if (!pmu->start_txn) { |
8516 | if (pmu->pmu_enable) { | |
8517 | /* | |
8518 | * If we have pmu_enable/pmu_disable calls, install | |
8519 | * transaction stubs that use that to try and batch | |
8520 | * hardware accesses. | |
8521 | */ | |
8522 | pmu->start_txn = perf_pmu_start_txn; | |
8523 | pmu->commit_txn = perf_pmu_commit_txn; | |
8524 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
8525 | } else { | |
fbbe0701 | 8526 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
8527 | pmu->commit_txn = perf_pmu_nop_int; |
8528 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 8529 | } |
5c92d124 | 8530 | } |
15dbf27c | 8531 | |
ad5133b7 PZ |
8532 | if (!pmu->pmu_enable) { |
8533 | pmu->pmu_enable = perf_pmu_nop_void; | |
8534 | pmu->pmu_disable = perf_pmu_nop_void; | |
8535 | } | |
8536 | ||
35edc2a5 PZ |
8537 | if (!pmu->event_idx) |
8538 | pmu->event_idx = perf_event_idx_default; | |
8539 | ||
b0a873eb | 8540 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 8541 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
8542 | ret = 0; |
8543 | unlock: | |
b0a873eb PZ |
8544 | mutex_unlock(&pmus_lock); |
8545 | ||
33696fc0 | 8546 | return ret; |
108b02cf | 8547 | |
abe43400 PZ |
8548 | free_dev: |
8549 | device_del(pmu->dev); | |
8550 | put_device(pmu->dev); | |
8551 | ||
2e80a82a PZ |
8552 | free_idr: |
8553 | if (pmu->type >= PERF_TYPE_MAX) | |
8554 | idr_remove(&pmu_idr, pmu->type); | |
8555 | ||
108b02cf PZ |
8556 | free_pdc: |
8557 | free_percpu(pmu->pmu_disable_count); | |
8558 | goto unlock; | |
f29ac756 | 8559 | } |
c464c76e | 8560 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 8561 | |
b0a873eb | 8562 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 8563 | { |
b0a873eb PZ |
8564 | mutex_lock(&pmus_lock); |
8565 | list_del_rcu(&pmu->entry); | |
8566 | mutex_unlock(&pmus_lock); | |
5c92d124 | 8567 | |
0475f9ea | 8568 | /* |
cde8e884 PZ |
8569 | * We dereference the pmu list under both SRCU and regular RCU, so |
8570 | * synchronize against both of those. | |
0475f9ea | 8571 | */ |
b0a873eb | 8572 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 8573 | synchronize_rcu(); |
d6d020e9 | 8574 | |
33696fc0 | 8575 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
8576 | if (pmu->type >= PERF_TYPE_MAX) |
8577 | idr_remove(&pmu_idr, pmu->type); | |
6e855cd4 AS |
8578 | if (pmu->nr_addr_filters) |
8579 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
abe43400 PZ |
8580 | device_del(pmu->dev); |
8581 | put_device(pmu->dev); | |
51676957 | 8582 | free_pmu_context(pmu); |
b0a873eb | 8583 | } |
c464c76e | 8584 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 8585 | |
cc34b98b MR |
8586 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
8587 | { | |
ccd41c86 | 8588 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
8589 | int ret; |
8590 | ||
8591 | if (!try_module_get(pmu->module)) | |
8592 | return -ENODEV; | |
ccd41c86 PZ |
8593 | |
8594 | if (event->group_leader != event) { | |
8b10c5e2 PZ |
8595 | /* |
8596 | * This ctx->mutex can nest when we're called through | |
8597 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
8598 | */ | |
8599 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
8600 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
8601 | BUG_ON(!ctx); |
8602 | } | |
8603 | ||
cc34b98b MR |
8604 | event->pmu = pmu; |
8605 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
8606 | |
8607 | if (ctx) | |
8608 | perf_event_ctx_unlock(event->group_leader, ctx); | |
8609 | ||
cc34b98b MR |
8610 | if (ret) |
8611 | module_put(pmu->module); | |
8612 | ||
8613 | return ret; | |
8614 | } | |
8615 | ||
18ab2cd3 | 8616 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb PZ |
8617 | { |
8618 | struct pmu *pmu = NULL; | |
8619 | int idx; | |
940c5b29 | 8620 | int ret; |
b0a873eb PZ |
8621 | |
8622 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a PZ |
8623 | |
8624 | rcu_read_lock(); | |
8625 | pmu = idr_find(&pmu_idr, event->attr.type); | |
8626 | rcu_read_unlock(); | |
940c5b29 | 8627 | if (pmu) { |
cc34b98b | 8628 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
8629 | if (ret) |
8630 | pmu = ERR_PTR(ret); | |
2e80a82a | 8631 | goto unlock; |
940c5b29 | 8632 | } |
2e80a82a | 8633 | |
b0a873eb | 8634 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 8635 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 8636 | if (!ret) |
e5f4d339 | 8637 | goto unlock; |
76e1d904 | 8638 | |
b0a873eb PZ |
8639 | if (ret != -ENOENT) { |
8640 | pmu = ERR_PTR(ret); | |
e5f4d339 | 8641 | goto unlock; |
f344011c | 8642 | } |
5c92d124 | 8643 | } |
e5f4d339 PZ |
8644 | pmu = ERR_PTR(-ENOENT); |
8645 | unlock: | |
b0a873eb | 8646 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 8647 | |
4aeb0b42 | 8648 | return pmu; |
5c92d124 IM |
8649 | } |
8650 | ||
f2fb6bef KL |
8651 | static void attach_sb_event(struct perf_event *event) |
8652 | { | |
8653 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
8654 | ||
8655 | raw_spin_lock(&pel->lock); | |
8656 | list_add_rcu(&event->sb_list, &pel->list); | |
8657 | raw_spin_unlock(&pel->lock); | |
8658 | } | |
8659 | ||
aab5b71e PZ |
8660 | /* |
8661 | * We keep a list of all !task (and therefore per-cpu) events | |
8662 | * that need to receive side-band records. | |
8663 | * | |
8664 | * This avoids having to scan all the various PMU per-cpu contexts | |
8665 | * looking for them. | |
8666 | */ | |
f2fb6bef KL |
8667 | static void account_pmu_sb_event(struct perf_event *event) |
8668 | { | |
8669 | struct perf_event_attr *attr = &event->attr; | |
8670 | ||
8671 | if (event->parent) | |
8672 | return; | |
8673 | ||
8674 | if (event->attach_state & PERF_ATTACH_TASK) | |
8675 | return; | |
8676 | ||
8677 | if (attr->mmap || attr->mmap_data || attr->mmap2 || | |
8678 | attr->comm || attr->comm_exec || | |
8679 | attr->task || | |
8680 | attr->context_switch) | |
8681 | attach_sb_event(event); | |
8682 | } | |
8683 | ||
4beb31f3 FW |
8684 | static void account_event_cpu(struct perf_event *event, int cpu) |
8685 | { | |
8686 | if (event->parent) | |
8687 | return; | |
8688 | ||
4beb31f3 FW |
8689 | if (is_cgroup_event(event)) |
8690 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
8691 | } | |
8692 | ||
555e0c1e FW |
8693 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
8694 | static void account_freq_event_nohz(void) | |
8695 | { | |
8696 | #ifdef CONFIG_NO_HZ_FULL | |
8697 | /* Lock so we don't race with concurrent unaccount */ | |
8698 | spin_lock(&nr_freq_lock); | |
8699 | if (atomic_inc_return(&nr_freq_events) == 1) | |
8700 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
8701 | spin_unlock(&nr_freq_lock); | |
8702 | #endif | |
8703 | } | |
8704 | ||
8705 | static void account_freq_event(void) | |
8706 | { | |
8707 | if (tick_nohz_full_enabled()) | |
8708 | account_freq_event_nohz(); | |
8709 | else | |
8710 | atomic_inc(&nr_freq_events); | |
8711 | } | |
8712 | ||
8713 | ||
766d6c07 FW |
8714 | static void account_event(struct perf_event *event) |
8715 | { | |
25432ae9 PZ |
8716 | bool inc = false; |
8717 | ||
4beb31f3 FW |
8718 | if (event->parent) |
8719 | return; | |
8720 | ||
766d6c07 | 8721 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 8722 | inc = true; |
766d6c07 FW |
8723 | if (event->attr.mmap || event->attr.mmap_data) |
8724 | atomic_inc(&nr_mmap_events); | |
8725 | if (event->attr.comm) | |
8726 | atomic_inc(&nr_comm_events); | |
8727 | if (event->attr.task) | |
8728 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
8729 | if (event->attr.freq) |
8730 | account_freq_event(); | |
45ac1403 AH |
8731 | if (event->attr.context_switch) { |
8732 | atomic_inc(&nr_switch_events); | |
25432ae9 | 8733 | inc = true; |
45ac1403 | 8734 | } |
4beb31f3 | 8735 | if (has_branch_stack(event)) |
25432ae9 | 8736 | inc = true; |
4beb31f3 | 8737 | if (is_cgroup_event(event)) |
25432ae9 PZ |
8738 | inc = true; |
8739 | ||
9107c89e PZ |
8740 | if (inc) { |
8741 | if (atomic_inc_not_zero(&perf_sched_count)) | |
8742 | goto enabled; | |
8743 | ||
8744 | mutex_lock(&perf_sched_mutex); | |
8745 | if (!atomic_read(&perf_sched_count)) { | |
8746 | static_branch_enable(&perf_sched_events); | |
8747 | /* | |
8748 | * Guarantee that all CPUs observe they key change and | |
8749 | * call the perf scheduling hooks before proceeding to | |
8750 | * install events that need them. | |
8751 | */ | |
8752 | synchronize_sched(); | |
8753 | } | |
8754 | /* | |
8755 | * Now that we have waited for the sync_sched(), allow further | |
8756 | * increments to by-pass the mutex. | |
8757 | */ | |
8758 | atomic_inc(&perf_sched_count); | |
8759 | mutex_unlock(&perf_sched_mutex); | |
8760 | } | |
8761 | enabled: | |
4beb31f3 FW |
8762 | |
8763 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
8764 | |
8765 | account_pmu_sb_event(event); | |
766d6c07 FW |
8766 | } |
8767 | ||
0793a61d | 8768 | /* |
cdd6c482 | 8769 | * Allocate and initialize a event structure |
0793a61d | 8770 | */ |
cdd6c482 | 8771 | static struct perf_event * |
c3f00c70 | 8772 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
8773 | struct task_struct *task, |
8774 | struct perf_event *group_leader, | |
8775 | struct perf_event *parent_event, | |
4dc0da86 | 8776 | perf_overflow_handler_t overflow_handler, |
79dff51e | 8777 | void *context, int cgroup_fd) |
0793a61d | 8778 | { |
51b0fe39 | 8779 | struct pmu *pmu; |
cdd6c482 IM |
8780 | struct perf_event *event; |
8781 | struct hw_perf_event *hwc; | |
90983b16 | 8782 | long err = -EINVAL; |
0793a61d | 8783 | |
66832eb4 ON |
8784 | if ((unsigned)cpu >= nr_cpu_ids) { |
8785 | if (!task || cpu != -1) | |
8786 | return ERR_PTR(-EINVAL); | |
8787 | } | |
8788 | ||
c3f00c70 | 8789 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 8790 | if (!event) |
d5d2bc0d | 8791 | return ERR_PTR(-ENOMEM); |
0793a61d | 8792 | |
04289bb9 | 8793 | /* |
cdd6c482 | 8794 | * Single events are their own group leaders, with an |
04289bb9 IM |
8795 | * empty sibling list: |
8796 | */ | |
8797 | if (!group_leader) | |
cdd6c482 | 8798 | group_leader = event; |
04289bb9 | 8799 | |
cdd6c482 IM |
8800 | mutex_init(&event->child_mutex); |
8801 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 8802 | |
cdd6c482 IM |
8803 | INIT_LIST_HEAD(&event->group_entry); |
8804 | INIT_LIST_HEAD(&event->event_entry); | |
8805 | INIT_LIST_HEAD(&event->sibling_list); | |
10c6db11 | 8806 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 8807 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 8808 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
8809 | INIT_HLIST_NODE(&event->hlist_entry); |
8810 | ||
10c6db11 | 8811 | |
cdd6c482 | 8812 | init_waitqueue_head(&event->waitq); |
e360adbe | 8813 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 8814 | |
cdd6c482 | 8815 | mutex_init(&event->mmap_mutex); |
375637bc | 8816 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 8817 | |
a6fa941d | 8818 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
8819 | event->cpu = cpu; |
8820 | event->attr = *attr; | |
8821 | event->group_leader = group_leader; | |
8822 | event->pmu = NULL; | |
cdd6c482 | 8823 | event->oncpu = -1; |
a96bbc16 | 8824 | |
cdd6c482 | 8825 | event->parent = parent_event; |
b84fbc9f | 8826 | |
17cf22c3 | 8827 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 8828 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 8829 | |
cdd6c482 | 8830 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 8831 | |
d580ff86 PZ |
8832 | if (task) { |
8833 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 8834 | /* |
50f16a8b PZ |
8835 | * XXX pmu::event_init needs to know what task to account to |
8836 | * and we cannot use the ctx information because we need the | |
8837 | * pmu before we get a ctx. | |
d580ff86 | 8838 | */ |
50f16a8b | 8839 | event->hw.target = task; |
d580ff86 PZ |
8840 | } |
8841 | ||
34f43927 PZ |
8842 | event->clock = &local_clock; |
8843 | if (parent_event) | |
8844 | event->clock = parent_event->clock; | |
8845 | ||
4dc0da86 | 8846 | if (!overflow_handler && parent_event) { |
b326e956 | 8847 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 AK |
8848 | context = parent_event->overflow_handler_context; |
8849 | } | |
66832eb4 | 8850 | |
1879445d WN |
8851 | if (overflow_handler) { |
8852 | event->overflow_handler = overflow_handler; | |
8853 | event->overflow_handler_context = context; | |
9ecda41a WN |
8854 | } else if (is_write_backward(event)){ |
8855 | event->overflow_handler = perf_event_output_backward; | |
8856 | event->overflow_handler_context = NULL; | |
1879445d | 8857 | } else { |
9ecda41a | 8858 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
8859 | event->overflow_handler_context = NULL; |
8860 | } | |
97eaf530 | 8861 | |
0231bb53 | 8862 | perf_event__state_init(event); |
a86ed508 | 8863 | |
4aeb0b42 | 8864 | pmu = NULL; |
b8e83514 | 8865 | |
cdd6c482 | 8866 | hwc = &event->hw; |
bd2b5b12 | 8867 | hwc->sample_period = attr->sample_period; |
0d48696f | 8868 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 8869 | hwc->sample_period = 1; |
eced1dfc | 8870 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 8871 | |
e7850595 | 8872 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 8873 | |
2023b359 | 8874 | /* |
cdd6c482 | 8875 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
2023b359 | 8876 | */ |
3dab77fb | 8877 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
90983b16 | 8878 | goto err_ns; |
a46a2300 YZ |
8879 | |
8880 | if (!has_branch_stack(event)) | |
8881 | event->attr.branch_sample_type = 0; | |
2023b359 | 8882 | |
79dff51e MF |
8883 | if (cgroup_fd != -1) { |
8884 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
8885 | if (err) | |
8886 | goto err_ns; | |
8887 | } | |
8888 | ||
b0a873eb | 8889 | pmu = perf_init_event(event); |
4aeb0b42 | 8890 | if (!pmu) |
90983b16 FW |
8891 | goto err_ns; |
8892 | else if (IS_ERR(pmu)) { | |
4aeb0b42 | 8893 | err = PTR_ERR(pmu); |
90983b16 | 8894 | goto err_ns; |
621a01ea | 8895 | } |
d5d2bc0d | 8896 | |
bed5b25a AS |
8897 | err = exclusive_event_init(event); |
8898 | if (err) | |
8899 | goto err_pmu; | |
8900 | ||
375637bc AS |
8901 | if (has_addr_filter(event)) { |
8902 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, | |
8903 | sizeof(unsigned long), | |
8904 | GFP_KERNEL); | |
8905 | if (!event->addr_filters_offs) | |
8906 | goto err_per_task; | |
8907 | ||
8908 | /* force hw sync on the address filters */ | |
8909 | event->addr_filters_gen = 1; | |
8910 | } | |
8911 | ||
cdd6c482 | 8912 | if (!event->parent) { |
927c7a9e | 8913 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 8914 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 8915 | if (err) |
375637bc | 8916 | goto err_addr_filters; |
d010b332 | 8917 | } |
f344011c | 8918 | } |
9ee318a7 | 8919 | |
927a5570 AS |
8920 | /* symmetric to unaccount_event() in _free_event() */ |
8921 | account_event(event); | |
8922 | ||
cdd6c482 | 8923 | return event; |
90983b16 | 8924 | |
375637bc AS |
8925 | err_addr_filters: |
8926 | kfree(event->addr_filters_offs); | |
8927 | ||
bed5b25a AS |
8928 | err_per_task: |
8929 | exclusive_event_destroy(event); | |
8930 | ||
90983b16 FW |
8931 | err_pmu: |
8932 | if (event->destroy) | |
8933 | event->destroy(event); | |
c464c76e | 8934 | module_put(pmu->module); |
90983b16 | 8935 | err_ns: |
79dff51e MF |
8936 | if (is_cgroup_event(event)) |
8937 | perf_detach_cgroup(event); | |
90983b16 FW |
8938 | if (event->ns) |
8939 | put_pid_ns(event->ns); | |
8940 | kfree(event); | |
8941 | ||
8942 | return ERR_PTR(err); | |
0793a61d TG |
8943 | } |
8944 | ||
cdd6c482 IM |
8945 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
8946 | struct perf_event_attr *attr) | |
974802ea | 8947 | { |
974802ea | 8948 | u32 size; |
cdf8073d | 8949 | int ret; |
974802ea PZ |
8950 | |
8951 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
8952 | return -EFAULT; | |
8953 | ||
8954 | /* | |
8955 | * zero the full structure, so that a short copy will be nice. | |
8956 | */ | |
8957 | memset(attr, 0, sizeof(*attr)); | |
8958 | ||
8959 | ret = get_user(size, &uattr->size); | |
8960 | if (ret) | |
8961 | return ret; | |
8962 | ||
8963 | if (size > PAGE_SIZE) /* silly large */ | |
8964 | goto err_size; | |
8965 | ||
8966 | if (!size) /* abi compat */ | |
8967 | size = PERF_ATTR_SIZE_VER0; | |
8968 | ||
8969 | if (size < PERF_ATTR_SIZE_VER0) | |
8970 | goto err_size; | |
8971 | ||
8972 | /* | |
8973 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
8974 | * ensure all the unknown bits are 0 - i.e. new |
8975 | * user-space does not rely on any kernel feature | |
8976 | * extensions we dont know about yet. | |
974802ea PZ |
8977 | */ |
8978 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
8979 | unsigned char __user *addr; |
8980 | unsigned char __user *end; | |
8981 | unsigned char val; | |
974802ea | 8982 | |
cdf8073d IS |
8983 | addr = (void __user *)uattr + sizeof(*attr); |
8984 | end = (void __user *)uattr + size; | |
974802ea | 8985 | |
cdf8073d | 8986 | for (; addr < end; addr++) { |
974802ea PZ |
8987 | ret = get_user(val, addr); |
8988 | if (ret) | |
8989 | return ret; | |
8990 | if (val) | |
8991 | goto err_size; | |
8992 | } | |
b3e62e35 | 8993 | size = sizeof(*attr); |
974802ea PZ |
8994 | } |
8995 | ||
8996 | ret = copy_from_user(attr, uattr, size); | |
8997 | if (ret) | |
8998 | return -EFAULT; | |
8999 | ||
cd757645 | 9000 | if (attr->__reserved_1) |
974802ea PZ |
9001 | return -EINVAL; |
9002 | ||
9003 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
9004 | return -EINVAL; | |
9005 | ||
9006 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
9007 | return -EINVAL; | |
9008 | ||
bce38cd5 SE |
9009 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
9010 | u64 mask = attr->branch_sample_type; | |
9011 | ||
9012 | /* only using defined bits */ | |
9013 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
9014 | return -EINVAL; | |
9015 | ||
9016 | /* at least one branch bit must be set */ | |
9017 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
9018 | return -EINVAL; | |
9019 | ||
bce38cd5 SE |
9020 | /* propagate priv level, when not set for branch */ |
9021 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
9022 | ||
9023 | /* exclude_kernel checked on syscall entry */ | |
9024 | if (!attr->exclude_kernel) | |
9025 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
9026 | ||
9027 | if (!attr->exclude_user) | |
9028 | mask |= PERF_SAMPLE_BRANCH_USER; | |
9029 | ||
9030 | if (!attr->exclude_hv) | |
9031 | mask |= PERF_SAMPLE_BRANCH_HV; | |
9032 | /* | |
9033 | * adjust user setting (for HW filter setup) | |
9034 | */ | |
9035 | attr->branch_sample_type = mask; | |
9036 | } | |
e712209a SE |
9037 | /* privileged levels capture (kernel, hv): check permissions */ |
9038 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
9039 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
9040 | return -EACCES; | |
bce38cd5 | 9041 | } |
4018994f | 9042 | |
c5ebcedb | 9043 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 9044 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
9045 | if (ret) |
9046 | return ret; | |
9047 | } | |
9048 | ||
9049 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
9050 | if (!arch_perf_have_user_stack_dump()) | |
9051 | return -ENOSYS; | |
9052 | ||
9053 | /* | |
9054 | * We have __u32 type for the size, but so far | |
9055 | * we can only use __u16 as maximum due to the | |
9056 | * __u16 sample size limit. | |
9057 | */ | |
9058 | if (attr->sample_stack_user >= USHRT_MAX) | |
9059 | ret = -EINVAL; | |
9060 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) | |
9061 | ret = -EINVAL; | |
9062 | } | |
4018994f | 9063 | |
60e2364e SE |
9064 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
9065 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
9066 | out: |
9067 | return ret; | |
9068 | ||
9069 | err_size: | |
9070 | put_user(sizeof(*attr), &uattr->size); | |
9071 | ret = -E2BIG; | |
9072 | goto out; | |
9073 | } | |
9074 | ||
ac9721f3 PZ |
9075 | static int |
9076 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 9077 | { |
b69cf536 | 9078 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
9079 | int ret = -EINVAL; |
9080 | ||
ac9721f3 | 9081 | if (!output_event) |
a4be7c27 PZ |
9082 | goto set; |
9083 | ||
ac9721f3 PZ |
9084 | /* don't allow circular references */ |
9085 | if (event == output_event) | |
a4be7c27 PZ |
9086 | goto out; |
9087 | ||
0f139300 PZ |
9088 | /* |
9089 | * Don't allow cross-cpu buffers | |
9090 | */ | |
9091 | if (output_event->cpu != event->cpu) | |
9092 | goto out; | |
9093 | ||
9094 | /* | |
76369139 | 9095 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
9096 | */ |
9097 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
9098 | goto out; | |
9099 | ||
34f43927 PZ |
9100 | /* |
9101 | * Mixing clocks in the same buffer is trouble you don't need. | |
9102 | */ | |
9103 | if (output_event->clock != event->clock) | |
9104 | goto out; | |
9105 | ||
9ecda41a WN |
9106 | /* |
9107 | * Either writing ring buffer from beginning or from end. | |
9108 | * Mixing is not allowed. | |
9109 | */ | |
9110 | if (is_write_backward(output_event) != is_write_backward(event)) | |
9111 | goto out; | |
9112 | ||
45bfb2e5 PZ |
9113 | /* |
9114 | * If both events generate aux data, they must be on the same PMU | |
9115 | */ | |
9116 | if (has_aux(event) && has_aux(output_event) && | |
9117 | event->pmu != output_event->pmu) | |
9118 | goto out; | |
9119 | ||
a4be7c27 | 9120 | set: |
cdd6c482 | 9121 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
9122 | /* Can't redirect output if we've got an active mmap() */ |
9123 | if (atomic_read(&event->mmap_count)) | |
9124 | goto unlock; | |
a4be7c27 | 9125 | |
ac9721f3 | 9126 | if (output_event) { |
76369139 FW |
9127 | /* get the rb we want to redirect to */ |
9128 | rb = ring_buffer_get(output_event); | |
9129 | if (!rb) | |
ac9721f3 | 9130 | goto unlock; |
a4be7c27 PZ |
9131 | } |
9132 | ||
b69cf536 | 9133 | ring_buffer_attach(event, rb); |
9bb5d40c | 9134 | |
a4be7c27 | 9135 | ret = 0; |
ac9721f3 PZ |
9136 | unlock: |
9137 | mutex_unlock(&event->mmap_mutex); | |
9138 | ||
a4be7c27 | 9139 | out: |
a4be7c27 PZ |
9140 | return ret; |
9141 | } | |
9142 | ||
f63a8daa PZ |
9143 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
9144 | { | |
9145 | if (b < a) | |
9146 | swap(a, b); | |
9147 | ||
9148 | mutex_lock(a); | |
9149 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
9150 | } | |
9151 | ||
34f43927 PZ |
9152 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
9153 | { | |
9154 | bool nmi_safe = false; | |
9155 | ||
9156 | switch (clk_id) { | |
9157 | case CLOCK_MONOTONIC: | |
9158 | event->clock = &ktime_get_mono_fast_ns; | |
9159 | nmi_safe = true; | |
9160 | break; | |
9161 | ||
9162 | case CLOCK_MONOTONIC_RAW: | |
9163 | event->clock = &ktime_get_raw_fast_ns; | |
9164 | nmi_safe = true; | |
9165 | break; | |
9166 | ||
9167 | case CLOCK_REALTIME: | |
9168 | event->clock = &ktime_get_real_ns; | |
9169 | break; | |
9170 | ||
9171 | case CLOCK_BOOTTIME: | |
9172 | event->clock = &ktime_get_boot_ns; | |
9173 | break; | |
9174 | ||
9175 | case CLOCK_TAI: | |
9176 | event->clock = &ktime_get_tai_ns; | |
9177 | break; | |
9178 | ||
9179 | default: | |
9180 | return -EINVAL; | |
9181 | } | |
9182 | ||
9183 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
9184 | return -EINVAL; | |
9185 | ||
9186 | return 0; | |
9187 | } | |
9188 | ||
0793a61d | 9189 | /** |
cdd6c482 | 9190 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 9191 | * |
cdd6c482 | 9192 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 9193 | * @pid: target pid |
9f66a381 | 9194 | * @cpu: target cpu |
cdd6c482 | 9195 | * @group_fd: group leader event fd |
0793a61d | 9196 | */ |
cdd6c482 IM |
9197 | SYSCALL_DEFINE5(perf_event_open, |
9198 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 9199 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 9200 | { |
b04243ef PZ |
9201 | struct perf_event *group_leader = NULL, *output_event = NULL; |
9202 | struct perf_event *event, *sibling; | |
cdd6c482 | 9203 | struct perf_event_attr attr; |
f63a8daa | 9204 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 9205 | struct file *event_file = NULL; |
2903ff01 | 9206 | struct fd group = {NULL, 0}; |
38a81da2 | 9207 | struct task_struct *task = NULL; |
89a1e187 | 9208 | struct pmu *pmu; |
ea635c64 | 9209 | int event_fd; |
b04243ef | 9210 | int move_group = 0; |
dc86cabe | 9211 | int err; |
a21b0b35 | 9212 | int f_flags = O_RDWR; |
79dff51e | 9213 | int cgroup_fd = -1; |
0793a61d | 9214 | |
2743a5b0 | 9215 | /* for future expandability... */ |
e5d1367f | 9216 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
9217 | return -EINVAL; |
9218 | ||
dc86cabe IM |
9219 | err = perf_copy_attr(attr_uptr, &attr); |
9220 | if (err) | |
9221 | return err; | |
eab656ae | 9222 | |
0764771d PZ |
9223 | if (!attr.exclude_kernel) { |
9224 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
9225 | return -EACCES; | |
9226 | } | |
9227 | ||
df58ab24 | 9228 | if (attr.freq) { |
cdd6c482 | 9229 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 9230 | return -EINVAL; |
0819b2e3 PZ |
9231 | } else { |
9232 | if (attr.sample_period & (1ULL << 63)) | |
9233 | return -EINVAL; | |
df58ab24 PZ |
9234 | } |
9235 | ||
97c79a38 ACM |
9236 | if (!attr.sample_max_stack) |
9237 | attr.sample_max_stack = sysctl_perf_event_max_stack; | |
9238 | ||
e5d1367f SE |
9239 | /* |
9240 | * In cgroup mode, the pid argument is used to pass the fd | |
9241 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
9242 | * designates the cpu on which to monitor threads from that | |
9243 | * cgroup. | |
9244 | */ | |
9245 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
9246 | return -EINVAL; | |
9247 | ||
a21b0b35 YD |
9248 | if (flags & PERF_FLAG_FD_CLOEXEC) |
9249 | f_flags |= O_CLOEXEC; | |
9250 | ||
9251 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
9252 | if (event_fd < 0) |
9253 | return event_fd; | |
9254 | ||
ac9721f3 | 9255 | if (group_fd != -1) { |
2903ff01 AV |
9256 | err = perf_fget_light(group_fd, &group); |
9257 | if (err) | |
d14b12d7 | 9258 | goto err_fd; |
2903ff01 | 9259 | group_leader = group.file->private_data; |
ac9721f3 PZ |
9260 | if (flags & PERF_FLAG_FD_OUTPUT) |
9261 | output_event = group_leader; | |
9262 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
9263 | group_leader = NULL; | |
9264 | } | |
9265 | ||
e5d1367f | 9266 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
9267 | task = find_lively_task_by_vpid(pid); |
9268 | if (IS_ERR(task)) { | |
9269 | err = PTR_ERR(task); | |
9270 | goto err_group_fd; | |
9271 | } | |
9272 | } | |
9273 | ||
1f4ee503 PZ |
9274 | if (task && group_leader && |
9275 | group_leader->attr.inherit != attr.inherit) { | |
9276 | err = -EINVAL; | |
9277 | goto err_task; | |
9278 | } | |
9279 | ||
fbfc623f YZ |
9280 | get_online_cpus(); |
9281 | ||
79c9ce57 PZ |
9282 | if (task) { |
9283 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); | |
9284 | if (err) | |
9285 | goto err_cpus; | |
9286 | ||
9287 | /* | |
9288 | * Reuse ptrace permission checks for now. | |
9289 | * | |
9290 | * We must hold cred_guard_mutex across this and any potential | |
9291 | * perf_install_in_context() call for this new event to | |
9292 | * serialize against exec() altering our credentials (and the | |
9293 | * perf_event_exit_task() that could imply). | |
9294 | */ | |
9295 | err = -EACCES; | |
9296 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
9297 | goto err_cred; | |
9298 | } | |
9299 | ||
79dff51e MF |
9300 | if (flags & PERF_FLAG_PID_CGROUP) |
9301 | cgroup_fd = pid; | |
9302 | ||
4dc0da86 | 9303 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 9304 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
9305 | if (IS_ERR(event)) { |
9306 | err = PTR_ERR(event); | |
79c9ce57 | 9307 | goto err_cred; |
d14b12d7 SE |
9308 | } |
9309 | ||
53b25335 VW |
9310 | if (is_sampling_event(event)) { |
9311 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
9312 | err = -ENOTSUPP; | |
9313 | goto err_alloc; | |
9314 | } | |
9315 | } | |
9316 | ||
89a1e187 PZ |
9317 | /* |
9318 | * Special case software events and allow them to be part of | |
9319 | * any hardware group. | |
9320 | */ | |
9321 | pmu = event->pmu; | |
b04243ef | 9322 | |
34f43927 PZ |
9323 | if (attr.use_clockid) { |
9324 | err = perf_event_set_clock(event, attr.clockid); | |
9325 | if (err) | |
9326 | goto err_alloc; | |
9327 | } | |
9328 | ||
b04243ef PZ |
9329 | if (group_leader && |
9330 | (is_software_event(event) != is_software_event(group_leader))) { | |
9331 | if (is_software_event(event)) { | |
9332 | /* | |
9333 | * If event and group_leader are not both a software | |
9334 | * event, and event is, then group leader is not. | |
9335 | * | |
9336 | * Allow the addition of software events to !software | |
9337 | * groups, this is safe because software events never | |
9338 | * fail to schedule. | |
9339 | */ | |
9340 | pmu = group_leader->pmu; | |
9341 | } else if (is_software_event(group_leader) && | |
9342 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { | |
9343 | /* | |
9344 | * In case the group is a pure software group, and we | |
9345 | * try to add a hardware event, move the whole group to | |
9346 | * the hardware context. | |
9347 | */ | |
9348 | move_group = 1; | |
9349 | } | |
9350 | } | |
89a1e187 PZ |
9351 | |
9352 | /* | |
9353 | * Get the target context (task or percpu): | |
9354 | */ | |
4af57ef2 | 9355 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
9356 | if (IS_ERR(ctx)) { |
9357 | err = PTR_ERR(ctx); | |
c6be5a5c | 9358 | goto err_alloc; |
89a1e187 PZ |
9359 | } |
9360 | ||
bed5b25a AS |
9361 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
9362 | err = -EBUSY; | |
9363 | goto err_context; | |
9364 | } | |
9365 | ||
ccff286d | 9366 | /* |
cdd6c482 | 9367 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 9368 | */ |
ac9721f3 | 9369 | if (group_leader) { |
dc86cabe | 9370 | err = -EINVAL; |
04289bb9 | 9371 | |
04289bb9 | 9372 | /* |
ccff286d IM |
9373 | * Do not allow a recursive hierarchy (this new sibling |
9374 | * becoming part of another group-sibling): | |
9375 | */ | |
9376 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 9377 | goto err_context; |
34f43927 PZ |
9378 | |
9379 | /* All events in a group should have the same clock */ | |
9380 | if (group_leader->clock != event->clock) | |
9381 | goto err_context; | |
9382 | ||
ccff286d IM |
9383 | /* |
9384 | * Do not allow to attach to a group in a different | |
9385 | * task or CPU context: | |
04289bb9 | 9386 | */ |
b04243ef | 9387 | if (move_group) { |
c3c87e77 PZ |
9388 | /* |
9389 | * Make sure we're both on the same task, or both | |
9390 | * per-cpu events. | |
9391 | */ | |
9392 | if (group_leader->ctx->task != ctx->task) | |
9393 | goto err_context; | |
9394 | ||
9395 | /* | |
9396 | * Make sure we're both events for the same CPU; | |
9397 | * grouping events for different CPUs is broken; since | |
9398 | * you can never concurrently schedule them anyhow. | |
9399 | */ | |
9400 | if (group_leader->cpu != event->cpu) | |
b04243ef PZ |
9401 | goto err_context; |
9402 | } else { | |
9403 | if (group_leader->ctx != ctx) | |
9404 | goto err_context; | |
9405 | } | |
9406 | ||
3b6f9e5c PM |
9407 | /* |
9408 | * Only a group leader can be exclusive or pinned | |
9409 | */ | |
0d48696f | 9410 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 9411 | goto err_context; |
ac9721f3 PZ |
9412 | } |
9413 | ||
9414 | if (output_event) { | |
9415 | err = perf_event_set_output(event, output_event); | |
9416 | if (err) | |
c3f00c70 | 9417 | goto err_context; |
ac9721f3 | 9418 | } |
0793a61d | 9419 | |
a21b0b35 YD |
9420 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
9421 | f_flags); | |
ea635c64 AV |
9422 | if (IS_ERR(event_file)) { |
9423 | err = PTR_ERR(event_file); | |
201c2f85 | 9424 | event_file = NULL; |
c3f00c70 | 9425 | goto err_context; |
ea635c64 | 9426 | } |
9b51f66d | 9427 | |
b04243ef | 9428 | if (move_group) { |
f63a8daa | 9429 | gctx = group_leader->ctx; |
f55fc2a5 | 9430 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
84c4e620 PZ |
9431 | if (gctx->task == TASK_TOMBSTONE) { |
9432 | err = -ESRCH; | |
9433 | goto err_locked; | |
9434 | } | |
f55fc2a5 PZ |
9435 | } else { |
9436 | mutex_lock(&ctx->mutex); | |
9437 | } | |
9438 | ||
84c4e620 PZ |
9439 | if (ctx->task == TASK_TOMBSTONE) { |
9440 | err = -ESRCH; | |
9441 | goto err_locked; | |
9442 | } | |
9443 | ||
a723968c PZ |
9444 | if (!perf_event_validate_size(event)) { |
9445 | err = -E2BIG; | |
9446 | goto err_locked; | |
9447 | } | |
9448 | ||
f55fc2a5 PZ |
9449 | /* |
9450 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
9451 | * because we need to serialize with concurrent event creation. | |
9452 | */ | |
9453 | if (!exclusive_event_installable(event, ctx)) { | |
9454 | /* exclusive and group stuff are assumed mutually exclusive */ | |
9455 | WARN_ON_ONCE(move_group); | |
f63a8daa | 9456 | |
f55fc2a5 PZ |
9457 | err = -EBUSY; |
9458 | goto err_locked; | |
9459 | } | |
f63a8daa | 9460 | |
f55fc2a5 PZ |
9461 | WARN_ON_ONCE(ctx->parent_ctx); |
9462 | ||
79c9ce57 PZ |
9463 | /* |
9464 | * This is the point on no return; we cannot fail hereafter. This is | |
9465 | * where we start modifying current state. | |
9466 | */ | |
9467 | ||
f55fc2a5 | 9468 | if (move_group) { |
f63a8daa PZ |
9469 | /* |
9470 | * See perf_event_ctx_lock() for comments on the details | |
9471 | * of swizzling perf_event::ctx. | |
9472 | */ | |
45a0e07a | 9473 | perf_remove_from_context(group_leader, 0); |
0231bb53 | 9474 | |
b04243ef PZ |
9475 | list_for_each_entry(sibling, &group_leader->sibling_list, |
9476 | group_entry) { | |
45a0e07a | 9477 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
9478 | put_ctx(gctx); |
9479 | } | |
b04243ef | 9480 | |
f63a8daa PZ |
9481 | /* |
9482 | * Wait for everybody to stop referencing the events through | |
9483 | * the old lists, before installing it on new lists. | |
9484 | */ | |
0cda4c02 | 9485 | synchronize_rcu(); |
f63a8daa | 9486 | |
8f95b435 PZI |
9487 | /* |
9488 | * Install the group siblings before the group leader. | |
9489 | * | |
9490 | * Because a group leader will try and install the entire group | |
9491 | * (through the sibling list, which is still in-tact), we can | |
9492 | * end up with siblings installed in the wrong context. | |
9493 | * | |
9494 | * By installing siblings first we NO-OP because they're not | |
9495 | * reachable through the group lists. | |
9496 | */ | |
b04243ef PZ |
9497 | list_for_each_entry(sibling, &group_leader->sibling_list, |
9498 | group_entry) { | |
8f95b435 | 9499 | perf_event__state_init(sibling); |
9fc81d87 | 9500 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
9501 | get_ctx(ctx); |
9502 | } | |
8f95b435 PZI |
9503 | |
9504 | /* | |
9505 | * Removing from the context ends up with disabled | |
9506 | * event. What we want here is event in the initial | |
9507 | * startup state, ready to be add into new context. | |
9508 | */ | |
9509 | perf_event__state_init(group_leader); | |
9510 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
9511 | get_ctx(ctx); | |
b04243ef | 9512 | |
f55fc2a5 PZ |
9513 | /* |
9514 | * Now that all events are installed in @ctx, nothing | |
9515 | * references @gctx anymore, so drop the last reference we have | |
9516 | * on it. | |
9517 | */ | |
9518 | put_ctx(gctx); | |
bed5b25a AS |
9519 | } |
9520 | ||
f73e22ab PZ |
9521 | /* |
9522 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
9523 | * that we're serialized against further additions and before | |
9524 | * perf_install_in_context() which is the point the event is active and | |
9525 | * can use these values. | |
9526 | */ | |
9527 | perf_event__header_size(event); | |
9528 | perf_event__id_header_size(event); | |
9529 | ||
78cd2c74 PZ |
9530 | event->owner = current; |
9531 | ||
e2d37cd2 | 9532 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 9533 | perf_unpin_context(ctx); |
f63a8daa | 9534 | |
f55fc2a5 | 9535 | if (move_group) |
f63a8daa | 9536 | mutex_unlock(&gctx->mutex); |
d859e29f | 9537 | mutex_unlock(&ctx->mutex); |
9b51f66d | 9538 | |
79c9ce57 PZ |
9539 | if (task) { |
9540 | mutex_unlock(&task->signal->cred_guard_mutex); | |
9541 | put_task_struct(task); | |
9542 | } | |
9543 | ||
fbfc623f YZ |
9544 | put_online_cpus(); |
9545 | ||
cdd6c482 IM |
9546 | mutex_lock(¤t->perf_event_mutex); |
9547 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
9548 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 9549 | |
8a49542c PZ |
9550 | /* |
9551 | * Drop the reference on the group_event after placing the | |
9552 | * new event on the sibling_list. This ensures destruction | |
9553 | * of the group leader will find the pointer to itself in | |
9554 | * perf_group_detach(). | |
9555 | */ | |
2903ff01 | 9556 | fdput(group); |
ea635c64 AV |
9557 | fd_install(event_fd, event_file); |
9558 | return event_fd; | |
0793a61d | 9559 | |
f55fc2a5 PZ |
9560 | err_locked: |
9561 | if (move_group) | |
9562 | mutex_unlock(&gctx->mutex); | |
9563 | mutex_unlock(&ctx->mutex); | |
9564 | /* err_file: */ | |
9565 | fput(event_file); | |
c3f00c70 | 9566 | err_context: |
fe4b04fa | 9567 | perf_unpin_context(ctx); |
ea635c64 | 9568 | put_ctx(ctx); |
c6be5a5c | 9569 | err_alloc: |
13005627 PZ |
9570 | /* |
9571 | * If event_file is set, the fput() above will have called ->release() | |
9572 | * and that will take care of freeing the event. | |
9573 | */ | |
9574 | if (!event_file) | |
9575 | free_event(event); | |
79c9ce57 PZ |
9576 | err_cred: |
9577 | if (task) | |
9578 | mutex_unlock(&task->signal->cred_guard_mutex); | |
1f4ee503 | 9579 | err_cpus: |
fbfc623f | 9580 | put_online_cpus(); |
1f4ee503 | 9581 | err_task: |
e7d0bc04 PZ |
9582 | if (task) |
9583 | put_task_struct(task); | |
89a1e187 | 9584 | err_group_fd: |
2903ff01 | 9585 | fdput(group); |
ea635c64 AV |
9586 | err_fd: |
9587 | put_unused_fd(event_fd); | |
dc86cabe | 9588 | return err; |
0793a61d TG |
9589 | } |
9590 | ||
fb0459d7 AV |
9591 | /** |
9592 | * perf_event_create_kernel_counter | |
9593 | * | |
9594 | * @attr: attributes of the counter to create | |
9595 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 9596 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
9597 | */ |
9598 | struct perf_event * | |
9599 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 9600 | struct task_struct *task, |
4dc0da86 AK |
9601 | perf_overflow_handler_t overflow_handler, |
9602 | void *context) | |
fb0459d7 | 9603 | { |
fb0459d7 | 9604 | struct perf_event_context *ctx; |
c3f00c70 | 9605 | struct perf_event *event; |
fb0459d7 | 9606 | int err; |
d859e29f | 9607 | |
fb0459d7 AV |
9608 | /* |
9609 | * Get the target context (task or percpu): | |
9610 | */ | |
d859e29f | 9611 | |
4dc0da86 | 9612 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 9613 | overflow_handler, context, -1); |
c3f00c70 PZ |
9614 | if (IS_ERR(event)) { |
9615 | err = PTR_ERR(event); | |
9616 | goto err; | |
9617 | } | |
d859e29f | 9618 | |
f8697762 | 9619 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 9620 | event->owner = TASK_TOMBSTONE; |
f8697762 | 9621 | |
4af57ef2 | 9622 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
9623 | if (IS_ERR(ctx)) { |
9624 | err = PTR_ERR(ctx); | |
c3f00c70 | 9625 | goto err_free; |
d859e29f | 9626 | } |
fb0459d7 | 9627 | |
fb0459d7 AV |
9628 | WARN_ON_ONCE(ctx->parent_ctx); |
9629 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
9630 | if (ctx->task == TASK_TOMBSTONE) { |
9631 | err = -ESRCH; | |
9632 | goto err_unlock; | |
9633 | } | |
9634 | ||
bed5b25a | 9635 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 9636 | err = -EBUSY; |
84c4e620 | 9637 | goto err_unlock; |
bed5b25a AS |
9638 | } |
9639 | ||
fb0459d7 | 9640 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 9641 | perf_unpin_context(ctx); |
fb0459d7 AV |
9642 | mutex_unlock(&ctx->mutex); |
9643 | ||
fb0459d7 AV |
9644 | return event; |
9645 | ||
84c4e620 PZ |
9646 | err_unlock: |
9647 | mutex_unlock(&ctx->mutex); | |
9648 | perf_unpin_context(ctx); | |
9649 | put_ctx(ctx); | |
c3f00c70 PZ |
9650 | err_free: |
9651 | free_event(event); | |
9652 | err: | |
c6567f64 | 9653 | return ERR_PTR(err); |
9b51f66d | 9654 | } |
fb0459d7 | 9655 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 9656 | |
0cda4c02 YZ |
9657 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
9658 | { | |
9659 | struct perf_event_context *src_ctx; | |
9660 | struct perf_event_context *dst_ctx; | |
9661 | struct perf_event *event, *tmp; | |
9662 | LIST_HEAD(events); | |
9663 | ||
9664 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
9665 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
9666 | ||
f63a8daa PZ |
9667 | /* |
9668 | * See perf_event_ctx_lock() for comments on the details | |
9669 | * of swizzling perf_event::ctx. | |
9670 | */ | |
9671 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
9672 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
9673 | event_entry) { | |
45a0e07a | 9674 | perf_remove_from_context(event, 0); |
9a545de0 | 9675 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 9676 | put_ctx(src_ctx); |
9886167d | 9677 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 9678 | } |
0cda4c02 | 9679 | |
8f95b435 PZI |
9680 | /* |
9681 | * Wait for the events to quiesce before re-instating them. | |
9682 | */ | |
0cda4c02 YZ |
9683 | synchronize_rcu(); |
9684 | ||
8f95b435 PZI |
9685 | /* |
9686 | * Re-instate events in 2 passes. | |
9687 | * | |
9688 | * Skip over group leaders and only install siblings on this first | |
9689 | * pass, siblings will not get enabled without a leader, however a | |
9690 | * leader will enable its siblings, even if those are still on the old | |
9691 | * context. | |
9692 | */ | |
9693 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
9694 | if (event->group_leader == event) | |
9695 | continue; | |
9696 | ||
9697 | list_del(&event->migrate_entry); | |
9698 | if (event->state >= PERF_EVENT_STATE_OFF) | |
9699 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9700 | account_event_cpu(event, dst_cpu); | |
9701 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
9702 | get_ctx(dst_ctx); | |
9703 | } | |
9704 | ||
9705 | /* | |
9706 | * Once all the siblings are setup properly, install the group leaders | |
9707 | * to make it go. | |
9708 | */ | |
9886167d PZ |
9709 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
9710 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
9711 | if (event->state >= PERF_EVENT_STATE_OFF) |
9712 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 9713 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
9714 | perf_install_in_context(dst_ctx, event, dst_cpu); |
9715 | get_ctx(dst_ctx); | |
9716 | } | |
9717 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 9718 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
9719 | } |
9720 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
9721 | ||
cdd6c482 | 9722 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 9723 | struct task_struct *child) |
d859e29f | 9724 | { |
cdd6c482 | 9725 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 9726 | u64 child_val; |
d859e29f | 9727 | |
cdd6c482 IM |
9728 | if (child_event->attr.inherit_stat) |
9729 | perf_event_read_event(child_event, child); | |
38b200d6 | 9730 | |
b5e58793 | 9731 | child_val = perf_event_count(child_event); |
d859e29f PM |
9732 | |
9733 | /* | |
9734 | * Add back the child's count to the parent's count: | |
9735 | */ | |
a6e6dea6 | 9736 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
9737 | atomic64_add(child_event->total_time_enabled, |
9738 | &parent_event->child_total_time_enabled); | |
9739 | atomic64_add(child_event->total_time_running, | |
9740 | &parent_event->child_total_time_running); | |
d859e29f PM |
9741 | } |
9742 | ||
9b51f66d | 9743 | static void |
8ba289b8 PZ |
9744 | perf_event_exit_event(struct perf_event *child_event, |
9745 | struct perf_event_context *child_ctx, | |
9746 | struct task_struct *child) | |
9b51f66d | 9747 | { |
8ba289b8 PZ |
9748 | struct perf_event *parent_event = child_event->parent; |
9749 | ||
1903d50c PZ |
9750 | /* |
9751 | * Do not destroy the 'original' grouping; because of the context | |
9752 | * switch optimization the original events could've ended up in a | |
9753 | * random child task. | |
9754 | * | |
9755 | * If we were to destroy the original group, all group related | |
9756 | * operations would cease to function properly after this random | |
9757 | * child dies. | |
9758 | * | |
9759 | * Do destroy all inherited groups, we don't care about those | |
9760 | * and being thorough is better. | |
9761 | */ | |
32132a3d PZ |
9762 | raw_spin_lock_irq(&child_ctx->lock); |
9763 | WARN_ON_ONCE(child_ctx->is_active); | |
9764 | ||
8ba289b8 | 9765 | if (parent_event) |
32132a3d PZ |
9766 | perf_group_detach(child_event); |
9767 | list_del_event(child_event, child_ctx); | |
a69b0ca4 | 9768 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
32132a3d | 9769 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 9770 | |
9b51f66d | 9771 | /* |
8ba289b8 | 9772 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 9773 | */ |
8ba289b8 | 9774 | if (!parent_event) { |
179033b3 | 9775 | perf_event_wakeup(child_event); |
8ba289b8 | 9776 | return; |
4bcf349a | 9777 | } |
8ba289b8 PZ |
9778 | /* |
9779 | * Child events can be cleaned up. | |
9780 | */ | |
9781 | ||
9782 | sync_child_event(child_event, child); | |
9783 | ||
9784 | /* | |
9785 | * Remove this event from the parent's list | |
9786 | */ | |
9787 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
9788 | mutex_lock(&parent_event->child_mutex); | |
9789 | list_del_init(&child_event->child_list); | |
9790 | mutex_unlock(&parent_event->child_mutex); | |
9791 | ||
9792 | /* | |
9793 | * Kick perf_poll() for is_event_hup(). | |
9794 | */ | |
9795 | perf_event_wakeup(parent_event); | |
9796 | free_event(child_event); | |
9797 | put_event(parent_event); | |
9b51f66d IM |
9798 | } |
9799 | ||
8dc85d54 | 9800 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 9801 | { |
211de6eb | 9802 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 9803 | struct perf_event *child_event, *next; |
63b6da39 PZ |
9804 | |
9805 | WARN_ON_ONCE(child != current); | |
9b51f66d | 9806 | |
6a3351b6 | 9807 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 9808 | if (!child_ctx) |
9b51f66d IM |
9809 | return; |
9810 | ||
ad3a37de | 9811 | /* |
6a3351b6 PZ |
9812 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
9813 | * ctx::mutex over the entire thing. This serializes against almost | |
9814 | * everything that wants to access the ctx. | |
9815 | * | |
9816 | * The exception is sys_perf_event_open() / | |
9817 | * perf_event_create_kernel_count() which does find_get_context() | |
9818 | * without ctx::mutex (it cannot because of the move_group double mutex | |
9819 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 9820 | */ |
6a3351b6 | 9821 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
9822 | |
9823 | /* | |
6a3351b6 PZ |
9824 | * In a single ctx::lock section, de-schedule the events and detach the |
9825 | * context from the task such that we cannot ever get it scheduled back | |
9826 | * in. | |
c93f7669 | 9827 | */ |
6a3351b6 | 9828 | raw_spin_lock_irq(&child_ctx->lock); |
63b6da39 | 9829 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
4a1c0f26 | 9830 | |
71a851b4 | 9831 | /* |
63b6da39 PZ |
9832 | * Now that the context is inactive, destroy the task <-> ctx relation |
9833 | * and mark the context dead. | |
71a851b4 | 9834 | */ |
63b6da39 PZ |
9835 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
9836 | put_ctx(child_ctx); /* cannot be last */ | |
9837 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
9838 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 9839 | |
211de6eb | 9840 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 9841 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 9842 | |
211de6eb PZ |
9843 | if (clone_ctx) |
9844 | put_ctx(clone_ctx); | |
4a1c0f26 | 9845 | |
9f498cc5 | 9846 | /* |
cdd6c482 IM |
9847 | * Report the task dead after unscheduling the events so that we |
9848 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
9849 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 9850 | */ |
cdd6c482 | 9851 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 9852 | |
ebf905fc | 9853 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 9854 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 9855 | |
a63eaf34 PM |
9856 | mutex_unlock(&child_ctx->mutex); |
9857 | ||
9858 | put_ctx(child_ctx); | |
9b51f66d IM |
9859 | } |
9860 | ||
8dc85d54 PZ |
9861 | /* |
9862 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 PZ |
9863 | * |
9864 | * Can be called with cred_guard_mutex held when called from | |
9865 | * install_exec_creds(). | |
8dc85d54 PZ |
9866 | */ |
9867 | void perf_event_exit_task(struct task_struct *child) | |
9868 | { | |
8882135b | 9869 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
9870 | int ctxn; |
9871 | ||
8882135b PZ |
9872 | mutex_lock(&child->perf_event_mutex); |
9873 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
9874 | owner_entry) { | |
9875 | list_del_init(&event->owner_entry); | |
9876 | ||
9877 | /* | |
9878 | * Ensure the list deletion is visible before we clear | |
9879 | * the owner, closes a race against perf_release() where | |
9880 | * we need to serialize on the owner->perf_event_mutex. | |
9881 | */ | |
f47c02c0 | 9882 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
9883 | } |
9884 | mutex_unlock(&child->perf_event_mutex); | |
9885 | ||
8dc85d54 PZ |
9886 | for_each_task_context_nr(ctxn) |
9887 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
9888 | |
9889 | /* | |
9890 | * The perf_event_exit_task_context calls perf_event_task | |
9891 | * with child's task_ctx, which generates EXIT events for | |
9892 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
9893 | * At this point we need to send EXIT events to cpu contexts. | |
9894 | */ | |
9895 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
9896 | } |
9897 | ||
889ff015 FW |
9898 | static void perf_free_event(struct perf_event *event, |
9899 | struct perf_event_context *ctx) | |
9900 | { | |
9901 | struct perf_event *parent = event->parent; | |
9902 | ||
9903 | if (WARN_ON_ONCE(!parent)) | |
9904 | return; | |
9905 | ||
9906 | mutex_lock(&parent->child_mutex); | |
9907 | list_del_init(&event->child_list); | |
9908 | mutex_unlock(&parent->child_mutex); | |
9909 | ||
a6fa941d | 9910 | put_event(parent); |
889ff015 | 9911 | |
652884fe | 9912 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 9913 | perf_group_detach(event); |
889ff015 | 9914 | list_del_event(event, ctx); |
652884fe | 9915 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
9916 | free_event(event); |
9917 | } | |
9918 | ||
bbbee908 | 9919 | /* |
652884fe | 9920 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 9921 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
9922 | * |
9923 | * Not all locks are strictly required, but take them anyway to be nice and | |
9924 | * help out with the lockdep assertions. | |
bbbee908 | 9925 | */ |
cdd6c482 | 9926 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 9927 | { |
8dc85d54 | 9928 | struct perf_event_context *ctx; |
cdd6c482 | 9929 | struct perf_event *event, *tmp; |
8dc85d54 | 9930 | int ctxn; |
bbbee908 | 9931 | |
8dc85d54 PZ |
9932 | for_each_task_context_nr(ctxn) { |
9933 | ctx = task->perf_event_ctxp[ctxn]; | |
9934 | if (!ctx) | |
9935 | continue; | |
bbbee908 | 9936 | |
8dc85d54 | 9937 | mutex_lock(&ctx->mutex); |
bbbee908 | 9938 | again: |
8dc85d54 PZ |
9939 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
9940 | group_entry) | |
9941 | perf_free_event(event, ctx); | |
bbbee908 | 9942 | |
8dc85d54 PZ |
9943 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
9944 | group_entry) | |
9945 | perf_free_event(event, ctx); | |
bbbee908 | 9946 | |
8dc85d54 PZ |
9947 | if (!list_empty(&ctx->pinned_groups) || |
9948 | !list_empty(&ctx->flexible_groups)) | |
9949 | goto again; | |
bbbee908 | 9950 | |
8dc85d54 | 9951 | mutex_unlock(&ctx->mutex); |
bbbee908 | 9952 | |
8dc85d54 PZ |
9953 | put_ctx(ctx); |
9954 | } | |
889ff015 FW |
9955 | } |
9956 | ||
4e231c79 PZ |
9957 | void perf_event_delayed_put(struct task_struct *task) |
9958 | { | |
9959 | int ctxn; | |
9960 | ||
9961 | for_each_task_context_nr(ctxn) | |
9962 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
9963 | } | |
9964 | ||
e03e7ee3 | 9965 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 9966 | { |
e03e7ee3 | 9967 | struct file *file; |
ffe8690c | 9968 | |
e03e7ee3 AS |
9969 | file = fget_raw(fd); |
9970 | if (!file) | |
9971 | return ERR_PTR(-EBADF); | |
ffe8690c | 9972 | |
e03e7ee3 AS |
9973 | if (file->f_op != &perf_fops) { |
9974 | fput(file); | |
9975 | return ERR_PTR(-EBADF); | |
9976 | } | |
ffe8690c | 9977 | |
e03e7ee3 | 9978 | return file; |
ffe8690c KX |
9979 | } |
9980 | ||
9981 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
9982 | { | |
9983 | if (!event) | |
9984 | return ERR_PTR(-EINVAL); | |
9985 | ||
9986 | return &event->attr; | |
9987 | } | |
9988 | ||
97dee4f3 PZ |
9989 | /* |
9990 | * inherit a event from parent task to child task: | |
9991 | */ | |
9992 | static struct perf_event * | |
9993 | inherit_event(struct perf_event *parent_event, | |
9994 | struct task_struct *parent, | |
9995 | struct perf_event_context *parent_ctx, | |
9996 | struct task_struct *child, | |
9997 | struct perf_event *group_leader, | |
9998 | struct perf_event_context *child_ctx) | |
9999 | { | |
1929def9 | 10000 | enum perf_event_active_state parent_state = parent_event->state; |
97dee4f3 | 10001 | struct perf_event *child_event; |
cee010ec | 10002 | unsigned long flags; |
97dee4f3 PZ |
10003 | |
10004 | /* | |
10005 | * Instead of creating recursive hierarchies of events, | |
10006 | * we link inherited events back to the original parent, | |
10007 | * which has a filp for sure, which we use as the reference | |
10008 | * count: | |
10009 | */ | |
10010 | if (parent_event->parent) | |
10011 | parent_event = parent_event->parent; | |
10012 | ||
10013 | child_event = perf_event_alloc(&parent_event->attr, | |
10014 | parent_event->cpu, | |
d580ff86 | 10015 | child, |
97dee4f3 | 10016 | group_leader, parent_event, |
79dff51e | 10017 | NULL, NULL, -1); |
97dee4f3 PZ |
10018 | if (IS_ERR(child_event)) |
10019 | return child_event; | |
a6fa941d | 10020 | |
c6e5b732 PZ |
10021 | /* |
10022 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
10023 | * must be under the same lock in order to serialize against | |
10024 | * perf_event_release_kernel(), such that either we must observe | |
10025 | * is_orphaned_event() or they will observe us on the child_list. | |
10026 | */ | |
10027 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
10028 | if (is_orphaned_event(parent_event) || |
10029 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 10030 | mutex_unlock(&parent_event->child_mutex); |
a6fa941d AV |
10031 | free_event(child_event); |
10032 | return NULL; | |
10033 | } | |
10034 | ||
97dee4f3 PZ |
10035 | get_ctx(child_ctx); |
10036 | ||
10037 | /* | |
10038 | * Make the child state follow the state of the parent event, | |
10039 | * not its attr.disabled bit. We hold the parent's mutex, | |
10040 | * so we won't race with perf_event_{en, dis}able_family. | |
10041 | */ | |
1929def9 | 10042 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
10043 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
10044 | else | |
10045 | child_event->state = PERF_EVENT_STATE_OFF; | |
10046 | ||
10047 | if (parent_event->attr.freq) { | |
10048 | u64 sample_period = parent_event->hw.sample_period; | |
10049 | struct hw_perf_event *hwc = &child_event->hw; | |
10050 | ||
10051 | hwc->sample_period = sample_period; | |
10052 | hwc->last_period = sample_period; | |
10053 | ||
10054 | local64_set(&hwc->period_left, sample_period); | |
10055 | } | |
10056 | ||
10057 | child_event->ctx = child_ctx; | |
10058 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
10059 | child_event->overflow_handler_context |
10060 | = parent_event->overflow_handler_context; | |
97dee4f3 | 10061 | |
614b6780 TG |
10062 | /* |
10063 | * Precalculate sample_data sizes | |
10064 | */ | |
10065 | perf_event__header_size(child_event); | |
6844c09d | 10066 | perf_event__id_header_size(child_event); |
614b6780 | 10067 | |
97dee4f3 PZ |
10068 | /* |
10069 | * Link it up in the child's context: | |
10070 | */ | |
cee010ec | 10071 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 10072 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 10073 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 10074 | |
97dee4f3 PZ |
10075 | /* |
10076 | * Link this into the parent event's child list | |
10077 | */ | |
97dee4f3 PZ |
10078 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
10079 | mutex_unlock(&parent_event->child_mutex); | |
10080 | ||
10081 | return child_event; | |
10082 | } | |
10083 | ||
10084 | static int inherit_group(struct perf_event *parent_event, | |
10085 | struct task_struct *parent, | |
10086 | struct perf_event_context *parent_ctx, | |
10087 | struct task_struct *child, | |
10088 | struct perf_event_context *child_ctx) | |
10089 | { | |
10090 | struct perf_event *leader; | |
10091 | struct perf_event *sub; | |
10092 | struct perf_event *child_ctr; | |
10093 | ||
10094 | leader = inherit_event(parent_event, parent, parent_ctx, | |
10095 | child, NULL, child_ctx); | |
10096 | if (IS_ERR(leader)) | |
10097 | return PTR_ERR(leader); | |
10098 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { | |
10099 | child_ctr = inherit_event(sub, parent, parent_ctx, | |
10100 | child, leader, child_ctx); | |
10101 | if (IS_ERR(child_ctr)) | |
10102 | return PTR_ERR(child_ctr); | |
10103 | } | |
10104 | return 0; | |
889ff015 FW |
10105 | } |
10106 | ||
10107 | static int | |
10108 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
10109 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 10110 | struct task_struct *child, int ctxn, |
889ff015 FW |
10111 | int *inherited_all) |
10112 | { | |
10113 | int ret; | |
8dc85d54 | 10114 | struct perf_event_context *child_ctx; |
889ff015 FW |
10115 | |
10116 | if (!event->attr.inherit) { | |
10117 | *inherited_all = 0; | |
10118 | return 0; | |
bbbee908 PZ |
10119 | } |
10120 | ||
fe4b04fa | 10121 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
10122 | if (!child_ctx) { |
10123 | /* | |
10124 | * This is executed from the parent task context, so | |
10125 | * inherit events that have been marked for cloning. | |
10126 | * First allocate and initialize a context for the | |
10127 | * child. | |
10128 | */ | |
bbbee908 | 10129 | |
734df5ab | 10130 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
10131 | if (!child_ctx) |
10132 | return -ENOMEM; | |
bbbee908 | 10133 | |
8dc85d54 | 10134 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
10135 | } |
10136 | ||
10137 | ret = inherit_group(event, parent, parent_ctx, | |
10138 | child, child_ctx); | |
10139 | ||
10140 | if (ret) | |
10141 | *inherited_all = 0; | |
10142 | ||
10143 | return ret; | |
bbbee908 PZ |
10144 | } |
10145 | ||
9b51f66d | 10146 | /* |
cdd6c482 | 10147 | * Initialize the perf_event context in task_struct |
9b51f66d | 10148 | */ |
985c8dcb | 10149 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 10150 | { |
889ff015 | 10151 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
10152 | struct perf_event_context *cloned_ctx; |
10153 | struct perf_event *event; | |
9b51f66d | 10154 | struct task_struct *parent = current; |
564c2b21 | 10155 | int inherited_all = 1; |
dddd3379 | 10156 | unsigned long flags; |
6ab423e0 | 10157 | int ret = 0; |
9b51f66d | 10158 | |
8dc85d54 | 10159 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
10160 | return 0; |
10161 | ||
ad3a37de | 10162 | /* |
25346b93 PM |
10163 | * If the parent's context is a clone, pin it so it won't get |
10164 | * swapped under us. | |
ad3a37de | 10165 | */ |
8dc85d54 | 10166 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
10167 | if (!parent_ctx) |
10168 | return 0; | |
25346b93 | 10169 | |
ad3a37de PM |
10170 | /* |
10171 | * No need to check if parent_ctx != NULL here; since we saw | |
10172 | * it non-NULL earlier, the only reason for it to become NULL | |
10173 | * is if we exit, and since we're currently in the middle of | |
10174 | * a fork we can't be exiting at the same time. | |
10175 | */ | |
ad3a37de | 10176 | |
9b51f66d IM |
10177 | /* |
10178 | * Lock the parent list. No need to lock the child - not PID | |
10179 | * hashed yet and not running, so nobody can access it. | |
10180 | */ | |
d859e29f | 10181 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
10182 | |
10183 | /* | |
10184 | * We dont have to disable NMIs - we are only looking at | |
10185 | * the list, not manipulating it: | |
10186 | */ | |
889ff015 | 10187 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
8dc85d54 PZ |
10188 | ret = inherit_task_group(event, parent, parent_ctx, |
10189 | child, ctxn, &inherited_all); | |
889ff015 FW |
10190 | if (ret) |
10191 | break; | |
10192 | } | |
b93f7978 | 10193 | |
dddd3379 TG |
10194 | /* |
10195 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
10196 | * to allocations, but we need to prevent rotation because | |
10197 | * rotate_ctx() will change the list from interrupt context. | |
10198 | */ | |
10199 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
10200 | parent_ctx->rotate_disable = 1; | |
10201 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
10202 | ||
889ff015 | 10203 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
8dc85d54 PZ |
10204 | ret = inherit_task_group(event, parent, parent_ctx, |
10205 | child, ctxn, &inherited_all); | |
889ff015 | 10206 | if (ret) |
9b51f66d | 10207 | break; |
564c2b21 PM |
10208 | } |
10209 | ||
dddd3379 TG |
10210 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
10211 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 10212 | |
8dc85d54 | 10213 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 10214 | |
05cbaa28 | 10215 | if (child_ctx && inherited_all) { |
564c2b21 PM |
10216 | /* |
10217 | * Mark the child context as a clone of the parent | |
10218 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
10219 | * |
10220 | * Note that if the parent is a clone, the holding of | |
10221 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 10222 | */ |
c5ed5145 | 10223 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
10224 | if (cloned_ctx) { |
10225 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 10226 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
10227 | } else { |
10228 | child_ctx->parent_ctx = parent_ctx; | |
10229 | child_ctx->parent_gen = parent_ctx->generation; | |
10230 | } | |
10231 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
10232 | } |
10233 | ||
c5ed5145 | 10234 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
d859e29f | 10235 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 10236 | |
25346b93 | 10237 | perf_unpin_context(parent_ctx); |
fe4b04fa | 10238 | put_ctx(parent_ctx); |
ad3a37de | 10239 | |
6ab423e0 | 10240 | return ret; |
9b51f66d IM |
10241 | } |
10242 | ||
8dc85d54 PZ |
10243 | /* |
10244 | * Initialize the perf_event context in task_struct | |
10245 | */ | |
10246 | int perf_event_init_task(struct task_struct *child) | |
10247 | { | |
10248 | int ctxn, ret; | |
10249 | ||
8550d7cb ON |
10250 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
10251 | mutex_init(&child->perf_event_mutex); | |
10252 | INIT_LIST_HEAD(&child->perf_event_list); | |
10253 | ||
8dc85d54 PZ |
10254 | for_each_task_context_nr(ctxn) { |
10255 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
10256 | if (ret) { |
10257 | perf_event_free_task(child); | |
8dc85d54 | 10258 | return ret; |
6c72e350 | 10259 | } |
8dc85d54 PZ |
10260 | } |
10261 | ||
10262 | return 0; | |
10263 | } | |
10264 | ||
220b140b PM |
10265 | static void __init perf_event_init_all_cpus(void) |
10266 | { | |
b28ab83c | 10267 | struct swevent_htable *swhash; |
220b140b | 10268 | int cpu; |
220b140b PM |
10269 | |
10270 | for_each_possible_cpu(cpu) { | |
b28ab83c PZ |
10271 | swhash = &per_cpu(swevent_htable, cpu); |
10272 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 10273 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
10274 | |
10275 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
10276 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
220b140b PM |
10277 | } |
10278 | } | |
10279 | ||
0db0628d | 10280 | static void perf_event_init_cpu(int cpu) |
0793a61d | 10281 | { |
108b02cf | 10282 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 10283 | |
b28ab83c | 10284 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 10285 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
10286 | struct swevent_hlist *hlist; |
10287 | ||
b28ab83c PZ |
10288 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
10289 | WARN_ON(!hlist); | |
10290 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 10291 | } |
b28ab83c | 10292 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
10293 | } |
10294 | ||
2965faa5 | 10295 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 10296 | static void __perf_event_exit_context(void *__info) |
0793a61d | 10297 | { |
108b02cf | 10298 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
10299 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
10300 | struct perf_event *event; | |
0793a61d | 10301 | |
fae3fde6 PZ |
10302 | raw_spin_lock(&ctx->lock); |
10303 | list_for_each_entry(event, &ctx->event_list, event_entry) | |
45a0e07a | 10304 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 10305 | raw_spin_unlock(&ctx->lock); |
0793a61d | 10306 | } |
108b02cf PZ |
10307 | |
10308 | static void perf_event_exit_cpu_context(int cpu) | |
10309 | { | |
10310 | struct perf_event_context *ctx; | |
10311 | struct pmu *pmu; | |
10312 | int idx; | |
10313 | ||
10314 | idx = srcu_read_lock(&pmus_srcu); | |
10315 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
917bdd1c | 10316 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
108b02cf PZ |
10317 | |
10318 | mutex_lock(&ctx->mutex); | |
10319 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
10320 | mutex_unlock(&ctx->mutex); | |
10321 | } | |
10322 | srcu_read_unlock(&pmus_srcu, idx); | |
108b02cf PZ |
10323 | } |
10324 | ||
cdd6c482 | 10325 | static void perf_event_exit_cpu(int cpu) |
0793a61d | 10326 | { |
e3703f8c | 10327 | perf_event_exit_cpu_context(cpu); |
0793a61d TG |
10328 | } |
10329 | #else | |
cdd6c482 | 10330 | static inline void perf_event_exit_cpu(int cpu) { } |
0793a61d TG |
10331 | #endif |
10332 | ||
c277443c PZ |
10333 | static int |
10334 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
10335 | { | |
10336 | int cpu; | |
10337 | ||
10338 | for_each_online_cpu(cpu) | |
10339 | perf_event_exit_cpu(cpu); | |
10340 | ||
10341 | return NOTIFY_OK; | |
10342 | } | |
10343 | ||
10344 | /* | |
10345 | * Run the perf reboot notifier at the very last possible moment so that | |
10346 | * the generic watchdog code runs as long as possible. | |
10347 | */ | |
10348 | static struct notifier_block perf_reboot_notifier = { | |
10349 | .notifier_call = perf_reboot, | |
10350 | .priority = INT_MIN, | |
10351 | }; | |
10352 | ||
0db0628d | 10353 | static int |
0793a61d TG |
10354 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
10355 | { | |
10356 | unsigned int cpu = (long)hcpu; | |
10357 | ||
4536e4d1 | 10358 | switch (action & ~CPU_TASKS_FROZEN) { |
0793a61d TG |
10359 | |
10360 | case CPU_UP_PREPARE: | |
1dcaac1c PZ |
10361 | /* |
10362 | * This must be done before the CPU comes alive, because the | |
10363 | * moment we can run tasks we can encounter (software) events. | |
10364 | * | |
10365 | * Specifically, someone can have inherited events on kthreadd | |
10366 | * or a pre-existing worker thread that gets re-bound. | |
10367 | */ | |
cdd6c482 | 10368 | perf_event_init_cpu(cpu); |
0793a61d TG |
10369 | break; |
10370 | ||
10371 | case CPU_DOWN_PREPARE: | |
1dcaac1c PZ |
10372 | /* |
10373 | * This must be done before the CPU dies because after that an | |
10374 | * active event might want to IPI the CPU and that'll not work | |
10375 | * so great for dead CPUs. | |
10376 | * | |
10377 | * XXX smp_call_function_single() return -ENXIO without a warn | |
10378 | * so we could possibly deal with this. | |
10379 | * | |
10380 | * This is safe against new events arriving because | |
10381 | * sys_perf_event_open() serializes against hotplug using | |
10382 | * get_online_cpus(). | |
10383 | */ | |
cdd6c482 | 10384 | perf_event_exit_cpu(cpu); |
0793a61d | 10385 | break; |
0793a61d TG |
10386 | default: |
10387 | break; | |
10388 | } | |
10389 | ||
10390 | return NOTIFY_OK; | |
10391 | } | |
10392 | ||
cdd6c482 | 10393 | void __init perf_event_init(void) |
0793a61d | 10394 | { |
3c502e7a JW |
10395 | int ret; |
10396 | ||
2e80a82a PZ |
10397 | idr_init(&pmu_idr); |
10398 | ||
220b140b | 10399 | perf_event_init_all_cpus(); |
b0a873eb | 10400 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
10401 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
10402 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
10403 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb PZ |
10404 | perf_tp_register(); |
10405 | perf_cpu_notifier(perf_cpu_notify); | |
c277443c | 10406 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
10407 | |
10408 | ret = init_hw_breakpoint(); | |
10409 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 10410 | |
b01c3a00 JO |
10411 | /* |
10412 | * Build time assertion that we keep the data_head at the intended | |
10413 | * location. IOW, validation we got the __reserved[] size right. | |
10414 | */ | |
10415 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
10416 | != 1024); | |
0793a61d | 10417 | } |
abe43400 | 10418 | |
fd979c01 CS |
10419 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
10420 | char *page) | |
10421 | { | |
10422 | struct perf_pmu_events_attr *pmu_attr = | |
10423 | container_of(attr, struct perf_pmu_events_attr, attr); | |
10424 | ||
10425 | if (pmu_attr->event_str) | |
10426 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
10427 | ||
10428 | return 0; | |
10429 | } | |
675965b0 | 10430 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 10431 | |
abe43400 PZ |
10432 | static int __init perf_event_sysfs_init(void) |
10433 | { | |
10434 | struct pmu *pmu; | |
10435 | int ret; | |
10436 | ||
10437 | mutex_lock(&pmus_lock); | |
10438 | ||
10439 | ret = bus_register(&pmu_bus); | |
10440 | if (ret) | |
10441 | goto unlock; | |
10442 | ||
10443 | list_for_each_entry(pmu, &pmus, entry) { | |
10444 | if (!pmu->name || pmu->type < 0) | |
10445 | continue; | |
10446 | ||
10447 | ret = pmu_dev_alloc(pmu); | |
10448 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
10449 | } | |
10450 | pmu_bus_running = 1; | |
10451 | ret = 0; | |
10452 | ||
10453 | unlock: | |
10454 | mutex_unlock(&pmus_lock); | |
10455 | ||
10456 | return ret; | |
10457 | } | |
10458 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
10459 | |
10460 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
10461 | static struct cgroup_subsys_state * |
10462 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
10463 | { |
10464 | struct perf_cgroup *jc; | |
e5d1367f | 10465 | |
1b15d055 | 10466 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
10467 | if (!jc) |
10468 | return ERR_PTR(-ENOMEM); | |
10469 | ||
e5d1367f SE |
10470 | jc->info = alloc_percpu(struct perf_cgroup_info); |
10471 | if (!jc->info) { | |
10472 | kfree(jc); | |
10473 | return ERR_PTR(-ENOMEM); | |
10474 | } | |
10475 | ||
e5d1367f SE |
10476 | return &jc->css; |
10477 | } | |
10478 | ||
eb95419b | 10479 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 10480 | { |
eb95419b TH |
10481 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
10482 | ||
e5d1367f SE |
10483 | free_percpu(jc->info); |
10484 | kfree(jc); | |
10485 | } | |
10486 | ||
10487 | static int __perf_cgroup_move(void *info) | |
10488 | { | |
10489 | struct task_struct *task = info; | |
ddaaf4e2 | 10490 | rcu_read_lock(); |
e5d1367f | 10491 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 10492 | rcu_read_unlock(); |
e5d1367f SE |
10493 | return 0; |
10494 | } | |
10495 | ||
1f7dd3e5 | 10496 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 10497 | { |
bb9d97b6 | 10498 | struct task_struct *task; |
1f7dd3e5 | 10499 | struct cgroup_subsys_state *css; |
bb9d97b6 | 10500 | |
1f7dd3e5 | 10501 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 10502 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
10503 | } |
10504 | ||
073219e9 | 10505 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
10506 | .css_alloc = perf_cgroup_css_alloc, |
10507 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 10508 | .attach = perf_cgroup_attach, |
e5d1367f SE |
10509 | }; |
10510 | #endif /* CONFIG_CGROUP_PERF */ |