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