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