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