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