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