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