Commit | Line | Data |
---|---|---|
8e86e015 | 1 | // SPDX-License-Identifier: GPL-2.0 |
0793a61d | 2 | /* |
57c0c15b | 3 | * Performance events core code: |
0793a61d | 4 | * |
98144511 | 5 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
e7e7ee2e | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
90eec103 | 7 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
d36b6910 | 8 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
0793a61d TG |
9 | */ |
10 | ||
11 | #include <linux/fs.h> | |
b9cacc7b | 12 | #include <linux/mm.h> |
0793a61d TG |
13 | #include <linux/cpu.h> |
14 | #include <linux/smp.h> | |
2e80a82a | 15 | #include <linux/idr.h> |
04289bb9 | 16 | #include <linux/file.h> |
0793a61d | 17 | #include <linux/poll.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
76e1d904 | 19 | #include <linux/hash.h> |
12351ef8 | 20 | #include <linux/tick.h> |
0793a61d | 21 | #include <linux/sysfs.h> |
22a4f650 | 22 | #include <linux/dcache.h> |
0793a61d | 23 | #include <linux/percpu.h> |
22a4f650 | 24 | #include <linux/ptrace.h> |
c277443c | 25 | #include <linux/reboot.h> |
b9cacc7b | 26 | #include <linux/vmstat.h> |
abe43400 | 27 | #include <linux/device.h> |
6e5fdeed | 28 | #include <linux/export.h> |
906010b2 | 29 | #include <linux/vmalloc.h> |
b9cacc7b | 30 | #include <linux/hardirq.h> |
03911132 | 31 | #include <linux/hugetlb.h> |
b9cacc7b | 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> | |
375637bc AS |
47 | #include <linux/namei.h> |
48 | #include <linux/parser.h> | |
e6017571 | 49 | #include <linux/sched/clock.h> |
6e84f315 | 50 | #include <linux/sched/mm.h> |
e4222673 HB |
51 | #include <linux/proc_ns.h> |
52 | #include <linux/mount.h> | |
6eef8a71 | 53 | #include <linux/min_heap.h> |
0793a61d | 54 | |
76369139 FW |
55 | #include "internal.h" |
56 | ||
4e193bd4 TB |
57 | #include <asm/irq_regs.h> |
58 | ||
272325c4 PZ |
59 | typedef int (*remote_function_f)(void *); |
60 | ||
fe4b04fa | 61 | struct remote_function_call { |
e7e7ee2e | 62 | struct task_struct *p; |
272325c4 | 63 | remote_function_f func; |
e7e7ee2e IM |
64 | void *info; |
65 | int ret; | |
fe4b04fa PZ |
66 | }; |
67 | ||
68 | static void remote_function(void *data) | |
69 | { | |
70 | struct remote_function_call *tfc = data; | |
71 | struct task_struct *p = tfc->p; | |
72 | ||
73 | if (p) { | |
0da4cf3e PZ |
74 | /* -EAGAIN */ |
75 | if (task_cpu(p) != smp_processor_id()) | |
76 | return; | |
77 | ||
78 | /* | |
79 | * Now that we're on right CPU with IRQs disabled, we can test | |
80 | * if we hit the right task without races. | |
81 | */ | |
82 | ||
83 | tfc->ret = -ESRCH; /* No such (running) process */ | |
84 | if (p != current) | |
fe4b04fa PZ |
85 | return; |
86 | } | |
87 | ||
88 | tfc->ret = tfc->func(tfc->info); | |
89 | } | |
90 | ||
91 | /** | |
92 | * task_function_call - call a function on the cpu on which a task runs | |
93 | * @p: the task to evaluate | |
94 | * @func: the function to be called | |
95 | * @info: the function call argument | |
96 | * | |
97 | * Calls the function @func when the task is currently running. This might | |
2ed6edd3 BR |
98 | * be on the current CPU, which just calls the function directly. This will |
99 | * retry due to any failures in smp_call_function_single(), such as if the | |
100 | * task_cpu() goes offline concurrently. | |
fe4b04fa | 101 | * |
2ed6edd3 | 102 | * returns @func return value or -ESRCH when the process isn't running |
fe4b04fa PZ |
103 | */ |
104 | static int | |
272325c4 | 105 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
fe4b04fa PZ |
106 | { |
107 | struct remote_function_call data = { | |
e7e7ee2e IM |
108 | .p = p, |
109 | .func = func, | |
110 | .info = info, | |
0da4cf3e | 111 | .ret = -EAGAIN, |
fe4b04fa | 112 | }; |
0da4cf3e | 113 | int ret; |
fe4b04fa | 114 | |
2ed6edd3 BR |
115 | for (;;) { |
116 | ret = smp_call_function_single(task_cpu(p), remote_function, | |
117 | &data, 1); | |
118 | ret = !ret ? data.ret : -EAGAIN; | |
119 | ||
120 | if (ret != -EAGAIN) | |
121 | break; | |
122 | ||
123 | cond_resched(); | |
124 | } | |
fe4b04fa | 125 | |
0da4cf3e | 126 | return ret; |
fe4b04fa PZ |
127 | } |
128 | ||
129 | /** | |
130 | * cpu_function_call - call a function on the cpu | |
131 | * @func: the function to be called | |
132 | * @info: the function call argument | |
133 | * | |
134 | * Calls the function @func on the remote cpu. | |
135 | * | |
136 | * returns: @func return value or -ENXIO when the cpu is offline | |
137 | */ | |
272325c4 | 138 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
fe4b04fa PZ |
139 | { |
140 | struct remote_function_call data = { | |
e7e7ee2e IM |
141 | .p = NULL, |
142 | .func = func, | |
143 | .info = info, | |
144 | .ret = -ENXIO, /* No such CPU */ | |
fe4b04fa PZ |
145 | }; |
146 | ||
147 | smp_call_function_single(cpu, remote_function, &data, 1); | |
148 | ||
149 | return data.ret; | |
150 | } | |
151 | ||
fae3fde6 PZ |
152 | static inline struct perf_cpu_context * |
153 | __get_cpu_context(struct perf_event_context *ctx) | |
154 | { | |
155 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); | |
156 | } | |
157 | ||
158 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, | |
159 | struct perf_event_context *ctx) | |
0017960f | 160 | { |
fae3fde6 PZ |
161 | raw_spin_lock(&cpuctx->ctx.lock); |
162 | if (ctx) | |
163 | raw_spin_lock(&ctx->lock); | |
164 | } | |
165 | ||
166 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, | |
167 | struct perf_event_context *ctx) | |
168 | { | |
169 | if (ctx) | |
170 | raw_spin_unlock(&ctx->lock); | |
171 | raw_spin_unlock(&cpuctx->ctx.lock); | |
172 | } | |
173 | ||
63b6da39 PZ |
174 | #define TASK_TOMBSTONE ((void *)-1L) |
175 | ||
176 | static bool is_kernel_event(struct perf_event *event) | |
177 | { | |
f47c02c0 | 178 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
63b6da39 PZ |
179 | } |
180 | ||
39a43640 PZ |
181 | /* |
182 | * On task ctx scheduling... | |
183 | * | |
184 | * When !ctx->nr_events a task context will not be scheduled. This means | |
185 | * we can disable the scheduler hooks (for performance) without leaving | |
186 | * pending task ctx state. | |
187 | * | |
188 | * This however results in two special cases: | |
189 | * | |
190 | * - removing the last event from a task ctx; this is relatively straight | |
191 | * forward and is done in __perf_remove_from_context. | |
192 | * | |
193 | * - adding the first event to a task ctx; this is tricky because we cannot | |
194 | * rely on ctx->is_active and therefore cannot use event_function_call(). | |
195 | * See perf_install_in_context(). | |
196 | * | |
39a43640 PZ |
197 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
198 | */ | |
199 | ||
fae3fde6 PZ |
200 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
201 | struct perf_event_context *, void *); | |
202 | ||
203 | struct event_function_struct { | |
204 | struct perf_event *event; | |
205 | event_f func; | |
206 | void *data; | |
207 | }; | |
208 | ||
209 | static int event_function(void *info) | |
210 | { | |
211 | struct event_function_struct *efs = info; | |
212 | struct perf_event *event = efs->event; | |
0017960f | 213 | struct perf_event_context *ctx = event->ctx; |
fae3fde6 PZ |
214 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
215 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
63b6da39 | 216 | int ret = 0; |
fae3fde6 | 217 | |
16444645 | 218 | lockdep_assert_irqs_disabled(); |
fae3fde6 | 219 | |
63b6da39 | 220 | perf_ctx_lock(cpuctx, task_ctx); |
fae3fde6 PZ |
221 | /* |
222 | * Since we do the IPI call without holding ctx->lock things can have | |
223 | * changed, double check we hit the task we set out to hit. | |
fae3fde6 PZ |
224 | */ |
225 | if (ctx->task) { | |
63b6da39 | 226 | if (ctx->task != current) { |
0da4cf3e | 227 | ret = -ESRCH; |
63b6da39 PZ |
228 | goto unlock; |
229 | } | |
fae3fde6 | 230 | |
fae3fde6 PZ |
231 | /* |
232 | * We only use event_function_call() on established contexts, | |
233 | * and event_function() is only ever called when active (or | |
234 | * rather, we'll have bailed in task_function_call() or the | |
235 | * above ctx->task != current test), therefore we must have | |
236 | * ctx->is_active here. | |
237 | */ | |
238 | WARN_ON_ONCE(!ctx->is_active); | |
239 | /* | |
240 | * And since we have ctx->is_active, cpuctx->task_ctx must | |
241 | * match. | |
242 | */ | |
63b6da39 PZ |
243 | WARN_ON_ONCE(task_ctx != ctx); |
244 | } else { | |
245 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
fae3fde6 | 246 | } |
63b6da39 | 247 | |
fae3fde6 | 248 | efs->func(event, cpuctx, ctx, efs->data); |
63b6da39 | 249 | unlock: |
fae3fde6 PZ |
250 | perf_ctx_unlock(cpuctx, task_ctx); |
251 | ||
63b6da39 | 252 | return ret; |
fae3fde6 PZ |
253 | } |
254 | ||
fae3fde6 | 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 | ||
63b6da39 PZ |
279 | if (task == TASK_TOMBSTONE) |
280 | return; | |
281 | ||
a096309b | 282 | again: |
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; | |
a096309b PZ |
292 | if (task == TASK_TOMBSTONE) { |
293 | raw_spin_unlock_irq(&ctx->lock); | |
294 | return; | |
0017960f | 295 | } |
a096309b PZ |
296 | if (ctx->is_active) { |
297 | raw_spin_unlock_irq(&ctx->lock); | |
298 | goto again; | |
299 | } | |
300 | func(event, NULL, ctx, data); | |
0017960f PZ |
301 | raw_spin_unlock_irq(&ctx->lock); |
302 | } | |
303 | ||
cca20946 PZ |
304 | /* |
305 | * Similar to event_function_call() + event_function(), but hard assumes IRQs | |
306 | * are already disabled and we're on the right CPU. | |
307 | */ | |
308 | static void event_function_local(struct perf_event *event, event_f func, void *data) | |
309 | { | |
310 | struct perf_event_context *ctx = event->ctx; | |
311 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
312 | struct task_struct *task = READ_ONCE(ctx->task); | |
313 | struct perf_event_context *task_ctx = NULL; | |
314 | ||
16444645 | 315 | lockdep_assert_irqs_disabled(); |
cca20946 PZ |
316 | |
317 | if (task) { | |
318 | if (task == TASK_TOMBSTONE) | |
319 | return; | |
320 | ||
321 | task_ctx = ctx; | |
322 | } | |
323 | ||
324 | perf_ctx_lock(cpuctx, task_ctx); | |
325 | ||
326 | task = ctx->task; | |
327 | if (task == TASK_TOMBSTONE) | |
328 | goto unlock; | |
329 | ||
330 | if (task) { | |
331 | /* | |
332 | * We must be either inactive or active and the right task, | |
333 | * otherwise we're screwed, since we cannot IPI to somewhere | |
334 | * else. | |
335 | */ | |
336 | if (ctx->is_active) { | |
337 | if (WARN_ON_ONCE(task != current)) | |
338 | goto unlock; | |
339 | ||
340 | if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) | |
341 | goto unlock; | |
342 | } | |
343 | } else { | |
344 | WARN_ON_ONCE(&cpuctx->ctx != ctx); | |
345 | } | |
346 | ||
347 | func(event, cpuctx, ctx, data); | |
348 | unlock: | |
349 | perf_ctx_unlock(cpuctx, task_ctx); | |
350 | } | |
351 | ||
e5d1367f SE |
352 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
353 | PERF_FLAG_FD_OUTPUT |\ | |
a21b0b35 YD |
354 | PERF_FLAG_PID_CGROUP |\ |
355 | PERF_FLAG_FD_CLOEXEC) | |
e5d1367f | 356 | |
bce38cd5 SE |
357 | /* |
358 | * branch priv levels that need permission checks | |
359 | */ | |
360 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ | |
361 | (PERF_SAMPLE_BRANCH_KERNEL |\ | |
362 | PERF_SAMPLE_BRANCH_HV) | |
363 | ||
0b3fcf17 SE |
364 | enum event_type_t { |
365 | EVENT_FLEXIBLE = 0x1, | |
366 | EVENT_PINNED = 0x2, | |
3cbaa590 | 367 | EVENT_TIME = 0x4, |
487f05e1 AS |
368 | /* see ctx_resched() for details */ |
369 | EVENT_CPU = 0x8, | |
0b3fcf17 SE |
370 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
371 | }; | |
372 | ||
e5d1367f SE |
373 | /* |
374 | * perf_sched_events : >0 events exist | |
375 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu | |
376 | */ | |
9107c89e PZ |
377 | |
378 | static void perf_sched_delayed(struct work_struct *work); | |
379 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); | |
380 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); | |
381 | static DEFINE_MUTEX(perf_sched_mutex); | |
382 | static atomic_t perf_sched_count; | |
383 | ||
e5d1367f | 384 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
ba532500 | 385 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
f2fb6bef | 386 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
e5d1367f | 387 | |
cdd6c482 IM |
388 | static atomic_t nr_mmap_events __read_mostly; |
389 | static atomic_t nr_comm_events __read_mostly; | |
e4222673 | 390 | static atomic_t nr_namespaces_events __read_mostly; |
cdd6c482 | 391 | static atomic_t nr_task_events __read_mostly; |
948b26b6 | 392 | static atomic_t nr_freq_events __read_mostly; |
45ac1403 | 393 | static atomic_t nr_switch_events __read_mostly; |
76193a94 | 394 | static atomic_t nr_ksymbol_events __read_mostly; |
6ee52e2a | 395 | static atomic_t nr_bpf_events __read_mostly; |
96aaab68 | 396 | static atomic_t nr_cgroup_events __read_mostly; |
e17d43b9 | 397 | static atomic_t nr_text_poke_events __read_mostly; |
9ee318a7 | 398 | |
108b02cf PZ |
399 | static LIST_HEAD(pmus); |
400 | static DEFINE_MUTEX(pmus_lock); | |
401 | static struct srcu_struct pmus_srcu; | |
a63fbed7 | 402 | static cpumask_var_t perf_online_mask; |
108b02cf | 403 | |
0764771d | 404 | /* |
cdd6c482 | 405 | * perf event paranoia level: |
0fbdea19 IM |
406 | * -1 - not paranoid at all |
407 | * 0 - disallow raw tracepoint access for unpriv | |
cdd6c482 | 408 | * 1 - disallow cpu events for unpriv |
0fbdea19 | 409 | * 2 - disallow kernel profiling for unpriv |
0764771d | 410 | */ |
0161028b | 411 | int sysctl_perf_event_paranoid __read_mostly = 2; |
0764771d | 412 | |
20443384 FW |
413 | /* Minimum for 512 kiB + 1 user control page */ |
414 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ | |
df58ab24 PZ |
415 | |
416 | /* | |
cdd6c482 | 417 | * max perf event sample rate |
df58ab24 | 418 | */ |
14c63f17 DH |
419 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
420 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) | |
421 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 | |
422 | ||
423 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; | |
424 | ||
425 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); | |
426 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; | |
427 | ||
d9494cb4 PZ |
428 | static int perf_sample_allowed_ns __read_mostly = |
429 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; | |
14c63f17 | 430 | |
18ab2cd3 | 431 | static void update_perf_cpu_limits(void) |
14c63f17 DH |
432 | { |
433 | u64 tmp = perf_sample_period_ns; | |
434 | ||
435 | tmp *= sysctl_perf_cpu_time_max_percent; | |
91a612ee PZ |
436 | tmp = div_u64(tmp, 100); |
437 | if (!tmp) | |
438 | tmp = 1; | |
439 | ||
440 | WRITE_ONCE(perf_sample_allowed_ns, tmp); | |
14c63f17 | 441 | } |
163ec435 | 442 | |
8d5bce0c | 443 | static bool perf_rotate_context(struct perf_cpu_context *cpuctx); |
9e630205 | 444 | |
163ec435 | 445 | int perf_proc_update_handler(struct ctl_table *table, int write, |
32927393 | 446 | void *buffer, size_t *lenp, loff_t *ppos) |
163ec435 | 447 | { |
1a51c5da SE |
448 | int ret; |
449 | int perf_cpu = sysctl_perf_cpu_time_max_percent; | |
ab7fdefb KL |
450 | /* |
451 | * If throttling is disabled don't allow the write: | |
452 | */ | |
1a51c5da | 453 | if (write && (perf_cpu == 100 || perf_cpu == 0)) |
ab7fdefb KL |
454 | return -EINVAL; |
455 | ||
1a51c5da SE |
456 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
457 | if (ret || !write) | |
458 | return ret; | |
459 | ||
163ec435 | 460 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
14c63f17 DH |
461 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
462 | update_perf_cpu_limits(); | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; | |
468 | ||
469 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, | |
32927393 | 470 | void *buffer, size_t *lenp, loff_t *ppos) |
14c63f17 | 471 | { |
1572e45a | 472 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
14c63f17 DH |
473 | |
474 | if (ret || !write) | |
475 | return ret; | |
476 | ||
b303e7c1 PZ |
477 | if (sysctl_perf_cpu_time_max_percent == 100 || |
478 | sysctl_perf_cpu_time_max_percent == 0) { | |
91a612ee PZ |
479 | printk(KERN_WARNING |
480 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); | |
481 | WRITE_ONCE(perf_sample_allowed_ns, 0); | |
482 | } else { | |
483 | update_perf_cpu_limits(); | |
484 | } | |
163ec435 PZ |
485 | |
486 | return 0; | |
487 | } | |
1ccd1549 | 488 | |
14c63f17 DH |
489 | /* |
490 | * perf samples are done in some very critical code paths (NMIs). | |
491 | * If they take too much CPU time, the system can lock up and not | |
492 | * get any real work done. This will drop the sample rate when | |
493 | * we detect that events are taking too long. | |
494 | */ | |
495 | #define NR_ACCUMULATED_SAMPLES 128 | |
d9494cb4 | 496 | static DEFINE_PER_CPU(u64, running_sample_length); |
14c63f17 | 497 | |
91a612ee PZ |
498 | static u64 __report_avg; |
499 | static u64 __report_allowed; | |
500 | ||
6a02ad66 | 501 | static void perf_duration_warn(struct irq_work *w) |
14c63f17 | 502 | { |
0d87d7ec | 503 | printk_ratelimited(KERN_INFO |
91a612ee PZ |
504 | "perf: interrupt took too long (%lld > %lld), lowering " |
505 | "kernel.perf_event_max_sample_rate to %d\n", | |
506 | __report_avg, __report_allowed, | |
507 | sysctl_perf_event_sample_rate); | |
6a02ad66 PZ |
508 | } |
509 | ||
510 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); | |
511 | ||
512 | void perf_sample_event_took(u64 sample_len_ns) | |
513 | { | |
91a612ee PZ |
514 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
515 | u64 running_len; | |
516 | u64 avg_len; | |
517 | u32 max; | |
14c63f17 | 518 | |
91a612ee | 519 | if (max_len == 0) |
14c63f17 DH |
520 | return; |
521 | ||
91a612ee PZ |
522 | /* Decay the counter by 1 average sample. */ |
523 | running_len = __this_cpu_read(running_sample_length); | |
524 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; | |
525 | running_len += sample_len_ns; | |
526 | __this_cpu_write(running_sample_length, running_len); | |
14c63f17 DH |
527 | |
528 | /* | |
91a612ee PZ |
529 | * Note: this will be biased artifically low until we have |
530 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us | |
14c63f17 DH |
531 | * from having to maintain a count. |
532 | */ | |
91a612ee PZ |
533 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
534 | if (avg_len <= max_len) | |
14c63f17 DH |
535 | return; |
536 | ||
91a612ee PZ |
537 | __report_avg = avg_len; |
538 | __report_allowed = max_len; | |
14c63f17 | 539 | |
91a612ee PZ |
540 | /* |
541 | * Compute a throttle threshold 25% below the current duration. | |
542 | */ | |
543 | avg_len += avg_len / 4; | |
544 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; | |
545 | if (avg_len < max) | |
546 | max /= (u32)avg_len; | |
547 | else | |
548 | max = 1; | |
14c63f17 | 549 | |
91a612ee PZ |
550 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
551 | WRITE_ONCE(max_samples_per_tick, max); | |
552 | ||
553 | sysctl_perf_event_sample_rate = max * HZ; | |
554 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; | |
6a02ad66 | 555 | |
cd578abb | 556 | if (!irq_work_queue(&perf_duration_work)) { |
91a612ee | 557 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
cd578abb | 558 | "kernel.perf_event_max_sample_rate to %d\n", |
91a612ee | 559 | __report_avg, __report_allowed, |
cd578abb PZ |
560 | sysctl_perf_event_sample_rate); |
561 | } | |
14c63f17 DH |
562 | } |
563 | ||
cdd6c482 | 564 | static atomic64_t perf_event_id; |
a96bbc16 | 565 | |
0b3fcf17 SE |
566 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
567 | enum event_type_t event_type); | |
568 | ||
569 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
570 | enum event_type_t event_type, |
571 | struct task_struct *task); | |
572 | ||
573 | static void update_context_time(struct perf_event_context *ctx); | |
574 | static u64 perf_event_time(struct perf_event *event); | |
0b3fcf17 | 575 | |
cdd6c482 | 576 | void __weak perf_event_print_debug(void) { } |
0793a61d | 577 | |
84c79910 | 578 | extern __weak const char *perf_pmu_name(void) |
0793a61d | 579 | { |
84c79910 | 580 | return "pmu"; |
0793a61d TG |
581 | } |
582 | ||
0b3fcf17 SE |
583 | static inline u64 perf_clock(void) |
584 | { | |
585 | return local_clock(); | |
586 | } | |
587 | ||
34f43927 PZ |
588 | static inline u64 perf_event_clock(struct perf_event *event) |
589 | { | |
590 | return event->clock(); | |
591 | } | |
592 | ||
0d3d73aa PZ |
593 | /* |
594 | * State based event timekeeping... | |
595 | * | |
596 | * The basic idea is to use event->state to determine which (if any) time | |
597 | * fields to increment with the current delta. This means we only need to | |
598 | * update timestamps when we change state or when they are explicitly requested | |
599 | * (read). | |
600 | * | |
601 | * Event groups make things a little more complicated, but not terribly so. The | |
602 | * rules for a group are that if the group leader is OFF the entire group is | |
603 | * OFF, irrespecive of what the group member states are. This results in | |
604 | * __perf_effective_state(). | |
605 | * | |
606 | * A futher ramification is that when a group leader flips between OFF and | |
607 | * !OFF, we need to update all group member times. | |
608 | * | |
609 | * | |
610 | * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we | |
611 | * need to make sure the relevant context time is updated before we try and | |
612 | * update our timestamps. | |
613 | */ | |
614 | ||
615 | static __always_inline enum perf_event_state | |
616 | __perf_effective_state(struct perf_event *event) | |
617 | { | |
618 | struct perf_event *leader = event->group_leader; | |
619 | ||
620 | if (leader->state <= PERF_EVENT_STATE_OFF) | |
621 | return leader->state; | |
622 | ||
623 | return event->state; | |
624 | } | |
625 | ||
626 | static __always_inline void | |
627 | __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running) | |
628 | { | |
629 | enum perf_event_state state = __perf_effective_state(event); | |
630 | u64 delta = now - event->tstamp; | |
631 | ||
632 | *enabled = event->total_time_enabled; | |
633 | if (state >= PERF_EVENT_STATE_INACTIVE) | |
634 | *enabled += delta; | |
635 | ||
636 | *running = event->total_time_running; | |
637 | if (state >= PERF_EVENT_STATE_ACTIVE) | |
638 | *running += delta; | |
639 | } | |
640 | ||
641 | static void perf_event_update_time(struct perf_event *event) | |
642 | { | |
643 | u64 now = perf_event_time(event); | |
644 | ||
645 | __perf_update_times(event, now, &event->total_time_enabled, | |
646 | &event->total_time_running); | |
647 | event->tstamp = now; | |
648 | } | |
649 | ||
650 | static void perf_event_update_sibling_time(struct perf_event *leader) | |
651 | { | |
652 | struct perf_event *sibling; | |
653 | ||
edb39592 | 654 | for_each_sibling_event(sibling, leader) |
0d3d73aa PZ |
655 | perf_event_update_time(sibling); |
656 | } | |
657 | ||
658 | static void | |
659 | perf_event_set_state(struct perf_event *event, enum perf_event_state state) | |
660 | { | |
661 | if (event->state == state) | |
662 | return; | |
663 | ||
664 | perf_event_update_time(event); | |
665 | /* | |
666 | * If a group leader gets enabled/disabled all its siblings | |
667 | * are affected too. | |
668 | */ | |
669 | if ((event->state < 0) ^ (state < 0)) | |
670 | perf_event_update_sibling_time(event); | |
671 | ||
672 | WRITE_ONCE(event->state, state); | |
673 | } | |
674 | ||
e5d1367f SE |
675 | #ifdef CONFIG_CGROUP_PERF |
676 | ||
e5d1367f SE |
677 | static inline bool |
678 | perf_cgroup_match(struct perf_event *event) | |
679 | { | |
680 | struct perf_event_context *ctx = event->ctx; | |
681 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
682 | ||
ef824fa1 TH |
683 | /* @event doesn't care about cgroup */ |
684 | if (!event->cgrp) | |
685 | return true; | |
686 | ||
687 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ | |
688 | if (!cpuctx->cgrp) | |
689 | return false; | |
690 | ||
691 | /* | |
692 | * Cgroup scoping is recursive. An event enabled for a cgroup is | |
693 | * also enabled for all its descendant cgroups. If @cpuctx's | |
694 | * cgroup is a descendant of @event's (the test covers identity | |
695 | * case), it's a match. | |
696 | */ | |
697 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, | |
698 | event->cgrp->css.cgroup); | |
e5d1367f SE |
699 | } |
700 | ||
e5d1367f SE |
701 | static inline void perf_detach_cgroup(struct perf_event *event) |
702 | { | |
4e2ba650 | 703 | css_put(&event->cgrp->css); |
e5d1367f SE |
704 | event->cgrp = NULL; |
705 | } | |
706 | ||
707 | static inline int is_cgroup_event(struct perf_event *event) | |
708 | { | |
709 | return event->cgrp != NULL; | |
710 | } | |
711 | ||
712 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
713 | { | |
714 | struct perf_cgroup_info *t; | |
715 | ||
716 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
717 | return t->time; | |
718 | } | |
719 | ||
720 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) | |
721 | { | |
722 | struct perf_cgroup_info *info; | |
723 | u64 now; | |
724 | ||
725 | now = perf_clock(); | |
726 | ||
727 | info = this_cpu_ptr(cgrp->info); | |
728 | ||
729 | info->time += now - info->timestamp; | |
730 | info->timestamp = now; | |
731 | } | |
732 | ||
733 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
734 | { | |
c917e0f2 SL |
735 | struct perf_cgroup *cgrp = cpuctx->cgrp; |
736 | struct cgroup_subsys_state *css; | |
737 | ||
738 | if (cgrp) { | |
739 | for (css = &cgrp->css; css; css = css->parent) { | |
740 | cgrp = container_of(css, struct perf_cgroup, css); | |
741 | __update_cgrp_time(cgrp); | |
742 | } | |
743 | } | |
e5d1367f SE |
744 | } |
745 | ||
746 | static inline void update_cgrp_time_from_event(struct perf_event *event) | |
747 | { | |
3f7cce3c SE |
748 | struct perf_cgroup *cgrp; |
749 | ||
e5d1367f | 750 | /* |
3f7cce3c SE |
751 | * ensure we access cgroup data only when needed and |
752 | * when we know the cgroup is pinned (css_get) | |
e5d1367f | 753 | */ |
3f7cce3c | 754 | if (!is_cgroup_event(event)) |
e5d1367f SE |
755 | return; |
756 | ||
614e4c4e | 757 | cgrp = perf_cgroup_from_task(current, event->ctx); |
3f7cce3c SE |
758 | /* |
759 | * Do not update time when cgroup is not active | |
760 | */ | |
28fa741c | 761 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
3f7cce3c | 762 | __update_cgrp_time(event->cgrp); |
e5d1367f SE |
763 | } |
764 | ||
765 | static inline void | |
3f7cce3c SE |
766 | perf_cgroup_set_timestamp(struct task_struct *task, |
767 | struct perf_event_context *ctx) | |
e5d1367f SE |
768 | { |
769 | struct perf_cgroup *cgrp; | |
770 | struct perf_cgroup_info *info; | |
c917e0f2 | 771 | struct cgroup_subsys_state *css; |
e5d1367f | 772 | |
3f7cce3c SE |
773 | /* |
774 | * ctx->lock held by caller | |
775 | * ensure we do not access cgroup data | |
776 | * unless we have the cgroup pinned (css_get) | |
777 | */ | |
778 | if (!task || !ctx->nr_cgroups) | |
e5d1367f SE |
779 | return; |
780 | ||
614e4c4e | 781 | cgrp = perf_cgroup_from_task(task, ctx); |
c917e0f2 SL |
782 | |
783 | for (css = &cgrp->css; css; css = css->parent) { | |
784 | cgrp = container_of(css, struct perf_cgroup, css); | |
785 | info = this_cpu_ptr(cgrp->info); | |
786 | info->timestamp = ctx->timestamp; | |
787 | } | |
e5d1367f SE |
788 | } |
789 | ||
058fe1c0 DCC |
790 | static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list); |
791 | ||
e5d1367f SE |
792 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
793 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ | |
794 | ||
795 | /* | |
796 | * reschedule events based on the cgroup constraint of task. | |
797 | * | |
798 | * mode SWOUT : schedule out everything | |
799 | * mode SWIN : schedule in based on cgroup for next | |
800 | */ | |
18ab2cd3 | 801 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
e5d1367f SE |
802 | { |
803 | struct perf_cpu_context *cpuctx; | |
058fe1c0 | 804 | struct list_head *list; |
e5d1367f SE |
805 | unsigned long flags; |
806 | ||
807 | /* | |
058fe1c0 DCC |
808 | * Disable interrupts and preemption to avoid this CPU's |
809 | * cgrp_cpuctx_entry to change under us. | |
e5d1367f SE |
810 | */ |
811 | local_irq_save(flags); | |
812 | ||
058fe1c0 DCC |
813 | list = this_cpu_ptr(&cgrp_cpuctx_list); |
814 | list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) { | |
815 | WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); | |
e5d1367f | 816 | |
058fe1c0 DCC |
817 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
818 | perf_pmu_disable(cpuctx->ctx.pmu); | |
e5d1367f | 819 | |
058fe1c0 DCC |
820 | if (mode & PERF_CGROUP_SWOUT) { |
821 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); | |
822 | /* | |
823 | * must not be done before ctxswout due | |
824 | * to event_filter_match() in event_sched_out() | |
825 | */ | |
826 | cpuctx->cgrp = NULL; | |
827 | } | |
e5d1367f | 828 | |
058fe1c0 DCC |
829 | if (mode & PERF_CGROUP_SWIN) { |
830 | WARN_ON_ONCE(cpuctx->cgrp); | |
831 | /* | |
832 | * set cgrp before ctxsw in to allow | |
833 | * event_filter_match() to not have to pass | |
834 | * task around | |
835 | * we pass the cpuctx->ctx to perf_cgroup_from_task() | |
836 | * because cgorup events are only per-cpu | |
837 | */ | |
838 | cpuctx->cgrp = perf_cgroup_from_task(task, | |
839 | &cpuctx->ctx); | |
840 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); | |
e5d1367f | 841 | } |
058fe1c0 DCC |
842 | perf_pmu_enable(cpuctx->ctx.pmu); |
843 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
e5d1367f SE |
844 | } |
845 | ||
e5d1367f SE |
846 | local_irq_restore(flags); |
847 | } | |
848 | ||
a8d757ef SE |
849 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
850 | struct task_struct *next) | |
e5d1367f | 851 | { |
a8d757ef SE |
852 | struct perf_cgroup *cgrp1; |
853 | struct perf_cgroup *cgrp2 = NULL; | |
854 | ||
ddaaf4e2 | 855 | rcu_read_lock(); |
a8d757ef SE |
856 | /* |
857 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
858 | * we do not need to pass the ctx here because we know |
859 | * we are holding the rcu lock | |
a8d757ef | 860 | */ |
614e4c4e | 861 | cgrp1 = perf_cgroup_from_task(task, NULL); |
70a01657 | 862 | cgrp2 = perf_cgroup_from_task(next, NULL); |
a8d757ef SE |
863 | |
864 | /* | |
865 | * only schedule out current cgroup events if we know | |
866 | * that we are switching to a different cgroup. Otherwise, | |
867 | * do no touch the cgroup events. | |
868 | */ | |
869 | if (cgrp1 != cgrp2) | |
870 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); | |
ddaaf4e2 SE |
871 | |
872 | rcu_read_unlock(); | |
e5d1367f SE |
873 | } |
874 | ||
a8d757ef SE |
875 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
876 | struct task_struct *task) | |
e5d1367f | 877 | { |
a8d757ef SE |
878 | struct perf_cgroup *cgrp1; |
879 | struct perf_cgroup *cgrp2 = NULL; | |
880 | ||
ddaaf4e2 | 881 | rcu_read_lock(); |
a8d757ef SE |
882 | /* |
883 | * we come here when we know perf_cgroup_events > 0 | |
614e4c4e SE |
884 | * we do not need to pass the ctx here because we know |
885 | * we are holding the rcu lock | |
a8d757ef | 886 | */ |
614e4c4e | 887 | cgrp1 = perf_cgroup_from_task(task, NULL); |
614e4c4e | 888 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
a8d757ef SE |
889 | |
890 | /* | |
891 | * only need to schedule in cgroup events if we are changing | |
892 | * cgroup during ctxsw. Cgroup events were not scheduled | |
893 | * out of ctxsw out if that was not the case. | |
894 | */ | |
895 | if (cgrp1 != cgrp2) | |
896 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); | |
ddaaf4e2 SE |
897 | |
898 | rcu_read_unlock(); | |
e5d1367f SE |
899 | } |
900 | ||
c2283c93 IR |
901 | static int perf_cgroup_ensure_storage(struct perf_event *event, |
902 | struct cgroup_subsys_state *css) | |
903 | { | |
904 | struct perf_cpu_context *cpuctx; | |
905 | struct perf_event **storage; | |
906 | int cpu, heap_size, ret = 0; | |
907 | ||
908 | /* | |
909 | * Allow storage to have sufficent space for an iterator for each | |
910 | * possibly nested cgroup plus an iterator for events with no cgroup. | |
911 | */ | |
912 | for (heap_size = 1; css; css = css->parent) | |
913 | heap_size++; | |
914 | ||
915 | for_each_possible_cpu(cpu) { | |
916 | cpuctx = per_cpu_ptr(event->pmu->pmu_cpu_context, cpu); | |
917 | if (heap_size <= cpuctx->heap_size) | |
918 | continue; | |
919 | ||
920 | storage = kmalloc_node(heap_size * sizeof(struct perf_event *), | |
921 | GFP_KERNEL, cpu_to_node(cpu)); | |
922 | if (!storage) { | |
923 | ret = -ENOMEM; | |
924 | break; | |
925 | } | |
926 | ||
927 | raw_spin_lock_irq(&cpuctx->ctx.lock); | |
928 | if (cpuctx->heap_size < heap_size) { | |
929 | swap(cpuctx->heap, storage); | |
930 | if (storage == cpuctx->heap_default) | |
931 | storage = NULL; | |
932 | cpuctx->heap_size = heap_size; | |
933 | } | |
934 | raw_spin_unlock_irq(&cpuctx->ctx.lock); | |
935 | ||
936 | kfree(storage); | |
937 | } | |
938 | ||
939 | return ret; | |
940 | } | |
941 | ||
e5d1367f SE |
942 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
943 | struct perf_event_attr *attr, | |
944 | struct perf_event *group_leader) | |
945 | { | |
946 | struct perf_cgroup *cgrp; | |
947 | struct cgroup_subsys_state *css; | |
2903ff01 AV |
948 | struct fd f = fdget(fd); |
949 | int ret = 0; | |
e5d1367f | 950 | |
2903ff01 | 951 | if (!f.file) |
e5d1367f SE |
952 | return -EBADF; |
953 | ||
b583043e | 954 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
ec903c0c | 955 | &perf_event_cgrp_subsys); |
3db272c0 LZ |
956 | if (IS_ERR(css)) { |
957 | ret = PTR_ERR(css); | |
958 | goto out; | |
959 | } | |
e5d1367f | 960 | |
c2283c93 IR |
961 | ret = perf_cgroup_ensure_storage(event, css); |
962 | if (ret) | |
963 | goto out; | |
964 | ||
e5d1367f SE |
965 | cgrp = container_of(css, struct perf_cgroup, css); |
966 | event->cgrp = cgrp; | |
967 | ||
968 | /* | |
969 | * all events in a group must monitor | |
970 | * the same cgroup because a task belongs | |
971 | * to only one perf cgroup at a time | |
972 | */ | |
973 | if (group_leader && group_leader->cgrp != cgrp) { | |
974 | perf_detach_cgroup(event); | |
975 | ret = -EINVAL; | |
e5d1367f | 976 | } |
3db272c0 | 977 | out: |
2903ff01 | 978 | fdput(f); |
e5d1367f SE |
979 | return ret; |
980 | } | |
981 | ||
982 | static inline void | |
983 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
984 | { | |
985 | struct perf_cgroup_info *t; | |
986 | t = per_cpu_ptr(event->cgrp->info, event->cpu); | |
987 | event->shadow_ctx_time = now - t->timestamp; | |
988 | } | |
989 | ||
db4a8356 | 990 | static inline void |
33238c50 | 991 | perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) |
db4a8356 DCC |
992 | { |
993 | struct perf_cpu_context *cpuctx; | |
994 | ||
995 | if (!is_cgroup_event(event)) | |
996 | return; | |
997 | ||
db4a8356 DCC |
998 | /* |
999 | * Because cgroup events are always per-cpu events, | |
07c59729 | 1000 | * @ctx == &cpuctx->ctx. |
db4a8356 | 1001 | */ |
07c59729 | 1002 | cpuctx = container_of(ctx, struct perf_cpu_context, ctx); |
33801b94 | 1003 | |
1004 | /* | |
1005 | * Since setting cpuctx->cgrp is conditional on the current @cgrp | |
1006 | * matching the event's cgroup, we must do this for every new event, | |
1007 | * because if the first would mismatch, the second would not try again | |
1008 | * and we would leave cpuctx->cgrp unset. | |
1009 | */ | |
33238c50 | 1010 | if (ctx->is_active && !cpuctx->cgrp) { |
be96b316 TH |
1011 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); |
1012 | ||
be96b316 TH |
1013 | if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) |
1014 | cpuctx->cgrp = cgrp; | |
058fe1c0 | 1015 | } |
33801b94 | 1016 | |
33238c50 | 1017 | if (ctx->nr_cgroups++) |
33801b94 | 1018 | return; |
33238c50 PZ |
1019 | |
1020 | list_add(&cpuctx->cgrp_cpuctx_entry, | |
1021 | per_cpu_ptr(&cgrp_cpuctx_list, event->cpu)); | |
1022 | } | |
1023 | ||
1024 | static inline void | |
1025 | perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) | |
1026 | { | |
1027 | struct perf_cpu_context *cpuctx; | |
1028 | ||
1029 | if (!is_cgroup_event(event)) | |
33801b94 | 1030 | return; |
1031 | ||
33238c50 PZ |
1032 | /* |
1033 | * Because cgroup events are always per-cpu events, | |
1034 | * @ctx == &cpuctx->ctx. | |
1035 | */ | |
1036 | cpuctx = container_of(ctx, struct perf_cpu_context, ctx); | |
1037 | ||
1038 | if (--ctx->nr_cgroups) | |
1039 | return; | |
1040 | ||
1041 | if (ctx->is_active && cpuctx->cgrp) | |
33801b94 | 1042 | cpuctx->cgrp = NULL; |
1043 | ||
33238c50 | 1044 | list_del(&cpuctx->cgrp_cpuctx_entry); |
db4a8356 DCC |
1045 | } |
1046 | ||
e5d1367f SE |
1047 | #else /* !CONFIG_CGROUP_PERF */ |
1048 | ||
1049 | static inline bool | |
1050 | perf_cgroup_match(struct perf_event *event) | |
1051 | { | |
1052 | return true; | |
1053 | } | |
1054 | ||
1055 | static inline void perf_detach_cgroup(struct perf_event *event) | |
1056 | {} | |
1057 | ||
1058 | static inline int is_cgroup_event(struct perf_event *event) | |
1059 | { | |
1060 | return 0; | |
1061 | } | |
1062 | ||
e5d1367f SE |
1063 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
1064 | { | |
1065 | } | |
1066 | ||
1067 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) | |
1068 | { | |
1069 | } | |
1070 | ||
a8d757ef SE |
1071 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
1072 | struct task_struct *next) | |
e5d1367f SE |
1073 | { |
1074 | } | |
1075 | ||
a8d757ef SE |
1076 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
1077 | struct task_struct *task) | |
e5d1367f SE |
1078 | { |
1079 | } | |
1080 | ||
1081 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, | |
1082 | struct perf_event_attr *attr, | |
1083 | struct perf_event *group_leader) | |
1084 | { | |
1085 | return -EINVAL; | |
1086 | } | |
1087 | ||
1088 | static inline void | |
3f7cce3c SE |
1089 | perf_cgroup_set_timestamp(struct task_struct *task, |
1090 | struct perf_event_context *ctx) | |
e5d1367f SE |
1091 | { |
1092 | } | |
1093 | ||
d00dbd29 | 1094 | static inline void |
e5d1367f SE |
1095 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
1096 | { | |
1097 | } | |
1098 | ||
1099 | static inline void | |
1100 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) | |
1101 | { | |
1102 | } | |
1103 | ||
1104 | static inline u64 perf_cgroup_event_time(struct perf_event *event) | |
1105 | { | |
1106 | return 0; | |
1107 | } | |
1108 | ||
db4a8356 | 1109 | static inline void |
33238c50 | 1110 | perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) |
db4a8356 DCC |
1111 | { |
1112 | } | |
1113 | ||
33238c50 PZ |
1114 | static inline void |
1115 | perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) | |
1116 | { | |
1117 | } | |
e5d1367f SE |
1118 | #endif |
1119 | ||
9e630205 SE |
1120 | /* |
1121 | * set default to be dependent on timer tick just | |
1122 | * like original code | |
1123 | */ | |
1124 | #define PERF_CPU_HRTIMER (1000 / HZ) | |
1125 | /* | |
8a1115ff | 1126 | * function must be called with interrupts disabled |
9e630205 | 1127 | */ |
272325c4 | 1128 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
9e630205 SE |
1129 | { |
1130 | struct perf_cpu_context *cpuctx; | |
8d5bce0c | 1131 | bool rotations; |
9e630205 | 1132 | |
16444645 | 1133 | lockdep_assert_irqs_disabled(); |
9e630205 SE |
1134 | |
1135 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); | |
9e630205 SE |
1136 | rotations = perf_rotate_context(cpuctx); |
1137 | ||
4cfafd30 PZ |
1138 | raw_spin_lock(&cpuctx->hrtimer_lock); |
1139 | if (rotations) | |
9e630205 | 1140 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
4cfafd30 PZ |
1141 | else |
1142 | cpuctx->hrtimer_active = 0; | |
1143 | raw_spin_unlock(&cpuctx->hrtimer_lock); | |
9e630205 | 1144 | |
4cfafd30 | 1145 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
9e630205 SE |
1146 | } |
1147 | ||
272325c4 | 1148 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
9e630205 | 1149 | { |
272325c4 | 1150 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1151 | struct pmu *pmu = cpuctx->ctx.pmu; |
272325c4 | 1152 | u64 interval; |
9e630205 SE |
1153 | |
1154 | /* no multiplexing needed for SW PMU */ | |
1155 | if (pmu->task_ctx_nr == perf_sw_context) | |
1156 | return; | |
1157 | ||
62b85639 SE |
1158 | /* |
1159 | * check default is sane, if not set then force to | |
1160 | * default interval (1/tick) | |
1161 | */ | |
272325c4 PZ |
1162 | interval = pmu->hrtimer_interval_ms; |
1163 | if (interval < 1) | |
1164 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; | |
62b85639 | 1165 | |
272325c4 | 1166 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
9e630205 | 1167 | |
4cfafd30 | 1168 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
30f9028b | 1169 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); |
272325c4 | 1170 | timer->function = perf_mux_hrtimer_handler; |
9e630205 SE |
1171 | } |
1172 | ||
272325c4 | 1173 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
9e630205 | 1174 | { |
272325c4 | 1175 | struct hrtimer *timer = &cpuctx->hrtimer; |
9e630205 | 1176 | struct pmu *pmu = cpuctx->ctx.pmu; |
4cfafd30 | 1177 | unsigned long flags; |
9e630205 SE |
1178 | |
1179 | /* not for SW PMU */ | |
1180 | if (pmu->task_ctx_nr == perf_sw_context) | |
272325c4 | 1181 | return 0; |
9e630205 | 1182 | |
4cfafd30 PZ |
1183 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
1184 | if (!cpuctx->hrtimer_active) { | |
1185 | cpuctx->hrtimer_active = 1; | |
1186 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); | |
30f9028b | 1187 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); |
4cfafd30 PZ |
1188 | } |
1189 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); | |
9e630205 | 1190 | |
272325c4 | 1191 | return 0; |
9e630205 SE |
1192 | } |
1193 | ||
33696fc0 | 1194 | void perf_pmu_disable(struct pmu *pmu) |
9e35ad38 | 1195 | { |
33696fc0 PZ |
1196 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1197 | if (!(*count)++) | |
1198 | pmu->pmu_disable(pmu); | |
9e35ad38 | 1199 | } |
9e35ad38 | 1200 | |
33696fc0 | 1201 | void perf_pmu_enable(struct pmu *pmu) |
9e35ad38 | 1202 | { |
33696fc0 PZ |
1203 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
1204 | if (!--(*count)) | |
1205 | pmu->pmu_enable(pmu); | |
9e35ad38 | 1206 | } |
9e35ad38 | 1207 | |
2fde4f94 | 1208 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
e9d2b064 PZ |
1209 | |
1210 | /* | |
2fde4f94 MR |
1211 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
1212 | * perf_event_task_tick() are fully serialized because they're strictly cpu | |
1213 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs | |
1214 | * disabled, while perf_event_task_tick is called from IRQ context. | |
e9d2b064 | 1215 | */ |
2fde4f94 | 1216 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
9e35ad38 | 1217 | { |
2fde4f94 | 1218 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
b5ab4cd5 | 1219 | |
16444645 | 1220 | lockdep_assert_irqs_disabled(); |
b5ab4cd5 | 1221 | |
2fde4f94 MR |
1222 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
1223 | ||
1224 | list_add(&ctx->active_ctx_list, head); | |
1225 | } | |
1226 | ||
1227 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) | |
1228 | { | |
16444645 | 1229 | lockdep_assert_irqs_disabled(); |
2fde4f94 MR |
1230 | |
1231 | WARN_ON(list_empty(&ctx->active_ctx_list)); | |
1232 | ||
1233 | list_del_init(&ctx->active_ctx_list); | |
9e35ad38 | 1234 | } |
9e35ad38 | 1235 | |
cdd6c482 | 1236 | static void get_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1237 | { |
8c94abbb | 1238 | refcount_inc(&ctx->refcount); |
a63eaf34 PM |
1239 | } |
1240 | ||
ff9ff926 KL |
1241 | static void *alloc_task_ctx_data(struct pmu *pmu) |
1242 | { | |
217c2a63 KL |
1243 | if (pmu->task_ctx_cache) |
1244 | return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL); | |
1245 | ||
ff9ff926 KL |
1246 | return kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
1247 | } | |
1248 | ||
1249 | static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data) | |
1250 | { | |
217c2a63 KL |
1251 | if (pmu->task_ctx_cache && task_ctx_data) |
1252 | kmem_cache_free(pmu->task_ctx_cache, task_ctx_data); | |
1253 | else | |
1254 | kfree(task_ctx_data); | |
ff9ff926 KL |
1255 | } |
1256 | ||
4af57ef2 YZ |
1257 | static void free_ctx(struct rcu_head *head) |
1258 | { | |
1259 | struct perf_event_context *ctx; | |
1260 | ||
1261 | ctx = container_of(head, struct perf_event_context, rcu_head); | |
ff9ff926 | 1262 | free_task_ctx_data(ctx->pmu, ctx->task_ctx_data); |
4af57ef2 YZ |
1263 | kfree(ctx); |
1264 | } | |
1265 | ||
cdd6c482 | 1266 | static void put_ctx(struct perf_event_context *ctx) |
a63eaf34 | 1267 | { |
8c94abbb | 1268 | if (refcount_dec_and_test(&ctx->refcount)) { |
564c2b21 PM |
1269 | if (ctx->parent_ctx) |
1270 | put_ctx(ctx->parent_ctx); | |
63b6da39 | 1271 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
c93f7669 | 1272 | put_task_struct(ctx->task); |
4af57ef2 | 1273 | call_rcu(&ctx->rcu_head, free_ctx); |
564c2b21 | 1274 | } |
a63eaf34 PM |
1275 | } |
1276 | ||
f63a8daa PZ |
1277 | /* |
1278 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and | |
1279 | * perf_pmu_migrate_context() we need some magic. | |
1280 | * | |
1281 | * Those places that change perf_event::ctx will hold both | |
1282 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. | |
1283 | * | |
8b10c5e2 PZ |
1284 | * Lock ordering is by mutex address. There are two other sites where |
1285 | * perf_event_context::mutex nests and those are: | |
1286 | * | |
1287 | * - perf_event_exit_task_context() [ child , 0 ] | |
8ba289b8 PZ |
1288 | * perf_event_exit_event() |
1289 | * put_event() [ parent, 1 ] | |
8b10c5e2 PZ |
1290 | * |
1291 | * - perf_event_init_context() [ parent, 0 ] | |
1292 | * inherit_task_group() | |
1293 | * inherit_group() | |
1294 | * inherit_event() | |
1295 | * perf_event_alloc() | |
1296 | * perf_init_event() | |
1297 | * perf_try_init_event() [ child , 1 ] | |
1298 | * | |
1299 | * While it appears there is an obvious deadlock here -- the parent and child | |
1300 | * nesting levels are inverted between the two. This is in fact safe because | |
1301 | * life-time rules separate them. That is an exiting task cannot fork, and a | |
1302 | * spawning task cannot (yet) exit. | |
1303 | * | |
1304 | * But remember that that these are parent<->child context relations, and | |
1305 | * migration does not affect children, therefore these two orderings should not | |
1306 | * interact. | |
f63a8daa PZ |
1307 | * |
1308 | * The change in perf_event::ctx does not affect children (as claimed above) | |
1309 | * because the sys_perf_event_open() case will install a new event and break | |
1310 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only | |
1311 | * concerned with cpuctx and that doesn't have children. | |
1312 | * | |
1313 | * The places that change perf_event::ctx will issue: | |
1314 | * | |
1315 | * perf_remove_from_context(); | |
1316 | * synchronize_rcu(); | |
1317 | * perf_install_in_context(); | |
1318 | * | |
1319 | * to affect the change. The remove_from_context() + synchronize_rcu() should | |
1320 | * quiesce the event, after which we can install it in the new location. This | |
1321 | * means that only external vectors (perf_fops, prctl) can perturb the event | |
1322 | * while in transit. Therefore all such accessors should also acquire | |
1323 | * perf_event_context::mutex to serialize against this. | |
1324 | * | |
1325 | * However; because event->ctx can change while we're waiting to acquire | |
1326 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() | |
1327 | * function. | |
1328 | * | |
1329 | * Lock order: | |
69143038 | 1330 | * exec_update_mutex |
f63a8daa PZ |
1331 | * task_struct::perf_event_mutex |
1332 | * perf_event_context::mutex | |
f63a8daa | 1333 | * perf_event::child_mutex; |
07c4a776 | 1334 | * perf_event_context::lock |
f63a8daa | 1335 | * perf_event::mmap_mutex |
c1e8d7c6 | 1336 | * mmap_lock |
18736eef | 1337 | * perf_addr_filters_head::lock |
82d94856 PZ |
1338 | * |
1339 | * cpu_hotplug_lock | |
1340 | * pmus_lock | |
1341 | * cpuctx->mutex / perf_event_context::mutex | |
f63a8daa | 1342 | */ |
a83fe28e PZ |
1343 | static struct perf_event_context * |
1344 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) | |
f63a8daa PZ |
1345 | { |
1346 | struct perf_event_context *ctx; | |
1347 | ||
1348 | again: | |
1349 | rcu_read_lock(); | |
6aa7de05 | 1350 | ctx = READ_ONCE(event->ctx); |
8c94abbb | 1351 | if (!refcount_inc_not_zero(&ctx->refcount)) { |
f63a8daa PZ |
1352 | rcu_read_unlock(); |
1353 | goto again; | |
1354 | } | |
1355 | rcu_read_unlock(); | |
1356 | ||
a83fe28e | 1357 | mutex_lock_nested(&ctx->mutex, nesting); |
f63a8daa PZ |
1358 | if (event->ctx != ctx) { |
1359 | mutex_unlock(&ctx->mutex); | |
1360 | put_ctx(ctx); | |
1361 | goto again; | |
1362 | } | |
1363 | ||
1364 | return ctx; | |
1365 | } | |
1366 | ||
a83fe28e PZ |
1367 | static inline struct perf_event_context * |
1368 | perf_event_ctx_lock(struct perf_event *event) | |
1369 | { | |
1370 | return perf_event_ctx_lock_nested(event, 0); | |
1371 | } | |
1372 | ||
f63a8daa PZ |
1373 | static void perf_event_ctx_unlock(struct perf_event *event, |
1374 | struct perf_event_context *ctx) | |
1375 | { | |
1376 | mutex_unlock(&ctx->mutex); | |
1377 | put_ctx(ctx); | |
1378 | } | |
1379 | ||
211de6eb PZ |
1380 | /* |
1381 | * This must be done under the ctx->lock, such as to serialize against | |
1382 | * context_equiv(), therefore we cannot call put_ctx() since that might end up | |
1383 | * calling scheduler related locks and ctx->lock nests inside those. | |
1384 | */ | |
1385 | static __must_check struct perf_event_context * | |
1386 | unclone_ctx(struct perf_event_context *ctx) | |
71a851b4 | 1387 | { |
211de6eb PZ |
1388 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
1389 | ||
1390 | lockdep_assert_held(&ctx->lock); | |
1391 | ||
1392 | if (parent_ctx) | |
71a851b4 | 1393 | ctx->parent_ctx = NULL; |
5a3126d4 | 1394 | ctx->generation++; |
211de6eb PZ |
1395 | |
1396 | return parent_ctx; | |
71a851b4 PZ |
1397 | } |
1398 | ||
1d953111 ON |
1399 | static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, |
1400 | enum pid_type type) | |
6844c09d | 1401 | { |
1d953111 | 1402 | u32 nr; |
6844c09d ACM |
1403 | /* |
1404 | * only top level events have the pid namespace they were created in | |
1405 | */ | |
1406 | if (event->parent) | |
1407 | event = event->parent; | |
1408 | ||
1d953111 ON |
1409 | nr = __task_pid_nr_ns(p, type, event->ns); |
1410 | /* avoid -1 if it is idle thread or runs in another ns */ | |
1411 | if (!nr && !pid_alive(p)) | |
1412 | nr = -1; | |
1413 | return nr; | |
6844c09d ACM |
1414 | } |
1415 | ||
1d953111 | 1416 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
6844c09d | 1417 | { |
6883f81a | 1418 | return perf_event_pid_type(event, p, PIDTYPE_TGID); |
1d953111 | 1419 | } |
6844c09d | 1420 | |
1d953111 ON |
1421 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
1422 | { | |
1423 | return perf_event_pid_type(event, p, PIDTYPE_PID); | |
6844c09d ACM |
1424 | } |
1425 | ||
7f453c24 | 1426 | /* |
cdd6c482 | 1427 | * If we inherit events we want to return the parent event id |
7f453c24 PZ |
1428 | * to userspace. |
1429 | */ | |
cdd6c482 | 1430 | static u64 primary_event_id(struct perf_event *event) |
7f453c24 | 1431 | { |
cdd6c482 | 1432 | u64 id = event->id; |
7f453c24 | 1433 | |
cdd6c482 IM |
1434 | if (event->parent) |
1435 | id = event->parent->id; | |
7f453c24 PZ |
1436 | |
1437 | return id; | |
1438 | } | |
1439 | ||
25346b93 | 1440 | /* |
cdd6c482 | 1441 | * Get the perf_event_context for a task and lock it. |
63b6da39 | 1442 | * |
25346b93 PM |
1443 | * This has to cope with with the fact that until it is locked, |
1444 | * the context could get moved to another task. | |
1445 | */ | |
cdd6c482 | 1446 | static struct perf_event_context * |
8dc85d54 | 1447 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
25346b93 | 1448 | { |
cdd6c482 | 1449 | struct perf_event_context *ctx; |
25346b93 | 1450 | |
9ed6060d | 1451 | retry: |
058ebd0e PZ |
1452 | /* |
1453 | * One of the few rules of preemptible RCU is that one cannot do | |
1454 | * rcu_read_unlock() while holding a scheduler (or nested) lock when | |
2fd59077 | 1455 | * part of the read side critical section was irqs-enabled -- see |
058ebd0e PZ |
1456 | * rcu_read_unlock_special(). |
1457 | * | |
1458 | * Since ctx->lock nests under rq->lock we must ensure the entire read | |
2fd59077 | 1459 | * side critical section has interrupts disabled. |
058ebd0e | 1460 | */ |
2fd59077 | 1461 | local_irq_save(*flags); |
058ebd0e | 1462 | rcu_read_lock(); |
8dc85d54 | 1463 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
25346b93 PM |
1464 | if (ctx) { |
1465 | /* | |
1466 | * If this context is a clone of another, it might | |
1467 | * get swapped for another underneath us by | |
cdd6c482 | 1468 | * perf_event_task_sched_out, though the |
25346b93 PM |
1469 | * rcu_read_lock() protects us from any context |
1470 | * getting freed. Lock the context and check if it | |
1471 | * got swapped before we could get the lock, and retry | |
1472 | * if so. If we locked the right context, then it | |
1473 | * can't get swapped on us any more. | |
1474 | */ | |
2fd59077 | 1475 | raw_spin_lock(&ctx->lock); |
8dc85d54 | 1476 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
2fd59077 | 1477 | raw_spin_unlock(&ctx->lock); |
058ebd0e | 1478 | rcu_read_unlock(); |
2fd59077 | 1479 | local_irq_restore(*flags); |
25346b93 PM |
1480 | goto retry; |
1481 | } | |
b49a9e7e | 1482 | |
63b6da39 | 1483 | if (ctx->task == TASK_TOMBSTONE || |
8c94abbb | 1484 | !refcount_inc_not_zero(&ctx->refcount)) { |
2fd59077 | 1485 | raw_spin_unlock(&ctx->lock); |
b49a9e7e | 1486 | ctx = NULL; |
828b6f0e PZ |
1487 | } else { |
1488 | WARN_ON_ONCE(ctx->task != task); | |
b49a9e7e | 1489 | } |
25346b93 PM |
1490 | } |
1491 | rcu_read_unlock(); | |
2fd59077 PM |
1492 | if (!ctx) |
1493 | local_irq_restore(*flags); | |
25346b93 PM |
1494 | return ctx; |
1495 | } | |
1496 | ||
1497 | /* | |
1498 | * Get the context for a task and increment its pin_count so it | |
1499 | * can't get swapped to another task. This also increments its | |
1500 | * reference count so that the context can't get freed. | |
1501 | */ | |
8dc85d54 PZ |
1502 | static struct perf_event_context * |
1503 | perf_pin_task_context(struct task_struct *task, int ctxn) | |
25346b93 | 1504 | { |
cdd6c482 | 1505 | struct perf_event_context *ctx; |
25346b93 PM |
1506 | unsigned long flags; |
1507 | ||
8dc85d54 | 1508 | ctx = perf_lock_task_context(task, ctxn, &flags); |
25346b93 PM |
1509 | if (ctx) { |
1510 | ++ctx->pin_count; | |
e625cce1 | 1511 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1512 | } |
1513 | return ctx; | |
1514 | } | |
1515 | ||
cdd6c482 | 1516 | static void perf_unpin_context(struct perf_event_context *ctx) |
25346b93 PM |
1517 | { |
1518 | unsigned long flags; | |
1519 | ||
e625cce1 | 1520 | raw_spin_lock_irqsave(&ctx->lock, flags); |
25346b93 | 1521 | --ctx->pin_count; |
e625cce1 | 1522 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
25346b93 PM |
1523 | } |
1524 | ||
f67218c3 PZ |
1525 | /* |
1526 | * Update the record of the current time in a context. | |
1527 | */ | |
1528 | static void update_context_time(struct perf_event_context *ctx) | |
1529 | { | |
1530 | u64 now = perf_clock(); | |
1531 | ||
1532 | ctx->time += now - ctx->timestamp; | |
1533 | ctx->timestamp = now; | |
1534 | } | |
1535 | ||
4158755d SE |
1536 | static u64 perf_event_time(struct perf_event *event) |
1537 | { | |
1538 | struct perf_event_context *ctx = event->ctx; | |
e5d1367f SE |
1539 | |
1540 | if (is_cgroup_event(event)) | |
1541 | return perf_cgroup_event_time(event); | |
1542 | ||
4158755d SE |
1543 | return ctx ? ctx->time : 0; |
1544 | } | |
1545 | ||
487f05e1 AS |
1546 | static enum event_type_t get_event_type(struct perf_event *event) |
1547 | { | |
1548 | struct perf_event_context *ctx = event->ctx; | |
1549 | enum event_type_t event_type; | |
1550 | ||
1551 | lockdep_assert_held(&ctx->lock); | |
1552 | ||
3bda69c1 AS |
1553 | /* |
1554 | * It's 'group type', really, because if our group leader is | |
1555 | * pinned, so are we. | |
1556 | */ | |
1557 | if (event->group_leader != event) | |
1558 | event = event->group_leader; | |
1559 | ||
487f05e1 AS |
1560 | event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; |
1561 | if (!ctx->task) | |
1562 | event_type |= EVENT_CPU; | |
1563 | ||
1564 | return event_type; | |
1565 | } | |
1566 | ||
8e1a2031 | 1567 | /* |
161c85fa | 1568 | * Helper function to initialize event group nodes. |
8e1a2031 | 1569 | */ |
161c85fa | 1570 | static void init_event_group(struct perf_event *event) |
8e1a2031 AB |
1571 | { |
1572 | RB_CLEAR_NODE(&event->group_node); | |
1573 | event->group_index = 0; | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * Extract pinned or flexible groups from the context | |
161c85fa | 1578 | * based on event attrs bits. |
8e1a2031 AB |
1579 | */ |
1580 | static struct perf_event_groups * | |
1581 | get_event_groups(struct perf_event *event, struct perf_event_context *ctx) | |
889ff015 FW |
1582 | { |
1583 | if (event->attr.pinned) | |
1584 | return &ctx->pinned_groups; | |
1585 | else | |
1586 | return &ctx->flexible_groups; | |
1587 | } | |
1588 | ||
8e1a2031 | 1589 | /* |
161c85fa | 1590 | * Helper function to initializes perf_event_group trees. |
8e1a2031 | 1591 | */ |
161c85fa | 1592 | static void perf_event_groups_init(struct perf_event_groups *groups) |
8e1a2031 AB |
1593 | { |
1594 | groups->tree = RB_ROOT; | |
1595 | groups->index = 0; | |
1596 | } | |
1597 | ||
1598 | /* | |
1599 | * Compare function for event groups; | |
161c85fa PZ |
1600 | * |
1601 | * Implements complex key that first sorts by CPU and then by virtual index | |
1602 | * which provides ordering when rotating groups for the same CPU. | |
8e1a2031 | 1603 | */ |
161c85fa PZ |
1604 | static bool |
1605 | perf_event_groups_less(struct perf_event *left, struct perf_event *right) | |
8e1a2031 | 1606 | { |
161c85fa PZ |
1607 | if (left->cpu < right->cpu) |
1608 | return true; | |
1609 | if (left->cpu > right->cpu) | |
1610 | return false; | |
1611 | ||
95ed6c70 IR |
1612 | #ifdef CONFIG_CGROUP_PERF |
1613 | if (left->cgrp != right->cgrp) { | |
1614 | if (!left->cgrp || !left->cgrp->css.cgroup) { | |
1615 | /* | |
1616 | * Left has no cgroup but right does, no cgroups come | |
1617 | * first. | |
1618 | */ | |
1619 | return true; | |
1620 | } | |
a6763625 | 1621 | if (!right->cgrp || !right->cgrp->css.cgroup) { |
95ed6c70 IR |
1622 | /* |
1623 | * Right has no cgroup but left does, no cgroups come | |
1624 | * first. | |
1625 | */ | |
1626 | return false; | |
1627 | } | |
1628 | /* Two dissimilar cgroups, order by id. */ | |
1629 | if (left->cgrp->css.cgroup->kn->id < right->cgrp->css.cgroup->kn->id) | |
1630 | return true; | |
1631 | ||
1632 | return false; | |
1633 | } | |
1634 | #endif | |
1635 | ||
161c85fa PZ |
1636 | if (left->group_index < right->group_index) |
1637 | return true; | |
1638 | if (left->group_index > right->group_index) | |
1639 | return false; | |
1640 | ||
1641 | return false; | |
8e1a2031 AB |
1642 | } |
1643 | ||
1644 | /* | |
161c85fa PZ |
1645 | * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for |
1646 | * key (see perf_event_groups_less). This places it last inside the CPU | |
1647 | * subtree. | |
8e1a2031 AB |
1648 | */ |
1649 | static void | |
1650 | perf_event_groups_insert(struct perf_event_groups *groups, | |
161c85fa | 1651 | struct perf_event *event) |
8e1a2031 AB |
1652 | { |
1653 | struct perf_event *node_event; | |
1654 | struct rb_node *parent; | |
1655 | struct rb_node **node; | |
1656 | ||
1657 | event->group_index = ++groups->index; | |
1658 | ||
1659 | node = &groups->tree.rb_node; | |
1660 | parent = *node; | |
1661 | ||
1662 | while (*node) { | |
1663 | parent = *node; | |
161c85fa | 1664 | node_event = container_of(*node, struct perf_event, group_node); |
8e1a2031 AB |
1665 | |
1666 | if (perf_event_groups_less(event, node_event)) | |
1667 | node = &parent->rb_left; | |
1668 | else | |
1669 | node = &parent->rb_right; | |
1670 | } | |
1671 | ||
1672 | rb_link_node(&event->group_node, parent, node); | |
1673 | rb_insert_color(&event->group_node, &groups->tree); | |
1674 | } | |
1675 | ||
1676 | /* | |
161c85fa | 1677 | * Helper function to insert event into the pinned or flexible groups. |
8e1a2031 AB |
1678 | */ |
1679 | static void | |
1680 | add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1681 | { | |
1682 | struct perf_event_groups *groups; | |
1683 | ||
1684 | groups = get_event_groups(event, ctx); | |
1685 | perf_event_groups_insert(groups, event); | |
1686 | } | |
1687 | ||
1688 | /* | |
161c85fa | 1689 | * Delete a group from a tree. |
8e1a2031 AB |
1690 | */ |
1691 | static void | |
1692 | perf_event_groups_delete(struct perf_event_groups *groups, | |
161c85fa | 1693 | struct perf_event *event) |
8e1a2031 | 1694 | { |
161c85fa PZ |
1695 | WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) || |
1696 | RB_EMPTY_ROOT(&groups->tree)); | |
8e1a2031 | 1697 | |
161c85fa | 1698 | rb_erase(&event->group_node, &groups->tree); |
8e1a2031 AB |
1699 | init_event_group(event); |
1700 | } | |
1701 | ||
1702 | /* | |
161c85fa | 1703 | * Helper function to delete event from its groups. |
8e1a2031 AB |
1704 | */ |
1705 | static void | |
1706 | del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx) | |
1707 | { | |
1708 | struct perf_event_groups *groups; | |
1709 | ||
1710 | groups = get_event_groups(event, ctx); | |
1711 | perf_event_groups_delete(groups, event); | |
1712 | } | |
1713 | ||
1714 | /* | |
95ed6c70 | 1715 | * Get the leftmost event in the cpu/cgroup subtree. |
8e1a2031 AB |
1716 | */ |
1717 | static struct perf_event * | |
95ed6c70 IR |
1718 | perf_event_groups_first(struct perf_event_groups *groups, int cpu, |
1719 | struct cgroup *cgrp) | |
8e1a2031 AB |
1720 | { |
1721 | struct perf_event *node_event = NULL, *match = NULL; | |
1722 | struct rb_node *node = groups->tree.rb_node; | |
95ed6c70 IR |
1723 | #ifdef CONFIG_CGROUP_PERF |
1724 | u64 node_cgrp_id, cgrp_id = 0; | |
1725 | ||
1726 | if (cgrp) | |
1727 | cgrp_id = cgrp->kn->id; | |
1728 | #endif | |
8e1a2031 AB |
1729 | |
1730 | while (node) { | |
161c85fa | 1731 | node_event = container_of(node, struct perf_event, group_node); |
8e1a2031 AB |
1732 | |
1733 | if (cpu < node_event->cpu) { | |
1734 | node = node->rb_left; | |
95ed6c70 IR |
1735 | continue; |
1736 | } | |
1737 | if (cpu > node_event->cpu) { | |
8e1a2031 | 1738 | node = node->rb_right; |
95ed6c70 IR |
1739 | continue; |
1740 | } | |
1741 | #ifdef CONFIG_CGROUP_PERF | |
1742 | node_cgrp_id = 0; | |
1743 | if (node_event->cgrp && node_event->cgrp->css.cgroup) | |
1744 | node_cgrp_id = node_event->cgrp->css.cgroup->kn->id; | |
1745 | ||
1746 | if (cgrp_id < node_cgrp_id) { | |
8e1a2031 | 1747 | node = node->rb_left; |
95ed6c70 IR |
1748 | continue; |
1749 | } | |
1750 | if (cgrp_id > node_cgrp_id) { | |
1751 | node = node->rb_right; | |
1752 | continue; | |
8e1a2031 | 1753 | } |
95ed6c70 IR |
1754 | #endif |
1755 | match = node_event; | |
1756 | node = node->rb_left; | |
8e1a2031 AB |
1757 | } |
1758 | ||
1759 | return match; | |
1760 | } | |
1761 | ||
1cac7b1a PZ |
1762 | /* |
1763 | * Like rb_entry_next_safe() for the @cpu subtree. | |
1764 | */ | |
1765 | static struct perf_event * | |
1766 | perf_event_groups_next(struct perf_event *event) | |
1767 | { | |
1768 | struct perf_event *next; | |
95ed6c70 IR |
1769 | #ifdef CONFIG_CGROUP_PERF |
1770 | u64 curr_cgrp_id = 0; | |
1771 | u64 next_cgrp_id = 0; | |
1772 | #endif | |
1cac7b1a PZ |
1773 | |
1774 | next = rb_entry_safe(rb_next(&event->group_node), typeof(*event), group_node); | |
95ed6c70 IR |
1775 | if (next == NULL || next->cpu != event->cpu) |
1776 | return NULL; | |
1777 | ||
1778 | #ifdef CONFIG_CGROUP_PERF | |
1779 | if (event->cgrp && event->cgrp->css.cgroup) | |
1780 | curr_cgrp_id = event->cgrp->css.cgroup->kn->id; | |
1cac7b1a | 1781 | |
95ed6c70 IR |
1782 | if (next->cgrp && next->cgrp->css.cgroup) |
1783 | next_cgrp_id = next->cgrp->css.cgroup->kn->id; | |
1784 | ||
1785 | if (curr_cgrp_id != next_cgrp_id) | |
1786 | return NULL; | |
1787 | #endif | |
1788 | return next; | |
1cac7b1a PZ |
1789 | } |
1790 | ||
8e1a2031 | 1791 | /* |
161c85fa | 1792 | * Iterate through the whole groups tree. |
8e1a2031 | 1793 | */ |
6e6804d2 PZ |
1794 | #define perf_event_groups_for_each(event, groups) \ |
1795 | for (event = rb_entry_safe(rb_first(&((groups)->tree)), \ | |
1796 | typeof(*event), group_node); event; \ | |
1797 | event = rb_entry_safe(rb_next(&event->group_node), \ | |
1798 | typeof(*event), group_node)) | |
8e1a2031 | 1799 | |
fccc714b | 1800 | /* |
788faab7 | 1801 | * Add an event from the lists for its context. |
fccc714b PZ |
1802 | * Must be called with ctx->mutex and ctx->lock held. |
1803 | */ | |
04289bb9 | 1804 | static void |
cdd6c482 | 1805 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1806 | { |
c994d613 PZ |
1807 | lockdep_assert_held(&ctx->lock); |
1808 | ||
8a49542c PZ |
1809 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
1810 | event->attach_state |= PERF_ATTACH_CONTEXT; | |
04289bb9 | 1811 | |
0d3d73aa PZ |
1812 | event->tstamp = perf_event_time(event); |
1813 | ||
04289bb9 | 1814 | /* |
8a49542c PZ |
1815 | * If we're a stand alone event or group leader, we go to the context |
1816 | * list, group events are kept attached to the group so that | |
1817 | * perf_group_detach can, at all times, locate all siblings. | |
04289bb9 | 1818 | */ |
8a49542c | 1819 | if (event->group_leader == event) { |
4ff6a8de | 1820 | event->group_caps = event->event_caps; |
8e1a2031 | 1821 | add_event_to_groups(event, ctx); |
5c148194 | 1822 | } |
592903cd | 1823 | |
cdd6c482 IM |
1824 | list_add_rcu(&event->event_entry, &ctx->event_list); |
1825 | ctx->nr_events++; | |
1826 | if (event->attr.inherit_stat) | |
bfbd3381 | 1827 | ctx->nr_stat++; |
5a3126d4 | 1828 | |
33238c50 PZ |
1829 | if (event->state > PERF_EVENT_STATE_OFF) |
1830 | perf_cgroup_event_enable(event, ctx); | |
1831 | ||
5a3126d4 | 1832 | ctx->generation++; |
04289bb9 IM |
1833 | } |
1834 | ||
0231bb53 JO |
1835 | /* |
1836 | * Initialize event state based on the perf_event_attr::disabled. | |
1837 | */ | |
1838 | static inline void perf_event__state_init(struct perf_event *event) | |
1839 | { | |
1840 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : | |
1841 | PERF_EVENT_STATE_INACTIVE; | |
1842 | } | |
1843 | ||
a723968c | 1844 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
c320c7b7 ACM |
1845 | { |
1846 | int entry = sizeof(u64); /* value */ | |
1847 | int size = 0; | |
1848 | int nr = 1; | |
1849 | ||
1850 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1851 | size += sizeof(u64); | |
1852 | ||
1853 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1854 | size += sizeof(u64); | |
1855 | ||
1856 | if (event->attr.read_format & PERF_FORMAT_ID) | |
1857 | entry += sizeof(u64); | |
1858 | ||
1859 | if (event->attr.read_format & PERF_FORMAT_GROUP) { | |
a723968c | 1860 | nr += nr_siblings; |
c320c7b7 ACM |
1861 | size += sizeof(u64); |
1862 | } | |
1863 | ||
1864 | size += entry * nr; | |
1865 | event->read_size = size; | |
1866 | } | |
1867 | ||
a723968c | 1868 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
c320c7b7 ACM |
1869 | { |
1870 | struct perf_sample_data *data; | |
c320c7b7 ACM |
1871 | u16 size = 0; |
1872 | ||
c320c7b7 ACM |
1873 | if (sample_type & PERF_SAMPLE_IP) |
1874 | size += sizeof(data->ip); | |
1875 | ||
6844c09d ACM |
1876 | if (sample_type & PERF_SAMPLE_ADDR) |
1877 | size += sizeof(data->addr); | |
1878 | ||
1879 | if (sample_type & PERF_SAMPLE_PERIOD) | |
1880 | size += sizeof(data->period); | |
1881 | ||
c3feedf2 AK |
1882 | if (sample_type & PERF_SAMPLE_WEIGHT) |
1883 | size += sizeof(data->weight); | |
1884 | ||
6844c09d ACM |
1885 | if (sample_type & PERF_SAMPLE_READ) |
1886 | size += event->read_size; | |
1887 | ||
d6be9ad6 SE |
1888 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
1889 | size += sizeof(data->data_src.val); | |
1890 | ||
fdfbbd07 AK |
1891 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
1892 | size += sizeof(data->txn); | |
1893 | ||
fc7ce9c7 KL |
1894 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
1895 | size += sizeof(data->phys_addr); | |
1896 | ||
6546b19f NK |
1897 | if (sample_type & PERF_SAMPLE_CGROUP) |
1898 | size += sizeof(data->cgroup); | |
1899 | ||
6844c09d ACM |
1900 | event->header_size = size; |
1901 | } | |
1902 | ||
a723968c PZ |
1903 | /* |
1904 | * Called at perf_event creation and when events are attached/detached from a | |
1905 | * group. | |
1906 | */ | |
1907 | static void perf_event__header_size(struct perf_event *event) | |
1908 | { | |
1909 | __perf_event_read_size(event, | |
1910 | event->group_leader->nr_siblings); | |
1911 | __perf_event_header_size(event, event->attr.sample_type); | |
1912 | } | |
1913 | ||
6844c09d ACM |
1914 | static void perf_event__id_header_size(struct perf_event *event) |
1915 | { | |
1916 | struct perf_sample_data *data; | |
1917 | u64 sample_type = event->attr.sample_type; | |
1918 | u16 size = 0; | |
1919 | ||
c320c7b7 ACM |
1920 | if (sample_type & PERF_SAMPLE_TID) |
1921 | size += sizeof(data->tid_entry); | |
1922 | ||
1923 | if (sample_type & PERF_SAMPLE_TIME) | |
1924 | size += sizeof(data->time); | |
1925 | ||
ff3d527c AH |
1926 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
1927 | size += sizeof(data->id); | |
1928 | ||
c320c7b7 ACM |
1929 | if (sample_type & PERF_SAMPLE_ID) |
1930 | size += sizeof(data->id); | |
1931 | ||
1932 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1933 | size += sizeof(data->stream_id); | |
1934 | ||
1935 | if (sample_type & PERF_SAMPLE_CPU) | |
1936 | size += sizeof(data->cpu_entry); | |
1937 | ||
6844c09d | 1938 | event->id_header_size = size; |
c320c7b7 ACM |
1939 | } |
1940 | ||
a723968c PZ |
1941 | static bool perf_event_validate_size(struct perf_event *event) |
1942 | { | |
1943 | /* | |
1944 | * The values computed here will be over-written when we actually | |
1945 | * attach the event. | |
1946 | */ | |
1947 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); | |
1948 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); | |
1949 | perf_event__id_header_size(event); | |
1950 | ||
1951 | /* | |
1952 | * Sum the lot; should not exceed the 64k limit we have on records. | |
1953 | * Conservative limit to allow for callchains and other variable fields. | |
1954 | */ | |
1955 | if (event->read_size + event->header_size + | |
1956 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) | |
1957 | return false; | |
1958 | ||
1959 | return true; | |
1960 | } | |
1961 | ||
8a49542c PZ |
1962 | static void perf_group_attach(struct perf_event *event) |
1963 | { | |
c320c7b7 | 1964 | struct perf_event *group_leader = event->group_leader, *pos; |
8a49542c | 1965 | |
a76a82a3 PZ |
1966 | lockdep_assert_held(&event->ctx->lock); |
1967 | ||
74c3337c PZ |
1968 | /* |
1969 | * We can have double attach due to group movement in perf_event_open. | |
1970 | */ | |
1971 | if (event->attach_state & PERF_ATTACH_GROUP) | |
1972 | return; | |
1973 | ||
8a49542c PZ |
1974 | event->attach_state |= PERF_ATTACH_GROUP; |
1975 | ||
1976 | if (group_leader == event) | |
1977 | return; | |
1978 | ||
652884fe PZ |
1979 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
1980 | ||
4ff6a8de | 1981 | group_leader->group_caps &= event->event_caps; |
8a49542c | 1982 | |
8343aae6 | 1983 | list_add_tail(&event->sibling_list, &group_leader->sibling_list); |
8a49542c | 1984 | group_leader->nr_siblings++; |
c320c7b7 ACM |
1985 | |
1986 | perf_event__header_size(group_leader); | |
1987 | ||
edb39592 | 1988 | for_each_sibling_event(pos, group_leader) |
c320c7b7 | 1989 | perf_event__header_size(pos); |
8a49542c PZ |
1990 | } |
1991 | ||
a63eaf34 | 1992 | /* |
788faab7 | 1993 | * Remove an event from the lists for its context. |
fccc714b | 1994 | * Must be called with ctx->mutex and ctx->lock held. |
a63eaf34 | 1995 | */ |
04289bb9 | 1996 | static void |
cdd6c482 | 1997 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
04289bb9 | 1998 | { |
652884fe PZ |
1999 | WARN_ON_ONCE(event->ctx != ctx); |
2000 | lockdep_assert_held(&ctx->lock); | |
2001 | ||
8a49542c PZ |
2002 | /* |
2003 | * We can have double detach due to exit/hot-unplug + close. | |
2004 | */ | |
2005 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) | |
a63eaf34 | 2006 | return; |
8a49542c PZ |
2007 | |
2008 | event->attach_state &= ~PERF_ATTACH_CONTEXT; | |
2009 | ||
cdd6c482 IM |
2010 | ctx->nr_events--; |
2011 | if (event->attr.inherit_stat) | |
bfbd3381 | 2012 | ctx->nr_stat--; |
8bc20959 | 2013 | |
cdd6c482 | 2014 | list_del_rcu(&event->event_entry); |
04289bb9 | 2015 | |
8a49542c | 2016 | if (event->group_leader == event) |
8e1a2031 | 2017 | del_event_from_groups(event, ctx); |
5c148194 | 2018 | |
b2e74a26 SE |
2019 | /* |
2020 | * If event was in error state, then keep it | |
2021 | * that way, otherwise bogus counts will be | |
2022 | * returned on read(). The only way to get out | |
2023 | * of error state is by explicit re-enabling | |
2024 | * of the event | |
2025 | */ | |
33238c50 PZ |
2026 | if (event->state > PERF_EVENT_STATE_OFF) { |
2027 | perf_cgroup_event_disable(event, ctx); | |
0d3d73aa | 2028 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); |
33238c50 | 2029 | } |
5a3126d4 PZ |
2030 | |
2031 | ctx->generation++; | |
050735b0 PZ |
2032 | } |
2033 | ||
ab43762e AS |
2034 | static int |
2035 | perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event) | |
2036 | { | |
2037 | if (!has_aux(aux_event)) | |
2038 | return 0; | |
2039 | ||
2040 | if (!event->pmu->aux_output_match) | |
2041 | return 0; | |
2042 | ||
2043 | return event->pmu->aux_output_match(aux_event); | |
2044 | } | |
2045 | ||
2046 | static void put_event(struct perf_event *event); | |
2047 | static void event_sched_out(struct perf_event *event, | |
2048 | struct perf_cpu_context *cpuctx, | |
2049 | struct perf_event_context *ctx); | |
2050 | ||
2051 | static void perf_put_aux_event(struct perf_event *event) | |
2052 | { | |
2053 | struct perf_event_context *ctx = event->ctx; | |
2054 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
2055 | struct perf_event *iter; | |
2056 | ||
2057 | /* | |
2058 | * If event uses aux_event tear down the link | |
2059 | */ | |
2060 | if (event->aux_event) { | |
2061 | iter = event->aux_event; | |
2062 | event->aux_event = NULL; | |
2063 | put_event(iter); | |
2064 | return; | |
2065 | } | |
2066 | ||
2067 | /* | |
2068 | * If the event is an aux_event, tear down all links to | |
2069 | * it from other events. | |
2070 | */ | |
2071 | for_each_sibling_event(iter, event->group_leader) { | |
2072 | if (iter->aux_event != event) | |
2073 | continue; | |
2074 | ||
2075 | iter->aux_event = NULL; | |
2076 | put_event(event); | |
2077 | ||
2078 | /* | |
2079 | * If it's ACTIVE, schedule it out and put it into ERROR | |
2080 | * state so that we don't try to schedule it again. Note | |
2081 | * that perf_event_enable() will clear the ERROR status. | |
2082 | */ | |
2083 | event_sched_out(iter, cpuctx, ctx); | |
2084 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); | |
2085 | } | |
2086 | } | |
2087 | ||
a4faf00d AS |
2088 | static bool perf_need_aux_event(struct perf_event *event) |
2089 | { | |
2090 | return !!event->attr.aux_output || !!event->attr.aux_sample_size; | |
2091 | } | |
2092 | ||
ab43762e AS |
2093 | static int perf_get_aux_event(struct perf_event *event, |
2094 | struct perf_event *group_leader) | |
2095 | { | |
2096 | /* | |
2097 | * Our group leader must be an aux event if we want to be | |
2098 | * an aux_output. This way, the aux event will precede its | |
2099 | * aux_output events in the group, and therefore will always | |
2100 | * schedule first. | |
2101 | */ | |
2102 | if (!group_leader) | |
2103 | return 0; | |
2104 | ||
a4faf00d AS |
2105 | /* |
2106 | * aux_output and aux_sample_size are mutually exclusive. | |
2107 | */ | |
2108 | if (event->attr.aux_output && event->attr.aux_sample_size) | |
2109 | return 0; | |
2110 | ||
2111 | if (event->attr.aux_output && | |
2112 | !perf_aux_output_match(event, group_leader)) | |
2113 | return 0; | |
2114 | ||
2115 | if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux) | |
ab43762e AS |
2116 | return 0; |
2117 | ||
2118 | if (!atomic_long_inc_not_zero(&group_leader->refcount)) | |
2119 | return 0; | |
2120 | ||
2121 | /* | |
2122 | * Link aux_outputs to their aux event; this is undone in | |
2123 | * perf_group_detach() by perf_put_aux_event(). When the | |
2124 | * group in torn down, the aux_output events loose their | |
2125 | * link to the aux_event and can't schedule any more. | |
2126 | */ | |
2127 | event->aux_event = group_leader; | |
2128 | ||
2129 | return 1; | |
2130 | } | |
2131 | ||
ab6f824c PZ |
2132 | static inline struct list_head *get_event_list(struct perf_event *event) |
2133 | { | |
2134 | struct perf_event_context *ctx = event->ctx; | |
2135 | return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active; | |
2136 | } | |
2137 | ||
8a49542c | 2138 | static void perf_group_detach(struct perf_event *event) |
050735b0 PZ |
2139 | { |
2140 | struct perf_event *sibling, *tmp; | |
6668128a | 2141 | struct perf_event_context *ctx = event->ctx; |
8a49542c | 2142 | |
6668128a | 2143 | lockdep_assert_held(&ctx->lock); |
a76a82a3 | 2144 | |
8a49542c PZ |
2145 | /* |
2146 | * We can have double detach due to exit/hot-unplug + close. | |
2147 | */ | |
2148 | if (!(event->attach_state & PERF_ATTACH_GROUP)) | |
2149 | return; | |
2150 | ||
2151 | event->attach_state &= ~PERF_ATTACH_GROUP; | |
2152 | ||
ab43762e AS |
2153 | perf_put_aux_event(event); |
2154 | ||
8a49542c PZ |
2155 | /* |
2156 | * If this is a sibling, remove it from its group. | |
2157 | */ | |
2158 | if (event->group_leader != event) { | |
8343aae6 | 2159 | list_del_init(&event->sibling_list); |
8a49542c | 2160 | event->group_leader->nr_siblings--; |
c320c7b7 | 2161 | goto out; |
8a49542c PZ |
2162 | } |
2163 | ||
04289bb9 | 2164 | /* |
cdd6c482 IM |
2165 | * If this was a group event with sibling events then |
2166 | * upgrade the siblings to singleton events by adding them | |
8a49542c | 2167 | * to whatever list we are on. |
04289bb9 | 2168 | */ |
8343aae6 | 2169 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { |
8e1a2031 | 2170 | |
04289bb9 | 2171 | sibling->group_leader = sibling; |
24868367 | 2172 | list_del_init(&sibling->sibling_list); |
d6f962b5 FW |
2173 | |
2174 | /* Inherit group flags from the previous leader */ | |
4ff6a8de | 2175 | sibling->group_caps = event->group_caps; |
652884fe | 2176 | |
8e1a2031 | 2177 | if (!RB_EMPTY_NODE(&event->group_node)) { |
8e1a2031 | 2178 | add_event_to_groups(sibling, event->ctx); |
6668128a | 2179 | |
ab6f824c PZ |
2180 | if (sibling->state == PERF_EVENT_STATE_ACTIVE) |
2181 | list_add_tail(&sibling->active_list, get_event_list(sibling)); | |
8e1a2031 AB |
2182 | } |
2183 | ||
652884fe | 2184 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
04289bb9 | 2185 | } |
c320c7b7 ACM |
2186 | |
2187 | out: | |
2188 | perf_event__header_size(event->group_leader); | |
2189 | ||
edb39592 | 2190 | for_each_sibling_event(tmp, event->group_leader) |
c320c7b7 | 2191 | perf_event__header_size(tmp); |
04289bb9 IM |
2192 | } |
2193 | ||
fadfe7be JO |
2194 | static bool is_orphaned_event(struct perf_event *event) |
2195 | { | |
a69b0ca4 | 2196 | return event->state == PERF_EVENT_STATE_DEAD; |
fadfe7be JO |
2197 | } |
2198 | ||
2c81a647 | 2199 | static inline int __pmu_filter_match(struct perf_event *event) |
66eb579e MR |
2200 | { |
2201 | struct pmu *pmu = event->pmu; | |
2202 | return pmu->filter_match ? pmu->filter_match(event) : 1; | |
2203 | } | |
2204 | ||
2c81a647 MR |
2205 | /* |
2206 | * Check whether we should attempt to schedule an event group based on | |
2207 | * PMU-specific filtering. An event group can consist of HW and SW events, | |
2208 | * potentially with a SW leader, so we must check all the filters, to | |
2209 | * determine whether a group is schedulable: | |
2210 | */ | |
2211 | static inline int pmu_filter_match(struct perf_event *event) | |
2212 | { | |
edb39592 | 2213 | struct perf_event *sibling; |
2c81a647 MR |
2214 | |
2215 | if (!__pmu_filter_match(event)) | |
2216 | return 0; | |
2217 | ||
edb39592 PZ |
2218 | for_each_sibling_event(sibling, event) { |
2219 | if (!__pmu_filter_match(sibling)) | |
2c81a647 MR |
2220 | return 0; |
2221 | } | |
2222 | ||
2223 | return 1; | |
2224 | } | |
2225 | ||
fa66f07a SE |
2226 | static inline int |
2227 | event_filter_match(struct perf_event *event) | |
2228 | { | |
0b8f1e2e PZ |
2229 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
2230 | perf_cgroup_match(event) && pmu_filter_match(event); | |
fa66f07a SE |
2231 | } |
2232 | ||
9ffcfa6f SE |
2233 | static void |
2234 | event_sched_out(struct perf_event *event, | |
3b6f9e5c | 2235 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 2236 | struct perf_event_context *ctx) |
3b6f9e5c | 2237 | { |
0d3d73aa | 2238 | enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; |
652884fe PZ |
2239 | |
2240 | WARN_ON_ONCE(event->ctx != ctx); | |
2241 | lockdep_assert_held(&ctx->lock); | |
2242 | ||
cdd6c482 | 2243 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
9ffcfa6f | 2244 | return; |
3b6f9e5c | 2245 | |
6668128a PZ |
2246 | /* |
2247 | * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but | |
2248 | * we can schedule events _OUT_ individually through things like | |
2249 | * __perf_remove_from_context(). | |
2250 | */ | |
2251 | list_del_init(&event->active_list); | |
2252 | ||
44377277 AS |
2253 | perf_pmu_disable(event->pmu); |
2254 | ||
28a967c3 PZ |
2255 | event->pmu->del(event, 0); |
2256 | event->oncpu = -1; | |
0d3d73aa | 2257 | |
1d54ad94 PZ |
2258 | if (READ_ONCE(event->pending_disable) >= 0) { |
2259 | WRITE_ONCE(event->pending_disable, -1); | |
33238c50 | 2260 | perf_cgroup_event_disable(event, ctx); |
0d3d73aa | 2261 | state = PERF_EVENT_STATE_OFF; |
970892a9 | 2262 | } |
0d3d73aa | 2263 | perf_event_set_state(event, state); |
3b6f9e5c | 2264 | |
cdd6c482 | 2265 | if (!is_software_event(event)) |
3b6f9e5c | 2266 | cpuctx->active_oncpu--; |
2fde4f94 MR |
2267 | if (!--ctx->nr_active) |
2268 | perf_event_ctx_deactivate(ctx); | |
0f5a2601 PZ |
2269 | if (event->attr.freq && event->attr.sample_freq) |
2270 | ctx->nr_freq--; | |
cdd6c482 | 2271 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
3b6f9e5c | 2272 | cpuctx->exclusive = 0; |
44377277 AS |
2273 | |
2274 | perf_pmu_enable(event->pmu); | |
3b6f9e5c PM |
2275 | } |
2276 | ||
d859e29f | 2277 | static void |
cdd6c482 | 2278 | group_sched_out(struct perf_event *group_event, |
d859e29f | 2279 | struct perf_cpu_context *cpuctx, |
cdd6c482 | 2280 | struct perf_event_context *ctx) |
d859e29f | 2281 | { |
cdd6c482 | 2282 | struct perf_event *event; |
0d3d73aa PZ |
2283 | |
2284 | if (group_event->state != PERF_EVENT_STATE_ACTIVE) | |
2285 | return; | |
d859e29f | 2286 | |
3f005e7d MR |
2287 | perf_pmu_disable(ctx->pmu); |
2288 | ||
cdd6c482 | 2289 | event_sched_out(group_event, cpuctx, ctx); |
d859e29f PM |
2290 | |
2291 | /* | |
2292 | * Schedule out siblings (if any): | |
2293 | */ | |
edb39592 | 2294 | for_each_sibling_event(event, group_event) |
cdd6c482 | 2295 | event_sched_out(event, cpuctx, ctx); |
d859e29f | 2296 | |
3f005e7d MR |
2297 | perf_pmu_enable(ctx->pmu); |
2298 | ||
0d3d73aa | 2299 | if (group_event->attr.exclusive) |
d859e29f PM |
2300 | cpuctx->exclusive = 0; |
2301 | } | |
2302 | ||
45a0e07a | 2303 | #define DETACH_GROUP 0x01UL |
0017960f | 2304 | |
0793a61d | 2305 | /* |
cdd6c482 | 2306 | * Cross CPU call to remove a performance event |
0793a61d | 2307 | * |
cdd6c482 | 2308 | * We disable the event on the hardware level first. After that we |
0793a61d TG |
2309 | * remove it from the context list. |
2310 | */ | |
fae3fde6 PZ |
2311 | static void |
2312 | __perf_remove_from_context(struct perf_event *event, | |
2313 | struct perf_cpu_context *cpuctx, | |
2314 | struct perf_event_context *ctx, | |
2315 | void *info) | |
0793a61d | 2316 | { |
45a0e07a | 2317 | unsigned long flags = (unsigned long)info; |
0793a61d | 2318 | |
3c5c8711 PZ |
2319 | if (ctx->is_active & EVENT_TIME) { |
2320 | update_context_time(ctx); | |
2321 | update_cgrp_time_from_cpuctx(cpuctx); | |
2322 | } | |
2323 | ||
cdd6c482 | 2324 | event_sched_out(event, cpuctx, ctx); |
45a0e07a | 2325 | if (flags & DETACH_GROUP) |
46ce0fe9 | 2326 | perf_group_detach(event); |
cdd6c482 | 2327 | list_del_event(event, ctx); |
39a43640 PZ |
2328 | |
2329 | if (!ctx->nr_events && ctx->is_active) { | |
64ce3126 | 2330 | ctx->is_active = 0; |
90c91dfb | 2331 | ctx->rotate_necessary = 0; |
39a43640 PZ |
2332 | if (ctx->task) { |
2333 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
2334 | cpuctx->task_ctx = NULL; | |
2335 | } | |
64ce3126 | 2336 | } |
0793a61d TG |
2337 | } |
2338 | ||
0793a61d | 2339 | /* |
cdd6c482 | 2340 | * Remove the event from a task's (or a CPU's) list of events. |
0793a61d | 2341 | * |
cdd6c482 IM |
2342 | * If event->ctx is a cloned context, callers must make sure that |
2343 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 PM |
2344 | * remains valid. This is OK when called from perf_release since |
2345 | * that only calls us on the top-level context, which can't be a clone. | |
cdd6c482 | 2346 | * When called from perf_event_exit_task, it's OK because the |
c93f7669 | 2347 | * context has been detached from its task. |
0793a61d | 2348 | */ |
45a0e07a | 2349 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
0793a61d | 2350 | { |
a76a82a3 PZ |
2351 | struct perf_event_context *ctx = event->ctx; |
2352 | ||
2353 | lockdep_assert_held(&ctx->mutex); | |
0793a61d | 2354 | |
45a0e07a | 2355 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
a76a82a3 PZ |
2356 | |
2357 | /* | |
2358 | * The above event_function_call() can NO-OP when it hits | |
2359 | * TASK_TOMBSTONE. In that case we must already have been detached | |
2360 | * from the context (by perf_event_exit_event()) but the grouping | |
2361 | * might still be in-tact. | |
2362 | */ | |
2363 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); | |
2364 | if ((flags & DETACH_GROUP) && | |
2365 | (event->attach_state & PERF_ATTACH_GROUP)) { | |
2366 | /* | |
2367 | * Since in that case we cannot possibly be scheduled, simply | |
2368 | * detach now. | |
2369 | */ | |
2370 | raw_spin_lock_irq(&ctx->lock); | |
2371 | perf_group_detach(event); | |
2372 | raw_spin_unlock_irq(&ctx->lock); | |
2373 | } | |
0793a61d TG |
2374 | } |
2375 | ||
d859e29f | 2376 | /* |
cdd6c482 | 2377 | * Cross CPU call to disable a performance event |
d859e29f | 2378 | */ |
fae3fde6 PZ |
2379 | static void __perf_event_disable(struct perf_event *event, |
2380 | struct perf_cpu_context *cpuctx, | |
2381 | struct perf_event_context *ctx, | |
2382 | void *info) | |
7b648018 | 2383 | { |
fae3fde6 PZ |
2384 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
2385 | return; | |
7b648018 | 2386 | |
3c5c8711 PZ |
2387 | if (ctx->is_active & EVENT_TIME) { |
2388 | update_context_time(ctx); | |
2389 | update_cgrp_time_from_event(event); | |
2390 | } | |
2391 | ||
fae3fde6 PZ |
2392 | if (event == event->group_leader) |
2393 | group_sched_out(event, cpuctx, ctx); | |
2394 | else | |
2395 | event_sched_out(event, cpuctx, ctx); | |
0d3d73aa PZ |
2396 | |
2397 | perf_event_set_state(event, PERF_EVENT_STATE_OFF); | |
33238c50 | 2398 | perf_cgroup_event_disable(event, ctx); |
7b648018 PZ |
2399 | } |
2400 | ||
d859e29f | 2401 | /* |
788faab7 | 2402 | * Disable an event. |
c93f7669 | 2403 | * |
cdd6c482 IM |
2404 | * If event->ctx is a cloned context, callers must make sure that |
2405 | * every task struct that event->ctx->task could possibly point to | |
9f014e3a | 2406 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2407 | * perf_event_for_each_child or perf_event_for_each because they |
2408 | * hold the top-level event's child_mutex, so any descendant that | |
8ba289b8 PZ |
2409 | * goes to exit will block in perf_event_exit_event(). |
2410 | * | |
cdd6c482 | 2411 | * When called from perf_pending_event it's OK because event->ctx |
c93f7669 | 2412 | * is the current context on this CPU and preemption is disabled, |
cdd6c482 | 2413 | * hence we can't get into perf_event_task_sched_out for this context. |
d859e29f | 2414 | */ |
f63a8daa | 2415 | static void _perf_event_disable(struct perf_event *event) |
d859e29f | 2416 | { |
cdd6c482 | 2417 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2418 | |
e625cce1 | 2419 | raw_spin_lock_irq(&ctx->lock); |
7b648018 | 2420 | if (event->state <= PERF_EVENT_STATE_OFF) { |
e625cce1 | 2421 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2422 | return; |
53cfbf59 | 2423 | } |
e625cce1 | 2424 | raw_spin_unlock_irq(&ctx->lock); |
7b648018 | 2425 | |
fae3fde6 PZ |
2426 | event_function_call(event, __perf_event_disable, NULL); |
2427 | } | |
2428 | ||
2429 | void perf_event_disable_local(struct perf_event *event) | |
2430 | { | |
2431 | event_function_local(event, __perf_event_disable, NULL); | |
d859e29f | 2432 | } |
f63a8daa PZ |
2433 | |
2434 | /* | |
2435 | * Strictly speaking kernel users cannot create groups and therefore this | |
2436 | * interface does not need the perf_event_ctx_lock() magic. | |
2437 | */ | |
2438 | void perf_event_disable(struct perf_event *event) | |
2439 | { | |
2440 | struct perf_event_context *ctx; | |
2441 | ||
2442 | ctx = perf_event_ctx_lock(event); | |
2443 | _perf_event_disable(event); | |
2444 | perf_event_ctx_unlock(event, ctx); | |
2445 | } | |
dcfce4a0 | 2446 | EXPORT_SYMBOL_GPL(perf_event_disable); |
d859e29f | 2447 | |
5aab90ce JO |
2448 | void perf_event_disable_inatomic(struct perf_event *event) |
2449 | { | |
1d54ad94 PZ |
2450 | WRITE_ONCE(event->pending_disable, smp_processor_id()); |
2451 | /* can fail, see perf_pending_event_disable() */ | |
5aab90ce JO |
2452 | irq_work_queue(&event->pending); |
2453 | } | |
2454 | ||
e5d1367f | 2455 | static void perf_set_shadow_time(struct perf_event *event, |
0d3d73aa | 2456 | struct perf_event_context *ctx) |
e5d1367f SE |
2457 | { |
2458 | /* | |
2459 | * use the correct time source for the time snapshot | |
2460 | * | |
2461 | * We could get by without this by leveraging the | |
2462 | * fact that to get to this function, the caller | |
2463 | * has most likely already called update_context_time() | |
2464 | * and update_cgrp_time_xx() and thus both timestamp | |
2465 | * are identical (or very close). Given that tstamp is, | |
2466 | * already adjusted for cgroup, we could say that: | |
2467 | * tstamp - ctx->timestamp | |
2468 | * is equivalent to | |
2469 | * tstamp - cgrp->timestamp. | |
2470 | * | |
2471 | * Then, in perf_output_read(), the calculation would | |
2472 | * work with no changes because: | |
2473 | * - event is guaranteed scheduled in | |
2474 | * - no scheduled out in between | |
2475 | * - thus the timestamp would be the same | |
2476 | * | |
2477 | * But this is a bit hairy. | |
2478 | * | |
2479 | * So instead, we have an explicit cgroup call to remain | |
2480 | * within the time time source all along. We believe it | |
2481 | * is cleaner and simpler to understand. | |
2482 | */ | |
2483 | if (is_cgroup_event(event)) | |
0d3d73aa | 2484 | perf_cgroup_set_shadow_time(event, event->tstamp); |
e5d1367f | 2485 | else |
0d3d73aa | 2486 | event->shadow_ctx_time = event->tstamp - ctx->timestamp; |
e5d1367f SE |
2487 | } |
2488 | ||
4fe757dd PZ |
2489 | #define MAX_INTERRUPTS (~0ULL) |
2490 | ||
2491 | static void perf_log_throttle(struct perf_event *event, int enable); | |
ec0d7729 | 2492 | static void perf_log_itrace_start(struct perf_event *event); |
4fe757dd | 2493 | |
235c7fc7 | 2494 | static int |
9ffcfa6f | 2495 | event_sched_in(struct perf_event *event, |
235c7fc7 | 2496 | struct perf_cpu_context *cpuctx, |
6e37738a | 2497 | struct perf_event_context *ctx) |
235c7fc7 | 2498 | { |
44377277 | 2499 | int ret = 0; |
4158755d | 2500 | |
ab6f824c PZ |
2501 | WARN_ON_ONCE(event->ctx != ctx); |
2502 | ||
63342411 PZ |
2503 | lockdep_assert_held(&ctx->lock); |
2504 | ||
cdd6c482 | 2505 | if (event->state <= PERF_EVENT_STATE_OFF) |
235c7fc7 IM |
2506 | return 0; |
2507 | ||
95ff4ca2 AS |
2508 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
2509 | /* | |
0c1cbc18 PZ |
2510 | * Order event::oncpu write to happen before the ACTIVE state is |
2511 | * visible. This allows perf_event_{stop,read}() to observe the correct | |
2512 | * ->oncpu if it sees ACTIVE. | |
95ff4ca2 AS |
2513 | */ |
2514 | smp_wmb(); | |
0d3d73aa | 2515 | perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); |
4fe757dd PZ |
2516 | |
2517 | /* | |
2518 | * Unthrottle events, since we scheduled we might have missed several | |
2519 | * ticks already, also for a heavily scheduling task there is little | |
2520 | * guarantee it'll get a tick in a timely manner. | |
2521 | */ | |
2522 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | |
2523 | perf_log_throttle(event, 1); | |
2524 | event->hw.interrupts = 0; | |
2525 | } | |
2526 | ||
44377277 AS |
2527 | perf_pmu_disable(event->pmu); |
2528 | ||
0d3d73aa | 2529 | perf_set_shadow_time(event, ctx); |
72f669c0 | 2530 | |
ec0d7729 AS |
2531 | perf_log_itrace_start(event); |
2532 | ||
a4eaf7f1 | 2533 | if (event->pmu->add(event, PERF_EF_START)) { |
0d3d73aa | 2534 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
cdd6c482 | 2535 | event->oncpu = -1; |
44377277 AS |
2536 | ret = -EAGAIN; |
2537 | goto out; | |
235c7fc7 IM |
2538 | } |
2539 | ||
cdd6c482 | 2540 | if (!is_software_event(event)) |
3b6f9e5c | 2541 | cpuctx->active_oncpu++; |
2fde4f94 MR |
2542 | if (!ctx->nr_active++) |
2543 | perf_event_ctx_activate(ctx); | |
0f5a2601 PZ |
2544 | if (event->attr.freq && event->attr.sample_freq) |
2545 | ctx->nr_freq++; | |
235c7fc7 | 2546 | |
cdd6c482 | 2547 | if (event->attr.exclusive) |
3b6f9e5c PM |
2548 | cpuctx->exclusive = 1; |
2549 | ||
44377277 AS |
2550 | out: |
2551 | perf_pmu_enable(event->pmu); | |
2552 | ||
2553 | return ret; | |
235c7fc7 IM |
2554 | } |
2555 | ||
6751b71e | 2556 | static int |
cdd6c482 | 2557 | group_sched_in(struct perf_event *group_event, |
6751b71e | 2558 | struct perf_cpu_context *cpuctx, |
6e37738a | 2559 | struct perf_event_context *ctx) |
6751b71e | 2560 | { |
6bde9b6c | 2561 | struct perf_event *event, *partial_group = NULL; |
4a234593 | 2562 | struct pmu *pmu = ctx->pmu; |
6751b71e | 2563 | |
cdd6c482 | 2564 | if (group_event->state == PERF_EVENT_STATE_OFF) |
6751b71e PM |
2565 | return 0; |
2566 | ||
fbbe0701 | 2567 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
6bde9b6c | 2568 | |
9ffcfa6f | 2569 | if (event_sched_in(group_event, cpuctx, ctx)) { |
ad5133b7 | 2570 | pmu->cancel_txn(pmu); |
272325c4 | 2571 | perf_mux_hrtimer_restart(cpuctx); |
6751b71e | 2572 | return -EAGAIN; |
90151c35 | 2573 | } |
6751b71e PM |
2574 | |
2575 | /* | |
2576 | * Schedule in siblings as one group (if any): | |
2577 | */ | |
edb39592 | 2578 | for_each_sibling_event(event, group_event) { |
9ffcfa6f | 2579 | if (event_sched_in(event, cpuctx, ctx)) { |
cdd6c482 | 2580 | partial_group = event; |
6751b71e PM |
2581 | goto group_error; |
2582 | } | |
2583 | } | |
2584 | ||
9ffcfa6f | 2585 | if (!pmu->commit_txn(pmu)) |
6e85158c | 2586 | return 0; |
9ffcfa6f | 2587 | |
6751b71e PM |
2588 | group_error: |
2589 | /* | |
2590 | * Groups can be scheduled in as one unit only, so undo any | |
2591 | * partial group before returning: | |
0d3d73aa | 2592 | * The events up to the failed event are scheduled out normally. |
6751b71e | 2593 | */ |
edb39592 | 2594 | for_each_sibling_event(event, group_event) { |
cdd6c482 | 2595 | if (event == partial_group) |
0d3d73aa | 2596 | break; |
d7842da4 | 2597 | |
0d3d73aa | 2598 | event_sched_out(event, cpuctx, ctx); |
6751b71e | 2599 | } |
9ffcfa6f | 2600 | event_sched_out(group_event, cpuctx, ctx); |
6751b71e | 2601 | |
ad5133b7 | 2602 | pmu->cancel_txn(pmu); |
90151c35 | 2603 | |
272325c4 | 2604 | perf_mux_hrtimer_restart(cpuctx); |
9e630205 | 2605 | |
6751b71e PM |
2606 | return -EAGAIN; |
2607 | } | |
2608 | ||
3b6f9e5c | 2609 | /* |
cdd6c482 | 2610 | * Work out whether we can put this event group on the CPU now. |
3b6f9e5c | 2611 | */ |
cdd6c482 | 2612 | static int group_can_go_on(struct perf_event *event, |
3b6f9e5c PM |
2613 | struct perf_cpu_context *cpuctx, |
2614 | int can_add_hw) | |
2615 | { | |
2616 | /* | |
cdd6c482 | 2617 | * Groups consisting entirely of software events can always go on. |
3b6f9e5c | 2618 | */ |
4ff6a8de | 2619 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
3b6f9e5c PM |
2620 | return 1; |
2621 | /* | |
2622 | * If an exclusive group is already on, no other hardware | |
cdd6c482 | 2623 | * events can go on. |
3b6f9e5c PM |
2624 | */ |
2625 | if (cpuctx->exclusive) | |
2626 | return 0; | |
2627 | /* | |
2628 | * If this group is exclusive and there are already | |
cdd6c482 | 2629 | * events on the CPU, it can't go on. |
3b6f9e5c | 2630 | */ |
cdd6c482 | 2631 | if (event->attr.exclusive && cpuctx->active_oncpu) |
3b6f9e5c PM |
2632 | return 0; |
2633 | /* | |
2634 | * Otherwise, try to add it if all previous groups were able | |
2635 | * to go on. | |
2636 | */ | |
2637 | return can_add_hw; | |
2638 | } | |
2639 | ||
cdd6c482 IM |
2640 | static void add_event_to_ctx(struct perf_event *event, |
2641 | struct perf_event_context *ctx) | |
53cfbf59 | 2642 | { |
cdd6c482 | 2643 | list_add_event(event, ctx); |
8a49542c | 2644 | perf_group_attach(event); |
53cfbf59 PM |
2645 | } |
2646 | ||
bd2afa49 PZ |
2647 | static void ctx_sched_out(struct perf_event_context *ctx, |
2648 | struct perf_cpu_context *cpuctx, | |
2649 | enum event_type_t event_type); | |
2c29ef0f PZ |
2650 | static void |
2651 | ctx_sched_in(struct perf_event_context *ctx, | |
2652 | struct perf_cpu_context *cpuctx, | |
2653 | enum event_type_t event_type, | |
2654 | struct task_struct *task); | |
fe4b04fa | 2655 | |
bd2afa49 | 2656 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2657 | struct perf_event_context *ctx, |
2658 | enum event_type_t event_type) | |
bd2afa49 PZ |
2659 | { |
2660 | if (!cpuctx->task_ctx) | |
2661 | return; | |
2662 | ||
2663 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) | |
2664 | return; | |
2665 | ||
487f05e1 | 2666 | ctx_sched_out(ctx, cpuctx, event_type); |
bd2afa49 PZ |
2667 | } |
2668 | ||
dce5855b PZ |
2669 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
2670 | struct perf_event_context *ctx, | |
2671 | struct task_struct *task) | |
2672 | { | |
2673 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); | |
2674 | if (ctx) | |
2675 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); | |
2676 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); | |
2677 | if (ctx) | |
2678 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); | |
2679 | } | |
2680 | ||
487f05e1 AS |
2681 | /* |
2682 | * We want to maintain the following priority of scheduling: | |
2683 | * - CPU pinned (EVENT_CPU | EVENT_PINNED) | |
2684 | * - task pinned (EVENT_PINNED) | |
2685 | * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) | |
2686 | * - task flexible (EVENT_FLEXIBLE). | |
2687 | * | |
2688 | * In order to avoid unscheduling and scheduling back in everything every | |
2689 | * time an event is added, only do it for the groups of equal priority and | |
2690 | * below. | |
2691 | * | |
2692 | * This can be called after a batch operation on task events, in which case | |
2693 | * event_type is a bit mask of the types of events involved. For CPU events, | |
2694 | * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. | |
2695 | */ | |
3e349507 | 2696 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
487f05e1 AS |
2697 | struct perf_event_context *task_ctx, |
2698 | enum event_type_t event_type) | |
0017960f | 2699 | { |
bd903afe | 2700 | enum event_type_t ctx_event_type; |
487f05e1 AS |
2701 | bool cpu_event = !!(event_type & EVENT_CPU); |
2702 | ||
2703 | /* | |
2704 | * If pinned groups are involved, flexible groups also need to be | |
2705 | * scheduled out. | |
2706 | */ | |
2707 | if (event_type & EVENT_PINNED) | |
2708 | event_type |= EVENT_FLEXIBLE; | |
2709 | ||
bd903afe SL |
2710 | ctx_event_type = event_type & EVENT_ALL; |
2711 | ||
3e349507 PZ |
2712 | perf_pmu_disable(cpuctx->ctx.pmu); |
2713 | if (task_ctx) | |
487f05e1 AS |
2714 | task_ctx_sched_out(cpuctx, task_ctx, event_type); |
2715 | ||
2716 | /* | |
2717 | * Decide which cpu ctx groups to schedule out based on the types | |
2718 | * of events that caused rescheduling: | |
2719 | * - EVENT_CPU: schedule out corresponding groups; | |
2720 | * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; | |
2721 | * - otherwise, do nothing more. | |
2722 | */ | |
2723 | if (cpu_event) | |
2724 | cpu_ctx_sched_out(cpuctx, ctx_event_type); | |
2725 | else if (ctx_event_type & EVENT_PINNED) | |
2726 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
2727 | ||
3e349507 PZ |
2728 | perf_event_sched_in(cpuctx, task_ctx, current); |
2729 | perf_pmu_enable(cpuctx->ctx.pmu); | |
0017960f PZ |
2730 | } |
2731 | ||
c68d224e SE |
2732 | void perf_pmu_resched(struct pmu *pmu) |
2733 | { | |
2734 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); | |
2735 | struct perf_event_context *task_ctx = cpuctx->task_ctx; | |
2736 | ||
2737 | perf_ctx_lock(cpuctx, task_ctx); | |
2738 | ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU); | |
2739 | perf_ctx_unlock(cpuctx, task_ctx); | |
2740 | } | |
2741 | ||
0793a61d | 2742 | /* |
cdd6c482 | 2743 | * Cross CPU call to install and enable a performance event |
682076ae | 2744 | * |
a096309b PZ |
2745 | * Very similar to remote_function() + event_function() but cannot assume that |
2746 | * things like ctx->is_active and cpuctx->task_ctx are set. | |
0793a61d | 2747 | */ |
fe4b04fa | 2748 | static int __perf_install_in_context(void *info) |
0793a61d | 2749 | { |
a096309b PZ |
2750 | struct perf_event *event = info; |
2751 | struct perf_event_context *ctx = event->ctx; | |
108b02cf | 2752 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
2c29ef0f | 2753 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
63cae12b | 2754 | bool reprogram = true; |
a096309b | 2755 | int ret = 0; |
0793a61d | 2756 | |
63b6da39 | 2757 | raw_spin_lock(&cpuctx->ctx.lock); |
39a43640 | 2758 | if (ctx->task) { |
b58f6b0d PZ |
2759 | raw_spin_lock(&ctx->lock); |
2760 | task_ctx = ctx; | |
a096309b | 2761 | |
63cae12b | 2762 | reprogram = (ctx->task == current); |
b58f6b0d | 2763 | |
39a43640 | 2764 | /* |
63cae12b PZ |
2765 | * If the task is running, it must be running on this CPU, |
2766 | * otherwise we cannot reprogram things. | |
2767 | * | |
2768 | * If its not running, we don't care, ctx->lock will | |
2769 | * serialize against it becoming runnable. | |
39a43640 | 2770 | */ |
63cae12b PZ |
2771 | if (task_curr(ctx->task) && !reprogram) { |
2772 | ret = -ESRCH; | |
2773 | goto unlock; | |
2774 | } | |
a096309b | 2775 | |
63cae12b | 2776 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
63b6da39 PZ |
2777 | } else if (task_ctx) { |
2778 | raw_spin_lock(&task_ctx->lock); | |
2c29ef0f | 2779 | } |
b58f6b0d | 2780 | |
33801b94 | 2781 | #ifdef CONFIG_CGROUP_PERF |
33238c50 | 2782 | if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) { |
33801b94 | 2783 | /* |
2784 | * If the current cgroup doesn't match the event's | |
2785 | * cgroup, we should not try to schedule it. | |
2786 | */ | |
2787 | struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); | |
2788 | reprogram = cgroup_is_descendant(cgrp->css.cgroup, | |
2789 | event->cgrp->css.cgroup); | |
2790 | } | |
2791 | #endif | |
2792 | ||
63cae12b | 2793 | if (reprogram) { |
a096309b PZ |
2794 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
2795 | add_event_to_ctx(event, ctx); | |
487f05e1 | 2796 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
a096309b PZ |
2797 | } else { |
2798 | add_event_to_ctx(event, ctx); | |
2799 | } | |
2800 | ||
63b6da39 | 2801 | unlock: |
2c29ef0f | 2802 | perf_ctx_unlock(cpuctx, task_ctx); |
fe4b04fa | 2803 | |
a096309b | 2804 | return ret; |
0793a61d TG |
2805 | } |
2806 | ||
8a58ddae AS |
2807 | static bool exclusive_event_installable(struct perf_event *event, |
2808 | struct perf_event_context *ctx); | |
2809 | ||
0793a61d | 2810 | /* |
a096309b PZ |
2811 | * Attach a performance event to a context. |
2812 | * | |
2813 | * Very similar to event_function_call, see comment there. | |
0793a61d TG |
2814 | */ |
2815 | static void | |
cdd6c482 IM |
2816 | perf_install_in_context(struct perf_event_context *ctx, |
2817 | struct perf_event *event, | |
0793a61d TG |
2818 | int cpu) |
2819 | { | |
a096309b | 2820 | struct task_struct *task = READ_ONCE(ctx->task); |
39a43640 | 2821 | |
fe4b04fa PZ |
2822 | lockdep_assert_held(&ctx->mutex); |
2823 | ||
8a58ddae AS |
2824 | WARN_ON_ONCE(!exclusive_event_installable(event, ctx)); |
2825 | ||
0cda4c02 YZ |
2826 | if (event->cpu != -1) |
2827 | event->cpu = cpu; | |
c3f00c70 | 2828 | |
0b8f1e2e PZ |
2829 | /* |
2830 | * Ensures that if we can observe event->ctx, both the event and ctx | |
2831 | * will be 'complete'. See perf_iterate_sb_cpu(). | |
2832 | */ | |
2833 | smp_store_release(&event->ctx, ctx); | |
2834 | ||
db0503e4 PZ |
2835 | /* |
2836 | * perf_event_attr::disabled events will not run and can be initialized | |
2837 | * without IPI. Except when this is the first event for the context, in | |
2838 | * that case we need the magic of the IPI to set ctx->is_active. | |
2839 | * | |
2840 | * The IOC_ENABLE that is sure to follow the creation of a disabled | |
2841 | * event will issue the IPI and reprogram the hardware. | |
2842 | */ | |
2843 | if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) { | |
2844 | raw_spin_lock_irq(&ctx->lock); | |
2845 | if (ctx->task == TASK_TOMBSTONE) { | |
2846 | raw_spin_unlock_irq(&ctx->lock); | |
2847 | return; | |
2848 | } | |
2849 | add_event_to_ctx(event, ctx); | |
2850 | raw_spin_unlock_irq(&ctx->lock); | |
2851 | return; | |
2852 | } | |
2853 | ||
a096309b PZ |
2854 | if (!task) { |
2855 | cpu_function_call(cpu, __perf_install_in_context, event); | |
2856 | return; | |
2857 | } | |
2858 | ||
2859 | /* | |
2860 | * Should not happen, we validate the ctx is still alive before calling. | |
2861 | */ | |
2862 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) | |
2863 | return; | |
2864 | ||
39a43640 PZ |
2865 | /* |
2866 | * Installing events is tricky because we cannot rely on ctx->is_active | |
2867 | * to be set in case this is the nr_events 0 -> 1 transition. | |
63cae12b PZ |
2868 | * |
2869 | * Instead we use task_curr(), which tells us if the task is running. | |
2870 | * However, since we use task_curr() outside of rq::lock, we can race | |
2871 | * against the actual state. This means the result can be wrong. | |
2872 | * | |
2873 | * If we get a false positive, we retry, this is harmless. | |
2874 | * | |
2875 | * If we get a false negative, things are complicated. If we are after | |
2876 | * perf_event_context_sched_in() ctx::lock will serialize us, and the | |
2877 | * value must be correct. If we're before, it doesn't matter since | |
2878 | * perf_event_context_sched_in() will program the counter. | |
2879 | * | |
2880 | * However, this hinges on the remote context switch having observed | |
2881 | * our task->perf_event_ctxp[] store, such that it will in fact take | |
2882 | * ctx::lock in perf_event_context_sched_in(). | |
2883 | * | |
2884 | * We do this by task_function_call(), if the IPI fails to hit the task | |
2885 | * we know any future context switch of task must see the | |
2886 | * perf_event_ctpx[] store. | |
39a43640 | 2887 | */ |
63cae12b | 2888 | |
63b6da39 | 2889 | /* |
63cae12b PZ |
2890 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
2891 | * task_cpu() load, such that if the IPI then does not find the task | |
2892 | * running, a future context switch of that task must observe the | |
2893 | * store. | |
63b6da39 | 2894 | */ |
63cae12b PZ |
2895 | smp_mb(); |
2896 | again: | |
2897 | if (!task_function_call(task, __perf_install_in_context, event)) | |
a096309b PZ |
2898 | return; |
2899 | ||
2900 | raw_spin_lock_irq(&ctx->lock); | |
2901 | task = ctx->task; | |
84c4e620 | 2902 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
a096309b PZ |
2903 | /* |
2904 | * Cannot happen because we already checked above (which also | |
2905 | * cannot happen), and we hold ctx->mutex, which serializes us | |
2906 | * against perf_event_exit_task_context(). | |
2907 | */ | |
63b6da39 PZ |
2908 | raw_spin_unlock_irq(&ctx->lock); |
2909 | return; | |
2910 | } | |
39a43640 | 2911 | /* |
63cae12b PZ |
2912 | * If the task is not running, ctx->lock will avoid it becoming so, |
2913 | * thus we can safely install the event. | |
39a43640 | 2914 | */ |
63cae12b PZ |
2915 | if (task_curr(task)) { |
2916 | raw_spin_unlock_irq(&ctx->lock); | |
2917 | goto again; | |
2918 | } | |
2919 | add_event_to_ctx(event, ctx); | |
2920 | raw_spin_unlock_irq(&ctx->lock); | |
0793a61d TG |
2921 | } |
2922 | ||
d859e29f | 2923 | /* |
cdd6c482 | 2924 | * Cross CPU call to enable a performance event |
d859e29f | 2925 | */ |
fae3fde6 PZ |
2926 | static void __perf_event_enable(struct perf_event *event, |
2927 | struct perf_cpu_context *cpuctx, | |
2928 | struct perf_event_context *ctx, | |
2929 | void *info) | |
04289bb9 | 2930 | { |
cdd6c482 | 2931 | struct perf_event *leader = event->group_leader; |
fae3fde6 | 2932 | struct perf_event_context *task_ctx; |
04289bb9 | 2933 | |
6e801e01 PZ |
2934 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2935 | event->state <= PERF_EVENT_STATE_ERROR) | |
fae3fde6 | 2936 | return; |
3cbed429 | 2937 | |
bd2afa49 PZ |
2938 | if (ctx->is_active) |
2939 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); | |
2940 | ||
0d3d73aa | 2941 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
33238c50 | 2942 | perf_cgroup_event_enable(event, ctx); |
04289bb9 | 2943 | |
fae3fde6 PZ |
2944 | if (!ctx->is_active) |
2945 | return; | |
2946 | ||
e5d1367f | 2947 | if (!event_filter_match(event)) { |
bd2afa49 | 2948 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
fae3fde6 | 2949 | return; |
e5d1367f | 2950 | } |
f4c4176f | 2951 | |
04289bb9 | 2952 | /* |
cdd6c482 | 2953 | * If the event is in a group and isn't the group leader, |
d859e29f | 2954 | * then don't put it on unless the group is on. |
04289bb9 | 2955 | */ |
bd2afa49 PZ |
2956 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
2957 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
fae3fde6 | 2958 | return; |
bd2afa49 | 2959 | } |
fe4b04fa | 2960 | |
fae3fde6 PZ |
2961 | task_ctx = cpuctx->task_ctx; |
2962 | if (ctx->task) | |
2963 | WARN_ON_ONCE(task_ctx != ctx); | |
d859e29f | 2964 | |
487f05e1 | 2965 | ctx_resched(cpuctx, task_ctx, get_event_type(event)); |
7b648018 PZ |
2966 | } |
2967 | ||
d859e29f | 2968 | /* |
788faab7 | 2969 | * Enable an event. |
c93f7669 | 2970 | * |
cdd6c482 IM |
2971 | * If event->ctx is a cloned context, callers must make sure that |
2972 | * every task struct that event->ctx->task could possibly point to | |
c93f7669 | 2973 | * remains valid. This condition is satisfied when called through |
cdd6c482 IM |
2974 | * perf_event_for_each_child or perf_event_for_each as described |
2975 | * for perf_event_disable. | |
d859e29f | 2976 | */ |
f63a8daa | 2977 | static void _perf_event_enable(struct perf_event *event) |
d859e29f | 2978 | { |
cdd6c482 | 2979 | struct perf_event_context *ctx = event->ctx; |
d859e29f | 2980 | |
7b648018 | 2981 | raw_spin_lock_irq(&ctx->lock); |
6e801e01 PZ |
2982 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
2983 | event->state < PERF_EVENT_STATE_ERROR) { | |
7b648018 | 2984 | raw_spin_unlock_irq(&ctx->lock); |
d859e29f PM |
2985 | return; |
2986 | } | |
2987 | ||
d859e29f | 2988 | /* |
cdd6c482 | 2989 | * If the event is in error state, clear that first. |
7b648018 PZ |
2990 | * |
2991 | * That way, if we see the event in error state below, we know that it | |
2992 | * has gone back into error state, as distinct from the task having | |
2993 | * been scheduled away before the cross-call arrived. | |
d859e29f | 2994 | */ |
cdd6c482 IM |
2995 | if (event->state == PERF_EVENT_STATE_ERROR) |
2996 | event->state = PERF_EVENT_STATE_OFF; | |
e625cce1 | 2997 | raw_spin_unlock_irq(&ctx->lock); |
fe4b04fa | 2998 | |
fae3fde6 | 2999 | event_function_call(event, __perf_event_enable, NULL); |
d859e29f | 3000 | } |
f63a8daa PZ |
3001 | |
3002 | /* | |
3003 | * See perf_event_disable(); | |
3004 | */ | |
3005 | void perf_event_enable(struct perf_event *event) | |
3006 | { | |
3007 | struct perf_event_context *ctx; | |
3008 | ||
3009 | ctx = perf_event_ctx_lock(event); | |
3010 | _perf_event_enable(event); | |
3011 | perf_event_ctx_unlock(event, ctx); | |
3012 | } | |
dcfce4a0 | 3013 | EXPORT_SYMBOL_GPL(perf_event_enable); |
d859e29f | 3014 | |
375637bc AS |
3015 | struct stop_event_data { |
3016 | struct perf_event *event; | |
3017 | unsigned int restart; | |
3018 | }; | |
3019 | ||
95ff4ca2 AS |
3020 | static int __perf_event_stop(void *info) |
3021 | { | |
375637bc AS |
3022 | struct stop_event_data *sd = info; |
3023 | struct perf_event *event = sd->event; | |
95ff4ca2 | 3024 | |
375637bc | 3025 | /* if it's already INACTIVE, do nothing */ |
95ff4ca2 AS |
3026 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
3027 | return 0; | |
3028 | ||
3029 | /* matches smp_wmb() in event_sched_in() */ | |
3030 | smp_rmb(); | |
3031 | ||
3032 | /* | |
3033 | * There is a window with interrupts enabled before we get here, | |
3034 | * so we need to check again lest we try to stop another CPU's event. | |
3035 | */ | |
3036 | if (READ_ONCE(event->oncpu) != smp_processor_id()) | |
3037 | return -EAGAIN; | |
3038 | ||
3039 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3040 | ||
375637bc AS |
3041 | /* |
3042 | * May race with the actual stop (through perf_pmu_output_stop()), | |
3043 | * but it is only used for events with AUX ring buffer, and such | |
3044 | * events will refuse to restart because of rb::aux_mmap_count==0, | |
3045 | * see comments in perf_aux_output_begin(). | |
3046 | * | |
788faab7 | 3047 | * Since this is happening on an event-local CPU, no trace is lost |
375637bc AS |
3048 | * while restarting. |
3049 | */ | |
3050 | if (sd->restart) | |
c9bbdd48 | 3051 | event->pmu->start(event, 0); |
375637bc | 3052 | |
95ff4ca2 AS |
3053 | return 0; |
3054 | } | |
3055 | ||
767ae086 | 3056 | static int perf_event_stop(struct perf_event *event, int restart) |
375637bc AS |
3057 | { |
3058 | struct stop_event_data sd = { | |
3059 | .event = event, | |
767ae086 | 3060 | .restart = restart, |
375637bc AS |
3061 | }; |
3062 | int ret = 0; | |
3063 | ||
3064 | do { | |
3065 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) | |
3066 | return 0; | |
3067 | ||
3068 | /* matches smp_wmb() in event_sched_in() */ | |
3069 | smp_rmb(); | |
3070 | ||
3071 | /* | |
3072 | * We only want to restart ACTIVE events, so if the event goes | |
3073 | * inactive here (event->oncpu==-1), there's nothing more to do; | |
3074 | * fall through with ret==-ENXIO. | |
3075 | */ | |
3076 | ret = cpu_function_call(READ_ONCE(event->oncpu), | |
3077 | __perf_event_stop, &sd); | |
3078 | } while (ret == -EAGAIN); | |
3079 | ||
3080 | return ret; | |
3081 | } | |
3082 | ||
3083 | /* | |
3084 | * In order to contain the amount of racy and tricky in the address filter | |
3085 | * configuration management, it is a two part process: | |
3086 | * | |
3087 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, | |
3088 | * we update the addresses of corresponding vmas in | |
c60f83b8 | 3089 | * event::addr_filter_ranges array and bump the event::addr_filters_gen; |
375637bc AS |
3090 | * (p2) when an event is scheduled in (pmu::add), it calls |
3091 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() | |
3092 | * if the generation has changed since the previous call. | |
3093 | * | |
3094 | * If (p1) happens while the event is active, we restart it to force (p2). | |
3095 | * | |
3096 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on | |
3097 | * pre-existing mappings, called once when new filters arrive via SET_FILTER | |
3098 | * ioctl; | |
3099 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly | |
c1e8d7c6 | 3100 | * registered mapping, called for every new mmap(), with mm::mmap_lock down |
375637bc AS |
3101 | * for reading; |
3102 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process | |
3103 | * of exec. | |
3104 | */ | |
3105 | void perf_event_addr_filters_sync(struct perf_event *event) | |
3106 | { | |
3107 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
3108 | ||
3109 | if (!has_addr_filter(event)) | |
3110 | return; | |
3111 | ||
3112 | raw_spin_lock(&ifh->lock); | |
3113 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { | |
3114 | event->pmu->addr_filters_sync(event); | |
3115 | event->hw.addr_filters_gen = event->addr_filters_gen; | |
3116 | } | |
3117 | raw_spin_unlock(&ifh->lock); | |
3118 | } | |
3119 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); | |
3120 | ||
f63a8daa | 3121 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
79f14641 | 3122 | { |
2023b359 | 3123 | /* |
cdd6c482 | 3124 | * not supported on inherited events |
2023b359 | 3125 | */ |
2e939d1d | 3126 | if (event->attr.inherit || !is_sampling_event(event)) |
2023b359 PZ |
3127 | return -EINVAL; |
3128 | ||
cdd6c482 | 3129 | atomic_add(refresh, &event->event_limit); |
f63a8daa | 3130 | _perf_event_enable(event); |
2023b359 PZ |
3131 | |
3132 | return 0; | |
79f14641 | 3133 | } |
f63a8daa PZ |
3134 | |
3135 | /* | |
3136 | * See perf_event_disable() | |
3137 | */ | |
3138 | int perf_event_refresh(struct perf_event *event, int refresh) | |
3139 | { | |
3140 | struct perf_event_context *ctx; | |
3141 | int ret; | |
3142 | ||
3143 | ctx = perf_event_ctx_lock(event); | |
3144 | ret = _perf_event_refresh(event, refresh); | |
3145 | perf_event_ctx_unlock(event, ctx); | |
3146 | ||
3147 | return ret; | |
3148 | } | |
26ca5c11 | 3149 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
79f14641 | 3150 | |
32ff77e8 MC |
3151 | static int perf_event_modify_breakpoint(struct perf_event *bp, |
3152 | struct perf_event_attr *attr) | |
3153 | { | |
3154 | int err; | |
3155 | ||
3156 | _perf_event_disable(bp); | |
3157 | ||
3158 | err = modify_user_hw_breakpoint_check(bp, attr, true); | |
32ff77e8 | 3159 | |
bf06278c | 3160 | if (!bp->attr.disabled) |
32ff77e8 | 3161 | _perf_event_enable(bp); |
bf06278c JO |
3162 | |
3163 | return err; | |
32ff77e8 MC |
3164 | } |
3165 | ||
3166 | static int perf_event_modify_attr(struct perf_event *event, | |
3167 | struct perf_event_attr *attr) | |
3168 | { | |
3169 | if (event->attr.type != attr->type) | |
3170 | return -EINVAL; | |
3171 | ||
3172 | switch (event->attr.type) { | |
3173 | case PERF_TYPE_BREAKPOINT: | |
3174 | return perf_event_modify_breakpoint(event, attr); | |
3175 | default: | |
3176 | /* Place holder for future additions. */ | |
3177 | return -EOPNOTSUPP; | |
3178 | } | |
3179 | } | |
3180 | ||
5b0311e1 FW |
3181 | static void ctx_sched_out(struct perf_event_context *ctx, |
3182 | struct perf_cpu_context *cpuctx, | |
3183 | enum event_type_t event_type) | |
235c7fc7 | 3184 | { |
6668128a | 3185 | struct perf_event *event, *tmp; |
db24d33e | 3186 | int is_active = ctx->is_active; |
235c7fc7 | 3187 | |
c994d613 | 3188 | lockdep_assert_held(&ctx->lock); |
235c7fc7 | 3189 | |
39a43640 PZ |
3190 | if (likely(!ctx->nr_events)) { |
3191 | /* | |
3192 | * See __perf_remove_from_context(). | |
3193 | */ | |
3194 | WARN_ON_ONCE(ctx->is_active); | |
3195 | if (ctx->task) | |
3196 | WARN_ON_ONCE(cpuctx->task_ctx); | |
facc4307 | 3197 | return; |
39a43640 PZ |
3198 | } |
3199 | ||
db24d33e | 3200 | ctx->is_active &= ~event_type; |
3cbaa590 PZ |
3201 | if (!(ctx->is_active & EVENT_ALL)) |
3202 | ctx->is_active = 0; | |
3203 | ||
63e30d3e PZ |
3204 | if (ctx->task) { |
3205 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3206 | if (!ctx->is_active) | |
3207 | cpuctx->task_ctx = NULL; | |
3208 | } | |
facc4307 | 3209 | |
8fdc6539 PZ |
3210 | /* |
3211 | * Always update time if it was set; not only when it changes. | |
3212 | * Otherwise we can 'forget' to update time for any but the last | |
3213 | * context we sched out. For example: | |
3214 | * | |
3215 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) | |
3216 | * ctx_sched_out(.event_type = EVENT_PINNED) | |
3217 | * | |
3218 | * would only update time for the pinned events. | |
3219 | */ | |
3cbaa590 PZ |
3220 | if (is_active & EVENT_TIME) { |
3221 | /* update (and stop) ctx time */ | |
3222 | update_context_time(ctx); | |
3223 | update_cgrp_time_from_cpuctx(cpuctx); | |
3224 | } | |
3225 | ||
8fdc6539 PZ |
3226 | is_active ^= ctx->is_active; /* changed bits */ |
3227 | ||
3cbaa590 | 3228 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
facc4307 | 3229 | return; |
5b0311e1 | 3230 | |
075e0b00 | 3231 | perf_pmu_disable(ctx->pmu); |
3cbaa590 | 3232 | if (is_active & EVENT_PINNED) { |
6668128a | 3233 | list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) |
889ff015 | 3234 | group_sched_out(event, cpuctx, ctx); |
9ed6060d | 3235 | } |
889ff015 | 3236 | |
3cbaa590 | 3237 | if (is_active & EVENT_FLEXIBLE) { |
6668128a | 3238 | list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list) |
8c9ed8e1 | 3239 | group_sched_out(event, cpuctx, ctx); |
90c91dfb PZ |
3240 | |
3241 | /* | |
3242 | * Since we cleared EVENT_FLEXIBLE, also clear | |
3243 | * rotate_necessary, is will be reset by | |
3244 | * ctx_flexible_sched_in() when needed. | |
3245 | */ | |
3246 | ctx->rotate_necessary = 0; | |
9ed6060d | 3247 | } |
1b9a644f | 3248 | perf_pmu_enable(ctx->pmu); |
235c7fc7 IM |
3249 | } |
3250 | ||
564c2b21 | 3251 | /* |
5a3126d4 PZ |
3252 | * Test whether two contexts are equivalent, i.e. whether they have both been |
3253 | * cloned from the same version of the same context. | |
3254 | * | |
3255 | * Equivalence is measured using a generation number in the context that is | |
3256 | * incremented on each modification to it; see unclone_ctx(), list_add_event() | |
3257 | * and list_del_event(). | |
564c2b21 | 3258 | */ |
cdd6c482 IM |
3259 | static int context_equiv(struct perf_event_context *ctx1, |
3260 | struct perf_event_context *ctx2) | |
564c2b21 | 3261 | { |
211de6eb PZ |
3262 | lockdep_assert_held(&ctx1->lock); |
3263 | lockdep_assert_held(&ctx2->lock); | |
3264 | ||
5a3126d4 PZ |
3265 | /* Pinning disables the swap optimization */ |
3266 | if (ctx1->pin_count || ctx2->pin_count) | |
3267 | return 0; | |
3268 | ||
3269 | /* If ctx1 is the parent of ctx2 */ | |
3270 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) | |
3271 | return 1; | |
3272 | ||
3273 | /* If ctx2 is the parent of ctx1 */ | |
3274 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) | |
3275 | return 1; | |
3276 | ||
3277 | /* | |
3278 | * If ctx1 and ctx2 have the same parent; we flatten the parent | |
3279 | * hierarchy, see perf_event_init_context(). | |
3280 | */ | |
3281 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && | |
3282 | ctx1->parent_gen == ctx2->parent_gen) | |
3283 | return 1; | |
3284 | ||
3285 | /* Unmatched */ | |
3286 | return 0; | |
564c2b21 PM |
3287 | } |
3288 | ||
cdd6c482 IM |
3289 | static void __perf_event_sync_stat(struct perf_event *event, |
3290 | struct perf_event *next_event) | |
bfbd3381 PZ |
3291 | { |
3292 | u64 value; | |
3293 | ||
cdd6c482 | 3294 | if (!event->attr.inherit_stat) |
bfbd3381 PZ |
3295 | return; |
3296 | ||
3297 | /* | |
cdd6c482 | 3298 | * Update the event value, we cannot use perf_event_read() |
bfbd3381 PZ |
3299 | * because we're in the middle of a context switch and have IRQs |
3300 | * disabled, which upsets smp_call_function_single(), however | |
cdd6c482 | 3301 | * we know the event must be on the current CPU, therefore we |
bfbd3381 PZ |
3302 | * don't need to use it. |
3303 | */ | |
0d3d73aa | 3304 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
3dbebf15 | 3305 | event->pmu->read(event); |
bfbd3381 | 3306 | |
0d3d73aa | 3307 | perf_event_update_time(event); |
bfbd3381 PZ |
3308 | |
3309 | /* | |
cdd6c482 | 3310 | * In order to keep per-task stats reliable we need to flip the event |
bfbd3381 PZ |
3311 | * values when we flip the contexts. |
3312 | */ | |
e7850595 PZ |
3313 | value = local64_read(&next_event->count); |
3314 | value = local64_xchg(&event->count, value); | |
3315 | local64_set(&next_event->count, value); | |
bfbd3381 | 3316 | |
cdd6c482 IM |
3317 | swap(event->total_time_enabled, next_event->total_time_enabled); |
3318 | swap(event->total_time_running, next_event->total_time_running); | |
19d2e755 | 3319 | |
bfbd3381 | 3320 | /* |
19d2e755 | 3321 | * Since we swizzled the values, update the user visible data too. |
bfbd3381 | 3322 | */ |
cdd6c482 IM |
3323 | perf_event_update_userpage(event); |
3324 | perf_event_update_userpage(next_event); | |
bfbd3381 PZ |
3325 | } |
3326 | ||
cdd6c482 IM |
3327 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
3328 | struct perf_event_context *next_ctx) | |
bfbd3381 | 3329 | { |
cdd6c482 | 3330 | struct perf_event *event, *next_event; |
bfbd3381 PZ |
3331 | |
3332 | if (!ctx->nr_stat) | |
3333 | return; | |
3334 | ||
02ffdbc8 PZ |
3335 | update_context_time(ctx); |
3336 | ||
cdd6c482 IM |
3337 | event = list_first_entry(&ctx->event_list, |
3338 | struct perf_event, event_entry); | |
bfbd3381 | 3339 | |
cdd6c482 IM |
3340 | next_event = list_first_entry(&next_ctx->event_list, |
3341 | struct perf_event, event_entry); | |
bfbd3381 | 3342 | |
cdd6c482 IM |
3343 | while (&event->event_entry != &ctx->event_list && |
3344 | &next_event->event_entry != &next_ctx->event_list) { | |
bfbd3381 | 3345 | |
cdd6c482 | 3346 | __perf_event_sync_stat(event, next_event); |
bfbd3381 | 3347 | |
cdd6c482 IM |
3348 | event = list_next_entry(event, event_entry); |
3349 | next_event = list_next_entry(next_event, event_entry); | |
bfbd3381 PZ |
3350 | } |
3351 | } | |
3352 | ||
fe4b04fa PZ |
3353 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
3354 | struct task_struct *next) | |
0793a61d | 3355 | { |
8dc85d54 | 3356 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
cdd6c482 | 3357 | struct perf_event_context *next_ctx; |
5a3126d4 | 3358 | struct perf_event_context *parent, *next_parent; |
108b02cf | 3359 | struct perf_cpu_context *cpuctx; |
c93f7669 | 3360 | int do_switch = 1; |
0793a61d | 3361 | |
108b02cf PZ |
3362 | if (likely(!ctx)) |
3363 | return; | |
10989fb2 | 3364 | |
108b02cf PZ |
3365 | cpuctx = __get_cpu_context(ctx); |
3366 | if (!cpuctx->task_ctx) | |
0793a61d TG |
3367 | return; |
3368 | ||
c93f7669 | 3369 | rcu_read_lock(); |
8dc85d54 | 3370 | next_ctx = next->perf_event_ctxp[ctxn]; |
5a3126d4 PZ |
3371 | if (!next_ctx) |
3372 | goto unlock; | |
3373 | ||
3374 | parent = rcu_dereference(ctx->parent_ctx); | |
3375 | next_parent = rcu_dereference(next_ctx->parent_ctx); | |
3376 | ||
3377 | /* If neither context have a parent context; they cannot be clones. */ | |
802c8a61 | 3378 | if (!parent && !next_parent) |
5a3126d4 PZ |
3379 | goto unlock; |
3380 | ||
3381 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { | |
c93f7669 PM |
3382 | /* |
3383 | * Looks like the two contexts are clones, so we might be | |
3384 | * able to optimize the context switch. We lock both | |
3385 | * contexts and check that they are clones under the | |
3386 | * lock (including re-checking that neither has been | |
3387 | * uncloned in the meantime). It doesn't matter which | |
3388 | * order we take the locks because no other cpu could | |
3389 | * be trying to lock both of these tasks. | |
3390 | */ | |
e625cce1 TG |
3391 | raw_spin_lock(&ctx->lock); |
3392 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); | |
c93f7669 | 3393 | if (context_equiv(ctx, next_ctx)) { |
c2b98a86 AB |
3394 | struct pmu *pmu = ctx->pmu; |
3395 | ||
63b6da39 PZ |
3396 | WRITE_ONCE(ctx->task, next); |
3397 | WRITE_ONCE(next_ctx->task, task); | |
5a158c3c | 3398 | |
c2b98a86 AB |
3399 | /* |
3400 | * PMU specific parts of task perf context can require | |
3401 | * additional synchronization. As an example of such | |
3402 | * synchronization see implementation details of Intel | |
3403 | * LBR call stack data profiling; | |
3404 | */ | |
3405 | if (pmu->swap_task_ctx) | |
3406 | pmu->swap_task_ctx(ctx, next_ctx); | |
3407 | else | |
3408 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); | |
5a158c3c | 3409 | |
63b6da39 PZ |
3410 | /* |
3411 | * RCU_INIT_POINTER here is safe because we've not | |
3412 | * modified the ctx and the above modification of | |
3413 | * ctx->task and ctx->task_ctx_data are immaterial | |
3414 | * since those values are always verified under | |
3415 | * ctx->lock which we're now holding. | |
3416 | */ | |
3417 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); | |
3418 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); | |
3419 | ||
c93f7669 | 3420 | do_switch = 0; |
bfbd3381 | 3421 | |
cdd6c482 | 3422 | perf_event_sync_stat(ctx, next_ctx); |
c93f7669 | 3423 | } |
e625cce1 TG |
3424 | raw_spin_unlock(&next_ctx->lock); |
3425 | raw_spin_unlock(&ctx->lock); | |
564c2b21 | 3426 | } |
5a3126d4 | 3427 | unlock: |
c93f7669 | 3428 | rcu_read_unlock(); |
564c2b21 | 3429 | |
c93f7669 | 3430 | if (do_switch) { |
facc4307 | 3431 | raw_spin_lock(&ctx->lock); |
487f05e1 | 3432 | task_ctx_sched_out(cpuctx, ctx, EVENT_ALL); |
facc4307 | 3433 | raw_spin_unlock(&ctx->lock); |
c93f7669 | 3434 | } |
0793a61d TG |
3435 | } |
3436 | ||
e48c1788 PZ |
3437 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
3438 | ||
ba532500 YZ |
3439 | void perf_sched_cb_dec(struct pmu *pmu) |
3440 | { | |
e48c1788 PZ |
3441 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
3442 | ||
ba532500 | 3443 | this_cpu_dec(perf_sched_cb_usages); |
e48c1788 PZ |
3444 | |
3445 | if (!--cpuctx->sched_cb_usage) | |
3446 | list_del(&cpuctx->sched_cb_entry); | |
ba532500 YZ |
3447 | } |
3448 | ||
e48c1788 | 3449 | |
ba532500 YZ |
3450 | void perf_sched_cb_inc(struct pmu *pmu) |
3451 | { | |
e48c1788 PZ |
3452 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
3453 | ||
3454 | if (!cpuctx->sched_cb_usage++) | |
3455 | list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); | |
3456 | ||
ba532500 YZ |
3457 | this_cpu_inc(perf_sched_cb_usages); |
3458 | } | |
3459 | ||
3460 | /* | |
3461 | * This function provides the context switch callback to the lower code | |
3462 | * layer. It is invoked ONLY when the context switch callback is enabled. | |
09e61b4f PZ |
3463 | * |
3464 | * This callback is relevant even to per-cpu events; for example multi event | |
3465 | * PEBS requires this to provide PID/TID information. This requires we flush | |
3466 | * all queued PEBS records before we context switch to a new task. | |
ba532500 YZ |
3467 | */ |
3468 | static void perf_pmu_sched_task(struct task_struct *prev, | |
3469 | struct task_struct *next, | |
3470 | bool sched_in) | |
3471 | { | |
3472 | struct perf_cpu_context *cpuctx; | |
3473 | struct pmu *pmu; | |
ba532500 YZ |
3474 | |
3475 | if (prev == next) | |
3476 | return; | |
3477 | ||
e48c1788 | 3478 | list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) { |
1fd7e416 | 3479 | pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */ |
ba532500 | 3480 | |
e48c1788 PZ |
3481 | if (WARN_ON_ONCE(!pmu->sched_task)) |
3482 | continue; | |
ba532500 | 3483 | |
e48c1788 PZ |
3484 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
3485 | perf_pmu_disable(pmu); | |
ba532500 | 3486 | |
e48c1788 | 3487 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
ba532500 | 3488 | |
e48c1788 PZ |
3489 | perf_pmu_enable(pmu); |
3490 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
ba532500 | 3491 | } |
ba532500 YZ |
3492 | } |
3493 | ||
45ac1403 AH |
3494 | static void perf_event_switch(struct task_struct *task, |
3495 | struct task_struct *next_prev, bool sched_in); | |
3496 | ||
8dc85d54 PZ |
3497 | #define for_each_task_context_nr(ctxn) \ |
3498 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) | |
3499 | ||
3500 | /* | |
3501 | * Called from scheduler to remove the events of the current task, | |
3502 | * with interrupts disabled. | |
3503 | * | |
3504 | * We stop each event and update the event value in event->count. | |
3505 | * | |
3506 | * This does not protect us against NMI, but disable() | |
3507 | * sets the disabled bit in the control field of event _before_ | |
3508 | * accessing the event control register. If a NMI hits, then it will | |
3509 | * not restart the event. | |
3510 | */ | |
ab0cce56 JO |
3511 | void __perf_event_task_sched_out(struct task_struct *task, |
3512 | struct task_struct *next) | |
8dc85d54 PZ |
3513 | { |
3514 | int ctxn; | |
3515 | ||
ba532500 YZ |
3516 | if (__this_cpu_read(perf_sched_cb_usages)) |
3517 | perf_pmu_sched_task(task, next, false); | |
3518 | ||
45ac1403 AH |
3519 | if (atomic_read(&nr_switch_events)) |
3520 | perf_event_switch(task, next, false); | |
3521 | ||
8dc85d54 PZ |
3522 | for_each_task_context_nr(ctxn) |
3523 | perf_event_context_sched_out(task, ctxn, next); | |
e5d1367f SE |
3524 | |
3525 | /* | |
3526 | * if cgroup events exist on this CPU, then we need | |
3527 | * to check if we have to switch out PMU state. | |
3528 | * cgroup event are system-wide mode only | |
3529 | */ | |
4a32fea9 | 3530 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
a8d757ef | 3531 | perf_cgroup_sched_out(task, next); |
8dc85d54 PZ |
3532 | } |
3533 | ||
5b0311e1 FW |
3534 | /* |
3535 | * Called with IRQs disabled | |
3536 | */ | |
3537 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, | |
3538 | enum event_type_t event_type) | |
3539 | { | |
3540 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); | |
04289bb9 IM |
3541 | } |
3542 | ||
6eef8a71 | 3543 | static bool perf_less_group_idx(const void *l, const void *r) |
0793a61d | 3544 | { |
24fb6b8e IR |
3545 | const struct perf_event *le = *(const struct perf_event **)l; |
3546 | const struct perf_event *re = *(const struct perf_event **)r; | |
6eef8a71 IR |
3547 | |
3548 | return le->group_index < re->group_index; | |
3549 | } | |
3550 | ||
3551 | static void swap_ptr(void *l, void *r) | |
3552 | { | |
3553 | void **lp = l, **rp = r; | |
3554 | ||
3555 | swap(*lp, *rp); | |
3556 | } | |
3557 | ||
3558 | static const struct min_heap_callbacks perf_min_heap = { | |
3559 | .elem_size = sizeof(struct perf_event *), | |
3560 | .less = perf_less_group_idx, | |
3561 | .swp = swap_ptr, | |
3562 | }; | |
3563 | ||
3564 | static void __heap_add(struct min_heap *heap, struct perf_event *event) | |
3565 | { | |
3566 | struct perf_event **itrs = heap->data; | |
3567 | ||
3568 | if (event) { | |
3569 | itrs[heap->nr] = event; | |
3570 | heap->nr++; | |
3571 | } | |
3572 | } | |
3573 | ||
836196be IR |
3574 | static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx, |
3575 | struct perf_event_groups *groups, int cpu, | |
6eef8a71 IR |
3576 | int (*func)(struct perf_event *, void *), |
3577 | void *data) | |
3578 | { | |
95ed6c70 IR |
3579 | #ifdef CONFIG_CGROUP_PERF |
3580 | struct cgroup_subsys_state *css = NULL; | |
3581 | #endif | |
6eef8a71 IR |
3582 | /* Space for per CPU and/or any CPU event iterators. */ |
3583 | struct perf_event *itrs[2]; | |
836196be IR |
3584 | struct min_heap event_heap; |
3585 | struct perf_event **evt; | |
1cac7b1a | 3586 | int ret; |
8e1a2031 | 3587 | |
836196be IR |
3588 | if (cpuctx) { |
3589 | event_heap = (struct min_heap){ | |
3590 | .data = cpuctx->heap, | |
3591 | .nr = 0, | |
3592 | .size = cpuctx->heap_size, | |
3593 | }; | |
c2283c93 IR |
3594 | |
3595 | lockdep_assert_held(&cpuctx->ctx.lock); | |
95ed6c70 IR |
3596 | |
3597 | #ifdef CONFIG_CGROUP_PERF | |
3598 | if (cpuctx->cgrp) | |
3599 | css = &cpuctx->cgrp->css; | |
3600 | #endif | |
836196be IR |
3601 | } else { |
3602 | event_heap = (struct min_heap){ | |
3603 | .data = itrs, | |
3604 | .nr = 0, | |
3605 | .size = ARRAY_SIZE(itrs), | |
3606 | }; | |
3607 | /* Events not within a CPU context may be on any CPU. */ | |
95ed6c70 | 3608 | __heap_add(&event_heap, perf_event_groups_first(groups, -1, NULL)); |
836196be IR |
3609 | } |
3610 | evt = event_heap.data; | |
3611 | ||
95ed6c70 IR |
3612 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, NULL)); |
3613 | ||
3614 | #ifdef CONFIG_CGROUP_PERF | |
3615 | for (; css; css = css->parent) | |
3616 | __heap_add(&event_heap, perf_event_groups_first(groups, cpu, css->cgroup)); | |
3617 | #endif | |
1cac7b1a | 3618 | |
6eef8a71 | 3619 | min_heapify_all(&event_heap, &perf_min_heap); |
1cac7b1a | 3620 | |
6eef8a71 | 3621 | while (event_heap.nr) { |
1cac7b1a PZ |
3622 | ret = func(*evt, data); |
3623 | if (ret) | |
3624 | return ret; | |
3625 | ||
3626 | *evt = perf_event_groups_next(*evt); | |
6eef8a71 IR |
3627 | if (*evt) |
3628 | min_heapify(&event_heap, 0, &perf_min_heap); | |
3629 | else | |
3630 | min_heap_pop(&event_heap, &perf_min_heap); | |
8e1a2031 | 3631 | } |
0793a61d | 3632 | |
1cac7b1a PZ |
3633 | return 0; |
3634 | } | |
3635 | ||
ab6f824c | 3636 | static int merge_sched_in(struct perf_event *event, void *data) |
1cac7b1a | 3637 | { |
2c2366c7 PZ |
3638 | struct perf_event_context *ctx = event->ctx; |
3639 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
3640 | int *can_add_hw = data; | |
ab6f824c | 3641 | |
1cac7b1a PZ |
3642 | if (event->state <= PERF_EVENT_STATE_OFF) |
3643 | return 0; | |
3644 | ||
3645 | if (!event_filter_match(event)) | |
3646 | return 0; | |
3647 | ||
2c2366c7 PZ |
3648 | if (group_can_go_on(event, cpuctx, *can_add_hw)) { |
3649 | if (!group_sched_in(event, cpuctx, ctx)) | |
ab6f824c | 3650 | list_add_tail(&event->active_list, get_event_list(event)); |
6668128a | 3651 | } |
1cac7b1a | 3652 | |
ab6f824c | 3653 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
33238c50 PZ |
3654 | if (event->attr.pinned) { |
3655 | perf_cgroup_event_disable(event, ctx); | |
ab6f824c | 3656 | perf_event_set_state(event, PERF_EVENT_STATE_ERROR); |
33238c50 | 3657 | } |
1cac7b1a | 3658 | |
2c2366c7 PZ |
3659 | *can_add_hw = 0; |
3660 | ctx->rotate_necessary = 1; | |
3b6f9e5c | 3661 | } |
1cac7b1a PZ |
3662 | |
3663 | return 0; | |
5b0311e1 FW |
3664 | } |
3665 | ||
3666 | static void | |
1cac7b1a PZ |
3667 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
3668 | struct perf_cpu_context *cpuctx) | |
5b0311e1 | 3669 | { |
2c2366c7 | 3670 | int can_add_hw = 1; |
3b6f9e5c | 3671 | |
836196be IR |
3672 | if (ctx != &cpuctx->ctx) |
3673 | cpuctx = NULL; | |
3674 | ||
3675 | visit_groups_merge(cpuctx, &ctx->pinned_groups, | |
1cac7b1a | 3676 | smp_processor_id(), |
2c2366c7 | 3677 | merge_sched_in, &can_add_hw); |
1cac7b1a | 3678 | } |
8e1a2031 | 3679 | |
1cac7b1a PZ |
3680 | static void |
3681 | ctx_flexible_sched_in(struct perf_event_context *ctx, | |
3682 | struct perf_cpu_context *cpuctx) | |
3683 | { | |
2c2366c7 | 3684 | int can_add_hw = 1; |
0793a61d | 3685 | |
836196be IR |
3686 | if (ctx != &cpuctx->ctx) |
3687 | cpuctx = NULL; | |
3688 | ||
3689 | visit_groups_merge(cpuctx, &ctx->flexible_groups, | |
1cac7b1a | 3690 | smp_processor_id(), |
2c2366c7 | 3691 | merge_sched_in, &can_add_hw); |
5b0311e1 FW |
3692 | } |
3693 | ||
3694 | static void | |
3695 | ctx_sched_in(struct perf_event_context *ctx, | |
3696 | struct perf_cpu_context *cpuctx, | |
e5d1367f SE |
3697 | enum event_type_t event_type, |
3698 | struct task_struct *task) | |
5b0311e1 | 3699 | { |
db24d33e | 3700 | int is_active = ctx->is_active; |
c994d613 PZ |
3701 | u64 now; |
3702 | ||
3703 | lockdep_assert_held(&ctx->lock); | |
e5d1367f | 3704 | |
5b0311e1 | 3705 | if (likely(!ctx->nr_events)) |
facc4307 | 3706 | return; |
5b0311e1 | 3707 | |
3cbaa590 | 3708 | ctx->is_active |= (event_type | EVENT_TIME); |
63e30d3e PZ |
3709 | if (ctx->task) { |
3710 | if (!is_active) | |
3711 | cpuctx->task_ctx = ctx; | |
3712 | else | |
3713 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); | |
3714 | } | |
3715 | ||
3cbaa590 PZ |
3716 | is_active ^= ctx->is_active; /* changed bits */ |
3717 | ||
3718 | if (is_active & EVENT_TIME) { | |
3719 | /* start ctx time */ | |
3720 | now = perf_clock(); | |
3721 | ctx->timestamp = now; | |
3722 | perf_cgroup_set_timestamp(task, ctx); | |
3723 | } | |
3724 | ||
5b0311e1 FW |
3725 | /* |
3726 | * First go through the list and put on any pinned groups | |
3727 | * in order to give them the best chance of going on. | |
3728 | */ | |
3cbaa590 | 3729 | if (is_active & EVENT_PINNED) |
6e37738a | 3730 | ctx_pinned_sched_in(ctx, cpuctx); |
5b0311e1 FW |
3731 | |
3732 | /* Then walk through the lower prio flexible groups */ | |
3cbaa590 | 3733 | if (is_active & EVENT_FLEXIBLE) |
6e37738a | 3734 | ctx_flexible_sched_in(ctx, cpuctx); |
235c7fc7 IM |
3735 | } |
3736 | ||
329c0e01 | 3737 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
e5d1367f SE |
3738 | enum event_type_t event_type, |
3739 | struct task_struct *task) | |
329c0e01 FW |
3740 | { |
3741 | struct perf_event_context *ctx = &cpuctx->ctx; | |
3742 | ||
e5d1367f | 3743 | ctx_sched_in(ctx, cpuctx, event_type, task); |
329c0e01 FW |
3744 | } |
3745 | ||
e5d1367f SE |
3746 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
3747 | struct task_struct *task) | |
235c7fc7 | 3748 | { |
108b02cf | 3749 | struct perf_cpu_context *cpuctx; |
235c7fc7 | 3750 | |
108b02cf | 3751 | cpuctx = __get_cpu_context(ctx); |
329c0e01 FW |
3752 | if (cpuctx->task_ctx == ctx) |
3753 | return; | |
3754 | ||
facc4307 | 3755 | perf_ctx_lock(cpuctx, ctx); |
fdccc3fb | 3756 | /* |
3757 | * We must check ctx->nr_events while holding ctx->lock, such | |
3758 | * that we serialize against perf_install_in_context(). | |
3759 | */ | |
3760 | if (!ctx->nr_events) | |
3761 | goto unlock; | |
3762 | ||
1b9a644f | 3763 | perf_pmu_disable(ctx->pmu); |
329c0e01 FW |
3764 | /* |
3765 | * We want to keep the following priority order: | |
3766 | * cpu pinned (that don't need to move), task pinned, | |
3767 | * cpu flexible, task flexible. | |
fe45bafb AS |
3768 | * |
3769 | * However, if task's ctx is not carrying any pinned | |
3770 | * events, no need to flip the cpuctx's events around. | |
329c0e01 | 3771 | */ |
8e1a2031 | 3772 | if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) |
fe45bafb | 3773 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
63e30d3e | 3774 | perf_event_sched_in(cpuctx, ctx, task); |
facc4307 | 3775 | perf_pmu_enable(ctx->pmu); |
fdccc3fb | 3776 | |
3777 | unlock: | |
facc4307 | 3778 | perf_ctx_unlock(cpuctx, ctx); |
235c7fc7 IM |
3779 | } |
3780 | ||
8dc85d54 PZ |
3781 | /* |
3782 | * Called from scheduler to add the events of the current task | |
3783 | * with interrupts disabled. | |
3784 | * | |
3785 | * We restore the event value and then enable it. | |
3786 | * | |
3787 | * This does not protect us against NMI, but enable() | |
3788 | * sets the enabled bit in the control field of event _before_ | |
3789 | * accessing the event control register. If a NMI hits, then it will | |
3790 | * keep the event running. | |
3791 | */ | |
ab0cce56 JO |
3792 | void __perf_event_task_sched_in(struct task_struct *prev, |
3793 | struct task_struct *task) | |
8dc85d54 PZ |
3794 | { |
3795 | struct perf_event_context *ctx; | |
3796 | int ctxn; | |
3797 | ||
7e41d177 PZ |
3798 | /* |
3799 | * If cgroup events exist on this CPU, then we need to check if we have | |
3800 | * to switch in PMU state; cgroup event are system-wide mode only. | |
3801 | * | |
3802 | * Since cgroup events are CPU events, we must schedule these in before | |
3803 | * we schedule in the task events. | |
3804 | */ | |
3805 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) | |
3806 | perf_cgroup_sched_in(prev, task); | |
3807 | ||
8dc85d54 PZ |
3808 | for_each_task_context_nr(ctxn) { |
3809 | ctx = task->perf_event_ctxp[ctxn]; | |
3810 | if (likely(!ctx)) | |
3811 | continue; | |
3812 | ||
e5d1367f | 3813 | perf_event_context_sched_in(ctx, task); |
8dc85d54 | 3814 | } |
d010b332 | 3815 | |
45ac1403 AH |
3816 | if (atomic_read(&nr_switch_events)) |
3817 | perf_event_switch(task, prev, true); | |
3818 | ||
ba532500 YZ |
3819 | if (__this_cpu_read(perf_sched_cb_usages)) |
3820 | perf_pmu_sched_task(prev, task, true); | |
235c7fc7 IM |
3821 | } |
3822 | ||
abd50713 PZ |
3823 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
3824 | { | |
3825 | u64 frequency = event->attr.sample_freq; | |
3826 | u64 sec = NSEC_PER_SEC; | |
3827 | u64 divisor, dividend; | |
3828 | ||
3829 | int count_fls, nsec_fls, frequency_fls, sec_fls; | |
3830 | ||
3831 | count_fls = fls64(count); | |
3832 | nsec_fls = fls64(nsec); | |
3833 | frequency_fls = fls64(frequency); | |
3834 | sec_fls = 30; | |
3835 | ||
3836 | /* | |
3837 | * We got @count in @nsec, with a target of sample_freq HZ | |
3838 | * the target period becomes: | |
3839 | * | |
3840 | * @count * 10^9 | |
3841 | * period = ------------------- | |
3842 | * @nsec * sample_freq | |
3843 | * | |
3844 | */ | |
3845 | ||
3846 | /* | |
3847 | * Reduce accuracy by one bit such that @a and @b converge | |
3848 | * to a similar magnitude. | |
3849 | */ | |
fe4b04fa | 3850 | #define REDUCE_FLS(a, b) \ |
abd50713 PZ |
3851 | do { \ |
3852 | if (a##_fls > b##_fls) { \ | |
3853 | a >>= 1; \ | |
3854 | a##_fls--; \ | |
3855 | } else { \ | |
3856 | b >>= 1; \ | |
3857 | b##_fls--; \ | |
3858 | } \ | |
3859 | } while (0) | |
3860 | ||
3861 | /* | |
3862 | * Reduce accuracy until either term fits in a u64, then proceed with | |
3863 | * the other, so that finally we can do a u64/u64 division. | |
3864 | */ | |
3865 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { | |
3866 | REDUCE_FLS(nsec, frequency); | |
3867 | REDUCE_FLS(sec, count); | |
3868 | } | |
3869 | ||
3870 | if (count_fls + sec_fls > 64) { | |
3871 | divisor = nsec * frequency; | |
3872 | ||
3873 | while (count_fls + sec_fls > 64) { | |
3874 | REDUCE_FLS(count, sec); | |
3875 | divisor >>= 1; | |
3876 | } | |
3877 | ||
3878 | dividend = count * sec; | |
3879 | } else { | |
3880 | dividend = count * sec; | |
3881 | ||
3882 | while (nsec_fls + frequency_fls > 64) { | |
3883 | REDUCE_FLS(nsec, frequency); | |
3884 | dividend >>= 1; | |
3885 | } | |
3886 | ||
3887 | divisor = nsec * frequency; | |
3888 | } | |
3889 | ||
f6ab91ad PZ |
3890 | if (!divisor) |
3891 | return dividend; | |
3892 | ||
abd50713 PZ |
3893 | return div64_u64(dividend, divisor); |
3894 | } | |
3895 | ||
e050e3f0 SE |
3896 | static DEFINE_PER_CPU(int, perf_throttled_count); |
3897 | static DEFINE_PER_CPU(u64, perf_throttled_seq); | |
3898 | ||
f39d47ff | 3899 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
bd2b5b12 | 3900 | { |
cdd6c482 | 3901 | struct hw_perf_event *hwc = &event->hw; |
f6ab91ad | 3902 | s64 period, sample_period; |
bd2b5b12 PZ |
3903 | s64 delta; |
3904 | ||
abd50713 | 3905 | period = perf_calculate_period(event, nsec, count); |
bd2b5b12 PZ |
3906 | |
3907 | delta = (s64)(period - hwc->sample_period); | |
3908 | delta = (delta + 7) / 8; /* low pass filter */ | |
3909 | ||
3910 | sample_period = hwc->sample_period + delta; | |
3911 | ||
3912 | if (!sample_period) | |
3913 | sample_period = 1; | |
3914 | ||
bd2b5b12 | 3915 | hwc->sample_period = sample_period; |
abd50713 | 3916 | |
e7850595 | 3917 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
f39d47ff SE |
3918 | if (disable) |
3919 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3920 | ||
e7850595 | 3921 | local64_set(&hwc->period_left, 0); |
f39d47ff SE |
3922 | |
3923 | if (disable) | |
3924 | event->pmu->start(event, PERF_EF_RELOAD); | |
abd50713 | 3925 | } |
bd2b5b12 PZ |
3926 | } |
3927 | ||
e050e3f0 SE |
3928 | /* |
3929 | * combine freq adjustment with unthrottling to avoid two passes over the | |
3930 | * events. At the same time, make sure, having freq events does not change | |
3931 | * the rate of unthrottling as that would introduce bias. | |
3932 | */ | |
3933 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, | |
3934 | int needs_unthr) | |
60db5e09 | 3935 | { |
cdd6c482 IM |
3936 | struct perf_event *event; |
3937 | struct hw_perf_event *hwc; | |
e050e3f0 | 3938 | u64 now, period = TICK_NSEC; |
abd50713 | 3939 | s64 delta; |
60db5e09 | 3940 | |
e050e3f0 SE |
3941 | /* |
3942 | * only need to iterate over all events iff: | |
3943 | * - context have events in frequency mode (needs freq adjust) | |
3944 | * - there are events to unthrottle on this cpu | |
3945 | */ | |
3946 | if (!(ctx->nr_freq || needs_unthr)) | |
0f5a2601 PZ |
3947 | return; |
3948 | ||
e050e3f0 | 3949 | raw_spin_lock(&ctx->lock); |
f39d47ff | 3950 | perf_pmu_disable(ctx->pmu); |
e050e3f0 | 3951 | |
03541f8b | 3952 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
cdd6c482 | 3953 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
60db5e09 PZ |
3954 | continue; |
3955 | ||
5632ab12 | 3956 | if (!event_filter_match(event)) |
5d27c23d PZ |
3957 | continue; |
3958 | ||
44377277 AS |
3959 | perf_pmu_disable(event->pmu); |
3960 | ||
cdd6c482 | 3961 | hwc = &event->hw; |
6a24ed6c | 3962 | |
ae23bff1 | 3963 | if (hwc->interrupts == MAX_INTERRUPTS) { |
e050e3f0 | 3964 | hwc->interrupts = 0; |
cdd6c482 | 3965 | perf_log_throttle(event, 1); |
a4eaf7f1 | 3966 | event->pmu->start(event, 0); |
a78ac325 PZ |
3967 | } |
3968 | ||
cdd6c482 | 3969 | if (!event->attr.freq || !event->attr.sample_freq) |
44377277 | 3970 | goto next; |
60db5e09 | 3971 | |
e050e3f0 SE |
3972 | /* |
3973 | * stop the event and update event->count | |
3974 | */ | |
3975 | event->pmu->stop(event, PERF_EF_UPDATE); | |
3976 | ||
e7850595 | 3977 | now = local64_read(&event->count); |
abd50713 PZ |
3978 | delta = now - hwc->freq_count_stamp; |
3979 | hwc->freq_count_stamp = now; | |
60db5e09 | 3980 | |
e050e3f0 SE |
3981 | /* |
3982 | * restart the event | |
3983 | * reload only if value has changed | |
f39d47ff SE |
3984 | * we have stopped the event so tell that |
3985 | * to perf_adjust_period() to avoid stopping it | |
3986 | * twice. | |
e050e3f0 | 3987 | */ |
abd50713 | 3988 | if (delta > 0) |
f39d47ff | 3989 | perf_adjust_period(event, period, delta, false); |
e050e3f0 SE |
3990 | |
3991 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); | |
44377277 AS |
3992 | next: |
3993 | perf_pmu_enable(event->pmu); | |
60db5e09 | 3994 | } |
e050e3f0 | 3995 | |
f39d47ff | 3996 | perf_pmu_enable(ctx->pmu); |
e050e3f0 | 3997 | raw_spin_unlock(&ctx->lock); |
60db5e09 PZ |
3998 | } |
3999 | ||
235c7fc7 | 4000 | /* |
8703a7cf | 4001 | * Move @event to the tail of the @ctx's elegible events. |
235c7fc7 | 4002 | */ |
8703a7cf | 4003 | static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event) |
0793a61d | 4004 | { |
dddd3379 TG |
4005 | /* |
4006 | * Rotate the first entry last of non-pinned groups. Rotation might be | |
4007 | * disabled by the inheritance code. | |
4008 | */ | |
8703a7cf PZ |
4009 | if (ctx->rotate_disable) |
4010 | return; | |
8e1a2031 | 4011 | |
8703a7cf PZ |
4012 | perf_event_groups_delete(&ctx->flexible_groups, event); |
4013 | perf_event_groups_insert(&ctx->flexible_groups, event); | |
235c7fc7 IM |
4014 | } |
4015 | ||
7fa343b7 | 4016 | /* pick an event from the flexible_groups to rotate */ |
8d5bce0c | 4017 | static inline struct perf_event * |
7fa343b7 | 4018 | ctx_event_to_rotate(struct perf_event_context *ctx) |
235c7fc7 | 4019 | { |
7fa343b7 SL |
4020 | struct perf_event *event; |
4021 | ||
4022 | /* pick the first active flexible event */ | |
4023 | event = list_first_entry_or_null(&ctx->flexible_active, | |
4024 | struct perf_event, active_list); | |
4025 | ||
4026 | /* if no active flexible event, pick the first event */ | |
4027 | if (!event) { | |
4028 | event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree), | |
4029 | typeof(*event), group_node); | |
4030 | } | |
4031 | ||
90c91dfb PZ |
4032 | /* |
4033 | * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in() | |
4034 | * finds there are unschedulable events, it will set it again. | |
4035 | */ | |
4036 | ctx->rotate_necessary = 0; | |
4037 | ||
7fa343b7 | 4038 | return event; |
8d5bce0c PZ |
4039 | } |
4040 | ||
4041 | static bool perf_rotate_context(struct perf_cpu_context *cpuctx) | |
4042 | { | |
4043 | struct perf_event *cpu_event = NULL, *task_event = NULL; | |
fd7d5517 IR |
4044 | struct perf_event_context *task_ctx = NULL; |
4045 | int cpu_rotate, task_rotate; | |
8d5bce0c PZ |
4046 | |
4047 | /* | |
4048 | * Since we run this from IRQ context, nobody can install new | |
4049 | * events, thus the event count values are stable. | |
4050 | */ | |
7fc23a53 | 4051 | |
fd7d5517 IR |
4052 | cpu_rotate = cpuctx->ctx.rotate_necessary; |
4053 | task_ctx = cpuctx->task_ctx; | |
4054 | task_rotate = task_ctx ? task_ctx->rotate_necessary : 0; | |
9717e6cd | 4055 | |
8d5bce0c PZ |
4056 | if (!(cpu_rotate || task_rotate)) |
4057 | return false; | |
0f5a2601 | 4058 | |
facc4307 | 4059 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
1b9a644f | 4060 | perf_pmu_disable(cpuctx->ctx.pmu); |
60db5e09 | 4061 | |
8d5bce0c | 4062 | if (task_rotate) |
7fa343b7 | 4063 | task_event = ctx_event_to_rotate(task_ctx); |
8d5bce0c | 4064 | if (cpu_rotate) |
7fa343b7 | 4065 | cpu_event = ctx_event_to_rotate(&cpuctx->ctx); |
8703a7cf | 4066 | |
8d5bce0c PZ |
4067 | /* |
4068 | * As per the order given at ctx_resched() first 'pop' task flexible | |
4069 | * and then, if needed CPU flexible. | |
4070 | */ | |
fd7d5517 IR |
4071 | if (task_event || (task_ctx && cpu_event)) |
4072 | ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE); | |
8d5bce0c PZ |
4073 | if (cpu_event) |
4074 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); | |
0793a61d | 4075 | |
8d5bce0c | 4076 | if (task_event) |
fd7d5517 | 4077 | rotate_ctx(task_ctx, task_event); |
8d5bce0c PZ |
4078 | if (cpu_event) |
4079 | rotate_ctx(&cpuctx->ctx, cpu_event); | |
235c7fc7 | 4080 | |
fd7d5517 | 4081 | perf_event_sched_in(cpuctx, task_ctx, current); |
235c7fc7 | 4082 | |
0f5a2601 PZ |
4083 | perf_pmu_enable(cpuctx->ctx.pmu); |
4084 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); | |
9e630205 | 4085 | |
8d5bce0c | 4086 | return true; |
e9d2b064 PZ |
4087 | } |
4088 | ||
4089 | void perf_event_task_tick(void) | |
4090 | { | |
2fde4f94 MR |
4091 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
4092 | struct perf_event_context *ctx, *tmp; | |
e050e3f0 | 4093 | int throttled; |
b5ab4cd5 | 4094 | |
16444645 | 4095 | lockdep_assert_irqs_disabled(); |
e9d2b064 | 4096 | |
e050e3f0 SE |
4097 | __this_cpu_inc(perf_throttled_seq); |
4098 | throttled = __this_cpu_xchg(perf_throttled_count, 0); | |
555e0c1e | 4099 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
e050e3f0 | 4100 | |
2fde4f94 | 4101 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
e050e3f0 | 4102 | perf_adjust_freq_unthr_context(ctx, throttled); |
0793a61d TG |
4103 | } |
4104 | ||
889ff015 FW |
4105 | static int event_enable_on_exec(struct perf_event *event, |
4106 | struct perf_event_context *ctx) | |
4107 | { | |
4108 | if (!event->attr.enable_on_exec) | |
4109 | return 0; | |
4110 | ||
4111 | event->attr.enable_on_exec = 0; | |
4112 | if (event->state >= PERF_EVENT_STATE_INACTIVE) | |
4113 | return 0; | |
4114 | ||
0d3d73aa | 4115 | perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); |
889ff015 FW |
4116 | |
4117 | return 1; | |
4118 | } | |
4119 | ||
57e7986e | 4120 | /* |
cdd6c482 | 4121 | * Enable all of a task's events that have been marked enable-on-exec. |
57e7986e PM |
4122 | * This expects task == current. |
4123 | */ | |
c1274499 | 4124 | static void perf_event_enable_on_exec(int ctxn) |
57e7986e | 4125 | { |
c1274499 | 4126 | struct perf_event_context *ctx, *clone_ctx = NULL; |
487f05e1 | 4127 | enum event_type_t event_type = 0; |
3e349507 | 4128 | struct perf_cpu_context *cpuctx; |
cdd6c482 | 4129 | struct perf_event *event; |
57e7986e PM |
4130 | unsigned long flags; |
4131 | int enabled = 0; | |
4132 | ||
4133 | local_irq_save(flags); | |
c1274499 | 4134 | ctx = current->perf_event_ctxp[ctxn]; |
cdd6c482 | 4135 | if (!ctx || !ctx->nr_events) |
57e7986e PM |
4136 | goto out; |
4137 | ||
3e349507 PZ |
4138 | cpuctx = __get_cpu_context(ctx); |
4139 | perf_ctx_lock(cpuctx, ctx); | |
7fce2509 | 4140 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
487f05e1 | 4141 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
3e349507 | 4142 | enabled |= event_enable_on_exec(event, ctx); |
487f05e1 AS |
4143 | event_type |= get_event_type(event); |
4144 | } | |
57e7986e PM |
4145 | |
4146 | /* | |
3e349507 | 4147 | * Unclone and reschedule this context if we enabled any event. |
57e7986e | 4148 | */ |
3e349507 | 4149 | if (enabled) { |
211de6eb | 4150 | clone_ctx = unclone_ctx(ctx); |
487f05e1 | 4151 | ctx_resched(cpuctx, ctx, event_type); |
7bbba0eb PZ |
4152 | } else { |
4153 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); | |
3e349507 PZ |
4154 | } |
4155 | perf_ctx_unlock(cpuctx, ctx); | |
57e7986e | 4156 | |
9ed6060d | 4157 | out: |
57e7986e | 4158 | local_irq_restore(flags); |
211de6eb PZ |
4159 | |
4160 | if (clone_ctx) | |
4161 | put_ctx(clone_ctx); | |
57e7986e PM |
4162 | } |
4163 | ||
0492d4c5 PZ |
4164 | struct perf_read_data { |
4165 | struct perf_event *event; | |
4166 | bool group; | |
7d88962e | 4167 | int ret; |
0492d4c5 PZ |
4168 | }; |
4169 | ||
451d24d1 | 4170 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
d6a2f903 | 4171 | { |
d6a2f903 DCC |
4172 | u16 local_pkg, event_pkg; |
4173 | ||
4174 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { | |
451d24d1 PZ |
4175 | int local_cpu = smp_processor_id(); |
4176 | ||
4177 | event_pkg = topology_physical_package_id(event_cpu); | |
4178 | local_pkg = topology_physical_package_id(local_cpu); | |
d6a2f903 DCC |
4179 | |
4180 | if (event_pkg == local_pkg) | |
4181 | return local_cpu; | |
4182 | } | |
4183 | ||
4184 | return event_cpu; | |
4185 | } | |
4186 | ||
0793a61d | 4187 | /* |
cdd6c482 | 4188 | * Cross CPU call to read the hardware event |
0793a61d | 4189 | */ |
cdd6c482 | 4190 | static void __perf_event_read(void *info) |
0793a61d | 4191 | { |
0492d4c5 PZ |
4192 | struct perf_read_data *data = info; |
4193 | struct perf_event *sub, *event = data->event; | |
cdd6c482 | 4194 | struct perf_event_context *ctx = event->ctx; |
108b02cf | 4195 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
4a00c16e | 4196 | struct pmu *pmu = event->pmu; |
621a01ea | 4197 | |
e1ac3614 PM |
4198 | /* |
4199 | * If this is a task context, we need to check whether it is | |
4200 | * the current task context of this cpu. If not it has been | |
4201 | * scheduled out before the smp call arrived. In that case | |
cdd6c482 IM |
4202 | * event->count would have been updated to a recent sample |
4203 | * when the event was scheduled out. | |
e1ac3614 PM |
4204 | */ |
4205 | if (ctx->task && cpuctx->task_ctx != ctx) | |
4206 | return; | |
4207 | ||
e625cce1 | 4208 | raw_spin_lock(&ctx->lock); |
0c1cbc18 | 4209 | if (ctx->is_active & EVENT_TIME) { |
542e72fc | 4210 | update_context_time(ctx); |
e5d1367f SE |
4211 | update_cgrp_time_from_event(event); |
4212 | } | |
0492d4c5 | 4213 | |
0d3d73aa PZ |
4214 | perf_event_update_time(event); |
4215 | if (data->group) | |
4216 | perf_event_update_sibling_time(event); | |
0c1cbc18 | 4217 | |
4a00c16e SB |
4218 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
4219 | goto unlock; | |
0492d4c5 | 4220 | |
4a00c16e SB |
4221 | if (!data->group) { |
4222 | pmu->read(event); | |
4223 | data->ret = 0; | |
0492d4c5 | 4224 | goto unlock; |
4a00c16e SB |
4225 | } |
4226 | ||
4227 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); | |
4228 | ||
4229 | pmu->read(event); | |
0492d4c5 | 4230 | |
edb39592 | 4231 | for_each_sibling_event(sub, event) { |
4a00c16e SB |
4232 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
4233 | /* | |
4234 | * Use sibling's PMU rather than @event's since | |
4235 | * sibling could be on different (eg: software) PMU. | |
4236 | */ | |
0492d4c5 | 4237 | sub->pmu->read(sub); |
4a00c16e | 4238 | } |
0492d4c5 | 4239 | } |
4a00c16e SB |
4240 | |
4241 | data->ret = pmu->commit_txn(pmu); | |
0492d4c5 PZ |
4242 | |
4243 | unlock: | |
e625cce1 | 4244 | raw_spin_unlock(&ctx->lock); |
0793a61d TG |
4245 | } |
4246 | ||
b5e58793 PZ |
4247 | static inline u64 perf_event_count(struct perf_event *event) |
4248 | { | |
c39a0e2c | 4249 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
b5e58793 PZ |
4250 | } |
4251 | ||
ffe8690c KX |
4252 | /* |
4253 | * NMI-safe method to read a local event, that is an event that | |
4254 | * is: | |
4255 | * - either for the current task, or for this CPU | |
4256 | * - does not have inherit set, for inherited task events | |
4257 | * will not be local and we cannot read them atomically | |
4258 | * - must not have a pmu::count method | |
4259 | */ | |
7d9285e8 YS |
4260 | int perf_event_read_local(struct perf_event *event, u64 *value, |
4261 | u64 *enabled, u64 *running) | |
ffe8690c KX |
4262 | { |
4263 | unsigned long flags; | |
f91840a3 | 4264 | int ret = 0; |
ffe8690c KX |
4265 | |
4266 | /* | |
4267 | * Disabling interrupts avoids all counter scheduling (context | |
4268 | * switches, timer based rotation and IPIs). | |
4269 | */ | |
4270 | local_irq_save(flags); | |
4271 | ||
ffe8690c KX |
4272 | /* |
4273 | * It must not be an event with inherit set, we cannot read | |
4274 | * all child counters from atomic context. | |
4275 | */ | |
f91840a3 AS |
4276 | if (event->attr.inherit) { |
4277 | ret = -EOPNOTSUPP; | |
4278 | goto out; | |
4279 | } | |
ffe8690c | 4280 | |
f91840a3 AS |
4281 | /* If this is a per-task event, it must be for current */ |
4282 | if ((event->attach_state & PERF_ATTACH_TASK) && | |
4283 | event->hw.target != current) { | |
4284 | ret = -EINVAL; | |
4285 | goto out; | |
4286 | } | |
4287 | ||
4288 | /* If this is a per-CPU event, it must be for this CPU */ | |
4289 | if (!(event->attach_state & PERF_ATTACH_TASK) && | |
4290 | event->cpu != smp_processor_id()) { | |
4291 | ret = -EINVAL; | |
4292 | goto out; | |
4293 | } | |
ffe8690c | 4294 | |
befb1b3c RC |
4295 | /* If this is a pinned event it must be running on this CPU */ |
4296 | if (event->attr.pinned && event->oncpu != smp_processor_id()) { | |
4297 | ret = -EBUSY; | |
4298 | goto out; | |
4299 | } | |
4300 | ||
ffe8690c KX |
4301 | /* |
4302 | * If the event is currently on this CPU, its either a per-task event, | |
4303 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise | |
4304 | * oncpu == -1). | |
4305 | */ | |
4306 | if (event->oncpu == smp_processor_id()) | |
4307 | event->pmu->read(event); | |
4308 | ||
f91840a3 | 4309 | *value = local64_read(&event->count); |
0d3d73aa PZ |
4310 | if (enabled || running) { |
4311 | u64 now = event->shadow_ctx_time + perf_clock(); | |
4312 | u64 __enabled, __running; | |
4313 | ||
4314 | __perf_update_times(event, now, &__enabled, &__running); | |
4315 | if (enabled) | |
4316 | *enabled = __enabled; | |
4317 | if (running) | |
4318 | *running = __running; | |
4319 | } | |
f91840a3 | 4320 | out: |
ffe8690c KX |
4321 | local_irq_restore(flags); |
4322 | ||
f91840a3 | 4323 | return ret; |
ffe8690c KX |
4324 | } |
4325 | ||
7d88962e | 4326 | static int perf_event_read(struct perf_event *event, bool group) |
0793a61d | 4327 | { |
0c1cbc18 | 4328 | enum perf_event_state state = READ_ONCE(event->state); |
451d24d1 | 4329 | int event_cpu, ret = 0; |
7d88962e | 4330 | |
0793a61d | 4331 | /* |
cdd6c482 IM |
4332 | * If event is enabled and currently active on a CPU, update the |
4333 | * value in the event structure: | |
0793a61d | 4334 | */ |
0c1cbc18 PZ |
4335 | again: |
4336 | if (state == PERF_EVENT_STATE_ACTIVE) { | |
4337 | struct perf_read_data data; | |
4338 | ||
4339 | /* | |
4340 | * Orders the ->state and ->oncpu loads such that if we see | |
4341 | * ACTIVE we must also see the right ->oncpu. | |
4342 | * | |
4343 | * Matches the smp_wmb() from event_sched_in(). | |
4344 | */ | |
4345 | smp_rmb(); | |
d6a2f903 | 4346 | |
451d24d1 PZ |
4347 | event_cpu = READ_ONCE(event->oncpu); |
4348 | if ((unsigned)event_cpu >= nr_cpu_ids) | |
4349 | return 0; | |
4350 | ||
0c1cbc18 PZ |
4351 | data = (struct perf_read_data){ |
4352 | .event = event, | |
4353 | .group = group, | |
4354 | .ret = 0, | |
4355 | }; | |
4356 | ||
451d24d1 PZ |
4357 | preempt_disable(); |
4358 | event_cpu = __perf_event_read_cpu(event, event_cpu); | |
d6a2f903 | 4359 | |
58763148 PZ |
4360 | /* |
4361 | * Purposely ignore the smp_call_function_single() return | |
4362 | * value. | |
4363 | * | |
451d24d1 | 4364 | * If event_cpu isn't a valid CPU it means the event got |
58763148 PZ |
4365 | * scheduled out and that will have updated the event count. |
4366 | * | |
4367 | * Therefore, either way, we'll have an up-to-date event count | |
4368 | * after this. | |
4369 | */ | |
451d24d1 PZ |
4370 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
4371 | preempt_enable(); | |
58763148 | 4372 | ret = data.ret; |
0c1cbc18 PZ |
4373 | |
4374 | } else if (state == PERF_EVENT_STATE_INACTIVE) { | |
2b8988c9 PZ |
4375 | struct perf_event_context *ctx = event->ctx; |
4376 | unsigned long flags; | |
4377 | ||
e625cce1 | 4378 | raw_spin_lock_irqsave(&ctx->lock, flags); |
0c1cbc18 PZ |
4379 | state = event->state; |
4380 | if (state != PERF_EVENT_STATE_INACTIVE) { | |
4381 | raw_spin_unlock_irqrestore(&ctx->lock, flags); | |
4382 | goto again; | |
4383 | } | |
4384 | ||
c530ccd9 | 4385 | /* |
0c1cbc18 PZ |
4386 | * May read while context is not active (e.g., thread is |
4387 | * blocked), in that case we cannot update context time | |
c530ccd9 | 4388 | */ |
0c1cbc18 | 4389 | if (ctx->is_active & EVENT_TIME) { |
c530ccd9 | 4390 | update_context_time(ctx); |
e5d1367f SE |
4391 | update_cgrp_time_from_event(event); |
4392 | } | |
0c1cbc18 | 4393 | |
0d3d73aa | 4394 | perf_event_update_time(event); |
0492d4c5 | 4395 | if (group) |
0d3d73aa | 4396 | perf_event_update_sibling_time(event); |
e625cce1 | 4397 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
0793a61d | 4398 | } |
7d88962e SB |
4399 | |
4400 | return ret; | |
0793a61d TG |
4401 | } |
4402 | ||
a63eaf34 | 4403 | /* |
cdd6c482 | 4404 | * Initialize the perf_event context in a task_struct: |
a63eaf34 | 4405 | */ |
eb184479 | 4406 | static void __perf_event_init_context(struct perf_event_context *ctx) |
a63eaf34 | 4407 | { |
e625cce1 | 4408 | raw_spin_lock_init(&ctx->lock); |
a63eaf34 | 4409 | mutex_init(&ctx->mutex); |
2fde4f94 | 4410 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
8e1a2031 AB |
4411 | perf_event_groups_init(&ctx->pinned_groups); |
4412 | perf_event_groups_init(&ctx->flexible_groups); | |
a63eaf34 | 4413 | INIT_LIST_HEAD(&ctx->event_list); |
6668128a PZ |
4414 | INIT_LIST_HEAD(&ctx->pinned_active); |
4415 | INIT_LIST_HEAD(&ctx->flexible_active); | |
8c94abbb | 4416 | refcount_set(&ctx->refcount, 1); |
eb184479 PZ |
4417 | } |
4418 | ||
4419 | static struct perf_event_context * | |
4420 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) | |
4421 | { | |
4422 | struct perf_event_context *ctx; | |
4423 | ||
4424 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); | |
4425 | if (!ctx) | |
4426 | return NULL; | |
4427 | ||
4428 | __perf_event_init_context(ctx); | |
7b3c92b8 MWO |
4429 | if (task) |
4430 | ctx->task = get_task_struct(task); | |
eb184479 PZ |
4431 | ctx->pmu = pmu; |
4432 | ||
4433 | return ctx; | |
a63eaf34 PM |
4434 | } |
4435 | ||
2ebd4ffb MH |
4436 | static struct task_struct * |
4437 | find_lively_task_by_vpid(pid_t vpid) | |
4438 | { | |
4439 | struct task_struct *task; | |
0793a61d TG |
4440 | |
4441 | rcu_read_lock(); | |
2ebd4ffb | 4442 | if (!vpid) |
0793a61d TG |
4443 | task = current; |
4444 | else | |
2ebd4ffb | 4445 | task = find_task_by_vpid(vpid); |
0793a61d TG |
4446 | if (task) |
4447 | get_task_struct(task); | |
4448 | rcu_read_unlock(); | |
4449 | ||
4450 | if (!task) | |
4451 | return ERR_PTR(-ESRCH); | |
4452 | ||
2ebd4ffb | 4453 | return task; |
2ebd4ffb MH |
4454 | } |
4455 | ||
fe4b04fa PZ |
4456 | /* |
4457 | * Returns a matching context with refcount and pincount. | |
4458 | */ | |
108b02cf | 4459 | static struct perf_event_context * |
4af57ef2 YZ |
4460 | find_get_context(struct pmu *pmu, struct task_struct *task, |
4461 | struct perf_event *event) | |
0793a61d | 4462 | { |
211de6eb | 4463 | struct perf_event_context *ctx, *clone_ctx = NULL; |
22a4f650 | 4464 | struct perf_cpu_context *cpuctx; |
4af57ef2 | 4465 | void *task_ctx_data = NULL; |
25346b93 | 4466 | unsigned long flags; |
8dc85d54 | 4467 | int ctxn, err; |
4af57ef2 | 4468 | int cpu = event->cpu; |
0793a61d | 4469 | |
22a4ec72 | 4470 | if (!task) { |
cdd6c482 | 4471 | /* Must be root to operate on a CPU event: */ |
da97e184 JFG |
4472 | err = perf_allow_cpu(&event->attr); |
4473 | if (err) | |
4474 | return ERR_PTR(err); | |
0793a61d | 4475 | |
108b02cf | 4476 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
0793a61d | 4477 | ctx = &cpuctx->ctx; |
c93f7669 | 4478 | get_ctx(ctx); |
fe4b04fa | 4479 | ++ctx->pin_count; |
0793a61d | 4480 | |
0793a61d TG |
4481 | return ctx; |
4482 | } | |
4483 | ||
8dc85d54 PZ |
4484 | err = -EINVAL; |
4485 | ctxn = pmu->task_ctx_nr; | |
4486 | if (ctxn < 0) | |
4487 | goto errout; | |
4488 | ||
4af57ef2 | 4489 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
ff9ff926 | 4490 | task_ctx_data = alloc_task_ctx_data(pmu); |
4af57ef2 YZ |
4491 | if (!task_ctx_data) { |
4492 | err = -ENOMEM; | |
4493 | goto errout; | |
4494 | } | |
4495 | } | |
4496 | ||
9ed6060d | 4497 | retry: |
8dc85d54 | 4498 | ctx = perf_lock_task_context(task, ctxn, &flags); |
c93f7669 | 4499 | if (ctx) { |
211de6eb | 4500 | clone_ctx = unclone_ctx(ctx); |
fe4b04fa | 4501 | ++ctx->pin_count; |
4af57ef2 YZ |
4502 | |
4503 | if (task_ctx_data && !ctx->task_ctx_data) { | |
4504 | ctx->task_ctx_data = task_ctx_data; | |
4505 | task_ctx_data = NULL; | |
4506 | } | |
e625cce1 | 4507 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
211de6eb PZ |
4508 | |
4509 | if (clone_ctx) | |
4510 | put_ctx(clone_ctx); | |
9137fb28 | 4511 | } else { |
eb184479 | 4512 | ctx = alloc_perf_context(pmu, task); |
c93f7669 PM |
4513 | err = -ENOMEM; |
4514 | if (!ctx) | |
4515 | goto errout; | |
eb184479 | 4516 | |
4af57ef2 YZ |
4517 | if (task_ctx_data) { |
4518 | ctx->task_ctx_data = task_ctx_data; | |
4519 | task_ctx_data = NULL; | |
4520 | } | |
4521 | ||
dbe08d82 ON |
4522 | err = 0; |
4523 | mutex_lock(&task->perf_event_mutex); | |
4524 | /* | |
4525 | * If it has already passed perf_event_exit_task(). | |
4526 | * we must see PF_EXITING, it takes this mutex too. | |
4527 | */ | |
4528 | if (task->flags & PF_EXITING) | |
4529 | err = -ESRCH; | |
4530 | else if (task->perf_event_ctxp[ctxn]) | |
4531 | err = -EAGAIN; | |
fe4b04fa | 4532 | else { |
9137fb28 | 4533 | get_ctx(ctx); |
fe4b04fa | 4534 | ++ctx->pin_count; |
dbe08d82 | 4535 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
fe4b04fa | 4536 | } |
dbe08d82 ON |
4537 | mutex_unlock(&task->perf_event_mutex); |
4538 | ||
4539 | if (unlikely(err)) { | |
9137fb28 | 4540 | put_ctx(ctx); |
dbe08d82 ON |
4541 | |
4542 | if (err == -EAGAIN) | |
4543 | goto retry; | |
4544 | goto errout; | |
a63eaf34 PM |
4545 | } |
4546 | } | |
4547 | ||
ff9ff926 | 4548 | free_task_ctx_data(pmu, task_ctx_data); |
0793a61d | 4549 | return ctx; |
c93f7669 | 4550 | |
9ed6060d | 4551 | errout: |
ff9ff926 | 4552 | free_task_ctx_data(pmu, task_ctx_data); |
c93f7669 | 4553 | return ERR_PTR(err); |
0793a61d TG |
4554 | } |
4555 | ||
6fb2915d | 4556 | static void perf_event_free_filter(struct perf_event *event); |
2541517c | 4557 | static void perf_event_free_bpf_prog(struct perf_event *event); |
6fb2915d | 4558 | |
cdd6c482 | 4559 | static void free_event_rcu(struct rcu_head *head) |
592903cd | 4560 | { |
cdd6c482 | 4561 | struct perf_event *event; |
592903cd | 4562 | |
cdd6c482 IM |
4563 | event = container_of(head, struct perf_event, rcu_head); |
4564 | if (event->ns) | |
4565 | put_pid_ns(event->ns); | |
6fb2915d | 4566 | perf_event_free_filter(event); |
cdd6c482 | 4567 | kfree(event); |
592903cd PZ |
4568 | } |
4569 | ||
b69cf536 | 4570 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 4571 | struct perf_buffer *rb); |
925d519a | 4572 | |
f2fb6bef KL |
4573 | static void detach_sb_event(struct perf_event *event) |
4574 | { | |
4575 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
4576 | ||
4577 | raw_spin_lock(&pel->lock); | |
4578 | list_del_rcu(&event->sb_list); | |
4579 | raw_spin_unlock(&pel->lock); | |
4580 | } | |
4581 | ||
a4f144eb | 4582 | static bool is_sb_event(struct perf_event *event) |
f2fb6bef | 4583 | { |
a4f144eb DCC |
4584 | struct perf_event_attr *attr = &event->attr; |
4585 | ||
f2fb6bef | 4586 | if (event->parent) |
a4f144eb | 4587 | return false; |
f2fb6bef KL |
4588 | |
4589 | if (event->attach_state & PERF_ATTACH_TASK) | |
a4f144eb | 4590 | return false; |
f2fb6bef | 4591 | |
a4f144eb DCC |
4592 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
4593 | attr->comm || attr->comm_exec || | |
76193a94 | 4594 | attr->task || attr->ksymbol || |
e17d43b9 | 4595 | attr->context_switch || attr->text_poke || |
21038f2b | 4596 | attr->bpf_event) |
a4f144eb DCC |
4597 | return true; |
4598 | return false; | |
4599 | } | |
4600 | ||
4601 | static void unaccount_pmu_sb_event(struct perf_event *event) | |
4602 | { | |
4603 | if (is_sb_event(event)) | |
4604 | detach_sb_event(event); | |
f2fb6bef KL |
4605 | } |
4606 | ||
4beb31f3 | 4607 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
f1600952 | 4608 | { |
4beb31f3 FW |
4609 | if (event->parent) |
4610 | return; | |
4611 | ||
4beb31f3 FW |
4612 | if (is_cgroup_event(event)) |
4613 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); | |
4614 | } | |
925d519a | 4615 | |
555e0c1e FW |
4616 | #ifdef CONFIG_NO_HZ_FULL |
4617 | static DEFINE_SPINLOCK(nr_freq_lock); | |
4618 | #endif | |
4619 | ||
4620 | static void unaccount_freq_event_nohz(void) | |
4621 | { | |
4622 | #ifdef CONFIG_NO_HZ_FULL | |
4623 | spin_lock(&nr_freq_lock); | |
4624 | if (atomic_dec_and_test(&nr_freq_events)) | |
4625 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); | |
4626 | spin_unlock(&nr_freq_lock); | |
4627 | #endif | |
4628 | } | |
4629 | ||
4630 | static void unaccount_freq_event(void) | |
4631 | { | |
4632 | if (tick_nohz_full_enabled()) | |
4633 | unaccount_freq_event_nohz(); | |
4634 | else | |
4635 | atomic_dec(&nr_freq_events); | |
4636 | } | |
4637 | ||
4beb31f3 FW |
4638 | static void unaccount_event(struct perf_event *event) |
4639 | { | |
25432ae9 PZ |
4640 | bool dec = false; |
4641 | ||
4beb31f3 FW |
4642 | if (event->parent) |
4643 | return; | |
4644 | ||
4645 | if (event->attach_state & PERF_ATTACH_TASK) | |
25432ae9 | 4646 | dec = true; |
4beb31f3 FW |
4647 | if (event->attr.mmap || event->attr.mmap_data) |
4648 | atomic_dec(&nr_mmap_events); | |
4649 | if (event->attr.comm) | |
4650 | atomic_dec(&nr_comm_events); | |
e4222673 HB |
4651 | if (event->attr.namespaces) |
4652 | atomic_dec(&nr_namespaces_events); | |
96aaab68 NK |
4653 | if (event->attr.cgroup) |
4654 | atomic_dec(&nr_cgroup_events); | |
4beb31f3 FW |
4655 | if (event->attr.task) |
4656 | atomic_dec(&nr_task_events); | |
948b26b6 | 4657 | if (event->attr.freq) |
555e0c1e | 4658 | unaccount_freq_event(); |
45ac1403 | 4659 | if (event->attr.context_switch) { |
25432ae9 | 4660 | dec = true; |
45ac1403 AH |
4661 | atomic_dec(&nr_switch_events); |
4662 | } | |
4beb31f3 | 4663 | if (is_cgroup_event(event)) |
25432ae9 | 4664 | dec = true; |
4beb31f3 | 4665 | if (has_branch_stack(event)) |
25432ae9 | 4666 | dec = true; |
76193a94 SL |
4667 | if (event->attr.ksymbol) |
4668 | atomic_dec(&nr_ksymbol_events); | |
6ee52e2a SL |
4669 | if (event->attr.bpf_event) |
4670 | atomic_dec(&nr_bpf_events); | |
e17d43b9 AH |
4671 | if (event->attr.text_poke) |
4672 | atomic_dec(&nr_text_poke_events); | |
25432ae9 | 4673 | |
9107c89e PZ |
4674 | if (dec) { |
4675 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) | |
4676 | schedule_delayed_work(&perf_sched_work, HZ); | |
4677 | } | |
4beb31f3 FW |
4678 | |
4679 | unaccount_event_cpu(event, event->cpu); | |
f2fb6bef KL |
4680 | |
4681 | unaccount_pmu_sb_event(event); | |
4beb31f3 | 4682 | } |
925d519a | 4683 | |
9107c89e PZ |
4684 | static void perf_sched_delayed(struct work_struct *work) |
4685 | { | |
4686 | mutex_lock(&perf_sched_mutex); | |
4687 | if (atomic_dec_and_test(&perf_sched_count)) | |
4688 | static_branch_disable(&perf_sched_events); | |
4689 | mutex_unlock(&perf_sched_mutex); | |
4690 | } | |
4691 | ||
bed5b25a AS |
4692 | /* |
4693 | * The following implement mutual exclusion of events on "exclusive" pmus | |
4694 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled | |
4695 | * at a time, so we disallow creating events that might conflict, namely: | |
4696 | * | |
4697 | * 1) cpu-wide events in the presence of per-task events, | |
4698 | * 2) per-task events in the presence of cpu-wide events, | |
4699 | * 3) two matching events on the same context. | |
4700 | * | |
4701 | * The former two cases are handled in the allocation path (perf_event_alloc(), | |
a0733e69 | 4702 | * _free_event()), the latter -- before the first perf_install_in_context(). |
bed5b25a AS |
4703 | */ |
4704 | static int exclusive_event_init(struct perf_event *event) | |
4705 | { | |
4706 | struct pmu *pmu = event->pmu; | |
4707 | ||
8a58ddae | 4708 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
4709 | return 0; |
4710 | ||
4711 | /* | |
4712 | * Prevent co-existence of per-task and cpu-wide events on the | |
4713 | * same exclusive pmu. | |
4714 | * | |
4715 | * Negative pmu::exclusive_cnt means there are cpu-wide | |
4716 | * events on this "exclusive" pmu, positive means there are | |
4717 | * per-task events. | |
4718 | * | |
4719 | * Since this is called in perf_event_alloc() path, event::ctx | |
4720 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK | |
4721 | * to mean "per-task event", because unlike other attach states it | |
4722 | * never gets cleared. | |
4723 | */ | |
4724 | if (event->attach_state & PERF_ATTACH_TASK) { | |
4725 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) | |
4726 | return -EBUSY; | |
4727 | } else { | |
4728 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) | |
4729 | return -EBUSY; | |
4730 | } | |
4731 | ||
4732 | return 0; | |
4733 | } | |
4734 | ||
4735 | static void exclusive_event_destroy(struct perf_event *event) | |
4736 | { | |
4737 | struct pmu *pmu = event->pmu; | |
4738 | ||
8a58ddae | 4739 | if (!is_exclusive_pmu(pmu)) |
bed5b25a AS |
4740 | return; |
4741 | ||
4742 | /* see comment in exclusive_event_init() */ | |
4743 | if (event->attach_state & PERF_ATTACH_TASK) | |
4744 | atomic_dec(&pmu->exclusive_cnt); | |
4745 | else | |
4746 | atomic_inc(&pmu->exclusive_cnt); | |
4747 | } | |
4748 | ||
4749 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) | |
4750 | { | |
3bf6215a | 4751 | if ((e1->pmu == e2->pmu) && |
bed5b25a AS |
4752 | (e1->cpu == e2->cpu || |
4753 | e1->cpu == -1 || | |
4754 | e2->cpu == -1)) | |
4755 | return true; | |
4756 | return false; | |
4757 | } | |
4758 | ||
bed5b25a AS |
4759 | static bool exclusive_event_installable(struct perf_event *event, |
4760 | struct perf_event_context *ctx) | |
4761 | { | |
4762 | struct perf_event *iter_event; | |
4763 | struct pmu *pmu = event->pmu; | |
4764 | ||
8a58ddae AS |
4765 | lockdep_assert_held(&ctx->mutex); |
4766 | ||
4767 | if (!is_exclusive_pmu(pmu)) | |
bed5b25a AS |
4768 | return true; |
4769 | ||
4770 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { | |
4771 | if (exclusive_event_match(iter_event, event)) | |
4772 | return false; | |
4773 | } | |
4774 | ||
4775 | return true; | |
4776 | } | |
4777 | ||
375637bc AS |
4778 | static void perf_addr_filters_splice(struct perf_event *event, |
4779 | struct list_head *head); | |
4780 | ||
683ede43 | 4781 | static void _free_event(struct perf_event *event) |
f1600952 | 4782 | { |
e360adbe | 4783 | irq_work_sync(&event->pending); |
925d519a | 4784 | |
4beb31f3 | 4785 | unaccount_event(event); |
9ee318a7 | 4786 | |
da97e184 JFG |
4787 | security_perf_event_free(event); |
4788 | ||
76369139 | 4789 | if (event->rb) { |
9bb5d40c PZ |
4790 | /* |
4791 | * Can happen when we close an event with re-directed output. | |
4792 | * | |
4793 | * Since we have a 0 refcount, perf_mmap_close() will skip | |
4794 | * over us; possibly making our ring_buffer_put() the last. | |
4795 | */ | |
4796 | mutex_lock(&event->mmap_mutex); | |
b69cf536 | 4797 | ring_buffer_attach(event, NULL); |
9bb5d40c | 4798 | mutex_unlock(&event->mmap_mutex); |
a4be7c27 PZ |
4799 | } |
4800 | ||
e5d1367f SE |
4801 | if (is_cgroup_event(event)) |
4802 | perf_detach_cgroup(event); | |
4803 | ||
a0733e69 PZ |
4804 | if (!event->parent) { |
4805 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
4806 | put_callchain_buffers(); | |
4807 | } | |
4808 | ||
4809 | perf_event_free_bpf_prog(event); | |
375637bc | 4810 | perf_addr_filters_splice(event, NULL); |
c60f83b8 | 4811 | kfree(event->addr_filter_ranges); |
a0733e69 PZ |
4812 | |
4813 | if (event->destroy) | |
4814 | event->destroy(event); | |
4815 | ||
1cf8dfe8 PZ |
4816 | /* |
4817 | * Must be after ->destroy(), due to uprobe_perf_close() using | |
4818 | * hw.target. | |
4819 | */ | |
621b6d2e PB |
4820 | if (event->hw.target) |
4821 | put_task_struct(event->hw.target); | |
4822 | ||
1cf8dfe8 PZ |
4823 | /* |
4824 | * perf_event_free_task() relies on put_ctx() being 'last', in particular | |
4825 | * all task references must be cleaned up. | |
4826 | */ | |
4827 | if (event->ctx) | |
4828 | put_ctx(event->ctx); | |
4829 | ||
62a92c8f AS |
4830 | exclusive_event_destroy(event); |
4831 | module_put(event->pmu->module); | |
a0733e69 PZ |
4832 | |
4833 | call_rcu(&event->rcu_head, free_event_rcu); | |
f1600952 PZ |
4834 | } |
4835 | ||
683ede43 PZ |
4836 | /* |
4837 | * Used to free events which have a known refcount of 1, such as in error paths | |
4838 | * where the event isn't exposed yet and inherited events. | |
4839 | */ | |
4840 | static void free_event(struct perf_event *event) | |
0793a61d | 4841 | { |
683ede43 PZ |
4842 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
4843 | "unexpected event refcount: %ld; ptr=%p\n", | |
4844 | atomic_long_read(&event->refcount), event)) { | |
4845 | /* leak to avoid use-after-free */ | |
4846 | return; | |
4847 | } | |
0793a61d | 4848 | |
683ede43 | 4849 | _free_event(event); |
0793a61d TG |
4850 | } |
4851 | ||
a66a3052 | 4852 | /* |
f8697762 | 4853 | * Remove user event from the owner task. |
a66a3052 | 4854 | */ |
f8697762 | 4855 | static void perf_remove_from_owner(struct perf_event *event) |
fb0459d7 | 4856 | { |
8882135b | 4857 | struct task_struct *owner; |
fb0459d7 | 4858 | |
8882135b | 4859 | rcu_read_lock(); |
8882135b | 4860 | /* |
f47c02c0 PZ |
4861 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
4862 | * observe !owner it means the list deletion is complete and we can | |
4863 | * indeed free this event, otherwise we need to serialize on | |
8882135b PZ |
4864 | * owner->perf_event_mutex. |
4865 | */ | |
506458ef | 4866 | owner = READ_ONCE(event->owner); |
8882135b PZ |
4867 | if (owner) { |
4868 | /* | |
4869 | * Since delayed_put_task_struct() also drops the last | |
4870 | * task reference we can safely take a new reference | |
4871 | * while holding the rcu_read_lock(). | |
4872 | */ | |
4873 | get_task_struct(owner); | |
4874 | } | |
4875 | rcu_read_unlock(); | |
4876 | ||
4877 | if (owner) { | |
f63a8daa PZ |
4878 | /* |
4879 | * If we're here through perf_event_exit_task() we're already | |
4880 | * holding ctx->mutex which would be an inversion wrt. the | |
4881 | * normal lock order. | |
4882 | * | |
4883 | * However we can safely take this lock because its the child | |
4884 | * ctx->mutex. | |
4885 | */ | |
4886 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); | |
4887 | ||
8882135b PZ |
4888 | /* |
4889 | * We have to re-check the event->owner field, if it is cleared | |
4890 | * we raced with perf_event_exit_task(), acquiring the mutex | |
4891 | * ensured they're done, and we can proceed with freeing the | |
4892 | * event. | |
4893 | */ | |
f47c02c0 | 4894 | if (event->owner) { |
8882135b | 4895 | list_del_init(&event->owner_entry); |
f47c02c0 PZ |
4896 | smp_store_release(&event->owner, NULL); |
4897 | } | |
8882135b PZ |
4898 | mutex_unlock(&owner->perf_event_mutex); |
4899 | put_task_struct(owner); | |
4900 | } | |
f8697762 JO |
4901 | } |
4902 | ||
f8697762 JO |
4903 | static void put_event(struct perf_event *event) |
4904 | { | |
f8697762 JO |
4905 | if (!atomic_long_dec_and_test(&event->refcount)) |
4906 | return; | |
4907 | ||
c6e5b732 PZ |
4908 | _free_event(event); |
4909 | } | |
4910 | ||
4911 | /* | |
4912 | * Kill an event dead; while event:refcount will preserve the event | |
4913 | * object, it will not preserve its functionality. Once the last 'user' | |
4914 | * gives up the object, we'll destroy the thing. | |
4915 | */ | |
4916 | int perf_event_release_kernel(struct perf_event *event) | |
4917 | { | |
a4f4bb6d | 4918 | struct perf_event_context *ctx = event->ctx; |
c6e5b732 | 4919 | struct perf_event *child, *tmp; |
82d94856 | 4920 | LIST_HEAD(free_list); |
c6e5b732 | 4921 | |
a4f4bb6d PZ |
4922 | /* |
4923 | * If we got here through err_file: fput(event_file); we will not have | |
4924 | * attached to a context yet. | |
4925 | */ | |
4926 | if (!ctx) { | |
4927 | WARN_ON_ONCE(event->attach_state & | |
4928 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); | |
4929 | goto no_ctx; | |
4930 | } | |
4931 | ||
f8697762 JO |
4932 | if (!is_kernel_event(event)) |
4933 | perf_remove_from_owner(event); | |
8882135b | 4934 | |
5fa7c8ec | 4935 | ctx = perf_event_ctx_lock(event); |
a83fe28e | 4936 | WARN_ON_ONCE(ctx->parent_ctx); |
a69b0ca4 | 4937 | perf_remove_from_context(event, DETACH_GROUP); |
683ede43 | 4938 | |
a69b0ca4 | 4939 | raw_spin_lock_irq(&ctx->lock); |
683ede43 | 4940 | /* |
d8a8cfc7 | 4941 | * Mark this event as STATE_DEAD, there is no external reference to it |
a69b0ca4 | 4942 | * anymore. |
683ede43 | 4943 | * |
a69b0ca4 PZ |
4944 | * Anybody acquiring event->child_mutex after the below loop _must_ |
4945 | * also see this, most importantly inherit_event() which will avoid | |
4946 | * placing more children on the list. | |
683ede43 | 4947 | * |
c6e5b732 PZ |
4948 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
4949 | * child events. | |
683ede43 | 4950 | */ |
a69b0ca4 PZ |
4951 | event->state = PERF_EVENT_STATE_DEAD; |
4952 | raw_spin_unlock_irq(&ctx->lock); | |
4953 | ||
4954 | perf_event_ctx_unlock(event, ctx); | |
683ede43 | 4955 | |
c6e5b732 PZ |
4956 | again: |
4957 | mutex_lock(&event->child_mutex); | |
4958 | list_for_each_entry(child, &event->child_list, child_list) { | |
a6fa941d | 4959 | |
c6e5b732 PZ |
4960 | /* |
4961 | * Cannot change, child events are not migrated, see the | |
4962 | * comment with perf_event_ctx_lock_nested(). | |
4963 | */ | |
506458ef | 4964 | ctx = READ_ONCE(child->ctx); |
c6e5b732 PZ |
4965 | /* |
4966 | * Since child_mutex nests inside ctx::mutex, we must jump | |
4967 | * through hoops. We start by grabbing a reference on the ctx. | |
4968 | * | |
4969 | * Since the event cannot get freed while we hold the | |
4970 | * child_mutex, the context must also exist and have a !0 | |
4971 | * reference count. | |
4972 | */ | |
4973 | get_ctx(ctx); | |
4974 | ||
4975 | /* | |
4976 | * Now that we have a ctx ref, we can drop child_mutex, and | |
4977 | * acquire ctx::mutex without fear of it going away. Then we | |
4978 | * can re-acquire child_mutex. | |
4979 | */ | |
4980 | mutex_unlock(&event->child_mutex); | |
4981 | mutex_lock(&ctx->mutex); | |
4982 | mutex_lock(&event->child_mutex); | |
4983 | ||
4984 | /* | |
4985 | * Now that we hold ctx::mutex and child_mutex, revalidate our | |
4986 | * state, if child is still the first entry, it didn't get freed | |
4987 | * and we can continue doing so. | |
4988 | */ | |
4989 | tmp = list_first_entry_or_null(&event->child_list, | |
4990 | struct perf_event, child_list); | |
4991 | if (tmp == child) { | |
4992 | perf_remove_from_context(child, DETACH_GROUP); | |
82d94856 | 4993 | list_move(&child->child_list, &free_list); |
c6e5b732 PZ |
4994 | /* |
4995 | * This matches the refcount bump in inherit_event(); | |
4996 | * this can't be the last reference. | |
4997 | */ | |
4998 | put_event(event); | |
4999 | } | |
5000 | ||
5001 | mutex_unlock(&event->child_mutex); | |
5002 | mutex_unlock(&ctx->mutex); | |
5003 | put_ctx(ctx); | |
5004 | goto again; | |
5005 | } | |
5006 | mutex_unlock(&event->child_mutex); | |
5007 | ||
82d94856 | 5008 | list_for_each_entry_safe(child, tmp, &free_list, child_list) { |
1cf8dfe8 PZ |
5009 | void *var = &child->ctx->refcount; |
5010 | ||
82d94856 PZ |
5011 | list_del(&child->child_list); |
5012 | free_event(child); | |
1cf8dfe8 PZ |
5013 | |
5014 | /* | |
5015 | * Wake any perf_event_free_task() waiting for this event to be | |
5016 | * freed. | |
5017 | */ | |
5018 | smp_mb(); /* pairs with wait_var_event() */ | |
5019 | wake_up_var(var); | |
82d94856 PZ |
5020 | } |
5021 | ||
a4f4bb6d PZ |
5022 | no_ctx: |
5023 | put_event(event); /* Must be the 'last' reference */ | |
683ede43 PZ |
5024 | return 0; |
5025 | } | |
5026 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); | |
5027 | ||
8b10c5e2 PZ |
5028 | /* |
5029 | * Called when the last reference to the file is gone. | |
5030 | */ | |
a6fa941d AV |
5031 | static int perf_release(struct inode *inode, struct file *file) |
5032 | { | |
c6e5b732 | 5033 | perf_event_release_kernel(file->private_data); |
a6fa941d | 5034 | return 0; |
fb0459d7 | 5035 | } |
fb0459d7 | 5036 | |
ca0dd44c | 5037 | static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
e53c0994 | 5038 | { |
cdd6c482 | 5039 | struct perf_event *child; |
e53c0994 PZ |
5040 | u64 total = 0; |
5041 | ||
59ed446f PZ |
5042 | *enabled = 0; |
5043 | *running = 0; | |
5044 | ||
6f10581a | 5045 | mutex_lock(&event->child_mutex); |
01add3ea | 5046 | |
7d88962e | 5047 | (void)perf_event_read(event, false); |
01add3ea SB |
5048 | total += perf_event_count(event); |
5049 | ||
59ed446f PZ |
5050 | *enabled += event->total_time_enabled + |
5051 | atomic64_read(&event->child_total_time_enabled); | |
5052 | *running += event->total_time_running + | |
5053 | atomic64_read(&event->child_total_time_running); | |
5054 | ||
5055 | list_for_each_entry(child, &event->child_list, child_list) { | |
7d88962e | 5056 | (void)perf_event_read(child, false); |
01add3ea | 5057 | total += perf_event_count(child); |
59ed446f PZ |
5058 | *enabled += child->total_time_enabled; |
5059 | *running += child->total_time_running; | |
5060 | } | |
6f10581a | 5061 | mutex_unlock(&event->child_mutex); |
e53c0994 PZ |
5062 | |
5063 | return total; | |
5064 | } | |
ca0dd44c PZ |
5065 | |
5066 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) | |
5067 | { | |
5068 | struct perf_event_context *ctx; | |
5069 | u64 count; | |
5070 | ||
5071 | ctx = perf_event_ctx_lock(event); | |
5072 | count = __perf_event_read_value(event, enabled, running); | |
5073 | perf_event_ctx_unlock(event, ctx); | |
5074 | ||
5075 | return count; | |
5076 | } | |
fb0459d7 | 5077 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
e53c0994 | 5078 | |
7d88962e | 5079 | static int __perf_read_group_add(struct perf_event *leader, |
fa8c2693 | 5080 | u64 read_format, u64 *values) |
3dab77fb | 5081 | { |
2aeb1883 | 5082 | struct perf_event_context *ctx = leader->ctx; |
fa8c2693 | 5083 | struct perf_event *sub; |
2aeb1883 | 5084 | unsigned long flags; |
fa8c2693 | 5085 | int n = 1; /* skip @nr */ |
7d88962e | 5086 | int ret; |
f63a8daa | 5087 | |
7d88962e SB |
5088 | ret = perf_event_read(leader, true); |
5089 | if (ret) | |
5090 | return ret; | |
abf4868b | 5091 | |
a9cd8194 PZ |
5092 | raw_spin_lock_irqsave(&ctx->lock, flags); |
5093 | ||
fa8c2693 PZ |
5094 | /* |
5095 | * Since we co-schedule groups, {enabled,running} times of siblings | |
5096 | * will be identical to those of the leader, so we only publish one | |
5097 | * set. | |
5098 | */ | |
5099 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
5100 | values[n++] += leader->total_time_enabled + | |
5101 | atomic64_read(&leader->child_total_time_enabled); | |
5102 | } | |
3dab77fb | 5103 | |
fa8c2693 PZ |
5104 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
5105 | values[n++] += leader->total_time_running + | |
5106 | atomic64_read(&leader->child_total_time_running); | |
5107 | } | |
5108 | ||
5109 | /* | |
5110 | * Write {count,id} tuples for every sibling. | |
5111 | */ | |
5112 | values[n++] += perf_event_count(leader); | |
abf4868b PZ |
5113 | if (read_format & PERF_FORMAT_ID) |
5114 | values[n++] = primary_event_id(leader); | |
3dab77fb | 5115 | |
edb39592 | 5116 | for_each_sibling_event(sub, leader) { |
fa8c2693 PZ |
5117 | values[n++] += perf_event_count(sub); |
5118 | if (read_format & PERF_FORMAT_ID) | |
5119 | values[n++] = primary_event_id(sub); | |
5120 | } | |
7d88962e | 5121 | |
2aeb1883 | 5122 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
7d88962e | 5123 | return 0; |
fa8c2693 | 5124 | } |
3dab77fb | 5125 | |
fa8c2693 PZ |
5126 | static int perf_read_group(struct perf_event *event, |
5127 | u64 read_format, char __user *buf) | |
5128 | { | |
5129 | struct perf_event *leader = event->group_leader, *child; | |
5130 | struct perf_event_context *ctx = leader->ctx; | |
7d88962e | 5131 | int ret; |
fa8c2693 | 5132 | u64 *values; |
3dab77fb | 5133 | |
fa8c2693 | 5134 | lockdep_assert_held(&ctx->mutex); |
3dab77fb | 5135 | |
fa8c2693 PZ |
5136 | values = kzalloc(event->read_size, GFP_KERNEL); |
5137 | if (!values) | |
5138 | return -ENOMEM; | |
3dab77fb | 5139 | |
fa8c2693 PZ |
5140 | values[0] = 1 + leader->nr_siblings; |
5141 | ||
5142 | /* | |
5143 | * By locking the child_mutex of the leader we effectively | |
5144 | * lock the child list of all siblings.. XXX explain how. | |
5145 | */ | |
5146 | mutex_lock(&leader->child_mutex); | |
abf4868b | 5147 | |
7d88962e SB |
5148 | ret = __perf_read_group_add(leader, read_format, values); |
5149 | if (ret) | |
5150 | goto unlock; | |
5151 | ||
5152 | list_for_each_entry(child, &leader->child_list, child_list) { | |
5153 | ret = __perf_read_group_add(child, read_format, values); | |
5154 | if (ret) | |
5155 | goto unlock; | |
5156 | } | |
abf4868b | 5157 | |
fa8c2693 | 5158 | mutex_unlock(&leader->child_mutex); |
abf4868b | 5159 | |
7d88962e | 5160 | ret = event->read_size; |
fa8c2693 PZ |
5161 | if (copy_to_user(buf, values, event->read_size)) |
5162 | ret = -EFAULT; | |
7d88962e | 5163 | goto out; |
fa8c2693 | 5164 | |
7d88962e SB |
5165 | unlock: |
5166 | mutex_unlock(&leader->child_mutex); | |
5167 | out: | |
fa8c2693 | 5168 | kfree(values); |
abf4868b | 5169 | return ret; |
3dab77fb PZ |
5170 | } |
5171 | ||
b15f495b | 5172 | static int perf_read_one(struct perf_event *event, |
3dab77fb PZ |
5173 | u64 read_format, char __user *buf) |
5174 | { | |
59ed446f | 5175 | u64 enabled, running; |
3dab77fb PZ |
5176 | u64 values[4]; |
5177 | int n = 0; | |
5178 | ||
ca0dd44c | 5179 | values[n++] = __perf_event_read_value(event, &enabled, &running); |
59ed446f PZ |
5180 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
5181 | values[n++] = enabled; | |
5182 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
5183 | values[n++] = running; | |
3dab77fb | 5184 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 5185 | values[n++] = primary_event_id(event); |
3dab77fb PZ |
5186 | |
5187 | if (copy_to_user(buf, values, n * sizeof(u64))) | |
5188 | return -EFAULT; | |
5189 | ||
5190 | return n * sizeof(u64); | |
5191 | } | |
5192 | ||
dc633982 JO |
5193 | static bool is_event_hup(struct perf_event *event) |
5194 | { | |
5195 | bool no_children; | |
5196 | ||
a69b0ca4 | 5197 | if (event->state > PERF_EVENT_STATE_EXIT) |
dc633982 JO |
5198 | return false; |
5199 | ||
5200 | mutex_lock(&event->child_mutex); | |
5201 | no_children = list_empty(&event->child_list); | |
5202 | mutex_unlock(&event->child_mutex); | |
5203 | return no_children; | |
5204 | } | |
5205 | ||
0793a61d | 5206 | /* |
cdd6c482 | 5207 | * Read the performance event - simple non blocking version for now |
0793a61d TG |
5208 | */ |
5209 | static ssize_t | |
b15f495b | 5210 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
0793a61d | 5211 | { |
cdd6c482 | 5212 | u64 read_format = event->attr.read_format; |
3dab77fb | 5213 | int ret; |
0793a61d | 5214 | |
3b6f9e5c | 5215 | /* |
788faab7 | 5216 | * Return end-of-file for a read on an event that is in |
3b6f9e5c PM |
5217 | * error state (i.e. because it was pinned but it couldn't be |
5218 | * scheduled on to the CPU at some point). | |
5219 | */ | |
cdd6c482 | 5220 | if (event->state == PERF_EVENT_STATE_ERROR) |
3b6f9e5c PM |
5221 | return 0; |
5222 | ||
c320c7b7 | 5223 | if (count < event->read_size) |
3dab77fb PZ |
5224 | return -ENOSPC; |
5225 | ||
cdd6c482 | 5226 | WARN_ON_ONCE(event->ctx->parent_ctx); |
3dab77fb | 5227 | if (read_format & PERF_FORMAT_GROUP) |
b15f495b | 5228 | ret = perf_read_group(event, read_format, buf); |
3dab77fb | 5229 | else |
b15f495b | 5230 | ret = perf_read_one(event, read_format, buf); |
0793a61d | 5231 | |
3dab77fb | 5232 | return ret; |
0793a61d TG |
5233 | } |
5234 | ||
0793a61d TG |
5235 | static ssize_t |
5236 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
5237 | { | |
cdd6c482 | 5238 | struct perf_event *event = file->private_data; |
f63a8daa PZ |
5239 | struct perf_event_context *ctx; |
5240 | int ret; | |
0793a61d | 5241 | |
da97e184 JFG |
5242 | ret = security_perf_event_read(event); |
5243 | if (ret) | |
5244 | return ret; | |
5245 | ||
f63a8daa | 5246 | ctx = perf_event_ctx_lock(event); |
b15f495b | 5247 | ret = __perf_read(event, buf, count); |
f63a8daa PZ |
5248 | perf_event_ctx_unlock(event, ctx); |
5249 | ||
5250 | return ret; | |
0793a61d TG |
5251 | } |
5252 | ||
9dd95748 | 5253 | static __poll_t perf_poll(struct file *file, poll_table *wait) |
0793a61d | 5254 | { |
cdd6c482 | 5255 | struct perf_event *event = file->private_data; |
56de4e8f | 5256 | struct perf_buffer *rb; |
a9a08845 | 5257 | __poll_t events = EPOLLHUP; |
c7138f37 | 5258 | |
e708d7ad | 5259 | poll_wait(file, &event->waitq, wait); |
179033b3 | 5260 | |
dc633982 | 5261 | if (is_event_hup(event)) |
179033b3 | 5262 | return events; |
c7138f37 | 5263 | |
10c6db11 | 5264 | /* |
9bb5d40c PZ |
5265 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
5266 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. | |
10c6db11 PZ |
5267 | */ |
5268 | mutex_lock(&event->mmap_mutex); | |
9bb5d40c PZ |
5269 | rb = event->rb; |
5270 | if (rb) | |
76369139 | 5271 | events = atomic_xchg(&rb->poll, 0); |
10c6db11 | 5272 | mutex_unlock(&event->mmap_mutex); |
0793a61d TG |
5273 | return events; |
5274 | } | |
5275 | ||
f63a8daa | 5276 | static void _perf_event_reset(struct perf_event *event) |
6de6a7b9 | 5277 | { |
7d88962e | 5278 | (void)perf_event_read(event, false); |
e7850595 | 5279 | local64_set(&event->count, 0); |
cdd6c482 | 5280 | perf_event_update_userpage(event); |
3df5edad PZ |
5281 | } |
5282 | ||
52ba4b0b LX |
5283 | /* Assume it's not an event with inherit set. */ |
5284 | u64 perf_event_pause(struct perf_event *event, bool reset) | |
5285 | { | |
5286 | struct perf_event_context *ctx; | |
5287 | u64 count; | |
5288 | ||
5289 | ctx = perf_event_ctx_lock(event); | |
5290 | WARN_ON_ONCE(event->attr.inherit); | |
5291 | _perf_event_disable(event); | |
5292 | count = local64_read(&event->count); | |
5293 | if (reset) | |
5294 | local64_set(&event->count, 0); | |
5295 | perf_event_ctx_unlock(event, ctx); | |
5296 | ||
5297 | return count; | |
5298 | } | |
5299 | EXPORT_SYMBOL_GPL(perf_event_pause); | |
5300 | ||
c93f7669 | 5301 | /* |
cdd6c482 IM |
5302 | * Holding the top-level event's child_mutex means that any |
5303 | * descendant process that has inherited this event will block | |
8ba289b8 | 5304 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
cdd6c482 | 5305 | * task existence requirements of perf_event_enable/disable. |
c93f7669 | 5306 | */ |
cdd6c482 IM |
5307 | static void perf_event_for_each_child(struct perf_event *event, |
5308 | void (*func)(struct perf_event *)) | |
3df5edad | 5309 | { |
cdd6c482 | 5310 | struct perf_event *child; |
3df5edad | 5311 | |
cdd6c482 | 5312 | WARN_ON_ONCE(event->ctx->parent_ctx); |
f63a8daa | 5313 | |
cdd6c482 IM |
5314 | mutex_lock(&event->child_mutex); |
5315 | func(event); | |
5316 | list_for_each_entry(child, &event->child_list, child_list) | |
3df5edad | 5317 | func(child); |
cdd6c482 | 5318 | mutex_unlock(&event->child_mutex); |
3df5edad PZ |
5319 | } |
5320 | ||
cdd6c482 IM |
5321 | static void perf_event_for_each(struct perf_event *event, |
5322 | void (*func)(struct perf_event *)) | |
3df5edad | 5323 | { |
cdd6c482 IM |
5324 | struct perf_event_context *ctx = event->ctx; |
5325 | struct perf_event *sibling; | |
3df5edad | 5326 | |
f63a8daa PZ |
5327 | lockdep_assert_held(&ctx->mutex); |
5328 | ||
cdd6c482 | 5329 | event = event->group_leader; |
75f937f2 | 5330 | |
cdd6c482 | 5331 | perf_event_for_each_child(event, func); |
edb39592 | 5332 | for_each_sibling_event(sibling, event) |
724b6daa | 5333 | perf_event_for_each_child(sibling, func); |
6de6a7b9 PZ |
5334 | } |
5335 | ||
fae3fde6 PZ |
5336 | static void __perf_event_period(struct perf_event *event, |
5337 | struct perf_cpu_context *cpuctx, | |
5338 | struct perf_event_context *ctx, | |
5339 | void *info) | |
c7999c6f | 5340 | { |
fae3fde6 | 5341 | u64 value = *((u64 *)info); |
c7999c6f | 5342 | bool active; |
08247e31 | 5343 | |
cdd6c482 | 5344 | if (event->attr.freq) { |
cdd6c482 | 5345 | event->attr.sample_freq = value; |
08247e31 | 5346 | } else { |
cdd6c482 IM |
5347 | event->attr.sample_period = value; |
5348 | event->hw.sample_period = value; | |
08247e31 | 5349 | } |
bad7192b PZ |
5350 | |
5351 | active = (event->state == PERF_EVENT_STATE_ACTIVE); | |
5352 | if (active) { | |
5353 | perf_pmu_disable(ctx->pmu); | |
1e02cd40 PZ |
5354 | /* |
5355 | * We could be throttled; unthrottle now to avoid the tick | |
5356 | * trying to unthrottle while we already re-started the event. | |
5357 | */ | |
5358 | if (event->hw.interrupts == MAX_INTERRUPTS) { | |
5359 | event->hw.interrupts = 0; | |
5360 | perf_log_throttle(event, 1); | |
5361 | } | |
bad7192b PZ |
5362 | event->pmu->stop(event, PERF_EF_UPDATE); |
5363 | } | |
5364 | ||
5365 | local64_set(&event->hw.period_left, 0); | |
5366 | ||
5367 | if (active) { | |
5368 | event->pmu->start(event, PERF_EF_RELOAD); | |
5369 | perf_pmu_enable(ctx->pmu); | |
5370 | } | |
c7999c6f PZ |
5371 | } |
5372 | ||
81ec3f3c JO |
5373 | static int perf_event_check_period(struct perf_event *event, u64 value) |
5374 | { | |
5375 | return event->pmu->check_period(event, value); | |
5376 | } | |
5377 | ||
3ca270fc | 5378 | static int _perf_event_period(struct perf_event *event, u64 value) |
c7999c6f | 5379 | { |
c7999c6f PZ |
5380 | if (!is_sampling_event(event)) |
5381 | return -EINVAL; | |
5382 | ||
c7999c6f PZ |
5383 | if (!value) |
5384 | return -EINVAL; | |
5385 | ||
5386 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) | |
5387 | return -EINVAL; | |
5388 | ||
81ec3f3c JO |
5389 | if (perf_event_check_period(event, value)) |
5390 | return -EINVAL; | |
5391 | ||
913a90bc RB |
5392 | if (!event->attr.freq && (value & (1ULL << 63))) |
5393 | return -EINVAL; | |
5394 | ||
fae3fde6 | 5395 | event_function_call(event, __perf_event_period, &value); |
08247e31 | 5396 | |
c7999c6f | 5397 | return 0; |
08247e31 PZ |
5398 | } |
5399 | ||
3ca270fc LX |
5400 | int perf_event_period(struct perf_event *event, u64 value) |
5401 | { | |
5402 | struct perf_event_context *ctx; | |
5403 | int ret; | |
5404 | ||
5405 | ctx = perf_event_ctx_lock(event); | |
5406 | ret = _perf_event_period(event, value); | |
5407 | perf_event_ctx_unlock(event, ctx); | |
5408 | ||
5409 | return ret; | |
5410 | } | |
5411 | EXPORT_SYMBOL_GPL(perf_event_period); | |
5412 | ||
ac9721f3 PZ |
5413 | static const struct file_operations perf_fops; |
5414 | ||
2903ff01 | 5415 | static inline int perf_fget_light(int fd, struct fd *p) |
ac9721f3 | 5416 | { |
2903ff01 AV |
5417 | struct fd f = fdget(fd); |
5418 | if (!f.file) | |
5419 | return -EBADF; | |
ac9721f3 | 5420 | |
2903ff01 AV |
5421 | if (f.file->f_op != &perf_fops) { |
5422 | fdput(f); | |
5423 | return -EBADF; | |
ac9721f3 | 5424 | } |
2903ff01 AV |
5425 | *p = f; |
5426 | return 0; | |
ac9721f3 PZ |
5427 | } |
5428 | ||
5429 | static int perf_event_set_output(struct perf_event *event, | |
5430 | struct perf_event *output_event); | |
6fb2915d | 5431 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
2541517c | 5432 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
32ff77e8 MC |
5433 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
5434 | struct perf_event_attr *attr); | |
a4be7c27 | 5435 | |
f63a8daa | 5436 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
d859e29f | 5437 | { |
cdd6c482 | 5438 | void (*func)(struct perf_event *); |
3df5edad | 5439 | u32 flags = arg; |
d859e29f PM |
5440 | |
5441 | switch (cmd) { | |
cdd6c482 | 5442 | case PERF_EVENT_IOC_ENABLE: |
f63a8daa | 5443 | func = _perf_event_enable; |
d859e29f | 5444 | break; |
cdd6c482 | 5445 | case PERF_EVENT_IOC_DISABLE: |
f63a8daa | 5446 | func = _perf_event_disable; |
79f14641 | 5447 | break; |
cdd6c482 | 5448 | case PERF_EVENT_IOC_RESET: |
f63a8daa | 5449 | func = _perf_event_reset; |
6de6a7b9 | 5450 | break; |
3df5edad | 5451 | |
cdd6c482 | 5452 | case PERF_EVENT_IOC_REFRESH: |
f63a8daa | 5453 | return _perf_event_refresh(event, arg); |
08247e31 | 5454 | |
cdd6c482 | 5455 | case PERF_EVENT_IOC_PERIOD: |
3ca270fc LX |
5456 | { |
5457 | u64 value; | |
08247e31 | 5458 | |
3ca270fc LX |
5459 | if (copy_from_user(&value, (u64 __user *)arg, sizeof(value))) |
5460 | return -EFAULT; | |
08247e31 | 5461 | |
3ca270fc LX |
5462 | return _perf_event_period(event, value); |
5463 | } | |
cf4957f1 JO |
5464 | case PERF_EVENT_IOC_ID: |
5465 | { | |
5466 | u64 id = primary_event_id(event); | |
5467 | ||
5468 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) | |
5469 | return -EFAULT; | |
5470 | return 0; | |
5471 | } | |
5472 | ||
cdd6c482 | 5473 | case PERF_EVENT_IOC_SET_OUTPUT: |
ac9721f3 | 5474 | { |
ac9721f3 | 5475 | int ret; |
ac9721f3 | 5476 | if (arg != -1) { |
2903ff01 AV |
5477 | struct perf_event *output_event; |
5478 | struct fd output; | |
5479 | ret = perf_fget_light(arg, &output); | |
5480 | if (ret) | |
5481 | return ret; | |
5482 | output_event = output.file->private_data; | |
5483 | ret = perf_event_set_output(event, output_event); | |
5484 | fdput(output); | |
5485 | } else { | |
5486 | ret = perf_event_set_output(event, NULL); | |
ac9721f3 | 5487 | } |
ac9721f3 PZ |
5488 | return ret; |
5489 | } | |
a4be7c27 | 5490 | |
6fb2915d LZ |
5491 | case PERF_EVENT_IOC_SET_FILTER: |
5492 | return perf_event_set_filter(event, (void __user *)arg); | |
5493 | ||
2541517c AS |
5494 | case PERF_EVENT_IOC_SET_BPF: |
5495 | return perf_event_set_bpf_prog(event, arg); | |
5496 | ||
86e7972f | 5497 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
56de4e8f | 5498 | struct perf_buffer *rb; |
86e7972f WN |
5499 | |
5500 | rcu_read_lock(); | |
5501 | rb = rcu_dereference(event->rb); | |
5502 | if (!rb || !rb->nr_pages) { | |
5503 | rcu_read_unlock(); | |
5504 | return -EINVAL; | |
5505 | } | |
5506 | rb_toggle_paused(rb, !!arg); | |
5507 | rcu_read_unlock(); | |
5508 | return 0; | |
5509 | } | |
f371b304 YS |
5510 | |
5511 | case PERF_EVENT_IOC_QUERY_BPF: | |
f4e2298e | 5512 | return perf_event_query_prog_array(event, (void __user *)arg); |
32ff77e8 MC |
5513 | |
5514 | case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: { | |
5515 | struct perf_event_attr new_attr; | |
5516 | int err = perf_copy_attr((struct perf_event_attr __user *)arg, | |
5517 | &new_attr); | |
5518 | ||
5519 | if (err) | |
5520 | return err; | |
5521 | ||
5522 | return perf_event_modify_attr(event, &new_attr); | |
5523 | } | |
d859e29f | 5524 | default: |
3df5edad | 5525 | return -ENOTTY; |
d859e29f | 5526 | } |
3df5edad PZ |
5527 | |
5528 | if (flags & PERF_IOC_FLAG_GROUP) | |
cdd6c482 | 5529 | perf_event_for_each(event, func); |
3df5edad | 5530 | else |
cdd6c482 | 5531 | perf_event_for_each_child(event, func); |
3df5edad PZ |
5532 | |
5533 | return 0; | |
d859e29f PM |
5534 | } |
5535 | ||
f63a8daa PZ |
5536 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
5537 | { | |
5538 | struct perf_event *event = file->private_data; | |
5539 | struct perf_event_context *ctx; | |
5540 | long ret; | |
5541 | ||
da97e184 JFG |
5542 | /* Treat ioctl like writes as it is likely a mutating operation. */ |
5543 | ret = security_perf_event_write(event); | |
5544 | if (ret) | |
5545 | return ret; | |
5546 | ||
f63a8daa PZ |
5547 | ctx = perf_event_ctx_lock(event); |
5548 | ret = _perf_ioctl(event, cmd, arg); | |
5549 | perf_event_ctx_unlock(event, ctx); | |
5550 | ||
5551 | return ret; | |
5552 | } | |
5553 | ||
b3f20785 PM |
5554 | #ifdef CONFIG_COMPAT |
5555 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, | |
5556 | unsigned long arg) | |
5557 | { | |
5558 | switch (_IOC_NR(cmd)) { | |
5559 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): | |
5560 | case _IOC_NR(PERF_EVENT_IOC_ID): | |
82489c5f ES |
5561 | case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF): |
5562 | case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES): | |
b3f20785 PM |
5563 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
5564 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | |
5565 | cmd &= ~IOCSIZE_MASK; | |
5566 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; | |
5567 | } | |
5568 | break; | |
5569 | } | |
5570 | return perf_ioctl(file, cmd, arg); | |
5571 | } | |
5572 | #else | |
5573 | # define perf_compat_ioctl NULL | |
5574 | #endif | |
5575 | ||
cdd6c482 | 5576 | int perf_event_task_enable(void) |
771d7cde | 5577 | { |
f63a8daa | 5578 | struct perf_event_context *ctx; |
cdd6c482 | 5579 | struct perf_event *event; |
771d7cde | 5580 | |
cdd6c482 | 5581 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
5582 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
5583 | ctx = perf_event_ctx_lock(event); | |
5584 | perf_event_for_each_child(event, _perf_event_enable); | |
5585 | perf_event_ctx_unlock(event, ctx); | |
5586 | } | |
cdd6c482 | 5587 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
5588 | |
5589 | return 0; | |
5590 | } | |
5591 | ||
cdd6c482 | 5592 | int perf_event_task_disable(void) |
771d7cde | 5593 | { |
f63a8daa | 5594 | struct perf_event_context *ctx; |
cdd6c482 | 5595 | struct perf_event *event; |
771d7cde | 5596 | |
cdd6c482 | 5597 | mutex_lock(¤t->perf_event_mutex); |
f63a8daa PZ |
5598 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
5599 | ctx = perf_event_ctx_lock(event); | |
5600 | perf_event_for_each_child(event, _perf_event_disable); | |
5601 | perf_event_ctx_unlock(event, ctx); | |
5602 | } | |
cdd6c482 | 5603 | mutex_unlock(¤t->perf_event_mutex); |
771d7cde PZ |
5604 | |
5605 | return 0; | |
5606 | } | |
5607 | ||
cdd6c482 | 5608 | static int perf_event_index(struct perf_event *event) |
194002b2 | 5609 | { |
a4eaf7f1 PZ |
5610 | if (event->hw.state & PERF_HES_STOPPED) |
5611 | return 0; | |
5612 | ||
cdd6c482 | 5613 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
194002b2 PZ |
5614 | return 0; |
5615 | ||
35edc2a5 | 5616 | return event->pmu->event_idx(event); |
194002b2 PZ |
5617 | } |
5618 | ||
c4794295 | 5619 | static void calc_timer_values(struct perf_event *event, |
e3f3541c | 5620 | u64 *now, |
7f310a5d EM |
5621 | u64 *enabled, |
5622 | u64 *running) | |
c4794295 | 5623 | { |
e3f3541c | 5624 | u64 ctx_time; |
c4794295 | 5625 | |
e3f3541c PZ |
5626 | *now = perf_clock(); |
5627 | ctx_time = event->shadow_ctx_time + *now; | |
0d3d73aa | 5628 | __perf_update_times(event, ctx_time, enabled, running); |
c4794295 EM |
5629 | } |
5630 | ||
fa731587 PZ |
5631 | static void perf_event_init_userpage(struct perf_event *event) |
5632 | { | |
5633 | struct perf_event_mmap_page *userpg; | |
56de4e8f | 5634 | struct perf_buffer *rb; |
fa731587 PZ |
5635 | |
5636 | rcu_read_lock(); | |
5637 | rb = rcu_dereference(event->rb); | |
5638 | if (!rb) | |
5639 | goto unlock; | |
5640 | ||
5641 | userpg = rb->user_page; | |
5642 | ||
5643 | /* Allow new userspace to detect that bit 0 is deprecated */ | |
5644 | userpg->cap_bit0_is_deprecated = 1; | |
5645 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); | |
e8c6deac AS |
5646 | userpg->data_offset = PAGE_SIZE; |
5647 | userpg->data_size = perf_data_size(rb); | |
fa731587 PZ |
5648 | |
5649 | unlock: | |
5650 | rcu_read_unlock(); | |
5651 | } | |
5652 | ||
c1317ec2 AL |
5653 | void __weak arch_perf_update_userpage( |
5654 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) | |
e3f3541c PZ |
5655 | { |
5656 | } | |
5657 | ||
38ff667b PZ |
5658 | /* |
5659 | * Callers need to ensure there can be no nesting of this function, otherwise | |
5660 | * the seqlock logic goes bad. We can not serialize this because the arch | |
5661 | * code calls this from NMI context. | |
5662 | */ | |
cdd6c482 | 5663 | void perf_event_update_userpage(struct perf_event *event) |
37d81828 | 5664 | { |
cdd6c482 | 5665 | struct perf_event_mmap_page *userpg; |
56de4e8f | 5666 | struct perf_buffer *rb; |
e3f3541c | 5667 | u64 enabled, running, now; |
38ff667b PZ |
5668 | |
5669 | rcu_read_lock(); | |
5ec4c599 PZ |
5670 | rb = rcu_dereference(event->rb); |
5671 | if (!rb) | |
5672 | goto unlock; | |
5673 | ||
0d641208 EM |
5674 | /* |
5675 | * compute total_time_enabled, total_time_running | |
5676 | * based on snapshot values taken when the event | |
5677 | * was last scheduled in. | |
5678 | * | |
5679 | * we cannot simply called update_context_time() | |
5680 | * because of locking issue as we can be called in | |
5681 | * NMI context | |
5682 | */ | |
e3f3541c | 5683 | calc_timer_values(event, &now, &enabled, &running); |
38ff667b | 5684 | |
76369139 | 5685 | userpg = rb->user_page; |
7b732a75 | 5686 | /* |
9d2dcc8f MF |
5687 | * Disable preemption to guarantee consistent time stamps are stored to |
5688 | * the user page. | |
7b732a75 PZ |
5689 | */ |
5690 | preempt_disable(); | |
37d81828 | 5691 | ++userpg->lock; |
92f22a38 | 5692 | barrier(); |
cdd6c482 | 5693 | userpg->index = perf_event_index(event); |
b5e58793 | 5694 | userpg->offset = perf_event_count(event); |
365a4038 | 5695 | if (userpg->index) |
e7850595 | 5696 | userpg->offset -= local64_read(&event->hw.prev_count); |
7b732a75 | 5697 | |
0d641208 | 5698 | userpg->time_enabled = enabled + |
cdd6c482 | 5699 | atomic64_read(&event->child_total_time_enabled); |
7f8b4e4e | 5700 | |
0d641208 | 5701 | userpg->time_running = running + |
cdd6c482 | 5702 | atomic64_read(&event->child_total_time_running); |
7f8b4e4e | 5703 | |
c1317ec2 | 5704 | arch_perf_update_userpage(event, userpg, now); |
e3f3541c | 5705 | |
92f22a38 | 5706 | barrier(); |
37d81828 | 5707 | ++userpg->lock; |
7b732a75 | 5708 | preempt_enable(); |
38ff667b | 5709 | unlock: |
7b732a75 | 5710 | rcu_read_unlock(); |
37d81828 | 5711 | } |
82975c46 | 5712 | EXPORT_SYMBOL_GPL(perf_event_update_userpage); |
37d81828 | 5713 | |
9e3ed2d7 | 5714 | static vm_fault_t perf_mmap_fault(struct vm_fault *vmf) |
906010b2 | 5715 | { |
11bac800 | 5716 | struct perf_event *event = vmf->vma->vm_file->private_data; |
56de4e8f | 5717 | struct perf_buffer *rb; |
9e3ed2d7 | 5718 | vm_fault_t ret = VM_FAULT_SIGBUS; |
906010b2 PZ |
5719 | |
5720 | if (vmf->flags & FAULT_FLAG_MKWRITE) { | |
5721 | if (vmf->pgoff == 0) | |
5722 | ret = 0; | |
5723 | return ret; | |
5724 | } | |
5725 | ||
5726 | rcu_read_lock(); | |
76369139 FW |
5727 | rb = rcu_dereference(event->rb); |
5728 | if (!rb) | |
906010b2 PZ |
5729 | goto unlock; |
5730 | ||
5731 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) | |
5732 | goto unlock; | |
5733 | ||
76369139 | 5734 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
906010b2 PZ |
5735 | if (!vmf->page) |
5736 | goto unlock; | |
5737 | ||
5738 | get_page(vmf->page); | |
11bac800 | 5739 | vmf->page->mapping = vmf->vma->vm_file->f_mapping; |
906010b2 PZ |
5740 | vmf->page->index = vmf->pgoff; |
5741 | ||
5742 | ret = 0; | |
5743 | unlock: | |
5744 | rcu_read_unlock(); | |
5745 | ||
5746 | return ret; | |
5747 | } | |
5748 | ||
10c6db11 | 5749 | static void ring_buffer_attach(struct perf_event *event, |
56de4e8f | 5750 | struct perf_buffer *rb) |
10c6db11 | 5751 | { |
56de4e8f | 5752 | struct perf_buffer *old_rb = NULL; |
10c6db11 PZ |
5753 | unsigned long flags; |
5754 | ||
b69cf536 PZ |
5755 | if (event->rb) { |
5756 | /* | |
5757 | * Should be impossible, we set this when removing | |
5758 | * event->rb_entry and wait/clear when adding event->rb_entry. | |
5759 | */ | |
5760 | WARN_ON_ONCE(event->rcu_pending); | |
10c6db11 | 5761 | |
b69cf536 | 5762 | old_rb = event->rb; |
b69cf536 PZ |
5763 | spin_lock_irqsave(&old_rb->event_lock, flags); |
5764 | list_del_rcu(&event->rb_entry); | |
5765 | spin_unlock_irqrestore(&old_rb->event_lock, flags); | |
10c6db11 | 5766 | |
2f993cf0 ON |
5767 | event->rcu_batches = get_state_synchronize_rcu(); |
5768 | event->rcu_pending = 1; | |
b69cf536 | 5769 | } |
10c6db11 | 5770 | |
b69cf536 | 5771 | if (rb) { |
2f993cf0 ON |
5772 | if (event->rcu_pending) { |
5773 | cond_synchronize_rcu(event->rcu_batches); | |
5774 | event->rcu_pending = 0; | |
5775 | } | |
5776 | ||
b69cf536 PZ |
5777 | spin_lock_irqsave(&rb->event_lock, flags); |
5778 | list_add_rcu(&event->rb_entry, &rb->event_list); | |
5779 | spin_unlock_irqrestore(&rb->event_lock, flags); | |
5780 | } | |
5781 | ||
767ae086 AS |
5782 | /* |
5783 | * Avoid racing with perf_mmap_close(AUX): stop the event | |
5784 | * before swizzling the event::rb pointer; if it's getting | |
5785 | * unmapped, its aux_mmap_count will be 0 and it won't | |
5786 | * restart. See the comment in __perf_pmu_output_stop(). | |
5787 | * | |
5788 | * Data will inevitably be lost when set_output is done in | |
5789 | * mid-air, but then again, whoever does it like this is | |
5790 | * not in for the data anyway. | |
5791 | */ | |
5792 | if (has_aux(event)) | |
5793 | perf_event_stop(event, 0); | |
5794 | ||
b69cf536 PZ |
5795 | rcu_assign_pointer(event->rb, rb); |
5796 | ||
5797 | if (old_rb) { | |
5798 | ring_buffer_put(old_rb); | |
5799 | /* | |
5800 | * Since we detached before setting the new rb, so that we | |
5801 | * could attach the new rb, we could have missed a wakeup. | |
5802 | * Provide it now. | |
5803 | */ | |
5804 | wake_up_all(&event->waitq); | |
5805 | } | |
10c6db11 PZ |
5806 | } |
5807 | ||
5808 | static void ring_buffer_wakeup(struct perf_event *event) | |
5809 | { | |
56de4e8f | 5810 | struct perf_buffer *rb; |
10c6db11 PZ |
5811 | |
5812 | rcu_read_lock(); | |
5813 | rb = rcu_dereference(event->rb); | |
9bb5d40c PZ |
5814 | if (rb) { |
5815 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) | |
5816 | wake_up_all(&event->waitq); | |
5817 | } | |
10c6db11 PZ |
5818 | rcu_read_unlock(); |
5819 | } | |
5820 | ||
56de4e8f | 5821 | struct perf_buffer *ring_buffer_get(struct perf_event *event) |
7b732a75 | 5822 | { |
56de4e8f | 5823 | struct perf_buffer *rb; |
7b732a75 | 5824 | |
ac9721f3 | 5825 | rcu_read_lock(); |
76369139 FW |
5826 | rb = rcu_dereference(event->rb); |
5827 | if (rb) { | |
fecb8ed2 | 5828 | if (!refcount_inc_not_zero(&rb->refcount)) |
76369139 | 5829 | rb = NULL; |
ac9721f3 PZ |
5830 | } |
5831 | rcu_read_unlock(); | |
5832 | ||
76369139 | 5833 | return rb; |
ac9721f3 PZ |
5834 | } |
5835 | ||
56de4e8f | 5836 | void ring_buffer_put(struct perf_buffer *rb) |
ac9721f3 | 5837 | { |
fecb8ed2 | 5838 | if (!refcount_dec_and_test(&rb->refcount)) |
ac9721f3 | 5839 | return; |
7b732a75 | 5840 | |
9bb5d40c | 5841 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
10c6db11 | 5842 | |
76369139 | 5843 | call_rcu(&rb->rcu_head, rb_free_rcu); |
7b732a75 PZ |
5844 | } |
5845 | ||
5846 | static void perf_mmap_open(struct vm_area_struct *vma) | |
5847 | { | |
cdd6c482 | 5848 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5849 | |
cdd6c482 | 5850 | atomic_inc(&event->mmap_count); |
9bb5d40c | 5851 | atomic_inc(&event->rb->mmap_count); |
1e0fb9ec | 5852 | |
45bfb2e5 PZ |
5853 | if (vma->vm_pgoff) |
5854 | atomic_inc(&event->rb->aux_mmap_count); | |
5855 | ||
1e0fb9ec | 5856 | if (event->pmu->event_mapped) |
bfe33492 | 5857 | event->pmu->event_mapped(event, vma->vm_mm); |
7b732a75 PZ |
5858 | } |
5859 | ||
95ff4ca2 AS |
5860 | static void perf_pmu_output_stop(struct perf_event *event); |
5861 | ||
9bb5d40c PZ |
5862 | /* |
5863 | * A buffer can be mmap()ed multiple times; either directly through the same | |
5864 | * event, or through other events by use of perf_event_set_output(). | |
5865 | * | |
5866 | * In order to undo the VM accounting done by perf_mmap() we need to destroy | |
5867 | * the buffer here, where we still have a VM context. This means we need | |
5868 | * to detach all events redirecting to us. | |
5869 | */ | |
7b732a75 PZ |
5870 | static void perf_mmap_close(struct vm_area_struct *vma) |
5871 | { | |
cdd6c482 | 5872 | struct perf_event *event = vma->vm_file->private_data; |
7b732a75 | 5873 | |
56de4e8f | 5874 | struct perf_buffer *rb = ring_buffer_get(event); |
9bb5d40c PZ |
5875 | struct user_struct *mmap_user = rb->mmap_user; |
5876 | int mmap_locked = rb->mmap_locked; | |
5877 | unsigned long size = perf_data_size(rb); | |
789f90fc | 5878 | |
1e0fb9ec | 5879 | if (event->pmu->event_unmapped) |
bfe33492 | 5880 | event->pmu->event_unmapped(event, vma->vm_mm); |
1e0fb9ec | 5881 | |
45bfb2e5 PZ |
5882 | /* |
5883 | * rb->aux_mmap_count will always drop before rb->mmap_count and | |
5884 | * event->mmap_count, so it is ok to use event->mmap_mutex to | |
5885 | * serialize with perf_mmap here. | |
5886 | */ | |
5887 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && | |
5888 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { | |
95ff4ca2 AS |
5889 | /* |
5890 | * Stop all AUX events that are writing to this buffer, | |
5891 | * so that we can free its AUX pages and corresponding PMU | |
5892 | * data. Note that after rb::aux_mmap_count dropped to zero, | |
5893 | * they won't start any more (see perf_aux_output_begin()). | |
5894 | */ | |
5895 | perf_pmu_output_stop(event); | |
5896 | ||
5897 | /* now it's safe to free the pages */ | |
36b3db03 AS |
5898 | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); |
5899 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
45bfb2e5 | 5900 | |
95ff4ca2 | 5901 | /* this has to be the last one */ |
45bfb2e5 | 5902 | rb_free_aux(rb); |
ca3bb3d0 | 5903 | WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); |
95ff4ca2 | 5904 | |
45bfb2e5 PZ |
5905 | mutex_unlock(&event->mmap_mutex); |
5906 | } | |
5907 | ||
9bb5d40c PZ |
5908 | atomic_dec(&rb->mmap_count); |
5909 | ||
5910 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) | |
b69cf536 | 5911 | goto out_put; |
9bb5d40c | 5912 | |
b69cf536 | 5913 | ring_buffer_attach(event, NULL); |
9bb5d40c PZ |
5914 | mutex_unlock(&event->mmap_mutex); |
5915 | ||
5916 | /* If there's still other mmap()s of this buffer, we're done. */ | |
b69cf536 PZ |
5917 | if (atomic_read(&rb->mmap_count)) |
5918 | goto out_put; | |
ac9721f3 | 5919 | |
9bb5d40c PZ |
5920 | /* |
5921 | * No other mmap()s, detach from all other events that might redirect | |
5922 | * into the now unreachable buffer. Somewhat complicated by the | |
5923 | * fact that rb::event_lock otherwise nests inside mmap_mutex. | |
5924 | */ | |
5925 | again: | |
5926 | rcu_read_lock(); | |
5927 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { | |
5928 | if (!atomic_long_inc_not_zero(&event->refcount)) { | |
5929 | /* | |
5930 | * This event is en-route to free_event() which will | |
5931 | * detach it and remove it from the list. | |
5932 | */ | |
5933 | continue; | |
5934 | } | |
5935 | rcu_read_unlock(); | |
789f90fc | 5936 | |
9bb5d40c PZ |
5937 | mutex_lock(&event->mmap_mutex); |
5938 | /* | |
5939 | * Check we didn't race with perf_event_set_output() which can | |
5940 | * swizzle the rb from under us while we were waiting to | |
5941 | * acquire mmap_mutex. | |
5942 | * | |
5943 | * If we find a different rb; ignore this event, a next | |
5944 | * iteration will no longer find it on the list. We have to | |
5945 | * still restart the iteration to make sure we're not now | |
5946 | * iterating the wrong list. | |
5947 | */ | |
b69cf536 PZ |
5948 | if (event->rb == rb) |
5949 | ring_buffer_attach(event, NULL); | |
5950 | ||
cdd6c482 | 5951 | mutex_unlock(&event->mmap_mutex); |
9bb5d40c | 5952 | put_event(event); |
ac9721f3 | 5953 | |
9bb5d40c PZ |
5954 | /* |
5955 | * Restart the iteration; either we're on the wrong list or | |
5956 | * destroyed its integrity by doing a deletion. | |
5957 | */ | |
5958 | goto again; | |
7b732a75 | 5959 | } |
9bb5d40c PZ |
5960 | rcu_read_unlock(); |
5961 | ||
5962 | /* | |
5963 | * It could be there's still a few 0-ref events on the list; they'll | |
5964 | * get cleaned up by free_event() -- they'll also still have their | |
5965 | * ref on the rb and will free it whenever they are done with it. | |
5966 | * | |
5967 | * Aside from that, this buffer is 'fully' detached and unmapped, | |
5968 | * undo the VM accounting. | |
5969 | */ | |
5970 | ||
d44248a4 SL |
5971 | atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked, |
5972 | &mmap_user->locked_vm); | |
70f8a3ca | 5973 | atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm); |
9bb5d40c PZ |
5974 | free_uid(mmap_user); |
5975 | ||
b69cf536 | 5976 | out_put: |
9bb5d40c | 5977 | ring_buffer_put(rb); /* could be last */ |
37d81828 PM |
5978 | } |
5979 | ||
f0f37e2f | 5980 | static const struct vm_operations_struct perf_mmap_vmops = { |
43a21ea8 | 5981 | .open = perf_mmap_open, |
fca0c116 | 5982 | .close = perf_mmap_close, /* non mergeable */ |
43a21ea8 PZ |
5983 | .fault = perf_mmap_fault, |
5984 | .page_mkwrite = perf_mmap_fault, | |
37d81828 PM |
5985 | }; |
5986 | ||
5987 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) | |
5988 | { | |
cdd6c482 | 5989 | struct perf_event *event = file->private_data; |
22a4f650 | 5990 | unsigned long user_locked, user_lock_limit; |
789f90fc | 5991 | struct user_struct *user = current_user(); |
56de4e8f | 5992 | struct perf_buffer *rb = NULL; |
22a4f650 | 5993 | unsigned long locked, lock_limit; |
7b732a75 PZ |
5994 | unsigned long vma_size; |
5995 | unsigned long nr_pages; | |
45bfb2e5 | 5996 | long user_extra = 0, extra = 0; |
d57e34fd | 5997 | int ret = 0, flags = 0; |
37d81828 | 5998 | |
c7920614 PZ |
5999 | /* |
6000 | * Don't allow mmap() of inherited per-task counters. This would | |
6001 | * create a performance issue due to all children writing to the | |
76369139 | 6002 | * same rb. |
c7920614 PZ |
6003 | */ |
6004 | if (event->cpu == -1 && event->attr.inherit) | |
6005 | return -EINVAL; | |
6006 | ||
43a21ea8 | 6007 | if (!(vma->vm_flags & VM_SHARED)) |
37d81828 | 6008 | return -EINVAL; |
7b732a75 | 6009 | |
da97e184 JFG |
6010 | ret = security_perf_event_read(event); |
6011 | if (ret) | |
6012 | return ret; | |
6013 | ||
7b732a75 | 6014 | vma_size = vma->vm_end - vma->vm_start; |
45bfb2e5 PZ |
6015 | |
6016 | if (vma->vm_pgoff == 0) { | |
6017 | nr_pages = (vma_size / PAGE_SIZE) - 1; | |
6018 | } else { | |
6019 | /* | |
6020 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already | |
6021 | * mapped, all subsequent mappings should have the same size | |
6022 | * and offset. Must be above the normal perf buffer. | |
6023 | */ | |
6024 | u64 aux_offset, aux_size; | |
6025 | ||
6026 | if (!event->rb) | |
6027 | return -EINVAL; | |
6028 | ||
6029 | nr_pages = vma_size / PAGE_SIZE; | |
6030 | ||
6031 | mutex_lock(&event->mmap_mutex); | |
6032 | ret = -EINVAL; | |
6033 | ||
6034 | rb = event->rb; | |
6035 | if (!rb) | |
6036 | goto aux_unlock; | |
6037 | ||
6aa7de05 MR |
6038 | aux_offset = READ_ONCE(rb->user_page->aux_offset); |
6039 | aux_size = READ_ONCE(rb->user_page->aux_size); | |
45bfb2e5 PZ |
6040 | |
6041 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) | |
6042 | goto aux_unlock; | |
6043 | ||
6044 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) | |
6045 | goto aux_unlock; | |
6046 | ||
6047 | /* already mapped with a different offset */ | |
6048 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) | |
6049 | goto aux_unlock; | |
6050 | ||
6051 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) | |
6052 | goto aux_unlock; | |
6053 | ||
6054 | /* already mapped with a different size */ | |
6055 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) | |
6056 | goto aux_unlock; | |
6057 | ||
6058 | if (!is_power_of_2(nr_pages)) | |
6059 | goto aux_unlock; | |
6060 | ||
6061 | if (!atomic_inc_not_zero(&rb->mmap_count)) | |
6062 | goto aux_unlock; | |
6063 | ||
6064 | if (rb_has_aux(rb)) { | |
6065 | atomic_inc(&rb->aux_mmap_count); | |
6066 | ret = 0; | |
6067 | goto unlock; | |
6068 | } | |
6069 | ||
6070 | atomic_set(&rb->aux_mmap_count, 1); | |
6071 | user_extra = nr_pages; | |
6072 | ||
6073 | goto accounting; | |
6074 | } | |
7b732a75 | 6075 | |
7730d865 | 6076 | /* |
76369139 | 6077 | * If we have rb pages ensure they're a power-of-two number, so we |
7730d865 PZ |
6078 | * can do bitmasks instead of modulo. |
6079 | */ | |
2ed11312 | 6080 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
37d81828 PM |
6081 | return -EINVAL; |
6082 | ||
7b732a75 | 6083 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
37d81828 PM |
6084 | return -EINVAL; |
6085 | ||
cdd6c482 | 6086 | WARN_ON_ONCE(event->ctx->parent_ctx); |
9bb5d40c | 6087 | again: |
cdd6c482 | 6088 | mutex_lock(&event->mmap_mutex); |
76369139 | 6089 | if (event->rb) { |
9bb5d40c | 6090 | if (event->rb->nr_pages != nr_pages) { |
ebb3c4c4 | 6091 | ret = -EINVAL; |
9bb5d40c PZ |
6092 | goto unlock; |
6093 | } | |
6094 | ||
6095 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { | |
6096 | /* | |
6097 | * Raced against perf_mmap_close() through | |
6098 | * perf_event_set_output(). Try again, hope for better | |
6099 | * luck. | |
6100 | */ | |
6101 | mutex_unlock(&event->mmap_mutex); | |
6102 | goto again; | |
6103 | } | |
6104 | ||
ebb3c4c4 PZ |
6105 | goto unlock; |
6106 | } | |
6107 | ||
789f90fc | 6108 | user_extra = nr_pages + 1; |
45bfb2e5 PZ |
6109 | |
6110 | accounting: | |
cdd6c482 | 6111 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
a3862d3f IM |
6112 | |
6113 | /* | |
6114 | * Increase the limit linearly with more CPUs: | |
6115 | */ | |
6116 | user_lock_limit *= num_online_cpus(); | |
6117 | ||
00346155 SL |
6118 | user_locked = atomic_long_read(&user->locked_vm); |
6119 | ||
6120 | /* | |
6121 | * sysctl_perf_event_mlock may have changed, so that | |
6122 | * user->locked_vm > user_lock_limit | |
6123 | */ | |
6124 | if (user_locked > user_lock_limit) | |
6125 | user_locked = user_lock_limit; | |
6126 | user_locked += user_extra; | |
c5078f78 | 6127 | |
c4b75479 | 6128 | if (user_locked > user_lock_limit) { |
d44248a4 SL |
6129 | /* |
6130 | * charge locked_vm until it hits user_lock_limit; | |
6131 | * charge the rest from pinned_vm | |
6132 | */ | |
789f90fc | 6133 | extra = user_locked - user_lock_limit; |
d44248a4 SL |
6134 | user_extra -= extra; |
6135 | } | |
7b732a75 | 6136 | |
78d7d407 | 6137 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
7b732a75 | 6138 | lock_limit >>= PAGE_SHIFT; |
70f8a3ca | 6139 | locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; |
7b732a75 | 6140 | |
da97e184 | 6141 | if ((locked > lock_limit) && perf_is_paranoid() && |
459ec28a | 6142 | !capable(CAP_IPC_LOCK)) { |
ebb3c4c4 PZ |
6143 | ret = -EPERM; |
6144 | goto unlock; | |
6145 | } | |
7b732a75 | 6146 | |
45bfb2e5 | 6147 | WARN_ON(!rb && event->rb); |
906010b2 | 6148 | |
d57e34fd | 6149 | if (vma->vm_flags & VM_WRITE) |
76369139 | 6150 | flags |= RING_BUFFER_WRITABLE; |
d57e34fd | 6151 | |
76369139 | 6152 | if (!rb) { |
45bfb2e5 PZ |
6153 | rb = rb_alloc(nr_pages, |
6154 | event->attr.watermark ? event->attr.wakeup_watermark : 0, | |
6155 | event->cpu, flags); | |
26cb63ad | 6156 | |
45bfb2e5 PZ |
6157 | if (!rb) { |
6158 | ret = -ENOMEM; | |
6159 | goto unlock; | |
6160 | } | |
43a21ea8 | 6161 | |
45bfb2e5 PZ |
6162 | atomic_set(&rb->mmap_count, 1); |
6163 | rb->mmap_user = get_current_user(); | |
6164 | rb->mmap_locked = extra; | |
26cb63ad | 6165 | |
45bfb2e5 | 6166 | ring_buffer_attach(event, rb); |
ac9721f3 | 6167 | |
45bfb2e5 PZ |
6168 | perf_event_init_userpage(event); |
6169 | perf_event_update_userpage(event); | |
6170 | } else { | |
1a594131 AS |
6171 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
6172 | event->attr.aux_watermark, flags); | |
45bfb2e5 PZ |
6173 | if (!ret) |
6174 | rb->aux_mmap_locked = extra; | |
6175 | } | |
9a0f05cb | 6176 | |
ebb3c4c4 | 6177 | unlock: |
45bfb2e5 PZ |
6178 | if (!ret) { |
6179 | atomic_long_add(user_extra, &user->locked_vm); | |
70f8a3ca | 6180 | atomic64_add(extra, &vma->vm_mm->pinned_vm); |
45bfb2e5 | 6181 | |
ac9721f3 | 6182 | atomic_inc(&event->mmap_count); |
45bfb2e5 PZ |
6183 | } else if (rb) { |
6184 | atomic_dec(&rb->mmap_count); | |
6185 | } | |
6186 | aux_unlock: | |
cdd6c482 | 6187 | mutex_unlock(&event->mmap_mutex); |
37d81828 | 6188 | |
9bb5d40c PZ |
6189 | /* |
6190 | * Since pinned accounting is per vm we cannot allow fork() to copy our | |
6191 | * vma. | |
6192 | */ | |
26cb63ad | 6193 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
37d81828 | 6194 | vma->vm_ops = &perf_mmap_vmops; |
7b732a75 | 6195 | |
1e0fb9ec | 6196 | if (event->pmu->event_mapped) |
bfe33492 | 6197 | event->pmu->event_mapped(event, vma->vm_mm); |
1e0fb9ec | 6198 | |
7b732a75 | 6199 | return ret; |
37d81828 PM |
6200 | } |
6201 | ||
3c446b3d PZ |
6202 | static int perf_fasync(int fd, struct file *filp, int on) |
6203 | { | |
496ad9aa | 6204 | struct inode *inode = file_inode(filp); |
cdd6c482 | 6205 | struct perf_event *event = filp->private_data; |
3c446b3d PZ |
6206 | int retval; |
6207 | ||
5955102c | 6208 | inode_lock(inode); |
cdd6c482 | 6209 | retval = fasync_helper(fd, filp, on, &event->fasync); |
5955102c | 6210 | inode_unlock(inode); |
3c446b3d PZ |
6211 | |
6212 | if (retval < 0) | |
6213 | return retval; | |
6214 | ||
6215 | return 0; | |
6216 | } | |
6217 | ||
0793a61d | 6218 | static const struct file_operations perf_fops = { |
3326c1ce | 6219 | .llseek = no_llseek, |
0793a61d TG |
6220 | .release = perf_release, |
6221 | .read = perf_read, | |
6222 | .poll = perf_poll, | |
d859e29f | 6223 | .unlocked_ioctl = perf_ioctl, |
b3f20785 | 6224 | .compat_ioctl = perf_compat_ioctl, |
37d81828 | 6225 | .mmap = perf_mmap, |
3c446b3d | 6226 | .fasync = perf_fasync, |
0793a61d TG |
6227 | }; |
6228 | ||
925d519a | 6229 | /* |
cdd6c482 | 6230 | * Perf event wakeup |
925d519a PZ |
6231 | * |
6232 | * If there's data, ensure we set the poll() state and publish everything | |
6233 | * to user-space before waking everybody up. | |
6234 | */ | |
6235 | ||
fed66e2c PZ |
6236 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
6237 | { | |
6238 | /* only the parent has fasync state */ | |
6239 | if (event->parent) | |
6240 | event = event->parent; | |
6241 | return &event->fasync; | |
6242 | } | |
6243 | ||
cdd6c482 | 6244 | void perf_event_wakeup(struct perf_event *event) |
925d519a | 6245 | { |
10c6db11 | 6246 | ring_buffer_wakeup(event); |
4c9e2542 | 6247 | |
cdd6c482 | 6248 | if (event->pending_kill) { |
fed66e2c | 6249 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
cdd6c482 | 6250 | event->pending_kill = 0; |
4c9e2542 | 6251 | } |
925d519a PZ |
6252 | } |
6253 | ||
1d54ad94 PZ |
6254 | static void perf_pending_event_disable(struct perf_event *event) |
6255 | { | |
6256 | int cpu = READ_ONCE(event->pending_disable); | |
6257 | ||
6258 | if (cpu < 0) | |
6259 | return; | |
6260 | ||
6261 | if (cpu == smp_processor_id()) { | |
6262 | WRITE_ONCE(event->pending_disable, -1); | |
6263 | perf_event_disable_local(event); | |
6264 | return; | |
6265 | } | |
6266 | ||
6267 | /* | |
6268 | * CPU-A CPU-B | |
6269 | * | |
6270 | * perf_event_disable_inatomic() | |
6271 | * @pending_disable = CPU-A; | |
6272 | * irq_work_queue(); | |
6273 | * | |
6274 | * sched-out | |
6275 | * @pending_disable = -1; | |
6276 | * | |
6277 | * sched-in | |
6278 | * perf_event_disable_inatomic() | |
6279 | * @pending_disable = CPU-B; | |
6280 | * irq_work_queue(); // FAILS | |
6281 | * | |
6282 | * irq_work_run() | |
6283 | * perf_pending_event() | |
6284 | * | |
6285 | * But the event runs on CPU-B and wants disabling there. | |
6286 | */ | |
6287 | irq_work_queue_on(&event->pending, cpu); | |
6288 | } | |
6289 | ||
e360adbe | 6290 | static void perf_pending_event(struct irq_work *entry) |
79f14641 | 6291 | { |
1d54ad94 | 6292 | struct perf_event *event = container_of(entry, struct perf_event, pending); |
d525211f PZ |
6293 | int rctx; |
6294 | ||
6295 | rctx = perf_swevent_get_recursion_context(); | |
6296 | /* | |
6297 | * If we 'fail' here, that's OK, it means recursion is already disabled | |
6298 | * and we won't recurse 'further'. | |
6299 | */ | |
79f14641 | 6300 | |
1d54ad94 | 6301 | perf_pending_event_disable(event); |
79f14641 | 6302 | |
cdd6c482 IM |
6303 | if (event->pending_wakeup) { |
6304 | event->pending_wakeup = 0; | |
6305 | perf_event_wakeup(event); | |
79f14641 | 6306 | } |
d525211f PZ |
6307 | |
6308 | if (rctx >= 0) | |
6309 | perf_swevent_put_recursion_context(rctx); | |
79f14641 PZ |
6310 | } |
6311 | ||
39447b38 ZY |
6312 | /* |
6313 | * We assume there is only KVM supporting the callbacks. | |
6314 | * Later on, we might change it to a list if there is | |
6315 | * another virtualization implementation supporting the callbacks. | |
6316 | */ | |
6317 | struct perf_guest_info_callbacks *perf_guest_cbs; | |
6318 | ||
6319 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
6320 | { | |
6321 | perf_guest_cbs = cbs; | |
6322 | return 0; | |
6323 | } | |
6324 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); | |
6325 | ||
6326 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) | |
6327 | { | |
6328 | perf_guest_cbs = NULL; | |
6329 | return 0; | |
6330 | } | |
6331 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); | |
6332 | ||
4018994f JO |
6333 | static void |
6334 | perf_output_sample_regs(struct perf_output_handle *handle, | |
6335 | struct pt_regs *regs, u64 mask) | |
6336 | { | |
6337 | int bit; | |
29dd3288 | 6338 | DECLARE_BITMAP(_mask, 64); |
4018994f | 6339 | |
29dd3288 MS |
6340 | bitmap_from_u64(_mask, mask); |
6341 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { | |
4018994f JO |
6342 | u64 val; |
6343 | ||
6344 | val = perf_reg_value(regs, bit); | |
6345 | perf_output_put(handle, val); | |
6346 | } | |
6347 | } | |
6348 | ||
60e2364e | 6349 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
88a7c26a AL |
6350 | struct pt_regs *regs, |
6351 | struct pt_regs *regs_user_copy) | |
4018994f | 6352 | { |
88a7c26a AL |
6353 | if (user_mode(regs)) { |
6354 | regs_user->abi = perf_reg_abi(current); | |
2565711f | 6355 | regs_user->regs = regs; |
085ebfe9 | 6356 | } else if (!(current->flags & PF_KTHREAD)) { |
88a7c26a | 6357 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
2565711f PZ |
6358 | } else { |
6359 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; | |
6360 | regs_user->regs = NULL; | |
4018994f JO |
6361 | } |
6362 | } | |
6363 | ||
60e2364e SE |
6364 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
6365 | struct pt_regs *regs) | |
6366 | { | |
6367 | regs_intr->regs = regs; | |
6368 | regs_intr->abi = perf_reg_abi(current); | |
6369 | } | |
6370 | ||
6371 | ||
c5ebcedb JO |
6372 | /* |
6373 | * Get remaining task size from user stack pointer. | |
6374 | * | |
6375 | * It'd be better to take stack vma map and limit this more | |
9f014e3a | 6376 | * precisely, but there's no way to get it safely under interrupt, |
c5ebcedb JO |
6377 | * so using TASK_SIZE as limit. |
6378 | */ | |
6379 | static u64 perf_ustack_task_size(struct pt_regs *regs) | |
6380 | { | |
6381 | unsigned long addr = perf_user_stack_pointer(regs); | |
6382 | ||
6383 | if (!addr || addr >= TASK_SIZE) | |
6384 | return 0; | |
6385 | ||
6386 | return TASK_SIZE - addr; | |
6387 | } | |
6388 | ||
6389 | static u16 | |
6390 | perf_sample_ustack_size(u16 stack_size, u16 header_size, | |
6391 | struct pt_regs *regs) | |
6392 | { | |
6393 | u64 task_size; | |
6394 | ||
6395 | /* No regs, no stack pointer, no dump. */ | |
6396 | if (!regs) | |
6397 | return 0; | |
6398 | ||
6399 | /* | |
6400 | * Check if we fit in with the requested stack size into the: | |
6401 | * - TASK_SIZE | |
6402 | * If we don't, we limit the size to the TASK_SIZE. | |
6403 | * | |
6404 | * - remaining sample size | |
6405 | * If we don't, we customize the stack size to | |
6406 | * fit in to the remaining sample size. | |
6407 | */ | |
6408 | ||
6409 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); | |
6410 | stack_size = min(stack_size, (u16) task_size); | |
6411 | ||
6412 | /* Current header size plus static size and dynamic size. */ | |
6413 | header_size += 2 * sizeof(u64); | |
6414 | ||
6415 | /* Do we fit in with the current stack dump size? */ | |
6416 | if ((u16) (header_size + stack_size) < header_size) { | |
6417 | /* | |
6418 | * If we overflow the maximum size for the sample, | |
6419 | * we customize the stack dump size to fit in. | |
6420 | */ | |
6421 | stack_size = USHRT_MAX - header_size - sizeof(u64); | |
6422 | stack_size = round_up(stack_size, sizeof(u64)); | |
6423 | } | |
6424 | ||
6425 | return stack_size; | |
6426 | } | |
6427 | ||
6428 | static void | |
6429 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, | |
6430 | struct pt_regs *regs) | |
6431 | { | |
6432 | /* Case of a kernel thread, nothing to dump */ | |
6433 | if (!regs) { | |
6434 | u64 size = 0; | |
6435 | perf_output_put(handle, size); | |
6436 | } else { | |
6437 | unsigned long sp; | |
6438 | unsigned int rem; | |
6439 | u64 dyn_size; | |
02e18447 | 6440 | mm_segment_t fs; |
c5ebcedb JO |
6441 | |
6442 | /* | |
6443 | * We dump: | |
6444 | * static size | |
6445 | * - the size requested by user or the best one we can fit | |
6446 | * in to the sample max size | |
6447 | * data | |
6448 | * - user stack dump data | |
6449 | * dynamic size | |
6450 | * - the actual dumped size | |
6451 | */ | |
6452 | ||
6453 | /* Static size. */ | |
6454 | perf_output_put(handle, dump_size); | |
6455 | ||
6456 | /* Data. */ | |
6457 | sp = perf_user_stack_pointer(regs); | |
02e18447 YC |
6458 | fs = get_fs(); |
6459 | set_fs(USER_DS); | |
c5ebcedb | 6460 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
02e18447 | 6461 | set_fs(fs); |
c5ebcedb JO |
6462 | dyn_size = dump_size - rem; |
6463 | ||
6464 | perf_output_skip(handle, rem); | |
6465 | ||
6466 | /* Dynamic size. */ | |
6467 | perf_output_put(handle, dyn_size); | |
6468 | } | |
6469 | } | |
6470 | ||
a4faf00d AS |
6471 | static unsigned long perf_prepare_sample_aux(struct perf_event *event, |
6472 | struct perf_sample_data *data, | |
6473 | size_t size) | |
6474 | { | |
6475 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 6476 | struct perf_buffer *rb; |
a4faf00d AS |
6477 | |
6478 | data->aux_size = 0; | |
6479 | ||
6480 | if (!sampler) | |
6481 | goto out; | |
6482 | ||
6483 | if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE)) | |
6484 | goto out; | |
6485 | ||
6486 | if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id())) | |
6487 | goto out; | |
6488 | ||
6489 | rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler); | |
6490 | if (!rb) | |
6491 | goto out; | |
6492 | ||
6493 | /* | |
6494 | * If this is an NMI hit inside sampling code, don't take | |
6495 | * the sample. See also perf_aux_sample_output(). | |
6496 | */ | |
6497 | if (READ_ONCE(rb->aux_in_sampling)) { | |
6498 | data->aux_size = 0; | |
6499 | } else { | |
6500 | size = min_t(size_t, size, perf_aux_size(rb)); | |
6501 | data->aux_size = ALIGN(size, sizeof(u64)); | |
6502 | } | |
6503 | ring_buffer_put(rb); | |
6504 | ||
6505 | out: | |
6506 | return data->aux_size; | |
6507 | } | |
6508 | ||
56de4e8f | 6509 | long perf_pmu_snapshot_aux(struct perf_buffer *rb, |
a4faf00d AS |
6510 | struct perf_event *event, |
6511 | struct perf_output_handle *handle, | |
6512 | unsigned long size) | |
6513 | { | |
6514 | unsigned long flags; | |
6515 | long ret; | |
6516 | ||
6517 | /* | |
6518 | * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler | |
6519 | * paths. If we start calling them in NMI context, they may race with | |
6520 | * the IRQ ones, that is, for example, re-starting an event that's just | |
6521 | * been stopped, which is why we're using a separate callback that | |
6522 | * doesn't change the event state. | |
6523 | * | |
6524 | * IRQs need to be disabled to prevent IPIs from racing with us. | |
6525 | */ | |
6526 | local_irq_save(flags); | |
6527 | /* | |
6528 | * Guard against NMI hits inside the critical section; | |
6529 | * see also perf_prepare_sample_aux(). | |
6530 | */ | |
6531 | WRITE_ONCE(rb->aux_in_sampling, 1); | |
6532 | barrier(); | |
6533 | ||
6534 | ret = event->pmu->snapshot_aux(event, handle, size); | |
6535 | ||
6536 | barrier(); | |
6537 | WRITE_ONCE(rb->aux_in_sampling, 0); | |
6538 | local_irq_restore(flags); | |
6539 | ||
6540 | return ret; | |
6541 | } | |
6542 | ||
6543 | static void perf_aux_sample_output(struct perf_event *event, | |
6544 | struct perf_output_handle *handle, | |
6545 | struct perf_sample_data *data) | |
6546 | { | |
6547 | struct perf_event *sampler = event->aux_event; | |
56de4e8f | 6548 | struct perf_buffer *rb; |
a4faf00d | 6549 | unsigned long pad; |
a4faf00d AS |
6550 | long size; |
6551 | ||
6552 | if (WARN_ON_ONCE(!sampler || !data->aux_size)) | |
6553 | return; | |
6554 | ||
6555 | rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler); | |
6556 | if (!rb) | |
6557 | return; | |
6558 | ||
6559 | size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size); | |
6560 | ||
6561 | /* | |
6562 | * An error here means that perf_output_copy() failed (returned a | |
6563 | * non-zero surplus that it didn't copy), which in its current | |
6564 | * enlightened implementation is not possible. If that changes, we'd | |
6565 | * like to know. | |
6566 | */ | |
6567 | if (WARN_ON_ONCE(size < 0)) | |
6568 | goto out_put; | |
6569 | ||
6570 | /* | |
6571 | * The pad comes from ALIGN()ing data->aux_size up to u64 in | |
6572 | * perf_prepare_sample_aux(), so should not be more than that. | |
6573 | */ | |
6574 | pad = data->aux_size - size; | |
6575 | if (WARN_ON_ONCE(pad >= sizeof(u64))) | |
6576 | pad = 8; | |
6577 | ||
6578 | if (pad) { | |
6579 | u64 zero = 0; | |
6580 | perf_output_copy(handle, &zero, pad); | |
6581 | } | |
6582 | ||
6583 | out_put: | |
6584 | ring_buffer_put(rb); | |
6585 | } | |
6586 | ||
c980d109 ACM |
6587 | static void __perf_event_header__init_id(struct perf_event_header *header, |
6588 | struct perf_sample_data *data, | |
6589 | struct perf_event *event) | |
6844c09d ACM |
6590 | { |
6591 | u64 sample_type = event->attr.sample_type; | |
6592 | ||
6593 | data->type = sample_type; | |
6594 | header->size += event->id_header_size; | |
6595 | ||
6596 | if (sample_type & PERF_SAMPLE_TID) { | |
6597 | /* namespace issues */ | |
6598 | data->tid_entry.pid = perf_event_pid(event, current); | |
6599 | data->tid_entry.tid = perf_event_tid(event, current); | |
6600 | } | |
6601 | ||
6602 | if (sample_type & PERF_SAMPLE_TIME) | |
34f43927 | 6603 | data->time = perf_event_clock(event); |
6844c09d | 6604 | |
ff3d527c | 6605 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
6844c09d ACM |
6606 | data->id = primary_event_id(event); |
6607 | ||
6608 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
6609 | data->stream_id = event->id; | |
6610 | ||
6611 | if (sample_type & PERF_SAMPLE_CPU) { | |
6612 | data->cpu_entry.cpu = raw_smp_processor_id(); | |
6613 | data->cpu_entry.reserved = 0; | |
6614 | } | |
6615 | } | |
6616 | ||
76369139 FW |
6617 | void perf_event_header__init_id(struct perf_event_header *header, |
6618 | struct perf_sample_data *data, | |
6619 | struct perf_event *event) | |
c980d109 ACM |
6620 | { |
6621 | if (event->attr.sample_id_all) | |
6622 | __perf_event_header__init_id(header, data, event); | |
6623 | } | |
6624 | ||
6625 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, | |
6626 | struct perf_sample_data *data) | |
6627 | { | |
6628 | u64 sample_type = data->type; | |
6629 | ||
6630 | if (sample_type & PERF_SAMPLE_TID) | |
6631 | perf_output_put(handle, data->tid_entry); | |
6632 | ||
6633 | if (sample_type & PERF_SAMPLE_TIME) | |
6634 | perf_output_put(handle, data->time); | |
6635 | ||
6636 | if (sample_type & PERF_SAMPLE_ID) | |
6637 | perf_output_put(handle, data->id); | |
6638 | ||
6639 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
6640 | perf_output_put(handle, data->stream_id); | |
6641 | ||
6642 | if (sample_type & PERF_SAMPLE_CPU) | |
6643 | perf_output_put(handle, data->cpu_entry); | |
ff3d527c AH |
6644 | |
6645 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
6646 | perf_output_put(handle, data->id); | |
c980d109 ACM |
6647 | } |
6648 | ||
76369139 FW |
6649 | void perf_event__output_id_sample(struct perf_event *event, |
6650 | struct perf_output_handle *handle, | |
6651 | struct perf_sample_data *sample) | |
c980d109 ACM |
6652 | { |
6653 | if (event->attr.sample_id_all) | |
6654 | __perf_event__output_id_sample(handle, sample); | |
6655 | } | |
6656 | ||
3dab77fb | 6657 | static void perf_output_read_one(struct perf_output_handle *handle, |
eed01528 SE |
6658 | struct perf_event *event, |
6659 | u64 enabled, u64 running) | |
3dab77fb | 6660 | { |
cdd6c482 | 6661 | u64 read_format = event->attr.read_format; |
3dab77fb PZ |
6662 | u64 values[4]; |
6663 | int n = 0; | |
6664 | ||
b5e58793 | 6665 | values[n++] = perf_event_count(event); |
3dab77fb | 6666 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
eed01528 | 6667 | values[n++] = enabled + |
cdd6c482 | 6668 | atomic64_read(&event->child_total_time_enabled); |
3dab77fb PZ |
6669 | } |
6670 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
eed01528 | 6671 | values[n++] = running + |
cdd6c482 | 6672 | atomic64_read(&event->child_total_time_running); |
3dab77fb PZ |
6673 | } |
6674 | if (read_format & PERF_FORMAT_ID) | |
cdd6c482 | 6675 | values[n++] = primary_event_id(event); |
3dab77fb | 6676 | |
76369139 | 6677 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
6678 | } |
6679 | ||
3dab77fb | 6680 | static void perf_output_read_group(struct perf_output_handle *handle, |
eed01528 SE |
6681 | struct perf_event *event, |
6682 | u64 enabled, u64 running) | |
3dab77fb | 6683 | { |
cdd6c482 IM |
6684 | struct perf_event *leader = event->group_leader, *sub; |
6685 | u64 read_format = event->attr.read_format; | |
3dab77fb PZ |
6686 | u64 values[5]; |
6687 | int n = 0; | |
6688 | ||
6689 | values[n++] = 1 + leader->nr_siblings; | |
6690 | ||
6691 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
eed01528 | 6692 | values[n++] = enabled; |
3dab77fb PZ |
6693 | |
6694 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
eed01528 | 6695 | values[n++] = running; |
3dab77fb | 6696 | |
9e5b127d PZ |
6697 | if ((leader != event) && |
6698 | (leader->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
6699 | leader->pmu->read(leader); |
6700 | ||
b5e58793 | 6701 | values[n++] = perf_event_count(leader); |
3dab77fb | 6702 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 6703 | values[n++] = primary_event_id(leader); |
3dab77fb | 6704 | |
76369139 | 6705 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb | 6706 | |
edb39592 | 6707 | for_each_sibling_event(sub, leader) { |
3dab77fb PZ |
6708 | n = 0; |
6709 | ||
6f5ab001 JO |
6710 | if ((sub != event) && |
6711 | (sub->state == PERF_EVENT_STATE_ACTIVE)) | |
3dab77fb PZ |
6712 | sub->pmu->read(sub); |
6713 | ||
b5e58793 | 6714 | values[n++] = perf_event_count(sub); |
3dab77fb | 6715 | if (read_format & PERF_FORMAT_ID) |
cdd6c482 | 6716 | values[n++] = primary_event_id(sub); |
3dab77fb | 6717 | |
76369139 | 6718 | __output_copy(handle, values, n * sizeof(u64)); |
3dab77fb PZ |
6719 | } |
6720 | } | |
6721 | ||
eed01528 SE |
6722 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
6723 | PERF_FORMAT_TOTAL_TIME_RUNNING) | |
6724 | ||
ba5213ae PZ |
6725 | /* |
6726 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. | |
6727 | * | |
6728 | * The problem is that its both hard and excessively expensive to iterate the | |
6729 | * child list, not to mention that its impossible to IPI the children running | |
6730 | * on another CPU, from interrupt/NMI context. | |
6731 | */ | |
3dab77fb | 6732 | static void perf_output_read(struct perf_output_handle *handle, |
cdd6c482 | 6733 | struct perf_event *event) |
3dab77fb | 6734 | { |
e3f3541c | 6735 | u64 enabled = 0, running = 0, now; |
eed01528 SE |
6736 | u64 read_format = event->attr.read_format; |
6737 | ||
6738 | /* | |
6739 | * compute total_time_enabled, total_time_running | |
6740 | * based on snapshot values taken when the event | |
6741 | * was last scheduled in. | |
6742 | * | |
6743 | * we cannot simply called update_context_time() | |
6744 | * because of locking issue as we are called in | |
6745 | * NMI context | |
6746 | */ | |
c4794295 | 6747 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
e3f3541c | 6748 | calc_timer_values(event, &now, &enabled, &running); |
eed01528 | 6749 | |
cdd6c482 | 6750 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
eed01528 | 6751 | perf_output_read_group(handle, event, enabled, running); |
3dab77fb | 6752 | else |
eed01528 | 6753 | perf_output_read_one(handle, event, enabled, running); |
3dab77fb PZ |
6754 | } |
6755 | ||
bbfd5e4f KL |
6756 | static inline bool perf_sample_save_hw_index(struct perf_event *event) |
6757 | { | |
6758 | return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; | |
6759 | } | |
6760 | ||
5622f295 MM |
6761 | void perf_output_sample(struct perf_output_handle *handle, |
6762 | struct perf_event_header *header, | |
6763 | struct perf_sample_data *data, | |
cdd6c482 | 6764 | struct perf_event *event) |
5622f295 MM |
6765 | { |
6766 | u64 sample_type = data->type; | |
6767 | ||
6768 | perf_output_put(handle, *header); | |
6769 | ||
ff3d527c AH |
6770 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
6771 | perf_output_put(handle, data->id); | |
6772 | ||
5622f295 MM |
6773 | if (sample_type & PERF_SAMPLE_IP) |
6774 | perf_output_put(handle, data->ip); | |
6775 | ||
6776 | if (sample_type & PERF_SAMPLE_TID) | |
6777 | perf_output_put(handle, data->tid_entry); | |
6778 | ||
6779 | if (sample_type & PERF_SAMPLE_TIME) | |
6780 | perf_output_put(handle, data->time); | |
6781 | ||
6782 | if (sample_type & PERF_SAMPLE_ADDR) | |
6783 | perf_output_put(handle, data->addr); | |
6784 | ||
6785 | if (sample_type & PERF_SAMPLE_ID) | |
6786 | perf_output_put(handle, data->id); | |
6787 | ||
6788 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
6789 | perf_output_put(handle, data->stream_id); | |
6790 | ||
6791 | if (sample_type & PERF_SAMPLE_CPU) | |
6792 | perf_output_put(handle, data->cpu_entry); | |
6793 | ||
6794 | if (sample_type & PERF_SAMPLE_PERIOD) | |
6795 | perf_output_put(handle, data->period); | |
6796 | ||
6797 | if (sample_type & PERF_SAMPLE_READ) | |
cdd6c482 | 6798 | perf_output_read(handle, event); |
5622f295 MM |
6799 | |
6800 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { | |
99e818cc | 6801 | int size = 1; |
5622f295 | 6802 | |
99e818cc JO |
6803 | size += data->callchain->nr; |
6804 | size *= sizeof(u64); | |
6805 | __output_copy(handle, data->callchain, size); | |
5622f295 MM |
6806 | } |
6807 | ||
6808 | if (sample_type & PERF_SAMPLE_RAW) { | |
7e3f977e DB |
6809 | struct perf_raw_record *raw = data->raw; |
6810 | ||
6811 | if (raw) { | |
6812 | struct perf_raw_frag *frag = &raw->frag; | |
6813 | ||
6814 | perf_output_put(handle, raw->size); | |
6815 | do { | |
6816 | if (frag->copy) { | |
6817 | __output_custom(handle, frag->copy, | |
6818 | frag->data, frag->size); | |
6819 | } else { | |
6820 | __output_copy(handle, frag->data, | |
6821 | frag->size); | |
6822 | } | |
6823 | if (perf_raw_frag_last(frag)) | |
6824 | break; | |
6825 | frag = frag->next; | |
6826 | } while (1); | |
6827 | if (frag->pad) | |
6828 | __output_skip(handle, NULL, frag->pad); | |
5622f295 MM |
6829 | } else { |
6830 | struct { | |
6831 | u32 size; | |
6832 | u32 data; | |
6833 | } raw = { | |
6834 | .size = sizeof(u32), | |
6835 | .data = 0, | |
6836 | }; | |
6837 | perf_output_put(handle, raw); | |
6838 | } | |
6839 | } | |
a7ac67ea | 6840 | |
bce38cd5 SE |
6841 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
6842 | if (data->br_stack) { | |
6843 | size_t size; | |
6844 | ||
6845 | size = data->br_stack->nr | |
6846 | * sizeof(struct perf_branch_entry); | |
6847 | ||
6848 | perf_output_put(handle, data->br_stack->nr); | |
bbfd5e4f KL |
6849 | if (perf_sample_save_hw_index(event)) |
6850 | perf_output_put(handle, data->br_stack->hw_idx); | |
bce38cd5 SE |
6851 | perf_output_copy(handle, data->br_stack->entries, size); |
6852 | } else { | |
6853 | /* | |
6854 | * we always store at least the value of nr | |
6855 | */ | |
6856 | u64 nr = 0; | |
6857 | perf_output_put(handle, nr); | |
6858 | } | |
6859 | } | |
4018994f JO |
6860 | |
6861 | if (sample_type & PERF_SAMPLE_REGS_USER) { | |
6862 | u64 abi = data->regs_user.abi; | |
6863 | ||
6864 | /* | |
6865 | * If there are no regs to dump, notice it through | |
6866 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
6867 | */ | |
6868 | perf_output_put(handle, abi); | |
6869 | ||
6870 | if (abi) { | |
6871 | u64 mask = event->attr.sample_regs_user; | |
6872 | perf_output_sample_regs(handle, | |
6873 | data->regs_user.regs, | |
6874 | mask); | |
6875 | } | |
6876 | } | |
c5ebcedb | 6877 | |
a5cdd40c | 6878 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
c5ebcedb JO |
6879 | perf_output_sample_ustack(handle, |
6880 | data->stack_user_size, | |
6881 | data->regs_user.regs); | |
a5cdd40c | 6882 | } |
c3feedf2 AK |
6883 | |
6884 | if (sample_type & PERF_SAMPLE_WEIGHT) | |
6885 | perf_output_put(handle, data->weight); | |
d6be9ad6 SE |
6886 | |
6887 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
6888 | perf_output_put(handle, data->data_src.val); | |
a5cdd40c | 6889 | |
fdfbbd07 AK |
6890 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
6891 | perf_output_put(handle, data->txn); | |
6892 | ||
60e2364e SE |
6893 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
6894 | u64 abi = data->regs_intr.abi; | |
6895 | /* | |
6896 | * If there are no regs to dump, notice it through | |
6897 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). | |
6898 | */ | |
6899 | perf_output_put(handle, abi); | |
6900 | ||
6901 | if (abi) { | |
6902 | u64 mask = event->attr.sample_regs_intr; | |
6903 | ||
6904 | perf_output_sample_regs(handle, | |
6905 | data->regs_intr.regs, | |
6906 | mask); | |
6907 | } | |
6908 | } | |
6909 | ||
fc7ce9c7 KL |
6910 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) |
6911 | perf_output_put(handle, data->phys_addr); | |
6912 | ||
6546b19f NK |
6913 | if (sample_type & PERF_SAMPLE_CGROUP) |
6914 | perf_output_put(handle, data->cgroup); | |
6915 | ||
a4faf00d AS |
6916 | if (sample_type & PERF_SAMPLE_AUX) { |
6917 | perf_output_put(handle, data->aux_size); | |
6918 | ||
6919 | if (data->aux_size) | |
6920 | perf_aux_sample_output(event, handle, data); | |
6921 | } | |
6922 | ||
a5cdd40c PZ |
6923 | if (!event->attr.watermark) { |
6924 | int wakeup_events = event->attr.wakeup_events; | |
6925 | ||
6926 | if (wakeup_events) { | |
56de4e8f | 6927 | struct perf_buffer *rb = handle->rb; |
a5cdd40c PZ |
6928 | int events = local_inc_return(&rb->events); |
6929 | ||
6930 | if (events >= wakeup_events) { | |
6931 | local_sub(wakeup_events, &rb->events); | |
6932 | local_inc(&rb->wakeup); | |
6933 | } | |
6934 | } | |
6935 | } | |
5622f295 MM |
6936 | } |
6937 | ||
fc7ce9c7 KL |
6938 | static u64 perf_virt_to_phys(u64 virt) |
6939 | { | |
6940 | u64 phys_addr = 0; | |
6941 | struct page *p = NULL; | |
6942 | ||
6943 | if (!virt) | |
6944 | return 0; | |
6945 | ||
6946 | if (virt >= TASK_SIZE) { | |
6947 | /* If it's vmalloc()d memory, leave phys_addr as 0 */ | |
6948 | if (virt_addr_valid((void *)(uintptr_t)virt) && | |
6949 | !(virt >= VMALLOC_START && virt < VMALLOC_END)) | |
6950 | phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); | |
6951 | } else { | |
6952 | /* | |
6953 | * Walking the pages tables for user address. | |
6954 | * Interrupts are disabled, so it prevents any tear down | |
6955 | * of the page tables. | |
dadbb612 | 6956 | * Try IRQ-safe get_user_page_fast_only first. |
fc7ce9c7 KL |
6957 | * If failed, leave phys_addr as 0. |
6958 | */ | |
d3296fb3 JO |
6959 | if (current->mm != NULL) { |
6960 | pagefault_disable(); | |
dadbb612 | 6961 | if (get_user_page_fast_only(virt, 0, &p)) |
d3296fb3 JO |
6962 | phys_addr = page_to_phys(p) + virt % PAGE_SIZE; |
6963 | pagefault_enable(); | |
6964 | } | |
fc7ce9c7 KL |
6965 | |
6966 | if (p) | |
6967 | put_page(p); | |
6968 | } | |
6969 | ||
6970 | return phys_addr; | |
6971 | } | |
6972 | ||
99e818cc JO |
6973 | static struct perf_callchain_entry __empty_callchain = { .nr = 0, }; |
6974 | ||
6cbc304f | 6975 | struct perf_callchain_entry * |
8cf7e0e2 JO |
6976 | perf_callchain(struct perf_event *event, struct pt_regs *regs) |
6977 | { | |
6978 | bool kernel = !event->attr.exclude_callchain_kernel; | |
6979 | bool user = !event->attr.exclude_callchain_user; | |
6980 | /* Disallow cross-task user callchains. */ | |
6981 | bool crosstask = event->ctx->task && event->ctx->task != current; | |
6982 | const u32 max_stack = event->attr.sample_max_stack; | |
99e818cc | 6983 | struct perf_callchain_entry *callchain; |
8cf7e0e2 JO |
6984 | |
6985 | if (!kernel && !user) | |
99e818cc | 6986 | return &__empty_callchain; |
8cf7e0e2 | 6987 | |
99e818cc JO |
6988 | callchain = get_perf_callchain(regs, 0, kernel, user, |
6989 | max_stack, crosstask, true); | |
6990 | return callchain ?: &__empty_callchain; | |
8cf7e0e2 JO |
6991 | } |
6992 | ||
5622f295 MM |
6993 | void perf_prepare_sample(struct perf_event_header *header, |
6994 | struct perf_sample_data *data, | |
cdd6c482 | 6995 | struct perf_event *event, |
5622f295 | 6996 | struct pt_regs *regs) |
7b732a75 | 6997 | { |
cdd6c482 | 6998 | u64 sample_type = event->attr.sample_type; |
7b732a75 | 6999 | |
cdd6c482 | 7000 | header->type = PERF_RECORD_SAMPLE; |
c320c7b7 | 7001 | header->size = sizeof(*header) + event->header_size; |
5622f295 MM |
7002 | |
7003 | header->misc = 0; | |
7004 | header->misc |= perf_misc_flags(regs); | |
6fab0192 | 7005 | |
c980d109 | 7006 | __perf_event_header__init_id(header, data, event); |
6844c09d | 7007 | |
c320c7b7 | 7008 | if (sample_type & PERF_SAMPLE_IP) |
5622f295 MM |
7009 | data->ip = perf_instruction_pointer(regs); |
7010 | ||
b23f3325 | 7011 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
5622f295 | 7012 | int size = 1; |
394ee076 | 7013 | |
6cbc304f PZ |
7014 | if (!(sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY)) |
7015 | data->callchain = perf_callchain(event, regs); | |
7016 | ||
99e818cc | 7017 | size += data->callchain->nr; |
5622f295 MM |
7018 | |
7019 | header->size += size * sizeof(u64); | |
394ee076 PZ |
7020 | } |
7021 | ||
3a43ce68 | 7022 | if (sample_type & PERF_SAMPLE_RAW) { |
7e3f977e DB |
7023 | struct perf_raw_record *raw = data->raw; |
7024 | int size; | |
7025 | ||
7026 | if (raw) { | |
7027 | struct perf_raw_frag *frag = &raw->frag; | |
7028 | u32 sum = 0; | |
7029 | ||
7030 | do { | |
7031 | sum += frag->size; | |
7032 | if (perf_raw_frag_last(frag)) | |
7033 | break; | |
7034 | frag = frag->next; | |
7035 | } while (1); | |
7036 | ||
7037 | size = round_up(sum + sizeof(u32), sizeof(u64)); | |
7038 | raw->size = size - sizeof(u32); | |
7039 | frag->pad = raw->size - sum; | |
7040 | } else { | |
7041 | size = sizeof(u64); | |
7042 | } | |
a044560c | 7043 | |
7e3f977e | 7044 | header->size += size; |
7f453c24 | 7045 | } |
bce38cd5 SE |
7046 | |
7047 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { | |
7048 | int size = sizeof(u64); /* nr */ | |
7049 | if (data->br_stack) { | |
bbfd5e4f KL |
7050 | if (perf_sample_save_hw_index(event)) |
7051 | size += sizeof(u64); | |
7052 | ||
bce38cd5 SE |
7053 | size += data->br_stack->nr |
7054 | * sizeof(struct perf_branch_entry); | |
7055 | } | |
7056 | header->size += size; | |
7057 | } | |
4018994f | 7058 | |
2565711f | 7059 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
88a7c26a AL |
7060 | perf_sample_regs_user(&data->regs_user, regs, |
7061 | &data->regs_user_copy); | |
2565711f | 7062 | |
4018994f JO |
7063 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
7064 | /* regs dump ABI info */ | |
7065 | int size = sizeof(u64); | |
7066 | ||
4018994f JO |
7067 | if (data->regs_user.regs) { |
7068 | u64 mask = event->attr.sample_regs_user; | |
7069 | size += hweight64(mask) * sizeof(u64); | |
7070 | } | |
7071 | ||
7072 | header->size += size; | |
7073 | } | |
c5ebcedb JO |
7074 | |
7075 | if (sample_type & PERF_SAMPLE_STACK_USER) { | |
7076 | /* | |
9f014e3a | 7077 | * Either we need PERF_SAMPLE_STACK_USER bit to be always |
c5ebcedb JO |
7078 | * processed as the last one or have additional check added |
7079 | * in case new sample type is added, because we could eat | |
7080 | * up the rest of the sample size. | |
7081 | */ | |
c5ebcedb JO |
7082 | u16 stack_size = event->attr.sample_stack_user; |
7083 | u16 size = sizeof(u64); | |
7084 | ||
c5ebcedb | 7085 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
2565711f | 7086 | data->regs_user.regs); |
c5ebcedb JO |
7087 | |
7088 | /* | |
7089 | * If there is something to dump, add space for the dump | |
7090 | * itself and for the field that tells the dynamic size, | |
7091 | * which is how many have been actually dumped. | |
7092 | */ | |
7093 | if (stack_size) | |
7094 | size += sizeof(u64) + stack_size; | |
7095 | ||
7096 | data->stack_user_size = stack_size; | |
7097 | header->size += size; | |
7098 | } | |
60e2364e SE |
7099 | |
7100 | if (sample_type & PERF_SAMPLE_REGS_INTR) { | |
7101 | /* regs dump ABI info */ | |
7102 | int size = sizeof(u64); | |
7103 | ||
7104 | perf_sample_regs_intr(&data->regs_intr, regs); | |
7105 | ||
7106 | if (data->regs_intr.regs) { | |
7107 | u64 mask = event->attr.sample_regs_intr; | |
7108 | ||
7109 | size += hweight64(mask) * sizeof(u64); | |
7110 | } | |
7111 | ||
7112 | header->size += size; | |
7113 | } | |
fc7ce9c7 KL |
7114 | |
7115 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) | |
7116 | data->phys_addr = perf_virt_to_phys(data->addr); | |
a4faf00d | 7117 | |
6546b19f NK |
7118 | #ifdef CONFIG_CGROUP_PERF |
7119 | if (sample_type & PERF_SAMPLE_CGROUP) { | |
7120 | struct cgroup *cgrp; | |
7121 | ||
7122 | /* protected by RCU */ | |
7123 | cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup; | |
7124 | data->cgroup = cgroup_id(cgrp); | |
7125 | } | |
7126 | #endif | |
7127 | ||
a4faf00d AS |
7128 | if (sample_type & PERF_SAMPLE_AUX) { |
7129 | u64 size; | |
7130 | ||
7131 | header->size += sizeof(u64); /* size */ | |
7132 | ||
7133 | /* | |
7134 | * Given the 16bit nature of header::size, an AUX sample can | |
7135 | * easily overflow it, what with all the preceding sample bits. | |
7136 | * Make sure this doesn't happen by using up to U16_MAX bytes | |
7137 | * per sample in total (rounded down to 8 byte boundary). | |
7138 | */ | |
7139 | size = min_t(size_t, U16_MAX - header->size, | |
7140 | event->attr.aux_sample_size); | |
7141 | size = rounddown(size, 8); | |
7142 | size = perf_prepare_sample_aux(event, data, size); | |
7143 | ||
7144 | WARN_ON_ONCE(size + header->size > U16_MAX); | |
7145 | header->size += size; | |
7146 | } | |
7147 | /* | |
7148 | * If you're adding more sample types here, you likely need to do | |
7149 | * something about the overflowing header::size, like repurpose the | |
7150 | * lowest 3 bits of size, which should be always zero at the moment. | |
7151 | * This raises a more important question, do we really need 512k sized | |
7152 | * samples and why, so good argumentation is in order for whatever you | |
7153 | * do here next. | |
7154 | */ | |
7155 | WARN_ON_ONCE(header->size & 7); | |
5622f295 | 7156 | } |
7f453c24 | 7157 | |
56201969 | 7158 | static __always_inline int |
9ecda41a WN |
7159 | __perf_event_output(struct perf_event *event, |
7160 | struct perf_sample_data *data, | |
7161 | struct pt_regs *regs, | |
7162 | int (*output_begin)(struct perf_output_handle *, | |
7163 | struct perf_event *, | |
7164 | unsigned int)) | |
5622f295 MM |
7165 | { |
7166 | struct perf_output_handle handle; | |
7167 | struct perf_event_header header; | |
56201969 | 7168 | int err; |
689802b2 | 7169 | |
927c7a9e FW |
7170 | /* protect the callchain buffers */ |
7171 | rcu_read_lock(); | |
7172 | ||
cdd6c482 | 7173 | perf_prepare_sample(&header, data, event, regs); |
5c148194 | 7174 | |
56201969 ACM |
7175 | err = output_begin(&handle, event, header.size); |
7176 | if (err) | |
927c7a9e | 7177 | goto exit; |
0322cd6e | 7178 | |
cdd6c482 | 7179 | perf_output_sample(&handle, &header, data, event); |
f413cdb8 | 7180 | |
8a057d84 | 7181 | perf_output_end(&handle); |
927c7a9e FW |
7182 | |
7183 | exit: | |
7184 | rcu_read_unlock(); | |
56201969 | 7185 | return err; |
0322cd6e PZ |
7186 | } |
7187 | ||
9ecda41a WN |
7188 | void |
7189 | perf_event_output_forward(struct perf_event *event, | |
7190 | struct perf_sample_data *data, | |
7191 | struct pt_regs *regs) | |
7192 | { | |
7193 | __perf_event_output(event, data, regs, perf_output_begin_forward); | |
7194 | } | |
7195 | ||
7196 | void | |
7197 | perf_event_output_backward(struct perf_event *event, | |
7198 | struct perf_sample_data *data, | |
7199 | struct pt_regs *regs) | |
7200 | { | |
7201 | __perf_event_output(event, data, regs, perf_output_begin_backward); | |
7202 | } | |
7203 | ||
56201969 | 7204 | int |
9ecda41a WN |
7205 | perf_event_output(struct perf_event *event, |
7206 | struct perf_sample_data *data, | |
7207 | struct pt_regs *regs) | |
7208 | { | |
56201969 | 7209 | return __perf_event_output(event, data, regs, perf_output_begin); |
9ecda41a WN |
7210 | } |
7211 | ||
38b200d6 | 7212 | /* |
cdd6c482 | 7213 | * read event_id |
38b200d6 PZ |
7214 | */ |
7215 | ||
7216 | struct perf_read_event { | |
7217 | struct perf_event_header header; | |
7218 | ||
7219 | u32 pid; | |
7220 | u32 tid; | |
38b200d6 PZ |
7221 | }; |
7222 | ||
7223 | static void | |
cdd6c482 | 7224 | perf_event_read_event(struct perf_event *event, |
38b200d6 PZ |
7225 | struct task_struct *task) |
7226 | { | |
7227 | struct perf_output_handle handle; | |
c980d109 | 7228 | struct perf_sample_data sample; |
dfc65094 | 7229 | struct perf_read_event read_event = { |
38b200d6 | 7230 | .header = { |
cdd6c482 | 7231 | .type = PERF_RECORD_READ, |
38b200d6 | 7232 | .misc = 0, |
c320c7b7 | 7233 | .size = sizeof(read_event) + event->read_size, |
38b200d6 | 7234 | }, |
cdd6c482 IM |
7235 | .pid = perf_event_pid(event, task), |
7236 | .tid = perf_event_tid(event, task), | |
38b200d6 | 7237 | }; |
3dab77fb | 7238 | int ret; |
38b200d6 | 7239 | |
c980d109 | 7240 | perf_event_header__init_id(&read_event.header, &sample, event); |
a7ac67ea | 7241 | ret = perf_output_begin(&handle, event, read_event.header.size); |
38b200d6 PZ |
7242 | if (ret) |
7243 | return; | |
7244 | ||
dfc65094 | 7245 | perf_output_put(&handle, read_event); |
cdd6c482 | 7246 | perf_output_read(&handle, event); |
c980d109 | 7247 | perf_event__output_id_sample(event, &handle, &sample); |
3dab77fb | 7248 | |
38b200d6 PZ |
7249 | perf_output_end(&handle); |
7250 | } | |
7251 | ||
aab5b71e | 7252 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
52d857a8 JO |
7253 | |
7254 | static void | |
aab5b71e PZ |
7255 | perf_iterate_ctx(struct perf_event_context *ctx, |
7256 | perf_iterate_f output, | |
b73e4fef | 7257 | void *data, bool all) |
52d857a8 JO |
7258 | { |
7259 | struct perf_event *event; | |
7260 | ||
7261 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
b73e4fef AS |
7262 | if (!all) { |
7263 | if (event->state < PERF_EVENT_STATE_INACTIVE) | |
7264 | continue; | |
7265 | if (!event_filter_match(event)) | |
7266 | continue; | |
7267 | } | |
7268 | ||
67516844 | 7269 | output(event, data); |
52d857a8 JO |
7270 | } |
7271 | } | |
7272 | ||
aab5b71e | 7273 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
f2fb6bef KL |
7274 | { |
7275 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); | |
7276 | struct perf_event *event; | |
7277 | ||
7278 | list_for_each_entry_rcu(event, &pel->list, sb_list) { | |
0b8f1e2e PZ |
7279 | /* |
7280 | * Skip events that are not fully formed yet; ensure that | |
7281 | * if we observe event->ctx, both event and ctx will be | |
7282 | * complete enough. See perf_install_in_context(). | |
7283 | */ | |
7284 | if (!smp_load_acquire(&event->ctx)) | |
7285 | continue; | |
7286 | ||
f2fb6bef KL |
7287 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
7288 | continue; | |
7289 | if (!event_filter_match(event)) | |
7290 | continue; | |
7291 | output(event, data); | |
7292 | } | |
7293 | } | |
7294 | ||
aab5b71e PZ |
7295 | /* |
7296 | * Iterate all events that need to receive side-band events. | |
7297 | * | |
7298 | * For new callers; ensure that account_pmu_sb_event() includes | |
7299 | * your event, otherwise it might not get delivered. | |
7300 | */ | |
52d857a8 | 7301 | static void |
aab5b71e | 7302 | perf_iterate_sb(perf_iterate_f output, void *data, |
52d857a8 JO |
7303 | struct perf_event_context *task_ctx) |
7304 | { | |
52d857a8 | 7305 | struct perf_event_context *ctx; |
52d857a8 JO |
7306 | int ctxn; |
7307 | ||
aab5b71e PZ |
7308 | rcu_read_lock(); |
7309 | preempt_disable(); | |
7310 | ||
4e93ad60 | 7311 | /* |
aab5b71e PZ |
7312 | * If we have task_ctx != NULL we only notify the task context itself. |
7313 | * The task_ctx is set only for EXIT events before releasing task | |
4e93ad60 JO |
7314 | * context. |
7315 | */ | |
7316 | if (task_ctx) { | |
aab5b71e PZ |
7317 | perf_iterate_ctx(task_ctx, output, data, false); |
7318 | goto done; | |
4e93ad60 JO |
7319 | } |
7320 | ||
aab5b71e | 7321 | perf_iterate_sb_cpu(output, data); |
f2fb6bef KL |
7322 | |
7323 | for_each_task_context_nr(ctxn) { | |
52d857a8 JO |
7324 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
7325 | if (ctx) | |
aab5b71e | 7326 | perf_iterate_ctx(ctx, output, data, false); |
52d857a8 | 7327 | } |
aab5b71e | 7328 | done: |
f2fb6bef | 7329 | preempt_enable(); |
52d857a8 | 7330 | rcu_read_unlock(); |
95ff4ca2 AS |
7331 | } |
7332 | ||
375637bc AS |
7333 | /* |
7334 | * Clear all file-based filters at exec, they'll have to be | |
7335 | * re-instated when/if these objects are mmapped again. | |
7336 | */ | |
7337 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) | |
7338 | { | |
7339 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
7340 | struct perf_addr_filter *filter; | |
7341 | unsigned int restart = 0, count = 0; | |
7342 | unsigned long flags; | |
7343 | ||
7344 | if (!has_addr_filter(event)) | |
7345 | return; | |
7346 | ||
7347 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
7348 | list_for_each_entry(filter, &ifh->list, entry) { | |
9511bce9 | 7349 | if (filter->path.dentry) { |
c60f83b8 AS |
7350 | event->addr_filter_ranges[count].start = 0; |
7351 | event->addr_filter_ranges[count].size = 0; | |
375637bc AS |
7352 | restart++; |
7353 | } | |
7354 | ||
7355 | count++; | |
7356 | } | |
7357 | ||
7358 | if (restart) | |
7359 | event->addr_filters_gen++; | |
7360 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
7361 | ||
7362 | if (restart) | |
767ae086 | 7363 | perf_event_stop(event, 1); |
375637bc AS |
7364 | } |
7365 | ||
7366 | void perf_event_exec(void) | |
7367 | { | |
7368 | struct perf_event_context *ctx; | |
7369 | int ctxn; | |
7370 | ||
7371 | rcu_read_lock(); | |
7372 | for_each_task_context_nr(ctxn) { | |
7373 | ctx = current->perf_event_ctxp[ctxn]; | |
7374 | if (!ctx) | |
7375 | continue; | |
7376 | ||
7377 | perf_event_enable_on_exec(ctxn); | |
7378 | ||
aab5b71e | 7379 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
375637bc AS |
7380 | true); |
7381 | } | |
7382 | rcu_read_unlock(); | |
7383 | } | |
7384 | ||
95ff4ca2 | 7385 | struct remote_output { |
56de4e8f | 7386 | struct perf_buffer *rb; |
95ff4ca2 AS |
7387 | int err; |
7388 | }; | |
7389 | ||
7390 | static void __perf_event_output_stop(struct perf_event *event, void *data) | |
7391 | { | |
7392 | struct perf_event *parent = event->parent; | |
7393 | struct remote_output *ro = data; | |
56de4e8f | 7394 | struct perf_buffer *rb = ro->rb; |
375637bc AS |
7395 | struct stop_event_data sd = { |
7396 | .event = event, | |
7397 | }; | |
95ff4ca2 AS |
7398 | |
7399 | if (!has_aux(event)) | |
7400 | return; | |
7401 | ||
7402 | if (!parent) | |
7403 | parent = event; | |
7404 | ||
7405 | /* | |
7406 | * In case of inheritance, it will be the parent that links to the | |
767ae086 AS |
7407 | * ring-buffer, but it will be the child that's actually using it. |
7408 | * | |
7409 | * We are using event::rb to determine if the event should be stopped, | |
7410 | * however this may race with ring_buffer_attach() (through set_output), | |
7411 | * which will make us skip the event that actually needs to be stopped. | |
7412 | * So ring_buffer_attach() has to stop an aux event before re-assigning | |
7413 | * its rb pointer. | |
95ff4ca2 AS |
7414 | */ |
7415 | if (rcu_dereference(parent->rb) == rb) | |
375637bc | 7416 | ro->err = __perf_event_stop(&sd); |
95ff4ca2 AS |
7417 | } |
7418 | ||
7419 | static int __perf_pmu_output_stop(void *info) | |
7420 | { | |
7421 | struct perf_event *event = info; | |
f3a519e4 | 7422 | struct pmu *pmu = event->ctx->pmu; |
8b6a3fe8 | 7423 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
95ff4ca2 AS |
7424 | struct remote_output ro = { |
7425 | .rb = event->rb, | |
7426 | }; | |
7427 | ||
7428 | rcu_read_lock(); | |
aab5b71e | 7429 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
95ff4ca2 | 7430 | if (cpuctx->task_ctx) |
aab5b71e | 7431 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
b73e4fef | 7432 | &ro, false); |
95ff4ca2 AS |
7433 | rcu_read_unlock(); |
7434 | ||
7435 | return ro.err; | |
7436 | } | |
7437 | ||
7438 | static void perf_pmu_output_stop(struct perf_event *event) | |
7439 | { | |
7440 | struct perf_event *iter; | |
7441 | int err, cpu; | |
7442 | ||
7443 | restart: | |
7444 | rcu_read_lock(); | |
7445 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { | |
7446 | /* | |
7447 | * For per-CPU events, we need to make sure that neither they | |
7448 | * nor their children are running; for cpu==-1 events it's | |
7449 | * sufficient to stop the event itself if it's active, since | |
7450 | * it can't have children. | |
7451 | */ | |
7452 | cpu = iter->cpu; | |
7453 | if (cpu == -1) | |
7454 | cpu = READ_ONCE(iter->oncpu); | |
7455 | ||
7456 | if (cpu == -1) | |
7457 | continue; | |
7458 | ||
7459 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); | |
7460 | if (err == -EAGAIN) { | |
7461 | rcu_read_unlock(); | |
7462 | goto restart; | |
7463 | } | |
7464 | } | |
7465 | rcu_read_unlock(); | |
52d857a8 JO |
7466 | } |
7467 | ||
60313ebe | 7468 | /* |
9f498cc5 PZ |
7469 | * task tracking -- fork/exit |
7470 | * | |
13d7a241 | 7471 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
60313ebe PZ |
7472 | */ |
7473 | ||
9f498cc5 | 7474 | struct perf_task_event { |
3a80b4a3 | 7475 | struct task_struct *task; |
cdd6c482 | 7476 | struct perf_event_context *task_ctx; |
60313ebe PZ |
7477 | |
7478 | struct { | |
7479 | struct perf_event_header header; | |
7480 | ||
7481 | u32 pid; | |
7482 | u32 ppid; | |
9f498cc5 PZ |
7483 | u32 tid; |
7484 | u32 ptid; | |
393b2ad8 | 7485 | u64 time; |
cdd6c482 | 7486 | } event_id; |
60313ebe PZ |
7487 | }; |
7488 | ||
67516844 JO |
7489 | static int perf_event_task_match(struct perf_event *event) |
7490 | { | |
13d7a241 SE |
7491 | return event->attr.comm || event->attr.mmap || |
7492 | event->attr.mmap2 || event->attr.mmap_data || | |
7493 | event->attr.task; | |
67516844 JO |
7494 | } |
7495 | ||
cdd6c482 | 7496 | static void perf_event_task_output(struct perf_event *event, |
52d857a8 | 7497 | void *data) |
60313ebe | 7498 | { |
52d857a8 | 7499 | struct perf_task_event *task_event = data; |
60313ebe | 7500 | struct perf_output_handle handle; |
c980d109 | 7501 | struct perf_sample_data sample; |
9f498cc5 | 7502 | struct task_struct *task = task_event->task; |
c980d109 | 7503 | int ret, size = task_event->event_id.header.size; |
8bb39f9a | 7504 | |
67516844 JO |
7505 | if (!perf_event_task_match(event)) |
7506 | return; | |
7507 | ||
c980d109 | 7508 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
60313ebe | 7509 | |
c980d109 | 7510 | ret = perf_output_begin(&handle, event, |
a7ac67ea | 7511 | task_event->event_id.header.size); |
ef60777c | 7512 | if (ret) |
c980d109 | 7513 | goto out; |
60313ebe | 7514 | |
cdd6c482 | 7515 | task_event->event_id.pid = perf_event_pid(event, task); |
cdd6c482 | 7516 | task_event->event_id.tid = perf_event_tid(event, task); |
f3bed55e IR |
7517 | |
7518 | if (task_event->event_id.header.type == PERF_RECORD_EXIT) { | |
7519 | task_event->event_id.ppid = perf_event_pid(event, | |
7520 | task->real_parent); | |
7521 | task_event->event_id.ptid = perf_event_pid(event, | |
7522 | task->real_parent); | |
7523 | } else { /* PERF_RECORD_FORK */ | |
7524 | task_event->event_id.ppid = perf_event_pid(event, current); | |
7525 | task_event->event_id.ptid = perf_event_tid(event, current); | |
7526 | } | |
9f498cc5 | 7527 | |
34f43927 PZ |
7528 | task_event->event_id.time = perf_event_clock(event); |
7529 | ||
cdd6c482 | 7530 | perf_output_put(&handle, task_event->event_id); |
393b2ad8 | 7531 | |
c980d109 ACM |
7532 | perf_event__output_id_sample(event, &handle, &sample); |
7533 | ||
60313ebe | 7534 | perf_output_end(&handle); |
c980d109 ACM |
7535 | out: |
7536 | task_event->event_id.header.size = size; | |
60313ebe PZ |
7537 | } |
7538 | ||
cdd6c482 IM |
7539 | static void perf_event_task(struct task_struct *task, |
7540 | struct perf_event_context *task_ctx, | |
3a80b4a3 | 7541 | int new) |
60313ebe | 7542 | { |
9f498cc5 | 7543 | struct perf_task_event task_event; |
60313ebe | 7544 | |
cdd6c482 IM |
7545 | if (!atomic_read(&nr_comm_events) && |
7546 | !atomic_read(&nr_mmap_events) && | |
7547 | !atomic_read(&nr_task_events)) | |
60313ebe PZ |
7548 | return; |
7549 | ||
9f498cc5 | 7550 | task_event = (struct perf_task_event){ |
3a80b4a3 PZ |
7551 | .task = task, |
7552 | .task_ctx = task_ctx, | |
cdd6c482 | 7553 | .event_id = { |
60313ebe | 7554 | .header = { |
cdd6c482 | 7555 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
573402db | 7556 | .misc = 0, |
cdd6c482 | 7557 | .size = sizeof(task_event.event_id), |
60313ebe | 7558 | }, |
573402db PZ |
7559 | /* .pid */ |
7560 | /* .ppid */ | |
9f498cc5 PZ |
7561 | /* .tid */ |
7562 | /* .ptid */ | |
34f43927 | 7563 | /* .time */ |
60313ebe PZ |
7564 | }, |
7565 | }; | |
7566 | ||
aab5b71e | 7567 | perf_iterate_sb(perf_event_task_output, |
52d857a8 JO |
7568 | &task_event, |
7569 | task_ctx); | |
9f498cc5 PZ |
7570 | } |
7571 | ||
cdd6c482 | 7572 | void perf_event_fork(struct task_struct *task) |
9f498cc5 | 7573 | { |
cdd6c482 | 7574 | perf_event_task(task, NULL, 1); |
e4222673 | 7575 | perf_event_namespaces(task); |
60313ebe PZ |
7576 | } |
7577 | ||
8d1b2d93 PZ |
7578 | /* |
7579 | * comm tracking | |
7580 | */ | |
7581 | ||
7582 | struct perf_comm_event { | |
22a4f650 IM |
7583 | struct task_struct *task; |
7584 | char *comm; | |
8d1b2d93 PZ |
7585 | int comm_size; |
7586 | ||
7587 | struct { | |
7588 | struct perf_event_header header; | |
7589 | ||
7590 | u32 pid; | |
7591 | u32 tid; | |
cdd6c482 | 7592 | } event_id; |
8d1b2d93 PZ |
7593 | }; |
7594 | ||
67516844 JO |
7595 | static int perf_event_comm_match(struct perf_event *event) |
7596 | { | |
7597 | return event->attr.comm; | |
7598 | } | |
7599 | ||
cdd6c482 | 7600 | static void perf_event_comm_output(struct perf_event *event, |
52d857a8 | 7601 | void *data) |
8d1b2d93 | 7602 | { |
52d857a8 | 7603 | struct perf_comm_event *comm_event = data; |
8d1b2d93 | 7604 | struct perf_output_handle handle; |
c980d109 | 7605 | struct perf_sample_data sample; |
cdd6c482 | 7606 | int size = comm_event->event_id.header.size; |
c980d109 ACM |
7607 | int ret; |
7608 | ||
67516844 JO |
7609 | if (!perf_event_comm_match(event)) |
7610 | return; | |
7611 | ||
c980d109 ACM |
7612 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
7613 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7614 | comm_event->event_id.header.size); |
8d1b2d93 PZ |
7615 | |
7616 | if (ret) | |
c980d109 | 7617 | goto out; |
8d1b2d93 | 7618 | |
cdd6c482 IM |
7619 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
7620 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); | |
709e50cf | 7621 | |
cdd6c482 | 7622 | perf_output_put(&handle, comm_event->event_id); |
76369139 | 7623 | __output_copy(&handle, comm_event->comm, |
8d1b2d93 | 7624 | comm_event->comm_size); |
c980d109 ACM |
7625 | |
7626 | perf_event__output_id_sample(event, &handle, &sample); | |
7627 | ||
8d1b2d93 | 7628 | perf_output_end(&handle); |
c980d109 ACM |
7629 | out: |
7630 | comm_event->event_id.header.size = size; | |
8d1b2d93 PZ |
7631 | } |
7632 | ||
cdd6c482 | 7633 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
8d1b2d93 | 7634 | { |
413ee3b4 | 7635 | char comm[TASK_COMM_LEN]; |
8d1b2d93 | 7636 | unsigned int size; |
8d1b2d93 | 7637 | |
413ee3b4 | 7638 | memset(comm, 0, sizeof(comm)); |
96b02d78 | 7639 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
888fcee0 | 7640 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
8d1b2d93 PZ |
7641 | |
7642 | comm_event->comm = comm; | |
7643 | comm_event->comm_size = size; | |
7644 | ||
cdd6c482 | 7645 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
8dc85d54 | 7646 | |
aab5b71e | 7647 | perf_iterate_sb(perf_event_comm_output, |
52d857a8 JO |
7648 | comm_event, |
7649 | NULL); | |
8d1b2d93 PZ |
7650 | } |
7651 | ||
82b89778 | 7652 | void perf_event_comm(struct task_struct *task, bool exec) |
8d1b2d93 | 7653 | { |
9ee318a7 PZ |
7654 | struct perf_comm_event comm_event; |
7655 | ||
cdd6c482 | 7656 | if (!atomic_read(&nr_comm_events)) |
9ee318a7 | 7657 | return; |
a63eaf34 | 7658 | |
9ee318a7 | 7659 | comm_event = (struct perf_comm_event){ |
8d1b2d93 | 7660 | .task = task, |
573402db PZ |
7661 | /* .comm */ |
7662 | /* .comm_size */ | |
cdd6c482 | 7663 | .event_id = { |
573402db | 7664 | .header = { |
cdd6c482 | 7665 | .type = PERF_RECORD_COMM, |
82b89778 | 7666 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
573402db PZ |
7667 | /* .size */ |
7668 | }, | |
7669 | /* .pid */ | |
7670 | /* .tid */ | |
8d1b2d93 PZ |
7671 | }, |
7672 | }; | |
7673 | ||
cdd6c482 | 7674 | perf_event_comm_event(&comm_event); |
8d1b2d93 PZ |
7675 | } |
7676 | ||
e4222673 HB |
7677 | /* |
7678 | * namespaces tracking | |
7679 | */ | |
7680 | ||
7681 | struct perf_namespaces_event { | |
7682 | struct task_struct *task; | |
7683 | ||
7684 | struct { | |
7685 | struct perf_event_header header; | |
7686 | ||
7687 | u32 pid; | |
7688 | u32 tid; | |
7689 | u64 nr_namespaces; | |
7690 | struct perf_ns_link_info link_info[NR_NAMESPACES]; | |
7691 | } event_id; | |
7692 | }; | |
7693 | ||
7694 | static int perf_event_namespaces_match(struct perf_event *event) | |
7695 | { | |
7696 | return event->attr.namespaces; | |
7697 | } | |
7698 | ||
7699 | static void perf_event_namespaces_output(struct perf_event *event, | |
7700 | void *data) | |
7701 | { | |
7702 | struct perf_namespaces_event *namespaces_event = data; | |
7703 | struct perf_output_handle handle; | |
7704 | struct perf_sample_data sample; | |
34900ec5 | 7705 | u16 header_size = namespaces_event->event_id.header.size; |
e4222673 HB |
7706 | int ret; |
7707 | ||
7708 | if (!perf_event_namespaces_match(event)) | |
7709 | return; | |
7710 | ||
7711 | perf_event_header__init_id(&namespaces_event->event_id.header, | |
7712 | &sample, event); | |
7713 | ret = perf_output_begin(&handle, event, | |
7714 | namespaces_event->event_id.header.size); | |
7715 | if (ret) | |
34900ec5 | 7716 | goto out; |
e4222673 HB |
7717 | |
7718 | namespaces_event->event_id.pid = perf_event_pid(event, | |
7719 | namespaces_event->task); | |
7720 | namespaces_event->event_id.tid = perf_event_tid(event, | |
7721 | namespaces_event->task); | |
7722 | ||
7723 | perf_output_put(&handle, namespaces_event->event_id); | |
7724 | ||
7725 | perf_event__output_id_sample(event, &handle, &sample); | |
7726 | ||
7727 | perf_output_end(&handle); | |
34900ec5 JO |
7728 | out: |
7729 | namespaces_event->event_id.header.size = header_size; | |
e4222673 HB |
7730 | } |
7731 | ||
7732 | static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, | |
7733 | struct task_struct *task, | |
7734 | const struct proc_ns_operations *ns_ops) | |
7735 | { | |
7736 | struct path ns_path; | |
7737 | struct inode *ns_inode; | |
ce623f89 | 7738 | int error; |
e4222673 HB |
7739 | |
7740 | error = ns_get_path(&ns_path, task, ns_ops); | |
7741 | if (!error) { | |
7742 | ns_inode = ns_path.dentry->d_inode; | |
7743 | ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); | |
7744 | ns_link_info->ino = ns_inode->i_ino; | |
0e18dd12 | 7745 | path_put(&ns_path); |
e4222673 HB |
7746 | } |
7747 | } | |
7748 | ||
7749 | void perf_event_namespaces(struct task_struct *task) | |
7750 | { | |
7751 | struct perf_namespaces_event namespaces_event; | |
7752 | struct perf_ns_link_info *ns_link_info; | |
7753 | ||
7754 | if (!atomic_read(&nr_namespaces_events)) | |
7755 | return; | |
7756 | ||
7757 | namespaces_event = (struct perf_namespaces_event){ | |
7758 | .task = task, | |
7759 | .event_id = { | |
7760 | .header = { | |
7761 | .type = PERF_RECORD_NAMESPACES, | |
7762 | .misc = 0, | |
7763 | .size = sizeof(namespaces_event.event_id), | |
7764 | }, | |
7765 | /* .pid */ | |
7766 | /* .tid */ | |
7767 | .nr_namespaces = NR_NAMESPACES, | |
7768 | /* .link_info[NR_NAMESPACES] */ | |
7769 | }, | |
7770 | }; | |
7771 | ||
7772 | ns_link_info = namespaces_event.event_id.link_info; | |
7773 | ||
7774 | perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], | |
7775 | task, &mntns_operations); | |
7776 | ||
7777 | #ifdef CONFIG_USER_NS | |
7778 | perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], | |
7779 | task, &userns_operations); | |
7780 | #endif | |
7781 | #ifdef CONFIG_NET_NS | |
7782 | perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], | |
7783 | task, &netns_operations); | |
7784 | #endif | |
7785 | #ifdef CONFIG_UTS_NS | |
7786 | perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], | |
7787 | task, &utsns_operations); | |
7788 | #endif | |
7789 | #ifdef CONFIG_IPC_NS | |
7790 | perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], | |
7791 | task, &ipcns_operations); | |
7792 | #endif | |
7793 | #ifdef CONFIG_PID_NS | |
7794 | perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], | |
7795 | task, &pidns_operations); | |
7796 | #endif | |
7797 | #ifdef CONFIG_CGROUPS | |
7798 | perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], | |
7799 | task, &cgroupns_operations); | |
7800 | #endif | |
7801 | ||
7802 | perf_iterate_sb(perf_event_namespaces_output, | |
7803 | &namespaces_event, | |
7804 | NULL); | |
7805 | } | |
7806 | ||
96aaab68 NK |
7807 | /* |
7808 | * cgroup tracking | |
7809 | */ | |
7810 | #ifdef CONFIG_CGROUP_PERF | |
7811 | ||
7812 | struct perf_cgroup_event { | |
7813 | char *path; | |
7814 | int path_size; | |
7815 | struct { | |
7816 | struct perf_event_header header; | |
7817 | u64 id; | |
7818 | char path[]; | |
7819 | } event_id; | |
7820 | }; | |
7821 | ||
7822 | static int perf_event_cgroup_match(struct perf_event *event) | |
7823 | { | |
7824 | return event->attr.cgroup; | |
7825 | } | |
7826 | ||
7827 | static void perf_event_cgroup_output(struct perf_event *event, void *data) | |
7828 | { | |
7829 | struct perf_cgroup_event *cgroup_event = data; | |
7830 | struct perf_output_handle handle; | |
7831 | struct perf_sample_data sample; | |
7832 | u16 header_size = cgroup_event->event_id.header.size; | |
7833 | int ret; | |
7834 | ||
7835 | if (!perf_event_cgroup_match(event)) | |
7836 | return; | |
7837 | ||
7838 | perf_event_header__init_id(&cgroup_event->event_id.header, | |
7839 | &sample, event); | |
7840 | ret = perf_output_begin(&handle, event, | |
7841 | cgroup_event->event_id.header.size); | |
7842 | if (ret) | |
7843 | goto out; | |
7844 | ||
7845 | perf_output_put(&handle, cgroup_event->event_id); | |
7846 | __output_copy(&handle, cgroup_event->path, cgroup_event->path_size); | |
7847 | ||
7848 | perf_event__output_id_sample(event, &handle, &sample); | |
7849 | ||
7850 | perf_output_end(&handle); | |
7851 | out: | |
7852 | cgroup_event->event_id.header.size = header_size; | |
7853 | } | |
7854 | ||
7855 | static void perf_event_cgroup(struct cgroup *cgrp) | |
7856 | { | |
7857 | struct perf_cgroup_event cgroup_event; | |
7858 | char path_enomem[16] = "//enomem"; | |
7859 | char *pathname; | |
7860 | size_t size; | |
7861 | ||
7862 | if (!atomic_read(&nr_cgroup_events)) | |
7863 | return; | |
7864 | ||
7865 | cgroup_event = (struct perf_cgroup_event){ | |
7866 | .event_id = { | |
7867 | .header = { | |
7868 | .type = PERF_RECORD_CGROUP, | |
7869 | .misc = 0, | |
7870 | .size = sizeof(cgroup_event.event_id), | |
7871 | }, | |
7872 | .id = cgroup_id(cgrp), | |
7873 | }, | |
7874 | }; | |
7875 | ||
7876 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); | |
7877 | if (pathname == NULL) { | |
7878 | cgroup_event.path = path_enomem; | |
7879 | } else { | |
7880 | /* just to be sure to have enough space for alignment */ | |
7881 | cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64)); | |
7882 | cgroup_event.path = pathname; | |
7883 | } | |
7884 | ||
7885 | /* | |
7886 | * Since our buffer works in 8 byte units we need to align our string | |
7887 | * size to a multiple of 8. However, we must guarantee the tail end is | |
7888 | * zero'd out to avoid leaking random bits to userspace. | |
7889 | */ | |
7890 | size = strlen(cgroup_event.path) + 1; | |
7891 | while (!IS_ALIGNED(size, sizeof(u64))) | |
7892 | cgroup_event.path[size++] = '\0'; | |
7893 | ||
7894 | cgroup_event.event_id.header.size += size; | |
7895 | cgroup_event.path_size = size; | |
7896 | ||
7897 | perf_iterate_sb(perf_event_cgroup_output, | |
7898 | &cgroup_event, | |
7899 | NULL); | |
7900 | ||
7901 | kfree(pathname); | |
7902 | } | |
7903 | ||
7904 | #endif | |
7905 | ||
0a4a9391 PZ |
7906 | /* |
7907 | * mmap tracking | |
7908 | */ | |
7909 | ||
7910 | struct perf_mmap_event { | |
089dd79d PZ |
7911 | struct vm_area_struct *vma; |
7912 | ||
7913 | const char *file_name; | |
7914 | int file_size; | |
13d7a241 SE |
7915 | int maj, min; |
7916 | u64 ino; | |
7917 | u64 ino_generation; | |
f972eb63 | 7918 | u32 prot, flags; |
0a4a9391 PZ |
7919 | |
7920 | struct { | |
7921 | struct perf_event_header header; | |
7922 | ||
7923 | u32 pid; | |
7924 | u32 tid; | |
7925 | u64 start; | |
7926 | u64 len; | |
7927 | u64 pgoff; | |
cdd6c482 | 7928 | } event_id; |
0a4a9391 PZ |
7929 | }; |
7930 | ||
67516844 JO |
7931 | static int perf_event_mmap_match(struct perf_event *event, |
7932 | void *data) | |
7933 | { | |
7934 | struct perf_mmap_event *mmap_event = data; | |
7935 | struct vm_area_struct *vma = mmap_event->vma; | |
7936 | int executable = vma->vm_flags & VM_EXEC; | |
7937 | ||
7938 | return (!executable && event->attr.mmap_data) || | |
13d7a241 | 7939 | (executable && (event->attr.mmap || event->attr.mmap2)); |
67516844 JO |
7940 | } |
7941 | ||
cdd6c482 | 7942 | static void perf_event_mmap_output(struct perf_event *event, |
52d857a8 | 7943 | void *data) |
0a4a9391 | 7944 | { |
52d857a8 | 7945 | struct perf_mmap_event *mmap_event = data; |
0a4a9391 | 7946 | struct perf_output_handle handle; |
c980d109 | 7947 | struct perf_sample_data sample; |
cdd6c482 | 7948 | int size = mmap_event->event_id.header.size; |
d9c1bb2f | 7949 | u32 type = mmap_event->event_id.header.type; |
c980d109 | 7950 | int ret; |
0a4a9391 | 7951 | |
67516844 JO |
7952 | if (!perf_event_mmap_match(event, data)) |
7953 | return; | |
7954 | ||
13d7a241 SE |
7955 | if (event->attr.mmap2) { |
7956 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; | |
7957 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); | |
7958 | mmap_event->event_id.header.size += sizeof(mmap_event->min); | |
7959 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); | |
d008d525 | 7960 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
f972eb63 PZ |
7961 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
7962 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); | |
13d7a241 SE |
7963 | } |
7964 | ||
c980d109 ACM |
7965 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
7966 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 7967 | mmap_event->event_id.header.size); |
0a4a9391 | 7968 | if (ret) |
c980d109 | 7969 | goto out; |
0a4a9391 | 7970 | |
cdd6c482 IM |
7971 | mmap_event->event_id.pid = perf_event_pid(event, current); |
7972 | mmap_event->event_id.tid = perf_event_tid(event, current); | |
709e50cf | 7973 | |
cdd6c482 | 7974 | perf_output_put(&handle, mmap_event->event_id); |
13d7a241 SE |
7975 | |
7976 | if (event->attr.mmap2) { | |
7977 | perf_output_put(&handle, mmap_event->maj); | |
7978 | perf_output_put(&handle, mmap_event->min); | |
7979 | perf_output_put(&handle, mmap_event->ino); | |
7980 | perf_output_put(&handle, mmap_event->ino_generation); | |
f972eb63 PZ |
7981 | perf_output_put(&handle, mmap_event->prot); |
7982 | perf_output_put(&handle, mmap_event->flags); | |
13d7a241 SE |
7983 | } |
7984 | ||
76369139 | 7985 | __output_copy(&handle, mmap_event->file_name, |
0a4a9391 | 7986 | mmap_event->file_size); |
c980d109 ACM |
7987 | |
7988 | perf_event__output_id_sample(event, &handle, &sample); | |
7989 | ||
78d613eb | 7990 | perf_output_end(&handle); |
c980d109 ACM |
7991 | out: |
7992 | mmap_event->event_id.header.size = size; | |
d9c1bb2f | 7993 | mmap_event->event_id.header.type = type; |
0a4a9391 PZ |
7994 | } |
7995 | ||
cdd6c482 | 7996 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
0a4a9391 | 7997 | { |
089dd79d PZ |
7998 | struct vm_area_struct *vma = mmap_event->vma; |
7999 | struct file *file = vma->vm_file; | |
13d7a241 SE |
8000 | int maj = 0, min = 0; |
8001 | u64 ino = 0, gen = 0; | |
f972eb63 | 8002 | u32 prot = 0, flags = 0; |
0a4a9391 PZ |
8003 | unsigned int size; |
8004 | char tmp[16]; | |
8005 | char *buf = NULL; | |
2c42cfbf | 8006 | char *name; |
413ee3b4 | 8007 | |
0b3589be PZ |
8008 | if (vma->vm_flags & VM_READ) |
8009 | prot |= PROT_READ; | |
8010 | if (vma->vm_flags & VM_WRITE) | |
8011 | prot |= PROT_WRITE; | |
8012 | if (vma->vm_flags & VM_EXEC) | |
8013 | prot |= PROT_EXEC; | |
8014 | ||
8015 | if (vma->vm_flags & VM_MAYSHARE) | |
8016 | flags = MAP_SHARED; | |
8017 | else | |
8018 | flags = MAP_PRIVATE; | |
8019 | ||
8020 | if (vma->vm_flags & VM_DENYWRITE) | |
8021 | flags |= MAP_DENYWRITE; | |
8022 | if (vma->vm_flags & VM_MAYEXEC) | |
8023 | flags |= MAP_EXECUTABLE; | |
8024 | if (vma->vm_flags & VM_LOCKED) | |
8025 | flags |= MAP_LOCKED; | |
03911132 | 8026 | if (is_vm_hugetlb_page(vma)) |
0b3589be PZ |
8027 | flags |= MAP_HUGETLB; |
8028 | ||
0a4a9391 | 8029 | if (file) { |
13d7a241 SE |
8030 | struct inode *inode; |
8031 | dev_t dev; | |
3ea2f2b9 | 8032 | |
2c42cfbf | 8033 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
0a4a9391 | 8034 | if (!buf) { |
c7e548b4 ON |
8035 | name = "//enomem"; |
8036 | goto cpy_name; | |
0a4a9391 | 8037 | } |
413ee3b4 | 8038 | /* |
3ea2f2b9 | 8039 | * d_path() works from the end of the rb backwards, so we |
413ee3b4 AB |
8040 | * need to add enough zero bytes after the string to handle |
8041 | * the 64bit alignment we do later. | |
8042 | */ | |
9bf39ab2 | 8043 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
0a4a9391 | 8044 | if (IS_ERR(name)) { |
c7e548b4 ON |
8045 | name = "//toolong"; |
8046 | goto cpy_name; | |
0a4a9391 | 8047 | } |
13d7a241 SE |
8048 | inode = file_inode(vma->vm_file); |
8049 | dev = inode->i_sb->s_dev; | |
8050 | ino = inode->i_ino; | |
8051 | gen = inode->i_generation; | |
8052 | maj = MAJOR(dev); | |
8053 | min = MINOR(dev); | |
f972eb63 | 8054 | |
c7e548b4 | 8055 | goto got_name; |
0a4a9391 | 8056 | } else { |
fbe26abe JO |
8057 | if (vma->vm_ops && vma->vm_ops->name) { |
8058 | name = (char *) vma->vm_ops->name(vma); | |
8059 | if (name) | |
8060 | goto cpy_name; | |
8061 | } | |
8062 | ||
2c42cfbf | 8063 | name = (char *)arch_vma_name(vma); |
c7e548b4 ON |
8064 | if (name) |
8065 | goto cpy_name; | |
089dd79d | 8066 | |
32c5fb7e | 8067 | if (vma->vm_start <= vma->vm_mm->start_brk && |
3af9e859 | 8068 | vma->vm_end >= vma->vm_mm->brk) { |
c7e548b4 ON |
8069 | name = "[heap]"; |
8070 | goto cpy_name; | |
32c5fb7e ON |
8071 | } |
8072 | if (vma->vm_start <= vma->vm_mm->start_stack && | |
3af9e859 | 8073 | vma->vm_end >= vma->vm_mm->start_stack) { |
c7e548b4 ON |
8074 | name = "[stack]"; |
8075 | goto cpy_name; | |
089dd79d PZ |
8076 | } |
8077 | ||
c7e548b4 ON |
8078 | name = "//anon"; |
8079 | goto cpy_name; | |
0a4a9391 PZ |
8080 | } |
8081 | ||
c7e548b4 ON |
8082 | cpy_name: |
8083 | strlcpy(tmp, name, sizeof(tmp)); | |
8084 | name = tmp; | |
0a4a9391 | 8085 | got_name: |
2c42cfbf PZ |
8086 | /* |
8087 | * Since our buffer works in 8 byte units we need to align our string | |
8088 | * size to a multiple of 8. However, we must guarantee the tail end is | |
8089 | * zero'd out to avoid leaking random bits to userspace. | |
8090 | */ | |
8091 | size = strlen(name)+1; | |
8092 | while (!IS_ALIGNED(size, sizeof(u64))) | |
8093 | name[size++] = '\0'; | |
0a4a9391 PZ |
8094 | |
8095 | mmap_event->file_name = name; | |
8096 | mmap_event->file_size = size; | |
13d7a241 SE |
8097 | mmap_event->maj = maj; |
8098 | mmap_event->min = min; | |
8099 | mmap_event->ino = ino; | |
8100 | mmap_event->ino_generation = gen; | |
f972eb63 PZ |
8101 | mmap_event->prot = prot; |
8102 | mmap_event->flags = flags; | |
0a4a9391 | 8103 | |
2fe85427 SE |
8104 | if (!(vma->vm_flags & VM_EXEC)) |
8105 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
8106 | ||
cdd6c482 | 8107 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
0a4a9391 | 8108 | |
aab5b71e | 8109 | perf_iterate_sb(perf_event_mmap_output, |
52d857a8 JO |
8110 | mmap_event, |
8111 | NULL); | |
665c2142 | 8112 | |
0a4a9391 PZ |
8113 | kfree(buf); |
8114 | } | |
8115 | ||
375637bc AS |
8116 | /* |
8117 | * Check whether inode and address range match filter criteria. | |
8118 | */ | |
8119 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, | |
8120 | struct file *file, unsigned long offset, | |
8121 | unsigned long size) | |
8122 | { | |
7f635ff1 MP |
8123 | /* d_inode(NULL) won't be equal to any mapped user-space file */ |
8124 | if (!filter->path.dentry) | |
8125 | return false; | |
8126 | ||
9511bce9 | 8127 | if (d_inode(filter->path.dentry) != file_inode(file)) |
375637bc AS |
8128 | return false; |
8129 | ||
8130 | if (filter->offset > offset + size) | |
8131 | return false; | |
8132 | ||
8133 | if (filter->offset + filter->size < offset) | |
8134 | return false; | |
8135 | ||
8136 | return true; | |
8137 | } | |
8138 | ||
c60f83b8 AS |
8139 | static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter, |
8140 | struct vm_area_struct *vma, | |
8141 | struct perf_addr_filter_range *fr) | |
8142 | { | |
8143 | unsigned long vma_size = vma->vm_end - vma->vm_start; | |
8144 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
8145 | struct file *file = vma->vm_file; | |
8146 | ||
8147 | if (!perf_addr_filter_match(filter, file, off, vma_size)) | |
8148 | return false; | |
8149 | ||
8150 | if (filter->offset < off) { | |
8151 | fr->start = vma->vm_start; | |
8152 | fr->size = min(vma_size, filter->size - (off - filter->offset)); | |
8153 | } else { | |
8154 | fr->start = vma->vm_start + filter->offset - off; | |
8155 | fr->size = min(vma->vm_end - fr->start, filter->size); | |
8156 | } | |
8157 | ||
8158 | return true; | |
8159 | } | |
8160 | ||
375637bc AS |
8161 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
8162 | { | |
8163 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
8164 | struct vm_area_struct *vma = data; | |
375637bc AS |
8165 | struct perf_addr_filter *filter; |
8166 | unsigned int restart = 0, count = 0; | |
c60f83b8 | 8167 | unsigned long flags; |
375637bc AS |
8168 | |
8169 | if (!has_addr_filter(event)) | |
8170 | return; | |
8171 | ||
c60f83b8 | 8172 | if (!vma->vm_file) |
375637bc AS |
8173 | return; |
8174 | ||
8175 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
8176 | list_for_each_entry(filter, &ifh->list, entry) { | |
c60f83b8 AS |
8177 | if (perf_addr_filter_vma_adjust(filter, vma, |
8178 | &event->addr_filter_ranges[count])) | |
375637bc | 8179 | restart++; |
375637bc AS |
8180 | |
8181 | count++; | |
8182 | } | |
8183 | ||
8184 | if (restart) | |
8185 | event->addr_filters_gen++; | |
8186 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
8187 | ||
8188 | if (restart) | |
767ae086 | 8189 | perf_event_stop(event, 1); |
375637bc AS |
8190 | } |
8191 | ||
8192 | /* | |
8193 | * Adjust all task's events' filters to the new vma | |
8194 | */ | |
8195 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) | |
8196 | { | |
8197 | struct perf_event_context *ctx; | |
8198 | int ctxn; | |
8199 | ||
12b40a23 MP |
8200 | /* |
8201 | * Data tracing isn't supported yet and as such there is no need | |
8202 | * to keep track of anything that isn't related to executable code: | |
8203 | */ | |
8204 | if (!(vma->vm_flags & VM_EXEC)) | |
8205 | return; | |
8206 | ||
375637bc AS |
8207 | rcu_read_lock(); |
8208 | for_each_task_context_nr(ctxn) { | |
8209 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); | |
8210 | if (!ctx) | |
8211 | continue; | |
8212 | ||
aab5b71e | 8213 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
375637bc AS |
8214 | } |
8215 | rcu_read_unlock(); | |
8216 | } | |
8217 | ||
3af9e859 | 8218 | void perf_event_mmap(struct vm_area_struct *vma) |
0a4a9391 | 8219 | { |
9ee318a7 PZ |
8220 | struct perf_mmap_event mmap_event; |
8221 | ||
cdd6c482 | 8222 | if (!atomic_read(&nr_mmap_events)) |
9ee318a7 PZ |
8223 | return; |
8224 | ||
8225 | mmap_event = (struct perf_mmap_event){ | |
089dd79d | 8226 | .vma = vma, |
573402db PZ |
8227 | /* .file_name */ |
8228 | /* .file_size */ | |
cdd6c482 | 8229 | .event_id = { |
573402db | 8230 | .header = { |
cdd6c482 | 8231 | .type = PERF_RECORD_MMAP, |
39447b38 | 8232 | .misc = PERF_RECORD_MISC_USER, |
573402db PZ |
8233 | /* .size */ |
8234 | }, | |
8235 | /* .pid */ | |
8236 | /* .tid */ | |
089dd79d PZ |
8237 | .start = vma->vm_start, |
8238 | .len = vma->vm_end - vma->vm_start, | |
3a0304e9 | 8239 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
0a4a9391 | 8240 | }, |
13d7a241 SE |
8241 | /* .maj (attr_mmap2 only) */ |
8242 | /* .min (attr_mmap2 only) */ | |
8243 | /* .ino (attr_mmap2 only) */ | |
8244 | /* .ino_generation (attr_mmap2 only) */ | |
f972eb63 PZ |
8245 | /* .prot (attr_mmap2 only) */ |
8246 | /* .flags (attr_mmap2 only) */ | |
0a4a9391 PZ |
8247 | }; |
8248 | ||
375637bc | 8249 | perf_addr_filters_adjust(vma); |
cdd6c482 | 8250 | perf_event_mmap_event(&mmap_event); |
0a4a9391 PZ |
8251 | } |
8252 | ||
68db7e98 AS |
8253 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
8254 | unsigned long size, u64 flags) | |
8255 | { | |
8256 | struct perf_output_handle handle; | |
8257 | struct perf_sample_data sample; | |
8258 | struct perf_aux_event { | |
8259 | struct perf_event_header header; | |
8260 | u64 offset; | |
8261 | u64 size; | |
8262 | u64 flags; | |
8263 | } rec = { | |
8264 | .header = { | |
8265 | .type = PERF_RECORD_AUX, | |
8266 | .misc = 0, | |
8267 | .size = sizeof(rec), | |
8268 | }, | |
8269 | .offset = head, | |
8270 | .size = size, | |
8271 | .flags = flags, | |
8272 | }; | |
8273 | int ret; | |
8274 | ||
8275 | perf_event_header__init_id(&rec.header, &sample, event); | |
8276 | ret = perf_output_begin(&handle, event, rec.header.size); | |
8277 | ||
8278 | if (ret) | |
8279 | return; | |
8280 | ||
8281 | perf_output_put(&handle, rec); | |
8282 | perf_event__output_id_sample(event, &handle, &sample); | |
8283 | ||
8284 | perf_output_end(&handle); | |
8285 | } | |
8286 | ||
f38b0dbb KL |
8287 | /* |
8288 | * Lost/dropped samples logging | |
8289 | */ | |
8290 | void perf_log_lost_samples(struct perf_event *event, u64 lost) | |
8291 | { | |
8292 | struct perf_output_handle handle; | |
8293 | struct perf_sample_data sample; | |
8294 | int ret; | |
8295 | ||
8296 | struct { | |
8297 | struct perf_event_header header; | |
8298 | u64 lost; | |
8299 | } lost_samples_event = { | |
8300 | .header = { | |
8301 | .type = PERF_RECORD_LOST_SAMPLES, | |
8302 | .misc = 0, | |
8303 | .size = sizeof(lost_samples_event), | |
8304 | }, | |
8305 | .lost = lost, | |
8306 | }; | |
8307 | ||
8308 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); | |
8309 | ||
8310 | ret = perf_output_begin(&handle, event, | |
8311 | lost_samples_event.header.size); | |
8312 | if (ret) | |
8313 | return; | |
8314 | ||
8315 | perf_output_put(&handle, lost_samples_event); | |
8316 | perf_event__output_id_sample(event, &handle, &sample); | |
8317 | perf_output_end(&handle); | |
8318 | } | |
8319 | ||
45ac1403 AH |
8320 | /* |
8321 | * context_switch tracking | |
8322 | */ | |
8323 | ||
8324 | struct perf_switch_event { | |
8325 | struct task_struct *task; | |
8326 | struct task_struct *next_prev; | |
8327 | ||
8328 | struct { | |
8329 | struct perf_event_header header; | |
8330 | u32 next_prev_pid; | |
8331 | u32 next_prev_tid; | |
8332 | } event_id; | |
8333 | }; | |
8334 | ||
8335 | static int perf_event_switch_match(struct perf_event *event) | |
8336 | { | |
8337 | return event->attr.context_switch; | |
8338 | } | |
8339 | ||
8340 | static void perf_event_switch_output(struct perf_event *event, void *data) | |
8341 | { | |
8342 | struct perf_switch_event *se = data; | |
8343 | struct perf_output_handle handle; | |
8344 | struct perf_sample_data sample; | |
8345 | int ret; | |
8346 | ||
8347 | if (!perf_event_switch_match(event)) | |
8348 | return; | |
8349 | ||
8350 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ | |
8351 | if (event->ctx->task) { | |
8352 | se->event_id.header.type = PERF_RECORD_SWITCH; | |
8353 | se->event_id.header.size = sizeof(se->event_id.header); | |
8354 | } else { | |
8355 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; | |
8356 | se->event_id.header.size = sizeof(se->event_id); | |
8357 | se->event_id.next_prev_pid = | |
8358 | perf_event_pid(event, se->next_prev); | |
8359 | se->event_id.next_prev_tid = | |
8360 | perf_event_tid(event, se->next_prev); | |
8361 | } | |
8362 | ||
8363 | perf_event_header__init_id(&se->event_id.header, &sample, event); | |
8364 | ||
8365 | ret = perf_output_begin(&handle, event, se->event_id.header.size); | |
8366 | if (ret) | |
8367 | return; | |
8368 | ||
8369 | if (event->ctx->task) | |
8370 | perf_output_put(&handle, se->event_id.header); | |
8371 | else | |
8372 | perf_output_put(&handle, se->event_id); | |
8373 | ||
8374 | perf_event__output_id_sample(event, &handle, &sample); | |
8375 | ||
8376 | perf_output_end(&handle); | |
8377 | } | |
8378 | ||
8379 | static void perf_event_switch(struct task_struct *task, | |
8380 | struct task_struct *next_prev, bool sched_in) | |
8381 | { | |
8382 | struct perf_switch_event switch_event; | |
8383 | ||
8384 | /* N.B. caller checks nr_switch_events != 0 */ | |
8385 | ||
8386 | switch_event = (struct perf_switch_event){ | |
8387 | .task = task, | |
8388 | .next_prev = next_prev, | |
8389 | .event_id = { | |
8390 | .header = { | |
8391 | /* .type */ | |
8392 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, | |
8393 | /* .size */ | |
8394 | }, | |
8395 | /* .next_prev_pid */ | |
8396 | /* .next_prev_tid */ | |
8397 | }, | |
8398 | }; | |
8399 | ||
101592b4 AB |
8400 | if (!sched_in && task->state == TASK_RUNNING) |
8401 | switch_event.event_id.header.misc |= | |
8402 | PERF_RECORD_MISC_SWITCH_OUT_PREEMPT; | |
8403 | ||
aab5b71e | 8404 | perf_iterate_sb(perf_event_switch_output, |
45ac1403 AH |
8405 | &switch_event, |
8406 | NULL); | |
8407 | } | |
8408 | ||
a78ac325 PZ |
8409 | /* |
8410 | * IRQ throttle logging | |
8411 | */ | |
8412 | ||
cdd6c482 | 8413 | static void perf_log_throttle(struct perf_event *event, int enable) |
a78ac325 PZ |
8414 | { |
8415 | struct perf_output_handle handle; | |
c980d109 | 8416 | struct perf_sample_data sample; |
a78ac325 PZ |
8417 | int ret; |
8418 | ||
8419 | struct { | |
8420 | struct perf_event_header header; | |
8421 | u64 time; | |
cca3f454 | 8422 | u64 id; |
7f453c24 | 8423 | u64 stream_id; |
a78ac325 PZ |
8424 | } throttle_event = { |
8425 | .header = { | |
cdd6c482 | 8426 | .type = PERF_RECORD_THROTTLE, |
a78ac325 PZ |
8427 | .misc = 0, |
8428 | .size = sizeof(throttle_event), | |
8429 | }, | |
34f43927 | 8430 | .time = perf_event_clock(event), |
cdd6c482 IM |
8431 | .id = primary_event_id(event), |
8432 | .stream_id = event->id, | |
a78ac325 PZ |
8433 | }; |
8434 | ||
966ee4d6 | 8435 | if (enable) |
cdd6c482 | 8436 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
966ee4d6 | 8437 | |
c980d109 ACM |
8438 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
8439 | ||
8440 | ret = perf_output_begin(&handle, event, | |
a7ac67ea | 8441 | throttle_event.header.size); |
a78ac325 PZ |
8442 | if (ret) |
8443 | return; | |
8444 | ||
8445 | perf_output_put(&handle, throttle_event); | |
c980d109 | 8446 | perf_event__output_id_sample(event, &handle, &sample); |
a78ac325 PZ |
8447 | perf_output_end(&handle); |
8448 | } | |
8449 | ||
76193a94 SL |
8450 | /* |
8451 | * ksymbol register/unregister tracking | |
8452 | */ | |
8453 | ||
8454 | struct perf_ksymbol_event { | |
8455 | const char *name; | |
8456 | int name_len; | |
8457 | struct { | |
8458 | struct perf_event_header header; | |
8459 | u64 addr; | |
8460 | u32 len; | |
8461 | u16 ksym_type; | |
8462 | u16 flags; | |
8463 | } event_id; | |
8464 | }; | |
8465 | ||
8466 | static int perf_event_ksymbol_match(struct perf_event *event) | |
8467 | { | |
8468 | return event->attr.ksymbol; | |
8469 | } | |
8470 | ||
8471 | static void perf_event_ksymbol_output(struct perf_event *event, void *data) | |
8472 | { | |
8473 | struct perf_ksymbol_event *ksymbol_event = data; | |
8474 | struct perf_output_handle handle; | |
8475 | struct perf_sample_data sample; | |
8476 | int ret; | |
8477 | ||
8478 | if (!perf_event_ksymbol_match(event)) | |
8479 | return; | |
8480 | ||
8481 | perf_event_header__init_id(&ksymbol_event->event_id.header, | |
8482 | &sample, event); | |
8483 | ret = perf_output_begin(&handle, event, | |
8484 | ksymbol_event->event_id.header.size); | |
8485 | if (ret) | |
8486 | return; | |
8487 | ||
8488 | perf_output_put(&handle, ksymbol_event->event_id); | |
8489 | __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len); | |
8490 | perf_event__output_id_sample(event, &handle, &sample); | |
8491 | ||
8492 | perf_output_end(&handle); | |
8493 | } | |
8494 | ||
8495 | void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister, | |
8496 | const char *sym) | |
8497 | { | |
8498 | struct perf_ksymbol_event ksymbol_event; | |
8499 | char name[KSYM_NAME_LEN]; | |
8500 | u16 flags = 0; | |
8501 | int name_len; | |
8502 | ||
8503 | if (!atomic_read(&nr_ksymbol_events)) | |
8504 | return; | |
8505 | ||
8506 | if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX || | |
8507 | ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN) | |
8508 | goto err; | |
8509 | ||
8510 | strlcpy(name, sym, KSYM_NAME_LEN); | |
8511 | name_len = strlen(name) + 1; | |
8512 | while (!IS_ALIGNED(name_len, sizeof(u64))) | |
8513 | name[name_len++] = '\0'; | |
8514 | BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64)); | |
8515 | ||
8516 | if (unregister) | |
8517 | flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER; | |
8518 | ||
8519 | ksymbol_event = (struct perf_ksymbol_event){ | |
8520 | .name = name, | |
8521 | .name_len = name_len, | |
8522 | .event_id = { | |
8523 | .header = { | |
8524 | .type = PERF_RECORD_KSYMBOL, | |
8525 | .size = sizeof(ksymbol_event.event_id) + | |
8526 | name_len, | |
8527 | }, | |
8528 | .addr = addr, | |
8529 | .len = len, | |
8530 | .ksym_type = ksym_type, | |
8531 | .flags = flags, | |
8532 | }, | |
8533 | }; | |
8534 | ||
8535 | perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL); | |
8536 | return; | |
8537 | err: | |
8538 | WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type); | |
8539 | } | |
8540 | ||
6ee52e2a SL |
8541 | /* |
8542 | * bpf program load/unload tracking | |
8543 | */ | |
8544 | ||
8545 | struct perf_bpf_event { | |
8546 | struct bpf_prog *prog; | |
8547 | struct { | |
8548 | struct perf_event_header header; | |
8549 | u16 type; | |
8550 | u16 flags; | |
8551 | u32 id; | |
8552 | u8 tag[BPF_TAG_SIZE]; | |
8553 | } event_id; | |
8554 | }; | |
8555 | ||
8556 | static int perf_event_bpf_match(struct perf_event *event) | |
8557 | { | |
8558 | return event->attr.bpf_event; | |
8559 | } | |
8560 | ||
8561 | static void perf_event_bpf_output(struct perf_event *event, void *data) | |
8562 | { | |
8563 | struct perf_bpf_event *bpf_event = data; | |
8564 | struct perf_output_handle handle; | |
8565 | struct perf_sample_data sample; | |
8566 | int ret; | |
8567 | ||
8568 | if (!perf_event_bpf_match(event)) | |
8569 | return; | |
8570 | ||
8571 | perf_event_header__init_id(&bpf_event->event_id.header, | |
8572 | &sample, event); | |
8573 | ret = perf_output_begin(&handle, event, | |
8574 | bpf_event->event_id.header.size); | |
8575 | if (ret) | |
8576 | return; | |
8577 | ||
8578 | perf_output_put(&handle, bpf_event->event_id); | |
8579 | perf_event__output_id_sample(event, &handle, &sample); | |
8580 | ||
8581 | perf_output_end(&handle); | |
8582 | } | |
8583 | ||
8584 | static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog, | |
8585 | enum perf_bpf_event_type type) | |
8586 | { | |
8587 | bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD; | |
6ee52e2a SL |
8588 | int i; |
8589 | ||
8590 | if (prog->aux->func_cnt == 0) { | |
6ee52e2a SL |
8591 | perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, |
8592 | (u64)(unsigned long)prog->bpf_func, | |
bfea9a85 JO |
8593 | prog->jited_len, unregister, |
8594 | prog->aux->ksym.name); | |
6ee52e2a SL |
8595 | } else { |
8596 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
8597 | struct bpf_prog *subprog = prog->aux->func[i]; | |
8598 | ||
6ee52e2a SL |
8599 | perf_event_ksymbol( |
8600 | PERF_RECORD_KSYMBOL_TYPE_BPF, | |
8601 | (u64)(unsigned long)subprog->bpf_func, | |
bfea9a85 JO |
8602 | subprog->jited_len, unregister, |
8603 | prog->aux->ksym.name); | |
6ee52e2a SL |
8604 | } |
8605 | } | |
8606 | } | |
8607 | ||
8608 | void perf_event_bpf_event(struct bpf_prog *prog, | |
8609 | enum perf_bpf_event_type type, | |
8610 | u16 flags) | |
8611 | { | |
8612 | struct perf_bpf_event bpf_event; | |
8613 | ||
8614 | if (type <= PERF_BPF_EVENT_UNKNOWN || | |
8615 | type >= PERF_BPF_EVENT_MAX) | |
8616 | return; | |
8617 | ||
8618 | switch (type) { | |
8619 | case PERF_BPF_EVENT_PROG_LOAD: | |
8620 | case PERF_BPF_EVENT_PROG_UNLOAD: | |
8621 | if (atomic_read(&nr_ksymbol_events)) | |
8622 | perf_event_bpf_emit_ksymbols(prog, type); | |
8623 | break; | |
8624 | default: | |
8625 | break; | |
8626 | } | |
8627 | ||
8628 | if (!atomic_read(&nr_bpf_events)) | |
8629 | return; | |
8630 | ||
8631 | bpf_event = (struct perf_bpf_event){ | |
8632 | .prog = prog, | |
8633 | .event_id = { | |
8634 | .header = { | |
8635 | .type = PERF_RECORD_BPF_EVENT, | |
8636 | .size = sizeof(bpf_event.event_id), | |
8637 | }, | |
8638 | .type = type, | |
8639 | .flags = flags, | |
8640 | .id = prog->aux->id, | |
8641 | }, | |
8642 | }; | |
8643 | ||
8644 | BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64)); | |
8645 | ||
8646 | memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE); | |
8647 | perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL); | |
8648 | } | |
8649 | ||
e17d43b9 AH |
8650 | struct perf_text_poke_event { |
8651 | const void *old_bytes; | |
8652 | const void *new_bytes; | |
8653 | size_t pad; | |
8654 | u16 old_len; | |
8655 | u16 new_len; | |
8656 | ||
8657 | struct { | |
8658 | struct perf_event_header header; | |
8659 | ||
8660 | u64 addr; | |
8661 | } event_id; | |
8662 | }; | |
8663 | ||
8664 | static int perf_event_text_poke_match(struct perf_event *event) | |
8665 | { | |
8666 | return event->attr.text_poke; | |
8667 | } | |
8668 | ||
8669 | static void perf_event_text_poke_output(struct perf_event *event, void *data) | |
8670 | { | |
8671 | struct perf_text_poke_event *text_poke_event = data; | |
8672 | struct perf_output_handle handle; | |
8673 | struct perf_sample_data sample; | |
8674 | u64 padding = 0; | |
8675 | int ret; | |
8676 | ||
8677 | if (!perf_event_text_poke_match(event)) | |
8678 | return; | |
8679 | ||
8680 | perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event); | |
8681 | ||
8682 | ret = perf_output_begin(&handle, event, text_poke_event->event_id.header.size); | |
8683 | if (ret) | |
8684 | return; | |
8685 | ||
8686 | perf_output_put(&handle, text_poke_event->event_id); | |
8687 | perf_output_put(&handle, text_poke_event->old_len); | |
8688 | perf_output_put(&handle, text_poke_event->new_len); | |
8689 | ||
8690 | __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len); | |
8691 | __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len); | |
8692 | ||
8693 | if (text_poke_event->pad) | |
8694 | __output_copy(&handle, &padding, text_poke_event->pad); | |
8695 | ||
8696 | perf_event__output_id_sample(event, &handle, &sample); | |
8697 | ||
8698 | perf_output_end(&handle); | |
8699 | } | |
8700 | ||
8701 | void perf_event_text_poke(const void *addr, const void *old_bytes, | |
8702 | size_t old_len, const void *new_bytes, size_t new_len) | |
8703 | { | |
8704 | struct perf_text_poke_event text_poke_event; | |
8705 | size_t tot, pad; | |
8706 | ||
8707 | if (!atomic_read(&nr_text_poke_events)) | |
8708 | return; | |
8709 | ||
8710 | tot = sizeof(text_poke_event.old_len) + old_len; | |
8711 | tot += sizeof(text_poke_event.new_len) + new_len; | |
8712 | pad = ALIGN(tot, sizeof(u64)) - tot; | |
8713 | ||
8714 | text_poke_event = (struct perf_text_poke_event){ | |
8715 | .old_bytes = old_bytes, | |
8716 | .new_bytes = new_bytes, | |
8717 | .pad = pad, | |
8718 | .old_len = old_len, | |
8719 | .new_len = new_len, | |
8720 | .event_id = { | |
8721 | .header = { | |
8722 | .type = PERF_RECORD_TEXT_POKE, | |
8723 | .misc = PERF_RECORD_MISC_KERNEL, | |
8724 | .size = sizeof(text_poke_event.event_id) + tot + pad, | |
8725 | }, | |
8726 | .addr = (unsigned long)addr, | |
8727 | }, | |
8728 | }; | |
8729 | ||
8730 | perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL); | |
8731 | } | |
8732 | ||
8d4e6c4c AS |
8733 | void perf_event_itrace_started(struct perf_event *event) |
8734 | { | |
8735 | event->attach_state |= PERF_ATTACH_ITRACE; | |
8736 | } | |
8737 | ||
ec0d7729 AS |
8738 | static void perf_log_itrace_start(struct perf_event *event) |
8739 | { | |
8740 | struct perf_output_handle handle; | |
8741 | struct perf_sample_data sample; | |
8742 | struct perf_aux_event { | |
8743 | struct perf_event_header header; | |
8744 | u32 pid; | |
8745 | u32 tid; | |
8746 | } rec; | |
8747 | int ret; | |
8748 | ||
8749 | if (event->parent) | |
8750 | event = event->parent; | |
8751 | ||
8752 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || | |
8d4e6c4c | 8753 | event->attach_state & PERF_ATTACH_ITRACE) |
ec0d7729 AS |
8754 | return; |
8755 | ||
ec0d7729 AS |
8756 | rec.header.type = PERF_RECORD_ITRACE_START; |
8757 | rec.header.misc = 0; | |
8758 | rec.header.size = sizeof(rec); | |
8759 | rec.pid = perf_event_pid(event, current); | |
8760 | rec.tid = perf_event_tid(event, current); | |
8761 | ||
8762 | perf_event_header__init_id(&rec.header, &sample, event); | |
8763 | ret = perf_output_begin(&handle, event, rec.header.size); | |
8764 | ||
8765 | if (ret) | |
8766 | return; | |
8767 | ||
8768 | perf_output_put(&handle, rec); | |
8769 | perf_event__output_id_sample(event, &handle, &sample); | |
8770 | ||
8771 | perf_output_end(&handle); | |
8772 | } | |
8773 | ||
475113d9 JO |
8774 | static int |
8775 | __perf_event_account_interrupt(struct perf_event *event, int throttle) | |
f6c7d5fe | 8776 | { |
cdd6c482 | 8777 | struct hw_perf_event *hwc = &event->hw; |
79f14641 | 8778 | int ret = 0; |
475113d9 | 8779 | u64 seq; |
96398826 | 8780 | |
e050e3f0 SE |
8781 | seq = __this_cpu_read(perf_throttled_seq); |
8782 | if (seq != hwc->interrupts_seq) { | |
8783 | hwc->interrupts_seq = seq; | |
8784 | hwc->interrupts = 1; | |
8785 | } else { | |
8786 | hwc->interrupts++; | |
8787 | if (unlikely(throttle | |
8788 | && hwc->interrupts >= max_samples_per_tick)) { | |
8789 | __this_cpu_inc(perf_throttled_count); | |
555e0c1e | 8790 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
163ec435 PZ |
8791 | hwc->interrupts = MAX_INTERRUPTS; |
8792 | perf_log_throttle(event, 0); | |
a78ac325 PZ |
8793 | ret = 1; |
8794 | } | |
e050e3f0 | 8795 | } |
60db5e09 | 8796 | |
cdd6c482 | 8797 | if (event->attr.freq) { |
def0a9b2 | 8798 | u64 now = perf_clock(); |
abd50713 | 8799 | s64 delta = now - hwc->freq_time_stamp; |
bd2b5b12 | 8800 | |
abd50713 | 8801 | hwc->freq_time_stamp = now; |
bd2b5b12 | 8802 | |
abd50713 | 8803 | if (delta > 0 && delta < 2*TICK_NSEC) |
f39d47ff | 8804 | perf_adjust_period(event, delta, hwc->last_period, true); |
bd2b5b12 PZ |
8805 | } |
8806 | ||
475113d9 JO |
8807 | return ret; |
8808 | } | |
8809 | ||
8810 | int perf_event_account_interrupt(struct perf_event *event) | |
8811 | { | |
8812 | return __perf_event_account_interrupt(event, 1); | |
8813 | } | |
8814 | ||
8815 | /* | |
8816 | * Generic event overflow handling, sampling. | |
8817 | */ | |
8818 | ||
8819 | static int __perf_event_overflow(struct perf_event *event, | |
8820 | int throttle, struct perf_sample_data *data, | |
8821 | struct pt_regs *regs) | |
8822 | { | |
8823 | int events = atomic_read(&event->event_limit); | |
8824 | int ret = 0; | |
8825 | ||
8826 | /* | |
8827 | * Non-sampling counters might still use the PMI to fold short | |
8828 | * hardware counters, ignore those. | |
8829 | */ | |
8830 | if (unlikely(!is_sampling_event(event))) | |
8831 | return 0; | |
8832 | ||
8833 | ret = __perf_event_account_interrupt(event, throttle); | |
cc1582c2 | 8834 | |
2023b359 PZ |
8835 | /* |
8836 | * XXX event_limit might not quite work as expected on inherited | |
cdd6c482 | 8837 | * events |
2023b359 PZ |
8838 | */ |
8839 | ||
cdd6c482 IM |
8840 | event->pending_kill = POLL_IN; |
8841 | if (events && atomic_dec_and_test(&event->event_limit)) { | |
79f14641 | 8842 | ret = 1; |
cdd6c482 | 8843 | event->pending_kill = POLL_HUP; |
5aab90ce JO |
8844 | |
8845 | perf_event_disable_inatomic(event); | |
79f14641 PZ |
8846 | } |
8847 | ||
aa6a5f3c | 8848 | READ_ONCE(event->overflow_handler)(event, data, regs); |
453f19ee | 8849 | |
fed66e2c | 8850 | if (*perf_event_fasync(event) && event->pending_kill) { |
a8b0ca17 PZ |
8851 | event->pending_wakeup = 1; |
8852 | irq_work_queue(&event->pending); | |
f506b3dc PZ |
8853 | } |
8854 | ||
79f14641 | 8855 | return ret; |
f6c7d5fe PZ |
8856 | } |
8857 | ||
a8b0ca17 | 8858 | int perf_event_overflow(struct perf_event *event, |
5622f295 MM |
8859 | struct perf_sample_data *data, |
8860 | struct pt_regs *regs) | |
850bc73f | 8861 | { |
a8b0ca17 | 8862 | return __perf_event_overflow(event, 1, data, regs); |
850bc73f PZ |
8863 | } |
8864 | ||
15dbf27c | 8865 | /* |
cdd6c482 | 8866 | * Generic software event infrastructure |
15dbf27c PZ |
8867 | */ |
8868 | ||
b28ab83c PZ |
8869 | struct swevent_htable { |
8870 | struct swevent_hlist *swevent_hlist; | |
8871 | struct mutex hlist_mutex; | |
8872 | int hlist_refcount; | |
8873 | ||
8874 | /* Recursion avoidance in each contexts */ | |
8875 | int recursion[PERF_NR_CONTEXTS]; | |
8876 | }; | |
8877 | ||
8878 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); | |
8879 | ||
7b4b6658 | 8880 | /* |
cdd6c482 IM |
8881 | * We directly increment event->count and keep a second value in |
8882 | * event->hw.period_left to count intervals. This period event | |
7b4b6658 PZ |
8883 | * is kept in the range [-sample_period, 0] so that we can use the |
8884 | * sign as trigger. | |
8885 | */ | |
8886 | ||
ab573844 | 8887 | u64 perf_swevent_set_period(struct perf_event *event) |
15dbf27c | 8888 | { |
cdd6c482 | 8889 | struct hw_perf_event *hwc = &event->hw; |
7b4b6658 PZ |
8890 | u64 period = hwc->last_period; |
8891 | u64 nr, offset; | |
8892 | s64 old, val; | |
8893 | ||
8894 | hwc->last_period = hwc->sample_period; | |
15dbf27c PZ |
8895 | |
8896 | again: | |
e7850595 | 8897 | old = val = local64_read(&hwc->period_left); |
7b4b6658 PZ |
8898 | if (val < 0) |
8899 | return 0; | |
15dbf27c | 8900 | |
7b4b6658 PZ |
8901 | nr = div64_u64(period + val, period); |
8902 | offset = nr * period; | |
8903 | val -= offset; | |
e7850595 | 8904 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
7b4b6658 | 8905 | goto again; |
15dbf27c | 8906 | |
7b4b6658 | 8907 | return nr; |
15dbf27c PZ |
8908 | } |
8909 | ||
0cff784a | 8910 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
a8b0ca17 | 8911 | struct perf_sample_data *data, |
5622f295 | 8912 | struct pt_regs *regs) |
15dbf27c | 8913 | { |
cdd6c482 | 8914 | struct hw_perf_event *hwc = &event->hw; |
850bc73f | 8915 | int throttle = 0; |
15dbf27c | 8916 | |
0cff784a PZ |
8917 | if (!overflow) |
8918 | overflow = perf_swevent_set_period(event); | |
15dbf27c | 8919 | |
7b4b6658 PZ |
8920 | if (hwc->interrupts == MAX_INTERRUPTS) |
8921 | return; | |
15dbf27c | 8922 | |
7b4b6658 | 8923 | for (; overflow; overflow--) { |
a8b0ca17 | 8924 | if (__perf_event_overflow(event, throttle, |
5622f295 | 8925 | data, regs)) { |
7b4b6658 PZ |
8926 | /* |
8927 | * We inhibit the overflow from happening when | |
8928 | * hwc->interrupts == MAX_INTERRUPTS. | |
8929 | */ | |
8930 | break; | |
8931 | } | |
cf450a73 | 8932 | throttle = 1; |
7b4b6658 | 8933 | } |
15dbf27c PZ |
8934 | } |
8935 | ||
a4eaf7f1 | 8936 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
a8b0ca17 | 8937 | struct perf_sample_data *data, |
5622f295 | 8938 | struct pt_regs *regs) |
7b4b6658 | 8939 | { |
cdd6c482 | 8940 | struct hw_perf_event *hwc = &event->hw; |
d6d020e9 | 8941 | |
e7850595 | 8942 | local64_add(nr, &event->count); |
d6d020e9 | 8943 | |
0cff784a PZ |
8944 | if (!regs) |
8945 | return; | |
8946 | ||
6c7e550f | 8947 | if (!is_sampling_event(event)) |
7b4b6658 | 8948 | return; |
d6d020e9 | 8949 | |
5d81e5cf AV |
8950 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
8951 | data->period = nr; | |
8952 | return perf_swevent_overflow(event, 1, data, regs); | |
8953 | } else | |
8954 | data->period = event->hw.last_period; | |
8955 | ||
0cff784a | 8956 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
a8b0ca17 | 8957 | return perf_swevent_overflow(event, 1, data, regs); |
0cff784a | 8958 | |
e7850595 | 8959 | if (local64_add_negative(nr, &hwc->period_left)) |
7b4b6658 | 8960 | return; |
df1a132b | 8961 | |
a8b0ca17 | 8962 | perf_swevent_overflow(event, 0, data, regs); |
d6d020e9 PZ |
8963 | } |
8964 | ||
f5ffe02e FW |
8965 | static int perf_exclude_event(struct perf_event *event, |
8966 | struct pt_regs *regs) | |
8967 | { | |
a4eaf7f1 | 8968 | if (event->hw.state & PERF_HES_STOPPED) |
91b2f482 | 8969 | return 1; |
a4eaf7f1 | 8970 | |
f5ffe02e FW |
8971 | if (regs) { |
8972 | if (event->attr.exclude_user && user_mode(regs)) | |
8973 | return 1; | |
8974 | ||
8975 | if (event->attr.exclude_kernel && !user_mode(regs)) | |
8976 | return 1; | |
8977 | } | |
8978 | ||
8979 | return 0; | |
8980 | } | |
8981 | ||
cdd6c482 | 8982 | static int perf_swevent_match(struct perf_event *event, |
1c432d89 | 8983 | enum perf_type_id type, |
6fb2915d LZ |
8984 | u32 event_id, |
8985 | struct perf_sample_data *data, | |
8986 | struct pt_regs *regs) | |
15dbf27c | 8987 | { |
cdd6c482 | 8988 | if (event->attr.type != type) |
a21ca2ca | 8989 | return 0; |
f5ffe02e | 8990 | |
cdd6c482 | 8991 | if (event->attr.config != event_id) |
15dbf27c PZ |
8992 | return 0; |
8993 | ||
f5ffe02e FW |
8994 | if (perf_exclude_event(event, regs)) |
8995 | return 0; | |
15dbf27c PZ |
8996 | |
8997 | return 1; | |
8998 | } | |
8999 | ||
76e1d904 FW |
9000 | static inline u64 swevent_hash(u64 type, u32 event_id) |
9001 | { | |
9002 | u64 val = event_id | (type << 32); | |
9003 | ||
9004 | return hash_64(val, SWEVENT_HLIST_BITS); | |
9005 | } | |
9006 | ||
49f135ed FW |
9007 | static inline struct hlist_head * |
9008 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) | |
76e1d904 | 9009 | { |
49f135ed FW |
9010 | u64 hash = swevent_hash(type, event_id); |
9011 | ||
9012 | return &hlist->heads[hash]; | |
9013 | } | |
76e1d904 | 9014 | |
49f135ed FW |
9015 | /* For the read side: events when they trigger */ |
9016 | static inline struct hlist_head * | |
b28ab83c | 9017 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
49f135ed FW |
9018 | { |
9019 | struct swevent_hlist *hlist; | |
76e1d904 | 9020 | |
b28ab83c | 9021 | hlist = rcu_dereference(swhash->swevent_hlist); |
76e1d904 FW |
9022 | if (!hlist) |
9023 | return NULL; | |
9024 | ||
49f135ed FW |
9025 | return __find_swevent_head(hlist, type, event_id); |
9026 | } | |
9027 | ||
9028 | /* For the event head insertion and removal in the hlist */ | |
9029 | static inline struct hlist_head * | |
b28ab83c | 9030 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
49f135ed FW |
9031 | { |
9032 | struct swevent_hlist *hlist; | |
9033 | u32 event_id = event->attr.config; | |
9034 | u64 type = event->attr.type; | |
9035 | ||
9036 | /* | |
9037 | * Event scheduling is always serialized against hlist allocation | |
9038 | * and release. Which makes the protected version suitable here. | |
9039 | * The context lock guarantees that. | |
9040 | */ | |
b28ab83c | 9041 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
49f135ed FW |
9042 | lockdep_is_held(&event->ctx->lock)); |
9043 | if (!hlist) | |
9044 | return NULL; | |
9045 | ||
9046 | return __find_swevent_head(hlist, type, event_id); | |
76e1d904 FW |
9047 | } |
9048 | ||
9049 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, | |
a8b0ca17 | 9050 | u64 nr, |
76e1d904 FW |
9051 | struct perf_sample_data *data, |
9052 | struct pt_regs *regs) | |
15dbf27c | 9053 | { |
4a32fea9 | 9054 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9055 | struct perf_event *event; |
76e1d904 | 9056 | struct hlist_head *head; |
15dbf27c | 9057 | |
76e1d904 | 9058 | rcu_read_lock(); |
b28ab83c | 9059 | head = find_swevent_head_rcu(swhash, type, event_id); |
76e1d904 FW |
9060 | if (!head) |
9061 | goto end; | |
9062 | ||
b67bfe0d | 9063 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
6fb2915d | 9064 | if (perf_swevent_match(event, type, event_id, data, regs)) |
a8b0ca17 | 9065 | perf_swevent_event(event, nr, data, regs); |
15dbf27c | 9066 | } |
76e1d904 FW |
9067 | end: |
9068 | rcu_read_unlock(); | |
15dbf27c PZ |
9069 | } |
9070 | ||
86038c5e PZI |
9071 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
9072 | ||
4ed7c92d | 9073 | int perf_swevent_get_recursion_context(void) |
96f6d444 | 9074 | { |
4a32fea9 | 9075 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
96f6d444 | 9076 | |
b28ab83c | 9077 | return get_recursion_context(swhash->recursion); |
96f6d444 | 9078 | } |
645e8cc0 | 9079 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
96f6d444 | 9080 | |
98b5c2c6 | 9081 | void perf_swevent_put_recursion_context(int rctx) |
15dbf27c | 9082 | { |
4a32fea9 | 9083 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
927c7a9e | 9084 | |
b28ab83c | 9085 | put_recursion_context(swhash->recursion, rctx); |
ce71b9df | 9086 | } |
15dbf27c | 9087 | |
86038c5e | 9088 | void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
b8e83514 | 9089 | { |
a4234bfc | 9090 | struct perf_sample_data data; |
4ed7c92d | 9091 | |
86038c5e | 9092 | if (WARN_ON_ONCE(!regs)) |
4ed7c92d | 9093 | return; |
a4234bfc | 9094 | |
fd0d000b | 9095 | perf_sample_data_init(&data, addr, 0); |
a8b0ca17 | 9096 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
86038c5e PZI |
9097 | } |
9098 | ||
9099 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) | |
9100 | { | |
9101 | int rctx; | |
9102 | ||
9103 | preempt_disable_notrace(); | |
9104 | rctx = perf_swevent_get_recursion_context(); | |
9105 | if (unlikely(rctx < 0)) | |
9106 | goto fail; | |
9107 | ||
9108 | ___perf_sw_event(event_id, nr, regs, addr); | |
4ed7c92d PZ |
9109 | |
9110 | perf_swevent_put_recursion_context(rctx); | |
86038c5e | 9111 | fail: |
1c024eca | 9112 | preempt_enable_notrace(); |
b8e83514 PZ |
9113 | } |
9114 | ||
cdd6c482 | 9115 | static void perf_swevent_read(struct perf_event *event) |
15dbf27c | 9116 | { |
15dbf27c PZ |
9117 | } |
9118 | ||
a4eaf7f1 | 9119 | static int perf_swevent_add(struct perf_event *event, int flags) |
15dbf27c | 9120 | { |
4a32fea9 | 9121 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
cdd6c482 | 9122 | struct hw_perf_event *hwc = &event->hw; |
76e1d904 FW |
9123 | struct hlist_head *head; |
9124 | ||
6c7e550f | 9125 | if (is_sampling_event(event)) { |
7b4b6658 | 9126 | hwc->last_period = hwc->sample_period; |
cdd6c482 | 9127 | perf_swevent_set_period(event); |
7b4b6658 | 9128 | } |
76e1d904 | 9129 | |
a4eaf7f1 PZ |
9130 | hwc->state = !(flags & PERF_EF_START); |
9131 | ||
b28ab83c | 9132 | head = find_swevent_head(swhash, event); |
12ca6ad2 | 9133 | if (WARN_ON_ONCE(!head)) |
76e1d904 FW |
9134 | return -EINVAL; |
9135 | ||
9136 | hlist_add_head_rcu(&event->hlist_entry, head); | |
6a694a60 | 9137 | perf_event_update_userpage(event); |
76e1d904 | 9138 | |
15dbf27c PZ |
9139 | return 0; |
9140 | } | |
9141 | ||
a4eaf7f1 | 9142 | static void perf_swevent_del(struct perf_event *event, int flags) |
15dbf27c | 9143 | { |
76e1d904 | 9144 | hlist_del_rcu(&event->hlist_entry); |
15dbf27c PZ |
9145 | } |
9146 | ||
a4eaf7f1 | 9147 | static void perf_swevent_start(struct perf_event *event, int flags) |
5c92d124 | 9148 | { |
a4eaf7f1 | 9149 | event->hw.state = 0; |
d6d020e9 | 9150 | } |
aa9c4c0f | 9151 | |
a4eaf7f1 | 9152 | static void perf_swevent_stop(struct perf_event *event, int flags) |
d6d020e9 | 9153 | { |
a4eaf7f1 | 9154 | event->hw.state = PERF_HES_STOPPED; |
bae43c99 IM |
9155 | } |
9156 | ||
49f135ed FW |
9157 | /* Deref the hlist from the update side */ |
9158 | static inline struct swevent_hlist * | |
b28ab83c | 9159 | swevent_hlist_deref(struct swevent_htable *swhash) |
49f135ed | 9160 | { |
b28ab83c PZ |
9161 | return rcu_dereference_protected(swhash->swevent_hlist, |
9162 | lockdep_is_held(&swhash->hlist_mutex)); | |
49f135ed FW |
9163 | } |
9164 | ||
b28ab83c | 9165 | static void swevent_hlist_release(struct swevent_htable *swhash) |
76e1d904 | 9166 | { |
b28ab83c | 9167 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
76e1d904 | 9168 | |
49f135ed | 9169 | if (!hlist) |
76e1d904 FW |
9170 | return; |
9171 | ||
70691d4a | 9172 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
fa4bbc4c | 9173 | kfree_rcu(hlist, rcu_head); |
76e1d904 FW |
9174 | } |
9175 | ||
3b364d7b | 9176 | static void swevent_hlist_put_cpu(int cpu) |
76e1d904 | 9177 | { |
b28ab83c | 9178 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 | 9179 | |
b28ab83c | 9180 | mutex_lock(&swhash->hlist_mutex); |
76e1d904 | 9181 | |
b28ab83c PZ |
9182 | if (!--swhash->hlist_refcount) |
9183 | swevent_hlist_release(swhash); | |
76e1d904 | 9184 | |
b28ab83c | 9185 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
9186 | } |
9187 | ||
3b364d7b | 9188 | static void swevent_hlist_put(void) |
76e1d904 FW |
9189 | { |
9190 | int cpu; | |
9191 | ||
76e1d904 | 9192 | for_each_possible_cpu(cpu) |
3b364d7b | 9193 | swevent_hlist_put_cpu(cpu); |
76e1d904 FW |
9194 | } |
9195 | ||
3b364d7b | 9196 | static int swevent_hlist_get_cpu(int cpu) |
76e1d904 | 9197 | { |
b28ab83c | 9198 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
76e1d904 FW |
9199 | int err = 0; |
9200 | ||
b28ab83c | 9201 | mutex_lock(&swhash->hlist_mutex); |
a63fbed7 TG |
9202 | if (!swevent_hlist_deref(swhash) && |
9203 | cpumask_test_cpu(cpu, perf_online_mask)) { | |
76e1d904 FW |
9204 | struct swevent_hlist *hlist; |
9205 | ||
9206 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); | |
9207 | if (!hlist) { | |
9208 | err = -ENOMEM; | |
9209 | goto exit; | |
9210 | } | |
b28ab83c | 9211 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
76e1d904 | 9212 | } |
b28ab83c | 9213 | swhash->hlist_refcount++; |
9ed6060d | 9214 | exit: |
b28ab83c | 9215 | mutex_unlock(&swhash->hlist_mutex); |
76e1d904 FW |
9216 | |
9217 | return err; | |
9218 | } | |
9219 | ||
3b364d7b | 9220 | static int swevent_hlist_get(void) |
76e1d904 | 9221 | { |
3b364d7b | 9222 | int err, cpu, failed_cpu; |
76e1d904 | 9223 | |
a63fbed7 | 9224 | mutex_lock(&pmus_lock); |
76e1d904 | 9225 | for_each_possible_cpu(cpu) { |
3b364d7b | 9226 | err = swevent_hlist_get_cpu(cpu); |
76e1d904 FW |
9227 | if (err) { |
9228 | failed_cpu = cpu; | |
9229 | goto fail; | |
9230 | } | |
9231 | } | |
a63fbed7 | 9232 | mutex_unlock(&pmus_lock); |
76e1d904 | 9233 | return 0; |
9ed6060d | 9234 | fail: |
76e1d904 FW |
9235 | for_each_possible_cpu(cpu) { |
9236 | if (cpu == failed_cpu) | |
9237 | break; | |
3b364d7b | 9238 | swevent_hlist_put_cpu(cpu); |
76e1d904 | 9239 | } |
a63fbed7 | 9240 | mutex_unlock(&pmus_lock); |
76e1d904 FW |
9241 | return err; |
9242 | } | |
9243 | ||
c5905afb | 9244 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
95476b64 | 9245 | |
b0a873eb PZ |
9246 | static void sw_perf_event_destroy(struct perf_event *event) |
9247 | { | |
9248 | u64 event_id = event->attr.config; | |
95476b64 | 9249 | |
b0a873eb PZ |
9250 | WARN_ON(event->parent); |
9251 | ||
c5905afb | 9252 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
3b364d7b | 9253 | swevent_hlist_put(); |
b0a873eb PZ |
9254 | } |
9255 | ||
9256 | static int perf_swevent_init(struct perf_event *event) | |
9257 | { | |
8176cced | 9258 | u64 event_id = event->attr.config; |
b0a873eb PZ |
9259 | |
9260 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
9261 | return -ENOENT; | |
9262 | ||
2481c5fa SE |
9263 | /* |
9264 | * no branch sampling for software events | |
9265 | */ | |
9266 | if (has_branch_stack(event)) | |
9267 | return -EOPNOTSUPP; | |
9268 | ||
b0a873eb PZ |
9269 | switch (event_id) { |
9270 | case PERF_COUNT_SW_CPU_CLOCK: | |
9271 | case PERF_COUNT_SW_TASK_CLOCK: | |
9272 | return -ENOENT; | |
9273 | ||
9274 | default: | |
9275 | break; | |
9276 | } | |
9277 | ||
ce677831 | 9278 | if (event_id >= PERF_COUNT_SW_MAX) |
b0a873eb PZ |
9279 | return -ENOENT; |
9280 | ||
9281 | if (!event->parent) { | |
9282 | int err; | |
9283 | ||
3b364d7b | 9284 | err = swevent_hlist_get(); |
b0a873eb PZ |
9285 | if (err) |
9286 | return err; | |
9287 | ||
c5905afb | 9288 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
b0a873eb PZ |
9289 | event->destroy = sw_perf_event_destroy; |
9290 | } | |
9291 | ||
9292 | return 0; | |
9293 | } | |
9294 | ||
9295 | static struct pmu perf_swevent = { | |
89a1e187 | 9296 | .task_ctx_nr = perf_sw_context, |
95476b64 | 9297 | |
34f43927 PZ |
9298 | .capabilities = PERF_PMU_CAP_NO_NMI, |
9299 | ||
b0a873eb | 9300 | .event_init = perf_swevent_init, |
a4eaf7f1 PZ |
9301 | .add = perf_swevent_add, |
9302 | .del = perf_swevent_del, | |
9303 | .start = perf_swevent_start, | |
9304 | .stop = perf_swevent_stop, | |
1c024eca | 9305 | .read = perf_swevent_read, |
1c024eca PZ |
9306 | }; |
9307 | ||
b0a873eb PZ |
9308 | #ifdef CONFIG_EVENT_TRACING |
9309 | ||
1c024eca PZ |
9310 | static int perf_tp_filter_match(struct perf_event *event, |
9311 | struct perf_sample_data *data) | |
9312 | { | |
7e3f977e | 9313 | void *record = data->raw->frag.data; |
1c024eca | 9314 | |
b71b437e PZ |
9315 | /* only top level events have filters set */ |
9316 | if (event->parent) | |
9317 | event = event->parent; | |
9318 | ||
1c024eca PZ |
9319 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
9320 | return 1; | |
9321 | return 0; | |
9322 | } | |
9323 | ||
9324 | static int perf_tp_event_match(struct perf_event *event, | |
9325 | struct perf_sample_data *data, | |
9326 | struct pt_regs *regs) | |
9327 | { | |
a0f7d0f7 FW |
9328 | if (event->hw.state & PERF_HES_STOPPED) |
9329 | return 0; | |
580d607c | 9330 | /* |
9fd2e48b | 9331 | * If exclude_kernel, only trace user-space tracepoints (uprobes) |
580d607c | 9332 | */ |
9fd2e48b | 9333 | if (event->attr.exclude_kernel && !user_mode(regs)) |
1c024eca PZ |
9334 | return 0; |
9335 | ||
9336 | if (!perf_tp_filter_match(event, data)) | |
9337 | return 0; | |
9338 | ||
9339 | return 1; | |
9340 | } | |
9341 | ||
85b67bcb AS |
9342 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
9343 | struct trace_event_call *call, u64 count, | |
9344 | struct pt_regs *regs, struct hlist_head *head, | |
9345 | struct task_struct *task) | |
9346 | { | |
e87c6bc3 | 9347 | if (bpf_prog_array_valid(call)) { |
85b67bcb | 9348 | *(struct pt_regs **)raw_data = regs; |
e87c6bc3 | 9349 | if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { |
85b67bcb AS |
9350 | perf_swevent_put_recursion_context(rctx); |
9351 | return; | |
9352 | } | |
9353 | } | |
9354 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, | |
8fd0fbbe | 9355 | rctx, task); |
85b67bcb AS |
9356 | } |
9357 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); | |
9358 | ||
1e1dcd93 | 9359 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
e6dab5ff | 9360 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
8fd0fbbe | 9361 | struct task_struct *task) |
95476b64 FW |
9362 | { |
9363 | struct perf_sample_data data; | |
8fd0fbbe | 9364 | struct perf_event *event; |
1c024eca | 9365 | |
95476b64 | 9366 | struct perf_raw_record raw = { |
7e3f977e DB |
9367 | .frag = { |
9368 | .size = entry_size, | |
9369 | .data = record, | |
9370 | }, | |
95476b64 FW |
9371 | }; |
9372 | ||
1e1dcd93 | 9373 | perf_sample_data_init(&data, 0, 0); |
95476b64 FW |
9374 | data.raw = &raw; |
9375 | ||
1e1dcd93 AS |
9376 | perf_trace_buf_update(record, event_type); |
9377 | ||
8fd0fbbe | 9378 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
1c024eca | 9379 | if (perf_tp_event_match(event, &data, regs)) |
a8b0ca17 | 9380 | perf_swevent_event(event, count, &data, regs); |
4f41c013 | 9381 | } |
ecc55f84 | 9382 | |
e6dab5ff AV |
9383 | /* |
9384 | * If we got specified a target task, also iterate its context and | |
9385 | * deliver this event there too. | |
9386 | */ | |
9387 | if (task && task != current) { | |
9388 | struct perf_event_context *ctx; | |
9389 | struct trace_entry *entry = record; | |
9390 | ||
9391 | rcu_read_lock(); | |
9392 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); | |
9393 | if (!ctx) | |
9394 | goto unlock; | |
9395 | ||
9396 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { | |
cd6fb677 JO |
9397 | if (event->cpu != smp_processor_id()) |
9398 | continue; | |
e6dab5ff AV |
9399 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
9400 | continue; | |
9401 | if (event->attr.config != entry->type) | |
9402 | continue; | |
9403 | if (perf_tp_event_match(event, &data, regs)) | |
9404 | perf_swevent_event(event, count, &data, regs); | |
9405 | } | |
9406 | unlock: | |
9407 | rcu_read_unlock(); | |
9408 | } | |
9409 | ||
ecc55f84 | 9410 | perf_swevent_put_recursion_context(rctx); |
95476b64 FW |
9411 | } |
9412 | EXPORT_SYMBOL_GPL(perf_tp_event); | |
9413 | ||
cdd6c482 | 9414 | static void tp_perf_event_destroy(struct perf_event *event) |
e077df4f | 9415 | { |
1c024eca | 9416 | perf_trace_destroy(event); |
e077df4f PZ |
9417 | } |
9418 | ||
b0a873eb | 9419 | static int perf_tp_event_init(struct perf_event *event) |
e077df4f | 9420 | { |
76e1d904 FW |
9421 | int err; |
9422 | ||
b0a873eb PZ |
9423 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
9424 | return -ENOENT; | |
9425 | ||
2481c5fa SE |
9426 | /* |
9427 | * no branch sampling for tracepoint events | |
9428 | */ | |
9429 | if (has_branch_stack(event)) | |
9430 | return -EOPNOTSUPP; | |
9431 | ||
1c024eca PZ |
9432 | err = perf_trace_init(event); |
9433 | if (err) | |
b0a873eb | 9434 | return err; |
e077df4f | 9435 | |
cdd6c482 | 9436 | event->destroy = tp_perf_event_destroy; |
e077df4f | 9437 | |
b0a873eb PZ |
9438 | return 0; |
9439 | } | |
9440 | ||
9441 | static struct pmu perf_tracepoint = { | |
89a1e187 PZ |
9442 | .task_ctx_nr = perf_sw_context, |
9443 | ||
b0a873eb | 9444 | .event_init = perf_tp_event_init, |
a4eaf7f1 PZ |
9445 | .add = perf_trace_add, |
9446 | .del = perf_trace_del, | |
9447 | .start = perf_swevent_start, | |
9448 | .stop = perf_swevent_stop, | |
b0a873eb | 9449 | .read = perf_swevent_read, |
b0a873eb PZ |
9450 | }; |
9451 | ||
33ea4b24 | 9452 | #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) |
e12f03d7 SL |
9453 | /* |
9454 | * Flags in config, used by dynamic PMU kprobe and uprobe | |
9455 | * The flags should match following PMU_FORMAT_ATTR(). | |
9456 | * | |
9457 | * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe | |
9458 | * if not set, create kprobe/uprobe | |
a6ca88b2 SL |
9459 | * |
9460 | * The following values specify a reference counter (or semaphore in the | |
9461 | * terminology of tools like dtrace, systemtap, etc.) Userspace Statically | |
9462 | * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset. | |
9463 | * | |
9464 | * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset | |
9465 | * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left | |
e12f03d7 SL |
9466 | */ |
9467 | enum perf_probe_config { | |
9468 | PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */ | |
a6ca88b2 SL |
9469 | PERF_UPROBE_REF_CTR_OFFSET_BITS = 32, |
9470 | PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS, | |
e12f03d7 SL |
9471 | }; |
9472 | ||
9473 | PMU_FORMAT_ATTR(retprobe, "config:0"); | |
a6ca88b2 | 9474 | #endif |
e12f03d7 | 9475 | |
a6ca88b2 SL |
9476 | #ifdef CONFIG_KPROBE_EVENTS |
9477 | static struct attribute *kprobe_attrs[] = { | |
e12f03d7 SL |
9478 | &format_attr_retprobe.attr, |
9479 | NULL, | |
9480 | }; | |
9481 | ||
a6ca88b2 | 9482 | static struct attribute_group kprobe_format_group = { |
e12f03d7 | 9483 | .name = "format", |
a6ca88b2 | 9484 | .attrs = kprobe_attrs, |
e12f03d7 SL |
9485 | }; |
9486 | ||
a6ca88b2 SL |
9487 | static const struct attribute_group *kprobe_attr_groups[] = { |
9488 | &kprobe_format_group, | |
e12f03d7 SL |
9489 | NULL, |
9490 | }; | |
9491 | ||
9492 | static int perf_kprobe_event_init(struct perf_event *event); | |
9493 | static struct pmu perf_kprobe = { | |
9494 | .task_ctx_nr = perf_sw_context, | |
9495 | .event_init = perf_kprobe_event_init, | |
9496 | .add = perf_trace_add, | |
9497 | .del = perf_trace_del, | |
9498 | .start = perf_swevent_start, | |
9499 | .stop = perf_swevent_stop, | |
9500 | .read = perf_swevent_read, | |
a6ca88b2 | 9501 | .attr_groups = kprobe_attr_groups, |
e12f03d7 SL |
9502 | }; |
9503 | ||
9504 | static int perf_kprobe_event_init(struct perf_event *event) | |
9505 | { | |
9506 | int err; | |
9507 | bool is_retprobe; | |
9508 | ||
9509 | if (event->attr.type != perf_kprobe.type) | |
9510 | return -ENOENT; | |
32e6e967 | 9511 | |
c9e0924e | 9512 | if (!perfmon_capable()) |
32e6e967 SL |
9513 | return -EACCES; |
9514 | ||
e12f03d7 SL |
9515 | /* |
9516 | * no branch sampling for probe events | |
9517 | */ | |
9518 | if (has_branch_stack(event)) | |
9519 | return -EOPNOTSUPP; | |
9520 | ||
9521 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
9522 | err = perf_kprobe_init(event, is_retprobe); | |
9523 | if (err) | |
9524 | return err; | |
9525 | ||
9526 | event->destroy = perf_kprobe_destroy; | |
9527 | ||
9528 | return 0; | |
9529 | } | |
9530 | #endif /* CONFIG_KPROBE_EVENTS */ | |
9531 | ||
33ea4b24 | 9532 | #ifdef CONFIG_UPROBE_EVENTS |
a6ca88b2 SL |
9533 | PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63"); |
9534 | ||
9535 | static struct attribute *uprobe_attrs[] = { | |
9536 | &format_attr_retprobe.attr, | |
9537 | &format_attr_ref_ctr_offset.attr, | |
9538 | NULL, | |
9539 | }; | |
9540 | ||
9541 | static struct attribute_group uprobe_format_group = { | |
9542 | .name = "format", | |
9543 | .attrs = uprobe_attrs, | |
9544 | }; | |
9545 | ||
9546 | static const struct attribute_group *uprobe_attr_groups[] = { | |
9547 | &uprobe_format_group, | |
9548 | NULL, | |
9549 | }; | |
9550 | ||
33ea4b24 SL |
9551 | static int perf_uprobe_event_init(struct perf_event *event); |
9552 | static struct pmu perf_uprobe = { | |
9553 | .task_ctx_nr = perf_sw_context, | |
9554 | .event_init = perf_uprobe_event_init, | |
9555 | .add = perf_trace_add, | |
9556 | .del = perf_trace_del, | |
9557 | .start = perf_swevent_start, | |
9558 | .stop = perf_swevent_stop, | |
9559 | .read = perf_swevent_read, | |
a6ca88b2 | 9560 | .attr_groups = uprobe_attr_groups, |
33ea4b24 SL |
9561 | }; |
9562 | ||
9563 | static int perf_uprobe_event_init(struct perf_event *event) | |
9564 | { | |
9565 | int err; | |
a6ca88b2 | 9566 | unsigned long ref_ctr_offset; |
33ea4b24 SL |
9567 | bool is_retprobe; |
9568 | ||
9569 | if (event->attr.type != perf_uprobe.type) | |
9570 | return -ENOENT; | |
32e6e967 | 9571 | |
c9e0924e | 9572 | if (!perfmon_capable()) |
32e6e967 SL |
9573 | return -EACCES; |
9574 | ||
33ea4b24 SL |
9575 | /* |
9576 | * no branch sampling for probe events | |
9577 | */ | |
9578 | if (has_branch_stack(event)) | |
9579 | return -EOPNOTSUPP; | |
9580 | ||
9581 | is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; | |
a6ca88b2 SL |
9582 | ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
9583 | err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe); | |
33ea4b24 SL |
9584 | if (err) |
9585 | return err; | |
9586 | ||
9587 | event->destroy = perf_uprobe_destroy; | |
9588 | ||
9589 | return 0; | |
9590 | } | |
9591 | #endif /* CONFIG_UPROBE_EVENTS */ | |
9592 | ||
b0a873eb PZ |
9593 | static inline void perf_tp_register(void) |
9594 | { | |
2e80a82a | 9595 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
e12f03d7 SL |
9596 | #ifdef CONFIG_KPROBE_EVENTS |
9597 | perf_pmu_register(&perf_kprobe, "kprobe", -1); | |
9598 | #endif | |
33ea4b24 SL |
9599 | #ifdef CONFIG_UPROBE_EVENTS |
9600 | perf_pmu_register(&perf_uprobe, "uprobe", -1); | |
9601 | #endif | |
e077df4f | 9602 | } |
6fb2915d | 9603 | |
6fb2915d LZ |
9604 | static void perf_event_free_filter(struct perf_event *event) |
9605 | { | |
9606 | ftrace_profile_free_filter(event); | |
9607 | } | |
9608 | ||
aa6a5f3c AS |
9609 | #ifdef CONFIG_BPF_SYSCALL |
9610 | static void bpf_overflow_handler(struct perf_event *event, | |
9611 | struct perf_sample_data *data, | |
9612 | struct pt_regs *regs) | |
9613 | { | |
9614 | struct bpf_perf_event_data_kern ctx = { | |
9615 | .data = data, | |
7d9285e8 | 9616 | .event = event, |
aa6a5f3c AS |
9617 | }; |
9618 | int ret = 0; | |
9619 | ||
c895f6f7 | 9620 | ctx.regs = perf_arch_bpf_user_pt_regs(regs); |
aa6a5f3c AS |
9621 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) |
9622 | goto out; | |
9623 | rcu_read_lock(); | |
88575199 | 9624 | ret = BPF_PROG_RUN(event->prog, &ctx); |
aa6a5f3c AS |
9625 | rcu_read_unlock(); |
9626 | out: | |
9627 | __this_cpu_dec(bpf_prog_active); | |
aa6a5f3c AS |
9628 | if (!ret) |
9629 | return; | |
9630 | ||
9631 | event->orig_overflow_handler(event, data, regs); | |
9632 | } | |
9633 | ||
9634 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
9635 | { | |
9636 | struct bpf_prog *prog; | |
9637 | ||
9638 | if (event->overflow_handler_context) | |
9639 | /* hw breakpoint or kernel counter */ | |
9640 | return -EINVAL; | |
9641 | ||
9642 | if (event->prog) | |
9643 | return -EEXIST; | |
9644 | ||
9645 | prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); | |
9646 | if (IS_ERR(prog)) | |
9647 | return PTR_ERR(prog); | |
9648 | ||
9649 | event->prog = prog; | |
9650 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); | |
9651 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); | |
9652 | return 0; | |
9653 | } | |
9654 | ||
9655 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
9656 | { | |
9657 | struct bpf_prog *prog = event->prog; | |
9658 | ||
9659 | if (!prog) | |
9660 | return; | |
9661 | ||
9662 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); | |
9663 | event->prog = NULL; | |
9664 | bpf_prog_put(prog); | |
9665 | } | |
9666 | #else | |
9667 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) | |
9668 | { | |
9669 | return -EOPNOTSUPP; | |
9670 | } | |
9671 | static void perf_event_free_bpf_handler(struct perf_event *event) | |
9672 | { | |
9673 | } | |
9674 | #endif | |
9675 | ||
e12f03d7 SL |
9676 | /* |
9677 | * returns true if the event is a tracepoint, or a kprobe/upprobe created | |
9678 | * with perf_event_open() | |
9679 | */ | |
9680 | static inline bool perf_event_is_tracing(struct perf_event *event) | |
9681 | { | |
9682 | if (event->pmu == &perf_tracepoint) | |
9683 | return true; | |
9684 | #ifdef CONFIG_KPROBE_EVENTS | |
9685 | if (event->pmu == &perf_kprobe) | |
9686 | return true; | |
33ea4b24 SL |
9687 | #endif |
9688 | #ifdef CONFIG_UPROBE_EVENTS | |
9689 | if (event->pmu == &perf_uprobe) | |
9690 | return true; | |
e12f03d7 SL |
9691 | #endif |
9692 | return false; | |
9693 | } | |
9694 | ||
2541517c AS |
9695 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
9696 | { | |
cf5f5cea | 9697 | bool is_kprobe, is_tracepoint, is_syscall_tp; |
2541517c | 9698 | struct bpf_prog *prog; |
e87c6bc3 | 9699 | int ret; |
2541517c | 9700 | |
e12f03d7 | 9701 | if (!perf_event_is_tracing(event)) |
f91840a3 | 9702 | return perf_event_set_bpf_handler(event, prog_fd); |
2541517c | 9703 | |
98b5c2c6 AS |
9704 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; |
9705 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; | |
cf5f5cea YS |
9706 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
9707 | if (!is_kprobe && !is_tracepoint && !is_syscall_tp) | |
98b5c2c6 | 9708 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
2541517c AS |
9709 | return -EINVAL; |
9710 | ||
9711 | prog = bpf_prog_get(prog_fd); | |
9712 | if (IS_ERR(prog)) | |
9713 | return PTR_ERR(prog); | |
9714 | ||
98b5c2c6 | 9715 | if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || |
cf5f5cea YS |
9716 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || |
9717 | (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { | |
2541517c AS |
9718 | /* valid fd, but invalid bpf program type */ |
9719 | bpf_prog_put(prog); | |
9720 | return -EINVAL; | |
9721 | } | |
9722 | ||
9802d865 JB |
9723 | /* Kprobe override only works for kprobes, not uprobes. */ |
9724 | if (prog->kprobe_override && | |
9725 | !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) { | |
9726 | bpf_prog_put(prog); | |
9727 | return -EINVAL; | |
9728 | } | |
9729 | ||
cf5f5cea | 9730 | if (is_tracepoint || is_syscall_tp) { |
32bbe007 AS |
9731 | int off = trace_event_get_offsets(event->tp_event); |
9732 | ||
9733 | if (prog->aux->max_ctx_offset > off) { | |
9734 | bpf_prog_put(prog); | |
9735 | return -EACCES; | |
9736 | } | |
9737 | } | |
2541517c | 9738 | |
e87c6bc3 YS |
9739 | ret = perf_event_attach_bpf_prog(event, prog); |
9740 | if (ret) | |
9741 | bpf_prog_put(prog); | |
9742 | return ret; | |
2541517c AS |
9743 | } |
9744 | ||
9745 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
9746 | { | |
e12f03d7 | 9747 | if (!perf_event_is_tracing(event)) { |
0b4c6841 | 9748 | perf_event_free_bpf_handler(event); |
2541517c | 9749 | return; |
2541517c | 9750 | } |
e87c6bc3 | 9751 | perf_event_detach_bpf_prog(event); |
2541517c AS |
9752 | } |
9753 | ||
e077df4f | 9754 | #else |
6fb2915d | 9755 | |
b0a873eb | 9756 | static inline void perf_tp_register(void) |
e077df4f | 9757 | { |
e077df4f | 9758 | } |
6fb2915d | 9759 | |
6fb2915d LZ |
9760 | static void perf_event_free_filter(struct perf_event *event) |
9761 | { | |
9762 | } | |
9763 | ||
2541517c AS |
9764 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
9765 | { | |
9766 | return -ENOENT; | |
9767 | } | |
9768 | ||
9769 | static void perf_event_free_bpf_prog(struct perf_event *event) | |
9770 | { | |
9771 | } | |
07b139c8 | 9772 | #endif /* CONFIG_EVENT_TRACING */ |
e077df4f | 9773 | |
24f1e32c | 9774 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
f5ffe02e | 9775 | void perf_bp_event(struct perf_event *bp, void *data) |
24f1e32c | 9776 | { |
f5ffe02e FW |
9777 | struct perf_sample_data sample; |
9778 | struct pt_regs *regs = data; | |
9779 | ||
fd0d000b | 9780 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
f5ffe02e | 9781 | |
a4eaf7f1 | 9782 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
a8b0ca17 | 9783 | perf_swevent_event(bp, 1, &sample, regs); |
24f1e32c FW |
9784 | } |
9785 | #endif | |
9786 | ||
375637bc AS |
9787 | /* |
9788 | * Allocate a new address filter | |
9789 | */ | |
9790 | static struct perf_addr_filter * | |
9791 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) | |
9792 | { | |
9793 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); | |
9794 | struct perf_addr_filter *filter; | |
9795 | ||
9796 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); | |
9797 | if (!filter) | |
9798 | return NULL; | |
9799 | ||
9800 | INIT_LIST_HEAD(&filter->entry); | |
9801 | list_add_tail(&filter->entry, filters); | |
9802 | ||
9803 | return filter; | |
9804 | } | |
9805 | ||
9806 | static void free_filters_list(struct list_head *filters) | |
9807 | { | |
9808 | struct perf_addr_filter *filter, *iter; | |
9809 | ||
9810 | list_for_each_entry_safe(filter, iter, filters, entry) { | |
9511bce9 | 9811 | path_put(&filter->path); |
375637bc AS |
9812 | list_del(&filter->entry); |
9813 | kfree(filter); | |
9814 | } | |
9815 | } | |
9816 | ||
9817 | /* | |
9818 | * Free existing address filters and optionally install new ones | |
9819 | */ | |
9820 | static void perf_addr_filters_splice(struct perf_event *event, | |
9821 | struct list_head *head) | |
9822 | { | |
9823 | unsigned long flags; | |
9824 | LIST_HEAD(list); | |
9825 | ||
9826 | if (!has_addr_filter(event)) | |
9827 | return; | |
9828 | ||
9829 | /* don't bother with children, they don't have their own filters */ | |
9830 | if (event->parent) | |
9831 | return; | |
9832 | ||
9833 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); | |
9834 | ||
9835 | list_splice_init(&event->addr_filters.list, &list); | |
9836 | if (head) | |
9837 | list_splice(head, &event->addr_filters.list); | |
9838 | ||
9839 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); | |
9840 | ||
9841 | free_filters_list(&list); | |
9842 | } | |
9843 | ||
9844 | /* | |
9845 | * Scan through mm's vmas and see if one of them matches the | |
9846 | * @filter; if so, adjust filter's address range. | |
c1e8d7c6 | 9847 | * Called with mm::mmap_lock down for reading. |
375637bc | 9848 | */ |
c60f83b8 AS |
9849 | static void perf_addr_filter_apply(struct perf_addr_filter *filter, |
9850 | struct mm_struct *mm, | |
9851 | struct perf_addr_filter_range *fr) | |
375637bc AS |
9852 | { |
9853 | struct vm_area_struct *vma; | |
9854 | ||
9855 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
c60f83b8 | 9856 | if (!vma->vm_file) |
375637bc AS |
9857 | continue; |
9858 | ||
c60f83b8 AS |
9859 | if (perf_addr_filter_vma_adjust(filter, vma, fr)) |
9860 | return; | |
375637bc | 9861 | } |
375637bc AS |
9862 | } |
9863 | ||
9864 | /* | |
9865 | * Update event's address range filters based on the | |
9866 | * task's existing mappings, if any. | |
9867 | */ | |
9868 | static void perf_event_addr_filters_apply(struct perf_event *event) | |
9869 | { | |
9870 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
9871 | struct task_struct *task = READ_ONCE(event->ctx->task); | |
9872 | struct perf_addr_filter *filter; | |
9873 | struct mm_struct *mm = NULL; | |
9874 | unsigned int count = 0; | |
9875 | unsigned long flags; | |
9876 | ||
9877 | /* | |
9878 | * We may observe TASK_TOMBSTONE, which means that the event tear-down | |
9879 | * will stop on the parent's child_mutex that our caller is also holding | |
9880 | */ | |
9881 | if (task == TASK_TOMBSTONE) | |
9882 | return; | |
9883 | ||
52a44f83 AS |
9884 | if (ifh->nr_file_filters) { |
9885 | mm = get_task_mm(event->ctx->task); | |
9886 | if (!mm) | |
9887 | goto restart; | |
375637bc | 9888 | |
d8ed45c5 | 9889 | mmap_read_lock(mm); |
52a44f83 | 9890 | } |
375637bc AS |
9891 | |
9892 | raw_spin_lock_irqsave(&ifh->lock, flags); | |
9893 | list_for_each_entry(filter, &ifh->list, entry) { | |
52a44f83 AS |
9894 | if (filter->path.dentry) { |
9895 | /* | |
9896 | * Adjust base offset if the filter is associated to a | |
9897 | * binary that needs to be mapped: | |
9898 | */ | |
9899 | event->addr_filter_ranges[count].start = 0; | |
9900 | event->addr_filter_ranges[count].size = 0; | |
375637bc | 9901 | |
c60f83b8 | 9902 | perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]); |
52a44f83 AS |
9903 | } else { |
9904 | event->addr_filter_ranges[count].start = filter->offset; | |
9905 | event->addr_filter_ranges[count].size = filter->size; | |
9906 | } | |
375637bc AS |
9907 | |
9908 | count++; | |
9909 | } | |
9910 | ||
9911 | event->addr_filters_gen++; | |
9912 | raw_spin_unlock_irqrestore(&ifh->lock, flags); | |
9913 | ||
52a44f83 | 9914 | if (ifh->nr_file_filters) { |
d8ed45c5 | 9915 | mmap_read_unlock(mm); |
375637bc | 9916 | |
52a44f83 AS |
9917 | mmput(mm); |
9918 | } | |
375637bc AS |
9919 | |
9920 | restart: | |
767ae086 | 9921 | perf_event_stop(event, 1); |
375637bc AS |
9922 | } |
9923 | ||
9924 | /* | |
9925 | * Address range filtering: limiting the data to certain | |
9926 | * instruction address ranges. Filters are ioctl()ed to us from | |
9927 | * userspace as ascii strings. | |
9928 | * | |
9929 | * Filter string format: | |
9930 | * | |
9931 | * ACTION RANGE_SPEC | |
9932 | * where ACTION is one of the | |
9933 | * * "filter": limit the trace to this region | |
9934 | * * "start": start tracing from this address | |
9935 | * * "stop": stop tracing at this address/region; | |
9936 | * RANGE_SPEC is | |
9937 | * * for kernel addresses: <start address>[/<size>] | |
9938 | * * for object files: <start address>[/<size>]@</path/to/object/file> | |
9939 | * | |
6ed70cf3 AS |
9940 | * if <size> is not specified or is zero, the range is treated as a single |
9941 | * address; not valid for ACTION=="filter". | |
375637bc AS |
9942 | */ |
9943 | enum { | |
e96271f3 | 9944 | IF_ACT_NONE = -1, |
375637bc AS |
9945 | IF_ACT_FILTER, |
9946 | IF_ACT_START, | |
9947 | IF_ACT_STOP, | |
9948 | IF_SRC_FILE, | |
9949 | IF_SRC_KERNEL, | |
9950 | IF_SRC_FILEADDR, | |
9951 | IF_SRC_KERNELADDR, | |
9952 | }; | |
9953 | ||
9954 | enum { | |
9955 | IF_STATE_ACTION = 0, | |
9956 | IF_STATE_SOURCE, | |
9957 | IF_STATE_END, | |
9958 | }; | |
9959 | ||
9960 | static const match_table_t if_tokens = { | |
9961 | { IF_ACT_FILTER, "filter" }, | |
9962 | { IF_ACT_START, "start" }, | |
9963 | { IF_ACT_STOP, "stop" }, | |
9964 | { IF_SRC_FILE, "%u/%u@%s" }, | |
9965 | { IF_SRC_KERNEL, "%u/%u" }, | |
9966 | { IF_SRC_FILEADDR, "%u@%s" }, | |
9967 | { IF_SRC_KERNELADDR, "%u" }, | |
e96271f3 | 9968 | { IF_ACT_NONE, NULL }, |
375637bc AS |
9969 | }; |
9970 | ||
9971 | /* | |
9972 | * Address filter string parser | |
9973 | */ | |
9974 | static int | |
9975 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, | |
9976 | struct list_head *filters) | |
9977 | { | |
9978 | struct perf_addr_filter *filter = NULL; | |
9979 | char *start, *orig, *filename = NULL; | |
375637bc AS |
9980 | substring_t args[MAX_OPT_ARGS]; |
9981 | int state = IF_STATE_ACTION, token; | |
9982 | unsigned int kernel = 0; | |
9983 | int ret = -EINVAL; | |
9984 | ||
9985 | orig = fstr = kstrdup(fstr, GFP_KERNEL); | |
9986 | if (!fstr) | |
9987 | return -ENOMEM; | |
9988 | ||
9989 | while ((start = strsep(&fstr, " ,\n")) != NULL) { | |
6ed70cf3 AS |
9990 | static const enum perf_addr_filter_action_t actions[] = { |
9991 | [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER, | |
9992 | [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START, | |
9993 | [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP, | |
9994 | }; | |
375637bc AS |
9995 | ret = -EINVAL; |
9996 | ||
9997 | if (!*start) | |
9998 | continue; | |
9999 | ||
10000 | /* filter definition begins */ | |
10001 | if (state == IF_STATE_ACTION) { | |
10002 | filter = perf_addr_filter_new(event, filters); | |
10003 | if (!filter) | |
10004 | goto fail; | |
10005 | } | |
10006 | ||
10007 | token = match_token(start, if_tokens, args); | |
10008 | switch (token) { | |
10009 | case IF_ACT_FILTER: | |
10010 | case IF_ACT_START: | |
375637bc AS |
10011 | case IF_ACT_STOP: |
10012 | if (state != IF_STATE_ACTION) | |
10013 | goto fail; | |
10014 | ||
6ed70cf3 | 10015 | filter->action = actions[token]; |
375637bc AS |
10016 | state = IF_STATE_SOURCE; |
10017 | break; | |
10018 | ||
10019 | case IF_SRC_KERNELADDR: | |
10020 | case IF_SRC_KERNEL: | |
10021 | kernel = 1; | |
10c3405f | 10022 | /* fall through */ |
375637bc AS |
10023 | |
10024 | case IF_SRC_FILEADDR: | |
10025 | case IF_SRC_FILE: | |
10026 | if (state != IF_STATE_SOURCE) | |
10027 | goto fail; | |
10028 | ||
375637bc AS |
10029 | *args[0].to = 0; |
10030 | ret = kstrtoul(args[0].from, 0, &filter->offset); | |
10031 | if (ret) | |
10032 | goto fail; | |
10033 | ||
6ed70cf3 | 10034 | if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) { |
375637bc AS |
10035 | *args[1].to = 0; |
10036 | ret = kstrtoul(args[1].from, 0, &filter->size); | |
10037 | if (ret) | |
10038 | goto fail; | |
10039 | } | |
10040 | ||
4059ffd0 | 10041 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
6ed70cf3 | 10042 | int fpos = token == IF_SRC_FILE ? 2 : 1; |
4059ffd0 MP |
10043 | |
10044 | filename = match_strdup(&args[fpos]); | |
375637bc AS |
10045 | if (!filename) { |
10046 | ret = -ENOMEM; | |
10047 | goto fail; | |
10048 | } | |
10049 | } | |
10050 | ||
10051 | state = IF_STATE_END; | |
10052 | break; | |
10053 | ||
10054 | default: | |
10055 | goto fail; | |
10056 | } | |
10057 | ||
10058 | /* | |
10059 | * Filter definition is fully parsed, validate and install it. | |
10060 | * Make sure that it doesn't contradict itself or the event's | |
10061 | * attribute. | |
10062 | */ | |
10063 | if (state == IF_STATE_END) { | |
9ccbfbb1 | 10064 | ret = -EINVAL; |
375637bc AS |
10065 | if (kernel && event->attr.exclude_kernel) |
10066 | goto fail; | |
10067 | ||
6ed70cf3 AS |
10068 | /* |
10069 | * ACTION "filter" must have a non-zero length region | |
10070 | * specified. | |
10071 | */ | |
10072 | if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER && | |
10073 | !filter->size) | |
10074 | goto fail; | |
10075 | ||
375637bc AS |
10076 | if (!kernel) { |
10077 | if (!filename) | |
10078 | goto fail; | |
10079 | ||
6ce77bfd AS |
10080 | /* |
10081 | * For now, we only support file-based filters | |
10082 | * in per-task events; doing so for CPU-wide | |
10083 | * events requires additional context switching | |
10084 | * trickery, since same object code will be | |
10085 | * mapped at different virtual addresses in | |
10086 | * different processes. | |
10087 | */ | |
10088 | ret = -EOPNOTSUPP; | |
10089 | if (!event->ctx->task) | |
10090 | goto fail_free_name; | |
10091 | ||
375637bc | 10092 | /* look up the path and grab its inode */ |
9511bce9 SL |
10093 | ret = kern_path(filename, LOOKUP_FOLLOW, |
10094 | &filter->path); | |
375637bc AS |
10095 | if (ret) |
10096 | goto fail_free_name; | |
10097 | ||
375637bc AS |
10098 | kfree(filename); |
10099 | filename = NULL; | |
10100 | ||
10101 | ret = -EINVAL; | |
9511bce9 SL |
10102 | if (!filter->path.dentry || |
10103 | !S_ISREG(d_inode(filter->path.dentry) | |
10104 | ->i_mode)) | |
375637bc | 10105 | goto fail; |
6ce77bfd AS |
10106 | |
10107 | event->addr_filters.nr_file_filters++; | |
375637bc AS |
10108 | } |
10109 | ||
10110 | /* ready to consume more filters */ | |
10111 | state = IF_STATE_ACTION; | |
10112 | filter = NULL; | |
10113 | } | |
10114 | } | |
10115 | ||
10116 | if (state != IF_STATE_ACTION) | |
10117 | goto fail; | |
10118 | ||
10119 | kfree(orig); | |
10120 | ||
10121 | return 0; | |
10122 | ||
10123 | fail_free_name: | |
10124 | kfree(filename); | |
10125 | fail: | |
10126 | free_filters_list(filters); | |
10127 | kfree(orig); | |
10128 | ||
10129 | return ret; | |
10130 | } | |
10131 | ||
10132 | static int | |
10133 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) | |
10134 | { | |
10135 | LIST_HEAD(filters); | |
10136 | int ret; | |
10137 | ||
10138 | /* | |
10139 | * Since this is called in perf_ioctl() path, we're already holding | |
10140 | * ctx::mutex. | |
10141 | */ | |
10142 | lockdep_assert_held(&event->ctx->mutex); | |
10143 | ||
10144 | if (WARN_ON_ONCE(event->parent)) | |
10145 | return -EINVAL; | |
10146 | ||
375637bc AS |
10147 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
10148 | if (ret) | |
6ce77bfd | 10149 | goto fail_clear_files; |
375637bc AS |
10150 | |
10151 | ret = event->pmu->addr_filters_validate(&filters); | |
6ce77bfd AS |
10152 | if (ret) |
10153 | goto fail_free_filters; | |
375637bc AS |
10154 | |
10155 | /* remove existing filters, if any */ | |
10156 | perf_addr_filters_splice(event, &filters); | |
10157 | ||
10158 | /* install new filters */ | |
10159 | perf_event_for_each_child(event, perf_event_addr_filters_apply); | |
10160 | ||
6ce77bfd AS |
10161 | return ret; |
10162 | ||
10163 | fail_free_filters: | |
10164 | free_filters_list(&filters); | |
10165 | ||
10166 | fail_clear_files: | |
10167 | event->addr_filters.nr_file_filters = 0; | |
10168 | ||
375637bc AS |
10169 | return ret; |
10170 | } | |
10171 | ||
c796bbbe AS |
10172 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
10173 | { | |
c796bbbe | 10174 | int ret = -EINVAL; |
e12f03d7 | 10175 | char *filter_str; |
c796bbbe AS |
10176 | |
10177 | filter_str = strndup_user(arg, PAGE_SIZE); | |
10178 | if (IS_ERR(filter_str)) | |
10179 | return PTR_ERR(filter_str); | |
10180 | ||
e12f03d7 SL |
10181 | #ifdef CONFIG_EVENT_TRACING |
10182 | if (perf_event_is_tracing(event)) { | |
10183 | struct perf_event_context *ctx = event->ctx; | |
10184 | ||
10185 | /* | |
10186 | * Beware, here be dragons!! | |
10187 | * | |
10188 | * the tracepoint muck will deadlock against ctx->mutex, but | |
10189 | * the tracepoint stuff does not actually need it. So | |
10190 | * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we | |
10191 | * already have a reference on ctx. | |
10192 | * | |
10193 | * This can result in event getting moved to a different ctx, | |
10194 | * but that does not affect the tracepoint state. | |
10195 | */ | |
10196 | mutex_unlock(&ctx->mutex); | |
10197 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); | |
10198 | mutex_lock(&ctx->mutex); | |
10199 | } else | |
10200 | #endif | |
10201 | if (has_addr_filter(event)) | |
375637bc | 10202 | ret = perf_event_set_addr_filter(event, filter_str); |
c796bbbe AS |
10203 | |
10204 | kfree(filter_str); | |
10205 | return ret; | |
10206 | } | |
10207 | ||
b0a873eb PZ |
10208 | /* |
10209 | * hrtimer based swevent callback | |
10210 | */ | |
f29ac756 | 10211 | |
b0a873eb | 10212 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
f29ac756 | 10213 | { |
b0a873eb PZ |
10214 | enum hrtimer_restart ret = HRTIMER_RESTART; |
10215 | struct perf_sample_data data; | |
10216 | struct pt_regs *regs; | |
10217 | struct perf_event *event; | |
10218 | u64 period; | |
f29ac756 | 10219 | |
b0a873eb | 10220 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
ba3dd36c PZ |
10221 | |
10222 | if (event->state != PERF_EVENT_STATE_ACTIVE) | |
10223 | return HRTIMER_NORESTART; | |
10224 | ||
b0a873eb | 10225 | event->pmu->read(event); |
f344011c | 10226 | |
fd0d000b | 10227 | perf_sample_data_init(&data, 0, event->hw.last_period); |
b0a873eb PZ |
10228 | regs = get_irq_regs(); |
10229 | ||
10230 | if (regs && !perf_exclude_event(event, regs)) { | |
77aeeebd | 10231 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
33b07b8b | 10232 | if (__perf_event_overflow(event, 1, &data, regs)) |
b0a873eb PZ |
10233 | ret = HRTIMER_NORESTART; |
10234 | } | |
24f1e32c | 10235 | |
b0a873eb PZ |
10236 | period = max_t(u64, 10000, event->hw.sample_period); |
10237 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); | |
24f1e32c | 10238 | |
b0a873eb | 10239 | return ret; |
f29ac756 PZ |
10240 | } |
10241 | ||
b0a873eb | 10242 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
5c92d124 | 10243 | { |
b0a873eb | 10244 | struct hw_perf_event *hwc = &event->hw; |
5d508e82 FBH |
10245 | s64 period; |
10246 | ||
10247 | if (!is_sampling_event(event)) | |
10248 | return; | |
f5ffe02e | 10249 | |
5d508e82 FBH |
10250 | period = local64_read(&hwc->period_left); |
10251 | if (period) { | |
10252 | if (period < 0) | |
10253 | period = 10000; | |
fa407f35 | 10254 | |
5d508e82 FBH |
10255 | local64_set(&hwc->period_left, 0); |
10256 | } else { | |
10257 | period = max_t(u64, 10000, hwc->sample_period); | |
10258 | } | |
3497d206 | 10259 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
30f9028b | 10260 | HRTIMER_MODE_REL_PINNED_HARD); |
24f1e32c | 10261 | } |
b0a873eb PZ |
10262 | |
10263 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) | |
24f1e32c | 10264 | { |
b0a873eb PZ |
10265 | struct hw_perf_event *hwc = &event->hw; |
10266 | ||
6c7e550f | 10267 | if (is_sampling_event(event)) { |
b0a873eb | 10268 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
fa407f35 | 10269 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
b0a873eb PZ |
10270 | |
10271 | hrtimer_cancel(&hwc->hrtimer); | |
10272 | } | |
24f1e32c FW |
10273 | } |
10274 | ||
ba3dd36c PZ |
10275 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
10276 | { | |
10277 | struct hw_perf_event *hwc = &event->hw; | |
10278 | ||
10279 | if (!is_sampling_event(event)) | |
10280 | return; | |
10281 | ||
30f9028b | 10282 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
ba3dd36c PZ |
10283 | hwc->hrtimer.function = perf_swevent_hrtimer; |
10284 | ||
10285 | /* | |
10286 | * Since hrtimers have a fixed rate, we can do a static freq->period | |
10287 | * mapping and avoid the whole period adjust feedback stuff. | |
10288 | */ | |
10289 | if (event->attr.freq) { | |
10290 | long freq = event->attr.sample_freq; | |
10291 | ||
10292 | event->attr.sample_period = NSEC_PER_SEC / freq; | |
10293 | hwc->sample_period = event->attr.sample_period; | |
10294 | local64_set(&hwc->period_left, hwc->sample_period); | |
778141e3 | 10295 | hwc->last_period = hwc->sample_period; |
ba3dd36c PZ |
10296 | event->attr.freq = 0; |
10297 | } | |
10298 | } | |
10299 | ||
b0a873eb PZ |
10300 | /* |
10301 | * Software event: cpu wall time clock | |
10302 | */ | |
10303 | ||
10304 | static void cpu_clock_event_update(struct perf_event *event) | |
24f1e32c | 10305 | { |
b0a873eb PZ |
10306 | s64 prev; |
10307 | u64 now; | |
10308 | ||
a4eaf7f1 | 10309 | now = local_clock(); |
b0a873eb PZ |
10310 | prev = local64_xchg(&event->hw.prev_count, now); |
10311 | local64_add(now - prev, &event->count); | |
24f1e32c | 10312 | } |
24f1e32c | 10313 | |
a4eaf7f1 | 10314 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 10315 | { |
a4eaf7f1 | 10316 | local64_set(&event->hw.prev_count, local_clock()); |
b0a873eb | 10317 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
10318 | } |
10319 | ||
a4eaf7f1 | 10320 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
f29ac756 | 10321 | { |
b0a873eb PZ |
10322 | perf_swevent_cancel_hrtimer(event); |
10323 | cpu_clock_event_update(event); | |
10324 | } | |
f29ac756 | 10325 | |
a4eaf7f1 PZ |
10326 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
10327 | { | |
10328 | if (flags & PERF_EF_START) | |
10329 | cpu_clock_event_start(event, flags); | |
6a694a60 | 10330 | perf_event_update_userpage(event); |
a4eaf7f1 PZ |
10331 | |
10332 | return 0; | |
10333 | } | |
10334 | ||
10335 | static void cpu_clock_event_del(struct perf_event *event, int flags) | |
10336 | { | |
10337 | cpu_clock_event_stop(event, flags); | |
10338 | } | |
10339 | ||
b0a873eb PZ |
10340 | static void cpu_clock_event_read(struct perf_event *event) |
10341 | { | |
10342 | cpu_clock_event_update(event); | |
10343 | } | |
f344011c | 10344 | |
b0a873eb PZ |
10345 | static int cpu_clock_event_init(struct perf_event *event) |
10346 | { | |
10347 | if (event->attr.type != PERF_TYPE_SOFTWARE) | |
10348 | return -ENOENT; | |
10349 | ||
10350 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) | |
10351 | return -ENOENT; | |
10352 | ||
2481c5fa SE |
10353 | /* |
10354 | * no branch sampling for software events | |
10355 | */ | |
10356 | if (has_branch_stack(event)) | |
10357 | return -EOPNOTSUPP; | |
10358 | ||
ba3dd36c PZ |
10359 | perf_swevent_init_hrtimer(event); |
10360 | ||
b0a873eb | 10361 | return 0; |
f29ac756 PZ |
10362 | } |
10363 | ||
b0a873eb | 10364 | static struct pmu perf_cpu_clock = { |
89a1e187 PZ |
10365 | .task_ctx_nr = perf_sw_context, |
10366 | ||
34f43927 PZ |
10367 | .capabilities = PERF_PMU_CAP_NO_NMI, |
10368 | ||
b0a873eb | 10369 | .event_init = cpu_clock_event_init, |
a4eaf7f1 PZ |
10370 | .add = cpu_clock_event_add, |
10371 | .del = cpu_clock_event_del, | |
10372 | .start = cpu_clock_event_start, | |
10373 | .stop = cpu_clock_event_stop, | |
b0a873eb PZ |
10374 | .read = cpu_clock_event_read, |
10375 | }; | |
10376 | ||
10377 | /* | |
10378 | * Software event: task time clock | |
10379 | */ | |
10380 | ||
10381 | static void task_clock_event_update(struct perf_event *event, u64 now) | |
5c92d124 | 10382 | { |
b0a873eb PZ |
10383 | u64 prev; |
10384 | s64 delta; | |
5c92d124 | 10385 | |
b0a873eb PZ |
10386 | prev = local64_xchg(&event->hw.prev_count, now); |
10387 | delta = now - prev; | |
10388 | local64_add(delta, &event->count); | |
10389 | } | |
5c92d124 | 10390 | |
a4eaf7f1 | 10391 | static void task_clock_event_start(struct perf_event *event, int flags) |
b0a873eb | 10392 | { |
a4eaf7f1 | 10393 | local64_set(&event->hw.prev_count, event->ctx->time); |
b0a873eb | 10394 | perf_swevent_start_hrtimer(event); |
b0a873eb PZ |
10395 | } |
10396 | ||
a4eaf7f1 | 10397 | static void task_clock_event_stop(struct perf_event *event, int flags) |
b0a873eb PZ |
10398 | { |
10399 | perf_swevent_cancel_hrtimer(event); | |
10400 | task_clock_event_update(event, event->ctx->time); | |
a4eaf7f1 PZ |
10401 | } |
10402 | ||
10403 | static int task_clock_event_add(struct perf_event *event, int flags) | |
10404 | { | |
10405 | if (flags & PERF_EF_START) | |
10406 | task_clock_event_start(event, flags); | |
6a694a60 | 10407 | perf_event_update_userpage(event); |
b0a873eb | 10408 | |
a4eaf7f1 PZ |
10409 | return 0; |
10410 | } | |
10411 | ||
10412 | static void task_clock_event_del(struct perf_event *event, int flags) | |
10413 | { | |
10414 | task_clock_event_stop(event, PERF_EF_UPDATE); | |
b0a873eb PZ |
10415 | } |
10416 | ||
10417 | static void task_clock_event_read(struct perf_event *event) | |
10418 | { | |
768a06e2 PZ |
10419 | u64 now = perf_clock(); |
10420 | u64 delta = now - event->ctx->timestamp; | |
10421 | u64 time = event->ctx->time + delta; | |
b0a873eb PZ |
10422 | |
10423 | task_clock_event_update(event, time); | |
10424 | } | |
10425 | ||
10426 | static int task_clock_event_init(struct perf_event *event) | |
6fb2915d | 10427 | { |
b0a873eb PZ |
10428 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
10429 | return -ENOENT; | |
10430 | ||
10431 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) | |
10432 | return -ENOENT; | |
10433 | ||
2481c5fa SE |
10434 | /* |
10435 | * no branch sampling for software events | |
10436 | */ | |
10437 | if (has_branch_stack(event)) | |
10438 | return -EOPNOTSUPP; | |
10439 | ||
ba3dd36c PZ |
10440 | perf_swevent_init_hrtimer(event); |
10441 | ||
b0a873eb | 10442 | return 0; |
6fb2915d LZ |
10443 | } |
10444 | ||
b0a873eb | 10445 | static struct pmu perf_task_clock = { |
89a1e187 PZ |
10446 | .task_ctx_nr = perf_sw_context, |
10447 | ||
34f43927 PZ |
10448 | .capabilities = PERF_PMU_CAP_NO_NMI, |
10449 | ||
b0a873eb | 10450 | .event_init = task_clock_event_init, |
a4eaf7f1 PZ |
10451 | .add = task_clock_event_add, |
10452 | .del = task_clock_event_del, | |
10453 | .start = task_clock_event_start, | |
10454 | .stop = task_clock_event_stop, | |
b0a873eb PZ |
10455 | .read = task_clock_event_read, |
10456 | }; | |
6fb2915d | 10457 | |
ad5133b7 | 10458 | static void perf_pmu_nop_void(struct pmu *pmu) |
e077df4f | 10459 | { |
e077df4f | 10460 | } |
6fb2915d | 10461 | |
fbbe0701 SB |
10462 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
10463 | { | |
10464 | } | |
10465 | ||
ad5133b7 | 10466 | static int perf_pmu_nop_int(struct pmu *pmu) |
6fb2915d | 10467 | { |
ad5133b7 | 10468 | return 0; |
6fb2915d LZ |
10469 | } |
10470 | ||
81ec3f3c JO |
10471 | static int perf_event_nop_int(struct perf_event *event, u64 value) |
10472 | { | |
10473 | return 0; | |
10474 | } | |
10475 | ||
18ab2cd3 | 10476 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
fbbe0701 SB |
10477 | |
10478 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) | |
6fb2915d | 10479 | { |
fbbe0701 SB |
10480 | __this_cpu_write(nop_txn_flags, flags); |
10481 | ||
10482 | if (flags & ~PERF_PMU_TXN_ADD) | |
10483 | return; | |
10484 | ||
ad5133b7 | 10485 | perf_pmu_disable(pmu); |
6fb2915d LZ |
10486 | } |
10487 | ||
ad5133b7 PZ |
10488 | static int perf_pmu_commit_txn(struct pmu *pmu) |
10489 | { | |
fbbe0701 SB |
10490 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
10491 | ||
10492 | __this_cpu_write(nop_txn_flags, 0); | |
10493 | ||
10494 | if (flags & ~PERF_PMU_TXN_ADD) | |
10495 | return 0; | |
10496 | ||
ad5133b7 PZ |
10497 | perf_pmu_enable(pmu); |
10498 | return 0; | |
10499 | } | |
e077df4f | 10500 | |
ad5133b7 | 10501 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
24f1e32c | 10502 | { |
fbbe0701 SB |
10503 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
10504 | ||
10505 | __this_cpu_write(nop_txn_flags, 0); | |
10506 | ||
10507 | if (flags & ~PERF_PMU_TXN_ADD) | |
10508 | return; | |
10509 | ||
ad5133b7 | 10510 | perf_pmu_enable(pmu); |
24f1e32c FW |
10511 | } |
10512 | ||
35edc2a5 PZ |
10513 | static int perf_event_idx_default(struct perf_event *event) |
10514 | { | |
c719f560 | 10515 | return 0; |
35edc2a5 PZ |
10516 | } |
10517 | ||
8dc85d54 PZ |
10518 | /* |
10519 | * Ensures all contexts with the same task_ctx_nr have the same | |
10520 | * pmu_cpu_context too. | |
10521 | */ | |
9e317041 | 10522 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
24f1e32c | 10523 | { |
8dc85d54 | 10524 | struct pmu *pmu; |
b326e956 | 10525 | |
8dc85d54 PZ |
10526 | if (ctxn < 0) |
10527 | return NULL; | |
24f1e32c | 10528 | |
8dc85d54 PZ |
10529 | list_for_each_entry(pmu, &pmus, entry) { |
10530 | if (pmu->task_ctx_nr == ctxn) | |
10531 | return pmu->pmu_cpu_context; | |
10532 | } | |
24f1e32c | 10533 | |
8dc85d54 | 10534 | return NULL; |
24f1e32c FW |
10535 | } |
10536 | ||
51676957 PZ |
10537 | static void free_pmu_context(struct pmu *pmu) |
10538 | { | |
df0062b2 WD |
10539 | /* |
10540 | * Static contexts such as perf_sw_context have a global lifetime | |
10541 | * and may be shared between different PMUs. Avoid freeing them | |
10542 | * when a single PMU is going away. | |
10543 | */ | |
10544 | if (pmu->task_ctx_nr > perf_invalid_context) | |
10545 | return; | |
10546 | ||
51676957 | 10547 | free_percpu(pmu->pmu_cpu_context); |
24f1e32c | 10548 | } |
6e855cd4 AS |
10549 | |
10550 | /* | |
10551 | * Let userspace know that this PMU supports address range filtering: | |
10552 | */ | |
10553 | static ssize_t nr_addr_filters_show(struct device *dev, | |
10554 | struct device_attribute *attr, | |
10555 | char *page) | |
10556 | { | |
10557 | struct pmu *pmu = dev_get_drvdata(dev); | |
10558 | ||
10559 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); | |
10560 | } | |
10561 | DEVICE_ATTR_RO(nr_addr_filters); | |
10562 | ||
2e80a82a | 10563 | static struct idr pmu_idr; |
d6d020e9 | 10564 | |
abe43400 PZ |
10565 | static ssize_t |
10566 | type_show(struct device *dev, struct device_attribute *attr, char *page) | |
10567 | { | |
10568 | struct pmu *pmu = dev_get_drvdata(dev); | |
10569 | ||
10570 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); | |
10571 | } | |
90826ca7 | 10572 | static DEVICE_ATTR_RO(type); |
abe43400 | 10573 | |
62b85639 SE |
10574 | static ssize_t |
10575 | perf_event_mux_interval_ms_show(struct device *dev, | |
10576 | struct device_attribute *attr, | |
10577 | char *page) | |
10578 | { | |
10579 | struct pmu *pmu = dev_get_drvdata(dev); | |
10580 | ||
10581 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); | |
10582 | } | |
10583 | ||
272325c4 PZ |
10584 | static DEFINE_MUTEX(mux_interval_mutex); |
10585 | ||
62b85639 SE |
10586 | static ssize_t |
10587 | perf_event_mux_interval_ms_store(struct device *dev, | |
10588 | struct device_attribute *attr, | |
10589 | const char *buf, size_t count) | |
10590 | { | |
10591 | struct pmu *pmu = dev_get_drvdata(dev); | |
10592 | int timer, cpu, ret; | |
10593 | ||
10594 | ret = kstrtoint(buf, 0, &timer); | |
10595 | if (ret) | |
10596 | return ret; | |
10597 | ||
10598 | if (timer < 1) | |
10599 | return -EINVAL; | |
10600 | ||
10601 | /* same value, noting to do */ | |
10602 | if (timer == pmu->hrtimer_interval_ms) | |
10603 | return count; | |
10604 | ||
272325c4 | 10605 | mutex_lock(&mux_interval_mutex); |
62b85639 SE |
10606 | pmu->hrtimer_interval_ms = timer; |
10607 | ||
10608 | /* update all cpuctx for this PMU */ | |
a63fbed7 | 10609 | cpus_read_lock(); |
272325c4 | 10610 | for_each_online_cpu(cpu) { |
62b85639 SE |
10611 | struct perf_cpu_context *cpuctx; |
10612 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
10613 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); | |
10614 | ||
272325c4 PZ |
10615 | cpu_function_call(cpu, |
10616 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); | |
62b85639 | 10617 | } |
a63fbed7 | 10618 | cpus_read_unlock(); |
272325c4 | 10619 | mutex_unlock(&mux_interval_mutex); |
62b85639 SE |
10620 | |
10621 | return count; | |
10622 | } | |
90826ca7 | 10623 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
62b85639 | 10624 | |
90826ca7 GKH |
10625 | static struct attribute *pmu_dev_attrs[] = { |
10626 | &dev_attr_type.attr, | |
10627 | &dev_attr_perf_event_mux_interval_ms.attr, | |
10628 | NULL, | |
abe43400 | 10629 | }; |
90826ca7 | 10630 | ATTRIBUTE_GROUPS(pmu_dev); |
abe43400 PZ |
10631 | |
10632 | static int pmu_bus_running; | |
10633 | static struct bus_type pmu_bus = { | |
10634 | .name = "event_source", | |
90826ca7 | 10635 | .dev_groups = pmu_dev_groups, |
abe43400 PZ |
10636 | }; |
10637 | ||
10638 | static void pmu_dev_release(struct device *dev) | |
10639 | { | |
10640 | kfree(dev); | |
10641 | } | |
10642 | ||
10643 | static int pmu_dev_alloc(struct pmu *pmu) | |
10644 | { | |
10645 | int ret = -ENOMEM; | |
10646 | ||
10647 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
10648 | if (!pmu->dev) | |
10649 | goto out; | |
10650 | ||
0c9d42ed | 10651 | pmu->dev->groups = pmu->attr_groups; |
abe43400 PZ |
10652 | device_initialize(pmu->dev); |
10653 | ret = dev_set_name(pmu->dev, "%s", pmu->name); | |
10654 | if (ret) | |
10655 | goto free_dev; | |
10656 | ||
10657 | dev_set_drvdata(pmu->dev, pmu); | |
10658 | pmu->dev->bus = &pmu_bus; | |
10659 | pmu->dev->release = pmu_dev_release; | |
10660 | ret = device_add(pmu->dev); | |
10661 | if (ret) | |
10662 | goto free_dev; | |
10663 | ||
6e855cd4 AS |
10664 | /* For PMUs with address filters, throw in an extra attribute: */ |
10665 | if (pmu->nr_addr_filters) | |
10666 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); | |
10667 | ||
10668 | if (ret) | |
10669 | goto del_dev; | |
10670 | ||
f3a3a825 JO |
10671 | if (pmu->attr_update) |
10672 | ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update); | |
10673 | ||
10674 | if (ret) | |
10675 | goto del_dev; | |
10676 | ||
abe43400 PZ |
10677 | out: |
10678 | return ret; | |
10679 | ||
6e855cd4 AS |
10680 | del_dev: |
10681 | device_del(pmu->dev); | |
10682 | ||
abe43400 PZ |
10683 | free_dev: |
10684 | put_device(pmu->dev); | |
10685 | goto out; | |
10686 | } | |
10687 | ||
547e9fd7 | 10688 | static struct lock_class_key cpuctx_mutex; |
facc4307 | 10689 | static struct lock_class_key cpuctx_lock; |
547e9fd7 | 10690 | |
03d8e80b | 10691 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
24f1e32c | 10692 | { |
66d258c5 | 10693 | int cpu, ret, max = PERF_TYPE_MAX; |
24f1e32c | 10694 | |
b0a873eb | 10695 | mutex_lock(&pmus_lock); |
33696fc0 PZ |
10696 | ret = -ENOMEM; |
10697 | pmu->pmu_disable_count = alloc_percpu(int); | |
10698 | if (!pmu->pmu_disable_count) | |
10699 | goto unlock; | |
f29ac756 | 10700 | |
2e80a82a PZ |
10701 | pmu->type = -1; |
10702 | if (!name) | |
10703 | goto skip_type; | |
10704 | pmu->name = name; | |
10705 | ||
66d258c5 PZ |
10706 | if (type != PERF_TYPE_SOFTWARE) { |
10707 | if (type >= 0) | |
10708 | max = type; | |
10709 | ||
10710 | ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL); | |
10711 | if (ret < 0) | |
2e80a82a | 10712 | goto free_pdc; |
66d258c5 PZ |
10713 | |
10714 | WARN_ON(type >= 0 && ret != type); | |
10715 | ||
10716 | type = ret; | |
2e80a82a PZ |
10717 | } |
10718 | pmu->type = type; | |
10719 | ||
abe43400 PZ |
10720 | if (pmu_bus_running) { |
10721 | ret = pmu_dev_alloc(pmu); | |
10722 | if (ret) | |
10723 | goto free_idr; | |
10724 | } | |
10725 | ||
2e80a82a | 10726 | skip_type: |
26657848 PZ |
10727 | if (pmu->task_ctx_nr == perf_hw_context) { |
10728 | static int hw_context_taken = 0; | |
10729 | ||
5101ef20 MR |
10730 | /* |
10731 | * Other than systems with heterogeneous CPUs, it never makes | |
10732 | * sense for two PMUs to share perf_hw_context. PMUs which are | |
10733 | * uncore must use perf_invalid_context. | |
10734 | */ | |
10735 | if (WARN_ON_ONCE(hw_context_taken && | |
10736 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) | |
26657848 PZ |
10737 | pmu->task_ctx_nr = perf_invalid_context; |
10738 | ||
10739 | hw_context_taken = 1; | |
10740 | } | |
10741 | ||
8dc85d54 PZ |
10742 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
10743 | if (pmu->pmu_cpu_context) | |
10744 | goto got_cpu_context; | |
f29ac756 | 10745 | |
c4814202 | 10746 | ret = -ENOMEM; |
108b02cf PZ |
10747 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
10748 | if (!pmu->pmu_cpu_context) | |
abe43400 | 10749 | goto free_dev; |
f344011c | 10750 | |
108b02cf PZ |
10751 | for_each_possible_cpu(cpu) { |
10752 | struct perf_cpu_context *cpuctx; | |
10753 | ||
10754 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
eb184479 | 10755 | __perf_event_init_context(&cpuctx->ctx); |
547e9fd7 | 10756 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
facc4307 | 10757 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
108b02cf | 10758 | cpuctx->ctx.pmu = pmu; |
a63fbed7 | 10759 | cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); |
9e630205 | 10760 | |
272325c4 | 10761 | __perf_mux_hrtimer_init(cpuctx, cpu); |
836196be IR |
10762 | |
10763 | cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default); | |
10764 | cpuctx->heap = cpuctx->heap_default; | |
108b02cf | 10765 | } |
76e1d904 | 10766 | |
8dc85d54 | 10767 | got_cpu_context: |
ad5133b7 PZ |
10768 | if (!pmu->start_txn) { |
10769 | if (pmu->pmu_enable) { | |
10770 | /* | |
10771 | * If we have pmu_enable/pmu_disable calls, install | |
10772 | * transaction stubs that use that to try and batch | |
10773 | * hardware accesses. | |
10774 | */ | |
10775 | pmu->start_txn = perf_pmu_start_txn; | |
10776 | pmu->commit_txn = perf_pmu_commit_txn; | |
10777 | pmu->cancel_txn = perf_pmu_cancel_txn; | |
10778 | } else { | |
fbbe0701 | 10779 | pmu->start_txn = perf_pmu_nop_txn; |
ad5133b7 PZ |
10780 | pmu->commit_txn = perf_pmu_nop_int; |
10781 | pmu->cancel_txn = perf_pmu_nop_void; | |
f344011c | 10782 | } |
5c92d124 | 10783 | } |
15dbf27c | 10784 | |
ad5133b7 PZ |
10785 | if (!pmu->pmu_enable) { |
10786 | pmu->pmu_enable = perf_pmu_nop_void; | |
10787 | pmu->pmu_disable = perf_pmu_nop_void; | |
10788 | } | |
10789 | ||
81ec3f3c JO |
10790 | if (!pmu->check_period) |
10791 | pmu->check_period = perf_event_nop_int; | |
10792 | ||
35edc2a5 PZ |
10793 | if (!pmu->event_idx) |
10794 | pmu->event_idx = perf_event_idx_default; | |
10795 | ||
d44f821b LK |
10796 | /* |
10797 | * Ensure the TYPE_SOFTWARE PMUs are at the head of the list, | |
10798 | * since these cannot be in the IDR. This way the linear search | |
10799 | * is fast, provided a valid software event is provided. | |
10800 | */ | |
10801 | if (type == PERF_TYPE_SOFTWARE || !name) | |
10802 | list_add_rcu(&pmu->entry, &pmus); | |
10803 | else | |
10804 | list_add_tail_rcu(&pmu->entry, &pmus); | |
10805 | ||
bed5b25a | 10806 | atomic_set(&pmu->exclusive_cnt, 0); |
33696fc0 PZ |
10807 | ret = 0; |
10808 | unlock: | |
b0a873eb PZ |
10809 | mutex_unlock(&pmus_lock); |
10810 | ||
33696fc0 | 10811 | return ret; |
108b02cf | 10812 | |
abe43400 PZ |
10813 | free_dev: |
10814 | device_del(pmu->dev); | |
10815 | put_device(pmu->dev); | |
10816 | ||
2e80a82a | 10817 | free_idr: |
66d258c5 | 10818 | if (pmu->type != PERF_TYPE_SOFTWARE) |
2e80a82a PZ |
10819 | idr_remove(&pmu_idr, pmu->type); |
10820 | ||
108b02cf PZ |
10821 | free_pdc: |
10822 | free_percpu(pmu->pmu_disable_count); | |
10823 | goto unlock; | |
f29ac756 | 10824 | } |
c464c76e | 10825 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
f29ac756 | 10826 | |
b0a873eb | 10827 | void perf_pmu_unregister(struct pmu *pmu) |
5c92d124 | 10828 | { |
b0a873eb PZ |
10829 | mutex_lock(&pmus_lock); |
10830 | list_del_rcu(&pmu->entry); | |
5c92d124 | 10831 | |
0475f9ea | 10832 | /* |
cde8e884 PZ |
10833 | * We dereference the pmu list under both SRCU and regular RCU, so |
10834 | * synchronize against both of those. | |
0475f9ea | 10835 | */ |
b0a873eb | 10836 | synchronize_srcu(&pmus_srcu); |
cde8e884 | 10837 | synchronize_rcu(); |
d6d020e9 | 10838 | |
33696fc0 | 10839 | free_percpu(pmu->pmu_disable_count); |
66d258c5 | 10840 | if (pmu->type != PERF_TYPE_SOFTWARE) |
2e80a82a | 10841 | idr_remove(&pmu_idr, pmu->type); |
a9f97721 | 10842 | if (pmu_bus_running) { |
0933840a JO |
10843 | if (pmu->nr_addr_filters) |
10844 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); | |
10845 | device_del(pmu->dev); | |
10846 | put_device(pmu->dev); | |
10847 | } | |
51676957 | 10848 | free_pmu_context(pmu); |
a9f97721 | 10849 | mutex_unlock(&pmus_lock); |
b0a873eb | 10850 | } |
c464c76e | 10851 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
d6d020e9 | 10852 | |
e321d02d KL |
10853 | static inline bool has_extended_regs(struct perf_event *event) |
10854 | { | |
10855 | return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || | |
10856 | (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); | |
10857 | } | |
10858 | ||
cc34b98b MR |
10859 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
10860 | { | |
ccd41c86 | 10861 | struct perf_event_context *ctx = NULL; |
cc34b98b MR |
10862 | int ret; |
10863 | ||
10864 | if (!try_module_get(pmu->module)) | |
10865 | return -ENODEV; | |
ccd41c86 | 10866 | |
0c7296ca PZ |
10867 | /* |
10868 | * A number of pmu->event_init() methods iterate the sibling_list to, | |
10869 | * for example, validate if the group fits on the PMU. Therefore, | |
10870 | * if this is a sibling event, acquire the ctx->mutex to protect | |
10871 | * the sibling_list. | |
10872 | */ | |
10873 | if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { | |
8b10c5e2 PZ |
10874 | /* |
10875 | * This ctx->mutex can nest when we're called through | |
10876 | * inheritance. See the perf_event_ctx_lock_nested() comment. | |
10877 | */ | |
10878 | ctx = perf_event_ctx_lock_nested(event->group_leader, | |
10879 | SINGLE_DEPTH_NESTING); | |
ccd41c86 PZ |
10880 | BUG_ON(!ctx); |
10881 | } | |
10882 | ||
cc34b98b MR |
10883 | event->pmu = pmu; |
10884 | ret = pmu->event_init(event); | |
ccd41c86 PZ |
10885 | |
10886 | if (ctx) | |
10887 | perf_event_ctx_unlock(event->group_leader, ctx); | |
10888 | ||
cc6795ae | 10889 | if (!ret) { |
e321d02d KL |
10890 | if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && |
10891 | has_extended_regs(event)) | |
10892 | ret = -EOPNOTSUPP; | |
10893 | ||
cc6795ae | 10894 | if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE && |
e321d02d | 10895 | event_has_any_exclude_flag(event)) |
cc6795ae | 10896 | ret = -EINVAL; |
e321d02d KL |
10897 | |
10898 | if (ret && event->destroy) | |
10899 | event->destroy(event); | |
cc6795ae AM |
10900 | } |
10901 | ||
cc34b98b MR |
10902 | if (ret) |
10903 | module_put(pmu->module); | |
10904 | ||
10905 | return ret; | |
10906 | } | |
10907 | ||
18ab2cd3 | 10908 | static struct pmu *perf_init_event(struct perf_event *event) |
b0a873eb | 10909 | { |
66d258c5 | 10910 | int idx, type, ret; |
85c617ab | 10911 | struct pmu *pmu; |
b0a873eb PZ |
10912 | |
10913 | idx = srcu_read_lock(&pmus_srcu); | |
2e80a82a | 10914 | |
40999312 KL |
10915 | /* Try parent's PMU first: */ |
10916 | if (event->parent && event->parent->pmu) { | |
10917 | pmu = event->parent->pmu; | |
10918 | ret = perf_try_init_event(pmu, event); | |
10919 | if (!ret) | |
10920 | goto unlock; | |
10921 | } | |
10922 | ||
66d258c5 PZ |
10923 | /* |
10924 | * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE | |
10925 | * are often aliases for PERF_TYPE_RAW. | |
10926 | */ | |
10927 | type = event->attr.type; | |
10928 | if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) | |
10929 | type = PERF_TYPE_RAW; | |
10930 | ||
10931 | again: | |
2e80a82a | 10932 | rcu_read_lock(); |
66d258c5 | 10933 | pmu = idr_find(&pmu_idr, type); |
2e80a82a | 10934 | rcu_read_unlock(); |
940c5b29 | 10935 | if (pmu) { |
cc34b98b | 10936 | ret = perf_try_init_event(pmu, event); |
66d258c5 PZ |
10937 | if (ret == -ENOENT && event->attr.type != type) { |
10938 | type = event->attr.type; | |
10939 | goto again; | |
10940 | } | |
10941 | ||
940c5b29 LM |
10942 | if (ret) |
10943 | pmu = ERR_PTR(ret); | |
66d258c5 | 10944 | |
2e80a82a | 10945 | goto unlock; |
940c5b29 | 10946 | } |
2e80a82a | 10947 | |
9f0bff11 | 10948 | list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) { |
cc34b98b | 10949 | ret = perf_try_init_event(pmu, event); |
b0a873eb | 10950 | if (!ret) |
e5f4d339 | 10951 | goto unlock; |
76e1d904 | 10952 | |
b0a873eb PZ |
10953 | if (ret != -ENOENT) { |
10954 | pmu = ERR_PTR(ret); | |
e5f4d339 | 10955 | goto unlock; |
f344011c | 10956 | } |
5c92d124 | 10957 | } |
e5f4d339 PZ |
10958 | pmu = ERR_PTR(-ENOENT); |
10959 | unlock: | |
b0a873eb | 10960 | srcu_read_unlock(&pmus_srcu, idx); |
15dbf27c | 10961 | |
4aeb0b42 | 10962 | return pmu; |
5c92d124 IM |
10963 | } |
10964 | ||
f2fb6bef KL |
10965 | static void attach_sb_event(struct perf_event *event) |
10966 | { | |
10967 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); | |
10968 | ||
10969 | raw_spin_lock(&pel->lock); | |
10970 | list_add_rcu(&event->sb_list, &pel->list); | |
10971 | raw_spin_unlock(&pel->lock); | |
10972 | } | |
10973 | ||
aab5b71e PZ |
10974 | /* |
10975 | * We keep a list of all !task (and therefore per-cpu) events | |
10976 | * that need to receive side-band records. | |
10977 | * | |
10978 | * This avoids having to scan all the various PMU per-cpu contexts | |
10979 | * looking for them. | |
10980 | */ | |
f2fb6bef KL |
10981 | static void account_pmu_sb_event(struct perf_event *event) |
10982 | { | |
a4f144eb | 10983 | if (is_sb_event(event)) |
f2fb6bef KL |
10984 | attach_sb_event(event); |
10985 | } | |
10986 | ||
4beb31f3 FW |
10987 | static void account_event_cpu(struct perf_event *event, int cpu) |
10988 | { | |
10989 | if (event->parent) | |
10990 | return; | |
10991 | ||
4beb31f3 FW |
10992 | if (is_cgroup_event(event)) |
10993 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); | |
10994 | } | |
10995 | ||
555e0c1e FW |
10996 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
10997 | static void account_freq_event_nohz(void) | |
10998 | { | |
10999 | #ifdef CONFIG_NO_HZ_FULL | |
11000 | /* Lock so we don't race with concurrent unaccount */ | |
11001 | spin_lock(&nr_freq_lock); | |
11002 | if (atomic_inc_return(&nr_freq_events) == 1) | |
11003 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); | |
11004 | spin_unlock(&nr_freq_lock); | |
11005 | #endif | |
11006 | } | |
11007 | ||
11008 | static void account_freq_event(void) | |
11009 | { | |
11010 | if (tick_nohz_full_enabled()) | |
11011 | account_freq_event_nohz(); | |
11012 | else | |
11013 | atomic_inc(&nr_freq_events); | |
11014 | } | |
11015 | ||
11016 | ||
766d6c07 FW |
11017 | static void account_event(struct perf_event *event) |
11018 | { | |
25432ae9 PZ |
11019 | bool inc = false; |
11020 | ||
4beb31f3 FW |
11021 | if (event->parent) |
11022 | return; | |
11023 | ||
766d6c07 | 11024 | if (event->attach_state & PERF_ATTACH_TASK) |
25432ae9 | 11025 | inc = true; |
766d6c07 FW |
11026 | if (event->attr.mmap || event->attr.mmap_data) |
11027 | atomic_inc(&nr_mmap_events); | |
11028 | if (event->attr.comm) | |
11029 | atomic_inc(&nr_comm_events); | |
e4222673 HB |
11030 | if (event->attr.namespaces) |
11031 | atomic_inc(&nr_namespaces_events); | |
96aaab68 NK |
11032 | if (event->attr.cgroup) |
11033 | atomic_inc(&nr_cgroup_events); | |
766d6c07 FW |
11034 | if (event->attr.task) |
11035 | atomic_inc(&nr_task_events); | |
555e0c1e FW |
11036 | if (event->attr.freq) |
11037 | account_freq_event(); | |
45ac1403 AH |
11038 | if (event->attr.context_switch) { |
11039 | atomic_inc(&nr_switch_events); | |
25432ae9 | 11040 | inc = true; |
45ac1403 | 11041 | } |
4beb31f3 | 11042 | if (has_branch_stack(event)) |
25432ae9 | 11043 | inc = true; |
4beb31f3 | 11044 | if (is_cgroup_event(event)) |
25432ae9 | 11045 | inc = true; |
76193a94 SL |
11046 | if (event->attr.ksymbol) |
11047 | atomic_inc(&nr_ksymbol_events); | |
6ee52e2a SL |
11048 | if (event->attr.bpf_event) |
11049 | atomic_inc(&nr_bpf_events); | |
e17d43b9 AH |
11050 | if (event->attr.text_poke) |
11051 | atomic_inc(&nr_text_poke_events); | |
25432ae9 | 11052 | |
9107c89e | 11053 | if (inc) { |
5bce9db1 AS |
11054 | /* |
11055 | * We need the mutex here because static_branch_enable() | |
11056 | * must complete *before* the perf_sched_count increment | |
11057 | * becomes visible. | |
11058 | */ | |
9107c89e PZ |
11059 | if (atomic_inc_not_zero(&perf_sched_count)) |
11060 | goto enabled; | |
11061 | ||
11062 | mutex_lock(&perf_sched_mutex); | |
11063 | if (!atomic_read(&perf_sched_count)) { | |
11064 | static_branch_enable(&perf_sched_events); | |
11065 | /* | |
11066 | * Guarantee that all CPUs observe they key change and | |
11067 | * call the perf scheduling hooks before proceeding to | |
11068 | * install events that need them. | |
11069 | */ | |
0809d954 | 11070 | synchronize_rcu(); |
9107c89e PZ |
11071 | } |
11072 | /* | |
11073 | * Now that we have waited for the sync_sched(), allow further | |
11074 | * increments to by-pass the mutex. | |
11075 | */ | |
11076 | atomic_inc(&perf_sched_count); | |
11077 | mutex_unlock(&perf_sched_mutex); | |
11078 | } | |
11079 | enabled: | |
4beb31f3 FW |
11080 | |
11081 | account_event_cpu(event, event->cpu); | |
f2fb6bef KL |
11082 | |
11083 | account_pmu_sb_event(event); | |
766d6c07 FW |
11084 | } |
11085 | ||
0793a61d | 11086 | /* |
788faab7 | 11087 | * Allocate and initialize an event structure |
0793a61d | 11088 | */ |
cdd6c482 | 11089 | static struct perf_event * |
c3f00c70 | 11090 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
d580ff86 PZ |
11091 | struct task_struct *task, |
11092 | struct perf_event *group_leader, | |
11093 | struct perf_event *parent_event, | |
4dc0da86 | 11094 | perf_overflow_handler_t overflow_handler, |
79dff51e | 11095 | void *context, int cgroup_fd) |
0793a61d | 11096 | { |
51b0fe39 | 11097 | struct pmu *pmu; |
cdd6c482 IM |
11098 | struct perf_event *event; |
11099 | struct hw_perf_event *hwc; | |
90983b16 | 11100 | long err = -EINVAL; |
0793a61d | 11101 | |
66832eb4 ON |
11102 | if ((unsigned)cpu >= nr_cpu_ids) { |
11103 | if (!task || cpu != -1) | |
11104 | return ERR_PTR(-EINVAL); | |
11105 | } | |
11106 | ||
c3f00c70 | 11107 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
cdd6c482 | 11108 | if (!event) |
d5d2bc0d | 11109 | return ERR_PTR(-ENOMEM); |
0793a61d | 11110 | |
04289bb9 | 11111 | /* |
cdd6c482 | 11112 | * Single events are their own group leaders, with an |
04289bb9 IM |
11113 | * empty sibling list: |
11114 | */ | |
11115 | if (!group_leader) | |
cdd6c482 | 11116 | group_leader = event; |
04289bb9 | 11117 | |
cdd6c482 IM |
11118 | mutex_init(&event->child_mutex); |
11119 | INIT_LIST_HEAD(&event->child_list); | |
fccc714b | 11120 | |
cdd6c482 IM |
11121 | INIT_LIST_HEAD(&event->event_entry); |
11122 | INIT_LIST_HEAD(&event->sibling_list); | |
6668128a | 11123 | INIT_LIST_HEAD(&event->active_list); |
8e1a2031 | 11124 | init_event_group(event); |
10c6db11 | 11125 | INIT_LIST_HEAD(&event->rb_entry); |
71ad88ef | 11126 | INIT_LIST_HEAD(&event->active_entry); |
375637bc | 11127 | INIT_LIST_HEAD(&event->addr_filters.list); |
f3ae75de SE |
11128 | INIT_HLIST_NODE(&event->hlist_entry); |
11129 | ||
10c6db11 | 11130 | |
cdd6c482 | 11131 | init_waitqueue_head(&event->waitq); |
1d54ad94 | 11132 | event->pending_disable = -1; |
e360adbe | 11133 | init_irq_work(&event->pending, perf_pending_event); |
0793a61d | 11134 | |
cdd6c482 | 11135 | mutex_init(&event->mmap_mutex); |
375637bc | 11136 | raw_spin_lock_init(&event->addr_filters.lock); |
7b732a75 | 11137 | |
a6fa941d | 11138 | atomic_long_set(&event->refcount, 1); |
cdd6c482 IM |
11139 | event->cpu = cpu; |
11140 | event->attr = *attr; | |
11141 | event->group_leader = group_leader; | |
11142 | event->pmu = NULL; | |
cdd6c482 | 11143 | event->oncpu = -1; |
a96bbc16 | 11144 | |
cdd6c482 | 11145 | event->parent = parent_event; |
b84fbc9f | 11146 | |
17cf22c3 | 11147 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
cdd6c482 | 11148 | event->id = atomic64_inc_return(&perf_event_id); |
a96bbc16 | 11149 | |
cdd6c482 | 11150 | event->state = PERF_EVENT_STATE_INACTIVE; |
329d876d | 11151 | |
d580ff86 PZ |
11152 | if (task) { |
11153 | event->attach_state = PERF_ATTACH_TASK; | |
d580ff86 | 11154 | /* |
50f16a8b PZ |
11155 | * XXX pmu::event_init needs to know what task to account to |
11156 | * and we cannot use the ctx information because we need the | |
11157 | * pmu before we get a ctx. | |
d580ff86 | 11158 | */ |
7b3c92b8 | 11159 | event->hw.target = get_task_struct(task); |
d580ff86 PZ |
11160 | } |
11161 | ||
34f43927 PZ |
11162 | event->clock = &local_clock; |
11163 | if (parent_event) | |
11164 | event->clock = parent_event->clock; | |
11165 | ||
4dc0da86 | 11166 | if (!overflow_handler && parent_event) { |
b326e956 | 11167 | overflow_handler = parent_event->overflow_handler; |
4dc0da86 | 11168 | context = parent_event->overflow_handler_context; |
f1e4ba5b | 11169 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
aa6a5f3c | 11170 | if (overflow_handler == bpf_overflow_handler) { |
85192dbf | 11171 | struct bpf_prog *prog = parent_event->prog; |
aa6a5f3c | 11172 | |
85192dbf | 11173 | bpf_prog_inc(prog); |
aa6a5f3c AS |
11174 | event->prog = prog; |
11175 | event->orig_overflow_handler = | |
11176 | parent_event->orig_overflow_handler; | |
11177 | } | |
11178 | #endif | |
4dc0da86 | 11179 | } |
66832eb4 | 11180 | |
1879445d WN |
11181 | if (overflow_handler) { |
11182 | event->overflow_handler = overflow_handler; | |
11183 | event->overflow_handler_context = context; | |
9ecda41a WN |
11184 | } else if (is_write_backward(event)){ |
11185 | event->overflow_handler = perf_event_output_backward; | |
11186 | event->overflow_handler_context = NULL; | |
1879445d | 11187 | } else { |
9ecda41a | 11188 | event->overflow_handler = perf_event_output_forward; |
1879445d WN |
11189 | event->overflow_handler_context = NULL; |
11190 | } | |
97eaf530 | 11191 | |
0231bb53 | 11192 | perf_event__state_init(event); |
a86ed508 | 11193 | |
4aeb0b42 | 11194 | pmu = NULL; |
b8e83514 | 11195 | |
cdd6c482 | 11196 | hwc = &event->hw; |
bd2b5b12 | 11197 | hwc->sample_period = attr->sample_period; |
0d48696f | 11198 | if (attr->freq && attr->sample_freq) |
bd2b5b12 | 11199 | hwc->sample_period = 1; |
eced1dfc | 11200 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 11201 | |
e7850595 | 11202 | local64_set(&hwc->period_left, hwc->sample_period); |
60db5e09 | 11203 | |
2023b359 | 11204 | /* |
ba5213ae PZ |
11205 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
11206 | * See perf_output_read(). | |
2023b359 | 11207 | */ |
ba5213ae | 11208 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
90983b16 | 11209 | goto err_ns; |
a46a2300 YZ |
11210 | |
11211 | if (!has_branch_stack(event)) | |
11212 | event->attr.branch_sample_type = 0; | |
2023b359 | 11213 | |
b0a873eb | 11214 | pmu = perf_init_event(event); |
85c617ab | 11215 | if (IS_ERR(pmu)) { |
4aeb0b42 | 11216 | err = PTR_ERR(pmu); |
90983b16 | 11217 | goto err_ns; |
621a01ea | 11218 | } |
d5d2bc0d | 11219 | |
09f4e8f0 PZ |
11220 | /* |
11221 | * Disallow uncore-cgroup events, they don't make sense as the cgroup will | |
11222 | * be different on other CPUs in the uncore mask. | |
11223 | */ | |
11224 | if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) { | |
11225 | err = -EINVAL; | |
11226 | goto err_pmu; | |
11227 | } | |
11228 | ||
ab43762e AS |
11229 | if (event->attr.aux_output && |
11230 | !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) { | |
11231 | err = -EOPNOTSUPP; | |
11232 | goto err_pmu; | |
11233 | } | |
11234 | ||
98add2af PZ |
11235 | if (cgroup_fd != -1) { |
11236 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); | |
11237 | if (err) | |
11238 | goto err_pmu; | |
11239 | } | |
11240 | ||
bed5b25a AS |
11241 | err = exclusive_event_init(event); |
11242 | if (err) | |
11243 | goto err_pmu; | |
11244 | ||
375637bc | 11245 | if (has_addr_filter(event)) { |
c60f83b8 AS |
11246 | event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters, |
11247 | sizeof(struct perf_addr_filter_range), | |
11248 | GFP_KERNEL); | |
11249 | if (!event->addr_filter_ranges) { | |
36cc2b92 | 11250 | err = -ENOMEM; |
375637bc | 11251 | goto err_per_task; |
36cc2b92 | 11252 | } |
375637bc | 11253 | |
18736eef AS |
11254 | /* |
11255 | * Clone the parent's vma offsets: they are valid until exec() | |
11256 | * even if the mm is not shared with the parent. | |
11257 | */ | |
11258 | if (event->parent) { | |
11259 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); | |
11260 | ||
11261 | raw_spin_lock_irq(&ifh->lock); | |
c60f83b8 AS |
11262 | memcpy(event->addr_filter_ranges, |
11263 | event->parent->addr_filter_ranges, | |
11264 | pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range)); | |
18736eef AS |
11265 | raw_spin_unlock_irq(&ifh->lock); |
11266 | } | |
11267 | ||
375637bc AS |
11268 | /* force hw sync on the address filters */ |
11269 | event->addr_filters_gen = 1; | |
11270 | } | |
11271 | ||
cdd6c482 | 11272 | if (!event->parent) { |
927c7a9e | 11273 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
97c79a38 | 11274 | err = get_callchain_buffers(attr->sample_max_stack); |
90983b16 | 11275 | if (err) |
375637bc | 11276 | goto err_addr_filters; |
d010b332 | 11277 | } |
f344011c | 11278 | } |
9ee318a7 | 11279 | |
da97e184 JFG |
11280 | err = security_perf_event_alloc(event); |
11281 | if (err) | |
11282 | goto err_callchain_buffer; | |
11283 | ||
927a5570 AS |
11284 | /* symmetric to unaccount_event() in _free_event() */ |
11285 | account_event(event); | |
11286 | ||
cdd6c482 | 11287 | return event; |
90983b16 | 11288 | |
da97e184 JFG |
11289 | err_callchain_buffer: |
11290 | if (!event->parent) { | |
11291 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) | |
11292 | put_callchain_buffers(); | |
11293 | } | |
375637bc | 11294 | err_addr_filters: |
c60f83b8 | 11295 | kfree(event->addr_filter_ranges); |
375637bc | 11296 | |
bed5b25a AS |
11297 | err_per_task: |
11298 | exclusive_event_destroy(event); | |
11299 | ||
90983b16 | 11300 | err_pmu: |
98add2af PZ |
11301 | if (is_cgroup_event(event)) |
11302 | perf_detach_cgroup(event); | |
90983b16 FW |
11303 | if (event->destroy) |
11304 | event->destroy(event); | |
c464c76e | 11305 | module_put(pmu->module); |
90983b16 FW |
11306 | err_ns: |
11307 | if (event->ns) | |
11308 | put_pid_ns(event->ns); | |
621b6d2e PB |
11309 | if (event->hw.target) |
11310 | put_task_struct(event->hw.target); | |
90983b16 FW |
11311 | kfree(event); |
11312 | ||
11313 | return ERR_PTR(err); | |
0793a61d TG |
11314 | } |
11315 | ||
cdd6c482 IM |
11316 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
11317 | struct perf_event_attr *attr) | |
974802ea | 11318 | { |
974802ea | 11319 | u32 size; |
cdf8073d | 11320 | int ret; |
974802ea | 11321 | |
c2ba8f41 | 11322 | /* Zero the full structure, so that a short copy will be nice. */ |
974802ea PZ |
11323 | memset(attr, 0, sizeof(*attr)); |
11324 | ||
11325 | ret = get_user(size, &uattr->size); | |
11326 | if (ret) | |
11327 | return ret; | |
11328 | ||
c2ba8f41 AS |
11329 | /* ABI compatibility quirk: */ |
11330 | if (!size) | |
974802ea | 11331 | size = PERF_ATTR_SIZE_VER0; |
c2ba8f41 | 11332 | if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
974802ea PZ |
11333 | goto err_size; |
11334 | ||
c2ba8f41 AS |
11335 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
11336 | if (ret) { | |
11337 | if (ret == -E2BIG) | |
11338 | goto err_size; | |
11339 | return ret; | |
974802ea PZ |
11340 | } |
11341 | ||
f12f42ac MX |
11342 | attr->size = size; |
11343 | ||
a4faf00d | 11344 | if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) |
974802ea PZ |
11345 | return -EINVAL; |
11346 | ||
11347 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) | |
11348 | return -EINVAL; | |
11349 | ||
11350 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) | |
11351 | return -EINVAL; | |
11352 | ||
bce38cd5 SE |
11353 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
11354 | u64 mask = attr->branch_sample_type; | |
11355 | ||
11356 | /* only using defined bits */ | |
11357 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) | |
11358 | return -EINVAL; | |
11359 | ||
11360 | /* at least one branch bit must be set */ | |
11361 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) | |
11362 | return -EINVAL; | |
11363 | ||
bce38cd5 SE |
11364 | /* propagate priv level, when not set for branch */ |
11365 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { | |
11366 | ||
11367 | /* exclude_kernel checked on syscall entry */ | |
11368 | if (!attr->exclude_kernel) | |
11369 | mask |= PERF_SAMPLE_BRANCH_KERNEL; | |
11370 | ||
11371 | if (!attr->exclude_user) | |
11372 | mask |= PERF_SAMPLE_BRANCH_USER; | |
11373 | ||
11374 | if (!attr->exclude_hv) | |
11375 | mask |= PERF_SAMPLE_BRANCH_HV; | |
11376 | /* | |
11377 | * adjust user setting (for HW filter setup) | |
11378 | */ | |
11379 | attr->branch_sample_type = mask; | |
11380 | } | |
e712209a | 11381 | /* privileged levels capture (kernel, hv): check permissions */ |
da97e184 JFG |
11382 | if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) { |
11383 | ret = perf_allow_kernel(attr); | |
11384 | if (ret) | |
11385 | return ret; | |
11386 | } | |
bce38cd5 | 11387 | } |
4018994f | 11388 | |
c5ebcedb | 11389 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
4018994f | 11390 | ret = perf_reg_validate(attr->sample_regs_user); |
c5ebcedb JO |
11391 | if (ret) |
11392 | return ret; | |
11393 | } | |
11394 | ||
11395 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { | |
11396 | if (!arch_perf_have_user_stack_dump()) | |
11397 | return -ENOSYS; | |
11398 | ||
11399 | /* | |
11400 | * We have __u32 type for the size, but so far | |
11401 | * we can only use __u16 as maximum due to the | |
11402 | * __u16 sample size limit. | |
11403 | */ | |
11404 | if (attr->sample_stack_user >= USHRT_MAX) | |
78b562fb | 11405 | return -EINVAL; |
c5ebcedb | 11406 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
78b562fb | 11407 | return -EINVAL; |
c5ebcedb | 11408 | } |
4018994f | 11409 | |
5f970521 JO |
11410 | if (!attr->sample_max_stack) |
11411 | attr->sample_max_stack = sysctl_perf_event_max_stack; | |
11412 | ||
60e2364e SE |
11413 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
11414 | ret = perf_reg_validate(attr->sample_regs_intr); | |
6546b19f NK |
11415 | |
11416 | #ifndef CONFIG_CGROUP_PERF | |
11417 | if (attr->sample_type & PERF_SAMPLE_CGROUP) | |
11418 | return -EINVAL; | |
11419 | #endif | |
11420 | ||
974802ea PZ |
11421 | out: |
11422 | return ret; | |
11423 | ||
11424 | err_size: | |
11425 | put_user(sizeof(*attr), &uattr->size); | |
11426 | ret = -E2BIG; | |
11427 | goto out; | |
11428 | } | |
11429 | ||
ac9721f3 PZ |
11430 | static int |
11431 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) | |
a4be7c27 | 11432 | { |
56de4e8f | 11433 | struct perf_buffer *rb = NULL; |
a4be7c27 PZ |
11434 | int ret = -EINVAL; |
11435 | ||
ac9721f3 | 11436 | if (!output_event) |
a4be7c27 PZ |
11437 | goto set; |
11438 | ||
ac9721f3 PZ |
11439 | /* don't allow circular references */ |
11440 | if (event == output_event) | |
a4be7c27 PZ |
11441 | goto out; |
11442 | ||
0f139300 PZ |
11443 | /* |
11444 | * Don't allow cross-cpu buffers | |
11445 | */ | |
11446 | if (output_event->cpu != event->cpu) | |
11447 | goto out; | |
11448 | ||
11449 | /* | |
76369139 | 11450 | * If its not a per-cpu rb, it must be the same task. |
0f139300 PZ |
11451 | */ |
11452 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) | |
11453 | goto out; | |
11454 | ||
34f43927 PZ |
11455 | /* |
11456 | * Mixing clocks in the same buffer is trouble you don't need. | |
11457 | */ | |
11458 | if (output_event->clock != event->clock) | |
11459 | goto out; | |
11460 | ||
9ecda41a WN |
11461 | /* |
11462 | * Either writing ring buffer from beginning or from end. | |
11463 | * Mixing is not allowed. | |
11464 | */ | |
11465 | if (is_write_backward(output_event) != is_write_backward(event)) | |
11466 | goto out; | |
11467 | ||
45bfb2e5 PZ |
11468 | /* |
11469 | * If both events generate aux data, they must be on the same PMU | |
11470 | */ | |
11471 | if (has_aux(event) && has_aux(output_event) && | |
11472 | event->pmu != output_event->pmu) | |
11473 | goto out; | |
11474 | ||
a4be7c27 | 11475 | set: |
cdd6c482 | 11476 | mutex_lock(&event->mmap_mutex); |
ac9721f3 PZ |
11477 | /* Can't redirect output if we've got an active mmap() */ |
11478 | if (atomic_read(&event->mmap_count)) | |
11479 | goto unlock; | |
a4be7c27 | 11480 | |
ac9721f3 | 11481 | if (output_event) { |
76369139 FW |
11482 | /* get the rb we want to redirect to */ |
11483 | rb = ring_buffer_get(output_event); | |
11484 | if (!rb) | |
ac9721f3 | 11485 | goto unlock; |
a4be7c27 PZ |
11486 | } |
11487 | ||
b69cf536 | 11488 | ring_buffer_attach(event, rb); |
9bb5d40c | 11489 | |
a4be7c27 | 11490 | ret = 0; |
ac9721f3 PZ |
11491 | unlock: |
11492 | mutex_unlock(&event->mmap_mutex); | |
11493 | ||
a4be7c27 | 11494 | out: |
a4be7c27 PZ |
11495 | return ret; |
11496 | } | |
11497 | ||
f63a8daa PZ |
11498 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
11499 | { | |
11500 | if (b < a) | |
11501 | swap(a, b); | |
11502 | ||
11503 | mutex_lock(a); | |
11504 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); | |
11505 | } | |
11506 | ||
34f43927 PZ |
11507 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
11508 | { | |
11509 | bool nmi_safe = false; | |
11510 | ||
11511 | switch (clk_id) { | |
11512 | case CLOCK_MONOTONIC: | |
11513 | event->clock = &ktime_get_mono_fast_ns; | |
11514 | nmi_safe = true; | |
11515 | break; | |
11516 | ||
11517 | case CLOCK_MONOTONIC_RAW: | |
11518 | event->clock = &ktime_get_raw_fast_ns; | |
11519 | nmi_safe = true; | |
11520 | break; | |
11521 | ||
11522 | case CLOCK_REALTIME: | |
11523 | event->clock = &ktime_get_real_ns; | |
11524 | break; | |
11525 | ||
11526 | case CLOCK_BOOTTIME: | |
9285ec4c | 11527 | event->clock = &ktime_get_boottime_ns; |
34f43927 PZ |
11528 | break; |
11529 | ||
11530 | case CLOCK_TAI: | |
9285ec4c | 11531 | event->clock = &ktime_get_clocktai_ns; |
34f43927 PZ |
11532 | break; |
11533 | ||
11534 | default: | |
11535 | return -EINVAL; | |
11536 | } | |
11537 | ||
11538 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) | |
11539 | return -EINVAL; | |
11540 | ||
11541 | return 0; | |
11542 | } | |
11543 | ||
321027c1 PZ |
11544 | /* |
11545 | * Variation on perf_event_ctx_lock_nested(), except we take two context | |
11546 | * mutexes. | |
11547 | */ | |
11548 | static struct perf_event_context * | |
11549 | __perf_event_ctx_lock_double(struct perf_event *group_leader, | |
11550 | struct perf_event_context *ctx) | |
11551 | { | |
11552 | struct perf_event_context *gctx; | |
11553 | ||
11554 | again: | |
11555 | rcu_read_lock(); | |
11556 | gctx = READ_ONCE(group_leader->ctx); | |
8c94abbb | 11557 | if (!refcount_inc_not_zero(&gctx->refcount)) { |
321027c1 PZ |
11558 | rcu_read_unlock(); |
11559 | goto again; | |
11560 | } | |
11561 | rcu_read_unlock(); | |
11562 | ||
11563 | mutex_lock_double(&gctx->mutex, &ctx->mutex); | |
11564 | ||
11565 | if (group_leader->ctx != gctx) { | |
11566 | mutex_unlock(&ctx->mutex); | |
11567 | mutex_unlock(&gctx->mutex); | |
11568 | put_ctx(gctx); | |
11569 | goto again; | |
11570 | } | |
11571 | ||
11572 | return gctx; | |
11573 | } | |
11574 | ||
0793a61d | 11575 | /** |
cdd6c482 | 11576 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
9f66a381 | 11577 | * |
cdd6c482 | 11578 | * @attr_uptr: event_id type attributes for monitoring/sampling |
0793a61d | 11579 | * @pid: target pid |
9f66a381 | 11580 | * @cpu: target cpu |
cdd6c482 | 11581 | * @group_fd: group leader event fd |
0793a61d | 11582 | */ |
cdd6c482 IM |
11583 | SYSCALL_DEFINE5(perf_event_open, |
11584 | struct perf_event_attr __user *, attr_uptr, | |
2743a5b0 | 11585 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
0793a61d | 11586 | { |
b04243ef PZ |
11587 | struct perf_event *group_leader = NULL, *output_event = NULL; |
11588 | struct perf_event *event, *sibling; | |
cdd6c482 | 11589 | struct perf_event_attr attr; |
f63a8daa | 11590 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
cdd6c482 | 11591 | struct file *event_file = NULL; |
2903ff01 | 11592 | struct fd group = {NULL, 0}; |
38a81da2 | 11593 | struct task_struct *task = NULL; |
89a1e187 | 11594 | struct pmu *pmu; |
ea635c64 | 11595 | int event_fd; |
b04243ef | 11596 | int move_group = 0; |
dc86cabe | 11597 | int err; |
a21b0b35 | 11598 | int f_flags = O_RDWR; |
79dff51e | 11599 | int cgroup_fd = -1; |
0793a61d | 11600 | |
2743a5b0 | 11601 | /* for future expandability... */ |
e5d1367f | 11602 | if (flags & ~PERF_FLAG_ALL) |
2743a5b0 PM |
11603 | return -EINVAL; |
11604 | ||
da97e184 JFG |
11605 | /* Do we allow access to perf_event_open(2) ? */ |
11606 | err = security_perf_event_open(&attr, PERF_SECURITY_OPEN); | |
11607 | if (err) | |
11608 | return err; | |
11609 | ||
dc86cabe IM |
11610 | err = perf_copy_attr(attr_uptr, &attr); |
11611 | if (err) | |
11612 | return err; | |
eab656ae | 11613 | |
0764771d | 11614 | if (!attr.exclude_kernel) { |
da97e184 JFG |
11615 | err = perf_allow_kernel(&attr); |
11616 | if (err) | |
11617 | return err; | |
0764771d PZ |
11618 | } |
11619 | ||
e4222673 | 11620 | if (attr.namespaces) { |
18aa1856 | 11621 | if (!perfmon_capable()) |
e4222673 HB |
11622 | return -EACCES; |
11623 | } | |
11624 | ||
df58ab24 | 11625 | if (attr.freq) { |
cdd6c482 | 11626 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
df58ab24 | 11627 | return -EINVAL; |
0819b2e3 PZ |
11628 | } else { |
11629 | if (attr.sample_period & (1ULL << 63)) | |
11630 | return -EINVAL; | |
df58ab24 PZ |
11631 | } |
11632 | ||
fc7ce9c7 | 11633 | /* Only privileged users can get physical addresses */ |
da97e184 JFG |
11634 | if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { |
11635 | err = perf_allow_kernel(&attr); | |
11636 | if (err) | |
11637 | return err; | |
11638 | } | |
fc7ce9c7 | 11639 | |
b0c8fdc7 DH |
11640 | err = security_locked_down(LOCKDOWN_PERF); |
11641 | if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR)) | |
11642 | /* REGS_INTR can leak data, lockdown must prevent this */ | |
11643 | return err; | |
11644 | ||
11645 | err = 0; | |
11646 | ||
e5d1367f SE |
11647 | /* |
11648 | * In cgroup mode, the pid argument is used to pass the fd | |
11649 | * opened to the cgroup directory in cgroupfs. The cpu argument | |
11650 | * designates the cpu on which to monitor threads from that | |
11651 | * cgroup. | |
11652 | */ | |
11653 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) | |
11654 | return -EINVAL; | |
11655 | ||
a21b0b35 YD |
11656 | if (flags & PERF_FLAG_FD_CLOEXEC) |
11657 | f_flags |= O_CLOEXEC; | |
11658 | ||
11659 | event_fd = get_unused_fd_flags(f_flags); | |
ea635c64 AV |
11660 | if (event_fd < 0) |
11661 | return event_fd; | |
11662 | ||
ac9721f3 | 11663 | if (group_fd != -1) { |
2903ff01 AV |
11664 | err = perf_fget_light(group_fd, &group); |
11665 | if (err) | |
d14b12d7 | 11666 | goto err_fd; |
2903ff01 | 11667 | group_leader = group.file->private_data; |
ac9721f3 PZ |
11668 | if (flags & PERF_FLAG_FD_OUTPUT) |
11669 | output_event = group_leader; | |
11670 | if (flags & PERF_FLAG_FD_NO_GROUP) | |
11671 | group_leader = NULL; | |
11672 | } | |
11673 | ||
e5d1367f | 11674 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
c6be5a5c PZ |
11675 | task = find_lively_task_by_vpid(pid); |
11676 | if (IS_ERR(task)) { | |
11677 | err = PTR_ERR(task); | |
11678 | goto err_group_fd; | |
11679 | } | |
11680 | } | |
11681 | ||
1f4ee503 PZ |
11682 | if (task && group_leader && |
11683 | group_leader->attr.inherit != attr.inherit) { | |
11684 | err = -EINVAL; | |
11685 | goto err_task; | |
11686 | } | |
11687 | ||
79c9ce57 | 11688 | if (task) { |
69143038 | 11689 | err = mutex_lock_interruptible(&task->signal->exec_update_mutex); |
79c9ce57 | 11690 | if (err) |
e5aeee51 | 11691 | goto err_task; |
79c9ce57 PZ |
11692 | |
11693 | /* | |
11694 | * Reuse ptrace permission checks for now. | |
11695 | * | |
69143038 | 11696 | * We must hold exec_update_mutex across this and any potential |
79c9ce57 PZ |
11697 | * perf_install_in_context() call for this new event to |
11698 | * serialize against exec() altering our credentials (and the | |
11699 | * perf_event_exit_task() that could imply). | |
11700 | */ | |
11701 | err = -EACCES; | |
11702 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) | |
11703 | goto err_cred; | |
11704 | } | |
11705 | ||
79dff51e MF |
11706 | if (flags & PERF_FLAG_PID_CGROUP) |
11707 | cgroup_fd = pid; | |
11708 | ||
4dc0da86 | 11709 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
79dff51e | 11710 | NULL, NULL, cgroup_fd); |
d14b12d7 SE |
11711 | if (IS_ERR(event)) { |
11712 | err = PTR_ERR(event); | |
79c9ce57 | 11713 | goto err_cred; |
d14b12d7 SE |
11714 | } |
11715 | ||
53b25335 VW |
11716 | if (is_sampling_event(event)) { |
11717 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { | |
a1396555 | 11718 | err = -EOPNOTSUPP; |
53b25335 VW |
11719 | goto err_alloc; |
11720 | } | |
11721 | } | |
11722 | ||
89a1e187 PZ |
11723 | /* |
11724 | * Special case software events and allow them to be part of | |
11725 | * any hardware group. | |
11726 | */ | |
11727 | pmu = event->pmu; | |
b04243ef | 11728 | |
34f43927 PZ |
11729 | if (attr.use_clockid) { |
11730 | err = perf_event_set_clock(event, attr.clockid); | |
11731 | if (err) | |
11732 | goto err_alloc; | |
11733 | } | |
11734 | ||
4ff6a8de DCC |
11735 | if (pmu->task_ctx_nr == perf_sw_context) |
11736 | event->event_caps |= PERF_EV_CAP_SOFTWARE; | |
11737 | ||
a1150c20 SL |
11738 | if (group_leader) { |
11739 | if (is_software_event(event) && | |
11740 | !in_software_context(group_leader)) { | |
b04243ef | 11741 | /* |
a1150c20 SL |
11742 | * If the event is a sw event, but the group_leader |
11743 | * is on hw context. | |
b04243ef | 11744 | * |
a1150c20 SL |
11745 | * Allow the addition of software events to hw |
11746 | * groups, this is safe because software events | |
11747 | * never fail to schedule. | |
b04243ef | 11748 | */ |
a1150c20 SL |
11749 | pmu = group_leader->ctx->pmu; |
11750 | } else if (!is_software_event(event) && | |
11751 | is_software_event(group_leader) && | |
4ff6a8de | 11752 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
b04243ef PZ |
11753 | /* |
11754 | * In case the group is a pure software group, and we | |
11755 | * try to add a hardware event, move the whole group to | |
11756 | * the hardware context. | |
11757 | */ | |
11758 | move_group = 1; | |
11759 | } | |
11760 | } | |
89a1e187 PZ |
11761 | |
11762 | /* | |
11763 | * Get the target context (task or percpu): | |
11764 | */ | |
4af57ef2 | 11765 | ctx = find_get_context(pmu, task, event); |
89a1e187 PZ |
11766 | if (IS_ERR(ctx)) { |
11767 | err = PTR_ERR(ctx); | |
c6be5a5c | 11768 | goto err_alloc; |
89a1e187 PZ |
11769 | } |
11770 | ||
ccff286d | 11771 | /* |
cdd6c482 | 11772 | * Look up the group leader (we will attach this event to it): |
04289bb9 | 11773 | */ |
ac9721f3 | 11774 | if (group_leader) { |
dc86cabe | 11775 | err = -EINVAL; |
04289bb9 | 11776 | |
04289bb9 | 11777 | /* |
ccff286d IM |
11778 | * Do not allow a recursive hierarchy (this new sibling |
11779 | * becoming part of another group-sibling): | |
11780 | */ | |
11781 | if (group_leader->group_leader != group_leader) | |
c3f00c70 | 11782 | goto err_context; |
34f43927 PZ |
11783 | |
11784 | /* All events in a group should have the same clock */ | |
11785 | if (group_leader->clock != event->clock) | |
11786 | goto err_context; | |
11787 | ||
ccff286d | 11788 | /* |
64aee2a9 MR |
11789 | * Make sure we're both events for the same CPU; |
11790 | * grouping events for different CPUs is broken; since | |
11791 | * you can never concurrently schedule them anyhow. | |
04289bb9 | 11792 | */ |
64aee2a9 MR |
11793 | if (group_leader->cpu != event->cpu) |
11794 | goto err_context; | |
c3c87e77 | 11795 | |
64aee2a9 MR |
11796 | /* |
11797 | * Make sure we're both on the same task, or both | |
11798 | * per-CPU events. | |
11799 | */ | |
11800 | if (group_leader->ctx->task != ctx->task) | |
11801 | goto err_context; | |
11802 | ||
11803 | /* | |
11804 | * Do not allow to attach to a group in a different task | |
11805 | * or CPU context. If we're moving SW events, we'll fix | |
11806 | * this up later, so allow that. | |
11807 | */ | |
11808 | if (!move_group && group_leader->ctx != ctx) | |
11809 | goto err_context; | |
b04243ef | 11810 | |
3b6f9e5c PM |
11811 | /* |
11812 | * Only a group leader can be exclusive or pinned | |
11813 | */ | |
0d48696f | 11814 | if (attr.exclusive || attr.pinned) |
c3f00c70 | 11815 | goto err_context; |
ac9721f3 PZ |
11816 | } |
11817 | ||
11818 | if (output_event) { | |
11819 | err = perf_event_set_output(event, output_event); | |
11820 | if (err) | |
c3f00c70 | 11821 | goto err_context; |
ac9721f3 | 11822 | } |
0793a61d | 11823 | |
a21b0b35 YD |
11824 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
11825 | f_flags); | |
ea635c64 AV |
11826 | if (IS_ERR(event_file)) { |
11827 | err = PTR_ERR(event_file); | |
201c2f85 | 11828 | event_file = NULL; |
c3f00c70 | 11829 | goto err_context; |
ea635c64 | 11830 | } |
9b51f66d | 11831 | |
b04243ef | 11832 | if (move_group) { |
321027c1 PZ |
11833 | gctx = __perf_event_ctx_lock_double(group_leader, ctx); |
11834 | ||
84c4e620 PZ |
11835 | if (gctx->task == TASK_TOMBSTONE) { |
11836 | err = -ESRCH; | |
11837 | goto err_locked; | |
11838 | } | |
321027c1 PZ |
11839 | |
11840 | /* | |
11841 | * Check if we raced against another sys_perf_event_open() call | |
11842 | * moving the software group underneath us. | |
11843 | */ | |
11844 | if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { | |
11845 | /* | |
11846 | * If someone moved the group out from under us, check | |
11847 | * if this new event wound up on the same ctx, if so | |
11848 | * its the regular !move_group case, otherwise fail. | |
11849 | */ | |
11850 | if (gctx != ctx) { | |
11851 | err = -EINVAL; | |
11852 | goto err_locked; | |
11853 | } else { | |
11854 | perf_event_ctx_unlock(group_leader, gctx); | |
11855 | move_group = 0; | |
11856 | } | |
11857 | } | |
8a58ddae AS |
11858 | |
11859 | /* | |
11860 | * Failure to create exclusive events returns -EBUSY. | |
11861 | */ | |
11862 | err = -EBUSY; | |
11863 | if (!exclusive_event_installable(group_leader, ctx)) | |
11864 | goto err_locked; | |
11865 | ||
11866 | for_each_sibling_event(sibling, group_leader) { | |
11867 | if (!exclusive_event_installable(sibling, ctx)) | |
11868 | goto err_locked; | |
11869 | } | |
f55fc2a5 PZ |
11870 | } else { |
11871 | mutex_lock(&ctx->mutex); | |
11872 | } | |
11873 | ||
84c4e620 PZ |
11874 | if (ctx->task == TASK_TOMBSTONE) { |
11875 | err = -ESRCH; | |
11876 | goto err_locked; | |
11877 | } | |
11878 | ||
a723968c PZ |
11879 | if (!perf_event_validate_size(event)) { |
11880 | err = -E2BIG; | |
11881 | goto err_locked; | |
11882 | } | |
11883 | ||
a63fbed7 TG |
11884 | if (!task) { |
11885 | /* | |
11886 | * Check if the @cpu we're creating an event for is online. | |
11887 | * | |
11888 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
11889 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
11890 | */ | |
11891 | struct perf_cpu_context *cpuctx = | |
11892 | container_of(ctx, struct perf_cpu_context, ctx); | |
11893 | ||
11894 | if (!cpuctx->online) { | |
11895 | err = -ENODEV; | |
11896 | goto err_locked; | |
11897 | } | |
11898 | } | |
11899 | ||
da9ec3d3 MR |
11900 | if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) { |
11901 | err = -EINVAL; | |
ab43762e | 11902 | goto err_locked; |
da9ec3d3 | 11903 | } |
a63fbed7 | 11904 | |
f55fc2a5 PZ |
11905 | /* |
11906 | * Must be under the same ctx::mutex as perf_install_in_context(), | |
11907 | * because we need to serialize with concurrent event creation. | |
11908 | */ | |
11909 | if (!exclusive_event_installable(event, ctx)) { | |
f55fc2a5 PZ |
11910 | err = -EBUSY; |
11911 | goto err_locked; | |
11912 | } | |
f63a8daa | 11913 | |
f55fc2a5 PZ |
11914 | WARN_ON_ONCE(ctx->parent_ctx); |
11915 | ||
79c9ce57 PZ |
11916 | /* |
11917 | * This is the point on no return; we cannot fail hereafter. This is | |
11918 | * where we start modifying current state. | |
11919 | */ | |
11920 | ||
f55fc2a5 | 11921 | if (move_group) { |
f63a8daa PZ |
11922 | /* |
11923 | * See perf_event_ctx_lock() for comments on the details | |
11924 | * of swizzling perf_event::ctx. | |
11925 | */ | |
45a0e07a | 11926 | perf_remove_from_context(group_leader, 0); |
279b5165 | 11927 | put_ctx(gctx); |
0231bb53 | 11928 | |
edb39592 | 11929 | for_each_sibling_event(sibling, group_leader) { |
45a0e07a | 11930 | perf_remove_from_context(sibling, 0); |
b04243ef PZ |
11931 | put_ctx(gctx); |
11932 | } | |
b04243ef | 11933 | |
f63a8daa PZ |
11934 | /* |
11935 | * Wait for everybody to stop referencing the events through | |
11936 | * the old lists, before installing it on new lists. | |
11937 | */ | |
0cda4c02 | 11938 | synchronize_rcu(); |
f63a8daa | 11939 | |
8f95b435 PZI |
11940 | /* |
11941 | * Install the group siblings before the group leader. | |
11942 | * | |
11943 | * Because a group leader will try and install the entire group | |
11944 | * (through the sibling list, which is still in-tact), we can | |
11945 | * end up with siblings installed in the wrong context. | |
11946 | * | |
11947 | * By installing siblings first we NO-OP because they're not | |
11948 | * reachable through the group lists. | |
11949 | */ | |
edb39592 | 11950 | for_each_sibling_event(sibling, group_leader) { |
8f95b435 | 11951 | perf_event__state_init(sibling); |
9fc81d87 | 11952 | perf_install_in_context(ctx, sibling, sibling->cpu); |
b04243ef PZ |
11953 | get_ctx(ctx); |
11954 | } | |
8f95b435 PZI |
11955 | |
11956 | /* | |
11957 | * Removing from the context ends up with disabled | |
11958 | * event. What we want here is event in the initial | |
11959 | * startup state, ready to be add into new context. | |
11960 | */ | |
11961 | perf_event__state_init(group_leader); | |
11962 | perf_install_in_context(ctx, group_leader, group_leader->cpu); | |
11963 | get_ctx(ctx); | |
bed5b25a AS |
11964 | } |
11965 | ||
f73e22ab PZ |
11966 | /* |
11967 | * Precalculate sample_data sizes; do while holding ctx::mutex such | |
11968 | * that we're serialized against further additions and before | |
11969 | * perf_install_in_context() which is the point the event is active and | |
11970 | * can use these values. | |
11971 | */ | |
11972 | perf_event__header_size(event); | |
11973 | perf_event__id_header_size(event); | |
11974 | ||
78cd2c74 PZ |
11975 | event->owner = current; |
11976 | ||
e2d37cd2 | 11977 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 11978 | perf_unpin_context(ctx); |
f63a8daa | 11979 | |
f55fc2a5 | 11980 | if (move_group) |
321027c1 | 11981 | perf_event_ctx_unlock(group_leader, gctx); |
d859e29f | 11982 | mutex_unlock(&ctx->mutex); |
9b51f66d | 11983 | |
79c9ce57 | 11984 | if (task) { |
69143038 | 11985 | mutex_unlock(&task->signal->exec_update_mutex); |
79c9ce57 PZ |
11986 | put_task_struct(task); |
11987 | } | |
11988 | ||
cdd6c482 IM |
11989 | mutex_lock(¤t->perf_event_mutex); |
11990 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); | |
11991 | mutex_unlock(¤t->perf_event_mutex); | |
082ff5a2 | 11992 | |
8a49542c PZ |
11993 | /* |
11994 | * Drop the reference on the group_event after placing the | |
11995 | * new event on the sibling_list. This ensures destruction | |
11996 | * of the group leader will find the pointer to itself in | |
11997 | * perf_group_detach(). | |
11998 | */ | |
2903ff01 | 11999 | fdput(group); |
ea635c64 AV |
12000 | fd_install(event_fd, event_file); |
12001 | return event_fd; | |
0793a61d | 12002 | |
f55fc2a5 PZ |
12003 | err_locked: |
12004 | if (move_group) | |
321027c1 | 12005 | perf_event_ctx_unlock(group_leader, gctx); |
f55fc2a5 PZ |
12006 | mutex_unlock(&ctx->mutex); |
12007 | /* err_file: */ | |
12008 | fput(event_file); | |
c3f00c70 | 12009 | err_context: |
fe4b04fa | 12010 | perf_unpin_context(ctx); |
ea635c64 | 12011 | put_ctx(ctx); |
c6be5a5c | 12012 | err_alloc: |
13005627 PZ |
12013 | /* |
12014 | * If event_file is set, the fput() above will have called ->release() | |
12015 | * and that will take care of freeing the event. | |
12016 | */ | |
12017 | if (!event_file) | |
12018 | free_event(event); | |
79c9ce57 PZ |
12019 | err_cred: |
12020 | if (task) | |
69143038 | 12021 | mutex_unlock(&task->signal->exec_update_mutex); |
1f4ee503 | 12022 | err_task: |
e7d0bc04 PZ |
12023 | if (task) |
12024 | put_task_struct(task); | |
89a1e187 | 12025 | err_group_fd: |
2903ff01 | 12026 | fdput(group); |
ea635c64 AV |
12027 | err_fd: |
12028 | put_unused_fd(event_fd); | |
dc86cabe | 12029 | return err; |
0793a61d TG |
12030 | } |
12031 | ||
fb0459d7 AV |
12032 | /** |
12033 | * perf_event_create_kernel_counter | |
12034 | * | |
12035 | * @attr: attributes of the counter to create | |
12036 | * @cpu: cpu in which the counter is bound | |
38a81da2 | 12037 | * @task: task to profile (NULL for percpu) |
fb0459d7 AV |
12038 | */ |
12039 | struct perf_event * | |
12040 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, | |
38a81da2 | 12041 | struct task_struct *task, |
4dc0da86 AK |
12042 | perf_overflow_handler_t overflow_handler, |
12043 | void *context) | |
fb0459d7 | 12044 | { |
fb0459d7 | 12045 | struct perf_event_context *ctx; |
c3f00c70 | 12046 | struct perf_event *event; |
fb0459d7 | 12047 | int err; |
d859e29f | 12048 | |
dce5affb AS |
12049 | /* |
12050 | * Grouping is not supported for kernel events, neither is 'AUX', | |
12051 | * make sure the caller's intentions are adjusted. | |
12052 | */ | |
12053 | if (attr->aux_output) | |
12054 | return ERR_PTR(-EINVAL); | |
12055 | ||
4dc0da86 | 12056 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
79dff51e | 12057 | overflow_handler, context, -1); |
c3f00c70 PZ |
12058 | if (IS_ERR(event)) { |
12059 | err = PTR_ERR(event); | |
12060 | goto err; | |
12061 | } | |
d859e29f | 12062 | |
f8697762 | 12063 | /* Mark owner so we could distinguish it from user events. */ |
63b6da39 | 12064 | event->owner = TASK_TOMBSTONE; |
f8697762 | 12065 | |
f25d8ba9 AS |
12066 | /* |
12067 | * Get the target context (task or percpu): | |
12068 | */ | |
4af57ef2 | 12069 | ctx = find_get_context(event->pmu, task, event); |
c6567f64 FW |
12070 | if (IS_ERR(ctx)) { |
12071 | err = PTR_ERR(ctx); | |
c3f00c70 | 12072 | goto err_free; |
d859e29f | 12073 | } |
fb0459d7 | 12074 | |
fb0459d7 AV |
12075 | WARN_ON_ONCE(ctx->parent_ctx); |
12076 | mutex_lock(&ctx->mutex); | |
84c4e620 PZ |
12077 | if (ctx->task == TASK_TOMBSTONE) { |
12078 | err = -ESRCH; | |
12079 | goto err_unlock; | |
12080 | } | |
12081 | ||
a63fbed7 TG |
12082 | if (!task) { |
12083 | /* | |
12084 | * Check if the @cpu we're creating an event for is online. | |
12085 | * | |
12086 | * We use the perf_cpu_context::ctx::mutex to serialize against | |
12087 | * the hotplug notifiers. See perf_event_{init,exit}_cpu(). | |
12088 | */ | |
12089 | struct perf_cpu_context *cpuctx = | |
12090 | container_of(ctx, struct perf_cpu_context, ctx); | |
12091 | if (!cpuctx->online) { | |
12092 | err = -ENODEV; | |
12093 | goto err_unlock; | |
12094 | } | |
12095 | } | |
12096 | ||
bed5b25a | 12097 | if (!exclusive_event_installable(event, ctx)) { |
bed5b25a | 12098 | err = -EBUSY; |
84c4e620 | 12099 | goto err_unlock; |
bed5b25a AS |
12100 | } |
12101 | ||
4ce54af8 | 12102 | perf_install_in_context(ctx, event, event->cpu); |
fe4b04fa | 12103 | perf_unpin_context(ctx); |
fb0459d7 AV |
12104 | mutex_unlock(&ctx->mutex); |
12105 | ||
fb0459d7 AV |
12106 | return event; |
12107 | ||
84c4e620 PZ |
12108 | err_unlock: |
12109 | mutex_unlock(&ctx->mutex); | |
12110 | perf_unpin_context(ctx); | |
12111 | put_ctx(ctx); | |
c3f00c70 PZ |
12112 | err_free: |
12113 | free_event(event); | |
12114 | err: | |
c6567f64 | 12115 | return ERR_PTR(err); |
9b51f66d | 12116 | } |
fb0459d7 | 12117 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
9b51f66d | 12118 | |
0cda4c02 YZ |
12119 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
12120 | { | |
12121 | struct perf_event_context *src_ctx; | |
12122 | struct perf_event_context *dst_ctx; | |
12123 | struct perf_event *event, *tmp; | |
12124 | LIST_HEAD(events); | |
12125 | ||
12126 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; | |
12127 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; | |
12128 | ||
f63a8daa PZ |
12129 | /* |
12130 | * See perf_event_ctx_lock() for comments on the details | |
12131 | * of swizzling perf_event::ctx. | |
12132 | */ | |
12133 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
0cda4c02 YZ |
12134 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
12135 | event_entry) { | |
45a0e07a | 12136 | perf_remove_from_context(event, 0); |
9a545de0 | 12137 | unaccount_event_cpu(event, src_cpu); |
0cda4c02 | 12138 | put_ctx(src_ctx); |
9886167d | 12139 | list_add(&event->migrate_entry, &events); |
0cda4c02 | 12140 | } |
0cda4c02 | 12141 | |
8f95b435 PZI |
12142 | /* |
12143 | * Wait for the events to quiesce before re-instating them. | |
12144 | */ | |
0cda4c02 YZ |
12145 | synchronize_rcu(); |
12146 | ||
8f95b435 PZI |
12147 | /* |
12148 | * Re-instate events in 2 passes. | |
12149 | * | |
12150 | * Skip over group leaders and only install siblings on this first | |
12151 | * pass, siblings will not get enabled without a leader, however a | |
12152 | * leader will enable its siblings, even if those are still on the old | |
12153 | * context. | |
12154 | */ | |
12155 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { | |
12156 | if (event->group_leader == event) | |
12157 | continue; | |
12158 | ||
12159 | list_del(&event->migrate_entry); | |
12160 | if (event->state >= PERF_EVENT_STATE_OFF) | |
12161 | event->state = PERF_EVENT_STATE_INACTIVE; | |
12162 | account_event_cpu(event, dst_cpu); | |
12163 | perf_install_in_context(dst_ctx, event, dst_cpu); | |
12164 | get_ctx(dst_ctx); | |
12165 | } | |
12166 | ||
12167 | /* | |
12168 | * Once all the siblings are setup properly, install the group leaders | |
12169 | * to make it go. | |
12170 | */ | |
9886167d PZ |
12171 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
12172 | list_del(&event->migrate_entry); | |
0cda4c02 YZ |
12173 | if (event->state >= PERF_EVENT_STATE_OFF) |
12174 | event->state = PERF_EVENT_STATE_INACTIVE; | |
9a545de0 | 12175 | account_event_cpu(event, dst_cpu); |
0cda4c02 YZ |
12176 | perf_install_in_context(dst_ctx, event, dst_cpu); |
12177 | get_ctx(dst_ctx); | |
12178 | } | |
12179 | mutex_unlock(&dst_ctx->mutex); | |
f63a8daa | 12180 | mutex_unlock(&src_ctx->mutex); |
0cda4c02 YZ |
12181 | } |
12182 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); | |
12183 | ||
cdd6c482 | 12184 | static void sync_child_event(struct perf_event *child_event, |
38b200d6 | 12185 | struct task_struct *child) |
d859e29f | 12186 | { |
cdd6c482 | 12187 | struct perf_event *parent_event = child_event->parent; |
8bc20959 | 12188 | u64 child_val; |
d859e29f | 12189 | |
cdd6c482 IM |
12190 | if (child_event->attr.inherit_stat) |
12191 | perf_event_read_event(child_event, child); | |
38b200d6 | 12192 | |
b5e58793 | 12193 | child_val = perf_event_count(child_event); |
d859e29f PM |
12194 | |
12195 | /* | |
12196 | * Add back the child's count to the parent's count: | |
12197 | */ | |
a6e6dea6 | 12198 | atomic64_add(child_val, &parent_event->child_count); |
cdd6c482 IM |
12199 | atomic64_add(child_event->total_time_enabled, |
12200 | &parent_event->child_total_time_enabled); | |
12201 | atomic64_add(child_event->total_time_running, | |
12202 | &parent_event->child_total_time_running); | |
d859e29f PM |
12203 | } |
12204 | ||
9b51f66d | 12205 | static void |
8ba289b8 PZ |
12206 | perf_event_exit_event(struct perf_event *child_event, |
12207 | struct perf_event_context *child_ctx, | |
12208 | struct task_struct *child) | |
9b51f66d | 12209 | { |
8ba289b8 PZ |
12210 | struct perf_event *parent_event = child_event->parent; |
12211 | ||
1903d50c PZ |
12212 | /* |
12213 | * Do not destroy the 'original' grouping; because of the context | |
12214 | * switch optimization the original events could've ended up in a | |
12215 | * random child task. | |
12216 | * | |
12217 | * If we were to destroy the original group, all group related | |
12218 | * operations would cease to function properly after this random | |
12219 | * child dies. | |
12220 | * | |
12221 | * Do destroy all inherited groups, we don't care about those | |
12222 | * and being thorough is better. | |
12223 | */ | |
32132a3d PZ |
12224 | raw_spin_lock_irq(&child_ctx->lock); |
12225 | WARN_ON_ONCE(child_ctx->is_active); | |
12226 | ||
8ba289b8 | 12227 | if (parent_event) |
32132a3d PZ |
12228 | perf_group_detach(child_event); |
12229 | list_del_event(child_event, child_ctx); | |
0d3d73aa | 12230 | perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */ |
32132a3d | 12231 | raw_spin_unlock_irq(&child_ctx->lock); |
0cc0c027 | 12232 | |
9b51f66d | 12233 | /* |
8ba289b8 | 12234 | * Parent events are governed by their filedesc, retain them. |
9b51f66d | 12235 | */ |
8ba289b8 | 12236 | if (!parent_event) { |
179033b3 | 12237 | perf_event_wakeup(child_event); |
8ba289b8 | 12238 | return; |
4bcf349a | 12239 | } |
8ba289b8 PZ |
12240 | /* |
12241 | * Child events can be cleaned up. | |
12242 | */ | |
12243 | ||
12244 | sync_child_event(child_event, child); | |
12245 | ||
12246 | /* | |
12247 | * Remove this event from the parent's list | |
12248 | */ | |
12249 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); | |
12250 | mutex_lock(&parent_event->child_mutex); | |
12251 | list_del_init(&child_event->child_list); | |
12252 | mutex_unlock(&parent_event->child_mutex); | |
12253 | ||
12254 | /* | |
12255 | * Kick perf_poll() for is_event_hup(). | |
12256 | */ | |
12257 | perf_event_wakeup(parent_event); | |
12258 | free_event(child_event); | |
12259 | put_event(parent_event); | |
9b51f66d IM |
12260 | } |
12261 | ||
8dc85d54 | 12262 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
9b51f66d | 12263 | { |
211de6eb | 12264 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
63b6da39 | 12265 | struct perf_event *child_event, *next; |
63b6da39 PZ |
12266 | |
12267 | WARN_ON_ONCE(child != current); | |
9b51f66d | 12268 | |
6a3351b6 | 12269 | child_ctx = perf_pin_task_context(child, ctxn); |
63b6da39 | 12270 | if (!child_ctx) |
9b51f66d IM |
12271 | return; |
12272 | ||
ad3a37de | 12273 | /* |
6a3351b6 PZ |
12274 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
12275 | * ctx::mutex over the entire thing. This serializes against almost | |
12276 | * everything that wants to access the ctx. | |
12277 | * | |
12278 | * The exception is sys_perf_event_open() / | |
12279 | * perf_event_create_kernel_count() which does find_get_context() | |
12280 | * without ctx::mutex (it cannot because of the move_group double mutex | |
12281 | * lock thing). See the comments in perf_install_in_context(). | |
ad3a37de | 12282 | */ |
6a3351b6 | 12283 | mutex_lock(&child_ctx->mutex); |
c93f7669 PM |
12284 | |
12285 | /* | |
6a3351b6 PZ |
12286 | * In a single ctx::lock section, de-schedule the events and detach the |
12287 | * context from the task such that we cannot ever get it scheduled back | |
12288 | * in. | |
c93f7669 | 12289 | */ |
6a3351b6 | 12290 | raw_spin_lock_irq(&child_ctx->lock); |
487f05e1 | 12291 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL); |
4a1c0f26 | 12292 | |
71a851b4 | 12293 | /* |
63b6da39 PZ |
12294 | * Now that the context is inactive, destroy the task <-> ctx relation |
12295 | * and mark the context dead. | |
71a851b4 | 12296 | */ |
63b6da39 PZ |
12297 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
12298 | put_ctx(child_ctx); /* cannot be last */ | |
12299 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); | |
12300 | put_task_struct(current); /* cannot be last */ | |
4a1c0f26 | 12301 | |
211de6eb | 12302 | clone_ctx = unclone_ctx(child_ctx); |
6a3351b6 | 12303 | raw_spin_unlock_irq(&child_ctx->lock); |
9f498cc5 | 12304 | |
211de6eb PZ |
12305 | if (clone_ctx) |
12306 | put_ctx(clone_ctx); | |
4a1c0f26 | 12307 | |
9f498cc5 | 12308 | /* |
cdd6c482 IM |
12309 | * Report the task dead after unscheduling the events so that we |
12310 | * won't get any samples after PERF_RECORD_EXIT. We can however still | |
12311 | * get a few PERF_RECORD_READ events. | |
9f498cc5 | 12312 | */ |
cdd6c482 | 12313 | perf_event_task(child, child_ctx, 0); |
a63eaf34 | 12314 | |
ebf905fc | 12315 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
8ba289b8 | 12316 | perf_event_exit_event(child_event, child_ctx, child); |
8bc20959 | 12317 | |
a63eaf34 PM |
12318 | mutex_unlock(&child_ctx->mutex); |
12319 | ||
12320 | put_ctx(child_ctx); | |
9b51f66d IM |
12321 | } |
12322 | ||
8dc85d54 PZ |
12323 | /* |
12324 | * When a child task exits, feed back event values to parent events. | |
79c9ce57 | 12325 | * |
69143038 | 12326 | * Can be called with exec_update_mutex held when called from |
96ecee29 | 12327 | * setup_new_exec(). |
8dc85d54 PZ |
12328 | */ |
12329 | void perf_event_exit_task(struct task_struct *child) | |
12330 | { | |
8882135b | 12331 | struct perf_event *event, *tmp; |
8dc85d54 PZ |
12332 | int ctxn; |
12333 | ||
8882135b PZ |
12334 | mutex_lock(&child->perf_event_mutex); |
12335 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, | |
12336 | owner_entry) { | |
12337 | list_del_init(&event->owner_entry); | |
12338 | ||
12339 | /* | |
12340 | * Ensure the list deletion is visible before we clear | |
12341 | * the owner, closes a race against perf_release() where | |
12342 | * we need to serialize on the owner->perf_event_mutex. | |
12343 | */ | |
f47c02c0 | 12344 | smp_store_release(&event->owner, NULL); |
8882135b PZ |
12345 | } |
12346 | mutex_unlock(&child->perf_event_mutex); | |
12347 | ||
8dc85d54 PZ |
12348 | for_each_task_context_nr(ctxn) |
12349 | perf_event_exit_task_context(child, ctxn); | |
4e93ad60 JO |
12350 | |
12351 | /* | |
12352 | * The perf_event_exit_task_context calls perf_event_task | |
12353 | * with child's task_ctx, which generates EXIT events for | |
12354 | * child contexts and sets child->perf_event_ctxp[] to NULL. | |
12355 | * At this point we need to send EXIT events to cpu contexts. | |
12356 | */ | |
12357 | perf_event_task(child, NULL, 0); | |
8dc85d54 PZ |
12358 | } |
12359 | ||
889ff015 FW |
12360 | static void perf_free_event(struct perf_event *event, |
12361 | struct perf_event_context *ctx) | |
12362 | { | |
12363 | struct perf_event *parent = event->parent; | |
12364 | ||
12365 | if (WARN_ON_ONCE(!parent)) | |
12366 | return; | |
12367 | ||
12368 | mutex_lock(&parent->child_mutex); | |
12369 | list_del_init(&event->child_list); | |
12370 | mutex_unlock(&parent->child_mutex); | |
12371 | ||
a6fa941d | 12372 | put_event(parent); |
889ff015 | 12373 | |
652884fe | 12374 | raw_spin_lock_irq(&ctx->lock); |
8a49542c | 12375 | perf_group_detach(event); |
889ff015 | 12376 | list_del_event(event, ctx); |
652884fe | 12377 | raw_spin_unlock_irq(&ctx->lock); |
889ff015 FW |
12378 | free_event(event); |
12379 | } | |
12380 | ||
bbbee908 | 12381 | /* |
1cf8dfe8 PZ |
12382 | * Free a context as created by inheritance by perf_event_init_task() below, |
12383 | * used by fork() in case of fail. | |
652884fe | 12384 | * |
1cf8dfe8 PZ |
12385 | * Even though the task has never lived, the context and events have been |
12386 | * exposed through the child_list, so we must take care tearing it all down. | |
bbbee908 | 12387 | */ |
cdd6c482 | 12388 | void perf_event_free_task(struct task_struct *task) |
bbbee908 | 12389 | { |
8dc85d54 | 12390 | struct perf_event_context *ctx; |
cdd6c482 | 12391 | struct perf_event *event, *tmp; |
8dc85d54 | 12392 | int ctxn; |
bbbee908 | 12393 | |
8dc85d54 PZ |
12394 | for_each_task_context_nr(ctxn) { |
12395 | ctx = task->perf_event_ctxp[ctxn]; | |
12396 | if (!ctx) | |
12397 | continue; | |
bbbee908 | 12398 | |
8dc85d54 | 12399 | mutex_lock(&ctx->mutex); |
e552a838 PZ |
12400 | raw_spin_lock_irq(&ctx->lock); |
12401 | /* | |
12402 | * Destroy the task <-> ctx relation and mark the context dead. | |
12403 | * | |
12404 | * This is important because even though the task hasn't been | |
12405 | * exposed yet the context has been (through child_list). | |
12406 | */ | |
12407 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); | |
12408 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | |
12409 | put_task_struct(task); /* cannot be last */ | |
12410 | raw_spin_unlock_irq(&ctx->lock); | |
bbbee908 | 12411 | |
15121c78 | 12412 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) |
8dc85d54 | 12413 | perf_free_event(event, ctx); |
bbbee908 | 12414 | |
8dc85d54 | 12415 | mutex_unlock(&ctx->mutex); |
1cf8dfe8 PZ |
12416 | |
12417 | /* | |
12418 | * perf_event_release_kernel() could've stolen some of our | |
12419 | * child events and still have them on its free_list. In that | |
12420 | * case we must wait for these events to have been freed (in | |
12421 | * particular all their references to this task must've been | |
12422 | * dropped). | |
12423 | * | |
12424 | * Without this copy_process() will unconditionally free this | |
12425 | * task (irrespective of its reference count) and | |
12426 | * _free_event()'s put_task_struct(event->hw.target) will be a | |
12427 | * use-after-free. | |
12428 | * | |
12429 | * Wait for all events to drop their context reference. | |
12430 | */ | |
12431 | wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1); | |
12432 | put_ctx(ctx); /* must be last */ | |
8dc85d54 | 12433 | } |
889ff015 FW |
12434 | } |
12435 | ||
4e231c79 PZ |
12436 | void perf_event_delayed_put(struct task_struct *task) |
12437 | { | |
12438 | int ctxn; | |
12439 | ||
12440 | for_each_task_context_nr(ctxn) | |
12441 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); | |
12442 | } | |
12443 | ||
e03e7ee3 | 12444 | struct file *perf_event_get(unsigned int fd) |
ffe8690c | 12445 | { |
02e5ad97 | 12446 | struct file *file = fget(fd); |
e03e7ee3 AS |
12447 | if (!file) |
12448 | return ERR_PTR(-EBADF); | |
ffe8690c | 12449 | |
e03e7ee3 AS |
12450 | if (file->f_op != &perf_fops) { |
12451 | fput(file); | |
12452 | return ERR_PTR(-EBADF); | |
12453 | } | |
ffe8690c | 12454 | |
e03e7ee3 | 12455 | return file; |
ffe8690c KX |
12456 | } |
12457 | ||
f8d959a5 YS |
12458 | const struct perf_event *perf_get_event(struct file *file) |
12459 | { | |
12460 | if (file->f_op != &perf_fops) | |
12461 | return ERR_PTR(-EINVAL); | |
12462 | ||
12463 | return file->private_data; | |
12464 | } | |
12465 | ||
ffe8690c KX |
12466 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
12467 | { | |
12468 | if (!event) | |
12469 | return ERR_PTR(-EINVAL); | |
12470 | ||
12471 | return &event->attr; | |
12472 | } | |
12473 | ||
97dee4f3 | 12474 | /* |
788faab7 | 12475 | * Inherit an event from parent task to child task. |
d8a8cfc7 PZ |
12476 | * |
12477 | * Returns: | |
12478 | * - valid pointer on success | |
12479 | * - NULL for orphaned events | |
12480 | * - IS_ERR() on error | |
97dee4f3 PZ |
12481 | */ |
12482 | static struct perf_event * | |
12483 | inherit_event(struct perf_event *parent_event, | |
12484 | struct task_struct *parent, | |
12485 | struct perf_event_context *parent_ctx, | |
12486 | struct task_struct *child, | |
12487 | struct perf_event *group_leader, | |
12488 | struct perf_event_context *child_ctx) | |
12489 | { | |
8ca2bd41 | 12490 | enum perf_event_state parent_state = parent_event->state; |
97dee4f3 | 12491 | struct perf_event *child_event; |
cee010ec | 12492 | unsigned long flags; |
97dee4f3 PZ |
12493 | |
12494 | /* | |
12495 | * Instead of creating recursive hierarchies of events, | |
12496 | * we link inherited events back to the original parent, | |
12497 | * which has a filp for sure, which we use as the reference | |
12498 | * count: | |
12499 | */ | |
12500 | if (parent_event->parent) | |
12501 | parent_event = parent_event->parent; | |
12502 | ||
12503 | child_event = perf_event_alloc(&parent_event->attr, | |
12504 | parent_event->cpu, | |
d580ff86 | 12505 | child, |
97dee4f3 | 12506 | group_leader, parent_event, |
79dff51e | 12507 | NULL, NULL, -1); |
97dee4f3 PZ |
12508 | if (IS_ERR(child_event)) |
12509 | return child_event; | |
a6fa941d | 12510 | |
313ccb96 JO |
12511 | |
12512 | if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) && | |
12513 | !child_ctx->task_ctx_data) { | |
12514 | struct pmu *pmu = child_event->pmu; | |
12515 | ||
ff9ff926 | 12516 | child_ctx->task_ctx_data = alloc_task_ctx_data(pmu); |
313ccb96 JO |
12517 | if (!child_ctx->task_ctx_data) { |
12518 | free_event(child_event); | |
697d8778 | 12519 | return ERR_PTR(-ENOMEM); |
313ccb96 JO |
12520 | } |
12521 | } | |
12522 | ||
c6e5b732 PZ |
12523 | /* |
12524 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) | |
12525 | * must be under the same lock in order to serialize against | |
12526 | * perf_event_release_kernel(), such that either we must observe | |
12527 | * is_orphaned_event() or they will observe us on the child_list. | |
12528 | */ | |
12529 | mutex_lock(&parent_event->child_mutex); | |
fadfe7be JO |
12530 | if (is_orphaned_event(parent_event) || |
12531 | !atomic_long_inc_not_zero(&parent_event->refcount)) { | |
c6e5b732 | 12532 | mutex_unlock(&parent_event->child_mutex); |
313ccb96 | 12533 | /* task_ctx_data is freed with child_ctx */ |
a6fa941d AV |
12534 | free_event(child_event); |
12535 | return NULL; | |
12536 | } | |
12537 | ||
97dee4f3 PZ |
12538 | get_ctx(child_ctx); |
12539 | ||
12540 | /* | |
12541 | * Make the child state follow the state of the parent event, | |
12542 | * not its attr.disabled bit. We hold the parent's mutex, | |
12543 | * so we won't race with perf_event_{en, dis}able_family. | |
12544 | */ | |
1929def9 | 12545 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
97dee4f3 PZ |
12546 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
12547 | else | |
12548 | child_event->state = PERF_EVENT_STATE_OFF; | |
12549 | ||
12550 | if (parent_event->attr.freq) { | |
12551 | u64 sample_period = parent_event->hw.sample_period; | |
12552 | struct hw_perf_event *hwc = &child_event->hw; | |
12553 | ||
12554 | hwc->sample_period = sample_period; | |
12555 | hwc->last_period = sample_period; | |
12556 | ||
12557 | local64_set(&hwc->period_left, sample_period); | |
12558 | } | |
12559 | ||
12560 | child_event->ctx = child_ctx; | |
12561 | child_event->overflow_handler = parent_event->overflow_handler; | |
4dc0da86 AK |
12562 | child_event->overflow_handler_context |
12563 | = parent_event->overflow_handler_context; | |
97dee4f3 | 12564 | |
614b6780 TG |
12565 | /* |
12566 | * Precalculate sample_data sizes | |
12567 | */ | |
12568 | perf_event__header_size(child_event); | |
6844c09d | 12569 | perf_event__id_header_size(child_event); |
614b6780 | 12570 | |
97dee4f3 PZ |
12571 | /* |
12572 | * Link it up in the child's context: | |
12573 | */ | |
cee010ec | 12574 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
97dee4f3 | 12575 | add_event_to_ctx(child_event, child_ctx); |
cee010ec | 12576 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
97dee4f3 | 12577 | |
97dee4f3 PZ |
12578 | /* |
12579 | * Link this into the parent event's child list | |
12580 | */ | |
97dee4f3 PZ |
12581 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
12582 | mutex_unlock(&parent_event->child_mutex); | |
12583 | ||
12584 | return child_event; | |
12585 | } | |
12586 | ||
d8a8cfc7 PZ |
12587 | /* |
12588 | * Inherits an event group. | |
12589 | * | |
12590 | * This will quietly suppress orphaned events; !inherit_event() is not an error. | |
12591 | * This matches with perf_event_release_kernel() removing all child events. | |
12592 | * | |
12593 | * Returns: | |
12594 | * - 0 on success | |
12595 | * - <0 on error | |
12596 | */ | |
97dee4f3 PZ |
12597 | static int inherit_group(struct perf_event *parent_event, |
12598 | struct task_struct *parent, | |
12599 | struct perf_event_context *parent_ctx, | |
12600 | struct task_struct *child, | |
12601 | struct perf_event_context *child_ctx) | |
12602 | { | |
12603 | struct perf_event *leader; | |
12604 | struct perf_event *sub; | |
12605 | struct perf_event *child_ctr; | |
12606 | ||
12607 | leader = inherit_event(parent_event, parent, parent_ctx, | |
12608 | child, NULL, child_ctx); | |
12609 | if (IS_ERR(leader)) | |
12610 | return PTR_ERR(leader); | |
d8a8cfc7 PZ |
12611 | /* |
12612 | * @leader can be NULL here because of is_orphaned_event(). In this | |
12613 | * case inherit_event() will create individual events, similar to what | |
12614 | * perf_group_detach() would do anyway. | |
12615 | */ | |
edb39592 | 12616 | for_each_sibling_event(sub, parent_event) { |
97dee4f3 PZ |
12617 | child_ctr = inherit_event(sub, parent, parent_ctx, |
12618 | child, leader, child_ctx); | |
12619 | if (IS_ERR(child_ctr)) | |
12620 | return PTR_ERR(child_ctr); | |
f733c6b5 | 12621 | |
00496fe5 | 12622 | if (sub->aux_event == parent_event && child_ctr && |
f733c6b5 AS |
12623 | !perf_get_aux_event(child_ctr, leader)) |
12624 | return -EINVAL; | |
97dee4f3 PZ |
12625 | } |
12626 | return 0; | |
889ff015 FW |
12627 | } |
12628 | ||
d8a8cfc7 PZ |
12629 | /* |
12630 | * Creates the child task context and tries to inherit the event-group. | |
12631 | * | |
12632 | * Clears @inherited_all on !attr.inherited or error. Note that we'll leave | |
12633 | * inherited_all set when we 'fail' to inherit an orphaned event; this is | |
12634 | * consistent with perf_event_release_kernel() removing all child events. | |
12635 | * | |
12636 | * Returns: | |
12637 | * - 0 on success | |
12638 | * - <0 on error | |
12639 | */ | |
889ff015 FW |
12640 | static int |
12641 | inherit_task_group(struct perf_event *event, struct task_struct *parent, | |
12642 | struct perf_event_context *parent_ctx, | |
8dc85d54 | 12643 | struct task_struct *child, int ctxn, |
889ff015 FW |
12644 | int *inherited_all) |
12645 | { | |
12646 | int ret; | |
8dc85d54 | 12647 | struct perf_event_context *child_ctx; |
889ff015 FW |
12648 | |
12649 | if (!event->attr.inherit) { | |
12650 | *inherited_all = 0; | |
12651 | return 0; | |
bbbee908 PZ |
12652 | } |
12653 | ||
fe4b04fa | 12654 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 FW |
12655 | if (!child_ctx) { |
12656 | /* | |
12657 | * This is executed from the parent task context, so | |
12658 | * inherit events that have been marked for cloning. | |
12659 | * First allocate and initialize a context for the | |
12660 | * child. | |
12661 | */ | |
734df5ab | 12662 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
889ff015 FW |
12663 | if (!child_ctx) |
12664 | return -ENOMEM; | |
bbbee908 | 12665 | |
8dc85d54 | 12666 | child->perf_event_ctxp[ctxn] = child_ctx; |
889ff015 FW |
12667 | } |
12668 | ||
12669 | ret = inherit_group(event, parent, parent_ctx, | |
12670 | child, child_ctx); | |
12671 | ||
12672 | if (ret) | |
12673 | *inherited_all = 0; | |
12674 | ||
12675 | return ret; | |
bbbee908 PZ |
12676 | } |
12677 | ||
9b51f66d | 12678 | /* |
cdd6c482 | 12679 | * Initialize the perf_event context in task_struct |
9b51f66d | 12680 | */ |
985c8dcb | 12681 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
9b51f66d | 12682 | { |
889ff015 | 12683 | struct perf_event_context *child_ctx, *parent_ctx; |
cdd6c482 IM |
12684 | struct perf_event_context *cloned_ctx; |
12685 | struct perf_event *event; | |
9b51f66d | 12686 | struct task_struct *parent = current; |
564c2b21 | 12687 | int inherited_all = 1; |
dddd3379 | 12688 | unsigned long flags; |
6ab423e0 | 12689 | int ret = 0; |
9b51f66d | 12690 | |
8dc85d54 | 12691 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6ab423e0 PZ |
12692 | return 0; |
12693 | ||
ad3a37de | 12694 | /* |
25346b93 PM |
12695 | * If the parent's context is a clone, pin it so it won't get |
12696 | * swapped under us. | |
ad3a37de | 12697 | */ |
8dc85d54 | 12698 | parent_ctx = perf_pin_task_context(parent, ctxn); |
ffb4ef21 PZ |
12699 | if (!parent_ctx) |
12700 | return 0; | |
25346b93 | 12701 | |
ad3a37de PM |
12702 | /* |
12703 | * No need to check if parent_ctx != NULL here; since we saw | |
12704 | * it non-NULL earlier, the only reason for it to become NULL | |
12705 | * is if we exit, and since we're currently in the middle of | |
12706 | * a fork we can't be exiting at the same time. | |
12707 | */ | |
ad3a37de | 12708 | |
9b51f66d IM |
12709 | /* |
12710 | * Lock the parent list. No need to lock the child - not PID | |
12711 | * hashed yet and not running, so nobody can access it. | |
12712 | */ | |
d859e29f | 12713 | mutex_lock(&parent_ctx->mutex); |
9b51f66d IM |
12714 | |
12715 | /* | |
12716 | * We dont have to disable NMIs - we are only looking at | |
12717 | * the list, not manipulating it: | |
12718 | */ | |
6e6804d2 | 12719 | perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { |
8dc85d54 PZ |
12720 | ret = inherit_task_group(event, parent, parent_ctx, |
12721 | child, ctxn, &inherited_all); | |
889ff015 | 12722 | if (ret) |
e7cc4865 | 12723 | goto out_unlock; |
889ff015 | 12724 | } |
b93f7978 | 12725 | |
dddd3379 TG |
12726 | /* |
12727 | * We can't hold ctx->lock when iterating the ->flexible_group list due | |
12728 | * to allocations, but we need to prevent rotation because | |
12729 | * rotate_ctx() will change the list from interrupt context. | |
12730 | */ | |
12731 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); | |
12732 | parent_ctx->rotate_disable = 1; | |
12733 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
12734 | ||
6e6804d2 | 12735 | perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { |
8dc85d54 PZ |
12736 | ret = inherit_task_group(event, parent, parent_ctx, |
12737 | child, ctxn, &inherited_all); | |
889ff015 | 12738 | if (ret) |
e7cc4865 | 12739 | goto out_unlock; |
564c2b21 PM |
12740 | } |
12741 | ||
dddd3379 TG |
12742 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
12743 | parent_ctx->rotate_disable = 0; | |
dddd3379 | 12744 | |
8dc85d54 | 12745 | child_ctx = child->perf_event_ctxp[ctxn]; |
889ff015 | 12746 | |
05cbaa28 | 12747 | if (child_ctx && inherited_all) { |
564c2b21 PM |
12748 | /* |
12749 | * Mark the child context as a clone of the parent | |
12750 | * context, or of whatever the parent is a clone of. | |
c5ed5145 PZ |
12751 | * |
12752 | * Note that if the parent is a clone, the holding of | |
12753 | * parent_ctx->lock avoids it from being uncloned. | |
564c2b21 | 12754 | */ |
c5ed5145 | 12755 | cloned_ctx = parent_ctx->parent_ctx; |
ad3a37de PM |
12756 | if (cloned_ctx) { |
12757 | child_ctx->parent_ctx = cloned_ctx; | |
25346b93 | 12758 | child_ctx->parent_gen = parent_ctx->parent_gen; |
564c2b21 PM |
12759 | } else { |
12760 | child_ctx->parent_ctx = parent_ctx; | |
12761 | child_ctx->parent_gen = parent_ctx->generation; | |
12762 | } | |
12763 | get_ctx(child_ctx->parent_ctx); | |
9b51f66d IM |
12764 | } |
12765 | ||
c5ed5145 | 12766 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
e7cc4865 | 12767 | out_unlock: |
d859e29f | 12768 | mutex_unlock(&parent_ctx->mutex); |
6ab423e0 | 12769 | |
25346b93 | 12770 | perf_unpin_context(parent_ctx); |
fe4b04fa | 12771 | put_ctx(parent_ctx); |
ad3a37de | 12772 | |
6ab423e0 | 12773 | return ret; |
9b51f66d IM |
12774 | } |
12775 | ||
8dc85d54 PZ |
12776 | /* |
12777 | * Initialize the perf_event context in task_struct | |
12778 | */ | |
12779 | int perf_event_init_task(struct task_struct *child) | |
12780 | { | |
12781 | int ctxn, ret; | |
12782 | ||
8550d7cb ON |
12783 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
12784 | mutex_init(&child->perf_event_mutex); | |
12785 | INIT_LIST_HEAD(&child->perf_event_list); | |
12786 | ||
8dc85d54 PZ |
12787 | for_each_task_context_nr(ctxn) { |
12788 | ret = perf_event_init_context(child, ctxn); | |
6c72e350 PZ |
12789 | if (ret) { |
12790 | perf_event_free_task(child); | |
8dc85d54 | 12791 | return ret; |
6c72e350 | 12792 | } |
8dc85d54 PZ |
12793 | } |
12794 | ||
12795 | return 0; | |
12796 | } | |
12797 | ||
220b140b PM |
12798 | static void __init perf_event_init_all_cpus(void) |
12799 | { | |
b28ab83c | 12800 | struct swevent_htable *swhash; |
220b140b | 12801 | int cpu; |
220b140b | 12802 | |
a63fbed7 TG |
12803 | zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); |
12804 | ||
220b140b | 12805 | for_each_possible_cpu(cpu) { |
b28ab83c PZ |
12806 | swhash = &per_cpu(swevent_htable, cpu); |
12807 | mutex_init(&swhash->hlist_mutex); | |
2fde4f94 | 12808 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
f2fb6bef KL |
12809 | |
12810 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); | |
12811 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); | |
e48c1788 | 12812 | |
058fe1c0 DCC |
12813 | #ifdef CONFIG_CGROUP_PERF |
12814 | INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu)); | |
12815 | #endif | |
e48c1788 | 12816 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
220b140b PM |
12817 | } |
12818 | } | |
12819 | ||
d18bf422 | 12820 | static void perf_swevent_init_cpu(unsigned int cpu) |
0793a61d | 12821 | { |
108b02cf | 12822 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
0793a61d | 12823 | |
b28ab83c | 12824 | mutex_lock(&swhash->hlist_mutex); |
059fcd8c | 12825 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
76e1d904 FW |
12826 | struct swevent_hlist *hlist; |
12827 | ||
b28ab83c PZ |
12828 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
12829 | WARN_ON(!hlist); | |
12830 | rcu_assign_pointer(swhash->swevent_hlist, hlist); | |
76e1d904 | 12831 | } |
b28ab83c | 12832 | mutex_unlock(&swhash->hlist_mutex); |
0793a61d TG |
12833 | } |
12834 | ||
2965faa5 | 12835 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
108b02cf | 12836 | static void __perf_event_exit_context(void *__info) |
0793a61d | 12837 | { |
108b02cf | 12838 | struct perf_event_context *ctx = __info; |
fae3fde6 PZ |
12839 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
12840 | struct perf_event *event; | |
0793a61d | 12841 | |
fae3fde6 | 12842 | raw_spin_lock(&ctx->lock); |
0ee098c9 | 12843 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
fae3fde6 | 12844 | list_for_each_entry(event, &ctx->event_list, event_entry) |
45a0e07a | 12845 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
fae3fde6 | 12846 | raw_spin_unlock(&ctx->lock); |
0793a61d | 12847 | } |
108b02cf PZ |
12848 | |
12849 | static void perf_event_exit_cpu_context(int cpu) | |
12850 | { | |
a63fbed7 | 12851 | struct perf_cpu_context *cpuctx; |
108b02cf PZ |
12852 | struct perf_event_context *ctx; |
12853 | struct pmu *pmu; | |
108b02cf | 12854 | |
a63fbed7 TG |
12855 | mutex_lock(&pmus_lock); |
12856 | list_for_each_entry(pmu, &pmus, entry) { | |
12857 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
12858 | ctx = &cpuctx->ctx; | |
108b02cf PZ |
12859 | |
12860 | mutex_lock(&ctx->mutex); | |
12861 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); | |
a63fbed7 | 12862 | cpuctx->online = 0; |
108b02cf PZ |
12863 | mutex_unlock(&ctx->mutex); |
12864 | } | |
a63fbed7 TG |
12865 | cpumask_clear_cpu(cpu, perf_online_mask); |
12866 | mutex_unlock(&pmus_lock); | |
108b02cf | 12867 | } |
00e16c3d TG |
12868 | #else |
12869 | ||
12870 | static void perf_event_exit_cpu_context(int cpu) { } | |
12871 | ||
12872 | #endif | |
108b02cf | 12873 | |
a63fbed7 TG |
12874 | int perf_event_init_cpu(unsigned int cpu) |
12875 | { | |
12876 | struct perf_cpu_context *cpuctx; | |
12877 | struct perf_event_context *ctx; | |
12878 | struct pmu *pmu; | |
12879 | ||
12880 | perf_swevent_init_cpu(cpu); | |
12881 | ||
12882 | mutex_lock(&pmus_lock); | |
12883 | cpumask_set_cpu(cpu, perf_online_mask); | |
12884 | list_for_each_entry(pmu, &pmus, entry) { | |
12885 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | |
12886 | ctx = &cpuctx->ctx; | |
12887 | ||
12888 | mutex_lock(&ctx->mutex); | |
12889 | cpuctx->online = 1; | |
12890 | mutex_unlock(&ctx->mutex); | |
12891 | } | |
12892 | mutex_unlock(&pmus_lock); | |
12893 | ||
12894 | return 0; | |
12895 | } | |
12896 | ||
00e16c3d | 12897 | int perf_event_exit_cpu(unsigned int cpu) |
0793a61d | 12898 | { |
e3703f8c | 12899 | perf_event_exit_cpu_context(cpu); |
00e16c3d | 12900 | return 0; |
0793a61d | 12901 | } |
0793a61d | 12902 | |
c277443c PZ |
12903 | static int |
12904 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) | |
12905 | { | |
12906 | int cpu; | |
12907 | ||
12908 | for_each_online_cpu(cpu) | |
12909 | perf_event_exit_cpu(cpu); | |
12910 | ||
12911 | return NOTIFY_OK; | |
12912 | } | |
12913 | ||
12914 | /* | |
12915 | * Run the perf reboot notifier at the very last possible moment so that | |
12916 | * the generic watchdog code runs as long as possible. | |
12917 | */ | |
12918 | static struct notifier_block perf_reboot_notifier = { | |
12919 | .notifier_call = perf_reboot, | |
12920 | .priority = INT_MIN, | |
12921 | }; | |
12922 | ||
cdd6c482 | 12923 | void __init perf_event_init(void) |
0793a61d | 12924 | { |
3c502e7a JW |
12925 | int ret; |
12926 | ||
2e80a82a PZ |
12927 | idr_init(&pmu_idr); |
12928 | ||
220b140b | 12929 | perf_event_init_all_cpus(); |
b0a873eb | 12930 | init_srcu_struct(&pmus_srcu); |
2e80a82a PZ |
12931 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
12932 | perf_pmu_register(&perf_cpu_clock, NULL, -1); | |
12933 | perf_pmu_register(&perf_task_clock, NULL, -1); | |
b0a873eb | 12934 | perf_tp_register(); |
00e16c3d | 12935 | perf_event_init_cpu(smp_processor_id()); |
c277443c | 12936 | register_reboot_notifier(&perf_reboot_notifier); |
3c502e7a JW |
12937 | |
12938 | ret = init_hw_breakpoint(); | |
12939 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); | |
b2029520 | 12940 | |
b01c3a00 JO |
12941 | /* |
12942 | * Build time assertion that we keep the data_head at the intended | |
12943 | * location. IOW, validation we got the __reserved[] size right. | |
12944 | */ | |
12945 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) | |
12946 | != 1024); | |
0793a61d | 12947 | } |
abe43400 | 12948 | |
fd979c01 CS |
12949 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
12950 | char *page) | |
12951 | { | |
12952 | struct perf_pmu_events_attr *pmu_attr = | |
12953 | container_of(attr, struct perf_pmu_events_attr, attr); | |
12954 | ||
12955 | if (pmu_attr->event_str) | |
12956 | return sprintf(page, "%s\n", pmu_attr->event_str); | |
12957 | ||
12958 | return 0; | |
12959 | } | |
675965b0 | 12960 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
fd979c01 | 12961 | |
abe43400 PZ |
12962 | static int __init perf_event_sysfs_init(void) |
12963 | { | |
12964 | struct pmu *pmu; | |
12965 | int ret; | |
12966 | ||
12967 | mutex_lock(&pmus_lock); | |
12968 | ||
12969 | ret = bus_register(&pmu_bus); | |
12970 | if (ret) | |
12971 | goto unlock; | |
12972 | ||
12973 | list_for_each_entry(pmu, &pmus, entry) { | |
12974 | if (!pmu->name || pmu->type < 0) | |
12975 | continue; | |
12976 | ||
12977 | ret = pmu_dev_alloc(pmu); | |
12978 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); | |
12979 | } | |
12980 | pmu_bus_running = 1; | |
12981 | ret = 0; | |
12982 | ||
12983 | unlock: | |
12984 | mutex_unlock(&pmus_lock); | |
12985 | ||
12986 | return ret; | |
12987 | } | |
12988 | device_initcall(perf_event_sysfs_init); | |
e5d1367f SE |
12989 | |
12990 | #ifdef CONFIG_CGROUP_PERF | |
eb95419b TH |
12991 | static struct cgroup_subsys_state * |
12992 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
e5d1367f SE |
12993 | { |
12994 | struct perf_cgroup *jc; | |
e5d1367f | 12995 | |
1b15d055 | 12996 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
e5d1367f SE |
12997 | if (!jc) |
12998 | return ERR_PTR(-ENOMEM); | |
12999 | ||
e5d1367f SE |
13000 | jc->info = alloc_percpu(struct perf_cgroup_info); |
13001 | if (!jc->info) { | |
13002 | kfree(jc); | |
13003 | return ERR_PTR(-ENOMEM); | |
13004 | } | |
13005 | ||
e5d1367f SE |
13006 | return &jc->css; |
13007 | } | |
13008 | ||
eb95419b | 13009 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
e5d1367f | 13010 | { |
eb95419b TH |
13011 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
13012 | ||
e5d1367f SE |
13013 | free_percpu(jc->info); |
13014 | kfree(jc); | |
13015 | } | |
13016 | ||
96aaab68 NK |
13017 | static int perf_cgroup_css_online(struct cgroup_subsys_state *css) |
13018 | { | |
13019 | perf_event_cgroup(css->cgroup); | |
13020 | return 0; | |
13021 | } | |
13022 | ||
e5d1367f SE |
13023 | static int __perf_cgroup_move(void *info) |
13024 | { | |
13025 | struct task_struct *task = info; | |
ddaaf4e2 | 13026 | rcu_read_lock(); |
e5d1367f | 13027 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
ddaaf4e2 | 13028 | rcu_read_unlock(); |
e5d1367f SE |
13029 | return 0; |
13030 | } | |
13031 | ||
1f7dd3e5 | 13032 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
e5d1367f | 13033 | { |
bb9d97b6 | 13034 | struct task_struct *task; |
1f7dd3e5 | 13035 | struct cgroup_subsys_state *css; |
bb9d97b6 | 13036 | |
1f7dd3e5 | 13037 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 13038 | task_function_call(task, __perf_cgroup_move, task); |
e5d1367f SE |
13039 | } |
13040 | ||
073219e9 | 13041 | struct cgroup_subsys perf_event_cgrp_subsys = { |
92fb9748 TH |
13042 | .css_alloc = perf_cgroup_css_alloc, |
13043 | .css_free = perf_cgroup_css_free, | |
96aaab68 | 13044 | .css_online = perf_cgroup_css_online, |
bb9d97b6 | 13045 | .attach = perf_cgroup_attach, |
968ebff1 TH |
13046 | /* |
13047 | * Implicitly enable on dfl hierarchy so that perf events can | |
13048 | * always be filtered by cgroup2 path as long as perf_event | |
13049 | * controller is not mounted on a legacy hierarchy. | |
13050 | */ | |
13051 | .implicit_on_dfl = true, | |
8cfd8147 | 13052 | .threaded = true, |
e5d1367f SE |
13053 | }; |
13054 | #endif /* CONFIG_CGROUP_PERF */ |