blob: eea1955c78684194b7972732c88cbd0914b4787b [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020044
Frederic Weisbecker76369132011-05-19 19:55:04 +020045#include "internal.h"
46
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047#include <asm/irq_regs.h>
48
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010049struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020050 struct task_struct *p;
51 int (*func)(void *info);
52 void *info;
53 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010054};
55
56static void remote_function(void *data)
57{
58 struct remote_function_call *tfc = data;
59 struct task_struct *p = tfc->p;
60
61 if (p) {
62 tfc->ret = -EAGAIN;
63 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
64 return;
65 }
66
67 tfc->ret = tfc->func(tfc->info);
68}
69
70/**
71 * task_function_call - call a function on the cpu on which a task runs
72 * @p: the task to evaluate
73 * @func: the function to be called
74 * @info: the function call argument
75 *
76 * Calls the function @func when the task is currently running. This might
77 * be on the current CPU, which just calls the function directly
78 *
79 * returns: @func return value, or
80 * -ESRCH - when the process isn't running
81 * -EAGAIN - when the process moved away
82 */
83static int
84task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
85{
86 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020087 .p = p,
88 .func = func,
89 .info = info,
90 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010091 };
92
93 if (task_curr(p))
94 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
95
96 return data.ret;
97}
98
99/**
100 * cpu_function_call - call a function on the cpu
101 * @func: the function to be called
102 * @info: the function call argument
103 *
104 * Calls the function @func on the remote cpu.
105 *
106 * returns: @func return value or -ENXIO when the cpu is offline
107 */
108static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
109{
110 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200111 .p = NULL,
112 .func = func,
113 .info = info,
114 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115 };
116
117 smp_call_function_single(cpu, remote_function, &data, 1);
118
119 return data.ret;
120}
121
Stephane Eraniane5d13672011-02-14 11:20:01 +0200122#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
123 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100124 PERF_FLAG_PID_CGROUP |\
125 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200126
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100127/*
128 * branch priv levels that need permission checks
129 */
130#define PERF_SAMPLE_BRANCH_PERM_PLM \
131 (PERF_SAMPLE_BRANCH_KERNEL |\
132 PERF_SAMPLE_BRANCH_HV)
133
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200134enum event_type_t {
135 EVENT_FLEXIBLE = 0x1,
136 EVENT_PINNED = 0x2,
137 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
138};
139
Stephane Eraniane5d13672011-02-14 11:20:01 +0200140/*
141 * perf_sched_events : >0 events exist
142 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
143 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100144struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200145static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100146static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200147
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200148static atomic_t nr_mmap_events __read_mostly;
149static atomic_t nr_comm_events __read_mostly;
150static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200151static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200153static LIST_HEAD(pmus);
154static DEFINE_MUTEX(pmus_lock);
155static struct srcu_struct pmus_srcu;
156
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157/*
158 * perf event paranoia level:
159 * -1 - not paranoid at all
160 * 0 - disallow raw tracepoint access for unpriv
161 * 1 - disallow cpu events for unpriv
162 * 2 - disallow kernel profiling for unpriv
163 */
164int sysctl_perf_event_paranoid __read_mostly = 1;
165
Frederic Weisbecker20443382011-03-31 03:33:29 +0200166/* Minimum for 512 kiB + 1 user control page */
167int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200168
169/*
170 * max perf event sample rate
171 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700172#define DEFAULT_MAX_SAMPLE_RATE 100000
173#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
174#define DEFAULT_CPU_TIME_MAX_PERCENT 25
175
176int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
177
178static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
179static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
180
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200181static int perf_sample_allowed_ns __read_mostly =
182 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700183
184void update_perf_cpu_limits(void)
185{
186 u64 tmp = perf_sample_period_ns;
187
188 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200189 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200190 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700191}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100192
Stephane Eranian9e630202013-04-03 14:21:33 +0200193static int perf_rotate_context(struct perf_cpu_context *cpuctx);
194
Peter Zijlstra163ec432011-02-16 11:22:34 +0100195int perf_proc_update_handler(struct ctl_table *table, int write,
196 void __user *buffer, size_t *lenp,
197 loff_t *ppos)
198{
Knut Petersen723478c2013-09-25 14:29:37 +0200199 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100200
201 if (ret || !write)
202 return ret;
203
204 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700205 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
206 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100207
208 return 0;
209}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210
Dave Hansen14c63f12013-06-21 08:51:36 -0700211int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
212
213int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
214 void __user *buffer, size_t *lenp,
215 loff_t *ppos)
216{
217 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
218
219 if (ret || !write)
220 return ret;
221
222 update_perf_cpu_limits();
223
224 return 0;
225}
226
227/*
228 * perf samples are done in some very critical code paths (NMIs).
229 * If they take too much CPU time, the system can lock up and not
230 * get any real work done. This will drop the sample rate when
231 * we detect that events are taking too long.
232 */
233#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200234static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700235
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100236static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700237{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100238 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700239 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200240 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100241
242 local_samples_len = __get_cpu_var(running_sample_length);
243 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
244
245 printk_ratelimited(KERN_WARNING
246 "perf interrupt took too long (%lld > %lld), lowering "
247 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100248 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100249 sysctl_perf_event_sample_rate);
250}
251
252static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
253
254void perf_sample_event_took(u64 sample_len_ns)
255{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200256 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100257 u64 avg_local_sample_len;
258 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700259
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200260 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700261 return;
262
263 /* decay the counter by 1 average sample */
264 local_samples_len = __get_cpu_var(running_sample_length);
265 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
266 local_samples_len += sample_len_ns;
267 __get_cpu_var(running_sample_length) = local_samples_len;
268
269 /*
270 * note: this will be biased artifically low until we have
271 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
272 * from having to maintain a count.
273 */
274 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
275
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200276 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700277 return;
278
279 if (max_samples_per_tick <= 1)
280 return;
281
282 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
283 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
284 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
285
Dave Hansen14c63f12013-06-21 08:51:36 -0700286 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100287
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100288 if (!irq_work_queue(&perf_duration_work)) {
289 early_printk("perf interrupt took too long (%lld > %lld), lowering "
290 "kernel.perf_event_max_sample_rate to %d\n",
291 avg_local_sample_len, allowed_ns >> 1,
292 sysctl_perf_event_sample_rate);
293 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700294}
295
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200296static atomic64_t perf_event_id;
297
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200298static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
299 enum event_type_t event_type);
300
301static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200302 enum event_type_t event_type,
303 struct task_struct *task);
304
305static void update_context_time(struct perf_event_context *ctx);
306static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200307
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200308void __weak perf_event_print_debug(void) { }
309
Matt Fleming84c79912010-10-03 21:41:13 +0100310extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200311{
Matt Fleming84c79912010-10-03 21:41:13 +0100312 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200313}
314
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200315static inline u64 perf_clock(void)
316{
317 return local_clock();
318}
319
Stephane Eraniane5d13672011-02-14 11:20:01 +0200320static inline struct perf_cpu_context *
321__get_cpu_context(struct perf_event_context *ctx)
322{
323 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
324}
325
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200326static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
327 struct perf_event_context *ctx)
328{
329 raw_spin_lock(&cpuctx->ctx.lock);
330 if (ctx)
331 raw_spin_lock(&ctx->lock);
332}
333
334static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
335 struct perf_event_context *ctx)
336{
337 if (ctx)
338 raw_spin_unlock(&ctx->lock);
339 raw_spin_unlock(&cpuctx->ctx.lock);
340}
341
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342#ifdef CONFIG_CGROUP_PERF
343
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200344/*
Li Zefan877c6852013-03-05 11:38:08 +0800345 * perf_cgroup_info keeps track of time_enabled for a cgroup.
346 * This is a per-cpu dynamically allocated data structure.
347 */
348struct perf_cgroup_info {
349 u64 time;
350 u64 timestamp;
351};
352
353struct perf_cgroup {
354 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900355 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800356};
357
358/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200359 * Must ensure cgroup is pinned (css_get) before calling
360 * this function. In other words, we cannot call this function
361 * if there is no cgroup event for the current CPU context.
362 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200363static inline struct perf_cgroup *
364perf_cgroup_from_task(struct task_struct *task)
365{
Tejun Heo073219e2014-02-08 10:36:58 -0500366 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400367 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200368}
369
370static inline bool
371perf_cgroup_match(struct perf_event *event)
372{
373 struct perf_event_context *ctx = event->ctx;
374 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
375
Tejun Heoef824fa2013-04-08 19:00:38 -0700376 /* @event doesn't care about cgroup */
377 if (!event->cgrp)
378 return true;
379
380 /* wants specific cgroup scope but @cpuctx isn't associated with any */
381 if (!cpuctx->cgrp)
382 return false;
383
384 /*
385 * Cgroup scoping is recursive. An event enabled for a cgroup is
386 * also enabled for all its descendant cgroups. If @cpuctx's
387 * cgroup is a descendant of @event's (the test covers identity
388 * case), it's a match.
389 */
390 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
391 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200392}
393
Stephane Eraniane5d13672011-02-14 11:20:01 +0200394static inline void perf_put_cgroup(struct perf_event *event)
395{
396 css_put(&event->cgrp->css);
397}
398
399static inline void perf_detach_cgroup(struct perf_event *event)
400{
401 perf_put_cgroup(event);
402 event->cgrp = NULL;
403}
404
405static inline int is_cgroup_event(struct perf_event *event)
406{
407 return event->cgrp != NULL;
408}
409
410static inline u64 perf_cgroup_event_time(struct perf_event *event)
411{
412 struct perf_cgroup_info *t;
413
414 t = per_cpu_ptr(event->cgrp->info, event->cpu);
415 return t->time;
416}
417
418static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
419{
420 struct perf_cgroup_info *info;
421 u64 now;
422
423 now = perf_clock();
424
425 info = this_cpu_ptr(cgrp->info);
426
427 info->time += now - info->timestamp;
428 info->timestamp = now;
429}
430
431static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
432{
433 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
434 if (cgrp_out)
435 __update_cgrp_time(cgrp_out);
436}
437
438static inline void update_cgrp_time_from_event(struct perf_event *event)
439{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200440 struct perf_cgroup *cgrp;
441
Stephane Eraniane5d13672011-02-14 11:20:01 +0200442 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200443 * ensure we access cgroup data only when needed and
444 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200445 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200446 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200447 return;
448
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200449 cgrp = perf_cgroup_from_task(current);
450 /*
451 * Do not update time when cgroup is not active
452 */
453 if (cgrp == event->cgrp)
454 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200455}
456
457static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200458perf_cgroup_set_timestamp(struct task_struct *task,
459 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200460{
461 struct perf_cgroup *cgrp;
462 struct perf_cgroup_info *info;
463
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200464 /*
465 * ctx->lock held by caller
466 * ensure we do not access cgroup data
467 * unless we have the cgroup pinned (css_get)
468 */
469 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200470 return;
471
472 cgrp = perf_cgroup_from_task(task);
473 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200474 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200475}
476
477#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
478#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
479
480/*
481 * reschedule events based on the cgroup constraint of task.
482 *
483 * mode SWOUT : schedule out everything
484 * mode SWIN : schedule in based on cgroup for next
485 */
486void perf_cgroup_switch(struct task_struct *task, int mode)
487{
488 struct perf_cpu_context *cpuctx;
489 struct pmu *pmu;
490 unsigned long flags;
491
492 /*
493 * disable interrupts to avoid geting nr_cgroup
494 * changes via __perf_event_disable(). Also
495 * avoids preemption.
496 */
497 local_irq_save(flags);
498
499 /*
500 * we reschedule only in the presence of cgroup
501 * constrained events.
502 */
503 rcu_read_lock();
504
505 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200506 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200507 if (cpuctx->unique_pmu != pmu)
508 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200509
Stephane Eraniane5d13672011-02-14 11:20:01 +0200510 /*
511 * perf_cgroup_events says at least one
512 * context on this CPU has cgroup events.
513 *
514 * ctx->nr_cgroups reports the number of cgroup
515 * events for a context.
516 */
517 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200518 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
519 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200520
521 if (mode & PERF_CGROUP_SWOUT) {
522 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
523 /*
524 * must not be done before ctxswout due
525 * to event_filter_match() in event_sched_out()
526 */
527 cpuctx->cgrp = NULL;
528 }
529
530 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200531 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200532 /*
533 * set cgrp before ctxsw in to allow
534 * event_filter_match() to not have to pass
535 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200536 */
537 cpuctx->cgrp = perf_cgroup_from_task(task);
538 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
539 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200540 perf_pmu_enable(cpuctx->ctx.pmu);
541 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200542 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200543 }
544
545 rcu_read_unlock();
546
547 local_irq_restore(flags);
548}
549
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200550static inline void perf_cgroup_sched_out(struct task_struct *task,
551 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200552{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200553 struct perf_cgroup *cgrp1;
554 struct perf_cgroup *cgrp2 = NULL;
555
556 /*
557 * we come here when we know perf_cgroup_events > 0
558 */
559 cgrp1 = perf_cgroup_from_task(task);
560
561 /*
562 * next is NULL when called from perf_event_enable_on_exec()
563 * that will systematically cause a cgroup_switch()
564 */
565 if (next)
566 cgrp2 = perf_cgroup_from_task(next);
567
568 /*
569 * only schedule out current cgroup events if we know
570 * that we are switching to a different cgroup. Otherwise,
571 * do no touch the cgroup events.
572 */
573 if (cgrp1 != cgrp2)
574 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200575}
576
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200577static inline void perf_cgroup_sched_in(struct task_struct *prev,
578 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200579{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200580 struct perf_cgroup *cgrp1;
581 struct perf_cgroup *cgrp2 = NULL;
582
583 /*
584 * we come here when we know perf_cgroup_events > 0
585 */
586 cgrp1 = perf_cgroup_from_task(task);
587
588 /* prev can never be NULL */
589 cgrp2 = perf_cgroup_from_task(prev);
590
591 /*
592 * only need to schedule in cgroup events if we are changing
593 * cgroup during ctxsw. Cgroup events were not scheduled
594 * out of ctxsw out if that was not the case.
595 */
596 if (cgrp1 != cgrp2)
597 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200598}
599
600static inline int perf_cgroup_connect(int fd, struct perf_event *event,
601 struct perf_event_attr *attr,
602 struct perf_event *group_leader)
603{
604 struct perf_cgroup *cgrp;
605 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400606 struct fd f = fdget(fd);
607 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608
Al Viro2903ff02012-08-28 12:52:22 -0400609 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200610 return -EBADF;
611
Tejun Heo5a17f542014-02-11 11:52:47 -0500612 css = css_tryget_from_dir(f.file->f_dentry, &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800613 if (IS_ERR(css)) {
614 ret = PTR_ERR(css);
615 goto out;
616 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200617
618 cgrp = container_of(css, struct perf_cgroup, css);
619 event->cgrp = cgrp;
620
621 /*
622 * all events in a group must monitor
623 * the same cgroup because a task belongs
624 * to only one perf cgroup at a time
625 */
626 if (group_leader && group_leader->cgrp != cgrp) {
627 perf_detach_cgroup(event);
628 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200629 }
Li Zefan3db272c2011-03-03 14:25:37 +0800630out:
Al Viro2903ff02012-08-28 12:52:22 -0400631 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200632 return ret;
633}
634
635static inline void
636perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
637{
638 struct perf_cgroup_info *t;
639 t = per_cpu_ptr(event->cgrp->info, event->cpu);
640 event->shadow_ctx_time = now - t->timestamp;
641}
642
643static inline void
644perf_cgroup_defer_enabled(struct perf_event *event)
645{
646 /*
647 * when the current task's perf cgroup does not match
648 * the event's, we need to remember to call the
649 * perf_mark_enable() function the first time a task with
650 * a matching perf cgroup is scheduled in.
651 */
652 if (is_cgroup_event(event) && !perf_cgroup_match(event))
653 event->cgrp_defer_enabled = 1;
654}
655
656static inline void
657perf_cgroup_mark_enabled(struct perf_event *event,
658 struct perf_event_context *ctx)
659{
660 struct perf_event *sub;
661 u64 tstamp = perf_event_time(event);
662
663 if (!event->cgrp_defer_enabled)
664 return;
665
666 event->cgrp_defer_enabled = 0;
667
668 event->tstamp_enabled = tstamp - event->total_time_enabled;
669 list_for_each_entry(sub, &event->sibling_list, group_entry) {
670 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
671 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
672 sub->cgrp_defer_enabled = 0;
673 }
674 }
675}
676#else /* !CONFIG_CGROUP_PERF */
677
678static inline bool
679perf_cgroup_match(struct perf_event *event)
680{
681 return true;
682}
683
684static inline void perf_detach_cgroup(struct perf_event *event)
685{}
686
687static inline int is_cgroup_event(struct perf_event *event)
688{
689 return 0;
690}
691
692static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
693{
694 return 0;
695}
696
697static inline void update_cgrp_time_from_event(struct perf_event *event)
698{
699}
700
701static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
702{
703}
704
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200705static inline void perf_cgroup_sched_out(struct task_struct *task,
706 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200707{
708}
709
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200710static inline void perf_cgroup_sched_in(struct task_struct *prev,
711 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200712{
713}
714
715static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
716 struct perf_event_attr *attr,
717 struct perf_event *group_leader)
718{
719 return -EINVAL;
720}
721
722static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200723perf_cgroup_set_timestamp(struct task_struct *task,
724 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200725{
726}
727
728void
729perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
730{
731}
732
733static inline void
734perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
735{
736}
737
738static inline u64 perf_cgroup_event_time(struct perf_event *event)
739{
740 return 0;
741}
742
743static inline void
744perf_cgroup_defer_enabled(struct perf_event *event)
745{
746}
747
748static inline void
749perf_cgroup_mark_enabled(struct perf_event *event,
750 struct perf_event_context *ctx)
751{
752}
753#endif
754
Stephane Eranian9e630202013-04-03 14:21:33 +0200755/*
756 * set default to be dependent on timer tick just
757 * like original code
758 */
759#define PERF_CPU_HRTIMER (1000 / HZ)
760/*
761 * function must be called with interrupts disbled
762 */
763static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
764{
765 struct perf_cpu_context *cpuctx;
766 enum hrtimer_restart ret = HRTIMER_NORESTART;
767 int rotations = 0;
768
769 WARN_ON(!irqs_disabled());
770
771 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
772
773 rotations = perf_rotate_context(cpuctx);
774
775 /*
776 * arm timer if needed
777 */
778 if (rotations) {
779 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
780 ret = HRTIMER_RESTART;
781 }
782
783 return ret;
784}
785
786/* CPU is going down */
787void perf_cpu_hrtimer_cancel(int cpu)
788{
789 struct perf_cpu_context *cpuctx;
790 struct pmu *pmu;
791 unsigned long flags;
792
793 if (WARN_ON(cpu != smp_processor_id()))
794 return;
795
796 local_irq_save(flags);
797
798 rcu_read_lock();
799
800 list_for_each_entry_rcu(pmu, &pmus, entry) {
801 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
802
803 if (pmu->task_ctx_nr == perf_sw_context)
804 continue;
805
806 hrtimer_cancel(&cpuctx->hrtimer);
807 }
808
809 rcu_read_unlock();
810
811 local_irq_restore(flags);
812}
813
814static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
815{
816 struct hrtimer *hr = &cpuctx->hrtimer;
817 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200818 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200819
820 /* no multiplexing needed for SW PMU */
821 if (pmu->task_ctx_nr == perf_sw_context)
822 return;
823
Stephane Eranian62b85632013-04-03 14:21:34 +0200824 /*
825 * check default is sane, if not set then force to
826 * default interval (1/tick)
827 */
828 timer = pmu->hrtimer_interval_ms;
829 if (timer < 1)
830 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
831
832 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200833
834 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
835 hr->function = perf_cpu_hrtimer_handler;
836}
837
838static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
839{
840 struct hrtimer *hr = &cpuctx->hrtimer;
841 struct pmu *pmu = cpuctx->ctx.pmu;
842
843 /* not for SW PMU */
844 if (pmu->task_ctx_nr == perf_sw_context)
845 return;
846
847 if (hrtimer_active(hr))
848 return;
849
850 if (!hrtimer_callback_running(hr))
851 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
852 0, HRTIMER_MODE_REL_PINNED, 0);
853}
854
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200855void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200857 int *count = this_cpu_ptr(pmu->pmu_disable_count);
858 if (!(*count)++)
859 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200860}
861
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200862void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200864 int *count = this_cpu_ptr(pmu->pmu_disable_count);
865 if (!--(*count))
866 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867}
868
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200869static DEFINE_PER_CPU(struct list_head, rotation_list);
870
871/*
872 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
873 * because they're strictly cpu affine and rotate_start is called with IRQs
874 * disabled, while rotate_context is called from IRQ context.
875 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200876static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200877{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200878 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200879 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200880
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200881 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200882
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200883 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200884 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885}
886
887static void get_ctx(struct perf_event_context *ctx)
888{
889 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
890}
891
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200892static void put_ctx(struct perf_event_context *ctx)
893{
894 if (atomic_dec_and_test(&ctx->refcount)) {
895 if (ctx->parent_ctx)
896 put_ctx(ctx->parent_ctx);
897 if (ctx->task)
898 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800899 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200900 }
901}
902
903static void unclone_ctx(struct perf_event_context *ctx)
904{
905 if (ctx->parent_ctx) {
906 put_ctx(ctx->parent_ctx);
907 ctx->parent_ctx = NULL;
908 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200909 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200910}
911
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200912static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
913{
914 /*
915 * only top level events have the pid namespace they were created in
916 */
917 if (event->parent)
918 event = event->parent;
919
920 return task_tgid_nr_ns(p, event->ns);
921}
922
923static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
924{
925 /*
926 * only top level events have the pid namespace they were created in
927 */
928 if (event->parent)
929 event = event->parent;
930
931 return task_pid_nr_ns(p, event->ns);
932}
933
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200934/*
935 * If we inherit events we want to return the parent event id
936 * to userspace.
937 */
938static u64 primary_event_id(struct perf_event *event)
939{
940 u64 id = event->id;
941
942 if (event->parent)
943 id = event->parent->id;
944
945 return id;
946}
947
948/*
949 * Get the perf_event_context for a task and lock it.
950 * This has to cope with with the fact that until it is locked,
951 * the context could get moved to another task.
952 */
953static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200954perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200955{
956 struct perf_event_context *ctx;
957
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200958retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200959 /*
960 * One of the few rules of preemptible RCU is that one cannot do
961 * rcu_read_unlock() while holding a scheduler (or nested) lock when
962 * part of the read side critical section was preemptible -- see
963 * rcu_read_unlock_special().
964 *
965 * Since ctx->lock nests under rq->lock we must ensure the entire read
966 * side critical section is non-preemptible.
967 */
968 preempt_disable();
969 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200970 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200971 if (ctx) {
972 /*
973 * If this context is a clone of another, it might
974 * get swapped for another underneath us by
975 * perf_event_task_sched_out, though the
976 * rcu_read_lock() protects us from any context
977 * getting freed. Lock the context and check if it
978 * got swapped before we could get the lock, and retry
979 * if so. If we locked the right context, then it
980 * can't get swapped on us any more.
981 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100982 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200983 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100984 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200985 rcu_read_unlock();
986 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987 goto retry;
988 }
989
990 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100991 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200992 ctx = NULL;
993 }
994 }
995 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200996 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200997 return ctx;
998}
999
1000/*
1001 * Get the context for a task and increment its pin_count so it
1002 * can't get swapped to another task. This also increments its
1003 * reference count so that the context can't get freed.
1004 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001005static struct perf_event_context *
1006perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001007{
1008 struct perf_event_context *ctx;
1009 unsigned long flags;
1010
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001011 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001012 if (ctx) {
1013 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001014 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001015 }
1016 return ctx;
1017}
1018
1019static void perf_unpin_context(struct perf_event_context *ctx)
1020{
1021 unsigned long flags;
1022
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001023 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001025 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026}
1027
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001028/*
1029 * Update the record of the current time in a context.
1030 */
1031static void update_context_time(struct perf_event_context *ctx)
1032{
1033 u64 now = perf_clock();
1034
1035 ctx->time += now - ctx->timestamp;
1036 ctx->timestamp = now;
1037}
1038
Stephane Eranian41587552011-01-03 18:20:01 +02001039static u64 perf_event_time(struct perf_event *event)
1040{
1041 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001042
1043 if (is_cgroup_event(event))
1044 return perf_cgroup_event_time(event);
1045
Stephane Eranian41587552011-01-03 18:20:01 +02001046 return ctx ? ctx->time : 0;
1047}
1048
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001049/*
1050 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001051 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001052 */
1053static void update_event_times(struct perf_event *event)
1054{
1055 struct perf_event_context *ctx = event->ctx;
1056 u64 run_end;
1057
1058 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1059 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1060 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001061 /*
1062 * in cgroup mode, time_enabled represents
1063 * the time the event was enabled AND active
1064 * tasks were in the monitored cgroup. This is
1065 * independent of the activity of the context as
1066 * there may be a mix of cgroup and non-cgroup events.
1067 *
1068 * That is why we treat cgroup events differently
1069 * here.
1070 */
1071 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001072 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001073 else if (ctx->is_active)
1074 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001075 else
1076 run_end = event->tstamp_stopped;
1077
1078 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001079
1080 if (event->state == PERF_EVENT_STATE_INACTIVE)
1081 run_end = event->tstamp_stopped;
1082 else
Stephane Eranian41587552011-01-03 18:20:01 +02001083 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001084
1085 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001086
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001087}
1088
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001089/*
1090 * Update total_time_enabled and total_time_running for all events in a group.
1091 */
1092static void update_group_times(struct perf_event *leader)
1093{
1094 struct perf_event *event;
1095
1096 update_event_times(leader);
1097 list_for_each_entry(event, &leader->sibling_list, group_entry)
1098 update_event_times(event);
1099}
1100
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001101static struct list_head *
1102ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1103{
1104 if (event->attr.pinned)
1105 return &ctx->pinned_groups;
1106 else
1107 return &ctx->flexible_groups;
1108}
1109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001110/*
1111 * Add a event from the lists for its context.
1112 * Must be called with ctx->mutex and ctx->lock held.
1113 */
1114static void
1115list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1116{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001117 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1118 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119
1120 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001121 * If we're a stand alone event or group leader, we go to the context
1122 * list, group events are kept attached to the group so that
1123 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001125 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001126 struct list_head *list;
1127
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001128 if (is_software_event(event))
1129 event->group_flags |= PERF_GROUP_SOFTWARE;
1130
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001131 list = ctx_group_list(event, ctx);
1132 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133 }
1134
Peter Zijlstra08309372011-03-03 11:31:20 +01001135 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001136 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001137
Stephane Eraniand010b332012-02-09 23:21:00 +01001138 if (has_branch_stack(event))
1139 ctx->nr_branch_stack++;
1140
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001142 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001143 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 ctx->nr_events++;
1145 if (event->attr.inherit_stat)
1146 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001147
1148 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149}
1150
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001151/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001152 * Initialize event state based on the perf_event_attr::disabled.
1153 */
1154static inline void perf_event__state_init(struct perf_event *event)
1155{
1156 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1157 PERF_EVENT_STATE_INACTIVE;
1158}
1159
1160/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001161 * Called at perf_event creation and when events are attached/detached from a
1162 * group.
1163 */
1164static void perf_event__read_size(struct perf_event *event)
1165{
1166 int entry = sizeof(u64); /* value */
1167 int size = 0;
1168 int nr = 1;
1169
1170 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1171 size += sizeof(u64);
1172
1173 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1174 size += sizeof(u64);
1175
1176 if (event->attr.read_format & PERF_FORMAT_ID)
1177 entry += sizeof(u64);
1178
1179 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1180 nr += event->group_leader->nr_siblings;
1181 size += sizeof(u64);
1182 }
1183
1184 size += entry * nr;
1185 event->read_size = size;
1186}
1187
1188static void perf_event__header_size(struct perf_event *event)
1189{
1190 struct perf_sample_data *data;
1191 u64 sample_type = event->attr.sample_type;
1192 u16 size = 0;
1193
1194 perf_event__read_size(event);
1195
1196 if (sample_type & PERF_SAMPLE_IP)
1197 size += sizeof(data->ip);
1198
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001199 if (sample_type & PERF_SAMPLE_ADDR)
1200 size += sizeof(data->addr);
1201
1202 if (sample_type & PERF_SAMPLE_PERIOD)
1203 size += sizeof(data->period);
1204
Andi Kleenc3feedf2013-01-24 16:10:28 +01001205 if (sample_type & PERF_SAMPLE_WEIGHT)
1206 size += sizeof(data->weight);
1207
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001208 if (sample_type & PERF_SAMPLE_READ)
1209 size += event->read_size;
1210
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001211 if (sample_type & PERF_SAMPLE_DATA_SRC)
1212 size += sizeof(data->data_src.val);
1213
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001214 if (sample_type & PERF_SAMPLE_TRANSACTION)
1215 size += sizeof(data->txn);
1216
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001217 event->header_size = size;
1218}
1219
1220static void perf_event__id_header_size(struct perf_event *event)
1221{
1222 struct perf_sample_data *data;
1223 u64 sample_type = event->attr.sample_type;
1224 u16 size = 0;
1225
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001226 if (sample_type & PERF_SAMPLE_TID)
1227 size += sizeof(data->tid_entry);
1228
1229 if (sample_type & PERF_SAMPLE_TIME)
1230 size += sizeof(data->time);
1231
Adrian Hunterff3d5272013-08-27 11:23:07 +03001232 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1233 size += sizeof(data->id);
1234
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001235 if (sample_type & PERF_SAMPLE_ID)
1236 size += sizeof(data->id);
1237
1238 if (sample_type & PERF_SAMPLE_STREAM_ID)
1239 size += sizeof(data->stream_id);
1240
1241 if (sample_type & PERF_SAMPLE_CPU)
1242 size += sizeof(data->cpu_entry);
1243
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001244 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001245}
1246
Peter Zijlstra8a495422010-05-27 15:47:49 +02001247static void perf_group_attach(struct perf_event *event)
1248{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001249 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001250
Peter Zijlstra74c33372010-10-15 11:40:29 +02001251 /*
1252 * We can have double attach due to group movement in perf_event_open.
1253 */
1254 if (event->attach_state & PERF_ATTACH_GROUP)
1255 return;
1256
Peter Zijlstra8a495422010-05-27 15:47:49 +02001257 event->attach_state |= PERF_ATTACH_GROUP;
1258
1259 if (group_leader == event)
1260 return;
1261
1262 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1263 !is_software_event(event))
1264 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1265
1266 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1267 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001268
1269 perf_event__header_size(group_leader);
1270
1271 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1272 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001273}
1274
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275/*
1276 * Remove a event from the lists for its context.
1277 * Must be called with ctx->mutex and ctx->lock held.
1278 */
1279static void
1280list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1281{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001282 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001283 /*
1284 * We can have double detach due to exit/hot-unplug + close.
1285 */
1286 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001288
1289 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1290
Stephane Eranian68cacd22011-03-23 16:03:06 +01001291 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001292 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001293 cpuctx = __get_cpu_context(ctx);
1294 /*
1295 * if there are no more cgroup events
1296 * then cler cgrp to avoid stale pointer
1297 * in update_cgrp_time_from_cpuctx()
1298 */
1299 if (!ctx->nr_cgroups)
1300 cpuctx->cgrp = NULL;
1301 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001302
Stephane Eraniand010b332012-02-09 23:21:00 +01001303 if (has_branch_stack(event))
1304 ctx->nr_branch_stack--;
1305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 ctx->nr_events--;
1307 if (event->attr.inherit_stat)
1308 ctx->nr_stat--;
1309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310 list_del_rcu(&event->event_entry);
1311
Peter Zijlstra8a495422010-05-27 15:47:49 +02001312 if (event->group_leader == event)
1313 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001315 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001316
1317 /*
1318 * If event was in error state, then keep it
1319 * that way, otherwise bogus counts will be
1320 * returned on read(). The only way to get out
1321 * of error state is by explicit re-enabling
1322 * of the event
1323 */
1324 if (event->state > PERF_EVENT_STATE_OFF)
1325 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001326
1327 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001328}
1329
Peter Zijlstra8a495422010-05-27 15:47:49 +02001330static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001331{
1332 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001333 struct list_head *list = NULL;
1334
1335 /*
1336 * We can have double detach due to exit/hot-unplug + close.
1337 */
1338 if (!(event->attach_state & PERF_ATTACH_GROUP))
1339 return;
1340
1341 event->attach_state &= ~PERF_ATTACH_GROUP;
1342
1343 /*
1344 * If this is a sibling, remove it from its group.
1345 */
1346 if (event->group_leader != event) {
1347 list_del_init(&event->group_entry);
1348 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001349 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001350 }
1351
1352 if (!list_empty(&event->group_entry))
1353 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001355 /*
1356 * If this was a group event with sibling events then
1357 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001358 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001359 */
1360 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001361 if (list)
1362 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001364
1365 /* Inherit group flags from the previous leader */
1366 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001367 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001368
1369out:
1370 perf_event__header_size(event->group_leader);
1371
1372 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1373 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374}
1375
Stephane Eranianfa66f072010-08-26 16:40:01 +02001376static inline int
1377event_filter_match(struct perf_event *event)
1378{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001379 return (event->cpu == -1 || event->cpu == smp_processor_id())
1380 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001381}
1382
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001383static void
1384event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 struct perf_cpu_context *cpuctx,
1386 struct perf_event_context *ctx)
1387{
Stephane Eranian41587552011-01-03 18:20:01 +02001388 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001389 u64 delta;
1390 /*
1391 * An event which could not be activated because of
1392 * filter mismatch still needs to have its timings
1393 * maintained, otherwise bogus information is return
1394 * via read() for time_enabled, time_running:
1395 */
1396 if (event->state == PERF_EVENT_STATE_INACTIVE
1397 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001398 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001399 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001400 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001401 }
1402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001403 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001404 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001405
Alexander Shishkin44377272013-12-16 14:17:36 +02001406 perf_pmu_disable(event->pmu);
1407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001408 event->state = PERF_EVENT_STATE_INACTIVE;
1409 if (event->pending_disable) {
1410 event->pending_disable = 0;
1411 event->state = PERF_EVENT_STATE_OFF;
1412 }
Stephane Eranian41587552011-01-03 18:20:01 +02001413 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001414 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 event->oncpu = -1;
1416
1417 if (!is_software_event(event))
1418 cpuctx->active_oncpu--;
1419 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001420 if (event->attr.freq && event->attr.sample_freq)
1421 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001422 if (event->attr.exclusive || !cpuctx->active_oncpu)
1423 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001424
1425 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426}
1427
1428static void
1429group_sched_out(struct perf_event *group_event,
1430 struct perf_cpu_context *cpuctx,
1431 struct perf_event_context *ctx)
1432{
1433 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001434 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435
1436 event_sched_out(group_event, cpuctx, ctx);
1437
1438 /*
1439 * Schedule out siblings (if any):
1440 */
1441 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1442 event_sched_out(event, cpuctx, ctx);
1443
Stephane Eranianfa66f072010-08-26 16:40:01 +02001444 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 cpuctx->exclusive = 0;
1446}
1447
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001448struct remove_event {
1449 struct perf_event *event;
1450 bool detach_group;
1451};
1452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453/*
1454 * Cross CPU call to remove a performance event
1455 *
1456 * We disable the event on the hardware level first. After that we
1457 * remove it from the context list.
1458 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001459static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001460{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001461 struct remove_event *re = info;
1462 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001464 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001466 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001468 if (re->detach_group)
1469 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001471 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1472 ctx->is_active = 0;
1473 cpuctx->task_ctx = NULL;
1474 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001475 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001476
1477 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478}
1479
1480
1481/*
1482 * Remove the event from a task's (or a CPU's) list of events.
1483 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484 * CPU events are removed with a smp call. For task events we only
1485 * call when the task is on a CPU.
1486 *
1487 * If event->ctx is a cloned context, callers must make sure that
1488 * every task struct that event->ctx->task could possibly point to
1489 * remains valid. This is OK when called from perf_release since
1490 * that only calls us on the top-level context, which can't be a clone.
1491 * When called from perf_event_exit_task, it's OK because the
1492 * context has been detached from its task.
1493 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001494static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495{
1496 struct perf_event_context *ctx = event->ctx;
1497 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001498 struct remove_event re = {
1499 .event = event,
1500 .detach_group = detach_group,
1501 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001503 lockdep_assert_held(&ctx->mutex);
1504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 if (!task) {
1506 /*
1507 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001508 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001510 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 return;
1512 }
1513
1514retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001515 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001516 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001517
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001518 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001520 * If we failed to find a running task, but find the context active now
1521 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001522 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001523 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001524 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525 goto retry;
1526 }
1527
1528 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001529 * Since the task isn't running, its safe to remove the event, us
1530 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001531 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001532 if (detach_group)
1533 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001534 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001535 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536}
1537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539 * Cross CPU call to disable a performance event
1540 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301541int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001542{
1543 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001545 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546
1547 /*
1548 * If this is a per-task event, need to check whether this
1549 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001550 *
1551 * Can trigger due to concurrent perf_event_context_sched_out()
1552 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553 */
1554 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001555 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001557 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001558
1559 /*
1560 * If the event is on, turn it off.
1561 * If it is in error state, leave it in error state.
1562 */
1563 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1564 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001565 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 update_group_times(event);
1567 if (event == event->group_leader)
1568 group_sched_out(event, cpuctx, ctx);
1569 else
1570 event_sched_out(event, cpuctx, ctx);
1571 event->state = PERF_EVENT_STATE_OFF;
1572 }
1573
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001574 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001575
1576 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577}
1578
1579/*
1580 * Disable a event.
1581 *
1582 * If event->ctx is a cloned context, callers must make sure that
1583 * every task struct that event->ctx->task could possibly point to
1584 * remains valid. This condition is satisifed when called through
1585 * perf_event_for_each_child or perf_event_for_each because they
1586 * hold the top-level event's child_mutex, so any descendant that
1587 * goes to exit will block in sync_child_event.
1588 * When called from perf_pending_event it's OK because event->ctx
1589 * is the current context on this CPU and preemption is disabled,
1590 * hence we can't get into perf_event_task_sched_out for this context.
1591 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001592void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593{
1594 struct perf_event_context *ctx = event->ctx;
1595 struct task_struct *task = ctx->task;
1596
1597 if (!task) {
1598 /*
1599 * Disable the event on the cpu that it's on
1600 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001601 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602 return;
1603 }
1604
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001605retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001606 if (!task_function_call(task, __perf_event_disable, event))
1607 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001609 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 /*
1611 * If the event is still active, we need to retry the cross-call.
1612 */
1613 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001614 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001615 /*
1616 * Reload the task pointer, it might have been changed by
1617 * a concurrent perf_event_context_sched_out().
1618 */
1619 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620 goto retry;
1621 }
1622
1623 /*
1624 * Since we have the lock this context can't be scheduled
1625 * in, so we can change the state safely.
1626 */
1627 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1628 update_group_times(event);
1629 event->state = PERF_EVENT_STATE_OFF;
1630 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001631 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001633EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634
Stephane Eraniane5d13672011-02-14 11:20:01 +02001635static void perf_set_shadow_time(struct perf_event *event,
1636 struct perf_event_context *ctx,
1637 u64 tstamp)
1638{
1639 /*
1640 * use the correct time source for the time snapshot
1641 *
1642 * We could get by without this by leveraging the
1643 * fact that to get to this function, the caller
1644 * has most likely already called update_context_time()
1645 * and update_cgrp_time_xx() and thus both timestamp
1646 * are identical (or very close). Given that tstamp is,
1647 * already adjusted for cgroup, we could say that:
1648 * tstamp - ctx->timestamp
1649 * is equivalent to
1650 * tstamp - cgrp->timestamp.
1651 *
1652 * Then, in perf_output_read(), the calculation would
1653 * work with no changes because:
1654 * - event is guaranteed scheduled in
1655 * - no scheduled out in between
1656 * - thus the timestamp would be the same
1657 *
1658 * But this is a bit hairy.
1659 *
1660 * So instead, we have an explicit cgroup call to remain
1661 * within the time time source all along. We believe it
1662 * is cleaner and simpler to understand.
1663 */
1664 if (is_cgroup_event(event))
1665 perf_cgroup_set_shadow_time(event, tstamp);
1666 else
1667 event->shadow_ctx_time = tstamp - ctx->timestamp;
1668}
1669
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001670#define MAX_INTERRUPTS (~0ULL)
1671
1672static void perf_log_throttle(struct perf_event *event, int enable);
1673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001675event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001677 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678{
Stephane Eranian41587552011-01-03 18:20:01 +02001679 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001680 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001681
Peter Zijlstra63342412014-05-05 11:49:16 +02001682 lockdep_assert_held(&ctx->lock);
1683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684 if (event->state <= PERF_EVENT_STATE_OFF)
1685 return 0;
1686
1687 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001688 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001689
1690 /*
1691 * Unthrottle events, since we scheduled we might have missed several
1692 * ticks already, also for a heavily scheduling task there is little
1693 * guarantee it'll get a tick in a timely manner.
1694 */
1695 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1696 perf_log_throttle(event, 1);
1697 event->hw.interrupts = 0;
1698 }
1699
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700 /*
1701 * The new state must be visible before we turn it on in the hardware:
1702 */
1703 smp_wmb();
1704
Alexander Shishkin44377272013-12-16 14:17:36 +02001705 perf_pmu_disable(event->pmu);
1706
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001707 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 event->state = PERF_EVENT_STATE_INACTIVE;
1709 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001710 ret = -EAGAIN;
1711 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 }
1713
Stephane Eranian41587552011-01-03 18:20:01 +02001714 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001715
Stephane Eraniane5d13672011-02-14 11:20:01 +02001716 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 if (!is_software_event(event))
1719 cpuctx->active_oncpu++;
1720 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001721 if (event->attr.freq && event->attr.sample_freq)
1722 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723
1724 if (event->attr.exclusive)
1725 cpuctx->exclusive = 1;
1726
Alexander Shishkin44377272013-12-16 14:17:36 +02001727out:
1728 perf_pmu_enable(event->pmu);
1729
1730 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731}
1732
1733static int
1734group_sched_in(struct perf_event *group_event,
1735 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001736 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737{
Lin Ming6bde9b62010-04-23 13:56:00 +08001738 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001739 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001740 u64 now = ctx->time;
1741 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742
1743 if (group_event->state == PERF_EVENT_STATE_OFF)
1744 return 0;
1745
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001746 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001747
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001748 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001749 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001750 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001752 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753
1754 /*
1755 * Schedule in siblings as one group (if any):
1756 */
1757 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001758 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001759 partial_group = event;
1760 goto group_error;
1761 }
1762 }
1763
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001764 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001765 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767group_error:
1768 /*
1769 * Groups can be scheduled in as one unit only, so undo any
1770 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001771 * The events up to the failed event are scheduled out normally,
1772 * tstamp_stopped will be updated.
1773 *
1774 * The failed events and the remaining siblings need to have
1775 * their timings updated as if they had gone thru event_sched_in()
1776 * and event_sched_out(). This is required to get consistent timings
1777 * across the group. This also takes care of the case where the group
1778 * could never be scheduled by ensuring tstamp_stopped is set to mark
1779 * the time the event was actually stopped, such that time delta
1780 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001781 */
1782 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1783 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001784 simulate = true;
1785
1786 if (simulate) {
1787 event->tstamp_running += now - event->tstamp_stopped;
1788 event->tstamp_stopped = now;
1789 } else {
1790 event_sched_out(event, cpuctx, ctx);
1791 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001793 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001795 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001796
Stephane Eranian9e630202013-04-03 14:21:33 +02001797 perf_cpu_hrtimer_restart(cpuctx);
1798
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799 return -EAGAIN;
1800}
1801
1802/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803 * Work out whether we can put this event group on the CPU now.
1804 */
1805static int group_can_go_on(struct perf_event *event,
1806 struct perf_cpu_context *cpuctx,
1807 int can_add_hw)
1808{
1809 /*
1810 * Groups consisting entirely of software events can always go on.
1811 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001812 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813 return 1;
1814 /*
1815 * If an exclusive group is already on, no other hardware
1816 * events can go on.
1817 */
1818 if (cpuctx->exclusive)
1819 return 0;
1820 /*
1821 * If this group is exclusive and there are already
1822 * events on the CPU, it can't go on.
1823 */
1824 if (event->attr.exclusive && cpuctx->active_oncpu)
1825 return 0;
1826 /*
1827 * Otherwise, try to add it if all previous groups were able
1828 * to go on.
1829 */
1830 return can_add_hw;
1831}
1832
1833static void add_event_to_ctx(struct perf_event *event,
1834 struct perf_event_context *ctx)
1835{
Stephane Eranian41587552011-01-03 18:20:01 +02001836 u64 tstamp = perf_event_time(event);
1837
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001839 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001840 event->tstamp_enabled = tstamp;
1841 event->tstamp_running = tstamp;
1842 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843}
1844
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001845static void task_ctx_sched_out(struct perf_event_context *ctx);
1846static void
1847ctx_sched_in(struct perf_event_context *ctx,
1848 struct perf_cpu_context *cpuctx,
1849 enum event_type_t event_type,
1850 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001851
Peter Zijlstradce58552011-04-09 21:17:46 +02001852static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1853 struct perf_event_context *ctx,
1854 struct task_struct *task)
1855{
1856 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1857 if (ctx)
1858 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1859 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1860 if (ctx)
1861 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1862}
1863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001864/*
1865 * Cross CPU call to install and enable a performance event
1866 *
1867 * Must be called with ctx->mutex held
1868 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001869static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001870{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871 struct perf_event *event = info;
1872 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001873 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001874 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1875 struct task_struct *task = current;
1876
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001877 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001878 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001879
1880 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001881 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001882 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001883 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001884 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001885
1886 /*
1887 * If the context we're installing events in is not the
1888 * active task_ctx, flip them.
1889 */
1890 if (ctx->task && task_ctx != ctx) {
1891 if (task_ctx)
1892 raw_spin_unlock(&task_ctx->lock);
1893 raw_spin_lock(&ctx->lock);
1894 task_ctx = ctx;
1895 }
1896
1897 if (task_ctx) {
1898 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001899 task = task_ctx->task;
1900 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001901
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001902 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001905 /*
1906 * update cgrp time only if current cgrp
1907 * matches event->cgrp. Must be done before
1908 * calling add_event_to_ctx()
1909 */
1910 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001912 add_event_to_ctx(event, ctx);
1913
1914 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001915 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001917 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001919 perf_pmu_enable(cpuctx->ctx.pmu);
1920 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001921
1922 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923}
1924
1925/*
1926 * Attach a performance event to a context
1927 *
1928 * First we add the event to the list with the hardware enable bit
1929 * in event->hw_config cleared.
1930 *
1931 * If the event is attached to a task which is on a CPU we use a smp
1932 * call to enable it in the task context. The task might have been
1933 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001934 */
1935static void
1936perf_install_in_context(struct perf_event_context *ctx,
1937 struct perf_event *event,
1938 int cpu)
1939{
1940 struct task_struct *task = ctx->task;
1941
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001942 lockdep_assert_held(&ctx->mutex);
1943
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001944 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001945 if (event->cpu != -1)
1946 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001947
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001948 if (!task) {
1949 /*
1950 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001951 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001953 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954 return;
1955 }
1956
1957retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001958 if (!task_function_call(task, __perf_install_in_context, event))
1959 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001961 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001963 * If we failed to find a running task, but find the context active now
1964 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001966 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001967 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 goto retry;
1969 }
1970
1971 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001972 * Since the task isn't running, its safe to add the event, us holding
1973 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001975 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001976 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977}
1978
1979/*
1980 * Put a event into inactive state and update time fields.
1981 * Enabling the leader of a group effectively enables all
1982 * the group members that aren't explicitly disabled, so we
1983 * have to update their ->tstamp_enabled also.
1984 * Note: this works for group members as well as group leaders
1985 * since the non-leader members' sibling_lists will be empty.
1986 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001987static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988{
1989 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001990 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991
1992 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001993 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001994 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001995 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1996 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001997 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998}
1999
2000/*
2001 * Cross CPU call to enable a performance event
2002 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002003static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004{
2005 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 struct perf_event_context *ctx = event->ctx;
2007 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002008 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009 int err;
2010
Jiri Olsa06f41792013-07-09 17:44:11 +02002011 /*
2012 * There's a time window between 'ctx->is_active' check
2013 * in perf_event_enable function and this place having:
2014 * - IRQs on
2015 * - ctx->lock unlocked
2016 *
2017 * where the task could be killed and 'ctx' deactivated
2018 * by perf_event_exit_task.
2019 */
2020 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002021 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002023 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 update_context_time(ctx);
2025
2026 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2027 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002028
2029 /*
2030 * set current task's cgroup time reference point
2031 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002032 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002033
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002034 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035
Stephane Eraniane5d13672011-02-14 11:20:01 +02002036 if (!event_filter_match(event)) {
2037 if (is_cgroup_event(event))
2038 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002039 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002040 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 /*
2043 * If the event is in a group and isn't the group leader,
2044 * then don't put it on unless the group is on.
2045 */
2046 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2047 goto unlock;
2048
2049 if (!group_can_go_on(event, cpuctx, 1)) {
2050 err = -EEXIST;
2051 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002053 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002055 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056 }
2057
2058 if (err) {
2059 /*
2060 * If this event can't go on and it's part of a
2061 * group, then the whole group has to come off.
2062 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002063 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002065 perf_cpu_hrtimer_restart(cpuctx);
2066 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002067 if (leader->attr.pinned) {
2068 update_group_times(leader);
2069 leader->state = PERF_EVENT_STATE_ERROR;
2070 }
2071 }
2072
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002073unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002074 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002075
2076 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002077}
2078
2079/*
2080 * Enable a event.
2081 *
2082 * If event->ctx is a cloned context, callers must make sure that
2083 * every task struct that event->ctx->task could possibly point to
2084 * remains valid. This condition is satisfied when called through
2085 * perf_event_for_each_child or perf_event_for_each as described
2086 * for perf_event_disable.
2087 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002088void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089{
2090 struct perf_event_context *ctx = event->ctx;
2091 struct task_struct *task = ctx->task;
2092
2093 if (!task) {
2094 /*
2095 * Enable the event on the cpu that it's on
2096 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002097 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 return;
2099 }
2100
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002101 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002102 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2103 goto out;
2104
2105 /*
2106 * If the event is in error state, clear that first.
2107 * That way, if we see the event in error state below, we
2108 * know that it has gone back into error state, as distinct
2109 * from the task having been scheduled away before the
2110 * cross-call arrived.
2111 */
2112 if (event->state == PERF_EVENT_STATE_ERROR)
2113 event->state = PERF_EVENT_STATE_OFF;
2114
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002115retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002116 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002117 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002118 goto out;
2119 }
2120
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002121 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002122
2123 if (!task_function_call(task, __perf_event_enable, event))
2124 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002126 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127
2128 /*
2129 * If the context is active and the event is still off,
2130 * we need to retry the cross-call.
2131 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002132 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2133 /*
2134 * task could have been flipped by a concurrent
2135 * perf_event_context_sched_out()
2136 */
2137 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002139 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002141out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002142 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002144EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145
Avi Kivity26ca5c12011-06-29 18:42:37 +03002146int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147{
2148 /*
2149 * not supported on inherited events
2150 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002151 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152 return -EINVAL;
2153
2154 atomic_add(refresh, &event->event_limit);
2155 perf_event_enable(event);
2156
2157 return 0;
2158}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002159EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002161static void ctx_sched_out(struct perf_event_context *ctx,
2162 struct perf_cpu_context *cpuctx,
2163 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164{
2165 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002166 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002167
Peter Zijlstradb24d332011-04-09 21:17:45 +02002168 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002170 return;
2171
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002173 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002174 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002175 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002176
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002177 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002178 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002179 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2180 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002181 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002182
Peter Zijlstradb24d332011-04-09 21:17:45 +02002183 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002184 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002185 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002186 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002187 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188}
2189
2190/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002191 * Test whether two contexts are equivalent, i.e. whether they have both been
2192 * cloned from the same version of the same context.
2193 *
2194 * Equivalence is measured using a generation number in the context that is
2195 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2196 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002197 */
2198static int context_equiv(struct perf_event_context *ctx1,
2199 struct perf_event_context *ctx2)
2200{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002201 /* Pinning disables the swap optimization */
2202 if (ctx1->pin_count || ctx2->pin_count)
2203 return 0;
2204
2205 /* If ctx1 is the parent of ctx2 */
2206 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2207 return 1;
2208
2209 /* If ctx2 is the parent of ctx1 */
2210 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2211 return 1;
2212
2213 /*
2214 * If ctx1 and ctx2 have the same parent; we flatten the parent
2215 * hierarchy, see perf_event_init_context().
2216 */
2217 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2218 ctx1->parent_gen == ctx2->parent_gen)
2219 return 1;
2220
2221 /* Unmatched */
2222 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223}
2224
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225static void __perf_event_sync_stat(struct perf_event *event,
2226 struct perf_event *next_event)
2227{
2228 u64 value;
2229
2230 if (!event->attr.inherit_stat)
2231 return;
2232
2233 /*
2234 * Update the event value, we cannot use perf_event_read()
2235 * because we're in the middle of a context switch and have IRQs
2236 * disabled, which upsets smp_call_function_single(), however
2237 * we know the event must be on the current CPU, therefore we
2238 * don't need to use it.
2239 */
2240 switch (event->state) {
2241 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002242 event->pmu->read(event);
2243 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002244
2245 case PERF_EVENT_STATE_INACTIVE:
2246 update_event_times(event);
2247 break;
2248
2249 default:
2250 break;
2251 }
2252
2253 /*
2254 * In order to keep per-task stats reliable we need to flip the event
2255 * values when we flip the contexts.
2256 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002257 value = local64_read(&next_event->count);
2258 value = local64_xchg(&event->count, value);
2259 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002260
2261 swap(event->total_time_enabled, next_event->total_time_enabled);
2262 swap(event->total_time_running, next_event->total_time_running);
2263
2264 /*
2265 * Since we swizzled the values, update the user visible data too.
2266 */
2267 perf_event_update_userpage(event);
2268 perf_event_update_userpage(next_event);
2269}
2270
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271static void perf_event_sync_stat(struct perf_event_context *ctx,
2272 struct perf_event_context *next_ctx)
2273{
2274 struct perf_event *event, *next_event;
2275
2276 if (!ctx->nr_stat)
2277 return;
2278
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002279 update_context_time(ctx);
2280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281 event = list_first_entry(&ctx->event_list,
2282 struct perf_event, event_entry);
2283
2284 next_event = list_first_entry(&next_ctx->event_list,
2285 struct perf_event, event_entry);
2286
2287 while (&event->event_entry != &ctx->event_list &&
2288 &next_event->event_entry != &next_ctx->event_list) {
2289
2290 __perf_event_sync_stat(event, next_event);
2291
2292 event = list_next_entry(event, event_entry);
2293 next_event = list_next_entry(next_event, event_entry);
2294 }
2295}
2296
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002297static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2298 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002300 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002301 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002302 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002303 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304 int do_switch = 1;
2305
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002306 if (likely(!ctx))
2307 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002309 cpuctx = __get_cpu_context(ctx);
2310 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311 return;
2312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002313 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002314 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002315 if (!next_ctx)
2316 goto unlock;
2317
2318 parent = rcu_dereference(ctx->parent_ctx);
2319 next_parent = rcu_dereference(next_ctx->parent_ctx);
2320
2321 /* If neither context have a parent context; they cannot be clones. */
2322 if (!parent && !next_parent)
2323 goto unlock;
2324
2325 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326 /*
2327 * Looks like the two contexts are clones, so we might be
2328 * able to optimize the context switch. We lock both
2329 * contexts and check that they are clones under the
2330 * lock (including re-checking that neither has been
2331 * uncloned in the meantime). It doesn't matter which
2332 * order we take the locks because no other cpu could
2333 * be trying to lock both of these tasks.
2334 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002335 raw_spin_lock(&ctx->lock);
2336 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337 if (context_equiv(ctx, next_ctx)) {
2338 /*
2339 * XXX do we need a memory barrier of sorts
2340 * wrt to rcu_dereference() of perf_event_ctxp
2341 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002342 task->perf_event_ctxp[ctxn] = next_ctx;
2343 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344 ctx->task = next;
2345 next_ctx->task = task;
2346 do_switch = 0;
2347
2348 perf_event_sync_stat(ctx, next_ctx);
2349 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002350 raw_spin_unlock(&next_ctx->lock);
2351 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002353unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354 rcu_read_unlock();
2355
2356 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002357 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002358 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002359 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002360 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 }
2362}
2363
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002364#define for_each_task_context_nr(ctxn) \
2365 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2366
2367/*
2368 * Called from scheduler to remove the events of the current task,
2369 * with interrupts disabled.
2370 *
2371 * We stop each event and update the event value in event->count.
2372 *
2373 * This does not protect us against NMI, but disable()
2374 * sets the disabled bit in the control field of event _before_
2375 * accessing the event control register. If a NMI hits, then it will
2376 * not restart the event.
2377 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002378void __perf_event_task_sched_out(struct task_struct *task,
2379 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002380{
2381 int ctxn;
2382
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002383 for_each_task_context_nr(ctxn)
2384 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002385
2386 /*
2387 * if cgroup events exist on this CPU, then we need
2388 * to check if we have to switch out PMU state.
2389 * cgroup event are system-wide mode only
2390 */
2391 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002392 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002393}
2394
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002395static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002397 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002398
2399 if (!cpuctx->task_ctx)
2400 return;
2401
2402 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2403 return;
2404
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002405 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406 cpuctx->task_ctx = NULL;
2407}
2408
2409/*
2410 * Called with IRQs disabled
2411 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002412static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2413 enum event_type_t event_type)
2414{
2415 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002416}
2417
2418static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002419ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002420 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421{
2422 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002424 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2425 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002427 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 continue;
2429
Stephane Eraniane5d13672011-02-14 11:20:01 +02002430 /* may need to reset tstamp_enabled */
2431 if (is_cgroup_event(event))
2432 perf_cgroup_mark_enabled(event, ctx);
2433
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002434 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002435 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436
2437 /*
2438 * If this pinned group hasn't been scheduled,
2439 * put it in error state.
2440 */
2441 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2442 update_group_times(event);
2443 event->state = PERF_EVENT_STATE_ERROR;
2444 }
2445 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002446}
2447
2448static void
2449ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002450 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002451{
2452 struct perf_event *event;
2453 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002455 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2456 /* Ignore events in OFF or ERROR state */
2457 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459 /*
2460 * Listen to the 'cpu' scheduling filter constraint
2461 * of events:
2462 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002463 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002464 continue;
2465
Stephane Eraniane5d13672011-02-14 11:20:01 +02002466 /* may need to reset tstamp_enabled */
2467 if (is_cgroup_event(event))
2468 perf_cgroup_mark_enabled(event, ctx);
2469
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002470 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002471 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002473 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002474 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002475}
2476
2477static void
2478ctx_sched_in(struct perf_event_context *ctx,
2479 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002480 enum event_type_t event_type,
2481 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002482{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002483 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002484 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002485
Peter Zijlstradb24d332011-04-09 21:17:45 +02002486 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002487 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002488 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002489
Stephane Eraniane5d13672011-02-14 11:20:01 +02002490 now = perf_clock();
2491 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002492 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002493 /*
2494 * First go through the list and put on any pinned groups
2495 * in order to give them the best chance of going on.
2496 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002497 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002498 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002499
2500 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002501 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002502 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503}
2504
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002505static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002506 enum event_type_t event_type,
2507 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002508{
2509 struct perf_event_context *ctx = &cpuctx->ctx;
2510
Stephane Eraniane5d13672011-02-14 11:20:01 +02002511 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002512}
2513
Stephane Eraniane5d13672011-02-14 11:20:01 +02002514static void perf_event_context_sched_in(struct perf_event_context *ctx,
2515 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002517 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002519 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002520 if (cpuctx->task_ctx == ctx)
2521 return;
2522
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002523 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002524 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002525 /*
2526 * We want to keep the following priority order:
2527 * cpu pinned (that don't need to move), task pinned,
2528 * cpu flexible, task flexible.
2529 */
2530 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2531
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002532 if (ctx->nr_events)
2533 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002534
Gleb Natapov86b47c22011-11-22 16:08:21 +02002535 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2536
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002537 perf_pmu_enable(ctx->pmu);
2538 perf_ctx_unlock(cpuctx, ctx);
2539
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002540 /*
2541 * Since these rotations are per-cpu, we need to ensure the
2542 * cpu-context we got scheduled on is actually rotating.
2543 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002544 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002545}
2546
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002547/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002548 * When sampling the branck stack in system-wide, it may be necessary
2549 * to flush the stack on context switch. This happens when the branch
2550 * stack does not tag its entries with the pid of the current task.
2551 * Otherwise it becomes impossible to associate a branch entry with a
2552 * task. This ambiguity is more likely to appear when the branch stack
2553 * supports priv level filtering and the user sets it to monitor only
2554 * at the user level (which could be a useful measurement in system-wide
2555 * mode). In that case, the risk is high of having a branch stack with
2556 * branch from multiple tasks. Flushing may mean dropping the existing
2557 * entries or stashing them somewhere in the PMU specific code layer.
2558 *
2559 * This function provides the context switch callback to the lower code
2560 * layer. It is invoked ONLY when there is at least one system-wide context
2561 * with at least one active event using taken branch sampling.
2562 */
2563static void perf_branch_stack_sched_in(struct task_struct *prev,
2564 struct task_struct *task)
2565{
2566 struct perf_cpu_context *cpuctx;
2567 struct pmu *pmu;
2568 unsigned long flags;
2569
2570 /* no need to flush branch stack if not changing task */
2571 if (prev == task)
2572 return;
2573
2574 local_irq_save(flags);
2575
2576 rcu_read_lock();
2577
2578 list_for_each_entry_rcu(pmu, &pmus, entry) {
2579 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2580
2581 /*
2582 * check if the context has at least one
2583 * event using PERF_SAMPLE_BRANCH_STACK
2584 */
2585 if (cpuctx->ctx.nr_branch_stack > 0
2586 && pmu->flush_branch_stack) {
2587
Stephane Eraniand010b332012-02-09 23:21:00 +01002588 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2589
2590 perf_pmu_disable(pmu);
2591
2592 pmu->flush_branch_stack();
2593
2594 perf_pmu_enable(pmu);
2595
2596 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2597 }
2598 }
2599
2600 rcu_read_unlock();
2601
2602 local_irq_restore(flags);
2603}
2604
2605/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002606 * Called from scheduler to add the events of the current task
2607 * with interrupts disabled.
2608 *
2609 * We restore the event value and then enable it.
2610 *
2611 * This does not protect us against NMI, but enable()
2612 * sets the enabled bit in the control field of event _before_
2613 * accessing the event control register. If a NMI hits, then it will
2614 * keep the event running.
2615 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002616void __perf_event_task_sched_in(struct task_struct *prev,
2617 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002618{
2619 struct perf_event_context *ctx;
2620 int ctxn;
2621
2622 for_each_task_context_nr(ctxn) {
2623 ctx = task->perf_event_ctxp[ctxn];
2624 if (likely(!ctx))
2625 continue;
2626
Stephane Eraniane5d13672011-02-14 11:20:01 +02002627 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002628 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002629 /*
2630 * if cgroup events exist on this CPU, then we need
2631 * to check if we have to switch in PMU state.
2632 * cgroup event are system-wide mode only
2633 */
2634 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002635 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002636
2637 /* check for system-wide branch_stack events */
2638 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2639 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002640}
2641
Peter Zijlstraabd50712010-01-26 18:50:16 +01002642static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2643{
2644 u64 frequency = event->attr.sample_freq;
2645 u64 sec = NSEC_PER_SEC;
2646 u64 divisor, dividend;
2647
2648 int count_fls, nsec_fls, frequency_fls, sec_fls;
2649
2650 count_fls = fls64(count);
2651 nsec_fls = fls64(nsec);
2652 frequency_fls = fls64(frequency);
2653 sec_fls = 30;
2654
2655 /*
2656 * We got @count in @nsec, with a target of sample_freq HZ
2657 * the target period becomes:
2658 *
2659 * @count * 10^9
2660 * period = -------------------
2661 * @nsec * sample_freq
2662 *
2663 */
2664
2665 /*
2666 * Reduce accuracy by one bit such that @a and @b converge
2667 * to a similar magnitude.
2668 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002669#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002670do { \
2671 if (a##_fls > b##_fls) { \
2672 a >>= 1; \
2673 a##_fls--; \
2674 } else { \
2675 b >>= 1; \
2676 b##_fls--; \
2677 } \
2678} while (0)
2679
2680 /*
2681 * Reduce accuracy until either term fits in a u64, then proceed with
2682 * the other, so that finally we can do a u64/u64 division.
2683 */
2684 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2685 REDUCE_FLS(nsec, frequency);
2686 REDUCE_FLS(sec, count);
2687 }
2688
2689 if (count_fls + sec_fls > 64) {
2690 divisor = nsec * frequency;
2691
2692 while (count_fls + sec_fls > 64) {
2693 REDUCE_FLS(count, sec);
2694 divisor >>= 1;
2695 }
2696
2697 dividend = count * sec;
2698 } else {
2699 dividend = count * sec;
2700
2701 while (nsec_fls + frequency_fls > 64) {
2702 REDUCE_FLS(nsec, frequency);
2703 dividend >>= 1;
2704 }
2705
2706 divisor = nsec * frequency;
2707 }
2708
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002709 if (!divisor)
2710 return dividend;
2711
Peter Zijlstraabd50712010-01-26 18:50:16 +01002712 return div64_u64(dividend, divisor);
2713}
2714
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002715static DEFINE_PER_CPU(int, perf_throttled_count);
2716static DEFINE_PER_CPU(u64, perf_throttled_seq);
2717
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002718static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719{
2720 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002721 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002722 s64 delta;
2723
Peter Zijlstraabd50712010-01-26 18:50:16 +01002724 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725
2726 delta = (s64)(period - hwc->sample_period);
2727 delta = (delta + 7) / 8; /* low pass filter */
2728
2729 sample_period = hwc->sample_period + delta;
2730
2731 if (!sample_period)
2732 sample_period = 1;
2733
2734 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002735
Peter Zijlstrae7850592010-05-21 14:43:08 +02002736 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002737 if (disable)
2738 event->pmu->stop(event, PERF_EF_UPDATE);
2739
Peter Zijlstrae7850592010-05-21 14:43:08 +02002740 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002741
2742 if (disable)
2743 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002744 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745}
2746
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002747/*
2748 * combine freq adjustment with unthrottling to avoid two passes over the
2749 * events. At the same time, make sure, having freq events does not change
2750 * the rate of unthrottling as that would introduce bias.
2751 */
2752static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2753 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754{
2755 struct perf_event *event;
2756 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002757 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002758 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002759
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002760 /*
2761 * only need to iterate over all events iff:
2762 * - context have events in frequency mode (needs freq adjust)
2763 * - there are events to unthrottle on this cpu
2764 */
2765 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002766 return;
2767
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002768 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002769 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002770
Paul Mackerras03541f82009-10-14 16:58:03 +11002771 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772 if (event->state != PERF_EVENT_STATE_ACTIVE)
2773 continue;
2774
Stephane Eranian5632ab12011-01-03 18:20:01 +02002775 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002776 continue;
2777
Alexander Shishkin44377272013-12-16 14:17:36 +02002778 perf_pmu_disable(event->pmu);
2779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780 hwc = &event->hw;
2781
Jiri Olsaae23bff2013-08-24 16:45:54 +02002782 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002783 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002784 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002785 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002786 }
2787
2788 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002789 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002791 /*
2792 * stop the event and update event->count
2793 */
2794 event->pmu->stop(event, PERF_EF_UPDATE);
2795
Peter Zijlstrae7850592010-05-21 14:43:08 +02002796 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002797 delta = now - hwc->freq_count_stamp;
2798 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002799
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002800 /*
2801 * restart the event
2802 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002803 * we have stopped the event so tell that
2804 * to perf_adjust_period() to avoid stopping it
2805 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002806 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002807 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002808 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002809
2810 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002811 next:
2812 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002813 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002814
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002815 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002816 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002817}
2818
2819/*
2820 * Round-robin a context's events:
2821 */
2822static void rotate_ctx(struct perf_event_context *ctx)
2823{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002824 /*
2825 * Rotate the first entry last of non-pinned groups. Rotation might be
2826 * disabled by the inheritance code.
2827 */
2828 if (!ctx->rotate_disable)
2829 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002830}
2831
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002832/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002833 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2834 * because they're strictly cpu affine and rotate_start is called with IRQs
2835 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002836 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002837static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002838{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002839 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002840 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002842 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002843 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002844 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2845 rotate = 1;
2846 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002847
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002848 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002849 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002850 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002851 if (ctx->nr_events != ctx->nr_active)
2852 rotate = 1;
2853 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002855 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002856 goto done;
2857
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002858 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002859 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002860
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002861 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2862 if (ctx)
2863 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002864
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002865 rotate_ctx(&cpuctx->ctx);
2866 if (ctx)
2867 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002868
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002869 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002870
2871 perf_pmu_enable(cpuctx->ctx.pmu);
2872 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002873done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002874 if (remove)
2875 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002876
2877 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002878}
2879
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002880#ifdef CONFIG_NO_HZ_FULL
2881bool perf_event_can_stop_tick(void)
2882{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002883 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002884 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002885 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002886 else
2887 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002888}
2889#endif
2890
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002891void perf_event_task_tick(void)
2892{
2893 struct list_head *head = &__get_cpu_var(rotation_list);
2894 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002895 struct perf_event_context *ctx;
2896 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002897
2898 WARN_ON(!irqs_disabled());
2899
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002900 __this_cpu_inc(perf_throttled_seq);
2901 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2902
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002903 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002904 ctx = &cpuctx->ctx;
2905 perf_adjust_freq_unthr_context(ctx, throttled);
2906
2907 ctx = cpuctx->task_ctx;
2908 if (ctx)
2909 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002910 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911}
2912
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002913static int event_enable_on_exec(struct perf_event *event,
2914 struct perf_event_context *ctx)
2915{
2916 if (!event->attr.enable_on_exec)
2917 return 0;
2918
2919 event->attr.enable_on_exec = 0;
2920 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2921 return 0;
2922
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002923 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002924
2925 return 1;
2926}
2927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002928/*
2929 * Enable all of a task's events that have been marked enable-on-exec.
2930 * This expects task == current.
2931 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002932static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002933{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934 struct perf_event *event;
2935 unsigned long flags;
2936 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002937 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002938
2939 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002940 if (!ctx || !ctx->nr_events)
2941 goto out;
2942
Stephane Eraniane566b762011-04-06 02:54:54 +02002943 /*
2944 * We must ctxsw out cgroup events to avoid conflict
2945 * when invoking perf_task_event_sched_in() later on
2946 * in this function. Otherwise we end up trying to
2947 * ctxswin cgroup events which are already scheduled
2948 * in.
2949 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002950 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002952 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002953 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002954
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002955 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002956 ret = event_enable_on_exec(event, ctx);
2957 if (ret)
2958 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959 }
2960
2961 /*
2962 * Unclone this context if we enabled any event.
2963 */
2964 if (enabled)
2965 unclone_ctx(ctx);
2966
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002967 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968
Stephane Eraniane566b762011-04-06 02:54:54 +02002969 /*
2970 * Also calls ctxswin for cgroup events, if any:
2971 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002972 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002973out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002974 local_irq_restore(flags);
2975}
2976
Peter Zijlstrae041e322014-05-21 17:32:19 +02002977void perf_event_exec(void)
2978{
2979 struct perf_event_context *ctx;
2980 int ctxn;
2981
2982 rcu_read_lock();
2983 for_each_task_context_nr(ctxn) {
2984 ctx = current->perf_event_ctxp[ctxn];
2985 if (!ctx)
2986 continue;
2987
2988 perf_event_enable_on_exec(ctx);
2989 }
2990 rcu_read_unlock();
2991}
2992
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993/*
2994 * Cross CPU call to read the hardware event
2995 */
2996static void __perf_event_read(void *info)
2997{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998 struct perf_event *event = info;
2999 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003000 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001
3002 /*
3003 * If this is a task context, we need to check whether it is
3004 * the current task context of this cpu. If not it has been
3005 * scheduled out before the smp call arrived. In that case
3006 * event->count would have been updated to a recent sample
3007 * when the event was scheduled out.
3008 */
3009 if (ctx->task && cpuctx->task_ctx != ctx)
3010 return;
3011
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003012 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003013 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003014 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003015 update_cgrp_time_from_event(event);
3016 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003018 if (event->state == PERF_EVENT_STATE_ACTIVE)
3019 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003020 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003021}
3022
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003023static inline u64 perf_event_count(struct perf_event *event)
3024{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003025 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003026}
3027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003028static u64 perf_event_read(struct perf_event *event)
3029{
3030 /*
3031 * If event is enabled and currently active on a CPU, update the
3032 * value in the event structure:
3033 */
3034 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3035 smp_call_function_single(event->oncpu,
3036 __perf_event_read, event, 1);
3037 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003038 struct perf_event_context *ctx = event->ctx;
3039 unsigned long flags;
3040
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003041 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003042 /*
3043 * may read while context is not active
3044 * (e.g., thread is blocked), in that case
3045 * we cannot update context time
3046 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003047 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003048 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003049 update_cgrp_time_from_event(event);
3050 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003052 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053 }
3054
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003055 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056}
3057
3058/*
3059 * Initialize the perf_event context in a task_struct:
3060 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003061static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003063 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003065 INIT_LIST_HEAD(&ctx->pinned_groups);
3066 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 INIT_LIST_HEAD(&ctx->event_list);
3068 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003069}
3070
Peter Zijlstraeb184472010-09-07 15:55:13 +02003071static struct perf_event_context *
3072alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003073{
3074 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003075
3076 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3077 if (!ctx)
3078 return NULL;
3079
3080 __perf_event_init_context(ctx);
3081 if (task) {
3082 ctx->task = task;
3083 get_task_struct(task);
3084 }
3085 ctx->pmu = pmu;
3086
3087 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003088}
3089
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003090static struct task_struct *
3091find_lively_task_by_vpid(pid_t vpid)
3092{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003093 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003094 int err;
3095
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003097 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003098 task = current;
3099 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003100 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101 if (task)
3102 get_task_struct(task);
3103 rcu_read_unlock();
3104
3105 if (!task)
3106 return ERR_PTR(-ESRCH);
3107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 /* Reuse ptrace permission checks for now. */
3109 err = -EACCES;
3110 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3111 goto errout;
3112
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003113 return task;
3114errout:
3115 put_task_struct(task);
3116 return ERR_PTR(err);
3117
3118}
3119
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003120/*
3121 * Returns a matching context with refcount and pincount.
3122 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003123static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003124find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003125{
3126 struct perf_event_context *ctx;
3127 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003129 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003131 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003132 /* Must be root to operate on a CPU event: */
3133 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3134 return ERR_PTR(-EACCES);
3135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136 /*
3137 * We could be clever and allow to attach a event to an
3138 * offline CPU and activate it when the CPU comes up, but
3139 * that's for later.
3140 */
3141 if (!cpu_online(cpu))
3142 return ERR_PTR(-ENODEV);
3143
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003144 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003145 ctx = &cpuctx->ctx;
3146 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003147 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148
3149 return ctx;
3150 }
3151
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003152 err = -EINVAL;
3153 ctxn = pmu->task_ctx_nr;
3154 if (ctxn < 0)
3155 goto errout;
3156
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003157retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003158 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003159 if (ctx) {
3160 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003161 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003162 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003163 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003164 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165 err = -ENOMEM;
3166 if (!ctx)
3167 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003168
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003169 err = 0;
3170 mutex_lock(&task->perf_event_mutex);
3171 /*
3172 * If it has already passed perf_event_exit_task().
3173 * we must see PF_EXITING, it takes this mutex too.
3174 */
3175 if (task->flags & PF_EXITING)
3176 err = -ESRCH;
3177 else if (task->perf_event_ctxp[ctxn])
3178 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003179 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003180 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003181 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003182 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003183 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003184 mutex_unlock(&task->perf_event_mutex);
3185
3186 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003187 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003188
3189 if (err == -EAGAIN)
3190 goto retry;
3191 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193 }
3194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003195 return ctx;
3196
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003197errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198 return ERR_PTR(err);
3199}
3200
Li Zefan6fb29152009-10-15 11:21:42 +08003201static void perf_event_free_filter(struct perf_event *event);
3202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203static void free_event_rcu(struct rcu_head *head)
3204{
3205 struct perf_event *event;
3206
3207 event = container_of(head, struct perf_event, rcu_head);
3208 if (event->ns)
3209 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003210 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211 kfree(event);
3212}
3213
Frederic Weisbecker76369132011-05-19 19:55:04 +02003214static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003215static void ring_buffer_attach(struct perf_event *event,
3216 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003218static void unaccount_event_cpu(struct perf_event *event, int cpu)
3219{
3220 if (event->parent)
3221 return;
3222
3223 if (has_branch_stack(event)) {
3224 if (!(event->attach_state & PERF_ATTACH_TASK))
3225 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3226 }
3227 if (is_cgroup_event(event))
3228 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3229}
3230
3231static void unaccount_event(struct perf_event *event)
3232{
3233 if (event->parent)
3234 return;
3235
3236 if (event->attach_state & PERF_ATTACH_TASK)
3237 static_key_slow_dec_deferred(&perf_sched_events);
3238 if (event->attr.mmap || event->attr.mmap_data)
3239 atomic_dec(&nr_mmap_events);
3240 if (event->attr.comm)
3241 atomic_dec(&nr_comm_events);
3242 if (event->attr.task)
3243 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003244 if (event->attr.freq)
3245 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003246 if (is_cgroup_event(event))
3247 static_key_slow_dec_deferred(&perf_sched_events);
3248 if (has_branch_stack(event))
3249 static_key_slow_dec_deferred(&perf_sched_events);
3250
3251 unaccount_event_cpu(event, event->cpu);
3252}
3253
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003254static void __free_event(struct perf_event *event)
3255{
3256 if (!event->parent) {
3257 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3258 put_callchain_buffers();
3259 }
3260
3261 if (event->destroy)
3262 event->destroy(event);
3263
3264 if (event->ctx)
3265 put_ctx(event->ctx);
3266
Yan, Zhengc464c762014-03-18 16:56:41 +08003267 if (event->pmu)
3268 module_put(event->pmu->module);
3269
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003270 call_rcu(&event->rcu_head, free_event_rcu);
3271}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003272
3273static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003275 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003277 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278
Frederic Weisbecker76369132011-05-19 19:55:04 +02003279 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003280 /*
3281 * Can happen when we close an event with re-directed output.
3282 *
3283 * Since we have a 0 refcount, perf_mmap_close() will skip
3284 * over us; possibly making our ring_buffer_put() the last.
3285 */
3286 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003287 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003288 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289 }
3290
Stephane Eraniane5d13672011-02-14 11:20:01 +02003291 if (is_cgroup_event(event))
3292 perf_detach_cgroup(event);
3293
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003294 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003295}
3296
Peter Zijlstra683ede42014-05-05 12:11:24 +02003297/*
3298 * Used to free events which have a known refcount of 1, such as in error paths
3299 * where the event isn't exposed yet and inherited events.
3300 */
3301static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003302{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003303 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3304 "unexpected event refcount: %ld; ptr=%p\n",
3305 atomic_long_read(&event->refcount), event)) {
3306 /* leak to avoid use-after-free */
3307 return;
3308 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003309
Peter Zijlstra683ede42014-05-05 12:11:24 +02003310 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003311}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003312
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003313/*
3314 * Called when the last reference to the file is gone.
3315 */
Al Viroa6fa9412012-08-20 14:59:25 +01003316static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003317{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003318 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra88821352010-11-09 19:01:43 +01003319 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003320
Al Viroa6fa9412012-08-20 14:59:25 +01003321 if (!atomic_long_dec_and_test(&event->refcount))
3322 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003323
Peter Zijlstra88821352010-11-09 19:01:43 +01003324 rcu_read_lock();
3325 owner = ACCESS_ONCE(event->owner);
3326 /*
3327 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3328 * !owner it means the list deletion is complete and we can indeed
3329 * free this event, otherwise we need to serialize on
3330 * owner->perf_event_mutex.
3331 */
3332 smp_read_barrier_depends();
3333 if (owner) {
3334 /*
3335 * Since delayed_put_task_struct() also drops the last
3336 * task reference we can safely take a new reference
3337 * while holding the rcu_read_lock().
3338 */
3339 get_task_struct(owner);
3340 }
3341 rcu_read_unlock();
3342
3343 if (owner) {
3344 mutex_lock(&owner->perf_event_mutex);
3345 /*
3346 * We have to re-check the event->owner field, if it is cleared
3347 * we raced with perf_event_exit_task(), acquiring the mutex
3348 * ensured they're done, and we can proceed with freeing the
3349 * event.
3350 */
3351 if (event->owner)
3352 list_del_init(&event->owner_entry);
3353 mutex_unlock(&owner->perf_event_mutex);
3354 put_task_struct(owner);
3355 }
3356
Peter Zijlstra683ede42014-05-05 12:11:24 +02003357 WARN_ON_ONCE(ctx->parent_ctx);
3358 /*
3359 * There are two ways this annotation is useful:
3360 *
3361 * 1) there is a lock recursion from perf_event_exit_task
3362 * see the comment there.
3363 *
3364 * 2) there is a lock-inversion with mmap_sem through
3365 * perf_event_read_group(), which takes faults while
3366 * holding ctx->mutex, however this is called after
3367 * the last filedesc died, so there is no possibility
3368 * to trigger the AB-BA case.
3369 */
3370 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3371 perf_remove_from_context(event, true);
3372 mutex_unlock(&ctx->mutex);
3373
3374 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003375}
3376
Peter Zijlstra683ede42014-05-05 12:11:24 +02003377int perf_event_release_kernel(struct perf_event *event)
3378{
3379 put_event(event);
3380 return 0;
3381}
3382EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3383
Al Viroa6fa9412012-08-20 14:59:25 +01003384static int perf_release(struct inode *inode, struct file *file)
3385{
3386 put_event(file->private_data);
3387 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003388}
3389
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003390u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391{
3392 struct perf_event *child;
3393 u64 total = 0;
3394
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003395 *enabled = 0;
3396 *running = 0;
3397
Peter Zijlstra6f105812009-11-20 22:19:56 +01003398 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003400 *enabled += event->total_time_enabled +
3401 atomic64_read(&event->child_total_time_enabled);
3402 *running += event->total_time_running +
3403 atomic64_read(&event->child_total_time_running);
3404
3405 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003407 *enabled += child->total_time_enabled;
3408 *running += child->total_time_running;
3409 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003410 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411
3412 return total;
3413}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003414EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003416static int perf_event_read_group(struct perf_event *event,
3417 u64 read_format, char __user *buf)
3418{
3419 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003420 int n = 0, size = 0, ret = -EFAULT;
3421 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003422 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003423 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003424
Peter Zijlstra6f105812009-11-20 22:19:56 +01003425 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003426 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427
3428 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003429 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3430 values[n++] = enabled;
3431 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3432 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003433 values[n++] = count;
3434 if (read_format & PERF_FORMAT_ID)
3435 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003436
3437 size = n * sizeof(u64);
3438
3439 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003440 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003441
Peter Zijlstra6f105812009-11-20 22:19:56 +01003442 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003443
3444 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003445 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003446
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003447 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003448 if (read_format & PERF_FORMAT_ID)
3449 values[n++] = primary_event_id(sub);
3450
3451 size = n * sizeof(u64);
3452
Stephane Eranian184d3da2009-11-23 21:40:49 -08003453 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003454 ret = -EFAULT;
3455 goto unlock;
3456 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003457
3458 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003460unlock:
3461 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003462
Peter Zijlstraabf48682009-11-20 22:19:49 +01003463 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464}
3465
3466static int perf_event_read_one(struct perf_event *event,
3467 u64 read_format, char __user *buf)
3468{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003469 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003470 u64 values[4];
3471 int n = 0;
3472
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003473 values[n++] = perf_event_read_value(event, &enabled, &running);
3474 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3475 values[n++] = enabled;
3476 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3477 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478 if (read_format & PERF_FORMAT_ID)
3479 values[n++] = primary_event_id(event);
3480
3481 if (copy_to_user(buf, values, n * sizeof(u64)))
3482 return -EFAULT;
3483
3484 return n * sizeof(u64);
3485}
3486
3487/*
3488 * Read the performance event - simple non blocking version for now
3489 */
3490static ssize_t
3491perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3492{
3493 u64 read_format = event->attr.read_format;
3494 int ret;
3495
3496 /*
3497 * Return end-of-file for a read on a event that is in
3498 * error state (i.e. because it was pinned but it couldn't be
3499 * scheduled on to the CPU at some point).
3500 */
3501 if (event->state == PERF_EVENT_STATE_ERROR)
3502 return 0;
3503
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003504 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003505 return -ENOSPC;
3506
3507 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508 if (read_format & PERF_FORMAT_GROUP)
3509 ret = perf_event_read_group(event, read_format, buf);
3510 else
3511 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003512
3513 return ret;
3514}
3515
3516static ssize_t
3517perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3518{
3519 struct perf_event *event = file->private_data;
3520
3521 return perf_read_hw(event, buf, count);
3522}
3523
3524static unsigned int perf_poll(struct file *file, poll_table *wait)
3525{
3526 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003527 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003528 unsigned int events = POLL_HUP;
3529
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003530 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003531 * Pin the event->rb by taking event->mmap_mutex; otherwise
3532 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003533 */
3534 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003535 rb = event->rb;
3536 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003537 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003538 mutex_unlock(&event->mmap_mutex);
3539
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003540 poll_wait(file, &event->waitq, wait);
3541
3542 return events;
3543}
3544
3545static void perf_event_reset(struct perf_event *event)
3546{
3547 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003548 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549 perf_event_update_userpage(event);
3550}
3551
3552/*
3553 * Holding the top-level event's child_mutex means that any
3554 * descendant process that has inherited this event will block
3555 * in sync_child_event if it goes to exit, thus satisfying the
3556 * task existence requirements of perf_event_enable/disable.
3557 */
3558static void perf_event_for_each_child(struct perf_event *event,
3559 void (*func)(struct perf_event *))
3560{
3561 struct perf_event *child;
3562
3563 WARN_ON_ONCE(event->ctx->parent_ctx);
3564 mutex_lock(&event->child_mutex);
3565 func(event);
3566 list_for_each_entry(child, &event->child_list, child_list)
3567 func(child);
3568 mutex_unlock(&event->child_mutex);
3569}
3570
3571static void perf_event_for_each(struct perf_event *event,
3572 void (*func)(struct perf_event *))
3573{
3574 struct perf_event_context *ctx = event->ctx;
3575 struct perf_event *sibling;
3576
3577 WARN_ON_ONCE(ctx->parent_ctx);
3578 mutex_lock(&ctx->mutex);
3579 event = event->group_leader;
3580
3581 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003583 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003584 mutex_unlock(&ctx->mutex);
3585}
3586
3587static int perf_event_period(struct perf_event *event, u64 __user *arg)
3588{
3589 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003590 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591 u64 value;
3592
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003593 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 return -EINVAL;
3595
John Blackwoodad0cf342010-09-28 18:03:11 -04003596 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003597 return -EFAULT;
3598
3599 if (!value)
3600 return -EINVAL;
3601
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003602 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003603 if (event->attr.freq) {
3604 if (value > sysctl_perf_event_sample_rate) {
3605 ret = -EINVAL;
3606 goto unlock;
3607 }
3608
3609 event->attr.sample_freq = value;
3610 } else {
3611 event->attr.sample_period = value;
3612 event->hw.sample_period = value;
3613 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003614
3615 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3616 if (active) {
3617 perf_pmu_disable(ctx->pmu);
3618 event->pmu->stop(event, PERF_EF_UPDATE);
3619 }
3620
3621 local64_set(&event->hw.period_left, 0);
3622
3623 if (active) {
3624 event->pmu->start(event, PERF_EF_RELOAD);
3625 perf_pmu_enable(ctx->pmu);
3626 }
3627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003628unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003629 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003630
3631 return ret;
3632}
3633
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003634static const struct file_operations perf_fops;
3635
Al Viro2903ff02012-08-28 12:52:22 -04003636static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003637{
Al Viro2903ff02012-08-28 12:52:22 -04003638 struct fd f = fdget(fd);
3639 if (!f.file)
3640 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003641
Al Viro2903ff02012-08-28 12:52:22 -04003642 if (f.file->f_op != &perf_fops) {
3643 fdput(f);
3644 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003645 }
Al Viro2903ff02012-08-28 12:52:22 -04003646 *p = f;
3647 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003648}
3649
3650static int perf_event_set_output(struct perf_event *event,
3651 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003652static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003653
3654static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3655{
3656 struct perf_event *event = file->private_data;
3657 void (*func)(struct perf_event *);
3658 u32 flags = arg;
3659
3660 switch (cmd) {
3661 case PERF_EVENT_IOC_ENABLE:
3662 func = perf_event_enable;
3663 break;
3664 case PERF_EVENT_IOC_DISABLE:
3665 func = perf_event_disable;
3666 break;
3667 case PERF_EVENT_IOC_RESET:
3668 func = perf_event_reset;
3669 break;
3670
3671 case PERF_EVENT_IOC_REFRESH:
3672 return perf_event_refresh(event, arg);
3673
3674 case PERF_EVENT_IOC_PERIOD:
3675 return perf_event_period(event, (u64 __user *)arg);
3676
Jiri Olsacf4957f2012-10-24 13:37:58 +02003677 case PERF_EVENT_IOC_ID:
3678 {
3679 u64 id = primary_event_id(event);
3680
3681 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3682 return -EFAULT;
3683 return 0;
3684 }
3685
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003686 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003687 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003688 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003689 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003690 struct perf_event *output_event;
3691 struct fd output;
3692 ret = perf_fget_light(arg, &output);
3693 if (ret)
3694 return ret;
3695 output_event = output.file->private_data;
3696 ret = perf_event_set_output(event, output_event);
3697 fdput(output);
3698 } else {
3699 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003700 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003701 return ret;
3702 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003703
Li Zefan6fb29152009-10-15 11:21:42 +08003704 case PERF_EVENT_IOC_SET_FILTER:
3705 return perf_event_set_filter(event, (void __user *)arg);
3706
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 default:
3708 return -ENOTTY;
3709 }
3710
3711 if (flags & PERF_IOC_FLAG_GROUP)
3712 perf_event_for_each(event, func);
3713 else
3714 perf_event_for_each_child(event, func);
3715
3716 return 0;
3717}
3718
3719int perf_event_task_enable(void)
3720{
3721 struct perf_event *event;
3722
3723 mutex_lock(&current->perf_event_mutex);
3724 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3725 perf_event_for_each_child(event, perf_event_enable);
3726 mutex_unlock(&current->perf_event_mutex);
3727
3728 return 0;
3729}
3730
3731int perf_event_task_disable(void)
3732{
3733 struct perf_event *event;
3734
3735 mutex_lock(&current->perf_event_mutex);
3736 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3737 perf_event_for_each_child(event, perf_event_disable);
3738 mutex_unlock(&current->perf_event_mutex);
3739
3740 return 0;
3741}
3742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743static int perf_event_index(struct perf_event *event)
3744{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003745 if (event->hw.state & PERF_HES_STOPPED)
3746 return 0;
3747
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003748 if (event->state != PERF_EVENT_STATE_ACTIVE)
3749 return 0;
3750
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003751 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003752}
3753
Eric B Munsonc4794292011-06-23 16:34:38 -04003754static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003755 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003756 u64 *enabled,
3757 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003758{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003759 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003760
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003761 *now = perf_clock();
3762 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003763 *enabled = ctx_time - event->tstamp_enabled;
3764 *running = ctx_time - event->tstamp_running;
3765}
3766
Peter Zijlstrafa731582013-09-19 10:16:42 +02003767static void perf_event_init_userpage(struct perf_event *event)
3768{
3769 struct perf_event_mmap_page *userpg;
3770 struct ring_buffer *rb;
3771
3772 rcu_read_lock();
3773 rb = rcu_dereference(event->rb);
3774 if (!rb)
3775 goto unlock;
3776
3777 userpg = rb->user_page;
3778
3779 /* Allow new userspace to detect that bit 0 is deprecated */
3780 userpg->cap_bit0_is_deprecated = 1;
3781 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3782
3783unlock:
3784 rcu_read_unlock();
3785}
3786
Peter Zijlstrac7206202012-03-22 17:26:36 +01003787void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003788{
3789}
3790
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791/*
3792 * Callers need to ensure there can be no nesting of this function, otherwise
3793 * the seqlock logic goes bad. We can not serialize this because the arch
3794 * code calls this from NMI context.
3795 */
3796void perf_event_update_userpage(struct perf_event *event)
3797{
3798 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003799 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003800 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003801
3802 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003803 rb = rcu_dereference(event->rb);
3804 if (!rb)
3805 goto unlock;
3806
Eric B Munson0d641202011-06-24 12:26:26 -04003807 /*
3808 * compute total_time_enabled, total_time_running
3809 * based on snapshot values taken when the event
3810 * was last scheduled in.
3811 *
3812 * we cannot simply called update_context_time()
3813 * because of locking issue as we can be called in
3814 * NMI context
3815 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003816 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003817
Frederic Weisbecker76369132011-05-19 19:55:04 +02003818 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819 /*
3820 * Disable preemption so as to not let the corresponding user-space
3821 * spin too long if we get preempted.
3822 */
3823 preempt_disable();
3824 ++userpg->lock;
3825 barrier();
3826 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003827 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003828 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003829 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003830
Eric B Munson0d641202011-06-24 12:26:26 -04003831 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003832 atomic64_read(&event->child_total_time_enabled);
3833
Eric B Munson0d641202011-06-24 12:26:26 -04003834 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003835 atomic64_read(&event->child_total_time_running);
3836
Peter Zijlstrac7206202012-03-22 17:26:36 +01003837 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003839 barrier();
3840 ++userpg->lock;
3841 preempt_enable();
3842unlock:
3843 rcu_read_unlock();
3844}
3845
Peter Zijlstra906010b2009-09-21 16:08:49 +02003846static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3847{
3848 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003849 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003850 int ret = VM_FAULT_SIGBUS;
3851
3852 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3853 if (vmf->pgoff == 0)
3854 ret = 0;
3855 return ret;
3856 }
3857
3858 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003859 rb = rcu_dereference(event->rb);
3860 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003861 goto unlock;
3862
3863 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3864 goto unlock;
3865
Frederic Weisbecker76369132011-05-19 19:55:04 +02003866 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003867 if (!vmf->page)
3868 goto unlock;
3869
3870 get_page(vmf->page);
3871 vmf->page->mapping = vma->vm_file->f_mapping;
3872 vmf->page->index = vmf->pgoff;
3873
3874 ret = 0;
3875unlock:
3876 rcu_read_unlock();
3877
3878 return ret;
3879}
3880
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003881static void ring_buffer_attach(struct perf_event *event,
3882 struct ring_buffer *rb)
3883{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003884 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003885 unsigned long flags;
3886
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003887 if (event->rb) {
3888 /*
3889 * Should be impossible, we set this when removing
3890 * event->rb_entry and wait/clear when adding event->rb_entry.
3891 */
3892 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003893
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003894 old_rb = event->rb;
3895 event->rcu_batches = get_state_synchronize_rcu();
3896 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003897
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003898 spin_lock_irqsave(&old_rb->event_lock, flags);
3899 list_del_rcu(&event->rb_entry);
3900 spin_unlock_irqrestore(&old_rb->event_lock, flags);
3901 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003902
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003903 if (event->rcu_pending && rb) {
3904 cond_synchronize_rcu(event->rcu_batches);
3905 event->rcu_pending = 0;
3906 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003907
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003908 if (rb) {
3909 spin_lock_irqsave(&rb->event_lock, flags);
3910 list_add_rcu(&event->rb_entry, &rb->event_list);
3911 spin_unlock_irqrestore(&rb->event_lock, flags);
3912 }
3913
3914 rcu_assign_pointer(event->rb, rb);
3915
3916 if (old_rb) {
3917 ring_buffer_put(old_rb);
3918 /*
3919 * Since we detached before setting the new rb, so that we
3920 * could attach the new rb, we could have missed a wakeup.
3921 * Provide it now.
3922 */
3923 wake_up_all(&event->waitq);
3924 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003925}
3926
3927static void ring_buffer_wakeup(struct perf_event *event)
3928{
3929 struct ring_buffer *rb;
3930
3931 rcu_read_lock();
3932 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003933 if (rb) {
3934 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3935 wake_up_all(&event->waitq);
3936 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003937 rcu_read_unlock();
3938}
3939
Frederic Weisbecker76369132011-05-19 19:55:04 +02003940static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003941{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003942 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003943
Frederic Weisbecker76369132011-05-19 19:55:04 +02003944 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3945 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003946}
3947
Frederic Weisbecker76369132011-05-19 19:55:04 +02003948static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003950 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003951
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003952 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003953 rb = rcu_dereference(event->rb);
3954 if (rb) {
3955 if (!atomic_inc_not_zero(&rb->refcount))
3956 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003957 }
3958 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959
Frederic Weisbecker76369132011-05-19 19:55:04 +02003960 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003961}
3962
Frederic Weisbecker76369132011-05-19 19:55:04 +02003963static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003964{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003965 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003966 return;
3967
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003968 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003969
Frederic Weisbecker76369132011-05-19 19:55:04 +02003970 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003971}
3972
3973static void perf_mmap_open(struct vm_area_struct *vma)
3974{
3975 struct perf_event *event = vma->vm_file->private_data;
3976
3977 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003978 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979}
3980
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003981/*
3982 * A buffer can be mmap()ed multiple times; either directly through the same
3983 * event, or through other events by use of perf_event_set_output().
3984 *
3985 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3986 * the buffer here, where we still have a VM context. This means we need
3987 * to detach all events redirecting to us.
3988 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989static void perf_mmap_close(struct vm_area_struct *vma)
3990{
3991 struct perf_event *event = vma->vm_file->private_data;
3992
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003993 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003994 struct user_struct *mmap_user = rb->mmap_user;
3995 int mmap_locked = rb->mmap_locked;
3996 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003998 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003999
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004000 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004001 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004002
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004003 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004004 mutex_unlock(&event->mmap_mutex);
4005
4006 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004007 if (atomic_read(&rb->mmap_count))
4008 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004009
4010 /*
4011 * No other mmap()s, detach from all other events that might redirect
4012 * into the now unreachable buffer. Somewhat complicated by the
4013 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4014 */
4015again:
4016 rcu_read_lock();
4017 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4018 if (!atomic_long_inc_not_zero(&event->refcount)) {
4019 /*
4020 * This event is en-route to free_event() which will
4021 * detach it and remove it from the list.
4022 */
4023 continue;
4024 }
4025 rcu_read_unlock();
4026
4027 mutex_lock(&event->mmap_mutex);
4028 /*
4029 * Check we didn't race with perf_event_set_output() which can
4030 * swizzle the rb from under us while we were waiting to
4031 * acquire mmap_mutex.
4032 *
4033 * If we find a different rb; ignore this event, a next
4034 * iteration will no longer find it on the list. We have to
4035 * still restart the iteration to make sure we're not now
4036 * iterating the wrong list.
4037 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004038 if (event->rb == rb)
4039 ring_buffer_attach(event, NULL);
4040
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004041 mutex_unlock(&event->mmap_mutex);
4042 put_event(event);
4043
4044 /*
4045 * Restart the iteration; either we're on the wrong list or
4046 * destroyed its integrity by doing a deletion.
4047 */
4048 goto again;
4049 }
4050 rcu_read_unlock();
4051
4052 /*
4053 * It could be there's still a few 0-ref events on the list; they'll
4054 * get cleaned up by free_event() -- they'll also still have their
4055 * ref on the rb and will free it whenever they are done with it.
4056 *
4057 * Aside from that, this buffer is 'fully' detached and unmapped,
4058 * undo the VM accounting.
4059 */
4060
4061 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4062 vma->vm_mm->pinned_vm -= mmap_locked;
4063 free_uid(mmap_user);
4064
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004065out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004066 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067}
4068
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004069static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070 .open = perf_mmap_open,
4071 .close = perf_mmap_close,
4072 .fault = perf_mmap_fault,
4073 .page_mkwrite = perf_mmap_fault,
4074};
4075
4076static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4077{
4078 struct perf_event *event = file->private_data;
4079 unsigned long user_locked, user_lock_limit;
4080 struct user_struct *user = current_user();
4081 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004082 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083 unsigned long vma_size;
4084 unsigned long nr_pages;
4085 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004086 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087
Peter Zijlstrac7920612010-05-18 10:33:24 +02004088 /*
4089 * Don't allow mmap() of inherited per-task counters. This would
4090 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004091 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004092 */
4093 if (event->cpu == -1 && event->attr.inherit)
4094 return -EINVAL;
4095
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096 if (!(vma->vm_flags & VM_SHARED))
4097 return -EINVAL;
4098
4099 vma_size = vma->vm_end - vma->vm_start;
4100 nr_pages = (vma_size / PAGE_SIZE) - 1;
4101
4102 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004103 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104 * can do bitmasks instead of modulo.
4105 */
4106 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4107 return -EINVAL;
4108
4109 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4110 return -EINVAL;
4111
4112 if (vma->vm_pgoff != 0)
4113 return -EINVAL;
4114
4115 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004116again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004118 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004119 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004121 goto unlock;
4122 }
4123
4124 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4125 /*
4126 * Raced against perf_mmap_close() through
4127 * perf_event_set_output(). Try again, hope for better
4128 * luck.
4129 */
4130 mutex_unlock(&event->mmap_mutex);
4131 goto again;
4132 }
4133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134 goto unlock;
4135 }
4136
4137 user_extra = nr_pages + 1;
4138 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4139
4140 /*
4141 * Increase the limit linearly with more CPUs:
4142 */
4143 user_lock_limit *= num_online_cpus();
4144
4145 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4146
4147 extra = 0;
4148 if (user_locked > user_lock_limit)
4149 extra = user_locked - user_lock_limit;
4150
Jiri Slaby78d7d402010-03-05 13:42:54 -08004151 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004153 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154
4155 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4156 !capable(CAP_IPC_LOCK)) {
4157 ret = -EPERM;
4158 goto unlock;
4159 }
4160
Frederic Weisbecker76369132011-05-19 19:55:04 +02004161 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004162
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004163 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004164 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004165
Vince Weaver4ec83632011-06-01 15:15:36 -04004166 rb = rb_alloc(nr_pages,
4167 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4168 event->cpu, flags);
4169
Frederic Weisbecker76369132011-05-19 19:55:04 +02004170 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004171 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004173 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004174
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004175 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004176 rb->mmap_locked = extra;
4177 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004179 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004180 vma->vm_mm->pinned_vm += extra;
4181
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004182 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004183
Peter Zijlstrafa731582013-09-19 10:16:42 +02004184 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004185 perf_event_update_userpage(event);
4186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004187unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004188 if (!ret)
4189 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190 mutex_unlock(&event->mmap_mutex);
4191
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004192 /*
4193 * Since pinned accounting is per vm we cannot allow fork() to copy our
4194 * vma.
4195 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004196 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004197 vma->vm_ops = &perf_mmap_vmops;
4198
4199 return ret;
4200}
4201
4202static int perf_fasync(int fd, struct file *filp, int on)
4203{
Al Viro496ad9a2013-01-23 17:07:38 -05004204 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205 struct perf_event *event = filp->private_data;
4206 int retval;
4207
4208 mutex_lock(&inode->i_mutex);
4209 retval = fasync_helper(fd, filp, on, &event->fasync);
4210 mutex_unlock(&inode->i_mutex);
4211
4212 if (retval < 0)
4213 return retval;
4214
4215 return 0;
4216}
4217
4218static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004219 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004220 .release = perf_release,
4221 .read = perf_read,
4222 .poll = perf_poll,
4223 .unlocked_ioctl = perf_ioctl,
4224 .compat_ioctl = perf_ioctl,
4225 .mmap = perf_mmap,
4226 .fasync = perf_fasync,
4227};
4228
4229/*
4230 * Perf event wakeup
4231 *
4232 * If there's data, ensure we set the poll() state and publish everything
4233 * to user-space before waking everybody up.
4234 */
4235
4236void perf_event_wakeup(struct perf_event *event)
4237{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004238 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239
4240 if (event->pending_kill) {
4241 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4242 event->pending_kill = 0;
4243 }
4244}
4245
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004246static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004247{
4248 struct perf_event *event = container_of(entry,
4249 struct perf_event, pending);
4250
4251 if (event->pending_disable) {
4252 event->pending_disable = 0;
4253 __perf_event_disable(event);
4254 }
4255
4256 if (event->pending_wakeup) {
4257 event->pending_wakeup = 0;
4258 perf_event_wakeup(event);
4259 }
4260}
4261
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004263 * We assume there is only KVM supporting the callbacks.
4264 * Later on, we might change it to a list if there is
4265 * another virtualization implementation supporting the callbacks.
4266 */
4267struct perf_guest_info_callbacks *perf_guest_cbs;
4268
4269int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4270{
4271 perf_guest_cbs = cbs;
4272 return 0;
4273}
4274EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4275
4276int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4277{
4278 perf_guest_cbs = NULL;
4279 return 0;
4280}
4281EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4282
Jiri Olsa40189942012-08-07 15:20:37 +02004283static void
4284perf_output_sample_regs(struct perf_output_handle *handle,
4285 struct pt_regs *regs, u64 mask)
4286{
4287 int bit;
4288
4289 for_each_set_bit(bit, (const unsigned long *) &mask,
4290 sizeof(mask) * BITS_PER_BYTE) {
4291 u64 val;
4292
4293 val = perf_reg_value(regs, bit);
4294 perf_output_put(handle, val);
4295 }
4296}
4297
4298static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4299 struct pt_regs *regs)
4300{
4301 if (!user_mode(regs)) {
4302 if (current->mm)
4303 regs = task_pt_regs(current);
4304 else
4305 regs = NULL;
4306 }
4307
4308 if (regs) {
4309 regs_user->regs = regs;
4310 regs_user->abi = perf_reg_abi(current);
4311 }
4312}
4313
Jiri Olsac5ebced2012-08-07 15:20:40 +02004314/*
4315 * Get remaining task size from user stack pointer.
4316 *
4317 * It'd be better to take stack vma map and limit this more
4318 * precisly, but there's no way to get it safely under interrupt,
4319 * so using TASK_SIZE as limit.
4320 */
4321static u64 perf_ustack_task_size(struct pt_regs *regs)
4322{
4323 unsigned long addr = perf_user_stack_pointer(regs);
4324
4325 if (!addr || addr >= TASK_SIZE)
4326 return 0;
4327
4328 return TASK_SIZE - addr;
4329}
4330
4331static u16
4332perf_sample_ustack_size(u16 stack_size, u16 header_size,
4333 struct pt_regs *regs)
4334{
4335 u64 task_size;
4336
4337 /* No regs, no stack pointer, no dump. */
4338 if (!regs)
4339 return 0;
4340
4341 /*
4342 * Check if we fit in with the requested stack size into the:
4343 * - TASK_SIZE
4344 * If we don't, we limit the size to the TASK_SIZE.
4345 *
4346 * - remaining sample size
4347 * If we don't, we customize the stack size to
4348 * fit in to the remaining sample size.
4349 */
4350
4351 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4352 stack_size = min(stack_size, (u16) task_size);
4353
4354 /* Current header size plus static size and dynamic size. */
4355 header_size += 2 * sizeof(u64);
4356
4357 /* Do we fit in with the current stack dump size? */
4358 if ((u16) (header_size + stack_size) < header_size) {
4359 /*
4360 * If we overflow the maximum size for the sample,
4361 * we customize the stack dump size to fit in.
4362 */
4363 stack_size = USHRT_MAX - header_size - sizeof(u64);
4364 stack_size = round_up(stack_size, sizeof(u64));
4365 }
4366
4367 return stack_size;
4368}
4369
4370static void
4371perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4372 struct pt_regs *regs)
4373{
4374 /* Case of a kernel thread, nothing to dump */
4375 if (!regs) {
4376 u64 size = 0;
4377 perf_output_put(handle, size);
4378 } else {
4379 unsigned long sp;
4380 unsigned int rem;
4381 u64 dyn_size;
4382
4383 /*
4384 * We dump:
4385 * static size
4386 * - the size requested by user or the best one we can fit
4387 * in to the sample max size
4388 * data
4389 * - user stack dump data
4390 * dynamic size
4391 * - the actual dumped size
4392 */
4393
4394 /* Static size. */
4395 perf_output_put(handle, dump_size);
4396
4397 /* Data. */
4398 sp = perf_user_stack_pointer(regs);
4399 rem = __output_copy_user(handle, (void *) sp, dump_size);
4400 dyn_size = dump_size - rem;
4401
4402 perf_output_skip(handle, rem);
4403
4404 /* Dynamic size. */
4405 perf_output_put(handle, dyn_size);
4406 }
4407}
4408
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004409static void __perf_event_header__init_id(struct perf_event_header *header,
4410 struct perf_sample_data *data,
4411 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004412{
4413 u64 sample_type = event->attr.sample_type;
4414
4415 data->type = sample_type;
4416 header->size += event->id_header_size;
4417
4418 if (sample_type & PERF_SAMPLE_TID) {
4419 /* namespace issues */
4420 data->tid_entry.pid = perf_event_pid(event, current);
4421 data->tid_entry.tid = perf_event_tid(event, current);
4422 }
4423
4424 if (sample_type & PERF_SAMPLE_TIME)
4425 data->time = perf_clock();
4426
Adrian Hunterff3d5272013-08-27 11:23:07 +03004427 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004428 data->id = primary_event_id(event);
4429
4430 if (sample_type & PERF_SAMPLE_STREAM_ID)
4431 data->stream_id = event->id;
4432
4433 if (sample_type & PERF_SAMPLE_CPU) {
4434 data->cpu_entry.cpu = raw_smp_processor_id();
4435 data->cpu_entry.reserved = 0;
4436 }
4437}
4438
Frederic Weisbecker76369132011-05-19 19:55:04 +02004439void perf_event_header__init_id(struct perf_event_header *header,
4440 struct perf_sample_data *data,
4441 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004442{
4443 if (event->attr.sample_id_all)
4444 __perf_event_header__init_id(header, data, event);
4445}
4446
4447static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4448 struct perf_sample_data *data)
4449{
4450 u64 sample_type = data->type;
4451
4452 if (sample_type & PERF_SAMPLE_TID)
4453 perf_output_put(handle, data->tid_entry);
4454
4455 if (sample_type & PERF_SAMPLE_TIME)
4456 perf_output_put(handle, data->time);
4457
4458 if (sample_type & PERF_SAMPLE_ID)
4459 perf_output_put(handle, data->id);
4460
4461 if (sample_type & PERF_SAMPLE_STREAM_ID)
4462 perf_output_put(handle, data->stream_id);
4463
4464 if (sample_type & PERF_SAMPLE_CPU)
4465 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004466
4467 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4468 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004469}
4470
Frederic Weisbecker76369132011-05-19 19:55:04 +02004471void perf_event__output_id_sample(struct perf_event *event,
4472 struct perf_output_handle *handle,
4473 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004474{
4475 if (event->attr.sample_id_all)
4476 __perf_event__output_id_sample(handle, sample);
4477}
4478
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004480 struct perf_event *event,
4481 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482{
4483 u64 read_format = event->attr.read_format;
4484 u64 values[4];
4485 int n = 0;
4486
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004487 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004489 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004490 atomic64_read(&event->child_total_time_enabled);
4491 }
4492 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004493 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494 atomic64_read(&event->child_total_time_running);
4495 }
4496 if (read_format & PERF_FORMAT_ID)
4497 values[n++] = primary_event_id(event);
4498
Frederic Weisbecker76369132011-05-19 19:55:04 +02004499 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004500}
4501
4502/*
4503 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4504 */
4505static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004506 struct perf_event *event,
4507 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508{
4509 struct perf_event *leader = event->group_leader, *sub;
4510 u64 read_format = event->attr.read_format;
4511 u64 values[5];
4512 int n = 0;
4513
4514 values[n++] = 1 + leader->nr_siblings;
4515
4516 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004517 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518
4519 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004520 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004521
4522 if (leader != event)
4523 leader->pmu->read(leader);
4524
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004525 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 if (read_format & PERF_FORMAT_ID)
4527 values[n++] = primary_event_id(leader);
4528
Frederic Weisbecker76369132011-05-19 19:55:04 +02004529 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530
4531 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4532 n = 0;
4533
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004534 if ((sub != event) &&
4535 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536 sub->pmu->read(sub);
4537
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004538 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539 if (read_format & PERF_FORMAT_ID)
4540 values[n++] = primary_event_id(sub);
4541
Frederic Weisbecker76369132011-05-19 19:55:04 +02004542 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004543 }
4544}
4545
Stephane Eranianeed01522010-10-26 16:08:01 +02004546#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4547 PERF_FORMAT_TOTAL_TIME_RUNNING)
4548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549static void perf_output_read(struct perf_output_handle *handle,
4550 struct perf_event *event)
4551{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004552 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004553 u64 read_format = event->attr.read_format;
4554
4555 /*
4556 * compute total_time_enabled, total_time_running
4557 * based on snapshot values taken when the event
4558 * was last scheduled in.
4559 *
4560 * we cannot simply called update_context_time()
4561 * because of locking issue as we are called in
4562 * NMI context
4563 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004564 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004565 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004568 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004570 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571}
4572
4573void perf_output_sample(struct perf_output_handle *handle,
4574 struct perf_event_header *header,
4575 struct perf_sample_data *data,
4576 struct perf_event *event)
4577{
4578 u64 sample_type = data->type;
4579
4580 perf_output_put(handle, *header);
4581
Adrian Hunterff3d5272013-08-27 11:23:07 +03004582 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4583 perf_output_put(handle, data->id);
4584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585 if (sample_type & PERF_SAMPLE_IP)
4586 perf_output_put(handle, data->ip);
4587
4588 if (sample_type & PERF_SAMPLE_TID)
4589 perf_output_put(handle, data->tid_entry);
4590
4591 if (sample_type & PERF_SAMPLE_TIME)
4592 perf_output_put(handle, data->time);
4593
4594 if (sample_type & PERF_SAMPLE_ADDR)
4595 perf_output_put(handle, data->addr);
4596
4597 if (sample_type & PERF_SAMPLE_ID)
4598 perf_output_put(handle, data->id);
4599
4600 if (sample_type & PERF_SAMPLE_STREAM_ID)
4601 perf_output_put(handle, data->stream_id);
4602
4603 if (sample_type & PERF_SAMPLE_CPU)
4604 perf_output_put(handle, data->cpu_entry);
4605
4606 if (sample_type & PERF_SAMPLE_PERIOD)
4607 perf_output_put(handle, data->period);
4608
4609 if (sample_type & PERF_SAMPLE_READ)
4610 perf_output_read(handle, event);
4611
4612 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4613 if (data->callchain) {
4614 int size = 1;
4615
4616 if (data->callchain)
4617 size += data->callchain->nr;
4618
4619 size *= sizeof(u64);
4620
Frederic Weisbecker76369132011-05-19 19:55:04 +02004621 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004622 } else {
4623 u64 nr = 0;
4624 perf_output_put(handle, nr);
4625 }
4626 }
4627
4628 if (sample_type & PERF_SAMPLE_RAW) {
4629 if (data->raw) {
4630 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004631 __output_copy(handle, data->raw->data,
4632 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633 } else {
4634 struct {
4635 u32 size;
4636 u32 data;
4637 } raw = {
4638 .size = sizeof(u32),
4639 .data = 0,
4640 };
4641 perf_output_put(handle, raw);
4642 }
4643 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004644
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004645 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4646 if (data->br_stack) {
4647 size_t size;
4648
4649 size = data->br_stack->nr
4650 * sizeof(struct perf_branch_entry);
4651
4652 perf_output_put(handle, data->br_stack->nr);
4653 perf_output_copy(handle, data->br_stack->entries, size);
4654 } else {
4655 /*
4656 * we always store at least the value of nr
4657 */
4658 u64 nr = 0;
4659 perf_output_put(handle, nr);
4660 }
4661 }
Jiri Olsa40189942012-08-07 15:20:37 +02004662
4663 if (sample_type & PERF_SAMPLE_REGS_USER) {
4664 u64 abi = data->regs_user.abi;
4665
4666 /*
4667 * If there are no regs to dump, notice it through
4668 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4669 */
4670 perf_output_put(handle, abi);
4671
4672 if (abi) {
4673 u64 mask = event->attr.sample_regs_user;
4674 perf_output_sample_regs(handle,
4675 data->regs_user.regs,
4676 mask);
4677 }
4678 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004679
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004680 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004681 perf_output_sample_ustack(handle,
4682 data->stack_user_size,
4683 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004684 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004685
4686 if (sample_type & PERF_SAMPLE_WEIGHT)
4687 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004688
4689 if (sample_type & PERF_SAMPLE_DATA_SRC)
4690 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004691
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004692 if (sample_type & PERF_SAMPLE_TRANSACTION)
4693 perf_output_put(handle, data->txn);
4694
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004695 if (!event->attr.watermark) {
4696 int wakeup_events = event->attr.wakeup_events;
4697
4698 if (wakeup_events) {
4699 struct ring_buffer *rb = handle->rb;
4700 int events = local_inc_return(&rb->events);
4701
4702 if (events >= wakeup_events) {
4703 local_sub(wakeup_events, &rb->events);
4704 local_inc(&rb->wakeup);
4705 }
4706 }
4707 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004708}
4709
4710void perf_prepare_sample(struct perf_event_header *header,
4711 struct perf_sample_data *data,
4712 struct perf_event *event,
4713 struct pt_regs *regs)
4714{
4715 u64 sample_type = event->attr.sample_type;
4716
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004717 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004718 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004719
4720 header->misc = 0;
4721 header->misc |= perf_misc_flags(regs);
4722
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004723 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004724
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004725 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004726 data->ip = perf_instruction_pointer(regs);
4727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004728 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4729 int size = 1;
4730
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004731 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732
4733 if (data->callchain)
4734 size += data->callchain->nr;
4735
4736 header->size += size * sizeof(u64);
4737 }
4738
4739 if (sample_type & PERF_SAMPLE_RAW) {
4740 int size = sizeof(u32);
4741
4742 if (data->raw)
4743 size += data->raw->size;
4744 else
4745 size += sizeof(u32);
4746
4747 WARN_ON_ONCE(size & (sizeof(u64)-1));
4748 header->size += size;
4749 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004750
4751 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4752 int size = sizeof(u64); /* nr */
4753 if (data->br_stack) {
4754 size += data->br_stack->nr
4755 * sizeof(struct perf_branch_entry);
4756 }
4757 header->size += size;
4758 }
Jiri Olsa40189942012-08-07 15:20:37 +02004759
4760 if (sample_type & PERF_SAMPLE_REGS_USER) {
4761 /* regs dump ABI info */
4762 int size = sizeof(u64);
4763
4764 perf_sample_regs_user(&data->regs_user, regs);
4765
4766 if (data->regs_user.regs) {
4767 u64 mask = event->attr.sample_regs_user;
4768 size += hweight64(mask) * sizeof(u64);
4769 }
4770
4771 header->size += size;
4772 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004773
4774 if (sample_type & PERF_SAMPLE_STACK_USER) {
4775 /*
4776 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4777 * processed as the last one or have additional check added
4778 * in case new sample type is added, because we could eat
4779 * up the rest of the sample size.
4780 */
4781 struct perf_regs_user *uregs = &data->regs_user;
4782 u16 stack_size = event->attr.sample_stack_user;
4783 u16 size = sizeof(u64);
4784
4785 if (!uregs->abi)
4786 perf_sample_regs_user(uregs, regs);
4787
4788 stack_size = perf_sample_ustack_size(stack_size, header->size,
4789 uregs->regs);
4790
4791 /*
4792 * If there is something to dump, add space for the dump
4793 * itself and for the field that tells the dynamic size,
4794 * which is how many have been actually dumped.
4795 */
4796 if (stack_size)
4797 size += sizeof(u64) + stack_size;
4798
4799 data->stack_user_size = stack_size;
4800 header->size += size;
4801 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004802}
4803
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004804static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004805 struct perf_sample_data *data,
4806 struct pt_regs *regs)
4807{
4808 struct perf_output_handle handle;
4809 struct perf_event_header header;
4810
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004811 /* protect the callchain buffers */
4812 rcu_read_lock();
4813
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004814 perf_prepare_sample(&header, data, event, regs);
4815
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004816 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004817 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004818
4819 perf_output_sample(&handle, &header, data, event);
4820
4821 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004822
4823exit:
4824 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004825}
4826
4827/*
4828 * read event_id
4829 */
4830
4831struct perf_read_event {
4832 struct perf_event_header header;
4833
4834 u32 pid;
4835 u32 tid;
4836};
4837
4838static void
4839perf_event_read_event(struct perf_event *event,
4840 struct task_struct *task)
4841{
4842 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004843 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004844 struct perf_read_event read_event = {
4845 .header = {
4846 .type = PERF_RECORD_READ,
4847 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004848 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004849 },
4850 .pid = perf_event_pid(event, task),
4851 .tid = perf_event_tid(event, task),
4852 };
4853 int ret;
4854
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004855 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004856 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857 if (ret)
4858 return;
4859
4860 perf_output_put(&handle, read_event);
4861 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004862 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863
4864 perf_output_end(&handle);
4865}
4866
Jiri Olsa52d857a2013-05-06 18:27:18 +02004867typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4868
4869static void
4870perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004871 perf_event_aux_output_cb output,
4872 void *data)
4873{
4874 struct perf_event *event;
4875
4876 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4877 if (event->state < PERF_EVENT_STATE_INACTIVE)
4878 continue;
4879 if (!event_filter_match(event))
4880 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004881 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004882 }
4883}
4884
4885static void
Jiri Olsa67516842013-07-09 18:56:31 +02004886perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004887 struct perf_event_context *task_ctx)
4888{
4889 struct perf_cpu_context *cpuctx;
4890 struct perf_event_context *ctx;
4891 struct pmu *pmu;
4892 int ctxn;
4893
4894 rcu_read_lock();
4895 list_for_each_entry_rcu(pmu, &pmus, entry) {
4896 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4897 if (cpuctx->unique_pmu != pmu)
4898 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004899 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004900 if (task_ctx)
4901 goto next;
4902 ctxn = pmu->task_ctx_nr;
4903 if (ctxn < 0)
4904 goto next;
4905 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4906 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004907 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004908next:
4909 put_cpu_ptr(pmu->pmu_cpu_context);
4910 }
4911
4912 if (task_ctx) {
4913 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004914 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004915 preempt_enable();
4916 }
4917 rcu_read_unlock();
4918}
4919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004920/*
4921 * task tracking -- fork/exit
4922 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004923 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004924 */
4925
4926struct perf_task_event {
4927 struct task_struct *task;
4928 struct perf_event_context *task_ctx;
4929
4930 struct {
4931 struct perf_event_header header;
4932
4933 u32 pid;
4934 u32 ppid;
4935 u32 tid;
4936 u32 ptid;
4937 u64 time;
4938 } event_id;
4939};
4940
Jiri Olsa67516842013-07-09 18:56:31 +02004941static int perf_event_task_match(struct perf_event *event)
4942{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004943 return event->attr.comm || event->attr.mmap ||
4944 event->attr.mmap2 || event->attr.mmap_data ||
4945 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004946}
4947
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004948static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004949 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004951 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004953 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004954 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004955 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004956
Jiri Olsa67516842013-07-09 18:56:31 +02004957 if (!perf_event_task_match(event))
4958 return;
4959
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004960 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004961
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004962 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004963 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004964 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004965 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004966
4967 task_event->event_id.pid = perf_event_pid(event, task);
4968 task_event->event_id.ppid = perf_event_pid(event, current);
4969
4970 task_event->event_id.tid = perf_event_tid(event, task);
4971 task_event->event_id.ptid = perf_event_tid(event, current);
4972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004973 perf_output_put(&handle, task_event->event_id);
4974
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004975 perf_event__output_id_sample(event, &handle, &sample);
4976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004977 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004978out:
4979 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004980}
4981
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982static void perf_event_task(struct task_struct *task,
4983 struct perf_event_context *task_ctx,
4984 int new)
4985{
4986 struct perf_task_event task_event;
4987
4988 if (!atomic_read(&nr_comm_events) &&
4989 !atomic_read(&nr_mmap_events) &&
4990 !atomic_read(&nr_task_events))
4991 return;
4992
4993 task_event = (struct perf_task_event){
4994 .task = task,
4995 .task_ctx = task_ctx,
4996 .event_id = {
4997 .header = {
4998 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4999 .misc = 0,
5000 .size = sizeof(task_event.event_id),
5001 },
5002 /* .pid */
5003 /* .ppid */
5004 /* .tid */
5005 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005006 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007 },
5008 };
5009
Jiri Olsa67516842013-07-09 18:56:31 +02005010 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005011 &task_event,
5012 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005013}
5014
5015void perf_event_fork(struct task_struct *task)
5016{
5017 perf_event_task(task, NULL, 1);
5018}
5019
5020/*
5021 * comm tracking
5022 */
5023
5024struct perf_comm_event {
5025 struct task_struct *task;
5026 char *comm;
5027 int comm_size;
5028
5029 struct {
5030 struct perf_event_header header;
5031
5032 u32 pid;
5033 u32 tid;
5034 } event_id;
5035};
5036
Jiri Olsa67516842013-07-09 18:56:31 +02005037static int perf_event_comm_match(struct perf_event *event)
5038{
5039 return event->attr.comm;
5040}
5041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005043 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005044{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005045 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005046 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005047 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005048 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005049 int ret;
5050
Jiri Olsa67516842013-07-09 18:56:31 +02005051 if (!perf_event_comm_match(event))
5052 return;
5053
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005054 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5055 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005056 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057
5058 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005059 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060
5061 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5062 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5063
5064 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005065 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005066 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005067
5068 perf_event__output_id_sample(event, &handle, &sample);
5069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005070 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005071out:
5072 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005073}
5074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005075static void perf_event_comm_event(struct perf_comm_event *comm_event)
5076{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005077 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079
5080 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005081 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005082 size = ALIGN(strlen(comm)+1, sizeof(u64));
5083
5084 comm_event->comm = comm;
5085 comm_event->comm_size = size;
5086
5087 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005088
Jiri Olsa67516842013-07-09 18:56:31 +02005089 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005090 comm_event,
5091 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005092}
5093
Adrian Hunter82b89772014-05-28 11:45:04 +03005094void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005095{
5096 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005097
5098 if (!atomic_read(&nr_comm_events))
5099 return;
5100
5101 comm_event = (struct perf_comm_event){
5102 .task = task,
5103 /* .comm */
5104 /* .comm_size */
5105 .event_id = {
5106 .header = {
5107 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005108 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109 /* .size */
5110 },
5111 /* .pid */
5112 /* .tid */
5113 },
5114 };
5115
5116 perf_event_comm_event(&comm_event);
5117}
5118
5119/*
5120 * mmap tracking
5121 */
5122
5123struct perf_mmap_event {
5124 struct vm_area_struct *vma;
5125
5126 const char *file_name;
5127 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005128 int maj, min;
5129 u64 ino;
5130 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005131 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005132
5133 struct {
5134 struct perf_event_header header;
5135
5136 u32 pid;
5137 u32 tid;
5138 u64 start;
5139 u64 len;
5140 u64 pgoff;
5141 } event_id;
5142};
5143
Jiri Olsa67516842013-07-09 18:56:31 +02005144static int perf_event_mmap_match(struct perf_event *event,
5145 void *data)
5146{
5147 struct perf_mmap_event *mmap_event = data;
5148 struct vm_area_struct *vma = mmap_event->vma;
5149 int executable = vma->vm_flags & VM_EXEC;
5150
5151 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005152 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005153}
5154
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005155static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005156 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005158 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005160 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005161 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005162 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005163
Jiri Olsa67516842013-07-09 18:56:31 +02005164 if (!perf_event_mmap_match(event, data))
5165 return;
5166
Stephane Eranian13d7a242013-08-21 12:10:24 +02005167 if (event->attr.mmap2) {
5168 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5169 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5170 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5171 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005172 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005173 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5174 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005175 }
5176
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005177 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5178 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005179 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005181 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182
5183 mmap_event->event_id.pid = perf_event_pid(event, current);
5184 mmap_event->event_id.tid = perf_event_tid(event, current);
5185
5186 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005187
5188 if (event->attr.mmap2) {
5189 perf_output_put(&handle, mmap_event->maj);
5190 perf_output_put(&handle, mmap_event->min);
5191 perf_output_put(&handle, mmap_event->ino);
5192 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005193 perf_output_put(&handle, mmap_event->prot);
5194 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005195 }
5196
Frederic Weisbecker76369132011-05-19 19:55:04 +02005197 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005199
5200 perf_event__output_id_sample(event, &handle, &sample);
5201
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005202 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005203out:
5204 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205}
5206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005207static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5208{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209 struct vm_area_struct *vma = mmap_event->vma;
5210 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005211 int maj = 0, min = 0;
5212 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005213 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214 unsigned int size;
5215 char tmp[16];
5216 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005217 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005218
5219 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005220 struct inode *inode;
5221 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005222
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005223 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005224 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005225 name = "//enomem";
5226 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005227 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005229 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230 * need to add enough zero bytes after the string to handle
5231 * the 64bit alignment we do later.
5232 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005233 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005234 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005235 name = "//toolong";
5236 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005238 inode = file_inode(vma->vm_file);
5239 dev = inode->i_sb->s_dev;
5240 ino = inode->i_ino;
5241 gen = inode->i_generation;
5242 maj = MAJOR(dev);
5243 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005244
5245 if (vma->vm_flags & VM_READ)
5246 prot |= PROT_READ;
5247 if (vma->vm_flags & VM_WRITE)
5248 prot |= PROT_WRITE;
5249 if (vma->vm_flags & VM_EXEC)
5250 prot |= PROT_EXEC;
5251
5252 if (vma->vm_flags & VM_MAYSHARE)
5253 flags = MAP_SHARED;
5254 else
5255 flags = MAP_PRIVATE;
5256
5257 if (vma->vm_flags & VM_DENYWRITE)
5258 flags |= MAP_DENYWRITE;
5259 if (vma->vm_flags & VM_MAYEXEC)
5260 flags |= MAP_EXECUTABLE;
5261 if (vma->vm_flags & VM_LOCKED)
5262 flags |= MAP_LOCKED;
5263 if (vma->vm_flags & VM_HUGETLB)
5264 flags |= MAP_HUGETLB;
5265
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267 } else {
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005268 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005269 if (name)
5270 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005272 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005274 name = "[heap]";
5275 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005276 }
5277 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005279 name = "[stack]";
5280 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281 }
5282
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005283 name = "//anon";
5284 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285 }
5286
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005287cpy_name:
5288 strlcpy(tmp, name, sizeof(tmp));
5289 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005290got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005291 /*
5292 * Since our buffer works in 8 byte units we need to align our string
5293 * size to a multiple of 8. However, we must guarantee the tail end is
5294 * zero'd out to avoid leaking random bits to userspace.
5295 */
5296 size = strlen(name)+1;
5297 while (!IS_ALIGNED(size, sizeof(u64)))
5298 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299
5300 mmap_event->file_name = name;
5301 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005302 mmap_event->maj = maj;
5303 mmap_event->min = min;
5304 mmap_event->ino = ino;
5305 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005306 mmap_event->prot = prot;
5307 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005308
Stephane Eranian2fe85422013-01-24 16:10:39 +01005309 if (!(vma->vm_flags & VM_EXEC))
5310 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005312 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5313
Jiri Olsa67516842013-07-09 18:56:31 +02005314 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005315 mmap_event,
5316 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005317
5318 kfree(buf);
5319}
5320
Eric B Munson3af9e852010-05-18 15:30:49 +01005321void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005322{
5323 struct perf_mmap_event mmap_event;
5324
5325 if (!atomic_read(&nr_mmap_events))
5326 return;
5327
5328 mmap_event = (struct perf_mmap_event){
5329 .vma = vma,
5330 /* .file_name */
5331 /* .file_size */
5332 .event_id = {
5333 .header = {
5334 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005335 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 /* .size */
5337 },
5338 /* .pid */
5339 /* .tid */
5340 .start = vma->vm_start,
5341 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005342 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005344 /* .maj (attr_mmap2 only) */
5345 /* .min (attr_mmap2 only) */
5346 /* .ino (attr_mmap2 only) */
5347 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005348 /* .prot (attr_mmap2 only) */
5349 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005350 };
5351
5352 perf_event_mmap_event(&mmap_event);
5353}
5354
5355/*
5356 * IRQ throttle logging
5357 */
5358
5359static void perf_log_throttle(struct perf_event *event, int enable)
5360{
5361 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005362 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363 int ret;
5364
5365 struct {
5366 struct perf_event_header header;
5367 u64 time;
5368 u64 id;
5369 u64 stream_id;
5370 } throttle_event = {
5371 .header = {
5372 .type = PERF_RECORD_THROTTLE,
5373 .misc = 0,
5374 .size = sizeof(throttle_event),
5375 },
5376 .time = perf_clock(),
5377 .id = primary_event_id(event),
5378 .stream_id = event->id,
5379 };
5380
5381 if (enable)
5382 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5383
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005384 perf_event_header__init_id(&throttle_event.header, &sample, event);
5385
5386 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005387 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 if (ret)
5389 return;
5390
5391 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005392 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 perf_output_end(&handle);
5394}
5395
5396/*
5397 * Generic event overflow handling, sampling.
5398 */
5399
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005400static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005401 int throttle, struct perf_sample_data *data,
5402 struct pt_regs *regs)
5403{
5404 int events = atomic_read(&event->event_limit);
5405 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005406 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005407 int ret = 0;
5408
Peter Zijlstra96398822010-11-24 18:55:29 +01005409 /*
5410 * Non-sampling counters might still use the PMI to fold short
5411 * hardware counters, ignore those.
5412 */
5413 if (unlikely(!is_sampling_event(event)))
5414 return 0;
5415
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005416 seq = __this_cpu_read(perf_throttled_seq);
5417 if (seq != hwc->interrupts_seq) {
5418 hwc->interrupts_seq = seq;
5419 hwc->interrupts = 1;
5420 } else {
5421 hwc->interrupts++;
5422 if (unlikely(throttle
5423 && hwc->interrupts >= max_samples_per_tick)) {
5424 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005425 hwc->interrupts = MAX_INTERRUPTS;
5426 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005427 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428 ret = 1;
5429 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005430 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431
5432 if (event->attr.freq) {
5433 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005434 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435
Peter Zijlstraabd50712010-01-26 18:50:16 +01005436 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437
Peter Zijlstraabd50712010-01-26 18:50:16 +01005438 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005439 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005440 }
5441
5442 /*
5443 * XXX event_limit might not quite work as expected on inherited
5444 * events
5445 */
5446
5447 event->pending_kill = POLL_IN;
5448 if (events && atomic_dec_and_test(&event->event_limit)) {
5449 ret = 1;
5450 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005451 event->pending_disable = 1;
5452 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005453 }
5454
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005455 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005456 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005457 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005458 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005459
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005460 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005461 event->pending_wakeup = 1;
5462 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005463 }
5464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005465 return ret;
5466}
5467
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005468int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469 struct perf_sample_data *data,
5470 struct pt_regs *regs)
5471{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005472 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005473}
5474
5475/*
5476 * Generic software event infrastructure
5477 */
5478
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005479struct swevent_htable {
5480 struct swevent_hlist *swevent_hlist;
5481 struct mutex hlist_mutex;
5482 int hlist_refcount;
5483
5484 /* Recursion avoidance in each contexts */
5485 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005486
5487 /* Keeps track of cpu being initialized/exited */
5488 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005489};
5490
5491static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005493/*
5494 * We directly increment event->count and keep a second value in
5495 * event->hw.period_left to count intervals. This period event
5496 * is kept in the range [-sample_period, 0] so that we can use the
5497 * sign as trigger.
5498 */
5499
Jiri Olsaab573842013-05-01 17:25:44 +02005500u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005501{
5502 struct hw_perf_event *hwc = &event->hw;
5503 u64 period = hwc->last_period;
5504 u64 nr, offset;
5505 s64 old, val;
5506
5507 hwc->last_period = hwc->sample_period;
5508
5509again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005510 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005511 if (val < 0)
5512 return 0;
5513
5514 nr = div64_u64(period + val, period);
5515 offset = nr * period;
5516 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005517 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005518 goto again;
5519
5520 return nr;
5521}
5522
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005523static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005524 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005525 struct pt_regs *regs)
5526{
5527 struct hw_perf_event *hwc = &event->hw;
5528 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005529
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005530 if (!overflow)
5531 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532
5533 if (hwc->interrupts == MAX_INTERRUPTS)
5534 return;
5535
5536 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005537 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005538 data, regs)) {
5539 /*
5540 * We inhibit the overflow from happening when
5541 * hwc->interrupts == MAX_INTERRUPTS.
5542 */
5543 break;
5544 }
5545 throttle = 1;
5546 }
5547}
5548
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005549static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005550 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005551 struct pt_regs *regs)
5552{
5553 struct hw_perf_event *hwc = &event->hw;
5554
Peter Zijlstrae7850592010-05-21 14:43:08 +02005555 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557 if (!regs)
5558 return;
5559
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005560 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005561 return;
5562
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005563 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5564 data->period = nr;
5565 return perf_swevent_overflow(event, 1, data, regs);
5566 } else
5567 data->period = event->hw.last_period;
5568
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005569 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005570 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005571
Peter Zijlstrae7850592010-05-21 14:43:08 +02005572 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005573 return;
5574
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005575 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576}
5577
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005578static int perf_exclude_event(struct perf_event *event,
5579 struct pt_regs *regs)
5580{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005581 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005582 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005583
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005584 if (regs) {
5585 if (event->attr.exclude_user && user_mode(regs))
5586 return 1;
5587
5588 if (event->attr.exclude_kernel && !user_mode(regs))
5589 return 1;
5590 }
5591
5592 return 0;
5593}
5594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005595static int perf_swevent_match(struct perf_event *event,
5596 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005597 u32 event_id,
5598 struct perf_sample_data *data,
5599 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005600{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 if (event->attr.type != type)
5602 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005604 if (event->attr.config != event_id)
5605 return 0;
5606
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005607 if (perf_exclude_event(event, regs))
5608 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609
5610 return 1;
5611}
5612
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005613static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005614{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005615 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005617 return hash_64(val, SWEVENT_HLIST_BITS);
5618}
5619
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005620static inline struct hlist_head *
5621__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005622{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005623 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005624
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005625 return &hlist->heads[hash];
5626}
5627
5628/* For the read side: events when they trigger */
5629static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005630find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005631{
5632 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005633
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005634 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005635 if (!hlist)
5636 return NULL;
5637
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005638 return __find_swevent_head(hlist, type, event_id);
5639}
5640
5641/* For the event head insertion and removal in the hlist */
5642static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005643find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005644{
5645 struct swevent_hlist *hlist;
5646 u32 event_id = event->attr.config;
5647 u64 type = event->attr.type;
5648
5649 /*
5650 * Event scheduling is always serialized against hlist allocation
5651 * and release. Which makes the protected version suitable here.
5652 * The context lock guarantees that.
5653 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005654 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005655 lockdep_is_held(&event->ctx->lock));
5656 if (!hlist)
5657 return NULL;
5658
5659 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005660}
5661
5662static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005663 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005664 struct perf_sample_data *data,
5665 struct pt_regs *regs)
5666{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005667 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005668 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005669 struct hlist_head *head;
5670
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005671 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005672 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005673 if (!head)
5674 goto end;
5675
Sasha Levinb67bfe02013-02-27 17:06:00 -08005676 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005677 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005678 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005679 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005680end:
5681 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682}
5683
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005684int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005685{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005686 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005687
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005688 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005690EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005692inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005694 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005695
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005696 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005697}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005698
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005699void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005701 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005702 int rctx;
5703
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005704 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005705 rctx = perf_swevent_get_recursion_context();
5706 if (rctx < 0)
5707 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005708
Robert Richterfd0d0002012-04-02 20:19:08 +02005709 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005710
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005711 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005712
5713 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005714 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005715}
5716
5717static void perf_swevent_read(struct perf_event *event)
5718{
5719}
5720
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005721static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005723 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005724 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005725 struct hlist_head *head;
5726
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005727 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005728 hwc->last_period = hwc->sample_period;
5729 perf_swevent_set_period(event);
5730 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005731
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005732 hwc->state = !(flags & PERF_EF_START);
5733
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005734 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02005735 if (!head) {
5736 /*
5737 * We can race with cpu hotplug code. Do not
5738 * WARN if the cpu just got unplugged.
5739 */
5740 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005741 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02005742 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005743
5744 hlist_add_head_rcu(&event->hlist_entry, head);
5745
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005746 return 0;
5747}
5748
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005749static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005750{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005751 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005752}
5753
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005754static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005755{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005756 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005757}
5758
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005759static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005760{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005761 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005762}
5763
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005764/* Deref the hlist from the update side */
5765static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005766swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005767{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005768 return rcu_dereference_protected(swhash->swevent_hlist,
5769 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005770}
5771
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005772static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005773{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005774 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005775
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005776 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005777 return;
5778
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005779 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005780 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005781}
5782
5783static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5784{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005785 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005786
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005787 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005788
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005789 if (!--swhash->hlist_refcount)
5790 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005791
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005792 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005793}
5794
5795static void swevent_hlist_put(struct perf_event *event)
5796{
5797 int cpu;
5798
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005799 for_each_possible_cpu(cpu)
5800 swevent_hlist_put_cpu(event, cpu);
5801}
5802
5803static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5804{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005805 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005806 int err = 0;
5807
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005808 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005809
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005810 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005811 struct swevent_hlist *hlist;
5812
5813 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5814 if (!hlist) {
5815 err = -ENOMEM;
5816 goto exit;
5817 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005818 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005819 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005820 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005821exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005822 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005823
5824 return err;
5825}
5826
5827static int swevent_hlist_get(struct perf_event *event)
5828{
5829 int err;
5830 int cpu, failed_cpu;
5831
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005832 get_online_cpus();
5833 for_each_possible_cpu(cpu) {
5834 err = swevent_hlist_get_cpu(event, cpu);
5835 if (err) {
5836 failed_cpu = cpu;
5837 goto fail;
5838 }
5839 }
5840 put_online_cpus();
5841
5842 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005843fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005844 for_each_possible_cpu(cpu) {
5845 if (cpu == failed_cpu)
5846 break;
5847 swevent_hlist_put_cpu(event, cpu);
5848 }
5849
5850 put_online_cpus();
5851 return err;
5852}
5853
Ingo Molnarc5905af2012-02-24 08:31:31 +01005854struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005855
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005856static void sw_perf_event_destroy(struct perf_event *event)
5857{
5858 u64 event_id = event->attr.config;
5859
5860 WARN_ON(event->parent);
5861
Ingo Molnarc5905af2012-02-24 08:31:31 +01005862 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005863 swevent_hlist_put(event);
5864}
5865
5866static int perf_swevent_init(struct perf_event *event)
5867{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005868 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005869
5870 if (event->attr.type != PERF_TYPE_SOFTWARE)
5871 return -ENOENT;
5872
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005873 /*
5874 * no branch sampling for software events
5875 */
5876 if (has_branch_stack(event))
5877 return -EOPNOTSUPP;
5878
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005879 switch (event_id) {
5880 case PERF_COUNT_SW_CPU_CLOCK:
5881 case PERF_COUNT_SW_TASK_CLOCK:
5882 return -ENOENT;
5883
5884 default:
5885 break;
5886 }
5887
Dan Carpenterce677832010-10-24 21:50:42 +02005888 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005889 return -ENOENT;
5890
5891 if (!event->parent) {
5892 int err;
5893
5894 err = swevent_hlist_get(event);
5895 if (err)
5896 return err;
5897
Ingo Molnarc5905af2012-02-24 08:31:31 +01005898 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005899 event->destroy = sw_perf_event_destroy;
5900 }
5901
5902 return 0;
5903}
5904
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005905static int perf_swevent_event_idx(struct perf_event *event)
5906{
5907 return 0;
5908}
5909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005910static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005911 .task_ctx_nr = perf_sw_context,
5912
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005913 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005914 .add = perf_swevent_add,
5915 .del = perf_swevent_del,
5916 .start = perf_swevent_start,
5917 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005918 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005919
5920 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005921};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005922
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005923#ifdef CONFIG_EVENT_TRACING
5924
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005925static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005926 struct perf_sample_data *data)
5927{
5928 void *record = data->raw->data;
5929
5930 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5931 return 1;
5932 return 0;
5933}
5934
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005935static int perf_tp_event_match(struct perf_event *event,
5936 struct perf_sample_data *data,
5937 struct pt_regs *regs)
5938{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005939 if (event->hw.state & PERF_HES_STOPPED)
5940 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005941 /*
5942 * All tracepoints are from kernel-space.
5943 */
5944 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005945 return 0;
5946
5947 if (!perf_tp_filter_match(event, data))
5948 return 0;
5949
5950 return 1;
5951}
5952
5953void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005954 struct pt_regs *regs, struct hlist_head *head, int rctx,
5955 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005956{
5957 struct perf_sample_data data;
5958 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005959
5960 struct perf_raw_record raw = {
5961 .size = entry_size,
5962 .data = record,
5963 };
5964
Robert Richterfd0d0002012-04-02 20:19:08 +02005965 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005966 data.raw = &raw;
5967
Sasha Levinb67bfe02013-02-27 17:06:00 -08005968 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005969 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005970 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005971 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005972
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005973 /*
5974 * If we got specified a target task, also iterate its context and
5975 * deliver this event there too.
5976 */
5977 if (task && task != current) {
5978 struct perf_event_context *ctx;
5979 struct trace_entry *entry = record;
5980
5981 rcu_read_lock();
5982 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5983 if (!ctx)
5984 goto unlock;
5985
5986 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5987 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5988 continue;
5989 if (event->attr.config != entry->type)
5990 continue;
5991 if (perf_tp_event_match(event, &data, regs))
5992 perf_swevent_event(event, count, &data, regs);
5993 }
5994unlock:
5995 rcu_read_unlock();
5996 }
5997
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005998 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005999}
6000EXPORT_SYMBOL_GPL(perf_tp_event);
6001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006002static void tp_perf_event_destroy(struct perf_event *event)
6003{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006004 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006005}
6006
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006007static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006008{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006009 int err;
6010
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006011 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6012 return -ENOENT;
6013
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006014 /*
6015 * no branch sampling for tracepoint events
6016 */
6017 if (has_branch_stack(event))
6018 return -EOPNOTSUPP;
6019
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006020 err = perf_trace_init(event);
6021 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006022 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006023
6024 event->destroy = tp_perf_event_destroy;
6025
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006026 return 0;
6027}
6028
6029static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006030 .task_ctx_nr = perf_sw_context,
6031
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006032 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006033 .add = perf_trace_add,
6034 .del = perf_trace_del,
6035 .start = perf_swevent_start,
6036 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006037 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006038
6039 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006040};
6041
6042static inline void perf_tp_register(void)
6043{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006044 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006045}
Li Zefan6fb29152009-10-15 11:21:42 +08006046
6047static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6048{
6049 char *filter_str;
6050 int ret;
6051
6052 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6053 return -EINVAL;
6054
6055 filter_str = strndup_user(arg, PAGE_SIZE);
6056 if (IS_ERR(filter_str))
6057 return PTR_ERR(filter_str);
6058
6059 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6060
6061 kfree(filter_str);
6062 return ret;
6063}
6064
6065static void perf_event_free_filter(struct perf_event *event)
6066{
6067 ftrace_profile_free_filter(event);
6068}
6069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006070#else
Li Zefan6fb29152009-10-15 11:21:42 +08006071
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006072static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074}
Li Zefan6fb29152009-10-15 11:21:42 +08006075
6076static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6077{
6078 return -ENOENT;
6079}
6080
6081static void perf_event_free_filter(struct perf_event *event)
6082{
6083}
6084
Li Zefan07b139c2009-12-21 14:27:35 +08006085#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006086
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006087#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006088void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006089{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006090 struct perf_sample_data sample;
6091 struct pt_regs *regs = data;
6092
Robert Richterfd0d0002012-04-02 20:19:08 +02006093 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006094
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006095 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006096 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006097}
6098#endif
6099
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006100/*
6101 * hrtimer based swevent callback
6102 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006104static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006105{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006106 enum hrtimer_restart ret = HRTIMER_RESTART;
6107 struct perf_sample_data data;
6108 struct pt_regs *regs;
6109 struct perf_event *event;
6110 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006111
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006112 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006113
6114 if (event->state != PERF_EVENT_STATE_ACTIVE)
6115 return HRTIMER_NORESTART;
6116
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006117 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118
Robert Richterfd0d0002012-04-02 20:19:08 +02006119 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006120 regs = get_irq_regs();
6121
6122 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006123 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006124 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006125 ret = HRTIMER_NORESTART;
6126 }
6127
6128 period = max_t(u64, 10000, event->hw.sample_period);
6129 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6130
6131 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006132}
6133
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006134static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006135{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006136 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006137 s64 period;
6138
6139 if (!is_sampling_event(event))
6140 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006141
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006142 period = local64_read(&hwc->period_left);
6143 if (period) {
6144 if (period < 0)
6145 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006146
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006147 local64_set(&hwc->period_left, 0);
6148 } else {
6149 period = max_t(u64, 10000, hwc->sample_period);
6150 }
6151 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006152 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006153 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006154}
6155
6156static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6157{
6158 struct hw_perf_event *hwc = &event->hw;
6159
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006160 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006161 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006162 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006163
6164 hrtimer_cancel(&hwc->hrtimer);
6165 }
6166}
6167
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006168static void perf_swevent_init_hrtimer(struct perf_event *event)
6169{
6170 struct hw_perf_event *hwc = &event->hw;
6171
6172 if (!is_sampling_event(event))
6173 return;
6174
6175 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6176 hwc->hrtimer.function = perf_swevent_hrtimer;
6177
6178 /*
6179 * Since hrtimers have a fixed rate, we can do a static freq->period
6180 * mapping and avoid the whole period adjust feedback stuff.
6181 */
6182 if (event->attr.freq) {
6183 long freq = event->attr.sample_freq;
6184
6185 event->attr.sample_period = NSEC_PER_SEC / freq;
6186 hwc->sample_period = event->attr.sample_period;
6187 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006188 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006189 event->attr.freq = 0;
6190 }
6191}
6192
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006193/*
6194 * Software event: cpu wall time clock
6195 */
6196
6197static void cpu_clock_event_update(struct perf_event *event)
6198{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006199 s64 prev;
6200 u64 now;
6201
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006202 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006203 prev = local64_xchg(&event->hw.prev_count, now);
6204 local64_add(now - prev, &event->count);
6205}
6206
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006207static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006208{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006209 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006210 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006211}
6212
6213static void cpu_clock_event_stop(struct perf_event *event, int flags)
6214{
6215 perf_swevent_cancel_hrtimer(event);
6216 cpu_clock_event_update(event);
6217}
6218
6219static int cpu_clock_event_add(struct perf_event *event, int flags)
6220{
6221 if (flags & PERF_EF_START)
6222 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006223
6224 return 0;
6225}
6226
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006227static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006228{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006229 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006230}
6231
6232static void cpu_clock_event_read(struct perf_event *event)
6233{
6234 cpu_clock_event_update(event);
6235}
6236
6237static int cpu_clock_event_init(struct perf_event *event)
6238{
6239 if (event->attr.type != PERF_TYPE_SOFTWARE)
6240 return -ENOENT;
6241
6242 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6243 return -ENOENT;
6244
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006245 /*
6246 * no branch sampling for software events
6247 */
6248 if (has_branch_stack(event))
6249 return -EOPNOTSUPP;
6250
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006251 perf_swevent_init_hrtimer(event);
6252
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006253 return 0;
6254}
6255
6256static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006257 .task_ctx_nr = perf_sw_context,
6258
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006259 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006260 .add = cpu_clock_event_add,
6261 .del = cpu_clock_event_del,
6262 .start = cpu_clock_event_start,
6263 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006264 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006265
6266 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006267};
6268
6269/*
6270 * Software event: task time clock
6271 */
6272
6273static void task_clock_event_update(struct perf_event *event, u64 now)
6274{
6275 u64 prev;
6276 s64 delta;
6277
6278 prev = local64_xchg(&event->hw.prev_count, now);
6279 delta = now - prev;
6280 local64_add(delta, &event->count);
6281}
6282
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006283static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006284{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006285 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006286 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006287}
6288
6289static void task_clock_event_stop(struct perf_event *event, int flags)
6290{
6291 perf_swevent_cancel_hrtimer(event);
6292 task_clock_event_update(event, event->ctx->time);
6293}
6294
6295static int task_clock_event_add(struct perf_event *event, int flags)
6296{
6297 if (flags & PERF_EF_START)
6298 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006299
6300 return 0;
6301}
6302
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006303static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006304{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006305 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006306}
6307
6308static void task_clock_event_read(struct perf_event *event)
6309{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006310 u64 now = perf_clock();
6311 u64 delta = now - event->ctx->timestamp;
6312 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006313
6314 task_clock_event_update(event, time);
6315}
6316
6317static int task_clock_event_init(struct perf_event *event)
6318{
6319 if (event->attr.type != PERF_TYPE_SOFTWARE)
6320 return -ENOENT;
6321
6322 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6323 return -ENOENT;
6324
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006325 /*
6326 * no branch sampling for software events
6327 */
6328 if (has_branch_stack(event))
6329 return -EOPNOTSUPP;
6330
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006331 perf_swevent_init_hrtimer(event);
6332
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006333 return 0;
6334}
6335
6336static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006337 .task_ctx_nr = perf_sw_context,
6338
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006339 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006340 .add = task_clock_event_add,
6341 .del = task_clock_event_del,
6342 .start = task_clock_event_start,
6343 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006344 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006345
6346 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006347};
6348
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006349static void perf_pmu_nop_void(struct pmu *pmu)
6350{
6351}
6352
6353static int perf_pmu_nop_int(struct pmu *pmu)
6354{
6355 return 0;
6356}
6357
6358static void perf_pmu_start_txn(struct pmu *pmu)
6359{
6360 perf_pmu_disable(pmu);
6361}
6362
6363static int perf_pmu_commit_txn(struct pmu *pmu)
6364{
6365 perf_pmu_enable(pmu);
6366 return 0;
6367}
6368
6369static void perf_pmu_cancel_txn(struct pmu *pmu)
6370{
6371 perf_pmu_enable(pmu);
6372}
6373
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006374static int perf_event_idx_default(struct perf_event *event)
6375{
6376 return event->hw.idx + 1;
6377}
6378
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006379/*
6380 * Ensures all contexts with the same task_ctx_nr have the same
6381 * pmu_cpu_context too.
6382 */
Mark Rutland9e317042014-02-10 17:44:18 +00006383static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006384{
6385 struct pmu *pmu;
6386
6387 if (ctxn < 0)
6388 return NULL;
6389
6390 list_for_each_entry(pmu, &pmus, entry) {
6391 if (pmu->task_ctx_nr == ctxn)
6392 return pmu->pmu_cpu_context;
6393 }
6394
6395 return NULL;
6396}
6397
Peter Zijlstra51676952010-12-07 14:18:20 +01006398static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006399{
Peter Zijlstra51676952010-12-07 14:18:20 +01006400 int cpu;
6401
6402 for_each_possible_cpu(cpu) {
6403 struct perf_cpu_context *cpuctx;
6404
6405 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6406
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006407 if (cpuctx->unique_pmu == old_pmu)
6408 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006409 }
6410}
6411
6412static void free_pmu_context(struct pmu *pmu)
6413{
6414 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006415
6416 mutex_lock(&pmus_lock);
6417 /*
6418 * Like a real lame refcount.
6419 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006420 list_for_each_entry(i, &pmus, entry) {
6421 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6422 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006423 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006424 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006425 }
6426
Peter Zijlstra51676952010-12-07 14:18:20 +01006427 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006428out:
6429 mutex_unlock(&pmus_lock);
6430}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006431static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006432
Peter Zijlstraabe43402010-11-17 23:17:37 +01006433static ssize_t
6434type_show(struct device *dev, struct device_attribute *attr, char *page)
6435{
6436 struct pmu *pmu = dev_get_drvdata(dev);
6437
6438 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6439}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006440static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006441
Stephane Eranian62b85632013-04-03 14:21:34 +02006442static ssize_t
6443perf_event_mux_interval_ms_show(struct device *dev,
6444 struct device_attribute *attr,
6445 char *page)
6446{
6447 struct pmu *pmu = dev_get_drvdata(dev);
6448
6449 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6450}
6451
6452static ssize_t
6453perf_event_mux_interval_ms_store(struct device *dev,
6454 struct device_attribute *attr,
6455 const char *buf, size_t count)
6456{
6457 struct pmu *pmu = dev_get_drvdata(dev);
6458 int timer, cpu, ret;
6459
6460 ret = kstrtoint(buf, 0, &timer);
6461 if (ret)
6462 return ret;
6463
6464 if (timer < 1)
6465 return -EINVAL;
6466
6467 /* same value, noting to do */
6468 if (timer == pmu->hrtimer_interval_ms)
6469 return count;
6470
6471 pmu->hrtimer_interval_ms = timer;
6472
6473 /* update all cpuctx for this PMU */
6474 for_each_possible_cpu(cpu) {
6475 struct perf_cpu_context *cpuctx;
6476 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6477 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6478
6479 if (hrtimer_active(&cpuctx->hrtimer))
6480 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6481 }
6482
6483 return count;
6484}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006485static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006486
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006487static struct attribute *pmu_dev_attrs[] = {
6488 &dev_attr_type.attr,
6489 &dev_attr_perf_event_mux_interval_ms.attr,
6490 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006491};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006492ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006493
6494static int pmu_bus_running;
6495static struct bus_type pmu_bus = {
6496 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006497 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006498};
6499
6500static void pmu_dev_release(struct device *dev)
6501{
6502 kfree(dev);
6503}
6504
6505static int pmu_dev_alloc(struct pmu *pmu)
6506{
6507 int ret = -ENOMEM;
6508
6509 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6510 if (!pmu->dev)
6511 goto out;
6512
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006513 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006514 device_initialize(pmu->dev);
6515 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6516 if (ret)
6517 goto free_dev;
6518
6519 dev_set_drvdata(pmu->dev, pmu);
6520 pmu->dev->bus = &pmu_bus;
6521 pmu->dev->release = pmu_dev_release;
6522 ret = device_add(pmu->dev);
6523 if (ret)
6524 goto free_dev;
6525
6526out:
6527 return ret;
6528
6529free_dev:
6530 put_device(pmu->dev);
6531 goto out;
6532}
6533
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006534static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006535static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006536
Mischa Jonker03d8e802013-06-04 11:45:48 +02006537int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006538{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006539 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006540
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006541 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006542 ret = -ENOMEM;
6543 pmu->pmu_disable_count = alloc_percpu(int);
6544 if (!pmu->pmu_disable_count)
6545 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006546
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006547 pmu->type = -1;
6548 if (!name)
6549 goto skip_type;
6550 pmu->name = name;
6551
6552 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006553 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6554 if (type < 0) {
6555 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006556 goto free_pdc;
6557 }
6558 }
6559 pmu->type = type;
6560
Peter Zijlstraabe43402010-11-17 23:17:37 +01006561 if (pmu_bus_running) {
6562 ret = pmu_dev_alloc(pmu);
6563 if (ret)
6564 goto free_idr;
6565 }
6566
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006567skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006568 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6569 if (pmu->pmu_cpu_context)
6570 goto got_cpu_context;
6571
Wei Yongjunc4814202013-04-12 11:05:54 +08006572 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006573 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6574 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006575 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006576
6577 for_each_possible_cpu(cpu) {
6578 struct perf_cpu_context *cpuctx;
6579
6580 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006581 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006582 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006583 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006584 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006585 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006586
6587 __perf_cpu_hrtimer_init(cpuctx, cpu);
6588
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006589 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006590 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006591 }
6592
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006593got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006594 if (!pmu->start_txn) {
6595 if (pmu->pmu_enable) {
6596 /*
6597 * If we have pmu_enable/pmu_disable calls, install
6598 * transaction stubs that use that to try and batch
6599 * hardware accesses.
6600 */
6601 pmu->start_txn = perf_pmu_start_txn;
6602 pmu->commit_txn = perf_pmu_commit_txn;
6603 pmu->cancel_txn = perf_pmu_cancel_txn;
6604 } else {
6605 pmu->start_txn = perf_pmu_nop_void;
6606 pmu->commit_txn = perf_pmu_nop_int;
6607 pmu->cancel_txn = perf_pmu_nop_void;
6608 }
6609 }
6610
6611 if (!pmu->pmu_enable) {
6612 pmu->pmu_enable = perf_pmu_nop_void;
6613 pmu->pmu_disable = perf_pmu_nop_void;
6614 }
6615
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006616 if (!pmu->event_idx)
6617 pmu->event_idx = perf_event_idx_default;
6618
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006619 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006620 ret = 0;
6621unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006622 mutex_unlock(&pmus_lock);
6623
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006624 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006625
Peter Zijlstraabe43402010-11-17 23:17:37 +01006626free_dev:
6627 device_del(pmu->dev);
6628 put_device(pmu->dev);
6629
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006630free_idr:
6631 if (pmu->type >= PERF_TYPE_MAX)
6632 idr_remove(&pmu_idr, pmu->type);
6633
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006634free_pdc:
6635 free_percpu(pmu->pmu_disable_count);
6636 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006637}
Yan, Zhengc464c762014-03-18 16:56:41 +08006638EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006639
6640void perf_pmu_unregister(struct pmu *pmu)
6641{
6642 mutex_lock(&pmus_lock);
6643 list_del_rcu(&pmu->entry);
6644 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645
6646 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006647 * We dereference the pmu list under both SRCU and regular RCU, so
6648 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006650 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006651 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006652
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006653 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006654 if (pmu->type >= PERF_TYPE_MAX)
6655 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006656 device_del(pmu->dev);
6657 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006658 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006659}
Yan, Zhengc464c762014-03-18 16:56:41 +08006660EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006662struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006663{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006664 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006665 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006666 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006667
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006668 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006669
6670 rcu_read_lock();
6671 pmu = idr_find(&pmu_idr, event->attr.type);
6672 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006673 if (pmu) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006674 if (!try_module_get(pmu->module)) {
6675 pmu = ERR_PTR(-ENODEV);
6676 goto unlock;
6677 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006678 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006679 ret = pmu->event_init(event);
6680 if (ret)
6681 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006682 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006683 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006684
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006685 list_for_each_entry_rcu(pmu, &pmus, entry) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006686 if (!try_module_get(pmu->module)) {
6687 pmu = ERR_PTR(-ENODEV);
6688 goto unlock;
6689 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006690 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006691 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006692 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006693 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006694
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006695 if (ret != -ENOENT) {
6696 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006697 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006700 pmu = ERR_PTR(-ENOENT);
6701unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006702 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006703
6704 return pmu;
6705}
6706
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006707static void account_event_cpu(struct perf_event *event, int cpu)
6708{
6709 if (event->parent)
6710 return;
6711
6712 if (has_branch_stack(event)) {
6713 if (!(event->attach_state & PERF_ATTACH_TASK))
6714 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6715 }
6716 if (is_cgroup_event(event))
6717 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6718}
6719
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006720static void account_event(struct perf_event *event)
6721{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006722 if (event->parent)
6723 return;
6724
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006725 if (event->attach_state & PERF_ATTACH_TASK)
6726 static_key_slow_inc(&perf_sched_events.key);
6727 if (event->attr.mmap || event->attr.mmap_data)
6728 atomic_inc(&nr_mmap_events);
6729 if (event->attr.comm)
6730 atomic_inc(&nr_comm_events);
6731 if (event->attr.task)
6732 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006733 if (event->attr.freq) {
6734 if (atomic_inc_return(&nr_freq_events) == 1)
6735 tick_nohz_full_kick_all();
6736 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006737 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006738 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006739 if (is_cgroup_event(event))
6740 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006741
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006742 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006743}
6744
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745/*
6746 * Allocate and initialize a event structure
6747 */
6748static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006749perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006750 struct task_struct *task,
6751 struct perf_event *group_leader,
6752 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006753 perf_overflow_handler_t overflow_handler,
6754 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006756 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006757 struct perf_event *event;
6758 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006759 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006760
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006761 if ((unsigned)cpu >= nr_cpu_ids) {
6762 if (!task || cpu != -1)
6763 return ERR_PTR(-EINVAL);
6764 }
6765
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006766 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006767 if (!event)
6768 return ERR_PTR(-ENOMEM);
6769
6770 /*
6771 * Single events are their own group leaders, with an
6772 * empty sibling list:
6773 */
6774 if (!group_leader)
6775 group_leader = event;
6776
6777 mutex_init(&event->child_mutex);
6778 INIT_LIST_HEAD(&event->child_list);
6779
6780 INIT_LIST_HEAD(&event->group_entry);
6781 INIT_LIST_HEAD(&event->event_entry);
6782 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006783 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006784 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006785 INIT_HLIST_NODE(&event->hlist_entry);
6786
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006788 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006789 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006790
6791 mutex_init(&event->mmap_mutex);
6792
Al Viroa6fa9412012-08-20 14:59:25 +01006793 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006794 event->cpu = cpu;
6795 event->attr = *attr;
6796 event->group_leader = group_leader;
6797 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006798 event->oncpu = -1;
6799
6800 event->parent = parent_event;
6801
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006802 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006803 event->id = atomic64_inc_return(&perf_event_id);
6804
6805 event->state = PERF_EVENT_STATE_INACTIVE;
6806
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006807 if (task) {
6808 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006809
6810 if (attr->type == PERF_TYPE_TRACEPOINT)
6811 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006812#ifdef CONFIG_HAVE_HW_BREAKPOINT
6813 /*
6814 * hw_breakpoint is a bit difficult here..
6815 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006816 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006817 event->hw.bp_target = task;
6818#endif
6819 }
6820
Avi Kivity4dc0da82011-06-29 18:42:35 +03006821 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006822 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006823 context = parent_event->overflow_handler_context;
6824 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006825
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006826 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006827 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006828
Jiri Olsa0231bb52013-02-01 11:23:45 +01006829 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006830
6831 pmu = NULL;
6832
6833 hwc = &event->hw;
6834 hwc->sample_period = attr->sample_period;
6835 if (attr->freq && attr->sample_freq)
6836 hwc->sample_period = 1;
6837 hwc->last_period = hwc->sample_period;
6838
Peter Zijlstrae7850592010-05-21 14:43:08 +02006839 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006840
6841 /*
6842 * we currently do not support PERF_FORMAT_GROUP on inherited events
6843 */
6844 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006845 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006846
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006847 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006848 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006849 goto err_ns;
6850 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006851 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006852 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006853 }
6854
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006855 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006856 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6857 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006858 if (err)
6859 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006860 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006861 }
6862
6863 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006864
6865err_pmu:
6866 if (event->destroy)
6867 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08006868 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006869err_ns:
6870 if (event->ns)
6871 put_pid_ns(event->ns);
6872 kfree(event);
6873
6874 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875}
6876
6877static int perf_copy_attr(struct perf_event_attr __user *uattr,
6878 struct perf_event_attr *attr)
6879{
6880 u32 size;
6881 int ret;
6882
6883 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6884 return -EFAULT;
6885
6886 /*
6887 * zero the full structure, so that a short copy will be nice.
6888 */
6889 memset(attr, 0, sizeof(*attr));
6890
6891 ret = get_user(size, &uattr->size);
6892 if (ret)
6893 return ret;
6894
6895 if (size > PAGE_SIZE) /* silly large */
6896 goto err_size;
6897
6898 if (!size) /* abi compat */
6899 size = PERF_ATTR_SIZE_VER0;
6900
6901 if (size < PERF_ATTR_SIZE_VER0)
6902 goto err_size;
6903
6904 /*
6905 * If we're handed a bigger struct than we know of,
6906 * ensure all the unknown bits are 0 - i.e. new
6907 * user-space does not rely on any kernel feature
6908 * extensions we dont know about yet.
6909 */
6910 if (size > sizeof(*attr)) {
6911 unsigned char __user *addr;
6912 unsigned char __user *end;
6913 unsigned char val;
6914
6915 addr = (void __user *)uattr + sizeof(*attr);
6916 end = (void __user *)uattr + size;
6917
6918 for (; addr < end; addr++) {
6919 ret = get_user(val, addr);
6920 if (ret)
6921 return ret;
6922 if (val)
6923 goto err_size;
6924 }
6925 size = sizeof(*attr);
6926 }
6927
6928 ret = copy_from_user(attr, uattr, size);
6929 if (ret)
6930 return -EFAULT;
6931
Stephane Eranian3090ffb2013-10-17 19:32:15 +02006932 /* disabled for now */
6933 if (attr->mmap2)
6934 return -EINVAL;
6935
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306936 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006937 return -EINVAL;
6938
6939 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6940 return -EINVAL;
6941
6942 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6943 return -EINVAL;
6944
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006945 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6946 u64 mask = attr->branch_sample_type;
6947
6948 /* only using defined bits */
6949 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6950 return -EINVAL;
6951
6952 /* at least one branch bit must be set */
6953 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6954 return -EINVAL;
6955
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006956 /* propagate priv level, when not set for branch */
6957 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6958
6959 /* exclude_kernel checked on syscall entry */
6960 if (!attr->exclude_kernel)
6961 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6962
6963 if (!attr->exclude_user)
6964 mask |= PERF_SAMPLE_BRANCH_USER;
6965
6966 if (!attr->exclude_hv)
6967 mask |= PERF_SAMPLE_BRANCH_HV;
6968 /*
6969 * adjust user setting (for HW filter setup)
6970 */
6971 attr->branch_sample_type = mask;
6972 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006973 /* privileged levels capture (kernel, hv): check permissions */
6974 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006975 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6976 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006977 }
Jiri Olsa40189942012-08-07 15:20:37 +02006978
Jiri Olsac5ebced2012-08-07 15:20:40 +02006979 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006980 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006981 if (ret)
6982 return ret;
6983 }
6984
6985 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6986 if (!arch_perf_have_user_stack_dump())
6987 return -ENOSYS;
6988
6989 /*
6990 * We have __u32 type for the size, but so far
6991 * we can only use __u16 as maximum due to the
6992 * __u16 sample size limit.
6993 */
6994 if (attr->sample_stack_user >= USHRT_MAX)
6995 ret = -EINVAL;
6996 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6997 ret = -EINVAL;
6998 }
Jiri Olsa40189942012-08-07 15:20:37 +02006999
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007000out:
7001 return ret;
7002
7003err_size:
7004 put_user(sizeof(*attr), &uattr->size);
7005 ret = -E2BIG;
7006 goto out;
7007}
7008
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007009static int
7010perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007011{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007012 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007013 int ret = -EINVAL;
7014
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007015 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016 goto set;
7017
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007018 /* don't allow circular references */
7019 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007020 goto out;
7021
Peter Zijlstra0f139302010-05-20 14:35:15 +02007022 /*
7023 * Don't allow cross-cpu buffers
7024 */
7025 if (output_event->cpu != event->cpu)
7026 goto out;
7027
7028 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007029 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007030 */
7031 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7032 goto out;
7033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007034set:
7035 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007036 /* Can't redirect output if we've got an active mmap() */
7037 if (atomic_read(&event->mmap_count))
7038 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007039
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007040 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007041 /* get the rb we want to redirect to */
7042 rb = ring_buffer_get(output_event);
7043 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007044 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045 }
7046
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007047 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007050unlock:
7051 mutex_unlock(&event->mmap_mutex);
7052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007053out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054 return ret;
7055}
7056
7057/**
7058 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7059 *
7060 * @attr_uptr: event_id type attributes for monitoring/sampling
7061 * @pid: target pid
7062 * @cpu: target cpu
7063 * @group_fd: group leader event fd
7064 */
7065SYSCALL_DEFINE5(perf_event_open,
7066 struct perf_event_attr __user *, attr_uptr,
7067 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7068{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007069 struct perf_event *group_leader = NULL, *output_event = NULL;
7070 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071 struct perf_event_attr attr;
7072 struct perf_event_context *ctx;
7073 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007074 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007075 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007076 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007077 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007078 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007079 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007080 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007081
7082 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007083 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007084 return -EINVAL;
7085
7086 err = perf_copy_attr(attr_uptr, &attr);
7087 if (err)
7088 return err;
7089
7090 if (!attr.exclude_kernel) {
7091 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7092 return -EACCES;
7093 }
7094
7095 if (attr.freq) {
7096 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7097 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007098 } else {
7099 if (attr.sample_period & (1ULL << 63))
7100 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007101 }
7102
Stephane Eraniane5d13672011-02-14 11:20:01 +02007103 /*
7104 * In cgroup mode, the pid argument is used to pass the fd
7105 * opened to the cgroup directory in cgroupfs. The cpu argument
7106 * designates the cpu on which to monitor threads from that
7107 * cgroup.
7108 */
7109 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7110 return -EINVAL;
7111
Yann Droneauda21b0b32014-01-05 21:36:33 +01007112 if (flags & PERF_FLAG_FD_CLOEXEC)
7113 f_flags |= O_CLOEXEC;
7114
7115 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007116 if (event_fd < 0)
7117 return event_fd;
7118
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007119 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007120 err = perf_fget_light(group_fd, &group);
7121 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007122 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007123 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007124 if (flags & PERF_FLAG_FD_OUTPUT)
7125 output_event = group_leader;
7126 if (flags & PERF_FLAG_FD_NO_GROUP)
7127 group_leader = NULL;
7128 }
7129
Stephane Eraniane5d13672011-02-14 11:20:01 +02007130 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007131 task = find_lively_task_by_vpid(pid);
7132 if (IS_ERR(task)) {
7133 err = PTR_ERR(task);
7134 goto err_group_fd;
7135 }
7136 }
7137
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007138 if (task && group_leader &&
7139 group_leader->attr.inherit != attr.inherit) {
7140 err = -EINVAL;
7141 goto err_task;
7142 }
7143
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007144 get_online_cpus();
7145
Avi Kivity4dc0da82011-06-29 18:42:35 +03007146 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7147 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007148 if (IS_ERR(event)) {
7149 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007150 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007151 }
7152
Stephane Eraniane5d13672011-02-14 11:20:01 +02007153 if (flags & PERF_FLAG_PID_CGROUP) {
7154 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007155 if (err) {
7156 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007157 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007158 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007159 }
7160
Vince Weaver53b25332014-05-16 17:12:12 -04007161 if (is_sampling_event(event)) {
7162 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7163 err = -ENOTSUPP;
7164 goto err_alloc;
7165 }
7166 }
7167
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007168 account_event(event);
7169
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007170 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007171 * Special case software events and allow them to be part of
7172 * any hardware group.
7173 */
7174 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007175
7176 if (group_leader &&
7177 (is_software_event(event) != is_software_event(group_leader))) {
7178 if (is_software_event(event)) {
7179 /*
7180 * If event and group_leader are not both a software
7181 * event, and event is, then group leader is not.
7182 *
7183 * Allow the addition of software events to !software
7184 * groups, this is safe because software events never
7185 * fail to schedule.
7186 */
7187 pmu = group_leader->pmu;
7188 } else if (is_software_event(group_leader) &&
7189 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7190 /*
7191 * In case the group is a pure software group, and we
7192 * try to add a hardware event, move the whole group to
7193 * the hardware context.
7194 */
7195 move_group = 1;
7196 }
7197 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007198
7199 /*
7200 * Get the target context (task or percpu):
7201 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007202 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007203 if (IS_ERR(ctx)) {
7204 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007205 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007206 }
7207
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007208 if (task) {
7209 put_task_struct(task);
7210 task = NULL;
7211 }
7212
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007213 /*
7214 * Look up the group leader (we will attach this event to it):
7215 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007216 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007217 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219 /*
7220 * Do not allow a recursive hierarchy (this new sibling
7221 * becoming part of another group-sibling):
7222 */
7223 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007224 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007225 /*
7226 * Do not allow to attach to a group in a different
7227 * task or CPU context:
7228 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007229 if (move_group) {
7230 if (group_leader->ctx->type != ctx->type)
7231 goto err_context;
7232 } else {
7233 if (group_leader->ctx != ctx)
7234 goto err_context;
7235 }
7236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007237 /*
7238 * Only a group leader can be exclusive or pinned
7239 */
7240 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007241 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007242 }
7243
7244 if (output_event) {
7245 err = perf_event_set_output(event, output_event);
7246 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007247 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007248 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007249
Yann Droneauda21b0b32014-01-05 21:36:33 +01007250 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7251 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007252 if (IS_ERR(event_file)) {
7253 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007254 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007255 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007256
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007257 if (move_group) {
7258 struct perf_event_context *gctx = group_leader->ctx;
7259
7260 mutex_lock(&gctx->mutex);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007261 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007262
7263 /*
7264 * Removing from the context ends up with disabled
7265 * event. What we want here is event in the initial
7266 * startup state, ready to be add into new context.
7267 */
7268 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007269 list_for_each_entry(sibling, &group_leader->sibling_list,
7270 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007271 perf_remove_from_context(sibling, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007272 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007273 put_ctx(gctx);
7274 }
7275 mutex_unlock(&gctx->mutex);
7276 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007277 }
7278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279 WARN_ON_ONCE(ctx->parent_ctx);
7280 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007281
7282 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007283 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007284 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007285 get_ctx(ctx);
7286 list_for_each_entry(sibling, &group_leader->sibling_list,
7287 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007288 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007289 get_ctx(ctx);
7290 }
7291 }
7292
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007293 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007294 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007295 mutex_unlock(&ctx->mutex);
7296
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007297 put_online_cpus();
7298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007299 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007300
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007301 mutex_lock(&current->perf_event_mutex);
7302 list_add_tail(&event->owner_entry, &current->perf_event_list);
7303 mutex_unlock(&current->perf_event_mutex);
7304
Peter Zijlstra8a495422010-05-27 15:47:49 +02007305 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007306 * Precalculate sample_data sizes
7307 */
7308 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007309 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007310
7311 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007312 * Drop the reference on the group_event after placing the
7313 * new event on the sibling_list. This ensures destruction
7314 * of the group leader will find the pointer to itself in
7315 * perf_group_detach().
7316 */
Al Viro2903ff02012-08-28 12:52:22 -04007317 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007318 fd_install(event_fd, event_file);
7319 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007320
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007321err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007322 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007323 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007324err_alloc:
7325 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007326err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007327 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007328err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007329 if (task)
7330 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007331err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007332 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007333err_fd:
7334 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007335 return err;
7336}
7337
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007338/**
7339 * perf_event_create_kernel_counter
7340 *
7341 * @attr: attributes of the counter to create
7342 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007343 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007344 */
7345struct perf_event *
7346perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007347 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007348 perf_overflow_handler_t overflow_handler,
7349 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007350{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007351 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007352 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007353 int err;
7354
7355 /*
7356 * Get the target context (task or percpu):
7357 */
7358
Avi Kivity4dc0da82011-06-29 18:42:35 +03007359 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7360 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007361 if (IS_ERR(event)) {
7362 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007363 goto err;
7364 }
7365
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007366 account_event(event);
7367
Matt Helsley38a81da2010-09-13 13:01:20 -07007368 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007369 if (IS_ERR(ctx)) {
7370 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007371 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007372 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007373
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007374 WARN_ON_ONCE(ctx->parent_ctx);
7375 mutex_lock(&ctx->mutex);
7376 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007377 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007378 mutex_unlock(&ctx->mutex);
7379
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007380 return event;
7381
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007382err_free:
7383 free_event(event);
7384err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007385 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007386}
7387EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7388
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007389void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7390{
7391 struct perf_event_context *src_ctx;
7392 struct perf_event_context *dst_ctx;
7393 struct perf_event *event, *tmp;
7394 LIST_HEAD(events);
7395
7396 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7397 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7398
7399 mutex_lock(&src_ctx->mutex);
7400 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7401 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007402 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007403 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007404 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007405 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007406 }
7407 mutex_unlock(&src_ctx->mutex);
7408
7409 synchronize_rcu();
7410
7411 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007412 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7413 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007414 if (event->state >= PERF_EVENT_STATE_OFF)
7415 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007416 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007417 perf_install_in_context(dst_ctx, event, dst_cpu);
7418 get_ctx(dst_ctx);
7419 }
7420 mutex_unlock(&dst_ctx->mutex);
7421}
7422EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007424static void sync_child_event(struct perf_event *child_event,
7425 struct task_struct *child)
7426{
7427 struct perf_event *parent_event = child_event->parent;
7428 u64 child_val;
7429
7430 if (child_event->attr.inherit_stat)
7431 perf_event_read_event(child_event, child);
7432
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007433 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007434
7435 /*
7436 * Add back the child's count to the parent's count:
7437 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007438 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007439 atomic64_add(child_event->total_time_enabled,
7440 &parent_event->child_total_time_enabled);
7441 atomic64_add(child_event->total_time_running,
7442 &parent_event->child_total_time_running);
7443
7444 /*
7445 * Remove this event from the parent's list
7446 */
7447 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7448 mutex_lock(&parent_event->child_mutex);
7449 list_del_init(&child_event->child_list);
7450 mutex_unlock(&parent_event->child_mutex);
7451
7452 /*
7453 * Release the parent event, if this was the last
7454 * reference to it.
7455 */
Al Viroa6fa9412012-08-20 14:59:25 +01007456 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007457}
7458
7459static void
7460__perf_event_exit_task(struct perf_event *child_event,
7461 struct perf_event_context *child_ctx,
7462 struct task_struct *child)
7463{
Peter Zijlstra15a2d4d2014-05-05 11:41:02 +02007464 perf_remove_from_context(child_event, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007466 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007467 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007468 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007469 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007470 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007471 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007472 sync_child_event(child_event, child);
7473 free_event(child_event);
7474 }
7475}
7476
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007477static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007478{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007479 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007480 struct perf_event_context *child_ctx;
7481 unsigned long flags;
7482
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007483 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007484 perf_event_task(child, NULL, 0);
7485 return;
7486 }
7487
7488 local_irq_save(flags);
7489 /*
7490 * We can't reschedule here because interrupts are disabled,
7491 * and either child is current or it is a task that can't be
7492 * scheduled, so we are now safe from rescheduling changing
7493 * our context.
7494 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007495 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007496
7497 /*
7498 * Take the context lock here so that if find_get_context is
7499 * reading child->perf_event_ctxp, we wait until it has
7500 * incremented the context's refcount before we do put_ctx below.
7501 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007502 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007503 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007504 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007505 /*
7506 * If this context is a clone; unclone it so it can't get
7507 * swapped to another process while we're removing all
7508 * the events from it.
7509 */
7510 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007511 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007512 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007513
7514 /*
7515 * Report the task dead after unscheduling the events so that we
7516 * won't get any samples after PERF_RECORD_EXIT. We can however still
7517 * get a few PERF_RECORD_READ events.
7518 */
7519 perf_event_task(child, child_ctx, 0);
7520
7521 /*
7522 * We can recurse on the same lock type through:
7523 *
7524 * __perf_event_exit_task()
7525 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007526 * put_event()
7527 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007528 *
7529 * But since its the parent context it won't be the same instance.
7530 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007531 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007532
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007533 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007534 __perf_event_exit_task(child_event, child_ctx, child);
7535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007536 mutex_unlock(&child_ctx->mutex);
7537
7538 put_ctx(child_ctx);
7539}
7540
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007541/*
7542 * When a child task exits, feed back event values to parent events.
7543 */
7544void perf_event_exit_task(struct task_struct *child)
7545{
Peter Zijlstra88821352010-11-09 19:01:43 +01007546 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007547 int ctxn;
7548
Peter Zijlstra88821352010-11-09 19:01:43 +01007549 mutex_lock(&child->perf_event_mutex);
7550 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7551 owner_entry) {
7552 list_del_init(&event->owner_entry);
7553
7554 /*
7555 * Ensure the list deletion is visible before we clear
7556 * the owner, closes a race against perf_release() where
7557 * we need to serialize on the owner->perf_event_mutex.
7558 */
7559 smp_wmb();
7560 event->owner = NULL;
7561 }
7562 mutex_unlock(&child->perf_event_mutex);
7563
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007564 for_each_task_context_nr(ctxn)
7565 perf_event_exit_task_context(child, ctxn);
7566}
7567
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007568static void perf_free_event(struct perf_event *event,
7569 struct perf_event_context *ctx)
7570{
7571 struct perf_event *parent = event->parent;
7572
7573 if (WARN_ON_ONCE(!parent))
7574 return;
7575
7576 mutex_lock(&parent->child_mutex);
7577 list_del_init(&event->child_list);
7578 mutex_unlock(&parent->child_mutex);
7579
Al Viroa6fa9412012-08-20 14:59:25 +01007580 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007581
Peter Zijlstra8a495422010-05-27 15:47:49 +02007582 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007583 list_del_event(event, ctx);
7584 free_event(event);
7585}
7586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007587/*
7588 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007589 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007590 */
7591void perf_event_free_task(struct task_struct *task)
7592{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007593 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007594 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007595 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007596
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007597 for_each_task_context_nr(ctxn) {
7598 ctx = task->perf_event_ctxp[ctxn];
7599 if (!ctx)
7600 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007601
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007602 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007603again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007604 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7605 group_entry)
7606 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007607
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007608 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7609 group_entry)
7610 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007611
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007612 if (!list_empty(&ctx->pinned_groups) ||
7613 !list_empty(&ctx->flexible_groups))
7614 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007615
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007616 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007617
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007618 put_ctx(ctx);
7619 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007620}
7621
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007622void perf_event_delayed_put(struct task_struct *task)
7623{
7624 int ctxn;
7625
7626 for_each_task_context_nr(ctxn)
7627 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7628}
7629
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007630/*
7631 * inherit a event from parent task to child task:
7632 */
7633static struct perf_event *
7634inherit_event(struct perf_event *parent_event,
7635 struct task_struct *parent,
7636 struct perf_event_context *parent_ctx,
7637 struct task_struct *child,
7638 struct perf_event *group_leader,
7639 struct perf_event_context *child_ctx)
7640{
7641 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007642 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007643
7644 /*
7645 * Instead of creating recursive hierarchies of events,
7646 * we link inherited events back to the original parent,
7647 * which has a filp for sure, which we use as the reference
7648 * count:
7649 */
7650 if (parent_event->parent)
7651 parent_event = parent_event->parent;
7652
7653 child_event = perf_event_alloc(&parent_event->attr,
7654 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007655 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007656 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007657 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007658 if (IS_ERR(child_event))
7659 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007660
7661 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7662 free_event(child_event);
7663 return NULL;
7664 }
7665
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007666 get_ctx(child_ctx);
7667
7668 /*
7669 * Make the child state follow the state of the parent event,
7670 * not its attr.disabled bit. We hold the parent's mutex,
7671 * so we won't race with perf_event_{en, dis}able_family.
7672 */
7673 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7674 child_event->state = PERF_EVENT_STATE_INACTIVE;
7675 else
7676 child_event->state = PERF_EVENT_STATE_OFF;
7677
7678 if (parent_event->attr.freq) {
7679 u64 sample_period = parent_event->hw.sample_period;
7680 struct hw_perf_event *hwc = &child_event->hw;
7681
7682 hwc->sample_period = sample_period;
7683 hwc->last_period = sample_period;
7684
7685 local64_set(&hwc->period_left, sample_period);
7686 }
7687
7688 child_event->ctx = child_ctx;
7689 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007690 child_event->overflow_handler_context
7691 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007692
7693 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007694 * Precalculate sample_data sizes
7695 */
7696 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007697 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007698
7699 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007700 * Link it up in the child's context:
7701 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007702 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007703 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007704 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007705
7706 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007707 * Link this into the parent event's child list
7708 */
7709 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7710 mutex_lock(&parent_event->child_mutex);
7711 list_add_tail(&child_event->child_list, &parent_event->child_list);
7712 mutex_unlock(&parent_event->child_mutex);
7713
7714 return child_event;
7715}
7716
7717static int inherit_group(struct perf_event *parent_event,
7718 struct task_struct *parent,
7719 struct perf_event_context *parent_ctx,
7720 struct task_struct *child,
7721 struct perf_event_context *child_ctx)
7722{
7723 struct perf_event *leader;
7724 struct perf_event *sub;
7725 struct perf_event *child_ctr;
7726
7727 leader = inherit_event(parent_event, parent, parent_ctx,
7728 child, NULL, child_ctx);
7729 if (IS_ERR(leader))
7730 return PTR_ERR(leader);
7731 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7732 child_ctr = inherit_event(sub, parent, parent_ctx,
7733 child, leader, child_ctx);
7734 if (IS_ERR(child_ctr))
7735 return PTR_ERR(child_ctr);
7736 }
7737 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007738}
7739
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007740static int
7741inherit_task_group(struct perf_event *event, struct task_struct *parent,
7742 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007743 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007744 int *inherited_all)
7745{
7746 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007747 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007748
7749 if (!event->attr.inherit) {
7750 *inherited_all = 0;
7751 return 0;
7752 }
7753
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007754 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007755 if (!child_ctx) {
7756 /*
7757 * This is executed from the parent task context, so
7758 * inherit events that have been marked for cloning.
7759 * First allocate and initialize a context for the
7760 * child.
7761 */
7762
Jiri Olsa734df5a2013-07-09 17:44:10 +02007763 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007764 if (!child_ctx)
7765 return -ENOMEM;
7766
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007767 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007768 }
7769
7770 ret = inherit_group(event, parent, parent_ctx,
7771 child, child_ctx);
7772
7773 if (ret)
7774 *inherited_all = 0;
7775
7776 return ret;
7777}
7778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007779/*
7780 * Initialize the perf_event context in task_struct
7781 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007782int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007783{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007784 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007785 struct perf_event_context *cloned_ctx;
7786 struct perf_event *event;
7787 struct task_struct *parent = current;
7788 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007789 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007790 int ret = 0;
7791
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007792 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007793 return 0;
7794
7795 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007796 * If the parent's context is a clone, pin it so it won't get
7797 * swapped under us.
7798 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007799 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02007800 if (!parent_ctx)
7801 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007802
7803 /*
7804 * No need to check if parent_ctx != NULL here; since we saw
7805 * it non-NULL earlier, the only reason for it to become NULL
7806 * is if we exit, and since we're currently in the middle of
7807 * a fork we can't be exiting at the same time.
7808 */
7809
7810 /*
7811 * Lock the parent list. No need to lock the child - not PID
7812 * hashed yet and not running, so nobody can access it.
7813 */
7814 mutex_lock(&parent_ctx->mutex);
7815
7816 /*
7817 * We dont have to disable NMIs - we are only looking at
7818 * the list, not manipulating it:
7819 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007820 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007821 ret = inherit_task_group(event, parent, parent_ctx,
7822 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007823 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007824 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007825 }
7826
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007827 /*
7828 * We can't hold ctx->lock when iterating the ->flexible_group list due
7829 * to allocations, but we need to prevent rotation because
7830 * rotate_ctx() will change the list from interrupt context.
7831 */
7832 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7833 parent_ctx->rotate_disable = 1;
7834 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7835
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007836 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007837 ret = inherit_task_group(event, parent, parent_ctx,
7838 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007839 if (ret)
7840 break;
7841 }
7842
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007843 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7844 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007845
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007846 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007847
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007848 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007849 /*
7850 * Mark the child context as a clone of the parent
7851 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007852 *
7853 * Note that if the parent is a clone, the holding of
7854 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007855 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007856 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007857 if (cloned_ctx) {
7858 child_ctx->parent_ctx = cloned_ctx;
7859 child_ctx->parent_gen = parent_ctx->parent_gen;
7860 } else {
7861 child_ctx->parent_ctx = parent_ctx;
7862 child_ctx->parent_gen = parent_ctx->generation;
7863 }
7864 get_ctx(child_ctx->parent_ctx);
7865 }
7866
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007867 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007868 mutex_unlock(&parent_ctx->mutex);
7869
7870 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007871 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007872
7873 return ret;
7874}
7875
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007876/*
7877 * Initialize the perf_event context in task_struct
7878 */
7879int perf_event_init_task(struct task_struct *child)
7880{
7881 int ctxn, ret;
7882
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007883 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7884 mutex_init(&child->perf_event_mutex);
7885 INIT_LIST_HEAD(&child->perf_event_list);
7886
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007887 for_each_task_context_nr(ctxn) {
7888 ret = perf_event_init_context(child, ctxn);
7889 if (ret)
7890 return ret;
7891 }
7892
7893 return 0;
7894}
7895
Paul Mackerras220b1402010-03-10 20:45:52 +11007896static void __init perf_event_init_all_cpus(void)
7897{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007898 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007899 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007900
7901 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007902 swhash = &per_cpu(swevent_htable, cpu);
7903 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007904 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007905 }
7906}
7907
Paul Gortmaker0db06282013-06-19 14:53:51 -04007908static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007909{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007910 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007911
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007912 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02007913 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007914 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007915 struct swevent_hlist *hlist;
7916
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007917 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7918 WARN_ON(!hlist);
7919 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007920 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007921 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007922}
7923
Peter Zijlstrac2774432010-12-08 15:29:02 +01007924#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007925static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007926{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007927 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7928
7929 WARN_ON(!irqs_disabled());
7930
7931 list_del_init(&cpuctx->rotation_list);
7932}
7933
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007934static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007935{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007936 struct remove_event re = { .detach_group = false };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007937 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007938
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007939 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007940
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007941 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007942 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
7943 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007944 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007945}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007946
7947static void perf_event_exit_cpu_context(int cpu)
7948{
7949 struct perf_event_context *ctx;
7950 struct pmu *pmu;
7951 int idx;
7952
7953 idx = srcu_read_lock(&pmus_srcu);
7954 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007955 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007956
7957 mutex_lock(&ctx->mutex);
7958 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7959 mutex_unlock(&ctx->mutex);
7960 }
7961 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007962}
7963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007964static void perf_event_exit_cpu(int cpu)
7965{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007966 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007967
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007968 perf_event_exit_cpu_context(cpu);
7969
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007970 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02007971 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007972 swevent_hlist_release(swhash);
7973 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007974}
7975#else
7976static inline void perf_event_exit_cpu(int cpu) { }
7977#endif
7978
Peter Zijlstrac2774432010-12-08 15:29:02 +01007979static int
7980perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7981{
7982 int cpu;
7983
7984 for_each_online_cpu(cpu)
7985 perf_event_exit_cpu(cpu);
7986
7987 return NOTIFY_OK;
7988}
7989
7990/*
7991 * Run the perf reboot notifier at the very last possible moment so that
7992 * the generic watchdog code runs as long as possible.
7993 */
7994static struct notifier_block perf_reboot_notifier = {
7995 .notifier_call = perf_reboot,
7996 .priority = INT_MIN,
7997};
7998
Paul Gortmaker0db06282013-06-19 14:53:51 -04007999static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008000perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8001{
8002 unsigned int cpu = (long)hcpu;
8003
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008004 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008005
8006 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008007 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008008 perf_event_init_cpu(cpu);
8009 break;
8010
Peter Zijlstra5e116372010-06-11 13:35:08 +02008011 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008012 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008013 perf_event_exit_cpu(cpu);
8014 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008015 default:
8016 break;
8017 }
8018
8019 return NOTIFY_OK;
8020}
8021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008022void __init perf_event_init(void)
8023{
Jason Wessel3c502e72010-11-04 17:33:01 -05008024 int ret;
8025
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008026 idr_init(&pmu_idr);
8027
Paul Mackerras220b1402010-03-10 20:45:52 +11008028 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008029 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008030 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8031 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8032 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008033 perf_tp_register();
8034 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008035 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008036
8037 ret = init_hw_breakpoint();
8038 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008039
8040 /* do not patch jump label more than once per second */
8041 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008042
8043 /*
8044 * Build time assertion that we keep the data_head at the intended
8045 * location. IOW, validation we got the __reserved[] size right.
8046 */
8047 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8048 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008049}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008050
8051static int __init perf_event_sysfs_init(void)
8052{
8053 struct pmu *pmu;
8054 int ret;
8055
8056 mutex_lock(&pmus_lock);
8057
8058 ret = bus_register(&pmu_bus);
8059 if (ret)
8060 goto unlock;
8061
8062 list_for_each_entry(pmu, &pmus, entry) {
8063 if (!pmu->name || pmu->type < 0)
8064 continue;
8065
8066 ret = pmu_dev_alloc(pmu);
8067 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8068 }
8069 pmu_bus_running = 1;
8070 ret = 0;
8071
8072unlock:
8073 mutex_unlock(&pmus_lock);
8074
8075 return ret;
8076}
8077device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008078
8079#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008080static struct cgroup_subsys_state *
8081perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008082{
8083 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008084
Li Zefan1b15d052011-03-03 14:26:06 +08008085 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008086 if (!jc)
8087 return ERR_PTR(-ENOMEM);
8088
Stephane Eraniane5d13672011-02-14 11:20:01 +02008089 jc->info = alloc_percpu(struct perf_cgroup_info);
8090 if (!jc->info) {
8091 kfree(jc);
8092 return ERR_PTR(-ENOMEM);
8093 }
8094
Stephane Eraniane5d13672011-02-14 11:20:01 +02008095 return &jc->css;
8096}
8097
Tejun Heoeb954192013-08-08 20:11:23 -04008098static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008099{
Tejun Heoeb954192013-08-08 20:11:23 -04008100 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8101
Stephane Eraniane5d13672011-02-14 11:20:01 +02008102 free_percpu(jc->info);
8103 kfree(jc);
8104}
8105
8106static int __perf_cgroup_move(void *info)
8107{
8108 struct task_struct *task = info;
8109 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8110 return 0;
8111}
8112
Tejun Heoeb954192013-08-08 20:11:23 -04008113static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8114 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008115{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008116 struct task_struct *task;
8117
Tejun Heo924f0d92014-02-13 06:58:41 -05008118 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008119 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008120}
8121
Tejun Heoeb954192013-08-08 20:11:23 -04008122static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8123 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008124 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008125{
8126 /*
8127 * cgroup_exit() is called in the copy_process() failure path.
8128 * Ignore this case since the task hasn't ran yet, this avoids
8129 * trying to poke a half freed task state from generic code.
8130 */
8131 if (!(task->flags & PF_EXITING))
8132 return;
8133
Tejun Heobb9d97b2011-12-12 18:12:21 -08008134 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008135}
8136
Tejun Heo073219e2014-02-08 10:36:58 -05008137struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008138 .css_alloc = perf_cgroup_css_alloc,
8139 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008140 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008141 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008142};
8143#endif /* CONFIG_CGROUP_PERF */