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