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