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