blob: f9ff5493171d83208b140d19f8276fe3908e670b [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020021#include <linux/sysfs.h>
22#include <linux/dcache.h>
23#include <linux/percpu.h>
24#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010025#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010027#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040028#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020029#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020030#include <linux/hardirq.h>
31#include <linux/rculist.h>
32#include <linux/uaccess.h>
33#include <linux/syscalls.h>
34#include <linux/anon_inodes.h>
35#include <linux/kernel_stat.h>
36#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080037#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050038#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020039#include <linux/mm_types.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020040
Frederic Weisbecker76369132011-05-19 19:55:04 +020041#include "internal.h"
42
Ingo Molnarcdd6c482009-09-21 12:02:48 +020043#include <asm/irq_regs.h>
44
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010045struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020046 struct task_struct *p;
47 int (*func)(void *info);
48 void *info;
49 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010050};
51
52static void remote_function(void *data)
53{
54 struct remote_function_call *tfc = data;
55 struct task_struct *p = tfc->p;
56
57 if (p) {
58 tfc->ret = -EAGAIN;
59 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
60 return;
61 }
62
63 tfc->ret = tfc->func(tfc->info);
64}
65
66/**
67 * task_function_call - call a function on the cpu on which a task runs
68 * @p: the task to evaluate
69 * @func: the function to be called
70 * @info: the function call argument
71 *
72 * Calls the function @func when the task is currently running. This might
73 * be on the current CPU, which just calls the function directly
74 *
75 * returns: @func return value, or
76 * -ESRCH - when the process isn't running
77 * -EAGAIN - when the process moved away
78 */
79static int
80task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
81{
82 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020083 .p = p,
84 .func = func,
85 .info = info,
86 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010087 };
88
89 if (task_curr(p))
90 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
91
92 return data.ret;
93}
94
95/**
96 * cpu_function_call - call a function on the cpu
97 * @func: the function to be called
98 * @info: the function call argument
99 *
100 * Calls the function @func on the remote cpu.
101 *
102 * returns: @func return value or -ENXIO when the cpu is offline
103 */
104static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
105{
106 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200107 .p = NULL,
108 .func = func,
109 .info = info,
110 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100111 };
112
113 smp_call_function_single(cpu, remote_function, &data, 1);
114
115 return data.ret;
116}
117
Stephane Eraniane5d13672011-02-14 11:20:01 +0200118#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
119 PERF_FLAG_FD_OUTPUT |\
120 PERF_FLAG_PID_CGROUP)
121
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100122/*
123 * branch priv levels that need permission checks
124 */
125#define PERF_SAMPLE_BRANCH_PERM_PLM \
126 (PERF_SAMPLE_BRANCH_KERNEL |\
127 PERF_SAMPLE_BRANCH_HV)
128
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200129enum event_type_t {
130 EVENT_FLEXIBLE = 0x1,
131 EVENT_PINNED = 0x2,
132 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
133};
134
Stephane Eraniane5d13672011-02-14 11:20:01 +0200135/*
136 * perf_sched_events : >0 events exist
137 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
138 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100139struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200140static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100141static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200142
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200143static atomic_t nr_mmap_events __read_mostly;
144static atomic_t nr_comm_events __read_mostly;
145static atomic_t nr_task_events __read_mostly;
146
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200147static LIST_HEAD(pmus);
148static DEFINE_MUTEX(pmus_lock);
149static struct srcu_struct pmus_srcu;
150
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151/*
152 * perf event paranoia level:
153 * -1 - not paranoid at all
154 * 0 - disallow raw tracepoint access for unpriv
155 * 1 - disallow cpu events for unpriv
156 * 2 - disallow kernel profiling for unpriv
157 */
158int sysctl_perf_event_paranoid __read_mostly = 1;
159
Frederic Weisbecker20443382011-03-31 03:33:29 +0200160/* Minimum for 512 kiB + 1 user control page */
161int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162
163/*
164 * max perf event sample rate
165 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100166#define DEFAULT_MAX_SAMPLE_RATE 100000
167int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
168static int max_samples_per_tick __read_mostly =
169 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
170
171int perf_proc_update_handler(struct ctl_table *table, int write,
172 void __user *buffer, size_t *lenp,
173 loff_t *ppos)
174{
175 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
176
177 if (ret || !write)
178 return ret;
179
180 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
181
182 return 0;
183}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200184
185static atomic64_t perf_event_id;
186
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200187static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
188 enum event_type_t event_type);
189
190static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200191 enum event_type_t event_type,
192 struct task_struct *task);
193
194static void update_context_time(struct perf_event_context *ctx);
195static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200196
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100197static void ring_buffer_attach(struct perf_event *event,
198 struct ring_buffer *rb);
199
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200200void __weak perf_event_print_debug(void) { }
201
Matt Fleming84c79912010-10-03 21:41:13 +0100202extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203{
Matt Fleming84c79912010-10-03 21:41:13 +0100204 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200205}
206
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200207static inline u64 perf_clock(void)
208{
209 return local_clock();
210}
211
Stephane Eraniane5d13672011-02-14 11:20:01 +0200212static inline struct perf_cpu_context *
213__get_cpu_context(struct perf_event_context *ctx)
214{
215 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
216}
217
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200218static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
219 struct perf_event_context *ctx)
220{
221 raw_spin_lock(&cpuctx->ctx.lock);
222 if (ctx)
223 raw_spin_lock(&ctx->lock);
224}
225
226static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
227 struct perf_event_context *ctx)
228{
229 if (ctx)
230 raw_spin_unlock(&ctx->lock);
231 raw_spin_unlock(&cpuctx->ctx.lock);
232}
233
Stephane Eraniane5d13672011-02-14 11:20:01 +0200234#ifdef CONFIG_CGROUP_PERF
235
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200236/*
237 * Must ensure cgroup is pinned (css_get) before calling
238 * this function. In other words, we cannot call this function
239 * if there is no cgroup event for the current CPU context.
240 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200241static inline struct perf_cgroup *
242perf_cgroup_from_task(struct task_struct *task)
243{
244 return container_of(task_subsys_state(task, perf_subsys_id),
245 struct perf_cgroup, css);
246}
247
248static inline bool
249perf_cgroup_match(struct perf_event *event)
250{
251 struct perf_event_context *ctx = event->ctx;
252 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
253
254 return !event->cgrp || event->cgrp == cpuctx->cgrp;
255}
256
Salman Qazi9c5da092012-06-14 15:31:09 -0700257static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200258{
Salman Qazi9c5da092012-06-14 15:31:09 -0700259 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200260}
261
262static inline void perf_put_cgroup(struct perf_event *event)
263{
264 css_put(&event->cgrp->css);
265}
266
267static inline void perf_detach_cgroup(struct perf_event *event)
268{
269 perf_put_cgroup(event);
270 event->cgrp = NULL;
271}
272
273static inline int is_cgroup_event(struct perf_event *event)
274{
275 return event->cgrp != NULL;
276}
277
278static inline u64 perf_cgroup_event_time(struct perf_event *event)
279{
280 struct perf_cgroup_info *t;
281
282 t = per_cpu_ptr(event->cgrp->info, event->cpu);
283 return t->time;
284}
285
286static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
287{
288 struct perf_cgroup_info *info;
289 u64 now;
290
291 now = perf_clock();
292
293 info = this_cpu_ptr(cgrp->info);
294
295 info->time += now - info->timestamp;
296 info->timestamp = now;
297}
298
299static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
300{
301 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
302 if (cgrp_out)
303 __update_cgrp_time(cgrp_out);
304}
305
306static inline void update_cgrp_time_from_event(struct perf_event *event)
307{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200308 struct perf_cgroup *cgrp;
309
Stephane Eraniane5d13672011-02-14 11:20:01 +0200310 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200311 * ensure we access cgroup data only when needed and
312 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200313 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200314 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200315 return;
316
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200317 cgrp = perf_cgroup_from_task(current);
318 /*
319 * Do not update time when cgroup is not active
320 */
321 if (cgrp == event->cgrp)
322 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200323}
324
325static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200326perf_cgroup_set_timestamp(struct task_struct *task,
327 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200328{
329 struct perf_cgroup *cgrp;
330 struct perf_cgroup_info *info;
331
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200332 /*
333 * ctx->lock held by caller
334 * ensure we do not access cgroup data
335 * unless we have the cgroup pinned (css_get)
336 */
337 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200338 return;
339
340 cgrp = perf_cgroup_from_task(task);
341 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200342 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200343}
344
345#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
346#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
347
348/*
349 * reschedule events based on the cgroup constraint of task.
350 *
351 * mode SWOUT : schedule out everything
352 * mode SWIN : schedule in based on cgroup for next
353 */
354void perf_cgroup_switch(struct task_struct *task, int mode)
355{
356 struct perf_cpu_context *cpuctx;
357 struct pmu *pmu;
358 unsigned long flags;
359
360 /*
361 * disable interrupts to avoid geting nr_cgroup
362 * changes via __perf_event_disable(). Also
363 * avoids preemption.
364 */
365 local_irq_save(flags);
366
367 /*
368 * we reschedule only in the presence of cgroup
369 * constrained events.
370 */
371 rcu_read_lock();
372
373 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200374 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200375 if (cpuctx->unique_pmu != pmu)
376 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200377
Stephane Eraniane5d13672011-02-14 11:20:01 +0200378 /*
379 * perf_cgroup_events says at least one
380 * context on this CPU has cgroup events.
381 *
382 * ctx->nr_cgroups reports the number of cgroup
383 * events for a context.
384 */
385 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200386 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
387 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200388
389 if (mode & PERF_CGROUP_SWOUT) {
390 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
391 /*
392 * must not be done before ctxswout due
393 * to event_filter_match() in event_sched_out()
394 */
395 cpuctx->cgrp = NULL;
396 }
397
398 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200399 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200400 /*
401 * set cgrp before ctxsw in to allow
402 * event_filter_match() to not have to pass
403 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200404 */
405 cpuctx->cgrp = perf_cgroup_from_task(task);
406 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
407 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200408 perf_pmu_enable(cpuctx->ctx.pmu);
409 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200410 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200411 }
412
413 rcu_read_unlock();
414
415 local_irq_restore(flags);
416}
417
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200418static inline void perf_cgroup_sched_out(struct task_struct *task,
419 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200420{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200421 struct perf_cgroup *cgrp1;
422 struct perf_cgroup *cgrp2 = NULL;
423
424 /*
425 * we come here when we know perf_cgroup_events > 0
426 */
427 cgrp1 = perf_cgroup_from_task(task);
428
429 /*
430 * next is NULL when called from perf_event_enable_on_exec()
431 * that will systematically cause a cgroup_switch()
432 */
433 if (next)
434 cgrp2 = perf_cgroup_from_task(next);
435
436 /*
437 * only schedule out current cgroup events if we know
438 * that we are switching to a different cgroup. Otherwise,
439 * do no touch the cgroup events.
440 */
441 if (cgrp1 != cgrp2)
442 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443}
444
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200445static inline void perf_cgroup_sched_in(struct task_struct *prev,
446 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200447{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200448 struct perf_cgroup *cgrp1;
449 struct perf_cgroup *cgrp2 = NULL;
450
451 /*
452 * we come here when we know perf_cgroup_events > 0
453 */
454 cgrp1 = perf_cgroup_from_task(task);
455
456 /* prev can never be NULL */
457 cgrp2 = perf_cgroup_from_task(prev);
458
459 /*
460 * only need to schedule in cgroup events if we are changing
461 * cgroup during ctxsw. Cgroup events were not scheduled
462 * out of ctxsw out if that was not the case.
463 */
464 if (cgrp1 != cgrp2)
465 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200466}
467
468static inline int perf_cgroup_connect(int fd, struct perf_event *event,
469 struct perf_event_attr *attr,
470 struct perf_event *group_leader)
471{
472 struct perf_cgroup *cgrp;
473 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400474 struct fd f = fdget(fd);
475 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200476
Al Viro2903ff02012-08-28 12:52:22 -0400477 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200478 return -EBADF;
479
Al Viro2903ff02012-08-28 12:52:22 -0400480 css = cgroup_css_from_dir(f.file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800481 if (IS_ERR(css)) {
482 ret = PTR_ERR(css);
483 goto out;
484 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200485
486 cgrp = container_of(css, struct perf_cgroup, css);
487 event->cgrp = cgrp;
488
Li Zefanf75e18c2011-03-03 14:25:50 +0800489 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700490 if (!perf_tryget_cgroup(event)) {
491 event->cgrp = NULL;
492 ret = -ENOENT;
493 goto out;
494 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800495
Stephane Eraniane5d13672011-02-14 11:20:01 +0200496 /*
497 * all events in a group must monitor
498 * the same cgroup because a task belongs
499 * to only one perf cgroup at a time
500 */
501 if (group_leader && group_leader->cgrp != cgrp) {
502 perf_detach_cgroup(event);
503 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200504 }
Li Zefan3db272c2011-03-03 14:25:37 +0800505out:
Al Viro2903ff02012-08-28 12:52:22 -0400506 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200507 return ret;
508}
509
510static inline void
511perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
512{
513 struct perf_cgroup_info *t;
514 t = per_cpu_ptr(event->cgrp->info, event->cpu);
515 event->shadow_ctx_time = now - t->timestamp;
516}
517
518static inline void
519perf_cgroup_defer_enabled(struct perf_event *event)
520{
521 /*
522 * when the current task's perf cgroup does not match
523 * the event's, we need to remember to call the
524 * perf_mark_enable() function the first time a task with
525 * a matching perf cgroup is scheduled in.
526 */
527 if (is_cgroup_event(event) && !perf_cgroup_match(event))
528 event->cgrp_defer_enabled = 1;
529}
530
531static inline void
532perf_cgroup_mark_enabled(struct perf_event *event,
533 struct perf_event_context *ctx)
534{
535 struct perf_event *sub;
536 u64 tstamp = perf_event_time(event);
537
538 if (!event->cgrp_defer_enabled)
539 return;
540
541 event->cgrp_defer_enabled = 0;
542
543 event->tstamp_enabled = tstamp - event->total_time_enabled;
544 list_for_each_entry(sub, &event->sibling_list, group_entry) {
545 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
546 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
547 sub->cgrp_defer_enabled = 0;
548 }
549 }
550}
551#else /* !CONFIG_CGROUP_PERF */
552
553static inline bool
554perf_cgroup_match(struct perf_event *event)
555{
556 return true;
557}
558
559static inline void perf_detach_cgroup(struct perf_event *event)
560{}
561
562static inline int is_cgroup_event(struct perf_event *event)
563{
564 return 0;
565}
566
567static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
568{
569 return 0;
570}
571
572static inline void update_cgrp_time_from_event(struct perf_event *event)
573{
574}
575
576static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
577{
578}
579
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200580static inline void perf_cgroup_sched_out(struct task_struct *task,
581 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582{
583}
584
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200585static inline void perf_cgroup_sched_in(struct task_struct *prev,
586 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200587{
588}
589
590static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
591 struct perf_event_attr *attr,
592 struct perf_event *group_leader)
593{
594 return -EINVAL;
595}
596
597static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200598perf_cgroup_set_timestamp(struct task_struct *task,
599 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200600{
601}
602
603void
604perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
605{
606}
607
608static inline void
609perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
610{
611}
612
613static inline u64 perf_cgroup_event_time(struct perf_event *event)
614{
615 return 0;
616}
617
618static inline void
619perf_cgroup_defer_enabled(struct perf_event *event)
620{
621}
622
623static inline void
624perf_cgroup_mark_enabled(struct perf_event *event,
625 struct perf_event_context *ctx)
626{
627}
628#endif
629
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200630void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200632 int *count = this_cpu_ptr(pmu->pmu_disable_count);
633 if (!(*count)++)
634 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200635}
636
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200637void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200638{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200639 int *count = this_cpu_ptr(pmu->pmu_disable_count);
640 if (!--(*count))
641 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200642}
643
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200644static DEFINE_PER_CPU(struct list_head, rotation_list);
645
646/*
647 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
648 * because they're strictly cpu affine and rotate_start is called with IRQs
649 * disabled, while rotate_context is called from IRQ context.
650 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200651static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200652{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200653 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200654 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200655
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200656 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200657
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200658 if (list_empty(&cpuctx->rotation_list))
659 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660}
661
662static void get_ctx(struct perf_event_context *ctx)
663{
664 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
665}
666
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200667static void put_ctx(struct perf_event_context *ctx)
668{
669 if (atomic_dec_and_test(&ctx->refcount)) {
670 if (ctx->parent_ctx)
671 put_ctx(ctx->parent_ctx);
672 if (ctx->task)
673 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800674 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200675 }
676}
677
678static void unclone_ctx(struct perf_event_context *ctx)
679{
680 if (ctx->parent_ctx) {
681 put_ctx(ctx->parent_ctx);
682 ctx->parent_ctx = NULL;
683 }
684}
685
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200686static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
687{
688 /*
689 * only top level events have the pid namespace they were created in
690 */
691 if (event->parent)
692 event = event->parent;
693
694 return task_tgid_nr_ns(p, event->ns);
695}
696
697static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
698{
699 /*
700 * only top level events have the pid namespace they were created in
701 */
702 if (event->parent)
703 event = event->parent;
704
705 return task_pid_nr_ns(p, event->ns);
706}
707
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200708/*
709 * If we inherit events we want to return the parent event id
710 * to userspace.
711 */
712static u64 primary_event_id(struct perf_event *event)
713{
714 u64 id = event->id;
715
716 if (event->parent)
717 id = event->parent->id;
718
719 return id;
720}
721
722/*
723 * Get the perf_event_context for a task and lock it.
724 * This has to cope with with the fact that until it is locked,
725 * the context could get moved to another task.
726 */
727static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200728perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200729{
730 struct perf_event_context *ctx;
731
732 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200733retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200734 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200735 if (ctx) {
736 /*
737 * If this context is a clone of another, it might
738 * get swapped for another underneath us by
739 * perf_event_task_sched_out, though the
740 * rcu_read_lock() protects us from any context
741 * getting freed. Lock the context and check if it
742 * got swapped before we could get the lock, and retry
743 * if so. If we locked the right context, then it
744 * can't get swapped on us any more.
745 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100746 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200747 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100748 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200749 goto retry;
750 }
751
752 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100753 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200754 ctx = NULL;
755 }
756 }
757 rcu_read_unlock();
758 return ctx;
759}
760
761/*
762 * Get the context for a task and increment its pin_count so it
763 * can't get swapped to another task. This also increments its
764 * reference count so that the context can't get freed.
765 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200766static struct perf_event_context *
767perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768{
769 struct perf_event_context *ctx;
770 unsigned long flags;
771
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200772 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200773 if (ctx) {
774 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100775 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200776 }
777 return ctx;
778}
779
780static void perf_unpin_context(struct perf_event_context *ctx)
781{
782 unsigned long flags;
783
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100784 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200785 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100786 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200787}
788
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100789/*
790 * Update the record of the current time in a context.
791 */
792static void update_context_time(struct perf_event_context *ctx)
793{
794 u64 now = perf_clock();
795
796 ctx->time += now - ctx->timestamp;
797 ctx->timestamp = now;
798}
799
Stephane Eranian41587552011-01-03 18:20:01 +0200800static u64 perf_event_time(struct perf_event *event)
801{
802 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200803
804 if (is_cgroup_event(event))
805 return perf_cgroup_event_time(event);
806
Stephane Eranian41587552011-01-03 18:20:01 +0200807 return ctx ? ctx->time : 0;
808}
809
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100810/*
811 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400812 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100813 */
814static void update_event_times(struct perf_event *event)
815{
816 struct perf_event_context *ctx = event->ctx;
817 u64 run_end;
818
819 if (event->state < PERF_EVENT_STATE_INACTIVE ||
820 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
821 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200822 /*
823 * in cgroup mode, time_enabled represents
824 * the time the event was enabled AND active
825 * tasks were in the monitored cgroup. This is
826 * independent of the activity of the context as
827 * there may be a mix of cgroup and non-cgroup events.
828 *
829 * That is why we treat cgroup events differently
830 * here.
831 */
832 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +0900833 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200834 else if (ctx->is_active)
835 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100836 else
837 run_end = event->tstamp_stopped;
838
839 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100840
841 if (event->state == PERF_EVENT_STATE_INACTIVE)
842 run_end = event->tstamp_stopped;
843 else
Stephane Eranian41587552011-01-03 18:20:01 +0200844 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100845
846 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200847
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100848}
849
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200850/*
851 * Update total_time_enabled and total_time_running for all events in a group.
852 */
853static void update_group_times(struct perf_event *leader)
854{
855 struct perf_event *event;
856
857 update_event_times(leader);
858 list_for_each_entry(event, &leader->sibling_list, group_entry)
859 update_event_times(event);
860}
861
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100862static struct list_head *
863ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
864{
865 if (event->attr.pinned)
866 return &ctx->pinned_groups;
867 else
868 return &ctx->flexible_groups;
869}
870
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200871/*
872 * Add a event from the lists for its context.
873 * Must be called with ctx->mutex and ctx->lock held.
874 */
875static void
876list_add_event(struct perf_event *event, struct perf_event_context *ctx)
877{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200878 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
879 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200880
881 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200882 * If we're a stand alone event or group leader, we go to the context
883 * list, group events are kept attached to the group so that
884 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200886 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100887 struct list_head *list;
888
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100889 if (is_software_event(event))
890 event->group_flags |= PERF_GROUP_SOFTWARE;
891
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100892 list = ctx_group_list(event, ctx);
893 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894 }
895
Peter Zijlstra08309372011-03-03 11:31:20 +0100896 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200897 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200898
Stephane Eraniand010b332012-02-09 23:21:00 +0100899 if (has_branch_stack(event))
900 ctx->nr_branch_stack++;
901
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200902 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200903 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200904 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200905 ctx->nr_events++;
906 if (event->attr.inherit_stat)
907 ctx->nr_stat++;
908}
909
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200910/*
911 * Called at perf_event creation and when events are attached/detached from a
912 * group.
913 */
914static void perf_event__read_size(struct perf_event *event)
915{
916 int entry = sizeof(u64); /* value */
917 int size = 0;
918 int nr = 1;
919
920 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
921 size += sizeof(u64);
922
923 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
924 size += sizeof(u64);
925
926 if (event->attr.read_format & PERF_FORMAT_ID)
927 entry += sizeof(u64);
928
929 if (event->attr.read_format & PERF_FORMAT_GROUP) {
930 nr += event->group_leader->nr_siblings;
931 size += sizeof(u64);
932 }
933
934 size += entry * nr;
935 event->read_size = size;
936}
937
938static void perf_event__header_size(struct perf_event *event)
939{
940 struct perf_sample_data *data;
941 u64 sample_type = event->attr.sample_type;
942 u16 size = 0;
943
944 perf_event__read_size(event);
945
946 if (sample_type & PERF_SAMPLE_IP)
947 size += sizeof(data->ip);
948
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200949 if (sample_type & PERF_SAMPLE_ADDR)
950 size += sizeof(data->addr);
951
952 if (sample_type & PERF_SAMPLE_PERIOD)
953 size += sizeof(data->period);
954
955 if (sample_type & PERF_SAMPLE_READ)
956 size += event->read_size;
957
958 event->header_size = size;
959}
960
961static void perf_event__id_header_size(struct perf_event *event)
962{
963 struct perf_sample_data *data;
964 u64 sample_type = event->attr.sample_type;
965 u16 size = 0;
966
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200967 if (sample_type & PERF_SAMPLE_TID)
968 size += sizeof(data->tid_entry);
969
970 if (sample_type & PERF_SAMPLE_TIME)
971 size += sizeof(data->time);
972
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200973 if (sample_type & PERF_SAMPLE_ID)
974 size += sizeof(data->id);
975
976 if (sample_type & PERF_SAMPLE_STREAM_ID)
977 size += sizeof(data->stream_id);
978
979 if (sample_type & PERF_SAMPLE_CPU)
980 size += sizeof(data->cpu_entry);
981
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200982 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200983}
984
Peter Zijlstra8a495422010-05-27 15:47:49 +0200985static void perf_group_attach(struct perf_event *event)
986{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200987 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200988
Peter Zijlstra74c33372010-10-15 11:40:29 +0200989 /*
990 * We can have double attach due to group movement in perf_event_open.
991 */
992 if (event->attach_state & PERF_ATTACH_GROUP)
993 return;
994
Peter Zijlstra8a495422010-05-27 15:47:49 +0200995 event->attach_state |= PERF_ATTACH_GROUP;
996
997 if (group_leader == event)
998 return;
999
1000 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1001 !is_software_event(event))
1002 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1003
1004 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1005 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001006
1007 perf_event__header_size(group_leader);
1008
1009 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1010 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001011}
1012
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013/*
1014 * Remove a event from the lists for its context.
1015 * Must be called with ctx->mutex and ctx->lock held.
1016 */
1017static void
1018list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1019{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001020 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001021 /*
1022 * We can have double detach due to exit/hot-unplug + close.
1023 */
1024 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001025 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001026
1027 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1028
Stephane Eranian68cacd22011-03-23 16:03:06 +01001029 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001030 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001031 cpuctx = __get_cpu_context(ctx);
1032 /*
1033 * if there are no more cgroup events
1034 * then cler cgrp to avoid stale pointer
1035 * in update_cgrp_time_from_cpuctx()
1036 */
1037 if (!ctx->nr_cgroups)
1038 cpuctx->cgrp = NULL;
1039 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001040
Stephane Eraniand010b332012-02-09 23:21:00 +01001041 if (has_branch_stack(event))
1042 ctx->nr_branch_stack--;
1043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001044 ctx->nr_events--;
1045 if (event->attr.inherit_stat)
1046 ctx->nr_stat--;
1047
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048 list_del_rcu(&event->event_entry);
1049
Peter Zijlstra8a495422010-05-27 15:47:49 +02001050 if (event->group_leader == event)
1051 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001053 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001054
1055 /*
1056 * If event was in error state, then keep it
1057 * that way, otherwise bogus counts will be
1058 * returned on read(). The only way to get out
1059 * of error state is by explicit re-enabling
1060 * of the event
1061 */
1062 if (event->state > PERF_EVENT_STATE_OFF)
1063 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001064}
1065
Peter Zijlstra8a495422010-05-27 15:47:49 +02001066static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001067{
1068 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001069 struct list_head *list = NULL;
1070
1071 /*
1072 * We can have double detach due to exit/hot-unplug + close.
1073 */
1074 if (!(event->attach_state & PERF_ATTACH_GROUP))
1075 return;
1076
1077 event->attach_state &= ~PERF_ATTACH_GROUP;
1078
1079 /*
1080 * If this is a sibling, remove it from its group.
1081 */
1082 if (event->group_leader != event) {
1083 list_del_init(&event->group_entry);
1084 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001085 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001086 }
1087
1088 if (!list_empty(&event->group_entry))
1089 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001090
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 /*
1092 * If this was a group event with sibling events then
1093 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001094 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095 */
1096 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001097 if (list)
1098 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001100
1101 /* Inherit group flags from the previous leader */
1102 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001104
1105out:
1106 perf_event__header_size(event->group_leader);
1107
1108 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1109 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001110}
1111
Stephane Eranianfa66f072010-08-26 16:40:01 +02001112static inline int
1113event_filter_match(struct perf_event *event)
1114{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001115 return (event->cpu == -1 || event->cpu == smp_processor_id())
1116 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001117}
1118
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001119static void
1120event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121 struct perf_cpu_context *cpuctx,
1122 struct perf_event_context *ctx)
1123{
Stephane Eranian41587552011-01-03 18:20:01 +02001124 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001125 u64 delta;
1126 /*
1127 * An event which could not be activated because of
1128 * filter mismatch still needs to have its timings
1129 * maintained, otherwise bogus information is return
1130 * via read() for time_enabled, time_running:
1131 */
1132 if (event->state == PERF_EVENT_STATE_INACTIVE
1133 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001134 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001135 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001136 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001137 }
1138
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001140 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141
1142 event->state = PERF_EVENT_STATE_INACTIVE;
1143 if (event->pending_disable) {
1144 event->pending_disable = 0;
1145 event->state = PERF_EVENT_STATE_OFF;
1146 }
Stephane Eranian41587552011-01-03 18:20:01 +02001147 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001148 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 event->oncpu = -1;
1150
1151 if (!is_software_event(event))
1152 cpuctx->active_oncpu--;
1153 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001154 if (event->attr.freq && event->attr.sample_freq)
1155 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156 if (event->attr.exclusive || !cpuctx->active_oncpu)
1157 cpuctx->exclusive = 0;
1158}
1159
1160static void
1161group_sched_out(struct perf_event *group_event,
1162 struct perf_cpu_context *cpuctx,
1163 struct perf_event_context *ctx)
1164{
1165 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001166 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001167
1168 event_sched_out(group_event, cpuctx, ctx);
1169
1170 /*
1171 * Schedule out siblings (if any):
1172 */
1173 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1174 event_sched_out(event, cpuctx, ctx);
1175
Stephane Eranianfa66f072010-08-26 16:40:01 +02001176 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177 cpuctx->exclusive = 0;
1178}
1179
1180/*
1181 * Cross CPU call to remove a performance event
1182 *
1183 * We disable the event on the hardware level first. After that we
1184 * remove it from the context list.
1185 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001186static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188 struct perf_event *event = info;
1189 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001190 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001192 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001194 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001195 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1196 ctx->is_active = 0;
1197 cpuctx->task_ctx = NULL;
1198 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001199 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001200
1201 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202}
1203
1204
1205/*
1206 * Remove the event from a task's (or a CPU's) list of events.
1207 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 * CPU events are removed with a smp call. For task events we only
1209 * call when the task is on a CPU.
1210 *
1211 * If event->ctx is a cloned context, callers must make sure that
1212 * every task struct that event->ctx->task could possibly point to
1213 * remains valid. This is OK when called from perf_release since
1214 * that only calls us on the top-level context, which can't be a clone.
1215 * When called from perf_event_exit_task, it's OK because the
1216 * context has been detached from its task.
1217 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001218static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219{
1220 struct perf_event_context *ctx = event->ctx;
1221 struct task_struct *task = ctx->task;
1222
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001223 lockdep_assert_held(&ctx->mutex);
1224
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 if (!task) {
1226 /*
1227 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001228 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001229 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001230 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001231 return;
1232 }
1233
1234retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001235 if (!task_function_call(task, __perf_remove_from_context, event))
1236 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001238 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001239 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001240 * If we failed to find a running task, but find the context active now
1241 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001243 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001244 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001245 goto retry;
1246 }
1247
1248 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001249 * Since the task isn't running, its safe to remove the event, us
1250 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001252 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001253 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001254}
1255
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 * Cross CPU call to disable a performance event
1258 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301259int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260{
1261 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001263 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264
1265 /*
1266 * If this is a per-task event, need to check whether this
1267 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001268 *
1269 * Can trigger due to concurrent perf_event_context_sched_out()
1270 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001271 */
1272 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001273 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001275 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001276
1277 /*
1278 * If the event is on, turn it off.
1279 * If it is in error state, leave it in error state.
1280 */
1281 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1282 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001283 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001284 update_group_times(event);
1285 if (event == event->group_leader)
1286 group_sched_out(event, cpuctx, ctx);
1287 else
1288 event_sched_out(event, cpuctx, ctx);
1289 event->state = PERF_EVENT_STATE_OFF;
1290 }
1291
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001292 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001293
1294 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001295}
1296
1297/*
1298 * Disable a event.
1299 *
1300 * If event->ctx is a cloned context, callers must make sure that
1301 * every task struct that event->ctx->task could possibly point to
1302 * remains valid. This condition is satisifed when called through
1303 * perf_event_for_each_child or perf_event_for_each because they
1304 * hold the top-level event's child_mutex, so any descendant that
1305 * goes to exit will block in sync_child_event.
1306 * When called from perf_pending_event it's OK because event->ctx
1307 * is the current context on this CPU and preemption is disabled,
1308 * hence we can't get into perf_event_task_sched_out for this context.
1309 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001310void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001311{
1312 struct perf_event_context *ctx = event->ctx;
1313 struct task_struct *task = ctx->task;
1314
1315 if (!task) {
1316 /*
1317 * Disable the event on the cpu that it's on
1318 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001319 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320 return;
1321 }
1322
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001323retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001324 if (!task_function_call(task, __perf_event_disable, event))
1325 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001327 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 /*
1329 * If the event is still active, we need to retry the cross-call.
1330 */
1331 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001332 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001333 /*
1334 * Reload the task pointer, it might have been changed by
1335 * a concurrent perf_event_context_sched_out().
1336 */
1337 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338 goto retry;
1339 }
1340
1341 /*
1342 * Since we have the lock this context can't be scheduled
1343 * in, so we can change the state safely.
1344 */
1345 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1346 update_group_times(event);
1347 event->state = PERF_EVENT_STATE_OFF;
1348 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001349 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001351EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352
Stephane Eraniane5d13672011-02-14 11:20:01 +02001353static void perf_set_shadow_time(struct perf_event *event,
1354 struct perf_event_context *ctx,
1355 u64 tstamp)
1356{
1357 /*
1358 * use the correct time source for the time snapshot
1359 *
1360 * We could get by without this by leveraging the
1361 * fact that to get to this function, the caller
1362 * has most likely already called update_context_time()
1363 * and update_cgrp_time_xx() and thus both timestamp
1364 * are identical (or very close). Given that tstamp is,
1365 * already adjusted for cgroup, we could say that:
1366 * tstamp - ctx->timestamp
1367 * is equivalent to
1368 * tstamp - cgrp->timestamp.
1369 *
1370 * Then, in perf_output_read(), the calculation would
1371 * work with no changes because:
1372 * - event is guaranteed scheduled in
1373 * - no scheduled out in between
1374 * - thus the timestamp would be the same
1375 *
1376 * But this is a bit hairy.
1377 *
1378 * So instead, we have an explicit cgroup call to remain
1379 * within the time time source all along. We believe it
1380 * is cleaner and simpler to understand.
1381 */
1382 if (is_cgroup_event(event))
1383 perf_cgroup_set_shadow_time(event, tstamp);
1384 else
1385 event->shadow_ctx_time = tstamp - ctx->timestamp;
1386}
1387
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001388#define MAX_INTERRUPTS (~0ULL)
1389
1390static void perf_log_throttle(struct perf_event *event, int enable);
1391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001392static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001393event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001395 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396{
Stephane Eranian41587552011-01-03 18:20:01 +02001397 u64 tstamp = perf_event_time(event);
1398
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001399 if (event->state <= PERF_EVENT_STATE_OFF)
1400 return 0;
1401
1402 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001403 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001404
1405 /*
1406 * Unthrottle events, since we scheduled we might have missed several
1407 * ticks already, also for a heavily scheduling task there is little
1408 * guarantee it'll get a tick in a timely manner.
1409 */
1410 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1411 perf_log_throttle(event, 1);
1412 event->hw.interrupts = 0;
1413 }
1414
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 /*
1416 * The new state must be visible before we turn it on in the hardware:
1417 */
1418 smp_wmb();
1419
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001420 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421 event->state = PERF_EVENT_STATE_INACTIVE;
1422 event->oncpu = -1;
1423 return -EAGAIN;
1424 }
1425
Stephane Eranian41587552011-01-03 18:20:01 +02001426 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001427
Stephane Eraniane5d13672011-02-14 11:20:01 +02001428 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001429
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430 if (!is_software_event(event))
1431 cpuctx->active_oncpu++;
1432 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001433 if (event->attr.freq && event->attr.sample_freq)
1434 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435
1436 if (event->attr.exclusive)
1437 cpuctx->exclusive = 1;
1438
1439 return 0;
1440}
1441
1442static int
1443group_sched_in(struct perf_event *group_event,
1444 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001445 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001446{
Lin Ming6bde9b62010-04-23 13:56:00 +08001447 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001448 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001449 u64 now = ctx->time;
1450 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451
1452 if (group_event->state == PERF_EVENT_STATE_OFF)
1453 return 0;
1454
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001455 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001456
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001457 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001458 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001460 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461
1462 /*
1463 * Schedule in siblings as one group (if any):
1464 */
1465 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001466 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467 partial_group = event;
1468 goto group_error;
1469 }
1470 }
1471
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001472 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001473 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475group_error:
1476 /*
1477 * Groups can be scheduled in as one unit only, so undo any
1478 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001479 * The events up to the failed event are scheduled out normally,
1480 * tstamp_stopped will be updated.
1481 *
1482 * The failed events and the remaining siblings need to have
1483 * their timings updated as if they had gone thru event_sched_in()
1484 * and event_sched_out(). This is required to get consistent timings
1485 * across the group. This also takes care of the case where the group
1486 * could never be scheduled by ensuring tstamp_stopped is set to mark
1487 * the time the event was actually stopped, such that time delta
1488 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001489 */
1490 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1491 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001492 simulate = true;
1493
1494 if (simulate) {
1495 event->tstamp_running += now - event->tstamp_stopped;
1496 event->tstamp_stopped = now;
1497 } else {
1498 event_sched_out(event, cpuctx, ctx);
1499 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001501 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001503 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 return -EAGAIN;
1506}
1507
1508/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 * Work out whether we can put this event group on the CPU now.
1510 */
1511static int group_can_go_on(struct perf_event *event,
1512 struct perf_cpu_context *cpuctx,
1513 int can_add_hw)
1514{
1515 /*
1516 * Groups consisting entirely of software events can always go on.
1517 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001518 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 return 1;
1520 /*
1521 * If an exclusive group is already on, no other hardware
1522 * events can go on.
1523 */
1524 if (cpuctx->exclusive)
1525 return 0;
1526 /*
1527 * If this group is exclusive and there are already
1528 * events on the CPU, it can't go on.
1529 */
1530 if (event->attr.exclusive && cpuctx->active_oncpu)
1531 return 0;
1532 /*
1533 * Otherwise, try to add it if all previous groups were able
1534 * to go on.
1535 */
1536 return can_add_hw;
1537}
1538
1539static void add_event_to_ctx(struct perf_event *event,
1540 struct perf_event_context *ctx)
1541{
Stephane Eranian41587552011-01-03 18:20:01 +02001542 u64 tstamp = perf_event_time(event);
1543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001545 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001546 event->tstamp_enabled = tstamp;
1547 event->tstamp_running = tstamp;
1548 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549}
1550
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001551static void task_ctx_sched_out(struct perf_event_context *ctx);
1552static void
1553ctx_sched_in(struct perf_event_context *ctx,
1554 struct perf_cpu_context *cpuctx,
1555 enum event_type_t event_type,
1556 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001557
Peter Zijlstradce58552011-04-09 21:17:46 +02001558static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1559 struct perf_event_context *ctx,
1560 struct task_struct *task)
1561{
1562 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1563 if (ctx)
1564 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1565 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1566 if (ctx)
1567 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1568}
1569
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570/*
1571 * Cross CPU call to install and enable a performance event
1572 *
1573 * Must be called with ctx->mutex held
1574 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001575static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 struct perf_event *event = info;
1578 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001579 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001580 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1581 struct task_struct *task = current;
1582
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001583 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001584 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585
1586 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001587 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001589 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001590 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001591
1592 /*
1593 * If the context we're installing events in is not the
1594 * active task_ctx, flip them.
1595 */
1596 if (ctx->task && task_ctx != ctx) {
1597 if (task_ctx)
1598 raw_spin_unlock(&task_ctx->lock);
1599 raw_spin_lock(&ctx->lock);
1600 task_ctx = ctx;
1601 }
1602
1603 if (task_ctx) {
1604 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001605 task = task_ctx->task;
1606 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001607
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001608 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001611 /*
1612 * update cgrp time only if current cgrp
1613 * matches event->cgrp. Must be done before
1614 * calling add_event_to_ctx()
1615 */
1616 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 add_event_to_ctx(event, ctx);
1619
1620 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001621 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001622 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001623 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001624
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001625 perf_pmu_enable(cpuctx->ctx.pmu);
1626 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001627
1628 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629}
1630
1631/*
1632 * Attach a performance event to a context
1633 *
1634 * First we add the event to the list with the hardware enable bit
1635 * in event->hw_config cleared.
1636 *
1637 * If the event is attached to a task which is on a CPU we use a smp
1638 * call to enable it in the task context. The task might have been
1639 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640 */
1641static void
1642perf_install_in_context(struct perf_event_context *ctx,
1643 struct perf_event *event,
1644 int cpu)
1645{
1646 struct task_struct *task = ctx->task;
1647
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001648 lockdep_assert_held(&ctx->mutex);
1649
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001650 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001651 if (event->cpu != -1)
1652 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001653
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 if (!task) {
1655 /*
1656 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001657 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001659 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 return;
1661 }
1662
1663retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001664 if (!task_function_call(task, __perf_install_in_context, event))
1665 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001666
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001667 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001669 * If we failed to find a running task, but find the context active now
1670 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001672 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001673 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674 goto retry;
1675 }
1676
1677 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001678 * Since the task isn't running, its safe to add the event, us holding
1679 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001681 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001682 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683}
1684
1685/*
1686 * Put a event into inactive state and update time fields.
1687 * Enabling the leader of a group effectively enables all
1688 * the group members that aren't explicitly disabled, so we
1689 * have to update their ->tstamp_enabled also.
1690 * Note: this works for group members as well as group leaders
1691 * since the non-leader members' sibling_lists will be empty.
1692 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001693static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694{
1695 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001696 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001697
1698 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001699 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001700 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001701 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1702 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001703 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704}
1705
1706/*
1707 * Cross CPU call to enable a performance event
1708 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001709static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001710{
1711 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 struct perf_event_context *ctx = event->ctx;
1713 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001714 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 int err;
1716
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001717 if (WARN_ON_ONCE(!ctx->is_active))
1718 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001720 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721 update_context_time(ctx);
1722
1723 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1724 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001725
1726 /*
1727 * set current task's cgroup time reference point
1728 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001729 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001730
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001731 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001732
Stephane Eraniane5d13672011-02-14 11:20:01 +02001733 if (!event_filter_match(event)) {
1734 if (is_cgroup_event(event))
1735 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001736 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001737 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 /*
1740 * If the event is in a group and isn't the group leader,
1741 * then don't put it on unless the group is on.
1742 */
1743 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1744 goto unlock;
1745
1746 if (!group_can_go_on(event, cpuctx, 1)) {
1747 err = -EEXIST;
1748 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001749 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001750 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001752 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 }
1754
1755 if (err) {
1756 /*
1757 * If this event can't go on and it's part of a
1758 * group, then the whole group has to come off.
1759 */
1760 if (leader != event)
1761 group_sched_out(leader, cpuctx, ctx);
1762 if (leader->attr.pinned) {
1763 update_group_times(leader);
1764 leader->state = PERF_EVENT_STATE_ERROR;
1765 }
1766 }
1767
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001768unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001769 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001770
1771 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772}
1773
1774/*
1775 * Enable a event.
1776 *
1777 * If event->ctx is a cloned context, callers must make sure that
1778 * every task struct that event->ctx->task could possibly point to
1779 * remains valid. This condition is satisfied when called through
1780 * perf_event_for_each_child or perf_event_for_each as described
1781 * for perf_event_disable.
1782 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001783void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784{
1785 struct perf_event_context *ctx = event->ctx;
1786 struct task_struct *task = ctx->task;
1787
1788 if (!task) {
1789 /*
1790 * Enable the event on the cpu that it's on
1791 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001792 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793 return;
1794 }
1795
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001796 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1798 goto out;
1799
1800 /*
1801 * If the event is in error state, clear that first.
1802 * That way, if we see the event in error state below, we
1803 * know that it has gone back into error state, as distinct
1804 * from the task having been scheduled away before the
1805 * cross-call arrived.
1806 */
1807 if (event->state == PERF_EVENT_STATE_ERROR)
1808 event->state = PERF_EVENT_STATE_OFF;
1809
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001810retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001811 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001812 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001813 goto out;
1814 }
1815
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001816 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001817
1818 if (!task_function_call(task, __perf_event_enable, event))
1819 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001821 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822
1823 /*
1824 * If the context is active and the event is still off,
1825 * we need to retry the cross-call.
1826 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001827 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1828 /*
1829 * task could have been flipped by a concurrent
1830 * perf_event_context_sched_out()
1831 */
1832 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001834 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001836out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001837 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001839EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001840
Avi Kivity26ca5c12011-06-29 18:42:37 +03001841int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842{
1843 /*
1844 * not supported on inherited events
1845 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001846 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847 return -EINVAL;
1848
1849 atomic_add(refresh, &event->event_limit);
1850 perf_event_enable(event);
1851
1852 return 0;
1853}
Avi Kivity26ca5c12011-06-29 18:42:37 +03001854EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001856static void ctx_sched_out(struct perf_event_context *ctx,
1857 struct perf_cpu_context *cpuctx,
1858 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859{
1860 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001861 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001862
Peter Zijlstradb24d332011-04-09 21:17:45 +02001863 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001864 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001865 return;
1866
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001868 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001869 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001870 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001871
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001872 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001873 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001874 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1875 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001876 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001877
Peter Zijlstradb24d332011-04-09 21:17:45 +02001878 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001879 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001880 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001881 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001882 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883}
1884
1885/*
1886 * Test whether two contexts are equivalent, i.e. whether they
1887 * have both been cloned from the same version of the same context
1888 * and they both have the same number of enabled events.
1889 * If the number of enabled events is the same, then the set
1890 * of enabled events should be the same, because these are both
1891 * inherited contexts, therefore we can't access individual events
1892 * in them directly with an fd; we can only enable/disable all
1893 * events via prctl, or enable/disable all events in a family
1894 * via ioctl, which will have the same effect on both contexts.
1895 */
1896static int context_equiv(struct perf_event_context *ctx1,
1897 struct perf_event_context *ctx2)
1898{
1899 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1900 && ctx1->parent_gen == ctx2->parent_gen
1901 && !ctx1->pin_count && !ctx2->pin_count;
1902}
1903
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904static void __perf_event_sync_stat(struct perf_event *event,
1905 struct perf_event *next_event)
1906{
1907 u64 value;
1908
1909 if (!event->attr.inherit_stat)
1910 return;
1911
1912 /*
1913 * Update the event value, we cannot use perf_event_read()
1914 * because we're in the middle of a context switch and have IRQs
1915 * disabled, which upsets smp_call_function_single(), however
1916 * we know the event must be on the current CPU, therefore we
1917 * don't need to use it.
1918 */
1919 switch (event->state) {
1920 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001921 event->pmu->read(event);
1922 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923
1924 case PERF_EVENT_STATE_INACTIVE:
1925 update_event_times(event);
1926 break;
1927
1928 default:
1929 break;
1930 }
1931
1932 /*
1933 * In order to keep per-task stats reliable we need to flip the event
1934 * values when we flip the contexts.
1935 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001936 value = local64_read(&next_event->count);
1937 value = local64_xchg(&event->count, value);
1938 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939
1940 swap(event->total_time_enabled, next_event->total_time_enabled);
1941 swap(event->total_time_running, next_event->total_time_running);
1942
1943 /*
1944 * Since we swizzled the values, update the user visible data too.
1945 */
1946 perf_event_update_userpage(event);
1947 perf_event_update_userpage(next_event);
1948}
1949
1950#define list_next_entry(pos, member) \
1951 list_entry(pos->member.next, typeof(*pos), member)
1952
1953static void perf_event_sync_stat(struct perf_event_context *ctx,
1954 struct perf_event_context *next_ctx)
1955{
1956 struct perf_event *event, *next_event;
1957
1958 if (!ctx->nr_stat)
1959 return;
1960
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001961 update_context_time(ctx);
1962
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963 event = list_first_entry(&ctx->event_list,
1964 struct perf_event, event_entry);
1965
1966 next_event = list_first_entry(&next_ctx->event_list,
1967 struct perf_event, event_entry);
1968
1969 while (&event->event_entry != &ctx->event_list &&
1970 &next_event->event_entry != &next_ctx->event_list) {
1971
1972 __perf_event_sync_stat(event, next_event);
1973
1974 event = list_next_entry(event, event_entry);
1975 next_event = list_next_entry(next_event, event_entry);
1976 }
1977}
1978
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001979static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1980 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001981{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001982 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001983 struct perf_event_context *next_ctx;
1984 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001985 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986 int do_switch = 1;
1987
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001988 if (likely(!ctx))
1989 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001991 cpuctx = __get_cpu_context(ctx);
1992 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993 return;
1994
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 rcu_read_lock();
1996 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001997 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998 if (parent && next_ctx &&
1999 rcu_dereference(next_ctx->parent_ctx) == parent) {
2000 /*
2001 * Looks like the two contexts are clones, so we might be
2002 * able to optimize the context switch. We lock both
2003 * contexts and check that they are clones under the
2004 * lock (including re-checking that neither has been
2005 * uncloned in the meantime). It doesn't matter which
2006 * order we take the locks because no other cpu could
2007 * be trying to lock both of these tasks.
2008 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002009 raw_spin_lock(&ctx->lock);
2010 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002011 if (context_equiv(ctx, next_ctx)) {
2012 /*
2013 * XXX do we need a memory barrier of sorts
2014 * wrt to rcu_dereference() of perf_event_ctxp
2015 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002016 task->perf_event_ctxp[ctxn] = next_ctx;
2017 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 ctx->task = next;
2019 next_ctx->task = task;
2020 do_switch = 0;
2021
2022 perf_event_sync_stat(ctx, next_ctx);
2023 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002024 raw_spin_unlock(&next_ctx->lock);
2025 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026 }
2027 rcu_read_unlock();
2028
2029 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002030 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002031 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002033 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 }
2035}
2036
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002037#define for_each_task_context_nr(ctxn) \
2038 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2039
2040/*
2041 * Called from scheduler to remove the events of the current task,
2042 * with interrupts disabled.
2043 *
2044 * We stop each event and update the event value in event->count.
2045 *
2046 * This does not protect us against NMI, but disable()
2047 * sets the disabled bit in the control field of event _before_
2048 * accessing the event control register. If a NMI hits, then it will
2049 * not restart the event.
2050 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002051void __perf_event_task_sched_out(struct task_struct *task,
2052 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002053{
2054 int ctxn;
2055
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002056 for_each_task_context_nr(ctxn)
2057 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002058
2059 /*
2060 * if cgroup events exist on this CPU, then we need
2061 * to check if we have to switch out PMU state.
2062 * cgroup event are system-wide mode only
2063 */
2064 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002065 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002066}
2067
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002068static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002070 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002071
2072 if (!cpuctx->task_ctx)
2073 return;
2074
2075 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2076 return;
2077
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002078 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002079 cpuctx->task_ctx = NULL;
2080}
2081
2082/*
2083 * Called with IRQs disabled
2084 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002085static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2086 enum event_type_t event_type)
2087{
2088 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089}
2090
2091static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002092ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002093 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094{
2095 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002097 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2098 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002100 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101 continue;
2102
Stephane Eraniane5d13672011-02-14 11:20:01 +02002103 /* may need to reset tstamp_enabled */
2104 if (is_cgroup_event(event))
2105 perf_cgroup_mark_enabled(event, ctx);
2106
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002107 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002108 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109
2110 /*
2111 * If this pinned group hasn't been scheduled,
2112 * put it in error state.
2113 */
2114 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2115 update_group_times(event);
2116 event->state = PERF_EVENT_STATE_ERROR;
2117 }
2118 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002119}
2120
2121static void
2122ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002123 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002124{
2125 struct perf_event *event;
2126 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002128 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2129 /* Ignore events in OFF or ERROR state */
2130 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132 /*
2133 * Listen to the 'cpu' scheduling filter constraint
2134 * of events:
2135 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002136 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137 continue;
2138
Stephane Eraniane5d13672011-02-14 11:20:01 +02002139 /* may need to reset tstamp_enabled */
2140 if (is_cgroup_event(event))
2141 perf_cgroup_mark_enabled(event, ctx);
2142
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002143 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002144 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002146 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002148}
2149
2150static void
2151ctx_sched_in(struct perf_event_context *ctx,
2152 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002153 enum event_type_t event_type,
2154 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002155{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002156 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002157 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002158
Peter Zijlstradb24d332011-04-09 21:17:45 +02002159 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002160 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002161 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002162
Stephane Eraniane5d13672011-02-14 11:20:01 +02002163 now = perf_clock();
2164 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002165 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002166 /*
2167 * First go through the list and put on any pinned groups
2168 * in order to give them the best chance of going on.
2169 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002170 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002171 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002172
2173 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002174 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002175 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176}
2177
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002178static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002179 enum event_type_t event_type,
2180 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002181{
2182 struct perf_event_context *ctx = &cpuctx->ctx;
2183
Stephane Eraniane5d13672011-02-14 11:20:01 +02002184 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002185}
2186
Stephane Eraniane5d13672011-02-14 11:20:01 +02002187static void perf_event_context_sched_in(struct perf_event_context *ctx,
2188 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002190 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002192 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002193 if (cpuctx->task_ctx == ctx)
2194 return;
2195
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002196 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002197 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002198 /*
2199 * We want to keep the following priority order:
2200 * cpu pinned (that don't need to move), task pinned,
2201 * cpu flexible, task flexible.
2202 */
2203 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2204
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002205 if (ctx->nr_events)
2206 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002207
Gleb Natapov86b47c22011-11-22 16:08:21 +02002208 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2209
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002210 perf_pmu_enable(ctx->pmu);
2211 perf_ctx_unlock(cpuctx, ctx);
2212
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002213 /*
2214 * Since these rotations are per-cpu, we need to ensure the
2215 * cpu-context we got scheduled on is actually rotating.
2216 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002217 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218}
2219
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002220/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002221 * When sampling the branck stack in system-wide, it may be necessary
2222 * to flush the stack on context switch. This happens when the branch
2223 * stack does not tag its entries with the pid of the current task.
2224 * Otherwise it becomes impossible to associate a branch entry with a
2225 * task. This ambiguity is more likely to appear when the branch stack
2226 * supports priv level filtering and the user sets it to monitor only
2227 * at the user level (which could be a useful measurement in system-wide
2228 * mode). In that case, the risk is high of having a branch stack with
2229 * branch from multiple tasks. Flushing may mean dropping the existing
2230 * entries or stashing them somewhere in the PMU specific code layer.
2231 *
2232 * This function provides the context switch callback to the lower code
2233 * layer. It is invoked ONLY when there is at least one system-wide context
2234 * with at least one active event using taken branch sampling.
2235 */
2236static void perf_branch_stack_sched_in(struct task_struct *prev,
2237 struct task_struct *task)
2238{
2239 struct perf_cpu_context *cpuctx;
2240 struct pmu *pmu;
2241 unsigned long flags;
2242
2243 /* no need to flush branch stack if not changing task */
2244 if (prev == task)
2245 return;
2246
2247 local_irq_save(flags);
2248
2249 rcu_read_lock();
2250
2251 list_for_each_entry_rcu(pmu, &pmus, entry) {
2252 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2253
2254 /*
2255 * check if the context has at least one
2256 * event using PERF_SAMPLE_BRANCH_STACK
2257 */
2258 if (cpuctx->ctx.nr_branch_stack > 0
2259 && pmu->flush_branch_stack) {
2260
2261 pmu = cpuctx->ctx.pmu;
2262
2263 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2264
2265 perf_pmu_disable(pmu);
2266
2267 pmu->flush_branch_stack();
2268
2269 perf_pmu_enable(pmu);
2270
2271 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2272 }
2273 }
2274
2275 rcu_read_unlock();
2276
2277 local_irq_restore(flags);
2278}
2279
2280/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002281 * Called from scheduler to add the events of the current task
2282 * with interrupts disabled.
2283 *
2284 * We restore the event value and then enable it.
2285 *
2286 * This does not protect us against NMI, but enable()
2287 * sets the enabled bit in the control field of event _before_
2288 * accessing the event control register. If a NMI hits, then it will
2289 * keep the event running.
2290 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002291void __perf_event_task_sched_in(struct task_struct *prev,
2292 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002293{
2294 struct perf_event_context *ctx;
2295 int ctxn;
2296
2297 for_each_task_context_nr(ctxn) {
2298 ctx = task->perf_event_ctxp[ctxn];
2299 if (likely(!ctx))
2300 continue;
2301
Stephane Eraniane5d13672011-02-14 11:20:01 +02002302 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002303 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002304 /*
2305 * if cgroup events exist on this CPU, then we need
2306 * to check if we have to switch in PMU state.
2307 * cgroup event are system-wide mode only
2308 */
2309 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002310 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002311
2312 /* check for system-wide branch_stack events */
2313 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2314 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315}
2316
Peter Zijlstraabd50712010-01-26 18:50:16 +01002317static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2318{
2319 u64 frequency = event->attr.sample_freq;
2320 u64 sec = NSEC_PER_SEC;
2321 u64 divisor, dividend;
2322
2323 int count_fls, nsec_fls, frequency_fls, sec_fls;
2324
2325 count_fls = fls64(count);
2326 nsec_fls = fls64(nsec);
2327 frequency_fls = fls64(frequency);
2328 sec_fls = 30;
2329
2330 /*
2331 * We got @count in @nsec, with a target of sample_freq HZ
2332 * the target period becomes:
2333 *
2334 * @count * 10^9
2335 * period = -------------------
2336 * @nsec * sample_freq
2337 *
2338 */
2339
2340 /*
2341 * Reduce accuracy by one bit such that @a and @b converge
2342 * to a similar magnitude.
2343 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002344#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002345do { \
2346 if (a##_fls > b##_fls) { \
2347 a >>= 1; \
2348 a##_fls--; \
2349 } else { \
2350 b >>= 1; \
2351 b##_fls--; \
2352 } \
2353} while (0)
2354
2355 /*
2356 * Reduce accuracy until either term fits in a u64, then proceed with
2357 * the other, so that finally we can do a u64/u64 division.
2358 */
2359 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2360 REDUCE_FLS(nsec, frequency);
2361 REDUCE_FLS(sec, count);
2362 }
2363
2364 if (count_fls + sec_fls > 64) {
2365 divisor = nsec * frequency;
2366
2367 while (count_fls + sec_fls > 64) {
2368 REDUCE_FLS(count, sec);
2369 divisor >>= 1;
2370 }
2371
2372 dividend = count * sec;
2373 } else {
2374 dividend = count * sec;
2375
2376 while (nsec_fls + frequency_fls > 64) {
2377 REDUCE_FLS(nsec, frequency);
2378 dividend >>= 1;
2379 }
2380
2381 divisor = nsec * frequency;
2382 }
2383
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002384 if (!divisor)
2385 return dividend;
2386
Peter Zijlstraabd50712010-01-26 18:50:16 +01002387 return div64_u64(dividend, divisor);
2388}
2389
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002390static DEFINE_PER_CPU(int, perf_throttled_count);
2391static DEFINE_PER_CPU(u64, perf_throttled_seq);
2392
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002393static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394{
2395 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002396 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002397 s64 delta;
2398
Peter Zijlstraabd50712010-01-26 18:50:16 +01002399 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400
2401 delta = (s64)(period - hwc->sample_period);
2402 delta = (delta + 7) / 8; /* low pass filter */
2403
2404 sample_period = hwc->sample_period + delta;
2405
2406 if (!sample_period)
2407 sample_period = 1;
2408
2409 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002410
Peter Zijlstrae7850592010-05-21 14:43:08 +02002411 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002412 if (disable)
2413 event->pmu->stop(event, PERF_EF_UPDATE);
2414
Peter Zijlstrae7850592010-05-21 14:43:08 +02002415 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002416
2417 if (disable)
2418 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002419 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420}
2421
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002422/*
2423 * combine freq adjustment with unthrottling to avoid two passes over the
2424 * events. At the same time, make sure, having freq events does not change
2425 * the rate of unthrottling as that would introduce bias.
2426 */
2427static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2428 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429{
2430 struct perf_event *event;
2431 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002432 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002433 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002434
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002435 /*
2436 * only need to iterate over all events iff:
2437 * - context have events in frequency mode (needs freq adjust)
2438 * - there are events to unthrottle on this cpu
2439 */
2440 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002441 return;
2442
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002443 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002444 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002445
Paul Mackerras03541f82009-10-14 16:58:03 +11002446 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002447 if (event->state != PERF_EVENT_STATE_ACTIVE)
2448 continue;
2449
Stephane Eranian5632ab12011-01-03 18:20:01 +02002450 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002451 continue;
2452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453 hwc = &event->hw;
2454
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002455 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2456 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002457 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002458 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459 }
2460
2461 if (!event->attr.freq || !event->attr.sample_freq)
2462 continue;
2463
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002464 /*
2465 * stop the event and update event->count
2466 */
2467 event->pmu->stop(event, PERF_EF_UPDATE);
2468
Peter Zijlstrae7850592010-05-21 14:43:08 +02002469 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002470 delta = now - hwc->freq_count_stamp;
2471 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002473 /*
2474 * restart the event
2475 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002476 * we have stopped the event so tell that
2477 * to perf_adjust_period() to avoid stopping it
2478 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002479 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002480 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002481 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002482
2483 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002484 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002485
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002486 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002487 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488}
2489
2490/*
2491 * Round-robin a context's events:
2492 */
2493static void rotate_ctx(struct perf_event_context *ctx)
2494{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002495 /*
2496 * Rotate the first entry last of non-pinned groups. Rotation might be
2497 * disabled by the inheritance code.
2498 */
2499 if (!ctx->rotate_disable)
2500 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002501}
2502
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002503/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002504 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2505 * because they're strictly cpu affine and rotate_start is called with IRQs
2506 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002507 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002508static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002510 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002511 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002513 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002514 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002515 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2516 rotate = 1;
2517 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002519 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002520 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002521 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002522 if (ctx->nr_events != ctx->nr_active)
2523 rotate = 1;
2524 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002525
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002526 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002527 goto done;
2528
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002529 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002530 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002531
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002532 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2533 if (ctx)
2534 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002535
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002536 rotate_ctx(&cpuctx->ctx);
2537 if (ctx)
2538 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002540 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002541
2542 perf_pmu_enable(cpuctx->ctx.pmu);
2543 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002544done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002545 if (remove)
2546 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002547}
2548
2549void perf_event_task_tick(void)
2550{
2551 struct list_head *head = &__get_cpu_var(rotation_list);
2552 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002553 struct perf_event_context *ctx;
2554 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002555
2556 WARN_ON(!irqs_disabled());
2557
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002558 __this_cpu_inc(perf_throttled_seq);
2559 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2560
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002561 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002562 ctx = &cpuctx->ctx;
2563 perf_adjust_freq_unthr_context(ctx, throttled);
2564
2565 ctx = cpuctx->task_ctx;
2566 if (ctx)
2567 perf_adjust_freq_unthr_context(ctx, throttled);
2568
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002569 if (cpuctx->jiffies_interval == 1 ||
2570 !(jiffies % cpuctx->jiffies_interval))
2571 perf_rotate_context(cpuctx);
2572 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002573}
2574
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002575static int event_enable_on_exec(struct perf_event *event,
2576 struct perf_event_context *ctx)
2577{
2578 if (!event->attr.enable_on_exec)
2579 return 0;
2580
2581 event->attr.enable_on_exec = 0;
2582 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2583 return 0;
2584
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002585 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002586
2587 return 1;
2588}
2589
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002590/*
2591 * Enable all of a task's events that have been marked enable-on-exec.
2592 * This expects task == current.
2593 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002594static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002595{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002596 struct perf_event *event;
2597 unsigned long flags;
2598 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002599 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002600
2601 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002602 if (!ctx || !ctx->nr_events)
2603 goto out;
2604
Stephane Eraniane566b762011-04-06 02:54:54 +02002605 /*
2606 * We must ctxsw out cgroup events to avoid conflict
2607 * when invoking perf_task_event_sched_in() later on
2608 * in this function. Otherwise we end up trying to
2609 * ctxswin cgroup events which are already scheduled
2610 * in.
2611 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002612 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002613
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002614 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002615 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002616
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002617 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002618 ret = event_enable_on_exec(event, ctx);
2619 if (ret)
2620 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002621 }
2622
2623 /*
2624 * Unclone this context if we enabled any event.
2625 */
2626 if (enabled)
2627 unclone_ctx(ctx);
2628
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002629 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002630
Stephane Eraniane566b762011-04-06 02:54:54 +02002631 /*
2632 * Also calls ctxswin for cgroup events, if any:
2633 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002634 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002635out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002636 local_irq_restore(flags);
2637}
2638
2639/*
2640 * Cross CPU call to read the hardware event
2641 */
2642static void __perf_event_read(void *info)
2643{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002644 struct perf_event *event = info;
2645 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002646 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002647
2648 /*
2649 * If this is a task context, we need to check whether it is
2650 * the current task context of this cpu. If not it has been
2651 * scheduled out before the smp call arrived. In that case
2652 * event->count would have been updated to a recent sample
2653 * when the event was scheduled out.
2654 */
2655 if (ctx->task && cpuctx->task_ctx != ctx)
2656 return;
2657
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002658 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002659 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002660 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002661 update_cgrp_time_from_event(event);
2662 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002663 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002664 if (event->state == PERF_EVENT_STATE_ACTIVE)
2665 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002666 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002667}
2668
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002669static inline u64 perf_event_count(struct perf_event *event)
2670{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002671 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002672}
2673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674static u64 perf_event_read(struct perf_event *event)
2675{
2676 /*
2677 * If event is enabled and currently active on a CPU, update the
2678 * value in the event structure:
2679 */
2680 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2681 smp_call_function_single(event->oncpu,
2682 __perf_event_read, event, 1);
2683 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002684 struct perf_event_context *ctx = event->ctx;
2685 unsigned long flags;
2686
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002687 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002688 /*
2689 * may read while context is not active
2690 * (e.g., thread is blocked), in that case
2691 * we cannot update context time
2692 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002693 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002694 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002695 update_cgrp_time_from_event(event);
2696 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002697 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002698 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002699 }
2700
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002701 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702}
2703
2704/*
2705 * Initialize the perf_event context in a task_struct:
2706 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002707static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002708{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002709 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002710 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002711 INIT_LIST_HEAD(&ctx->pinned_groups);
2712 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713 INIT_LIST_HEAD(&ctx->event_list);
2714 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715}
2716
Peter Zijlstraeb184472010-09-07 15:55:13 +02002717static struct perf_event_context *
2718alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719{
2720 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002721
2722 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2723 if (!ctx)
2724 return NULL;
2725
2726 __perf_event_init_context(ctx);
2727 if (task) {
2728 ctx->task = task;
2729 get_task_struct(task);
2730 }
2731 ctx->pmu = pmu;
2732
2733 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734}
2735
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002736static struct task_struct *
2737find_lively_task_by_vpid(pid_t vpid)
2738{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740 int err;
2741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002743 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 task = current;
2745 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002746 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747 if (task)
2748 get_task_struct(task);
2749 rcu_read_unlock();
2750
2751 if (!task)
2752 return ERR_PTR(-ESRCH);
2753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754 /* Reuse ptrace permission checks for now. */
2755 err = -EACCES;
2756 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2757 goto errout;
2758
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002759 return task;
2760errout:
2761 put_task_struct(task);
2762 return ERR_PTR(err);
2763
2764}
2765
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002766/*
2767 * Returns a matching context with refcount and pincount.
2768 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002769static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002770find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002771{
2772 struct perf_event_context *ctx;
2773 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002775 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002776
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002777 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 /* Must be root to operate on a CPU event: */
2779 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2780 return ERR_PTR(-EACCES);
2781
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782 /*
2783 * We could be clever and allow to attach a event to an
2784 * offline CPU and activate it when the CPU comes up, but
2785 * that's for later.
2786 */
2787 if (!cpu_online(cpu))
2788 return ERR_PTR(-ENODEV);
2789
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002790 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 ctx = &cpuctx->ctx;
2792 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002793 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794
2795 return ctx;
2796 }
2797
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002798 err = -EINVAL;
2799 ctxn = pmu->task_ctx_nr;
2800 if (ctxn < 0)
2801 goto errout;
2802
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002803retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002804 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805 if (ctx) {
2806 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002807 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002808 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002809 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002810 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002811 err = -ENOMEM;
2812 if (!ctx)
2813 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002814
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002815 err = 0;
2816 mutex_lock(&task->perf_event_mutex);
2817 /*
2818 * If it has already passed perf_event_exit_task().
2819 * we must see PF_EXITING, it takes this mutex too.
2820 */
2821 if (task->flags & PF_EXITING)
2822 err = -ESRCH;
2823 else if (task->perf_event_ctxp[ctxn])
2824 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002825 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002826 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002827 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002828 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002829 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002830 mutex_unlock(&task->perf_event_mutex);
2831
2832 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002833 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002834
2835 if (err == -EAGAIN)
2836 goto retry;
2837 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002838 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002839 }
2840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841 return ctx;
2842
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002843errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002844 return ERR_PTR(err);
2845}
2846
Li Zefan6fb29152009-10-15 11:21:42 +08002847static void perf_event_free_filter(struct perf_event *event);
2848
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849static void free_event_rcu(struct rcu_head *head)
2850{
2851 struct perf_event *event;
2852
2853 event = container_of(head, struct perf_event, rcu_head);
2854 if (event->ns)
2855 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002856 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857 kfree(event);
2858}
2859
Frederic Weisbecker76369132011-05-19 19:55:04 +02002860static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861
2862static void free_event(struct perf_event *event)
2863{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002864 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002865
2866 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002867 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01002868 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002869 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002870 atomic_dec(&nr_mmap_events);
2871 if (event->attr.comm)
2872 atomic_dec(&nr_comm_events);
2873 if (event->attr.task)
2874 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002875 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2876 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002877 if (is_cgroup_event(event)) {
2878 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01002879 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01002880 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002881
2882 if (has_branch_stack(event)) {
2883 static_key_slow_dec_deferred(&perf_sched_events);
2884 /* is system-wide event */
2885 if (!(event->attach_state & PERF_ATTACH_TASK))
2886 atomic_dec(&per_cpu(perf_branch_stack_events,
2887 event->cpu));
2888 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002889 }
2890
Frederic Weisbecker76369132011-05-19 19:55:04 +02002891 if (event->rb) {
2892 ring_buffer_put(event->rb);
2893 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002894 }
2895
Stephane Eraniane5d13672011-02-14 11:20:01 +02002896 if (is_cgroup_event(event))
2897 perf_detach_cgroup(event);
2898
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002899 if (event->destroy)
2900 event->destroy(event);
2901
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002902 if (event->ctx)
2903 put_ctx(event->ctx);
2904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 call_rcu(&event->rcu_head, free_event_rcu);
2906}
2907
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002908int perf_event_release_kernel(struct perf_event *event)
2909{
2910 struct perf_event_context *ctx = event->ctx;
2911
2912 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002913 /*
2914 * There are two ways this annotation is useful:
2915 *
2916 * 1) there is a lock recursion from perf_event_exit_task
2917 * see the comment there.
2918 *
2919 * 2) there is a lock-inversion with mmap_sem through
2920 * perf_event_read_group(), which takes faults while
2921 * holding ctx->mutex, however this is called after
2922 * the last filedesc died, so there is no possibility
2923 * to trigger the AB-BA case.
2924 */
2925 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002926 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002927 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002928 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02002929 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002930 mutex_unlock(&ctx->mutex);
2931
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002932 free_event(event);
2933
2934 return 0;
2935}
2936EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2937
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002938/*
2939 * Called when the last reference to the file is gone.
2940 */
Al Viroa6fa9412012-08-20 14:59:25 +01002941static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002942{
Peter Zijlstra88821352010-11-09 19:01:43 +01002943 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002944
Al Viroa6fa9412012-08-20 14:59:25 +01002945 if (!atomic_long_dec_and_test(&event->refcount))
2946 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002947
Peter Zijlstra88821352010-11-09 19:01:43 +01002948 rcu_read_lock();
2949 owner = ACCESS_ONCE(event->owner);
2950 /*
2951 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2952 * !owner it means the list deletion is complete and we can indeed
2953 * free this event, otherwise we need to serialize on
2954 * owner->perf_event_mutex.
2955 */
2956 smp_read_barrier_depends();
2957 if (owner) {
2958 /*
2959 * Since delayed_put_task_struct() also drops the last
2960 * task reference we can safely take a new reference
2961 * while holding the rcu_read_lock().
2962 */
2963 get_task_struct(owner);
2964 }
2965 rcu_read_unlock();
2966
2967 if (owner) {
2968 mutex_lock(&owner->perf_event_mutex);
2969 /*
2970 * We have to re-check the event->owner field, if it is cleared
2971 * we raced with perf_event_exit_task(), acquiring the mutex
2972 * ensured they're done, and we can proceed with freeing the
2973 * event.
2974 */
2975 if (event->owner)
2976 list_del_init(&event->owner_entry);
2977 mutex_unlock(&owner->perf_event_mutex);
2978 put_task_struct(owner);
2979 }
2980
Al Viroa6fa9412012-08-20 14:59:25 +01002981 perf_event_release_kernel(event);
2982}
2983
2984static int perf_release(struct inode *inode, struct file *file)
2985{
2986 put_event(file->private_data);
2987 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002988}
2989
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002990u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002991{
2992 struct perf_event *child;
2993 u64 total = 0;
2994
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002995 *enabled = 0;
2996 *running = 0;
2997
Peter Zijlstra6f105812009-11-20 22:19:56 +01002998 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002999 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003000 *enabled += event->total_time_enabled +
3001 atomic64_read(&event->child_total_time_enabled);
3002 *running += event->total_time_running +
3003 atomic64_read(&event->child_total_time_running);
3004
3005 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003007 *enabled += child->total_time_enabled;
3008 *running += child->total_time_running;
3009 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003010 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011
3012 return total;
3013}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003014EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003016static int perf_event_read_group(struct perf_event *event,
3017 u64 read_format, char __user *buf)
3018{
3019 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003020 int n = 0, size = 0, ret = -EFAULT;
3021 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003022 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003023 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003024
Peter Zijlstra6f105812009-11-20 22:19:56 +01003025 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003026 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027
3028 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003029 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3030 values[n++] = enabled;
3031 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3032 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003033 values[n++] = count;
3034 if (read_format & PERF_FORMAT_ID)
3035 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036
3037 size = n * sizeof(u64);
3038
3039 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003040 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003041
Peter Zijlstra6f105812009-11-20 22:19:56 +01003042 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043
3044 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003045 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003046
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003047 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003048 if (read_format & PERF_FORMAT_ID)
3049 values[n++] = primary_event_id(sub);
3050
3051 size = n * sizeof(u64);
3052
Stephane Eranian184d3da2009-11-23 21:40:49 -08003053 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003054 ret = -EFAULT;
3055 goto unlock;
3056 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003057
3058 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003060unlock:
3061 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062
Peter Zijlstraabf48682009-11-20 22:19:49 +01003063 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064}
3065
3066static int perf_event_read_one(struct perf_event *event,
3067 u64 read_format, char __user *buf)
3068{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003069 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003070 u64 values[4];
3071 int n = 0;
3072
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003073 values[n++] = perf_event_read_value(event, &enabled, &running);
3074 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3075 values[n++] = enabled;
3076 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3077 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003078 if (read_format & PERF_FORMAT_ID)
3079 values[n++] = primary_event_id(event);
3080
3081 if (copy_to_user(buf, values, n * sizeof(u64)))
3082 return -EFAULT;
3083
3084 return n * sizeof(u64);
3085}
3086
3087/*
3088 * Read the performance event - simple non blocking version for now
3089 */
3090static ssize_t
3091perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3092{
3093 u64 read_format = event->attr.read_format;
3094 int ret;
3095
3096 /*
3097 * Return end-of-file for a read on a event that is in
3098 * error state (i.e. because it was pinned but it couldn't be
3099 * scheduled on to the CPU at some point).
3100 */
3101 if (event->state == PERF_EVENT_STATE_ERROR)
3102 return 0;
3103
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003104 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105 return -ENOSPC;
3106
3107 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 if (read_format & PERF_FORMAT_GROUP)
3109 ret = perf_event_read_group(event, read_format, buf);
3110 else
3111 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
3113 return ret;
3114}
3115
3116static ssize_t
3117perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3118{
3119 struct perf_event *event = file->private_data;
3120
3121 return perf_read_hw(event, buf, count);
3122}
3123
3124static unsigned int perf_poll(struct file *file, poll_table *wait)
3125{
3126 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003127 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128 unsigned int events = POLL_HUP;
3129
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003130 /*
3131 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3132 * grabs the rb reference but perf_event_set_output() overrides it.
3133 * Here is the timeline for two threads T1, T2:
3134 * t0: T1, rb = rcu_dereference(event->rb)
3135 * t1: T2, old_rb = event->rb
3136 * t2: T2, event->rb = new rb
3137 * t3: T2, ring_buffer_detach(old_rb)
3138 * t4: T1, ring_buffer_attach(rb1)
3139 * t5: T1, poll_wait(event->waitq)
3140 *
3141 * To avoid this problem, we grab mmap_mutex in perf_poll()
3142 * thereby ensuring that the assignment of the new ring buffer
3143 * and the detachment of the old buffer appear atomic to perf_poll()
3144 */
3145 mutex_lock(&event->mmap_mutex);
3146
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003147 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003148 rb = rcu_dereference(event->rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003149 if (rb) {
3150 ring_buffer_attach(event, rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003151 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003152 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 rcu_read_unlock();
3154
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003155 mutex_unlock(&event->mmap_mutex);
3156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157 poll_wait(file, &event->waitq, wait);
3158
3159 return events;
3160}
3161
3162static void perf_event_reset(struct perf_event *event)
3163{
3164 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003165 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166 perf_event_update_userpage(event);
3167}
3168
3169/*
3170 * Holding the top-level event's child_mutex means that any
3171 * descendant process that has inherited this event will block
3172 * in sync_child_event if it goes to exit, thus satisfying the
3173 * task existence requirements of perf_event_enable/disable.
3174 */
3175static void perf_event_for_each_child(struct perf_event *event,
3176 void (*func)(struct perf_event *))
3177{
3178 struct perf_event *child;
3179
3180 WARN_ON_ONCE(event->ctx->parent_ctx);
3181 mutex_lock(&event->child_mutex);
3182 func(event);
3183 list_for_each_entry(child, &event->child_list, child_list)
3184 func(child);
3185 mutex_unlock(&event->child_mutex);
3186}
3187
3188static void perf_event_for_each(struct perf_event *event,
3189 void (*func)(struct perf_event *))
3190{
3191 struct perf_event_context *ctx = event->ctx;
3192 struct perf_event *sibling;
3193
3194 WARN_ON_ONCE(ctx->parent_ctx);
3195 mutex_lock(&ctx->mutex);
3196 event = event->group_leader;
3197
3198 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003200 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201 mutex_unlock(&ctx->mutex);
3202}
3203
3204static int perf_event_period(struct perf_event *event, u64 __user *arg)
3205{
3206 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 int ret = 0;
3208 u64 value;
3209
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003210 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211 return -EINVAL;
3212
John Blackwoodad0cf342010-09-28 18:03:11 -04003213 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 return -EFAULT;
3215
3216 if (!value)
3217 return -EINVAL;
3218
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003219 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220 if (event->attr.freq) {
3221 if (value > sysctl_perf_event_sample_rate) {
3222 ret = -EINVAL;
3223 goto unlock;
3224 }
3225
3226 event->attr.sample_freq = value;
3227 } else {
3228 event->attr.sample_period = value;
3229 event->hw.sample_period = value;
3230 }
3231unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003232 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233
3234 return ret;
3235}
3236
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003237static const struct file_operations perf_fops;
3238
Al Viro2903ff02012-08-28 12:52:22 -04003239static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003240{
Al Viro2903ff02012-08-28 12:52:22 -04003241 struct fd f = fdget(fd);
3242 if (!f.file)
3243 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003244
Al Viro2903ff02012-08-28 12:52:22 -04003245 if (f.file->f_op != &perf_fops) {
3246 fdput(f);
3247 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003248 }
Al Viro2903ff02012-08-28 12:52:22 -04003249 *p = f;
3250 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003251}
3252
3253static int perf_event_set_output(struct perf_event *event,
3254 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003255static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256
3257static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3258{
3259 struct perf_event *event = file->private_data;
3260 void (*func)(struct perf_event *);
3261 u32 flags = arg;
3262
3263 switch (cmd) {
3264 case PERF_EVENT_IOC_ENABLE:
3265 func = perf_event_enable;
3266 break;
3267 case PERF_EVENT_IOC_DISABLE:
3268 func = perf_event_disable;
3269 break;
3270 case PERF_EVENT_IOC_RESET:
3271 func = perf_event_reset;
3272 break;
3273
3274 case PERF_EVENT_IOC_REFRESH:
3275 return perf_event_refresh(event, arg);
3276
3277 case PERF_EVENT_IOC_PERIOD:
3278 return perf_event_period(event, (u64 __user *)arg);
3279
3280 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003281 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003282 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003283 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003284 struct perf_event *output_event;
3285 struct fd output;
3286 ret = perf_fget_light(arg, &output);
3287 if (ret)
3288 return ret;
3289 output_event = output.file->private_data;
3290 ret = perf_event_set_output(event, output_event);
3291 fdput(output);
3292 } else {
3293 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003294 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003295 return ret;
3296 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297
Li Zefan6fb29152009-10-15 11:21:42 +08003298 case PERF_EVENT_IOC_SET_FILTER:
3299 return perf_event_set_filter(event, (void __user *)arg);
3300
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301 default:
3302 return -ENOTTY;
3303 }
3304
3305 if (flags & PERF_IOC_FLAG_GROUP)
3306 perf_event_for_each(event, func);
3307 else
3308 perf_event_for_each_child(event, func);
3309
3310 return 0;
3311}
3312
3313int perf_event_task_enable(void)
3314{
3315 struct perf_event *event;
3316
3317 mutex_lock(&current->perf_event_mutex);
3318 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3319 perf_event_for_each_child(event, perf_event_enable);
3320 mutex_unlock(&current->perf_event_mutex);
3321
3322 return 0;
3323}
3324
3325int perf_event_task_disable(void)
3326{
3327 struct perf_event *event;
3328
3329 mutex_lock(&current->perf_event_mutex);
3330 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3331 perf_event_for_each_child(event, perf_event_disable);
3332 mutex_unlock(&current->perf_event_mutex);
3333
3334 return 0;
3335}
3336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003337static int perf_event_index(struct perf_event *event)
3338{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003339 if (event->hw.state & PERF_HES_STOPPED)
3340 return 0;
3341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342 if (event->state != PERF_EVENT_STATE_ACTIVE)
3343 return 0;
3344
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003345 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346}
3347
Eric B Munsonc4794292011-06-23 16:34:38 -04003348static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003349 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003350 u64 *enabled,
3351 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003352{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003353 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003354
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003355 *now = perf_clock();
3356 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003357 *enabled = ctx_time - event->tstamp_enabled;
3358 *running = ctx_time - event->tstamp_running;
3359}
3360
Peter Zijlstrac7206202012-03-22 17:26:36 +01003361void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003362{
3363}
3364
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365/*
3366 * Callers need to ensure there can be no nesting of this function, otherwise
3367 * the seqlock logic goes bad. We can not serialize this because the arch
3368 * code calls this from NMI context.
3369 */
3370void perf_event_update_userpage(struct perf_event *event)
3371{
3372 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003373 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003374 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003375
3376 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003377 /*
3378 * compute total_time_enabled, total_time_running
3379 * based on snapshot values taken when the event
3380 * was last scheduled in.
3381 *
3382 * we cannot simply called update_context_time()
3383 * because of locking issue as we can be called in
3384 * NMI context
3385 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003386 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003387 rb = rcu_dereference(event->rb);
3388 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389 goto unlock;
3390
Frederic Weisbecker76369132011-05-19 19:55:04 +02003391 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003392
3393 /*
3394 * Disable preemption so as to not let the corresponding user-space
3395 * spin too long if we get preempted.
3396 */
3397 preempt_disable();
3398 ++userpg->lock;
3399 barrier();
3400 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003401 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003402 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003403 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003404
Eric B Munson0d641202011-06-24 12:26:26 -04003405 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406 atomic64_read(&event->child_total_time_enabled);
3407
Eric B Munson0d641202011-06-24 12:26:26 -04003408 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 atomic64_read(&event->child_total_time_running);
3410
Peter Zijlstrac7206202012-03-22 17:26:36 +01003411 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003412
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003413 barrier();
3414 ++userpg->lock;
3415 preempt_enable();
3416unlock:
3417 rcu_read_unlock();
3418}
3419
Peter Zijlstra906010b2009-09-21 16:08:49 +02003420static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3421{
3422 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003423 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003424 int ret = VM_FAULT_SIGBUS;
3425
3426 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3427 if (vmf->pgoff == 0)
3428 ret = 0;
3429 return ret;
3430 }
3431
3432 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003433 rb = rcu_dereference(event->rb);
3434 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003435 goto unlock;
3436
3437 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3438 goto unlock;
3439
Frederic Weisbecker76369132011-05-19 19:55:04 +02003440 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003441 if (!vmf->page)
3442 goto unlock;
3443
3444 get_page(vmf->page);
3445 vmf->page->mapping = vma->vm_file->f_mapping;
3446 vmf->page->index = vmf->pgoff;
3447
3448 ret = 0;
3449unlock:
3450 rcu_read_unlock();
3451
3452 return ret;
3453}
3454
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003455static void ring_buffer_attach(struct perf_event *event,
3456 struct ring_buffer *rb)
3457{
3458 unsigned long flags;
3459
3460 if (!list_empty(&event->rb_entry))
3461 return;
3462
3463 spin_lock_irqsave(&rb->event_lock, flags);
3464 if (!list_empty(&event->rb_entry))
3465 goto unlock;
3466
3467 list_add(&event->rb_entry, &rb->event_list);
3468unlock:
3469 spin_unlock_irqrestore(&rb->event_lock, flags);
3470}
3471
3472static void ring_buffer_detach(struct perf_event *event,
3473 struct ring_buffer *rb)
3474{
3475 unsigned long flags;
3476
3477 if (list_empty(&event->rb_entry))
3478 return;
3479
3480 spin_lock_irqsave(&rb->event_lock, flags);
3481 list_del_init(&event->rb_entry);
3482 wake_up_all(&event->waitq);
3483 spin_unlock_irqrestore(&rb->event_lock, flags);
3484}
3485
3486static void ring_buffer_wakeup(struct perf_event *event)
3487{
3488 struct ring_buffer *rb;
3489
3490 rcu_read_lock();
3491 rb = rcu_dereference(event->rb);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003492 if (!rb)
3493 goto unlock;
3494
3495 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003496 wake_up_all(&event->waitq);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003497
3498unlock:
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003499 rcu_read_unlock();
3500}
3501
Frederic Weisbecker76369132011-05-19 19:55:04 +02003502static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003503{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003504 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003505
Frederic Weisbecker76369132011-05-19 19:55:04 +02003506 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3507 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508}
3509
Frederic Weisbecker76369132011-05-19 19:55:04 +02003510static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003511{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003512 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003513
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003514 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003515 rb = rcu_dereference(event->rb);
3516 if (rb) {
3517 if (!atomic_inc_not_zero(&rb->refcount))
3518 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003519 }
3520 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003521
Frederic Weisbecker76369132011-05-19 19:55:04 +02003522 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003523}
3524
Frederic Weisbecker76369132011-05-19 19:55:04 +02003525static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003526{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003527 struct perf_event *event, *n;
3528 unsigned long flags;
3529
Frederic Weisbecker76369132011-05-19 19:55:04 +02003530 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003531 return;
3532
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003533 spin_lock_irqsave(&rb->event_lock, flags);
3534 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3535 list_del_init(&event->rb_entry);
3536 wake_up_all(&event->waitq);
3537 }
3538 spin_unlock_irqrestore(&rb->event_lock, flags);
3539
Frederic Weisbecker76369132011-05-19 19:55:04 +02003540 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003541}
3542
3543static void perf_mmap_open(struct vm_area_struct *vma)
3544{
3545 struct perf_event *event = vma->vm_file->private_data;
3546
3547 atomic_inc(&event->mmap_count);
3548}
3549
3550static void perf_mmap_close(struct vm_area_struct *vma)
3551{
3552 struct perf_event *event = vma->vm_file->private_data;
3553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003555 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003556 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003557 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003558
Peter Zijlstra906010b2009-09-21 16:08:49 +02003559 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003560 vma->vm_mm->pinned_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003561 rcu_assign_pointer(event->rb, NULL);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003562 ring_buffer_detach(event, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003564
Frederic Weisbecker76369132011-05-19 19:55:04 +02003565 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003566 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567 }
3568}
3569
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003570static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003571 .open = perf_mmap_open,
3572 .close = perf_mmap_close,
3573 .fault = perf_mmap_fault,
3574 .page_mkwrite = perf_mmap_fault,
3575};
3576
3577static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3578{
3579 struct perf_event *event = file->private_data;
3580 unsigned long user_locked, user_lock_limit;
3581 struct user_struct *user = current_user();
3582 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003583 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003584 unsigned long vma_size;
3585 unsigned long nr_pages;
3586 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003587 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003588
Peter Zijlstrac7920612010-05-18 10:33:24 +02003589 /*
3590 * Don't allow mmap() of inherited per-task counters. This would
3591 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003592 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003593 */
3594 if (event->cpu == -1 && event->attr.inherit)
3595 return -EINVAL;
3596
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003597 if (!(vma->vm_flags & VM_SHARED))
3598 return -EINVAL;
3599
3600 vma_size = vma->vm_end - vma->vm_start;
3601 nr_pages = (vma_size / PAGE_SIZE) - 1;
3602
3603 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003604 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003605 * can do bitmasks instead of modulo.
3606 */
3607 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3608 return -EINVAL;
3609
3610 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3611 return -EINVAL;
3612
3613 if (vma->vm_pgoff != 0)
3614 return -EINVAL;
3615
3616 WARN_ON_ONCE(event->ctx->parent_ctx);
3617 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003618 if (event->rb) {
3619 if (event->rb->nr_pages == nr_pages)
3620 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003621 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003622 ret = -EINVAL;
3623 goto unlock;
3624 }
3625
3626 user_extra = nr_pages + 1;
3627 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3628
3629 /*
3630 * Increase the limit linearly with more CPUs:
3631 */
3632 user_lock_limit *= num_online_cpus();
3633
3634 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3635
3636 extra = 0;
3637 if (user_locked > user_lock_limit)
3638 extra = user_locked - user_lock_limit;
3639
Jiri Slaby78d7d402010-03-05 13:42:54 -08003640 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003641 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003642 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643
3644 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3645 !capable(CAP_IPC_LOCK)) {
3646 ret = -EPERM;
3647 goto unlock;
3648 }
3649
Frederic Weisbecker76369132011-05-19 19:55:04 +02003650 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003651
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003652 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003653 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003654
Vince Weaver4ec83632011-06-01 15:15:36 -04003655 rb = rb_alloc(nr_pages,
3656 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3657 event->cpu, flags);
3658
Frederic Weisbecker76369132011-05-19 19:55:04 +02003659 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003660 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003661 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003662 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003663 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003664
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003665 atomic_long_add(user_extra, &user->locked_vm);
3666 event->mmap_locked = extra;
3667 event->mmap_user = get_current_user();
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003668 vma->vm_mm->pinned_vm += event->mmap_locked;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003669
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003670 perf_event_update_userpage(event);
3671
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003673 if (!ret)
3674 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003675 mutex_unlock(&event->mmap_mutex);
3676
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003677 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003678 vma->vm_ops = &perf_mmap_vmops;
3679
3680 return ret;
3681}
3682
3683static int perf_fasync(int fd, struct file *filp, int on)
3684{
3685 struct inode *inode = filp->f_path.dentry->d_inode;
3686 struct perf_event *event = filp->private_data;
3687 int retval;
3688
3689 mutex_lock(&inode->i_mutex);
3690 retval = fasync_helper(fd, filp, on, &event->fasync);
3691 mutex_unlock(&inode->i_mutex);
3692
3693 if (retval < 0)
3694 return retval;
3695
3696 return 0;
3697}
3698
3699static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003700 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003701 .release = perf_release,
3702 .read = perf_read,
3703 .poll = perf_poll,
3704 .unlocked_ioctl = perf_ioctl,
3705 .compat_ioctl = perf_ioctl,
3706 .mmap = perf_mmap,
3707 .fasync = perf_fasync,
3708};
3709
3710/*
3711 * Perf event wakeup
3712 *
3713 * If there's data, ensure we set the poll() state and publish everything
3714 * to user-space before waking everybody up.
3715 */
3716
3717void perf_event_wakeup(struct perf_event *event)
3718{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003719 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720
3721 if (event->pending_kill) {
3722 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3723 event->pending_kill = 0;
3724 }
3725}
3726
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003727static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003728{
3729 struct perf_event *event = container_of(entry,
3730 struct perf_event, pending);
3731
3732 if (event->pending_disable) {
3733 event->pending_disable = 0;
3734 __perf_event_disable(event);
3735 }
3736
3737 if (event->pending_wakeup) {
3738 event->pending_wakeup = 0;
3739 perf_event_wakeup(event);
3740 }
3741}
3742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003744 * We assume there is only KVM supporting the callbacks.
3745 * Later on, we might change it to a list if there is
3746 * another virtualization implementation supporting the callbacks.
3747 */
3748struct perf_guest_info_callbacks *perf_guest_cbs;
3749
3750int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3751{
3752 perf_guest_cbs = cbs;
3753 return 0;
3754}
3755EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3756
3757int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3758{
3759 perf_guest_cbs = NULL;
3760 return 0;
3761}
3762EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3763
Jiri Olsa40189942012-08-07 15:20:37 +02003764static void
3765perf_output_sample_regs(struct perf_output_handle *handle,
3766 struct pt_regs *regs, u64 mask)
3767{
3768 int bit;
3769
3770 for_each_set_bit(bit, (const unsigned long *) &mask,
3771 sizeof(mask) * BITS_PER_BYTE) {
3772 u64 val;
3773
3774 val = perf_reg_value(regs, bit);
3775 perf_output_put(handle, val);
3776 }
3777}
3778
3779static void perf_sample_regs_user(struct perf_regs_user *regs_user,
3780 struct pt_regs *regs)
3781{
3782 if (!user_mode(regs)) {
3783 if (current->mm)
3784 regs = task_pt_regs(current);
3785 else
3786 regs = NULL;
3787 }
3788
3789 if (regs) {
3790 regs_user->regs = regs;
3791 regs_user->abi = perf_reg_abi(current);
3792 }
3793}
3794
Jiri Olsac5ebced2012-08-07 15:20:40 +02003795/*
3796 * Get remaining task size from user stack pointer.
3797 *
3798 * It'd be better to take stack vma map and limit this more
3799 * precisly, but there's no way to get it safely under interrupt,
3800 * so using TASK_SIZE as limit.
3801 */
3802static u64 perf_ustack_task_size(struct pt_regs *regs)
3803{
3804 unsigned long addr = perf_user_stack_pointer(regs);
3805
3806 if (!addr || addr >= TASK_SIZE)
3807 return 0;
3808
3809 return TASK_SIZE - addr;
3810}
3811
3812static u16
3813perf_sample_ustack_size(u16 stack_size, u16 header_size,
3814 struct pt_regs *regs)
3815{
3816 u64 task_size;
3817
3818 /* No regs, no stack pointer, no dump. */
3819 if (!regs)
3820 return 0;
3821
3822 /*
3823 * Check if we fit in with the requested stack size into the:
3824 * - TASK_SIZE
3825 * If we don't, we limit the size to the TASK_SIZE.
3826 *
3827 * - remaining sample size
3828 * If we don't, we customize the stack size to
3829 * fit in to the remaining sample size.
3830 */
3831
3832 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
3833 stack_size = min(stack_size, (u16) task_size);
3834
3835 /* Current header size plus static size and dynamic size. */
3836 header_size += 2 * sizeof(u64);
3837
3838 /* Do we fit in with the current stack dump size? */
3839 if ((u16) (header_size + stack_size) < header_size) {
3840 /*
3841 * If we overflow the maximum size for the sample,
3842 * we customize the stack dump size to fit in.
3843 */
3844 stack_size = USHRT_MAX - header_size - sizeof(u64);
3845 stack_size = round_up(stack_size, sizeof(u64));
3846 }
3847
3848 return stack_size;
3849}
3850
3851static void
3852perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
3853 struct pt_regs *regs)
3854{
3855 /* Case of a kernel thread, nothing to dump */
3856 if (!regs) {
3857 u64 size = 0;
3858 perf_output_put(handle, size);
3859 } else {
3860 unsigned long sp;
3861 unsigned int rem;
3862 u64 dyn_size;
3863
3864 /*
3865 * We dump:
3866 * static size
3867 * - the size requested by user or the best one we can fit
3868 * in to the sample max size
3869 * data
3870 * - user stack dump data
3871 * dynamic size
3872 * - the actual dumped size
3873 */
3874
3875 /* Static size. */
3876 perf_output_put(handle, dump_size);
3877
3878 /* Data. */
3879 sp = perf_user_stack_pointer(regs);
3880 rem = __output_copy_user(handle, (void *) sp, dump_size);
3881 dyn_size = dump_size - rem;
3882
3883 perf_output_skip(handle, rem);
3884
3885 /* Dynamic size. */
3886 perf_output_put(handle, dyn_size);
3887 }
3888}
3889
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003890static void __perf_event_header__init_id(struct perf_event_header *header,
3891 struct perf_sample_data *data,
3892 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003893{
3894 u64 sample_type = event->attr.sample_type;
3895
3896 data->type = sample_type;
3897 header->size += event->id_header_size;
3898
3899 if (sample_type & PERF_SAMPLE_TID) {
3900 /* namespace issues */
3901 data->tid_entry.pid = perf_event_pid(event, current);
3902 data->tid_entry.tid = perf_event_tid(event, current);
3903 }
3904
3905 if (sample_type & PERF_SAMPLE_TIME)
3906 data->time = perf_clock();
3907
3908 if (sample_type & PERF_SAMPLE_ID)
3909 data->id = primary_event_id(event);
3910
3911 if (sample_type & PERF_SAMPLE_STREAM_ID)
3912 data->stream_id = event->id;
3913
3914 if (sample_type & PERF_SAMPLE_CPU) {
3915 data->cpu_entry.cpu = raw_smp_processor_id();
3916 data->cpu_entry.reserved = 0;
3917 }
3918}
3919
Frederic Weisbecker76369132011-05-19 19:55:04 +02003920void perf_event_header__init_id(struct perf_event_header *header,
3921 struct perf_sample_data *data,
3922 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003923{
3924 if (event->attr.sample_id_all)
3925 __perf_event_header__init_id(header, data, event);
3926}
3927
3928static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3929 struct perf_sample_data *data)
3930{
3931 u64 sample_type = data->type;
3932
3933 if (sample_type & PERF_SAMPLE_TID)
3934 perf_output_put(handle, data->tid_entry);
3935
3936 if (sample_type & PERF_SAMPLE_TIME)
3937 perf_output_put(handle, data->time);
3938
3939 if (sample_type & PERF_SAMPLE_ID)
3940 perf_output_put(handle, data->id);
3941
3942 if (sample_type & PERF_SAMPLE_STREAM_ID)
3943 perf_output_put(handle, data->stream_id);
3944
3945 if (sample_type & PERF_SAMPLE_CPU)
3946 perf_output_put(handle, data->cpu_entry);
3947}
3948
Frederic Weisbecker76369132011-05-19 19:55:04 +02003949void perf_event__output_id_sample(struct perf_event *event,
3950 struct perf_output_handle *handle,
3951 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003952{
3953 if (event->attr.sample_id_all)
3954 __perf_event__output_id_sample(handle, sample);
3955}
3956
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003958 struct perf_event *event,
3959 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003960{
3961 u64 read_format = event->attr.read_format;
3962 u64 values[4];
3963 int n = 0;
3964
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003965 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003966 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003967 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003968 atomic64_read(&event->child_total_time_enabled);
3969 }
3970 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003971 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003972 atomic64_read(&event->child_total_time_running);
3973 }
3974 if (read_format & PERF_FORMAT_ID)
3975 values[n++] = primary_event_id(event);
3976
Frederic Weisbecker76369132011-05-19 19:55:04 +02003977 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003978}
3979
3980/*
3981 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3982 */
3983static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003984 struct perf_event *event,
3985 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986{
3987 struct perf_event *leader = event->group_leader, *sub;
3988 u64 read_format = event->attr.read_format;
3989 u64 values[5];
3990 int n = 0;
3991
3992 values[n++] = 1 + leader->nr_siblings;
3993
3994 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003995 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996
3997 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003998 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999
4000 if (leader != event)
4001 leader->pmu->read(leader);
4002
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004003 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004004 if (read_format & PERF_FORMAT_ID)
4005 values[n++] = primary_event_id(leader);
4006
Frederic Weisbecker76369132011-05-19 19:55:04 +02004007 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008
4009 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4010 n = 0;
4011
4012 if (sub != event)
4013 sub->pmu->read(sub);
4014
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004015 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004016 if (read_format & PERF_FORMAT_ID)
4017 values[n++] = primary_event_id(sub);
4018
Frederic Weisbecker76369132011-05-19 19:55:04 +02004019 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004020 }
4021}
4022
Stephane Eranianeed01522010-10-26 16:08:01 +02004023#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4024 PERF_FORMAT_TOTAL_TIME_RUNNING)
4025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004026static void perf_output_read(struct perf_output_handle *handle,
4027 struct perf_event *event)
4028{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004029 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004030 u64 read_format = event->attr.read_format;
4031
4032 /*
4033 * compute total_time_enabled, total_time_running
4034 * based on snapshot values taken when the event
4035 * was last scheduled in.
4036 *
4037 * we cannot simply called update_context_time()
4038 * because of locking issue as we are called in
4039 * NMI context
4040 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004041 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004042 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004044 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004045 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004046 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004047 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004048}
4049
4050void perf_output_sample(struct perf_output_handle *handle,
4051 struct perf_event_header *header,
4052 struct perf_sample_data *data,
4053 struct perf_event *event)
4054{
4055 u64 sample_type = data->type;
4056
4057 perf_output_put(handle, *header);
4058
4059 if (sample_type & PERF_SAMPLE_IP)
4060 perf_output_put(handle, data->ip);
4061
4062 if (sample_type & PERF_SAMPLE_TID)
4063 perf_output_put(handle, data->tid_entry);
4064
4065 if (sample_type & PERF_SAMPLE_TIME)
4066 perf_output_put(handle, data->time);
4067
4068 if (sample_type & PERF_SAMPLE_ADDR)
4069 perf_output_put(handle, data->addr);
4070
4071 if (sample_type & PERF_SAMPLE_ID)
4072 perf_output_put(handle, data->id);
4073
4074 if (sample_type & PERF_SAMPLE_STREAM_ID)
4075 perf_output_put(handle, data->stream_id);
4076
4077 if (sample_type & PERF_SAMPLE_CPU)
4078 perf_output_put(handle, data->cpu_entry);
4079
4080 if (sample_type & PERF_SAMPLE_PERIOD)
4081 perf_output_put(handle, data->period);
4082
4083 if (sample_type & PERF_SAMPLE_READ)
4084 perf_output_read(handle, event);
4085
4086 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4087 if (data->callchain) {
4088 int size = 1;
4089
4090 if (data->callchain)
4091 size += data->callchain->nr;
4092
4093 size *= sizeof(u64);
4094
Frederic Weisbecker76369132011-05-19 19:55:04 +02004095 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096 } else {
4097 u64 nr = 0;
4098 perf_output_put(handle, nr);
4099 }
4100 }
4101
4102 if (sample_type & PERF_SAMPLE_RAW) {
4103 if (data->raw) {
4104 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004105 __output_copy(handle, data->raw->data,
4106 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004107 } else {
4108 struct {
4109 u32 size;
4110 u32 data;
4111 } raw = {
4112 .size = sizeof(u32),
4113 .data = 0,
4114 };
4115 perf_output_put(handle, raw);
4116 }
4117 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004118
4119 if (!event->attr.watermark) {
4120 int wakeup_events = event->attr.wakeup_events;
4121
4122 if (wakeup_events) {
4123 struct ring_buffer *rb = handle->rb;
4124 int events = local_inc_return(&rb->events);
4125
4126 if (events >= wakeup_events) {
4127 local_sub(wakeup_events, &rb->events);
4128 local_inc(&rb->wakeup);
4129 }
4130 }
4131 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004132
4133 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4134 if (data->br_stack) {
4135 size_t size;
4136
4137 size = data->br_stack->nr
4138 * sizeof(struct perf_branch_entry);
4139
4140 perf_output_put(handle, data->br_stack->nr);
4141 perf_output_copy(handle, data->br_stack->entries, size);
4142 } else {
4143 /*
4144 * we always store at least the value of nr
4145 */
4146 u64 nr = 0;
4147 perf_output_put(handle, nr);
4148 }
4149 }
Jiri Olsa40189942012-08-07 15:20:37 +02004150
4151 if (sample_type & PERF_SAMPLE_REGS_USER) {
4152 u64 abi = data->regs_user.abi;
4153
4154 /*
4155 * If there are no regs to dump, notice it through
4156 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4157 */
4158 perf_output_put(handle, abi);
4159
4160 if (abi) {
4161 u64 mask = event->attr.sample_regs_user;
4162 perf_output_sample_regs(handle,
4163 data->regs_user.regs,
4164 mask);
4165 }
4166 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004167
4168 if (sample_type & PERF_SAMPLE_STACK_USER)
4169 perf_output_sample_ustack(handle,
4170 data->stack_user_size,
4171 data->regs_user.regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172}
4173
4174void perf_prepare_sample(struct perf_event_header *header,
4175 struct perf_sample_data *data,
4176 struct perf_event *event,
4177 struct pt_regs *regs)
4178{
4179 u64 sample_type = event->attr.sample_type;
4180
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004182 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004183
4184 header->misc = 0;
4185 header->misc |= perf_misc_flags(regs);
4186
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004187 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004188
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004189 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190 data->ip = perf_instruction_pointer(regs);
4191
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4193 int size = 1;
4194
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004195 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004196
4197 if (data->callchain)
4198 size += data->callchain->nr;
4199
4200 header->size += size * sizeof(u64);
4201 }
4202
4203 if (sample_type & PERF_SAMPLE_RAW) {
4204 int size = sizeof(u32);
4205
4206 if (data->raw)
4207 size += data->raw->size;
4208 else
4209 size += sizeof(u32);
4210
4211 WARN_ON_ONCE(size & (sizeof(u64)-1));
4212 header->size += size;
4213 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004214
4215 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4216 int size = sizeof(u64); /* nr */
4217 if (data->br_stack) {
4218 size += data->br_stack->nr
4219 * sizeof(struct perf_branch_entry);
4220 }
4221 header->size += size;
4222 }
Jiri Olsa40189942012-08-07 15:20:37 +02004223
4224 if (sample_type & PERF_SAMPLE_REGS_USER) {
4225 /* regs dump ABI info */
4226 int size = sizeof(u64);
4227
4228 perf_sample_regs_user(&data->regs_user, regs);
4229
4230 if (data->regs_user.regs) {
4231 u64 mask = event->attr.sample_regs_user;
4232 size += hweight64(mask) * sizeof(u64);
4233 }
4234
4235 header->size += size;
4236 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004237
4238 if (sample_type & PERF_SAMPLE_STACK_USER) {
4239 /*
4240 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4241 * processed as the last one or have additional check added
4242 * in case new sample type is added, because we could eat
4243 * up the rest of the sample size.
4244 */
4245 struct perf_regs_user *uregs = &data->regs_user;
4246 u16 stack_size = event->attr.sample_stack_user;
4247 u16 size = sizeof(u64);
4248
4249 if (!uregs->abi)
4250 perf_sample_regs_user(uregs, regs);
4251
4252 stack_size = perf_sample_ustack_size(stack_size, header->size,
4253 uregs->regs);
4254
4255 /*
4256 * If there is something to dump, add space for the dump
4257 * itself and for the field that tells the dynamic size,
4258 * which is how many have been actually dumped.
4259 */
4260 if (stack_size)
4261 size += sizeof(u64) + stack_size;
4262
4263 data->stack_user_size = stack_size;
4264 header->size += size;
4265 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004266}
4267
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004268static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 struct perf_sample_data *data,
4270 struct pt_regs *regs)
4271{
4272 struct perf_output_handle handle;
4273 struct perf_event_header header;
4274
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004275 /* protect the callchain buffers */
4276 rcu_read_lock();
4277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278 perf_prepare_sample(&header, data, event, regs);
4279
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004280 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004281 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004282
4283 perf_output_sample(&handle, &header, data, event);
4284
4285 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004286
4287exit:
4288 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004289}
4290
4291/*
4292 * read event_id
4293 */
4294
4295struct perf_read_event {
4296 struct perf_event_header header;
4297
4298 u32 pid;
4299 u32 tid;
4300};
4301
4302static void
4303perf_event_read_event(struct perf_event *event,
4304 struct task_struct *task)
4305{
4306 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004307 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308 struct perf_read_event read_event = {
4309 .header = {
4310 .type = PERF_RECORD_READ,
4311 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004312 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004313 },
4314 .pid = perf_event_pid(event, task),
4315 .tid = perf_event_tid(event, task),
4316 };
4317 int ret;
4318
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004319 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004320 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004321 if (ret)
4322 return;
4323
4324 perf_output_put(&handle, read_event);
4325 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004326 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004327
4328 perf_output_end(&handle);
4329}
4330
4331/*
4332 * task tracking -- fork/exit
4333 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004334 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335 */
4336
4337struct perf_task_event {
4338 struct task_struct *task;
4339 struct perf_event_context *task_ctx;
4340
4341 struct {
4342 struct perf_event_header header;
4343
4344 u32 pid;
4345 u32 ppid;
4346 u32 tid;
4347 u32 ptid;
4348 u64 time;
4349 } event_id;
4350};
4351
4352static void perf_event_task_output(struct perf_event *event,
4353 struct perf_task_event *task_event)
4354{
4355 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004356 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004358 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004359
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004360 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004361
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004362 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004363 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004364 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004365 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004366
4367 task_event->event_id.pid = perf_event_pid(event, task);
4368 task_event->event_id.ppid = perf_event_pid(event, current);
4369
4370 task_event->event_id.tid = perf_event_tid(event, task);
4371 task_event->event_id.ptid = perf_event_tid(event, current);
4372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373 perf_output_put(&handle, task_event->event_id);
4374
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004375 perf_event__output_id_sample(event, &handle, &sample);
4376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004378out:
4379 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004380}
4381
4382static int perf_event_task_match(struct perf_event *event)
4383{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004384 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004385 return 0;
4386
Stephane Eranian5632ab12011-01-03 18:20:01 +02004387 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004388 return 0;
4389
Eric B Munson3af9e852010-05-18 15:30:49 +01004390 if (event->attr.comm || event->attr.mmap ||
4391 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392 return 1;
4393
4394 return 0;
4395}
4396
4397static void perf_event_task_ctx(struct perf_event_context *ctx,
4398 struct perf_task_event *task_event)
4399{
4400 struct perf_event *event;
4401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004402 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4403 if (perf_event_task_match(event))
4404 perf_event_task_output(event, task_event);
4405 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406}
4407
4408static void perf_event_task_event(struct perf_task_event *task_event)
4409{
4410 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004411 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004412 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004413 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004414
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004415 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004416 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004417 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02004418 if (cpuctx->unique_pmu != pmu)
Peter Zijlstra51676952010-12-07 14:18:20 +01004419 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004420 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004421
4422 ctx = task_event->task_ctx;
4423 if (!ctx) {
4424 ctxn = pmu->task_ctx_nr;
4425 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004426 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004427 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4428 }
4429 if (ctx)
4430 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004431next:
4432 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004433 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004434 rcu_read_unlock();
4435}
4436
4437static void perf_event_task(struct task_struct *task,
4438 struct perf_event_context *task_ctx,
4439 int new)
4440{
4441 struct perf_task_event task_event;
4442
4443 if (!atomic_read(&nr_comm_events) &&
4444 !atomic_read(&nr_mmap_events) &&
4445 !atomic_read(&nr_task_events))
4446 return;
4447
4448 task_event = (struct perf_task_event){
4449 .task = task,
4450 .task_ctx = task_ctx,
4451 .event_id = {
4452 .header = {
4453 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4454 .misc = 0,
4455 .size = sizeof(task_event.event_id),
4456 },
4457 /* .pid */
4458 /* .ppid */
4459 /* .tid */
4460 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004461 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004462 },
4463 };
4464
4465 perf_event_task_event(&task_event);
4466}
4467
4468void perf_event_fork(struct task_struct *task)
4469{
4470 perf_event_task(task, NULL, 1);
4471}
4472
4473/*
4474 * comm tracking
4475 */
4476
4477struct perf_comm_event {
4478 struct task_struct *task;
4479 char *comm;
4480 int comm_size;
4481
4482 struct {
4483 struct perf_event_header header;
4484
4485 u32 pid;
4486 u32 tid;
4487 } event_id;
4488};
4489
4490static void perf_event_comm_output(struct perf_event *event,
4491 struct perf_comm_event *comm_event)
4492{
4493 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004494 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004496 int ret;
4497
4498 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4499 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004500 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501
4502 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004503 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504
4505 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4506 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4507
4508 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004509 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004511
4512 perf_event__output_id_sample(event, &handle, &sample);
4513
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004514 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004515out:
4516 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517}
4518
4519static int perf_event_comm_match(struct perf_event *event)
4520{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004521 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004522 return 0;
4523
Stephane Eranian5632ab12011-01-03 18:20:01 +02004524 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004525 return 0;
4526
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004527 if (event->attr.comm)
4528 return 1;
4529
4530 return 0;
4531}
4532
4533static void perf_event_comm_ctx(struct perf_event_context *ctx,
4534 struct perf_comm_event *comm_event)
4535{
4536 struct perf_event *event;
4537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004538 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4539 if (perf_event_comm_match(event))
4540 perf_event_comm_output(event, comm_event);
4541 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004542}
4543
4544static void perf_event_comm_event(struct perf_comm_event *comm_event)
4545{
4546 struct perf_cpu_context *cpuctx;
4547 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004550 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004551 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004552
4553 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004554 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004555 size = ALIGN(strlen(comm)+1, sizeof(u64));
4556
4557 comm_event->comm = comm;
4558 comm_event->comm_size = size;
4559
4560 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004561 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004562 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004563 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02004564 if (cpuctx->unique_pmu != pmu)
Peter Zijlstra51676952010-12-07 14:18:20 +01004565 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004566 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004567
4568 ctxn = pmu->task_ctx_nr;
4569 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004570 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004571
4572 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4573 if (ctx)
4574 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004575next:
4576 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004577 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578 rcu_read_unlock();
4579}
4580
4581void perf_event_comm(struct task_struct *task)
4582{
4583 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004584 struct perf_event_context *ctx;
4585 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004586
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004587 for_each_task_context_nr(ctxn) {
4588 ctx = task->perf_event_ctxp[ctxn];
4589 if (!ctx)
4590 continue;
4591
4592 perf_event_enable_on_exec(ctx);
4593 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004594
4595 if (!atomic_read(&nr_comm_events))
4596 return;
4597
4598 comm_event = (struct perf_comm_event){
4599 .task = task,
4600 /* .comm */
4601 /* .comm_size */
4602 .event_id = {
4603 .header = {
4604 .type = PERF_RECORD_COMM,
4605 .misc = 0,
4606 /* .size */
4607 },
4608 /* .pid */
4609 /* .tid */
4610 },
4611 };
4612
4613 perf_event_comm_event(&comm_event);
4614}
4615
4616/*
4617 * mmap tracking
4618 */
4619
4620struct perf_mmap_event {
4621 struct vm_area_struct *vma;
4622
4623 const char *file_name;
4624 int file_size;
4625
4626 struct {
4627 struct perf_event_header header;
4628
4629 u32 pid;
4630 u32 tid;
4631 u64 start;
4632 u64 len;
4633 u64 pgoff;
4634 } event_id;
4635};
4636
4637static void perf_event_mmap_output(struct perf_event *event,
4638 struct perf_mmap_event *mmap_event)
4639{
4640 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004641 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004643 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004644
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004645 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4646 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004647 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004649 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004650
4651 mmap_event->event_id.pid = perf_event_pid(event, current);
4652 mmap_event->event_id.tid = perf_event_tid(event, current);
4653
4654 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004655 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004657
4658 perf_event__output_id_sample(event, &handle, &sample);
4659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004661out:
4662 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004663}
4664
4665static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004666 struct perf_mmap_event *mmap_event,
4667 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004669 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004670 return 0;
4671
Stephane Eranian5632ab12011-01-03 18:20:01 +02004672 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004673 return 0;
4674
Eric B Munson3af9e852010-05-18 15:30:49 +01004675 if ((!executable && event->attr.mmap_data) ||
4676 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004677 return 1;
4678
4679 return 0;
4680}
4681
4682static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004683 struct perf_mmap_event *mmap_event,
4684 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004685{
4686 struct perf_event *event;
4687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004689 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690 perf_event_mmap_output(event, mmap_event);
4691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004692}
4693
4694static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4695{
4696 struct perf_cpu_context *cpuctx;
4697 struct perf_event_context *ctx;
4698 struct vm_area_struct *vma = mmap_event->vma;
4699 struct file *file = vma->vm_file;
4700 unsigned int size;
4701 char tmp[16];
4702 char *buf = NULL;
4703 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004704 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004705 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004706
4707 memset(tmp, 0, sizeof(tmp));
4708
4709 if (file) {
4710 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004711 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004712 * need to add enough zero bytes after the string to handle
4713 * the 64bit alignment we do later.
4714 */
4715 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4716 if (!buf) {
4717 name = strncpy(tmp, "//enomem", sizeof(tmp));
4718 goto got_name;
4719 }
4720 name = d_path(&file->f_path, buf, PATH_MAX);
4721 if (IS_ERR(name)) {
4722 name = strncpy(tmp, "//toolong", sizeof(tmp));
4723 goto got_name;
4724 }
4725 } else {
4726 if (arch_vma_name(mmap_event->vma)) {
4727 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4728 sizeof(tmp));
4729 goto got_name;
4730 }
4731
4732 if (!vma->vm_mm) {
4733 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4734 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004735 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4736 vma->vm_end >= vma->vm_mm->brk) {
4737 name = strncpy(tmp, "[heap]", sizeof(tmp));
4738 goto got_name;
4739 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4740 vma->vm_end >= vma->vm_mm->start_stack) {
4741 name = strncpy(tmp, "[stack]", sizeof(tmp));
4742 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004743 }
4744
4745 name = strncpy(tmp, "//anon", sizeof(tmp));
4746 goto got_name;
4747 }
4748
4749got_name:
4750 size = ALIGN(strlen(name)+1, sizeof(u64));
4751
4752 mmap_event->file_name = name;
4753 mmap_event->file_size = size;
4754
4755 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4756
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004757 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004758 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004759 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02004760 if (cpuctx->unique_pmu != pmu)
Peter Zijlstra51676952010-12-07 14:18:20 +01004761 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004762 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4763 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004764
4765 ctxn = pmu->task_ctx_nr;
4766 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004767 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004768
4769 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4770 if (ctx) {
4771 perf_event_mmap_ctx(ctx, mmap_event,
4772 vma->vm_flags & VM_EXEC);
4773 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004774next:
4775 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004776 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004777 rcu_read_unlock();
4778
4779 kfree(buf);
4780}
4781
Eric B Munson3af9e852010-05-18 15:30:49 +01004782void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004783{
4784 struct perf_mmap_event mmap_event;
4785
4786 if (!atomic_read(&nr_mmap_events))
4787 return;
4788
4789 mmap_event = (struct perf_mmap_event){
4790 .vma = vma,
4791 /* .file_name */
4792 /* .file_size */
4793 .event_id = {
4794 .header = {
4795 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004796 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004797 /* .size */
4798 },
4799 /* .pid */
4800 /* .tid */
4801 .start = vma->vm_start,
4802 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004803 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004804 },
4805 };
4806
4807 perf_event_mmap_event(&mmap_event);
4808}
4809
4810/*
4811 * IRQ throttle logging
4812 */
4813
4814static void perf_log_throttle(struct perf_event *event, int enable)
4815{
4816 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004817 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004818 int ret;
4819
4820 struct {
4821 struct perf_event_header header;
4822 u64 time;
4823 u64 id;
4824 u64 stream_id;
4825 } throttle_event = {
4826 .header = {
4827 .type = PERF_RECORD_THROTTLE,
4828 .misc = 0,
4829 .size = sizeof(throttle_event),
4830 },
4831 .time = perf_clock(),
4832 .id = primary_event_id(event),
4833 .stream_id = event->id,
4834 };
4835
4836 if (enable)
4837 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4838
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004839 perf_event_header__init_id(&throttle_event.header, &sample, event);
4840
4841 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004842 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843 if (ret)
4844 return;
4845
4846 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004847 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848 perf_output_end(&handle);
4849}
4850
4851/*
4852 * Generic event overflow handling, sampling.
4853 */
4854
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004855static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856 int throttle, struct perf_sample_data *data,
4857 struct pt_regs *regs)
4858{
4859 int events = atomic_read(&event->event_limit);
4860 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004861 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862 int ret = 0;
4863
Peter Zijlstra96398822010-11-24 18:55:29 +01004864 /*
4865 * Non-sampling counters might still use the PMI to fold short
4866 * hardware counters, ignore those.
4867 */
4868 if (unlikely(!is_sampling_event(event)))
4869 return 0;
4870
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004871 seq = __this_cpu_read(perf_throttled_seq);
4872 if (seq != hwc->interrupts_seq) {
4873 hwc->interrupts_seq = seq;
4874 hwc->interrupts = 1;
4875 } else {
4876 hwc->interrupts++;
4877 if (unlikely(throttle
4878 && hwc->interrupts >= max_samples_per_tick)) {
4879 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01004880 hwc->interrupts = MAX_INTERRUPTS;
4881 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882 ret = 1;
4883 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004884 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885
4886 if (event->attr.freq) {
4887 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004888 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889
Peter Zijlstraabd50712010-01-26 18:50:16 +01004890 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004891
Peter Zijlstraabd50712010-01-26 18:50:16 +01004892 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004893 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894 }
4895
4896 /*
4897 * XXX event_limit might not quite work as expected on inherited
4898 * events
4899 */
4900
4901 event->pending_kill = POLL_IN;
4902 if (events && atomic_dec_and_test(&event->event_limit)) {
4903 ret = 1;
4904 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004905 event->pending_disable = 1;
4906 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004907 }
4908
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004909 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004910 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004911 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004912 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004913
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004914 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004915 event->pending_wakeup = 1;
4916 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004917 }
4918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004919 return ret;
4920}
4921
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004922int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923 struct perf_sample_data *data,
4924 struct pt_regs *regs)
4925{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004926 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004927}
4928
4929/*
4930 * Generic software event infrastructure
4931 */
4932
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004933struct swevent_htable {
4934 struct swevent_hlist *swevent_hlist;
4935 struct mutex hlist_mutex;
4936 int hlist_refcount;
4937
4938 /* Recursion avoidance in each contexts */
4939 int recursion[PERF_NR_CONTEXTS];
4940};
4941
4942static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004944/*
4945 * We directly increment event->count and keep a second value in
4946 * event->hw.period_left to count intervals. This period event
4947 * is kept in the range [-sample_period, 0] so that we can use the
4948 * sign as trigger.
4949 */
4950
4951static u64 perf_swevent_set_period(struct perf_event *event)
4952{
4953 struct hw_perf_event *hwc = &event->hw;
4954 u64 period = hwc->last_period;
4955 u64 nr, offset;
4956 s64 old, val;
4957
4958 hwc->last_period = hwc->sample_period;
4959
4960again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004961 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004962 if (val < 0)
4963 return 0;
4964
4965 nr = div64_u64(period + val, period);
4966 offset = nr * period;
4967 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004968 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004969 goto again;
4970
4971 return nr;
4972}
4973
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004974static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004975 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976 struct pt_regs *regs)
4977{
4978 struct hw_perf_event *hwc = &event->hw;
4979 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004980
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004981 if (!overflow)
4982 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004983
4984 if (hwc->interrupts == MAX_INTERRUPTS)
4985 return;
4986
4987 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004988 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004989 data, regs)) {
4990 /*
4991 * We inhibit the overflow from happening when
4992 * hwc->interrupts == MAX_INTERRUPTS.
4993 */
4994 break;
4995 }
4996 throttle = 1;
4997 }
4998}
4999
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005000static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005001 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 struct pt_regs *regs)
5003{
5004 struct hw_perf_event *hwc = &event->hw;
5005
Peter Zijlstrae7850592010-05-21 14:43:08 +02005006 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005008 if (!regs)
5009 return;
5010
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005011 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005012 return;
5013
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005014 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5015 data->period = nr;
5016 return perf_swevent_overflow(event, 1, data, regs);
5017 } else
5018 data->period = event->hw.last_period;
5019
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005020 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005021 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005022
Peter Zijlstrae7850592010-05-21 14:43:08 +02005023 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005024 return;
5025
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005026 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005027}
5028
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005029static int perf_exclude_event(struct perf_event *event,
5030 struct pt_regs *regs)
5031{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005032 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005033 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005034
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005035 if (regs) {
5036 if (event->attr.exclude_user && user_mode(regs))
5037 return 1;
5038
5039 if (event->attr.exclude_kernel && !user_mode(regs))
5040 return 1;
5041 }
5042
5043 return 0;
5044}
5045
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005046static int perf_swevent_match(struct perf_event *event,
5047 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005048 u32 event_id,
5049 struct perf_sample_data *data,
5050 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005051{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052 if (event->attr.type != type)
5053 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005054
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055 if (event->attr.config != event_id)
5056 return 0;
5057
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005058 if (perf_exclude_event(event, regs))
5059 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060
5061 return 1;
5062}
5063
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005064static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005065{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005066 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005067
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005068 return hash_64(val, SWEVENT_HLIST_BITS);
5069}
5070
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005071static inline struct hlist_head *
5072__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005073{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005074 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005075
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005076 return &hlist->heads[hash];
5077}
5078
5079/* For the read side: events when they trigger */
5080static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005081find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005082{
5083 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005084
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005085 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005086 if (!hlist)
5087 return NULL;
5088
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005089 return __find_swevent_head(hlist, type, event_id);
5090}
5091
5092/* For the event head insertion and removal in the hlist */
5093static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005094find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005095{
5096 struct swevent_hlist *hlist;
5097 u32 event_id = event->attr.config;
5098 u64 type = event->attr.type;
5099
5100 /*
5101 * Event scheduling is always serialized against hlist allocation
5102 * and release. Which makes the protected version suitable here.
5103 * The context lock guarantees that.
5104 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005105 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005106 lockdep_is_held(&event->ctx->lock));
5107 if (!hlist)
5108 return NULL;
5109
5110 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005111}
5112
5113static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005114 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005115 struct perf_sample_data *data,
5116 struct pt_regs *regs)
5117{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005118 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005119 struct perf_event *event;
5120 struct hlist_node *node;
5121 struct hlist_head *head;
5122
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005123 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005124 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005125 if (!head)
5126 goto end;
5127
5128 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005129 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005130 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005131 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005132end:
5133 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134}
5135
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005136int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005137{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005138 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005139
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005140 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005141}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005142EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005143
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005144inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005146 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005147
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005148 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005149}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005150
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005151void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005152{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005153 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005154 int rctx;
5155
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005156 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005157 rctx = perf_swevent_get_recursion_context();
5158 if (rctx < 0)
5159 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005160
Robert Richterfd0d0002012-04-02 20:19:08 +02005161 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005162
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005163 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005164
5165 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005166 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167}
5168
5169static void perf_swevent_read(struct perf_event *event)
5170{
5171}
5172
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005173static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005175 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005176 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005177 struct hlist_head *head;
5178
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005179 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180 hwc->last_period = hwc->sample_period;
5181 perf_swevent_set_period(event);
5182 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005183
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005184 hwc->state = !(flags & PERF_EF_START);
5185
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005186 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005187 if (WARN_ON_ONCE(!head))
5188 return -EINVAL;
5189
5190 hlist_add_head_rcu(&event->hlist_entry, head);
5191
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005192 return 0;
5193}
5194
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005195static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005196{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005197 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198}
5199
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005200static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005201{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005202 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005203}
5204
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005205static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005206{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005207 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005208}
5209
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005210/* Deref the hlist from the update side */
5211static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005212swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005213{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005214 return rcu_dereference_protected(swhash->swevent_hlist,
5215 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005216}
5217
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005218static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005219{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005220 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005221
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005222 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005223 return;
5224
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005225 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005226 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005227}
5228
5229static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5230{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005231 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005232
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005233 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005234
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005235 if (!--swhash->hlist_refcount)
5236 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005237
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005238 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005239}
5240
5241static void swevent_hlist_put(struct perf_event *event)
5242{
5243 int cpu;
5244
5245 if (event->cpu != -1) {
5246 swevent_hlist_put_cpu(event, event->cpu);
5247 return;
5248 }
5249
5250 for_each_possible_cpu(cpu)
5251 swevent_hlist_put_cpu(event, cpu);
5252}
5253
5254static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5255{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005256 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005257 int err = 0;
5258
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005259 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005260
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005261 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005262 struct swevent_hlist *hlist;
5263
5264 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5265 if (!hlist) {
5266 err = -ENOMEM;
5267 goto exit;
5268 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005269 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005270 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005271 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005272exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005273 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005274
5275 return err;
5276}
5277
5278static int swevent_hlist_get(struct perf_event *event)
5279{
5280 int err;
5281 int cpu, failed_cpu;
5282
5283 if (event->cpu != -1)
5284 return swevent_hlist_get_cpu(event, event->cpu);
5285
5286 get_online_cpus();
5287 for_each_possible_cpu(cpu) {
5288 err = swevent_hlist_get_cpu(event, cpu);
5289 if (err) {
5290 failed_cpu = cpu;
5291 goto fail;
5292 }
5293 }
5294 put_online_cpus();
5295
5296 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005297fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005298 for_each_possible_cpu(cpu) {
5299 if (cpu == failed_cpu)
5300 break;
5301 swevent_hlist_put_cpu(event, cpu);
5302 }
5303
5304 put_online_cpus();
5305 return err;
5306}
5307
Ingo Molnarc5905af2012-02-24 08:31:31 +01005308struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005309
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005310static void sw_perf_event_destroy(struct perf_event *event)
5311{
5312 u64 event_id = event->attr.config;
5313
5314 WARN_ON(event->parent);
5315
Ingo Molnarc5905af2012-02-24 08:31:31 +01005316 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005317 swevent_hlist_put(event);
5318}
5319
5320static int perf_swevent_init(struct perf_event *event)
5321{
5322 int event_id = event->attr.config;
5323
5324 if (event->attr.type != PERF_TYPE_SOFTWARE)
5325 return -ENOENT;
5326
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005327 /*
5328 * no branch sampling for software events
5329 */
5330 if (has_branch_stack(event))
5331 return -EOPNOTSUPP;
5332
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005333 switch (event_id) {
5334 case PERF_COUNT_SW_CPU_CLOCK:
5335 case PERF_COUNT_SW_TASK_CLOCK:
5336 return -ENOENT;
5337
5338 default:
5339 break;
5340 }
5341
Dan Carpenterce677832010-10-24 21:50:42 +02005342 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005343 return -ENOENT;
5344
5345 if (!event->parent) {
5346 int err;
5347
5348 err = swevent_hlist_get(event);
5349 if (err)
5350 return err;
5351
Ingo Molnarc5905af2012-02-24 08:31:31 +01005352 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005353 event->destroy = sw_perf_event_destroy;
5354 }
5355
5356 return 0;
5357}
5358
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005359static int perf_swevent_event_idx(struct perf_event *event)
5360{
5361 return 0;
5362}
5363
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005364static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005365 .task_ctx_nr = perf_sw_context,
5366
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005367 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005368 .add = perf_swevent_add,
5369 .del = perf_swevent_del,
5370 .start = perf_swevent_start,
5371 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005372 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005373
5374 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005375};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005376
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005377#ifdef CONFIG_EVENT_TRACING
5378
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005379static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005380 struct perf_sample_data *data)
5381{
5382 void *record = data->raw->data;
5383
5384 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5385 return 1;
5386 return 0;
5387}
5388
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005389static int perf_tp_event_match(struct perf_event *event,
5390 struct perf_sample_data *data,
5391 struct pt_regs *regs)
5392{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005393 if (event->hw.state & PERF_HES_STOPPED)
5394 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005395 /*
5396 * All tracepoints are from kernel-space.
5397 */
5398 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005399 return 0;
5400
5401 if (!perf_tp_filter_match(event, data))
5402 return 0;
5403
5404 return 1;
5405}
5406
5407void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005408 struct pt_regs *regs, struct hlist_head *head, int rctx,
5409 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005410{
5411 struct perf_sample_data data;
5412 struct perf_event *event;
5413 struct hlist_node *node;
5414
5415 struct perf_raw_record raw = {
5416 .size = entry_size,
5417 .data = record,
5418 };
5419
Robert Richterfd0d0002012-04-02 20:19:08 +02005420 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005421 data.raw = &raw;
5422
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005423 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5424 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005425 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005426 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005427
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005428 /*
5429 * If we got specified a target task, also iterate its context and
5430 * deliver this event there too.
5431 */
5432 if (task && task != current) {
5433 struct perf_event_context *ctx;
5434 struct trace_entry *entry = record;
5435
5436 rcu_read_lock();
5437 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5438 if (!ctx)
5439 goto unlock;
5440
5441 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5442 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5443 continue;
5444 if (event->attr.config != entry->type)
5445 continue;
5446 if (perf_tp_event_match(event, &data, regs))
5447 perf_swevent_event(event, count, &data, regs);
5448 }
5449unlock:
5450 rcu_read_unlock();
5451 }
5452
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005453 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005454}
5455EXPORT_SYMBOL_GPL(perf_tp_event);
5456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005457static void tp_perf_event_destroy(struct perf_event *event)
5458{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005459 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005460}
5461
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005462static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005464 int err;
5465
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005466 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5467 return -ENOENT;
5468
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005469 /*
5470 * no branch sampling for tracepoint events
5471 */
5472 if (has_branch_stack(event))
5473 return -EOPNOTSUPP;
5474
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005475 err = perf_trace_init(event);
5476 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005477 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005478
5479 event->destroy = tp_perf_event_destroy;
5480
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005481 return 0;
5482}
5483
5484static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005485 .task_ctx_nr = perf_sw_context,
5486
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005487 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005488 .add = perf_trace_add,
5489 .del = perf_trace_del,
5490 .start = perf_swevent_start,
5491 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005492 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005493
5494 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005495};
5496
5497static inline void perf_tp_register(void)
5498{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005499 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500}
Li Zefan6fb29152009-10-15 11:21:42 +08005501
5502static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5503{
5504 char *filter_str;
5505 int ret;
5506
5507 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5508 return -EINVAL;
5509
5510 filter_str = strndup_user(arg, PAGE_SIZE);
5511 if (IS_ERR(filter_str))
5512 return PTR_ERR(filter_str);
5513
5514 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5515
5516 kfree(filter_str);
5517 return ret;
5518}
5519
5520static void perf_event_free_filter(struct perf_event *event)
5521{
5522 ftrace_profile_free_filter(event);
5523}
5524
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005525#else
Li Zefan6fb29152009-10-15 11:21:42 +08005526
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005527static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005528{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005529}
Li Zefan6fb29152009-10-15 11:21:42 +08005530
5531static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5532{
5533 return -ENOENT;
5534}
5535
5536static void perf_event_free_filter(struct perf_event *event)
5537{
5538}
5539
Li Zefan07b139c2009-12-21 14:27:35 +08005540#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005541
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005542#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005543void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005544{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005545 struct perf_sample_data sample;
5546 struct pt_regs *regs = data;
5547
Robert Richterfd0d0002012-04-02 20:19:08 +02005548 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005549
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005550 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005551 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005552}
5553#endif
5554
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005555/*
5556 * hrtimer based swevent callback
5557 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005558
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005559static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005561 enum hrtimer_restart ret = HRTIMER_RESTART;
5562 struct perf_sample_data data;
5563 struct pt_regs *regs;
5564 struct perf_event *event;
5565 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005566
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005567 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005568
5569 if (event->state != PERF_EVENT_STATE_ACTIVE)
5570 return HRTIMER_NORESTART;
5571
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005572 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005573
Robert Richterfd0d0002012-04-02 20:19:08 +02005574 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005575 regs = get_irq_regs();
5576
5577 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005578 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02005579 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005580 ret = HRTIMER_NORESTART;
5581 }
5582
5583 period = max_t(u64, 10000, event->hw.sample_period);
5584 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5585
5586 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005587}
5588
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005589static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005591 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005592 s64 period;
5593
5594 if (!is_sampling_event(event))
5595 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005596
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005597 period = local64_read(&hwc->period_left);
5598 if (period) {
5599 if (period < 0)
5600 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005601
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005602 local64_set(&hwc->period_left, 0);
5603 } else {
5604 period = max_t(u64, 10000, hwc->sample_period);
5605 }
5606 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005607 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005608 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005609}
5610
5611static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5612{
5613 struct hw_perf_event *hwc = &event->hw;
5614
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005615 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005616 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005617 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005618
5619 hrtimer_cancel(&hwc->hrtimer);
5620 }
5621}
5622
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005623static void perf_swevent_init_hrtimer(struct perf_event *event)
5624{
5625 struct hw_perf_event *hwc = &event->hw;
5626
5627 if (!is_sampling_event(event))
5628 return;
5629
5630 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5631 hwc->hrtimer.function = perf_swevent_hrtimer;
5632
5633 /*
5634 * Since hrtimers have a fixed rate, we can do a static freq->period
5635 * mapping and avoid the whole period adjust feedback stuff.
5636 */
5637 if (event->attr.freq) {
5638 long freq = event->attr.sample_freq;
5639
5640 event->attr.sample_period = NSEC_PER_SEC / freq;
5641 hwc->sample_period = event->attr.sample_period;
5642 local64_set(&hwc->period_left, hwc->sample_period);
5643 event->attr.freq = 0;
5644 }
5645}
5646
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005647/*
5648 * Software event: cpu wall time clock
5649 */
5650
5651static void cpu_clock_event_update(struct perf_event *event)
5652{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005653 s64 prev;
5654 u64 now;
5655
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005656 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005657 prev = local64_xchg(&event->hw.prev_count, now);
5658 local64_add(now - prev, &event->count);
5659}
5660
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005661static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005662{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005663 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005664 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005665}
5666
5667static void cpu_clock_event_stop(struct perf_event *event, int flags)
5668{
5669 perf_swevent_cancel_hrtimer(event);
5670 cpu_clock_event_update(event);
5671}
5672
5673static int cpu_clock_event_add(struct perf_event *event, int flags)
5674{
5675 if (flags & PERF_EF_START)
5676 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005677
5678 return 0;
5679}
5680
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005681static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005682{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005683 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005684}
5685
5686static void cpu_clock_event_read(struct perf_event *event)
5687{
5688 cpu_clock_event_update(event);
5689}
5690
5691static int cpu_clock_event_init(struct perf_event *event)
5692{
5693 if (event->attr.type != PERF_TYPE_SOFTWARE)
5694 return -ENOENT;
5695
5696 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5697 return -ENOENT;
5698
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005699 /*
5700 * no branch sampling for software events
5701 */
5702 if (has_branch_stack(event))
5703 return -EOPNOTSUPP;
5704
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005705 perf_swevent_init_hrtimer(event);
5706
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005707 return 0;
5708}
5709
5710static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005711 .task_ctx_nr = perf_sw_context,
5712
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005713 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005714 .add = cpu_clock_event_add,
5715 .del = cpu_clock_event_del,
5716 .start = cpu_clock_event_start,
5717 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005718 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005719
5720 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005721};
5722
5723/*
5724 * Software event: task time clock
5725 */
5726
5727static void task_clock_event_update(struct perf_event *event, u64 now)
5728{
5729 u64 prev;
5730 s64 delta;
5731
5732 prev = local64_xchg(&event->hw.prev_count, now);
5733 delta = now - prev;
5734 local64_add(delta, &event->count);
5735}
5736
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005737static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005738{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005739 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005740 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005741}
5742
5743static void task_clock_event_stop(struct perf_event *event, int flags)
5744{
5745 perf_swevent_cancel_hrtimer(event);
5746 task_clock_event_update(event, event->ctx->time);
5747}
5748
5749static int task_clock_event_add(struct perf_event *event, int flags)
5750{
5751 if (flags & PERF_EF_START)
5752 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005753
5754 return 0;
5755}
5756
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005757static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005758{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005759 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005760}
5761
5762static void task_clock_event_read(struct perf_event *event)
5763{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005764 u64 now = perf_clock();
5765 u64 delta = now - event->ctx->timestamp;
5766 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005767
5768 task_clock_event_update(event, time);
5769}
5770
5771static int task_clock_event_init(struct perf_event *event)
5772{
5773 if (event->attr.type != PERF_TYPE_SOFTWARE)
5774 return -ENOENT;
5775
5776 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5777 return -ENOENT;
5778
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005779 /*
5780 * no branch sampling for software events
5781 */
5782 if (has_branch_stack(event))
5783 return -EOPNOTSUPP;
5784
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005785 perf_swevent_init_hrtimer(event);
5786
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005787 return 0;
5788}
5789
5790static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005791 .task_ctx_nr = perf_sw_context,
5792
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005793 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005794 .add = task_clock_event_add,
5795 .del = task_clock_event_del,
5796 .start = task_clock_event_start,
5797 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005798 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005799
5800 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005801};
5802
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005803static void perf_pmu_nop_void(struct pmu *pmu)
5804{
5805}
5806
5807static int perf_pmu_nop_int(struct pmu *pmu)
5808{
5809 return 0;
5810}
5811
5812static void perf_pmu_start_txn(struct pmu *pmu)
5813{
5814 perf_pmu_disable(pmu);
5815}
5816
5817static int perf_pmu_commit_txn(struct pmu *pmu)
5818{
5819 perf_pmu_enable(pmu);
5820 return 0;
5821}
5822
5823static void perf_pmu_cancel_txn(struct pmu *pmu)
5824{
5825 perf_pmu_enable(pmu);
5826}
5827
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005828static int perf_event_idx_default(struct perf_event *event)
5829{
5830 return event->hw.idx + 1;
5831}
5832
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005833/*
5834 * Ensures all contexts with the same task_ctx_nr have the same
5835 * pmu_cpu_context too.
5836 */
5837static void *find_pmu_context(int ctxn)
5838{
5839 struct pmu *pmu;
5840
5841 if (ctxn < 0)
5842 return NULL;
5843
5844 list_for_each_entry(pmu, &pmus, entry) {
5845 if (pmu->task_ctx_nr == ctxn)
5846 return pmu->pmu_cpu_context;
5847 }
5848
5849 return NULL;
5850}
5851
Peter Zijlstra51676952010-12-07 14:18:20 +01005852static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005853{
Peter Zijlstra51676952010-12-07 14:18:20 +01005854 int cpu;
5855
5856 for_each_possible_cpu(cpu) {
5857 struct perf_cpu_context *cpuctx;
5858
5859 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5860
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02005861 if (cpuctx->unique_pmu == old_pmu)
5862 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01005863 }
5864}
5865
5866static void free_pmu_context(struct pmu *pmu)
5867{
5868 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005869
5870 mutex_lock(&pmus_lock);
5871 /*
5872 * Like a real lame refcount.
5873 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005874 list_for_each_entry(i, &pmus, entry) {
5875 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5876 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005877 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005878 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005879 }
5880
Peter Zijlstra51676952010-12-07 14:18:20 +01005881 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005882out:
5883 mutex_unlock(&pmus_lock);
5884}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005885static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005886
Peter Zijlstraabe43402010-11-17 23:17:37 +01005887static ssize_t
5888type_show(struct device *dev, struct device_attribute *attr, char *page)
5889{
5890 struct pmu *pmu = dev_get_drvdata(dev);
5891
5892 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5893}
5894
5895static struct device_attribute pmu_dev_attrs[] = {
5896 __ATTR_RO(type),
5897 __ATTR_NULL,
5898};
5899
5900static int pmu_bus_running;
5901static struct bus_type pmu_bus = {
5902 .name = "event_source",
5903 .dev_attrs = pmu_dev_attrs,
5904};
5905
5906static void pmu_dev_release(struct device *dev)
5907{
5908 kfree(dev);
5909}
5910
5911static int pmu_dev_alloc(struct pmu *pmu)
5912{
5913 int ret = -ENOMEM;
5914
5915 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5916 if (!pmu->dev)
5917 goto out;
5918
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01005919 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01005920 device_initialize(pmu->dev);
5921 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5922 if (ret)
5923 goto free_dev;
5924
5925 dev_set_drvdata(pmu->dev, pmu);
5926 pmu->dev->bus = &pmu_bus;
5927 pmu->dev->release = pmu_dev_release;
5928 ret = device_add(pmu->dev);
5929 if (ret)
5930 goto free_dev;
5931
5932out:
5933 return ret;
5934
5935free_dev:
5936 put_device(pmu->dev);
5937 goto out;
5938}
5939
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005940static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005941static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005942
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005943int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005944{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005945 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005946
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005947 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005948 ret = -ENOMEM;
5949 pmu->pmu_disable_count = alloc_percpu(int);
5950 if (!pmu->pmu_disable_count)
5951 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005952
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005953 pmu->type = -1;
5954 if (!name)
5955 goto skip_type;
5956 pmu->name = name;
5957
5958 if (type < 0) {
5959 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5960 if (!err)
5961 goto free_pdc;
5962
5963 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5964 if (err) {
5965 ret = err;
5966 goto free_pdc;
5967 }
5968 }
5969 pmu->type = type;
5970
Peter Zijlstraabe43402010-11-17 23:17:37 +01005971 if (pmu_bus_running) {
5972 ret = pmu_dev_alloc(pmu);
5973 if (ret)
5974 goto free_idr;
5975 }
5976
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005977skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005978 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5979 if (pmu->pmu_cpu_context)
5980 goto got_cpu_context;
5981
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005982 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5983 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005984 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005985
5986 for_each_possible_cpu(cpu) {
5987 struct perf_cpu_context *cpuctx;
5988
5989 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005990 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005991 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005992 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005993 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005994 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005995 cpuctx->jiffies_interval = 1;
5996 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02005997 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005998 }
5999
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006000got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006001 if (!pmu->start_txn) {
6002 if (pmu->pmu_enable) {
6003 /*
6004 * If we have pmu_enable/pmu_disable calls, install
6005 * transaction stubs that use that to try and batch
6006 * hardware accesses.
6007 */
6008 pmu->start_txn = perf_pmu_start_txn;
6009 pmu->commit_txn = perf_pmu_commit_txn;
6010 pmu->cancel_txn = perf_pmu_cancel_txn;
6011 } else {
6012 pmu->start_txn = perf_pmu_nop_void;
6013 pmu->commit_txn = perf_pmu_nop_int;
6014 pmu->cancel_txn = perf_pmu_nop_void;
6015 }
6016 }
6017
6018 if (!pmu->pmu_enable) {
6019 pmu->pmu_enable = perf_pmu_nop_void;
6020 pmu->pmu_disable = perf_pmu_nop_void;
6021 }
6022
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006023 if (!pmu->event_idx)
6024 pmu->event_idx = perf_event_idx_default;
6025
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006026 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006027 ret = 0;
6028unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006029 mutex_unlock(&pmus_lock);
6030
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006031 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006032
Peter Zijlstraabe43402010-11-17 23:17:37 +01006033free_dev:
6034 device_del(pmu->dev);
6035 put_device(pmu->dev);
6036
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006037free_idr:
6038 if (pmu->type >= PERF_TYPE_MAX)
6039 idr_remove(&pmu_idr, pmu->type);
6040
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006041free_pdc:
6042 free_percpu(pmu->pmu_disable_count);
6043 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006044}
6045
6046void perf_pmu_unregister(struct pmu *pmu)
6047{
6048 mutex_lock(&pmus_lock);
6049 list_del_rcu(&pmu->entry);
6050 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051
6052 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006053 * We dereference the pmu list under both SRCU and regular RCU, so
6054 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006055 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006056 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006057 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006058
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006059 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006060 if (pmu->type >= PERF_TYPE_MAX)
6061 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006062 device_del(pmu->dev);
6063 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006064 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006065}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006066
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006067struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006068{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006069 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006070 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006071 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006072
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006073 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006074
6075 rcu_read_lock();
6076 pmu = idr_find(&pmu_idr, event->attr.type);
6077 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006078 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006079 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006080 ret = pmu->event_init(event);
6081 if (ret)
6082 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006083 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006084 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006085
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006086 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006087 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006088 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006089 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006090 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006091
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006092 if (ret != -ENOENT) {
6093 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006094 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006097 pmu = ERR_PTR(-ENOENT);
6098unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006099 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006100
6101 return pmu;
6102}
6103
6104/*
6105 * Allocate and initialize a event structure
6106 */
6107static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006108perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006109 struct task_struct *task,
6110 struct perf_event *group_leader,
6111 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006112 perf_overflow_handler_t overflow_handler,
6113 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006115 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006116 struct perf_event *event;
6117 struct hw_perf_event *hwc;
6118 long err;
6119
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006120 if ((unsigned)cpu >= nr_cpu_ids) {
6121 if (!task || cpu != -1)
6122 return ERR_PTR(-EINVAL);
6123 }
6124
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006125 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006126 if (!event)
6127 return ERR_PTR(-ENOMEM);
6128
6129 /*
6130 * Single events are their own group leaders, with an
6131 * empty sibling list:
6132 */
6133 if (!group_leader)
6134 group_leader = event;
6135
6136 mutex_init(&event->child_mutex);
6137 INIT_LIST_HEAD(&event->child_list);
6138
6139 INIT_LIST_HEAD(&event->group_entry);
6140 INIT_LIST_HEAD(&event->event_entry);
6141 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006142 INIT_LIST_HEAD(&event->rb_entry);
6143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006145 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006146
6147 mutex_init(&event->mmap_mutex);
6148
Al Viroa6fa9412012-08-20 14:59:25 +01006149 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150 event->cpu = cpu;
6151 event->attr = *attr;
6152 event->group_leader = group_leader;
6153 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006154 event->oncpu = -1;
6155
6156 event->parent = parent_event;
6157
6158 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6159 event->id = atomic64_inc_return(&perf_event_id);
6160
6161 event->state = PERF_EVENT_STATE_INACTIVE;
6162
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006163 if (task) {
6164 event->attach_state = PERF_ATTACH_TASK;
6165#ifdef CONFIG_HAVE_HW_BREAKPOINT
6166 /*
6167 * hw_breakpoint is a bit difficult here..
6168 */
6169 if (attr->type == PERF_TYPE_BREAKPOINT)
6170 event->hw.bp_target = task;
6171#endif
6172 }
6173
Avi Kivity4dc0da82011-06-29 18:42:35 +03006174 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006175 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006176 context = parent_event->overflow_handler_context;
6177 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006178
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006179 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006180 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006182 if (attr->disabled)
6183 event->state = PERF_EVENT_STATE_OFF;
6184
6185 pmu = NULL;
6186
6187 hwc = &event->hw;
6188 hwc->sample_period = attr->sample_period;
6189 if (attr->freq && attr->sample_freq)
6190 hwc->sample_period = 1;
6191 hwc->last_period = hwc->sample_period;
6192
Peter Zijlstrae7850592010-05-21 14:43:08 +02006193 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006194
6195 /*
6196 * we currently do not support PERF_FORMAT_GROUP on inherited events
6197 */
6198 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6199 goto done;
6200
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006201 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203done:
6204 err = 0;
6205 if (!pmu)
6206 err = -EINVAL;
6207 else if (IS_ERR(pmu))
6208 err = PTR_ERR(pmu);
6209
6210 if (err) {
6211 if (event->ns)
6212 put_pid_ns(event->ns);
6213 kfree(event);
6214 return ERR_PTR(err);
6215 }
6216
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006217 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006218 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01006219 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01006220 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006221 atomic_inc(&nr_mmap_events);
6222 if (event->attr.comm)
6223 atomic_inc(&nr_comm_events);
6224 if (event->attr.task)
6225 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006226 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6227 err = get_callchain_buffers();
6228 if (err) {
6229 free_event(event);
6230 return ERR_PTR(err);
6231 }
6232 }
Stephane Eraniand010b332012-02-09 23:21:00 +01006233 if (has_branch_stack(event)) {
6234 static_key_slow_inc(&perf_sched_events.key);
6235 if (!(event->attach_state & PERF_ATTACH_TASK))
6236 atomic_inc(&per_cpu(perf_branch_stack_events,
6237 event->cpu));
6238 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006239 }
6240
6241 return event;
6242}
6243
6244static int perf_copy_attr(struct perf_event_attr __user *uattr,
6245 struct perf_event_attr *attr)
6246{
6247 u32 size;
6248 int ret;
6249
6250 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6251 return -EFAULT;
6252
6253 /*
6254 * zero the full structure, so that a short copy will be nice.
6255 */
6256 memset(attr, 0, sizeof(*attr));
6257
6258 ret = get_user(size, &uattr->size);
6259 if (ret)
6260 return ret;
6261
6262 if (size > PAGE_SIZE) /* silly large */
6263 goto err_size;
6264
6265 if (!size) /* abi compat */
6266 size = PERF_ATTR_SIZE_VER0;
6267
6268 if (size < PERF_ATTR_SIZE_VER0)
6269 goto err_size;
6270
6271 /*
6272 * If we're handed a bigger struct than we know of,
6273 * ensure all the unknown bits are 0 - i.e. new
6274 * user-space does not rely on any kernel feature
6275 * extensions we dont know about yet.
6276 */
6277 if (size > sizeof(*attr)) {
6278 unsigned char __user *addr;
6279 unsigned char __user *end;
6280 unsigned char val;
6281
6282 addr = (void __user *)uattr + sizeof(*attr);
6283 end = (void __user *)uattr + size;
6284
6285 for (; addr < end; addr++) {
6286 ret = get_user(val, addr);
6287 if (ret)
6288 return ret;
6289 if (val)
6290 goto err_size;
6291 }
6292 size = sizeof(*attr);
6293 }
6294
6295 ret = copy_from_user(attr, uattr, size);
6296 if (ret)
6297 return -EFAULT;
6298
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306299 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300 return -EINVAL;
6301
6302 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6303 return -EINVAL;
6304
6305 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6306 return -EINVAL;
6307
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006308 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6309 u64 mask = attr->branch_sample_type;
6310
6311 /* only using defined bits */
6312 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6313 return -EINVAL;
6314
6315 /* at least one branch bit must be set */
6316 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6317 return -EINVAL;
6318
6319 /* kernel level capture: check permissions */
6320 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6321 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6322 return -EACCES;
6323
6324 /* propagate priv level, when not set for branch */
6325 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6326
6327 /* exclude_kernel checked on syscall entry */
6328 if (!attr->exclude_kernel)
6329 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6330
6331 if (!attr->exclude_user)
6332 mask |= PERF_SAMPLE_BRANCH_USER;
6333
6334 if (!attr->exclude_hv)
6335 mask |= PERF_SAMPLE_BRANCH_HV;
6336 /*
6337 * adjust user setting (for HW filter setup)
6338 */
6339 attr->branch_sample_type = mask;
6340 }
6341 }
Jiri Olsa40189942012-08-07 15:20:37 +02006342
Jiri Olsac5ebced2012-08-07 15:20:40 +02006343 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006344 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006345 if (ret)
6346 return ret;
6347 }
6348
6349 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6350 if (!arch_perf_have_user_stack_dump())
6351 return -ENOSYS;
6352
6353 /*
6354 * We have __u32 type for the size, but so far
6355 * we can only use __u16 as maximum due to the
6356 * __u16 sample size limit.
6357 */
6358 if (attr->sample_stack_user >= USHRT_MAX)
6359 ret = -EINVAL;
6360 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6361 ret = -EINVAL;
6362 }
Jiri Olsa40189942012-08-07 15:20:37 +02006363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006364out:
6365 return ret;
6366
6367err_size:
6368 put_user(sizeof(*attr), &uattr->size);
6369 ret = -E2BIG;
6370 goto out;
6371}
6372
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006373static int
6374perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006375{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006376 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006377 int ret = -EINVAL;
6378
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006379 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380 goto set;
6381
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006382 /* don't allow circular references */
6383 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006384 goto out;
6385
Peter Zijlstra0f139302010-05-20 14:35:15 +02006386 /*
6387 * Don't allow cross-cpu buffers
6388 */
6389 if (output_event->cpu != event->cpu)
6390 goto out;
6391
6392 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006393 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006394 */
6395 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6396 goto out;
6397
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006398set:
6399 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006400 /* Can't redirect output if we've got an active mmap() */
6401 if (atomic_read(&event->mmap_count))
6402 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006403
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006404 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006405 /* get the rb we want to redirect to */
6406 rb = ring_buffer_get(output_event);
6407 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006408 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006409 }
6410
Frederic Weisbecker76369132011-05-19 19:55:04 +02006411 old_rb = event->rb;
6412 rcu_assign_pointer(event->rb, rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006413 if (old_rb)
6414 ring_buffer_detach(event, old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006415 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006416unlock:
6417 mutex_unlock(&event->mmap_mutex);
6418
Frederic Weisbecker76369132011-05-19 19:55:04 +02006419 if (old_rb)
6420 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422 return ret;
6423}
6424
6425/**
6426 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6427 *
6428 * @attr_uptr: event_id type attributes for monitoring/sampling
6429 * @pid: target pid
6430 * @cpu: target cpu
6431 * @group_fd: group leader event fd
6432 */
6433SYSCALL_DEFINE5(perf_event_open,
6434 struct perf_event_attr __user *, attr_uptr,
6435 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6436{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006437 struct perf_event *group_leader = NULL, *output_event = NULL;
6438 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006439 struct perf_event_attr attr;
6440 struct perf_event_context *ctx;
6441 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006442 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006443 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006444 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006445 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006446 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447 int err;
6448
6449 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006450 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006451 return -EINVAL;
6452
6453 err = perf_copy_attr(attr_uptr, &attr);
6454 if (err)
6455 return err;
6456
6457 if (!attr.exclude_kernel) {
6458 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6459 return -EACCES;
6460 }
6461
6462 if (attr.freq) {
6463 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6464 return -EINVAL;
6465 }
6466
Stephane Eraniane5d13672011-02-14 11:20:01 +02006467 /*
6468 * In cgroup mode, the pid argument is used to pass the fd
6469 * opened to the cgroup directory in cgroupfs. The cpu argument
6470 * designates the cpu on which to monitor threads from that
6471 * cgroup.
6472 */
6473 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6474 return -EINVAL;
6475
Al Viroab72a702012-08-21 09:40:46 -04006476 event_fd = get_unused_fd();
Al Viroea635c62010-05-26 17:40:29 -04006477 if (event_fd < 0)
6478 return event_fd;
6479
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006480 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04006481 err = perf_fget_light(group_fd, &group);
6482 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006483 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04006484 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006485 if (flags & PERF_FLAG_FD_OUTPUT)
6486 output_event = group_leader;
6487 if (flags & PERF_FLAG_FD_NO_GROUP)
6488 group_leader = NULL;
6489 }
6490
Stephane Eraniane5d13672011-02-14 11:20:01 +02006491 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006492 task = find_lively_task_by_vpid(pid);
6493 if (IS_ERR(task)) {
6494 err = PTR_ERR(task);
6495 goto err_group_fd;
6496 }
6497 }
6498
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006499 get_online_cpus();
6500
Avi Kivity4dc0da82011-06-29 18:42:35 +03006501 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6502 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006503 if (IS_ERR(event)) {
6504 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006505 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006506 }
6507
Stephane Eraniane5d13672011-02-14 11:20:01 +02006508 if (flags & PERF_FLAG_PID_CGROUP) {
6509 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6510 if (err)
6511 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006512 /*
6513 * one more event:
6514 * - that has cgroup constraint on event->cpu
6515 * - that may need work on context switch
6516 */
6517 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006518 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006519 }
6520
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006521 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006522 * Special case software events and allow them to be part of
6523 * any hardware group.
6524 */
6525 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006526
6527 if (group_leader &&
6528 (is_software_event(event) != is_software_event(group_leader))) {
6529 if (is_software_event(event)) {
6530 /*
6531 * If event and group_leader are not both a software
6532 * event, and event is, then group leader is not.
6533 *
6534 * Allow the addition of software events to !software
6535 * groups, this is safe because software events never
6536 * fail to schedule.
6537 */
6538 pmu = group_leader->pmu;
6539 } else if (is_software_event(group_leader) &&
6540 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6541 /*
6542 * In case the group is a pure software group, and we
6543 * try to add a hardware event, move the whole group to
6544 * the hardware context.
6545 */
6546 move_group = 1;
6547 }
6548 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006549
6550 /*
6551 * Get the target context (task or percpu):
6552 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006553 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006554 if (IS_ERR(ctx)) {
6555 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006556 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006557 }
6558
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006559 if (task) {
6560 put_task_struct(task);
6561 task = NULL;
6562 }
6563
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006564 /*
6565 * Look up the group leader (we will attach this event to it):
6566 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006567 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006568 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006569
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006570 /*
6571 * Do not allow a recursive hierarchy (this new sibling
6572 * becoming part of another group-sibling):
6573 */
6574 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006575 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006576 /*
6577 * Do not allow to attach to a group in a different
6578 * task or CPU context:
6579 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006580 if (move_group) {
6581 if (group_leader->ctx->type != ctx->type)
6582 goto err_context;
6583 } else {
6584 if (group_leader->ctx != ctx)
6585 goto err_context;
6586 }
6587
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006588 /*
6589 * Only a group leader can be exclusive or pinned
6590 */
6591 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006592 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006593 }
6594
6595 if (output_event) {
6596 err = perf_event_set_output(event, output_event);
6597 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006598 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006599 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600
Al Viroea635c62010-05-26 17:40:29 -04006601 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6602 if (IS_ERR(event_file)) {
6603 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006604 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006605 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006606
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006607 if (move_group) {
6608 struct perf_event_context *gctx = group_leader->ctx;
6609
6610 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006611 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006612 list_for_each_entry(sibling, &group_leader->sibling_list,
6613 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006614 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006615 put_ctx(gctx);
6616 }
6617 mutex_unlock(&gctx->mutex);
6618 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006619 }
6620
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006621 WARN_ON_ONCE(ctx->parent_ctx);
6622 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006623
6624 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006625 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006626 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006627 get_ctx(ctx);
6628 list_for_each_entry(sibling, &group_leader->sibling_list,
6629 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006630 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006631 get_ctx(ctx);
6632 }
6633 }
6634
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006635 perf_install_in_context(ctx, event, event->cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006636 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006637 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638 mutex_unlock(&ctx->mutex);
6639
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006640 put_online_cpus();
6641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006642 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006643
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006644 mutex_lock(&current->perf_event_mutex);
6645 list_add_tail(&event->owner_entry, &current->perf_event_list);
6646 mutex_unlock(&current->perf_event_mutex);
6647
Peter Zijlstra8a495422010-05-27 15:47:49 +02006648 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006649 * Precalculate sample_data sizes
6650 */
6651 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006652 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006653
6654 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006655 * Drop the reference on the group_event after placing the
6656 * new event on the sibling_list. This ensures destruction
6657 * of the group leader will find the pointer to itself in
6658 * perf_group_detach().
6659 */
Al Viro2903ff02012-08-28 12:52:22 -04006660 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006661 fd_install(event_fd, event_file);
6662 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006663
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006664err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006665 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006666 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006667err_alloc:
6668 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006669err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006670 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006671 if (task)
6672 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006673err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04006674 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006675err_fd:
6676 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677 return err;
6678}
6679
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006680/**
6681 * perf_event_create_kernel_counter
6682 *
6683 * @attr: attributes of the counter to create
6684 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006685 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006686 */
6687struct perf_event *
6688perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006689 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006690 perf_overflow_handler_t overflow_handler,
6691 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006692{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006693 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006694 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006695 int err;
6696
6697 /*
6698 * Get the target context (task or percpu):
6699 */
6700
Avi Kivity4dc0da82011-06-29 18:42:35 +03006701 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6702 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006703 if (IS_ERR(event)) {
6704 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006705 goto err;
6706 }
6707
Matt Helsley38a81da2010-09-13 13:01:20 -07006708 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006709 if (IS_ERR(ctx)) {
6710 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006711 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006712 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006713
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006714 WARN_ON_ONCE(ctx->parent_ctx);
6715 mutex_lock(&ctx->mutex);
6716 perf_install_in_context(ctx, event, cpu);
6717 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006718 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006719 mutex_unlock(&ctx->mutex);
6720
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006721 return event;
6722
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006723err_free:
6724 free_event(event);
6725err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006726 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006727}
6728EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6729
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006730void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
6731{
6732 struct perf_event_context *src_ctx;
6733 struct perf_event_context *dst_ctx;
6734 struct perf_event *event, *tmp;
6735 LIST_HEAD(events);
6736
6737 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
6738 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
6739
6740 mutex_lock(&src_ctx->mutex);
6741 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
6742 event_entry) {
6743 perf_remove_from_context(event);
6744 put_ctx(src_ctx);
6745 list_add(&event->event_entry, &events);
6746 }
6747 mutex_unlock(&src_ctx->mutex);
6748
6749 synchronize_rcu();
6750
6751 mutex_lock(&dst_ctx->mutex);
6752 list_for_each_entry_safe(event, tmp, &events, event_entry) {
6753 list_del(&event->event_entry);
6754 if (event->state >= PERF_EVENT_STATE_OFF)
6755 event->state = PERF_EVENT_STATE_INACTIVE;
6756 perf_install_in_context(dst_ctx, event, dst_cpu);
6757 get_ctx(dst_ctx);
6758 }
6759 mutex_unlock(&dst_ctx->mutex);
6760}
6761EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
6762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006763static void sync_child_event(struct perf_event *child_event,
6764 struct task_struct *child)
6765{
6766 struct perf_event *parent_event = child_event->parent;
6767 u64 child_val;
6768
6769 if (child_event->attr.inherit_stat)
6770 perf_event_read_event(child_event, child);
6771
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006772 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006773
6774 /*
6775 * Add back the child's count to the parent's count:
6776 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006777 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006778 atomic64_add(child_event->total_time_enabled,
6779 &parent_event->child_total_time_enabled);
6780 atomic64_add(child_event->total_time_running,
6781 &parent_event->child_total_time_running);
6782
6783 /*
6784 * Remove this event from the parent's list
6785 */
6786 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6787 mutex_lock(&parent_event->child_mutex);
6788 list_del_init(&child_event->child_list);
6789 mutex_unlock(&parent_event->child_mutex);
6790
6791 /*
6792 * Release the parent event, if this was the last
6793 * reference to it.
6794 */
Al Viroa6fa9412012-08-20 14:59:25 +01006795 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006796}
6797
6798static void
6799__perf_event_exit_task(struct perf_event *child_event,
6800 struct perf_event_context *child_ctx,
6801 struct task_struct *child)
6802{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006803 if (child_event->parent) {
6804 raw_spin_lock_irq(&child_ctx->lock);
6805 perf_group_detach(child_event);
6806 raw_spin_unlock_irq(&child_ctx->lock);
6807 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006808
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006809 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006811 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006812 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006813 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006814 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006815 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006816 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006817 sync_child_event(child_event, child);
6818 free_event(child_event);
6819 }
6820}
6821
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006822static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006823{
6824 struct perf_event *child_event, *tmp;
6825 struct perf_event_context *child_ctx;
6826 unsigned long flags;
6827
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006828 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006829 perf_event_task(child, NULL, 0);
6830 return;
6831 }
6832
6833 local_irq_save(flags);
6834 /*
6835 * We can't reschedule here because interrupts are disabled,
6836 * and either child is current or it is a task that can't be
6837 * scheduled, so we are now safe from rescheduling changing
6838 * our context.
6839 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006840 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006841
6842 /*
6843 * Take the context lock here so that if find_get_context is
6844 * reading child->perf_event_ctxp, we wait until it has
6845 * incremented the context's refcount before we do put_ctx below.
6846 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006847 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006848 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006849 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006850 /*
6851 * If this context is a clone; unclone it so it can't get
6852 * swapped to another process while we're removing all
6853 * the events from it.
6854 */
6855 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006856 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006857 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006858
6859 /*
6860 * Report the task dead after unscheduling the events so that we
6861 * won't get any samples after PERF_RECORD_EXIT. We can however still
6862 * get a few PERF_RECORD_READ events.
6863 */
6864 perf_event_task(child, child_ctx, 0);
6865
6866 /*
6867 * We can recurse on the same lock type through:
6868 *
6869 * __perf_event_exit_task()
6870 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01006871 * put_event()
6872 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006873 *
6874 * But since its the parent context it won't be the same instance.
6875 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006876 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006877
6878again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006879 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6880 group_entry)
6881 __perf_event_exit_task(child_event, child_ctx, child);
6882
6883 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884 group_entry)
6885 __perf_event_exit_task(child_event, child_ctx, child);
6886
6887 /*
6888 * If the last event was a group event, it will have appended all
6889 * its siblings to the list, but we obtained 'tmp' before that which
6890 * will still point to the list head terminating the iteration.
6891 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006892 if (!list_empty(&child_ctx->pinned_groups) ||
6893 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006894 goto again;
6895
6896 mutex_unlock(&child_ctx->mutex);
6897
6898 put_ctx(child_ctx);
6899}
6900
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006901/*
6902 * When a child task exits, feed back event values to parent events.
6903 */
6904void perf_event_exit_task(struct task_struct *child)
6905{
Peter Zijlstra88821352010-11-09 19:01:43 +01006906 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006907 int ctxn;
6908
Peter Zijlstra88821352010-11-09 19:01:43 +01006909 mutex_lock(&child->perf_event_mutex);
6910 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6911 owner_entry) {
6912 list_del_init(&event->owner_entry);
6913
6914 /*
6915 * Ensure the list deletion is visible before we clear
6916 * the owner, closes a race against perf_release() where
6917 * we need to serialize on the owner->perf_event_mutex.
6918 */
6919 smp_wmb();
6920 event->owner = NULL;
6921 }
6922 mutex_unlock(&child->perf_event_mutex);
6923
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006924 for_each_task_context_nr(ctxn)
6925 perf_event_exit_task_context(child, ctxn);
6926}
6927
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006928static void perf_free_event(struct perf_event *event,
6929 struct perf_event_context *ctx)
6930{
6931 struct perf_event *parent = event->parent;
6932
6933 if (WARN_ON_ONCE(!parent))
6934 return;
6935
6936 mutex_lock(&parent->child_mutex);
6937 list_del_init(&event->child_list);
6938 mutex_unlock(&parent->child_mutex);
6939
Al Viroa6fa9412012-08-20 14:59:25 +01006940 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006941
Peter Zijlstra8a495422010-05-27 15:47:49 +02006942 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006943 list_del_event(event, ctx);
6944 free_event(event);
6945}
6946
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006947/*
6948 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006949 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 */
6951void perf_event_free_task(struct task_struct *task)
6952{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006953 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006954 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006955 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006956
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006957 for_each_task_context_nr(ctxn) {
6958 ctx = task->perf_event_ctxp[ctxn];
6959 if (!ctx)
6960 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006961
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006962 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006963again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006964 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6965 group_entry)
6966 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006967
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006968 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6969 group_entry)
6970 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006971
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006972 if (!list_empty(&ctx->pinned_groups) ||
6973 !list_empty(&ctx->flexible_groups))
6974 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006975
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006976 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006977
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006978 put_ctx(ctx);
6979 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006980}
6981
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006982void perf_event_delayed_put(struct task_struct *task)
6983{
6984 int ctxn;
6985
6986 for_each_task_context_nr(ctxn)
6987 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6988}
6989
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006990/*
6991 * inherit a event from parent task to child task:
6992 */
6993static struct perf_event *
6994inherit_event(struct perf_event *parent_event,
6995 struct task_struct *parent,
6996 struct perf_event_context *parent_ctx,
6997 struct task_struct *child,
6998 struct perf_event *group_leader,
6999 struct perf_event_context *child_ctx)
7000{
7001 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007002 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007003
7004 /*
7005 * Instead of creating recursive hierarchies of events,
7006 * we link inherited events back to the original parent,
7007 * which has a filp for sure, which we use as the reference
7008 * count:
7009 */
7010 if (parent_event->parent)
7011 parent_event = parent_event->parent;
7012
7013 child_event = perf_event_alloc(&parent_event->attr,
7014 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007015 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007016 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007017 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007018 if (IS_ERR(child_event))
7019 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007020
7021 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7022 free_event(child_event);
7023 return NULL;
7024 }
7025
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007026 get_ctx(child_ctx);
7027
7028 /*
7029 * Make the child state follow the state of the parent event,
7030 * not its attr.disabled bit. We hold the parent's mutex,
7031 * so we won't race with perf_event_{en, dis}able_family.
7032 */
7033 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7034 child_event->state = PERF_EVENT_STATE_INACTIVE;
7035 else
7036 child_event->state = PERF_EVENT_STATE_OFF;
7037
7038 if (parent_event->attr.freq) {
7039 u64 sample_period = parent_event->hw.sample_period;
7040 struct hw_perf_event *hwc = &child_event->hw;
7041
7042 hwc->sample_period = sample_period;
7043 hwc->last_period = sample_period;
7044
7045 local64_set(&hwc->period_left, sample_period);
7046 }
7047
7048 child_event->ctx = child_ctx;
7049 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007050 child_event->overflow_handler_context
7051 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007052
7053 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007054 * Precalculate sample_data sizes
7055 */
7056 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007057 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007058
7059 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007060 * Link it up in the child's context:
7061 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007062 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007063 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007064 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007065
7066 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007067 * Link this into the parent event's child list
7068 */
7069 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7070 mutex_lock(&parent_event->child_mutex);
7071 list_add_tail(&child_event->child_list, &parent_event->child_list);
7072 mutex_unlock(&parent_event->child_mutex);
7073
7074 return child_event;
7075}
7076
7077static int inherit_group(struct perf_event *parent_event,
7078 struct task_struct *parent,
7079 struct perf_event_context *parent_ctx,
7080 struct task_struct *child,
7081 struct perf_event_context *child_ctx)
7082{
7083 struct perf_event *leader;
7084 struct perf_event *sub;
7085 struct perf_event *child_ctr;
7086
7087 leader = inherit_event(parent_event, parent, parent_ctx,
7088 child, NULL, child_ctx);
7089 if (IS_ERR(leader))
7090 return PTR_ERR(leader);
7091 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7092 child_ctr = inherit_event(sub, parent, parent_ctx,
7093 child, leader, child_ctx);
7094 if (IS_ERR(child_ctr))
7095 return PTR_ERR(child_ctr);
7096 }
7097 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007098}
7099
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007100static int
7101inherit_task_group(struct perf_event *event, struct task_struct *parent,
7102 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007103 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007104 int *inherited_all)
7105{
7106 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007107 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007108
7109 if (!event->attr.inherit) {
7110 *inherited_all = 0;
7111 return 0;
7112 }
7113
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007114 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007115 if (!child_ctx) {
7116 /*
7117 * This is executed from the parent task context, so
7118 * inherit events that have been marked for cloning.
7119 * First allocate and initialize a context for the
7120 * child.
7121 */
7122
Peter Zijlstraeb184472010-09-07 15:55:13 +02007123 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007124 if (!child_ctx)
7125 return -ENOMEM;
7126
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007127 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007128 }
7129
7130 ret = inherit_group(event, parent, parent_ctx,
7131 child, child_ctx);
7132
7133 if (ret)
7134 *inherited_all = 0;
7135
7136 return ret;
7137}
7138
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007139/*
7140 * Initialize the perf_event context in task_struct
7141 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007142int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007144 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007145 struct perf_event_context *cloned_ctx;
7146 struct perf_event *event;
7147 struct task_struct *parent = current;
7148 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007149 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007150 int ret = 0;
7151
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007152 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007153 return 0;
7154
7155 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156 * If the parent's context is a clone, pin it so it won't get
7157 * swapped under us.
7158 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007159 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007160
7161 /*
7162 * No need to check if parent_ctx != NULL here; since we saw
7163 * it non-NULL earlier, the only reason for it to become NULL
7164 * is if we exit, and since we're currently in the middle of
7165 * a fork we can't be exiting at the same time.
7166 */
7167
7168 /*
7169 * Lock the parent list. No need to lock the child - not PID
7170 * hashed yet and not running, so nobody can access it.
7171 */
7172 mutex_lock(&parent_ctx->mutex);
7173
7174 /*
7175 * We dont have to disable NMIs - we are only looking at
7176 * the list, not manipulating it:
7177 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007178 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007179 ret = inherit_task_group(event, parent, parent_ctx,
7180 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007181 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007182 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007183 }
7184
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007185 /*
7186 * We can't hold ctx->lock when iterating the ->flexible_group list due
7187 * to allocations, but we need to prevent rotation because
7188 * rotate_ctx() will change the list from interrupt context.
7189 */
7190 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7191 parent_ctx->rotate_disable = 1;
7192 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7193
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007194 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007195 ret = inherit_task_group(event, parent, parent_ctx,
7196 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007197 if (ret)
7198 break;
7199 }
7200
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007201 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7202 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007203
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007204 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007205
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007206 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007207 /*
7208 * Mark the child context as a clone of the parent
7209 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007210 *
7211 * Note that if the parent is a clone, the holding of
7212 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007213 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007214 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007215 if (cloned_ctx) {
7216 child_ctx->parent_ctx = cloned_ctx;
7217 child_ctx->parent_gen = parent_ctx->parent_gen;
7218 } else {
7219 child_ctx->parent_ctx = parent_ctx;
7220 child_ctx->parent_gen = parent_ctx->generation;
7221 }
7222 get_ctx(child_ctx->parent_ctx);
7223 }
7224
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007225 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007226 mutex_unlock(&parent_ctx->mutex);
7227
7228 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007229 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007230
7231 return ret;
7232}
7233
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007234/*
7235 * Initialize the perf_event context in task_struct
7236 */
7237int perf_event_init_task(struct task_struct *child)
7238{
7239 int ctxn, ret;
7240
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007241 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7242 mutex_init(&child->perf_event_mutex);
7243 INIT_LIST_HEAD(&child->perf_event_list);
7244
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007245 for_each_task_context_nr(ctxn) {
7246 ret = perf_event_init_context(child, ctxn);
7247 if (ret)
7248 return ret;
7249 }
7250
7251 return 0;
7252}
7253
Paul Mackerras220b1402010-03-10 20:45:52 +11007254static void __init perf_event_init_all_cpus(void)
7255{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007256 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007257 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007258
7259 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007260 swhash = &per_cpu(swevent_htable, cpu);
7261 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007262 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007263 }
7264}
7265
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266static void __cpuinit perf_event_init_cpu(int cpu)
7267{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007268 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007269
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007270 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007271 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007272 struct swevent_hlist *hlist;
7273
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007274 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7275 WARN_ON(!hlist);
7276 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007277 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007278 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279}
7280
Peter Zijlstrac2774432010-12-08 15:29:02 +01007281#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007282static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007283{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007284 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7285
7286 WARN_ON(!irqs_disabled());
7287
7288 list_del_init(&cpuctx->rotation_list);
7289}
7290
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007291static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007292{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007293 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007294 struct perf_event *event, *tmp;
7295
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007296 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007297
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007298 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007299 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007300 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007301 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007302}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007303
7304static void perf_event_exit_cpu_context(int cpu)
7305{
7306 struct perf_event_context *ctx;
7307 struct pmu *pmu;
7308 int idx;
7309
7310 idx = srcu_read_lock(&pmus_srcu);
7311 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007312 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007313
7314 mutex_lock(&ctx->mutex);
7315 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7316 mutex_unlock(&ctx->mutex);
7317 }
7318 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007319}
7320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007321static void perf_event_exit_cpu(int cpu)
7322{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007323 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007324
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007325 mutex_lock(&swhash->hlist_mutex);
7326 swevent_hlist_release(swhash);
7327 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007328
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007329 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007330}
7331#else
7332static inline void perf_event_exit_cpu(int cpu) { }
7333#endif
7334
Peter Zijlstrac2774432010-12-08 15:29:02 +01007335static int
7336perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7337{
7338 int cpu;
7339
7340 for_each_online_cpu(cpu)
7341 perf_event_exit_cpu(cpu);
7342
7343 return NOTIFY_OK;
7344}
7345
7346/*
7347 * Run the perf reboot notifier at the very last possible moment so that
7348 * the generic watchdog code runs as long as possible.
7349 */
7350static struct notifier_block perf_reboot_notifier = {
7351 .notifier_call = perf_reboot,
7352 .priority = INT_MIN,
7353};
7354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007355static int __cpuinit
7356perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7357{
7358 unsigned int cpu = (long)hcpu;
7359
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007360 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007361
7362 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007363 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007364 perf_event_init_cpu(cpu);
7365 break;
7366
Peter Zijlstra5e116372010-06-11 13:35:08 +02007367 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007368 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007369 perf_event_exit_cpu(cpu);
7370 break;
7371
7372 default:
7373 break;
7374 }
7375
7376 return NOTIFY_OK;
7377}
7378
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007379void __init perf_event_init(void)
7380{
Jason Wessel3c502e72010-11-04 17:33:01 -05007381 int ret;
7382
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007383 idr_init(&pmu_idr);
7384
Paul Mackerras220b1402010-03-10 20:45:52 +11007385 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007386 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007387 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7388 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7389 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007390 perf_tp_register();
7391 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007392 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007393
7394 ret = init_hw_breakpoint();
7395 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007396
7397 /* do not patch jump label more than once per second */
7398 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007399
7400 /*
7401 * Build time assertion that we keep the data_head at the intended
7402 * location. IOW, validation we got the __reserved[] size right.
7403 */
7404 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7405 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007406}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007407
7408static int __init perf_event_sysfs_init(void)
7409{
7410 struct pmu *pmu;
7411 int ret;
7412
7413 mutex_lock(&pmus_lock);
7414
7415 ret = bus_register(&pmu_bus);
7416 if (ret)
7417 goto unlock;
7418
7419 list_for_each_entry(pmu, &pmus, entry) {
7420 if (!pmu->name || pmu->type < 0)
7421 continue;
7422
7423 ret = pmu_dev_alloc(pmu);
7424 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7425 }
7426 pmu_bus_running = 1;
7427 ret = 0;
7428
7429unlock:
7430 mutex_unlock(&pmus_lock);
7431
7432 return ret;
7433}
7434device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007435
7436#ifdef CONFIG_CGROUP_PERF
Tejun Heo92fb9742012-11-19 08:13:38 -08007437static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007438{
7439 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007440
Li Zefan1b15d052011-03-03 14:26:06 +08007441 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007442 if (!jc)
7443 return ERR_PTR(-ENOMEM);
7444
Stephane Eraniane5d13672011-02-14 11:20:01 +02007445 jc->info = alloc_percpu(struct perf_cgroup_info);
7446 if (!jc->info) {
7447 kfree(jc);
7448 return ERR_PTR(-ENOMEM);
7449 }
7450
Stephane Eraniane5d13672011-02-14 11:20:01 +02007451 return &jc->css;
7452}
7453
Tejun Heo92fb9742012-11-19 08:13:38 -08007454static void perf_cgroup_css_free(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007455{
7456 struct perf_cgroup *jc;
7457 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7458 struct perf_cgroup, css);
7459 free_percpu(jc->info);
7460 kfree(jc);
7461}
7462
7463static int __perf_cgroup_move(void *info)
7464{
7465 struct task_struct *task = info;
7466 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7467 return 0;
7468}
7469
Li Zefan761b3ef2012-01-31 13:47:36 +08007470static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007471{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007472 struct task_struct *task;
7473
7474 cgroup_taskset_for_each(task, cgrp, tset)
7475 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007476}
7477
Li Zefan761b3ef2012-01-31 13:47:36 +08007478static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7479 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007480{
7481 /*
7482 * cgroup_exit() is called in the copy_process() failure path.
7483 * Ignore this case since the task hasn't ran yet, this avoids
7484 * trying to poke a half freed task state from generic code.
7485 */
7486 if (!(task->flags & PF_EXITING))
7487 return;
7488
Tejun Heobb9d97b2011-12-12 18:12:21 -08007489 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007490}
7491
7492struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007493 .name = "perf_event",
7494 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007495 .css_alloc = perf_cgroup_css_alloc,
7496 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007497 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007498 .attach = perf_cgroup_attach,
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07007499
7500 /*
7501 * perf_event cgroup doesn't handle nesting correctly.
7502 * ctx->nr_cgroups adjustments should be propagated through the
7503 * cgroup hierarchy. Fix it and remove the following.
7504 */
7505 .broken_hierarchy = true,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007506};
7507#endif /* CONFIG_CGROUP_PERF */