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