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