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