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