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> | |
0793a61d | 47 | |
76369139 FW |
48 | #include "internal.h" |
49 | ||
4e193bd4 TB |
50 | #include <asm/irq_regs.h> |
51 | ||
272325c4 PZ |
52 | typedef int (*remote_function_f)(void *); |
53 | ||
fe4b04fa | 54 | struct remote_function_call { |
e7e7ee2e | 55 | struct task_struct *p; |
272325c4 | 56 | remote_function_f func; |
e7e7ee2e IM |
57 | void *info; |
58 | int ret; | |
fe4b04fa PZ |
59 | }; |
60 | ||
61 | static void remote_function(void *data) | |
62 | { | |
63 | struct remote_function_call *tfc = data; | |
64 | struct task_struct *p = tfc->p; | |
65 | ||
66 | if (p) { | |
0da4cf3e PZ |
67 | /* -EAGAIN */ |
68 | if (task_cpu(p) != smp_processor_id()) | |
69 | return; | |
70 | ||
71 | /* | |
72 | * Now that we're on right CPU with IRQs disabled, we can test | |
73 | * if we hit the right task without races. | |
74 | */ | |
75 | ||
76 | tfc->ret = -ESRCH; /* No such (running) process */ | |
77 | if (p != current) | |
fe4b04fa PZ |
78 | return; |
79 | } | |
80 | ||
81 | tfc->ret = tfc->func(tfc->info); | |
82 | } | |
83 | ||
84 | /** | |
85 | * task_function_call - call a function on the cpu on which a task runs | |
86 | * @p: the task to evaluate | |
87 | * @func: the function to be called | |
88 | * @info: the function call argument | |
89 | * | |
90 | * Calls the function @func when the task is currently running. This might | |
91 | * be on the current CPU, which just calls the function directly | |
92 | * | |
93 | * returns: @func return value, or | |
94 | * -ESRCH - when the process isn't running | |
95 | * -EAGAIN - when the process moved away | |
96 | */ | |
97 | static int | |
272325c4 | 98 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
99 | { |
100 | struct remote_function_call data = { | |
e7e7ee2e IM |
101 | .p = p, |
102 | .func = func, | |
103 | .info = info, | |
0da4cf3e | 104 | .ret = -EAGAIN, |
fe4b04fa | 105 | }; |
0da4cf3e | 106 | int ret; |
fe4b04fa | 107 | |
0da4cf3e PZ |
108 | do { |
109 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); | |
110 | if (!ret) | |
111 | ret = data.ret; | |
112 | } while (ret == -EAGAIN); | |
fe4b04fa | 113 | |
0da4cf3e | 114 | return ret; |
fe4b04fa PZ |
115 | } |
116 | ||
117 | /** | |
118 | * cpu_function_call - call a function on the cpu | |
119 | * @func: the function to be called | |
120 | * @info: the function call argument | |
121 | * | |
122 | * Calls the function @func on the remote cpu. | |
123 | * | |
124 | * returns: @func return value or -ENXIO when the cpu is offline | |
125 | */ | |
272325c4 | 126 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
127 | { |
128 | struct remote_function_call data = { | |
e7e7ee2e IM |
129 | .p = NULL, |
130 | .func = func, | |
131 | .info = info, | |
132 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
133 | }; |
134 | ||
135 | smp_call_function_single(cpu, remote_function, &data, 1); | |
136 | ||
137 | return data.ret; | |
138 | } | |
139 | ||
fae3fde6 PZ |
140 | static inline struct perf_cpu_context * |
141 | __get_cpu_context(struct perf_event_context *ctx) | |
142 | { | |
143 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); | |
144 | } | |
145 | ||
146 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, | |
147 | struct perf_event_context *ctx) | |
0017960f | 148 | { |
fae3fde6 PZ |
149 | raw_spin_lock(&cpuctx->ctx.lock); |
150 | if (ctx) | |
151 | raw_spin_lock(&ctx->lock); | |
152 | } | |
153 | ||
154 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
155 | struct perf_event_context *ctx) | |
156 | { | |
157 | if (ctx) | |
158 | raw_spin_unlock(&ctx->lock); | |
159 | raw_spin_unlock(&cpuctx->ctx.lock); | |
160 | } | |
161 | ||
63b6da39 PZ |
162 | #define TASK_TOMBSTONE ((void *)-1L) |
163 | ||
164 | static bool is_kernel_event(struct perf_event *event) | |
165 | { | |
f47c02c0 | 166 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
167 | } |
168 | ||
39a43640 PZ |
169 | /* |
170 | * On task ctx scheduling... | |
171 | * | |
172 | * When !ctx->nr_events a task context will not be scheduled. This means | |
173 | * we can disable the scheduler hooks (for performance) without leaving | |
174 | * pending task ctx state. | |
175 | * | |
176 | * This however results in two special cases: | |
177 | * | |
178 | * - removing the last event from a task ctx; this is relatively straight | |
179 | * forward and is done in __perf_remove_from_context. | |
180 | * | |
181 | * - adding the first event to a task ctx; this is tricky because we cannot | |
182 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
183 | * See perf_install_in_context(). | |
184 | * | |
39a43640 PZ |
185 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
186 | */ | |
187 | ||
fae3fde6 PZ |
188 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
189 | struct perf_event_context *, void *); | |
190 | ||
191 | struct event_function_struct { | |
192 | struct perf_event *event; | |
193 | event_f func; | |
194 | void *data; | |
195 | }; | |
196 | ||
197 | static int event_function(void *info) | |
198 | { | |
199 | struct event_function_struct *efs = info; | |
200 | struct perf_event *event = efs->event; | |
0017960f | 201 | struct perf_event_context *ctx = event->ctx; |
fae3fde6 PZ |
202 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
203 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
63b6da39 | 204 | int ret = 0; |
fae3fde6 PZ |
205 | |
206 | WARN_ON_ONCE(!irqs_disabled()); | |
207 | ||
63b6da39 | 208 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
209 | /* |
210 | * Since we do the IPI call without holding ctx->lock things can have | |
211 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
212 | */ |
213 | if (ctx->task) { | |
63b6da39 | 214 | if (ctx->task != current) { |
0da4cf3e | 215 | ret = -ESRCH; |
63b6da39 PZ |
216 | goto unlock; |
217 | } | |
fae3fde6 | 218 | |
fae3fde6 PZ |
219 | /* |
220 | * We only use event_function_call() on established contexts, | |
221 | * and event_function() is only ever called when active (or | |
222 | * rather, we'll have bailed in task_function_call() or the | |
223 | * above ctx->task != current test), therefore we must have | |
224 | * ctx->is_active here. | |
225 | */ | |
226 | WARN_ON_ONCE(!ctx->is_active); | |
227 | /* | |
228 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
229 | * match. | |
230 | */ | |
63b6da39 PZ |
231 | WARN_ON_ONCE(task_ctx != ctx); |
232 | } else { | |
233 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 234 | } |
63b6da39 | 235 | |
fae3fde6 | 236 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 237 | unlock: |
fae3fde6 PZ |
238 | perf_ctx_unlock(cpuctx, task_ctx); |
239 | ||
63b6da39 | 240 | return ret; |
fae3fde6 PZ |
241 | } |
242 | ||
243 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
244 | { | |
245 | struct event_function_struct efs = { | |
246 | .event = event, | |
247 | .func = func, | |
248 | .data = data, | |
249 | }; | |
250 | ||
251 | int ret = event_function(&efs); | |
252 | WARN_ON_ONCE(ret); | |
253 | } | |
254 | ||
255 | static void event_function_call(struct perf_event *event, event_f func, void *data) | |
0017960f PZ |
256 | { |
257 | struct perf_event_context *ctx = event->ctx; | |
63b6da39 | 258 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
fae3fde6 PZ |
259 | struct event_function_struct efs = { |
260 | .event = event, | |
261 | .func = func, | |
262 | .data = data, | |
263 | }; | |
0017960f | 264 | |
c97f4736 PZ |
265 | if (!event->parent) { |
266 | /* | |
267 | * If this is a !child event, we must hold ctx::mutex to | |
268 | * stabilize the the event->ctx relation. See | |
269 | * perf_event_ctx_lock(). | |
270 | */ | |
271 | lockdep_assert_held(&ctx->mutex); | |
272 | } | |
0017960f PZ |
273 | |
274 | if (!task) { | |
fae3fde6 | 275 | cpu_function_call(event->cpu, event_function, &efs); |
0017960f PZ |
276 | return; |
277 | } | |
278 | ||
63b6da39 PZ |
279 | if (task == TASK_TOMBSTONE) |
280 | return; | |
281 | ||
a096309b | 282 | again: |
fae3fde6 | 283 | if (!task_function_call(task, event_function, &efs)) |
0017960f PZ |
284 | return; |
285 | ||
286 | raw_spin_lock_irq(&ctx->lock); | |
63b6da39 PZ |
287 | /* |
288 | * Reload the task pointer, it might have been changed by | |
289 | * a concurrent perf_event_context_sched_out(). | |
290 | */ | |
291 | task = ctx->task; | |
a096309b PZ |
292 | if (task == TASK_TOMBSTONE) { |
293 | raw_spin_unlock_irq(&ctx->lock); | |
294 | return; | |
0017960f | 295 | } |
a096309b PZ |
296 | if (ctx->is_active) { |
297 | raw_spin_unlock_irq(&ctx->lock); | |
298 | goto again; | |
299 | } | |
300 | func(event, NULL, ctx, data); | |
0017960f PZ |
301 | raw_spin_unlock_irq(&ctx->lock); |
302 | } | |
303 | ||
e5d1367f SE |
304 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
305 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
306 | PERF_FLAG_PID_CGROUP |\ |
307 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 308 | |
bce38cd5 SE |
309 | /* |
310 | * branch priv levels that need permission checks | |
311 | */ | |
312 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
313 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
314 | PERF_SAMPLE_BRANCH_HV) | |
315 | ||
0b3fcf17 SE |
316 | enum event_type_t { |
317 | EVENT_FLEXIBLE = 0x1, | |
318 | EVENT_PINNED = 0x2, | |
3cbaa590 | 319 | EVENT_TIME = 0x4, |
0b3fcf17 SE |
320 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
321 | }; | |
322 | ||
e5d1367f SE |
323 | /* |
324 | * perf_sched_events : >0 events exist | |
325 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu | |
326 | */ | |
9107c89e PZ |
327 | |
328 | static void perf_sched_delayed(struct work_struct *work); | |
329 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
330 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
331 | static DEFINE_MUTEX(perf_sched_mutex); | |
332 | static atomic_t perf_sched_count; | |
333 | ||
e5d1367f | 334 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
ba532500 | 335 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
e5d1367f | 336 | |
cdd6c482 IM |
337 | static atomic_t nr_mmap_events __read_mostly; |
338 | static atomic_t nr_comm_events __read_mostly; | |
339 | static atomic_t nr_task_events __read_mostly; | |
948b26b6 | 340 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 341 | static atomic_t nr_switch_events __read_mostly; |
9ee318a7 | 342 | |
108b02cf PZ |
343 | static LIST_HEAD(pmus); |
344 | static DEFINE_MUTEX(pmus_lock); | |
345 | static struct srcu_struct pmus_srcu; | |
346 | ||
0764771d | 347 | /* |
cdd6c482 | 348 | * perf event paranoia level: |
0fbdea19 IM |
349 | * -1 - not paranoid at all |
350 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 351 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 352 | * 2 - disallow kernel profiling for unpriv |
0764771d | 353 | */ |
cdd6c482 | 354 | int sysctl_perf_event_paranoid __read_mostly = 1; |
0764771d | 355 | |
20443384 FW |
356 | /* Minimum for 512 kiB + 1 user control page */ |
357 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
358 | |
359 | /* | |
cdd6c482 | 360 | * max perf event sample rate |
df58ab24 | 361 | */ |
14c63f17 DH |
362 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
363 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
364 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
365 | ||
366 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
367 | ||
368 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
369 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
370 | ||
d9494cb4 PZ |
371 | static int perf_sample_allowed_ns __read_mostly = |
372 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 373 | |
18ab2cd3 | 374 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
375 | { |
376 | u64 tmp = perf_sample_period_ns; | |
377 | ||
378 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
379 | tmp = div_u64(tmp, 100); |
380 | if (!tmp) | |
381 | tmp = 1; | |
382 | ||
383 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 384 | } |
163ec435 | 385 | |
9e630205 SE |
386 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
387 | ||
163ec435 PZ |
388 | int perf_proc_update_handler(struct ctl_table *table, int write, |
389 | void __user *buffer, size_t *lenp, | |
390 | loff_t *ppos) | |
391 | { | |
723478c8 | 392 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
163ec435 PZ |
393 | |
394 | if (ret || !write) | |
395 | return ret; | |
396 | ||
397 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); | |
14c63f17 DH |
398 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
399 | update_perf_cpu_limits(); | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
405 | ||
406 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
407 | void __user *buffer, size_t *lenp, | |
408 | loff_t *ppos) | |
409 | { | |
410 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
411 | ||
412 | if (ret || !write) | |
413 | return ret; | |
414 | ||
b303e7c1 PZ |
415 | if (sysctl_perf_cpu_time_max_percent == 100 || |
416 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
417 | printk(KERN_WARNING |
418 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
419 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
420 | } else { | |
421 | update_perf_cpu_limits(); | |
422 | } | |
163ec435 PZ |
423 | |
424 | return 0; | |
425 | } | |
1ccd1549 | 426 | |
14c63f17 DH |
427 | /* |
428 | * perf samples are done in some very critical code paths (NMIs). | |
429 | * If they take too much CPU time, the system can lock up and not | |
430 | * get any real work done. This will drop the sample rate when | |
431 | * we detect that events are taking too long. | |
432 | */ | |
433 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 434 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 435 | |
91a612ee PZ |
436 | static u64 __report_avg; |
437 | static u64 __report_allowed; | |
438 | ||
6a02ad66 | 439 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 440 | { |
6a02ad66 | 441 | printk_ratelimited(KERN_WARNING |
91a612ee PZ |
442 | "perf: interrupt took too long (%lld > %lld), lowering " |
443 | "kernel.perf_event_max_sample_rate to %d\n", | |
444 | __report_avg, __report_allowed, | |
445 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
446 | } |
447 | ||
448 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
449 | ||
450 | void perf_sample_event_took(u64 sample_len_ns) | |
451 | { | |
91a612ee PZ |
452 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
453 | u64 running_len; | |
454 | u64 avg_len; | |
455 | u32 max; | |
14c63f17 | 456 | |
91a612ee | 457 | if (max_len == 0) |
14c63f17 DH |
458 | return; |
459 | ||
91a612ee PZ |
460 | /* Decay the counter by 1 average sample. */ |
461 | running_len = __this_cpu_read(running_sample_length); | |
462 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
463 | running_len += sample_len_ns; | |
464 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
465 | |
466 | /* | |
91a612ee PZ |
467 | * Note: this will be biased artifically low until we have |
468 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
469 | * from having to maintain a count. |
470 | */ | |
91a612ee PZ |
471 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
472 | if (avg_len <= max_len) | |
14c63f17 DH |
473 | return; |
474 | ||
91a612ee PZ |
475 | __report_avg = avg_len; |
476 | __report_allowed = max_len; | |
14c63f17 | 477 | |
91a612ee PZ |
478 | /* |
479 | * Compute a throttle threshold 25% below the current duration. | |
480 | */ | |
481 | avg_len += avg_len / 4; | |
482 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
483 | if (avg_len < max) | |
484 | max /= (u32)avg_len; | |
485 | else | |
486 | max = 1; | |
14c63f17 | 487 | |
91a612ee PZ |
488 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
489 | WRITE_ONCE(max_samples_per_tick, max); | |
490 | ||
491 | sysctl_perf_event_sample_rate = max * HZ; | |
492 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 493 | |
cd578abb | 494 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 495 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 496 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 497 | __report_avg, __report_allowed, |
cd578abb PZ |
498 | sysctl_perf_event_sample_rate); |
499 | } | |
14c63f17 DH |
500 | } |
501 | ||
cdd6c482 | 502 | static atomic64_t perf_event_id; |
a96bbc16 | 503 | |
0b3fcf17 SE |
504 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
505 | enum event_type_t event_type); | |
506 | ||
507 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
508 | enum event_type_t event_type, |
509 | struct task_struct *task); | |
510 | ||
511 | static void update_context_time(struct perf_event_context *ctx); | |
512 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 513 | |
cdd6c482 | 514 | void __weak perf_event_print_debug(void) { } |
0793a61d | 515 | |
84c79910 | 516 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 517 | { |
84c79910 | 518 | return "pmu"; |
0793a61d TG |
519 | } |
520 | ||
0b3fcf17 SE |
521 | static inline u64 perf_clock(void) |
522 | { | |
523 | return local_clock(); | |
524 | } | |
525 | ||
34f43927 PZ |
526 | static inline u64 perf_event_clock(struct perf_event *event) |
527 | { | |
528 | return event->clock(); | |
529 | } | |
530 | ||
e5d1367f SE |
531 | #ifdef CONFIG_CGROUP_PERF |
532 | ||
e5d1367f SE |
533 | static inline bool |
534 | perf_cgroup_match(struct perf_event *event) | |
535 | { | |
536 | struct perf_event_context *ctx = event->ctx; | |
537 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
538 | ||
ef824fa1 TH |
539 | /* @event doesn't care about cgroup */ |
540 | if (!event->cgrp) | |
541 | return true; | |
542 | ||
543 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
544 | if (!cpuctx->cgrp) | |
545 | return false; | |
546 | ||
547 | /* | |
548 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
549 | * also enabled for all its descendant cgroups. If @cpuctx's | |
550 | * cgroup is a descendant of @event's (the test covers identity | |
551 | * case), it's a match. | |
552 | */ | |
553 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
554 | event->cgrp->css.cgroup); | |
e5d1367f SE |
555 | } |
556 | ||
e5d1367f SE |
557 | static inline void perf_detach_cgroup(struct perf_event *event) |
558 | { | |
4e2ba650 | 559 | css_put(&event->cgrp->css); |
e5d1367f SE |
560 | event->cgrp = NULL; |
561 | } | |
562 | ||
563 | static inline int is_cgroup_event(struct perf_event *event) | |
564 | { | |
565 | return event->cgrp != NULL; | |
566 | } | |
567 | ||
568 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
569 | { | |
570 | struct perf_cgroup_info *t; | |
571 | ||
572 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
573 | return t->time; | |
574 | } | |
575 | ||
576 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
577 | { | |
578 | struct perf_cgroup_info *info; | |
579 | u64 now; | |
580 | ||
581 | now = perf_clock(); | |
582 | ||
583 | info = this_cpu_ptr(cgrp->info); | |
584 | ||
585 | info->time += now - info->timestamp; | |
586 | info->timestamp = now; | |
587 | } | |
588 | ||
589 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
590 | { | |
591 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; | |
592 | if (cgrp_out) | |
593 | __update_cgrp_time(cgrp_out); | |
594 | } | |
595 | ||
596 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
597 | { | |
3f7cce3c SE |
598 | struct perf_cgroup *cgrp; |
599 | ||
e5d1367f | 600 | /* |
3f7cce3c SE |
601 | * ensure we access cgroup data only when needed and |
602 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 603 | */ |
3f7cce3c | 604 | if (!is_cgroup_event(event)) |
e5d1367f SE |
605 | return; |
606 | ||
614e4c4e | 607 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
608 | /* |
609 | * Do not update time when cgroup is not active | |
610 | */ | |
611 | if (cgrp == event->cgrp) | |
612 | __update_cgrp_time(event->cgrp); | |
e5d1367f SE |
613 | } |
614 | ||
615 | static inline void | |
3f7cce3c SE |
616 | perf_cgroup_set_timestamp(struct task_struct *task, |
617 | struct perf_event_context *ctx) | |
e5d1367f SE |
618 | { |
619 | struct perf_cgroup *cgrp; | |
620 | struct perf_cgroup_info *info; | |
621 | ||
3f7cce3c SE |
622 | /* |
623 | * ctx->lock held by caller | |
624 | * ensure we do not access cgroup data | |
625 | * unless we have the cgroup pinned (css_get) | |
626 | */ | |
627 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
628 | return; |
629 | ||
614e4c4e | 630 | cgrp = perf_cgroup_from_task(task, ctx); |
e5d1367f | 631 | info = this_cpu_ptr(cgrp->info); |
3f7cce3c | 632 | info->timestamp = ctx->timestamp; |
e5d1367f SE |
633 | } |
634 | ||
635 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ | |
636 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
637 | ||
638 | /* | |
639 | * reschedule events based on the cgroup constraint of task. | |
640 | * | |
641 | * mode SWOUT : schedule out everything | |
642 | * mode SWIN : schedule in based on cgroup for next | |
643 | */ | |
18ab2cd3 | 644 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
645 | { |
646 | struct perf_cpu_context *cpuctx; | |
647 | struct pmu *pmu; | |
648 | unsigned long flags; | |
649 | ||
650 | /* | |
651 | * disable interrupts to avoid geting nr_cgroup | |
652 | * changes via __perf_event_disable(). Also | |
653 | * avoids preemption. | |
654 | */ | |
655 | local_irq_save(flags); | |
656 | ||
657 | /* | |
658 | * we reschedule only in the presence of cgroup | |
659 | * constrained events. | |
660 | */ | |
e5d1367f SE |
661 | |
662 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
e5d1367f | 663 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95cf59ea PZ |
664 | if (cpuctx->unique_pmu != pmu) |
665 | continue; /* ensure we process each cpuctx once */ | |
e5d1367f | 666 | |
e5d1367f SE |
667 | /* |
668 | * perf_cgroup_events says at least one | |
669 | * context on this CPU has cgroup events. | |
670 | * | |
671 | * ctx->nr_cgroups reports the number of cgroup | |
672 | * events for a context. | |
673 | */ | |
674 | if (cpuctx->ctx.nr_cgroups > 0) { | |
facc4307 PZ |
675 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
676 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f SE |
677 | |
678 | if (mode & PERF_CGROUP_SWOUT) { | |
679 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
680 | /* | |
681 | * must not be done before ctxswout due | |
682 | * to event_filter_match() in event_sched_out() | |
683 | */ | |
684 | cpuctx->cgrp = NULL; | |
685 | } | |
686 | ||
687 | if (mode & PERF_CGROUP_SWIN) { | |
e566b76e | 688 | WARN_ON_ONCE(cpuctx->cgrp); |
95cf59ea PZ |
689 | /* |
690 | * set cgrp before ctxsw in to allow | |
691 | * event_filter_match() to not have to pass | |
692 | * task around | |
614e4c4e SE |
693 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
694 | * because cgorup events are only per-cpu | |
e5d1367f | 695 | */ |
614e4c4e | 696 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
e5d1367f SE |
697 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
698 | } | |
facc4307 PZ |
699 | perf_pmu_enable(cpuctx->ctx.pmu); |
700 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f | 701 | } |
e5d1367f SE |
702 | } |
703 | ||
e5d1367f SE |
704 | local_irq_restore(flags); |
705 | } | |
706 | ||
a8d757ef SE |
707 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
708 | struct task_struct *next) | |
e5d1367f | 709 | { |
a8d757ef SE |
710 | struct perf_cgroup *cgrp1; |
711 | struct perf_cgroup *cgrp2 = NULL; | |
712 | ||
ddaaf4e2 | 713 | rcu_read_lock(); |
a8d757ef SE |
714 | /* |
715 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
716 | * we do not need to pass the ctx here because we know |
717 | * we are holding the rcu lock | |
a8d757ef | 718 | */ |
614e4c4e | 719 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 720 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
721 | |
722 | /* | |
723 | * only schedule out current cgroup events if we know | |
724 | * that we are switching to a different cgroup. Otherwise, | |
725 | * do no touch the cgroup events. | |
726 | */ | |
727 | if (cgrp1 != cgrp2) | |
728 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
729 | |
730 | rcu_read_unlock(); | |
e5d1367f SE |
731 | } |
732 | ||
a8d757ef SE |
733 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
734 | struct task_struct *task) | |
e5d1367f | 735 | { |
a8d757ef SE |
736 | struct perf_cgroup *cgrp1; |
737 | struct perf_cgroup *cgrp2 = NULL; | |
738 | ||
ddaaf4e2 | 739 | rcu_read_lock(); |
a8d757ef SE |
740 | /* |
741 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
742 | * we do not need to pass the ctx here because we know |
743 | * we are holding the rcu lock | |
a8d757ef | 744 | */ |
614e4c4e | 745 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 746 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
747 | |
748 | /* | |
749 | * only need to schedule in cgroup events if we are changing | |
750 | * cgroup during ctxsw. Cgroup events were not scheduled | |
751 | * out of ctxsw out if that was not the case. | |
752 | */ | |
753 | if (cgrp1 != cgrp2) | |
754 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
755 | |
756 | rcu_read_unlock(); | |
e5d1367f SE |
757 | } |
758 | ||
759 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, | |
760 | struct perf_event_attr *attr, | |
761 | struct perf_event *group_leader) | |
762 | { | |
763 | struct perf_cgroup *cgrp; | |
764 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
765 | struct fd f = fdget(fd); |
766 | int ret = 0; | |
e5d1367f | 767 | |
2903ff01 | 768 | if (!f.file) |
e5d1367f SE |
769 | return -EBADF; |
770 | ||
b583043e | 771 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 772 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
773 | if (IS_ERR(css)) { |
774 | ret = PTR_ERR(css); | |
775 | goto out; | |
776 | } | |
e5d1367f SE |
777 | |
778 | cgrp = container_of(css, struct perf_cgroup, css); | |
779 | event->cgrp = cgrp; | |
780 | ||
781 | /* | |
782 | * all events in a group must monitor | |
783 | * the same cgroup because a task belongs | |
784 | * to only one perf cgroup at a time | |
785 | */ | |
786 | if (group_leader && group_leader->cgrp != cgrp) { | |
787 | perf_detach_cgroup(event); | |
788 | ret = -EINVAL; | |
e5d1367f | 789 | } |
3db272c0 | 790 | out: |
2903ff01 | 791 | fdput(f); |
e5d1367f SE |
792 | return ret; |
793 | } | |
794 | ||
795 | static inline void | |
796 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
797 | { | |
798 | struct perf_cgroup_info *t; | |
799 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
800 | event->shadow_ctx_time = now - t->timestamp; | |
801 | } | |
802 | ||
803 | static inline void | |
804 | perf_cgroup_defer_enabled(struct perf_event *event) | |
805 | { | |
806 | /* | |
807 | * when the current task's perf cgroup does not match | |
808 | * the event's, we need to remember to call the | |
809 | * perf_mark_enable() function the first time a task with | |
810 | * a matching perf cgroup is scheduled in. | |
811 | */ | |
812 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) | |
813 | event->cgrp_defer_enabled = 1; | |
814 | } | |
815 | ||
816 | static inline void | |
817 | perf_cgroup_mark_enabled(struct perf_event *event, | |
818 | struct perf_event_context *ctx) | |
819 | { | |
820 | struct perf_event *sub; | |
821 | u64 tstamp = perf_event_time(event); | |
822 | ||
823 | if (!event->cgrp_defer_enabled) | |
824 | return; | |
825 | ||
826 | event->cgrp_defer_enabled = 0; | |
827 | ||
828 | event->tstamp_enabled = tstamp - event->total_time_enabled; | |
829 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
830 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { | |
831 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
832 | sub->cgrp_defer_enabled = 0; | |
833 | } | |
834 | } | |
835 | } | |
836 | #else /* !CONFIG_CGROUP_PERF */ | |
837 | ||
838 | static inline bool | |
839 | perf_cgroup_match(struct perf_event *event) | |
840 | { | |
841 | return true; | |
842 | } | |
843 | ||
844 | static inline void perf_detach_cgroup(struct perf_event *event) | |
845 | {} | |
846 | ||
847 | static inline int is_cgroup_event(struct perf_event *event) | |
848 | { | |
849 | return 0; | |
850 | } | |
851 | ||
852 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) | |
853 | { | |
854 | return 0; | |
855 | } | |
856 | ||
857 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
858 | { | |
859 | } | |
860 | ||
861 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
862 | { | |
863 | } | |
864 | ||
a8d757ef SE |
865 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
866 | struct task_struct *next) | |
e5d1367f SE |
867 | { |
868 | } | |
869 | ||
a8d757ef SE |
870 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
871 | struct task_struct *task) | |
e5d1367f SE |
872 | { |
873 | } | |
874 | ||
875 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
876 | struct perf_event_attr *attr, | |
877 | struct perf_event *group_leader) | |
878 | { | |
879 | return -EINVAL; | |
880 | } | |
881 | ||
882 | static inline void | |
3f7cce3c SE |
883 | perf_cgroup_set_timestamp(struct task_struct *task, |
884 | struct perf_event_context *ctx) | |
e5d1367f SE |
885 | { |
886 | } | |
887 | ||
888 | void | |
889 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) | |
890 | { | |
891 | } | |
892 | ||
893 | static inline void | |
894 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
895 | { | |
896 | } | |
897 | ||
898 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
899 | { | |
900 | return 0; | |
901 | } | |
902 | ||
903 | static inline void | |
904 | perf_cgroup_defer_enabled(struct perf_event *event) | |
905 | { | |
906 | } | |
907 | ||
908 | static inline void | |
909 | perf_cgroup_mark_enabled(struct perf_event *event, | |
910 | struct perf_event_context *ctx) | |
911 | { | |
912 | } | |
913 | #endif | |
914 | ||
9e630205 SE |
915 | /* |
916 | * set default to be dependent on timer tick just | |
917 | * like original code | |
918 | */ | |
919 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
920 | /* | |
921 | * function must be called with interrupts disbled | |
922 | */ | |
272325c4 | 923 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
924 | { |
925 | struct perf_cpu_context *cpuctx; | |
9e630205 SE |
926 | int rotations = 0; |
927 | ||
928 | WARN_ON(!irqs_disabled()); | |
929 | ||
930 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
931 | rotations = perf_rotate_context(cpuctx); |
932 | ||
4cfafd30 PZ |
933 | raw_spin_lock(&cpuctx->hrtimer_lock); |
934 | if (rotations) | |
9e630205 | 935 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
936 | else |
937 | cpuctx->hrtimer_active = 0; | |
938 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 939 | |
4cfafd30 | 940 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
941 | } |
942 | ||
272325c4 | 943 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 944 | { |
272325c4 | 945 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 946 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 947 | u64 interval; |
9e630205 SE |
948 | |
949 | /* no multiplexing needed for SW PMU */ | |
950 | if (pmu->task_ctx_nr == perf_sw_context) | |
951 | return; | |
952 | ||
62b85639 SE |
953 | /* |
954 | * check default is sane, if not set then force to | |
955 | * default interval (1/tick) | |
956 | */ | |
272325c4 PZ |
957 | interval = pmu->hrtimer_interval_ms; |
958 | if (interval < 1) | |
959 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 960 | |
272325c4 | 961 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 962 | |
4cfafd30 PZ |
963 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
964 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); | |
272325c4 | 965 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
966 | } |
967 | ||
272325c4 | 968 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 969 | { |
272325c4 | 970 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 971 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 972 | unsigned long flags; |
9e630205 SE |
973 | |
974 | /* not for SW PMU */ | |
975 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 976 | return 0; |
9e630205 | 977 | |
4cfafd30 PZ |
978 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
979 | if (!cpuctx->hrtimer_active) { | |
980 | cpuctx->hrtimer_active = 1; | |
981 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
982 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); | |
983 | } | |
984 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 985 | |
272325c4 | 986 | return 0; |
9e630205 SE |
987 | } |
988 | ||
33696fc0 | 989 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 990 | { |
33696fc0 PZ |
991 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
992 | if (!(*count)++) | |
993 | pmu->pmu_disable(pmu); | |
9e35ad38 | 994 | } |
9e35ad38 | 995 | |
33696fc0 | 996 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 997 | { |
33696fc0 PZ |
998 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
999 | if (!--(*count)) | |
1000 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1001 | } |
9e35ad38 | 1002 | |
2fde4f94 | 1003 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1004 | |
1005 | /* | |
2fde4f94 MR |
1006 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1007 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1008 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1009 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1010 | */ |
2fde4f94 | 1011 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1012 | { |
2fde4f94 | 1013 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1014 | |
e9d2b064 | 1015 | WARN_ON(!irqs_disabled()); |
b5ab4cd5 | 1016 | |
2fde4f94 MR |
1017 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1018 | ||
1019 | list_add(&ctx->active_ctx_list, head); | |
1020 | } | |
1021 | ||
1022 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1023 | { | |
1024 | WARN_ON(!irqs_disabled()); | |
1025 | ||
1026 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1027 | ||
1028 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1029 | } |
9e35ad38 | 1030 | |
cdd6c482 | 1031 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1032 | { |
e5289d4a | 1033 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
a63eaf34 PM |
1034 | } |
1035 | ||
4af57ef2 YZ |
1036 | static void free_ctx(struct rcu_head *head) |
1037 | { | |
1038 | struct perf_event_context *ctx; | |
1039 | ||
1040 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
1041 | kfree(ctx->task_ctx_data); | |
1042 | kfree(ctx); | |
1043 | } | |
1044 | ||
cdd6c482 | 1045 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1046 | { |
564c2b21 PM |
1047 | if (atomic_dec_and_test(&ctx->refcount)) { |
1048 | if (ctx->parent_ctx) | |
1049 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1050 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1051 | put_task_struct(ctx->task); |
4af57ef2 | 1052 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1053 | } |
a63eaf34 PM |
1054 | } |
1055 | ||
f63a8daa PZ |
1056 | /* |
1057 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1058 | * perf_pmu_migrate_context() we need some magic. | |
1059 | * | |
1060 | * Those places that change perf_event::ctx will hold both | |
1061 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1062 | * | |
8b10c5e2 PZ |
1063 | * Lock ordering is by mutex address. There are two other sites where |
1064 | * perf_event_context::mutex nests and those are: | |
1065 | * | |
1066 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1067 | * perf_event_exit_event() |
1068 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1069 | * |
1070 | * - perf_event_init_context() [ parent, 0 ] | |
1071 | * inherit_task_group() | |
1072 | * inherit_group() | |
1073 | * inherit_event() | |
1074 | * perf_event_alloc() | |
1075 | * perf_init_event() | |
1076 | * perf_try_init_event() [ child , 1 ] | |
1077 | * | |
1078 | * While it appears there is an obvious deadlock here -- the parent and child | |
1079 | * nesting levels are inverted between the two. This is in fact safe because | |
1080 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1081 | * spawning task cannot (yet) exit. | |
1082 | * | |
1083 | * But remember that that these are parent<->child context relations, and | |
1084 | * migration does not affect children, therefore these two orderings should not | |
1085 | * interact. | |
f63a8daa PZ |
1086 | * |
1087 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1088 | * because the sys_perf_event_open() case will install a new event and break | |
1089 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1090 | * concerned with cpuctx and that doesn't have children. | |
1091 | * | |
1092 | * The places that change perf_event::ctx will issue: | |
1093 | * | |
1094 | * perf_remove_from_context(); | |
1095 | * synchronize_rcu(); | |
1096 | * perf_install_in_context(); | |
1097 | * | |
1098 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1099 | * quiesce the event, after which we can install it in the new location. This | |
1100 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1101 | * while in transit. Therefore all such accessors should also acquire | |
1102 | * perf_event_context::mutex to serialize against this. | |
1103 | * | |
1104 | * However; because event->ctx can change while we're waiting to acquire | |
1105 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1106 | * function. | |
1107 | * | |
1108 | * Lock order: | |
1109 | * task_struct::perf_event_mutex | |
1110 | * perf_event_context::mutex | |
f63a8daa | 1111 | * perf_event::child_mutex; |
07c4a776 | 1112 | * perf_event_context::lock |
f63a8daa PZ |
1113 | * perf_event::mmap_mutex |
1114 | * mmap_sem | |
1115 | */ | |
a83fe28e PZ |
1116 | static struct perf_event_context * |
1117 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1118 | { |
1119 | struct perf_event_context *ctx; | |
1120 | ||
1121 | again: | |
1122 | rcu_read_lock(); | |
1123 | ctx = ACCESS_ONCE(event->ctx); | |
1124 | if (!atomic_inc_not_zero(&ctx->refcount)) { | |
1125 | rcu_read_unlock(); | |
1126 | goto again; | |
1127 | } | |
1128 | rcu_read_unlock(); | |
1129 | ||
a83fe28e | 1130 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1131 | if (event->ctx != ctx) { |
1132 | mutex_unlock(&ctx->mutex); | |
1133 | put_ctx(ctx); | |
1134 | goto again; | |
1135 | } | |
1136 | ||
1137 | return ctx; | |
1138 | } | |
1139 | ||
a83fe28e PZ |
1140 | static inline struct perf_event_context * |
1141 | perf_event_ctx_lock(struct perf_event *event) | |
1142 | { | |
1143 | return perf_event_ctx_lock_nested(event, 0); | |
1144 | } | |
1145 | ||
f63a8daa PZ |
1146 | static void perf_event_ctx_unlock(struct perf_event *event, |
1147 | struct perf_event_context *ctx) | |
1148 | { | |
1149 | mutex_unlock(&ctx->mutex); | |
1150 | put_ctx(ctx); | |
1151 | } | |
1152 | ||
211de6eb PZ |
1153 | /* |
1154 | * This must be done under the ctx->lock, such as to serialize against | |
1155 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1156 | * calling scheduler related locks and ctx->lock nests inside those. | |
1157 | */ | |
1158 | static __must_check struct perf_event_context * | |
1159 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1160 | { |
211de6eb PZ |
1161 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1162 | ||
1163 | lockdep_assert_held(&ctx->lock); | |
1164 | ||
1165 | if (parent_ctx) | |
71a851b4 | 1166 | ctx->parent_ctx = NULL; |
5a3126d4 | 1167 | ctx->generation++; |
211de6eb PZ |
1168 | |
1169 | return parent_ctx; | |
71a851b4 PZ |
1170 | } |
1171 | ||
6844c09d ACM |
1172 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
1173 | { | |
1174 | /* | |
1175 | * only top level events have the pid namespace they were created in | |
1176 | */ | |
1177 | if (event->parent) | |
1178 | event = event->parent; | |
1179 | ||
1180 | return task_tgid_nr_ns(p, event->ns); | |
1181 | } | |
1182 | ||
1183 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) | |
1184 | { | |
1185 | /* | |
1186 | * only top level events have the pid namespace they were created in | |
1187 | */ | |
1188 | if (event->parent) | |
1189 | event = event->parent; | |
1190 | ||
1191 | return task_pid_nr_ns(p, event->ns); | |
1192 | } | |
1193 | ||
7f453c24 | 1194 | /* |
cdd6c482 | 1195 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1196 | * to userspace. |
1197 | */ | |
cdd6c482 | 1198 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1199 | { |
cdd6c482 | 1200 | u64 id = event->id; |
7f453c24 | 1201 | |
cdd6c482 IM |
1202 | if (event->parent) |
1203 | id = event->parent->id; | |
7f453c24 PZ |
1204 | |
1205 | return id; | |
1206 | } | |
1207 | ||
25346b93 | 1208 | /* |
cdd6c482 | 1209 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1210 | * |
25346b93 PM |
1211 | * This has to cope with with the fact that until it is locked, |
1212 | * the context could get moved to another task. | |
1213 | */ | |
cdd6c482 | 1214 | static struct perf_event_context * |
8dc85d54 | 1215 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1216 | { |
cdd6c482 | 1217 | struct perf_event_context *ctx; |
25346b93 | 1218 | |
9ed6060d | 1219 | retry: |
058ebd0e PZ |
1220 | /* |
1221 | * One of the few rules of preemptible RCU is that one cannot do | |
1222 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1223 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1224 | * rcu_read_unlock_special(). |
1225 | * | |
1226 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1227 | * side critical section has interrupts disabled. |
058ebd0e | 1228 | */ |
2fd59077 | 1229 | local_irq_save(*flags); |
058ebd0e | 1230 | rcu_read_lock(); |
8dc85d54 | 1231 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1232 | if (ctx) { |
1233 | /* | |
1234 | * If this context is a clone of another, it might | |
1235 | * get swapped for another underneath us by | |
cdd6c482 | 1236 | * perf_event_task_sched_out, though the |
25346b93 PM |
1237 | * rcu_read_lock() protects us from any context |
1238 | * getting freed. Lock the context and check if it | |
1239 | * got swapped before we could get the lock, and retry | |
1240 | * if so. If we locked the right context, then it | |
1241 | * can't get swapped on us any more. | |
1242 | */ | |
2fd59077 | 1243 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1244 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1245 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1246 | rcu_read_unlock(); |
2fd59077 | 1247 | local_irq_restore(*flags); |
25346b93 PM |
1248 | goto retry; |
1249 | } | |
b49a9e7e | 1250 | |
63b6da39 PZ |
1251 | if (ctx->task == TASK_TOMBSTONE || |
1252 | !atomic_inc_not_zero(&ctx->refcount)) { | |
2fd59077 | 1253 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1254 | ctx = NULL; |
828b6f0e PZ |
1255 | } else { |
1256 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1257 | } |
25346b93 PM |
1258 | } |
1259 | rcu_read_unlock(); | |
2fd59077 PM |
1260 | if (!ctx) |
1261 | local_irq_restore(*flags); | |
25346b93 PM |
1262 | return ctx; |
1263 | } | |
1264 | ||
1265 | /* | |
1266 | * Get the context for a task and increment its pin_count so it | |
1267 | * can't get swapped to another task. This also increments its | |
1268 | * reference count so that the context can't get freed. | |
1269 | */ | |
8dc85d54 PZ |
1270 | static struct perf_event_context * |
1271 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1272 | { |
cdd6c482 | 1273 | struct perf_event_context *ctx; |
25346b93 PM |
1274 | unsigned long flags; |
1275 | ||
8dc85d54 | 1276 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1277 | if (ctx) { |
1278 | ++ctx->pin_count; | |
e625cce1 | 1279 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1280 | } |
1281 | return ctx; | |
1282 | } | |
1283 | ||
cdd6c482 | 1284 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1285 | { |
1286 | unsigned long flags; | |
1287 | ||
e625cce1 | 1288 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1289 | --ctx->pin_count; |
e625cce1 | 1290 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1291 | } |
1292 | ||
f67218c3 PZ |
1293 | /* |
1294 | * Update the record of the current time in a context. | |
1295 | */ | |
1296 | static void update_context_time(struct perf_event_context *ctx) | |
1297 | { | |
1298 | u64 now = perf_clock(); | |
1299 | ||
1300 | ctx->time += now - ctx->timestamp; | |
1301 | ctx->timestamp = now; | |
1302 | } | |
1303 | ||
4158755d SE |
1304 | static u64 perf_event_time(struct perf_event *event) |
1305 | { | |
1306 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1307 | |
1308 | if (is_cgroup_event(event)) | |
1309 | return perf_cgroup_event_time(event); | |
1310 | ||
4158755d SE |
1311 | return ctx ? ctx->time : 0; |
1312 | } | |
1313 | ||
f67218c3 PZ |
1314 | /* |
1315 | * Update the total_time_enabled and total_time_running fields for a event. | |
1316 | */ | |
1317 | static void update_event_times(struct perf_event *event) | |
1318 | { | |
1319 | struct perf_event_context *ctx = event->ctx; | |
1320 | u64 run_end; | |
1321 | ||
3cbaa590 PZ |
1322 | lockdep_assert_held(&ctx->lock); |
1323 | ||
f67218c3 PZ |
1324 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
1325 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) | |
1326 | return; | |
3cbaa590 | 1327 | |
e5d1367f SE |
1328 | /* |
1329 | * in cgroup mode, time_enabled represents | |
1330 | * the time the event was enabled AND active | |
1331 | * tasks were in the monitored cgroup. This is | |
1332 | * independent of the activity of the context as | |
1333 | * there may be a mix of cgroup and non-cgroup events. | |
1334 | * | |
1335 | * That is why we treat cgroup events differently | |
1336 | * here. | |
1337 | */ | |
1338 | if (is_cgroup_event(event)) | |
46cd6a7f | 1339 | run_end = perf_cgroup_event_time(event); |
e5d1367f SE |
1340 | else if (ctx->is_active) |
1341 | run_end = ctx->time; | |
acd1d7c1 PZ |
1342 | else |
1343 | run_end = event->tstamp_stopped; | |
1344 | ||
1345 | event->total_time_enabled = run_end - event->tstamp_enabled; | |
f67218c3 PZ |
1346 | |
1347 | if (event->state == PERF_EVENT_STATE_INACTIVE) | |
1348 | run_end = event->tstamp_stopped; | |
1349 | else | |
4158755d | 1350 | run_end = perf_event_time(event); |
f67218c3 PZ |
1351 | |
1352 | event->total_time_running = run_end - event->tstamp_running; | |
e5d1367f | 1353 | |
f67218c3 PZ |
1354 | } |
1355 | ||
96c21a46 PZ |
1356 | /* |
1357 | * Update total_time_enabled and total_time_running for all events in a group. | |
1358 | */ | |
1359 | static void update_group_times(struct perf_event *leader) | |
1360 | { | |
1361 | struct perf_event *event; | |
1362 | ||
1363 | update_event_times(leader); | |
1364 | list_for_each_entry(event, &leader->sibling_list, group_entry) | |
1365 | update_event_times(event); | |
1366 | } | |
1367 | ||
889ff015 FW |
1368 | static struct list_head * |
1369 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) | |
1370 | { | |
1371 | if (event->attr.pinned) | |
1372 | return &ctx->pinned_groups; | |
1373 | else | |
1374 | return &ctx->flexible_groups; | |
1375 | } | |
1376 | ||
fccc714b | 1377 | /* |
cdd6c482 | 1378 | * Add a event from the lists for its context. |
fccc714b PZ |
1379 | * Must be called with ctx->mutex and ctx->lock held. |
1380 | */ | |
04289bb9 | 1381 | static void |
cdd6c482 | 1382 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1383 | { |
c994d613 PZ |
1384 | lockdep_assert_held(&ctx->lock); |
1385 | ||
8a49542c PZ |
1386 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1387 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 IM |
1388 | |
1389 | /* | |
8a49542c PZ |
1390 | * If we're a stand alone event or group leader, we go to the context |
1391 | * list, group events are kept attached to the group so that | |
1392 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1393 | */ |
8a49542c | 1394 | if (event->group_leader == event) { |
889ff015 FW |
1395 | struct list_head *list; |
1396 | ||
d6f962b5 FW |
1397 | if (is_software_event(event)) |
1398 | event->group_flags |= PERF_GROUP_SOFTWARE; | |
1399 | ||
889ff015 FW |
1400 | list = ctx_group_list(event, ctx); |
1401 | list_add_tail(&event->group_entry, list); | |
5c148194 | 1402 | } |
592903cd | 1403 | |
08309379 | 1404 | if (is_cgroup_event(event)) |
e5d1367f | 1405 | ctx->nr_cgroups++; |
e5d1367f | 1406 | |
cdd6c482 IM |
1407 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1408 | ctx->nr_events++; | |
1409 | if (event->attr.inherit_stat) | |
bfbd3381 | 1410 | ctx->nr_stat++; |
5a3126d4 PZ |
1411 | |
1412 | ctx->generation++; | |
04289bb9 IM |
1413 | } |
1414 | ||
0231bb53 JO |
1415 | /* |
1416 | * Initialize event state based on the perf_event_attr::disabled. | |
1417 | */ | |
1418 | static inline void perf_event__state_init(struct perf_event *event) | |
1419 | { | |
1420 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1421 | PERF_EVENT_STATE_INACTIVE; | |
1422 | } | |
1423 | ||
a723968c | 1424 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1425 | { |
1426 | int entry = sizeof(u64); /* value */ | |
1427 | int size = 0; | |
1428 | int nr = 1; | |
1429 | ||
1430 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1431 | size += sizeof(u64); | |
1432 | ||
1433 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1434 | size += sizeof(u64); | |
1435 | ||
1436 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1437 | entry += sizeof(u64); | |
1438 | ||
1439 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1440 | nr += nr_siblings; |
c320c7b7 ACM |
1441 | size += sizeof(u64); |
1442 | } | |
1443 | ||
1444 | size += entry * nr; | |
1445 | event->read_size = size; | |
1446 | } | |
1447 | ||
a723968c | 1448 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1449 | { |
1450 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1451 | u16 size = 0; |
1452 | ||
c320c7b7 ACM |
1453 | if (sample_type & PERF_SAMPLE_IP) |
1454 | size += sizeof(data->ip); | |
1455 | ||
6844c09d ACM |
1456 | if (sample_type & PERF_SAMPLE_ADDR) |
1457 | size += sizeof(data->addr); | |
1458 | ||
1459 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1460 | size += sizeof(data->period); | |
1461 | ||
c3feedf2 AK |
1462 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1463 | size += sizeof(data->weight); | |
1464 | ||
6844c09d ACM |
1465 | if (sample_type & PERF_SAMPLE_READ) |
1466 | size += event->read_size; | |
1467 | ||
d6be9ad6 SE |
1468 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1469 | size += sizeof(data->data_src.val); | |
1470 | ||
fdfbbd07 AK |
1471 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1472 | size += sizeof(data->txn); | |
1473 | ||
6844c09d ACM |
1474 | event->header_size = size; |
1475 | } | |
1476 | ||
a723968c PZ |
1477 | /* |
1478 | * Called at perf_event creation and when events are attached/detached from a | |
1479 | * group. | |
1480 | */ | |
1481 | static void perf_event__header_size(struct perf_event *event) | |
1482 | { | |
1483 | __perf_event_read_size(event, | |
1484 | event->group_leader->nr_siblings); | |
1485 | __perf_event_header_size(event, event->attr.sample_type); | |
1486 | } | |
1487 | ||
6844c09d ACM |
1488 | static void perf_event__id_header_size(struct perf_event *event) |
1489 | { | |
1490 | struct perf_sample_data *data; | |
1491 | u64 sample_type = event->attr.sample_type; | |
1492 | u16 size = 0; | |
1493 | ||
c320c7b7 ACM |
1494 | if (sample_type & PERF_SAMPLE_TID) |
1495 | size += sizeof(data->tid_entry); | |
1496 | ||
1497 | if (sample_type & PERF_SAMPLE_TIME) | |
1498 | size += sizeof(data->time); | |
1499 | ||
ff3d527c AH |
1500 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1501 | size += sizeof(data->id); | |
1502 | ||
c320c7b7 ACM |
1503 | if (sample_type & PERF_SAMPLE_ID) |
1504 | size += sizeof(data->id); | |
1505 | ||
1506 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1507 | size += sizeof(data->stream_id); | |
1508 | ||
1509 | if (sample_type & PERF_SAMPLE_CPU) | |
1510 | size += sizeof(data->cpu_entry); | |
1511 | ||
6844c09d | 1512 | event->id_header_size = size; |
c320c7b7 ACM |
1513 | } |
1514 | ||
a723968c PZ |
1515 | static bool perf_event_validate_size(struct perf_event *event) |
1516 | { | |
1517 | /* | |
1518 | * The values computed here will be over-written when we actually | |
1519 | * attach the event. | |
1520 | */ | |
1521 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1522 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1523 | perf_event__id_header_size(event); | |
1524 | ||
1525 | /* | |
1526 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1527 | * Conservative limit to allow for callchains and other variable fields. | |
1528 | */ | |
1529 | if (event->read_size + event->header_size + | |
1530 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1531 | return false; | |
1532 | ||
1533 | return true; | |
1534 | } | |
1535 | ||
8a49542c PZ |
1536 | static void perf_group_attach(struct perf_event *event) |
1537 | { | |
c320c7b7 | 1538 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1539 | |
74c3337c PZ |
1540 | /* |
1541 | * We can have double attach due to group movement in perf_event_open. | |
1542 | */ | |
1543 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1544 | return; | |
1545 | ||
8a49542c PZ |
1546 | event->attach_state |= PERF_ATTACH_GROUP; |
1547 | ||
1548 | if (group_leader == event) | |
1549 | return; | |
1550 | ||
652884fe PZ |
1551 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1552 | ||
8a49542c PZ |
1553 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
1554 | !is_software_event(event)) | |
1555 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; | |
1556 | ||
1557 | list_add_tail(&event->group_entry, &group_leader->sibling_list); | |
1558 | group_leader->nr_siblings++; | |
c320c7b7 ACM |
1559 | |
1560 | perf_event__header_size(group_leader); | |
1561 | ||
1562 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) | |
1563 | perf_event__header_size(pos); | |
8a49542c PZ |
1564 | } |
1565 | ||
a63eaf34 | 1566 | /* |
cdd6c482 | 1567 | * Remove a event from the lists for its context. |
fccc714b | 1568 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1569 | */ |
04289bb9 | 1570 | static void |
cdd6c482 | 1571 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1572 | { |
68cacd29 | 1573 | struct perf_cpu_context *cpuctx; |
652884fe PZ |
1574 | |
1575 | WARN_ON_ONCE(event->ctx != ctx); | |
1576 | lockdep_assert_held(&ctx->lock); | |
1577 | ||
8a49542c PZ |
1578 | /* |
1579 | * We can have double detach due to exit/hot-unplug + close. | |
1580 | */ | |
1581 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 1582 | return; |
8a49542c PZ |
1583 | |
1584 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
1585 | ||
68cacd29 | 1586 | if (is_cgroup_event(event)) { |
e5d1367f | 1587 | ctx->nr_cgroups--; |
70a01657 PZ |
1588 | /* |
1589 | * Because cgroup events are always per-cpu events, this will | |
1590 | * always be called from the right CPU. | |
1591 | */ | |
68cacd29 SE |
1592 | cpuctx = __get_cpu_context(ctx); |
1593 | /* | |
70a01657 PZ |
1594 | * If there are no more cgroup events then clear cgrp to avoid |
1595 | * stale pointer in update_cgrp_time_from_cpuctx(). | |
68cacd29 SE |
1596 | */ |
1597 | if (!ctx->nr_cgroups) | |
1598 | cpuctx->cgrp = NULL; | |
1599 | } | |
e5d1367f | 1600 | |
cdd6c482 IM |
1601 | ctx->nr_events--; |
1602 | if (event->attr.inherit_stat) | |
bfbd3381 | 1603 | ctx->nr_stat--; |
8bc20959 | 1604 | |
cdd6c482 | 1605 | list_del_rcu(&event->event_entry); |
04289bb9 | 1606 | |
8a49542c PZ |
1607 | if (event->group_leader == event) |
1608 | list_del_init(&event->group_entry); | |
5c148194 | 1609 | |
96c21a46 | 1610 | update_group_times(event); |
b2e74a26 SE |
1611 | |
1612 | /* | |
1613 | * If event was in error state, then keep it | |
1614 | * that way, otherwise bogus counts will be | |
1615 | * returned on read(). The only way to get out | |
1616 | * of error state is by explicit re-enabling | |
1617 | * of the event | |
1618 | */ | |
1619 | if (event->state > PERF_EVENT_STATE_OFF) | |
1620 | event->state = PERF_EVENT_STATE_OFF; | |
5a3126d4 PZ |
1621 | |
1622 | ctx->generation++; | |
050735b0 PZ |
1623 | } |
1624 | ||
8a49542c | 1625 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
1626 | { |
1627 | struct perf_event *sibling, *tmp; | |
8a49542c PZ |
1628 | struct list_head *list = NULL; |
1629 | ||
1630 | /* | |
1631 | * We can have double detach due to exit/hot-unplug + close. | |
1632 | */ | |
1633 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
1634 | return; | |
1635 | ||
1636 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
1637 | ||
1638 | /* | |
1639 | * If this is a sibling, remove it from its group. | |
1640 | */ | |
1641 | if (event->group_leader != event) { | |
1642 | list_del_init(&event->group_entry); | |
1643 | event->group_leader->nr_siblings--; | |
c320c7b7 | 1644 | goto out; |
8a49542c PZ |
1645 | } |
1646 | ||
1647 | if (!list_empty(&event->group_entry)) | |
1648 | list = &event->group_entry; | |
2e2af50b | 1649 | |
04289bb9 | 1650 | /* |
cdd6c482 IM |
1651 | * If this was a group event with sibling events then |
1652 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 1653 | * to whatever list we are on. |
04289bb9 | 1654 | */ |
cdd6c482 | 1655 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
8a49542c PZ |
1656 | if (list) |
1657 | list_move_tail(&sibling->group_entry, list); | |
04289bb9 | 1658 | sibling->group_leader = sibling; |
d6f962b5 FW |
1659 | |
1660 | /* Inherit group flags from the previous leader */ | |
1661 | sibling->group_flags = event->group_flags; | |
652884fe PZ |
1662 | |
1663 | WARN_ON_ONCE(sibling->ctx != event->ctx); | |
04289bb9 | 1664 | } |
c320c7b7 ACM |
1665 | |
1666 | out: | |
1667 | perf_event__header_size(event->group_leader); | |
1668 | ||
1669 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) | |
1670 | perf_event__header_size(tmp); | |
04289bb9 IM |
1671 | } |
1672 | ||
fadfe7be JO |
1673 | static bool is_orphaned_event(struct perf_event *event) |
1674 | { | |
a69b0ca4 | 1675 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
1676 | } |
1677 | ||
66eb579e MR |
1678 | static inline int pmu_filter_match(struct perf_event *event) |
1679 | { | |
1680 | struct pmu *pmu = event->pmu; | |
1681 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
1682 | } | |
1683 | ||
fa66f07a SE |
1684 | static inline int |
1685 | event_filter_match(struct perf_event *event) | |
1686 | { | |
e5d1367f | 1687 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
66eb579e | 1688 | && perf_cgroup_match(event) && pmu_filter_match(event); |
fa66f07a SE |
1689 | } |
1690 | ||
9ffcfa6f SE |
1691 | static void |
1692 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 1693 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1694 | struct perf_event_context *ctx) |
3b6f9e5c | 1695 | { |
4158755d | 1696 | u64 tstamp = perf_event_time(event); |
fa66f07a | 1697 | u64 delta; |
652884fe PZ |
1698 | |
1699 | WARN_ON_ONCE(event->ctx != ctx); | |
1700 | lockdep_assert_held(&ctx->lock); | |
1701 | ||
fa66f07a SE |
1702 | /* |
1703 | * An event which could not be activated because of | |
1704 | * filter mismatch still needs to have its timings | |
1705 | * maintained, otherwise bogus information is return | |
1706 | * via read() for time_enabled, time_running: | |
1707 | */ | |
1708 | if (event->state == PERF_EVENT_STATE_INACTIVE | |
1709 | && !event_filter_match(event)) { | |
e5d1367f | 1710 | delta = tstamp - event->tstamp_stopped; |
fa66f07a | 1711 | event->tstamp_running += delta; |
4158755d | 1712 | event->tstamp_stopped = tstamp; |
fa66f07a SE |
1713 | } |
1714 | ||
cdd6c482 | 1715 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 1716 | return; |
3b6f9e5c | 1717 | |
44377277 AS |
1718 | perf_pmu_disable(event->pmu); |
1719 | ||
28a967c3 PZ |
1720 | event->tstamp_stopped = tstamp; |
1721 | event->pmu->del(event, 0); | |
1722 | event->oncpu = -1; | |
cdd6c482 IM |
1723 | event->state = PERF_EVENT_STATE_INACTIVE; |
1724 | if (event->pending_disable) { | |
1725 | event->pending_disable = 0; | |
1726 | event->state = PERF_EVENT_STATE_OFF; | |
970892a9 | 1727 | } |
3b6f9e5c | 1728 | |
cdd6c482 | 1729 | if (!is_software_event(event)) |
3b6f9e5c | 1730 | cpuctx->active_oncpu--; |
2fde4f94 MR |
1731 | if (!--ctx->nr_active) |
1732 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
1733 | if (event->attr.freq && event->attr.sample_freq) |
1734 | ctx->nr_freq--; | |
cdd6c482 | 1735 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 1736 | cpuctx->exclusive = 0; |
44377277 AS |
1737 | |
1738 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
1739 | } |
1740 | ||
d859e29f | 1741 | static void |
cdd6c482 | 1742 | group_sched_out(struct perf_event *group_event, |
d859e29f | 1743 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 1744 | struct perf_event_context *ctx) |
d859e29f | 1745 | { |
cdd6c482 | 1746 | struct perf_event *event; |
fa66f07a | 1747 | int state = group_event->state; |
d859e29f | 1748 | |
cdd6c482 | 1749 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
1750 | |
1751 | /* | |
1752 | * Schedule out siblings (if any): | |
1753 | */ | |
cdd6c482 IM |
1754 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
1755 | event_sched_out(event, cpuctx, ctx); | |
d859e29f | 1756 | |
fa66f07a | 1757 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
d859e29f PM |
1758 | cpuctx->exclusive = 0; |
1759 | } | |
1760 | ||
45a0e07a | 1761 | #define DETACH_GROUP 0x01UL |
0017960f | 1762 | |
0793a61d | 1763 | /* |
cdd6c482 | 1764 | * Cross CPU call to remove a performance event |
0793a61d | 1765 | * |
cdd6c482 | 1766 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
1767 | * remove it from the context list. |
1768 | */ | |
fae3fde6 PZ |
1769 | static void |
1770 | __perf_remove_from_context(struct perf_event *event, | |
1771 | struct perf_cpu_context *cpuctx, | |
1772 | struct perf_event_context *ctx, | |
1773 | void *info) | |
0793a61d | 1774 | { |
45a0e07a | 1775 | unsigned long flags = (unsigned long)info; |
0793a61d | 1776 | |
cdd6c482 | 1777 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 1778 | if (flags & DETACH_GROUP) |
46ce0fe9 | 1779 | perf_group_detach(event); |
cdd6c482 | 1780 | list_del_event(event, ctx); |
39a43640 PZ |
1781 | |
1782 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 1783 | ctx->is_active = 0; |
39a43640 PZ |
1784 | if (ctx->task) { |
1785 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
1786 | cpuctx->task_ctx = NULL; | |
1787 | } | |
64ce3126 | 1788 | } |
0793a61d TG |
1789 | } |
1790 | ||
0793a61d | 1791 | /* |
cdd6c482 | 1792 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 1793 | * |
cdd6c482 IM |
1794 | * If event->ctx is a cloned context, callers must make sure that |
1795 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
1796 | * remains valid. This is OK when called from perf_release since |
1797 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 1798 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 1799 | * context has been detached from its task. |
0793a61d | 1800 | */ |
45a0e07a | 1801 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 1802 | { |
fae3fde6 | 1803 | lockdep_assert_held(&event->ctx->mutex); |
0793a61d | 1804 | |
45a0e07a | 1805 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
0793a61d TG |
1806 | } |
1807 | ||
d859e29f | 1808 | /* |
cdd6c482 | 1809 | * Cross CPU call to disable a performance event |
d859e29f | 1810 | */ |
fae3fde6 PZ |
1811 | static void __perf_event_disable(struct perf_event *event, |
1812 | struct perf_cpu_context *cpuctx, | |
1813 | struct perf_event_context *ctx, | |
1814 | void *info) | |
7b648018 | 1815 | { |
fae3fde6 PZ |
1816 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
1817 | return; | |
7b648018 | 1818 | |
fae3fde6 PZ |
1819 | update_context_time(ctx); |
1820 | update_cgrp_time_from_event(event); | |
1821 | update_group_times(event); | |
1822 | if (event == event->group_leader) | |
1823 | group_sched_out(event, cpuctx, ctx); | |
1824 | else | |
1825 | event_sched_out(event, cpuctx, ctx); | |
1826 | event->state = PERF_EVENT_STATE_OFF; | |
7b648018 PZ |
1827 | } |
1828 | ||
d859e29f | 1829 | /* |
cdd6c482 | 1830 | * Disable a event. |
c93f7669 | 1831 | * |
cdd6c482 IM |
1832 | * If event->ctx is a cloned context, callers must make sure that |
1833 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 1834 | * remains valid. This condition is satisifed when called through |
cdd6c482 IM |
1835 | * perf_event_for_each_child or perf_event_for_each because they |
1836 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
1837 | * goes to exit will block in perf_event_exit_event(). |
1838 | * | |
cdd6c482 | 1839 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 1840 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 1841 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 1842 | */ |
f63a8daa | 1843 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 1844 | { |
cdd6c482 | 1845 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 1846 | |
e625cce1 | 1847 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 1848 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 1849 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1850 | return; |
53cfbf59 | 1851 | } |
e625cce1 | 1852 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 1853 | |
fae3fde6 PZ |
1854 | event_function_call(event, __perf_event_disable, NULL); |
1855 | } | |
1856 | ||
1857 | void perf_event_disable_local(struct perf_event *event) | |
1858 | { | |
1859 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 1860 | } |
f63a8daa PZ |
1861 | |
1862 | /* | |
1863 | * Strictly speaking kernel users cannot create groups and therefore this | |
1864 | * interface does not need the perf_event_ctx_lock() magic. | |
1865 | */ | |
1866 | void perf_event_disable(struct perf_event *event) | |
1867 | { | |
1868 | struct perf_event_context *ctx; | |
1869 | ||
1870 | ctx = perf_event_ctx_lock(event); | |
1871 | _perf_event_disable(event); | |
1872 | perf_event_ctx_unlock(event, ctx); | |
1873 | } | |
dcfce4a0 | 1874 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 1875 | |
e5d1367f SE |
1876 | static void perf_set_shadow_time(struct perf_event *event, |
1877 | struct perf_event_context *ctx, | |
1878 | u64 tstamp) | |
1879 | { | |
1880 | /* | |
1881 | * use the correct time source for the time snapshot | |
1882 | * | |
1883 | * We could get by without this by leveraging the | |
1884 | * fact that to get to this function, the caller | |
1885 | * has most likely already called update_context_time() | |
1886 | * and update_cgrp_time_xx() and thus both timestamp | |
1887 | * are identical (or very close). Given that tstamp is, | |
1888 | * already adjusted for cgroup, we could say that: | |
1889 | * tstamp - ctx->timestamp | |
1890 | * is equivalent to | |
1891 | * tstamp - cgrp->timestamp. | |
1892 | * | |
1893 | * Then, in perf_output_read(), the calculation would | |
1894 | * work with no changes because: | |
1895 | * - event is guaranteed scheduled in | |
1896 | * - no scheduled out in between | |
1897 | * - thus the timestamp would be the same | |
1898 | * | |
1899 | * But this is a bit hairy. | |
1900 | * | |
1901 | * So instead, we have an explicit cgroup call to remain | |
1902 | * within the time time source all along. We believe it | |
1903 | * is cleaner and simpler to understand. | |
1904 | */ | |
1905 | if (is_cgroup_event(event)) | |
1906 | perf_cgroup_set_shadow_time(event, tstamp); | |
1907 | else | |
1908 | event->shadow_ctx_time = tstamp - ctx->timestamp; | |
1909 | } | |
1910 | ||
4fe757dd PZ |
1911 | #define MAX_INTERRUPTS (~0ULL) |
1912 | ||
1913 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 1914 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 1915 | |
235c7fc7 | 1916 | static int |
9ffcfa6f | 1917 | event_sched_in(struct perf_event *event, |
235c7fc7 | 1918 | struct perf_cpu_context *cpuctx, |
6e37738a | 1919 | struct perf_event_context *ctx) |
235c7fc7 | 1920 | { |
4158755d | 1921 | u64 tstamp = perf_event_time(event); |
44377277 | 1922 | int ret = 0; |
4158755d | 1923 | |
63342411 PZ |
1924 | lockdep_assert_held(&ctx->lock); |
1925 | ||
cdd6c482 | 1926 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
1927 | return 0; |
1928 | ||
95ff4ca2 AS |
1929 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
1930 | /* | |
1931 | * Order event::oncpu write to happen before the ACTIVE state | |
1932 | * is visible. | |
1933 | */ | |
1934 | smp_wmb(); | |
1935 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); | |
4fe757dd PZ |
1936 | |
1937 | /* | |
1938 | * Unthrottle events, since we scheduled we might have missed several | |
1939 | * ticks already, also for a heavily scheduling task there is little | |
1940 | * guarantee it'll get a tick in a timely manner. | |
1941 | */ | |
1942 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
1943 | perf_log_throttle(event, 1); | |
1944 | event->hw.interrupts = 0; | |
1945 | } | |
1946 | ||
235c7fc7 IM |
1947 | /* |
1948 | * The new state must be visible before we turn it on in the hardware: | |
1949 | */ | |
1950 | smp_wmb(); | |
1951 | ||
44377277 AS |
1952 | perf_pmu_disable(event->pmu); |
1953 | ||
72f669c0 SL |
1954 | perf_set_shadow_time(event, ctx, tstamp); |
1955 | ||
ec0d7729 AS |
1956 | perf_log_itrace_start(event); |
1957 | ||
a4eaf7f1 | 1958 | if (event->pmu->add(event, PERF_EF_START)) { |
cdd6c482 IM |
1959 | event->state = PERF_EVENT_STATE_INACTIVE; |
1960 | event->oncpu = -1; | |
44377277 AS |
1961 | ret = -EAGAIN; |
1962 | goto out; | |
235c7fc7 IM |
1963 | } |
1964 | ||
00a2916f PZ |
1965 | event->tstamp_running += tstamp - event->tstamp_stopped; |
1966 | ||
cdd6c482 | 1967 | if (!is_software_event(event)) |
3b6f9e5c | 1968 | cpuctx->active_oncpu++; |
2fde4f94 MR |
1969 | if (!ctx->nr_active++) |
1970 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
1971 | if (event->attr.freq && event->attr.sample_freq) |
1972 | ctx->nr_freq++; | |
235c7fc7 | 1973 | |
cdd6c482 | 1974 | if (event->attr.exclusive) |
3b6f9e5c PM |
1975 | cpuctx->exclusive = 1; |
1976 | ||
44377277 AS |
1977 | out: |
1978 | perf_pmu_enable(event->pmu); | |
1979 | ||
1980 | return ret; | |
235c7fc7 IM |
1981 | } |
1982 | ||
6751b71e | 1983 | static int |
cdd6c482 | 1984 | group_sched_in(struct perf_event *group_event, |
6751b71e | 1985 | struct perf_cpu_context *cpuctx, |
6e37738a | 1986 | struct perf_event_context *ctx) |
6751b71e | 1987 | { |
6bde9b6c | 1988 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 1989 | struct pmu *pmu = ctx->pmu; |
d7842da4 SE |
1990 | u64 now = ctx->time; |
1991 | bool simulate = false; | |
6751b71e | 1992 | |
cdd6c482 | 1993 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
1994 | return 0; |
1995 | ||
fbbe0701 | 1996 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 1997 | |
9ffcfa6f | 1998 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 1999 | pmu->cancel_txn(pmu); |
272325c4 | 2000 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2001 | return -EAGAIN; |
90151c35 | 2002 | } |
6751b71e PM |
2003 | |
2004 | /* | |
2005 | * Schedule in siblings as one group (if any): | |
2006 | */ | |
cdd6c482 | 2007 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
9ffcfa6f | 2008 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2009 | partial_group = event; |
6751b71e PM |
2010 | goto group_error; |
2011 | } | |
2012 | } | |
2013 | ||
9ffcfa6f | 2014 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2015 | return 0; |
9ffcfa6f | 2016 | |
6751b71e PM |
2017 | group_error: |
2018 | /* | |
2019 | * Groups can be scheduled in as one unit only, so undo any | |
2020 | * partial group before returning: | |
d7842da4 SE |
2021 | * The events up to the failed event are scheduled out normally, |
2022 | * tstamp_stopped will be updated. | |
2023 | * | |
2024 | * The failed events and the remaining siblings need to have | |
2025 | * their timings updated as if they had gone thru event_sched_in() | |
2026 | * and event_sched_out(). This is required to get consistent timings | |
2027 | * across the group. This also takes care of the case where the group | |
2028 | * could never be scheduled by ensuring tstamp_stopped is set to mark | |
2029 | * the time the event was actually stopped, such that time delta | |
2030 | * calculation in update_event_times() is correct. | |
6751b71e | 2031 | */ |
cdd6c482 IM |
2032 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
2033 | if (event == partial_group) | |
d7842da4 SE |
2034 | simulate = true; |
2035 | ||
2036 | if (simulate) { | |
2037 | event->tstamp_running += now - event->tstamp_stopped; | |
2038 | event->tstamp_stopped = now; | |
2039 | } else { | |
2040 | event_sched_out(event, cpuctx, ctx); | |
2041 | } | |
6751b71e | 2042 | } |
9ffcfa6f | 2043 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2044 | |
ad5133b7 | 2045 | pmu->cancel_txn(pmu); |
90151c35 | 2046 | |
272325c4 | 2047 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2048 | |
6751b71e PM |
2049 | return -EAGAIN; |
2050 | } | |
2051 | ||
3b6f9e5c | 2052 | /* |
cdd6c482 | 2053 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2054 | */ |
cdd6c482 | 2055 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2056 | struct perf_cpu_context *cpuctx, |
2057 | int can_add_hw) | |
2058 | { | |
2059 | /* | |
cdd6c482 | 2060 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2061 | */ |
d6f962b5 | 2062 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
3b6f9e5c PM |
2063 | return 1; |
2064 | /* | |
2065 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2066 | * events can go on. |
3b6f9e5c PM |
2067 | */ |
2068 | if (cpuctx->exclusive) | |
2069 | return 0; | |
2070 | /* | |
2071 | * If this group is exclusive and there are already | |
cdd6c482 | 2072 | * events on the CPU, it can't go on. |
3b6f9e5c | 2073 | */ |
cdd6c482 | 2074 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2075 | return 0; |
2076 | /* | |
2077 | * Otherwise, try to add it if all previous groups were able | |
2078 | * to go on. | |
2079 | */ | |
2080 | return can_add_hw; | |
2081 | } | |
2082 | ||
cdd6c482 IM |
2083 | static void add_event_to_ctx(struct perf_event *event, |
2084 | struct perf_event_context *ctx) | |
53cfbf59 | 2085 | { |
4158755d SE |
2086 | u64 tstamp = perf_event_time(event); |
2087 | ||
cdd6c482 | 2088 | list_add_event(event, ctx); |
8a49542c | 2089 | perf_group_attach(event); |
4158755d SE |
2090 | event->tstamp_enabled = tstamp; |
2091 | event->tstamp_running = tstamp; | |
2092 | event->tstamp_stopped = tstamp; | |
53cfbf59 PM |
2093 | } |
2094 | ||
bd2afa49 PZ |
2095 | static void ctx_sched_out(struct perf_event_context *ctx, |
2096 | struct perf_cpu_context *cpuctx, | |
2097 | enum event_type_t event_type); | |
2c29ef0f PZ |
2098 | static void |
2099 | ctx_sched_in(struct perf_event_context *ctx, | |
2100 | struct perf_cpu_context *cpuctx, | |
2101 | enum event_type_t event_type, | |
2102 | struct task_struct *task); | |
fe4b04fa | 2103 | |
bd2afa49 PZ |
2104 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
2105 | struct perf_event_context *ctx) | |
2106 | { | |
2107 | if (!cpuctx->task_ctx) | |
2108 | return; | |
2109 | ||
2110 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2111 | return; | |
2112 | ||
2113 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); | |
2114 | } | |
2115 | ||
dce5855b PZ |
2116 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2117 | struct perf_event_context *ctx, | |
2118 | struct task_struct *task) | |
2119 | { | |
2120 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2121 | if (ctx) | |
2122 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2123 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2124 | if (ctx) | |
2125 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2126 | } | |
2127 | ||
3e349507 PZ |
2128 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
2129 | struct perf_event_context *task_ctx) | |
0017960f | 2130 | { |
3e349507 PZ |
2131 | perf_pmu_disable(cpuctx->ctx.pmu); |
2132 | if (task_ctx) | |
2133 | task_ctx_sched_out(cpuctx, task_ctx); | |
2134 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
2135 | perf_event_sched_in(cpuctx, task_ctx, current); | |
2136 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2137 | } |
2138 | ||
0793a61d | 2139 | /* |
cdd6c482 | 2140 | * Cross CPU call to install and enable a performance event |
682076ae | 2141 | * |
a096309b PZ |
2142 | * Very similar to remote_function() + event_function() but cannot assume that |
2143 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2144 | */ |
fe4b04fa | 2145 | static int __perf_install_in_context(void *info) |
0793a61d | 2146 | { |
a096309b PZ |
2147 | struct perf_event *event = info; |
2148 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2149 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2150 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
a096309b PZ |
2151 | bool activate = true; |
2152 | int ret = 0; | |
0793a61d | 2153 | |
63b6da39 | 2154 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2155 | if (ctx->task) { |
b58f6b0d PZ |
2156 | raw_spin_lock(&ctx->lock); |
2157 | task_ctx = ctx; | |
a096309b PZ |
2158 | |
2159 | /* If we're on the wrong CPU, try again */ | |
2160 | if (task_cpu(ctx->task) != smp_processor_id()) { | |
2161 | ret = -ESRCH; | |
63b6da39 | 2162 | goto unlock; |
a096309b | 2163 | } |
b58f6b0d | 2164 | |
39a43640 | 2165 | /* |
a096309b PZ |
2166 | * If we're on the right CPU, see if the task we target is |
2167 | * current, if not we don't have to activate the ctx, a future | |
2168 | * context switch will do that for us. | |
39a43640 | 2169 | */ |
a096309b PZ |
2170 | if (ctx->task != current) |
2171 | activate = false; | |
2172 | else | |
2173 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); | |
2174 | ||
63b6da39 PZ |
2175 | } else if (task_ctx) { |
2176 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2177 | } |
b58f6b0d | 2178 | |
a096309b PZ |
2179 | if (activate) { |
2180 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2181 | add_event_to_ctx(event, ctx); | |
2182 | ctx_resched(cpuctx, task_ctx); | |
2183 | } else { | |
2184 | add_event_to_ctx(event, ctx); | |
2185 | } | |
2186 | ||
63b6da39 | 2187 | unlock: |
2c29ef0f | 2188 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2189 | |
a096309b | 2190 | return ret; |
0793a61d TG |
2191 | } |
2192 | ||
2193 | /* | |
a096309b PZ |
2194 | * Attach a performance event to a context. |
2195 | * | |
2196 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2197 | */ |
2198 | static void | |
cdd6c482 IM |
2199 | perf_install_in_context(struct perf_event_context *ctx, |
2200 | struct perf_event *event, | |
0793a61d TG |
2201 | int cpu) |
2202 | { | |
a096309b | 2203 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2204 | |
fe4b04fa PZ |
2205 | lockdep_assert_held(&ctx->mutex); |
2206 | ||
c3f00c70 | 2207 | event->ctx = ctx; |
0cda4c02 YZ |
2208 | if (event->cpu != -1) |
2209 | event->cpu = cpu; | |
c3f00c70 | 2210 | |
a096309b PZ |
2211 | if (!task) { |
2212 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2213 | return; | |
2214 | } | |
2215 | ||
2216 | /* | |
2217 | * Should not happen, we validate the ctx is still alive before calling. | |
2218 | */ | |
2219 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2220 | return; | |
2221 | ||
39a43640 PZ |
2222 | /* |
2223 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2224 | * to be set in case this is the nr_events 0 -> 1 transition. | |
39a43640 | 2225 | */ |
a096309b | 2226 | again: |
63b6da39 | 2227 | /* |
a096309b PZ |
2228 | * Cannot use task_function_call() because we need to run on the task's |
2229 | * CPU regardless of whether its current or not. | |
63b6da39 | 2230 | */ |
a096309b PZ |
2231 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
2232 | return; | |
2233 | ||
2234 | raw_spin_lock_irq(&ctx->lock); | |
2235 | task = ctx->task; | |
84c4e620 | 2236 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2237 | /* |
2238 | * Cannot happen because we already checked above (which also | |
2239 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2240 | * against perf_event_exit_task_context(). | |
2241 | */ | |
63b6da39 PZ |
2242 | raw_spin_unlock_irq(&ctx->lock); |
2243 | return; | |
2244 | } | |
39a43640 | 2245 | raw_spin_unlock_irq(&ctx->lock); |
39a43640 | 2246 | /* |
a096309b PZ |
2247 | * Since !ctx->is_active doesn't mean anything, we must IPI |
2248 | * unconditionally. | |
39a43640 | 2249 | */ |
a096309b | 2250 | goto again; |
0793a61d TG |
2251 | } |
2252 | ||
fa289bec | 2253 | /* |
cdd6c482 | 2254 | * Put a event into inactive state and update time fields. |
fa289bec PM |
2255 | * Enabling the leader of a group effectively enables all |
2256 | * the group members that aren't explicitly disabled, so we | |
2257 | * have to update their ->tstamp_enabled also. | |
2258 | * Note: this works for group members as well as group leaders | |
2259 | * since the non-leader members' sibling_lists will be empty. | |
2260 | */ | |
1d9b482e | 2261 | static void __perf_event_mark_enabled(struct perf_event *event) |
fa289bec | 2262 | { |
cdd6c482 | 2263 | struct perf_event *sub; |
4158755d | 2264 | u64 tstamp = perf_event_time(event); |
fa289bec | 2265 | |
cdd6c482 | 2266 | event->state = PERF_EVENT_STATE_INACTIVE; |
4158755d | 2267 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
9ed6060d | 2268 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
4158755d SE |
2269 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
2270 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; | |
9ed6060d | 2271 | } |
fa289bec PM |
2272 | } |
2273 | ||
d859e29f | 2274 | /* |
cdd6c482 | 2275 | * Cross CPU call to enable a performance event |
d859e29f | 2276 | */ |
fae3fde6 PZ |
2277 | static void __perf_event_enable(struct perf_event *event, |
2278 | struct perf_cpu_context *cpuctx, | |
2279 | struct perf_event_context *ctx, | |
2280 | void *info) | |
04289bb9 | 2281 | { |
cdd6c482 | 2282 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2283 | struct perf_event_context *task_ctx; |
04289bb9 | 2284 | |
6e801e01 PZ |
2285 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2286 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2287 | return; |
3cbed429 | 2288 | |
bd2afa49 PZ |
2289 | if (ctx->is_active) |
2290 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2291 | ||
1d9b482e | 2292 | __perf_event_mark_enabled(event); |
04289bb9 | 2293 | |
fae3fde6 PZ |
2294 | if (!ctx->is_active) |
2295 | return; | |
2296 | ||
e5d1367f | 2297 | if (!event_filter_match(event)) { |
bd2afa49 | 2298 | if (is_cgroup_event(event)) |
e5d1367f | 2299 | perf_cgroup_defer_enabled(event); |
bd2afa49 | 2300 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2301 | return; |
e5d1367f | 2302 | } |
f4c4176f | 2303 | |
04289bb9 | 2304 | /* |
cdd6c482 | 2305 | * If the event is in a group and isn't the group leader, |
d859e29f | 2306 | * then don't put it on unless the group is on. |
04289bb9 | 2307 | */ |
bd2afa49 PZ |
2308 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2309 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2310 | return; |
bd2afa49 | 2311 | } |
fe4b04fa | 2312 | |
fae3fde6 PZ |
2313 | task_ctx = cpuctx->task_ctx; |
2314 | if (ctx->task) | |
2315 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2316 | |
fae3fde6 | 2317 | ctx_resched(cpuctx, task_ctx); |
7b648018 PZ |
2318 | } |
2319 | ||
d859e29f | 2320 | /* |
cdd6c482 | 2321 | * Enable a event. |
c93f7669 | 2322 | * |
cdd6c482 IM |
2323 | * If event->ctx is a cloned context, callers must make sure that |
2324 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2325 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2326 | * perf_event_for_each_child or perf_event_for_each as described |
2327 | * for perf_event_disable. | |
d859e29f | 2328 | */ |
f63a8daa | 2329 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2330 | { |
cdd6c482 | 2331 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2332 | |
7b648018 | 2333 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2334 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2335 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2336 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2337 | return; |
2338 | } | |
2339 | ||
d859e29f | 2340 | /* |
cdd6c482 | 2341 | * If the event is in error state, clear that first. |
7b648018 PZ |
2342 | * |
2343 | * That way, if we see the event in error state below, we know that it | |
2344 | * has gone back into error state, as distinct from the task having | |
2345 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2346 | */ |
cdd6c482 IM |
2347 | if (event->state == PERF_EVENT_STATE_ERROR) |
2348 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2349 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2350 | |
fae3fde6 | 2351 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 2352 | } |
f63a8daa PZ |
2353 | |
2354 | /* | |
2355 | * See perf_event_disable(); | |
2356 | */ | |
2357 | void perf_event_enable(struct perf_event *event) | |
2358 | { | |
2359 | struct perf_event_context *ctx; | |
2360 | ||
2361 | ctx = perf_event_ctx_lock(event); | |
2362 | _perf_event_enable(event); | |
2363 | perf_event_ctx_unlock(event, ctx); | |
2364 | } | |
dcfce4a0 | 2365 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 2366 | |
95ff4ca2 AS |
2367 | static int __perf_event_stop(void *info) |
2368 | { | |
2369 | struct perf_event *event = info; | |
2370 | ||
2371 | /* for AUX events, our job is done if the event is already inactive */ | |
2372 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
2373 | return 0; | |
2374 | ||
2375 | /* matches smp_wmb() in event_sched_in() */ | |
2376 | smp_rmb(); | |
2377 | ||
2378 | /* | |
2379 | * There is a window with interrupts enabled before we get here, | |
2380 | * so we need to check again lest we try to stop another CPU's event. | |
2381 | */ | |
2382 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
2383 | return -EAGAIN; | |
2384 | ||
2385 | event->pmu->stop(event, PERF_EF_UPDATE); | |
2386 | ||
2387 | return 0; | |
2388 | } | |
2389 | ||
f63a8daa | 2390 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 2391 | { |
2023b359 | 2392 | /* |
cdd6c482 | 2393 | * not supported on inherited events |
2023b359 | 2394 | */ |
2e939d1d | 2395 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
2396 | return -EINVAL; |
2397 | ||
cdd6c482 | 2398 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 2399 | _perf_event_enable(event); |
2023b359 PZ |
2400 | |
2401 | return 0; | |
79f14641 | 2402 | } |
f63a8daa PZ |
2403 | |
2404 | /* | |
2405 | * See perf_event_disable() | |
2406 | */ | |
2407 | int perf_event_refresh(struct perf_event *event, int refresh) | |
2408 | { | |
2409 | struct perf_event_context *ctx; | |
2410 | int ret; | |
2411 | ||
2412 | ctx = perf_event_ctx_lock(event); | |
2413 | ret = _perf_event_refresh(event, refresh); | |
2414 | perf_event_ctx_unlock(event, ctx); | |
2415 | ||
2416 | return ret; | |
2417 | } | |
26ca5c11 | 2418 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 2419 | |
5b0311e1 FW |
2420 | static void ctx_sched_out(struct perf_event_context *ctx, |
2421 | struct perf_cpu_context *cpuctx, | |
2422 | enum event_type_t event_type) | |
235c7fc7 | 2423 | { |
db24d33e | 2424 | int is_active = ctx->is_active; |
c994d613 | 2425 | struct perf_event *event; |
235c7fc7 | 2426 | |
c994d613 | 2427 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 2428 | |
39a43640 PZ |
2429 | if (likely(!ctx->nr_events)) { |
2430 | /* | |
2431 | * See __perf_remove_from_context(). | |
2432 | */ | |
2433 | WARN_ON_ONCE(ctx->is_active); | |
2434 | if (ctx->task) | |
2435 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 2436 | return; |
39a43640 PZ |
2437 | } |
2438 | ||
db24d33e | 2439 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
2440 | if (!(ctx->is_active & EVENT_ALL)) |
2441 | ctx->is_active = 0; | |
2442 | ||
63e30d3e PZ |
2443 | if (ctx->task) { |
2444 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2445 | if (!ctx->is_active) | |
2446 | cpuctx->task_ctx = NULL; | |
2447 | } | |
facc4307 | 2448 | |
8fdc6539 PZ |
2449 | /* |
2450 | * Always update time if it was set; not only when it changes. | |
2451 | * Otherwise we can 'forget' to update time for any but the last | |
2452 | * context we sched out. For example: | |
2453 | * | |
2454 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
2455 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
2456 | * | |
2457 | * would only update time for the pinned events. | |
2458 | */ | |
3cbaa590 PZ |
2459 | if (is_active & EVENT_TIME) { |
2460 | /* update (and stop) ctx time */ | |
2461 | update_context_time(ctx); | |
2462 | update_cgrp_time_from_cpuctx(cpuctx); | |
2463 | } | |
2464 | ||
8fdc6539 PZ |
2465 | is_active ^= ctx->is_active; /* changed bits */ |
2466 | ||
3cbaa590 | 2467 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 2468 | return; |
5b0311e1 | 2469 | |
075e0b00 | 2470 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 2471 | if (is_active & EVENT_PINNED) { |
889ff015 FW |
2472 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
2473 | group_sched_out(event, cpuctx, ctx); | |
9ed6060d | 2474 | } |
889ff015 | 2475 | |
3cbaa590 | 2476 | if (is_active & EVENT_FLEXIBLE) { |
889ff015 | 2477 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
8c9ed8e1 | 2478 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 2479 | } |
1b9a644f | 2480 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
2481 | } |
2482 | ||
564c2b21 | 2483 | /* |
5a3126d4 PZ |
2484 | * Test whether two contexts are equivalent, i.e. whether they have both been |
2485 | * cloned from the same version of the same context. | |
2486 | * | |
2487 | * Equivalence is measured using a generation number in the context that is | |
2488 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
2489 | * and list_del_event(). | |
564c2b21 | 2490 | */ |
cdd6c482 IM |
2491 | static int context_equiv(struct perf_event_context *ctx1, |
2492 | struct perf_event_context *ctx2) | |
564c2b21 | 2493 | { |
211de6eb PZ |
2494 | lockdep_assert_held(&ctx1->lock); |
2495 | lockdep_assert_held(&ctx2->lock); | |
2496 | ||
5a3126d4 PZ |
2497 | /* Pinning disables the swap optimization */ |
2498 | if (ctx1->pin_count || ctx2->pin_count) | |
2499 | return 0; | |
2500 | ||
2501 | /* If ctx1 is the parent of ctx2 */ | |
2502 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
2503 | return 1; | |
2504 | ||
2505 | /* If ctx2 is the parent of ctx1 */ | |
2506 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
2507 | return 1; | |
2508 | ||
2509 | /* | |
2510 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
2511 | * hierarchy, see perf_event_init_context(). | |
2512 | */ | |
2513 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
2514 | ctx1->parent_gen == ctx2->parent_gen) | |
2515 | return 1; | |
2516 | ||
2517 | /* Unmatched */ | |
2518 | return 0; | |
564c2b21 PM |
2519 | } |
2520 | ||
cdd6c482 IM |
2521 | static void __perf_event_sync_stat(struct perf_event *event, |
2522 | struct perf_event *next_event) | |
bfbd3381 PZ |
2523 | { |
2524 | u64 value; | |
2525 | ||
cdd6c482 | 2526 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
2527 | return; |
2528 | ||
2529 | /* | |
cdd6c482 | 2530 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
2531 | * because we're in the middle of a context switch and have IRQs |
2532 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 2533 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
2534 | * don't need to use it. |
2535 | */ | |
cdd6c482 IM |
2536 | switch (event->state) { |
2537 | case PERF_EVENT_STATE_ACTIVE: | |
3dbebf15 PZ |
2538 | event->pmu->read(event); |
2539 | /* fall-through */ | |
bfbd3381 | 2540 | |
cdd6c482 IM |
2541 | case PERF_EVENT_STATE_INACTIVE: |
2542 | update_event_times(event); | |
bfbd3381 PZ |
2543 | break; |
2544 | ||
2545 | default: | |
2546 | break; | |
2547 | } | |
2548 | ||
2549 | /* | |
cdd6c482 | 2550 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
2551 | * values when we flip the contexts. |
2552 | */ | |
e7850595 PZ |
2553 | value = local64_read(&next_event->count); |
2554 | value = local64_xchg(&event->count, value); | |
2555 | local64_set(&next_event->count, value); | |
bfbd3381 | 2556 | |
cdd6c482 IM |
2557 | swap(event->total_time_enabled, next_event->total_time_enabled); |
2558 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 2559 | |
bfbd3381 | 2560 | /* |
19d2e755 | 2561 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 2562 | */ |
cdd6c482 IM |
2563 | perf_event_update_userpage(event); |
2564 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
2565 | } |
2566 | ||
cdd6c482 IM |
2567 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
2568 | struct perf_event_context *next_ctx) | |
bfbd3381 | 2569 | { |
cdd6c482 | 2570 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
2571 | |
2572 | if (!ctx->nr_stat) | |
2573 | return; | |
2574 | ||
02ffdbc8 PZ |
2575 | update_context_time(ctx); |
2576 | ||
cdd6c482 IM |
2577 | event = list_first_entry(&ctx->event_list, |
2578 | struct perf_event, event_entry); | |
bfbd3381 | 2579 | |
cdd6c482 IM |
2580 | next_event = list_first_entry(&next_ctx->event_list, |
2581 | struct perf_event, event_entry); | |
bfbd3381 | 2582 | |
cdd6c482 IM |
2583 | while (&event->event_entry != &ctx->event_list && |
2584 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 2585 | |
cdd6c482 | 2586 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 2587 | |
cdd6c482 IM |
2588 | event = list_next_entry(event, event_entry); |
2589 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
2590 | } |
2591 | } | |
2592 | ||
fe4b04fa PZ |
2593 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
2594 | struct task_struct *next) | |
0793a61d | 2595 | { |
8dc85d54 | 2596 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 2597 | struct perf_event_context *next_ctx; |
5a3126d4 | 2598 | struct perf_event_context *parent, *next_parent; |
108b02cf | 2599 | struct perf_cpu_context *cpuctx; |
c93f7669 | 2600 | int do_switch = 1; |
0793a61d | 2601 | |
108b02cf PZ |
2602 | if (likely(!ctx)) |
2603 | return; | |
10989fb2 | 2604 | |
108b02cf PZ |
2605 | cpuctx = __get_cpu_context(ctx); |
2606 | if (!cpuctx->task_ctx) | |
0793a61d TG |
2607 | return; |
2608 | ||
c93f7669 | 2609 | rcu_read_lock(); |
8dc85d54 | 2610 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
2611 | if (!next_ctx) |
2612 | goto unlock; | |
2613 | ||
2614 | parent = rcu_dereference(ctx->parent_ctx); | |
2615 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
2616 | ||
2617 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 2618 | if (!parent && !next_parent) |
5a3126d4 PZ |
2619 | goto unlock; |
2620 | ||
2621 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
2622 | /* |
2623 | * Looks like the two contexts are clones, so we might be | |
2624 | * able to optimize the context switch. We lock both | |
2625 | * contexts and check that they are clones under the | |
2626 | * lock (including re-checking that neither has been | |
2627 | * uncloned in the meantime). It doesn't matter which | |
2628 | * order we take the locks because no other cpu could | |
2629 | * be trying to lock both of these tasks. | |
2630 | */ | |
e625cce1 TG |
2631 | raw_spin_lock(&ctx->lock); |
2632 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 2633 | if (context_equiv(ctx, next_ctx)) { |
63b6da39 PZ |
2634 | WRITE_ONCE(ctx->task, next); |
2635 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c YZ |
2636 | |
2637 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
2638 | ||
63b6da39 PZ |
2639 | /* |
2640 | * RCU_INIT_POINTER here is safe because we've not | |
2641 | * modified the ctx and the above modification of | |
2642 | * ctx->task and ctx->task_ctx_data are immaterial | |
2643 | * since those values are always verified under | |
2644 | * ctx->lock which we're now holding. | |
2645 | */ | |
2646 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
2647 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
2648 | ||
c93f7669 | 2649 | do_switch = 0; |
bfbd3381 | 2650 | |
cdd6c482 | 2651 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 2652 | } |
e625cce1 TG |
2653 | raw_spin_unlock(&next_ctx->lock); |
2654 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 2655 | } |
5a3126d4 | 2656 | unlock: |
c93f7669 | 2657 | rcu_read_unlock(); |
564c2b21 | 2658 | |
c93f7669 | 2659 | if (do_switch) { |
facc4307 | 2660 | raw_spin_lock(&ctx->lock); |
8833d0e2 | 2661 | task_ctx_sched_out(cpuctx, ctx); |
facc4307 | 2662 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 2663 | } |
0793a61d TG |
2664 | } |
2665 | ||
ba532500 YZ |
2666 | void perf_sched_cb_dec(struct pmu *pmu) |
2667 | { | |
2668 | this_cpu_dec(perf_sched_cb_usages); | |
2669 | } | |
2670 | ||
2671 | void perf_sched_cb_inc(struct pmu *pmu) | |
2672 | { | |
2673 | this_cpu_inc(perf_sched_cb_usages); | |
2674 | } | |
2675 | ||
2676 | /* | |
2677 | * This function provides the context switch callback to the lower code | |
2678 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
2679 | */ | |
2680 | static void perf_pmu_sched_task(struct task_struct *prev, | |
2681 | struct task_struct *next, | |
2682 | bool sched_in) | |
2683 | { | |
2684 | struct perf_cpu_context *cpuctx; | |
2685 | struct pmu *pmu; | |
2686 | unsigned long flags; | |
2687 | ||
2688 | if (prev == next) | |
2689 | return; | |
2690 | ||
2691 | local_irq_save(flags); | |
2692 | ||
2693 | rcu_read_lock(); | |
2694 | ||
2695 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
2696 | if (pmu->sched_task) { | |
2697 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); | |
2698 | ||
2699 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); | |
2700 | ||
2701 | perf_pmu_disable(pmu); | |
2702 | ||
2703 | pmu->sched_task(cpuctx->task_ctx, sched_in); | |
2704 | ||
2705 | perf_pmu_enable(pmu); | |
2706 | ||
2707 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
2708 | } | |
2709 | } | |
2710 | ||
2711 | rcu_read_unlock(); | |
2712 | ||
2713 | local_irq_restore(flags); | |
2714 | } | |
2715 | ||
45ac1403 AH |
2716 | static void perf_event_switch(struct task_struct *task, |
2717 | struct task_struct *next_prev, bool sched_in); | |
2718 | ||
8dc85d54 PZ |
2719 | #define for_each_task_context_nr(ctxn) \ |
2720 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
2721 | ||
2722 | /* | |
2723 | * Called from scheduler to remove the events of the current task, | |
2724 | * with interrupts disabled. | |
2725 | * | |
2726 | * We stop each event and update the event value in event->count. | |
2727 | * | |
2728 | * This does not protect us against NMI, but disable() | |
2729 | * sets the disabled bit in the control field of event _before_ | |
2730 | * accessing the event control register. If a NMI hits, then it will | |
2731 | * not restart the event. | |
2732 | */ | |
ab0cce56 JO |
2733 | void __perf_event_task_sched_out(struct task_struct *task, |
2734 | struct task_struct *next) | |
8dc85d54 PZ |
2735 | { |
2736 | int ctxn; | |
2737 | ||
ba532500 YZ |
2738 | if (__this_cpu_read(perf_sched_cb_usages)) |
2739 | perf_pmu_sched_task(task, next, false); | |
2740 | ||
45ac1403 AH |
2741 | if (atomic_read(&nr_switch_events)) |
2742 | perf_event_switch(task, next, false); | |
2743 | ||
8dc85d54 PZ |
2744 | for_each_task_context_nr(ctxn) |
2745 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
2746 | |
2747 | /* | |
2748 | * if cgroup events exist on this CPU, then we need | |
2749 | * to check if we have to switch out PMU state. | |
2750 | * cgroup event are system-wide mode only | |
2751 | */ | |
4a32fea9 | 2752 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 2753 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
2754 | } |
2755 | ||
5b0311e1 FW |
2756 | /* |
2757 | * Called with IRQs disabled | |
2758 | */ | |
2759 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
2760 | enum event_type_t event_type) | |
2761 | { | |
2762 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
2763 | } |
2764 | ||
235c7fc7 | 2765 | static void |
5b0311e1 | 2766 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
6e37738a | 2767 | struct perf_cpu_context *cpuctx) |
0793a61d | 2768 | { |
cdd6c482 | 2769 | struct perf_event *event; |
0793a61d | 2770 | |
889ff015 FW |
2771 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
2772 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2773 | continue; |
5632ab12 | 2774 | if (!event_filter_match(event)) |
3b6f9e5c PM |
2775 | continue; |
2776 | ||
e5d1367f SE |
2777 | /* may need to reset tstamp_enabled */ |
2778 | if (is_cgroup_event(event)) | |
2779 | perf_cgroup_mark_enabled(event, ctx); | |
2780 | ||
8c9ed8e1 | 2781 | if (group_can_go_on(event, cpuctx, 1)) |
6e37738a | 2782 | group_sched_in(event, cpuctx, ctx); |
3b6f9e5c PM |
2783 | |
2784 | /* | |
2785 | * If this pinned group hasn't been scheduled, | |
2786 | * put it in error state. | |
2787 | */ | |
cdd6c482 IM |
2788 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2789 | update_group_times(event); | |
2790 | event->state = PERF_EVENT_STATE_ERROR; | |
53cfbf59 | 2791 | } |
3b6f9e5c | 2792 | } |
5b0311e1 FW |
2793 | } |
2794 | ||
2795 | static void | |
2796 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
6e37738a | 2797 | struct perf_cpu_context *cpuctx) |
5b0311e1 FW |
2798 | { |
2799 | struct perf_event *event; | |
2800 | int can_add_hw = 1; | |
3b6f9e5c | 2801 | |
889ff015 FW |
2802 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
2803 | /* Ignore events in OFF or ERROR state */ | |
2804 | if (event->state <= PERF_EVENT_STATE_OFF) | |
3b6f9e5c | 2805 | continue; |
04289bb9 IM |
2806 | /* |
2807 | * Listen to the 'cpu' scheduling filter constraint | |
cdd6c482 | 2808 | * of events: |
04289bb9 | 2809 | */ |
5632ab12 | 2810 | if (!event_filter_match(event)) |
0793a61d TG |
2811 | continue; |
2812 | ||
e5d1367f SE |
2813 | /* may need to reset tstamp_enabled */ |
2814 | if (is_cgroup_event(event)) | |
2815 | perf_cgroup_mark_enabled(event, ctx); | |
2816 | ||
9ed6060d | 2817 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
6e37738a | 2818 | if (group_sched_in(event, cpuctx, ctx)) |
dd0e6ba2 | 2819 | can_add_hw = 0; |
9ed6060d | 2820 | } |
0793a61d | 2821 | } |
5b0311e1 FW |
2822 | } |
2823 | ||
2824 | static void | |
2825 | ctx_sched_in(struct perf_event_context *ctx, | |
2826 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
2827 | enum event_type_t event_type, |
2828 | struct task_struct *task) | |
5b0311e1 | 2829 | { |
db24d33e | 2830 | int is_active = ctx->is_active; |
c994d613 PZ |
2831 | u64 now; |
2832 | ||
2833 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 2834 | |
5b0311e1 | 2835 | if (likely(!ctx->nr_events)) |
facc4307 | 2836 | return; |
5b0311e1 | 2837 | |
3cbaa590 | 2838 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
2839 | if (ctx->task) { |
2840 | if (!is_active) | |
2841 | cpuctx->task_ctx = ctx; | |
2842 | else | |
2843 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2844 | } | |
2845 | ||
3cbaa590 PZ |
2846 | is_active ^= ctx->is_active; /* changed bits */ |
2847 | ||
2848 | if (is_active & EVENT_TIME) { | |
2849 | /* start ctx time */ | |
2850 | now = perf_clock(); | |
2851 | ctx->timestamp = now; | |
2852 | perf_cgroup_set_timestamp(task, ctx); | |
2853 | } | |
2854 | ||
5b0311e1 FW |
2855 | /* |
2856 | * First go through the list and put on any pinned groups | |
2857 | * in order to give them the best chance of going on. | |
2858 | */ | |
3cbaa590 | 2859 | if (is_active & EVENT_PINNED) |
6e37738a | 2860 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
2861 | |
2862 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 2863 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 2864 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
2865 | } |
2866 | ||
329c0e01 | 2867 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
2868 | enum event_type_t event_type, |
2869 | struct task_struct *task) | |
329c0e01 FW |
2870 | { |
2871 | struct perf_event_context *ctx = &cpuctx->ctx; | |
2872 | ||
e5d1367f | 2873 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
2874 | } |
2875 | ||
e5d1367f SE |
2876 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
2877 | struct task_struct *task) | |
235c7fc7 | 2878 | { |
108b02cf | 2879 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 2880 | |
108b02cf | 2881 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
2882 | if (cpuctx->task_ctx == ctx) |
2883 | return; | |
2884 | ||
facc4307 | 2885 | perf_ctx_lock(cpuctx, ctx); |
1b9a644f | 2886 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
2887 | /* |
2888 | * We want to keep the following priority order: | |
2889 | * cpu pinned (that don't need to move), task pinned, | |
2890 | * cpu flexible, task flexible. | |
2891 | */ | |
2892 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
63e30d3e | 2893 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 PZ |
2894 | perf_pmu_enable(ctx->pmu); |
2895 | perf_ctx_unlock(cpuctx, ctx); | |
235c7fc7 IM |
2896 | } |
2897 | ||
8dc85d54 PZ |
2898 | /* |
2899 | * Called from scheduler to add the events of the current task | |
2900 | * with interrupts disabled. | |
2901 | * | |
2902 | * We restore the event value and then enable it. | |
2903 | * | |
2904 | * This does not protect us against NMI, but enable() | |
2905 | * sets the enabled bit in the control field of event _before_ | |
2906 | * accessing the event control register. If a NMI hits, then it will | |
2907 | * keep the event running. | |
2908 | */ | |
ab0cce56 JO |
2909 | void __perf_event_task_sched_in(struct task_struct *prev, |
2910 | struct task_struct *task) | |
8dc85d54 PZ |
2911 | { |
2912 | struct perf_event_context *ctx; | |
2913 | int ctxn; | |
2914 | ||
7e41d177 PZ |
2915 | /* |
2916 | * If cgroup events exist on this CPU, then we need to check if we have | |
2917 | * to switch in PMU state; cgroup event are system-wide mode only. | |
2918 | * | |
2919 | * Since cgroup events are CPU events, we must schedule these in before | |
2920 | * we schedule in the task events. | |
2921 | */ | |
2922 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
2923 | perf_cgroup_sched_in(prev, task); | |
2924 | ||
8dc85d54 PZ |
2925 | for_each_task_context_nr(ctxn) { |
2926 | ctx = task->perf_event_ctxp[ctxn]; | |
2927 | if (likely(!ctx)) | |
2928 | continue; | |
2929 | ||
e5d1367f | 2930 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 2931 | } |
d010b332 | 2932 | |
45ac1403 AH |
2933 | if (atomic_read(&nr_switch_events)) |
2934 | perf_event_switch(task, prev, true); | |
2935 | ||
ba532500 YZ |
2936 | if (__this_cpu_read(perf_sched_cb_usages)) |
2937 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
2938 | } |
2939 | ||
abd50713 PZ |
2940 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
2941 | { | |
2942 | u64 frequency = event->attr.sample_freq; | |
2943 | u64 sec = NSEC_PER_SEC; | |
2944 | u64 divisor, dividend; | |
2945 | ||
2946 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
2947 | ||
2948 | count_fls = fls64(count); | |
2949 | nsec_fls = fls64(nsec); | |
2950 | frequency_fls = fls64(frequency); | |
2951 | sec_fls = 30; | |
2952 | ||
2953 | /* | |
2954 | * We got @count in @nsec, with a target of sample_freq HZ | |
2955 | * the target period becomes: | |
2956 | * | |
2957 | * @count * 10^9 | |
2958 | * period = ------------------- | |
2959 | * @nsec * sample_freq | |
2960 | * | |
2961 | */ | |
2962 | ||
2963 | /* | |
2964 | * Reduce accuracy by one bit such that @a and @b converge | |
2965 | * to a similar magnitude. | |
2966 | */ | |
fe4b04fa | 2967 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
2968 | do { \ |
2969 | if (a##_fls > b##_fls) { \ | |
2970 | a >>= 1; \ | |
2971 | a##_fls--; \ | |
2972 | } else { \ | |
2973 | b >>= 1; \ | |
2974 | b##_fls--; \ | |
2975 | } \ | |
2976 | } while (0) | |
2977 | ||
2978 | /* | |
2979 | * Reduce accuracy until either term fits in a u64, then proceed with | |
2980 | * the other, so that finally we can do a u64/u64 division. | |
2981 | */ | |
2982 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
2983 | REDUCE_FLS(nsec, frequency); | |
2984 | REDUCE_FLS(sec, count); | |
2985 | } | |
2986 | ||
2987 | if (count_fls + sec_fls > 64) { | |
2988 | divisor = nsec * frequency; | |
2989 | ||
2990 | while (count_fls + sec_fls > 64) { | |
2991 | REDUCE_FLS(count, sec); | |
2992 | divisor >>= 1; | |
2993 | } | |
2994 | ||
2995 | dividend = count * sec; | |
2996 | } else { | |
2997 | dividend = count * sec; | |
2998 | ||
2999 | while (nsec_fls + frequency_fls > 64) { | |
3000 | REDUCE_FLS(nsec, frequency); | |
3001 | dividend >>= 1; | |
3002 | } | |
3003 | ||
3004 | divisor = nsec * frequency; | |
3005 | } | |
3006 | ||
f6ab91ad PZ |
3007 | if (!divisor) |
3008 | return dividend; | |
3009 | ||
abd50713 PZ |
3010 | return div64_u64(dividend, divisor); |
3011 | } | |
3012 | ||
e050e3f0 SE |
3013 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3014 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3015 | ||
f39d47ff | 3016 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3017 | { |
cdd6c482 | 3018 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3019 | s64 period, sample_period; |
bd2b5b12 PZ |
3020 | s64 delta; |
3021 | ||
abd50713 | 3022 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3023 | |
3024 | delta = (s64)(period - hwc->sample_period); | |
3025 | delta = (delta + 7) / 8; /* low pass filter */ | |
3026 | ||
3027 | sample_period = hwc->sample_period + delta; | |
3028 | ||
3029 | if (!sample_period) | |
3030 | sample_period = 1; | |
3031 | ||
bd2b5b12 | 3032 | hwc->sample_period = sample_period; |
abd50713 | 3033 | |
e7850595 | 3034 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3035 | if (disable) |
3036 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3037 | ||
e7850595 | 3038 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3039 | |
3040 | if (disable) | |
3041 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3042 | } |
bd2b5b12 PZ |
3043 | } |
3044 | ||
e050e3f0 SE |
3045 | /* |
3046 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3047 | * events. At the same time, make sure, having freq events does not change | |
3048 | * the rate of unthrottling as that would introduce bias. | |
3049 | */ | |
3050 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3051 | int needs_unthr) | |
60db5e09 | 3052 | { |
cdd6c482 IM |
3053 | struct perf_event *event; |
3054 | struct hw_perf_event *hwc; | |
e050e3f0 | 3055 | u64 now, period = TICK_NSEC; |
abd50713 | 3056 | s64 delta; |
60db5e09 | 3057 | |
e050e3f0 SE |
3058 | /* |
3059 | * only need to iterate over all events iff: | |
3060 | * - context have events in frequency mode (needs freq adjust) | |
3061 | * - there are events to unthrottle on this cpu | |
3062 | */ | |
3063 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3064 | return; |
3065 | ||
e050e3f0 | 3066 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3067 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3068 | |
03541f8b | 3069 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3070 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3071 | continue; |
3072 | ||
5632ab12 | 3073 | if (!event_filter_match(event)) |
5d27c23d PZ |
3074 | continue; |
3075 | ||
44377277 AS |
3076 | perf_pmu_disable(event->pmu); |
3077 | ||
cdd6c482 | 3078 | hwc = &event->hw; |
6a24ed6c | 3079 | |
ae23bff1 | 3080 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3081 | hwc->interrupts = 0; |
cdd6c482 | 3082 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3083 | event->pmu->start(event, 0); |
a78ac325 PZ |
3084 | } |
3085 | ||
cdd6c482 | 3086 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3087 | goto next; |
60db5e09 | 3088 | |
e050e3f0 SE |
3089 | /* |
3090 | * stop the event and update event->count | |
3091 | */ | |
3092 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3093 | ||
e7850595 | 3094 | now = local64_read(&event->count); |
abd50713 PZ |
3095 | delta = now - hwc->freq_count_stamp; |
3096 | hwc->freq_count_stamp = now; | |
60db5e09 | 3097 | |
e050e3f0 SE |
3098 | /* |
3099 | * restart the event | |
3100 | * reload only if value has changed | |
f39d47ff SE |
3101 | * we have stopped the event so tell that |
3102 | * to perf_adjust_period() to avoid stopping it | |
3103 | * twice. | |
e050e3f0 | 3104 | */ |
abd50713 | 3105 | if (delta > 0) |
f39d47ff | 3106 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3107 | |
3108 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3109 | next: |
3110 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3111 | } |
e050e3f0 | 3112 | |
f39d47ff | 3113 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3114 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3115 | } |
3116 | ||
235c7fc7 | 3117 | /* |
cdd6c482 | 3118 | * Round-robin a context's events: |
235c7fc7 | 3119 | */ |
cdd6c482 | 3120 | static void rotate_ctx(struct perf_event_context *ctx) |
0793a61d | 3121 | { |
dddd3379 TG |
3122 | /* |
3123 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
3124 | * disabled by the inheritance code. | |
3125 | */ | |
3126 | if (!ctx->rotate_disable) | |
3127 | list_rotate_left(&ctx->flexible_groups); | |
235c7fc7 IM |
3128 | } |
3129 | ||
9e630205 | 3130 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
235c7fc7 | 3131 | { |
8dc85d54 | 3132 | struct perf_event_context *ctx = NULL; |
2fde4f94 | 3133 | int rotate = 0; |
7fc23a53 | 3134 | |
b5ab4cd5 | 3135 | if (cpuctx->ctx.nr_events) { |
b5ab4cd5 PZ |
3136 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
3137 | rotate = 1; | |
3138 | } | |
235c7fc7 | 3139 | |
8dc85d54 | 3140 | ctx = cpuctx->task_ctx; |
b5ab4cd5 | 3141 | if (ctx && ctx->nr_events) { |
b5ab4cd5 PZ |
3142 | if (ctx->nr_events != ctx->nr_active) |
3143 | rotate = 1; | |
3144 | } | |
9717e6cd | 3145 | |
e050e3f0 | 3146 | if (!rotate) |
0f5a2601 PZ |
3147 | goto done; |
3148 | ||
facc4307 | 3149 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 3150 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 3151 | |
e050e3f0 SE |
3152 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
3153 | if (ctx) | |
3154 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 3155 | |
e050e3f0 SE |
3156 | rotate_ctx(&cpuctx->ctx); |
3157 | if (ctx) | |
3158 | rotate_ctx(ctx); | |
235c7fc7 | 3159 | |
e050e3f0 | 3160 | perf_event_sched_in(cpuctx, ctx, current); |
235c7fc7 | 3161 | |
0f5a2601 PZ |
3162 | perf_pmu_enable(cpuctx->ctx.pmu); |
3163 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
b5ab4cd5 | 3164 | done: |
9e630205 SE |
3165 | |
3166 | return rotate; | |
e9d2b064 PZ |
3167 | } |
3168 | ||
3169 | void perf_event_task_tick(void) | |
3170 | { | |
2fde4f94 MR |
3171 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
3172 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 3173 | int throttled; |
b5ab4cd5 | 3174 | |
e9d2b064 PZ |
3175 | WARN_ON(!irqs_disabled()); |
3176 | ||
e050e3f0 SE |
3177 | __this_cpu_inc(perf_throttled_seq); |
3178 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 3179 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 3180 | |
2fde4f94 | 3181 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 3182 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
3183 | } |
3184 | ||
889ff015 FW |
3185 | static int event_enable_on_exec(struct perf_event *event, |
3186 | struct perf_event_context *ctx) | |
3187 | { | |
3188 | if (!event->attr.enable_on_exec) | |
3189 | return 0; | |
3190 | ||
3191 | event->attr.enable_on_exec = 0; | |
3192 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
3193 | return 0; | |
3194 | ||
1d9b482e | 3195 | __perf_event_mark_enabled(event); |
889ff015 FW |
3196 | |
3197 | return 1; | |
3198 | } | |
3199 | ||
57e7986e | 3200 | /* |
cdd6c482 | 3201 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
3202 | * This expects task == current. |
3203 | */ | |
c1274499 | 3204 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 3205 | { |
c1274499 | 3206 | struct perf_event_context *ctx, *clone_ctx = NULL; |
3e349507 | 3207 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 3208 | struct perf_event *event; |
57e7986e PM |
3209 | unsigned long flags; |
3210 | int enabled = 0; | |
3211 | ||
3212 | local_irq_save(flags); | |
c1274499 | 3213 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 3214 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
3215 | goto out; |
3216 | ||
3e349507 PZ |
3217 | cpuctx = __get_cpu_context(ctx); |
3218 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 3219 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
3e349507 PZ |
3220 | list_for_each_entry(event, &ctx->event_list, event_entry) |
3221 | enabled |= event_enable_on_exec(event, ctx); | |
57e7986e PM |
3222 | |
3223 | /* | |
3e349507 | 3224 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 3225 | */ |
3e349507 | 3226 | if (enabled) { |
211de6eb | 3227 | clone_ctx = unclone_ctx(ctx); |
3e349507 PZ |
3228 | ctx_resched(cpuctx, ctx); |
3229 | } | |
3230 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 3231 | |
9ed6060d | 3232 | out: |
57e7986e | 3233 | local_irq_restore(flags); |
211de6eb PZ |
3234 | |
3235 | if (clone_ctx) | |
3236 | put_ctx(clone_ctx); | |
57e7986e PM |
3237 | } |
3238 | ||
e041e328 PZ |
3239 | void perf_event_exec(void) |
3240 | { | |
e041e328 PZ |
3241 | int ctxn; |
3242 | ||
3243 | rcu_read_lock(); | |
c1274499 PZ |
3244 | for_each_task_context_nr(ctxn) |
3245 | perf_event_enable_on_exec(ctxn); | |
e041e328 PZ |
3246 | rcu_read_unlock(); |
3247 | } | |
3248 | ||
0492d4c5 PZ |
3249 | struct perf_read_data { |
3250 | struct perf_event *event; | |
3251 | bool group; | |
7d88962e | 3252 | int ret; |
0492d4c5 PZ |
3253 | }; |
3254 | ||
0793a61d | 3255 | /* |
cdd6c482 | 3256 | * Cross CPU call to read the hardware event |
0793a61d | 3257 | */ |
cdd6c482 | 3258 | static void __perf_event_read(void *info) |
0793a61d | 3259 | { |
0492d4c5 PZ |
3260 | struct perf_read_data *data = info; |
3261 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 3262 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 3263 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 3264 | struct pmu *pmu = event->pmu; |
621a01ea | 3265 | |
e1ac3614 PM |
3266 | /* |
3267 | * If this is a task context, we need to check whether it is | |
3268 | * the current task context of this cpu. If not it has been | |
3269 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
3270 | * event->count would have been updated to a recent sample |
3271 | * when the event was scheduled out. | |
e1ac3614 PM |
3272 | */ |
3273 | if (ctx->task && cpuctx->task_ctx != ctx) | |
3274 | return; | |
3275 | ||
e625cce1 | 3276 | raw_spin_lock(&ctx->lock); |
e5d1367f | 3277 | if (ctx->is_active) { |
542e72fc | 3278 | update_context_time(ctx); |
e5d1367f SE |
3279 | update_cgrp_time_from_event(event); |
3280 | } | |
0492d4c5 | 3281 | |
cdd6c482 | 3282 | update_event_times(event); |
4a00c16e SB |
3283 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
3284 | goto unlock; | |
0492d4c5 | 3285 | |
4a00c16e SB |
3286 | if (!data->group) { |
3287 | pmu->read(event); | |
3288 | data->ret = 0; | |
0492d4c5 | 3289 | goto unlock; |
4a00c16e SB |
3290 | } |
3291 | ||
3292 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
3293 | ||
3294 | pmu->read(event); | |
0492d4c5 PZ |
3295 | |
3296 | list_for_each_entry(sub, &event->sibling_list, group_entry) { | |
3297 | update_event_times(sub); | |
4a00c16e SB |
3298 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
3299 | /* | |
3300 | * Use sibling's PMU rather than @event's since | |
3301 | * sibling could be on different (eg: software) PMU. | |
3302 | */ | |
0492d4c5 | 3303 | sub->pmu->read(sub); |
4a00c16e | 3304 | } |
0492d4c5 | 3305 | } |
4a00c16e SB |
3306 | |
3307 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
3308 | |
3309 | unlock: | |
e625cce1 | 3310 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
3311 | } |
3312 | ||
b5e58793 PZ |
3313 | static inline u64 perf_event_count(struct perf_event *event) |
3314 | { | |
eacd3ecc MF |
3315 | if (event->pmu->count) |
3316 | return event->pmu->count(event); | |
3317 | ||
3318 | return __perf_event_count(event); | |
b5e58793 PZ |
3319 | } |
3320 | ||
ffe8690c KX |
3321 | /* |
3322 | * NMI-safe method to read a local event, that is an event that | |
3323 | * is: | |
3324 | * - either for the current task, or for this CPU | |
3325 | * - does not have inherit set, for inherited task events | |
3326 | * will not be local and we cannot read them atomically | |
3327 | * - must not have a pmu::count method | |
3328 | */ | |
3329 | u64 perf_event_read_local(struct perf_event *event) | |
3330 | { | |
3331 | unsigned long flags; | |
3332 | u64 val; | |
3333 | ||
3334 | /* | |
3335 | * Disabling interrupts avoids all counter scheduling (context | |
3336 | * switches, timer based rotation and IPIs). | |
3337 | */ | |
3338 | local_irq_save(flags); | |
3339 | ||
3340 | /* If this is a per-task event, it must be for current */ | |
3341 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && | |
3342 | event->hw.target != current); | |
3343 | ||
3344 | /* If this is a per-CPU event, it must be for this CPU */ | |
3345 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && | |
3346 | event->cpu != smp_processor_id()); | |
3347 | ||
3348 | /* | |
3349 | * It must not be an event with inherit set, we cannot read | |
3350 | * all child counters from atomic context. | |
3351 | */ | |
3352 | WARN_ON_ONCE(event->attr.inherit); | |
3353 | ||
3354 | /* | |
3355 | * It must not have a pmu::count method, those are not | |
3356 | * NMI safe. | |
3357 | */ | |
3358 | WARN_ON_ONCE(event->pmu->count); | |
3359 | ||
3360 | /* | |
3361 | * If the event is currently on this CPU, its either a per-task event, | |
3362 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
3363 | * oncpu == -1). | |
3364 | */ | |
3365 | if (event->oncpu == smp_processor_id()) | |
3366 | event->pmu->read(event); | |
3367 | ||
3368 | val = local64_read(&event->count); | |
3369 | local_irq_restore(flags); | |
3370 | ||
3371 | return val; | |
3372 | } | |
3373 | ||
7d88962e | 3374 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 3375 | { |
7d88962e SB |
3376 | int ret = 0; |
3377 | ||
0793a61d | 3378 | /* |
cdd6c482 IM |
3379 | * If event is enabled and currently active on a CPU, update the |
3380 | * value in the event structure: | |
0793a61d | 3381 | */ |
cdd6c482 | 3382 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
0492d4c5 PZ |
3383 | struct perf_read_data data = { |
3384 | .event = event, | |
3385 | .group = group, | |
7d88962e | 3386 | .ret = 0, |
0492d4c5 | 3387 | }; |
cdd6c482 | 3388 | smp_call_function_single(event->oncpu, |
0492d4c5 | 3389 | __perf_event_read, &data, 1); |
7d88962e | 3390 | ret = data.ret; |
cdd6c482 | 3391 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
2b8988c9 PZ |
3392 | struct perf_event_context *ctx = event->ctx; |
3393 | unsigned long flags; | |
3394 | ||
e625cce1 | 3395 | raw_spin_lock_irqsave(&ctx->lock, flags); |
c530ccd9 SE |
3396 | /* |
3397 | * may read while context is not active | |
3398 | * (e.g., thread is blocked), in that case | |
3399 | * we cannot update context time | |
3400 | */ | |
e5d1367f | 3401 | if (ctx->is_active) { |
c530ccd9 | 3402 | update_context_time(ctx); |
e5d1367f SE |
3403 | update_cgrp_time_from_event(event); |
3404 | } | |
0492d4c5 PZ |
3405 | if (group) |
3406 | update_group_times(event); | |
3407 | else | |
3408 | update_event_times(event); | |
e625cce1 | 3409 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 3410 | } |
7d88962e SB |
3411 | |
3412 | return ret; | |
0793a61d TG |
3413 | } |
3414 | ||
a63eaf34 | 3415 | /* |
cdd6c482 | 3416 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 3417 | */ |
eb184479 | 3418 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 3419 | { |
e625cce1 | 3420 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 3421 | mutex_init(&ctx->mutex); |
2fde4f94 | 3422 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
889ff015 FW |
3423 | INIT_LIST_HEAD(&ctx->pinned_groups); |
3424 | INIT_LIST_HEAD(&ctx->flexible_groups); | |
a63eaf34 PM |
3425 | INIT_LIST_HEAD(&ctx->event_list); |
3426 | atomic_set(&ctx->refcount, 1); | |
eb184479 PZ |
3427 | } |
3428 | ||
3429 | static struct perf_event_context * | |
3430 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
3431 | { | |
3432 | struct perf_event_context *ctx; | |
3433 | ||
3434 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
3435 | if (!ctx) | |
3436 | return NULL; | |
3437 | ||
3438 | __perf_event_init_context(ctx); | |
3439 | if (task) { | |
3440 | ctx->task = task; | |
3441 | get_task_struct(task); | |
0793a61d | 3442 | } |
eb184479 PZ |
3443 | ctx->pmu = pmu; |
3444 | ||
3445 | return ctx; | |
a63eaf34 PM |
3446 | } |
3447 | ||
2ebd4ffb MH |
3448 | static struct task_struct * |
3449 | find_lively_task_by_vpid(pid_t vpid) | |
3450 | { | |
3451 | struct task_struct *task; | |
3452 | int err; | |
0793a61d TG |
3453 | |
3454 | rcu_read_lock(); | |
2ebd4ffb | 3455 | if (!vpid) |
0793a61d TG |
3456 | task = current; |
3457 | else | |
2ebd4ffb | 3458 | task = find_task_by_vpid(vpid); |
0793a61d TG |
3459 | if (task) |
3460 | get_task_struct(task); | |
3461 | rcu_read_unlock(); | |
3462 | ||
3463 | if (!task) | |
3464 | return ERR_PTR(-ESRCH); | |
3465 | ||
0793a61d | 3466 | /* Reuse ptrace permission checks for now. */ |
c93f7669 | 3467 | err = -EACCES; |
caaee623 | 3468 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) |
c93f7669 PM |
3469 | goto errout; |
3470 | ||
2ebd4ffb MH |
3471 | return task; |
3472 | errout: | |
3473 | put_task_struct(task); | |
3474 | return ERR_PTR(err); | |
3475 | ||
3476 | } | |
3477 | ||
fe4b04fa PZ |
3478 | /* |
3479 | * Returns a matching context with refcount and pincount. | |
3480 | */ | |
108b02cf | 3481 | static struct perf_event_context * |
4af57ef2 YZ |
3482 | find_get_context(struct pmu *pmu, struct task_struct *task, |
3483 | struct perf_event *event) | |
0793a61d | 3484 | { |
211de6eb | 3485 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 3486 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 3487 | void *task_ctx_data = NULL; |
25346b93 | 3488 | unsigned long flags; |
8dc85d54 | 3489 | int ctxn, err; |
4af57ef2 | 3490 | int cpu = event->cpu; |
0793a61d | 3491 | |
22a4ec72 | 3492 | if (!task) { |
cdd6c482 | 3493 | /* Must be root to operate on a CPU event: */ |
0764771d | 3494 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
0793a61d TG |
3495 | return ERR_PTR(-EACCES); |
3496 | ||
0793a61d | 3497 | /* |
cdd6c482 | 3498 | * We could be clever and allow to attach a event to an |
0793a61d TG |
3499 | * offline CPU and activate it when the CPU comes up, but |
3500 | * that's for later. | |
3501 | */ | |
f6325e30 | 3502 | if (!cpu_online(cpu)) |
0793a61d TG |
3503 | return ERR_PTR(-ENODEV); |
3504 | ||
108b02cf | 3505 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 3506 | ctx = &cpuctx->ctx; |
c93f7669 | 3507 | get_ctx(ctx); |
fe4b04fa | 3508 | ++ctx->pin_count; |
0793a61d | 3509 | |
0793a61d TG |
3510 | return ctx; |
3511 | } | |
3512 | ||
8dc85d54 PZ |
3513 | err = -EINVAL; |
3514 | ctxn = pmu->task_ctx_nr; | |
3515 | if (ctxn < 0) | |
3516 | goto errout; | |
3517 | ||
4af57ef2 YZ |
3518 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
3519 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); | |
3520 | if (!task_ctx_data) { | |
3521 | err = -ENOMEM; | |
3522 | goto errout; | |
3523 | } | |
3524 | } | |
3525 | ||
9ed6060d | 3526 | retry: |
8dc85d54 | 3527 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 3528 | if (ctx) { |
211de6eb | 3529 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 3530 | ++ctx->pin_count; |
4af57ef2 YZ |
3531 | |
3532 | if (task_ctx_data && !ctx->task_ctx_data) { | |
3533 | ctx->task_ctx_data = task_ctx_data; | |
3534 | task_ctx_data = NULL; | |
3535 | } | |
e625cce1 | 3536 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
3537 | |
3538 | if (clone_ctx) | |
3539 | put_ctx(clone_ctx); | |
9137fb28 | 3540 | } else { |
eb184479 | 3541 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
3542 | err = -ENOMEM; |
3543 | if (!ctx) | |
3544 | goto errout; | |
eb184479 | 3545 | |
4af57ef2 YZ |
3546 | if (task_ctx_data) { |
3547 | ctx->task_ctx_data = task_ctx_data; | |
3548 | task_ctx_data = NULL; | |
3549 | } | |
3550 | ||
dbe08d82 ON |
3551 | err = 0; |
3552 | mutex_lock(&task->perf_event_mutex); | |
3553 | /* | |
3554 | * If it has already passed perf_event_exit_task(). | |
3555 | * we must see PF_EXITING, it takes this mutex too. | |
3556 | */ | |
3557 | if (task->flags & PF_EXITING) | |
3558 | err = -ESRCH; | |
3559 | else if (task->perf_event_ctxp[ctxn]) | |
3560 | err = -EAGAIN; | |
fe4b04fa | 3561 | else { |
9137fb28 | 3562 | get_ctx(ctx); |
fe4b04fa | 3563 | ++ctx->pin_count; |
dbe08d82 | 3564 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 3565 | } |
dbe08d82 ON |
3566 | mutex_unlock(&task->perf_event_mutex); |
3567 | ||
3568 | if (unlikely(err)) { | |
9137fb28 | 3569 | put_ctx(ctx); |
dbe08d82 ON |
3570 | |
3571 | if (err == -EAGAIN) | |
3572 | goto retry; | |
3573 | goto errout; | |
a63eaf34 PM |
3574 | } |
3575 | } | |
3576 | ||
4af57ef2 | 3577 | kfree(task_ctx_data); |
0793a61d | 3578 | return ctx; |
c93f7669 | 3579 | |
9ed6060d | 3580 | errout: |
4af57ef2 | 3581 | kfree(task_ctx_data); |
c93f7669 | 3582 | return ERR_PTR(err); |
0793a61d TG |
3583 | } |
3584 | ||
6fb2915d | 3585 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 3586 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 3587 | |
cdd6c482 | 3588 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 3589 | { |
cdd6c482 | 3590 | struct perf_event *event; |
592903cd | 3591 | |
cdd6c482 IM |
3592 | event = container_of(head, struct perf_event, rcu_head); |
3593 | if (event->ns) | |
3594 | put_pid_ns(event->ns); | |
6fb2915d | 3595 | perf_event_free_filter(event); |
cdd6c482 | 3596 | kfree(event); |
592903cd PZ |
3597 | } |
3598 | ||
b69cf536 PZ |
3599 | static void ring_buffer_attach(struct perf_event *event, |
3600 | struct ring_buffer *rb); | |
925d519a | 3601 | |
4beb31f3 | 3602 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 3603 | { |
4beb31f3 FW |
3604 | if (event->parent) |
3605 | return; | |
3606 | ||
4beb31f3 FW |
3607 | if (is_cgroup_event(event)) |
3608 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
3609 | } | |
925d519a | 3610 | |
555e0c1e FW |
3611 | #ifdef CONFIG_NO_HZ_FULL |
3612 | static DEFINE_SPINLOCK(nr_freq_lock); | |
3613 | #endif | |
3614 | ||
3615 | static void unaccount_freq_event_nohz(void) | |
3616 | { | |
3617 | #ifdef CONFIG_NO_HZ_FULL | |
3618 | spin_lock(&nr_freq_lock); | |
3619 | if (atomic_dec_and_test(&nr_freq_events)) | |
3620 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
3621 | spin_unlock(&nr_freq_lock); | |
3622 | #endif | |
3623 | } | |
3624 | ||
3625 | static void unaccount_freq_event(void) | |
3626 | { | |
3627 | if (tick_nohz_full_enabled()) | |
3628 | unaccount_freq_event_nohz(); | |
3629 | else | |
3630 | atomic_dec(&nr_freq_events); | |
3631 | } | |
3632 | ||
4beb31f3 FW |
3633 | static void unaccount_event(struct perf_event *event) |
3634 | { | |
25432ae9 PZ |
3635 | bool dec = false; |
3636 | ||
4beb31f3 FW |
3637 | if (event->parent) |
3638 | return; | |
3639 | ||
3640 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 3641 | dec = true; |
4beb31f3 FW |
3642 | if (event->attr.mmap || event->attr.mmap_data) |
3643 | atomic_dec(&nr_mmap_events); | |
3644 | if (event->attr.comm) | |
3645 | atomic_dec(&nr_comm_events); | |
3646 | if (event->attr.task) | |
3647 | atomic_dec(&nr_task_events); | |
948b26b6 | 3648 | if (event->attr.freq) |
555e0c1e | 3649 | unaccount_freq_event(); |
45ac1403 | 3650 | if (event->attr.context_switch) { |
25432ae9 | 3651 | dec = true; |
45ac1403 AH |
3652 | atomic_dec(&nr_switch_events); |
3653 | } | |
4beb31f3 | 3654 | if (is_cgroup_event(event)) |
25432ae9 | 3655 | dec = true; |
4beb31f3 | 3656 | if (has_branch_stack(event)) |
25432ae9 PZ |
3657 | dec = true; |
3658 | ||
9107c89e PZ |
3659 | if (dec) { |
3660 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
3661 | schedule_delayed_work(&perf_sched_work, HZ); | |
3662 | } | |
4beb31f3 FW |
3663 | |
3664 | unaccount_event_cpu(event, event->cpu); | |
3665 | } | |
925d519a | 3666 | |
9107c89e PZ |
3667 | static void perf_sched_delayed(struct work_struct *work) |
3668 | { | |
3669 | mutex_lock(&perf_sched_mutex); | |
3670 | if (atomic_dec_and_test(&perf_sched_count)) | |
3671 | static_branch_disable(&perf_sched_events); | |
3672 | mutex_unlock(&perf_sched_mutex); | |
3673 | } | |
3674 | ||
bed5b25a AS |
3675 | /* |
3676 | * The following implement mutual exclusion of events on "exclusive" pmus | |
3677 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
3678 | * at a time, so we disallow creating events that might conflict, namely: | |
3679 | * | |
3680 | * 1) cpu-wide events in the presence of per-task events, | |
3681 | * 2) per-task events in the presence of cpu-wide events, | |
3682 | * 3) two matching events on the same context. | |
3683 | * | |
3684 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 3685 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
3686 | */ |
3687 | static int exclusive_event_init(struct perf_event *event) | |
3688 | { | |
3689 | struct pmu *pmu = event->pmu; | |
3690 | ||
3691 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3692 | return 0; | |
3693 | ||
3694 | /* | |
3695 | * Prevent co-existence of per-task and cpu-wide events on the | |
3696 | * same exclusive pmu. | |
3697 | * | |
3698 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
3699 | * events on this "exclusive" pmu, positive means there are | |
3700 | * per-task events. | |
3701 | * | |
3702 | * Since this is called in perf_event_alloc() path, event::ctx | |
3703 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
3704 | * to mean "per-task event", because unlike other attach states it | |
3705 | * never gets cleared. | |
3706 | */ | |
3707 | if (event->attach_state & PERF_ATTACH_TASK) { | |
3708 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
3709 | return -EBUSY; | |
3710 | } else { | |
3711 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
3712 | return -EBUSY; | |
3713 | } | |
3714 | ||
3715 | return 0; | |
3716 | } | |
3717 | ||
3718 | static void exclusive_event_destroy(struct perf_event *event) | |
3719 | { | |
3720 | struct pmu *pmu = event->pmu; | |
3721 | ||
3722 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3723 | return; | |
3724 | ||
3725 | /* see comment in exclusive_event_init() */ | |
3726 | if (event->attach_state & PERF_ATTACH_TASK) | |
3727 | atomic_dec(&pmu->exclusive_cnt); | |
3728 | else | |
3729 | atomic_inc(&pmu->exclusive_cnt); | |
3730 | } | |
3731 | ||
3732 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
3733 | { | |
3734 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && | |
3735 | (e1->cpu == e2->cpu || | |
3736 | e1->cpu == -1 || | |
3737 | e2->cpu == -1)) | |
3738 | return true; | |
3739 | return false; | |
3740 | } | |
3741 | ||
3742 | /* Called under the same ctx::mutex as perf_install_in_context() */ | |
3743 | static bool exclusive_event_installable(struct perf_event *event, | |
3744 | struct perf_event_context *ctx) | |
3745 | { | |
3746 | struct perf_event *iter_event; | |
3747 | struct pmu *pmu = event->pmu; | |
3748 | ||
3749 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) | |
3750 | return true; | |
3751 | ||
3752 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
3753 | if (exclusive_event_match(iter_event, event)) | |
3754 | return false; | |
3755 | } | |
3756 | ||
3757 | return true; | |
3758 | } | |
3759 | ||
683ede43 | 3760 | static void _free_event(struct perf_event *event) |
f1600952 | 3761 | { |
e360adbe | 3762 | irq_work_sync(&event->pending); |
925d519a | 3763 | |
4beb31f3 | 3764 | unaccount_event(event); |
9ee318a7 | 3765 | |
76369139 | 3766 | if (event->rb) { |
9bb5d40c PZ |
3767 | /* |
3768 | * Can happen when we close an event with re-directed output. | |
3769 | * | |
3770 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
3771 | * over us; possibly making our ring_buffer_put() the last. | |
3772 | */ | |
3773 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 3774 | ring_buffer_attach(event, NULL); |
9bb5d40c | 3775 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
3776 | } |
3777 | ||
e5d1367f SE |
3778 | if (is_cgroup_event(event)) |
3779 | perf_detach_cgroup(event); | |
3780 | ||
a0733e69 PZ |
3781 | if (!event->parent) { |
3782 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
3783 | put_callchain_buffers(); | |
3784 | } | |
3785 | ||
3786 | perf_event_free_bpf_prog(event); | |
3787 | ||
3788 | if (event->destroy) | |
3789 | event->destroy(event); | |
3790 | ||
3791 | if (event->ctx) | |
3792 | put_ctx(event->ctx); | |
3793 | ||
3794 | if (event->pmu) { | |
3795 | exclusive_event_destroy(event); | |
3796 | module_put(event->pmu->module); | |
3797 | } | |
3798 | ||
3799 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
3800 | } |
3801 | ||
683ede43 PZ |
3802 | /* |
3803 | * Used to free events which have a known refcount of 1, such as in error paths | |
3804 | * where the event isn't exposed yet and inherited events. | |
3805 | */ | |
3806 | static void free_event(struct perf_event *event) | |
0793a61d | 3807 | { |
683ede43 PZ |
3808 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
3809 | "unexpected event refcount: %ld; ptr=%p\n", | |
3810 | atomic_long_read(&event->refcount), event)) { | |
3811 | /* leak to avoid use-after-free */ | |
3812 | return; | |
3813 | } | |
0793a61d | 3814 | |
683ede43 | 3815 | _free_event(event); |
0793a61d TG |
3816 | } |
3817 | ||
a66a3052 | 3818 | /* |
f8697762 | 3819 | * Remove user event from the owner task. |
a66a3052 | 3820 | */ |
f8697762 | 3821 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 3822 | { |
8882135b | 3823 | struct task_struct *owner; |
fb0459d7 | 3824 | |
8882135b | 3825 | rcu_read_lock(); |
8882135b | 3826 | /* |
f47c02c0 PZ |
3827 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
3828 | * observe !owner it means the list deletion is complete and we can | |
3829 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
3830 | * owner->perf_event_mutex. |
3831 | */ | |
f47c02c0 | 3832 | owner = lockless_dereference(event->owner); |
8882135b PZ |
3833 | if (owner) { |
3834 | /* | |
3835 | * Since delayed_put_task_struct() also drops the last | |
3836 | * task reference we can safely take a new reference | |
3837 | * while holding the rcu_read_lock(). | |
3838 | */ | |
3839 | get_task_struct(owner); | |
3840 | } | |
3841 | rcu_read_unlock(); | |
3842 | ||
3843 | if (owner) { | |
f63a8daa PZ |
3844 | /* |
3845 | * If we're here through perf_event_exit_task() we're already | |
3846 | * holding ctx->mutex which would be an inversion wrt. the | |
3847 | * normal lock order. | |
3848 | * | |
3849 | * However we can safely take this lock because its the child | |
3850 | * ctx->mutex. | |
3851 | */ | |
3852 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
3853 | ||
8882135b PZ |
3854 | /* |
3855 | * We have to re-check the event->owner field, if it is cleared | |
3856 | * we raced with perf_event_exit_task(), acquiring the mutex | |
3857 | * ensured they're done, and we can proceed with freeing the | |
3858 | * event. | |
3859 | */ | |
f47c02c0 | 3860 | if (event->owner) { |
8882135b | 3861 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
3862 | smp_store_release(&event->owner, NULL); |
3863 | } | |
8882135b PZ |
3864 | mutex_unlock(&owner->perf_event_mutex); |
3865 | put_task_struct(owner); | |
3866 | } | |
f8697762 JO |
3867 | } |
3868 | ||
f8697762 JO |
3869 | static void put_event(struct perf_event *event) |
3870 | { | |
f8697762 JO |
3871 | if (!atomic_long_dec_and_test(&event->refcount)) |
3872 | return; | |
3873 | ||
c6e5b732 PZ |
3874 | _free_event(event); |
3875 | } | |
3876 | ||
3877 | /* | |
3878 | * Kill an event dead; while event:refcount will preserve the event | |
3879 | * object, it will not preserve its functionality. Once the last 'user' | |
3880 | * gives up the object, we'll destroy the thing. | |
3881 | */ | |
3882 | int perf_event_release_kernel(struct perf_event *event) | |
3883 | { | |
a4f4bb6d | 3884 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 PZ |
3885 | struct perf_event *child, *tmp; |
3886 | ||
a4f4bb6d PZ |
3887 | /* |
3888 | * If we got here through err_file: fput(event_file); we will not have | |
3889 | * attached to a context yet. | |
3890 | */ | |
3891 | if (!ctx) { | |
3892 | WARN_ON_ONCE(event->attach_state & | |
3893 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
3894 | goto no_ctx; | |
3895 | } | |
3896 | ||
f8697762 JO |
3897 | if (!is_kernel_event(event)) |
3898 | perf_remove_from_owner(event); | |
8882135b | 3899 | |
5fa7c8ec | 3900 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 3901 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 3902 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 3903 | |
a69b0ca4 | 3904 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 3905 | /* |
a69b0ca4 PZ |
3906 | * Mark this even as STATE_DEAD, there is no external reference to it |
3907 | * anymore. | |
683ede43 | 3908 | * |
a69b0ca4 PZ |
3909 | * Anybody acquiring event->child_mutex after the below loop _must_ |
3910 | * also see this, most importantly inherit_event() which will avoid | |
3911 | * placing more children on the list. | |
683ede43 | 3912 | * |
c6e5b732 PZ |
3913 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
3914 | * child events. | |
683ede43 | 3915 | */ |
a69b0ca4 PZ |
3916 | event->state = PERF_EVENT_STATE_DEAD; |
3917 | raw_spin_unlock_irq(&ctx->lock); | |
3918 | ||
3919 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 3920 | |
c6e5b732 PZ |
3921 | again: |
3922 | mutex_lock(&event->child_mutex); | |
3923 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 3924 | |
c6e5b732 PZ |
3925 | /* |
3926 | * Cannot change, child events are not migrated, see the | |
3927 | * comment with perf_event_ctx_lock_nested(). | |
3928 | */ | |
3929 | ctx = lockless_dereference(child->ctx); | |
3930 | /* | |
3931 | * Since child_mutex nests inside ctx::mutex, we must jump | |
3932 | * through hoops. We start by grabbing a reference on the ctx. | |
3933 | * | |
3934 | * Since the event cannot get freed while we hold the | |
3935 | * child_mutex, the context must also exist and have a !0 | |
3936 | * reference count. | |
3937 | */ | |
3938 | get_ctx(ctx); | |
3939 | ||
3940 | /* | |
3941 | * Now that we have a ctx ref, we can drop child_mutex, and | |
3942 | * acquire ctx::mutex without fear of it going away. Then we | |
3943 | * can re-acquire child_mutex. | |
3944 | */ | |
3945 | mutex_unlock(&event->child_mutex); | |
3946 | mutex_lock(&ctx->mutex); | |
3947 | mutex_lock(&event->child_mutex); | |
3948 | ||
3949 | /* | |
3950 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
3951 | * state, if child is still the first entry, it didn't get freed | |
3952 | * and we can continue doing so. | |
3953 | */ | |
3954 | tmp = list_first_entry_or_null(&event->child_list, | |
3955 | struct perf_event, child_list); | |
3956 | if (tmp == child) { | |
3957 | perf_remove_from_context(child, DETACH_GROUP); | |
3958 | list_del(&child->child_list); | |
3959 | free_event(child); | |
3960 | /* | |
3961 | * This matches the refcount bump in inherit_event(); | |
3962 | * this can't be the last reference. | |
3963 | */ | |
3964 | put_event(event); | |
3965 | } | |
3966 | ||
3967 | mutex_unlock(&event->child_mutex); | |
3968 | mutex_unlock(&ctx->mutex); | |
3969 | put_ctx(ctx); | |
3970 | goto again; | |
3971 | } | |
3972 | mutex_unlock(&event->child_mutex); | |
3973 | ||
a4f4bb6d PZ |
3974 | no_ctx: |
3975 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
3976 | return 0; |
3977 | } | |
3978 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
3979 | ||
8b10c5e2 PZ |
3980 | /* |
3981 | * Called when the last reference to the file is gone. | |
3982 | */ | |
a6fa941d AV |
3983 | static int perf_release(struct inode *inode, struct file *file) |
3984 | { | |
c6e5b732 | 3985 | perf_event_release_kernel(file->private_data); |
a6fa941d | 3986 | return 0; |
fb0459d7 | 3987 | } |
fb0459d7 | 3988 | |
59ed446f | 3989 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 3990 | { |
cdd6c482 | 3991 | struct perf_event *child; |
e53c0994 PZ |
3992 | u64 total = 0; |
3993 | ||
59ed446f PZ |
3994 | *enabled = 0; |
3995 | *running = 0; | |
3996 | ||
6f10581a | 3997 | mutex_lock(&event->child_mutex); |
01add3ea | 3998 | |
7d88962e | 3999 | (void)perf_event_read(event, false); |
01add3ea SB |
4000 | total += perf_event_count(event); |
4001 | ||
59ed446f PZ |
4002 | *enabled += event->total_time_enabled + |
4003 | atomic64_read(&event->child_total_time_enabled); | |
4004 | *running += event->total_time_running + | |
4005 | atomic64_read(&event->child_total_time_running); | |
4006 | ||
4007 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 4008 | (void)perf_event_read(child, false); |
01add3ea | 4009 | total += perf_event_count(child); |
59ed446f PZ |
4010 | *enabled += child->total_time_enabled; |
4011 | *running += child->total_time_running; | |
4012 | } | |
6f10581a | 4013 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
4014 | |
4015 | return total; | |
4016 | } | |
fb0459d7 | 4017 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 4018 | |
7d88962e | 4019 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 4020 | u64 read_format, u64 *values) |
3dab77fb | 4021 | { |
fa8c2693 PZ |
4022 | struct perf_event *sub; |
4023 | int n = 1; /* skip @nr */ | |
7d88962e | 4024 | int ret; |
f63a8daa | 4025 | |
7d88962e SB |
4026 | ret = perf_event_read(leader, true); |
4027 | if (ret) | |
4028 | return ret; | |
abf4868b | 4029 | |
fa8c2693 PZ |
4030 | /* |
4031 | * Since we co-schedule groups, {enabled,running} times of siblings | |
4032 | * will be identical to those of the leader, so we only publish one | |
4033 | * set. | |
4034 | */ | |
4035 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
4036 | values[n++] += leader->total_time_enabled + | |
4037 | atomic64_read(&leader->child_total_time_enabled); | |
4038 | } | |
3dab77fb | 4039 | |
fa8c2693 PZ |
4040 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
4041 | values[n++] += leader->total_time_running + | |
4042 | atomic64_read(&leader->child_total_time_running); | |
4043 | } | |
4044 | ||
4045 | /* | |
4046 | * Write {count,id} tuples for every sibling. | |
4047 | */ | |
4048 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
4049 | if (read_format & PERF_FORMAT_ID) |
4050 | values[n++] = primary_event_id(leader); | |
3dab77fb | 4051 | |
fa8c2693 PZ |
4052 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
4053 | values[n++] += perf_event_count(sub); | |
4054 | if (read_format & PERF_FORMAT_ID) | |
4055 | values[n++] = primary_event_id(sub); | |
4056 | } | |
7d88962e SB |
4057 | |
4058 | return 0; | |
fa8c2693 | 4059 | } |
3dab77fb | 4060 | |
fa8c2693 PZ |
4061 | static int perf_read_group(struct perf_event *event, |
4062 | u64 read_format, char __user *buf) | |
4063 | { | |
4064 | struct perf_event *leader = event->group_leader, *child; | |
4065 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 4066 | int ret; |
fa8c2693 | 4067 | u64 *values; |
3dab77fb | 4068 | |
fa8c2693 | 4069 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 4070 | |
fa8c2693 PZ |
4071 | values = kzalloc(event->read_size, GFP_KERNEL); |
4072 | if (!values) | |
4073 | return -ENOMEM; | |
3dab77fb | 4074 | |
fa8c2693 PZ |
4075 | values[0] = 1 + leader->nr_siblings; |
4076 | ||
4077 | /* | |
4078 | * By locking the child_mutex of the leader we effectively | |
4079 | * lock the child list of all siblings.. XXX explain how. | |
4080 | */ | |
4081 | mutex_lock(&leader->child_mutex); | |
abf4868b | 4082 | |
7d88962e SB |
4083 | ret = __perf_read_group_add(leader, read_format, values); |
4084 | if (ret) | |
4085 | goto unlock; | |
4086 | ||
4087 | list_for_each_entry(child, &leader->child_list, child_list) { | |
4088 | ret = __perf_read_group_add(child, read_format, values); | |
4089 | if (ret) | |
4090 | goto unlock; | |
4091 | } | |
abf4868b | 4092 | |
fa8c2693 | 4093 | mutex_unlock(&leader->child_mutex); |
abf4868b | 4094 | |
7d88962e | 4095 | ret = event->read_size; |
fa8c2693 PZ |
4096 | if (copy_to_user(buf, values, event->read_size)) |
4097 | ret = -EFAULT; | |
7d88962e | 4098 | goto out; |
fa8c2693 | 4099 | |
7d88962e SB |
4100 | unlock: |
4101 | mutex_unlock(&leader->child_mutex); | |
4102 | out: | |
fa8c2693 | 4103 | kfree(values); |
abf4868b | 4104 | return ret; |
3dab77fb PZ |
4105 | } |
4106 | ||
b15f495b | 4107 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
4108 | u64 read_format, char __user *buf) |
4109 | { | |
59ed446f | 4110 | u64 enabled, running; |
3dab77fb PZ |
4111 | u64 values[4]; |
4112 | int n = 0; | |
4113 | ||
59ed446f PZ |
4114 | values[n++] = perf_event_read_value(event, &enabled, &running); |
4115 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
4116 | values[n++] = enabled; | |
4117 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
4118 | values[n++] = running; | |
3dab77fb | 4119 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 4120 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
4121 | |
4122 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
4123 | return -EFAULT; | |
4124 | ||
4125 | return n * sizeof(u64); | |
4126 | } | |
4127 | ||
dc633982 JO |
4128 | static bool is_event_hup(struct perf_event *event) |
4129 | { | |
4130 | bool no_children; | |
4131 | ||
a69b0ca4 | 4132 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
4133 | return false; |
4134 | ||
4135 | mutex_lock(&event->child_mutex); | |
4136 | no_children = list_empty(&event->child_list); | |
4137 | mutex_unlock(&event->child_mutex); | |
4138 | return no_children; | |
4139 | } | |
4140 | ||
0793a61d | 4141 | /* |
cdd6c482 | 4142 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
4143 | */ |
4144 | static ssize_t | |
b15f495b | 4145 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 4146 | { |
cdd6c482 | 4147 | u64 read_format = event->attr.read_format; |
3dab77fb | 4148 | int ret; |
0793a61d | 4149 | |
3b6f9e5c | 4150 | /* |
cdd6c482 | 4151 | * Return end-of-file for a read on a event that is in |
3b6f9e5c PM |
4152 | * error state (i.e. because it was pinned but it couldn't be |
4153 | * scheduled on to the CPU at some point). | |
4154 | */ | |
cdd6c482 | 4155 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
4156 | return 0; |
4157 | ||
c320c7b7 | 4158 | if (count < event->read_size) |
3dab77fb PZ |
4159 | return -ENOSPC; |
4160 | ||
cdd6c482 | 4161 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 4162 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 4163 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 4164 | else |
b15f495b | 4165 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 4166 | |
3dab77fb | 4167 | return ret; |
0793a61d TG |
4168 | } |
4169 | ||
0793a61d TG |
4170 | static ssize_t |
4171 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
4172 | { | |
cdd6c482 | 4173 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
4174 | struct perf_event_context *ctx; |
4175 | int ret; | |
0793a61d | 4176 | |
f63a8daa | 4177 | ctx = perf_event_ctx_lock(event); |
b15f495b | 4178 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
4179 | perf_event_ctx_unlock(event, ctx); |
4180 | ||
4181 | return ret; | |
0793a61d TG |
4182 | } |
4183 | ||
4184 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
4185 | { | |
cdd6c482 | 4186 | struct perf_event *event = file->private_data; |
76369139 | 4187 | struct ring_buffer *rb; |
61b67684 | 4188 | unsigned int events = POLLHUP; |
c7138f37 | 4189 | |
e708d7ad | 4190 | poll_wait(file, &event->waitq, wait); |
179033b3 | 4191 | |
dc633982 | 4192 | if (is_event_hup(event)) |
179033b3 | 4193 | return events; |
c7138f37 | 4194 | |
10c6db11 | 4195 | /* |
9bb5d40c PZ |
4196 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
4197 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
4198 | */ |
4199 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
4200 | rb = event->rb; |
4201 | if (rb) | |
76369139 | 4202 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 4203 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
4204 | return events; |
4205 | } | |
4206 | ||
f63a8daa | 4207 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 4208 | { |
7d88962e | 4209 | (void)perf_event_read(event, false); |
e7850595 | 4210 | local64_set(&event->count, 0); |
cdd6c482 | 4211 | perf_event_update_userpage(event); |
3df5edad PZ |
4212 | } |
4213 | ||
c93f7669 | 4214 | /* |
cdd6c482 IM |
4215 | * Holding the top-level event's child_mutex means that any |
4216 | * descendant process that has inherited this event will block | |
8ba289b8 | 4217 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 4218 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 4219 | */ |
cdd6c482 IM |
4220 | static void perf_event_for_each_child(struct perf_event *event, |
4221 | void (*func)(struct perf_event *)) | |
3df5edad | 4222 | { |
cdd6c482 | 4223 | struct perf_event *child; |
3df5edad | 4224 | |
cdd6c482 | 4225 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 4226 | |
cdd6c482 IM |
4227 | mutex_lock(&event->child_mutex); |
4228 | func(event); | |
4229 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 4230 | func(child); |
cdd6c482 | 4231 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
4232 | } |
4233 | ||
cdd6c482 IM |
4234 | static void perf_event_for_each(struct perf_event *event, |
4235 | void (*func)(struct perf_event *)) | |
3df5edad | 4236 | { |
cdd6c482 IM |
4237 | struct perf_event_context *ctx = event->ctx; |
4238 | struct perf_event *sibling; | |
3df5edad | 4239 | |
f63a8daa PZ |
4240 | lockdep_assert_held(&ctx->mutex); |
4241 | ||
cdd6c482 | 4242 | event = event->group_leader; |
75f937f2 | 4243 | |
cdd6c482 | 4244 | perf_event_for_each_child(event, func); |
cdd6c482 | 4245 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
724b6daa | 4246 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
4247 | } |
4248 | ||
fae3fde6 PZ |
4249 | static void __perf_event_period(struct perf_event *event, |
4250 | struct perf_cpu_context *cpuctx, | |
4251 | struct perf_event_context *ctx, | |
4252 | void *info) | |
c7999c6f | 4253 | { |
fae3fde6 | 4254 | u64 value = *((u64 *)info); |
c7999c6f | 4255 | bool active; |
08247e31 | 4256 | |
cdd6c482 | 4257 | if (event->attr.freq) { |
cdd6c482 | 4258 | event->attr.sample_freq = value; |
08247e31 | 4259 | } else { |
cdd6c482 IM |
4260 | event->attr.sample_period = value; |
4261 | event->hw.sample_period = value; | |
08247e31 | 4262 | } |
bad7192b PZ |
4263 | |
4264 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
4265 | if (active) { | |
4266 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
4267 | /* |
4268 | * We could be throttled; unthrottle now to avoid the tick | |
4269 | * trying to unthrottle while we already re-started the event. | |
4270 | */ | |
4271 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
4272 | event->hw.interrupts = 0; | |
4273 | perf_log_throttle(event, 1); | |
4274 | } | |
bad7192b PZ |
4275 | event->pmu->stop(event, PERF_EF_UPDATE); |
4276 | } | |
4277 | ||
4278 | local64_set(&event->hw.period_left, 0); | |
4279 | ||
4280 | if (active) { | |
4281 | event->pmu->start(event, PERF_EF_RELOAD); | |
4282 | perf_pmu_enable(ctx->pmu); | |
4283 | } | |
c7999c6f PZ |
4284 | } |
4285 | ||
4286 | static int perf_event_period(struct perf_event *event, u64 __user *arg) | |
4287 | { | |
c7999c6f PZ |
4288 | u64 value; |
4289 | ||
4290 | if (!is_sampling_event(event)) | |
4291 | return -EINVAL; | |
4292 | ||
4293 | if (copy_from_user(&value, arg, sizeof(value))) | |
4294 | return -EFAULT; | |
4295 | ||
4296 | if (!value) | |
4297 | return -EINVAL; | |
4298 | ||
4299 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
4300 | return -EINVAL; | |
4301 | ||
fae3fde6 | 4302 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 4303 | |
c7999c6f | 4304 | return 0; |
08247e31 PZ |
4305 | } |
4306 | ||
ac9721f3 PZ |
4307 | static const struct file_operations perf_fops; |
4308 | ||
2903ff01 | 4309 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 4310 | { |
2903ff01 AV |
4311 | struct fd f = fdget(fd); |
4312 | if (!f.file) | |
4313 | return -EBADF; | |
ac9721f3 | 4314 | |
2903ff01 AV |
4315 | if (f.file->f_op != &perf_fops) { |
4316 | fdput(f); | |
4317 | return -EBADF; | |
ac9721f3 | 4318 | } |
2903ff01 AV |
4319 | *p = f; |
4320 | return 0; | |
ac9721f3 PZ |
4321 | } |
4322 | ||
4323 | static int perf_event_set_output(struct perf_event *event, | |
4324 | struct perf_event *output_event); | |
6fb2915d | 4325 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 4326 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
a4be7c27 | 4327 | |
f63a8daa | 4328 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 4329 | { |
cdd6c482 | 4330 | void (*func)(struct perf_event *); |
3df5edad | 4331 | u32 flags = arg; |
d859e29f PM |
4332 | |
4333 | switch (cmd) { | |
cdd6c482 | 4334 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 4335 | func = _perf_event_enable; |
d859e29f | 4336 | break; |
cdd6c482 | 4337 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 4338 | func = _perf_event_disable; |
79f14641 | 4339 | break; |
cdd6c482 | 4340 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 4341 | func = _perf_event_reset; |
6de6a7b9 | 4342 | break; |
3df5edad | 4343 | |
cdd6c482 | 4344 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 4345 | return _perf_event_refresh(event, arg); |
08247e31 | 4346 | |
cdd6c482 IM |
4347 | case PERF_EVENT_IOC_PERIOD: |
4348 | return perf_event_period(event, (u64 __user *)arg); | |
08247e31 | 4349 | |
cf4957f1 JO |
4350 | case PERF_EVENT_IOC_ID: |
4351 | { | |
4352 | u64 id = primary_event_id(event); | |
4353 | ||
4354 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
4355 | return -EFAULT; | |
4356 | return 0; | |
4357 | } | |
4358 | ||
cdd6c482 | 4359 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 4360 | { |
ac9721f3 | 4361 | int ret; |
ac9721f3 | 4362 | if (arg != -1) { |
2903ff01 AV |
4363 | struct perf_event *output_event; |
4364 | struct fd output; | |
4365 | ret = perf_fget_light(arg, &output); | |
4366 | if (ret) | |
4367 | return ret; | |
4368 | output_event = output.file->private_data; | |
4369 | ret = perf_event_set_output(event, output_event); | |
4370 | fdput(output); | |
4371 | } else { | |
4372 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 4373 | } |
ac9721f3 PZ |
4374 | return ret; |
4375 | } | |
a4be7c27 | 4376 | |
6fb2915d LZ |
4377 | case PERF_EVENT_IOC_SET_FILTER: |
4378 | return perf_event_set_filter(event, (void __user *)arg); | |
4379 | ||
2541517c AS |
4380 | case PERF_EVENT_IOC_SET_BPF: |
4381 | return perf_event_set_bpf_prog(event, arg); | |
4382 | ||
86e7972f WN |
4383 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
4384 | struct ring_buffer *rb; | |
4385 | ||
4386 | rcu_read_lock(); | |
4387 | rb = rcu_dereference(event->rb); | |
4388 | if (!rb || !rb->nr_pages) { | |
4389 | rcu_read_unlock(); | |
4390 | return -EINVAL; | |
4391 | } | |
4392 | rb_toggle_paused(rb, !!arg); | |
4393 | rcu_read_unlock(); | |
4394 | return 0; | |
4395 | } | |
d859e29f | 4396 | default: |
3df5edad | 4397 | return -ENOTTY; |
d859e29f | 4398 | } |
3df5edad PZ |
4399 | |
4400 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 4401 | perf_event_for_each(event, func); |
3df5edad | 4402 | else |
cdd6c482 | 4403 | perf_event_for_each_child(event, func); |
3df5edad PZ |
4404 | |
4405 | return 0; | |
d859e29f PM |
4406 | } |
4407 | ||
f63a8daa PZ |
4408 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4409 | { | |
4410 | struct perf_event *event = file->private_data; | |
4411 | struct perf_event_context *ctx; | |
4412 | long ret; | |
4413 | ||
4414 | ctx = perf_event_ctx_lock(event); | |
4415 | ret = _perf_ioctl(event, cmd, arg); | |
4416 | perf_event_ctx_unlock(event, ctx); | |
4417 | ||
4418 | return ret; | |
4419 | } | |
4420 | ||
b3f20785 PM |
4421 | #ifdef CONFIG_COMPAT |
4422 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
4423 | unsigned long arg) | |
4424 | { | |
4425 | switch (_IOC_NR(cmd)) { | |
4426 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
4427 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
4428 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ | |
4429 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
4430 | cmd &= ~IOCSIZE_MASK; | |
4431 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
4432 | } | |
4433 | break; | |
4434 | } | |
4435 | return perf_ioctl(file, cmd, arg); | |
4436 | } | |
4437 | #else | |
4438 | # define perf_compat_ioctl NULL | |
4439 | #endif | |
4440 | ||
cdd6c482 | 4441 | int perf_event_task_enable(void) |
771d7cde | 4442 | { |
f63a8daa | 4443 | struct perf_event_context *ctx; |
cdd6c482 | 4444 | struct perf_event *event; |
771d7cde | 4445 | |
cdd6c482 | 4446 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4447 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4448 | ctx = perf_event_ctx_lock(event); | |
4449 | perf_event_for_each_child(event, _perf_event_enable); | |
4450 | perf_event_ctx_unlock(event, ctx); | |
4451 | } | |
cdd6c482 | 4452 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4453 | |
4454 | return 0; | |
4455 | } | |
4456 | ||
cdd6c482 | 4457 | int perf_event_task_disable(void) |
771d7cde | 4458 | { |
f63a8daa | 4459 | struct perf_event_context *ctx; |
cdd6c482 | 4460 | struct perf_event *event; |
771d7cde | 4461 | |
cdd6c482 | 4462 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
4463 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
4464 | ctx = perf_event_ctx_lock(event); | |
4465 | perf_event_for_each_child(event, _perf_event_disable); | |
4466 | perf_event_ctx_unlock(event, ctx); | |
4467 | } | |
cdd6c482 | 4468 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
4469 | |
4470 | return 0; | |
4471 | } | |
4472 | ||
cdd6c482 | 4473 | static int perf_event_index(struct perf_event *event) |
194002b2 | 4474 | { |
a4eaf7f1 PZ |
4475 | if (event->hw.state & PERF_HES_STOPPED) |
4476 | return 0; | |
4477 | ||
cdd6c482 | 4478 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
4479 | return 0; |
4480 | ||
35edc2a5 | 4481 | return event->pmu->event_idx(event); |
194002b2 PZ |
4482 | } |
4483 | ||
c4794295 | 4484 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 4485 | u64 *now, |
7f310a5d EM |
4486 | u64 *enabled, |
4487 | u64 *running) | |
c4794295 | 4488 | { |
e3f3541c | 4489 | u64 ctx_time; |
c4794295 | 4490 | |
e3f3541c PZ |
4491 | *now = perf_clock(); |
4492 | ctx_time = event->shadow_ctx_time + *now; | |
c4794295 EM |
4493 | *enabled = ctx_time - event->tstamp_enabled; |
4494 | *running = ctx_time - event->tstamp_running; | |
4495 | } | |
4496 | ||
fa731587 PZ |
4497 | static void perf_event_init_userpage(struct perf_event *event) |
4498 | { | |
4499 | struct perf_event_mmap_page *userpg; | |
4500 | struct ring_buffer *rb; | |
4501 | ||
4502 | rcu_read_lock(); | |
4503 | rb = rcu_dereference(event->rb); | |
4504 | if (!rb) | |
4505 | goto unlock; | |
4506 | ||
4507 | userpg = rb->user_page; | |
4508 | ||
4509 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
4510 | userpg->cap_bit0_is_deprecated = 1; | |
4511 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
4512 | userpg->data_offset = PAGE_SIZE; |
4513 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
4514 | |
4515 | unlock: | |
4516 | rcu_read_unlock(); | |
4517 | } | |
4518 | ||
c1317ec2 AL |
4519 | void __weak arch_perf_update_userpage( |
4520 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
4521 | { |
4522 | } | |
4523 | ||
38ff667b PZ |
4524 | /* |
4525 | * Callers need to ensure there can be no nesting of this function, otherwise | |
4526 | * the seqlock logic goes bad. We can not serialize this because the arch | |
4527 | * code calls this from NMI context. | |
4528 | */ | |
cdd6c482 | 4529 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 4530 | { |
cdd6c482 | 4531 | struct perf_event_mmap_page *userpg; |
76369139 | 4532 | struct ring_buffer *rb; |
e3f3541c | 4533 | u64 enabled, running, now; |
38ff667b PZ |
4534 | |
4535 | rcu_read_lock(); | |
5ec4c599 PZ |
4536 | rb = rcu_dereference(event->rb); |
4537 | if (!rb) | |
4538 | goto unlock; | |
4539 | ||
0d641208 EM |
4540 | /* |
4541 | * compute total_time_enabled, total_time_running | |
4542 | * based on snapshot values taken when the event | |
4543 | * was last scheduled in. | |
4544 | * | |
4545 | * we cannot simply called update_context_time() | |
4546 | * because of locking issue as we can be called in | |
4547 | * NMI context | |
4548 | */ | |
e3f3541c | 4549 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 4550 | |
76369139 | 4551 | userpg = rb->user_page; |
7b732a75 PZ |
4552 | /* |
4553 | * Disable preemption so as to not let the corresponding user-space | |
4554 | * spin too long if we get preempted. | |
4555 | */ | |
4556 | preempt_disable(); | |
37d81828 | 4557 | ++userpg->lock; |
92f22a38 | 4558 | barrier(); |
cdd6c482 | 4559 | userpg->index = perf_event_index(event); |
b5e58793 | 4560 | userpg->offset = perf_event_count(event); |
365a4038 | 4561 | if (userpg->index) |
e7850595 | 4562 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 4563 | |
0d641208 | 4564 | userpg->time_enabled = enabled + |
cdd6c482 | 4565 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 4566 | |
0d641208 | 4567 | userpg->time_running = running + |
cdd6c482 | 4568 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 4569 | |
c1317ec2 | 4570 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 4571 | |
92f22a38 | 4572 | barrier(); |
37d81828 | 4573 | ++userpg->lock; |
7b732a75 | 4574 | preempt_enable(); |
38ff667b | 4575 | unlock: |
7b732a75 | 4576 | rcu_read_unlock(); |
37d81828 PM |
4577 | } |
4578 | ||
906010b2 PZ |
4579 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
4580 | { | |
4581 | struct perf_event *event = vma->vm_file->private_data; | |
76369139 | 4582 | struct ring_buffer *rb; |
906010b2 PZ |
4583 | int ret = VM_FAULT_SIGBUS; |
4584 | ||
4585 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
4586 | if (vmf->pgoff == 0) | |
4587 | ret = 0; | |
4588 | return ret; | |
4589 | } | |
4590 | ||
4591 | rcu_read_lock(); | |
76369139 FW |
4592 | rb = rcu_dereference(event->rb); |
4593 | if (!rb) | |
906010b2 PZ |
4594 | goto unlock; |
4595 | ||
4596 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
4597 | goto unlock; | |
4598 | ||
76369139 | 4599 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
4600 | if (!vmf->page) |
4601 | goto unlock; | |
4602 | ||
4603 | get_page(vmf->page); | |
4604 | vmf->page->mapping = vma->vm_file->f_mapping; | |
4605 | vmf->page->index = vmf->pgoff; | |
4606 | ||
4607 | ret = 0; | |
4608 | unlock: | |
4609 | rcu_read_unlock(); | |
4610 | ||
4611 | return ret; | |
4612 | } | |
4613 | ||
10c6db11 PZ |
4614 | static void ring_buffer_attach(struct perf_event *event, |
4615 | struct ring_buffer *rb) | |
4616 | { | |
b69cf536 | 4617 | struct ring_buffer *old_rb = NULL; |
10c6db11 PZ |
4618 | unsigned long flags; |
4619 | ||
b69cf536 PZ |
4620 | if (event->rb) { |
4621 | /* | |
4622 | * Should be impossible, we set this when removing | |
4623 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
4624 | */ | |
4625 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 4626 | |
b69cf536 | 4627 | old_rb = event->rb; |
b69cf536 PZ |
4628 | spin_lock_irqsave(&old_rb->event_lock, flags); |
4629 | list_del_rcu(&event->rb_entry); | |
4630 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 4631 | |
2f993cf0 ON |
4632 | event->rcu_batches = get_state_synchronize_rcu(); |
4633 | event->rcu_pending = 1; | |
b69cf536 | 4634 | } |
10c6db11 | 4635 | |
b69cf536 | 4636 | if (rb) { |
2f993cf0 ON |
4637 | if (event->rcu_pending) { |
4638 | cond_synchronize_rcu(event->rcu_batches); | |
4639 | event->rcu_pending = 0; | |
4640 | } | |
4641 | ||
b69cf536 PZ |
4642 | spin_lock_irqsave(&rb->event_lock, flags); |
4643 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
4644 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
4645 | } | |
4646 | ||
4647 | rcu_assign_pointer(event->rb, rb); | |
4648 | ||
4649 | if (old_rb) { | |
4650 | ring_buffer_put(old_rb); | |
4651 | /* | |
4652 | * Since we detached before setting the new rb, so that we | |
4653 | * could attach the new rb, we could have missed a wakeup. | |
4654 | * Provide it now. | |
4655 | */ | |
4656 | wake_up_all(&event->waitq); | |
4657 | } | |
10c6db11 PZ |
4658 | } |
4659 | ||
4660 | static void ring_buffer_wakeup(struct perf_event *event) | |
4661 | { | |
4662 | struct ring_buffer *rb; | |
4663 | ||
4664 | rcu_read_lock(); | |
4665 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
4666 | if (rb) { |
4667 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
4668 | wake_up_all(&event->waitq); | |
4669 | } | |
10c6db11 PZ |
4670 | rcu_read_unlock(); |
4671 | } | |
4672 | ||
fdc26706 | 4673 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 4674 | { |
76369139 | 4675 | struct ring_buffer *rb; |
7b732a75 | 4676 | |
ac9721f3 | 4677 | rcu_read_lock(); |
76369139 FW |
4678 | rb = rcu_dereference(event->rb); |
4679 | if (rb) { | |
4680 | if (!atomic_inc_not_zero(&rb->refcount)) | |
4681 | rb = NULL; | |
ac9721f3 PZ |
4682 | } |
4683 | rcu_read_unlock(); | |
4684 | ||
76369139 | 4685 | return rb; |
ac9721f3 PZ |
4686 | } |
4687 | ||
fdc26706 | 4688 | void ring_buffer_put(struct ring_buffer *rb) |
ac9721f3 | 4689 | { |
76369139 | 4690 | if (!atomic_dec_and_test(&rb->refcount)) |
ac9721f3 | 4691 | return; |
7b732a75 | 4692 | |
9bb5d40c | 4693 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 4694 | |
76369139 | 4695 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
4696 | } |
4697 | ||
4698 | static void perf_mmap_open(struct vm_area_struct *vma) | |
4699 | { | |
cdd6c482 | 4700 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4701 | |
cdd6c482 | 4702 | atomic_inc(&event->mmap_count); |
9bb5d40c | 4703 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 4704 | |
45bfb2e5 PZ |
4705 | if (vma->vm_pgoff) |
4706 | atomic_inc(&event->rb->aux_mmap_count); | |
4707 | ||
1e0fb9ec AL |
4708 | if (event->pmu->event_mapped) |
4709 | event->pmu->event_mapped(event); | |
7b732a75 PZ |
4710 | } |
4711 | ||
95ff4ca2 AS |
4712 | static void perf_pmu_output_stop(struct perf_event *event); |
4713 | ||
9bb5d40c PZ |
4714 | /* |
4715 | * A buffer can be mmap()ed multiple times; either directly through the same | |
4716 | * event, or through other events by use of perf_event_set_output(). | |
4717 | * | |
4718 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
4719 | * the buffer here, where we still have a VM context. This means we need | |
4720 | * to detach all events redirecting to us. | |
4721 | */ | |
7b732a75 PZ |
4722 | static void perf_mmap_close(struct vm_area_struct *vma) |
4723 | { | |
cdd6c482 | 4724 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 4725 | |
b69cf536 | 4726 | struct ring_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
4727 | struct user_struct *mmap_user = rb->mmap_user; |
4728 | int mmap_locked = rb->mmap_locked; | |
4729 | unsigned long size = perf_data_size(rb); | |
789f90fc | 4730 | |
1e0fb9ec AL |
4731 | if (event->pmu->event_unmapped) |
4732 | event->pmu->event_unmapped(event); | |
4733 | ||
45bfb2e5 PZ |
4734 | /* |
4735 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
4736 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
4737 | * serialize with perf_mmap here. | |
4738 | */ | |
4739 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
4740 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
4741 | /* |
4742 | * Stop all AUX events that are writing to this buffer, | |
4743 | * so that we can free its AUX pages and corresponding PMU | |
4744 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
4745 | * they won't start any more (see perf_aux_output_begin()). | |
4746 | */ | |
4747 | perf_pmu_output_stop(event); | |
4748 | ||
4749 | /* now it's safe to free the pages */ | |
45bfb2e5 PZ |
4750 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
4751 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; | |
4752 | ||
95ff4ca2 | 4753 | /* this has to be the last one */ |
45bfb2e5 | 4754 | rb_free_aux(rb); |
95ff4ca2 AS |
4755 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
4756 | ||
45bfb2e5 PZ |
4757 | mutex_unlock(&event->mmap_mutex); |
4758 | } | |
4759 | ||
9bb5d40c PZ |
4760 | atomic_dec(&rb->mmap_count); |
4761 | ||
4762 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 4763 | goto out_put; |
9bb5d40c | 4764 | |
b69cf536 | 4765 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
4766 | mutex_unlock(&event->mmap_mutex); |
4767 | ||
4768 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
4769 | if (atomic_read(&rb->mmap_count)) |
4770 | goto out_put; | |
ac9721f3 | 4771 | |
9bb5d40c PZ |
4772 | /* |
4773 | * No other mmap()s, detach from all other events that might redirect | |
4774 | * into the now unreachable buffer. Somewhat complicated by the | |
4775 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
4776 | */ | |
4777 | again: | |
4778 | rcu_read_lock(); | |
4779 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
4780 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
4781 | /* | |
4782 | * This event is en-route to free_event() which will | |
4783 | * detach it and remove it from the list. | |
4784 | */ | |
4785 | continue; | |
4786 | } | |
4787 | rcu_read_unlock(); | |
789f90fc | 4788 | |
9bb5d40c PZ |
4789 | mutex_lock(&event->mmap_mutex); |
4790 | /* | |
4791 | * Check we didn't race with perf_event_set_output() which can | |
4792 | * swizzle the rb from under us while we were waiting to | |
4793 | * acquire mmap_mutex. | |
4794 | * | |
4795 | * If we find a different rb; ignore this event, a next | |
4796 | * iteration will no longer find it on the list. We have to | |
4797 | * still restart the iteration to make sure we're not now | |
4798 | * iterating the wrong list. | |
4799 | */ | |
b69cf536 PZ |
4800 | if (event->rb == rb) |
4801 | ring_buffer_attach(event, NULL); | |
4802 | ||
cdd6c482 | 4803 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 4804 | put_event(event); |
ac9721f3 | 4805 | |
9bb5d40c PZ |
4806 | /* |
4807 | * Restart the iteration; either we're on the wrong list or | |
4808 | * destroyed its integrity by doing a deletion. | |
4809 | */ | |
4810 | goto again; | |
7b732a75 | 4811 | } |
9bb5d40c PZ |
4812 | rcu_read_unlock(); |
4813 | ||
4814 | /* | |
4815 | * It could be there's still a few 0-ref events on the list; they'll | |
4816 | * get cleaned up by free_event() -- they'll also still have their | |
4817 | * ref on the rb and will free it whenever they are done with it. | |
4818 | * | |
4819 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
4820 | * undo the VM accounting. | |
4821 | */ | |
4822 | ||
4823 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); | |
4824 | vma->vm_mm->pinned_vm -= mmap_locked; | |
4825 | free_uid(mmap_user); | |
4826 | ||
b69cf536 | 4827 | out_put: |
9bb5d40c | 4828 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
4829 | } |
4830 | ||
f0f37e2f | 4831 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 4832 | .open = perf_mmap_open, |
45bfb2e5 | 4833 | .close = perf_mmap_close, /* non mergable */ |
43a21ea8 PZ |
4834 | .fault = perf_mmap_fault, |
4835 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
4836 | }; |
4837 | ||
4838 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
4839 | { | |
cdd6c482 | 4840 | struct perf_event *event = file->private_data; |
22a4f650 | 4841 | unsigned long user_locked, user_lock_limit; |
789f90fc | 4842 | struct user_struct *user = current_user(); |
22a4f650 | 4843 | unsigned long locked, lock_limit; |
45bfb2e5 | 4844 | struct ring_buffer *rb = NULL; |
7b732a75 PZ |
4845 | unsigned long vma_size; |
4846 | unsigned long nr_pages; | |
45bfb2e5 | 4847 | long user_extra = 0, extra = 0; |
d57e34fd | 4848 | int ret = 0, flags = 0; |
37d81828 | 4849 | |
c7920614 PZ |
4850 | /* |
4851 | * Don't allow mmap() of inherited per-task counters. This would | |
4852 | * create a performance issue due to all children writing to the | |
76369139 | 4853 | * same rb. |
c7920614 PZ |
4854 | */ |
4855 | if (event->cpu == -1 && event->attr.inherit) | |
4856 | return -EINVAL; | |
4857 | ||
43a21ea8 | 4858 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 4859 | return -EINVAL; |
7b732a75 PZ |
4860 | |
4861 | vma_size = vma->vm_end - vma->vm_start; | |
45bfb2e5 PZ |
4862 | |
4863 | if (vma->vm_pgoff == 0) { | |
4864 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
4865 | } else { | |
4866 | /* | |
4867 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
4868 | * mapped, all subsequent mappings should have the same size | |
4869 | * and offset. Must be above the normal perf buffer. | |
4870 | */ | |
4871 | u64 aux_offset, aux_size; | |
4872 | ||
4873 | if (!event->rb) | |
4874 | return -EINVAL; | |
4875 | ||
4876 | nr_pages = vma_size / PAGE_SIZE; | |
4877 | ||
4878 | mutex_lock(&event->mmap_mutex); | |
4879 | ret = -EINVAL; | |
4880 | ||
4881 | rb = event->rb; | |
4882 | if (!rb) | |
4883 | goto aux_unlock; | |
4884 | ||
4885 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); | |
4886 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); | |
4887 | ||
4888 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
4889 | goto aux_unlock; | |
4890 | ||
4891 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
4892 | goto aux_unlock; | |
4893 | ||
4894 | /* already mapped with a different offset */ | |
4895 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
4896 | goto aux_unlock; | |
4897 | ||
4898 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
4899 | goto aux_unlock; | |
4900 | ||
4901 | /* already mapped with a different size */ | |
4902 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
4903 | goto aux_unlock; | |
4904 | ||
4905 | if (!is_power_of_2(nr_pages)) | |
4906 | goto aux_unlock; | |
4907 | ||
4908 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
4909 | goto aux_unlock; | |
4910 | ||
4911 | if (rb_has_aux(rb)) { | |
4912 | atomic_inc(&rb->aux_mmap_count); | |
4913 | ret = 0; | |
4914 | goto unlock; | |
4915 | } | |
4916 | ||
4917 | atomic_set(&rb->aux_mmap_count, 1); | |
4918 | user_extra = nr_pages; | |
4919 | ||
4920 | goto accounting; | |
4921 | } | |
7b732a75 | 4922 | |
7730d865 | 4923 | /* |
76369139 | 4924 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
4925 | * can do bitmasks instead of modulo. |
4926 | */ | |
2ed11312 | 4927 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
4928 | return -EINVAL; |
4929 | ||
7b732a75 | 4930 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
4931 | return -EINVAL; |
4932 | ||
cdd6c482 | 4933 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 4934 | again: |
cdd6c482 | 4935 | mutex_lock(&event->mmap_mutex); |
76369139 | 4936 | if (event->rb) { |
9bb5d40c | 4937 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 4938 | ret = -EINVAL; |
9bb5d40c PZ |
4939 | goto unlock; |
4940 | } | |
4941 | ||
4942 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
4943 | /* | |
4944 | * Raced against perf_mmap_close() through | |
4945 | * perf_event_set_output(). Try again, hope for better | |
4946 | * luck. | |
4947 | */ | |
4948 | mutex_unlock(&event->mmap_mutex); | |
4949 | goto again; | |
4950 | } | |
4951 | ||
ebb3c4c4 PZ |
4952 | goto unlock; |
4953 | } | |
4954 | ||
789f90fc | 4955 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
4956 | |
4957 | accounting: | |
cdd6c482 | 4958 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
4959 | |
4960 | /* | |
4961 | * Increase the limit linearly with more CPUs: | |
4962 | */ | |
4963 | user_lock_limit *= num_online_cpus(); | |
4964 | ||
789f90fc | 4965 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
c5078f78 | 4966 | |
789f90fc PZ |
4967 | if (user_locked > user_lock_limit) |
4968 | extra = user_locked - user_lock_limit; | |
7b732a75 | 4969 | |
78d7d407 | 4970 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 4971 | lock_limit >>= PAGE_SHIFT; |
bc3e53f6 | 4972 | locked = vma->vm_mm->pinned_vm + extra; |
7b732a75 | 4973 | |
459ec28a IM |
4974 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
4975 | !capable(CAP_IPC_LOCK)) { | |
ebb3c4c4 PZ |
4976 | ret = -EPERM; |
4977 | goto unlock; | |
4978 | } | |
7b732a75 | 4979 | |
45bfb2e5 | 4980 | WARN_ON(!rb && event->rb); |
906010b2 | 4981 | |
d57e34fd | 4982 | if (vma->vm_flags & VM_WRITE) |
76369139 | 4983 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 4984 | |
76369139 | 4985 | if (!rb) { |
45bfb2e5 PZ |
4986 | rb = rb_alloc(nr_pages, |
4987 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
4988 | event->cpu, flags); | |
26cb63ad | 4989 | |
45bfb2e5 PZ |
4990 | if (!rb) { |
4991 | ret = -ENOMEM; | |
4992 | goto unlock; | |
4993 | } | |
43a21ea8 | 4994 | |
45bfb2e5 PZ |
4995 | atomic_set(&rb->mmap_count, 1); |
4996 | rb->mmap_user = get_current_user(); | |
4997 | rb->mmap_locked = extra; | |
26cb63ad | 4998 | |
45bfb2e5 | 4999 | ring_buffer_attach(event, rb); |
ac9721f3 | 5000 | |
45bfb2e5 PZ |
5001 | perf_event_init_userpage(event); |
5002 | perf_event_update_userpage(event); | |
5003 | } else { | |
1a594131 AS |
5004 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
5005 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
5006 | if (!ret) |
5007 | rb->aux_mmap_locked = extra; | |
5008 | } | |
9a0f05cb | 5009 | |
ebb3c4c4 | 5010 | unlock: |
45bfb2e5 PZ |
5011 | if (!ret) { |
5012 | atomic_long_add(user_extra, &user->locked_vm); | |
5013 | vma->vm_mm->pinned_vm += extra; | |
5014 | ||
ac9721f3 | 5015 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
5016 | } else if (rb) { |
5017 | atomic_dec(&rb->mmap_count); | |
5018 | } | |
5019 | aux_unlock: | |
cdd6c482 | 5020 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 5021 | |
9bb5d40c PZ |
5022 | /* |
5023 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
5024 | * vma. | |
5025 | */ | |
26cb63ad | 5026 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 5027 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 5028 | |
1e0fb9ec AL |
5029 | if (event->pmu->event_mapped) |
5030 | event->pmu->event_mapped(event); | |
5031 | ||
7b732a75 | 5032 | return ret; |
37d81828 PM |
5033 | } |
5034 | ||
3c446b3d PZ |
5035 | static int perf_fasync(int fd, struct file *filp, int on) |
5036 | { | |
496ad9aa | 5037 | struct inode *inode = file_inode(filp); |
cdd6c482 | 5038 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
5039 | int retval; |
5040 | ||
5955102c | 5041 | inode_lock(inode); |
cdd6c482 | 5042 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 5043 | inode_unlock(inode); |
3c446b3d PZ |
5044 | |
5045 | if (retval < 0) | |
5046 | return retval; | |
5047 | ||
5048 | return 0; | |
5049 | } | |
5050 | ||
0793a61d | 5051 | static const struct file_operations perf_fops = { |
3326c1ce | 5052 | .llseek = no_llseek, |
0793a61d TG |
5053 | .release = perf_release, |
5054 | .read = perf_read, | |
5055 | .poll = perf_poll, | |
d859e29f | 5056 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 5057 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 5058 | .mmap = perf_mmap, |
3c446b3d | 5059 | .fasync = perf_fasync, |
0793a61d TG |
5060 | }; |
5061 | ||
925d519a | 5062 | /* |
cdd6c482 | 5063 | * Perf event wakeup |
925d519a PZ |
5064 | * |
5065 | * If there's data, ensure we set the poll() state and publish everything | |
5066 | * to user-space before waking everybody up. | |
5067 | */ | |
5068 | ||
fed66e2c PZ |
5069 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
5070 | { | |
5071 | /* only the parent has fasync state */ | |
5072 | if (event->parent) | |
5073 | event = event->parent; | |
5074 | return &event->fasync; | |
5075 | } | |
5076 | ||
cdd6c482 | 5077 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 5078 | { |
10c6db11 | 5079 | ring_buffer_wakeup(event); |
4c9e2542 | 5080 | |
cdd6c482 | 5081 | if (event->pending_kill) { |
fed66e2c | 5082 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 5083 | event->pending_kill = 0; |
4c9e2542 | 5084 | } |
925d519a PZ |
5085 | } |
5086 | ||
e360adbe | 5087 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 5088 | { |
cdd6c482 IM |
5089 | struct perf_event *event = container_of(entry, |
5090 | struct perf_event, pending); | |
d525211f PZ |
5091 | int rctx; |
5092 | ||
5093 | rctx = perf_swevent_get_recursion_context(); | |
5094 | /* | |
5095 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
5096 | * and we won't recurse 'further'. | |
5097 | */ | |
79f14641 | 5098 | |
cdd6c482 IM |
5099 | if (event->pending_disable) { |
5100 | event->pending_disable = 0; | |
fae3fde6 | 5101 | perf_event_disable_local(event); |
79f14641 PZ |
5102 | } |
5103 | ||
cdd6c482 IM |
5104 | if (event->pending_wakeup) { |
5105 | event->pending_wakeup = 0; | |
5106 | perf_event_wakeup(event); | |
79f14641 | 5107 | } |
d525211f PZ |
5108 | |
5109 | if (rctx >= 0) | |
5110 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
5111 | } |
5112 | ||
39447b38 ZY |
5113 | /* |
5114 | * We assume there is only KVM supporting the callbacks. | |
5115 | * Later on, we might change it to a list if there is | |
5116 | * another virtualization implementation supporting the callbacks. | |
5117 | */ | |
5118 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
5119 | ||
5120 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5121 | { | |
5122 | perf_guest_cbs = cbs; | |
5123 | return 0; | |
5124 | } | |
5125 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
5126 | ||
5127 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
5128 | { | |
5129 | perf_guest_cbs = NULL; | |
5130 | return 0; | |
5131 | } | |
5132 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
5133 | ||
4018994f JO |
5134 | static void |
5135 | perf_output_sample_regs(struct perf_output_handle *handle, | |
5136 | struct pt_regs *regs, u64 mask) | |
5137 | { | |
5138 | int bit; | |
5139 | ||
5140 | for_each_set_bit(bit, (const unsigned long *) &mask, | |
5141 | sizeof(mask) * BITS_PER_BYTE) { | |
5142 | u64 val; | |
5143 | ||
5144 | val = perf_reg_value(regs, bit); | |
5145 | perf_output_put(handle, val); | |
5146 | } | |
5147 | } | |
5148 | ||
60e2364e | 5149 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
5150 | struct pt_regs *regs, |
5151 | struct pt_regs *regs_user_copy) | |
4018994f | 5152 | { |
88a7c26a AL |
5153 | if (user_mode(regs)) { |
5154 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 5155 | regs_user->regs = regs; |
88a7c26a AL |
5156 | } else if (current->mm) { |
5157 | perf_get_regs_user(regs_user, regs, regs_user_copy); | |
2565711f PZ |
5158 | } else { |
5159 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
5160 | regs_user->regs = NULL; | |
4018994f JO |
5161 | } |
5162 | } | |
5163 | ||
60e2364e SE |
5164 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
5165 | struct pt_regs *regs) | |
5166 | { | |
5167 | regs_intr->regs = regs; | |
5168 | regs_intr->abi = perf_reg_abi(current); | |
5169 | } | |
5170 | ||
5171 | ||
c5ebcedb JO |
5172 | /* |
5173 | * Get remaining task size from user stack pointer. | |
5174 | * | |
5175 | * It'd be better to take stack vma map and limit this more | |
5176 | * precisly, but there's no way to get it safely under interrupt, | |
5177 | * so using TASK_SIZE as limit. | |
5178 | */ | |
5179 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
5180 | { | |
5181 | unsigned long addr = perf_user_stack_pointer(regs); | |
5182 | ||
5183 | if (!addr || addr >= TASK_SIZE) | |
5184 | return 0; | |
5185 | ||
5186 | return TASK_SIZE - addr; | |
5187 | } | |
5188 | ||
5189 | static u16 | |
5190 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
5191 | struct pt_regs *regs) | |
5192 | { | |
5193 | u64 task_size; | |
5194 | ||
5195 | /* No regs, no stack pointer, no dump. */ | |
5196 | if (!regs) | |
5197 | return 0; | |
5198 | ||
5199 | /* | |
5200 | * Check if we fit in with the requested stack size into the: | |
5201 | * - TASK_SIZE | |
5202 | * If we don't, we limit the size to the TASK_SIZE. | |
5203 | * | |
5204 | * - remaining sample size | |
5205 | * If we don't, we customize the stack size to | |
5206 | * fit in to the remaining sample size. | |
5207 | */ | |
5208 | ||
5209 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
5210 | stack_size = min(stack_size, (u16) task_size); | |
5211 | ||
5212 | /* Current header size plus static size and dynamic size. */ | |
5213 | header_size += 2 * sizeof(u64); | |
5214 | ||
5215 | /* Do we fit in with the current stack dump size? */ | |
5216 | if ((u16) (header_size + stack_size) < header_size) { | |
5217 | /* | |
5218 | * If we overflow the maximum size for the sample, | |
5219 | * we customize the stack dump size to fit in. | |
5220 | */ | |
5221 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
5222 | stack_size = round_up(stack_size, sizeof(u64)); | |
5223 | } | |
5224 | ||
5225 | return stack_size; | |
5226 | } | |
5227 | ||
5228 | static void | |
5229 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
5230 | struct pt_regs *regs) | |
5231 | { | |
5232 | /* Case of a kernel thread, nothing to dump */ | |
5233 | if (!regs) { | |
5234 | u64 size = 0; | |
5235 | perf_output_put(handle, size); | |
5236 | } else { | |
5237 | unsigned long sp; | |
5238 | unsigned int rem; | |
5239 | u64 dyn_size; | |
5240 | ||
5241 | /* | |
5242 | * We dump: | |
5243 | * static size | |
5244 | * - the size requested by user or the best one we can fit | |
5245 | * in to the sample max size | |
5246 | * data | |
5247 | * - user stack dump data | |
5248 | * dynamic size | |
5249 | * - the actual dumped size | |
5250 | */ | |
5251 | ||
5252 | /* Static size. */ | |
5253 | perf_output_put(handle, dump_size); | |
5254 | ||
5255 | /* Data. */ | |
5256 | sp = perf_user_stack_pointer(regs); | |
5257 | rem = __output_copy_user(handle, (void *) sp, dump_size); | |
5258 | dyn_size = dump_size - rem; | |
5259 | ||
5260 | perf_output_skip(handle, rem); | |
5261 | ||
5262 | /* Dynamic size. */ | |
5263 | perf_output_put(handle, dyn_size); | |
5264 | } | |
5265 | } | |
5266 | ||
c980d109 ACM |
5267 | static void __perf_event_header__init_id(struct perf_event_header *header, |
5268 | struct perf_sample_data *data, | |
5269 | struct perf_event *event) | |
6844c09d ACM |
5270 | { |
5271 | u64 sample_type = event->attr.sample_type; | |
5272 | ||
5273 | data->type = sample_type; | |
5274 | header->size += event->id_header_size; | |
5275 | ||
5276 | if (sample_type & PERF_SAMPLE_TID) { | |
5277 | /* namespace issues */ | |
5278 | data->tid_entry.pid = perf_event_pid(event, current); | |
5279 | data->tid_entry.tid = perf_event_tid(event, current); | |
5280 | } | |
5281 | ||
5282 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 5283 | data->time = perf_event_clock(event); |
6844c09d | 5284 | |
ff3d527c | 5285 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
5286 | data->id = primary_event_id(event); |
5287 | ||
5288 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5289 | data->stream_id = event->id; | |
5290 | ||
5291 | if (sample_type & PERF_SAMPLE_CPU) { | |
5292 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
5293 | data->cpu_entry.reserved = 0; | |
5294 | } | |
5295 | } | |
5296 | ||
76369139 FW |
5297 | void perf_event_header__init_id(struct perf_event_header *header, |
5298 | struct perf_sample_data *data, | |
5299 | struct perf_event *event) | |
c980d109 ACM |
5300 | { |
5301 | if (event->attr.sample_id_all) | |
5302 | __perf_event_header__init_id(header, data, event); | |
5303 | } | |
5304 | ||
5305 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
5306 | struct perf_sample_data *data) | |
5307 | { | |
5308 | u64 sample_type = data->type; | |
5309 | ||
5310 | if (sample_type & PERF_SAMPLE_TID) | |
5311 | perf_output_put(handle, data->tid_entry); | |
5312 | ||
5313 | if (sample_type & PERF_SAMPLE_TIME) | |
5314 | perf_output_put(handle, data->time); | |
5315 | ||
5316 | if (sample_type & PERF_SAMPLE_ID) | |
5317 | perf_output_put(handle, data->id); | |
5318 | ||
5319 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5320 | perf_output_put(handle, data->stream_id); | |
5321 | ||
5322 | if (sample_type & PERF_SAMPLE_CPU) | |
5323 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
5324 | |
5325 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
5326 | perf_output_put(handle, data->id); | |
c980d109 ACM |
5327 | } |
5328 | ||
76369139 FW |
5329 | void perf_event__output_id_sample(struct perf_event *event, |
5330 | struct perf_output_handle *handle, | |
5331 | struct perf_sample_data *sample) | |
c980d109 ACM |
5332 | { |
5333 | if (event->attr.sample_id_all) | |
5334 | __perf_event__output_id_sample(handle, sample); | |
5335 | } | |
5336 | ||
3dab77fb | 5337 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
5338 | struct perf_event *event, |
5339 | u64 enabled, u64 running) | |
3dab77fb | 5340 | { |
cdd6c482 | 5341 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
5342 | u64 values[4]; |
5343 | int n = 0; | |
5344 | ||
b5e58793 | 5345 | values[n++] = perf_event_count(event); |
3dab77fb | 5346 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 5347 | values[n++] = enabled + |
cdd6c482 | 5348 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
5349 | } |
5350 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 5351 | values[n++] = running + |
cdd6c482 | 5352 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
5353 | } |
5354 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 5355 | values[n++] = primary_event_id(event); |
3dab77fb | 5356 | |
76369139 | 5357 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5358 | } |
5359 | ||
5360 | /* | |
cdd6c482 | 5361 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
3dab77fb PZ |
5362 | */ |
5363 | static void perf_output_read_group(struct perf_output_handle *handle, | |
eed01528 SE |
5364 | struct perf_event *event, |
5365 | u64 enabled, u64 running) | |
3dab77fb | 5366 | { |
cdd6c482 IM |
5367 | struct perf_event *leader = event->group_leader, *sub; |
5368 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
5369 | u64 values[5]; |
5370 | int n = 0; | |
5371 | ||
5372 | values[n++] = 1 + leader->nr_siblings; | |
5373 | ||
5374 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 5375 | values[n++] = enabled; |
3dab77fb PZ |
5376 | |
5377 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 5378 | values[n++] = running; |
3dab77fb | 5379 | |
cdd6c482 | 5380 | if (leader != event) |
3dab77fb PZ |
5381 | leader->pmu->read(leader); |
5382 | ||
b5e58793 | 5383 | values[n++] = perf_event_count(leader); |
3dab77fb | 5384 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5385 | values[n++] = primary_event_id(leader); |
3dab77fb | 5386 | |
76369139 | 5387 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 5388 | |
65abc865 | 5389 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
3dab77fb PZ |
5390 | n = 0; |
5391 | ||
6f5ab001 JO |
5392 | if ((sub != event) && |
5393 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
5394 | sub->pmu->read(sub); |
5395 | ||
b5e58793 | 5396 | values[n++] = perf_event_count(sub); |
3dab77fb | 5397 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5398 | values[n++] = primary_event_id(sub); |
3dab77fb | 5399 | |
76369139 | 5400 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
5401 | } |
5402 | } | |
5403 | ||
eed01528 SE |
5404 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
5405 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5406 | ||
3dab77fb | 5407 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 5408 | struct perf_event *event) |
3dab77fb | 5409 | { |
e3f3541c | 5410 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
5411 | u64 read_format = event->attr.read_format; |
5412 | ||
5413 | /* | |
5414 | * compute total_time_enabled, total_time_running | |
5415 | * based on snapshot values taken when the event | |
5416 | * was last scheduled in. | |
5417 | * | |
5418 | * we cannot simply called update_context_time() | |
5419 | * because of locking issue as we are called in | |
5420 | * NMI context | |
5421 | */ | |
c4794295 | 5422 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 5423 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 5424 | |
cdd6c482 | 5425 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 5426 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 5427 | else |
eed01528 | 5428 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
5429 | } |
5430 | ||
5622f295 MM |
5431 | void perf_output_sample(struct perf_output_handle *handle, |
5432 | struct perf_event_header *header, | |
5433 | struct perf_sample_data *data, | |
cdd6c482 | 5434 | struct perf_event *event) |
5622f295 MM |
5435 | { |
5436 | u64 sample_type = data->type; | |
5437 | ||
5438 | perf_output_put(handle, *header); | |
5439 | ||
ff3d527c AH |
5440 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
5441 | perf_output_put(handle, data->id); | |
5442 | ||
5622f295 MM |
5443 | if (sample_type & PERF_SAMPLE_IP) |
5444 | perf_output_put(handle, data->ip); | |
5445 | ||
5446 | if (sample_type & PERF_SAMPLE_TID) | |
5447 | perf_output_put(handle, data->tid_entry); | |
5448 | ||
5449 | if (sample_type & PERF_SAMPLE_TIME) | |
5450 | perf_output_put(handle, data->time); | |
5451 | ||
5452 | if (sample_type & PERF_SAMPLE_ADDR) | |
5453 | perf_output_put(handle, data->addr); | |
5454 | ||
5455 | if (sample_type & PERF_SAMPLE_ID) | |
5456 | perf_output_put(handle, data->id); | |
5457 | ||
5458 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
5459 | perf_output_put(handle, data->stream_id); | |
5460 | ||
5461 | if (sample_type & PERF_SAMPLE_CPU) | |
5462 | perf_output_put(handle, data->cpu_entry); | |
5463 | ||
5464 | if (sample_type & PERF_SAMPLE_PERIOD) | |
5465 | perf_output_put(handle, data->period); | |
5466 | ||
5467 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 5468 | perf_output_read(handle, event); |
5622f295 MM |
5469 | |
5470 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
5471 | if (data->callchain) { | |
5472 | int size = 1; | |
5473 | ||
5474 | if (data->callchain) | |
5475 | size += data->callchain->nr; | |
5476 | ||
5477 | size *= sizeof(u64); | |
5478 | ||
76369139 | 5479 | __output_copy(handle, data->callchain, size); |
5622f295 MM |
5480 | } else { |
5481 | u64 nr = 0; | |
5482 | perf_output_put(handle, nr); | |
5483 | } | |
5484 | } | |
5485 | ||
5486 | if (sample_type & PERF_SAMPLE_RAW) { | |
5487 | if (data->raw) { | |
fa128e6a AS |
5488 | u32 raw_size = data->raw->size; |
5489 | u32 real_size = round_up(raw_size + sizeof(u32), | |
5490 | sizeof(u64)) - sizeof(u32); | |
5491 | u64 zero = 0; | |
5492 | ||
5493 | perf_output_put(handle, real_size); | |
5494 | __output_copy(handle, data->raw->data, raw_size); | |
5495 | if (real_size - raw_size) | |
5496 | __output_copy(handle, &zero, real_size - raw_size); | |
5622f295 MM |
5497 | } else { |
5498 | struct { | |
5499 | u32 size; | |
5500 | u32 data; | |
5501 | } raw = { | |
5502 | .size = sizeof(u32), | |
5503 | .data = 0, | |
5504 | }; | |
5505 | perf_output_put(handle, raw); | |
5506 | } | |
5507 | } | |
a7ac67ea | 5508 | |
bce38cd5 SE |
5509 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
5510 | if (data->br_stack) { | |
5511 | size_t size; | |
5512 | ||
5513 | size = data->br_stack->nr | |
5514 | * sizeof(struct perf_branch_entry); | |
5515 | ||
5516 | perf_output_put(handle, data->br_stack->nr); | |
5517 | perf_output_copy(handle, data->br_stack->entries, size); | |
5518 | } else { | |
5519 | /* | |
5520 | * we always store at least the value of nr | |
5521 | */ | |
5522 | u64 nr = 0; | |
5523 | perf_output_put(handle, nr); | |
5524 | } | |
5525 | } | |
4018994f JO |
5526 | |
5527 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
5528 | u64 abi = data->regs_user.abi; | |
5529 | ||
5530 | /* | |
5531 | * If there are no regs to dump, notice it through | |
5532 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5533 | */ | |
5534 | perf_output_put(handle, abi); | |
5535 | ||
5536 | if (abi) { | |
5537 | u64 mask = event->attr.sample_regs_user; | |
5538 | perf_output_sample_regs(handle, | |
5539 | data->regs_user.regs, | |
5540 | mask); | |
5541 | } | |
5542 | } | |
c5ebcedb | 5543 | |
a5cdd40c | 5544 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
5545 | perf_output_sample_ustack(handle, |
5546 | data->stack_user_size, | |
5547 | data->regs_user.regs); | |
a5cdd40c | 5548 | } |
c3feedf2 AK |
5549 | |
5550 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
5551 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
5552 | |
5553 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
5554 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 5555 | |
fdfbbd07 AK |
5556 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
5557 | perf_output_put(handle, data->txn); | |
5558 | ||
60e2364e SE |
5559 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
5560 | u64 abi = data->regs_intr.abi; | |
5561 | /* | |
5562 | * If there are no regs to dump, notice it through | |
5563 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
5564 | */ | |
5565 | perf_output_put(handle, abi); | |
5566 | ||
5567 | if (abi) { | |
5568 | u64 mask = event->attr.sample_regs_intr; | |
5569 | ||
5570 | perf_output_sample_regs(handle, | |
5571 | data->regs_intr.regs, | |
5572 | mask); | |
5573 | } | |
5574 | } | |
5575 | ||
a5cdd40c PZ |
5576 | if (!event->attr.watermark) { |
5577 | int wakeup_events = event->attr.wakeup_events; | |
5578 | ||
5579 | if (wakeup_events) { | |
5580 | struct ring_buffer *rb = handle->rb; | |
5581 | int events = local_inc_return(&rb->events); | |
5582 | ||
5583 | if (events >= wakeup_events) { | |
5584 | local_sub(wakeup_events, &rb->events); | |
5585 | local_inc(&rb->wakeup); | |
5586 | } | |
5587 | } | |
5588 | } | |
5622f295 MM |
5589 | } |
5590 | ||
5591 | void perf_prepare_sample(struct perf_event_header *header, | |
5592 | struct perf_sample_data *data, | |
cdd6c482 | 5593 | struct perf_event *event, |
5622f295 | 5594 | struct pt_regs *regs) |
7b732a75 | 5595 | { |
cdd6c482 | 5596 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 5597 | |
cdd6c482 | 5598 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 5599 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
5600 | |
5601 | header->misc = 0; | |
5602 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 5603 | |
c980d109 | 5604 | __perf_event_header__init_id(header, data, event); |
6844c09d | 5605 | |
c320c7b7 | 5606 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
5607 | data->ip = perf_instruction_pointer(regs); |
5608 | ||
b23f3325 | 5609 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 5610 | int size = 1; |
394ee076 | 5611 | |
e6dab5ff | 5612 | data->callchain = perf_callchain(event, regs); |
5622f295 MM |
5613 | |
5614 | if (data->callchain) | |
5615 | size += data->callchain->nr; | |
5616 | ||
5617 | header->size += size * sizeof(u64); | |
394ee076 PZ |
5618 | } |
5619 | ||
3a43ce68 | 5620 | if (sample_type & PERF_SAMPLE_RAW) { |
a044560c PZ |
5621 | int size = sizeof(u32); |
5622 | ||
5623 | if (data->raw) | |
5624 | size += data->raw->size; | |
5625 | else | |
5626 | size += sizeof(u32); | |
5627 | ||
fa128e6a | 5628 | header->size += round_up(size, sizeof(u64)); |
7f453c24 | 5629 | } |
bce38cd5 SE |
5630 | |
5631 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
5632 | int size = sizeof(u64); /* nr */ | |
5633 | if (data->br_stack) { | |
5634 | size += data->br_stack->nr | |
5635 | * sizeof(struct perf_branch_entry); | |
5636 | } | |
5637 | header->size += size; | |
5638 | } | |
4018994f | 5639 | |
2565711f | 5640 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
5641 | perf_sample_regs_user(&data->regs_user, regs, |
5642 | &data->regs_user_copy); | |
2565711f | 5643 | |
4018994f JO |
5644 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
5645 | /* regs dump ABI info */ | |
5646 | int size = sizeof(u64); | |
5647 | ||
4018994f JO |
5648 | if (data->regs_user.regs) { |
5649 | u64 mask = event->attr.sample_regs_user; | |
5650 | size += hweight64(mask) * sizeof(u64); | |
5651 | } | |
5652 | ||
5653 | header->size += size; | |
5654 | } | |
c5ebcedb JO |
5655 | |
5656 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
5657 | /* | |
5658 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways | |
5659 | * processed as the last one or have additional check added | |
5660 | * in case new sample type is added, because we could eat | |
5661 | * up the rest of the sample size. | |
5662 | */ | |
c5ebcedb JO |
5663 | u16 stack_size = event->attr.sample_stack_user; |
5664 | u16 size = sizeof(u64); | |
5665 | ||
c5ebcedb | 5666 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 5667 | data->regs_user.regs); |
c5ebcedb JO |
5668 | |
5669 | /* | |
5670 | * If there is something to dump, add space for the dump | |
5671 | * itself and for the field that tells the dynamic size, | |
5672 | * which is how many have been actually dumped. | |
5673 | */ | |
5674 | if (stack_size) | |
5675 | size += sizeof(u64) + stack_size; | |
5676 | ||
5677 | data->stack_user_size = stack_size; | |
5678 | header->size += size; | |
5679 | } | |
60e2364e SE |
5680 | |
5681 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
5682 | /* regs dump ABI info */ | |
5683 | int size = sizeof(u64); | |
5684 | ||
5685 | perf_sample_regs_intr(&data->regs_intr, regs); | |
5686 | ||
5687 | if (data->regs_intr.regs) { | |
5688 | u64 mask = event->attr.sample_regs_intr; | |
5689 | ||
5690 | size += hweight64(mask) * sizeof(u64); | |
5691 | } | |
5692 | ||
5693 | header->size += size; | |
5694 | } | |
5622f295 | 5695 | } |
7f453c24 | 5696 | |
21509084 YZ |
5697 | void perf_event_output(struct perf_event *event, |
5698 | struct perf_sample_data *data, | |
5699 | struct pt_regs *regs) | |
5622f295 MM |
5700 | { |
5701 | struct perf_output_handle handle; | |
5702 | struct perf_event_header header; | |
689802b2 | 5703 | |
927c7a9e FW |
5704 | /* protect the callchain buffers */ |
5705 | rcu_read_lock(); | |
5706 | ||
cdd6c482 | 5707 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 5708 | |
a7ac67ea | 5709 | if (perf_output_begin(&handle, event, header.size)) |
927c7a9e | 5710 | goto exit; |
0322cd6e | 5711 | |
cdd6c482 | 5712 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 5713 | |
8a057d84 | 5714 | perf_output_end(&handle); |
927c7a9e FW |
5715 | |
5716 | exit: | |
5717 | rcu_read_unlock(); | |
0322cd6e PZ |
5718 | } |
5719 | ||
38b200d6 | 5720 | /* |
cdd6c482 | 5721 | * read event_id |
38b200d6 PZ |
5722 | */ |
5723 | ||
5724 | struct perf_read_event { | |
5725 | struct perf_event_header header; | |
5726 | ||
5727 | u32 pid; | |
5728 | u32 tid; | |
38b200d6 PZ |
5729 | }; |
5730 | ||
5731 | static void | |
cdd6c482 | 5732 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
5733 | struct task_struct *task) |
5734 | { | |
5735 | struct perf_output_handle handle; | |
c980d109 | 5736 | struct perf_sample_data sample; |
dfc65094 | 5737 | struct perf_read_event read_event = { |
38b200d6 | 5738 | .header = { |
cdd6c482 | 5739 | .type = PERF_RECORD_READ, |
38b200d6 | 5740 | .misc = 0, |
c320c7b7 | 5741 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 5742 | }, |
cdd6c482 IM |
5743 | .pid = perf_event_pid(event, task), |
5744 | .tid = perf_event_tid(event, task), | |
38b200d6 | 5745 | }; |
3dab77fb | 5746 | int ret; |
38b200d6 | 5747 | |
c980d109 | 5748 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 5749 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
5750 | if (ret) |
5751 | return; | |
5752 | ||
dfc65094 | 5753 | perf_output_put(&handle, read_event); |
cdd6c482 | 5754 | perf_output_read(&handle, event); |
c980d109 | 5755 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 5756 | |
38b200d6 PZ |
5757 | perf_output_end(&handle); |
5758 | } | |
5759 | ||
52d857a8 JO |
5760 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
5761 | ||
5762 | static void | |
5763 | perf_event_aux_ctx(struct perf_event_context *ctx, | |
52d857a8 JO |
5764 | perf_event_aux_output_cb output, |
5765 | void *data) | |
5766 | { | |
5767 | struct perf_event *event; | |
5768 | ||
5769 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
5770 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
5771 | continue; | |
5772 | if (!event_filter_match(event)) | |
5773 | continue; | |
67516844 | 5774 | output(event, data); |
52d857a8 JO |
5775 | } |
5776 | } | |
5777 | ||
4e93ad60 JO |
5778 | static void |
5779 | perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data, | |
5780 | struct perf_event_context *task_ctx) | |
5781 | { | |
5782 | rcu_read_lock(); | |
5783 | preempt_disable(); | |
5784 | perf_event_aux_ctx(task_ctx, output, data); | |
5785 | preempt_enable(); | |
5786 | rcu_read_unlock(); | |
5787 | } | |
5788 | ||
52d857a8 | 5789 | static void |
67516844 | 5790 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
52d857a8 JO |
5791 | struct perf_event_context *task_ctx) |
5792 | { | |
5793 | struct perf_cpu_context *cpuctx; | |
5794 | struct perf_event_context *ctx; | |
5795 | struct pmu *pmu; | |
5796 | int ctxn; | |
5797 | ||
4e93ad60 JO |
5798 | /* |
5799 | * If we have task_ctx != NULL we only notify | |
5800 | * the task context itself. The task_ctx is set | |
5801 | * only for EXIT events before releasing task | |
5802 | * context. | |
5803 | */ | |
5804 | if (task_ctx) { | |
5805 | perf_event_aux_task_ctx(output, data, task_ctx); | |
5806 | return; | |
5807 | } | |
5808 | ||
52d857a8 JO |
5809 | rcu_read_lock(); |
5810 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
5811 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); | |
5812 | if (cpuctx->unique_pmu != pmu) | |
5813 | goto next; | |
67516844 | 5814 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
52d857a8 JO |
5815 | ctxn = pmu->task_ctx_nr; |
5816 | if (ctxn < 0) | |
5817 | goto next; | |
5818 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
5819 | if (ctx) | |
67516844 | 5820 | perf_event_aux_ctx(ctx, output, data); |
52d857a8 JO |
5821 | next: |
5822 | put_cpu_ptr(pmu->pmu_cpu_context); | |
5823 | } | |
52d857a8 | 5824 | rcu_read_unlock(); |
95ff4ca2 AS |
5825 | } |
5826 | ||
5827 | struct remote_output { | |
5828 | struct ring_buffer *rb; | |
5829 | int err; | |
5830 | }; | |
5831 | ||
5832 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
5833 | { | |
5834 | struct perf_event *parent = event->parent; | |
5835 | struct remote_output *ro = data; | |
5836 | struct ring_buffer *rb = ro->rb; | |
5837 | ||
5838 | if (!has_aux(event)) | |
5839 | return; | |
5840 | ||
5841 | if (!parent) | |
5842 | parent = event; | |
5843 | ||
5844 | /* | |
5845 | * In case of inheritance, it will be the parent that links to the | |
5846 | * ring-buffer, but it will be the child that's actually using it: | |
5847 | */ | |
5848 | if (rcu_dereference(parent->rb) == rb) | |
5849 | ro->err = __perf_event_stop(event); | |
5850 | } | |
5851 | ||
5852 | static int __perf_pmu_output_stop(void *info) | |
5853 | { | |
5854 | struct perf_event *event = info; | |
5855 | struct pmu *pmu = event->pmu; | |
5856 | struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); | |
5857 | struct remote_output ro = { | |
5858 | .rb = event->rb, | |
5859 | }; | |
5860 | ||
5861 | rcu_read_lock(); | |
5862 | perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro); | |
5863 | if (cpuctx->task_ctx) | |
5864 | perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop, | |
5865 | &ro); | |
5866 | rcu_read_unlock(); | |
5867 | ||
5868 | return ro.err; | |
5869 | } | |
5870 | ||
5871 | static void perf_pmu_output_stop(struct perf_event *event) | |
5872 | { | |
5873 | struct perf_event *iter; | |
5874 | int err, cpu; | |
5875 | ||
5876 | restart: | |
5877 | rcu_read_lock(); | |
5878 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
5879 | /* | |
5880 | * For per-CPU events, we need to make sure that neither they | |
5881 | * nor their children are running; for cpu==-1 events it's | |
5882 | * sufficient to stop the event itself if it's active, since | |
5883 | * it can't have children. | |
5884 | */ | |
5885 | cpu = iter->cpu; | |
5886 | if (cpu == -1) | |
5887 | cpu = READ_ONCE(iter->oncpu); | |
5888 | ||
5889 | if (cpu == -1) | |
5890 | continue; | |
5891 | ||
5892 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
5893 | if (err == -EAGAIN) { | |
5894 | rcu_read_unlock(); | |
5895 | goto restart; | |
5896 | } | |
5897 | } | |
5898 | rcu_read_unlock(); | |
52d857a8 JO |
5899 | } |
5900 | ||
60313ebe | 5901 | /* |
9f498cc5 PZ |
5902 | * task tracking -- fork/exit |
5903 | * | |
13d7a241 | 5904 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
5905 | */ |
5906 | ||
9f498cc5 | 5907 | struct perf_task_event { |
3a80b4a3 | 5908 | struct task_struct *task; |
cdd6c482 | 5909 | struct perf_event_context *task_ctx; |
60313ebe PZ |
5910 | |
5911 | struct { | |
5912 | struct perf_event_header header; | |
5913 | ||
5914 | u32 pid; | |
5915 | u32 ppid; | |
9f498cc5 PZ |
5916 | u32 tid; |
5917 | u32 ptid; | |
393b2ad8 | 5918 | u64 time; |
cdd6c482 | 5919 | } event_id; |
60313ebe PZ |
5920 | }; |
5921 | ||
67516844 JO |
5922 | static int perf_event_task_match(struct perf_event *event) |
5923 | { | |
13d7a241 SE |
5924 | return event->attr.comm || event->attr.mmap || |
5925 | event->attr.mmap2 || event->attr.mmap_data || | |
5926 | event->attr.task; | |
67516844 JO |
5927 | } |
5928 | ||
cdd6c482 | 5929 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 5930 | void *data) |
60313ebe | 5931 | { |
52d857a8 | 5932 | struct perf_task_event *task_event = data; |
60313ebe | 5933 | struct perf_output_handle handle; |
c980d109 | 5934 | struct perf_sample_data sample; |
9f498cc5 | 5935 | struct task_struct *task = task_event->task; |
c980d109 | 5936 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 5937 | |
67516844 JO |
5938 | if (!perf_event_task_match(event)) |
5939 | return; | |
5940 | ||
c980d109 | 5941 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 5942 | |
c980d109 | 5943 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 5944 | task_event->event_id.header.size); |
ef60777c | 5945 | if (ret) |
c980d109 | 5946 | goto out; |
60313ebe | 5947 | |
cdd6c482 IM |
5948 | task_event->event_id.pid = perf_event_pid(event, task); |
5949 | task_event->event_id.ppid = perf_event_pid(event, current); | |
60313ebe | 5950 | |
cdd6c482 IM |
5951 | task_event->event_id.tid = perf_event_tid(event, task); |
5952 | task_event->event_id.ptid = perf_event_tid(event, current); | |
9f498cc5 | 5953 | |
34f43927 PZ |
5954 | task_event->event_id.time = perf_event_clock(event); |
5955 | ||
cdd6c482 | 5956 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 5957 | |
c980d109 ACM |
5958 | perf_event__output_id_sample(event, &handle, &sample); |
5959 | ||
60313ebe | 5960 | perf_output_end(&handle); |
c980d109 ACM |
5961 | out: |
5962 | task_event->event_id.header.size = size; | |
60313ebe PZ |
5963 | } |
5964 | ||
cdd6c482 IM |
5965 | static void perf_event_task(struct task_struct *task, |
5966 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 5967 | int new) |
60313ebe | 5968 | { |
9f498cc5 | 5969 | struct perf_task_event task_event; |
60313ebe | 5970 | |
cdd6c482 IM |
5971 | if (!atomic_read(&nr_comm_events) && |
5972 | !atomic_read(&nr_mmap_events) && | |
5973 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
5974 | return; |
5975 | ||
9f498cc5 | 5976 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
5977 | .task = task, |
5978 | .task_ctx = task_ctx, | |
cdd6c482 | 5979 | .event_id = { |
60313ebe | 5980 | .header = { |
cdd6c482 | 5981 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 5982 | .misc = 0, |
cdd6c482 | 5983 | .size = sizeof(task_event.event_id), |
60313ebe | 5984 | }, |
573402db PZ |
5985 | /* .pid */ |
5986 | /* .ppid */ | |
9f498cc5 PZ |
5987 | /* .tid */ |
5988 | /* .ptid */ | |
34f43927 | 5989 | /* .time */ |
60313ebe PZ |
5990 | }, |
5991 | }; | |
5992 | ||
67516844 | 5993 | perf_event_aux(perf_event_task_output, |
52d857a8 JO |
5994 | &task_event, |
5995 | task_ctx); | |
9f498cc5 PZ |
5996 | } |
5997 | ||
cdd6c482 | 5998 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 5999 | { |
cdd6c482 | 6000 | perf_event_task(task, NULL, 1); |
60313ebe PZ |
6001 | } |
6002 | ||
8d1b2d93 PZ |
6003 | /* |
6004 | * comm tracking | |
6005 | */ | |
6006 | ||
6007 | struct perf_comm_event { | |
22a4f650 IM |
6008 | struct task_struct *task; |
6009 | char *comm; | |
8d1b2d93 PZ |
6010 | int comm_size; |
6011 | ||
6012 | struct { | |
6013 | struct perf_event_header header; | |
6014 | ||
6015 | u32 pid; | |
6016 | u32 tid; | |
cdd6c482 | 6017 | } event_id; |
8d1b2d93 PZ |
6018 | }; |
6019 | ||
67516844 JO |
6020 | static int perf_event_comm_match(struct perf_event *event) |
6021 | { | |
6022 | return event->attr.comm; | |
6023 | } | |
6024 | ||
cdd6c482 | 6025 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 6026 | void *data) |
8d1b2d93 | 6027 | { |
52d857a8 | 6028 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 6029 | struct perf_output_handle handle; |
c980d109 | 6030 | struct perf_sample_data sample; |
cdd6c482 | 6031 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
6032 | int ret; |
6033 | ||
67516844 JO |
6034 | if (!perf_event_comm_match(event)) |
6035 | return; | |
6036 | ||
c980d109 ACM |
6037 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
6038 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6039 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
6040 | |
6041 | if (ret) | |
c980d109 | 6042 | goto out; |
8d1b2d93 | 6043 | |
cdd6c482 IM |
6044 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
6045 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 6046 | |
cdd6c482 | 6047 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 6048 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 6049 | comm_event->comm_size); |
c980d109 ACM |
6050 | |
6051 | perf_event__output_id_sample(event, &handle, &sample); | |
6052 | ||
8d1b2d93 | 6053 | perf_output_end(&handle); |
c980d109 ACM |
6054 | out: |
6055 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
6056 | } |
6057 | ||
cdd6c482 | 6058 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 6059 | { |
413ee3b4 | 6060 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 6061 | unsigned int size; |
8d1b2d93 | 6062 | |
413ee3b4 | 6063 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 6064 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 6065 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
6066 | |
6067 | comm_event->comm = comm; | |
6068 | comm_event->comm_size = size; | |
6069 | ||
cdd6c482 | 6070 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 6071 | |
67516844 | 6072 | perf_event_aux(perf_event_comm_output, |
52d857a8 JO |
6073 | comm_event, |
6074 | NULL); | |
8d1b2d93 PZ |
6075 | } |
6076 | ||
82b89778 | 6077 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 6078 | { |
9ee318a7 PZ |
6079 | struct perf_comm_event comm_event; |
6080 | ||
cdd6c482 | 6081 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 6082 | return; |
a63eaf34 | 6083 | |
9ee318a7 | 6084 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 6085 | .task = task, |
573402db PZ |
6086 | /* .comm */ |
6087 | /* .comm_size */ | |
cdd6c482 | 6088 | .event_id = { |
573402db | 6089 | .header = { |
cdd6c482 | 6090 | .type = PERF_RECORD_COMM, |
82b89778 | 6091 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
6092 | /* .size */ |
6093 | }, | |
6094 | /* .pid */ | |
6095 | /* .tid */ | |
8d1b2d93 PZ |
6096 | }, |
6097 | }; | |
6098 | ||
cdd6c482 | 6099 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
6100 | } |
6101 | ||
0a4a9391 PZ |
6102 | /* |
6103 | * mmap tracking | |
6104 | */ | |
6105 | ||
6106 | struct perf_mmap_event { | |
089dd79d PZ |
6107 | struct vm_area_struct *vma; |
6108 | ||
6109 | const char *file_name; | |
6110 | int file_size; | |
13d7a241 SE |
6111 | int maj, min; |
6112 | u64 ino; | |
6113 | u64 ino_generation; | |
f972eb63 | 6114 | u32 prot, flags; |
0a4a9391 PZ |
6115 | |
6116 | struct { | |
6117 | struct perf_event_header header; | |
6118 | ||
6119 | u32 pid; | |
6120 | u32 tid; | |
6121 | u64 start; | |
6122 | u64 len; | |
6123 | u64 pgoff; | |
cdd6c482 | 6124 | } event_id; |
0a4a9391 PZ |
6125 | }; |
6126 | ||
67516844 JO |
6127 | static int perf_event_mmap_match(struct perf_event *event, |
6128 | void *data) | |
6129 | { | |
6130 | struct perf_mmap_event *mmap_event = data; | |
6131 | struct vm_area_struct *vma = mmap_event->vma; | |
6132 | int executable = vma->vm_flags & VM_EXEC; | |
6133 | ||
6134 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 6135 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
6136 | } |
6137 | ||
cdd6c482 | 6138 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 6139 | void *data) |
0a4a9391 | 6140 | { |
52d857a8 | 6141 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 6142 | struct perf_output_handle handle; |
c980d109 | 6143 | struct perf_sample_data sample; |
cdd6c482 | 6144 | int size = mmap_event->event_id.header.size; |
c980d109 | 6145 | int ret; |
0a4a9391 | 6146 | |
67516844 JO |
6147 | if (!perf_event_mmap_match(event, data)) |
6148 | return; | |
6149 | ||
13d7a241 SE |
6150 | if (event->attr.mmap2) { |
6151 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
6152 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
6153 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
6154 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 6155 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
6156 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
6157 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
6158 | } |
6159 | ||
c980d109 ACM |
6160 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
6161 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6162 | mmap_event->event_id.header.size); |
0a4a9391 | 6163 | if (ret) |
c980d109 | 6164 | goto out; |
0a4a9391 | 6165 | |
cdd6c482 IM |
6166 | mmap_event->event_id.pid = perf_event_pid(event, current); |
6167 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 6168 | |
cdd6c482 | 6169 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
6170 | |
6171 | if (event->attr.mmap2) { | |
6172 | perf_output_put(&handle, mmap_event->maj); | |
6173 | perf_output_put(&handle, mmap_event->min); | |
6174 | perf_output_put(&handle, mmap_event->ino); | |
6175 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
6176 | perf_output_put(&handle, mmap_event->prot); |
6177 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
6178 | } |
6179 | ||
76369139 | 6180 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 6181 | mmap_event->file_size); |
c980d109 ACM |
6182 | |
6183 | perf_event__output_id_sample(event, &handle, &sample); | |
6184 | ||
78d613eb | 6185 | perf_output_end(&handle); |
c980d109 ACM |
6186 | out: |
6187 | mmap_event->event_id.header.size = size; | |
0a4a9391 PZ |
6188 | } |
6189 | ||
cdd6c482 | 6190 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 6191 | { |
089dd79d PZ |
6192 | struct vm_area_struct *vma = mmap_event->vma; |
6193 | struct file *file = vma->vm_file; | |
13d7a241 SE |
6194 | int maj = 0, min = 0; |
6195 | u64 ino = 0, gen = 0; | |
f972eb63 | 6196 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
6197 | unsigned int size; |
6198 | char tmp[16]; | |
6199 | char *buf = NULL; | |
2c42cfbf | 6200 | char *name; |
413ee3b4 | 6201 | |
0a4a9391 | 6202 | if (file) { |
13d7a241 SE |
6203 | struct inode *inode; |
6204 | dev_t dev; | |
3ea2f2b9 | 6205 | |
2c42cfbf | 6206 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 6207 | if (!buf) { |
c7e548b4 ON |
6208 | name = "//enomem"; |
6209 | goto cpy_name; | |
0a4a9391 | 6210 | } |
413ee3b4 | 6211 | /* |
3ea2f2b9 | 6212 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
6213 | * need to add enough zero bytes after the string to handle |
6214 | * the 64bit alignment we do later. | |
6215 | */ | |
9bf39ab2 | 6216 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 6217 | if (IS_ERR(name)) { |
c7e548b4 ON |
6218 | name = "//toolong"; |
6219 | goto cpy_name; | |
0a4a9391 | 6220 | } |
13d7a241 SE |
6221 | inode = file_inode(vma->vm_file); |
6222 | dev = inode->i_sb->s_dev; | |
6223 | ino = inode->i_ino; | |
6224 | gen = inode->i_generation; | |
6225 | maj = MAJOR(dev); | |
6226 | min = MINOR(dev); | |
f972eb63 PZ |
6227 | |
6228 | if (vma->vm_flags & VM_READ) | |
6229 | prot |= PROT_READ; | |
6230 | if (vma->vm_flags & VM_WRITE) | |
6231 | prot |= PROT_WRITE; | |
6232 | if (vma->vm_flags & VM_EXEC) | |
6233 | prot |= PROT_EXEC; | |
6234 | ||
6235 | if (vma->vm_flags & VM_MAYSHARE) | |
6236 | flags = MAP_SHARED; | |
6237 | else | |
6238 | flags = MAP_PRIVATE; | |
6239 | ||
6240 | if (vma->vm_flags & VM_DENYWRITE) | |
6241 | flags |= MAP_DENYWRITE; | |
6242 | if (vma->vm_flags & VM_MAYEXEC) | |
6243 | flags |= MAP_EXECUTABLE; | |
6244 | if (vma->vm_flags & VM_LOCKED) | |
6245 | flags |= MAP_LOCKED; | |
6246 | if (vma->vm_flags & VM_HUGETLB) | |
6247 | flags |= MAP_HUGETLB; | |
6248 | ||
c7e548b4 | 6249 | goto got_name; |
0a4a9391 | 6250 | } else { |
fbe26abe JO |
6251 | if (vma->vm_ops && vma->vm_ops->name) { |
6252 | name = (char *) vma->vm_ops->name(vma); | |
6253 | if (name) | |
6254 | goto cpy_name; | |
6255 | } | |
6256 | ||
2c42cfbf | 6257 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
6258 | if (name) |
6259 | goto cpy_name; | |
089dd79d | 6260 | |
32c5fb7e | 6261 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 6262 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
6263 | name = "[heap]"; |
6264 | goto cpy_name; | |
32c5fb7e ON |
6265 | } |
6266 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 6267 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
6268 | name = "[stack]"; |
6269 | goto cpy_name; | |
089dd79d PZ |
6270 | } |
6271 | ||
c7e548b4 ON |
6272 | name = "//anon"; |
6273 | goto cpy_name; | |
0a4a9391 PZ |
6274 | } |
6275 | ||
c7e548b4 ON |
6276 | cpy_name: |
6277 | strlcpy(tmp, name, sizeof(tmp)); | |
6278 | name = tmp; | |
0a4a9391 | 6279 | got_name: |
2c42cfbf PZ |
6280 | /* |
6281 | * Since our buffer works in 8 byte units we need to align our string | |
6282 | * size to a multiple of 8. However, we must guarantee the tail end is | |
6283 | * zero'd out to avoid leaking random bits to userspace. | |
6284 | */ | |
6285 | size = strlen(name)+1; | |
6286 | while (!IS_ALIGNED(size, sizeof(u64))) | |
6287 | name[size++] = '\0'; | |
0a4a9391 PZ |
6288 | |
6289 | mmap_event->file_name = name; | |
6290 | mmap_event->file_size = size; | |
13d7a241 SE |
6291 | mmap_event->maj = maj; |
6292 | mmap_event->min = min; | |
6293 | mmap_event->ino = ino; | |
6294 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
6295 | mmap_event->prot = prot; |
6296 | mmap_event->flags = flags; | |
0a4a9391 | 6297 | |
2fe85427 SE |
6298 | if (!(vma->vm_flags & VM_EXEC)) |
6299 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
6300 | ||
cdd6c482 | 6301 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 6302 | |
67516844 | 6303 | perf_event_aux(perf_event_mmap_output, |
52d857a8 JO |
6304 | mmap_event, |
6305 | NULL); | |
665c2142 | 6306 | |
0a4a9391 PZ |
6307 | kfree(buf); |
6308 | } | |
6309 | ||
3af9e859 | 6310 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 6311 | { |
9ee318a7 PZ |
6312 | struct perf_mmap_event mmap_event; |
6313 | ||
cdd6c482 | 6314 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
6315 | return; |
6316 | ||
6317 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 6318 | .vma = vma, |
573402db PZ |
6319 | /* .file_name */ |
6320 | /* .file_size */ | |
cdd6c482 | 6321 | .event_id = { |
573402db | 6322 | .header = { |
cdd6c482 | 6323 | .type = PERF_RECORD_MMAP, |
39447b38 | 6324 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
6325 | /* .size */ |
6326 | }, | |
6327 | /* .pid */ | |
6328 | /* .tid */ | |
089dd79d PZ |
6329 | .start = vma->vm_start, |
6330 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 6331 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 6332 | }, |
13d7a241 SE |
6333 | /* .maj (attr_mmap2 only) */ |
6334 | /* .min (attr_mmap2 only) */ | |
6335 | /* .ino (attr_mmap2 only) */ | |
6336 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
6337 | /* .prot (attr_mmap2 only) */ |
6338 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
6339 | }; |
6340 | ||
cdd6c482 | 6341 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
6342 | } |
6343 | ||
68db7e98 AS |
6344 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
6345 | unsigned long size, u64 flags) | |
6346 | { | |
6347 | struct perf_output_handle handle; | |
6348 | struct perf_sample_data sample; | |
6349 | struct perf_aux_event { | |
6350 | struct perf_event_header header; | |
6351 | u64 offset; | |
6352 | u64 size; | |
6353 | u64 flags; | |
6354 | } rec = { | |
6355 | .header = { | |
6356 | .type = PERF_RECORD_AUX, | |
6357 | .misc = 0, | |
6358 | .size = sizeof(rec), | |
6359 | }, | |
6360 | .offset = head, | |
6361 | .size = size, | |
6362 | .flags = flags, | |
6363 | }; | |
6364 | int ret; | |
6365 | ||
6366 | perf_event_header__init_id(&rec.header, &sample, event); | |
6367 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6368 | ||
6369 | if (ret) | |
6370 | return; | |
6371 | ||
6372 | perf_output_put(&handle, rec); | |
6373 | perf_event__output_id_sample(event, &handle, &sample); | |
6374 | ||
6375 | perf_output_end(&handle); | |
6376 | } | |
6377 | ||
f38b0dbb KL |
6378 | /* |
6379 | * Lost/dropped samples logging | |
6380 | */ | |
6381 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
6382 | { | |
6383 | struct perf_output_handle handle; | |
6384 | struct perf_sample_data sample; | |
6385 | int ret; | |
6386 | ||
6387 | struct { | |
6388 | struct perf_event_header header; | |
6389 | u64 lost; | |
6390 | } lost_samples_event = { | |
6391 | .header = { | |
6392 | .type = PERF_RECORD_LOST_SAMPLES, | |
6393 | .misc = 0, | |
6394 | .size = sizeof(lost_samples_event), | |
6395 | }, | |
6396 | .lost = lost, | |
6397 | }; | |
6398 | ||
6399 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
6400 | ||
6401 | ret = perf_output_begin(&handle, event, | |
6402 | lost_samples_event.header.size); | |
6403 | if (ret) | |
6404 | return; | |
6405 | ||
6406 | perf_output_put(&handle, lost_samples_event); | |
6407 | perf_event__output_id_sample(event, &handle, &sample); | |
6408 | perf_output_end(&handle); | |
6409 | } | |
6410 | ||
45ac1403 AH |
6411 | /* |
6412 | * context_switch tracking | |
6413 | */ | |
6414 | ||
6415 | struct perf_switch_event { | |
6416 | struct task_struct *task; | |
6417 | struct task_struct *next_prev; | |
6418 | ||
6419 | struct { | |
6420 | struct perf_event_header header; | |
6421 | u32 next_prev_pid; | |
6422 | u32 next_prev_tid; | |
6423 | } event_id; | |
6424 | }; | |
6425 | ||
6426 | static int perf_event_switch_match(struct perf_event *event) | |
6427 | { | |
6428 | return event->attr.context_switch; | |
6429 | } | |
6430 | ||
6431 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
6432 | { | |
6433 | struct perf_switch_event *se = data; | |
6434 | struct perf_output_handle handle; | |
6435 | struct perf_sample_data sample; | |
6436 | int ret; | |
6437 | ||
6438 | if (!perf_event_switch_match(event)) | |
6439 | return; | |
6440 | ||
6441 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
6442 | if (event->ctx->task) { | |
6443 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
6444 | se->event_id.header.size = sizeof(se->event_id.header); | |
6445 | } else { | |
6446 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
6447 | se->event_id.header.size = sizeof(se->event_id); | |
6448 | se->event_id.next_prev_pid = | |
6449 | perf_event_pid(event, se->next_prev); | |
6450 | se->event_id.next_prev_tid = | |
6451 | perf_event_tid(event, se->next_prev); | |
6452 | } | |
6453 | ||
6454 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
6455 | ||
6456 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
6457 | if (ret) | |
6458 | return; | |
6459 | ||
6460 | if (event->ctx->task) | |
6461 | perf_output_put(&handle, se->event_id.header); | |
6462 | else | |
6463 | perf_output_put(&handle, se->event_id); | |
6464 | ||
6465 | perf_event__output_id_sample(event, &handle, &sample); | |
6466 | ||
6467 | perf_output_end(&handle); | |
6468 | } | |
6469 | ||
6470 | static void perf_event_switch(struct task_struct *task, | |
6471 | struct task_struct *next_prev, bool sched_in) | |
6472 | { | |
6473 | struct perf_switch_event switch_event; | |
6474 | ||
6475 | /* N.B. caller checks nr_switch_events != 0 */ | |
6476 | ||
6477 | switch_event = (struct perf_switch_event){ | |
6478 | .task = task, | |
6479 | .next_prev = next_prev, | |
6480 | .event_id = { | |
6481 | .header = { | |
6482 | /* .type */ | |
6483 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
6484 | /* .size */ | |
6485 | }, | |
6486 | /* .next_prev_pid */ | |
6487 | /* .next_prev_tid */ | |
6488 | }, | |
6489 | }; | |
6490 | ||
6491 | perf_event_aux(perf_event_switch_output, | |
6492 | &switch_event, | |
6493 | NULL); | |
6494 | } | |
6495 | ||
a78ac325 PZ |
6496 | /* |
6497 | * IRQ throttle logging | |
6498 | */ | |
6499 | ||
cdd6c482 | 6500 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
6501 | { |
6502 | struct perf_output_handle handle; | |
c980d109 | 6503 | struct perf_sample_data sample; |
a78ac325 PZ |
6504 | int ret; |
6505 | ||
6506 | struct { | |
6507 | struct perf_event_header header; | |
6508 | u64 time; | |
cca3f454 | 6509 | u64 id; |
7f453c24 | 6510 | u64 stream_id; |
a78ac325 PZ |
6511 | } throttle_event = { |
6512 | .header = { | |
cdd6c482 | 6513 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
6514 | .misc = 0, |
6515 | .size = sizeof(throttle_event), | |
6516 | }, | |
34f43927 | 6517 | .time = perf_event_clock(event), |
cdd6c482 IM |
6518 | .id = primary_event_id(event), |
6519 | .stream_id = event->id, | |
a78ac325 PZ |
6520 | }; |
6521 | ||
966ee4d6 | 6522 | if (enable) |
cdd6c482 | 6523 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 6524 | |
c980d109 ACM |
6525 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
6526 | ||
6527 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 6528 | throttle_event.header.size); |
a78ac325 PZ |
6529 | if (ret) |
6530 | return; | |
6531 | ||
6532 | perf_output_put(&handle, throttle_event); | |
c980d109 | 6533 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
6534 | perf_output_end(&handle); |
6535 | } | |
6536 | ||
ec0d7729 AS |
6537 | static void perf_log_itrace_start(struct perf_event *event) |
6538 | { | |
6539 | struct perf_output_handle handle; | |
6540 | struct perf_sample_data sample; | |
6541 | struct perf_aux_event { | |
6542 | struct perf_event_header header; | |
6543 | u32 pid; | |
6544 | u32 tid; | |
6545 | } rec; | |
6546 | int ret; | |
6547 | ||
6548 | if (event->parent) | |
6549 | event = event->parent; | |
6550 | ||
6551 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
6552 | event->hw.itrace_started) | |
6553 | return; | |
6554 | ||
ec0d7729 AS |
6555 | rec.header.type = PERF_RECORD_ITRACE_START; |
6556 | rec.header.misc = 0; | |
6557 | rec.header.size = sizeof(rec); | |
6558 | rec.pid = perf_event_pid(event, current); | |
6559 | rec.tid = perf_event_tid(event, current); | |
6560 | ||
6561 | perf_event_header__init_id(&rec.header, &sample, event); | |
6562 | ret = perf_output_begin(&handle, event, rec.header.size); | |
6563 | ||
6564 | if (ret) | |
6565 | return; | |
6566 | ||
6567 | perf_output_put(&handle, rec); | |
6568 | perf_event__output_id_sample(event, &handle, &sample); | |
6569 | ||
6570 | perf_output_end(&handle); | |
6571 | } | |
6572 | ||
f6c7d5fe | 6573 | /* |
cdd6c482 | 6574 | * Generic event overflow handling, sampling. |
f6c7d5fe PZ |
6575 | */ |
6576 | ||
a8b0ca17 | 6577 | static int __perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6578 | int throttle, struct perf_sample_data *data, |
6579 | struct pt_regs *regs) | |
f6c7d5fe | 6580 | { |
cdd6c482 IM |
6581 | int events = atomic_read(&event->event_limit); |
6582 | struct hw_perf_event *hwc = &event->hw; | |
e050e3f0 | 6583 | u64 seq; |
79f14641 PZ |
6584 | int ret = 0; |
6585 | ||
96398826 PZ |
6586 | /* |
6587 | * Non-sampling counters might still use the PMI to fold short | |
6588 | * hardware counters, ignore those. | |
6589 | */ | |
6590 | if (unlikely(!is_sampling_event(event))) | |
6591 | return 0; | |
6592 | ||
e050e3f0 SE |
6593 | seq = __this_cpu_read(perf_throttled_seq); |
6594 | if (seq != hwc->interrupts_seq) { | |
6595 | hwc->interrupts_seq = seq; | |
6596 | hwc->interrupts = 1; | |
6597 | } else { | |
6598 | hwc->interrupts++; | |
6599 | if (unlikely(throttle | |
6600 | && hwc->interrupts >= max_samples_per_tick)) { | |
6601 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 6602 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
6603 | hwc->interrupts = MAX_INTERRUPTS; |
6604 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
6605 | ret = 1; |
6606 | } | |
e050e3f0 | 6607 | } |
60db5e09 | 6608 | |
cdd6c482 | 6609 | if (event->attr.freq) { |
def0a9b2 | 6610 | u64 now = perf_clock(); |
abd50713 | 6611 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 6612 | |
abd50713 | 6613 | hwc->freq_time_stamp = now; |
bd2b5b12 | 6614 | |
abd50713 | 6615 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 6616 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
6617 | } |
6618 | ||
2023b359 PZ |
6619 | /* |
6620 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 6621 | * events |
2023b359 PZ |
6622 | */ |
6623 | ||
cdd6c482 IM |
6624 | event->pending_kill = POLL_IN; |
6625 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 6626 | ret = 1; |
cdd6c482 | 6627 | event->pending_kill = POLL_HUP; |
a8b0ca17 PZ |
6628 | event->pending_disable = 1; |
6629 | irq_work_queue(&event->pending); | |
79f14641 PZ |
6630 | } |
6631 | ||
1879445d | 6632 | event->overflow_handler(event, data, regs); |
453f19ee | 6633 | |
fed66e2c | 6634 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
6635 | event->pending_wakeup = 1; |
6636 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
6637 | } |
6638 | ||
79f14641 | 6639 | return ret; |
f6c7d5fe PZ |
6640 | } |
6641 | ||
a8b0ca17 | 6642 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
6643 | struct perf_sample_data *data, |
6644 | struct pt_regs *regs) | |
850bc73f | 6645 | { |
a8b0ca17 | 6646 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
6647 | } |
6648 | ||
15dbf27c | 6649 | /* |
cdd6c482 | 6650 | * Generic software event infrastructure |
15dbf27c PZ |
6651 | */ |
6652 | ||
b28ab83c PZ |
6653 | struct swevent_htable { |
6654 | struct swevent_hlist *swevent_hlist; | |
6655 | struct mutex hlist_mutex; | |
6656 | int hlist_refcount; | |
6657 | ||
6658 | /* Recursion avoidance in each contexts */ | |
6659 | int recursion[PERF_NR_CONTEXTS]; | |
6660 | }; | |
6661 | ||
6662 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
6663 | ||
7b4b6658 | 6664 | /* |
cdd6c482 IM |
6665 | * We directly increment event->count and keep a second value in |
6666 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
6667 | * is kept in the range [-sample_period, 0] so that we can use the |
6668 | * sign as trigger. | |
6669 | */ | |
6670 | ||
ab573844 | 6671 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 6672 | { |
cdd6c482 | 6673 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
6674 | u64 period = hwc->last_period; |
6675 | u64 nr, offset; | |
6676 | s64 old, val; | |
6677 | ||
6678 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
6679 | |
6680 | again: | |
e7850595 | 6681 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
6682 | if (val < 0) |
6683 | return 0; | |
15dbf27c | 6684 | |
7b4b6658 PZ |
6685 | nr = div64_u64(period + val, period); |
6686 | offset = nr * period; | |
6687 | val -= offset; | |
e7850595 | 6688 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 6689 | goto again; |
15dbf27c | 6690 | |
7b4b6658 | 6691 | return nr; |
15dbf27c PZ |
6692 | } |
6693 | ||
0cff784a | 6694 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 6695 | struct perf_sample_data *data, |
5622f295 | 6696 | struct pt_regs *regs) |
15dbf27c | 6697 | { |
cdd6c482 | 6698 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 6699 | int throttle = 0; |
15dbf27c | 6700 | |
0cff784a PZ |
6701 | if (!overflow) |
6702 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 6703 | |
7b4b6658 PZ |
6704 | if (hwc->interrupts == MAX_INTERRUPTS) |
6705 | return; | |
15dbf27c | 6706 | |
7b4b6658 | 6707 | for (; overflow; overflow--) { |
a8b0ca17 | 6708 | if (__perf_event_overflow(event, throttle, |
5622f295 | 6709 | data, regs)) { |
7b4b6658 PZ |
6710 | /* |
6711 | * We inhibit the overflow from happening when | |
6712 | * hwc->interrupts == MAX_INTERRUPTS. | |
6713 | */ | |
6714 | break; | |
6715 | } | |
cf450a73 | 6716 | throttle = 1; |
7b4b6658 | 6717 | } |
15dbf27c PZ |
6718 | } |
6719 | ||
a4eaf7f1 | 6720 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 6721 | struct perf_sample_data *data, |
5622f295 | 6722 | struct pt_regs *regs) |
7b4b6658 | 6723 | { |
cdd6c482 | 6724 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 6725 | |
e7850595 | 6726 | local64_add(nr, &event->count); |
d6d020e9 | 6727 | |
0cff784a PZ |
6728 | if (!regs) |
6729 | return; | |
6730 | ||
6c7e550f | 6731 | if (!is_sampling_event(event)) |
7b4b6658 | 6732 | return; |
d6d020e9 | 6733 | |
5d81e5cf AV |
6734 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
6735 | data->period = nr; | |
6736 | return perf_swevent_overflow(event, 1, data, regs); | |
6737 | } else | |
6738 | data->period = event->hw.last_period; | |
6739 | ||
0cff784a | 6740 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 6741 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 6742 | |
e7850595 | 6743 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 6744 | return; |
df1a132b | 6745 | |
a8b0ca17 | 6746 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
6747 | } |
6748 | ||
f5ffe02e FW |
6749 | static int perf_exclude_event(struct perf_event *event, |
6750 | struct pt_regs *regs) | |
6751 | { | |
a4eaf7f1 | 6752 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 6753 | return 1; |
a4eaf7f1 | 6754 | |
f5ffe02e FW |
6755 | if (regs) { |
6756 | if (event->attr.exclude_user && user_mode(regs)) | |
6757 | return 1; | |
6758 | ||
6759 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
6760 | return 1; | |
6761 | } | |
6762 | ||
6763 | return 0; | |
6764 | } | |
6765 | ||
cdd6c482 | 6766 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 6767 | enum perf_type_id type, |
6fb2915d LZ |
6768 | u32 event_id, |
6769 | struct perf_sample_data *data, | |
6770 | struct pt_regs *regs) | |
15dbf27c | 6771 | { |
cdd6c482 | 6772 | if (event->attr.type != type) |
a21ca2ca | 6773 | return 0; |
f5ffe02e | 6774 | |
cdd6c482 | 6775 | if (event->attr.config != event_id) |
15dbf27c PZ |
6776 | return 0; |
6777 | ||
f5ffe02e FW |
6778 | if (perf_exclude_event(event, regs)) |
6779 | return 0; | |
15dbf27c PZ |
6780 | |
6781 | return 1; | |
6782 | } | |
6783 | ||
76e1d904 FW |
6784 | static inline u64 swevent_hash(u64 type, u32 event_id) |
6785 | { | |
6786 | u64 val = event_id | (type << 32); | |
6787 | ||
6788 | return hash_64(val, SWEVENT_HLIST_BITS); | |
6789 | } | |
6790 | ||
49f135ed FW |
6791 | static inline struct hlist_head * |
6792 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 6793 | { |
49f135ed FW |
6794 | u64 hash = swevent_hash(type, event_id); |
6795 | ||
6796 | return &hlist->heads[hash]; | |
6797 | } | |
76e1d904 | 6798 | |
49f135ed FW |
6799 | /* For the read side: events when they trigger */ |
6800 | static inline struct hlist_head * | |
b28ab83c | 6801 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
6802 | { |
6803 | struct swevent_hlist *hlist; | |
76e1d904 | 6804 | |
b28ab83c | 6805 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
6806 | if (!hlist) |
6807 | return NULL; | |
6808 | ||
49f135ed FW |
6809 | return __find_swevent_head(hlist, type, event_id); |
6810 | } | |
6811 | ||
6812 | /* For the event head insertion and removal in the hlist */ | |
6813 | static inline struct hlist_head * | |
b28ab83c | 6814 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
6815 | { |
6816 | struct swevent_hlist *hlist; | |
6817 | u32 event_id = event->attr.config; | |
6818 | u64 type = event->attr.type; | |
6819 | ||
6820 | /* | |
6821 | * Event scheduling is always serialized against hlist allocation | |
6822 | * and release. Which makes the protected version suitable here. | |
6823 | * The context lock guarantees that. | |
6824 | */ | |
b28ab83c | 6825 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
6826 | lockdep_is_held(&event->ctx->lock)); |
6827 | if (!hlist) | |
6828 | return NULL; | |
6829 | ||
6830 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
6831 | } |
6832 | ||
6833 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 6834 | u64 nr, |
76e1d904 FW |
6835 | struct perf_sample_data *data, |
6836 | struct pt_regs *regs) | |
15dbf27c | 6837 | { |
4a32fea9 | 6838 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 6839 | struct perf_event *event; |
76e1d904 | 6840 | struct hlist_head *head; |
15dbf27c | 6841 | |
76e1d904 | 6842 | rcu_read_lock(); |
b28ab83c | 6843 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
6844 | if (!head) |
6845 | goto end; | |
6846 | ||
b67bfe0d | 6847 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 6848 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 6849 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 6850 | } |
76e1d904 FW |
6851 | end: |
6852 | rcu_read_unlock(); | |
15dbf27c PZ |
6853 | } |
6854 | ||
86038c5e PZI |
6855 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
6856 | ||
4ed7c92d | 6857 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 6858 | { |
4a32fea9 | 6859 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 6860 | |
b28ab83c | 6861 | return get_recursion_context(swhash->recursion); |
96f6d444 | 6862 | } |
645e8cc0 | 6863 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 6864 | |
fa9f90be | 6865 | inline void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 6866 | { |
4a32fea9 | 6867 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 6868 | |
b28ab83c | 6869 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 6870 | } |
15dbf27c | 6871 | |
86038c5e | 6872 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 6873 | { |
a4234bfc | 6874 | struct perf_sample_data data; |
4ed7c92d | 6875 | |
86038c5e | 6876 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 6877 | return; |
a4234bfc | 6878 | |
fd0d000b | 6879 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 6880 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
6881 | } |
6882 | ||
6883 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
6884 | { | |
6885 | int rctx; | |
6886 | ||
6887 | preempt_disable_notrace(); | |
6888 | rctx = perf_swevent_get_recursion_context(); | |
6889 | if (unlikely(rctx < 0)) | |
6890 | goto fail; | |
6891 | ||
6892 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
6893 | |
6894 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 6895 | fail: |
1c024eca | 6896 | preempt_enable_notrace(); |
b8e83514 PZ |
6897 | } |
6898 | ||
cdd6c482 | 6899 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 6900 | { |
15dbf27c PZ |
6901 | } |
6902 | ||
a4eaf7f1 | 6903 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 6904 | { |
4a32fea9 | 6905 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 6906 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
6907 | struct hlist_head *head; |
6908 | ||
6c7e550f | 6909 | if (is_sampling_event(event)) { |
7b4b6658 | 6910 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 6911 | perf_swevent_set_period(event); |
7b4b6658 | 6912 | } |
76e1d904 | 6913 | |
a4eaf7f1 PZ |
6914 | hwc->state = !(flags & PERF_EF_START); |
6915 | ||
b28ab83c | 6916 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 6917 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
6918 | return -EINVAL; |
6919 | ||
6920 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 6921 | perf_event_update_userpage(event); |
76e1d904 | 6922 | |
15dbf27c PZ |
6923 | return 0; |
6924 | } | |
6925 | ||
a4eaf7f1 | 6926 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 6927 | { |
76e1d904 | 6928 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
6929 | } |
6930 | ||
a4eaf7f1 | 6931 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 6932 | { |
a4eaf7f1 | 6933 | event->hw.state = 0; |
d6d020e9 | 6934 | } |
aa9c4c0f | 6935 | |
a4eaf7f1 | 6936 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 6937 | { |
a4eaf7f1 | 6938 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
6939 | } |
6940 | ||
49f135ed FW |
6941 | /* Deref the hlist from the update side */ |
6942 | static inline struct swevent_hlist * | |
b28ab83c | 6943 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 6944 | { |
b28ab83c PZ |
6945 | return rcu_dereference_protected(swhash->swevent_hlist, |
6946 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
6947 | } |
6948 | ||
b28ab83c | 6949 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 6950 | { |
b28ab83c | 6951 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 6952 | |
49f135ed | 6953 | if (!hlist) |
76e1d904 FW |
6954 | return; |
6955 | ||
70691d4a | 6956 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 6957 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
6958 | } |
6959 | ||
3b364d7b | 6960 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 6961 | { |
b28ab83c | 6962 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 6963 | |
b28ab83c | 6964 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 6965 | |
b28ab83c PZ |
6966 | if (!--swhash->hlist_refcount) |
6967 | swevent_hlist_release(swhash); | |
76e1d904 | 6968 | |
b28ab83c | 6969 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
6970 | } |
6971 | ||
3b364d7b | 6972 | static void swevent_hlist_put(void) |
76e1d904 FW |
6973 | { |
6974 | int cpu; | |
6975 | ||
76e1d904 | 6976 | for_each_possible_cpu(cpu) |
3b364d7b | 6977 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
6978 | } |
6979 | ||
3b364d7b | 6980 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 6981 | { |
b28ab83c | 6982 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
6983 | int err = 0; |
6984 | ||
b28ab83c | 6985 | mutex_lock(&swhash->hlist_mutex); |
b28ab83c | 6986 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
76e1d904 FW |
6987 | struct swevent_hlist *hlist; |
6988 | ||
6989 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
6990 | if (!hlist) { | |
6991 | err = -ENOMEM; | |
6992 | goto exit; | |
6993 | } | |
b28ab83c | 6994 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 6995 | } |
b28ab83c | 6996 | swhash->hlist_refcount++; |
9ed6060d | 6997 | exit: |
b28ab83c | 6998 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
6999 | |
7000 | return err; | |
7001 | } | |
7002 | ||
3b364d7b | 7003 | static int swevent_hlist_get(void) |
76e1d904 | 7004 | { |
3b364d7b | 7005 | int err, cpu, failed_cpu; |
76e1d904 | 7006 | |
76e1d904 FW |
7007 | get_online_cpus(); |
7008 | for_each_possible_cpu(cpu) { | |
3b364d7b | 7009 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
7010 | if (err) { |
7011 | failed_cpu = cpu; | |
7012 | goto fail; | |
7013 | } | |
7014 | } | |
7015 | put_online_cpus(); | |
7016 | ||
7017 | return 0; | |
9ed6060d | 7018 | fail: |
76e1d904 FW |
7019 | for_each_possible_cpu(cpu) { |
7020 | if (cpu == failed_cpu) | |
7021 | break; | |
3b364d7b | 7022 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
7023 | } |
7024 | ||
7025 | put_online_cpus(); | |
7026 | return err; | |
7027 | } | |
7028 | ||
c5905afb | 7029 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 7030 | |
b0a873eb PZ |
7031 | static void sw_perf_event_destroy(struct perf_event *event) |
7032 | { | |
7033 | u64 event_id = event->attr.config; | |
95476b64 | 7034 | |
b0a873eb PZ |
7035 | WARN_ON(event->parent); |
7036 | ||
c5905afb | 7037 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 7038 | swevent_hlist_put(); |
b0a873eb PZ |
7039 | } |
7040 | ||
7041 | static int perf_swevent_init(struct perf_event *event) | |
7042 | { | |
8176cced | 7043 | u64 event_id = event->attr.config; |
b0a873eb PZ |
7044 | |
7045 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7046 | return -ENOENT; | |
7047 | ||
2481c5fa SE |
7048 | /* |
7049 | * no branch sampling for software events | |
7050 | */ | |
7051 | if (has_branch_stack(event)) | |
7052 | return -EOPNOTSUPP; | |
7053 | ||
b0a873eb PZ |
7054 | switch (event_id) { |
7055 | case PERF_COUNT_SW_CPU_CLOCK: | |
7056 | case PERF_COUNT_SW_TASK_CLOCK: | |
7057 | return -ENOENT; | |
7058 | ||
7059 | default: | |
7060 | break; | |
7061 | } | |
7062 | ||
ce677831 | 7063 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
7064 | return -ENOENT; |
7065 | ||
7066 | if (!event->parent) { | |
7067 | int err; | |
7068 | ||
3b364d7b | 7069 | err = swevent_hlist_get(); |
b0a873eb PZ |
7070 | if (err) |
7071 | return err; | |
7072 | ||
c5905afb | 7073 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
7074 | event->destroy = sw_perf_event_destroy; |
7075 | } | |
7076 | ||
7077 | return 0; | |
7078 | } | |
7079 | ||
7080 | static struct pmu perf_swevent = { | |
89a1e187 | 7081 | .task_ctx_nr = perf_sw_context, |
95476b64 | 7082 | |
34f43927 PZ |
7083 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7084 | ||
b0a873eb | 7085 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
7086 | .add = perf_swevent_add, |
7087 | .del = perf_swevent_del, | |
7088 | .start = perf_swevent_start, | |
7089 | .stop = perf_swevent_stop, | |
1c024eca | 7090 | .read = perf_swevent_read, |
1c024eca PZ |
7091 | }; |
7092 | ||
b0a873eb PZ |
7093 | #ifdef CONFIG_EVENT_TRACING |
7094 | ||
1c024eca PZ |
7095 | static int perf_tp_filter_match(struct perf_event *event, |
7096 | struct perf_sample_data *data) | |
7097 | { | |
7098 | void *record = data->raw->data; | |
7099 | ||
b71b437e PZ |
7100 | /* only top level events have filters set */ |
7101 | if (event->parent) | |
7102 | event = event->parent; | |
7103 | ||
1c024eca PZ |
7104 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
7105 | return 1; | |
7106 | return 0; | |
7107 | } | |
7108 | ||
7109 | static int perf_tp_event_match(struct perf_event *event, | |
7110 | struct perf_sample_data *data, | |
7111 | struct pt_regs *regs) | |
7112 | { | |
a0f7d0f7 FW |
7113 | if (event->hw.state & PERF_HES_STOPPED) |
7114 | return 0; | |
580d607c PZ |
7115 | /* |
7116 | * All tracepoints are from kernel-space. | |
7117 | */ | |
7118 | if (event->attr.exclude_kernel) | |
1c024eca PZ |
7119 | return 0; |
7120 | ||
7121 | if (!perf_tp_filter_match(event, data)) | |
7122 | return 0; | |
7123 | ||
7124 | return 1; | |
7125 | } | |
7126 | ||
7127 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, | |
e6dab5ff AV |
7128 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
7129 | struct task_struct *task) | |
95476b64 FW |
7130 | { |
7131 | struct perf_sample_data data; | |
1c024eca | 7132 | struct perf_event *event; |
1c024eca | 7133 | |
95476b64 FW |
7134 | struct perf_raw_record raw = { |
7135 | .size = entry_size, | |
7136 | .data = record, | |
7137 | }; | |
7138 | ||
fd0d000b | 7139 | perf_sample_data_init(&data, addr, 0); |
95476b64 FW |
7140 | data.raw = &raw; |
7141 | ||
b67bfe0d | 7142 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 7143 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 7144 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 7145 | } |
ecc55f84 | 7146 | |
e6dab5ff AV |
7147 | /* |
7148 | * If we got specified a target task, also iterate its context and | |
7149 | * deliver this event there too. | |
7150 | */ | |
7151 | if (task && task != current) { | |
7152 | struct perf_event_context *ctx; | |
7153 | struct trace_entry *entry = record; | |
7154 | ||
7155 | rcu_read_lock(); | |
7156 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
7157 | if (!ctx) | |
7158 | goto unlock; | |
7159 | ||
7160 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
7161 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7162 | continue; | |
7163 | if (event->attr.config != entry->type) | |
7164 | continue; | |
7165 | if (perf_tp_event_match(event, &data, regs)) | |
7166 | perf_swevent_event(event, count, &data, regs); | |
7167 | } | |
7168 | unlock: | |
7169 | rcu_read_unlock(); | |
7170 | } | |
7171 | ||
ecc55f84 | 7172 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
7173 | } |
7174 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
7175 | ||
cdd6c482 | 7176 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 7177 | { |
1c024eca | 7178 | perf_trace_destroy(event); |
e077df4f PZ |
7179 | } |
7180 | ||
b0a873eb | 7181 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 7182 | { |
76e1d904 FW |
7183 | int err; |
7184 | ||
b0a873eb PZ |
7185 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
7186 | return -ENOENT; | |
7187 | ||
2481c5fa SE |
7188 | /* |
7189 | * no branch sampling for tracepoint events | |
7190 | */ | |
7191 | if (has_branch_stack(event)) | |
7192 | return -EOPNOTSUPP; | |
7193 | ||
1c024eca PZ |
7194 | err = perf_trace_init(event); |
7195 | if (err) | |
b0a873eb | 7196 | return err; |
e077df4f | 7197 | |
cdd6c482 | 7198 | event->destroy = tp_perf_event_destroy; |
e077df4f | 7199 | |
b0a873eb PZ |
7200 | return 0; |
7201 | } | |
7202 | ||
7203 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
7204 | .task_ctx_nr = perf_sw_context, |
7205 | ||
b0a873eb | 7206 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
7207 | .add = perf_trace_add, |
7208 | .del = perf_trace_del, | |
7209 | .start = perf_swevent_start, | |
7210 | .stop = perf_swevent_stop, | |
b0a873eb | 7211 | .read = perf_swevent_read, |
b0a873eb PZ |
7212 | }; |
7213 | ||
7214 | static inline void perf_tp_register(void) | |
7215 | { | |
2e80a82a | 7216 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e077df4f | 7217 | } |
6fb2915d LZ |
7218 | |
7219 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) | |
7220 | { | |
7221 | char *filter_str; | |
7222 | int ret; | |
7223 | ||
7224 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7225 | return -EINVAL; | |
7226 | ||
7227 | filter_str = strndup_user(arg, PAGE_SIZE); | |
7228 | if (IS_ERR(filter_str)) | |
7229 | return PTR_ERR(filter_str); | |
7230 | ||
7231 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
7232 | ||
7233 | kfree(filter_str); | |
7234 | return ret; | |
7235 | } | |
7236 | ||
7237 | static void perf_event_free_filter(struct perf_event *event) | |
7238 | { | |
7239 | ftrace_profile_free_filter(event); | |
7240 | } | |
7241 | ||
2541517c AS |
7242 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7243 | { | |
7244 | struct bpf_prog *prog; | |
7245 | ||
7246 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
7247 | return -EINVAL; | |
7248 | ||
7249 | if (event->tp_event->prog) | |
7250 | return -EEXIST; | |
7251 | ||
04a22fae WN |
7252 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
7253 | /* bpf programs can only be attached to u/kprobes */ | |
2541517c AS |
7254 | return -EINVAL; |
7255 | ||
7256 | prog = bpf_prog_get(prog_fd); | |
7257 | if (IS_ERR(prog)) | |
7258 | return PTR_ERR(prog); | |
7259 | ||
6c373ca8 | 7260 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
2541517c AS |
7261 | /* valid fd, but invalid bpf program type */ |
7262 | bpf_prog_put(prog); | |
7263 | return -EINVAL; | |
7264 | } | |
7265 | ||
7266 | event->tp_event->prog = prog; | |
7267 | ||
7268 | return 0; | |
7269 | } | |
7270 | ||
7271 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7272 | { | |
7273 | struct bpf_prog *prog; | |
7274 | ||
7275 | if (!event->tp_event) | |
7276 | return; | |
7277 | ||
7278 | prog = event->tp_event->prog; | |
7279 | if (prog) { | |
7280 | event->tp_event->prog = NULL; | |
7281 | bpf_prog_put(prog); | |
7282 | } | |
7283 | } | |
7284 | ||
e077df4f | 7285 | #else |
6fb2915d | 7286 | |
b0a873eb | 7287 | static inline void perf_tp_register(void) |
e077df4f | 7288 | { |
e077df4f | 7289 | } |
6fb2915d LZ |
7290 | |
7291 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) | |
7292 | { | |
7293 | return -ENOENT; | |
7294 | } | |
7295 | ||
7296 | static void perf_event_free_filter(struct perf_event *event) | |
7297 | { | |
7298 | } | |
7299 | ||
2541517c AS |
7300 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
7301 | { | |
7302 | return -ENOENT; | |
7303 | } | |
7304 | ||
7305 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
7306 | { | |
7307 | } | |
07b139c8 | 7308 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 7309 | |
24f1e32c | 7310 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 7311 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 7312 | { |
f5ffe02e FW |
7313 | struct perf_sample_data sample; |
7314 | struct pt_regs *regs = data; | |
7315 | ||
fd0d000b | 7316 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 7317 | |
a4eaf7f1 | 7318 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 7319 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
7320 | } |
7321 | #endif | |
7322 | ||
b0a873eb PZ |
7323 | /* |
7324 | * hrtimer based swevent callback | |
7325 | */ | |
f29ac756 | 7326 | |
b0a873eb | 7327 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 7328 | { |
b0a873eb PZ |
7329 | enum hrtimer_restart ret = HRTIMER_RESTART; |
7330 | struct perf_sample_data data; | |
7331 | struct pt_regs *regs; | |
7332 | struct perf_event *event; | |
7333 | u64 period; | |
f29ac756 | 7334 | |
b0a873eb | 7335 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
7336 | |
7337 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
7338 | return HRTIMER_NORESTART; | |
7339 | ||
b0a873eb | 7340 | event->pmu->read(event); |
f344011c | 7341 | |
fd0d000b | 7342 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
7343 | regs = get_irq_regs(); |
7344 | ||
7345 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 7346 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 7347 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
7348 | ret = HRTIMER_NORESTART; |
7349 | } | |
24f1e32c | 7350 | |
b0a873eb PZ |
7351 | period = max_t(u64, 10000, event->hw.sample_period); |
7352 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 7353 | |
b0a873eb | 7354 | return ret; |
f29ac756 PZ |
7355 | } |
7356 | ||
b0a873eb | 7357 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 7358 | { |
b0a873eb | 7359 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
7360 | s64 period; |
7361 | ||
7362 | if (!is_sampling_event(event)) | |
7363 | return; | |
f5ffe02e | 7364 | |
5d508e82 FBH |
7365 | period = local64_read(&hwc->period_left); |
7366 | if (period) { | |
7367 | if (period < 0) | |
7368 | period = 10000; | |
fa407f35 | 7369 | |
5d508e82 FBH |
7370 | local64_set(&hwc->period_left, 0); |
7371 | } else { | |
7372 | period = max_t(u64, 10000, hwc->sample_period); | |
7373 | } | |
3497d206 TG |
7374 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
7375 | HRTIMER_MODE_REL_PINNED); | |
24f1e32c | 7376 | } |
b0a873eb PZ |
7377 | |
7378 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 7379 | { |
b0a873eb PZ |
7380 | struct hw_perf_event *hwc = &event->hw; |
7381 | ||
6c7e550f | 7382 | if (is_sampling_event(event)) { |
b0a873eb | 7383 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 7384 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
7385 | |
7386 | hrtimer_cancel(&hwc->hrtimer); | |
7387 | } | |
24f1e32c FW |
7388 | } |
7389 | ||
ba3dd36c PZ |
7390 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
7391 | { | |
7392 | struct hw_perf_event *hwc = &event->hw; | |
7393 | ||
7394 | if (!is_sampling_event(event)) | |
7395 | return; | |
7396 | ||
7397 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
7398 | hwc->hrtimer.function = perf_swevent_hrtimer; | |
7399 | ||
7400 | /* | |
7401 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
7402 | * mapping and avoid the whole period adjust feedback stuff. | |
7403 | */ | |
7404 | if (event->attr.freq) { | |
7405 | long freq = event->attr.sample_freq; | |
7406 | ||
7407 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
7408 | hwc->sample_period = event->attr.sample_period; | |
7409 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 7410 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
7411 | event->attr.freq = 0; |
7412 | } | |
7413 | } | |
7414 | ||
b0a873eb PZ |
7415 | /* |
7416 | * Software event: cpu wall time clock | |
7417 | */ | |
7418 | ||
7419 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 7420 | { |
b0a873eb PZ |
7421 | s64 prev; |
7422 | u64 now; | |
7423 | ||
a4eaf7f1 | 7424 | now = local_clock(); |
b0a873eb PZ |
7425 | prev = local64_xchg(&event->hw.prev_count, now); |
7426 | local64_add(now - prev, &event->count); | |
24f1e32c | 7427 | } |
24f1e32c | 7428 | |
a4eaf7f1 | 7429 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 7430 | { |
a4eaf7f1 | 7431 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 7432 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
7433 | } |
7434 | ||
a4eaf7f1 | 7435 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 7436 | { |
b0a873eb PZ |
7437 | perf_swevent_cancel_hrtimer(event); |
7438 | cpu_clock_event_update(event); | |
7439 | } | |
f29ac756 | 7440 | |
a4eaf7f1 PZ |
7441 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
7442 | { | |
7443 | if (flags & PERF_EF_START) | |
7444 | cpu_clock_event_start(event, flags); | |
6a694a60 | 7445 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
7446 | |
7447 | return 0; | |
7448 | } | |
7449 | ||
7450 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
7451 | { | |
7452 | cpu_clock_event_stop(event, flags); | |
7453 | } | |
7454 | ||
b0a873eb PZ |
7455 | static void cpu_clock_event_read(struct perf_event *event) |
7456 | { | |
7457 | cpu_clock_event_update(event); | |
7458 | } | |
f344011c | 7459 | |
b0a873eb PZ |
7460 | static int cpu_clock_event_init(struct perf_event *event) |
7461 | { | |
7462 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
7463 | return -ENOENT; | |
7464 | ||
7465 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
7466 | return -ENOENT; | |
7467 | ||
2481c5fa SE |
7468 | /* |
7469 | * no branch sampling for software events | |
7470 | */ | |
7471 | if (has_branch_stack(event)) | |
7472 | return -EOPNOTSUPP; | |
7473 | ||
ba3dd36c PZ |
7474 | perf_swevent_init_hrtimer(event); |
7475 | ||
b0a873eb | 7476 | return 0; |
f29ac756 PZ |
7477 | } |
7478 | ||
b0a873eb | 7479 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
7480 | .task_ctx_nr = perf_sw_context, |
7481 | ||
34f43927 PZ |
7482 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7483 | ||
b0a873eb | 7484 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
7485 | .add = cpu_clock_event_add, |
7486 | .del = cpu_clock_event_del, | |
7487 | .start = cpu_clock_event_start, | |
7488 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
7489 | .read = cpu_clock_event_read, |
7490 | }; | |
7491 | ||
7492 | /* | |
7493 | * Software event: task time clock | |
7494 | */ | |
7495 | ||
7496 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 7497 | { |
b0a873eb PZ |
7498 | u64 prev; |
7499 | s64 delta; | |
5c92d124 | 7500 | |
b0a873eb PZ |
7501 | prev = local64_xchg(&event->hw.prev_count, now); |
7502 | delta = now - prev; | |
7503 | local64_add(delta, &event->count); | |
7504 | } | |
5c92d124 | 7505 | |
a4eaf7f1 | 7506 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 7507 | { |
a4eaf7f1 | 7508 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 7509 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
7510 | } |
7511 | ||
a4eaf7f1 | 7512 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
7513 | { |
7514 | perf_swevent_cancel_hrtimer(event); | |
7515 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
7516 | } |
7517 | ||
7518 | static int task_clock_event_add(struct perf_event *event, int flags) | |
7519 | { | |
7520 | if (flags & PERF_EF_START) | |
7521 | task_clock_event_start(event, flags); | |
6a694a60 | 7522 | perf_event_update_userpage(event); |
b0a873eb | 7523 | |
a4eaf7f1 PZ |
7524 | return 0; |
7525 | } | |
7526 | ||
7527 | static void task_clock_event_del(struct perf_event *event, int flags) | |
7528 | { | |
7529 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
7530 | } |
7531 | ||
7532 | static void task_clock_event_read(struct perf_event *event) | |
7533 | { | |
768a06e2 PZ |
7534 | u64 now = perf_clock(); |
7535 | u64 delta = now - event->ctx->timestamp; | |
7536 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
7537 | |
7538 | task_clock_event_update(event, time); | |
7539 | } | |
7540 | ||
7541 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 7542 | { |
b0a873eb PZ |
7543 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
7544 | return -ENOENT; | |
7545 | ||
7546 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
7547 | return -ENOENT; | |
7548 | ||
2481c5fa SE |
7549 | /* |
7550 | * no branch sampling for software events | |
7551 | */ | |
7552 | if (has_branch_stack(event)) | |
7553 | return -EOPNOTSUPP; | |
7554 | ||
ba3dd36c PZ |
7555 | perf_swevent_init_hrtimer(event); |
7556 | ||
b0a873eb | 7557 | return 0; |
6fb2915d LZ |
7558 | } |
7559 | ||
b0a873eb | 7560 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
7561 | .task_ctx_nr = perf_sw_context, |
7562 | ||
34f43927 PZ |
7563 | .capabilities = PERF_PMU_CAP_NO_NMI, |
7564 | ||
b0a873eb | 7565 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
7566 | .add = task_clock_event_add, |
7567 | .del = task_clock_event_del, | |
7568 | .start = task_clock_event_start, | |
7569 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
7570 | .read = task_clock_event_read, |
7571 | }; | |
6fb2915d | 7572 | |
ad5133b7 | 7573 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 7574 | { |
e077df4f | 7575 | } |
6fb2915d | 7576 | |
fbbe0701 SB |
7577 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
7578 | { | |
7579 | } | |
7580 | ||
ad5133b7 | 7581 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 7582 | { |
ad5133b7 | 7583 | return 0; |
6fb2915d LZ |
7584 | } |
7585 | ||
18ab2cd3 | 7586 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
7587 | |
7588 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 7589 | { |
fbbe0701 SB |
7590 | __this_cpu_write(nop_txn_flags, flags); |
7591 | ||
7592 | if (flags & ~PERF_PMU_TXN_ADD) | |
7593 | return; | |
7594 | ||
ad5133b7 | 7595 | perf_pmu_disable(pmu); |
6fb2915d LZ |
7596 | } |
7597 | ||
ad5133b7 PZ |
7598 | static int perf_pmu_commit_txn(struct pmu *pmu) |
7599 | { | |
fbbe0701 SB |
7600 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
7601 | ||
7602 | __this_cpu_write(nop_txn_flags, 0); | |
7603 | ||
7604 | if (flags & ~PERF_PMU_TXN_ADD) | |
7605 | return 0; | |
7606 | ||
ad5133b7 PZ |
7607 | perf_pmu_enable(pmu); |
7608 | return 0; | |
7609 | } | |
e077df4f | 7610 | |
ad5133b7 | 7611 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 7612 | { |
fbbe0701 SB |
7613 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
7614 | ||
7615 | __this_cpu_write(nop_txn_flags, 0); | |
7616 | ||
7617 | if (flags & ~PERF_PMU_TXN_ADD) | |
7618 | return; | |
7619 | ||
ad5133b7 | 7620 | perf_pmu_enable(pmu); |
24f1e32c FW |
7621 | } |
7622 | ||
35edc2a5 PZ |
7623 | static int perf_event_idx_default(struct perf_event *event) |
7624 | { | |
c719f560 | 7625 | return 0; |
35edc2a5 PZ |
7626 | } |
7627 | ||
8dc85d54 PZ |
7628 | /* |
7629 | * Ensures all contexts with the same task_ctx_nr have the same | |
7630 | * pmu_cpu_context too. | |
7631 | */ | |
9e317041 | 7632 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 7633 | { |
8dc85d54 | 7634 | struct pmu *pmu; |
b326e956 | 7635 | |
8dc85d54 PZ |
7636 | if (ctxn < 0) |
7637 | return NULL; | |
24f1e32c | 7638 | |
8dc85d54 PZ |
7639 | list_for_each_entry(pmu, &pmus, entry) { |
7640 | if (pmu->task_ctx_nr == ctxn) | |
7641 | return pmu->pmu_cpu_context; | |
7642 | } | |
24f1e32c | 7643 | |
8dc85d54 | 7644 | return NULL; |
24f1e32c FW |
7645 | } |
7646 | ||
51676957 | 7647 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
24f1e32c | 7648 | { |
51676957 PZ |
7649 | int cpu; |
7650 | ||
7651 | for_each_possible_cpu(cpu) { | |
7652 | struct perf_cpu_context *cpuctx; | |
7653 | ||
7654 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
7655 | ||
3f1f3320 PZ |
7656 | if (cpuctx->unique_pmu == old_pmu) |
7657 | cpuctx->unique_pmu = pmu; | |
51676957 PZ |
7658 | } |
7659 | } | |
7660 | ||
7661 | static void free_pmu_context(struct pmu *pmu) | |
7662 | { | |
7663 | struct pmu *i; | |
f5ffe02e | 7664 | |
8dc85d54 | 7665 | mutex_lock(&pmus_lock); |
0475f9ea | 7666 | /* |
8dc85d54 | 7667 | * Like a real lame refcount. |
0475f9ea | 7668 | */ |
51676957 PZ |
7669 | list_for_each_entry(i, &pmus, entry) { |
7670 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { | |
7671 | update_pmu_context(i, pmu); | |
8dc85d54 | 7672 | goto out; |
51676957 | 7673 | } |
8dc85d54 | 7674 | } |
d6d020e9 | 7675 | |
51676957 | 7676 | free_percpu(pmu->pmu_cpu_context); |
8dc85d54 PZ |
7677 | out: |
7678 | mutex_unlock(&pmus_lock); | |
24f1e32c | 7679 | } |
2e80a82a | 7680 | static struct idr pmu_idr; |
d6d020e9 | 7681 | |
abe43400 PZ |
7682 | static ssize_t |
7683 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
7684 | { | |
7685 | struct pmu *pmu = dev_get_drvdata(dev); | |
7686 | ||
7687 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
7688 | } | |
90826ca7 | 7689 | static DEVICE_ATTR_RO(type); |
abe43400 | 7690 | |
62b85639 SE |
7691 | static ssize_t |
7692 | perf_event_mux_interval_ms_show(struct device *dev, | |
7693 | struct device_attribute *attr, | |
7694 | char *page) | |
7695 | { | |
7696 | struct pmu *pmu = dev_get_drvdata(dev); | |
7697 | ||
7698 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
7699 | } | |
7700 | ||
272325c4 PZ |
7701 | static DEFINE_MUTEX(mux_interval_mutex); |
7702 | ||
62b85639 SE |
7703 | static ssize_t |
7704 | perf_event_mux_interval_ms_store(struct device *dev, | |
7705 | struct device_attribute *attr, | |
7706 | const char *buf, size_t count) | |
7707 | { | |
7708 | struct pmu *pmu = dev_get_drvdata(dev); | |
7709 | int timer, cpu, ret; | |
7710 | ||
7711 | ret = kstrtoint(buf, 0, &timer); | |
7712 | if (ret) | |
7713 | return ret; | |
7714 | ||
7715 | if (timer < 1) | |
7716 | return -EINVAL; | |
7717 | ||
7718 | /* same value, noting to do */ | |
7719 | if (timer == pmu->hrtimer_interval_ms) | |
7720 | return count; | |
7721 | ||
272325c4 | 7722 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
7723 | pmu->hrtimer_interval_ms = timer; |
7724 | ||
7725 | /* update all cpuctx for this PMU */ | |
272325c4 PZ |
7726 | get_online_cpus(); |
7727 | for_each_online_cpu(cpu) { | |
62b85639 SE |
7728 | struct perf_cpu_context *cpuctx; |
7729 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
7730 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
7731 | ||
272325c4 PZ |
7732 | cpu_function_call(cpu, |
7733 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 7734 | } |
272325c4 PZ |
7735 | put_online_cpus(); |
7736 | mutex_unlock(&mux_interval_mutex); | |
62b85639 SE |
7737 | |
7738 | return count; | |
7739 | } | |
90826ca7 | 7740 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 7741 | |
90826ca7 GKH |
7742 | static struct attribute *pmu_dev_attrs[] = { |
7743 | &dev_attr_type.attr, | |
7744 | &dev_attr_perf_event_mux_interval_ms.attr, | |
7745 | NULL, | |
abe43400 | 7746 | }; |
90826ca7 | 7747 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
7748 | |
7749 | static int pmu_bus_running; | |
7750 | static struct bus_type pmu_bus = { | |
7751 | .name = "event_source", | |
90826ca7 | 7752 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
7753 | }; |
7754 | ||
7755 | static void pmu_dev_release(struct device *dev) | |
7756 | { | |
7757 | kfree(dev); | |
7758 | } | |
7759 | ||
7760 | static int pmu_dev_alloc(struct pmu *pmu) | |
7761 | { | |
7762 | int ret = -ENOMEM; | |
7763 | ||
7764 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
7765 | if (!pmu->dev) | |
7766 | goto out; | |
7767 | ||
0c9d42ed | 7768 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
7769 | device_initialize(pmu->dev); |
7770 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
7771 | if (ret) | |
7772 | goto free_dev; | |
7773 | ||
7774 | dev_set_drvdata(pmu->dev, pmu); | |
7775 | pmu->dev->bus = &pmu_bus; | |
7776 | pmu->dev->release = pmu_dev_release; | |
7777 | ret = device_add(pmu->dev); | |
7778 | if (ret) | |
7779 | goto free_dev; | |
7780 | ||
7781 | out: | |
7782 | return ret; | |
7783 | ||
7784 | free_dev: | |
7785 | put_device(pmu->dev); | |
7786 | goto out; | |
7787 | } | |
7788 | ||
547e9fd7 | 7789 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 7790 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 7791 | |
03d8e80b | 7792 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 7793 | { |
108b02cf | 7794 | int cpu, ret; |
24f1e32c | 7795 | |
b0a873eb | 7796 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
7797 | ret = -ENOMEM; |
7798 | pmu->pmu_disable_count = alloc_percpu(int); | |
7799 | if (!pmu->pmu_disable_count) | |
7800 | goto unlock; | |
f29ac756 | 7801 | |
2e80a82a PZ |
7802 | pmu->type = -1; |
7803 | if (!name) | |
7804 | goto skip_type; | |
7805 | pmu->name = name; | |
7806 | ||
7807 | if (type < 0) { | |
0e9c3be2 TH |
7808 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
7809 | if (type < 0) { | |
7810 | ret = type; | |
2e80a82a PZ |
7811 | goto free_pdc; |
7812 | } | |
7813 | } | |
7814 | pmu->type = type; | |
7815 | ||
abe43400 PZ |
7816 | if (pmu_bus_running) { |
7817 | ret = pmu_dev_alloc(pmu); | |
7818 | if (ret) | |
7819 | goto free_idr; | |
7820 | } | |
7821 | ||
2e80a82a | 7822 | skip_type: |
26657848 PZ |
7823 | if (pmu->task_ctx_nr == perf_hw_context) { |
7824 | static int hw_context_taken = 0; | |
7825 | ||
7826 | if (WARN_ON_ONCE(hw_context_taken)) | |
7827 | pmu->task_ctx_nr = perf_invalid_context; | |
7828 | ||
7829 | hw_context_taken = 1; | |
7830 | } | |
7831 | ||
8dc85d54 PZ |
7832 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
7833 | if (pmu->pmu_cpu_context) | |
7834 | goto got_cpu_context; | |
f29ac756 | 7835 | |
c4814202 | 7836 | ret = -ENOMEM; |
108b02cf PZ |
7837 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
7838 | if (!pmu->pmu_cpu_context) | |
abe43400 | 7839 | goto free_dev; |
f344011c | 7840 | |
108b02cf PZ |
7841 | for_each_possible_cpu(cpu) { |
7842 | struct perf_cpu_context *cpuctx; | |
7843 | ||
7844 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 7845 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 7846 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 7847 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 7848 | cpuctx->ctx.pmu = pmu; |
9e630205 | 7849 | |
272325c4 | 7850 | __perf_mux_hrtimer_init(cpuctx, cpu); |
9e630205 | 7851 | |
3f1f3320 | 7852 | cpuctx->unique_pmu = pmu; |
108b02cf | 7853 | } |
76e1d904 | 7854 | |
8dc85d54 | 7855 | got_cpu_context: |
ad5133b7 PZ |
7856 | if (!pmu->start_txn) { |
7857 | if (pmu->pmu_enable) { | |
7858 | /* | |
7859 | * If we have pmu_enable/pmu_disable calls, install | |
7860 | * transaction stubs that use that to try and batch | |
7861 | * hardware accesses. | |
7862 | */ | |
7863 | pmu->start_txn = perf_pmu_start_txn; | |
7864 | pmu->commit_txn = perf_pmu_commit_txn; | |
7865 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
7866 | } else { | |
fbbe0701 | 7867 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
7868 | pmu->commit_txn = perf_pmu_nop_int; |
7869 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 7870 | } |
5c92d124 | 7871 | } |
15dbf27c | 7872 | |
ad5133b7 PZ |
7873 | if (!pmu->pmu_enable) { |
7874 | pmu->pmu_enable = perf_pmu_nop_void; | |
7875 | pmu->pmu_disable = perf_pmu_nop_void; | |
7876 | } | |
7877 | ||
35edc2a5 PZ |
7878 | if (!pmu->event_idx) |
7879 | pmu->event_idx = perf_event_idx_default; | |
7880 | ||
b0a873eb | 7881 | list_add_rcu(&pmu->entry, &pmus); |
bed5b25a | 7882 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
7883 | ret = 0; |
7884 | unlock: | |
b0a873eb PZ |
7885 | mutex_unlock(&pmus_lock); |
7886 | ||
33696fc0 | 7887 | return ret; |
108b02cf | 7888 | |
abe43400 PZ |
7889 | free_dev: |
7890 | device_del(pmu->dev); | |
7891 | put_device(pmu->dev); | |
7892 | ||
2e80a82a PZ |
7893 | free_idr: |
7894 | if (pmu->type >= PERF_TYPE_MAX) | |
7895 | idr_remove(&pmu_idr, pmu->type); | |
7896 | ||
108b02cf PZ |
7897 | free_pdc: |
7898 | free_percpu(pmu->pmu_disable_count); | |
7899 | goto unlock; | |
f29ac756 | 7900 | } |
c464c76e | 7901 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 7902 | |
b0a873eb | 7903 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 7904 | { |
b0a873eb PZ |
7905 | mutex_lock(&pmus_lock); |
7906 | list_del_rcu(&pmu->entry); | |
7907 | mutex_unlock(&pmus_lock); | |
5c92d124 | 7908 | |
0475f9ea | 7909 | /* |
cde8e884 PZ |
7910 | * We dereference the pmu list under both SRCU and regular RCU, so |
7911 | * synchronize against both of those. | |
0475f9ea | 7912 | */ |
b0a873eb | 7913 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 7914 | synchronize_rcu(); |
d6d020e9 | 7915 | |
33696fc0 | 7916 | free_percpu(pmu->pmu_disable_count); |
2e80a82a PZ |
7917 | if (pmu->type >= PERF_TYPE_MAX) |
7918 | idr_remove(&pmu_idr, pmu->type); | |
abe43400 PZ |
7919 | device_del(pmu->dev); |
7920 | put_device(pmu->dev); | |
51676957 | 7921 | free_pmu_context(pmu); |
b0a873eb | 7922 | } |
c464c76e | 7923 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 7924 | |
cc34b98b MR |
7925 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
7926 | { | |
ccd41c86 | 7927 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
7928 | int ret; |
7929 | ||
7930 | if (!try_module_get(pmu->module)) | |
7931 | return -ENODEV; | |
ccd41c86 PZ |
7932 | |
7933 | if (event->group_leader != event) { | |
8b10c5e2 PZ |
7934 | /* |
7935 | * This ctx->mutex can nest when we're called through | |
7936 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
7937 | */ | |
7938 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
7939 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
7940 | BUG_ON(!ctx); |
7941 | } | |
7942 | ||
cc34b98b MR |
7943 | event->pmu = pmu; |
7944 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
7945 | |
7946 | if (ctx) | |
7947 | perf_event_ctx_unlock(event->group_leader, ctx); | |
7948 | ||
cc34b98b MR |
7949 | if (ret) |
7950 | module_put(pmu->module); | |
7951 | ||
7952 | return ret; | |
7953 | } | |
7954 | ||
18ab2cd3 | 7955 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb PZ |
7956 | { |
7957 | struct pmu *pmu = NULL; | |
7958 | int idx; | |
940c5b29 | 7959 | int ret; |
b0a873eb PZ |
7960 | |
7961 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a PZ |
7962 | |
7963 | rcu_read_lock(); | |
7964 | pmu = idr_find(&pmu_idr, event->attr.type); | |
7965 | rcu_read_unlock(); | |
940c5b29 | 7966 | if (pmu) { |
cc34b98b | 7967 | ret = perf_try_init_event(pmu, event); |
940c5b29 LM |
7968 | if (ret) |
7969 | pmu = ERR_PTR(ret); | |
2e80a82a | 7970 | goto unlock; |
940c5b29 | 7971 | } |
2e80a82a | 7972 | |
b0a873eb | 7973 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
cc34b98b | 7974 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 7975 | if (!ret) |
e5f4d339 | 7976 | goto unlock; |
76e1d904 | 7977 | |
b0a873eb PZ |
7978 | if (ret != -ENOENT) { |
7979 | pmu = ERR_PTR(ret); | |
e5f4d339 | 7980 | goto unlock; |
f344011c | 7981 | } |
5c92d124 | 7982 | } |
e5f4d339 PZ |
7983 | pmu = ERR_PTR(-ENOENT); |
7984 | unlock: | |
b0a873eb | 7985 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 7986 | |
4aeb0b42 | 7987 | return pmu; |
5c92d124 IM |
7988 | } |
7989 | ||
4beb31f3 FW |
7990 | static void account_event_cpu(struct perf_event *event, int cpu) |
7991 | { | |
7992 | if (event->parent) | |
7993 | return; | |
7994 | ||
4beb31f3 FW |
7995 | if (is_cgroup_event(event)) |
7996 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
7997 | } | |
7998 | ||
555e0c1e FW |
7999 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
8000 | static void account_freq_event_nohz(void) | |
8001 | { | |
8002 | #ifdef CONFIG_NO_HZ_FULL | |
8003 | /* Lock so we don't race with concurrent unaccount */ | |
8004 | spin_lock(&nr_freq_lock); | |
8005 | if (atomic_inc_return(&nr_freq_events) == 1) | |
8006 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
8007 | spin_unlock(&nr_freq_lock); | |
8008 | #endif | |
8009 | } | |
8010 | ||
8011 | static void account_freq_event(void) | |
8012 | { | |
8013 | if (tick_nohz_full_enabled()) | |
8014 | account_freq_event_nohz(); | |
8015 | else | |
8016 | atomic_inc(&nr_freq_events); | |
8017 | } | |
8018 | ||
8019 | ||
766d6c07 FW |
8020 | static void account_event(struct perf_event *event) |
8021 | { | |
25432ae9 PZ |
8022 | bool inc = false; |
8023 | ||
4beb31f3 FW |
8024 | if (event->parent) |
8025 | return; | |
8026 | ||
766d6c07 | 8027 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 8028 | inc = true; |
766d6c07 FW |
8029 | if (event->attr.mmap || event->attr.mmap_data) |
8030 | atomic_inc(&nr_mmap_events); | |
8031 | if (event->attr.comm) | |
8032 | atomic_inc(&nr_comm_events); | |
8033 | if (event->attr.task) | |
8034 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
8035 | if (event->attr.freq) |
8036 | account_freq_event(); | |
45ac1403 AH |
8037 | if (event->attr.context_switch) { |
8038 | atomic_inc(&nr_switch_events); | |
25432ae9 | 8039 | inc = true; |
45ac1403 | 8040 | } |
4beb31f3 | 8041 | if (has_branch_stack(event)) |
25432ae9 | 8042 | inc = true; |
4beb31f3 | 8043 | if (is_cgroup_event(event)) |
25432ae9 PZ |
8044 | inc = true; |
8045 | ||
9107c89e PZ |
8046 | if (inc) { |
8047 | if (atomic_inc_not_zero(&perf_sched_count)) | |
8048 | goto enabled; | |
8049 | ||
8050 | mutex_lock(&perf_sched_mutex); | |
8051 | if (!atomic_read(&perf_sched_count)) { | |
8052 | static_branch_enable(&perf_sched_events); | |
8053 | /* | |
8054 | * Guarantee that all CPUs observe they key change and | |
8055 | * call the perf scheduling hooks before proceeding to | |
8056 | * install events that need them. | |
8057 | */ | |
8058 | synchronize_sched(); | |
8059 | } | |
8060 | /* | |
8061 | * Now that we have waited for the sync_sched(), allow further | |
8062 | * increments to by-pass the mutex. | |
8063 | */ | |
8064 | atomic_inc(&perf_sched_count); | |
8065 | mutex_unlock(&perf_sched_mutex); | |
8066 | } | |
8067 | enabled: | |
4beb31f3 FW |
8068 | |
8069 | account_event_cpu(event, event->cpu); | |
766d6c07 FW |
8070 | } |
8071 | ||
0793a61d | 8072 | /* |
cdd6c482 | 8073 | * Allocate and initialize a event structure |
0793a61d | 8074 | */ |
cdd6c482 | 8075 | static struct perf_event * |
c3f00c70 | 8076 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
8077 | struct task_struct *task, |
8078 | struct perf_event *group_leader, | |
8079 | struct perf_event *parent_event, | |
4dc0da86 | 8080 | perf_overflow_handler_t overflow_handler, |
79dff51e | 8081 | void *context, int cgroup_fd) |
0793a61d | 8082 | { |
51b0fe39 | 8083 | struct pmu *pmu; |
cdd6c482 IM |
8084 | struct perf_event *event; |
8085 | struct hw_perf_event *hwc; | |
90983b16 | 8086 | long err = -EINVAL; |
0793a61d | 8087 | |
66832eb4 ON |
8088 | if ((unsigned)cpu >= nr_cpu_ids) { |
8089 | if (!task || cpu != -1) | |
8090 | return ERR_PTR(-EINVAL); | |
8091 | } | |
8092 | ||
c3f00c70 | 8093 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 8094 | if (!event) |
d5d2bc0d | 8095 | return ERR_PTR(-ENOMEM); |
0793a61d | 8096 | |
04289bb9 | 8097 | /* |
cdd6c482 | 8098 | * Single events are their own group leaders, with an |
04289bb9 IM |
8099 | * empty sibling list: |
8100 | */ | |
8101 | if (!group_leader) | |
cdd6c482 | 8102 | group_leader = event; |
04289bb9 | 8103 | |
cdd6c482 IM |
8104 | mutex_init(&event->child_mutex); |
8105 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 8106 | |
cdd6c482 IM |
8107 | INIT_LIST_HEAD(&event->group_entry); |
8108 | INIT_LIST_HEAD(&event->event_entry); | |
8109 | INIT_LIST_HEAD(&event->sibling_list); | |
10c6db11 | 8110 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 8111 | INIT_LIST_HEAD(&event->active_entry); |
f3ae75de SE |
8112 | INIT_HLIST_NODE(&event->hlist_entry); |
8113 | ||
10c6db11 | 8114 | |
cdd6c482 | 8115 | init_waitqueue_head(&event->waitq); |
e360adbe | 8116 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 8117 | |
cdd6c482 | 8118 | mutex_init(&event->mmap_mutex); |
7b732a75 | 8119 | |
a6fa941d | 8120 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
8121 | event->cpu = cpu; |
8122 | event->attr = *attr; | |
8123 | event->group_leader = group_leader; | |
8124 | event->pmu = NULL; | |
cdd6c482 | 8125 | event->oncpu = -1; |
a96bbc16 | 8126 | |
cdd6c482 | 8127 | event->parent = parent_event; |
b84fbc9f | 8128 | |
17cf22c3 | 8129 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 8130 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 8131 | |
cdd6c482 | 8132 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 8133 | |
d580ff86 PZ |
8134 | if (task) { |
8135 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 8136 | /* |
50f16a8b PZ |
8137 | * XXX pmu::event_init needs to know what task to account to |
8138 | * and we cannot use the ctx information because we need the | |
8139 | * pmu before we get a ctx. | |
d580ff86 | 8140 | */ |
50f16a8b | 8141 | event->hw.target = task; |
d580ff86 PZ |
8142 | } |
8143 | ||
34f43927 PZ |
8144 | event->clock = &local_clock; |
8145 | if (parent_event) | |
8146 | event->clock = parent_event->clock; | |
8147 | ||
4dc0da86 | 8148 | if (!overflow_handler && parent_event) { |
b326e956 | 8149 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 AK |
8150 | context = parent_event->overflow_handler_context; |
8151 | } | |
66832eb4 | 8152 | |
1879445d WN |
8153 | if (overflow_handler) { |
8154 | event->overflow_handler = overflow_handler; | |
8155 | event->overflow_handler_context = context; | |
8156 | } else { | |
8157 | event->overflow_handler = perf_event_output; | |
8158 | event->overflow_handler_context = NULL; | |
8159 | } | |
97eaf530 | 8160 | |
0231bb53 | 8161 | perf_event__state_init(event); |
a86ed508 | 8162 | |
4aeb0b42 | 8163 | pmu = NULL; |
b8e83514 | 8164 | |
cdd6c482 | 8165 | hwc = &event->hw; |
bd2b5b12 | 8166 | hwc->sample_period = attr->sample_period; |
0d48696f | 8167 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 8168 | hwc->sample_period = 1; |
eced1dfc | 8169 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 8170 | |
e7850595 | 8171 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 8172 | |
2023b359 | 8173 | /* |
cdd6c482 | 8174 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
2023b359 | 8175 | */ |
3dab77fb | 8176 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
90983b16 | 8177 | goto err_ns; |
a46a2300 YZ |
8178 | |
8179 | if (!has_branch_stack(event)) | |
8180 | event->attr.branch_sample_type = 0; | |
2023b359 | 8181 | |
79dff51e MF |
8182 | if (cgroup_fd != -1) { |
8183 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
8184 | if (err) | |
8185 | goto err_ns; | |
8186 | } | |
8187 | ||
b0a873eb | 8188 | pmu = perf_init_event(event); |
4aeb0b42 | 8189 | if (!pmu) |
90983b16 FW |
8190 | goto err_ns; |
8191 | else if (IS_ERR(pmu)) { | |
4aeb0b42 | 8192 | err = PTR_ERR(pmu); |
90983b16 | 8193 | goto err_ns; |
621a01ea | 8194 | } |
d5d2bc0d | 8195 | |
bed5b25a AS |
8196 | err = exclusive_event_init(event); |
8197 | if (err) | |
8198 | goto err_pmu; | |
8199 | ||
cdd6c482 | 8200 | if (!event->parent) { |
927c7a9e FW |
8201 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
8202 | err = get_callchain_buffers(); | |
90983b16 | 8203 | if (err) |
bed5b25a | 8204 | goto err_per_task; |
d010b332 | 8205 | } |
f344011c | 8206 | } |
9ee318a7 | 8207 | |
927a5570 AS |
8208 | /* symmetric to unaccount_event() in _free_event() */ |
8209 | account_event(event); | |
8210 | ||
cdd6c482 | 8211 | return event; |
90983b16 | 8212 | |
bed5b25a AS |
8213 | err_per_task: |
8214 | exclusive_event_destroy(event); | |
8215 | ||
90983b16 FW |
8216 | err_pmu: |
8217 | if (event->destroy) | |
8218 | event->destroy(event); | |
c464c76e | 8219 | module_put(pmu->module); |
90983b16 | 8220 | err_ns: |
79dff51e MF |
8221 | if (is_cgroup_event(event)) |
8222 | perf_detach_cgroup(event); | |
90983b16 FW |
8223 | if (event->ns) |
8224 | put_pid_ns(event->ns); | |
8225 | kfree(event); | |
8226 | ||
8227 | return ERR_PTR(err); | |
0793a61d TG |
8228 | } |
8229 | ||
cdd6c482 IM |
8230 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
8231 | struct perf_event_attr *attr) | |
974802ea | 8232 | { |
974802ea | 8233 | u32 size; |
cdf8073d | 8234 | int ret; |
974802ea PZ |
8235 | |
8236 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) | |
8237 | return -EFAULT; | |
8238 | ||
8239 | /* | |
8240 | * zero the full structure, so that a short copy will be nice. | |
8241 | */ | |
8242 | memset(attr, 0, sizeof(*attr)); | |
8243 | ||
8244 | ret = get_user(size, &uattr->size); | |
8245 | if (ret) | |
8246 | return ret; | |
8247 | ||
8248 | if (size > PAGE_SIZE) /* silly large */ | |
8249 | goto err_size; | |
8250 | ||
8251 | if (!size) /* abi compat */ | |
8252 | size = PERF_ATTR_SIZE_VER0; | |
8253 | ||
8254 | if (size < PERF_ATTR_SIZE_VER0) | |
8255 | goto err_size; | |
8256 | ||
8257 | /* | |
8258 | * If we're handed a bigger struct than we know of, | |
cdf8073d IS |
8259 | * ensure all the unknown bits are 0 - i.e. new |
8260 | * user-space does not rely on any kernel feature | |
8261 | * extensions we dont know about yet. | |
974802ea PZ |
8262 | */ |
8263 | if (size > sizeof(*attr)) { | |
cdf8073d IS |
8264 | unsigned char __user *addr; |
8265 | unsigned char __user *end; | |
8266 | unsigned char val; | |
974802ea | 8267 | |
cdf8073d IS |
8268 | addr = (void __user *)uattr + sizeof(*attr); |
8269 | end = (void __user *)uattr + size; | |
974802ea | 8270 | |
cdf8073d | 8271 | for (; addr < end; addr++) { |
974802ea PZ |
8272 | ret = get_user(val, addr); |
8273 | if (ret) | |
8274 | return ret; | |
8275 | if (val) | |
8276 | goto err_size; | |
8277 | } | |
b3e62e35 | 8278 | size = sizeof(*attr); |
974802ea PZ |
8279 | } |
8280 | ||
8281 | ret = copy_from_user(attr, uattr, size); | |
8282 | if (ret) | |
8283 | return -EFAULT; | |
8284 | ||
cd757645 | 8285 | if (attr->__reserved_1) |
974802ea PZ |
8286 | return -EINVAL; |
8287 | ||
8288 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
8289 | return -EINVAL; | |
8290 | ||
8291 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
8292 | return -EINVAL; | |
8293 | ||
bce38cd5 SE |
8294 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
8295 | u64 mask = attr->branch_sample_type; | |
8296 | ||
8297 | /* only using defined bits */ | |
8298 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
8299 | return -EINVAL; | |
8300 | ||
8301 | /* at least one branch bit must be set */ | |
8302 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
8303 | return -EINVAL; | |
8304 | ||
bce38cd5 SE |
8305 | /* propagate priv level, when not set for branch */ |
8306 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
8307 | ||
8308 | /* exclude_kernel checked on syscall entry */ | |
8309 | if (!attr->exclude_kernel) | |
8310 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
8311 | ||
8312 | if (!attr->exclude_user) | |
8313 | mask |= PERF_SAMPLE_BRANCH_USER; | |
8314 | ||
8315 | if (!attr->exclude_hv) | |
8316 | mask |= PERF_SAMPLE_BRANCH_HV; | |
8317 | /* | |
8318 | * adjust user setting (for HW filter setup) | |
8319 | */ | |
8320 | attr->branch_sample_type = mask; | |
8321 | } | |
e712209a SE |
8322 | /* privileged levels capture (kernel, hv): check permissions */ |
8323 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) | |
2b923c8f SE |
8324 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
8325 | return -EACCES; | |
bce38cd5 | 8326 | } |
4018994f | 8327 | |
c5ebcedb | 8328 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 8329 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
8330 | if (ret) |
8331 | return ret; | |
8332 | } | |
8333 | ||
8334 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
8335 | if (!arch_perf_have_user_stack_dump()) | |
8336 | return -ENOSYS; | |
8337 | ||
8338 | /* | |
8339 | * We have __u32 type for the size, but so far | |
8340 | * we can only use __u16 as maximum due to the | |
8341 | * __u16 sample size limit. | |
8342 | */ | |
8343 | if (attr->sample_stack_user >= USHRT_MAX) | |
8344 | ret = -EINVAL; | |
8345 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) | |
8346 | ret = -EINVAL; | |
8347 | } | |
4018994f | 8348 | |
60e2364e SE |
8349 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
8350 | ret = perf_reg_validate(attr->sample_regs_intr); | |
974802ea PZ |
8351 | out: |
8352 | return ret; | |
8353 | ||
8354 | err_size: | |
8355 | put_user(sizeof(*attr), &uattr->size); | |
8356 | ret = -E2BIG; | |
8357 | goto out; | |
8358 | } | |
8359 | ||
ac9721f3 PZ |
8360 | static int |
8361 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 8362 | { |
b69cf536 | 8363 | struct ring_buffer *rb = NULL; |
a4be7c27 PZ |
8364 | int ret = -EINVAL; |
8365 | ||
ac9721f3 | 8366 | if (!output_event) |
a4be7c27 PZ |
8367 | goto set; |
8368 | ||
ac9721f3 PZ |
8369 | /* don't allow circular references */ |
8370 | if (event == output_event) | |
a4be7c27 PZ |
8371 | goto out; |
8372 | ||
0f139300 PZ |
8373 | /* |
8374 | * Don't allow cross-cpu buffers | |
8375 | */ | |
8376 | if (output_event->cpu != event->cpu) | |
8377 | goto out; | |
8378 | ||
8379 | /* | |
76369139 | 8380 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
8381 | */ |
8382 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
8383 | goto out; | |
8384 | ||
34f43927 PZ |
8385 | /* |
8386 | * Mixing clocks in the same buffer is trouble you don't need. | |
8387 | */ | |
8388 | if (output_event->clock != event->clock) | |
8389 | goto out; | |
8390 | ||
45bfb2e5 PZ |
8391 | /* |
8392 | * If both events generate aux data, they must be on the same PMU | |
8393 | */ | |
8394 | if (has_aux(event) && has_aux(output_event) && | |
8395 | event->pmu != output_event->pmu) | |
8396 | goto out; | |
8397 | ||
a4be7c27 | 8398 | set: |
cdd6c482 | 8399 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
8400 | /* Can't redirect output if we've got an active mmap() */ |
8401 | if (atomic_read(&event->mmap_count)) | |
8402 | goto unlock; | |
a4be7c27 | 8403 | |
ac9721f3 | 8404 | if (output_event) { |
76369139 FW |
8405 | /* get the rb we want to redirect to */ |
8406 | rb = ring_buffer_get(output_event); | |
8407 | if (!rb) | |
ac9721f3 | 8408 | goto unlock; |
a4be7c27 PZ |
8409 | } |
8410 | ||
b69cf536 | 8411 | ring_buffer_attach(event, rb); |
9bb5d40c | 8412 | |
a4be7c27 | 8413 | ret = 0; |
ac9721f3 PZ |
8414 | unlock: |
8415 | mutex_unlock(&event->mmap_mutex); | |
8416 | ||
a4be7c27 | 8417 | out: |
a4be7c27 PZ |
8418 | return ret; |
8419 | } | |
8420 | ||
f63a8daa PZ |
8421 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
8422 | { | |
8423 | if (b < a) | |
8424 | swap(a, b); | |
8425 | ||
8426 | mutex_lock(a); | |
8427 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
8428 | } | |
8429 | ||
34f43927 PZ |
8430 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
8431 | { | |
8432 | bool nmi_safe = false; | |
8433 | ||
8434 | switch (clk_id) { | |
8435 | case CLOCK_MONOTONIC: | |
8436 | event->clock = &ktime_get_mono_fast_ns; | |
8437 | nmi_safe = true; | |
8438 | break; | |
8439 | ||
8440 | case CLOCK_MONOTONIC_RAW: | |
8441 | event->clock = &ktime_get_raw_fast_ns; | |
8442 | nmi_safe = true; | |
8443 | break; | |
8444 | ||
8445 | case CLOCK_REALTIME: | |
8446 | event->clock = &ktime_get_real_ns; | |
8447 | break; | |
8448 | ||
8449 | case CLOCK_BOOTTIME: | |
8450 | event->clock = &ktime_get_boot_ns; | |
8451 | break; | |
8452 | ||
8453 | case CLOCK_TAI: | |
8454 | event->clock = &ktime_get_tai_ns; | |
8455 | break; | |
8456 | ||
8457 | default: | |
8458 | return -EINVAL; | |
8459 | } | |
8460 | ||
8461 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
8462 | return -EINVAL; | |
8463 | ||
8464 | return 0; | |
8465 | } | |
8466 | ||
0793a61d | 8467 | /** |
cdd6c482 | 8468 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 8469 | * |
cdd6c482 | 8470 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 8471 | * @pid: target pid |
9f66a381 | 8472 | * @cpu: target cpu |
cdd6c482 | 8473 | * @group_fd: group leader event fd |
0793a61d | 8474 | */ |
cdd6c482 IM |
8475 | SYSCALL_DEFINE5(perf_event_open, |
8476 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 8477 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 8478 | { |
b04243ef PZ |
8479 | struct perf_event *group_leader = NULL, *output_event = NULL; |
8480 | struct perf_event *event, *sibling; | |
cdd6c482 | 8481 | struct perf_event_attr attr; |
f63a8daa | 8482 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 8483 | struct file *event_file = NULL; |
2903ff01 | 8484 | struct fd group = {NULL, 0}; |
38a81da2 | 8485 | struct task_struct *task = NULL; |
89a1e187 | 8486 | struct pmu *pmu; |
ea635c64 | 8487 | int event_fd; |
b04243ef | 8488 | int move_group = 0; |
dc86cabe | 8489 | int err; |
a21b0b35 | 8490 | int f_flags = O_RDWR; |
79dff51e | 8491 | int cgroup_fd = -1; |
0793a61d | 8492 | |
2743a5b0 | 8493 | /* for future expandability... */ |
e5d1367f | 8494 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
8495 | return -EINVAL; |
8496 | ||
dc86cabe IM |
8497 | err = perf_copy_attr(attr_uptr, &attr); |
8498 | if (err) | |
8499 | return err; | |
eab656ae | 8500 | |
0764771d PZ |
8501 | if (!attr.exclude_kernel) { |
8502 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) | |
8503 | return -EACCES; | |
8504 | } | |
8505 | ||
df58ab24 | 8506 | if (attr.freq) { |
cdd6c482 | 8507 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 8508 | return -EINVAL; |
0819b2e3 PZ |
8509 | } else { |
8510 | if (attr.sample_period & (1ULL << 63)) | |
8511 | return -EINVAL; | |
df58ab24 PZ |
8512 | } |
8513 | ||
e5d1367f SE |
8514 | /* |
8515 | * In cgroup mode, the pid argument is used to pass the fd | |
8516 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
8517 | * designates the cpu on which to monitor threads from that | |
8518 | * cgroup. | |
8519 | */ | |
8520 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
8521 | return -EINVAL; | |
8522 | ||
a21b0b35 YD |
8523 | if (flags & PERF_FLAG_FD_CLOEXEC) |
8524 | f_flags |= O_CLOEXEC; | |
8525 | ||
8526 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
8527 | if (event_fd < 0) |
8528 | return event_fd; | |
8529 | ||
ac9721f3 | 8530 | if (group_fd != -1) { |
2903ff01 AV |
8531 | err = perf_fget_light(group_fd, &group); |
8532 | if (err) | |
d14b12d7 | 8533 | goto err_fd; |
2903ff01 | 8534 | group_leader = group.file->private_data; |
ac9721f3 PZ |
8535 | if (flags & PERF_FLAG_FD_OUTPUT) |
8536 | output_event = group_leader; | |
8537 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
8538 | group_leader = NULL; | |
8539 | } | |
8540 | ||
e5d1367f | 8541 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
8542 | task = find_lively_task_by_vpid(pid); |
8543 | if (IS_ERR(task)) { | |
8544 | err = PTR_ERR(task); | |
8545 | goto err_group_fd; | |
8546 | } | |
8547 | } | |
8548 | ||
1f4ee503 PZ |
8549 | if (task && group_leader && |
8550 | group_leader->attr.inherit != attr.inherit) { | |
8551 | err = -EINVAL; | |
8552 | goto err_task; | |
8553 | } | |
8554 | ||
fbfc623f YZ |
8555 | get_online_cpus(); |
8556 | ||
79dff51e MF |
8557 | if (flags & PERF_FLAG_PID_CGROUP) |
8558 | cgroup_fd = pid; | |
8559 | ||
4dc0da86 | 8560 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 8561 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
8562 | if (IS_ERR(event)) { |
8563 | err = PTR_ERR(event); | |
1f4ee503 | 8564 | goto err_cpus; |
d14b12d7 SE |
8565 | } |
8566 | ||
53b25335 VW |
8567 | if (is_sampling_event(event)) { |
8568 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
8569 | err = -ENOTSUPP; | |
8570 | goto err_alloc; | |
8571 | } | |
8572 | } | |
8573 | ||
89a1e187 PZ |
8574 | /* |
8575 | * Special case software events and allow them to be part of | |
8576 | * any hardware group. | |
8577 | */ | |
8578 | pmu = event->pmu; | |
b04243ef | 8579 | |
34f43927 PZ |
8580 | if (attr.use_clockid) { |
8581 | err = perf_event_set_clock(event, attr.clockid); | |
8582 | if (err) | |
8583 | goto err_alloc; | |
8584 | } | |
8585 | ||
b04243ef PZ |
8586 | if (group_leader && |
8587 | (is_software_event(event) != is_software_event(group_leader))) { | |
8588 | if (is_software_event(event)) { | |
8589 | /* | |
8590 | * If event and group_leader are not both a software | |
8591 | * event, and event is, then group leader is not. | |
8592 | * | |
8593 | * Allow the addition of software events to !software | |
8594 | * groups, this is safe because software events never | |
8595 | * fail to schedule. | |
8596 | */ | |
8597 | pmu = group_leader->pmu; | |
8598 | } else if (is_software_event(group_leader) && | |
8599 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { | |
8600 | /* | |
8601 | * In case the group is a pure software group, and we | |
8602 | * try to add a hardware event, move the whole group to | |
8603 | * the hardware context. | |
8604 | */ | |
8605 | move_group = 1; | |
8606 | } | |
8607 | } | |
89a1e187 PZ |
8608 | |
8609 | /* | |
8610 | * Get the target context (task or percpu): | |
8611 | */ | |
4af57ef2 | 8612 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
8613 | if (IS_ERR(ctx)) { |
8614 | err = PTR_ERR(ctx); | |
c6be5a5c | 8615 | goto err_alloc; |
89a1e187 PZ |
8616 | } |
8617 | ||
bed5b25a AS |
8618 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
8619 | err = -EBUSY; | |
8620 | goto err_context; | |
8621 | } | |
8622 | ||
fd1edb3a PZ |
8623 | if (task) { |
8624 | put_task_struct(task); | |
8625 | task = NULL; | |
8626 | } | |
8627 | ||
ccff286d | 8628 | /* |
cdd6c482 | 8629 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 8630 | */ |
ac9721f3 | 8631 | if (group_leader) { |
dc86cabe | 8632 | err = -EINVAL; |
04289bb9 | 8633 | |
04289bb9 | 8634 | /* |
ccff286d IM |
8635 | * Do not allow a recursive hierarchy (this new sibling |
8636 | * becoming part of another group-sibling): | |
8637 | */ | |
8638 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 8639 | goto err_context; |
34f43927 PZ |
8640 | |
8641 | /* All events in a group should have the same clock */ | |
8642 | if (group_leader->clock != event->clock) | |
8643 | goto err_context; | |
8644 | ||
ccff286d IM |
8645 | /* |
8646 | * Do not allow to attach to a group in a different | |
8647 | * task or CPU context: | |
04289bb9 | 8648 | */ |
b04243ef | 8649 | if (move_group) { |
c3c87e77 PZ |
8650 | /* |
8651 | * Make sure we're both on the same task, or both | |
8652 | * per-cpu events. | |
8653 | */ | |
8654 | if (group_leader->ctx->task != ctx->task) | |
8655 | goto err_context; | |
8656 | ||
8657 | /* | |
8658 | * Make sure we're both events for the same CPU; | |
8659 | * grouping events for different CPUs is broken; since | |
8660 | * you can never concurrently schedule them anyhow. | |
8661 | */ | |
8662 | if (group_leader->cpu != event->cpu) | |
b04243ef PZ |
8663 | goto err_context; |
8664 | } else { | |
8665 | if (group_leader->ctx != ctx) | |
8666 | goto err_context; | |
8667 | } | |
8668 | ||
3b6f9e5c PM |
8669 | /* |
8670 | * Only a group leader can be exclusive or pinned | |
8671 | */ | |
0d48696f | 8672 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 8673 | goto err_context; |
ac9721f3 PZ |
8674 | } |
8675 | ||
8676 | if (output_event) { | |
8677 | err = perf_event_set_output(event, output_event); | |
8678 | if (err) | |
c3f00c70 | 8679 | goto err_context; |
ac9721f3 | 8680 | } |
0793a61d | 8681 | |
a21b0b35 YD |
8682 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
8683 | f_flags); | |
ea635c64 AV |
8684 | if (IS_ERR(event_file)) { |
8685 | err = PTR_ERR(event_file); | |
201c2f85 | 8686 | event_file = NULL; |
c3f00c70 | 8687 | goto err_context; |
ea635c64 | 8688 | } |
9b51f66d | 8689 | |
b04243ef | 8690 | if (move_group) { |
f63a8daa | 8691 | gctx = group_leader->ctx; |
f55fc2a5 | 8692 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
84c4e620 PZ |
8693 | if (gctx->task == TASK_TOMBSTONE) { |
8694 | err = -ESRCH; | |
8695 | goto err_locked; | |
8696 | } | |
f55fc2a5 PZ |
8697 | } else { |
8698 | mutex_lock(&ctx->mutex); | |
8699 | } | |
8700 | ||
84c4e620 PZ |
8701 | if (ctx->task == TASK_TOMBSTONE) { |
8702 | err = -ESRCH; | |
8703 | goto err_locked; | |
8704 | } | |
8705 | ||
a723968c PZ |
8706 | if (!perf_event_validate_size(event)) { |
8707 | err = -E2BIG; | |
8708 | goto err_locked; | |
8709 | } | |
8710 | ||
f55fc2a5 PZ |
8711 | /* |
8712 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
8713 | * because we need to serialize with concurrent event creation. | |
8714 | */ | |
8715 | if (!exclusive_event_installable(event, ctx)) { | |
8716 | /* exclusive and group stuff are assumed mutually exclusive */ | |
8717 | WARN_ON_ONCE(move_group); | |
f63a8daa | 8718 | |
f55fc2a5 PZ |
8719 | err = -EBUSY; |
8720 | goto err_locked; | |
8721 | } | |
f63a8daa | 8722 | |
f55fc2a5 PZ |
8723 | WARN_ON_ONCE(ctx->parent_ctx); |
8724 | ||
8725 | if (move_group) { | |
f63a8daa PZ |
8726 | /* |
8727 | * See perf_event_ctx_lock() for comments on the details | |
8728 | * of swizzling perf_event::ctx. | |
8729 | */ | |
45a0e07a | 8730 | perf_remove_from_context(group_leader, 0); |
0231bb53 | 8731 | |
b04243ef PZ |
8732 | list_for_each_entry(sibling, &group_leader->sibling_list, |
8733 | group_entry) { | |
45a0e07a | 8734 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
8735 | put_ctx(gctx); |
8736 | } | |
b04243ef | 8737 | |
f63a8daa PZ |
8738 | /* |
8739 | * Wait for everybody to stop referencing the events through | |
8740 | * the old lists, before installing it on new lists. | |
8741 | */ | |
0cda4c02 | 8742 | synchronize_rcu(); |
f63a8daa | 8743 | |
8f95b435 PZI |
8744 | /* |
8745 | * Install the group siblings before the group leader. | |
8746 | * | |
8747 | * Because a group leader will try and install the entire group | |
8748 | * (through the sibling list, which is still in-tact), we can | |
8749 | * end up with siblings installed in the wrong context. | |
8750 | * | |
8751 | * By installing siblings first we NO-OP because they're not | |
8752 | * reachable through the group lists. | |
8753 | */ | |
b04243ef PZ |
8754 | list_for_each_entry(sibling, &group_leader->sibling_list, |
8755 | group_entry) { | |
8f95b435 | 8756 | perf_event__state_init(sibling); |
9fc81d87 | 8757 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
8758 | get_ctx(ctx); |
8759 | } | |
8f95b435 PZI |
8760 | |
8761 | /* | |
8762 | * Removing from the context ends up with disabled | |
8763 | * event. What we want here is event in the initial | |
8764 | * startup state, ready to be add into new context. | |
8765 | */ | |
8766 | perf_event__state_init(group_leader); | |
8767 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
8768 | get_ctx(ctx); | |
b04243ef | 8769 | |
f55fc2a5 PZ |
8770 | /* |
8771 | * Now that all events are installed in @ctx, nothing | |
8772 | * references @gctx anymore, so drop the last reference we have | |
8773 | * on it. | |
8774 | */ | |
8775 | put_ctx(gctx); | |
bed5b25a AS |
8776 | } |
8777 | ||
f73e22ab PZ |
8778 | /* |
8779 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
8780 | * that we're serialized against further additions and before | |
8781 | * perf_install_in_context() which is the point the event is active and | |
8782 | * can use these values. | |
8783 | */ | |
8784 | perf_event__header_size(event); | |
8785 | perf_event__id_header_size(event); | |
8786 | ||
78cd2c74 PZ |
8787 | event->owner = current; |
8788 | ||
e2d37cd2 | 8789 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 8790 | perf_unpin_context(ctx); |
f63a8daa | 8791 | |
f55fc2a5 | 8792 | if (move_group) |
f63a8daa | 8793 | mutex_unlock(&gctx->mutex); |
d859e29f | 8794 | mutex_unlock(&ctx->mutex); |
9b51f66d | 8795 | |
fbfc623f YZ |
8796 | put_online_cpus(); |
8797 | ||
cdd6c482 IM |
8798 | mutex_lock(¤t->perf_event_mutex); |
8799 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
8800 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 8801 | |
8a49542c PZ |
8802 | /* |
8803 | * Drop the reference on the group_event after placing the | |
8804 | * new event on the sibling_list. This ensures destruction | |
8805 | * of the group leader will find the pointer to itself in | |
8806 | * perf_group_detach(). | |
8807 | */ | |
2903ff01 | 8808 | fdput(group); |
ea635c64 AV |
8809 | fd_install(event_fd, event_file); |
8810 | return event_fd; | |
0793a61d | 8811 | |
f55fc2a5 PZ |
8812 | err_locked: |
8813 | if (move_group) | |
8814 | mutex_unlock(&gctx->mutex); | |
8815 | mutex_unlock(&ctx->mutex); | |
8816 | /* err_file: */ | |
8817 | fput(event_file); | |
c3f00c70 | 8818 | err_context: |
fe4b04fa | 8819 | perf_unpin_context(ctx); |
ea635c64 | 8820 | put_ctx(ctx); |
c6be5a5c | 8821 | err_alloc: |
13005627 PZ |
8822 | /* |
8823 | * If event_file is set, the fput() above will have called ->release() | |
8824 | * and that will take care of freeing the event. | |
8825 | */ | |
8826 | if (!event_file) | |
8827 | free_event(event); | |
1f4ee503 | 8828 | err_cpus: |
fbfc623f | 8829 | put_online_cpus(); |
1f4ee503 | 8830 | err_task: |
e7d0bc04 PZ |
8831 | if (task) |
8832 | put_task_struct(task); | |
89a1e187 | 8833 | err_group_fd: |
2903ff01 | 8834 | fdput(group); |
ea635c64 AV |
8835 | err_fd: |
8836 | put_unused_fd(event_fd); | |
dc86cabe | 8837 | return err; |
0793a61d TG |
8838 | } |
8839 | ||
fb0459d7 AV |
8840 | /** |
8841 | * perf_event_create_kernel_counter | |
8842 | * | |
8843 | * @attr: attributes of the counter to create | |
8844 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 8845 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
8846 | */ |
8847 | struct perf_event * | |
8848 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 8849 | struct task_struct *task, |
4dc0da86 AK |
8850 | perf_overflow_handler_t overflow_handler, |
8851 | void *context) | |
fb0459d7 | 8852 | { |
fb0459d7 | 8853 | struct perf_event_context *ctx; |
c3f00c70 | 8854 | struct perf_event *event; |
fb0459d7 | 8855 | int err; |
d859e29f | 8856 | |
fb0459d7 AV |
8857 | /* |
8858 | * Get the target context (task or percpu): | |
8859 | */ | |
d859e29f | 8860 | |
4dc0da86 | 8861 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 8862 | overflow_handler, context, -1); |
c3f00c70 PZ |
8863 | if (IS_ERR(event)) { |
8864 | err = PTR_ERR(event); | |
8865 | goto err; | |
8866 | } | |
d859e29f | 8867 | |
f8697762 | 8868 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 8869 | event->owner = TASK_TOMBSTONE; |
f8697762 | 8870 | |
4af57ef2 | 8871 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
8872 | if (IS_ERR(ctx)) { |
8873 | err = PTR_ERR(ctx); | |
c3f00c70 | 8874 | goto err_free; |
d859e29f | 8875 | } |
fb0459d7 | 8876 | |
fb0459d7 AV |
8877 | WARN_ON_ONCE(ctx->parent_ctx); |
8878 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
8879 | if (ctx->task == TASK_TOMBSTONE) { |
8880 | err = -ESRCH; | |
8881 | goto err_unlock; | |
8882 | } | |
8883 | ||
bed5b25a | 8884 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 8885 | err = -EBUSY; |
84c4e620 | 8886 | goto err_unlock; |
bed5b25a AS |
8887 | } |
8888 | ||
fb0459d7 | 8889 | perf_install_in_context(ctx, event, cpu); |
fe4b04fa | 8890 | perf_unpin_context(ctx); |
fb0459d7 AV |
8891 | mutex_unlock(&ctx->mutex); |
8892 | ||
fb0459d7 AV |
8893 | return event; |
8894 | ||
84c4e620 PZ |
8895 | err_unlock: |
8896 | mutex_unlock(&ctx->mutex); | |
8897 | perf_unpin_context(ctx); | |
8898 | put_ctx(ctx); | |
c3f00c70 PZ |
8899 | err_free: |
8900 | free_event(event); | |
8901 | err: | |
c6567f64 | 8902 | return ERR_PTR(err); |
9b51f66d | 8903 | } |
fb0459d7 | 8904 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 8905 | |
0cda4c02 YZ |
8906 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
8907 | { | |
8908 | struct perf_event_context *src_ctx; | |
8909 | struct perf_event_context *dst_ctx; | |
8910 | struct perf_event *event, *tmp; | |
8911 | LIST_HEAD(events); | |
8912 | ||
8913 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
8914 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
8915 | ||
f63a8daa PZ |
8916 | /* |
8917 | * See perf_event_ctx_lock() for comments on the details | |
8918 | * of swizzling perf_event::ctx. | |
8919 | */ | |
8920 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
8921 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
8922 | event_entry) { | |
45a0e07a | 8923 | perf_remove_from_context(event, 0); |
9a545de0 | 8924 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 8925 | put_ctx(src_ctx); |
9886167d | 8926 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 8927 | } |
0cda4c02 | 8928 | |
8f95b435 PZI |
8929 | /* |
8930 | * Wait for the events to quiesce before re-instating them. | |
8931 | */ | |
0cda4c02 YZ |
8932 | synchronize_rcu(); |
8933 | ||
8f95b435 PZI |
8934 | /* |
8935 | * Re-instate events in 2 passes. | |
8936 | * | |
8937 | * Skip over group leaders and only install siblings on this first | |
8938 | * pass, siblings will not get enabled without a leader, however a | |
8939 | * leader will enable its siblings, even if those are still on the old | |
8940 | * context. | |
8941 | */ | |
8942 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
8943 | if (event->group_leader == event) | |
8944 | continue; | |
8945 | ||
8946 | list_del(&event->migrate_entry); | |
8947 | if (event->state >= PERF_EVENT_STATE_OFF) | |
8948 | event->state = PERF_EVENT_STATE_INACTIVE; | |
8949 | account_event_cpu(event, dst_cpu); | |
8950 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
8951 | get_ctx(dst_ctx); | |
8952 | } | |
8953 | ||
8954 | /* | |
8955 | * Once all the siblings are setup properly, install the group leaders | |
8956 | * to make it go. | |
8957 | */ | |
9886167d PZ |
8958 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
8959 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
8960 | if (event->state >= PERF_EVENT_STATE_OFF) |
8961 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 8962 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
8963 | perf_install_in_context(dst_ctx, event, dst_cpu); |
8964 | get_ctx(dst_ctx); | |
8965 | } | |
8966 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 8967 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
8968 | } |
8969 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
8970 | ||
cdd6c482 | 8971 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 8972 | struct task_struct *child) |
d859e29f | 8973 | { |
cdd6c482 | 8974 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 8975 | u64 child_val; |
d859e29f | 8976 | |
cdd6c482 IM |
8977 | if (child_event->attr.inherit_stat) |
8978 | perf_event_read_event(child_event, child); | |
38b200d6 | 8979 | |
b5e58793 | 8980 | child_val = perf_event_count(child_event); |
d859e29f PM |
8981 | |
8982 | /* | |
8983 | * Add back the child's count to the parent's count: | |
8984 | */ | |
a6e6dea6 | 8985 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
8986 | atomic64_add(child_event->total_time_enabled, |
8987 | &parent_event->child_total_time_enabled); | |
8988 | atomic64_add(child_event->total_time_running, | |
8989 | &parent_event->child_total_time_running); | |
d859e29f PM |
8990 | } |
8991 | ||
9b51f66d | 8992 | static void |
8ba289b8 PZ |
8993 | perf_event_exit_event(struct perf_event *child_event, |
8994 | struct perf_event_context *child_ctx, | |
8995 | struct task_struct *child) | |
9b51f66d | 8996 | { |
8ba289b8 PZ |
8997 | struct perf_event *parent_event = child_event->parent; |
8998 | ||
1903d50c PZ |
8999 | /* |
9000 | * Do not destroy the 'original' grouping; because of the context | |
9001 | * switch optimization the original events could've ended up in a | |
9002 | * random child task. | |
9003 | * | |
9004 | * If we were to destroy the original group, all group related | |
9005 | * operations would cease to function properly after this random | |
9006 | * child dies. | |
9007 | * | |
9008 | * Do destroy all inherited groups, we don't care about those | |
9009 | * and being thorough is better. | |
9010 | */ | |
32132a3d PZ |
9011 | raw_spin_lock_irq(&child_ctx->lock); |
9012 | WARN_ON_ONCE(child_ctx->is_active); | |
9013 | ||
8ba289b8 | 9014 | if (parent_event) |
32132a3d PZ |
9015 | perf_group_detach(child_event); |
9016 | list_del_event(child_event, child_ctx); | |
a69b0ca4 | 9017 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
32132a3d | 9018 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 9019 | |
9b51f66d | 9020 | /* |
8ba289b8 | 9021 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 9022 | */ |
8ba289b8 | 9023 | if (!parent_event) { |
179033b3 | 9024 | perf_event_wakeup(child_event); |
8ba289b8 | 9025 | return; |
4bcf349a | 9026 | } |
8ba289b8 PZ |
9027 | /* |
9028 | * Child events can be cleaned up. | |
9029 | */ | |
9030 | ||
9031 | sync_child_event(child_event, child); | |
9032 | ||
9033 | /* | |
9034 | * Remove this event from the parent's list | |
9035 | */ | |
9036 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
9037 | mutex_lock(&parent_event->child_mutex); | |
9038 | list_del_init(&child_event->child_list); | |
9039 | mutex_unlock(&parent_event->child_mutex); | |
9040 | ||
9041 | /* | |
9042 | * Kick perf_poll() for is_event_hup(). | |
9043 | */ | |
9044 | perf_event_wakeup(parent_event); | |
9045 | free_event(child_event); | |
9046 | put_event(parent_event); | |
9b51f66d IM |
9047 | } |
9048 | ||
8dc85d54 | 9049 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 9050 | { |
211de6eb | 9051 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 9052 | struct perf_event *child_event, *next; |
63b6da39 PZ |
9053 | |
9054 | WARN_ON_ONCE(child != current); | |
9b51f66d | 9055 | |
6a3351b6 | 9056 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 9057 | if (!child_ctx) |
9b51f66d IM |
9058 | return; |
9059 | ||
ad3a37de | 9060 | /* |
6a3351b6 PZ |
9061 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
9062 | * ctx::mutex over the entire thing. This serializes against almost | |
9063 | * everything that wants to access the ctx. | |
9064 | * | |
9065 | * The exception is sys_perf_event_open() / | |
9066 | * perf_event_create_kernel_count() which does find_get_context() | |
9067 | * without ctx::mutex (it cannot because of the move_group double mutex | |
9068 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 9069 | */ |
6a3351b6 | 9070 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
9071 | |
9072 | /* | |
6a3351b6 PZ |
9073 | * In a single ctx::lock section, de-schedule the events and detach the |
9074 | * context from the task such that we cannot ever get it scheduled back | |
9075 | * in. | |
c93f7669 | 9076 | */ |
6a3351b6 | 9077 | raw_spin_lock_irq(&child_ctx->lock); |
63b6da39 | 9078 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
4a1c0f26 | 9079 | |
71a851b4 | 9080 | /* |
63b6da39 PZ |
9081 | * Now that the context is inactive, destroy the task <-> ctx relation |
9082 | * and mark the context dead. | |
71a851b4 | 9083 | */ |
63b6da39 PZ |
9084 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
9085 | put_ctx(child_ctx); /* cannot be last */ | |
9086 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
9087 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 9088 | |
211de6eb | 9089 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 9090 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 9091 | |
211de6eb PZ |
9092 | if (clone_ctx) |
9093 | put_ctx(clone_ctx); | |
4a1c0f26 | 9094 | |
9f498cc5 | 9095 | /* |
cdd6c482 IM |
9096 | * Report the task dead after unscheduling the events so that we |
9097 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
9098 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 9099 | */ |
cdd6c482 | 9100 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 9101 | |
ebf905fc | 9102 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 9103 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 9104 | |
a63eaf34 PM |
9105 | mutex_unlock(&child_ctx->mutex); |
9106 | ||
9107 | put_ctx(child_ctx); | |
9b51f66d IM |
9108 | } |
9109 | ||
8dc85d54 PZ |
9110 | /* |
9111 | * When a child task exits, feed back event values to parent events. | |
9112 | */ | |
9113 | void perf_event_exit_task(struct task_struct *child) | |
9114 | { | |
8882135b | 9115 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
9116 | int ctxn; |
9117 | ||
8882135b PZ |
9118 | mutex_lock(&child->perf_event_mutex); |
9119 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
9120 | owner_entry) { | |
9121 | list_del_init(&event->owner_entry); | |
9122 | ||
9123 | /* | |
9124 | * Ensure the list deletion is visible before we clear | |
9125 | * the owner, closes a race against perf_release() where | |
9126 | * we need to serialize on the owner->perf_event_mutex. | |
9127 | */ | |
f47c02c0 | 9128 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
9129 | } |
9130 | mutex_unlock(&child->perf_event_mutex); | |
9131 | ||
8dc85d54 PZ |
9132 | for_each_task_context_nr(ctxn) |
9133 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
9134 | |
9135 | /* | |
9136 | * The perf_event_exit_task_context calls perf_event_task | |
9137 | * with child's task_ctx, which generates EXIT events for | |
9138 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
9139 | * At this point we need to send EXIT events to cpu contexts. | |
9140 | */ | |
9141 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
9142 | } |
9143 | ||
889ff015 FW |
9144 | static void perf_free_event(struct perf_event *event, |
9145 | struct perf_event_context *ctx) | |
9146 | { | |
9147 | struct perf_event *parent = event->parent; | |
9148 | ||
9149 | if (WARN_ON_ONCE(!parent)) | |
9150 | return; | |
9151 | ||
9152 | mutex_lock(&parent->child_mutex); | |
9153 | list_del_init(&event->child_list); | |
9154 | mutex_unlock(&parent->child_mutex); | |
9155 | ||
a6fa941d | 9156 | put_event(parent); |
889ff015 | 9157 | |
652884fe | 9158 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 9159 | perf_group_detach(event); |
889ff015 | 9160 | list_del_event(event, ctx); |
652884fe | 9161 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
9162 | free_event(event); |
9163 | } | |
9164 | ||
bbbee908 | 9165 | /* |
652884fe | 9166 | * Free an unexposed, unused context as created by inheritance by |
8dc85d54 | 9167 | * perf_event_init_task below, used by fork() in case of fail. |
652884fe PZ |
9168 | * |
9169 | * Not all locks are strictly required, but take them anyway to be nice and | |
9170 | * help out with the lockdep assertions. | |
bbbee908 | 9171 | */ |
cdd6c482 | 9172 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 9173 | { |
8dc85d54 | 9174 | struct perf_event_context *ctx; |
cdd6c482 | 9175 | struct perf_event *event, *tmp; |
8dc85d54 | 9176 | int ctxn; |
bbbee908 | 9177 | |
8dc85d54 PZ |
9178 | for_each_task_context_nr(ctxn) { |
9179 | ctx = task->perf_event_ctxp[ctxn]; | |
9180 | if (!ctx) | |
9181 | continue; | |
bbbee908 | 9182 | |
8dc85d54 | 9183 | mutex_lock(&ctx->mutex); |
bbbee908 | 9184 | again: |
8dc85d54 PZ |
9185 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
9186 | group_entry) | |
9187 | perf_free_event(event, ctx); | |
bbbee908 | 9188 | |
8dc85d54 PZ |
9189 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
9190 | group_entry) | |
9191 | perf_free_event(event, ctx); | |
bbbee908 | 9192 | |
8dc85d54 PZ |
9193 | if (!list_empty(&ctx->pinned_groups) || |
9194 | !list_empty(&ctx->flexible_groups)) | |
9195 | goto again; | |
bbbee908 | 9196 | |
8dc85d54 | 9197 | mutex_unlock(&ctx->mutex); |
bbbee908 | 9198 | |
8dc85d54 PZ |
9199 | put_ctx(ctx); |
9200 | } | |
889ff015 FW |
9201 | } |
9202 | ||
4e231c79 PZ |
9203 | void perf_event_delayed_put(struct task_struct *task) |
9204 | { | |
9205 | int ctxn; | |
9206 | ||
9207 | for_each_task_context_nr(ctxn) | |
9208 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
9209 | } | |
9210 | ||
e03e7ee3 | 9211 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 9212 | { |
e03e7ee3 | 9213 | struct file *file; |
ffe8690c | 9214 | |
e03e7ee3 AS |
9215 | file = fget_raw(fd); |
9216 | if (!file) | |
9217 | return ERR_PTR(-EBADF); | |
ffe8690c | 9218 | |
e03e7ee3 AS |
9219 | if (file->f_op != &perf_fops) { |
9220 | fput(file); | |
9221 | return ERR_PTR(-EBADF); | |
9222 | } | |
ffe8690c | 9223 | |
e03e7ee3 | 9224 | return file; |
ffe8690c KX |
9225 | } |
9226 | ||
9227 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) | |
9228 | { | |
9229 | if (!event) | |
9230 | return ERR_PTR(-EINVAL); | |
9231 | ||
9232 | return &event->attr; | |
9233 | } | |
9234 | ||
97dee4f3 PZ |
9235 | /* |
9236 | * inherit a event from parent task to child task: | |
9237 | */ | |
9238 | static struct perf_event * | |
9239 | inherit_event(struct perf_event *parent_event, | |
9240 | struct task_struct *parent, | |
9241 | struct perf_event_context *parent_ctx, | |
9242 | struct task_struct *child, | |
9243 | struct perf_event *group_leader, | |
9244 | struct perf_event_context *child_ctx) | |
9245 | { | |
1929def9 | 9246 | enum perf_event_active_state parent_state = parent_event->state; |
97dee4f3 | 9247 | struct perf_event *child_event; |
cee010ec | 9248 | unsigned long flags; |
97dee4f3 PZ |
9249 | |
9250 | /* | |
9251 | * Instead of creating recursive hierarchies of events, | |
9252 | * we link inherited events back to the original parent, | |
9253 | * which has a filp for sure, which we use as the reference | |
9254 | * count: | |
9255 | */ | |
9256 | if (parent_event->parent) | |
9257 | parent_event = parent_event->parent; | |
9258 | ||
9259 | child_event = perf_event_alloc(&parent_event->attr, | |
9260 | parent_event->cpu, | |
d580ff86 | 9261 | child, |
97dee4f3 | 9262 | group_leader, parent_event, |
79dff51e | 9263 | NULL, NULL, -1); |
97dee4f3 PZ |
9264 | if (IS_ERR(child_event)) |
9265 | return child_event; | |
a6fa941d | 9266 | |
c6e5b732 PZ |
9267 | /* |
9268 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
9269 | * must be under the same lock in order to serialize against | |
9270 | * perf_event_release_kernel(), such that either we must observe | |
9271 | * is_orphaned_event() or they will observe us on the child_list. | |
9272 | */ | |
9273 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
9274 | if (is_orphaned_event(parent_event) || |
9275 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 9276 | mutex_unlock(&parent_event->child_mutex); |
a6fa941d AV |
9277 | free_event(child_event); |
9278 | return NULL; | |
9279 | } | |
9280 | ||
97dee4f3 PZ |
9281 | get_ctx(child_ctx); |
9282 | ||
9283 | /* | |
9284 | * Make the child state follow the state of the parent event, | |
9285 | * not its attr.disabled bit. We hold the parent's mutex, | |
9286 | * so we won't race with perf_event_{en, dis}able_family. | |
9287 | */ | |
1929def9 | 9288 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
9289 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
9290 | else | |
9291 | child_event->state = PERF_EVENT_STATE_OFF; | |
9292 | ||
9293 | if (parent_event->attr.freq) { | |
9294 | u64 sample_period = parent_event->hw.sample_period; | |
9295 | struct hw_perf_event *hwc = &child_event->hw; | |
9296 | ||
9297 | hwc->sample_period = sample_period; | |
9298 | hwc->last_period = sample_period; | |
9299 | ||
9300 | local64_set(&hwc->period_left, sample_period); | |
9301 | } | |
9302 | ||
9303 | child_event->ctx = child_ctx; | |
9304 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
9305 | child_event->overflow_handler_context |
9306 | = parent_event->overflow_handler_context; | |
97dee4f3 | 9307 | |
614b6780 TG |
9308 | /* |
9309 | * Precalculate sample_data sizes | |
9310 | */ | |
9311 | perf_event__header_size(child_event); | |
6844c09d | 9312 | perf_event__id_header_size(child_event); |
614b6780 | 9313 | |
97dee4f3 PZ |
9314 | /* |
9315 | * Link it up in the child's context: | |
9316 | */ | |
cee010ec | 9317 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 9318 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 9319 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 9320 | |
97dee4f3 PZ |
9321 | /* |
9322 | * Link this into the parent event's child list | |
9323 | */ | |
97dee4f3 PZ |
9324 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
9325 | mutex_unlock(&parent_event->child_mutex); | |
9326 | ||
9327 | return child_event; | |
9328 | } | |
9329 | ||
9330 | static int inherit_group(struct perf_event *parent_event, | |
9331 | struct task_struct *parent, | |
9332 | struct perf_event_context *parent_ctx, | |
9333 | struct task_struct *child, | |
9334 | struct perf_event_context *child_ctx) | |
9335 | { | |
9336 | struct perf_event *leader; | |
9337 | struct perf_event *sub; | |
9338 | struct perf_event *child_ctr; | |
9339 | ||
9340 | leader = inherit_event(parent_event, parent, parent_ctx, | |
9341 | child, NULL, child_ctx); | |
9342 | if (IS_ERR(leader)) | |
9343 | return PTR_ERR(leader); | |
9344 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { | |
9345 | child_ctr = inherit_event(sub, parent, parent_ctx, | |
9346 | child, leader, child_ctx); | |
9347 | if (IS_ERR(child_ctr)) | |
9348 | return PTR_ERR(child_ctr); | |
9349 | } | |
9350 | return 0; | |
889ff015 FW |
9351 | } |
9352 | ||
9353 | static int | |
9354 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
9355 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 9356 | struct task_struct *child, int ctxn, |
889ff015 FW |
9357 | int *inherited_all) |
9358 | { | |
9359 | int ret; | |
8dc85d54 | 9360 | struct perf_event_context *child_ctx; |
889ff015 FW |
9361 | |
9362 | if (!event->attr.inherit) { | |
9363 | *inherited_all = 0; | |
9364 | return 0; | |
bbbee908 PZ |
9365 | } |
9366 | ||
fe4b04fa | 9367 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
9368 | if (!child_ctx) { |
9369 | /* | |
9370 | * This is executed from the parent task context, so | |
9371 | * inherit events that have been marked for cloning. | |
9372 | * First allocate and initialize a context for the | |
9373 | * child. | |
9374 | */ | |
bbbee908 | 9375 | |
734df5ab | 9376 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
9377 | if (!child_ctx) |
9378 | return -ENOMEM; | |
bbbee908 | 9379 | |
8dc85d54 | 9380 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
9381 | } |
9382 | ||
9383 | ret = inherit_group(event, parent, parent_ctx, | |
9384 | child, child_ctx); | |
9385 | ||
9386 | if (ret) | |
9387 | *inherited_all = 0; | |
9388 | ||
9389 | return ret; | |
bbbee908 PZ |
9390 | } |
9391 | ||
9b51f66d | 9392 | /* |
cdd6c482 | 9393 | * Initialize the perf_event context in task_struct |
9b51f66d | 9394 | */ |
985c8dcb | 9395 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 9396 | { |
889ff015 | 9397 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
9398 | struct perf_event_context *cloned_ctx; |
9399 | struct perf_event *event; | |
9b51f66d | 9400 | struct task_struct *parent = current; |
564c2b21 | 9401 | int inherited_all = 1; |
dddd3379 | 9402 | unsigned long flags; |
6ab423e0 | 9403 | int ret = 0; |
9b51f66d | 9404 | |
8dc85d54 | 9405 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
9406 | return 0; |
9407 | ||
ad3a37de | 9408 | /* |
25346b93 PM |
9409 | * If the parent's context is a clone, pin it so it won't get |
9410 | * swapped under us. | |
ad3a37de | 9411 | */ |
8dc85d54 | 9412 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
9413 | if (!parent_ctx) |
9414 | return 0; | |
25346b93 | 9415 | |
ad3a37de PM |
9416 | /* |
9417 | * No need to check if parent_ctx != NULL here; since we saw | |
9418 | * it non-NULL earlier, the only reason for it to become NULL | |
9419 | * is if we exit, and since we're currently in the middle of | |
9420 | * a fork we can't be exiting at the same time. | |
9421 | */ | |
ad3a37de | 9422 | |
9b51f66d IM |
9423 | /* |
9424 | * Lock the parent list. No need to lock the child - not PID | |
9425 | * hashed yet and not running, so nobody can access it. | |
9426 | */ | |
d859e29f | 9427 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
9428 | |
9429 | /* | |
9430 | * We dont have to disable NMIs - we are only looking at | |
9431 | * the list, not manipulating it: | |
9432 | */ | |
889ff015 | 9433 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
8dc85d54 PZ |
9434 | ret = inherit_task_group(event, parent, parent_ctx, |
9435 | child, ctxn, &inherited_all); | |
889ff015 FW |
9436 | if (ret) |
9437 | break; | |
9438 | } | |
b93f7978 | 9439 | |
dddd3379 TG |
9440 | /* |
9441 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
9442 | * to allocations, but we need to prevent rotation because | |
9443 | * rotate_ctx() will change the list from interrupt context. | |
9444 | */ | |
9445 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
9446 | parent_ctx->rotate_disable = 1; | |
9447 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
9448 | ||
889ff015 | 9449 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
8dc85d54 PZ |
9450 | ret = inherit_task_group(event, parent, parent_ctx, |
9451 | child, ctxn, &inherited_all); | |
889ff015 | 9452 | if (ret) |
9b51f66d | 9453 | break; |
564c2b21 PM |
9454 | } |
9455 | ||
dddd3379 TG |
9456 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
9457 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 9458 | |
8dc85d54 | 9459 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 9460 | |
05cbaa28 | 9461 | if (child_ctx && inherited_all) { |
564c2b21 PM |
9462 | /* |
9463 | * Mark the child context as a clone of the parent | |
9464 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
9465 | * |
9466 | * Note that if the parent is a clone, the holding of | |
9467 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 9468 | */ |
c5ed5145 | 9469 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
9470 | if (cloned_ctx) { |
9471 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 9472 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
9473 | } else { |
9474 | child_ctx->parent_ctx = parent_ctx; | |
9475 | child_ctx->parent_gen = parent_ctx->generation; | |
9476 | } | |
9477 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
9478 | } |
9479 | ||
c5ed5145 | 9480 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
d859e29f | 9481 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 9482 | |
25346b93 | 9483 | perf_unpin_context(parent_ctx); |
fe4b04fa | 9484 | put_ctx(parent_ctx); |
ad3a37de | 9485 | |
6ab423e0 | 9486 | return ret; |
9b51f66d IM |
9487 | } |
9488 | ||
8dc85d54 PZ |
9489 | /* |
9490 | * Initialize the perf_event context in task_struct | |
9491 | */ | |
9492 | int perf_event_init_task(struct task_struct *child) | |
9493 | { | |
9494 | int ctxn, ret; | |
9495 | ||
8550d7cb ON |
9496 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
9497 | mutex_init(&child->perf_event_mutex); | |
9498 | INIT_LIST_HEAD(&child->perf_event_list); | |
9499 | ||
8dc85d54 PZ |
9500 | for_each_task_context_nr(ctxn) { |
9501 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
9502 | if (ret) { |
9503 | perf_event_free_task(child); | |
8dc85d54 | 9504 | return ret; |
6c72e350 | 9505 | } |
8dc85d54 PZ |
9506 | } |
9507 | ||
9508 | return 0; | |
9509 | } | |
9510 | ||
220b140b PM |
9511 | static void __init perf_event_init_all_cpus(void) |
9512 | { | |
b28ab83c | 9513 | struct swevent_htable *swhash; |
220b140b | 9514 | int cpu; |
220b140b PM |
9515 | |
9516 | for_each_possible_cpu(cpu) { | |
b28ab83c PZ |
9517 | swhash = &per_cpu(swevent_htable, cpu); |
9518 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 9519 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
220b140b PM |
9520 | } |
9521 | } | |
9522 | ||
0db0628d | 9523 | static void perf_event_init_cpu(int cpu) |
0793a61d | 9524 | { |
108b02cf | 9525 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 9526 | |
b28ab83c | 9527 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 9528 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
9529 | struct swevent_hlist *hlist; |
9530 | ||
b28ab83c PZ |
9531 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
9532 | WARN_ON(!hlist); | |
9533 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 9534 | } |
b28ab83c | 9535 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
9536 | } |
9537 | ||
2965faa5 | 9538 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 9539 | static void __perf_event_exit_context(void *__info) |
0793a61d | 9540 | { |
108b02cf | 9541 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
9542 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
9543 | struct perf_event *event; | |
0793a61d | 9544 | |
fae3fde6 PZ |
9545 | raw_spin_lock(&ctx->lock); |
9546 | list_for_each_entry(event, &ctx->event_list, event_entry) | |
45a0e07a | 9547 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 9548 | raw_spin_unlock(&ctx->lock); |
0793a61d | 9549 | } |
108b02cf PZ |
9550 | |
9551 | static void perf_event_exit_cpu_context(int cpu) | |
9552 | { | |
9553 | struct perf_event_context *ctx; | |
9554 | struct pmu *pmu; | |
9555 | int idx; | |
9556 | ||
9557 | idx = srcu_read_lock(&pmus_srcu); | |
9558 | list_for_each_entry_rcu(pmu, &pmus, entry) { | |
917bdd1c | 9559 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
108b02cf PZ |
9560 | |
9561 | mutex_lock(&ctx->mutex); | |
9562 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
9563 | mutex_unlock(&ctx->mutex); | |
9564 | } | |
9565 | srcu_read_unlock(&pmus_srcu, idx); | |
108b02cf PZ |
9566 | } |
9567 | ||
cdd6c482 | 9568 | static void perf_event_exit_cpu(int cpu) |
0793a61d | 9569 | { |
e3703f8c | 9570 | perf_event_exit_cpu_context(cpu); |
0793a61d TG |
9571 | } |
9572 | #else | |
cdd6c482 | 9573 | static inline void perf_event_exit_cpu(int cpu) { } |
0793a61d TG |
9574 | #endif |
9575 | ||
c277443c PZ |
9576 | static int |
9577 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
9578 | { | |
9579 | int cpu; | |
9580 | ||
9581 | for_each_online_cpu(cpu) | |
9582 | perf_event_exit_cpu(cpu); | |
9583 | ||
9584 | return NOTIFY_OK; | |
9585 | } | |
9586 | ||
9587 | /* | |
9588 | * Run the perf reboot notifier at the very last possible moment so that | |
9589 | * the generic watchdog code runs as long as possible. | |
9590 | */ | |
9591 | static struct notifier_block perf_reboot_notifier = { | |
9592 | .notifier_call = perf_reboot, | |
9593 | .priority = INT_MIN, | |
9594 | }; | |
9595 | ||
0db0628d | 9596 | static int |
0793a61d TG |
9597 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
9598 | { | |
9599 | unsigned int cpu = (long)hcpu; | |
9600 | ||
4536e4d1 | 9601 | switch (action & ~CPU_TASKS_FROZEN) { |
0793a61d TG |
9602 | |
9603 | case CPU_UP_PREPARE: | |
1dcaac1c PZ |
9604 | /* |
9605 | * This must be done before the CPU comes alive, because the | |
9606 | * moment we can run tasks we can encounter (software) events. | |
9607 | * | |
9608 | * Specifically, someone can have inherited events on kthreadd | |
9609 | * or a pre-existing worker thread that gets re-bound. | |
9610 | */ | |
cdd6c482 | 9611 | perf_event_init_cpu(cpu); |
0793a61d TG |
9612 | break; |
9613 | ||
9614 | case CPU_DOWN_PREPARE: | |
1dcaac1c PZ |
9615 | /* |
9616 | * This must be done before the CPU dies because after that an | |
9617 | * active event might want to IPI the CPU and that'll not work | |
9618 | * so great for dead CPUs. | |
9619 | * | |
9620 | * XXX smp_call_function_single() return -ENXIO without a warn | |
9621 | * so we could possibly deal with this. | |
9622 | * | |
9623 | * This is safe against new events arriving because | |
9624 | * sys_perf_event_open() serializes against hotplug using | |
9625 | * get_online_cpus(). | |
9626 | */ | |
cdd6c482 | 9627 | perf_event_exit_cpu(cpu); |
0793a61d | 9628 | break; |
0793a61d TG |
9629 | default: |
9630 | break; | |
9631 | } | |
9632 | ||
9633 | return NOTIFY_OK; | |
9634 | } | |
9635 | ||
cdd6c482 | 9636 | void __init perf_event_init(void) |
0793a61d | 9637 | { |
3c502e7a JW |
9638 | int ret; |
9639 | ||
2e80a82a PZ |
9640 | idr_init(&pmu_idr); |
9641 | ||
220b140b | 9642 | perf_event_init_all_cpus(); |
b0a873eb | 9643 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
9644 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
9645 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
9646 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb PZ |
9647 | perf_tp_register(); |
9648 | perf_cpu_notifier(perf_cpu_notify); | |
c277443c | 9649 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
9650 | |
9651 | ret = init_hw_breakpoint(); | |
9652 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 9653 | |
b01c3a00 JO |
9654 | /* |
9655 | * Build time assertion that we keep the data_head at the intended | |
9656 | * location. IOW, validation we got the __reserved[] size right. | |
9657 | */ | |
9658 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
9659 | != 1024); | |
0793a61d | 9660 | } |
abe43400 | 9661 | |
fd979c01 CS |
9662 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
9663 | char *page) | |
9664 | { | |
9665 | struct perf_pmu_events_attr *pmu_attr = | |
9666 | container_of(attr, struct perf_pmu_events_attr, attr); | |
9667 | ||
9668 | if (pmu_attr->event_str) | |
9669 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
9670 | ||
9671 | return 0; | |
9672 | } | |
675965b0 | 9673 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 9674 | |
abe43400 PZ |
9675 | static int __init perf_event_sysfs_init(void) |
9676 | { | |
9677 | struct pmu *pmu; | |
9678 | int ret; | |
9679 | ||
9680 | mutex_lock(&pmus_lock); | |
9681 | ||
9682 | ret = bus_register(&pmu_bus); | |
9683 | if (ret) | |
9684 | goto unlock; | |
9685 | ||
9686 | list_for_each_entry(pmu, &pmus, entry) { | |
9687 | if (!pmu->name || pmu->type < 0) | |
9688 | continue; | |
9689 | ||
9690 | ret = pmu_dev_alloc(pmu); | |
9691 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
9692 | } | |
9693 | pmu_bus_running = 1; | |
9694 | ret = 0; | |
9695 | ||
9696 | unlock: | |
9697 | mutex_unlock(&pmus_lock); | |
9698 | ||
9699 | return ret; | |
9700 | } | |
9701 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
9702 | |
9703 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
9704 | static struct cgroup_subsys_state * |
9705 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
9706 | { |
9707 | struct perf_cgroup *jc; | |
e5d1367f | 9708 | |
1b15d055 | 9709 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
9710 | if (!jc) |
9711 | return ERR_PTR(-ENOMEM); | |
9712 | ||
e5d1367f SE |
9713 | jc->info = alloc_percpu(struct perf_cgroup_info); |
9714 | if (!jc->info) { | |
9715 | kfree(jc); | |
9716 | return ERR_PTR(-ENOMEM); | |
9717 | } | |
9718 | ||
e5d1367f SE |
9719 | return &jc->css; |
9720 | } | |
9721 | ||
eb95419b | 9722 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 9723 | { |
eb95419b TH |
9724 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
9725 | ||
e5d1367f SE |
9726 | free_percpu(jc->info); |
9727 | kfree(jc); | |
9728 | } | |
9729 | ||
9730 | static int __perf_cgroup_move(void *info) | |
9731 | { | |
9732 | struct task_struct *task = info; | |
ddaaf4e2 | 9733 | rcu_read_lock(); |
e5d1367f | 9734 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 9735 | rcu_read_unlock(); |
e5d1367f SE |
9736 | return 0; |
9737 | } | |
9738 | ||
1f7dd3e5 | 9739 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 9740 | { |
bb9d97b6 | 9741 | struct task_struct *task; |
1f7dd3e5 | 9742 | struct cgroup_subsys_state *css; |
bb9d97b6 | 9743 | |
1f7dd3e5 | 9744 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 9745 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
9746 | } |
9747 | ||
073219e9 | 9748 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
9749 | .css_alloc = perf_cgroup_css_alloc, |
9750 | .css_free = perf_cgroup_css_free, | |
bb9d97b6 | 9751 | .attach = perf_cgroup_attach, |
e5d1367f SE |
9752 | }; |
9753 | #endif /* CONFIG_CGROUP_PERF */ |