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