blob: 228fdb042fadb24a3636de6c67ef5ecbbbe87016 [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
256static inline void perf_get_cgroup(struct perf_event *event)
257{
258 css_get(&event->cgrp->css);
259}
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;
470 struct file *file;
471 int ret = 0, fput_needed;
472
473 file = fget_light(fd, &fput_needed);
474 if (!file)
475 return -EBADF;
476
477 css = cgroup_css_from_dir(file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800478 if (IS_ERR(css)) {
479 ret = PTR_ERR(css);
480 goto out;
481 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200482
483 cgrp = container_of(css, struct perf_cgroup, css);
484 event->cgrp = cgrp;
485
Li Zefanf75e18c2011-03-03 14:25:50 +0800486 /* must be done before we fput() the file */
487 perf_get_cgroup(event);
488
Stephane Eraniane5d13672011-02-14 11:20:01 +0200489 /*
490 * all events in a group must monitor
491 * the same cgroup because a task belongs
492 * to only one perf cgroup at a time
493 */
494 if (group_leader && group_leader->cgrp != cgrp) {
495 perf_detach_cgroup(event);
496 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200497 }
Li Zefan3db272c2011-03-03 14:25:37 +0800498out:
Stephane Eraniane5d13672011-02-14 11:20:01 +0200499 fput_light(file, fput_needed);
500 return ret;
501}
502
503static inline void
504perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
505{
506 struct perf_cgroup_info *t;
507 t = per_cpu_ptr(event->cgrp->info, event->cpu);
508 event->shadow_ctx_time = now - t->timestamp;
509}
510
511static inline void
512perf_cgroup_defer_enabled(struct perf_event *event)
513{
514 /*
515 * when the current task's perf cgroup does not match
516 * the event's, we need to remember to call the
517 * perf_mark_enable() function the first time a task with
518 * a matching perf cgroup is scheduled in.
519 */
520 if (is_cgroup_event(event) && !perf_cgroup_match(event))
521 event->cgrp_defer_enabled = 1;
522}
523
524static inline void
525perf_cgroup_mark_enabled(struct perf_event *event,
526 struct perf_event_context *ctx)
527{
528 struct perf_event *sub;
529 u64 tstamp = perf_event_time(event);
530
531 if (!event->cgrp_defer_enabled)
532 return;
533
534 event->cgrp_defer_enabled = 0;
535
536 event->tstamp_enabled = tstamp - event->total_time_enabled;
537 list_for_each_entry(sub, &event->sibling_list, group_entry) {
538 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
539 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
540 sub->cgrp_defer_enabled = 0;
541 }
542 }
543}
544#else /* !CONFIG_CGROUP_PERF */
545
546static inline bool
547perf_cgroup_match(struct perf_event *event)
548{
549 return true;
550}
551
552static inline void perf_detach_cgroup(struct perf_event *event)
553{}
554
555static inline int is_cgroup_event(struct perf_event *event)
556{
557 return 0;
558}
559
560static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
561{
562 return 0;
563}
564
565static inline void update_cgrp_time_from_event(struct perf_event *event)
566{
567}
568
569static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
570{
571}
572
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200573static inline void perf_cgroup_sched_out(struct task_struct *task,
574 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200575{
576}
577
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200578static inline void perf_cgroup_sched_in(struct task_struct *prev,
579 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580{
581}
582
583static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
584 struct perf_event_attr *attr,
585 struct perf_event *group_leader)
586{
587 return -EINVAL;
588}
589
590static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200591perf_cgroup_set_timestamp(struct task_struct *task,
592 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200593{
594}
595
596void
597perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
598{
599}
600
601static inline void
602perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
603{
604}
605
606static inline u64 perf_cgroup_event_time(struct perf_event *event)
607{
608 return 0;
609}
610
611static inline void
612perf_cgroup_defer_enabled(struct perf_event *event)
613{
614}
615
616static inline void
617perf_cgroup_mark_enabled(struct perf_event *event,
618 struct perf_event_context *ctx)
619{
620}
621#endif
622
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200623void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200624{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200625 int *count = this_cpu_ptr(pmu->pmu_disable_count);
626 if (!(*count)++)
627 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200628}
629
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200630void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200632 int *count = this_cpu_ptr(pmu->pmu_disable_count);
633 if (!--(*count))
634 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200635}
636
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200637static DEFINE_PER_CPU(struct list_head, rotation_list);
638
639/*
640 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
641 * because they're strictly cpu affine and rotate_start is called with IRQs
642 * disabled, while rotate_context is called from IRQ context.
643 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200644static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200645{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200646 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200647 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200648
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200649 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200650
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200651 if (list_empty(&cpuctx->rotation_list))
652 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200653}
654
655static void get_ctx(struct perf_event_context *ctx)
656{
657 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
658}
659
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660static void put_ctx(struct perf_event_context *ctx)
661{
662 if (atomic_dec_and_test(&ctx->refcount)) {
663 if (ctx->parent_ctx)
664 put_ctx(ctx->parent_ctx);
665 if (ctx->task)
666 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800667 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200668 }
669}
670
671static void unclone_ctx(struct perf_event_context *ctx)
672{
673 if (ctx->parent_ctx) {
674 put_ctx(ctx->parent_ctx);
675 ctx->parent_ctx = NULL;
676 }
677}
678
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200679static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
680{
681 /*
682 * only top level events have the pid namespace they were created in
683 */
684 if (event->parent)
685 event = event->parent;
686
687 return task_tgid_nr_ns(p, event->ns);
688}
689
690static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
691{
692 /*
693 * only top level events have the pid namespace they were created in
694 */
695 if (event->parent)
696 event = event->parent;
697
698 return task_pid_nr_ns(p, event->ns);
699}
700
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701/*
702 * If we inherit events we want to return the parent event id
703 * to userspace.
704 */
705static u64 primary_event_id(struct perf_event *event)
706{
707 u64 id = event->id;
708
709 if (event->parent)
710 id = event->parent->id;
711
712 return id;
713}
714
715/*
716 * Get the perf_event_context for a task and lock it.
717 * This has to cope with with the fact that until it is locked,
718 * the context could get moved to another task.
719 */
720static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200721perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200722{
723 struct perf_event_context *ctx;
724
725 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200726retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200727 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200728 if (ctx) {
729 /*
730 * If this context is a clone of another, it might
731 * get swapped for another underneath us by
732 * perf_event_task_sched_out, though the
733 * rcu_read_lock() protects us from any context
734 * getting freed. Lock the context and check if it
735 * got swapped before we could get the lock, and retry
736 * if so. If we locked the right context, then it
737 * can't get swapped on us any more.
738 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100739 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200740 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100741 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200742 goto retry;
743 }
744
745 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100746 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200747 ctx = NULL;
748 }
749 }
750 rcu_read_unlock();
751 return ctx;
752}
753
754/*
755 * Get the context for a task and increment its pin_count so it
756 * can't get swapped to another task. This also increments its
757 * reference count so that the context can't get freed.
758 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200759static struct perf_event_context *
760perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200761{
762 struct perf_event_context *ctx;
763 unsigned long flags;
764
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200765 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200766 if (ctx) {
767 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100768 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200769 }
770 return ctx;
771}
772
773static void perf_unpin_context(struct perf_event_context *ctx)
774{
775 unsigned long flags;
776
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100777 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200778 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100779 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200780}
781
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100782/*
783 * Update the record of the current time in a context.
784 */
785static void update_context_time(struct perf_event_context *ctx)
786{
787 u64 now = perf_clock();
788
789 ctx->time += now - ctx->timestamp;
790 ctx->timestamp = now;
791}
792
Stephane Eranian41587552011-01-03 18:20:01 +0200793static u64 perf_event_time(struct perf_event *event)
794{
795 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200796
797 if (is_cgroup_event(event))
798 return perf_cgroup_event_time(event);
799
Stephane Eranian41587552011-01-03 18:20:01 +0200800 return ctx ? ctx->time : 0;
801}
802
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100803/*
804 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400805 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100806 */
807static void update_event_times(struct perf_event *event)
808{
809 struct perf_event_context *ctx = event->ctx;
810 u64 run_end;
811
812 if (event->state < PERF_EVENT_STATE_INACTIVE ||
813 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
814 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200815 /*
816 * in cgroup mode, time_enabled represents
817 * the time the event was enabled AND active
818 * tasks were in the monitored cgroup. This is
819 * independent of the activity of the context as
820 * there may be a mix of cgroup and non-cgroup events.
821 *
822 * That is why we treat cgroup events differently
823 * here.
824 */
825 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +0900826 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200827 else if (ctx->is_active)
828 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100829 else
830 run_end = event->tstamp_stopped;
831
832 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100833
834 if (event->state == PERF_EVENT_STATE_INACTIVE)
835 run_end = event->tstamp_stopped;
836 else
Stephane Eranian41587552011-01-03 18:20:01 +0200837 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100838
839 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200840
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100841}
842
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200843/*
844 * Update total_time_enabled and total_time_running for all events in a group.
845 */
846static void update_group_times(struct perf_event *leader)
847{
848 struct perf_event *event;
849
850 update_event_times(leader);
851 list_for_each_entry(event, &leader->sibling_list, group_entry)
852 update_event_times(event);
853}
854
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100855static struct list_head *
856ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
857{
858 if (event->attr.pinned)
859 return &ctx->pinned_groups;
860 else
861 return &ctx->flexible_groups;
862}
863
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200864/*
865 * Add a event from the lists for its context.
866 * Must be called with ctx->mutex and ctx->lock held.
867 */
868static void
869list_add_event(struct perf_event *event, struct perf_event_context *ctx)
870{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200871 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
872 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200873
874 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200875 * If we're a stand alone event or group leader, we go to the context
876 * list, group events are kept attached to the group so that
877 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200879 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100880 struct list_head *list;
881
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100882 if (is_software_event(event))
883 event->group_flags |= PERF_GROUP_SOFTWARE;
884
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100885 list = ctx_group_list(event, ctx);
886 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200887 }
888
Peter Zijlstra08309372011-03-03 11:31:20 +0100889 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200890 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200891
Stephane Eraniand010b332012-02-09 23:21:00 +0100892 if (has_branch_stack(event))
893 ctx->nr_branch_stack++;
894
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200895 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200896 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200897 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200898 ctx->nr_events++;
899 if (event->attr.inherit_stat)
900 ctx->nr_stat++;
901}
902
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200903/*
904 * Called at perf_event creation and when events are attached/detached from a
905 * group.
906 */
907static void perf_event__read_size(struct perf_event *event)
908{
909 int entry = sizeof(u64); /* value */
910 int size = 0;
911 int nr = 1;
912
913 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
914 size += sizeof(u64);
915
916 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
917 size += sizeof(u64);
918
919 if (event->attr.read_format & PERF_FORMAT_ID)
920 entry += sizeof(u64);
921
922 if (event->attr.read_format & PERF_FORMAT_GROUP) {
923 nr += event->group_leader->nr_siblings;
924 size += sizeof(u64);
925 }
926
927 size += entry * nr;
928 event->read_size = size;
929}
930
931static void perf_event__header_size(struct perf_event *event)
932{
933 struct perf_sample_data *data;
934 u64 sample_type = event->attr.sample_type;
935 u16 size = 0;
936
937 perf_event__read_size(event);
938
939 if (sample_type & PERF_SAMPLE_IP)
940 size += sizeof(data->ip);
941
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200942 if (sample_type & PERF_SAMPLE_ADDR)
943 size += sizeof(data->addr);
944
945 if (sample_type & PERF_SAMPLE_PERIOD)
946 size += sizeof(data->period);
947
948 if (sample_type & PERF_SAMPLE_READ)
949 size += event->read_size;
950
951 event->header_size = size;
952}
953
954static void perf_event__id_header_size(struct perf_event *event)
955{
956 struct perf_sample_data *data;
957 u64 sample_type = event->attr.sample_type;
958 u16 size = 0;
959
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200960 if (sample_type & PERF_SAMPLE_TID)
961 size += sizeof(data->tid_entry);
962
963 if (sample_type & PERF_SAMPLE_TIME)
964 size += sizeof(data->time);
965
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200966 if (sample_type & PERF_SAMPLE_ID)
967 size += sizeof(data->id);
968
969 if (sample_type & PERF_SAMPLE_STREAM_ID)
970 size += sizeof(data->stream_id);
971
972 if (sample_type & PERF_SAMPLE_CPU)
973 size += sizeof(data->cpu_entry);
974
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200975 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200976}
977
Peter Zijlstra8a495422010-05-27 15:47:49 +0200978static void perf_group_attach(struct perf_event *event)
979{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200980 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200981
Peter Zijlstra74c33372010-10-15 11:40:29 +0200982 /*
983 * We can have double attach due to group movement in perf_event_open.
984 */
985 if (event->attach_state & PERF_ATTACH_GROUP)
986 return;
987
Peter Zijlstra8a495422010-05-27 15:47:49 +0200988 event->attach_state |= PERF_ATTACH_GROUP;
989
990 if (group_leader == event)
991 return;
992
993 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
994 !is_software_event(event))
995 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
996
997 list_add_tail(&event->group_entry, &group_leader->sibling_list);
998 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200999
1000 perf_event__header_size(group_leader);
1001
1002 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1003 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001004}
1005
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001006/*
1007 * Remove a event from the lists for its context.
1008 * Must be called with ctx->mutex and ctx->lock held.
1009 */
1010static void
1011list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1012{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001013 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001014 /*
1015 * We can have double detach due to exit/hot-unplug + close.
1016 */
1017 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001019
1020 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1021
Stephane Eranian68cacd22011-03-23 16:03:06 +01001022 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001023 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001024 cpuctx = __get_cpu_context(ctx);
1025 /*
1026 * if there are no more cgroup events
1027 * then cler cgrp to avoid stale pointer
1028 * in update_cgrp_time_from_cpuctx()
1029 */
1030 if (!ctx->nr_cgroups)
1031 cpuctx->cgrp = NULL;
1032 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001033
Stephane Eraniand010b332012-02-09 23:21:00 +01001034 if (has_branch_stack(event))
1035 ctx->nr_branch_stack--;
1036
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037 ctx->nr_events--;
1038 if (event->attr.inherit_stat)
1039 ctx->nr_stat--;
1040
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001041 list_del_rcu(&event->event_entry);
1042
Peter Zijlstra8a495422010-05-27 15:47:49 +02001043 if (event->group_leader == event)
1044 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001045
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001046 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001047
1048 /*
1049 * If event was in error state, then keep it
1050 * that way, otherwise bogus counts will be
1051 * returned on read(). The only way to get out
1052 * of error state is by explicit re-enabling
1053 * of the event
1054 */
1055 if (event->state > PERF_EVENT_STATE_OFF)
1056 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001057}
1058
Peter Zijlstra8a495422010-05-27 15:47:49 +02001059static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001060{
1061 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001062 struct list_head *list = NULL;
1063
1064 /*
1065 * We can have double detach due to exit/hot-unplug + close.
1066 */
1067 if (!(event->attach_state & PERF_ATTACH_GROUP))
1068 return;
1069
1070 event->attach_state &= ~PERF_ATTACH_GROUP;
1071
1072 /*
1073 * If this is a sibling, remove it from its group.
1074 */
1075 if (event->group_leader != event) {
1076 list_del_init(&event->group_entry);
1077 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001078 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001079 }
1080
1081 if (!list_empty(&event->group_entry))
1082 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001083
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084 /*
1085 * If this was a group event with sibling events then
1086 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001087 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088 */
1089 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001090 if (list)
1091 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001092 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001093
1094 /* Inherit group flags from the previous leader */
1095 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001097
1098out:
1099 perf_event__header_size(event->group_leader);
1100
1101 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1102 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103}
1104
Stephane Eranianfa66f072010-08-26 16:40:01 +02001105static inline int
1106event_filter_match(struct perf_event *event)
1107{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001108 return (event->cpu == -1 || event->cpu == smp_processor_id())
1109 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001110}
1111
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001112static void
1113event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001114 struct perf_cpu_context *cpuctx,
1115 struct perf_event_context *ctx)
1116{
Stephane Eranian41587552011-01-03 18:20:01 +02001117 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001118 u64 delta;
1119 /*
1120 * An event which could not be activated because of
1121 * filter mismatch still needs to have its timings
1122 * maintained, otherwise bogus information is return
1123 * via read() for time_enabled, time_running:
1124 */
1125 if (event->state == PERF_EVENT_STATE_INACTIVE
1126 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001127 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001128 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001129 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001130 }
1131
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001133 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001134
1135 event->state = PERF_EVENT_STATE_INACTIVE;
1136 if (event->pending_disable) {
1137 event->pending_disable = 0;
1138 event->state = PERF_EVENT_STATE_OFF;
1139 }
Stephane Eranian41587552011-01-03 18:20:01 +02001140 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001141 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 event->oncpu = -1;
1143
1144 if (!is_software_event(event))
1145 cpuctx->active_oncpu--;
1146 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001147 if (event->attr.freq && event->attr.sample_freq)
1148 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 if (event->attr.exclusive || !cpuctx->active_oncpu)
1150 cpuctx->exclusive = 0;
1151}
1152
1153static void
1154group_sched_out(struct perf_event *group_event,
1155 struct perf_cpu_context *cpuctx,
1156 struct perf_event_context *ctx)
1157{
1158 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001159 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160
1161 event_sched_out(group_event, cpuctx, ctx);
1162
1163 /*
1164 * Schedule out siblings (if any):
1165 */
1166 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1167 event_sched_out(event, cpuctx, ctx);
1168
Stephane Eranianfa66f072010-08-26 16:40:01 +02001169 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001170 cpuctx->exclusive = 0;
1171}
1172
1173/*
1174 * Cross CPU call to remove a performance event
1175 *
1176 * We disable the event on the hardware level first. After that we
1177 * remove it from the context list.
1178 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001179static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001180{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001181 struct perf_event *event = info;
1182 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001183 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001184
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001185 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001188 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1189 ctx->is_active = 0;
1190 cpuctx->task_ctx = NULL;
1191 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001192 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001193
1194 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195}
1196
1197
1198/*
1199 * Remove the event from a task's (or a CPU's) list of events.
1200 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001201 * CPU events are removed with a smp call. For task events we only
1202 * call when the task is on a CPU.
1203 *
1204 * If event->ctx is a cloned context, callers must make sure that
1205 * every task struct that event->ctx->task could possibly point to
1206 * remains valid. This is OK when called from perf_release since
1207 * that only calls us on the top-level context, which can't be a clone.
1208 * When called from perf_event_exit_task, it's OK because the
1209 * context has been detached from its task.
1210 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001211static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001212{
1213 struct perf_event_context *ctx = event->ctx;
1214 struct task_struct *task = ctx->task;
1215
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001216 lockdep_assert_held(&ctx->mutex);
1217
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001218 if (!task) {
1219 /*
1220 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001221 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001222 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001223 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224 return;
1225 }
1226
1227retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001228 if (!task_function_call(task, __perf_remove_from_context, event))
1229 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001231 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001233 * If we failed to find a running task, but find the context active now
1234 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001236 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001237 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 goto retry;
1239 }
1240
1241 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001242 * Since the task isn't running, its safe to remove the event, us
1243 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001245 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001246 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247}
1248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001249/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250 * Cross CPU call to disable a performance event
1251 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001252static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253{
1254 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001256 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257
1258 /*
1259 * If this is a per-task event, need to check whether this
1260 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001261 *
1262 * Can trigger due to concurrent perf_event_context_sched_out()
1263 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 */
1265 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001266 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001268 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269
1270 /*
1271 * If the event is on, turn it off.
1272 * If it is in error state, leave it in error state.
1273 */
1274 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1275 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001276 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277 update_group_times(event);
1278 if (event == event->group_leader)
1279 group_sched_out(event, cpuctx, ctx);
1280 else
1281 event_sched_out(event, cpuctx, ctx);
1282 event->state = PERF_EVENT_STATE_OFF;
1283 }
1284
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001285 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001286
1287 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001288}
1289
1290/*
1291 * Disable a event.
1292 *
1293 * If event->ctx is a cloned context, callers must make sure that
1294 * every task struct that event->ctx->task could possibly point to
1295 * remains valid. This condition is satisifed when called through
1296 * perf_event_for_each_child or perf_event_for_each because they
1297 * hold the top-level event's child_mutex, so any descendant that
1298 * goes to exit will block in sync_child_event.
1299 * When called from perf_pending_event it's OK because event->ctx
1300 * is the current context on this CPU and preemption is disabled,
1301 * hence we can't get into perf_event_task_sched_out for this context.
1302 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001303void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001304{
1305 struct perf_event_context *ctx = event->ctx;
1306 struct task_struct *task = ctx->task;
1307
1308 if (!task) {
1309 /*
1310 * Disable the event on the cpu that it's on
1311 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001312 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001313 return;
1314 }
1315
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001316retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001317 if (!task_function_call(task, __perf_event_disable, event))
1318 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001320 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001321 /*
1322 * If the event is still active, we need to retry the cross-call.
1323 */
1324 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001325 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001326 /*
1327 * Reload the task pointer, it might have been changed by
1328 * a concurrent perf_event_context_sched_out().
1329 */
1330 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331 goto retry;
1332 }
1333
1334 /*
1335 * Since we have the lock this context can't be scheduled
1336 * in, so we can change the state safely.
1337 */
1338 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1339 update_group_times(event);
1340 event->state = PERF_EVENT_STATE_OFF;
1341 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001342 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001344EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345
Stephane Eraniane5d13672011-02-14 11:20:01 +02001346static void perf_set_shadow_time(struct perf_event *event,
1347 struct perf_event_context *ctx,
1348 u64 tstamp)
1349{
1350 /*
1351 * use the correct time source for the time snapshot
1352 *
1353 * We could get by without this by leveraging the
1354 * fact that to get to this function, the caller
1355 * has most likely already called update_context_time()
1356 * and update_cgrp_time_xx() and thus both timestamp
1357 * are identical (or very close). Given that tstamp is,
1358 * already adjusted for cgroup, we could say that:
1359 * tstamp - ctx->timestamp
1360 * is equivalent to
1361 * tstamp - cgrp->timestamp.
1362 *
1363 * Then, in perf_output_read(), the calculation would
1364 * work with no changes because:
1365 * - event is guaranteed scheduled in
1366 * - no scheduled out in between
1367 * - thus the timestamp would be the same
1368 *
1369 * But this is a bit hairy.
1370 *
1371 * So instead, we have an explicit cgroup call to remain
1372 * within the time time source all along. We believe it
1373 * is cleaner and simpler to understand.
1374 */
1375 if (is_cgroup_event(event))
1376 perf_cgroup_set_shadow_time(event, tstamp);
1377 else
1378 event->shadow_ctx_time = tstamp - ctx->timestamp;
1379}
1380
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001381#define MAX_INTERRUPTS (~0ULL)
1382
1383static void perf_log_throttle(struct perf_event *event, int enable);
1384
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001386event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001388 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389{
Stephane Eranian41587552011-01-03 18:20:01 +02001390 u64 tstamp = perf_event_time(event);
1391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001392 if (event->state <= PERF_EVENT_STATE_OFF)
1393 return 0;
1394
1395 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001396 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001397
1398 /*
1399 * Unthrottle events, since we scheduled we might have missed several
1400 * ticks already, also for a heavily scheduling task there is little
1401 * guarantee it'll get a tick in a timely manner.
1402 */
1403 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1404 perf_log_throttle(event, 1);
1405 event->hw.interrupts = 0;
1406 }
1407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001408 /*
1409 * The new state must be visible before we turn it on in the hardware:
1410 */
1411 smp_wmb();
1412
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001413 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414 event->state = PERF_EVENT_STATE_INACTIVE;
1415 event->oncpu = -1;
1416 return -EAGAIN;
1417 }
1418
Stephane Eranian41587552011-01-03 18:20:01 +02001419 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001420
Stephane Eraniane5d13672011-02-14 11:20:01 +02001421 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423 if (!is_software_event(event))
1424 cpuctx->active_oncpu++;
1425 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001426 if (event->attr.freq && event->attr.sample_freq)
1427 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428
1429 if (event->attr.exclusive)
1430 cpuctx->exclusive = 1;
1431
1432 return 0;
1433}
1434
1435static int
1436group_sched_in(struct perf_event *group_event,
1437 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001438 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439{
Lin Ming6bde9b62010-04-23 13:56:00 +08001440 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001441 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001442 u64 now = ctx->time;
1443 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444
1445 if (group_event->state == PERF_EVENT_STATE_OFF)
1446 return 0;
1447
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001448 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001449
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001450 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001451 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001452 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +02001453 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001454
1455 /*
1456 * Schedule in siblings as one group (if any):
1457 */
1458 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001459 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001460 partial_group = event;
1461 goto group_error;
1462 }
1463 }
1464
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001465 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001466 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001467
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001468group_error:
1469 /*
1470 * Groups can be scheduled in as one unit only, so undo any
1471 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001472 * The events up to the failed event are scheduled out normally,
1473 * tstamp_stopped will be updated.
1474 *
1475 * The failed events and the remaining siblings need to have
1476 * their timings updated as if they had gone thru event_sched_in()
1477 * and event_sched_out(). This is required to get consistent timings
1478 * across the group. This also takes care of the case where the group
1479 * could never be scheduled by ensuring tstamp_stopped is set to mark
1480 * the time the event was actually stopped, such that time delta
1481 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001482 */
1483 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1484 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001485 simulate = true;
1486
1487 if (simulate) {
1488 event->tstamp_running += now - event->tstamp_stopped;
1489 event->tstamp_stopped = now;
1490 } else {
1491 event_sched_out(event, cpuctx, ctx);
1492 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001494 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001496 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +02001497
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498 return -EAGAIN;
1499}
1500
1501/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502 * Work out whether we can put this event group on the CPU now.
1503 */
1504static int group_can_go_on(struct perf_event *event,
1505 struct perf_cpu_context *cpuctx,
1506 int can_add_hw)
1507{
1508 /*
1509 * Groups consisting entirely of software events can always go on.
1510 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001511 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001512 return 1;
1513 /*
1514 * If an exclusive group is already on, no other hardware
1515 * events can go on.
1516 */
1517 if (cpuctx->exclusive)
1518 return 0;
1519 /*
1520 * If this group is exclusive and there are already
1521 * events on the CPU, it can't go on.
1522 */
1523 if (event->attr.exclusive && cpuctx->active_oncpu)
1524 return 0;
1525 /*
1526 * Otherwise, try to add it if all previous groups were able
1527 * to go on.
1528 */
1529 return can_add_hw;
1530}
1531
1532static void add_event_to_ctx(struct perf_event *event,
1533 struct perf_event_context *ctx)
1534{
Stephane Eranian41587552011-01-03 18:20:01 +02001535 u64 tstamp = perf_event_time(event);
1536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001538 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001539 event->tstamp_enabled = tstamp;
1540 event->tstamp_running = tstamp;
1541 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001542}
1543
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001544static void task_ctx_sched_out(struct perf_event_context *ctx);
1545static void
1546ctx_sched_in(struct perf_event_context *ctx,
1547 struct perf_cpu_context *cpuctx,
1548 enum event_type_t event_type,
1549 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001550
Peter Zijlstradce58552011-04-09 21:17:46 +02001551static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1552 struct perf_event_context *ctx,
1553 struct task_struct *task)
1554{
1555 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1556 if (ctx)
1557 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1558 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1559 if (ctx)
1560 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1561}
1562
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563/*
1564 * Cross CPU call to install and enable a performance event
1565 *
1566 * Must be called with ctx->mutex held
1567 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001568static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570 struct perf_event *event = info;
1571 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001572 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001573 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1574 struct task_struct *task = current;
1575
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001576 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001577 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578
1579 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001580 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001582 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001583 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001584
1585 /*
1586 * If the context we're installing events in is not the
1587 * active task_ctx, flip them.
1588 */
1589 if (ctx->task && task_ctx != ctx) {
1590 if (task_ctx)
1591 raw_spin_unlock(&task_ctx->lock);
1592 raw_spin_lock(&ctx->lock);
1593 task_ctx = ctx;
1594 }
1595
1596 if (task_ctx) {
1597 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001598 task = task_ctx->task;
1599 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001600
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001601 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001604 /*
1605 * update cgrp time only if current cgrp
1606 * matches event->cgrp. Must be done before
1607 * calling add_event_to_ctx()
1608 */
1609 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 add_event_to_ctx(event, ctx);
1612
1613 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001614 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001615 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001616 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001618 perf_pmu_enable(cpuctx->ctx.pmu);
1619 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001620
1621 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001622}
1623
1624/*
1625 * Attach a performance event to a context
1626 *
1627 * First we add the event to the list with the hardware enable bit
1628 * in event->hw_config cleared.
1629 *
1630 * If the event is attached to a task which is on a CPU we use a smp
1631 * call to enable it in the task context. The task might have been
1632 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001633 */
1634static void
1635perf_install_in_context(struct perf_event_context *ctx,
1636 struct perf_event *event,
1637 int cpu)
1638{
1639 struct task_struct *task = ctx->task;
1640
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001641 lockdep_assert_held(&ctx->mutex);
1642
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001643 event->ctx = ctx;
1644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645 if (!task) {
1646 /*
1647 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001648 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001650 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001651 return;
1652 }
1653
1654retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001655 if (!task_function_call(task, __perf_install_in_context, event))
1656 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001658 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001660 * If we failed to find a running task, but find the context active now
1661 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001663 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001664 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665 goto retry;
1666 }
1667
1668 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001669 * Since the task isn't running, its safe to add the event, us holding
1670 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001672 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001673 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674}
1675
1676/*
1677 * Put a event into inactive state and update time fields.
1678 * Enabling the leader of a group effectively enables all
1679 * the group members that aren't explicitly disabled, so we
1680 * have to update their ->tstamp_enabled also.
1681 * Note: this works for group members as well as group leaders
1682 * since the non-leader members' sibling_lists will be empty.
1683 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001684static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685{
1686 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001687 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688
1689 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001690 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001691 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001692 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1693 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001694 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695}
1696
1697/*
1698 * Cross CPU call to enable a performance event
1699 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001700static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701{
1702 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703 struct perf_event_context *ctx = event->ctx;
1704 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001705 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706 int err;
1707
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001708 if (WARN_ON_ONCE(!ctx->is_active))
1709 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001710
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001711 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 update_context_time(ctx);
1713
1714 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1715 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001716
1717 /*
1718 * set current task's cgroup time reference point
1719 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001720 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001721
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001722 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723
Stephane Eraniane5d13672011-02-14 11:20:01 +02001724 if (!event_filter_match(event)) {
1725 if (is_cgroup_event(event))
1726 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001727 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001728 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730 /*
1731 * If the event is in a group and isn't the group leader,
1732 * then don't put it on unless the group is on.
1733 */
1734 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1735 goto unlock;
1736
1737 if (!group_can_go_on(event, cpuctx, 1)) {
1738 err = -EEXIST;
1739 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001740 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001741 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001743 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001744 }
1745
1746 if (err) {
1747 /*
1748 * If this event can't go on and it's part of a
1749 * group, then the whole group has to come off.
1750 */
1751 if (leader != event)
1752 group_sched_out(leader, cpuctx, ctx);
1753 if (leader->attr.pinned) {
1754 update_group_times(leader);
1755 leader->state = PERF_EVENT_STATE_ERROR;
1756 }
1757 }
1758
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001759unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001760 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001761
1762 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763}
1764
1765/*
1766 * Enable a event.
1767 *
1768 * If event->ctx is a cloned context, callers must make sure that
1769 * every task struct that event->ctx->task could possibly point to
1770 * remains valid. This condition is satisfied when called through
1771 * perf_event_for_each_child or perf_event_for_each as described
1772 * for perf_event_disable.
1773 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001774void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775{
1776 struct perf_event_context *ctx = event->ctx;
1777 struct task_struct *task = ctx->task;
1778
1779 if (!task) {
1780 /*
1781 * Enable the event on the cpu that it's on
1782 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001783 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784 return;
1785 }
1786
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001787 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1789 goto out;
1790
1791 /*
1792 * If the event is in error state, clear that first.
1793 * That way, if we see the event in error state below, we
1794 * know that it has gone back into error state, as distinct
1795 * from the task having been scheduled away before the
1796 * cross-call arrived.
1797 */
1798 if (event->state == PERF_EVENT_STATE_ERROR)
1799 event->state = PERF_EVENT_STATE_OFF;
1800
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001801retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001802 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001803 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001804 goto out;
1805 }
1806
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001807 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001808
1809 if (!task_function_call(task, __perf_event_enable, event))
1810 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001812 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813
1814 /*
1815 * If the context is active and the event is still off,
1816 * we need to retry the cross-call.
1817 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001818 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1819 /*
1820 * task could have been flipped by a concurrent
1821 * perf_event_context_sched_out()
1822 */
1823 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001824 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001825 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001827out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001828 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001830EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831
Avi Kivity26ca5c12011-06-29 18:42:37 +03001832int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833{
1834 /*
1835 * not supported on inherited events
1836 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001837 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838 return -EINVAL;
1839
1840 atomic_add(refresh, &event->event_limit);
1841 perf_event_enable(event);
1842
1843 return 0;
1844}
Avi Kivity26ca5c12011-06-29 18:42:37 +03001845EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001847static void ctx_sched_out(struct perf_event_context *ctx,
1848 struct perf_cpu_context *cpuctx,
1849 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850{
1851 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001852 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853
Peter Zijlstradb24d332011-04-09 21:17:45 +02001854 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001856 return;
1857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001859 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001860 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001861 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001862
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001863 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001864 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001865 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1866 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001867 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001868
Peter Zijlstradb24d332011-04-09 21:17:45 +02001869 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001870 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001871 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001872 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001873 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874}
1875
1876/*
1877 * Test whether two contexts are equivalent, i.e. whether they
1878 * have both been cloned from the same version of the same context
1879 * and they both have the same number of enabled events.
1880 * If the number of enabled events is the same, then the set
1881 * of enabled events should be the same, because these are both
1882 * inherited contexts, therefore we can't access individual events
1883 * in them directly with an fd; we can only enable/disable all
1884 * events via prctl, or enable/disable all events in a family
1885 * via ioctl, which will have the same effect on both contexts.
1886 */
1887static int context_equiv(struct perf_event_context *ctx1,
1888 struct perf_event_context *ctx2)
1889{
1890 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1891 && ctx1->parent_gen == ctx2->parent_gen
1892 && !ctx1->pin_count && !ctx2->pin_count;
1893}
1894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895static void __perf_event_sync_stat(struct perf_event *event,
1896 struct perf_event *next_event)
1897{
1898 u64 value;
1899
1900 if (!event->attr.inherit_stat)
1901 return;
1902
1903 /*
1904 * Update the event value, we cannot use perf_event_read()
1905 * because we're in the middle of a context switch and have IRQs
1906 * disabled, which upsets smp_call_function_single(), however
1907 * we know the event must be on the current CPU, therefore we
1908 * don't need to use it.
1909 */
1910 switch (event->state) {
1911 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001912 event->pmu->read(event);
1913 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001914
1915 case PERF_EVENT_STATE_INACTIVE:
1916 update_event_times(event);
1917 break;
1918
1919 default:
1920 break;
1921 }
1922
1923 /*
1924 * In order to keep per-task stats reliable we need to flip the event
1925 * values when we flip the contexts.
1926 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001927 value = local64_read(&next_event->count);
1928 value = local64_xchg(&event->count, value);
1929 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930
1931 swap(event->total_time_enabled, next_event->total_time_enabled);
1932 swap(event->total_time_running, next_event->total_time_running);
1933
1934 /*
1935 * Since we swizzled the values, update the user visible data too.
1936 */
1937 perf_event_update_userpage(event);
1938 perf_event_update_userpage(next_event);
1939}
1940
1941#define list_next_entry(pos, member) \
1942 list_entry(pos->member.next, typeof(*pos), member)
1943
1944static void perf_event_sync_stat(struct perf_event_context *ctx,
1945 struct perf_event_context *next_ctx)
1946{
1947 struct perf_event *event, *next_event;
1948
1949 if (!ctx->nr_stat)
1950 return;
1951
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001952 update_context_time(ctx);
1953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954 event = list_first_entry(&ctx->event_list,
1955 struct perf_event, event_entry);
1956
1957 next_event = list_first_entry(&next_ctx->event_list,
1958 struct perf_event, event_entry);
1959
1960 while (&event->event_entry != &ctx->event_list &&
1961 &next_event->event_entry != &next_ctx->event_list) {
1962
1963 __perf_event_sync_stat(event, next_event);
1964
1965 event = list_next_entry(event, event_entry);
1966 next_event = list_next_entry(next_event, event_entry);
1967 }
1968}
1969
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001970static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1971 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001973 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974 struct perf_event_context *next_ctx;
1975 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001976 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977 int do_switch = 1;
1978
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001979 if (likely(!ctx))
1980 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001981
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001982 cpuctx = __get_cpu_context(ctx);
1983 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984 return;
1985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986 rcu_read_lock();
1987 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001988 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989 if (parent && next_ctx &&
1990 rcu_dereference(next_ctx->parent_ctx) == parent) {
1991 /*
1992 * Looks like the two contexts are clones, so we might be
1993 * able to optimize the context switch. We lock both
1994 * contexts and check that they are clones under the
1995 * lock (including re-checking that neither has been
1996 * uncloned in the meantime). It doesn't matter which
1997 * order we take the locks because no other cpu could
1998 * be trying to lock both of these tasks.
1999 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002000 raw_spin_lock(&ctx->lock);
2001 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 if (context_equiv(ctx, next_ctx)) {
2003 /*
2004 * XXX do we need a memory barrier of sorts
2005 * wrt to rcu_dereference() of perf_event_ctxp
2006 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002007 task->perf_event_ctxp[ctxn] = next_ctx;
2008 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009 ctx->task = next;
2010 next_ctx->task = task;
2011 do_switch = 0;
2012
2013 perf_event_sync_stat(ctx, next_ctx);
2014 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002015 raw_spin_unlock(&next_ctx->lock);
2016 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002017 }
2018 rcu_read_unlock();
2019
2020 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002021 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002022 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002023 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002024 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002025 }
2026}
2027
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002028#define for_each_task_context_nr(ctxn) \
2029 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2030
2031/*
2032 * Called from scheduler to remove the events of the current task,
2033 * with interrupts disabled.
2034 *
2035 * We stop each event and update the event value in event->count.
2036 *
2037 * This does not protect us against NMI, but disable()
2038 * sets the disabled bit in the control field of event _before_
2039 * accessing the event control register. If a NMI hits, then it will
2040 * not restart the event.
2041 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002042void __perf_event_task_sched_out(struct task_struct *task,
2043 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002044{
2045 int ctxn;
2046
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002047 for_each_task_context_nr(ctxn)
2048 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002049
2050 /*
2051 * if cgroup events exist on this CPU, then we need
2052 * to check if we have to switch out PMU state.
2053 * cgroup event are system-wide mode only
2054 */
2055 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002056 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002057}
2058
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002059static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002061 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062
2063 if (!cpuctx->task_ctx)
2064 return;
2065
2066 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2067 return;
2068
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002069 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002070 cpuctx->task_ctx = NULL;
2071}
2072
2073/*
2074 * Called with IRQs disabled
2075 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002076static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2077 enum event_type_t event_type)
2078{
2079 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080}
2081
2082static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002083ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002084 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085{
2086 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002088 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2089 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002091 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092 continue;
2093
Stephane Eraniane5d13672011-02-14 11:20:01 +02002094 /* may need to reset tstamp_enabled */
2095 if (is_cgroup_event(event))
2096 perf_cgroup_mark_enabled(event, ctx);
2097
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002098 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002099 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002100
2101 /*
2102 * If this pinned group hasn't been scheduled,
2103 * put it in error state.
2104 */
2105 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2106 update_group_times(event);
2107 event->state = PERF_EVENT_STATE_ERROR;
2108 }
2109 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002110}
2111
2112static void
2113ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002114 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002115{
2116 struct perf_event *event;
2117 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002119 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2120 /* Ignore events in OFF or ERROR state */
2121 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 /*
2124 * Listen to the 'cpu' scheduling filter constraint
2125 * of events:
2126 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002127 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128 continue;
2129
Stephane Eraniane5d13672011-02-14 11:20:01 +02002130 /* may need to reset tstamp_enabled */
2131 if (is_cgroup_event(event))
2132 perf_cgroup_mark_enabled(event, ctx);
2133
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002134 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002135 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002137 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002139}
2140
2141static void
2142ctx_sched_in(struct perf_event_context *ctx,
2143 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002144 enum event_type_t event_type,
2145 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002146{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002147 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002148 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002149
Peter Zijlstradb24d332011-04-09 21:17:45 +02002150 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002151 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002152 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002153
Stephane Eraniane5d13672011-02-14 11:20:01 +02002154 now = perf_clock();
2155 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002156 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002157 /*
2158 * First go through the list and put on any pinned groups
2159 * in order to give them the best chance of going on.
2160 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002161 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002162 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002163
2164 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002165 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002166 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002167}
2168
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002169static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002170 enum event_type_t event_type,
2171 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002172{
2173 struct perf_event_context *ctx = &cpuctx->ctx;
2174
Stephane Eraniane5d13672011-02-14 11:20:01 +02002175 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002176}
2177
Stephane Eraniane5d13672011-02-14 11:20:01 +02002178static void perf_event_context_sched_in(struct perf_event_context *ctx,
2179 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002181 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002183 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002184 if (cpuctx->task_ctx == ctx)
2185 return;
2186
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002187 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002188 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002189 /*
2190 * We want to keep the following priority order:
2191 * cpu pinned (that don't need to move), task pinned,
2192 * cpu flexible, task flexible.
2193 */
2194 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2195
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002196 if (ctx->nr_events)
2197 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002198
Gleb Natapov86b47c22011-11-22 16:08:21 +02002199 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2200
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002201 perf_pmu_enable(ctx->pmu);
2202 perf_ctx_unlock(cpuctx, ctx);
2203
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002204 /*
2205 * Since these rotations are per-cpu, we need to ensure the
2206 * cpu-context we got scheduled on is actually rotating.
2207 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002208 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209}
2210
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002211/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002212 * When sampling the branck stack in system-wide, it may be necessary
2213 * to flush the stack on context switch. This happens when the branch
2214 * stack does not tag its entries with the pid of the current task.
2215 * Otherwise it becomes impossible to associate a branch entry with a
2216 * task. This ambiguity is more likely to appear when the branch stack
2217 * supports priv level filtering and the user sets it to monitor only
2218 * at the user level (which could be a useful measurement in system-wide
2219 * mode). In that case, the risk is high of having a branch stack with
2220 * branch from multiple tasks. Flushing may mean dropping the existing
2221 * entries or stashing them somewhere in the PMU specific code layer.
2222 *
2223 * This function provides the context switch callback to the lower code
2224 * layer. It is invoked ONLY when there is at least one system-wide context
2225 * with at least one active event using taken branch sampling.
2226 */
2227static void perf_branch_stack_sched_in(struct task_struct *prev,
2228 struct task_struct *task)
2229{
2230 struct perf_cpu_context *cpuctx;
2231 struct pmu *pmu;
2232 unsigned long flags;
2233
2234 /* no need to flush branch stack if not changing task */
2235 if (prev == task)
2236 return;
2237
2238 local_irq_save(flags);
2239
2240 rcu_read_lock();
2241
2242 list_for_each_entry_rcu(pmu, &pmus, entry) {
2243 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2244
2245 /*
2246 * check if the context has at least one
2247 * event using PERF_SAMPLE_BRANCH_STACK
2248 */
2249 if (cpuctx->ctx.nr_branch_stack > 0
2250 && pmu->flush_branch_stack) {
2251
2252 pmu = cpuctx->ctx.pmu;
2253
2254 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2255
2256 perf_pmu_disable(pmu);
2257
2258 pmu->flush_branch_stack();
2259
2260 perf_pmu_enable(pmu);
2261
2262 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2263 }
2264 }
2265
2266 rcu_read_unlock();
2267
2268 local_irq_restore(flags);
2269}
2270
2271/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002272 * Called from scheduler to add the events of the current task
2273 * with interrupts disabled.
2274 *
2275 * We restore the event value and then enable it.
2276 *
2277 * This does not protect us against NMI, but enable()
2278 * sets the enabled bit in the control field of event _before_
2279 * accessing the event control register. If a NMI hits, then it will
2280 * keep the event running.
2281 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002282void __perf_event_task_sched_in(struct task_struct *prev,
2283 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002284{
2285 struct perf_event_context *ctx;
2286 int ctxn;
2287
2288 for_each_task_context_nr(ctxn) {
2289 ctx = task->perf_event_ctxp[ctxn];
2290 if (likely(!ctx))
2291 continue;
2292
Stephane Eraniane5d13672011-02-14 11:20:01 +02002293 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002294 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002295 /*
2296 * if cgroup events exist on this CPU, then we need
2297 * to check if we have to switch in PMU state.
2298 * cgroup event are system-wide mode only
2299 */
2300 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002301 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002302
2303 /* check for system-wide branch_stack events */
2304 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2305 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306}
2307
Peter Zijlstraabd50712010-01-26 18:50:16 +01002308static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2309{
2310 u64 frequency = event->attr.sample_freq;
2311 u64 sec = NSEC_PER_SEC;
2312 u64 divisor, dividend;
2313
2314 int count_fls, nsec_fls, frequency_fls, sec_fls;
2315
2316 count_fls = fls64(count);
2317 nsec_fls = fls64(nsec);
2318 frequency_fls = fls64(frequency);
2319 sec_fls = 30;
2320
2321 /*
2322 * We got @count in @nsec, with a target of sample_freq HZ
2323 * the target period becomes:
2324 *
2325 * @count * 10^9
2326 * period = -------------------
2327 * @nsec * sample_freq
2328 *
2329 */
2330
2331 /*
2332 * Reduce accuracy by one bit such that @a and @b converge
2333 * to a similar magnitude.
2334 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002335#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002336do { \
2337 if (a##_fls > b##_fls) { \
2338 a >>= 1; \
2339 a##_fls--; \
2340 } else { \
2341 b >>= 1; \
2342 b##_fls--; \
2343 } \
2344} while (0)
2345
2346 /*
2347 * Reduce accuracy until either term fits in a u64, then proceed with
2348 * the other, so that finally we can do a u64/u64 division.
2349 */
2350 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2351 REDUCE_FLS(nsec, frequency);
2352 REDUCE_FLS(sec, count);
2353 }
2354
2355 if (count_fls + sec_fls > 64) {
2356 divisor = nsec * frequency;
2357
2358 while (count_fls + sec_fls > 64) {
2359 REDUCE_FLS(count, sec);
2360 divisor >>= 1;
2361 }
2362
2363 dividend = count * sec;
2364 } else {
2365 dividend = count * sec;
2366
2367 while (nsec_fls + frequency_fls > 64) {
2368 REDUCE_FLS(nsec, frequency);
2369 dividend >>= 1;
2370 }
2371
2372 divisor = nsec * frequency;
2373 }
2374
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002375 if (!divisor)
2376 return dividend;
2377
Peter Zijlstraabd50712010-01-26 18:50:16 +01002378 return div64_u64(dividend, divisor);
2379}
2380
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002381static DEFINE_PER_CPU(int, perf_throttled_count);
2382static DEFINE_PER_CPU(u64, perf_throttled_seq);
2383
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002384static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385{
2386 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002387 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388 s64 delta;
2389
Peter Zijlstraabd50712010-01-26 18:50:16 +01002390 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391
2392 delta = (s64)(period - hwc->sample_period);
2393 delta = (delta + 7) / 8; /* low pass filter */
2394
2395 sample_period = hwc->sample_period + delta;
2396
2397 if (!sample_period)
2398 sample_period = 1;
2399
2400 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002401
Peter Zijlstrae7850592010-05-21 14:43:08 +02002402 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002403 if (disable)
2404 event->pmu->stop(event, PERF_EF_UPDATE);
2405
Peter Zijlstrae7850592010-05-21 14:43:08 +02002406 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002407
2408 if (disable)
2409 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002410 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411}
2412
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002413/*
2414 * combine freq adjustment with unthrottling to avoid two passes over the
2415 * events. At the same time, make sure, having freq events does not change
2416 * the rate of unthrottling as that would introduce bias.
2417 */
2418static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2419 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420{
2421 struct perf_event *event;
2422 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002423 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002424 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002425
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002426 /*
2427 * only need to iterate over all events iff:
2428 * - context have events in frequency mode (needs freq adjust)
2429 * - there are events to unthrottle on this cpu
2430 */
2431 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002432 return;
2433
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002434 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002435 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002436
Paul Mackerras03541f82009-10-14 16:58:03 +11002437 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438 if (event->state != PERF_EVENT_STATE_ACTIVE)
2439 continue;
2440
Stephane Eranian5632ab12011-01-03 18:20:01 +02002441 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002442 continue;
2443
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444 hwc = &event->hw;
2445
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002446 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2447 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002449 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450 }
2451
2452 if (!event->attr.freq || !event->attr.sample_freq)
2453 continue;
2454
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002455 /*
2456 * stop the event and update event->count
2457 */
2458 event->pmu->stop(event, PERF_EF_UPDATE);
2459
Peter Zijlstrae7850592010-05-21 14:43:08 +02002460 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002461 delta = now - hwc->freq_count_stamp;
2462 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002463
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002464 /*
2465 * restart the event
2466 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002467 * we have stopped the event so tell that
2468 * to perf_adjust_period() to avoid stopping it
2469 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002470 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002471 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002472 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002473
2474 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002476
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002477 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002478 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479}
2480
2481/*
2482 * Round-robin a context's events:
2483 */
2484static void rotate_ctx(struct perf_event_context *ctx)
2485{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002486 /*
2487 * Rotate the first entry last of non-pinned groups. Rotation might be
2488 * disabled by the inheritance code.
2489 */
2490 if (!ctx->rotate_disable)
2491 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002492}
2493
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002494/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002495 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2496 * because they're strictly cpu affine and rotate_start is called with IRQs
2497 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002498 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002499static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002500{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002501 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002502 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002504 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002505 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002506 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2507 rotate = 1;
2508 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002510 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002511 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002512 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002513 if (ctx->nr_events != ctx->nr_active)
2514 rotate = 1;
2515 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002517 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002518 goto done;
2519
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002520 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002521 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002522
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002523 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2524 if (ctx)
2525 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002526
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002527 rotate_ctx(&cpuctx->ctx);
2528 if (ctx)
2529 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002530
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002531 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002532
2533 perf_pmu_enable(cpuctx->ctx.pmu);
2534 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002535done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002536 if (remove)
2537 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002538}
2539
2540void perf_event_task_tick(void)
2541{
2542 struct list_head *head = &__get_cpu_var(rotation_list);
2543 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002544 struct perf_event_context *ctx;
2545 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002546
2547 WARN_ON(!irqs_disabled());
2548
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002549 __this_cpu_inc(perf_throttled_seq);
2550 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2551
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002552 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002553 ctx = &cpuctx->ctx;
2554 perf_adjust_freq_unthr_context(ctx, throttled);
2555
2556 ctx = cpuctx->task_ctx;
2557 if (ctx)
2558 perf_adjust_freq_unthr_context(ctx, throttled);
2559
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002560 if (cpuctx->jiffies_interval == 1 ||
2561 !(jiffies % cpuctx->jiffies_interval))
2562 perf_rotate_context(cpuctx);
2563 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002564}
2565
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002566static int event_enable_on_exec(struct perf_event *event,
2567 struct perf_event_context *ctx)
2568{
2569 if (!event->attr.enable_on_exec)
2570 return 0;
2571
2572 event->attr.enable_on_exec = 0;
2573 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2574 return 0;
2575
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002576 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002577
2578 return 1;
2579}
2580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002581/*
2582 * Enable all of a task's events that have been marked enable-on-exec.
2583 * This expects task == current.
2584 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002585static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002586{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002587 struct perf_event *event;
2588 unsigned long flags;
2589 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002590 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591
2592 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002593 if (!ctx || !ctx->nr_events)
2594 goto out;
2595
Stephane Eraniane566b762011-04-06 02:54:54 +02002596 /*
2597 * We must ctxsw out cgroup events to avoid conflict
2598 * when invoking perf_task_event_sched_in() later on
2599 * in this function. Otherwise we end up trying to
2600 * ctxswin cgroup events which are already scheduled
2601 * in.
2602 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002603 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002604
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002605 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002606 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002607
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002608 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002609 ret = event_enable_on_exec(event, ctx);
2610 if (ret)
2611 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612 }
2613
2614 /*
2615 * Unclone this context if we enabled any event.
2616 */
2617 if (enabled)
2618 unclone_ctx(ctx);
2619
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002620 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002621
Stephane Eraniane566b762011-04-06 02:54:54 +02002622 /*
2623 * Also calls ctxswin for cgroup events, if any:
2624 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002625 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002626out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627 local_irq_restore(flags);
2628}
2629
2630/*
2631 * Cross CPU call to read the hardware event
2632 */
2633static void __perf_event_read(void *info)
2634{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002635 struct perf_event *event = info;
2636 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002637 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002638
2639 /*
2640 * If this is a task context, we need to check whether it is
2641 * the current task context of this cpu. If not it has been
2642 * scheduled out before the smp call arrived. In that case
2643 * event->count would have been updated to a recent sample
2644 * when the event was scheduled out.
2645 */
2646 if (ctx->task && cpuctx->task_ctx != ctx)
2647 return;
2648
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002649 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002650 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002651 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002652 update_cgrp_time_from_event(event);
2653 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002655 if (event->state == PERF_EVENT_STATE_ACTIVE)
2656 event->pmu->read(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002657 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002658}
2659
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002660static inline u64 perf_event_count(struct perf_event *event)
2661{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002662 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002663}
2664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665static u64 perf_event_read(struct perf_event *event)
2666{
2667 /*
2668 * If event is enabled and currently active on a CPU, update the
2669 * value in the event structure:
2670 */
2671 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2672 smp_call_function_single(event->oncpu,
2673 __perf_event_read, event, 1);
2674 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002675 struct perf_event_context *ctx = event->ctx;
2676 unsigned long flags;
2677
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002678 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002679 /*
2680 * may read while context is not active
2681 * (e.g., thread is blocked), in that case
2682 * we cannot update context time
2683 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002684 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002685 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002686 update_cgrp_time_from_event(event);
2687 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002688 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002689 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690 }
2691
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002692 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693}
2694
2695/*
2696 * Initialize the perf_event context in a task_struct:
2697 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002698static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002699{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002700 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002701 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002702 INIT_LIST_HEAD(&ctx->pinned_groups);
2703 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002704 INIT_LIST_HEAD(&ctx->event_list);
2705 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002706}
2707
Peter Zijlstraeb184472010-09-07 15:55:13 +02002708static struct perf_event_context *
2709alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002710{
2711 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002712
2713 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2714 if (!ctx)
2715 return NULL;
2716
2717 __perf_event_init_context(ctx);
2718 if (task) {
2719 ctx->task = task;
2720 get_task_struct(task);
2721 }
2722 ctx->pmu = pmu;
2723
2724 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725}
2726
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002727static struct task_struct *
2728find_lively_task_by_vpid(pid_t vpid)
2729{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731 int err;
2732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002733 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002734 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735 task = current;
2736 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002737 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002738 if (task)
2739 get_task_struct(task);
2740 rcu_read_unlock();
2741
2742 if (!task)
2743 return ERR_PTR(-ESRCH);
2744
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745 /* Reuse ptrace permission checks for now. */
2746 err = -EACCES;
2747 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2748 goto errout;
2749
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002750 return task;
2751errout:
2752 put_task_struct(task);
2753 return ERR_PTR(err);
2754
2755}
2756
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002757/*
2758 * Returns a matching context with refcount and pincount.
2759 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002760static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002761find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762{
2763 struct perf_event_context *ctx;
2764 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002765 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002766 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002768 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769 /* Must be root to operate on a CPU event: */
2770 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2771 return ERR_PTR(-EACCES);
2772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773 /*
2774 * We could be clever and allow to attach a event to an
2775 * offline CPU and activate it when the CPU comes up, but
2776 * that's for later.
2777 */
2778 if (!cpu_online(cpu))
2779 return ERR_PTR(-ENODEV);
2780
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002781 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782 ctx = &cpuctx->ctx;
2783 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002784 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002785
2786 return ctx;
2787 }
2788
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002789 err = -EINVAL;
2790 ctxn = pmu->task_ctx_nr;
2791 if (ctxn < 0)
2792 goto errout;
2793
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002794retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002795 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002796 if (ctx) {
2797 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002798 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002799 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002800 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002801 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002802 err = -ENOMEM;
2803 if (!ctx)
2804 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002805
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002806 err = 0;
2807 mutex_lock(&task->perf_event_mutex);
2808 /*
2809 * If it has already passed perf_event_exit_task().
2810 * we must see PF_EXITING, it takes this mutex too.
2811 */
2812 if (task->flags & PF_EXITING)
2813 err = -ESRCH;
2814 else if (task->perf_event_ctxp[ctxn])
2815 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002816 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002817 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002818 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002819 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002820 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002821 mutex_unlock(&task->perf_event_mutex);
2822
2823 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002824 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002825
2826 if (err == -EAGAIN)
2827 goto retry;
2828 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002830 }
2831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002832 return ctx;
2833
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002834errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002835 return ERR_PTR(err);
2836}
2837
Li Zefan6fb29152009-10-15 11:21:42 +08002838static void perf_event_free_filter(struct perf_event *event);
2839
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002840static void free_event_rcu(struct rcu_head *head)
2841{
2842 struct perf_event *event;
2843
2844 event = container_of(head, struct perf_event, rcu_head);
2845 if (event->ns)
2846 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002847 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002848 kfree(event);
2849}
2850
Frederic Weisbecker76369132011-05-19 19:55:04 +02002851static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002852
2853static void free_event(struct perf_event *event)
2854{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002855 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002856
2857 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002858 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01002859 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002860 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861 atomic_dec(&nr_mmap_events);
2862 if (event->attr.comm)
2863 atomic_dec(&nr_comm_events);
2864 if (event->attr.task)
2865 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002866 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2867 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002868 if (is_cgroup_event(event)) {
2869 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01002870 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01002871 }
Stephane Eraniand010b332012-02-09 23:21:00 +01002872
2873 if (has_branch_stack(event)) {
2874 static_key_slow_dec_deferred(&perf_sched_events);
2875 /* is system-wide event */
2876 if (!(event->attach_state & PERF_ATTACH_TASK))
2877 atomic_dec(&per_cpu(perf_branch_stack_events,
2878 event->cpu));
2879 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002880 }
2881
Frederic Weisbecker76369132011-05-19 19:55:04 +02002882 if (event->rb) {
2883 ring_buffer_put(event->rb);
2884 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002885 }
2886
Stephane Eraniane5d13672011-02-14 11:20:01 +02002887 if (is_cgroup_event(event))
2888 perf_detach_cgroup(event);
2889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002890 if (event->destroy)
2891 event->destroy(event);
2892
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002893 if (event->ctx)
2894 put_ctx(event->ctx);
2895
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002896 call_rcu(&event->rcu_head, free_event_rcu);
2897}
2898
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002899int perf_event_release_kernel(struct perf_event *event)
2900{
2901 struct perf_event_context *ctx = event->ctx;
2902
2903 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002904 /*
2905 * There are two ways this annotation is useful:
2906 *
2907 * 1) there is a lock recursion from perf_event_exit_task
2908 * see the comment there.
2909 *
2910 * 2) there is a lock-inversion with mmap_sem through
2911 * perf_event_read_group(), which takes faults while
2912 * holding ctx->mutex, however this is called after
2913 * the last filedesc died, so there is no possibility
2914 * to trigger the AB-BA case.
2915 */
2916 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002917 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002918 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002919 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02002920 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002921 mutex_unlock(&ctx->mutex);
2922
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002923 free_event(event);
2924
2925 return 0;
2926}
2927EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2928
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002929/*
2930 * Called when the last reference to the file is gone.
2931 */
Al Virod156b472012-08-20 14:59:25 +01002932static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002933{
Peter Zijlstra88821352010-11-09 19:01:43 +01002934 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002935
Al Virod156b472012-08-20 14:59:25 +01002936 if (!atomic_long_dec_and_test(&event->refcount))
2937 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002938
Peter Zijlstra88821352010-11-09 19:01:43 +01002939 rcu_read_lock();
2940 owner = ACCESS_ONCE(event->owner);
2941 /*
2942 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2943 * !owner it means the list deletion is complete and we can indeed
2944 * free this event, otherwise we need to serialize on
2945 * owner->perf_event_mutex.
2946 */
2947 smp_read_barrier_depends();
2948 if (owner) {
2949 /*
2950 * Since delayed_put_task_struct() also drops the last
2951 * task reference we can safely take a new reference
2952 * while holding the rcu_read_lock().
2953 */
2954 get_task_struct(owner);
2955 }
2956 rcu_read_unlock();
2957
2958 if (owner) {
2959 mutex_lock(&owner->perf_event_mutex);
2960 /*
2961 * We have to re-check the event->owner field, if it is cleared
2962 * we raced with perf_event_exit_task(), acquiring the mutex
2963 * ensured they're done, and we can proceed with freeing the
2964 * event.
2965 */
2966 if (event->owner)
2967 list_del_init(&event->owner_entry);
2968 mutex_unlock(&owner->perf_event_mutex);
2969 put_task_struct(owner);
2970 }
2971
Al Virod156b472012-08-20 14:59:25 +01002972 perf_event_release_kernel(event);
2973}
2974
2975static int perf_release(struct inode *inode, struct file *file)
2976{
2977 put_event(file->private_data);
2978 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002979}
2980
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002981u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002982{
2983 struct perf_event *child;
2984 u64 total = 0;
2985
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002986 *enabled = 0;
2987 *running = 0;
2988
Peter Zijlstra6f105812009-11-20 22:19:56 +01002989 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002991 *enabled += event->total_time_enabled +
2992 atomic64_read(&event->child_total_time_enabled);
2993 *running += event->total_time_running +
2994 atomic64_read(&event->child_total_time_running);
2995
2996 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002998 *enabled += child->total_time_enabled;
2999 *running += child->total_time_running;
3000 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003001 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002
3003 return total;
3004}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003005EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007static int perf_event_read_group(struct perf_event *event,
3008 u64 read_format, char __user *buf)
3009{
3010 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003011 int n = 0, size = 0, ret = -EFAULT;
3012 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003013 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003014 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003015
Peter Zijlstra6f105812009-11-20 22:19:56 +01003016 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003017 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003018
3019 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003020 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3021 values[n++] = enabled;
3022 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3023 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003024 values[n++] = count;
3025 if (read_format & PERF_FORMAT_ID)
3026 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027
3028 size = n * sizeof(u64);
3029
3030 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003031 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003032
Peter Zijlstra6f105812009-11-20 22:19:56 +01003033 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034
3035 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003036 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003038 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003039 if (read_format & PERF_FORMAT_ID)
3040 values[n++] = primary_event_id(sub);
3041
3042 size = n * sizeof(u64);
3043
Stephane Eranian184d3da2009-11-23 21:40:49 -08003044 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003045 ret = -EFAULT;
3046 goto unlock;
3047 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003048
3049 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003051unlock:
3052 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053
Peter Zijlstraabf48682009-11-20 22:19:49 +01003054 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055}
3056
3057static int perf_event_read_one(struct perf_event *event,
3058 u64 read_format, char __user *buf)
3059{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003060 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061 u64 values[4];
3062 int n = 0;
3063
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003064 values[n++] = perf_event_read_value(event, &enabled, &running);
3065 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3066 values[n++] = enabled;
3067 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3068 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003069 if (read_format & PERF_FORMAT_ID)
3070 values[n++] = primary_event_id(event);
3071
3072 if (copy_to_user(buf, values, n * sizeof(u64)))
3073 return -EFAULT;
3074
3075 return n * sizeof(u64);
3076}
3077
3078/*
3079 * Read the performance event - simple non blocking version for now
3080 */
3081static ssize_t
3082perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3083{
3084 u64 read_format = event->attr.read_format;
3085 int ret;
3086
3087 /*
3088 * Return end-of-file for a read on a event that is in
3089 * error state (i.e. because it was pinned but it couldn't be
3090 * scheduled on to the CPU at some point).
3091 */
3092 if (event->state == PERF_EVENT_STATE_ERROR)
3093 return 0;
3094
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003095 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096 return -ENOSPC;
3097
3098 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003099 if (read_format & PERF_FORMAT_GROUP)
3100 ret = perf_event_read_group(event, read_format, buf);
3101 else
3102 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003103
3104 return ret;
3105}
3106
3107static ssize_t
3108perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3109{
3110 struct perf_event *event = file->private_data;
3111
3112 return perf_read_hw(event, buf, count);
3113}
3114
3115static unsigned int perf_poll(struct file *file, poll_table *wait)
3116{
3117 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003118 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003119 unsigned int events = POLL_HUP;
3120
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003121 /*
3122 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3123 * grabs the rb reference but perf_event_set_output() overrides it.
3124 * Here is the timeline for two threads T1, T2:
3125 * t0: T1, rb = rcu_dereference(event->rb)
3126 * t1: T2, old_rb = event->rb
3127 * t2: T2, event->rb = new rb
3128 * t3: T2, ring_buffer_detach(old_rb)
3129 * t4: T1, ring_buffer_attach(rb1)
3130 * t5: T1, poll_wait(event->waitq)
3131 *
3132 * To avoid this problem, we grab mmap_mutex in perf_poll()
3133 * thereby ensuring that the assignment of the new ring buffer
3134 * and the detachment of the old buffer appear atomic to perf_poll()
3135 */
3136 mutex_lock(&event->mmap_mutex);
3137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003138 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003139 rb = rcu_dereference(event->rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003140 if (rb) {
3141 ring_buffer_attach(event, rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003142 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003143 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144 rcu_read_unlock();
3145
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003146 mutex_unlock(&event->mmap_mutex);
3147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148 poll_wait(file, &event->waitq, wait);
3149
3150 return events;
3151}
3152
3153static void perf_event_reset(struct perf_event *event)
3154{
3155 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003156 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157 perf_event_update_userpage(event);
3158}
3159
3160/*
3161 * Holding the top-level event's child_mutex means that any
3162 * descendant process that has inherited this event will block
3163 * in sync_child_event if it goes to exit, thus satisfying the
3164 * task existence requirements of perf_event_enable/disable.
3165 */
3166static void perf_event_for_each_child(struct perf_event *event,
3167 void (*func)(struct perf_event *))
3168{
3169 struct perf_event *child;
3170
3171 WARN_ON_ONCE(event->ctx->parent_ctx);
3172 mutex_lock(&event->child_mutex);
3173 func(event);
3174 list_for_each_entry(child, &event->child_list, child_list)
3175 func(child);
3176 mutex_unlock(&event->child_mutex);
3177}
3178
3179static void perf_event_for_each(struct perf_event *event,
3180 void (*func)(struct perf_event *))
3181{
3182 struct perf_event_context *ctx = event->ctx;
3183 struct perf_event *sibling;
3184
3185 WARN_ON_ONCE(ctx->parent_ctx);
3186 mutex_lock(&ctx->mutex);
3187 event = event->group_leader;
3188
3189 perf_event_for_each_child(event, func);
3190 func(event);
3191 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003192 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193 mutex_unlock(&ctx->mutex);
3194}
3195
3196static int perf_event_period(struct perf_event *event, u64 __user *arg)
3197{
3198 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199 int ret = 0;
3200 u64 value;
3201
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003202 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203 return -EINVAL;
3204
John Blackwoodad0cf342010-09-28 18:03:11 -04003205 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206 return -EFAULT;
3207
3208 if (!value)
3209 return -EINVAL;
3210
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003211 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212 if (event->attr.freq) {
3213 if (value > sysctl_perf_event_sample_rate) {
3214 ret = -EINVAL;
3215 goto unlock;
3216 }
3217
3218 event->attr.sample_freq = value;
3219 } else {
3220 event->attr.sample_period = value;
3221 event->hw.sample_period = value;
3222 }
3223unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003224 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225
3226 return ret;
3227}
3228
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003229static const struct file_operations perf_fops;
3230
Al Virod156b472012-08-20 14:59:25 +01003231static struct file *perf_fget_light(int fd, int *fput_needed)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003232{
3233 struct file *file;
3234
3235 file = fget_light(fd, fput_needed);
3236 if (!file)
3237 return ERR_PTR(-EBADF);
3238
3239 if (file->f_op != &perf_fops) {
3240 fput_light(file, *fput_needed);
3241 *fput_needed = 0;
3242 return ERR_PTR(-EBADF);
3243 }
3244
Al Virod156b472012-08-20 14:59:25 +01003245 return file;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003246}
3247
3248static int perf_event_set_output(struct perf_event *event,
3249 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003250static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003251
3252static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3253{
3254 struct perf_event *event = file->private_data;
3255 void (*func)(struct perf_event *);
3256 u32 flags = arg;
3257
3258 switch (cmd) {
3259 case PERF_EVENT_IOC_ENABLE:
3260 func = perf_event_enable;
3261 break;
3262 case PERF_EVENT_IOC_DISABLE:
3263 func = perf_event_disable;
3264 break;
3265 case PERF_EVENT_IOC_RESET:
3266 func = perf_event_reset;
3267 break;
3268
3269 case PERF_EVENT_IOC_REFRESH:
3270 return perf_event_refresh(event, arg);
3271
3272 case PERF_EVENT_IOC_PERIOD:
3273 return perf_event_period(event, (u64 __user *)arg);
3274
3275 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003276 {
Al Virod156b472012-08-20 14:59:25 +01003277 struct file *output_file = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003278 struct perf_event *output_event = NULL;
3279 int fput_needed = 0;
3280 int ret;
3281
3282 if (arg != -1) {
Al Virod156b472012-08-20 14:59:25 +01003283 output_file = perf_fget_light(arg, &fput_needed);
3284 if (IS_ERR(output_file))
3285 return PTR_ERR(output_file);
3286 output_event = output_file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003287 }
3288
3289 ret = perf_event_set_output(event, output_event);
3290 if (output_event)
Al Virod156b472012-08-20 14:59:25 +01003291 fput_light(output_file, fput_needed);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003292
3293 return ret;
3294 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003295
Li Zefan6fb29152009-10-15 11:21:42 +08003296 case PERF_EVENT_IOC_SET_FILTER:
3297 return perf_event_set_filter(event, (void __user *)arg);
3298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299 default:
3300 return -ENOTTY;
3301 }
3302
3303 if (flags & PERF_IOC_FLAG_GROUP)
3304 perf_event_for_each(event, func);
3305 else
3306 perf_event_for_each_child(event, func);
3307
3308 return 0;
3309}
3310
3311int perf_event_task_enable(void)
3312{
3313 struct perf_event *event;
3314
3315 mutex_lock(&current->perf_event_mutex);
3316 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3317 perf_event_for_each_child(event, perf_event_enable);
3318 mutex_unlock(&current->perf_event_mutex);
3319
3320 return 0;
3321}
3322
3323int perf_event_task_disable(void)
3324{
3325 struct perf_event *event;
3326
3327 mutex_lock(&current->perf_event_mutex);
3328 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3329 perf_event_for_each_child(event, perf_event_disable);
3330 mutex_unlock(&current->perf_event_mutex);
3331
3332 return 0;
3333}
3334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335static int perf_event_index(struct perf_event *event)
3336{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003337 if (event->hw.state & PERF_HES_STOPPED)
3338 return 0;
3339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 if (event->state != PERF_EVENT_STATE_ACTIVE)
3341 return 0;
3342
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003343 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344}
3345
Eric B Munsonc4794292011-06-23 16:34:38 -04003346static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003347 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003348 u64 *enabled,
3349 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003350{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003351 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003352
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003353 *now = perf_clock();
3354 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003355 *enabled = ctx_time - event->tstamp_enabled;
3356 *running = ctx_time - event->tstamp_running;
3357}
3358
Peter Zijlstrac7206202012-03-22 17:26:36 +01003359void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003360{
3361}
3362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363/*
3364 * Callers need to ensure there can be no nesting of this function, otherwise
3365 * the seqlock logic goes bad. We can not serialize this because the arch
3366 * code calls this from NMI context.
3367 */
3368void perf_event_update_userpage(struct perf_event *event)
3369{
3370 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003371 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003372 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003373
3374 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003375 /*
3376 * compute total_time_enabled, total_time_running
3377 * based on snapshot values taken when the event
3378 * was last scheduled in.
3379 *
3380 * we cannot simply called update_context_time()
3381 * because of locking issue as we can be called in
3382 * NMI context
3383 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003384 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003385 rb = rcu_dereference(event->rb);
3386 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387 goto unlock;
3388
Frederic Weisbecker76369132011-05-19 19:55:04 +02003389 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390
3391 /*
3392 * Disable preemption so as to not let the corresponding user-space
3393 * spin too long if we get preempted.
3394 */
3395 preempt_disable();
3396 ++userpg->lock;
3397 barrier();
3398 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003399 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003400 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003401 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402
Eric B Munson0d641202011-06-24 12:26:26 -04003403 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003404 atomic64_read(&event->child_total_time_enabled);
3405
Eric B Munson0d641202011-06-24 12:26:26 -04003406 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 atomic64_read(&event->child_total_time_running);
3408
Peter Zijlstrac7206202012-03-22 17:26:36 +01003409 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003410
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 barrier();
3412 ++userpg->lock;
3413 preempt_enable();
3414unlock:
3415 rcu_read_unlock();
3416}
3417
Peter Zijlstra906010b2009-09-21 16:08:49 +02003418static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3419{
3420 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003421 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003422 int ret = VM_FAULT_SIGBUS;
3423
3424 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3425 if (vmf->pgoff == 0)
3426 ret = 0;
3427 return ret;
3428 }
3429
3430 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003431 rb = rcu_dereference(event->rb);
3432 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003433 goto unlock;
3434
3435 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3436 goto unlock;
3437
Frederic Weisbecker76369132011-05-19 19:55:04 +02003438 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003439 if (!vmf->page)
3440 goto unlock;
3441
3442 get_page(vmf->page);
3443 vmf->page->mapping = vma->vm_file->f_mapping;
3444 vmf->page->index = vmf->pgoff;
3445
3446 ret = 0;
3447unlock:
3448 rcu_read_unlock();
3449
3450 return ret;
3451}
3452
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003453static void ring_buffer_attach(struct perf_event *event,
3454 struct ring_buffer *rb)
3455{
3456 unsigned long flags;
3457
3458 if (!list_empty(&event->rb_entry))
3459 return;
3460
3461 spin_lock_irqsave(&rb->event_lock, flags);
3462 if (!list_empty(&event->rb_entry))
3463 goto unlock;
3464
3465 list_add(&event->rb_entry, &rb->event_list);
3466unlock:
3467 spin_unlock_irqrestore(&rb->event_lock, flags);
3468}
3469
3470static void ring_buffer_detach(struct perf_event *event,
3471 struct ring_buffer *rb)
3472{
3473 unsigned long flags;
3474
3475 if (list_empty(&event->rb_entry))
3476 return;
3477
3478 spin_lock_irqsave(&rb->event_lock, flags);
3479 list_del_init(&event->rb_entry);
3480 wake_up_all(&event->waitq);
3481 spin_unlock_irqrestore(&rb->event_lock, flags);
3482}
3483
3484static void ring_buffer_wakeup(struct perf_event *event)
3485{
3486 struct ring_buffer *rb;
3487
3488 rcu_read_lock();
3489 rb = rcu_dereference(event->rb);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003490 if (!rb)
3491 goto unlock;
3492
3493 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003494 wake_up_all(&event->waitq);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003495
3496unlock:
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003497 rcu_read_unlock();
3498}
3499
Frederic Weisbecker76369132011-05-19 19:55:04 +02003500static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003501{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003502 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003503
Frederic Weisbecker76369132011-05-19 19:55:04 +02003504 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3505 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003506}
3507
Frederic Weisbecker76369132011-05-19 19:55:04 +02003508static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003510 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003511
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003512 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003513 rb = rcu_dereference(event->rb);
3514 if (rb) {
3515 if (!atomic_inc_not_zero(&rb->refcount))
3516 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003517 }
3518 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003519
Frederic Weisbecker76369132011-05-19 19:55:04 +02003520 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003521}
3522
Frederic Weisbecker76369132011-05-19 19:55:04 +02003523static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003524{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003525 struct perf_event *event, *n;
3526 unsigned long flags;
3527
Frederic Weisbecker76369132011-05-19 19:55:04 +02003528 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003529 return;
3530
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003531 spin_lock_irqsave(&rb->event_lock, flags);
3532 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3533 list_del_init(&event->rb_entry);
3534 wake_up_all(&event->waitq);
3535 }
3536 spin_unlock_irqrestore(&rb->event_lock, flags);
3537
Frederic Weisbecker76369132011-05-19 19:55:04 +02003538 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539}
3540
3541static void perf_mmap_open(struct vm_area_struct *vma)
3542{
3543 struct perf_event *event = vma->vm_file->private_data;
3544
3545 atomic_inc(&event->mmap_count);
3546}
3547
3548static void perf_mmap_close(struct vm_area_struct *vma)
3549{
3550 struct perf_event *event = vma->vm_file->private_data;
3551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003553 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003554 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003555 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003556
Peter Zijlstra906010b2009-09-21 16:08:49 +02003557 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003558 vma->vm_mm->pinned_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003559 rcu_assign_pointer(event->rb, NULL);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003560 ring_buffer_detach(event, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003561 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003562
Frederic Weisbecker76369132011-05-19 19:55:04 +02003563 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003564 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003565 }
3566}
3567
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003568static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569 .open = perf_mmap_open,
3570 .close = perf_mmap_close,
3571 .fault = perf_mmap_fault,
3572 .page_mkwrite = perf_mmap_fault,
3573};
3574
3575static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3576{
3577 struct perf_event *event = file->private_data;
3578 unsigned long user_locked, user_lock_limit;
3579 struct user_struct *user = current_user();
3580 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003581 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582 unsigned long vma_size;
3583 unsigned long nr_pages;
3584 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003585 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003586
Peter Zijlstrac7920612010-05-18 10:33:24 +02003587 /*
3588 * Don't allow mmap() of inherited per-task counters. This would
3589 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003590 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003591 */
3592 if (event->cpu == -1 && event->attr.inherit)
3593 return -EINVAL;
3594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003595 if (!(vma->vm_flags & VM_SHARED))
3596 return -EINVAL;
3597
3598 vma_size = vma->vm_end - vma->vm_start;
3599 nr_pages = (vma_size / PAGE_SIZE) - 1;
3600
3601 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003602 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003603 * can do bitmasks instead of modulo.
3604 */
3605 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3606 return -EINVAL;
3607
3608 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3609 return -EINVAL;
3610
3611 if (vma->vm_pgoff != 0)
3612 return -EINVAL;
3613
3614 WARN_ON_ONCE(event->ctx->parent_ctx);
3615 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003616 if (event->rb) {
3617 if (event->rb->nr_pages == nr_pages)
3618 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003619 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003620 ret = -EINVAL;
3621 goto unlock;
3622 }
3623
3624 user_extra = nr_pages + 1;
3625 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3626
3627 /*
3628 * Increase the limit linearly with more CPUs:
3629 */
3630 user_lock_limit *= num_online_cpus();
3631
3632 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3633
3634 extra = 0;
3635 if (user_locked > user_lock_limit)
3636 extra = user_locked - user_lock_limit;
3637
Jiri Slaby78d7d402010-03-05 13:42:54 -08003638 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003639 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003640 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003641
3642 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3643 !capable(CAP_IPC_LOCK)) {
3644 ret = -EPERM;
3645 goto unlock;
3646 }
3647
Frederic Weisbecker76369132011-05-19 19:55:04 +02003648 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003649
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003650 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003651 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003652
Vince Weaver4ec83632011-06-01 15:15:36 -04003653 rb = rb_alloc(nr_pages,
3654 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3655 event->cpu, flags);
3656
Frederic Weisbecker76369132011-05-19 19:55:04 +02003657 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003658 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003659 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003660 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003661 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003662
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003663 atomic_long_add(user_extra, &user->locked_vm);
3664 event->mmap_locked = extra;
3665 event->mmap_user = get_current_user();
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003666 vma->vm_mm->pinned_vm += event->mmap_locked;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003667
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003668 perf_event_update_userpage(event);
3669
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003670unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003671 if (!ret)
3672 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673 mutex_unlock(&event->mmap_mutex);
3674
3675 vma->vm_flags |= VM_RESERVED;
3676 vma->vm_ops = &perf_mmap_vmops;
3677
3678 return ret;
3679}
3680
3681static int perf_fasync(int fd, struct file *filp, int on)
3682{
3683 struct inode *inode = filp->f_path.dentry->d_inode;
3684 struct perf_event *event = filp->private_data;
3685 int retval;
3686
3687 mutex_lock(&inode->i_mutex);
3688 retval = fasync_helper(fd, filp, on, &event->fasync);
3689 mutex_unlock(&inode->i_mutex);
3690
3691 if (retval < 0)
3692 return retval;
3693
3694 return 0;
3695}
3696
3697static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003698 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699 .release = perf_release,
3700 .read = perf_read,
3701 .poll = perf_poll,
3702 .unlocked_ioctl = perf_ioctl,
3703 .compat_ioctl = perf_ioctl,
3704 .mmap = perf_mmap,
3705 .fasync = perf_fasync,
3706};
3707
3708/*
3709 * Perf event wakeup
3710 *
3711 * If there's data, ensure we set the poll() state and publish everything
3712 * to user-space before waking everybody up.
3713 */
3714
3715void perf_event_wakeup(struct perf_event *event)
3716{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003717 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003718
3719 if (event->pending_kill) {
3720 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3721 event->pending_kill = 0;
3722 }
3723}
3724
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003725static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003726{
3727 struct perf_event *event = container_of(entry,
3728 struct perf_event, pending);
3729
3730 if (event->pending_disable) {
3731 event->pending_disable = 0;
3732 __perf_event_disable(event);
3733 }
3734
3735 if (event->pending_wakeup) {
3736 event->pending_wakeup = 0;
3737 perf_event_wakeup(event);
3738 }
3739}
3740
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003741/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003742 * We assume there is only KVM supporting the callbacks.
3743 * Later on, we might change it to a list if there is
3744 * another virtualization implementation supporting the callbacks.
3745 */
3746struct perf_guest_info_callbacks *perf_guest_cbs;
3747
3748int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3749{
3750 perf_guest_cbs = cbs;
3751 return 0;
3752}
3753EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3754
3755int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3756{
3757 perf_guest_cbs = NULL;
3758 return 0;
3759}
3760EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3761
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003762static void __perf_event_header__init_id(struct perf_event_header *header,
3763 struct perf_sample_data *data,
3764 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003765{
3766 u64 sample_type = event->attr.sample_type;
3767
3768 data->type = sample_type;
3769 header->size += event->id_header_size;
3770
3771 if (sample_type & PERF_SAMPLE_TID) {
3772 /* namespace issues */
3773 data->tid_entry.pid = perf_event_pid(event, current);
3774 data->tid_entry.tid = perf_event_tid(event, current);
3775 }
3776
3777 if (sample_type & PERF_SAMPLE_TIME)
3778 data->time = perf_clock();
3779
3780 if (sample_type & PERF_SAMPLE_ID)
3781 data->id = primary_event_id(event);
3782
3783 if (sample_type & PERF_SAMPLE_STREAM_ID)
3784 data->stream_id = event->id;
3785
3786 if (sample_type & PERF_SAMPLE_CPU) {
3787 data->cpu_entry.cpu = raw_smp_processor_id();
3788 data->cpu_entry.reserved = 0;
3789 }
3790}
3791
Frederic Weisbecker76369132011-05-19 19:55:04 +02003792void perf_event_header__init_id(struct perf_event_header *header,
3793 struct perf_sample_data *data,
3794 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003795{
3796 if (event->attr.sample_id_all)
3797 __perf_event_header__init_id(header, data, event);
3798}
3799
3800static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3801 struct perf_sample_data *data)
3802{
3803 u64 sample_type = data->type;
3804
3805 if (sample_type & PERF_SAMPLE_TID)
3806 perf_output_put(handle, data->tid_entry);
3807
3808 if (sample_type & PERF_SAMPLE_TIME)
3809 perf_output_put(handle, data->time);
3810
3811 if (sample_type & PERF_SAMPLE_ID)
3812 perf_output_put(handle, data->id);
3813
3814 if (sample_type & PERF_SAMPLE_STREAM_ID)
3815 perf_output_put(handle, data->stream_id);
3816
3817 if (sample_type & PERF_SAMPLE_CPU)
3818 perf_output_put(handle, data->cpu_entry);
3819}
3820
Frederic Weisbecker76369132011-05-19 19:55:04 +02003821void perf_event__output_id_sample(struct perf_event *event,
3822 struct perf_output_handle *handle,
3823 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003824{
3825 if (event->attr.sample_id_all)
3826 __perf_event__output_id_sample(handle, sample);
3827}
3828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003830 struct perf_event *event,
3831 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003832{
3833 u64 read_format = event->attr.read_format;
3834 u64 values[4];
3835 int n = 0;
3836
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003837 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003838 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003839 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003840 atomic64_read(&event->child_total_time_enabled);
3841 }
3842 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003843 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003844 atomic64_read(&event->child_total_time_running);
3845 }
3846 if (read_format & PERF_FORMAT_ID)
3847 values[n++] = primary_event_id(event);
3848
Frederic Weisbecker76369132011-05-19 19:55:04 +02003849 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003850}
3851
3852/*
3853 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3854 */
3855static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003856 struct perf_event *event,
3857 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003858{
3859 struct perf_event *leader = event->group_leader, *sub;
3860 u64 read_format = event->attr.read_format;
3861 u64 values[5];
3862 int n = 0;
3863
3864 values[n++] = 1 + leader->nr_siblings;
3865
3866 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003867 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868
3869 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003870 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871
3872 if (leader != event)
3873 leader->pmu->read(leader);
3874
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003875 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003876 if (read_format & PERF_FORMAT_ID)
3877 values[n++] = primary_event_id(leader);
3878
Frederic Weisbecker76369132011-05-19 19:55:04 +02003879 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880
3881 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3882 n = 0;
3883
3884 if (sub != event)
3885 sub->pmu->read(sub);
3886
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003887 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 if (read_format & PERF_FORMAT_ID)
3889 values[n++] = primary_event_id(sub);
3890
Frederic Weisbecker76369132011-05-19 19:55:04 +02003891 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892 }
3893}
3894
Stephane Eranianeed01522010-10-26 16:08:01 +02003895#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3896 PERF_FORMAT_TOTAL_TIME_RUNNING)
3897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003898static void perf_output_read(struct perf_output_handle *handle,
3899 struct perf_event *event)
3900{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003901 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02003902 u64 read_format = event->attr.read_format;
3903
3904 /*
3905 * compute total_time_enabled, total_time_running
3906 * based on snapshot values taken when the event
3907 * was last scheduled in.
3908 *
3909 * we cannot simply called update_context_time()
3910 * because of locking issue as we are called in
3911 * NMI context
3912 */
Eric B Munsonc4794292011-06-23 16:34:38 -04003913 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003914 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02003915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003917 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003919 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920}
3921
3922void perf_output_sample(struct perf_output_handle *handle,
3923 struct perf_event_header *header,
3924 struct perf_sample_data *data,
3925 struct perf_event *event)
3926{
3927 u64 sample_type = data->type;
3928
3929 perf_output_put(handle, *header);
3930
3931 if (sample_type & PERF_SAMPLE_IP)
3932 perf_output_put(handle, data->ip);
3933
3934 if (sample_type & PERF_SAMPLE_TID)
3935 perf_output_put(handle, data->tid_entry);
3936
3937 if (sample_type & PERF_SAMPLE_TIME)
3938 perf_output_put(handle, data->time);
3939
3940 if (sample_type & PERF_SAMPLE_ADDR)
3941 perf_output_put(handle, data->addr);
3942
3943 if (sample_type & PERF_SAMPLE_ID)
3944 perf_output_put(handle, data->id);
3945
3946 if (sample_type & PERF_SAMPLE_STREAM_ID)
3947 perf_output_put(handle, data->stream_id);
3948
3949 if (sample_type & PERF_SAMPLE_CPU)
3950 perf_output_put(handle, data->cpu_entry);
3951
3952 if (sample_type & PERF_SAMPLE_PERIOD)
3953 perf_output_put(handle, data->period);
3954
3955 if (sample_type & PERF_SAMPLE_READ)
3956 perf_output_read(handle, event);
3957
3958 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3959 if (data->callchain) {
3960 int size = 1;
3961
3962 if (data->callchain)
3963 size += data->callchain->nr;
3964
3965 size *= sizeof(u64);
3966
Frederic Weisbecker76369132011-05-19 19:55:04 +02003967 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003968 } else {
3969 u64 nr = 0;
3970 perf_output_put(handle, nr);
3971 }
3972 }
3973
3974 if (sample_type & PERF_SAMPLE_RAW) {
3975 if (data->raw) {
3976 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003977 __output_copy(handle, data->raw->data,
3978 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979 } else {
3980 struct {
3981 u32 size;
3982 u32 data;
3983 } raw = {
3984 .size = sizeof(u32),
3985 .data = 0,
3986 };
3987 perf_output_put(handle, raw);
3988 }
3989 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02003990
3991 if (!event->attr.watermark) {
3992 int wakeup_events = event->attr.wakeup_events;
3993
3994 if (wakeup_events) {
3995 struct ring_buffer *rb = handle->rb;
3996 int events = local_inc_return(&rb->events);
3997
3998 if (events >= wakeup_events) {
3999 local_sub(wakeup_events, &rb->events);
4000 local_inc(&rb->wakeup);
4001 }
4002 }
4003 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004004
4005 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4006 if (data->br_stack) {
4007 size_t size;
4008
4009 size = data->br_stack->nr
4010 * sizeof(struct perf_branch_entry);
4011
4012 perf_output_put(handle, data->br_stack->nr);
4013 perf_output_copy(handle, data->br_stack->entries, size);
4014 } else {
4015 /*
4016 * we always store at least the value of nr
4017 */
4018 u64 nr = 0;
4019 perf_output_put(handle, nr);
4020 }
4021 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004022}
4023
4024void perf_prepare_sample(struct perf_event_header *header,
4025 struct perf_sample_data *data,
4026 struct perf_event *event,
4027 struct pt_regs *regs)
4028{
4029 u64 sample_type = event->attr.sample_type;
4030
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004032 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004033
4034 header->misc = 0;
4035 header->misc |= perf_misc_flags(regs);
4036
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004037 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004038
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004039 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040 data->ip = perf_instruction_pointer(regs);
4041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004042 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4043 int size = 1;
4044
4045 data->callchain = perf_callchain(regs);
4046
4047 if (data->callchain)
4048 size += data->callchain->nr;
4049
4050 header->size += size * sizeof(u64);
4051 }
4052
4053 if (sample_type & PERF_SAMPLE_RAW) {
4054 int size = sizeof(u32);
4055
4056 if (data->raw)
4057 size += data->raw->size;
4058 else
4059 size += sizeof(u32);
4060
4061 WARN_ON_ONCE(size & (sizeof(u64)-1));
4062 header->size += size;
4063 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004064
4065 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4066 int size = sizeof(u64); /* nr */
4067 if (data->br_stack) {
4068 size += data->br_stack->nr
4069 * sizeof(struct perf_branch_entry);
4070 }
4071 header->size += size;
4072 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004073}
4074
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004075static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076 struct perf_sample_data *data,
4077 struct pt_regs *regs)
4078{
4079 struct perf_output_handle handle;
4080 struct perf_event_header header;
4081
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004082 /* protect the callchain buffers */
4083 rcu_read_lock();
4084
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085 perf_prepare_sample(&header, data, event, regs);
4086
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004087 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004088 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004089
4090 perf_output_sample(&handle, &header, data, event);
4091
4092 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004093
4094exit:
4095 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096}
4097
4098/*
4099 * read event_id
4100 */
4101
4102struct perf_read_event {
4103 struct perf_event_header header;
4104
4105 u32 pid;
4106 u32 tid;
4107};
4108
4109static void
4110perf_event_read_event(struct perf_event *event,
4111 struct task_struct *task)
4112{
4113 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004114 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115 struct perf_read_event read_event = {
4116 .header = {
4117 .type = PERF_RECORD_READ,
4118 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004119 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120 },
4121 .pid = perf_event_pid(event, task),
4122 .tid = perf_event_tid(event, task),
4123 };
4124 int ret;
4125
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004126 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004127 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128 if (ret)
4129 return;
4130
4131 perf_output_put(&handle, read_event);
4132 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004133 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134
4135 perf_output_end(&handle);
4136}
4137
4138/*
4139 * task tracking -- fork/exit
4140 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004141 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004142 */
4143
4144struct perf_task_event {
4145 struct task_struct *task;
4146 struct perf_event_context *task_ctx;
4147
4148 struct {
4149 struct perf_event_header header;
4150
4151 u32 pid;
4152 u32 ppid;
4153 u32 tid;
4154 u32 ptid;
4155 u64 time;
4156 } event_id;
4157};
4158
4159static void perf_event_task_output(struct perf_event *event,
4160 struct perf_task_event *task_event)
4161{
4162 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004163 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004164 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004165 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004166
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004167 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004168
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004169 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004170 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004171 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004172 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004173
4174 task_event->event_id.pid = perf_event_pid(event, task);
4175 task_event->event_id.ppid = perf_event_pid(event, current);
4176
4177 task_event->event_id.tid = perf_event_tid(event, task);
4178 task_event->event_id.ptid = perf_event_tid(event, current);
4179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180 perf_output_put(&handle, task_event->event_id);
4181
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004182 perf_event__output_id_sample(event, &handle, &sample);
4183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004185out:
4186 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004187}
4188
4189static int perf_event_task_match(struct perf_event *event)
4190{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004191 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004192 return 0;
4193
Stephane Eranian5632ab12011-01-03 18:20:01 +02004194 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004195 return 0;
4196
Eric B Munson3af9e852010-05-18 15:30:49 +01004197 if (event->attr.comm || event->attr.mmap ||
4198 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199 return 1;
4200
4201 return 0;
4202}
4203
4204static void perf_event_task_ctx(struct perf_event_context *ctx,
4205 struct perf_task_event *task_event)
4206{
4207 struct perf_event *event;
4208
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004209 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4210 if (perf_event_task_match(event))
4211 perf_event_task_output(event, task_event);
4212 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213}
4214
4215static void perf_event_task_event(struct perf_task_event *task_event)
4216{
4217 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004218 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004219 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004220 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004221
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004222 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004223 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004224 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004225 if (cpuctx->active_pmu != pmu)
4226 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004227 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004228
4229 ctx = task_event->task_ctx;
4230 if (!ctx) {
4231 ctxn = pmu->task_ctx_nr;
4232 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004233 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004234 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4235 }
4236 if (ctx)
4237 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004238next:
4239 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004240 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004241 rcu_read_unlock();
4242}
4243
4244static void perf_event_task(struct task_struct *task,
4245 struct perf_event_context *task_ctx,
4246 int new)
4247{
4248 struct perf_task_event task_event;
4249
4250 if (!atomic_read(&nr_comm_events) &&
4251 !atomic_read(&nr_mmap_events) &&
4252 !atomic_read(&nr_task_events))
4253 return;
4254
4255 task_event = (struct perf_task_event){
4256 .task = task,
4257 .task_ctx = task_ctx,
4258 .event_id = {
4259 .header = {
4260 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4261 .misc = 0,
4262 .size = sizeof(task_event.event_id),
4263 },
4264 /* .pid */
4265 /* .ppid */
4266 /* .tid */
4267 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004268 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 },
4270 };
4271
4272 perf_event_task_event(&task_event);
4273}
4274
4275void perf_event_fork(struct task_struct *task)
4276{
4277 perf_event_task(task, NULL, 1);
4278}
4279
4280/*
4281 * comm tracking
4282 */
4283
4284struct perf_comm_event {
4285 struct task_struct *task;
4286 char *comm;
4287 int comm_size;
4288
4289 struct {
4290 struct perf_event_header header;
4291
4292 u32 pid;
4293 u32 tid;
4294 } event_id;
4295};
4296
4297static void perf_event_comm_output(struct perf_event *event,
4298 struct perf_comm_event *comm_event)
4299{
4300 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004301 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004303 int ret;
4304
4305 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4306 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004307 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308
4309 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004310 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311
4312 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4313 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4314
4315 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004316 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004318
4319 perf_event__output_id_sample(event, &handle, &sample);
4320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004321 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004322out:
4323 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004324}
4325
4326static int perf_event_comm_match(struct perf_event *event)
4327{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004328 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004329 return 0;
4330
Stephane Eranian5632ab12011-01-03 18:20:01 +02004331 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004332 return 0;
4333
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004334 if (event->attr.comm)
4335 return 1;
4336
4337 return 0;
4338}
4339
4340static void perf_event_comm_ctx(struct perf_event_context *ctx,
4341 struct perf_comm_event *comm_event)
4342{
4343 struct perf_event *event;
4344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004345 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4346 if (perf_event_comm_match(event))
4347 perf_event_comm_output(event, comm_event);
4348 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004349}
4350
4351static void perf_event_comm_event(struct perf_comm_event *comm_event)
4352{
4353 struct perf_cpu_context *cpuctx;
4354 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004356 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004357 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004358 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004359
4360 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004361 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004362 size = ALIGN(strlen(comm)+1, sizeof(u64));
4363
4364 comm_event->comm = comm;
4365 comm_event->comm_size = size;
4366
4367 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004368 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004369 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004370 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004371 if (cpuctx->active_pmu != pmu)
4372 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004373 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004374
4375 ctxn = pmu->task_ctx_nr;
4376 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004377 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004378
4379 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4380 if (ctx)
4381 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004382next:
4383 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004384 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004385 rcu_read_unlock();
4386}
4387
4388void perf_event_comm(struct task_struct *task)
4389{
4390 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004391 struct perf_event_context *ctx;
4392 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004393
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004394 for_each_task_context_nr(ctxn) {
4395 ctx = task->perf_event_ctxp[ctxn];
4396 if (!ctx)
4397 continue;
4398
4399 perf_event_enable_on_exec(ctx);
4400 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004401
4402 if (!atomic_read(&nr_comm_events))
4403 return;
4404
4405 comm_event = (struct perf_comm_event){
4406 .task = task,
4407 /* .comm */
4408 /* .comm_size */
4409 .event_id = {
4410 .header = {
4411 .type = PERF_RECORD_COMM,
4412 .misc = 0,
4413 /* .size */
4414 },
4415 /* .pid */
4416 /* .tid */
4417 },
4418 };
4419
4420 perf_event_comm_event(&comm_event);
4421}
4422
4423/*
4424 * mmap tracking
4425 */
4426
4427struct perf_mmap_event {
4428 struct vm_area_struct *vma;
4429
4430 const char *file_name;
4431 int file_size;
4432
4433 struct {
4434 struct perf_event_header header;
4435
4436 u32 pid;
4437 u32 tid;
4438 u64 start;
4439 u64 len;
4440 u64 pgoff;
4441 } event_id;
4442};
4443
4444static void perf_event_mmap_output(struct perf_event *event,
4445 struct perf_mmap_event *mmap_event)
4446{
4447 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004448 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004449 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004450 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004452 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4453 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004454 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004455 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004456 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004457
4458 mmap_event->event_id.pid = perf_event_pid(event, current);
4459 mmap_event->event_id.tid = perf_event_tid(event, current);
4460
4461 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004462 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004464
4465 perf_event__output_id_sample(event, &handle, &sample);
4466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004468out:
4469 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004470}
4471
4472static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004473 struct perf_mmap_event *mmap_event,
4474 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004476 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004477 return 0;
4478
Stephane Eranian5632ab12011-01-03 18:20:01 +02004479 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004480 return 0;
4481
Eric B Munson3af9e852010-05-18 15:30:49 +01004482 if ((!executable && event->attr.mmap_data) ||
4483 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484 return 1;
4485
4486 return 0;
4487}
4488
4489static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004490 struct perf_mmap_event *mmap_event,
4491 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492{
4493 struct perf_event *event;
4494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004496 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 perf_event_mmap_output(event, mmap_event);
4498 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004499}
4500
4501static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4502{
4503 struct perf_cpu_context *cpuctx;
4504 struct perf_event_context *ctx;
4505 struct vm_area_struct *vma = mmap_event->vma;
4506 struct file *file = vma->vm_file;
4507 unsigned int size;
4508 char tmp[16];
4509 char *buf = NULL;
4510 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004511 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004512 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513
4514 memset(tmp, 0, sizeof(tmp));
4515
4516 if (file) {
4517 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004518 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004519 * need to add enough zero bytes after the string to handle
4520 * the 64bit alignment we do later.
4521 */
4522 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4523 if (!buf) {
4524 name = strncpy(tmp, "//enomem", sizeof(tmp));
4525 goto got_name;
4526 }
4527 name = d_path(&file->f_path, buf, PATH_MAX);
4528 if (IS_ERR(name)) {
4529 name = strncpy(tmp, "//toolong", sizeof(tmp));
4530 goto got_name;
4531 }
4532 } else {
4533 if (arch_vma_name(mmap_event->vma)) {
4534 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4535 sizeof(tmp));
4536 goto got_name;
4537 }
4538
4539 if (!vma->vm_mm) {
4540 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4541 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004542 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4543 vma->vm_end >= vma->vm_mm->brk) {
4544 name = strncpy(tmp, "[heap]", sizeof(tmp));
4545 goto got_name;
4546 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4547 vma->vm_end >= vma->vm_mm->start_stack) {
4548 name = strncpy(tmp, "[stack]", sizeof(tmp));
4549 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550 }
4551
4552 name = strncpy(tmp, "//anon", sizeof(tmp));
4553 goto got_name;
4554 }
4555
4556got_name:
4557 size = ALIGN(strlen(name)+1, sizeof(u64));
4558
4559 mmap_event->file_name = name;
4560 mmap_event->file_size = size;
4561
4562 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4563
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004564 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004565 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004566 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004567 if (cpuctx->active_pmu != pmu)
4568 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004569 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4570 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004571
4572 ctxn = pmu->task_ctx_nr;
4573 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004574 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004575
4576 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4577 if (ctx) {
4578 perf_event_mmap_ctx(ctx, mmap_event,
4579 vma->vm_flags & VM_EXEC);
4580 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004581next:
4582 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004583 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004584 rcu_read_unlock();
4585
4586 kfree(buf);
4587}
4588
Eric B Munson3af9e852010-05-18 15:30:49 +01004589void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004590{
4591 struct perf_mmap_event mmap_event;
4592
4593 if (!atomic_read(&nr_mmap_events))
4594 return;
4595
4596 mmap_event = (struct perf_mmap_event){
4597 .vma = vma,
4598 /* .file_name */
4599 /* .file_size */
4600 .event_id = {
4601 .header = {
4602 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004603 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004604 /* .size */
4605 },
4606 /* .pid */
4607 /* .tid */
4608 .start = vma->vm_start,
4609 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004610 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611 },
4612 };
4613
4614 perf_event_mmap_event(&mmap_event);
4615}
4616
4617/*
4618 * IRQ throttle logging
4619 */
4620
4621static void perf_log_throttle(struct perf_event *event, int enable)
4622{
4623 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004624 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004625 int ret;
4626
4627 struct {
4628 struct perf_event_header header;
4629 u64 time;
4630 u64 id;
4631 u64 stream_id;
4632 } throttle_event = {
4633 .header = {
4634 .type = PERF_RECORD_THROTTLE,
4635 .misc = 0,
4636 .size = sizeof(throttle_event),
4637 },
4638 .time = perf_clock(),
4639 .id = primary_event_id(event),
4640 .stream_id = event->id,
4641 };
4642
4643 if (enable)
4644 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4645
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004646 perf_event_header__init_id(&throttle_event.header, &sample, event);
4647
4648 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004649 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004650 if (ret)
4651 return;
4652
4653 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004654 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004655 perf_output_end(&handle);
4656}
4657
4658/*
4659 * Generic event overflow handling, sampling.
4660 */
4661
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004662static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004663 int throttle, struct perf_sample_data *data,
4664 struct pt_regs *regs)
4665{
4666 int events = atomic_read(&event->event_limit);
4667 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004668 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669 int ret = 0;
4670
Peter Zijlstra96398822010-11-24 18:55:29 +01004671 /*
4672 * Non-sampling counters might still use the PMI to fold short
4673 * hardware counters, ignore those.
4674 */
4675 if (unlikely(!is_sampling_event(event)))
4676 return 0;
4677
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004678 seq = __this_cpu_read(perf_throttled_seq);
4679 if (seq != hwc->interrupts_seq) {
4680 hwc->interrupts_seq = seq;
4681 hwc->interrupts = 1;
4682 } else {
4683 hwc->interrupts++;
4684 if (unlikely(throttle
4685 && hwc->interrupts >= max_samples_per_tick)) {
4686 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01004687 hwc->interrupts = MAX_INTERRUPTS;
4688 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004689 ret = 1;
4690 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004692
4693 if (event->attr.freq) {
4694 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004695 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696
Peter Zijlstraabd50712010-01-26 18:50:16 +01004697 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
Peter Zijlstraabd50712010-01-26 18:50:16 +01004699 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004700 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004701 }
4702
4703 /*
4704 * XXX event_limit might not quite work as expected on inherited
4705 * events
4706 */
4707
4708 event->pending_kill = POLL_IN;
4709 if (events && atomic_dec_and_test(&event->event_limit)) {
4710 ret = 1;
4711 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004712 event->pending_disable = 1;
4713 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004714 }
4715
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004716 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004717 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004718 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004719 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004720
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004721 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004722 event->pending_wakeup = 1;
4723 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004724 }
4725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004726 return ret;
4727}
4728
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004729int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730 struct perf_sample_data *data,
4731 struct pt_regs *regs)
4732{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004733 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004734}
4735
4736/*
4737 * Generic software event infrastructure
4738 */
4739
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004740struct swevent_htable {
4741 struct swevent_hlist *swevent_hlist;
4742 struct mutex hlist_mutex;
4743 int hlist_refcount;
4744
4745 /* Recursion avoidance in each contexts */
4746 int recursion[PERF_NR_CONTEXTS];
4747};
4748
4749static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751/*
4752 * We directly increment event->count and keep a second value in
4753 * event->hw.period_left to count intervals. This period event
4754 * is kept in the range [-sample_period, 0] so that we can use the
4755 * sign as trigger.
4756 */
4757
4758static u64 perf_swevent_set_period(struct perf_event *event)
4759{
4760 struct hw_perf_event *hwc = &event->hw;
4761 u64 period = hwc->last_period;
4762 u64 nr, offset;
4763 s64 old, val;
4764
4765 hwc->last_period = hwc->sample_period;
4766
4767again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004768 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769 if (val < 0)
4770 return 0;
4771
4772 nr = div64_u64(period + val, period);
4773 offset = nr * period;
4774 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004775 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776 goto again;
4777
4778 return nr;
4779}
4780
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004781static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004782 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004783 struct pt_regs *regs)
4784{
4785 struct hw_perf_event *hwc = &event->hw;
4786 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004787
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004788 if (!overflow)
4789 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790
4791 if (hwc->interrupts == MAX_INTERRUPTS)
4792 return;
4793
4794 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004795 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004796 data, regs)) {
4797 /*
4798 * We inhibit the overflow from happening when
4799 * hwc->interrupts == MAX_INTERRUPTS.
4800 */
4801 break;
4802 }
4803 throttle = 1;
4804 }
4805}
4806
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004807static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004808 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004809 struct pt_regs *regs)
4810{
4811 struct hw_perf_event *hwc = &event->hw;
4812
Peter Zijlstrae7850592010-05-21 14:43:08 +02004813 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004814
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004815 if (!regs)
4816 return;
4817
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004818 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004819 return;
4820
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03004821 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
4822 data->period = nr;
4823 return perf_swevent_overflow(event, 1, data, regs);
4824 } else
4825 data->period = event->hw.last_period;
4826
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004827 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004828 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004829
Peter Zijlstrae7850592010-05-21 14:43:08 +02004830 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004831 return;
4832
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004833 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004834}
4835
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004836static int perf_exclude_event(struct perf_event *event,
4837 struct pt_regs *regs)
4838{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004839 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01004840 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004841
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004842 if (regs) {
4843 if (event->attr.exclude_user && user_mode(regs))
4844 return 1;
4845
4846 if (event->attr.exclude_kernel && !user_mode(regs))
4847 return 1;
4848 }
4849
4850 return 0;
4851}
4852
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004853static int perf_swevent_match(struct perf_event *event,
4854 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004855 u32 event_id,
4856 struct perf_sample_data *data,
4857 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004858{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859 if (event->attr.type != type)
4860 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004861
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862 if (event->attr.config != event_id)
4863 return 0;
4864
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004865 if (perf_exclude_event(event, regs))
4866 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867
4868 return 1;
4869}
4870
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004871static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004873 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004875 return hash_64(val, SWEVENT_HLIST_BITS);
4876}
4877
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004878static inline struct hlist_head *
4879__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004880{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004881 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004882
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004883 return &hlist->heads[hash];
4884}
4885
4886/* For the read side: events when they trigger */
4887static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004888find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004889{
4890 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004891
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004892 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004893 if (!hlist)
4894 return NULL;
4895
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004896 return __find_swevent_head(hlist, type, event_id);
4897}
4898
4899/* For the event head insertion and removal in the hlist */
4900static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004901find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004902{
4903 struct swevent_hlist *hlist;
4904 u32 event_id = event->attr.config;
4905 u64 type = event->attr.type;
4906
4907 /*
4908 * Event scheduling is always serialized against hlist allocation
4909 * and release. Which makes the protected version suitable here.
4910 * The context lock guarantees that.
4911 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004912 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004913 lockdep_is_held(&event->ctx->lock));
4914 if (!hlist)
4915 return NULL;
4916
4917 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004918}
4919
4920static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004921 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004922 struct perf_sample_data *data,
4923 struct pt_regs *regs)
4924{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004925 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004926 struct perf_event *event;
4927 struct hlist_node *node;
4928 struct hlist_head *head;
4929
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004930 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004931 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004932 if (!head)
4933 goto end;
4934
4935 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004936 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004937 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004938 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004939end:
4940 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004941}
4942
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004943int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004944{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004945 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004946
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004947 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004948}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004949EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004951inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004953 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004954
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004955 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004956}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004957
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004958void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004959{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004960 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004961 int rctx;
4962
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004963 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004964 rctx = perf_swevent_get_recursion_context();
4965 if (rctx < 0)
4966 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004967
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004968 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004969
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004970 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004971
4972 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004973 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004974}
4975
4976static void perf_swevent_read(struct perf_event *event)
4977{
4978}
4979
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004980static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004981{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004982 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004983 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004984 struct hlist_head *head;
4985
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004986 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987 hwc->last_period = hwc->sample_period;
4988 perf_swevent_set_period(event);
4989 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004990
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004991 hwc->state = !(flags & PERF_EF_START);
4992
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004993 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004994 if (WARN_ON_ONCE(!head))
4995 return -EINVAL;
4996
4997 hlist_add_head_rcu(&event->hlist_entry, head);
4998
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004999 return 0;
5000}
5001
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005002static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005004 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005005}
5006
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005007static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005008{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005009 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005010}
5011
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005012static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005013{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005014 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005015}
5016
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005017/* Deref the hlist from the update side */
5018static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005019swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005020{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005021 return rcu_dereference_protected(swhash->swevent_hlist,
5022 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005023}
5024
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005025static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005026{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005027 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005028
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005029 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005030 return;
5031
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005032 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005033 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005034}
5035
5036static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5037{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005038 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005039
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005040 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005041
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005042 if (!--swhash->hlist_refcount)
5043 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005044
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005045 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005046}
5047
5048static void swevent_hlist_put(struct perf_event *event)
5049{
5050 int cpu;
5051
5052 if (event->cpu != -1) {
5053 swevent_hlist_put_cpu(event, event->cpu);
5054 return;
5055 }
5056
5057 for_each_possible_cpu(cpu)
5058 swevent_hlist_put_cpu(event, cpu);
5059}
5060
5061static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5062{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005063 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005064 int err = 0;
5065
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005066 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005067
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005068 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005069 struct swevent_hlist *hlist;
5070
5071 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5072 if (!hlist) {
5073 err = -ENOMEM;
5074 goto exit;
5075 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005076 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005077 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005078 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005079exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005080 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005081
5082 return err;
5083}
5084
5085static int swevent_hlist_get(struct perf_event *event)
5086{
5087 int err;
5088 int cpu, failed_cpu;
5089
5090 if (event->cpu != -1)
5091 return swevent_hlist_get_cpu(event, event->cpu);
5092
5093 get_online_cpus();
5094 for_each_possible_cpu(cpu) {
5095 err = swevent_hlist_get_cpu(event, cpu);
5096 if (err) {
5097 failed_cpu = cpu;
5098 goto fail;
5099 }
5100 }
5101 put_online_cpus();
5102
5103 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005104fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005105 for_each_possible_cpu(cpu) {
5106 if (cpu == failed_cpu)
5107 break;
5108 swevent_hlist_put_cpu(event, cpu);
5109 }
5110
5111 put_online_cpus();
5112 return err;
5113}
5114
Ingo Molnarc5905af2012-02-24 08:31:31 +01005115struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005116
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005117static void sw_perf_event_destroy(struct perf_event *event)
5118{
5119 u64 event_id = event->attr.config;
5120
5121 WARN_ON(event->parent);
5122
Ingo Molnarc5905af2012-02-24 08:31:31 +01005123 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005124 swevent_hlist_put(event);
5125}
5126
5127static int perf_swevent_init(struct perf_event *event)
5128{
5129 int event_id = event->attr.config;
5130
5131 if (event->attr.type != PERF_TYPE_SOFTWARE)
5132 return -ENOENT;
5133
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005134 /*
5135 * no branch sampling for software events
5136 */
5137 if (has_branch_stack(event))
5138 return -EOPNOTSUPP;
5139
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005140 switch (event_id) {
5141 case PERF_COUNT_SW_CPU_CLOCK:
5142 case PERF_COUNT_SW_TASK_CLOCK:
5143 return -ENOENT;
5144
5145 default:
5146 break;
5147 }
5148
Dan Carpenterce677832010-10-24 21:50:42 +02005149 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005150 return -ENOENT;
5151
5152 if (!event->parent) {
5153 int err;
5154
5155 err = swevent_hlist_get(event);
5156 if (err)
5157 return err;
5158
Ingo Molnarc5905af2012-02-24 08:31:31 +01005159 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005160 event->destroy = sw_perf_event_destroy;
5161 }
5162
5163 return 0;
5164}
5165
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005166static int perf_swevent_event_idx(struct perf_event *event)
5167{
5168 return 0;
5169}
5170
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005171static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005172 .task_ctx_nr = perf_sw_context,
5173
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005174 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005175 .add = perf_swevent_add,
5176 .del = perf_swevent_del,
5177 .start = perf_swevent_start,
5178 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005179 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005180
5181 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005182};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005183
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005184#ifdef CONFIG_EVENT_TRACING
5185
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005186static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005187 struct perf_sample_data *data)
5188{
5189 void *record = data->raw->data;
5190
5191 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5192 return 1;
5193 return 0;
5194}
5195
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005196static int perf_tp_event_match(struct perf_event *event,
5197 struct perf_sample_data *data,
5198 struct pt_regs *regs)
5199{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005200 if (event->hw.state & PERF_HES_STOPPED)
5201 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005202 /*
5203 * All tracepoints are from kernel-space.
5204 */
5205 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005206 return 0;
5207
5208 if (!perf_tp_filter_match(event, data))
5209 return 0;
5210
5211 return 1;
5212}
5213
5214void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005215 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005216{
5217 struct perf_sample_data data;
5218 struct perf_event *event;
5219 struct hlist_node *node;
5220
5221 struct perf_raw_record raw = {
5222 .size = entry_size,
5223 .data = record,
5224 };
5225
5226 perf_sample_data_init(&data, addr);
5227 data.raw = &raw;
5228
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005229 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5230 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005231 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005232 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005233
5234 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005235}
5236EXPORT_SYMBOL_GPL(perf_tp_event);
5237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005238static void tp_perf_event_destroy(struct perf_event *event)
5239{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005240 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241}
5242
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005243static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005245 int err;
5246
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005247 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5248 return -ENOENT;
5249
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005250 /*
5251 * no branch sampling for tracepoint events
5252 */
5253 if (has_branch_stack(event))
5254 return -EOPNOTSUPP;
5255
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005256 err = perf_trace_init(event);
5257 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005258 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259
5260 event->destroy = tp_perf_event_destroy;
5261
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005262 return 0;
5263}
5264
5265static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005266 .task_ctx_nr = perf_sw_context,
5267
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005268 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005269 .add = perf_trace_add,
5270 .del = perf_trace_del,
5271 .start = perf_swevent_start,
5272 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005273 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005274
5275 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005276};
5277
5278static inline void perf_tp_register(void)
5279{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005280 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281}
Li Zefan6fb29152009-10-15 11:21:42 +08005282
5283static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5284{
5285 char *filter_str;
5286 int ret;
5287
5288 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5289 return -EINVAL;
5290
5291 filter_str = strndup_user(arg, PAGE_SIZE);
5292 if (IS_ERR(filter_str))
5293 return PTR_ERR(filter_str);
5294
5295 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5296
5297 kfree(filter_str);
5298 return ret;
5299}
5300
5301static void perf_event_free_filter(struct perf_event *event)
5302{
5303 ftrace_profile_free_filter(event);
5304}
5305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306#else
Li Zefan6fb29152009-10-15 11:21:42 +08005307
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005308static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005309{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310}
Li Zefan6fb29152009-10-15 11:21:42 +08005311
5312static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5313{
5314 return -ENOENT;
5315}
5316
5317static void perf_event_free_filter(struct perf_event *event)
5318{
5319}
5320
Li Zefan07b139c2009-12-21 14:27:35 +08005321#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005322
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005323#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005324void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005325{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005326 struct perf_sample_data sample;
5327 struct pt_regs *regs = data;
5328
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005329 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005330
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005331 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005332 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005333}
5334#endif
5335
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005336/*
5337 * hrtimer based swevent callback
5338 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005340static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005342 enum hrtimer_restart ret = HRTIMER_RESTART;
5343 struct perf_sample_data data;
5344 struct pt_regs *regs;
5345 struct perf_event *event;
5346 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005348 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005349
5350 if (event->state != PERF_EVENT_STATE_ACTIVE)
5351 return HRTIMER_NORESTART;
5352
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005353 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005354
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005355 perf_sample_data_init(&data, 0);
5356 data.period = event->hw.last_period;
5357 regs = get_irq_regs();
5358
5359 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005360 if (!(event->attr.exclude_idle && is_idle_task(current)))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005361 if (perf_event_overflow(event, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005362 ret = HRTIMER_NORESTART;
5363 }
5364
5365 period = max_t(u64, 10000, event->hw.sample_period);
5366 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5367
5368 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369}
5370
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005371static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005373 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005374 s64 period;
5375
5376 if (!is_sampling_event(event))
5377 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005378
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005379 period = local64_read(&hwc->period_left);
5380 if (period) {
5381 if (period < 0)
5382 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005383
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005384 local64_set(&hwc->period_left, 0);
5385 } else {
5386 period = max_t(u64, 10000, hwc->sample_period);
5387 }
5388 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005389 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005390 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005391}
5392
5393static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5394{
5395 struct hw_perf_event *hwc = &event->hw;
5396
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005397 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005398 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005399 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005400
5401 hrtimer_cancel(&hwc->hrtimer);
5402 }
5403}
5404
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005405static void perf_swevent_init_hrtimer(struct perf_event *event)
5406{
5407 struct hw_perf_event *hwc = &event->hw;
5408
5409 if (!is_sampling_event(event))
5410 return;
5411
5412 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5413 hwc->hrtimer.function = perf_swevent_hrtimer;
5414
5415 /*
5416 * Since hrtimers have a fixed rate, we can do a static freq->period
5417 * mapping and avoid the whole period adjust feedback stuff.
5418 */
5419 if (event->attr.freq) {
5420 long freq = event->attr.sample_freq;
5421
5422 event->attr.sample_period = NSEC_PER_SEC / freq;
5423 hwc->sample_period = event->attr.sample_period;
5424 local64_set(&hwc->period_left, hwc->sample_period);
5425 event->attr.freq = 0;
5426 }
5427}
5428
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005429/*
5430 * Software event: cpu wall time clock
5431 */
5432
5433static void cpu_clock_event_update(struct perf_event *event)
5434{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005435 s64 prev;
5436 u64 now;
5437
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005438 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005439 prev = local64_xchg(&event->hw.prev_count, now);
5440 local64_add(now - prev, &event->count);
5441}
5442
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005443static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005444{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005445 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005446 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005447}
5448
5449static void cpu_clock_event_stop(struct perf_event *event, int flags)
5450{
5451 perf_swevent_cancel_hrtimer(event);
5452 cpu_clock_event_update(event);
5453}
5454
5455static int cpu_clock_event_add(struct perf_event *event, int flags)
5456{
5457 if (flags & PERF_EF_START)
5458 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005459
5460 return 0;
5461}
5462
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005463static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005464{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005465 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005466}
5467
5468static void cpu_clock_event_read(struct perf_event *event)
5469{
5470 cpu_clock_event_update(event);
5471}
5472
5473static int cpu_clock_event_init(struct perf_event *event)
5474{
5475 if (event->attr.type != PERF_TYPE_SOFTWARE)
5476 return -ENOENT;
5477
5478 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5479 return -ENOENT;
5480
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005481 /*
5482 * no branch sampling for software events
5483 */
5484 if (has_branch_stack(event))
5485 return -EOPNOTSUPP;
5486
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005487 perf_swevent_init_hrtimer(event);
5488
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005489 return 0;
5490}
5491
5492static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005493 .task_ctx_nr = perf_sw_context,
5494
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005495 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005496 .add = cpu_clock_event_add,
5497 .del = cpu_clock_event_del,
5498 .start = cpu_clock_event_start,
5499 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005500 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005501
5502 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005503};
5504
5505/*
5506 * Software event: task time clock
5507 */
5508
5509static void task_clock_event_update(struct perf_event *event, u64 now)
5510{
5511 u64 prev;
5512 s64 delta;
5513
5514 prev = local64_xchg(&event->hw.prev_count, now);
5515 delta = now - prev;
5516 local64_add(delta, &event->count);
5517}
5518
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005519static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005520{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005521 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005522 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005523}
5524
5525static void task_clock_event_stop(struct perf_event *event, int flags)
5526{
5527 perf_swevent_cancel_hrtimer(event);
5528 task_clock_event_update(event, event->ctx->time);
5529}
5530
5531static int task_clock_event_add(struct perf_event *event, int flags)
5532{
5533 if (flags & PERF_EF_START)
5534 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005535
5536 return 0;
5537}
5538
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005539static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005540{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005541 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005542}
5543
5544static void task_clock_event_read(struct perf_event *event)
5545{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005546 u64 now = perf_clock();
5547 u64 delta = now - event->ctx->timestamp;
5548 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005549
5550 task_clock_event_update(event, time);
5551}
5552
5553static int task_clock_event_init(struct perf_event *event)
5554{
5555 if (event->attr.type != PERF_TYPE_SOFTWARE)
5556 return -ENOENT;
5557
5558 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5559 return -ENOENT;
5560
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005561 /*
5562 * no branch sampling for software events
5563 */
5564 if (has_branch_stack(event))
5565 return -EOPNOTSUPP;
5566
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005567 perf_swevent_init_hrtimer(event);
5568
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005569 return 0;
5570}
5571
5572static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005573 .task_ctx_nr = perf_sw_context,
5574
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005575 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005576 .add = task_clock_event_add,
5577 .del = task_clock_event_del,
5578 .start = task_clock_event_start,
5579 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005580 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005581
5582 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005583};
5584
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005585static void perf_pmu_nop_void(struct pmu *pmu)
5586{
5587}
5588
5589static int perf_pmu_nop_int(struct pmu *pmu)
5590{
5591 return 0;
5592}
5593
5594static void perf_pmu_start_txn(struct pmu *pmu)
5595{
5596 perf_pmu_disable(pmu);
5597}
5598
5599static int perf_pmu_commit_txn(struct pmu *pmu)
5600{
5601 perf_pmu_enable(pmu);
5602 return 0;
5603}
5604
5605static void perf_pmu_cancel_txn(struct pmu *pmu)
5606{
5607 perf_pmu_enable(pmu);
5608}
5609
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005610static int perf_event_idx_default(struct perf_event *event)
5611{
5612 return event->hw.idx + 1;
5613}
5614
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005615/*
5616 * Ensures all contexts with the same task_ctx_nr have the same
5617 * pmu_cpu_context too.
5618 */
5619static void *find_pmu_context(int ctxn)
5620{
5621 struct pmu *pmu;
5622
5623 if (ctxn < 0)
5624 return NULL;
5625
5626 list_for_each_entry(pmu, &pmus, entry) {
5627 if (pmu->task_ctx_nr == ctxn)
5628 return pmu->pmu_cpu_context;
5629 }
5630
5631 return NULL;
5632}
5633
Peter Zijlstra51676952010-12-07 14:18:20 +01005634static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005635{
Peter Zijlstra51676952010-12-07 14:18:20 +01005636 int cpu;
5637
5638 for_each_possible_cpu(cpu) {
5639 struct perf_cpu_context *cpuctx;
5640
5641 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5642
5643 if (cpuctx->active_pmu == old_pmu)
5644 cpuctx->active_pmu = pmu;
5645 }
5646}
5647
5648static void free_pmu_context(struct pmu *pmu)
5649{
5650 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005651
5652 mutex_lock(&pmus_lock);
5653 /*
5654 * Like a real lame refcount.
5655 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005656 list_for_each_entry(i, &pmus, entry) {
5657 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5658 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005659 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005660 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005661 }
5662
Peter Zijlstra51676952010-12-07 14:18:20 +01005663 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005664out:
5665 mutex_unlock(&pmus_lock);
5666}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005667static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005668
Peter Zijlstraabe43402010-11-17 23:17:37 +01005669static ssize_t
5670type_show(struct device *dev, struct device_attribute *attr, char *page)
5671{
5672 struct pmu *pmu = dev_get_drvdata(dev);
5673
5674 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5675}
5676
5677static struct device_attribute pmu_dev_attrs[] = {
5678 __ATTR_RO(type),
5679 __ATTR_NULL,
5680};
5681
5682static int pmu_bus_running;
5683static struct bus_type pmu_bus = {
5684 .name = "event_source",
5685 .dev_attrs = pmu_dev_attrs,
5686};
5687
5688static void pmu_dev_release(struct device *dev)
5689{
5690 kfree(dev);
5691}
5692
5693static int pmu_dev_alloc(struct pmu *pmu)
5694{
5695 int ret = -ENOMEM;
5696
5697 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5698 if (!pmu->dev)
5699 goto out;
5700
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01005701 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01005702 device_initialize(pmu->dev);
5703 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5704 if (ret)
5705 goto free_dev;
5706
5707 dev_set_drvdata(pmu->dev, pmu);
5708 pmu->dev->bus = &pmu_bus;
5709 pmu->dev->release = pmu_dev_release;
5710 ret = device_add(pmu->dev);
5711 if (ret)
5712 goto free_dev;
5713
5714out:
5715 return ret;
5716
5717free_dev:
5718 put_device(pmu->dev);
5719 goto out;
5720}
5721
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005722static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005723static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005724
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005725int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005726{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005727 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005728
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005729 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005730 ret = -ENOMEM;
5731 pmu->pmu_disable_count = alloc_percpu(int);
5732 if (!pmu->pmu_disable_count)
5733 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005734
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005735 pmu->type = -1;
5736 if (!name)
5737 goto skip_type;
5738 pmu->name = name;
5739
5740 if (type < 0) {
5741 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5742 if (!err)
5743 goto free_pdc;
5744
5745 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5746 if (err) {
5747 ret = err;
5748 goto free_pdc;
5749 }
5750 }
5751 pmu->type = type;
5752
Peter Zijlstraabe43402010-11-17 23:17:37 +01005753 if (pmu_bus_running) {
5754 ret = pmu_dev_alloc(pmu);
5755 if (ret)
5756 goto free_idr;
5757 }
5758
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005759skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005760 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5761 if (pmu->pmu_cpu_context)
5762 goto got_cpu_context;
5763
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005764 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5765 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005766 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005767
5768 for_each_possible_cpu(cpu) {
5769 struct perf_cpu_context *cpuctx;
5770
5771 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005772 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005773 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005774 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005775 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005776 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005777 cpuctx->jiffies_interval = 1;
5778 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005779 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005780 }
5781
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005782got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005783 if (!pmu->start_txn) {
5784 if (pmu->pmu_enable) {
5785 /*
5786 * If we have pmu_enable/pmu_disable calls, install
5787 * transaction stubs that use that to try and batch
5788 * hardware accesses.
5789 */
5790 pmu->start_txn = perf_pmu_start_txn;
5791 pmu->commit_txn = perf_pmu_commit_txn;
5792 pmu->cancel_txn = perf_pmu_cancel_txn;
5793 } else {
5794 pmu->start_txn = perf_pmu_nop_void;
5795 pmu->commit_txn = perf_pmu_nop_int;
5796 pmu->cancel_txn = perf_pmu_nop_void;
5797 }
5798 }
5799
5800 if (!pmu->pmu_enable) {
5801 pmu->pmu_enable = perf_pmu_nop_void;
5802 pmu->pmu_disable = perf_pmu_nop_void;
5803 }
5804
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005805 if (!pmu->event_idx)
5806 pmu->event_idx = perf_event_idx_default;
5807
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005808 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005809 ret = 0;
5810unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005811 mutex_unlock(&pmus_lock);
5812
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005813 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005814
Peter Zijlstraabe43402010-11-17 23:17:37 +01005815free_dev:
5816 device_del(pmu->dev);
5817 put_device(pmu->dev);
5818
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005819free_idr:
5820 if (pmu->type >= PERF_TYPE_MAX)
5821 idr_remove(&pmu_idr, pmu->type);
5822
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005823free_pdc:
5824 free_percpu(pmu->pmu_disable_count);
5825 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005826}
5827
5828void perf_pmu_unregister(struct pmu *pmu)
5829{
5830 mutex_lock(&pmus_lock);
5831 list_del_rcu(&pmu->entry);
5832 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005833
5834 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005835 * We dereference the pmu list under both SRCU and regular RCU, so
5836 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005837 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005838 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005839 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005840
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005841 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005842 if (pmu->type >= PERF_TYPE_MAX)
5843 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01005844 device_del(pmu->dev);
5845 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01005846 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005847}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005849struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005850{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005851 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005852 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08005853 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005854
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005855 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005856
5857 rcu_read_lock();
5858 pmu = idr_find(&pmu_idr, event->attr.type);
5859 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08005860 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005861 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005862 ret = pmu->event_init(event);
5863 if (ret)
5864 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005865 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08005866 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005867
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005868 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005869 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005870 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005871 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005872 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005873
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005874 if (ret != -ENOENT) {
5875 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005876 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005877 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005878 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005879 pmu = ERR_PTR(-ENOENT);
5880unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005881 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005882
5883 return pmu;
5884}
5885
5886/*
5887 * Allocate and initialize a event structure
5888 */
5889static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005890perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005891 struct task_struct *task,
5892 struct perf_event *group_leader,
5893 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03005894 perf_overflow_handler_t overflow_handler,
5895 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005897 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005898 struct perf_event *event;
5899 struct hw_perf_event *hwc;
5900 long err;
5901
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005902 if ((unsigned)cpu >= nr_cpu_ids) {
5903 if (!task || cpu != -1)
5904 return ERR_PTR(-EINVAL);
5905 }
5906
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005907 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908 if (!event)
5909 return ERR_PTR(-ENOMEM);
5910
5911 /*
5912 * Single events are their own group leaders, with an
5913 * empty sibling list:
5914 */
5915 if (!group_leader)
5916 group_leader = event;
5917
5918 mutex_init(&event->child_mutex);
5919 INIT_LIST_HEAD(&event->child_list);
5920
5921 INIT_LIST_HEAD(&event->group_entry);
5922 INIT_LIST_HEAD(&event->event_entry);
5923 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005924 INIT_LIST_HEAD(&event->rb_entry);
5925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005926 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005927 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928
5929 mutex_init(&event->mmap_mutex);
5930
Al Virod156b472012-08-20 14:59:25 +01005931 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932 event->cpu = cpu;
5933 event->attr = *attr;
5934 event->group_leader = group_leader;
5935 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005936 event->oncpu = -1;
5937
5938 event->parent = parent_event;
5939
5940 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5941 event->id = atomic64_inc_return(&perf_event_id);
5942
5943 event->state = PERF_EVENT_STATE_INACTIVE;
5944
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005945 if (task) {
5946 event->attach_state = PERF_ATTACH_TASK;
5947#ifdef CONFIG_HAVE_HW_BREAKPOINT
5948 /*
5949 * hw_breakpoint is a bit difficult here..
5950 */
5951 if (attr->type == PERF_TYPE_BREAKPOINT)
5952 event->hw.bp_target = task;
5953#endif
5954 }
5955
Avi Kivity4dc0da82011-06-29 18:42:35 +03005956 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005957 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005958 context = parent_event->overflow_handler_context;
5959 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005960
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005961 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005962 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964 if (attr->disabled)
5965 event->state = PERF_EVENT_STATE_OFF;
5966
5967 pmu = NULL;
5968
5969 hwc = &event->hw;
5970 hwc->sample_period = attr->sample_period;
5971 if (attr->freq && attr->sample_freq)
5972 hwc->sample_period = 1;
5973 hwc->last_period = hwc->sample_period;
5974
Peter Zijlstrae7850592010-05-21 14:43:08 +02005975 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005976
5977 /*
5978 * we currently do not support PERF_FORMAT_GROUP on inherited events
5979 */
5980 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5981 goto done;
5982
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005983 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985done:
5986 err = 0;
5987 if (!pmu)
5988 err = -EINVAL;
5989 else if (IS_ERR(pmu))
5990 err = PTR_ERR(pmu);
5991
5992 if (err) {
5993 if (event->ns)
5994 put_pid_ns(event->ns);
5995 kfree(event);
5996 return ERR_PTR(err);
5997 }
5998
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005999 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006000 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01006001 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01006002 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006003 atomic_inc(&nr_mmap_events);
6004 if (event->attr.comm)
6005 atomic_inc(&nr_comm_events);
6006 if (event->attr.task)
6007 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006008 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6009 err = get_callchain_buffers();
6010 if (err) {
6011 free_event(event);
6012 return ERR_PTR(err);
6013 }
6014 }
Stephane Eraniand010b332012-02-09 23:21:00 +01006015 if (has_branch_stack(event)) {
6016 static_key_slow_inc(&perf_sched_events.key);
6017 if (!(event->attach_state & PERF_ATTACH_TASK))
6018 atomic_inc(&per_cpu(perf_branch_stack_events,
6019 event->cpu));
6020 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006021 }
6022
6023 return event;
6024}
6025
6026static int perf_copy_attr(struct perf_event_attr __user *uattr,
6027 struct perf_event_attr *attr)
6028{
6029 u32 size;
6030 int ret;
6031
6032 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6033 return -EFAULT;
6034
6035 /*
6036 * zero the full structure, so that a short copy will be nice.
6037 */
6038 memset(attr, 0, sizeof(*attr));
6039
6040 ret = get_user(size, &uattr->size);
6041 if (ret)
6042 return ret;
6043
6044 if (size > PAGE_SIZE) /* silly large */
6045 goto err_size;
6046
6047 if (!size) /* abi compat */
6048 size = PERF_ATTR_SIZE_VER0;
6049
6050 if (size < PERF_ATTR_SIZE_VER0)
6051 goto err_size;
6052
6053 /*
6054 * If we're handed a bigger struct than we know of,
6055 * ensure all the unknown bits are 0 - i.e. new
6056 * user-space does not rely on any kernel feature
6057 * extensions we dont know about yet.
6058 */
6059 if (size > sizeof(*attr)) {
6060 unsigned char __user *addr;
6061 unsigned char __user *end;
6062 unsigned char val;
6063
6064 addr = (void __user *)uattr + sizeof(*attr);
6065 end = (void __user *)uattr + size;
6066
6067 for (; addr < end; addr++) {
6068 ret = get_user(val, addr);
6069 if (ret)
6070 return ret;
6071 if (val)
6072 goto err_size;
6073 }
6074 size = sizeof(*attr);
6075 }
6076
6077 ret = copy_from_user(attr, uattr, size);
6078 if (ret)
6079 return -EFAULT;
6080
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306081 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006082 return -EINVAL;
6083
6084 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6085 return -EINVAL;
6086
6087 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6088 return -EINVAL;
6089
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006090 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6091 u64 mask = attr->branch_sample_type;
6092
6093 /* only using defined bits */
6094 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6095 return -EINVAL;
6096
6097 /* at least one branch bit must be set */
6098 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6099 return -EINVAL;
6100
6101 /* kernel level capture: check permissions */
6102 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6103 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6104 return -EACCES;
6105
6106 /* propagate priv level, when not set for branch */
6107 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6108
6109 /* exclude_kernel checked on syscall entry */
6110 if (!attr->exclude_kernel)
6111 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6112
6113 if (!attr->exclude_user)
6114 mask |= PERF_SAMPLE_BRANCH_USER;
6115
6116 if (!attr->exclude_hv)
6117 mask |= PERF_SAMPLE_BRANCH_HV;
6118 /*
6119 * adjust user setting (for HW filter setup)
6120 */
6121 attr->branch_sample_type = mask;
6122 }
6123 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006124out:
6125 return ret;
6126
6127err_size:
6128 put_user(sizeof(*attr), &uattr->size);
6129 ret = -E2BIG;
6130 goto out;
6131}
6132
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006133static int
6134perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006135{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006136 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137 int ret = -EINVAL;
6138
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006139 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006140 goto set;
6141
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006142 /* don't allow circular references */
6143 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144 goto out;
6145
Peter Zijlstra0f139302010-05-20 14:35:15 +02006146 /*
6147 * Don't allow cross-cpu buffers
6148 */
6149 if (output_event->cpu != event->cpu)
6150 goto out;
6151
6152 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006153 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006154 */
6155 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6156 goto out;
6157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006158set:
6159 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006160 /* Can't redirect output if we've got an active mmap() */
6161 if (atomic_read(&event->mmap_count))
6162 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006163
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006164 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006165 /* get the rb we want to redirect to */
6166 rb = ring_buffer_get(output_event);
6167 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006168 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006169 }
6170
Frederic Weisbecker76369132011-05-19 19:55:04 +02006171 old_rb = event->rb;
6172 rcu_assign_pointer(event->rb, rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006173 if (old_rb)
6174 ring_buffer_detach(event, old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006175 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006176unlock:
6177 mutex_unlock(&event->mmap_mutex);
6178
Frederic Weisbecker76369132011-05-19 19:55:04 +02006179 if (old_rb)
6180 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006181out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006182 return ret;
6183}
6184
6185/**
6186 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6187 *
6188 * @attr_uptr: event_id type attributes for monitoring/sampling
6189 * @pid: target pid
6190 * @cpu: target cpu
6191 * @group_fd: group leader event fd
6192 */
6193SYSCALL_DEFINE5(perf_event_open,
6194 struct perf_event_attr __user *, attr_uptr,
6195 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6196{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006197 struct perf_event *group_leader = NULL, *output_event = NULL;
6198 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006199 struct perf_event_attr attr;
6200 struct perf_event_context *ctx;
6201 struct file *event_file = NULL;
6202 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006203 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006204 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006205 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006206 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006207 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006208 int err;
6209
6210 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006211 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006212 return -EINVAL;
6213
6214 err = perf_copy_attr(attr_uptr, &attr);
6215 if (err)
6216 return err;
6217
6218 if (!attr.exclude_kernel) {
6219 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6220 return -EACCES;
6221 }
6222
6223 if (attr.freq) {
6224 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6225 return -EINVAL;
6226 }
6227
Stephane Eraniane5d13672011-02-14 11:20:01 +02006228 /*
6229 * In cgroup mode, the pid argument is used to pass the fd
6230 * opened to the cgroup directory in cgroupfs. The cpu argument
6231 * designates the cpu on which to monitor threads from that
6232 * cgroup.
6233 */
6234 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6235 return -EINVAL;
6236
Al Viroea635c62010-05-26 17:40:29 -04006237 event_fd = get_unused_fd_flags(O_RDWR);
6238 if (event_fd < 0)
6239 return event_fd;
6240
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006241 if (group_fd != -1) {
Al Virod156b472012-08-20 14:59:25 +01006242 group_file = perf_fget_light(group_fd, &fput_needed);
6243 if (IS_ERR(group_file)) {
6244 err = PTR_ERR(group_file);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006245 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006246 }
Al Virod156b472012-08-20 14:59:25 +01006247 group_leader = group_file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006248 if (flags & PERF_FLAG_FD_OUTPUT)
6249 output_event = group_leader;
6250 if (flags & PERF_FLAG_FD_NO_GROUP)
6251 group_leader = NULL;
6252 }
6253
Stephane Eraniane5d13672011-02-14 11:20:01 +02006254 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006255 task = find_lively_task_by_vpid(pid);
6256 if (IS_ERR(task)) {
6257 err = PTR_ERR(task);
6258 goto err_group_fd;
6259 }
6260 }
6261
Avi Kivity4dc0da82011-06-29 18:42:35 +03006262 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6263 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006264 if (IS_ERR(event)) {
6265 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006266 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006267 }
6268
Stephane Eraniane5d13672011-02-14 11:20:01 +02006269 if (flags & PERF_FLAG_PID_CGROUP) {
6270 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6271 if (err)
6272 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006273 /*
6274 * one more event:
6275 * - that has cgroup constraint on event->cpu
6276 * - that may need work on context switch
6277 */
6278 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006279 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006280 }
6281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006282 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006283 * Special case software events and allow them to be part of
6284 * any hardware group.
6285 */
6286 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006287
6288 if (group_leader &&
6289 (is_software_event(event) != is_software_event(group_leader))) {
6290 if (is_software_event(event)) {
6291 /*
6292 * If event and group_leader are not both a software
6293 * event, and event is, then group leader is not.
6294 *
6295 * Allow the addition of software events to !software
6296 * groups, this is safe because software events never
6297 * fail to schedule.
6298 */
6299 pmu = group_leader->pmu;
6300 } else if (is_software_event(group_leader) &&
6301 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6302 /*
6303 * In case the group is a pure software group, and we
6304 * try to add a hardware event, move the whole group to
6305 * the hardware context.
6306 */
6307 move_group = 1;
6308 }
6309 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006310
6311 /*
6312 * Get the target context (task or percpu):
6313 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006314 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006315 if (IS_ERR(ctx)) {
6316 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006317 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006318 }
6319
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006320 if (task) {
6321 put_task_struct(task);
6322 task = NULL;
6323 }
6324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006325 /*
6326 * Look up the group leader (we will attach this event to it):
6327 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006328 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006329 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006330
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006331 /*
6332 * Do not allow a recursive hierarchy (this new sibling
6333 * becoming part of another group-sibling):
6334 */
6335 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006336 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006337 /*
6338 * Do not allow to attach to a group in a different
6339 * task or CPU context:
6340 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006341 if (move_group) {
6342 if (group_leader->ctx->type != ctx->type)
6343 goto err_context;
6344 } else {
6345 if (group_leader->ctx != ctx)
6346 goto err_context;
6347 }
6348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349 /*
6350 * Only a group leader can be exclusive or pinned
6351 */
6352 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006353 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006354 }
6355
6356 if (output_event) {
6357 err = perf_event_set_output(event, output_event);
6358 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006359 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006360 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006361
Al Viroea635c62010-05-26 17:40:29 -04006362 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6363 if (IS_ERR(event_file)) {
6364 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006365 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006366 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006368 if (move_group) {
6369 struct perf_event_context *gctx = group_leader->ctx;
6370
6371 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006372 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006373 list_for_each_entry(sibling, &group_leader->sibling_list,
6374 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006375 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006376 put_ctx(gctx);
6377 }
6378 mutex_unlock(&gctx->mutex);
6379 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380 }
6381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006382 WARN_ON_ONCE(ctx->parent_ctx);
6383 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006384
6385 if (move_group) {
6386 perf_install_in_context(ctx, group_leader, cpu);
6387 get_ctx(ctx);
6388 list_for_each_entry(sibling, &group_leader->sibling_list,
6389 group_entry) {
6390 perf_install_in_context(ctx, sibling, cpu);
6391 get_ctx(ctx);
6392 }
6393 }
6394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395 perf_install_in_context(ctx, event, cpu);
6396 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006397 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006398 mutex_unlock(&ctx->mutex);
6399
6400 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006402 mutex_lock(&current->perf_event_mutex);
6403 list_add_tail(&event->owner_entry, &current->perf_event_list);
6404 mutex_unlock(&current->perf_event_mutex);
6405
Peter Zijlstra8a495422010-05-27 15:47:49 +02006406 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006407 * Precalculate sample_data sizes
6408 */
6409 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006410 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006411
6412 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006413 * Drop the reference on the group_event after placing the
6414 * new event on the sibling_list. This ensures destruction
6415 * of the group leader will find the pointer to itself in
6416 * perf_group_detach().
6417 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006419 fd_install(event_fd, event_file);
6420 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006422err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006423 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006424 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006425err_alloc:
6426 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006427err_task:
6428 if (task)
6429 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006430err_group_fd:
6431 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006432err_fd:
6433 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006434 return err;
6435}
6436
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006437/**
6438 * perf_event_create_kernel_counter
6439 *
6440 * @attr: attributes of the counter to create
6441 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006442 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006443 */
6444struct perf_event *
6445perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006446 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006447 perf_overflow_handler_t overflow_handler,
6448 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006449{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006450 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006451 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006452 int err;
6453
6454 /*
6455 * Get the target context (task or percpu):
6456 */
6457
Avi Kivity4dc0da82011-06-29 18:42:35 +03006458 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6459 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006460 if (IS_ERR(event)) {
6461 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006462 goto err;
6463 }
6464
Matt Helsley38a81da2010-09-13 13:01:20 -07006465 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006466 if (IS_ERR(ctx)) {
6467 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006468 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006469 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006470
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006471 WARN_ON_ONCE(ctx->parent_ctx);
6472 mutex_lock(&ctx->mutex);
6473 perf_install_in_context(ctx, event, cpu);
6474 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006475 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006476 mutex_unlock(&ctx->mutex);
6477
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006478 return event;
6479
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006480err_free:
6481 free_event(event);
6482err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006483 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006484}
6485EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006487static void sync_child_event(struct perf_event *child_event,
6488 struct task_struct *child)
6489{
6490 struct perf_event *parent_event = child_event->parent;
6491 u64 child_val;
6492
6493 if (child_event->attr.inherit_stat)
6494 perf_event_read_event(child_event, child);
6495
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006496 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497
6498 /*
6499 * Add back the child's count to the parent's count:
6500 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006501 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006502 atomic64_add(child_event->total_time_enabled,
6503 &parent_event->child_total_time_enabled);
6504 atomic64_add(child_event->total_time_running,
6505 &parent_event->child_total_time_running);
6506
6507 /*
6508 * Remove this event from the parent's list
6509 */
6510 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6511 mutex_lock(&parent_event->child_mutex);
6512 list_del_init(&child_event->child_list);
6513 mutex_unlock(&parent_event->child_mutex);
6514
6515 /*
6516 * Release the parent event, if this was the last
6517 * reference to it.
6518 */
Al Virod156b472012-08-20 14:59:25 +01006519 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520}
6521
6522static void
6523__perf_event_exit_task(struct perf_event *child_event,
6524 struct perf_event_context *child_ctx,
6525 struct task_struct *child)
6526{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006527 if (child_event->parent) {
6528 raw_spin_lock_irq(&child_ctx->lock);
6529 perf_group_detach(child_event);
6530 raw_spin_unlock_irq(&child_ctx->lock);
6531 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006533 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006534
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006536 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006538 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006540 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541 sync_child_event(child_event, child);
6542 free_event(child_event);
6543 }
6544}
6545
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006546static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006547{
6548 struct perf_event *child_event, *tmp;
6549 struct perf_event_context *child_ctx;
6550 unsigned long flags;
6551
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006552 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006553 perf_event_task(child, NULL, 0);
6554 return;
6555 }
6556
6557 local_irq_save(flags);
6558 /*
6559 * We can't reschedule here because interrupts are disabled,
6560 * and either child is current or it is a task that can't be
6561 * scheduled, so we are now safe from rescheduling changing
6562 * our context.
6563 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006564 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006565
6566 /*
6567 * Take the context lock here so that if find_get_context is
6568 * reading child->perf_event_ctxp, we wait until it has
6569 * incremented the context's refcount before we do put_ctx below.
6570 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006571 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006572 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006573 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006574 /*
6575 * If this context is a clone; unclone it so it can't get
6576 * swapped to another process while we're removing all
6577 * the events from it.
6578 */
6579 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006580 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006581 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006582
6583 /*
6584 * Report the task dead after unscheduling the events so that we
6585 * won't get any samples after PERF_RECORD_EXIT. We can however still
6586 * get a few PERF_RECORD_READ events.
6587 */
6588 perf_event_task(child, child_ctx, 0);
6589
6590 /*
6591 * We can recurse on the same lock type through:
6592 *
6593 * __perf_event_exit_task()
6594 * sync_child_event()
Al Virod156b472012-08-20 14:59:25 +01006595 * put_event()
6596 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006597 *
6598 * But since its the parent context it won't be the same instance.
6599 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006600 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601
6602again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006603 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6604 group_entry)
6605 __perf_event_exit_task(child_event, child_ctx, child);
6606
6607 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006608 group_entry)
6609 __perf_event_exit_task(child_event, child_ctx, child);
6610
6611 /*
6612 * If the last event was a group event, it will have appended all
6613 * its siblings to the list, but we obtained 'tmp' before that which
6614 * will still point to the list head terminating the iteration.
6615 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006616 if (!list_empty(&child_ctx->pinned_groups) ||
6617 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006618 goto again;
6619
6620 mutex_unlock(&child_ctx->mutex);
6621
6622 put_ctx(child_ctx);
6623}
6624
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006625/*
6626 * When a child task exits, feed back event values to parent events.
6627 */
6628void perf_event_exit_task(struct task_struct *child)
6629{
Peter Zijlstra88821352010-11-09 19:01:43 +01006630 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006631 int ctxn;
6632
Peter Zijlstra88821352010-11-09 19:01:43 +01006633 mutex_lock(&child->perf_event_mutex);
6634 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6635 owner_entry) {
6636 list_del_init(&event->owner_entry);
6637
6638 /*
6639 * Ensure the list deletion is visible before we clear
6640 * the owner, closes a race against perf_release() where
6641 * we need to serialize on the owner->perf_event_mutex.
6642 */
6643 smp_wmb();
6644 event->owner = NULL;
6645 }
6646 mutex_unlock(&child->perf_event_mutex);
6647
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006648 for_each_task_context_nr(ctxn)
6649 perf_event_exit_task_context(child, ctxn);
6650}
6651
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006652static void perf_free_event(struct perf_event *event,
6653 struct perf_event_context *ctx)
6654{
6655 struct perf_event *parent = event->parent;
6656
6657 if (WARN_ON_ONCE(!parent))
6658 return;
6659
6660 mutex_lock(&parent->child_mutex);
6661 list_del_init(&event->child_list);
6662 mutex_unlock(&parent->child_mutex);
6663
Al Virod156b472012-08-20 14:59:25 +01006664 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006665
Peter Zijlstra8a495422010-05-27 15:47:49 +02006666 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006667 list_del_event(event, ctx);
6668 free_event(event);
6669}
6670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671/*
6672 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006673 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006674 */
6675void perf_event_free_task(struct task_struct *task)
6676{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006677 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006678 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006679 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006680
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006681 for_each_task_context_nr(ctxn) {
6682 ctx = task->perf_event_ctxp[ctxn];
6683 if (!ctx)
6684 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006685
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006686 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006688 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6689 group_entry)
6690 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006691
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006692 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6693 group_entry)
6694 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006695
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006696 if (!list_empty(&ctx->pinned_groups) ||
6697 !list_empty(&ctx->flexible_groups))
6698 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006700 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006701
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006702 put_ctx(ctx);
6703 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006704}
6705
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006706void perf_event_delayed_put(struct task_struct *task)
6707{
6708 int ctxn;
6709
6710 for_each_task_context_nr(ctxn)
6711 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6712}
6713
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006714/*
6715 * inherit a event from parent task to child task:
6716 */
6717static struct perf_event *
6718inherit_event(struct perf_event *parent_event,
6719 struct task_struct *parent,
6720 struct perf_event_context *parent_ctx,
6721 struct task_struct *child,
6722 struct perf_event *group_leader,
6723 struct perf_event_context *child_ctx)
6724{
6725 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006726 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006727
6728 /*
6729 * Instead of creating recursive hierarchies of events,
6730 * we link inherited events back to the original parent,
6731 * which has a filp for sure, which we use as the reference
6732 * count:
6733 */
6734 if (parent_event->parent)
6735 parent_event = parent_event->parent;
6736
6737 child_event = perf_event_alloc(&parent_event->attr,
6738 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006739 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006740 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006741 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006742 if (IS_ERR(child_event))
6743 return child_event;
Al Virod156b472012-08-20 14:59:25 +01006744
6745 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6746 free_event(child_event);
6747 return NULL;
6748 }
6749
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006750 get_ctx(child_ctx);
6751
6752 /*
6753 * Make the child state follow the state of the parent event,
6754 * not its attr.disabled bit. We hold the parent's mutex,
6755 * so we won't race with perf_event_{en, dis}able_family.
6756 */
6757 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6758 child_event->state = PERF_EVENT_STATE_INACTIVE;
6759 else
6760 child_event->state = PERF_EVENT_STATE_OFF;
6761
6762 if (parent_event->attr.freq) {
6763 u64 sample_period = parent_event->hw.sample_period;
6764 struct hw_perf_event *hwc = &child_event->hw;
6765
6766 hwc->sample_period = sample_period;
6767 hwc->last_period = sample_period;
6768
6769 local64_set(&hwc->period_left, sample_period);
6770 }
6771
6772 child_event->ctx = child_ctx;
6773 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006774 child_event->overflow_handler_context
6775 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006776
6777 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006778 * Precalculate sample_data sizes
6779 */
6780 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006781 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006782
6783 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006784 * Link it up in the child's context:
6785 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006786 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006787 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006788 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006789
6790 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006791 * Link this into the parent event's child list
6792 */
6793 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6794 mutex_lock(&parent_event->child_mutex);
6795 list_add_tail(&child_event->child_list, &parent_event->child_list);
6796 mutex_unlock(&parent_event->child_mutex);
6797
6798 return child_event;
6799}
6800
6801static int inherit_group(struct perf_event *parent_event,
6802 struct task_struct *parent,
6803 struct perf_event_context *parent_ctx,
6804 struct task_struct *child,
6805 struct perf_event_context *child_ctx)
6806{
6807 struct perf_event *leader;
6808 struct perf_event *sub;
6809 struct perf_event *child_ctr;
6810
6811 leader = inherit_event(parent_event, parent, parent_ctx,
6812 child, NULL, child_ctx);
6813 if (IS_ERR(leader))
6814 return PTR_ERR(leader);
6815 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6816 child_ctr = inherit_event(sub, parent, parent_ctx,
6817 child, leader, child_ctx);
6818 if (IS_ERR(child_ctr))
6819 return PTR_ERR(child_ctr);
6820 }
6821 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006822}
6823
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006824static int
6825inherit_task_group(struct perf_event *event, struct task_struct *parent,
6826 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006827 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006828 int *inherited_all)
6829{
6830 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006831 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006832
6833 if (!event->attr.inherit) {
6834 *inherited_all = 0;
6835 return 0;
6836 }
6837
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006838 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006839 if (!child_ctx) {
6840 /*
6841 * This is executed from the parent task context, so
6842 * inherit events that have been marked for cloning.
6843 * First allocate and initialize a context for the
6844 * child.
6845 */
6846
Peter Zijlstraeb184472010-09-07 15:55:13 +02006847 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006848 if (!child_ctx)
6849 return -ENOMEM;
6850
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006851 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006852 }
6853
6854 ret = inherit_group(event, parent, parent_ctx,
6855 child, child_ctx);
6856
6857 if (ret)
6858 *inherited_all = 0;
6859
6860 return ret;
6861}
6862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006863/*
6864 * Initialize the perf_event context in task_struct
6865 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006866int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006867{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006868 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006869 struct perf_event_context *cloned_ctx;
6870 struct perf_event *event;
6871 struct task_struct *parent = current;
6872 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006873 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006874 int ret = 0;
6875
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006876 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006877 return 0;
6878
6879 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006880 * If the parent's context is a clone, pin it so it won't get
6881 * swapped under us.
6882 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006883 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884
6885 /*
6886 * No need to check if parent_ctx != NULL here; since we saw
6887 * it non-NULL earlier, the only reason for it to become NULL
6888 * is if we exit, and since we're currently in the middle of
6889 * a fork we can't be exiting at the same time.
6890 */
6891
6892 /*
6893 * Lock the parent list. No need to lock the child - not PID
6894 * hashed yet and not running, so nobody can access it.
6895 */
6896 mutex_lock(&parent_ctx->mutex);
6897
6898 /*
6899 * We dont have to disable NMIs - we are only looking at
6900 * the list, not manipulating it:
6901 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006902 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006903 ret = inherit_task_group(event, parent, parent_ctx,
6904 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006905 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006906 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006907 }
6908
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006909 /*
6910 * We can't hold ctx->lock when iterating the ->flexible_group list due
6911 * to allocations, but we need to prevent rotation because
6912 * rotate_ctx() will change the list from interrupt context.
6913 */
6914 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6915 parent_ctx->rotate_disable = 1;
6916 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6917
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006918 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006919 ret = inherit_task_group(event, parent, parent_ctx,
6920 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006921 if (ret)
6922 break;
6923 }
6924
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006925 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6926 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006927
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006928 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006929
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006930 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006931 /*
6932 * Mark the child context as a clone of the parent
6933 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006934 *
6935 * Note that if the parent is a clone, the holding of
6936 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006937 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006938 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006939 if (cloned_ctx) {
6940 child_ctx->parent_ctx = cloned_ctx;
6941 child_ctx->parent_gen = parent_ctx->parent_gen;
6942 } else {
6943 child_ctx->parent_ctx = parent_ctx;
6944 child_ctx->parent_gen = parent_ctx->generation;
6945 }
6946 get_ctx(child_ctx->parent_ctx);
6947 }
6948
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006949 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 mutex_unlock(&parent_ctx->mutex);
6951
6952 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006953 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006954
6955 return ret;
6956}
6957
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006958/*
6959 * Initialize the perf_event context in task_struct
6960 */
6961int perf_event_init_task(struct task_struct *child)
6962{
6963 int ctxn, ret;
6964
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01006965 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6966 mutex_init(&child->perf_event_mutex);
6967 INIT_LIST_HEAD(&child->perf_event_list);
6968
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006969 for_each_task_context_nr(ctxn) {
6970 ret = perf_event_init_context(child, ctxn);
6971 if (ret)
6972 return ret;
6973 }
6974
6975 return 0;
6976}
6977
Paul Mackerras220b1402010-03-10 20:45:52 +11006978static void __init perf_event_init_all_cpus(void)
6979{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006980 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006981 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006982
6983 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006984 swhash = &per_cpu(swevent_htable, cpu);
6985 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006986 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006987 }
6988}
6989
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990static void __cpuinit perf_event_init_cpu(int cpu)
6991{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006992 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006993
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006994 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07006995 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006996 struct swevent_hlist *hlist;
6997
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006998 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6999 WARN_ON(!hlist);
7000 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007001 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007002 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007003}
7004
Peter Zijlstrac2774432010-12-08 15:29:02 +01007005#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007006static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007007{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007008 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7009
7010 WARN_ON(!irqs_disabled());
7011
7012 list_del_init(&cpuctx->rotation_list);
7013}
7014
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007015static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007017 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007018 struct perf_event *event, *tmp;
7019
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007020 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007021
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007022 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007023 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007024 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007025 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007026}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007027
7028static void perf_event_exit_cpu_context(int cpu)
7029{
7030 struct perf_event_context *ctx;
7031 struct pmu *pmu;
7032 int idx;
7033
7034 idx = srcu_read_lock(&pmus_srcu);
7035 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007036 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007037
7038 mutex_lock(&ctx->mutex);
7039 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7040 mutex_unlock(&ctx->mutex);
7041 }
7042 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007043}
7044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045static void perf_event_exit_cpu(int cpu)
7046{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007047 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007048
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007049 mutex_lock(&swhash->hlist_mutex);
7050 swevent_hlist_release(swhash);
7051 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007052
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007053 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054}
7055#else
7056static inline void perf_event_exit_cpu(int cpu) { }
7057#endif
7058
Peter Zijlstrac2774432010-12-08 15:29:02 +01007059static int
7060perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7061{
7062 int cpu;
7063
7064 for_each_online_cpu(cpu)
7065 perf_event_exit_cpu(cpu);
7066
7067 return NOTIFY_OK;
7068}
7069
7070/*
7071 * Run the perf reboot notifier at the very last possible moment so that
7072 * the generic watchdog code runs as long as possible.
7073 */
7074static struct notifier_block perf_reboot_notifier = {
7075 .notifier_call = perf_reboot,
7076 .priority = INT_MIN,
7077};
7078
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007079static int __cpuinit
7080perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7081{
7082 unsigned int cpu = (long)hcpu;
7083
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007084 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007085
7086 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007087 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007088 perf_event_init_cpu(cpu);
7089 break;
7090
Peter Zijlstra5e116372010-06-11 13:35:08 +02007091 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007092 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007093 perf_event_exit_cpu(cpu);
7094 break;
7095
7096 default:
7097 break;
7098 }
7099
7100 return NOTIFY_OK;
7101}
7102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007103void __init perf_event_init(void)
7104{
Jason Wessel3c502e72010-11-04 17:33:01 -05007105 int ret;
7106
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007107 idr_init(&pmu_idr);
7108
Paul Mackerras220b1402010-03-10 20:45:52 +11007109 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007110 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007111 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7112 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7113 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007114 perf_tp_register();
7115 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007116 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007117
7118 ret = init_hw_breakpoint();
7119 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007120
7121 /* do not patch jump label more than once per second */
7122 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007123
7124 /*
7125 * Build time assertion that we keep the data_head at the intended
7126 * location. IOW, validation we got the __reserved[] size right.
7127 */
7128 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7129 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007130}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007131
7132static int __init perf_event_sysfs_init(void)
7133{
7134 struct pmu *pmu;
7135 int ret;
7136
7137 mutex_lock(&pmus_lock);
7138
7139 ret = bus_register(&pmu_bus);
7140 if (ret)
7141 goto unlock;
7142
7143 list_for_each_entry(pmu, &pmus, entry) {
7144 if (!pmu->name || pmu->type < 0)
7145 continue;
7146
7147 ret = pmu_dev_alloc(pmu);
7148 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7149 }
7150 pmu_bus_running = 1;
7151 ret = 0;
7152
7153unlock:
7154 mutex_unlock(&pmus_lock);
7155
7156 return ret;
7157}
7158device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007159
7160#ifdef CONFIG_CGROUP_PERF
Li Zefan761b3ef2012-01-31 13:47:36 +08007161static struct cgroup_subsys_state *perf_cgroup_create(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007162{
7163 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007164
Li Zefan1b15d052011-03-03 14:26:06 +08007165 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007166 if (!jc)
7167 return ERR_PTR(-ENOMEM);
7168
Stephane Eraniane5d13672011-02-14 11:20:01 +02007169 jc->info = alloc_percpu(struct perf_cgroup_info);
7170 if (!jc->info) {
7171 kfree(jc);
7172 return ERR_PTR(-ENOMEM);
7173 }
7174
Stephane Eraniane5d13672011-02-14 11:20:01 +02007175 return &jc->css;
7176}
7177
Li Zefan761b3ef2012-01-31 13:47:36 +08007178static void perf_cgroup_destroy(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007179{
7180 struct perf_cgroup *jc;
7181 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7182 struct perf_cgroup, css);
7183 free_percpu(jc->info);
7184 kfree(jc);
7185}
7186
7187static int __perf_cgroup_move(void *info)
7188{
7189 struct task_struct *task = info;
7190 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7191 return 0;
7192}
7193
Li Zefan761b3ef2012-01-31 13:47:36 +08007194static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007195{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007196 struct task_struct *task;
7197
7198 cgroup_taskset_for_each(task, cgrp, tset)
7199 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007200}
7201
Li Zefan761b3ef2012-01-31 13:47:36 +08007202static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7203 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007204{
7205 /*
7206 * cgroup_exit() is called in the copy_process() failure path.
7207 * Ignore this case since the task hasn't ran yet, this avoids
7208 * trying to poke a half freed task state from generic code.
7209 */
7210 if (!(task->flags & PF_EXITING))
7211 return;
7212
Tejun Heobb9d97b2011-12-12 18:12:21 -08007213 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007214}
7215
7216struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007217 .name = "perf_event",
7218 .subsys_id = perf_subsys_id,
7219 .create = perf_cgroup_create,
7220 .destroy = perf_cgroup_destroy,
7221 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007222 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007223};
7224#endif /* CONFIG_CGROUP_PERF */