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