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