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