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