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