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