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