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