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