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