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