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