blob: fa0b2d4ad83c5f7a08dfecf27d16d33d928a80f2 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
Frederic Weisbecker76369132011-05-19 19:55:04 +020043#include "internal.h"
44
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045#include <asm/irq_regs.h>
46
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010047struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020048 struct task_struct *p;
49 int (*func)(void *info);
50 void *info;
51 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052};
53
54static void remote_function(void *data)
55{
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
58
59 if (p) {
60 tfc->ret = -EAGAIN;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 return;
63 }
64
65 tfc->ret = tfc->func(tfc->info);
66}
67
68/**
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
73 *
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
76 *
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
80 */
81static int
82task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
83{
84 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020085 .p = p,
86 .func = func,
87 .info = info,
88 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010089 };
90
91 if (task_curr(p))
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
93
94 return data.ret;
95}
96
97/**
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
101 *
102 * Calls the function @func on the remote cpu.
103 *
104 * returns: @func return value or -ENXIO when the cpu is offline
105 */
106static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
107{
108 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200109 .p = NULL,
110 .func = func,
111 .info = info,
112 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100113 };
114
115 smp_call_function_single(cpu, remote_function, &data, 1);
116
117 return data.ret;
118}
119
Stephane Eraniane5d13672011-02-14 11:20:01 +0200120#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100122 PERF_FLAG_PID_CGROUP |\
123 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200124
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100125/*
126 * branch priv levels that need permission checks
127 */
128#define PERF_SAMPLE_BRANCH_PERM_PLM \
129 (PERF_SAMPLE_BRANCH_KERNEL |\
130 PERF_SAMPLE_BRANCH_HV)
131
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200132enum event_type_t {
133 EVENT_FLEXIBLE = 0x1,
134 EVENT_PINNED = 0x2,
135 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
136};
137
Stephane Eraniane5d13672011-02-14 11:20:01 +0200138/*
139 * perf_sched_events : >0 events exist
140 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
141 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100142struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200143static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100144static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200145
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200146static atomic_t nr_mmap_events __read_mostly;
147static atomic_t nr_comm_events __read_mostly;
148static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200149static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200150
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200151static LIST_HEAD(pmus);
152static DEFINE_MUTEX(pmus_lock);
153static struct srcu_struct pmus_srcu;
154
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200155/*
156 * perf event paranoia level:
157 * -1 - not paranoid at all
158 * 0 - disallow raw tracepoint access for unpriv
159 * 1 - disallow cpu events for unpriv
160 * 2 - disallow kernel profiling for unpriv
161 */
162int sysctl_perf_event_paranoid __read_mostly = 1;
163
Frederic Weisbecker20443382011-03-31 03:33:29 +0200164/* Minimum for 512 kiB + 1 user control page */
165int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200166
167/*
168 * max perf event sample rate
169 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700170#define DEFAULT_MAX_SAMPLE_RATE 100000
171#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
172#define DEFAULT_CPU_TIME_MAX_PERCENT 25
173
174int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
175
176static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
177static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
178
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200179static int perf_sample_allowed_ns __read_mostly =
180 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700181
182void update_perf_cpu_limits(void)
183{
184 u64 tmp = perf_sample_period_ns;
185
186 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200187 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200188 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700189}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100190
Stephane Eranian9e630202013-04-03 14:21:33 +0200191static int perf_rotate_context(struct perf_cpu_context *cpuctx);
192
Peter Zijlstra163ec432011-02-16 11:22:34 +0100193int perf_proc_update_handler(struct ctl_table *table, int write,
194 void __user *buffer, size_t *lenp,
195 loff_t *ppos)
196{
Knut Petersen723478c2013-09-25 14:29:37 +0200197 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100198
199 if (ret || !write)
200 return ret;
201
202 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700203 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
204 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100205
206 return 0;
207}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200208
Dave Hansen14c63f12013-06-21 08:51:36 -0700209int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
210
211int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
212 void __user *buffer, size_t *lenp,
213 loff_t *ppos)
214{
215 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
216
217 if (ret || !write)
218 return ret;
219
220 update_perf_cpu_limits();
221
222 return 0;
223}
224
225/*
226 * perf samples are done in some very critical code paths (NMIs).
227 * If they take too much CPU time, the system can lock up and not
228 * get any real work done. This will drop the sample rate when
229 * we detect that events are taking too long.
230 */
231#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200232static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700233
234void perf_sample_event_took(u64 sample_len_ns)
235{
236 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200237 u64 local_samples_len;
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200238 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700239
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200240 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700241 return;
242
243 /* decay the counter by 1 average sample */
244 local_samples_len = __get_cpu_var(running_sample_length);
245 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
246 local_samples_len += sample_len_ns;
247 __get_cpu_var(running_sample_length) = local_samples_len;
248
249 /*
250 * note: this will be biased artifically low until we have
251 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
252 * from having to maintain a count.
253 */
254 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
255
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200256 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700257 return;
258
259 if (max_samples_per_tick <= 1)
260 return;
261
262 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
263 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
264 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
265
266 printk_ratelimited(KERN_WARNING
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200267 "perf samples too long (%lld > %lld), lowering "
Dave Hansen14c63f12013-06-21 08:51:36 -0700268 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200269 avg_local_sample_len, allowed_ns,
Dave Hansen14c63f12013-06-21 08:51:36 -0700270 sysctl_perf_event_sample_rate);
271
272 update_perf_cpu_limits();
273}
274
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200275static atomic64_t perf_event_id;
276
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200277static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
278 enum event_type_t event_type);
279
280static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200281 enum event_type_t event_type,
282 struct task_struct *task);
283
284static void update_context_time(struct perf_event_context *ctx);
285static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200286
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200287void __weak perf_event_print_debug(void) { }
288
Matt Fleming84c79912010-10-03 21:41:13 +0100289extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200290{
Matt Fleming84c79912010-10-03 21:41:13 +0100291 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200292}
293
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200294static inline u64 perf_clock(void)
295{
296 return local_clock();
297}
298
Stephane Eraniane5d13672011-02-14 11:20:01 +0200299static inline struct perf_cpu_context *
300__get_cpu_context(struct perf_event_context *ctx)
301{
302 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
303}
304
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200305static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
306 struct perf_event_context *ctx)
307{
308 raw_spin_lock(&cpuctx->ctx.lock);
309 if (ctx)
310 raw_spin_lock(&ctx->lock);
311}
312
313static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
314 struct perf_event_context *ctx)
315{
316 if (ctx)
317 raw_spin_unlock(&ctx->lock);
318 raw_spin_unlock(&cpuctx->ctx.lock);
319}
320
Stephane Eraniane5d13672011-02-14 11:20:01 +0200321#ifdef CONFIG_CGROUP_PERF
322
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200323/*
Li Zefan877c6852013-03-05 11:38:08 +0800324 * perf_cgroup_info keeps track of time_enabled for a cgroup.
325 * This is a per-cpu dynamically allocated data structure.
326 */
327struct perf_cgroup_info {
328 u64 time;
329 u64 timestamp;
330};
331
332struct perf_cgroup {
333 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900334 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800335};
336
337/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200338 * Must ensure cgroup is pinned (css_get) before calling
339 * this function. In other words, we cannot call this function
340 * if there is no cgroup event for the current CPU context.
341 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342static inline struct perf_cgroup *
343perf_cgroup_from_task(struct task_struct *task)
344{
Tejun Heo8af01f52013-08-08 20:11:22 -0400345 return container_of(task_css(task, perf_subsys_id),
346 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200347}
348
349static inline bool
350perf_cgroup_match(struct perf_event *event)
351{
352 struct perf_event_context *ctx = event->ctx;
353 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
354
Tejun Heoef824fa2013-04-08 19:00:38 -0700355 /* @event doesn't care about cgroup */
356 if (!event->cgrp)
357 return true;
358
359 /* wants specific cgroup scope but @cpuctx isn't associated with any */
360 if (!cpuctx->cgrp)
361 return false;
362
363 /*
364 * Cgroup scoping is recursive. An event enabled for a cgroup is
365 * also enabled for all its descendant cgroups. If @cpuctx's
366 * cgroup is a descendant of @event's (the test covers identity
367 * case), it's a match.
368 */
369 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
370 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200371}
372
Salman Qazi9c5da092012-06-14 15:31:09 -0700373static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200374{
Salman Qazi9c5da092012-06-14 15:31:09 -0700375 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200376}
377
378static inline void perf_put_cgroup(struct perf_event *event)
379{
380 css_put(&event->cgrp->css);
381}
382
383static inline void perf_detach_cgroup(struct perf_event *event)
384{
385 perf_put_cgroup(event);
386 event->cgrp = NULL;
387}
388
389static inline int is_cgroup_event(struct perf_event *event)
390{
391 return event->cgrp != NULL;
392}
393
394static inline u64 perf_cgroup_event_time(struct perf_event *event)
395{
396 struct perf_cgroup_info *t;
397
398 t = per_cpu_ptr(event->cgrp->info, event->cpu);
399 return t->time;
400}
401
402static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
403{
404 struct perf_cgroup_info *info;
405 u64 now;
406
407 now = perf_clock();
408
409 info = this_cpu_ptr(cgrp->info);
410
411 info->time += now - info->timestamp;
412 info->timestamp = now;
413}
414
415static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
416{
417 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
418 if (cgrp_out)
419 __update_cgrp_time(cgrp_out);
420}
421
422static inline void update_cgrp_time_from_event(struct perf_event *event)
423{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200424 struct perf_cgroup *cgrp;
425
Stephane Eraniane5d13672011-02-14 11:20:01 +0200426 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200427 * ensure we access cgroup data only when needed and
428 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200429 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200430 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200431 return;
432
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200433 cgrp = perf_cgroup_from_task(current);
434 /*
435 * Do not update time when cgroup is not active
436 */
437 if (cgrp == event->cgrp)
438 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200439}
440
441static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200442perf_cgroup_set_timestamp(struct task_struct *task,
443 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200444{
445 struct perf_cgroup *cgrp;
446 struct perf_cgroup_info *info;
447
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200448 /*
449 * ctx->lock held by caller
450 * ensure we do not access cgroup data
451 * unless we have the cgroup pinned (css_get)
452 */
453 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200454 return;
455
456 cgrp = perf_cgroup_from_task(task);
457 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200458 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200459}
460
461#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
462#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
463
464/*
465 * reschedule events based on the cgroup constraint of task.
466 *
467 * mode SWOUT : schedule out everything
468 * mode SWIN : schedule in based on cgroup for next
469 */
470void perf_cgroup_switch(struct task_struct *task, int mode)
471{
472 struct perf_cpu_context *cpuctx;
473 struct pmu *pmu;
474 unsigned long flags;
475
476 /*
477 * disable interrupts to avoid geting nr_cgroup
478 * changes via __perf_event_disable(). Also
479 * avoids preemption.
480 */
481 local_irq_save(flags);
482
483 /*
484 * we reschedule only in the presence of cgroup
485 * constrained events.
486 */
487 rcu_read_lock();
488
489 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200490 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200491 if (cpuctx->unique_pmu != pmu)
492 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200493
Stephane Eraniane5d13672011-02-14 11:20:01 +0200494 /*
495 * perf_cgroup_events says at least one
496 * context on this CPU has cgroup events.
497 *
498 * ctx->nr_cgroups reports the number of cgroup
499 * events for a context.
500 */
501 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200502 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
503 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200504
505 if (mode & PERF_CGROUP_SWOUT) {
506 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
507 /*
508 * must not be done before ctxswout due
509 * to event_filter_match() in event_sched_out()
510 */
511 cpuctx->cgrp = NULL;
512 }
513
514 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200515 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200516 /*
517 * set cgrp before ctxsw in to allow
518 * event_filter_match() to not have to pass
519 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200520 */
521 cpuctx->cgrp = perf_cgroup_from_task(task);
522 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
523 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200524 perf_pmu_enable(cpuctx->ctx.pmu);
525 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200526 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200527 }
528
529 rcu_read_unlock();
530
531 local_irq_restore(flags);
532}
533
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200534static inline void perf_cgroup_sched_out(struct task_struct *task,
535 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200536{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200537 struct perf_cgroup *cgrp1;
538 struct perf_cgroup *cgrp2 = NULL;
539
540 /*
541 * we come here when we know perf_cgroup_events > 0
542 */
543 cgrp1 = perf_cgroup_from_task(task);
544
545 /*
546 * next is NULL when called from perf_event_enable_on_exec()
547 * that will systematically cause a cgroup_switch()
548 */
549 if (next)
550 cgrp2 = perf_cgroup_from_task(next);
551
552 /*
553 * only schedule out current cgroup events if we know
554 * that we are switching to a different cgroup. Otherwise,
555 * do no touch the cgroup events.
556 */
557 if (cgrp1 != cgrp2)
558 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559}
560
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200561static inline void perf_cgroup_sched_in(struct task_struct *prev,
562 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200563{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200564 struct perf_cgroup *cgrp1;
565 struct perf_cgroup *cgrp2 = NULL;
566
567 /*
568 * we come here when we know perf_cgroup_events > 0
569 */
570 cgrp1 = perf_cgroup_from_task(task);
571
572 /* prev can never be NULL */
573 cgrp2 = perf_cgroup_from_task(prev);
574
575 /*
576 * only need to schedule in cgroup events if we are changing
577 * cgroup during ctxsw. Cgroup events were not scheduled
578 * out of ctxsw out if that was not the case.
579 */
580 if (cgrp1 != cgrp2)
581 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582}
583
584static inline int perf_cgroup_connect(int fd, struct perf_event *event,
585 struct perf_event_attr *attr,
586 struct perf_event *group_leader)
587{
588 struct perf_cgroup *cgrp;
589 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400590 struct fd f = fdget(fd);
591 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200592
Al Viro2903ff02012-08-28 12:52:22 -0400593 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200594 return -EBADF;
595
Tejun Heob77d7b62013-08-13 11:01:54 -0400596 rcu_read_lock();
597
Tejun Heo35cf0832013-08-26 18:40:56 -0400598 css = css_from_dir(f.file->f_dentry, &perf_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800599 if (IS_ERR(css)) {
600 ret = PTR_ERR(css);
601 goto out;
602 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603
604 cgrp = container_of(css, struct perf_cgroup, css);
605 event->cgrp = cgrp;
606
Li Zefanf75e18c2011-03-03 14:25:50 +0800607 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700608 if (!perf_tryget_cgroup(event)) {
609 event->cgrp = NULL;
610 ret = -ENOENT;
611 goto out;
612 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800613
Stephane Eraniane5d13672011-02-14 11:20:01 +0200614 /*
615 * all events in a group must monitor
616 * the same cgroup because a task belongs
617 * to only one perf cgroup at a time
618 */
619 if (group_leader && group_leader->cgrp != cgrp) {
620 perf_detach_cgroup(event);
621 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200622 }
Li Zefan3db272c2011-03-03 14:25:37 +0800623out:
Tejun Heob77d7b62013-08-13 11:01:54 -0400624 rcu_read_unlock();
Al Viro2903ff02012-08-28 12:52:22 -0400625 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200626 return ret;
627}
628
629static inline void
630perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
631{
632 struct perf_cgroup_info *t;
633 t = per_cpu_ptr(event->cgrp->info, event->cpu);
634 event->shadow_ctx_time = now - t->timestamp;
635}
636
637static inline void
638perf_cgroup_defer_enabled(struct perf_event *event)
639{
640 /*
641 * when the current task's perf cgroup does not match
642 * the event's, we need to remember to call the
643 * perf_mark_enable() function the first time a task with
644 * a matching perf cgroup is scheduled in.
645 */
646 if (is_cgroup_event(event) && !perf_cgroup_match(event))
647 event->cgrp_defer_enabled = 1;
648}
649
650static inline void
651perf_cgroup_mark_enabled(struct perf_event *event,
652 struct perf_event_context *ctx)
653{
654 struct perf_event *sub;
655 u64 tstamp = perf_event_time(event);
656
657 if (!event->cgrp_defer_enabled)
658 return;
659
660 event->cgrp_defer_enabled = 0;
661
662 event->tstamp_enabled = tstamp - event->total_time_enabled;
663 list_for_each_entry(sub, &event->sibling_list, group_entry) {
664 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
665 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
666 sub->cgrp_defer_enabled = 0;
667 }
668 }
669}
670#else /* !CONFIG_CGROUP_PERF */
671
672static inline bool
673perf_cgroup_match(struct perf_event *event)
674{
675 return true;
676}
677
678static inline void perf_detach_cgroup(struct perf_event *event)
679{}
680
681static inline int is_cgroup_event(struct perf_event *event)
682{
683 return 0;
684}
685
686static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
687{
688 return 0;
689}
690
691static inline void update_cgrp_time_from_event(struct perf_event *event)
692{
693}
694
695static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
696{
697}
698
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200699static inline void perf_cgroup_sched_out(struct task_struct *task,
700 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200701{
702}
703
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200704static inline void perf_cgroup_sched_in(struct task_struct *prev,
705 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200706{
707}
708
709static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
710 struct perf_event_attr *attr,
711 struct perf_event *group_leader)
712{
713 return -EINVAL;
714}
715
716static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200717perf_cgroup_set_timestamp(struct task_struct *task,
718 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200719{
720}
721
722void
723perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
724{
725}
726
727static inline void
728perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
729{
730}
731
732static inline u64 perf_cgroup_event_time(struct perf_event *event)
733{
734 return 0;
735}
736
737static inline void
738perf_cgroup_defer_enabled(struct perf_event *event)
739{
740}
741
742static inline void
743perf_cgroup_mark_enabled(struct perf_event *event,
744 struct perf_event_context *ctx)
745{
746}
747#endif
748
Stephane Eranian9e630202013-04-03 14:21:33 +0200749/*
750 * set default to be dependent on timer tick just
751 * like original code
752 */
753#define PERF_CPU_HRTIMER (1000 / HZ)
754/*
755 * function must be called with interrupts disbled
756 */
757static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
758{
759 struct perf_cpu_context *cpuctx;
760 enum hrtimer_restart ret = HRTIMER_NORESTART;
761 int rotations = 0;
762
763 WARN_ON(!irqs_disabled());
764
765 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
766
767 rotations = perf_rotate_context(cpuctx);
768
769 /*
770 * arm timer if needed
771 */
772 if (rotations) {
773 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
774 ret = HRTIMER_RESTART;
775 }
776
777 return ret;
778}
779
780/* CPU is going down */
781void perf_cpu_hrtimer_cancel(int cpu)
782{
783 struct perf_cpu_context *cpuctx;
784 struct pmu *pmu;
785 unsigned long flags;
786
787 if (WARN_ON(cpu != smp_processor_id()))
788 return;
789
790 local_irq_save(flags);
791
792 rcu_read_lock();
793
794 list_for_each_entry_rcu(pmu, &pmus, entry) {
795 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
796
797 if (pmu->task_ctx_nr == perf_sw_context)
798 continue;
799
800 hrtimer_cancel(&cpuctx->hrtimer);
801 }
802
803 rcu_read_unlock();
804
805 local_irq_restore(flags);
806}
807
808static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
809{
810 struct hrtimer *hr = &cpuctx->hrtimer;
811 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200812 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200813
814 /* no multiplexing needed for SW PMU */
815 if (pmu->task_ctx_nr == perf_sw_context)
816 return;
817
Stephane Eranian62b85632013-04-03 14:21:34 +0200818 /*
819 * check default is sane, if not set then force to
820 * default interval (1/tick)
821 */
822 timer = pmu->hrtimer_interval_ms;
823 if (timer < 1)
824 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
825
826 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200827
828 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
829 hr->function = perf_cpu_hrtimer_handler;
830}
831
832static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
833{
834 struct hrtimer *hr = &cpuctx->hrtimer;
835 struct pmu *pmu = cpuctx->ctx.pmu;
836
837 /* not for SW PMU */
838 if (pmu->task_ctx_nr == perf_sw_context)
839 return;
840
841 if (hrtimer_active(hr))
842 return;
843
844 if (!hrtimer_callback_running(hr))
845 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
846 0, HRTIMER_MODE_REL_PINNED, 0);
847}
848
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200849void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200850{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200851 int *count = this_cpu_ptr(pmu->pmu_disable_count);
852 if (!(*count)++)
853 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200854}
855
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200856void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200857{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200858 int *count = this_cpu_ptr(pmu->pmu_disable_count);
859 if (!--(*count))
860 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200861}
862
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200863static DEFINE_PER_CPU(struct list_head, rotation_list);
864
865/*
866 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
867 * because they're strictly cpu affine and rotate_start is called with IRQs
868 * disabled, while rotate_context is called from IRQ context.
869 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200870static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200871{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200872 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200873 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200874
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200875 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200876
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200877 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200878 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200879}
880
881static void get_ctx(struct perf_event_context *ctx)
882{
883 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
884}
885
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200886static void put_ctx(struct perf_event_context *ctx)
887{
888 if (atomic_dec_and_test(&ctx->refcount)) {
889 if (ctx->parent_ctx)
890 put_ctx(ctx->parent_ctx);
891 if (ctx->task)
892 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800893 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894 }
895}
896
897static void unclone_ctx(struct perf_event_context *ctx)
898{
899 if (ctx->parent_ctx) {
900 put_ctx(ctx->parent_ctx);
901 ctx->parent_ctx = NULL;
902 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200903 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200904}
905
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200906static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
907{
908 /*
909 * only top level events have the pid namespace they were created in
910 */
911 if (event->parent)
912 event = event->parent;
913
914 return task_tgid_nr_ns(p, event->ns);
915}
916
917static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
918{
919 /*
920 * only top level events have the pid namespace they were created in
921 */
922 if (event->parent)
923 event = event->parent;
924
925 return task_pid_nr_ns(p, event->ns);
926}
927
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200928/*
929 * If we inherit events we want to return the parent event id
930 * to userspace.
931 */
932static u64 primary_event_id(struct perf_event *event)
933{
934 u64 id = event->id;
935
936 if (event->parent)
937 id = event->parent->id;
938
939 return id;
940}
941
942/*
943 * Get the perf_event_context for a task and lock it.
944 * This has to cope with with the fact that until it is locked,
945 * the context could get moved to another task.
946 */
947static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200948perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200949{
950 struct perf_event_context *ctx;
951
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200952retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200953 /*
954 * One of the few rules of preemptible RCU is that one cannot do
955 * rcu_read_unlock() while holding a scheduler (or nested) lock when
956 * part of the read side critical section was preemptible -- see
957 * rcu_read_unlock_special().
958 *
959 * Since ctx->lock nests under rq->lock we must ensure the entire read
960 * side critical section is non-preemptible.
961 */
962 preempt_disable();
963 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200964 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 if (ctx) {
966 /*
967 * If this context is a clone of another, it might
968 * get swapped for another underneath us by
969 * perf_event_task_sched_out, though the
970 * rcu_read_lock() protects us from any context
971 * getting freed. Lock the context and check if it
972 * got swapped before we could get the lock, and retry
973 * if so. If we locked the right context, then it
974 * can't get swapped on us any more.
975 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100976 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200977 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100978 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200979 rcu_read_unlock();
980 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981 goto retry;
982 }
983
984 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100985 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200986 ctx = NULL;
987 }
988 }
989 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200990 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200991 return ctx;
992}
993
994/*
995 * Get the context for a task and increment its pin_count so it
996 * can't get swapped to another task. This also increments its
997 * reference count so that the context can't get freed.
998 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200999static struct perf_event_context *
1000perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001001{
1002 struct perf_event_context *ctx;
1003 unsigned long flags;
1004
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001005 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001006 if (ctx) {
1007 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001008 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009 }
1010 return ctx;
1011}
1012
1013static void perf_unpin_context(struct perf_event_context *ctx)
1014{
1015 unsigned long flags;
1016
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001017 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001019 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020}
1021
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001022/*
1023 * Update the record of the current time in a context.
1024 */
1025static void update_context_time(struct perf_event_context *ctx)
1026{
1027 u64 now = perf_clock();
1028
1029 ctx->time += now - ctx->timestamp;
1030 ctx->timestamp = now;
1031}
1032
Stephane Eranian41587552011-01-03 18:20:01 +02001033static u64 perf_event_time(struct perf_event *event)
1034{
1035 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001036
1037 if (is_cgroup_event(event))
1038 return perf_cgroup_event_time(event);
1039
Stephane Eranian41587552011-01-03 18:20:01 +02001040 return ctx ? ctx->time : 0;
1041}
1042
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001043/*
1044 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001045 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001046 */
1047static void update_event_times(struct perf_event *event)
1048{
1049 struct perf_event_context *ctx = event->ctx;
1050 u64 run_end;
1051
1052 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1053 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1054 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001055 /*
1056 * in cgroup mode, time_enabled represents
1057 * the time the event was enabled AND active
1058 * tasks were in the monitored cgroup. This is
1059 * independent of the activity of the context as
1060 * there may be a mix of cgroup and non-cgroup events.
1061 *
1062 * That is why we treat cgroup events differently
1063 * here.
1064 */
1065 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001066 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001067 else if (ctx->is_active)
1068 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001069 else
1070 run_end = event->tstamp_stopped;
1071
1072 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001073
1074 if (event->state == PERF_EVENT_STATE_INACTIVE)
1075 run_end = event->tstamp_stopped;
1076 else
Stephane Eranian41587552011-01-03 18:20:01 +02001077 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001078
1079 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001080
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001081}
1082
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001083/*
1084 * Update total_time_enabled and total_time_running for all events in a group.
1085 */
1086static void update_group_times(struct perf_event *leader)
1087{
1088 struct perf_event *event;
1089
1090 update_event_times(leader);
1091 list_for_each_entry(event, &leader->sibling_list, group_entry)
1092 update_event_times(event);
1093}
1094
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001095static struct list_head *
1096ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1097{
1098 if (event->attr.pinned)
1099 return &ctx->pinned_groups;
1100 else
1101 return &ctx->flexible_groups;
1102}
1103
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104/*
1105 * Add a event from the lists for its context.
1106 * Must be called with ctx->mutex and ctx->lock held.
1107 */
1108static void
1109list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1110{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001111 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1112 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001113
1114 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001115 * If we're a stand alone event or group leader, we go to the context
1116 * list, group events are kept attached to the group so that
1117 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001119 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001120 struct list_head *list;
1121
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001122 if (is_software_event(event))
1123 event->group_flags |= PERF_GROUP_SOFTWARE;
1124
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001125 list = ctx_group_list(event, ctx);
1126 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127 }
1128
Peter Zijlstra08309372011-03-03 11:31:20 +01001129 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001130 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001131
Stephane Eraniand010b332012-02-09 23:21:00 +01001132 if (has_branch_stack(event))
1133 ctx->nr_branch_stack++;
1134
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001136 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001137 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001138 ctx->nr_events++;
1139 if (event->attr.inherit_stat)
1140 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001141
1142 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143}
1144
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001145/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001146 * Initialize event state based on the perf_event_attr::disabled.
1147 */
1148static inline void perf_event__state_init(struct perf_event *event)
1149{
1150 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1151 PERF_EVENT_STATE_INACTIVE;
1152}
1153
1154/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001155 * Called at perf_event creation and when events are attached/detached from a
1156 * group.
1157 */
1158static void perf_event__read_size(struct perf_event *event)
1159{
1160 int entry = sizeof(u64); /* value */
1161 int size = 0;
1162 int nr = 1;
1163
1164 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1165 size += sizeof(u64);
1166
1167 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1168 size += sizeof(u64);
1169
1170 if (event->attr.read_format & PERF_FORMAT_ID)
1171 entry += sizeof(u64);
1172
1173 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1174 nr += event->group_leader->nr_siblings;
1175 size += sizeof(u64);
1176 }
1177
1178 size += entry * nr;
1179 event->read_size = size;
1180}
1181
1182static void perf_event__header_size(struct perf_event *event)
1183{
1184 struct perf_sample_data *data;
1185 u64 sample_type = event->attr.sample_type;
1186 u16 size = 0;
1187
1188 perf_event__read_size(event);
1189
1190 if (sample_type & PERF_SAMPLE_IP)
1191 size += sizeof(data->ip);
1192
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001193 if (sample_type & PERF_SAMPLE_ADDR)
1194 size += sizeof(data->addr);
1195
1196 if (sample_type & PERF_SAMPLE_PERIOD)
1197 size += sizeof(data->period);
1198
Andi Kleenc3feedf2013-01-24 16:10:28 +01001199 if (sample_type & PERF_SAMPLE_WEIGHT)
1200 size += sizeof(data->weight);
1201
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001202 if (sample_type & PERF_SAMPLE_READ)
1203 size += event->read_size;
1204
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001205 if (sample_type & PERF_SAMPLE_DATA_SRC)
1206 size += sizeof(data->data_src.val);
1207
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001208 if (sample_type & PERF_SAMPLE_TRANSACTION)
1209 size += sizeof(data->txn);
1210
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001211 event->header_size = size;
1212}
1213
1214static void perf_event__id_header_size(struct perf_event *event)
1215{
1216 struct perf_sample_data *data;
1217 u64 sample_type = event->attr.sample_type;
1218 u16 size = 0;
1219
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001220 if (sample_type & PERF_SAMPLE_TID)
1221 size += sizeof(data->tid_entry);
1222
1223 if (sample_type & PERF_SAMPLE_TIME)
1224 size += sizeof(data->time);
1225
Adrian Hunterff3d5272013-08-27 11:23:07 +03001226 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1227 size += sizeof(data->id);
1228
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001229 if (sample_type & PERF_SAMPLE_ID)
1230 size += sizeof(data->id);
1231
1232 if (sample_type & PERF_SAMPLE_STREAM_ID)
1233 size += sizeof(data->stream_id);
1234
1235 if (sample_type & PERF_SAMPLE_CPU)
1236 size += sizeof(data->cpu_entry);
1237
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001238 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001239}
1240
Peter Zijlstra8a495422010-05-27 15:47:49 +02001241static void perf_group_attach(struct perf_event *event)
1242{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001243 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001244
Peter Zijlstra74c33372010-10-15 11:40:29 +02001245 /*
1246 * We can have double attach due to group movement in perf_event_open.
1247 */
1248 if (event->attach_state & PERF_ATTACH_GROUP)
1249 return;
1250
Peter Zijlstra8a495422010-05-27 15:47:49 +02001251 event->attach_state |= PERF_ATTACH_GROUP;
1252
1253 if (group_leader == event)
1254 return;
1255
1256 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1257 !is_software_event(event))
1258 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1259
1260 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1261 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001262
1263 perf_event__header_size(group_leader);
1264
1265 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1266 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001267}
1268
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269/*
1270 * Remove a event from the lists for its context.
1271 * Must be called with ctx->mutex and ctx->lock held.
1272 */
1273static void
1274list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1275{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001276 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001277 /*
1278 * We can have double detach due to exit/hot-unplug + close.
1279 */
1280 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001281 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001282
1283 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1284
Stephane Eranian68cacd22011-03-23 16:03:06 +01001285 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001286 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001287 cpuctx = __get_cpu_context(ctx);
1288 /*
1289 * if there are no more cgroup events
1290 * then cler cgrp to avoid stale pointer
1291 * in update_cgrp_time_from_cpuctx()
1292 */
1293 if (!ctx->nr_cgroups)
1294 cpuctx->cgrp = NULL;
1295 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001296
Stephane Eraniand010b332012-02-09 23:21:00 +01001297 if (has_branch_stack(event))
1298 ctx->nr_branch_stack--;
1299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001300 ctx->nr_events--;
1301 if (event->attr.inherit_stat)
1302 ctx->nr_stat--;
1303
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001304 list_del_rcu(&event->event_entry);
1305
Peter Zijlstra8a495422010-05-27 15:47:49 +02001306 if (event->group_leader == event)
1307 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001309 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001310
1311 /*
1312 * If event was in error state, then keep it
1313 * that way, otherwise bogus counts will be
1314 * returned on read(). The only way to get out
1315 * of error state is by explicit re-enabling
1316 * of the event
1317 */
1318 if (event->state > PERF_EVENT_STATE_OFF)
1319 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001320
1321 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001322}
1323
Peter Zijlstra8a495422010-05-27 15:47:49 +02001324static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001325{
1326 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001327 struct list_head *list = NULL;
1328
1329 /*
1330 * We can have double detach due to exit/hot-unplug + close.
1331 */
1332 if (!(event->attach_state & PERF_ATTACH_GROUP))
1333 return;
1334
1335 event->attach_state &= ~PERF_ATTACH_GROUP;
1336
1337 /*
1338 * If this is a sibling, remove it from its group.
1339 */
1340 if (event->group_leader != event) {
1341 list_del_init(&event->group_entry);
1342 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001343 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001344 }
1345
1346 if (!list_empty(&event->group_entry))
1347 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001349 /*
1350 * If this was a group event with sibling events then
1351 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001352 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353 */
1354 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001355 if (list)
1356 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001357 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001358
1359 /* Inherit group flags from the previous leader */
1360 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001362
1363out:
1364 perf_event__header_size(event->group_leader);
1365
1366 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1367 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001368}
1369
Stephane Eranianfa66f072010-08-26 16:40:01 +02001370static inline int
1371event_filter_match(struct perf_event *event)
1372{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001373 return (event->cpu == -1 || event->cpu == smp_processor_id())
1374 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001375}
1376
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001377static void
1378event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 struct perf_cpu_context *cpuctx,
1380 struct perf_event_context *ctx)
1381{
Stephane Eranian41587552011-01-03 18:20:01 +02001382 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001383 u64 delta;
1384 /*
1385 * An event which could not be activated because of
1386 * filter mismatch still needs to have its timings
1387 * maintained, otherwise bogus information is return
1388 * via read() for time_enabled, time_running:
1389 */
1390 if (event->state == PERF_EVENT_STATE_INACTIVE
1391 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001392 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001393 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001394 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001395 }
1396
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001397 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001398 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001399
Alexander Shishkin44377272013-12-16 14:17:36 +02001400 perf_pmu_disable(event->pmu);
1401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001402 event->state = PERF_EVENT_STATE_INACTIVE;
1403 if (event->pending_disable) {
1404 event->pending_disable = 0;
1405 event->state = PERF_EVENT_STATE_OFF;
1406 }
Stephane Eranian41587552011-01-03 18:20:01 +02001407 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001408 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409 event->oncpu = -1;
1410
1411 if (!is_software_event(event))
1412 cpuctx->active_oncpu--;
1413 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001414 if (event->attr.freq && event->attr.sample_freq)
1415 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416 if (event->attr.exclusive || !cpuctx->active_oncpu)
1417 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001418
1419 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001420}
1421
1422static void
1423group_sched_out(struct perf_event *group_event,
1424 struct perf_cpu_context *cpuctx,
1425 struct perf_event_context *ctx)
1426{
1427 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001428 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001429
1430 event_sched_out(group_event, cpuctx, ctx);
1431
1432 /*
1433 * Schedule out siblings (if any):
1434 */
1435 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1436 event_sched_out(event, cpuctx, ctx);
1437
Stephane Eranianfa66f072010-08-26 16:40:01 +02001438 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439 cpuctx->exclusive = 0;
1440}
1441
1442/*
1443 * Cross CPU call to remove a performance event
1444 *
1445 * We disable the event on the hardware level first. After that we
1446 * remove it from the context list.
1447 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001448static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001449{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001450 struct perf_event *event = info;
1451 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001452 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001454 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001456 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001457 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1458 ctx->is_active = 0;
1459 cpuctx->task_ctx = NULL;
1460 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001461 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001462
1463 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464}
1465
1466
1467/*
1468 * Remove the event from a task's (or a CPU's) list of events.
1469 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470 * CPU events are removed with a smp call. For task events we only
1471 * call when the task is on a CPU.
1472 *
1473 * If event->ctx is a cloned context, callers must make sure that
1474 * every task struct that event->ctx->task could possibly point to
1475 * remains valid. This is OK when called from perf_release since
1476 * that only calls us on the top-level context, which can't be a clone.
1477 * When called from perf_event_exit_task, it's OK because the
1478 * context has been detached from its task.
1479 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001480static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481{
1482 struct perf_event_context *ctx = event->ctx;
1483 struct task_struct *task = ctx->task;
1484
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001485 lockdep_assert_held(&ctx->mutex);
1486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487 if (!task) {
1488 /*
1489 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001490 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001491 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001492 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493 return;
1494 }
1495
1496retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001497 if (!task_function_call(task, __perf_remove_from_context, event))
1498 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001499
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001500 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001501 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001502 * If we failed to find a running task, but find the context active now
1503 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001505 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001506 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507 goto retry;
1508 }
1509
1510 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001511 * Since the task isn't running, its safe to remove the event, us
1512 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001514 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001515 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516}
1517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 * Cross CPU call to disable a performance event
1520 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301521int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001522{
1523 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001524 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001525 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001526
1527 /*
1528 * If this is a per-task event, need to check whether this
1529 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001530 *
1531 * Can trigger due to concurrent perf_event_context_sched_out()
1532 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533 */
1534 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001535 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001537 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538
1539 /*
1540 * If the event is on, turn it off.
1541 * If it is in error state, leave it in error state.
1542 */
1543 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1544 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001545 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546 update_group_times(event);
1547 if (event == event->group_leader)
1548 group_sched_out(event, cpuctx, ctx);
1549 else
1550 event_sched_out(event, cpuctx, ctx);
1551 event->state = PERF_EVENT_STATE_OFF;
1552 }
1553
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001554 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001555
1556 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557}
1558
1559/*
1560 * Disable a event.
1561 *
1562 * If event->ctx is a cloned context, callers must make sure that
1563 * every task struct that event->ctx->task could possibly point to
1564 * remains valid. This condition is satisifed when called through
1565 * perf_event_for_each_child or perf_event_for_each because they
1566 * hold the top-level event's child_mutex, so any descendant that
1567 * goes to exit will block in sync_child_event.
1568 * When called from perf_pending_event it's OK because event->ctx
1569 * is the current context on this CPU and preemption is disabled,
1570 * hence we can't get into perf_event_task_sched_out for this context.
1571 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001572void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573{
1574 struct perf_event_context *ctx = event->ctx;
1575 struct task_struct *task = ctx->task;
1576
1577 if (!task) {
1578 /*
1579 * Disable the event on the cpu that it's on
1580 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001581 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 return;
1583 }
1584
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001585retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001586 if (!task_function_call(task, __perf_event_disable, event))
1587 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001589 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590 /*
1591 * If the event is still active, we need to retry the cross-call.
1592 */
1593 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001594 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001595 /*
1596 * Reload the task pointer, it might have been changed by
1597 * a concurrent perf_event_context_sched_out().
1598 */
1599 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001600 goto retry;
1601 }
1602
1603 /*
1604 * Since we have the lock this context can't be scheduled
1605 * in, so we can change the state safely.
1606 */
1607 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1608 update_group_times(event);
1609 event->state = PERF_EVENT_STATE_OFF;
1610 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001611 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001613EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614
Stephane Eraniane5d13672011-02-14 11:20:01 +02001615static void perf_set_shadow_time(struct perf_event *event,
1616 struct perf_event_context *ctx,
1617 u64 tstamp)
1618{
1619 /*
1620 * use the correct time source for the time snapshot
1621 *
1622 * We could get by without this by leveraging the
1623 * fact that to get to this function, the caller
1624 * has most likely already called update_context_time()
1625 * and update_cgrp_time_xx() and thus both timestamp
1626 * are identical (or very close). Given that tstamp is,
1627 * already adjusted for cgroup, we could say that:
1628 * tstamp - ctx->timestamp
1629 * is equivalent to
1630 * tstamp - cgrp->timestamp.
1631 *
1632 * Then, in perf_output_read(), the calculation would
1633 * work with no changes because:
1634 * - event is guaranteed scheduled in
1635 * - no scheduled out in between
1636 * - thus the timestamp would be the same
1637 *
1638 * But this is a bit hairy.
1639 *
1640 * So instead, we have an explicit cgroup call to remain
1641 * within the time time source all along. We believe it
1642 * is cleaner and simpler to understand.
1643 */
1644 if (is_cgroup_event(event))
1645 perf_cgroup_set_shadow_time(event, tstamp);
1646 else
1647 event->shadow_ctx_time = tstamp - ctx->timestamp;
1648}
1649
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001650#define MAX_INTERRUPTS (~0ULL)
1651
1652static void perf_log_throttle(struct perf_event *event, int enable);
1653
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001655event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001657 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658{
Stephane Eranian41587552011-01-03 18:20:01 +02001659 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001660 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001661
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 if (event->state <= PERF_EVENT_STATE_OFF)
1663 return 0;
1664
1665 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001666 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001667
1668 /*
1669 * Unthrottle events, since we scheduled we might have missed several
1670 * ticks already, also for a heavily scheduling task there is little
1671 * guarantee it'll get a tick in a timely manner.
1672 */
1673 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1674 perf_log_throttle(event, 1);
1675 event->hw.interrupts = 0;
1676 }
1677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 /*
1679 * The new state must be visible before we turn it on in the hardware:
1680 */
1681 smp_wmb();
1682
Alexander Shishkin44377272013-12-16 14:17:36 +02001683 perf_pmu_disable(event->pmu);
1684
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001685 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686 event->state = PERF_EVENT_STATE_INACTIVE;
1687 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001688 ret = -EAGAIN;
1689 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690 }
1691
Stephane Eranian41587552011-01-03 18:20:01 +02001692 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001693
Stephane Eraniane5d13672011-02-14 11:20:01 +02001694 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001695
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696 if (!is_software_event(event))
1697 cpuctx->active_oncpu++;
1698 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001699 if (event->attr.freq && event->attr.sample_freq)
1700 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701
1702 if (event->attr.exclusive)
1703 cpuctx->exclusive = 1;
1704
Alexander Shishkin44377272013-12-16 14:17:36 +02001705out:
1706 perf_pmu_enable(event->pmu);
1707
1708 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709}
1710
1711static int
1712group_sched_in(struct perf_event *group_event,
1713 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001714 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715{
Lin Ming6bde9b62010-04-23 13:56:00 +08001716 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001717 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001718 u64 now = ctx->time;
1719 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720
1721 if (group_event->state == PERF_EVENT_STATE_OFF)
1722 return 0;
1723
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001724 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001725
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001726 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001727 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001728 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001730 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731
1732 /*
1733 * Schedule in siblings as one group (if any):
1734 */
1735 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001736 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737 partial_group = event;
1738 goto group_error;
1739 }
1740 }
1741
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001742 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001743 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001744
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745group_error:
1746 /*
1747 * Groups can be scheduled in as one unit only, so undo any
1748 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001749 * The events up to the failed event are scheduled out normally,
1750 * tstamp_stopped will be updated.
1751 *
1752 * The failed events and the remaining siblings need to have
1753 * their timings updated as if they had gone thru event_sched_in()
1754 * and event_sched_out(). This is required to get consistent timings
1755 * across the group. This also takes care of the case where the group
1756 * could never be scheduled by ensuring tstamp_stopped is set to mark
1757 * the time the event was actually stopped, such that time delta
1758 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001759 */
1760 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1761 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001762 simulate = true;
1763
1764 if (simulate) {
1765 event->tstamp_running += now - event->tstamp_stopped;
1766 event->tstamp_stopped = now;
1767 } else {
1768 event_sched_out(event, cpuctx, ctx);
1769 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001771 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001773 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001774
Stephane Eranian9e630202013-04-03 14:21:33 +02001775 perf_cpu_hrtimer_restart(cpuctx);
1776
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777 return -EAGAIN;
1778}
1779
1780/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001781 * Work out whether we can put this event group on the CPU now.
1782 */
1783static int group_can_go_on(struct perf_event *event,
1784 struct perf_cpu_context *cpuctx,
1785 int can_add_hw)
1786{
1787 /*
1788 * Groups consisting entirely of software events can always go on.
1789 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001790 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791 return 1;
1792 /*
1793 * If an exclusive group is already on, no other hardware
1794 * events can go on.
1795 */
1796 if (cpuctx->exclusive)
1797 return 0;
1798 /*
1799 * If this group is exclusive and there are already
1800 * events on the CPU, it can't go on.
1801 */
1802 if (event->attr.exclusive && cpuctx->active_oncpu)
1803 return 0;
1804 /*
1805 * Otherwise, try to add it if all previous groups were able
1806 * to go on.
1807 */
1808 return can_add_hw;
1809}
1810
1811static void add_event_to_ctx(struct perf_event *event,
1812 struct perf_event_context *ctx)
1813{
Stephane Eranian41587552011-01-03 18:20:01 +02001814 u64 tstamp = perf_event_time(event);
1815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001816 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001817 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001818 event->tstamp_enabled = tstamp;
1819 event->tstamp_running = tstamp;
1820 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821}
1822
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001823static void task_ctx_sched_out(struct perf_event_context *ctx);
1824static void
1825ctx_sched_in(struct perf_event_context *ctx,
1826 struct perf_cpu_context *cpuctx,
1827 enum event_type_t event_type,
1828 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001829
Peter Zijlstradce58552011-04-09 21:17:46 +02001830static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1831 struct perf_event_context *ctx,
1832 struct task_struct *task)
1833{
1834 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1835 if (ctx)
1836 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1837 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1838 if (ctx)
1839 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1840}
1841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842/*
1843 * Cross CPU call to install and enable a performance event
1844 *
1845 * Must be called with ctx->mutex held
1846 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001847static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849 struct perf_event *event = info;
1850 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001851 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001852 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1853 struct task_struct *task = current;
1854
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001855 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001856 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001857
1858 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001859 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001861 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001862 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001863
1864 /*
1865 * If the context we're installing events in is not the
1866 * active task_ctx, flip them.
1867 */
1868 if (ctx->task && task_ctx != ctx) {
1869 if (task_ctx)
1870 raw_spin_unlock(&task_ctx->lock);
1871 raw_spin_lock(&ctx->lock);
1872 task_ctx = ctx;
1873 }
1874
1875 if (task_ctx) {
1876 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001877 task = task_ctx->task;
1878 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001879
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001880 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001881
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001882 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001883 /*
1884 * update cgrp time only if current cgrp
1885 * matches event->cgrp. Must be done before
1886 * calling add_event_to_ctx()
1887 */
1888 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890 add_event_to_ctx(event, ctx);
1891
1892 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001893 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001895 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001897 perf_pmu_enable(cpuctx->ctx.pmu);
1898 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001899
1900 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901}
1902
1903/*
1904 * Attach a performance event to a context
1905 *
1906 * First we add the event to the list with the hardware enable bit
1907 * in event->hw_config cleared.
1908 *
1909 * If the event is attached to a task which is on a CPU we use a smp
1910 * call to enable it in the task context. The task might have been
1911 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001912 */
1913static void
1914perf_install_in_context(struct perf_event_context *ctx,
1915 struct perf_event *event,
1916 int cpu)
1917{
1918 struct task_struct *task = ctx->task;
1919
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001920 lockdep_assert_held(&ctx->mutex);
1921
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001922 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001923 if (event->cpu != -1)
1924 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 if (!task) {
1927 /*
1928 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001929 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001931 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932 return;
1933 }
1934
1935retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001936 if (!task_function_call(task, __perf_install_in_context, event))
1937 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001939 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001941 * If we failed to find a running task, but find the context active now
1942 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001944 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001945 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946 goto retry;
1947 }
1948
1949 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001950 * Since the task isn't running, its safe to add the event, us holding
1951 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001953 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001954 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001955}
1956
1957/*
1958 * Put a event into inactive state and update time fields.
1959 * Enabling the leader of a group effectively enables all
1960 * the group members that aren't explicitly disabled, so we
1961 * have to update their ->tstamp_enabled also.
1962 * Note: this works for group members as well as group leaders
1963 * since the non-leader members' sibling_lists will be empty.
1964 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001965static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966{
1967 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001968 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001969
1970 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001971 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001972 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001973 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1974 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001975 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976}
1977
1978/*
1979 * Cross CPU call to enable a performance event
1980 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001981static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982{
1983 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984 struct perf_event_context *ctx = event->ctx;
1985 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001986 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001987 int err;
1988
Jiri Olsa06f41792013-07-09 17:44:11 +02001989 /*
1990 * There's a time window between 'ctx->is_active' check
1991 * in perf_event_enable function and this place having:
1992 * - IRQs on
1993 * - ctx->lock unlocked
1994 *
1995 * where the task could be killed and 'ctx' deactivated
1996 * by perf_event_exit_task.
1997 */
1998 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001999 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002001 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 update_context_time(ctx);
2003
2004 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2005 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002006
2007 /*
2008 * set current task's cgroup time reference point
2009 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002010 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002011
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002012 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013
Stephane Eraniane5d13672011-02-14 11:20:01 +02002014 if (!event_filter_match(event)) {
2015 if (is_cgroup_event(event))
2016 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002017 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002018 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 /*
2021 * If the event is in a group and isn't the group leader,
2022 * then don't put it on unless the group is on.
2023 */
2024 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2025 goto unlock;
2026
2027 if (!group_can_go_on(event, cpuctx, 1)) {
2028 err = -EEXIST;
2029 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002031 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002033 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 }
2035
2036 if (err) {
2037 /*
2038 * If this event can't go on and it's part of a
2039 * group, then the whole group has to come off.
2040 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002041 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002043 perf_cpu_hrtimer_restart(cpuctx);
2044 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 if (leader->attr.pinned) {
2046 update_group_times(leader);
2047 leader->state = PERF_EVENT_STATE_ERROR;
2048 }
2049 }
2050
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002051unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002052 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002053
2054 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055}
2056
2057/*
2058 * Enable a event.
2059 *
2060 * If event->ctx is a cloned context, callers must make sure that
2061 * every task struct that event->ctx->task could possibly point to
2062 * remains valid. This condition is satisfied when called through
2063 * perf_event_for_each_child or perf_event_for_each as described
2064 * for perf_event_disable.
2065 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002066void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002067{
2068 struct perf_event_context *ctx = event->ctx;
2069 struct task_struct *task = ctx->task;
2070
2071 if (!task) {
2072 /*
2073 * Enable the event on the cpu that it's on
2074 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002075 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002076 return;
2077 }
2078
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002079 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2081 goto out;
2082
2083 /*
2084 * If the event is in error state, clear that first.
2085 * That way, if we see the event in error state below, we
2086 * know that it has gone back into error state, as distinct
2087 * from the task having been scheduled away before the
2088 * cross-call arrived.
2089 */
2090 if (event->state == PERF_EVENT_STATE_ERROR)
2091 event->state = PERF_EVENT_STATE_OFF;
2092
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002093retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002094 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002095 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002096 goto out;
2097 }
2098
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002099 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002100
2101 if (!task_function_call(task, __perf_event_enable, event))
2102 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002104 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105
2106 /*
2107 * If the context is active and the event is still off,
2108 * we need to retry the cross-call.
2109 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002110 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2111 /*
2112 * task could have been flipped by a concurrent
2113 * perf_event_context_sched_out()
2114 */
2115 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002117 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002119out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002120 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002122EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123
Avi Kivity26ca5c12011-06-29 18:42:37 +03002124int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125{
2126 /*
2127 * not supported on inherited events
2128 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002129 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002130 return -EINVAL;
2131
2132 atomic_add(refresh, &event->event_limit);
2133 perf_event_enable(event);
2134
2135 return 0;
2136}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002137EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002139static void ctx_sched_out(struct perf_event_context *ctx,
2140 struct perf_cpu_context *cpuctx,
2141 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142{
2143 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002144 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145
Peter Zijlstradb24d332011-04-09 21:17:45 +02002146 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002148 return;
2149
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002151 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002152 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002153 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002154
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002155 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002156 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002157 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2158 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002159 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002160
Peter Zijlstradb24d332011-04-09 21:17:45 +02002161 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002162 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002163 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002164 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002165 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166}
2167
2168/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002169 * Test whether two contexts are equivalent, i.e. whether they have both been
2170 * cloned from the same version of the same context.
2171 *
2172 * Equivalence is measured using a generation number in the context that is
2173 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2174 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175 */
2176static int context_equiv(struct perf_event_context *ctx1,
2177 struct perf_event_context *ctx2)
2178{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002179 /* Pinning disables the swap optimization */
2180 if (ctx1->pin_count || ctx2->pin_count)
2181 return 0;
2182
2183 /* If ctx1 is the parent of ctx2 */
2184 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2185 return 1;
2186
2187 /* If ctx2 is the parent of ctx1 */
2188 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2189 return 1;
2190
2191 /*
2192 * If ctx1 and ctx2 have the same parent; we flatten the parent
2193 * hierarchy, see perf_event_init_context().
2194 */
2195 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2196 ctx1->parent_gen == ctx2->parent_gen)
2197 return 1;
2198
2199 /* Unmatched */
2200 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002201}
2202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203static void __perf_event_sync_stat(struct perf_event *event,
2204 struct perf_event *next_event)
2205{
2206 u64 value;
2207
2208 if (!event->attr.inherit_stat)
2209 return;
2210
2211 /*
2212 * Update the event value, we cannot use perf_event_read()
2213 * because we're in the middle of a context switch and have IRQs
2214 * disabled, which upsets smp_call_function_single(), however
2215 * we know the event must be on the current CPU, therefore we
2216 * don't need to use it.
2217 */
2218 switch (event->state) {
2219 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002220 event->pmu->read(event);
2221 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002222
2223 case PERF_EVENT_STATE_INACTIVE:
2224 update_event_times(event);
2225 break;
2226
2227 default:
2228 break;
2229 }
2230
2231 /*
2232 * In order to keep per-task stats reliable we need to flip the event
2233 * values when we flip the contexts.
2234 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002235 value = local64_read(&next_event->count);
2236 value = local64_xchg(&event->count, value);
2237 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238
2239 swap(event->total_time_enabled, next_event->total_time_enabled);
2240 swap(event->total_time_running, next_event->total_time_running);
2241
2242 /*
2243 * Since we swizzled the values, update the user visible data too.
2244 */
2245 perf_event_update_userpage(event);
2246 perf_event_update_userpage(next_event);
2247}
2248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249static void perf_event_sync_stat(struct perf_event_context *ctx,
2250 struct perf_event_context *next_ctx)
2251{
2252 struct perf_event *event, *next_event;
2253
2254 if (!ctx->nr_stat)
2255 return;
2256
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002257 update_context_time(ctx);
2258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259 event = list_first_entry(&ctx->event_list,
2260 struct perf_event, event_entry);
2261
2262 next_event = list_first_entry(&next_ctx->event_list,
2263 struct perf_event, event_entry);
2264
2265 while (&event->event_entry != &ctx->event_list &&
2266 &next_event->event_entry != &next_ctx->event_list) {
2267
2268 __perf_event_sync_stat(event, next_event);
2269
2270 event = list_next_entry(event, event_entry);
2271 next_event = list_next_entry(next_event, event_entry);
2272 }
2273}
2274
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002275static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2276 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002277{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002278 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002280 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002281 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002282 int do_switch = 1;
2283
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002284 if (likely(!ctx))
2285 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002287 cpuctx = __get_cpu_context(ctx);
2288 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289 return;
2290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002292 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002293 if (!next_ctx)
2294 goto unlock;
2295
2296 parent = rcu_dereference(ctx->parent_ctx);
2297 next_parent = rcu_dereference(next_ctx->parent_ctx);
2298
2299 /* If neither context have a parent context; they cannot be clones. */
2300 if (!parent && !next_parent)
2301 goto unlock;
2302
2303 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304 /*
2305 * Looks like the two contexts are clones, so we might be
2306 * able to optimize the context switch. We lock both
2307 * contexts and check that they are clones under the
2308 * lock (including re-checking that neither has been
2309 * uncloned in the meantime). It doesn't matter which
2310 * order we take the locks because no other cpu could
2311 * be trying to lock both of these tasks.
2312 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002313 raw_spin_lock(&ctx->lock);
2314 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315 if (context_equiv(ctx, next_ctx)) {
2316 /*
2317 * XXX do we need a memory barrier of sorts
2318 * wrt to rcu_dereference() of perf_event_ctxp
2319 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002320 task->perf_event_ctxp[ctxn] = next_ctx;
2321 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002322 ctx->task = next;
2323 next_ctx->task = task;
2324 do_switch = 0;
2325
2326 perf_event_sync_stat(ctx, next_ctx);
2327 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002328 raw_spin_unlock(&next_ctx->lock);
2329 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002331unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002332 rcu_read_unlock();
2333
2334 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002335 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002336 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002338 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002339 }
2340}
2341
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002342#define for_each_task_context_nr(ctxn) \
2343 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2344
2345/*
2346 * Called from scheduler to remove the events of the current task,
2347 * with interrupts disabled.
2348 *
2349 * We stop each event and update the event value in event->count.
2350 *
2351 * This does not protect us against NMI, but disable()
2352 * sets the disabled bit in the control field of event _before_
2353 * accessing the event control register. If a NMI hits, then it will
2354 * not restart the event.
2355 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002356void __perf_event_task_sched_out(struct task_struct *task,
2357 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002358{
2359 int ctxn;
2360
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002361 for_each_task_context_nr(ctxn)
2362 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002363
2364 /*
2365 * if cgroup events exist on this CPU, then we need
2366 * to check if we have to switch out PMU state.
2367 * cgroup event are system-wide mode only
2368 */
2369 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002370 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002371}
2372
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002373static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002375 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002376
2377 if (!cpuctx->task_ctx)
2378 return;
2379
2380 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2381 return;
2382
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002383 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384 cpuctx->task_ctx = NULL;
2385}
2386
2387/*
2388 * Called with IRQs disabled
2389 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002390static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2391 enum event_type_t event_type)
2392{
2393 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394}
2395
2396static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002397ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002398 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399{
2400 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002402 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2403 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002405 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406 continue;
2407
Stephane Eraniane5d13672011-02-14 11:20:01 +02002408 /* may need to reset tstamp_enabled */
2409 if (is_cgroup_event(event))
2410 perf_cgroup_mark_enabled(event, ctx);
2411
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002412 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002413 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414
2415 /*
2416 * If this pinned group hasn't been scheduled,
2417 * put it in error state.
2418 */
2419 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2420 update_group_times(event);
2421 event->state = PERF_EVENT_STATE_ERROR;
2422 }
2423 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002424}
2425
2426static void
2427ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002428 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002429{
2430 struct perf_event *event;
2431 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002432
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002433 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2434 /* Ignore events in OFF or ERROR state */
2435 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437 /*
2438 * Listen to the 'cpu' scheduling filter constraint
2439 * of events:
2440 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002441 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002442 continue;
2443
Stephane Eraniane5d13672011-02-14 11:20:01 +02002444 /* may need to reset tstamp_enabled */
2445 if (is_cgroup_event(event))
2446 perf_cgroup_mark_enabled(event, ctx);
2447
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002448 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002449 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002451 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002453}
2454
2455static void
2456ctx_sched_in(struct perf_event_context *ctx,
2457 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002458 enum event_type_t event_type,
2459 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002460{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002461 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002462 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002463
Peter Zijlstradb24d332011-04-09 21:17:45 +02002464 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002465 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002466 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002467
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 now = perf_clock();
2469 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002470 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002471 /*
2472 * First go through the list and put on any pinned groups
2473 * in order to give them the best chance of going on.
2474 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002475 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002476 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002477
2478 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002479 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002480 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481}
2482
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002483static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002484 enum event_type_t event_type,
2485 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002486{
2487 struct perf_event_context *ctx = &cpuctx->ctx;
2488
Stephane Eraniane5d13672011-02-14 11:20:01 +02002489 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002490}
2491
Stephane Eraniane5d13672011-02-14 11:20:01 +02002492static void perf_event_context_sched_in(struct perf_event_context *ctx,
2493 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002495 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002496
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002497 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002498 if (cpuctx->task_ctx == ctx)
2499 return;
2500
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002501 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002502 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002503 /*
2504 * We want to keep the following priority order:
2505 * cpu pinned (that don't need to move), task pinned,
2506 * cpu flexible, task flexible.
2507 */
2508 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2509
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002510 if (ctx->nr_events)
2511 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002512
Gleb Natapov86b47c22011-11-22 16:08:21 +02002513 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2514
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002515 perf_pmu_enable(ctx->pmu);
2516 perf_ctx_unlock(cpuctx, ctx);
2517
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002518 /*
2519 * Since these rotations are per-cpu, we need to ensure the
2520 * cpu-context we got scheduled on is actually rotating.
2521 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002522 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523}
2524
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002525/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002526 * When sampling the branck stack in system-wide, it may be necessary
2527 * to flush the stack on context switch. This happens when the branch
2528 * stack does not tag its entries with the pid of the current task.
2529 * Otherwise it becomes impossible to associate a branch entry with a
2530 * task. This ambiguity is more likely to appear when the branch stack
2531 * supports priv level filtering and the user sets it to monitor only
2532 * at the user level (which could be a useful measurement in system-wide
2533 * mode). In that case, the risk is high of having a branch stack with
2534 * branch from multiple tasks. Flushing may mean dropping the existing
2535 * entries or stashing them somewhere in the PMU specific code layer.
2536 *
2537 * This function provides the context switch callback to the lower code
2538 * layer. It is invoked ONLY when there is at least one system-wide context
2539 * with at least one active event using taken branch sampling.
2540 */
2541static void perf_branch_stack_sched_in(struct task_struct *prev,
2542 struct task_struct *task)
2543{
2544 struct perf_cpu_context *cpuctx;
2545 struct pmu *pmu;
2546 unsigned long flags;
2547
2548 /* no need to flush branch stack if not changing task */
2549 if (prev == task)
2550 return;
2551
2552 local_irq_save(flags);
2553
2554 rcu_read_lock();
2555
2556 list_for_each_entry_rcu(pmu, &pmus, entry) {
2557 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2558
2559 /*
2560 * check if the context has at least one
2561 * event using PERF_SAMPLE_BRANCH_STACK
2562 */
2563 if (cpuctx->ctx.nr_branch_stack > 0
2564 && pmu->flush_branch_stack) {
2565
2566 pmu = cpuctx->ctx.pmu;
2567
2568 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2569
2570 perf_pmu_disable(pmu);
2571
2572 pmu->flush_branch_stack();
2573
2574 perf_pmu_enable(pmu);
2575
2576 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2577 }
2578 }
2579
2580 rcu_read_unlock();
2581
2582 local_irq_restore(flags);
2583}
2584
2585/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002586 * Called from scheduler to add the events of the current task
2587 * with interrupts disabled.
2588 *
2589 * We restore the event value and then enable it.
2590 *
2591 * This does not protect us against NMI, but enable()
2592 * sets the enabled bit in the control field of event _before_
2593 * accessing the event control register. If a NMI hits, then it will
2594 * keep the event running.
2595 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002596void __perf_event_task_sched_in(struct task_struct *prev,
2597 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002598{
2599 struct perf_event_context *ctx;
2600 int ctxn;
2601
2602 for_each_task_context_nr(ctxn) {
2603 ctx = task->perf_event_ctxp[ctxn];
2604 if (likely(!ctx))
2605 continue;
2606
Stephane Eraniane5d13672011-02-14 11:20:01 +02002607 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002608 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002609 /*
2610 * if cgroup events exist on this CPU, then we need
2611 * to check if we have to switch in PMU state.
2612 * cgroup event are system-wide mode only
2613 */
2614 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002615 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002616
2617 /* check for system-wide branch_stack events */
2618 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2619 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002620}
2621
Peter Zijlstraabd50712010-01-26 18:50:16 +01002622static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2623{
2624 u64 frequency = event->attr.sample_freq;
2625 u64 sec = NSEC_PER_SEC;
2626 u64 divisor, dividend;
2627
2628 int count_fls, nsec_fls, frequency_fls, sec_fls;
2629
2630 count_fls = fls64(count);
2631 nsec_fls = fls64(nsec);
2632 frequency_fls = fls64(frequency);
2633 sec_fls = 30;
2634
2635 /*
2636 * We got @count in @nsec, with a target of sample_freq HZ
2637 * the target period becomes:
2638 *
2639 * @count * 10^9
2640 * period = -------------------
2641 * @nsec * sample_freq
2642 *
2643 */
2644
2645 /*
2646 * Reduce accuracy by one bit such that @a and @b converge
2647 * to a similar magnitude.
2648 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002649#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002650do { \
2651 if (a##_fls > b##_fls) { \
2652 a >>= 1; \
2653 a##_fls--; \
2654 } else { \
2655 b >>= 1; \
2656 b##_fls--; \
2657 } \
2658} while (0)
2659
2660 /*
2661 * Reduce accuracy until either term fits in a u64, then proceed with
2662 * the other, so that finally we can do a u64/u64 division.
2663 */
2664 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2665 REDUCE_FLS(nsec, frequency);
2666 REDUCE_FLS(sec, count);
2667 }
2668
2669 if (count_fls + sec_fls > 64) {
2670 divisor = nsec * frequency;
2671
2672 while (count_fls + sec_fls > 64) {
2673 REDUCE_FLS(count, sec);
2674 divisor >>= 1;
2675 }
2676
2677 dividend = count * sec;
2678 } else {
2679 dividend = count * sec;
2680
2681 while (nsec_fls + frequency_fls > 64) {
2682 REDUCE_FLS(nsec, frequency);
2683 dividend >>= 1;
2684 }
2685
2686 divisor = nsec * frequency;
2687 }
2688
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002689 if (!divisor)
2690 return dividend;
2691
Peter Zijlstraabd50712010-01-26 18:50:16 +01002692 return div64_u64(dividend, divisor);
2693}
2694
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002695static DEFINE_PER_CPU(int, perf_throttled_count);
2696static DEFINE_PER_CPU(u64, perf_throttled_seq);
2697
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002698static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002699{
2700 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002701 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702 s64 delta;
2703
Peter Zijlstraabd50712010-01-26 18:50:16 +01002704 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002705
2706 delta = (s64)(period - hwc->sample_period);
2707 delta = (delta + 7) / 8; /* low pass filter */
2708
2709 sample_period = hwc->sample_period + delta;
2710
2711 if (!sample_period)
2712 sample_period = 1;
2713
2714 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002715
Peter Zijlstrae7850592010-05-21 14:43:08 +02002716 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002717 if (disable)
2718 event->pmu->stop(event, PERF_EF_UPDATE);
2719
Peter Zijlstrae7850592010-05-21 14:43:08 +02002720 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002721
2722 if (disable)
2723 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002724 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725}
2726
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002727/*
2728 * combine freq adjustment with unthrottling to avoid two passes over the
2729 * events. At the same time, make sure, having freq events does not change
2730 * the rate of unthrottling as that would introduce bias.
2731 */
2732static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2733 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734{
2735 struct perf_event *event;
2736 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002737 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002738 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002740 /*
2741 * only need to iterate over all events iff:
2742 * - context have events in frequency mode (needs freq adjust)
2743 * - there are events to unthrottle on this cpu
2744 */
2745 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002746 return;
2747
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002748 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002749 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002750
Paul Mackerras03541f82009-10-14 16:58:03 +11002751 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002752 if (event->state != PERF_EVENT_STATE_ACTIVE)
2753 continue;
2754
Stephane Eranian5632ab12011-01-03 18:20:01 +02002755 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002756 continue;
2757
Alexander Shishkin44377272013-12-16 14:17:36 +02002758 perf_pmu_disable(event->pmu);
2759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 hwc = &event->hw;
2761
Jiri Olsaae23bff2013-08-24 16:45:54 +02002762 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002763 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002765 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766 }
2767
2768 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002769 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002771 /*
2772 * stop the event and update event->count
2773 */
2774 event->pmu->stop(event, PERF_EF_UPDATE);
2775
Peter Zijlstrae7850592010-05-21 14:43:08 +02002776 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002777 delta = now - hwc->freq_count_stamp;
2778 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002779
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002780 /*
2781 * restart the event
2782 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002783 * we have stopped the event so tell that
2784 * to perf_adjust_period() to avoid stopping it
2785 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002786 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002787 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002788 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002789
2790 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002791 next:
2792 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002794
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002795 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002796 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002797}
2798
2799/*
2800 * Round-robin a context's events:
2801 */
2802static void rotate_ctx(struct perf_event_context *ctx)
2803{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002804 /*
2805 * Rotate the first entry last of non-pinned groups. Rotation might be
2806 * disabled by the inheritance code.
2807 */
2808 if (!ctx->rotate_disable)
2809 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810}
2811
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002812/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002813 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2814 * because they're strictly cpu affine and rotate_start is called with IRQs
2815 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002816 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002817static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002818{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002819 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002820 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002822 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002823 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002824 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2825 rotate = 1;
2826 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002828 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002829 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002830 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002831 if (ctx->nr_events != ctx->nr_active)
2832 rotate = 1;
2833 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002834
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002835 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002836 goto done;
2837
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002838 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002839 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002840
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002841 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2842 if (ctx)
2843 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002844
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002845 rotate_ctx(&cpuctx->ctx);
2846 if (ctx)
2847 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002848
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002849 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002850
2851 perf_pmu_enable(cpuctx->ctx.pmu);
2852 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002853done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002854 if (remove)
2855 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002856
2857 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002858}
2859
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002860#ifdef CONFIG_NO_HZ_FULL
2861bool perf_event_can_stop_tick(void)
2862{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002863 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002864 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002865 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002866 else
2867 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002868}
2869#endif
2870
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002871void perf_event_task_tick(void)
2872{
2873 struct list_head *head = &__get_cpu_var(rotation_list);
2874 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002875 struct perf_event_context *ctx;
2876 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002877
2878 WARN_ON(!irqs_disabled());
2879
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002880 __this_cpu_inc(perf_throttled_seq);
2881 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2882
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002883 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002884 ctx = &cpuctx->ctx;
2885 perf_adjust_freq_unthr_context(ctx, throttled);
2886
2887 ctx = cpuctx->task_ctx;
2888 if (ctx)
2889 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002890 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891}
2892
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002893static int event_enable_on_exec(struct perf_event *event,
2894 struct perf_event_context *ctx)
2895{
2896 if (!event->attr.enable_on_exec)
2897 return 0;
2898
2899 event->attr.enable_on_exec = 0;
2900 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2901 return 0;
2902
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002903 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002904
2905 return 1;
2906}
2907
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002908/*
2909 * Enable all of a task's events that have been marked enable-on-exec.
2910 * This expects task == current.
2911 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002912static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914 struct perf_event *event;
2915 unsigned long flags;
2916 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002917 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918
2919 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002920 if (!ctx || !ctx->nr_events)
2921 goto out;
2922
Stephane Eraniane566b762011-04-06 02:54:54 +02002923 /*
2924 * We must ctxsw out cgroup events to avoid conflict
2925 * when invoking perf_task_event_sched_in() later on
2926 * in this function. Otherwise we end up trying to
2927 * ctxswin cgroup events which are already scheduled
2928 * in.
2929 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002930 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002931
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002932 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002933 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002935 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002936 ret = event_enable_on_exec(event, ctx);
2937 if (ret)
2938 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002939 }
2940
2941 /*
2942 * Unclone this context if we enabled any event.
2943 */
2944 if (enabled)
2945 unclone_ctx(ctx);
2946
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002947 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948
Stephane Eraniane566b762011-04-06 02:54:54 +02002949 /*
2950 * Also calls ctxswin for cgroup events, if any:
2951 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002952 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002953out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002954 local_irq_restore(flags);
2955}
2956
2957/*
2958 * Cross CPU call to read the hardware event
2959 */
2960static void __perf_event_read(void *info)
2961{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002962 struct perf_event *event = info;
2963 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002964 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965
2966 /*
2967 * If this is a task context, we need to check whether it is
2968 * the current task context of this cpu. If not it has been
2969 * scheduled out before the smp call arrived. In that case
2970 * event->count would have been updated to a recent sample
2971 * when the event was scheduled out.
2972 */
2973 if (ctx->task && cpuctx->task_ctx != ctx)
2974 return;
2975
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002976 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002977 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002978 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002979 update_cgrp_time_from_event(event);
2980 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002981 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002982 if (event->state == PERF_EVENT_STATE_ACTIVE)
2983 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002984 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985}
2986
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002987static inline u64 perf_event_count(struct perf_event *event)
2988{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002989 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002990}
2991
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992static u64 perf_event_read(struct perf_event *event)
2993{
2994 /*
2995 * If event is enabled and currently active on a CPU, update the
2996 * value in the event structure:
2997 */
2998 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2999 smp_call_function_single(event->oncpu,
3000 __perf_event_read, event, 1);
3001 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003002 struct perf_event_context *ctx = event->ctx;
3003 unsigned long flags;
3004
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003005 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003006 /*
3007 * may read while context is not active
3008 * (e.g., thread is blocked), in that case
3009 * we cannot update context time
3010 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003011 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003012 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003013 update_cgrp_time_from_event(event);
3014 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003016 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017 }
3018
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003019 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003020}
3021
3022/*
3023 * Initialize the perf_event context in a task_struct:
3024 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003025static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003027 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003028 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003029 INIT_LIST_HEAD(&ctx->pinned_groups);
3030 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031 INIT_LIST_HEAD(&ctx->event_list);
3032 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003033}
3034
Peter Zijlstraeb184472010-09-07 15:55:13 +02003035static struct perf_event_context *
3036alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037{
3038 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003039
3040 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3041 if (!ctx)
3042 return NULL;
3043
3044 __perf_event_init_context(ctx);
3045 if (task) {
3046 ctx->task = task;
3047 get_task_struct(task);
3048 }
3049 ctx->pmu = pmu;
3050
3051 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003052}
3053
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003054static struct task_struct *
3055find_lively_task_by_vpid(pid_t vpid)
3056{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003058 int err;
3059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003061 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062 task = current;
3063 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003064 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003065 if (task)
3066 get_task_struct(task);
3067 rcu_read_unlock();
3068
3069 if (!task)
3070 return ERR_PTR(-ESRCH);
3071
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072 /* Reuse ptrace permission checks for now. */
3073 err = -EACCES;
3074 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3075 goto errout;
3076
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003077 return task;
3078errout:
3079 put_task_struct(task);
3080 return ERR_PTR(err);
3081
3082}
3083
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003084/*
3085 * Returns a matching context with refcount and pincount.
3086 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003087static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003088find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003089{
3090 struct perf_event_context *ctx;
3091 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003092 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003093 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003094
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003095 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096 /* Must be root to operate on a CPU event: */
3097 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3098 return ERR_PTR(-EACCES);
3099
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003100 /*
3101 * We could be clever and allow to attach a event to an
3102 * offline CPU and activate it when the CPU comes up, but
3103 * that's for later.
3104 */
3105 if (!cpu_online(cpu))
3106 return ERR_PTR(-ENODEV);
3107
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003108 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109 ctx = &cpuctx->ctx;
3110 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003111 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
3113 return ctx;
3114 }
3115
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003116 err = -EINVAL;
3117 ctxn = pmu->task_ctx_nr;
3118 if (ctxn < 0)
3119 goto errout;
3120
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003121retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003122 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003123 if (ctx) {
3124 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003125 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003126 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003127 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003128 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129 err = -ENOMEM;
3130 if (!ctx)
3131 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003132
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003133 err = 0;
3134 mutex_lock(&task->perf_event_mutex);
3135 /*
3136 * If it has already passed perf_event_exit_task().
3137 * we must see PF_EXITING, it takes this mutex too.
3138 */
3139 if (task->flags & PF_EXITING)
3140 err = -ESRCH;
3141 else if (task->perf_event_ctxp[ctxn])
3142 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003143 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003144 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003145 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003146 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003147 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003148 mutex_unlock(&task->perf_event_mutex);
3149
3150 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003151 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003152
3153 if (err == -EAGAIN)
3154 goto retry;
3155 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157 }
3158
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003159 return ctx;
3160
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003161errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003162 return ERR_PTR(err);
3163}
3164
Li Zefan6fb29152009-10-15 11:21:42 +08003165static void perf_event_free_filter(struct perf_event *event);
3166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167static void free_event_rcu(struct rcu_head *head)
3168{
3169 struct perf_event *event;
3170
3171 event = container_of(head, struct perf_event, rcu_head);
3172 if (event->ns)
3173 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003174 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175 kfree(event);
3176}
3177
Frederic Weisbecker76369132011-05-19 19:55:04 +02003178static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003179static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003180
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003181static void unaccount_event_cpu(struct perf_event *event, int cpu)
3182{
3183 if (event->parent)
3184 return;
3185
3186 if (has_branch_stack(event)) {
3187 if (!(event->attach_state & PERF_ATTACH_TASK))
3188 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3189 }
3190 if (is_cgroup_event(event))
3191 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3192}
3193
3194static void unaccount_event(struct perf_event *event)
3195{
3196 if (event->parent)
3197 return;
3198
3199 if (event->attach_state & PERF_ATTACH_TASK)
3200 static_key_slow_dec_deferred(&perf_sched_events);
3201 if (event->attr.mmap || event->attr.mmap_data)
3202 atomic_dec(&nr_mmap_events);
3203 if (event->attr.comm)
3204 atomic_dec(&nr_comm_events);
3205 if (event->attr.task)
3206 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003207 if (event->attr.freq)
3208 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003209 if (is_cgroup_event(event))
3210 static_key_slow_dec_deferred(&perf_sched_events);
3211 if (has_branch_stack(event))
3212 static_key_slow_dec_deferred(&perf_sched_events);
3213
3214 unaccount_event_cpu(event, event->cpu);
3215}
3216
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003217static void __free_event(struct perf_event *event)
3218{
3219 if (!event->parent) {
3220 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3221 put_callchain_buffers();
3222 }
3223
3224 if (event->destroy)
3225 event->destroy(event);
3226
3227 if (event->ctx)
3228 put_ctx(event->ctx);
3229
3230 call_rcu(&event->rcu_head, free_event_rcu);
3231}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232static void free_event(struct perf_event *event)
3233{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003234 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003235
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003236 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237
Frederic Weisbecker76369132011-05-19 19:55:04 +02003238 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003239 struct ring_buffer *rb;
3240
3241 /*
3242 * Can happen when we close an event with re-directed output.
3243 *
3244 * Since we have a 0 refcount, perf_mmap_close() will skip
3245 * over us; possibly making our ring_buffer_put() the last.
3246 */
3247 mutex_lock(&event->mmap_mutex);
3248 rb = event->rb;
3249 if (rb) {
3250 rcu_assign_pointer(event->rb, NULL);
3251 ring_buffer_detach(event, rb);
3252 ring_buffer_put(rb); /* could be last */
3253 }
3254 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003255 }
3256
Stephane Eraniane5d13672011-02-14 11:20:01 +02003257 if (is_cgroup_event(event))
3258 perf_detach_cgroup(event);
3259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003261 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003262}
3263
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003264int perf_event_release_kernel(struct perf_event *event)
3265{
3266 struct perf_event_context *ctx = event->ctx;
3267
3268 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02003269 /*
3270 * There are two ways this annotation is useful:
3271 *
3272 * 1) there is a lock recursion from perf_event_exit_task
3273 * see the comment there.
3274 *
3275 * 2) there is a lock-inversion with mmap_sem through
3276 * perf_event_read_group(), which takes faults while
3277 * holding ctx->mutex, however this is called after
3278 * the last filedesc died, so there is no possibility
3279 * to trigger the AB-BA case.
3280 */
3281 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003282 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02003283 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003284 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02003285 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003286 mutex_unlock(&ctx->mutex);
3287
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003288 free_event(event);
3289
3290 return 0;
3291}
3292EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3293
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003294/*
3295 * Called when the last reference to the file is gone.
3296 */
Al Viroa6fa9412012-08-20 14:59:25 +01003297static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003298{
Peter Zijlstra88821352010-11-09 19:01:43 +01003299 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003300
Al Viroa6fa9412012-08-20 14:59:25 +01003301 if (!atomic_long_dec_and_test(&event->refcount))
3302 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003303
Peter Zijlstra88821352010-11-09 19:01:43 +01003304 rcu_read_lock();
3305 owner = ACCESS_ONCE(event->owner);
3306 /*
3307 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3308 * !owner it means the list deletion is complete and we can indeed
3309 * free this event, otherwise we need to serialize on
3310 * owner->perf_event_mutex.
3311 */
3312 smp_read_barrier_depends();
3313 if (owner) {
3314 /*
3315 * Since delayed_put_task_struct() also drops the last
3316 * task reference we can safely take a new reference
3317 * while holding the rcu_read_lock().
3318 */
3319 get_task_struct(owner);
3320 }
3321 rcu_read_unlock();
3322
3323 if (owner) {
3324 mutex_lock(&owner->perf_event_mutex);
3325 /*
3326 * We have to re-check the event->owner field, if it is cleared
3327 * we raced with perf_event_exit_task(), acquiring the mutex
3328 * ensured they're done, and we can proceed with freeing the
3329 * event.
3330 */
3331 if (event->owner)
3332 list_del_init(&event->owner_entry);
3333 mutex_unlock(&owner->perf_event_mutex);
3334 put_task_struct(owner);
3335 }
3336
Al Viroa6fa9412012-08-20 14:59:25 +01003337 perf_event_release_kernel(event);
3338}
3339
3340static int perf_release(struct inode *inode, struct file *file)
3341{
3342 put_event(file->private_data);
3343 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003344}
3345
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003346u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347{
3348 struct perf_event *child;
3349 u64 total = 0;
3350
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003351 *enabled = 0;
3352 *running = 0;
3353
Peter Zijlstra6f105812009-11-20 22:19:56 +01003354 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003356 *enabled += event->total_time_enabled +
3357 atomic64_read(&event->child_total_time_enabled);
3358 *running += event->total_time_running +
3359 atomic64_read(&event->child_total_time_running);
3360
3361 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003363 *enabled += child->total_time_enabled;
3364 *running += child->total_time_running;
3365 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003366 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367
3368 return total;
3369}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003370EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372static int perf_event_read_group(struct perf_event *event,
3373 u64 read_format, char __user *buf)
3374{
3375 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003376 int n = 0, size = 0, ret = -EFAULT;
3377 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003378 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003379 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003380
Peter Zijlstra6f105812009-11-20 22:19:56 +01003381 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003382 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003383
3384 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003385 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3386 values[n++] = enabled;
3387 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3388 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003389 values[n++] = count;
3390 if (read_format & PERF_FORMAT_ID)
3391 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003392
3393 size = n * sizeof(u64);
3394
3395 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003396 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003397
Peter Zijlstra6f105812009-11-20 22:19:56 +01003398 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399
3400 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003401 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003403 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003404 if (read_format & PERF_FORMAT_ID)
3405 values[n++] = primary_event_id(sub);
3406
3407 size = n * sizeof(u64);
3408
Stephane Eranian184d3da2009-11-23 21:40:49 -08003409 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003410 ret = -EFAULT;
3411 goto unlock;
3412 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003413
3414 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003416unlock:
3417 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418
Peter Zijlstraabf48682009-11-20 22:19:49 +01003419 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420}
3421
3422static int perf_event_read_one(struct perf_event *event,
3423 u64 read_format, char __user *buf)
3424{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003425 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426 u64 values[4];
3427 int n = 0;
3428
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003429 values[n++] = perf_event_read_value(event, &enabled, &running);
3430 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3431 values[n++] = enabled;
3432 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3433 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434 if (read_format & PERF_FORMAT_ID)
3435 values[n++] = primary_event_id(event);
3436
3437 if (copy_to_user(buf, values, n * sizeof(u64)))
3438 return -EFAULT;
3439
3440 return n * sizeof(u64);
3441}
3442
3443/*
3444 * Read the performance event - simple non blocking version for now
3445 */
3446static ssize_t
3447perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3448{
3449 u64 read_format = event->attr.read_format;
3450 int ret;
3451
3452 /*
3453 * Return end-of-file for a read on a event that is in
3454 * error state (i.e. because it was pinned but it couldn't be
3455 * scheduled on to the CPU at some point).
3456 */
3457 if (event->state == PERF_EVENT_STATE_ERROR)
3458 return 0;
3459
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003460 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461 return -ENOSPC;
3462
3463 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 if (read_format & PERF_FORMAT_GROUP)
3465 ret = perf_event_read_group(event, read_format, buf);
3466 else
3467 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003468
3469 return ret;
3470}
3471
3472static ssize_t
3473perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3474{
3475 struct perf_event *event = file->private_data;
3476
3477 return perf_read_hw(event, buf, count);
3478}
3479
3480static unsigned int perf_poll(struct file *file, poll_table *wait)
3481{
3482 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003483 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003484 unsigned int events = POLL_HUP;
3485
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003486 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003487 * Pin the event->rb by taking event->mmap_mutex; otherwise
3488 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003489 */
3490 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003491 rb = event->rb;
3492 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003493 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003494 mutex_unlock(&event->mmap_mutex);
3495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496 poll_wait(file, &event->waitq, wait);
3497
3498 return events;
3499}
3500
3501static void perf_event_reset(struct perf_event *event)
3502{
3503 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003504 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003505 perf_event_update_userpage(event);
3506}
3507
3508/*
3509 * Holding the top-level event's child_mutex means that any
3510 * descendant process that has inherited this event will block
3511 * in sync_child_event if it goes to exit, thus satisfying the
3512 * task existence requirements of perf_event_enable/disable.
3513 */
3514static void perf_event_for_each_child(struct perf_event *event,
3515 void (*func)(struct perf_event *))
3516{
3517 struct perf_event *child;
3518
3519 WARN_ON_ONCE(event->ctx->parent_ctx);
3520 mutex_lock(&event->child_mutex);
3521 func(event);
3522 list_for_each_entry(child, &event->child_list, child_list)
3523 func(child);
3524 mutex_unlock(&event->child_mutex);
3525}
3526
3527static void perf_event_for_each(struct perf_event *event,
3528 void (*func)(struct perf_event *))
3529{
3530 struct perf_event_context *ctx = event->ctx;
3531 struct perf_event *sibling;
3532
3533 WARN_ON_ONCE(ctx->parent_ctx);
3534 mutex_lock(&ctx->mutex);
3535 event = event->group_leader;
3536
3537 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003538 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003539 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003540 mutex_unlock(&ctx->mutex);
3541}
3542
3543static int perf_event_period(struct perf_event *event, u64 __user *arg)
3544{
3545 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003546 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003547 u64 value;
3548
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003549 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 return -EINVAL;
3551
John Blackwoodad0cf342010-09-28 18:03:11 -04003552 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003553 return -EFAULT;
3554
3555 if (!value)
3556 return -EINVAL;
3557
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003558 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559 if (event->attr.freq) {
3560 if (value > sysctl_perf_event_sample_rate) {
3561 ret = -EINVAL;
3562 goto unlock;
3563 }
3564
3565 event->attr.sample_freq = value;
3566 } else {
3567 event->attr.sample_period = value;
3568 event->hw.sample_period = value;
3569 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003570
3571 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3572 if (active) {
3573 perf_pmu_disable(ctx->pmu);
3574 event->pmu->stop(event, PERF_EF_UPDATE);
3575 }
3576
3577 local64_set(&event->hw.period_left, 0);
3578
3579 if (active) {
3580 event->pmu->start(event, PERF_EF_RELOAD);
3581 perf_pmu_enable(ctx->pmu);
3582 }
3583
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003584unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003585 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003586
3587 return ret;
3588}
3589
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003590static const struct file_operations perf_fops;
3591
Al Viro2903ff02012-08-28 12:52:22 -04003592static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003593{
Al Viro2903ff02012-08-28 12:52:22 -04003594 struct fd f = fdget(fd);
3595 if (!f.file)
3596 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003597
Al Viro2903ff02012-08-28 12:52:22 -04003598 if (f.file->f_op != &perf_fops) {
3599 fdput(f);
3600 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003601 }
Al Viro2903ff02012-08-28 12:52:22 -04003602 *p = f;
3603 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003604}
3605
3606static int perf_event_set_output(struct perf_event *event,
3607 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003608static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003609
3610static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3611{
3612 struct perf_event *event = file->private_data;
3613 void (*func)(struct perf_event *);
3614 u32 flags = arg;
3615
3616 switch (cmd) {
3617 case PERF_EVENT_IOC_ENABLE:
3618 func = perf_event_enable;
3619 break;
3620 case PERF_EVENT_IOC_DISABLE:
3621 func = perf_event_disable;
3622 break;
3623 case PERF_EVENT_IOC_RESET:
3624 func = perf_event_reset;
3625 break;
3626
3627 case PERF_EVENT_IOC_REFRESH:
3628 return perf_event_refresh(event, arg);
3629
3630 case PERF_EVENT_IOC_PERIOD:
3631 return perf_event_period(event, (u64 __user *)arg);
3632
Jiri Olsacf4957f2012-10-24 13:37:58 +02003633 case PERF_EVENT_IOC_ID:
3634 {
3635 u64 id = primary_event_id(event);
3636
3637 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3638 return -EFAULT;
3639 return 0;
3640 }
3641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003642 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003643 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003644 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003645 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003646 struct perf_event *output_event;
3647 struct fd output;
3648 ret = perf_fget_light(arg, &output);
3649 if (ret)
3650 return ret;
3651 output_event = output.file->private_data;
3652 ret = perf_event_set_output(event, output_event);
3653 fdput(output);
3654 } else {
3655 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003656 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003657 return ret;
3658 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003659
Li Zefan6fb29152009-10-15 11:21:42 +08003660 case PERF_EVENT_IOC_SET_FILTER:
3661 return perf_event_set_filter(event, (void __user *)arg);
3662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003663 default:
3664 return -ENOTTY;
3665 }
3666
3667 if (flags & PERF_IOC_FLAG_GROUP)
3668 perf_event_for_each(event, func);
3669 else
3670 perf_event_for_each_child(event, func);
3671
3672 return 0;
3673}
3674
3675int perf_event_task_enable(void)
3676{
3677 struct perf_event *event;
3678
3679 mutex_lock(&current->perf_event_mutex);
3680 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3681 perf_event_for_each_child(event, perf_event_enable);
3682 mutex_unlock(&current->perf_event_mutex);
3683
3684 return 0;
3685}
3686
3687int perf_event_task_disable(void)
3688{
3689 struct perf_event *event;
3690
3691 mutex_lock(&current->perf_event_mutex);
3692 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3693 perf_event_for_each_child(event, perf_event_disable);
3694 mutex_unlock(&current->perf_event_mutex);
3695
3696 return 0;
3697}
3698
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699static int perf_event_index(struct perf_event *event)
3700{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003701 if (event->hw.state & PERF_HES_STOPPED)
3702 return 0;
3703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 if (event->state != PERF_EVENT_STATE_ACTIVE)
3705 return 0;
3706
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003707 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708}
3709
Eric B Munsonc4794292011-06-23 16:34:38 -04003710static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003711 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003712 u64 *enabled,
3713 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003714{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003715 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003716
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003717 *now = perf_clock();
3718 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003719 *enabled = ctx_time - event->tstamp_enabled;
3720 *running = ctx_time - event->tstamp_running;
3721}
3722
Peter Zijlstrafa731582013-09-19 10:16:42 +02003723static void perf_event_init_userpage(struct perf_event *event)
3724{
3725 struct perf_event_mmap_page *userpg;
3726 struct ring_buffer *rb;
3727
3728 rcu_read_lock();
3729 rb = rcu_dereference(event->rb);
3730 if (!rb)
3731 goto unlock;
3732
3733 userpg = rb->user_page;
3734
3735 /* Allow new userspace to detect that bit 0 is deprecated */
3736 userpg->cap_bit0_is_deprecated = 1;
3737 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3738
3739unlock:
3740 rcu_read_unlock();
3741}
3742
Peter Zijlstrac7206202012-03-22 17:26:36 +01003743void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003744{
3745}
3746
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003747/*
3748 * Callers need to ensure there can be no nesting of this function, otherwise
3749 * the seqlock logic goes bad. We can not serialize this because the arch
3750 * code calls this from NMI context.
3751 */
3752void perf_event_update_userpage(struct perf_event *event)
3753{
3754 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003755 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003756 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757
3758 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003759 rb = rcu_dereference(event->rb);
3760 if (!rb)
3761 goto unlock;
3762
Eric B Munson0d641202011-06-24 12:26:26 -04003763 /*
3764 * compute total_time_enabled, total_time_running
3765 * based on snapshot values taken when the event
3766 * was last scheduled in.
3767 *
3768 * we cannot simply called update_context_time()
3769 * because of locking issue as we can be called in
3770 * NMI context
3771 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003772 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773
Frederic Weisbecker76369132011-05-19 19:55:04 +02003774 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003775 /*
3776 * Disable preemption so as to not let the corresponding user-space
3777 * spin too long if we get preempted.
3778 */
3779 preempt_disable();
3780 ++userpg->lock;
3781 barrier();
3782 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003783 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003784 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003785 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786
Eric B Munson0d641202011-06-24 12:26:26 -04003787 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003788 atomic64_read(&event->child_total_time_enabled);
3789
Eric B Munson0d641202011-06-24 12:26:26 -04003790 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791 atomic64_read(&event->child_total_time_running);
3792
Peter Zijlstrac7206202012-03-22 17:26:36 +01003793 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003794
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003795 barrier();
3796 ++userpg->lock;
3797 preempt_enable();
3798unlock:
3799 rcu_read_unlock();
3800}
3801
Peter Zijlstra906010b2009-09-21 16:08:49 +02003802static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3803{
3804 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003805 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003806 int ret = VM_FAULT_SIGBUS;
3807
3808 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3809 if (vmf->pgoff == 0)
3810 ret = 0;
3811 return ret;
3812 }
3813
3814 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003815 rb = rcu_dereference(event->rb);
3816 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003817 goto unlock;
3818
3819 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3820 goto unlock;
3821
Frederic Weisbecker76369132011-05-19 19:55:04 +02003822 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003823 if (!vmf->page)
3824 goto unlock;
3825
3826 get_page(vmf->page);
3827 vmf->page->mapping = vma->vm_file->f_mapping;
3828 vmf->page->index = vmf->pgoff;
3829
3830 ret = 0;
3831unlock:
3832 rcu_read_unlock();
3833
3834 return ret;
3835}
3836
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003837static void ring_buffer_attach(struct perf_event *event,
3838 struct ring_buffer *rb)
3839{
3840 unsigned long flags;
3841
3842 if (!list_empty(&event->rb_entry))
3843 return;
3844
3845 spin_lock_irqsave(&rb->event_lock, flags);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003846 if (list_empty(&event->rb_entry))
3847 list_add(&event->rb_entry, &rb->event_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003848 spin_unlock_irqrestore(&rb->event_lock, flags);
3849}
3850
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003851static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003852{
3853 unsigned long flags;
3854
3855 if (list_empty(&event->rb_entry))
3856 return;
3857
3858 spin_lock_irqsave(&rb->event_lock, flags);
3859 list_del_init(&event->rb_entry);
3860 wake_up_all(&event->waitq);
3861 spin_unlock_irqrestore(&rb->event_lock, flags);
3862}
3863
3864static void ring_buffer_wakeup(struct perf_event *event)
3865{
3866 struct ring_buffer *rb;
3867
3868 rcu_read_lock();
3869 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003870 if (rb) {
3871 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3872 wake_up_all(&event->waitq);
3873 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003874 rcu_read_unlock();
3875}
3876
Frederic Weisbecker76369132011-05-19 19:55:04 +02003877static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003878{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003879 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003880
Frederic Weisbecker76369132011-05-19 19:55:04 +02003881 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3882 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003883}
3884
Frederic Weisbecker76369132011-05-19 19:55:04 +02003885static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003886{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003887 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003889 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003890 rb = rcu_dereference(event->rb);
3891 if (rb) {
3892 if (!atomic_inc_not_zero(&rb->refcount))
3893 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003894 }
3895 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003896
Frederic Weisbecker76369132011-05-19 19:55:04 +02003897 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003898}
3899
Frederic Weisbecker76369132011-05-19 19:55:04 +02003900static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003901{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003902 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003903 return;
3904
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003905 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003906
Frederic Weisbecker76369132011-05-19 19:55:04 +02003907 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003908}
3909
3910static void perf_mmap_open(struct vm_area_struct *vma)
3911{
3912 struct perf_event *event = vma->vm_file->private_data;
3913
3914 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003915 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916}
3917
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003918/*
3919 * A buffer can be mmap()ed multiple times; either directly through the same
3920 * event, or through other events by use of perf_event_set_output().
3921 *
3922 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3923 * the buffer here, where we still have a VM context. This means we need
3924 * to detach all events redirecting to us.
3925 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003926static void perf_mmap_close(struct vm_area_struct *vma)
3927{
3928 struct perf_event *event = vma->vm_file->private_data;
3929
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003930 struct ring_buffer *rb = event->rb;
3931 struct user_struct *mmap_user = rb->mmap_user;
3932 int mmap_locked = rb->mmap_locked;
3933 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003935 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003936
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003937 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3938 return;
3939
3940 /* Detach current event from the buffer. */
3941 rcu_assign_pointer(event->rb, NULL);
3942 ring_buffer_detach(event, rb);
3943 mutex_unlock(&event->mmap_mutex);
3944
3945 /* If there's still other mmap()s of this buffer, we're done. */
3946 if (atomic_read(&rb->mmap_count)) {
3947 ring_buffer_put(rb); /* can't be last */
3948 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949 }
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003950
3951 /*
3952 * No other mmap()s, detach from all other events that might redirect
3953 * into the now unreachable buffer. Somewhat complicated by the
3954 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3955 */
3956again:
3957 rcu_read_lock();
3958 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3959 if (!atomic_long_inc_not_zero(&event->refcount)) {
3960 /*
3961 * This event is en-route to free_event() which will
3962 * detach it and remove it from the list.
3963 */
3964 continue;
3965 }
3966 rcu_read_unlock();
3967
3968 mutex_lock(&event->mmap_mutex);
3969 /*
3970 * Check we didn't race with perf_event_set_output() which can
3971 * swizzle the rb from under us while we were waiting to
3972 * acquire mmap_mutex.
3973 *
3974 * If we find a different rb; ignore this event, a next
3975 * iteration will no longer find it on the list. We have to
3976 * still restart the iteration to make sure we're not now
3977 * iterating the wrong list.
3978 */
3979 if (event->rb == rb) {
3980 rcu_assign_pointer(event->rb, NULL);
3981 ring_buffer_detach(event, rb);
3982 ring_buffer_put(rb); /* can't be last, we still have one */
3983 }
3984 mutex_unlock(&event->mmap_mutex);
3985 put_event(event);
3986
3987 /*
3988 * Restart the iteration; either we're on the wrong list or
3989 * destroyed its integrity by doing a deletion.
3990 */
3991 goto again;
3992 }
3993 rcu_read_unlock();
3994
3995 /*
3996 * It could be there's still a few 0-ref events on the list; they'll
3997 * get cleaned up by free_event() -- they'll also still have their
3998 * ref on the rb and will free it whenever they are done with it.
3999 *
4000 * Aside from that, this buffer is 'fully' detached and unmapped,
4001 * undo the VM accounting.
4002 */
4003
4004 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4005 vma->vm_mm->pinned_vm -= mmap_locked;
4006 free_uid(mmap_user);
4007
4008 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004009}
4010
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004011static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012 .open = perf_mmap_open,
4013 .close = perf_mmap_close,
4014 .fault = perf_mmap_fault,
4015 .page_mkwrite = perf_mmap_fault,
4016};
4017
4018static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4019{
4020 struct perf_event *event = file->private_data;
4021 unsigned long user_locked, user_lock_limit;
4022 struct user_struct *user = current_user();
4023 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004024 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004025 unsigned long vma_size;
4026 unsigned long nr_pages;
4027 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004028 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004029
Peter Zijlstrac7920612010-05-18 10:33:24 +02004030 /*
4031 * Don't allow mmap() of inherited per-task counters. This would
4032 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004033 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004034 */
4035 if (event->cpu == -1 && event->attr.inherit)
4036 return -EINVAL;
4037
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004038 if (!(vma->vm_flags & VM_SHARED))
4039 return -EINVAL;
4040
4041 vma_size = vma->vm_end - vma->vm_start;
4042 nr_pages = (vma_size / PAGE_SIZE) - 1;
4043
4044 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004045 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004046 * can do bitmasks instead of modulo.
4047 */
4048 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4049 return -EINVAL;
4050
4051 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4052 return -EINVAL;
4053
4054 if (vma->vm_pgoff != 0)
4055 return -EINVAL;
4056
4057 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004058again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004060 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004061 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004062 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004063 goto unlock;
4064 }
4065
4066 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4067 /*
4068 * Raced against perf_mmap_close() through
4069 * perf_event_set_output(). Try again, hope for better
4070 * luck.
4071 */
4072 mutex_unlock(&event->mmap_mutex);
4073 goto again;
4074 }
4075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076 goto unlock;
4077 }
4078
4079 user_extra = nr_pages + 1;
4080 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4081
4082 /*
4083 * Increase the limit linearly with more CPUs:
4084 */
4085 user_lock_limit *= num_online_cpus();
4086
4087 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4088
4089 extra = 0;
4090 if (user_locked > user_lock_limit)
4091 extra = user_locked - user_lock_limit;
4092
Jiri Slaby78d7d402010-03-05 13:42:54 -08004093 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004095 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096
4097 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4098 !capable(CAP_IPC_LOCK)) {
4099 ret = -EPERM;
4100 goto unlock;
4101 }
4102
Frederic Weisbecker76369132011-05-19 19:55:04 +02004103 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004104
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004105 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004106 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004107
Vince Weaver4ec83632011-06-01 15:15:36 -04004108 rb = rb_alloc(nr_pages,
4109 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4110 event->cpu, flags);
4111
Frederic Weisbecker76369132011-05-19 19:55:04 +02004112 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004113 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004114 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004115 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004116
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004117 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004118 rb->mmap_locked = extra;
4119 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004121 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004122 vma->vm_mm->pinned_vm += extra;
4123
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004124 ring_buffer_attach(event, rb);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004125 rcu_assign_pointer(event->rb, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004126
Peter Zijlstrafa731582013-09-19 10:16:42 +02004127 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004128 perf_event_update_userpage(event);
4129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004130unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004131 if (!ret)
4132 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133 mutex_unlock(&event->mmap_mutex);
4134
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004135 /*
4136 * Since pinned accounting is per vm we cannot allow fork() to copy our
4137 * vma.
4138 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004139 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004140 vma->vm_ops = &perf_mmap_vmops;
4141
4142 return ret;
4143}
4144
4145static int perf_fasync(int fd, struct file *filp, int on)
4146{
Al Viro496ad9a2013-01-23 17:07:38 -05004147 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004148 struct perf_event *event = filp->private_data;
4149 int retval;
4150
4151 mutex_lock(&inode->i_mutex);
4152 retval = fasync_helper(fd, filp, on, &event->fasync);
4153 mutex_unlock(&inode->i_mutex);
4154
4155 if (retval < 0)
4156 return retval;
4157
4158 return 0;
4159}
4160
4161static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004162 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004163 .release = perf_release,
4164 .read = perf_read,
4165 .poll = perf_poll,
4166 .unlocked_ioctl = perf_ioctl,
4167 .compat_ioctl = perf_ioctl,
4168 .mmap = perf_mmap,
4169 .fasync = perf_fasync,
4170};
4171
4172/*
4173 * Perf event wakeup
4174 *
4175 * If there's data, ensure we set the poll() state and publish everything
4176 * to user-space before waking everybody up.
4177 */
4178
4179void perf_event_wakeup(struct perf_event *event)
4180{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004181 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182
4183 if (event->pending_kill) {
4184 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4185 event->pending_kill = 0;
4186 }
4187}
4188
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004189static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190{
4191 struct perf_event *event = container_of(entry,
4192 struct perf_event, pending);
4193
4194 if (event->pending_disable) {
4195 event->pending_disable = 0;
4196 __perf_event_disable(event);
4197 }
4198
4199 if (event->pending_wakeup) {
4200 event->pending_wakeup = 0;
4201 perf_event_wakeup(event);
4202 }
4203}
4204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004206 * We assume there is only KVM supporting the callbacks.
4207 * Later on, we might change it to a list if there is
4208 * another virtualization implementation supporting the callbacks.
4209 */
4210struct perf_guest_info_callbacks *perf_guest_cbs;
4211
4212int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4213{
4214 perf_guest_cbs = cbs;
4215 return 0;
4216}
4217EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4218
4219int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4220{
4221 perf_guest_cbs = NULL;
4222 return 0;
4223}
4224EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4225
Jiri Olsa40189942012-08-07 15:20:37 +02004226static void
4227perf_output_sample_regs(struct perf_output_handle *handle,
4228 struct pt_regs *regs, u64 mask)
4229{
4230 int bit;
4231
4232 for_each_set_bit(bit, (const unsigned long *) &mask,
4233 sizeof(mask) * BITS_PER_BYTE) {
4234 u64 val;
4235
4236 val = perf_reg_value(regs, bit);
4237 perf_output_put(handle, val);
4238 }
4239}
4240
4241static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4242 struct pt_regs *regs)
4243{
4244 if (!user_mode(regs)) {
4245 if (current->mm)
4246 regs = task_pt_regs(current);
4247 else
4248 regs = NULL;
4249 }
4250
4251 if (regs) {
4252 regs_user->regs = regs;
4253 regs_user->abi = perf_reg_abi(current);
4254 }
4255}
4256
Jiri Olsac5ebced2012-08-07 15:20:40 +02004257/*
4258 * Get remaining task size from user stack pointer.
4259 *
4260 * It'd be better to take stack vma map and limit this more
4261 * precisly, but there's no way to get it safely under interrupt,
4262 * so using TASK_SIZE as limit.
4263 */
4264static u64 perf_ustack_task_size(struct pt_regs *regs)
4265{
4266 unsigned long addr = perf_user_stack_pointer(regs);
4267
4268 if (!addr || addr >= TASK_SIZE)
4269 return 0;
4270
4271 return TASK_SIZE - addr;
4272}
4273
4274static u16
4275perf_sample_ustack_size(u16 stack_size, u16 header_size,
4276 struct pt_regs *regs)
4277{
4278 u64 task_size;
4279
4280 /* No regs, no stack pointer, no dump. */
4281 if (!regs)
4282 return 0;
4283
4284 /*
4285 * Check if we fit in with the requested stack size into the:
4286 * - TASK_SIZE
4287 * If we don't, we limit the size to the TASK_SIZE.
4288 *
4289 * - remaining sample size
4290 * If we don't, we customize the stack size to
4291 * fit in to the remaining sample size.
4292 */
4293
4294 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4295 stack_size = min(stack_size, (u16) task_size);
4296
4297 /* Current header size plus static size and dynamic size. */
4298 header_size += 2 * sizeof(u64);
4299
4300 /* Do we fit in with the current stack dump size? */
4301 if ((u16) (header_size + stack_size) < header_size) {
4302 /*
4303 * If we overflow the maximum size for the sample,
4304 * we customize the stack dump size to fit in.
4305 */
4306 stack_size = USHRT_MAX - header_size - sizeof(u64);
4307 stack_size = round_up(stack_size, sizeof(u64));
4308 }
4309
4310 return stack_size;
4311}
4312
4313static void
4314perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4315 struct pt_regs *regs)
4316{
4317 /* Case of a kernel thread, nothing to dump */
4318 if (!regs) {
4319 u64 size = 0;
4320 perf_output_put(handle, size);
4321 } else {
4322 unsigned long sp;
4323 unsigned int rem;
4324 u64 dyn_size;
4325
4326 /*
4327 * We dump:
4328 * static size
4329 * - the size requested by user or the best one we can fit
4330 * in to the sample max size
4331 * data
4332 * - user stack dump data
4333 * dynamic size
4334 * - the actual dumped size
4335 */
4336
4337 /* Static size. */
4338 perf_output_put(handle, dump_size);
4339
4340 /* Data. */
4341 sp = perf_user_stack_pointer(regs);
4342 rem = __output_copy_user(handle, (void *) sp, dump_size);
4343 dyn_size = dump_size - rem;
4344
4345 perf_output_skip(handle, rem);
4346
4347 /* Dynamic size. */
4348 perf_output_put(handle, dyn_size);
4349 }
4350}
4351
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004352static void __perf_event_header__init_id(struct perf_event_header *header,
4353 struct perf_sample_data *data,
4354 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004355{
4356 u64 sample_type = event->attr.sample_type;
4357
4358 data->type = sample_type;
4359 header->size += event->id_header_size;
4360
4361 if (sample_type & PERF_SAMPLE_TID) {
4362 /* namespace issues */
4363 data->tid_entry.pid = perf_event_pid(event, current);
4364 data->tid_entry.tid = perf_event_tid(event, current);
4365 }
4366
4367 if (sample_type & PERF_SAMPLE_TIME)
4368 data->time = perf_clock();
4369
Adrian Hunterff3d5272013-08-27 11:23:07 +03004370 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004371 data->id = primary_event_id(event);
4372
4373 if (sample_type & PERF_SAMPLE_STREAM_ID)
4374 data->stream_id = event->id;
4375
4376 if (sample_type & PERF_SAMPLE_CPU) {
4377 data->cpu_entry.cpu = raw_smp_processor_id();
4378 data->cpu_entry.reserved = 0;
4379 }
4380}
4381
Frederic Weisbecker76369132011-05-19 19:55:04 +02004382void perf_event_header__init_id(struct perf_event_header *header,
4383 struct perf_sample_data *data,
4384 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004385{
4386 if (event->attr.sample_id_all)
4387 __perf_event_header__init_id(header, data, event);
4388}
4389
4390static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4391 struct perf_sample_data *data)
4392{
4393 u64 sample_type = data->type;
4394
4395 if (sample_type & PERF_SAMPLE_TID)
4396 perf_output_put(handle, data->tid_entry);
4397
4398 if (sample_type & PERF_SAMPLE_TIME)
4399 perf_output_put(handle, data->time);
4400
4401 if (sample_type & PERF_SAMPLE_ID)
4402 perf_output_put(handle, data->id);
4403
4404 if (sample_type & PERF_SAMPLE_STREAM_ID)
4405 perf_output_put(handle, data->stream_id);
4406
4407 if (sample_type & PERF_SAMPLE_CPU)
4408 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004409
4410 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4411 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004412}
4413
Frederic Weisbecker76369132011-05-19 19:55:04 +02004414void perf_event__output_id_sample(struct perf_event *event,
4415 struct perf_output_handle *handle,
4416 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004417{
4418 if (event->attr.sample_id_all)
4419 __perf_event__output_id_sample(handle, sample);
4420}
4421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004423 struct perf_event *event,
4424 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425{
4426 u64 read_format = event->attr.read_format;
4427 u64 values[4];
4428 int n = 0;
4429
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004430 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004432 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433 atomic64_read(&event->child_total_time_enabled);
4434 }
4435 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004436 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437 atomic64_read(&event->child_total_time_running);
4438 }
4439 if (read_format & PERF_FORMAT_ID)
4440 values[n++] = primary_event_id(event);
4441
Frederic Weisbecker76369132011-05-19 19:55:04 +02004442 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443}
4444
4445/*
4446 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4447 */
4448static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004449 struct perf_event *event,
4450 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451{
4452 struct perf_event *leader = event->group_leader, *sub;
4453 u64 read_format = event->attr.read_format;
4454 u64 values[5];
4455 int n = 0;
4456
4457 values[n++] = 1 + leader->nr_siblings;
4458
4459 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004460 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004461
4462 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004463 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464
4465 if (leader != event)
4466 leader->pmu->read(leader);
4467
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004468 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004469 if (read_format & PERF_FORMAT_ID)
4470 values[n++] = primary_event_id(leader);
4471
Frederic Weisbecker76369132011-05-19 19:55:04 +02004472 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004473
4474 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4475 n = 0;
4476
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004477 if ((sub != event) &&
4478 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479 sub->pmu->read(sub);
4480
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004481 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482 if (read_format & PERF_FORMAT_ID)
4483 values[n++] = primary_event_id(sub);
4484
Frederic Weisbecker76369132011-05-19 19:55:04 +02004485 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004486 }
4487}
4488
Stephane Eranianeed01522010-10-26 16:08:01 +02004489#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4490 PERF_FORMAT_TOTAL_TIME_RUNNING)
4491
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492static void perf_output_read(struct perf_output_handle *handle,
4493 struct perf_event *event)
4494{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004495 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004496 u64 read_format = event->attr.read_format;
4497
4498 /*
4499 * compute total_time_enabled, total_time_running
4500 * based on snapshot values taken when the event
4501 * was last scheduled in.
4502 *
4503 * we cannot simply called update_context_time()
4504 * because of locking issue as we are called in
4505 * NMI context
4506 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004507 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004508 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004511 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004513 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004514}
4515
4516void perf_output_sample(struct perf_output_handle *handle,
4517 struct perf_event_header *header,
4518 struct perf_sample_data *data,
4519 struct perf_event *event)
4520{
4521 u64 sample_type = data->type;
4522
4523 perf_output_put(handle, *header);
4524
Adrian Hunterff3d5272013-08-27 11:23:07 +03004525 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4526 perf_output_put(handle, data->id);
4527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528 if (sample_type & PERF_SAMPLE_IP)
4529 perf_output_put(handle, data->ip);
4530
4531 if (sample_type & PERF_SAMPLE_TID)
4532 perf_output_put(handle, data->tid_entry);
4533
4534 if (sample_type & PERF_SAMPLE_TIME)
4535 perf_output_put(handle, data->time);
4536
4537 if (sample_type & PERF_SAMPLE_ADDR)
4538 perf_output_put(handle, data->addr);
4539
4540 if (sample_type & PERF_SAMPLE_ID)
4541 perf_output_put(handle, data->id);
4542
4543 if (sample_type & PERF_SAMPLE_STREAM_ID)
4544 perf_output_put(handle, data->stream_id);
4545
4546 if (sample_type & PERF_SAMPLE_CPU)
4547 perf_output_put(handle, data->cpu_entry);
4548
4549 if (sample_type & PERF_SAMPLE_PERIOD)
4550 perf_output_put(handle, data->period);
4551
4552 if (sample_type & PERF_SAMPLE_READ)
4553 perf_output_read(handle, event);
4554
4555 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4556 if (data->callchain) {
4557 int size = 1;
4558
4559 if (data->callchain)
4560 size += data->callchain->nr;
4561
4562 size *= sizeof(u64);
4563
Frederic Weisbecker76369132011-05-19 19:55:04 +02004564 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004565 } else {
4566 u64 nr = 0;
4567 perf_output_put(handle, nr);
4568 }
4569 }
4570
4571 if (sample_type & PERF_SAMPLE_RAW) {
4572 if (data->raw) {
4573 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004574 __output_copy(handle, data->raw->data,
4575 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004576 } else {
4577 struct {
4578 u32 size;
4579 u32 data;
4580 } raw = {
4581 .size = sizeof(u32),
4582 .data = 0,
4583 };
4584 perf_output_put(handle, raw);
4585 }
4586 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004587
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004588 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4589 if (data->br_stack) {
4590 size_t size;
4591
4592 size = data->br_stack->nr
4593 * sizeof(struct perf_branch_entry);
4594
4595 perf_output_put(handle, data->br_stack->nr);
4596 perf_output_copy(handle, data->br_stack->entries, size);
4597 } else {
4598 /*
4599 * we always store at least the value of nr
4600 */
4601 u64 nr = 0;
4602 perf_output_put(handle, nr);
4603 }
4604 }
Jiri Olsa40189942012-08-07 15:20:37 +02004605
4606 if (sample_type & PERF_SAMPLE_REGS_USER) {
4607 u64 abi = data->regs_user.abi;
4608
4609 /*
4610 * If there are no regs to dump, notice it through
4611 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4612 */
4613 perf_output_put(handle, abi);
4614
4615 if (abi) {
4616 u64 mask = event->attr.sample_regs_user;
4617 perf_output_sample_regs(handle,
4618 data->regs_user.regs,
4619 mask);
4620 }
4621 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004622
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004623 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004624 perf_output_sample_ustack(handle,
4625 data->stack_user_size,
4626 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004627 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004628
4629 if (sample_type & PERF_SAMPLE_WEIGHT)
4630 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004631
4632 if (sample_type & PERF_SAMPLE_DATA_SRC)
4633 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004634
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004635 if (sample_type & PERF_SAMPLE_TRANSACTION)
4636 perf_output_put(handle, data->txn);
4637
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004638 if (!event->attr.watermark) {
4639 int wakeup_events = event->attr.wakeup_events;
4640
4641 if (wakeup_events) {
4642 struct ring_buffer *rb = handle->rb;
4643 int events = local_inc_return(&rb->events);
4644
4645 if (events >= wakeup_events) {
4646 local_sub(wakeup_events, &rb->events);
4647 local_inc(&rb->wakeup);
4648 }
4649 }
4650 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651}
4652
4653void perf_prepare_sample(struct perf_event_header *header,
4654 struct perf_sample_data *data,
4655 struct perf_event *event,
4656 struct pt_regs *regs)
4657{
4658 u64 sample_type = event->attr.sample_type;
4659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004661 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004662
4663 header->misc = 0;
4664 header->misc |= perf_misc_flags(regs);
4665
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004666 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004667
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004668 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669 data->ip = perf_instruction_pointer(regs);
4670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4672 int size = 1;
4673
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004674 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004675
4676 if (data->callchain)
4677 size += data->callchain->nr;
4678
4679 header->size += size * sizeof(u64);
4680 }
4681
4682 if (sample_type & PERF_SAMPLE_RAW) {
4683 int size = sizeof(u32);
4684
4685 if (data->raw)
4686 size += data->raw->size;
4687 else
4688 size += sizeof(u32);
4689
4690 WARN_ON_ONCE(size & (sizeof(u64)-1));
4691 header->size += size;
4692 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004693
4694 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4695 int size = sizeof(u64); /* nr */
4696 if (data->br_stack) {
4697 size += data->br_stack->nr
4698 * sizeof(struct perf_branch_entry);
4699 }
4700 header->size += size;
4701 }
Jiri Olsa40189942012-08-07 15:20:37 +02004702
4703 if (sample_type & PERF_SAMPLE_REGS_USER) {
4704 /* regs dump ABI info */
4705 int size = sizeof(u64);
4706
4707 perf_sample_regs_user(&data->regs_user, regs);
4708
4709 if (data->regs_user.regs) {
4710 u64 mask = event->attr.sample_regs_user;
4711 size += hweight64(mask) * sizeof(u64);
4712 }
4713
4714 header->size += size;
4715 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004716
4717 if (sample_type & PERF_SAMPLE_STACK_USER) {
4718 /*
4719 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4720 * processed as the last one or have additional check added
4721 * in case new sample type is added, because we could eat
4722 * up the rest of the sample size.
4723 */
4724 struct perf_regs_user *uregs = &data->regs_user;
4725 u16 stack_size = event->attr.sample_stack_user;
4726 u16 size = sizeof(u64);
4727
4728 if (!uregs->abi)
4729 perf_sample_regs_user(uregs, regs);
4730
4731 stack_size = perf_sample_ustack_size(stack_size, header->size,
4732 uregs->regs);
4733
4734 /*
4735 * If there is something to dump, add space for the dump
4736 * itself and for the field that tells the dynamic size,
4737 * which is how many have been actually dumped.
4738 */
4739 if (stack_size)
4740 size += sizeof(u64) + stack_size;
4741
4742 data->stack_user_size = stack_size;
4743 header->size += size;
4744 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004745}
4746
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004747static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004748 struct perf_sample_data *data,
4749 struct pt_regs *regs)
4750{
4751 struct perf_output_handle handle;
4752 struct perf_event_header header;
4753
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004754 /* protect the callchain buffers */
4755 rcu_read_lock();
4756
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004757 perf_prepare_sample(&header, data, event, regs);
4758
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004759 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004760 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004761
4762 perf_output_sample(&handle, &header, data, event);
4763
4764 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004765
4766exit:
4767 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004768}
4769
4770/*
4771 * read event_id
4772 */
4773
4774struct perf_read_event {
4775 struct perf_event_header header;
4776
4777 u32 pid;
4778 u32 tid;
4779};
4780
4781static void
4782perf_event_read_event(struct perf_event *event,
4783 struct task_struct *task)
4784{
4785 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004786 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004787 struct perf_read_event read_event = {
4788 .header = {
4789 .type = PERF_RECORD_READ,
4790 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004791 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004792 },
4793 .pid = perf_event_pid(event, task),
4794 .tid = perf_event_tid(event, task),
4795 };
4796 int ret;
4797
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004798 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004799 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004800 if (ret)
4801 return;
4802
4803 perf_output_put(&handle, read_event);
4804 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004805 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004806
4807 perf_output_end(&handle);
4808}
4809
Jiri Olsa52d857a2013-05-06 18:27:18 +02004810typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4811
4812static void
4813perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004814 perf_event_aux_output_cb output,
4815 void *data)
4816{
4817 struct perf_event *event;
4818
4819 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4820 if (event->state < PERF_EVENT_STATE_INACTIVE)
4821 continue;
4822 if (!event_filter_match(event))
4823 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004824 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004825 }
4826}
4827
4828static void
Jiri Olsa67516842013-07-09 18:56:31 +02004829perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004830 struct perf_event_context *task_ctx)
4831{
4832 struct perf_cpu_context *cpuctx;
4833 struct perf_event_context *ctx;
4834 struct pmu *pmu;
4835 int ctxn;
4836
4837 rcu_read_lock();
4838 list_for_each_entry_rcu(pmu, &pmus, entry) {
4839 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4840 if (cpuctx->unique_pmu != pmu)
4841 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004842 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004843 if (task_ctx)
4844 goto next;
4845 ctxn = pmu->task_ctx_nr;
4846 if (ctxn < 0)
4847 goto next;
4848 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4849 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004850 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004851next:
4852 put_cpu_ptr(pmu->pmu_cpu_context);
4853 }
4854
4855 if (task_ctx) {
4856 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004857 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004858 preempt_enable();
4859 }
4860 rcu_read_unlock();
4861}
4862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863/*
4864 * task tracking -- fork/exit
4865 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004866 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867 */
4868
4869struct perf_task_event {
4870 struct task_struct *task;
4871 struct perf_event_context *task_ctx;
4872
4873 struct {
4874 struct perf_event_header header;
4875
4876 u32 pid;
4877 u32 ppid;
4878 u32 tid;
4879 u32 ptid;
4880 u64 time;
4881 } event_id;
4882};
4883
Jiri Olsa67516842013-07-09 18:56:31 +02004884static int perf_event_task_match(struct perf_event *event)
4885{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004886 return event->attr.comm || event->attr.mmap ||
4887 event->attr.mmap2 || event->attr.mmap_data ||
4888 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004889}
4890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004891static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004892 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004894 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004895 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004896 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004897 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004898 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004899
Jiri Olsa67516842013-07-09 18:56:31 +02004900 if (!perf_event_task_match(event))
4901 return;
4902
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004903 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004904
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004905 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004906 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004907 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004908 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004909
4910 task_event->event_id.pid = perf_event_pid(event, task);
4911 task_event->event_id.ppid = perf_event_pid(event, current);
4912
4913 task_event->event_id.tid = perf_event_tid(event, task);
4914 task_event->event_id.ptid = perf_event_tid(event, current);
4915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916 perf_output_put(&handle, task_event->event_id);
4917
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004918 perf_event__output_id_sample(event, &handle, &sample);
4919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004920 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004921out:
4922 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923}
4924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004925static void perf_event_task(struct task_struct *task,
4926 struct perf_event_context *task_ctx,
4927 int new)
4928{
4929 struct perf_task_event task_event;
4930
4931 if (!atomic_read(&nr_comm_events) &&
4932 !atomic_read(&nr_mmap_events) &&
4933 !atomic_read(&nr_task_events))
4934 return;
4935
4936 task_event = (struct perf_task_event){
4937 .task = task,
4938 .task_ctx = task_ctx,
4939 .event_id = {
4940 .header = {
4941 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4942 .misc = 0,
4943 .size = sizeof(task_event.event_id),
4944 },
4945 /* .pid */
4946 /* .ppid */
4947 /* .tid */
4948 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004949 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950 },
4951 };
4952
Jiri Olsa67516842013-07-09 18:56:31 +02004953 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004954 &task_event,
4955 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004956}
4957
4958void perf_event_fork(struct task_struct *task)
4959{
4960 perf_event_task(task, NULL, 1);
4961}
4962
4963/*
4964 * comm tracking
4965 */
4966
4967struct perf_comm_event {
4968 struct task_struct *task;
4969 char *comm;
4970 int comm_size;
4971
4972 struct {
4973 struct perf_event_header header;
4974
4975 u32 pid;
4976 u32 tid;
4977 } event_id;
4978};
4979
Jiri Olsa67516842013-07-09 18:56:31 +02004980static int perf_event_comm_match(struct perf_event *event)
4981{
4982 return event->attr.comm;
4983}
4984
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004985static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004986 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004988 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004989 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004990 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004991 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004992 int ret;
4993
Jiri Olsa67516842013-07-09 18:56:31 +02004994 if (!perf_event_comm_match(event))
4995 return;
4996
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004997 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4998 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004999 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005000
5001 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005002 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003
5004 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5005 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5006
5007 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005008 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005010
5011 perf_event__output_id_sample(event, &handle, &sample);
5012
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005013 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005014out:
5015 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005016}
5017
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005018static void perf_event_comm_event(struct perf_comm_event *comm_event)
5019{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022
5023 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005024 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025 size = ALIGN(strlen(comm)+1, sizeof(u64));
5026
5027 comm_event->comm = comm;
5028 comm_event->comm_size = size;
5029
5030 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005031
Jiri Olsa67516842013-07-09 18:56:31 +02005032 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005033 comm_event,
5034 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005035}
5036
5037void perf_event_comm(struct task_struct *task)
5038{
5039 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005040 struct perf_event_context *ctx;
5041 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005043 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005044 for_each_task_context_nr(ctxn) {
5045 ctx = task->perf_event_ctxp[ctxn];
5046 if (!ctx)
5047 continue;
5048
5049 perf_event_enable_on_exec(ctx);
5050 }
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005051 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052
5053 if (!atomic_read(&nr_comm_events))
5054 return;
5055
5056 comm_event = (struct perf_comm_event){
5057 .task = task,
5058 /* .comm */
5059 /* .comm_size */
5060 .event_id = {
5061 .header = {
5062 .type = PERF_RECORD_COMM,
5063 .misc = 0,
5064 /* .size */
5065 },
5066 /* .pid */
5067 /* .tid */
5068 },
5069 };
5070
5071 perf_event_comm_event(&comm_event);
5072}
5073
5074/*
5075 * mmap tracking
5076 */
5077
5078struct perf_mmap_event {
5079 struct vm_area_struct *vma;
5080
5081 const char *file_name;
5082 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005083 int maj, min;
5084 u64 ino;
5085 u64 ino_generation;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086
5087 struct {
5088 struct perf_event_header header;
5089
5090 u32 pid;
5091 u32 tid;
5092 u64 start;
5093 u64 len;
5094 u64 pgoff;
5095 } event_id;
5096};
5097
Jiri Olsa67516842013-07-09 18:56:31 +02005098static int perf_event_mmap_match(struct perf_event *event,
5099 void *data)
5100{
5101 struct perf_mmap_event *mmap_event = data;
5102 struct vm_area_struct *vma = mmap_event->vma;
5103 int executable = vma->vm_flags & VM_EXEC;
5104
5105 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005106 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005107}
5108
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005110 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005112 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005114 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005116 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005117
Jiri Olsa67516842013-07-09 18:56:31 +02005118 if (!perf_event_mmap_match(event, data))
5119 return;
5120
Stephane Eranian13d7a242013-08-21 12:10:24 +02005121 if (event->attr.mmap2) {
5122 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5123 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5124 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5125 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005126 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005127 }
5128
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005129 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5130 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005131 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005132 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005133 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134
5135 mmap_event->event_id.pid = perf_event_pid(event, current);
5136 mmap_event->event_id.tid = perf_event_tid(event, current);
5137
5138 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005139
5140 if (event->attr.mmap2) {
5141 perf_output_put(&handle, mmap_event->maj);
5142 perf_output_put(&handle, mmap_event->min);
5143 perf_output_put(&handle, mmap_event->ino);
5144 perf_output_put(&handle, mmap_event->ino_generation);
5145 }
5146
Frederic Weisbecker76369132011-05-19 19:55:04 +02005147 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005148 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005149
5150 perf_event__output_id_sample(event, &handle, &sample);
5151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005152 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005153out:
5154 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005155}
5156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5158{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 struct vm_area_struct *vma = mmap_event->vma;
5160 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005161 int maj = 0, min = 0;
5162 u64 ino = 0, gen = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005163 unsigned int size;
5164 char tmp[16];
5165 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005166 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167
5168 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005169 struct inode *inode;
5170 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005171
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005172 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005173 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005174 name = "//enomem";
5175 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005176 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005178 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179 * need to add enough zero bytes after the string to handle
5180 * the 64bit alignment we do later.
5181 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005182 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005184 name = "//toolong";
5185 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005187 inode = file_inode(vma->vm_file);
5188 dev = inode->i_sb->s_dev;
5189 ino = inode->i_ino;
5190 gen = inode->i_generation;
5191 maj = MAJOR(dev);
5192 min = MINOR(dev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005194 } else {
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005195 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005196 if (name)
5197 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005199 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005200 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005201 name = "[heap]";
5202 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005203 }
5204 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005206 name = "[stack]";
5207 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005208 }
5209
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005210 name = "//anon";
5211 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212 }
5213
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005214cpy_name:
5215 strlcpy(tmp, name, sizeof(tmp));
5216 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005218 /*
5219 * Since our buffer works in 8 byte units we need to align our string
5220 * size to a multiple of 8. However, we must guarantee the tail end is
5221 * zero'd out to avoid leaking random bits to userspace.
5222 */
5223 size = strlen(name)+1;
5224 while (!IS_ALIGNED(size, sizeof(u64)))
5225 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005226
5227 mmap_event->file_name = name;
5228 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005229 mmap_event->maj = maj;
5230 mmap_event->min = min;
5231 mmap_event->ino = ino;
5232 mmap_event->ino_generation = gen;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233
Stephane Eranian2fe85422013-01-24 16:10:39 +01005234 if (!(vma->vm_flags & VM_EXEC))
5235 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5238
Jiri Olsa67516842013-07-09 18:56:31 +02005239 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005240 mmap_event,
5241 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005242
5243 kfree(buf);
5244}
5245
Eric B Munson3af9e852010-05-18 15:30:49 +01005246void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247{
5248 struct perf_mmap_event mmap_event;
5249
5250 if (!atomic_read(&nr_mmap_events))
5251 return;
5252
5253 mmap_event = (struct perf_mmap_event){
5254 .vma = vma,
5255 /* .file_name */
5256 /* .file_size */
5257 .event_id = {
5258 .header = {
5259 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005260 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261 /* .size */
5262 },
5263 /* .pid */
5264 /* .tid */
5265 .start = vma->vm_start,
5266 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005267 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005269 /* .maj (attr_mmap2 only) */
5270 /* .min (attr_mmap2 only) */
5271 /* .ino (attr_mmap2 only) */
5272 /* .ino_generation (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273 };
5274
5275 perf_event_mmap_event(&mmap_event);
5276}
5277
5278/*
5279 * IRQ throttle logging
5280 */
5281
5282static void perf_log_throttle(struct perf_event *event, int enable)
5283{
5284 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005285 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005286 int ret;
5287
5288 struct {
5289 struct perf_event_header header;
5290 u64 time;
5291 u64 id;
5292 u64 stream_id;
5293 } throttle_event = {
5294 .header = {
5295 .type = PERF_RECORD_THROTTLE,
5296 .misc = 0,
5297 .size = sizeof(throttle_event),
5298 },
5299 .time = perf_clock(),
5300 .id = primary_event_id(event),
5301 .stream_id = event->id,
5302 };
5303
5304 if (enable)
5305 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5306
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005307 perf_event_header__init_id(&throttle_event.header, &sample, event);
5308
5309 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005310 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005311 if (ret)
5312 return;
5313
5314 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005315 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005316 perf_output_end(&handle);
5317}
5318
5319/*
5320 * Generic event overflow handling, sampling.
5321 */
5322
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005323static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324 int throttle, struct perf_sample_data *data,
5325 struct pt_regs *regs)
5326{
5327 int events = atomic_read(&event->event_limit);
5328 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005329 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005330 int ret = 0;
5331
Peter Zijlstra96398822010-11-24 18:55:29 +01005332 /*
5333 * Non-sampling counters might still use the PMI to fold short
5334 * hardware counters, ignore those.
5335 */
5336 if (unlikely(!is_sampling_event(event)))
5337 return 0;
5338
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005339 seq = __this_cpu_read(perf_throttled_seq);
5340 if (seq != hwc->interrupts_seq) {
5341 hwc->interrupts_seq = seq;
5342 hwc->interrupts = 1;
5343 } else {
5344 hwc->interrupts++;
5345 if (unlikely(throttle
5346 && hwc->interrupts >= max_samples_per_tick)) {
5347 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005348 hwc->interrupts = MAX_INTERRUPTS;
5349 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005350 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005351 ret = 1;
5352 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005353 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005354
5355 if (event->attr.freq) {
5356 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005357 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005358
Peter Zijlstraabd50712010-01-26 18:50:16 +01005359 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360
Peter Zijlstraabd50712010-01-26 18:50:16 +01005361 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005362 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363 }
5364
5365 /*
5366 * XXX event_limit might not quite work as expected on inherited
5367 * events
5368 */
5369
5370 event->pending_kill = POLL_IN;
5371 if (events && atomic_dec_and_test(&event->event_limit)) {
5372 ret = 1;
5373 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005374 event->pending_disable = 1;
5375 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005376 }
5377
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005378 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005379 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005380 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005381 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005382
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005383 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005384 event->pending_wakeup = 1;
5385 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005386 }
5387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 return ret;
5389}
5390
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005391int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392 struct perf_sample_data *data,
5393 struct pt_regs *regs)
5394{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005395 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396}
5397
5398/*
5399 * Generic software event infrastructure
5400 */
5401
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005402struct swevent_htable {
5403 struct swevent_hlist *swevent_hlist;
5404 struct mutex hlist_mutex;
5405 int hlist_refcount;
5406
5407 /* Recursion avoidance in each contexts */
5408 int recursion[PERF_NR_CONTEXTS];
5409};
5410
5411static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5412
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413/*
5414 * We directly increment event->count and keep a second value in
5415 * event->hw.period_left to count intervals. This period event
5416 * is kept in the range [-sample_period, 0] so that we can use the
5417 * sign as trigger.
5418 */
5419
Jiri Olsaab573842013-05-01 17:25:44 +02005420u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005421{
5422 struct hw_perf_event *hwc = &event->hw;
5423 u64 period = hwc->last_period;
5424 u64 nr, offset;
5425 s64 old, val;
5426
5427 hwc->last_period = hwc->sample_period;
5428
5429again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005430 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 if (val < 0)
5432 return 0;
5433
5434 nr = div64_u64(period + val, period);
5435 offset = nr * period;
5436 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005437 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438 goto again;
5439
5440 return nr;
5441}
5442
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005443static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005444 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 struct pt_regs *regs)
5446{
5447 struct hw_perf_event *hwc = &event->hw;
5448 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005449
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005450 if (!overflow)
5451 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452
5453 if (hwc->interrupts == MAX_INTERRUPTS)
5454 return;
5455
5456 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005457 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005458 data, regs)) {
5459 /*
5460 * We inhibit the overflow from happening when
5461 * hwc->interrupts == MAX_INTERRUPTS.
5462 */
5463 break;
5464 }
5465 throttle = 1;
5466 }
5467}
5468
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005469static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005470 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005471 struct pt_regs *regs)
5472{
5473 struct hw_perf_event *hwc = &event->hw;
5474
Peter Zijlstrae7850592010-05-21 14:43:08 +02005475 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005477 if (!regs)
5478 return;
5479
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005480 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005481 return;
5482
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005483 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5484 data->period = nr;
5485 return perf_swevent_overflow(event, 1, data, regs);
5486 } else
5487 data->period = event->hw.last_period;
5488
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005489 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005490 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005491
Peter Zijlstrae7850592010-05-21 14:43:08 +02005492 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005493 return;
5494
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005495 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005496}
5497
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005498static int perf_exclude_event(struct perf_event *event,
5499 struct pt_regs *regs)
5500{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005501 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005502 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005503
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005504 if (regs) {
5505 if (event->attr.exclude_user && user_mode(regs))
5506 return 1;
5507
5508 if (event->attr.exclude_kernel && !user_mode(regs))
5509 return 1;
5510 }
5511
5512 return 0;
5513}
5514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005515static int perf_swevent_match(struct perf_event *event,
5516 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005517 u32 event_id,
5518 struct perf_sample_data *data,
5519 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005520{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005521 if (event->attr.type != type)
5522 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005523
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005524 if (event->attr.config != event_id)
5525 return 0;
5526
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005527 if (perf_exclude_event(event, regs))
5528 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005529
5530 return 1;
5531}
5532
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005533static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005534{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005535 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005536
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005537 return hash_64(val, SWEVENT_HLIST_BITS);
5538}
5539
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005540static inline struct hlist_head *
5541__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005542{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005543 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005544
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005545 return &hlist->heads[hash];
5546}
5547
5548/* For the read side: events when they trigger */
5549static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005550find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005551{
5552 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005553
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005554 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005555 if (!hlist)
5556 return NULL;
5557
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005558 return __find_swevent_head(hlist, type, event_id);
5559}
5560
5561/* For the event head insertion and removal in the hlist */
5562static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005563find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005564{
5565 struct swevent_hlist *hlist;
5566 u32 event_id = event->attr.config;
5567 u64 type = event->attr.type;
5568
5569 /*
5570 * Event scheduling is always serialized against hlist allocation
5571 * and release. Which makes the protected version suitable here.
5572 * The context lock guarantees that.
5573 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005574 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005575 lockdep_is_held(&event->ctx->lock));
5576 if (!hlist)
5577 return NULL;
5578
5579 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005580}
5581
5582static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005583 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005584 struct perf_sample_data *data,
5585 struct pt_regs *regs)
5586{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005587 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005588 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005589 struct hlist_head *head;
5590
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005591 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005592 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005593 if (!head)
5594 goto end;
5595
Sasha Levinb67bfe02013-02-27 17:06:00 -08005596 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005597 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005598 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005600end:
5601 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005602}
5603
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005604int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005605{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005606 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005607
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005608 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005610EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005611
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005612inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005613{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005614 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005615
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005616 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005617}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005618
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005619void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005620{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005621 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005622 int rctx;
5623
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005624 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005625 rctx = perf_swevent_get_recursion_context();
5626 if (rctx < 0)
5627 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005628
Robert Richterfd0d0002012-04-02 20:19:08 +02005629 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005630
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005631 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005632
5633 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005634 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005635}
5636
5637static void perf_swevent_read(struct perf_event *event)
5638{
5639}
5640
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005641static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005643 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005645 struct hlist_head *head;
5646
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005647 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005648 hwc->last_period = hwc->sample_period;
5649 perf_swevent_set_period(event);
5650 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005651
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005652 hwc->state = !(flags & PERF_EF_START);
5653
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005654 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005655 if (WARN_ON_ONCE(!head))
5656 return -EINVAL;
5657
5658 hlist_add_head_rcu(&event->hlist_entry, head);
5659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005660 return 0;
5661}
5662
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005663static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005665 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005666}
5667
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005668static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005669{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005670 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005671}
5672
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005673static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005674{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005675 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005676}
5677
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005678/* Deref the hlist from the update side */
5679static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005680swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005681{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005682 return rcu_dereference_protected(swhash->swevent_hlist,
5683 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005684}
5685
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005686static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005687{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005688 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005689
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005690 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005691 return;
5692
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005693 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005694 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005695}
5696
5697static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5698{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005699 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005700
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005701 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005702
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005703 if (!--swhash->hlist_refcount)
5704 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005705
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005706 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005707}
5708
5709static void swevent_hlist_put(struct perf_event *event)
5710{
5711 int cpu;
5712
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005713 for_each_possible_cpu(cpu)
5714 swevent_hlist_put_cpu(event, cpu);
5715}
5716
5717static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5718{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005719 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005720 int err = 0;
5721
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005722 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005723
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005724 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005725 struct swevent_hlist *hlist;
5726
5727 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5728 if (!hlist) {
5729 err = -ENOMEM;
5730 goto exit;
5731 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005732 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005733 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005734 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005735exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005736 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005737
5738 return err;
5739}
5740
5741static int swevent_hlist_get(struct perf_event *event)
5742{
5743 int err;
5744 int cpu, failed_cpu;
5745
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005746 get_online_cpus();
5747 for_each_possible_cpu(cpu) {
5748 err = swevent_hlist_get_cpu(event, cpu);
5749 if (err) {
5750 failed_cpu = cpu;
5751 goto fail;
5752 }
5753 }
5754 put_online_cpus();
5755
5756 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005757fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005758 for_each_possible_cpu(cpu) {
5759 if (cpu == failed_cpu)
5760 break;
5761 swevent_hlist_put_cpu(event, cpu);
5762 }
5763
5764 put_online_cpus();
5765 return err;
5766}
5767
Ingo Molnarc5905af2012-02-24 08:31:31 +01005768struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005769
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005770static void sw_perf_event_destroy(struct perf_event *event)
5771{
5772 u64 event_id = event->attr.config;
5773
5774 WARN_ON(event->parent);
5775
Ingo Molnarc5905af2012-02-24 08:31:31 +01005776 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005777 swevent_hlist_put(event);
5778}
5779
5780static int perf_swevent_init(struct perf_event *event)
5781{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005782 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005783
5784 if (event->attr.type != PERF_TYPE_SOFTWARE)
5785 return -ENOENT;
5786
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005787 /*
5788 * no branch sampling for software events
5789 */
5790 if (has_branch_stack(event))
5791 return -EOPNOTSUPP;
5792
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005793 switch (event_id) {
5794 case PERF_COUNT_SW_CPU_CLOCK:
5795 case PERF_COUNT_SW_TASK_CLOCK:
5796 return -ENOENT;
5797
5798 default:
5799 break;
5800 }
5801
Dan Carpenterce677832010-10-24 21:50:42 +02005802 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005803 return -ENOENT;
5804
5805 if (!event->parent) {
5806 int err;
5807
5808 err = swevent_hlist_get(event);
5809 if (err)
5810 return err;
5811
Ingo Molnarc5905af2012-02-24 08:31:31 +01005812 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005813 event->destroy = sw_perf_event_destroy;
5814 }
5815
5816 return 0;
5817}
5818
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005819static int perf_swevent_event_idx(struct perf_event *event)
5820{
5821 return 0;
5822}
5823
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005824static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005825 .task_ctx_nr = perf_sw_context,
5826
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005827 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005828 .add = perf_swevent_add,
5829 .del = perf_swevent_del,
5830 .start = perf_swevent_start,
5831 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005832 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005833
5834 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005835};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005837#ifdef CONFIG_EVENT_TRACING
5838
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005839static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005840 struct perf_sample_data *data)
5841{
5842 void *record = data->raw->data;
5843
5844 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5845 return 1;
5846 return 0;
5847}
5848
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005849static int perf_tp_event_match(struct perf_event *event,
5850 struct perf_sample_data *data,
5851 struct pt_regs *regs)
5852{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005853 if (event->hw.state & PERF_HES_STOPPED)
5854 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005855 /*
5856 * All tracepoints are from kernel-space.
5857 */
5858 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005859 return 0;
5860
5861 if (!perf_tp_filter_match(event, data))
5862 return 0;
5863
5864 return 1;
5865}
5866
5867void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005868 struct pt_regs *regs, struct hlist_head *head, int rctx,
5869 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005870{
5871 struct perf_sample_data data;
5872 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005873
5874 struct perf_raw_record raw = {
5875 .size = entry_size,
5876 .data = record,
5877 };
5878
Robert Richterfd0d0002012-04-02 20:19:08 +02005879 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005880 data.raw = &raw;
5881
Sasha Levinb67bfe02013-02-27 17:06:00 -08005882 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005883 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005884 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005885 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005886
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005887 /*
5888 * If we got specified a target task, also iterate its context and
5889 * deliver this event there too.
5890 */
5891 if (task && task != current) {
5892 struct perf_event_context *ctx;
5893 struct trace_entry *entry = record;
5894
5895 rcu_read_lock();
5896 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5897 if (!ctx)
5898 goto unlock;
5899
5900 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5901 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5902 continue;
5903 if (event->attr.config != entry->type)
5904 continue;
5905 if (perf_tp_event_match(event, &data, regs))
5906 perf_swevent_event(event, count, &data, regs);
5907 }
5908unlock:
5909 rcu_read_unlock();
5910 }
5911
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005912 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005913}
5914EXPORT_SYMBOL_GPL(perf_tp_event);
5915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005916static void tp_perf_event_destroy(struct perf_event *event)
5917{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005918 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919}
5920
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005921static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005922{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005923 int err;
5924
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005925 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5926 return -ENOENT;
5927
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005928 /*
5929 * no branch sampling for tracepoint events
5930 */
5931 if (has_branch_stack(event))
5932 return -EOPNOTSUPP;
5933
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005934 err = perf_trace_init(event);
5935 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005936 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005937
5938 event->destroy = tp_perf_event_destroy;
5939
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005940 return 0;
5941}
5942
5943static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005944 .task_ctx_nr = perf_sw_context,
5945
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005946 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005947 .add = perf_trace_add,
5948 .del = perf_trace_del,
5949 .start = perf_swevent_start,
5950 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005951 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005952
5953 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005954};
5955
5956static inline void perf_tp_register(void)
5957{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005958 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959}
Li Zefan6fb29152009-10-15 11:21:42 +08005960
5961static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5962{
5963 char *filter_str;
5964 int ret;
5965
5966 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5967 return -EINVAL;
5968
5969 filter_str = strndup_user(arg, PAGE_SIZE);
5970 if (IS_ERR(filter_str))
5971 return PTR_ERR(filter_str);
5972
5973 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5974
5975 kfree(filter_str);
5976 return ret;
5977}
5978
5979static void perf_event_free_filter(struct perf_event *event)
5980{
5981 ftrace_profile_free_filter(event);
5982}
5983
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984#else
Li Zefan6fb29152009-10-15 11:21:42 +08005985
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005986static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005987{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988}
Li Zefan6fb29152009-10-15 11:21:42 +08005989
5990static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5991{
5992 return -ENOENT;
5993}
5994
5995static void perf_event_free_filter(struct perf_event *event)
5996{
5997}
5998
Li Zefan07b139c2009-12-21 14:27:35 +08005999#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006000
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006001#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006002void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006003{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006004 struct perf_sample_data sample;
6005 struct pt_regs *regs = data;
6006
Robert Richterfd0d0002012-04-02 20:19:08 +02006007 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006008
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006009 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006010 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006011}
6012#endif
6013
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006014/*
6015 * hrtimer based swevent callback
6016 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006017
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006018static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006019{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006020 enum hrtimer_restart ret = HRTIMER_RESTART;
6021 struct perf_sample_data data;
6022 struct pt_regs *regs;
6023 struct perf_event *event;
6024 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006025
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006026 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006027
6028 if (event->state != PERF_EVENT_STATE_ACTIVE)
6029 return HRTIMER_NORESTART;
6030
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006031 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006032
Robert Richterfd0d0002012-04-02 20:19:08 +02006033 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006034 regs = get_irq_regs();
6035
6036 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006037 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006038 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006039 ret = HRTIMER_NORESTART;
6040 }
6041
6042 period = max_t(u64, 10000, event->hw.sample_period);
6043 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6044
6045 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006046}
6047
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006048static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006049{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006050 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006051 s64 period;
6052
6053 if (!is_sampling_event(event))
6054 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006055
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006056 period = local64_read(&hwc->period_left);
6057 if (period) {
6058 if (period < 0)
6059 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006060
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006061 local64_set(&hwc->period_left, 0);
6062 } else {
6063 period = max_t(u64, 10000, hwc->sample_period);
6064 }
6065 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006066 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006067 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006068}
6069
6070static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6071{
6072 struct hw_perf_event *hwc = &event->hw;
6073
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006074 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006075 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006076 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006077
6078 hrtimer_cancel(&hwc->hrtimer);
6079 }
6080}
6081
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006082static void perf_swevent_init_hrtimer(struct perf_event *event)
6083{
6084 struct hw_perf_event *hwc = &event->hw;
6085
6086 if (!is_sampling_event(event))
6087 return;
6088
6089 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6090 hwc->hrtimer.function = perf_swevent_hrtimer;
6091
6092 /*
6093 * Since hrtimers have a fixed rate, we can do a static freq->period
6094 * mapping and avoid the whole period adjust feedback stuff.
6095 */
6096 if (event->attr.freq) {
6097 long freq = event->attr.sample_freq;
6098
6099 event->attr.sample_period = NSEC_PER_SEC / freq;
6100 hwc->sample_period = event->attr.sample_period;
6101 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006102 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006103 event->attr.freq = 0;
6104 }
6105}
6106
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006107/*
6108 * Software event: cpu wall time clock
6109 */
6110
6111static void cpu_clock_event_update(struct perf_event *event)
6112{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006113 s64 prev;
6114 u64 now;
6115
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006116 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006117 prev = local64_xchg(&event->hw.prev_count, now);
6118 local64_add(now - prev, &event->count);
6119}
6120
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006121static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006122{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006123 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006124 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006125}
6126
6127static void cpu_clock_event_stop(struct perf_event *event, int flags)
6128{
6129 perf_swevent_cancel_hrtimer(event);
6130 cpu_clock_event_update(event);
6131}
6132
6133static int cpu_clock_event_add(struct perf_event *event, int flags)
6134{
6135 if (flags & PERF_EF_START)
6136 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006137
6138 return 0;
6139}
6140
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006141static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006142{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006143 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006144}
6145
6146static void cpu_clock_event_read(struct perf_event *event)
6147{
6148 cpu_clock_event_update(event);
6149}
6150
6151static int cpu_clock_event_init(struct perf_event *event)
6152{
6153 if (event->attr.type != PERF_TYPE_SOFTWARE)
6154 return -ENOENT;
6155
6156 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6157 return -ENOENT;
6158
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006159 /*
6160 * no branch sampling for software events
6161 */
6162 if (has_branch_stack(event))
6163 return -EOPNOTSUPP;
6164
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006165 perf_swevent_init_hrtimer(event);
6166
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006167 return 0;
6168}
6169
6170static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006171 .task_ctx_nr = perf_sw_context,
6172
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006173 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006174 .add = cpu_clock_event_add,
6175 .del = cpu_clock_event_del,
6176 .start = cpu_clock_event_start,
6177 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006178 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006179
6180 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006181};
6182
6183/*
6184 * Software event: task time clock
6185 */
6186
6187static void task_clock_event_update(struct perf_event *event, u64 now)
6188{
6189 u64 prev;
6190 s64 delta;
6191
6192 prev = local64_xchg(&event->hw.prev_count, now);
6193 delta = now - prev;
6194 local64_add(delta, &event->count);
6195}
6196
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006197static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006198{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006199 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006200 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006201}
6202
6203static void task_clock_event_stop(struct perf_event *event, int flags)
6204{
6205 perf_swevent_cancel_hrtimer(event);
6206 task_clock_event_update(event, event->ctx->time);
6207}
6208
6209static int task_clock_event_add(struct perf_event *event, int flags)
6210{
6211 if (flags & PERF_EF_START)
6212 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006213
6214 return 0;
6215}
6216
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006217static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006218{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006219 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006220}
6221
6222static void task_clock_event_read(struct perf_event *event)
6223{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006224 u64 now = perf_clock();
6225 u64 delta = now - event->ctx->timestamp;
6226 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006227
6228 task_clock_event_update(event, time);
6229}
6230
6231static int task_clock_event_init(struct perf_event *event)
6232{
6233 if (event->attr.type != PERF_TYPE_SOFTWARE)
6234 return -ENOENT;
6235
6236 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6237 return -ENOENT;
6238
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006239 /*
6240 * no branch sampling for software events
6241 */
6242 if (has_branch_stack(event))
6243 return -EOPNOTSUPP;
6244
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006245 perf_swevent_init_hrtimer(event);
6246
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006247 return 0;
6248}
6249
6250static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006251 .task_ctx_nr = perf_sw_context,
6252
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006253 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006254 .add = task_clock_event_add,
6255 .del = task_clock_event_del,
6256 .start = task_clock_event_start,
6257 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006258 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006259
6260 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006261};
6262
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006263static void perf_pmu_nop_void(struct pmu *pmu)
6264{
6265}
6266
6267static int perf_pmu_nop_int(struct pmu *pmu)
6268{
6269 return 0;
6270}
6271
6272static void perf_pmu_start_txn(struct pmu *pmu)
6273{
6274 perf_pmu_disable(pmu);
6275}
6276
6277static int perf_pmu_commit_txn(struct pmu *pmu)
6278{
6279 perf_pmu_enable(pmu);
6280 return 0;
6281}
6282
6283static void perf_pmu_cancel_txn(struct pmu *pmu)
6284{
6285 perf_pmu_enable(pmu);
6286}
6287
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006288static int perf_event_idx_default(struct perf_event *event)
6289{
6290 return event->hw.idx + 1;
6291}
6292
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006293/*
6294 * Ensures all contexts with the same task_ctx_nr have the same
6295 * pmu_cpu_context too.
6296 */
6297static void *find_pmu_context(int ctxn)
6298{
6299 struct pmu *pmu;
6300
6301 if (ctxn < 0)
6302 return NULL;
6303
6304 list_for_each_entry(pmu, &pmus, entry) {
6305 if (pmu->task_ctx_nr == ctxn)
6306 return pmu->pmu_cpu_context;
6307 }
6308
6309 return NULL;
6310}
6311
Peter Zijlstra51676952010-12-07 14:18:20 +01006312static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006313{
Peter Zijlstra51676952010-12-07 14:18:20 +01006314 int cpu;
6315
6316 for_each_possible_cpu(cpu) {
6317 struct perf_cpu_context *cpuctx;
6318
6319 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6320
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006321 if (cpuctx->unique_pmu == old_pmu)
6322 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006323 }
6324}
6325
6326static void free_pmu_context(struct pmu *pmu)
6327{
6328 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006329
6330 mutex_lock(&pmus_lock);
6331 /*
6332 * Like a real lame refcount.
6333 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006334 list_for_each_entry(i, &pmus, entry) {
6335 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6336 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006337 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006338 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006339 }
6340
Peter Zijlstra51676952010-12-07 14:18:20 +01006341 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006342out:
6343 mutex_unlock(&pmus_lock);
6344}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006345static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006346
Peter Zijlstraabe43402010-11-17 23:17:37 +01006347static ssize_t
6348type_show(struct device *dev, struct device_attribute *attr, char *page)
6349{
6350 struct pmu *pmu = dev_get_drvdata(dev);
6351
6352 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6353}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006354static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006355
Stephane Eranian62b85632013-04-03 14:21:34 +02006356static ssize_t
6357perf_event_mux_interval_ms_show(struct device *dev,
6358 struct device_attribute *attr,
6359 char *page)
6360{
6361 struct pmu *pmu = dev_get_drvdata(dev);
6362
6363 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6364}
6365
6366static ssize_t
6367perf_event_mux_interval_ms_store(struct device *dev,
6368 struct device_attribute *attr,
6369 const char *buf, size_t count)
6370{
6371 struct pmu *pmu = dev_get_drvdata(dev);
6372 int timer, cpu, ret;
6373
6374 ret = kstrtoint(buf, 0, &timer);
6375 if (ret)
6376 return ret;
6377
6378 if (timer < 1)
6379 return -EINVAL;
6380
6381 /* same value, noting to do */
6382 if (timer == pmu->hrtimer_interval_ms)
6383 return count;
6384
6385 pmu->hrtimer_interval_ms = timer;
6386
6387 /* update all cpuctx for this PMU */
6388 for_each_possible_cpu(cpu) {
6389 struct perf_cpu_context *cpuctx;
6390 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6391 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6392
6393 if (hrtimer_active(&cpuctx->hrtimer))
6394 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6395 }
6396
6397 return count;
6398}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006399static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006400
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006401static struct attribute *pmu_dev_attrs[] = {
6402 &dev_attr_type.attr,
6403 &dev_attr_perf_event_mux_interval_ms.attr,
6404 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006405};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006406ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006407
6408static int pmu_bus_running;
6409static struct bus_type pmu_bus = {
6410 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006411 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006412};
6413
6414static void pmu_dev_release(struct device *dev)
6415{
6416 kfree(dev);
6417}
6418
6419static int pmu_dev_alloc(struct pmu *pmu)
6420{
6421 int ret = -ENOMEM;
6422
6423 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6424 if (!pmu->dev)
6425 goto out;
6426
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006427 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006428 device_initialize(pmu->dev);
6429 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6430 if (ret)
6431 goto free_dev;
6432
6433 dev_set_drvdata(pmu->dev, pmu);
6434 pmu->dev->bus = &pmu_bus;
6435 pmu->dev->release = pmu_dev_release;
6436 ret = device_add(pmu->dev);
6437 if (ret)
6438 goto free_dev;
6439
6440out:
6441 return ret;
6442
6443free_dev:
6444 put_device(pmu->dev);
6445 goto out;
6446}
6447
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006448static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006449static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006450
Mischa Jonker03d8e802013-06-04 11:45:48 +02006451int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006452{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006453 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006454
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006455 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006456 ret = -ENOMEM;
6457 pmu->pmu_disable_count = alloc_percpu(int);
6458 if (!pmu->pmu_disable_count)
6459 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006460
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006461 pmu->type = -1;
6462 if (!name)
6463 goto skip_type;
6464 pmu->name = name;
6465
6466 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006467 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6468 if (type < 0) {
6469 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006470 goto free_pdc;
6471 }
6472 }
6473 pmu->type = type;
6474
Peter Zijlstraabe43402010-11-17 23:17:37 +01006475 if (pmu_bus_running) {
6476 ret = pmu_dev_alloc(pmu);
6477 if (ret)
6478 goto free_idr;
6479 }
6480
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006481skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006482 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6483 if (pmu->pmu_cpu_context)
6484 goto got_cpu_context;
6485
Wei Yongjunc4814202013-04-12 11:05:54 +08006486 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006487 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6488 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006489 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006490
6491 for_each_possible_cpu(cpu) {
6492 struct perf_cpu_context *cpuctx;
6493
6494 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006495 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006496 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006497 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006498 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006499 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006500
6501 __perf_cpu_hrtimer_init(cpuctx, cpu);
6502
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006503 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006504 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006505 }
6506
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006507got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006508 if (!pmu->start_txn) {
6509 if (pmu->pmu_enable) {
6510 /*
6511 * If we have pmu_enable/pmu_disable calls, install
6512 * transaction stubs that use that to try and batch
6513 * hardware accesses.
6514 */
6515 pmu->start_txn = perf_pmu_start_txn;
6516 pmu->commit_txn = perf_pmu_commit_txn;
6517 pmu->cancel_txn = perf_pmu_cancel_txn;
6518 } else {
6519 pmu->start_txn = perf_pmu_nop_void;
6520 pmu->commit_txn = perf_pmu_nop_int;
6521 pmu->cancel_txn = perf_pmu_nop_void;
6522 }
6523 }
6524
6525 if (!pmu->pmu_enable) {
6526 pmu->pmu_enable = perf_pmu_nop_void;
6527 pmu->pmu_disable = perf_pmu_nop_void;
6528 }
6529
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006530 if (!pmu->event_idx)
6531 pmu->event_idx = perf_event_idx_default;
6532
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006533 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006534 ret = 0;
6535unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006536 mutex_unlock(&pmus_lock);
6537
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006538 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006539
Peter Zijlstraabe43402010-11-17 23:17:37 +01006540free_dev:
6541 device_del(pmu->dev);
6542 put_device(pmu->dev);
6543
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006544free_idr:
6545 if (pmu->type >= PERF_TYPE_MAX)
6546 idr_remove(&pmu_idr, pmu->type);
6547
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006548free_pdc:
6549 free_percpu(pmu->pmu_disable_count);
6550 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006551}
6552
6553void perf_pmu_unregister(struct pmu *pmu)
6554{
6555 mutex_lock(&pmus_lock);
6556 list_del_rcu(&pmu->entry);
6557 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006558
6559 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006560 * We dereference the pmu list under both SRCU and regular RCU, so
6561 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006563 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006564 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006565
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006566 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006567 if (pmu->type >= PERF_TYPE_MAX)
6568 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006569 device_del(pmu->dev);
6570 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006571 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006572}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006573
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006574struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006575{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006576 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006577 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006578 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006579
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006580 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006581
6582 rcu_read_lock();
6583 pmu = idr_find(&pmu_idr, event->attr.type);
6584 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006585 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006586 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006587 ret = pmu->event_init(event);
6588 if (ret)
6589 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006590 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006591 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006592
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006593 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006594 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006595 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006596 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006597 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006598
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006599 if (ret != -ENOENT) {
6600 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006601 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006603 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006604 pmu = ERR_PTR(-ENOENT);
6605unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006606 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006607
6608 return pmu;
6609}
6610
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006611static void account_event_cpu(struct perf_event *event, int cpu)
6612{
6613 if (event->parent)
6614 return;
6615
6616 if (has_branch_stack(event)) {
6617 if (!(event->attach_state & PERF_ATTACH_TASK))
6618 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6619 }
6620 if (is_cgroup_event(event))
6621 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6622}
6623
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006624static void account_event(struct perf_event *event)
6625{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006626 if (event->parent)
6627 return;
6628
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006629 if (event->attach_state & PERF_ATTACH_TASK)
6630 static_key_slow_inc(&perf_sched_events.key);
6631 if (event->attr.mmap || event->attr.mmap_data)
6632 atomic_inc(&nr_mmap_events);
6633 if (event->attr.comm)
6634 atomic_inc(&nr_comm_events);
6635 if (event->attr.task)
6636 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006637 if (event->attr.freq) {
6638 if (atomic_inc_return(&nr_freq_events) == 1)
6639 tick_nohz_full_kick_all();
6640 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006641 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006642 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006643 if (is_cgroup_event(event))
6644 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006645
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006646 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006647}
6648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649/*
6650 * Allocate and initialize a event structure
6651 */
6652static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006653perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006654 struct task_struct *task,
6655 struct perf_event *group_leader,
6656 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006657 perf_overflow_handler_t overflow_handler,
6658 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006660 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661 struct perf_event *event;
6662 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006663 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006664
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006665 if ((unsigned)cpu >= nr_cpu_ids) {
6666 if (!task || cpu != -1)
6667 return ERR_PTR(-EINVAL);
6668 }
6669
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006670 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671 if (!event)
6672 return ERR_PTR(-ENOMEM);
6673
6674 /*
6675 * Single events are their own group leaders, with an
6676 * empty sibling list:
6677 */
6678 if (!group_leader)
6679 group_leader = event;
6680
6681 mutex_init(&event->child_mutex);
6682 INIT_LIST_HEAD(&event->child_list);
6683
6684 INIT_LIST_HEAD(&event->group_entry);
6685 INIT_LIST_HEAD(&event->event_entry);
6686 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006687 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006688 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006689 INIT_HLIST_NODE(&event->hlist_entry);
6690
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006693 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694
6695 mutex_init(&event->mmap_mutex);
6696
Al Viroa6fa9412012-08-20 14:59:25 +01006697 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698 event->cpu = cpu;
6699 event->attr = *attr;
6700 event->group_leader = group_leader;
6701 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006702 event->oncpu = -1;
6703
6704 event->parent = parent_event;
6705
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006706 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006707 event->id = atomic64_inc_return(&perf_event_id);
6708
6709 event->state = PERF_EVENT_STATE_INACTIVE;
6710
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006711 if (task) {
6712 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006713
6714 if (attr->type == PERF_TYPE_TRACEPOINT)
6715 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006716#ifdef CONFIG_HAVE_HW_BREAKPOINT
6717 /*
6718 * hw_breakpoint is a bit difficult here..
6719 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006720 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006721 event->hw.bp_target = task;
6722#endif
6723 }
6724
Avi Kivity4dc0da82011-06-29 18:42:35 +03006725 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006726 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006727 context = parent_event->overflow_handler_context;
6728 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006729
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006730 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006731 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006732
Jiri Olsa0231bb52013-02-01 11:23:45 +01006733 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006734
6735 pmu = NULL;
6736
6737 hwc = &event->hw;
6738 hwc->sample_period = attr->sample_period;
6739 if (attr->freq && attr->sample_freq)
6740 hwc->sample_period = 1;
6741 hwc->last_period = hwc->sample_period;
6742
Peter Zijlstrae7850592010-05-21 14:43:08 +02006743 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744
6745 /*
6746 * we currently do not support PERF_FORMAT_GROUP on inherited events
6747 */
6748 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006749 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006751 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006752 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006753 goto err_ns;
6754 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006756 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006757 }
6758
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006760 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6761 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006762 if (err)
6763 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006764 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006765 }
6766
6767 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006768
6769err_pmu:
6770 if (event->destroy)
6771 event->destroy(event);
6772err_ns:
6773 if (event->ns)
6774 put_pid_ns(event->ns);
6775 kfree(event);
6776
6777 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006778}
6779
6780static int perf_copy_attr(struct perf_event_attr __user *uattr,
6781 struct perf_event_attr *attr)
6782{
6783 u32 size;
6784 int ret;
6785
6786 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6787 return -EFAULT;
6788
6789 /*
6790 * zero the full structure, so that a short copy will be nice.
6791 */
6792 memset(attr, 0, sizeof(*attr));
6793
6794 ret = get_user(size, &uattr->size);
6795 if (ret)
6796 return ret;
6797
6798 if (size > PAGE_SIZE) /* silly large */
6799 goto err_size;
6800
6801 if (!size) /* abi compat */
6802 size = PERF_ATTR_SIZE_VER0;
6803
6804 if (size < PERF_ATTR_SIZE_VER0)
6805 goto err_size;
6806
6807 /*
6808 * If we're handed a bigger struct than we know of,
6809 * ensure all the unknown bits are 0 - i.e. new
6810 * user-space does not rely on any kernel feature
6811 * extensions we dont know about yet.
6812 */
6813 if (size > sizeof(*attr)) {
6814 unsigned char __user *addr;
6815 unsigned char __user *end;
6816 unsigned char val;
6817
6818 addr = (void __user *)uattr + sizeof(*attr);
6819 end = (void __user *)uattr + size;
6820
6821 for (; addr < end; addr++) {
6822 ret = get_user(val, addr);
6823 if (ret)
6824 return ret;
6825 if (val)
6826 goto err_size;
6827 }
6828 size = sizeof(*attr);
6829 }
6830
6831 ret = copy_from_user(attr, uattr, size);
6832 if (ret)
6833 return -EFAULT;
6834
Stephane Eranian3090ffb2013-10-17 19:32:15 +02006835 /* disabled for now */
6836 if (attr->mmap2)
6837 return -EINVAL;
6838
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306839 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006840 return -EINVAL;
6841
6842 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6843 return -EINVAL;
6844
6845 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6846 return -EINVAL;
6847
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006848 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6849 u64 mask = attr->branch_sample_type;
6850
6851 /* only using defined bits */
6852 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6853 return -EINVAL;
6854
6855 /* at least one branch bit must be set */
6856 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6857 return -EINVAL;
6858
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006859 /* propagate priv level, when not set for branch */
6860 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6861
6862 /* exclude_kernel checked on syscall entry */
6863 if (!attr->exclude_kernel)
6864 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6865
6866 if (!attr->exclude_user)
6867 mask |= PERF_SAMPLE_BRANCH_USER;
6868
6869 if (!attr->exclude_hv)
6870 mask |= PERF_SAMPLE_BRANCH_HV;
6871 /*
6872 * adjust user setting (for HW filter setup)
6873 */
6874 attr->branch_sample_type = mask;
6875 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006876 /* privileged levels capture (kernel, hv): check permissions */
6877 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006878 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6879 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006880 }
Jiri Olsa40189942012-08-07 15:20:37 +02006881
Jiri Olsac5ebced2012-08-07 15:20:40 +02006882 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006883 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006884 if (ret)
6885 return ret;
6886 }
6887
6888 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6889 if (!arch_perf_have_user_stack_dump())
6890 return -ENOSYS;
6891
6892 /*
6893 * We have __u32 type for the size, but so far
6894 * we can only use __u16 as maximum due to the
6895 * __u16 sample size limit.
6896 */
6897 if (attr->sample_stack_user >= USHRT_MAX)
6898 ret = -EINVAL;
6899 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6900 ret = -EINVAL;
6901 }
Jiri Olsa40189942012-08-07 15:20:37 +02006902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006903out:
6904 return ret;
6905
6906err_size:
6907 put_user(sizeof(*attr), &uattr->size);
6908 ret = -E2BIG;
6909 goto out;
6910}
6911
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006912static int
6913perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006914{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006915 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006916 int ret = -EINVAL;
6917
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006918 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006919 goto set;
6920
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006921 /* don't allow circular references */
6922 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006923 goto out;
6924
Peter Zijlstra0f139302010-05-20 14:35:15 +02006925 /*
6926 * Don't allow cross-cpu buffers
6927 */
6928 if (output_event->cpu != event->cpu)
6929 goto out;
6930
6931 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006932 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006933 */
6934 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6935 goto out;
6936
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006937set:
6938 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006939 /* Can't redirect output if we've got an active mmap() */
6940 if (atomic_read(&event->mmap_count))
6941 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006942
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006943 old_rb = event->rb;
6944
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006945 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006946 /* get the rb we want to redirect to */
6947 rb = ring_buffer_get(output_event);
6948 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006949 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 }
6951
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006952 if (old_rb)
6953 ring_buffer_detach(event, old_rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006954
6955 if (rb)
6956 ring_buffer_attach(event, rb);
6957
6958 rcu_assign_pointer(event->rb, rb);
6959
6960 if (old_rb) {
6961 ring_buffer_put(old_rb);
6962 /*
6963 * Since we detached before setting the new rb, so that we
6964 * could attach the new rb, we could have missed a wakeup.
6965 * Provide it now.
6966 */
6967 wake_up_all(&event->waitq);
6968 }
6969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006970 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006971unlock:
6972 mutex_unlock(&event->mmap_mutex);
6973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006974out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006975 return ret;
6976}
6977
6978/**
6979 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6980 *
6981 * @attr_uptr: event_id type attributes for monitoring/sampling
6982 * @pid: target pid
6983 * @cpu: target cpu
6984 * @group_fd: group leader event fd
6985 */
6986SYSCALL_DEFINE5(perf_event_open,
6987 struct perf_event_attr __user *, attr_uptr,
6988 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6989{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006990 struct perf_event *group_leader = NULL, *output_event = NULL;
6991 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006992 struct perf_event_attr attr;
6993 struct perf_event_context *ctx;
6994 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006995 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006996 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006997 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006998 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006999 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007000 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007001 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007002
7003 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007004 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007005 return -EINVAL;
7006
7007 err = perf_copy_attr(attr_uptr, &attr);
7008 if (err)
7009 return err;
7010
7011 if (!attr.exclude_kernel) {
7012 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7013 return -EACCES;
7014 }
7015
7016 if (attr.freq) {
7017 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7018 return -EINVAL;
7019 }
7020
Stephane Eraniane5d13672011-02-14 11:20:01 +02007021 /*
7022 * In cgroup mode, the pid argument is used to pass the fd
7023 * opened to the cgroup directory in cgroupfs. The cpu argument
7024 * designates the cpu on which to monitor threads from that
7025 * cgroup.
7026 */
7027 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7028 return -EINVAL;
7029
Yann Droneauda21b0b32014-01-05 21:36:33 +01007030 if (flags & PERF_FLAG_FD_CLOEXEC)
7031 f_flags |= O_CLOEXEC;
7032
7033 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007034 if (event_fd < 0)
7035 return event_fd;
7036
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007037 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007038 err = perf_fget_light(group_fd, &group);
7039 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007040 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007041 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007042 if (flags & PERF_FLAG_FD_OUTPUT)
7043 output_event = group_leader;
7044 if (flags & PERF_FLAG_FD_NO_GROUP)
7045 group_leader = NULL;
7046 }
7047
Stephane Eraniane5d13672011-02-14 11:20:01 +02007048 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007049 task = find_lively_task_by_vpid(pid);
7050 if (IS_ERR(task)) {
7051 err = PTR_ERR(task);
7052 goto err_group_fd;
7053 }
7054 }
7055
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007056 get_online_cpus();
7057
Avi Kivity4dc0da82011-06-29 18:42:35 +03007058 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7059 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007060 if (IS_ERR(event)) {
7061 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007062 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007063 }
7064
Stephane Eraniane5d13672011-02-14 11:20:01 +02007065 if (flags & PERF_FLAG_PID_CGROUP) {
7066 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007067 if (err) {
7068 __free_event(event);
7069 goto err_task;
7070 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007071 }
7072
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007073 account_event(event);
7074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007076 * Special case software events and allow them to be part of
7077 * any hardware group.
7078 */
7079 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007080
7081 if (group_leader &&
7082 (is_software_event(event) != is_software_event(group_leader))) {
7083 if (is_software_event(event)) {
7084 /*
7085 * If event and group_leader are not both a software
7086 * event, and event is, then group leader is not.
7087 *
7088 * Allow the addition of software events to !software
7089 * groups, this is safe because software events never
7090 * fail to schedule.
7091 */
7092 pmu = group_leader->pmu;
7093 } else if (is_software_event(group_leader) &&
7094 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7095 /*
7096 * In case the group is a pure software group, and we
7097 * try to add a hardware event, move the whole group to
7098 * the hardware context.
7099 */
7100 move_group = 1;
7101 }
7102 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007103
7104 /*
7105 * Get the target context (task or percpu):
7106 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007107 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007108 if (IS_ERR(ctx)) {
7109 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007110 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007111 }
7112
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007113 if (task) {
7114 put_task_struct(task);
7115 task = NULL;
7116 }
7117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007118 /*
7119 * Look up the group leader (we will attach this event to it):
7120 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007121 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007122 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007123
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007124 /*
7125 * Do not allow a recursive hierarchy (this new sibling
7126 * becoming part of another group-sibling):
7127 */
7128 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007129 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007130 /*
7131 * Do not allow to attach to a group in a different
7132 * task or CPU context:
7133 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007134 if (move_group) {
7135 if (group_leader->ctx->type != ctx->type)
7136 goto err_context;
7137 } else {
7138 if (group_leader->ctx != ctx)
7139 goto err_context;
7140 }
7141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007142 /*
7143 * Only a group leader can be exclusive or pinned
7144 */
7145 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007146 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007147 }
7148
7149 if (output_event) {
7150 err = perf_event_set_output(event, output_event);
7151 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007152 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007153 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154
Yann Droneauda21b0b32014-01-05 21:36:33 +01007155 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7156 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007157 if (IS_ERR(event_file)) {
7158 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007159 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007160 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007161
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007162 if (move_group) {
7163 struct perf_event_context *gctx = group_leader->ctx;
7164
7165 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007166 perf_remove_from_context(group_leader);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007167
7168 /*
7169 * Removing from the context ends up with disabled
7170 * event. What we want here is event in the initial
7171 * startup state, ready to be add into new context.
7172 */
7173 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007174 list_for_each_entry(sibling, &group_leader->sibling_list,
7175 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007176 perf_remove_from_context(sibling);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007177 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007178 put_ctx(gctx);
7179 }
7180 mutex_unlock(&gctx->mutex);
7181 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007182 }
7183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007184 WARN_ON_ONCE(ctx->parent_ctx);
7185 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007186
7187 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007188 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007189 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007190 get_ctx(ctx);
7191 list_for_each_entry(sibling, &group_leader->sibling_list,
7192 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007193 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007194 get_ctx(ctx);
7195 }
7196 }
7197
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007198 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007199 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007200 mutex_unlock(&ctx->mutex);
7201
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007202 put_online_cpus();
7203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007204 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007205
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007206 mutex_lock(&current->perf_event_mutex);
7207 list_add_tail(&event->owner_entry, &current->perf_event_list);
7208 mutex_unlock(&current->perf_event_mutex);
7209
Peter Zijlstra8a495422010-05-27 15:47:49 +02007210 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007211 * Precalculate sample_data sizes
7212 */
7213 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007214 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007215
7216 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007217 * Drop the reference on the group_event after placing the
7218 * new event on the sibling_list. This ensures destruction
7219 * of the group leader will find the pointer to itself in
7220 * perf_group_detach().
7221 */
Al Viro2903ff02012-08-28 12:52:22 -04007222 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007223 fd_install(event_fd, event_file);
7224 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007225
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007226err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007227 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007228 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007229err_alloc:
7230 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007231err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007232 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007233 if (task)
7234 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007235err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007236 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007237err_fd:
7238 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239 return err;
7240}
7241
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007242/**
7243 * perf_event_create_kernel_counter
7244 *
7245 * @attr: attributes of the counter to create
7246 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007247 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007248 */
7249struct perf_event *
7250perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007251 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007252 perf_overflow_handler_t overflow_handler,
7253 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007254{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007255 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007256 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007257 int err;
7258
7259 /*
7260 * Get the target context (task or percpu):
7261 */
7262
Avi Kivity4dc0da82011-06-29 18:42:35 +03007263 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7264 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007265 if (IS_ERR(event)) {
7266 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007267 goto err;
7268 }
7269
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007270 account_event(event);
7271
Matt Helsley38a81da2010-09-13 13:01:20 -07007272 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007273 if (IS_ERR(ctx)) {
7274 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007275 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007276 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007277
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007278 WARN_ON_ONCE(ctx->parent_ctx);
7279 mutex_lock(&ctx->mutex);
7280 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007281 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007282 mutex_unlock(&ctx->mutex);
7283
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007284 return event;
7285
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007286err_free:
7287 free_event(event);
7288err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007289 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007290}
7291EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7292
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007293void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7294{
7295 struct perf_event_context *src_ctx;
7296 struct perf_event_context *dst_ctx;
7297 struct perf_event *event, *tmp;
7298 LIST_HEAD(events);
7299
7300 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7301 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7302
7303 mutex_lock(&src_ctx->mutex);
7304 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7305 event_entry) {
7306 perf_remove_from_context(event);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007307 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007308 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007309 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007310 }
7311 mutex_unlock(&src_ctx->mutex);
7312
7313 synchronize_rcu();
7314
7315 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007316 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7317 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007318 if (event->state >= PERF_EVENT_STATE_OFF)
7319 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007320 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007321 perf_install_in_context(dst_ctx, event, dst_cpu);
7322 get_ctx(dst_ctx);
7323 }
7324 mutex_unlock(&dst_ctx->mutex);
7325}
7326EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7327
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328static void sync_child_event(struct perf_event *child_event,
7329 struct task_struct *child)
7330{
7331 struct perf_event *parent_event = child_event->parent;
7332 u64 child_val;
7333
7334 if (child_event->attr.inherit_stat)
7335 perf_event_read_event(child_event, child);
7336
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007337 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007338
7339 /*
7340 * Add back the child's count to the parent's count:
7341 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007342 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007343 atomic64_add(child_event->total_time_enabled,
7344 &parent_event->child_total_time_enabled);
7345 atomic64_add(child_event->total_time_running,
7346 &parent_event->child_total_time_running);
7347
7348 /*
7349 * Remove this event from the parent's list
7350 */
7351 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7352 mutex_lock(&parent_event->child_mutex);
7353 list_del_init(&child_event->child_list);
7354 mutex_unlock(&parent_event->child_mutex);
7355
7356 /*
7357 * Release the parent event, if this was the last
7358 * reference to it.
7359 */
Al Viroa6fa9412012-08-20 14:59:25 +01007360 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007361}
7362
7363static void
7364__perf_event_exit_task(struct perf_event *child_event,
7365 struct perf_event_context *child_ctx,
7366 struct task_struct *child)
7367{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007368 if (child_event->parent) {
7369 raw_spin_lock_irq(&child_ctx->lock);
7370 perf_group_detach(child_event);
7371 raw_spin_unlock_irq(&child_ctx->lock);
7372 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007373
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007374 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007375
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007376 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007377 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007378 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007379 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007380 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007381 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007382 sync_child_event(child_event, child);
7383 free_event(child_event);
7384 }
7385}
7386
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007387static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007388{
7389 struct perf_event *child_event, *tmp;
7390 struct perf_event_context *child_ctx;
7391 unsigned long flags;
7392
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007393 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007394 perf_event_task(child, NULL, 0);
7395 return;
7396 }
7397
7398 local_irq_save(flags);
7399 /*
7400 * We can't reschedule here because interrupts are disabled,
7401 * and either child is current or it is a task that can't be
7402 * scheduled, so we are now safe from rescheduling changing
7403 * our context.
7404 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007405 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007406
7407 /*
7408 * Take the context lock here so that if find_get_context is
7409 * reading child->perf_event_ctxp, we wait until it has
7410 * incremented the context's refcount before we do put_ctx below.
7411 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007412 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007413 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007414 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007415 /*
7416 * If this context is a clone; unclone it so it can't get
7417 * swapped to another process while we're removing all
7418 * the events from it.
7419 */
7420 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007421 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007422 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423
7424 /*
7425 * Report the task dead after unscheduling the events so that we
7426 * won't get any samples after PERF_RECORD_EXIT. We can however still
7427 * get a few PERF_RECORD_READ events.
7428 */
7429 perf_event_task(child, child_ctx, 0);
7430
7431 /*
7432 * We can recurse on the same lock type through:
7433 *
7434 * __perf_event_exit_task()
7435 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007436 * put_event()
7437 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007438 *
7439 * But since its the parent context it won't be the same instance.
7440 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007441 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007442
7443again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007444 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7445 group_entry)
7446 __perf_event_exit_task(child_event, child_ctx, child);
7447
7448 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007449 group_entry)
7450 __perf_event_exit_task(child_event, child_ctx, child);
7451
7452 /*
7453 * If the last event was a group event, it will have appended all
7454 * its siblings to the list, but we obtained 'tmp' before that which
7455 * will still point to the list head terminating the iteration.
7456 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007457 if (!list_empty(&child_ctx->pinned_groups) ||
7458 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007459 goto again;
7460
7461 mutex_unlock(&child_ctx->mutex);
7462
7463 put_ctx(child_ctx);
7464}
7465
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007466/*
7467 * When a child task exits, feed back event values to parent events.
7468 */
7469void perf_event_exit_task(struct task_struct *child)
7470{
Peter Zijlstra88821352010-11-09 19:01:43 +01007471 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007472 int ctxn;
7473
Peter Zijlstra88821352010-11-09 19:01:43 +01007474 mutex_lock(&child->perf_event_mutex);
7475 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7476 owner_entry) {
7477 list_del_init(&event->owner_entry);
7478
7479 /*
7480 * Ensure the list deletion is visible before we clear
7481 * the owner, closes a race against perf_release() where
7482 * we need to serialize on the owner->perf_event_mutex.
7483 */
7484 smp_wmb();
7485 event->owner = NULL;
7486 }
7487 mutex_unlock(&child->perf_event_mutex);
7488
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007489 for_each_task_context_nr(ctxn)
7490 perf_event_exit_task_context(child, ctxn);
7491}
7492
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007493static void perf_free_event(struct perf_event *event,
7494 struct perf_event_context *ctx)
7495{
7496 struct perf_event *parent = event->parent;
7497
7498 if (WARN_ON_ONCE(!parent))
7499 return;
7500
7501 mutex_lock(&parent->child_mutex);
7502 list_del_init(&event->child_list);
7503 mutex_unlock(&parent->child_mutex);
7504
Al Viroa6fa9412012-08-20 14:59:25 +01007505 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007506
Peter Zijlstra8a495422010-05-27 15:47:49 +02007507 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007508 list_del_event(event, ctx);
7509 free_event(event);
7510}
7511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007512/*
7513 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007514 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007515 */
7516void perf_event_free_task(struct task_struct *task)
7517{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007518 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007519 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007520 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007521
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007522 for_each_task_context_nr(ctxn) {
7523 ctx = task->perf_event_ctxp[ctxn];
7524 if (!ctx)
7525 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007526
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007527 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007528again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007529 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7530 group_entry)
7531 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007532
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007533 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7534 group_entry)
7535 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007536
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007537 if (!list_empty(&ctx->pinned_groups) ||
7538 !list_empty(&ctx->flexible_groups))
7539 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007540
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007541 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007542
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007543 put_ctx(ctx);
7544 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007545}
7546
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007547void perf_event_delayed_put(struct task_struct *task)
7548{
7549 int ctxn;
7550
7551 for_each_task_context_nr(ctxn)
7552 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7553}
7554
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007555/*
7556 * inherit a event from parent task to child task:
7557 */
7558static struct perf_event *
7559inherit_event(struct perf_event *parent_event,
7560 struct task_struct *parent,
7561 struct perf_event_context *parent_ctx,
7562 struct task_struct *child,
7563 struct perf_event *group_leader,
7564 struct perf_event_context *child_ctx)
7565{
7566 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007567 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007568
7569 /*
7570 * Instead of creating recursive hierarchies of events,
7571 * we link inherited events back to the original parent,
7572 * which has a filp for sure, which we use as the reference
7573 * count:
7574 */
7575 if (parent_event->parent)
7576 parent_event = parent_event->parent;
7577
7578 child_event = perf_event_alloc(&parent_event->attr,
7579 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007580 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007581 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007582 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007583 if (IS_ERR(child_event))
7584 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007585
7586 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7587 free_event(child_event);
7588 return NULL;
7589 }
7590
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007591 get_ctx(child_ctx);
7592
7593 /*
7594 * Make the child state follow the state of the parent event,
7595 * not its attr.disabled bit. We hold the parent's mutex,
7596 * so we won't race with perf_event_{en, dis}able_family.
7597 */
7598 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7599 child_event->state = PERF_EVENT_STATE_INACTIVE;
7600 else
7601 child_event->state = PERF_EVENT_STATE_OFF;
7602
7603 if (parent_event->attr.freq) {
7604 u64 sample_period = parent_event->hw.sample_period;
7605 struct hw_perf_event *hwc = &child_event->hw;
7606
7607 hwc->sample_period = sample_period;
7608 hwc->last_period = sample_period;
7609
7610 local64_set(&hwc->period_left, sample_period);
7611 }
7612
7613 child_event->ctx = child_ctx;
7614 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007615 child_event->overflow_handler_context
7616 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007617
7618 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007619 * Precalculate sample_data sizes
7620 */
7621 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007622 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007623
7624 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007625 * Link it up in the child's context:
7626 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007627 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007628 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007629 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007630
7631 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007632 * Link this into the parent event's child list
7633 */
7634 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7635 mutex_lock(&parent_event->child_mutex);
7636 list_add_tail(&child_event->child_list, &parent_event->child_list);
7637 mutex_unlock(&parent_event->child_mutex);
7638
7639 return child_event;
7640}
7641
7642static int inherit_group(struct perf_event *parent_event,
7643 struct task_struct *parent,
7644 struct perf_event_context *parent_ctx,
7645 struct task_struct *child,
7646 struct perf_event_context *child_ctx)
7647{
7648 struct perf_event *leader;
7649 struct perf_event *sub;
7650 struct perf_event *child_ctr;
7651
7652 leader = inherit_event(parent_event, parent, parent_ctx,
7653 child, NULL, child_ctx);
7654 if (IS_ERR(leader))
7655 return PTR_ERR(leader);
7656 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7657 child_ctr = inherit_event(sub, parent, parent_ctx,
7658 child, leader, child_ctx);
7659 if (IS_ERR(child_ctr))
7660 return PTR_ERR(child_ctr);
7661 }
7662 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007663}
7664
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007665static int
7666inherit_task_group(struct perf_event *event, struct task_struct *parent,
7667 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007668 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007669 int *inherited_all)
7670{
7671 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007672 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007673
7674 if (!event->attr.inherit) {
7675 *inherited_all = 0;
7676 return 0;
7677 }
7678
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007679 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007680 if (!child_ctx) {
7681 /*
7682 * This is executed from the parent task context, so
7683 * inherit events that have been marked for cloning.
7684 * First allocate and initialize a context for the
7685 * child.
7686 */
7687
Jiri Olsa734df5a2013-07-09 17:44:10 +02007688 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007689 if (!child_ctx)
7690 return -ENOMEM;
7691
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007692 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007693 }
7694
7695 ret = inherit_group(event, parent, parent_ctx,
7696 child, child_ctx);
7697
7698 if (ret)
7699 *inherited_all = 0;
7700
7701 return ret;
7702}
7703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007704/*
7705 * Initialize the perf_event context in task_struct
7706 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007707int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007708{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007709 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007710 struct perf_event_context *cloned_ctx;
7711 struct perf_event *event;
7712 struct task_struct *parent = current;
7713 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007714 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007715 int ret = 0;
7716
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007717 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007718 return 0;
7719
7720 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007721 * If the parent's context is a clone, pin it so it won't get
7722 * swapped under us.
7723 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007724 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007725
7726 /*
7727 * No need to check if parent_ctx != NULL here; since we saw
7728 * it non-NULL earlier, the only reason for it to become NULL
7729 * is if we exit, and since we're currently in the middle of
7730 * a fork we can't be exiting at the same time.
7731 */
7732
7733 /*
7734 * Lock the parent list. No need to lock the child - not PID
7735 * hashed yet and not running, so nobody can access it.
7736 */
7737 mutex_lock(&parent_ctx->mutex);
7738
7739 /*
7740 * We dont have to disable NMIs - we are only looking at
7741 * the list, not manipulating it:
7742 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007743 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007744 ret = inherit_task_group(event, parent, parent_ctx,
7745 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007746 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007747 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007748 }
7749
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007750 /*
7751 * We can't hold ctx->lock when iterating the ->flexible_group list due
7752 * to allocations, but we need to prevent rotation because
7753 * rotate_ctx() will change the list from interrupt context.
7754 */
7755 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7756 parent_ctx->rotate_disable = 1;
7757 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7758
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007759 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007760 ret = inherit_task_group(event, parent, parent_ctx,
7761 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007762 if (ret)
7763 break;
7764 }
7765
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007766 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7767 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007768
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007769 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007770
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007771 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007772 /*
7773 * Mark the child context as a clone of the parent
7774 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007775 *
7776 * Note that if the parent is a clone, the holding of
7777 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007778 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007779 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007780 if (cloned_ctx) {
7781 child_ctx->parent_ctx = cloned_ctx;
7782 child_ctx->parent_gen = parent_ctx->parent_gen;
7783 } else {
7784 child_ctx->parent_ctx = parent_ctx;
7785 child_ctx->parent_gen = parent_ctx->generation;
7786 }
7787 get_ctx(child_ctx->parent_ctx);
7788 }
7789
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007790 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007791 mutex_unlock(&parent_ctx->mutex);
7792
7793 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007794 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007795
7796 return ret;
7797}
7798
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007799/*
7800 * Initialize the perf_event context in task_struct
7801 */
7802int perf_event_init_task(struct task_struct *child)
7803{
7804 int ctxn, ret;
7805
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007806 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7807 mutex_init(&child->perf_event_mutex);
7808 INIT_LIST_HEAD(&child->perf_event_list);
7809
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007810 for_each_task_context_nr(ctxn) {
7811 ret = perf_event_init_context(child, ctxn);
7812 if (ret)
7813 return ret;
7814 }
7815
7816 return 0;
7817}
7818
Paul Mackerras220b1402010-03-10 20:45:52 +11007819static void __init perf_event_init_all_cpus(void)
7820{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007821 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007822 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007823
7824 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007825 swhash = &per_cpu(swevent_htable, cpu);
7826 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007827 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007828 }
7829}
7830
Paul Gortmaker0db06282013-06-19 14:53:51 -04007831static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007832{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007833 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007834
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007835 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007836 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007837 struct swevent_hlist *hlist;
7838
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007839 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7840 WARN_ON(!hlist);
7841 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007842 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007843 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007844}
7845
Peter Zijlstrac2774432010-12-08 15:29:02 +01007846#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007847static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007848{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007849 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7850
7851 WARN_ON(!irqs_disabled());
7852
7853 list_del_init(&cpuctx->rotation_list);
7854}
7855
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007856static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007857{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007858 struct perf_event_context *ctx = __info;
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007859 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007860
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007861 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007862
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007863 rcu_read_lock();
7864 list_for_each_entry_rcu(event, &ctx->event_list, event_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007865 __perf_remove_from_context(event);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007866 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007867}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007868
7869static void perf_event_exit_cpu_context(int cpu)
7870{
7871 struct perf_event_context *ctx;
7872 struct pmu *pmu;
7873 int idx;
7874
7875 idx = srcu_read_lock(&pmus_srcu);
7876 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007877 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007878
7879 mutex_lock(&ctx->mutex);
7880 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7881 mutex_unlock(&ctx->mutex);
7882 }
7883 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007884}
7885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007886static void perf_event_exit_cpu(int cpu)
7887{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007888 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007889
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007890 perf_event_exit_cpu_context(cpu);
7891
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007892 mutex_lock(&swhash->hlist_mutex);
7893 swevent_hlist_release(swhash);
7894 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007895}
7896#else
7897static inline void perf_event_exit_cpu(int cpu) { }
7898#endif
7899
Peter Zijlstrac2774432010-12-08 15:29:02 +01007900static int
7901perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7902{
7903 int cpu;
7904
7905 for_each_online_cpu(cpu)
7906 perf_event_exit_cpu(cpu);
7907
7908 return NOTIFY_OK;
7909}
7910
7911/*
7912 * Run the perf reboot notifier at the very last possible moment so that
7913 * the generic watchdog code runs as long as possible.
7914 */
7915static struct notifier_block perf_reboot_notifier = {
7916 .notifier_call = perf_reboot,
7917 .priority = INT_MIN,
7918};
7919
Paul Gortmaker0db06282013-06-19 14:53:51 -04007920static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007921perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7922{
7923 unsigned int cpu = (long)hcpu;
7924
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007925 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007926
7927 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007928 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007929 perf_event_init_cpu(cpu);
7930 break;
7931
Peter Zijlstra5e116372010-06-11 13:35:08 +02007932 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007933 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007934 perf_event_exit_cpu(cpu);
7935 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007936 default:
7937 break;
7938 }
7939
7940 return NOTIFY_OK;
7941}
7942
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007943void __init perf_event_init(void)
7944{
Jason Wessel3c502e72010-11-04 17:33:01 -05007945 int ret;
7946
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007947 idr_init(&pmu_idr);
7948
Paul Mackerras220b1402010-03-10 20:45:52 +11007949 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007950 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007951 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7952 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7953 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007954 perf_tp_register();
7955 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007956 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007957
7958 ret = init_hw_breakpoint();
7959 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007960
7961 /* do not patch jump label more than once per second */
7962 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007963
7964 /*
7965 * Build time assertion that we keep the data_head at the intended
7966 * location. IOW, validation we got the __reserved[] size right.
7967 */
7968 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7969 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007970}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007971
7972static int __init perf_event_sysfs_init(void)
7973{
7974 struct pmu *pmu;
7975 int ret;
7976
7977 mutex_lock(&pmus_lock);
7978
7979 ret = bus_register(&pmu_bus);
7980 if (ret)
7981 goto unlock;
7982
7983 list_for_each_entry(pmu, &pmus, entry) {
7984 if (!pmu->name || pmu->type < 0)
7985 continue;
7986
7987 ret = pmu_dev_alloc(pmu);
7988 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7989 }
7990 pmu_bus_running = 1;
7991 ret = 0;
7992
7993unlock:
7994 mutex_unlock(&pmus_lock);
7995
7996 return ret;
7997}
7998device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007999
8000#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008001static struct cgroup_subsys_state *
8002perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008003{
8004 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008005
Li Zefan1b15d052011-03-03 14:26:06 +08008006 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008007 if (!jc)
8008 return ERR_PTR(-ENOMEM);
8009
Stephane Eraniane5d13672011-02-14 11:20:01 +02008010 jc->info = alloc_percpu(struct perf_cgroup_info);
8011 if (!jc->info) {
8012 kfree(jc);
8013 return ERR_PTR(-ENOMEM);
8014 }
8015
Stephane Eraniane5d13672011-02-14 11:20:01 +02008016 return &jc->css;
8017}
8018
Tejun Heoeb954192013-08-08 20:11:23 -04008019static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008020{
Tejun Heoeb954192013-08-08 20:11:23 -04008021 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8022
Stephane Eraniane5d13672011-02-14 11:20:01 +02008023 free_percpu(jc->info);
8024 kfree(jc);
8025}
8026
8027static int __perf_cgroup_move(void *info)
8028{
8029 struct task_struct *task = info;
8030 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8031 return 0;
8032}
8033
Tejun Heoeb954192013-08-08 20:11:23 -04008034static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8035 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008036{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008037 struct task_struct *task;
8038
Tejun Heod99c8722013-08-08 20:11:27 -04008039 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008040 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008041}
8042
Tejun Heoeb954192013-08-08 20:11:23 -04008043static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8044 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008045 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008046{
8047 /*
8048 * cgroup_exit() is called in the copy_process() failure path.
8049 * Ignore this case since the task hasn't ran yet, this avoids
8050 * trying to poke a half freed task state from generic code.
8051 */
8052 if (!(task->flags & PF_EXITING))
8053 return;
8054
Tejun Heobb9d97b2011-12-12 18:12:21 -08008055 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008056}
8057
8058struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008059 .name = "perf_event",
8060 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08008061 .css_alloc = perf_cgroup_css_alloc,
8062 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008063 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008064 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008065};
8066#endif /* CONFIG_CGROUP_PERF */