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