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