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