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