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