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