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