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