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