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