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