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