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