blob: bd9c5bca42ae0f70c95aae6a1584bbf8551abe83 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020039
Frederic Weisbecker76369132011-05-19 19:55:04 +020040#include "internal.h"
41
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042#include <asm/irq_regs.h>
43
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010044struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020045 struct task_struct *p;
46 int (*func)(void *info);
47 void *info;
48 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010049};
50
51static void remote_function(void *data)
52{
53 struct remote_function_call *tfc = data;
54 struct task_struct *p = tfc->p;
55
56 if (p) {
57 tfc->ret = -EAGAIN;
58 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
59 return;
60 }
61
62 tfc->ret = tfc->func(tfc->info);
63}
64
65/**
66 * task_function_call - call a function on the cpu on which a task runs
67 * @p: the task to evaluate
68 * @func: the function to be called
69 * @info: the function call argument
70 *
71 * Calls the function @func when the task is currently running. This might
72 * be on the current CPU, which just calls the function directly
73 *
74 * returns: @func return value, or
75 * -ESRCH - when the process isn't running
76 * -EAGAIN - when the process moved away
77 */
78static int
79task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
80{
81 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020082 .p = p,
83 .func = func,
84 .info = info,
85 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010086 };
87
88 if (task_curr(p))
89 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
90
91 return data.ret;
92}
93
94/**
95 * cpu_function_call - call a function on the cpu
96 * @func: the function to be called
97 * @info: the function call argument
98 *
99 * Calls the function @func on the remote cpu.
100 *
101 * returns: @func return value or -ENXIO when the cpu is offline
102 */
103static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
104{
105 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200106 .p = NULL,
107 .func = func,
108 .info = info,
109 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100110 };
111
112 smp_call_function_single(cpu, remote_function, &data, 1);
113
114 return data.ret;
115}
116
Stephane Eraniane5d13672011-02-14 11:20:01 +0200117#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
118 PERF_FLAG_FD_OUTPUT |\
119 PERF_FLAG_PID_CGROUP)
120
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100121/*
122 * branch priv levels that need permission checks
123 */
124#define PERF_SAMPLE_BRANCH_PERM_PLM \
125 (PERF_SAMPLE_BRANCH_KERNEL |\
126 PERF_SAMPLE_BRANCH_HV)
127
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200128enum event_type_t {
129 EVENT_FLEXIBLE = 0x1,
130 EVENT_PINNED = 0x2,
131 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
132};
133
Stephane Eraniane5d13672011-02-14 11:20:01 +0200134/*
135 * perf_sched_events : >0 events exist
136 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
137 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100138struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200139static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100140static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200141
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200142static atomic_t nr_mmap_events __read_mostly;
143static atomic_t nr_comm_events __read_mostly;
144static atomic_t nr_task_events __read_mostly;
145
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200146static LIST_HEAD(pmus);
147static DEFINE_MUTEX(pmus_lock);
148static struct srcu_struct pmus_srcu;
149
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200150/*
151 * perf event paranoia level:
152 * -1 - not paranoid at all
153 * 0 - disallow raw tracepoint access for unpriv
154 * 1 - disallow cpu events for unpriv
155 * 2 - disallow kernel profiling for unpriv
156 */
157int sysctl_perf_event_paranoid __read_mostly = 1;
158
Frederic Weisbecker20443382011-03-31 03:33:29 +0200159/* Minimum for 512 kiB + 1 user control page */
160int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200161
162/*
163 * max perf event sample rate
164 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100165#define DEFAULT_MAX_SAMPLE_RATE 100000
166int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
167static int max_samples_per_tick __read_mostly =
168 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
169
170int perf_proc_update_handler(struct ctl_table *table, int write,
171 void __user *buffer, size_t *lenp,
172 loff_t *ppos)
173{
174 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
175
176 if (ret || !write)
177 return ret;
178
179 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
180
181 return 0;
182}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200183
184static atomic64_t perf_event_id;
185
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200186static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
187 enum event_type_t event_type);
188
189static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200190 enum event_type_t event_type,
191 struct task_struct *task);
192
193static void update_context_time(struct perf_event_context *ctx);
194static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200195
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100196static void ring_buffer_attach(struct perf_event *event,
197 struct ring_buffer *rb);
198
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200199void __weak perf_event_print_debug(void) { }
200
Matt Fleming84c79912010-10-03 21:41:13 +0100201extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200202{
Matt Fleming84c79912010-10-03 21:41:13 +0100203 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200204}
205
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200206static inline u64 perf_clock(void)
207{
208 return local_clock();
209}
210
Stephane Eraniane5d13672011-02-14 11:20:01 +0200211static inline struct perf_cpu_context *
212__get_cpu_context(struct perf_event_context *ctx)
213{
214 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
215}
216
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200217static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
218 struct perf_event_context *ctx)
219{
220 raw_spin_lock(&cpuctx->ctx.lock);
221 if (ctx)
222 raw_spin_lock(&ctx->lock);
223}
224
225static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
226 struct perf_event_context *ctx)
227{
228 if (ctx)
229 raw_spin_unlock(&ctx->lock);
230 raw_spin_unlock(&cpuctx->ctx.lock);
231}
232
Stephane Eraniane5d13672011-02-14 11:20:01 +0200233#ifdef CONFIG_CGROUP_PERF
234
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200235/*
236 * Must ensure cgroup is pinned (css_get) before calling
237 * this function. In other words, we cannot call this function
238 * if there is no cgroup event for the current CPU context.
239 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200240static inline struct perf_cgroup *
241perf_cgroup_from_task(struct task_struct *task)
242{
243 return container_of(task_subsys_state(task, perf_subsys_id),
244 struct perf_cgroup, css);
245}
246
247static inline bool
248perf_cgroup_match(struct perf_event *event)
249{
250 struct perf_event_context *ctx = event->ctx;
251 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
252
253 return !event->cgrp || event->cgrp == cpuctx->cgrp;
254}
255
Salman Qazi9c5da092012-06-14 15:31:09 -0700256static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200257{
Salman Qazi9c5da092012-06-14 15:31:09 -0700258 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200259}
260
261static inline void perf_put_cgroup(struct perf_event *event)
262{
263 css_put(&event->cgrp->css);
264}
265
266static inline void perf_detach_cgroup(struct perf_event *event)
267{
268 perf_put_cgroup(event);
269 event->cgrp = NULL;
270}
271
272static inline int is_cgroup_event(struct perf_event *event)
273{
274 return event->cgrp != NULL;
275}
276
277static inline u64 perf_cgroup_event_time(struct perf_event *event)
278{
279 struct perf_cgroup_info *t;
280
281 t = per_cpu_ptr(event->cgrp->info, event->cpu);
282 return t->time;
283}
284
285static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
286{
287 struct perf_cgroup_info *info;
288 u64 now;
289
290 now = perf_clock();
291
292 info = this_cpu_ptr(cgrp->info);
293
294 info->time += now - info->timestamp;
295 info->timestamp = now;
296}
297
298static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
299{
300 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
301 if (cgrp_out)
302 __update_cgrp_time(cgrp_out);
303}
304
305static inline void update_cgrp_time_from_event(struct perf_event *event)
306{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200307 struct perf_cgroup *cgrp;
308
Stephane Eraniane5d13672011-02-14 11:20:01 +0200309 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200310 * ensure we access cgroup data only when needed and
311 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200312 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200313 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200314 return;
315
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200316 cgrp = perf_cgroup_from_task(current);
317 /*
318 * Do not update time when cgroup is not active
319 */
320 if (cgrp == event->cgrp)
321 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200322}
323
324static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200325perf_cgroup_set_timestamp(struct task_struct *task,
326 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200327{
328 struct perf_cgroup *cgrp;
329 struct perf_cgroup_info *info;
330
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200331 /*
332 * ctx->lock held by caller
333 * ensure we do not access cgroup data
334 * unless we have the cgroup pinned (css_get)
335 */
336 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200337 return;
338
339 cgrp = perf_cgroup_from_task(task);
340 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200341 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342}
343
344#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
345#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
346
347/*
348 * reschedule events based on the cgroup constraint of task.
349 *
350 * mode SWOUT : schedule out everything
351 * mode SWIN : schedule in based on cgroup for next
352 */
353void perf_cgroup_switch(struct task_struct *task, int mode)
354{
355 struct perf_cpu_context *cpuctx;
356 struct pmu *pmu;
357 unsigned long flags;
358
359 /*
360 * disable interrupts to avoid geting nr_cgroup
361 * changes via __perf_event_disable(). Also
362 * avoids preemption.
363 */
364 local_irq_save(flags);
365
366 /*
367 * we reschedule only in the presence of cgroup
368 * constrained events.
369 */
370 rcu_read_lock();
371
372 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
374
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375 /*
376 * perf_cgroup_events says at least one
377 * context on this CPU has cgroup events.
378 *
379 * ctx->nr_cgroups reports the number of cgroup
380 * events for a context.
381 */
382 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200383 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
384 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200385
386 if (mode & PERF_CGROUP_SWOUT) {
387 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
388 /*
389 * must not be done before ctxswout due
390 * to event_filter_match() in event_sched_out()
391 */
392 cpuctx->cgrp = NULL;
393 }
394
395 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200396 WARN_ON_ONCE(cpuctx->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200397 /* set cgrp before ctxsw in to
398 * allow event_filter_match() to not
399 * have to pass task around
400 */
401 cpuctx->cgrp = perf_cgroup_from_task(task);
402 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
403 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200404 perf_pmu_enable(cpuctx->ctx.pmu);
405 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200406 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200407 }
408
409 rcu_read_unlock();
410
411 local_irq_restore(flags);
412}
413
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200414static inline void perf_cgroup_sched_out(struct task_struct *task,
415 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200416{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200417 struct perf_cgroup *cgrp1;
418 struct perf_cgroup *cgrp2 = NULL;
419
420 /*
421 * we come here when we know perf_cgroup_events > 0
422 */
423 cgrp1 = perf_cgroup_from_task(task);
424
425 /*
426 * next is NULL when called from perf_event_enable_on_exec()
427 * that will systematically cause a cgroup_switch()
428 */
429 if (next)
430 cgrp2 = perf_cgroup_from_task(next);
431
432 /*
433 * only schedule out current cgroup events if we know
434 * that we are switching to a different cgroup. Otherwise,
435 * do no touch the cgroup events.
436 */
437 if (cgrp1 != cgrp2)
438 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200439}
440
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200441static inline void perf_cgroup_sched_in(struct task_struct *prev,
442 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200444 struct perf_cgroup *cgrp1;
445 struct perf_cgroup *cgrp2 = NULL;
446
447 /*
448 * we come here when we know perf_cgroup_events > 0
449 */
450 cgrp1 = perf_cgroup_from_task(task);
451
452 /* prev can never be NULL */
453 cgrp2 = perf_cgroup_from_task(prev);
454
455 /*
456 * only need to schedule in cgroup events if we are changing
457 * cgroup during ctxsw. Cgroup events were not scheduled
458 * out of ctxsw out if that was not the case.
459 */
460 if (cgrp1 != cgrp2)
461 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200462}
463
464static inline int perf_cgroup_connect(int fd, struct perf_event *event,
465 struct perf_event_attr *attr,
466 struct perf_event *group_leader)
467{
468 struct perf_cgroup *cgrp;
469 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400470 struct fd f = fdget(fd);
471 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200472
Al Viro2903ff02012-08-28 12:52:22 -0400473 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200474 return -EBADF;
475
Al Viro2903ff02012-08-28 12:52:22 -0400476 css = cgroup_css_from_dir(f.file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800477 if (IS_ERR(css)) {
478 ret = PTR_ERR(css);
479 goto out;
480 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200481
482 cgrp = container_of(css, struct perf_cgroup, css);
483 event->cgrp = cgrp;
484
Li Zefanf75e18c2011-03-03 14:25:50 +0800485 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700486 if (!perf_tryget_cgroup(event)) {
487 event->cgrp = NULL;
488 ret = -ENOENT;
489 goto out;
490 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800491
Stephane Eraniane5d13672011-02-14 11:20:01 +0200492 /*
493 * all events in a group must monitor
494 * the same cgroup because a task belongs
495 * to only one perf cgroup at a time
496 */
497 if (group_leader && group_leader->cgrp != cgrp) {
498 perf_detach_cgroup(event);
499 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200500 }
Li Zefan3db272c2011-03-03 14:25:37 +0800501out:
Al Viro2903ff02012-08-28 12:52:22 -0400502 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200503 return ret;
504}
505
506static inline void
507perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
508{
509 struct perf_cgroup_info *t;
510 t = per_cpu_ptr(event->cgrp->info, event->cpu);
511 event->shadow_ctx_time = now - t->timestamp;
512}
513
514static inline void
515perf_cgroup_defer_enabled(struct perf_event *event)
516{
517 /*
518 * when the current task's perf cgroup does not match
519 * the event's, we need to remember to call the
520 * perf_mark_enable() function the first time a task with
521 * a matching perf cgroup is scheduled in.
522 */
523 if (is_cgroup_event(event) && !perf_cgroup_match(event))
524 event->cgrp_defer_enabled = 1;
525}
526
527static inline void
528perf_cgroup_mark_enabled(struct perf_event *event,
529 struct perf_event_context *ctx)
530{
531 struct perf_event *sub;
532 u64 tstamp = perf_event_time(event);
533
534 if (!event->cgrp_defer_enabled)
535 return;
536
537 event->cgrp_defer_enabled = 0;
538
539 event->tstamp_enabled = tstamp - event->total_time_enabled;
540 list_for_each_entry(sub, &event->sibling_list, group_entry) {
541 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
542 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
543 sub->cgrp_defer_enabled = 0;
544 }
545 }
546}
547#else /* !CONFIG_CGROUP_PERF */
548
549static inline bool
550perf_cgroup_match(struct perf_event *event)
551{
552 return true;
553}
554
555static inline void perf_detach_cgroup(struct perf_event *event)
556{}
557
558static inline int is_cgroup_event(struct perf_event *event)
559{
560 return 0;
561}
562
563static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
564{
565 return 0;
566}
567
568static inline void update_cgrp_time_from_event(struct perf_event *event)
569{
570}
571
572static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
573{
574}
575
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200576static inline void perf_cgroup_sched_out(struct task_struct *task,
577 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200578{
579}
580
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200581static inline void perf_cgroup_sched_in(struct task_struct *prev,
582 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200583{
584}
585
586static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
587 struct perf_event_attr *attr,
588 struct perf_event *group_leader)
589{
590 return -EINVAL;
591}
592
593static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200594perf_cgroup_set_timestamp(struct task_struct *task,
595 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200596{
597}
598
599void
600perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
601{
602}
603
604static inline void
605perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
606{
607}
608
609static inline u64 perf_cgroup_event_time(struct perf_event *event)
610{
611 return 0;
612}
613
614static inline void
615perf_cgroup_defer_enabled(struct perf_event *event)
616{
617}
618
619static inline void
620perf_cgroup_mark_enabled(struct perf_event *event,
621 struct perf_event_context *ctx)
622{
623}
624#endif
625
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200626void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200627{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200628 int *count = this_cpu_ptr(pmu->pmu_disable_count);
629 if (!(*count)++)
630 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631}
632
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200633void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200634{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200635 int *count = this_cpu_ptr(pmu->pmu_disable_count);
636 if (!--(*count))
637 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200638}
639
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200640static DEFINE_PER_CPU(struct list_head, rotation_list);
641
642/*
643 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
644 * because they're strictly cpu affine and rotate_start is called with IRQs
645 * disabled, while rotate_context is called from IRQ context.
646 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200647static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200648{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200649 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200650 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200651
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200652 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200653
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200654 if (list_empty(&cpuctx->rotation_list))
655 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656}
657
658static void get_ctx(struct perf_event_context *ctx)
659{
660 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
661}
662
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200663static void put_ctx(struct perf_event_context *ctx)
664{
665 if (atomic_dec_and_test(&ctx->refcount)) {
666 if (ctx->parent_ctx)
667 put_ctx(ctx->parent_ctx);
668 if (ctx->task)
669 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800670 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200671 }
672}
673
674static void unclone_ctx(struct perf_event_context *ctx)
675{
676 if (ctx->parent_ctx) {
677 put_ctx(ctx->parent_ctx);
678 ctx->parent_ctx = NULL;
679 }
680}
681
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200682static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
683{
684 /*
685 * only top level events have the pid namespace they were created in
686 */
687 if (event->parent)
688 event = event->parent;
689
690 return task_tgid_nr_ns(p, event->ns);
691}
692
693static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
694{
695 /*
696 * only top level events have the pid namespace they were created in
697 */
698 if (event->parent)
699 event = event->parent;
700
701 return task_pid_nr_ns(p, event->ns);
702}
703
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200704/*
705 * If we inherit events we want to return the parent event id
706 * to userspace.
707 */
708static u64 primary_event_id(struct perf_event *event)
709{
710 u64 id = event->id;
711
712 if (event->parent)
713 id = event->parent->id;
714
715 return id;
716}
717
718/*
719 * Get the perf_event_context for a task and lock it.
720 * This has to cope with with the fact that until it is locked,
721 * the context could get moved to another task.
722 */
723static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200724perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200725{
726 struct perf_event_context *ctx;
727
728 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200729retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200730 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200731 if (ctx) {
732 /*
733 * If this context is a clone of another, it might
734 * get swapped for another underneath us by
735 * perf_event_task_sched_out, though the
736 * rcu_read_lock() protects us from any context
737 * getting freed. Lock the context and check if it
738 * got swapped before we could get the lock, and retry
739 * if so. If we locked the right context, then it
740 * can't get swapped on us any more.
741 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100742 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200743 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100744 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200745 goto retry;
746 }
747
748 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100749 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200750 ctx = NULL;
751 }
752 }
753 rcu_read_unlock();
754 return ctx;
755}
756
757/*
758 * Get the context for a task and increment its pin_count so it
759 * can't get swapped to another task. This also increments its
760 * reference count so that the context can't get freed.
761 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200762static struct perf_event_context *
763perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200764{
765 struct perf_event_context *ctx;
766 unsigned long flags;
767
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200768 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200769 if (ctx) {
770 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100771 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200772 }
773 return ctx;
774}
775
776static void perf_unpin_context(struct perf_event_context *ctx)
777{
778 unsigned long flags;
779
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100780 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200781 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100782 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783}
784
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100785/*
786 * Update the record of the current time in a context.
787 */
788static void update_context_time(struct perf_event_context *ctx)
789{
790 u64 now = perf_clock();
791
792 ctx->time += now - ctx->timestamp;
793 ctx->timestamp = now;
794}
795
Stephane Eranian41587552011-01-03 18:20:01 +0200796static u64 perf_event_time(struct perf_event *event)
797{
798 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200799
800 if (is_cgroup_event(event))
801 return perf_cgroup_event_time(event);
802
Stephane Eranian41587552011-01-03 18:20:01 +0200803 return ctx ? ctx->time : 0;
804}
805
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100806/*
807 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400808 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100809 */
810static void update_event_times(struct perf_event *event)
811{
812 struct perf_event_context *ctx = event->ctx;
813 u64 run_end;
814
815 if (event->state < PERF_EVENT_STATE_INACTIVE ||
816 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
817 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200818 /*
819 * in cgroup mode, time_enabled represents
820 * the time the event was enabled AND active
821 * tasks were in the monitored cgroup. This is
822 * independent of the activity of the context as
823 * there may be a mix of cgroup and non-cgroup events.
824 *
825 * That is why we treat cgroup events differently
826 * here.
827 */
828 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +0900829 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200830 else if (ctx->is_active)
831 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100832 else
833 run_end = event->tstamp_stopped;
834
835 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100836
837 if (event->state == PERF_EVENT_STATE_INACTIVE)
838 run_end = event->tstamp_stopped;
839 else
Stephane Eranian41587552011-01-03 18:20:01 +0200840 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100841
842 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200843
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100844}
845
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200846/*
847 * Update total_time_enabled and total_time_running for all events in a group.
848 */
849static void update_group_times(struct perf_event *leader)
850{
851 struct perf_event *event;
852
853 update_event_times(leader);
854 list_for_each_entry(event, &leader->sibling_list, group_entry)
855 update_event_times(event);
856}
857
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100858static struct list_head *
859ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
860{
861 if (event->attr.pinned)
862 return &ctx->pinned_groups;
863 else
864 return &ctx->flexible_groups;
865}
866
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867/*
868 * Add a event from the lists for its context.
869 * Must be called with ctx->mutex and ctx->lock held.
870 */
871static void
872list_add_event(struct perf_event *event, struct perf_event_context *ctx)
873{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200874 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
875 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200876
877 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200878 * If we're a stand alone event or group leader, we go to the context
879 * list, group events are kept attached to the group so that
880 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200881 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200882 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100883 struct list_head *list;
884
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100885 if (is_software_event(event))
886 event->group_flags |= PERF_GROUP_SOFTWARE;
887
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100888 list = ctx_group_list(event, ctx);
889 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890 }
891
Peter Zijlstra08309372011-03-03 11:31:20 +0100892 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200893 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200894
Stephane Eraniand010b332012-02-09 23:21:00 +0100895 if (has_branch_stack(event))
896 ctx->nr_branch_stack++;
897
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200898 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200899 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200900 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200901 ctx->nr_events++;
902 if (event->attr.inherit_stat)
903 ctx->nr_stat++;
904}
905
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200906/*
907 * Called at perf_event creation and when events are attached/detached from a
908 * group.
909 */
910static void perf_event__read_size(struct perf_event *event)
911{
912 int entry = sizeof(u64); /* value */
913 int size = 0;
914 int nr = 1;
915
916 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
917 size += sizeof(u64);
918
919 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
920 size += sizeof(u64);
921
922 if (event->attr.read_format & PERF_FORMAT_ID)
923 entry += sizeof(u64);
924
925 if (event->attr.read_format & PERF_FORMAT_GROUP) {
926 nr += event->group_leader->nr_siblings;
927 size += sizeof(u64);
928 }
929
930 size += entry * nr;
931 event->read_size = size;
932}
933
934static void perf_event__header_size(struct perf_event *event)
935{
936 struct perf_sample_data *data;
937 u64 sample_type = event->attr.sample_type;
938 u16 size = 0;
939
940 perf_event__read_size(event);
941
942 if (sample_type & PERF_SAMPLE_IP)
943 size += sizeof(data->ip);
944
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200945 if (sample_type & PERF_SAMPLE_ADDR)
946 size += sizeof(data->addr);
947
948 if (sample_type & PERF_SAMPLE_PERIOD)
949 size += sizeof(data->period);
950
951 if (sample_type & PERF_SAMPLE_READ)
952 size += event->read_size;
953
954 event->header_size = size;
955}
956
957static void perf_event__id_header_size(struct perf_event *event)
958{
959 struct perf_sample_data *data;
960 u64 sample_type = event->attr.sample_type;
961 u16 size = 0;
962
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200963 if (sample_type & PERF_SAMPLE_TID)
964 size += sizeof(data->tid_entry);
965
966 if (sample_type & PERF_SAMPLE_TIME)
967 size += sizeof(data->time);
968
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200969 if (sample_type & PERF_SAMPLE_ID)
970 size += sizeof(data->id);
971
972 if (sample_type & PERF_SAMPLE_STREAM_ID)
973 size += sizeof(data->stream_id);
974
975 if (sample_type & PERF_SAMPLE_CPU)
976 size += sizeof(data->cpu_entry);
977
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200978 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200979}
980
Peter Zijlstra8a495422010-05-27 15:47:49 +0200981static void perf_group_attach(struct perf_event *event)
982{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200983 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200984
Peter Zijlstra74c33372010-10-15 11:40:29 +0200985 /*
986 * We can have double attach due to group movement in perf_event_open.
987 */
988 if (event->attach_state & PERF_ATTACH_GROUP)
989 return;
990
Peter Zijlstra8a495422010-05-27 15:47:49 +0200991 event->attach_state |= PERF_ATTACH_GROUP;
992
993 if (group_leader == event)
994 return;
995
996 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
997 !is_software_event(event))
998 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
999
1000 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1001 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001002
1003 perf_event__header_size(group_leader);
1004
1005 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1006 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001007}
1008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009/*
1010 * Remove a event from the lists for its context.
1011 * Must be called with ctx->mutex and ctx->lock held.
1012 */
1013static void
1014list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1015{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001016 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001017 /*
1018 * We can have double detach due to exit/hot-unplug + close.
1019 */
1020 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001021 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001022
1023 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1024
Stephane Eranian68cacd22011-03-23 16:03:06 +01001025 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001026 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001027 cpuctx = __get_cpu_context(ctx);
1028 /*
1029 * if there are no more cgroup events
1030 * then cler cgrp to avoid stale pointer
1031 * in update_cgrp_time_from_cpuctx()
1032 */
1033 if (!ctx->nr_cgroups)
1034 cpuctx->cgrp = NULL;
1035 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001036
Stephane Eraniand010b332012-02-09 23:21:00 +01001037 if (has_branch_stack(event))
1038 ctx->nr_branch_stack--;
1039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040 ctx->nr_events--;
1041 if (event->attr.inherit_stat)
1042 ctx->nr_stat--;
1043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001044 list_del_rcu(&event->event_entry);
1045
Peter Zijlstra8a495422010-05-27 15:47:49 +02001046 if (event->group_leader == event)
1047 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001049 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001050
1051 /*
1052 * If event was in error state, then keep it
1053 * that way, otherwise bogus counts will be
1054 * returned on read(). The only way to get out
1055 * of error state is by explicit re-enabling
1056 * of the event
1057 */
1058 if (event->state > PERF_EVENT_STATE_OFF)
1059 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001060}
1061
Peter Zijlstra8a495422010-05-27 15:47:49 +02001062static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001063{
1064 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001065 struct list_head *list = NULL;
1066
1067 /*
1068 * We can have double detach due to exit/hot-unplug + close.
1069 */
1070 if (!(event->attach_state & PERF_ATTACH_GROUP))
1071 return;
1072
1073 event->attach_state &= ~PERF_ATTACH_GROUP;
1074
1075 /*
1076 * If this is a sibling, remove it from its group.
1077 */
1078 if (event->group_leader != event) {
1079 list_del_init(&event->group_entry);
1080 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001081 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001082 }
1083
1084 if (!list_empty(&event->group_entry))
1085 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001087 /*
1088 * If this was a group event with sibling events then
1089 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001090 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 */
1092 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001093 if (list)
1094 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001096
1097 /* Inherit group flags from the previous leader */
1098 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001100
1101out:
1102 perf_event__header_size(event->group_leader);
1103
1104 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1105 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106}
1107
Stephane Eranianfa66f072010-08-26 16:40:01 +02001108static inline int
1109event_filter_match(struct perf_event *event)
1110{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001111 return (event->cpu == -1 || event->cpu == smp_processor_id())
1112 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001113}
1114
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001115static void
1116event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117 struct perf_cpu_context *cpuctx,
1118 struct perf_event_context *ctx)
1119{
Stephane Eranian41587552011-01-03 18:20:01 +02001120 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001121 u64 delta;
1122 /*
1123 * An event which could not be activated because of
1124 * filter mismatch still needs to have its timings
1125 * maintained, otherwise bogus information is return
1126 * via read() for time_enabled, time_running:
1127 */
1128 if (event->state == PERF_EVENT_STATE_INACTIVE
1129 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001130 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001131 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001132 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001133 }
1134
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001136 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137
1138 event->state = PERF_EVENT_STATE_INACTIVE;
1139 if (event->pending_disable) {
1140 event->pending_disable = 0;
1141 event->state = PERF_EVENT_STATE_OFF;
1142 }
Stephane Eranian41587552011-01-03 18:20:01 +02001143 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001144 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001145 event->oncpu = -1;
1146
1147 if (!is_software_event(event))
1148 cpuctx->active_oncpu--;
1149 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001150 if (event->attr.freq && event->attr.sample_freq)
1151 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152 if (event->attr.exclusive || !cpuctx->active_oncpu)
1153 cpuctx->exclusive = 0;
1154}
1155
1156static void
1157group_sched_out(struct perf_event *group_event,
1158 struct perf_cpu_context *cpuctx,
1159 struct perf_event_context *ctx)
1160{
1161 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001162 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001163
1164 event_sched_out(group_event, cpuctx, ctx);
1165
1166 /*
1167 * Schedule out siblings (if any):
1168 */
1169 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1170 event_sched_out(event, cpuctx, ctx);
1171
Stephane Eranianfa66f072010-08-26 16:40:01 +02001172 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173 cpuctx->exclusive = 0;
1174}
1175
1176/*
1177 * Cross CPU call to remove a performance event
1178 *
1179 * We disable the event on the hardware level first. After that we
1180 * remove it from the context list.
1181 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001182static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001183{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001184 struct perf_event *event = info;
1185 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001186 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001188 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001189 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001190 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001191 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1192 ctx->is_active = 0;
1193 cpuctx->task_ctx = NULL;
1194 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001195 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001196
1197 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198}
1199
1200
1201/*
1202 * Remove the event from a task's (or a CPU's) list of events.
1203 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204 * CPU events are removed with a smp call. For task events we only
1205 * call when the task is on a CPU.
1206 *
1207 * If event->ctx is a cloned context, callers must make sure that
1208 * every task struct that event->ctx->task could possibly point to
1209 * remains valid. This is OK when called from perf_release since
1210 * that only calls us on the top-level context, which can't be a clone.
1211 * When called from perf_event_exit_task, it's OK because the
1212 * context has been detached from its task.
1213 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001214static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215{
1216 struct perf_event_context *ctx = event->ctx;
1217 struct task_struct *task = ctx->task;
1218
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001219 lockdep_assert_held(&ctx->mutex);
1220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001221 if (!task) {
1222 /*
1223 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001224 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001226 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227 return;
1228 }
1229
1230retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001231 if (!task_function_call(task, __perf_remove_from_context, event))
1232 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001234 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001236 * If we failed to find a running task, but find the context active now
1237 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001239 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001240 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 goto retry;
1242 }
1243
1244 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001245 * Since the task isn't running, its safe to remove the event, us
1246 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001248 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001249 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250}
1251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253 * Cross CPU call to disable a performance event
1254 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301255int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256{
1257 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001259 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260
1261 /*
1262 * If this is a per-task event, need to check whether this
1263 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001264 *
1265 * Can trigger due to concurrent perf_event_context_sched_out()
1266 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 */
1268 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001269 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001271 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272
1273 /*
1274 * If the event is on, turn it off.
1275 * If it is in error state, leave it in error state.
1276 */
1277 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1278 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001279 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 update_group_times(event);
1281 if (event == event->group_leader)
1282 group_sched_out(event, cpuctx, ctx);
1283 else
1284 event_sched_out(event, cpuctx, ctx);
1285 event->state = PERF_EVENT_STATE_OFF;
1286 }
1287
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001288 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001289
1290 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291}
1292
1293/*
1294 * Disable a event.
1295 *
1296 * If event->ctx is a cloned context, callers must make sure that
1297 * every task struct that event->ctx->task could possibly point to
1298 * remains valid. This condition is satisifed when called through
1299 * perf_event_for_each_child or perf_event_for_each because they
1300 * hold the top-level event's child_mutex, so any descendant that
1301 * goes to exit will block in sync_child_event.
1302 * When called from perf_pending_event it's OK because event->ctx
1303 * is the current context on this CPU and preemption is disabled,
1304 * hence we can't get into perf_event_task_sched_out for this context.
1305 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001306void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307{
1308 struct perf_event_context *ctx = event->ctx;
1309 struct task_struct *task = ctx->task;
1310
1311 if (!task) {
1312 /*
1313 * Disable the event on the cpu that it's on
1314 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001315 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316 return;
1317 }
1318
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001319retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001320 if (!task_function_call(task, __perf_event_disable, event))
1321 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001323 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 /*
1325 * If the event is still active, we need to retry the cross-call.
1326 */
1327 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001328 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001329 /*
1330 * Reload the task pointer, it might have been changed by
1331 * a concurrent perf_event_context_sched_out().
1332 */
1333 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334 goto retry;
1335 }
1336
1337 /*
1338 * Since we have the lock this context can't be scheduled
1339 * in, so we can change the state safely.
1340 */
1341 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1342 update_group_times(event);
1343 event->state = PERF_EVENT_STATE_OFF;
1344 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001345 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001347EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001348
Stephane Eraniane5d13672011-02-14 11:20:01 +02001349static void perf_set_shadow_time(struct perf_event *event,
1350 struct perf_event_context *ctx,
1351 u64 tstamp)
1352{
1353 /*
1354 * use the correct time source for the time snapshot
1355 *
1356 * We could get by without this by leveraging the
1357 * fact that to get to this function, the caller
1358 * has most likely already called update_context_time()
1359 * and update_cgrp_time_xx() and thus both timestamp
1360 * are identical (or very close). Given that tstamp is,
1361 * already adjusted for cgroup, we could say that:
1362 * tstamp - ctx->timestamp
1363 * is equivalent to
1364 * tstamp - cgrp->timestamp.
1365 *
1366 * Then, in perf_output_read(), the calculation would
1367 * work with no changes because:
1368 * - event is guaranteed scheduled in
1369 * - no scheduled out in between
1370 * - thus the timestamp would be the same
1371 *
1372 * But this is a bit hairy.
1373 *
1374 * So instead, we have an explicit cgroup call to remain
1375 * within the time time source all along. We believe it
1376 * is cleaner and simpler to understand.
1377 */
1378 if (is_cgroup_event(event))
1379 perf_cgroup_set_shadow_time(event, tstamp);
1380 else
1381 event->shadow_ctx_time = tstamp - ctx->timestamp;
1382}
1383
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001384#define MAX_INTERRUPTS (~0ULL)
1385
1386static void perf_log_throttle(struct perf_event *event, int enable);
1387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001389event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001391 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001392{
Stephane Eranian41587552011-01-03 18:20:01 +02001393 u64 tstamp = perf_event_time(event);
1394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395 if (event->state <= PERF_EVENT_STATE_OFF)
1396 return 0;
1397
1398 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001399 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001400
1401 /*
1402 * Unthrottle events, since we scheduled we might have missed several
1403 * ticks already, also for a heavily scheduling task there is little
1404 * guarantee it'll get a tick in a timely manner.
1405 */
1406 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1407 perf_log_throttle(event, 1);
1408 event->hw.interrupts = 0;
1409 }
1410
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001411 /*
1412 * The new state must be visible before we turn it on in the hardware:
1413 */
1414 smp_wmb();
1415
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001416 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 event->state = PERF_EVENT_STATE_INACTIVE;
1418 event->oncpu = -1;
1419 return -EAGAIN;
1420 }
1421
Stephane Eranian41587552011-01-03 18:20:01 +02001422 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001423
Stephane Eraniane5d13672011-02-14 11:20:01 +02001424 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001425
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426 if (!is_software_event(event))
1427 cpuctx->active_oncpu++;
1428 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001429 if (event->attr.freq && event->attr.sample_freq)
1430 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001431
1432 if (event->attr.exclusive)
1433 cpuctx->exclusive = 1;
1434
1435 return 0;
1436}
1437
1438static int
1439group_sched_in(struct perf_event *group_event,
1440 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001441 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442{
Lin Ming6bde9b62010-04-23 13:56:00 +08001443 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001444 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001445 u64 now = ctx->time;
1446 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447
1448 if (group_event->state == PERF_EVENT_STATE_OFF)
1449 return 0;
1450
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001451 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001452
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001453 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001454 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001456 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001457
1458 /*
1459 * Schedule in siblings as one group (if any):
1460 */
1461 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001462 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 partial_group = event;
1464 goto group_error;
1465 }
1466 }
1467
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001468 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001469 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001470
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001471group_error:
1472 /*
1473 * Groups can be scheduled in as one unit only, so undo any
1474 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001475 * The events up to the failed event are scheduled out normally,
1476 * tstamp_stopped will be updated.
1477 *
1478 * The failed events and the remaining siblings need to have
1479 * their timings updated as if they had gone thru event_sched_in()
1480 * and event_sched_out(). This is required to get consistent timings
1481 * across the group. This also takes care of the case where the group
1482 * could never be scheduled by ensuring tstamp_stopped is set to mark
1483 * the time the event was actually stopped, such that time delta
1484 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001485 */
1486 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1487 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001488 simulate = true;
1489
1490 if (simulate) {
1491 event->tstamp_running += now - event->tstamp_stopped;
1492 event->tstamp_stopped = now;
1493 } else {
1494 event_sched_out(event, cpuctx, ctx);
1495 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001496 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001497 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001499 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001501 return -EAGAIN;
1502}
1503
1504/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 * Work out whether we can put this event group on the CPU now.
1506 */
1507static int group_can_go_on(struct perf_event *event,
1508 struct perf_cpu_context *cpuctx,
1509 int can_add_hw)
1510{
1511 /*
1512 * Groups consisting entirely of software events can always go on.
1513 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001514 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001515 return 1;
1516 /*
1517 * If an exclusive group is already on, no other hardware
1518 * events can go on.
1519 */
1520 if (cpuctx->exclusive)
1521 return 0;
1522 /*
1523 * If this group is exclusive and there are already
1524 * events on the CPU, it can't go on.
1525 */
1526 if (event->attr.exclusive && cpuctx->active_oncpu)
1527 return 0;
1528 /*
1529 * Otherwise, try to add it if all previous groups were able
1530 * to go on.
1531 */
1532 return can_add_hw;
1533}
1534
1535static void add_event_to_ctx(struct perf_event *event,
1536 struct perf_event_context *ctx)
1537{
Stephane Eranian41587552011-01-03 18:20:01 +02001538 u64 tstamp = perf_event_time(event);
1539
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001540 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001541 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001542 event->tstamp_enabled = tstamp;
1543 event->tstamp_running = tstamp;
1544 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001545}
1546
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001547static void task_ctx_sched_out(struct perf_event_context *ctx);
1548static void
1549ctx_sched_in(struct perf_event_context *ctx,
1550 struct perf_cpu_context *cpuctx,
1551 enum event_type_t event_type,
1552 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001553
Peter Zijlstradce58552011-04-09 21:17:46 +02001554static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1555 struct perf_event_context *ctx,
1556 struct task_struct *task)
1557{
1558 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1559 if (ctx)
1560 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1561 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1562 if (ctx)
1563 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1564}
1565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566/*
1567 * Cross CPU call to install and enable a performance event
1568 *
1569 * Must be called with ctx->mutex held
1570 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001571static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573 struct perf_event *event = info;
1574 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001575 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001576 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1577 struct task_struct *task = current;
1578
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001579 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001580 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581
1582 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001583 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001585 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001586 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001587
1588 /*
1589 * If the context we're installing events in is not the
1590 * active task_ctx, flip them.
1591 */
1592 if (ctx->task && task_ctx != ctx) {
1593 if (task_ctx)
1594 raw_spin_unlock(&task_ctx->lock);
1595 raw_spin_lock(&ctx->lock);
1596 task_ctx = ctx;
1597 }
1598
1599 if (task_ctx) {
1600 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001601 task = task_ctx->task;
1602 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001603
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001604 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001606 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001607 /*
1608 * update cgrp time only if current cgrp
1609 * matches event->cgrp. Must be done before
1610 * calling add_event_to_ctx()
1611 */
1612 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614 add_event_to_ctx(event, ctx);
1615
1616 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001617 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001619 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001621 perf_pmu_enable(cpuctx->ctx.pmu);
1622 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001623
1624 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625}
1626
1627/*
1628 * Attach a performance event to a context
1629 *
1630 * First we add the event to the list with the hardware enable bit
1631 * in event->hw_config cleared.
1632 *
1633 * If the event is attached to a task which is on a CPU we use a smp
1634 * call to enable it in the task context. The task might have been
1635 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001636 */
1637static void
1638perf_install_in_context(struct perf_event_context *ctx,
1639 struct perf_event *event,
1640 int cpu)
1641{
1642 struct task_struct *task = ctx->task;
1643
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001644 lockdep_assert_held(&ctx->mutex);
1645
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001646 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001647 if (event->cpu != -1)
1648 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650 if (!task) {
1651 /*
1652 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001653 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001655 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656 return;
1657 }
1658
1659retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001660 if (!task_function_call(task, __perf_install_in_context, event))
1661 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001663 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001665 * If we failed to find a running task, but find the context active now
1666 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001668 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001669 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670 goto retry;
1671 }
1672
1673 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001674 * Since the task isn't running, its safe to add the event, us holding
1675 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001677 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001678 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679}
1680
1681/*
1682 * Put a event into inactive state and update time fields.
1683 * Enabling the leader of a group effectively enables all
1684 * the group members that aren't explicitly disabled, so we
1685 * have to update their ->tstamp_enabled also.
1686 * Note: this works for group members as well as group leaders
1687 * since the non-leader members' sibling_lists will be empty.
1688 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001689static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690{
1691 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001692 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693
1694 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001695 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001696 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001697 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1698 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001699 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700}
1701
1702/*
1703 * Cross CPU call to enable a performance event
1704 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001705static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706{
1707 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 struct perf_event_context *ctx = event->ctx;
1709 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001710 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711 int err;
1712
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001713 if (WARN_ON_ONCE(!ctx->is_active))
1714 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001716 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 update_context_time(ctx);
1718
1719 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1720 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001721
1722 /*
1723 * set current task's cgroup time reference point
1724 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001725 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001726
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001727 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
Stephane Eraniane5d13672011-02-14 11:20:01 +02001729 if (!event_filter_match(event)) {
1730 if (is_cgroup_event(event))
1731 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001732 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001733 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001734
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001735 /*
1736 * If the event is in a group and isn't the group leader,
1737 * then don't put it on unless the group is on.
1738 */
1739 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1740 goto unlock;
1741
1742 if (!group_can_go_on(event, cpuctx, 1)) {
1743 err = -EEXIST;
1744 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001746 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001748 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001749 }
1750
1751 if (err) {
1752 /*
1753 * If this event can't go on and it's part of a
1754 * group, then the whole group has to come off.
1755 */
1756 if (leader != event)
1757 group_sched_out(leader, cpuctx, ctx);
1758 if (leader->attr.pinned) {
1759 update_group_times(leader);
1760 leader->state = PERF_EVENT_STATE_ERROR;
1761 }
1762 }
1763
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001764unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001765 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001766
1767 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768}
1769
1770/*
1771 * Enable a event.
1772 *
1773 * If event->ctx is a cloned context, callers must make sure that
1774 * every task struct that event->ctx->task could possibly point to
1775 * remains valid. This condition is satisfied when called through
1776 * perf_event_for_each_child or perf_event_for_each as described
1777 * for perf_event_disable.
1778 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001779void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780{
1781 struct perf_event_context *ctx = event->ctx;
1782 struct task_struct *task = ctx->task;
1783
1784 if (!task) {
1785 /*
1786 * Enable the event on the cpu that it's on
1787 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001788 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 return;
1790 }
1791
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001792 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1794 goto out;
1795
1796 /*
1797 * If the event is in error state, clear that first.
1798 * That way, if we see the event in error state below, we
1799 * know that it has gone back into error state, as distinct
1800 * from the task having been scheduled away before the
1801 * cross-call arrived.
1802 */
1803 if (event->state == PERF_EVENT_STATE_ERROR)
1804 event->state = PERF_EVENT_STATE_OFF;
1805
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001806retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001807 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001808 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001809 goto out;
1810 }
1811
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001812 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001813
1814 if (!task_function_call(task, __perf_event_enable, event))
1815 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001816
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001817 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818
1819 /*
1820 * If the context is active and the event is still off,
1821 * we need to retry the cross-call.
1822 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001823 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1824 /*
1825 * task could have been flipped by a concurrent
1826 * perf_event_context_sched_out()
1827 */
1828 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001830 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001832out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001833 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001835EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001836
Avi Kivity26ca5c12011-06-29 18:42:37 +03001837int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838{
1839 /*
1840 * not supported on inherited events
1841 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001842 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843 return -EINVAL;
1844
1845 atomic_add(refresh, &event->event_limit);
1846 perf_event_enable(event);
1847
1848 return 0;
1849}
Avi Kivity26ca5c12011-06-29 18:42:37 +03001850EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001852static void ctx_sched_out(struct perf_event_context *ctx,
1853 struct perf_cpu_context *cpuctx,
1854 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855{
1856 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001857 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Peter Zijlstradb24d332011-04-09 21:17:45 +02001859 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001861 return;
1862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001864 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001865 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001866 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001867
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001868 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001869 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001870 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1871 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001872 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001873
Peter Zijlstradb24d332011-04-09 21:17:45 +02001874 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001875 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001876 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001877 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001878 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001879}
1880
1881/*
1882 * Test whether two contexts are equivalent, i.e. whether they
1883 * have both been cloned from the same version of the same context
1884 * and they both have the same number of enabled events.
1885 * If the number of enabled events is the same, then the set
1886 * of enabled events should be the same, because these are both
1887 * inherited contexts, therefore we can't access individual events
1888 * in them directly with an fd; we can only enable/disable all
1889 * events via prctl, or enable/disable all events in a family
1890 * via ioctl, which will have the same effect on both contexts.
1891 */
1892static int context_equiv(struct perf_event_context *ctx1,
1893 struct perf_event_context *ctx2)
1894{
1895 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1896 && ctx1->parent_gen == ctx2->parent_gen
1897 && !ctx1->pin_count && !ctx2->pin_count;
1898}
1899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900static void __perf_event_sync_stat(struct perf_event *event,
1901 struct perf_event *next_event)
1902{
1903 u64 value;
1904
1905 if (!event->attr.inherit_stat)
1906 return;
1907
1908 /*
1909 * Update the event value, we cannot use perf_event_read()
1910 * because we're in the middle of a context switch and have IRQs
1911 * disabled, which upsets smp_call_function_single(), however
1912 * we know the event must be on the current CPU, therefore we
1913 * don't need to use it.
1914 */
1915 switch (event->state) {
1916 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001917 event->pmu->read(event);
1918 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919
1920 case PERF_EVENT_STATE_INACTIVE:
1921 update_event_times(event);
1922 break;
1923
1924 default:
1925 break;
1926 }
1927
1928 /*
1929 * In order to keep per-task stats reliable we need to flip the event
1930 * values when we flip the contexts.
1931 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001932 value = local64_read(&next_event->count);
1933 value = local64_xchg(&event->count, value);
1934 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935
1936 swap(event->total_time_enabled, next_event->total_time_enabled);
1937 swap(event->total_time_running, next_event->total_time_running);
1938
1939 /*
1940 * Since we swizzled the values, update the user visible data too.
1941 */
1942 perf_event_update_userpage(event);
1943 perf_event_update_userpage(next_event);
1944}
1945
1946#define list_next_entry(pos, member) \
1947 list_entry(pos->member.next, typeof(*pos), member)
1948
1949static void perf_event_sync_stat(struct perf_event_context *ctx,
1950 struct perf_event_context *next_ctx)
1951{
1952 struct perf_event *event, *next_event;
1953
1954 if (!ctx->nr_stat)
1955 return;
1956
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001957 update_context_time(ctx);
1958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001959 event = list_first_entry(&ctx->event_list,
1960 struct perf_event, event_entry);
1961
1962 next_event = list_first_entry(&next_ctx->event_list,
1963 struct perf_event, event_entry);
1964
1965 while (&event->event_entry != &ctx->event_list &&
1966 &next_event->event_entry != &next_ctx->event_list) {
1967
1968 __perf_event_sync_stat(event, next_event);
1969
1970 event = list_next_entry(event, event_entry);
1971 next_event = list_next_entry(next_event, event_entry);
1972 }
1973}
1974
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001975static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1976 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001978 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001979 struct perf_event_context *next_ctx;
1980 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001981 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982 int do_switch = 1;
1983
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001984 if (likely(!ctx))
1985 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001987 cpuctx = __get_cpu_context(ctx);
1988 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989 return;
1990
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991 rcu_read_lock();
1992 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001993 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994 if (parent && next_ctx &&
1995 rcu_dereference(next_ctx->parent_ctx) == parent) {
1996 /*
1997 * Looks like the two contexts are clones, so we might be
1998 * able to optimize the context switch. We lock both
1999 * contexts and check that they are clones under the
2000 * lock (including re-checking that neither has been
2001 * uncloned in the meantime). It doesn't matter which
2002 * order we take the locks because no other cpu could
2003 * be trying to lock both of these tasks.
2004 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002005 raw_spin_lock(&ctx->lock);
2006 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002007 if (context_equiv(ctx, next_ctx)) {
2008 /*
2009 * XXX do we need a memory barrier of sorts
2010 * wrt to rcu_dereference() of perf_event_ctxp
2011 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002012 task->perf_event_ctxp[ctxn] = next_ctx;
2013 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 ctx->task = next;
2015 next_ctx->task = task;
2016 do_switch = 0;
2017
2018 perf_event_sync_stat(ctx, next_ctx);
2019 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002020 raw_spin_unlock(&next_ctx->lock);
2021 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022 }
2023 rcu_read_unlock();
2024
2025 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002026 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002027 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002028 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002029 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030 }
2031}
2032
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002033#define for_each_task_context_nr(ctxn) \
2034 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2035
2036/*
2037 * Called from scheduler to remove the events of the current task,
2038 * with interrupts disabled.
2039 *
2040 * We stop each event and update the event value in event->count.
2041 *
2042 * This does not protect us against NMI, but disable()
2043 * sets the disabled bit in the control field of event _before_
2044 * accessing the event control register. If a NMI hits, then it will
2045 * not restart the event.
2046 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002047void __perf_event_task_sched_out(struct task_struct *task,
2048 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002049{
2050 int ctxn;
2051
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002052 for_each_task_context_nr(ctxn)
2053 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002054
2055 /*
2056 * if cgroup events exist on this CPU, then we need
2057 * to check if we have to switch out PMU state.
2058 * cgroup event are system-wide mode only
2059 */
2060 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002061 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002062}
2063
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002064static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002066 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002067
2068 if (!cpuctx->task_ctx)
2069 return;
2070
2071 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2072 return;
2073
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002074 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075 cpuctx->task_ctx = NULL;
2076}
2077
2078/*
2079 * Called with IRQs disabled
2080 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002081static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2082 enum event_type_t event_type)
2083{
2084 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085}
2086
2087static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002088ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002089 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090{
2091 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002093 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2094 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002095 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002096 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097 continue;
2098
Stephane Eraniane5d13672011-02-14 11:20:01 +02002099 /* may need to reset tstamp_enabled */
2100 if (is_cgroup_event(event))
2101 perf_cgroup_mark_enabled(event, ctx);
2102
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002103 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002104 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105
2106 /*
2107 * If this pinned group hasn't been scheduled,
2108 * put it in error state.
2109 */
2110 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2111 update_group_times(event);
2112 event->state = PERF_EVENT_STATE_ERROR;
2113 }
2114 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002115}
2116
2117static void
2118ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002119 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002120{
2121 struct perf_event *event;
2122 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002124 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2125 /* Ignore events in OFF or ERROR state */
2126 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128 /*
2129 * Listen to the 'cpu' scheduling filter constraint
2130 * of events:
2131 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002132 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 continue;
2134
Stephane Eraniane5d13672011-02-14 11:20:01 +02002135 /* may need to reset tstamp_enabled */
2136 if (is_cgroup_event(event))
2137 perf_cgroup_mark_enabled(event, ctx);
2138
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002139 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002140 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002142 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002144}
2145
2146static void
2147ctx_sched_in(struct perf_event_context *ctx,
2148 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002149 enum event_type_t event_type,
2150 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002151{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002152 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002153 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002154
Peter Zijlstradb24d332011-04-09 21:17:45 +02002155 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002156 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002157 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002158
Stephane Eraniane5d13672011-02-14 11:20:01 +02002159 now = perf_clock();
2160 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002161 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002162 /*
2163 * First go through the list and put on any pinned groups
2164 * in order to give them the best chance of going on.
2165 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002166 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002167 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002168
2169 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002170 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002171 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172}
2173
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002174static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002175 enum event_type_t event_type,
2176 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002177{
2178 struct perf_event_context *ctx = &cpuctx->ctx;
2179
Stephane Eraniane5d13672011-02-14 11:20:01 +02002180 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002181}
2182
Stephane Eraniane5d13672011-02-14 11:20:01 +02002183static void perf_event_context_sched_in(struct perf_event_context *ctx,
2184 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002185{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002186 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002187
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002188 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002189 if (cpuctx->task_ctx == ctx)
2190 return;
2191
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002192 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002193 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002194 /*
2195 * We want to keep the following priority order:
2196 * cpu pinned (that don't need to move), task pinned,
2197 * cpu flexible, task flexible.
2198 */
2199 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2200
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002201 if (ctx->nr_events)
2202 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002203
Gleb Natapov86b47c22011-11-22 16:08:21 +02002204 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2205
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002206 perf_pmu_enable(ctx->pmu);
2207 perf_ctx_unlock(cpuctx, ctx);
2208
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002209 /*
2210 * Since these rotations are per-cpu, we need to ensure the
2211 * cpu-context we got scheduled on is actually rotating.
2212 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002213 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002214}
2215
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002216/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002217 * When sampling the branck stack in system-wide, it may be necessary
2218 * to flush the stack on context switch. This happens when the branch
2219 * stack does not tag its entries with the pid of the current task.
2220 * Otherwise it becomes impossible to associate a branch entry with a
2221 * task. This ambiguity is more likely to appear when the branch stack
2222 * supports priv level filtering and the user sets it to monitor only
2223 * at the user level (which could be a useful measurement in system-wide
2224 * mode). In that case, the risk is high of having a branch stack with
2225 * branch from multiple tasks. Flushing may mean dropping the existing
2226 * entries or stashing them somewhere in the PMU specific code layer.
2227 *
2228 * This function provides the context switch callback to the lower code
2229 * layer. It is invoked ONLY when there is at least one system-wide context
2230 * with at least one active event using taken branch sampling.
2231 */
2232static void perf_branch_stack_sched_in(struct task_struct *prev,
2233 struct task_struct *task)
2234{
2235 struct perf_cpu_context *cpuctx;
2236 struct pmu *pmu;
2237 unsigned long flags;
2238
2239 /* no need to flush branch stack if not changing task */
2240 if (prev == task)
2241 return;
2242
2243 local_irq_save(flags);
2244
2245 rcu_read_lock();
2246
2247 list_for_each_entry_rcu(pmu, &pmus, entry) {
2248 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2249
2250 /*
2251 * check if the context has at least one
2252 * event using PERF_SAMPLE_BRANCH_STACK
2253 */
2254 if (cpuctx->ctx.nr_branch_stack > 0
2255 && pmu->flush_branch_stack) {
2256
2257 pmu = cpuctx->ctx.pmu;
2258
2259 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2260
2261 perf_pmu_disable(pmu);
2262
2263 pmu->flush_branch_stack();
2264
2265 perf_pmu_enable(pmu);
2266
2267 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2268 }
2269 }
2270
2271 rcu_read_unlock();
2272
2273 local_irq_restore(flags);
2274}
2275
2276/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002277 * Called from scheduler to add the events of the current task
2278 * with interrupts disabled.
2279 *
2280 * We restore the event value and then enable it.
2281 *
2282 * This does not protect us against NMI, but enable()
2283 * sets the enabled bit in the control field of event _before_
2284 * accessing the event control register. If a NMI hits, then it will
2285 * keep the event running.
2286 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002287void __perf_event_task_sched_in(struct task_struct *prev,
2288 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002289{
2290 struct perf_event_context *ctx;
2291 int ctxn;
2292
2293 for_each_task_context_nr(ctxn) {
2294 ctx = task->perf_event_ctxp[ctxn];
2295 if (likely(!ctx))
2296 continue;
2297
Stephane Eraniane5d13672011-02-14 11:20:01 +02002298 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002299 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002300 /*
2301 * if cgroup events exist on this CPU, then we need
2302 * to check if we have to switch in PMU state.
2303 * cgroup event are system-wide mode only
2304 */
2305 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002306 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002307
2308 /* check for system-wide branch_stack events */
2309 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2310 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311}
2312
Peter Zijlstraabd50712010-01-26 18:50:16 +01002313static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2314{
2315 u64 frequency = event->attr.sample_freq;
2316 u64 sec = NSEC_PER_SEC;
2317 u64 divisor, dividend;
2318
2319 int count_fls, nsec_fls, frequency_fls, sec_fls;
2320
2321 count_fls = fls64(count);
2322 nsec_fls = fls64(nsec);
2323 frequency_fls = fls64(frequency);
2324 sec_fls = 30;
2325
2326 /*
2327 * We got @count in @nsec, with a target of sample_freq HZ
2328 * the target period becomes:
2329 *
2330 * @count * 10^9
2331 * period = -------------------
2332 * @nsec * sample_freq
2333 *
2334 */
2335
2336 /*
2337 * Reduce accuracy by one bit such that @a and @b converge
2338 * to a similar magnitude.
2339 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002340#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002341do { \
2342 if (a##_fls > b##_fls) { \
2343 a >>= 1; \
2344 a##_fls--; \
2345 } else { \
2346 b >>= 1; \
2347 b##_fls--; \
2348 } \
2349} while (0)
2350
2351 /*
2352 * Reduce accuracy until either term fits in a u64, then proceed with
2353 * the other, so that finally we can do a u64/u64 division.
2354 */
2355 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2356 REDUCE_FLS(nsec, frequency);
2357 REDUCE_FLS(sec, count);
2358 }
2359
2360 if (count_fls + sec_fls > 64) {
2361 divisor = nsec * frequency;
2362
2363 while (count_fls + sec_fls > 64) {
2364 REDUCE_FLS(count, sec);
2365 divisor >>= 1;
2366 }
2367
2368 dividend = count * sec;
2369 } else {
2370 dividend = count * sec;
2371
2372 while (nsec_fls + frequency_fls > 64) {
2373 REDUCE_FLS(nsec, frequency);
2374 dividend >>= 1;
2375 }
2376
2377 divisor = nsec * frequency;
2378 }
2379
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002380 if (!divisor)
2381 return dividend;
2382
Peter Zijlstraabd50712010-01-26 18:50:16 +01002383 return div64_u64(dividend, divisor);
2384}
2385
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002386static DEFINE_PER_CPU(int, perf_throttled_count);
2387static DEFINE_PER_CPU(u64, perf_throttled_seq);
2388
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002389static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390{
2391 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002392 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393 s64 delta;
2394
Peter Zijlstraabd50712010-01-26 18:50:16 +01002395 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396
2397 delta = (s64)(period - hwc->sample_period);
2398 delta = (delta + 7) / 8; /* low pass filter */
2399
2400 sample_period = hwc->sample_period + delta;
2401
2402 if (!sample_period)
2403 sample_period = 1;
2404
2405 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002406
Peter Zijlstrae7850592010-05-21 14:43:08 +02002407 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002408 if (disable)
2409 event->pmu->stop(event, PERF_EF_UPDATE);
2410
Peter Zijlstrae7850592010-05-21 14:43:08 +02002411 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002412
2413 if (disable)
2414 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002415 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002416}
2417
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002418/*
2419 * combine freq adjustment with unthrottling to avoid two passes over the
2420 * events. At the same time, make sure, having freq events does not change
2421 * the rate of unthrottling as that would introduce bias.
2422 */
2423static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2424 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002425{
2426 struct perf_event *event;
2427 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002428 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002429 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002431 /*
2432 * only need to iterate over all events iff:
2433 * - context have events in frequency mode (needs freq adjust)
2434 * - there are events to unthrottle on this cpu
2435 */
2436 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002437 return;
2438
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002439 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002440 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002441
Paul Mackerras03541f82009-10-14 16:58:03 +11002442 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443 if (event->state != PERF_EVENT_STATE_ACTIVE)
2444 continue;
2445
Stephane Eranian5632ab12011-01-03 18:20:01 +02002446 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002447 continue;
2448
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002449 hwc = &event->hw;
2450
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002451 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2452 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002454 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002455 }
2456
2457 if (!event->attr.freq || !event->attr.sample_freq)
2458 continue;
2459
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002460 /*
2461 * stop the event and update event->count
2462 */
2463 event->pmu->stop(event, PERF_EF_UPDATE);
2464
Peter Zijlstrae7850592010-05-21 14:43:08 +02002465 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002466 delta = now - hwc->freq_count_stamp;
2467 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002468
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002469 /*
2470 * restart the event
2471 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002472 * we have stopped the event so tell that
2473 * to perf_adjust_period() to avoid stopping it
2474 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002475 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002476 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002477 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002478
2479 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002481
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002482 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002483 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002484}
2485
2486/*
2487 * Round-robin a context's events:
2488 */
2489static void rotate_ctx(struct perf_event_context *ctx)
2490{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002491 /*
2492 * Rotate the first entry last of non-pinned groups. Rotation might be
2493 * disabled by the inheritance code.
2494 */
2495 if (!ctx->rotate_disable)
2496 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497}
2498
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002499/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002500 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2501 * because they're strictly cpu affine and rotate_start is called with IRQs
2502 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002503 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002504static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002506 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002507 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002508
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002509 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002510 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002511 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2512 rotate = 1;
2513 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002515 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002516 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002517 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002518 if (ctx->nr_events != ctx->nr_active)
2519 rotate = 1;
2520 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002522 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002523 goto done;
2524
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002525 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002526 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002527
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002528 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2529 if (ctx)
2530 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002531
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002532 rotate_ctx(&cpuctx->ctx);
2533 if (ctx)
2534 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002535
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002536 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002537
2538 perf_pmu_enable(cpuctx->ctx.pmu);
2539 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002540done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002541 if (remove)
2542 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002543}
2544
2545void perf_event_task_tick(void)
2546{
2547 struct list_head *head = &__get_cpu_var(rotation_list);
2548 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002549 struct perf_event_context *ctx;
2550 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002551
2552 WARN_ON(!irqs_disabled());
2553
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002554 __this_cpu_inc(perf_throttled_seq);
2555 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2556
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002557 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002558 ctx = &cpuctx->ctx;
2559 perf_adjust_freq_unthr_context(ctx, throttled);
2560
2561 ctx = cpuctx->task_ctx;
2562 if (ctx)
2563 perf_adjust_freq_unthr_context(ctx, throttled);
2564
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002565 if (cpuctx->jiffies_interval == 1 ||
2566 !(jiffies % cpuctx->jiffies_interval))
2567 perf_rotate_context(cpuctx);
2568 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002569}
2570
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002571static int event_enable_on_exec(struct perf_event *event,
2572 struct perf_event_context *ctx)
2573{
2574 if (!event->attr.enable_on_exec)
2575 return 0;
2576
2577 event->attr.enable_on_exec = 0;
2578 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2579 return 0;
2580
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002581 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002582
2583 return 1;
2584}
2585
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002586/*
2587 * Enable all of a task's events that have been marked enable-on-exec.
2588 * This expects task == current.
2589 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002590static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002592 struct perf_event *event;
2593 unsigned long flags;
2594 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002595 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002596
2597 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002598 if (!ctx || !ctx->nr_events)
2599 goto out;
2600
Stephane Eraniane566b762011-04-06 02:54:54 +02002601 /*
2602 * We must ctxsw out cgroup events to avoid conflict
2603 * when invoking perf_task_event_sched_in() later on
2604 * in this function. Otherwise we end up trying to
2605 * ctxswin cgroup events which are already scheduled
2606 * in.
2607 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002608 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002609
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002610 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002611 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002613 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002614 ret = event_enable_on_exec(event, ctx);
2615 if (ret)
2616 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002617 }
2618
2619 /*
2620 * Unclone this context if we enabled any event.
2621 */
2622 if (enabled)
2623 unclone_ctx(ctx);
2624
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002625 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002626
Stephane Eraniane566b762011-04-06 02:54:54 +02002627 /*
2628 * Also calls ctxswin for cgroup events, if any:
2629 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002630 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002631out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632 local_irq_restore(flags);
2633}
2634
2635/*
2636 * Cross CPU call to read the hardware event
2637 */
2638static void __perf_event_read(void *info)
2639{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002640 struct perf_event *event = info;
2641 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002642 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002643
2644 /*
2645 * If this is a task context, we need to check whether it is
2646 * the current task context of this cpu. If not it has been
2647 * scheduled out before the smp call arrived. In that case
2648 * event->count would have been updated to a recent sample
2649 * when the event was scheduled out.
2650 */
2651 if (ctx->task && cpuctx->task_ctx != ctx)
2652 return;
2653
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002654 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002655 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002656 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002657 update_cgrp_time_from_event(event);
2658 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002660 if (event->state == PERF_EVENT_STATE_ACTIVE)
2661 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002662 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002663}
2664
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002665static inline u64 perf_event_count(struct perf_event *event)
2666{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002667 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002668}
2669
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670static u64 perf_event_read(struct perf_event *event)
2671{
2672 /*
2673 * If event is enabled and currently active on a CPU, update the
2674 * value in the event structure:
2675 */
2676 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2677 smp_call_function_single(event->oncpu,
2678 __perf_event_read, event, 1);
2679 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002680 struct perf_event_context *ctx = event->ctx;
2681 unsigned long flags;
2682
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002683 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002684 /*
2685 * may read while context is not active
2686 * (e.g., thread is blocked), in that case
2687 * we cannot update context time
2688 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002689 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002690 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002691 update_cgrp_time_from_event(event);
2692 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002694 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695 }
2696
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002697 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698}
2699
2700/*
2701 * Initialize the perf_event context in a task_struct:
2702 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002703static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002704{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002705 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002706 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002707 INIT_LIST_HEAD(&ctx->pinned_groups);
2708 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709 INIT_LIST_HEAD(&ctx->event_list);
2710 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002711}
2712
Peter Zijlstraeb184472010-09-07 15:55:13 +02002713static struct perf_event_context *
2714alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715{
2716 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002717
2718 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2719 if (!ctx)
2720 return NULL;
2721
2722 __perf_event_init_context(ctx);
2723 if (task) {
2724 ctx->task = task;
2725 get_task_struct(task);
2726 }
2727 ctx->pmu = pmu;
2728
2729 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730}
2731
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002732static struct task_struct *
2733find_lively_task_by_vpid(pid_t vpid)
2734{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002736 int err;
2737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002738 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002739 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740 task = current;
2741 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002742 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743 if (task)
2744 get_task_struct(task);
2745 rcu_read_unlock();
2746
2747 if (!task)
2748 return ERR_PTR(-ESRCH);
2749
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750 /* Reuse ptrace permission checks for now. */
2751 err = -EACCES;
2752 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2753 goto errout;
2754
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002755 return task;
2756errout:
2757 put_task_struct(task);
2758 return ERR_PTR(err);
2759
2760}
2761
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002762/*
2763 * Returns a matching context with refcount and pincount.
2764 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002765static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002766find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767{
2768 struct perf_event_context *ctx;
2769 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002771 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002773 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774 /* Must be root to operate on a CPU event: */
2775 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2776 return ERR_PTR(-EACCES);
2777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 /*
2779 * We could be clever and allow to attach a event to an
2780 * offline CPU and activate it when the CPU comes up, but
2781 * that's for later.
2782 */
2783 if (!cpu_online(cpu))
2784 return ERR_PTR(-ENODEV);
2785
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002786 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002787 ctx = &cpuctx->ctx;
2788 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002789 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790
2791 return ctx;
2792 }
2793
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002794 err = -EINVAL;
2795 ctxn = pmu->task_ctx_nr;
2796 if (ctxn < 0)
2797 goto errout;
2798
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002799retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002800 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801 if (ctx) {
2802 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002803 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002804 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002805 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002806 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002807 err = -ENOMEM;
2808 if (!ctx)
2809 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002810
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002811 err = 0;
2812 mutex_lock(&task->perf_event_mutex);
2813 /*
2814 * If it has already passed perf_event_exit_task().
2815 * we must see PF_EXITING, it takes this mutex too.
2816 */
2817 if (task->flags & PF_EXITING)
2818 err = -ESRCH;
2819 else if (task->perf_event_ctxp[ctxn])
2820 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002821 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002822 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002823 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002824 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002825 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002826 mutex_unlock(&task->perf_event_mutex);
2827
2828 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002829 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002830
2831 if (err == -EAGAIN)
2832 goto retry;
2833 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002834 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002835 }
2836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002837 return ctx;
2838
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002839errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002840 return ERR_PTR(err);
2841}
2842
Li Zefan6fb29152009-10-15 11:21:42 +08002843static void perf_event_free_filter(struct perf_event *event);
2844
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002845static void free_event_rcu(struct rcu_head *head)
2846{
2847 struct perf_event *event;
2848
2849 event = container_of(head, struct perf_event, rcu_head);
2850 if (event->ns)
2851 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002852 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002853 kfree(event);
2854}
2855
Frederic Weisbecker76369132011-05-19 19:55:04 +02002856static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857
2858static void free_event(struct perf_event *event)
2859{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002860 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861
2862 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002863 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01002864 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002865 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002866 atomic_dec(&nr_mmap_events);
2867 if (event->attr.comm)
2868 atomic_dec(&nr_comm_events);
2869 if (event->attr.task)
2870 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002871 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2872 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002873 if (is_cgroup_event(event)) {
2874 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01002875 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01002876 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002877
2878 if (has_branch_stack(event)) {
2879 static_key_slow_dec_deferred(&perf_sched_events);
2880 /* is system-wide event */
2881 if (!(event->attach_state & PERF_ATTACH_TASK))
2882 atomic_dec(&per_cpu(perf_branch_stack_events,
2883 event->cpu));
2884 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002885 }
2886
Frederic Weisbecker76369132011-05-19 19:55:04 +02002887 if (event->rb) {
2888 ring_buffer_put(event->rb);
2889 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002890 }
2891
Stephane Eraniane5d13672011-02-14 11:20:01 +02002892 if (is_cgroup_event(event))
2893 perf_detach_cgroup(event);
2894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002895 if (event->destroy)
2896 event->destroy(event);
2897
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002898 if (event->ctx)
2899 put_ctx(event->ctx);
2900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002901 call_rcu(&event->rcu_head, free_event_rcu);
2902}
2903
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002904int perf_event_release_kernel(struct perf_event *event)
2905{
2906 struct perf_event_context *ctx = event->ctx;
2907
2908 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002909 /*
2910 * There are two ways this annotation is useful:
2911 *
2912 * 1) there is a lock recursion from perf_event_exit_task
2913 * see the comment there.
2914 *
2915 * 2) there is a lock-inversion with mmap_sem through
2916 * perf_event_read_group(), which takes faults while
2917 * holding ctx->mutex, however this is called after
2918 * the last filedesc died, so there is no possibility
2919 * to trigger the AB-BA case.
2920 */
2921 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002922 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002923 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002924 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02002925 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002926 mutex_unlock(&ctx->mutex);
2927
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002928 free_event(event);
2929
2930 return 0;
2931}
2932EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2933
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002934/*
2935 * Called when the last reference to the file is gone.
2936 */
Al Viroa6fa9412012-08-20 14:59:25 +01002937static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002938{
Peter Zijlstra88821352010-11-09 19:01:43 +01002939 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002940
Al Viroa6fa9412012-08-20 14:59:25 +01002941 if (!atomic_long_dec_and_test(&event->refcount))
2942 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002943
Peter Zijlstra88821352010-11-09 19:01:43 +01002944 rcu_read_lock();
2945 owner = ACCESS_ONCE(event->owner);
2946 /*
2947 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2948 * !owner it means the list deletion is complete and we can indeed
2949 * free this event, otherwise we need to serialize on
2950 * owner->perf_event_mutex.
2951 */
2952 smp_read_barrier_depends();
2953 if (owner) {
2954 /*
2955 * Since delayed_put_task_struct() also drops the last
2956 * task reference we can safely take a new reference
2957 * while holding the rcu_read_lock().
2958 */
2959 get_task_struct(owner);
2960 }
2961 rcu_read_unlock();
2962
2963 if (owner) {
2964 mutex_lock(&owner->perf_event_mutex);
2965 /*
2966 * We have to re-check the event->owner field, if it is cleared
2967 * we raced with perf_event_exit_task(), acquiring the mutex
2968 * ensured they're done, and we can proceed with freeing the
2969 * event.
2970 */
2971 if (event->owner)
2972 list_del_init(&event->owner_entry);
2973 mutex_unlock(&owner->perf_event_mutex);
2974 put_task_struct(owner);
2975 }
2976
Al Viroa6fa9412012-08-20 14:59:25 +01002977 perf_event_release_kernel(event);
2978}
2979
2980static int perf_release(struct inode *inode, struct file *file)
2981{
2982 put_event(file->private_data);
2983 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002984}
2985
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002986u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002987{
2988 struct perf_event *child;
2989 u64 total = 0;
2990
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002991 *enabled = 0;
2992 *running = 0;
2993
Peter Zijlstra6f105812009-11-20 22:19:56 +01002994 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002995 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002996 *enabled += event->total_time_enabled +
2997 atomic64_read(&event->child_total_time_enabled);
2998 *running += event->total_time_running +
2999 atomic64_read(&event->child_total_time_running);
3000
3001 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003003 *enabled += child->total_time_enabled;
3004 *running += child->total_time_running;
3005 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003006 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007
3008 return total;
3009}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003010EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012static int perf_event_read_group(struct perf_event *event,
3013 u64 read_format, char __user *buf)
3014{
3015 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003016 int n = 0, size = 0, ret = -EFAULT;
3017 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003018 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003019 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003020
Peter Zijlstra6f105812009-11-20 22:19:56 +01003021 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003022 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003023
3024 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003025 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3026 values[n++] = enabled;
3027 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3028 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003029 values[n++] = count;
3030 if (read_format & PERF_FORMAT_ID)
3031 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003032
3033 size = n * sizeof(u64);
3034
3035 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003036 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037
Peter Zijlstra6f105812009-11-20 22:19:56 +01003038 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039
3040 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003041 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003043 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003044 if (read_format & PERF_FORMAT_ID)
3045 values[n++] = primary_event_id(sub);
3046
3047 size = n * sizeof(u64);
3048
Stephane Eranian184d3da2009-11-23 21:40:49 -08003049 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003050 ret = -EFAULT;
3051 goto unlock;
3052 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003053
3054 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003056unlock:
3057 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003058
Peter Zijlstraabf48682009-11-20 22:19:49 +01003059 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060}
3061
3062static int perf_event_read_one(struct perf_event *event,
3063 u64 read_format, char __user *buf)
3064{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003065 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066 u64 values[4];
3067 int n = 0;
3068
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003069 values[n++] = perf_event_read_value(event, &enabled, &running);
3070 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3071 values[n++] = enabled;
3072 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3073 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074 if (read_format & PERF_FORMAT_ID)
3075 values[n++] = primary_event_id(event);
3076
3077 if (copy_to_user(buf, values, n * sizeof(u64)))
3078 return -EFAULT;
3079
3080 return n * sizeof(u64);
3081}
3082
3083/*
3084 * Read the performance event - simple non blocking version for now
3085 */
3086static ssize_t
3087perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3088{
3089 u64 read_format = event->attr.read_format;
3090 int ret;
3091
3092 /*
3093 * Return end-of-file for a read on a event that is in
3094 * error state (i.e. because it was pinned but it couldn't be
3095 * scheduled on to the CPU at some point).
3096 */
3097 if (event->state == PERF_EVENT_STATE_ERROR)
3098 return 0;
3099
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003100 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101 return -ENOSPC;
3102
3103 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003104 if (read_format & PERF_FORMAT_GROUP)
3105 ret = perf_event_read_group(event, read_format, buf);
3106 else
3107 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108
3109 return ret;
3110}
3111
3112static ssize_t
3113perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3114{
3115 struct perf_event *event = file->private_data;
3116
3117 return perf_read_hw(event, buf, count);
3118}
3119
3120static unsigned int perf_poll(struct file *file, poll_table *wait)
3121{
3122 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003123 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003124 unsigned int events = POLL_HUP;
3125
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003126 /*
3127 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3128 * grabs the rb reference but perf_event_set_output() overrides it.
3129 * Here is the timeline for two threads T1, T2:
3130 * t0: T1, rb = rcu_dereference(event->rb)
3131 * t1: T2, old_rb = event->rb
3132 * t2: T2, event->rb = new rb
3133 * t3: T2, ring_buffer_detach(old_rb)
3134 * t4: T1, ring_buffer_attach(rb1)
3135 * t5: T1, poll_wait(event->waitq)
3136 *
3137 * To avoid this problem, we grab mmap_mutex in perf_poll()
3138 * thereby ensuring that the assignment of the new ring buffer
3139 * and the detachment of the old buffer appear atomic to perf_poll()
3140 */
3141 mutex_lock(&event->mmap_mutex);
3142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003144 rb = rcu_dereference(event->rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003145 if (rb) {
3146 ring_buffer_attach(event, rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003147 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003148 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003149 rcu_read_unlock();
3150
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003151 mutex_unlock(&event->mmap_mutex);
3152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 poll_wait(file, &event->waitq, wait);
3154
3155 return events;
3156}
3157
3158static void perf_event_reset(struct perf_event *event)
3159{
3160 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003161 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003162 perf_event_update_userpage(event);
3163}
3164
3165/*
3166 * Holding the top-level event's child_mutex means that any
3167 * descendant process that has inherited this event will block
3168 * in sync_child_event if it goes to exit, thus satisfying the
3169 * task existence requirements of perf_event_enable/disable.
3170 */
3171static void perf_event_for_each_child(struct perf_event *event,
3172 void (*func)(struct perf_event *))
3173{
3174 struct perf_event *child;
3175
3176 WARN_ON_ONCE(event->ctx->parent_ctx);
3177 mutex_lock(&event->child_mutex);
3178 func(event);
3179 list_for_each_entry(child, &event->child_list, child_list)
3180 func(child);
3181 mutex_unlock(&event->child_mutex);
3182}
3183
3184static void perf_event_for_each(struct perf_event *event,
3185 void (*func)(struct perf_event *))
3186{
3187 struct perf_event_context *ctx = event->ctx;
3188 struct perf_event *sibling;
3189
3190 WARN_ON_ONCE(ctx->parent_ctx);
3191 mutex_lock(&ctx->mutex);
3192 event = event->group_leader;
3193
3194 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003195 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003196 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197 mutex_unlock(&ctx->mutex);
3198}
3199
3200static int perf_event_period(struct perf_event *event, u64 __user *arg)
3201{
3202 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203 int ret = 0;
3204 u64 value;
3205
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003206 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 return -EINVAL;
3208
John Blackwoodad0cf342010-09-28 18:03:11 -04003209 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003210 return -EFAULT;
3211
3212 if (!value)
3213 return -EINVAL;
3214
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003215 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 if (event->attr.freq) {
3217 if (value > sysctl_perf_event_sample_rate) {
3218 ret = -EINVAL;
3219 goto unlock;
3220 }
3221
3222 event->attr.sample_freq = value;
3223 } else {
3224 event->attr.sample_period = value;
3225 event->hw.sample_period = value;
3226 }
3227unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003228 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229
3230 return ret;
3231}
3232
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003233static const struct file_operations perf_fops;
3234
Al Viro2903ff02012-08-28 12:52:22 -04003235static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003236{
Al Viro2903ff02012-08-28 12:52:22 -04003237 struct fd f = fdget(fd);
3238 if (!f.file)
3239 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003240
Al Viro2903ff02012-08-28 12:52:22 -04003241 if (f.file->f_op != &perf_fops) {
3242 fdput(f);
3243 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003244 }
Al Viro2903ff02012-08-28 12:52:22 -04003245 *p = f;
3246 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003247}
3248
3249static int perf_event_set_output(struct perf_event *event,
3250 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003251static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252
3253static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3254{
3255 struct perf_event *event = file->private_data;
3256 void (*func)(struct perf_event *);
3257 u32 flags = arg;
3258
3259 switch (cmd) {
3260 case PERF_EVENT_IOC_ENABLE:
3261 func = perf_event_enable;
3262 break;
3263 case PERF_EVENT_IOC_DISABLE:
3264 func = perf_event_disable;
3265 break;
3266 case PERF_EVENT_IOC_RESET:
3267 func = perf_event_reset;
3268 break;
3269
3270 case PERF_EVENT_IOC_REFRESH:
3271 return perf_event_refresh(event, arg);
3272
3273 case PERF_EVENT_IOC_PERIOD:
3274 return perf_event_period(event, (u64 __user *)arg);
3275
3276 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003277 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003278 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003279 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003280 struct perf_event *output_event;
3281 struct fd output;
3282 ret = perf_fget_light(arg, &output);
3283 if (ret)
3284 return ret;
3285 output_event = output.file->private_data;
3286 ret = perf_event_set_output(event, output_event);
3287 fdput(output);
3288 } else {
3289 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003290 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003291 return ret;
3292 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293
Li Zefan6fb29152009-10-15 11:21:42 +08003294 case PERF_EVENT_IOC_SET_FILTER:
3295 return perf_event_set_filter(event, (void __user *)arg);
3296
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297 default:
3298 return -ENOTTY;
3299 }
3300
3301 if (flags & PERF_IOC_FLAG_GROUP)
3302 perf_event_for_each(event, func);
3303 else
3304 perf_event_for_each_child(event, func);
3305
3306 return 0;
3307}
3308
3309int perf_event_task_enable(void)
3310{
3311 struct perf_event *event;
3312
3313 mutex_lock(&current->perf_event_mutex);
3314 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3315 perf_event_for_each_child(event, perf_event_enable);
3316 mutex_unlock(&current->perf_event_mutex);
3317
3318 return 0;
3319}
3320
3321int perf_event_task_disable(void)
3322{
3323 struct perf_event *event;
3324
3325 mutex_lock(&current->perf_event_mutex);
3326 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3327 perf_event_for_each_child(event, perf_event_disable);
3328 mutex_unlock(&current->perf_event_mutex);
3329
3330 return 0;
3331}
3332
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333static int perf_event_index(struct perf_event *event)
3334{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003335 if (event->hw.state & PERF_HES_STOPPED)
3336 return 0;
3337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 if (event->state != PERF_EVENT_STATE_ACTIVE)
3339 return 0;
3340
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003341 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342}
3343
Eric B Munsonc4794292011-06-23 16:34:38 -04003344static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003345 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003346 u64 *enabled,
3347 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003348{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003349 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003350
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003351 *now = perf_clock();
3352 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003353 *enabled = ctx_time - event->tstamp_enabled;
3354 *running = ctx_time - event->tstamp_running;
3355}
3356
Peter Zijlstrac7206202012-03-22 17:26:36 +01003357void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003358{
3359}
3360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361/*
3362 * Callers need to ensure there can be no nesting of this function, otherwise
3363 * the seqlock logic goes bad. We can not serialize this because the arch
3364 * code calls this from NMI context.
3365 */
3366void perf_event_update_userpage(struct perf_event *event)
3367{
3368 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003369 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003370 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371
3372 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003373 /*
3374 * compute total_time_enabled, total_time_running
3375 * based on snapshot values taken when the event
3376 * was last scheduled in.
3377 *
3378 * we cannot simply called update_context_time()
3379 * because of locking issue as we can be called in
3380 * NMI context
3381 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003382 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003383 rb = rcu_dereference(event->rb);
3384 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003385 goto unlock;
3386
Frederic Weisbecker76369132011-05-19 19:55:04 +02003387 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003388
3389 /*
3390 * Disable preemption so as to not let the corresponding user-space
3391 * spin too long if we get preempted.
3392 */
3393 preempt_disable();
3394 ++userpg->lock;
3395 barrier();
3396 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003397 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003398 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003399 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003400
Eric B Munson0d641202011-06-24 12:26:26 -04003401 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 atomic64_read(&event->child_total_time_enabled);
3403
Eric B Munson0d641202011-06-24 12:26:26 -04003404 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405 atomic64_read(&event->child_total_time_running);
3406
Peter Zijlstrac7206202012-03-22 17:26:36 +01003407 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 barrier();
3410 ++userpg->lock;
3411 preempt_enable();
3412unlock:
3413 rcu_read_unlock();
3414}
3415
Peter Zijlstra906010b2009-09-21 16:08:49 +02003416static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3417{
3418 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003419 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003420 int ret = VM_FAULT_SIGBUS;
3421
3422 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3423 if (vmf->pgoff == 0)
3424 ret = 0;
3425 return ret;
3426 }
3427
3428 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003429 rb = rcu_dereference(event->rb);
3430 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003431 goto unlock;
3432
3433 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3434 goto unlock;
3435
Frederic Weisbecker76369132011-05-19 19:55:04 +02003436 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003437 if (!vmf->page)
3438 goto unlock;
3439
3440 get_page(vmf->page);
3441 vmf->page->mapping = vma->vm_file->f_mapping;
3442 vmf->page->index = vmf->pgoff;
3443
3444 ret = 0;
3445unlock:
3446 rcu_read_unlock();
3447
3448 return ret;
3449}
3450
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003451static void ring_buffer_attach(struct perf_event *event,
3452 struct ring_buffer *rb)
3453{
3454 unsigned long flags;
3455
3456 if (!list_empty(&event->rb_entry))
3457 return;
3458
3459 spin_lock_irqsave(&rb->event_lock, flags);
3460 if (!list_empty(&event->rb_entry))
3461 goto unlock;
3462
3463 list_add(&event->rb_entry, &rb->event_list);
3464unlock:
3465 spin_unlock_irqrestore(&rb->event_lock, flags);
3466}
3467
3468static void ring_buffer_detach(struct perf_event *event,
3469 struct ring_buffer *rb)
3470{
3471 unsigned long flags;
3472
3473 if (list_empty(&event->rb_entry))
3474 return;
3475
3476 spin_lock_irqsave(&rb->event_lock, flags);
3477 list_del_init(&event->rb_entry);
3478 wake_up_all(&event->waitq);
3479 spin_unlock_irqrestore(&rb->event_lock, flags);
3480}
3481
3482static void ring_buffer_wakeup(struct perf_event *event)
3483{
3484 struct ring_buffer *rb;
3485
3486 rcu_read_lock();
3487 rb = rcu_dereference(event->rb);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003488 if (!rb)
3489 goto unlock;
3490
3491 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003492 wake_up_all(&event->waitq);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003493
3494unlock:
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003495 rcu_read_unlock();
3496}
3497
Frederic Weisbecker76369132011-05-19 19:55:04 +02003498static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003499{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003500 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003501
Frederic Weisbecker76369132011-05-19 19:55:04 +02003502 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3503 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003504}
3505
Frederic Weisbecker76369132011-05-19 19:55:04 +02003506static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003507{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003508 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003510 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003511 rb = rcu_dereference(event->rb);
3512 if (rb) {
3513 if (!atomic_inc_not_zero(&rb->refcount))
3514 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003515 }
3516 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517
Frederic Weisbecker76369132011-05-19 19:55:04 +02003518 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003519}
3520
Frederic Weisbecker76369132011-05-19 19:55:04 +02003521static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003522{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003523 struct perf_event *event, *n;
3524 unsigned long flags;
3525
Frederic Weisbecker76369132011-05-19 19:55:04 +02003526 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003527 return;
3528
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003529 spin_lock_irqsave(&rb->event_lock, flags);
3530 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3531 list_del_init(&event->rb_entry);
3532 wake_up_all(&event->waitq);
3533 }
3534 spin_unlock_irqrestore(&rb->event_lock, flags);
3535
Frederic Weisbecker76369132011-05-19 19:55:04 +02003536 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003537}
3538
3539static void perf_mmap_open(struct vm_area_struct *vma)
3540{
3541 struct perf_event *event = vma->vm_file->private_data;
3542
3543 atomic_inc(&event->mmap_count);
3544}
3545
3546static void perf_mmap_close(struct vm_area_struct *vma)
3547{
3548 struct perf_event *event = vma->vm_file->private_data;
3549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003551 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003552 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003553 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554
Peter Zijlstra906010b2009-09-21 16:08:49 +02003555 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003556 vma->vm_mm->pinned_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003557 rcu_assign_pointer(event->rb, NULL);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003558 ring_buffer_detach(event, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003560
Frederic Weisbecker76369132011-05-19 19:55:04 +02003561 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003562 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563 }
3564}
3565
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003566static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567 .open = perf_mmap_open,
3568 .close = perf_mmap_close,
3569 .fault = perf_mmap_fault,
3570 .page_mkwrite = perf_mmap_fault,
3571};
3572
3573static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3574{
3575 struct perf_event *event = file->private_data;
3576 unsigned long user_locked, user_lock_limit;
3577 struct user_struct *user = current_user();
3578 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003579 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003580 unsigned long vma_size;
3581 unsigned long nr_pages;
3582 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003583 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003584
Peter Zijlstrac7920612010-05-18 10:33:24 +02003585 /*
3586 * Don't allow mmap() of inherited per-task counters. This would
3587 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003588 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003589 */
3590 if (event->cpu == -1 && event->attr.inherit)
3591 return -EINVAL;
3592
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003593 if (!(vma->vm_flags & VM_SHARED))
3594 return -EINVAL;
3595
3596 vma_size = vma->vm_end - vma->vm_start;
3597 nr_pages = (vma_size / PAGE_SIZE) - 1;
3598
3599 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003600 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003601 * can do bitmasks instead of modulo.
3602 */
3603 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3604 return -EINVAL;
3605
3606 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3607 return -EINVAL;
3608
3609 if (vma->vm_pgoff != 0)
3610 return -EINVAL;
3611
3612 WARN_ON_ONCE(event->ctx->parent_ctx);
3613 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003614 if (event->rb) {
3615 if (event->rb->nr_pages == nr_pages)
3616 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003617 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003618 ret = -EINVAL;
3619 goto unlock;
3620 }
3621
3622 user_extra = nr_pages + 1;
3623 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3624
3625 /*
3626 * Increase the limit linearly with more CPUs:
3627 */
3628 user_lock_limit *= num_online_cpus();
3629
3630 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3631
3632 extra = 0;
3633 if (user_locked > user_lock_limit)
3634 extra = user_locked - user_lock_limit;
3635
Jiri Slaby78d7d402010-03-05 13:42:54 -08003636 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003637 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003638 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003639
3640 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3641 !capable(CAP_IPC_LOCK)) {
3642 ret = -EPERM;
3643 goto unlock;
3644 }
3645
Frederic Weisbecker76369132011-05-19 19:55:04 +02003646 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003647
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003648 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003649 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003650
Vince Weaver4ec83632011-06-01 15:15:36 -04003651 rb = rb_alloc(nr_pages,
3652 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3653 event->cpu, flags);
3654
Frederic Weisbecker76369132011-05-19 19:55:04 +02003655 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003656 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003658 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003659 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003660
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003661 atomic_long_add(user_extra, &user->locked_vm);
3662 event->mmap_locked = extra;
3663 event->mmap_user = get_current_user();
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003664 vma->vm_mm->pinned_vm += event->mmap_locked;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003665
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003666 perf_event_update_userpage(event);
3667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003668unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003669 if (!ret)
3670 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003671 mutex_unlock(&event->mmap_mutex);
3672
3673 vma->vm_flags |= VM_RESERVED;
3674 vma->vm_ops = &perf_mmap_vmops;
3675
3676 return ret;
3677}
3678
3679static int perf_fasync(int fd, struct file *filp, int on)
3680{
3681 struct inode *inode = filp->f_path.dentry->d_inode;
3682 struct perf_event *event = filp->private_data;
3683 int retval;
3684
3685 mutex_lock(&inode->i_mutex);
3686 retval = fasync_helper(fd, filp, on, &event->fasync);
3687 mutex_unlock(&inode->i_mutex);
3688
3689 if (retval < 0)
3690 return retval;
3691
3692 return 0;
3693}
3694
3695static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003696 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003697 .release = perf_release,
3698 .read = perf_read,
3699 .poll = perf_poll,
3700 .unlocked_ioctl = perf_ioctl,
3701 .compat_ioctl = perf_ioctl,
3702 .mmap = perf_mmap,
3703 .fasync = perf_fasync,
3704};
3705
3706/*
3707 * Perf event wakeup
3708 *
3709 * If there's data, ensure we set the poll() state and publish everything
3710 * to user-space before waking everybody up.
3711 */
3712
3713void perf_event_wakeup(struct perf_event *event)
3714{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003715 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003716
3717 if (event->pending_kill) {
3718 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3719 event->pending_kill = 0;
3720 }
3721}
3722
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003723static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003724{
3725 struct perf_event *event = container_of(entry,
3726 struct perf_event, pending);
3727
3728 if (event->pending_disable) {
3729 event->pending_disable = 0;
3730 __perf_event_disable(event);
3731 }
3732
3733 if (event->pending_wakeup) {
3734 event->pending_wakeup = 0;
3735 perf_event_wakeup(event);
3736 }
3737}
3738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003740 * We assume there is only KVM supporting the callbacks.
3741 * Later on, we might change it to a list if there is
3742 * another virtualization implementation supporting the callbacks.
3743 */
3744struct perf_guest_info_callbacks *perf_guest_cbs;
3745
3746int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3747{
3748 perf_guest_cbs = cbs;
3749 return 0;
3750}
3751EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3752
3753int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3754{
3755 perf_guest_cbs = NULL;
3756 return 0;
3757}
3758EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3759
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003760static void __perf_event_header__init_id(struct perf_event_header *header,
3761 struct perf_sample_data *data,
3762 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003763{
3764 u64 sample_type = event->attr.sample_type;
3765
3766 data->type = sample_type;
3767 header->size += event->id_header_size;
3768
3769 if (sample_type & PERF_SAMPLE_TID) {
3770 /* namespace issues */
3771 data->tid_entry.pid = perf_event_pid(event, current);
3772 data->tid_entry.tid = perf_event_tid(event, current);
3773 }
3774
3775 if (sample_type & PERF_SAMPLE_TIME)
3776 data->time = perf_clock();
3777
3778 if (sample_type & PERF_SAMPLE_ID)
3779 data->id = primary_event_id(event);
3780
3781 if (sample_type & PERF_SAMPLE_STREAM_ID)
3782 data->stream_id = event->id;
3783
3784 if (sample_type & PERF_SAMPLE_CPU) {
3785 data->cpu_entry.cpu = raw_smp_processor_id();
3786 data->cpu_entry.reserved = 0;
3787 }
3788}
3789
Frederic Weisbecker76369132011-05-19 19:55:04 +02003790void perf_event_header__init_id(struct perf_event_header *header,
3791 struct perf_sample_data *data,
3792 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003793{
3794 if (event->attr.sample_id_all)
3795 __perf_event_header__init_id(header, data, event);
3796}
3797
3798static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3799 struct perf_sample_data *data)
3800{
3801 u64 sample_type = data->type;
3802
3803 if (sample_type & PERF_SAMPLE_TID)
3804 perf_output_put(handle, data->tid_entry);
3805
3806 if (sample_type & PERF_SAMPLE_TIME)
3807 perf_output_put(handle, data->time);
3808
3809 if (sample_type & PERF_SAMPLE_ID)
3810 perf_output_put(handle, data->id);
3811
3812 if (sample_type & PERF_SAMPLE_STREAM_ID)
3813 perf_output_put(handle, data->stream_id);
3814
3815 if (sample_type & PERF_SAMPLE_CPU)
3816 perf_output_put(handle, data->cpu_entry);
3817}
3818
Frederic Weisbecker76369132011-05-19 19:55:04 +02003819void perf_event__output_id_sample(struct perf_event *event,
3820 struct perf_output_handle *handle,
3821 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003822{
3823 if (event->attr.sample_id_all)
3824 __perf_event__output_id_sample(handle, sample);
3825}
3826
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003827static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003828 struct perf_event *event,
3829 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003830{
3831 u64 read_format = event->attr.read_format;
3832 u64 values[4];
3833 int n = 0;
3834
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003835 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003836 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003837 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003838 atomic64_read(&event->child_total_time_enabled);
3839 }
3840 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003841 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003842 atomic64_read(&event->child_total_time_running);
3843 }
3844 if (read_format & PERF_FORMAT_ID)
3845 values[n++] = primary_event_id(event);
3846
Frederic Weisbecker76369132011-05-19 19:55:04 +02003847 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848}
3849
3850/*
3851 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3852 */
3853static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003854 struct perf_event *event,
3855 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003856{
3857 struct perf_event *leader = event->group_leader, *sub;
3858 u64 read_format = event->attr.read_format;
3859 u64 values[5];
3860 int n = 0;
3861
3862 values[n++] = 1 + leader->nr_siblings;
3863
3864 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003865 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866
3867 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003868 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869
3870 if (leader != event)
3871 leader->pmu->read(leader);
3872
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003873 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003874 if (read_format & PERF_FORMAT_ID)
3875 values[n++] = primary_event_id(leader);
3876
Frederic Weisbecker76369132011-05-19 19:55:04 +02003877 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003878
3879 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3880 n = 0;
3881
3882 if (sub != event)
3883 sub->pmu->read(sub);
3884
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003885 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003886 if (read_format & PERF_FORMAT_ID)
3887 values[n++] = primary_event_id(sub);
3888
Frederic Weisbecker76369132011-05-19 19:55:04 +02003889 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003890 }
3891}
3892
Stephane Eranianeed01522010-10-26 16:08:01 +02003893#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3894 PERF_FORMAT_TOTAL_TIME_RUNNING)
3895
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003896static void perf_output_read(struct perf_output_handle *handle,
3897 struct perf_event *event)
3898{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003899 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02003900 u64 read_format = event->attr.read_format;
3901
3902 /*
3903 * compute total_time_enabled, total_time_running
3904 * based on snapshot values taken when the event
3905 * was last scheduled in.
3906 *
3907 * we cannot simply called update_context_time()
3908 * because of locking issue as we are called in
3909 * NMI context
3910 */
Eric B Munsonc4794292011-06-23 16:34:38 -04003911 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003912 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02003913
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003914 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003915 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003917 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918}
3919
3920void perf_output_sample(struct perf_output_handle *handle,
3921 struct perf_event_header *header,
3922 struct perf_sample_data *data,
3923 struct perf_event *event)
3924{
3925 u64 sample_type = data->type;
3926
3927 perf_output_put(handle, *header);
3928
3929 if (sample_type & PERF_SAMPLE_IP)
3930 perf_output_put(handle, data->ip);
3931
3932 if (sample_type & PERF_SAMPLE_TID)
3933 perf_output_put(handle, data->tid_entry);
3934
3935 if (sample_type & PERF_SAMPLE_TIME)
3936 perf_output_put(handle, data->time);
3937
3938 if (sample_type & PERF_SAMPLE_ADDR)
3939 perf_output_put(handle, data->addr);
3940
3941 if (sample_type & PERF_SAMPLE_ID)
3942 perf_output_put(handle, data->id);
3943
3944 if (sample_type & PERF_SAMPLE_STREAM_ID)
3945 perf_output_put(handle, data->stream_id);
3946
3947 if (sample_type & PERF_SAMPLE_CPU)
3948 perf_output_put(handle, data->cpu_entry);
3949
3950 if (sample_type & PERF_SAMPLE_PERIOD)
3951 perf_output_put(handle, data->period);
3952
3953 if (sample_type & PERF_SAMPLE_READ)
3954 perf_output_read(handle, event);
3955
3956 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3957 if (data->callchain) {
3958 int size = 1;
3959
3960 if (data->callchain)
3961 size += data->callchain->nr;
3962
3963 size *= sizeof(u64);
3964
Frederic Weisbecker76369132011-05-19 19:55:04 +02003965 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003966 } else {
3967 u64 nr = 0;
3968 perf_output_put(handle, nr);
3969 }
3970 }
3971
3972 if (sample_type & PERF_SAMPLE_RAW) {
3973 if (data->raw) {
3974 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003975 __output_copy(handle, data->raw->data,
3976 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977 } else {
3978 struct {
3979 u32 size;
3980 u32 data;
3981 } raw = {
3982 .size = sizeof(u32),
3983 .data = 0,
3984 };
3985 perf_output_put(handle, raw);
3986 }
3987 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02003988
3989 if (!event->attr.watermark) {
3990 int wakeup_events = event->attr.wakeup_events;
3991
3992 if (wakeup_events) {
3993 struct ring_buffer *rb = handle->rb;
3994 int events = local_inc_return(&rb->events);
3995
3996 if (events >= wakeup_events) {
3997 local_sub(wakeup_events, &rb->events);
3998 local_inc(&rb->wakeup);
3999 }
4000 }
4001 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004002
4003 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4004 if (data->br_stack) {
4005 size_t size;
4006
4007 size = data->br_stack->nr
4008 * sizeof(struct perf_branch_entry);
4009
4010 perf_output_put(handle, data->br_stack->nr);
4011 perf_output_copy(handle, data->br_stack->entries, size);
4012 } else {
4013 /*
4014 * we always store at least the value of nr
4015 */
4016 u64 nr = 0;
4017 perf_output_put(handle, nr);
4018 }
4019 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004020}
4021
4022void perf_prepare_sample(struct perf_event_header *header,
4023 struct perf_sample_data *data,
4024 struct perf_event *event,
4025 struct pt_regs *regs)
4026{
4027 u64 sample_type = event->attr.sample_type;
4028
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004029 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004030 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031
4032 header->misc = 0;
4033 header->misc |= perf_misc_flags(regs);
4034
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004035 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004036
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004037 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004038 data->ip = perf_instruction_pointer(regs);
4039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4041 int size = 1;
4042
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004043 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004044
4045 if (data->callchain)
4046 size += data->callchain->nr;
4047
4048 header->size += size * sizeof(u64);
4049 }
4050
4051 if (sample_type & PERF_SAMPLE_RAW) {
4052 int size = sizeof(u32);
4053
4054 if (data->raw)
4055 size += data->raw->size;
4056 else
4057 size += sizeof(u32);
4058
4059 WARN_ON_ONCE(size & (sizeof(u64)-1));
4060 header->size += size;
4061 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004062
4063 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4064 int size = sizeof(u64); /* nr */
4065 if (data->br_stack) {
4066 size += data->br_stack->nr
4067 * sizeof(struct perf_branch_entry);
4068 }
4069 header->size += size;
4070 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004071}
4072
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004073static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004074 struct perf_sample_data *data,
4075 struct pt_regs *regs)
4076{
4077 struct perf_output_handle handle;
4078 struct perf_event_header header;
4079
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004080 /* protect the callchain buffers */
4081 rcu_read_lock();
4082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083 perf_prepare_sample(&header, data, event, regs);
4084
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004085 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004086 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087
4088 perf_output_sample(&handle, &header, data, event);
4089
4090 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004091
4092exit:
4093 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094}
4095
4096/*
4097 * read event_id
4098 */
4099
4100struct perf_read_event {
4101 struct perf_event_header header;
4102
4103 u32 pid;
4104 u32 tid;
4105};
4106
4107static void
4108perf_event_read_event(struct perf_event *event,
4109 struct task_struct *task)
4110{
4111 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004112 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113 struct perf_read_event read_event = {
4114 .header = {
4115 .type = PERF_RECORD_READ,
4116 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004117 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 },
4119 .pid = perf_event_pid(event, task),
4120 .tid = perf_event_tid(event, task),
4121 };
4122 int ret;
4123
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004124 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004125 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126 if (ret)
4127 return;
4128
4129 perf_output_put(&handle, read_event);
4130 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004131 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004132
4133 perf_output_end(&handle);
4134}
4135
4136/*
4137 * task tracking -- fork/exit
4138 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004139 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004140 */
4141
4142struct perf_task_event {
4143 struct task_struct *task;
4144 struct perf_event_context *task_ctx;
4145
4146 struct {
4147 struct perf_event_header header;
4148
4149 u32 pid;
4150 u32 ppid;
4151 u32 tid;
4152 u32 ptid;
4153 u64 time;
4154 } event_id;
4155};
4156
4157static void perf_event_task_output(struct perf_event *event,
4158 struct perf_task_event *task_event)
4159{
4160 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004161 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004163 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004164
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004165 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004167 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004168 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004169 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004170 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004171
4172 task_event->event_id.pid = perf_event_pid(event, task);
4173 task_event->event_id.ppid = perf_event_pid(event, current);
4174
4175 task_event->event_id.tid = perf_event_tid(event, task);
4176 task_event->event_id.ptid = perf_event_tid(event, current);
4177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178 perf_output_put(&handle, task_event->event_id);
4179
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004180 perf_event__output_id_sample(event, &handle, &sample);
4181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004183out:
4184 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185}
4186
4187static int perf_event_task_match(struct perf_event *event)
4188{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004189 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004190 return 0;
4191
Stephane Eranian5632ab12011-01-03 18:20:01 +02004192 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004193 return 0;
4194
Eric B Munson3af9e852010-05-18 15:30:49 +01004195 if (event->attr.comm || event->attr.mmap ||
4196 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004197 return 1;
4198
4199 return 0;
4200}
4201
4202static void perf_event_task_ctx(struct perf_event_context *ctx,
4203 struct perf_task_event *task_event)
4204{
4205 struct perf_event *event;
4206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4208 if (perf_event_task_match(event))
4209 perf_event_task_output(event, task_event);
4210 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004211}
4212
4213static void perf_event_task_event(struct perf_task_event *task_event)
4214{
4215 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004216 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004217 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004218 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004220 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004221 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004222 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004223 if (cpuctx->active_pmu != pmu)
4224 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004225 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004226
4227 ctx = task_event->task_ctx;
4228 if (!ctx) {
4229 ctxn = pmu->task_ctx_nr;
4230 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004231 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004232 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4233 }
4234 if (ctx)
4235 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004236next:
4237 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004238 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239 rcu_read_unlock();
4240}
4241
4242static void perf_event_task(struct task_struct *task,
4243 struct perf_event_context *task_ctx,
4244 int new)
4245{
4246 struct perf_task_event task_event;
4247
4248 if (!atomic_read(&nr_comm_events) &&
4249 !atomic_read(&nr_mmap_events) &&
4250 !atomic_read(&nr_task_events))
4251 return;
4252
4253 task_event = (struct perf_task_event){
4254 .task = task,
4255 .task_ctx = task_ctx,
4256 .event_id = {
4257 .header = {
4258 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4259 .misc = 0,
4260 .size = sizeof(task_event.event_id),
4261 },
4262 /* .pid */
4263 /* .ppid */
4264 /* .tid */
4265 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004266 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267 },
4268 };
4269
4270 perf_event_task_event(&task_event);
4271}
4272
4273void perf_event_fork(struct task_struct *task)
4274{
4275 perf_event_task(task, NULL, 1);
4276}
4277
4278/*
4279 * comm tracking
4280 */
4281
4282struct perf_comm_event {
4283 struct task_struct *task;
4284 char *comm;
4285 int comm_size;
4286
4287 struct {
4288 struct perf_event_header header;
4289
4290 u32 pid;
4291 u32 tid;
4292 } event_id;
4293};
4294
4295static void perf_event_comm_output(struct perf_event *event,
4296 struct perf_comm_event *comm_event)
4297{
4298 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004299 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004300 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004301 int ret;
4302
4303 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4304 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004305 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306
4307 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004308 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309
4310 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4311 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4312
4313 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004314 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004315 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004316
4317 perf_event__output_id_sample(event, &handle, &sample);
4318
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004319 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004320out:
4321 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322}
4323
4324static int perf_event_comm_match(struct perf_event *event)
4325{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004326 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004327 return 0;
4328
Stephane Eranian5632ab12011-01-03 18:20:01 +02004329 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004330 return 0;
4331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332 if (event->attr.comm)
4333 return 1;
4334
4335 return 0;
4336}
4337
4338static void perf_event_comm_ctx(struct perf_event_context *ctx,
4339 struct perf_comm_event *comm_event)
4340{
4341 struct perf_event *event;
4342
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4344 if (perf_event_comm_match(event))
4345 perf_event_comm_output(event, comm_event);
4346 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004347}
4348
4349static void perf_event_comm_event(struct perf_comm_event *comm_event)
4350{
4351 struct perf_cpu_context *cpuctx;
4352 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004353 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004355 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004356 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357
4358 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004359 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360 size = ALIGN(strlen(comm)+1, sizeof(u64));
4361
4362 comm_event->comm = comm;
4363 comm_event->comm_size = size;
4364
4365 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004366 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004367 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004368 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004369 if (cpuctx->active_pmu != pmu)
4370 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004371 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004372
4373 ctxn = pmu->task_ctx_nr;
4374 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004375 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004376
4377 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4378 if (ctx)
4379 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004380next:
4381 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004382 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004383 rcu_read_unlock();
4384}
4385
4386void perf_event_comm(struct task_struct *task)
4387{
4388 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004389 struct perf_event_context *ctx;
4390 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004391
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004392 for_each_task_context_nr(ctxn) {
4393 ctx = task->perf_event_ctxp[ctxn];
4394 if (!ctx)
4395 continue;
4396
4397 perf_event_enable_on_exec(ctx);
4398 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004399
4400 if (!atomic_read(&nr_comm_events))
4401 return;
4402
4403 comm_event = (struct perf_comm_event){
4404 .task = task,
4405 /* .comm */
4406 /* .comm_size */
4407 .event_id = {
4408 .header = {
4409 .type = PERF_RECORD_COMM,
4410 .misc = 0,
4411 /* .size */
4412 },
4413 /* .pid */
4414 /* .tid */
4415 },
4416 };
4417
4418 perf_event_comm_event(&comm_event);
4419}
4420
4421/*
4422 * mmap tracking
4423 */
4424
4425struct perf_mmap_event {
4426 struct vm_area_struct *vma;
4427
4428 const char *file_name;
4429 int file_size;
4430
4431 struct {
4432 struct perf_event_header header;
4433
4434 u32 pid;
4435 u32 tid;
4436 u64 start;
4437 u64 len;
4438 u64 pgoff;
4439 } event_id;
4440};
4441
4442static void perf_event_mmap_output(struct perf_event *event,
4443 struct perf_mmap_event *mmap_event)
4444{
4445 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004446 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004448 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004449
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004450 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4451 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004452 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004454 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004455
4456 mmap_event->event_id.pid = perf_event_pid(event, current);
4457 mmap_event->event_id.tid = perf_event_tid(event, current);
4458
4459 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004460 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004461 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004462
4463 perf_event__output_id_sample(event, &handle, &sample);
4464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004466out:
4467 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004468}
4469
4470static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004471 struct perf_mmap_event *mmap_event,
4472 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004473{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004474 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004475 return 0;
4476
Stephane Eranian5632ab12011-01-03 18:20:01 +02004477 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004478 return 0;
4479
Eric B Munson3af9e852010-05-18 15:30:49 +01004480 if ((!executable && event->attr.mmap_data) ||
4481 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482 return 1;
4483
4484 return 0;
4485}
4486
4487static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004488 struct perf_mmap_event *mmap_event,
4489 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004490{
4491 struct perf_event *event;
4492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004494 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 perf_event_mmap_output(event, mmap_event);
4496 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497}
4498
4499static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4500{
4501 struct perf_cpu_context *cpuctx;
4502 struct perf_event_context *ctx;
4503 struct vm_area_struct *vma = mmap_event->vma;
4504 struct file *file = vma->vm_file;
4505 unsigned int size;
4506 char tmp[16];
4507 char *buf = NULL;
4508 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004509 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004510 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511
4512 memset(tmp, 0, sizeof(tmp));
4513
4514 if (file) {
4515 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004516 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517 * need to add enough zero bytes after the string to handle
4518 * the 64bit alignment we do later.
4519 */
4520 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4521 if (!buf) {
4522 name = strncpy(tmp, "//enomem", sizeof(tmp));
4523 goto got_name;
4524 }
4525 name = d_path(&file->f_path, buf, PATH_MAX);
4526 if (IS_ERR(name)) {
4527 name = strncpy(tmp, "//toolong", sizeof(tmp));
4528 goto got_name;
4529 }
4530 } else {
4531 if (arch_vma_name(mmap_event->vma)) {
4532 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4533 sizeof(tmp));
4534 goto got_name;
4535 }
4536
4537 if (!vma->vm_mm) {
4538 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4539 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004540 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4541 vma->vm_end >= vma->vm_mm->brk) {
4542 name = strncpy(tmp, "[heap]", sizeof(tmp));
4543 goto got_name;
4544 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4545 vma->vm_end >= vma->vm_mm->start_stack) {
4546 name = strncpy(tmp, "[stack]", sizeof(tmp));
4547 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548 }
4549
4550 name = strncpy(tmp, "//anon", sizeof(tmp));
4551 goto got_name;
4552 }
4553
4554got_name:
4555 size = ALIGN(strlen(name)+1, sizeof(u64));
4556
4557 mmap_event->file_name = name;
4558 mmap_event->file_size = size;
4559
4560 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4561
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004562 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004563 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004564 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004565 if (cpuctx->active_pmu != pmu)
4566 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004567 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4568 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004569
4570 ctxn = pmu->task_ctx_nr;
4571 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004572 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004573
4574 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4575 if (ctx) {
4576 perf_event_mmap_ctx(ctx, mmap_event,
4577 vma->vm_flags & VM_EXEC);
4578 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004579next:
4580 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004581 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004582 rcu_read_unlock();
4583
4584 kfree(buf);
4585}
4586
Eric B Munson3af9e852010-05-18 15:30:49 +01004587void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004588{
4589 struct perf_mmap_event mmap_event;
4590
4591 if (!atomic_read(&nr_mmap_events))
4592 return;
4593
4594 mmap_event = (struct perf_mmap_event){
4595 .vma = vma,
4596 /* .file_name */
4597 /* .file_size */
4598 .event_id = {
4599 .header = {
4600 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004601 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004602 /* .size */
4603 },
4604 /* .pid */
4605 /* .tid */
4606 .start = vma->vm_start,
4607 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004608 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609 },
4610 };
4611
4612 perf_event_mmap_event(&mmap_event);
4613}
4614
4615/*
4616 * IRQ throttle logging
4617 */
4618
4619static void perf_log_throttle(struct perf_event *event, int enable)
4620{
4621 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004622 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004623 int ret;
4624
4625 struct {
4626 struct perf_event_header header;
4627 u64 time;
4628 u64 id;
4629 u64 stream_id;
4630 } throttle_event = {
4631 .header = {
4632 .type = PERF_RECORD_THROTTLE,
4633 .misc = 0,
4634 .size = sizeof(throttle_event),
4635 },
4636 .time = perf_clock(),
4637 .id = primary_event_id(event),
4638 .stream_id = event->id,
4639 };
4640
4641 if (enable)
4642 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4643
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004644 perf_event_header__init_id(&throttle_event.header, &sample, event);
4645
4646 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004647 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648 if (ret)
4649 return;
4650
4651 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004652 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004653 perf_output_end(&handle);
4654}
4655
4656/*
4657 * Generic event overflow handling, sampling.
4658 */
4659
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004660static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004661 int throttle, struct perf_sample_data *data,
4662 struct pt_regs *regs)
4663{
4664 int events = atomic_read(&event->event_limit);
4665 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004666 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004667 int ret = 0;
4668
Peter Zijlstra96398822010-11-24 18:55:29 +01004669 /*
4670 * Non-sampling counters might still use the PMI to fold short
4671 * hardware counters, ignore those.
4672 */
4673 if (unlikely(!is_sampling_event(event)))
4674 return 0;
4675
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004676 seq = __this_cpu_read(perf_throttled_seq);
4677 if (seq != hwc->interrupts_seq) {
4678 hwc->interrupts_seq = seq;
4679 hwc->interrupts = 1;
4680 } else {
4681 hwc->interrupts++;
4682 if (unlikely(throttle
4683 && hwc->interrupts >= max_samples_per_tick)) {
4684 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01004685 hwc->interrupts = MAX_INTERRUPTS;
4686 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687 ret = 1;
4688 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004689 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690
4691 if (event->attr.freq) {
4692 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004693 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004694
Peter Zijlstraabd50712010-01-26 18:50:16 +01004695 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696
Peter Zijlstraabd50712010-01-26 18:50:16 +01004697 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004698 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004699 }
4700
4701 /*
4702 * XXX event_limit might not quite work as expected on inherited
4703 * events
4704 */
4705
4706 event->pending_kill = POLL_IN;
4707 if (events && atomic_dec_and_test(&event->event_limit)) {
4708 ret = 1;
4709 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004710 event->pending_disable = 1;
4711 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004712 }
4713
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004714 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004715 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004716 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004717 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004718
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004719 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004720 event->pending_wakeup = 1;
4721 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004722 }
4723
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004724 return ret;
4725}
4726
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004727int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004728 struct perf_sample_data *data,
4729 struct pt_regs *regs)
4730{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004731 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732}
4733
4734/*
4735 * Generic software event infrastructure
4736 */
4737
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004738struct swevent_htable {
4739 struct swevent_hlist *swevent_hlist;
4740 struct mutex hlist_mutex;
4741 int hlist_refcount;
4742
4743 /* Recursion avoidance in each contexts */
4744 int recursion[PERF_NR_CONTEXTS];
4745};
4746
4747static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4748
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004749/*
4750 * We directly increment event->count and keep a second value in
4751 * event->hw.period_left to count intervals. This period event
4752 * is kept in the range [-sample_period, 0] so that we can use the
4753 * sign as trigger.
4754 */
4755
4756static u64 perf_swevent_set_period(struct perf_event *event)
4757{
4758 struct hw_perf_event *hwc = &event->hw;
4759 u64 period = hwc->last_period;
4760 u64 nr, offset;
4761 s64 old, val;
4762
4763 hwc->last_period = hwc->sample_period;
4764
4765again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004766 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004767 if (val < 0)
4768 return 0;
4769
4770 nr = div64_u64(period + val, period);
4771 offset = nr * period;
4772 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004773 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774 goto again;
4775
4776 return nr;
4777}
4778
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004779static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004780 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004781 struct pt_regs *regs)
4782{
4783 struct hw_perf_event *hwc = &event->hw;
4784 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004786 if (!overflow)
4787 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004788
4789 if (hwc->interrupts == MAX_INTERRUPTS)
4790 return;
4791
4792 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004793 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794 data, regs)) {
4795 /*
4796 * We inhibit the overflow from happening when
4797 * hwc->interrupts == MAX_INTERRUPTS.
4798 */
4799 break;
4800 }
4801 throttle = 1;
4802 }
4803}
4804
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004805static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004806 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004807 struct pt_regs *regs)
4808{
4809 struct hw_perf_event *hwc = &event->hw;
4810
Peter Zijlstrae7850592010-05-21 14:43:08 +02004811 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004812
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004813 if (!regs)
4814 return;
4815
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004816 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004817 return;
4818
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03004819 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
4820 data->period = nr;
4821 return perf_swevent_overflow(event, 1, data, regs);
4822 } else
4823 data->period = event->hw.last_period;
4824
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004825 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004826 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004827
Peter Zijlstrae7850592010-05-21 14:43:08 +02004828 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004829 return;
4830
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004831 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004832}
4833
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004834static int perf_exclude_event(struct perf_event *event,
4835 struct pt_regs *regs)
4836{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004837 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01004838 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004839
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004840 if (regs) {
4841 if (event->attr.exclude_user && user_mode(regs))
4842 return 1;
4843
4844 if (event->attr.exclude_kernel && !user_mode(regs))
4845 return 1;
4846 }
4847
4848 return 0;
4849}
4850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851static int perf_swevent_match(struct perf_event *event,
4852 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004853 u32 event_id,
4854 struct perf_sample_data *data,
4855 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857 if (event->attr.type != type)
4858 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004859
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004860 if (event->attr.config != event_id)
4861 return 0;
4862
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004863 if (perf_exclude_event(event, regs))
4864 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004865
4866 return 1;
4867}
4868
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004869static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004871 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004873 return hash_64(val, SWEVENT_HLIST_BITS);
4874}
4875
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004876static inline struct hlist_head *
4877__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004878{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004879 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004880
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004881 return &hlist->heads[hash];
4882}
4883
4884/* For the read side: events when they trigger */
4885static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004886find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004887{
4888 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004889
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004890 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004891 if (!hlist)
4892 return NULL;
4893
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004894 return __find_swevent_head(hlist, type, event_id);
4895}
4896
4897/* For the event head insertion and removal in the hlist */
4898static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004899find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004900{
4901 struct swevent_hlist *hlist;
4902 u32 event_id = event->attr.config;
4903 u64 type = event->attr.type;
4904
4905 /*
4906 * Event scheduling is always serialized against hlist allocation
4907 * and release. Which makes the protected version suitable here.
4908 * The context lock guarantees that.
4909 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004910 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004911 lockdep_is_held(&event->ctx->lock));
4912 if (!hlist)
4913 return NULL;
4914
4915 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004916}
4917
4918static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004919 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004920 struct perf_sample_data *data,
4921 struct pt_regs *regs)
4922{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004923 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004924 struct perf_event *event;
4925 struct hlist_node *node;
4926 struct hlist_head *head;
4927
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004928 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004929 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004930 if (!head)
4931 goto end;
4932
4933 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004934 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004935 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004936 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004937end:
4938 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004939}
4940
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004941int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004942{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004943 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004944
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004945 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004946}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004947EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004948
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004949inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004951 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004952
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004953 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004954}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004955
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004956void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004957{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004958 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004959 int rctx;
4960
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004961 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004962 rctx = perf_swevent_get_recursion_context();
4963 if (rctx < 0)
4964 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965
Robert Richterfd0d0002012-04-02 20:19:08 +02004966 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004967
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004968 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004969
4970 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004971 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972}
4973
4974static void perf_swevent_read(struct perf_event *event)
4975{
4976}
4977
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004978static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004979{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004980 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004981 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004982 struct hlist_head *head;
4983
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004984 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004985 hwc->last_period = hwc->sample_period;
4986 perf_swevent_set_period(event);
4987 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004988
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004989 hwc->state = !(flags & PERF_EF_START);
4990
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004991 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004992 if (WARN_ON_ONCE(!head))
4993 return -EINVAL;
4994
4995 hlist_add_head_rcu(&event->hlist_entry, head);
4996
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997 return 0;
4998}
4999
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005000static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005002 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003}
5004
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005005static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005006{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005007 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005008}
5009
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005010static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005011{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005012 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005013}
5014
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005015/* Deref the hlist from the update side */
5016static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005017swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005018{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005019 return rcu_dereference_protected(swhash->swevent_hlist,
5020 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005021}
5022
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005023static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005024{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005025 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005026
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005027 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005028 return;
5029
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005030 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005031 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005032}
5033
5034static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5035{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005036 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005037
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005038 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005039
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005040 if (!--swhash->hlist_refcount)
5041 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005042
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005043 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005044}
5045
5046static void swevent_hlist_put(struct perf_event *event)
5047{
5048 int cpu;
5049
5050 if (event->cpu != -1) {
5051 swevent_hlist_put_cpu(event, event->cpu);
5052 return;
5053 }
5054
5055 for_each_possible_cpu(cpu)
5056 swevent_hlist_put_cpu(event, cpu);
5057}
5058
5059static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5060{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005061 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005062 int err = 0;
5063
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005064 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005065
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005066 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005067 struct swevent_hlist *hlist;
5068
5069 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5070 if (!hlist) {
5071 err = -ENOMEM;
5072 goto exit;
5073 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005074 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005075 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005076 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005077exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005078 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005079
5080 return err;
5081}
5082
5083static int swevent_hlist_get(struct perf_event *event)
5084{
5085 int err;
5086 int cpu, failed_cpu;
5087
5088 if (event->cpu != -1)
5089 return swevent_hlist_get_cpu(event, event->cpu);
5090
5091 get_online_cpus();
5092 for_each_possible_cpu(cpu) {
5093 err = swevent_hlist_get_cpu(event, cpu);
5094 if (err) {
5095 failed_cpu = cpu;
5096 goto fail;
5097 }
5098 }
5099 put_online_cpus();
5100
5101 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005102fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005103 for_each_possible_cpu(cpu) {
5104 if (cpu == failed_cpu)
5105 break;
5106 swevent_hlist_put_cpu(event, cpu);
5107 }
5108
5109 put_online_cpus();
5110 return err;
5111}
5112
Ingo Molnarc5905af2012-02-24 08:31:31 +01005113struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005114
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005115static void sw_perf_event_destroy(struct perf_event *event)
5116{
5117 u64 event_id = event->attr.config;
5118
5119 WARN_ON(event->parent);
5120
Ingo Molnarc5905af2012-02-24 08:31:31 +01005121 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005122 swevent_hlist_put(event);
5123}
5124
5125static int perf_swevent_init(struct perf_event *event)
5126{
5127 int event_id = event->attr.config;
5128
5129 if (event->attr.type != PERF_TYPE_SOFTWARE)
5130 return -ENOENT;
5131
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005132 /*
5133 * no branch sampling for software events
5134 */
5135 if (has_branch_stack(event))
5136 return -EOPNOTSUPP;
5137
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005138 switch (event_id) {
5139 case PERF_COUNT_SW_CPU_CLOCK:
5140 case PERF_COUNT_SW_TASK_CLOCK:
5141 return -ENOENT;
5142
5143 default:
5144 break;
5145 }
5146
Dan Carpenterce677832010-10-24 21:50:42 +02005147 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005148 return -ENOENT;
5149
5150 if (!event->parent) {
5151 int err;
5152
5153 err = swevent_hlist_get(event);
5154 if (err)
5155 return err;
5156
Ingo Molnarc5905af2012-02-24 08:31:31 +01005157 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005158 event->destroy = sw_perf_event_destroy;
5159 }
5160
5161 return 0;
5162}
5163
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005164static int perf_swevent_event_idx(struct perf_event *event)
5165{
5166 return 0;
5167}
5168
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005169static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005170 .task_ctx_nr = perf_sw_context,
5171
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005172 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005173 .add = perf_swevent_add,
5174 .del = perf_swevent_del,
5175 .start = perf_swevent_start,
5176 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005177 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005178
5179 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005180};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005181
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005182#ifdef CONFIG_EVENT_TRACING
5183
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005184static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005185 struct perf_sample_data *data)
5186{
5187 void *record = data->raw->data;
5188
5189 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5190 return 1;
5191 return 0;
5192}
5193
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005194static int perf_tp_event_match(struct perf_event *event,
5195 struct perf_sample_data *data,
5196 struct pt_regs *regs)
5197{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005198 if (event->hw.state & PERF_HES_STOPPED)
5199 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005200 /*
5201 * All tracepoints are from kernel-space.
5202 */
5203 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005204 return 0;
5205
5206 if (!perf_tp_filter_match(event, data))
5207 return 0;
5208
5209 return 1;
5210}
5211
5212void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005213 struct pt_regs *regs, struct hlist_head *head, int rctx,
5214 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005215{
5216 struct perf_sample_data data;
5217 struct perf_event *event;
5218 struct hlist_node *node;
5219
5220 struct perf_raw_record raw = {
5221 .size = entry_size,
5222 .data = record,
5223 };
5224
Robert Richterfd0d0002012-04-02 20:19:08 +02005225 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005226 data.raw = &raw;
5227
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005228 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5229 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005230 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005231 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005232
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005233 /*
5234 * If we got specified a target task, also iterate its context and
5235 * deliver this event there too.
5236 */
5237 if (task && task != current) {
5238 struct perf_event_context *ctx;
5239 struct trace_entry *entry = record;
5240
5241 rcu_read_lock();
5242 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5243 if (!ctx)
5244 goto unlock;
5245
5246 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5247 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5248 continue;
5249 if (event->attr.config != entry->type)
5250 continue;
5251 if (perf_tp_event_match(event, &data, regs))
5252 perf_swevent_event(event, count, &data, regs);
5253 }
5254unlock:
5255 rcu_read_unlock();
5256 }
5257
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005258 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005259}
5260EXPORT_SYMBOL_GPL(perf_tp_event);
5261
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005262static void tp_perf_event_destroy(struct perf_event *event)
5263{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005264 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265}
5266
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005267static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005269 int err;
5270
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005271 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5272 return -ENOENT;
5273
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005274 /*
5275 * no branch sampling for tracepoint events
5276 */
5277 if (has_branch_stack(event))
5278 return -EOPNOTSUPP;
5279
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005280 err = perf_trace_init(event);
5281 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005282 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283
5284 event->destroy = tp_perf_event_destroy;
5285
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005286 return 0;
5287}
5288
5289static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005290 .task_ctx_nr = perf_sw_context,
5291
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005292 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005293 .add = perf_trace_add,
5294 .del = perf_trace_del,
5295 .start = perf_swevent_start,
5296 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005297 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005298
5299 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005300};
5301
5302static inline void perf_tp_register(void)
5303{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005304 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005305}
Li Zefan6fb29152009-10-15 11:21:42 +08005306
5307static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5308{
5309 char *filter_str;
5310 int ret;
5311
5312 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5313 return -EINVAL;
5314
5315 filter_str = strndup_user(arg, PAGE_SIZE);
5316 if (IS_ERR(filter_str))
5317 return PTR_ERR(filter_str);
5318
5319 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5320
5321 kfree(filter_str);
5322 return ret;
5323}
5324
5325static void perf_event_free_filter(struct perf_event *event)
5326{
5327 ftrace_profile_free_filter(event);
5328}
5329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005330#else
Li Zefan6fb29152009-10-15 11:21:42 +08005331
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005332static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005333{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005334}
Li Zefan6fb29152009-10-15 11:21:42 +08005335
5336static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5337{
5338 return -ENOENT;
5339}
5340
5341static void perf_event_free_filter(struct perf_event *event)
5342{
5343}
5344
Li Zefan07b139c2009-12-21 14:27:35 +08005345#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005346
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005347#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005348void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005349{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005350 struct perf_sample_data sample;
5351 struct pt_regs *regs = data;
5352
Robert Richterfd0d0002012-04-02 20:19:08 +02005353 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005354
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005355 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005356 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005357}
5358#endif
5359
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005360/*
5361 * hrtimer based swevent callback
5362 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005364static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005366 enum hrtimer_restart ret = HRTIMER_RESTART;
5367 struct perf_sample_data data;
5368 struct pt_regs *regs;
5369 struct perf_event *event;
5370 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005371
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005372 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005373
5374 if (event->state != PERF_EVENT_STATE_ACTIVE)
5375 return HRTIMER_NORESTART;
5376
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005377 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378
Robert Richterfd0d0002012-04-02 20:19:08 +02005379 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005380 regs = get_irq_regs();
5381
5382 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005383 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02005384 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005385 ret = HRTIMER_NORESTART;
5386 }
5387
5388 period = max_t(u64, 10000, event->hw.sample_period);
5389 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5390
5391 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392}
5393
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005394static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005395{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005396 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005397 s64 period;
5398
5399 if (!is_sampling_event(event))
5400 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005401
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005402 period = local64_read(&hwc->period_left);
5403 if (period) {
5404 if (period < 0)
5405 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005406
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005407 local64_set(&hwc->period_left, 0);
5408 } else {
5409 period = max_t(u64, 10000, hwc->sample_period);
5410 }
5411 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005412 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005413 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005414}
5415
5416static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5417{
5418 struct hw_perf_event *hwc = &event->hw;
5419
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005420 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005421 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005422 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005423
5424 hrtimer_cancel(&hwc->hrtimer);
5425 }
5426}
5427
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005428static void perf_swevent_init_hrtimer(struct perf_event *event)
5429{
5430 struct hw_perf_event *hwc = &event->hw;
5431
5432 if (!is_sampling_event(event))
5433 return;
5434
5435 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5436 hwc->hrtimer.function = perf_swevent_hrtimer;
5437
5438 /*
5439 * Since hrtimers have a fixed rate, we can do a static freq->period
5440 * mapping and avoid the whole period adjust feedback stuff.
5441 */
5442 if (event->attr.freq) {
5443 long freq = event->attr.sample_freq;
5444
5445 event->attr.sample_period = NSEC_PER_SEC / freq;
5446 hwc->sample_period = event->attr.sample_period;
5447 local64_set(&hwc->period_left, hwc->sample_period);
5448 event->attr.freq = 0;
5449 }
5450}
5451
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005452/*
5453 * Software event: cpu wall time clock
5454 */
5455
5456static void cpu_clock_event_update(struct perf_event *event)
5457{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005458 s64 prev;
5459 u64 now;
5460
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005461 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005462 prev = local64_xchg(&event->hw.prev_count, now);
5463 local64_add(now - prev, &event->count);
5464}
5465
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005466static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005467{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005468 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005469 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005470}
5471
5472static void cpu_clock_event_stop(struct perf_event *event, int flags)
5473{
5474 perf_swevent_cancel_hrtimer(event);
5475 cpu_clock_event_update(event);
5476}
5477
5478static int cpu_clock_event_add(struct perf_event *event, int flags)
5479{
5480 if (flags & PERF_EF_START)
5481 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005482
5483 return 0;
5484}
5485
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005486static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005487{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005488 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005489}
5490
5491static void cpu_clock_event_read(struct perf_event *event)
5492{
5493 cpu_clock_event_update(event);
5494}
5495
5496static int cpu_clock_event_init(struct perf_event *event)
5497{
5498 if (event->attr.type != PERF_TYPE_SOFTWARE)
5499 return -ENOENT;
5500
5501 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5502 return -ENOENT;
5503
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005504 /*
5505 * no branch sampling for software events
5506 */
5507 if (has_branch_stack(event))
5508 return -EOPNOTSUPP;
5509
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005510 perf_swevent_init_hrtimer(event);
5511
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005512 return 0;
5513}
5514
5515static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005516 .task_ctx_nr = perf_sw_context,
5517
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005518 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005519 .add = cpu_clock_event_add,
5520 .del = cpu_clock_event_del,
5521 .start = cpu_clock_event_start,
5522 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005523 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005524
5525 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005526};
5527
5528/*
5529 * Software event: task time clock
5530 */
5531
5532static void task_clock_event_update(struct perf_event *event, u64 now)
5533{
5534 u64 prev;
5535 s64 delta;
5536
5537 prev = local64_xchg(&event->hw.prev_count, now);
5538 delta = now - prev;
5539 local64_add(delta, &event->count);
5540}
5541
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005542static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005543{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005544 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005545 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005546}
5547
5548static void task_clock_event_stop(struct perf_event *event, int flags)
5549{
5550 perf_swevent_cancel_hrtimer(event);
5551 task_clock_event_update(event, event->ctx->time);
5552}
5553
5554static int task_clock_event_add(struct perf_event *event, int flags)
5555{
5556 if (flags & PERF_EF_START)
5557 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005558
5559 return 0;
5560}
5561
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005562static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005563{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005564 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005565}
5566
5567static void task_clock_event_read(struct perf_event *event)
5568{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005569 u64 now = perf_clock();
5570 u64 delta = now - event->ctx->timestamp;
5571 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005572
5573 task_clock_event_update(event, time);
5574}
5575
5576static int task_clock_event_init(struct perf_event *event)
5577{
5578 if (event->attr.type != PERF_TYPE_SOFTWARE)
5579 return -ENOENT;
5580
5581 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5582 return -ENOENT;
5583
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005584 /*
5585 * no branch sampling for software events
5586 */
5587 if (has_branch_stack(event))
5588 return -EOPNOTSUPP;
5589
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005590 perf_swevent_init_hrtimer(event);
5591
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005592 return 0;
5593}
5594
5595static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005596 .task_ctx_nr = perf_sw_context,
5597
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005598 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005599 .add = task_clock_event_add,
5600 .del = task_clock_event_del,
5601 .start = task_clock_event_start,
5602 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005603 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005604
5605 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005606};
5607
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005608static void perf_pmu_nop_void(struct pmu *pmu)
5609{
5610}
5611
5612static int perf_pmu_nop_int(struct pmu *pmu)
5613{
5614 return 0;
5615}
5616
5617static void perf_pmu_start_txn(struct pmu *pmu)
5618{
5619 perf_pmu_disable(pmu);
5620}
5621
5622static int perf_pmu_commit_txn(struct pmu *pmu)
5623{
5624 perf_pmu_enable(pmu);
5625 return 0;
5626}
5627
5628static void perf_pmu_cancel_txn(struct pmu *pmu)
5629{
5630 perf_pmu_enable(pmu);
5631}
5632
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005633static int perf_event_idx_default(struct perf_event *event)
5634{
5635 return event->hw.idx + 1;
5636}
5637
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005638/*
5639 * Ensures all contexts with the same task_ctx_nr have the same
5640 * pmu_cpu_context too.
5641 */
5642static void *find_pmu_context(int ctxn)
5643{
5644 struct pmu *pmu;
5645
5646 if (ctxn < 0)
5647 return NULL;
5648
5649 list_for_each_entry(pmu, &pmus, entry) {
5650 if (pmu->task_ctx_nr == ctxn)
5651 return pmu->pmu_cpu_context;
5652 }
5653
5654 return NULL;
5655}
5656
Peter Zijlstra51676952010-12-07 14:18:20 +01005657static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005658{
Peter Zijlstra51676952010-12-07 14:18:20 +01005659 int cpu;
5660
5661 for_each_possible_cpu(cpu) {
5662 struct perf_cpu_context *cpuctx;
5663
5664 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5665
5666 if (cpuctx->active_pmu == old_pmu)
5667 cpuctx->active_pmu = pmu;
5668 }
5669}
5670
5671static void free_pmu_context(struct pmu *pmu)
5672{
5673 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005674
5675 mutex_lock(&pmus_lock);
5676 /*
5677 * Like a real lame refcount.
5678 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005679 list_for_each_entry(i, &pmus, entry) {
5680 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5681 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005682 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005683 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005684 }
5685
Peter Zijlstra51676952010-12-07 14:18:20 +01005686 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005687out:
5688 mutex_unlock(&pmus_lock);
5689}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005690static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005691
Peter Zijlstraabe43402010-11-17 23:17:37 +01005692static ssize_t
5693type_show(struct device *dev, struct device_attribute *attr, char *page)
5694{
5695 struct pmu *pmu = dev_get_drvdata(dev);
5696
5697 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5698}
5699
5700static struct device_attribute pmu_dev_attrs[] = {
5701 __ATTR_RO(type),
5702 __ATTR_NULL,
5703};
5704
5705static int pmu_bus_running;
5706static struct bus_type pmu_bus = {
5707 .name = "event_source",
5708 .dev_attrs = pmu_dev_attrs,
5709};
5710
5711static void pmu_dev_release(struct device *dev)
5712{
5713 kfree(dev);
5714}
5715
5716static int pmu_dev_alloc(struct pmu *pmu)
5717{
5718 int ret = -ENOMEM;
5719
5720 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5721 if (!pmu->dev)
5722 goto out;
5723
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01005724 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01005725 device_initialize(pmu->dev);
5726 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5727 if (ret)
5728 goto free_dev;
5729
5730 dev_set_drvdata(pmu->dev, pmu);
5731 pmu->dev->bus = &pmu_bus;
5732 pmu->dev->release = pmu_dev_release;
5733 ret = device_add(pmu->dev);
5734 if (ret)
5735 goto free_dev;
5736
5737out:
5738 return ret;
5739
5740free_dev:
5741 put_device(pmu->dev);
5742 goto out;
5743}
5744
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005745static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005746static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005747
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005748int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005749{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005750 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005751
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005752 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005753 ret = -ENOMEM;
5754 pmu->pmu_disable_count = alloc_percpu(int);
5755 if (!pmu->pmu_disable_count)
5756 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005757
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005758 pmu->type = -1;
5759 if (!name)
5760 goto skip_type;
5761 pmu->name = name;
5762
5763 if (type < 0) {
5764 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5765 if (!err)
5766 goto free_pdc;
5767
5768 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5769 if (err) {
5770 ret = err;
5771 goto free_pdc;
5772 }
5773 }
5774 pmu->type = type;
5775
Peter Zijlstraabe43402010-11-17 23:17:37 +01005776 if (pmu_bus_running) {
5777 ret = pmu_dev_alloc(pmu);
5778 if (ret)
5779 goto free_idr;
5780 }
5781
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005782skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005783 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5784 if (pmu->pmu_cpu_context)
5785 goto got_cpu_context;
5786
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005787 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5788 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005789 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005790
5791 for_each_possible_cpu(cpu) {
5792 struct perf_cpu_context *cpuctx;
5793
5794 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005795 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005796 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005797 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005798 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005799 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005800 cpuctx->jiffies_interval = 1;
5801 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005802 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005803 }
5804
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005805got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005806 if (!pmu->start_txn) {
5807 if (pmu->pmu_enable) {
5808 /*
5809 * If we have pmu_enable/pmu_disable calls, install
5810 * transaction stubs that use that to try and batch
5811 * hardware accesses.
5812 */
5813 pmu->start_txn = perf_pmu_start_txn;
5814 pmu->commit_txn = perf_pmu_commit_txn;
5815 pmu->cancel_txn = perf_pmu_cancel_txn;
5816 } else {
5817 pmu->start_txn = perf_pmu_nop_void;
5818 pmu->commit_txn = perf_pmu_nop_int;
5819 pmu->cancel_txn = perf_pmu_nop_void;
5820 }
5821 }
5822
5823 if (!pmu->pmu_enable) {
5824 pmu->pmu_enable = perf_pmu_nop_void;
5825 pmu->pmu_disable = perf_pmu_nop_void;
5826 }
5827
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005828 if (!pmu->event_idx)
5829 pmu->event_idx = perf_event_idx_default;
5830
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005831 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005832 ret = 0;
5833unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005834 mutex_unlock(&pmus_lock);
5835
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005836 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005837
Peter Zijlstraabe43402010-11-17 23:17:37 +01005838free_dev:
5839 device_del(pmu->dev);
5840 put_device(pmu->dev);
5841
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005842free_idr:
5843 if (pmu->type >= PERF_TYPE_MAX)
5844 idr_remove(&pmu_idr, pmu->type);
5845
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005846free_pdc:
5847 free_percpu(pmu->pmu_disable_count);
5848 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005849}
5850
5851void perf_pmu_unregister(struct pmu *pmu)
5852{
5853 mutex_lock(&pmus_lock);
5854 list_del_rcu(&pmu->entry);
5855 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856
5857 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005858 * We dereference the pmu list under both SRCU and regular RCU, so
5859 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005860 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005861 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005862 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005863
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005864 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005865 if (pmu->type >= PERF_TYPE_MAX)
5866 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01005867 device_del(pmu->dev);
5868 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01005869 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005870}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005871
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005872struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005873{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005874 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005875 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08005876 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005877
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005878 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005879
5880 rcu_read_lock();
5881 pmu = idr_find(&pmu_idr, event->attr.type);
5882 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08005883 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005884 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005885 ret = pmu->event_init(event);
5886 if (ret)
5887 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005888 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08005889 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005890
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005891 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005892 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005893 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005894 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005895 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005896
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005897 if (ret != -ENOENT) {
5898 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005899 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005902 pmu = ERR_PTR(-ENOENT);
5903unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005904 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005905
5906 return pmu;
5907}
5908
5909/*
5910 * Allocate and initialize a event structure
5911 */
5912static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005913perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005914 struct task_struct *task,
5915 struct perf_event *group_leader,
5916 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03005917 perf_overflow_handler_t overflow_handler,
5918 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005920 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921 struct perf_event *event;
5922 struct hw_perf_event *hwc;
5923 long err;
5924
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005925 if ((unsigned)cpu >= nr_cpu_ids) {
5926 if (!task || cpu != -1)
5927 return ERR_PTR(-EINVAL);
5928 }
5929
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005930 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931 if (!event)
5932 return ERR_PTR(-ENOMEM);
5933
5934 /*
5935 * Single events are their own group leaders, with an
5936 * empty sibling list:
5937 */
5938 if (!group_leader)
5939 group_leader = event;
5940
5941 mutex_init(&event->child_mutex);
5942 INIT_LIST_HEAD(&event->child_list);
5943
5944 INIT_LIST_HEAD(&event->group_entry);
5945 INIT_LIST_HEAD(&event->event_entry);
5946 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005947 INIT_LIST_HEAD(&event->rb_entry);
5948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005950 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005951
5952 mutex_init(&event->mmap_mutex);
5953
Al Viroa6fa9412012-08-20 14:59:25 +01005954 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005955 event->cpu = cpu;
5956 event->attr = *attr;
5957 event->group_leader = group_leader;
5958 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959 event->oncpu = -1;
5960
5961 event->parent = parent_event;
5962
5963 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5964 event->id = atomic64_inc_return(&perf_event_id);
5965
5966 event->state = PERF_EVENT_STATE_INACTIVE;
5967
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005968 if (task) {
5969 event->attach_state = PERF_ATTACH_TASK;
5970#ifdef CONFIG_HAVE_HW_BREAKPOINT
5971 /*
5972 * hw_breakpoint is a bit difficult here..
5973 */
5974 if (attr->type == PERF_TYPE_BREAKPOINT)
5975 event->hw.bp_target = task;
5976#endif
5977 }
5978
Avi Kivity4dc0da82011-06-29 18:42:35 +03005979 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005980 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005981 context = parent_event->overflow_handler_context;
5982 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005983
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005984 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005985 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005986
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005987 if (attr->disabled)
5988 event->state = PERF_EVENT_STATE_OFF;
5989
5990 pmu = NULL;
5991
5992 hwc = &event->hw;
5993 hwc->sample_period = attr->sample_period;
5994 if (attr->freq && attr->sample_freq)
5995 hwc->sample_period = 1;
5996 hwc->last_period = hwc->sample_period;
5997
Peter Zijlstrae7850592010-05-21 14:43:08 +02005998 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005999
6000 /*
6001 * we currently do not support PERF_FORMAT_GROUP on inherited events
6002 */
6003 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6004 goto done;
6005
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006006 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006008done:
6009 err = 0;
6010 if (!pmu)
6011 err = -EINVAL;
6012 else if (IS_ERR(pmu))
6013 err = PTR_ERR(pmu);
6014
6015 if (err) {
6016 if (event->ns)
6017 put_pid_ns(event->ns);
6018 kfree(event);
6019 return ERR_PTR(err);
6020 }
6021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006022 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006023 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01006024 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01006025 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026 atomic_inc(&nr_mmap_events);
6027 if (event->attr.comm)
6028 atomic_inc(&nr_comm_events);
6029 if (event->attr.task)
6030 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006031 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6032 err = get_callchain_buffers();
6033 if (err) {
6034 free_event(event);
6035 return ERR_PTR(err);
6036 }
6037 }
Stephane Eraniand010b332012-02-09 23:21:00 +01006038 if (has_branch_stack(event)) {
6039 static_key_slow_inc(&perf_sched_events.key);
6040 if (!(event->attach_state & PERF_ATTACH_TASK))
6041 atomic_inc(&per_cpu(perf_branch_stack_events,
6042 event->cpu));
6043 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006044 }
6045
6046 return event;
6047}
6048
6049static int perf_copy_attr(struct perf_event_attr __user *uattr,
6050 struct perf_event_attr *attr)
6051{
6052 u32 size;
6053 int ret;
6054
6055 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6056 return -EFAULT;
6057
6058 /*
6059 * zero the full structure, so that a short copy will be nice.
6060 */
6061 memset(attr, 0, sizeof(*attr));
6062
6063 ret = get_user(size, &uattr->size);
6064 if (ret)
6065 return ret;
6066
6067 if (size > PAGE_SIZE) /* silly large */
6068 goto err_size;
6069
6070 if (!size) /* abi compat */
6071 size = PERF_ATTR_SIZE_VER0;
6072
6073 if (size < PERF_ATTR_SIZE_VER0)
6074 goto err_size;
6075
6076 /*
6077 * If we're handed a bigger struct than we know of,
6078 * ensure all the unknown bits are 0 - i.e. new
6079 * user-space does not rely on any kernel feature
6080 * extensions we dont know about yet.
6081 */
6082 if (size > sizeof(*attr)) {
6083 unsigned char __user *addr;
6084 unsigned char __user *end;
6085 unsigned char val;
6086
6087 addr = (void __user *)uattr + sizeof(*attr);
6088 end = (void __user *)uattr + size;
6089
6090 for (; addr < end; addr++) {
6091 ret = get_user(val, addr);
6092 if (ret)
6093 return ret;
6094 if (val)
6095 goto err_size;
6096 }
6097 size = sizeof(*attr);
6098 }
6099
6100 ret = copy_from_user(attr, uattr, size);
6101 if (ret)
6102 return -EFAULT;
6103
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306104 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006105 return -EINVAL;
6106
6107 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6108 return -EINVAL;
6109
6110 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6111 return -EINVAL;
6112
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006113 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6114 u64 mask = attr->branch_sample_type;
6115
6116 /* only using defined bits */
6117 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6118 return -EINVAL;
6119
6120 /* at least one branch bit must be set */
6121 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6122 return -EINVAL;
6123
6124 /* kernel level capture: check permissions */
6125 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6126 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6127 return -EACCES;
6128
6129 /* propagate priv level, when not set for branch */
6130 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6131
6132 /* exclude_kernel checked on syscall entry */
6133 if (!attr->exclude_kernel)
6134 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6135
6136 if (!attr->exclude_user)
6137 mask |= PERF_SAMPLE_BRANCH_USER;
6138
6139 if (!attr->exclude_hv)
6140 mask |= PERF_SAMPLE_BRANCH_HV;
6141 /*
6142 * adjust user setting (for HW filter setup)
6143 */
6144 attr->branch_sample_type = mask;
6145 }
6146 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006147out:
6148 return ret;
6149
6150err_size:
6151 put_user(sizeof(*attr), &uattr->size);
6152 ret = -E2BIG;
6153 goto out;
6154}
6155
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006156static int
6157perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006158{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006159 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006160 int ret = -EINVAL;
6161
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006162 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006163 goto set;
6164
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006165 /* don't allow circular references */
6166 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006167 goto out;
6168
Peter Zijlstra0f139302010-05-20 14:35:15 +02006169 /*
6170 * Don't allow cross-cpu buffers
6171 */
6172 if (output_event->cpu != event->cpu)
6173 goto out;
6174
6175 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006176 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006177 */
6178 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6179 goto out;
6180
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006181set:
6182 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006183 /* Can't redirect output if we've got an active mmap() */
6184 if (atomic_read(&event->mmap_count))
6185 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006186
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006187 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006188 /* get the rb we want to redirect to */
6189 rb = ring_buffer_get(output_event);
6190 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006191 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006192 }
6193
Frederic Weisbecker76369132011-05-19 19:55:04 +02006194 old_rb = event->rb;
6195 rcu_assign_pointer(event->rb, rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006196 if (old_rb)
6197 ring_buffer_detach(event, old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006198 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006199unlock:
6200 mutex_unlock(&event->mmap_mutex);
6201
Frederic Weisbecker76369132011-05-19 19:55:04 +02006202 if (old_rb)
6203 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006204out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006205 return ret;
6206}
6207
6208/**
6209 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6210 *
6211 * @attr_uptr: event_id type attributes for monitoring/sampling
6212 * @pid: target pid
6213 * @cpu: target cpu
6214 * @group_fd: group leader event fd
6215 */
6216SYSCALL_DEFINE5(perf_event_open,
6217 struct perf_event_attr __user *, attr_uptr,
6218 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6219{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006220 struct perf_event *group_leader = NULL, *output_event = NULL;
6221 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006222 struct perf_event_attr attr;
6223 struct perf_event_context *ctx;
6224 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006225 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006226 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006227 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006228 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006229 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006230 int err;
6231
6232 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006233 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006234 return -EINVAL;
6235
6236 err = perf_copy_attr(attr_uptr, &attr);
6237 if (err)
6238 return err;
6239
6240 if (!attr.exclude_kernel) {
6241 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6242 return -EACCES;
6243 }
6244
6245 if (attr.freq) {
6246 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6247 return -EINVAL;
6248 }
6249
Stephane Eraniane5d13672011-02-14 11:20:01 +02006250 /*
6251 * In cgroup mode, the pid argument is used to pass the fd
6252 * opened to the cgroup directory in cgroupfs. The cpu argument
6253 * designates the cpu on which to monitor threads from that
6254 * cgroup.
6255 */
6256 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6257 return -EINVAL;
6258
Al Viroab72a702012-08-21 09:40:46 -04006259 event_fd = get_unused_fd();
Al Viroea635c62010-05-26 17:40:29 -04006260 if (event_fd < 0)
6261 return event_fd;
6262
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006263 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04006264 err = perf_fget_light(group_fd, &group);
6265 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006266 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04006267 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006268 if (flags & PERF_FLAG_FD_OUTPUT)
6269 output_event = group_leader;
6270 if (flags & PERF_FLAG_FD_NO_GROUP)
6271 group_leader = NULL;
6272 }
6273
Stephane Eraniane5d13672011-02-14 11:20:01 +02006274 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006275 task = find_lively_task_by_vpid(pid);
6276 if (IS_ERR(task)) {
6277 err = PTR_ERR(task);
6278 goto err_group_fd;
6279 }
6280 }
6281
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006282 get_online_cpus();
6283
Avi Kivity4dc0da82011-06-29 18:42:35 +03006284 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6285 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006286 if (IS_ERR(event)) {
6287 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006288 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006289 }
6290
Stephane Eraniane5d13672011-02-14 11:20:01 +02006291 if (flags & PERF_FLAG_PID_CGROUP) {
6292 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6293 if (err)
6294 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006295 /*
6296 * one more event:
6297 * - that has cgroup constraint on event->cpu
6298 * - that may need work on context switch
6299 */
6300 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006301 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006302 }
6303
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006304 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006305 * Special case software events and allow them to be part of
6306 * any hardware group.
6307 */
6308 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006309
6310 if (group_leader &&
6311 (is_software_event(event) != is_software_event(group_leader))) {
6312 if (is_software_event(event)) {
6313 /*
6314 * If event and group_leader are not both a software
6315 * event, and event is, then group leader is not.
6316 *
6317 * Allow the addition of software events to !software
6318 * groups, this is safe because software events never
6319 * fail to schedule.
6320 */
6321 pmu = group_leader->pmu;
6322 } else if (is_software_event(group_leader) &&
6323 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6324 /*
6325 * In case the group is a pure software group, and we
6326 * try to add a hardware event, move the whole group to
6327 * the hardware context.
6328 */
6329 move_group = 1;
6330 }
6331 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006332
6333 /*
6334 * Get the target context (task or percpu):
6335 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006336 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006337 if (IS_ERR(ctx)) {
6338 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006339 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006340 }
6341
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006342 if (task) {
6343 put_task_struct(task);
6344 task = NULL;
6345 }
6346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347 /*
6348 * Look up the group leader (we will attach this event to it):
6349 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006350 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006351 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006353 /*
6354 * Do not allow a recursive hierarchy (this new sibling
6355 * becoming part of another group-sibling):
6356 */
6357 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006358 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006359 /*
6360 * Do not allow to attach to a group in a different
6361 * task or CPU context:
6362 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006363 if (move_group) {
6364 if (group_leader->ctx->type != ctx->type)
6365 goto err_context;
6366 } else {
6367 if (group_leader->ctx != ctx)
6368 goto err_context;
6369 }
6370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006371 /*
6372 * Only a group leader can be exclusive or pinned
6373 */
6374 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006375 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006376 }
6377
6378 if (output_event) {
6379 err = perf_event_set_output(event, output_event);
6380 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006381 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006382 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006383
Al Viroea635c62010-05-26 17:40:29 -04006384 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6385 if (IS_ERR(event_file)) {
6386 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006387 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006388 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006389
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006390 if (move_group) {
6391 struct perf_event_context *gctx = group_leader->ctx;
6392
6393 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006394 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006395 list_for_each_entry(sibling, &group_leader->sibling_list,
6396 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006397 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006398 put_ctx(gctx);
6399 }
6400 mutex_unlock(&gctx->mutex);
6401 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006402 }
6403
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 WARN_ON_ONCE(ctx->parent_ctx);
6405 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006406
6407 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006408 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006409 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006410 get_ctx(ctx);
6411 list_for_each_entry(sibling, &group_leader->sibling_list,
6412 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006413 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006414 get_ctx(ctx);
6415 }
6416 }
6417
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006418 perf_install_in_context(ctx, event, event->cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006419 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006420 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421 mutex_unlock(&ctx->mutex);
6422
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006423 put_online_cpus();
6424
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006425 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006427 mutex_lock(&current->perf_event_mutex);
6428 list_add_tail(&event->owner_entry, &current->perf_event_list);
6429 mutex_unlock(&current->perf_event_mutex);
6430
Peter Zijlstra8a495422010-05-27 15:47:49 +02006431 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006432 * Precalculate sample_data sizes
6433 */
6434 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006435 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006436
6437 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006438 * Drop the reference on the group_event after placing the
6439 * new event on the sibling_list. This ensures destruction
6440 * of the group leader will find the pointer to itself in
6441 * perf_group_detach().
6442 */
Al Viro2903ff02012-08-28 12:52:22 -04006443 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006444 fd_install(event_fd, event_file);
6445 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006446
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006447err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006448 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006449 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006450err_alloc:
6451 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006452err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006453 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006454 if (task)
6455 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006456err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04006457 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006458err_fd:
6459 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006460 return err;
6461}
6462
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006463/**
6464 * perf_event_create_kernel_counter
6465 *
6466 * @attr: attributes of the counter to create
6467 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006468 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006469 */
6470struct perf_event *
6471perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006472 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006473 perf_overflow_handler_t overflow_handler,
6474 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006475{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006476 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006477 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006478 int err;
6479
6480 /*
6481 * Get the target context (task or percpu):
6482 */
6483
Avi Kivity4dc0da82011-06-29 18:42:35 +03006484 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6485 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006486 if (IS_ERR(event)) {
6487 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006488 goto err;
6489 }
6490
Matt Helsley38a81da2010-09-13 13:01:20 -07006491 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006492 if (IS_ERR(ctx)) {
6493 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006494 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006495 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006496
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006497 WARN_ON_ONCE(ctx->parent_ctx);
6498 mutex_lock(&ctx->mutex);
6499 perf_install_in_context(ctx, event, cpu);
6500 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006501 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006502 mutex_unlock(&ctx->mutex);
6503
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006504 return event;
6505
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006506err_free:
6507 free_event(event);
6508err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006509 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006510}
6511EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6512
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006513void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
6514{
6515 struct perf_event_context *src_ctx;
6516 struct perf_event_context *dst_ctx;
6517 struct perf_event *event, *tmp;
6518 LIST_HEAD(events);
6519
6520 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
6521 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
6522
6523 mutex_lock(&src_ctx->mutex);
6524 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
6525 event_entry) {
6526 perf_remove_from_context(event);
6527 put_ctx(src_ctx);
6528 list_add(&event->event_entry, &events);
6529 }
6530 mutex_unlock(&src_ctx->mutex);
6531
6532 synchronize_rcu();
6533
6534 mutex_lock(&dst_ctx->mutex);
6535 list_for_each_entry_safe(event, tmp, &events, event_entry) {
6536 list_del(&event->event_entry);
6537 if (event->state >= PERF_EVENT_STATE_OFF)
6538 event->state = PERF_EVENT_STATE_INACTIVE;
6539 perf_install_in_context(dst_ctx, event, dst_cpu);
6540 get_ctx(dst_ctx);
6541 }
6542 mutex_unlock(&dst_ctx->mutex);
6543}
6544EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
6545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546static void sync_child_event(struct perf_event *child_event,
6547 struct task_struct *child)
6548{
6549 struct perf_event *parent_event = child_event->parent;
6550 u64 child_val;
6551
6552 if (child_event->attr.inherit_stat)
6553 perf_event_read_event(child_event, child);
6554
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006555 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006556
6557 /*
6558 * Add back the child's count to the parent's count:
6559 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006560 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006561 atomic64_add(child_event->total_time_enabled,
6562 &parent_event->child_total_time_enabled);
6563 atomic64_add(child_event->total_time_running,
6564 &parent_event->child_total_time_running);
6565
6566 /*
6567 * Remove this event from the parent's list
6568 */
6569 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6570 mutex_lock(&parent_event->child_mutex);
6571 list_del_init(&child_event->child_list);
6572 mutex_unlock(&parent_event->child_mutex);
6573
6574 /*
6575 * Release the parent event, if this was the last
6576 * reference to it.
6577 */
Al Viroa6fa9412012-08-20 14:59:25 +01006578 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006579}
6580
6581static void
6582__perf_event_exit_task(struct perf_event *child_event,
6583 struct perf_event_context *child_ctx,
6584 struct task_struct *child)
6585{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006586 if (child_event->parent) {
6587 raw_spin_lock_irq(&child_ctx->lock);
6588 perf_group_detach(child_event);
6589 raw_spin_unlock_irq(&child_ctx->lock);
6590 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006591
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006592 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006593
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006594 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006595 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006596 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006597 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006598 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006599 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600 sync_child_event(child_event, child);
6601 free_event(child_event);
6602 }
6603}
6604
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006605static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006606{
6607 struct perf_event *child_event, *tmp;
6608 struct perf_event_context *child_ctx;
6609 unsigned long flags;
6610
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006611 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006612 perf_event_task(child, NULL, 0);
6613 return;
6614 }
6615
6616 local_irq_save(flags);
6617 /*
6618 * We can't reschedule here because interrupts are disabled,
6619 * and either child is current or it is a task that can't be
6620 * scheduled, so we are now safe from rescheduling changing
6621 * our context.
6622 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006623 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006624
6625 /*
6626 * Take the context lock here so that if find_get_context is
6627 * reading child->perf_event_ctxp, we wait until it has
6628 * incremented the context's refcount before we do put_ctx below.
6629 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006630 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006631 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006632 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006633 /*
6634 * If this context is a clone; unclone it so it can't get
6635 * swapped to another process while we're removing all
6636 * the events from it.
6637 */
6638 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006639 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006640 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006641
6642 /*
6643 * Report the task dead after unscheduling the events so that we
6644 * won't get any samples after PERF_RECORD_EXIT. We can however still
6645 * get a few PERF_RECORD_READ events.
6646 */
6647 perf_event_task(child, child_ctx, 0);
6648
6649 /*
6650 * We can recurse on the same lock type through:
6651 *
6652 * __perf_event_exit_task()
6653 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01006654 * put_event()
6655 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006656 *
6657 * But since its the parent context it won't be the same instance.
6658 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006659 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006660
6661again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006662 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6663 group_entry)
6664 __perf_event_exit_task(child_event, child_ctx, child);
6665
6666 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006667 group_entry)
6668 __perf_event_exit_task(child_event, child_ctx, child);
6669
6670 /*
6671 * If the last event was a group event, it will have appended all
6672 * its siblings to the list, but we obtained 'tmp' before that which
6673 * will still point to the list head terminating the iteration.
6674 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006675 if (!list_empty(&child_ctx->pinned_groups) ||
6676 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677 goto again;
6678
6679 mutex_unlock(&child_ctx->mutex);
6680
6681 put_ctx(child_ctx);
6682}
6683
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006684/*
6685 * When a child task exits, feed back event values to parent events.
6686 */
6687void perf_event_exit_task(struct task_struct *child)
6688{
Peter Zijlstra88821352010-11-09 19:01:43 +01006689 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006690 int ctxn;
6691
Peter Zijlstra88821352010-11-09 19:01:43 +01006692 mutex_lock(&child->perf_event_mutex);
6693 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6694 owner_entry) {
6695 list_del_init(&event->owner_entry);
6696
6697 /*
6698 * Ensure the list deletion is visible before we clear
6699 * the owner, closes a race against perf_release() where
6700 * we need to serialize on the owner->perf_event_mutex.
6701 */
6702 smp_wmb();
6703 event->owner = NULL;
6704 }
6705 mutex_unlock(&child->perf_event_mutex);
6706
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006707 for_each_task_context_nr(ctxn)
6708 perf_event_exit_task_context(child, ctxn);
6709}
6710
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006711static void perf_free_event(struct perf_event *event,
6712 struct perf_event_context *ctx)
6713{
6714 struct perf_event *parent = event->parent;
6715
6716 if (WARN_ON_ONCE(!parent))
6717 return;
6718
6719 mutex_lock(&parent->child_mutex);
6720 list_del_init(&event->child_list);
6721 mutex_unlock(&parent->child_mutex);
6722
Al Viroa6fa9412012-08-20 14:59:25 +01006723 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006724
Peter Zijlstra8a495422010-05-27 15:47:49 +02006725 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006726 list_del_event(event, ctx);
6727 free_event(event);
6728}
6729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006730/*
6731 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006732 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733 */
6734void perf_event_free_task(struct task_struct *task)
6735{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006736 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006737 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006738 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006739
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006740 for_each_task_context_nr(ctxn) {
6741 ctx = task->perf_event_ctxp[ctxn];
6742 if (!ctx)
6743 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006745 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006746again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006747 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6748 group_entry)
6749 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006751 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6752 group_entry)
6753 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006754
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006755 if (!list_empty(&ctx->pinned_groups) ||
6756 !list_empty(&ctx->flexible_groups))
6757 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006758
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006759 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006760
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006761 put_ctx(ctx);
6762 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006763}
6764
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006765void perf_event_delayed_put(struct task_struct *task)
6766{
6767 int ctxn;
6768
6769 for_each_task_context_nr(ctxn)
6770 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6771}
6772
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006773/*
6774 * inherit a event from parent task to child task:
6775 */
6776static struct perf_event *
6777inherit_event(struct perf_event *parent_event,
6778 struct task_struct *parent,
6779 struct perf_event_context *parent_ctx,
6780 struct task_struct *child,
6781 struct perf_event *group_leader,
6782 struct perf_event_context *child_ctx)
6783{
6784 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006785 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006786
6787 /*
6788 * Instead of creating recursive hierarchies of events,
6789 * we link inherited events back to the original parent,
6790 * which has a filp for sure, which we use as the reference
6791 * count:
6792 */
6793 if (parent_event->parent)
6794 parent_event = parent_event->parent;
6795
6796 child_event = perf_event_alloc(&parent_event->attr,
6797 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006798 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006799 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006800 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006801 if (IS_ERR(child_event))
6802 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01006803
6804 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6805 free_event(child_event);
6806 return NULL;
6807 }
6808
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006809 get_ctx(child_ctx);
6810
6811 /*
6812 * Make the child state follow the state of the parent event,
6813 * not its attr.disabled bit. We hold the parent's mutex,
6814 * so we won't race with perf_event_{en, dis}able_family.
6815 */
6816 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6817 child_event->state = PERF_EVENT_STATE_INACTIVE;
6818 else
6819 child_event->state = PERF_EVENT_STATE_OFF;
6820
6821 if (parent_event->attr.freq) {
6822 u64 sample_period = parent_event->hw.sample_period;
6823 struct hw_perf_event *hwc = &child_event->hw;
6824
6825 hwc->sample_period = sample_period;
6826 hwc->last_period = sample_period;
6827
6828 local64_set(&hwc->period_left, sample_period);
6829 }
6830
6831 child_event->ctx = child_ctx;
6832 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006833 child_event->overflow_handler_context
6834 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006835
6836 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006837 * Precalculate sample_data sizes
6838 */
6839 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006840 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006841
6842 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006843 * Link it up in the child's context:
6844 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006845 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006846 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006847 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006848
6849 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006850 * Link this into the parent event's child list
6851 */
6852 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6853 mutex_lock(&parent_event->child_mutex);
6854 list_add_tail(&child_event->child_list, &parent_event->child_list);
6855 mutex_unlock(&parent_event->child_mutex);
6856
6857 return child_event;
6858}
6859
6860static int inherit_group(struct perf_event *parent_event,
6861 struct task_struct *parent,
6862 struct perf_event_context *parent_ctx,
6863 struct task_struct *child,
6864 struct perf_event_context *child_ctx)
6865{
6866 struct perf_event *leader;
6867 struct perf_event *sub;
6868 struct perf_event *child_ctr;
6869
6870 leader = inherit_event(parent_event, parent, parent_ctx,
6871 child, NULL, child_ctx);
6872 if (IS_ERR(leader))
6873 return PTR_ERR(leader);
6874 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6875 child_ctr = inherit_event(sub, parent, parent_ctx,
6876 child, leader, child_ctx);
6877 if (IS_ERR(child_ctr))
6878 return PTR_ERR(child_ctr);
6879 }
6880 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006881}
6882
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006883static int
6884inherit_task_group(struct perf_event *event, struct task_struct *parent,
6885 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006886 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006887 int *inherited_all)
6888{
6889 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006890 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006891
6892 if (!event->attr.inherit) {
6893 *inherited_all = 0;
6894 return 0;
6895 }
6896
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006897 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006898 if (!child_ctx) {
6899 /*
6900 * This is executed from the parent task context, so
6901 * inherit events that have been marked for cloning.
6902 * First allocate and initialize a context for the
6903 * child.
6904 */
6905
Peter Zijlstraeb184472010-09-07 15:55:13 +02006906 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006907 if (!child_ctx)
6908 return -ENOMEM;
6909
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006910 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006911 }
6912
6913 ret = inherit_group(event, parent, parent_ctx,
6914 child, child_ctx);
6915
6916 if (ret)
6917 *inherited_all = 0;
6918
6919 return ret;
6920}
6921
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006922/*
6923 * Initialize the perf_event context in task_struct
6924 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006925int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006926{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006927 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006928 struct perf_event_context *cloned_ctx;
6929 struct perf_event *event;
6930 struct task_struct *parent = current;
6931 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006932 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006933 int ret = 0;
6934
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006935 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006936 return 0;
6937
6938 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006939 * If the parent's context is a clone, pin it so it won't get
6940 * swapped under us.
6941 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006942 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006943
6944 /*
6945 * No need to check if parent_ctx != NULL here; since we saw
6946 * it non-NULL earlier, the only reason for it to become NULL
6947 * is if we exit, and since we're currently in the middle of
6948 * a fork we can't be exiting at the same time.
6949 */
6950
6951 /*
6952 * Lock the parent list. No need to lock the child - not PID
6953 * hashed yet and not running, so nobody can access it.
6954 */
6955 mutex_lock(&parent_ctx->mutex);
6956
6957 /*
6958 * We dont have to disable NMIs - we are only looking at
6959 * the list, not manipulating it:
6960 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006961 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006962 ret = inherit_task_group(event, parent, parent_ctx,
6963 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006964 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006965 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006966 }
6967
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006968 /*
6969 * We can't hold ctx->lock when iterating the ->flexible_group list due
6970 * to allocations, but we need to prevent rotation because
6971 * rotate_ctx() will change the list from interrupt context.
6972 */
6973 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6974 parent_ctx->rotate_disable = 1;
6975 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6976
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006977 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006978 ret = inherit_task_group(event, parent, parent_ctx,
6979 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006980 if (ret)
6981 break;
6982 }
6983
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006984 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6985 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006986
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006987 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006988
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006989 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990 /*
6991 * Mark the child context as a clone of the parent
6992 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006993 *
6994 * Note that if the parent is a clone, the holding of
6995 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006996 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006997 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006998 if (cloned_ctx) {
6999 child_ctx->parent_ctx = cloned_ctx;
7000 child_ctx->parent_gen = parent_ctx->parent_gen;
7001 } else {
7002 child_ctx->parent_ctx = parent_ctx;
7003 child_ctx->parent_gen = parent_ctx->generation;
7004 }
7005 get_ctx(child_ctx->parent_ctx);
7006 }
7007
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007008 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007009 mutex_unlock(&parent_ctx->mutex);
7010
7011 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007012 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007013
7014 return ret;
7015}
7016
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007017/*
7018 * Initialize the perf_event context in task_struct
7019 */
7020int perf_event_init_task(struct task_struct *child)
7021{
7022 int ctxn, ret;
7023
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007024 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7025 mutex_init(&child->perf_event_mutex);
7026 INIT_LIST_HEAD(&child->perf_event_list);
7027
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007028 for_each_task_context_nr(ctxn) {
7029 ret = perf_event_init_context(child, ctxn);
7030 if (ret)
7031 return ret;
7032 }
7033
7034 return 0;
7035}
7036
Paul Mackerras220b1402010-03-10 20:45:52 +11007037static void __init perf_event_init_all_cpus(void)
7038{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007039 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007040 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007041
7042 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007043 swhash = &per_cpu(swevent_htable, cpu);
7044 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007045 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007046 }
7047}
7048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049static void __cpuinit perf_event_init_cpu(int cpu)
7050{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007051 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007052
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007053 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007054 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007055 struct swevent_hlist *hlist;
7056
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007057 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7058 WARN_ON(!hlist);
7059 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007060 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007061 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007062}
7063
Peter Zijlstrac2774432010-12-08 15:29:02 +01007064#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007065static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007066{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007067 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7068
7069 WARN_ON(!irqs_disabled());
7070
7071 list_del_init(&cpuctx->rotation_list);
7072}
7073
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007074static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007076 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007077 struct perf_event *event, *tmp;
7078
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007079 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007080
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007081 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007082 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007083 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007084 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007085}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007086
7087static void perf_event_exit_cpu_context(int cpu)
7088{
7089 struct perf_event_context *ctx;
7090 struct pmu *pmu;
7091 int idx;
7092
7093 idx = srcu_read_lock(&pmus_srcu);
7094 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007095 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007096
7097 mutex_lock(&ctx->mutex);
7098 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7099 mutex_unlock(&ctx->mutex);
7100 }
7101 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007102}
7103
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007104static void perf_event_exit_cpu(int cpu)
7105{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007106 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007107
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007108 mutex_lock(&swhash->hlist_mutex);
7109 swevent_hlist_release(swhash);
7110 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007111
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007112 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007113}
7114#else
7115static inline void perf_event_exit_cpu(int cpu) { }
7116#endif
7117
Peter Zijlstrac2774432010-12-08 15:29:02 +01007118static int
7119perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7120{
7121 int cpu;
7122
7123 for_each_online_cpu(cpu)
7124 perf_event_exit_cpu(cpu);
7125
7126 return NOTIFY_OK;
7127}
7128
7129/*
7130 * Run the perf reboot notifier at the very last possible moment so that
7131 * the generic watchdog code runs as long as possible.
7132 */
7133static struct notifier_block perf_reboot_notifier = {
7134 .notifier_call = perf_reboot,
7135 .priority = INT_MIN,
7136};
7137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007138static int __cpuinit
7139perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7140{
7141 unsigned int cpu = (long)hcpu;
7142
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007143 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007144
7145 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007146 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007147 perf_event_init_cpu(cpu);
7148 break;
7149
Peter Zijlstra5e116372010-06-11 13:35:08 +02007150 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007151 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007152 perf_event_exit_cpu(cpu);
7153 break;
7154
7155 default:
7156 break;
7157 }
7158
7159 return NOTIFY_OK;
7160}
7161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162void __init perf_event_init(void)
7163{
Jason Wessel3c502e72010-11-04 17:33:01 -05007164 int ret;
7165
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007166 idr_init(&pmu_idr);
7167
Paul Mackerras220b1402010-03-10 20:45:52 +11007168 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007169 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007170 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7171 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7172 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007173 perf_tp_register();
7174 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007175 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007176
7177 ret = init_hw_breakpoint();
7178 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007179
7180 /* do not patch jump label more than once per second */
7181 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007182
7183 /*
7184 * Build time assertion that we keep the data_head at the intended
7185 * location. IOW, validation we got the __reserved[] size right.
7186 */
7187 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7188 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007190
7191static int __init perf_event_sysfs_init(void)
7192{
7193 struct pmu *pmu;
7194 int ret;
7195
7196 mutex_lock(&pmus_lock);
7197
7198 ret = bus_register(&pmu_bus);
7199 if (ret)
7200 goto unlock;
7201
7202 list_for_each_entry(pmu, &pmus, entry) {
7203 if (!pmu->name || pmu->type < 0)
7204 continue;
7205
7206 ret = pmu_dev_alloc(pmu);
7207 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7208 }
7209 pmu_bus_running = 1;
7210 ret = 0;
7211
7212unlock:
7213 mutex_unlock(&pmus_lock);
7214
7215 return ret;
7216}
7217device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007218
7219#ifdef CONFIG_CGROUP_PERF
Li Zefan761b3ef2012-01-31 13:47:36 +08007220static struct cgroup_subsys_state *perf_cgroup_create(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007221{
7222 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007223
Li Zefan1b15d052011-03-03 14:26:06 +08007224 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007225 if (!jc)
7226 return ERR_PTR(-ENOMEM);
7227
Stephane Eraniane5d13672011-02-14 11:20:01 +02007228 jc->info = alloc_percpu(struct perf_cgroup_info);
7229 if (!jc->info) {
7230 kfree(jc);
7231 return ERR_PTR(-ENOMEM);
7232 }
7233
Stephane Eraniane5d13672011-02-14 11:20:01 +02007234 return &jc->css;
7235}
7236
Li Zefan761b3ef2012-01-31 13:47:36 +08007237static void perf_cgroup_destroy(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007238{
7239 struct perf_cgroup *jc;
7240 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7241 struct perf_cgroup, css);
7242 free_percpu(jc->info);
7243 kfree(jc);
7244}
7245
7246static int __perf_cgroup_move(void *info)
7247{
7248 struct task_struct *task = info;
7249 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7250 return 0;
7251}
7252
Li Zefan761b3ef2012-01-31 13:47:36 +08007253static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007254{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007255 struct task_struct *task;
7256
7257 cgroup_taskset_for_each(task, cgrp, tset)
7258 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007259}
7260
Li Zefan761b3ef2012-01-31 13:47:36 +08007261static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7262 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007263{
7264 /*
7265 * cgroup_exit() is called in the copy_process() failure path.
7266 * Ignore this case since the task hasn't ran yet, this avoids
7267 * trying to poke a half freed task state from generic code.
7268 */
7269 if (!(task->flags & PF_EXITING))
7270 return;
7271
Tejun Heobb9d97b2011-12-12 18:12:21 -08007272 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007273}
7274
7275struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007276 .name = "perf_event",
7277 .subsys_id = perf_subsys_id,
7278 .create = perf_cgroup_create,
7279 .destroy = perf_cgroup_destroy,
7280 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007281 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007282};
7283#endif /* CONFIG_CGROUP_PERF */