blob: 5c87264730065f672a95f6cb4815fbc791421a22 [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
1400 event->state = PERF_EVENT_STATE_INACTIVE;
1401 if (event->pending_disable) {
1402 event->pending_disable = 0;
1403 event->state = PERF_EVENT_STATE_OFF;
1404 }
Stephane Eranian41587552011-01-03 18:20:01 +02001405 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001406 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 event->oncpu = -1;
1408
1409 if (!is_software_event(event))
1410 cpuctx->active_oncpu--;
1411 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001412 if (event->attr.freq && event->attr.sample_freq)
1413 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414 if (event->attr.exclusive || !cpuctx->active_oncpu)
1415 cpuctx->exclusive = 0;
1416}
1417
1418static void
1419group_sched_out(struct perf_event *group_event,
1420 struct perf_cpu_context *cpuctx,
1421 struct perf_event_context *ctx)
1422{
1423 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001424 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425
1426 event_sched_out(group_event, cpuctx, ctx);
1427
1428 /*
1429 * Schedule out siblings (if any):
1430 */
1431 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1432 event_sched_out(event, cpuctx, ctx);
1433
Stephane Eranianfa66f072010-08-26 16:40:01 +02001434 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435 cpuctx->exclusive = 0;
1436}
1437
1438/*
1439 * Cross CPU call to remove a performance event
1440 *
1441 * We disable the event on the hardware level first. After that we
1442 * remove it from the context list.
1443 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001444static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001446 struct perf_event *event = info;
1447 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001448 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001449
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001450 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001452 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001453 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1454 ctx->is_active = 0;
1455 cpuctx->task_ctx = NULL;
1456 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001457 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001458
1459 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001460}
1461
1462
1463/*
1464 * Remove the event from a task's (or a CPU's) list of events.
1465 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001466 * CPU events are removed with a smp call. For task events we only
1467 * call when the task is on a CPU.
1468 *
1469 * If event->ctx is a cloned context, callers must make sure that
1470 * every task struct that event->ctx->task could possibly point to
1471 * remains valid. This is OK when called from perf_release since
1472 * that only calls us on the top-level context, which can't be a clone.
1473 * When called from perf_event_exit_task, it's OK because the
1474 * context has been detached from its task.
1475 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001476static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477{
1478 struct perf_event_context *ctx = event->ctx;
1479 struct task_struct *task = ctx->task;
1480
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001481 lockdep_assert_held(&ctx->mutex);
1482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001483 if (!task) {
1484 /*
1485 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001486 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001488 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001489 return;
1490 }
1491
1492retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001493 if (!task_function_call(task, __perf_remove_from_context, event))
1494 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001496 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001498 * If we failed to find a running task, but find the context active now
1499 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001501 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001502 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503 goto retry;
1504 }
1505
1506 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001507 * Since the task isn't running, its safe to remove the event, us
1508 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001510 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001511 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001512}
1513
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001514/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001515 * Cross CPU call to disable a performance event
1516 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301517int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518{
1519 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001520 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001521 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001522
1523 /*
1524 * If this is a per-task event, need to check whether this
1525 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001526 *
1527 * Can trigger due to concurrent perf_event_context_sched_out()
1528 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001529 */
1530 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001531 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001532
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001533 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001534
1535 /*
1536 * If the event is on, turn it off.
1537 * If it is in error state, leave it in error state.
1538 */
1539 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1540 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001541 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001542 update_group_times(event);
1543 if (event == event->group_leader)
1544 group_sched_out(event, cpuctx, ctx);
1545 else
1546 event_sched_out(event, cpuctx, ctx);
1547 event->state = PERF_EVENT_STATE_OFF;
1548 }
1549
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001550 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001551
1552 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553}
1554
1555/*
1556 * Disable a event.
1557 *
1558 * If event->ctx is a cloned context, callers must make sure that
1559 * every task struct that event->ctx->task could possibly point to
1560 * remains valid. This condition is satisifed when called through
1561 * perf_event_for_each_child or perf_event_for_each because they
1562 * hold the top-level event's child_mutex, so any descendant that
1563 * goes to exit will block in sync_child_event.
1564 * When called from perf_pending_event it's OK because event->ctx
1565 * is the current context on this CPU and preemption is disabled,
1566 * hence we can't get into perf_event_task_sched_out for this context.
1567 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001568void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569{
1570 struct perf_event_context *ctx = event->ctx;
1571 struct task_struct *task = ctx->task;
1572
1573 if (!task) {
1574 /*
1575 * Disable the event on the cpu that it's on
1576 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001577 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578 return;
1579 }
1580
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001581retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001582 if (!task_function_call(task, __perf_event_disable, event))
1583 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001585 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001586 /*
1587 * If the event is still active, we need to retry the cross-call.
1588 */
1589 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001590 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001591 /*
1592 * Reload the task pointer, it might have been changed by
1593 * a concurrent perf_event_context_sched_out().
1594 */
1595 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001596 goto retry;
1597 }
1598
1599 /*
1600 * Since we have the lock this context can't be scheduled
1601 * in, so we can change the state safely.
1602 */
1603 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1604 update_group_times(event);
1605 event->state = PERF_EVENT_STATE_OFF;
1606 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001607 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001609EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610
Stephane Eraniane5d13672011-02-14 11:20:01 +02001611static void perf_set_shadow_time(struct perf_event *event,
1612 struct perf_event_context *ctx,
1613 u64 tstamp)
1614{
1615 /*
1616 * use the correct time source for the time snapshot
1617 *
1618 * We could get by without this by leveraging the
1619 * fact that to get to this function, the caller
1620 * has most likely already called update_context_time()
1621 * and update_cgrp_time_xx() and thus both timestamp
1622 * are identical (or very close). Given that tstamp is,
1623 * already adjusted for cgroup, we could say that:
1624 * tstamp - ctx->timestamp
1625 * is equivalent to
1626 * tstamp - cgrp->timestamp.
1627 *
1628 * Then, in perf_output_read(), the calculation would
1629 * work with no changes because:
1630 * - event is guaranteed scheduled in
1631 * - no scheduled out in between
1632 * - thus the timestamp would be the same
1633 *
1634 * But this is a bit hairy.
1635 *
1636 * So instead, we have an explicit cgroup call to remain
1637 * within the time time source all along. We believe it
1638 * is cleaner and simpler to understand.
1639 */
1640 if (is_cgroup_event(event))
1641 perf_cgroup_set_shadow_time(event, tstamp);
1642 else
1643 event->shadow_ctx_time = tstamp - ctx->timestamp;
1644}
1645
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001646#define MAX_INTERRUPTS (~0ULL)
1647
1648static void perf_log_throttle(struct perf_event *event, int enable);
1649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001651event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001653 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654{
Stephane Eranian41587552011-01-03 18:20:01 +02001655 u64 tstamp = perf_event_time(event);
1656
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 if (event->state <= PERF_EVENT_STATE_OFF)
1658 return 0;
1659
1660 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001661 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001662
1663 /*
1664 * Unthrottle events, since we scheduled we might have missed several
1665 * ticks already, also for a heavily scheduling task there is little
1666 * guarantee it'll get a tick in a timely manner.
1667 */
1668 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1669 perf_log_throttle(event, 1);
1670 event->hw.interrupts = 0;
1671 }
1672
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001673 /*
1674 * The new state must be visible before we turn it on in the hardware:
1675 */
1676 smp_wmb();
1677
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001678 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679 event->state = PERF_EVENT_STATE_INACTIVE;
1680 event->oncpu = -1;
1681 return -EAGAIN;
1682 }
1683
Stephane Eranian41587552011-01-03 18:20:01 +02001684 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001685
Stephane Eraniane5d13672011-02-14 11:20:01 +02001686 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688 if (!is_software_event(event))
1689 cpuctx->active_oncpu++;
1690 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001691 if (event->attr.freq && event->attr.sample_freq)
1692 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693
1694 if (event->attr.exclusive)
1695 cpuctx->exclusive = 1;
1696
1697 return 0;
1698}
1699
1700static int
1701group_sched_in(struct perf_event *group_event,
1702 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001703 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704{
Lin Ming6bde9b62010-04-23 13:56:00 +08001705 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001706 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001707 u64 now = ctx->time;
1708 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709
1710 if (group_event->state == PERF_EVENT_STATE_OFF)
1711 return 0;
1712
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001713 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001714
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001715 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001716 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001717 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001719 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720
1721 /*
1722 * Schedule in siblings as one group (if any):
1723 */
1724 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001725 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726 partial_group = event;
1727 goto group_error;
1728 }
1729 }
1730
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001731 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001732 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734group_error:
1735 /*
1736 * Groups can be scheduled in as one unit only, so undo any
1737 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001738 * The events up to the failed event are scheduled out normally,
1739 * tstamp_stopped will be updated.
1740 *
1741 * The failed events and the remaining siblings need to have
1742 * their timings updated as if they had gone thru event_sched_in()
1743 * and event_sched_out(). This is required to get consistent timings
1744 * across the group. This also takes care of the case where the group
1745 * could never be scheduled by ensuring tstamp_stopped is set to mark
1746 * the time the event was actually stopped, such that time delta
1747 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748 */
1749 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1750 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001751 simulate = true;
1752
1753 if (simulate) {
1754 event->tstamp_running += now - event->tstamp_stopped;
1755 event->tstamp_stopped = now;
1756 } else {
1757 event_sched_out(event, cpuctx, ctx);
1758 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001759 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001760 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001762 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001763
Stephane Eranian9e630202013-04-03 14:21:33 +02001764 perf_cpu_hrtimer_restart(cpuctx);
1765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766 return -EAGAIN;
1767}
1768
1769/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 * Work out whether we can put this event group on the CPU now.
1771 */
1772static int group_can_go_on(struct perf_event *event,
1773 struct perf_cpu_context *cpuctx,
1774 int can_add_hw)
1775{
1776 /*
1777 * Groups consisting entirely of software events can always go on.
1778 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001779 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780 return 1;
1781 /*
1782 * If an exclusive group is already on, no other hardware
1783 * events can go on.
1784 */
1785 if (cpuctx->exclusive)
1786 return 0;
1787 /*
1788 * If this group is exclusive and there are already
1789 * events on the CPU, it can't go on.
1790 */
1791 if (event->attr.exclusive && cpuctx->active_oncpu)
1792 return 0;
1793 /*
1794 * Otherwise, try to add it if all previous groups were able
1795 * to go on.
1796 */
1797 return can_add_hw;
1798}
1799
1800static void add_event_to_ctx(struct perf_event *event,
1801 struct perf_event_context *ctx)
1802{
Stephane Eranian41587552011-01-03 18:20:01 +02001803 u64 tstamp = perf_event_time(event);
1804
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001806 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001807 event->tstamp_enabled = tstamp;
1808 event->tstamp_running = tstamp;
1809 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001810}
1811
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001812static void task_ctx_sched_out(struct perf_event_context *ctx);
1813static void
1814ctx_sched_in(struct perf_event_context *ctx,
1815 struct perf_cpu_context *cpuctx,
1816 enum event_type_t event_type,
1817 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001818
Peter Zijlstradce58552011-04-09 21:17:46 +02001819static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1820 struct perf_event_context *ctx,
1821 struct task_struct *task)
1822{
1823 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1824 if (ctx)
1825 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1826 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1827 if (ctx)
1828 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1829}
1830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831/*
1832 * Cross CPU call to install and enable a performance event
1833 *
1834 * Must be called with ctx->mutex held
1835 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001836static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838 struct perf_event *event = info;
1839 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001840 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001841 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1842 struct task_struct *task = current;
1843
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001844 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001845 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846
1847 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001848 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001850 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001851 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001852
1853 /*
1854 * If the context we're installing events in is not the
1855 * active task_ctx, flip them.
1856 */
1857 if (ctx->task && task_ctx != ctx) {
1858 if (task_ctx)
1859 raw_spin_unlock(&task_ctx->lock);
1860 raw_spin_lock(&ctx->lock);
1861 task_ctx = ctx;
1862 }
1863
1864 if (task_ctx) {
1865 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001866 task = task_ctx->task;
1867 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001868
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001869 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001872 /*
1873 * update cgrp time only if current cgrp
1874 * matches event->cgrp. Must be done before
1875 * calling add_event_to_ctx()
1876 */
1877 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001879 add_event_to_ctx(event, ctx);
1880
1881 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001882 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001884 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001885
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001886 perf_pmu_enable(cpuctx->ctx.pmu);
1887 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001888
1889 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890}
1891
1892/*
1893 * Attach a performance event to a context
1894 *
1895 * First we add the event to the list with the hardware enable bit
1896 * in event->hw_config cleared.
1897 *
1898 * If the event is attached to a task which is on a CPU we use a smp
1899 * call to enable it in the task context. The task might have been
1900 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901 */
1902static void
1903perf_install_in_context(struct perf_event_context *ctx,
1904 struct perf_event *event,
1905 int cpu)
1906{
1907 struct task_struct *task = ctx->task;
1908
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001909 lockdep_assert_held(&ctx->mutex);
1910
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001911 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001912 if (event->cpu != -1)
1913 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915 if (!task) {
1916 /*
1917 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001918 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001920 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001921 return;
1922 }
1923
1924retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001925 if (!task_function_call(task, __perf_install_in_context, event))
1926 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001927
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001928 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001930 * If we failed to find a running task, but find the context active now
1931 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001933 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001934 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935 goto retry;
1936 }
1937
1938 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001939 * Since the task isn't running, its safe to add the event, us holding
1940 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001942 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001943 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944}
1945
1946/*
1947 * Put a event into inactive state and update time fields.
1948 * Enabling the leader of a group effectively enables all
1949 * the group members that aren't explicitly disabled, so we
1950 * have to update their ->tstamp_enabled also.
1951 * Note: this works for group members as well as group leaders
1952 * since the non-leader members' sibling_lists will be empty.
1953 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001954static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001955{
1956 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001957 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958
1959 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001960 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001961 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001962 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1963 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001964 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965}
1966
1967/*
1968 * Cross CPU call to enable a performance event
1969 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001970static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001971{
1972 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973 struct perf_event_context *ctx = event->ctx;
1974 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001975 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976 int err;
1977
Jiri Olsa06f41792013-07-09 17:44:11 +02001978 /*
1979 * There's a time window between 'ctx->is_active' check
1980 * in perf_event_enable function and this place having:
1981 * - IRQs on
1982 * - ctx->lock unlocked
1983 *
1984 * where the task could be killed and 'ctx' deactivated
1985 * by perf_event_exit_task.
1986 */
1987 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001988 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001990 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991 update_context_time(ctx);
1992
1993 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1994 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001995
1996 /*
1997 * set current task's cgroup time reference point
1998 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001999 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002000
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002001 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002
Stephane Eraniane5d13672011-02-14 11:20:01 +02002003 if (!event_filter_match(event)) {
2004 if (is_cgroup_event(event))
2005 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002006 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002007 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009 /*
2010 * If the event is in a group and isn't the group leader,
2011 * then don't put it on unless the group is on.
2012 */
2013 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2014 goto unlock;
2015
2016 if (!group_can_go_on(event, cpuctx, 1)) {
2017 err = -EEXIST;
2018 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002020 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002022 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002023 }
2024
2025 if (err) {
2026 /*
2027 * If this event can't go on and it's part of a
2028 * group, then the whole group has to come off.
2029 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002030 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002031 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002032 perf_cpu_hrtimer_restart(cpuctx);
2033 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 if (leader->attr.pinned) {
2035 update_group_times(leader);
2036 leader->state = PERF_EVENT_STATE_ERROR;
2037 }
2038 }
2039
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002040unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002041 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002042
2043 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002044}
2045
2046/*
2047 * Enable a event.
2048 *
2049 * If event->ctx is a cloned context, callers must make sure that
2050 * every task struct that event->ctx->task could possibly point to
2051 * remains valid. This condition is satisfied when called through
2052 * perf_event_for_each_child or perf_event_for_each as described
2053 * for perf_event_disable.
2054 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002055void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056{
2057 struct perf_event_context *ctx = event->ctx;
2058 struct task_struct *task = ctx->task;
2059
2060 if (!task) {
2061 /*
2062 * Enable the event on the cpu that it's on
2063 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002064 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 return;
2066 }
2067
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002068 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2070 goto out;
2071
2072 /*
2073 * If the event is in error state, clear that first.
2074 * That way, if we see the event in error state below, we
2075 * know that it has gone back into error state, as distinct
2076 * from the task having been scheduled away before the
2077 * cross-call arrived.
2078 */
2079 if (event->state == PERF_EVENT_STATE_ERROR)
2080 event->state = PERF_EVENT_STATE_OFF;
2081
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002082retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002083 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002084 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002085 goto out;
2086 }
2087
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002088 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002089
2090 if (!task_function_call(task, __perf_event_enable, event))
2091 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002093 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094
2095 /*
2096 * If the context is active and the event is still off,
2097 * we need to retry the cross-call.
2098 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002099 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2100 /*
2101 * task could have been flipped by a concurrent
2102 * perf_event_context_sched_out()
2103 */
2104 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002106 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002108out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002109 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002110}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002111EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112
Avi Kivity26ca5c12011-06-29 18:42:37 +03002113int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114{
2115 /*
2116 * not supported on inherited events
2117 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002118 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002119 return -EINVAL;
2120
2121 atomic_add(refresh, &event->event_limit);
2122 perf_event_enable(event);
2123
2124 return 0;
2125}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002126EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002128static void ctx_sched_out(struct perf_event_context *ctx,
2129 struct perf_cpu_context *cpuctx,
2130 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131{
2132 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002133 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002134
Peter Zijlstradb24d332011-04-09 21:17:45 +02002135 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002137 return;
2138
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002139 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002140 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002141 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002142 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002143
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002144 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002145 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002146 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2147 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002148 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002149
Peter Zijlstradb24d332011-04-09 21:17:45 +02002150 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002151 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002152 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002153 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002154 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155}
2156
2157/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002158 * Test whether two contexts are equivalent, i.e. whether they have both been
2159 * cloned from the same version of the same context.
2160 *
2161 * Equivalence is measured using a generation number in the context that is
2162 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2163 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164 */
2165static int context_equiv(struct perf_event_context *ctx1,
2166 struct perf_event_context *ctx2)
2167{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002168 /* Pinning disables the swap optimization */
2169 if (ctx1->pin_count || ctx2->pin_count)
2170 return 0;
2171
2172 /* If ctx1 is the parent of ctx2 */
2173 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2174 return 1;
2175
2176 /* If ctx2 is the parent of ctx1 */
2177 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2178 return 1;
2179
2180 /*
2181 * If ctx1 and ctx2 have the same parent; we flatten the parent
2182 * hierarchy, see perf_event_init_context().
2183 */
2184 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2185 ctx1->parent_gen == ctx2->parent_gen)
2186 return 1;
2187
2188 /* Unmatched */
2189 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190}
2191
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002192static void __perf_event_sync_stat(struct perf_event *event,
2193 struct perf_event *next_event)
2194{
2195 u64 value;
2196
2197 if (!event->attr.inherit_stat)
2198 return;
2199
2200 /*
2201 * Update the event value, we cannot use perf_event_read()
2202 * because we're in the middle of a context switch and have IRQs
2203 * disabled, which upsets smp_call_function_single(), however
2204 * we know the event must be on the current CPU, therefore we
2205 * don't need to use it.
2206 */
2207 switch (event->state) {
2208 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002209 event->pmu->read(event);
2210 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002211
2212 case PERF_EVENT_STATE_INACTIVE:
2213 update_event_times(event);
2214 break;
2215
2216 default:
2217 break;
2218 }
2219
2220 /*
2221 * In order to keep per-task stats reliable we need to flip the event
2222 * values when we flip the contexts.
2223 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002224 value = local64_read(&next_event->count);
2225 value = local64_xchg(&event->count, value);
2226 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002227
2228 swap(event->total_time_enabled, next_event->total_time_enabled);
2229 swap(event->total_time_running, next_event->total_time_running);
2230
2231 /*
2232 * Since we swizzled the values, update the user visible data too.
2233 */
2234 perf_event_update_userpage(event);
2235 perf_event_update_userpage(next_event);
2236}
2237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238static void perf_event_sync_stat(struct perf_event_context *ctx,
2239 struct perf_event_context *next_ctx)
2240{
2241 struct perf_event *event, *next_event;
2242
2243 if (!ctx->nr_stat)
2244 return;
2245
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002246 update_context_time(ctx);
2247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248 event = list_first_entry(&ctx->event_list,
2249 struct perf_event, event_entry);
2250
2251 next_event = list_first_entry(&next_ctx->event_list,
2252 struct perf_event, event_entry);
2253
2254 while (&event->event_entry != &ctx->event_list &&
2255 &next_event->event_entry != &next_ctx->event_list) {
2256
2257 __perf_event_sync_stat(event, next_event);
2258
2259 event = list_next_entry(event, event_entry);
2260 next_event = list_next_entry(next_event, event_entry);
2261 }
2262}
2263
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002264static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2265 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002267 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002269 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002270 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271 int do_switch = 1;
2272
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002273 if (likely(!ctx))
2274 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002276 cpuctx = __get_cpu_context(ctx);
2277 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278 return;
2279
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002281 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002282 if (!next_ctx)
2283 goto unlock;
2284
2285 parent = rcu_dereference(ctx->parent_ctx);
2286 next_parent = rcu_dereference(next_ctx->parent_ctx);
2287
2288 /* If neither context have a parent context; they cannot be clones. */
2289 if (!parent && !next_parent)
2290 goto unlock;
2291
2292 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 /*
2294 * Looks like the two contexts are clones, so we might be
2295 * able to optimize the context switch. We lock both
2296 * contexts and check that they are clones under the
2297 * lock (including re-checking that neither has been
2298 * uncloned in the meantime). It doesn't matter which
2299 * order we take the locks because no other cpu could
2300 * be trying to lock both of these tasks.
2301 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002302 raw_spin_lock(&ctx->lock);
2303 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304 if (context_equiv(ctx, next_ctx)) {
2305 /*
2306 * XXX do we need a memory barrier of sorts
2307 * wrt to rcu_dereference() of perf_event_ctxp
2308 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002309 task->perf_event_ctxp[ctxn] = next_ctx;
2310 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311 ctx->task = next;
2312 next_ctx->task = task;
2313 do_switch = 0;
2314
2315 perf_event_sync_stat(ctx, next_ctx);
2316 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002317 raw_spin_unlock(&next_ctx->lock);
2318 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002320unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321 rcu_read_unlock();
2322
2323 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002324 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002325 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002327 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328 }
2329}
2330
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002331#define for_each_task_context_nr(ctxn) \
2332 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2333
2334/*
2335 * Called from scheduler to remove the events of the current task,
2336 * with interrupts disabled.
2337 *
2338 * We stop each event and update the event value in event->count.
2339 *
2340 * This does not protect us against NMI, but disable()
2341 * sets the disabled bit in the control field of event _before_
2342 * accessing the event control register. If a NMI hits, then it will
2343 * not restart the event.
2344 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002345void __perf_event_task_sched_out(struct task_struct *task,
2346 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002347{
2348 int ctxn;
2349
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002350 for_each_task_context_nr(ctxn)
2351 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002352
2353 /*
2354 * if cgroup events exist on this CPU, then we need
2355 * to check if we have to switch out PMU state.
2356 * cgroup event are system-wide mode only
2357 */
2358 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002359 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002360}
2361
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002362static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002364 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365
2366 if (!cpuctx->task_ctx)
2367 return;
2368
2369 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2370 return;
2371
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002372 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373 cpuctx->task_ctx = NULL;
2374}
2375
2376/*
2377 * Called with IRQs disabled
2378 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002379static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2380 enum event_type_t event_type)
2381{
2382 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383}
2384
2385static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002386ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002387 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388{
2389 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002391 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2392 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002394 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395 continue;
2396
Stephane Eraniane5d13672011-02-14 11:20:01 +02002397 /* may need to reset tstamp_enabled */
2398 if (is_cgroup_event(event))
2399 perf_cgroup_mark_enabled(event, ctx);
2400
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002401 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002402 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002403
2404 /*
2405 * If this pinned group hasn't been scheduled,
2406 * put it in error state.
2407 */
2408 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2409 update_group_times(event);
2410 event->state = PERF_EVENT_STATE_ERROR;
2411 }
2412 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002413}
2414
2415static void
2416ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002417 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002418{
2419 struct perf_event *event;
2420 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002422 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2423 /* Ignore events in OFF or ERROR state */
2424 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002425 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426 /*
2427 * Listen to the 'cpu' scheduling filter constraint
2428 * of events:
2429 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002430 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431 continue;
2432
Stephane Eraniane5d13672011-02-14 11:20:01 +02002433 /* may need to reset tstamp_enabled */
2434 if (is_cgroup_event(event))
2435 perf_cgroup_mark_enabled(event, ctx);
2436
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002437 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002438 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002439 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002440 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002441 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002442}
2443
2444static void
2445ctx_sched_in(struct perf_event_context *ctx,
2446 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002447 enum event_type_t event_type,
2448 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002449{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002450 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002451 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002452
Peter Zijlstradb24d332011-04-09 21:17:45 +02002453 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002454 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002455 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002456
Stephane Eraniane5d13672011-02-14 11:20:01 +02002457 now = perf_clock();
2458 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002459 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002460 /*
2461 * First go through the list and put on any pinned groups
2462 * in order to give them the best chance of going on.
2463 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002464 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002465 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002466
2467 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002468 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002469 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470}
2471
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002472static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002473 enum event_type_t event_type,
2474 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002475{
2476 struct perf_event_context *ctx = &cpuctx->ctx;
2477
Stephane Eraniane5d13672011-02-14 11:20:01 +02002478 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002479}
2480
Stephane Eraniane5d13672011-02-14 11:20:01 +02002481static void perf_event_context_sched_in(struct perf_event_context *ctx,
2482 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002483{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002484 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002485
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002486 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002487 if (cpuctx->task_ctx == ctx)
2488 return;
2489
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002490 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002491 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002492 /*
2493 * We want to keep the following priority order:
2494 * cpu pinned (that don't need to move), task pinned,
2495 * cpu flexible, task flexible.
2496 */
2497 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2498
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002499 if (ctx->nr_events)
2500 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002501
Gleb Natapov86b47c22011-11-22 16:08:21 +02002502 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2503
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002504 perf_pmu_enable(ctx->pmu);
2505 perf_ctx_unlock(cpuctx, ctx);
2506
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002507 /*
2508 * Since these rotations are per-cpu, we need to ensure the
2509 * cpu-context we got scheduled on is actually rotating.
2510 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002511 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512}
2513
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002514/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002515 * When sampling the branck stack in system-wide, it may be necessary
2516 * to flush the stack on context switch. This happens when the branch
2517 * stack does not tag its entries with the pid of the current task.
2518 * Otherwise it becomes impossible to associate a branch entry with a
2519 * task. This ambiguity is more likely to appear when the branch stack
2520 * supports priv level filtering and the user sets it to monitor only
2521 * at the user level (which could be a useful measurement in system-wide
2522 * mode). In that case, the risk is high of having a branch stack with
2523 * branch from multiple tasks. Flushing may mean dropping the existing
2524 * entries or stashing them somewhere in the PMU specific code layer.
2525 *
2526 * This function provides the context switch callback to the lower code
2527 * layer. It is invoked ONLY when there is at least one system-wide context
2528 * with at least one active event using taken branch sampling.
2529 */
2530static void perf_branch_stack_sched_in(struct task_struct *prev,
2531 struct task_struct *task)
2532{
2533 struct perf_cpu_context *cpuctx;
2534 struct pmu *pmu;
2535 unsigned long flags;
2536
2537 /* no need to flush branch stack if not changing task */
2538 if (prev == task)
2539 return;
2540
2541 local_irq_save(flags);
2542
2543 rcu_read_lock();
2544
2545 list_for_each_entry_rcu(pmu, &pmus, entry) {
2546 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2547
2548 /*
2549 * check if the context has at least one
2550 * event using PERF_SAMPLE_BRANCH_STACK
2551 */
2552 if (cpuctx->ctx.nr_branch_stack > 0
2553 && pmu->flush_branch_stack) {
2554
2555 pmu = cpuctx->ctx.pmu;
2556
2557 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2558
2559 perf_pmu_disable(pmu);
2560
2561 pmu->flush_branch_stack();
2562
2563 perf_pmu_enable(pmu);
2564
2565 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2566 }
2567 }
2568
2569 rcu_read_unlock();
2570
2571 local_irq_restore(flags);
2572}
2573
2574/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002575 * Called from scheduler to add the events of the current task
2576 * with interrupts disabled.
2577 *
2578 * We restore the event value and then enable it.
2579 *
2580 * This does not protect us against NMI, but enable()
2581 * sets the enabled bit in the control field of event _before_
2582 * accessing the event control register. If a NMI hits, then it will
2583 * keep the event running.
2584 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002585void __perf_event_task_sched_in(struct task_struct *prev,
2586 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002587{
2588 struct perf_event_context *ctx;
2589 int ctxn;
2590
2591 for_each_task_context_nr(ctxn) {
2592 ctx = task->perf_event_ctxp[ctxn];
2593 if (likely(!ctx))
2594 continue;
2595
Stephane Eraniane5d13672011-02-14 11:20:01 +02002596 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002597 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002598 /*
2599 * if cgroup events exist on this CPU, then we need
2600 * to check if we have to switch in PMU state.
2601 * cgroup event are system-wide mode only
2602 */
2603 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002604 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002605
2606 /* check for system-wide branch_stack events */
2607 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2608 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002609}
2610
Peter Zijlstraabd50712010-01-26 18:50:16 +01002611static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2612{
2613 u64 frequency = event->attr.sample_freq;
2614 u64 sec = NSEC_PER_SEC;
2615 u64 divisor, dividend;
2616
2617 int count_fls, nsec_fls, frequency_fls, sec_fls;
2618
2619 count_fls = fls64(count);
2620 nsec_fls = fls64(nsec);
2621 frequency_fls = fls64(frequency);
2622 sec_fls = 30;
2623
2624 /*
2625 * We got @count in @nsec, with a target of sample_freq HZ
2626 * the target period becomes:
2627 *
2628 * @count * 10^9
2629 * period = -------------------
2630 * @nsec * sample_freq
2631 *
2632 */
2633
2634 /*
2635 * Reduce accuracy by one bit such that @a and @b converge
2636 * to a similar magnitude.
2637 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002638#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002639do { \
2640 if (a##_fls > b##_fls) { \
2641 a >>= 1; \
2642 a##_fls--; \
2643 } else { \
2644 b >>= 1; \
2645 b##_fls--; \
2646 } \
2647} while (0)
2648
2649 /*
2650 * Reduce accuracy until either term fits in a u64, then proceed with
2651 * the other, so that finally we can do a u64/u64 division.
2652 */
2653 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2654 REDUCE_FLS(nsec, frequency);
2655 REDUCE_FLS(sec, count);
2656 }
2657
2658 if (count_fls + sec_fls > 64) {
2659 divisor = nsec * frequency;
2660
2661 while (count_fls + sec_fls > 64) {
2662 REDUCE_FLS(count, sec);
2663 divisor >>= 1;
2664 }
2665
2666 dividend = count * sec;
2667 } else {
2668 dividend = count * sec;
2669
2670 while (nsec_fls + frequency_fls > 64) {
2671 REDUCE_FLS(nsec, frequency);
2672 dividend >>= 1;
2673 }
2674
2675 divisor = nsec * frequency;
2676 }
2677
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002678 if (!divisor)
2679 return dividend;
2680
Peter Zijlstraabd50712010-01-26 18:50:16 +01002681 return div64_u64(dividend, divisor);
2682}
2683
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002684static DEFINE_PER_CPU(int, perf_throttled_count);
2685static DEFINE_PER_CPU(u64, perf_throttled_seq);
2686
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002687static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002688{
2689 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002690 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691 s64 delta;
2692
Peter Zijlstraabd50712010-01-26 18:50:16 +01002693 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002694
2695 delta = (s64)(period - hwc->sample_period);
2696 delta = (delta + 7) / 8; /* low pass filter */
2697
2698 sample_period = hwc->sample_period + delta;
2699
2700 if (!sample_period)
2701 sample_period = 1;
2702
2703 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002704
Peter Zijlstrae7850592010-05-21 14:43:08 +02002705 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002706 if (disable)
2707 event->pmu->stop(event, PERF_EF_UPDATE);
2708
Peter Zijlstrae7850592010-05-21 14:43:08 +02002709 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002710
2711 if (disable)
2712 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002713 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714}
2715
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002716/*
2717 * combine freq adjustment with unthrottling to avoid two passes over the
2718 * events. At the same time, make sure, having freq events does not change
2719 * the rate of unthrottling as that would introduce bias.
2720 */
2721static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2722 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002723{
2724 struct perf_event *event;
2725 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002726 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002727 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002729 /*
2730 * only need to iterate over all events iff:
2731 * - context have events in frequency mode (needs freq adjust)
2732 * - there are events to unthrottle on this cpu
2733 */
2734 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002735 return;
2736
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002737 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002738 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002739
Paul Mackerras03541f82009-10-14 16:58:03 +11002740 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741 if (event->state != PERF_EVENT_STATE_ACTIVE)
2742 continue;
2743
Stephane Eranian5632ab12011-01-03 18:20:01 +02002744 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002745 continue;
2746
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747 hwc = &event->hw;
2748
Jiri Olsaae23bff2013-08-24 16:45:54 +02002749 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002750 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002752 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002753 }
2754
2755 if (!event->attr.freq || !event->attr.sample_freq)
2756 continue;
2757
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002758 /*
2759 * stop the event and update event->count
2760 */
2761 event->pmu->stop(event, PERF_EF_UPDATE);
2762
Peter Zijlstrae7850592010-05-21 14:43:08 +02002763 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002764 delta = now - hwc->freq_count_stamp;
2765 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002767 /*
2768 * restart the event
2769 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002770 * we have stopped the event so tell that
2771 * to perf_adjust_period() to avoid stopping it
2772 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002773 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002774 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002775 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002776
2777 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002779
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002780 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002781 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782}
2783
2784/*
2785 * Round-robin a context's events:
2786 */
2787static void rotate_ctx(struct perf_event_context *ctx)
2788{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002789 /*
2790 * Rotate the first entry last of non-pinned groups. Rotation might be
2791 * disabled by the inheritance code.
2792 */
2793 if (!ctx->rotate_disable)
2794 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002795}
2796
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002797/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002798 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2799 * because they're strictly cpu affine and rotate_start is called with IRQs
2800 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002801 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002802static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002804 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002805 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002806
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002807 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002808 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002809 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2810 rotate = 1;
2811 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002812
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002813 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002814 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002815 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002816 if (ctx->nr_events != ctx->nr_active)
2817 rotate = 1;
2818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002819
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002820 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002821 goto done;
2822
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002823 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002824 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002825
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002826 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2827 if (ctx)
2828 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002829
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002830 rotate_ctx(&cpuctx->ctx);
2831 if (ctx)
2832 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002833
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002834 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002835
2836 perf_pmu_enable(cpuctx->ctx.pmu);
2837 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002838done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002839 if (remove)
2840 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002841
2842 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002843}
2844
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002845#ifdef CONFIG_NO_HZ_FULL
2846bool perf_event_can_stop_tick(void)
2847{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002848 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002849 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002850 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002851 else
2852 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002853}
2854#endif
2855
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002856void perf_event_task_tick(void)
2857{
2858 struct list_head *head = &__get_cpu_var(rotation_list);
2859 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002860 struct perf_event_context *ctx;
2861 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002862
2863 WARN_ON(!irqs_disabled());
2864
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002865 __this_cpu_inc(perf_throttled_seq);
2866 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2867
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002868 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002869 ctx = &cpuctx->ctx;
2870 perf_adjust_freq_unthr_context(ctx, throttled);
2871
2872 ctx = cpuctx->task_ctx;
2873 if (ctx)
2874 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002875 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876}
2877
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002878static int event_enable_on_exec(struct perf_event *event,
2879 struct perf_event_context *ctx)
2880{
2881 if (!event->attr.enable_on_exec)
2882 return 0;
2883
2884 event->attr.enable_on_exec = 0;
2885 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2886 return 0;
2887
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002888 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002889
2890 return 1;
2891}
2892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002893/*
2894 * Enable all of a task's events that have been marked enable-on-exec.
2895 * This expects task == current.
2896 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002897static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002899 struct perf_event *event;
2900 unsigned long flags;
2901 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002902 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903
2904 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 if (!ctx || !ctx->nr_events)
2906 goto out;
2907
Stephane Eraniane566b762011-04-06 02:54:54 +02002908 /*
2909 * We must ctxsw out cgroup events to avoid conflict
2910 * when invoking perf_task_event_sched_in() later on
2911 * in this function. Otherwise we end up trying to
2912 * ctxswin cgroup events which are already scheduled
2913 * in.
2914 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002915 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002917 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002918 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002920 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002921 ret = event_enable_on_exec(event, ctx);
2922 if (ret)
2923 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002924 }
2925
2926 /*
2927 * Unclone this context if we enabled any event.
2928 */
2929 if (enabled)
2930 unclone_ctx(ctx);
2931
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002932 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002933
Stephane Eraniane566b762011-04-06 02:54:54 +02002934 /*
2935 * Also calls ctxswin for cgroup events, if any:
2936 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002937 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002938out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002939 local_irq_restore(flags);
2940}
2941
2942/*
2943 * Cross CPU call to read the hardware event
2944 */
2945static void __perf_event_read(void *info)
2946{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947 struct perf_event *event = info;
2948 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002949 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950
2951 /*
2952 * If this is a task context, we need to check whether it is
2953 * the current task context of this cpu. If not it has been
2954 * scheduled out before the smp call arrived. In that case
2955 * event->count would have been updated to a recent sample
2956 * when the event was scheduled out.
2957 */
2958 if (ctx->task && cpuctx->task_ctx != ctx)
2959 return;
2960
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002961 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002962 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002963 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002964 update_cgrp_time_from_event(event);
2965 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002967 if (event->state == PERF_EVENT_STATE_ACTIVE)
2968 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002969 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970}
2971
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002972static inline u64 perf_event_count(struct perf_event *event)
2973{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002974 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002975}
2976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002977static u64 perf_event_read(struct perf_event *event)
2978{
2979 /*
2980 * If event is enabled and currently active on a CPU, update the
2981 * value in the event structure:
2982 */
2983 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2984 smp_call_function_single(event->oncpu,
2985 __perf_event_read, event, 1);
2986 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002987 struct perf_event_context *ctx = event->ctx;
2988 unsigned long flags;
2989
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002990 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002991 /*
2992 * may read while context is not active
2993 * (e.g., thread is blocked), in that case
2994 * we cannot update context time
2995 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002996 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002997 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002998 update_cgrp_time_from_event(event);
2999 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003000 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003001 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002 }
3003
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003004 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005}
3006
3007/*
3008 * Initialize the perf_event context in a task_struct:
3009 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003010static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003012 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003013 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003014 INIT_LIST_HEAD(&ctx->pinned_groups);
3015 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003016 INIT_LIST_HEAD(&ctx->event_list);
3017 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003018}
3019
Peter Zijlstraeb184472010-09-07 15:55:13 +02003020static struct perf_event_context *
3021alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022{
3023 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003024
3025 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3026 if (!ctx)
3027 return NULL;
3028
3029 __perf_event_init_context(ctx);
3030 if (task) {
3031 ctx->task = task;
3032 get_task_struct(task);
3033 }
3034 ctx->pmu = pmu;
3035
3036 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037}
3038
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003039static struct task_struct *
3040find_lively_task_by_vpid(pid_t vpid)
3041{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043 int err;
3044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003046 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047 task = current;
3048 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003049 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050 if (task)
3051 get_task_struct(task);
3052 rcu_read_unlock();
3053
3054 if (!task)
3055 return ERR_PTR(-ESRCH);
3056
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057 /* Reuse ptrace permission checks for now. */
3058 err = -EACCES;
3059 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3060 goto errout;
3061
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003062 return task;
3063errout:
3064 put_task_struct(task);
3065 return ERR_PTR(err);
3066
3067}
3068
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003069/*
3070 * Returns a matching context with refcount and pincount.
3071 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003072static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003073find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074{
3075 struct perf_event_context *ctx;
3076 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003078 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003080 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081 /* Must be root to operate on a CPU event: */
3082 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3083 return ERR_PTR(-EACCES);
3084
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003085 /*
3086 * We could be clever and allow to attach a event to an
3087 * offline CPU and activate it when the CPU comes up, but
3088 * that's for later.
3089 */
3090 if (!cpu_online(cpu))
3091 return ERR_PTR(-ENODEV);
3092
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003093 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003094 ctx = &cpuctx->ctx;
3095 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003096 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003097
3098 return ctx;
3099 }
3100
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003101 err = -EINVAL;
3102 ctxn = pmu->task_ctx_nr;
3103 if (ctxn < 0)
3104 goto errout;
3105
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003106retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003107 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 if (ctx) {
3109 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003110 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003111 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003112 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003113 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114 err = -ENOMEM;
3115 if (!ctx)
3116 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003117
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003118 err = 0;
3119 mutex_lock(&task->perf_event_mutex);
3120 /*
3121 * If it has already passed perf_event_exit_task().
3122 * we must see PF_EXITING, it takes this mutex too.
3123 */
3124 if (task->flags & PF_EXITING)
3125 err = -ESRCH;
3126 else if (task->perf_event_ctxp[ctxn])
3127 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003128 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003129 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003130 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003131 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003132 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003133 mutex_unlock(&task->perf_event_mutex);
3134
3135 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003136 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003137
3138 if (err == -EAGAIN)
3139 goto retry;
3140 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142 }
3143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144 return ctx;
3145
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003146errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003147 return ERR_PTR(err);
3148}
3149
Li Zefan6fb29152009-10-15 11:21:42 +08003150static void perf_event_free_filter(struct perf_event *event);
3151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152static void free_event_rcu(struct rcu_head *head)
3153{
3154 struct perf_event *event;
3155
3156 event = container_of(head, struct perf_event, rcu_head);
3157 if (event->ns)
3158 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003159 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003160 kfree(event);
3161}
3162
Frederic Weisbecker76369132011-05-19 19:55:04 +02003163static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003164static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003166static void unaccount_event_cpu(struct perf_event *event, int cpu)
3167{
3168 if (event->parent)
3169 return;
3170
3171 if (has_branch_stack(event)) {
3172 if (!(event->attach_state & PERF_ATTACH_TASK))
3173 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3174 }
3175 if (is_cgroup_event(event))
3176 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3177}
3178
3179static void unaccount_event(struct perf_event *event)
3180{
3181 if (event->parent)
3182 return;
3183
3184 if (event->attach_state & PERF_ATTACH_TASK)
3185 static_key_slow_dec_deferred(&perf_sched_events);
3186 if (event->attr.mmap || event->attr.mmap_data)
3187 atomic_dec(&nr_mmap_events);
3188 if (event->attr.comm)
3189 atomic_dec(&nr_comm_events);
3190 if (event->attr.task)
3191 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003192 if (event->attr.freq)
3193 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003194 if (is_cgroup_event(event))
3195 static_key_slow_dec_deferred(&perf_sched_events);
3196 if (has_branch_stack(event))
3197 static_key_slow_dec_deferred(&perf_sched_events);
3198
3199 unaccount_event_cpu(event, event->cpu);
3200}
3201
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003202static void __free_event(struct perf_event *event)
3203{
3204 if (!event->parent) {
3205 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3206 put_callchain_buffers();
3207 }
3208
3209 if (event->destroy)
3210 event->destroy(event);
3211
3212 if (event->ctx)
3213 put_ctx(event->ctx);
3214
3215 call_rcu(&event->rcu_head, free_event_rcu);
3216}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217static void free_event(struct perf_event *event)
3218{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003219 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003221 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222
Frederic Weisbecker76369132011-05-19 19:55:04 +02003223 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003224 struct ring_buffer *rb;
3225
3226 /*
3227 * Can happen when we close an event with re-directed output.
3228 *
3229 * Since we have a 0 refcount, perf_mmap_close() will skip
3230 * over us; possibly making our ring_buffer_put() the last.
3231 */
3232 mutex_lock(&event->mmap_mutex);
3233 rb = event->rb;
3234 if (rb) {
3235 rcu_assign_pointer(event->rb, NULL);
3236 ring_buffer_detach(event, rb);
3237 ring_buffer_put(rb); /* could be last */
3238 }
3239 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003240 }
3241
Stephane Eraniane5d13672011-02-14 11:20:01 +02003242 if (is_cgroup_event(event))
3243 perf_detach_cgroup(event);
3244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003246 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247}
3248
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003249int perf_event_release_kernel(struct perf_event *event)
3250{
3251 struct perf_event_context *ctx = event->ctx;
3252
3253 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02003254 /*
3255 * There are two ways this annotation is useful:
3256 *
3257 * 1) there is a lock recursion from perf_event_exit_task
3258 * see the comment there.
3259 *
3260 * 2) there is a lock-inversion with mmap_sem through
3261 * perf_event_read_group(), which takes faults while
3262 * holding ctx->mutex, however this is called after
3263 * the last filedesc died, so there is no possibility
3264 * to trigger the AB-BA case.
3265 */
3266 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003267 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02003268 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003269 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02003270 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003271 mutex_unlock(&ctx->mutex);
3272
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003273 free_event(event);
3274
3275 return 0;
3276}
3277EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3278
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003279/*
3280 * Called when the last reference to the file is gone.
3281 */
Al Viroa6fa9412012-08-20 14:59:25 +01003282static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003283{
Peter Zijlstra88821352010-11-09 19:01:43 +01003284 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003285
Al Viroa6fa9412012-08-20 14:59:25 +01003286 if (!atomic_long_dec_and_test(&event->refcount))
3287 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003288
Peter Zijlstra88821352010-11-09 19:01:43 +01003289 rcu_read_lock();
3290 owner = ACCESS_ONCE(event->owner);
3291 /*
3292 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3293 * !owner it means the list deletion is complete and we can indeed
3294 * free this event, otherwise we need to serialize on
3295 * owner->perf_event_mutex.
3296 */
3297 smp_read_barrier_depends();
3298 if (owner) {
3299 /*
3300 * Since delayed_put_task_struct() also drops the last
3301 * task reference we can safely take a new reference
3302 * while holding the rcu_read_lock().
3303 */
3304 get_task_struct(owner);
3305 }
3306 rcu_read_unlock();
3307
3308 if (owner) {
3309 mutex_lock(&owner->perf_event_mutex);
3310 /*
3311 * We have to re-check the event->owner field, if it is cleared
3312 * we raced with perf_event_exit_task(), acquiring the mutex
3313 * ensured they're done, and we can proceed with freeing the
3314 * event.
3315 */
3316 if (event->owner)
3317 list_del_init(&event->owner_entry);
3318 mutex_unlock(&owner->perf_event_mutex);
3319 put_task_struct(owner);
3320 }
3321
Al Viroa6fa9412012-08-20 14:59:25 +01003322 perf_event_release_kernel(event);
3323}
3324
3325static int perf_release(struct inode *inode, struct file *file)
3326{
3327 put_event(file->private_data);
3328 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003329}
3330
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003331u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332{
3333 struct perf_event *child;
3334 u64 total = 0;
3335
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003336 *enabled = 0;
3337 *running = 0;
3338
Peter Zijlstra6f105812009-11-20 22:19:56 +01003339 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003341 *enabled += event->total_time_enabled +
3342 atomic64_read(&event->child_total_time_enabled);
3343 *running += event->total_time_running +
3344 atomic64_read(&event->child_total_time_running);
3345
3346 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003348 *enabled += child->total_time_enabled;
3349 *running += child->total_time_running;
3350 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003351 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352
3353 return total;
3354}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003355EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357static int perf_event_read_group(struct perf_event *event,
3358 u64 read_format, char __user *buf)
3359{
3360 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003361 int n = 0, size = 0, ret = -EFAULT;
3362 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003363 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003364 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003365
Peter Zijlstra6f105812009-11-20 22:19:56 +01003366 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003367 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003368
3369 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003370 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3371 values[n++] = enabled;
3372 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3373 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003374 values[n++] = count;
3375 if (read_format & PERF_FORMAT_ID)
3376 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377
3378 size = n * sizeof(u64);
3379
3380 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003381 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382
Peter Zijlstra6f105812009-11-20 22:19:56 +01003383 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384
3385 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003386 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003388 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003389 if (read_format & PERF_FORMAT_ID)
3390 values[n++] = primary_event_id(sub);
3391
3392 size = n * sizeof(u64);
3393
Stephane Eranian184d3da2009-11-23 21:40:49 -08003394 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003395 ret = -EFAULT;
3396 goto unlock;
3397 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003398
3399 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003400 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003401unlock:
3402 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403
Peter Zijlstraabf48682009-11-20 22:19:49 +01003404 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405}
3406
3407static int perf_event_read_one(struct perf_event *event,
3408 u64 read_format, char __user *buf)
3409{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003410 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 u64 values[4];
3412 int n = 0;
3413
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003414 values[n++] = perf_event_read_value(event, &enabled, &running);
3415 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3416 values[n++] = enabled;
3417 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3418 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419 if (read_format & PERF_FORMAT_ID)
3420 values[n++] = primary_event_id(event);
3421
3422 if (copy_to_user(buf, values, n * sizeof(u64)))
3423 return -EFAULT;
3424
3425 return n * sizeof(u64);
3426}
3427
3428/*
3429 * Read the performance event - simple non blocking version for now
3430 */
3431static ssize_t
3432perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3433{
3434 u64 read_format = event->attr.read_format;
3435 int ret;
3436
3437 /*
3438 * Return end-of-file for a read on a event that is in
3439 * error state (i.e. because it was pinned but it couldn't be
3440 * scheduled on to the CPU at some point).
3441 */
3442 if (event->state == PERF_EVENT_STATE_ERROR)
3443 return 0;
3444
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003445 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003446 return -ENOSPC;
3447
3448 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003449 if (read_format & PERF_FORMAT_GROUP)
3450 ret = perf_event_read_group(event, read_format, buf);
3451 else
3452 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453
3454 return ret;
3455}
3456
3457static ssize_t
3458perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3459{
3460 struct perf_event *event = file->private_data;
3461
3462 return perf_read_hw(event, buf, count);
3463}
3464
3465static unsigned int perf_poll(struct file *file, poll_table *wait)
3466{
3467 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003468 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 unsigned int events = POLL_HUP;
3470
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003471 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003472 * Pin the event->rb by taking event->mmap_mutex; otherwise
3473 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003474 */
3475 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003476 rb = event->rb;
3477 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003478 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003479 mutex_unlock(&event->mmap_mutex);
3480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481 poll_wait(file, &event->waitq, wait);
3482
3483 return events;
3484}
3485
3486static void perf_event_reset(struct perf_event *event)
3487{
3488 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003489 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003490 perf_event_update_userpage(event);
3491}
3492
3493/*
3494 * Holding the top-level event's child_mutex means that any
3495 * descendant process that has inherited this event will block
3496 * in sync_child_event if it goes to exit, thus satisfying the
3497 * task existence requirements of perf_event_enable/disable.
3498 */
3499static void perf_event_for_each_child(struct perf_event *event,
3500 void (*func)(struct perf_event *))
3501{
3502 struct perf_event *child;
3503
3504 WARN_ON_ONCE(event->ctx->parent_ctx);
3505 mutex_lock(&event->child_mutex);
3506 func(event);
3507 list_for_each_entry(child, &event->child_list, child_list)
3508 func(child);
3509 mutex_unlock(&event->child_mutex);
3510}
3511
3512static void perf_event_for_each(struct perf_event *event,
3513 void (*func)(struct perf_event *))
3514{
3515 struct perf_event_context *ctx = event->ctx;
3516 struct perf_event *sibling;
3517
3518 WARN_ON_ONCE(ctx->parent_ctx);
3519 mutex_lock(&ctx->mutex);
3520 event = event->group_leader;
3521
3522 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003523 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003524 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 mutex_unlock(&ctx->mutex);
3526}
3527
3528static int perf_event_period(struct perf_event *event, u64 __user *arg)
3529{
3530 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003531 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003532 u64 value;
3533
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003534 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003535 return -EINVAL;
3536
John Blackwoodad0cf342010-09-28 18:03:11 -04003537 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003538 return -EFAULT;
3539
3540 if (!value)
3541 return -EINVAL;
3542
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003543 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003544 if (event->attr.freq) {
3545 if (value > sysctl_perf_event_sample_rate) {
3546 ret = -EINVAL;
3547 goto unlock;
3548 }
3549
3550 event->attr.sample_freq = value;
3551 } else {
3552 event->attr.sample_period = value;
3553 event->hw.sample_period = value;
3554 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003555
3556 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3557 if (active) {
3558 perf_pmu_disable(ctx->pmu);
3559 event->pmu->stop(event, PERF_EF_UPDATE);
3560 }
3561
3562 local64_set(&event->hw.period_left, 0);
3563
3564 if (active) {
3565 event->pmu->start(event, PERF_EF_RELOAD);
3566 perf_pmu_enable(ctx->pmu);
3567 }
3568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003570 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003571
3572 return ret;
3573}
3574
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003575static const struct file_operations perf_fops;
3576
Al Viro2903ff02012-08-28 12:52:22 -04003577static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003578{
Al Viro2903ff02012-08-28 12:52:22 -04003579 struct fd f = fdget(fd);
3580 if (!f.file)
3581 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003582
Al Viro2903ff02012-08-28 12:52:22 -04003583 if (f.file->f_op != &perf_fops) {
3584 fdput(f);
3585 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003586 }
Al Viro2903ff02012-08-28 12:52:22 -04003587 *p = f;
3588 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003589}
3590
3591static int perf_event_set_output(struct perf_event *event,
3592 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003593static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594
3595static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3596{
3597 struct perf_event *event = file->private_data;
3598 void (*func)(struct perf_event *);
3599 u32 flags = arg;
3600
3601 switch (cmd) {
3602 case PERF_EVENT_IOC_ENABLE:
3603 func = perf_event_enable;
3604 break;
3605 case PERF_EVENT_IOC_DISABLE:
3606 func = perf_event_disable;
3607 break;
3608 case PERF_EVENT_IOC_RESET:
3609 func = perf_event_reset;
3610 break;
3611
3612 case PERF_EVENT_IOC_REFRESH:
3613 return perf_event_refresh(event, arg);
3614
3615 case PERF_EVENT_IOC_PERIOD:
3616 return perf_event_period(event, (u64 __user *)arg);
3617
Jiri Olsacf4957f2012-10-24 13:37:58 +02003618 case PERF_EVENT_IOC_ID:
3619 {
3620 u64 id = primary_event_id(event);
3621
3622 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3623 return -EFAULT;
3624 return 0;
3625 }
3626
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003627 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003628 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003629 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003630 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003631 struct perf_event *output_event;
3632 struct fd output;
3633 ret = perf_fget_light(arg, &output);
3634 if (ret)
3635 return ret;
3636 output_event = output.file->private_data;
3637 ret = perf_event_set_output(event, output_event);
3638 fdput(output);
3639 } else {
3640 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003641 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003642 return ret;
3643 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003644
Li Zefan6fb29152009-10-15 11:21:42 +08003645 case PERF_EVENT_IOC_SET_FILTER:
3646 return perf_event_set_filter(event, (void __user *)arg);
3647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003648 default:
3649 return -ENOTTY;
3650 }
3651
3652 if (flags & PERF_IOC_FLAG_GROUP)
3653 perf_event_for_each(event, func);
3654 else
3655 perf_event_for_each_child(event, func);
3656
3657 return 0;
3658}
3659
3660int perf_event_task_enable(void)
3661{
3662 struct perf_event *event;
3663
3664 mutex_lock(&current->perf_event_mutex);
3665 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3666 perf_event_for_each_child(event, perf_event_enable);
3667 mutex_unlock(&current->perf_event_mutex);
3668
3669 return 0;
3670}
3671
3672int perf_event_task_disable(void)
3673{
3674 struct perf_event *event;
3675
3676 mutex_lock(&current->perf_event_mutex);
3677 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3678 perf_event_for_each_child(event, perf_event_disable);
3679 mutex_unlock(&current->perf_event_mutex);
3680
3681 return 0;
3682}
3683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003684static int perf_event_index(struct perf_event *event)
3685{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003686 if (event->hw.state & PERF_HES_STOPPED)
3687 return 0;
3688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003689 if (event->state != PERF_EVENT_STATE_ACTIVE)
3690 return 0;
3691
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003692 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003693}
3694
Eric B Munsonc4794292011-06-23 16:34:38 -04003695static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003696 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003697 u64 *enabled,
3698 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003699{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003700 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003701
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003702 *now = perf_clock();
3703 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003704 *enabled = ctx_time - event->tstamp_enabled;
3705 *running = ctx_time - event->tstamp_running;
3706}
3707
Peter Zijlstrafa731582013-09-19 10:16:42 +02003708static void perf_event_init_userpage(struct perf_event *event)
3709{
3710 struct perf_event_mmap_page *userpg;
3711 struct ring_buffer *rb;
3712
3713 rcu_read_lock();
3714 rb = rcu_dereference(event->rb);
3715 if (!rb)
3716 goto unlock;
3717
3718 userpg = rb->user_page;
3719
3720 /* Allow new userspace to detect that bit 0 is deprecated */
3721 userpg->cap_bit0_is_deprecated = 1;
3722 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3723
3724unlock:
3725 rcu_read_unlock();
3726}
3727
Peter Zijlstrac7206202012-03-22 17:26:36 +01003728void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003729{
3730}
3731
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732/*
3733 * Callers need to ensure there can be no nesting of this function, otherwise
3734 * the seqlock logic goes bad. We can not serialize this because the arch
3735 * code calls this from NMI context.
3736 */
3737void perf_event_update_userpage(struct perf_event *event)
3738{
3739 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003740 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003741 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003742
3743 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003744 rb = rcu_dereference(event->rb);
3745 if (!rb)
3746 goto unlock;
3747
Eric B Munson0d641202011-06-24 12:26:26 -04003748 /*
3749 * compute total_time_enabled, total_time_running
3750 * based on snapshot values taken when the event
3751 * was last scheduled in.
3752 *
3753 * we cannot simply called update_context_time()
3754 * because of locking issue as we can be called in
3755 * NMI context
3756 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003757 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003758
Frederic Weisbecker76369132011-05-19 19:55:04 +02003759 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003760 /*
3761 * Disable preemption so as to not let the corresponding user-space
3762 * spin too long if we get preempted.
3763 */
3764 preempt_disable();
3765 ++userpg->lock;
3766 barrier();
3767 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003768 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003769 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003770 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771
Eric B Munson0d641202011-06-24 12:26:26 -04003772 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773 atomic64_read(&event->child_total_time_enabled);
3774
Eric B Munson0d641202011-06-24 12:26:26 -04003775 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003776 atomic64_read(&event->child_total_time_running);
3777
Peter Zijlstrac7206202012-03-22 17:26:36 +01003778 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780 barrier();
3781 ++userpg->lock;
3782 preempt_enable();
3783unlock:
3784 rcu_read_unlock();
3785}
3786
Peter Zijlstra906010b2009-09-21 16:08:49 +02003787static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3788{
3789 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003790 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003791 int ret = VM_FAULT_SIGBUS;
3792
3793 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3794 if (vmf->pgoff == 0)
3795 ret = 0;
3796 return ret;
3797 }
3798
3799 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003800 rb = rcu_dereference(event->rb);
3801 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003802 goto unlock;
3803
3804 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3805 goto unlock;
3806
Frederic Weisbecker76369132011-05-19 19:55:04 +02003807 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003808 if (!vmf->page)
3809 goto unlock;
3810
3811 get_page(vmf->page);
3812 vmf->page->mapping = vma->vm_file->f_mapping;
3813 vmf->page->index = vmf->pgoff;
3814
3815 ret = 0;
3816unlock:
3817 rcu_read_unlock();
3818
3819 return ret;
3820}
3821
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003822static void ring_buffer_attach(struct perf_event *event,
3823 struct ring_buffer *rb)
3824{
3825 unsigned long flags;
3826
3827 if (!list_empty(&event->rb_entry))
3828 return;
3829
3830 spin_lock_irqsave(&rb->event_lock, flags);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003831 if (list_empty(&event->rb_entry))
3832 list_add(&event->rb_entry, &rb->event_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003833 spin_unlock_irqrestore(&rb->event_lock, flags);
3834}
3835
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003836static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003837{
3838 unsigned long flags;
3839
3840 if (list_empty(&event->rb_entry))
3841 return;
3842
3843 spin_lock_irqsave(&rb->event_lock, flags);
3844 list_del_init(&event->rb_entry);
3845 wake_up_all(&event->waitq);
3846 spin_unlock_irqrestore(&rb->event_lock, flags);
3847}
3848
3849static void ring_buffer_wakeup(struct perf_event *event)
3850{
3851 struct ring_buffer *rb;
3852
3853 rcu_read_lock();
3854 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003855 if (rb) {
3856 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3857 wake_up_all(&event->waitq);
3858 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003859 rcu_read_unlock();
3860}
3861
Frederic Weisbecker76369132011-05-19 19:55:04 +02003862static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003863{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003864 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003865
Frederic Weisbecker76369132011-05-19 19:55:04 +02003866 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3867 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868}
3869
Frederic Weisbecker76369132011-05-19 19:55:04 +02003870static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003872 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003873
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003874 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003875 rb = rcu_dereference(event->rb);
3876 if (rb) {
3877 if (!atomic_inc_not_zero(&rb->refcount))
3878 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003879 }
3880 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881
Frederic Weisbecker76369132011-05-19 19:55:04 +02003882 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003883}
3884
Frederic Weisbecker76369132011-05-19 19:55:04 +02003885static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003886{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003887 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003888 return;
3889
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003890 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003891
Frederic Weisbecker76369132011-05-19 19:55:04 +02003892 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003893}
3894
3895static void perf_mmap_open(struct vm_area_struct *vma)
3896{
3897 struct perf_event *event = vma->vm_file->private_data;
3898
3899 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003900 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003901}
3902
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003903/*
3904 * A buffer can be mmap()ed multiple times; either directly through the same
3905 * event, or through other events by use of perf_event_set_output().
3906 *
3907 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3908 * the buffer here, where we still have a VM context. This means we need
3909 * to detach all events redirecting to us.
3910 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003911static void perf_mmap_close(struct vm_area_struct *vma)
3912{
3913 struct perf_event *event = vma->vm_file->private_data;
3914
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003915 struct ring_buffer *rb = event->rb;
3916 struct user_struct *mmap_user = rb->mmap_user;
3917 int mmap_locked = rb->mmap_locked;
3918 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003919
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003920 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003921
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003922 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3923 return;
3924
3925 /* Detach current event from the buffer. */
3926 rcu_assign_pointer(event->rb, NULL);
3927 ring_buffer_detach(event, rb);
3928 mutex_unlock(&event->mmap_mutex);
3929
3930 /* If there's still other mmap()s of this buffer, we're done. */
3931 if (atomic_read(&rb->mmap_count)) {
3932 ring_buffer_put(rb); /* can't be last */
3933 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934 }
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003935
3936 /*
3937 * No other mmap()s, detach from all other events that might redirect
3938 * into the now unreachable buffer. Somewhat complicated by the
3939 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3940 */
3941again:
3942 rcu_read_lock();
3943 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3944 if (!atomic_long_inc_not_zero(&event->refcount)) {
3945 /*
3946 * This event is en-route to free_event() which will
3947 * detach it and remove it from the list.
3948 */
3949 continue;
3950 }
3951 rcu_read_unlock();
3952
3953 mutex_lock(&event->mmap_mutex);
3954 /*
3955 * Check we didn't race with perf_event_set_output() which can
3956 * swizzle the rb from under us while we were waiting to
3957 * acquire mmap_mutex.
3958 *
3959 * If we find a different rb; ignore this event, a next
3960 * iteration will no longer find it on the list. We have to
3961 * still restart the iteration to make sure we're not now
3962 * iterating the wrong list.
3963 */
3964 if (event->rb == rb) {
3965 rcu_assign_pointer(event->rb, NULL);
3966 ring_buffer_detach(event, rb);
3967 ring_buffer_put(rb); /* can't be last, we still have one */
3968 }
3969 mutex_unlock(&event->mmap_mutex);
3970 put_event(event);
3971
3972 /*
3973 * Restart the iteration; either we're on the wrong list or
3974 * destroyed its integrity by doing a deletion.
3975 */
3976 goto again;
3977 }
3978 rcu_read_unlock();
3979
3980 /*
3981 * It could be there's still a few 0-ref events on the list; they'll
3982 * get cleaned up by free_event() -- they'll also still have their
3983 * ref on the rb and will free it whenever they are done with it.
3984 *
3985 * Aside from that, this buffer is 'fully' detached and unmapped,
3986 * undo the VM accounting.
3987 */
3988
3989 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3990 vma->vm_mm->pinned_vm -= mmap_locked;
3991 free_uid(mmap_user);
3992
3993 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003994}
3995
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003996static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997 .open = perf_mmap_open,
3998 .close = perf_mmap_close,
3999 .fault = perf_mmap_fault,
4000 .page_mkwrite = perf_mmap_fault,
4001};
4002
4003static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4004{
4005 struct perf_event *event = file->private_data;
4006 unsigned long user_locked, user_lock_limit;
4007 struct user_struct *user = current_user();
4008 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004009 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010 unsigned long vma_size;
4011 unsigned long nr_pages;
4012 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004013 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014
Peter Zijlstrac7920612010-05-18 10:33:24 +02004015 /*
4016 * Don't allow mmap() of inherited per-task counters. This would
4017 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004018 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004019 */
4020 if (event->cpu == -1 && event->attr.inherit)
4021 return -EINVAL;
4022
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004023 if (!(vma->vm_flags & VM_SHARED))
4024 return -EINVAL;
4025
4026 vma_size = vma->vm_end - vma->vm_start;
4027 nr_pages = (vma_size / PAGE_SIZE) - 1;
4028
4029 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004030 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031 * can do bitmasks instead of modulo.
4032 */
4033 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4034 return -EINVAL;
4035
4036 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4037 return -EINVAL;
4038
4039 if (vma->vm_pgoff != 0)
4040 return -EINVAL;
4041
4042 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004043again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004044 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004045 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004046 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004047 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004048 goto unlock;
4049 }
4050
4051 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4052 /*
4053 * Raced against perf_mmap_close() through
4054 * perf_event_set_output(). Try again, hope for better
4055 * luck.
4056 */
4057 mutex_unlock(&event->mmap_mutex);
4058 goto again;
4059 }
4060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004061 goto unlock;
4062 }
4063
4064 user_extra = nr_pages + 1;
4065 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4066
4067 /*
4068 * Increase the limit linearly with more CPUs:
4069 */
4070 user_lock_limit *= num_online_cpus();
4071
4072 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4073
4074 extra = 0;
4075 if (user_locked > user_lock_limit)
4076 extra = user_locked - user_lock_limit;
4077
Jiri Slaby78d7d402010-03-05 13:42:54 -08004078 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004080 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004081
4082 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4083 !capable(CAP_IPC_LOCK)) {
4084 ret = -EPERM;
4085 goto unlock;
4086 }
4087
Frederic Weisbecker76369132011-05-19 19:55:04 +02004088 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004089
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004090 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004091 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004092
Vince Weaver4ec83632011-06-01 15:15:36 -04004093 rb = rb_alloc(nr_pages,
4094 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4095 event->cpu, flags);
4096
Frederic Weisbecker76369132011-05-19 19:55:04 +02004097 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004098 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004099 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004100 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004101
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004102 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004103 rb->mmap_locked = extra;
4104 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004105
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004106 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004107 vma->vm_mm->pinned_vm += extra;
4108
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004109 ring_buffer_attach(event, rb);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004110 rcu_assign_pointer(event->rb, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004111
Peter Zijlstrafa731582013-09-19 10:16:42 +02004112 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004113 perf_event_update_userpage(event);
4114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004116 if (!ret)
4117 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 mutex_unlock(&event->mmap_mutex);
4119
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004120 /*
4121 * Since pinned accounting is per vm we cannot allow fork() to copy our
4122 * vma.
4123 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004124 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125 vma->vm_ops = &perf_mmap_vmops;
4126
4127 return ret;
4128}
4129
4130static int perf_fasync(int fd, struct file *filp, int on)
4131{
Al Viro496ad9a2013-01-23 17:07:38 -05004132 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133 struct perf_event *event = filp->private_data;
4134 int retval;
4135
4136 mutex_lock(&inode->i_mutex);
4137 retval = fasync_helper(fd, filp, on, &event->fasync);
4138 mutex_unlock(&inode->i_mutex);
4139
4140 if (retval < 0)
4141 return retval;
4142
4143 return 0;
4144}
4145
4146static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004147 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004148 .release = perf_release,
4149 .read = perf_read,
4150 .poll = perf_poll,
4151 .unlocked_ioctl = perf_ioctl,
4152 .compat_ioctl = perf_ioctl,
4153 .mmap = perf_mmap,
4154 .fasync = perf_fasync,
4155};
4156
4157/*
4158 * Perf event wakeup
4159 *
4160 * If there's data, ensure we set the poll() state and publish everything
4161 * to user-space before waking everybody up.
4162 */
4163
4164void perf_event_wakeup(struct perf_event *event)
4165{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004166 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004167
4168 if (event->pending_kill) {
4169 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4170 event->pending_kill = 0;
4171 }
4172}
4173
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004174static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175{
4176 struct perf_event *event = container_of(entry,
4177 struct perf_event, pending);
4178
4179 if (event->pending_disable) {
4180 event->pending_disable = 0;
4181 __perf_event_disable(event);
4182 }
4183
4184 if (event->pending_wakeup) {
4185 event->pending_wakeup = 0;
4186 perf_event_wakeup(event);
4187 }
4188}
4189
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004191 * We assume there is only KVM supporting the callbacks.
4192 * Later on, we might change it to a list if there is
4193 * another virtualization implementation supporting the callbacks.
4194 */
4195struct perf_guest_info_callbacks *perf_guest_cbs;
4196
4197int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4198{
4199 perf_guest_cbs = cbs;
4200 return 0;
4201}
4202EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4203
4204int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4205{
4206 perf_guest_cbs = NULL;
4207 return 0;
4208}
4209EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4210
Jiri Olsa40189942012-08-07 15:20:37 +02004211static void
4212perf_output_sample_regs(struct perf_output_handle *handle,
4213 struct pt_regs *regs, u64 mask)
4214{
4215 int bit;
4216
4217 for_each_set_bit(bit, (const unsigned long *) &mask,
4218 sizeof(mask) * BITS_PER_BYTE) {
4219 u64 val;
4220
4221 val = perf_reg_value(regs, bit);
4222 perf_output_put(handle, val);
4223 }
4224}
4225
4226static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4227 struct pt_regs *regs)
4228{
4229 if (!user_mode(regs)) {
4230 if (current->mm)
4231 regs = task_pt_regs(current);
4232 else
4233 regs = NULL;
4234 }
4235
4236 if (regs) {
4237 regs_user->regs = regs;
4238 regs_user->abi = perf_reg_abi(current);
4239 }
4240}
4241
Jiri Olsac5ebced2012-08-07 15:20:40 +02004242/*
4243 * Get remaining task size from user stack pointer.
4244 *
4245 * It'd be better to take stack vma map and limit this more
4246 * precisly, but there's no way to get it safely under interrupt,
4247 * so using TASK_SIZE as limit.
4248 */
4249static u64 perf_ustack_task_size(struct pt_regs *regs)
4250{
4251 unsigned long addr = perf_user_stack_pointer(regs);
4252
4253 if (!addr || addr >= TASK_SIZE)
4254 return 0;
4255
4256 return TASK_SIZE - addr;
4257}
4258
4259static u16
4260perf_sample_ustack_size(u16 stack_size, u16 header_size,
4261 struct pt_regs *regs)
4262{
4263 u64 task_size;
4264
4265 /* No regs, no stack pointer, no dump. */
4266 if (!regs)
4267 return 0;
4268
4269 /*
4270 * Check if we fit in with the requested stack size into the:
4271 * - TASK_SIZE
4272 * If we don't, we limit the size to the TASK_SIZE.
4273 *
4274 * - remaining sample size
4275 * If we don't, we customize the stack size to
4276 * fit in to the remaining sample size.
4277 */
4278
4279 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4280 stack_size = min(stack_size, (u16) task_size);
4281
4282 /* Current header size plus static size and dynamic size. */
4283 header_size += 2 * sizeof(u64);
4284
4285 /* Do we fit in with the current stack dump size? */
4286 if ((u16) (header_size + stack_size) < header_size) {
4287 /*
4288 * If we overflow the maximum size for the sample,
4289 * we customize the stack dump size to fit in.
4290 */
4291 stack_size = USHRT_MAX - header_size - sizeof(u64);
4292 stack_size = round_up(stack_size, sizeof(u64));
4293 }
4294
4295 return stack_size;
4296}
4297
4298static void
4299perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4300 struct pt_regs *regs)
4301{
4302 /* Case of a kernel thread, nothing to dump */
4303 if (!regs) {
4304 u64 size = 0;
4305 perf_output_put(handle, size);
4306 } else {
4307 unsigned long sp;
4308 unsigned int rem;
4309 u64 dyn_size;
4310
4311 /*
4312 * We dump:
4313 * static size
4314 * - the size requested by user or the best one we can fit
4315 * in to the sample max size
4316 * data
4317 * - user stack dump data
4318 * dynamic size
4319 * - the actual dumped size
4320 */
4321
4322 /* Static size. */
4323 perf_output_put(handle, dump_size);
4324
4325 /* Data. */
4326 sp = perf_user_stack_pointer(regs);
4327 rem = __output_copy_user(handle, (void *) sp, dump_size);
4328 dyn_size = dump_size - rem;
4329
4330 perf_output_skip(handle, rem);
4331
4332 /* Dynamic size. */
4333 perf_output_put(handle, dyn_size);
4334 }
4335}
4336
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004337static void __perf_event_header__init_id(struct perf_event_header *header,
4338 struct perf_sample_data *data,
4339 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004340{
4341 u64 sample_type = event->attr.sample_type;
4342
4343 data->type = sample_type;
4344 header->size += event->id_header_size;
4345
4346 if (sample_type & PERF_SAMPLE_TID) {
4347 /* namespace issues */
4348 data->tid_entry.pid = perf_event_pid(event, current);
4349 data->tid_entry.tid = perf_event_tid(event, current);
4350 }
4351
4352 if (sample_type & PERF_SAMPLE_TIME)
4353 data->time = perf_clock();
4354
Adrian Hunterff3d5272013-08-27 11:23:07 +03004355 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004356 data->id = primary_event_id(event);
4357
4358 if (sample_type & PERF_SAMPLE_STREAM_ID)
4359 data->stream_id = event->id;
4360
4361 if (sample_type & PERF_SAMPLE_CPU) {
4362 data->cpu_entry.cpu = raw_smp_processor_id();
4363 data->cpu_entry.reserved = 0;
4364 }
4365}
4366
Frederic Weisbecker76369132011-05-19 19:55:04 +02004367void perf_event_header__init_id(struct perf_event_header *header,
4368 struct perf_sample_data *data,
4369 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004370{
4371 if (event->attr.sample_id_all)
4372 __perf_event_header__init_id(header, data, event);
4373}
4374
4375static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4376 struct perf_sample_data *data)
4377{
4378 u64 sample_type = data->type;
4379
4380 if (sample_type & PERF_SAMPLE_TID)
4381 perf_output_put(handle, data->tid_entry);
4382
4383 if (sample_type & PERF_SAMPLE_TIME)
4384 perf_output_put(handle, data->time);
4385
4386 if (sample_type & PERF_SAMPLE_ID)
4387 perf_output_put(handle, data->id);
4388
4389 if (sample_type & PERF_SAMPLE_STREAM_ID)
4390 perf_output_put(handle, data->stream_id);
4391
4392 if (sample_type & PERF_SAMPLE_CPU)
4393 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004394
4395 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4396 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004397}
4398
Frederic Weisbecker76369132011-05-19 19:55:04 +02004399void perf_event__output_id_sample(struct perf_event *event,
4400 struct perf_output_handle *handle,
4401 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004402{
4403 if (event->attr.sample_id_all)
4404 __perf_event__output_id_sample(handle, sample);
4405}
4406
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004407static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004408 struct perf_event *event,
4409 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410{
4411 u64 read_format = event->attr.read_format;
4412 u64 values[4];
4413 int n = 0;
4414
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004415 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004416 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004417 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004418 atomic64_read(&event->child_total_time_enabled);
4419 }
4420 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004421 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 atomic64_read(&event->child_total_time_running);
4423 }
4424 if (read_format & PERF_FORMAT_ID)
4425 values[n++] = primary_event_id(event);
4426
Frederic Weisbecker76369132011-05-19 19:55:04 +02004427 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428}
4429
4430/*
4431 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4432 */
4433static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004434 struct perf_event *event,
4435 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436{
4437 struct perf_event *leader = event->group_leader, *sub;
4438 u64 read_format = event->attr.read_format;
4439 u64 values[5];
4440 int n = 0;
4441
4442 values[n++] = 1 + leader->nr_siblings;
4443
4444 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004445 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446
4447 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004448 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004449
4450 if (leader != event)
4451 leader->pmu->read(leader);
4452
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004453 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454 if (read_format & PERF_FORMAT_ID)
4455 values[n++] = primary_event_id(leader);
4456
Frederic Weisbecker76369132011-05-19 19:55:04 +02004457 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458
4459 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4460 n = 0;
4461
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004462 if ((sub != event) &&
4463 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464 sub->pmu->read(sub);
4465
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004466 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 if (read_format & PERF_FORMAT_ID)
4468 values[n++] = primary_event_id(sub);
4469
Frederic Weisbecker76369132011-05-19 19:55:04 +02004470 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471 }
4472}
4473
Stephane Eranianeed01522010-10-26 16:08:01 +02004474#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4475 PERF_FORMAT_TOTAL_TIME_RUNNING)
4476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477static void perf_output_read(struct perf_output_handle *handle,
4478 struct perf_event *event)
4479{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004480 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004481 u64 read_format = event->attr.read_format;
4482
4483 /*
4484 * compute total_time_enabled, total_time_running
4485 * based on snapshot values taken when the event
4486 * was last scheduled in.
4487 *
4488 * we cannot simply called update_context_time()
4489 * because of locking issue as we are called in
4490 * NMI context
4491 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004492 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004493 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004496 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004498 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004499}
4500
4501void perf_output_sample(struct perf_output_handle *handle,
4502 struct perf_event_header *header,
4503 struct perf_sample_data *data,
4504 struct perf_event *event)
4505{
4506 u64 sample_type = data->type;
4507
4508 perf_output_put(handle, *header);
4509
Adrian Hunterff3d5272013-08-27 11:23:07 +03004510 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4511 perf_output_put(handle, data->id);
4512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513 if (sample_type & PERF_SAMPLE_IP)
4514 perf_output_put(handle, data->ip);
4515
4516 if (sample_type & PERF_SAMPLE_TID)
4517 perf_output_put(handle, data->tid_entry);
4518
4519 if (sample_type & PERF_SAMPLE_TIME)
4520 perf_output_put(handle, data->time);
4521
4522 if (sample_type & PERF_SAMPLE_ADDR)
4523 perf_output_put(handle, data->addr);
4524
4525 if (sample_type & PERF_SAMPLE_ID)
4526 perf_output_put(handle, data->id);
4527
4528 if (sample_type & PERF_SAMPLE_STREAM_ID)
4529 perf_output_put(handle, data->stream_id);
4530
4531 if (sample_type & PERF_SAMPLE_CPU)
4532 perf_output_put(handle, data->cpu_entry);
4533
4534 if (sample_type & PERF_SAMPLE_PERIOD)
4535 perf_output_put(handle, data->period);
4536
4537 if (sample_type & PERF_SAMPLE_READ)
4538 perf_output_read(handle, event);
4539
4540 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4541 if (data->callchain) {
4542 int size = 1;
4543
4544 if (data->callchain)
4545 size += data->callchain->nr;
4546
4547 size *= sizeof(u64);
4548
Frederic Weisbecker76369132011-05-19 19:55:04 +02004549 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550 } else {
4551 u64 nr = 0;
4552 perf_output_put(handle, nr);
4553 }
4554 }
4555
4556 if (sample_type & PERF_SAMPLE_RAW) {
4557 if (data->raw) {
4558 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004559 __output_copy(handle, data->raw->data,
4560 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004561 } else {
4562 struct {
4563 u32 size;
4564 u32 data;
4565 } raw = {
4566 .size = sizeof(u32),
4567 .data = 0,
4568 };
4569 perf_output_put(handle, raw);
4570 }
4571 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004572
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004573 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4574 if (data->br_stack) {
4575 size_t size;
4576
4577 size = data->br_stack->nr
4578 * sizeof(struct perf_branch_entry);
4579
4580 perf_output_put(handle, data->br_stack->nr);
4581 perf_output_copy(handle, data->br_stack->entries, size);
4582 } else {
4583 /*
4584 * we always store at least the value of nr
4585 */
4586 u64 nr = 0;
4587 perf_output_put(handle, nr);
4588 }
4589 }
Jiri Olsa40189942012-08-07 15:20:37 +02004590
4591 if (sample_type & PERF_SAMPLE_REGS_USER) {
4592 u64 abi = data->regs_user.abi;
4593
4594 /*
4595 * If there are no regs to dump, notice it through
4596 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4597 */
4598 perf_output_put(handle, abi);
4599
4600 if (abi) {
4601 u64 mask = event->attr.sample_regs_user;
4602 perf_output_sample_regs(handle,
4603 data->regs_user.regs,
4604 mask);
4605 }
4606 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004607
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004608 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004609 perf_output_sample_ustack(handle,
4610 data->stack_user_size,
4611 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004612 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004613
4614 if (sample_type & PERF_SAMPLE_WEIGHT)
4615 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004616
4617 if (sample_type & PERF_SAMPLE_DATA_SRC)
4618 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004619
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004620 if (sample_type & PERF_SAMPLE_TRANSACTION)
4621 perf_output_put(handle, data->txn);
4622
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004623 if (!event->attr.watermark) {
4624 int wakeup_events = event->attr.wakeup_events;
4625
4626 if (wakeup_events) {
4627 struct ring_buffer *rb = handle->rb;
4628 int events = local_inc_return(&rb->events);
4629
4630 if (events >= wakeup_events) {
4631 local_sub(wakeup_events, &rb->events);
4632 local_inc(&rb->wakeup);
4633 }
4634 }
4635 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004636}
4637
4638void perf_prepare_sample(struct perf_event_header *header,
4639 struct perf_sample_data *data,
4640 struct perf_event *event,
4641 struct pt_regs *regs)
4642{
4643 u64 sample_type = event->attr.sample_type;
4644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004645 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004646 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004647
4648 header->misc = 0;
4649 header->misc |= perf_misc_flags(regs);
4650
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004651 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004652
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004653 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 data->ip = perf_instruction_pointer(regs);
4655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4657 int size = 1;
4658
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004659 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660
4661 if (data->callchain)
4662 size += data->callchain->nr;
4663
4664 header->size += size * sizeof(u64);
4665 }
4666
4667 if (sample_type & PERF_SAMPLE_RAW) {
4668 int size = sizeof(u32);
4669
4670 if (data->raw)
4671 size += data->raw->size;
4672 else
4673 size += sizeof(u32);
4674
4675 WARN_ON_ONCE(size & (sizeof(u64)-1));
4676 header->size += size;
4677 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004678
4679 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4680 int size = sizeof(u64); /* nr */
4681 if (data->br_stack) {
4682 size += data->br_stack->nr
4683 * sizeof(struct perf_branch_entry);
4684 }
4685 header->size += size;
4686 }
Jiri Olsa40189942012-08-07 15:20:37 +02004687
4688 if (sample_type & PERF_SAMPLE_REGS_USER) {
4689 /* regs dump ABI info */
4690 int size = sizeof(u64);
4691
4692 perf_sample_regs_user(&data->regs_user, regs);
4693
4694 if (data->regs_user.regs) {
4695 u64 mask = event->attr.sample_regs_user;
4696 size += hweight64(mask) * sizeof(u64);
4697 }
4698
4699 header->size += size;
4700 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004701
4702 if (sample_type & PERF_SAMPLE_STACK_USER) {
4703 /*
4704 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4705 * processed as the last one or have additional check added
4706 * in case new sample type is added, because we could eat
4707 * up the rest of the sample size.
4708 */
4709 struct perf_regs_user *uregs = &data->regs_user;
4710 u16 stack_size = event->attr.sample_stack_user;
4711 u16 size = sizeof(u64);
4712
4713 if (!uregs->abi)
4714 perf_sample_regs_user(uregs, regs);
4715
4716 stack_size = perf_sample_ustack_size(stack_size, header->size,
4717 uregs->regs);
4718
4719 /*
4720 * If there is something to dump, add space for the dump
4721 * itself and for the field that tells the dynamic size,
4722 * which is how many have been actually dumped.
4723 */
4724 if (stack_size)
4725 size += sizeof(u64) + stack_size;
4726
4727 data->stack_user_size = stack_size;
4728 header->size += size;
4729 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730}
4731
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004732static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004733 struct perf_sample_data *data,
4734 struct pt_regs *regs)
4735{
4736 struct perf_output_handle handle;
4737 struct perf_event_header header;
4738
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004739 /* protect the callchain buffers */
4740 rcu_read_lock();
4741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742 perf_prepare_sample(&header, data, event, regs);
4743
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004744 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004745 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746
4747 perf_output_sample(&handle, &header, data, event);
4748
4749 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004750
4751exit:
4752 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004753}
4754
4755/*
4756 * read event_id
4757 */
4758
4759struct perf_read_event {
4760 struct perf_event_header header;
4761
4762 u32 pid;
4763 u32 tid;
4764};
4765
4766static void
4767perf_event_read_event(struct perf_event *event,
4768 struct task_struct *task)
4769{
4770 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004771 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004772 struct perf_read_event read_event = {
4773 .header = {
4774 .type = PERF_RECORD_READ,
4775 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004776 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004777 },
4778 .pid = perf_event_pid(event, task),
4779 .tid = perf_event_tid(event, task),
4780 };
4781 int ret;
4782
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004783 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004784 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785 if (ret)
4786 return;
4787
4788 perf_output_put(&handle, read_event);
4789 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004790 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004791
4792 perf_output_end(&handle);
4793}
4794
Jiri Olsa52d857a2013-05-06 18:27:18 +02004795typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4796
4797static void
4798perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004799 perf_event_aux_output_cb output,
4800 void *data)
4801{
4802 struct perf_event *event;
4803
4804 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4805 if (event->state < PERF_EVENT_STATE_INACTIVE)
4806 continue;
4807 if (!event_filter_match(event))
4808 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004809 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004810 }
4811}
4812
4813static void
Jiri Olsa67516842013-07-09 18:56:31 +02004814perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004815 struct perf_event_context *task_ctx)
4816{
4817 struct perf_cpu_context *cpuctx;
4818 struct perf_event_context *ctx;
4819 struct pmu *pmu;
4820 int ctxn;
4821
4822 rcu_read_lock();
4823 list_for_each_entry_rcu(pmu, &pmus, entry) {
4824 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4825 if (cpuctx->unique_pmu != pmu)
4826 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004827 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004828 if (task_ctx)
4829 goto next;
4830 ctxn = pmu->task_ctx_nr;
4831 if (ctxn < 0)
4832 goto next;
4833 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4834 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004835 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004836next:
4837 put_cpu_ptr(pmu->pmu_cpu_context);
4838 }
4839
4840 if (task_ctx) {
4841 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004842 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004843 preempt_enable();
4844 }
4845 rcu_read_unlock();
4846}
4847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848/*
4849 * task tracking -- fork/exit
4850 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004851 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004852 */
4853
4854struct perf_task_event {
4855 struct task_struct *task;
4856 struct perf_event_context *task_ctx;
4857
4858 struct {
4859 struct perf_event_header header;
4860
4861 u32 pid;
4862 u32 ppid;
4863 u32 tid;
4864 u32 ptid;
4865 u64 time;
4866 } event_id;
4867};
4868
Jiri Olsa67516842013-07-09 18:56:31 +02004869static int perf_event_task_match(struct perf_event *event)
4870{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004871 return event->attr.comm || event->attr.mmap ||
4872 event->attr.mmap2 || event->attr.mmap_data ||
4873 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004874}
4875
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004876static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004877 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004879 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004881 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004883 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004884
Jiri Olsa67516842013-07-09 18:56:31 +02004885 if (!perf_event_task_match(event))
4886 return;
4887
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004888 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004890 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004891 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004892 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004893 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894
4895 task_event->event_id.pid = perf_event_pid(event, task);
4896 task_event->event_id.ppid = perf_event_pid(event, current);
4897
4898 task_event->event_id.tid = perf_event_tid(event, task);
4899 task_event->event_id.ptid = perf_event_tid(event, current);
4900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901 perf_output_put(&handle, task_event->event_id);
4902
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004903 perf_event__output_id_sample(event, &handle, &sample);
4904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004905 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004906out:
4907 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004908}
4909
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004910static void perf_event_task(struct task_struct *task,
4911 struct perf_event_context *task_ctx,
4912 int new)
4913{
4914 struct perf_task_event task_event;
4915
4916 if (!atomic_read(&nr_comm_events) &&
4917 !atomic_read(&nr_mmap_events) &&
4918 !atomic_read(&nr_task_events))
4919 return;
4920
4921 task_event = (struct perf_task_event){
4922 .task = task,
4923 .task_ctx = task_ctx,
4924 .event_id = {
4925 .header = {
4926 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4927 .misc = 0,
4928 .size = sizeof(task_event.event_id),
4929 },
4930 /* .pid */
4931 /* .ppid */
4932 /* .tid */
4933 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004934 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004935 },
4936 };
4937
Jiri Olsa67516842013-07-09 18:56:31 +02004938 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004939 &task_event,
4940 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004941}
4942
4943void perf_event_fork(struct task_struct *task)
4944{
4945 perf_event_task(task, NULL, 1);
4946}
4947
4948/*
4949 * comm tracking
4950 */
4951
4952struct perf_comm_event {
4953 struct task_struct *task;
4954 char *comm;
4955 int comm_size;
4956
4957 struct {
4958 struct perf_event_header header;
4959
4960 u32 pid;
4961 u32 tid;
4962 } event_id;
4963};
4964
Jiri Olsa67516842013-07-09 18:56:31 +02004965static int perf_event_comm_match(struct perf_event *event)
4966{
4967 return event->attr.comm;
4968}
4969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004970static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004971 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004973 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004974 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004975 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004977 int ret;
4978
Jiri Olsa67516842013-07-09 18:56:31 +02004979 if (!perf_event_comm_match(event))
4980 return;
4981
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004982 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4983 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004984 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004985
4986 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004987 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004988
4989 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4990 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4991
4992 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004993 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004995
4996 perf_event__output_id_sample(event, &handle, &sample);
4997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004999out:
5000 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001}
5002
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003static void perf_event_comm_event(struct perf_comm_event *comm_event)
5004{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005005 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005006 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007
5008 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005009 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005010 size = ALIGN(strlen(comm)+1, sizeof(u64));
5011
5012 comm_event->comm = comm;
5013 comm_event->comm_size = size;
5014
5015 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005016
Jiri Olsa67516842013-07-09 18:56:31 +02005017 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005018 comm_event,
5019 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020}
5021
5022void perf_event_comm(struct task_struct *task)
5023{
5024 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005025 struct perf_event_context *ctx;
5026 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005027
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005028 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005029 for_each_task_context_nr(ctxn) {
5030 ctx = task->perf_event_ctxp[ctxn];
5031 if (!ctx)
5032 continue;
5033
5034 perf_event_enable_on_exec(ctx);
5035 }
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005036 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005037
5038 if (!atomic_read(&nr_comm_events))
5039 return;
5040
5041 comm_event = (struct perf_comm_event){
5042 .task = task,
5043 /* .comm */
5044 /* .comm_size */
5045 .event_id = {
5046 .header = {
5047 .type = PERF_RECORD_COMM,
5048 .misc = 0,
5049 /* .size */
5050 },
5051 /* .pid */
5052 /* .tid */
5053 },
5054 };
5055
5056 perf_event_comm_event(&comm_event);
5057}
5058
5059/*
5060 * mmap tracking
5061 */
5062
5063struct perf_mmap_event {
5064 struct vm_area_struct *vma;
5065
5066 const char *file_name;
5067 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005068 int maj, min;
5069 u64 ino;
5070 u64 ino_generation;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005071
5072 struct {
5073 struct perf_event_header header;
5074
5075 u32 pid;
5076 u32 tid;
5077 u64 start;
5078 u64 len;
5079 u64 pgoff;
5080 } event_id;
5081};
5082
Jiri Olsa67516842013-07-09 18:56:31 +02005083static int perf_event_mmap_match(struct perf_event *event,
5084 void *data)
5085{
5086 struct perf_mmap_event *mmap_event = data;
5087 struct vm_area_struct *vma = mmap_event->vma;
5088 int executable = vma->vm_flags & VM_EXEC;
5089
5090 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005091 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005092}
5093
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005094static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005095 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005097 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005098 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005099 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005100 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005101 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005102
Jiri Olsa67516842013-07-09 18:56:31 +02005103 if (!perf_event_mmap_match(event, data))
5104 return;
5105
Stephane Eranian13d7a242013-08-21 12:10:24 +02005106 if (event->attr.mmap2) {
5107 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5108 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5109 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5110 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005111 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005112 }
5113
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005114 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5115 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005116 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005117 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005118 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005119
5120 mmap_event->event_id.pid = perf_event_pid(event, current);
5121 mmap_event->event_id.tid = perf_event_tid(event, current);
5122
5123 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005124
5125 if (event->attr.mmap2) {
5126 perf_output_put(&handle, mmap_event->maj);
5127 perf_output_put(&handle, mmap_event->min);
5128 perf_output_put(&handle, mmap_event->ino);
5129 perf_output_put(&handle, mmap_event->ino_generation);
5130 }
5131
Frederic Weisbecker76369132011-05-19 19:55:04 +02005132 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005134
5135 perf_event__output_id_sample(event, &handle, &sample);
5136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005137 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005138out:
5139 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140}
5141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005142static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5143{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005144 struct vm_area_struct *vma = mmap_event->vma;
5145 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005146 int maj = 0, min = 0;
5147 u64 ino = 0, gen = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005148 unsigned int size;
5149 char tmp[16];
5150 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005151 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005152
5153 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005154 struct inode *inode;
5155 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005156
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005157 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005158 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005159 name = "//enomem";
5160 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005161 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005162 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005163 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005164 * need to add enough zero bytes after the string to handle
5165 * the 64bit alignment we do later.
5166 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005167 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005168 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005169 name = "//toolong";
5170 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005171 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005172 inode = file_inode(vma->vm_file);
5173 dev = inode->i_sb->s_dev;
5174 ino = inode->i_ino;
5175 gen = inode->i_generation;
5176 maj = MAJOR(dev);
5177 min = MINOR(dev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005178 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179 } else {
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005180 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005181 if (name)
5182 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005184 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005185 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005186 name = "[heap]";
5187 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005188 }
5189 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005191 name = "[stack]";
5192 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193 }
5194
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005195 name = "//anon";
5196 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005197 }
5198
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005199cpy_name:
5200 strlcpy(tmp, name, sizeof(tmp));
5201 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005202got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005203 /*
5204 * Since our buffer works in 8 byte units we need to align our string
5205 * size to a multiple of 8. However, we must guarantee the tail end is
5206 * zero'd out to avoid leaking random bits to userspace.
5207 */
5208 size = strlen(name)+1;
5209 while (!IS_ALIGNED(size, sizeof(u64)))
5210 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005211
5212 mmap_event->file_name = name;
5213 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005214 mmap_event->maj = maj;
5215 mmap_event->min = min;
5216 mmap_event->ino = ino;
5217 mmap_event->ino_generation = gen;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005218
Stephane Eranian2fe85422013-01-24 16:10:39 +01005219 if (!(vma->vm_flags & VM_EXEC))
5220 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5221
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5223
Jiri Olsa67516842013-07-09 18:56:31 +02005224 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005225 mmap_event,
5226 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005227
5228 kfree(buf);
5229}
5230
Eric B Munson3af9e852010-05-18 15:30:49 +01005231void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232{
5233 struct perf_mmap_event mmap_event;
5234
5235 if (!atomic_read(&nr_mmap_events))
5236 return;
5237
5238 mmap_event = (struct perf_mmap_event){
5239 .vma = vma,
5240 /* .file_name */
5241 /* .file_size */
5242 .event_id = {
5243 .header = {
5244 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005245 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246 /* .size */
5247 },
5248 /* .pid */
5249 /* .tid */
5250 .start = vma->vm_start,
5251 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005252 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005254 /* .maj (attr_mmap2 only) */
5255 /* .min (attr_mmap2 only) */
5256 /* .ino (attr_mmap2 only) */
5257 /* .ino_generation (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258 };
5259
5260 perf_event_mmap_event(&mmap_event);
5261}
5262
5263/*
5264 * IRQ throttle logging
5265 */
5266
5267static void perf_log_throttle(struct perf_event *event, int enable)
5268{
5269 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005270 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271 int ret;
5272
5273 struct {
5274 struct perf_event_header header;
5275 u64 time;
5276 u64 id;
5277 u64 stream_id;
5278 } throttle_event = {
5279 .header = {
5280 .type = PERF_RECORD_THROTTLE,
5281 .misc = 0,
5282 .size = sizeof(throttle_event),
5283 },
5284 .time = perf_clock(),
5285 .id = primary_event_id(event),
5286 .stream_id = event->id,
5287 };
5288
5289 if (enable)
5290 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5291
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005292 perf_event_header__init_id(&throttle_event.header, &sample, event);
5293
5294 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005295 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 if (ret)
5297 return;
5298
5299 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005300 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 perf_output_end(&handle);
5302}
5303
5304/*
5305 * Generic event overflow handling, sampling.
5306 */
5307
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005308static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005309 int throttle, struct perf_sample_data *data,
5310 struct pt_regs *regs)
5311{
5312 int events = atomic_read(&event->event_limit);
5313 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005314 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315 int ret = 0;
5316
Peter Zijlstra96398822010-11-24 18:55:29 +01005317 /*
5318 * Non-sampling counters might still use the PMI to fold short
5319 * hardware counters, ignore those.
5320 */
5321 if (unlikely(!is_sampling_event(event)))
5322 return 0;
5323
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005324 seq = __this_cpu_read(perf_throttled_seq);
5325 if (seq != hwc->interrupts_seq) {
5326 hwc->interrupts_seq = seq;
5327 hwc->interrupts = 1;
5328 } else {
5329 hwc->interrupts++;
5330 if (unlikely(throttle
5331 && hwc->interrupts >= max_samples_per_tick)) {
5332 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005333 hwc->interrupts = MAX_INTERRUPTS;
5334 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005335 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 ret = 1;
5337 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005338 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339
5340 if (event->attr.freq) {
5341 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005342 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343
Peter Zijlstraabd50712010-01-26 18:50:16 +01005344 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345
Peter Zijlstraabd50712010-01-26 18:50:16 +01005346 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005347 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005348 }
5349
5350 /*
5351 * XXX event_limit might not quite work as expected on inherited
5352 * events
5353 */
5354
5355 event->pending_kill = POLL_IN;
5356 if (events && atomic_dec_and_test(&event->event_limit)) {
5357 ret = 1;
5358 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005359 event->pending_disable = 1;
5360 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005361 }
5362
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005363 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005364 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005365 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005366 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005367
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005368 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005369 event->pending_wakeup = 1;
5370 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005371 }
5372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005373 return ret;
5374}
5375
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005376int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377 struct perf_sample_data *data,
5378 struct pt_regs *regs)
5379{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005380 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381}
5382
5383/*
5384 * Generic software event infrastructure
5385 */
5386
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005387struct swevent_htable {
5388 struct swevent_hlist *swevent_hlist;
5389 struct mutex hlist_mutex;
5390 int hlist_refcount;
5391
5392 /* Recursion avoidance in each contexts */
5393 int recursion[PERF_NR_CONTEXTS];
5394};
5395
5396static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5397
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005398/*
5399 * We directly increment event->count and keep a second value in
5400 * event->hw.period_left to count intervals. This period event
5401 * is kept in the range [-sample_period, 0] so that we can use the
5402 * sign as trigger.
5403 */
5404
Jiri Olsaab573842013-05-01 17:25:44 +02005405u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005406{
5407 struct hw_perf_event *hwc = &event->hw;
5408 u64 period = hwc->last_period;
5409 u64 nr, offset;
5410 s64 old, val;
5411
5412 hwc->last_period = hwc->sample_period;
5413
5414again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005415 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005416 if (val < 0)
5417 return 0;
5418
5419 nr = div64_u64(period + val, period);
5420 offset = nr * period;
5421 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005422 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423 goto again;
5424
5425 return nr;
5426}
5427
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005428static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005429 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005430 struct pt_regs *regs)
5431{
5432 struct hw_perf_event *hwc = &event->hw;
5433 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005435 if (!overflow)
5436 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437
5438 if (hwc->interrupts == MAX_INTERRUPTS)
5439 return;
5440
5441 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005442 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443 data, regs)) {
5444 /*
5445 * We inhibit the overflow from happening when
5446 * hwc->interrupts == MAX_INTERRUPTS.
5447 */
5448 break;
5449 }
5450 throttle = 1;
5451 }
5452}
5453
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005454static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005455 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456 struct pt_regs *regs)
5457{
5458 struct hw_perf_event *hwc = &event->hw;
5459
Peter Zijlstrae7850592010-05-21 14:43:08 +02005460 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005461
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005462 if (!regs)
5463 return;
5464
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005465 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005466 return;
5467
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005468 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5469 data->period = nr;
5470 return perf_swevent_overflow(event, 1, data, regs);
5471 } else
5472 data->period = event->hw.last_period;
5473
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005474 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005475 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005476
Peter Zijlstrae7850592010-05-21 14:43:08 +02005477 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005478 return;
5479
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005480 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481}
5482
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005483static int perf_exclude_event(struct perf_event *event,
5484 struct pt_regs *regs)
5485{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005486 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005487 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005488
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005489 if (regs) {
5490 if (event->attr.exclude_user && user_mode(regs))
5491 return 1;
5492
5493 if (event->attr.exclude_kernel && !user_mode(regs))
5494 return 1;
5495 }
5496
5497 return 0;
5498}
5499
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500static int perf_swevent_match(struct perf_event *event,
5501 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005502 u32 event_id,
5503 struct perf_sample_data *data,
5504 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005505{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005506 if (event->attr.type != type)
5507 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005508
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005509 if (event->attr.config != event_id)
5510 return 0;
5511
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005512 if (perf_exclude_event(event, regs))
5513 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005514
5515 return 1;
5516}
5517
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005518static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005519{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005520 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005521
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005522 return hash_64(val, SWEVENT_HLIST_BITS);
5523}
5524
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005525static inline struct hlist_head *
5526__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005527{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005528 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005529
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005530 return &hlist->heads[hash];
5531}
5532
5533/* For the read side: events when they trigger */
5534static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005535find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005536{
5537 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005538
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005539 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005540 if (!hlist)
5541 return NULL;
5542
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005543 return __find_swevent_head(hlist, type, event_id);
5544}
5545
5546/* For the event head insertion and removal in the hlist */
5547static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005548find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005549{
5550 struct swevent_hlist *hlist;
5551 u32 event_id = event->attr.config;
5552 u64 type = event->attr.type;
5553
5554 /*
5555 * Event scheduling is always serialized against hlist allocation
5556 * and release. Which makes the protected version suitable here.
5557 * The context lock guarantees that.
5558 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005559 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005560 lockdep_is_held(&event->ctx->lock));
5561 if (!hlist)
5562 return NULL;
5563
5564 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005565}
5566
5567static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005568 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005569 struct perf_sample_data *data,
5570 struct pt_regs *regs)
5571{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005572 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005573 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005574 struct hlist_head *head;
5575
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005576 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005577 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005578 if (!head)
5579 goto end;
5580
Sasha Levinb67bfe02013-02-27 17:06:00 -08005581 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005582 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005583 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005585end:
5586 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005587}
5588
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005589int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005591 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005592
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005593 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005595EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005596
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005597inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005598{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005599 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005600
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005601 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005602}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005603
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005604void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005605{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005606 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005607 int rctx;
5608
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005609 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005610 rctx = perf_swevent_get_recursion_context();
5611 if (rctx < 0)
5612 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005613
Robert Richterfd0d0002012-04-02 20:19:08 +02005614 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005615
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005616 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005617
5618 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005619 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005620}
5621
5622static void perf_swevent_read(struct perf_event *event)
5623{
5624}
5625
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005626static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005627{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005628 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005630 struct hlist_head *head;
5631
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005632 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633 hwc->last_period = hwc->sample_period;
5634 perf_swevent_set_period(event);
5635 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005636
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005637 hwc->state = !(flags & PERF_EF_START);
5638
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005639 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005640 if (WARN_ON_ONCE(!head))
5641 return -EINVAL;
5642
5643 hlist_add_head_rcu(&event->hlist_entry, head);
5644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645 return 0;
5646}
5647
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005648static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005650 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005651}
5652
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005653static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005654{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005655 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005656}
5657
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005658static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005659{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005660 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005661}
5662
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005663/* Deref the hlist from the update side */
5664static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005665swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005666{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005667 return rcu_dereference_protected(swhash->swevent_hlist,
5668 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005669}
5670
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005671static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005672{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005673 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005674
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005675 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005676 return;
5677
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005678 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005679 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005680}
5681
5682static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5683{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005684 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005685
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005686 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005687
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005688 if (!--swhash->hlist_refcount)
5689 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005690
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005691 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005692}
5693
5694static void swevent_hlist_put(struct perf_event *event)
5695{
5696 int cpu;
5697
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005698 for_each_possible_cpu(cpu)
5699 swevent_hlist_put_cpu(event, cpu);
5700}
5701
5702static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5703{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005704 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005705 int err = 0;
5706
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005707 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005708
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005709 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005710 struct swevent_hlist *hlist;
5711
5712 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5713 if (!hlist) {
5714 err = -ENOMEM;
5715 goto exit;
5716 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005717 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005718 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005719 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005720exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005721 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005722
5723 return err;
5724}
5725
5726static int swevent_hlist_get(struct perf_event *event)
5727{
5728 int err;
5729 int cpu, failed_cpu;
5730
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005731 get_online_cpus();
5732 for_each_possible_cpu(cpu) {
5733 err = swevent_hlist_get_cpu(event, cpu);
5734 if (err) {
5735 failed_cpu = cpu;
5736 goto fail;
5737 }
5738 }
5739 put_online_cpus();
5740
5741 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005742fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005743 for_each_possible_cpu(cpu) {
5744 if (cpu == failed_cpu)
5745 break;
5746 swevent_hlist_put_cpu(event, cpu);
5747 }
5748
5749 put_online_cpus();
5750 return err;
5751}
5752
Ingo Molnarc5905af2012-02-24 08:31:31 +01005753struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005754
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005755static void sw_perf_event_destroy(struct perf_event *event)
5756{
5757 u64 event_id = event->attr.config;
5758
5759 WARN_ON(event->parent);
5760
Ingo Molnarc5905af2012-02-24 08:31:31 +01005761 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005762 swevent_hlist_put(event);
5763}
5764
5765static int perf_swevent_init(struct perf_event *event)
5766{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005767 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005768
5769 if (event->attr.type != PERF_TYPE_SOFTWARE)
5770 return -ENOENT;
5771
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005772 /*
5773 * no branch sampling for software events
5774 */
5775 if (has_branch_stack(event))
5776 return -EOPNOTSUPP;
5777
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005778 switch (event_id) {
5779 case PERF_COUNT_SW_CPU_CLOCK:
5780 case PERF_COUNT_SW_TASK_CLOCK:
5781 return -ENOENT;
5782
5783 default:
5784 break;
5785 }
5786
Dan Carpenterce677832010-10-24 21:50:42 +02005787 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005788 return -ENOENT;
5789
5790 if (!event->parent) {
5791 int err;
5792
5793 err = swevent_hlist_get(event);
5794 if (err)
5795 return err;
5796
Ingo Molnarc5905af2012-02-24 08:31:31 +01005797 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005798 event->destroy = sw_perf_event_destroy;
5799 }
5800
5801 return 0;
5802}
5803
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005804static int perf_swevent_event_idx(struct perf_event *event)
5805{
5806 return 0;
5807}
5808
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005809static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005810 .task_ctx_nr = perf_sw_context,
5811
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005812 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005813 .add = perf_swevent_add,
5814 .del = perf_swevent_del,
5815 .start = perf_swevent_start,
5816 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005817 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005818
5819 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005820};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005821
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005822#ifdef CONFIG_EVENT_TRACING
5823
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005824static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005825 struct perf_sample_data *data)
5826{
5827 void *record = data->raw->data;
5828
5829 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5830 return 1;
5831 return 0;
5832}
5833
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005834static int perf_tp_event_match(struct perf_event *event,
5835 struct perf_sample_data *data,
5836 struct pt_regs *regs)
5837{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005838 if (event->hw.state & PERF_HES_STOPPED)
5839 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005840 /*
5841 * All tracepoints are from kernel-space.
5842 */
5843 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005844 return 0;
5845
5846 if (!perf_tp_filter_match(event, data))
5847 return 0;
5848
5849 return 1;
5850}
5851
5852void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005853 struct pt_regs *regs, struct hlist_head *head, int rctx,
5854 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005855{
5856 struct perf_sample_data data;
5857 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005858
5859 struct perf_raw_record raw = {
5860 .size = entry_size,
5861 .data = record,
5862 };
5863
Robert Richterfd0d0002012-04-02 20:19:08 +02005864 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005865 data.raw = &raw;
5866
Sasha Levinb67bfe02013-02-27 17:06:00 -08005867 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005868 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005869 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005870 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005871
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005872 /*
5873 * If we got specified a target task, also iterate its context and
5874 * deliver this event there too.
5875 */
5876 if (task && task != current) {
5877 struct perf_event_context *ctx;
5878 struct trace_entry *entry = record;
5879
5880 rcu_read_lock();
5881 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5882 if (!ctx)
5883 goto unlock;
5884
5885 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5886 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5887 continue;
5888 if (event->attr.config != entry->type)
5889 continue;
5890 if (perf_tp_event_match(event, &data, regs))
5891 perf_swevent_event(event, count, &data, regs);
5892 }
5893unlock:
5894 rcu_read_unlock();
5895 }
5896
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005897 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005898}
5899EXPORT_SYMBOL_GPL(perf_tp_event);
5900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901static void tp_perf_event_destroy(struct perf_event *event)
5902{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005903 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904}
5905
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005906static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005908 int err;
5909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005910 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5911 return -ENOENT;
5912
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005913 /*
5914 * no branch sampling for tracepoint events
5915 */
5916 if (has_branch_stack(event))
5917 return -EOPNOTSUPP;
5918
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005919 err = perf_trace_init(event);
5920 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005921 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005922
5923 event->destroy = tp_perf_event_destroy;
5924
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005925 return 0;
5926}
5927
5928static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005929 .task_ctx_nr = perf_sw_context,
5930
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005931 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005932 .add = perf_trace_add,
5933 .del = perf_trace_del,
5934 .start = perf_swevent_start,
5935 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005936 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005937
5938 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005939};
5940
5941static inline void perf_tp_register(void)
5942{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005943 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005944}
Li Zefan6fb29152009-10-15 11:21:42 +08005945
5946static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5947{
5948 char *filter_str;
5949 int ret;
5950
5951 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5952 return -EINVAL;
5953
5954 filter_str = strndup_user(arg, PAGE_SIZE);
5955 if (IS_ERR(filter_str))
5956 return PTR_ERR(filter_str);
5957
5958 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5959
5960 kfree(filter_str);
5961 return ret;
5962}
5963
5964static void perf_event_free_filter(struct perf_event *event)
5965{
5966 ftrace_profile_free_filter(event);
5967}
5968
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969#else
Li Zefan6fb29152009-10-15 11:21:42 +08005970
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005971static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005973}
Li Zefan6fb29152009-10-15 11:21:42 +08005974
5975static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5976{
5977 return -ENOENT;
5978}
5979
5980static void perf_event_free_filter(struct perf_event *event)
5981{
5982}
5983
Li Zefan07b139c2009-12-21 14:27:35 +08005984#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005986#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005987void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005988{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005989 struct perf_sample_data sample;
5990 struct pt_regs *regs = data;
5991
Robert Richterfd0d0002012-04-02 20:19:08 +02005992 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005993
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005994 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005995 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005996}
5997#endif
5998
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005999/*
6000 * hrtimer based swevent callback
6001 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006002
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006003static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006004{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006005 enum hrtimer_restart ret = HRTIMER_RESTART;
6006 struct perf_sample_data data;
6007 struct pt_regs *regs;
6008 struct perf_event *event;
6009 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006011 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006012
6013 if (event->state != PERF_EVENT_STATE_ACTIVE)
6014 return HRTIMER_NORESTART;
6015
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006016 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006017
Robert Richterfd0d0002012-04-02 20:19:08 +02006018 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006019 regs = get_irq_regs();
6020
6021 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006022 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006023 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006024 ret = HRTIMER_NORESTART;
6025 }
6026
6027 period = max_t(u64, 10000, event->hw.sample_period);
6028 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6029
6030 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006031}
6032
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006033static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006034{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006035 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006036 s64 period;
6037
6038 if (!is_sampling_event(event))
6039 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006040
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006041 period = local64_read(&hwc->period_left);
6042 if (period) {
6043 if (period < 0)
6044 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006045
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006046 local64_set(&hwc->period_left, 0);
6047 } else {
6048 period = max_t(u64, 10000, hwc->sample_period);
6049 }
6050 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006051 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006052 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006053}
6054
6055static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6056{
6057 struct hw_perf_event *hwc = &event->hw;
6058
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006059 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006060 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006061 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006062
6063 hrtimer_cancel(&hwc->hrtimer);
6064 }
6065}
6066
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006067static void perf_swevent_init_hrtimer(struct perf_event *event)
6068{
6069 struct hw_perf_event *hwc = &event->hw;
6070
6071 if (!is_sampling_event(event))
6072 return;
6073
6074 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6075 hwc->hrtimer.function = perf_swevent_hrtimer;
6076
6077 /*
6078 * Since hrtimers have a fixed rate, we can do a static freq->period
6079 * mapping and avoid the whole period adjust feedback stuff.
6080 */
6081 if (event->attr.freq) {
6082 long freq = event->attr.sample_freq;
6083
6084 event->attr.sample_period = NSEC_PER_SEC / freq;
6085 hwc->sample_period = event->attr.sample_period;
6086 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006087 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006088 event->attr.freq = 0;
6089 }
6090}
6091
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006092/*
6093 * Software event: cpu wall time clock
6094 */
6095
6096static void cpu_clock_event_update(struct perf_event *event)
6097{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006098 s64 prev;
6099 u64 now;
6100
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006101 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006102 prev = local64_xchg(&event->hw.prev_count, now);
6103 local64_add(now - prev, &event->count);
6104}
6105
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006106static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006107{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006108 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006109 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006110}
6111
6112static void cpu_clock_event_stop(struct perf_event *event, int flags)
6113{
6114 perf_swevent_cancel_hrtimer(event);
6115 cpu_clock_event_update(event);
6116}
6117
6118static int cpu_clock_event_add(struct perf_event *event, int flags)
6119{
6120 if (flags & PERF_EF_START)
6121 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006122
6123 return 0;
6124}
6125
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006126static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006127{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006128 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006129}
6130
6131static void cpu_clock_event_read(struct perf_event *event)
6132{
6133 cpu_clock_event_update(event);
6134}
6135
6136static int cpu_clock_event_init(struct perf_event *event)
6137{
6138 if (event->attr.type != PERF_TYPE_SOFTWARE)
6139 return -ENOENT;
6140
6141 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6142 return -ENOENT;
6143
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006144 /*
6145 * no branch sampling for software events
6146 */
6147 if (has_branch_stack(event))
6148 return -EOPNOTSUPP;
6149
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006150 perf_swevent_init_hrtimer(event);
6151
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006152 return 0;
6153}
6154
6155static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006156 .task_ctx_nr = perf_sw_context,
6157
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006158 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006159 .add = cpu_clock_event_add,
6160 .del = cpu_clock_event_del,
6161 .start = cpu_clock_event_start,
6162 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006163 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006164
6165 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006166};
6167
6168/*
6169 * Software event: task time clock
6170 */
6171
6172static void task_clock_event_update(struct perf_event *event, u64 now)
6173{
6174 u64 prev;
6175 s64 delta;
6176
6177 prev = local64_xchg(&event->hw.prev_count, now);
6178 delta = now - prev;
6179 local64_add(delta, &event->count);
6180}
6181
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006182static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006183{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006184 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006185 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006186}
6187
6188static void task_clock_event_stop(struct perf_event *event, int flags)
6189{
6190 perf_swevent_cancel_hrtimer(event);
6191 task_clock_event_update(event, event->ctx->time);
6192}
6193
6194static int task_clock_event_add(struct perf_event *event, int flags)
6195{
6196 if (flags & PERF_EF_START)
6197 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006198
6199 return 0;
6200}
6201
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006202static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006203{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006204 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006205}
6206
6207static void task_clock_event_read(struct perf_event *event)
6208{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006209 u64 now = perf_clock();
6210 u64 delta = now - event->ctx->timestamp;
6211 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006212
6213 task_clock_event_update(event, time);
6214}
6215
6216static int task_clock_event_init(struct perf_event *event)
6217{
6218 if (event->attr.type != PERF_TYPE_SOFTWARE)
6219 return -ENOENT;
6220
6221 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6222 return -ENOENT;
6223
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006224 /*
6225 * no branch sampling for software events
6226 */
6227 if (has_branch_stack(event))
6228 return -EOPNOTSUPP;
6229
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006230 perf_swevent_init_hrtimer(event);
6231
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006232 return 0;
6233}
6234
6235static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006236 .task_ctx_nr = perf_sw_context,
6237
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006238 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006239 .add = task_clock_event_add,
6240 .del = task_clock_event_del,
6241 .start = task_clock_event_start,
6242 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006243 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006244
6245 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006246};
6247
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006248static void perf_pmu_nop_void(struct pmu *pmu)
6249{
6250}
6251
6252static int perf_pmu_nop_int(struct pmu *pmu)
6253{
6254 return 0;
6255}
6256
6257static void perf_pmu_start_txn(struct pmu *pmu)
6258{
6259 perf_pmu_disable(pmu);
6260}
6261
6262static int perf_pmu_commit_txn(struct pmu *pmu)
6263{
6264 perf_pmu_enable(pmu);
6265 return 0;
6266}
6267
6268static void perf_pmu_cancel_txn(struct pmu *pmu)
6269{
6270 perf_pmu_enable(pmu);
6271}
6272
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006273static int perf_event_idx_default(struct perf_event *event)
6274{
6275 return event->hw.idx + 1;
6276}
6277
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006278/*
6279 * Ensures all contexts with the same task_ctx_nr have the same
6280 * pmu_cpu_context too.
6281 */
6282static void *find_pmu_context(int ctxn)
6283{
6284 struct pmu *pmu;
6285
6286 if (ctxn < 0)
6287 return NULL;
6288
6289 list_for_each_entry(pmu, &pmus, entry) {
6290 if (pmu->task_ctx_nr == ctxn)
6291 return pmu->pmu_cpu_context;
6292 }
6293
6294 return NULL;
6295}
6296
Peter Zijlstra51676952010-12-07 14:18:20 +01006297static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006298{
Peter Zijlstra51676952010-12-07 14:18:20 +01006299 int cpu;
6300
6301 for_each_possible_cpu(cpu) {
6302 struct perf_cpu_context *cpuctx;
6303
6304 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6305
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006306 if (cpuctx->unique_pmu == old_pmu)
6307 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006308 }
6309}
6310
6311static void free_pmu_context(struct pmu *pmu)
6312{
6313 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006314
6315 mutex_lock(&pmus_lock);
6316 /*
6317 * Like a real lame refcount.
6318 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006319 list_for_each_entry(i, &pmus, entry) {
6320 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6321 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006322 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006323 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006324 }
6325
Peter Zijlstra51676952010-12-07 14:18:20 +01006326 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006327out:
6328 mutex_unlock(&pmus_lock);
6329}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006330static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006331
Peter Zijlstraabe43402010-11-17 23:17:37 +01006332static ssize_t
6333type_show(struct device *dev, struct device_attribute *attr, char *page)
6334{
6335 struct pmu *pmu = dev_get_drvdata(dev);
6336
6337 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6338}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006339static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006340
Stephane Eranian62b85632013-04-03 14:21:34 +02006341static ssize_t
6342perf_event_mux_interval_ms_show(struct device *dev,
6343 struct device_attribute *attr,
6344 char *page)
6345{
6346 struct pmu *pmu = dev_get_drvdata(dev);
6347
6348 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6349}
6350
6351static ssize_t
6352perf_event_mux_interval_ms_store(struct device *dev,
6353 struct device_attribute *attr,
6354 const char *buf, size_t count)
6355{
6356 struct pmu *pmu = dev_get_drvdata(dev);
6357 int timer, cpu, ret;
6358
6359 ret = kstrtoint(buf, 0, &timer);
6360 if (ret)
6361 return ret;
6362
6363 if (timer < 1)
6364 return -EINVAL;
6365
6366 /* same value, noting to do */
6367 if (timer == pmu->hrtimer_interval_ms)
6368 return count;
6369
6370 pmu->hrtimer_interval_ms = timer;
6371
6372 /* update all cpuctx for this PMU */
6373 for_each_possible_cpu(cpu) {
6374 struct perf_cpu_context *cpuctx;
6375 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6376 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6377
6378 if (hrtimer_active(&cpuctx->hrtimer))
6379 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6380 }
6381
6382 return count;
6383}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006384static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006385
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006386static struct attribute *pmu_dev_attrs[] = {
6387 &dev_attr_type.attr,
6388 &dev_attr_perf_event_mux_interval_ms.attr,
6389 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006390};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006391ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006392
6393static int pmu_bus_running;
6394static struct bus_type pmu_bus = {
6395 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006396 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006397};
6398
6399static void pmu_dev_release(struct device *dev)
6400{
6401 kfree(dev);
6402}
6403
6404static int pmu_dev_alloc(struct pmu *pmu)
6405{
6406 int ret = -ENOMEM;
6407
6408 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6409 if (!pmu->dev)
6410 goto out;
6411
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006412 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006413 device_initialize(pmu->dev);
6414 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6415 if (ret)
6416 goto free_dev;
6417
6418 dev_set_drvdata(pmu->dev, pmu);
6419 pmu->dev->bus = &pmu_bus;
6420 pmu->dev->release = pmu_dev_release;
6421 ret = device_add(pmu->dev);
6422 if (ret)
6423 goto free_dev;
6424
6425out:
6426 return ret;
6427
6428free_dev:
6429 put_device(pmu->dev);
6430 goto out;
6431}
6432
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006433static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006434static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006435
Mischa Jonker03d8e802013-06-04 11:45:48 +02006436int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006437{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006438 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006439
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006440 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006441 ret = -ENOMEM;
6442 pmu->pmu_disable_count = alloc_percpu(int);
6443 if (!pmu->pmu_disable_count)
6444 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006445
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006446 pmu->type = -1;
6447 if (!name)
6448 goto skip_type;
6449 pmu->name = name;
6450
6451 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006452 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6453 if (type < 0) {
6454 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006455 goto free_pdc;
6456 }
6457 }
6458 pmu->type = type;
6459
Peter Zijlstraabe43402010-11-17 23:17:37 +01006460 if (pmu_bus_running) {
6461 ret = pmu_dev_alloc(pmu);
6462 if (ret)
6463 goto free_idr;
6464 }
6465
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006466skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006467 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6468 if (pmu->pmu_cpu_context)
6469 goto got_cpu_context;
6470
Wei Yongjunc4814202013-04-12 11:05:54 +08006471 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006472 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6473 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006474 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006475
6476 for_each_possible_cpu(cpu) {
6477 struct perf_cpu_context *cpuctx;
6478
6479 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006480 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006481 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006482 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006483 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006484 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006485
6486 __perf_cpu_hrtimer_init(cpuctx, cpu);
6487
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006488 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006489 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006490 }
6491
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006492got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006493 if (!pmu->start_txn) {
6494 if (pmu->pmu_enable) {
6495 /*
6496 * If we have pmu_enable/pmu_disable calls, install
6497 * transaction stubs that use that to try and batch
6498 * hardware accesses.
6499 */
6500 pmu->start_txn = perf_pmu_start_txn;
6501 pmu->commit_txn = perf_pmu_commit_txn;
6502 pmu->cancel_txn = perf_pmu_cancel_txn;
6503 } else {
6504 pmu->start_txn = perf_pmu_nop_void;
6505 pmu->commit_txn = perf_pmu_nop_int;
6506 pmu->cancel_txn = perf_pmu_nop_void;
6507 }
6508 }
6509
6510 if (!pmu->pmu_enable) {
6511 pmu->pmu_enable = perf_pmu_nop_void;
6512 pmu->pmu_disable = perf_pmu_nop_void;
6513 }
6514
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006515 if (!pmu->event_idx)
6516 pmu->event_idx = perf_event_idx_default;
6517
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006518 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006519 ret = 0;
6520unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006521 mutex_unlock(&pmus_lock);
6522
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006523 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006524
Peter Zijlstraabe43402010-11-17 23:17:37 +01006525free_dev:
6526 device_del(pmu->dev);
6527 put_device(pmu->dev);
6528
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006529free_idr:
6530 if (pmu->type >= PERF_TYPE_MAX)
6531 idr_remove(&pmu_idr, pmu->type);
6532
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006533free_pdc:
6534 free_percpu(pmu->pmu_disable_count);
6535 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006536}
6537
6538void perf_pmu_unregister(struct pmu *pmu)
6539{
6540 mutex_lock(&pmus_lock);
6541 list_del_rcu(&pmu->entry);
6542 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006543
6544 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006545 * We dereference the pmu list under both SRCU and regular RCU, so
6546 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006547 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006548 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006549 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006550
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006551 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006552 if (pmu->type >= PERF_TYPE_MAX)
6553 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006554 device_del(pmu->dev);
6555 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006556 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006557}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006558
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006559struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006560{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006561 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006562 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006563 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006564
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006565 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006566
6567 rcu_read_lock();
6568 pmu = idr_find(&pmu_idr, event->attr.type);
6569 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006570 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006571 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006572 ret = pmu->event_init(event);
6573 if (ret)
6574 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006575 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006576 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006577
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006578 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006579 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006580 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006581 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006582 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006583
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006584 if (ret != -ENOENT) {
6585 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006586 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006587 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006588 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006589 pmu = ERR_PTR(-ENOENT);
6590unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006591 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006592
6593 return pmu;
6594}
6595
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006596static void account_event_cpu(struct perf_event *event, int cpu)
6597{
6598 if (event->parent)
6599 return;
6600
6601 if (has_branch_stack(event)) {
6602 if (!(event->attach_state & PERF_ATTACH_TASK))
6603 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6604 }
6605 if (is_cgroup_event(event))
6606 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6607}
6608
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006609static void account_event(struct perf_event *event)
6610{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006611 if (event->parent)
6612 return;
6613
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006614 if (event->attach_state & PERF_ATTACH_TASK)
6615 static_key_slow_inc(&perf_sched_events.key);
6616 if (event->attr.mmap || event->attr.mmap_data)
6617 atomic_inc(&nr_mmap_events);
6618 if (event->attr.comm)
6619 atomic_inc(&nr_comm_events);
6620 if (event->attr.task)
6621 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006622 if (event->attr.freq) {
6623 if (atomic_inc_return(&nr_freq_events) == 1)
6624 tick_nohz_full_kick_all();
6625 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006626 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006627 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006628 if (is_cgroup_event(event))
6629 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006630
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006631 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006632}
6633
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006634/*
6635 * Allocate and initialize a event structure
6636 */
6637static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006638perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006639 struct task_struct *task,
6640 struct perf_event *group_leader,
6641 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006642 perf_overflow_handler_t overflow_handler,
6643 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006644{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006645 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006646 struct perf_event *event;
6647 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006648 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006650 if ((unsigned)cpu >= nr_cpu_ids) {
6651 if (!task || cpu != -1)
6652 return ERR_PTR(-EINVAL);
6653 }
6654
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006655 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006656 if (!event)
6657 return ERR_PTR(-ENOMEM);
6658
6659 /*
6660 * Single events are their own group leaders, with an
6661 * empty sibling list:
6662 */
6663 if (!group_leader)
6664 group_leader = event;
6665
6666 mutex_init(&event->child_mutex);
6667 INIT_LIST_HEAD(&event->child_list);
6668
6669 INIT_LIST_HEAD(&event->group_entry);
6670 INIT_LIST_HEAD(&event->event_entry);
6671 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006672 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006673 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006674 INIT_HLIST_NODE(&event->hlist_entry);
6675
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006676
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006678 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006679
6680 mutex_init(&event->mmap_mutex);
6681
Al Viroa6fa9412012-08-20 14:59:25 +01006682 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006683 event->cpu = cpu;
6684 event->attr = *attr;
6685 event->group_leader = group_leader;
6686 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687 event->oncpu = -1;
6688
6689 event->parent = parent_event;
6690
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006691 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692 event->id = atomic64_inc_return(&perf_event_id);
6693
6694 event->state = PERF_EVENT_STATE_INACTIVE;
6695
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006696 if (task) {
6697 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006698
6699 if (attr->type == PERF_TYPE_TRACEPOINT)
6700 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006701#ifdef CONFIG_HAVE_HW_BREAKPOINT
6702 /*
6703 * hw_breakpoint is a bit difficult here..
6704 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006705 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006706 event->hw.bp_target = task;
6707#endif
6708 }
6709
Avi Kivity4dc0da82011-06-29 18:42:35 +03006710 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006711 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006712 context = parent_event->overflow_handler_context;
6713 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006714
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006715 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006716 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006717
Jiri Olsa0231bb52013-02-01 11:23:45 +01006718 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719
6720 pmu = NULL;
6721
6722 hwc = &event->hw;
6723 hwc->sample_period = attr->sample_period;
6724 if (attr->freq && attr->sample_freq)
6725 hwc->sample_period = 1;
6726 hwc->last_period = hwc->sample_period;
6727
Peter Zijlstrae7850592010-05-21 14:43:08 +02006728 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006729
6730 /*
6731 * we currently do not support PERF_FORMAT_GROUP on inherited events
6732 */
6733 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006734 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006736 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006737 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006738 goto err_ns;
6739 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006741 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742 }
6743
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006745 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6746 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006747 if (err)
6748 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006749 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750 }
6751
6752 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006753
6754err_pmu:
6755 if (event->destroy)
6756 event->destroy(event);
6757err_ns:
6758 if (event->ns)
6759 put_pid_ns(event->ns);
6760 kfree(event);
6761
6762 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006763}
6764
6765static int perf_copy_attr(struct perf_event_attr __user *uattr,
6766 struct perf_event_attr *attr)
6767{
6768 u32 size;
6769 int ret;
6770
6771 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6772 return -EFAULT;
6773
6774 /*
6775 * zero the full structure, so that a short copy will be nice.
6776 */
6777 memset(attr, 0, sizeof(*attr));
6778
6779 ret = get_user(size, &uattr->size);
6780 if (ret)
6781 return ret;
6782
6783 if (size > PAGE_SIZE) /* silly large */
6784 goto err_size;
6785
6786 if (!size) /* abi compat */
6787 size = PERF_ATTR_SIZE_VER0;
6788
6789 if (size < PERF_ATTR_SIZE_VER0)
6790 goto err_size;
6791
6792 /*
6793 * If we're handed a bigger struct than we know of,
6794 * ensure all the unknown bits are 0 - i.e. new
6795 * user-space does not rely on any kernel feature
6796 * extensions we dont know about yet.
6797 */
6798 if (size > sizeof(*attr)) {
6799 unsigned char __user *addr;
6800 unsigned char __user *end;
6801 unsigned char val;
6802
6803 addr = (void __user *)uattr + sizeof(*attr);
6804 end = (void __user *)uattr + size;
6805
6806 for (; addr < end; addr++) {
6807 ret = get_user(val, addr);
6808 if (ret)
6809 return ret;
6810 if (val)
6811 goto err_size;
6812 }
6813 size = sizeof(*attr);
6814 }
6815
6816 ret = copy_from_user(attr, uattr, size);
6817 if (ret)
6818 return -EFAULT;
6819
Stephane Eranian3090ffb2013-10-17 19:32:15 +02006820 /* disabled for now */
6821 if (attr->mmap2)
6822 return -EINVAL;
6823
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306824 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006825 return -EINVAL;
6826
6827 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6828 return -EINVAL;
6829
6830 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6831 return -EINVAL;
6832
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006833 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6834 u64 mask = attr->branch_sample_type;
6835
6836 /* only using defined bits */
6837 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6838 return -EINVAL;
6839
6840 /* at least one branch bit must be set */
6841 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6842 return -EINVAL;
6843
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006844 /* propagate priv level, when not set for branch */
6845 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6846
6847 /* exclude_kernel checked on syscall entry */
6848 if (!attr->exclude_kernel)
6849 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6850
6851 if (!attr->exclude_user)
6852 mask |= PERF_SAMPLE_BRANCH_USER;
6853
6854 if (!attr->exclude_hv)
6855 mask |= PERF_SAMPLE_BRANCH_HV;
6856 /*
6857 * adjust user setting (for HW filter setup)
6858 */
6859 attr->branch_sample_type = mask;
6860 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006861 /* privileged levels capture (kernel, hv): check permissions */
6862 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006863 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6864 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006865 }
Jiri Olsa40189942012-08-07 15:20:37 +02006866
Jiri Olsac5ebced2012-08-07 15:20:40 +02006867 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006868 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006869 if (ret)
6870 return ret;
6871 }
6872
6873 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6874 if (!arch_perf_have_user_stack_dump())
6875 return -ENOSYS;
6876
6877 /*
6878 * We have __u32 type for the size, but so far
6879 * we can only use __u16 as maximum due to the
6880 * __u16 sample size limit.
6881 */
6882 if (attr->sample_stack_user >= USHRT_MAX)
6883 ret = -EINVAL;
6884 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6885 ret = -EINVAL;
6886 }
Jiri Olsa40189942012-08-07 15:20:37 +02006887
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006888out:
6889 return ret;
6890
6891err_size:
6892 put_user(sizeof(*attr), &uattr->size);
6893 ret = -E2BIG;
6894 goto out;
6895}
6896
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006897static int
6898perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006900 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006901 int ret = -EINVAL;
6902
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006903 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006904 goto set;
6905
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006906 /* don't allow circular references */
6907 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006908 goto out;
6909
Peter Zijlstra0f139302010-05-20 14:35:15 +02006910 /*
6911 * Don't allow cross-cpu buffers
6912 */
6913 if (output_event->cpu != event->cpu)
6914 goto out;
6915
6916 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006917 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006918 */
6919 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6920 goto out;
6921
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006922set:
6923 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006924 /* Can't redirect output if we've got an active mmap() */
6925 if (atomic_read(&event->mmap_count))
6926 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006927
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006928 old_rb = event->rb;
6929
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006930 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006931 /* get the rb we want to redirect to */
6932 rb = ring_buffer_get(output_event);
6933 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006934 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006935 }
6936
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006937 if (old_rb)
6938 ring_buffer_detach(event, old_rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006939
6940 if (rb)
6941 ring_buffer_attach(event, rb);
6942
6943 rcu_assign_pointer(event->rb, rb);
6944
6945 if (old_rb) {
6946 ring_buffer_put(old_rb);
6947 /*
6948 * Since we detached before setting the new rb, so that we
6949 * could attach the new rb, we could have missed a wakeup.
6950 * Provide it now.
6951 */
6952 wake_up_all(&event->waitq);
6953 }
6954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006955 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006956unlock:
6957 mutex_unlock(&event->mmap_mutex);
6958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006959out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006960 return ret;
6961}
6962
6963/**
6964 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6965 *
6966 * @attr_uptr: event_id type attributes for monitoring/sampling
6967 * @pid: target pid
6968 * @cpu: target cpu
6969 * @group_fd: group leader event fd
6970 */
6971SYSCALL_DEFINE5(perf_event_open,
6972 struct perf_event_attr __user *, attr_uptr,
6973 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6974{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006975 struct perf_event *group_leader = NULL, *output_event = NULL;
6976 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006977 struct perf_event_attr attr;
6978 struct perf_event_context *ctx;
6979 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006980 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006981 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006982 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006983 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006984 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006985 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01006986 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987
6988 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006989 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990 return -EINVAL;
6991
6992 err = perf_copy_attr(attr_uptr, &attr);
6993 if (err)
6994 return err;
6995
6996 if (!attr.exclude_kernel) {
6997 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6998 return -EACCES;
6999 }
7000
7001 if (attr.freq) {
7002 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7003 return -EINVAL;
7004 }
7005
Stephane Eraniane5d13672011-02-14 11:20:01 +02007006 /*
7007 * In cgroup mode, the pid argument is used to pass the fd
7008 * opened to the cgroup directory in cgroupfs. The cpu argument
7009 * designates the cpu on which to monitor threads from that
7010 * cgroup.
7011 */
7012 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7013 return -EINVAL;
7014
Yann Droneauda21b0b32014-01-05 21:36:33 +01007015 if (flags & PERF_FLAG_FD_CLOEXEC)
7016 f_flags |= O_CLOEXEC;
7017
7018 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007019 if (event_fd < 0)
7020 return event_fd;
7021
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007022 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007023 err = perf_fget_light(group_fd, &group);
7024 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007025 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007026 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007027 if (flags & PERF_FLAG_FD_OUTPUT)
7028 output_event = group_leader;
7029 if (flags & PERF_FLAG_FD_NO_GROUP)
7030 group_leader = NULL;
7031 }
7032
Stephane Eraniane5d13672011-02-14 11:20:01 +02007033 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007034 task = find_lively_task_by_vpid(pid);
7035 if (IS_ERR(task)) {
7036 err = PTR_ERR(task);
7037 goto err_group_fd;
7038 }
7039 }
7040
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007041 get_online_cpus();
7042
Avi Kivity4dc0da82011-06-29 18:42:35 +03007043 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7044 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007045 if (IS_ERR(event)) {
7046 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007047 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007048 }
7049
Stephane Eraniane5d13672011-02-14 11:20:01 +02007050 if (flags & PERF_FLAG_PID_CGROUP) {
7051 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007052 if (err) {
7053 __free_event(event);
7054 goto err_task;
7055 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007056 }
7057
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007058 account_event(event);
7059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007060 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007061 * Special case software events and allow them to be part of
7062 * any hardware group.
7063 */
7064 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007065
7066 if (group_leader &&
7067 (is_software_event(event) != is_software_event(group_leader))) {
7068 if (is_software_event(event)) {
7069 /*
7070 * If event and group_leader are not both a software
7071 * event, and event is, then group leader is not.
7072 *
7073 * Allow the addition of software events to !software
7074 * groups, this is safe because software events never
7075 * fail to schedule.
7076 */
7077 pmu = group_leader->pmu;
7078 } else if (is_software_event(group_leader) &&
7079 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7080 /*
7081 * In case the group is a pure software group, and we
7082 * try to add a hardware event, move the whole group to
7083 * the hardware context.
7084 */
7085 move_group = 1;
7086 }
7087 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007088
7089 /*
7090 * Get the target context (task or percpu):
7091 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007092 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007093 if (IS_ERR(ctx)) {
7094 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007095 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007096 }
7097
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007098 if (task) {
7099 put_task_struct(task);
7100 task = NULL;
7101 }
7102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007103 /*
7104 * Look up the group leader (we will attach this event to it):
7105 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007106 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007107 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007108
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007109 /*
7110 * Do not allow a recursive hierarchy (this new sibling
7111 * becoming part of another group-sibling):
7112 */
7113 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007114 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007115 /*
7116 * Do not allow to attach to a group in a different
7117 * task or CPU context:
7118 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007119 if (move_group) {
7120 if (group_leader->ctx->type != ctx->type)
7121 goto err_context;
7122 } else {
7123 if (group_leader->ctx != ctx)
7124 goto err_context;
7125 }
7126
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007127 /*
7128 * Only a group leader can be exclusive or pinned
7129 */
7130 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007131 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007132 }
7133
7134 if (output_event) {
7135 err = perf_event_set_output(event, output_event);
7136 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007137 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007138 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007139
Yann Droneauda21b0b32014-01-05 21:36:33 +01007140 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7141 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007142 if (IS_ERR(event_file)) {
7143 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007144 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007145 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007147 if (move_group) {
7148 struct perf_event_context *gctx = group_leader->ctx;
7149
7150 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007151 perf_remove_from_context(group_leader);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007152
7153 /*
7154 * Removing from the context ends up with disabled
7155 * event. What we want here is event in the initial
7156 * startup state, ready to be add into new context.
7157 */
7158 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007159 list_for_each_entry(sibling, &group_leader->sibling_list,
7160 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007161 perf_remove_from_context(sibling);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007162 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007163 put_ctx(gctx);
7164 }
7165 mutex_unlock(&gctx->mutex);
7166 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007167 }
7168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007169 WARN_ON_ONCE(ctx->parent_ctx);
7170 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007171
7172 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007173 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007174 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007175 get_ctx(ctx);
7176 list_for_each_entry(sibling, &group_leader->sibling_list,
7177 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007178 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007179 get_ctx(ctx);
7180 }
7181 }
7182
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007183 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007184 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007185 mutex_unlock(&ctx->mutex);
7186
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007187 put_online_cpus();
7188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007191 mutex_lock(&current->perf_event_mutex);
7192 list_add_tail(&event->owner_entry, &current->perf_event_list);
7193 mutex_unlock(&current->perf_event_mutex);
7194
Peter Zijlstra8a495422010-05-27 15:47:49 +02007195 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007196 * Precalculate sample_data sizes
7197 */
7198 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007199 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007200
7201 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007202 * Drop the reference on the group_event after placing the
7203 * new event on the sibling_list. This ensures destruction
7204 * of the group leader will find the pointer to itself in
7205 * perf_group_detach().
7206 */
Al Viro2903ff02012-08-28 12:52:22 -04007207 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007208 fd_install(event_fd, event_file);
7209 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007210
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007211err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007212 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007213 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007214err_alloc:
7215 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007216err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007217 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007218 if (task)
7219 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007220err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007221 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007222err_fd:
7223 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224 return err;
7225}
7226
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007227/**
7228 * perf_event_create_kernel_counter
7229 *
7230 * @attr: attributes of the counter to create
7231 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007232 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007233 */
7234struct perf_event *
7235perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007236 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007237 perf_overflow_handler_t overflow_handler,
7238 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007239{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007240 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007241 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007242 int err;
7243
7244 /*
7245 * Get the target context (task or percpu):
7246 */
7247
Avi Kivity4dc0da82011-06-29 18:42:35 +03007248 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7249 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007250 if (IS_ERR(event)) {
7251 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007252 goto err;
7253 }
7254
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007255 account_event(event);
7256
Matt Helsley38a81da2010-09-13 13:01:20 -07007257 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007258 if (IS_ERR(ctx)) {
7259 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007260 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007261 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007262
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007263 WARN_ON_ONCE(ctx->parent_ctx);
7264 mutex_lock(&ctx->mutex);
7265 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007266 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007267 mutex_unlock(&ctx->mutex);
7268
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007269 return event;
7270
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007271err_free:
7272 free_event(event);
7273err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007274 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007275}
7276EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7277
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007278void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7279{
7280 struct perf_event_context *src_ctx;
7281 struct perf_event_context *dst_ctx;
7282 struct perf_event *event, *tmp;
7283 LIST_HEAD(events);
7284
7285 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7286 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7287
7288 mutex_lock(&src_ctx->mutex);
7289 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7290 event_entry) {
7291 perf_remove_from_context(event);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007292 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007293 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007294 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007295 }
7296 mutex_unlock(&src_ctx->mutex);
7297
7298 synchronize_rcu();
7299
7300 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007301 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7302 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007303 if (event->state >= PERF_EVENT_STATE_OFF)
7304 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007305 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007306 perf_install_in_context(dst_ctx, event, dst_cpu);
7307 get_ctx(dst_ctx);
7308 }
7309 mutex_unlock(&dst_ctx->mutex);
7310}
7311EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007313static void sync_child_event(struct perf_event *child_event,
7314 struct task_struct *child)
7315{
7316 struct perf_event *parent_event = child_event->parent;
7317 u64 child_val;
7318
7319 if (child_event->attr.inherit_stat)
7320 perf_event_read_event(child_event, child);
7321
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007322 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007323
7324 /*
7325 * Add back the child's count to the parent's count:
7326 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007327 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328 atomic64_add(child_event->total_time_enabled,
7329 &parent_event->child_total_time_enabled);
7330 atomic64_add(child_event->total_time_running,
7331 &parent_event->child_total_time_running);
7332
7333 /*
7334 * Remove this event from the parent's list
7335 */
7336 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7337 mutex_lock(&parent_event->child_mutex);
7338 list_del_init(&child_event->child_list);
7339 mutex_unlock(&parent_event->child_mutex);
7340
7341 /*
7342 * Release the parent event, if this was the last
7343 * reference to it.
7344 */
Al Viroa6fa9412012-08-20 14:59:25 +01007345 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007346}
7347
7348static void
7349__perf_event_exit_task(struct perf_event *child_event,
7350 struct perf_event_context *child_ctx,
7351 struct task_struct *child)
7352{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007353 if (child_event->parent) {
7354 raw_spin_lock_irq(&child_ctx->lock);
7355 perf_group_detach(child_event);
7356 raw_spin_unlock_irq(&child_ctx->lock);
7357 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007358
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007359 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007361 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007362 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007363 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007364 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007365 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007366 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007367 sync_child_event(child_event, child);
7368 free_event(child_event);
7369 }
7370}
7371
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007372static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007373{
7374 struct perf_event *child_event, *tmp;
7375 struct perf_event_context *child_ctx;
7376 unsigned long flags;
7377
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007378 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007379 perf_event_task(child, NULL, 0);
7380 return;
7381 }
7382
7383 local_irq_save(flags);
7384 /*
7385 * We can't reschedule here because interrupts are disabled,
7386 * and either child is current or it is a task that can't be
7387 * scheduled, so we are now safe from rescheduling changing
7388 * our context.
7389 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007390 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007391
7392 /*
7393 * Take the context lock here so that if find_get_context is
7394 * reading child->perf_event_ctxp, we wait until it has
7395 * incremented the context's refcount before we do put_ctx below.
7396 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007397 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007398 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007399 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007400 /*
7401 * If this context is a clone; unclone it so it can't get
7402 * swapped to another process while we're removing all
7403 * the events from it.
7404 */
7405 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007406 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007407 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007408
7409 /*
7410 * Report the task dead after unscheduling the events so that we
7411 * won't get any samples after PERF_RECORD_EXIT. We can however still
7412 * get a few PERF_RECORD_READ events.
7413 */
7414 perf_event_task(child, child_ctx, 0);
7415
7416 /*
7417 * We can recurse on the same lock type through:
7418 *
7419 * __perf_event_exit_task()
7420 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007421 * put_event()
7422 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423 *
7424 * But since its the parent context it won't be the same instance.
7425 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007426 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007427
7428again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007429 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7430 group_entry)
7431 __perf_event_exit_task(child_event, child_ctx, child);
7432
7433 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007434 group_entry)
7435 __perf_event_exit_task(child_event, child_ctx, child);
7436
7437 /*
7438 * If the last event was a group event, it will have appended all
7439 * its siblings to the list, but we obtained 'tmp' before that which
7440 * will still point to the list head terminating the iteration.
7441 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007442 if (!list_empty(&child_ctx->pinned_groups) ||
7443 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007444 goto again;
7445
7446 mutex_unlock(&child_ctx->mutex);
7447
7448 put_ctx(child_ctx);
7449}
7450
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007451/*
7452 * When a child task exits, feed back event values to parent events.
7453 */
7454void perf_event_exit_task(struct task_struct *child)
7455{
Peter Zijlstra88821352010-11-09 19:01:43 +01007456 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007457 int ctxn;
7458
Peter Zijlstra88821352010-11-09 19:01:43 +01007459 mutex_lock(&child->perf_event_mutex);
7460 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7461 owner_entry) {
7462 list_del_init(&event->owner_entry);
7463
7464 /*
7465 * Ensure the list deletion is visible before we clear
7466 * the owner, closes a race against perf_release() where
7467 * we need to serialize on the owner->perf_event_mutex.
7468 */
7469 smp_wmb();
7470 event->owner = NULL;
7471 }
7472 mutex_unlock(&child->perf_event_mutex);
7473
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007474 for_each_task_context_nr(ctxn)
7475 perf_event_exit_task_context(child, ctxn);
7476}
7477
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007478static void perf_free_event(struct perf_event *event,
7479 struct perf_event_context *ctx)
7480{
7481 struct perf_event *parent = event->parent;
7482
7483 if (WARN_ON_ONCE(!parent))
7484 return;
7485
7486 mutex_lock(&parent->child_mutex);
7487 list_del_init(&event->child_list);
7488 mutex_unlock(&parent->child_mutex);
7489
Al Viroa6fa9412012-08-20 14:59:25 +01007490 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007491
Peter Zijlstra8a495422010-05-27 15:47:49 +02007492 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007493 list_del_event(event, ctx);
7494 free_event(event);
7495}
7496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007497/*
7498 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007499 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007500 */
7501void perf_event_free_task(struct task_struct *task)
7502{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007503 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007504 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007505 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007506
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007507 for_each_task_context_nr(ctxn) {
7508 ctx = task->perf_event_ctxp[ctxn];
7509 if (!ctx)
7510 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007511
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007512 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007513again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007514 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7515 group_entry)
7516 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007517
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007518 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7519 group_entry)
7520 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007521
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007522 if (!list_empty(&ctx->pinned_groups) ||
7523 !list_empty(&ctx->flexible_groups))
7524 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007525
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007526 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007527
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007528 put_ctx(ctx);
7529 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007530}
7531
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007532void perf_event_delayed_put(struct task_struct *task)
7533{
7534 int ctxn;
7535
7536 for_each_task_context_nr(ctxn)
7537 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7538}
7539
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007540/*
7541 * inherit a event from parent task to child task:
7542 */
7543static struct perf_event *
7544inherit_event(struct perf_event *parent_event,
7545 struct task_struct *parent,
7546 struct perf_event_context *parent_ctx,
7547 struct task_struct *child,
7548 struct perf_event *group_leader,
7549 struct perf_event_context *child_ctx)
7550{
7551 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007552 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007553
7554 /*
7555 * Instead of creating recursive hierarchies of events,
7556 * we link inherited events back to the original parent,
7557 * which has a filp for sure, which we use as the reference
7558 * count:
7559 */
7560 if (parent_event->parent)
7561 parent_event = parent_event->parent;
7562
7563 child_event = perf_event_alloc(&parent_event->attr,
7564 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007565 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007566 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007567 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007568 if (IS_ERR(child_event))
7569 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007570
7571 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7572 free_event(child_event);
7573 return NULL;
7574 }
7575
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007576 get_ctx(child_ctx);
7577
7578 /*
7579 * Make the child state follow the state of the parent event,
7580 * not its attr.disabled bit. We hold the parent's mutex,
7581 * so we won't race with perf_event_{en, dis}able_family.
7582 */
7583 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7584 child_event->state = PERF_EVENT_STATE_INACTIVE;
7585 else
7586 child_event->state = PERF_EVENT_STATE_OFF;
7587
7588 if (parent_event->attr.freq) {
7589 u64 sample_period = parent_event->hw.sample_period;
7590 struct hw_perf_event *hwc = &child_event->hw;
7591
7592 hwc->sample_period = sample_period;
7593 hwc->last_period = sample_period;
7594
7595 local64_set(&hwc->period_left, sample_period);
7596 }
7597
7598 child_event->ctx = child_ctx;
7599 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007600 child_event->overflow_handler_context
7601 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007602
7603 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007604 * Precalculate sample_data sizes
7605 */
7606 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007607 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007608
7609 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007610 * Link it up in the child's context:
7611 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007612 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007613 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007614 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007615
7616 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007617 * Link this into the parent event's child list
7618 */
7619 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7620 mutex_lock(&parent_event->child_mutex);
7621 list_add_tail(&child_event->child_list, &parent_event->child_list);
7622 mutex_unlock(&parent_event->child_mutex);
7623
7624 return child_event;
7625}
7626
7627static int inherit_group(struct perf_event *parent_event,
7628 struct task_struct *parent,
7629 struct perf_event_context *parent_ctx,
7630 struct task_struct *child,
7631 struct perf_event_context *child_ctx)
7632{
7633 struct perf_event *leader;
7634 struct perf_event *sub;
7635 struct perf_event *child_ctr;
7636
7637 leader = inherit_event(parent_event, parent, parent_ctx,
7638 child, NULL, child_ctx);
7639 if (IS_ERR(leader))
7640 return PTR_ERR(leader);
7641 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7642 child_ctr = inherit_event(sub, parent, parent_ctx,
7643 child, leader, child_ctx);
7644 if (IS_ERR(child_ctr))
7645 return PTR_ERR(child_ctr);
7646 }
7647 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007648}
7649
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007650static int
7651inherit_task_group(struct perf_event *event, struct task_struct *parent,
7652 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007653 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007654 int *inherited_all)
7655{
7656 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007657 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007658
7659 if (!event->attr.inherit) {
7660 *inherited_all = 0;
7661 return 0;
7662 }
7663
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007664 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007665 if (!child_ctx) {
7666 /*
7667 * This is executed from the parent task context, so
7668 * inherit events that have been marked for cloning.
7669 * First allocate and initialize a context for the
7670 * child.
7671 */
7672
Jiri Olsa734df5a2013-07-09 17:44:10 +02007673 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007674 if (!child_ctx)
7675 return -ENOMEM;
7676
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007677 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007678 }
7679
7680 ret = inherit_group(event, parent, parent_ctx,
7681 child, child_ctx);
7682
7683 if (ret)
7684 *inherited_all = 0;
7685
7686 return ret;
7687}
7688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007689/*
7690 * Initialize the perf_event context in task_struct
7691 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007692int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007693{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007694 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007695 struct perf_event_context *cloned_ctx;
7696 struct perf_event *event;
7697 struct task_struct *parent = current;
7698 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007699 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007700 int ret = 0;
7701
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007702 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007703 return 0;
7704
7705 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007706 * If the parent's context is a clone, pin it so it won't get
7707 * swapped under us.
7708 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007709 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007710
7711 /*
7712 * No need to check if parent_ctx != NULL here; since we saw
7713 * it non-NULL earlier, the only reason for it to become NULL
7714 * is if we exit, and since we're currently in the middle of
7715 * a fork we can't be exiting at the same time.
7716 */
7717
7718 /*
7719 * Lock the parent list. No need to lock the child - not PID
7720 * hashed yet and not running, so nobody can access it.
7721 */
7722 mutex_lock(&parent_ctx->mutex);
7723
7724 /*
7725 * We dont have to disable NMIs - we are only looking at
7726 * the list, not manipulating it:
7727 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007728 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007729 ret = inherit_task_group(event, parent, parent_ctx,
7730 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007731 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007732 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007733 }
7734
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007735 /*
7736 * We can't hold ctx->lock when iterating the ->flexible_group list due
7737 * to allocations, but we need to prevent rotation because
7738 * rotate_ctx() will change the list from interrupt context.
7739 */
7740 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7741 parent_ctx->rotate_disable = 1;
7742 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7743
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007744 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007745 ret = inherit_task_group(event, parent, parent_ctx,
7746 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007747 if (ret)
7748 break;
7749 }
7750
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007751 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7752 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007753
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007754 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007755
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007756 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007757 /*
7758 * Mark the child context as a clone of the parent
7759 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007760 *
7761 * Note that if the parent is a clone, the holding of
7762 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007763 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007764 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007765 if (cloned_ctx) {
7766 child_ctx->parent_ctx = cloned_ctx;
7767 child_ctx->parent_gen = parent_ctx->parent_gen;
7768 } else {
7769 child_ctx->parent_ctx = parent_ctx;
7770 child_ctx->parent_gen = parent_ctx->generation;
7771 }
7772 get_ctx(child_ctx->parent_ctx);
7773 }
7774
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007775 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776 mutex_unlock(&parent_ctx->mutex);
7777
7778 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007779 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007780
7781 return ret;
7782}
7783
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007784/*
7785 * Initialize the perf_event context in task_struct
7786 */
7787int perf_event_init_task(struct task_struct *child)
7788{
7789 int ctxn, ret;
7790
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007791 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7792 mutex_init(&child->perf_event_mutex);
7793 INIT_LIST_HEAD(&child->perf_event_list);
7794
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007795 for_each_task_context_nr(ctxn) {
7796 ret = perf_event_init_context(child, ctxn);
7797 if (ret)
7798 return ret;
7799 }
7800
7801 return 0;
7802}
7803
Paul Mackerras220b1402010-03-10 20:45:52 +11007804static void __init perf_event_init_all_cpus(void)
7805{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007806 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007807 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007808
7809 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007810 swhash = &per_cpu(swevent_htable, cpu);
7811 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007812 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007813 }
7814}
7815
Paul Gortmaker0db06282013-06-19 14:53:51 -04007816static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007817{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007818 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007819
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007820 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007821 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007822 struct swevent_hlist *hlist;
7823
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007824 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7825 WARN_ON(!hlist);
7826 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007827 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007828 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007829}
7830
Peter Zijlstrac2774432010-12-08 15:29:02 +01007831#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007832static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007833{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007834 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7835
7836 WARN_ON(!irqs_disabled());
7837
7838 list_del_init(&cpuctx->rotation_list);
7839}
7840
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007841static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007842{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007843 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007844 struct perf_event *event, *tmp;
7845
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007846 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007847
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007848 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007849 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007850 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007851 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007852}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007853
7854static void perf_event_exit_cpu_context(int cpu)
7855{
7856 struct perf_event_context *ctx;
7857 struct pmu *pmu;
7858 int idx;
7859
7860 idx = srcu_read_lock(&pmus_srcu);
7861 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007862 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007863
7864 mutex_lock(&ctx->mutex);
7865 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7866 mutex_unlock(&ctx->mutex);
7867 }
7868 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007869}
7870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007871static void perf_event_exit_cpu(int cpu)
7872{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007873 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007874
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007875 mutex_lock(&swhash->hlist_mutex);
7876 swevent_hlist_release(swhash);
7877 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007878
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007879 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007880}
7881#else
7882static inline void perf_event_exit_cpu(int cpu) { }
7883#endif
7884
Peter Zijlstrac2774432010-12-08 15:29:02 +01007885static int
7886perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7887{
7888 int cpu;
7889
7890 for_each_online_cpu(cpu)
7891 perf_event_exit_cpu(cpu);
7892
7893 return NOTIFY_OK;
7894}
7895
7896/*
7897 * Run the perf reboot notifier at the very last possible moment so that
7898 * the generic watchdog code runs as long as possible.
7899 */
7900static struct notifier_block perf_reboot_notifier = {
7901 .notifier_call = perf_reboot,
7902 .priority = INT_MIN,
7903};
7904
Paul Gortmaker0db06282013-06-19 14:53:51 -04007905static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007906perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7907{
7908 unsigned int cpu = (long)hcpu;
7909
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007910 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007911
7912 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007913 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007914 perf_event_init_cpu(cpu);
7915 break;
7916
Peter Zijlstra5e116372010-06-11 13:35:08 +02007917 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007918 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007919 perf_event_exit_cpu(cpu);
7920 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007921 default:
7922 break;
7923 }
7924
7925 return NOTIFY_OK;
7926}
7927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007928void __init perf_event_init(void)
7929{
Jason Wessel3c502e72010-11-04 17:33:01 -05007930 int ret;
7931
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007932 idr_init(&pmu_idr);
7933
Paul Mackerras220b1402010-03-10 20:45:52 +11007934 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007935 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007936 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7937 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7938 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007939 perf_tp_register();
7940 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007941 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007942
7943 ret = init_hw_breakpoint();
7944 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007945
7946 /* do not patch jump label more than once per second */
7947 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007948
7949 /*
7950 * Build time assertion that we keep the data_head at the intended
7951 * location. IOW, validation we got the __reserved[] size right.
7952 */
7953 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7954 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007955}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007956
7957static int __init perf_event_sysfs_init(void)
7958{
7959 struct pmu *pmu;
7960 int ret;
7961
7962 mutex_lock(&pmus_lock);
7963
7964 ret = bus_register(&pmu_bus);
7965 if (ret)
7966 goto unlock;
7967
7968 list_for_each_entry(pmu, &pmus, entry) {
7969 if (!pmu->name || pmu->type < 0)
7970 continue;
7971
7972 ret = pmu_dev_alloc(pmu);
7973 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7974 }
7975 pmu_bus_running = 1;
7976 ret = 0;
7977
7978unlock:
7979 mutex_unlock(&pmus_lock);
7980
7981 return ret;
7982}
7983device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007984
7985#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04007986static struct cgroup_subsys_state *
7987perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007988{
7989 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007990
Li Zefan1b15d052011-03-03 14:26:06 +08007991 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007992 if (!jc)
7993 return ERR_PTR(-ENOMEM);
7994
Stephane Eraniane5d13672011-02-14 11:20:01 +02007995 jc->info = alloc_percpu(struct perf_cgroup_info);
7996 if (!jc->info) {
7997 kfree(jc);
7998 return ERR_PTR(-ENOMEM);
7999 }
8000
Stephane Eraniane5d13672011-02-14 11:20:01 +02008001 return &jc->css;
8002}
8003
Tejun Heoeb954192013-08-08 20:11:23 -04008004static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008005{
Tejun Heoeb954192013-08-08 20:11:23 -04008006 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8007
Stephane Eraniane5d13672011-02-14 11:20:01 +02008008 free_percpu(jc->info);
8009 kfree(jc);
8010}
8011
8012static int __perf_cgroup_move(void *info)
8013{
8014 struct task_struct *task = info;
8015 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8016 return 0;
8017}
8018
Tejun Heoeb954192013-08-08 20:11:23 -04008019static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8020 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008021{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008022 struct task_struct *task;
8023
Tejun Heod99c8722013-08-08 20:11:27 -04008024 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008025 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008026}
8027
Tejun Heoeb954192013-08-08 20:11:23 -04008028static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8029 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008030 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008031{
8032 /*
8033 * cgroup_exit() is called in the copy_process() failure path.
8034 * Ignore this case since the task hasn't ran yet, this avoids
8035 * trying to poke a half freed task state from generic code.
8036 */
8037 if (!(task->flags & PF_EXITING))
8038 return;
8039
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
8043struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008044 .name = "perf_event",
8045 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08008046 .css_alloc = perf_cgroup_css_alloc,
8047 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008048 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008049 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008050};
8051#endif /* CONFIG_CGROUP_PERF */