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 | ||
400 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); | |
14c63f17 DH |
401 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
402 | update_perf_cpu_limits(); | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
407 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
408 | ||
409 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
410 | void __user *buffer, size_t *lenp, | |
411 | loff_t *ppos) | |
412 | { | |
413 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
414 | ||
415 | if (ret || !write) | |
416 | return ret; | |
417 | ||
b303e7c1 PZ |
418 | if (sysctl_perf_cpu_time_max_percent == 100 || |
419 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
420 | printk(KERN_WARNING |
421 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
422 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
423 | } else { | |
424 | update_perf_cpu_limits(); | |
425 | } | |
163ec435 PZ |
426 | |
427 | return 0; | |
428 | } | |
1ccd1549 | 429 | |
14c63f17 DH |
430 | /* |
431 | * perf samples are done in some very critical code paths (NMIs). | |
432 | * If they take too much CPU time, the system can lock up and not | |
433 | * get any real work done. This will drop the sample rate when | |
434 | * we detect that events are taking too long. | |
435 | */ | |
436 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 437 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 438 | |
91a612ee PZ |
439 | static u64 __report_avg; |
440 | static u64 __report_allowed; | |
441 | ||
6a02ad66 | 442 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 443 | { |
6a02ad66 | 444 | printk_ratelimited(KERN_WARNING |
91a612ee PZ |
445 | "perf: interrupt took too long (%lld > %lld), lowering " |
446 | "kernel.perf_event_max_sample_rate to %d\n", | |
447 | __report_avg, __report_allowed, | |
448 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
449 | } |
450 | ||
451 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
452 | ||
453 | void perf_sample_event_took(u64 sample_len_ns) | |
454 | { | |
91a612ee PZ |
455 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
456 | u64 running_len; | |
457 | u64 avg_len; | |
458 | u32 max; | |
14c63f17 | 459 | |
91a612ee | 460 | if (max_len == 0) |
14c63f17 DH |
461 | return; |
462 | ||
91a612ee PZ |
463 | /* Decay the counter by 1 average sample. */ |
464 | running_len = __this_cpu_read(running_sample_length); | |
465 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
466 | running_len += sample_len_ns; | |
467 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
468 | |
469 | /* | |
91a612ee PZ |
470 | * Note: this will be biased artifically low until we have |
471 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
472 | * from having to maintain a count. |
473 | */ | |
91a612ee PZ |
474 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
475 | if (avg_len <= max_len) | |
14c63f17 DH |
476 | return; |
477 | ||
91a612ee PZ |
478 | __report_avg = avg_len; |
479 | __report_allowed = max_len; | |
14c63f17 | 480 | |
91a612ee PZ |
481 | /* |
482 | * Compute a throttle threshold 25% below the current duration. | |
483 | */ | |
484 | avg_len += avg_len / 4; | |
485 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
486 | if (avg_len < max) | |
487 | max /= (u32)avg_len; | |
488 | else | |
489 | max = 1; | |
14c63f17 | 490 | |
91a612ee PZ |
491 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
492 | WRITE_ONCE(max_samples_per_tick, max); | |
493 | ||
494 | sysctl_perf_event_sample_rate = max * HZ; | |
495 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 496 | |
cd578abb | 497 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 498 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 499 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 500 | __report_avg, __report_allowed, |
cd578abb PZ |
501 | sysctl_perf_event_sample_rate); |
502 | } | |
14c63f17 DH |
503 | } |
504 | ||
cdd6c482 | 505 | static atomic64_t perf_event_id; |
a96bbc16 | 506 | |
0b3fcf17 SE |
507 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
508 | enum event_type_t event_type); | |
509 | ||
510 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
511 | enum event_type_t event_type, |
512 | struct task_struct *task); | |
513 | ||
514 | static void update_context_time(struct perf_event_context *ctx); | |
515 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 516 | |
cdd6c482 | 517 | void __weak perf_event_print_debug(void) { } |
0793a61d | 518 | |
84c79910 | 519 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 520 | { |
84c79910 | 521 | return "pmu"; |
0793a61d TG |
522 | } |
523 | ||
0b3fcf17 SE |
524 | static inline u64 perf_clock(void) |
525 | { | |
526 | return local_clock(); | |
527 | } | |
528 | ||
34f43927 PZ |
529 | static inline u64 perf_event_clock(struct perf_event *event) |
530 | { | |
531 | return event->clock(); | |
532 | } | |
533 | ||
e5d1367f SE |
534 | #ifdef CONFIG_CGROUP_PERF |
535 | ||
e5d1367f SE |
536 | static inline bool |
537 | perf_cgroup_match(struct perf_event *event) | |
538 | { | |
539 | struct perf_event_context *ctx = event->ctx; | |
540 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
541 | ||
ef824fa1 TH |
542 | /* @event doesn't care about cgroup */ |
543 | if (!event->cgrp) | |
544 | return true; | |
545 | ||
546 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
547 | if (!cpuctx->cgrp) | |
548 | return false; | |
549 | ||
550 | /* | |
551 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
552 | * also enabled for all its descendant cgroups. If @cpuctx's | |
553 | * cgroup is a descendant of @event's (the test covers identity | |
554 | * case), it's a match. | |
555 | */ | |
556 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
557 | event->cgrp->css.cgroup); | |
e5d1367f SE |
558 | } |
559 | ||
e5d1367f SE |
560 | static inline void perf_detach_cgroup(struct perf_event *event) |
561 | { | |
4e2ba650 | 562 | css_put(&event->cgrp->css); |
e5d1367f SE |
563 | event->cgrp = NULL; |
564 | } | |
565 | ||
566 | static inline int is_cgroup_event(struct perf_event *event) | |
567 | { | |
568 | return event->cgrp != NULL; | |
569 | } | |
570 | ||
571 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
572 | { | |
573 | struct perf_cgroup_info *t; | |
574 | ||
575 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
576 | return t->time; | |
577 | } | |
578 | ||
579 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
580 | { | |
581 | struct perf_cgroup_info *info; | |
582 | u64 now; | |
583 | ||
584 | now = perf_clock(); | |
585 | ||
586 | info = this_cpu_ptr(cgrp->info); | |
587 | ||
588 | info->time += now - info->timestamp; | |
589 | info->timestamp = now; | |
590 | } | |
591 | ||
592 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
593 | { | |
594 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
595 | if (cgrp_out) | |
596 | __update_cgrp_time(cgrp_out); | |
597 | } | |
598 | ||
599 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
600 | { | |
3f7cce3c SE |
601 | struct perf_cgroup *cgrp; |
602 | ||
e5d1367f | 603 | /* |
3f7cce3c SE |
604 | * ensure we access cgroup data only when needed and |
605 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 606 | */ |
3f7cce3c | 607 | if (!is_cgroup_event(event)) |
e5d1367f SE |
608 | return; |
609 | ||
614e4c4e | 610 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
611 | /* |
612 | * Do not update time when cgroup is not active | |
613 | */ | |
614 | if (cgrp == event->cgrp) | |
615 | __update_cgrp_time(event->cgrp); | |
e5d1367f SE |
616 | } |
617 | ||
618 | static inline void | |
3f7cce3c SE |
619 | perf_cgroup_set_timestamp(struct task_struct *task, |
620 | struct perf_event_context *ctx) | |
e5d1367f SE |
621 | { |
622 | struct perf_cgroup *cgrp; | |
623 | struct perf_cgroup_info *info; | |
624 | ||
3f7cce3c SE |
625 | /* |
626 | * ctx->lock held by caller | |
627 | * ensure we do not access cgroup data | |
628 | * unless we have the cgroup pinned (css_get) | |
629 | */ | |
630 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
631 | return; |
632 | ||
614e4c4e | 633 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 634 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 635 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
636 | } |
637 | ||
638 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ | |
639 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
640 | ||
641 | /* | |
642 | * reschedule events based on the cgroup constraint of task. | |
643 | * | |
644 | * mode SWOUT : schedule out everything | |
645 | * mode SWIN : schedule in based on cgroup for next | |
646 | */ | |
18ab2cd3 | 647 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
648 | { |
649 | struct perf_cpu_context *cpuctx; | |
650 | struct pmu *pmu; | |
651 | unsigned long flags; | |
652 | ||
653 | /* | |
654 | * disable interrupts to avoid geting nr_cgroup | |
655 | * changes via __perf_event_disable(). Also | |
656 | * avoids preemption. | |
657 | */ | |
658 | local_irq_save(flags); | |
659 | ||
660 | /* | |
661 | * we reschedule only in the presence of cgroup | |
662 | * constrained events. | |
663 | */ | |
e5d1367f SE |
664 | |
665 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
e5d1367f | 666 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95cf59ea PZ |
667 | if (cpuctx->unique_pmu != pmu) |
668 | continue; /* ensure we process each cpuctx once */ | |
e5d1367f | 669 | |
e5d1367f SE |
670 | /* |
671 | * perf_cgroup_events says at least one | |
672 | * context on this CPU has cgroup events. | |
673 | * | |
674 | * ctx->nr_cgroups reports the number of cgroup | |
675 | * events for a context. | |
676 | */ | |
677 | if (cpuctx->ctx.nr_cgroups > 0) { | |
facc4307 PZ |
678 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
679 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f SE |
680 | |
681 | if (mode & PERF_CGROUP_SWOUT) { | |
682 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
683 | /* | |
684 | * must not be done before ctxswout due | |
685 | * to event_filter_match() in event_sched_out() | |
686 | */ | |
687 | cpuctx->cgrp = NULL; | |
688 | } | |
689 | ||
690 | if (mode & PERF_CGROUP_SWIN) { | |
e566b76e | 691 | WARN_ON_ONCE(cpuctx->cgrp); |
95cf59ea PZ |
692 | /* |
693 | * set cgrp before ctxsw in to allow | |
694 | * event_filter_match() to not have to pass | |
695 | * task around | |
614e4c4e SE |
696 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
697 | * because cgorup events are only per-cpu | |
e5d1367f | 698 | */ |
614e4c4e | 699 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
e5d1367f SE |
700 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
701 | } | |
facc4307 PZ |
702 | perf_pmu_enable(cpuctx->ctx.pmu); |
703 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f | 704 | } |
e5d1367f SE |
705 | } |
706 | ||
e5d1367f SE |
707 | local_irq_restore(flags); |
708 | } | |
709 | ||
a8d757ef SE |
710 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
711 | struct task_struct *next) | |
e5d1367f | 712 | { |
a8d757ef SE |
713 | struct perf_cgroup *cgrp1; |
714 | struct perf_cgroup *cgrp2 = NULL; | |
715 | ||
ddaaf4e2 | 716 | rcu_read_lock(); |
a8d757ef SE |
717 | /* |
718 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
719 | * we do not need to pass the ctx here because we know |
720 | * we are holding the rcu lock | |
a8d757ef | 721 | */ |
614e4c4e | 722 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 723 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
724 | |
725 | /* | |
726 | * only schedule out current cgroup events if we know | |
727 | * that we are switching to a different cgroup. Otherwise, | |
728 | * do no touch the cgroup events. | |
729 | */ | |
730 | if (cgrp1 != cgrp2) | |
731 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
732 | |
733 | rcu_read_unlock(); | |
e5d1367f SE |
734 | } |
735 | ||
a8d757ef SE |
736 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
737 | struct task_struct *task) | |
e5d1367f | 738 | { |
a8d757ef SE |
739 | struct perf_cgroup *cgrp1; |
740 | struct perf_cgroup *cgrp2 = NULL; | |
741 | ||
ddaaf4e2 | 742 | rcu_read_lock(); |
a8d757ef SE |
743 | /* |
744 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
745 | * we do not need to pass the ctx here because we know |
746 | * we are holding the rcu lock | |
a8d757ef | 747 | */ |
614e4c4e | 748 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 749 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
750 | |
751 | /* | |
752 | * only need to schedule in cgroup events if we are changing | |
753 | * cgroup during ctxsw. Cgroup events were not scheduled | |
754 | * out of ctxsw out if that was not the case. | |
755 | */ | |
756 | if (cgrp1 != cgrp2) | |
757 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
758 | |
759 | rcu_read_unlock(); | |
e5d1367f SE |
760 | } |
761 | ||
762 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
763 | struct perf_event_attr *attr, | |
764 | struct perf_event *group_leader) | |
765 | { | |
766 | struct perf_cgroup *cgrp; | |
767 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
768 | struct fd f = fdget(fd); |
769 | int ret = 0; | |
e5d1367f | 770 | |
2903ff01 | 771 | if (!f.file) |
e5d1367f SE |
772 | return -EBADF; |
773 | ||
b583043e | 774 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 775 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
776 | if (IS_ERR(css)) { |
777 | ret = PTR_ERR(css); | |
778 | goto out; | |
779 | } | |
e5d1367f SE |
780 | |
781 | cgrp = container_of(css, struct perf_cgroup, css); | |
782 | event->cgrp = cgrp; | |
783 | ||
784 | /* | |
785 | * all events in a group must monitor | |
786 | * the same cgroup because a task belongs | |
787 | * to only one perf cgroup at a time | |
788 | */ | |
789 | if (group_leader && group_leader->cgrp != cgrp) { | |
790 | perf_detach_cgroup(event); | |
791 | ret = -EINVAL; | |
e5d1367f | 792 | } |
3db272c0 | 793 | out: |
2903ff01 | 794 | fdput(f); |
e5d1367f SE |
795 | return ret; |
796 | } | |
797 | ||
798 | static inline void | |
799 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
800 | { | |
801 | struct perf_cgroup_info *t; | |
802 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
803 | event->shadow_ctx_time = now - t->timestamp; | |
804 | } | |
805 | ||
806 | static inline void | |
807 | perf_cgroup_defer_enabled(struct perf_event *event) | |
808 | { | |
809 | /* | |
810 | * when the current task's perf cgroup does not match | |
811 | * the event's, we need to remember to call the | |
812 | * perf_mark_enable() function the first time a task with | |
813 | * a matching perf cgroup is scheduled in. | |
814 | */ | |
815 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) | |
816 | event->cgrp_defer_enabled = 1; | |
817 | } | |
818 | ||
819 | static inline void | |
820 | perf_cgroup_mark_enabled(struct perf_event *event, | |
821 | struct perf_event_context *ctx) | |
822 | { | |
823 | struct perf_event *sub; | |
824 | u64 tstamp = perf_event_time(event); | |
825 | ||
826 | if (!event->cgrp_defer_enabled) | |
827 | return; | |
828 | ||
829 | event->cgrp_defer_enabled = 0; | |
830 | ||
831 | event->tstamp_enabled = tstamp - event->total_time_enabled; | |
832 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
833 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { | |
834 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
835 | sub->cgrp_defer_enabled = 0; | |
836 | } | |
837 | } | |
838 | } | |
839 | #else /* !CONFIG_CGROUP_PERF */ | |
840 | ||
841 | static inline bool | |
842 | perf_cgroup_match(struct perf_event *event) | |
843 | { | |
844 | return true; | |
845 | } | |
846 | ||
847 | static inline void perf_detach_cgroup(struct perf_event *event) | |
848 | {} | |
849 | ||
850 | static inline int is_cgroup_event(struct perf_event *event) | |
851 | { | |
852 | return 0; | |
853 | } | |
854 | ||
855 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) | |
856 | { | |
857 | return 0; | |
858 | } | |
859 | ||
860 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
861 | { | |
862 | } | |
863 | ||
864 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
865 | { | |
866 | } | |
867 | ||
a8d757ef SE |
868 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
869 | struct task_struct *next) | |
e5d1367f SE |
870 | { |
871 | } | |
872 | ||
a8d757ef SE |
873 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
874 | struct task_struct *task) | |
e5d1367f SE |
875 | { |
876 | } | |
877 | ||
878 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
879 | struct perf_event_attr *attr, | |
880 | struct perf_event *group_leader) | |
881 | { | |
882 | return -EINVAL; | |
883 | } | |
884 | ||
885 | static inline void | |
3f7cce3c SE |
886 | perf_cgroup_set_timestamp(struct task_struct *task, |
887 | struct perf_event_context *ctx) | |
e5d1367f SE |
888 | { |
889 | } | |
890 | ||
891 | void | |
892 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
893 | { | |
894 | } | |
895 | ||
896 | static inline void | |
897 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
898 | { | |
899 | } | |
900 | ||
901 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
902 | { | |
903 | return 0; | |
904 | } | |
905 | ||
906 | static inline void | |
907 | perf_cgroup_defer_enabled(struct perf_event *event) | |
908 | { | |
909 | } | |
910 | ||
911 | static inline void | |
912 | perf_cgroup_mark_enabled(struct perf_event *event, | |
913 | struct perf_event_context *ctx) | |
914 | { | |
915 | } | |
916 | #endif | |
917 | ||
9e630205 SE |
918 | /* |
919 | * set default to be dependent on timer tick just | |
920 | * like original code | |
921 | */ | |
922 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
923 | /* | |
924 | * function must be called with interrupts disbled | |
925 | */ | |
272325c4 | 926 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
927 | { |
928 | struct perf_cpu_context *cpuctx; | |
9e630205 SE |
929 | int rotations = 0; |
930 | ||
931 | WARN_ON(!irqs_disabled()); | |
932 | ||
933 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
934 | rotations = perf_rotate_context(cpuctx); |
935 | ||
4cfafd30 PZ |
936 | raw_spin_lock(&cpuctx->hrtimer_lock); |
937 | if (rotations) | |
9e630205 | 938 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
939 | else |
940 | cpuctx->hrtimer_active = 0; | |
941 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 942 | |
4cfafd30 | 943 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
944 | } |
945 | ||
272325c4 | 946 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 947 | { |
272325c4 | 948 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 949 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 950 | u64 interval; |
9e630205 SE |
951 | |
952 | /* no multiplexing needed for SW PMU */ | |
953 | if (pmu->task_ctx_nr == perf_sw_context) | |
954 | return; | |
955 | ||
62b85639 SE |
956 | /* |
957 | * check default is sane, if not set then force to | |
958 | * default interval (1/tick) | |
959 | */ | |
272325c4 PZ |
960 | interval = pmu->hrtimer_interval_ms; |
961 | if (interval < 1) | |
962 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 963 | |
272325c4 | 964 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 965 | |
4cfafd30 PZ |
966 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
967 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 968 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
969 | } |
970 | ||
272325c4 | 971 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 972 | { |
272325c4 | 973 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 974 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 975 | unsigned long flags; |
9e630205 SE |
976 | |
977 | /* not for SW PMU */ | |
978 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 979 | return 0; |
9e630205 | 980 | |
4cfafd30 PZ |
981 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
982 | if (!cpuctx->hrtimer_active) { | |
983 | cpuctx->hrtimer_active = 1; | |
984 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
985 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
986 | } | |
987 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 988 | |
272325c4 | 989 | return 0; |
9e630205 SE |
990 | } |
991 | ||
33696fc0 | 992 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 993 | { |
33696fc0 PZ |
994 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
995 | if (!(*count)++) | |
996 | pmu->pmu_disable(pmu); | |
9e35ad38 | 997 | } |
9e35ad38 | 998 | |
33696fc0 | 999 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1000 | { |
33696fc0 PZ |
1001 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1002 | if (!--(*count)) | |
1003 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1004 | } |
9e35ad38 | 1005 | |
2fde4f94 | 1006 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1007 | |
1008 | /* | |
2fde4f94 MR |
1009 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1010 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1011 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1012 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1013 | */ |
2fde4f94 | 1014 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1015 | { |
2fde4f94 | 1016 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1017 | |
e9d2b064 | 1018 | WARN_ON(!irqs_disabled()); |
b5ab4cd5 | 1019 | |
2fde4f94 MR |
1020 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1021 | ||
1022 | list_add(&ctx->active_ctx_list, head); | |
1023 | } | |
1024 | ||
1025 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1026 | { | |
1027 | WARN_ON(!irqs_disabled()); | |
1028 | ||
1029 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1030 | ||
1031 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1032 | } |
9e35ad38 | 1033 | |
cdd6c482 | 1034 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1035 | { |
e5289d4a | 1036 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1037 | } |
1038 | ||
4af57ef2 YZ |
1039 | static void free_ctx(struct rcu_head *head) |
1040 | { | |
1041 | struct perf_event_context *ctx; | |
1042 | ||
1043 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1044 | kfree(ctx->task_ctx_data); | |
1045 | kfree(ctx); | |
1046 | } | |
1047 | ||
cdd6c482 | 1048 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1049 | { |
564c2b21 PM |
1050 | if (atomic_dec_and_test(&ctx->refcount)) { |
1051 | if (ctx->parent_ctx) | |
1052 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1053 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1054 | put_task_struct(ctx->task); |
4af57ef2 | 1055 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1056 | } |
a63eaf34 PM |
1057 | } |
1058 | ||
f63a8daa PZ |
1059 | /* |
1060 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1061 | * perf_pmu_migrate_context() we need some magic. | |
1062 | * | |
1063 | * Those places that change perf_event::ctx will hold both | |
1064 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1065 | * | |
8b10c5e2 PZ |
1066 | * Lock ordering is by mutex address. There are two other sites where |
1067 | * perf_event_context::mutex nests and those are: | |
1068 | * | |
1069 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1070 | * perf_event_exit_event() |
1071 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1072 | * |
1073 | * - perf_event_init_context() [ parent, 0 ] | |
1074 | * inherit_task_group() | |
1075 | * inherit_group() | |
1076 | * inherit_event() | |
1077 | * perf_event_alloc() | |
1078 | * perf_init_event() | |
1079 | * perf_try_init_event() [ child , 1 ] | |
1080 | * | |
1081 | * While it appears there is an obvious deadlock here -- the parent and child | |
1082 | * nesting levels are inverted between the two. This is in fact safe because | |
1083 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1084 | * spawning task cannot (yet) exit. | |
1085 | * | |
1086 | * But remember that that these are parent<->child context relations, and | |
1087 | * migration does not affect children, therefore these two orderings should not | |
1088 | * interact. | |
f63a8daa PZ |
1089 | * |
1090 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1091 | * because the sys_perf_event_open() case will install a new event and break | |
1092 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1093 | * concerned with cpuctx and that doesn't have children. | |
1094 | * | |
1095 | * The places that change perf_event::ctx will issue: | |
1096 | * | |
1097 | * perf_remove_from_context(); | |
1098 | * synchronize_rcu(); | |
1099 | * perf_install_in_context(); | |
1100 | * | |
1101 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1102 | * quiesce the event, after which we can install it in the new location. This | |
1103 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1104 | * while in transit. Therefore all such accessors should also acquire | |
1105 | * perf_event_context::mutex to serialize against this. | |
1106 | * | |
1107 | * However; because event->ctx can change while we're waiting to acquire | |
1108 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1109 | * function. | |
1110 | * | |
1111 | * Lock order: | |
79c9ce57 | 1112 | * cred_guard_mutex |
f63a8daa PZ |
1113 | * task_struct::perf_event_mutex |
1114 | * perf_event_context::mutex | |
f63a8daa | 1115 | * perf_event::child_mutex; |
07c4a776 | 1116 | * perf_event_context::lock |
f63a8daa PZ |
1117 | * perf_event::mmap_mutex |
1118 | * mmap_sem | |
1119 | */ | |
a83fe28e PZ |
1120 | static struct perf_event_context * |
1121 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1122 | { |
1123 | struct perf_event_context *ctx; | |
1124 | ||
1125 | again: | |
1126 | rcu_read_lock(); | |
1127 | ctx = ACCESS_ONCE(event->ctx); | |
1128 | if (!atomic_inc_not_zero(&ctx->refcount)) { | |
1129 | rcu_read_unlock(); | |
1130 | goto again; | |
1131 | } | |
1132 | rcu_read_unlock(); | |
1133 | ||
a83fe28e | 1134 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1135 | if (event->ctx != ctx) { |
1136 | mutex_unlock(&ctx->mutex); | |
1137 | put_ctx(ctx); | |
1138 | goto again; | |
1139 | } | |
1140 | ||
1141 | return ctx; | |
1142 | } | |
1143 | ||
a83fe28e PZ |
1144 | static inline struct perf_event_context * |
1145 | perf_event_ctx_lock(struct perf_event *event) | |
1146 | { | |
1147 | return perf_event_ctx_lock_nested(event, 0); | |
1148 | } | |
1149 | ||
f63a8daa PZ |
1150 | static void perf_event_ctx_unlock(struct perf_event *event, |
1151 | struct perf_event_context *ctx) | |
1152 | { | |
1153 | mutex_unlock(&ctx->mutex); | |
1154 | put_ctx(ctx); | |
1155 | } | |
1156 | ||
211de6eb PZ |
1157 | /* |
1158 | * This must be done under the ctx->lock, such as to serialize against | |
1159 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1160 | * calling scheduler related locks and ctx->lock nests inside those. | |
1161 | */ | |
1162 | static __must_check struct perf_event_context * | |
1163 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1164 | { |
211de6eb PZ |
1165 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1166 | ||
1167 | lockdep_assert_held(&ctx->lock); | |
1168 | ||
1169 | if (parent_ctx) | |
71a851b4 | 1170 | ctx->parent_ctx = NULL; |
5a3126d4 | 1171 | ctx->generation++; |
211de6eb PZ |
1172 | |
1173 | return parent_ctx; | |
71a851b4 PZ |
1174 | } |
1175 | ||
6844c09d ACM |
1176 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
1177 | { | |
1178 | /* | |
1179 | * only top level events have the pid namespace they were created in | |
1180 | */ | |
1181 | if (event->parent) | |
1182 | event = event->parent; | |
1183 | ||
1184 | return task_tgid_nr_ns(p, event->ns); | |
1185 | } | |
1186 | ||
1187 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) | |
1188 | { | |
1189 | /* | |
1190 | * only top level events have the pid namespace they were created in | |
1191 | */ | |
1192 | if (event->parent) | |
1193 | event = event->parent; | |
1194 | ||
1195 | return task_pid_nr_ns(p, event->ns); | |
1196 | } | |
1197 | ||
7f453c24 | 1198 | /* |
cdd6c482 | 1199 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1200 | * to userspace. |
1201 | */ | |
cdd6c482 | 1202 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1203 | { |
cdd6c482 | 1204 | u64 id = event->id; |
7f453c24 | 1205 | |
cdd6c482 IM |
1206 | if (event->parent) |
1207 | id = event->parent->id; | |
7f453c24 PZ |
1208 | |
1209 | return id; | |
1210 | } | |
1211 | ||
25346b93 | 1212 | /* |
cdd6c482 | 1213 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1214 | * |
25346b93 PM |
1215 | * This has to cope with with the fact that until it is locked, |
1216 | * the context could get moved to another task. | |
1217 | */ | |
cdd6c482 | 1218 | static struct perf_event_context * |
8dc85d54 | 1219 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1220 | { |
cdd6c482 | 1221 | struct perf_event_context *ctx; |
25346b93 | 1222 | |
9ed6060d | 1223 | retry: |
058ebd0e PZ |
1224 | /* |
1225 | * One of the few rules of preemptible RCU is that one cannot do | |
1226 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1227 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1228 | * rcu_read_unlock_special(). |
1229 | * | |
1230 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1231 | * side critical section has interrupts disabled. |
058ebd0e | 1232 | */ |
2fd59077 | 1233 | local_irq_save(*flags); |
058ebd0e | 1234 | rcu_read_lock(); |
8dc85d54 | 1235 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1236 | if (ctx) { |
1237 | /* | |
1238 | * If this context is a clone of another, it might | |
1239 | * get swapped for another underneath us by | |
cdd6c482 | 1240 | * perf_event_task_sched_out, though the |
25346b93 PM |
1241 | * rcu_read_lock() protects us from any context |
1242 | * getting freed. Lock the context and check if it | |
1243 | * got swapped before we could get the lock, and retry | |
1244 | * if so. If we locked the right context, then it | |
1245 | * can't get swapped on us any more. | |
1246 | */ | |
2fd59077 | 1247 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1248 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1249 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1250 | rcu_read_unlock(); |
2fd59077 | 1251 | local_irq_restore(*flags); |
25346b93 PM |
1252 | goto retry; |
1253 | } | |
b49a9e7e | 1254 | |
63b6da39 PZ |
1255 | if (ctx->task == TASK_TOMBSTONE || |
1256 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1257 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1258 | ctx = NULL; |
828b6f0e PZ |
1259 | } else { |
1260 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1261 | } |
25346b93 PM |
1262 | } |
1263 | rcu_read_unlock(); | |
2fd59077 PM |
1264 | if (!ctx) |
1265 | local_irq_restore(*flags); | |
25346b93 PM |
1266 | return ctx; |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * Get the context for a task and increment its pin_count so it | |
1271 | * can't get swapped to another task. This also increments its | |
1272 | * reference count so that the context can't get freed. | |
1273 | */ | |
8dc85d54 PZ |
1274 | static struct perf_event_context * |
1275 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1276 | { |
cdd6c482 | 1277 | struct perf_event_context *ctx; |
25346b93 PM |
1278 | unsigned long flags; |
1279 | ||
8dc85d54 | 1280 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1281 | if (ctx) { |
1282 | ++ctx->pin_count; | |
e625cce1 | 1283 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1284 | } |
1285 | return ctx; | |
1286 | } | |
1287 | ||
cdd6c482 | 1288 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1289 | { |
1290 | unsigned long flags; | |
1291 | ||
e625cce1 | 1292 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1293 | --ctx->pin_count; |
e625cce1 | 1294 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1295 | } |
1296 | ||
f67218c3 PZ |
1297 | /* |
1298 | * Update the record of the current time in a context. | |
1299 | */ | |
1300 | static void update_context_time(struct perf_event_context *ctx) | |
1301 | { | |
1302 | u64 now = perf_clock(); | |
1303 | ||
1304 | ctx->time += now - ctx->timestamp; | |
1305 | ctx->timestamp = now; | |
1306 | } | |
1307 | ||
4158755d SE |
1308 | static u64 perf_event_time(struct perf_event *event) |
1309 | { | |
1310 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1311 | |
1312 | if (is_cgroup_event(event)) | |
1313 | return perf_cgroup_event_time(event); | |
1314 | ||
4158755d SE |
1315 | return ctx ? ctx->time : 0; |
1316 | } | |
1317 | ||
f67218c3 PZ |
1318 | /* |
1319 | * Update the total_time_enabled and total_time_running fields for a event. | |
1320 | */ | |
1321 | static void update_event_times(struct perf_event *event) | |
1322 | { | |
1323 | struct perf_event_context *ctx = event->ctx; | |
1324 | u64 run_end; | |
1325 | ||
3cbaa590 PZ |
1326 | lockdep_assert_held(&ctx->lock); |
1327 | ||
f67218c3 PZ |
1328 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
1329 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) | |
1330 | return; | |
3cbaa590 | 1331 | |
e5d1367f SE |
1332 | /* |
1333 | * in cgroup mode, time_enabled represents | |
1334 | * the time the event was enabled AND active | |
1335 | * tasks were in the monitored cgroup. This is | |
1336 | * independent of the activity of the context as | |
1337 | * there may be a mix of cgroup and non-cgroup events. | |
1338 | * | |
1339 | * That is why we treat cgroup events differently | |
1340 | * here. | |
1341 | */ | |
1342 | if (is_cgroup_event(event)) | |
46cd6a7f | 1343 | run_end = perf_cgroup_event_time(event); |
e5d1367f SE |
1344 | else if (ctx->is_active) |
1345 | run_end = ctx->time; | |
acd1d7c1 PZ |
1346 | else |
1347 | run_end = event->tstamp_stopped; | |
1348 | ||
1349 | event->total_time_enabled = run_end - event->tstamp_enabled; | |
f67218c3 PZ |
1350 | |
1351 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
1352 | run_end = event->tstamp_stopped; | |
1353 | else | |
4158755d | 1354 | run_end = perf_event_time(event); |
f67218c3 PZ |
1355 | |
1356 | event->total_time_running = run_end - event->tstamp_running; | |
e5d1367f | 1357 | |
f67218c3 PZ |
1358 | } |
1359 | ||
96c21a46 PZ |
1360 | /* |
1361 | * Update total_time_enabled and total_time_running for all events in a group. | |
1362 | */ | |
1363 | static void update_group_times(struct perf_event *leader) | |
1364 | { | |
1365 | struct perf_event *event; | |
1366 | ||
1367 | update_event_times(leader); | |
1368 | list_for_each_entry(event, &leader->sibling_list, group_entry) | |
1369 | update_event_times(event); | |
1370 | } | |
1371 | ||
889ff015 FW |
1372 | static struct list_head * |
1373 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) | |
1374 | { | |
1375 | if (event->attr.pinned) | |
1376 | return &ctx->pinned_groups; | |
1377 | else | |
1378 | return &ctx->flexible_groups; | |
1379 | } | |
1380 | ||
fccc714b | 1381 | /* |
cdd6c482 | 1382 | * Add a event from the lists for its context. |
fccc714b PZ |
1383 | * Must be called with ctx->mutex and ctx->lock held. |
1384 | */ | |
04289bb9 | 1385 | static void |
cdd6c482 | 1386 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1387 | { |
c994d613 PZ |
1388 | lockdep_assert_held(&ctx->lock); |
1389 | ||
8a49542c PZ |
1390 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1391 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 IM |
1392 | |
1393 | /* | |
8a49542c PZ |
1394 | * If we're a stand alone event or group leader, we go to the context |
1395 | * list, group events are kept attached to the group so that | |
1396 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1397 | */ |
8a49542c | 1398 | if (event->group_leader == event) { |
889ff015 FW |
1399 | struct list_head *list; |
1400 | ||
d6f962b5 FW |
1401 | if (is_software_event(event)) |
1402 | event->group_flags |= PERF_GROUP_SOFTWARE; | |
1403 | ||
889ff015 FW |
1404 | list = ctx_group_list(event, ctx); |
1405 | list_add_tail(&event->group_entry, list); | |
5c148194 | 1406 | } |
592903cd | 1407 | |
08309379 | 1408 | if (is_cgroup_event(event)) |
e5d1367f | 1409 | ctx->nr_cgroups++; |
e5d1367f | 1410 | |
cdd6c482 IM |
1411 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1412 | ctx->nr_events++; | |
1413 | if (event->attr.inherit_stat) | |
bfbd3381 | 1414 | ctx->nr_stat++; |
5a3126d4 PZ |
1415 | |
1416 | ctx->generation++; | |
04289bb9 IM |
1417 | } |
1418 | ||
0231bb53 JO |
1419 | /* |
1420 | * Initialize event state based on the perf_event_attr::disabled. | |
1421 | */ | |
1422 | static inline void perf_event__state_init(struct perf_event *event) | |
1423 | { | |
1424 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1425 | PERF_EVENT_STATE_INACTIVE; | |
1426 | } | |
1427 | ||
a723968c | 1428 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1429 | { |
1430 | int entry = sizeof(u64); /* value */ | |
1431 | int size = 0; | |
1432 | int nr = 1; | |
1433 | ||
1434 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1435 | size += sizeof(u64); | |
1436 | ||
1437 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1438 | size += sizeof(u64); | |
1439 | ||
1440 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1441 | entry += sizeof(u64); | |
1442 | ||
1443 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1444 | nr += nr_siblings; |
c320c7b7 ACM |
1445 | size += sizeof(u64); |
1446 | } | |
1447 | ||
1448 | size += entry * nr; | |
1449 | event->read_size = size; | |
1450 | } | |
1451 | ||
a723968c | 1452 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1453 | { |
1454 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1455 | u16 size = 0; |
1456 | ||
c320c7b7 ACM |
1457 | if (sample_type & PERF_SAMPLE_IP) |
1458 | size += sizeof(data->ip); | |
1459 | ||
6844c09d ACM |
1460 | if (sample_type & PERF_SAMPLE_ADDR) |
1461 | size += sizeof(data->addr); | |
1462 | ||
1463 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1464 | size += sizeof(data->period); | |
1465 | ||
c3feedf2 AK |
1466 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1467 | size += sizeof(data->weight); | |
1468 | ||
6844c09d ACM |
1469 | if (sample_type & PERF_SAMPLE_READ) |
1470 | size += event->read_size; | |
1471 | ||
d6be9ad6 SE |
1472 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1473 | size += sizeof(data->data_src.val); | |
1474 | ||
fdfbbd07 AK |
1475 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1476 | size += sizeof(data->txn); | |
1477 | ||
6844c09d ACM |
1478 | event->header_size = size; |
1479 | } | |
1480 | ||
a723968c PZ |
1481 | /* |
1482 | * Called at perf_event creation and when events are attached/detached from a | |
1483 | * group. | |
1484 | */ | |
1485 | static void perf_event__header_size(struct perf_event *event) | |
1486 | { | |
1487 | __perf_event_read_size(event, | |
1488 | event->group_leader->nr_siblings); | |
1489 | __perf_event_header_size(event, event->attr.sample_type); | |
1490 | } | |
1491 | ||
6844c09d ACM |
1492 | static void perf_event__id_header_size(struct perf_event *event) |
1493 | { | |
1494 | struct perf_sample_data *data; | |
1495 | u64 sample_type = event->attr.sample_type; | |
1496 | u16 size = 0; | |
1497 | ||
c320c7b7 ACM |
1498 | if (sample_type & PERF_SAMPLE_TID) |
1499 | size += sizeof(data->tid_entry); | |
1500 | ||
1501 | if (sample_type & PERF_SAMPLE_TIME) | |
1502 | size += sizeof(data->time); | |
1503 | ||
ff3d527c AH |
1504 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1505 | size += sizeof(data->id); | |
1506 | ||
c320c7b7 ACM |
1507 | if (sample_type & PERF_SAMPLE_ID) |
1508 | size += sizeof(data->id); | |
1509 | ||
1510 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1511 | size += sizeof(data->stream_id); | |
1512 | ||
1513 | if (sample_type & PERF_SAMPLE_CPU) | |
1514 | size += sizeof(data->cpu_entry); | |
1515 | ||
6844c09d | 1516 | event->id_header_size = size; |
c320c7b7 ACM |
1517 | } |
1518 | ||
a723968c PZ |
1519 | static bool perf_event_validate_size(struct perf_event *event) |
1520 | { | |
1521 | /* | |
1522 | * The values computed here will be over-written when we actually | |
1523 | * attach the event. | |
1524 | */ | |
1525 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1526 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1527 | perf_event__id_header_size(event); | |
1528 | ||
1529 | /* | |
1530 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1531 | * Conservative limit to allow for callchains and other variable fields. | |
1532 | */ | |
1533 | if (event->read_size + event->header_size + | |
1534 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1535 | return false; | |
1536 | ||
1537 | return true; | |
1538 | } | |
1539 | ||
8a49542c PZ |
1540 | static void perf_group_attach(struct perf_event *event) |
1541 | { | |
c320c7b7 | 1542 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1543 | |
74c3337c PZ |
1544 | /* |
1545 | * We can have double attach due to group movement in perf_event_open. | |
1546 | */ | |
1547 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1548 | return; | |
1549 | ||
8a49542c PZ |
1550 | event->attach_state |= PERF_ATTACH_GROUP; |
1551 | ||
1552 | if (group_leader == event) | |
1553 | return; | |
1554 | ||
652884fe PZ |
1555 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1556 | ||
8a49542c PZ |
1557 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
1558 | !is_software_event(event)) | |
1559 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; | |
1560 | ||
1561 | list_add_tail(&event->group_entry, &group_leader->sibling_list); | |
1562 | group_leader->nr_siblings++; | |
c320c7b7 ACM |
1563 | |
1564 | perf_event__header_size(group_leader); | |
1565 | ||
1566 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) | |
1567 | perf_event__header_size(pos); | |
8a49542c PZ |
1568 | } |
1569 | ||
a63eaf34 | 1570 | /* |
cdd6c482 | 1571 | * Remove a event from the lists for its context. |
fccc714b | 1572 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1573 | */ |
04289bb9 | 1574 | static void |
cdd6c482 | 1575 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1576 | { |
68cacd29 | 1577 | struct perf_cpu_context *cpuctx; |
652884fe PZ |
1578 | |
1579 | WARN_ON_ONCE(event->ctx != ctx); | |
1580 | lockdep_assert_held(&ctx->lock); | |
1581 | ||
8a49542c PZ |
1582 | /* |
1583 | * We can have double detach due to exit/hot-unplug + close. | |
1584 | */ | |
1585 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1586 | return; |
8a49542c PZ |
1587 | |
1588 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1589 | ||
68cacd29 | 1590 | if (is_cgroup_event(event)) { |
e5d1367f | 1591 | ctx->nr_cgroups--; |
70a01657 PZ |
1592 | /* |
1593 | * Because cgroup events are always per-cpu events, this will | |
1594 | * always be called from the right CPU. | |
1595 | */ | |
68cacd29 SE |
1596 | cpuctx = __get_cpu_context(ctx); |
1597 | /* | |
70a01657 PZ |
1598 | * If there are no more cgroup events then clear cgrp to avoid |
1599 | * stale pointer in update_cgrp_time_from_cpuctx(). | |
68cacd29 SE |
1600 | */ |
1601 | if (!ctx->nr_cgroups) | |
1602 | cpuctx->cgrp = NULL; | |
1603 | } | |
e5d1367f | 1604 | |
cdd6c482 IM |
1605 | ctx->nr_events--; |
1606 | if (event->attr.inherit_stat) | |
bfbd3381 | 1607 | ctx->nr_stat--; |
8bc20959 | 1608 | |
cdd6c482 | 1609 | list_del_rcu(&event->event_entry); |
04289bb9 | 1610 | |
8a49542c PZ |
1611 | if (event->group_leader == event) |
1612 | list_del_init(&event->group_entry); | |
5c148194 | 1613 | |
96c21a46 | 1614 | update_group_times(event); |
b2e74a26 SE |
1615 | |
1616 | /* | |
1617 | * If event was in error state, then keep it | |
1618 | * that way, otherwise bogus counts will be | |
1619 | * returned on read(). The only way to get out | |
1620 | * of error state is by explicit re-enabling | |
1621 | * of the event | |
1622 | */ | |
1623 | if (event->state > PERF_EVENT_STATE_OFF) | |
1624 | event->state = PERF_EVENT_STATE_OFF; | |
5a3126d4 PZ |
1625 | |
1626 | ctx->generation++; | |
050735b0 PZ |
1627 | } |
1628 | ||
8a49542c | 1629 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1630 | { |
1631 | struct perf_event *sibling, *tmp; | |
8a49542c PZ |
1632 | struct list_head *list = NULL; |
1633 | ||
1634 | /* | |
1635 | * We can have double detach due to exit/hot-unplug + close. | |
1636 | */ | |
1637 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1638 | return; | |
1639 | ||
1640 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1641 | ||
1642 | /* | |
1643 | * If this is a sibling, remove it from its group. | |
1644 | */ | |
1645 | if (event->group_leader != event) { | |
1646 | list_del_init(&event->group_entry); | |
1647 | event->group_leader->nr_siblings--; | |
c320c7b7 | 1648 | goto out; |
8a49542c PZ |
1649 | } |
1650 | ||
1651 | if (!list_empty(&event->group_entry)) | |
1652 | list = &event->group_entry; | |
2e2af50b | 1653 | |
04289bb9 | 1654 | /* |
cdd6c482 IM |
1655 | * If this was a group event with sibling events then |
1656 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1657 | * to whatever list we are on. |
04289bb9 | 1658 | */ |
cdd6c482 | 1659 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
8a49542c PZ |
1660 | if (list) |
1661 | list_move_tail(&sibling->group_entry, list); | |
04289bb9 | 1662 | sibling->group_leader = sibling; |
d6f962b5 FW |
1663 | |
1664 | /* Inherit group flags from the previous leader */ | |
1665 | sibling->group_flags = event->group_flags; | |
652884fe PZ |
1666 | |
1667 | WARN_ON_ONCE(sibling->ctx != event->ctx); | |
04289bb9 | 1668 | } |
c320c7b7 ACM |
1669 | |
1670 | out: | |
1671 | perf_event__header_size(event->group_leader); | |
1672 | ||
1673 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) | |
1674 | perf_event__header_size(tmp); | |
04289bb9 IM |
1675 | } |
1676 | ||
fadfe7be JO |
1677 | static bool is_orphaned_event(struct perf_event *event) |
1678 | { | |
a69b0ca4 | 1679 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1680 | } |
1681 | ||
66eb579e MR |
1682 | static inline int pmu_filter_match(struct perf_event *event) |
1683 | { | |
1684 | struct pmu *pmu = event->pmu; | |
1685 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1686 | } | |
1687 | ||
fa66f07a SE |
1688 | static inline int |
1689 | event_filter_match(struct perf_event *event) | |
1690 | { | |
e5d1367f | 1691 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
66eb579e | 1692 | && perf_cgroup_match(event) && pmu_filter_match(event); |
fa66f07a SE |
1693 | } |
1694 | ||
9ffcfa6f SE |
1695 | static void |
1696 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1697 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1698 | struct perf_event_context *ctx) |
3b6f9e5c | 1699 | { |
4158755d | 1700 | u64 tstamp = perf_event_time(event); |
fa66f07a | 1701 | u64 delta; |
652884fe PZ |
1702 | |
1703 | WARN_ON_ONCE(event->ctx != ctx); | |
1704 | lockdep_assert_held(&ctx->lock); | |
1705 | ||
fa66f07a SE |
1706 | /* |
1707 | * An event which could not be activated because of | |
1708 | * filter mismatch still needs to have its timings | |
1709 | * maintained, otherwise bogus information is return | |
1710 | * via read() for time_enabled, time_running: | |
1711 | */ | |
1712 | if (event->state == PERF_EVENT_STATE_INACTIVE | |
1713 | && !event_filter_match(event)) { | |
e5d1367f | 1714 | delta = tstamp - event->tstamp_stopped; |
fa66f07a | 1715 | event->tstamp_running += delta; |
4158755d | 1716 | event->tstamp_stopped = tstamp; |
fa66f07a SE |
1717 | } |
1718 | ||
cdd6c482 | 1719 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1720 | return; |
3b6f9e5c | 1721 | |
44377277 AS |
1722 | perf_pmu_disable(event->pmu); |
1723 | ||
28a967c3 PZ |
1724 | event->tstamp_stopped = tstamp; |
1725 | event->pmu->del(event, 0); | |
1726 | event->oncpu = -1; | |
cdd6c482 IM |
1727 | event->state = PERF_EVENT_STATE_INACTIVE; |
1728 | if (event->pending_disable) { | |
1729 | event->pending_disable = 0; | |
1730 | event->state = PERF_EVENT_STATE_OFF; | |
970892a9 | 1731 | } |
3b6f9e5c | 1732 | |
cdd6c482 | 1733 | if (!is_software_event(event)) |
3b6f9e5c | 1734 | cpuctx->active_oncpu--; |
2fde4f94 MR |
1735 | if (!--ctx->nr_active) |
1736 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
1737 | if (event->attr.freq && event->attr.sample_freq) |
1738 | ctx->nr_freq--; | |
cdd6c482 | 1739 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 1740 | cpuctx->exclusive = 0; |
44377277 AS |
1741 | |
1742 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
1743 | } |
1744 | ||
d859e29f | 1745 | static void |
cdd6c482 | 1746 | group_sched_out(struct perf_event *group_event, |
d859e29f | 1747 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1748 | struct perf_event_context *ctx) |
d859e29f | 1749 | { |
cdd6c482 | 1750 | struct perf_event *event; |
fa66f07a | 1751 | int state = group_event->state; |
d859e29f | 1752 | |
cdd6c482 | 1753 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
1754 | |
1755 | /* | |
1756 | * Schedule out siblings (if any): | |
1757 | */ | |
cdd6c482 IM |
1758 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
1759 | event_sched_out(event, cpuctx, ctx); | |
d859e29f | 1760 | |
fa66f07a | 1761 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
d859e29f PM |
1762 | cpuctx->exclusive = 0; |
1763 | } | |
1764 | ||
45a0e07a | 1765 | #define DETACH_GROUP 0x01UL |
0017960f | 1766 | |
0793a61d | 1767 | /* |
cdd6c482 | 1768 | * Cross CPU call to remove a performance event |
0793a61d | 1769 | * |
cdd6c482 | 1770 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
1771 | * remove it from the context list. |
1772 | */ | |
fae3fde6 PZ |
1773 | static void |
1774 | __perf_remove_from_context(struct perf_event *event, | |
1775 | struct perf_cpu_context *cpuctx, | |
1776 | struct perf_event_context *ctx, | |
1777 | void *info) | |
0793a61d | 1778 | { |
45a0e07a | 1779 | unsigned long flags = (unsigned long)info; |
0793a61d | 1780 | |
cdd6c482 | 1781 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 1782 | if (flags & DETACH_GROUP) |
46ce0fe9 | 1783 | perf_group_detach(event); |
cdd6c482 | 1784 | list_del_event(event, ctx); |
39a43640 PZ |
1785 | |
1786 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 1787 | ctx->is_active = 0; |
39a43640 PZ |
1788 | if (ctx->task) { |
1789 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
1790 | cpuctx->task_ctx = NULL; | |
1791 | } | |
64ce3126 | 1792 | } |
0793a61d TG |
1793 | } |
1794 | ||
0793a61d | 1795 | /* |
cdd6c482 | 1796 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 1797 | * |
cdd6c482 IM |
1798 | * If event->ctx is a cloned context, callers must make sure that |
1799 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
1800 | * remains valid. This is OK when called from perf_release since |
1801 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 1802 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 1803 | * context has been detached from its task. |
0793a61d | 1804 | */ |
45a0e07a | 1805 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 1806 | { |
fae3fde6 | 1807 | lockdep_assert_held(&event->ctx->mutex); |
0793a61d | 1808 | |
45a0e07a | 1809 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
0793a61d TG |
1810 | } |
1811 | ||
d859e29f | 1812 | /* |
cdd6c482 | 1813 | * Cross CPU call to disable a performance event |
d859e29f | 1814 | */ |
fae3fde6 PZ |
1815 | static void __perf_event_disable(struct perf_event *event, |
1816 | struct perf_cpu_context *cpuctx, | |
1817 | struct perf_event_context *ctx, | |
1818 | void *info) | |
7b648018 | 1819 | { |
fae3fde6 PZ |
1820 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
1821 | return; | |
7b648018 | 1822 | |
fae3fde6 PZ |
1823 | update_context_time(ctx); |
1824 | update_cgrp_time_from_event(event); | |
1825 | update_group_times(event); | |
1826 | if (event == event->group_leader) | |
1827 | group_sched_out(event, cpuctx, ctx); | |
1828 | else | |
1829 | event_sched_out(event, cpuctx, ctx); | |
1830 | event->state = PERF_EVENT_STATE_OFF; | |
7b648018 PZ |
1831 | } |
1832 | ||
d859e29f | 1833 | /* |
cdd6c482 | 1834 | * Disable a event. |
c93f7669 | 1835 | * |
cdd6c482 IM |
1836 | * If event->ctx is a cloned context, callers must make sure that |
1837 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 1838 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
1839 | * perf_event_for_each_child or perf_event_for_each because they |
1840 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
1841 | * goes to exit will block in perf_event_exit_event(). |
1842 | * | |
cdd6c482 | 1843 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 1844 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 1845 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 1846 | */ |
f63a8daa | 1847 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 1848 | { |
cdd6c482 | 1849 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 1850 | |
e625cce1 | 1851 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 1852 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 1853 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1854 | return; |
53cfbf59 | 1855 | } |
e625cce1 | 1856 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1857 | |
fae3fde6 PZ |
1858 | event_function_call(event, __perf_event_disable, NULL); |
1859 | } | |
1860 | ||
1861 | void perf_event_disable_local(struct perf_event *event) | |
1862 | { | |
1863 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 1864 | } |
f63a8daa PZ |
1865 | |
1866 | /* | |
1867 | * Strictly speaking kernel users cannot create groups and therefore this | |
1868 | * interface does not need the perf_event_ctx_lock() magic. | |
1869 | */ | |
1870 | void perf_event_disable(struct perf_event *event) | |
1871 | { | |
1872 | struct perf_event_context *ctx; | |
1873 | ||
1874 | ctx = perf_event_ctx_lock(event); | |
1875 | _perf_event_disable(event); | |
1876 | perf_event_ctx_unlock(event, ctx); | |
1877 | } | |
dcfce4a0 | 1878 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 1879 | |
e5d1367f SE |
1880 | static void perf_set_shadow_time(struct perf_event *event, |
1881 | struct perf_event_context *ctx, | |
1882 | u64 tstamp) | |
1883 | { | |
1884 | /* | |
1885 | * use the correct time source for the time snapshot | |
1886 | * | |
1887 | * We could get by without this by leveraging the | |
1888 | * fact that to get to this function, the caller | |
1889 | * has most likely already called update_context_time() | |
1890 | * and update_cgrp_time_xx() and thus both timestamp | |
1891 | * are identical (or very close). Given that tstamp is, | |
1892 | * already adjusted for cgroup, we could say that: | |
1893 | * tstamp - ctx->timestamp | |
1894 | * is equivalent to | |
1895 | * tstamp - cgrp->timestamp. | |
1896 | * | |
1897 | * Then, in perf_output_read(), the calculation would | |
1898 | * work with no changes because: | |
1899 | * - event is guaranteed scheduled in | |
1900 | * - no scheduled out in between | |
1901 | * - thus the timestamp would be the same | |
1902 | * | |
1903 | * But this is a bit hairy. | |
1904 | * | |
1905 | * So instead, we have an explicit cgroup call to remain | |
1906 | * within the time time source all along. We believe it | |
1907 | * is cleaner and simpler to understand. | |
1908 | */ | |
1909 | if (is_cgroup_event(event)) | |
1910 | perf_cgroup_set_shadow_time(event, tstamp); | |
1911 | else | |
1912 | event->shadow_ctx_time = tstamp - ctx->timestamp; | |
1913 | } | |
1914 | ||
4fe757dd PZ |
1915 | #define MAX_INTERRUPTS (~0ULL) |
1916 | ||
1917 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 1918 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 1919 | |
235c7fc7 | 1920 | static int |
9ffcfa6f | 1921 | event_sched_in(struct perf_event *event, |
235c7fc7 | 1922 | struct perf_cpu_context *cpuctx, |
6e37738a | 1923 | struct perf_event_context *ctx) |
235c7fc7 | 1924 | { |
4158755d | 1925 | u64 tstamp = perf_event_time(event); |
44377277 | 1926 | int ret = 0; |
4158755d | 1927 | |
63342411 PZ |
1928 | lockdep_assert_held(&ctx->lock); |
1929 | ||
cdd6c482 | 1930 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
1931 | return 0; |
1932 | ||
95ff4ca2 AS |
1933 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
1934 | /* | |
1935 | * Order event::oncpu write to happen before the ACTIVE state | |
1936 | * is visible. | |
1937 | */ | |
1938 | smp_wmb(); | |
1939 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); | |
4fe757dd PZ |
1940 | |
1941 | /* | |
1942 | * Unthrottle events, since we scheduled we might have missed several | |
1943 | * ticks already, also for a heavily scheduling task there is little | |
1944 | * guarantee it'll get a tick in a timely manner. | |
1945 | */ | |
1946 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
1947 | perf_log_throttle(event, 1); | |
1948 | event->hw.interrupts = 0; | |
1949 | } | |
1950 | ||
235c7fc7 IM |
1951 | /* |
1952 | * The new state must be visible before we turn it on in the hardware: | |
1953 | */ | |
1954 | smp_wmb(); | |
1955 | ||
44377277 AS |
1956 | perf_pmu_disable(event->pmu); |
1957 | ||
72f669c0 SL |
1958 | perf_set_shadow_time(event, ctx, tstamp); |
1959 | ||
ec0d7729 AS |
1960 | perf_log_itrace_start(event); |
1961 | ||
a4eaf7f1 | 1962 | if (event->pmu->add(event, PERF_EF_START)) { |
cdd6c482 IM |
1963 | event->state = PERF_EVENT_STATE_INACTIVE; |
1964 | event->oncpu = -1; | |
44377277 AS |
1965 | ret = -EAGAIN; |
1966 | goto out; | |
235c7fc7 IM |
1967 | } |
1968 | ||
00a2916f PZ |
1969 | event->tstamp_running += tstamp - event->tstamp_stopped; |
1970 | ||
cdd6c482 | 1971 | if (!is_software_event(event)) |
3b6f9e5c | 1972 | cpuctx->active_oncpu++; |
2fde4f94 MR |
1973 | if (!ctx->nr_active++) |
1974 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
1975 | if (event->attr.freq && event->attr.sample_freq) |
1976 | ctx->nr_freq++; | |
235c7fc7 | 1977 | |
cdd6c482 | 1978 | if (event->attr.exclusive) |
3b6f9e5c PM |
1979 | cpuctx->exclusive = 1; |
1980 | ||
44377277 AS |
1981 | out: |
1982 | perf_pmu_enable(event->pmu); | |
1983 | ||
1984 | return ret; | |
235c7fc7 IM |
1985 | } |
1986 | ||
6751b71e | 1987 | static int |
cdd6c482 | 1988 | group_sched_in(struct perf_event *group_event, |
6751b71e | 1989 | struct perf_cpu_context *cpuctx, |
6e37738a | 1990 | struct perf_event_context *ctx) |
6751b71e | 1991 | { |
6bde9b6c | 1992 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 1993 | struct pmu *pmu = ctx->pmu; |
d7842da4 SE |
1994 | u64 now = ctx->time; |
1995 | bool simulate = false; | |
6751b71e | 1996 | |
cdd6c482 | 1997 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
1998 | return 0; |
1999 | ||
fbbe0701 | 2000 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2001 | |
9ffcfa6f | 2002 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2003 | pmu->cancel_txn(pmu); |
272325c4 | 2004 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2005 | return -EAGAIN; |
90151c35 | 2006 | } |
6751b71e PM |
2007 | |
2008 | /* | |
2009 | * Schedule in siblings as one group (if any): | |
2010 | */ | |
cdd6c482 | 2011 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
9ffcfa6f | 2012 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2013 | partial_group = event; |
6751b71e PM |
2014 | goto group_error; |
2015 | } | |
2016 | } | |
2017 | ||
9ffcfa6f | 2018 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2019 | return 0; |
9ffcfa6f | 2020 | |
6751b71e PM |
2021 | group_error: |
2022 | /* | |
2023 | * Groups can be scheduled in as one unit only, so undo any | |
2024 | * partial group before returning: | |
d7842da4 SE |
2025 | * The events up to the failed event are scheduled out normally, |
2026 | * tstamp_stopped will be updated. | |
2027 | * | |
2028 | * The failed events and the remaining siblings need to have | |
2029 | * their timings updated as if they had gone thru event_sched_in() | |
2030 | * and event_sched_out(). This is required to get consistent timings | |
2031 | * across the group. This also takes care of the case where the group | |
2032 | * could never be scheduled by ensuring tstamp_stopped is set to mark | |
2033 | * the time the event was actually stopped, such that time delta | |
2034 | * calculation in update_event_times() is correct. | |
6751b71e | 2035 | */ |
cdd6c482 IM |
2036 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
2037 | if (event == partial_group) | |
d7842da4 SE |
2038 | simulate = true; |
2039 | ||
2040 | if (simulate) { | |
2041 | event->tstamp_running += now - event->tstamp_stopped; | |
2042 | event->tstamp_stopped = now; | |
2043 | } else { | |
2044 | event_sched_out(event, cpuctx, ctx); | |
2045 | } | |
6751b71e | 2046 | } |
9ffcfa6f | 2047 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2048 | |
ad5133b7 | 2049 | pmu->cancel_txn(pmu); |
90151c35 | 2050 | |
272325c4 | 2051 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2052 | |
6751b71e PM |
2053 | return -EAGAIN; |
2054 | } | |
2055 | ||
3b6f9e5c | 2056 | /* |
cdd6c482 | 2057 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2058 | */ |
cdd6c482 | 2059 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2060 | struct perf_cpu_context *cpuctx, |
2061 | int can_add_hw) | |
2062 | { | |
2063 | /* | |
cdd6c482 | 2064 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2065 | */ |
d6f962b5 | 2066 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
3b6f9e5c PM |
2067 | return 1; |
2068 | /* | |
2069 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2070 | * events can go on. |
3b6f9e5c PM |
2071 | */ |
2072 | if (cpuctx->exclusive) | |
2073 | return 0; | |
2074 | /* | |
2075 | * If this group is exclusive and there are already | |
cdd6c482 | 2076 | * events on the CPU, it can't go on. |
3b6f9e5c | 2077 | */ |
cdd6c482 | 2078 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2079 | return 0; |
2080 | /* | |
2081 | * Otherwise, try to add it if all previous groups were able | |
2082 | * to go on. | |
2083 | */ | |
2084 | return can_add_hw; | |
2085 | } | |
2086 | ||
cdd6c482 IM |
2087 | static void add_event_to_ctx(struct perf_event *event, |
2088 | struct perf_event_context *ctx) | |
53cfbf59 | 2089 | { |
4158755d SE |
2090 | u64 tstamp = perf_event_time(event); |
2091 | ||
cdd6c482 | 2092 | list_add_event(event, ctx); |
8a49542c | 2093 | perf_group_attach(event); |
4158755d SE |
2094 | event->tstamp_enabled = tstamp; |
2095 | event->tstamp_running = tstamp; | |
2096 | event->tstamp_stopped = tstamp; | |
53cfbf59 PM |
2097 | } |
2098 | ||
bd2afa49 PZ |
2099 | static void ctx_sched_out(struct perf_event_context *ctx, |
2100 | struct perf_cpu_context *cpuctx, | |
2101 | enum event_type_t event_type); | |
2c29ef0f PZ |
2102 | static void |
2103 | ctx_sched_in(struct perf_event_context *ctx, | |
2104 | struct perf_cpu_context *cpuctx, | |
2105 | enum event_type_t event_type, | |
2106 | struct task_struct *task); | |
fe4b04fa | 2107 | |
bd2afa49 PZ |
2108 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
2109 | struct perf_event_context *ctx) | |
2110 | { | |
2111 | if (!cpuctx->task_ctx) | |
2112 | return; | |
2113 | ||
2114 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2115 | return; | |
2116 | ||
2117 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); | |
2118 | } | |
2119 | ||
dce5855b PZ |
2120 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2121 | struct perf_event_context *ctx, | |
2122 | struct task_struct *task) | |
2123 | { | |
2124 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2125 | if (ctx) | |
2126 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2127 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2128 | if (ctx) | |
2129 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2130 | } | |
2131 | ||
3e349507 PZ |
2132 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
2133 | struct perf_event_context *task_ctx) | |
0017960f | 2134 | { |
3e349507 PZ |
2135 | perf_pmu_disable(cpuctx->ctx.pmu); |
2136 | if (task_ctx) | |
2137 | task_ctx_sched_out(cpuctx, task_ctx); | |
2138 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
2139 | perf_event_sched_in(cpuctx, task_ctx, current); | |
2140 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2141 | } |
2142 | ||
0793a61d | 2143 | /* |
cdd6c482 | 2144 | * Cross CPU call to install and enable a performance event |
682076ae | 2145 | * |
a096309b PZ |
2146 | * Very similar to remote_function() + event_function() but cannot assume that |
2147 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2148 | */ |
fe4b04fa | 2149 | static int __perf_install_in_context(void *info) |
0793a61d | 2150 | { |
a096309b PZ |
2151 | struct perf_event *event = info; |
2152 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2153 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2154 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
a096309b PZ |
2155 | bool activate = true; |
2156 | int ret = 0; | |
0793a61d | 2157 | |
63b6da39 | 2158 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2159 | if (ctx->task) { |
b58f6b0d PZ |
2160 | raw_spin_lock(&ctx->lock); |
2161 | task_ctx = ctx; | |
a096309b PZ |
2162 | |
2163 | /* If we're on the wrong CPU, try again */ | |
2164 | if (task_cpu(ctx->task) != smp_processor_id()) { | |
2165 | ret = -ESRCH; | |
63b6da39 | 2166 | goto unlock; |
a096309b | 2167 | } |
b58f6b0d | 2168 | |
39a43640 | 2169 | /* |
a096309b PZ |
2170 | * If we're on the right CPU, see if the task we target is |
2171 | * current, if not we don't have to activate the ctx, a future | |
2172 | * context switch will do that for us. | |
39a43640 | 2173 | */ |
a096309b PZ |
2174 | if (ctx->task != current) |
2175 | activate = false; | |
2176 | else | |
2177 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); | |
2178 | ||
63b6da39 PZ |
2179 | } else if (task_ctx) { |
2180 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2181 | } |
b58f6b0d | 2182 | |
a096309b PZ |
2183 | if (activate) { |
2184 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2185 | add_event_to_ctx(event, ctx); | |
2186 | ctx_resched(cpuctx, task_ctx); | |
2187 | } else { | |
2188 | add_event_to_ctx(event, ctx); | |
2189 | } | |
2190 | ||
63b6da39 | 2191 | unlock: |
2c29ef0f | 2192 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2193 | |
a096309b | 2194 | return ret; |
0793a61d TG |
2195 | } |
2196 | ||
2197 | /* | |
a096309b PZ |
2198 | * Attach a performance event to a context. |
2199 | * | |
2200 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2201 | */ |
2202 | static void | |
cdd6c482 IM |
2203 | perf_install_in_context(struct perf_event_context *ctx, |
2204 | struct perf_event *event, | |
0793a61d TG |
2205 | int cpu) |
2206 | { | |
a096309b | 2207 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2208 | |
fe4b04fa PZ |
2209 | lockdep_assert_held(&ctx->mutex); |
2210 | ||
c3f00c70 | 2211 | event->ctx = ctx; |
0cda4c02 YZ |
2212 | if (event->cpu != -1) |
2213 | event->cpu = cpu; | |
c3f00c70 | 2214 | |
a096309b PZ |
2215 | if (!task) { |
2216 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2217 | return; | |
2218 | } | |
2219 | ||
2220 | /* | |
2221 | * Should not happen, we validate the ctx is still alive before calling. | |
2222 | */ | |
2223 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2224 | return; | |
2225 | ||
39a43640 PZ |
2226 | /* |
2227 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2228 | * to be set in case this is the nr_events 0 -> 1 transition. | |
39a43640 | 2229 | */ |
a096309b | 2230 | again: |
63b6da39 | 2231 | /* |
a096309b PZ |
2232 | * Cannot use task_function_call() because we need to run on the task's |
2233 | * CPU regardless of whether its current or not. | |
63b6da39 | 2234 | */ |
a096309b PZ |
2235 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
2236 | return; | |
2237 | ||
2238 | raw_spin_lock_irq(&ctx->lock); | |
2239 | task = ctx->task; | |
84c4e620 | 2240 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2241 | /* |
2242 | * Cannot happen because we already checked above (which also | |
2243 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2244 | * against perf_event_exit_task_context(). | |
2245 | */ | |
63b6da39 PZ |
2246 | raw_spin_unlock_irq(&ctx->lock); |
2247 | return; | |
2248 | } | |
39a43640 | 2249 | raw_spin_unlock_irq(&ctx->lock); |
39a43640 | 2250 | /* |
a096309b PZ |
2251 | * Since !ctx->is_active doesn't mean anything, we must IPI |
2252 | * unconditionally. | |
39a43640 | 2253 | */ |
a096309b | 2254 | goto again; |
0793a61d TG |
2255 | } |
2256 | ||
fa289bec | 2257 | /* |
cdd6c482 | 2258 | * Put a event into inactive state and update time fields. |
fa289bec PM |
2259 | * Enabling the leader of a group effectively enables all |
2260 | * the group members that aren't explicitly disabled, so we | |
2261 | * have to update their ->tstamp_enabled also. | |
2262 | * Note: this works for group members as well as group leaders | |
2263 | * since the non-leader members' sibling_lists will be empty. | |
2264 | */ | |
1d9b482e | 2265 | static void __perf_event_mark_enabled(struct perf_event *event) |
fa289bec | 2266 | { |
cdd6c482 | 2267 | struct perf_event *sub; |
4158755d | 2268 | u64 tstamp = perf_event_time(event); |
fa289bec | 2269 | |
cdd6c482 | 2270 | event->state = PERF_EVENT_STATE_INACTIVE; |
4158755d | 2271 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
9ed6060d | 2272 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
4158755d SE |
2273 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
2274 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
9ed6060d | 2275 | } |
fa289bec PM |
2276 | } |
2277 | ||
d859e29f | 2278 | /* |
cdd6c482 | 2279 | * Cross CPU call to enable a performance event |
d859e29f | 2280 | */ |
fae3fde6 PZ |
2281 | static void __perf_event_enable(struct perf_event *event, |
2282 | struct perf_cpu_context *cpuctx, | |
2283 | struct perf_event_context *ctx, | |
2284 | void *info) | |
04289bb9 | 2285 | { |
cdd6c482 | 2286 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2287 | struct perf_event_context *task_ctx; |
04289bb9 | 2288 | |
6e801e01 PZ |
2289 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2290 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2291 | return; |
3cbed429 | 2292 | |
bd2afa49 PZ |
2293 | if (ctx->is_active) |
2294 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2295 | ||
1d9b482e | 2296 | __perf_event_mark_enabled(event); |
04289bb9 | 2297 | |
fae3fde6 PZ |
2298 | if (!ctx->is_active) |
2299 | return; | |
2300 | ||
e5d1367f | 2301 | if (!event_filter_match(event)) { |
bd2afa49 | 2302 | if (is_cgroup_event(event)) |
e5d1367f | 2303 | perf_cgroup_defer_enabled(event); |
bd2afa49 | 2304 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2305 | return; |
e5d1367f | 2306 | } |
f4c4176f | 2307 | |
04289bb9 | 2308 | /* |
cdd6c482 | 2309 | * If the event is in a group and isn't the group leader, |
d859e29f | 2310 | * then don't put it on unless the group is on. |
04289bb9 | 2311 | */ |
bd2afa49 PZ |
2312 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2313 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2314 | return; |
bd2afa49 | 2315 | } |
fe4b04fa | 2316 | |
fae3fde6 PZ |
2317 | task_ctx = cpuctx->task_ctx; |
2318 | if (ctx->task) | |
2319 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2320 | |
fae3fde6 | 2321 | ctx_resched(cpuctx, task_ctx); |
7b648018 PZ |
2322 | } |
2323 | ||
d859e29f | 2324 | /* |
cdd6c482 | 2325 | * Enable a event. |
c93f7669 | 2326 | * |
cdd6c482 IM |
2327 | * If event->ctx is a cloned context, callers must make sure that |
2328 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2329 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2330 | * perf_event_for_each_child or perf_event_for_each as described |
2331 | * for perf_event_disable. | |
d859e29f | 2332 | */ |
f63a8daa | 2333 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2334 | { |
cdd6c482 | 2335 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2336 | |
7b648018 | 2337 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2338 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2339 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2340 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2341 | return; |
2342 | } | |
2343 | ||
d859e29f | 2344 | /* |
cdd6c482 | 2345 | * If the event is in error state, clear that first. |
7b648018 PZ |
2346 | * |
2347 | * That way, if we see the event in error state below, we know that it | |
2348 | * has gone back into error state, as distinct from the task having | |
2349 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2350 | */ |
cdd6c482 IM |
2351 | if (event->state == PERF_EVENT_STATE_ERROR) |
2352 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2353 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2354 | |
fae3fde6 | 2355 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2356 | } |
f63a8daa PZ |
2357 | |
2358 | /* | |
2359 | * See perf_event_disable(); | |
2360 | */ | |
2361 | void perf_event_enable(struct perf_event *event) | |
2362 | { | |
2363 | struct perf_event_context *ctx; | |
2364 | ||
2365 | ctx = perf_event_ctx_lock(event); | |
2366 | _perf_event_enable(event); | |
2367 | perf_event_ctx_unlock(event, ctx); | |
2368 | } | |
dcfce4a0 | 2369 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2370 | |
375637bc AS |
2371 | struct stop_event_data { |
2372 | struct perf_event *event; | |
2373 | unsigned int restart; | |
2374 | }; | |
2375 | ||
95ff4ca2 AS |
2376 | static int __perf_event_stop(void *info) |
2377 | { | |
375637bc AS |
2378 | struct stop_event_data *sd = info; |
2379 | struct perf_event *event = sd->event; | |
95ff4ca2 | 2380 | |
375637bc | 2381 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
2382 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
2383 | return 0; | |
2384 | ||
2385 | /* matches smp_wmb() in event_sched_in() */ | |
2386 | smp_rmb(); | |
2387 | ||
2388 | /* | |
2389 | * There is a window with interrupts enabled before we get here, | |
2390 | * so we need to check again lest we try to stop another CPU's event. | |
2391 | */ | |
2392 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2393 | return -EAGAIN; | |
2394 | ||
2395 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2396 | ||
375637bc AS |
2397 | /* |
2398 | * May race with the actual stop (through perf_pmu_output_stop()), | |
2399 | * but it is only used for events with AUX ring buffer, and such | |
2400 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
2401 | * see comments in perf_aux_output_begin(). | |
2402 | * | |
2403 | * Since this is happening on a event-local CPU, no trace is lost | |
2404 | * while restarting. | |
2405 | */ | |
2406 | if (sd->restart) | |
2407 | event->pmu->start(event, PERF_EF_START); | |
2408 | ||
95ff4ca2 AS |
2409 | return 0; |
2410 | } | |
2411 | ||
375637bc AS |
2412 | static int perf_event_restart(struct perf_event *event) |
2413 | { | |
2414 | struct stop_event_data sd = { | |
2415 | .event = event, | |
2416 | .restart = 1, | |
2417 | }; | |
2418 | int ret = 0; | |
2419 | ||
2420 | do { | |
2421 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2422 | return 0; | |
2423 | ||
2424 | /* matches smp_wmb() in event_sched_in() */ | |
2425 | smp_rmb(); | |
2426 | ||
2427 | /* | |
2428 | * We only want to restart ACTIVE events, so if the event goes | |
2429 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
2430 | * fall through with ret==-ENXIO. | |
2431 | */ | |
2432 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
2433 | __perf_event_stop, &sd); | |
2434 | } while (ret == -EAGAIN); | |
2435 | ||
2436 | return ret; | |
2437 | } | |
2438 | ||
2439 | /* | |
2440 | * In order to contain the amount of racy and tricky in the address filter | |
2441 | * configuration management, it is a two part process: | |
2442 | * | |
2443 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
2444 | * we update the addresses of corresponding vmas in | |
2445 | * event::addr_filters_offs array and bump the event::addr_filters_gen; | |
2446 | * (p2) when an event is scheduled in (pmu::add), it calls | |
2447 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
2448 | * if the generation has changed since the previous call. | |
2449 | * | |
2450 | * If (p1) happens while the event is active, we restart it to force (p2). | |
2451 | * | |
2452 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
2453 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
2454 | * ioctl; | |
2455 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
2456 | * registered mapping, called for every new mmap(), with mm::mmap_sem down | |
2457 | * for reading; | |
2458 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
2459 | * of exec. | |
2460 | */ | |
2461 | void perf_event_addr_filters_sync(struct perf_event *event) | |
2462 | { | |
2463 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
2464 | ||
2465 | if (!has_addr_filter(event)) | |
2466 | return; | |
2467 | ||
2468 | raw_spin_lock(&ifh->lock); | |
2469 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
2470 | event->pmu->addr_filters_sync(event); | |
2471 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
2472 | } | |
2473 | raw_spin_unlock(&ifh->lock); | |
2474 | } | |
2475 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
2476 | ||
f63a8daa | 2477 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2478 | { |
2023b359 | 2479 | /* |
cdd6c482 | 2480 | * not supported on inherited events |
2023b359 | 2481 | */ |
2e939d1d | 2482 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2483 | return -EINVAL; |
2484 | ||
cdd6c482 | 2485 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2486 | _perf_event_enable(event); |
2023b359 PZ |
2487 | |
2488 | return 0; | |
79f14641 | 2489 | } |
f63a8daa PZ |
2490 | |
2491 | /* | |
2492 | * See perf_event_disable() | |
2493 | */ | |
2494 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2495 | { | |
2496 | struct perf_event_context *ctx; | |
2497 | int ret; | |
2498 | ||
2499 | ctx = perf_event_ctx_lock(event); | |
2500 | ret = _perf_event_refresh(event, refresh); | |
2501 | perf_event_ctx_unlock(event, ctx); | |
2502 | ||
2503 | return ret; | |
2504 | } | |
26ca5c11 | 2505 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2506 | |
5b0311e1 FW |
2507 | static void ctx_sched_out(struct perf_event_context *ctx, |
2508 | struct perf_cpu_context *cpuctx, | |
2509 | enum event_type_t event_type) | |
235c7fc7 | 2510 | { |
db24d33e | 2511 | int is_active = ctx->is_active; |
c994d613 | 2512 | struct perf_event *event; |
235c7fc7 | 2513 | |
c994d613 | 2514 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2515 | |
39a43640 PZ |
2516 | if (likely(!ctx->nr_events)) { |
2517 | /* | |
2518 | * See __perf_remove_from_context(). | |
2519 | */ | |
2520 | WARN_ON_ONCE(ctx->is_active); | |
2521 | if (ctx->task) | |
2522 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2523 | return; |
39a43640 PZ |
2524 | } |
2525 | ||
db24d33e | 2526 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2527 | if (!(ctx->is_active & EVENT_ALL)) |
2528 | ctx->is_active = 0; | |
2529 | ||
63e30d3e PZ |
2530 | if (ctx->task) { |
2531 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2532 | if (!ctx->is_active) | |
2533 | cpuctx->task_ctx = NULL; | |
2534 | } | |
facc4307 | 2535 | |
8fdc6539 PZ |
2536 | /* |
2537 | * Always update time if it was set; not only when it changes. | |
2538 | * Otherwise we can 'forget' to update time for any but the last | |
2539 | * context we sched out. For example: | |
2540 | * | |
2541 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2542 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2543 | * | |
2544 | * would only update time for the pinned events. | |
2545 | */ | |
3cbaa590 PZ |
2546 | if (is_active & EVENT_TIME) { |
2547 | /* update (and stop) ctx time */ | |
2548 | update_context_time(ctx); | |
2549 | update_cgrp_time_from_cpuctx(cpuctx); | |
2550 | } | |
2551 | ||
8fdc6539 PZ |
2552 | is_active ^= ctx->is_active; /* changed bits */ |
2553 | ||
3cbaa590 | 2554 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2555 | return; |
5b0311e1 | 2556 | |
075e0b00 | 2557 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2558 | if (is_active & EVENT_PINNED) { |
889ff015 FW |
2559 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
2560 | group_sched_out(event, cpuctx, ctx); | |
9ed6060d | 2561 | } |
889ff015 | 2562 | |
3cbaa590 | 2563 | if (is_active & EVENT_FLEXIBLE) { |
889ff015 | 2564 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
8c9ed8e1 | 2565 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2566 | } |
1b9a644f | 2567 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2568 | } |
2569 | ||
564c2b21 | 2570 | /* |
5a3126d4 PZ |
2571 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2572 | * cloned from the same version of the same context. | |
2573 | * | |
2574 | * Equivalence is measured using a generation number in the context that is | |
2575 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2576 | * and list_del_event(). | |
564c2b21 | 2577 | */ |
cdd6c482 IM |
2578 | static int context_equiv(struct perf_event_context *ctx1, |
2579 | struct perf_event_context *ctx2) | |
564c2b21 | 2580 | { |
211de6eb PZ |
2581 | lockdep_assert_held(&ctx1->lock); |
2582 | lockdep_assert_held(&ctx2->lock); | |
2583 | ||
5a3126d4 PZ |
2584 | /* Pinning disables the swap optimization */ |
2585 | if (ctx1->pin_count || ctx2->pin_count) | |
2586 | return 0; | |
2587 | ||
2588 | /* If ctx1 is the parent of ctx2 */ | |
2589 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2590 | return 1; | |
2591 | ||
2592 | /* If ctx2 is the parent of ctx1 */ | |
2593 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2594 | return 1; | |
2595 | ||
2596 | /* | |
2597 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2598 | * hierarchy, see perf_event_init_context(). | |
2599 | */ | |
2600 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2601 | ctx1->parent_gen == ctx2->parent_gen) | |
2602 | return 1; | |
2603 | ||
2604 | /* Unmatched */ | |
2605 | return 0; | |
564c2b21 PM |
2606 | } |
2607 | ||
cdd6c482 IM |
2608 | static void __perf_event_sync_stat(struct perf_event *event, |
2609 | struct perf_event *next_event) | |
bfbd3381 PZ |
2610 | { |
2611 | u64 value; | |
2612 | ||
cdd6c482 | 2613 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2614 | return; |
2615 | ||
2616 | /* | |
cdd6c482 | 2617 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2618 | * because we're in the middle of a context switch and have IRQs |
2619 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2620 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2621 | * don't need to use it. |
2622 | */ | |
cdd6c482 IM |
2623 | switch (event->state) { |
2624 | case PERF_EVENT_STATE_ACTIVE: | |
3dbebf15 PZ |
2625 | event->pmu->read(event); |
2626 | /* fall-through */ | |
bfbd3381 | 2627 | |
cdd6c482 IM |
2628 | case PERF_EVENT_STATE_INACTIVE: |
2629 | update_event_times(event); | |
bfbd3381 PZ |
2630 | break; |
2631 | ||
2632 | default: | |
2633 | break; | |
2634 | } | |
2635 | ||
2636 | /* | |
cdd6c482 | 2637 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2638 | * values when we flip the contexts. |
2639 | */ | |
e7850595 PZ |
2640 | value = local64_read(&next_event->count); |
2641 | value = local64_xchg(&event->count, value); | |
2642 | local64_set(&next_event->count, value); | |
bfbd3381 | 2643 | |
cdd6c482 IM |
2644 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2645 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2646 | |
bfbd3381 | 2647 | /* |
19d2e755 | 2648 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2649 | */ |
cdd6c482 IM |
2650 | perf_event_update_userpage(event); |
2651 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2652 | } |
2653 | ||
cdd6c482 IM |
2654 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2655 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2656 | { |
cdd6c482 | 2657 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2658 | |
2659 | if (!ctx->nr_stat) | |
2660 | return; | |
2661 | ||
02ffdbc8 PZ |
2662 | update_context_time(ctx); |
2663 | ||
cdd6c482 IM |
2664 | event = list_first_entry(&ctx->event_list, |
2665 | struct perf_event, event_entry); | |
bfbd3381 | 2666 | |
cdd6c482 IM |
2667 | next_event = list_first_entry(&next_ctx->event_list, |
2668 | struct perf_event, event_entry); | |
bfbd3381 | 2669 | |
cdd6c482 IM |
2670 | while (&event->event_entry != &ctx->event_list && |
2671 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 2672 | |
cdd6c482 | 2673 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 2674 | |
cdd6c482 IM |
2675 | event = list_next_entry(event, event_entry); |
2676 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
2677 | } |
2678 | } | |
2679 | ||
fe4b04fa PZ |
2680 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
2681 | struct task_struct *next) | |
0793a61d | 2682 | { |
8dc85d54 | 2683 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 2684 | struct perf_event_context *next_ctx; |
5a3126d4 | 2685 | struct perf_event_context *parent, *next_parent; |
108b02cf | 2686 | struct perf_cpu_context *cpuctx; |
c93f7669 | 2687 | int do_switch = 1; |
0793a61d | 2688 | |
108b02cf PZ |
2689 | if (likely(!ctx)) |
2690 | return; | |
10989fb2 | 2691 | |
108b02cf PZ |
2692 | cpuctx = __get_cpu_context(ctx); |
2693 | if (!cpuctx->task_ctx) | |
0793a61d TG |
2694 | return; |
2695 | ||
c93f7669 | 2696 | rcu_read_lock(); |
8dc85d54 | 2697 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
2698 | if (!next_ctx) |
2699 | goto unlock; | |
2700 | ||
2701 | parent = rcu_dereference(ctx->parent_ctx); | |
2702 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
2703 | ||
2704 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 2705 | if (!parent && !next_parent) |
5a3126d4 PZ |
2706 | goto unlock; |
2707 | ||
2708 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
2709 | /* |
2710 | * Looks like the two contexts are clones, so we might be | |
2711 | * able to optimize the context switch. We lock both | |
2712 | * contexts and check that they are clones under the | |
2713 | * lock (including re-checking that neither has been | |
2714 | * uncloned in the meantime). It doesn't matter which | |
2715 | * order we take the locks because no other cpu could | |
2716 | * be trying to lock both of these tasks. | |
2717 | */ | |
e625cce1 TG |
2718 | raw_spin_lock(&ctx->lock); |
2719 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 2720 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
2721 | WRITE_ONCE(ctx->task, next); |
2722 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
2723 | |
2724 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
2725 | ||
63b6da39 PZ |
2726 | /* |
2727 | * RCU_INIT_POINTER here is safe because we've not | |
2728 | * modified the ctx and the above modification of | |
2729 | * ctx->task and ctx->task_ctx_data are immaterial | |
2730 | * since those values are always verified under | |
2731 | * ctx->lock which we're now holding. | |
2732 | */ | |
2733 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
2734 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
2735 | ||
c93f7669 | 2736 | do_switch = 0; |
bfbd3381 | 2737 | |
cdd6c482 | 2738 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 2739 | } |
e625cce1 TG |
2740 | raw_spin_unlock(&next_ctx->lock); |
2741 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 2742 | } |
5a3126d4 | 2743 | unlock: |
c93f7669 | 2744 | rcu_read_unlock(); |
564c2b21 | 2745 | |
c93f7669 | 2746 | if (do_switch) { |
facc4307 | 2747 | raw_spin_lock(&ctx->lock); |
8833d0e2 | 2748 | task_ctx_sched_out(cpuctx, ctx); |
facc4307 | 2749 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 2750 | } |
0793a61d TG |
2751 | } |
2752 | ||
ba532500 YZ |
2753 | void perf_sched_cb_dec(struct pmu *pmu) |
2754 | { | |
2755 | this_cpu_dec(perf_sched_cb_usages); | |
2756 | } | |
2757 | ||
2758 | void perf_sched_cb_inc(struct pmu *pmu) | |
2759 | { | |
2760 | this_cpu_inc(perf_sched_cb_usages); | |
2761 | } | |
2762 | ||
2763 | /* | |
2764 | * This function provides the context switch callback to the lower code | |
2765 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
2766 | */ | |
2767 | static void perf_pmu_sched_task(struct task_struct *prev, | |
2768 | struct task_struct *next, | |
2769 | bool sched_in) | |
2770 | { | |
2771 | struct perf_cpu_context *cpuctx; | |
2772 | struct pmu *pmu; | |
2773 | unsigned long flags; | |
2774 | ||
2775 | if (prev == next) | |
2776 | return; | |
2777 | ||
2778 | local_irq_save(flags); | |
2779 | ||
2780 | rcu_read_lock(); | |
2781 | ||
2782 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
2783 | if (pmu->sched_task) { | |
2784 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); | |
2785 | ||
2786 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); | |
2787 | ||
2788 | perf_pmu_disable(pmu); | |
2789 | ||
2790 | pmu->sched_task(cpuctx->task_ctx, sched_in); | |
2791 | ||
2792 | perf_pmu_enable(pmu); | |
2793 | ||
2794 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
2795 | } | |
2796 | } | |
2797 | ||
2798 | rcu_read_unlock(); | |
2799 | ||
2800 | local_irq_restore(flags); | |
2801 | } | |
2802 | ||
45ac1403 AH |
2803 | static void perf_event_switch(struct task_struct *task, |
2804 | struct task_struct *next_prev, bool sched_in); | |
2805 | ||
8dc85d54 PZ |
2806 | #define for_each_task_context_nr(ctxn) \ |
2807 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
2808 | ||
2809 | /* | |
2810 | * Called from scheduler to remove the events of the current task, | |
2811 | * with interrupts disabled. | |
2812 | * | |
2813 | * We stop each event and update the event value in event->count. | |
2814 | * | |
2815 | * This does not protect us against NMI, but disable() | |
2816 | * sets the disabled bit in the control field of event _before_ | |
2817 | * accessing the event control register. If a NMI hits, then it will | |
2818 | * not restart the event. | |
2819 | */ | |
ab0cce56 JO |
2820 | void __perf_event_task_sched_out(struct task_struct *task, |
2821 | struct task_struct *next) | |
8dc85d54 PZ |
2822 | { |
2823 | int ctxn; | |
2824 | ||
ba532500 YZ |
2825 | if (__this_cpu_read(perf_sched_cb_usages)) |
2826 | perf_pmu_sched_task(task, next, false); | |
2827 | ||
45ac1403 AH |
2828 | if (atomic_read(&nr_switch_events)) |
2829 | perf_event_switch(task, next, false); | |
2830 | ||
8dc85d54 PZ |
2831 | for_each_task_context_nr(ctxn) |
2832 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
2833 | |
2834 | /* | |
2835 | * if cgroup events exist on this CPU, then we need | |
2836 | * to check if we have to switch out PMU state. | |
2837 | * cgroup event are system-wide mode only | |
2838 | */ | |
4a32fea9 | 2839 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 2840 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
2841 | } |
2842 | ||
5b0311e1 FW |
2843 | /* |
2844 | * Called with IRQs disabled | |
2845 | */ | |
2846 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
2847 | enum event_type_t event_type) | |
2848 | { | |
2849 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
2850 | } |
2851 | ||
235c7fc7 | 2852 | static void |
5b0311e1 | 2853 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
6e37738a | 2854 | struct perf_cpu_context *cpuctx) |
0793a61d | 2855 | { |
cdd6c482 | 2856 | struct perf_event *event; |
0793a61d | 2857 | |
889ff015 FW |
2858 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
2859 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2860 | continue; |
5632ab12 | 2861 | if (!event_filter_match(event)) |
3b6f9e5c PM |
2862 | continue; |
2863 | ||
e5d1367f SE |
2864 | /* may need to reset tstamp_enabled */ |
2865 | if (is_cgroup_event(event)) | |
2866 | perf_cgroup_mark_enabled(event, ctx); | |
2867 | ||
8c9ed8e1 | 2868 | if (group_can_go_on(event, cpuctx, 1)) |
6e37738a | 2869 | group_sched_in(event, cpuctx, ctx); |
3b6f9e5c PM |
2870 | |
2871 | /* | |
2872 | * If this pinned group hasn't been scheduled, | |
2873 | * put it in error state. | |
2874 | */ | |
cdd6c482 IM |
2875 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2876 | update_group_times(event); | |
2877 | event->state = PERF_EVENT_STATE_ERROR; | |
53cfbf59 | 2878 | } |
3b6f9e5c | 2879 | } |
5b0311e1 FW |
2880 | } |
2881 | ||
2882 | static void | |
2883 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
6e37738a | 2884 | struct perf_cpu_context *cpuctx) |
5b0311e1 FW |
2885 | { |
2886 | struct perf_event *event; | |
2887 | int can_add_hw = 1; | |
3b6f9e5c | 2888 | |
889ff015 FW |
2889 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
2890 | /* Ignore events in OFF or ERROR state */ | |
2891 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2892 | continue; |
04289bb9 IM |
2893 | /* |
2894 | * Listen to the 'cpu' scheduling filter constraint | |
cdd6c482 | 2895 | * of events: |
04289bb9 | 2896 | */ |
5632ab12 | 2897 | if (!event_filter_match(event)) |
0793a61d TG |
2898 | continue; |
2899 | ||
e5d1367f SE |
2900 | /* may need to reset tstamp_enabled */ |
2901 | if (is_cgroup_event(event)) | |
2902 | perf_cgroup_mark_enabled(event, ctx); | |
2903 | ||
9ed6060d | 2904 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
6e37738a | 2905 | if (group_sched_in(event, cpuctx, ctx)) |
dd0e6ba2 | 2906 | can_add_hw = 0; |
9ed6060d | 2907 | } |
0793a61d | 2908 | } |
5b0311e1 FW |
2909 | } |
2910 | ||
2911 | static void | |
2912 | ctx_sched_in(struct perf_event_context *ctx, | |
2913 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
2914 | enum event_type_t event_type, |
2915 | struct task_struct *task) | |
5b0311e1 | 2916 | { |
db24d33e | 2917 | int is_active = ctx->is_active; |
c994d613 PZ |
2918 | u64 now; |
2919 | ||
2920 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 2921 | |
5b0311e1 | 2922 | if (likely(!ctx->nr_events)) |
facc4307 | 2923 | return; |
5b0311e1 | 2924 | |
3cbaa590 | 2925 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
2926 | if (ctx->task) { |
2927 | if (!is_active) | |
2928 | cpuctx->task_ctx = ctx; | |
2929 | else | |
2930 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2931 | } | |
2932 | ||
3cbaa590 PZ |
2933 | is_active ^= ctx->is_active; /* changed bits */ |
2934 | ||
2935 | if (is_active & EVENT_TIME) { | |
2936 | /* start ctx time */ | |
2937 | now = perf_clock(); | |
2938 | ctx->timestamp = now; | |
2939 | perf_cgroup_set_timestamp(task, ctx); | |
2940 | } | |
2941 | ||
5b0311e1 FW |
2942 | /* |
2943 | * First go through the list and put on any pinned groups | |
2944 | * in order to give them the best chance of going on. | |
2945 | */ | |
3cbaa590 | 2946 | if (is_active & EVENT_PINNED) |
6e37738a | 2947 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
2948 | |
2949 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 2950 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 2951 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
2952 | } |
2953 | ||
329c0e01 | 2954 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
2955 | enum event_type_t event_type, |
2956 | struct task_struct *task) | |
329c0e01 FW |
2957 | { |
2958 | struct perf_event_context *ctx = &cpuctx->ctx; | |
2959 | ||
e5d1367f | 2960 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
2961 | } |
2962 | ||
e5d1367f SE |
2963 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
2964 | struct task_struct *task) | |
235c7fc7 | 2965 | { |
108b02cf | 2966 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 2967 | |
108b02cf | 2968 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
2969 | if (cpuctx->task_ctx == ctx) |
2970 | return; | |
2971 | ||
facc4307 | 2972 | perf_ctx_lock(cpuctx, ctx); |
1b9a644f | 2973 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
2974 | /* |
2975 | * We want to keep the following priority order: | |
2976 | * cpu pinned (that don't need to move), task pinned, | |
2977 | * cpu flexible, task flexible. | |
2978 | */ | |
2979 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
63e30d3e | 2980 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 PZ |
2981 | perf_pmu_enable(ctx->pmu); |
2982 | perf_ctx_unlock(cpuctx, ctx); | |
235c7fc7 IM |
2983 | } |
2984 | ||
8dc85d54 PZ |
2985 | /* |
2986 | * Called from scheduler to add the events of the current task | |
2987 | * with interrupts disabled. | |
2988 | * | |
2989 | * We restore the event value and then enable it. | |
2990 | * | |
2991 | * This does not protect us against NMI, but enable() | |
2992 | * sets the enabled bit in the control field of event _before_ | |
2993 | * accessing the event control register. If a NMI hits, then it will | |
2994 | * keep the event running. | |
2995 | */ | |
ab0cce56 JO |
2996 | void __perf_event_task_sched_in(struct task_struct *prev, |
2997 | struct task_struct *task) | |
8dc85d54 PZ |
2998 | { |
2999 | struct perf_event_context *ctx; | |
3000 | int ctxn; | |
3001 | ||
7e41d177 PZ |
3002 | /* |
3003 | * If cgroup events exist on this CPU, then we need to check if we have | |
3004 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3005 | * | |
3006 | * Since cgroup events are CPU events, we must schedule these in before | |
3007 | * we schedule in the task events. | |
3008 | */ | |
3009 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3010 | perf_cgroup_sched_in(prev, task); | |
3011 | ||
8dc85d54 PZ |
3012 | for_each_task_context_nr(ctxn) { |
3013 | ctx = task->perf_event_ctxp[ctxn]; | |
3014 | if (likely(!ctx)) | |
3015 | continue; | |
3016 | ||
e5d1367f | 3017 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3018 | } |
d010b332 | 3019 | |
45ac1403 AH |
3020 | if (atomic_read(&nr_switch_events)) |
3021 | perf_event_switch(task, prev, true); | |
3022 | ||
ba532500 YZ |
3023 | if (__this_cpu_read(perf_sched_cb_usages)) |
3024 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3025 | } |
3026 | ||
abd50713 PZ |
3027 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3028 | { | |
3029 | u64 frequency = event->attr.sample_freq; | |
3030 | u64 sec = NSEC_PER_SEC; | |
3031 | u64 divisor, dividend; | |
3032 | ||
3033 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3034 | ||
3035 | count_fls = fls64(count); | |
3036 | nsec_fls = fls64(nsec); | |
3037 | frequency_fls = fls64(frequency); | |
3038 | sec_fls = 30; | |
3039 | ||
3040 | /* | |
3041 | * We got @count in @nsec, with a target of sample_freq HZ | |
3042 | * the target period becomes: | |
3043 | * | |
3044 | * @count * 10^9 | |
3045 | * period = ------------------- | |
3046 | * @nsec * sample_freq | |
3047 | * | |
3048 | */ | |
3049 | ||
3050 | /* | |
3051 | * Reduce accuracy by one bit such that @a and @b converge | |
3052 | * to a similar magnitude. | |
3053 | */ | |
fe4b04fa | 3054 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3055 | do { \ |
3056 | if (a##_fls > b##_fls) { \ | |
3057 | a >>= 1; \ | |
3058 | a##_fls--; \ | |
3059 | } else { \ | |
3060 | b >>= 1; \ | |
3061 | b##_fls--; \ | |
3062 | } \ | |
3063 | } while (0) | |
3064 | ||
3065 | /* | |
3066 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3067 | * the other, so that finally we can do a u64/u64 division. | |
3068 | */ | |
3069 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3070 | REDUCE_FLS(nsec, frequency); | |
3071 | REDUCE_FLS(sec, count); | |
3072 | } | |
3073 | ||
3074 | if (count_fls + sec_fls > 64) { | |
3075 | divisor = nsec * frequency; | |
3076 | ||
3077 | while (count_fls + sec_fls > 64) { | |
3078 | REDUCE_FLS(count, sec); | |
3079 | divisor >>= 1; | |
3080 | } | |
3081 | ||
3082 | dividend = count * sec; | |
3083 | } else { | |
3084 | dividend = count * sec; | |
3085 | ||
3086 | while (nsec_fls + frequency_fls > 64) { | |
3087 | REDUCE_FLS(nsec, frequency); | |
3088 | dividend >>= 1; | |
3089 | } | |
3090 | ||
3091 | divisor = nsec * frequency; | |
3092 | } | |
3093 | ||
f6ab91ad PZ |
3094 | if (!divisor) |
3095 | return dividend; | |
3096 | ||
abd50713 PZ |
3097 | return div64_u64(dividend, divisor); |
3098 | } | |
3099 | ||
e050e3f0 SE |
3100 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3101 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3102 | ||
f39d47ff | 3103 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3104 | { |
cdd6c482 | 3105 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3106 | s64 period, sample_period; |
bd2b5b12 PZ |
3107 | s64 delta; |
3108 | ||
abd50713 | 3109 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3110 | |
3111 | delta = (s64)(period - hwc->sample_period); | |
3112 | delta = (delta + 7) / 8; /* low pass filter */ | |
3113 | ||
3114 | sample_period = hwc->sample_period + delta; | |
3115 | ||
3116 | if (!sample_period) | |
3117 | sample_period = 1; | |
3118 | ||
bd2b5b12 | 3119 | hwc->sample_period = sample_period; |
abd50713 | 3120 | |
e7850595 | 3121 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3122 | if (disable) |
3123 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3124 | ||
e7850595 | 3125 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3126 | |
3127 | if (disable) | |
3128 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3129 | } |
bd2b5b12 PZ |
3130 | } |
3131 | ||
e050e3f0 SE |
3132 | /* |
3133 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3134 | * events. At the same time, make sure, having freq events does not change | |
3135 | * the rate of unthrottling as that would introduce bias. | |
3136 | */ | |
3137 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3138 | int needs_unthr) | |
60db5e09 | 3139 | { |
cdd6c482 IM |
3140 | struct perf_event *event; |
3141 | struct hw_perf_event *hwc; | |
e050e3f0 | 3142 | u64 now, period = TICK_NSEC; |
abd50713 | 3143 | s64 delta; |
60db5e09 | 3144 | |
e050e3f0 SE |
3145 | /* |
3146 | * only need to iterate over all events iff: | |
3147 | * - context have events in frequency mode (needs freq adjust) | |
3148 | * - there are events to unthrottle on this cpu | |
3149 | */ | |
3150 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3151 | return; |
3152 | ||
e050e3f0 | 3153 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3154 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3155 | |
03541f8b | 3156 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3157 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3158 | continue; |
3159 | ||
5632ab12 | 3160 | if (!event_filter_match(event)) |
5d27c23d PZ |
3161 | continue; |
3162 | ||
44377277 AS |
3163 | perf_pmu_disable(event->pmu); |
3164 | ||
cdd6c482 | 3165 | hwc = &event->hw; |
6a24ed6c | 3166 | |
ae23bff1 | 3167 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3168 | hwc->interrupts = 0; |
cdd6c482 | 3169 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3170 | event->pmu->start(event, 0); |
a78ac325 PZ |
3171 | } |
3172 | ||
cdd6c482 | 3173 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3174 | goto next; |
60db5e09 | 3175 | |
e050e3f0 SE |
3176 | /* |
3177 | * stop the event and update event->count | |
3178 | */ | |
3179 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3180 | ||
e7850595 | 3181 | now = local64_read(&event->count); |
abd50713 PZ |
3182 | delta = now - hwc->freq_count_stamp; |
3183 | hwc->freq_count_stamp = now; | |
60db5e09 | 3184 | |
e050e3f0 SE |
3185 | /* |
3186 | * restart the event | |
3187 | * reload only if value has changed | |
f39d47ff SE |
3188 | * we have stopped the event so tell that |
3189 | * to perf_adjust_period() to avoid stopping it | |
3190 | * twice. | |
e050e3f0 | 3191 | */ |
abd50713 | 3192 | if (delta > 0) |
f39d47ff | 3193 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3194 | |
3195 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3196 | next: |
3197 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3198 | } |
e050e3f0 | 3199 | |
f39d47ff | 3200 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3201 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3202 | } |
3203 | ||
235c7fc7 | 3204 | /* |
cdd6c482 | 3205 | * Round-robin a context's events: |
235c7fc7 | 3206 | */ |
cdd6c482 | 3207 | static void rotate_ctx(struct perf_event_context *ctx) |
0793a61d | 3208 | { |
dddd3379 TG |
3209 | /* |
3210 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3211 | * disabled by the inheritance code. | |
3212 | */ | |
3213 | if (!ctx->rotate_disable) | |
3214 | list_rotate_left(&ctx->flexible_groups); | |
235c7fc7 IM |
3215 | } |
3216 | ||
9e630205 | 3217 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
235c7fc7 | 3218 | { |
8dc85d54 | 3219 | struct perf_event_context *ctx = NULL; |
2fde4f94 | 3220 | int rotate = 0; |
7fc23a53 | 3221 | |
b5ab4cd5 | 3222 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 PZ |
3223 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
3224 | rotate = 1; | |
3225 | } | |
235c7fc7 | 3226 | |
8dc85d54 | 3227 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3228 | if (ctx && ctx->nr_events) { |
b5ab4cd5 PZ |
3229 | if (ctx->nr_events != ctx->nr_active) |
3230 | rotate = 1; | |
3231 | } | |
9717e6cd | 3232 | |
e050e3f0 | 3233 | if (!rotate) |
0f5a2601 PZ |
3234 | goto done; |
3235 | ||
facc4307 | 3236 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3237 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3238 | |
e050e3f0 SE |
3239 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
3240 | if (ctx) | |
3241 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3242 | |
e050e3f0 SE |
3243 | rotate_ctx(&cpuctx->ctx); |
3244 | if (ctx) | |
3245 | rotate_ctx(ctx); | |
235c7fc7 | 3246 | |
e050e3f0 | 3247 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3248 | |
0f5a2601 PZ |
3249 | perf_pmu_enable(cpuctx->ctx.pmu); |
3250 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
b5ab4cd5 | 3251 | done: |
9e630205 SE |
3252 | |
3253 | return rotate; | |
e9d2b064 PZ |
3254 | } |
3255 | ||
3256 | void perf_event_task_tick(void) | |
3257 | { | |
2fde4f94 MR |
3258 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3259 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3260 | int throttled; |
b5ab4cd5 | 3261 | |
e9d2b064 PZ |
3262 | WARN_ON(!irqs_disabled()); |
3263 | ||
e050e3f0 SE |
3264 | __this_cpu_inc(perf_throttled_seq); |
3265 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3266 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3267 | |
2fde4f94 | 3268 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3269 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3270 | } |
3271 | ||
889ff015 FW |
3272 | static int event_enable_on_exec(struct perf_event *event, |
3273 | struct perf_event_context *ctx) | |
3274 | { | |
3275 | if (!event->attr.enable_on_exec) | |
3276 | return 0; | |
3277 | ||
3278 | event->attr.enable_on_exec = 0; | |
3279 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3280 | return 0; | |
3281 | ||
1d9b482e | 3282 | __perf_event_mark_enabled(event); |
889ff015 FW |
3283 | |
3284 | return 1; | |
3285 | } | |
3286 | ||
57e7986e | 3287 | /* |
cdd6c482 | 3288 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3289 | * This expects task == current. |
3290 | */ | |
c1274499 | 3291 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3292 | { |
c1274499 | 3293 | struct perf_event_context *ctx, *clone_ctx = NULL; |
3e349507 | 3294 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3295 | struct perf_event *event; |
57e7986e PM |
3296 | unsigned long flags; |
3297 | int enabled = 0; | |
3298 | ||
3299 | local_irq_save(flags); | |
c1274499 | 3300 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3301 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3302 | goto out; |
3303 | ||
3e349507 PZ |
3304 | cpuctx = __get_cpu_context(ctx); |
3305 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3306 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
3e349507 PZ |
3307 | list_for_each_entry(event, &ctx->event_list, event_entry) |
3308 | enabled |= event_enable_on_exec(event, ctx); | |
57e7986e PM |
3309 | |
3310 | /* | |
3e349507 | 3311 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3312 | */ |
3e349507 | 3313 | if (enabled) { |
211de6eb | 3314 | clone_ctx = unclone_ctx(ctx); |
3e349507 PZ |
3315 | ctx_resched(cpuctx, ctx); |
3316 | } | |
3317 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3318 | |
9ed6060d | 3319 | out: |
57e7986e | 3320 | local_irq_restore(flags); |
211de6eb PZ |
3321 | |
3322 | if (clone_ctx) | |
3323 | put_ctx(clone_ctx); | |
57e7986e PM |
3324 | } |
3325 | ||
0492d4c5 PZ |
3326 | struct perf_read_data { |
3327 | struct perf_event *event; | |
3328 | bool group; | |
7d88962e | 3329 | int ret; |
0492d4c5 PZ |
3330 | }; |
3331 | ||
0793a61d | 3332 | /* |
cdd6c482 | 3333 | * Cross CPU call to read the hardware event |
0793a61d | 3334 | */ |
cdd6c482 | 3335 | static void __perf_event_read(void *info) |
0793a61d | 3336 | { |
0492d4c5 PZ |
3337 | struct perf_read_data *data = info; |
3338 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3339 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3340 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3341 | struct pmu *pmu = event->pmu; |
621a01ea | 3342 | |
e1ac3614 PM |
3343 | /* |
3344 | * If this is a task context, we need to check whether it is | |
3345 | * the current task context of this cpu. If not it has been | |
3346 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3347 | * event->count would have been updated to a recent sample |
3348 | * when the event was scheduled out. | |
e1ac3614 PM |
3349 | */ |
3350 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3351 | return; | |
3352 | ||
e625cce1 | 3353 | raw_spin_lock(&ctx->lock); |
e5d1367f | 3354 | if (ctx->is_active) { |
542e72fc | 3355 | update_context_time(ctx); |
e5d1367f SE |
3356 | update_cgrp_time_from_event(event); |
3357 | } | |
0492d4c5 | 3358 | |
cdd6c482 | 3359 | update_event_times(event); |
4a00c16e SB |
3360 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3361 | goto unlock; | |
0492d4c5 | 3362 | |
4a00c16e SB |
3363 | if (!data->group) { |
3364 | pmu->read(event); | |
3365 | data->ret = 0; | |
0492d4c5 | 3366 | goto unlock; |
4a00c16e SB |
3367 | } |
3368 | ||
3369 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3370 | ||
3371 | pmu->read(event); | |
0492d4c5 PZ |
3372 | |
3373 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
3374 | update_event_times(sub); | |
4a00c16e SB |
3375 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3376 | /* | |
3377 | * Use sibling's PMU rather than @event's since | |
3378 | * sibling could be on different (eg: software) PMU. | |
3379 | */ | |
0492d4c5 | 3380 | sub->pmu->read(sub); |
4a00c16e | 3381 | } |
0492d4c5 | 3382 | } |
4a00c16e SB |
3383 | |
3384 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3385 | |
3386 | unlock: | |
e625cce1 | 3387 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3388 | } |
3389 | ||
b5e58793 PZ |
3390 | static inline u64 perf_event_count(struct perf_event *event) |
3391 | { | |
eacd3ecc MF |
3392 | if (event->pmu->count) |
3393 | return event->pmu->count(event); | |
3394 | ||
3395 | return __perf_event_count(event); | |
b5e58793 PZ |
3396 | } |
3397 | ||
ffe8690c KX |
3398 | /* |
3399 | * NMI-safe method to read a local event, that is an event that | |
3400 | * is: | |
3401 | * - either for the current task, or for this CPU | |
3402 | * - does not have inherit set, for inherited task events | |
3403 | * will not be local and we cannot read them atomically | |
3404 | * - must not have a pmu::count method | |
3405 | */ | |
3406 | u64 perf_event_read_local(struct perf_event *event) | |
3407 | { | |
3408 | unsigned long flags; | |
3409 | u64 val; | |
3410 | ||
3411 | /* | |
3412 | * Disabling interrupts avoids all counter scheduling (context | |
3413 | * switches, timer based rotation and IPIs). | |
3414 | */ | |
3415 | local_irq_save(flags); | |
3416 | ||
3417 | /* If this is a per-task event, it must be for current */ | |
3418 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && | |
3419 | event->hw.target != current); | |
3420 | ||
3421 | /* If this is a per-CPU event, it must be for this CPU */ | |
3422 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && | |
3423 | event->cpu != smp_processor_id()); | |
3424 | ||
3425 | /* | |
3426 | * It must not be an event with inherit set, we cannot read | |
3427 | * all child counters from atomic context. | |
3428 | */ | |
3429 | WARN_ON_ONCE(event->attr.inherit); | |
3430 | ||
3431 | /* | |
3432 | * It must not have a pmu::count method, those are not | |
3433 | * NMI safe. | |
3434 | */ | |
3435 | WARN_ON_ONCE(event->pmu->count); | |
3436 | ||
3437 | /* | |
3438 | * If the event is currently on this CPU, its either a per-task event, | |
3439 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3440 | * oncpu == -1). | |
3441 | */ | |
3442 | if (event->oncpu == smp_processor_id()) | |
3443 | event->pmu->read(event); | |
3444 | ||
3445 | val = local64_read(&event->count); | |
3446 | local_irq_restore(flags); | |
3447 | ||
3448 | return val; | |
3449 | } | |
3450 | ||
7d88962e | 3451 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3452 | { |
7d88962e SB |
3453 | int ret = 0; |
3454 | ||
0793a61d | 3455 | /* |
cdd6c482 IM |
3456 | * If event is enabled and currently active on a CPU, update the |
3457 | * value in the event structure: | |
0793a61d | 3458 | */ |
cdd6c482 | 3459 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
0492d4c5 PZ |
3460 | struct perf_read_data data = { |
3461 | .event = event, | |
3462 | .group = group, | |
7d88962e | 3463 | .ret = 0, |
0492d4c5 | 3464 | }; |
cdd6c482 | 3465 | smp_call_function_single(event->oncpu, |
0492d4c5 | 3466 | __perf_event_read, &data, 1); |
7d88962e | 3467 | ret = data.ret; |
cdd6c482 | 3468 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2b8988c9 PZ |
3469 | struct perf_event_context *ctx = event->ctx; |
3470 | unsigned long flags; | |
3471 | ||
e625cce1 | 3472 | raw_spin_lock_irqsave(&ctx->lock, flags); |
c530ccd9 SE |
3473 | /* |
3474 | * may read while context is not active | |
3475 | * (e.g., thread is blocked), in that case | |
3476 | * we cannot update context time | |
3477 | */ | |
e5d1367f | 3478 | if (ctx->is_active) { |
c530ccd9 | 3479 | update_context_time(ctx); |
e5d1367f SE |
3480 | update_cgrp_time_from_event(event); |
3481 | } | |
0492d4c5 PZ |
3482 | if (group) |
3483 | update_group_times(event); | |
3484 | else | |
3485 | update_event_times(event); | |
e625cce1 | 3486 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3487 | } |
7d88962e SB |
3488 | |
3489 | return ret; | |
0793a61d TG |
3490 | } |
3491 | ||
a63eaf34 | 3492 | /* |
cdd6c482 | 3493 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3494 | */ |
eb184479 | 3495 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 3496 | { |
e625cce1 | 3497 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 3498 | mutex_init(&ctx->mutex); |
2fde4f94 | 3499 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
889ff015 FW |
3500 | INIT_LIST_HEAD(&ctx->pinned_groups); |
3501 | INIT_LIST_HEAD(&ctx->flexible_groups); | |
a63eaf34 PM |
3502 | INIT_LIST_HEAD(&ctx->event_list); |
3503 | atomic_set(&ctx->refcount, 1); | |
eb184479 PZ |
3504 | } |
3505 | ||
3506 | static struct perf_event_context * | |
3507 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
3508 | { | |
3509 | struct perf_event_context *ctx; | |
3510 | ||
3511 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
3512 | if (!ctx) | |
3513 | return NULL; | |
3514 | ||
3515 | __perf_event_init_context(ctx); | |
3516 | if (task) { | |
3517 | ctx->task = task; | |
3518 | get_task_struct(task); | |
0793a61d | 3519 | } |
eb184479 PZ |
3520 | ctx->pmu = pmu; |
3521 | ||
3522 | return ctx; | |
a63eaf34 PM |
3523 | } |
3524 | ||
2ebd4ffb MH |
3525 | static struct task_struct * |
3526 | find_lively_task_by_vpid(pid_t vpid) | |
3527 | { | |
3528 | struct task_struct *task; | |
0793a61d TG |
3529 | |
3530 | rcu_read_lock(); | |
2ebd4ffb | 3531 | if (!vpid) |
0793a61d TG |
3532 | task = current; |
3533 | else | |
2ebd4ffb | 3534 | task = find_task_by_vpid(vpid); |
0793a61d TG |
3535 | if (task) |
3536 | get_task_struct(task); | |
3537 | rcu_read_unlock(); | |
3538 | ||
3539 | if (!task) | |
3540 | return ERR_PTR(-ESRCH); | |
3541 | ||
2ebd4ffb | 3542 | return task; |
2ebd4ffb MH |
3543 | } |
3544 | ||
fe4b04fa PZ |
3545 | /* |
3546 | * Returns a matching context with refcount and pincount. | |
3547 | */ | |
108b02cf | 3548 | static struct perf_event_context * |
4af57ef2 YZ |
3549 | find_get_context(struct pmu *pmu, struct task_struct *task, |
3550 | struct perf_event *event) | |
0793a61d | 3551 | { |
211de6eb | 3552 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 3553 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 3554 | void *task_ctx_data = NULL; |
25346b93 | 3555 | unsigned long flags; |
8dc85d54 | 3556 | int ctxn, err; |
4af57ef2 | 3557 | int cpu = event->cpu; |
0793a61d | 3558 | |
22a4ec72 | 3559 | if (!task) { |
cdd6c482 | 3560 | /* Must be root to operate on a CPU event: */ |
0764771d | 3561 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
3562 | return ERR_PTR(-EACCES); |
3563 | ||
0793a61d | 3564 | /* |
cdd6c482 | 3565 | * We could be clever and allow to attach a event to an |
0793a61d TG |
3566 | * offline CPU and activate it when the CPU comes up, but |
3567 | * that's for later. | |
3568 | */ | |
f6325e30 | 3569 | if (!cpu_online(cpu)) |
0793a61d TG |
3570 | return ERR_PTR(-ENODEV); |
3571 | ||
108b02cf | 3572 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 3573 | ctx = &cpuctx->ctx; |
c93f7669 | 3574 | get_ctx(ctx); |
fe4b04fa | 3575 | ++ctx->pin_count; |
0793a61d | 3576 | |
0793a61d TG |
3577 | return ctx; |
3578 | } | |
3579 | ||
8dc85d54 PZ |
3580 | err = -EINVAL; |
3581 | ctxn = pmu->task_ctx_nr; | |
3582 | if (ctxn < 0) | |
3583 | goto errout; | |
3584 | ||
4af57ef2 YZ |
3585 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
3586 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
3587 | if (!task_ctx_data) { | |
3588 | err = -ENOMEM; | |
3589 | goto errout; | |
3590 | } | |
3591 | } | |
3592 | ||
9ed6060d | 3593 | retry: |
8dc85d54 | 3594 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 3595 | if (ctx) { |
211de6eb | 3596 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 3597 | ++ctx->pin_count; |
4af57ef2 YZ |
3598 | |
3599 | if (task_ctx_data && !ctx->task_ctx_data) { | |
3600 | ctx->task_ctx_data = task_ctx_data; | |
3601 | task_ctx_data = NULL; | |
3602 | } | |
e625cce1 | 3603 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
3604 | |
3605 | if (clone_ctx) | |
3606 | put_ctx(clone_ctx); | |
9137fb28 | 3607 | } else { |
eb184479 | 3608 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
3609 | err = -ENOMEM; |
3610 | if (!ctx) | |
3611 | goto errout; | |
eb184479 | 3612 | |
4af57ef2 YZ |
3613 | if (task_ctx_data) { |
3614 | ctx->task_ctx_data = task_ctx_data; | |
3615 | task_ctx_data = NULL; | |
3616 | } | |
3617 | ||
dbe08d82 ON |
3618 | err = 0; |
3619 | mutex_lock(&task->perf_event_mutex); | |
3620 | /* | |
3621 | * If it has already passed perf_event_exit_task(). | |
3622 | * we must see PF_EXITING, it takes this mutex too. | |
3623 | */ | |
3624 | if (task->flags & PF_EXITING) | |
3625 | err = -ESRCH; | |
3626 | else if (task->perf_event_ctxp[ctxn]) | |
3627 | err = -EAGAIN; | |
fe4b04fa | 3628 | else { |
9137fb28 | 3629 | get_ctx(ctx); |
fe4b04fa | 3630 | ++ctx->pin_count; |
dbe08d82 | 3631 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 3632 | } |
dbe08d82 ON |
3633 | mutex_unlock(&task->perf_event_mutex); |
3634 | ||
3635 | if (unlikely(err)) { | |
9137fb28 | 3636 | put_ctx(ctx); |
dbe08d82 ON |
3637 | |
3638 | if (err == -EAGAIN) | |
3639 | goto retry; | |
3640 | goto errout; | |
a63eaf34 PM |
3641 | } |
3642 | } | |
3643 | ||
4af57ef2 | 3644 | kfree(task_ctx_data); |
0793a61d | 3645 | return ctx; |
c93f7669 | 3646 | |
9ed6060d | 3647 | errout: |
4af57ef2 | 3648 | kfree(task_ctx_data); |
c93f7669 | 3649 | return ERR_PTR(err); |
0793a61d TG |
3650 | } |
3651 | ||
6fb2915d | 3652 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 3653 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 3654 | |
cdd6c482 | 3655 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 3656 | { |
cdd6c482 | 3657 | struct perf_event *event; |
592903cd | 3658 | |
cdd6c482 IM |
3659 | event = container_of(head, struct perf_event, rcu_head); |
3660 | if (event->ns) | |
3661 | put_pid_ns(event->ns); | |
6fb2915d | 3662 | perf_event_free_filter(event); |
cdd6c482 | 3663 | kfree(event); |
592903cd PZ |
3664 | } |
3665 | ||
b69cf536 PZ |
3666 | static void ring_buffer_attach(struct perf_event *event, |
3667 | struct ring_buffer *rb); | |
925d519a | 3668 | |
f2fb6bef KL |
3669 | static void detach_sb_event(struct perf_event *event) |
3670 | { | |
3671 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
3672 | ||
3673 | raw_spin_lock(&pel->lock); | |
3674 | list_del_rcu(&event->sb_list); | |
3675 | raw_spin_unlock(&pel->lock); | |
3676 | } | |
3677 | ||
3678 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
3679 | { | |
3680 | if (event->parent) | |
3681 | return; | |
3682 | ||
3683 | if (event->attach_state & PERF_ATTACH_TASK) | |
3684 | return; | |
3685 | ||
3686 | detach_sb_event(event); | |
3687 | } | |
3688 | ||
4beb31f3 | 3689 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 3690 | { |
4beb31f3 FW |
3691 | if (event->parent) |
3692 | return; | |
3693 | ||
4beb31f3 FW |
3694 | if (is_cgroup_event(event)) |
3695 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
3696 | } | |
925d519a | 3697 | |
555e0c1e FW |
3698 | #ifdef CONFIG_NO_HZ_FULL |
3699 | static DEFINE_SPINLOCK(nr_freq_lock); | |
3700 | #endif | |
3701 | ||
3702 | static void unaccount_freq_event_nohz(void) | |
3703 | { | |
3704 | #ifdef CONFIG_NO_HZ_FULL | |
3705 | spin_lock(&nr_freq_lock); | |
3706 | if (atomic_dec_and_test(&nr_freq_events)) | |
3707 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
3708 | spin_unlock(&nr_freq_lock); | |
3709 | #endif | |
3710 | } | |
3711 | ||
3712 | static void unaccount_freq_event(void) | |
3713 | { | |
3714 | if (tick_nohz_full_enabled()) | |
3715 | unaccount_freq_event_nohz(); | |
3716 | else | |
3717 | atomic_dec(&nr_freq_events); | |
3718 | } | |
3719 | ||
4beb31f3 FW |
3720 | static void unaccount_event(struct perf_event *event) |
3721 | { | |
25432ae9 PZ |
3722 | bool dec = false; |
3723 | ||
4beb31f3 FW |
3724 | if (event->parent) |
3725 | return; | |
3726 | ||
3727 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 3728 | dec = true; |
4beb31f3 FW |
3729 | if (event->attr.mmap || event->attr.mmap_data) |
3730 | atomic_dec(&nr_mmap_events); | |
3731 | if (event->attr.comm) | |
3732 | atomic_dec(&nr_comm_events); | |
3733 | if (event->attr.task) | |
3734 | atomic_dec(&nr_task_events); | |
948b26b6 | 3735 | if (event->attr.freq) |
555e0c1e | 3736 | unaccount_freq_event(); |
45ac1403 | 3737 | if (event->attr.context_switch) { |
25432ae9 | 3738 | dec = true; |
45ac1403 AH |
3739 | atomic_dec(&nr_switch_events); |
3740 | } | |
4beb31f3 | 3741 | if (is_cgroup_event(event)) |
25432ae9 | 3742 | dec = true; |
4beb31f3 | 3743 | if (has_branch_stack(event)) |
25432ae9 PZ |
3744 | dec = true; |
3745 | ||
9107c89e PZ |
3746 | if (dec) { |
3747 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
3748 | schedule_delayed_work(&perf_sched_work, HZ); | |
3749 | } | |
4beb31f3 FW |
3750 | |
3751 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
3752 | |
3753 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 3754 | } |
925d519a | 3755 | |
9107c89e PZ |
3756 | static void perf_sched_delayed(struct work_struct *work) |
3757 | { | |
3758 | mutex_lock(&perf_sched_mutex); | |
3759 | if (atomic_dec_and_test(&perf_sched_count)) | |
3760 | static_branch_disable(&perf_sched_events); | |
3761 | mutex_unlock(&perf_sched_mutex); | |
3762 | } | |
3763 | ||
bed5b25a AS |
3764 | /* |
3765 | * The following implement mutual exclusion of events on "exclusive" pmus | |
3766 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
3767 | * at a time, so we disallow creating events that might conflict, namely: | |
3768 | * | |
3769 | * 1) cpu-wide events in the presence of per-task events, | |
3770 | * 2) per-task events in the presence of cpu-wide events, | |
3771 | * 3) two matching events on the same context. | |
3772 | * | |
3773 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 3774 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
3775 | */ |
3776 | static int exclusive_event_init(struct perf_event *event) | |
3777 | { | |
3778 | struct pmu *pmu = event->pmu; | |
3779 | ||
3780 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3781 | return 0; | |
3782 | ||
3783 | /* | |
3784 | * Prevent co-existence of per-task and cpu-wide events on the | |
3785 | * same exclusive pmu. | |
3786 | * | |
3787 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
3788 | * events on this "exclusive" pmu, positive means there are | |
3789 | * per-task events. | |
3790 | * | |
3791 | * Since this is called in perf_event_alloc() path, event::ctx | |
3792 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
3793 | * to mean "per-task event", because unlike other attach states it | |
3794 | * never gets cleared. | |
3795 | */ | |
3796 | if (event->attach_state & PERF_ATTACH_TASK) { | |
3797 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
3798 | return -EBUSY; | |
3799 | } else { | |
3800 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
3801 | return -EBUSY; | |
3802 | } | |
3803 | ||
3804 | return 0; | |
3805 | } | |
3806 | ||
3807 | static void exclusive_event_destroy(struct perf_event *event) | |
3808 | { | |
3809 | struct pmu *pmu = event->pmu; | |
3810 | ||
3811 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3812 | return; | |
3813 | ||
3814 | /* see comment in exclusive_event_init() */ | |
3815 | if (event->attach_state & PERF_ATTACH_TASK) | |
3816 | atomic_dec(&pmu->exclusive_cnt); | |
3817 | else | |
3818 | atomic_inc(&pmu->exclusive_cnt); | |
3819 | } | |
3820 | ||
3821 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
3822 | { | |
3823 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && | |
3824 | (e1->cpu == e2->cpu || | |
3825 | e1->cpu == -1 || | |
3826 | e2->cpu == -1)) | |
3827 | return true; | |
3828 | return false; | |
3829 | } | |
3830 | ||
3831 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
3832 | static bool exclusive_event_installable(struct perf_event *event, | |
3833 | struct perf_event_context *ctx) | |
3834 | { | |
3835 | struct perf_event *iter_event; | |
3836 | struct pmu *pmu = event->pmu; | |
3837 | ||
3838 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3839 | return true; | |
3840 | ||
3841 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
3842 | if (exclusive_event_match(iter_event, event)) | |
3843 | return false; | |
3844 | } | |
3845 | ||
3846 | return true; | |
3847 | } | |
3848 | ||
375637bc AS |
3849 | static void perf_addr_filters_splice(struct perf_event *event, |
3850 | struct list_head *head); | |
3851 | ||
683ede43 | 3852 | static void _free_event(struct perf_event *event) |
f1600952 | 3853 | { |
e360adbe | 3854 | irq_work_sync(&event->pending); |
925d519a | 3855 | |
4beb31f3 | 3856 | unaccount_event(event); |
9ee318a7 | 3857 | |
76369139 | 3858 | if (event->rb) { |
9bb5d40c PZ |
3859 | /* |
3860 | * Can happen when we close an event with re-directed output. | |
3861 | * | |
3862 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
3863 | * over us; possibly making our ring_buffer_put() the last. | |
3864 | */ | |
3865 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 3866 | ring_buffer_attach(event, NULL); |
9bb5d40c | 3867 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
3868 | } |
3869 | ||
e5d1367f SE |
3870 | if (is_cgroup_event(event)) |
3871 | perf_detach_cgroup(event); | |
3872 | ||
a0733e69 PZ |
3873 | if (!event->parent) { |
3874 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
3875 | put_callchain_buffers(); | |
3876 | } | |
3877 | ||
3878 | perf_event_free_bpf_prog(event); | |
375637bc AS |
3879 | perf_addr_filters_splice(event, NULL); |
3880 | kfree(event->addr_filters_offs); | |
a0733e69 PZ |
3881 | |
3882 | if (event->destroy) | |
3883 | event->destroy(event); | |
3884 | ||
3885 | if (event->ctx) | |
3886 | put_ctx(event->ctx); | |
3887 | ||
3888 | if (event->pmu) { | |
3889 | exclusive_event_destroy(event); | |
3890 | module_put(event->pmu->module); | |
3891 | } | |
3892 | ||
3893 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
3894 | } |
3895 | ||
683ede43 PZ |
3896 | /* |
3897 | * Used to free events which have a known refcount of 1, such as in error paths | |
3898 | * where the event isn't exposed yet and inherited events. | |
3899 | */ | |
3900 | static void free_event(struct perf_event *event) | |
0793a61d | 3901 | { |
683ede43 PZ |
3902 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
3903 | "unexpected event refcount: %ld; ptr=%p\n", | |
3904 | atomic_long_read(&event->refcount), event)) { | |
3905 | /* leak to avoid use-after-free */ | |
3906 | return; | |
3907 | } | |
0793a61d | 3908 | |
683ede43 | 3909 | _free_event(event); |
0793a61d TG |
3910 | } |
3911 | ||
a66a3052 | 3912 | /* |
f8697762 | 3913 | * Remove user event from the owner task. |
a66a3052 | 3914 | */ |
f8697762 | 3915 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 3916 | { |
8882135b | 3917 | struct task_struct *owner; |
fb0459d7 | 3918 | |
8882135b | 3919 | rcu_read_lock(); |
8882135b | 3920 | /* |
f47c02c0 PZ |
3921 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
3922 | * observe !owner it means the list deletion is complete and we can | |
3923 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
3924 | * owner->perf_event_mutex. |
3925 | */ | |
f47c02c0 | 3926 | owner = lockless_dereference(event->owner); |
8882135b PZ |
3927 | if (owner) { |
3928 | /* | |
3929 | * Since delayed_put_task_struct() also drops the last | |
3930 | * task reference we can safely take a new reference | |
3931 | * while holding the rcu_read_lock(). | |
3932 | */ | |
3933 | get_task_struct(owner); | |
3934 | } | |
3935 | rcu_read_unlock(); | |
3936 | ||
3937 | if (owner) { | |
f63a8daa PZ |
3938 | /* |
3939 | * If we're here through perf_event_exit_task() we're already | |
3940 | * holding ctx->mutex which would be an inversion wrt. the | |
3941 | * normal lock order. | |
3942 | * | |
3943 | * However we can safely take this lock because its the child | |
3944 | * ctx->mutex. | |
3945 | */ | |
3946 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
3947 | ||
8882135b PZ |
3948 | /* |
3949 | * We have to re-check the event->owner field, if it is cleared | |
3950 | * we raced with perf_event_exit_task(), acquiring the mutex | |
3951 | * ensured they're done, and we can proceed with freeing the | |
3952 | * event. | |
3953 | */ | |
f47c02c0 | 3954 | if (event->owner) { |
8882135b | 3955 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
3956 | smp_store_release(&event->owner, NULL); |
3957 | } | |
8882135b PZ |
3958 | mutex_unlock(&owner->perf_event_mutex); |
3959 | put_task_struct(owner); | |
3960 | } | |
f8697762 JO |
3961 | } |
3962 | ||
f8697762 JO |
3963 | static void put_event(struct perf_event *event) |
3964 | { | |
f8697762 JO |
3965 | if (!atomic_long_dec_and_test(&event->refcount)) |
3966 | return; | |
3967 | ||
c6e5b732 PZ |
3968 | _free_event(event); |
3969 | } | |
3970 | ||
3971 | /* | |
3972 | * Kill an event dead; while event:refcount will preserve the event | |
3973 | * object, it will not preserve its functionality. Once the last 'user' | |
3974 | * gives up the object, we'll destroy the thing. | |
3975 | */ | |
3976 | int perf_event_release_kernel(struct perf_event *event) | |
3977 | { | |
a4f4bb6d | 3978 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 PZ |
3979 | struct perf_event *child, *tmp; |
3980 | ||
a4f4bb6d PZ |
3981 | /* |
3982 | * If we got here through err_file: fput(event_file); we will not have | |
3983 | * attached to a context yet. | |
3984 | */ | |
3985 | if (!ctx) { | |
3986 | WARN_ON_ONCE(event->attach_state & | |
3987 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
3988 | goto no_ctx; | |
3989 | } | |
3990 | ||
f8697762 JO |
3991 | if (!is_kernel_event(event)) |
3992 | perf_remove_from_owner(event); | |
8882135b | 3993 | |
5fa7c8ec | 3994 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 3995 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 3996 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 3997 | |
a69b0ca4 | 3998 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 3999 | /* |
a69b0ca4 PZ |
4000 | * Mark this even as STATE_DEAD, there is no external reference to it |
4001 | * anymore. | |
683ede43 | 4002 | * |
a69b0ca4 PZ |
4003 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4004 | * also see this, most importantly inherit_event() which will avoid | |
4005 | * placing more children on the list. | |
683ede43 | 4006 | * |
c6e5b732 PZ |
4007 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4008 | * child events. | |
683ede43 | 4009 | */ |
a69b0ca4 PZ |
4010 | event->state = PERF_EVENT_STATE_DEAD; |
4011 | raw_spin_unlock_irq(&ctx->lock); | |
4012 | ||
4013 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4014 | |
c6e5b732 PZ |
4015 | again: |
4016 | mutex_lock(&event->child_mutex); | |
4017 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4018 | |
c6e5b732 PZ |
4019 | /* |
4020 | * Cannot change, child events are not migrated, see the | |
4021 | * comment with perf_event_ctx_lock_nested(). | |
4022 | */ | |
4023 | ctx = lockless_dereference(child->ctx); | |
4024 | /* | |
4025 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4026 | * through hoops. We start by grabbing a reference on the ctx. | |
4027 | * | |
4028 | * Since the event cannot get freed while we hold the | |
4029 | * child_mutex, the context must also exist and have a !0 | |
4030 | * reference count. | |
4031 | */ | |
4032 | get_ctx(ctx); | |
4033 | ||
4034 | /* | |
4035 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4036 | * acquire ctx::mutex without fear of it going away. Then we | |
4037 | * can re-acquire child_mutex. | |
4038 | */ | |
4039 | mutex_unlock(&event->child_mutex); | |
4040 | mutex_lock(&ctx->mutex); | |
4041 | mutex_lock(&event->child_mutex); | |
4042 | ||
4043 | /* | |
4044 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4045 | * state, if child is still the first entry, it didn't get freed | |
4046 | * and we can continue doing so. | |
4047 | */ | |
4048 | tmp = list_first_entry_or_null(&event->child_list, | |
4049 | struct perf_event, child_list); | |
4050 | if (tmp == child) { | |
4051 | perf_remove_from_context(child, DETACH_GROUP); | |
4052 | list_del(&child->child_list); | |
4053 | free_event(child); | |
4054 | /* | |
4055 | * This matches the refcount bump in inherit_event(); | |
4056 | * this can't be the last reference. | |
4057 | */ | |
4058 | put_event(event); | |
4059 | } | |
4060 | ||
4061 | mutex_unlock(&event->child_mutex); | |
4062 | mutex_unlock(&ctx->mutex); | |
4063 | put_ctx(ctx); | |
4064 | goto again; | |
4065 | } | |
4066 | mutex_unlock(&event->child_mutex); | |
4067 | ||
a4f4bb6d PZ |
4068 | no_ctx: |
4069 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
4070 | return 0; |
4071 | } | |
4072 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
4073 | ||
8b10c5e2 PZ |
4074 | /* |
4075 | * Called when the last reference to the file is gone. | |
4076 | */ | |
a6fa941d AV |
4077 | static int perf_release(struct inode *inode, struct file *file) |
4078 | { | |
c6e5b732 | 4079 | perf_event_release_kernel(file->private_data); |
a6fa941d | 4080 | return 0; |
fb0459d7 | 4081 | } |
fb0459d7 | 4082 | |
59ed446f | 4083 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 4084 | { |
cdd6c482 | 4085 | struct perf_event *child; |
e53c0994 PZ |
4086 | u64 total = 0; |
4087 | ||
59ed446f PZ |
4088 | *enabled = 0; |
4089 | *running = 0; | |
4090 | ||
6f10581a | 4091 | mutex_lock(&event->child_mutex); |
01add3ea | 4092 | |
7d88962e | 4093 | (void)perf_event_read(event, false); |
01add3ea SB |
4094 | total += perf_event_count(event); |
4095 | ||
59ed446f PZ |
4096 | *enabled += event->total_time_enabled + |
4097 | atomic64_read(&event->child_total_time_enabled); | |
4098 | *running += event->total_time_running + | |
4099 | atomic64_read(&event->child_total_time_running); | |
4100 | ||
4101 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4102 | (void)perf_event_read(child, false); |
01add3ea | 4103 | total += perf_event_count(child); |
59ed446f PZ |
4104 | *enabled += child->total_time_enabled; |
4105 | *running += child->total_time_running; | |
4106 | } | |
6f10581a | 4107 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4108 | |
4109 | return total; | |
4110 | } | |
fb0459d7 | 4111 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4112 | |
7d88962e | 4113 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4114 | u64 read_format, u64 *values) |
3dab77fb | 4115 | { |
fa8c2693 PZ |
4116 | struct perf_event *sub; |
4117 | int n = 1; /* skip @nr */ | |
7d88962e | 4118 | int ret; |
f63a8daa | 4119 | |
7d88962e SB |
4120 | ret = perf_event_read(leader, true); |
4121 | if (ret) | |
4122 | return ret; | |
abf4868b | 4123 | |
fa8c2693 PZ |
4124 | /* |
4125 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4126 | * will be identical to those of the leader, so we only publish one | |
4127 | * set. | |
4128 | */ | |
4129 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4130 | values[n++] += leader->total_time_enabled + | |
4131 | atomic64_read(&leader->child_total_time_enabled); | |
4132 | } | |
3dab77fb | 4133 | |
fa8c2693 PZ |
4134 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4135 | values[n++] += leader->total_time_running + | |
4136 | atomic64_read(&leader->child_total_time_running); | |
4137 | } | |
4138 | ||
4139 | /* | |
4140 | * Write {count,id} tuples for every sibling. | |
4141 | */ | |
4142 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4143 | if (read_format & PERF_FORMAT_ID) |
4144 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4145 | |
fa8c2693 PZ |
4146 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
4147 | values[n++] += perf_event_count(sub); | |
4148 | if (read_format & PERF_FORMAT_ID) | |
4149 | values[n++] = primary_event_id(sub); | |
4150 | } | |
7d88962e SB |
4151 | |
4152 | return 0; | |
fa8c2693 | 4153 | } |
3dab77fb | 4154 | |
fa8c2693 PZ |
4155 | static int perf_read_group(struct perf_event *event, |
4156 | u64 read_format, char __user *buf) | |
4157 | { | |
4158 | struct perf_event *leader = event->group_leader, *child; | |
4159 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4160 | int ret; |
fa8c2693 | 4161 | u64 *values; |
3dab77fb | 4162 | |
fa8c2693 | 4163 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4164 | |
fa8c2693 PZ |
4165 | values = kzalloc(event->read_size, GFP_KERNEL); |
4166 | if (!values) | |
4167 | return -ENOMEM; | |
3dab77fb | 4168 | |
fa8c2693 PZ |
4169 | values[0] = 1 + leader->nr_siblings; |
4170 | ||
4171 | /* | |
4172 | * By locking the child_mutex of the leader we effectively | |
4173 | * lock the child list of all siblings.. XXX explain how. | |
4174 | */ | |
4175 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4176 | |
7d88962e SB |
4177 | ret = __perf_read_group_add(leader, read_format, values); |
4178 | if (ret) | |
4179 | goto unlock; | |
4180 | ||
4181 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4182 | ret = __perf_read_group_add(child, read_format, values); | |
4183 | if (ret) | |
4184 | goto unlock; | |
4185 | } | |
abf4868b | 4186 | |
fa8c2693 | 4187 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4188 | |
7d88962e | 4189 | ret = event->read_size; |
fa8c2693 PZ |
4190 | if (copy_to_user(buf, values, event->read_size)) |
4191 | ret = -EFAULT; | |
7d88962e | 4192 | goto out; |
fa8c2693 | 4193 | |
7d88962e SB |
4194 | unlock: |
4195 | mutex_unlock(&leader->child_mutex); | |
4196 | out: | |
fa8c2693 | 4197 | kfree(values); |
abf4868b | 4198 | return ret; |
3dab77fb PZ |
4199 | } |
4200 | ||
b15f495b | 4201 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4202 | u64 read_format, char __user *buf) |
4203 | { | |
59ed446f | 4204 | u64 enabled, running; |
3dab77fb PZ |
4205 | u64 values[4]; |
4206 | int n = 0; | |
4207 | ||
59ed446f PZ |
4208 | values[n++] = perf_event_read_value(event, &enabled, &running); |
4209 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
4210 | values[n++] = enabled; | |
4211 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4212 | values[n++] = running; | |
3dab77fb | 4213 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4214 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4215 | |
4216 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4217 | return -EFAULT; | |
4218 | ||
4219 | return n * sizeof(u64); | |
4220 | } | |
4221 | ||
dc633982 JO |
4222 | static bool is_event_hup(struct perf_event *event) |
4223 | { | |
4224 | bool no_children; | |
4225 | ||
a69b0ca4 | 4226 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4227 | return false; |
4228 | ||
4229 | mutex_lock(&event->child_mutex); | |
4230 | no_children = list_empty(&event->child_list); | |
4231 | mutex_unlock(&event->child_mutex); | |
4232 | return no_children; | |
4233 | } | |
4234 | ||
0793a61d | 4235 | /* |
cdd6c482 | 4236 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4237 | */ |
4238 | static ssize_t | |
b15f495b | 4239 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4240 | { |
cdd6c482 | 4241 | u64 read_format = event->attr.read_format; |
3dab77fb | 4242 | int ret; |
0793a61d | 4243 | |
3b6f9e5c | 4244 | /* |
cdd6c482 | 4245 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4246 | * error state (i.e. because it was pinned but it couldn't be |
4247 | * scheduled on to the CPU at some point). | |
4248 | */ | |
cdd6c482 | 4249 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4250 | return 0; |
4251 | ||
c320c7b7 | 4252 | if (count < event->read_size) |
3dab77fb PZ |
4253 | return -ENOSPC; |
4254 | ||
cdd6c482 | 4255 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4256 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4257 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4258 | else |
b15f495b | 4259 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4260 | |
3dab77fb | 4261 | return ret; |
0793a61d TG |
4262 | } |
4263 | ||
0793a61d TG |
4264 | static ssize_t |
4265 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4266 | { | |
cdd6c482 | 4267 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4268 | struct perf_event_context *ctx; |
4269 | int ret; | |
0793a61d | 4270 | |
f63a8daa | 4271 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4272 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4273 | perf_event_ctx_unlock(event, ctx); |
4274 | ||
4275 | return ret; | |
0793a61d TG |
4276 | } |
4277 | ||
4278 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
4279 | { | |
cdd6c482 | 4280 | struct perf_event *event = file->private_data; |
76369139 | 4281 | struct ring_buffer *rb; |
61b67684 | 4282 | unsigned int events = POLLHUP; |
c7138f37 | 4283 | |
e708d7ad | 4284 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4285 | |
dc633982 | 4286 | if (is_event_hup(event)) |
179033b3 | 4287 | return events; |
c7138f37 | 4288 | |
10c6db11 | 4289 | /* |
9bb5d40c PZ |
4290 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4291 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4292 | */ |
4293 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4294 | rb = event->rb; |
4295 | if (rb) | |
76369139 | 4296 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4297 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4298 | return events; |
4299 | } | |
4300 | ||
f63a8daa | 4301 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4302 | { |
7d88962e | 4303 | (void)perf_event_read(event, false); |
e7850595 | 4304 | local64_set(&event->count, 0); |
cdd6c482 | 4305 | perf_event_update_userpage(event); |
3df5edad PZ |
4306 | } |
4307 | ||
c93f7669 | 4308 | /* |
cdd6c482 IM |
4309 | * Holding the top-level event's child_mutex means that any |
4310 | * descendant process that has inherited this event will block | |
8ba289b8 | 4311 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4312 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4313 | */ |
cdd6c482 IM |
4314 | static void perf_event_for_each_child(struct perf_event *event, |
4315 | void (*func)(struct perf_event *)) | |
3df5edad | 4316 | { |
cdd6c482 | 4317 | struct perf_event *child; |
3df5edad | 4318 | |
cdd6c482 | 4319 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4320 | |
cdd6c482 IM |
4321 | mutex_lock(&event->child_mutex); |
4322 | func(event); | |
4323 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4324 | func(child); |
cdd6c482 | 4325 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4326 | } |
4327 | ||
cdd6c482 IM |
4328 | static void perf_event_for_each(struct perf_event *event, |
4329 | void (*func)(struct perf_event *)) | |
3df5edad | 4330 | { |
cdd6c482 IM |
4331 | struct perf_event_context *ctx = event->ctx; |
4332 | struct perf_event *sibling; | |
3df5edad | 4333 | |
f63a8daa PZ |
4334 | lockdep_assert_held(&ctx->mutex); |
4335 | ||
cdd6c482 | 4336 | event = event->group_leader; |
75f937f2 | 4337 | |
cdd6c482 | 4338 | perf_event_for_each_child(event, func); |
cdd6c482 | 4339 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
724b6daa | 4340 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4341 | } |
4342 | ||
fae3fde6 PZ |
4343 | static void __perf_event_period(struct perf_event *event, |
4344 | struct perf_cpu_context *cpuctx, | |
4345 | struct perf_event_context *ctx, | |
4346 | void *info) | |
c7999c6f | 4347 | { |
fae3fde6 | 4348 | u64 value = *((u64 *)info); |
c7999c6f | 4349 | bool active; |
08247e31 | 4350 | |
cdd6c482 | 4351 | if (event->attr.freq) { |
cdd6c482 | 4352 | event->attr.sample_freq = value; |
08247e31 | 4353 | } else { |
cdd6c482 IM |
4354 | event->attr.sample_period = value; |
4355 | event->hw.sample_period = value; | |
08247e31 | 4356 | } |
bad7192b PZ |
4357 | |
4358 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4359 | if (active) { | |
4360 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4361 | /* |
4362 | * We could be throttled; unthrottle now to avoid the tick | |
4363 | * trying to unthrottle while we already re-started the event. | |
4364 | */ | |
4365 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4366 | event->hw.interrupts = 0; | |
4367 | perf_log_throttle(event, 1); | |
4368 | } | |
bad7192b PZ |
4369 | event->pmu->stop(event, PERF_EF_UPDATE); |
4370 | } | |
4371 | ||
4372 | local64_set(&event->hw.period_left, 0); | |
4373 | ||
4374 | if (active) { | |
4375 | event->pmu->start(event, PERF_EF_RELOAD); | |
4376 | perf_pmu_enable(ctx->pmu); | |
4377 | } | |
c7999c6f PZ |
4378 | } |
4379 | ||
4380 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4381 | { | |
c7999c6f PZ |
4382 | u64 value; |
4383 | ||
4384 | if (!is_sampling_event(event)) | |
4385 | return -EINVAL; | |
4386 | ||
4387 | if (copy_from_user(&value, arg, sizeof(value))) | |
4388 | return -EFAULT; | |
4389 | ||
4390 | if (!value) | |
4391 | return -EINVAL; | |
4392 | ||
4393 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4394 | return -EINVAL; | |
4395 | ||
fae3fde6 | 4396 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4397 | |
c7999c6f | 4398 | return 0; |
08247e31 PZ |
4399 | } |
4400 | ||
ac9721f3 PZ |
4401 | static const struct file_operations perf_fops; |
4402 | ||
2903ff01 | 4403 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4404 | { |
2903ff01 AV |
4405 | struct fd f = fdget(fd); |
4406 | if (!f.file) | |
4407 | return -EBADF; | |
ac9721f3 | 4408 | |
2903ff01 AV |
4409 | if (f.file->f_op != &perf_fops) { |
4410 | fdput(f); | |
4411 | return -EBADF; | |
ac9721f3 | 4412 | } |
2903ff01 AV |
4413 | *p = f; |
4414 | return 0; | |
ac9721f3 PZ |
4415 | } |
4416 | ||
4417 | static int perf_event_set_output(struct perf_event *event, | |
4418 | struct perf_event *output_event); | |
6fb2915d | 4419 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4420 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4421 | |
f63a8daa | 4422 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4423 | { |
cdd6c482 | 4424 | void (*func)(struct perf_event *); |
3df5edad | 4425 | u32 flags = arg; |
d859e29f PM |
4426 | |
4427 | switch (cmd) { | |
cdd6c482 | 4428 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4429 | func = _perf_event_enable; |
d859e29f | 4430 | break; |
cdd6c482 | 4431 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4432 | func = _perf_event_disable; |
79f14641 | 4433 | break; |
cdd6c482 | 4434 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4435 | func = _perf_event_reset; |
6de6a7b9 | 4436 | break; |
3df5edad | 4437 | |
cdd6c482 | 4438 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4439 | return _perf_event_refresh(event, arg); |
08247e31 | 4440 | |
cdd6c482 IM |
4441 | case PERF_EVENT_IOC_PERIOD: |
4442 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4443 | |
cf4957f1 JO |
4444 | case PERF_EVENT_IOC_ID: |
4445 | { | |
4446 | u64 id = primary_event_id(event); | |
4447 | ||
4448 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4449 | return -EFAULT; | |
4450 | return 0; | |
4451 | } | |
4452 | ||
cdd6c482 | 4453 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4454 | { |
ac9721f3 | 4455 | int ret; |
ac9721f3 | 4456 | if (arg != -1) { |
2903ff01 AV |
4457 | struct perf_event *output_event; |
4458 | struct fd output; | |
4459 | ret = perf_fget_light(arg, &output); | |
4460 | if (ret) | |
4461 | return ret; | |
4462 | output_event = output.file->private_data; | |
4463 | ret = perf_event_set_output(event, output_event); | |
4464 | fdput(output); | |
4465 | } else { | |
4466 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 4467 | } |
ac9721f3 PZ |
4468 | return ret; |
4469 | } | |
a4be7c27 | 4470 | |
6fb2915d LZ |
4471 | case PERF_EVENT_IOC_SET_FILTER: |
4472 | return perf_event_set_filter(event, (void __user *)arg); | |
4473 | ||
2541517c AS |
4474 | case PERF_EVENT_IOC_SET_BPF: |
4475 | return perf_event_set_bpf_prog(event, arg); | |
4476 | ||
86e7972f WN |
4477 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
4478 | struct ring_buffer *rb; | |
4479 | ||
4480 | rcu_read_lock(); | |
4481 | rb = rcu_dereference(event->rb); | |
4482 | if (!rb || !rb->nr_pages) { | |
4483 | rcu_read_unlock(); | |
4484 | return -EINVAL; | |
4485 | } | |
4486 | rb_toggle_paused(rb, !!arg); | |
4487 | rcu_read_unlock(); | |
4488 | return 0; | |
4489 | } | |
d859e29f | 4490 | default: |
3df5edad | 4491 | return -ENOTTY; |
d859e29f | 4492 | } |
3df5edad PZ |
4493 | |
4494 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 4495 | perf_event_for_each(event, func); |
3df5edad | 4496 | else |
cdd6c482 | 4497 | perf_event_for_each_child(event, func); |
3df5edad PZ |
4498 | |
4499 | return 0; | |
d859e29f PM |
4500 | } |
4501 | ||
f63a8daa PZ |
4502 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4503 | { | |
4504 | struct perf_event *event = file->private_data; | |
4505 | struct perf_event_context *ctx; | |
4506 | long ret; | |
4507 | ||
4508 | ctx = perf_event_ctx_lock(event); | |
4509 | ret = _perf_ioctl(event, cmd, arg); | |
4510 | perf_event_ctx_unlock(event, ctx); | |
4511 | ||
4512 | return ret; | |
4513 | } | |
4514 | ||
b3f20785 PM |
4515 | #ifdef CONFIG_COMPAT |
4516 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
4517 | unsigned long arg) | |
4518 | { | |
4519 | switch (_IOC_NR(cmd)) { | |
4520 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
4521 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
4522 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
4523 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
4524 | cmd &= ~IOCSIZE_MASK; | |
4525 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
4526 | } | |
4527 | break; | |
4528 | } | |
4529 | return perf_ioctl(file, cmd, arg); | |
4530 | } | |
4531 | #else | |
4532 | # define perf_compat_ioctl NULL | |
4533 | #endif | |
4534 | ||
cdd6c482 | 4535 | int perf_event_task_enable(void) |
771d7cde | 4536 | { |
f63a8daa | 4537 | struct perf_event_context *ctx; |
cdd6c482 | 4538 | struct perf_event *event; |
771d7cde | 4539 | |
cdd6c482 | 4540 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4541 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4542 | ctx = perf_event_ctx_lock(event); | |
4543 | perf_event_for_each_child(event, _perf_event_enable); | |
4544 | perf_event_ctx_unlock(event, ctx); | |
4545 | } | |
cdd6c482 | 4546 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4547 | |
4548 | return 0; | |
4549 | } | |
4550 | ||
cdd6c482 | 4551 | int perf_event_task_disable(void) |
771d7cde | 4552 | { |
f63a8daa | 4553 | struct perf_event_context *ctx; |
cdd6c482 | 4554 | struct perf_event *event; |
771d7cde | 4555 | |
cdd6c482 | 4556 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4557 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4558 | ctx = perf_event_ctx_lock(event); | |
4559 | perf_event_for_each_child(event, _perf_event_disable); | |
4560 | perf_event_ctx_unlock(event, ctx); | |
4561 | } | |
cdd6c482 | 4562 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4563 | |
4564 | return 0; | |
4565 | } | |
4566 | ||
cdd6c482 | 4567 | static int perf_event_index(struct perf_event *event) |
194002b2 | 4568 | { |
a4eaf7f1 PZ |
4569 | if (event->hw.state & PERF_HES_STOPPED) |
4570 | return 0; | |
4571 | ||
cdd6c482 | 4572 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
4573 | return 0; |
4574 | ||
35edc2a5 | 4575 | return event->pmu->event_idx(event); |
194002b2 PZ |
4576 | } |
4577 | ||
c4794295 | 4578 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 4579 | u64 *now, |
7f310a5d EM |
4580 | u64 *enabled, |
4581 | u64 *running) | |
c4794295 | 4582 | { |
e3f3541c | 4583 | u64 ctx_time; |
c4794295 | 4584 | |
e3f3541c PZ |
4585 | *now = perf_clock(); |
4586 | ctx_time = event->shadow_ctx_time + *now; | |
c4794295 EM |
4587 | *enabled = ctx_time - event->tstamp_enabled; |
4588 | *running = ctx_time - event->tstamp_running; | |
4589 | } | |
4590 | ||
fa731587 PZ |
4591 | static void perf_event_init_userpage(struct perf_event *event) |
4592 | { | |
4593 | struct perf_event_mmap_page *userpg; | |
4594 | struct ring_buffer *rb; | |
4595 | ||
4596 | rcu_read_lock(); | |
4597 | rb = rcu_dereference(event->rb); | |
4598 | if (!rb) | |
4599 | goto unlock; | |
4600 | ||
4601 | userpg = rb->user_page; | |
4602 | ||
4603 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
4604 | userpg->cap_bit0_is_deprecated = 1; | |
4605 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
4606 | userpg->data_offset = PAGE_SIZE; |
4607 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
4608 | |
4609 | unlock: | |
4610 | rcu_read_unlock(); | |
4611 | } | |
4612 | ||
c1317ec2 AL |
4613 | void __weak arch_perf_update_userpage( |
4614 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
4615 | { |
4616 | } | |
4617 | ||
38ff667b PZ |
4618 | /* |
4619 | * Callers need to ensure there can be no nesting of this function, otherwise | |
4620 | * the seqlock logic goes bad. We can not serialize this because the arch | |
4621 | * code calls this from NMI context. | |
4622 | */ | |
cdd6c482 | 4623 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 4624 | { |
cdd6c482 | 4625 | struct perf_event_mmap_page *userpg; |
76369139 | 4626 | struct ring_buffer *rb; |
e3f3541c | 4627 | u64 enabled, running, now; |
38ff667b PZ |
4628 | |
4629 | rcu_read_lock(); | |
5ec4c599 PZ |
4630 | rb = rcu_dereference(event->rb); |
4631 | if (!rb) | |
4632 | goto unlock; | |
4633 | ||
0d641208 EM |
4634 | /* |
4635 | * compute total_time_enabled, total_time_running | |
4636 | * based on snapshot values taken when the event | |
4637 | * was last scheduled in. | |
4638 | * | |
4639 | * we cannot simply called update_context_time() | |
4640 | * because of locking issue as we can be called in | |
4641 | * NMI context | |
4642 | */ | |
e3f3541c | 4643 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 4644 | |
76369139 | 4645 | userpg = rb->user_page; |
7b732a75 PZ |
4646 | /* |
4647 | * Disable preemption so as to not let the corresponding user-space | |
4648 | * spin too long if we get preempted. | |
4649 | */ | |
4650 | preempt_disable(); | |
37d81828 | 4651 | ++userpg->lock; |
92f22a38 | 4652 | barrier(); |
cdd6c482 | 4653 | userpg->index = perf_event_index(event); |
b5e58793 | 4654 | userpg->offset = perf_event_count(event); |
365a4038 | 4655 | if (userpg->index) |
e7850595 | 4656 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 4657 | |
0d641208 | 4658 | userpg->time_enabled = enabled + |
cdd6c482 | 4659 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 4660 | |
0d641208 | 4661 | userpg->time_running = running + |
cdd6c482 | 4662 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 4663 | |
c1317ec2 | 4664 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 4665 | |
92f22a38 | 4666 | barrier(); |
37d81828 | 4667 | ++userpg->lock; |
7b732a75 | 4668 | preempt_enable(); |
38ff667b | 4669 | unlock: |
7b732a75 | 4670 | rcu_read_unlock(); |
37d81828 PM |
4671 | } |
4672 | ||
906010b2 PZ |
4673 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
4674 | { | |
4675 | struct perf_event *event = vma->vm_file->private_data; | |
76369139 | 4676 | struct ring_buffer *rb; |
906010b2 PZ |
4677 | int ret = VM_FAULT_SIGBUS; |
4678 | ||
4679 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
4680 | if (vmf->pgoff == 0) | |
4681 | ret = 0; | |
4682 | return ret; | |
4683 | } | |
4684 | ||
4685 | rcu_read_lock(); | |
76369139 FW |
4686 | rb = rcu_dereference(event->rb); |
4687 | if (!rb) | |
906010b2 PZ |
4688 | goto unlock; |
4689 | ||
4690 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
4691 | goto unlock; | |
4692 | ||
76369139 | 4693 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
4694 | if (!vmf->page) |
4695 | goto unlock; | |
4696 | ||
4697 | get_page(vmf->page); | |
4698 | vmf->page->mapping = vma->vm_file->f_mapping; | |
4699 | vmf->page->index = vmf->pgoff; | |
4700 | ||
4701 | ret = 0; | |
4702 | unlock: | |
4703 | rcu_read_unlock(); | |
4704 | ||
4705 | return ret; | |
4706 | } | |
4707 | ||
10c6db11 PZ |
4708 | static void ring_buffer_attach(struct perf_event *event, |
4709 | struct ring_buffer *rb) | |
4710 | { | |
b69cf536 | 4711 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
4712 | unsigned long flags; |
4713 | ||
b69cf536 PZ |
4714 | if (event->rb) { |
4715 | /* | |
4716 | * Should be impossible, we set this when removing | |
4717 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
4718 | */ | |
4719 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 4720 | |
b69cf536 | 4721 | old_rb = event->rb; |
b69cf536 PZ |
4722 | spin_lock_irqsave(&old_rb->event_lock, flags); |
4723 | list_del_rcu(&event->rb_entry); | |
4724 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 4725 | |
2f993cf0 ON |
4726 | event->rcu_batches = get_state_synchronize_rcu(); |
4727 | event->rcu_pending = 1; | |
b69cf536 | 4728 | } |
10c6db11 | 4729 | |
b69cf536 | 4730 | if (rb) { |
2f993cf0 ON |
4731 | if (event->rcu_pending) { |
4732 | cond_synchronize_rcu(event->rcu_batches); | |
4733 | event->rcu_pending = 0; | |
4734 | } | |
4735 | ||
b69cf536 PZ |
4736 | spin_lock_irqsave(&rb->event_lock, flags); |
4737 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
4738 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
4739 | } | |
4740 | ||
4741 | rcu_assign_pointer(event->rb, rb); | |
4742 | ||
4743 | if (old_rb) { | |
4744 | ring_buffer_put(old_rb); | |
4745 | /* | |
4746 | * Since we detached before setting the new rb, so that we | |
4747 | * could attach the new rb, we could have missed a wakeup. | |
4748 | * Provide it now. | |
4749 | */ | |
4750 | wake_up_all(&event->waitq); | |
4751 | } | |
10c6db11 PZ |
4752 | } |
4753 | ||
4754 | static void ring_buffer_wakeup(struct perf_event *event) | |
4755 | { | |
4756 | struct ring_buffer *rb; | |
4757 | ||
4758 | rcu_read_lock(); | |
4759 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
4760 | if (rb) { |
4761 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
4762 | wake_up_all(&event->waitq); | |
4763 | } | |
10c6db11 PZ |
4764 | rcu_read_unlock(); |
4765 | } | |
4766 | ||
fdc26706 | 4767 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 4768 | { |
76369139 | 4769 | struct ring_buffer *rb; |
7b732a75 | 4770 | |
ac9721f3 | 4771 | rcu_read_lock(); |
76369139 FW |
4772 | rb = rcu_dereference(event->rb); |
4773 | if (rb) { | |
4774 | if (!atomic_inc_not_zero(&rb->refcount)) | |
4775 | rb = NULL; | |
ac9721f3 PZ |
4776 | } |
4777 | rcu_read_unlock(); | |
4778 | ||
76369139 | 4779 | return rb; |
ac9721f3 PZ |
4780 | } |
4781 | ||
fdc26706 | 4782 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 4783 | { |
76369139 | 4784 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 4785 | return; |
7b732a75 | 4786 | |
9bb5d40c | 4787 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 4788 | |
76369139 | 4789 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
4790 | } |
4791 | ||
4792 | static void perf_mmap_open(struct vm_area_struct *vma) | |
4793 | { | |
cdd6c482 | 4794 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4795 | |
cdd6c482 | 4796 | atomic_inc(&event->mmap_count); |
9bb5d40c | 4797 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 4798 | |
45bfb2e5 PZ |
4799 | if (vma->vm_pgoff) |
4800 | atomic_inc(&event->rb->aux_mmap_count); | |
4801 | ||
1e0fb9ec AL |
4802 | if (event->pmu->event_mapped) |
4803 | event->pmu->event_mapped(event); | |
7b732a75 PZ |
4804 | } |
4805 | ||
95ff4ca2 AS |
4806 | static void perf_pmu_output_stop(struct perf_event *event); |
4807 | ||
9bb5d40c PZ |
4808 | /* |
4809 | * A buffer can be mmap()ed multiple times; either directly through the same | |
4810 | * event, or through other events by use of perf_event_set_output(). | |
4811 | * | |
4812 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
4813 | * the buffer here, where we still have a VM context. This means we need | |
4814 | * to detach all events redirecting to us. | |
4815 | */ | |
7b732a75 PZ |
4816 | static void perf_mmap_close(struct vm_area_struct *vma) |
4817 | { | |
cdd6c482 | 4818 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4819 | |
b69cf536 | 4820 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
4821 | struct user_struct *mmap_user = rb->mmap_user; |
4822 | int mmap_locked = rb->mmap_locked; | |
4823 | unsigned long size = perf_data_size(rb); | |
789f90fc | 4824 | |
1e0fb9ec AL |
4825 | if (event->pmu->event_unmapped) |
4826 | event->pmu->event_unmapped(event); | |
4827 | ||
45bfb2e5 PZ |
4828 | /* |
4829 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
4830 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
4831 | * serialize with perf_mmap here. | |
4832 | */ | |
4833 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
4834 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
4835 | /* |
4836 | * Stop all AUX events that are writing to this buffer, | |
4837 | * so that we can free its AUX pages and corresponding PMU | |
4838 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
4839 | * they won't start any more (see perf_aux_output_begin()). | |
4840 | */ | |
4841 | perf_pmu_output_stop(event); | |
4842 | ||
4843 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
4844 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
4845 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
4846 | ||
95ff4ca2 | 4847 | /* this has to be the last one */ |
45bfb2e5 | 4848 | rb_free_aux(rb); |
95ff4ca2 AS |
4849 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
4850 | ||
45bfb2e5 PZ |
4851 | mutex_unlock(&event->mmap_mutex); |
4852 | } | |
4853 | ||
9bb5d40c PZ |
4854 | atomic_dec(&rb->mmap_count); |
4855 | ||
4856 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 4857 | goto out_put; |
9bb5d40c | 4858 | |
b69cf536 | 4859 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
4860 | mutex_unlock(&event->mmap_mutex); |
4861 | ||
4862 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
4863 | if (atomic_read(&rb->mmap_count)) |
4864 | goto out_put; | |
ac9721f3 | 4865 | |
9bb5d40c PZ |
4866 | /* |
4867 | * No other mmap()s, detach from all other events that might redirect | |
4868 | * into the now unreachable buffer. Somewhat complicated by the | |
4869 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
4870 | */ | |
4871 | again: | |
4872 | rcu_read_lock(); | |
4873 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
4874 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
4875 | /* | |
4876 | * This event is en-route to free_event() which will | |
4877 | * detach it and remove it from the list. | |
4878 | */ | |
4879 | continue; | |
4880 | } | |
4881 | rcu_read_unlock(); | |
789f90fc | 4882 | |
9bb5d40c PZ |
4883 | mutex_lock(&event->mmap_mutex); |
4884 | /* | |
4885 | * Check we didn't race with perf_event_set_output() which can | |
4886 | * swizzle the rb from under us while we were waiting to | |
4887 | * acquire mmap_mutex. | |
4888 | * | |
4889 | * If we find a different rb; ignore this event, a next | |
4890 | * iteration will no longer find it on the list. We have to | |
4891 | * still restart the iteration to make sure we're not now | |
4892 | * iterating the wrong list. | |
4893 | */ | |
b69cf536 PZ |
4894 | if (event->rb == rb) |
4895 | ring_buffer_attach(event, NULL); | |
4896 | ||
cdd6c482 | 4897 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 4898 | put_event(event); |
ac9721f3 | 4899 | |
9bb5d40c PZ |
4900 | /* |
4901 | * Restart the iteration; either we're on the wrong list or | |
4902 | * destroyed its integrity by doing a deletion. | |
4903 | */ | |
4904 | goto again; | |
7b732a75 | 4905 | } |
9bb5d40c PZ |
4906 | rcu_read_unlock(); |
4907 | ||
4908 | /* | |
4909 | * It could be there's still a few 0-ref events on the list; they'll | |
4910 | * get cleaned up by free_event() -- they'll also still have their | |
4911 | * ref on the rb and will free it whenever they are done with it. | |
4912 | * | |
4913 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
4914 | * undo the VM accounting. | |
4915 | */ | |
4916 | ||
4917 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
4918 | vma->vm_mm->pinned_vm -= mmap_locked; | |
4919 | free_uid(mmap_user); | |
4920 | ||
b69cf536 | 4921 | out_put: |
9bb5d40c | 4922 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
4923 | } |
4924 | ||
f0f37e2f | 4925 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 4926 | .open = perf_mmap_open, |
45bfb2e5 | 4927 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
4928 | .fault = perf_mmap_fault, |
4929 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
4930 | }; |
4931 | ||
4932 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
4933 | { | |
cdd6c482 | 4934 | struct perf_event *event = file->private_data; |
22a4f650 | 4935 | unsigned long user_locked, user_lock_limit; |
789f90fc | 4936 | struct user_struct *user = current_user(); |
22a4f650 | 4937 | unsigned long locked, lock_limit; |
45bfb2e5 | 4938 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
4939 | unsigned long vma_size; |
4940 | unsigned long nr_pages; | |
45bfb2e5 | 4941 | long user_extra = 0, extra = 0; |
d57e34fd | 4942 | int ret = 0, flags = 0; |
37d81828 | 4943 | |
c7920614 PZ |
4944 | /* |
4945 | * Don't allow mmap() of inherited per-task counters. This would | |
4946 | * create a performance issue due to all children writing to the | |
76369139 | 4947 | * same rb. |
c7920614 PZ |
4948 | */ |
4949 | if (event->cpu == -1 && event->attr.inherit) | |
4950 | return -EINVAL; | |
4951 | ||
43a21ea8 | 4952 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 4953 | return -EINVAL; |
7b732a75 PZ |
4954 | |
4955 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
4956 | |
4957 | if (vma->vm_pgoff == 0) { | |
4958 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
4959 | } else { | |
4960 | /* | |
4961 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
4962 | * mapped, all subsequent mappings should have the same size | |
4963 | * and offset. Must be above the normal perf buffer. | |
4964 | */ | |
4965 | u64 aux_offset, aux_size; | |
4966 | ||
4967 | if (!event->rb) | |
4968 | return -EINVAL; | |
4969 | ||
4970 | nr_pages = vma_size / PAGE_SIZE; | |
4971 | ||
4972 | mutex_lock(&event->mmap_mutex); | |
4973 | ret = -EINVAL; | |
4974 | ||
4975 | rb = event->rb; | |
4976 | if (!rb) | |
4977 | goto aux_unlock; | |
4978 | ||
4979 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); | |
4980 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); | |
4981 | ||
4982 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
4983 | goto aux_unlock; | |
4984 | ||
4985 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
4986 | goto aux_unlock; | |
4987 | ||
4988 | /* already mapped with a different offset */ | |
4989 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
4990 | goto aux_unlock; | |
4991 | ||
4992 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
4993 | goto aux_unlock; | |
4994 | ||
4995 | /* already mapped with a different size */ | |
4996 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
4997 | goto aux_unlock; | |
4998 | ||
4999 | if (!is_power_of_2(nr_pages)) | |
5000 | goto aux_unlock; | |
5001 | ||
5002 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
5003 | goto aux_unlock; | |
5004 | ||
5005 | if (rb_has_aux(rb)) { | |
5006 | atomic_inc(&rb->aux_mmap_count); | |
5007 | ret = 0; | |
5008 | goto unlock; | |
5009 | } | |
5010 | ||
5011 | atomic_set(&rb->aux_mmap_count, 1); | |
5012 | user_extra = nr_pages; | |
5013 | ||
5014 | goto accounting; | |
5015 | } | |
7b732a75 | 5016 | |
7730d865 | 5017 | /* |
76369139 | 5018 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
5019 | * can do bitmasks instead of modulo. |
5020 | */ | |
2ed11312 | 5021 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
5022 | return -EINVAL; |
5023 | ||
7b732a75 | 5024 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
5025 | return -EINVAL; |
5026 | ||
cdd6c482 | 5027 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 5028 | again: |
cdd6c482 | 5029 | mutex_lock(&event->mmap_mutex); |
76369139 | 5030 | if (event->rb) { |
9bb5d40c | 5031 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 5032 | ret = -EINVAL; |
9bb5d40c PZ |
5033 | goto unlock; |
5034 | } | |
5035 | ||
5036 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
5037 | /* | |
5038 | * Raced against perf_mmap_close() through | |
5039 | * perf_event_set_output(). Try again, hope for better | |
5040 | * luck. | |
5041 | */ | |
5042 | mutex_unlock(&event->mmap_mutex); | |
5043 | goto again; | |
5044 | } | |
5045 | ||
ebb3c4c4 PZ |
5046 | goto unlock; |
5047 | } | |
5048 | ||
789f90fc | 5049 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
5050 | |
5051 | accounting: | |
cdd6c482 | 5052 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
5053 | |
5054 | /* | |
5055 | * Increase the limit linearly with more CPUs: | |
5056 | */ | |
5057 | user_lock_limit *= num_online_cpus(); | |
5058 | ||
789f90fc | 5059 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 5060 | |
789f90fc PZ |
5061 | if (user_locked > user_lock_limit) |
5062 | extra = user_locked - user_lock_limit; | |
7b732a75 | 5063 | |
78d7d407 | 5064 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 5065 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 5066 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 5067 | |
459ec28a IM |
5068 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
5069 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
5070 | ret = -EPERM; |
5071 | goto unlock; | |
5072 | } | |
7b732a75 | 5073 | |
45bfb2e5 | 5074 | WARN_ON(!rb && event->rb); |
906010b2 | 5075 | |
d57e34fd | 5076 | if (vma->vm_flags & VM_WRITE) |
76369139 | 5077 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 5078 | |
76369139 | 5079 | if (!rb) { |
45bfb2e5 PZ |
5080 | rb = rb_alloc(nr_pages, |
5081 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
5082 | event->cpu, flags); | |
26cb63ad | 5083 | |
45bfb2e5 PZ |
5084 | if (!rb) { |
5085 | ret = -ENOMEM; | |
5086 | goto unlock; | |
5087 | } | |
43a21ea8 | 5088 | |
45bfb2e5 PZ |
5089 | atomic_set(&rb->mmap_count, 1); |
5090 | rb->mmap_user = get_current_user(); | |
5091 | rb->mmap_locked = extra; | |
26cb63ad | 5092 | |
45bfb2e5 | 5093 | ring_buffer_attach(event, rb); |
ac9721f3 | 5094 | |
45bfb2e5 PZ |
5095 | perf_event_init_userpage(event); |
5096 | perf_event_update_userpage(event); | |
5097 | } else { | |
1a594131 AS |
5098 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5099 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5100 | if (!ret) |
5101 | rb->aux_mmap_locked = extra; | |
5102 | } | |
9a0f05cb | 5103 | |
ebb3c4c4 | 5104 | unlock: |
45bfb2e5 PZ |
5105 | if (!ret) { |
5106 | atomic_long_add(user_extra, &user->locked_vm); | |
5107 | vma->vm_mm->pinned_vm += extra; | |
5108 | ||
ac9721f3 | 5109 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5110 | } else if (rb) { |
5111 | atomic_dec(&rb->mmap_count); | |
5112 | } | |
5113 | aux_unlock: | |
cdd6c482 | 5114 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5115 | |
9bb5d40c PZ |
5116 | /* |
5117 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5118 | * vma. | |
5119 | */ | |
26cb63ad | 5120 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5121 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5122 | |
1e0fb9ec AL |
5123 | if (event->pmu->event_mapped) |
5124 | event->pmu->event_mapped(event); | |
5125 | ||
7b732a75 | 5126 | return ret; |
37d81828 PM |
5127 | } |
5128 | ||
3c446b3d PZ |
5129 | static int perf_fasync(int fd, struct file *filp, int on) |
5130 | { | |
496ad9aa | 5131 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5132 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5133 | int retval; |
5134 | ||
5955102c | 5135 | inode_lock(inode); |
cdd6c482 | 5136 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5137 | inode_unlock(inode); |
3c446b3d PZ |
5138 | |
5139 | if (retval < 0) | |
5140 | return retval; | |
5141 | ||
5142 | return 0; | |
5143 | } | |
5144 | ||
0793a61d | 5145 | static const struct file_operations perf_fops = { |
3326c1ce | 5146 | .llseek = no_llseek, |
0793a61d TG |
5147 | .release = perf_release, |
5148 | .read = perf_read, | |
5149 | .poll = perf_poll, | |
d859e29f | 5150 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5151 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5152 | .mmap = perf_mmap, |
3c446b3d | 5153 | .fasync = perf_fasync, |
0793a61d TG |
5154 | }; |
5155 | ||
925d519a | 5156 | /* |
cdd6c482 | 5157 | * Perf event wakeup |
925d519a PZ |
5158 | * |
5159 | * If there's data, ensure we set the poll() state and publish everything | |
5160 | * to user-space before waking everybody up. | |
5161 | */ | |
5162 | ||
fed66e2c PZ |
5163 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5164 | { | |
5165 | /* only the parent has fasync state */ | |
5166 | if (event->parent) | |
5167 | event = event->parent; | |
5168 | return &event->fasync; | |
5169 | } | |
5170 | ||
cdd6c482 | 5171 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5172 | { |
10c6db11 | 5173 | ring_buffer_wakeup(event); |
4c9e2542 | 5174 | |
cdd6c482 | 5175 | if (event->pending_kill) { |
fed66e2c | 5176 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5177 | event->pending_kill = 0; |
4c9e2542 | 5178 | } |
925d519a PZ |
5179 | } |
5180 | ||
e360adbe | 5181 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5182 | { |
cdd6c482 IM |
5183 | struct perf_event *event = container_of(entry, |
5184 | struct perf_event, pending); | |
d525211f PZ |
5185 | int rctx; |
5186 | ||
5187 | rctx = perf_swevent_get_recursion_context(); | |
5188 | /* | |
5189 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5190 | * and we won't recurse 'further'. | |
5191 | */ | |
79f14641 | 5192 | |
cdd6c482 IM |
5193 | if (event->pending_disable) { |
5194 | event->pending_disable = 0; | |
fae3fde6 | 5195 | perf_event_disable_local(event); |
79f14641 PZ |
5196 | } |
5197 | ||
cdd6c482 IM |
5198 | if (event->pending_wakeup) { |
5199 | event->pending_wakeup = 0; | |
5200 | perf_event_wakeup(event); | |
79f14641 | 5201 | } |
d525211f PZ |
5202 | |
5203 | if (rctx >= 0) | |
5204 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5205 | } |
5206 | ||
39447b38 ZY |
5207 | /* |
5208 | * We assume there is only KVM supporting the callbacks. | |
5209 | * Later on, we might change it to a list if there is | |
5210 | * another virtualization implementation supporting the callbacks. | |
5211 | */ | |
5212 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5213 | ||
5214 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5215 | { | |
5216 | perf_guest_cbs = cbs; | |
5217 | return 0; | |
5218 | } | |
5219 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5220 | ||
5221 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5222 | { | |
5223 | perf_guest_cbs = NULL; | |
5224 | return 0; | |
5225 | } | |
5226 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5227 | ||
4018994f JO |
5228 | static void |
5229 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5230 | struct pt_regs *regs, u64 mask) | |
5231 | { | |
5232 | int bit; | |
5233 | ||
5234 | for_each_set_bit(bit, (const unsigned long *) &mask, | |
5235 | sizeof(mask) * BITS_PER_BYTE) { | |
5236 | u64 val; | |
5237 | ||
5238 | val = perf_reg_value(regs, bit); | |
5239 | perf_output_put(handle, val); | |
5240 | } | |
5241 | } | |
5242 | ||
60e2364e | 5243 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5244 | struct pt_regs *regs, |
5245 | struct pt_regs *regs_user_copy) | |
4018994f | 5246 | { |
88a7c26a AL |
5247 | if (user_mode(regs)) { |
5248 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5249 | regs_user->regs = regs; |
88a7c26a AL |
5250 | } else if (current->mm) { |
5251 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5252 | } else { |
5253 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5254 | regs_user->regs = NULL; | |
4018994f JO |
5255 | } |
5256 | } | |
5257 | ||
60e2364e SE |
5258 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5259 | struct pt_regs *regs) | |
5260 | { | |
5261 | regs_intr->regs = regs; | |
5262 | regs_intr->abi = perf_reg_abi(current); | |
5263 | } | |
5264 | ||
5265 | ||
c5ebcedb JO |
5266 | /* |
5267 | * Get remaining task size from user stack pointer. | |
5268 | * | |
5269 | * It'd be better to take stack vma map and limit this more | |
5270 | * precisly, but there's no way to get it safely under interrupt, | |
5271 | * so using TASK_SIZE as limit. | |
5272 | */ | |
5273 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5274 | { | |
5275 | unsigned long addr = perf_user_stack_pointer(regs); | |
5276 | ||
5277 | if (!addr || addr >= TASK_SIZE) | |
5278 | return 0; | |
5279 | ||
5280 | return TASK_SIZE - addr; | |
5281 | } | |
5282 | ||
5283 | static u16 | |
5284 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5285 | struct pt_regs *regs) | |
5286 | { | |
5287 | u64 task_size; | |
5288 | ||
5289 | /* No regs, no stack pointer, no dump. */ | |
5290 | if (!regs) | |
5291 | return 0; | |
5292 | ||
5293 | /* | |
5294 | * Check if we fit in with the requested stack size into the: | |
5295 | * - TASK_SIZE | |
5296 | * If we don't, we limit the size to the TASK_SIZE. | |
5297 | * | |
5298 | * - remaining sample size | |
5299 | * If we don't, we customize the stack size to | |
5300 | * fit in to the remaining sample size. | |
5301 | */ | |
5302 | ||
5303 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5304 | stack_size = min(stack_size, (u16) task_size); | |
5305 | ||
5306 | /* Current header size plus static size and dynamic size. */ | |
5307 | header_size += 2 * sizeof(u64); | |
5308 | ||
5309 | /* Do we fit in with the current stack dump size? */ | |
5310 | if ((u16) (header_size + stack_size) < header_size) { | |
5311 | /* | |
5312 | * If we overflow the maximum size for the sample, | |
5313 | * we customize the stack dump size to fit in. | |
5314 | */ | |
5315 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5316 | stack_size = round_up(stack_size, sizeof(u64)); | |
5317 | } | |
5318 | ||
5319 | return stack_size; | |
5320 | } | |
5321 | ||
5322 | static void | |
5323 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5324 | struct pt_regs *regs) | |
5325 | { | |
5326 | /* Case of a kernel thread, nothing to dump */ | |
5327 | if (!regs) { | |
5328 | u64 size = 0; | |
5329 | perf_output_put(handle, size); | |
5330 | } else { | |
5331 | unsigned long sp; | |
5332 | unsigned int rem; | |
5333 | u64 dyn_size; | |
5334 | ||
5335 | /* | |
5336 | * We dump: | |
5337 | * static size | |
5338 | * - the size requested by user or the best one we can fit | |
5339 | * in to the sample max size | |
5340 | * data | |
5341 | * - user stack dump data | |
5342 | * dynamic size | |
5343 | * - the actual dumped size | |
5344 | */ | |
5345 | ||
5346 | /* Static size. */ | |
5347 | perf_output_put(handle, dump_size); | |
5348 | ||
5349 | /* Data. */ | |
5350 | sp = perf_user_stack_pointer(regs); | |
5351 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5352 | dyn_size = dump_size - rem; | |
5353 | ||
5354 | perf_output_skip(handle, rem); | |
5355 | ||
5356 | /* Dynamic size. */ | |
5357 | perf_output_put(handle, dyn_size); | |
5358 | } | |
5359 | } | |
5360 | ||
c980d109 ACM |
5361 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5362 | struct perf_sample_data *data, | |
5363 | struct perf_event *event) | |
6844c09d ACM |
5364 | { |
5365 | u64 sample_type = event->attr.sample_type; | |
5366 | ||
5367 | data->type = sample_type; | |
5368 | header->size += event->id_header_size; | |
5369 | ||
5370 | if (sample_type & PERF_SAMPLE_TID) { | |
5371 | /* namespace issues */ | |
5372 | data->tid_entry.pid = perf_event_pid(event, current); | |
5373 | data->tid_entry.tid = perf_event_tid(event, current); | |
5374 | } | |
5375 | ||
5376 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5377 | data->time = perf_event_clock(event); |
6844c09d | 5378 | |
ff3d527c | 5379 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5380 | data->id = primary_event_id(event); |
5381 | ||
5382 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5383 | data->stream_id = event->id; | |
5384 | ||
5385 | if (sample_type & PERF_SAMPLE_CPU) { | |
5386 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5387 | data->cpu_entry.reserved = 0; | |
5388 | } | |
5389 | } | |
5390 | ||
76369139 FW |
5391 | void perf_event_header__init_id(struct perf_event_header *header, |
5392 | struct perf_sample_data *data, | |
5393 | struct perf_event *event) | |
c980d109 ACM |
5394 | { |
5395 | if (event->attr.sample_id_all) | |
5396 | __perf_event_header__init_id(header, data, event); | |
5397 | } | |
5398 | ||
5399 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5400 | struct perf_sample_data *data) | |
5401 | { | |
5402 | u64 sample_type = data->type; | |
5403 | ||
5404 | if (sample_type & PERF_SAMPLE_TID) | |
5405 | perf_output_put(handle, data->tid_entry); | |
5406 | ||
5407 | if (sample_type & PERF_SAMPLE_TIME) | |
5408 | perf_output_put(handle, data->time); | |
5409 | ||
5410 | if (sample_type & PERF_SAMPLE_ID) | |
5411 | perf_output_put(handle, data->id); | |
5412 | ||
5413 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5414 | perf_output_put(handle, data->stream_id); | |
5415 | ||
5416 | if (sample_type & PERF_SAMPLE_CPU) | |
5417 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5418 | |
5419 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5420 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5421 | } |
5422 | ||
76369139 FW |
5423 | void perf_event__output_id_sample(struct perf_event *event, |
5424 | struct perf_output_handle *handle, | |
5425 | struct perf_sample_data *sample) | |
c980d109 ACM |
5426 | { |
5427 | if (event->attr.sample_id_all) | |
5428 | __perf_event__output_id_sample(handle, sample); | |
5429 | } | |
5430 | ||
3dab77fb | 5431 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5432 | struct perf_event *event, |
5433 | u64 enabled, u64 running) | |
3dab77fb | 5434 | { |
cdd6c482 | 5435 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5436 | u64 values[4]; |
5437 | int n = 0; | |
5438 | ||
b5e58793 | 5439 | values[n++] = perf_event_count(event); |
3dab77fb | 5440 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5441 | values[n++] = enabled + |
cdd6c482 | 5442 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5443 | } |
5444 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5445 | values[n++] = running + |
cdd6c482 | 5446 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5447 | } |
5448 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 5449 | values[n++] = primary_event_id(event); |
3dab77fb | 5450 | |
76369139 | 5451 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5452 | } |
5453 | ||
5454 | /* | |
cdd6c482 | 5455 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
3dab77fb PZ |
5456 | */ |
5457 | static void perf_output_read_group(struct perf_output_handle *handle, | |
eed01528 SE |
5458 | struct perf_event *event, |
5459 | u64 enabled, u64 running) | |
3dab77fb | 5460 | { |
cdd6c482 IM |
5461 | struct perf_event *leader = event->group_leader, *sub; |
5462 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
5463 | u64 values[5]; |
5464 | int n = 0; | |
5465 | ||
5466 | values[n++] = 1 + leader->nr_siblings; | |
5467 | ||
5468 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 5469 | values[n++] = enabled; |
3dab77fb PZ |
5470 | |
5471 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 5472 | values[n++] = running; |
3dab77fb | 5473 | |
cdd6c482 | 5474 | if (leader != event) |
3dab77fb PZ |
5475 | leader->pmu->read(leader); |
5476 | ||
b5e58793 | 5477 | values[n++] = perf_event_count(leader); |
3dab77fb | 5478 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5479 | values[n++] = primary_event_id(leader); |
3dab77fb | 5480 | |
76369139 | 5481 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 5482 | |
65abc865 | 5483 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
3dab77fb PZ |
5484 | n = 0; |
5485 | ||
6f5ab001 JO |
5486 | if ((sub != event) && |
5487 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
5488 | sub->pmu->read(sub); |
5489 | ||
b5e58793 | 5490 | values[n++] = perf_event_count(sub); |
3dab77fb | 5491 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5492 | values[n++] = primary_event_id(sub); |
3dab77fb | 5493 | |
76369139 | 5494 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5495 | } |
5496 | } | |
5497 | ||
eed01528 SE |
5498 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
5499 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5500 | ||
3dab77fb | 5501 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 5502 | struct perf_event *event) |
3dab77fb | 5503 | { |
e3f3541c | 5504 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
5505 | u64 read_format = event->attr.read_format; |
5506 | ||
5507 | /* | |
5508 | * compute total_time_enabled, total_time_running | |
5509 | * based on snapshot values taken when the event | |
5510 | * was last scheduled in. | |
5511 | * | |
5512 | * we cannot simply called update_context_time() | |
5513 | * because of locking issue as we are called in | |
5514 | * NMI context | |
5515 | */ | |
c4794295 | 5516 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 5517 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 5518 | |
cdd6c482 | 5519 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 5520 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 5521 | else |
eed01528 | 5522 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
5523 | } |
5524 | ||
5622f295 MM |
5525 | void perf_output_sample(struct perf_output_handle *handle, |
5526 | struct perf_event_header *header, | |
5527 | struct perf_sample_data *data, | |
cdd6c482 | 5528 | struct perf_event *event) |
5622f295 MM |
5529 | { |
5530 | u64 sample_type = data->type; | |
5531 | ||
5532 | perf_output_put(handle, *header); | |
5533 | ||
ff3d527c AH |
5534 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
5535 | perf_output_put(handle, data->id); | |
5536 | ||
5622f295 MM |
5537 | if (sample_type & PERF_SAMPLE_IP) |
5538 | perf_output_put(handle, data->ip); | |
5539 | ||
5540 | if (sample_type & PERF_SAMPLE_TID) | |
5541 | perf_output_put(handle, data->tid_entry); | |
5542 | ||
5543 | if (sample_type & PERF_SAMPLE_TIME) | |
5544 | perf_output_put(handle, data->time); | |
5545 | ||
5546 | if (sample_type & PERF_SAMPLE_ADDR) | |
5547 | perf_output_put(handle, data->addr); | |
5548 | ||
5549 | if (sample_type & PERF_SAMPLE_ID) | |
5550 | perf_output_put(handle, data->id); | |
5551 | ||
5552 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5553 | perf_output_put(handle, data->stream_id); | |
5554 | ||
5555 | if (sample_type & PERF_SAMPLE_CPU) | |
5556 | perf_output_put(handle, data->cpu_entry); | |
5557 | ||
5558 | if (sample_type & PERF_SAMPLE_PERIOD) | |
5559 | perf_output_put(handle, data->period); | |
5560 | ||
5561 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 5562 | perf_output_read(handle, event); |
5622f295 MM |
5563 | |
5564 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
5565 | if (data->callchain) { | |
5566 | int size = 1; | |
5567 | ||
5568 | if (data->callchain) | |
5569 | size += data->callchain->nr; | |
5570 | ||
5571 | size *= sizeof(u64); | |
5572 | ||
76369139 | 5573 | __output_copy(handle, data->callchain, size); |
5622f295 MM |
5574 | } else { |
5575 | u64 nr = 0; | |
5576 | perf_output_put(handle, nr); | |
5577 | } | |
5578 | } | |
5579 | ||
5580 | if (sample_type & PERF_SAMPLE_RAW) { | |
5581 | if (data->raw) { | |
fa128e6a AS |
5582 | u32 raw_size = data->raw->size; |
5583 | u32 real_size = round_up(raw_size + sizeof(u32), | |
5584 | sizeof(u64)) - sizeof(u32); | |
5585 | u64 zero = 0; | |
5586 | ||
5587 | perf_output_put(handle, real_size); | |
5588 | __output_copy(handle, data->raw->data, raw_size); | |
5589 | if (real_size - raw_size) | |
5590 | __output_copy(handle, &zero, real_size - raw_size); | |
5622f295 MM |
5591 | } else { |
5592 | struct { | |
5593 | u32 size; | |
5594 | u32 data; | |
5595 | } raw = { | |
5596 | .size = sizeof(u32), | |
5597 | .data = 0, | |
5598 | }; | |
5599 | perf_output_put(handle, raw); | |
5600 | } | |
5601 | } | |
a7ac67ea | 5602 | |
bce38cd5 SE |
5603 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
5604 | if (data->br_stack) { | |
5605 | size_t size; | |
5606 | ||
5607 | size = data->br_stack->nr | |
5608 | * sizeof(struct perf_branch_entry); | |
5609 | ||
5610 | perf_output_put(handle, data->br_stack->nr); | |
5611 | perf_output_copy(handle, data->br_stack->entries, size); | |
5612 | } else { | |
5613 | /* | |
5614 | * we always store at least the value of nr | |
5615 | */ | |
5616 | u64 nr = 0; | |
5617 | perf_output_put(handle, nr); | |
5618 | } | |
5619 | } | |
4018994f JO |
5620 | |
5621 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
5622 | u64 abi = data->regs_user.abi; | |
5623 | ||
5624 | /* | |
5625 | * If there are no regs to dump, notice it through | |
5626 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5627 | */ | |
5628 | perf_output_put(handle, abi); | |
5629 | ||
5630 | if (abi) { | |
5631 | u64 mask = event->attr.sample_regs_user; | |
5632 | perf_output_sample_regs(handle, | |
5633 | data->regs_user.regs, | |
5634 | mask); | |
5635 | } | |
5636 | } | |
c5ebcedb | 5637 | |
a5cdd40c | 5638 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
5639 | perf_output_sample_ustack(handle, |
5640 | data->stack_user_size, | |
5641 | data->regs_user.regs); | |
a5cdd40c | 5642 | } |
c3feedf2 AK |
5643 | |
5644 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
5645 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
5646 | |
5647 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
5648 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 5649 | |
fdfbbd07 AK |
5650 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
5651 | perf_output_put(handle, data->txn); | |
5652 | ||
60e2364e SE |
5653 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
5654 | u64 abi = data->regs_intr.abi; | |
5655 | /* | |
5656 | * If there are no regs to dump, notice it through | |
5657 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5658 | */ | |
5659 | perf_output_put(handle, abi); | |
5660 | ||
5661 | if (abi) { | |
5662 | u64 mask = event->attr.sample_regs_intr; | |
5663 | ||
5664 | perf_output_sample_regs(handle, | |
5665 | data->regs_intr.regs, | |
5666 | mask); | |
5667 | } | |
5668 | } | |
5669 | ||
a5cdd40c PZ |
5670 | if (!event->attr.watermark) { |
5671 | int wakeup_events = event->attr.wakeup_events; | |
5672 | ||
5673 | if (wakeup_events) { | |
5674 | struct ring_buffer *rb = handle->rb; | |
5675 | int events = local_inc_return(&rb->events); | |
5676 | ||
5677 | if (events >= wakeup_events) { | |
5678 | local_sub(wakeup_events, &rb->events); | |
5679 | local_inc(&rb->wakeup); | |
5680 | } | |
5681 | } | |
5682 | } | |
5622f295 MM |
5683 | } |
5684 | ||
5685 | void perf_prepare_sample(struct perf_event_header *header, | |
5686 | struct perf_sample_data *data, | |
cdd6c482 | 5687 | struct perf_event *event, |
5622f295 | 5688 | struct pt_regs *regs) |
7b732a75 | 5689 | { |
cdd6c482 | 5690 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 5691 | |
cdd6c482 | 5692 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 5693 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
5694 | |
5695 | header->misc = 0; | |
5696 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 5697 | |
c980d109 | 5698 | __perf_event_header__init_id(header, data, event); |
6844c09d | 5699 | |
c320c7b7 | 5700 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
5701 | data->ip = perf_instruction_pointer(regs); |
5702 | ||
b23f3325 | 5703 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 5704 | int size = 1; |
394ee076 | 5705 | |
e6dab5ff | 5706 | data->callchain = perf_callchain(event, regs); |
5622f295 MM |
5707 | |
5708 | if (data->callchain) | |
5709 | size += data->callchain->nr; | |
5710 | ||
5711 | header->size += size * sizeof(u64); | |
394ee076 PZ |
5712 | } |
5713 | ||
3a43ce68 | 5714 | if (sample_type & PERF_SAMPLE_RAW) { |
a044560c PZ |
5715 | int size = sizeof(u32); |
5716 | ||
5717 | if (data->raw) | |
5718 | size += data->raw->size; | |
5719 | else | |
5720 | size += sizeof(u32); | |
5721 | ||
fa128e6a | 5722 | header->size += round_up(size, sizeof(u64)); |
7f453c24 | 5723 | } |
bce38cd5 SE |
5724 | |
5725 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
5726 | int size = sizeof(u64); /* nr */ | |
5727 | if (data->br_stack) { | |
5728 | size += data->br_stack->nr | |
5729 | * sizeof(struct perf_branch_entry); | |
5730 | } | |
5731 | header->size += size; | |
5732 | } | |
4018994f | 5733 | |
2565711f | 5734 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
5735 | perf_sample_regs_user(&data->regs_user, regs, |
5736 | &data->regs_user_copy); | |
2565711f | 5737 | |
4018994f JO |
5738 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
5739 | /* regs dump ABI info */ | |
5740 | int size = sizeof(u64); | |
5741 | ||
4018994f JO |
5742 | if (data->regs_user.regs) { |
5743 | u64 mask = event->attr.sample_regs_user; | |
5744 | size += hweight64(mask) * sizeof(u64); | |
5745 | } | |
5746 | ||
5747 | header->size += size; | |
5748 | } | |
c5ebcedb JO |
5749 | |
5750 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
5751 | /* | |
5752 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
5753 | * processed as the last one or have additional check added | |
5754 | * in case new sample type is added, because we could eat | |
5755 | * up the rest of the sample size. | |
5756 | */ | |
c5ebcedb JO |
5757 | u16 stack_size = event->attr.sample_stack_user; |
5758 | u16 size = sizeof(u64); | |
5759 | ||
c5ebcedb | 5760 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 5761 | data->regs_user.regs); |
c5ebcedb JO |
5762 | |
5763 | /* | |
5764 | * If there is something to dump, add space for the dump | |
5765 | * itself and for the field that tells the dynamic size, | |
5766 | * which is how many have been actually dumped. | |
5767 | */ | |
5768 | if (stack_size) | |
5769 | size += sizeof(u64) + stack_size; | |
5770 | ||
5771 | data->stack_user_size = stack_size; | |
5772 | header->size += size; | |
5773 | } | |
60e2364e SE |
5774 | |
5775 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
5776 | /* regs dump ABI info */ | |
5777 | int size = sizeof(u64); | |
5778 | ||
5779 | perf_sample_regs_intr(&data->regs_intr, regs); | |
5780 | ||
5781 | if (data->regs_intr.regs) { | |
5782 | u64 mask = event->attr.sample_regs_intr; | |
5783 | ||
5784 | size += hweight64(mask) * sizeof(u64); | |
5785 | } | |
5786 | ||
5787 | header->size += size; | |
5788 | } | |
5622f295 | 5789 | } |
7f453c24 | 5790 | |
9ecda41a WN |
5791 | static void __always_inline |
5792 | __perf_event_output(struct perf_event *event, | |
5793 | struct perf_sample_data *data, | |
5794 | struct pt_regs *regs, | |
5795 | int (*output_begin)(struct perf_output_handle *, | |
5796 | struct perf_event *, | |
5797 | unsigned int)) | |
5622f295 MM |
5798 | { |
5799 | struct perf_output_handle handle; | |
5800 | struct perf_event_header header; | |
689802b2 | 5801 | |
927c7a9e FW |
5802 | /* protect the callchain buffers */ |
5803 | rcu_read_lock(); | |
5804 | ||
cdd6c482 | 5805 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 5806 | |
9ecda41a | 5807 | if (output_begin(&handle, event, header.size)) |
927c7a9e | 5808 | goto exit; |
0322cd6e | 5809 | |
cdd6c482 | 5810 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 5811 | |
8a057d84 | 5812 | perf_output_end(&handle); |
927c7a9e FW |
5813 | |
5814 | exit: | |
5815 | rcu_read_unlock(); | |
0322cd6e PZ |
5816 | } |
5817 | ||
9ecda41a WN |
5818 | void |
5819 | perf_event_output_forward(struct perf_event *event, | |
5820 | struct perf_sample_data *data, | |
5821 | struct pt_regs *regs) | |
5822 | { | |
5823 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
5824 | } | |
5825 | ||
5826 | void | |
5827 | perf_event_output_backward(struct perf_event *event, | |
5828 | struct perf_sample_data *data, | |
5829 | struct pt_regs *regs) | |
5830 | { | |
5831 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
5832 | } | |
5833 | ||
5834 | void | |
5835 | perf_event_output(struct perf_event *event, | |
5836 | struct perf_sample_data *data, | |
5837 | struct pt_regs *regs) | |
5838 | { | |
5839 | __perf_event_output(event, data, regs, perf_output_begin); | |
5840 | } | |
5841 | ||
38b200d6 | 5842 | /* |
cdd6c482 | 5843 | * read event_id |
38b200d6 PZ |
5844 | */ |
5845 | ||
5846 | struct perf_read_event { | |
5847 | struct perf_event_header header; | |
5848 | ||
5849 | u32 pid; | |
5850 | u32 tid; | |
38b200d6 PZ |
5851 | }; |
5852 | ||
5853 | static void | |
cdd6c482 | 5854 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
5855 | struct task_struct *task) |
5856 | { | |
5857 | struct perf_output_handle handle; | |
c980d109 | 5858 | struct perf_sample_data sample; |
dfc65094 | 5859 | struct perf_read_event read_event = { |
38b200d6 | 5860 | .header = { |
cdd6c482 | 5861 | .type = PERF_RECORD_READ, |
38b200d6 | 5862 | .misc = 0, |
c320c7b7 | 5863 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 5864 | }, |
cdd6c482 IM |
5865 | .pid = perf_event_pid(event, task), |
5866 | .tid = perf_event_tid(event, task), | |
38b200d6 | 5867 | }; |
3dab77fb | 5868 | int ret; |
38b200d6 | 5869 | |
c980d109 | 5870 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 5871 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
5872 | if (ret) |
5873 | return; | |
5874 | ||
dfc65094 | 5875 | perf_output_put(&handle, read_event); |
cdd6c482 | 5876 | perf_output_read(&handle, event); |
c980d109 | 5877 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 5878 | |
38b200d6 PZ |
5879 | perf_output_end(&handle); |
5880 | } | |
5881 | ||
52d857a8 JO |
5882 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
5883 | ||
5884 | static void | |
5885 | perf_event_aux_ctx(struct perf_event_context *ctx, | |
52d857a8 | 5886 | perf_event_aux_output_cb output, |
b73e4fef | 5887 | void *data, bool all) |
52d857a8 JO |
5888 | { |
5889 | struct perf_event *event; | |
5890 | ||
5891 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
5892 | if (!all) { |
5893 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
5894 | continue; | |
5895 | if (!event_filter_match(event)) | |
5896 | continue; | |
5897 | } | |
5898 | ||
67516844 | 5899 | output(event, data); |
52d857a8 JO |
5900 | } |
5901 | } | |
5902 | ||
4e93ad60 JO |
5903 | static void |
5904 | perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data, | |
5905 | struct perf_event_context *task_ctx) | |
5906 | { | |
5907 | rcu_read_lock(); | |
5908 | preempt_disable(); | |
b73e4fef | 5909 | perf_event_aux_ctx(task_ctx, output, data, false); |
4e93ad60 JO |
5910 | preempt_enable(); |
5911 | rcu_read_unlock(); | |
5912 | } | |
5913 | ||
f2fb6bef KL |
5914 | static void perf_event_sb_iterate(perf_event_aux_output_cb output, void *data) |
5915 | { | |
5916 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
5917 | struct perf_event *event; | |
5918 | ||
5919 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
5920 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
5921 | continue; | |
5922 | if (!event_filter_match(event)) | |
5923 | continue; | |
5924 | output(event, data); | |
5925 | } | |
5926 | } | |
5927 | ||
52d857a8 | 5928 | static void |
67516844 | 5929 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
52d857a8 JO |
5930 | struct perf_event_context *task_ctx) |
5931 | { | |
52d857a8 | 5932 | struct perf_event_context *ctx; |
52d857a8 JO |
5933 | int ctxn; |
5934 | ||
4e93ad60 JO |
5935 | /* |
5936 | * If we have task_ctx != NULL we only notify | |
5937 | * the task context itself. The task_ctx is set | |
5938 | * only for EXIT events before releasing task | |
5939 | * context. | |
5940 | */ | |
5941 | if (task_ctx) { | |
5942 | perf_event_aux_task_ctx(output, data, task_ctx); | |
5943 | return; | |
5944 | } | |
5945 | ||
52d857a8 | 5946 | rcu_read_lock(); |
f2fb6bef KL |
5947 | preempt_disable(); |
5948 | perf_event_sb_iterate(output, data); | |
5949 | ||
5950 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
5951 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
5952 | if (ctx) | |
b73e4fef | 5953 | perf_event_aux_ctx(ctx, output, data, false); |
52d857a8 | 5954 | } |
f2fb6bef | 5955 | preempt_enable(); |
52d857a8 | 5956 | rcu_read_unlock(); |
95ff4ca2 AS |
5957 | } |
5958 | ||
375637bc AS |
5959 | /* |
5960 | * Clear all file-based filters at exec, they'll have to be | |
5961 | * re-instated when/if these objects are mmapped again. | |
5962 | */ | |
5963 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
5964 | { | |
5965 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
5966 | struct perf_addr_filter *filter; | |
5967 | unsigned int restart = 0, count = 0; | |
5968 | unsigned long flags; | |
5969 | ||
5970 | if (!has_addr_filter(event)) | |
5971 | return; | |
5972 | ||
5973 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
5974 | list_for_each_entry(filter, &ifh->list, entry) { | |
5975 | if (filter->inode) { | |
5976 | event->addr_filters_offs[count] = 0; | |
5977 | restart++; | |
5978 | } | |
5979 | ||
5980 | count++; | |
5981 | } | |
5982 | ||
5983 | if (restart) | |
5984 | event->addr_filters_gen++; | |
5985 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
5986 | ||
5987 | if (restart) | |
5988 | perf_event_restart(event); | |
5989 | } | |
5990 | ||
5991 | void perf_event_exec(void) | |
5992 | { | |
5993 | struct perf_event_context *ctx; | |
5994 | int ctxn; | |
5995 | ||
5996 | rcu_read_lock(); | |
5997 | for_each_task_context_nr(ctxn) { | |
5998 | ctx = current->perf_event_ctxp[ctxn]; | |
5999 | if (!ctx) | |
6000 | continue; | |
6001 | ||
6002 | perf_event_enable_on_exec(ctxn); | |
6003 | ||
6004 | perf_event_aux_ctx(ctx, perf_event_addr_filters_exec, NULL, | |
6005 | true); | |
6006 | } | |
6007 | rcu_read_unlock(); | |
6008 | } | |
6009 | ||
95ff4ca2 AS |
6010 | struct remote_output { |
6011 | struct ring_buffer *rb; | |
6012 | int err; | |
6013 | }; | |
6014 | ||
6015 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
6016 | { | |
6017 | struct perf_event *parent = event->parent; | |
6018 | struct remote_output *ro = data; | |
6019 | struct ring_buffer *rb = ro->rb; | |
375637bc AS |
6020 | struct stop_event_data sd = { |
6021 | .event = event, | |
6022 | }; | |
95ff4ca2 AS |
6023 | |
6024 | if (!has_aux(event)) | |
6025 | return; | |
6026 | ||
6027 | if (!parent) | |
6028 | parent = event; | |
6029 | ||
6030 | /* | |
6031 | * In case of inheritance, it will be the parent that links to the | |
6032 | * ring-buffer, but it will be the child that's actually using it: | |
6033 | */ | |
6034 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 6035 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
6036 | } |
6037 | ||
6038 | static int __perf_pmu_output_stop(void *info) | |
6039 | { | |
6040 | struct perf_event *event = info; | |
6041 | struct pmu *pmu = event->pmu; | |
6042 | struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); | |
6043 | struct remote_output ro = { | |
6044 | .rb = event->rb, | |
6045 | }; | |
6046 | ||
6047 | rcu_read_lock(); | |
b73e4fef | 6048 | perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 AS |
6049 | if (cpuctx->task_ctx) |
6050 | perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop, | |
b73e4fef | 6051 | &ro, false); |
95ff4ca2 AS |
6052 | rcu_read_unlock(); |
6053 | ||
6054 | return ro.err; | |
6055 | } | |
6056 | ||
6057 | static void perf_pmu_output_stop(struct perf_event *event) | |
6058 | { | |
6059 | struct perf_event *iter; | |
6060 | int err, cpu; | |
6061 | ||
6062 | restart: | |
6063 | rcu_read_lock(); | |
6064 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
6065 | /* | |
6066 | * For per-CPU events, we need to make sure that neither they | |
6067 | * nor their children are running; for cpu==-1 events it's | |
6068 | * sufficient to stop the event itself if it's active, since | |
6069 | * it can't have children. | |
6070 | */ | |
6071 | cpu = iter->cpu; | |
6072 | if (cpu == -1) | |
6073 | cpu = READ_ONCE(iter->oncpu); | |
6074 | ||
6075 | if (cpu == -1) | |
6076 | continue; | |
6077 | ||
6078 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
6079 | if (err == -EAGAIN) { | |
6080 | rcu_read_unlock(); | |
6081 | goto restart; | |
6082 | } | |
6083 | } | |
6084 | rcu_read_unlock(); | |
52d857a8 JO |
6085 | } |
6086 | ||
60313ebe | 6087 | /* |
9f498cc5 PZ |
6088 | * task tracking -- fork/exit |
6089 | * | |
13d7a241 | 6090 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
6091 | */ |
6092 | ||
9f498cc5 | 6093 | struct perf_task_event { |
3a80b4a3 | 6094 | struct task_struct *task; |
cdd6c482 | 6095 | struct perf_event_context *task_ctx; |
60313ebe PZ |
6096 | |
6097 | struct { | |
6098 | struct perf_event_header header; | |
6099 | ||
6100 | u32 pid; | |
6101 | u32 ppid; | |
9f498cc5 PZ |
6102 | u32 tid; |
6103 | u32 ptid; | |
393b2ad8 | 6104 | u64 time; |
cdd6c482 | 6105 | } event_id; |
60313ebe PZ |
6106 | }; |
6107 | ||
67516844 JO |
6108 | static int perf_event_task_match(struct perf_event *event) |
6109 | { | |
13d7a241 SE |
6110 | return event->attr.comm || event->attr.mmap || |
6111 | event->attr.mmap2 || event->attr.mmap_data || | |
6112 | event->attr.task; | |
67516844 JO |
6113 | } |
6114 | ||
cdd6c482 | 6115 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 6116 | void *data) |
60313ebe | 6117 | { |
52d857a8 | 6118 | struct perf_task_event *task_event = data; |
60313ebe | 6119 | struct perf_output_handle handle; |
c980d109 | 6120 | struct perf_sample_data sample; |
9f498cc5 | 6121 | struct task_struct *task = task_event->task; |
c980d109 | 6122 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 6123 | |
67516844 JO |
6124 | if (!perf_event_task_match(event)) |
6125 | return; | |
6126 | ||
c980d109 | 6127 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 6128 | |
c980d109 | 6129 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 6130 | task_event->event_id.header.size); |
ef60777c | 6131 | if (ret) |
c980d109 | 6132 | goto out; |
60313ebe | 6133 | |
cdd6c482 IM |
6134 | task_event->event_id.pid = perf_event_pid(event, task); |
6135 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 6136 | |
cdd6c482 IM |
6137 | task_event->event_id.tid = perf_event_tid(event, task); |
6138 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 6139 | |
34f43927 PZ |
6140 | task_event->event_id.time = perf_event_clock(event); |
6141 | ||
cdd6c482 | 6142 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 6143 | |
c980d109 ACM |
6144 | perf_event__output_id_sample(event, &handle, &sample); |
6145 | ||
60313ebe | 6146 | perf_output_end(&handle); |
c980d109 ACM |
6147 | out: |
6148 | task_event->event_id.header.size = size; | |
60313ebe PZ |
6149 | } |
6150 | ||
cdd6c482 IM |
6151 | static void perf_event_task(struct task_struct *task, |
6152 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 6153 | int new) |
60313ebe | 6154 | { |
9f498cc5 | 6155 | struct perf_task_event task_event; |
60313ebe | 6156 | |
cdd6c482 IM |
6157 | if (!atomic_read(&nr_comm_events) && |
6158 | !atomic_read(&nr_mmap_events) && | |
6159 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
6160 | return; |
6161 | ||
9f498cc5 | 6162 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
6163 | .task = task, |
6164 | .task_ctx = task_ctx, | |
cdd6c482 | 6165 | .event_id = { |
60313ebe | 6166 | .header = { |
cdd6c482 | 6167 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 6168 | .misc = 0, |
cdd6c482 | 6169 | .size = sizeof(task_event.event_id), |
60313ebe | 6170 | }, |
573402db PZ |
6171 | /* .pid */ |
6172 | /* .ppid */ | |
9f498cc5 PZ |
6173 | /* .tid */ |
6174 | /* .ptid */ | |
34f43927 | 6175 | /* .time */ |
60313ebe PZ |
6176 | }, |
6177 | }; | |
6178 | ||
67516844 | 6179 | perf_event_aux(perf_event_task_output, |
52d857a8 JO |
6180 | &task_event, |
6181 | task_ctx); | |
9f498cc5 PZ |
6182 | } |
6183 | ||
cdd6c482 | 6184 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 6185 | { |
cdd6c482 | 6186 | perf_event_task(task, NULL, 1); |
60313ebe PZ |
6187 | } |
6188 | ||
8d1b2d93 PZ |
6189 | /* |
6190 | * comm tracking | |
6191 | */ | |
6192 | ||
6193 | struct perf_comm_event { | |
22a4f650 IM |
6194 | struct task_struct *task; |
6195 | char *comm; | |
8d1b2d93 PZ |
6196 | int comm_size; |
6197 | ||
6198 | struct { | |
6199 | struct perf_event_header header; | |
6200 | ||
6201 | u32 pid; | |
6202 | u32 tid; | |
cdd6c482 | 6203 | } event_id; |
8d1b2d93 PZ |
6204 | }; |
6205 | ||
67516844 JO |
6206 | static int perf_event_comm_match(struct perf_event *event) |
6207 | { | |
6208 | return event->attr.comm; | |
6209 | } | |
6210 | ||
cdd6c482 | 6211 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6212 | void *data) |
8d1b2d93 | 6213 | { |
52d857a8 | 6214 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6215 | struct perf_output_handle handle; |
c980d109 | 6216 | struct perf_sample_data sample; |
cdd6c482 | 6217 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6218 | int ret; |
6219 | ||
67516844 JO |
6220 | if (!perf_event_comm_match(event)) |
6221 | return; | |
6222 | ||
c980d109 ACM |
6223 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6224 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6225 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6226 | |
6227 | if (ret) | |
c980d109 | 6228 | goto out; |
8d1b2d93 | 6229 | |
cdd6c482 IM |
6230 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6231 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6232 | |
cdd6c482 | 6233 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6234 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6235 | comm_event->comm_size); |
c980d109 ACM |
6236 | |
6237 | perf_event__output_id_sample(event, &handle, &sample); | |
6238 | ||
8d1b2d93 | 6239 | perf_output_end(&handle); |
c980d109 ACM |
6240 | out: |
6241 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6242 | } |
6243 | ||
cdd6c482 | 6244 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6245 | { |
413ee3b4 | 6246 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6247 | unsigned int size; |
8d1b2d93 | 6248 | |
413ee3b4 | 6249 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6250 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6251 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6252 | |
6253 | comm_event->comm = comm; | |
6254 | comm_event->comm_size = size; | |
6255 | ||
cdd6c482 | 6256 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6257 | |
67516844 | 6258 | perf_event_aux(perf_event_comm_output, |
52d857a8 JO |
6259 | comm_event, |
6260 | NULL); | |
8d1b2d93 PZ |
6261 | } |
6262 | ||
82b89778 | 6263 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6264 | { |
9ee318a7 PZ |
6265 | struct perf_comm_event comm_event; |
6266 | ||
cdd6c482 | 6267 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6268 | return; |
a63eaf34 | 6269 | |
9ee318a7 | 6270 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6271 | .task = task, |
573402db PZ |
6272 | /* .comm */ |
6273 | /* .comm_size */ | |
cdd6c482 | 6274 | .event_id = { |
573402db | 6275 | .header = { |
cdd6c482 | 6276 | .type = PERF_RECORD_COMM, |
82b89778 | 6277 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6278 | /* .size */ |
6279 | }, | |
6280 | /* .pid */ | |
6281 | /* .tid */ | |
8d1b2d93 PZ |
6282 | }, |
6283 | }; | |
6284 | ||
cdd6c482 | 6285 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6286 | } |
6287 | ||
0a4a9391 PZ |
6288 | /* |
6289 | * mmap tracking | |
6290 | */ | |
6291 | ||
6292 | struct perf_mmap_event { | |
089dd79d PZ |
6293 | struct vm_area_struct *vma; |
6294 | ||
6295 | const char *file_name; | |
6296 | int file_size; | |
13d7a241 SE |
6297 | int maj, min; |
6298 | u64 ino; | |
6299 | u64 ino_generation; | |
f972eb63 | 6300 | u32 prot, flags; |
0a4a9391 PZ |
6301 | |
6302 | struct { | |
6303 | struct perf_event_header header; | |
6304 | ||
6305 | u32 pid; | |
6306 | u32 tid; | |
6307 | u64 start; | |
6308 | u64 len; | |
6309 | u64 pgoff; | |
cdd6c482 | 6310 | } event_id; |
0a4a9391 PZ |
6311 | }; |
6312 | ||
67516844 JO |
6313 | static int perf_event_mmap_match(struct perf_event *event, |
6314 | void *data) | |
6315 | { | |
6316 | struct perf_mmap_event *mmap_event = data; | |
6317 | struct vm_area_struct *vma = mmap_event->vma; | |
6318 | int executable = vma->vm_flags & VM_EXEC; | |
6319 | ||
6320 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 6321 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
6322 | } |
6323 | ||
cdd6c482 | 6324 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 6325 | void *data) |
0a4a9391 | 6326 | { |
52d857a8 | 6327 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 6328 | struct perf_output_handle handle; |
c980d109 | 6329 | struct perf_sample_data sample; |
cdd6c482 | 6330 | int size = mmap_event->event_id.header.size; |
c980d109 | 6331 | int ret; |
0a4a9391 | 6332 | |
67516844 JO |
6333 | if (!perf_event_mmap_match(event, data)) |
6334 | return; | |
6335 | ||
13d7a241 SE |
6336 | if (event->attr.mmap2) { |
6337 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
6338 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
6339 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
6340 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 6341 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
6342 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
6343 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
6344 | } |
6345 | ||
c980d109 ACM |
6346 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
6347 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6348 | mmap_event->event_id.header.size); |
0a4a9391 | 6349 | if (ret) |
c980d109 | 6350 | goto out; |
0a4a9391 | 6351 | |
cdd6c482 IM |
6352 | mmap_event->event_id.pid = perf_event_pid(event, current); |
6353 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 6354 | |
cdd6c482 | 6355 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
6356 | |
6357 | if (event->attr.mmap2) { | |
6358 | perf_output_put(&handle, mmap_event->maj); | |
6359 | perf_output_put(&handle, mmap_event->min); | |
6360 | perf_output_put(&handle, mmap_event->ino); | |
6361 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
6362 | perf_output_put(&handle, mmap_event->prot); |
6363 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
6364 | } |
6365 | ||
76369139 | 6366 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 6367 | mmap_event->file_size); |
c980d109 ACM |
6368 | |
6369 | perf_event__output_id_sample(event, &handle, &sample); | |
6370 | ||
78d613eb | 6371 | perf_output_end(&handle); |
c980d109 ACM |
6372 | out: |
6373 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
6374 | } |
6375 | ||
cdd6c482 | 6376 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 6377 | { |
089dd79d PZ |
6378 | struct vm_area_struct *vma = mmap_event->vma; |
6379 | struct file *file = vma->vm_file; | |
13d7a241 SE |
6380 | int maj = 0, min = 0; |
6381 | u64 ino = 0, gen = 0; | |
f972eb63 | 6382 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
6383 | unsigned int size; |
6384 | char tmp[16]; | |
6385 | char *buf = NULL; | |
2c42cfbf | 6386 | char *name; |
413ee3b4 | 6387 | |
0a4a9391 | 6388 | if (file) { |
13d7a241 SE |
6389 | struct inode *inode; |
6390 | dev_t dev; | |
3ea2f2b9 | 6391 | |
2c42cfbf | 6392 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 6393 | if (!buf) { |
c7e548b4 ON |
6394 | name = "//enomem"; |
6395 | goto cpy_name; | |
0a4a9391 | 6396 | } |
413ee3b4 | 6397 | /* |
3ea2f2b9 | 6398 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
6399 | * need to add enough zero bytes after the string to handle |
6400 | * the 64bit alignment we do later. | |
6401 | */ | |
9bf39ab2 | 6402 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 6403 | if (IS_ERR(name)) { |
c7e548b4 ON |
6404 | name = "//toolong"; |
6405 | goto cpy_name; | |
0a4a9391 | 6406 | } |
13d7a241 SE |
6407 | inode = file_inode(vma->vm_file); |
6408 | dev = inode->i_sb->s_dev; | |
6409 | ino = inode->i_ino; | |
6410 | gen = inode->i_generation; | |
6411 | maj = MAJOR(dev); | |
6412 | min = MINOR(dev); | |
f972eb63 PZ |
6413 | |
6414 | if (vma->vm_flags & VM_READ) | |
6415 | prot |= PROT_READ; | |
6416 | if (vma->vm_flags & VM_WRITE) | |
6417 | prot |= PROT_WRITE; | |
6418 | if (vma->vm_flags & VM_EXEC) | |
6419 | prot |= PROT_EXEC; | |
6420 | ||
6421 | if (vma->vm_flags & VM_MAYSHARE) | |
6422 | flags = MAP_SHARED; | |
6423 | else | |
6424 | flags = MAP_PRIVATE; | |
6425 | ||
6426 | if (vma->vm_flags & VM_DENYWRITE) | |
6427 | flags |= MAP_DENYWRITE; | |
6428 | if (vma->vm_flags & VM_MAYEXEC) | |
6429 | flags |= MAP_EXECUTABLE; | |
6430 | if (vma->vm_flags & VM_LOCKED) | |
6431 | flags |= MAP_LOCKED; | |
6432 | if (vma->vm_flags & VM_HUGETLB) | |
6433 | flags |= MAP_HUGETLB; | |
6434 | ||
c7e548b4 | 6435 | goto got_name; |
0a4a9391 | 6436 | } else { |
fbe26abe JO |
6437 | if (vma->vm_ops && vma->vm_ops->name) { |
6438 | name = (char *) vma->vm_ops->name(vma); | |
6439 | if (name) | |
6440 | goto cpy_name; | |
6441 | } | |
6442 | ||
2c42cfbf | 6443 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
6444 | if (name) |
6445 | goto cpy_name; | |
089dd79d | 6446 | |
32c5fb7e | 6447 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 6448 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
6449 | name = "[heap]"; |
6450 | goto cpy_name; | |
32c5fb7e ON |
6451 | } |
6452 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 6453 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
6454 | name = "[stack]"; |
6455 | goto cpy_name; | |
089dd79d PZ |
6456 | } |
6457 | ||
c7e548b4 ON |
6458 | name = "//anon"; |
6459 | goto cpy_name; | |
0a4a9391 PZ |
6460 | } |
6461 | ||
c7e548b4 ON |
6462 | cpy_name: |
6463 | strlcpy(tmp, name, sizeof(tmp)); | |
6464 | name = tmp; | |
0a4a9391 | 6465 | got_name: |
2c42cfbf PZ |
6466 | /* |
6467 | * Since our buffer works in 8 byte units we need to align our string | |
6468 | * size to a multiple of 8. However, we must guarantee the tail end is | |
6469 | * zero'd out to avoid leaking random bits to userspace. | |
6470 | */ | |
6471 | size = strlen(name)+1; | |
6472 | while (!IS_ALIGNED(size, sizeof(u64))) | |
6473 | name[size++] = '\0'; | |
0a4a9391 PZ |
6474 | |
6475 | mmap_event->file_name = name; | |
6476 | mmap_event->file_size = size; | |
13d7a241 SE |
6477 | mmap_event->maj = maj; |
6478 | mmap_event->min = min; | |
6479 | mmap_event->ino = ino; | |
6480 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
6481 | mmap_event->prot = prot; |
6482 | mmap_event->flags = flags; | |
0a4a9391 | 6483 | |
2fe85427 SE |
6484 | if (!(vma->vm_flags & VM_EXEC)) |
6485 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
6486 | ||
cdd6c482 | 6487 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 6488 | |
67516844 | 6489 | perf_event_aux(perf_event_mmap_output, |
52d857a8 JO |
6490 | mmap_event, |
6491 | NULL); | |
665c2142 | 6492 | |
0a4a9391 PZ |
6493 | kfree(buf); |
6494 | } | |
6495 | ||
375637bc AS |
6496 | /* |
6497 | * Whether this @filter depends on a dynamic object which is not loaded | |
6498 | * yet or its load addresses are not known. | |
6499 | */ | |
6500 | static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter) | |
6501 | { | |
6502 | return filter->filter && filter->inode; | |
6503 | } | |
6504 | ||
6505 | /* | |
6506 | * Check whether inode and address range match filter criteria. | |
6507 | */ | |
6508 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
6509 | struct file *file, unsigned long offset, | |
6510 | unsigned long size) | |
6511 | { | |
6512 | if (filter->inode != file->f_inode) | |
6513 | return false; | |
6514 | ||
6515 | if (filter->offset > offset + size) | |
6516 | return false; | |
6517 | ||
6518 | if (filter->offset + filter->size < offset) | |
6519 | return false; | |
6520 | ||
6521 | return true; | |
6522 | } | |
6523 | ||
6524 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) | |
6525 | { | |
6526 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
6527 | struct vm_area_struct *vma = data; | |
6528 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; | |
6529 | struct file *file = vma->vm_file; | |
6530 | struct perf_addr_filter *filter; | |
6531 | unsigned int restart = 0, count = 0; | |
6532 | ||
6533 | if (!has_addr_filter(event)) | |
6534 | return; | |
6535 | ||
6536 | if (!file) | |
6537 | return; | |
6538 | ||
6539 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
6540 | list_for_each_entry(filter, &ifh->list, entry) { | |
6541 | if (perf_addr_filter_match(filter, file, off, | |
6542 | vma->vm_end - vma->vm_start)) { | |
6543 | event->addr_filters_offs[count] = vma->vm_start; | |
6544 | restart++; | |
6545 | } | |
6546 | ||
6547 | count++; | |
6548 | } | |
6549 | ||
6550 | if (restart) | |
6551 | event->addr_filters_gen++; | |
6552 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
6553 | ||
6554 | if (restart) | |
6555 | perf_event_restart(event); | |
6556 | } | |
6557 | ||
6558 | /* | |
6559 | * Adjust all task's events' filters to the new vma | |
6560 | */ | |
6561 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
6562 | { | |
6563 | struct perf_event_context *ctx; | |
6564 | int ctxn; | |
6565 | ||
6566 | rcu_read_lock(); | |
6567 | for_each_task_context_nr(ctxn) { | |
6568 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
6569 | if (!ctx) | |
6570 | continue; | |
6571 | ||
6572 | perf_event_aux_ctx(ctx, __perf_addr_filters_adjust, vma, true); | |
6573 | } | |
6574 | rcu_read_unlock(); | |
6575 | } | |
6576 | ||
3af9e859 | 6577 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 6578 | { |
9ee318a7 PZ |
6579 | struct perf_mmap_event mmap_event; |
6580 | ||
cdd6c482 | 6581 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
6582 | return; |
6583 | ||
6584 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 6585 | .vma = vma, |
573402db PZ |
6586 | /* .file_name */ |
6587 | /* .file_size */ | |
cdd6c482 | 6588 | .event_id = { |
573402db | 6589 | .header = { |
cdd6c482 | 6590 | .type = PERF_RECORD_MMAP, |
39447b38 | 6591 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
6592 | /* .size */ |
6593 | }, | |
6594 | /* .pid */ | |
6595 | /* .tid */ | |
089dd79d PZ |
6596 | .start = vma->vm_start, |
6597 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 6598 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 6599 | }, |
13d7a241 SE |
6600 | /* .maj (attr_mmap2 only) */ |
6601 | /* .min (attr_mmap2 only) */ | |
6602 | /* .ino (attr_mmap2 only) */ | |
6603 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
6604 | /* .prot (attr_mmap2 only) */ |
6605 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
6606 | }; |
6607 | ||
375637bc | 6608 | perf_addr_filters_adjust(vma); |
cdd6c482 | 6609 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
6610 | } |
6611 | ||
68db7e98 AS |
6612 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
6613 | unsigned long size, u64 flags) | |
6614 | { | |
6615 | struct perf_output_handle handle; | |
6616 | struct perf_sample_data sample; | |
6617 | struct perf_aux_event { | |
6618 | struct perf_event_header header; | |
6619 | u64 offset; | |
6620 | u64 size; | |
6621 | u64 flags; | |
6622 | } rec = { | |
6623 | .header = { | |
6624 | .type = PERF_RECORD_AUX, | |
6625 | .misc = 0, | |
6626 | .size = sizeof(rec), | |
6627 | }, | |
6628 | .offset = head, | |
6629 | .size = size, | |
6630 | .flags = flags, | |
6631 | }; | |
6632 | int ret; | |
6633 | ||
6634 | perf_event_header__init_id(&rec.header, &sample, event); | |
6635 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6636 | ||
6637 | if (ret) | |
6638 | return; | |
6639 | ||
6640 | perf_output_put(&handle, rec); | |
6641 | perf_event__output_id_sample(event, &handle, &sample); | |
6642 | ||
6643 | perf_output_end(&handle); | |
6644 | } | |
6645 | ||
f38b0dbb KL |
6646 | /* |
6647 | * Lost/dropped samples logging | |
6648 | */ | |
6649 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
6650 | { | |
6651 | struct perf_output_handle handle; | |
6652 | struct perf_sample_data sample; | |
6653 | int ret; | |
6654 | ||
6655 | struct { | |
6656 | struct perf_event_header header; | |
6657 | u64 lost; | |
6658 | } lost_samples_event = { | |
6659 | .header = { | |
6660 | .type = PERF_RECORD_LOST_SAMPLES, | |
6661 | .misc = 0, | |
6662 | .size = sizeof(lost_samples_event), | |
6663 | }, | |
6664 | .lost = lost, | |
6665 | }; | |
6666 | ||
6667 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
6668 | ||
6669 | ret = perf_output_begin(&handle, event, | |
6670 | lost_samples_event.header.size); | |
6671 | if (ret) | |
6672 | return; | |
6673 | ||
6674 | perf_output_put(&handle, lost_samples_event); | |
6675 | perf_event__output_id_sample(event, &handle, &sample); | |
6676 | perf_output_end(&handle); | |
6677 | } | |
6678 | ||
45ac1403 AH |
6679 | /* |
6680 | * context_switch tracking | |
6681 | */ | |
6682 | ||
6683 | struct perf_switch_event { | |
6684 | struct task_struct *task; | |
6685 | struct task_struct *next_prev; | |
6686 | ||
6687 | struct { | |
6688 | struct perf_event_header header; | |
6689 | u32 next_prev_pid; | |
6690 | u32 next_prev_tid; | |
6691 | } event_id; | |
6692 | }; | |
6693 | ||
6694 | static int perf_event_switch_match(struct perf_event *event) | |
6695 | { | |
6696 | return event->attr.context_switch; | |
6697 | } | |
6698 | ||
6699 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
6700 | { | |
6701 | struct perf_switch_event *se = data; | |
6702 | struct perf_output_handle handle; | |
6703 | struct perf_sample_data sample; | |
6704 | int ret; | |
6705 | ||
6706 | if (!perf_event_switch_match(event)) | |
6707 | return; | |
6708 | ||
6709 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
6710 | if (event->ctx->task) { | |
6711 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
6712 | se->event_id.header.size = sizeof(se->event_id.header); | |
6713 | } else { | |
6714 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
6715 | se->event_id.header.size = sizeof(se->event_id); | |
6716 | se->event_id.next_prev_pid = | |
6717 | perf_event_pid(event, se->next_prev); | |
6718 | se->event_id.next_prev_tid = | |
6719 | perf_event_tid(event, se->next_prev); | |
6720 | } | |
6721 | ||
6722 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
6723 | ||
6724 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
6725 | if (ret) | |
6726 | return; | |
6727 | ||
6728 | if (event->ctx->task) | |
6729 | perf_output_put(&handle, se->event_id.header); | |
6730 | else | |
6731 | perf_output_put(&handle, se->event_id); | |
6732 | ||
6733 | perf_event__output_id_sample(event, &handle, &sample); | |
6734 | ||
6735 | perf_output_end(&handle); | |
6736 | } | |
6737 | ||
6738 | static void perf_event_switch(struct task_struct *task, | |
6739 | struct task_struct *next_prev, bool sched_in) | |
6740 | { | |
6741 | struct perf_switch_event switch_event; | |
6742 | ||
6743 | /* N.B. caller checks nr_switch_events != 0 */ | |
6744 | ||
6745 | switch_event = (struct perf_switch_event){ | |
6746 | .task = task, | |
6747 | .next_prev = next_prev, | |
6748 | .event_id = { | |
6749 | .header = { | |
6750 | /* .type */ | |
6751 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
6752 | /* .size */ | |
6753 | }, | |
6754 | /* .next_prev_pid */ | |
6755 | /* .next_prev_tid */ | |
6756 | }, | |
6757 | }; | |
6758 | ||
6759 | perf_event_aux(perf_event_switch_output, | |
6760 | &switch_event, | |
6761 | NULL); | |
6762 | } | |
6763 | ||
a78ac325 PZ |
6764 | /* |
6765 | * IRQ throttle logging | |
6766 | */ | |
6767 | ||
cdd6c482 | 6768 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
6769 | { |
6770 | struct perf_output_handle handle; | |
c980d109 | 6771 | struct perf_sample_data sample; |
a78ac325 PZ |
6772 | int ret; |
6773 | ||
6774 | struct { | |
6775 | struct perf_event_header header; | |
6776 | u64 time; | |
cca3f454 | 6777 | u64 id; |
7f453c24 | 6778 | u64 stream_id; |
a78ac325 PZ |
6779 | } throttle_event = { |
6780 | .header = { | |
cdd6c482 | 6781 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
6782 | .misc = 0, |
6783 | .size = sizeof(throttle_event), | |
6784 | }, | |
34f43927 | 6785 | .time = perf_event_clock(event), |
cdd6c482 IM |
6786 | .id = primary_event_id(event), |
6787 | .stream_id = event->id, | |
a78ac325 PZ |
6788 | }; |
6789 | ||
966ee4d6 | 6790 | if (enable) |
cdd6c482 | 6791 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 6792 | |
c980d109 ACM |
6793 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
6794 | ||
6795 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6796 | throttle_event.header.size); |
a78ac325 PZ |
6797 | if (ret) |
6798 | return; | |
6799 | ||
6800 | perf_output_put(&handle, throttle_event); | |
c980d109 | 6801 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
6802 | perf_output_end(&handle); |
6803 | } | |
6804 | ||
ec0d7729 AS |
6805 | static void perf_log_itrace_start(struct perf_event *event) |
6806 | { | |
6807 | struct perf_output_handle handle; | |
6808 | struct perf_sample_data sample; | |
6809 | struct perf_aux_event { | |
6810 | struct perf_event_header header; | |
6811 | u32 pid; | |
6812 | u32 tid; | |
6813 | } rec; | |
6814 | int ret; | |
6815 | ||
6816 | if (event->parent) | |
6817 | event = event->parent; | |
6818 | ||
6819 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
6820 | event->hw.itrace_started) | |
6821 | return; | |
6822 | ||
ec0d7729 AS |
6823 | rec.header.type = PERF_RECORD_ITRACE_START; |
6824 | rec.header.misc = 0; | |
6825 | rec.header.size = sizeof(rec); | |
6826 | rec.pid = perf_event_pid(event, current); | |
6827 | rec.tid = perf_event_tid(event, current); | |
6828 | ||
6829 | perf_event_header__init_id(&rec.header, &sample, event); | |
6830 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6831 | ||
6832 | if (ret) | |
6833 | return; | |
6834 | ||
6835 | perf_output_put(&handle, rec); | |
6836 | perf_event__output_id_sample(event, &handle, &sample); | |
6837 | ||
6838 | perf_output_end(&handle); | |
6839 | } | |
6840 | ||
f6c7d5fe | 6841 | /* |
cdd6c482 | 6842 | * Generic event overflow handling, sampling. |
f6c7d5fe PZ |
6843 | */ |
6844 | ||
a8b0ca17 | 6845 | static int __perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6846 | int throttle, struct perf_sample_data *data, |
6847 | struct pt_regs *regs) | |
f6c7d5fe | 6848 | { |
cdd6c482 IM |
6849 | int events = atomic_read(&event->event_limit); |
6850 | struct hw_perf_event *hwc = &event->hw; | |
e050e3f0 | 6851 | u64 seq; |
79f14641 PZ |
6852 | int ret = 0; |
6853 | ||
96398826 PZ |
6854 | /* |
6855 | * Non-sampling counters might still use the PMI to fold short | |
6856 | * hardware counters, ignore those. | |
6857 | */ | |
6858 | if (unlikely(!is_sampling_event(event))) | |
6859 | return 0; | |
6860 | ||
e050e3f0 SE |
6861 | seq = __this_cpu_read(perf_throttled_seq); |
6862 | if (seq != hwc->interrupts_seq) { | |
6863 | hwc->interrupts_seq = seq; | |
6864 | hwc->interrupts = 1; | |
6865 | } else { | |
6866 | hwc->interrupts++; | |
6867 | if (unlikely(throttle | |
6868 | && hwc->interrupts >= max_samples_per_tick)) { | |
6869 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 6870 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
6871 | hwc->interrupts = MAX_INTERRUPTS; |
6872 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
6873 | ret = 1; |
6874 | } | |
e050e3f0 | 6875 | } |
60db5e09 | 6876 | |
cdd6c482 | 6877 | if (event->attr.freq) { |
def0a9b2 | 6878 | u64 now = perf_clock(); |
abd50713 | 6879 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 6880 | |
abd50713 | 6881 | hwc->freq_time_stamp = now; |
bd2b5b12 | 6882 | |
abd50713 | 6883 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 6884 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
6885 | } |
6886 | ||
2023b359 PZ |
6887 | /* |
6888 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 6889 | * events |
2023b359 PZ |
6890 | */ |
6891 | ||
cdd6c482 IM |
6892 | event->pending_kill = POLL_IN; |
6893 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 6894 | ret = 1; |
cdd6c482 | 6895 | event->pending_kill = POLL_HUP; |
a8b0ca17 PZ |
6896 | event->pending_disable = 1; |
6897 | irq_work_queue(&event->pending); | |
79f14641 PZ |
6898 | } |
6899 | ||
1879445d | 6900 | event->overflow_handler(event, data, regs); |
453f19ee | 6901 | |
fed66e2c | 6902 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
6903 | event->pending_wakeup = 1; |
6904 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
6905 | } |
6906 | ||
79f14641 | 6907 | return ret; |
f6c7d5fe PZ |
6908 | } |
6909 | ||
a8b0ca17 | 6910 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6911 | struct perf_sample_data *data, |
6912 | struct pt_regs *regs) | |
850bc73f | 6913 | { |
a8b0ca17 | 6914 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
6915 | } |
6916 | ||
15dbf27c | 6917 | /* |
cdd6c482 | 6918 | * Generic software event infrastructure |
15dbf27c PZ |
6919 | */ |
6920 | ||
b28ab83c PZ |
6921 | struct swevent_htable { |
6922 | struct swevent_hlist *swevent_hlist; | |
6923 | struct mutex hlist_mutex; | |
6924 | int hlist_refcount; | |
6925 | ||
6926 | /* Recursion avoidance in each contexts */ | |
6927 | int recursion[PERF_NR_CONTEXTS]; | |
6928 | }; | |
6929 | ||
6930 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
6931 | ||
7b4b6658 | 6932 | /* |
cdd6c482 IM |
6933 | * We directly increment event->count and keep a second value in |
6934 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
6935 | * is kept in the range [-sample_period, 0] so that we can use the |
6936 | * sign as trigger. | |
6937 | */ | |
6938 | ||
ab573844 | 6939 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 6940 | { |
cdd6c482 | 6941 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
6942 | u64 period = hwc->last_period; |
6943 | u64 nr, offset; | |
6944 | s64 old, val; | |
6945 | ||
6946 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
6947 | |
6948 | again: | |
e7850595 | 6949 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
6950 | if (val < 0) |
6951 | return 0; | |
15dbf27c | 6952 | |
7b4b6658 PZ |
6953 | nr = div64_u64(period + val, period); |
6954 | offset = nr * period; | |
6955 | val -= offset; | |
e7850595 | 6956 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 6957 | goto again; |
15dbf27c | 6958 | |
7b4b6658 | 6959 | return nr; |
15dbf27c PZ |
6960 | } |
6961 | ||
0cff784a | 6962 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 6963 | struct perf_sample_data *data, |
5622f295 | 6964 | struct pt_regs *regs) |
15dbf27c | 6965 | { |
cdd6c482 | 6966 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 6967 | int throttle = 0; |
15dbf27c | 6968 | |
0cff784a PZ |
6969 | if (!overflow) |
6970 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 6971 | |
7b4b6658 PZ |
6972 | if (hwc->interrupts == MAX_INTERRUPTS) |
6973 | return; | |
15dbf27c | 6974 | |
7b4b6658 | 6975 | for (; overflow; overflow--) { |
a8b0ca17 | 6976 | if (__perf_event_overflow(event, throttle, |
5622f295 | 6977 | data, regs)) { |
7b4b6658 PZ |
6978 | /* |
6979 | * We inhibit the overflow from happening when | |
6980 | * hwc->interrupts == MAX_INTERRUPTS. | |
6981 | */ | |
6982 | break; | |
6983 | } | |
cf450a73 | 6984 | throttle = 1; |
7b4b6658 | 6985 | } |
15dbf27c PZ |
6986 | } |
6987 | ||
a4eaf7f1 | 6988 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 6989 | struct perf_sample_data *data, |
5622f295 | 6990 | struct pt_regs *regs) |
7b4b6658 | 6991 | { |
cdd6c482 | 6992 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 6993 | |
e7850595 | 6994 | local64_add(nr, &event->count); |
d6d020e9 | 6995 | |
0cff784a PZ |
6996 | if (!regs) |
6997 | return; | |
6998 | ||
6c7e550f | 6999 | if (!is_sampling_event(event)) |
7b4b6658 | 7000 | return; |
d6d020e9 | 7001 | |
5d81e5cf AV |
7002 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
7003 | data->period = nr; | |
7004 | return perf_swevent_overflow(event, 1, data, regs); | |
7005 | } else | |
7006 | data->period = event->hw.last_period; | |
7007 | ||
0cff784a | 7008 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 7009 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 7010 | |
e7850595 | 7011 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 7012 | return; |
df1a132b | 7013 | |
a8b0ca17 | 7014 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
7015 | } |
7016 | ||
f5ffe02e FW |
7017 | static int perf_exclude_event(struct perf_event *event, |
7018 | struct pt_regs *regs) | |
7019 | { | |
a4eaf7f1 | 7020 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 7021 | return 1; |
a4eaf7f1 | 7022 | |
f5ffe02e FW |
7023 | if (regs) { |
7024 | if (event->attr.exclude_user && user_mode(regs)) | |
7025 | return 1; | |
7026 | ||
7027 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
7028 | return 1; | |
7029 | } | |
7030 | ||
7031 | return 0; | |
7032 | } | |
7033 | ||
cdd6c482 | 7034 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 7035 | enum perf_type_id type, |
6fb2915d LZ |
7036 | u32 event_id, |
7037 | struct perf_sample_data *data, | |
7038 | struct pt_regs *regs) | |
15dbf27c | 7039 | { |
cdd6c482 | 7040 | if (event->attr.type != type) |
a21ca2ca | 7041 | return 0; |
f5ffe02e | 7042 | |
cdd6c482 | 7043 | if (event->attr.config != event_id) |
15dbf27c PZ |
7044 | return 0; |
7045 | ||
f5ffe02e FW |
7046 | if (perf_exclude_event(event, regs)) |
7047 | return 0; | |
15dbf27c PZ |
7048 | |
7049 | return 1; | |
7050 | } | |
7051 | ||
76e1d904 FW |
7052 | static inline u64 swevent_hash(u64 type, u32 event_id) |
7053 | { | |
7054 | u64 val = event_id | (type << 32); | |
7055 | ||
7056 | return hash_64(val, SWEVENT_HLIST_BITS); | |
7057 | } | |
7058 | ||
49f135ed FW |
7059 | static inline struct hlist_head * |
7060 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 7061 | { |
49f135ed FW |
7062 | u64 hash = swevent_hash(type, event_id); |
7063 | ||
7064 | return &hlist->heads[hash]; | |
7065 | } | |
76e1d904 | 7066 | |
49f135ed FW |
7067 | /* For the read side: events when they trigger */ |
7068 | static inline struct hlist_head * | |
b28ab83c | 7069 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
7070 | { |
7071 | struct swevent_hlist *hlist; | |
76e1d904 | 7072 | |
b28ab83c | 7073 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
7074 | if (!hlist) |
7075 | return NULL; | |
7076 | ||
49f135ed FW |
7077 | return __find_swevent_head(hlist, type, event_id); |
7078 | } | |
7079 | ||
7080 | /* For the event head insertion and removal in the hlist */ | |
7081 | static inline struct hlist_head * | |
b28ab83c | 7082 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
7083 | { |
7084 | struct swevent_hlist *hlist; | |
7085 | u32 event_id = event->attr.config; | |
7086 | u64 type = event->attr.type; | |
7087 | ||
7088 | /* | |
7089 | * Event scheduling is always serialized against hlist allocation | |
7090 | * and release. Which makes the protected version suitable here. | |
7091 | * The context lock guarantees that. | |
7092 | */ | |
b28ab83c | 7093 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
7094 | lockdep_is_held(&event->ctx->lock)); |
7095 | if (!hlist) | |
7096 | return NULL; | |
7097 | ||
7098 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
7099 | } |
7100 | ||
7101 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 7102 | u64 nr, |
76e1d904 FW |
7103 | struct perf_sample_data *data, |
7104 | struct pt_regs *regs) | |
15dbf27c | 7105 | { |
4a32fea9 | 7106 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7107 | struct perf_event *event; |
76e1d904 | 7108 | struct hlist_head *head; |
15dbf27c | 7109 | |
76e1d904 | 7110 | rcu_read_lock(); |
b28ab83c | 7111 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
7112 | if (!head) |
7113 | goto end; | |
7114 | ||
b67bfe0d | 7115 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 7116 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 7117 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 7118 | } |
76e1d904 FW |
7119 | end: |
7120 | rcu_read_unlock(); | |
15dbf27c PZ |
7121 | } |
7122 | ||
86038c5e PZI |
7123 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
7124 | ||
4ed7c92d | 7125 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 7126 | { |
4a32fea9 | 7127 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 7128 | |
b28ab83c | 7129 | return get_recursion_context(swhash->recursion); |
96f6d444 | 7130 | } |
645e8cc0 | 7131 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 7132 | |
fa9f90be | 7133 | inline void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 7134 | { |
4a32fea9 | 7135 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 7136 | |
b28ab83c | 7137 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 7138 | } |
15dbf27c | 7139 | |
86038c5e | 7140 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 7141 | { |
a4234bfc | 7142 | struct perf_sample_data data; |
4ed7c92d | 7143 | |
86038c5e | 7144 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 7145 | return; |
a4234bfc | 7146 | |
fd0d000b | 7147 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 7148 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
7149 | } |
7150 | ||
7151 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
7152 | { | |
7153 | int rctx; | |
7154 | ||
7155 | preempt_disable_notrace(); | |
7156 | rctx = perf_swevent_get_recursion_context(); | |
7157 | if (unlikely(rctx < 0)) | |
7158 | goto fail; | |
7159 | ||
7160 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
7161 | |
7162 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 7163 | fail: |
1c024eca | 7164 | preempt_enable_notrace(); |
b8e83514 PZ |
7165 | } |
7166 | ||
cdd6c482 | 7167 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 7168 | { |
15dbf27c PZ |
7169 | } |
7170 | ||
a4eaf7f1 | 7171 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 7172 | { |
4a32fea9 | 7173 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 7174 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
7175 | struct hlist_head *head; |
7176 | ||
6c7e550f | 7177 | if (is_sampling_event(event)) { |
7b4b6658 | 7178 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 7179 | perf_swevent_set_period(event); |
7b4b6658 | 7180 | } |
76e1d904 | 7181 | |
a4eaf7f1 PZ |
7182 | hwc->state = !(flags & PERF_EF_START); |
7183 | ||
b28ab83c | 7184 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 7185 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
7186 | return -EINVAL; |
7187 | ||
7188 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 7189 | perf_event_update_userpage(event); |
76e1d904 | 7190 | |
15dbf27c PZ |
7191 | return 0; |
7192 | } | |
7193 | ||
a4eaf7f1 | 7194 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 7195 | { |
76e1d904 | 7196 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
7197 | } |
7198 | ||
a4eaf7f1 | 7199 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 7200 | { |
a4eaf7f1 | 7201 | event->hw.state = 0; |
d6d020e9 | 7202 | } |
aa9c4c0f | 7203 | |
a4eaf7f1 | 7204 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 7205 | { |
a4eaf7f1 | 7206 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
7207 | } |
7208 | ||
49f135ed FW |
7209 | /* Deref the hlist from the update side */ |
7210 | static inline struct swevent_hlist * | |
b28ab83c | 7211 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 7212 | { |
b28ab83c PZ |
7213 | return rcu_dereference_protected(swhash->swevent_hlist, |
7214 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
7215 | } |
7216 | ||
b28ab83c | 7217 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 7218 | { |
b28ab83c | 7219 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 7220 | |
49f135ed | 7221 | if (!hlist) |
76e1d904 FW |
7222 | return; |
7223 | ||
70691d4a | 7224 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 7225 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
7226 | } |
7227 | ||
3b364d7b | 7228 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 7229 | { |
b28ab83c | 7230 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 7231 | |
b28ab83c | 7232 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 7233 | |
b28ab83c PZ |
7234 | if (!--swhash->hlist_refcount) |
7235 | swevent_hlist_release(swhash); | |
76e1d904 | 7236 | |
b28ab83c | 7237 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7238 | } |
7239 | ||
3b364d7b | 7240 | static void swevent_hlist_put(void) |
76e1d904 FW |
7241 | { |
7242 | int cpu; | |
7243 | ||
76e1d904 | 7244 | for_each_possible_cpu(cpu) |
3b364d7b | 7245 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7246 | } |
7247 | ||
3b364d7b | 7248 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 7249 | { |
b28ab83c | 7250 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
7251 | int err = 0; |
7252 | ||
b28ab83c | 7253 | mutex_lock(&swhash->hlist_mutex); |
b28ab83c | 7254 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
76e1d904 FW |
7255 | struct swevent_hlist *hlist; |
7256 | ||
7257 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
7258 | if (!hlist) { | |
7259 | err = -ENOMEM; | |
7260 | goto exit; | |
7261 | } | |
b28ab83c | 7262 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 7263 | } |
b28ab83c | 7264 | swhash->hlist_refcount++; |
9ed6060d | 7265 | exit: |
b28ab83c | 7266 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
7267 | |
7268 | return err; | |
7269 | } | |
7270 | ||
3b364d7b | 7271 | static int swevent_hlist_get(void) |
76e1d904 | 7272 | { |
3b364d7b | 7273 | int err, cpu, failed_cpu; |
76e1d904 | 7274 | |
76e1d904 FW |
7275 | get_online_cpus(); |
7276 | for_each_possible_cpu(cpu) { | |
3b364d7b | 7277 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
7278 | if (err) { |
7279 | failed_cpu = cpu; | |
7280 | goto fail; | |
7281 | } | |
7282 | } | |
7283 | put_online_cpus(); | |
7284 | ||
7285 | return 0; | |
9ed6060d | 7286 | fail: |
76e1d904 FW |
7287 | for_each_possible_cpu(cpu) { |
7288 | if (cpu == failed_cpu) | |
7289 | break; | |
3b364d7b | 7290 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7291 | } |
7292 | ||
7293 | put_online_cpus(); | |
7294 | return err; | |
7295 | } | |
7296 | ||
c5905afb | 7297 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 7298 | |
b0a873eb PZ |
7299 | static void sw_perf_event_destroy(struct perf_event *event) |
7300 | { | |
7301 | u64 event_id = event->attr.config; | |
95476b64 | 7302 | |
b0a873eb PZ |
7303 | WARN_ON(event->parent); |
7304 | ||
c5905afb | 7305 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 7306 | swevent_hlist_put(); |
b0a873eb PZ |
7307 | } |
7308 | ||
7309 | static int perf_swevent_init(struct perf_event *event) | |
7310 | { | |
8176cced | 7311 | u64 event_id = event->attr.config; |
b0a873eb PZ |
7312 | |
7313 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7314 | return -ENOENT; | |
7315 | ||
2481c5fa SE |
7316 | /* |
7317 | * no branch sampling for software events | |
7318 | */ | |
7319 | if (has_branch_stack(event)) | |
7320 | return -EOPNOTSUPP; | |
7321 | ||
b0a873eb PZ |
7322 | switch (event_id) { |
7323 | case PERF_COUNT_SW_CPU_CLOCK: | |
7324 | case PERF_COUNT_SW_TASK_CLOCK: | |
7325 | return -ENOENT; | |
7326 | ||
7327 | default: | |
7328 | break; | |
7329 | } | |
7330 | ||
ce677831 | 7331 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
7332 | return -ENOENT; |
7333 | ||
7334 | if (!event->parent) { | |
7335 | int err; | |
7336 | ||
3b364d7b | 7337 | err = swevent_hlist_get(); |
b0a873eb PZ |
7338 | if (err) |
7339 | return err; | |
7340 | ||
c5905afb | 7341 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
7342 | event->destroy = sw_perf_event_destroy; |
7343 | } | |
7344 | ||
7345 | return 0; | |
7346 | } | |
7347 | ||
7348 | static struct pmu perf_swevent = { | |
89a1e187 | 7349 | .task_ctx_nr = perf_sw_context, |
95476b64 | 7350 | |
34f43927 PZ |
7351 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7352 | ||
b0a873eb | 7353 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
7354 | .add = perf_swevent_add, |
7355 | .del = perf_swevent_del, | |
7356 | .start = perf_swevent_start, | |
7357 | .stop = perf_swevent_stop, | |
1c024eca | 7358 | .read = perf_swevent_read, |
1c024eca PZ |
7359 | }; |
7360 | ||
b0a873eb PZ |
7361 | #ifdef CONFIG_EVENT_TRACING |
7362 | ||
1c024eca PZ |
7363 | static int perf_tp_filter_match(struct perf_event *event, |
7364 | struct perf_sample_data *data) | |
7365 | { | |
7366 | void *record = data->raw->data; | |
7367 | ||
b71b437e PZ |
7368 | /* only top level events have filters set */ |
7369 | if (event->parent) | |
7370 | event = event->parent; | |
7371 | ||
1c024eca PZ |
7372 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
7373 | return 1; | |
7374 | return 0; | |
7375 | } | |
7376 | ||
7377 | static int perf_tp_event_match(struct perf_event *event, | |
7378 | struct perf_sample_data *data, | |
7379 | struct pt_regs *regs) | |
7380 | { | |
a0f7d0f7 FW |
7381 | if (event->hw.state & PERF_HES_STOPPED) |
7382 | return 0; | |
580d607c PZ |
7383 | /* |
7384 | * All tracepoints are from kernel-space. | |
7385 | */ | |
7386 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
7387 | return 0; |
7388 | ||
7389 | if (!perf_tp_filter_match(event, data)) | |
7390 | return 0; | |
7391 | ||
7392 | return 1; | |
7393 | } | |
7394 | ||
7395 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, | |
e6dab5ff AV |
7396 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
7397 | struct task_struct *task) | |
95476b64 FW |
7398 | { |
7399 | struct perf_sample_data data; | |
1c024eca | 7400 | struct perf_event *event; |
1c024eca | 7401 | |
95476b64 FW |
7402 | struct perf_raw_record raw = { |
7403 | .size = entry_size, | |
7404 | .data = record, | |
7405 | }; | |
7406 | ||
fd0d000b | 7407 | perf_sample_data_init(&data, addr, 0); |
95476b64 FW |
7408 | data.raw = &raw; |
7409 | ||
b67bfe0d | 7410 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 7411 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 7412 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 7413 | } |
ecc55f84 | 7414 | |
e6dab5ff AV |
7415 | /* |
7416 | * If we got specified a target task, also iterate its context and | |
7417 | * deliver this event there too. | |
7418 | */ | |
7419 | if (task && task != current) { | |
7420 | struct perf_event_context *ctx; | |
7421 | struct trace_entry *entry = record; | |
7422 | ||
7423 | rcu_read_lock(); | |
7424 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
7425 | if (!ctx) | |
7426 | goto unlock; | |
7427 | ||
7428 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
7429 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7430 | continue; | |
7431 | if (event->attr.config != entry->type) | |
7432 | continue; | |
7433 | if (perf_tp_event_match(event, &data, regs)) | |
7434 | perf_swevent_event(event, count, &data, regs); | |
7435 | } | |
7436 | unlock: | |
7437 | rcu_read_unlock(); | |
7438 | } | |
7439 | ||
ecc55f84 | 7440 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
7441 | } |
7442 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
7443 | ||
cdd6c482 | 7444 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 7445 | { |
1c024eca | 7446 | perf_trace_destroy(event); |
e077df4f PZ |
7447 | } |
7448 | ||
b0a873eb | 7449 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 7450 | { |
76e1d904 FW |
7451 | int err; |
7452 | ||
b0a873eb PZ |
7453 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
7454 | return -ENOENT; | |
7455 | ||
2481c5fa SE |
7456 | /* |
7457 | * no branch sampling for tracepoint events | |
7458 | */ | |
7459 | if (has_branch_stack(event)) | |
7460 | return -EOPNOTSUPP; | |
7461 | ||
1c024eca PZ |
7462 | err = perf_trace_init(event); |
7463 | if (err) | |
b0a873eb | 7464 | return err; |
e077df4f | 7465 | |
cdd6c482 | 7466 | event->destroy = tp_perf_event_destroy; |
e077df4f | 7467 | |
b0a873eb PZ |
7468 | return 0; |
7469 | } | |
7470 | ||
7471 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
7472 | .task_ctx_nr = perf_sw_context, |
7473 | ||
b0a873eb | 7474 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
7475 | .add = perf_trace_add, |
7476 | .del = perf_trace_del, | |
7477 | .start = perf_swevent_start, | |
7478 | .stop = perf_swevent_stop, | |
b0a873eb | 7479 | .read = perf_swevent_read, |
b0a873eb PZ |
7480 | }; |
7481 | ||
7482 | static inline void perf_tp_register(void) | |
7483 | { | |
2e80a82a | 7484 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e077df4f | 7485 | } |
6fb2915d | 7486 | |
6fb2915d LZ |
7487 | static void perf_event_free_filter(struct perf_event *event) |
7488 | { | |
7489 | ftrace_profile_free_filter(event); | |
7490 | } | |
7491 | ||
2541517c AS |
7492 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7493 | { | |
7494 | struct bpf_prog *prog; | |
7495 | ||
7496 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7497 | return -EINVAL; | |
7498 | ||
7499 | if (event->tp_event->prog) | |
7500 | return -EEXIST; | |
7501 | ||
04a22fae WN |
7502 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
7503 | /* bpf programs can only be attached to u/kprobes */ | |
2541517c AS |
7504 | return -EINVAL; |
7505 | ||
7506 | prog = bpf_prog_get(prog_fd); | |
7507 | if (IS_ERR(prog)) | |
7508 | return PTR_ERR(prog); | |
7509 | ||
6c373ca8 | 7510 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
2541517c AS |
7511 | /* valid fd, but invalid bpf program type */ |
7512 | bpf_prog_put(prog); | |
7513 | return -EINVAL; | |
7514 | } | |
7515 | ||
7516 | event->tp_event->prog = prog; | |
7517 | ||
7518 | return 0; | |
7519 | } | |
7520 | ||
7521 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7522 | { | |
7523 | struct bpf_prog *prog; | |
7524 | ||
7525 | if (!event->tp_event) | |
7526 | return; | |
7527 | ||
7528 | prog = event->tp_event->prog; | |
7529 | if (prog) { | |
7530 | event->tp_event->prog = NULL; | |
7531 | bpf_prog_put(prog); | |
7532 | } | |
7533 | } | |
7534 | ||
e077df4f | 7535 | #else |
6fb2915d | 7536 | |
b0a873eb | 7537 | static inline void perf_tp_register(void) |
e077df4f | 7538 | { |
e077df4f | 7539 | } |
6fb2915d | 7540 | |
6fb2915d LZ |
7541 | static void perf_event_free_filter(struct perf_event *event) |
7542 | { | |
7543 | } | |
7544 | ||
2541517c AS |
7545 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7546 | { | |
7547 | return -ENOENT; | |
7548 | } | |
7549 | ||
7550 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7551 | { | |
7552 | } | |
07b139c8 | 7553 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 7554 | |
24f1e32c | 7555 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 7556 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 7557 | { |
f5ffe02e FW |
7558 | struct perf_sample_data sample; |
7559 | struct pt_regs *regs = data; | |
7560 | ||
fd0d000b | 7561 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 7562 | |
a4eaf7f1 | 7563 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 7564 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
7565 | } |
7566 | #endif | |
7567 | ||
375637bc AS |
7568 | /* |
7569 | * Allocate a new address filter | |
7570 | */ | |
7571 | static struct perf_addr_filter * | |
7572 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
7573 | { | |
7574 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
7575 | struct perf_addr_filter *filter; | |
7576 | ||
7577 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
7578 | if (!filter) | |
7579 | return NULL; | |
7580 | ||
7581 | INIT_LIST_HEAD(&filter->entry); | |
7582 | list_add_tail(&filter->entry, filters); | |
7583 | ||
7584 | return filter; | |
7585 | } | |
7586 | ||
7587 | static void free_filters_list(struct list_head *filters) | |
7588 | { | |
7589 | struct perf_addr_filter *filter, *iter; | |
7590 | ||
7591 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
7592 | if (filter->inode) | |
7593 | iput(filter->inode); | |
7594 | list_del(&filter->entry); | |
7595 | kfree(filter); | |
7596 | } | |
7597 | } | |
7598 | ||
7599 | /* | |
7600 | * Free existing address filters and optionally install new ones | |
7601 | */ | |
7602 | static void perf_addr_filters_splice(struct perf_event *event, | |
7603 | struct list_head *head) | |
7604 | { | |
7605 | unsigned long flags; | |
7606 | LIST_HEAD(list); | |
7607 | ||
7608 | if (!has_addr_filter(event)) | |
7609 | return; | |
7610 | ||
7611 | /* don't bother with children, they don't have their own filters */ | |
7612 | if (event->parent) | |
7613 | return; | |
7614 | ||
7615 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
7616 | ||
7617 | list_splice_init(&event->addr_filters.list, &list); | |
7618 | if (head) | |
7619 | list_splice(head, &event->addr_filters.list); | |
7620 | ||
7621 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
7622 | ||
7623 | free_filters_list(&list); | |
7624 | } | |
7625 | ||
7626 | /* | |
7627 | * Scan through mm's vmas and see if one of them matches the | |
7628 | * @filter; if so, adjust filter's address range. | |
7629 | * Called with mm::mmap_sem down for reading. | |
7630 | */ | |
7631 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, | |
7632 | struct mm_struct *mm) | |
7633 | { | |
7634 | struct vm_area_struct *vma; | |
7635 | ||
7636 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
7637 | struct file *file = vma->vm_file; | |
7638 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
7639 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
7640 | ||
7641 | if (!file) | |
7642 | continue; | |
7643 | ||
7644 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
7645 | continue; | |
7646 | ||
7647 | return vma->vm_start; | |
7648 | } | |
7649 | ||
7650 | return 0; | |
7651 | } | |
7652 | ||
7653 | /* | |
7654 | * Update event's address range filters based on the | |
7655 | * task's existing mappings, if any. | |
7656 | */ | |
7657 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
7658 | { | |
7659 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
7660 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
7661 | struct perf_addr_filter *filter; | |
7662 | struct mm_struct *mm = NULL; | |
7663 | unsigned int count = 0; | |
7664 | unsigned long flags; | |
7665 | ||
7666 | /* | |
7667 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
7668 | * will stop on the parent's child_mutex that our caller is also holding | |
7669 | */ | |
7670 | if (task == TASK_TOMBSTONE) | |
7671 | return; | |
7672 | ||
7673 | mm = get_task_mm(event->ctx->task); | |
7674 | if (!mm) | |
7675 | goto restart; | |
7676 | ||
7677 | down_read(&mm->mmap_sem); | |
7678 | ||
7679 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7680 | list_for_each_entry(filter, &ifh->list, entry) { | |
7681 | event->addr_filters_offs[count] = 0; | |
7682 | ||
7683 | if (perf_addr_filter_needs_mmap(filter)) | |
7684 | event->addr_filters_offs[count] = | |
7685 | perf_addr_filter_apply(filter, mm); | |
7686 | ||
7687 | count++; | |
7688 | } | |
7689 | ||
7690 | event->addr_filters_gen++; | |
7691 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7692 | ||
7693 | up_read(&mm->mmap_sem); | |
7694 | ||
7695 | mmput(mm); | |
7696 | ||
7697 | restart: | |
7698 | perf_event_restart(event); | |
7699 | } | |
7700 | ||
7701 | /* | |
7702 | * Address range filtering: limiting the data to certain | |
7703 | * instruction address ranges. Filters are ioctl()ed to us from | |
7704 | * userspace as ascii strings. | |
7705 | * | |
7706 | * Filter string format: | |
7707 | * | |
7708 | * ACTION RANGE_SPEC | |
7709 | * where ACTION is one of the | |
7710 | * * "filter": limit the trace to this region | |
7711 | * * "start": start tracing from this address | |
7712 | * * "stop": stop tracing at this address/region; | |
7713 | * RANGE_SPEC is | |
7714 | * * for kernel addresses: <start address>[/<size>] | |
7715 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
7716 | * | |
7717 | * if <size> is not specified, the range is treated as a single address. | |
7718 | */ | |
7719 | enum { | |
7720 | IF_ACT_FILTER, | |
7721 | IF_ACT_START, | |
7722 | IF_ACT_STOP, | |
7723 | IF_SRC_FILE, | |
7724 | IF_SRC_KERNEL, | |
7725 | IF_SRC_FILEADDR, | |
7726 | IF_SRC_KERNELADDR, | |
7727 | }; | |
7728 | ||
7729 | enum { | |
7730 | IF_STATE_ACTION = 0, | |
7731 | IF_STATE_SOURCE, | |
7732 | IF_STATE_END, | |
7733 | }; | |
7734 | ||
7735 | static const match_table_t if_tokens = { | |
7736 | { IF_ACT_FILTER, "filter" }, | |
7737 | { IF_ACT_START, "start" }, | |
7738 | { IF_ACT_STOP, "stop" }, | |
7739 | { IF_SRC_FILE, "%u/%u@%s" }, | |
7740 | { IF_SRC_KERNEL, "%u/%u" }, | |
7741 | { IF_SRC_FILEADDR, "%u@%s" }, | |
7742 | { IF_SRC_KERNELADDR, "%u" }, | |
7743 | }; | |
7744 | ||
7745 | /* | |
7746 | * Address filter string parser | |
7747 | */ | |
7748 | static int | |
7749 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
7750 | struct list_head *filters) | |
7751 | { | |
7752 | struct perf_addr_filter *filter = NULL; | |
7753 | char *start, *orig, *filename = NULL; | |
7754 | struct path path; | |
7755 | substring_t args[MAX_OPT_ARGS]; | |
7756 | int state = IF_STATE_ACTION, token; | |
7757 | unsigned int kernel = 0; | |
7758 | int ret = -EINVAL; | |
7759 | ||
7760 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
7761 | if (!fstr) | |
7762 | return -ENOMEM; | |
7763 | ||
7764 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
7765 | ret = -EINVAL; | |
7766 | ||
7767 | if (!*start) | |
7768 | continue; | |
7769 | ||
7770 | /* filter definition begins */ | |
7771 | if (state == IF_STATE_ACTION) { | |
7772 | filter = perf_addr_filter_new(event, filters); | |
7773 | if (!filter) | |
7774 | goto fail; | |
7775 | } | |
7776 | ||
7777 | token = match_token(start, if_tokens, args); | |
7778 | switch (token) { | |
7779 | case IF_ACT_FILTER: | |
7780 | case IF_ACT_START: | |
7781 | filter->filter = 1; | |
7782 | ||
7783 | case IF_ACT_STOP: | |
7784 | if (state != IF_STATE_ACTION) | |
7785 | goto fail; | |
7786 | ||
7787 | state = IF_STATE_SOURCE; | |
7788 | break; | |
7789 | ||
7790 | case IF_SRC_KERNELADDR: | |
7791 | case IF_SRC_KERNEL: | |
7792 | kernel = 1; | |
7793 | ||
7794 | case IF_SRC_FILEADDR: | |
7795 | case IF_SRC_FILE: | |
7796 | if (state != IF_STATE_SOURCE) | |
7797 | goto fail; | |
7798 | ||
7799 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) | |
7800 | filter->range = 1; | |
7801 | ||
7802 | *args[0].to = 0; | |
7803 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
7804 | if (ret) | |
7805 | goto fail; | |
7806 | ||
7807 | if (filter->range) { | |
7808 | *args[1].to = 0; | |
7809 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
7810 | if (ret) | |
7811 | goto fail; | |
7812 | } | |
7813 | ||
7814 | if (token == IF_SRC_FILE) { | |
7815 | filename = match_strdup(&args[2]); | |
7816 | if (!filename) { | |
7817 | ret = -ENOMEM; | |
7818 | goto fail; | |
7819 | } | |
7820 | } | |
7821 | ||
7822 | state = IF_STATE_END; | |
7823 | break; | |
7824 | ||
7825 | default: | |
7826 | goto fail; | |
7827 | } | |
7828 | ||
7829 | /* | |
7830 | * Filter definition is fully parsed, validate and install it. | |
7831 | * Make sure that it doesn't contradict itself or the event's | |
7832 | * attribute. | |
7833 | */ | |
7834 | if (state == IF_STATE_END) { | |
7835 | if (kernel && event->attr.exclude_kernel) | |
7836 | goto fail; | |
7837 | ||
7838 | if (!kernel) { | |
7839 | if (!filename) | |
7840 | goto fail; | |
7841 | ||
7842 | /* look up the path and grab its inode */ | |
7843 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
7844 | if (ret) | |
7845 | goto fail_free_name; | |
7846 | ||
7847 | filter->inode = igrab(d_inode(path.dentry)); | |
7848 | path_put(&path); | |
7849 | kfree(filename); | |
7850 | filename = NULL; | |
7851 | ||
7852 | ret = -EINVAL; | |
7853 | if (!filter->inode || | |
7854 | !S_ISREG(filter->inode->i_mode)) | |
7855 | /* free_filters_list() will iput() */ | |
7856 | goto fail; | |
7857 | } | |
7858 | ||
7859 | /* ready to consume more filters */ | |
7860 | state = IF_STATE_ACTION; | |
7861 | filter = NULL; | |
7862 | } | |
7863 | } | |
7864 | ||
7865 | if (state != IF_STATE_ACTION) | |
7866 | goto fail; | |
7867 | ||
7868 | kfree(orig); | |
7869 | ||
7870 | return 0; | |
7871 | ||
7872 | fail_free_name: | |
7873 | kfree(filename); | |
7874 | fail: | |
7875 | free_filters_list(filters); | |
7876 | kfree(orig); | |
7877 | ||
7878 | return ret; | |
7879 | } | |
7880 | ||
7881 | static int | |
7882 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
7883 | { | |
7884 | LIST_HEAD(filters); | |
7885 | int ret; | |
7886 | ||
7887 | /* | |
7888 | * Since this is called in perf_ioctl() path, we're already holding | |
7889 | * ctx::mutex. | |
7890 | */ | |
7891 | lockdep_assert_held(&event->ctx->mutex); | |
7892 | ||
7893 | if (WARN_ON_ONCE(event->parent)) | |
7894 | return -EINVAL; | |
7895 | ||
7896 | /* | |
7897 | * For now, we only support filtering in per-task events; doing so | |
7898 | * for CPU-wide events requires additional context switching trickery, | |
7899 | * since same object code will be mapped at different virtual | |
7900 | * addresses in different processes. | |
7901 | */ | |
7902 | if (!event->ctx->task) | |
7903 | return -EOPNOTSUPP; | |
7904 | ||
7905 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); | |
7906 | if (ret) | |
7907 | return ret; | |
7908 | ||
7909 | ret = event->pmu->addr_filters_validate(&filters); | |
7910 | if (ret) { | |
7911 | free_filters_list(&filters); | |
7912 | return ret; | |
7913 | } | |
7914 | ||
7915 | /* remove existing filters, if any */ | |
7916 | perf_addr_filters_splice(event, &filters); | |
7917 | ||
7918 | /* install new filters */ | |
7919 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
7920 | ||
7921 | return ret; | |
7922 | } | |
7923 | ||
c796bbbe AS |
7924 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
7925 | { | |
7926 | char *filter_str; | |
7927 | int ret = -EINVAL; | |
7928 | ||
375637bc AS |
7929 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
7930 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && | |
7931 | !has_addr_filter(event)) | |
c796bbbe AS |
7932 | return -EINVAL; |
7933 | ||
7934 | filter_str = strndup_user(arg, PAGE_SIZE); | |
7935 | if (IS_ERR(filter_str)) | |
7936 | return PTR_ERR(filter_str); | |
7937 | ||
7938 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && | |
7939 | event->attr.type == PERF_TYPE_TRACEPOINT) | |
7940 | ret = ftrace_profile_set_filter(event, event->attr.config, | |
7941 | filter_str); | |
375637bc AS |
7942 | else if (has_addr_filter(event)) |
7943 | ret = perf_event_set_addr_filter(event, filter_str); | |
c796bbbe AS |
7944 | |
7945 | kfree(filter_str); | |
7946 | return ret; | |
7947 | } | |
7948 | ||
b0a873eb PZ |
7949 | /* |
7950 | * hrtimer based swevent callback | |
7951 | */ | |
f29ac756 | 7952 | |
b0a873eb | 7953 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 7954 | { |
b0a873eb PZ |
7955 | enum hrtimer_restart ret = HRTIMER_RESTART; |
7956 | struct perf_sample_data data; | |
7957 | struct pt_regs *regs; | |
7958 | struct perf_event *event; | |
7959 | u64 period; | |
f29ac756 | 7960 | |
b0a873eb | 7961 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
7962 | |
7963 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
7964 | return HRTIMER_NORESTART; | |
7965 | ||
b0a873eb | 7966 | event->pmu->read(event); |
f344011c | 7967 | |
fd0d000b | 7968 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
7969 | regs = get_irq_regs(); |
7970 | ||
7971 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 7972 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 7973 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
7974 | ret = HRTIMER_NORESTART; |
7975 | } | |
24f1e32c | 7976 | |
b0a873eb PZ |
7977 | period = max_t(u64, 10000, event->hw.sample_period); |
7978 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 7979 | |
b0a873eb | 7980 | return ret; |
f29ac756 PZ |
7981 | } |
7982 | ||
b0a873eb | 7983 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 7984 | { |
b0a873eb | 7985 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
7986 | s64 period; |
7987 | ||
7988 | if (!is_sampling_event(event)) | |
7989 | return; | |
f5ffe02e | 7990 | |
5d508e82 FBH |
7991 | period = local64_read(&hwc->period_left); |
7992 | if (period) { | |
7993 | if (period < 0) | |
7994 | period = 10000; | |
fa407f35 | 7995 | |
5d508e82 FBH |
7996 | local64_set(&hwc->period_left, 0); |
7997 | } else { | |
7998 | period = max_t(u64, 10000, hwc->sample_period); | |
7999 | } | |
3497d206 TG |
8000 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
8001 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 8002 | } |
b0a873eb PZ |
8003 | |
8004 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 8005 | { |
b0a873eb PZ |
8006 | struct hw_perf_event *hwc = &event->hw; |
8007 | ||
6c7e550f | 8008 | if (is_sampling_event(event)) { |
b0a873eb | 8009 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 8010 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
8011 | |
8012 | hrtimer_cancel(&hwc->hrtimer); | |
8013 | } | |
24f1e32c FW |
8014 | } |
8015 | ||
ba3dd36c PZ |
8016 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
8017 | { | |
8018 | struct hw_perf_event *hwc = &event->hw; | |
8019 | ||
8020 | if (!is_sampling_event(event)) | |
8021 | return; | |
8022 | ||
8023 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
8024 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
8025 | ||
8026 | /* | |
8027 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
8028 | * mapping and avoid the whole period adjust feedback stuff. | |
8029 | */ | |
8030 | if (event->attr.freq) { | |
8031 | long freq = event->attr.sample_freq; | |
8032 | ||
8033 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
8034 | hwc->sample_period = event->attr.sample_period; | |
8035 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 8036 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
8037 | event->attr.freq = 0; |
8038 | } | |
8039 | } | |
8040 | ||
b0a873eb PZ |
8041 | /* |
8042 | * Software event: cpu wall time clock | |
8043 | */ | |
8044 | ||
8045 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 8046 | { |
b0a873eb PZ |
8047 | s64 prev; |
8048 | u64 now; | |
8049 | ||
a4eaf7f1 | 8050 | now = local_clock(); |
b0a873eb PZ |
8051 | prev = local64_xchg(&event->hw.prev_count, now); |
8052 | local64_add(now - prev, &event->count); | |
24f1e32c | 8053 | } |
24f1e32c | 8054 | |
a4eaf7f1 | 8055 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8056 | { |
a4eaf7f1 | 8057 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 8058 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8059 | } |
8060 | ||
a4eaf7f1 | 8061 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 8062 | { |
b0a873eb PZ |
8063 | perf_swevent_cancel_hrtimer(event); |
8064 | cpu_clock_event_update(event); | |
8065 | } | |
f29ac756 | 8066 | |
a4eaf7f1 PZ |
8067 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
8068 | { | |
8069 | if (flags & PERF_EF_START) | |
8070 | cpu_clock_event_start(event, flags); | |
6a694a60 | 8071 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
8072 | |
8073 | return 0; | |
8074 | } | |
8075 | ||
8076 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
8077 | { | |
8078 | cpu_clock_event_stop(event, flags); | |
8079 | } | |
8080 | ||
b0a873eb PZ |
8081 | static void cpu_clock_event_read(struct perf_event *event) |
8082 | { | |
8083 | cpu_clock_event_update(event); | |
8084 | } | |
f344011c | 8085 | |
b0a873eb PZ |
8086 | static int cpu_clock_event_init(struct perf_event *event) |
8087 | { | |
8088 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
8089 | return -ENOENT; | |
8090 | ||
8091 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
8092 | return -ENOENT; | |
8093 | ||
2481c5fa SE |
8094 | /* |
8095 | * no branch sampling for software events | |
8096 | */ | |
8097 | if (has_branch_stack(event)) | |
8098 | return -EOPNOTSUPP; | |
8099 | ||
ba3dd36c PZ |
8100 | perf_swevent_init_hrtimer(event); |
8101 | ||
b0a873eb | 8102 | return 0; |
f29ac756 PZ |
8103 | } |
8104 | ||
b0a873eb | 8105 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
8106 | .task_ctx_nr = perf_sw_context, |
8107 | ||
34f43927 PZ |
8108 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8109 | ||
b0a873eb | 8110 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
8111 | .add = cpu_clock_event_add, |
8112 | .del = cpu_clock_event_del, | |
8113 | .start = cpu_clock_event_start, | |
8114 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
8115 | .read = cpu_clock_event_read, |
8116 | }; | |
8117 | ||
8118 | /* | |
8119 | * Software event: task time clock | |
8120 | */ | |
8121 | ||
8122 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 8123 | { |
b0a873eb PZ |
8124 | u64 prev; |
8125 | s64 delta; | |
5c92d124 | 8126 | |
b0a873eb PZ |
8127 | prev = local64_xchg(&event->hw.prev_count, now); |
8128 | delta = now - prev; | |
8129 | local64_add(delta, &event->count); | |
8130 | } | |
5c92d124 | 8131 | |
a4eaf7f1 | 8132 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 8133 | { |
a4eaf7f1 | 8134 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 8135 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
8136 | } |
8137 | ||
a4eaf7f1 | 8138 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
8139 | { |
8140 | perf_swevent_cancel_hrtimer(event); | |
8141 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
8142 | } |
8143 | ||
8144 | static int task_clock_event_add(struct perf_event *event, int flags) | |
8145 | { | |
8146 | if (flags & PERF_EF_START) | |
8147 | task_clock_event_start(event, flags); | |
6a694a60 | 8148 | perf_event_update_userpage(event); |
b0a873eb | 8149 | |
a4eaf7f1 PZ |
8150 | return 0; |
8151 | } | |
8152 | ||
8153 | static void task_clock_event_del(struct perf_event *event, int flags) | |
8154 | { | |
8155 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
8156 | } |
8157 | ||
8158 | static void task_clock_event_read(struct perf_event *event) | |
8159 | { | |
768a06e2 PZ |
8160 | u64 now = perf_clock(); |
8161 | u64 delta = now - event->ctx->timestamp; | |
8162 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
8163 | |
8164 | task_clock_event_update(event, time); | |
8165 | } | |
8166 | ||
8167 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 8168 | { |
b0a873eb PZ |
8169 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
8170 | return -ENOENT; | |
8171 | ||
8172 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
8173 | return -ENOENT; | |
8174 | ||
2481c5fa SE |
8175 | /* |
8176 | * no branch sampling for software events | |
8177 | */ | |
8178 | if (has_branch_stack(event)) | |
8179 | return -EOPNOTSUPP; | |
8180 | ||
ba3dd36c PZ |
8181 | perf_swevent_init_hrtimer(event); |
8182 | ||
b0a873eb | 8183 | return 0; |
6fb2915d LZ |
8184 | } |
8185 | ||
b0a873eb | 8186 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
8187 | .task_ctx_nr = perf_sw_context, |
8188 | ||
34f43927 PZ |
8189 | .capabilities = PERF_PMU_CAP_NO_NMI, |
8190 | ||
b0a873eb | 8191 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
8192 | .add = task_clock_event_add, |
8193 | .del = task_clock_event_del, | |
8194 | .start = task_clock_event_start, | |
8195 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
8196 | .read = task_clock_event_read, |
8197 | }; | |
6fb2915d | 8198 | |
ad5133b7 | 8199 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 8200 | { |
e077df4f | 8201 | } |
6fb2915d | 8202 | |
fbbe0701 SB |
8203 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
8204 | { | |
8205 | } | |
8206 | ||
ad5133b7 | 8207 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 8208 | { |
ad5133b7 | 8209 | return 0; |
6fb2915d LZ |
8210 | } |
8211 | ||
18ab2cd3 | 8212 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
8213 | |
8214 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 8215 | { |
fbbe0701 SB |
8216 | __this_cpu_write(nop_txn_flags, flags); |
8217 | ||
8218 | if (flags & ~PERF_PMU_TXN_ADD) | |
8219 | return; | |
8220 | ||
ad5133b7 | 8221 | perf_pmu_disable(pmu); |
6fb2915d LZ |
8222 | } |
8223 | ||
ad5133b7 PZ |
8224 | static int perf_pmu_commit_txn(struct pmu *pmu) |
8225 | { | |
fbbe0701 SB |
8226 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8227 | ||
8228 | __this_cpu_write(nop_txn_flags, 0); | |
8229 | ||
8230 | if (flags & ~PERF_PMU_TXN_ADD) | |
8231 | return 0; | |
8232 | ||
ad5133b7 PZ |
8233 | perf_pmu_enable(pmu); |
8234 | return 0; | |
8235 | } | |
e077df4f | 8236 | |
ad5133b7 | 8237 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 8238 | { |
fbbe0701 SB |
8239 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
8240 | ||
8241 | __this_cpu_write(nop_txn_flags, 0); | |
8242 | ||
8243 | if (flags & ~PERF_PMU_TXN_ADD) | |
8244 | return; | |
8245 | ||
ad5133b7 | 8246 | perf_pmu_enable(pmu); |
24f1e32c FW |
8247 | } |
8248 | ||
35edc2a5 PZ |
8249 | static int perf_event_idx_default(struct perf_event *event) |
8250 | { | |
c719f560 | 8251 | return 0; |
35edc2a5 PZ |
8252 | } |
8253 | ||
8dc85d54 PZ |
8254 | /* |
8255 | * Ensures all contexts with the same task_ctx_nr have the same | |
8256 | * pmu_cpu_context too. | |
8257 | */ | |
9e317041 | 8258 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 8259 | { |
8dc85d54 | 8260 | struct pmu *pmu; |
b326e956 | 8261 | |
8dc85d54 PZ |
8262 | if (ctxn < 0) |
8263 | return NULL; | |
24f1e32c | 8264 | |
8dc85d54 PZ |
8265 | list_for_each_entry(pmu, &pmus, entry) { |
8266 | if (pmu->task_ctx_nr == ctxn) | |
8267 | return pmu->pmu_cpu_context; | |
8268 | } | |
24f1e32c | 8269 | |
8dc85d54 | 8270 | return NULL; |
24f1e32c FW |
8271 | } |
8272 | ||
51676957 | 8273 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
24f1e32c | 8274 | { |
51676957 PZ |
8275 | int cpu; |
8276 | ||
8277 | for_each_possible_cpu(cpu) { | |
8278 | struct perf_cpu_context *cpuctx; | |
8279 | ||
8280 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8281 | ||
3f1f3320 PZ |
8282 | if (cpuctx->unique_pmu == old_pmu) |
8283 | cpuctx->unique_pmu = pmu; | |
51676957 PZ |
8284 | } |
8285 | } | |
8286 | ||
8287 | static void free_pmu_context(struct pmu *pmu) | |
8288 | { | |
8289 | struct pmu *i; | |
f5ffe02e | 8290 | |
8dc85d54 | 8291 | mutex_lock(&pmus_lock); |
0475f9ea | 8292 | /* |
8dc85d54 | 8293 | * Like a real lame refcount. |
0475f9ea | 8294 | */ |
51676957 PZ |
8295 | list_for_each_entry(i, &pmus, entry) { |
8296 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { | |
8297 | update_pmu_context(i, pmu); | |
8dc85d54 | 8298 | goto out; |
51676957 | 8299 | } |
8dc85d54 | 8300 | } |
d6d020e9 | 8301 | |
51676957 | 8302 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 PZ |
8303 | out: |
8304 | mutex_unlock(&pmus_lock); | |
24f1e32c | 8305 | } |
6e855cd4 AS |
8306 | |
8307 | /* | |
8308 | * Let userspace know that this PMU supports address range filtering: | |
8309 | */ | |
8310 | static ssize_t nr_addr_filters_show(struct device *dev, | |
8311 | struct device_attribute *attr, | |
8312 | char *page) | |
8313 | { | |
8314 | struct pmu *pmu = dev_get_drvdata(dev); | |
8315 | ||
8316 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
8317 | } | |
8318 | DEVICE_ATTR_RO(nr_addr_filters); | |
8319 | ||
2e80a82a | 8320 | static struct idr pmu_idr; |
d6d020e9 | 8321 | |
abe43400 PZ |
8322 | static ssize_t |
8323 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
8324 | { | |
8325 | struct pmu *pmu = dev_get_drvdata(dev); | |
8326 | ||
8327 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
8328 | } | |
90826ca7 | 8329 | static DEVICE_ATTR_RO(type); |
abe43400 | 8330 | |
62b85639 SE |
8331 | static ssize_t |
8332 | perf_event_mux_interval_ms_show(struct device *dev, | |
8333 | struct device_attribute *attr, | |
8334 | char *page) | |
8335 | { | |
8336 | struct pmu *pmu = dev_get_drvdata(dev); | |
8337 | ||
8338 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
8339 | } | |
8340 | ||
272325c4 PZ |
8341 | static DEFINE_MUTEX(mux_interval_mutex); |
8342 | ||
62b85639 SE |
8343 | static ssize_t |
8344 | perf_event_mux_interval_ms_store(struct device *dev, | |
8345 | struct device_attribute *attr, | |
8346 | const char *buf, size_t count) | |
8347 | { | |
8348 | struct pmu *pmu = dev_get_drvdata(dev); | |
8349 | int timer, cpu, ret; | |
8350 | ||
8351 | ret = kstrtoint(buf, 0, &timer); | |
8352 | if (ret) | |
8353 | return ret; | |
8354 | ||
8355 | if (timer < 1) | |
8356 | return -EINVAL; | |
8357 | ||
8358 | /* same value, noting to do */ | |
8359 | if (timer == pmu->hrtimer_interval_ms) | |
8360 | return count; | |
8361 | ||
272325c4 | 8362 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
8363 | pmu->hrtimer_interval_ms = timer; |
8364 | ||
8365 | /* update all cpuctx for this PMU */ | |
272325c4 PZ |
8366 | get_online_cpus(); |
8367 | for_each_online_cpu(cpu) { | |
62b85639 SE |
8368 | struct perf_cpu_context *cpuctx; |
8369 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
8370 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
8371 | ||
272325c4 PZ |
8372 | cpu_function_call(cpu, |
8373 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 8374 | } |
272325c4 PZ |
8375 | put_online_cpus(); |
8376 | mutex_unlock(&mux_interval_mutex); | |
62b85639 SE |
8377 | |
8378 | return count; | |
8379 | } | |
90826ca7 | 8380 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 8381 | |
90826ca7 GKH |
8382 | static struct attribute *pmu_dev_attrs[] = { |
8383 | &dev_attr_type.attr, | |
8384 | &dev_attr_perf_event_mux_interval_ms.attr, | |
8385 | NULL, | |
abe43400 | 8386 | }; |
90826ca7 | 8387 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
8388 | |
8389 | static int pmu_bus_running; | |
8390 | static struct bus_type pmu_bus = { | |
8391 | .name = "event_source", | |
90826ca7 | 8392 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
8393 | }; |
8394 | ||
8395 | static void pmu_dev_release(struct device *dev) | |
8396 | { | |
8397 | kfree(dev); | |
8398 | } | |
8399 | ||
8400 | static int pmu_dev_alloc(struct pmu *pmu) | |
8401 | { | |
8402 | int ret = -ENOMEM; | |
8403 | ||
8404 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
8405 | if (!pmu->dev) | |
8406 | goto out; | |
8407 | ||
0c9d42ed | 8408 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
8409 | device_initialize(pmu->dev); |
8410 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
8411 | if (ret) | |
8412 | goto free_dev; | |
8413 | ||
8414 | dev_set_drvdata(pmu->dev, pmu); | |
8415 | pmu->dev->bus = &pmu_bus; | |
8416 | pmu->dev->release = pmu_dev_release; | |
8417 | ret = device_add(pmu->dev); | |
8418 | if (ret) | |
8419 | goto free_dev; | |
8420 | ||
6e855cd4 AS |
8421 | /* For PMUs with address filters, throw in an extra attribute: */ |
8422 | if (pmu->nr_addr_filters) | |
8423 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
8424 | ||
8425 | if (ret) | |
8426 | goto del_dev; | |
8427 | ||
abe43400 PZ |
8428 | out: |
8429 | return ret; | |
8430 | ||
6e855cd4 AS |
8431 | del_dev: |
8432 | device_del(pmu->dev); | |
8433 | ||
abe43400 PZ |
8434 | free_dev: |
8435 | put_device(pmu->dev); | |
8436 | goto out; | |
8437 | } | |
8438 | ||
547e9fd7 | 8439 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 8440 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 8441 | |
03d8e80b | 8442 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 8443 | { |
108b02cf | 8444 | int cpu, ret; |
24f1e32c | 8445 | |
b0a873eb | 8446 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
8447 | ret = -ENOMEM; |
8448 | pmu->pmu_disable_count = alloc_percpu(int); | |
8449 | if (!pmu->pmu_disable_count) | |
8450 | goto unlock; | |
f29ac756 | 8451 | |
2e80a82a PZ |
8452 | pmu->type = -1; |
8453 | if (!name) | |
8454 | goto skip_type; | |
8455 | pmu->name = name; | |
8456 | ||
8457 | if (type < 0) { | |
0e9c3be2 TH |
8458 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
8459 | if (type < 0) { | |
8460 | ret = type; | |
2e80a82a PZ |
8461 | goto free_pdc; |
8462 | } | |
8463 | } | |
8464 | pmu->type = type; | |
8465 | ||
abe43400 PZ |
8466 | if (pmu_bus_running) { |
8467 | ret = pmu_dev_alloc(pmu); | |
8468 | if (ret) | |
8469 | goto free_idr; | |
8470 | } | |
8471 | ||
2e80a82a | 8472 | skip_type: |
26657848 PZ |
8473 | if (pmu->task_ctx_nr == perf_hw_context) { |
8474 | static int hw_context_taken = 0; | |
8475 | ||
5101ef20 MR |
8476 | /* |
8477 | * Other than systems with heterogeneous CPUs, it never makes | |
8478 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
8479 | * uncore must use perf_invalid_context. | |
8480 | */ | |
8481 | if (WARN_ON_ONCE(hw_context_taken && | |
8482 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
8483 | pmu->task_ctx_nr = perf_invalid_context; |
8484 | ||
8485 | hw_context_taken = 1; | |
8486 | } | |
8487 | ||
8dc85d54 PZ |
8488 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
8489 | if (pmu->pmu_cpu_context) | |
8490 | goto got_cpu_context; | |
f29ac756 | 8491 | |
c4814202 | 8492 | ret = -ENOMEM; |
108b02cf PZ |
8493 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
8494 | if (!pmu->pmu_cpu_context) | |
abe43400 | 8495 | goto free_dev; |
f344011c | 8496 | |
108b02cf PZ |
8497 | for_each_possible_cpu(cpu) { |
8498 | struct perf_cpu_context *cpuctx; | |
8499 | ||
8500 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 8501 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 8502 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 8503 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 8504 | cpuctx->ctx.pmu = pmu; |
9e630205 | 8505 | |
272325c4 | 8506 | __perf_mux_hrtimer_init(cpuctx, cpu); |
9e630205 | 8507 | |
3f1f3320 | 8508 | cpuctx->unique_pmu = pmu; |
108b02cf | 8509 | } |
76e1d904 | 8510 | |
8dc85d54 | 8511 | got_cpu_context: |
ad5133b7 PZ |
8512 | if (!pmu->start_txn) { |
8513 | if (pmu->pmu_enable) { | |
8514 | /* | |
8515 | * If we have pmu_enable/pmu_disable calls, install | |
8516 | * transaction stubs that use that to try and batch | |
8517 | * hardware accesses. | |
8518 | */ | |
8519 | pmu->start_txn = perf_pmu_start_txn; | |
8520 | pmu->commit_txn = perf_pmu_commit_txn; | |
8521 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
8522 | } else { | |
fbbe0701 | 8523 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
8524 | pmu->commit_txn = perf_pmu_nop_int; |
8525 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 8526 | } |
5c92d124 | 8527 | } |
15dbf27c | 8528 | |
ad5133b7 PZ |
8529 | if (!pmu->pmu_enable) { |
8530 | pmu->pmu_enable = perf_pmu_nop_void; | |
8531 | pmu->pmu_disable = perf_pmu_nop_void; | |
8532 | } | |
8533 | ||
35edc2a5 PZ |
8534 | if (!pmu->event_idx) |
8535 | pmu->event_idx = perf_event_idx_default; | |
8536 | ||
b0a873eb | 8537 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 8538 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
8539 | ret = 0; |
8540 | unlock: | |
b0a873eb PZ |
8541 | mutex_unlock(&pmus_lock); |
8542 | ||
33696fc0 | 8543 | return ret; |
108b02cf | 8544 | |
abe43400 PZ |
8545 | free_dev: |
8546 | device_del(pmu->dev); | |
8547 | put_device(pmu->dev); | |
8548 | ||
2e80a82a PZ |
8549 | free_idr: |
8550 | if (pmu->type >= PERF_TYPE_MAX) | |
8551 | idr_remove(&pmu_idr, pmu->type); | |
8552 | ||
108b02cf PZ |
8553 | free_pdc: |
8554 | free_percpu(pmu->pmu_disable_count); | |
8555 | goto unlock; | |
f29ac756 | 8556 | } |
c464c76e | 8557 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 8558 | |
b0a873eb | 8559 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 8560 | { |
b0a873eb PZ |
8561 | mutex_lock(&pmus_lock); |
8562 | list_del_rcu(&pmu->entry); | |
8563 | mutex_unlock(&pmus_lock); | |
5c92d124 | 8564 | |
0475f9ea | 8565 | /* |
cde8e884 PZ |
8566 | * We dereference the pmu list under both SRCU and regular RCU, so |
8567 | * synchronize against both of those. | |
0475f9ea | 8568 | */ |
b0a873eb | 8569 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 8570 | synchronize_rcu(); |
d6d020e9 | 8571 | |
33696fc0 | 8572 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
8573 | if (pmu->type >= PERF_TYPE_MAX) |
8574 | idr_remove(&pmu_idr, pmu->type); | |
6e855cd4 AS |
8575 | if (pmu->nr_addr_filters) |
8576 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
abe43400 PZ |
8577 | device_del(pmu->dev); |
8578 | put_device(pmu->dev); | |
51676957 | 8579 | free_pmu_context(pmu); |
b0a873eb | 8580 | } |
c464c76e | 8581 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 8582 | |
cc34b98b MR |
8583 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
8584 | { | |
ccd41c86 | 8585 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
8586 | int ret; |
8587 | ||
8588 | if (!try_module_get(pmu->module)) | |
8589 | return -ENODEV; | |
ccd41c86 PZ |
8590 | |
8591 | if (event->group_leader != event) { | |
8b10c5e2 PZ |
8592 | /* |
8593 | * This ctx->mutex can nest when we're called through | |
8594 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
8595 | */ | |
8596 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
8597 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
8598 | BUG_ON(!ctx); |
8599 | } | |
8600 | ||
cc34b98b MR |
8601 | event->pmu = pmu; |
8602 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
8603 | |
8604 | if (ctx) | |
8605 | perf_event_ctx_unlock(event->group_leader, ctx); | |
8606 | ||
cc34b98b MR |
8607 | if (ret) |
8608 | module_put(pmu->module); | |
8609 | ||
8610 | return ret; | |
8611 | } | |
8612 | ||
18ab2cd3 | 8613 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb PZ |
8614 | { |
8615 | struct pmu *pmu = NULL; | |
8616 | int idx; | |
940c5b29 | 8617 | int ret; |
b0a873eb PZ |
8618 | |
8619 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a PZ |
8620 | |
8621 | rcu_read_lock(); | |
8622 | pmu = idr_find(&pmu_idr, event->attr.type); | |
8623 | rcu_read_unlock(); | |
940c5b29 | 8624 | if (pmu) { |
cc34b98b | 8625 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
8626 | if (ret) |
8627 | pmu = ERR_PTR(ret); | |
2e80a82a | 8628 | goto unlock; |
940c5b29 | 8629 | } |
2e80a82a | 8630 | |
b0a873eb | 8631 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 8632 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 8633 | if (!ret) |
e5f4d339 | 8634 | goto unlock; |
76e1d904 | 8635 | |
b0a873eb PZ |
8636 | if (ret != -ENOENT) { |
8637 | pmu = ERR_PTR(ret); | |
e5f4d339 | 8638 | goto unlock; |
f344011c | 8639 | } |
5c92d124 | 8640 | } |
e5f4d339 PZ |
8641 | pmu = ERR_PTR(-ENOENT); |
8642 | unlock: | |
b0a873eb | 8643 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 8644 | |
4aeb0b42 | 8645 | return pmu; |
5c92d124 IM |
8646 | } |
8647 | ||
f2fb6bef KL |
8648 | static void attach_sb_event(struct perf_event *event) |
8649 | { | |
8650 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
8651 | ||
8652 | raw_spin_lock(&pel->lock); | |
8653 | list_add_rcu(&event->sb_list, &pel->list); | |
8654 | raw_spin_unlock(&pel->lock); | |
8655 | } | |
8656 | ||
8657 | static void account_pmu_sb_event(struct perf_event *event) | |
8658 | { | |
8659 | struct perf_event_attr *attr = &event->attr; | |
8660 | ||
8661 | if (event->parent) | |
8662 | return; | |
8663 | ||
8664 | if (event->attach_state & PERF_ATTACH_TASK) | |
8665 | return; | |
8666 | ||
8667 | if (attr->mmap || attr->mmap_data || attr->mmap2 || | |
8668 | attr->comm || attr->comm_exec || | |
8669 | attr->task || | |
8670 | attr->context_switch) | |
8671 | attach_sb_event(event); | |
8672 | } | |
8673 | ||
4beb31f3 FW |
8674 | static void account_event_cpu(struct perf_event *event, int cpu) |
8675 | { | |
8676 | if (event->parent) | |
8677 | return; | |
8678 | ||
4beb31f3 FW |
8679 | if (is_cgroup_event(event)) |
8680 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
8681 | } | |
8682 | ||
555e0c1e FW |
8683 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
8684 | static void account_freq_event_nohz(void) | |
8685 | { | |
8686 | #ifdef CONFIG_NO_HZ_FULL | |
8687 | /* Lock so we don't race with concurrent unaccount */ | |
8688 | spin_lock(&nr_freq_lock); | |
8689 | if (atomic_inc_return(&nr_freq_events) == 1) | |
8690 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
8691 | spin_unlock(&nr_freq_lock); | |
8692 | #endif | |
8693 | } | |
8694 | ||
8695 | static void account_freq_event(void) | |
8696 | { | |
8697 | if (tick_nohz_full_enabled()) | |
8698 | account_freq_event_nohz(); | |
8699 | else | |
8700 | atomic_inc(&nr_freq_events); | |
8701 | } | |
8702 | ||
8703 | ||
766d6c07 FW |
8704 | static void account_event(struct perf_event *event) |
8705 | { | |
25432ae9 PZ |
8706 | bool inc = false; |
8707 | ||
4beb31f3 FW |
8708 | if (event->parent) |
8709 | return; | |
8710 | ||
766d6c07 | 8711 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 8712 | inc = true; |
766d6c07 FW |
8713 | if (event->attr.mmap || event->attr.mmap_data) |
8714 | atomic_inc(&nr_mmap_events); | |
8715 | if (event->attr.comm) | |
8716 | atomic_inc(&nr_comm_events); | |
8717 | if (event->attr.task) | |
8718 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
8719 | if (event->attr.freq) |
8720 | account_freq_event(); | |
45ac1403 AH |
8721 | if (event->attr.context_switch) { |
8722 | atomic_inc(&nr_switch_events); | |
25432ae9 | 8723 | inc = true; |
45ac1403 | 8724 | } |
4beb31f3 | 8725 | if (has_branch_stack(event)) |
25432ae9 | 8726 | inc = true; |
4beb31f3 | 8727 | if (is_cgroup_event(event)) |
25432ae9 PZ |
8728 | inc = true; |
8729 | ||
9107c89e PZ |
8730 | if (inc) { |
8731 | if (atomic_inc_not_zero(&perf_sched_count)) | |
8732 | goto enabled; | |
8733 | ||
8734 | mutex_lock(&perf_sched_mutex); | |
8735 | if (!atomic_read(&perf_sched_count)) { | |
8736 | static_branch_enable(&perf_sched_events); | |
8737 | /* | |
8738 | * Guarantee that all CPUs observe they key change and | |
8739 | * call the perf scheduling hooks before proceeding to | |
8740 | * install events that need them. | |
8741 | */ | |
8742 | synchronize_sched(); | |
8743 | } | |
8744 | /* | |
8745 | * Now that we have waited for the sync_sched(), allow further | |
8746 | * increments to by-pass the mutex. | |
8747 | */ | |
8748 | atomic_inc(&perf_sched_count); | |
8749 | mutex_unlock(&perf_sched_mutex); | |
8750 | } | |
8751 | enabled: | |
4beb31f3 FW |
8752 | |
8753 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
8754 | |
8755 | account_pmu_sb_event(event); | |
766d6c07 FW |
8756 | } |
8757 | ||
0793a61d | 8758 | /* |
cdd6c482 | 8759 | * Allocate and initialize a event structure |
0793a61d | 8760 | */ |
cdd6c482 | 8761 | static struct perf_event * |
c3f00c70 | 8762 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
8763 | struct task_struct *task, |
8764 | struct perf_event *group_leader, | |
8765 | struct perf_event *parent_event, | |
4dc0da86 | 8766 | perf_overflow_handler_t overflow_handler, |
79dff51e | 8767 | void *context, int cgroup_fd) |
0793a61d | 8768 | { |
51b0fe39 | 8769 | struct pmu *pmu; |
cdd6c482 IM |
8770 | struct perf_event *event; |
8771 | struct hw_perf_event *hwc; | |
90983b16 | 8772 | long err = -EINVAL; |
0793a61d | 8773 | |
66832eb4 ON |
8774 | if ((unsigned)cpu >= nr_cpu_ids) { |
8775 | if (!task || cpu != -1) | |
8776 | return ERR_PTR(-EINVAL); | |
8777 | } | |
8778 | ||
c3f00c70 | 8779 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 8780 | if (!event) |
d5d2bc0d | 8781 | return ERR_PTR(-ENOMEM); |
0793a61d | 8782 | |
04289bb9 | 8783 | /* |
cdd6c482 | 8784 | * Single events are their own group leaders, with an |
04289bb9 IM |
8785 | * empty sibling list: |
8786 | */ | |
8787 | if (!group_leader) | |
cdd6c482 | 8788 | group_leader = event; |
04289bb9 | 8789 | |
cdd6c482 IM |
8790 | mutex_init(&event->child_mutex); |
8791 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 8792 | |
cdd6c482 IM |
8793 | INIT_LIST_HEAD(&event->group_entry); |
8794 | INIT_LIST_HEAD(&event->event_entry); | |
8795 | INIT_LIST_HEAD(&event->sibling_list); | |
10c6db11 | 8796 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 8797 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 8798 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
8799 | INIT_HLIST_NODE(&event->hlist_entry); |
8800 | ||
10c6db11 | 8801 | |
cdd6c482 | 8802 | init_waitqueue_head(&event->waitq); |
e360adbe | 8803 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 8804 | |
cdd6c482 | 8805 | mutex_init(&event->mmap_mutex); |
375637bc | 8806 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 8807 | |
a6fa941d | 8808 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
8809 | event->cpu = cpu; |
8810 | event->attr = *attr; | |
8811 | event->group_leader = group_leader; | |
8812 | event->pmu = NULL; | |
cdd6c482 | 8813 | event->oncpu = -1; |
a96bbc16 | 8814 | |
cdd6c482 | 8815 | event->parent = parent_event; |
b84fbc9f | 8816 | |
17cf22c3 | 8817 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 8818 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 8819 | |
cdd6c482 | 8820 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 8821 | |
d580ff86 PZ |
8822 | if (task) { |
8823 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 8824 | /* |
50f16a8b PZ |
8825 | * XXX pmu::event_init needs to know what task to account to |
8826 | * and we cannot use the ctx information because we need the | |
8827 | * pmu before we get a ctx. | |
d580ff86 | 8828 | */ |
50f16a8b | 8829 | event->hw.target = task; |
d580ff86 PZ |
8830 | } |
8831 | ||
34f43927 PZ |
8832 | event->clock = &local_clock; |
8833 | if (parent_event) | |
8834 | event->clock = parent_event->clock; | |
8835 | ||
4dc0da86 | 8836 | if (!overflow_handler && parent_event) { |
b326e956 | 8837 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 AK |
8838 | context = parent_event->overflow_handler_context; |
8839 | } | |
66832eb4 | 8840 | |
1879445d WN |
8841 | if (overflow_handler) { |
8842 | event->overflow_handler = overflow_handler; | |
8843 | event->overflow_handler_context = context; | |
9ecda41a WN |
8844 | } else if (is_write_backward(event)){ |
8845 | event->overflow_handler = perf_event_output_backward; | |
8846 | event->overflow_handler_context = NULL; | |
1879445d | 8847 | } else { |
9ecda41a | 8848 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
8849 | event->overflow_handler_context = NULL; |
8850 | } | |
97eaf530 | 8851 | |
0231bb53 | 8852 | perf_event__state_init(event); |
a86ed508 | 8853 | |
4aeb0b42 | 8854 | pmu = NULL; |
b8e83514 | 8855 | |
cdd6c482 | 8856 | hwc = &event->hw; |
bd2b5b12 | 8857 | hwc->sample_period = attr->sample_period; |
0d48696f | 8858 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 8859 | hwc->sample_period = 1; |
eced1dfc | 8860 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 8861 | |
e7850595 | 8862 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 8863 | |
2023b359 | 8864 | /* |
cdd6c482 | 8865 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
2023b359 | 8866 | */ |
3dab77fb | 8867 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
90983b16 | 8868 | goto err_ns; |
a46a2300 YZ |
8869 | |
8870 | if (!has_branch_stack(event)) | |
8871 | event->attr.branch_sample_type = 0; | |
2023b359 | 8872 | |
79dff51e MF |
8873 | if (cgroup_fd != -1) { |
8874 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
8875 | if (err) | |
8876 | goto err_ns; | |
8877 | } | |
8878 | ||
b0a873eb | 8879 | pmu = perf_init_event(event); |
4aeb0b42 | 8880 | if (!pmu) |
90983b16 FW |
8881 | goto err_ns; |
8882 | else if (IS_ERR(pmu)) { | |
4aeb0b42 | 8883 | err = PTR_ERR(pmu); |
90983b16 | 8884 | goto err_ns; |
621a01ea | 8885 | } |
d5d2bc0d | 8886 | |
bed5b25a AS |
8887 | err = exclusive_event_init(event); |
8888 | if (err) | |
8889 | goto err_pmu; | |
8890 | ||
375637bc AS |
8891 | if (has_addr_filter(event)) { |
8892 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, | |
8893 | sizeof(unsigned long), | |
8894 | GFP_KERNEL); | |
8895 | if (!event->addr_filters_offs) | |
8896 | goto err_per_task; | |
8897 | ||
8898 | /* force hw sync on the address filters */ | |
8899 | event->addr_filters_gen = 1; | |
8900 | } | |
8901 | ||
cdd6c482 | 8902 | if (!event->parent) { |
927c7a9e | 8903 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 8904 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 8905 | if (err) |
375637bc | 8906 | goto err_addr_filters; |
d010b332 | 8907 | } |
f344011c | 8908 | } |
9ee318a7 | 8909 | |
927a5570 AS |
8910 | /* symmetric to unaccount_event() in _free_event() */ |
8911 | account_event(event); | |
8912 | ||
cdd6c482 | 8913 | return event; |
90983b16 | 8914 | |
375637bc AS |
8915 | err_addr_filters: |
8916 | kfree(event->addr_filters_offs); | |
8917 | ||
bed5b25a AS |
8918 | err_per_task: |
8919 | exclusive_event_destroy(event); | |
8920 | ||
90983b16 FW |
8921 | err_pmu: |
8922 | if (event->destroy) | |
8923 | event->destroy(event); | |
c464c76e | 8924 | module_put(pmu->module); |
90983b16 | 8925 | err_ns: |
79dff51e MF |
8926 | if (is_cgroup_event(event)) |
8927 | perf_detach_cgroup(event); | |
90983b16 FW |
8928 | if (event->ns) |
8929 | put_pid_ns(event->ns); | |
8930 | kfree(event); | |
8931 | ||
8932 | return ERR_PTR(err); | |
0793a61d TG |
8933 | } |
8934 | ||
cdd6c482 IM |
8935 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
8936 | struct perf_event_attr *attr) | |
974802ea | 8937 | { |
974802ea | 8938 | u32 size; |
cdf8073d | 8939 | int ret; |
974802ea PZ |
8940 | |
8941 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
8942 | return -EFAULT; | |
8943 | ||
8944 | /* | |
8945 | * zero the full structure, so that a short copy will be nice. | |
8946 | */ | |
8947 | memset(attr, 0, sizeof(*attr)); | |
8948 | ||
8949 | ret = get_user(size, &uattr->size); | |
8950 | if (ret) | |
8951 | return ret; | |
8952 | ||
8953 | if (size > PAGE_SIZE) /* silly large */ | |
8954 | goto err_size; | |
8955 | ||
8956 | if (!size) /* abi compat */ | |
8957 | size = PERF_ATTR_SIZE_VER0; | |
8958 | ||
8959 | if (size < PERF_ATTR_SIZE_VER0) | |
8960 | goto err_size; | |
8961 | ||
8962 | /* | |
8963 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
8964 | * ensure all the unknown bits are 0 - i.e. new |
8965 | * user-space does not rely on any kernel feature | |
8966 | * extensions we dont know about yet. | |
974802ea PZ |
8967 | */ |
8968 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
8969 | unsigned char __user *addr; |
8970 | unsigned char __user *end; | |
8971 | unsigned char val; | |
974802ea | 8972 | |
cdf8073d IS |
8973 | addr = (void __user *)uattr + sizeof(*attr); |
8974 | end = (void __user *)uattr + size; | |
974802ea | 8975 | |
cdf8073d | 8976 | for (; addr < end; addr++) { |
974802ea PZ |
8977 | ret = get_user(val, addr); |
8978 | if (ret) | |
8979 | return ret; | |
8980 | if (val) | |
8981 | goto err_size; | |
8982 | } | |
b3e62e35 | 8983 | size = sizeof(*attr); |
974802ea PZ |
8984 | } |
8985 | ||
8986 | ret = copy_from_user(attr, uattr, size); | |
8987 | if (ret) | |
8988 | return -EFAULT; | |
8989 | ||
cd757645 | 8990 | if (attr->__reserved_1) |
974802ea PZ |
8991 | return -EINVAL; |
8992 | ||
8993 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
8994 | return -EINVAL; | |
8995 | ||
8996 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
8997 | return -EINVAL; | |
8998 | ||
bce38cd5 SE |
8999 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
9000 | u64 mask = attr->branch_sample_type; | |
9001 | ||
9002 | /* only using defined bits */ | |
9003 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
9004 | return -EINVAL; | |
9005 | ||
9006 | /* at least one branch bit must be set */ | |
9007 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
9008 | return -EINVAL; | |
9009 | ||
bce38cd5 SE |
9010 | /* propagate priv level, when not set for branch */ |
9011 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
9012 | ||
9013 | /* exclude_kernel checked on syscall entry */ | |
9014 | if (!attr->exclude_kernel) | |
9015 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
9016 | ||
9017 | if (!attr->exclude_user) | |
9018 | mask |= PERF_SAMPLE_BRANCH_USER; | |
9019 | ||
9020 | if (!attr->exclude_hv) | |
9021 | mask |= PERF_SAMPLE_BRANCH_HV; | |
9022 | /* | |
9023 | * adjust user setting (for HW filter setup) | |
9024 | */ | |
9025 | attr->branch_sample_type = mask; | |
9026 | } | |
e712209a SE |
9027 | /* privileged levels capture (kernel, hv): check permissions */ |
9028 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
9029 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
9030 | return -EACCES; | |
bce38cd5 | 9031 | } |
4018994f | 9032 | |
c5ebcedb | 9033 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 9034 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
9035 | if (ret) |
9036 | return ret; | |
9037 | } | |
9038 | ||
9039 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
9040 | if (!arch_perf_have_user_stack_dump()) | |
9041 | return -ENOSYS; | |
9042 | ||
9043 | /* | |
9044 | * We have __u32 type for the size, but so far | |
9045 | * we can only use __u16 as maximum due to the | |
9046 | * __u16 sample size limit. | |
9047 | */ | |
9048 | if (attr->sample_stack_user >= USHRT_MAX) | |
9049 | ret = -EINVAL; | |
9050 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) | |
9051 | ret = -EINVAL; | |
9052 | } | |
4018994f | 9053 | |
60e2364e SE |
9054 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
9055 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
9056 | out: |
9057 | return ret; | |
9058 | ||
9059 | err_size: | |
9060 | put_user(sizeof(*attr), &uattr->size); | |
9061 | ret = -E2BIG; | |
9062 | goto out; | |
9063 | } | |
9064 | ||
ac9721f3 PZ |
9065 | static int |
9066 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 9067 | { |
b69cf536 | 9068 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
9069 | int ret = -EINVAL; |
9070 | ||
ac9721f3 | 9071 | if (!output_event) |
a4be7c27 PZ |
9072 | goto set; |
9073 | ||
ac9721f3 PZ |
9074 | /* don't allow circular references */ |
9075 | if (event == output_event) | |
a4be7c27 PZ |
9076 | goto out; |
9077 | ||
0f139300 PZ |
9078 | /* |
9079 | * Don't allow cross-cpu buffers | |
9080 | */ | |
9081 | if (output_event->cpu != event->cpu) | |
9082 | goto out; | |
9083 | ||
9084 | /* | |
76369139 | 9085 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
9086 | */ |
9087 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
9088 | goto out; | |
9089 | ||
34f43927 PZ |
9090 | /* |
9091 | * Mixing clocks in the same buffer is trouble you don't need. | |
9092 | */ | |
9093 | if (output_event->clock != event->clock) | |
9094 | goto out; | |
9095 | ||
9ecda41a WN |
9096 | /* |
9097 | * Either writing ring buffer from beginning or from end. | |
9098 | * Mixing is not allowed. | |
9099 | */ | |
9100 | if (is_write_backward(output_event) != is_write_backward(event)) | |
9101 | goto out; | |
9102 | ||
45bfb2e5 PZ |
9103 | /* |
9104 | * If both events generate aux data, they must be on the same PMU | |
9105 | */ | |
9106 | if (has_aux(event) && has_aux(output_event) && | |
9107 | event->pmu != output_event->pmu) | |
9108 | goto out; | |
9109 | ||
a4be7c27 | 9110 | set: |
cdd6c482 | 9111 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
9112 | /* Can't redirect output if we've got an active mmap() */ |
9113 | if (atomic_read(&event->mmap_count)) | |
9114 | goto unlock; | |
a4be7c27 | 9115 | |
ac9721f3 | 9116 | if (output_event) { |
76369139 FW |
9117 | /* get the rb we want to redirect to */ |
9118 | rb = ring_buffer_get(output_event); | |
9119 | if (!rb) | |
ac9721f3 | 9120 | goto unlock; |
a4be7c27 PZ |
9121 | } |
9122 | ||
b69cf536 | 9123 | ring_buffer_attach(event, rb); |
9bb5d40c | 9124 | |
a4be7c27 | 9125 | ret = 0; |
ac9721f3 PZ |
9126 | unlock: |
9127 | mutex_unlock(&event->mmap_mutex); | |
9128 | ||
a4be7c27 | 9129 | out: |
a4be7c27 PZ |
9130 | return ret; |
9131 | } | |
9132 | ||
f63a8daa PZ |
9133 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
9134 | { | |
9135 | if (b < a) | |
9136 | swap(a, b); | |
9137 | ||
9138 | mutex_lock(a); | |
9139 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
9140 | } | |
9141 | ||
34f43927 PZ |
9142 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
9143 | { | |
9144 | bool nmi_safe = false; | |
9145 | ||
9146 | switch (clk_id) { | |
9147 | case CLOCK_MONOTONIC: | |
9148 | event->clock = &ktime_get_mono_fast_ns; | |
9149 | nmi_safe = true; | |
9150 | break; | |
9151 | ||
9152 | case CLOCK_MONOTONIC_RAW: | |
9153 | event->clock = &ktime_get_raw_fast_ns; | |
9154 | nmi_safe = true; | |
9155 | break; | |
9156 | ||
9157 | case CLOCK_REALTIME: | |
9158 | event->clock = &ktime_get_real_ns; | |
9159 | break; | |
9160 | ||
9161 | case CLOCK_BOOTTIME: | |
9162 | event->clock = &ktime_get_boot_ns; | |
9163 | break; | |
9164 | ||
9165 | case CLOCK_TAI: | |
9166 | event->clock = &ktime_get_tai_ns; | |
9167 | break; | |
9168 | ||
9169 | default: | |
9170 | return -EINVAL; | |
9171 | } | |
9172 | ||
9173 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
9174 | return -EINVAL; | |
9175 | ||
9176 | return 0; | |
9177 | } | |
9178 | ||
0793a61d | 9179 | /** |
cdd6c482 | 9180 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 9181 | * |
cdd6c482 | 9182 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 9183 | * @pid: target pid |
9f66a381 | 9184 | * @cpu: target cpu |
cdd6c482 | 9185 | * @group_fd: group leader event fd |
0793a61d | 9186 | */ |
cdd6c482 IM |
9187 | SYSCALL_DEFINE5(perf_event_open, |
9188 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 9189 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 9190 | { |
b04243ef PZ |
9191 | struct perf_event *group_leader = NULL, *output_event = NULL; |
9192 | struct perf_event *event, *sibling; | |
cdd6c482 | 9193 | struct perf_event_attr attr; |
f63a8daa | 9194 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 9195 | struct file *event_file = NULL; |
2903ff01 | 9196 | struct fd group = {NULL, 0}; |
38a81da2 | 9197 | struct task_struct *task = NULL; |
89a1e187 | 9198 | struct pmu *pmu; |
ea635c64 | 9199 | int event_fd; |
b04243ef | 9200 | int move_group = 0; |
dc86cabe | 9201 | int err; |
a21b0b35 | 9202 | int f_flags = O_RDWR; |
79dff51e | 9203 | int cgroup_fd = -1; |
0793a61d | 9204 | |
2743a5b0 | 9205 | /* for future expandability... */ |
e5d1367f | 9206 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
9207 | return -EINVAL; |
9208 | ||
dc86cabe IM |
9209 | err = perf_copy_attr(attr_uptr, &attr); |
9210 | if (err) | |
9211 | return err; | |
eab656ae | 9212 | |
0764771d PZ |
9213 | if (!attr.exclude_kernel) { |
9214 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
9215 | return -EACCES; | |
9216 | } | |
9217 | ||
df58ab24 | 9218 | if (attr.freq) { |
cdd6c482 | 9219 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 9220 | return -EINVAL; |
0819b2e3 PZ |
9221 | } else { |
9222 | if (attr.sample_period & (1ULL << 63)) | |
9223 | return -EINVAL; | |
df58ab24 PZ |
9224 | } |
9225 | ||
97c79a38 ACM |
9226 | if (!attr.sample_max_stack) |
9227 | attr.sample_max_stack = sysctl_perf_event_max_stack; | |
9228 | ||
e5d1367f SE |
9229 | /* |
9230 | * In cgroup mode, the pid argument is used to pass the fd | |
9231 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
9232 | * designates the cpu on which to monitor threads from that | |
9233 | * cgroup. | |
9234 | */ | |
9235 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
9236 | return -EINVAL; | |
9237 | ||
a21b0b35 YD |
9238 | if (flags & PERF_FLAG_FD_CLOEXEC) |
9239 | f_flags |= O_CLOEXEC; | |
9240 | ||
9241 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
9242 | if (event_fd < 0) |
9243 | return event_fd; | |
9244 | ||
ac9721f3 | 9245 | if (group_fd != -1) { |
2903ff01 AV |
9246 | err = perf_fget_light(group_fd, &group); |
9247 | if (err) | |
d14b12d7 | 9248 | goto err_fd; |
2903ff01 | 9249 | group_leader = group.file->private_data; |
ac9721f3 PZ |
9250 | if (flags & PERF_FLAG_FD_OUTPUT) |
9251 | output_event = group_leader; | |
9252 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
9253 | group_leader = NULL; | |
9254 | } | |
9255 | ||
e5d1367f | 9256 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
9257 | task = find_lively_task_by_vpid(pid); |
9258 | if (IS_ERR(task)) { | |
9259 | err = PTR_ERR(task); | |
9260 | goto err_group_fd; | |
9261 | } | |
9262 | } | |
9263 | ||
1f4ee503 PZ |
9264 | if (task && group_leader && |
9265 | group_leader->attr.inherit != attr.inherit) { | |
9266 | err = -EINVAL; | |
9267 | goto err_task; | |
9268 | } | |
9269 | ||
fbfc623f YZ |
9270 | get_online_cpus(); |
9271 | ||
79c9ce57 PZ |
9272 | if (task) { |
9273 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); | |
9274 | if (err) | |
9275 | goto err_cpus; | |
9276 | ||
9277 | /* | |
9278 | * Reuse ptrace permission checks for now. | |
9279 | * | |
9280 | * We must hold cred_guard_mutex across this and any potential | |
9281 | * perf_install_in_context() call for this new event to | |
9282 | * serialize against exec() altering our credentials (and the | |
9283 | * perf_event_exit_task() that could imply). | |
9284 | */ | |
9285 | err = -EACCES; | |
9286 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
9287 | goto err_cred; | |
9288 | } | |
9289 | ||
79dff51e MF |
9290 | if (flags & PERF_FLAG_PID_CGROUP) |
9291 | cgroup_fd = pid; | |
9292 | ||
4dc0da86 | 9293 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 9294 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
9295 | if (IS_ERR(event)) { |
9296 | err = PTR_ERR(event); | |
79c9ce57 | 9297 | goto err_cred; |
d14b12d7 SE |
9298 | } |
9299 | ||
53b25335 VW |
9300 | if (is_sampling_event(event)) { |
9301 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
9302 | err = -ENOTSUPP; | |
9303 | goto err_alloc; | |
9304 | } | |
9305 | } | |
9306 | ||
89a1e187 PZ |
9307 | /* |
9308 | * Special case software events and allow them to be part of | |
9309 | * any hardware group. | |
9310 | */ | |
9311 | pmu = event->pmu; | |
b04243ef | 9312 | |
34f43927 PZ |
9313 | if (attr.use_clockid) { |
9314 | err = perf_event_set_clock(event, attr.clockid); | |
9315 | if (err) | |
9316 | goto err_alloc; | |
9317 | } | |
9318 | ||
b04243ef PZ |
9319 | if (group_leader && |
9320 | (is_software_event(event) != is_software_event(group_leader))) { | |
9321 | if (is_software_event(event)) { | |
9322 | /* | |
9323 | * If event and group_leader are not both a software | |
9324 | * event, and event is, then group leader is not. | |
9325 | * | |
9326 | * Allow the addition of software events to !software | |
9327 | * groups, this is safe because software events never | |
9328 | * fail to schedule. | |
9329 | */ | |
9330 | pmu = group_leader->pmu; | |
9331 | } else if (is_software_event(group_leader) && | |
9332 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { | |
9333 | /* | |
9334 | * In case the group is a pure software group, and we | |
9335 | * try to add a hardware event, move the whole group to | |
9336 | * the hardware context. | |
9337 | */ | |
9338 | move_group = 1; | |
9339 | } | |
9340 | } | |
89a1e187 PZ |
9341 | |
9342 | /* | |
9343 | * Get the target context (task or percpu): | |
9344 | */ | |
4af57ef2 | 9345 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
9346 | if (IS_ERR(ctx)) { |
9347 | err = PTR_ERR(ctx); | |
c6be5a5c | 9348 | goto err_alloc; |
89a1e187 PZ |
9349 | } |
9350 | ||
bed5b25a AS |
9351 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
9352 | err = -EBUSY; | |
9353 | goto err_context; | |
9354 | } | |
9355 | ||
ccff286d | 9356 | /* |
cdd6c482 | 9357 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 9358 | */ |
ac9721f3 | 9359 | if (group_leader) { |
dc86cabe | 9360 | err = -EINVAL; |
04289bb9 | 9361 | |
04289bb9 | 9362 | /* |
ccff286d IM |
9363 | * Do not allow a recursive hierarchy (this new sibling |
9364 | * becoming part of another group-sibling): | |
9365 | */ | |
9366 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 9367 | goto err_context; |
34f43927 PZ |
9368 | |
9369 | /* All events in a group should have the same clock */ | |
9370 | if (group_leader->clock != event->clock) | |
9371 | goto err_context; | |
9372 | ||
ccff286d IM |
9373 | /* |
9374 | * Do not allow to attach to a group in a different | |
9375 | * task or CPU context: | |
04289bb9 | 9376 | */ |
b04243ef | 9377 | if (move_group) { |
c3c87e77 PZ |
9378 | /* |
9379 | * Make sure we're both on the same task, or both | |
9380 | * per-cpu events. | |
9381 | */ | |
9382 | if (group_leader->ctx->task != ctx->task) | |
9383 | goto err_context; | |
9384 | ||
9385 | /* | |
9386 | * Make sure we're both events for the same CPU; | |
9387 | * grouping events for different CPUs is broken; since | |
9388 | * you can never concurrently schedule them anyhow. | |
9389 | */ | |
9390 | if (group_leader->cpu != event->cpu) | |
b04243ef PZ |
9391 | goto err_context; |
9392 | } else { | |
9393 | if (group_leader->ctx != ctx) | |
9394 | goto err_context; | |
9395 | } | |
9396 | ||
3b6f9e5c PM |
9397 | /* |
9398 | * Only a group leader can be exclusive or pinned | |
9399 | */ | |
0d48696f | 9400 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 9401 | goto err_context; |
ac9721f3 PZ |
9402 | } |
9403 | ||
9404 | if (output_event) { | |
9405 | err = perf_event_set_output(event, output_event); | |
9406 | if (err) | |
c3f00c70 | 9407 | goto err_context; |
ac9721f3 | 9408 | } |
0793a61d | 9409 | |
a21b0b35 YD |
9410 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
9411 | f_flags); | |
ea635c64 AV |
9412 | if (IS_ERR(event_file)) { |
9413 | err = PTR_ERR(event_file); | |
201c2f85 | 9414 | event_file = NULL; |
c3f00c70 | 9415 | goto err_context; |
ea635c64 | 9416 | } |
9b51f66d | 9417 | |
b04243ef | 9418 | if (move_group) { |
f63a8daa | 9419 | gctx = group_leader->ctx; |
f55fc2a5 | 9420 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
84c4e620 PZ |
9421 | if (gctx->task == TASK_TOMBSTONE) { |
9422 | err = -ESRCH; | |
9423 | goto err_locked; | |
9424 | } | |
f55fc2a5 PZ |
9425 | } else { |
9426 | mutex_lock(&ctx->mutex); | |
9427 | } | |
9428 | ||
84c4e620 PZ |
9429 | if (ctx->task == TASK_TOMBSTONE) { |
9430 | err = -ESRCH; | |
9431 | goto err_locked; | |
9432 | } | |
9433 | ||
a723968c PZ |
9434 | if (!perf_event_validate_size(event)) { |
9435 | err = -E2BIG; | |
9436 | goto err_locked; | |
9437 | } | |
9438 | ||
f55fc2a5 PZ |
9439 | /* |
9440 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
9441 | * because we need to serialize with concurrent event creation. | |
9442 | */ | |
9443 | if (!exclusive_event_installable(event, ctx)) { | |
9444 | /* exclusive and group stuff are assumed mutually exclusive */ | |
9445 | WARN_ON_ONCE(move_group); | |
f63a8daa | 9446 | |
f55fc2a5 PZ |
9447 | err = -EBUSY; |
9448 | goto err_locked; | |
9449 | } | |
f63a8daa | 9450 | |
f55fc2a5 PZ |
9451 | WARN_ON_ONCE(ctx->parent_ctx); |
9452 | ||
79c9ce57 PZ |
9453 | /* |
9454 | * This is the point on no return; we cannot fail hereafter. This is | |
9455 | * where we start modifying current state. | |
9456 | */ | |
9457 | ||
f55fc2a5 | 9458 | if (move_group) { |
f63a8daa PZ |
9459 | /* |
9460 | * See perf_event_ctx_lock() for comments on the details | |
9461 | * of swizzling perf_event::ctx. | |
9462 | */ | |
45a0e07a | 9463 | perf_remove_from_context(group_leader, 0); |
0231bb53 | 9464 | |
b04243ef PZ |
9465 | list_for_each_entry(sibling, &group_leader->sibling_list, |
9466 | group_entry) { | |
45a0e07a | 9467 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
9468 | put_ctx(gctx); |
9469 | } | |
b04243ef | 9470 | |
f63a8daa PZ |
9471 | /* |
9472 | * Wait for everybody to stop referencing the events through | |
9473 | * the old lists, before installing it on new lists. | |
9474 | */ | |
0cda4c02 | 9475 | synchronize_rcu(); |
f63a8daa | 9476 | |
8f95b435 PZI |
9477 | /* |
9478 | * Install the group siblings before the group leader. | |
9479 | * | |
9480 | * Because a group leader will try and install the entire group | |
9481 | * (through the sibling list, which is still in-tact), we can | |
9482 | * end up with siblings installed in the wrong context. | |
9483 | * | |
9484 | * By installing siblings first we NO-OP because they're not | |
9485 | * reachable through the group lists. | |
9486 | */ | |
b04243ef PZ |
9487 | list_for_each_entry(sibling, &group_leader->sibling_list, |
9488 | group_entry) { | |
8f95b435 | 9489 | perf_event__state_init(sibling); |
9fc81d87 | 9490 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
9491 | get_ctx(ctx); |
9492 | } | |
8f95b435 PZI |
9493 | |
9494 | /* | |
9495 | * Removing from the context ends up with disabled | |
9496 | * event. What we want here is event in the initial | |
9497 | * startup state, ready to be add into new context. | |
9498 | */ | |
9499 | perf_event__state_init(group_leader); | |
9500 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
9501 | get_ctx(ctx); | |
b04243ef | 9502 | |
f55fc2a5 PZ |
9503 | /* |
9504 | * Now that all events are installed in @ctx, nothing | |
9505 | * references @gctx anymore, so drop the last reference we have | |
9506 | * on it. | |
9507 | */ | |
9508 | put_ctx(gctx); | |
bed5b25a AS |
9509 | } |
9510 | ||
f73e22ab PZ |
9511 | /* |
9512 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
9513 | * that we're serialized against further additions and before | |
9514 | * perf_install_in_context() which is the point the event is active and | |
9515 | * can use these values. | |
9516 | */ | |
9517 | perf_event__header_size(event); | |
9518 | perf_event__id_header_size(event); | |
9519 | ||
78cd2c74 PZ |
9520 | event->owner = current; |
9521 | ||
e2d37cd2 | 9522 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 9523 | perf_unpin_context(ctx); |
f63a8daa | 9524 | |
f55fc2a5 | 9525 | if (move_group) |
f63a8daa | 9526 | mutex_unlock(&gctx->mutex); |
d859e29f | 9527 | mutex_unlock(&ctx->mutex); |
9b51f66d | 9528 | |
79c9ce57 PZ |
9529 | if (task) { |
9530 | mutex_unlock(&task->signal->cred_guard_mutex); | |
9531 | put_task_struct(task); | |
9532 | } | |
9533 | ||
fbfc623f YZ |
9534 | put_online_cpus(); |
9535 | ||
cdd6c482 IM |
9536 | mutex_lock(¤t->perf_event_mutex); |
9537 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
9538 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 9539 | |
8a49542c PZ |
9540 | /* |
9541 | * Drop the reference on the group_event after placing the | |
9542 | * new event on the sibling_list. This ensures destruction | |
9543 | * of the group leader will find the pointer to itself in | |
9544 | * perf_group_detach(). | |
9545 | */ | |
2903ff01 | 9546 | fdput(group); |
ea635c64 AV |
9547 | fd_install(event_fd, event_file); |
9548 | return event_fd; | |
0793a61d | 9549 | |
f55fc2a5 PZ |
9550 | err_locked: |
9551 | if (move_group) | |
9552 | mutex_unlock(&gctx->mutex); | |
9553 | mutex_unlock(&ctx->mutex); | |
9554 | /* err_file: */ | |
9555 | fput(event_file); | |
c3f00c70 | 9556 | err_context: |
fe4b04fa | 9557 | perf_unpin_context(ctx); |
ea635c64 | 9558 | put_ctx(ctx); |
c6be5a5c | 9559 | err_alloc: |
13005627 PZ |
9560 | /* |
9561 | * If event_file is set, the fput() above will have called ->release() | |
9562 | * and that will take care of freeing the event. | |
9563 | */ | |
9564 | if (!event_file) | |
9565 | free_event(event); | |
79c9ce57 PZ |
9566 | err_cred: |
9567 | if (task) | |
9568 | mutex_unlock(&task->signal->cred_guard_mutex); | |
1f4ee503 | 9569 | err_cpus: |
fbfc623f | 9570 | put_online_cpus(); |
1f4ee503 | 9571 | err_task: |
e7d0bc04 PZ |
9572 | if (task) |
9573 | put_task_struct(task); | |
89a1e187 | 9574 | err_group_fd: |
2903ff01 | 9575 | fdput(group); |
ea635c64 AV |
9576 | err_fd: |
9577 | put_unused_fd(event_fd); | |
dc86cabe | 9578 | return err; |
0793a61d TG |
9579 | } |
9580 | ||
fb0459d7 AV |
9581 | /** |
9582 | * perf_event_create_kernel_counter | |
9583 | * | |
9584 | * @attr: attributes of the counter to create | |
9585 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 9586 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
9587 | */ |
9588 | struct perf_event * | |
9589 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 9590 | struct task_struct *task, |
4dc0da86 AK |
9591 | perf_overflow_handler_t overflow_handler, |
9592 | void *context) | |
fb0459d7 | 9593 | { |
fb0459d7 | 9594 | struct perf_event_context *ctx; |
c3f00c70 | 9595 | struct perf_event *event; |
fb0459d7 | 9596 | int err; |
d859e29f | 9597 | |
fb0459d7 AV |
9598 | /* |
9599 | * Get the target context (task or percpu): | |
9600 | */ | |
d859e29f | 9601 | |
4dc0da86 | 9602 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 9603 | overflow_handler, context, -1); |
c3f00c70 PZ |
9604 | if (IS_ERR(event)) { |
9605 | err = PTR_ERR(event); | |
9606 | goto err; | |
9607 | } | |
d859e29f | 9608 | |
f8697762 | 9609 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 9610 | event->owner = TASK_TOMBSTONE; |
f8697762 | 9611 | |
4af57ef2 | 9612 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
9613 | if (IS_ERR(ctx)) { |
9614 | err = PTR_ERR(ctx); | |
c3f00c70 | 9615 | goto err_free; |
d859e29f | 9616 | } |
fb0459d7 | 9617 | |
fb0459d7 AV |
9618 | WARN_ON_ONCE(ctx->parent_ctx); |
9619 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
9620 | if (ctx->task == TASK_TOMBSTONE) { |
9621 | err = -ESRCH; | |
9622 | goto err_unlock; | |
9623 | } | |
9624 | ||
bed5b25a | 9625 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 9626 | err = -EBUSY; |
84c4e620 | 9627 | goto err_unlock; |
bed5b25a AS |
9628 | } |
9629 | ||
fb0459d7 | 9630 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 9631 | perf_unpin_context(ctx); |
fb0459d7 AV |
9632 | mutex_unlock(&ctx->mutex); |
9633 | ||
fb0459d7 AV |
9634 | return event; |
9635 | ||
84c4e620 PZ |
9636 | err_unlock: |
9637 | mutex_unlock(&ctx->mutex); | |
9638 | perf_unpin_context(ctx); | |
9639 | put_ctx(ctx); | |
c3f00c70 PZ |
9640 | err_free: |
9641 | free_event(event); | |
9642 | err: | |
c6567f64 | 9643 | return ERR_PTR(err); |
9b51f66d | 9644 | } |
fb0459d7 | 9645 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 9646 | |
0cda4c02 YZ |
9647 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
9648 | { | |
9649 | struct perf_event_context *src_ctx; | |
9650 | struct perf_event_context *dst_ctx; | |
9651 | struct perf_event *event, *tmp; | |
9652 | LIST_HEAD(events); | |
9653 | ||
9654 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
9655 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
9656 | ||
f63a8daa PZ |
9657 | /* |
9658 | * See perf_event_ctx_lock() for comments on the details | |
9659 | * of swizzling perf_event::ctx. | |
9660 | */ | |
9661 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
9662 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
9663 | event_entry) { | |
45a0e07a | 9664 | perf_remove_from_context(event, 0); |
9a545de0 | 9665 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 9666 | put_ctx(src_ctx); |
9886167d | 9667 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 9668 | } |
0cda4c02 | 9669 | |
8f95b435 PZI |
9670 | /* |
9671 | * Wait for the events to quiesce before re-instating them. | |
9672 | */ | |
0cda4c02 YZ |
9673 | synchronize_rcu(); |
9674 | ||
8f95b435 PZI |
9675 | /* |
9676 | * Re-instate events in 2 passes. | |
9677 | * | |
9678 | * Skip over group leaders and only install siblings on this first | |
9679 | * pass, siblings will not get enabled without a leader, however a | |
9680 | * leader will enable its siblings, even if those are still on the old | |
9681 | * context. | |
9682 | */ | |
9683 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
9684 | if (event->group_leader == event) | |
9685 | continue; | |
9686 | ||
9687 | list_del(&event->migrate_entry); | |
9688 | if (event->state >= PERF_EVENT_STATE_OFF) | |
9689 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9690 | account_event_cpu(event, dst_cpu); | |
9691 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
9692 | get_ctx(dst_ctx); | |
9693 | } | |
9694 | ||
9695 | /* | |
9696 | * Once all the siblings are setup properly, install the group leaders | |
9697 | * to make it go. | |
9698 | */ | |
9886167d PZ |
9699 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
9700 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
9701 | if (event->state >= PERF_EVENT_STATE_OFF) |
9702 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 9703 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
9704 | perf_install_in_context(dst_ctx, event, dst_cpu); |
9705 | get_ctx(dst_ctx); | |
9706 | } | |
9707 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 9708 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
9709 | } |
9710 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
9711 | ||
cdd6c482 | 9712 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 9713 | struct task_struct *child) |
d859e29f | 9714 | { |
cdd6c482 | 9715 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 9716 | u64 child_val; |
d859e29f | 9717 | |
cdd6c482 IM |
9718 | if (child_event->attr.inherit_stat) |
9719 | perf_event_read_event(child_event, child); | |
38b200d6 | 9720 | |
b5e58793 | 9721 | child_val = perf_event_count(child_event); |
d859e29f PM |
9722 | |
9723 | /* | |
9724 | * Add back the child's count to the parent's count: | |
9725 | */ | |
a6e6dea6 | 9726 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
9727 | atomic64_add(child_event->total_time_enabled, |
9728 | &parent_event->child_total_time_enabled); | |
9729 | atomic64_add(child_event->total_time_running, | |
9730 | &parent_event->child_total_time_running); | |
d859e29f PM |
9731 | } |
9732 | ||
9b51f66d | 9733 | static void |
8ba289b8 PZ |
9734 | perf_event_exit_event(struct perf_event *child_event, |
9735 | struct perf_event_context *child_ctx, | |
9736 | struct task_struct *child) | |
9b51f66d | 9737 | { |
8ba289b8 PZ |
9738 | struct perf_event *parent_event = child_event->parent; |
9739 | ||
1903d50c PZ |
9740 | /* |
9741 | * Do not destroy the 'original' grouping; because of the context | |
9742 | * switch optimization the original events could've ended up in a | |
9743 | * random child task. | |
9744 | * | |
9745 | * If we were to destroy the original group, all group related | |
9746 | * operations would cease to function properly after this random | |
9747 | * child dies. | |
9748 | * | |
9749 | * Do destroy all inherited groups, we don't care about those | |
9750 | * and being thorough is better. | |
9751 | */ | |
32132a3d PZ |
9752 | raw_spin_lock_irq(&child_ctx->lock); |
9753 | WARN_ON_ONCE(child_ctx->is_active); | |
9754 | ||
8ba289b8 | 9755 | if (parent_event) |
32132a3d PZ |
9756 | perf_group_detach(child_event); |
9757 | list_del_event(child_event, child_ctx); | |
a69b0ca4 | 9758 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
32132a3d | 9759 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 9760 | |
9b51f66d | 9761 | /* |
8ba289b8 | 9762 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 9763 | */ |
8ba289b8 | 9764 | if (!parent_event) { |
179033b3 | 9765 | perf_event_wakeup(child_event); |
8ba289b8 | 9766 | return; |
4bcf349a | 9767 | } |
8ba289b8 PZ |
9768 | /* |
9769 | * Child events can be cleaned up. | |
9770 | */ | |
9771 | ||
9772 | sync_child_event(child_event, child); | |
9773 | ||
9774 | /* | |
9775 | * Remove this event from the parent's list | |
9776 | */ | |
9777 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
9778 | mutex_lock(&parent_event->child_mutex); | |
9779 | list_del_init(&child_event->child_list); | |
9780 | mutex_unlock(&parent_event->child_mutex); | |
9781 | ||
9782 | /* | |
9783 | * Kick perf_poll() for is_event_hup(). | |
9784 | */ | |
9785 | perf_event_wakeup(parent_event); | |
9786 | free_event(child_event); | |
9787 | put_event(parent_event); | |
9b51f66d IM |
9788 | } |
9789 | ||
8dc85d54 | 9790 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 9791 | { |
211de6eb | 9792 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 9793 | struct perf_event *child_event, *next; |
63b6da39 PZ |
9794 | |
9795 | WARN_ON_ONCE(child != current); | |
9b51f66d | 9796 | |
6a3351b6 | 9797 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 9798 | if (!child_ctx) |
9b51f66d IM |
9799 | return; |
9800 | ||
ad3a37de | 9801 | /* |
6a3351b6 PZ |
9802 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
9803 | * ctx::mutex over the entire thing. This serializes against almost | |
9804 | * everything that wants to access the ctx. | |
9805 | * | |
9806 | * The exception is sys_perf_event_open() / | |
9807 | * perf_event_create_kernel_count() which does find_get_context() | |
9808 | * without ctx::mutex (it cannot because of the move_group double mutex | |
9809 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 9810 | */ |
6a3351b6 | 9811 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
9812 | |
9813 | /* | |
6a3351b6 PZ |
9814 | * In a single ctx::lock section, de-schedule the events and detach the |
9815 | * context from the task such that we cannot ever get it scheduled back | |
9816 | * in. | |
c93f7669 | 9817 | */ |
6a3351b6 | 9818 | raw_spin_lock_irq(&child_ctx->lock); |
63b6da39 | 9819 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
4a1c0f26 | 9820 | |
71a851b4 | 9821 | /* |
63b6da39 PZ |
9822 | * Now that the context is inactive, destroy the task <-> ctx relation |
9823 | * and mark the context dead. | |
71a851b4 | 9824 | */ |
63b6da39 PZ |
9825 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
9826 | put_ctx(child_ctx); /* cannot be last */ | |
9827 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
9828 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 9829 | |
211de6eb | 9830 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 9831 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 9832 | |
211de6eb PZ |
9833 | if (clone_ctx) |
9834 | put_ctx(clone_ctx); | |
4a1c0f26 | 9835 | |
9f498cc5 | 9836 | /* |
cdd6c482 IM |
9837 | * Report the task dead after unscheduling the events so that we |
9838 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
9839 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 9840 | */ |
cdd6c482 | 9841 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 9842 | |
ebf905fc | 9843 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 9844 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 9845 | |
a63eaf34 PM |
9846 | mutex_unlock(&child_ctx->mutex); |
9847 | ||
9848 | put_ctx(child_ctx); | |
9b51f66d IM |
9849 | } |
9850 | ||
8dc85d54 PZ |
9851 | /* |
9852 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 PZ |
9853 | * |
9854 | * Can be called with cred_guard_mutex held when called from | |
9855 | * install_exec_creds(). | |
8dc85d54 PZ |
9856 | */ |
9857 | void perf_event_exit_task(struct task_struct *child) | |
9858 | { | |
8882135b | 9859 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
9860 | int ctxn; |
9861 | ||
8882135b PZ |
9862 | mutex_lock(&child->perf_event_mutex); |
9863 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
9864 | owner_entry) { | |
9865 | list_del_init(&event->owner_entry); | |
9866 | ||
9867 | /* | |
9868 | * Ensure the list deletion is visible before we clear | |
9869 | * the owner, closes a race against perf_release() where | |
9870 | * we need to serialize on the owner->perf_event_mutex. | |
9871 | */ | |
f47c02c0 | 9872 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
9873 | } |
9874 | mutex_unlock(&child->perf_event_mutex); | |
9875 | ||
8dc85d54 PZ |
9876 | for_each_task_context_nr(ctxn) |
9877 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
9878 | |
9879 | /* | |
9880 | * The perf_event_exit_task_context calls perf_event_task | |
9881 | * with child's task_ctx, which generates EXIT events for | |
9882 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
9883 | * At this point we need to send EXIT events to cpu contexts. | |
9884 | */ | |
9885 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
9886 | } |
9887 | ||
889ff015 FW |
9888 | static void perf_free_event(struct perf_event *event, |
9889 | struct perf_event_context *ctx) | |
9890 | { | |
9891 | struct perf_event *parent = event->parent; | |
9892 | ||
9893 | if (WARN_ON_ONCE(!parent)) | |
9894 | return; | |
9895 | ||
9896 | mutex_lock(&parent->child_mutex); | |
9897 | list_del_init(&event->child_list); | |
9898 | mutex_unlock(&parent->child_mutex); | |
9899 | ||
a6fa941d | 9900 | put_event(parent); |
889ff015 | 9901 | |
652884fe | 9902 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 9903 | perf_group_detach(event); |
889ff015 | 9904 | list_del_event(event, ctx); |
652884fe | 9905 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
9906 | free_event(event); |
9907 | } | |
9908 | ||
bbbee908 | 9909 | /* |
652884fe | 9910 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 9911 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
9912 | * |
9913 | * Not all locks are strictly required, but take them anyway to be nice and | |
9914 | * help out with the lockdep assertions. | |
bbbee908 | 9915 | */ |
cdd6c482 | 9916 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 9917 | { |
8dc85d54 | 9918 | struct perf_event_context *ctx; |
cdd6c482 | 9919 | struct perf_event *event, *tmp; |
8dc85d54 | 9920 | int ctxn; |
bbbee908 | 9921 | |
8dc85d54 PZ |
9922 | for_each_task_context_nr(ctxn) { |
9923 | ctx = task->perf_event_ctxp[ctxn]; | |
9924 | if (!ctx) | |
9925 | continue; | |
bbbee908 | 9926 | |
8dc85d54 | 9927 | mutex_lock(&ctx->mutex); |
bbbee908 | 9928 | again: |
8dc85d54 PZ |
9929 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
9930 | group_entry) | |
9931 | perf_free_event(event, ctx); | |
bbbee908 | 9932 | |
8dc85d54 PZ |
9933 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
9934 | group_entry) | |
9935 | perf_free_event(event, ctx); | |
bbbee908 | 9936 | |
8dc85d54 PZ |
9937 | if (!list_empty(&ctx->pinned_groups) || |
9938 | !list_empty(&ctx->flexible_groups)) | |
9939 | goto again; | |
bbbee908 | 9940 | |
8dc85d54 | 9941 | mutex_unlock(&ctx->mutex); |
bbbee908 | 9942 | |
8dc85d54 PZ |
9943 | put_ctx(ctx); |
9944 | } | |
889ff015 FW |
9945 | } |
9946 | ||
4e231c79 PZ |
9947 | void perf_event_delayed_put(struct task_struct *task) |
9948 | { | |
9949 | int ctxn; | |
9950 | ||
9951 | for_each_task_context_nr(ctxn) | |
9952 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
9953 | } | |
9954 | ||
e03e7ee3 | 9955 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 9956 | { |
e03e7ee3 | 9957 | struct file *file; |
ffe8690c | 9958 | |
e03e7ee3 AS |
9959 | file = fget_raw(fd); |
9960 | if (!file) | |
9961 | return ERR_PTR(-EBADF); | |
ffe8690c | 9962 | |
e03e7ee3 AS |
9963 | if (file->f_op != &perf_fops) { |
9964 | fput(file); | |
9965 | return ERR_PTR(-EBADF); | |
9966 | } | |
ffe8690c | 9967 | |
e03e7ee3 | 9968 | return file; |
ffe8690c KX |
9969 | } |
9970 | ||
9971 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
9972 | { | |
9973 | if (!event) | |
9974 | return ERR_PTR(-EINVAL); | |
9975 | ||
9976 | return &event->attr; | |
9977 | } | |
9978 | ||
97dee4f3 PZ |
9979 | /* |
9980 | * inherit a event from parent task to child task: | |
9981 | */ | |
9982 | static struct perf_event * | |
9983 | inherit_event(struct perf_event *parent_event, | |
9984 | struct task_struct *parent, | |
9985 | struct perf_event_context *parent_ctx, | |
9986 | struct task_struct *child, | |
9987 | struct perf_event *group_leader, | |
9988 | struct perf_event_context *child_ctx) | |
9989 | { | |
1929def9 | 9990 | enum perf_event_active_state parent_state = parent_event->state; |
97dee4f3 | 9991 | struct perf_event *child_event; |
cee010ec | 9992 | unsigned long flags; |
97dee4f3 PZ |
9993 | |
9994 | /* | |
9995 | * Instead of creating recursive hierarchies of events, | |
9996 | * we link inherited events back to the original parent, | |
9997 | * which has a filp for sure, which we use as the reference | |
9998 | * count: | |
9999 | */ | |
10000 | if (parent_event->parent) | |
10001 | parent_event = parent_event->parent; | |
10002 | ||
10003 | child_event = perf_event_alloc(&parent_event->attr, | |
10004 | parent_event->cpu, | |
d580ff86 | 10005 | child, |
97dee4f3 | 10006 | group_leader, parent_event, |
79dff51e | 10007 | NULL, NULL, -1); |
97dee4f3 PZ |
10008 | if (IS_ERR(child_event)) |
10009 | return child_event; | |
a6fa941d | 10010 | |
c6e5b732 PZ |
10011 | /* |
10012 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
10013 | * must be under the same lock in order to serialize against | |
10014 | * perf_event_release_kernel(), such that either we must observe | |
10015 | * is_orphaned_event() or they will observe us on the child_list. | |
10016 | */ | |
10017 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
10018 | if (is_orphaned_event(parent_event) || |
10019 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 10020 | mutex_unlock(&parent_event->child_mutex); |
a6fa941d AV |
10021 | free_event(child_event); |
10022 | return NULL; | |
10023 | } | |
10024 | ||
97dee4f3 PZ |
10025 | get_ctx(child_ctx); |
10026 | ||
10027 | /* | |
10028 | * Make the child state follow the state of the parent event, | |
10029 | * not its attr.disabled bit. We hold the parent's mutex, | |
10030 | * so we won't race with perf_event_{en, dis}able_family. | |
10031 | */ | |
1929def9 | 10032 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
10033 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
10034 | else | |
10035 | child_event->state = PERF_EVENT_STATE_OFF; | |
10036 | ||
10037 | if (parent_event->attr.freq) { | |
10038 | u64 sample_period = parent_event->hw.sample_period; | |
10039 | struct hw_perf_event *hwc = &child_event->hw; | |
10040 | ||
10041 | hwc->sample_period = sample_period; | |
10042 | hwc->last_period = sample_period; | |
10043 | ||
10044 | local64_set(&hwc->period_left, sample_period); | |
10045 | } | |
10046 | ||
10047 | child_event->ctx = child_ctx; | |
10048 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
10049 | child_event->overflow_handler_context |
10050 | = parent_event->overflow_handler_context; | |
97dee4f3 | 10051 | |
614b6780 TG |
10052 | /* |
10053 | * Precalculate sample_data sizes | |
10054 | */ | |
10055 | perf_event__header_size(child_event); | |
6844c09d | 10056 | perf_event__id_header_size(child_event); |
614b6780 | 10057 | |
97dee4f3 PZ |
10058 | /* |
10059 | * Link it up in the child's context: | |
10060 | */ | |
cee010ec | 10061 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 10062 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 10063 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 10064 | |
97dee4f3 PZ |
10065 | /* |
10066 | * Link this into the parent event's child list | |
10067 | */ | |
97dee4f3 PZ |
10068 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
10069 | mutex_unlock(&parent_event->child_mutex); | |
10070 | ||
10071 | return child_event; | |
10072 | } | |
10073 | ||
10074 | static int inherit_group(struct perf_event *parent_event, | |
10075 | struct task_struct *parent, | |
10076 | struct perf_event_context *parent_ctx, | |
10077 | struct task_struct *child, | |
10078 | struct perf_event_context *child_ctx) | |
10079 | { | |
10080 | struct perf_event *leader; | |
10081 | struct perf_event *sub; | |
10082 | struct perf_event *child_ctr; | |
10083 | ||
10084 | leader = inherit_event(parent_event, parent, parent_ctx, | |
10085 | child, NULL, child_ctx); | |
10086 | if (IS_ERR(leader)) | |
10087 | return PTR_ERR(leader); | |
10088 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { | |
10089 | child_ctr = inherit_event(sub, parent, parent_ctx, | |
10090 | child, leader, child_ctx); | |
10091 | if (IS_ERR(child_ctr)) | |
10092 | return PTR_ERR(child_ctr); | |
10093 | } | |
10094 | return 0; | |
889ff015 FW |
10095 | } |
10096 | ||
10097 | static int | |
10098 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
10099 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 10100 | struct task_struct *child, int ctxn, |
889ff015 FW |
10101 | int *inherited_all) |
10102 | { | |
10103 | int ret; | |
8dc85d54 | 10104 | struct perf_event_context *child_ctx; |
889ff015 FW |
10105 | |
10106 | if (!event->attr.inherit) { | |
10107 | *inherited_all = 0; | |
10108 | return 0; | |
bbbee908 PZ |
10109 | } |
10110 | ||
fe4b04fa | 10111 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
10112 | if (!child_ctx) { |
10113 | /* | |
10114 | * This is executed from the parent task context, so | |
10115 | * inherit events that have been marked for cloning. | |
10116 | * First allocate and initialize a context for the | |
10117 | * child. | |
10118 | */ | |
bbbee908 | 10119 | |
734df5ab | 10120 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
10121 | if (!child_ctx) |
10122 | return -ENOMEM; | |
bbbee908 | 10123 | |
8dc85d54 | 10124 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
10125 | } |
10126 | ||
10127 | ret = inherit_group(event, parent, parent_ctx, | |
10128 | child, child_ctx); | |
10129 | ||
10130 | if (ret) | |
10131 | *inherited_all = 0; | |
10132 | ||
10133 | return ret; | |
bbbee908 PZ |
10134 | } |
10135 | ||
9b51f66d | 10136 | /* |
cdd6c482 | 10137 | * Initialize the perf_event context in task_struct |
9b51f66d | 10138 | */ |
985c8dcb | 10139 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 10140 | { |
889ff015 | 10141 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
10142 | struct perf_event_context *cloned_ctx; |
10143 | struct perf_event *event; | |
9b51f66d | 10144 | struct task_struct *parent = current; |
564c2b21 | 10145 | int inherited_all = 1; |
dddd3379 | 10146 | unsigned long flags; |
6ab423e0 | 10147 | int ret = 0; |
9b51f66d | 10148 | |
8dc85d54 | 10149 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
10150 | return 0; |
10151 | ||
ad3a37de | 10152 | /* |
25346b93 PM |
10153 | * If the parent's context is a clone, pin it so it won't get |
10154 | * swapped under us. | |
ad3a37de | 10155 | */ |
8dc85d54 | 10156 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
10157 | if (!parent_ctx) |
10158 | return 0; | |
25346b93 | 10159 | |
ad3a37de PM |
10160 | /* |
10161 | * No need to check if parent_ctx != NULL here; since we saw | |
10162 | * it non-NULL earlier, the only reason for it to become NULL | |
10163 | * is if we exit, and since we're currently in the middle of | |
10164 | * a fork we can't be exiting at the same time. | |
10165 | */ | |
ad3a37de | 10166 | |
9b51f66d IM |
10167 | /* |
10168 | * Lock the parent list. No need to lock the child - not PID | |
10169 | * hashed yet and not running, so nobody can access it. | |
10170 | */ | |
d859e29f | 10171 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
10172 | |
10173 | /* | |
10174 | * We dont have to disable NMIs - we are only looking at | |
10175 | * the list, not manipulating it: | |
10176 | */ | |
889ff015 | 10177 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
8dc85d54 PZ |
10178 | ret = inherit_task_group(event, parent, parent_ctx, |
10179 | child, ctxn, &inherited_all); | |
889ff015 FW |
10180 | if (ret) |
10181 | break; | |
10182 | } | |
b93f7978 | 10183 | |
dddd3379 TG |
10184 | /* |
10185 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
10186 | * to allocations, but we need to prevent rotation because | |
10187 | * rotate_ctx() will change the list from interrupt context. | |
10188 | */ | |
10189 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
10190 | parent_ctx->rotate_disable = 1; | |
10191 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
10192 | ||
889ff015 | 10193 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
8dc85d54 PZ |
10194 | ret = inherit_task_group(event, parent, parent_ctx, |
10195 | child, ctxn, &inherited_all); | |
889ff015 | 10196 | if (ret) |
9b51f66d | 10197 | break; |
564c2b21 PM |
10198 | } |
10199 | ||
dddd3379 TG |
10200 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
10201 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 10202 | |
8dc85d54 | 10203 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 10204 | |
05cbaa28 | 10205 | if (child_ctx && inherited_all) { |
564c2b21 PM |
10206 | /* |
10207 | * Mark the child context as a clone of the parent | |
10208 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
10209 | * |
10210 | * Note that if the parent is a clone, the holding of | |
10211 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 10212 | */ |
c5ed5145 | 10213 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
10214 | if (cloned_ctx) { |
10215 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 10216 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
10217 | } else { |
10218 | child_ctx->parent_ctx = parent_ctx; | |
10219 | child_ctx->parent_gen = parent_ctx->generation; | |
10220 | } | |
10221 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
10222 | } |
10223 | ||
c5ed5145 | 10224 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
d859e29f | 10225 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 10226 | |
25346b93 | 10227 | perf_unpin_context(parent_ctx); |
fe4b04fa | 10228 | put_ctx(parent_ctx); |
ad3a37de | 10229 | |
6ab423e0 | 10230 | return ret; |
9b51f66d IM |
10231 | } |
10232 | ||
8dc85d54 PZ |
10233 | /* |
10234 | * Initialize the perf_event context in task_struct | |
10235 | */ | |
10236 | int perf_event_init_task(struct task_struct *child) | |
10237 | { | |
10238 | int ctxn, ret; | |
10239 | ||
8550d7cb ON |
10240 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
10241 | mutex_init(&child->perf_event_mutex); | |
10242 | INIT_LIST_HEAD(&child->perf_event_list); | |
10243 | ||
8dc85d54 PZ |
10244 | for_each_task_context_nr(ctxn) { |
10245 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
10246 | if (ret) { |
10247 | perf_event_free_task(child); | |
8dc85d54 | 10248 | return ret; |
6c72e350 | 10249 | } |
8dc85d54 PZ |
10250 | } |
10251 | ||
10252 | return 0; | |
10253 | } | |
10254 | ||
220b140b PM |
10255 | static void __init perf_event_init_all_cpus(void) |
10256 | { | |
b28ab83c | 10257 | struct swevent_htable *swhash; |
220b140b | 10258 | int cpu; |
220b140b PM |
10259 | |
10260 | for_each_possible_cpu(cpu) { | |
b28ab83c PZ |
10261 | swhash = &per_cpu(swevent_htable, cpu); |
10262 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 10263 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
10264 | |
10265 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
10266 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
220b140b PM |
10267 | } |
10268 | } | |
10269 | ||
0db0628d | 10270 | static void perf_event_init_cpu(int cpu) |
0793a61d | 10271 | { |
108b02cf | 10272 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 10273 | |
b28ab83c | 10274 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 10275 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
10276 | struct swevent_hlist *hlist; |
10277 | ||
b28ab83c PZ |
10278 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
10279 | WARN_ON(!hlist); | |
10280 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 10281 | } |
b28ab83c | 10282 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
10283 | } |
10284 | ||
2965faa5 | 10285 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 10286 | static void __perf_event_exit_context(void *__info) |
0793a61d | 10287 | { |
108b02cf | 10288 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
10289 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
10290 | struct perf_event *event; | |
0793a61d | 10291 | |
fae3fde6 PZ |
10292 | raw_spin_lock(&ctx->lock); |
10293 | list_for_each_entry(event, &ctx->event_list, event_entry) | |
45a0e07a | 10294 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 10295 | raw_spin_unlock(&ctx->lock); |
0793a61d | 10296 | } |
108b02cf PZ |
10297 | |
10298 | static void perf_event_exit_cpu_context(int cpu) | |
10299 | { | |
10300 | struct perf_event_context *ctx; | |
10301 | struct pmu *pmu; | |
10302 | int idx; | |
10303 | ||
10304 | idx = srcu_read_lock(&pmus_srcu); | |
10305 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
917bdd1c | 10306 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
108b02cf PZ |
10307 | |
10308 | mutex_lock(&ctx->mutex); | |
10309 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
10310 | mutex_unlock(&ctx->mutex); | |
10311 | } | |
10312 | srcu_read_unlock(&pmus_srcu, idx); | |
108b02cf PZ |
10313 | } |
10314 | ||
cdd6c482 | 10315 | static void perf_event_exit_cpu(int cpu) |
0793a61d | 10316 | { |
e3703f8c | 10317 | perf_event_exit_cpu_context(cpu); |
0793a61d TG |
10318 | } |
10319 | #else | |
cdd6c482 | 10320 | static inline void perf_event_exit_cpu(int cpu) { } |
0793a61d TG |
10321 | #endif |
10322 | ||
c277443c PZ |
10323 | static int |
10324 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
10325 | { | |
10326 | int cpu; | |
10327 | ||
10328 | for_each_online_cpu(cpu) | |
10329 | perf_event_exit_cpu(cpu); | |
10330 | ||
10331 | return NOTIFY_OK; | |
10332 | } | |
10333 | ||
10334 | /* | |
10335 | * Run the perf reboot notifier at the very last possible moment so that | |
10336 | * the generic watchdog code runs as long as possible. | |
10337 | */ | |
10338 | static struct notifier_block perf_reboot_notifier = { | |
10339 | .notifier_call = perf_reboot, | |
10340 | .priority = INT_MIN, | |
10341 | }; | |
10342 | ||
0db0628d | 10343 | static int |
0793a61d TG |
10344 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
10345 | { | |
10346 | unsigned int cpu = (long)hcpu; | |
10347 | ||
4536e4d1 | 10348 | switch (action & ~CPU_TASKS_FROZEN) { |
0793a61d TG |
10349 | |
10350 | case CPU_UP_PREPARE: | |
1dcaac1c PZ |
10351 | /* |
10352 | * This must be done before the CPU comes alive, because the | |
10353 | * moment we can run tasks we can encounter (software) events. | |
10354 | * | |
10355 | * Specifically, someone can have inherited events on kthreadd | |
10356 | * or a pre-existing worker thread that gets re-bound. | |
10357 | */ | |
cdd6c482 | 10358 | perf_event_init_cpu(cpu); |
0793a61d TG |
10359 | break; |
10360 | ||
10361 | case CPU_DOWN_PREPARE: | |
1dcaac1c PZ |
10362 | /* |
10363 | * This must be done before the CPU dies because after that an | |
10364 | * active event might want to IPI the CPU and that'll not work | |
10365 | * so great for dead CPUs. | |
10366 | * | |
10367 | * XXX smp_call_function_single() return -ENXIO without a warn | |
10368 | * so we could possibly deal with this. | |
10369 | * | |
10370 | * This is safe against new events arriving because | |
10371 | * sys_perf_event_open() serializes against hotplug using | |
10372 | * get_online_cpus(). | |
10373 | */ | |
cdd6c482 | 10374 | perf_event_exit_cpu(cpu); |
0793a61d | 10375 | break; |
0793a61d TG |
10376 | default: |
10377 | break; | |
10378 | } | |
10379 | ||
10380 | return NOTIFY_OK; | |
10381 | } | |
10382 | ||
cdd6c482 | 10383 | void __init perf_event_init(void) |
0793a61d | 10384 | { |
3c502e7a JW |
10385 | int ret; |
10386 | ||
2e80a82a PZ |
10387 | idr_init(&pmu_idr); |
10388 | ||
220b140b | 10389 | perf_event_init_all_cpus(); |
b0a873eb | 10390 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
10391 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
10392 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
10393 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb PZ |
10394 | perf_tp_register(); |
10395 | perf_cpu_notifier(perf_cpu_notify); | |
c277443c | 10396 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
10397 | |
10398 | ret = init_hw_breakpoint(); | |
10399 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 10400 | |
b01c3a00 JO |
10401 | /* |
10402 | * Build time assertion that we keep the data_head at the intended | |
10403 | * location. IOW, validation we got the __reserved[] size right. | |
10404 | */ | |
10405 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
10406 | != 1024); | |
0793a61d | 10407 | } |
abe43400 | 10408 | |
fd979c01 CS |
10409 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
10410 | char *page) | |
10411 | { | |
10412 | struct perf_pmu_events_attr *pmu_attr = | |
10413 | container_of(attr, struct perf_pmu_events_attr, attr); | |
10414 | ||
10415 | if (pmu_attr->event_str) | |
10416 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
10417 | ||
10418 | return 0; | |
10419 | } | |
675965b0 | 10420 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 10421 | |
abe43400 PZ |
10422 | static int __init perf_event_sysfs_init(void) |
10423 | { | |
10424 | struct pmu *pmu; | |
10425 | int ret; | |
10426 | ||
10427 | mutex_lock(&pmus_lock); | |
10428 | ||
10429 | ret = bus_register(&pmu_bus); | |
10430 | if (ret) | |
10431 | goto unlock; | |
10432 | ||
10433 | list_for_each_entry(pmu, &pmus, entry) { | |
10434 | if (!pmu->name || pmu->type < 0) | |
10435 | continue; | |
10436 | ||
10437 | ret = pmu_dev_alloc(pmu); | |
10438 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
10439 | } | |
10440 | pmu_bus_running = 1; | |
10441 | ret = 0; | |
10442 | ||
10443 | unlock: | |
10444 | mutex_unlock(&pmus_lock); | |
10445 | ||
10446 | return ret; | |
10447 | } | |
10448 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
10449 | |
10450 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
10451 | static struct cgroup_subsys_state * |
10452 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
10453 | { |
10454 | struct perf_cgroup *jc; | |
e5d1367f | 10455 | |
1b15d055 | 10456 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
10457 | if (!jc) |
10458 | return ERR_PTR(-ENOMEM); | |
10459 | ||
e5d1367f SE |
10460 | jc->info = alloc_percpu(struct perf_cgroup_info); |
10461 | if (!jc->info) { | |
10462 | kfree(jc); | |
10463 | return ERR_PTR(-ENOMEM); | |
10464 | } | |
10465 | ||
e5d1367f SE |
10466 | return &jc->css; |
10467 | } | |
10468 | ||
eb95419b | 10469 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 10470 | { |
eb95419b TH |
10471 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
10472 | ||
e5d1367f SE |
10473 | free_percpu(jc->info); |
10474 | kfree(jc); | |
10475 | } | |
10476 | ||
10477 | static int __perf_cgroup_move(void *info) | |
10478 | { | |
10479 | struct task_struct *task = info; | |
ddaaf4e2 | 10480 | rcu_read_lock(); |
e5d1367f | 10481 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 10482 | rcu_read_unlock(); |
e5d1367f SE |
10483 | return 0; |
10484 | } | |
10485 | ||
1f7dd3e5 | 10486 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 10487 | { |
bb9d97b6 | 10488 | struct task_struct *task; |
1f7dd3e5 | 10489 | struct cgroup_subsys_state *css; |
bb9d97b6 | 10490 | |
1f7dd3e5 | 10491 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 10492 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
10493 | } |
10494 | ||
073219e9 | 10495 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
10496 | .css_alloc = perf_cgroup_css_alloc, |
10497 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 10498 | .attach = perf_cgroup_attach, |
e5d1367f SE |
10499 | }; |
10500 | #endif /* CONFIG_CGROUP_PERF */ |