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