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