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